The `syntax-local-introduce` solution breaks when the macro is used from a 
different module.
The datum->syntax solution may or may not have the behavior you are looking 
for:

#lang racket 

(module a racket
  (require (for-syntax syntax/parse)) 
  (require (for-syntax racket/syntax)) 

  (define x 'a)
  (define-syntax (my-macro stx) 
    (syntax-parse stx 
      [(_ x:id) 
       #:with a #'a 
       #:with y (datum->syntax #'a (syntax-e #'x)) 
       #`(lambda (a b) y)]))
  (provide my-macro))

(require 'a)

; These work
((my-macro a) 1 2) ; => 1
((my-macro b) 1 2) ; => 2

; Is this what you want?
(define x 'b)  
((my-macro x) 1 2) ; => 'a


Notice that references are resolved in the complete environment of the 
macro definition site, and not only to names bound in the macro-generated 
template.

An alternative approach would be to have the macro unhygienicially 
introduce the *bindings* a and b. The effect is different:

#lang racket 

(module a racket
  (require (for-syntax syntax/parse)) 
  (require (for-syntax racket/syntax)) 

  (define-syntax (my-macro stx) 
    (syntax-parse stx 
      [(_ x:id) 
       #:with a (datum->syntax this-syntax 'a) 
       #:with b (datum->syntax this-syntax 'b) 
       #`(lambda (a b) x)]))
  (provide my-macro))

(require 'a)

; These work
((my-macro a) 1 2) ; => 1
((my-macro b) 1 2) ; => 2

; Is this what you want?
(define x 'b)  
((my-macro x) 1 2) ; => 'b


On Saturday, February 23, 2019 at 8:22:57 AM UTC-5, Stefano Lande wrote:
>
> Thanks to both of you, it is exactly what I was trying to do.
>
> Stefano
>
> Il giorno ven 22 feb 2019 alle ore 18:37 Matthias Felleisen <
> [email protected] <javascript:>> ha scritto:
>
>>
>> And that’s better of course because you pick up the lexical scope, which 
>>
>>   (let ([x 4]) [(my-macro x) 1 2])
>>
>> shows. 
>>
>>
>>
>> > On Feb 22, 2019, at 1:26 PM, Sam Caldwell <[email protected] 
>> <javascript:>> wrote:
>> > 
>> > You can also do this with syntax-local-introduce to remove x's use-site 
>> scope*:
>> > 
>> > #lang racket
>> > 
>> > (require (for-syntax syntax/parse))
>> > 
>> > (define-syntax (my-macro stx)
>> >    (syntax-parse stx
>> >      [(_ x:id)
>> >       #:with x- (syntax-local-introduce #'x)
>> >       #'(lambda (a b) x-)]))
>> > 
>> > ((my-macro a) 1 2)
>> > ;; 1
>> > 
>> > ((my-macro b) 1 2)
>> > ;; 2
>> > 
>> > (define x 3)
>> > ((my-macro x) 1 2)
>> > ;; 3
>> > 
>> > 
>> > 
>> > -Sam
>> > 
>> > * well, I think that's what's going on.
>> > 
>> > On Fri, Feb 22, 2019 at 1:21 PM Matthias Felleisen <
>> [email protected] <javascript:>> wrote:
>> > 
>> > 
>> > > On Feb 22, 2019, at 1:08 PM, Stefano Lande <[email protected] 
>> <javascript:>> wrote:
>> > > 
>> > > Dear all,
>> > > 
>> > > first of all, I might being misusing the terminology. Sorry about it.
>> > > 
>> > > I would like to write a macro that gets an identifier and return its 
>> value in the new lexical scope created by the macro.
>> > > For example:
>> > > 
>> > > > (define-syntax (my-macro stx)
>> > >    (syntax-parse stx
>> > >      [(_ x:id)  #'(lambda (a b) x) ]))
>> > > 
>> > > 
>> > > > ((my-macro a) 1 2) 
>> > > 1
>> > > 
>> > > >((my-macro b) 1 2) 
>> > > 2
>> > > 
>> > > 
>> > > 
>> > > my-macro as above of course would not work. Is possible to receive an 
>> identifier, strip the lexical context, and evaluate it in the context of 
>> (lambda (a b) body) ?
>> > 
>> > 
>> > Here is one way to get your macro: 
>> > 
>> > #lang racket
>> > 
>> > (require (for-syntax syntax/parse))
>> > (require (for-syntax racket/syntax))
>> > 
>> > (define-syntax (my-macro stx)
>> >   (syntax-parse stx
>> >     [(_ x:id)
>> >      #:with a #'a
>> >      #:with y (datum->syntax #'a (syntax-e #'x))
>> >      #`(lambda (a b) y)]))
>> > 
>> > [(my-macro a) 1 2]
>> > [(my-macro b) 1 2]
>> > (define x 3)
>> > [(my-macro x) 1 2]
>> > 
>> > -- 
>> > 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 [email protected] <javascript:>.
>> > 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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to