260 likes | 361 Views
Environments and the Contour Model. Environments. Consist of: frames chained together. Frames are:. Global Environment. Primitives. Global user declarations. Function Evaluation. > (Y 2). Function Evaluation. So, X is bound in Y What about +? Unbound When unbound lookup ….
E N D
Environments Consist of: frames chained together Frames are:
Global Environment Primitives Global user declarations
Function Evaluation > (Y 2)
Function Evaluation • So, X is bound in Y • What about +? • Unbound • When unbound lookup …
Y 3 What about multiple bindings? • (define x 3) • (define y 2) • (define z (lambda (x) (let ((y 3)) (+ x y)))) • (z 4) X and Y multiply defined. Always use first binding!
Passing Mechanism • By value: • Copy of argument is value for formal argument • Pointer in the case of lists • Copy of string or integer for primitive types
Primitive-Environment User-Environment Points at line where defined b 5 square (2) Points at environment in which defined • line 1: (define b 5) line 2: (define (square num) line 3: (* num num)) line 4: (define result (square 5)) P = 4 Points at line where execution snapshot created
Primitive-Environment User-Environment b 5 square (2) square’ • line 1: (define b 5) line 2: (define (square num) line 3: (* num num)) line 4: (define result (square 5)) num 5 P = 3 P = 4 Points at line where function execution ends Return pointer
Primitive-Environment User-Environment b 5 square 2 result 25 • line 1: (define b 5) line 2: (define (square num) line 3: (* num num)) line 4: (define result (square 5)) P = 5
Primitive-Environment User-Environment (1) factorial line 1: (define (factorial n) line 2: (if (< n 2) line 3: 1 line 4: (* n (factorial (- n 1))))) line 5: (define result (factorial 3)) P = 5
Primitive-Environment User-Environment (1) factorial factorial’ n 3 line 1: (define (factorial n) line 2: (if (< n 2) line 3: 1 line 4: (* n (factorial (- n 1))))) line 5: (define result (factorial 3)) P = 5 P = 4
Primitive-Environment User-Environment (1) factorial factorial’ n 3 P = 5 P = 4 factorial’’ line 1: (define (factorial n) line 2: (if (< n 2) line 3: 1 line 4: (* n (factorial (- n 1))))) line 5: (define result (factorial 3)) n 2 P = 4
Primitive-Environment User-Environment (1) factorial factorial’ n 3 P = 5 P = 4 factorial’’ line 1: (define (factorial n) line 2: (if (< n 2) line 3: 1 line 4: (* n (factorial(- n 1))))) line 5: (define result (factorial 3)) n 2 P = 4 factorial’’’ n 1 P = 4
Primitive-Environment User-Environment (1) factorial 6 result P = 6
Warning Simplified Diagrams! • Next slides are simplified: • Lambdas for a, b, etc not present Extra environment frame is for the let inside of main. Remember: (let ((a 0) (b 0)) <body>) is equivalent to ((lambda (a b) <body>) 0 0) Main’ 5 a 6 b (3) f g (8)
Code Line 1: (define (main) Line 2: (let (( a 0) (b 0)) Line 3: (define (f) Line 4: (let ((a 0)) Line 5: (set! a b) Line 6: (writeln "in f: A and B are - " a b) Line 7: (g))) Line 8: (define (g) Line 9: (let ((b 0)) Line10: (set! b (+ a 2)) Line11: (writeln "in g: A and B are - " a b) Line12: (f))) Line13: (set! a 5) Line14: (set! b 6) Line15: (f))) Line16: (define a 10) Line17: (define b 20) Line18: (main)
Primitive-Environment User-Environment P = 18 10 a 20 b main (1) Main’ 5 a P = 15 6 b (3) f g (8) F’ a 6 P = 7
Primitive-Environment User-Environment P = 18 10 a 20 b main (1) Main’ 5 a P = 15 6 b (3) f g (8) G’ F’ a 6 b 7 P = 7 P = 12
Primitive-Environment User-Environment P = 18 10 a 20 b main (1) Main’ 5 a P = 15 6 b (3) f g (8) F’ G’ F’’ a 6 b 7 a 6 P = 7 P = 12 P = 7
Primitive-Environment User-Environment P = 18 10 a 20 b main (1) Main’ 5 a P = 15 6 b (3) f g (8) F’ G’ F’’ G’’ a 6 b 7 a 6 b 7 P = 7 P = 12 P = 7 P = 12
Primitive-Environment User-Environment 10 P = 18 a 20 b main (1) Main’ P = 15 F’ 5 a a 6 P = 7 6 b (3) f g (8)
Primitive-Environment User-Environment 10 P = 18 a 20 b main (1) Main’ P = 15 F’ 5 a a 6 P = 7 6 b G’ (3) f b 8 g (8) P = 12
Primitive-Environment User-Environment 10 P = 18 a 20 b main (1) Main’ F’ P = 15 5 a a 6 P = 7 6 b G’ (3) f b 8 F’’ g (8) a 8 P = 12 P = 7
Primitive-Environment User-Environment 10 P = 18 a 20 b main (1) Main’ F’ P = 15 5 a a 6 P = 7 6 b G’ (3) f b 8 F’’ g (8) G’’ a 8 b 10 P = 12 P = 7 P = 12