Re: When do named subs bind to their variables? (Re: Questionable scope of state variables ([perl #113930] Lexical subs))

2012-07-09 Thread Father Chrysostomos via RT
On Sat Jul 07 18:35:03 2012, tom christiansen wrote:
 Father Chrysostomos via RT perlbug-comm...@perl.org wrote
on Sat, 07 Jul 2012 17:44:46 PDT:
 
  I’m forwarding this to the Perl 6 language list, so see if I can
 find
  an answer there.
 
 I do have an answer from Damian, which I will enclose below, and a
 Rakudo result for you.
 
  [This conversation is about how lexical subs should be implemented
 in
  Perl 5.  What Perl 6 does may help in determining how to iron out
 the
  edge cases.]
 
 [...]
 
  This question might be more appropriate:  In this example, which @a
  does the bar subroutine see (in Perl 6)?
 
  sub foo {
  my @a = (1,2,3);
  my sub bar { say @a };
  @a := [4,5,6];
  bar();
  }
 
 The answer to your immediate question is that if you call foo(),
 it prints out 456 under Rakudo.

Thank you.  So the bar sub seems to be closing over the name @a (the
container/variable slot/pad entry/whatever), rather than the actual
array itself.

Since I don’t have it installed, could you tell me what this does?

sub foo {
my @a = (1,2,3);
my sub bar { say @a };
bar();
@a := [4,5,6];
bar();
}
foo();

And this?

sub foo {
my @a = (1,2,3);
bar();
@a := [4,5,6];
bar();
my sub bar { say @a };
}
foo();

And this?

sub foo {
my @a = (1,2,3);
my sub bar { say @a };
my $bar = bar;
$bar(); # is this syntax right?
@a := [4,5,6];
$bar();
}
foo();

 
 Following is Damian's answer to my question, shared with permission.
 
 --tom
 
 From:  Damian Conway dam...@conway.org
 To:Tom Christiansen tchr...@perl.com
 CC:Larry Wall la...@wall.org
 Date:  Sun, 08 Jul 2012 07:17:19 +1000
 Delivery-Date: Sat, 07 Jul 2012 15:19:09
 Subject:   Re: my subs and state vars
 In-Reply-To:   22255.1341691089@chthon
 
 X-Spam-Status: No, score=-102.6 required=4.5
 tests=BAYES_00,RCVD_IN_DNSWL_LOW,
USER_IN_WHITELIST autolearn=ham version=3.3.0
 
 X-Google-Sender-Auth: UHLwfgo2kyvv2prdl6qJm-RfLF8
 Content-Type:  text/plain; charset=ISO-8859-1
 
  It looks like perl5 may be close to having my subs, but a puzzle
  has emerged about how in some circumstances to treat state
  variables  within those.  [I'm pretty sure that perl6 has
 thought
  this through thoroughly, but [I] am personally unfamiliar with
 the
  outcome of said contemplations.]
 
  I bet you aren't, though.  Any ideas or clues?
 
 The right things to do (and what Rakudo actually does) is to treat
 lexical subs as lexically scoped *instances* of the specified sub
 within the current surrounding block.
 
 That is: a lexical sub is like a my var, in that you get a new
 one
 each time the surrounding block is executed. Rather than like an
 our
 variable, where you get a new lexically scoped alias to the same
 package
 scoped variable.
 
 By that reasoning, state vars inside a my sub must belong to each
 instance of the sub, just as state vars inside anonymous subs
 belong to
 each instance of the anonymous sub.
 
 Another way of thinking about what Perl 6 does is that:
 
 my sub foo { whatever() }
 
 is just syntactic sugar for:
 
 my foo := sub { whatever() }

Does that mean I cannot call it before it is declared?

 That is: create a lexically scoped Code object and alias it at
 run-time
 to an anonymous subroutine. So the rules for state variables
 inside
 lexical subs *must* be the same as the rules for state variables
 inside
 anonymous subs, since they're actually just two ways of creating
 the
 same thing.

I see.

 
 With this approach, in Perl 6 it's easy to specify exactly what
 you want:
 
 sub recount_from ($n) {
 
 my sub counter {
 state $count = $n;   # Each instance of counter has
 its own count
 say $count--;
 die if $count == 0;
 }
 
 while prompt recount $n  {
 counter;
 }
 }
 
 vs:
 
 sub first_count_down_from ($n) {
 
 state $count = $n;   # All instances of counter share
 a common count
 
 my sub counter {
 say $count--;
 die if $count == 0;
 }
 
 while prompt first count $n  {
 counter;
 }
 }
 
 Feel free to forward the above to anyone who might find it useful.

What I am really trying to find out is when the subroutine is actually
cloned, and whether there can be multiple clones within a single call of
the enclosing sub.

-- 

Father Chrysostomos



When do named subs bind to their variables? (Re: Questionable scope of state variables ([perl #113930] Lexical subs))

2012-07-09 Thread Father Chrysostomos via RT
I’m forwarding this to the Perl 6 language list, so see if I can find an
answer there.

[This conversation is about how lexical subs should be implemented in
Perl 5.  What Perl 6 does may help in determining how to iron out the
edge cases.]

On Sat Jul 07 13:23:17 2012, sprout wrote:
 On Sat Jul 07 12:53:32 2012, tom christiansen wrote:
  Are you worried about this situation?
  
  sub outer {
  my @subs;
  for $i (1 .. 10) {
  my sub inner {
  state $x = rand();
  return [ $i = $x ];
  }
  push @subs, \inner;
  }
  return @subs;
  }
  
  The important question here is whether all those $x variables have
  the same random number, or whether they have different ones.
  
  And no, I don't know what the right answer is.  I do agree that
  is the right question, though. :)
 
 I think I have the right answer now.  If we document that only
 *anonymous* subroutines get their own copies of state variables when
 cloned, then my subs (personal subs? idiotic subs?) share them.
 
 In the example you gave, those $x variables would all have the same
 random number.
 
 If whether \inner will clone the sub or not is supposed to be a matter
 of optimisation, that’s the only way it can work.
...
  And later on there is this, which is interesting but not completely
  revealing, since it deals with a my not a state:
  
  Lexical names do not share this problem, since the symbol goes out
  of
  scope synchronously with its usage. Unlike global subs, they do
  not need a
  compile-time binding, but like global subs, they perform a binding
  to the
  lexical symbol at clone time (again, conceptually at the entry to
  the
  outer lexical scope, but possibly deferred.)
  
  sub foo {
  # conceptual cloning happens to both blocks below
  my $x = 1;
  my sub bar { print $x } # already conceptually
  cloned, but can be lazily deferred
  my baz := { bar(); print $x }; # block is cloned
  immediately, forcing cloning of bar
  my $code = bar;# this would also force
  bar to be cloned
  return baz;
  }
 
 That is interesting, since it is very similar to what I came up with on
 my own.
 
 If a sub is conceptually cloned when the block enters, does that mean
 that my $code = bar (\bar in p5) twice in a row should produce the
 same value?
 
 How does Perl 6 deal with my subs in for loops?

This question might be more appropriate:  In this example, which @a does
the bar subroutine see (in Perl 6)?

sub foo {
my @a = (1,2,3);
my sub bar { say @a };
@a := [4,5,6];
bar();
}

-- 

Father Chrysostomos



Re: [perl #113930] Lexical subs

2012-07-08 Thread Moritz Lenz
On 07/08/2012 09:57 PM, Father Chrysostomos via RT wrote:
 my $x;
 my sub f { say $x }
 for 1..10 - $x { f(); }

It prints

Any()
Any()
Any()
Any()
Any()
Any()
Any()
Any()
Any()
Any()

(because Any is the default value in uninitialized variables).

As an aside, you can run short Perl 6 scripts on IRC (on irc.perl.org
and irc.freenode.org) with something like

/msg p6eval p6: my $x; sub sub f { say $x }; for 1..10 - $x { f() }

This runs it through both rakudo and niecza.

If you want, I can also send the bot into #p5p.

Cheers,
Moritz


Re: [perl #113930] Lexical subs

2012-07-08 Thread Damian Conway
 But by using the term ‘variable’, which is ambiguous, you are not
 answering my question! :-)

Sorry. I tend to think of *every* variable name as merely being
an alias for some underlying storage mechanism. ;-)

 Does

 my $x;
 for 1..10 - $x {}

 cause the existing name $x to refer temporarily to each of the numbers,
 or is a new $x name created?

A new one is created (each time through the loop).


 What does this do?

 my $x;
 my sub f { say $x }
 for 1..10 - $x { f(); }

It prints 'Any()' ten times (i.e. the equivalent of printing ten Perl 5 undefs).

The two $x's definitely exist at the same time during the loop.
For example, this:

my $x = 'outer x';
my sub f { say $x }

for 1..10 - $x {
print $x, : ;
f();
}

prints:

1: outer x
2: outer x
3: outer x
4: outer x
5: outer x
6: outer x
7: outer x
8: outer x
9: outer x
10: outer x


Damian


Re: When do named subs bind to their variables? (Re: Questionable scope of state variables ([perl #113930] Lexical subs))

2012-07-07 Thread Tom Christiansen
Father Chrysostomos via RT perlbug-comm...@perl.org wrote
   on Sat, 07 Jul 2012 17:44:46 PDT: 

 I’m forwarding this to the Perl 6 language list, so see if I can find
 an answer there.

I do have an answer from Damian, which I will enclose below, and a 
Rakudo result for you.

 [This conversation is about how lexical subs should be implemented in
 Perl 5.  What Perl 6 does may help in determining how to iron out the
 edge cases.]

[...]

 This question might be more appropriate:  In this example, which @a
 does the bar subroutine see (in Perl 6)?

 sub foo {
 my @a = (1,2,3);
 my sub bar { say @a };
 @a := [4,5,6];
 bar();
 }

The answer to your immediate question is that if you call foo(), 
it prints out 456 under Rakudo.

Following is Damian's answer to my question, shared with permission.

--tom

From:  Damian Conway dam...@conway.org
To:Tom Christiansen tchr...@perl.com
CC:Larry Wall la...@wall.org
Date:  Sun, 08 Jul 2012 07:17:19 +1000
Delivery-Date: Sat, 07 Jul 2012 15:19:09
Subject:   Re: my subs and state vars
In-Reply-To:   22255.1341691089@chthon

X-Spam-Status: No, score=-102.6 required=4.5 
tests=BAYES_00,RCVD_IN_DNSWL_LOW,
   USER_IN_WHITELIST autolearn=ham version=3.3.0

X-Google-Sender-Auth: UHLwfgo2kyvv2prdl6qJm-RfLF8
Content-Type:  text/plain; charset=ISO-8859-1

 It looks like perl5 may be close to having my subs, but a puzzle
 has emerged about how in some circumstances to treat state
 variables  within those.  [I'm pretty sure that perl6 has thought
 this through thoroughly, but [I] am personally unfamiliar with the
 outcome of said contemplations.]

 I bet you aren't, though.  Any ideas or clues?

The right things to do (and what Rakudo actually does) is to treat
lexical subs as lexically scoped *instances* of the specified sub
within the current surrounding block.

That is: a lexical sub is like a my var, in that you get a new one
each time the surrounding block is executed. Rather than like an our
variable, where you get a new lexically scoped alias to the same package
scoped variable.

By that reasoning, state vars inside a my sub must belong to each
instance of the sub, just as state vars inside anonymous subs belong to
each instance of the anonymous sub.

Another way of thinking about what Perl 6 does is that:

my sub foo { whatever() }

is just syntactic sugar for:

my foo := sub { whatever() }

That is: create a lexically scoped Code object and alias it at run-time
to an anonymous subroutine. So the rules for state variables inside
lexical subs *must* be the same as the rules for state variables inside
anonymous subs, since they're actually just two ways of creating the
same thing.

With this approach, in Perl 6 it's easy to specify exactly what you want:

sub recount_from ($n) {

my sub counter {
state $count = $n;   # Each instance of counter has its own 
count
say $count--;
die if $count == 0;
}

while prompt recount $n  {
counter;
}
}

vs:

sub first_count_down_from ($n) {

state $count = $n;   # All instances of counter share a common 
count

my sub counter {
say $count--;
die if $count == 0;
}

while prompt first count $n  {
counter;
}
}

Feel free to forward the above to anyone who might find it useful.

Damian



Re: When do named subs bind to their variables? (Re: Questionable scope of state variables ([perl #113930] Lexical subs))

2012-07-07 Thread Tom Christiansen
Father Chrysostomos via RT perlbug-comm...@perl.org wrote
   on Sat, 07 Jul 2012 18:54:15 PDT: 


Thank you.  So the bar sub seems to be closing over the name @a (the
container/variable slot/pad entry/whatever), rather than the actual
array itself.

Since I don't  have it installed, could you tell me what this does?

All three of those say the same thing:

123
456

--tom


Re: When do named subs bind to their variables? (Re: Questionable scope of state variables ([perl #113930] Lexical subs))

2012-07-07 Thread Damian Conway
Father Chrysostomos asked:

 What I am really trying to find out is when the subroutine is actually
 cloned,

Yes. It is supposed to be (or at least must *appear* to be),
and currently is (or appears to be) in Rakudo.


 and whether there can be multiple clones within a single call of
 the enclosing sub.

Yes. For example, a lexical sub might be declared in a loop inside the
enclosing sub, in which case it should produce multiple instances, one
per iteration.

For example, this:

sub outer_sub () {
for (1..3) {
state $call_num = 1;
my sub inner_sub {
state $inner_state = (1..100).pick; # i.e. random number
say [call {$call_num++}] \$inner_state = $inner_state;
}

say \nsub id: , inner_sub.id;
inner_sub();
inner_sub();
}
}

outer_sub();

produces:

sub id: -4628941774842748435
[call 1] $inner_state = 89
[call 2] $inner_state = 89

sub id: -4628941774848253711
[call 3] $inner_state = 16
[call 4] $inner_state = 16

sub id: -4628941774839825925
[call 5] $inner_state = 26
[call 6] $inner_state = 26

under Rakudo


BTW, Both the above yes answers are consistent with (and can be
inferred from) the previous explanation that:

my sub foo { whatever() }

 is just a syntactic convenience for:

my foo := sub { whatever() }

HTH,

Damian


Re: lexical subs

2007-03-09 Thread Juerd Waalboer
Just a short note: please, if this is implemented, make sure that either
Perl 6 conforms to Perl 5 behaviour, or the other way around. 
-- 
korajn salutojn,

  juerd waalboer:  perl hacker  [EMAIL PROTECTED]  http://juerd.nl/sig
  convolution: ict solutions and consultancy [EMAIL PROTECTED]

Ik vertrouw stemcomputers niet.
Zie http://www.wijvertrouwenstemcomputersniet.nl/.


Re: lexical subs

2007-03-09 Thread Juerd Waalboer
Juerd Waalboer skribis 2007-03-09 21:27 (+0100):
 Just a short note: please, if this is implemented, make sure that either
 Perl 6 conforms to Perl 5 behaviour, or the other way around. 

Wanted to CC this list, but by accident replaced the To instead. Now
CC'ing p5p.
-- 
korajn salutojn,

  juerd waalboer:  perl hacker  [EMAIL PROTECTED]  http://juerd.nl/sig
  convolution: ict solutions and consultancy [EMAIL PROTECTED]

Ik vertrouw stemcomputers niet.
Zie http://www.wijvertrouwenstemcomputersniet.nl/.