generalising tree traversals to dags
play

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


  1. 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

  2. Motivation Goal Do stuff on acyclic graphs, but pretend they are only trees. 2 / 17

  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

  4. The Idea Γ ⊢ p : τ 3 / 17

  5. The Idea Γ ⊢ p : τ 3 / 17

  6. The Idea Γ ⊢ p : τ 3 / 17

  7. The Idea Γ ⊢ p : τ 3 / 17

  8. The Idea Γ ⊢ p : τ Result 3 / 17

  9. The Idea 5 10 11 3 2 9 7 8 Γ ⊢ p : τ unravels to Result 3 / 17

  10. The Idea 5 10 11 3 2 9 7 8 Γ ⊢ p : τ unravels to Result 3 / 17

  11. The Idea 5 10 11 3 2 9 7 8 Γ ⊢ p : τ unravels to Result Attribute Grammar 3 / 17

  12. The Idea 5 10 11 3 2 9 7 8 Γ ⊢ p : τ unravels to Result 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

  13. What’s the Catch? 4 / 17

  14. What’s the Catch? It doesn’t work 4 / 17

  15. What’s the Catch? It doesn’t work in general. 4 / 17

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

  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

  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 t 1 t 2 ) = leavesBelow ( d − 1 ) t 1 ∪ leavesBelow ( d − 1 ) t 2 5 / 17

  19. A Toy Example data IntTree = Leaf Int | Node IntTree IntTree leavesBelow :: Int → IntTree → Set Int depth leaves leavesBelow d ( Leaf i ) | d � 0 = Set . singleton i | otherwise = Set . empty leavesBelow d ( Node t 1 t 2 ) = leavesBelow ( d − 1 ) t 1 ∪ leavesBelow ( d − 1 ) t 2 5 / 17

  20. A Toy Example data IntTree = Leaf Int | Node IntTree IntTree leavesBelow :: Int → IntTree → Set Int depth leaves leavesBelow d ( Leaf i ) | d � 0 = Set . singleton i | otherwise = Set . empty leavesBelow d ( Node t 1 t 2 ) = leavesBelow ( d − 1 ) t 1 ∪ leavesBelow ( d − 1 ) t 2 5 / 17

  21. Traversal on Graphs A B leaves depth C 6 / 17

  22. Traversal on Graphs d 1 d 2 A B leaves depth C 6 / 17

  23. Traversal on Graphs d 1 d 2 A B leaves depth d ′ d ′ 1 2 C 6 / 17

  24. Traversal on Graphs d 1 d 2 A B leaves depth d ′ ⊕ d ′ 1 2 C 6 / 17

  25. Traversal on Graphs d 1 d 2 A B leaves depth d ′ ⊕ d ′ 1 2 C For which traversals is this correct? 6 / 17

  26. But before that, let’s implement it! = Leaf Int data IntTree | Node IntTree IntTree 7 / 17

  27. But before that, let’s implement it! a = Leaf Int data IntTree | Node a a 7 / 17

  28. But before that, let’s implement it! data IntTreeF a = Leaf Int | Node a a 7 / 17

  29. But before that, let’s implement it! data IntTreeF a = Leaf Int | Node a a leavesBelow :: Int → Tree IntTreeF → Set Int leavesBelow = runAG leavesBelow S leavesBelow I 7 / 17

  30. But before that, let’s implement it! data IntTreeF a = Leaf Int | Node a a leavesBelow :: Int → Tree IntTreeF → Set Int leavesBelow = runAG leavesBelow S leavesBelow I leavesBelow G :: Int → Dag IntTreeF → Set Int leavesBelow G = runAGDag min leavesBelow S leavesBelow I ⊕ 7 / 17

  31. Implementing the semantic functions leavesBelow I :: Inh IntTreeF atts Int leavesBelow I ( Leaf i ) = ∅ leavesBelow I ( Node t 1 t 2 ) = t 1 �→ d & t 2 �→ d where d = above − 1 8 / 17

  32. Implementing the semantic functions leavesBelow I :: Inh IntTreeF atts Int leavesBelow I ( Leaf i ) = ∅ leavesBelow I ( Node t 1 t 2 ) = t 1 �→ d & t 2 �→ d where d = above − 1 leavesBelow S :: ( Int ∈ atts ) ⇒ Syn IntTreeF atts ( Set Int ) leavesBelow S ( Leaf i ) | ( above :: Int ) � 0 = Set . singleton i | otherwise = Set . empty leavesBelow S ( Node t 1 t 2 ) = below t 1 ∪ below t 2 8 / 17

  33. Correctness 5 10 11 3 2 9 7 8 unravels to Result 9 / 17

  34. Correctness 5 10 11 3 2 9 7 8 unravels to Result Attribute Grammar 9 / 17

  35. Correctness 5 10 11 3 2 9 7 8 & ⊕ unravels to Result Attribute Grammar 9 / 17

  36. Correctness the same Attribute Grammar 5 10 11 3 2 9 7 8 & ⊕ merge operator unravels to Result Attribute Grammar 9 / 17

  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

  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 ′ . & ⊕ 5 10 11 3 2 9 7 8 r � U r ′ 10 / 17

  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

  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 = ⇒ leavesBelow G d g ⊇ leavesBelow d ( U ( g )) 10 / 17

  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 = ⇒ leavesBelow G d g ⊇ leavesBelow d ( U ( g )) for leavesBelow G d g ⊆ leavesBelow d ( U ( g )) see paper 10 / 17

  42. Termination ◮ We know: non-circular AGs terminate on any tree. ◮ But: non-circular AGs may diverge on DAGs. 11 / 17

  43. Termination ◮ We know: non-circular AGs terminate on any tree. ◮ But: non-circular AGs may diverge on DAGs. Example A A 1 4 1 2 3 4 2 3 B 1 B 2 B 11 / 17

  44. Termination ◮ We know: non-circular AGs terminate on any tree. ◮ But: non-circular AGs may diverge on DAGs. Example A A 1 4 1 2 3 4 2 3 B 1 B 2 B Theorem (termination) Let G, ⊕ , and � be as before. If � is well-founded on inherited attributes, then ( G , ⊕ ) terminates on any DAG. 11 / 17

  45. Correspondence Theorem for Copying AGs Copying AGs ◮ inherited attributes are just propagated, not changed ◮ Example: Bird’s repmin problem. 12 / 17

  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

  47. Graph Transformations ◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs. 13 / 17

  48. Graph Transformations ◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs. & ⊕ 5 10 11 3 2 9 8 7 unravels to 13 / 17

  49. Graph Transformations ◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs. & ⊕ 5 5 10 10 11 11 3 3 2 2 9 9 8 8 7 7 unravels to 13 / 17

  50. Graph Transformations ◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs. & ⊕ 5 5 10 10 11 11 3 3 2 2 9 9 8 8 7 7 unravels to unravels to 13 / 17

Recommend


More recommend