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

9th Workshop on Generic Programming, 28th September, 2013, Boston, Massachusetts 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

Macro Tree Transducers on One Slide

Tree Transducers in FP

  • automaton transforming trees to trees
  • states are interpreted as functions
  • tree transducer = set of mutually recursive functions

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts 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

Macro Tree Transducers on One Slide

Tree Transducers in FP

  • automaton transforming trees to trees
  • states are interpreted as functions
  • tree transducer = set of mutually recursive functions

Macro tree transducers

  • extension of tree transducers
  • each function may have accumulation parameters

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts 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

This Paper: A Different Interpretation of MTT

still: MTTs as generalisation of top-down tree transducers

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 3

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

This Paper: A Different Interpretation of MTT

still: MTTs as generalisation of top-down tree transducers

Our interpretation of tree transducers

  • literal interpretation: states are still states
  • hence: a single function meta programming

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 3

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

This Paper: A Different Interpretation of MTT

still: MTTs as generalisation of top-down tree transducers

Our interpretation of tree transducers

  • literal interpretation: states are still states
  • hence: a single function meta programming

How so?

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 3

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

This Paper: A Different Interpretation of MTT

still: MTTs as generalisation of top-down tree transducers

Our interpretation of tree transducers

  • literal interpretation: states are still states
  • hence: a single function meta programming

How so?

Macro Tree Transducers = Tree Transducers + parametricity

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 3

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 From String Acceptors to Tree Transducers

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 4

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

Agenda

1 From String Acceptors to Tree Transducers 2 Programming with Tree Transducers in Haskell

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 4

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

Agenda

1 From String Acceptors to Tree Transducers 2 Programming with Tree Transducers in Haskell 3 Tree Transducers with Polymorphic State Space

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 4

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

Agenda

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

(= Tree Transducers with Accumulation Parameters)

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 4

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

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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 q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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 ∈ QF? q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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 ∈ QF?

Acceptor

q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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 ∈ QF?

Transducer?

q, s → q′

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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

Transducer

q, s → q′, w

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 5

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

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 6

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

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 6

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

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 6

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

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 6

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 6

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 6

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 6

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 7

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

Tree Transducers

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

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 7

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 7

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

And now in Haskell

f . . . q′ q′′ q

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 8

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 8

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 8

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 9

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 9

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

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 µf = f ∗ Empty

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 9

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 9

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

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) subst :: Map Var µSig → µSig → µSig subst = transsubstD

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 9

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 10

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 10

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 11

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 11

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 11

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 11

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 11

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 12

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 12

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 13

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 13

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 13

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 13

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

So what?

What do we gain?

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 14

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

So what?

What do we gain?

Practice

  • MTTs as a meta programming framework
  • composition and manipulation of MTTs in a structured

manner

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 14

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

So what?

What do we gain?

Practice

  • MTTs as a meta programming framework
  • composition and manipulation of MTTs in a structured

manner

Theory

  • more elegant proofs of compositionality results (using

parametricity and fold fusion)

  • monadic MTTs: generalisation of non-deterministic / partial

MTTs

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 14

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

Conclusion

Implemented in the compositional data types library:

> cabal install compdata

Patrick Bahr, Laurence E. Day — Programming Macro Tree Transducers — WGP ’13, 28th September, Boston, Massachusetts Slide 15

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 16

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

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 — WGP ’13, 28th September, Boston, Massachusetts Slide 16