[Haskell-cafe] Continuations and coroutines

Mario Blažević mblazevic at stilo.com
Thu Jun 24 12:19:00 EDT 2010


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 <paul at cogito.org.uk <mailto:paul at 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 at haskell.org <mailto:Haskell-Cafe at haskell.org>
>     http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


-- 
Mario Blazevic
mblazevic at 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.


More information about the Haskell-Cafe mailing list