Programming Macro Tree Transducers Patrick Bahr 1 Laurence E. Day 2 1 - - PowerPoint PPT Presentation

programming macro tree transducers
SMART_READER_LITE
LIVE PREVIEW

Programming Macro Tree Transducers Patrick Bahr 1 Laurence E. Day 2 1 - - PowerPoint PPT Presentation

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Faculty of Science Programming Macro Tree Transducers Patrick Bahr 1 Laurence E. Day 2 1 University of Copenhagen, Department of Computer Science


slide-1
SLIDE 1

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Faculty of Science

Programming Macro Tree Transducers

Patrick Bahr1 Laurence E. Day2

1University of Copenhagen,

Department of Computer Science paba@diku.dk

2University of Nottingham,

Functional Programming Laboratory led@cs.nott.ac.uk

FP Lab Away Day, 13th June, 2013 Slide 1

slide-2
SLIDE 2

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Agenda

1 String Acceptors & String Transducers

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 2

slide-3
SLIDE 3

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Agenda

1 String Acceptors & String Transducers 2 Tree Acceptors & Tree Transducers

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 2

slide-4
SLIDE 4

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Agenda

1 String Acceptors & String Transducers 2 Tree Acceptors & Tree Transducers 3 Programming with Tree Transducers in Haskell

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 2

slide-5
SLIDE 5

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Agenda

1 String Acceptors & String Transducers 2 Tree Acceptors & Tree Transducers 3 Programming with Tree Transducers in Haskell 4 Tree Transducers with Polymorphic State Space

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 2

slide-6
SLIDE 6

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Agenda

1 String Acceptors & String Transducers 2 Tree Acceptors & Tree Transducers 3 Programming with Tree Transducers in Haskell 4 Tree Transducers with Polymorphic State Space 5 Macro Tree Transducers

(= Tree Transducers with Accumulation Parameters)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 2

slide-7
SLIDE 7

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Agenda

1 String Acceptors & String Transducers 2 Tree Acceptors & Tree Transducers 3 Programming with Tree Transducers in Haskell 4 Tree Transducers with Polymorphic State Space 5 Macro Tree Transducers

(= Tree Transducers with Accumulation Parameters)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 2

slide-8
SLIDE 8

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Agenda

1 String Acceptors & String Transducers 2 Tree Acceptors & Tree Transducers 3 Programming with Tree Transducers in Haskell 4 Tree Transducers with Polymorphic State Space 5 Macro Tree Transducers

(= Tree Transducers with Accumulation Parameters)

Why Tree Transducers?

  • Compositionality deforestation
  • Manipulation of transducers
  • Composition with state transition functions

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 2

slide-9
SLIDE 9

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-10
SLIDE 10

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-11
SLIDE 11

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-12
SLIDE 12

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-13
SLIDE 13

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-14
SLIDE 14

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-15
SLIDE 15

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q4 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-16
SLIDE 16

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q4 ∈ QF? q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-17
SLIDE 17

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q4 ∈ QF?

Acceptor

q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-18
SLIDE 18

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q4 ∈ QF?

Transducer?

q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-19
SLIDE 19

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q4

Transducer

q, s → q′, w

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-20
SLIDE 20

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q4

Transducer

q, s → q′, w

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-21
SLIDE 21

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q4

ne w w ε

  • rd

Transducer

q, s → q′, w

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-22
SLIDE 22

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Finite State Automata – On Strings

w

  • r

d

q0 q1 q2 q3 q4

ne w w ε

  • rd

new word Transducer

q, s → q′, w

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 3

slide-23
SLIDE 23

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Now on Trees!

not and not b

  • r

tt b

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 4

slide-24
SLIDE 24

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Now on Trees!

not and not b

  • r

tt b q0

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 4

slide-25
SLIDE 25

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Now on Trees!

not and not b

  • r

tt b q0 q1

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 4

slide-26
SLIDE 26

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Now on Trees!

not and not b

  • r

tt b q0 q1 q2 q3

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 4

slide-27
SLIDE 27

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Now on Trees!

not and not b

  • r

tt b q0 q1 q2 q3 q4 q5 q6

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 4

