From: Jeff Barnett
Subject: Macro expansion semantics
Date: 
Message-ID: <467ed3d7$0$4678$4c368faf@roadrunner.com>
Consider the following macro:

(defmacro foo (&body body &environment env)
  `(macrolet((outside (form)
           (macroexpand form ',env)))
     ,@body))

My question is what does spec-compliant Lisp guarantee about the 
expansion of form in (outside form). The answer would seem to depend on 
whether macroexpand is permitted to expand an expansion, whether it is 
allowed to peek inside an expansion, and whether the compiler is allowed 
to add information it learns during compilation to environment. Another 
way of asking (part of) my question is, "Is this macro portable?"

-- Jeff Barnett

From: Tobias C. Rittweiler
Subject: Re: Macro expansion semantics
Date: 
Message-ID: <87d4zlcbl7.fsf@freebits.de>
Jeff Barnett <······@ca.rr.com> writes:

> Consider the following macro:
>
> (defmacro foo (&body body &environment env)
>  `(macrolet((outside (form)
>           (macroexpand form ',env)))
>     ,@body))
>
> ... Another way of asking (part of) my question is, "Is this
> macro portable?"

No, &environment objects are specified to have dynamic extent. (cf.
CLHS 3.4.4)

  -T.
From: Jeff Barnett
Subject: Re: Macro expansion semantics
Date: 
Message-ID: <4680a0ce$0$4686$4c368faf@roadrunner.com>
Tobias C. Rittweiler wrote:
> Jeff Barnett <······@ca.rr.com> writes:
>
>   
>> Consider the following macro:
>>
>> (defmacro foo (&body body &environment env)
>>  `(macrolet((outside (form)
>>           (macroexpand form ',env)))
>>     ,@body))
>>
>> ... Another way of asking (part of) my question is, "Is this
>> macro portable?"
>>     
>
> No, &environment objects are specified to have dynamic extent. (cf.
> CLHS 3.4.4)
>
>   -T.
>   
Thanks for the reply. I forgot about the dynamic extent restriction. It 
seems that to achieve the sorts of effects that one might expect with 
the above, it would be necessary to write a code walker. That move is 
only justified for a major program development hack. Another mechanism 
that would allow some flexibility would be to give macroexpand a 
"recursive" option so you could play with the environment while it is 
still guaranteed to exist. However, the spec doesn't define such a 
capability though some implementations provide it as an extension (not 
portable).

-- Jeff Barnett
From: Pascal Costanza
Subject: Re: Macro expansion semantics
Date: 
Message-ID: <5ebr1gF29d5soU1@mid.individual.net>
Jeff Barnett wrote:
> Tobias C. Rittweiler wrote:
>> Jeff Barnett <······@ca.rr.com> writes:
>>
>>  
>>> Consider the following macro:
>>>
>>> (defmacro foo (&body body &environment env)
>>>  `(macrolet((outside (form)
>>>           (macroexpand form ',env)))
>>>     ,@body))
>>>
>>> ... Another way of asking (part of) my question is, "Is this
>>> macro portable?"
>>>     
>>
>> No, &environment objects are specified to have dynamic extent. (cf.
>> CLHS 3.4.4)
>>
>>   -T.
>>   
> Thanks for the reply. I forgot about the dynamic extent restriction. It 
> seems that to achieve the sorts of effects that one might expect with 
> the above, it would be necessary to write a code walker. 

No, but it requires a lot of work.

What you could do is shadow all macro-defining forms (like defmacro, 
macrolet, define-symbol-macro, symbol-macrolet, etc.) and create your 
own custom macro environment. Then you could do with it whatever you 
want, without the need for a code walker.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/