Generalising Tree Traversals to DAGs Exploiting Sharing without the - - PowerPoint PPT Presentation

generalising tree traversals to dags
SMART_READER_LITE
LIVE PREVIEW

Generalising Tree Traversals to DAGs Exploiting Sharing without the - - PowerPoint PPT Presentation

Generalising Tree Traversals to DAGs Exploiting Sharing without the Pain Patrick Bahr 1 Emil Axelsson 2 1 University of Copenhagen paba@diku.dk 2 Chalmers University of Technology emax@chalmers.se PEPM 2015 Motivation Goal Do stuff on acyclic


slide-1
SLIDE 1

Generalising Tree Traversals to DAGs

Exploiting Sharing without the Pain Patrick Bahr1 Emil Axelsson2

1University of Copenhagen

paba@diku.dk

2Chalmers University of Technology

emax@chalmers.se

PEPM 2015

slide-2
SLIDE 2

Motivation

Goal

Do stuff on acyclic graphs, but pretend they are only trees.

2 / 17

slide-3
SLIDE 3

Motivation

Goal

Do stuff on acyclic graphs, but pretend they are only trees.

Primary Application

Abstract Syntax Graphs/Trees:

◮ type inference ◮ program analyses ◮ program transformations ◮ . . .

2 / 17

slide-4
SLIDE 4

The Idea

Γ ⊢ p : τ

3 / 17

slide-5
SLIDE 5

The Idea

Γ ⊢ p : τ

3 / 17

slide-6
SLIDE 6

The Idea

Γ ⊢ p : τ

3 / 17

slide-7
SLIDE 7

The Idea

Γ ⊢ p : τ

3 / 17

slide-8
SLIDE 8

The Idea

Γ ⊢ p : τ Result

3 / 17

slide-9
SLIDE 9

The Idea

Γ ⊢ p : τ Result

7 11 8 10 2 9 5 3

unravels to

3 / 17

slide-10
SLIDE 10

The Idea

Γ ⊢ p : τ Result

7 11 8 10 2 9 5 3

unravels to

3 / 17

slide-11
SLIDE 11

The Idea

Γ ⊢ p : τ Result

7 11 8 10 2 9 5 3

unravels to

Attribute Grammar

3 / 17

slide-12
SLIDE 12

The Idea

Γ ⊢ p : τ Result

7 11 8 10 2 9 5 3

unravels to

Attribute Grammar

Why?

◮ It’s more difficult to get a traversal on graphs right. ◮ But: it’s more efficient to traverse the graph.

3 / 17

slide-13
SLIDE 13

What’s the Catch?

4 / 17

slide-14
SLIDE 14

What’s the Catch? It doesn’t work

4 / 17

slide-15
SLIDE 15

What’s the Catch? It doesn’t work in general.

4 / 17

slide-16
SLIDE 16

What’s the Catch? It doesn’t work in general. But: it does work for many cases.

4 / 17

slide-17
SLIDE 17

What’s the Catch? It doesn’t work in general. But: it does work for many cases.

Our Contribution

◮ Identify classes of AGs for which this approach works. ◮ Prototype implementation in Haskell. ◮ Case studies and benchmarks.

4 / 17

slide-18
SLIDE 18

A Toy Example

data IntTree = Leaf Int | Node IntTree IntTree leavesBelow :: Int → IntTree → Set Int leavesBelow d (Leaf i) | d 0 = Set.singleton i | otherwise = Set.empty leavesBelow d (Node t1 t2) = leavesBelow (d − 1) t1 ∪ leavesBelow (d − 1) t2

5 / 17

slide-19
SLIDE 19

A Toy Example

data IntTree = Leaf Int | Node IntTree IntTree leavesBelow :: Int → IntTree → Set Int leavesBelow d (Leaf i) | d 0 = Set.singleton i | otherwise = Set.empty leavesBelow d (Node t1 t2) = leavesBelow (d − 1) t1 ∪ leavesBelow (d − 1) t2 depth leaves

5 / 17

slide-20
SLIDE 20

A Toy Example

