Re: [Haskell-cafe] Continuations and coroutines

2010-06-24 Thread Mario Blažević

Yves Parès wrote:
It helps me understand better, but would you have some simple code that 
would do that ?


	You can look at the definition of the coroutine monad transformer in 
the monad-coroutine package as well:


   http://hackage.haskell.org/package/monad-coroutine

The heart of the library is in the data type


newtype Coroutine s m r = Coroutine {
   resume :: m (Either (s (Coroutine s m r)) r)
}


where s is an arbitrary functor (like Yield, for example), m is an 
arbitrary monad, and r is the coroutine's final result type.



	You can also read the "Trampolined Style" and "The essence of 
multitasking" papers:


http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.45.5447&rep=rep1&type=pdf
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.92.4514&rep=rep1&type=pdf





2010/6/19 Paul Johnson mailto:p...@cogito.org.uk>>

On 19/06/10 10:36, Yves Parčs wrote:

Hello,

I saw on the haskell wikibook that coroutines could be
implemented by using continuations :

http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style#Example:_coroutines
(unhappily, the section is empty)
Since I'm actually learning the wonders of continuations, I just
wonder : how ?
 



Coroutines depend on the ability to suspend and resume execution.  A
continuation acts as the "resume point" in the current function.
 The "callCC" function in the continuation monad takes a function
that expects the continuation as an argument (which is how you get
access to it).  So you say something like:

 >  yield = callCC $ \continuation -> 

Then you would typically store the continuation somewhere and call
some other previously stored continuation to switch contexts.

Continuations can be used to pass data back into the continuation:
you call the continuation with an argument, and that argument
becomes the return value of the "callCC".  In this case you probably
just want to use ().

You typically have a queue for continuations, so the new
continuation goes on the back of the queue and then you call the
head of the queue.  Obvious modifications for priority, simulated
time, real time or whatever else you are trying to schedule.  This
implies some kind of monadic state to store the queue in, so you
will probably make your monad of type "ContT (State Queue)"

If you want a thread to wait, say on a semaphore, then you have a
queue of continuations in the semaphore data structure.

Is this any help?

Paul.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org 
http://www.haskell.org/mailman/listinfo/haskell-cafe





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



--
Mario Blazevic
mblaze...@stilo.com
Stilo International

This message, including any attachments, is for the sole use of the
intended recipient(s) and may contain confidential and privileged
information. Any unauthorized review, use, disclosure, copying, or
distribution is strictly prohibited. If you are not the intended
recipient(s) please contact the sender by reply email and destroy
all copies of the original message and any attachments.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Continuations and coroutines

2010-06-20 Thread Paul Johnson

On 20/06/10 22:03, Paul Johnson wrote:

On 19/06/10 17:23, Yves Parès wrote:
It helps me understand better, but would you have some simple code 
that would do that ?





http://www.cs.chalmers.se/~koen/pubs/jfp99-monad.ps



Except that the paper I'm trying to refer to seems to have fallen off 
the net.  Its "A Poor Man's Concurrency Monad".  Does anyone have a copy?


Paul.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Continuations and coroutines

2010-06-20 Thread Paul Johnson

On 19/06/10 17:23, Yves Parès wrote:
It helps me understand better, but would you have some simple code 
that would do that ?





http://www.cs.chalmers.se/~koen/pubs/jfp99-monad.ps

Paul.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Continuations and coroutines

2010-06-19 Thread Yves Parès
It helps me understand better, but would you have some simple code that
would do that ?


2010/6/19 Paul Johnson 

> On 19/06/10 10:36, Yves Parčs wrote:
>
>> Hello,
>>
>> I saw on the haskell wikibook that coroutines could be implemented by
>> using continuations :
>> http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style#Example:_coroutines(unhappily,
>>  the section is empty)
>> Since I'm actually learning the wonders of continuations, I just wonder :
>> how ?
>>
>>
>
> Coroutines depend on the ability to suspend and resume execution.  A
> continuation acts as the "resume point" in the current function.  The
> "callCC" function in the continuation monad takes a function that expects
> the continuation as an argument (which is how you get access to it).  So you
> say something like:
>
> >  yield = callCC $ \continuation -> 
>
> Then you would typically store the continuation somewhere and call some
> other previously stored continuation to switch contexts.
>
> Continuations can be used to pass data back into the continuation: you call
> the continuation with an argument, and that argument becomes the return
> value of the "callCC".  In this case you probably just want to use ().
>
> You typically have a queue for continuations, so the new continuation goes
> on the back of the queue and then you call the head of the queue.  Obvious
> modifications for priority, simulated time, real time or whatever else you
> are trying to schedule.  This implies some kind of monadic state to store
> the queue in, so you will probably make your monad of type "ContT (State
> Queue)"
>
> If you want a thread to wait, say on a semaphore, then you have a queue of
> continuations in the semaphore data structure.
>
> Is this any help?
>
> Paul.
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Continuations and coroutines

2010-06-19 Thread Paul Johnson

On 19/06/10 10:36, Yves Parès wrote:

Hello,

I saw on the haskell wikibook that coroutines could be implemented by 
using continuations : 
http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style#Example:_coroutines 
(unhappily, the section is empty)
Since I'm actually learning the wonders of continuations, I just 
wonder : how ?
   


Coroutines depend on the ability to suspend and resume execution.  A 
continuation acts as the "resume point" in the current function.  The 
"callCC" function in the continuation monad takes a function that 
expects the continuation as an argument (which is how you get access to 
it).  So you say something like:


>  yield = callCC $ \continuation -> 

Then you would typically store the continuation somewhere and call some 
other previously stored continuation to switch contexts.


Continuations can be used to pass data back into the continuation: you 
call the continuation with an argument, and that argument becomes the 
return value of the "callCC".  In this case you probably just want to 
use ().


You typically have a queue for continuations, so the new continuation 
goes on the back of the queue and then you call the head of the queue.  
Obvious modifications for priority, simulated time, real time or 
whatever else you are trying to schedule.  This implies some kind of 
monadic state to store the queue in, so you will probably make your 
monad of type "ContT (State Queue)"


If you want a thread to wait, say on a semaphore, then you have a queue 
of continuations in the semaphore data structure.


Is this any help?

Paul.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Continuations and coroutines

2010-06-19 Thread Yves Parès
Hello,

I saw on the haskell wikibook that coroutines could be implemented by using
continuations :
http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style#Example:_coroutines(unhappily,
the section is empty)
Since I'm actually learning the wonders of continuations, I just wonder :
how ?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe