灯下 登录
计算机科学 / SICP / section 中英对照

3.2 The Environment Model of Evaluation

When we introduced compound procedures in Chapter 1, we used the

substitution model of evaluation (1.1.5) to define what is meant

by applying a procedure to arguments:

在第 1 章引入复合过程时,我们使用代换模型 (1.1.5) 来定义将过程应用于实参的含义:

To apply a compound procedure to arguments, evaluate the body of the procedure

with each formal parameter replaced by the corresponding argument.

将复合过程应用于实参,就是在过程体中用相应实参替换每个形参后对过程体求值。

Once we admit assignment into our programming language, such a definition is no

longer adequate. In particular, 3.1.3 argued that, in the

presence of assignment, a variable can no longer 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.

一旦我们在程序设计语言中引入赋值,上述定义便不再充分。具体而言,3.1.3 节已论证:在存在赋值的情况下,变量不能再被简单地看作某个值的名称,而必须以某种方式指代一个可存储值的"位置"。在我们新的求值模型中,这些位置将被维护在称为环境 (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. (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. 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.

环境是一个框架 (frames) 的序列。每个框架是一张(可能为空的)绑定 (bindings) 表,将变量名与相应的值关联起来。(一个框架中对任何变量至多只有一个绑定。)每个框架还有一个指向其外围环境 (enclosing environment) 的指针,除非出于讨论目的,该框架被视为全局的。变量相对于某个环境的值,是在该环境的框架序列中第一个包含该变量绑定的框架所给出的值。如果序列中没有任何框架为该变量指定绑定,则称该变量在此环境中是未绑定的 (unbound)。

Figure 3.1 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. 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.

Figure 3.1: A simple environment structure.

图 3.1 展示了一个简单的环境结构,由三个框架构成,分别标记为 I、II 和 III。图中,A、B、C、D 是指向环境的指针,C 和 D 指向同一个环境。变量 z 和 x 绑定在框架 II 中,而 y 和 x 绑定在框架 I 中。x 在环境 D 中的值为 3。x 相对于环境 B 的值也是 3,确定过程如下:我们检查序列中的第一个框架(框架 III),未找到 x 的绑定,便前进到外围环境 D,在框架 I 中找到了该绑定。另一方面,x 在环境 A 中的值为 7,因为序列中的第一个框架(框架 II)包含了 x 到 7 的绑定。相对于环境 A,框架 II 中 x 到 7 的绑定遮蔽 (shadow) 了框架 I 中 x 到 3 的绑定。图 3.1:一个简单的环境结构。

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. 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. Thus, in

our model of evaluation we will always speak of evaluating an expression with

respect to some environment. To describe interactions with the interpreter, we

will suppose that there is a global environment, consisting of a single frame

(with no enclosing environment) that includes values for the symbols associated

with the primitive procedures. For example, the idea that + is the

symbol for addition is captured by saying that the symbol + is bound in

the global environment to the primitive addition procedure.

环境对于求值过程至关重要,因为它决定了表达式应在何种上下文中被求值。可以说,程序设计语言中的表达式本身并不具有任何含义;一个表达式只有在某个环境中求值时,才获得相应的含义。即便是像 (+ 1 1) 这样直白的表达式,其解读也依赖于一种理解:人们处于一个以 + 为加法符号的上下文中。因此,在我们的求值模型中,我们将始终谈论在某个环境中对表达式求值。为了描述与解释器的交互,我们假设存在一个全局环境,它由一个单一框架(没有外围环境)构成,该框架中包含了与基本过程相关联的符号的值。例如,"+ 是加法符号"这一概念,可以表述为:符号 + 在全局环境中被绑定到基本加法过程。