monads-tf 0.1.0.3 → 0.3.0.1
raw patch · 14 files changed
Files
- Control/Monad/Cont/Class.hs +6/−12
- Control/Monad/Error.hs +0/−148
- Control/Monad/Error/Class.hs +0/−164
- Control/Monad/Except.hs +148/−0
- Control/Monad/Except/Class.hs +150/−0
- Control/Monad/List.hs +0/−25
- Control/Monad/RWS/Class.hs +6/−8
- Control/Monad/Reader/Class.hs +5/−14
- Control/Monad/State/Class.hs +3/−11
- Control/Monad/Writer/Class.hs +25/−16
- Setup.hs +0/−2
- changelog.md +86/−0
- monads-tf.cabal +20/−19
- readme.md +8/−0
Control/Monad/Cont/Class.hs view
@@ -54,9 +54,8 @@ import Control.Monad.Trans.Cont (ContT) import qualified Control.Monad.Trans.Cont as ContT-import Control.Monad.Trans.Error as Error+import Control.Monad.Trans.Except as Except import Control.Monad.Trans.Identity as Identity-import Control.Monad.Trans.List as List import Control.Monad.Trans.Maybe as Maybe import Control.Monad.Trans.Reader as Reader import Control.Monad.Trans.RWS.Lazy as LazyRWS@@ -66,17 +65,15 @@ import Control.Monad.Trans.Writer.Lazy as LazyWriter import Control.Monad.Trans.Writer.Strict as StrictWriter -import Data.Monoid- class (Monad m) => MonadCont m where {- | @callCC@ (call-with-current-continuation) calls a function with the current continuation as its argument. Provides an escape continuation mechanism for use with Continuation monads. Escape continuations allow to abort the current computation and return a value immediately.- They achieve a similar effect to 'Control.Monad.Error.throwError'- and 'Control.Monad.Error.catchError'- within an 'Control.Monad.Error.Error' monad.+ They achieve a similar effect to 'Control.Monad.Except.throwError'+ and 'Control.Monad.Except.catchError'+ within a 'Control.Monad.Except.MonadError' monad. Advantage of this function over calling @return@ is that it makes the continuation explicit, allowing more flexibility and better control@@ -95,14 +92,11 @@ -- --------------------------------------------------------------------------- -- Instances for other mtl transformers -instance (Error e, MonadCont m) => MonadCont (ErrorT e m) where- callCC = Error.liftCallCC callCC+instance (MonadCont m) => MonadCont (ExceptT e m) where+ callCC = Except.liftCallCC callCC instance (MonadCont m) => MonadCont (IdentityT m) where callCC = Identity.liftCallCC callCC--instance (MonadCont m) => MonadCont (ListT m) where- callCC = List.liftCallCC callCC instance (MonadCont m) => MonadCont (MaybeT m) where callCC = Maybe.liftCallCC callCC
− Control/Monad/Error.hs
@@ -1,148 +0,0 @@-{-# LANGUAGE CPP #-}-{- |-Module : Control.Monad.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 LICENSE)--Maintainer : ross@soi.city.ac.uk-Stability : experimental-Portability : non-portable (type families)--[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://web.cecs.pdx.edu/~andy/>)--}-module Control.Monad.Error (- -- * Monads with error handling- MonadError(..),- Error,- -- * The ErrorT monad transformer- ErrorT(..),- mapErrorT,- module Control.Monad,- module Control.Monad.Fix,- module Control.Monad.Trans,- -- * Example 1: Custom Error Data Type- -- $customErrorExample-- -- * Example 2: Using ErrorT Monad Transformer- -- $ErrorTExample- ) where--import Control.Monad.Error.Class-import Control.Monad.Trans-import Control.Monad.Trans.Error (ErrorT(..), mapErrorT)--import Control.Monad-import Control.Monad.Fix-#if !(MIN_VERSION_base(4,6,0))-import Control.Monad.Instances () -- deprecated from base-4.6-#endif--{- $customErrorExample-Here is an example that demonstrates the use of a custom 'Error' data type with-the 'throwError' and 'catchError' exception mechanism from 'MonadError'.-The example throws an exception if the user enters an empty string-or a string longer than 5 characters. Otherwise it prints length of the string.-->-- This is the type to represent length calculation error.->data LengthError = EmptyString -- Entered string was empty.-> | StringTooLong Int -- A string is longer than 5 characters.-> -- Records a length of the string.-> | OtherError String -- Other error, stores the problem description.->->-- We make LengthError an instance of the Error class->-- to be able to throw it as an exception.->instance Error LengthError where-> noMsg = OtherError "A String Error!"-> strMsg s = OtherError s->->-- Converts LengthError to a readable message.->instance Show LengthError where-> show EmptyString = "The string was empty!"-> show (StringTooLong len) =-> "The length of the string (" ++ (show len) ++ ") is bigger than 5!"-> show (OtherError msg) = msg->->-- For our monad type constructor, we use Either LengthError->-- which represents failure using Left LengthError->-- or a successful result of type a using Right a.->type LengthMonad = Either LengthError->->main = do-> putStrLn "Please enter a string:"-> s <- getLine-> reportResult (calculateLength s)->->-- Wraps length calculation to catch the errors.->-- Returns either length of the string or an error.->calculateLength :: String -> LengthMonad Int->calculateLength s = (calculateLengthOrFail s) `catchError` Left->->-- Attempts to calculate length and throws an error if the provided string is->-- empty or longer than 5 characters.->-- The processing is done in Either monad.->calculateLengthOrFail :: String -> LengthMonad Int->calculateLengthOrFail [] = throwError EmptyString->calculateLengthOrFail s | len > 5 = throwError (StringTooLong len)-> | otherwise = return len-> where len = length s->->-- Prints result of the string length calculation.->reportResult :: LengthMonad Int -> IO ()->reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))->reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))--}--{- $ErrorTExample-@'ErrorT'@ monad transformer can be used to add error handling to another monad.-Here is an example how to combine it with an @IO@ monad:-->import Control.Monad.Error->->-- An IO monad which can return String failure.->-- It is convenient to define the monad type of the combined monad,->-- especially if we combine more monad transformers.->type LengthMonad = ErrorT String IO->->main = do-> -- runErrorT removes the ErrorT wrapper-> r <- runErrorT calculateLength-> reportResult r->->-- Asks user for a non-empty string and returns its length.->-- Throws an error if user enters an empty string.->calculateLength :: LengthMonad Int->calculateLength = do-> -- all the IO operations have to be lifted to the IO monad in the monad stack-> liftIO $ putStrLn "Please enter a non-empty string: "-> s <- liftIO getLine-> if null s-> then throwError "The string was empty!"-> else return $ length s->->-- Prints result of the string length calculation.->reportResult :: Either String Int -> IO ()->reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))->reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))--}
− Control/Monad/Error/Class.hs
@@ -1,164 +0,0 @@-{-# LANGUAGE CPP #-}-{- |-Module : Control.Monad.Error.Class-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 LICENSE)--Maintainer : ross@soi.city.ac.uk-Stability : experimental-Portability : non-portable (type families)--[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:] @'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://web.cecs.pdx.edu/~andy/>)--}-module Control.Monad.Error.Class (- Error(..),- MonadError(..),- ) where--import Control.Monad.Trans.Error (Error(..), ErrorT)-import qualified Control.Monad.Trans.Error as ErrorT (throwError, catchError)-import Control.Monad.Trans.Identity as Identity-import Control.Monad.Trans.List as List-import Control.Monad.Trans.Maybe as Maybe-import Control.Monad.Trans.Reader as Reader-import Control.Monad.Trans.RWS.Lazy as LazyRWS-import Control.Monad.Trans.RWS.Strict as StrictRWS-import Control.Monad.Trans.State.Lazy as LazyState-import Control.Monad.Trans.State.Strict as StrictState-import Control.Monad.Trans.Writer.Lazy as LazyWriter-import Control.Monad.Trans.Writer.Strict as StrictWriter-import Control.Monad.Trans--import qualified Control.Exception-#if !(MIN_VERSION_base(4,6,0))-import Control.Monad.Instances () -- deprecated from base-4.6-#endif-import Data.Monoid-import System.IO--{- |-The strategy of combining computations that can throw exceptions-by bypassing bound functions-from the point an exception is thrown to the point that it is handled.--Is parameterized over the type of error information and-the monad type constructor.-It is common to use @'Data.Either' String@ as the monad type constructor-for an error monad in which error descriptions take the form of strings.-In that case and many other common cases the resulting monad is already defined-as an instance of the 'MonadError' class.-You can also define your own error type and\/or use a monad type constructor-other than @'Data.Either' String@ or @'Data.Either' IOError@.-In these cases you will have to explicitly define instances of the 'Error'-and\/or 'MonadError' classes.--}-class (Monad m) => MonadError m where- type ErrorType m-- -- | Is used within a monadic computation to begin exception processing.- throwError :: ErrorType m -> m a-- {- |- A handler function to handle previous errors and return to normal execution.- A common idiom is:-- > do { action1; action2; action3 } `catchError` handler-- where the @action@ functions can call 'throwError'.- Note that @handler@ and the do-block must have the same return type.- -}- catchError :: m a -> (ErrorType m -> m a) -> m a--instance MonadError IO where- type ErrorType IO = IOError- throwError = ioError- catchError = Control.Exception.catch---- ------------------------------------------------------------------------------ Our parameterizable error monad--instance (Error e) => MonadError (Either e) where- type ErrorType (Either e) = e- throwError = Left- Left l `catchError` h = h l- Right r `catchError` _ = Right r--instance (Monad m, Error e) => MonadError (ErrorT e m) where- type ErrorType (ErrorT e m) = e- throwError = ErrorT.throwError- catchError = ErrorT.catchError---- ------------------------------------------------------------------------------ Instances for other mtl transformers--instance (MonadError m) => MonadError (IdentityT m) where- type ErrorType (IdentityT m) = ErrorType m- throwError = lift . throwError- catchError = Identity.liftCatch catchError--instance (MonadError m) => MonadError (ListT m) where- type ErrorType (ListT m) = ErrorType m- throwError = lift . throwError- catchError = List.liftCatch catchError--instance (MonadError m) => MonadError (MaybeT m) where- type ErrorType (MaybeT m) = ErrorType m- throwError = lift . throwError- catchError = Maybe.liftCatch catchError--instance (MonadError m) => MonadError (ReaderT r m) where- type ErrorType (ReaderT r m) = ErrorType m- throwError = lift . throwError- catchError = Reader.liftCatch catchError--instance (Monoid w, MonadError m) => MonadError (LazyRWS.RWST r w s m) where- type ErrorType (LazyRWS.RWST r w s m) = ErrorType m- throwError = lift . throwError- catchError = LazyRWS.liftCatch catchError--instance (Monoid w, MonadError m) => MonadError (StrictRWS.RWST r w s m) where- type ErrorType (StrictRWS.RWST r w s m) = ErrorType m- throwError = lift . throwError- catchError = StrictRWS.liftCatch catchError--instance (MonadError m) => MonadError (LazyState.StateT s m) where- type ErrorType (LazyState.StateT s m) = ErrorType m- throwError = lift . throwError- catchError = LazyState.liftCatch catchError--instance (MonadError m) => MonadError (StrictState.StateT s m) where- type ErrorType (StrictState.StateT s m) = ErrorType m- throwError = lift . throwError- catchError = StrictState.liftCatch catchError--instance (Monoid w, MonadError m) => MonadError (LazyWriter.WriterT w m) where- type ErrorType (LazyWriter.WriterT w m) = ErrorType m- throwError = lift . throwError- catchError = LazyWriter.liftCatch catchError--instance (Monoid w, MonadError m) => MonadError (StrictWriter.WriterT w m) where- type ErrorType (StrictWriter.WriterT w m) = ErrorType m- throwError = lift . throwError- catchError = StrictWriter.liftCatch catchError
+ Control/Monad/Except.hs view
@@ -0,0 +1,148 @@+{- |+Module : Control.Monad.Except+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 LICENSE)++Maintainer : ross@soi.city.ac.uk+Stability : experimental+Portability : non-portable (type families)++[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://web.cecs.pdx.edu/~andy/>)+-}+module Control.Monad.Except (+ -- * Monads with error handling+ MonadError(..),+ tryError,+ withError,+ -- * The ExceptT monad transformer+ ExceptT(..),+ runExceptT,+ mapExceptT,+ withExceptT,+ module Control.Monad,+ module Control.Monad.Fix,+ module Control.Monad.Trans,+ -- * Example 1: Custom Error Data Type+ -- $customErrorExample++ -- * Example 2: Using ExceptT Monad Transformer+ -- $ExceptTExample+ ) where++import Control.Monad.Except.Class+import Control.Monad.Trans+import Control.Monad.Trans.Except (ExceptT(..), runExceptT, mapExceptT, withExceptT)++import Control.Monad+import Control.Monad.Fix++-- | 'MonadError' analogue to the 'Control.Exception.try' function.+tryError :: (MonadError m) => m a -> m (Either (ErrorType m) a)+tryError action = (Right <$> action) `catchError` (pure . Left)++-- | 'MonadError' analogue to the 'withExceptT' function.+-- Modify the value (but not the type) of an error.+-- The type is fixed because of the 'ErrorType' type family.+withError :: (MonadError m) => (ErrorType m -> ErrorType m) -> m a -> m a+withError f action = tryError action >>= either (throwError . f) pure++{- $customErrorExample+Here is an example that demonstrates the use of a custom 'Error' data type with+the 'throwError' and 'catchError' exception mechanism from 'MonadError'.+The example throws an exception if the user enters an empty string+or a string longer than 5 characters. Otherwise it prints length of the string.++>import Control.Monad.Except+>+>-- This is the type to represent length calculation error.+>data LengthError = EmptyString -- Entered string was empty.+> | StringTooLong Int -- A string is longer than 5 characters.+> -- Records a length of the string.+> | OtherError String -- Other error, stores the problem description.+>+>-- Converts LengthError to a readable message.+>instance Show LengthError where+> show EmptyString = "The string was empty!"+> show (StringTooLong len) =+> "The length of the string (" ++ (show len) ++ ") is bigger than 5!"+> show (OtherError msg) = msg+>+>-- For our monad type constructor, we use Either LengthError+>-- which represents failure using Left LengthError+>-- or a successful result of type a using Right a.+>type LengthMonad = Either LengthError+>+>main = do+> putStrLn "Please enter a string:"+> s <- getLine+> reportResult (calculateLengthOrFail s)+>+>-- Attempts to calculate length and throws an error if the provided string is+>-- empty or longer than 5 characters.+>-- The processing is done in Either monad.+>calculateLengthOrFail :: String -> LengthMonad Int+>calculateLengthOrFail [] = throwError EmptyString+>calculateLengthOrFail s | len > 5 = throwError (StringTooLong len)+> | otherwise = return len+> where len = length s+>+>-- Prints result of the string length calculation.+>reportResult :: LengthMonad Int -> IO ()+>reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))+>reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))+-}++{- $ExceptTExample+@'ErrorT'@ monad transformer can be used to add error handling to another monad.+Here is an example how to combine it with an @IO@ monad:++>import Control.Monad.Except+>+>-- An IO monad which can return String failure.+>-- It is convenient to define the monad type of the combined monad,+>-- especially if we combine more monad transformers.+>type LengthMonad = ExceptT String IO+>+>main = do+> -- runExceptT removes the ExceptT wrapper+> r <- runExceptT calculateLength+> reportResult r+>+>-- Asks user for a non-empty string and returns its length.+>-- Throws an error if user enters an empty string.+>calculateLength :: LengthMonad Int+>calculateLength = do+> -- all the IO operations have to be lifted to the IO monad in the monad stack+> liftIO $ putStrLn "Please enter a non-empty string: "+> s <- liftIO getLine+> if null s+> then throwError "The string was empty!"+> else return $ length s+>+>-- Prints result of the string length calculation.+>reportResult :: Either String Int -> IO ()+>reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))+>reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))+-}
+ Control/Monad/Except/Class.hs view
@@ -0,0 +1,150 @@+{- |+Module : Control.Monad.Except.Class+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 LICENSE)++Maintainer : ross@soi.city.ac.uk+Stability : experimental+Portability : non-portable (type families)++[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:] @'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://web.cecs.pdx.edu/~andy/>)+-}+module Control.Monad.Except.Class (+ MonadError(..),+ ) where++import Control.Monad.Trans.Except (ExceptT)+import qualified Control.Monad.Trans.Except as ExceptT (throwE, catchE)+import Control.Monad.Trans.Identity as Identity+import Control.Monad.Trans.Maybe as Maybe+import Control.Monad.Trans.Reader as Reader+import Control.Monad.Trans.RWS.Lazy as LazyRWS+import Control.Monad.Trans.RWS.Strict as StrictRWS+import Control.Monad.Trans.State.Lazy as LazyState+import Control.Monad.Trans.State.Strict as StrictState+import Control.Monad.Trans.Writer.Lazy as LazyWriter+import Control.Monad.Trans.Writer.Strict as StrictWriter+import Control.Monad.Trans++import qualified Control.Exception++{- |+The strategy of combining computations that can throw exceptions+by bypassing bound functions+from the point an exception is thrown to the point that it is handled.++Is parameterized over the type of error information and+the monad type constructor.+It is common to use @'Data.Either' String@ as the monad type constructor+for an error monad in which error descriptions take the form of strings.+In that case and many other common cases the resulting monad is already defined+as an instance of the 'MonadError' class.+You can also define your own a monad type constructor+other than @'Data.Either' e@, in which case you will have to explicitly define+an instance of the 'MonadError' class.+-}+class (Monad m) => MonadError m where+ type ErrorType m++ -- | Is used within a monadic computation to begin exception processing.+ throwError :: ErrorType m -> m a++ {- |+ A handler function to handle previous errors and return to normal execution.+ A common idiom is:++ > do { action1; action2; action3 } `catchError` handler++ where the @action@ functions can call 'throwError'.+ Note that @handler@ and the do-block must have the same return type.+ -}+ catchError :: m a -> (ErrorType m -> m a) -> m a++instance MonadError IO where+ type ErrorType IO = IOError+ throwError = ioError+ catchError = Control.Exception.catch++-- ---------------------------------------------------------------------------+-- Our parameterizable error monad++instance MonadError (Either e) where+ type ErrorType (Either e) = e+ throwError = Left+ Left l `catchError` h = h l+ Right r `catchError` _ = Right r++instance (Monad m) => MonadError (ExceptT e m) where+ type ErrorType (ExceptT e m) = e+ throwError = ExceptT.throwE+ catchError = ExceptT.catchE++-- ---------------------------------------------------------------------------+-- Instances for other mtl transformers++instance (MonadError m) => MonadError (IdentityT m) where+ type ErrorType (IdentityT m) = ErrorType m+ throwError = lift . throwError+ catchError = Identity.liftCatch catchError++instance (MonadError m) => MonadError (MaybeT m) where+ type ErrorType (MaybeT m) = ErrorType m+ throwError = lift . throwError+ catchError = Maybe.liftCatch catchError++instance (MonadError m) => MonadError (ReaderT r m) where+ type ErrorType (ReaderT r m) = ErrorType m+ throwError = lift . throwError+ catchError = Reader.liftCatch catchError++instance (Monoid w, MonadError m) => MonadError (LazyRWS.RWST r w s m) where+ type ErrorType (LazyRWS.RWST r w s m) = ErrorType m+ throwError = lift . throwError+ catchError = LazyRWS.liftCatch catchError++instance (Monoid w, MonadError m) => MonadError (StrictRWS.RWST r w s m) where+ type ErrorType (StrictRWS.RWST r w s m) = ErrorType m+ throwError = lift . throwError+ catchError = StrictRWS.liftCatch catchError++instance (MonadError m) => MonadError (LazyState.StateT s m) where+ type ErrorType (LazyState.StateT s m) = ErrorType m+ throwError = lift . throwError+ catchError = LazyState.liftCatch catchError++instance (MonadError m) => MonadError (StrictState.StateT s m) where+ type ErrorType (StrictState.StateT s m) = ErrorType m+ throwError = lift . throwError+ catchError = StrictState.liftCatch catchError++instance (Monoid w, MonadError m) => MonadError (LazyWriter.WriterT w m) where+ type ErrorType (LazyWriter.WriterT w m) = ErrorType m+ throwError = lift . throwError+ catchError = LazyWriter.liftCatch catchError++instance (Monoid w, MonadError m) => MonadError (StrictWriter.WriterT w m) where+ type ErrorType (StrictWriter.WriterT w m) = ErrorType m+ throwError = lift . throwError+ catchError = StrictWriter.liftCatch catchError
− Control/Monad/List.hs
@@ -1,25 +0,0 @@--------------------------------------------------------------------------------- |--- Module : Control.Monad.List--- Copyright : (c) Andy Gill 2001,--- (c) Oregon Graduate Institute of Science and Technology, 2001--- License : BSD-style (see the file LICENSE)------ Maintainer : ross@soi.city.ac.uk--- Stability : experimental--- Portability : portable------ The List monad.-----------------------------------------------------------------------------------module Control.Monad.List (- ListT(..),- mapListT,- module Control.Monad,- module Control.Monad.Trans,- ) where--import Control.Monad-import Control.Monad.Trans-import Control.Monad.Trans.List
Control/Monad/RWS/Class.hs view
@@ -30,26 +30,24 @@ import Control.Monad.State.Class import Control.Monad.Writer.Class -import Control.Monad.Trans.Error(Error, ErrorT)+import Control.Monad.Trans.Except(ExceptT) import Control.Monad.Trans.Maybe(MaybeT) import Control.Monad.Trans.Identity(IdentityT) import Control.Monad.Trans.RWS.Lazy as Lazy (RWST) import qualified Control.Monad.Trans.RWS.Strict as Strict (RWST) -import Data.Monoid- class (Monoid (WriterType m), MonadReader m, MonadWriter m, MonadState m) => MonadRWS m instance (Monoid w, Monad m) => MonadRWS (Lazy.RWST r w s m) instance (Monoid w, Monad m) => MonadRWS (Strict.RWST r w s m)- + --------------------------------------------------------------------------- -- Instances for other mtl transformers- -instance (Error e, MonadRWS m) => MonadRWS (ErrorT e m)- ++instance (MonadRWS m) => MonadRWS (ExceptT e m)+ instance (MonadRWS m) => MonadRWS (IdentityT m)- + instance (MonadRWS m) => MonadRWS (MaybeT m)
Control/Monad/Reader/Class.hs view
@@ -30,8 +30,7 @@ than using the 'Control.Monad.State.State' monad. Inspired by the paper- /Functional Programming with Overloading and- Higher-Order Polymorphism/, + /Functional Programming with Overloading and Higher-Order Polymorphism/, Mark P Jones (<http://web.cecs.pdx.edu/~mpj/>) Advanced School of Functional Programming, 1995. -}@@ -42,9 +41,8 @@ ) where import Control.Monad.Trans.Cont as Cont-import Control.Monad.Trans.Error+import Control.Monad.Trans.Except import Control.Monad.Trans.Identity-import Control.Monad.Trans.List import Control.Monad.Trans.Maybe import Control.Monad.Trans.Reader (ReaderT) import qualified Control.Monad.Trans.Reader as ReaderT (ask, local)@@ -56,8 +54,6 @@ import Control.Monad.Trans.Writer.Strict as Strict import Control.Monad.Trans -import Data.Monoid- -- ---------------------------------------------------------------------------- -- class MonadReader -- asks for the internal (non-mutable) state.@@ -116,20 +112,15 @@ ask = lift ask local = Cont.liftLocal ask local -instance (Error e, MonadReader m) => MonadReader (ErrorT e m) where- type EnvType (ErrorT e m) = EnvType m+instance (MonadReader m) => MonadReader (ExceptT e m) where+ type EnvType (ExceptT e m) = EnvType m ask = lift ask- local = mapErrorT . local+ local = mapExceptT . local instance (MonadReader m) => MonadReader (IdentityT m) where type EnvType (IdentityT m) = EnvType m ask = lift ask local = mapIdentityT . local--instance (MonadReader m) => MonadReader (ListT m) where- type EnvType (ListT m) = EnvType m- ask = lift ask- local = mapListT . local instance (MonadReader m) => MonadReader (MaybeT m) where type EnvType (MaybeT m) = EnvType m
Control/Monad/State/Class.hs view
@@ -27,9 +27,8 @@ import Control.Monad.Trans (lift) import Control.Monad.Trans.Cont-import Control.Monad.Trans.Error+import Control.Monad.Trans.Except import Control.Monad.Trans.Identity-import Control.Monad.Trans.List import Control.Monad.Trans.Maybe import Control.Monad.Trans.Reader import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS (RWST, get, put)@@ -39,8 +38,6 @@ import Control.Monad.Trans.Writer.Lazy as Lazy import Control.Monad.Trans.Writer.Strict as Strict -import Data.Monoid- -- --------------------------------------------------------------------------- -- | /get/ returns the state from the internals of the monad. --@@ -104,18 +101,13 @@ get = lift get put = lift . put -instance (Error e, MonadState m) => MonadState (ErrorT e m) where- type StateType (ErrorT e m) = StateType m+instance (MonadState m) => MonadState (ExceptT e m) where+ type StateType (ExceptT e m) = StateType m get = lift get put = lift . put instance (MonadState m) => MonadState (IdentityT m) where type StateType (IdentityT m) = StateType m- get = lift get- put = lift . put--instance (MonadState m) => MonadState (ListT m) where- type StateType (ListT m) = StateType m get = lift get put = lift . put
Control/Monad/Writer/Class.hs view
@@ -24,7 +24,7 @@ censor, ) where -import Control.Monad.Trans.Error as Error+import Control.Monad.Trans.Except as Except import Control.Monad.Trans.Identity as Identity import Control.Monad.Trans.Maybe as Maybe import Control.Monad.Trans.Reader@@ -40,31 +40,40 @@ WriterT, tell, listen, pass) import Control.Monad.Trans (lift) -import Data.Monoid- -- --------------------------------------------------------------------------- -- MonadWriter class------ tell is like tell on the MUD's it shouts to monad--- what you want to be heard. The monad carries this 'packet'--- upwards, merging it if needed (hence the Monoid requirement).------ listen listens to a monad acting, and returns what the monad "said".------ pass lets you provide a writer transformer which changes internals of--- the written object. class (Monoid (WriterType m), Monad m) => MonadWriter m where type WriterType m++ -- | Shout to the monad what you want to be heard. The monad carries+ -- this packet upwards, merging it if needed (hence the 'Monoid'+ -- requirement). tell :: WriterType m -> m ()++ -- | Listen to a monad acting, and return what the monad "said". listen :: m a -> m (a, WriterType m)++ -- | Provide a writer transformer which changes internals of the+ -- written object. pass :: m (a, WriterType m -> WriterType m) -> m a +-- | @'listens' f m@ is an action that executes the action @m@ and adds+-- the result of applying @f@ to the output to the value of the computation.+--+-- * @'listens' f m = 'liftM' (id *** f) ('listen' m)@+-- listens :: (MonadWriter m) => (WriterType m -> b) -> m a -> m (a, b) listens f m = do ~(a, w) <- listen m return (a, f w) +-- | @'censor' f m@ is an action that executes the action @m@ and+-- applies the function @f@ to its output, leaving the return value+-- unchanged.+--+-- * @'censor' f m = 'pass' ('liftM' (\\ x -> (x,f)) m)@+-- censor :: (MonadWriter m) => (WriterType m -> WriterType m) -> m a -> m a censor f m = pass $ do a <- m@@ -97,11 +106,11 @@ -- --------------------------------------------------------------------------- -- Instances for other mtl transformers -instance (Error e, MonadWriter m) => MonadWriter (ErrorT e m) where- type WriterType (ErrorT e m) = WriterType m+instance (MonadWriter m) => MonadWriter (ExceptT e m) where+ type WriterType (ExceptT e m) = WriterType m tell = lift . tell- listen = Error.liftListen listen- pass = Error.liftPass pass+ listen = Except.liftListen listen+ pass = Except.liftPass pass instance (MonadWriter m) => MonadWriter (IdentityT m) where type WriterType (IdentityT m) = WriterType m
− Setup.hs
@@ -1,2 +0,0 @@-import Distribution.Simple-main = defaultMain
+ changelog.md view
@@ -0,0 +1,86 @@+## 0.3.0.1++Documentation improvements++Author: Ross Paterson++Published by: Chris Martin++Date: 2023-07-10++## 0.3.0.0++Remove deprecated modules:++* `Control.Monad.Error`+* `Control.Monad.Error.Class`+* `Control.Monad.List`++Add support for `transformers-0.6.*`++Published by: Chris Martin++Date: 2023-07-10++## 0.2.1.0++Add `MonadCont`, `MonadReader`, `MonadState`, `MonadRWS`,+`MonadWriter` instances for `ExceptT`.++Published by: Chris Martin++Date: 2023-07-10++## 0.2.0.0++Added new modules `Control.Monad.Except` and+`Control.Monad.Except.Class`++Deprecated modules `Control.Monad.Error` and+`Control.Monad.Error.Class` in favor of the new `Except`+modules, following what `transformers-0.4.0.0` did.++Deprecated module `Control.Monad.List`, following what+`transformers-0.5.3.0` did.++Contributors: Ross Paterson and Chris Martin++Published by: Chris Martin++Date: 2023-07-10++## 0.1.0.3++Published by: Ross Paterson++Date: 2016-06-08++## 0.1.0.2++Published by: Ross Paterson++Date: 2014-04-19++## 0.1.0.1++Published by: Ross Paterson++Date: 2012-09-16++## 0.1.0.0++Published by: Ross Paterson++Date: 2010-03-26++## 0.0.0.1++Published by: Ross Paterson++Date: 2009-03-22++## 0.0.0.0++Published by: Ross Paterson++Date: 2009-01-10
monads-tf.cabal view
@@ -1,30 +1,28 @@+cabal-version: 3.0+ name: monads-tf-version: 0.1.0.3-license: BSD3+version: 0.3.0.1+license: BSD-3-Clause license-file: LICENSE author: Andy Gill-maintainer: Ross Paterson <ross@soi.city.ac.uk>+maintainer: Ross Paterson <ross@soi.city.ac.uk>,+ Chris Martin <chris@typeclasses.com> category: Control synopsis: Monad classes, using type families+homepage: https://github.com/typeclasses/monads-tf description:- Monad classes using type families, with instances for various- monad transformers, 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 package is almost a compatible replacement for the @mtl-tf@ package.-build-type: Simple-cabal-version: >= 1.2.3+ Monad classes using type families, with instances for+ various monad transformers. +extra-source-files: *.md+ library exposed-modules: Control.Monad.Cont Control.Monad.Cont.Class- Control.Monad.Error- Control.Monad.Error.Class+ Control.Monad.Except+ Control.Monad.Except.Class Control.Monad.Identity- Control.Monad.List Control.Monad.RWS Control.Monad.RWS.Class Control.Monad.RWS.Lazy@@ -40,8 +38,11 @@ Control.Monad.Writer.Class Control.Monad.Writer.Lazy Control.Monad.Writer.Strict- build-depends: base < 6, transformers >= 0.2.0.0 && < 0.6- extensions:- FlexibleContexts+ build-depends:+ , base ^>= 4.16 || ^>= 4.17 || ^>= 4.18+ , transformers ^>= 0.5.6 || ^>= 0.6+ default-extensions: TypeFamilies- exposed: False+ default-language: GHC2021+ ghc-options: -Wall+ hs-source-dirs: .
+ readme.md view
@@ -0,0 +1,8 @@+Monad classes using type families, with instances for various monad transformers,+inspired by the paper+[Functional Programming with Overloading and Higher-Order Polymorphism][paper],+by Mark P Jones, in *Advanced School of Functional Programming*, 1995.++This package is almost a compatible replacement for the `mtl-tf` package.++ [paper]: https://web.cecs.pdx.edu/~mpj/pubs/springschool.html