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__

Relevant Pages

  • Re: C++ vs C
    ... Created functions carry an environment with them which the ... <end quote> ... free the environment until you are certain that no created closure ... Parametric polymorphism combines the advantages of strong typing, ...
  • Re: I thought this was the one that worked?
    ... A "closure" is an expression that can ... In ruby this environment always exists for a block regardless of the ... presence of free variables, since the declaration of the variables ... that are free in the block can be added after the creation ...
  • Re: I thought this was the one that worked?
    ... > It's a closure because it carries the context of its creation with it. ... closure is around an *environment*, ... lambda{baz} ... it didn't exist when the lambda was ...
  • Re: "returning a closure"
    ... there is just the idea that the closure is still ... closed in the environment where it was created: it's free variables ... opposed to the lexical scope that we mostly use nowadays. ...
  • Re: Implementing Closures
    ... | into a heap-allocated closure. ... | all variables that can be destructively updated, so activation records ... It allows the closure's environment to be ... | that you must heap allocate imperative variables). ...