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
> "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
>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
http://c2.com/cgi/wiki?ContinuationExplanation
http://c2.com/cgi/wiki?ContinuationsAndCoroutines
http://c2.com/cgi/wiki?CoRoutine
http://c2.com/cgi/wiki?LexicalClosure
Cheers,
Michael
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
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
[ 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
> http://c2.com/cgi/wiki?ContinuationsAndCoroutines
and that's even referencing Dan's blog ;)
leo