Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Does Parrot have "True coroutines"?

8 views
Skip to first unread message

Klaas-Jan Stol

unread,
Nov 4, 2004, 11:00:26 AM11/4/04
to perl6-i...@perl.org
Hello,

I spoke (through email) with Roberto Ierusalimschy, one of the creators of the Lua programming language, and I said that Parrot has good support for implementing coroutines and closures (heck, they are explicitly there).

However, in a reply, Roberto asked:

"Are you sure Parrot support "true" coroutines? Does it integrate
coroutines and closures correctly? (For instance, a single closure may
refer to variables in several different coroutines.)"

Mmmmm, I wouldn't know. In Lua, one can create a coroutine explicitly (through a kind of package "coroutine", an example is included:

co = coroutine.create(function ()
for i=1,10 do
print("co", i)
coroutine.yield()
end
end)

Anybody got a clue on whether Parrot has "true" coroutines?

thanks,
Klaas-Jan


Leopold Toetsch

unread,
Nov 4, 2004, 2:24:25 PM11/4/04
to Klaas-Jan Stol, perl6-i...@perl.org
Klaas-Jan Stol <vand...@home.nl> wrote:
> Hello,

> "Are you sure Parrot support "true" coroutines? Does it integrate
> coroutines and closures correctly? (For instance, a single closure may
> refer to variables in several different coroutines.)"

Well, I don't know how "true" coroutines are defined, but Parrot, as
it's CPS based, has no problems with coroutines and there are no
restrictions to coroutines, AFAIK.

How coroutines finally really behave WRT argument passing isn't really
layed out. There is a good article in Dan's blog IIRC.

> co = coroutine.create(function ()
> for i=1,10 do
> print("co", i)
> coroutine.yield()
> end
> end)

In Parrot's enough to include a .yield() ...

,--[ simplest usage ]-
| $ cat coro.imc
| .sub main @MAIN
| $I0 = coro()
| print $I0
| $I0 = coro()
| print $I0
| .end
| .sub coro
| .yield(4)
| .yield(2)
| .end
|
| $ ./parrot coro.imc
| 42
`---------------------

to get a static coroutine. Above Lua snippet would use the C<newsub>
opcode to create a Coroutine function object. Such objects act as
closures too (modulo untested, unimplemented features ;)

> thanks,
> Klaas-Jan

leo

Klaas-Jan Stol

unread,
Nov 4, 2004, 4:11:07 PM11/4/04
to l...@toetsch.at, perl6-i...@perl.org

>Well, I don't know how "true" coroutines are defined, but Parrot, as
>it's CPS based, has no problems with coroutines and there are no
>restrictions to coroutines, AFAIK.
>
>
To be honest, I hadn't thought of this, either (this "true"-ness of
coroutines), but then again, I'm no expert on these things.

>How coroutines finally really behave WRT argument passing isn't really
>layed out. There is a good article in Dan's blog IIRC.
>
>

I'll read it again.

>
>
>>co = coroutine.create(function ()
>> for i=1,10 do
>> print("co", i)
>> coroutine.yield()
>> end
>> end)
>>
>>
>
>In Parrot's enough to include a .yield() ...
>
>,--[ simplest usage ]-
>| $ cat coro.imc
>| .sub main @MAIN
>| $I0 = coro()
>| print $I0
>| $I0 = coro()
>| print $I0
>| .end
>| .sub coro
>| .yield(4)
>| .yield(2)
>| .end
>|
>| $ ./parrot coro.imc
>| 42
>`---------------------
>
>
>

I hadn't seen ".yield(x)"
Is
.yield(x)

the same as:

.pcc_begin_yield
.return x
.pcc_end_yield
?

>to get a static coroutine. Above Lua snippet would use the C<newsub>
>opcode to create a Coroutine function object. Such objects act as
>closures too (modulo untested, unimplemented features ;)
>
>
>

So, correct me if I'm wrong, a Coroutine is also a closure?
In that case, anytime you would create a closure, you could also create
a coroutine?

