transformers (empty) → 0.0.0.0
raw patch · 18 files changed
+1930/−0 lines, 18 filesdep +basesetup-changed
Dependencies added: base
Files
- Control/Monad/Identity.hs +92/−0
- Control/Monad/Trans.hs +42/−0
- Control/Monad/Trans/Cont.hs +108/−0
- Control/Monad/Trans/Error.hs +204/−0
- Control/Monad/Trans/List.hs +73/−0
- Control/Monad/Trans/RWS.hs +26/−0
- Control/Monad/Trans/RWS/Lazy.hs +210/−0
- Control/Monad/Trans/RWS/Strict.hs +210/−0
- Control/Monad/Trans/Reader.hs +124/−0
- Control/Monad/Trans/State.hs +26/−0
- Control/Monad/Trans/State/Lazy.hs +226/−0
- Control/Monad/Trans/State/Strict.hs +226/−0
- Control/Monad/Trans/Writer.hs +26/−0
- Control/Monad/Trans/Writer/Lazy.hs +135/−0
- Control/Monad/Trans/Writer/Strict.hs +135/−0
- LICENSE +31/−0
- Setup.hs +2/−0
- transformers.cabal +34/−0
+ Control/Monad/Identity.hs view
@@ -0,0 +1,92 @@+{- |+Module : Control.Monad.Identity+Copyright : (c) Andy Gill 2001,+ (c) Oregon Graduate Institute of Science and Technology 2001,+ (c) Jeff Newbern 2003-2006,+ (c) Andriy Palamarchuk 2006+License : BSD-style (see the file libraries/base/LICENSE)++Maintainer : libraries@haskell.org+Stability : experimental+Portability : portable++[Computation type:] Simple function application.++[Binding strategy:] The bound function is applied to the input value.+@'Identity' x >>= f == 'Identity' (f x)@++[Useful for:] Monads can be derived from monad transformers applied to the+'Identity' monad.++[Zero and plus:] None.++[Example type:] @'Identity' a@++The @Identity@ monad is a monad that does not embody any computational strategy.+It simply applies the bound function to its input without any modification.+Computationally, there is no reason to use the @Identity@ monad+instead of the much simpler act of simply applying functions to their arguments.+The purpose of the @Identity@ monad is its fundamental role in the theory+of monad transformers.+Any monad transformer applied to the @Identity@ monad yields a non-transformer+version of that monad.++ Inspired by the paper+ /Functional Programming with Overloading and+ Higher-Order Polymorphism/,+ Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+ Advanced School of Functional Programming, 1995.+-}++module Control.Monad.Identity (+ Identity(..),+ ) where++import Control.Monad+import Control.Monad.Fix++{- | Identity wrapper.+Abstraction for wrapping up a object.+If you have an monadic function, say:++> example :: Int -> Identity Int+> example x = return (x*x)++ you can \"run\" it, using++> Main> runIdentity (example 42)+> 1764 :: Int++A typical use of the Identity monad is to derive a monad+from a monad transformer.++@+-- derive the 'Control.Monad.State.State' monad using the 'Control.Monad.State.StateT' monad transformer+type 'Control.Monad.State.State' s a = 'Control.Monad.State.StateT' s 'Identity' a+@++The @'runIdentity'@ label is used in the type definition because it follows+a style of monad definition that explicitly represents monad values as+computations. In this style, a monadic computation is built up using the monadic+operators and then the value of the computation is extracted+using the @run******@ function.+Because the @Identity@ monad does not do any computation, its definition+is trivial.+For a better example of this style of monad,+see the @'Control.Monad.State.State'@ monad.+-}++newtype Identity a = Identity { runIdentity :: a }++-- ---------------------------------------------------------------------------+-- Identity instances for Functor and Monad++instance Functor Identity where+ fmap f m = Identity (f (runIdentity m))++instance Monad Identity where+ return a = Identity a+ m >>= k = k (runIdentity m)++instance MonadFix Identity where+ mfix f = Identity (fix (runIdentity . f))
+ Control/Monad/Trans.hs view
@@ -0,0 +1,42 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- The MonadTrans class.+--+-- Inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+-- Advanced School of Functional Programming, 1995.+-----------------------------------------------------------------------------++module Control.Monad.Trans (+ MonadTrans(..),+ MonadIO(..),+ ) where++import System.IO++-- ---------------------------------------------------------------------------+-- MonadTrans class+--+-- Monad to facilitate stackable Monads.+-- Provides a way of digging into an outer+-- monad, giving access to (lifting) the inner monad.++class MonadTrans t where+ lift :: Monad m => m a -> t m a++class (Monad m) => MonadIO m where+ liftIO :: IO a -> m a++instance MonadIO IO where+ liftIO = id
+ Control/Monad/Trans/Cont.hs view
@@ -0,0 +1,108 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.Cont+-- Copyright : (c) The University of Glasgow 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Continuation monads.+--+-----------------------------------------------------------------------------++module Control.Monad.Trans.Cont (+ -- * The Cont monad+ Cont,+ runCont,+ mapCont,+ withCont,+ -- * The ContT monad transformer+ ContT(..),+ mapContT,+ withContT,+ callCC,+ -- * Lifting other operations+ liftLocal,+ ) where++import Control.Monad.Identity+import Control.Monad.Trans++import Control.Monad++{- |+Continuation monad.+@Cont r a@ is a CPS computation that produces an intermediate result+of type @a@ within a CPS computation whose final result type is @r@.++The @return@ function simply creates a continuation which passes the value on.++The @>>=@ operator adds the bound function into the continuation chain.+-}+type Cont r = ContT r Identity++-- | Runs a CPS computation, returns its result after applying the final+-- continuation to it.+runCont :: Cont r a -- ^ continuation computation (@Cont@).+ -> (a -> r) -- ^ the final continuation, which produces+ -- the final result (often 'id').+ -> r+runCont m k = runIdentity (runContT m (Identity . k))++mapCont :: (r -> r) -> Cont r a -> Cont r a+mapCont f = mapContT (Identity . f . runIdentity)++withCont :: ((b -> r) -> (a -> r)) -> Cont r a -> Cont r b+withCont f = withContT ((Identity .) . f . (runIdentity .))++{- |+The continuation monad transformer.+Can be used to add continuation handling to other monads.+-}+newtype ContT r m a = ContT { runContT :: (a -> m r) -> m r }++mapContT :: (m r -> m r) -> ContT r m a -> ContT r m a+mapContT f m = ContT $ f . runContT m++withContT :: ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b+withContT f m = ContT $ runContT m . f++instance Functor (ContT r m) where+ fmap f m = ContT $ \c -> runContT m (c . f)++instance (Monad m) => Monad (ContT r m) where+ return a = ContT ($ a)+ m >>= k = ContT $ \c -> runContT m (\a -> runContT (k a) c)++instance MonadTrans (ContT r) where+ lift m = ContT (m >>=)++instance (MonadIO m) => MonadIO (ContT r m) where+ liftIO = lift . liftIO++-- | @callCC@ (call-with-current-continuation) calls its argument+-- function, passing it the current continuation. It provides+-- an escape continuation mechanism for use with continuation+-- monads. Escape continuations one allow to abort the current+-- computation and return a value immediately. They achieve a+-- similar effect to 'Control.Monad.Trans.Error.throwError'+-- and 'Control.Monad.Trans.Error.catchError' within an+-- 'Control.Monad.Trans.Error.ErrorT' monad. The advantage of this+-- function over calling 'return' is that it makes the continuation+-- explicit, allowing more flexibility and better control.+--+-- The standard idiom used with @callCC@ is to provide a lambda-expression+-- to name the continuation. Then calling the named continuation anywhere+-- within its scope will escape from the computation, even if it is many+-- layers deep within nested computations.+callCC :: ((a -> ContT r m b) -> ContT r m a) -> ContT r m a+callCC f = ContT $ \c -> runContT (f (\a -> ContT $ \_ -> c a)) c++-- | @'liftLocal' ask local@ yields a @local@ function for @'ContT' r m@.+liftLocal :: Monad m => m r' -> ((r' -> r') -> m r -> m r) ->+ (r' -> r') -> ContT r m a -> ContT r m a+liftLocal ask local f m = ContT $ \c -> do+ r <- ask+ local f (runContT m (local (const r) . c))
+ Control/Monad/Trans/Error.hs view
@@ -0,0 +1,204 @@+{- |+Module : Control.Monad.Trans.Error+Copyright : (c) Michael Weber <michael.weber@post.rwth-aachen.de> 2001,+ (c) Jeff Newbern 2003-2006,+ (c) Andriy Palamarchuk 2006+License : BSD-style (see the file libraries/base/LICENSE)++Maintainer : libraries@haskell.org+Stability : experimental+Portability : portable++[Computation type:] Computations which may fail or throw exceptions.++[Binding strategy:] Failure records information about the cause\/location+of the failure. Failure values bypass the bound function,+other values are used as inputs to the bound function.++[Useful for:] Building computations from sequences of functions that may fail+or using exception handling to structure error handling.++[Zero and plus:] Zero is represented by an empty error and the plus operation+executes its second argument if the first fails.++[Example type:] @'Data.Either' String a@++The Error monad (also called the Exception monad).+-}++{-+ Rendered by Michael Weber <mailto:michael.weber@post.rwth-aachen.de>,+ inspired by the Haskell Monad Template Library from+ Andy Gill (<http://www.cse.ogi.edu/~andy/>)+-}+module Control.Monad.Trans.Error (+ -- * The ErrorT monad transformer+ Error(..),+ ErrorList(..),+ ErrorT(..),+ mapErrorT,+ throwError,+ catchError,+ -- * Lifting other operations+ liftCallCC,+ liftListen,+ liftPass,+ ) where++import Control.Exception (IOException)+import Control.Monad+import Control.Monad.Fix+import Control.Monad.Trans++import Control.Monad.Instances ()+import System.IO++instance MonadPlus IO where+ mzero = ioError (userError "mzero")+ m `mplus` n = m `catch` \_ -> n++-- | An exception to be thrown.+-- An instance must redefine at least one of 'noMsg', 'strMsg'.+class Error a where+ -- | Creates an exception without a message.+ -- Default implementation is @'strMsg' \"\"@.+ noMsg :: a+ -- | Creates an exception with a message.+ -- Default implementation is 'noMsg'.+ strMsg :: String -> a++ noMsg = strMsg ""+ strMsg _ = noMsg++-- | A string can be thrown as an error.+instance ErrorList a => Error [a] where+ strMsg = listMsg++instance Error IOException where+ strMsg = userError++-- | Workaround so that we can have a Haskell 98 instance @'Error' 'String'@.+class ErrorList a where+ listMsg :: String -> [a]++instance ErrorList Char where+ listMsg = id++-- ---------------------------------------------------------------------------+-- Our parameterizable error monad++instance (Error e) => Monad (Either e) where+ return = Right+ Left l >>= _ = Left l+ Right r >>= k = k r+ fail msg = Left (strMsg msg)++instance (Error e) => MonadPlus (Either e) where+ mzero = Left noMsg+ Left _ `mplus` n = n+ m `mplus` _ = m++instance (Error e) => MonadFix (Either e) where+ mfix f = let+ a = f $ case a of+ Right r -> r+ _ -> error "empty mfix argument"+ in a++{- |+The error monad transformer. It can be used to add error handling to other+monads.++The @ErrorT@ Monad structure is parameterized over two things:++ * e - The error type.++ * m - The inner monad.++Here are some examples of use:++> -- wraps IO action that can throw an error e+> type ErrorWithIO e a = ErrorT e IO a+> ==> ErrorT (IO (Either e a))+>+> -- IO monad wrapped in StateT inside of ErrorT+> type ErrorAndStateWithIO e s a = ErrorT e (StateT s IO) a+> ==> ErrorT (StateT s IO (Either e a))+> ==> ErrorT (StateT (s -> IO (Either e a,s)))+-}++newtype ErrorT e m a = ErrorT { runErrorT :: m (Either e a) }++mapErrorT :: (m (Either e a) -> n (Either e' b))+ -> ErrorT e m a+ -> ErrorT e' n b+mapErrorT f m = ErrorT $ f (runErrorT m)++instance (Monad m) => Functor (ErrorT e m) where+ fmap f = ErrorT . liftM (fmap f) . runErrorT++instance (Monad m, Error e) => Monad (ErrorT e m) where+ return a = ErrorT $ return (Right a)+ m >>= k = ErrorT $ do+ a <- runErrorT m+ case a of+ Left l -> return (Left l)+ Right r -> runErrorT (k r)+ fail msg = ErrorT $ return (Left (strMsg msg))++instance (Monad m, Error e) => MonadPlus (ErrorT e m) where+ mzero = ErrorT $ return (Left noMsg)+ m `mplus` n = ErrorT $ do+ a <- runErrorT m+ case a of+ Left _ -> runErrorT n+ Right r -> return (Right r)++instance (MonadFix m, Error e) => MonadFix (ErrorT e m) where+ mfix f = ErrorT $ mfix $ \a -> runErrorT $ f $ case a of+ Right r -> r+ _ -> error "empty mfix argument"++instance (Error e) => MonadTrans (ErrorT e) where+ lift m = ErrorT $ do+ a <- m+ return (Right a)++instance (Error e, MonadIO m) => MonadIO (ErrorT e m) where+ liftIO = lift . liftIO++-- | Signal an error+throwError :: (Monad m, Error e) => e -> ErrorT e m a+throwError l = ErrorT $ return (Left l)++-- | Handle an error+catchError :: (Monad m, Error e) =>+ ErrorT e m a -> (e -> ErrorT e m a) -> ErrorT e m a+m `catchError` h = ErrorT $ do+ a <- runErrorT m+ case a of+ Left l -> runErrorT (h l)+ Right r -> return (Right r)++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: (((Either e a -> m (Either e b)) -> m (Either e a)) ->+ m (Either e a)) -> ((a -> ErrorT e m b) -> ErrorT e m a) -> ErrorT e m a+liftCallCC callCC f = ErrorT $+ callCC $ \c ->+ runErrorT (f (\a -> ErrorT $ c (Right a)))++-- | Lift a @listen@ operation to the new monad.+liftListen :: Monad m =>+ (m (Either e a) -> m (Either e a,w)) -> ErrorT e m a -> ErrorT e m (a,w)+liftListen listen = mapErrorT $ \ m -> do+ (a, w) <- listen m+ return $! fmap (\ r -> (r, w)) a++-- | Lift a @pass@ operation to the new monad.+liftPass :: Monad m => (m (Either e a,w -> w) -> m (Either e a)) ->+ ErrorT e m (a,w -> w) -> ErrorT e m a+liftPass pass = mapErrorT $ \ m -> pass $ do+ a <- m+ return $! case a of+ Left l -> (Left l, id)+ Right (r, f) -> (Right r, f)
+ Control/Monad/Trans/List.hs view
@@ -0,0 +1,73 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.List+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- The List monad.+--+-----------------------------------------------------------------------------++module Control.Monad.Trans.List (+ -- * The ListT monad transformer+ ListT(..),+ mapListT,+ -- * Lifting other operations+ liftCallCC,+ liftCatch,+ ) where++import Control.Monad+import Control.Monad.Trans++-- | Parameterizable list monad, with an inner monad.+--+-- /Note:/ this does not yield a monad unless the argument monad is commutative.+newtype ListT m a = ListT { runListT :: m [a] }++mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b+mapListT f m = ListT $ f (runListT m)++instance (Monad m) => Functor (ListT m) where+ fmap f = mapListT $ liftM $ map f++instance (Monad m) => Monad (ListT m) where+ return a = ListT $ return [a]+ m >>= k = ListT $ do+ a <- runListT m+ b <- mapM (runListT . k) a+ return (concat b)+ fail _ = ListT $ return []++instance (Monad m) => MonadPlus (ListT m) where+ mzero = ListT $ return []+ m `mplus` n = ListT $ do+ a <- runListT m+ b <- runListT n+ return (a ++ b)++instance MonadTrans ListT where+ lift m = ListT $ do+ a <- m+ return [a]++instance (MonadIO m) => MonadIO (ListT m) where+ liftIO = lift . liftIO++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: ((([a] -> m [b]) -> m [a]) -> m [a]) ->+ ((a -> ListT m b) -> ListT m a) -> ListT m a+liftCallCC callCC f = ListT $+ callCC $ \c ->+ runListT (f (\a -> ListT $ c [a]))++-- | Lift a @catchError@ operation to the new monad.+liftCatch :: (m [a] -> (e -> m [a]) -> m [a]) ->+ ListT m a -> (e -> ListT m a) -> ListT m a+liftCatch catchError m h = ListT $ runListT m+ `catchError` \e -> runListT (h e)
+ Control/Monad/Trans/RWS.hs view
@@ -0,0 +1,26 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.RWS+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Combination Reader, Writer and State monad transformer.+--+-- Inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+-- Advanced School of Functional Programming, 1995.+-----------------------------------------------------------------------------++module Control.Monad.Trans.RWS (+ module Control.Monad.Trans.RWS.Lazy+ ) where++import Control.Monad.Trans.RWS.Lazy+
+ Control/Monad/Trans/RWS/Lazy.hs view
@@ -0,0 +1,210 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.RWS.Lazy+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Lazy RWS monad.+--+-- Inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+-- Advanced School of Functional Programming, 1995.+-----------------------------------------------------------------------------++module Control.Monad.Trans.RWS.Lazy (+ -- * The RWS monad+ RWS,+ runRWS,+ evalRWS,+ execRWS,+ mapRWS,+ withRWS,+ -- * The RWST monad transformer+ RWST(..),+ evalRWST,+ execRWST,+ mapRWST,+ withRWST,+ -- * Reader operations+ ask,+ local,+ asks,+ -- * Writer operations+ tell,+ listen,+ pass,+ listens,+ censor,+ -- * State operations+ get,+ put,+ modify,+ gets,+ -- * Lifting other operations+ liftCallCC,+ liftCatch,+ ) where++import Control.Monad+import Control.Monad.Fix+import Control.Monad.Identity+import Control.Monad.Trans+import Data.Monoid++type RWS r w s = RWST r w s Identity++runRWS :: RWS r w s a -> r -> s -> (a, s, w)+runRWS m r s = runIdentity (runRWST m r s)++evalRWS :: RWS r w s a -> r -> s -> (a, w)+evalRWS m r s = let+ (a, _, w) = runRWS m r s+ in (a, w)++execRWS :: RWS r w s a -> r -> s -> (s, w)+execRWS m r s = let+ (_, s', w) = runRWS m r s+ in (s', w)++mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b+mapRWS f = mapRWST (Identity . f . runIdentity)++withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a+withRWS = withRWST++-- ---------------------------------------------------------------------------+-- Our parameterizable RWS monad, with an inner monad++newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) }++evalRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (a, w)+evalRWST m r s = do+ ~(a, _, w) <- runRWST m r s+ return (a, w)++execRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (s, w)+execRWST m r s = do+ ~(_, s', w) <- runRWST m r s+ return (s', w)++mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b+mapRWST f m = RWST $ \r s -> f (runRWST m r s)++withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a+withRWST f m = RWST $ \r s -> uncurry (runRWST m) (f r s)++instance (Monad m) => Functor (RWST r w s m) where+ fmap f m = RWST $ \r s -> do+ ~(a, s', w) <- runRWST m r s+ return (f a, s', w)++instance (Monoid w, Monad m) => Monad (RWST r w s m) where+ return a = RWST $ \_ s -> return (a, s, mempty)+ m >>= k = RWST $ \r s -> do+ ~(a, s', w) <- runRWST m r s+ ~(b, s'',w') <- runRWST (k a) r s'+ return (b, s'', w `mappend` w')+ fail msg = RWST $ \_ _ -> fail msg++instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where+ mzero = RWST $ \_ _ -> mzero+ m `mplus` n = RWST $ \r s -> runRWST m r s `mplus` runRWST n r s++instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where+ mfix f = RWST $ \r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s++instance (Monoid w) => MonadTrans (RWST r w s) where+ lift m = RWST $ \_ s -> do+ a <- m+ return (a, s, mempty)++instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where+ liftIO = lift . liftIO++-- ---------------------------------------------------------------------------+-- Reader operations++ask :: (Monoid w, Monad m) => RWST r w s m r+ask = RWST $ \r s -> return (r, s, mempty)++local :: (Monoid w, Monad m) => (r -> r) -> RWST r w s m a -> RWST r w s m a+local f m = RWST $ \r s -> runRWST m (f r) s++asks :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a+asks f = do+ r <- ask+ return (f r)++-- ---------------------------------------------------------------------------+-- Writer operations++tell :: (Monoid w, Monad m) => w -> RWST r w s m ()+tell w = RWST $ \_ s -> return ((),s,w)++listen :: (Monoid w, Monad m) => RWST r w s m a -> RWST r w s m (a, w)+listen m = RWST $ \r s -> do+ ~(a, s', w) <- runRWST m r s+ return ((a, w), s', w)++pass :: (Monoid w, Monad m) => RWST r w s m (a, w -> w) -> RWST r w s m a+pass m = RWST $ \r s -> do+ ~((a, f), s', w) <- runRWST m r s+ return (a, s', f w)++listens :: (Monoid w, Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b)+listens f m = do+ ~(a, w) <- listen m+ return (a, f w)+ +censor :: (Monoid w, Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a+censor f m = pass $ do+ a <- m+ return (a, f)++-- ---------------------------------------------------------------------------+-- State operations++get :: (Monoid w, Monad m) => RWST r w s m s+get = RWST $ \_ s -> return (s, s, mempty)++put :: (Monoid w, Monad m) => s -> RWST r w s m ()+put s = RWST $ \_ _ -> return ((), s, mempty)++-- | Monadic state transformer.+--+-- Maps an old state to a new state inside a state monad.+-- The old state is thrown away.+--+modify :: (Monoid w, Monad m) => (s -> s) -> RWST r w s m ()+modify f = do+ s <- get+ put (f s)+ +-- | Gets specific component of the state, using a projection function+-- supplied.++gets :: (Monoid w, Monad m) => (s -> a) -> RWST r w s m a+gets f = do+ s <- get+ return (f s)++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: (Monoid w) =>+ ((((a,s,w) -> m (b,s,w)) -> m (a,s,w)) -> m (a,s,w)) ->+ ((a -> RWST r w s m b) -> RWST r w s m a) -> RWST r w s m a+liftCallCC callCC f = RWST $ \r s ->+ callCC $ \c ->+ runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s++-- | Lift a @catchError@ operation to the new monad.+liftCatch :: (m (a,s,w) -> (e -> m (a,s,w)) -> m (a,s,w)) ->+ RWST l w s m a -> (e -> RWST l w s m a) -> RWST l w s m a+liftCatch catchError m h =+ RWST $ \r s -> runRWST m r s `catchError` \e -> runRWST (h e) r s
+ Control/Monad/Trans/RWS/Strict.hs view
@@ -0,0 +1,210 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.RWS.Strict+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Strict RWS monad.+--+-- Inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+-- Advanced School of Functional Programming, 1995.+-----------------------------------------------------------------------------++module Control.Monad.Trans.RWS.Strict (+ -- * The RWS monad+ RWS,+ runRWS,+ evalRWS,+ execRWS,+ mapRWS,+ withRWS,+ -- * The RWST monad transformer+ RWST(..),+ evalRWST,+ execRWST,+ mapRWST,+ withRWST,+ -- * Reader operations+ ask,+ local,+ asks,+ -- * Writer operations+ tell,+ listen,+ pass,+ listens,+ censor,+ -- * State operations+ get,+ put,+ modify,+ gets,+ -- * Lifting other operations+ liftCallCC,+ liftCatch,+ ) where++import Control.Monad+import Control.Monad.Fix+import Control.Monad.Identity+import Control.Monad.Trans+import Data.Monoid++type RWS r w s = RWST r w s Identity++runRWS :: RWS r w s a -> r -> s -> (a, s, w)+runRWS m r s = runIdentity (runRWST m r s)++evalRWS :: RWS r w s a -> r -> s -> (a, w)+evalRWS m r s = let+ (a, _, w) = runRWS m r s+ in (a, w)++execRWS :: RWS r w s a -> r -> s -> (s, w)+execRWS m r s = let+ (_, s', w) = runRWS m r s+ in (s', w)++mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b+mapRWS f = mapRWST (Identity . f . runIdentity)++withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a+withRWS = withRWST++-- ---------------------------------------------------------------------------+-- Our parameterizable RWS monad, with an inner monad++newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) }++evalRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (a, w)+evalRWST m r s = do+ (a, _, w) <- runRWST m r s+ return (a, w)++execRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (s, w)+execRWST m r s = do+ (_, s', w) <- runRWST m r s+ return (s', w)++mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b+mapRWST f m = RWST $ \r s -> f (runRWST m r s)++withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a+withRWST f m = RWST $ \r s -> uncurry (runRWST m) (f r s)++instance (Monad m) => Functor (RWST r w s m) where+ fmap f m = RWST $ \r s -> do+ (a, s', w) <- runRWST m r s+ return (f a, s', w)++instance (Monoid w, Monad m) => Monad (RWST r w s m) where+ return a = RWST $ \_ s -> return (a, s, mempty)+ m >>= k = RWST $ \r s -> do+ (a, s', w) <- runRWST m r s+ (b, s'',w') <- runRWST (k a) r s'+ return (b, s'', w `mappend` w')+ fail msg = RWST $ \_ _ -> fail msg++instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where+ mzero = RWST $ \_ _ -> mzero+ m `mplus` n = RWST $ \r s -> runRWST m r s `mplus` runRWST n r s++instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where+ mfix f = RWST $ \r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s++instance (Monoid w) => MonadTrans (RWST r w s) where+ lift m = RWST $ \_ s -> do+ a <- m+ return (a, s, mempty)++instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where+ liftIO = lift . liftIO++-- ---------------------------------------------------------------------------+-- Reader operations++ask :: (Monoid w, Monad m) => RWST r w s m r+ask = RWST $ \r s -> return (r, s, mempty)++local :: (Monoid w, Monad m) => (r -> r) -> RWST r w s m a -> RWST r w s m a+local f m = RWST $ \r s -> runRWST m (f r) s++asks :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a+asks f = do+ r <- ask+ return (f r)++-- ---------------------------------------------------------------------------+-- Writer operations++tell :: (Monoid w, Monad m) => w -> RWST r w s m ()+tell w = RWST $ \_ s -> return ((),s,w)++listen :: (Monoid w, Monad m) => RWST r w s m a -> RWST r w s m (a, w)+listen m = RWST $ \r s -> do+ (a, s', w) <- runRWST m r s+ return ((a, w), s', w)++pass :: (Monoid w, Monad m) => RWST r w s m (a, w -> w) -> RWST r w s m a+pass m = RWST $ \r s -> do+ ((a, f), s', w) <- runRWST m r s+ return (a, s', f w)++listens :: (Monoid w, Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b)+listens f m = do+ (a, w) <- listen m+ return (a, f w)+ +censor :: (Monoid w, Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a+censor f m = pass $ do+ a <- m+ return (a, f)++-- ---------------------------------------------------------------------------+-- State operations++get :: (Monoid w, Monad m) => RWST r w s m s+get = RWST $ \_ s -> return (s, s, mempty)++put :: (Monoid w, Monad m) => s -> RWST r w s m ()+put s = RWST $ \_ _ -> return ((), s, mempty)++-- | Monadic state transformer.+--+-- Maps an old state to a new state inside a state monad.+-- The old state is thrown away.+--+modify :: (Monoid w, Monad m) => (s -> s) -> RWST r w s m ()+modify f = do+ s <- get+ put (f s)+ +-- | Gets specific component of the state, using a projection function+-- supplied.++gets :: (Monoid w, Monad m) => (s -> a) -> RWST r w s m a+gets f = do+ s <- get+ return (f s)++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: (Monoid w) =>+ ((((a,s,w) -> m (b,s,w)) -> m (a,s,w)) -> m (a,s,w)) ->+ ((a -> RWST r w s m b) -> RWST r w s m a) -> RWST r w s m a+liftCallCC callCC f = RWST $ \r s ->+ callCC $ \c ->+ runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s++-- | Lift a @catchError@ operation to the new monad.+liftCatch :: (m (a,s,w) -> (e -> m (a,s,w)) -> m (a,s,w)) ->+ RWST l w s m a -> (e -> RWST l w s m a) -> RWST l w s m a+liftCatch catchError m h =+ RWST $ \r s -> runRWST m r s `catchError` \e -> runRWST (h e) r s
+ Control/Monad/Trans/Reader.hs view
@@ -0,0 +1,124 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.Reader+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Declaration of the MonadReader class+--+-- Inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+-- Advanced School of Functional Programming, 1995.+-----------------------------------------------------------------------------++module Control.Monad.Trans.Reader (+ -- * The Reader monad+ Reader,+ runReader,+ mapReader,+ withReader,+ -- * The ReaderT monad transformer+ ReaderT(..),+ mapReaderT,+ withReaderT,+ -- * Reader operations+ ask,+ local,+ asks,+ -- * Lifting other operations+ liftCallCC,+ liftCatch,+ ) where++import Control.Monad.Identity+import Control.Monad.Trans++import Control.Monad+import Control.Monad.Fix+import Control.Monad.Instances ()++-- | The parameterizable reader monad.+--+-- The 'return' function creates a @Reader@ that ignores the environment,+-- and produces the given value.+--+-- The binding operator @>>=@ produces a @Reader@ that uses the+-- environment to extract the value its left-hand side, and then applies+-- the bound function to that value in the same environment.+type Reader r = ReaderT r Identity++-- | Runs @Reader@ and extracts the final value from it.+runReader :: Reader r a -- ^ A @Reader@ to run.+ -> r -- ^ An initial environment.+ -> a+runReader m = runIdentity . runReaderT m++mapReader :: (a -> b) -> Reader r a -> Reader r b+mapReader f = mapReaderT (Identity . f . runIdentity)++-- | A more general version of 'local'.+withReader :: (r' -> r) -> Reader r a -> Reader r' a+withReader = withReaderT++-- | The reader monad transformer.+-- Can be used to add environment reading functionality to other monads.+newtype ReaderT r m a = ReaderT { runReaderT :: r -> m a }++mapReaderT :: (m a -> n b) -> ReaderT w m a -> ReaderT w n b+mapReaderT f m = ReaderT $ f . runReaderT m++withReaderT :: (r' -> r) -> ReaderT r m a -> ReaderT r' m a+withReaderT f m = ReaderT $ runReaderT m . f++instance (Monad m) => Functor (ReaderT r m) where+ fmap f m = ReaderT (liftM f . runReaderT m)++instance (Monad m) => Monad (ReaderT r m) where+ return a = ReaderT $ \_ -> return a+ m >>= k = ReaderT $ \r -> do+ a <- runReaderT m r+ runReaderT (k a) r+ fail msg = ReaderT $ \_ -> fail msg++instance (MonadPlus m) => MonadPlus (ReaderT r m) where+ mzero = ReaderT $ \_ -> mzero+ m `mplus` n = ReaderT $ \r -> runReaderT m r `mplus` runReaderT n r++instance (MonadFix m) => MonadFix (ReaderT r m) where+ mfix f = ReaderT $ \r -> mfix $ \a -> runReaderT (f a) r++instance MonadTrans (ReaderT r) where+ lift m = ReaderT $ \_ -> m++instance (MonadIO m) => MonadIO (ReaderT r m) where+ liftIO = lift . liftIO++ask :: (Monad m) => ReaderT r m r+ask = ReaderT return++local :: (Monad m) => (r -> r) -> ReaderT r m a -> ReaderT r m a+local f m = ReaderT $ \r -> runReaderT m (f r)++asks :: (Monad m) => (r -> a) -> ReaderT r m a+asks f = do+ r <- ask+ return (f r)++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: (((a -> m b) -> m a) -> m a) ->+ ((a -> ReaderT r m b) -> ReaderT r m a) -> ReaderT r m a+liftCallCC callCC f = ReaderT $ \r ->+ callCC $ \c ->+ runReaderT (f (\a -> ReaderT $ \_ -> c a)) r++-- | Lift a @catchError@ operation to the new monad.+liftCatch :: (m a -> (e -> m a) -> m a) ->+ ReaderT r m a -> (e -> ReaderT r m a) -> ReaderT r m a+liftCatch f m h = ReaderT $ \r -> f (runReaderT m r) (\e -> runReaderT (h e) r)
+ Control/Monad/Trans/State.hs view
@@ -0,0 +1,26 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.State+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- State monads.+--+-- This module is inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+-- Advanced School of Functional Programming, 1995.++-----------------------------------------------------------------------------++module Control.Monad.Trans.State (+ module Control.Monad.Trans.State.Lazy+ ) where++import Control.Monad.Trans.State.Lazy
+ Control/Monad/Trans/State/Lazy.hs view
@@ -0,0 +1,226 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.State.Lazy+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Lazy state monads.+--+-- This module is inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+-- Advanced School of Functional Programming, 1995.+--+-- See below for examples.++-----------------------------------------------------------------------------++module Control.Monad.Trans.State.Lazy (+ -- * The State monad+ State,+ runState,+ evalState,+ execState,+ mapState,+ withState,+ -- * The StateT monad transformer+ StateT(..),+ evalStateT,+ execStateT,+ mapStateT,+ withStateT,+ -- * State operations+ get,+ put,+ modify,+ gets,+ -- * Lifting other operations+ liftCallCC,+ liftCatch,+ liftListen,+ liftPass,+ ) where++import Control.Monad+import Control.Monad.Fix+import Control.Monad.Identity+import Control.Monad.Trans++-- ---------------------------------------------------------------------------+-- | A parameterizable state monad where /s/ is the type of the state+-- to carry and /a/ is the type of the /return value/.++type State s = StateT s Identity++runState :: State s a -> s -> (a, s)+runState m = runIdentity . runStateT m++-- |Evaluate this state monad with the given initial state,throwing+-- away the final state. Very much like @fst@ composed with+-- @runstate@.++evalState :: State s a -- ^The state to evaluate+ -> s -- ^An initial value+ -> a -- ^The return value of the state application+evalState m s = fst (runState m s)++-- |Execute this state and return the new state, throwing away the+-- return value. Very much like @snd@ composed with+-- @runstate@.++execState :: State s a -- ^The state to evaluate+ -> s -- ^An initial value+ -> s -- ^The new state+execState m s = snd (runState m s)++-- |Map a stateful computation from one (return value, state) pair to+-- another. For instance, to convert numberTree from a function that+-- returns a tree to a function that returns the sum of the numbered+-- tree (see the Examples section for numberTree and sumTree) you may+-- write:+--+-- > sumNumberedTree :: (Eq a) => Tree a -> State (Table a) Int+-- > sumNumberedTree = mapState (\ (t, tab) -> (sumTree t, tab)) . numberTree++mapState :: ((a, s) -> (b, s)) -> State s a -> State s b+mapState f = mapStateT (Identity . f . runIdentity)++-- |Apply this function to this state and return the resulting state.+withState :: (s -> s) -> State s a -> State s a+withState = withStateT++-- ---------------------------------------------------------------------------+-- | A parameterizable state monad for encapsulating an inner+-- monad.+--+-- The StateT Monad structure is parameterized over two things:+--+-- * s - The state.+--+-- * m - The inner monad.+--+-- Here are some examples of use:+--+-- (Parser from ParseLib with Hugs)+--+-- > type Parser a = StateT String [] a+-- > ==> StateT (String -> [(a,String)])+--+-- For example, item can be written as:+--+-- > item = do (x:xs) <- get+-- > put xs+-- > return x+-- >+-- > type BoringState s a = StateT s Identity a+-- > ==> StateT (s -> Identity (a,s))+-- >+-- > type StateWithIO s a = StateT s IO a+-- > ==> StateT (s -> IO (a,s))+-- >+-- > type StateWithErr s a = StateT s Maybe a+-- > ==> StateT (s -> Maybe (a,s))++newtype StateT s m a = StateT { runStateT :: s -> m (a,s) }++-- |Similar to 'evalState'+evalStateT :: (Monad m) => StateT s m a -> s -> m a+evalStateT m s = do+ ~(a, _) <- runStateT m s+ return a++-- |Similar to 'execState'+execStateT :: (Monad m) => StateT s m a -> s -> m s+execStateT m s = do+ ~(_, s') <- runStateT m s+ return s'++-- |Similar to 'mapState'+mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b+mapStateT f m = StateT $ f . runStateT m++-- |Similar to 'withState'+withStateT :: (s -> s) -> StateT s m a -> StateT s m a+withStateT f m = StateT $ runStateT m . f++instance (Monad m) => Functor (StateT s m) where+ fmap f = mapStateT $ liftM $ \ ~(x, s') -> (f x, s')++instance (Monad m) => Monad (StateT s m) where+ return a = StateT $ \s -> return (a, s)+ m >>= k = StateT $ \s -> do+ ~(a, s') <- runStateT m s+ runStateT (k a) s'+ fail str = StateT $ \_ -> fail str++instance (MonadPlus m) => MonadPlus (StateT s m) where+ mzero = StateT $ \_ -> mzero+ m `mplus` n = StateT $ \s -> runStateT m s `mplus` runStateT n s++instance (MonadFix m) => MonadFix (StateT s m) where+ mfix f = StateT $ \s -> mfix $ \ ~(a, _) -> runStateT (f a) s++instance MonadTrans (StateT s) where+ lift m = StateT $ \s -> do+ a <- m+ return (a, s)++instance (MonadIO m) => MonadIO (StateT s m) where+ liftIO = lift . liftIO++get :: (Monad m) => StateT s m s+get = StateT $ \s -> return (s, s)++put :: (Monad m) => s -> StateT s m ()+put s = StateT $ \_ -> return ((), s)++-- | Monadic state transformer.+--+-- Maps an old state to a new state inside a state monad.+-- The old state is thrown away.++modify :: (Monad m) => (s -> s) -> StateT s m ()+modify f = do+ s <- get+ put (f s)++-- | Gets specific component of the state, using a projection function+-- supplied.++gets :: (Monad m) => (s -> a) -> StateT s m a+gets f = do+ s <- get+ return (f s)++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: ((((a,s) -> m (b,s)) -> m (a,s)) -> m (a,s)) ->+ ((a -> StateT s m b) -> StateT s m a) -> StateT s m a+liftCallCC callCC f = StateT $ \s ->+ callCC $ \c ->+ runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s++-- | Lift a @catchError@ operation to the new monad.+liftCatch :: (m (a,s) -> (e -> m (a,s)) -> m (a,s)) ->+ StateT s m a -> (e -> StateT s m a) -> StateT s m a+liftCatch catchError m h =+ StateT $ \s -> runStateT m s `catchError` \e -> runStateT (h e) s++-- | Lift a @listen@ operation to the new monad.+liftListen :: Monad m =>+ (m (a,s) -> m ((a,s),w)) -> StateT s m a -> StateT s m (a,w)+liftListen listen m = StateT $ \s -> do+ ~((a, s'), w) <- listen (runStateT m s)+ return ((a, w), s')++-- | Lift a @pass@ operation to the new monad.+liftPass :: Monad m =>+ (m ((a,s),b) -> m (a,s)) -> StateT s m (a,b) -> StateT s m a+liftPass pass m = StateT $ \s -> pass $ do+ ~((a, f), s') <- runStateT m s+ return ((a, s'), f)
+ Control/Monad/Trans/State/Strict.hs view
@@ -0,0 +1,226 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.State.Strict+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Strict state monads.+--+-- This module is inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)+-- Advanced School of Functional Programming, 1995.+--+-- See below for examples.++-----------------------------------------------------------------------------++module Control.Monad.Trans.State.Strict (+ -- * The State monad+ State,+ runState,+ evalState,+ execState,+ mapState,+ withState,+ -- * The StateT monad transformer+ StateT(..),+ evalStateT,+ execStateT,+ mapStateT,+ withStateT,+ -- * State operations+ get,+ put,+ modify,+ gets,+ -- * Lifting other operations+ liftCallCC,+ liftCatch,+ liftListen,+ liftPass,+ ) where++import Control.Monad+import Control.Monad.Fix+import Control.Monad.Identity+import Control.Monad.Trans++-- ---------------------------------------------------------------------------+-- | A parameterizable state monad where /s/ is the type of the state+-- to carry and /a/ is the type of the /return value/.++type State s = StateT s Identity++runState :: State s a -> s -> (a, s)+runState m = runIdentity . runStateT m++-- |Evaluate this state monad with the given initial state,throwing+-- away the final state. Very much like @fst@ composed with+-- @runstate@.++evalState :: State s a -- ^The state to evaluate+ -> s -- ^An initial value+ -> a -- ^The return value of the state application+evalState m s = fst (runState m s)++-- |Execute this state and return the new state, throwing away the+-- return value. Very much like @snd@ composed with+-- @runstate@.++execState :: State s a -- ^The state to evaluate+ -> s -- ^An initial value+ -> s -- ^The new state+execState m s = snd (runState m s)++-- |Map a stateful computation from one (return value, state) pair to+-- another. For instance, to convert numberTree from a function that+-- returns a tree to a function that returns the sum of the numbered+-- tree (see the Examples section for numberTree and sumTree) you may+-- write:+--+-- > sumNumberedTree :: (Eq a) => Tree a -> State (Table a) Int+-- > sumNumberedTree = mapState (\ (t, tab) -> (sumTree t, tab)) . numberTree++mapState :: ((a, s) -> (b, s)) -> State s a -> State s b+mapState f = mapStateT (Identity . f . runIdentity)++-- |Apply this function to this state and return the resulting state.+withState :: (s -> s) -> State s a -> State s a+withState = withStateT++-- ---------------------------------------------------------------------------+-- | A parameterizable state monad for encapsulating an inner+-- monad.+--+-- The StateT Monad structure is parameterized over two things:+--+-- * s - The state.+--+-- * m - The inner monad.+--+-- Here are some examples of use:+--+-- (Parser from ParseLib with Hugs)+--+-- > type Parser a = StateT String [] a+-- > ==> StateT (String -> [(a,String)])+--+-- For example, item can be written as:+--+-- > item = do (x:xs) <- get+-- > put xs+-- > return x+-- >+-- > type BoringState s a = StateT s Identity a+-- > ==> StateT (s -> Identity (a,s))+-- >+-- > type StateWithIO s a = StateT s IO a+-- > ==> StateT (s -> IO (a,s))+-- >+-- > type StateWithErr s a = StateT s Maybe a+-- > ==> StateT (s -> Maybe (a,s))++newtype StateT s m a = StateT { runStateT :: s -> m (a,s) }++-- |Similar to 'evalState'+evalStateT :: (Monad m) => StateT s m a -> s -> m a+evalStateT m s = do+ (a, _) <- runStateT m s+ return a++-- |Similar to 'execState'+execStateT :: (Monad m) => StateT s m a -> s -> m s+execStateT m s = do+ (_, s') <- runStateT m s+ return s'++-- |Similar to 'mapState'+mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b+mapStateT f m = StateT $ f . runStateT m++-- |Similar to 'withState'+withStateT :: (s -> s) -> StateT s m a -> StateT s m a+withStateT f m = StateT $ runStateT m . f++instance (Monad m) => Functor (StateT s m) where+ fmap f = mapStateT $ liftM $ \ (x, s') -> (f x, s')++instance (Monad m) => Monad (StateT s m) where+ return a = StateT $ \s -> return (a, s)+ m >>= k = StateT $ \s -> do+ (a, s') <- runStateT m s+ runStateT (k a) s'+ fail str = StateT $ \_ -> fail str++instance (MonadPlus m) => MonadPlus (StateT s m) where+ mzero = StateT $ \_ -> mzero+ m `mplus` n = StateT $ \s -> runStateT m s `mplus` runStateT n s++instance (MonadFix m) => MonadFix (StateT s m) where+ mfix f = StateT $ \s -> mfix $ \ ~(a, _) -> runStateT (f a) s++instance MonadTrans (StateT s) where+ lift m = StateT $ \s -> do+ a <- m+ return (a, s)++instance (MonadIO m) => MonadIO (StateT s m) where+ liftIO = lift . liftIO++get :: (Monad m) => StateT s m s+get = StateT $ \s -> return (s, s)++put :: (Monad m) => s -> StateT s m ()+put s = StateT $ \_ -> return ((), s)++-- | Monadic state transformer.+--+-- Maps an old state to a new state inside a state monad.+-- The old state is thrown away.++modify :: (Monad m) => (s -> s) -> StateT s m ()+modify f = do+ s <- get+ put (f s)++-- | Gets specific component of the state, using a projection function+-- supplied.++gets :: (Monad m) => (s -> a) -> StateT s m a+gets f = do+ s <- get+ return (f s)++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: ((((a,s) -> m (b,s)) -> m (a,s)) -> m (a,s)) ->+ ((a -> StateT s m b) -> StateT s m a) -> StateT s m a+liftCallCC callCC f = StateT $ \s ->+ callCC $ \c ->+ runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s++-- | Lift a @catchError@ operation to the new monad.+liftCatch :: (m (a,s) -> (e -> m (a,s)) -> m (a,s)) ->+ StateT s m a -> (e -> StateT s m a) -> StateT s m a+liftCatch catchError m h =+ StateT $ \s -> runStateT m s `catchError` \e -> runStateT (h e) s++-- | Lift a @listen@ operation to the new monad.+liftListen :: Monad m =>+ (m (a,s) -> m ((a,s),w)) -> StateT s m a -> StateT s m (a,w)+liftListen listen m = StateT $ \s -> do+ ((a, s'), w) <- listen (runStateT m s)+ return ((a, w), s')++-- | Lift a @pass@ operation to the new monad.+liftPass :: Monad m =>+ (m ((a,s),b) -> m (a,s)) -> StateT s m (a,b) -> StateT s m a+liftPass pass m = StateT $ \s -> pass $ do+ ((a, f), s') <- runStateT m s+ return ((a, s'), f)
+ Control/Monad/Trans/Writer.hs view
@@ -0,0 +1,26 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.Writer+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- The WriterT monad transformer.+--+-- Inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)+-- Advanced School of Functional Programming, 1995.+-----------------------------------------------------------------------------++module Control.Monad.Trans.Writer (+ module Control.Monad.Trans.Writer.Lazy+ ) where++import Control.Monad.Trans.Writer.Lazy+
+ Control/Monad/Trans/Writer/Lazy.hs view
@@ -0,0 +1,135 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.Writer.Lazy+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Lazy writer monads.+--+-- Inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)+-- Advanced School of Functional Programming, 1995.+-----------------------------------------------------------------------------++module Control.Monad.Trans.Writer.Lazy (+ -- * The Writer monad+ Writer,+ runWriter,+ execWriter,+ mapWriter,+ -- * The WriterT monad transformer+ WriterT(..),+ execWriterT,+ mapWriterT,+ -- * Writer operations+ tell,+ listen,+ pass,+ listens,+ censor,+ -- * Lifting other operations+ liftCallCC,+ liftCatch,+ ) where++import Control.Monad+import Control.Monad.Fix+import Control.Monad.Identity+import Control.Monad.Trans+import Data.Monoid++-- ---------------------------------------------------------------------------+-- Our parameterizable writer monad++type Writer w = WriterT w Identity++runWriter :: Writer w a -> (a, w)+runWriter = runIdentity . runWriterT++execWriter :: Writer w a -> w+execWriter m = snd (runWriter m)++mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b+mapWriter f = mapWriterT (Identity . f . runIdentity)++-- ---------------------------------------------------------------------------+-- Our parameterizable writer monad, with an inner monad++newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }++execWriterT :: Monad m => WriterT w m a -> m w+execWriterT m = do+ ~(_, w) <- runWriterT m+ return w++mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b+mapWriterT f m = WriterT $ f (runWriterT m)++instance (Monad m) => Functor (WriterT w m) where+ fmap f = mapWriterT $ liftM $ \ ~(a, w) -> (f a, w)++instance (Monoid w, Monad m) => Monad (WriterT w m) where+ return a = WriterT $ return (a, mempty)+ m >>= k = WriterT $ do+ ~(a, w) <- runWriterT m+ ~(b, w') <- runWriterT (k a)+ return (b, w `mappend` w')+ fail msg = WriterT $ fail msg++instance (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) where+ mzero = WriterT mzero+ m `mplus` n = WriterT $ runWriterT m `mplus` runWriterT n++instance (Monoid w, MonadFix m) => MonadFix (WriterT w m) where+ mfix m = WriterT $ mfix $ \ ~(a, _) -> runWriterT (m a)++instance (Monoid w) => MonadTrans (WriterT w) where+ lift m = WriterT $ do+ a <- m+ return (a, mempty)++instance (Monoid w, MonadIO m) => MonadIO (WriterT w m) where+ liftIO = lift . liftIO++tell :: (Monoid w, Monad m) => w -> WriterT w m ()+tell w = WriterT $ return ((), w)++listen :: (Monoid w, Monad m) => WriterT w m a -> WriterT w m (a, w)+listen m = WriterT $ do+ ~(a, w) <- runWriterT m+ return ((a, w), w)++pass :: (Monoid w, Monad m) => WriterT w m (a, w -> w) -> WriterT w m a+pass m = WriterT $ do+ ~((a, f), w) <- runWriterT m+ return (a, f w)++listens :: (Monoid w, Monad m) => (w -> b) -> WriterT w m a -> WriterT w m (a, b)+listens f m = do+ ~(a, w) <- listen m+ return (a, f w)++censor :: (Monoid w, Monad m) => (w -> w) -> WriterT w m a -> WriterT w m a+censor f m = pass $ do+ a <- m+ return (a, f)++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: (Monoid w) => ((((a,w) -> m (b,w)) -> m (a,w)) -> m (a,w)) ->+ ((a -> WriterT w m b) -> WriterT w m a) -> WriterT w m a+liftCallCC callCC f = WriterT $+ callCC $ \c ->+ runWriterT (f (\a -> WriterT $ c (a, mempty)))++-- | Lift a @catchError@ operation to the new monad.+liftCatch :: (m (a,w) -> (e -> m (a,w)) -> m (a,w)) ->+ WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a+liftCatch catchError m h =+ WriterT $ runWriterT m `catchError` \e -> runWriterT (h e)
+ Control/Monad/Trans/Writer/Strict.hs view
@@ -0,0 +1,135 @@+-----------------------------------------------------------------------------+-- |+-- Module : Control.Monad.Trans.Writer.Strict+-- Copyright : (c) Andy Gill 2001,+-- (c) Oregon Graduate Institute of Science and Technology, 2001+-- License : BSD-style (see the file libraries/base/LICENSE)+--+-- Maintainer : libraries@haskell.org+-- Stability : experimental+-- Portability : portable+--+-- Strict writer monads.+--+-- Inspired by the paper+-- /Functional Programming with Overloading and+-- Higher-Order Polymorphism/,+-- Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)+-- Advanced School of Functional Programming, 1995.+-----------------------------------------------------------------------------++module Control.Monad.Trans.Writer.Strict (+ -- * The Writer monad+ Writer,+ runWriter,+ execWriter,+ mapWriter,+ -- * The WriterT monad transformer+ WriterT(..),+ execWriterT,+ mapWriterT,+ -- * Writer operations+ tell,+ listen,+ pass,+ listens,+ censor,+ -- * Lifting other operations+ liftCallCC,+ liftCatch,+ ) where++import Control.Monad+import Control.Monad.Fix+import Control.Monad.Identity+import Control.Monad.Trans+import Data.Monoid++-- ---------------------------------------------------------------------------+-- Our parameterizable writer monad++type Writer w = WriterT w Identity++runWriter :: Writer w a -> (a, w)+runWriter = runIdentity . runWriterT++execWriter :: Writer w a -> w+execWriter m = snd (runWriter m)++mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b+mapWriter f = mapWriterT (Identity . f . runIdentity)++-- ---------------------------------------------------------------------------+-- Our parameterizable writer monad, with an inner monad++newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }++execWriterT :: Monad m => WriterT w m a -> m w+execWriterT m = do+ (_, w) <- runWriterT m+ return w++mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b+mapWriterT f m = WriterT $ f (runWriterT m)++instance (Monad m) => Functor (WriterT w m) where+ fmap f = mapWriterT $ liftM $ \ (a, w) -> (f a, w)++instance (Monoid w, Monad m) => Monad (WriterT w m) where+ return a = WriterT $ return (a, mempty)+ m >>= k = WriterT $ do+ (a, w) <- runWriterT m+ (b, w') <- runWriterT (k a)+ return (b, w `mappend` w')+ fail msg = WriterT $ fail msg++instance (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) where+ mzero = WriterT mzero+ m `mplus` n = WriterT $ runWriterT m `mplus` runWriterT n++instance (Monoid w, MonadFix m) => MonadFix (WriterT w m) where+ mfix m = WriterT $ mfix $ \ ~(a, _) -> runWriterT (m a)++instance (Monoid w) => MonadTrans (WriterT w) where+ lift m = WriterT $ do+ a <- m+ return (a, mempty)++instance (Monoid w, MonadIO m) => MonadIO (WriterT w m) where+ liftIO = lift . liftIO++tell :: (Monoid w, Monad m) => w -> WriterT w m ()+tell w = WriterT $ return ((), w)++listen :: (Monoid w, Monad m) => WriterT w m a -> WriterT w m (a, w)+listen m = WriterT $ do+ (a, w) <- runWriterT m+ return ((a, w), w)++pass :: (Monoid w, Monad m) => WriterT w m (a, w -> w) -> WriterT w m a+pass m = WriterT $ do+ ((a, f), w) <- runWriterT m+ return (a, f w)++listens :: (Monoid w, Monad m) => (w -> b) -> WriterT w m a -> WriterT w m (a, b)+listens f m = do+ (a, w) <- listen m+ return (a, f w)++censor :: (Monoid w, Monad m) => (w -> w) -> WriterT w m a -> WriterT w m a+censor f m = pass $ do+ a <- m+ return (a, f)++-- | Lift a @callCC@ operation to the new monad.+liftCallCC :: (Monoid w) => ((((a,w) -> m (b,w)) -> m (a,w)) -> m (a,w)) ->+ ((a -> WriterT w m b) -> WriterT w m a) -> WriterT w m a+liftCallCC callCC f = WriterT $+ callCC $ \c ->+ runWriterT (f (\a -> WriterT $ c (a, mempty)))++-- | Lift a @catchError@ operation to the new monad.+liftCatch :: (m (a,w) -> (e -> m (a,w)) -> m (a,w)) ->+ WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a+liftCatch catchError m h =+ WriterT $ runWriterT m `catchError` \e -> runWriterT (h e)
+ LICENSE view
@@ -0,0 +1,31 @@+The Glasgow Haskell Compiler License++Copyright 2004, The University Court of the University of Glasgow.+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++- Redistributions of source code must retain the above copyright notice,+this list of conditions and the following disclaimer.++- Redistributions in binary form must reproduce the above copyright notice,+this list of conditions and the following disclaimer in the documentation+and/or other materials provided with the distribution.++- Neither name of the University nor the names of its contributors may be+used to endorse or promote products derived from this software without+specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY COURT OF THE UNIVERSITY OF+GLASGOW AND THE CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE+UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH+DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ transformers.cabal view
@@ -0,0 +1,34 @@+name: transformers+version: 0.0.0.0+license: BSD3+license-file: LICENSE+author: Andy Gill+maintainer: libraries@haskell.org+category: Control+synopsis: Concrete monad transformers+description:+ Haskell 98 part of a monad transformer library, inspired by the paper+ /Functional Programming with Overloading and Higher-Order Polymorphism/,+ by Mark P Jones, in /Advanced School of Functional Programming/, 1995+ (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>).+ .+ This part contains the monad transformer class, the concrete monad+ transformers, operations and liftings.+build-type: Simple+exposed-modules:+ Control.Monad.Identity+ Control.Monad.Trans+ Control.Monad.Trans.Cont+ Control.Monad.Trans.Error+ Control.Monad.Trans.List+ Control.Monad.Trans.Reader+ Control.Monad.Trans.RWS+ Control.Monad.Trans.RWS.Lazy+ Control.Monad.Trans.RWS.Strict+ Control.Monad.Trans.State+ Control.Monad.Trans.State.Lazy+ Control.Monad.Trans.State.Strict+ Control.Monad.Trans.Writer+ Control.Monad.Trans.Writer.Lazy+ Control.Monad.Trans.Writer.Strict+build-depends: base