Subject: Re: Request for help constructing a simple macro
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 02 Mar 2007 06:38:56 -0600
Newsgroups: comp.lang.lisp
Message-ID: <m6udnUUieYp9h3XYnZ2dnUVZ_uCinZ2d@speakeasy.net>
Dan Bensen  <randomgeek@cyberspace.net> wrote:
+---------------
| Thomas Elam wrote:
| > I am not sure that a symbol is really just an object though. Is it?
| 
| Yes, definitely.  CLtL2 ch10:
| "A Lisp symbol is a data object that has three user-visible components:
|      * The property list
|      * The print name
|      * The package cell"
+---------------

Please don't use CLtL2 when trying to give definitive references;
it's not normative. The CLHS lists *five* standard attributes for
symbols:

    http://alu.org/HyperSpec/Body/syscla_symbol.html
    System Class SYMBOL 
    ...
    Symbols have the following attributes. For historical reasons,
    these are sometimes referred to as cells, although the actual
    internal representation of symbols and their attributes is
    implementation-dependent. 

    Name
      ...
    Package
      ...
    Property list
      ...
    Value
      ...
    Function
      ...

+---------------
| > Isn't an unbound symbol just a `symbol object'? (Does an unbound
| > symbol take up space? Or is it just the same as an inexistent symbol?)
| 
| It takes up space.  You can create an unbound symbol (object) with the 
| function GENSYM.  It will have a print name (a member variable), but you 
| won't be able to use it (no binding).
+---------------

Sure you can use it, just not for its "value"! You can use it for
its identity, e.g., as an EOF marker for READ, or a sentinal in a
search, or lots of other things. The CLHS (same URL as above) says
as much:

    Symbols are used for their object identity to name various
    entities in Common Lisp, including (but not limited to)
    linguistic entities such as variables and functions.

And if you SETF its SYMBOL-VALUE (see below), then you can even
use it as a variable! [Though be careful not to lose the last
reference to it, since unlike an interned symbol you can't get 
it back knowing only its name.]

+---------------
| > What I would like to be very clear about is where the name of
| > the function is stored (isn't it in the symbol having that
| > function as a value?). Likewise I'd like to know whether a
| > variable name is handled in a similar way.
| 
| I'm not sure what you mean.  Every identifier has an entry in a symbol 
| table somewhere.  The entry for a nonlexical name points to the symbol 
| that was created for that name instead of directly to the data.  The 
| symbol has the name stored in its print-name slot.  Someone else should
| explain how the symbol keeps track of its variables and functions.
| The plist must have pointers to them, but I don't know the details.
+---------------

While the plist *was* used for such things in some ancient Lisps, in
ANSI CL the plist is no longer used for "value" and "function"; they
now have their own dedicated (virtual) slots, accessed with SYMBOL-VALUE
and SYMBOL-FUNCTION, respectively.

I say "virtual" slots, since in some implementations either or both
of "value" and "function" might not be stored in structure-like slots
per se. E.g., in CMUCL there is no "function slot" per se in the symbol
object; instead, SYMBOL-FUNCTION refers to an internal "INFO" table
(that also contains lots of other stuff, such as documentation).
Similarly, some CLs with threads don't store the values of global
variables directly in the symbols, but in per-thread structures
accessed indirectly through the symbols, e.g., using a "global
variable index" slot in the symbol, say.

Still, unless one is a maintainer of the internals of some specific
implementation, one seldom needs to care whether the "slots" of a
symbol are "real" or not. For everyday purposes, one might as well
say a symbol "has" value & function "slots" [as well as name, package,
and plist "slots"], since the standard accessors provide the appearance
that they do.


-Rob

p.s. Another aspect of Elam's question wasn't properly answered:
+---------------
| > What I would like to be very clear about is where the name of
| > the function is stored (isn't it in the symbol having that
| > function as a value?).
+---------------

It *might* be, but not all functions have names [think of a LAMBDA].
That is, there is a mapping from "name x lexical_environment --> function",
if such a function exists, but there is no guarantee that there is
a mapping from "function --> name".

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