I've also included the reply I got from Roberto:
<quote>

Untested code:
--------------------------------------------------------
function f(x)
A = function ()
x=x+1
end

B = coroutine.wrap(
function (y)
C = coroutine.wrap(
function (z)
while true do
coroutine.yield(x+y+z)
end
end)

while true do
y=y+1
coroutine.yield()
end
end)
end

X = f()
----------------------------------------------------------
Now we have:

- function A: each call increments x (in the main thread)

- coroutine B: each call increments y (in that coroutine)

- coroutine C: each call returns x+y+z (each in a different coroutine)


Of course, all this wold be more fun with recursion :)
-- Roberto

</quote>

Do you think the above code snippet could work? That is, without much
special code, can this be done in PIR?
(Of course, why would one ever want to write /such/ code :-P, but that's
another issue)

Thanks,
Klaas-Jan


Michael Walter

unread,
Nov 4, 2004, 4:43:07 PM11/4/04
to Klaas-Jan Stol, l...@toetsch.at, perl6-i...@perl.org

Stéphane Payrard

unread,
Nov 4, 2004, 9:14:09 PM11/4/04
to perl6-i...@perl.org
On Thu, Nov 04, 2004 at 10:11:07PM +0100, Klaas-Jan Stol wrote:
>
> I hadn't seen ".yield(x)"
> Is
> .yield(x)
>
> the same as:
>
> .pcc_begin_yield
> .return x
> .pcc_end_yield
> ?
>

Yes. This alternative syntax has been checked in yesterday
and is documented in the updated calling_conventions.pod.
The parentheses are mandatory.
Similarly you have:

.return(x)

which is equivalent to:

.pcc_begin_return
.return x
.pcc_end_return


--
stef

Luke Palmer

unread,
Nov 4, 2004, 12:53:45 PM11/4/04
to Klaas-Jan Stol, perl6-i...@perl.org
Klaas-Jan Stol writes:
> Hello,
>
> I spoke (through email) with Roberto Ierusalimschy, one of the creators of
> the Lua programming language, and I said that Parrot has good support for
> implementing coroutines and closures (heck, they are explicitly there).
>
> However, in a reply, Roberto asked:
>
> "Are you sure Parrot support "true" coroutines? Does it integrate
> coroutines and closures correctly? (For instance, a single closure may
> refer to variables in several different coroutines.)"

I have no idea what he means by that, but I'm sure parrot does it.
Parrot supports full contintuations, and coroutines can be implemented
using continuations. I can't fathom anything you'd want to do with
coroutines that parrot's continuations wouldn't allow you to do.

> Mmmmm, I wouldn't know. In Lua, one can create a coroutine explicitly
> (through a kind of package "coroutine", an example is included:
>
> co = coroutine.create(function ()
> for i=1,10 do
> print("co", i)
> coroutine.yield()
> end
> end)

That one's easy. Is that what he means by "true" coroutine? What's a
fake coroutine?

Luke

Leopold Toetsch

unread,
Nov 5, 2004, 8:37:19 AM11/5/04
to Klaas-Jan Stol, perl6-i...@perl.org
Klaas-Jan Stol <vand...@home.nl> wrote:

[ nested coroutines ]

> Do you think the above code snippet could work? That is, without much
> special code, can this be done in PIR?

As said, coroutine's (argument passing and more) behavior isn't yet
layed out and there are several possibilities for that. I'd say, just
try it ;) Coroutines should much be like Closures plus the yield()
functionality.

leo

Leopold Toetsch

unread,
Nov 5, 2004, 8:38:20 AM11/5/04
to Michael Walter, perl6-i...@perl.org
Michael Walter <michael...@gmail.com> wrote:
> I sense confusion between "closure", "continuation" and "coroutine".

> http://c2.com/cgi/wiki?ContinuationsAndCoroutines

and that's even referencing Dan's blog ;)

leo

0 new messages