Skip to content

Commit b5b8054

Browse files
authored
Merge pull request #91 from natefaubion/monad-throw
Add MonadThrow class
2 parents d8d76f1 + 551c930 commit b5b8054

File tree

7 files changed

+47
-21
lines changed

7 files changed

+47
-21
lines changed

src/Control/Monad/Error/Class.purs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,34 @@ import Prelude
77
import Data.Maybe (Maybe(..))
88
import 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

src/Control/Monad/Except/Trans.purs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Control.Alt (class Alt)
1212
import Control.Alternative (class Alternative)
1313
import Control.Monad.Cont.Class (class MonadCont, callCC)
1414
import Control.Monad.Eff.Class (class MonadEff, liftEff)
15-
import Control.Monad.Error.Class (class MonadError, throwError, catchError)
15+
import Control.Monad.Error.Class (class MonadThrow, class MonadError, throwError, catchError)
1616
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
1717
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
1818
import Control.Monad.State.Class (class MonadState, state)
@@ -110,8 +110,10 @@ instance monadContExceptT :: MonadCont m => MonadCont (ExceptT e m) where
110110
callCC f = ExceptT $ callCC \c ->
111111
case f (\a -> ExceptT $ c (Right a)) of ExceptT b -> b
112112

113-
instance monadErrorExceptT :: Monad m => MonadError e (ExceptT e m) where
113+
instance monadThrowExceptT :: Monad m => MonadThrow e (ExceptT e m) where
114114
throwError = ExceptT <<< pure <<< Left
115+
116+
instance monadErrorExceptT :: Monad m => MonadError e (ExceptT e m) where
115117
catchError (ExceptT m) k =
116118
ExceptT (m >>= either (\a -> case k a of ExceptT b -> b) (pure <<< Right))
117119

src/Control/Monad/Maybe/Trans.purs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Control.Alt (class Alt)
1111
import Control.Alternative (class Alternative)
1212
import Control.Monad.Cont.Class (class MonadCont, callCC)
1313
import Control.Monad.Eff.Class (class MonadEff, liftEff)
14-
import Control.Monad.Error.Class (class MonadError, catchError, throwError)
14+
import Control.Monad.Error.Class (class MonadThrow, class MonadError, catchError, throwError)
1515
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
1616
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
1717
import Control.Monad.State.Class (class MonadState, state)
@@ -94,8 +94,10 @@ instance monadContMaybeT :: MonadCont m => MonadCont (MaybeT m) where
9494
callCC f =
9595
MaybeT $ callCC \c -> case f (\a -> MaybeT $ c $ Just a) of MaybeT m -> m
9696

97-
instance monadErrorMaybeT :: MonadError e m => MonadError e (MaybeT m) where
97+
instance monadThrowMaybeT :: MonadThrow e m => MonadThrow e (MaybeT m) where
9898
throwError e = lift (throwError e)
99+
100+
instance monadErrorMaybeT :: MonadError e m => MonadError e (MaybeT m) where
99101
catchError (MaybeT m) h =
100102
MaybeT $ catchError m (\a -> case h a of MaybeT b -> b)
101103

src/Control/Monad/RWS/Trans.purs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Control.Alt (class Alt, (<|>))
1212
import Control.Alternative (class Alternative)
1313
import Control.Lazy (class Lazy)
1414
import Control.Monad.Eff.Class (class MonadEff, liftEff)
15-
import Control.Monad.Error.Class (class MonadError, throwError, catchError)
15+
import Control.Monad.Error.Class (class MonadThrow, class MonadError, throwError, catchError)
1616
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader)
1717
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
1818
import Control.Monad.State.Class (class MonadState)
@@ -110,8 +110,10 @@ instance monadWriterRWST :: (Monad m, Monoid w) => MonadWriter w (RWST r w s m)
110110
m' r s >>= \(RWSResult s' (Tuple a f) w) ->
111111
pure $ RWSResult s' a (f w)
112112

113-
instance monadErrorRWST :: (MonadError e m, Monoid w) => MonadError e (RWST r w s m) where
113+
instance monadThrowRWST :: (MonadThrow e m, Monoid w) => MonadThrow e (RWST r w s m) where
114114
throwError e = lift (throwError e)
115+
116+
instance monadErrorRWST :: (MonadError e m, Monoid w) => MonadError e (RWST r w s m) where
115117
catchError m h = RWST $ \r s ->
116118
catchError
117119
(case m of RWST m' -> m' r s)

