[racket] macro expansion function in Scheme

From: Bas Steunebrink (bas at idsia.ch)
Date: Tue Jul 12 19:08:01 EDT 2011

Excellent idea! I looked at the source code of macro-debugger but there 
was quite an intimidating amount of it. :-s So I took the ugliest but 
fastest path: simply catch the textual output of the expand/step-text 
function and use Racket's reader to get the result of the last step as a 
datum (I know it's horrible but it works!). Any anomalies (such as 
"lambda:2" -> "lambda") are easily fixed. :-)

Thanks again for the helpful pointers,

Op 12-7-2011 16:35, Matthias Felleisen schreef:
> Since Ryan's stepper does that and renders the expression for you, I suspect he could supply something close to syntax->datum*.
> On Jul 12, 2011, at 10:30 AM, Bas Steunebrink 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.

Posted on the users mailing list.