[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

lexical closures

Closures ...

   Date: 1985 May 14 15:54:28 PST (=GMT-8hr)
   From: Robert Elton Maas <REM@imsss.su.edu>

   I.e. if function A binds special X to some initial value, makes a
   closure of B with free variable X referring to A's fluid-binding of X,
   then lambda-binds X another level deep to a new value, then calls
   B-closure, obviously references to X within B-closure will get the
   first binding of X,

It turns out that the B-closure will refer to the second (deeper) value of
X, but not because of anything involving closures.  Lexical closures in
Common Lisp do not include special variables [see pp 39-40].  If X is
a special variable, the free X in B will always refer to the most recent
binding of X; lexical closures don't change this.

One reason for the confusion may be that other dialects of Lisp, such as
ZetaLisp and Franz, do provide closures over special variables that work
more or less in the way you describe.

   Date: 15 May 1985 0213-PDT
   From: Rem@imsss
   Subject: Another question about lexical closures

   There's a little ambiguity about when two bindings are "the same" and when
   they are "different" bindings. If you bind exactly the same lexical variable
   (in the same function or other executable form) to exactly the same value
   (the values are EQ), at two different times, are the two bindings you get
   considered to be the same or different?

If you call the same function twice, you get two different bindings (p 36:
"a new binding is established on every invocation").  The text on pages
87-89 seems clear on what this means for closures, particularly the example
in the middle of page 88 (think of each LET as a function call).  Now if
TWO-FUNS were called twice, there would be two different bindings of X, and
closures involving different bindings must be different if the bound values
could be changed by SETQ.  Since the second of them includes a SETQ, the
closures returned in the list must be different each time.  Now if it weren't
for the SETQ, each call to TWO-FUNS would still normally return different
closures because X might have a different value each time.  If two calls
happened to use the same EQ value twice, I don't see anything to guarantee
that the closures would be different (after all, they would be 
"behaviorially identical with respect to invocation"), but would any
implementation notice that they could be the same?

So I think the book is fairly clear, although you might have to reread it
a few times if you aren't already familiar with the stuff involved.