Re: [racket-users] Re: My macro works in the global namespace, but fails inside of a let

2019-10-07 Thread Eric Griffis
Hi Cistian,

Both of your examples are hygienic, in the sense that neither actually
do anything that violates macro hygiene.

When `a_definition` is defined in the module context (i.e., the
"global namespace"), it has the same scope as the `a_definition` used
inside `my-macro`. (There's more to it, but this is enough for now.)

When `a_definition` is defined in a local context (e.g., inside a
`(let () ...)` form) outside `my-macro`, it won't have the same scope
as the `a_definition` used inside `my-macro`.

There's an easy trick to get around this. As Simon suggested, make
`a_definition` an argument to `my-macro`:

  (define-simple-macro (my-macro a_definition)
(display a_definition))

Now, each call to `my-macro` "borrows" `a_definition` from the macro
caller's scope. There are other ways to tackle the problem, but this
is the safest option.

Eric

On Mon, Oct 7, 2019 at 7:33 AM Cistian Alejandro Alvarado Vázquez
 wrote:
>
> Okay, that I am aware of, but why is it hygienic inside of let but *not* in 
> the global namespace? Also, you should (from my limited understanding!) still 
> be able to access bindings from inside of a macro, no? For example, I can 
> call functions inside of a macro and those identifiers are available even 
> though they weren't provided as arguments. So it appears that macros have 
> access to module-level bindings but NOT more local bindings, and that is what 
> I'm confused about.
>
> On Monday, 7 October 2019 08:04:56 UTC-5, Simon Schlee wrote:
>>
>> Your second macro does not work as you expect, because by default macros in 
>> racket are hygienic, described roughly that means that a macro only 
>> "manipulates" the syntax it is given as arguments, not with the syntax 
>> around its invocation. As gfb has mentioned one way to do this is to use 
>> parameters, this way you can achieve dynamic effects without breaking 
>> hygiene.
>>
>> Why is hygiene good? This section in the guide explains: 
>> https://docs.racket-lang.org/guide/pattern-macros.html?q=macro#%28part._.Lexical_.Scope%29
>>
>> You say you want to use some definitions in your macro, why not just pass 
>> the relevant ones as parameters?
>> Without knowing more about what you are trying to do, it is difficult to 
>> suggest a good direction/solution.
>>
>> Not part of the answer, but related and interesting:
>> (fifth RacketCon): Matthew Flatt — Bindings as Sets of Scopes
>> https://www.youtube.com/watch?v=ABWLveMNdzg
>
> --
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/47407fc2-2c51-4cf1-a5b7-78a390484fbf%40googlegroups.com.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAORuSUwA-%3DLEK7DughwXZKyW2d10oxV8EcXgGgx5jnonioMX6g%40mail.gmail.com.


[racket-users] Re: My macro works in the global namespace, but fails inside of a let

2019-10-07 Thread Cistian Alejandro Alvarado Vázquez
Okay, that I am aware of, but why is it hygienic inside of let but *not* in 
the global namespace? Also, you should (from my limited understanding!) 
still be able to access bindings from inside of a macro, no? For example, I 
can call functions inside of a macro and those identifiers are available 
even though they weren't provided as arguments. So it appears that macros 
have access to module-level bindings but NOT more local bindings, and that 
is what I'm confused about.

On Monday, 7 October 2019 08:04:56 UTC-5, Simon Schlee wrote:
>
> Your second macro does not work as you expect, because by default macros 
> in racket are hygienic, described roughly that means that a macro only 
> "manipulates" the syntax it is given as arguments, not with the syntax 
> around its invocation. As gfb has mentioned one way to do this is to use 
> parameters, this way you can achieve dynamic effects without breaking 
> hygiene.
>
> Why is hygiene good? This section in the guide explains: 
> https://docs.racket-lang.org/guide/pattern-macros.html?q=macro#%28part._.Lexical_.Scope%29
>
> You say you want to use some definitions in your macro, why not just pass 
> the relevant ones as parameters? 
> Without knowing more about what you are trying to do, it is difficult to 
> suggest a good direction/solution.
>
> Not part of the answer, but related and interesting:
> (fifth RacketCon): Matthew Flatt — Bindings as Sets of Scopes
> https://www.youtube.com/watch?v=ABWLveMNdzg
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/47407fc2-2c51-4cf1-a5b7-78a390484fbf%40googlegroups.com.


[racket-users] Re: My macro works in the global namespace, but fails inside of a let

2019-10-07 Thread Simon Schlee
I meant to write (less confusing):
"You say you want to use some definitions in your macro, why not just pass 
the relevant ones as arguments to your macro?" 

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/75ca34f2-4aa7-4b05-9421-bb143ff225c0%40googlegroups.com.


[racket-users] Re: My macro works in the global namespace, but fails inside of a let

2019-10-07 Thread Simon Schlee
Your second macro does not work as you expect, because by default macros in 
racket are hygienic, described roughly that means that a macro only 
"manipulates" the syntax it is given as arguments, not with the syntax 
around its invocation. As gfb has mentioned one way to do this is to use 
parameters, this way you can achieve dynamic effects without breaking 
hygiene.

Why is hygiene good? This section in the guide explains: 
https://docs.racket-lang.org/guide/pattern-macros.html?q=macro#%28part._.Lexical_.Scope%29

You say you want to use some definitions in your macro, why not just pass 
the relevant ones as parameters? 
Without knowing more about what you are trying to do, it is difficult to 
suggest a good direction/solution.

Not part of the answer, but related and interesting:
(fifth RacketCon): Matthew Flatt — Bindings as Sets of Scopes
https://www.youtube.com/watch?v=ABWLveMNdzg

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/05e6ed93-596d-472e-8e3e-b2e7d87c695b%40googlegroups.com.


[racket-users] Re: My macro works in the global namespace, but fails inside of a let

2019-10-06 Thread gfb
Hmm, maybe you'd find “parameters” (not to be confused with function 
parameters) useful for your goals.

#lang racket

(define current_meaning (make-parameter "some string"))

(define (display_current_meaning)
  (display (current_meaning)))

(display_current_meaning)

(let ()
  (parameterize ([current_meaning "a different string"])
(display_current_meaning)))

(display_current_meaning)

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/47a28e82-eb5f-418d-8f45-71876c561719%40googlegroups.com.


[racket-users] Re: My macro works in the global namespace, but fails inside of a let

2019-10-06 Thread gfb
No doubt you will soon get more precise answers, and help for your goal, 
from others, but the first step is that macros in scheme and racket are not 
straight textual substitutions.

The a_definition in the template of my_macro has much the same scope as it 
would if my_macro were just a function (defined with define, and then I'd 
refer to (display a_definition) as “the body” rather than “the template”).

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/f88821df-5cac-40fb-a097-ee94ca25c066%40googlegroups.com.