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

modular tree automata
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 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

slide-2
SLIDE 2

Goals

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

2

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 5

How do we achieve these goals?

3

slide-6
SLIDE 6

How do we achieve these goals?

Locality simple syntax-directed functions are local in nature

3

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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. f q1

  • q2. . .

qn f q f ( x1 , x2 , . . . , xn ) − → t[x1, x2, . . . , xn]

4

slide-12
SLIDE 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. f q1

  • q2. . .

qn f q f (q1(x1), q2(x2), . . . , qn(xn)) − → q(t[x1, x2, . . . , xn])

4

slide-13
SLIDE 13

Compositionality

We shall compose tree automata along 3 different dimensions.

5

slide-14
SLIDE 14

Compositionality

We shall compose tree automata along 3 different dimensions. sequential composition: a.k.a. deforestation µF1 µF2 µF3 A1 A2

5

slide-15
SLIDE 15

Compositionality

We shall compose tree automata along 3 different dimensions. sequential composition: a.k.a. deforestation µF1 µF2 µF3 A1 A2 A1 ◦ A2

5

slide-16
SLIDE 16

Compositionality

We shall compose tree automata along 3 different dimensions. sequential composition: a.k.a. deforestation µF1 µF2 µF3 A1 A2 A1 ◦ A2 input signature: the type of the AST A1 : µF → R A2 : µG → R

5

slide-17
SLIDE 17

Compositionality

We shall compose tree automata along 3 different dimensions. sequential composition: a.k.a. deforestation µF1 µF2 µF3 A1 A2 A1 ◦ A2 input signature: the type of the AST A1 : µF → R A2 : µG → R = ⇒ A1 + A2 : µ(F + G) → R

5

slide-18
SLIDE 18

Compositionality

We shall compose tree automata along 3 different dimensions. sequential composition: a.k.a. deforestation µF1 µF2 µF3 A1 A2 A1 ◦ A2 input signature: the type of the AST A1 : µF → R A2 : µG → R = ⇒ A1 + A2 : µ(F + G) → R

  • utput type: tupling / product automaton construction

A1 : µF → R A2 : µF → S

5

slide-19
SLIDE 19

Compositionality

We shall compose tree automata along 3 different dimensions. sequential composition: a.k.a. deforestation µF1 µF2 µF3 A1 A2 A1 ◦ A2 input signature: the type of the AST A1 : µF → R A2 : µG → R = ⇒ A1 + A2 : µ(F + G) → R

  • utput type: tupling / product automaton construction

A1 : µF → R A2 : µF → S = ⇒ A1 × A2 : µF → R × S

5

slide-20
SLIDE 20

Contextuality

tupling / product automaton construction A1 : µF → R A2 : µF → S = ⇒ A1 × A2 : µ(F) → R × S

6

slide-21
SLIDE 21

Contextuality

tupling / product automaton construction A1 : F → R A2 : F → S = ⇒ A1 × A2 : F → R × S

6

slide-22
SLIDE 22

Contextuality

tupling / product automaton construction A1 : F → R A2 : F → S = ⇒ A1 × A2 : F → R × S mutumorphisms / dependent product automata A1 : F → R A2 : R ⇒ F → S

6

slide-23
SLIDE 23

Contextuality

tupling / product automaton construction A1 : F → R A2 : F → S = ⇒ A1 × A2 : F → R × S mutumorphisms / dependent product automata A1 : F → R A2 : R ⇒ F → S = ⇒ A1 × A2 : F → R × S

6

slide-24
SLIDE 24

Contextuality

tupling / product automaton construction A1 : F → R A2 : F → S = ⇒ A1 × A2 : F → R × S mutumorphisms / dependent product automata A1 : S ⇒ F → R A2 : R ⇒ F → S = ⇒ A1 × A2 : F → R × S

6

slide-25
SLIDE 25

Outline

1

Introduction

2

State Transition Functions Composing State Spaces Compositional Signatures

3

Tree Transducers Bottom-Up Tree Transducers Decomposing Tree Transducers

4

Conclusions

7

slide-26
SLIDE 26

Terms in Haskell

Data types as fixed points of functors data Term f = In (f (Term f ))

8

slide-27
SLIDE 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

slide-28
SLIDE 28

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn f q . . .

9

slide-29
SLIDE 29

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn q

9

slide-30
SLIDE 30

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn q1 q2 qn q

9

slide-31
SLIDE 31

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn q1 q2 qn q Bottom-up state transition rules as algebras type UpState f q = f q → q

9

