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 graphs, but pretend they are only trees. 2 / 17
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
The Idea Γ ⊢ p : τ 3 / 17
The Idea Γ ⊢ p : τ 3 / 17
The Idea Γ ⊢ p : τ 3 / 17
The Idea Γ ⊢ p : τ 3 / 17
The Idea Γ ⊢ p : τ Result 3 / 17
The Idea 5 10 11 3 2 9 7 8 Γ ⊢ p : τ unravels to Result 3 / 17
The Idea 5 10 11 3 2 9 7 8 Γ ⊢ p : τ unravels to Result 3 / 17
The Idea 5 10 11 3 2 9 7 8 Γ ⊢ p : τ unravels to Result Attribute Grammar 3 / 17
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
What’s the Catch? 4 / 17
What’s the Catch? It doesn’t work 4 / 17
What’s the Catch? It doesn’t work in general. 4 / 17
What’s the Catch? It doesn’t work in general. But: it does work for many cases. 4 / 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
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
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
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
Traversal on Graphs A B leaves depth C 6 / 17
Traversal on Graphs d 1 d 2 A B leaves depth C 6 / 17
Traversal on Graphs d 1 d 2 A B leaves depth d ′ d ′ 1 2 C 6 / 17
Traversal on Graphs d 1 d 2 A B leaves depth d ′ ⊕ d ′ 1 2 C 6 / 17
Traversal on Graphs d 1 d 2 A B leaves depth d ′ ⊕ d ′ 1 2 C For which traversals is this correct? 6 / 17
But before that, let’s implement it! = Leaf Int data IntTree | Node IntTree IntTree 7 / 17
But before that, let’s implement it! a = Leaf Int data IntTree | Node a a 7 / 17
But before that, let’s implement it! data IntTreeF a = Leaf Int | Node a a 7 / 17
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
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
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
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
Correctness 5 10 11 3 2 9 7 8 unravels to Result 9 / 17
Correctness 5 10 11 3 2 9 7 8 unravels to Result Attribute Grammar 9 / 17
Correctness 5 10 11 3 2 9 7 8 & ⊕ unravels to Result Attribute Grammar 9 / 17
Correctness the same Attribute Grammar 5 10 11 3 2 9 7 8 & ⊕ merge operator unravels to Result Attribute Grammar 9 / 17
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
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
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
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
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
Termination ◮ We know: non-circular AGs terminate on any tree. ◮ But: non-circular AGs may diverge on DAGs. 11 / 17
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
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
Correspondence Theorem for Copying AGs Copying AGs ◮ inherited attributes are just propagated, not changed ◮ Example: Bird’s repmin problem. 12 / 17
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
Graph Transformations ◮ Our framework generalises to tree/DAG-transformations ◮ Idea: attributes may contain trees/DAGs. 13 / 17
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
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
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