monad-coroutine 0.5 → 0.5.1
raw patch · 4 files changed
+17/−20 lines, 4 filesdep ~transformersPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: transformers
API changes (from Hackage documentation)
- Control.Monad.Coroutine: NestedFunctor :: (l (r x)) -> NestedFunctor l r x
- Control.Monad.Coroutine: instance (Functor l, Functor r) => Functor (NestedFunctor l r)
- Control.Monad.Coroutine: nest :: (Functor a, Functor b) => a x -> b y -> NestedFunctor a b (x, y)
- Control.Monad.Coroutine: newtype NestedFunctor l r x
+ Control.Monad.Coroutine: composePair :: (Functor a, Functor b) => a x -> b y -> Compose a b (x, y)
- Control.Monad.Coroutine: Both :: (NestedFunctor l r x) -> SomeFunctor l r x
+ Control.Monad.Coroutine: Both :: (Compose l r x) -> SomeFunctor l r x
Files
- Control/Monad/Coroutine.hs +10/−14
- Control/Monad/Coroutine/Nested.hs +2/−2
- TestCoroutine.hs +3/−2
- monad-coroutine.cabal +2/−2
Control/Monad/Coroutine.hs view
@@ -62,16 +62,17 @@ mapMonad, mapSuspension, -- * Running Coroutine computations Naught, runCoroutine, pogoStick, foldRun, seesaw, SeesawResolver(..),- -- * Nested and coupled Coroutine computations- NestedFunctor (NestedFunctor), SomeFunctor(..), nest,+ -- * Coupled Coroutine computations+ SomeFunctor(..), composePair, couple, merge ) where import Control.Monad (liftM, when)-import Control.Monad.Trans (MonadTrans(..), MonadIO(..))+import Control.Monad.IO.Class (MonadIO(..))+import Control.Monad.Trans.Class (MonadTrans(..)) import Data.Either (either, partitionEithers)-import Data.Traversable (Traversable, sequence)+import Data.Functor.Compose (Compose(..)) import Control.Monad.Parallel @@ -110,21 +111,16 @@ instance Functor Naught where fmap f _ = undefined --- | Combines two functors into one, applying both.-newtype NestedFunctor l r x = NestedFunctor (l (r x))-instance (Functor l, Functor r) => Functor (NestedFunctor l r) where- fmap f (NestedFunctor lr) = NestedFunctor ((fmap . fmap) f lr)- -- | Combines two functors into one, applying either or both of them. Used for coupled coroutines.-data SomeFunctor l r x = LeftSome (l x) | RightSome (r x) | Both (NestedFunctor l r x)+data SomeFunctor l r x = LeftSome (l x) | RightSome (r x) | Both (Compose l r x) instance (Functor l, Functor r) => Functor (SomeFunctor l r) where fmap f (LeftSome l) = LeftSome (fmap f l) fmap f (RightSome r) = RightSome (fmap f r) fmap f (Both lr) = Both (fmap f lr) --- | Combines two values under two functors into a pair of values under a single 'NestedFunctor'.-nest :: (Functor a, Functor b) => a x -> b y -> NestedFunctor a b (x, y)-nest a b = NestedFunctor $ fmap (\x-> fmap ((,) x) b) a+-- | Combines two values under two functors into a pair of values under a single 'Compose'.+composePair :: (Functor a, Functor b) => a x -> b y -> Compose a b (x, y)+composePair a b = Compose $ fmap (\x-> fmap ((,) x) b) a -- | Suspend the current 'Coroutine'. suspend :: (Monad m, Functor s) => s (Coroutine s m x) -> Coroutine s m x@@ -169,7 +165,7 @@ couple runPair t1 t2 = Coroutine{resume= runPair proceed (resume t1) (resume t2)} where proceed :: CoroutineStepResult s1 m x -> CoroutineStepResult s2 m y -> m (CoroutineStepResult (SomeFunctor s1 s2) m (x, y)) proceed (Right x) (Right y) = return $ Right (x, y)- proceed (Left s1) (Left s2) = return $ Left $ fmap (uncurry (couple runPair)) (Both $ nest s1 s2)+ proceed (Left s1) (Left s2) = return $ Left $ fmap (uncurry (couple runPair)) (Both $ composePair s1 s2) proceed (Right x) (Left s2) = return $ Left $ fmap (couple runPair (return x)) (RightSome s2) proceed (Left s1) (Right y) = return $ Left $ fmap (flip (couple runPair) (return y)) (LeftSome s1)
Control/Monad/Coroutine/Nested.hs view
@@ -39,7 +39,7 @@ where import Control.Monad (join, liftM)-import Control.Monad.Trans (lift)+import Control.Monad.Trans.Class (lift) import Control.Monad.Coroutine import Control.Monad.Coroutine.SuspensionFunctors@@ -66,7 +66,7 @@ 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 $ nest s1 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
TestCoroutine.hs view
@@ -21,8 +21,9 @@ import Prelude hiding (sequence) import Control.Exception (assert) import Control.Monad (liftM, mapM, when)-import Control.Monad.Identity (runIdentity) import Control.Parallel (pseq)+import Data.Functor.Compose (Compose(..))+import Data.Functor.Identity (runIdentity) import Data.List (find) import Data.Maybe (fromJust) import System.Environment (getArgs)@@ -58,7 +59,7 @@ resume (couple bindM2 (fibs 1) (fibs 2)) where resume :: SomeFunctor (Yield Integer) (Yield Integer) c -> c- resume (Both (NestedFunctor (Yield n1 (Yield n2 c)))) = assert (n1 == n2) c+ resume (Both (Compose (Yield n1 (Yield n2 c)))) = assert (n1 == n2) c fibs ix = mapM_ (yieldApply (fib ix)) nums >> return (fib ix $ last nums) twoFibsSeesaw :: MonadParallel m => [Int] -> m Integer
monad-coroutine.cabal view
@@ -1,5 +1,5 @@ Name: monad-coroutine-Version: 0.5+Version: 0.5.1 Cabal-Version: >= 1.2 Build-Type: Simple Synopsis: Coroutine monad transformer for suspending and resuming monadic computations@@ -23,5 +23,5 @@ Library Exposed-Modules: Control.Monad.Coroutine, Control.Monad.Coroutine.SuspensionFunctors, Control.Monad.Coroutine.Nested- Build-Depends: base < 5, monad-parallel, transformers+ Build-Depends: base < 5, monad-parallel, transformers >= 0.2 && < 0.3 GHC-prof-options: -auto-all