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

Semantic question about closures

    Date: 1985 May 14 15:54:28 PST (=GMT-8hr)
    From: Robert Elton Maas <REM at IMSSS.SU.EDU>
    Re:   Semantic question about closures

    When a closure is made, with some variable encapsulated in the
    closure, is that variable accessible only with the exact function
    that was closed (...), or is that variable accessible also from any
    function called by the closure...

The value is not visible.  This is exactly what lexical scoping means.
A variable DOES NOT EXIST outside of code textually enclosed by the
binding form.  You shouldn't worry about closures when trying to
understand lexical variables--- closures are just an implementation

I think that you are confusing yourself by talking about "free
variables"; this is not a very useful concept in Common Lisp.  Instead
you should think in terms of special variables and lexical variables.
Special variables and lexical variables have very different semantics,
and the language has possibly done a disservice by making them appear
to be similar.

The operation of "binding" a lexical variable is quite unlike that of
binding a special.  Binding a lexical variable actually creates a new
"variable" which is then associated with that name within the lexical
scope.  By a "variable", I mean a nameless memory location which can be
used for storing a value.  The fact that some variables can be stack
allocated is unimportant for understanding what a piece of code does.

The reason that the called function in your example cannot see the
original variable is that its variable is totally different from the
one in the calling function, although it does happen to have the same
name.  The fact that it has the same name is totally irrelevant,
since the association of name with variable does not exist outside of
the lexical context in which the variable was created.

It is important to draw the distinction between variables and names.
This may be difficult for people whose primary experience is with
old-fashioned shallow/special-binding lisps, since in these
implementations there is a one-to-one correspondence between variables
(value cells) and names (symbols).

   (thus somehow a copy of the gut function must be
    made such that it internally references that closure-cell directly
    rather than how the gut function in vacuuo would reference that
    named variable)

Your parenthetical inference is incorrect, however.  There is no "gut
function" to copy.  The compiler must determine at compile time
whether it is going to close over a variable.  If it decides to close
over variables, it compiles code to access closure variables.  Lexical
variable references do not exist "in vacuo".  A lexical variable is
created when it is bound.

    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, ...

Special variables are not closed over.  The meaning of a special
variable reference is "give me the most recent binding in my current
dynamic context."  This is incompatible with the notion of closing
over variables.