demeterf: functions and traversals in combination

43
DemeterF: Functions and Traversals in Combination by Brian Chadwick

Upload: reilly

Post on 25-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

DemeterF: Functions and Traversals in Combination. by Brian Chadwick. Introduction. Do EoPL functions using functional transformations Translate a class dictionary into a traverser (that works with a visitor) transformer (that works with function objects). Default Transformer copy object. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: DemeterF: Functions and Traversals in Combination

DemeterF:Functions and Traversals in

Combinationby Brian Chadwick

Page 2: DemeterF: Functions and Traversals in Combination

Introduction

• Do EoPL functions using functional transformations

• Translate a class dictionary into a– traverser (that works with a visitor)– transformer (that works with function objects)

Page 3: DemeterF: Functions and Traversals in Combination

Default Transformercopy object

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 4: DemeterF: Functions and Traversals in Combination

Parameterize Default Transformer

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

PathSpec apply(J j) { return new Complement(j); }

Page 5: DemeterF: Functions and Traversals in Combination

Parameterize Default Transformer

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

PathSpec combine(J j, Boolean fn, Boolean sn) { return fn && sn; }

PathSpec combine(Object j, Boolean fn, Boolean sn) { return fn && sn; }

Page 6: DemeterF: Functions and Traversals in Combination

Simple applicationProgram transformation

• Old– E : Num | Var | Op | Call …– Op : Plus | Equals.– Equals = “=“.

• New– E : … | Bool.– Bool : True | False.

class BoolTrans extends IDf { static E newtrue = Call.parse(“(= 1 1)”), static E newfalse= Call.parse(“(= 1 0) “); E apply(True t) {return newtrue; } E apply(False t) {return newfalse; }}

apply for transformation of result returned by builder

Page 7: DemeterF: Functions and Traversals in Combination

de Bruijn indices

• Old– Var : Sym.– Sym = Ident.

• New– Var : Sym | Addr.– Addr = Integer.

class AddrTrans extends IDf { Var apply(Var var, SymList senv) { return new Addr(senv.lookup(var));}}class SymExtender extends IDa { SymList update(Lambda l, SymList senv) { return senv.push(l.formals); }}

for later

Page 8: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpec

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 9: DemeterF: Functions and Traversals in Combination

well-formed movie

• show how the default builder is modified to combine Boolean objects.

Page 10: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecwell-formed specialization 1

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

#t: true#f: false

#t #t #t #t

#t

Page 11: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecwell-formed specialization 2

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

#t: true#f: false

#t #t #t #t

#t #t

Page 12: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecwell-formed specialization 3

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

#t: true#f: false

#t #t #f #t

#t

Page 13: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecwell-formed specialization 4

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

#t: true#f: false

#t #t #f #t

#t

#f

Page 14: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecwell-formed specialization 5

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

#t: true#f: false

#t #t #f #t

#f

#t

#t

Page 15: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecwell-formed specialization 6

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

#t: true#f: false

#t #t #f #t

#f#t

Page 16: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecwell-formed specialization 7

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

#t: true#f: false

#t #t #f #t

#f

Page 17: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecwell-formed specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

#t: true#f: false

#t #t #f #t

Page 18: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 19: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

:S means a copy

Page 20: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 21: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 22: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 23: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 24: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 25: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

Page 26: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

insert NOT

:N

Page 27: DemeterF: Functions and Traversals in Combination

The default Builder for PathSpecNOT_JOIN_MERGE specialization

:S:S

:J

:S:S

:J

:M

1

2

3 4

56

7 8

9

10after blue arrowcopy is built(like after)

Count only uponfirst visit (red) andupon final visit (blue).For leaf nodes, countonly in red.

insert NOT

:N :N

:M

:N

Page 28: DemeterF: Functions and Traversals in Combination

Illustration of combinefor capacity constraint violation

:C

:Cons

:E

:Cons

:E :Empty

:C

:Cons

:Cons:E

:E

(w1,0)

(w2,0)

(w3,0)

(w4,0)

(0,0)

(w1,0)

:Empty (0,0)

(w2,0)

(w2+w3,0)

(w2+w3,1)

(w1+w2+w3,1)

:Cons (w1+w2+w3+w4,1)(w1+w2+w3+w4,2)

both containers (C) violate capacity constraints

123

4

5

6

3a

7

7a

8

9

9a

10

10a

11

11a

12

12a13

13a

14

15

15a

16

16a

1717a

1818a

1919a

20

after blue arrowcombine isactive (like after)

Page 29: DemeterF: Functions and Traversals in Combination

Illustration of combinefor capacity constraint violation

:C

:Cons

:E

:Cons

:E :Empty

:C

:Cons

:Cons:E

:E

(w1,0)

(w2,0)

(w3,0)

(w4,0)

(0,0)

(w1,0)

:Empty (0,0)

(w2,0)

(w2+w3,0)

(w2+w3,1)

