[racket] macro expansion function in Scheme

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Tue Jul 12 10:46:24 EDT 2011

I think you want to use `expand' and `eval-syntax', and avoid
`syntax->datum' entirely.

On Tue, Jul 12, 2011 at 10:30 AM, Bas Steunebrink <bas at idsia.ch> wrote:
> Thanks Matthias and Eli, I understand what expand does and that
> syntax->datum throws away too much (by design). Still my question remains of
> how to obtain a datum that is equivalent to a given quoted expression (when
> eval'ed) but with all macros expanded (generating fresh symbols where
> necessary). So does there exist a function syntax->datum* such that:
>
>> (define-syntax m
>    (syntax-rules ()
>      ((_ x)
>       (lambda (x) (lambda (y) x)))))
>> (((m y) #t) #f)
> #t   ; OK
>> (syntax->datum* (expand '(m y)))
> (lambda (y) (lambda (g42) y))   ; g42 is a fresh symbol
>> (((eval (syntax->datum* (expand '(m y)))) #t) #f)
> #t   ; using Racket's syntax->datum gives #f
>
> Please note that I'm really interested in getting just the macro-expanded
> datum (otherwise I would've been done already!).
>
>
> Thanks again for your kind help,
> Bas
>
>
>
>
> On 7/12/11 16:17 PM, Matthias Felleisen wrote:
>>
>>
>> On Jul 12, 2011, at 10:16 AM, Eli Barzilay wrote:
>>
>>> 8 minutes ago, Matthias Felleisen wrote:
>>>>
>>>> The following experiment produces a bit more insight:
>>>>
>>>>> (((eval (expand '(m y))) #t) #f)
>>>>
>>>> #t
>>>>
>>>> The result of expand compiles to the correct closure.
>>>>
>>>> So the docs for syntax->datum are dead serious when they say that it
>>>> "returns a datum by stripping the lexical information, source
>>>> location information, properties, and tamper status from stx."
>>>
>>> Try also the macro stepper.  From an upcoming package for the repl:
>>>
>>>  ->  (define-syntax m
>>>       (syntax-rules ()
>>>         ((_ x)
>>>          (lambda (x) (lambda (y) x)))))
>>>  ->  ,stx (m y) *
>>>  syntax set
>>>  stepper:
>>>  Macro transformation
>>>  (m y)
>>>    ==>
>>>  (lambda:1 (y) (lambda:1 (y:1) y))
>>>
>>> (But you'd usually want to use the gui version...)
>>
>> Fire up drracket and run the stepper.
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>



-- 
sam th
samth at ccs.neu.edu



Posted on the users mailing list.