Download presentation
Presentation is loading. Please wait.
Published byAbraham Cooper Modified over 9 years ago
1
p: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1 E3E3 x:40 y:13 E 2 53 Practice Session 10 The Environment Model
2
Frame: A substitution, a mapping between variables and values. Every variable in a frame has a single value. Environment: A finite sequence of frames, where the last frame is the global environment. The global environment: A single-frame environment, the only environment that statically exists. Enclosing environment: For an environment E, the enclosing environment is E, excluding its first frame. GE I x:3 y:5 II z:6 x:7 III n:1 y:2 E1 E2 E 1 =, E 2 =, GE= Enclosing-env (E 1 ) = GE Enclosing-env (E 2 ) = GE Environments: Environment Model: Introduction
3
GE I x:3 y:5 II z:6 x:7 III n:1 y:2 E1 E2 E 1 =, E 2 =, GE= Enclosing-env (E 1 ) = GE Enclosing-env (E 2 ) = GE The value of variable in an environment: The value of x in the environment E is its values in the first frame in which it is define. Environments: Environment Model: Introduction
4
A procedure value (closure) is a data structure with: (1) Procedure parameters. (2) Procedure body. (3) The environment in which it has been created. Procedures: > (define square (lambda (x) (* x x))) GE square: p:(x( b:(* x x( p:(y( b:y > (lambda (y) y) b1 b2 Environment Model: Introduction
5
Procedure application: (1) Create new frame, mapping the procedure params to application values. (2) The new frame extends the environment associated with the procedure. (3) Evaluate the body of the procedure in the new environment. Procedures: GE square: p:(x( b:(* x x( GE E1E1 B x:5 > (define square (lambda (x) (* x x))) > (square 5) b1 Environment Model: Introduction
6
GE sq: sum-of-squares: f: p: (x) b 1 : (* x x) p: (x y( b 2 : (+ (sq x) (sq y)) p: (a) b 3 : (sum-of-squares (+ a 1) (* a 2)) Environment Model: Definition and Application
7
B3B3 E1E1 a:5 GE 136 B2B2 E2E2 x:6 y:10 B1B1 E3E3 x:6 B1B1 x:10 E2E2 100 E1E1 136 GE sq: sum-of-squares: f: p: (x) b 1 : (* x x) p: (x y( b 2 : (+ (sq x) (sq y)) p: (a) b 3 : (sum-of-squares (+ a 1) (* a 2)) Environment Model: Definition and Application E2E2 36 E4E4
8
Environment Model: Definition and Let GE a:8 b:5 c:8 f: b1b1 E1E1 x:8 y:8 GE 16 p: (x y) b 1 : (+ x y)
9
GE 53 b2b2 E1E1 a:8 b:5 c:3 p: (a b c) b 2 : (let…) E1E1 53 b3b3 E2E2 d:13 e:40 E2E2 53 b1b1 E3E3 x:40 y:13 f: p: (x y) b 1 : (+ x y) p: p: (d e) b 3 : (f e d) Environment Model: Definition and Let GE a:8 b:5 c:8
10
GE p:(n) b:(if…) fact: bE1E1 n:3 GE 6 bE2E2 n:2 E1E1 2 bE3E3 n:1 E2E2 1 bE4E4 n:0 E3E3 1 Environment Model: Recursion
11
GE p:(x y) b 1 :(lambda(sel)…) make-pair: P1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 GE Environment Model: Pair ADT
12
GE b2b2 E2E2 sel: b3b3 E3E3 a:5 b:10 E25E25 GE p:(x y) b 1 :(lambda(sel)…) make-pair: P1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 GE p:(a b) b 3 :a Environment Model: Pair ADT > (p1 (lambda (a b) a)) 5
13
p:(x y) b 3 :(p1(lambda…) b3b3 E2E2 x:1 y:2 GE p:(first second) b 4 :first b1b1 E3E3 E2E2 sel: b4b4 E4E4 first:5 second:10 GE p:(x y) b 1 :(lambda(sel)…) make-pair: P1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 GE Environment Model: Pair ADT > (let ((x 1) (y 2)) (p1 (lambda (first second) first))) b4 b3
14
Environment Model: Lexical (static) vs Dynamic Scoping Lexical Scoping: A closure “carries” the environment in which it has been created. In application of a closure, its carried environment is extended. Dynamic Scoping: A closure does not correspond to any environment. In application of a closure, the calling environment is extended. Simpler implementation (no need to “store” environments).
15
GE p:(x y) b 1 :(lambda(sel)…) make-pair: p1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 p:(x y) b 3 :(p1(lambda…) b3b3 E2E2 x:1 y:2 p:(first second) b 4 :first b2b2 E3E3 sel: b4b4 E4E4 first:1 second:2 1 1 1 Environment Model: Dynamic Scoping - Example > (let ((x 1) (y 2)) (p1 (lambda (first second) first))) b4 b3
16
p:(n c) b 1 :(if…) GE fact$: b1b1 E1E1 n:3 GE 6 p(fact-3) b 3 :(fact-3) c: p:(fact-n-1) b 2 :(c…) b1b1 E2E2 n:2 E1E1 6 c: p:(fact-n-1) b 2 :(c…) b1b1 E3E3 n:1 E2E2 6 c: b2b2 E4E4 fact-n-1:1 E3E3 6 b2b2 E5E5 fact-n-1:2 E4E4 6 b3b3 E6E6 fact-n-1:6 E5E5 6 Environment Model: CPS
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.