Re: A question (confusion) about closure

Samik <samikc@xxxxxxxxx> writes:


I have a confusion regarding closure and its definition. As per Paul

When a function refers to a variable defined outside it, it’s called a
free variable. A function that refers to free lexical variable is
called a closure.

However as per CLHS a closure or lexical closure is:

lexical closure n. a function that, when invoked on arguments,
executes the body of a lambda
expression in the lexical environment that was captured at the time of
the creation of the lexical closure, augmented by bindings of the
function's parameters to the corresponding arguments.

lexical environment n. that part of the environment that contains
bindings whose names have lexical scope. A lexical environment
contains, among other things: ordinary bindings of variable names to
values, lexically established bindings of function names to functions,
macros, symbol macros, blocks, tags, and local declarations (see

Can any pro lisper kindly help me?

;; John's example:
(let ((val const)) ; local, lexical variable
(defun func (...) ; lexical closure
(setf val ...) ; val is free in func

(defvar *val* 'value) ; global, dynamic variable
(defun gunc (...) ; a closure too!
(setf *val* ...) ; *val* is free in gunc.

Now, technically, in Common Lisp a function whose free variables are
all global, dynamic variables wouldn't be called a closure, since
these variable are not really enclosed in an environment with the
function: they just lie in the global environment, along these
functions, and since it's considered a closed universe, there's no
outside to be considered enclosed. When everybody is something,
there's no point in naming it.

Even a function without free variables could be considered a closure,
if the implementation encloses it in an empty environment, just for
the sake of homogeneity with the other closures. But CL leaves the
implementations free to optimize this case out.

__Pascal Bourguignon__