slide-32
SLIDE 32

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn q1 q2 qn q 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

slide-33
SLIDE 33

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn q1 q2 qn q 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) a.k.a. catamorphism / fold

9

slide-34
SLIDE 34

Composing State Spaces – Motivating Example

A simple expression language data Sig e = Val Int | Plus e e

10

slide-35
SLIDE 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

slide-36
SLIDE 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

slide-37
SLIDE 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 ] The problem codeSt :: UpState Sig Code codeSt (Val i) = [Acc i ] codeSt (Plus x y) = x + + [Store a] + + y + + [Add a] where a = . . . Sig Code → Code

10

slide-38
SLIDE 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

slide-39
SLIDE 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

slide-40
SLIDE 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

slide-41
SLIDE 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

slide-42
SLIDE 42

Product Automata

Deriving projections class a ∈ b where pr :: b → a

12

slide-43
SLIDE 43

Product Automata

Deriving projections class a ∈ b where pr :: b → a a ∈ b iff b is of the form (b1, (b2, ... bn)) and a = bi for some i

12

slide-44
SLIDE 44

Product Automata

Deriving projections class a ∈ b where pr :: b → a a ∈ b iff b is of the form (b1, (b2, ... bn)) and a = bi for some i For example: Addr ∈ (Code, Addr)

12

slide-45
SLIDE 45

Product Automata

Deriving projections class a ∈ b where pr :: b → a a ∈ b iff b is of the form (b1, (b2, ... bn)) and a = bi for some i For example: Addr ∈ (Code, Addr) Dependent state transition functions type UpState f q = f q → q

12

slide-46
SLIDE 46

Product Automata

Deriving projections class a ∈ b where pr :: b → a a ∈ b iff b is of the form (b1, (b2, ... bn)) and a = bi 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

12

slide-47
SLIDE 47

Product Automata

Deriving projections class a ∈ b where pr :: b → a a ∈ b iff b is of the form (b1, (b2, ... bn)) and a = bi 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

12

slide-48
SLIDE 48

Product Automata

Deriving projections class a ∈ b where pr :: b → a a ∈ b iff b is of the form (b1, (b2, ... bn)) and a = bi 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

12

slide-49
SLIDE 49

Product Automata

Deriving projections class a ∈ b where pr :: b → a a ∈ b iff b is of the form (b1, (b2, ... bn)) and a = bi 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)

12

slide-50
SLIDE 50

Running Dependent State Transition Functions

The types type UpState f q = f q → q type DUpState f p q = (q ∈ p) ⇒ f p → q

13

slide-51
SLIDE 51

Running Dependent State Transition Functions

The types type UpState f q = f q → q type DUpState f p q = (q ∈ p) ⇒ f p → q Running dependent state transitions runDUpState :: Functor f ⇒ DUpState f q q → Term f → q runDUpState f = runUpState f

13

slide-52
SLIDE 52

Running Dependent State Transition Functions

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

13

slide-53
SLIDE 53

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

14

slide-54
SLIDE 54

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

14

slide-55
SLIDE 55

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)

14

slide-56
SLIDE 56

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) (Code ∈ q, Int ∈ q) ⇒ DUpState Sig q (Code, Int)

14

slide-57
SLIDE 57

Outline

1

Introduction

2

State Transition Functions Composing State Spaces Compositional Signatures

3

Tree Transducers Bottom-Up Tree Transducers Decomposing Tree Transducers

4

Conclusions

15

slide-58
SLIDE 58

Combining Signatures

Signatures & automata may be combined in the style of “Data types ` a la carte” [Swierstra 2008].

16

slide-59
SLIDE 59

Combining Signatures

Signatures & automata may be combined in the style of “Data types ` a la carte” [Swierstra 2008]. Coproduct of signatures data (f ⊕ g) e = Inl (f e) | Inr (g e)

16

slide-60
SLIDE 60

Combining Signatures

Signatures & automata may be combined in the style of “Data types ` a la carte” [Swierstra 2008]. Coproduct of signatures data (f ⊕ g) e = Inl (f e) | Inr (g e) Example data Inc e = Inc e type Sig′ = Inc ⊕ Sig

16

slide-61
SLIDE 61

Combining Signatures

Signatures & automata may be combined in the style of “Data types ` a la carte” [Swierstra 2008]. Coproduct of signatures data (f ⊕ g) e = Inl (f e) | Inr (g e) Example data Inc e = Inc e type Sig′ = Inc ⊕ Sig Subsignature type class class f g where inj :: f a → g a

