Michael Bevilacqua-Linn on 15 Mar 2010 19:45:29 -0700


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

Re: closures


Thanks Andrew!

That's something I didn't know.

Are you saying my (half-assed) definition of a first-class procedure is wrong, or just that I've played fast and lose here by implying that you need first class procedures/functions to have closures?

Thanks,
MBL

On Mon, Mar 15, 2010 at 7:32 PM, Andrew Gwozdziewycz <apgwoz@gmail.com> wrote:


On Mon, Mar 15, 2010 at 5:36 PM, Michael Bevilacqua-Linn <michael.bevilacqualinn@gmail.com> wrote:

A first-class function (or procedure) means that there's a facility in the language that allows you to create procedures at execution time, store a reference to them in a variable, basically treat them as you would any other piece of data that your program is working with. 

No. This is wrong. As you can have closures in a compiled language. In a compiled language, closures might be created by doing  lambda lifting, which creates a named function with extra parameters for the variables that used to be free.

In other words, a function such as:

    (define (make-counter x)
         (lambda (n)
              (begin
                  (setf! x (+ x n))
                  x)))

Might compile to the following procedures (illustrative only):

(define (lifted-anonymous-function-1 n FREE_X)
     (begin
          (set-box! FREE_X (+ (unbox FREE_X) n))
          (unbox FREE_X)))

(define (make-counter x)
     (make-closure lifted-anonymous-function-1 (box x)))

Applying the closure would then call the reference to the function, pass the given and the boxed parameters from the closure.
 
On Mon, Mar 15, 2010 at 4:35 PM, <mjd-phillylambda@plover.com> wrote:

> So I'd like to test my understanding with this group...
>
>       int x;
>
>       void foo(int x);
>       void baz(void (*F)(void));
>
>       void callback(void)
>       {
>               foo(x);
>       }
>
>       void bar(void)
>       {
>               baz(&callback);
>       }
>
> C doesn't "support" closures, but the code above has one.

Only in a trivial sense.  C doesn't support nested function scopes, so
in C one can't construct an interesting example of closures or of
their failure in C.  I wanted to construct a C example for you, but
none of my constructions made sense as C.

To not-answer your question:

> The above is a closure, if you could do such a thing in C.

I think the only thing you could usefully do with this question is to
unask it.  Trying to understand closures by studying the non-behavior
of the nonexistent closures of C is pointless.

If you like Perl, I suggest that you read chapter 3 of "Higher-Order
Perl", available for free download at:

       http://hop.perl.plover.com/

I think the explanation there is both clear and rigorous.

If you like some other language that has closures, I suggest you ask
for an example in that language.

If you only like C, I suggest you either forget about closures
(because C doesn't have them) or learn to like some language that does
have them.

> The word closure
> is used here because we create an instance of foo() over which x is "closed".
>
> Do I have it right so far?

No:

* You have not created an instance of foo, or indeed of anything.
 Functions always have static duration in C.  Instances of functions
 are created at compile time, and not afterward.

* The jargon is that the function is closed over the variable, not the
 other way around.

> I would appreciate it if someone could propose a few problems which
> are trivial to solve with closures, but difficult without.

a. Implement an object-oriented programming system.  Closures make
  adequate objects.  This is discussed in detail in _Structure and
  interpretation of Computer Programs_, should you want to know more.

b. Almost anything in _Higher-Order Perl_; see the table of contents.








--
http://www.apgwoz.com


  • Follow-Ups:
  • References:
    • closures
      • From: "Mark M. Hoffman" <mhoffman@lightlink.com>
    • Re: closures
      • From: mjd-phillylambda@plover.com
    • Re: closures
      • From: Michael Bevilacqua-Linn <michael.bevilacqualinn@gmail.com>
    • Re: closures
      • From: Andrew Gwozdziewycz <apgwoz@gmail.com>