Presentation is loading. Please wait.

Presentation is loading. Please wait.

6.001 SICP 1 6.001 SICP – October 29 6001-environment Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:

Similar presentations


Presentation on theme: "6.001 SICP 1 6.001 SICP – October 29 6001-environment Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:"— Presentation transcript:

1

2 6.001 SICP 1 6.001 SICP – October 29 6001-environment Trevor Darrell trevor@csail.mit.edu 32-D512 Office Hour: W 11 6.001 web page: http://sicp.csail.mit.edu/ section web page: http://www.csail.mit.edu/~trevor/6001/ Environment Model Diagram Components Rules Let example More examples

3 6.001 SICP 2 Why do we need a new evaluation model? The substitution model broke down when we added side effects! In Object Oriented Programming, we need a model to represent hierarchies, shadowing, and inheritance. Looking Ahead: We'll be writing a meta-circular evaluator in Scheme. You'll see that the code we write closely relates to the rules of the environment model. It's boring, but important: It's a technical investment now, but you'll get a lot out of it soon.

4 6.001 SICP 3 Diagram Components Frames: We draw a frame as a box. In the box go bindings. Every frame (except the frame representing the global environment) needs to have a link to exactly one other frame. Bindings: A binding is an association between an identifier (or symbol) to a value. Procedure Objects: These are special objects (symbolized by the double bubble) created by evaluating a lambda expression as described below.

5 6.001 SICP 4 Frame: a table of bindings Binding:a pairing of a name and a value Example: 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 2 1 x: 15 A y:

6 6.001 SICP 5 Environment: a sequence of frames Environment E1 consists of frames A and B E2 x: 10 B E1 x: 15 A 2 1 y: this arrow is called the enclosing environment pointer Environment E2 consists of frame B only A frame may be shared by multiple environments

7 6.001 SICP 6 Rules Looking up a name Define / Set Lambda Combination / Procedure application

8 6.001 SICP 7 Looking up an identifier 1.Look for a value in the current frame. 2.If there is no value for that identifier in the current frame, follow the link from the current frame to the one that it is linked from. 3.Continue until we find a binding for the identifier we're looking up or until we run out of frames in our chain of links. If there's no binding in the GE, the identifier we're looking up is an unbound variable. x: 10 B E1 x: 15 A 2 1 y:

9 6.001 SICP 8 set and define Define (define var expression) 1.Evaluates the expression with respect to the current environment. 2.Adds a binding to the frame in which it is evaluated.

10 6.001 SICP 9 set and define Set! (set! var expression) 1.Evaluates the expression with respect to the current environment. 2.Lookup the identifier in the current environment 3.Rebind the identifier in the frame it was found to the value of the expression.

11 6.001 SICP 10 Lambda Evaluating a lambda expression will result in a two-part procedure object (two circles next to each other -- the double bubble). The pointer of the left circle points down to a list of the parameters and the body of the procedure. The pointer of the right circle points up to the environment frame in which the lambda expression was evaluated. Note that nothing else happens until we apply the procedure.

12 6.001 SICP 11 Procedure object: “double bubble” (lambda (x) (* x x)) Environment pointer Code pointer parameters: x body: (* x x) Points to environment in which lambda was evaluated!

13 6.001 SICP 12 1.Draw a new environment frame. 2.Link the new environment frame to the environment frame pointed to by the right circle of the procedure object. 3.Bind the parameters to their values in this new environment frame. 4.Evaluate the body of the procedure in this new environment frame. Combinations / Procedure application To evaluate a combination with respect to an environment, first evaluate the subexpressions with respect to the environment and then apply the value of the operator subexpression to the values of the operand ubexpressions. Applying a procedure P

14 6.001 SICP 13 Example1 Evaluate: (define a 5) (define foo (lambda (x) (+ a x))) (foo 4)

15 6.001 SICP 14 Example1 Here's what happens: 1.We bind a to 5 2.A new frame is created because of the application. It is shown with a dashed link to the procedure whose application creates the new frame. 3.In the new frame we bind the procedure's formal parameter x to the actual argument 4. 4.The new frame has as its parent the environment pointer from the procedure. Together then, the new frame and the enclosing environment define a new environment (E1). 5.Now we evaluate the body of the procedure with respect to the new environment (E1). 6.To evaluate the (+ a x) expression, we look up x and find it in the local frame to get a value 4. We look up a and find that it is NOT in the local frame. So we follow the frame parent link up to the surrounding frame (the GE), where we successfully find a binding for a whose value is 5. The addition finally returns 9.

