SICP:3.2 The Environment Model of Evaluation
SICP:3.2 The Environment Model of Evaluation
本文逐段翻译 SICP 第二版 §3.2《The Environment Model of Evaluation》,覆盖 environments、frames、bindings、procedure application、closures、local state、internal definitions 与 Exercises 3.9–3.11;保留 Figure 3.1–3.11 的原始 GIF 和 Scheme code。
TL;DR
- Assignment 使 substitution model 不再足够;environment model 用 environments 中的 frames 保存 variable bindings。
- Environment 是 frame sequence;每个 frame 既保存 bindings,又指向 enclosing environment。
- Procedure 是 code 与 environment pointer 的 pair;调用 procedure 时新建 frame,把 parameters 绑定到 arguments,并在 procedure environment 下执行 body。
- Closure 保存创建时的 environment,因此能持有 local state;多个 objects 可以共享 code,却拥有彼此独立的 frames。
- Internal definitions 在调用 procedure 时写入当前 frame,local procedures 通过 enclosing environment 访问 outer parameters。
名词解释
| 名词 | 含义 | 本节对应物 |
|---|---|---|
| environment | frame sequence,用于解释 expression | global environment、E1、E2 |
| frame | 保存 variable bindings 的 table | x → 5、balance → 50 |
| binding | variable name 与 value 的 association | square → procedure |
| enclosing environment | 当前 frame 指向的上层 environment | E1 → global |
| procedure object | code 与创建环境 pointer 的 pair | square、W1 |
| closure | 捕获 environment 的 procedure | make-withdraw 返回值 |
| shadowing | inner frame 的 binding 覆盖 outer binding | E2 中的 x |
目录
| 章节 | 说明 |
|---|---|
| 3.2 The Environment Model of Evaluation | environment model 动机 |
| 3.2.1 The Rules for Evaluation | procedure / define / set! |
| 3.2.2 Applying Simple Procedures | (f 5) 的 frames |
| 3.2.3 Frames as the Repository of Local State | closure 与 state |
| 3.2.4 Internal Definitions | local procedure definitions |
| Exercises 3.9–3.11 | environment tracing |
3.2 The Environment Model of Evaluation
在 Chapter 1 引入 compound procedures 时,使用 substitution model of evaluation(§1.1.5)定义 procedure application:
- 把 compound procedure apply 到 arguments 时,用每个对应 argument 替换 formal parameter,然后 evaluate procedure body。
一旦 language 允许 assignment,这个 definition 就不再 adequate。§3.1.3 说明,在 assignment 存在时,variable 不能再被看作单纯的 value name;variable 必须以某种方式 designate 一个可以存放 values 的 “place”。在新的 evaluation model 中,这些 places 由 environments 维护。
Environment 是一系列 frames。每个 frame 是一个(可能为空的)bindings table,把 variable names 与对应 values 关联起来;同一个 frame 对一个 variable 至多有一个 binding。每个 frame 还有一个指向 enclosing environment 的 pointer,除非它被视为 global frame。
相对于某个 environment,variable 的 value 是:environment frame sequence 中第一个包含该 variable binding 的 frame 给出的 value。如果 sequence 中没有 frame 为 variable 指定 binding,则 variable 在该 environment 中是 unbound。

