[net.lang.lisp] question for a Common Lisp guru

patcl@hammer.UUCP (Pat Clancy) (02/25/86)

The book Common Lisp (1984) says that "defun", used to
establish global function definitions, is a macro (p. 67).
Yet, this doesn't seem possible, as there does not seem to
be any special form which could be used to implement such
a macro. Also, there are some references in the book to
"defun" as a special form, although it is not listed
in the table of all special forms (p. 57). So, is the
book wrong to define "defun" as a macro, and should it really
be one of the special forms?

Pat Clancy
Tektronix

costin@h-sc1.UUCP (dan costin) (02/28/86)

In article <1829@hammer.UUCP> patcl@hammer.UUCP (Pat Clancy) writes:
>
>The book Common Lisp (1984) says that "defun", used to
>establish global function definitions, is a macro (p. 67).
>Yet, this doesn't seem possible, as there does not seem to
>be any special form which could be used to implement such
>a macro...

If the good book says it's a macro, then it's a macro. :-)
Seriously, though, if you use the step function you'll soon find out that
all functions are expanded into blocks with all sorts of stuff in them.
So defun is a macro, really.  Try (describe 'defun).  That should be
the absolutest right answer about what it is.

-dan costin

diamant@hpfcla.UUCP (03/01/86)

/***** hpfclp:net.lang.lisp / hammer!patcl / 11:13 pm  Feb 24, 1986*/

The book Common Lisp (1984) says that "defun", used to
establish global function definitions, is a macro (p. 67).
Yet, this doesn't seem possible, as there does not seem to
be any special form which could be used to implement such
a macro. Also, there are some references in the book to
"defun" as a special form, although it is not listed
in the table of all special forms (p. 57). So, is the
book wrong to define "defun" as a macro, and should it really
be one of the special forms?

Pat Clancy
Tektronix
/* ---------- */

Defun can (and typically is) implemented with (setf (symbol-function ...

In particular, a simple definition (no error checking; documentation not
handled, etc) for defun is:

(defmacro defun (name &rest body)
    `(setf (symbol-function ',name #'(lambda ,@body))))


John Diamant
Fort Collins Systems Division	UUCP:  {ihnp4!hpfcla,hplabs}!hpfclp!diamant
Hewlett Packard Co.		ARPA/CSNET: diamant%hpfclp@hplabs
Fort Collins, CO

barmar@mit-eddie.UUCP (Barry Margolin) (03/02/86)

In article <1829@hammer.UUCP> patcl@hammer.UUCP (Pat Clancy) writes:
>The book Common Lisp (1984) says that "defun", used to
>establish global function definitions, is a macro (p. 67).
>Yet, this doesn't seem possible, as there does not seem to
>be any special form which could be used to implement such
>a macro.

It can expand into
	(setf (symbol-value ...
See the last paragraph of the symbol-function description on page 90 of
the Common Lisp book.

> Also, there are some references in the book to
>"defun" as a special form, although it is not listed
>in the table of all special forms (p. 57). So, is the
>book wrong to define "defun" as a macro, and should it really
>be one of the special forms?

These references are in error, probably left over from the more
Maclisp-like original definition.
-- 
    Barry Margolin
    ARPA: barmar@MIT-Multics
    UUCP: ..!genrad!mit-eddie!barmar

patcl@hammer.UUCP (03/04/86)

In article <1142@mit-eddie.UUCP> barmar@eddie.UUCP (Barry Margolin) writes:
>>There does not seem to be any special form which could be
>>used to implement such a macro [defun].
>
>It can expand into
>	(setf (symbol-value ...
>See the last paragraph of the symbol-function description on page 90 of
>the Common Lisp book.
>

Unfortunately, setf is itself a macro. So what is the final real
form that that defun expands to? I've gotten several mail responses
but none that appear to be correct, and I do not have access to a
CL implementation.

Pat Clancy
"this is not a signature line"

christer@kuling.UUCP (03/04/86)

-- 
SMail: Christer Johansson  UUCP:  {seismo,seismo!mcvax}!enea!kuling!christer OR
       Sernandersv. 9:136         christer@kuling.UUCP
       S-752 63  Uppsala   Phone: Int. +46 - 18 46 31 54
           SWEDEN                 Nat. 018 - 46 31 54

shebs@utah-cs.UUCP (Stanley Shebs) (03/06/86)

In article <1840@hammer.UUCP> patcl@hammer.UUCP (Pat Clancy) writes:
>In article <1142@mit-eddie.UUCP> barmar@eddie.UUCP (Barry Margolin) writes:
>>>There does not seem to be any special form which could be
>>>used to implement such a macro [defun].
>>
>>It can expand into
>>	(setf (symbol-value ...
>
>Unfortunately, setf is itself a macro. So what is the final real
>form that that defun expands to?

Common Lisp doesn't specify it.  Remember, Common Lisp is "just" an
interchange standard - Steele's book does *not* describe any particular
implementation!  So in the case of defun, the implementation is free
to expand it into whatever implementation-dependent stuff it feels like
producing.  Pretty entertaining, no?  There *is* one suggested restriction
(the note on p.58), that implementations not expand macros into
implementation-dependent special forms.  Implementation-dependent
*functions* are OK tho, and in fact that's what you have to use.
(setf (symbol-function ...)) eventually expands into %set-symbol-function
or some such.  In the case of our Common Lisp (PCLS) which is built
on top of PSL, defun eventually turns into a call on the PSL function
PutD.

Almost all the setf functions are this way.  You will find GET, but not
PUT - that's because half the existing Lisp implementations use the argument
order (put symbol indicator value), and the other half do it as (put symbol
value indicator).  Common Lisp gets some additional compatibility by
not specifying it, and saying that "GET is setfable".

All this brings up an important question: what is the minimum set of
Common Lisp constructs needed to implement the rest of Common Lisp?
We've worked on this question, and decided it's very hard.  As the
above examples show, the absence of functions like PUT means that
you must have SETF of GET in the Common Lisp "kernel", which means
that you have to have SETF, which probably means that DEFINE-SETF-METHOD
has to be in this kernel, ad infinitum...  It's not easy to define
*any* subsets of Common Lisp!

>Pat Clancy
							stan shebs

barmar@mit-eddie.UUCP (03/06/86)

In article <1840@hammer.UUCP> patcl@hammer.UUCP (Pat Clancy) writes:
>Unfortunately, setf is itself a macro. So what is the final real
>form that that defun expands to? I've gotten several mail responses
>but none that appear to be correct, and I do not have access to a
>CL implementation.

It can expand into a call to an implementation-dependent function, as is
the case for most setf expansions.  For example, on a Symbolics 3600 it
might expand into a call to FSET, e.g.
	(defun foo ...)
  ==>	(setf (symbol-function foo) #'(lambda ...))
  ==>	(fset 'foo #'(lambda ...))

This is greatly simplified, as there might have to be other forms in the
expansion to deal with declarations, documentation, etc.
-- 
    Barry Margolin
    ARPA: barmar@MIT-Multics
    UUCP: ..!genrad!mit-eddie!barmar