6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Evaluation in the environment model Slide 12.1.16 Now we are ready to see how to connect environments to All evaluation occurs in an environment evaluation. First, as we have already suggested, all evaluation is The current environment changes when the going to take place with respect to some environment. The environment will provide the context for how to interpret symbols and names. Notice that this won't al ways be the same environment, and a key issue is that whenever we apply a procedure, we will create a new environment, which captures he context for interpreting the variables of that procedure 6 001 SICP Slide 12.1.17 Evaluation in the environment model We have said that all evaluation of expressions is going to take place with respect to an environment. And we have also seen All evaluation occurs in an environment that environments can inherit other environments as part of the interpreter applies a procedure chaining process. Nonetheless, this means that eventually we have to terminate that chaining of environments, and to do that The top environment is called the global environment(GE) we have a special environment, called the global environment Only the gE has no enclosing environment It has no enclosing environment and is the only such environment with that property. This is the environment in which we will normally evaluate expressions, as it is our starting point. It is also going to hold the bindings for our basic| 4 6001S expressions in our language Evaluation in the environment model Slide l2.1.18 So we are ready to start putting together rules for evaluating All evaluation occurs in an environment The current environment changes when the expressions with respect to an environment. Before we do, lets terpreter applies a procedure remind you that our central rule for evaluation deal with combinations, and in this new model things are nearly the same top environment is called the global environment(GE) as they were in the substitution model ly the GE has no enclosing environment As before, we will evaluate the subexpressions in any order To evaluate a combinatio Evaluate the subexpressions in the current environment now with respect to the current environment. Then we will ply the value of the first to the values of the rest apply the value of the first subexpression to the values of the others. We will see shortly how to do that in a very mechanis 6 001 SICP I way. With all of this in place, lets start actually building tfo stic environment model 6.001 Notes: Section 12.2
6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 12.1.16 Now we are ready to see how to connect environments to evaluation. First, as we have already suggested, all evaluation is going to take place with respect to some environment. The environment will provide the context for how to interpret symbols and names. Notice that this won't always be the same environment, and a key issue is that whenever we apply a procedure, we will create a new environment, which captures the context for interpreting the variables of that procedure. Slide 12.1.17 We have said that all evaluation of expressions is going to take place with respect to an environment. And we have also seen that environments can inherit other environments as part of the chaining process. Nonetheless, this means that eventually we have to terminate that chaining of environments, and to do that we have a special environment, called the global environment. It has no enclosing environment and is the only such environment with that property. This is the environment in which we will normally evaluate expressions, as it is our starting point. It is also going to hold the bindings for our basic expressions in our language. Slide 12.1.18 So we are ready to start putting together rules for evaluating expressions with respect to an environment. Before we do, lets remind you that our central rule for evaluation deal with combinations, and in this new model things are nearly the same as they were in the substitution model. As before, we will evaluate the subexpressions in any order, now with respect to the current environment. Then we will apply the value of the first subexpression to the values of the others. We will see shortly how to do that in a very mechanistic way. With all of this in place, lets start actually building the environment model. 6.001 Notes: Section 12.2
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 12.2.1 Okay, we are ready to start looking at our rules for evaluation inName-rule our new model, especially to how environments are created and manipulated as part of the evaluation process The simplest possible expressions are just numbers. These are self-evaluating which means that their value is just their value independent of the environment The first interesting rule comes with names. We have a way of dealing with evaluation of names, our simplest kind of primitive, and that rule should tell us how to get the value ated with a variabl 6001Sc Slide 12.2.2 A name X evaluated in environment E gi\ The rule is straightforward, but quite mechanistic. A name X the value of x in the first frame of e whe is bound evaluated in an environment e gives back the value of X in the first frame of e in which there is a binding for X. Remember, an environment is a sequence of frames, so we will start in the first frame of the lent, looking for a binding of that name. We will continue up the chain of frames until we find the first such binding, in which case we return the value associated with that binding Slide 12.2.3 Name-rule So here are our example environments from before. We will A name X evaluated in environment E gives call the top level environment, the global environment, or our the value of x in the first frame of e where x is bound stopgap environment Environment El has its own frame, and inherits as its enclosing environment, the global environment z:10 Slide 12. 2.4 A name x evaluated in environment E gives We can now talk about the value of a variable with respect to the value of x in the first frame of e where x is bound an environment; in fact we should always talk about the value I o=> 10 2In=>10 *Ie=> 15 of any expression with respect to some environment. For example, we can ask for the value of the variable z with respect to the global environment. Notice the notation we use for this namely the expression, then a i to indicate the evaluation with respect to some environment, then a subscript to denote the actual environment. Note that normally we will evaluate expressions with respect to the global environment, since that is where we interact with the Scheme system. If I type in a the expression to the Scheme interpreter, I am interacting with
6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 12.2.1 Okay, we are ready to start looking at our rules for evaluation in our new model, especially to how environments are created and manipulated as part of the evaluation process. The simplest possible expressions are just numbers. These are self-evaluating which means that their value is just their value, independent of the environment. The first interesting rule comes with names. We have a way of dealing with evaluation of names, our simplest kind of primitive, and that rule should tell us how to get the value associated with a variable. Slide 12.2.2 The rule is straightforward, but quite mechanistic. A name X evaluated in an environment E gives back the value of X in the first frame of E in which there is a binding for X. Remember, an environment is a sequence of frames, so we will start in the first frame of the environment, looking for a binding of that name. We will continue up the chain of frames until we find the first such binding, in which case we return the value associated with that binding. Slide 12.2.3 So here are our example environments from before. We will call the top level environment, the global environment, or our stopgap environment. Environment E1 has its own frame, and inherits as its enclosing environment, the global environment. Slide 12.2.4 We can now talk about the value of a variable with respect to an environment; in fact we should always talk about the value of any expression with respect to some environment. For example, we can ask for the value of the variable z with respect to the global environment. Notice the notation we use for this, namely the expression, then a | to indicate the evaluation with respect to some environment, then a subscript to denote the actual environment. Note that normally we will evaluate expressions with respect to the global environment, since that is where we interact with the Scheme system. If I type in an expression to the Scheme interpreter, I am interacting with the