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

datafun
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Datafun

Michael Arntzenius1 Neel Krishnaswami2

1University of Birmingham 2University of Cambridge

ICFP 2016

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
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
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
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
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
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
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
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
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
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
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
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
SLIDE 16

Compute fixed points

  • f monotone maps
  • n semilattices

satisfying an ascending chain condition

slide-17
SLIDE 17

◮ Fixed point: Keep going until nothing changes. ◮ Monotone: Unidirectional: ⊥ ⇒ constant ⇒ ⊤ ◮ Semilattice:

⊤ ⊥ . . . -2

  • 1

1 2 . . .

(varying) (constants) (undefined)

◮ ACC: Can’t go up forever.

slide-18
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
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
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
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
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
SLIDE 23

Tracking monotonicity: Function application ∆; Γ ⊢ f : A

+

→ B ∆; Γ ⊢ a : A ∆; Γ ⊢ f a : B

monotone app

slide-24
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
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
SLIDE 26

Tracking monotonicity: Finite sets

e ::= ... | {} | e ∪ e | {e} | (x ∈ e) e

slide-27
SLIDE 27

Tracking monotonicity: Finite sets

e ::= ... | {} | e ∪ e | {e} | (x ∈ e) e ∆; ∅ ⊢ e : A ∆; Γ ⊢ {e} : {A}

slide-28
SLIDE 28

Tracking monotonicity: Finite sets

e ::= ... | {} | e ∪ e | {e} | (x ∈ e) e | {e | x ∈ e, ...} ∆; ∅ ⊢ e : A ∆; Γ ⊢ {e} : {A}

slide-29
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
SLIDE 30

Fixed points

fix x is e

slide-31
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
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
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
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
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
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
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
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
SLIDE 39

Fin.

slide-40
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
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
SLIDE 42

Datafun vs Flix

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

slide-43
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 {}