Download presentation
Presentation is loading. Please wait.
Published byErlin Yuliani Tanudjaja Modified over 5 years ago
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 must memorize these four steps
3
(square 4) | GE square | GE ==> #[proc] ==> 16 * | E1
x: 10 *: #[prim] GE square: E1 parameters: x body: (* x x) A x: 4 square | GE ==> #[proc] (* x x) | E1 ==> 16 * | E1 ==> #[prim] x | E1 ==> 4
4
Example: inc-square inc-square: GE square: p: x b: (* x x)
p: y b: (+ 1 (square y)) (define square (lambda (x) (* x x))) | GE (define inc-square (lambda (y) (+ 1 (square y))) | GE
5
Example cont'd: (inc-square 4) | GE
p: x b: (* x x) square: inc-square: p: y b: ( (square y)) E1 y: 4 inc-square | GE ==> #[compound-proc ...] (+ 1 (square y)) | E1 + | E1 ==> #[prim] (square y) | E1
6
Example cont'd: (square y) | E1
GE p: x b: (* x x) square: inc-square: p: y b: ( (square y)) E1 y: 4 E2 x: 4 square | E1 ==> #[compound] y | E1 ==> 4 (* x x) | E2 ==> 16 (+ 1 16) ==> 17 * | 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
(define ca (make-counter 0)) | GE
p: n b:(lambda () (set! n (+ n 1)) n) make-counter: ca: E1 n: 0 environment pointer points to E1 because the lambda was evaluated in E1 p: b:(set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) | E1
10
(ca) | GE ==> 1 GE p: n b:(lambda () (set! n (+ n 1)) n)
make-counter: E1 n: 0 p: b:(set! n (+ n 1)) n ca: 1 E2 empty (set! n (+ n 1)) | E2 n | E2 ==> 1
11
(ca) | GE ==> 2 GE p: n b:(lambda () (set! n (+ n 1)) n)
make-counter: E1 n: 0 p: b:(set! n (+ n 1)) n ca: 1 2 E3 empty (set! n (+ n 1)) | E3 n | E3 ==> 2
12
(define cb (make-counter 0)) | GE
p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E3 cb: E4 n: 0 p: b:(set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) | E4
13
(cb) | GE ==> 1 n: 0 E4 p: b:(set! n (+ n 1)) n cb: GE
p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E2 1 E5
14
Capturing state in local frames & procedures
p: b:(set! n (+ n 1)) n cb: GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E2
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 the frame 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
p: x b:(define good-enou ..) (define improve ..) sqrt:
(sqrt 2) | GE GE p: x b:(define good-enou ..) (define improve ..) (define sqrt-iter ..) (sqrt-iter 1.0) sqrt: guess: 1 good-enou? E1 x: 2 good-enough: p: guess b:(< (abs ….) improve: sqrt-iter: guess: 1 sqrt-iter
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
Example: Pair/List Mutation
(define a (list 1 2)) (define b a) a ==> (1 2) b ==> (1 2) 1 2 b a 10 X (set-car! a 10) b ==> (10 2)
22
Example 2: Pair/List Mutation
(define x (list 'a 'b)) a b x X 2 1 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
Your Turn 3 4 x x ==> (3 4) y ==> (1 2) (set-car! x y) x ==>
followed by (set-cdr! y (cdr x)) X ((1 4) 4) X 1 2 y ((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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.