slide-28
SLIDE 28

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Now on Trees!

not and not b

  • r

tt b q0 q1 q2 q3 q4 q5 q6 q, f → q1, . . . , qn

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 4

slide-29
SLIDE 29

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Now on Trees!

not and not b

  • r

tt b q0 q1 q2 q3 q4 q5 q6 q, f → q1, . . . , qn Often rendered as a rewrite rule: q(f (x1, . . . , xn)) → f (q1(x1), . . . , qn(xn))

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 4

slide-30
SLIDE 30

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Tree Transducers

f . . . f q1

  • q2. . .

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

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 5

slide-31
SLIDE 31

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Tree Transducers

f . . . q′ q′′ q q(f (x1, . . . , xn)) → f (q1(x1), . . . , qn(xn))

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 5

slide-32
SLIDE 32

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Tree Transducers

f . . . q′ q′′ q q(f (x1, . . . , xn)) → t[q′(xi)|q′ ∈ Q, 1 ≤ i ≤ n]

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 5

slide-33
SLIDE 33

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Negation Normal Form

The setting

  • Signature: F = {or/2, and/2, not/1, tt/0, ff/0, b/0}
  • State space: {q0, q1}

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 6

slide-34
SLIDE 34

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Negation Normal Form

The setting

  • Signature: F = {or/2, and/2, not/1, tt/0, ff/0, b/0}
  • State space: {q0, q1}

Transduction rules

q0(not(x1)) → q1(x1) q1(not(x1)) → q0(x1)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 6

slide-35
SLIDE 35

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Negation Normal Form

The setting

  • Signature: F = {or/2, and/2, not/1, tt/0, ff/0, b/0}
  • State space: {q0, q1}

Transduction rules

q0(not(x1)) → q1(x1) q1(not(x1)) → q0(x1) q0(and(x1, x2)) → and(q0(x1), q0(x2)) q1(and(x1, x2)) → or(q1(x1), q1(x2)) q0(or(x1, x2)) → or(q0(x1), q0(x2)) q1(or(x1, x2)) → and(q1(x1), q1(x2))

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 6

slide-36
SLIDE 36

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Negation Normal Form

The setting

  • Signature: F = {or/2, and/2, not/1, tt/0, ff/0, b/0}
  • State space: {q0, q1}

Transduction rules

q0(not(x1)) → q1(x1) q1(not(x1)) → q0(x1) q0(and(x1, x2)) → and(q0(x1), q0(x2)) q1(and(x1, x2)) → or(q1(x1), q1(x2)) q0(or(x1, x2)) → or(q0(x1), q0(x2)) q1(or(x1, x2)) → and(q1(x1), q1(x2)) q0(b) → b q1(b) → not(b) q0(tt) → tt q0(ff) → ff q1(tt) → ff q1(ff) → tt

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 6

slide-37
SLIDE 37

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

A Run of the Transducer

not q0 and not b

  • r

tt b

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 7

slide-38
SLIDE 38

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

A Run of the Transducer

not q0 and not b

  • r

tt b and q1 not b

  • r

tt b q0(not(x1)) → q1(x1)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 7

slide-39
SLIDE 39

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

A Run of the Transducer

not q0 and not b

  • r

tt b and q1 not b

  • r

tt b

  • r

not q1 b

  • r

q1 tt b q1(and(x1, x2)) → or(q1(x1), q1(x2))

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 7

slide-40
SLIDE 40

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

A Run of the Transducer

not q0 and not b

  • r

tt b and q1 not b

  • r

tt b

  • r

not q1 b

  • r

q1 tt b

  • r

b q0 and tt q1 b q1

2

q1(not(x1)) → q0(x1) q1(or(x1, x2)) → and(q1(x1), q1(x2))

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 7

slide-41
SLIDE 41

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

A Run of the Transducer

not q0 and not b

  • r

tt b and q1 not b

  • r

tt b

  • r

not q1 b

  • r

q1 tt b

  • r

b q0 and tt q1 b q1

  • r

b and ff not b

2 3

q0(b) → b q1(tt) → ff q1(b) → not(b)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 7

slide-42
SLIDE 42

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

And now in Haskell

f . . . q′ q′′ q

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 8