data IntTree = Leaf Int | Node IntTree IntTree leavesBelow :: Int → IntTree → Set Int leavesBelow d (Leaf i) | d 0 = Set.singleton i | otherwise = Set.empty leavesBelow d (Node t1 t2) = leavesBelow (d − 1) t1 ∪ leavesBelow (d − 1) t2 depth leaves

5 / 17

slide-21
SLIDE 21

Traversal on Graphs

A B C depth leaves

6 / 17

slide-22
SLIDE 22

Traversal on Graphs

A B C depth leaves d1 d2

6 / 17

slide-23
SLIDE 23

Traversal on Graphs

A B C depth leaves d′

1

d′

2

d1 d2

6 / 17

slide-24
SLIDE 24

Traversal on Graphs

A B C depth leaves ⊕ d′

1

d′

2

d1 d2

6 / 17

slide-25
SLIDE 25

Traversal on Graphs

A B C depth leaves ⊕ d′

1

d′

2

d1 d2

For which traversals is this correct?

6 / 17

slide-26
SLIDE 26

But before that, let’s implement it!

data IntTree = Leaf Int | Node IntTree IntTree

7 / 17

slide-27
SLIDE 27

But before that, let’s implement it!

data IntTree a = Leaf Int | Node a a

7 / 17

slide-28
SLIDE 28

But before that, let’s implement it!

data IntTreeF a = Leaf Int | Node a a

7 / 17

slide-29
SLIDE 29

But before that, let’s implement it!

data IntTreeF a = Leaf Int | Node a a leavesBelow :: Int → Tree IntTreeF → Set Int leavesBelow = runAG leavesBelowS leavesBelowI

7 / 17

slide-30
SLIDE 30

But before that, let’s implement it!

data IntTreeF a = Leaf Int | Node a a leavesBelow :: Int → Tree IntTreeF → Set Int leavesBelow = runAG leavesBelowS leavesBelowI leavesBelowG :: Int → Dag IntTreeF → Set Int leavesBelowG = runAGDag min leavesBelowS leavesBelowI ⊕

7 / 17

slide-31
SLIDE 31

Implementing the semantic functions

leavesBelowI :: Inh IntTreeF atts Int leavesBelowI (Leaf i) = ∅ leavesBelowI (Node t1 t2) = t1 → d & t2 → d where d = above − 1

8 / 17

slide-32
SLIDE 32

Implementing the semantic functions

leavesBelowI :: Inh IntTreeF atts Int leavesBelowI (Leaf i) = ∅ leavesBelowI (Node t1 t2) = t1 → d & t2 → d where d = above − 1 leavesBelowS :: (Int ∈ atts) ⇒ Syn IntTreeF atts (Set Int) leavesBelowS (Leaf i) | (above :: Int) 0 = Set.singleton i | otherwise = Set.empty leavesBelowS (Node t1 t2) = below t1 ∪ below t2

8 / 17

slide-33
SLIDE 33

Correctness

7 11 8 10 2 9 5 3

unravels to

Result

9 / 17

slide-34
SLIDE 34

Correctness

7 11 8 10 2 9 5 3

unravels to

Result Attribute Grammar

9 / 17

slide-35
SLIDE 35

Correctness

7 11 8 10 2 9 5 3

unravels to

Result & ⊕ Attribute Grammar

9 / 17

slide-36
SLIDE 36

Correctness

7 11 8 10 2 9 5 3

unravels to

Result & ⊕ the same Attribute Grammar merge operator Attribute Grammar

9 / 17

slide-37
SLIDE 37

Correspondence Theorems

Theorem (Monotone AGs)

Let (1) G be a non-circular AG, (2) ⊕ an assoc., comm. operator on inherited attributes, and (3) such that G is monotone and ⊕ is decreasing w.r.t. . If (G, ⊕) terminates on a DAG g with result r, then G terminates on U (g) with result r′ such that r r′.

10 / 17

slide-38
SLIDE 38

Correspondence Theorems

Theorem (Monotone AGs)

Let (1) G be a non-circular AG, (2) ⊕ an assoc., comm. operator on inherited attributes, and (3) such that G is monotone and ⊕ is decreasing w.r.t. . If (G, ⊕) terminates on a DAG g with result r, then G terminates on U (g) with result r′ such that r r′.

