Subject: Re: Which ones come first? - Macro question -
From: rpw3@rpw3.org (Rob Warnock)
Date: Mon, 01 Sep 2008 03:17:28 -0500
Newsgroups: comp.lang.lisp
Message-ID: <g8Gdnc02L-2FOCbVnZ2dnUVZ_t3inZ2d@speakeasy.net>
Rainer Joswig  <joswig@lisp.de> wrote:
+---------------
|  rock69 <rocco.rossi@gmail.com> wrote:
| > I can't find any info on this matter. Do reader macros (such as
| > BACKQUOTE, for instance) expand before ordinary macros, or is it the
| > other way around? ...
| 
| read macros are expanded at read time.
| `(foo ,a)  will be expanded at read time to some implementation
| specific thing. For example:
|   (SYSTEM::BQ-LIST (QUOTE FOO) A)
+---------------

However, depending on which implementation one is using and the
default values of the printer variables (or values one has set),
one might not see the true internal form (as above), but instead
just see the backquoted form printed out again, e.g.:

    > (quote `(foo ,a))

    `(FOO ,A)
    > 

However, most [but not all!!] CLs will show the "real" internal form
if you turn off pretty-printing. I've found it helpful to keep a tiny
utility function in my toolbox for just this purpose:  ;-}

    > (defun no-pp (x)
	(let ((*print-pretty* nil))
	  (write x)))

    NO-PP
    > (no-pp (quote `(foo ,a)))
    (LISP::BACKQ-LIST (QUOTE FOO) A)
    `(FOO ,A)
    > 

Note that we see the form printed twice: once with pretty-printing
turned off when printed by the WRITE, and then again with the default
pretty-printing back on when the *value* of the WRITE (just its argument)
is printed by the top-level REPL.

Rocco, nota bene!! You will see that Rainer's & my CL implementations
display *different* forms as the output of the `(FOO ,A) form!!!  Which
is right?!? Answer: *BOTH!* *NEITHER!* THERE IS NO "RIGHT"!  ;-} ;-}
It's implementation-dependent. He's using Lispworks, I think,
and I'm using CMUCL.

Unlike the Scheme standard, which specifies a specific reader
transformation to be used for backquote & friends:

    `form ==> (quasiquote form)
    ,form ==> (unquote form)
    ,@form ==> (unquote-splicing form)

the CLHS does not:

    2.4.6 Backquote
    ...
    An implementation is free to interpret a backquoted form F1 as
    any form F2 that, when evaluated, will produce a result that is
    the same under equal as the result implied by the above definition,
    provided that the side-effect behavior of the substitute form F2
    is also consistent with the description given above. The constructed
    copy of the template might or might not share list structure with
    the template itself.

and:

    2.4.6.1 Notes about Backquote

    Since the exact manner in which the Lisp reader will parse an
    expression involving the backquote reader macro is not specified,
    an implementation is free to choose any representation that preserves
    the semantics described.

    Often an implementation will choose a representation that facilitates
    pretty printing of the expression, so that (pprint `(a ,b)) will
    display `(a ,b) and not, for example, (list 'a b). However, this
    is not a requirement. 
    
    ...[and then mentions the Scheme standard for a possible
        but *not* required choice of internal representation]...

You have been warned!  ;-}


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607