Version 1 (modified by blamario, 5 years ago) |
---|

# Package monad-coroutine

This library, implemented by the Control.Monad.Coroutine module, provides a limited coroutine functionality in Haskell. The centerpiece of the approach is the monad transformer Coroutine, that transforms an arbitrary monadic computation into a suspendable and resumable one. The basic definition is simple:

newtype Coroutine s m r = Coroutine {resume :: m (CoroutineState s m r)} data CoroutineState s m r = Done r | Suspend! (s (Coroutine s m r)) instance (Functor s, Monad m) => Monad (Coroutine s m) where return x = Coroutine (return (Done x)) t >>= f = Coroutine (resume t >>= apply f) where apply f (Done x) = resume (f x) apply f (Suspend s) = return (Suspend (fmap (>>= f) s))

The Coroutine transformer type is parameterized by a functor. Here is an example of one functor particularly useful for a Coroutine computation:

data Yield x y = Yield x y instance Functor (Yield x) where fmap f (Yield x y) = Yield x (f y)