Re: [racket-users] Basic macro question

2017-02-05 Thread Jon Zeppieri
On Sun, Feb 5, 2017 at 10:12 PM,   wrote:
> Ahh... datum->syntax, I thought I had seen something like this before. It is 
> treating "a", or a's form, as the scope for the new ids essentially, but I 
> can pick standard names. This just presupposes I only care to use my "a" 
> macro once in any given scope. Does this really make it an unhygenic solution?

If you're planning to use the identifiers outside the macro, then yes.
The idea is that at an identifier's use site, you should only see
bindings that are lexically apparent. So, for example, if you had:

(define x 20)
(a)
x

You should expect `x` on the third line to evaluate to 20, because
that's the lexically apparent definition of x. But non-hygienic macros
can break that rule.

- J

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Basic macro question

2017-02-05 Thread keccak384
On Sunday, February 5, 2017 at 10:04:47 PM UTC-5, Philip McGrath wrote:
> You need to have the lexical context information of x, y, and z come from 
> stx: otherwise, they will be protected by macro expansion (as a matter of 
> hygiene).
> 
> 
> Here's one way to do it:
> (define-syntax (a stx)
>   (syntax-parse stx
>     [(a)
>      (with-syntax ([x (datum->syntax stx 'x)]
>                           [y (datum->syntax stx 'y)]
>                           [z (datum->syntax stx 'z)])
>        #`(begin
>            (define x 97)
>            (define y 98)
>            (define z 99)))]))
> 
> 
> You may also want to look at format-id.
> 
> 
> Also, I found Greg Hendershott's Fear of Macros 
> (http://www.greghendershott.com/fear-of-macros/index.html) a useful 
> supplement to the Racket Guide/Reference in understanding the macro system: 
> ch. 4 addresses some of these issues.
> 
> 
> 
> -Philip
> 
> 
> 
> On Sun, Feb 5, 2017 at 8:41 PM,   wrote:
> I must be missing something simple here.
> 
> 
> 
> 229> (define-syntax a (lambda (stx) (syntax-parse stx [(a) #`(begin (define x 
> 97) (define y 98) (define z 99))])))
> 
> 230>(a)
> 
> 231>y
> 
> 232; y:undefined;
> 
> 233; cannot reference undefined identifier
> 
> 234; [,bt for context]
> 
> 
> 
> If the macro is given these ids, like (a x y z), then it will work, but can't 
> I also pick standard names like this in advance, or is that somehow 
> fundamentally "unhygienic"? Perhaps I have to generate the names in a place 
> visible to both the definition and use or something...
> 
> 
> 
> 
> 
> ## Peter
> 
> 
> 
> --
> 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> 
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users...@googlegroups.com.
> 
> For more options, visit https://groups.google.com/d/optout.

Ahh... datum->syntax, I thought I had seen something like this before. It is 
treating "a", or a's form, as the scope for the new ids essentially, but I can 
pick standard names. This just presupposes I only care to use my "a" macro once 
in any given scope. Does this really make it an unhygenic solution? 

In truth, I want many of these names generated and only care to use it once, so 
this seems better than passing in every name at the one macro invocation. 

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Basic macro question

2017-02-05 Thread Philip McGrath
You need to have the lexical context information of x, y, and z come from
stx: otherwise, they will be protected by macro expansion (as a matter of
hygiene).

Here's one way to do it:

> (define-syntax (a stx)
>   (syntax-parse stx
> [(a)
>  (with-syntax ([x (datum->syntax stx 'x)]
>   [y (datum->syntax stx 'y)]
>   [z (datum->syntax stx 'z)])
>#`(begin
>(define x 97)
>(define y 98)
>(define z 99)))]))


You may also want to look at format-id.

Also, I found Greg Hendershott's *Fear of Macros* (
http://www.greghendershott.com/fear-of-macros/index.html) a useful
supplement to the Racket Guide/Reference in understanding the macro system:
ch. 4 addresses some of these issues.

-Philip

On Sun, Feb 5, 2017 at 8:41 PM,  wrote:

> I must be missing something simple here.
>
> 229> (define-syntax a (lambda (stx) (syntax-parse stx [(a) #`(begin
> (define x 97) (define y 98) (define z 99))])))
> 230>(a)
> 231>y
> 232; y:undefined;
> 233; cannot reference undefined identifier
> 234; [,bt for context]
>
> If the macro is given these ids, like (a x y z), then it will work, but
> can't I also pick standard names like this in advance, or is that somehow
> fundamentally "unhygienic"? Perhaps I have to generate the names in a place
> visible to both the definition and use or something...
>
>
> ## Peter
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Basic macro question

2017-02-05 Thread Jon Zeppieri
On Sun, Feb 5, 2017 at 9:41 PM,   wrote:
> I must be missing something simple here.
>
> 229> (define-syntax a (lambda (stx) (syntax-parse stx [(a) #`(begin (define x 
> 97) (define y 98) (define z 99))])))
> 230>(a)
> 231>y
> 232; y:undefined;
> 233; cannot reference undefined identifier
> 234; [,bt for context]
>
> If the macro is given these ids, like (a x y z), then it will work, but can't 
> I also pick standard names like this in advance, or is that somehow 
> fundamentally "unhygienic"?

Yep, it's a matter of hygiene.

> Perhaps I have to generate the names in a place visible to both the 
> definition and use or something...

Not sure exactly what you have in mind. At any rate, you can provide
the identifiers to the macro, like you mentioned, or you can make a
non-hygienic macro.

-J

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.