Sunday, October 01, 2006

Closure on closures

In a previous post, closures were discussed without much being said. That was over a year ago, since then I've come across a few posts that clarify.

Here's a description of the concept by comparison to objects:
Objects are data with methods attached, closures are functions with data attached.

(Sorry, I couldn't find the source for this quote, so this is a paraphrase from memory.)

An explanation of where the term closure comes from by Max Hailpern, co-author of Concrete Abstractions - and why they didn't use it in their book:
The reason [it wasn't included] is that "closure" only makes sense in a particular historical context, where procedures had previously been left "open", that is with free variables not associated with any particular binding. This was the case in various pre-Scheme Lisps, and lead to what was known as the "funarg problem," short for "functional argument", though it also was manifested when procedures were used in other first-class ways than as arguments, for example, as return values, where it was confusingly called the "upward funarg problem" (by contrast to the "downward funarg problem," where the arg was genuinely an arg). The "funarg problem" is what logicians had been calling "capture of free variables," which occurs in the lambda calculus if you do plain substitution, without renaming, in place of proper beta-reduction.

So anyhow, an evolutionary milestone in the development of the Lisp family of languages was the realizations that procedures should be "closed", that is, converted into a form where all the variables are bound rather than free. (The way this is normally done, as others have written in this thread, is by associating the procedure text, which still has free variables, with a binding environment that provides the bindings.)

Because this was such a big deal in terms of the languages' evolution, Lisp hackers took to using the word "closure" rather than just "procedure" to emphasize that they were talking about this great new lexically scoped kind of procedure.

As with so many words, it stuck, and people continued using it long after the distinction it was making ceased to be relevant. (When was the last time you saw a procedure that hadn't been closed?)

(Link to source.)

For a mathematical perspective see Ted Dziuba's post on closures as a recurrence relation.