16

slide-62
SLIDE 62

Combining Signatures

Signatures & automata may be combined in the style of “Data types ` a la carte” [Swierstra 2008]. Coproduct of signatures data (f ⊕ g) e = Inl (f e) | Inr (g e) Example data Inc e = Inc e type Sig′ = Inc ⊕ Sig Subsignature type class class f g where inj :: f a → g a f g iff g = g1 ⊕ g2 ⊕ ... ⊕ gn and f = gi, 0 < i ≤ n

16

slide-63
SLIDE 63

Combining Signatures

Signatures & automata may be combined in the style of “Data types ` a la carte” [Swierstra 2008]. Coproduct of signatures data (f ⊕ g) e = Inl (f e) | Inr (g e) Example data Inc e = Inc e type Sig′ = Inc ⊕ Sig Subsignature type class class f g where inj :: f a → g a For example: Inc Sig′ f g iff g = g1 ⊕ g2 ⊕ ... ⊕ gn and f = gi, 0 < i ≤ n

16

slide-64
SLIDE 64

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

17

slide-65
SLIDE 65

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

17

slide-66
SLIDE 66

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

17

slide-67
SLIDE 67

Outline

1

Introduction

2

State Transition Functions Composing State Spaces Compositional Signatures

3

Tree Transducers Bottom-Up Tree Transducers Decomposing Tree Transducers

4

Conclusions

18

slide-68
SLIDE 68

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f . . . q

19

slide-69
SLIDE 69

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn q

19

slide-70
SLIDE 70

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn q From terms to contexts data Term f = In (f (Term f )) data Context f a = In (f (Context f a)) | Hole a

19

slide-71
SLIDE 71

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn q From terms to contexts data Term f = In (f (Term f )) data Context f a = In (f (Context f a)) | Hole a type Term f = Context f Empty

19

slide-72
SLIDE 72

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn q From terms to contexts data Term f = In (f (Term f )) data Context f a = In (f (Context f a)) | Hole a Representing transduction rules, [Hasuo et al. 2007] type UpTrans f q g = ∀ a.f (q,a) → (q, Context g a)

19

slide-73
SLIDE 73

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn q From terms to contexts data Term f = In (f (Term f )) data Context f a = In (f (Context f a)) | Hole a Representing transduction rules, [Hasuo et al. 2007] type UpTrans f q g = ∀ a.f (q,a) → (q, Context g a)

19

slide-74
SLIDE 74

Tree Homomorphisms

type UpTrans f q g = ∀ a . f (q, a) → (q, Context g a)

20

slide-75
SLIDE 75

Tree Homomorphisms

type UpTrans f g = ∀ a . f a → Context g a

20

slide-76
SLIDE 76

Tree Homomorphisms

type Hom f g = ∀ a . f a → Context g a

20

slide-77
SLIDE 77

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

20

slide-78
SLIDE 78

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

20

slide-79
SLIDE 79

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 simpCxt :: Functor g ⇒ g a → Context g a simpCxt t = In (fmap Hole t)

20

slide-80
SLIDE 80

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)

21

slide-81
SLIDE 81

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

21

slide-82
SLIDE 82

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

21

slide-83
SLIDE 83

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

21

slide-84
SLIDE 84

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 From stateful homomorphisms to tree transducers upTrans :: (Functor f , Functor g) ⇒ UpState f q → QHom f q g → UpTrans f q g upTrans st hom t = (q, c) where q = st (fmap fst t) c = fmap snd (hom fst t)

21

slide-85
SLIDE 85

An Example

Extending the signature with let bindings type Name = String data Let e = LetIn Name e e | Var Name type LetSig = Let ⊕ Sig

22

slide-86
SLIDE 86

An Example

Extending the signature with let bindings type Name = String data Let e = LetIn Name e e | Var Name type LetSig = Let ⊕ Sig

type Vars = Set Name class FreeVarsSt f where freeVarsSt :: UpState f Vars

22

slide-87
SLIDE 87

An Example

Extending the signature with let bindings type Name = String data Let e = LetIn Name e e | Var Name type LetSig = Let ⊕ Sig

type Vars = Set Name class FreeVarsSt f where freeVarsSt :: UpState f Vars instance FreeVarsSt Sig where freeVarsSt (Plus x y) = x ‘union‘ y freeVarsSt (Val ) = empty instance FreeVarsSt Let where freeVarsSt (Var v) = singleton v freeVarsSt (LetIn v e s) = if v ‘member‘ s then e ‘union‘ delete v s else s

