Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Closure on Closures (beta)

by adrianh (Chancellor)
on Jul 08, 2003 at 00:13 UTC ( #272155=note: print w/ replies, xml ) Need Help??


in reply to Re: Re^2: Closure on Closures (beta)
in thread Closure on Closures

I think the "normal CS definition of a closure" is a pretty elusive beast; mythical, maybe.

The The Free On-line Dictionary of Computing's

a closure is a data structure that holds an expression and an environment of variable bindings in which that expression is to be evaluated.

works for me.

A closure is code that retains a binding to a free variable even after the variable has gone out of scope.

Close enough :-)

I suggested that the closure is actually created when the sub is first entered via the goto.

So is this a closure?:

{ my $foo = 99; CODE: print ++$foo, "\n"; } goto CODE;

I don't think the above, or your original example, meet your or my definitions of a closure.

The variable bindings are not retained - because we don't get 100, 101, etc. That binding is an essential part of what a closure is all about.

Really, it's very similar to broquaint 's "named closure" example; only, the inner block is not a subroutine

I don't think it is because "named" closures retain the variable bindings of their scope they were declared in.

I do agree with you and broquaint that the Camel's definition is off. Closures and naming are separate concepts. It seems to be one of those things that Perl defines differently from the rest of the world :-) Why should a closure suddenly become something else if its assigned to a symbol table entry.


Comment on Re^4: Closure on Closures (beta)
Download Code
Re: Re^4: Closure on Closures (beta)
by sauoq (Abbot) on Jul 08, 2003 at 00:26 UTC
    So is this a closure?

    Uhm, yeah... at least to the same extent that my example was.

    The variable bindings are not retained - because we don't get 100, 101, etc.

    The binding are only retained after the first entry via the goto, at which point, the variable does increment with each successive jump back to that code.

    It behaves strangely. After the first jump into the code, it begins acting like a closure; the variable binding is retained. It isn't useful, in part because there is no easy way to initialize the variable first. Perl seems to initialize it to undef for us. I'm not even sure that this is the behavior I'd like to see. I figure it should either maintain the binding and act just like a named closure, or it should emit an error/warning. As it is, it works silently under strict checking and with warnings on.

    -sauoq
    "My two cents aren't worth a dime.";
    
      The binding are only retained after the first entry via the goto, at which point, the variable does increment with each successive jump back to that code.

      There is a bit of storage in there somewhere - but the original variable binding isn't. That's what I was trying to say.

        There is a bit of storage in there somewhere - but the original variable binding isn't.

        I agree, it's not the original¹ That's why I said in my original reply to broquaint, "It makes sense, however, to think of it as a closure that is created on the first entry into the bare block via the goto" (original emphasis).

        That's what I was trying to say.

        You're kidding me! You mean we've been saying the same thing this whole time? ;-)

        Actually, PadWalker suggests it is the original storage space but that it gets reinitialized to undef.

        -sauoq
        "My two cents aren't worth a dime.";
        

      Being able to run code more than once using the same variable isn't enough to be "a closure".

      a closure is a data structure that holds an expression and an environment of variable bindings in which that expression is to be evaluated.

      There might be a data structure involved in the not-first iterations of the

      { my $foo = 99; CODE: print ++$foo, "\n"; } goto CODE;

      example. But such a data structure is not evident and you can't call the example "a closure" (based on that definition and also IMHO). You could postulate that the example hints at a closure being use behind the scenes.

      But I suspect that you might be hard pressed to find one data structure that contains the 'print' "expression" and the "environment" containing the second(?) instance of $foo or you'd have to define your one "data structure" as something that contains way more stuff than that.

      The "named closure" examples are an edge case for me. I don't particularly mind people calling them a closure but I'm more likely to restrict my use of that word because I think a narrower definition of that word is usually more useful.

      I call the "named closure" case "static (or 'state') variables that happen to be implemented using a closure". And it turns out that the closure in that case is stored in the symbol table and when that storing happens is actually important.

      for( 2,3,5,7 ) { my $x= my $y= $_; sub pow { return $y *= $x; } } print pow(), $/ for 1..3; __END__ 4 8 16

      Unfortunately, Perl isn't (currently) smart enough to warn about that case, but it will warn about this equivalent:

      sub wow { my $x= my $y= shift @_; sub pow { return $y *= $x; } } wow($_) for 2, 3, 5; print pow(), $/ for 1..3; __END__ Variable "$y" will not stay shared at - line 4. Variable "$x" will not stay shared at - line 4. 4 8 16

      A closure is generated when that code is compiled. And that closure is stored in *{$main::{pow}}{CODE}. When new instances of $x and $y are created, no new closures are created.

      So I usually only talk about closures when I'm talking about the reference to the code (the reference that also contains references to some closed-over variable instances). If the reference isn't evident, then "closure" or not usually is an implementation detail.

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://272155]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-07-22 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (113 votes), past polls