Figure 3.1: 一个 simple environment structure。
Figure 3.1 的 environment structure 由三个 frames 组成,标记为 I、II、III。图中的 A、B、C、D 是指向 environments 的 pointers;C 和 D 指向同一 environment。Variables z 和 x 绑定在 frame II,y 和 x 绑定在 frame I。
在 environment D 中,x 的 value 是 3;在 environment B 中,x 的 value 也是 3:先检查 sequence 的第一个 frame(III),没有找到 x binding,于是沿 enclosing pointer 到 environment D,并在 frame I 中找到 binding。
但 environment A 中 x 的 value 是 7,因为 sequence 的第一个 frame(II)包含 x → 7 binding。相对于 A,frame II 中 x → 7 的 binding shadows frame I 中 x → 3 的 binding。
Environment 对 evaluation 至关重要,因为它决定 expression 被 evaluate 的 context。Expression 本身并没有 meaning;expression 只有相对于某个 environment 被 evaluate 时才获得 meaning。即使是简单的 (+ 1 1),也依赖于 context 中 symbol + 绑定到 addition procedure。
因此,在 evaluation model 中总是说“相对于某个 environment evaluate expression”。与 interpreter 交互时,假设存在 global environment:它由一个没有 enclosing environment 的 frame 构成,包含 primitive procedures 对应 symbols 的 values。例如,symbol + 在 global environment 中绑定到 primitive addition procedure,这表达了 “+ 表示 addition” 的含义。
3.2.1 The Rules for Evaluation
Interpreter evaluate combination 的 overall specification 与 §1.1.3 相同:
- Evaluate combination 的 subexpressions。
- 把 operator subexpression 的 value apply 到 operand subexpressions 的 values。
Environment model 替代 substitution model,用于规定如何把 compound procedure apply 到 arguments。
在 environment model 中,procedure 总是一个 pair,由一些 code 和一个指向 environment 的 pointer 组成。Procedure 只有一种创建方式:evaluate lambda expression。
Evaluate lambda 会产生一个 procedure:它的 code 从 lambda expression 的 text 获得,environment 是 evaluate 该 lambda 以产生 procedure 时所在的 environment。
例如,在 global environment 中 evaluate:
(define (square x)
(* x x))
Procedure definition syntax 只是 implicit lambda expression 的 syntactic sugar;等价写法是:
(define square
(lambda (x) (* x x)))
它在 global environment 中 evaluate (lambda (x) (* x x)),并把 resulting value 绑定到 square。

Figure 3.2: 在 global environment 中 evaluate (define (square x) (* x x)) 产生的 environment structure。
Procedure object 是一个 pair:code 指定 procedure 有一个 formal parameter x,body 为 (* x x);environment part 指向 global environment,因为 lambda 在 global environment 中被 evaluate。新的 binding 把 procedure object 与 symbol square 关联,并加入 global frame。一般来说,define 通过向 frames 添加 bindings 创建 definitions。
Apply procedure 的规则是:创建一个新 environment,其中的 frame 把 parameters 绑定到 arguments;该 frame 的 enclosing environment 是 procedure object 指定的 environment。然后在这个 new environment 中 evaluate procedure body。
例如,在 global environment 中 evaluate (square 5):

Figure 3.3: 在 global environment 中 evaluate (square 5) 创建的 environment。
会创建 environment E1。E1 以一个 frame 开始,其中 procedure formal parameter x 绑定到 argument 5。该 frame 的 enclosing environment 是 global environment,因为这是 square procedure object 的 environment part。于是,在 E1 中 evaluate (* x x),结果为 (* 5 5),即 25。
Environment model 可以总结为两个 rules:
- 把 procedure object apply 到一组 arguments:构造 frame,将 procedure parameters 绑定到 call arguments,在新 environment 中 evaluate body;新 frame 的 enclosing environment 是被 apply 的 procedure object 的 environment part。
- 通过相对于给定 environment evaluate
lambdaexpression 创建 procedure;resulting procedure object 是lambdatext 与创建它的 environment pointer 组成的 pair。
此外,使用 define 定义 symbol,会在 current environment frame 中创建 binding,并把 symbol 赋予 indicated value。1 Evaluate (set! <variable> <value>) 时,在 environment 中寻找 variable binding,并将它修改为 new value;寻找 sequence 中第一个包含该 binding 的 frame。如果 variable 在 environment 中 unbound,set! 报错。
这些 rules 比 substitution model complex,但仍然 reasonably straightforward,并且准确描述 interpreter 如何 evaluate expressions。Chapter 4 将展示如何把这个 model 用作实现 working interpreter 的 blueprint。
3.2.2 Applying Simple Procedures
§1.1.5 使用 substitution model 分析过下面 procedures,并说明 (f 5) 的 result 为 136:
(define (square x)
(* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))
也可以使用 environment model 分析同一个 example。

