Presentation is loading. Please wait.

Presentation is loading. Please wait.

Environments and the Contour Model

Similar presentations


Presentation on theme: "Environments and the Contour Model"— Presentation transcript:

1 Environments and the Contour Model

2 Environments Name Value X 2 Y (lambda (x) (+ x 2)) Z “testing” …
Consist of: frames chained together Name Value X 2 Y (lambda (x) (+ x 2)) Z “testing” Frames are:

3 Global Environment Name Value + … Name Value X 2 Y
Primitives Name Value + Name Value X 2 Y (lambda (x) (+ x 2)) Z “testing” Global user declarations

4 Function Evaluation > (Y 2) Name Value + … Name Value X 2 Y
(lambda (x) (+ x 2)) Z “testing” Name Value X 2

5 Function Evaluation So, X is bound in Y What about +?
Unbound When unbound lookup …

6 Traverse Frames Name Value + … Name Value X 2 Y (lambda (x) (+ x 2)) Z
“testing” Name Value X 2

7 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! Name Value + Name Value X 3 Y 2 Z Name Value X 4 Y 3

8 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

9 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

10 Points at line where function execution ends
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

11 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

12 Primitive-Environment User-Environment
factorial (1) 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

13 Primitive-Environment User-Environment
factorial (1) 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 = 4 P = 5

14 Primitive-Environment User-Environment
factorial (1) factorial’ n 3 P = 4 P = 5 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

15 Primitive-Environment User-Environment
factorial (1) factorial’ n 3 P = 4 P = 5 factorial’’ n 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)) 2 P = 4 factorial’’’ n 1 P = 4

16 Primitive-Environment User-Environment
factorial (1) result 6 P = 6

17 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’ a 5 b 6 f (3) g (8)

18 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)

19 Primitive-Environment User-Environment
a 10 b 20 main (1) Main’ a 5 P = 15 b 6 f (3) g (8) F’ a 6 P = 7

20 Primitive-Environment User-Environment
a 10 b 20 main (1) Main’ a 5 P = 15 b 6 f (3) g (8) G’ F’ a 6 b 7 P = 7 P = 12

21 Primitive-Environment User-Environment
a 10 b 20 main (1) Main’ a 5 P = 15 b 6 f (3) g (8) F’ G’ F’’ a 6 b 7 a 6 P = 7 P = 12 P = 7

22 Primitive-Environment User-Environment
a 10 b 20 main (1) Main’ a 5 P = 15 b 6 f (3) g (8) F’ G’ F’’ G’’ a 6 b 7 a 6 b 7 P = 7 P = 12 P = 7 P = 12

23 Primitive-Environment User-Environment
a 10 P = 18 b 20 main (1) Main’ P = 15 F’ a 5 a 6 P = 7 b 6 f (3) g (8)

24 Primitive-Environment User-Environment
a 10 P = 18 b 20 main (1) Main’ P = 15 F’ a 5 a 6 P = 7 b 6 G’ f (3) b 8 g (8) P = 12

25 Primitive-Environment User-Environment
a 10 P = 18 b 20 main (1) Main’ F’ P = 15 a 5 a 6 P = 7 b 6 G’ f (3) b 8 F’’ g (8) a 8 P = 12 P = 7

26 Primitive-Environment User-Environment
a 10 P = 18 b 20 main (1) Main’ F’ P = 15 a 5 a 6 P = 7 b 6 G’ f (3) b 8 F’’ g (8) G’’ a 8 b 10 P = 12 P = 7 P = 12


Download ppt "Environments and the Contour Model"

Similar presentations


Ads by Google