src/Control/Monad/Reader/Trans.purs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Control.Alt (class Alt, (<|>))
1212
import Control.Alternative (class Alternative)
1313
import Control.Monad.Cont.Class (class MonadCont, callCC)
1414
import Control.Monad.Eff.Class (class MonadEff, liftEff)
15-
import Control.Monad.Error.Class (class MonadError, catchError, throwError)
15+
import Control.Monad.Error.Class (class MonadThrow, class MonadError, catchError, throwError)
1616
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, asks, local)
1717
import Control.Monad.Rec.Class (class MonadRec, tailRecM)
1818
import Control.Monad.State.Class (class MonadState, state)
@@ -84,8 +84,10 @@ instance monadContReaderT :: MonadCont m => MonadCont (ReaderT r m) where
8484
callCC f = ReaderT \r -> callCC \c ->
8585
case f (ReaderT <<< const <<< c) of ReaderT f' -> f' r
8686

87-
instance monadErrorReaderT :: MonadError e m => MonadError e (ReaderT r m) where
87+
instance monadThrowReaderT :: MonadThrow e m => MonadThrow e (ReaderT r m) where
8888
throwError = lift <<< throwError
89+
90+
instance monadErrorReaderT :: MonadError e m => MonadError e (ReaderT r m) where
8991
catchError (ReaderT m) h =
9092
ReaderT \r -> catchError (m r) (\e -> case h e of ReaderT f -> f r)
9193

src/Control/Monad/State/Trans.purs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Control.Alternative (class Alternative)
1313
import Control.Lazy (class Lazy)
1414
import Control.Monad.Cont.Class (class MonadCont, callCC)
1515
import Control.Monad.Eff.Class (class MonadEff, liftEff)
16-
import Control.Monad.Error.Class (class MonadError, catchError, throwError)
16+
import Control.Monad.Error.Class (class MonadThrow, class MonadError, catchError, throwError)
1717
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
1818
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
1919
import Control.Monad.State.Class (class MonadState, get, gets, modify, put, state)
@@ -108,8 +108,10 @@ instance monadContStateT :: MonadCont m => MonadCont (StateT s m) where
108108
callCC f = StateT \s -> callCC \c ->
109109
case f (\a -> StateT \s' -> c (Tuple a s')) of StateT f' -> f' s
110110

111-
instance monadErrorStateT :: MonadError e m => MonadError e (StateT s m) where
111+
instance monadThrowStateT :: MonadThrow e m => MonadThrow e (StateT s m) where
112112
throwError e = lift (throwError e)
113+
114+
instance monadErrorStateT :: MonadError e m => MonadError e (StateT s m) where
113115
catchError (StateT m) h =
114116
StateT \s -> catchError (m s) (\e -> case h e of StateT f -> f s)
115117

src/Control/Monad/Writer/Trans.purs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Control.Alt (class Alt, (<|>))
1212
import Control.Alternative (class Alternative)
1313
import Control.Monad.Cont.Class (class MonadCont, callCC)
1414
import Control.Monad.Eff.Class (class MonadEff, liftEff)
15-
import Control.Monad.Error.Class (class MonadError, catchError, throwError)
15+
import Control.Monad.Error.Class (class MonadThrow, class MonadError, catchError, throwError)
1616
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
1717
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
1818
import Control.Monad.State.Class (class MonadState, state)
@@ -102,8 +102,10 @@ instance monadContWriterT :: (Monoid w, MonadCont m) => MonadCont (WriterT w m)
102102
callCC f = WriterT $ callCC \c ->
103103
case f (\a -> WriterT $ c (Tuple a mempty)) of WriterT b -> b
104104

105-
instance monadErrorWriterT :: (Monoid w, MonadError e m) => MonadError e (WriterT w m) where
105+
instance monadThrowWriterT :: (Monoid w, MonadThrow e m) => MonadThrow e (WriterT w m) where
106106
throwError e = lift (throwError e)
107+
108+
instance monadErrorWriterT :: (Monoid w, MonadError e m) => MonadError e (WriterT w m) where
107109
catchError (WriterT m) h = WriterT $ catchError m (\e -> case h e of WriterT a -> a)
108110

109111
instance monadAskWriterT :: (Monoid w, MonadAsk r m) => MonadAsk r (WriterT w m) where

0 commit comments

Comments
 (0)