MASSACHVSETTS INSTITVTE OF TECHNOLOGY
Department of Electrical Engineering and Computer Science
6.001---Structure and Interpretation of Computer Programs
Spring Semester, 1999
Recitation -- Friday, March 19
Why are we doing this?
What are environment diagrams made of?
What are the rules again?
To evaluate a comibination 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 subexpressions.
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 in this manner until we find a binding for the identifier we're looking up or until we run out of frames in our chain of links (i.e. we come to the global environment). If there's no binding in the global environment, the identifier we're looking up is an unbound variable.
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.
Define adds a binding to the frame in which it is evaluated.
Draw a new environment frame. Bind the parameters to their values in this new environment frame.
Link the new environment frame to the environment frame pointed to by the right circle of the procedure object.
Evaluate the body of the procedure in this new environment frame.
Evaluate the expression with respect to the current environment.
Lookup the identifier in the current environment (see step #2)
Rebind the identifier in the frame it was found to the value of the expression.
Just de-sugar! (See examples, below)
Evaluate the expressions below, following the rules of the environment model. Draw the cooresponding environment diagram in the box.
(define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))
(define n 6) (fact 3)
We haven't defined the rule for let. Well, let's just desugar it:
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.
Consider the example below using higher order procedures. Use the environment model to evaluate the following expressions.
(define make-counter (lambda () (let ((count 0)) (lambda () (set! count (+ 1 count)) count))))
(define days (make-counter)) (define dollars (make-counter))
(days)