(w1+w2+w3,1)

:Cons (w1+w2+w3+w4,1)(w1+w2+w3+w4,2)

both containers (C) violate capacity constraints

Page 30: DemeterF: Functions and Traversals in Combination

Theory

t[f,b](d) => d’, where d’=f(d), d is atomict[f,b](c(d0, … ,dn)) => f(b(c(d0, … ,dn), d’0, … ,d’n)), where d’i =

t[f,b](di)Default functions:id[f](d) => did[b](c(d0, … ,dn), d’0, … ,d’n) => c(d0, … ,dn)b[c](c(d0, … ,dn), d’0, … ,d’n) => c(d’0, … ,d’n)

On left side of => the term c(…) only indicates a compound object.

f = applyb = combine

Page 31: DemeterF: Functions and Traversals in Combination

Theory

• f is a polymorphic function that takes a single argument and returns a result.

• b is a function object that is responsible for reconstruction of data types.

Page 32: DemeterF: Functions and Traversals in Combination

Traversal Component Approach

• implemented in DemeterF• Traversal with 3 components: Builder

(combine), Transformer (apply), Augmentor (update)

Page 33: DemeterF: Functions and Traversals in Combination

Augmentors / update methods

• so far: we covered combine and apply methods– combine: to combine information up the object– apply: to transform before the information is sent up.– up refers to the traversal: when a traversal has

finished visiting an object, it goes up.• add: update, to send information down the object• if it's not used/needed, it does not need to be

mentioned, since the traversal will do the passing around.

Page 34: DemeterF: Functions and Traversals in Combination

motivating example:Address computation

Var : Sym | Addr.Addr = Integer.class AddrTrans extends IDf { Var apply(Var var, SymList senv) { return new Addr(senv.lookup(var)); } }class SymExtender extends IDa { SymList update(Lambda la, Symlist senv){ return senv.push(la.formals); } }

Page 35: DemeterF: Functions and Traversals in Combination

Optional argument

• The argument passed *into* the traversal (by the programmer) is available everywhere (in everyapply/combine/update function) but it may be needed only in some objects.

• In the typechecker case is only needed when looking up a variable use, or modifying the type-environment with a binding.  

Page 36: DemeterF: Functions and Traversals in Combination

Like a let for subtraversals

• An update argument can be viewed (almost) as a 'let' for sub-traversals: before traversing sub-terms we do a recalculation of the traversal argument. Update is called before traversing sub terms, and themodified value is only available for sub terms.

Page 37: DemeterF: Functions and Traversals in Combination

Structure-shy passing of argument• If you look at the Scheme code you would write to

traverse the same structure; you would pass alongan argument to the functions, all the way through recursive calls until it is needed. At a lambdathe recursive call on the body would look something like:

  (type-Lambda l tenv)    (cases Exp l       (Lambda (arg body)         (type-Exp body (tenv-extend tenv arg)))

       (Call (op arglist)          (let ((ret (type-Exp op tenv)) ...)

       (... other cases...)))

Page 38: DemeterF: Functions and Traversals in Combination

Default: passing it along:no code needed with DemeterF

• Note that we don't change the traversal argument in most cases, only when binding an argument to a type. Usually we just keep passing it along throughout the traversal functions. The augmentor/update methods encapsulate (only) the changes of this argument, so we simply write:

• TEnv update(Lambda l, TEnv te) {return te.extend(l.formal);}• The traversal takes care of the default case

when we don't need to change the argument.

Page 39: DemeterF: Functions and Traversals in Combination

All arguments are optional

• All arguments are now optional... thus the most general method is:    Object combine(){ return ...; }Since it is applicable in all cases (all arguments optional, including the 'traversal argument').(the traversal argument is the one updated by update methods. There is only one traversal argument)

Page 40: DemeterF: Functions and Traversals in Combination

AST illustration• Call

    |--- Lambda    |      |--- Arg    |      |     |--- Type    |      |     |     |--- int    |      |     |    |      |     |--- Sym    |      |           |--- 'a'    |      |    |      |--- Call    |            |--- Plus    |            |--- Sym    |            |     |--- 'a'    |            |    |            |--- Num    |                  |--- 2    |---  Num           |--- 4

Update is called at each label and the Object returned is then available to all terms 'connected'and to the right of that label.

Page 41: DemeterF: Functions and Traversals in Combination

Technical Details

• Methods you want to call determine from where you inherit: ID (all), IDa (update), IDb (combine), IDba (combine, update), IDf (apply), IDfa (apply, update), IDfb(apply, combine).

Page 42: DemeterF: Functions and Traversals in Combination

Motivation

• Showing a full scheme type-checking function, and highlighting the points wherethe type-environment is passed, used, and extended.  The number of cases where it is just passed along motivates the want/need to put the modifications in one place, and being able to ignore theargument when it's not really needed.

Page 43: DemeterF: Functions and Traversals in Combination

Traversal

• a function: