[plt-scheme] Hygienic Macros

From: Paulo J. Matos (pocmatos at gmail.com)
Date: Thu Feb 12 17:15:22 EST 2009

On Thu, Feb 12, 2009 at 1:57 PM, Matthias Felleisen
<matthias at ccs.neu.edu> wrote:
>
> The Hitchhiker's Guide to the Metauniverse used to advertise hygiene with a
> range of examples, starting from if/orelse and going to loop macros,
> especially for, which are notoriously to get right.
>
> Story: After I had written the Hygiene paper Dan and I went to the MIT lab
> late one night and encountered an old Lisper (name withheld) hacking on a
> Lisp machine. My first. We asked him to write one of those macros. He wrote
> the worst possible version. So we wrote a client that messed up. He was
> actually stumped for a moment and didn't know why the thing didn't behave as
> expected. Then we went through the defensive spiel of all people who make
> mistakes in programming.
>
> Lesson is: if you want to reason about the surface syntax of macro uses and
> if you want to have lots of programmers leverage the power of syntactic
> abstraction, you want hygiene, i.e., enforced lexical scoping. (Only
> superstars on their highest alert level get things right all the time.)
>
> -- Matthias
>
>

Thanks Matthias for the comment and Will and Eli for the examples. It
will be very useful! :)

>
>
>
> On Feb 12, 2009, at 8:46 AM, Will Farr wrote:
>
>> Paulo,
>>
>> Here's another one (probably Eli's examples are better, but I've been
>> bitten by this one in practice):
>>
>> Suppose you define a macro do-range:
>>
>> (define-syntax do-range
>>  (syntax-rules ()
>>    ((do-range (i llow hhigh) body ...)
>>     (let ((low llow)
>>           (high hhigh))
>>       (let loop ((i low))
>>         (if (< i high)
>>             (begin body ...
>>                    (loop (add1 i)))
>>             'done))))))
>>
>> And then try to use it in
>>
>> (define (vector-n-smallest vector n <)
>>   (let* ((store (vector-copy vector 0 (+ n 1))))
>>      (insert-sort! store <)
>>      (do-range (i (+ n 1) (vector-length vector))
>>         (vector-set! store n (vector-ref vector i))
>>         (insert-sort! store <))
>>      (vector-copy store 0 n)))
>>
>> Without hygiene, the '< in the do-range macro will refer to the '<
>> that is passed into vector-n-smallest.  There's no way to avoid this
>> without introducing hygiene (i.e. it's not a bug that can be fixed by
>> simply using (gensym)).  And, it bit me in real life :).
>>
>> Will
>>
>> On Thu, Feb 12, 2009 at 7:07 AM, Paulo J. Matos <pocmatos at gmail.com>
>> wrote:
>>>
>>> Hi all,
>>>
>>> What would be a good simple example of an hygienic macro that would
>>> not work as expected if the macro were not hygienic?
>>> I would like an example that would be simple to understand to
>>> programming language students that are non-schemers. All the ones I
>>> have found from papers don't seem 'simple' enough.
>>>
>>> Any suggestions?
>>>
>>> --
>>> Paulo Jorge Matos - pocmatos at gmail.com
>>> Webpage: http://www.personal.soton.ac.uk/pocm
>>> _________________________________________________
>>>  For list-related administrative tasks:
>>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>>
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>



-- 
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm


Posted on the users mailing list.