Changes between Version 1 and Version 2 of monad-coroutine
- Timestamp:
- 03/20/10 20:24:32 (3 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
monad-coroutine
v1 v2 2 2 = Package monad-coroutine = 3 3 4 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, thattransforms an arbitrary monadic computation into a suspendable and resumable one. The basic definition is simple:4 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`, which transforms an arbitrary monadic computation into a suspendable and resumable one. The basic definition is simple: 5 5 6 6 {{{ 7 newtype Coroutine s m r = Coroutine {resume :: m (CoroutineState s m r)} 8 9 data CoroutineState s m r = Done r | Suspend! (s (Coroutine s m r)) 7 newtype Coroutine s m r = Coroutine {resume :: m (Either (s (Coroutine s m r)) r)} 10 8 11 9 instance (Functor s, Monad m) => Monad (Coroutine s m) where 12 return x = Coroutine (return (Done x)) 13 t >>= f = Coroutine (resume t >>= apply f) 14 where apply f (Done x) = resume (f x) 15 apply f (Suspend s) = return (Suspend (fmap (>>= f) s)) 10 return = Coroutine . return . Right 11 t >>= f = Coroutine (resume t >>= either (return . Left . fmap (>>= f)) (resume . f)) 16 12 }}} 17 13 18 The Coroutine transformer type is parameterized by a functor. Here is an example of one functor particularly useful for a Coroutine computation:14 The Coroutine transformer type is parameterized by a functor. The functor in question wraps the resumption of a suspended coroutine, and it can carry other information as well. Module `Control.Monad.Coroutine.SuspensionFunctors` exports some useful functors, one of which is `Yield`: 19 15 20 16 {{{
