Exercise 4.17. Draw diagrams of the environment in effect when evaluating the expression <e3> in the procedure in the text, comparing how this will be structured when definitions are interpreted sequentially with how it will be structured if definitions are scanned out as described. Why is there an extra frame in the transformed program? Explain why this difference in environment structure can never make a difference in the behavior of a correct program. Design a way to make the interpreter implement the ‘‘simultaneous’’ scope rule for internal definitions without constructing the extra frame.
对于原始的表达式
(lambda <vars>
(define u <e1>)
(define v <e2>)
<e3>)
我们可以如下画图 environment 的图:
+--------------------------------------+
| sth: --+ |
| | |
+--------+-----------------------------+
| ^
| |
| E1 |
| +------+-----+
v | <vars> |
Parameter: <vars> | u:<e1> |
body: (define..) | v:<e2> |
(define..) +------------+
<e3> <e3>
对于新的表达式
(lambda <vars>
(let ((u '*unassigned*)
(v '*unassigned*))
(set! u <e1>)
(set! v <e2>)
<e3>))
我们可以做出如下的 environment 图:
+--------------------------------------+
| sth: --+ |
| | |
+--------+-----------------------------+
| ^
| |
| E1 |
| +------+-----+
v |<vars> |<----------+
Parameter: <vars> +------------+ |
|
body: (let ((u '*unassigned*) | E2
(v '*unassigned*)) +------+-----+
... |u: *unass..*|
<e3>) |v: *unass..*|
+------------+
<e3>
对于一个“正确”的程序(一个 “不正确”的程序见 练习 4.19 ,这两个过程在运行的时候是没有任何区别的。原因在于,对于我们设计的寻找变量的机制而言,新多出来的 E2 与 E1 的合并正好就是原来的 environment 图的 E1。所以没有任何区别。
设计一个过程只产生一个 frame:TODO。