7 11 8 10 2 9 5 3

U r r′

  • & ⊕

10 / 17

slide-39
SLIDE 39

Correspondence Theorems

Theorem (Monotone AGs)

Let (1) G be a non-circular AG, (2) ⊕ an assoc., comm. operator on inherited attributes, and (3) such that G is monotone and ⊕ is decreasing w.r.t. . If (G, ⊕) terminates on a DAG g with result r, then G terminates on U (g) with result r′ such that r r′.

Example

For the leavesBelow AG, define as follows:

◮ on Int: x y ⇐

⇒ x ≤ y

◮ on Set Int: S T ⇐

⇒ S ⊇ T

10 / 17

slide-40
SLIDE 40

Correspondence Theorems

Theorem (Monotone AGs)

Let (1) G be a non-circular AG, (2) ⊕ an assoc., comm. operator on inherited attributes, and (3) such that G is monotone and ⊕ is decreasing w.r.t. . If (G, ⊕) terminates on a DAG g with result r, then G terminates on U (g) with result r′ such that r r′.

Example

For the leavesBelow AG, define as follows:

◮ on Int: x y ⇐

⇒ x ≤ y

◮ on Set Int: S T ⇐

⇒ S ⊇ T = ⇒ leavesBelowG d g ⊇ leavesBelow d (U (g))

10 / 17

slide-41
SLIDE 41

Correspondence Theorems

Theorem (Monotone AGs)

Let (1) G be a non-circular AG, (2) ⊕ an assoc., comm. operator on inherited attributes, and (3) such that G is monotone and ⊕ is decreasing w.r.t. . If (G, ⊕) terminates on a DAG g with result r, then G terminates on U (g) with result r′ such that r r′.

Example

For the leavesBelow AG, define as follows:

◮ on Int: x y ⇐

⇒ x ≤ y

◮ on Set Int: S T ⇐

⇒ S ⊇ T = ⇒ leavesBelowG d g ⊇ leavesBelow d (U (g)) for leavesBelowG d g ⊆ leavesBelow d (U (g)) see paper

10 / 17

slide-42
SLIDE 42

Termination

◮ We know: non-circular AGs terminate on any tree. ◮ But: non-circular AGs may diverge on DAGs.

11 / 17

slide-43
SLIDE 43

Termination

◮ We know: non-circular AGs terminate on any tree. ◮ But: non-circular AGs may diverge on DAGs.

Example

A B1 B2

1 2 3 4

A B

1 2 3 4

11 / 17

slide-44
SLIDE 44

Termination

◮ We know: non-circular AGs terminate on any tree. ◮ But: non-circular AGs may diverge on DAGs.

Example

A B1 B2

1 2 3 4

A B

1 2 3 4

Theorem (termination)

Let G, ⊕, and be as before. If is well-founded on inherited attributes, then (G, ⊕) terminates on any DAG.

11 / 17

slide-45
SLIDE 45

Correspondence Theorem for Copying AGs

Copying AGs

◮ inherited attributes are just propagated, not changed ◮ Example: Bird’s repmin problem.

12 / 17

slide-46
SLIDE 46

Correspondence Theorem for Copying AGs

Copying AGs

◮ inherited attributes are just propagated, not changed ◮ Example: Bird’s repmin problem.

Theorem (copying AGs)

Let (1) G be a copying, non-circular AG, and (2) x ⊕ y ∈ {x, y} for all x, y. Then (i) (G, ⊕) terminates on any DAG, and (ii) G, ⊕(g) = G (U (g)).

12 / 17

slide-47
SLIDE 47

Graph Transformations

◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs.

13 / 17

slide-48
SLIDE 48

Graph Transformations

◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs.

7 11 8 10 2 9 5 3

unravels to

& ⊕

13 / 17

slide-49
SLIDE 49

Graph Transformations

◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs.

7 11 8 10 2 9 5 3

unravels to

7 11 8 10 2 9 5 3

& ⊕

13 / 17

slide-50
SLIDE 50

Graph Transformations

◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs.

7 11 8 10 2 9 5 3

unravels to

7 11 8 10 2 9 5 3

