contstuff 1.2.0 → 1.2.3
raw patch · 4 files changed
+70/−50 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Control.ContStuff.Classes: forkOS :: Forkable m => m a -> m ThreadId
+ Control.ContStuff.Trans: liftMaybe :: Monad m => m (Maybe a) -> MaybeT r m a
- Control.ContStuff.Monads: runState :: s -> (s -> a -> r) -> State r s a -> r
+ Control.ContStuff.Monads: runState :: s -> (a -> s -> r) -> State r s a -> r
- Control.ContStuff.Trans: StateT :: (s -> (s -> a -> m r) -> m r) -> StateT r s m a
+ Control.ContStuff.Trans: StateT :: ((a -> s -> m r) -> s -> m r) -> StateT r s m a
- Control.ContStuff.Trans: getStateT :: StateT r s m a -> s -> (s -> a -> m r) -> m r
+ Control.ContStuff.Trans: getStateT :: StateT r s m a -> (a -> s -> m r) -> s -> m r
- Control.ContStuff.Trans: runStateT :: s -> (s -> a -> m r) -> StateT r s m a -> m r
+ Control.ContStuff.Trans: runStateT :: s -> (a -> s -> m r) -> StateT r s m a -> m r
Files
- Control/ContStuff/Classes.hs +5/−4
- Control/ContStuff/Monads.hs +2/−2
- Control/ContStuff/Trans.hs +60/−41
- contstuff.cabal +3/−3
Control/ContStuff/Classes.hs view
@@ -93,18 +93,19 @@ -- | Generalization of 'Conc.forkIO'. forkIO :: m a -> m ThreadId - -- x| Generalization of 'Conc.forkOS'.- -- forkOS :: m a -> m ThreadId+ -- | Generalization of 'Conc.forkOS'.+ forkOS :: m a -> m ThreadId - -- x| Generalization of 'Conc.runInBoundThread'.+ -- Generalization of 'Conc.runInBoundThread'. -- runInBoundThread :: m a -> m a - -- x| Generalization of 'Conc.runInUnboundThread'.+ -- Generalization of 'Conc.runInUnboundThread'. -- runInUnboundThread :: m a -> m a instance Forkable IO where forkIO = Conc.forkIO . (() <$)+ forkOS = Conc.forkOS . (() <$) ----------------
Control/ContStuff/Monads.hs view
@@ -130,8 +130,8 @@ -- | Run a stateful computation. -runState :: s -> (s -> a -> r) -> State r s a -> r-runState s0 k c = runIdentity $ runStateT s0 (\s1 -> Identity . k s1) c+runState :: s -> (a -> s -> r) -> State r s a -> r+runState s0 k c = runIdentity $ runStateT s0 (\x -> Identity . k x) c -- | Run a stateful computation returning its result.
Control/ContStuff/Trans.hs view
@@ -30,7 +30,7 @@ -- ** Exceptions EitherT(..),- runEitherT, evalEitherT, modifyEitherT, testEitherT,+ runEitherT, evalEitherT, liftMaybe, modifyEitherT, testEitherT, MaybeT(..), runMaybeT, evalMaybeT, modifyMaybeT, testMaybeT,@@ -93,12 +93,8 @@ cx (\xx yx kx -> cf (\xf yf kf -> fold xf (yf yx) kf) xx kx) z k instance Forkable m => Forkable (ChoiceT r i m) where- forkIO =- let findAllM_ :: Monad m => ChoiceT r i m a -> m r- findAllM_ = runChoiceT (\_ _ k -> k undefined)- undefined- (const $ return undefined)- in lift . forkIO . findAllM_+ forkIO = lift . forkIO . findAll'+ forkOS = lift . forkOS . findAll' instance Functor (ChoiceT r i m) where fmap f (ChoiceT c) =@@ -156,6 +152,13 @@ runChoiceT (\_ _ k -> k undefined) undefined (const $ pure undefined) +-- | Find all solutions and ignore them. Internal utility function.++findAll' :: Monad m => ChoiceT r i m a -> m r+findAll' =+ runChoiceT (\_ _ k -> k undefined) undefined (const $ return undefined)++ -- | Find the first solution. findFirst :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)@@ -219,6 +222,9 @@ forkIO (ContT c) = ContT $ \k -> forkIO (c (return . const undefined)) >>= k+ forkOS (ContT c) =+ ContT $ \k ->+ forkOS (c (return . const undefined)) >>= k instance Functor (ContT r m) where fmap f (ContT c) = ContT $ \k -> c (\x -> k (f x))@@ -293,10 +299,8 @@ getEitherT (f (\x -> EitherT $ \_ _ -> k x)) k expk instance Forkable m => Forkable (EitherT r e m) where- forkIO (EitherT c) =- let uk :: Monad m => a -> m b- uk = const (return undefined)- in lift . forkIO $ c uk uk+ forkIO (EitherT c) = lift . forkIO $ c deReturn deReturn+ forkOS (EitherT c) = lift . forkOS $ c deReturn deReturn instance HasExceptions (EitherT r e m) where type Exception (EitherT r e m) = e@@ -358,6 +362,13 @@ in runEitherT (pc True) (pc False) +-- | Helper function: Turn an arbitrary pure value into a monadic+-- 'bottom'.++deReturn :: Monad m => a -> m b+deReturn = const (return undefined)++ ------------ -- MaybeT -- ------------@@ -389,8 +400,8 @@ getMaybeT (f (\x -> MaybeT $ \_ _ -> just x)) just noth instance Forkable m => Forkable (MaybeT r m) where- forkIO (MaybeT c) =- lift . forkIO $ c (const $ return undefined) (return undefined)+ forkIO (MaybeT c) = lift . forkIO $ c deReturn (return undefined)+ forkOS (MaybeT c) = lift . forkOS $ c deReturn (return undefined) instance HasExceptions (MaybeT r m) where type Exception (MaybeT r m) = ()@@ -436,6 +447,15 @@ evalMaybeT (MaybeT c) = c (pure . Just) (pure Nothing) +-- | Lift a computation which returns a 'Maybe'-wrapped value to a+-- 'MaybeT' computation.++liftMaybe :: Monad m => m (Maybe a) -> MaybeT r m a+liftMaybe c =+ MaybeT $ \just nothing ->+ c >>= maybe nothing just++ -- | Modify the result of a 'MaybeT' computation along the way. modifyMaybeT :: Functor m => (r -> r) -> MaybeT r m ()@@ -481,8 +501,8 @@ ------------- -- | Monad transformer for computations with readable environment.--- Unlike the other monad transformers this one allows no CPS effects,--- which makes it commutative.+-- Unlike the other monad transformers this one allows no CPS effects+-- and also hides its constructors, which makes it commutative. -- -- If you need CPS effects, consider using 'StateT'. @@ -495,6 +515,7 @@ instance Forkable m => Forkable (ReaderT e m) where forkIO (ReaderT c) = ReaderT (forkIO c)+ forkOS (ReaderT c) = ReaderT (forkOS c) instance Functor (ReaderT e m) where fmap f (ReaderT c) = ReaderT (fmap f c)@@ -528,7 +549,7 @@ -- | Monad transformer for stateful computations. newtype StateT r s m a =- StateT { getStateT :: s -> (s -> a -> m r) -> m r }+ StateT { getStateT :: (a -> s -> m r) -> s -> m r } instance Applicative m => Abortable (StateT r s m) where type Result (StateT r s m) = r@@ -537,34 +558,31 @@ instance Alternative m => Alternative (StateT r s m) where empty = StateT . const . const $ empty StateT c <|> StateT d =- StateT $ \s0 k -> c s0 k <|> d s0 k+ StateT $ \k s0 -> c k s0 <|> d k s0 instance Applicative (StateT r s m) where pure = return- StateT cf <*> StateT cx =- StateT $ \s0 k -> cf s0 (\s1 f -> cx s1 (\s2 x -> k s2 (f x)))+ StateT cf <*> StateT cx = StateT $ \k -> cf (\f -> cx (k . f)) instance CallCC (StateT r s m) where- callCC f =- StateT $ \s0 k ->- getStateT (f (\x -> StateT $ \s1 _ -> k s1 x)) s0 k+ callCC f = StateT $ \k -> getStateT (f (\x -> StateT $ \_ -> k x)) k instance Forkable m => Forkable (StateT r s m) where- forkIO (StateT c) =- StateT $ \s0 k ->- forkIO (c s0 (const $ pure undefined))- >>= k s0+ forkIO (StateT c) = StateT $ \k s0 -> forkIO (c deReturn s0) >>= flip k s0+ forkOS (StateT c) = StateT $ \k s0 -> forkOS (c deReturn s0) >>= flip k s0 instance Functor (StateT r s m) where- fmap f (StateT c) =- StateT $ \s0 k -> c s0 (\s1 -> k s1 . f)+ {-# INLINE fmap #-}+ fmap f (StateT c) = StateT $ \k -> c (k . f) instance Monad (StateT r s m) where- return x = StateT $ \s0 k -> k s0 x- StateT c >>= f =- StateT $ \s0 k -> c s0 (\s1 x -> getStateT (f x) s1 k)+ {-# INLINE return #-}+ return x = StateT ($ x)+ {-# INLINE (>>=) #-}+ StateT c >>= f = StateT $ \k -> c (\x -> getStateT (f x) k) instance MonadIO m => MonadIO (StateT r s m) where+ {-# INLINE liftIO #-} liftIO = lift . liftIO instance Alternative m => MonadPlus (StateT r s m) where@@ -573,38 +591,39 @@ instance Readable (StateT r s m) where type StateOf (StateT r s m) = s- get = StateT $ \s0 k -> k s0 s0+ get = StateT $ \k s0 -> k s0 s0 instance Stateful (StateT r s m) where- put s1 = s1 `seq` StateT $ \_ k -> k s1 ()- putLazy s1 = StateT $ \_ k -> k s1 ()+ put s1 = s1 `seq` StateT $ \k -> const (k () s1)+ putLazy s1 = StateT $ \k -> const (k () s1) instance MonadTrans (StateT r s) where- lift c = StateT $ \s0 k -> c >>= k s0+ {-# INLINE lift #-}+ lift c = StateT $ \k s0 -> c >>= flip k s0 instance Alternative m => Writable (StateT r s m) r where- tell x = StateT $ \s0 k -> pure x <|> k s0 ()+ tell x = StateT $ \k s0 -> pure x <|> k () s0 instance (Functor m, Monoid w) => Writable (StateT (r, w) s m) w where- tell x = StateT $ \s0 k -> fmap (second (`mappend` x)) (k s0 ())+ tell x = StateT $ \k -> fmap (second (`mappend` x)) . k () -- | Run a state transformer. -runStateT :: s -> (s -> a -> m r) -> StateT r s m a -> m r-runStateT s0 k (StateT c) = c s0 k+runStateT :: s -> (a -> s -> m r) -> StateT r s m a -> m r+runStateT s0 k (StateT c) = c k s0 -- | Run a state transformer returning its result. evalStateT :: Applicative m => s -> StateT r s m r -> m r-evalStateT s0 (StateT c) = c s0 (\_ x -> pure x)+evalStateT s0 (StateT c) = c (\x -> const (pure x)) s0 -- | Run a state transformer returning its final state. execStateT :: Applicative m => s -> StateT s s m a -> m s-execStateT s0 (StateT c) = c s0 (\s1 _ -> pure s1)+execStateT s0 (StateT c) = c (\_ s1 -> pure s1) s0 -------------
contstuff.cabal view
@@ -1,5 +1,5 @@ Name: contstuff-Version: 1.2.0+Version: 1.2.3 Category: Control, Monads Synopsis: Fast, easy to use CPS-based monad transformers Maintainer: Ertugrul Söylemez <es@ertes.de>@@ -17,7 +17,7 @@ transformers. Most of the usual monad transformers are implemented, including ChoiceT, ContT, EitherT, MaybeT and StateT. Because of the design of this library, many other monad transformers are just- special cases of those, including e.g. WriterT.+ special cases of those, including e.g. ReaderT and WriterT. The Control.ContStuff.Simple module also provides simplified monad transformer wrappers, which hide the underlying CPS, so you get the@@ -29,7 +29,7 @@ Build-depends: base >= 4 && <= 5, transformers >= 0.2.2.0- GHC-Options: -W+ GHC-Options: -W -O2 Exposed-modules: Control.ContStuff Control.ContStuff.Classes