[racket] ok what's wrong there 'syntax-rule' evaluating its operand (??)

From: Alexis King (lexi.lambda at gmail.com)
Date: Thu Jan 15 03:40:30 EST 2015

This isn’t so much about interning or gensym (though those are useful when you do try to break hygiene). Every syntax object in Racket contains information about its lexical scope, so even identical symbols may not refer to the same bindings since they’re not in the same lexical scope! If my understanding is correct, this is how Racket maintains hygiene with its macros by default.

Now, define-syntax-rule always maintains hygiene. There is no mechanism to override this. This is probably because breaking hygiene is something to be done with caution, so the easy way to define simple macros give you no way to do it to prevent those newer to the language from ignoring the consequences.

However, note that define-syntax-rule is itself a macro that actually just expands to a define-syntax form. There’s nothing “magical” about define-syntax-rule, it’s just a handy shorthand.

Fortunately, using define-syntax with things like syntax-case and syntax-parse makes writing more complicated macros relatively painless. The syntax template system along with things like with-syntax make things fairly simple once you wrap your brain around how macros (or more properly, syntax transformers) actually work.

Greg Hendershott has written a wonderful guide on how macros actually work that starts with define-syntax rather than define-syntax-rule to prevent confusion that define-syntax-rule is somehow something special. This guide is called Fear of Macros <http://www.greghendershott.com/fear-of-macros/>  and I’d highly recommend reading it.

> On Jan 15, 2015, at 00:30, Thomas Lynch <thomas.lynch at reasoningtechnology.com> wrote:
> 
> thank you! ..  of course that makes sense.  I guess that gensym and uninterned symbols have something to do with this?
> 
> The identifiers in the macro are 'interned' (is that the terminology? rather than renamed?) so in fact table-auth was never defined.  Then at run time there are complaints as such.  Most all macro expanders work this way, though we might have some way to distinguish which identifiers get munged.
> 
> . so is there a simple way to communicate with  define-syntax-rule and tell it not to íntern' certain identifiers in its body? ... '(native table-author ...) or some such? ..  Of course there is always the define-syntax.
> 
> On Thu, Jan 15, 2015 at 3:28 PM, Daniel Prager <daniel.a.prager at gmail.com <mailto:daniel.a.prager at gmail.com>> wrote:
> Hi Thomas 
> 
> IIUC you get an error with your original syntax-rules version because syntax-rules (and syntax-case and syntax-parse) is hygienic by default. In this case think of table-author etc. being renamed inside the macro to avoid clashing with variables defined in the calling code. Hence the undefined error in the calling code. 
> 
> You *want* and expect it to be defined by the macro, but the default behavior says otherwise, and needs to be explicitly over-ridden.
> 
> 
> Dan
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150115/178f5b2d/attachment-0001.html>

Posted on the users mailing list.