SLIDE 1 Datafun
Michael Arntzenius1 Neel Krishnaswami2
1University of Birmingham 2University of Cambridge
ICFP 2016
SLIDE 2
1 2 3 4 print c # can be replaced by print 0 5 print x # but this can’t 6
SLIDE 3
1 x := 0 x = 0 2 3 4 print c 5 print x 6
SLIDE 4
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 4 print c 5 print x 6
SLIDE 5
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = 0, c = 0 4 print c 5 print x 6
SLIDE 6
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = 0, c = 0 4 print c x = 0, c = 0 5 print x 6
SLIDE 7
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = 0, c = 0 4 print c x = 0, c = 0 5 print x x = 0, c = 0 6
SLIDE 8
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = 0, c = 0 4 print c x = 0, c = 0 5 print x x = 0, c = 0 6 x += 1 x = 1, c = 0
SLIDE 9
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = 0, c = 0 4 print c x = 0, c = 0 5 print x x = 0, c = 0 6 x += 1 x = 1, c = 0
SLIDE 10
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = ⊤, c = 0 4 print c x = 0, c = 0 5 print x x = 0, c = 0 6 x += 1 x = 1, c = 0
SLIDE 11
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = ⊤, c = 0 4 print c x = ⊤, c = 0 5 print x x = 0, c = 0 6 x += 1 x = 1, c = 0
SLIDE 12
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = ⊤, c = 0 4 print c x = ⊤, c = 0 5 print x x = ⊤, c = 0 6 x += 1 x = 1, c = 0
SLIDE 13
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = ⊤, c = 0 4 print c x = ⊤, c = 0 5 print x x = ⊤, c = 0 6 x += 1 x = ⊤, c = 0
SLIDE 14
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = ⊤, c = 0 4 print c x = ⊤, c = 0 5 print x x = ⊤, c = 0 6 x += 1 x = ⊤, c = 0
SLIDE 15
1 x := 0 x = 0 2 c := x x = 0, c = 0 3 while true do x = ⊤, c = 0 4 print c x = ⊤, c = 0 5 print x x = ⊤, c = 0 6 x += 1 x = ⊤, c = 0
SLIDE 16 Compute fixed points
- f monotone maps
- n semilattices
satisfying an ascending chain condition
SLIDE 17 ◮ Fixed point: Keep going until nothing changes. ◮ Monotone: Unidirectional: ⊥ ⇒ constant ⇒ ⊤ ◮ Semilattice:
⊤ ⊥ . . . -2
1 2 . . .
(varying) (constants) (undefined)
◮ ACC: Can’t go up forever.
SLIDE 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)
SLIDE 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.
SLIDE 20 Types as posets Type Meaning Ordering N naturals 0 < 1 < 2 < . . . 2 booleans false < true {A} finite subsets of A ⊆ A → B functions pointwise A
+
→ B monotone functions pointwise
SLIDE 21 Types as posets Type Meaning Ordering N naturals 0 < 1 < 2 < . . . 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
SLIDE 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 Γ.”
SLIDE 23 Tracking monotonicity: Function application ∆; Γ ⊢ f : A
+
→ B ∆; Γ ⊢ a : A ∆; Γ ⊢ f a : B
monotone app
SLIDE 24 Tracking monotonicity: Function application ∆; Γ ⊢ f : A
+
→ B ∆; Γ ⊢ a : A ∆; Γ ⊢ f a : B
monotone app
∆; Γ ⊢ f : A → B ∆; ∅ ⊢ a : A ∆; Γ ⊢ f a : B
discrete app
SLIDE 25 Tracking monotonicity: Function application ∆; Γ ⊢ f : A
+
→ B ∆; Γ ⊢ a : A ∆; Γ ⊢ f a : B
monotone app
∆; Γ ⊢ f : A → B ∆; ∅ ⊢ a : A ∆; Γ ⊢ f a : B
discrete app
Otherwise: coerce : (N → N) → (N
+
→ N) coerce f x = f x
SLIDE 26
Tracking monotonicity: Finite sets
e ::= ... | {} | e ∪ e | {e} | (x ∈ e) e
SLIDE 27
Tracking monotonicity: Finite sets
e ::= ... | {} | e ∪ e | {e} | (x ∈ e) e ∆; ∅ ⊢ e : A ∆; Γ ⊢ {e} : {A}
SLIDE 28
Tracking monotonicity: Finite sets
e ::= ... | {} | e ∪ e | {e} | (x ∈ e) e | {e | x ∈ e, ...} ∆; ∅ ⊢ e : A ∆; Γ ⊢ {e} : {A}
SLIDE 29
Example: Relational composition
(•) : {A × eq B}
+
→ {eq B × C}
+
→ {A × C} s • t = {(x, z) | (x, y) ∈ s, (!y, z) ∈ t}
SLIDE 30
Fixed points
fix x is e
SLIDE 31 Fixed points
fix x is e ∆; Γ, x:
fin
L ⊢ e :
fin
L ∆; Γ ⊢ fix x is e :
fin
L
A monotone map on a finite semilattice with decidable equality.
SLIDE 32 Fixed points
fix x is e ∆; Γ, x:
fin
L ⊢ e :
fin
L ∆; Γ ⊢ fix x is e :
fin
L
A monotone map on a finite semilattice with decidable equality.
SLIDE 33 Example: Reachability path : {
fin
A ×
fin
A}
+
→ {
fin
A ×
fin
A} 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).
SLIDE 34 Example: CYK Parsing
Nonterminals: A, B, C... Literal strings: s, t, ... Rules are all of the form A → B C or A → s.
SLIDE 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:
SLIDE 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:
A → s w[i..j] = s A(i, j) A → B C B(i, j) C(j, k) A(i, k)
where A(i, j) = “A produces the substring w[i..j]” and w is the input string
SLIDE 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)}
SLIDE 38 Summary
◮ Many algorithms are concisely expressed as fixed points
- f 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
SLIDE 39
Fin.
SLIDE 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
SLIDE 41 Datafun vs Datalog
Datafun pros Datalog pros Functional abstraction! Finiteness/ACC is automatic Semilattices other than Pfin Often more concise Can do arithmetic Existing optimization literature Can nest sets See also: Flix, PLDI 2016, Madsen et al
SLIDE 42 Datafun vs Flix
Datafun pros Flix pros Functions on relations Programmer-defined semilattices Types for monotonicity No types for monotonicity
SLIDE 43 Boolean elimination in a monotone world
∆; ∅ ⊢ e1 : 2 ∆; Γ ⊢ e2 : A ∆; Γ ⊢ e3 : A ∆; Γ ⊢ if e1 then e2 else e3 : A Discrete If ∆; Γ ⊢ e1 : 2 ∆; Γ ⊢ e2 : L ∆; Γ ⊢ if e1 then e2 else ε : L Monotone If For example: guard : 2
+
→ {A}
+
→ {A} guard c s = if c then s else {}