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

deriving modular recursion schemes from tree automata
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

Outline

1

Tree Automata Bottom-Up Tree Acceptors Bottom-Up Tree Transducers

2

Introducing Modularity Composing State Spaces Compositional Signatures Decomposing Tree Transducers

3

Other Automata

2

slide-3
SLIDE 3

Outline

1

Tree Automata Bottom-Up Tree Acceptors Bottom-Up Tree Transducers

2

Introducing Modularity Composing State Spaces Compositional Signatures Decomposing Tree Transducers

3

Other Automata

3

slide-4
SLIDE 4

Bottom-Up Tree Acceptors

f q1

  • q2. . .

qn f q . . .

4

slide-5
SLIDE 5

Bottom-Up Tree Acceptors

f q1

  • q2. . .

qn f q . . . f (q1(x1), q2(x2), . . . , qn(xn)) → q(f (x1, x2, . . . , xn))

4

slide-6
SLIDE 6

An Example

The signature F = {and/2, not/1, tt/0, ff/0} e.g.: not(and(not(ff), and(tt, ff)))

5

slide-7
SLIDE 7

An Example

The signature F = {and/2, not/1, tt/0, ff/0} e.g.: not(and(not(ff), and(tt, ff))) The states set of states: Q = {q0, q1} q0 false q1 true accepting states: Qa = {q1}

5

slide-8
SLIDE 8

An Example

The signature F = {and/2, not/1, tt/0, ff/0} e.g.: not(and(not(ff), and(tt, ff))) The states set of states: Q = {q0, q1} q0 false q1 true accepting states: Qa = {q1} The rules of the automaton ff → q0(ff) tt → q1(tt) not(q0(x)) → q1(not(x)) not(q1(x)) → q0(not(x)) and(q1(x), q1(y)) → q1(and(x, y)) and(q0(x), q1(y)) → q0(and(x, y)) and(q1(x), q0(y)) → q0(and(x, y)) and(q0(x), q0(y)) → q0(and(x, y))

5

slide-9
SLIDE 9

A Run of a Bottom-Up Tree Acceptor

not and not ff and tt ff

6

slide-10
SLIDE 10

A Run of a Bottom-Up Tree Acceptor

not and not ff and tt ff not and not q0 ff and q1 tt q0 ff

3

ff → q0(ff) tt → q1(tt)

6

slide-11
SLIDE 11

A Run of a Bottom-Up Tree Acceptor

not and not ff and tt ff not and not q0 ff and q1 tt q0 ff not and q1 not ff q0 and tt ff

3 2

not(q0(x)) → q1(not(x)) and(q1(x), q0(y)) → q0(and(x, y))

6

slide-12
SLIDE 12

A Run of a Bottom-Up Tree Acceptor

not and not ff and tt ff not and not q0 ff and q1 tt q0 ff not and q1 not ff q0 and tt ff not q0 and not ff and tt ff

3 2

not(q0(x)) → q1(not(x)) and(q1(x), q0(y)) → q0(and(x, y))

6

slide-13
SLIDE 13

A Run of a Bottom-Up Tree Acceptor

not and not ff and tt ff not and not q0 ff and q1 tt q0 ff not and q1 not ff q0 and tt ff not q0 and not ff and tt ff q1 not and not ff and tt ff

3 2

not(q0(x)) → q1(not(x)) and(q1(x), q0(y)) → q0(and(x, y))

6

slide-14
SLIDE 14

Terms in Haskell

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

7

slide-15
SLIDE 15

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

7

slide-16
SLIDE 16

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 Example data F a = And a a | Not a | TT | FF

7

slide-17
SLIDE 17

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 Example data F a = And a a | Not a | TT | FF instance Functor F where fmap f TT = TT fmap f (And x y) = And (f x) (f y) . . .

7

slide-18
SLIDE 18

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 Example data F a = And a a | Not a | TT | FF deriving Functor instance Functor F where fmap f TT = TT fmap f (And x y) = And (f x) (f y) . . .

7

slide-19
SLIDE 19

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn f q . . .

8

slide-20
SLIDE 20

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn q

8

slide-21
SLIDE 21

Bottom-Up State Transitions in Haskell

f q1

  • q2. . .

qn q1 q2 qn q

8

slide-22
SLIDE 22

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

8

slide-23
SLIDE 23

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)

8

slide-24
SLIDE 24

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

8

slide-25
SLIDE 25

An Example

Signature data F a = And a a | Not a | TT | FF

9

slide-26
SLIDE 26

An Example

