310 likes | 338 Views
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.
E N D
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 6.001 SICP
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. 6.001 SICP
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. 6.001 SICP
A x: 15 y: 1 2 Frame: a table of bindings • Binding: a pairing of a name and a value 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 6.001 SICP
B x: 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 6.001 SICP
Rules • Looking up a name • Define / Set • Lambda • Combination / Procedure application 6.001 SICP
B x: 10 A x: 15 E1 y: 1 2 Looking up an identifier • Look for a value in the current frame. • 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. • 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. 6.001 SICP
set and define Define (define var expression) • Evaluates the expression with respect to the current environment. • Adds a binding to the frame in which it is evaluated. 6.001 SICP
set and define Set!(set! var expression) • Evaluates the expression with respect to the current environment. • Lookup the identifier in the current environment • Rebind the identifier in the frame it was found to the value of the expression. 6.001 SICP
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. 6.001 SICP
Environmentpointer Code pointer parameters: xbody: (* x x) Procedure object: “double bubble” (lambda (x) (* x x)) Points to environment in which lambda was evaluated! 6.001 SICP
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 • Draw a new environment frame. • Link the new environment frame to the environment frame pointed to by the right circle of the procedure object. • Bind the parameters to their values in this new environment frame. • Evaluate the body of the procedure in this new environment frame. 6.001 SICP
Example1 Evaluate: (define a 5) (define foo (lambda (x) (+ a x))) (foo 4) 6.001 SICP
Example1 Here's what happens: • We bind a to 5 • 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. • In the new frame we bind the procedure's formal parameter x to the actual argument 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). • Now we evaluate the body of the procedure with respect to the new environment (E1). • 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. 6.001 SICP
(define a 5) | GE(define foo (lambda (x) (+ a x)))| GE(foo 4)| GE Example1 a:5 GE 6.001 SICP
E1 (+ a x) (define a 5) (define foo (lambda (x) (+ a x)))(foo 4) Procedure application: “drop a frame” a:5 +: [proc] GE foo: • Draw frame • Set enclosing env. ptr of frame to be env. ptr of proc. • Assign args to vals in new frame • Eval body w.r.t. new frame x: 4 p: xb:(+ a x) (+ 5 4) ([proc] 5 4) 9 6.001 SICP
Example2 Evaluate: (define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))) (define n 10) (fact 3) n 6.001 SICP
Example2 fact: GE n: 10 n: 1 n: 2 n: 3 p: nb:(if (= 0 n)1 (* (fact (- n 1)))) 6.001 SICP
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 ((nv)…) body) ==> ((l (n …) body) v … ) (let ((x (+ 2 5)) (y 7)) (* x y)) ((lambda (x y) (* x y)) (+ 2 5) 7) 6.001 SICP
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) 6.001 SICP
(define (adder y) (lambda (x) (+ y x)))| GE (define add3 (adder 3))| GE (add3 4)| GE add3: adder: GE E1 x: 3 p: xb:(lambda (x) (+ y x)) E2 p: yb:(+ y x) Y: 4 6.001 SICP
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) 6.001 SICP
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) ... )) 6.001 SICP
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)) 6.001 SICP
Example4 (define prev (make-prev '*first-call*)) (prev 'a): 6.001 SICP
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 6.001 SICP
Example5 - Corresponding Env. Diagram 6.001 SICP
What created this? • (define (foo x) ...) w.r.t. GE; (foo 1) w.r.t. ANY ENVIRONMENT • (define (foo) (define x 1) ...) 6.001 SICP
or this? (define bar (let ((x 1)) (lambda (...) ...))) 6.001 SICP
this? (define (make-bar x) (lambda (…) ….) (define bar1 (make-bar 1)) 6.001 SICP
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) ==> ? 6.001 SICP