From: Alan M. Carroll
Subject: <none>
Date: 
Message-ID: <1990Oct30.225047.17818@ux1.cso.uiuc.edu>
A very basic question from a non-expert Lisper:

What is the best way to have a macro expand to more than 1 top-level form in
CommonLisp? Right now I use something like `(progn form1 form2 ...), but I
worry that since the forms aren't actually at the top level in the file,
they might not be processed/compiled/etc. correctly. What I want to accomplish
is to have macros that set up read/writers in CLOS automatically (I have large
numbers of almost identical ones, only the class name / slot names change).
Thanks!

From: Tim Moore
Subject: <None>
Date: 
Message-ID: <1990Oct30.162608.20530@hellgate.utah.edu>
In article <······················@ux1.cso.uiuc.edu> ·······@cs.uiuc.edu (Alan M. Carroll) writes:
>A very basic question from a non-expert Lisper:
>
>What is the best way to have a macro expand to more than 1 top-level form in
>CommonLisp? Right now I use something like `(progn form1 form2 ...), but I
>worry that since the forms aren't actually at the top level in the file,
>they might not be processed/compiled/etc. correctly. What I want to accomplish
>is to have macros that set up read/writers in CLOS automatically (I have large
>numbers of almost identical ones, only the class name / slot names change).

PROGN will work fine for what you want. If a progn is seen at
top level, the forms of the progn are processed as if they are at top
level too.

Tim Moore                    ·····@cs.utah.edu {bellcore,hplabs}!utah-cs!moore
"Ah, youth. Ah, statute of limitations."
		-John Waters
From: Barry Margolin
Subject: Macro expanding into multiple forms
Date: 
Message-ID: <1990Oct31.010958.28374@Think.COM>
In article <······················@ux1.cso.uiuc.edu> ·······@cs.uiuc.edu (Alan M. Carroll) writes:
>What is the best way to have a macro expand to more than 1 top-level form in
>CommonLisp? Right now I use something like `(progn form1 form2 ...), but I
>worry that since the forms aren't actually at the top level in the file,
>they might not be processed/compiled/etc. correctly.

That is the correct way to do it.  See the last sentence of p.83 of CLtL
2nd Edition: "As a special case, however, if a PROGN form appears at top
level, then all forms within that PROGN are considered by the compiler to
be top-level forms."
--
Barry Margolin, Thinking Machines Corp.

······@think.com
{uunet,harvard}!think!barmar
From: Pete Halverson
Subject: Re: <none>
Date: 
Message-ID: <13279@crdgw1.crd.ge.com>
In article <······················@ux1.cso.uiuc.edu> Alan M. Carroll writes:
>What is the best way to have a macro expand to more than 1 top-level form in
>CommonLisp? Right now I use something like `(progn form1 form2 ...), but I
>worry that since the forms aren't actually at the top level in the file,
>they might not be processed/compiled/etc. correctly. 

Using progn is the accepted way to handle macros with multiple forms.  In
CLtL.1, Steele said that "if a progn form appears at a top level, then all
forms within that progn are considered by the compiler to be top-level
forms".  Furthermore, in the latest version, apparently "compilers must
handle defining forms properly in *all* situations, not just top-level
contexts" [emphasis added].

--
===============================================================================
Pete Halverson                        		    INET: ·········@crd.ge.com 
GE Corporate R&D Center	                      UUCP: uunet!crd.ge.com!halverson
Schenectady, NY                     "Money for nuthin' and your MIPS for free"
From: Larry Masinter
Subject: Re: <none>
Date: 
Message-ID: <MASINTER.90Oct31212008@origami.parc.xerox.com>
>Using progn is the accepted way to handle macros with multiple forms.  In
>CLtL.1, Steele said that "if a progn form appears at a top level, then all
>forms within that progn are considered by the compiler to be top-level
>forms".  Furthermore, in the latest version, apparently "compilers must
>handle defining forms properly in *all* situations, not just top-level
>contexts" [emphasis added].

There's still a distinction between top level and not, however. The
following is fine:

(progn 
   (defmacro pair (x y) `(cons ,x ,y))
   (defun get-pair (x y) (pair x y)))


(get-pair 1 2) => '(1 . 2)

but

(defun definem () 
    (defmacro pair (x y) `(cons ,x ,y))
    (defun get-pair (x y) (pair x y)))
(definem)
(get-pair) 

is an error in a file to be compiled, since

   "Users must ensure that the
    body of the macro is evaluable at compile time if it is referenced
    within the file being compiled."


Writing down what works and doesn't was painful.

--
Larry Masinter (········@parc.xerox.com)
Xerox Palo Alto Research Center (PARC)
3333 Coyote Hill Road; Palo Alto, CA USA 94304
Fax: (415) 494-4333