From: Arne  Hanssen
Subject: Trying to understand macros
Date: 
Message-ID: <arne.hansenNOSPAM-3442AA.19051406122007@news.chello.no>
I've been trying to understand macros in Lisp.  In Grahams "On Lisp" 
there is an example where he show how to make quote using a read-macro.

The place where I am stuck is at this code.

(defmacro q (obj)
  `(quote ,obj)
  
Graham shows that this code will note work for (q (q a)) giving (quote 
(q a)) instead of (quote (quote a)). 

Why?. My impression was that one could make another macro-expand inside 
the body of a macro. Like calling (do ...) inside the (when ...) macro.

From: Rainer Joswig
Subject: Re: Trying to understand macros
Date: 
Message-ID: <joswig-96BBF9.19165706122007@news-europe.giganews.com>
In article <·······································@news.chello.no>,
 Arne  Hanssen <·················@runbox.com> wrote:

> I've been trying to understand macros in Lisp.  In Grahams "On Lisp" 
> there is an example where he show how to make quote using a read-macro.
> 
> The place where I am stuck is at this code.
> 
> (defmacro q (obj)
>   `(quote ,obj)
>   
> Graham shows that this code will note work for (q (q a)) giving (quote 
> (q a)) instead of (quote (quote a)). 
> 
> Why?. My impression was that one could make another macro-expand inside 
> the body of a macro. Like calling (do ...) inside the (when ...) macro.


Well, quote stops evaluation and thus macroexpansion.

(q (q a))  is being expanded first to  (quote (q a)).

(quote (q a)) cannot be further expanded and its subforms
will also not be expanded. QUOTE is special operator.

-- 
http://lispm.dyndns.org/
From: Ken Tilton
Subject: Re: Trying to understand macros
Date: 
Message-ID: <BSX5j.42$m_.3@newsfe08.lga>
Rainer Joswig wrote:
> In article <·······································@news.chello.no>,
>  Arne  Hanssen <·················@runbox.com> wrote:
> 
> 
>>I've been trying to understand macros in Lisp.  In Grahams "On Lisp" 
>>there is an example where he show how to make quote using a read-macro.
>>
>>The place where I am stuck is at this code.
>>
>>(defmacro q (obj)
>>  `(quote ,obj)
>>  
>>Graham shows that this code will note work for (q (q a)) giving (quote 
>>(q a)) instead of (quote (quote a)). 
>>
>>Why?. My impression was that one could make another macro-expand inside 
>>the body of a macro. Like calling (do ...) inside the (when ...) macro.
> 
> 
> 
> Well, quote stops evaluation and thus macroexpansion.
> 
> (q (q a))  is being expanded first to  (quote (q a)).

Ah, but why is it not first being expanded to (q (quote a))?

kt

> 
> (quote (q a)) cannot be further expanded and its subforms
> will also not be expanded. QUOTE is special operator.
> 

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Kent M Pitman
Subject: Re: Trying to understand macros
Date: 
Message-ID: <ufxyf72y6.fsf@nhplace.com>
Ken Tilton <···········@optonline.net> writes:

> Rainer Joswig wrote:
> > In article <·······································@news.chello.no>,
> >  Arne  Hanssen <·················@runbox.com> wrote:
> >
> >> I've been trying to understand macros in Lisp.  In Grahams "On
> >> Lisp" there is an example where he show how to make quote using a
> >> read-macro.
> >>
> >>The place where I am stuck is at this code.
> >>
> >>(defmacro q (obj)
> >>  `(quote ,obj)
> >>  Graham shows that this code will note work for (q (q a)) giving
> >> (quote (q a)) instead of (quote (quote a)). Why?. My impression was
> >> that one could make another macro-expand inside the body of a
> >> macro. Like calling (do ...) inside the (when ...) macro.
> > Well, quote stops evaluation and thus macroexpansion.
> > (q (q a))  is being expanded first to  (quote (q a)).
> 
> Ah, but why is it not first being expanded to (q (quote a))?

Because if you did macro expansion from the inside out instead of the
outside in, you could never implement things like binding forms that
need to control the meaning of things inside of them.

The question is different in detail but has some conceptual relation 
to asking why

 (let ((y 3))
   (let ((x 4))
     (+ x y)))

is not appropriately evaluated by first evaluating the inner
 (let ((x 4)) (+ x y))
to get a result, and then evaluating 
 (let ((y 3)) <result>)

The very meaning of QUOTE is "do not evaluate this", not "don't
evaluate what's left of this after evaluating the parts you want to
evaluate without my getting a chance to stop you".

Probably it's the silliness about how things like "$FOO" are evaluated
in scripting languages, violating quotation, that is confusing things.
If people think about QUOTE as the stronger form, '$FOO' they will get
better mileage.
From: Rainer Joswig
Subject: Re: Trying to understand macros
Date: 
Message-ID: <joswig-A5FAD7.20373106122007@news-europe.giganews.com>
In article <·············@newsfe08.lga>,
 Ken Tilton <···········@optonline.net> wrote:

> Rainer Joswig wrote:
> > In article <·······································@news.chello.no>,
> >  Arne  Hanssen <·················@runbox.com> wrote:
> > 
> > 
> >>I've been trying to understand macros in Lisp.  In Grahams "On Lisp" 
> >>there is an example where he show how to make quote using a read-macro.
> >>
> >>The place where I am stuck is at this code.
> >>
> >>(defmacro q (obj)
> >>  `(quote ,obj)
> >>  
> >>Graham shows that this code will note work for (q (q a)) giving (quote 
> >>(q a)) instead of (quote (quote a)). 
> >>
> >>Why?. My impression was that one could make another macro-expand inside 
> >>the body of a macro. Like calling (do ...) inside the (when ...) macro.
> > 
> > 
> > 
> > Well, quote stops evaluation and thus macroexpansion.
> > 
> > (q (q a))  is being expanded first to  (quote (q a)).

If you have a form (a (b c)), first the symbol 'a' will
be checked. What is it? A function? A macro? A special operator?
We have a macro. So the macro a gets called with the form
(b c). It returns (quote (b c)). Now we look again.
But at QUOTE. What is quote? A macro? No. A function? No.
It is a special operator. Done. The form stays as such.


> 
> Ah, but why is it not first being expanded to (q (quote a))?
> 
> kt
> 
> > 
> > (quote (q a)) cannot be further expanded and its subforms
> > will also not be expanded. QUOTE is special operator.
> >

-- 
http://lispm.dyndns.org/
From: Kaz Kylheku
Subject: Re: Trying to understand macros
Date: 
Message-ID: <5fe9bbf9-82c2-4244-9a48-cb8f28c0cb77@e25g2000prg.googlegroups.com>
On Dec 6, 10:05 am, Arne  Hanssen <·················@runbox.com>
wrote:
> I've been trying to understand macros in Lisp.  In Grahams "On Lisp"
> there is an example where he show how to make quote using a read-macro.
>
> The place where I am stuck is at this code.
>
> (defmacro q (obj)
>   `(quote ,obj)
>
> Graham shows that this code will note work for (q (q a)) giving (quote
> (q a)) instead of (quote (quote a)).
>
> Why?.

Graham's point seems to be that the read notation ' and others, are
not macros, and can't be implemented as macros.

Thus ''a will yield (quote (quote a)) (at read time), but (q (q a))
will not yield (quote (quote a)).

> My impression was that one could make another macro-expand inside
> the body of a macro.

Your impression is correct, because a macro can have complete control
over how its contents are treated. So given

 (q <stuff>)

when the Q macro is called upon to expand that form, it can do
anything it wants with <stuff> to produce a translation. Q can be the
front end for a compiler for a completely different programming
language.

Some Lisp implementations have backquote as a macro. In that style of
backquote implemenation, the `X notation transliterates into something
like (system-package::backquote X), and the expansion is deferred to
the macro. The unquotes and splice appearing within X are also
rendered as list forms, and of course X can contain nested
backquotes.  The backquote macro will walk the form and perform
expansions, treating the embedded unquotes, splices and nested
backquotes appropriately.

Similarly, your Q macro could walk the form looking for other
instances of Q being called, changing them into QUOTE.   In this case,
that /can/ be done by calling MACROEXPAND on a compound form which has
the symbol Q in the first position. Your macro still has to walk the
form to find these expressions.

This code walking can be tricky business, even in this kind of
situation where you don't have to worry about recognizing special
operators. You don't care about those, because you're emulating a read
notation which is blind to evaluation context. However, that read
notation can take place inside various animals that you have to
handle. Consider

 #(1 2 3 'X)  ;; read as #(1 2 3 (QUOTE X))

 #(1 2 3 (Q X)) ;; code walker has to handle vectors to make this work

What about structures?

 #(foo :slot-x 'x)  ;; no problem, SLOT-X gets bound to (QUOTE X)!

 #(foo :slot-x (q x)) ;; what to do?

To handle this case, your code walker needs to be able to gain access
to the list of slots of the given structure type, iterate over that
list and walk the contents of each of the slot, if it is a form.
Problem is, Common Lisp doesn't give you a portable way to do this.
You can either hack it in nonportable ways, or live without ``Q
correctness'' within struct literals.
From: Daniel Weinreb
Subject: Re: Trying to understand macros
Date: 
Message-ID: <pH16j.9732$6k1.5689@trndny02>
Arne Hanssen wrote:
> I've been trying to understand macros in Lisp.  In Grahams "On Lisp" 
> there is an example where he show how to make quote using a read-macro.

Don't confuse "read macros" with "macros".  They are entirely
different.  "Read macros" are hardly ever used.  If you are
just learning Lisp, pay no attention to them, and focus on
ordinary Lisp macros.

> 
> The place where I am stuck is at this code.
> 
> (defmacro q (obj)
>   `(quote ,obj)
>   
> Graham shows that this code will note work for (q (q a)) giving (quote 
> (q a)) instead of (quote (quote a)). 
> 
> Why?. My impression was that one could make another macro-expand inside 
> the body of a macro. Like calling (do ...) inside the (when ...) macro.