mstate 0.2.4 → 0.2.7
raw patch · 2 files changed
+23/−16 lines, 2 filesdep ~basedep ~monad-peeldep ~mtl
Dependency ranges changed: base, monad-peel, mtl, stm
Files
- mstate.cabal +6/−10
- src/Control/Concurrent/MState.hs +17/−6
mstate.cabal view
@@ -6,27 +6,23 @@ the state values (if desired). Author: Nils Schweinsberg-Maintainer: <mail@n-sch.de>+Maintainer: <mail@nils.cc> -Version: 0.2.4+Version: 0.2.7 Category: Concurrency, Monads License: BSD3 License-File: LICENSE Cabal-Version: >= 1.6 Build-Type: Simple -source-repository head- type: git- location: https://github.com/mcmaniac/mstate.git- Library GHC-Options: -Wall Hs-Source-Dirs: src Build-Depends:- base == 4.*,- mtl == 2.*,- stm == 2.*,- monad-peel == 0.1.*+ base == 4.*,+ mtl,+ stm,+ monad-peel >= 0.1.1 Exposed-Modules: Control.Concurrent.MState
src/Control/Concurrent/MState.hs view
@@ -16,7 +16,7 @@ --------------------------------------------------------------------------- module Control.Concurrent.MState- ( + ( -- * The MState Monad MState , module Control.Monad.State.Class@@ -24,6 +24,7 @@ , evalMState , execMState , mapMState+ , mapMState_ -- , withMState , modifyM , modifyM_@@ -38,8 +39,6 @@ -- $example ) where -import Prelude hiding (catch)- import Control.Applicative import Control.Monad.State.Class import Control.Monad.Cont@@ -131,6 +130,14 @@ liftIO . atomically $ writeTVar r v' return b +mapMState_ :: (MonadIO m, MonadIO n)+ => (m a -> n b)+ -> MState t m a+ -> MState t n b+mapMState_ f m = MState $ \s -> do+ b <- f $ runMState' m s+ return b+ {- TODO: What's the point of this function? Does it make sense for MStates? -- | Apply a function to the state before running the `MState`@@ -228,6 +235,10 @@ pure = return (<*>) = ap +instance (Alternative m, Monad m) => Alternative (MState t m) where+ empty = MState $ \_ -> empty+ m <|> n = MState $ \t -> runMState' m t <|> runMState' n t+ instance (MonadPlus m) => MonadPlus (MState t m) where mzero = MState $ \_ -> mzero m `mplus` n = MState $ \t -> runMState' m t `mplus` runMState' n t@@ -287,13 +298,13 @@ > import Control.Concurrent > import Control.Concurrent.MState > import Control.Monad.State-> +> > type MyState a = MState Int IO a-> +> > -- Expected state value: 2 > main :: IO () > main = print =<< execMState incTwice 0-> +> > incTwice :: MyState () > incTwice = do > -- increase in the current thread