slide-43
SLIDE 43

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

And now in Haskell

f . . . q′ q′′ q

Representation in Haskell

type TransD f q g = ∀a . (q, f a) → g∗(q, a)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 8

slide-44
SLIDE 44

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

And now in Haskell

f . . . q′ q′′ q

Representation in Haskell

type TransD f q g = ∀a . (q, f a) → g∗(q, a)

Free Monad of a Functor g

data g∗ a = Re a | In (g (g∗ a))

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 8

slide-45
SLIDE 45

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Substitution

type Var = String data Sig a = Add a a | Val Int | Let Var a a | Var Var

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 9

slide-46
SLIDE 46

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Substitution

type Var = String data Sig a = Add a a | Val Int | Let Var a a | Var Var transsubst :: TransD Sig (Map Var µSig) Sig transsubst (m, Var v) = case Map.lookup v m of Nothing → iVar v Just t → toFree t transsubst (m, Let v b s) = iLet v (Re (m, b)) (Re (m \ v, s)) transsubst (m, Val n) = iVal n transsubst (m, Add x y) = Re (m, x) ‘iAdd‘ Re (m, y)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 9

slide-47
SLIDE 47

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Substitution

type Var = String data Sig a = Add a a | Val Int | Let Var a a | Var Var transsubst :: TransD Sig (Map Var µSig) Sig transsubst (m, Var v) = case Map.lookup v m of Nothing → iVar v Just t → toFree t transsubst (m, Let v b s) = iLet v (Re (m, b)) (Re (m \ v, s)) transsubst (m, Val n) = iVal n transsubst (m, Add x y) = Re (m, x) ‘iAdd‘ Re (m, y) type TransD f q g = ∀a . (q, f a) → g∗(q, a)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 9

slide-48
SLIDE 48

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Substitution

type Var = String data Sig a = Add a a | Val Int | Let Var a a | Var Var transsubst :: TransD Sig (Map Var µSig) Sig transsubst (m, Var v) = case Map.lookup v m of Nothing → iVar v Just t → toFree t transsubst (m, Let v b s) = iLet v (Re (m, b)) (Re (m \ v, s)) transsubst (m, Val n) = iVal n transsubst (m, Add x y) = Re (m, x) ‘iAdd‘ Re (m, y) type TransD f q g = ∀a . (q, f a) → g∗(q, a) subst :: Map Var µSig → µSig → µSig subst = transsubstD

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 9

slide-49
SLIDE 49

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Non-Example: Inlining

transinline :: TransD Sig (Map Var µSig) Sig transinline (m, Var v) = case Map.lookup v m of Nothing → iVar v Just e → toFree e transinline (m, Let v b s) = Re (m [v → b] , s) transinline (m, Val n) = iVal n transinline (m, Add x y) = Re (m, x) ‘iAdd‘ Re (m, y) inline :: µSig → µSig inline = transinlineD ∅

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 10

slide-50
SLIDE 50

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Non-Example: Inlining

transinline :: TransD Sig (Map Var µSig) Sig transinline (m, Var v) = case Map.lookup v m of Nothing → iVar v Just e → toFree e transinline (m, Let v b s) = Re (m [v → b] , s) transinline (m, Val n) = iVal n transinline (m, Add x y) = Re (m, x) ‘iAdd‘ Re (m, y) inline :: µSig → µSig inline = transinlineD ∅

Recall the type TransD

type TransD f q g = ∀a . (q, f a) → g∗(q, a)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 10

slide-51
SLIDE 51

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Transducers with Polymorphic State Space

The original type TransD

type TransD f q g = ∀a . (q, f a) → g∗(q, a)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 11

slide-52
SLIDE 52

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Transducers with Polymorphic State Space

The original type TransD

type TransD f q g = ∀a . (q, f a) → g∗(q, a)

An equivalent representation

type TransD f q g = ∀a.q → f a → g∗(q, a)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 11

slide-53
SLIDE 53

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Transducers with Polymorphic State Space

The original type TransD

type TransD f q g = ∀a . (q, f a) → g∗(q, a)

An equivalent representation

type TransD f q g = ∀a.q → f (q → a) → g∗ a

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 11

