1 / 28

Lecture 13: Assignment and the Environment Model (EM)

Lecture 13: Assignment and the Environment Model (EM). Sections 3.1 and 3.2. To apply a compound procedure P to arguments:. 1. Create a new frame A 2. Make A into an environment E: A's enclosing environment pointer goes to the same frame as the environment pointer of P

olinda
Download Presentation

Lecture 13: Assignment and the Environment Model (EM)

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. Lecture 13: Assignment and the Environment Model (EM) Sections 3.1 and 3.2

  2. To apply a compound procedure P to arguments: 1. Create a new frame A 2. Make A into an environment E: A's enclosing environment pointer goes to the same frame as the environment pointer of P 3. In A, bind the parameters of P to the argument values 4. Evaluate the body of P with E as the current environment You mustmemorize thesefour steps

  3. A E1 (square 4) | GE (* x x) | E1 x: 10 *: #[prim] GE square: parameters: xbody: (* x x) x: 4 square | GE ==> #[proc] ==> 16 x | E1 ==> 4 * |E1 ==> #[prim]

  4. Example: inc-square (define square (lambda (x) (* x x))) | GE (define inc-square (lambda (y) (+ 1 (square y))) | GE inc-square: GE square: p: xb: (* x x) p: yb: (+ 1 (square y))

  5. inc-square: GE square: E1 p: xb: (* x x) p: yb: (+ 1 (square y)) Example cont'd: (inc-square 4) | GE y: 4 inc-square | GE ==> #[compound-proc ...] (+ 1 (square y)) | E1 + |E1 ==> #[prim] (square y) | E1

  6. inc-square: GE square: E2 E1 y: 4 p: xb: (* x x) p: yb: (+ 1 (square y)) Example cont'd: (square y) | E1 x: 4 square | E1 ==> #[compound] y | E1 ==> 4 (* x x) | E2 (+ 1 16) ==> 17 ==> 16 * |E2 ==> #[prim] x | E2 ==> 4

  7. Lessons from the inc-square example • EM doesn't show the complete state of the interpreter • missing the stack of pending operations • The GE contains all standard bindings (*, cons, etc) • omitted from EM drawings • Useful to link environment pointer of each frame to the procedure that created it

  8. Example: make-counter • Counter: something which counts up from a number (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0))(ca) ==> 1(ca) ==> 2(define cb (make-counter 0))(cb) ==> 1(ca) ==> 3(cb) ==> 2 ; ca and cb are independent

  9. make-counter: GE ca: E1 p: nb:(lambda () (set! n (+ n 1)) n) p: b:(set! n (+ n 1)) n (define ca (make-counter 0)) | GE n: 0 environment pointerpoints to E1because the lambdawas evaluated in E1 (lambda () (set! n (+ n 1)) n) | E1

  10. make-counter: GE ca: E1 1 n: 0 p: nb:(lambda () (set! n (+ n 1)) n) E2 p: b:(set! n (+ n 1)) n (ca) | GE ==> 1 empty (set! n (+ n 1)) | E2 n | E2 ==> 1

  11. make-counter: GE ca: E1 1 n: 0 p: nb:(lambda () (set! n (+ n 1)) n) 2 E3 p: b:(set! n (+ n 1)) n (ca) | GE ==> 2 empty (set! n (+ n 1)) | E3 n | E3 ==> 2

  12. make-counter: GE cb: ca: E1 E4 n: 2 p: nb:(lambda () (set! n (+ n 1)) n) E3 p: b:(set! n (+ n 1)) n p: b:(set! n (+ n 1)) n (define cb (make-counter 0)) | GE n: 0 (lambda () (set! n (+ n 1)) n) | E4

  13. make-counter: GE ca: cb: E1 E4 n: 2 n: 0 p: nb:(lambda () (set! n (+ n 1)) n) 1 E2 E5 p: b:(set! n (+ n 1)) n p: b:(set! n (+ n 1)) n (cb) | GE ==> 1

  14. make-counter: GE ca: cb: E1 E4 n: 2 n: 1 p: nb:(lambda () (set! n (+ n 1)) n) E2 p: b:(set! n (+ n 1)) n p: b:(set! n (+ n 1)) n Capturing state in local frames & procedures

  15. Lessons from the make-counter example • Environment diagrams get complicated very quickly • Rules are meant for the computer to follow, not to help humans • A lambda inside a procedure body captures theframe that was active when the lambda was evaluated • this effect can be used to store local state

  16. Lets do a message passing example together (define (cons x y) (define (dispatch op) (cond ((eq? op 'car) x) ((eq? op 'cdr) y) (else (error "Unknown op -- CONS" op)))) dispatch) (define (car x) (x 'car)) (define (cdr x) (x 'cdr)) (define a (cons 1 2)) (car a)

  17. Another thing the substitution model did not explain well: • Nested definitions : block structure (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0))

  18. sqrt: GE E1 x: 2 good-enough: improve: sqrt-iter: p: xb:(define good-enou ..) (define improve ..) (define sqrt-iter ..) (sqrt-iter 1.0) p: guessb:(< (abs ….) guess: 1 sqrt-iter guess: 1 good-enou? (sqrt 2) | GE

  19. Mutators for compound data

  20. Compound Data • constructor: (cons x y) creates a new pair p • selectors: (car p) returns car part of pair (cdr p) returns cdr part of pair • mutators: (set-car! p new-x) changes car pointer in pair (set-cdr! p new-y) changes cdr pointer in pair ; Pair,anytype -> undef -- side-effect only!

  21. a X b 1 2 10 Example: Pair/List Mutation (define a (list 1 2)) (define b a) a ==> (1 2) b ==> (1 2) (set-car! a 10) b ==> (10 2)

  22. X x a b 1 2 Example 2: Pair/List Mutation (define x (list 'a 'b)) • How mutate to achieve the result at right? • (set-car! (cdr x) (list 1 2)) • Eval (cdr x) to get a pair object • Change car pointer of that pair object

  23. Sharing, Equivalence and Identity • How can we tell if two things are equivalent? -- Well, what do you mean by "equivalent"? • The same object: test with eq?(eq? a b) ==> #t • Objects that "look" the same: test with equal?(equal? (list 1 2) (list 1 2)) ==> #t(eq? (list 1 2) (list 1 2)) ==> #f • If we change an object, is it the same object? -- Yes, if we retain the same pointer to the object • How tell if parts of an object is shared with another? -- If we mutate one, see if the other also changes

  24. X x y 3 1 4 2 X ((1 4) 4) Your Turn x ==> (3 4) y ==> (1 2) (set-car! x y) x ==> followed by (set-cdr! y (cdr x)) x ==> ((1 2) 4)

  25. Summary • Scheme provides built-in mutators • set! to change a binding • set-car! and set-cdr! to change a pair • Mutation introduces substantial complexity • Unexpected side effects • Substitution model is no longer sufficient to explain behavior

  26. The Implementation of Pairs (define (cons x y) (let ((new (get-new-pair))) (set-car! new x) (set-cdr! new y) new))

  27. Mutation as Assignment (1) (define (cons x y) (define (set-x! v) (set! x v)) (define (set-y! v) (set! y v)) (lambda (m) (cond ((eq? m 'car) x) ((eq? m 'cdr) y) ((eq? m 'set-car!) set-x!) ((eq? m 'set-cdr!) set-y!) (else (error "Undefined operation" m)))))

  28. Mutation as Assignment (2) (define (car z) (z 'car)) (define (cdr z) (z 'cdr)) (define (set-car! z new-value) ((z 'set-car!) new-value) z) (define (set-cdr! z new-value) ((z 'set-cdr!) new-value) z)

More Related