module Control.Monad.Coroutine.Nested
(
pogoStickNested, coupleNested, seesawNested,
AncestorFunctor,
liftOut
)
where
import Control.Monad (join, liftM)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Coroutine
import Control.Monad.Coroutine.SuspensionFunctors
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 x
pogoStickNested reveal t =
Coroutine{resume= resume t
>>= \s-> case s
of Right result -> return (Right result)
Left (LeftF s) -> return (Left (fmap (pogoStickNested reveal) s))
Left (RightF c) -> resume (pogoStickNested reveal (reveal c))}
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)
coupleNested runPair = coupleNested' where
coupleNested' t1 t2 = Coroutine{resume= runPair (\ st1 st2 -> return (proceed st1 st2)) (resume t1) (resume t2)}
proceed (Right x) (Right y) = Right (x, y)
proceed (Left (RightF s)) (Right y) = Left $ RightF $ fmap (flip coupleNested' (return y)) (LeftSome s)
proceed (Right x) (Left (RightF s)) = Left $ RightF $ fmap (coupleNested' (return x)) (RightSome s)
proceed (Left (RightF s1)) (Left (RightF s2)) =
Left $ RightF $ fmap (uncurry coupleNested') (Both $ composePair s1 s2)
proceed l (Left (LeftF s)) = Left $ LeftF $ fmap (coupleNested' (Coroutine $ return l)) s
proceed (Left (LeftF s)) r = Left $ LeftF $ fmap (flip coupleNested' (Coroutine $ return r)) s
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)
seesawNested runPair resolver t1 t2 = seesaw' t1 t2 where
seesaw' t1 t2 = Coroutine{resume= bouncePair t1 t2}
bouncePair t1 t2 = runPair proceed (resume t1) (resume t2)
proceed (Left (LeftF s1)) state2 = return $ Left $ fmap ((flip seesaw' (Coroutine $ return state2))) s1
proceed state1 (Left (LeftF s2)) = return $ Left $ fmap (seesaw' (Coroutine $ return state1)) s2
proceed (Right x) (Right y) = return $ Right (x, y)
proceed state1@(Right x) (Left (RightF s2)) = proceed state1 =<< resume (resumeRight resolver s2)
proceed (Left (RightF s1)) state2@(Right y) = flip proceed state2 =<< resume (resumeLeft resolver s1)
proceed state1@(Left (RightF s1)) state2@(Left (RightF s2)) =
resumeAny resolver ((flip proceed state2 =<<) . resume) ((proceed state1 =<<) . resume) bouncePair s1 s2
class Functor c => ChildFunctor c where
type Parent c :: * -> *
wrap :: Parent c x -> c x
instance (Functor p, Functor s) => ChildFunctor (EitherFunctor p s) where
type Parent (EitherFunctor p s) = p
wrap = LeftF
class (Functor a, Functor d) => AncestorFunctor a d where
liftFunctor :: a x -> d x
instance Functor a => AncestorFunctor a a where
liftFunctor = id
instance (Functor a, ChildFunctor d, d' ~ Parent d, AncestorFunctor a d') => AncestorFunctor a d where
liftFunctor = wrap . (liftFunctor :: a x -> d' x)
liftOut :: forall m a d x. (Monad m, Functor a, AncestorFunctor a d) => Coroutine a m x -> Coroutine d m x
liftOut cort = mapSuspension liftFunctor cort