Figure 3.4: 在 global frame 中定义 f、square 和 sum-of-squares 后创建的 procedure objects。
三个 procedure objects 都由 code 与 global environment pointer 组成。
Evaluate (f 5) 会创建 environment E1,其中 f 的 formal parameter a 绑定到 5。接着在 E1 中 evaluate f body:
(sum-of-squares (+ a 1) (* a 2))
先 evaluate subexpressions。sum-of-squares 的 value 是 procedure object:先在 E1 第一 frame 查找,没有找到,再到 enclosing global environment 找到它。Primitive + 和 * 对 a 执行 operations,得到 6 和 10。
然后把 sum-of-squares apply 到 6、10,创建 E2,其中 x 和 y 分别绑定 6、10。在 E2 中 evaluate (+ (square x) (square y))。
调用 (square x) 时,从 global frame 找到 square,x 的 value 是 6;创建 E3,将 square 的 parameter x 绑定 6,在 E3 中 evaluate (* x x)。调用 (square y) 同样创建 E4,将另一个 x 绑定 10。
每次调用 square 都创建一个带 x binding 的新 environment;不同 frames 使同名 local variables 保持 separate。每个 square frame 都指向 global environment,因为 square procedure object 的 environment part 是 global environment。

Figure 3.5: 使用 Figure 3.4 中 procedures evaluate (f 5) 创建的 environments。
两个 square calls 的 results 由 sum-of-squares 相加,再由 f 返回。
Exercises 3.9–3.11
Exercise 3.9. §1.2.1 使用 substitution model 分析 recursive 和 iterative factorial:
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
分别展示使用两种 factorial evaluate (factorial 6) 时创建的 environment structures。
3.2.3 Frames as the Repository of Local State
Environment model 可以说明如何使用 procedures 和 assignment 表示带 local state 的 objects。考虑 §3.1.1 的 “withdrawal processor”:
(define (make-withdraw balance)
(lambda (amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds")))
分析:
(define W1 (make-withdraw 100))
(W1 50)
50

Figure 3.6: 在 global environment 中定义 make-withdraw 的结果。
定义 make-withdraw 会产生一个 procedure object,包含指向 global environment 的 pointer;不同之处在于 procedure body 自身是一个 lambda expression。
调用 (make-withdraw 100) 时,创建 E1,把 formal parameter balance 绑定到 100。在 E1 中 evaluate body 的 lambda,构造一个新的 procedure object:它的 code 是 lambda 指定的 code,environment 是 E1。该 procedure object 作为 call result 返回,并由 global environment 中的 define 绑定为 W1。

Figure 3.7: evaluate (define W1 (make-withdraw 100)) 的结果。
调用 (W1 50) 时,为 W1 的 parameter amount 创建 frame,绑定 amount → 50。关键点是该 frame 的 enclosing environment 不是 global,而是 E1,因为 W1 procedure object 指向 E1。
在新 environment 中 evaluate:
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds")
amount 在 first frame 中找到;balance 则沿 enclosing pointer 到 E1 找到。

Figure 3.8: apply W1 procedure object 创建的 environments。
执行 set! 时,E1 中的 balance binding 被修改。W1 call 完成时,balance 是 50,包含 balance 的 frame 仍被 W1 procedure object 指向。包含 amount 的 frame 随 procedure call 结束而不再 relevant,因为其他 environment parts 已没有 pointer 指向它。
下一次调用 W1 会创建新的 amount frame,其 enclosing environment 仍是 E1。E1 就是持有 W1 local state variable 的 “place”。

Figure 3.9: W1 call 后的 environments。
再次调用 make-withdraw:
(define W2 (make-withdraw 100))
会创建 Figure 3.10 的 structure:W2 是一个 procedure object,由 code 与 environment 组成;W2 的 environment E2 是新的 call 创建的,包含自己的 balance binding。
W1 和 W2 具有相同 code,即 make-withdraw body 中 lambda 指定的 code,但它们的 environments 不同。W1 calls access E1 中的 balance,W2 calls access E2 中的 balance,因此一个 object 的 local-state changes 不影响另一个。

Figure 3.10: 使用 (define W2 (make-withdraw 100)) 创建 second object。
Exercise 3.10. make-withdraw 中的 local variable balance 是作为 make-withdraw parameter 创建的,也可以用 let 显式创建:
(define (make-withdraw initial-amount)
(let ((balance initial-amount))
(lambda (amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))))
let 是 procedure call 的 syntactic sugar:
(let ((<var> <exp>)) <body>)
等价于:
((lambda (<var>) <body>) <exp>)
使用 environment model 分析这个 alternate make-withdraw 对以下 sequence 的 interactions:
(define W1 (make-withdraw 100))
(W1 50)
(define W2 (make-withdraw 100))
说明两个 versions 是否产生相同 behavior,以及 environment structures 有何不同。
3.2.4 Internal Definitions
§1.1.8 介绍过 procedures 可以拥有 internal definitions,从而产生 block structure。下面是计算 square roots 的 procedure:
(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(average guess (/ x guess)))
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))
Environment model 可以说明这些 internal definitions 为什么按期望 behavior。

Figure 3.11: 带 internal definitions 的 sqrt procedure。
sqrt symbol 在 global environment 中绑定到一个 procedure object,该 object 的 associated environment 是 global environment。调用 (sqrt 2) 时创建 subordinate to global 的 E1,把 parameter x 绑定 2,并在 E1 中 evaluate body。
Body 的第一个 expression:
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
会在 E1 中定义 good-enough?:把 symbol 加入 E1 first frame,并绑定到 associated environment 为 E1 的 procedure object。类似地,improve 和 sqrt-iter 也在 E1 中定义。
随后在 E1 中 evaluate (sqrt-iter 1.0),创建 E2,把 sqrt-iter 的 parameter guess 绑定 1。sqrt-iter 调用 good-enough?,创建 E3,把另一个 guess 绑定 1。
虽然 sqrt-iter 和 good-enough? 都有 parameter guess,但它们是位于不同 frames 中的 distinct local variables。E2 和 E3 都以 E1 为 enclosing environment,因为两个 procedures 的 environment parts 都是 E1。因此,good-enough? body 中的 free variable x 会引用 E1 中的 x,即原始 sqrt call 的 argument。
Environment model 解释了 local procedure definitions 为什么适合 modularize programs:
- Local procedure names 绑定在 enclosing procedure 运行时创建的 frame 中,不会与 enclosing procedure 外部的 names 冲突。
- Local procedures 可以直接使用 parameter names 作为 free variables,访问 enclosing procedure 的 arguments;因为 local procedure body 在 subordinate to enclosing procedure evaluation environment 的 environment 中执行。
Exercise 3.11
考虑 §3.1.1 的 message-passing make-account:
(define (make-account balance)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
dispatch)
展示以下 interaction sequence 产生的 environment structure:
(define acc (make-account 50))
((acc 'deposit) 40)
90
((acc 'withdraw) 60)
30
acc 的 local state 保存在何处?如果再定义:
(define acc2 (make-account 100))
两个 accounts 的 local states 如何保持 distinct?两者 environment structure 的哪些 parts 是 shared 的?
图与格式复核
- 保留 Figure 3.1–3.11 的 11 个原始 GIF,按各 environment-tracing 段落的原文位置插入。
- 显示宽度按原始 GIF pixel width 保留:163、190、310、386、388、389、383、384、356、391、395。
- 保留 procedure objects、frames、enclosing pointers、closures、
set!与 internal definitions 的 Scheme code。
术语对照
| English | 中文处理 |
|---|---|
| environment / frame / binding | environment / frame / binding |
| enclosing environment | enclosing environment |
| procedure object | procedure object |
| closure | closure |
| shadowing | shadowing |
| internal definition | internal definition |
参考资料
-
本节 footnote 说明 interpreter 的 operand evaluation order 可以由 implementation 决定;本节 environment model 不依赖具体 order。 ↩
评论 (0)