Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Perl 6 coroutines (RFC 31)

by MeowChow (Vicar)
on Jul 13, 2001 at 11:58 UTC ( #96318=note: print w/replies, xml ) Need Help??

in reply to Perl 6 coroutines (RFC 31)

That coroutine RFC seems rather sketchy. First of all, it's describing generators, not coroutines. Coroutines are symmetrical; a callee and caller are equal partners in saving and resuming state (unlike the master-slave paradigm suggested by the RFC). My feeble understanding of coroutine implementation is that it is best to create a continuation primitive first, from which coroutines, "green" threads, generators, and exceptions can all be easily derived. This is, by the way, an area in which Python (Stackless), and of course Scheme, are both far ahead of Perl.

I also failed to understand the logic of the following example from the RFC:

... one could write: %newhash = map {yield transform_key($_); transform_val($_)} +%oldhash; This flattens the %oldhash to a sequence of key/value pairs
This does not make sense to me, since $_ is a localized stack-based variable, which should in principle be wrapped up in the saved local context/state. Thus calls to transform_val would be operating on keys, not values.
               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
Re: Re: Perl 6 coroutines (RFC 31)
by John M. Dlugosz (Monsignor) on Jul 13, 2001 at 18:52 UTC
    I couldn't follow the continuation lab you mentioned. But I think the idea of a continuation primitive is exactly what we need, and my comments to Tilly earlier on this thread seems to be that.

    Specifically, the first yeild returns a continuation object. Use that object to "resume", and it knows exactly which instance you mean. The resume should pass parameters, too. It would re-bind @_ in the function, so the line after yeild could look at that (again) if desired.

    The resumer thing could be an object with various members, including call and reset and who knows what else. Or, it could return a function reference and just calling that will resume.

    my $next= $container->iterate ('inorder'); while (my $node= $next->()) { ... }
    Details: the iterate() function would do set-up and yeild with a token, using another built-in function to generate it. Or it could be a separate statement that creates it and yeilds at once. Then it goes into the traversal loop.

    What do you think? —John

      The semantics of real continuations are less trivial than that, and they appear (to me, at least) substantially more convoluted when applied to procedural, as opposed to functional languages. What you're describing is still a form of generators, which can be built with continuations and anonymous subs, but doesn't provide the full power of continuations.

      Additionaly, rebinding @_ is most likely a bad idea. There are better ways to pass values through to continuations. Changing your local context somewhat defeats the purpose of continuations to begin with.

                     s aamecha.s a..a\u$&owag.print
        So how would you do it, in Perl?
Re (tilly) 1: Perl 6 coroutines (RFC 31)
by tilly (Archbishop) on Jul 14, 2001 at 22:41 UTC
    I think that Damian's proposal assumes that Perl is going to maintain a global stack, and what is saved is just the internal state of the function. Then $_, being a global, will be whatever it is currently in Perl while where you start in the block will be preserved.
      I believe that would create alot of nasty confusion, and make localized globals all but useless inside of coroutines. For instance, the following would be quite troublesome:
      sub my_iter { for (@_) { yield $_; # upon return, $_ may no longer be localized. if we # modify it, we could be modifying a global value # outside our scope. there's no way to tell, and # that's rather nasty. } }
      Of course, one could avoid using localized globals inside coroutines/generators in the first place, but where's the fun in that, I ask ;)
                     s aamecha.s a..a\u$&owag.print
Re: Re: Perl 6 coroutines (RFC 31)
by John M. Dlugosz (Monsignor) on Jul 16, 2001 at 02:26 UTC
    Could you elaborate on the distinction between "coroutines", "generators", and "call with concurrent continuation"?

      YAIUL (Yet Another Informative USENET Link ;-)
                     s aamecha.s a..a\u$&owag.print

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2020-10-20 21:15 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (210 votes). Check out past polls.