16 6.001 SICP 15 (define a 5) | GE (define foo (lambda (x) (+ a x))) | GE (foo 4) | GE GE a:5a:5 Example1

17 6.001 SICP 16 (define a 5) (define foo (lambda (x) (+ a x))) (foo 4) GE p: x b:(+ a x) foo: E1 x: 4 a:5a:5 Procedure application: “drop a frame” 1.Draw frame 2.Set enclosing env. ptr of frame to be env. ptr of proc. 3.Assign args to vals in new frame 4.Eval body w.r.t. new frame (+ a x) (+ 5 4) ([proc] 5 4) 9 +: [proc]

18 6.001 SICP 17 Example2 Evaluate: (define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))) (define n 10) (fact 3) n

19 6.001 SICP 18 GE p: n b:(if (= 0 n) 1 (* (fact (- n 1)))) fact: n: 10 n: 1 Example2 n: 2n: 3

20 6.001 SICP 19 let Let just creates another frame linked from the current frame. The bindings in that frame are let-variables bound to the result of evaluating the let- expressions with respect to the original environment. (let ((n v)…) body) ==> ((  (n …) body) v … ) (let ((x (+ 2 5)) (y 7)) (* x y)) ((lambda (x y) (* x y)) (+ 2 5) 7)

21 6.001 SICP 20 Example3 (define (adder y) (let ((inc y)) (lambda (x) (+ inc x)))) (define add3 (adder 3)) (add3 4) (define (adder y) (lambda (inc) (lambda (x) (+ inc x)) y)) ((adder 3) 4) (define (adder y) (lambda (x) (+ y x))) (define add3 (adder 3)) (add3 4)

22 6.001 SICP 21 (define (adder y) (lambda (x) (+ y x))) | GE (define add3 (adder 3)) | GE (add3 4) | GE GE p: x b:(lambda (x) (+ y x)) adder: E1 x: 3 p: y b:(+ y x) add3: E2 Y: 4

23 6.001 SICP 22 Example4 Let’s use this technique to build a one-element buffer…write a function that returns the argument in it’s previous call… (define last-value '*first-call*) (define (prev new) (define temp last-value) (set! Last-value new) temp)

24 6.001 SICP 23 Example4 Now, consider the following let statement. The effect of this statement in the environment is then shown. (let ((last-value '*first-call* )) (define (prev new)... ))

25 6.001 SICP 24 Example4 Define instead a procedure make-prev that can be used to create the procedure prev and add it to the environment (to make completely clear the binding action of procedures) (define (make-prev last-value) (lambda (new) (define temp last-value) (set! last-value new) temp))

26 6.001 SICP 25 Example4 (define prev (make-prev '*first-call*)) (prev 'a) :

27 6.001 SICP 26 Block structure and Lexical Scoping … (define F (lambda (a b c) (define G (lambda (b a) (- a b))) (define H (lambda (a x) (* a b))) (+ (G a b) (H b a)))) (F 2 3 4) … GE Scope 1 Scope 2 Scope 3

28 6.001 SICP 27 Example5 - Corresponding Env. Diagram

29 6.001 SICP 28 What created this? (define (foo x)...) w.r.t. GE; (foo 1) w.r.t. ANY ENVIRONMENT (define (foo) (define x 1)...)

30 6.001 SICP 29 or this? (define bar (let ((x 1)) (lambda (...)...)))

31 6.001 SICP 30 this? (define (make-bar x) (lambda (…) ….) (define bar1 (make-bar 1))

32 6.001 SICP 31 Example6 Evaluate: (define make-count-proc-1 (lambda (f) (lambda (x) (let ((count 0)) (cond ((eq? x ‘count) count) (else (set! Cont (+ count 1)) (f x)))))) (define sqrt-c-1 (makecount-proc-1 sqrt)) (sqrt-c-1 4)==> ? (sqrt-c-1 ‘count)==> ?


Download ppt "6.001 SICP 1 6.001 SICP – October 29 6001-environment Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:"

Similar presentations


Ads by Google