Practice session #9: The environment model Environment Model: Introduction Environments: • Frame: GE A substitution from variables to values (i.e., every variable in a frame has a single value). I x:3 y:5 • 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. II z:6 x:7 III n:1 y:2 E1 E2 EI<=EG ,>I,III<=2E ,>I,II<=1> Enclosing-env (E1) = GE Enclosing-env (E2) = GE Environment Model: Introduction Environments: • The value of variable in an environment: GE The value of x in the environment E is its values in the first frame in which it is define. I x:3 y:5 II z:6 x:7 III n:1 y:2 E1 E2 EI<=EG ,>I,III<=2E ,>I,II<=1> Enclosing-env (E1) = GE Enclosing-env (E2) = GE Environment Model: Introduction Procedures: • A procedure value (closure) is a data structure with: (1) Procedure parameters. (2) Procedure body. (3) The environment in which it has been created. b1 > (define square (lambda (x) (* x x))) > (lambda (y) y) b2 GE square: p:(x( b(x x *(:1 p:(y( by:2 Environment Model: Introduction Procedures: • 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. b1 > (define square (lambda (x) (* x x))) > (square 5) GE square: p:(x( b(x x *(:1 E1 B1 x:5 GE Environment Model: Definition and Application GE sq: p: (x) b1: (* x x) sum-of-squares: f: p :(x y( b2: (+ (sq x) (sq y)) p: (a) b3: (sum-of-squares (+ a 1) (* a 2)) Environment Model: Definition and Application GE sq: p: (x) b1: (* x x) sum-of-squares: f: p :(x y( b2: (+ (sq x) (sq y)) E1 B3 GE 136 a:5 E2 x:6 y:10 B2 E1 136 p: (a) b3: (sum-of-squares (+ a 1) (* a 2)) E3 x:6 B1 E2 36 E4 x:10 B1 E2 100 Environment Model: Definition and Let GE a:8 b:5 c:8 f: p: (x y) b1: (+ x y) E1 x:8 y:8 b1 GE 16 Environment Model: Definition and Let GE a:8 b:5 c:8 f: p: (x y) b1: (+ x y) p: E1 b2 a:8 b:5 c:3 p: (a b c) b2: (let…) GE 53 p: (d e) b3: (f e d) E2 b3 E1 53 d:13 e:40 E3 b1 x:40 y:13 E2 53 Environment Model: Recursion GE fact: p:(n) b:(if…) E1 b n:3 GE 6 E2 b n:2 E1 2 E3 b n:1 E2 1 E4 b n:0 E3 1 Environment Model: Pair ADT, Lazy implementation GE make-pair: P1: E1 b1 x:5 y:10 p:(x y) b1:(lambda(sel)…) GE p:(sel) b2:(sel x y) Environment Model: Pair ADT, Lazy implementation GE make-pair: P1: E1 x:5 y:10 p:(a b) b3:a E2 sel: b1 b2 p:(x y) b1:(lambda(sel)…) GE p:(sel) b2:(sel x y) GE 5 E3 a:5 b:10 b3 E2 5 > (p1 (lambda (a b) a)) Environment Model: Pair ADT, Lazy implementation GE make-pair: P1: E1 p:(x y) b3:(p1(lambda…) E2 b3 x:1 y:2 b1 x:5 y:10 GE p:(first second) b4:first p:(x y) b1:(lambda(sel)…) GE p:(sel) b2:(sel x y) E3 b1 E2 sel: E4 b4 first:5 second:10 > (let ((x 1) (y 2)) b3 (p1 (lambda (first second) first))) b4 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). Environment Model: Dynamic Scoping - Example GE make-pair: p1: E1 p:(x y) b1:(lambda(sel)…) b1 p:(sel) b2:(sel x y) x:5 y:10 1 1 E2 E3 b3 x:1 y:2 p:(x y) b3:(p1(lambda…) b2 sel: p:(first second) b4:first 1 E4 b4 first:1 second:2 > (let ((x 1) (y 2)) b3 (p1 (lambda (first second) first))) b4 Environment Model: CPS GE fact:$ p:(n c) b1:(if…) p(fact-3) b3:(fact-3) E1 b1 n:3 c: E2 GE 6 b1 n:2 c: p:(fact-n-1) b2:(c…) b2 fact-n-1:2 b3 fact-n-1:6 E3 E5 6 b1 n:1 c: E2 6 E4 p:(fact-n-1) b2:(c…) E5 E6 E1 6 b2 fact-n-1:1 E4 6 E3 6
© Copyright 2026 Paperzz