1 / 16

The Environment Model

GE. The Environment Model. a:8 b:5 c:8. p: (x y) b 1 : (+ x y). p: (a b c) b 2 : (let…). f:. p:. Practice Session 10. GE 53. E 1. b 2. E 1 53. E 2 53. a:8 b:5 c:3. E 2. b 3. E 3. b 1. d:13 e:40. x:40 y:13. p: (d e) b 3 : (f e d). Environment Model: Introduction.

idalee
Download Presentation

The Environment Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GE The Environment Model a:8 b:5 c:8 p: (x y) b1: (+ x y) p: (a b c) b2: (let…) f: p: Practice Session 10 GE 53 E1 b2 E1 53 E2 53 a:8 b:5 c:3 E2 b3 E3 b1 d:13 e:40 x:40 y:13 p: (d e) b3: (f e d)

  2. Environment Model: Introduction Environments: • 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 E2 E1 • E1=<II,I>, E2=<III,I>, GE=<I> • Enclosing-env(E1) = GEEnclosing-env(E2) = GE

  3. Environment Model: Introduction Environments: • 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. GE I x:3 y:5 II z:6 x:7 III n:1 y:2 E2 E1 • E1=<II,I>, E2=<III,I>, GE=<I> • Enclosing-env(E1) = GEEnclosing-env(E2) = GE

  4. 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( p:(y( b:y

  5. 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( B E1 GE x:5

  6. Environment Model: Definition and Application GE sq: sum-of-squares: f: p: (x) b1: (* x x) p:(x y( • b2: (+ (sq x) • (sq y)) • p: (a) • b3: (sum-of-squares • (+ a 1) • (* a 2))

  7. Environment Model: Definition and Application GE sq: sum-of-squares: f: p: (x) b1: (* x x) p:(x y( • b2: (+ (sq x) • (sq y)) GE 136 E1 B3 a:5 • p: (a) • b3: (sum-of-squares • (+ a 1) • (* a 2)) E1 136 E2 B2 E2 36 x:6 y:10 E3 B1 E2 100 x:6 E4 B1 x:10

  8. Environment Model: Definition and Let GE a:8 b:5 c:8 p: (x y) b1: (+ x y) f: GE 16 E1 b1 x:8 y:8

  9. Environment Model: Definition and Let GE a:8 b:5 c:8 p: (x y) b1: (+ x y) f: p: p: (a b c) b2: (let…) GE 53 E1 b2 a:8 b:5 c:3 E1 53 E2 b3 d:13 e:40 E2 53 E3 b1 p: (d e) b3: (f e d) x:40 y:13

  10. Environment Model: Recursion GE fact: p:(n) b:(if…) E2 1 E3 1 E1 2 GE 6 E3 b E4 b E2 b E1 b n:1 n:0 n:2 n:3

  11. Environment Model: Pair ADT GE make-pair: p:(x y) b1:(lambda(sel)…) • P1: E1 b1 GE p:(sel) b2:(sel x y) x:5 y:10

  12. Environment Model: Pair ADT GE make-pair: p:(x y) b1:(lambda(sel)…) • P1: > (p1 (lambda (a b) a)) E1 b1 GE p:(sel) b2:(sel x y) x:5 y:10 p:(a b) b3:a GE E2 b2 5 sel: b3 E3 E2 5 a:5 b:10

  13. Environment Model: Pair ADT GE make-pair: p:(x y) b1:(lambda(sel)…) • P1: > (let ((x 1) (y 2)) (p1 (lambda (first second) first))) b3 E1 b1 GE b4 p:(sel) b2:(sel x y) x:5 y:10 p:(x y) b3:(p1(lambda…) E2 b3 E3 b1 GE E2 x:1 y:2 sel: E4 b4 p:(first second) b4:first first:5 second:10

  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. Environment Model: Dynamic Scoping - Example GE make-pair: p:(x y) b1:(lambda(sel)…) p1: > (let ((x 1) (y 2)) (p1 (lambda (first second) first))) E1 b1 p:(sel) b2:(sel x y) x:5 y:10 b3 b4 1 1 p:(x y) b3:(p1(lambda…) E3 b2 E2 b3 x:1 y:2 sel: p:(first second) b4:first E4 b4 1 first:1 second:2

  16. Environment Model: CPS GE fact$: p:(n c) b1:(if…) p(fact-3) b3:(fact-3) E1 b1 GE • 6 E2 b1 E1 6 E3 b1 • E2 • 6 n:3 n:2 n:1 c: c: c: p:(fact-n-1) b2:(c…) p:(fact-n-1) b2:(c…) E4 b2 • E3 • 6 fact-n-1:1 E5 b2 • E4 • 6 fact-n-1:2 E6 b3 • E5 6 fact-n-1:6

More Related