monad-coroutine-0.5.1: Coroutine monad transformer for suspending and resuming monadic computations



This module defines nestable suspension functors for use with the Coroutine monad transformer, as well as functions for running nested coroutines of this sort.

Coroutines can be run from within another coroutine. In this case, the nested coroutines always suspend to their invoker. If a function from this module, such as pogoStickNested, is used to run a nested coroutine, the parent coroutine can be automatically suspended as well. A single suspension can thus suspend an entire chain of nested coroutines.

Nestable coroutines of this kind should group their suspension functors into an EitherFunctor. You can adjust a normal suspension, such as the one produced by yield, using functions mapSuspension and liftOut. To run nested coroutines, use functions pogoStickNested, seesawNested, and coupleNested.



pogoStickNested :: forall s1 s2 m x. (Functor s1, Functor s2, Monad m) => (s2 (Coroutine (EitherFunctor s1 s2) m x) -> Coroutine (EitherFunctor s1 s2) m x) -> Coroutine (EitherFunctor s1 s2) m x -> Coroutine s1 m xSource

Run a nested Coroutine that can suspend both itself and the current Coroutine.

coupleNested :: forall s0 s1 s2 m x y r. (Monad m, Functor s0, Monad s0, Functor s1, Functor s2) => (forall x y r. (x -> y -> m r) -> m x -> m y -> m r) -> Coroutine (EitherFunctor s0 s1) m x -> Coroutine (EitherFunctor s0 s2) m y -> Coroutine (EitherFunctor s0 (SomeFunctor s1 s2)) m (x, y)Source

Weaves two nested coroutines into one.

seesawNested :: (Monad m, Functor s0, Functor s1, Functor s2) => (forall x y r. (x -> y -> m r) -> m x -> m y -> m r) -> SeesawResolver s1 s2 -> Coroutine (EitherFunctor s0 s1) m x -> Coroutine (EitherFunctor s0 s2) m y -> Coroutine s0 m (x, y)Source

Like seesaw, but for nested coroutines that are allowed to suspend the current coroutine as well as themselves. If both coroutines try to suspend the current coroutine in the same step, the left coroutine's suspension will have precedence.

class (Functor a, Functor d) => AncestorFunctor a d Source

Class of functors that can be lifted.


(d' ~ Parent d, Functor a, ChildFunctor d, AncestorFunctor a d') => AncestorFunctor a d 
Functor a => AncestorFunctor a a 

liftOut :: forall m a d x. (Monad m, Functor a, AncestorFunctor a d) => Coroutine a m x -> Coroutine d m xSource

Converts a coroutine into a descendant nested coroutine.