[gambit-list] How to have define-macro expand to multiple

Adrien Piérard pierarda at iro.umontreal.ca
Mon Sep 14 02:10:53 EDT 2009


Oh, well, if you use your function in just one macro, and if you need
no shared state, you can simply do

(define-macro (mac arg1)
  (define (f x) ...)
  `(... ,(f arg1) ... ,(f 42))))

If not, then I shall invite you to join us in the munificent world of
macro-generating macros!


(define-macro (they-all-know fun . macro-defs-and-bodies)
              `(begin
                 ,@(map (lambda (def-and-body)
                          (let ((def (car def-and-body))
                                (body (cdr def-and-body)))
                            `(define-macro ,def
                                           ,fun
                                           , at body)))
                        macro-defs-and-bodies)))

(they-all-know (define (succ x) (+ x 1))
                 ((list-me-and-plus-one x) `(list ,x ,(succ x)))
                 ((list-me-and-plus-two x) `(list ,x ,(succ (succ x)))))

(pretty-print (list-me-and-plus-one 41))
(pretty-print (list-me-and-plus-two 40))


That should do it.


P!

2009/9/14 Isaac Freeman <memotype at gmail.com>:
> Yuck...
>
> that's disappointing... so my options are to a) use ugly EVAL and
> macro hacks to inject my function in to "macro space", or b) use long
> ugly monolithic macros...
>
> Thanks for the response, I'll have to weigh my options and figure out
> how I want to go about this.
>
> On Sun, Sep 13, 2009 at 11:18 PM, Adrien Piérard
> <pierarda at iro.umontreal.ca> wrote:
>> If you mean being able to do this
>>
>> (define (fun x)
>>  (list x x))
>>
>> (define-macro (mac arg)
>>  (let ((r (fun arg)))
>>    `(pretty-print ',r)))
>>
>> (mac 42)
>>
>> in the REPL, but not to compile it, it's normal.
>>
>> REPL:
>> When you eval the first define it in the REPL, gambit learns about FUN.
>> Then, the definition of macro knows it as you do eval it.
>> And when you apply the macro, it works fine.
>>
>> Compilation:
>> First, macro expansion happens. It does not know FUN yet.
>> So, it fails, for you call a function that will be eval'd *after*
>> macro expansion!
>>
>>
>> How to do it?
>> It's a bit tricky, but you can try that (though other ways exist):
>>
>>
>> (define-macro (start-macro)
>>  ;; macro environment learns about FUN
>>  (eval '(define (fun x)
>>           (list x x))))
>>
>> (define-macro (mac arg)
>>  ;; what was DEFINED in a macro is shared by all macros at this stage
>>  ;; But it's only accessible through EVAL.
>>  (let ((r (eval `(fun ,arg))))
>>    `(pretty-print ',r)))
>>
>> ;; Call the macro, to add FUN to the macro env
>> (start-macro)
>>
>> ;; When this is expanded (at macro expansion time), it will know about FUN
>> ;; which has been added to the macro environment, at macro expansion time,
>> ;; on the previous line.
>> (mac 42)
>>
>>
>>
>> Does this help you?
>>
>> P!
>>
>>
>> 2009/9/14 Isaac Freeman <memotype at gmail.com>:
>>> So, I have that much working... Is there a way to modularize
>>> DEFINE-MACRO code? When in the gsi REPL, I can define a function, and
>>> then call that from the macro, but when I try that in a file and try
>>> to compile that, it complains that the function isn't defined... am I
>>> missing something?
>>>
>>> Thanks.
>>>
>>>
>>> On Sun, Sep 13, 2009 at 11:42 AM, Ben Weaver <ben at orangesoda.net> wrote:
>>>> Hi Isaac,
>>>>
>>>> On Sun, Sep 13, 2009 at 11:24 AM, Isaac Freeman <memotype at gmail.com> wrote:
>>>>
>>>>> Specifically I'm looking to expand a macro to
>>>>> multiple, distinct, top-level (define)'s.
>>>>
>>>> Try expanding into a BEGIN:
>>>>
>>>> (begin
>>>>  (define ...)
>>>>  (define ...)
>>>>  ...)
>>>>
>>>> Good luck!
>>>>
>>>> -Ben
>>>>
>>>
>>>
>>>
>>> --
>>> Isaac Freeman
>>> memotype (at) gmail.com
>>>
>>> "The diversity of mankind is a basic postulate of our knowledge of
>>> human beings. But if mankind is diverse and individuated, then how can
>>> anyone propose equality as an ideal? Every year, scholars hold
>>> Conferences on Equality and call for greater equality, and no one
>>> challenges the basic tenet. But what justification can equality find
>>> in the nature of man? If each individual is unique, how else can he be
>>> made 'equal' to others than by destroying most of what is human in him
>>> and reducing human society to the mindless uniformity of the ant
>>> heap?" --Murray N. Rothbard
>>> _______________________________________________
>>> Gambit-list mailing list
>>> Gambit-list at iro.umontreal.ca
>>> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
>>>
>>
>>
>>
>> --
>> Français, English, 日本語, 한국어
>>
>
>
>
> --
> Isaac Freeman
> memotype (at) gmail.com
>
> "The diversity of mankind is a basic postulate of our knowledge of
> human beings. But if mankind is diverse and individuated, then how can
> anyone propose equality as an ideal? Every year, scholars hold
> Conferences on Equality and call for greater equality, and no one
> challenges the basic tenet. But what justification can equality find
> in the nature of man? If each individual is unique, how else can he be
> made 'equal' to others than by destroying most of what is human in him
> and reducing human society to the mindless uniformity of the ant
> heap?" --Murray N. Rothbard
>



-- 
Français, English, 日本語, 한국어



More information about the Gambit-list mailing list