1 / 26

Environments and the Contour Model

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 ….

Download Presentation

Environments and the Contour 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. Environments and the Contour Model

  2. Environments Consist of: frames chained together Frames are:

  3. Global Environment Primitives Global user declarations

  4. Function Evaluation > (Y 2)

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

  6. Traverse Frames

  7. 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!

  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. 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 (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

  13. 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

  14. 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

  15. 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

  16. Primitive-Environment User-Environment (1) factorial 6 result 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’ 5 a 6 b (3) f 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 P = 18 10 a 20 b main (1) Main’ 5 a P = 15 6 b (3) f g (8) F’ a 6 P = 7

  20. 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

  21. 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

  22. 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

  23. 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)

  24. 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

  25. 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

  26. 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

More Related