Signature data F a = And a a | Not a | TT | FF States data Q = Q0 | Q1 Accepting states acc :: Q → Bool acc Q1 = True acc Q0 = False

9

slide-27
SLIDE 27

An Example

Signature data F a = And a a | Not a | TT | FF States data Q = Q0 | Q1 Accepting states acc :: Q → Bool acc Q1 = True acc Q0 = False State transition function trans :: F Q → Q trans FF = Q0 trans TT = Q1 trans (Not Q0) = Q1 trans (Not Q1) = Q0 trans (And Q1 Q1) = Q1 trans (And ) = Q0

9

slide-28
SLIDE 28

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f . . . q

10

slide-29
SLIDE 29

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn q

10

slide-30
SLIDE 30

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn q f (q1(x1), q2(x2), . . . , qn(xn)) − → q(t) f ∈ F t ∈ T (G, X) X = {x1, x2, . . . , xn}

10

slide-31
SLIDE 31

An Example

The signature F = {and/2, not/1, ff/0, tt/0, b/0}

11

slide-32
SLIDE 32

An Example

The signature F = {and/2, not/1, ff/0, tt/0, b/0} The states q0 false q1 true q2 don’t know

11

slide-33
SLIDE 33

An Example

The signature F = {and/2, not/1, ff/0, tt/0, b/0} The states q0 false q1 true q2 don’t know Transduction rules tt → q1(tt) ff → q0(ff) b → q2(b) not(q0(x)) → q1(tt) not(q1(x)) → q0(ff) not(q2(x)) → q2(not(x)) and(q(x), p(y)) → q0(ff) if q0 ∈ {p, q} and(q1(x), q1(y)) → q1(tt) and(q1(x), q2(y)) → q2(y) and(q2(x), q1(y)) → q2(x) and(q2(x), q2(y)) → q2(and(x, y))

11

slide-34
SLIDE 34

A Run of a Bottom-Up Transducer

and not b not and ff b

12

slide-35
SLIDE 35

A Run of a Bottom-Up Transducer

and not b not and ff b and not q2 b not and q0 ff q2 b

3

ff → q0(ff) b → q2(b)

12

slide-36
SLIDE 36

A Run of a Bottom-Up Transducer

and not b not and ff b and not q2 b not and q0 ff q2 b and q2 not b not q0 ff

3 2

not(q2(x)) → q2(not(x)) and(q(x), p(y)) → q0(ff) if q0 ∈ {p, q}

12

slide-37
SLIDE 37

A Run of a Bottom-Up Transducer

and not b not and ff b and not q2 b not and q0 ff q2 b and q2 not b not q0 ff and q2 not b q1 tt

3 2

not(q0(x)) → q1(tt)

12

slide-38
SLIDE 38

A Run of a Bottom-Up Transducer

and not b not and ff b and not q2 b not and q0 ff q2 b and q2 not b not q0 ff and q2 not b q1 tt q2 not b

3 2

and(q2(x), q1(y)) → q2(x)

12

slide-39
SLIDE 39

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f q

13

slide-40
SLIDE 40

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f q ∈ T (G, X)

13

slide-41
SLIDE 41

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f q ∈ T (G, X) From terms to contexts data Term f = In (f (Term f )) data Context f a = In (f (Context f a)) | Hole a

13

slide-42
SLIDE 42

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f q ∈ T (G, X) 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

13

slide-43
SLIDE 43

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f q ∈ T (G, X) 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)

13

slide-44
SLIDE 44

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f q ∈ T (G, X) 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)

13

slide-45
SLIDE 45

Bottom-Up Tree Transducers

f q1

  • q2. . .

qn f q ∈ T (G, X) f (q1(x1), q2(x2), . . . , qn(xn)) − → q(t) 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)

13

slide-46
SLIDE 46

An Example

Signature And state data F a = And a a | Not a | TT | FF | B data Q = Q0 | Q1 | Q2

14

slide-47
SLIDE 47

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

14

slide-48
SLIDE 48

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 → q1(tt) trans TT = (Q1, tt )

14

slide-49
SLIDE 49

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 → q1(tt) trans TT = (Q1, tt ) tt :: Context F a tt = In TT

14

slide-50
SLIDE 50

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 → q1(tt) trans TT = (Q1, tt ) not(q2(x)) → q2(not(x)) trans (Not (Q2, x)) = (Q2, not (Hole x))

14

slide-51
SLIDE 51

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 → q1(tt) trans TT = (Q1, tt ) not(q2(x)) → q2(not(x)) trans (Not (Q2, x)) = (Q2, not (Hole x)) not :: Context F a → Context F a not = In . Not

