modular tree automata
play

Modular Tree Automata Deriving Modular Recursion Schemes from Tree - PowerPoint PPT Presentation

Modular Tree Automata Deriving Modular Recursion Schemes from Tree Automata Patrick Bahr University of Copenhagen, Department of Computer Science paba@diku.dk 11th International Conference on Mathematics of Program Construction Madrid,


  1. Modular Tree Automata Deriving Modular Recursion Schemes from Tree Automata Patrick Bahr University of Copenhagen, Department of Computer Science paba@diku.dk 11th International Conference on Mathematics of Program Construction Madrid, Spain, June 25 - 27, 2012

  2. Goals Syntax-directed computations on ASTs program analysis complex program transformations compiler construction in general 2

  3. Goals Syntax-directed computations on ASTs program analysis complex program transformations compiler construction in general Desired properties extensibility modularity reusability build complex programs by combining simple ones 2

  4. Goals Syntax-directed computations on ASTs program analysis complex program transformations compiler construction in general Desired properties extensibility modularity reusability build complex programs by combining simple ones Embed the solution into Haskell. 2

  5. How do we achieve these goals? 3

  6. How do we achieve these goals? Locality simple syntax-directed functions are local in nature 3

  7. How do we achieve these goals? Locality simple syntax-directed functions are local in nature Compositionality syntax-directed functions can be combined and composed 3

  8. How do we achieve these goals? Locality simple syntax-directed functions are local in nature Compositionality syntax-directed functions can be combined and composed Contextuality syntax-directed functions may depend on (the result of) others 3

  9. How do we achieve these goals? Locality simple syntax-directed functions are local in nature Compositionality syntax-directed functions can be combined and composed Contextuality syntax-directed functions may depend on (the result of) others NB: This breaks locality and has to be carefully restricted! But it is convenient/necessary for ◮ compositionality ◮ expressivity 3

  10. Locality Tree automata Computation according to a set of rules. Applicability of rules depend only on “local” information. The effect of a rule application is locally restricted. 4

  11. Locality Tree automata Computation according to a set of rules. Applicability of rules depend only on “local” information. The effect of a rule application is locally restricted. q f f q 1 q 2 . . . q n f ( x 1 , x 2 , . . . , x n ) − → t [ x 1 , x 2 , . . . , x n ] 4

  12. Locality Tree automata Computation according to a set of rules. Applicability of rules depend only on “local” information. The effect of a rule application is locally restricted. q f f q 1 q 2 . . . q n f ( q 1 ( x 1 ) , q 2 ( x 2 ) , . . . , q n ( x n )) − → q ( t [ x 1 , x 2 , . . . , x n ]) 4

  13. Compositionality We shall compose tree automata along 3 different dimensions. 5

  14. Compositionality We shall compose tree automata along 3 different dimensions. sequential composition : a.k.a. deforestation � A 1 � � A 2 � µ F 1 µ F 2 µ F 3 5

  15. Compositionality We shall compose tree automata along 3 different dimensions. sequential composition : a.k.a. deforestation � A 1 � � A 2 � µ F 1 µ F 2 µ F 3 � A 1 ◦ A 2 � 5

  16. Compositionality We shall compose tree automata along 3 different dimensions. sequential composition : a.k.a. deforestation � A 1 � � A 2 � µ F 1 µ F 2 µ F 3 � A 1 ◦ A 2 � input signature : the type of the AST � A 1 � : µ F → R � A 2 � : µ G → R 5

  17. Compositionality We shall compose tree automata along 3 different dimensions. sequential composition : a.k.a. deforestation � A 1 � � A 2 � µ F 1 µ F 2 µ F 3 � A 1 ◦ A 2 � input signature : the type of the AST � A 1 � : µ F → R = ⇒ � A 1 + A 2 � : µ ( F + G ) → R � A 2 � : µ G → R 5

  18. Compositionality We shall compose tree automata along 3 different dimensions. sequential composition : a.k.a. deforestation � A 1 � � A 2 � µ F 1 µ F 2 µ F 3 � A 1 ◦ A 2 � input signature : the type of the AST � A 1 � : µ F → R = ⇒ � A 1 + A 2 � : µ ( F + G ) → R � A 2 � : µ G → R output type : tupling / product automaton construction � A 1 � : µ F → R � A 2 � : µ F → S 5

  19. Compositionality We shall compose tree automata along 3 different dimensions. sequential composition : a.k.a. deforestation � A 1 � � A 2 � µ F 1 µ F 2 µ F 3 � A 1 ◦ A 2 � input signature : the type of the AST � A 1 � : µ F → R = ⇒ � A 1 + A 2 � : µ ( F + G ) → R � A 2 � : µ G → R output type : tupling / product automaton construction � A 1 � : µ F → R = ⇒ � A 1 × A 2 � : µ F → R × S � A 2 � : µ F → S 5

  20. Contextuality tupling / product automaton construction � A 1 � : µ F → R = ⇒ � A 1 × A 2 � : µ ( F ) → R × S � A 2 � : µ F → S 6

  21. Contextuality tupling / product automaton construction A 1 : F → R = ⇒ A 1 × A 2 : F → R × S A 2 : F → S 6

  22. Contextuality tupling / product automaton construction A 1 : F → R = ⇒ A 1 × A 2 : F → R × S A 2 : F → S mutumorphisms / dependent product automata A 1 : F → R A 2 : R ⇒ F → S 6

  23. Contextuality tupling / product automaton construction A 1 : F → R = ⇒ A 1 × A 2 : F → R × S A 2 : F → S mutumorphisms / dependent product automata A 1 : F → R = ⇒ A 1 × A 2 : F → R × S A 2 : R ⇒ F → S 6

  24. Contextuality tupling / product automaton construction A 1 : F → R = ⇒ A 1 × A 2 : F → R × S A 2 : F → S mutumorphisms / dependent product automata A 1 : S ⇒ F → R = ⇒ A 1 × A 2 : F → R × S A 2 : R ⇒ F → S 6

  25. Outline Introduction 1 State Transition Functions 2 Composing State Spaces Compositional Signatures Tree Transducers 3 Bottom-Up Tree Transducers Decomposing Tree Transducers Conclusions 4 7

  26. Terms in Haskell Data types as fixed points of functors data Term f = In ( f ( Term f )) 8

  27. Terms in Haskell Data types as fixed points of functors data Term f = In ( f ( Term f )) Functors class Functor f where fmap :: ( a → b ) → f a → f b 8

  28. Bottom-Up State Transitions in Haskell q f f q 1 q 2 . . . q n . . . 9

  29. Bottom-Up State Transitions in Haskell q f q 1 q 2 . . . q n 9

  30. Bottom-Up State Transitions in Haskell q f q 1 q 2 . . . q n q 1 q 2 q n 9

  31. Bottom-Up State Transitions in Haskell q f q 1 q 2 . . . q n q 1 q 2 q n Bottom-up state transition rules as algebras type UpState f q = f q → q 9

  32. Bottom-Up State Transitions in Haskell q f q 1 q 2 . . . q n q 1 q 2 q n Bottom-up state transition rules as algebras type UpState f q = f q → q runUpState :: Functor f ⇒ UpState f q → Term f → q runUpState φ ( In t ) = φ ( fmap ( runUpState φ ) t ) 9

  33. Bottom-Up State Transitions in Haskell q f q 1 q 2 . . . q n q 1 q 2 q n Bottom-up state transition rules as algebras a.k.a. catamorphism / fold type UpState f q = f q → q runUpState :: Functor f ⇒ UpState f q → Term f → q runUpState φ ( In t ) = φ ( fmap ( runUpState φ ) t ) 9

  34. Composing State Spaces – Motivating Example A simple expression language data Sig e = Val Int | Plus e e 10

  35. Composing State Spaces – Motivating Example A simple expression language data Sig e = Val Int | Plus e e Task: writing a code generator type Addr = Int data Instr = Acc Int | Load Addr | Store Addr | Add Addr type Code = [ Instr ] 10

  36. Composing State Spaces – Motivating Example A simple expression language data Sig e = Val Int | Plus e e Task: writing a code generator type Addr = Int data Instr = Acc Int | Load Addr | Store Addr | Add Addr type Code = [ Instr ] The problem codeSt :: UpState Sig Code codeSt ( Val i ) = [ Acc i ] codeSt ( Plus x y ) = x + + [ Store a ] + + y + + [ Add a ] where a = . . . 10

  37. Composing State Spaces – Motivating Example A simple expression language data Sig e = Val Int | Plus e e Task: writing a code generator type Addr = Int data Instr = Acc Int | Load Addr | Store Addr | Add Addr type Code = [ Instr ] Sig Code → Code The problem codeSt :: UpState Sig Code codeSt ( Val i ) = [ Acc i ] codeSt ( Plus x y ) = x + + [ Store a ] + + y + + [ Add a ] where a = . . . 10

  38. Tupling Tuple the code with an address counter codeAddrSt :: UpState Sig ( Code , Addr ) codeAddrSt ( Val i ) = ([ Acc i ] , 0 ) codeAddrSt ( Plus ( x , a ′ ) ( y , a )) = ( x + + [ Store a ] + + y + + [ Add a ] , 1 + max a a ′ ) 11

  39. Tupling Tuple the code with an address counter codeAddrSt :: UpState Sig ( Code , Addr ) codeAddrSt ( Val i ) = ([ Acc i ] , 0 ) codeAddrSt ( Plus ( x , a ′ ) ( y , a )) = ( x + + [ Store a ] + + y + + [ Add a ] , 1 + max a a ′ ) Run the automaton code :: Term Sig → ( Code , Addr ) code = runUpState codeAddrSt 11

  40. Tupling Tuple the code with an address counter codeAddrSt :: UpState Sig ( Code , Addr ) codeAddrSt ( Val i ) = ([ Acc i ] , 0 ) codeAddrSt ( Plus ( x , a ′ ) ( y , a )) = ( x + + [ Store a ] + + y + + [ Add a ] , 1 + max a a ′ ) Run the automaton code :: Term Sig → ( Code , Addr ) code = fst . runUpState codeAddrSt 11

  41. Tupling Tuple the code with an address counter codeAddrSt :: UpState Sig ( Code , Addr ) codeAddrSt ( Val i ) = ([ Acc i ] , 0 ) codeAddrSt ( Plus ( x , a ′ ) ( y , a )) = ( x + + [ Store a ] + + y + + [ Add a ] , 1 + max a a ′ ) Run the automaton code :: Term Sig → Code code = fst . runUpState codeAddrSt 11

Recommend


More recommend