John Cowan skrev:
> R. Kent Dybvig scripsit:
>
>> Actually, the point is that they permit one to deal sensibly with lexical
>> scoping at the source level. This not only simplifies the coding of many
>> macros but also allows the definition of others that cannot be written
>> with defmacro. For example, one can use syntax-rules and syntax-case to
>> write macros that perform arbitrary code motion (e.g., define-integrable)
>> without breaking lexical scope.
>
> I'm no expert on the subject and you are, but I don't see how
> there can be anything that define-macro cannot do, since it applies
> a Turing-complete language to arbitrarily large parts of the
> program.
Lexical scope is one of the key concepts in Scheme. Therefore it
is important to have a macro system, that makes it easy to write
macros that respect lexical scope.
Below is a small example, that illustrates what "macros
respect lexical scope means".
In the the expansion of foo below, the occurrence of a is bound to
the a in the scope of the macro definition.
(define a 0)
(define-syntax foo
(syntax-rules ()
((_)
(+ a 100))))
(display (let ((a 42)) (foo)))
(newline)
(set! a 10)
(display (let ((a 42)) (foo)))
(newline)
The program displays
100
110
How would you define foo in a defmacro system?
My attempt went horribly wrong:
(require (lib "defmacro.ss"))
(define a 0)
(define-macro (foo)
'(+ a 100))
(display (let ((a 42)) (foo)))
(newline)
(set! a 10)
(display (let ((a 42)) (foo)))
(newline)
It displays:
142
142
> I'm no expert on the subject and you are, but I don't see how
> there can be anything that define-macro cannot do, since it applies
> a Turing-complete language to arbitrarily large parts of the
> program.
The "arbitrarily large parts" is cheating. Standard macros ought
to be understood without whole-program knowledge.
--
Jens Axel S?gaard
Received on Tue May 29 2007 - 05:02:35 UTC