14

slide-52
SLIDE 52

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 → q1(tt) trans TT = (Q1, tt ) not(q2(x)) → q2(not(x)) trans (Not (Q2, x)) = (Q2, not (Hole x)) and(q(x), p(y)) → q0(ff) if q0 ∈ {q, p}

14

slide-53
SLIDE 53

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 → q1(tt) trans TT = (Q1, tt ) not(q2(x)) → q2(not(x)) trans (Not (Q2, x)) = (Q2, not (Hole x)) and(q(x), p(y)) → q0(ff) if q0 ∈ {q, p} trans (And (q, x) (p, y)) | q ≡ Q0 ∨ p ≡ Q0 = (Q0, ff )

14

slide-54
SLIDE 54

Outline

1

Tree Automata Bottom-Up Tree Acceptors Bottom-Up Tree Transducers

2

Introducing Modularity Composing State Spaces Compositional Signatures Decomposing Tree Transducers

3

Other Automata

15

slide-55
SLIDE 55

Composing State Spaces – Motivating Example

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

16

slide-56
SLIDE 56

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

slide-57
SLIDE 57

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

slide-58
SLIDE 58

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

16

slide-59
SLIDE 59

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

slide-60
SLIDE 60

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

slide-61
SLIDE 61

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

slide-62
SLIDE 62

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

slide-63
SLIDE 63

Product Automata

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

18

slide-64
SLIDE 64

Product Automata

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

18

slide-65
SLIDE 65

Product Automata

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

18

slide-66
SLIDE 66

Product Automata

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

18

slide-67
SLIDE 67

Product Automata

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

18

slide-68
SLIDE 68

Product Automata

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

18

slide-69
SLIDE 69

Product Automata

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

18

slide-70
SLIDE 70

Product Automata

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

18

slide-71
SLIDE 71

Running Dependent State Transition Functions

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

19

slide-72
SLIDE 72

Running Dependent State Transition Functions

The types type UpState f q = f q → 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

slide-73
SLIDE 73

Running Dependent State Transition Functions

The types type UpState f q = f q → 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

slide-74
SLIDE 74

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

slide-75
SLIDE 75

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

slide-76
SLIDE 76

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

slide-77
SLIDE 77

Outline

1

Tree Automata Bottom-Up Tree Acceptors Bottom-Up Tree Transducers

2

Introducing Modularity Composing State Spaces Compositional Signatures Decomposing Tree Transducers

3

Other Automata

21

slide-78
SLIDE 78

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

slide-79
SLIDE 79

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

slide-80
SLIDE 80

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

slide-81
SLIDE 81

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

slide-82
SLIDE 82

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

slide-83
SLIDE 83

Subsignatures

Subsignature type class class f g where inj :: f a → g a

24

slide-84
SLIDE 84

Subsignatures

Subsignature type class class f g where inj :: f a → g a f g iff g = g1 ⊕ g2 ⊕ ... ⊕ g n and f = gi, 0 < i ≤ n

24

slide-85
SLIDE 85

Subsignatures

Subsignature type class class f g where inj :: f a → g a For example: Inc Inc ⊕ Sig

  • Sig′

f g iff g = g1 ⊕ g2 ⊕ ... ⊕ g n and f = gi, 0 < i ≤ n

24

slide-86
SLIDE 86

Subsignatures

Subsignature type class class f g where inj :: f a → g a For example: Inc Inc ⊕ Sig

  • Sig′

f g iff g = g1 ⊕ g2 ⊕ ... ⊕ g n and f = gi, 0 < i ≤ n Injection and projection functions inject :: (g f ) ⇒ g (Context f a) → Context f a inject = In . inj

24

slide-87
SLIDE 87

Outline

1

Tree Automata Bottom-Up Tree Acceptors Bottom-Up Tree Transducers

2

Introducing Modularity Composing State Spaces Compositional Signatures Decomposing Tree Transducers

3

Other Automata

25

slide-88
SLIDE 88

Tree Homomorphisms

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

26

slide-89
SLIDE 89

Tree Homomorphisms

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

26

slide-90
SLIDE 90

Tree Homomorphisms

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

26

slide-91
SLIDE 91

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

slide-92
SLIDE 92

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

slide-93
SLIDE 93

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)

26

slide-94
SLIDE 94

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

slide-95
SLIDE 95

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

slide-96
SLIDE 96

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

slide-97
SLIDE 97

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

slide-98
SLIDE 98

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

