From: Franz Kafka
Subject: &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions
Date: 
Message-ID: <b3b6b110.0304151149.70e175a0@posting.google.com>
I have been programming in Lisp for a while and found a few operators
which have not been discussed in any Lisp book I've read. I am just
curious
about what these would be used for. 

&whole as a keyword in defmacro? How does it differ from &body? What
would
&whole be used for?

&environment -- In ANSI CL what is an environment? Why to some
versions of eval take an environment as a second arg, and others
don't?

defsetf, define-setf-method, define-setf-expansion, (defun (setf var)
...
I have read about them in On Lisp, and in Peter Novig's Paradigms
book. They have never been fully explained. What is possible with setf
methods? Could I use setf methods to create a Symbolic Diff.
Package--not that I would do it?

Is there any book that explains the more estoric features of CL; the
features that are even more mysterious than defmacro?

BTW, I am looking for a book about creating a Lisp Compiler in
Hardware. I want one that shows how to actually build the hardware,
and is not only about theroy? Does such a book exist?

From: Paul F. Dietz
Subject: Re: &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions
Date: 
Message-ID: <zn6dnQojWfBFOwGjXTWcpg@dls.net>
Franz Kafka wrote:

> &environment -- In ANSI CL what is an environment? Why to some
> versions of eval take an environment as a second arg, and others
> don't?

The primary use of &environment in portable code is to capture
MACROLET and SYMBOL-MACROLET bindings.  This enables a macro to
expand other macros in the current lexical environment via
MACROEXPAND or MACROEXPAND-1.  By encoding information in
the macro expansions you can use this to pass information
down at macroexpansion time (I've used this trick to good effect).

	Paul
From: Michael Hudson
Subject: Re: &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions
Date: 
Message-ID: <7h3brz7sf14.fsf@pc150.maths.bris.ac.uk>
·································@hotmail.com (Franz Kafka) writes:

> defsetf, define-setf-method, define-setf-expansion, (defun (setf var)
> ...
> I have read about them in On Lisp, and in Peter Novig's Paradigms
> book. They have never been fully explained.

I think you need to hit the HyperSpec for complete explanations of all
these.  Some of them are a bit fiddly.

> What is possible with setf methods? 

Well, writing 

(setf (blah blat) bling)

where blah, blat and bling are up to you.  If CL didn't have
hashtables built in and you wanted to implement them, you'd probably
write

(defun (setf gethash) (table key)
    ...)

at some point (I hope I have the syntax right...).

> Could I use setf methods to create a Symbolic Diff.  Package--not
> that I would do it?

Um, probably, in some suitably unhelpful twisted sense.  I'm not sure
I understand the question well enough to answer helpfully.  I kind of
doubt it.

> Is there any book that explains the more estoric features of CL; the
> features that are even more mysterious than defmacro?

You do know about the HyperSpec, don't you?

Cheers,
M.

-- 
  The potential of having part of my car's drive-train functioning
  as a pr0n server is both intellectually fascinating and 
  metaphysically disturbing.                             -- Steed, asr
From: Peter Seibel
Subject: Re: &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions
Date: 
Message-ID: <m38yubfps2.fsf@javamonkey.com>
·································@hotmail.com (Franz Kafka) writes:

> &environment -- In ANSI CL what is an environment? Why to some
> versions of eval take an environment as a second arg, and others
> don't?

The environment object is an opaque, implementation-dependent blob of
data about the lexical environment. The only thing you can do with it
is pass it to other functions (e.g. CONSTANTP) that take an
environment object.

If you look in CLTL2 you'll find a set of functions for getting actual
information out of an environment object but, as I understand it, the
X3J13 committee couldn't work out the details of that API to their
satisfaction so left them out of the final standard.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Pekka P. Pirinen
Subject: Re: &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions
Date: 
Message-ID: <uistdid4n.fsf@globalgraphics.com>
Peter Seibel <·····@javamonkey.com> writes:
> The environment object is an opaque, implementation-dependent blob of
> data about the lexical environment.  [...]
>
> If you look in CLTL2 you'll find a set of functions for getting actual
> information out of an environment object [...]

Let's not limit ourselves to lexical here, it's the environment.  The
CLtL2 functions access all environment information, including
proclamations, i.e., global info.  While global definitions and
declarations are usually not physically stored in the environment
object, a compiler might maintain a separate environment (ANS 3.2.1),
where it stores info about the code it's compiling, so the environment
does matter even for non-lexical bindings.

>> Why to some versions of eval take an environment as a second arg,
>> and others don't?

The standard doesn't specify an environment argument, because it's
hard to specify in a consistent and easily-implementable way, and
rarely, if ever, needed.  Nevertheless, implementations have
environment objects, which typically share structure with how the
interpreter stores this information, so it might be easy to write an
interface that plugs one into the other, as long as you don't have to
worry about the exact specification.
-- 
Pekka P. Pirinen
Pick your enemies carefully.  They're harder to get rid of than friends.
From: Henrik Motakef
Subject: Re: &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions
Date: 
Message-ID: <87brz78pkw.fsf@interim.henrik-motakef.de>
·································@hotmail.com (Franz Kafka) writes:

> I have been programming in Lisp for a while and found a few operators
> which have not been discussed in any Lisp book I've read. I am just
> curious
> about what these would be used for. 
>
> &whole as a keyword in defmacro? How does it differ from &body? What
> would
> &whole be used for?

| * (defmacro foo (&whole whole &body forms)
|     (format t "WHOLE is: ~s, BODY is: ~s" whole forms)
|     `(progn ,@forms))
| FOO
| * (foo (1+ 1))
| WHOLE is: (FOO (1+ 1)), BODY is: ((1+ 1))
| 2

The &whole argument contains the whole macro-calling form, including
the macro name.


> &environment -- In ANSI CL what is an environment? Why to some
> versions of eval take an environment as a second arg, and others
> don't?

It represents the current lexical environment, i.e. roughly the set of
non-special variable bindings at the current point in the code. It is
not something you should usually bother about, and implementations are
more or less free to use whatever they want to represent it, for
example nothing useful at all.

> defsetf, define-setf-method, define-setf-expansion, (defun (setf var)
> ...
> I have read about them in On Lisp, and in Peter Novig's Paradigms
> book. They have never been fully explained. What is possible with setf
> methods? Could I use setf methods to create a Symbolic Diff.
> Package--not that I would do it?

I'm not sure what you mean with "Symbolic Diff. Package" and how SETF
and friends are related to it, but however.

Most languages have some conventions on how stuff-accessing operators
relate to the corresponding stuff-modifying operators. For example, in
Java you may have two methods `Stuff getStuff()' and 
'void setStuff(Stuff newStuff)'.

In Lisp, this is done with SETF. Basically, (setf form value) tells
Lisp that you want subsequent calls to `form' to return `value' from
now on, for example `(setf (car x) :foo)' means `Make it so that 
(car x) will return :foo'. In a way, SETF lets you pretend that some
forms are variables and that you can simply change their value, even
if they not, actually. These pseudo-variables (like `(car x)') are
called "places" or "generalized references".

One nice feature of this is that you can not only use SETF on places,
but some other convenience operators like SHIFTF, ROTATEF, PUSHNEW
etc.

Of course, sooner or later SETF has to know how to set some actual
value so that the promise holds true. For simple stuff like CAR, this
is already defined by the standard, for your own kinds of places
(like, you want to allow `(setf (stuff x) :new-stuff)' make your
`(stuff foo)' function return :new-stuff, but only you understand the
semantics of #'stuff and what SETF is supposed to do in that case),
you have it what should be done, with one of the functions and macros
you listed above (there are more, e.g. the :accessor slot option in
DEFCLASS). Which one you use basically depends on how much
fine-grained control you need.

Regards
Henrik
From: Barry Margolin
Subject: Re: &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions
Date: 
Message-ID: <kZ_ma.28$MT3.801@paloalto-snr1.gtei.net>
In article <··············@interim.henrik-motakef.de>,
Henrik Motakef  <··············@web.de> wrote:
>·································@hotmail.com (Franz Kafka) writes:
>
>> I have been programming in Lisp for a while and found a few operators
>> which have not been discussed in any Lisp book I've read. I am just
>> curious
>> about what these would be used for. 
>>
>> &whole as a keyword in defmacro? How does it differ from &body? What
>> would
>> &whole be used for?
>
>| * (defmacro foo (&whole whole &body forms)
>|     (format t "WHOLE is: ~s, BODY is: ~s" whole forms)
>|     `(progn ,@forms))
>| FOO
>| * (foo (1+ 1))
>| WHOLE is: (FOO (1+ 1)), BODY is: ((1+ 1))
>| 2
>
>The &whole argument contains the whole macro-calling form, including
>the macro name.

And to answer the third question about what it's used for:

1) You could have the same macro function on different macro names, and it
   could look at the name in the calling form to decide what to do:

(defmacro funny-macro (&whole whole ...)
  (ecase (car whole)
    (foo ...)
    (bar ...)
    ...))

(setf (macro-function 'foo) (macro-function 'funny-macro))
(setf (macro-function 'bar) (macro-function 'funny-macro))
...

2) You can use it to side-effect the calling form.  This used to be popular
   for "memoizing" macros in interpreted Lisps -- a macro invocation only
   had to be expanded the first time it was encountered.  Maclisp had a
   helpful function DISPLACE that was useful along with this:

(defun displace (new-cons old-cons)
  (setf (car new-cons) (car old-cons))
  (setf (cdr new-cons) (cdr old-cons)))

(defmacro another-macro (&whole whole ...)
  (displace whole
            `(<typical macro expansion code>)))

In the original Maclisp macro system, a macro was just a function that got
one argument, the whole macro-calling form.  All the &keyword stuff was
implemented by a higher-level macro.  &WHOLE was included as a way for
the higher-level macro to provide access to the underlying feature.

-- 
Barry Margolin, ··············@level3.com
Genuity Managed Services, a Level(3) Company, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Gabe Garza
Subject: Re: &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions
Date: 
Message-ID: <8765pfferl.fsf@ix.netcom.com>
·································@hotmail.com (Franz Kafka) writes:

> &whole as a keyword in defmacro? How does it differ from &body? What
> would
> &whole be used for?

In addition to the reasons Barry Margolin and others gave, &whole is
useful in compiler macros (see DEFINE-COMPILER-MACRO in the spec) when
you want to punt and not change the form at all...  See 

http://groups.google.com/groups?selm=87he9o5j4i.fsf%40ix.netcom.com&oe=UTF-8&output=gplain

For an example.

Gabe Garza