Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.

Similar presentations


Presentation on theme: "The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are."— Presentation transcript:

1 The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are resolved in Dylan An expression only has a value with respect to an environment

2 Binding identifier : value Frame unordered set of bindings Environment ordered list of frames

3 x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff)  global environment (top level frame)

4 The Lookup Rule the value of an identifier x is the value associated with x in the first (lowest) frame containing a binding for x undefined if there aren't any

5 Define rule To evaluate (define ident expr): evaluate expr in the current environment add a binding to the top-level frame (the global environment) Set! rule To evaluate (set! ident expr): evaluate expr in the current environment look up ident using lookup rule change that binding to value of expr

6 (define (d ) (+ 5 7)) x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff)

7 (define (d ) (+ 5 7)) x:10 +:{add} y:20 d:12 z:30 x:() w:foobar x:oh y:(list of stuff)

8 x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff) (set! x 'my)

9 x:10 +:{add} y:20 z:30 x:() w:foobar x:my y:(list of stuff) (set! x 'my)

10 Method rule To evaluate (method (params) body) in environment env : create a function object consisting of params body (unevaluated) env Example: value of (method ((x ) (y )) (sqrt (+ (* x x) (* y y)))) in environment env is params: ((x ) (y )) body: (sqrt (+ (* x x) (* y y))) env: env

11 Eval rule To evaluate (f a1... an) in environment e : evaluate f,a1,...,an in e apply f to arguments a1,...,an Apply rule To apply f to arguments a1,...,an : create a new frame with parameters of f bound to arguments a1,...,an append that frame to front of environment e’ in the closure of f to get e’’ evaluate body of f in environment e’’

12 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 x:100 +:{add} y:200 z:300

13 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 x:100 +:{add} y:200 z:300

14 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 x:10 +:{add} y:200 z:300

15 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 x:10 +:{add} y:200 z:300

16 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 z:300

17 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300

18 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300

19 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15

20 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15

21 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15

22 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15y: 20

23 (define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15y: 20


Download ppt "The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are."

Similar presentations


Ads by Google