280 likes | 378 Views
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
E N D
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 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
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]
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))
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
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
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
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
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
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
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
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
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
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
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
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)
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))
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
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!
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)
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
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
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)
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
The Implementation of Pairs (define (cons x y) (let ((new (get-new-pair))) (set-car! new x) (set-cdr! new y) new))
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)))))
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)