slide-54
SLIDE 54

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Transducers with Polymorphic State Space

The original type TransD

type TransD f q g = ∀a . (q, f a) → g∗(q, a)

An equivalent representation

type TransD f q g = ∀a.q → f (q → a) → g∗ a

Deriving the type TransM

type TransM f q g = ∀ a.q a → f (q a → a) → g∗ a

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 11

slide-55
SLIDE 55

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Transducers with Polymorphic State Space

The original type TransD

type TransD f q g = ∀a . (q, f a) → g∗(q, a)

An equivalent representation

type TransD f q g = ∀a.q → f (q → a) → g∗ a

Deriving the type TransM

type TransM f q g = ∀ a.q a → f (q (g∗ a) → a) → g∗ a

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 11

slide-56
SLIDE 56

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Inlining

transinline :: Trans′M Sig (Map Var) Sig transinline m (Var v) = case Map.lookup v m of Nothing → iVar v Just e x → e transinline m (Let v b s) = s (m [v → b m]) transinline m (Val n) = iVal n transinline m (Add x y) = x m ‘iAdd‘ y m

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 12

slide-57
SLIDE 57

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Example: Inlining

transinline :: Trans′M Sig (Map Var) Sig transinline m (Var v) = case Map.lookup v m of Nothing → iVar v Just e x → e transinline m (Let v b s) = s (m [v → b m]) transinline m (Val n) = iVal n transinline m (Add x y) = x m ‘iAdd‘ y m inline :: µSig → µSig inline = transinlineM ∅

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 12

slide-58
SLIDE 58

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Macro Tree Transduction Rule Illustrated

f . . . f q2 q q1 type TransM f q g = ∀ a. q a → f (q (g∗ a) → a

  • ) → g∗ a

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 13

slide-59
SLIDE 59

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Macro Tree Transduction Rule Illustrated

f . . . f q2 q q1 . . . type TransM f q g = ∀ a. q a → f (q (g∗ a) → a

  • ) → g∗ a

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 13

slide-60
SLIDE 60

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Macro Tree Transduction Rule Illustrated

f . . . f q2 q q1 . . .

q3 q4

type TransM f q g = ∀ a. q a → f (q (g∗ a) → a

  • ) → g∗ a

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 13

slide-61
SLIDE 61

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Macro Tree Transduction Rule Illustrated

f . . . f q2 q q1 . . .

q3 q4

. . . type TransM f q g = ∀ a. q a → f (q (g∗ a) → a

  • ) → g∗ a

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 13

slide-62
SLIDE 62

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Conclusion

Why Tree Transducers?

  • Compositionality deforestation
  • Manipulation of transducers
  • Composition with state transition functions

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 14

slide-63
SLIDE 63

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Conclusion

Why Tree Transducers?

  • Compositionality deforestation
  • Manipulation of transducers
  • Composition with state transition functions

Implementation

> cabal install compdata

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 14

slide-64
SLIDE 64

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Conclusion

Why Tree Transducers?

  • Compositionality deforestation
  • Manipulation of transducers
  • Composition with state transition functions

Implementation

> cabal install compdata

Future work

  • proper formalisation
  • monadic transducers
  • graph transducers

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 14

slide-65
SLIDE 65

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Bonus Slide: Definition of Macro Tree Transducers

q(f (x1, . . . , xn), y1, . . . , ym) → u for each f /n ∈ F and q/(m + 1) ∈ Q

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 15

slide-66
SLIDE 66

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e

Bonus Slide: Definition of Macro Tree Transducers

q(f (x1, . . . , xn), y1, . . . , ym) → u for each f /n ∈ F and q/(m + 1) ∈ Q Where u ∈ RHSn,m, which is defined as follows: 1 ≤ i ≤ m yi ∈ RHSn,m g/k ∈ G u1, . . . , uk ∈ RHSn,m g(u1, . . . , uk) ∈ RHSn,m 1 ≤ i ≤ n q′/(k + 1) ∈ Q u1, . . . , uk ∈ RHSn,m q′(xi, u1, . . . , uk) ∈ RHSn,m

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — FP Lab Away Day, 13th June, 2013 Slide 15