22

slide-88
SLIDE 88

An Example (Cont’d)

class RemLetHom f q g where remLetHom :: QHom f q g instance (Vars ∈ q, Let g, Functor g) ⇒ RemLetHom Let q g where remLetHom qOf (LetIn v s) | ¬ (v ‘member‘ qOf s) = Hole s remLetHom t = simpCxt (inj t) instance (Functor f , Functor g, f g) ⇒ RemLetHom f q g where remLetHom = simpCxt . inj

23

slide-89
SLIDE 89

An Example (Cont’d)

class RemLetHom f q g where remLetHom :: QHom f q g instance (Vars ∈ q, Let g, Functor g) ⇒ RemLetHom Let q g where remLetHom qOf (LetIn v s) | ¬ (v ‘member‘ qOf s) = Hole s remLetHom t = simpCxt (inj t) instance (Functor f , Functor g, f g) ⇒ RemLetHom f q g where remLetHom = simpCxt . inj Combining state transition and homomorphism remLet :: (Functor f , FreeVarsSt f , RemLetHom f Vars f ) ⇒ Term f → (Vars, Term f ) remLet = runUpHom freeVarsSt remLetHom

23

slide-90
SLIDE 90

An Example (Cont’d)

class RemLetHom f q g where remLetHom :: QHom f q g instance (Vars ∈ q, Let g, Functor g) ⇒ RemLetHom Let q g where remLetHom qOf (LetIn v s) | ¬ (v ‘member‘ qOf s) = Hole s remLetHom t = simpCxt (inj t) instance (Functor f , Functor g, f g) ⇒ RemLetHom f q g where remLetHom = simpCxt . inj Combining state transition and homomorphism remLet :: (Functor f , FreeVarsSt f , RemLetHom f Vars f ) ⇒ Term f → (Vars, Term f ) remLet = runUpHom freeVarsSt remLetHom runUpHom :: UpState f q → QHom f q g → Term f → Term g runUpHom st hom = runUpTrans (upTrans st hom)

23

slide-91
SLIDE 91

An Example (Cont’d)

class RemLetHom f q g where remLetHom :: QHom f q g instance (Vars ∈ q, Let g, Functor g) ⇒ RemLetHom Let q g where remLetHom qOf (LetIn v s) | ¬ (v ‘member‘ qOf s) = Hole s remLetHom t = simpCxt (inj t) instance (Functor f , Functor g, f g) ⇒ RemLetHom f q g where remLetHom = simpCxt . inj Combining state transition and homomorphism remLet :: (Functor f , FreeVarsSt f , RemLetHom f Vars f ) ⇒ Term f → (Vars, Term f ) remLet = runUpHom freeVarsSt remLetHom remLet :: Term LetSig → Term LetSig remLet :: Term (Inc ⊕ LetSig) → Term (Inc ⊕ LetSig)

23

slide-92
SLIDE 92

Beyond Bottom-Up Tree Automata

What have we seen? Bottom-up tree acceptors (a.k.a. folds) Bottom-up tree transducers “dependent” versions thereof

24

slide-93
SLIDE 93

Beyond Bottom-Up Tree Automata

What have we seen? Bottom-up tree acceptors (a.k.a. folds) Bottom-up tree transducers “dependent” versions thereof Other Tree recursion schemes Top-down tree acceptors Top-down tree transducers “dependent” versions thereof

24

slide-94
SLIDE 94

Beyond Bottom-Up Tree Automata

What have we seen? Bottom-up tree acceptors (a.k.a. folds) Bottom-up tree transducers “dependent” versions thereof Other Tree recursion schemes Top-down tree acceptors Top-down tree transducers “dependent” versions thereof automata with bidirectional state propagation (restricted versions of macro tree transducers)

24

slide-95
SLIDE 95

What have we gained?

Modularity & Reusability modularity along three dimensions (signature, sequential composition, state space) decoupling of state propagation and tree transformation

  • perations on automata (beyond product & sum) allow us to

construct new automata from old ones

25

slide-96
SLIDE 96

What have we gained?

Modularity & Reusability modularity along three dimensions (signature, sequential composition, state space) decoupling of state propagation and tree transformation

  • perations on automata (beyond product & sum) allow us to

construct new automata from old ones Interface between tree automata dependencies between automata by constraints on the state space modularity allows us to replace individual components

25

slide-97
SLIDE 97

Try It Out!

This is part of the compositional data types Haskell library compdata:

> cabal install compdata

http://hackage.haskell.org/package/compdata

26