2009-07-09

Guile Scheme Macro Bug?

syntax-rules是R5RS Scheme中规定的标准hygienic macro system,不过Guile Scheme中的实现似乎有点问题。
(define-syntax dotimes
(syntax-rules ()
((_ n body ...)
(let loop ((counter n))
(if (> counter 0)
(begin
body ...
(loop (- counter 1))))))))

上面定义了一个宏dotimes。理论上说,作为hyginic macro,外部环境对于宏里面的内部符号应该没有任何影响:

> (let ((loop 2)) (dotimes 4 (display loop)))
2222
> (let ((n 2)) (dotimes 4 (display n)))
2222
> (let ((counter 2)) (dotimes 4 (display counter)))
2222
> (let ((- 'minus)) (dotimes 4 (display -)))
minusminusminusminus

MIT-scheme, Gambit以及PLT中都是上面的行为,唯独Guile过不了最后一关:

guile> (let ((- 'minus)) (dotimes 4 (display -)))
minus<unnamed port>: In expression (- syntmp-counter-21 1):
<unnamed port>: Wrong type to apply: minus

明显的是,宏内部变量名都已被转换,比如counter变成了syntmp-counter-21,但'-'还是引用了外部let环境下的'-'。这该是个bug。

标签:

0 Comments:

发表评论

<< Home