我最近拥有第二版的“编程语言基础”。在第29页,该书介绍了以下针对Lambda演算的Scheme风格的语法:
<expression> ::= <identifier>
::= (lambda (<identifier>) <expression>)
::= (<expression> <expression>)
然后继续为自由变量和绑定变量引入定义(定义1.3.3,第31页),内容为:
A variable x occurs free in a lambda calculus expression E if and only if
1. E is a variable reference and E is the same as x; or
2. E is of the form (lambda (y) E'), where y is different from x and x occurs free in E'; or
3. E is of the form (E1 E2) and x occurs free in E1 or E2.
A variable x occurs bound in a lambda calculus expression E if and only if
1. E is of the form (lambda (y) E'), where x occurs bound in E' or x and y are the same variable and y occurs free in E'; or
2. E is of the form (E1 E2) and x occurs bound in E1 or E2.
这样的定义随后可以很容易地在两个Scheme过程中转换,分别是occurs-free?
和occurs-bound?
:
(define occurs-free?
(lambda (var exp)
(cond
((symbol? exp) (eqv? exp var))
((eqv? (car exp) 'lambda)
(and (not (eqv? (caadr exp) var))
(occurs-free? var (caddr exp))))
(else (or (occurs-free? var (car exp))
(occurs-free? var (cadr exp)))))))
(define occurs-bound?
(lambda (var exp)
(cond
((symbol? exp) #f)
((eqv? (car exp) 'lambda)
(or (occurs-bound? var (caddr exp))
(and (eqv? (caadr exp) var)
(occurs-free? var (caddr exp)))))
(else (or (occurs-bound? var (car exp))
(occurs-bound? var (cadr exp)))))))
当组合器(即仅由绑定变量组成的过程)作为输入输入时,人们期望occurs-bound?
返回#t
。但是,对于K组合器,测试失败:
> (occurs-bound? 'x '(lambda (c) (lambda (x) c)))
#f
这是因为在绑定变量的定义中,第1点指出要绑定的变量,必须既在lambda中进行量化,又在其体内自由显示。由于x
在内部lambda的主体中被忽略,因此测试返回false。
显然,occurs-bound?
没有在第三版中提供,因此无法与该书的最新版本进行任何比较。
以上定义是否有缺陷,或者我缺少什么? Here's a repl for the code。
谢谢
答案 0 :(得分:1)
为什么要有缺陷?您怀疑的起因是什么?
'x
是否绑定在'(lambda (c) (lambda (y) c))
中?
'x
是否绑定在'(lambda (c) (lambda (z) c))
中?
对于您来说,不是很奇怪吗?我希望不会。那么为什么要认为它出现在等效的 '(lambda (c) (lambda (x) c))
中呢?
不应该。甚至不在那里。