stateWriter 0.2.9 → 0.2.10
raw patch · 5 files changed
+75/−17 lines, 5 filesdep ~base
Dependency ranges changed: base
Files
- Control/Monad/RSS/Lazy.hs +2/−1
- Control/Monad/RSS/Strict.hs +2/−1
- Control/Monad/Trans/RSS/Lazy.hs +24/−7
- Control/Monad/Trans/RSS/Strict.hs +46/−7
- stateWriter.cabal +1/−1
Control/Monad/RSS/Lazy.hs view
@@ -17,6 +17,7 @@ withRSS, -- * The RSST monad transformer RSST,+ runRSST, evalRSST, execRSST, withRSST,@@ -33,7 +34,7 @@ import Control.Monad.Trans import Control.Monad.Trans.RSS.Lazy ( RSS, rss, runRSS, evalRSS, execRSS, withRSS,- RSST, evalRSST, execRSST, withRSST)+ RSST, evalRSST, execRSST, withRSST, runRSST) import Control.Monad import Control.Monad.Fix
Control/Monad/RSS/Strict.hs view
@@ -17,6 +17,7 @@ withRSS, -- * The RSST monad transformer RSST,+ runRSST, evalRSST, execRSST, withRSST,@@ -33,7 +34,7 @@ import Control.Monad.Trans import Control.Monad.Trans.RSS.Strict ( RSS, rss, runRSS, evalRSS, execRSS, withRSS,- RSST, evalRSST, execRSST, withRSST)+ RSST, evalRSST, execRSST, withRSST, runRSST) import Control.Monad import Control.Monad.Fix
Control/Monad/Trans/RSS/Lazy.hs view
@@ -1,4 +1,7 @@-{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE UndecidableInstances #-} module Control.Monad.Trans.RSS.Lazy ( -- * The RWS monad RSS,@@ -13,6 +16,8 @@ evalRSST, execRSST, withRSST,+ -- * Helpers+ liftCatch ) where import Control.Applicative@@ -20,6 +25,8 @@ import Control.Monad.Fix import Control.Monad.IO.Class import Control.Monad.Trans.Class+import Control.Monad.Except+import Control.Monad.Signatures import Data.Functor.Identity import Control.Monad.State@@ -117,23 +124,23 @@ fmap (\ ~(a, (s', w)) -> (f a, (s', w))) $ runRSST' m r s instance (Monad m) => Monad (RSST r w s m) where- return a = RSST $ \_ s -> return (a, s)+ return = pure m >>= k = RSST $ \r s -> do ~(a, (s', w)) <- runRSST' m r s runRSST' (k a) r (s',w) fail msg = RSST $ \_ _ -> fail msg instance (MonadPlus m) => MonadPlus (RSST r w s m) where- mzero = RSST $ \_ _ -> mzero- m `mplus` n = RSST $ \r s -> runRSST' m r s `mplus` runRSST' n r s+ mzero = empty+ mplus = (<|>) instance (Functor m, Monad m) => Applicative (RSST r w s m) where- pure = return+ pure a = RSST $ \_ s -> pure (a, s) (<*>) = ap instance (Functor m, MonadPlus m) => Alternative (RSST r w s m) where- empty = mzero- (<|>) = mplus+ empty = RSST $ \_ _ -> empty+ m <|> n = RSST $ \r s -> runRSST' m r s <|> runRSST' n r s instance (MonadFix m) => MonadFix (RSST r w s m) where mfix f = RSST $ \r s -> mfix $ \ ~(a, _) -> runRSST' (f a) r s@@ -172,4 +179,14 @@ return (a, (s', w `mappend` fw w')) instance (Monoid w, Monad m) => MonadRWS r w s (RSST r w s m)++instance (Monoid w, MonadError e m) => MonadError e (RSST r w s m) where+ throwError = lift . throwError+ catchError = liftCatch catchError++-- | Lift a @catchE@ operation to the new monad.+liftCatch :: Catch e m (a,(s,w)) -> Catch e (RSST r w s m) a+liftCatch catchE m h =+ RSST $ \ r s -> runRSST' m r s `catchE` \ e -> runRSST' (h e) r s+{-# INLINE liftCatch #-}
Control/Monad/Trans/RSS/Strict.hs view
@@ -1,4 +1,7 @@-{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE UndecidableInstances #-} module Control.Monad.Trans.RSS.Strict ( -- * The RWS monad RSS,@@ -13,6 +16,8 @@ evalRSST, execRSST, withRSST,+ -- * Helpers+ liftCatch ) where import Control.Applicative@@ -20,6 +25,8 @@ import Control.Monad.Fix import Control.Monad.IO.Class import Control.Monad.Trans.Class+import Control.Monad.Except+import Control.Monad.Signatures import Data.Functor.Identity import Control.Monad.State@@ -115,60 +122,92 @@ instance (Functor m) => Functor (RSST r w s m) where fmap f m = RSST $ \r s -> fmap (\ (a, (s', w)) -> (f a, (s', w))) $ runRSST' m r s+ {-# INLINE fmap #-} instance (Monad m) => Monad (RSST r w s m) where- return a = RSST $ \_ s -> return (a, s)+ return = pure+ {-# INLINE return #-} m >>= k = RSST $ \r s -> do (a, (s', w)) <- runRSST' m r s runRSST' (k a) r (s',w)+ {-# INLINE (>>=) #-} fail msg = RSST $ \_ _ -> fail msg instance (MonadPlus m) => MonadPlus (RSST r w s m) where- mzero = RSST $ \_ _ -> mzero- m `mplus` n = RSST $ \r s -> runRSST' m r s `mplus` runRSST' n r s+ mzero = empty+ {-# INLINE mzero #-}+ mplus = (<|>)+ {-# INLINE mplus #-} instance (Functor m, Monad m) => Applicative (RSST r w s m) where- pure = return+ pure a = RSST $ \_ s -> pure (a, s)+ {-# INLINE pure #-} (<*>) = ap+ {-# INLINE (<*>) #-} instance (Functor m, MonadPlus m) => Alternative (RSST r w s m) where- empty = mzero- (<|>) = mplus+ empty = RSST $ \_ _ -> empty+ {-# INLINE empty #-}+ m <|> n = RSST $ \r s -> runRSST' m r s <|> runRSST' n r s+ {-# INLINE (<|>) #-} instance (MonadFix m) => MonadFix (RSST r w s m) where mfix f = RSST $ \r s -> mfix $ \ ~(a, _) -> runRSST' (f a) r s+ {-# INLINE mfix #-} instance MonadTrans (RSST r w s) where lift m = RSST $ \_ s -> do a <- m return (a, s)+ {-# INLINE lift #-} instance (MonadIO m) => MonadIO (RSST r w s m) where liftIO = lift . liftIO+ {-# INLINE liftIO #-} instance Monad m => MonadState s (RSST r w s m) where get = RSST $ \_ (s,w) -> return (s,(s,w))+ {-# INLINE get #-} put ns = RSST $ \_ (_,w) -> return ((),(ns,w))+ {-# INLINE put #-} state f = RSST $ \_ (s,w) -> case f s of (a,s') -> return (a, (s', w))+ {-# INLINE state #-} instance Monad m => MonadReader r (RSST r w s m) where ask = RSST $ \r s -> return (r, s)+ {-# INLINE ask #-} local f rw = RSST $ \r s -> runRSST' rw (f r) s+ {-# INLINE local #-} reader f = RSST $ \r s -> return (f r, s)+ {-# INLINE reader #-} instance (Monoid w, Monad m) => MonadWriter w (RSST r w s m) where writer (a,w) = tell w >> return a+ {-# INLINE writer #-} tell w = RSST $ \_ (s, ow) -> let nw = ow `mappend` w in nw `seq` return ((), (s, nw))+ {-# INLINE tell #-} listen rw = RSST $ \r (s, w) -> do (a, (ns, nw)) <- runRSST' rw r (s,mempty) let ow = w `mappend` nw ow `seq` return ((a, nw), (ns, ow))+ {-# INLINE listen #-} pass rw = RSST $ \r (s, w) -> do ( (a, fw), (s', w') ) <- runRSST' rw r (s, mempty) return (a, (s', w `mappend` fw w'))+ {-# INLINE pass #-} instance (Monoid w, Monad m) => MonadRWS r w s (RSST r w s m)++instance (Monoid w, MonadError e m) => MonadError e (RSST r w s m) where+ throwError = lift . throwError+ catchError = liftCatch catchError++-- | Lift a @catchE@ operation to the new monad.+liftCatch :: Catch e m (a,(s,w)) -> Catch e (RSST r w s m) a+liftCatch catchE m h =+ RSST $ \ r s -> runRSST' m r s `catchE` \ e -> runRSST' (h e) r s+{-# INLINE liftCatch #-}
stateWriter.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: stateWriter-version: 0.2.9+version: 0.2.10 synopsis: A faster variant of the RWS monad transformers. description: This is a version of the RWS monad transformers that should be much faster than what's found in transformers. The writer in the strict version does not leak memory. license: BSD3