atrans 0.1.0.0 → 0.1.0.1
raw patch · 2 files changed
+9/−6 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- atrans.cabal +1/−1
- src/Control/Monad/Backend.hs +8/−5
atrans.cabal view
@@ -1,5 +1,5 @@ name: atrans-version: 0.1.0.0+version: 0.1.0.1 synopsis: A small collection of monad (transformer) instances. description: Defines monad transformers and gives instances based on the mtl transformer library. homepage: https://github.com/aphorisme/atrans
src/Control/Monad/Backend.hs view
@@ -16,11 +16,11 @@ import Control.Monad.Except import Control.Monad.State-import Control.Concurrent.MVar (MVar, isEmptyMVar, takeMVar, putMVar, readMVar)+import Control.Concurrent.MVar (MVar, isEmptyMVar, modifyMVar_, takeMVar, putMVar, readMVar) import Control.Arrow (second) --- | The 'BackendT' transformer is a 'StateT', where the state is encapsulated into a 'MVar'.+-- | The 'BackendT' transformer is a 'StateT', where the state is encapsulated into a 'MVar'. The initialized 'MVar' has to be non empty, otherwise every state change is blocking. newtype BackendT s m a = BackendT (MVar s -> m (MVar s, a)) -- | 'runBackendT' unwraps the 'BackendT' monad.@@ -41,9 +41,12 @@ instance (Monad m, MonadIO m) => MonadState s (BackendT s m) where get = BackendT $ \s -> do { v <- liftIO (readMVar s); return (s, v) } put s = BackendT $- \st -> do b <- liftIO (isEmptyMVar st)- if b then do { liftIO (putMVar st s); return (st, ()) }- else do { liftIO (takeMVar st); liftIO (putMVar st s); return (st, ()) }+ \st -> do { liftIO (modifyMVar_ st (\_ -> return s)); return (st, ()) }+ state f = BackendT $+ \ms -> do s <- liftIO (takeMVar ms)+ let (x, s') = f s+ liftIO (putMVar ms s')+ return (ms, x) instance (Monad m, MonadIO m) => MonadIO (BackendT s m) where liftIO io = BackendT $ \s -> do { x <- liftIO io; return (s, x) }