datafun
play

Datafun Michael Arntzenius 1 Neel Krishnaswami 2 1 University of - PowerPoint PPT Presentation

Datafun Michael Arntzenius 1 Neel Krishnaswami 2 1 University of Birmingham 2 University of Cambridge ICFP 2016 1 2 3 4 # can be replaced by print 0 print c 5 # but this cant print x 6 1 x = 0 x := 0 2 3 4 print c 5 print x 6


  1. Datafun Michael Arntzenius 1 Neel Krishnaswami 2 1 University of Birmingham 2 University of Cambridge ICFP 2016

  2. 1 2 3 4 # can be replaced by print 0 print c 5 # but this can’t print x 6

  3. 1 x = 0 x := 0 2 3 4 print c 5 print x 6

  4. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 4 print c 5 print x 6

  5. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = 0, c = 0 while true do 4 print c 5 print x 6

  6. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = 0, c = 0 while true do 4 x = 0, c = 0 print c 5 print x 6

  7. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = 0, c = 0 while true do 4 x = 0, c = 0 print c 5 x = 0, c = 0 print x 6

  8. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = 0, c = 0 while true do 4 x = 0, c = 0 print c 5 x = 0, c = 0 print x 6 x = 1, c = 0 x += 1

  9. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = 0, c = 0 while true do 4 x = 0, c = 0 print c 5 x = 0 , c = 0 print x 6 x = 1, c = 0 x += 1

  10. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = ⊤ , c = 0 while true do 4 x = 0, c = 0 print c 5 x = 0, c = 0 print x 6 x = 1, c = 0 x += 1

  11. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = ⊤ , c = 0 while true do 4 x = ⊤ , c = 0 print c 5 x = 0, c = 0 print x 6 x = 1, c = 0 x += 1

  12. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = ⊤ , c = 0 while true do 4 x = ⊤ , c = 0 print c 5 x = ⊤ , c = 0 print x 6 x = 1, c = 0 x += 1

  13. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = ⊤ , c = 0 while true do 4 x = ⊤ , c = 0 print c 5 x = ⊤ , c = 0 print x x = ⊤ , c = 0 6 x += 1

  14. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = ⊤ , c = 0 while true do 4 x = ⊤ , c = 0 print c 5 x = ⊤ , c = 0 print x x = ⊤ , c = 0 6 x += 1

  15. 1 x = 0 x := 0 2 x = 0, c = 0 c := x 3 x = ⊤ , c = 0 while true do 4 x = ⊤ , c = 0 print c 5 x = ⊤ , c = 0 print x x = ⊤ , c = 0 6 x += 1

  16. Compute fixed points of monotone maps on semilattices satisfying an ascending chain condition

  17. ◮ Fixed point : Keep going until nothing changes. ◮ Monotone : Unidirectional: ⊥ ⇒ constant ⇒ ⊤ ◮ Semilattice : ⊤ (varying) . . . -2 . . . -1 0 1 2 (constants) (undefined) ⊥ ◮ ACC : Can’t go up forever.

  18. Examples of computing fixed points of monotone maps on semilattices satisfying an ascending chain condition : ◮ Static analyses ◮ Graph algorithms: reachability, shortest path, ... ◮ Parsing context-free grammars ◮ Datalog (as long as the semilattice is finite sets)

  19. Datafun is: ◮ a simply typed λ -calculus ◮ where types are posets & some are semilattices ◮ that tracks monotonicity via types ◮ to let you compute fixed points ◮ and know they terminate.

  20. Types as posets Type Meaning Ordering naturals 0 < 1 < 2 < . . . N 2 booleans false < true ⊆ { A } finite subsets of A A → B functions pointwise + A → B monotone functions pointwise

  21. Types as posets Type Meaning Ordering naturals 0 < 1 < 2 < . . . N 2 booleans false < true ⊆ { A } finite subsets of A A → B functions pointwise + A → B monotone functions pointwise + member : N → { N } → 2 member x s = ∃ ( y ∈ s ) x = y

  22. Tracking monotonicity ◮ Two types of function : discrete or monotone ◮ Two kinds of variable : discrete or monotone ◮ Two typing contexts : ∆ discrete, Γ monotone ∆ ; Γ ⊢ e : A “e has type A with free variables ∆, Γ ; moreover, e is monotone in Γ .”

  23. Tracking monotonicity: Function application + ∆ ; Γ ⊢ f : A ∆ ; Γ ⊢ a : A → B monotone app ∆ ; Γ ⊢ f a : B

  24. Tracking monotonicity: Function application + ∆ ; Γ ⊢ f : A ∆ ; Γ ⊢ a : A → B monotone app ∆ ; Γ ⊢ f a : B ∆ ; Γ ⊢ f : A → B ∆ ; ∅ ⊢ a : A discrete app ∆ ; Γ ⊢ f a : B

  25. Tracking monotonicity: Function application + ∆ ; Γ ⊢ f : A ∆ ; Γ ⊢ a : A → B monotone app ∆ ; Γ ⊢ f a : B ∆ ; Γ ⊢ f : A → B ∆ ; ∅ ⊢ a : A discrete app ∆ ; Γ ⊢ f a : B Otherwise: + coerce : ( N → N ) → ( N → N ) coerce f x = f x

  26. Tracking monotonicity: Finite sets e ::= ... | {} | e ∪ e | { e } | � ( x ∈ e ) e

  27. Tracking monotonicity: Finite sets e ::= ... | {} | e ∪ e | { e } | � ( x ∈ e ) e ∆ ; ∅ ⊢ e : A ∆ ; Γ ⊢ { e } : { A }

  28. Tracking monotonicity: Finite sets e ::= ... | {} | e ∪ e | { e } | � ( x ∈ e ) e { e | x ∈ e , ... } | ∆ ; ∅ ⊢ e : A ∆ ; Γ ⊢ { e } : { A }

  29. Example: Relational composition + + ( • ) : { A × eq B × C } → { A × C } B } → { eq s • t = { ( x , z ) | ( x , y ) ∈ s , (! y , z ) ∈ t }

  30. Fixed points fix x is e

  31. Fixed points fix x is e L ⊢ e : ∆ ; Γ, x : L fin fin ∆ ; Γ ⊢ fix x is e : L fin A monotone map on a finite semilattice with decidable equality .

  32. Fixed points fix x is e L ⊢ e : ∆ ; Γ, x : L fin fin ∆ ; Γ ⊢ fix x is e : L fin A monotone map on a finite semilattice with decidable equality .

  33. Example: Reachability + path : { A × A } → { A × A } fin fin fin fin path E = fix P is E ∪ ( P • P ) In Datalog: path(X,Y) :- edge(X,Y). path(X,Z) :- path(X,Y), path(Y,Z).

  34. Example: CYK Parsing Nonterminals: A , B , C ... Literal strings: s , t , ... Rules are all of the form A → B C or A → s .

  35. Example: CYK Parsing Nonterminals: A , B , C ... Literal strings: s , t , ... Rules are all of the form A → B C or A → s . Apply the following inference rules to saturation :

  36. Example: CYK Parsing Nonterminals: A , B , C ... Literal strings: s , t , ... Rules are all of the form A → B C or A → s . Apply the following inference rules to saturation : w [ i .. j ] = s B ( i , j ) C ( j , k ) A → s A → B C A ( i , j ) A ( i , k ) where A ( i , j ) = “ A produces the substring w [ i .. j ] ” and w is the input string

  37. Example: CYK Parsing type rule = Concat ( symbol , symbol ) | String ( string ) type grammar = { symbol × rule } type fact = symbol × N × N step : string → grammar → { fact } + → { fact } step w G prev = { ( a , i , k ) | ( a , Concat ( b , c )) ∈ G , (! b , i , j ) ∈ prev , (! c , ! j , k ) ∈ prev } ∪ { ( a , i , i + length s ) | ( a , String ( s )) ∈ G , i ∈ range 0 ( length w − length s ) , s = substring w i ( i + length s ) }

  38. Summary ◮ Many algorithms are concisely expressed as fixed points of monotone maps on semilattices . ◮ Datafun is a simple, pure, and total language for computing these fixed points. ◮ Key idea: track monotonicity with types! ◮ Has a simple denotational semantics (in paper) & prototype implementation (on github). ◮ Generalizes Datalog to other semilattices. rntz.net/datafun

  39. Fin.

  40. Future work ◮ Optimization ◮ Semi-na¨ ıve evaluation ◮ Dataflow (push & pull) ◮ Magic sets ◮ More semilattice types ◮ More flexible termination/ACC checking ◮ Aggregation operations (summing, averaging) ◮ Commutative monoids? ◮ Other applications of types for monotonicity ◮ Types for functoriality? ◮ LVars & monotone processes

  41. Datafun vs Datalog Datafun pros Datalog pros Functional abstraction! Finiteness/ACC is automatic Semilattices other than P fin Often more concise Can do arithmetic Existing optimization literature Can nest sets See also : Flix , PLDI 2016, Madsen et al

  42. Datafun vs Flix Datafun pros Flix pros Functions on relations Programmer-defined semilattices Types for monotonicity No types for monotonicity

  43. Boolean elimination in a monotone world ∆ ; ∅ ⊢ e 1 : 2 ∆ ; Γ ⊢ e 2 : A ∆ ; Γ ⊢ e 3 : A Discrete If ∆ ; Γ ⊢ if e 1 then e 2 else e 3 : A ∆ ; Γ ⊢ e 1 : 2 ∆ ; Γ ⊢ e 2 : L ∆ ; Γ ⊢ if e 1 then e 2 else ε : L Monotone If For example: + + guard : 2 → { A } → { A } guard c s = if c then s else {}

More recommend