Lecture 13 Assignment and the environments model Chapter

  • Slides: 40
Download presentation
Lecture 13 - Assignment and the environments model Chapter 3 13 שיעור - מבוא

Lecture 13 - Assignment and the environments model Chapter 3 13 שיעור - מבוא מורחב 1

Primitive Data • constructor: (define x 10) creates a new binding for name; special

Primitive Data • constructor: (define x 10) creates a new binding for name; special form • selector: x • mutator: (set! x "foo") returns value bound to name changes the binding for name; special form מבוא מורחב 2

Assignment -- set! • Substitution model -- functional programming: (define x 10) (+ x

Assignment -- set! • Substitution model -- functional programming: (define x 10) (+ x 5) ==> 15 - expression has same value. . . each time it evaluated (in (+ x 5) ==> 15 same scope as binding) • With assignment: (define x 10) (+ x 5) ==> 15. . . (set! x 94). . . (+ x 5) ==> 99 - expression "value" depends on when it is evaluated מבוא מורחב 3

Why? To have objects with local state • The state is summarized in a

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. מבוא מורחב 4

Procedures with local state Suppose we want to implement counters (define ca (make-counter 0))

Procedures with local state Suppose we want to implement counters (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 How can we define such procedures? 13 שיעור - מבוא מורחב 5

A counter: an object with state Ø(define make-counter (lambda (n) (lambda ()(set! n (+

A counter: an object with state Ø(define make-counter (lambda (n) (lambda ()(set! n (+ n 1)) n ))) set! does assignment to an already defined variable. It is a special form. Ø(define ca (make-counter 0)) ca is a procedure. Variable n is its local state. 13 שיעור - מבוא מורחב 6

Can the substitution model explain this behavior? (define make-counter (lambda (n) (lambda () (set!

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 שיעור - מבוא מורחב 7

The Environments Model Substitution model: a single global environment Environment Table Name score Value

The Environments Model Substitution model: a single global environment Environment Table Name score Value 23 Environments model: many environments. Generalizes the substitution model. 13 שיעור - מבוא מורחב 8

Frame: a table of bindings • Binding: Example: A a pairing of a name

Frame: a table of bindings • Binding: Example: A a pairing of a name and a value x is bound to 15 in frame A y is bound to (1 2) in frame A the value of the variable x in frame A is 15 x: 15 y: 1 13 שיעור - מבוא מורחב 2 9

Environment: a sequence of frames • Environment E 1 consists of frames A and

Environment: a sequence of frames • Environment E 1 consists of frames A and B • Environment E 2 consists of frame B only • A frame may be shared by multiple environments B z: 10 E 2 A E 1 this arrow is called the enclosing environment pointer x: 15 y: 13 שיעור - מבוא מורחב 1 2 10

Evaluation in the environment model • All evaluations occur in an environment • The

Evaluation in the environment model • All evaluations occur in an environment • The current environment changes when the interpreter applies a procedure • The top environment is called the global environment (GE) • Only the GE has no enclosing environment 13 שיעור - מבוא מורחב 11

The Environment Model • A precise, completely mechanical, description of: • name-rule looking up

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 שיעור - מבוא מורחב 12

Name-rule • A name X evaluated in environment E gives the value of X

Name-rule • A name X evaluated in environment E gives the value of X in the first frame of E where X is bound • z | GE ==> 10 z | E 1 ==> 10 x | E 1 ==> 15 • In E 1, the binding of x in frame A shadows the binding of x in B B GE A E 1 • x | z: 10 x: 3 GE ==> 3 x: 15 y: 13 שיעור - מבוא מורחב 1 2 13

Define-rule • A define special form evaluated in environment E creates or replaces a

Define-rule • A define special form evaluated in environment E creates or replaces a binding in the first frame of E (define z 20) | B z: 10 x: 3 z: 20 A x: 15 y: z: 25 GE E 1 (define z 25) | GE 13 שיעור - מבוא מורחב 1 2 E 1 14

Set!-rule • A set! of variable X evaluated in environment E changes the binding

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) | E 1 B z: 10 20 25 x: 3 A x: 15 y: GE E 1 13 שיעור - מבוא מורחב 1 2 15

Your turn: evaluate the following in order (+ z 1) (set! z (define (set!

Your turn: evaluate the following in order (+ z 1) (set! z (define (set! y | E 1 (+ z 1)) | E 1 z (+ z 1)) | GE B GE A E 1 11 ==> (modify EM) E 1 Error: unbound variable: y z: 10 11 x: 3 x: 15 y: z: 12 13 שיעור - מבוא מורחב 1 2 16

Double bubble: how to draw a procedure (lambda (x) (* x x)) t prin

Double bubble: how to draw a procedure (lambda (x) (* x x)) t prin l l eva bda-ru lam #[proc-. . . ] e Environment pointer A compound proc that squares its argument Code pointer parameters: x body: (* x x) 13 שיעור - מבוא מורחב 17

Lambda-rule • A lambda special form evaluated in environment E creates a procedure whose

Lambda-rule • A lambda special form evaluated in environment E creates a procedure whose environment pointer points to E (define square (lambda (x) (* x x))) | E 1 B A E 1 z: 10 x: 3 x: 15 square: Evaluating a lambda actually returns a pointer to the procedure object parameters: x 13 שיעור - מבוא מורחב body: (* x x) environment pointer points to frame A because the lambda was evaluated in E 1 and E 1 A 18

To apply a compound procedure P to arguments (Application Rule) 1. Create a new

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 you memorize these four steps 13 שיעור - מבוא מורחב 19

(square 4) | GE x: 10 square: GE *: #[prim] parameters: x body: (*

(square 4) | GE x: 10 square: GE *: #[prim] parameters: x body: (* x x) A x: 4 E 1 square | (* x x) | * | E 1 GE E 1 ==> ==> 16 frame becomes inaccessible x | 13 שיעור - מבוא מורחב E 1 ==> 4 20

Example: inc-square GE inc-square: p: x b: (* x x) p: y b: (+

Example: inc-square GE inc-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))) | 13 שיעור - מבוא מורחב GE 21

Example cont'd: (inc-square 4) | GE GE inc-square: E 1 y: 4 p: x

Example cont'd: (inc-square 4) | GE GE inc-square: E 1 y: 4 p: x b: (* x x) p: y b: (+ 1 (square y)) inc-square | GE ==> (+ 1 (square y)) | E 1 + | E 1 ==> #[prim] 13 ( שיעור square y) | - מבוא מורחב E 1 22

Example cont'd: (inc-square 4) | GE E 1 inc-square: E 1 E 2 y:

Example cont'd: (inc-square 4) | GE E 1 inc-square: E 1 E 2 y: 4 p: x b: (* x x) p: y b: (+ 1 (square y)) | square | E 1 ==> (* x x) | E 2 ==> 16 * | E 2 ==> #[prim] x: 4 frames become inaccessible E 1 y | E 1 ==> 4 (+ 1 16) ==> 17 x | E 2 ==> 4 13 שיעור - מבוא מורחב 23

Example: explaining make-counter • Counter: an object that counts up starting from some integer

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 שיעור - מבוא מורחב 25

(define ca (make-counter 0)) | GE GE make-counter: ca: E 1 is pointed at

(define ca (make-counter 0)) | GE GE make-counter: ca: E 1 is pointed at so does not disappear! E 1 p: n b: (lambda () (set! n (+ n 1)) n) n: 0 environment pointer points to E 1 because the lambda was evaluated in E 1 p: b: (set! n (+ n 1)) n (lambda () (set! n 13 שיעור (+ - מורחב n 1)) n) | מבוא E 1 26

(ca) | GE GE ==> 1 make-counter: ca: E 1 p: n b: (lambda

(ca) | GE GE ==> 1 make-counter: ca: E 1 p: n b: (lambda () (set! n (+ n 1)) n) n: 0 1 E 2 Empty (no args) p: b: (set! n (+ n 1)) n (set! n (+ n 1)) | 13 E 2 שיעור - מורחב n |מבוא E 2 ==> 1 27

(ca) | GE GE ==> 2 make-counter: ca: E 1 p: n b: (lambda

(ca) | GE GE ==> 2 make-counter: ca: E 1 p: n b: (lambda () (set! n (+ n 1)) n) n: 0 1 2 E 3 empty p: b: (set! n (+ n 1)) n (set! n (+ n 1)) | 13 E 3 שיעור - מורחב n |מבוא E 3 ==> 2 28

Example: explaining make-counter • Counter: something which counts up from a number (define make-counter

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 29

(define ca (make-counter 0)) | GE GE make-counter: ca: E 1 p: n b:

(define ca (make-counter 0)) | GE GE make-counter: ca: E 1 p: n b: (lambda () (set! n (+ n 1)) n) n: 0 environment pointer points to E 1 because the lambda was evaluated in E 1 p: b: (set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) | E 1 30

(ca) | GE GE ==> 1 make-counter: ca: E 1 p: n b: (lambda

(ca) | GE GE ==> 1 make-counter: ca: E 1 p: n b: (lambda () (set! n (+ n 1)) n) n: 0 1 E 2 empty p: b: (set! n (+ n 1)) n (set! n (+ n 1)) | E 2 n | E 2 ==> 1 31

(ca) | GE GE ==> 2 make-counter: ca: E 1 p: n b: (lambda

(ca) | GE GE ==> 2 make-counter: ca: E 1 p: n b: (lambda () (set! n (+ n 1)) n) n: 0 1 2 E 3 empty p: b: (set! n (+ n 1)) n (set! n (+ n 1)) | E 3 n | E 3 ==> 2 32

(define cb (make-counter 0)) | GE make-counter: ca: cb: E 1 p: n b:

(define cb (make-counter 0)) | GE make-counter: ca: cb: E 1 p: n b: (lambda () (set! n (+ n 1)) n) GE E 4 n: 2 n: 0 E 3 p: b: (set! n (+ n 1)) n (lambda () (set! n (+ n 1)) n) | p: b: (set! n (+ n 1)) n E 4 33

(cb) | GE GE ==> 1 make-counter: ca: cb: E 1 p: n b:

(cb) | GE GE ==> 1 make-counter: ca: cb: E 1 p: n b: (lambda () (set! n (+ n 1)) n) E 4 n: 2 n: 0 1 E 2 p: b: (set! n (+ n 1)) n E 5 p: b: (set! n (+ n 1)) n 34

Capturing state in local frames & procedures GE make-counter: ca: cb: E 1 p:

Capturing state in local frames & procedures GE make-counter: ca: cb: E 1 p: n b: (lambda () (set! n (+ n 1)) n) E 4 n: 2 n: 1 E 2 p: b: (set! n (+ n 1)) n 35

Lessons from the make-counter example • Environment diagrams get complicated very quickly • Rules

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 36

Explaining Nested Definitions • Nested definitions : block structure (define (sqrt x) (define (good-enough?

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 שיעור - מבוא מורחב 37

(sqrt 2) | GE GE sqrt: p: x b: (define E 1 x: 2

(sqrt 2) | GE GE sqrt: p: x b: (define E 1 x: 2 good-enough: improve: p: guess. . ) sqrt-iter: b: (< (abs …. ) good-enou (define improve. . ) (define sqrt-iter. . ) (sqrt-iter 1. 0) guess: 1 The same x in all subprocedures guess: 1 13 good-enou? שיעור - מבוא מורחב sqrt-iter 38

message passing example (define (cons x y) (define (dispatch op) (cond ((eq? op 'car)

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 שיעור - מבוא מורחב 39

(define a (cons 1 2)) | GE cons: p: x y b: (define (dispatch

(define a (cons 1 2)) | GE cons: p: x y b: (define (dispatch op). . ) dispatch GE car: a: E 1 x: 1 y: 2 dispatch: p: x b: (x ‘car) cdr: p: x b: (x ‘cdr) p: op b: (cond ((eq? op 'car) x). . ) 13 שיעור - מבוא מורחב 40

(car a) | GE ==> 1 GE cons: p: x y b: (define (dispatch

(car a) | GE ==> 1 GE cons: p: x y b: (define (dispatch op). . ) dispatch car: a: E 1 x: 1 y: 2 dispatch: E 2 p: op b: (cond ((eq? op 'car) x). . ) (x ‘car) | E 2 (cond. . ) | E 3 ==> 1 p: x b: (x ‘car) E 3 13 שיעור - מבוא מורחב cdr: p: x b: (x ‘cdr) x: op: ‘car 41