& ⊕

unravels to

13 / 17

slide-51
SLIDE 51

Example: Bird’s Repmin Problem

newtype MinS = MinS Int; newtype MinI = MinI Int minS :: Syn IntTreeF atts MinS minS (Leaf i) = MinS i minS (Node a b) = min (below a) (below b) minI :: Inh IntTreeF atts MinI minI = ∅ rep :: (MinI ∈ atts) ⇒ Rewrite IntTreeF atts IntTreeF rep (Leaf i) = let MinI i′ = above in Leaf i′ rep (Node a b) = Node a b

14 / 17

slide-52
SLIDE 52

Example: Bird’s Repmin Problem

newtype MinS = MinS Int; newtype MinI = MinI Int minS :: Syn IntTreeF atts MinS minS (Leaf i) = MinS i minS (Node a b) = min (below a) (below b) minI :: Inh IntTreeF atts MinI minI = ∅ rep :: (MinI ∈ atts) ⇒ Rewrite IntTreeF atts IntTreeF rep (Leaf i) = let MinI i′ = above in Leaf i′ rep (Node a b) = Node a b repmin :: Tree IntTreeF → Tree IntTreeF repmin = runRewrite minS minI rep init where init (MinS i) = MinI i

14 / 17

slide-53
SLIDE 53

Example: Bird’s Repmin Problem

newtype MinS = MinS Int; newtype MinI = MinI Int minS :: Syn IntTreeF atts MinS minS (Leaf i) = MinS i minS (Node a b) = min (below a) (below b) minI :: Inh IntTreeF atts MinI minI = ∅ rep :: (MinI ∈ atts) ⇒ Rewrite IntTreeF atts IntTreeF rep (Leaf i) = let MinI i′ = above in Leaf i′ rep (Node a b) = Node a b repmin :: Tree IntTreeF → Tree IntTreeF repmin = runRewrite minS minI rep init where init (MinS i) = MinI i repminG :: Dag IntTreeF → Dag IntTreeF repminG = runRewriteDag const minS minI rep init where init (MinS i) = MinI i

14 / 17

slide-54
SLIDE 54

Summary

Our Contributions

◮ Haskell library to run AGs on DAGs ◮ Correspondence & termination theorems to prove correctness

15 / 17

slide-55
SLIDE 55

Summary

Our Contributions

◮ Haskell library to run AGs on DAGs ◮ Correspondence & termination theorems to prove correctness

More in the paper

◮ Examples: type inference; circuits ◮ full theory & proofs ◮ parametric AGs (→ tech report) ◮ Benchmarks (→ tech report)

15 / 17

slide-56
SLIDE 56

Conclusion

Future and Ongoing Work

◮ AGs with fixpoint iteration cyclic graphs ◮ mutually recursive data types and GADTs ◮ deep pattern matching in AGs ◮ corresponding notion of non-circularity for AGs on DAGs

16 / 17

slide-57
SLIDE 57

Conclusion

Future and Ongoing Work

◮ AGs with fixpoint iteration cyclic graphs ◮ mutually recursive data types and GADTs ◮ deep pattern matching in AGs ◮ corresponding notion of non-circularity for AGs on DAGs

Implementation

Available from http://j.mp/AG-DAG.

◮ Haskell library source code ◮ more examples ◮ benchmarks

16 / 17

slide-58
SLIDE 58

Conclusion

Future and Ongoing Work

◮ AGs with fixpoint iteration cyclic graphs ◮ mutually recursive data types and GADTs ◮ deep pattern matching in AGs ◮ corresponding notion of non-circularity for AGs on DAGs

Implementation

Available from http://j.mp/AG-DAG.

◮ Haskell library source code ◮ more examples ◮ benchmarks

Try the compositional datatypes library

> cabal install compdata-dags

16 / 17

slide-59
SLIDE 59

Generalising Tree Traversals to DAGs

Exploiting Sharing without the Pain Patrick Bahr1 Emil Axelsson2

1University of Copenhagen

paba@diku.dk

2Chalmers University of Technology

emax@chalmers.se

Source Code Repository

http://j.mp/AG-DAG

Haskell Library

> cabal install compdata-dags