deriving modular recursion schemes from tree automata
play

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

Deriving Modular Recursion Schemes from Tree Automata Patrick Bahr University of Copenhagen, Department of Computer Science paba@diku.dk Computing Science Colloquium, Utrecht University, April 12th, 2012 Outline Tree Automata 1 Bottom-Up


  1. An Example Signature And state data F a = And a a | Not a | TT | FF | B data Q = Q0 | Q1 | Q2 type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) data Context f a = In ( f ( Context f a )) | Hole a The transduction function trans :: UpTrans F Q F tt → q 1 (tt) trans TT = ( Q1 , tt ) 14

  2. An Example Signature And state data F a = And a a | Not a | TT | FF | B data Q = Q0 | Q1 | Q2 type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) data Context f a = In ( f ( Context f a )) | Hole a tt :: Context F a The transduction function tt = In TT trans :: UpTrans F Q F tt → q 1 (tt) trans TT = ( Q1 , tt ) 14

  3. An Example Signature And state data F a = And a a | Not a | TT | FF | B data Q = Q0 | Q1 | Q2 type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) data Context f a = In ( f ( Context f a )) | Hole a The transduction function trans :: UpTrans F Q F tt → q 1 (tt) trans TT = ( Q1 , tt ) not( q 2 ( x )) → q 2 (not( x )) trans ( Not ( Q2 , x )) = ( Q2 , not ( Hole x )) 14

  4. An Example Signature And state data F a = And a a | Not a | TT | FF | B data Q = Q0 | Q1 | Q2 type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) data Context f a = In ( f ( Context f a )) | Hole a The transduction function not :: Context F a → Context F a trans :: UpTrans F Q F not = In . Not tt → q 1 (tt) trans TT = ( Q1 , tt ) not( q 2 ( x )) → q 2 (not( x )) trans ( Not ( Q2 , x )) = ( Q2 , not ( Hole x )) 14

  5. An Example Signature And state data F a = And a a | Not a | TT | FF | B data Q = Q0 | Q1 | Q2 type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) data Context f a = In ( f ( Context f a )) | Hole a The transduction function trans :: UpTrans F Q F tt → q 1 (tt) trans TT = ( Q1 , tt ) not( q 2 ( x )) → q 2 (not( x )) trans ( Not ( Q2 , x )) = ( Q2 , not ( Hole x )) and( q ( x ) , p ( y )) → q 0 (ff) if q 0 ∈ { q , p } 14

  6. An Example Signature And state data F a = And a a | Not a | TT | FF | B data Q = Q0 | Q1 | Q2 type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) data Context f a = In ( f ( Context f a )) | Hole a The transduction function trans :: UpTrans F Q F tt → q 1 (tt) trans TT = ( Q1 , tt ) not( q 2 ( x )) → q 2 (not( x )) trans ( Not ( Q2 , x )) = ( Q2 , not ( Hole x )) and( q ( x ) , p ( y )) → q 0 (ff) if q 0 ∈ { q , p } trans ( And ( q , x ) ( p , y )) | q ≡ Q0 ∨ p ≡ Q0 = ( Q0 , ff ) 14

  7. Outline Tree Automata 1 Bottom-Up Tree Acceptors Bottom-Up Tree Transducers Introducing Modularity 2 Composing State Spaces Compositional Signatures Decomposing Tree Transducers Other Automata 3 15

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

  9. 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 ] 16

  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 = . . . 16

  11. 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 = . . . 16

  12. 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 ′ ) 17

  13. 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 17

  14. 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 17

  15. 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 17

  16. Product Automata Deriving projections class a ∈ b where pr :: b → a 18

  17. Product Automata Deriving projections class a ∈ b where a ∈ b iff pr :: b → a b is of the form ( b 1 , ( b 2 , ... )) and a = b i for some i 18

  18. Product Automata Deriving projections class a ∈ b where a ∈ b iff pr :: b → a b is of the form ( b 1 , ( b 2 , ... )) and a = b i for some i For example: Addr ∈ ( Code , Addr ) 18

  19. Product Automata Deriving projections class a ∈ b where a ∈ b iff pr :: b → a b is of the form ( b 1 , ( b 2 , ... )) and a = b i for some i For example: Addr ∈ ( Code , Addr ) Dependent state transition functions type UpState f q = f q → q 18

  20. Product Automata Deriving projections class a ∈ b where a ∈ b iff pr :: b → a b is of the form ( b 1 , ( b 2 , ... )) and a = b i for some i For example: Addr ∈ ( Code , Addr ) Dependent state transition functions type UpState f q = f q → q type DUpState f p q = ( q ∈ p ) ⇒ f p → q 18

  21. Product Automata Deriving projections class a ∈ b where a ∈ b iff pr :: b → a b is of the form ( b 1 , ( b 2 , ... )) and a = b i for some i For example: Addr ∈ ( Code , Addr ) Dependent state transition functions type UpState f q = f q → q type DUpState f p q = ( q ∈ p ) ⇒ f p → q 18

  22. Product Automata Deriving projections class a ∈ b where a ∈ b iff pr :: b → a b is of the form ( b 1 , ( b 2 , ... )) and a = b i for some i For example: Addr ∈ ( Code , Addr ) Dependent state transition functions type UpState f q = f q → q type DUpState f p q = ( q ∈ p ) ⇒ f p → q 18

  23. Product Automata Deriving projections class a ∈ b where a ∈ b iff pr :: b → a b is of the form ( b 1 , ( b 2 , ... )) and a = b i for some i For example: Addr ∈ ( Code , Addr ) Dependent state transition functions type UpState f q = f q → q type DUpState f p q = ( q ∈ p ) ⇒ f p → q Product state transition ( ⊗ ) :: ( p ∈ c , q ∈ c ) ⇒ DUpState f c p → DUpState f c q → DUpState f c ( p , q ) ( sp ⊗ sq ) t = ( sp t , sq t ) 18

  24. Running Dependent State Transition Functions The types type UpState = f q → q f q type DUpState f p q = ( q ∈ p ) ⇒ f p → q 19

  25. Running Dependent State Transition Functions The types type UpState = f q → q f q type DUpState f p q = ( q ∈ p ) ⇒ f p → q From state transition to dependent state transition dUpState :: Functor f ⇒ UpState f q → DUpState f p q dUpState st = st . fmap pr 19

  26. Running Dependent State Transition Functions The types type UpState = f q → q f q type DUpState f p q = ( q ∈ p ) ⇒ f p → q From state transition to dependent state transition dUpState :: Functor f ⇒ UpState f q → DUpState f p q dUpState st = st . fmap pr Running dependent state transitions runDUpState :: Functor f ⇒ DUpState f q q → Term f → q runDUpState f = runUpState f 19

  27. The Code Generator Example The code generator codeSt :: ( Int ∈ q ) ⇒ DUpState Sig q Code codeSt ( Val i ) = [ Acc i ] codeSt ( Plus x y ) = pr x + + [ Store a ] + + pr y + + [ Add a ] where a = pr y 20

  28. The Code Generator Example The code generator codeSt :: ( Int ∈ q ) ⇒ DUpState Sig q Code codeSt ( Val i ) = [ Acc i ] codeSt ( Plus x y ) = pr x + + [ Store a ] + + pr y + + [ Add a ] where a = pr y Generating fresh addresses heightSt :: UpState Sig Int heightSt ( Val ) = 0 heightSt ( Plus x y ) = 1 + max x y 20

  29. The Code Generator Example The code generator codeSt :: ( Int ∈ q ) ⇒ DUpState Sig q Code codeSt ( Val i ) = [ Acc i ] codeSt ( Plus x y ) = pr x + + [ Store a ] + + pr y + + [ Add a ] where a = pr y Generating fresh addresses heightSt :: UpState Sig Int heightSt ( Val ) = 0 heightSt ( Plus x y ) = 1 + max x y Combining the components code :: Term Sig → Code code = fst . runUpState ( codeSt ⊗ dUpState heightSt ) 20

  30. Outline Tree Automata 1 Bottom-Up Tree Acceptors Bottom-Up Tree Transducers Introducing Modularity 2 Composing State Spaces Compositional Signatures Decomposing Tree Transducers Other Automata 3 21

  31. Combining Signatures Coproduct of signatures data ( f ⊕ g ) e = Inl ( f e ) | Inr ( g e ) f ⊕ g is the sum of the signatures f and g 22

  32. Combining Signatures Coproduct of signatures data ( f ⊕ g ) e = Inl ( f e ) | Inr ( g e ) f ⊕ g is the sum of the signatures f and g Example data Inc e = Inc e type Sig ′ = Inc ⊕ Sig 22

  33. Combining Automata Making the height compositional class HeightSt f where heightSt :: DUpState f q Int instance ( HeightSt f , HeightSt g ) ⇒ HeightSt ( f ⊕ g ) where heightSt ( Inl x ) = heightSt x heightSt ( Inr x ) = heightSt x 23

  34. Combining Automata Making the height compositional class HeightSt f where heightSt :: DUpState f q Int instance ( HeightSt f , HeightSt g ) ⇒ HeightSt ( f ⊕ g ) where heightSt ( Inl x ) = heightSt x heightSt ( Inr x ) = heightSt x Defining the height on Sig instance HeightSt Sig where heightSt ( Val ) = 0 heightSt ( Plus x y ) = 1 + max x y 23

  35. Combining Automata Making the height compositional class HeightSt f where heightSt :: DUpState f q Int instance ( HeightSt f , HeightSt g ) ⇒ HeightSt ( f ⊕ g ) where heightSt ( Inl x ) = heightSt x heightSt ( Inr x ) = heightSt x Defining the height on Sig instance HeightSt Sig where heightSt ( Val ) = 0 heightSt ( Plus x y ) = 1 + max x y Defining the height on Inc instance HeightSt Inc where heightSt ( Inc x ) = 1 + x 23

  36. Subsignatures Subsignature type class class f � g where inj :: f a → g a 24

  37. Subsignatures Subsignature type class class f � g where f � g iff inj :: f a → g a g = g 1 ⊕ g 2 ⊕ ... ⊕ g n and f = g i , 0 < i ≤ n 24

  38. Subsignatures Subsignature type class class f � g where f � g iff inj :: f a → g a g = g 1 ⊕ g 2 ⊕ ... ⊕ g n and f = g i , 0 < i ≤ n For example: Inc � Inc ⊕ Sig � �� � Sig ′ 24

  39. Subsignatures Subsignature type class class f � g where f � g iff inj :: f a → g a g = g 1 ⊕ g 2 ⊕ ... ⊕ g n and f = g i , 0 < i ≤ n For example: Inc � Inc ⊕ Sig � �� � Sig ′ Injection and projection functions inject :: ( g � f ) ⇒ g ( Context f a ) → Context f a inject = In . inj 24

  40. Outline Tree Automata 1 Bottom-Up Tree Acceptors Bottom-Up Tree Transducers Introducing Modularity 2 Composing State Spaces Compositional Signatures Decomposing Tree Transducers Other Automata 3 25

  41. Tree Homomorphisms type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) 26

  42. Tree Homomorphisms type UpTrans f g = ∀ a . f a → Context g a 26

  43. Tree Homomorphisms type Hom f g = ∀ a . f a → Context g a 26

  44. Tree Homomorphisms type Hom f g = ∀ a . f a → Context g a Example (Desugaring) class DesugHom f g where desugHom :: Hom f g desugar :: ( Functor f , Functor g , DesugHom f g ) ⇒ Term f → Term g desugar = runHom desugHom 26

  45. Tree Homomorphisms type Hom f g = ∀ a . f a → Context g a Example (Desugaring) class DesugHom f g where desugHom :: Hom f g desugar :: ( Functor f , Functor g , DesugHom f g ) ⇒ Term f → Term g desugar = runHom desugHom instance ( Sig � g ) ⇒ DesugHom Inc g where desugHom ( Inc x ) = Hole x ‘ plus ‘ val 1 instance ( Functor g , f � g ) ⇒ DesugHom f g where desugHom = simpCxt . inj 26

  46. Tree Homomorphisms type Hom f g = ∀ a . f a → Context g a Example (Desugaring) class DesugHom f g where desugHom :: Hom f g desugar :: ( Functor f , Functor g , DesugHom f g ) ⇒ Term f → Term g desugar = runHom desugHom simpCxt :: Functor g ⇒ g a → Context g a instance ( Sig � g ) ⇒ DesugHom Inc g where simpCxt t = In ( fmap Hole t ) desugHom ( Inc x ) = Hole x ‘ plus ‘ val 1 instance ( Functor g , f � g ) ⇒ DesugHom f g where desugHom = simpCxt . inj 26

  47. Stateful Tree Homomorphisms Decomposing tree transducers type Hom f g = ∀ a . f a → Context g a type UpState f q = f q → q type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) 27

  48. Stateful Tree Homomorphisms Decomposing tree transducers type Hom f g = ∀ a . f a → Context g a type UpState f q = f q → q type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) Making homomorphisms dependent on a state type QHom f q g = ∀ a . f a → Context g a 27

  49. Stateful Tree Homomorphisms Decomposing tree transducers type Hom f g = ∀ a . f a → Context g a type UpState f q = f q → q type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) Making homomorphisms dependent on a state type QHom f q g = ∀ a . f ( q , a ) → Context g a 27

  50. Stateful Tree Homomorphisms Decomposing tree transducers type Hom f g = ∀ a . f a → Context g a type UpState f q = f q → q type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) Making homomorphisms dependent on a state type QHom f q g = ∀ a . ( a → q ) → f a → Context g a 27

  51. Stateful Tree Homomorphisms Decomposing tree transducers type Hom f g = ∀ a . f a → Context g a type UpState f q = f q → q type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) Making homomorphisms dependent on a state type QHom f q g = ∀ a . q → ( a → q ) → f a → Context g a 27

  52. Stateful Tree Homomorphisms Decomposing tree transducers type Hom f g = ∀ a . f a → Context g a type UpState f q = f q → q type UpTrans f q g = ∀ a . f ( q , a ) → ( q , Context g a ) Making homomorphisms dependent on a state type QHom f q g = ∀ a . q → ( a → q ) → f a → Context g a Using implicit parameters type QHom f q g = ∀ a . (? above :: q , ? below :: a → q ) ⇒ f a → Context g a 27

  53. An Example Extending the signature with let bindings type Name = String data Let e = LetIn Name e e | Var Name type LetSig = Let ⊕ Sig 28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend