Question about declare-top



First of, please excuse my earlier incomplete reply, which fired off by 
accident.

On Mon, 4 Jul 2005, Richard Fateman wrote:

> (defun foo(x)(declare (special x))
>  (bar))
>
> (defun bar() x)  ;; since x is special, refers to the DYNAMIC binding of x

Well, yes. Unfortunately.

However, to keep things sane for the reader, I'd really really recommend
declaring X special in BAR as well. Unless it is globally special, that 
is.

First off, without this the implementation is likely to signal a compile 
time warning for an unbound variable in BAR. Secondly, and more 
significantly it alerts the reader to the magic.

Finally, being able to interpret the X in BAR as referring to the dynamic 
binding requires a rather nasty reading of the spec: 3.1.2.1.1.2 which 
defines dynamic variables doesn't straightforwardly allow this. I'm fairly 
certain that the exhibited style is _technically_ legal, but right now I'm 
unable to piece the evidence from CLHS.

IIRC the story goes something like this: an apparently unbound symbol 
evaluates to the symbol-value of the symbol + special bindings are 
effectively required to be stored as symbol-values => X gets the dynamic 
binding if one exists. This, or something like that, is the reason 
for

   (defun quux () x)
   (setf (symbol-value 'x) 42)
   (quux) => 42

working the way it does without special variables. Eugh.

So, unless there is some fantastic reason to do otherwise (I admit that 
maintaining something the size and age of Maxima probably qualifies as 
this fantastic):

   (defun bar () (declare (special x)) x)

is the way to read a locally special variable.

Sorry for picking at nits.

Cheers,

  -- Nikodemus              Schemer: "Buddha is small, clean, and serious."
                   Lispnik: "Buddha is big, has hairy armpits, and laughs."