slide-99
SLIDE 99

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

slide-100
SLIDE 100

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

slide-101
SLIDE 101

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

28

slide-102
SLIDE 102

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

28

slide-103
SLIDE 103

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 (LetIn v s) | ¬ (v ‘member‘ below s) = Hole s remLetHom t = simpCxt (inj t) instance (Functor f , Functor g, f g) ⇒ RemLetHom f q g where remLetHom = simpCxt . inj

29

slide-104
SLIDE 104

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 (LetIn v s) | ¬ (v ‘member‘ below 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

29

slide-105
SLIDE 105

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 (LetIn v s) | ¬ (v ‘member‘ below 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 st hom = runUpTrans (upTrans st hom)

29

slide-106
SLIDE 106

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 (LetIn v s) | ¬ (v ‘member‘ below 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)

29

slide-107
SLIDE 107

Outline

1

Tree Automata Bottom-Up Tree Acceptors Bottom-Up Tree Transducers

2

Introducing Modularity Composing State Spaces Compositional Signatures Decomposing Tree Transducers

3

Other Automata

30

slide-108
SLIDE 108

Top-Down Tree Transducers

f q1

  • q2. . .

qn f q1 q2 q q

31

slide-109
SLIDE 109

Top-Down Tree Transducers

f q1

  • q2. . .

qn f q1 q2 q q

31

slide-110
SLIDE 110

Top-Down Tree Transducers

f q1

  • q2. . .

qn f q1 q2 q q q(f (x1, x2, . . . , xn)) − → t f ∈ F t ∈ T (G, Q(X)) Q(X) = {p(xi) | p ∈ Q, 1 ≤ i ≤ n}

31

slide-111
SLIDE 111

Top-Down Tree Transducers

f q1

  • q2. . .

qn f q1 q2 q q q(f (x1, x2, . . . , xn)) − → t f ∈ F t ∈ T (G, Q(X)) Q(X) = {p(xi) | p ∈ Q, 1 ≤ i ≤ n} Representation in Haskell type DownTrans f q g = ∀ a . (q, f a) → Context g (q, a)

31

slide-112
SLIDE 112

Decomposing Top-Down Tree Transducers

State transition depends on transformation Successor states are assigned to variable occurrences on right-hand side. q0(f (x)) → g(q1(x), q2(x))

32

slide-113
SLIDE 113

Decomposing Top-Down Tree Transducers

State transition depends on transformation Successor states are assigned to variable occurrences on right-hand side. q0(f (x)) → g(q1(x), q2(x)) Assignment to variables instead restriction: if q(x) and p(x) occur on right-hand side, then p = q.

32

slide-114
SLIDE 114

Decomposing Top-Down Tree Transducers

State transition depends on transformation Successor states are assigned to variable occurrences on right-hand side. q0(f (x)) → g(q1(x), q2(x)) Assignment to variables instead restriction: if q(x) and p(x) occur on right-hand side, then p = q. How to represent top-down state transformations? first try: type DownState f q = ∀ a . (q, f a) → f q

32

slide-115
SLIDE 115

Decomposing Top-Down Tree Transducers

State transition depends on transformation Successor states are assigned to variable occurrences on right-hand side. q0(f (x)) → g(q1(x), q2(x)) Assignment to variables instead restriction: if q(x) and p(x) occur on right-hand side, then p = q. How to represent top-down state transformations? first try: type DownState f q = ∀ a . (q, f a) → f q permits changing the shape of the input: bad :: ∀ a . (q, Sig a) → Sig q bad (q, Plus x y) = Val 1 bad (q, Val i) = Val 1

32

slide-116
SLIDE 116

Top-Down State Transitions

Using explicit placeholders type DownState f q = ∀ i . Ord i ⇒ (q, f i) → Map i q

33

slide-117
SLIDE 117

Top-Down State Transitions

Using explicit placeholders type DownState f q = ∀ i . Ord i ⇒ (q, f i) → Map i q construct function of type ∀ a . (q, f a) → f q that preserves the shape

33

slide-118
SLIDE 118

Top-Down State Transitions

Using explicit placeholders type DownState f q = ∀ i . Ord i ⇒ (q, f i) → Map i q construct function of type ∀ a . (q, f a) → f q that preserves the shape Combining with stateful tree homomorphisms

type QHom f q g = ∀ a . (?above :: q, ?below :: a → q) ⇒ f a → Context g a

33

slide-119
SLIDE 119

Top-Down State Transitions

Using explicit placeholders type DownState f q = ∀ i . Ord i ⇒ (q, f i) → Map i q construct function of type ∀ a . (q, f a) → f q that preserves the shape Combining with stateful tree homomorphisms

type QHom f q g = ∀ a . (?above :: q, ?below :: a → q) ⇒ f a → Context g a type DownTrans f q g = ∀ a . (q, f a) → Context g (q, a)

33

slide-120
SLIDE 120

Why Tree Transducers

More structure, more flexibility Tree transducers can manipulated more easily

34

slide-121
SLIDE 121

Why Tree Transducers

More structure, more flexibility Tree transducers can manipulated more easily, e.g. data (f :&: a) e = f e :&: a

34

slide-122
SLIDE 122

Why Tree Transducers

More structure, more flexibility Tree transducers can manipulated more easily, e.g. data (f :&: a) e = f e :&: a lift :: UpTrans f q g → UpTrans (f :&: a) q (g :&: a)

34

slide-123
SLIDE 123

Why Tree Transducers

More structure, more flexibility Tree transducers can manipulated more easily, e.g. data (f :&: a) e = f e :&: a lift :: UpTrans f q g → UpTrans (f :&: a) q (g :&: a) Tree transducers compose We may leverage composition theorems for tree transducers. comp :: DownTrans g p h → DownTrans f q g → DownTrans f (q, p) h

34

slide-124
SLIDE 124

Why Tree Transducers

More structure, more flexibility Tree transducers can manipulated more easily, e.g. data (f :&: a) e = f e :&: a lift :: UpTrans f q g → UpTrans (f :&: a) q (g :&: a) Tree transducers compose We may leverage composition theorems for tree transducers. comp :: DownTrans g p h → DownTrans f q g → DownTrans f (q, p) h potential for fusion

34

slide-125
SLIDE 125

Beyond Tree Transducers

Bidirectional state transitions bottom-up + top-down state transition bottom-up + top-down state transition + stateful homomorphism

35

slide-126
SLIDE 126

Beyond Tree Transducers

Bidirectional state transitions bottom-up + top-down state transition bottom-up + top-down state transition + stateful homomorphism Macro Tree Transducers states may have arguments taken from the term necessary for ’non-local’ transformations, e.g. substitution, inlining

35

slide-127
SLIDE 127

Beyond Tree Transducers

Bidirectional state transitions bottom-up + top-down state transition bottom-up + top-down state transition + stateful homomorphism Macro Tree Transducers states may have arguments taken from the term necessary for ’non-local’ transformations, e.g. substitution, inlining

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

35

slide-128
SLIDE 128

Beyond Tree Transducers

Bidirectional state transitions bottom-up + top-down state transition bottom-up + top-down state transition + stateful homomorphism Macro Tree Transducers states may have arguments taken from the term necessary for ’non-local’ transformations, e.g. substitution, inlining

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

35

slide-129
SLIDE 129

Beyond Tree Transducers

Bidirectional state transitions bottom-up + top-down state transition bottom-up + top-down state transition + stateful homomorphism Macro Tree Transducers states may have arguments taken from the term necessary for ’non-local’ transformations, e.g. substitution, inlining

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

35

slide-130
SLIDE 130

Beyond Tree Transducers

Bidirectional state transitions bottom-up + top-down state transition bottom-up + top-down state transition + stateful homomorphism Macro Tree Transducers states may have arguments taken from the term necessary for ’non-local’ transformations, e.g. substitution, inlining

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

35

slide-131
SLIDE 131

Beyond Tree Transducers

Bidirectional state transitions bottom-up + top-down state transition bottom-up + top-down state transition + stateful homomorphism Macro Tree Transducers states may have arguments taken from the term necessary for ’non-local’ transformations, e.g. substitution, inlining

type UpTrans’ f q g = ∀ a . f (q a, a) → (q (Context g a), Context g a) type DownTrans’ f q g = ∀ a . (q a, f a) → Context g (q (Context g a), a) e.g. for substitutions: q = Map Var

35

slide-132
SLIDE 132

Beyond Tree Transducers

Bidirectional state transitions bottom-up + top-down state transition bottom-up + top-down state transition + stateful homomorphism Macro Tree Transducers states may have arguments taken from the term necessary for ’non-local’ transformations, e.g. substitution, inlining

type UpTrans’ f q g = ∀ a . f (q a, a) → (q (Context g a), Context g a) type DownTrans’ f q g = ∀ a . (q a, f a) → Context g (q (Context g a), a) e.g. for substitutions: q = Map Var

generic programming

35