400 likes | 428 Views
Explores assignment, substitution model, and local state procedures in Scheme programming, illustrating how environments impact variable binding and evaluation rules.
E N D
Lecture 13 - Assignment and the environments modelChapter 3 מבוא מורחב - שיעור 13
Primitive Data • constructor: (define x 10) creates a new binding for name; special form • selector:x returns value bound to name • mutator:(set! x "foo") changes the binding for name; special form מבוא מורחב
Assignment -- set! • Substitution model -- functional programming:(define x 10)(+ x 5) ==> 15 - expression has same value... each time it evaluated (in(+ x 5) ==> 15same scope as binding) • With assignment:(define x 10)(+ x 5) ==> 15 - expression "value" depends... on when it is evaluated(set! x 94)...(+ x 5) ==> 99 מבוא מורחב
Why? To have objects with local state • The state is summarized in a set of variables. • When the object changes its state the variable changes its value. מבוא מורחב
Procedures with local state Suppose we want to implement counters (define ca (make-counter 0)) 1 (ca) ==> 2 (ca) ==> (define cb (make-counter 0)) (cb) ==> 1 3 (ca) ==> How can we define such procedures? מבוא מורחב - שיעור 13
set! does assignment to an already defined variable. It is a special form. ca is a procedure. Variable n is its local state. A counter: an object with state • (define make-counter (lambda (n) (lambda ()(set! n (+ n 1)) n ))) • (define ca (make-counter 0)) מבוא מורחב - שיעור 13
Can the substitution model explain this behavior? (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0)) ca ==> (lambda () (set! 0 (+ 0 1)) 0) (ca) ((set! 0 (+ 0 1)) 0) ==> ? The substitution model is not adequate for assignment! מבוא מורחב - שיעור 13
Substitution model: a single global environment Environment Table Name Value score 23 The Environments Model Environments model: many environments. Generalizes the substitution model. מבוא מורחב - שיעור 13
Example: x is bound to 15 in frame Ay is bound to (1 2) in frame A the value of the variable x in frame A is 15 A x: 15 y: 1 2 Frame: a table of bindings • Binding: a pairing of a name and a value מבוא מורחב - שיעור 13
B z: 10 E2 A x: 15 E1 y: 1 2 Environment: a sequence of frames • Environment E1 consists of frames A and B • Environment E2 consists of frame B only • A frame may be shared by multiple environments this arrow is calledthe enclosingenvironment pointer מבוא מורחב - שיעור 13
Evaluation in the environment model • All evaluations occur in an environment • The current environment changes when theinterpreter applies a procedure • The top environment is called the global environment (GE) • Only the GE has no enclosing environment מבוא מורחב - שיעור 13
The Environment Model • A precise, completely mechanical, description of: • name-rule looking up the value of a variable • define-rule creating a new definition of a var • set!-rule changing the value of a variable • lambda-rule creating a procedure • application rule applying a procedure • Enables analyzing arbitrary scheme code: • Example: make-counter • Basis for implementing a scheme interpreter • for now: draw EM state with boxes and pointers • later on: implement with code מבוא מורחב - שיעור 13
In E1, the binding of x in frame A shadows the binding of x in B B • x | GE ==> 3 z: 10x: 3 GE A x: 15 E1 y: 1 2 Name-rule • A name X evaluated in environment E givesthe value of X in the first frame of E where X is bound • z | GE ==> 10 z | E1 ==> 10 x | E1 ==> 15 מבוא מורחב - שיעור 13
B z: 10 x: 3 GE z: 20 A x: 15 y: E1 1 2 Define-rule • A define special form evaluated in environment Ecreates or replaces a binding in the first frame of E (define z 25) | E1 (define z 20) | GE z: 25 מבוא מורחב - שיעור 13
B z: 10 x: 3 GE 20 25 A x: 15 y: E1 1 2 Set!-rule • A set! of variable X evaluated in environment E changes the binding of X in the first frame of E where X is bound (set! z 20) | GE (set! z 25) | E1 מבוא מורחב - שיעור 13
11 1 2 Your turn: evaluate the following in order 11 (+ z 1) | E1 ==> (set! z (+ z 1)) | E1 (modify EM) (define z (+ z 1)) | E1 (modify EM) (set! y (+ z 1)) | GE (modify EM) Error:unbound variable: y B z: 10x: 3 GE A x: 15 z: 12 E1 y: מבוא מורחב - שיעור 13
(lambda (x) (* x x)) print Environmentpointer #[proc-...] evallambda-rule A compound procthat squares itsargument Code pointer parameters: xbody: (* x x) Double bubble: how to draw a procedure מבוא מורחב - שיעור 13
B z: 10x: 3 A x: 15 E1 square: Evaluating a lambda actually returns a pointer to the procedure object parameters: xbody: (* x x) Lambda-rule • A lambda special form evaluated in environment Ecreates a procedure whose environment pointer points to E (define square (lambda (x) (* x x)))| E1 environment pointerpoints to frame Abecause the lambdawas evaluated in E1and E1 A מבוא מורחב - שיעור 13
To apply a compound procedure P to arguments (Application Rule) 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 We recommend youmemorize thesefour steps מבוא מורחב - שיעור 13
A E1 frame becomes inaccessible (square 4) | GE (* x x) | E1 x: 10 *: #[prim] GE square: parameters: xbody: (* x x) x: 4 square | GE ==> ==> 16 x | E1 ==> 4 * |E1 ==> מבוא מורחב - שיעור 13
inc-square: square: p: xb: (* x x) p: yb: (+ 1 (square y)) Example: inc-square (define square (lambda (x) (* x x))) | GE GE (define inc-square (lambda (y) (+ 1 (square y))) | GE מבוא מורחב - שיעור 13
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 ==> (+ 1 (square y)) | E1 + |E1 ==> #[prim] (square y) | E1 מבוא מורחב - שיעור 13
E2 E1 frames become inaccessible Example cont'd: (inc-square 4) | E1 inc-square: GE square: y: 4 x: 4 p: xb: (* x x) p: yb: (+ 1 (square y)) | E1 square | E1 ==> y | E1 ==> 4 (* x x) | E2 (+ 1 16) ==> 17 ==> 16 * |E2 ==> #[prim] x | E2 ==> 4 מבוא מורחב - שיעור 13
Example: explaining make-counter • Counter: an object that counts up starting from some integer (define make-counter (lambda (n) (lambda () (set! n (+ n 1))n ; returned and retained value ))) (define ca (make-counter 0))(ca) ==> 1(ca) ==> 2(define cb (make-counter 0))(cb) ==> 1(ca) ==> 3(cb) ==> 2 מבוא מורחב - שיעור 13
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 E1 is pointed at sodoes not disappear! n: 0 environment pointerpoints to E1because the lambdawas evaluated in E1 (lambda () (set! n (+ n 1)) n) | E1 מבוא מורחב - שיעור 13
E1 1 p: b:(set! n (+ n 1)) n (ca) | GE ==> 1 make-counter: GE ca: n: 0 p: nb:(lambda () (set! n (+ n 1)) n) E2 Empty (no args) (set! n (+ n 1)) | E2 n | E2 ==> 1 מבוא מורחב - שיעור 13
E1 1 2 p: b:(set! n (+ n 1)) n (ca) | GE ==> 2 make-counter: GE ca: n: 0 p: nb:(lambda () (set! n (+ n 1)) n) E3 empty (set! n (+ n 1)) | E3 n | E3 ==> 2 מבוא מורחב - שיעור 13
Example: explaining 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
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
Explaining Nested Definitions • 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)) מבוא מורחב - שיעור 13
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 ….) The same x in all subprocedures (sqrt 2) | GE guess: 1 sqrt-iter guess: 1 good-enou? מבוא מורחב - שיעור 13
message passing example (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) מבוא מורחב - שיעור 13
cons: GE E1 x: 1 y: 2 p: x yb:(define (dispatch op) ..) dispatch dispatch: p: op b:(cond ((eq? op 'car) x) .... ) (define a (cons 1 2)) | GE car: cdr: a: p: xb:(x ‘cdr) p: xb:(x ‘car) מבוא מורחב - שיעור 13
cons: GE p: x yb:(define (dispatch op) ..) dispatch E2 x: p: op b:(cond ((eq? op 'car) x) .... ) op: ‘car E3 (car a) | GE ==> 1 car: cdr: a: E1 x: 1 y: 2 p: xb:(x ‘cdr) p: xb:(x ‘car) dispatch: (x ‘car) | E2 מבוא מורחב - שיעור 13 (cond ..) | E3 ==> 1