@@ -7,24 +7,34 @@ import Prelude
77import Data.Maybe (Maybe (..))
88import Data.Either (Either (..), either )
99
10- -- | The `MonadError` type class represents those monads which support errors via
11- -- | `throwError` and `catchError`.
10+ -- | The `MonadThrow` type class represents those monads which support errors via
11+ -- | `throwError`, where `throwError e` halts, yielding the error `e`.
12+ -- |
13+ -- | An implementation is provided for `ExceptT`, and for other monad transformers
14+ -- | defined in this library.
15+ -- |
16+ -- | Laws:
17+ -- |
18+ -- | - Left zero: `throwError e >>= f = throwError e`
19+ -- |
20+ class Monad m <= MonadThrow e m | m -> e where
21+ throwError :: forall a . e -> m a
22+
23+ -- | The `MonadError` type class represents those monads which support catching
24+ -- | errors.
1225-- |
13- -- | - `throwError e` throws the error `e`
1426-- | - `catchError x f` calls the error handler `f` if an error is thrown during the
1527-- | evaluation of `x`.
1628-- |
17- -- | An implementation is provided for `ErrorT `, and for other monad transformers
29+ -- | An implementation is provided for `ExceptT `, and for other monad transformers
1830-- | defined in this library.
1931-- |
2032-- | Laws:
2133-- |
22- -- | - Left zero: `throwError e >>= f = throwError e`
2334-- | - Catch: `catchError (throwError e) f = f e`
2435-- | - Pure: `catchError (pure a) f = pure a`
2536-- |
26- class Monad m <= MonadError e m | m -> e where
27- throwError :: forall a . e -> m a
37+ class MonadThrow e m <= MonadError e m | m -> e where
2838 catchError :: forall a . m a -> (e -> m a ) -> m a
2939
3040-- | This function allows you to provide a predicate for selecting the
@@ -45,13 +55,17 @@ catchJust p act handler = catchError act handle
4555 Nothing -> throwError e
4656 Just b -> handler b
4757
48- instance monadErrorEither :: MonadError e (Either e ) where
58+ instance monadThrowEither :: MonadThrow e (Either e ) where
4959 throwError = Left
60+
61+ instance monadErrorEither :: MonadError e (Either e ) where
5062 catchError (Left e) h = h e
5163 catchError (Right x) _ = Right x
5264
53- instance monadErrorMaybe :: MonadError Unit Maybe where
65+ instance monadThrowMaybe :: MonadThrow Unit Maybe where
5466 throwError = const Nothing
67+
68+ instance monadErrorMaybe :: MonadError Unit Maybe where
5569 catchError Nothing f = f unit
5670 catchError (Just a) _ = Just a
5771
0 commit comments