{-# LANGUAGE CPP #-}
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 ()
#endif
import Data.Monoid
import System.IO
class (Monad m) => MonadError m where
type ErrorType m
throwError :: ErrorType m -> m a
catchError :: m a -> (ErrorType m -> m a) -> m a
instance MonadError IO where
type ErrorType IO = IOError
throwError :: ErrorType IO -> IO a
throwError = ErrorType IO -> IO a
forall a. IOError -> IO a
ioError
catchError :: IO a -> (ErrorType IO -> IO a) -> IO a
catchError = IO a -> (ErrorType IO -> IO a) -> IO a
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
Control.Exception.catch
instance (Error e) => MonadError (Either e) where
type ErrorType (Either e) = e
throwError :: ErrorType (Either e) -> Either e a
throwError = ErrorType (Either e) -> Either e a
forall a b. a -> Either a b
Left
Left e
l catchError :: Either e a -> (ErrorType (Either e) -> Either e a) -> Either e a
`catchError` ErrorType (Either e) -> Either e a
h = ErrorType (Either e) -> Either e a
h e
ErrorType (Either e)
l
Right a
r `catchError` ErrorType (Either e) -> Either e a
_ = a -> Either e a
forall a b. b -> Either a b
Right a
r
instance (Monad m, Error e) => MonadError (ErrorT e m) where
type ErrorType (ErrorT e m) = e
throwError :: ErrorType (ErrorT e m) -> ErrorT e m a
throwError = ErrorType (ErrorT e m) -> ErrorT e m a
forall (m :: * -> *) e a. Monad m => e -> ErrorT e m a
ErrorT.throwError
catchError :: ErrorT e m a
-> (ErrorType (ErrorT e m) -> ErrorT e m a) -> ErrorT e m a
catchError = ErrorT e m a
-> (ErrorType (ErrorT e m) -> ErrorT e m a) -> ErrorT e m a
forall (m :: * -> *) e a.
Monad m =>
ErrorT e m a -> (e -> ErrorT e m a) -> ErrorT e m a
ErrorT.catchError
instance (MonadError m) => MonadError (IdentityT m) where
type ErrorType (IdentityT m) = ErrorType m
throwError :: ErrorType (IdentityT m) -> IdentityT m a
throwError = m a -> IdentityT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> IdentityT m a)
-> (ErrorType m -> m a) -> ErrorType m -> IdentityT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: IdentityT m a
-> (ErrorType (IdentityT m) -> IdentityT m a) -> IdentityT m a
catchError = Catch (ErrorType m) m a -> Catch (ErrorType m) (IdentityT m) a
forall k e (m :: k -> *) (a :: k).
Catch e m a -> Catch e (IdentityT m) a
Identity.liftCatch Catch (ErrorType m) m a
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
catchError
instance (MonadError m) => MonadError (ListT m) where
type ErrorType (ListT m) = ErrorType m
throwError :: ErrorType (ListT m) -> ListT m a
throwError = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a)
-> (ErrorType m -> m a) -> ErrorType m -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: ListT m a -> (ErrorType (ListT m) -> ListT m a) -> ListT m a
catchError = Catch (ErrorType m) m [a] -> Catch (ErrorType m) (ListT m) a
forall e (m :: * -> *) a. Catch e m [a] -> Catch e (ListT m) a
List.liftCatch Catch (ErrorType m) m [a]
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
catchError
instance (MonadError m) => MonadError (MaybeT m) where
type ErrorType (MaybeT m) = ErrorType m
throwError :: ErrorType (MaybeT m) -> MaybeT m a
throwError = m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> MaybeT m a)
-> (ErrorType m -> m a) -> ErrorType m -> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: MaybeT m a -> (ErrorType (MaybeT m) -> MaybeT m a) -> MaybeT m a
catchError = Catch (ErrorType m) m (Maybe a) -> Catch (ErrorType m) (MaybeT m) a
forall e (m :: * -> *) a.
Catch e m (Maybe a) -> Catch e (MaybeT m) a
Maybe.liftCatch Catch (ErrorType m) m (Maybe a)
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
catchError
instance (MonadError m) => MonadError (ReaderT r m) where
type ErrorType (ReaderT r m) = ErrorType m
throwError :: ErrorType (ReaderT r m) -> ReaderT r m a
throwError = m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT r m a)
-> (ErrorType m -> m a) -> ErrorType m -> ReaderT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: ReaderT r m a
-> (ErrorType (ReaderT r m) -> ReaderT r m a) -> ReaderT r m a
catchError = Catch (ErrorType m) m a -> Catch (ErrorType m) (ReaderT r m) a
forall e (m :: * -> *) a r. Catch e m a -> Catch e (ReaderT r m) a
Reader.liftCatch Catch (ErrorType m) m a
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
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 :: ErrorType (RWST r w s m) -> RWST r w s m a
throwError = m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w s m a)
-> (ErrorType m -> m a) -> ErrorType m -> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: RWST r w s m a
-> (ErrorType (RWST r w s m) -> RWST r w s m a) -> RWST r w s m a
catchError = Catch (ErrorType m) m (a, s, w)
-> Catch (ErrorType m) (RWST r w s m) a
forall e (m :: * -> *) a s w r.
Catch e m (a, s, w) -> Catch e (RWST r w s m) a
LazyRWS.liftCatch Catch (ErrorType m) m (a, s, w)
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
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 :: ErrorType (RWST r w s m) -> RWST r w s m a
throwError = m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w s m a)
-> (ErrorType m -> m a) -> ErrorType m -> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: RWST r w s m a
-> (ErrorType (RWST r w s m) -> RWST r w s m a) -> RWST r w s m a
catchError = Catch (ErrorType m) m (a, s, w)
-> Catch (ErrorType m) (RWST r w s m) a
forall e (m :: * -> *) a s w r.
Catch e m (a, s, w) -> Catch e (RWST r w s m) a
StrictRWS.liftCatch Catch (ErrorType m) m (a, s, w)
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
catchError
instance (MonadError m) => MonadError (LazyState.StateT s m) where
type ErrorType (LazyState.StateT s m) = ErrorType m
throwError :: ErrorType (StateT s m) -> StateT s m a
throwError = m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT s m a)
-> (ErrorType m -> m a) -> ErrorType m -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: StateT s m a
-> (ErrorType (StateT s m) -> StateT s m a) -> StateT s m a
catchError = Catch (ErrorType m) m (a, s) -> Catch (ErrorType m) (StateT s m) a
forall e (m :: * -> *) a s.
Catch e m (a, s) -> Catch e (StateT s m) a
LazyState.liftCatch Catch (ErrorType m) m (a, s)
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
catchError
instance (MonadError m) => MonadError (StrictState.StateT s m) where
type ErrorType (StrictState.StateT s m) = ErrorType m
throwError :: ErrorType (StateT s m) -> StateT s m a
throwError = m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT s m a)
-> (ErrorType m -> m a) -> ErrorType m -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: StateT s m a
-> (ErrorType (StateT s m) -> StateT s m a) -> StateT s m a
catchError = Catch (ErrorType m) m (a, s) -> Catch (ErrorType m) (StateT s m) a
forall e (m :: * -> *) a s.
Catch e m (a, s) -> Catch e (StateT s m) a
StrictState.liftCatch Catch (ErrorType m) m (a, s)
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
catchError
instance (Monoid w, MonadError m) => MonadError (LazyWriter.WriterT w m) where
type ErrorType (LazyWriter.WriterT w m) = ErrorType m
throwError :: ErrorType (WriterT w m) -> WriterT w m a
throwError = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a)
-> (ErrorType m -> m a) -> ErrorType m -> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: WriterT w m a
-> (ErrorType (WriterT w m) -> WriterT w m a) -> WriterT w m a
catchError = Catch (ErrorType m) m (a, w) -> Catch (ErrorType m) (WriterT w m) a
forall e (m :: * -> *) a w.
Catch e m (a, w) -> Catch e (WriterT w m) a
LazyWriter.liftCatch Catch (ErrorType m) m (a, w)
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
catchError
instance (Monoid w, MonadError m) => MonadError (StrictWriter.WriterT w m) where
type ErrorType (StrictWriter.WriterT w m) = ErrorType m
throwError :: ErrorType (WriterT w m) -> WriterT w m a
throwError = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a)
-> (ErrorType m -> m a) -> ErrorType m -> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError
catchError :: WriterT w m a
-> (ErrorType (WriterT w m) -> WriterT w m a) -> WriterT w m a
catchError = Catch (ErrorType m) m (a, w) -> Catch (ErrorType m) (WriterT w m) a
forall e (m :: * -> *) a w.
Catch e m (a, w) -> Catch e (WriterT w m) a
StrictWriter.liftCatch Catch (ErrorType m) m (a, w)
forall (m :: * -> *) a.
MonadError m =>
m a -> (ErrorType m -> m a) -> m a
catchError