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
Goals Syntax-directed computations on ASTs program analysis complex program transformations compiler construction in general 2
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
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
How do we achieve these goals? 3
How do we achieve these goals? Locality simple syntax-directed functions are local in nature 3
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
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
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
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
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
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
Compositionality We shall compose tree automata along 3 different dimensions. 5
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
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
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
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
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
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
Contextuality tupling / product automaton construction � A 1 � : µ F → R = ⇒ � A 1 × A 2 � : µ ( F ) → R × S � A 2 � : µ F → S 6
Contextuality tupling / product automaton construction A 1 : F → R = ⇒ A 1 × A 2 : F → R × S A 2 : F → S 6
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
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
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
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
Terms in Haskell Data types as fixed points of functors data Term f = In ( f ( Term f )) 8
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
Bottom-Up State Transitions in Haskell q f f q 1 q 2 . . . q n . . . 9
Bottom-Up State Transitions in Haskell q f q 1 q 2 . . . q n 9
Bottom-Up State Transitions in Haskell q f q 1 q 2 . . . q n q 1 q 2 q n 9
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
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
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
Composing State Spaces – Motivating Example A simple expression language data Sig e = Val Int | Plus e e 10
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
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
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
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
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
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
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