When we introduced compound procedures functions in chapter 1, we used the substitution model of evaluation (section 1.1.5) to define what is meant by applying a procedure function to arguments:
Once we admit
assignment
reassignment
into our programming language, such a
definition is no longer adequate. In particular,
section 3.1.3 argued that, in the
presence of
assignment,
reassignment,
a variable cannot be considered to be merely a name for
a value. Rather, a variable must somehow designate a
place
in which values can be stored.
In our new model of
evaluation, these places will be maintained in structures called
environments.
An environment is a sequence of frames. Each frame is a table (possibly empty) of bindings, which associate variable names with their corresponding values. bindings. Each binding is an entry in the frame for one name. A name is either assigned a value, or unassigned (section 3.2.3). In an environment diagram, a binding is written as its name followed by a colon. Nothing is written after the colon for an unassigned name. For a name assigned a primitive value such as a number, the value is written after the colon. For a name assigned a function or other compound value, an arrow leads from the binding to the value, which is drawn separately. (A single frame may contain at most one binding for any variable.) Each frame also has a pointer to its enclosing environment, unless, for the purposes of discussion, the frame is considered to be global. unless the frame is the built-in frame, reserved for Python's built-in functions and values. The value of a variable with respect to an environment is the value given by the binding of the variable in the first frame in the environment that contains a binding for that variable. If no frame in the sequence specifies a binding for the variable, then the variable is said to be unbound in the environment.
| Original | Python | |
|
Figure 3.1 A simple environment structure. |
Figure 3.2
A simple
environment structure.
|
Figure 3.1 Figure 3.2 shows a simple environment structure consisting of three frames, labeled I, II, and III. In the diagram, A, B, C, and D are pointers to environments. C and D point to the same environment. The variables z and x are bound in frame II, while y and x are bound in frame I. x have bindings in frame II, while y and x have bindings in frame I. The value of x in environment D is 3. The value of x with respect to environment B is also 3. This is determined as follows: We examine the first frame in the sequence (frame III) and do not find a binding for x, so we proceed to the enclosing environment D and find the binding in frame I. On the other hand, the value of x in environment A is 7, because the first frame in the sequence (frame II) contains a binding of x to 7. With respect to environment A, the binding of x to 7 in frame II is said to shadow the binding of x to 3 in frame I.
The environment is crucial to the evaluation process, because it determines the context in which an expression should be evaluated. Indeed, one could say that expressions in a programming language do not, in themselves, have any meaning. Rather, an expression acquires a meaning only with respect to some environment in which it is evaluated.
| Original | Python | |
| Even the interpretation of an expression as straightforward as (+ 1 1) depends on an understanding that one is operating in a context in which + is the symbol for addition. | Even the interpretation of an expression as straightforward as print(1) depends on an understanding that one is operating in a context in which the name print refers to the built-in function that prints a value. |