Safe Haskell | Safe-Inferred |
---|
This module provides throwE
and catchE
for Either
. These two
functions reside here because throwE
and catchE
correspond to return
and (>>=
) for the flipped Either
monad: EitherR
. Similarly, this
module defines throwT
and catchT
for EitherT
, which correspond to the
Monad
operations for EitherRT
.
These throw and catch functions improve upon MonadError
because:
-
catch
is more general and allows you to change the left value's type - They are Haskell98
More advanced users can use EitherR
and EitherRT
to program in an
entirely symmetric "success monad" where exceptional results are the norm
and successful results terminate the computation. This allows you to chain
error-handlers using do
notation and pass around exceptional values of
varying types until you can finally recover from the error:
runEitherRT $ do e2 <- ioExceptionHandler e1 bool <- arithmeticExceptionhandler e2 when bool $ lift $ putStrLn "DEBUG: Arithmetic handler did something"
If any of the above error handlers succeed
, no other handlers are tried.
If you choose not to typefully distinguish between the error and sucess
monad, then use flipE
and flipET
, which swap the type variables without
changing the type.
- newtype EitherR r e = EitherR {
- runEitherR :: Either e r
- succeed :: r -> EitherR r e
- throwE :: e -> Either e r
- catchE :: Either a r -> (a -> Either b r) -> Either b r
- handleE :: (a -> Either b r) -> Either a r -> Either b r
- fmapL :: (a -> b) -> Either a r -> Either b r
- flipE :: Either a b -> Either b a
- newtype EitherRT r m e = EitherRT {
- runEitherRT :: EitherT e m r
- succeedT :: Monad m => r -> EitherRT r m e
- throwT :: Monad m => e -> EitherT e m r
- catchT :: Monad m => EitherT a m r -> (a -> EitherT b m r) -> EitherT b m r
- handleT :: Monad m => (a -> EitherT b m r) -> EitherT a m r -> EitherT b m r
- fmapLT :: Monad m => (a -> b) -> EitherT a m r -> EitherT b m r
- flipET :: Monad m => EitherT a m b -> EitherT b m a
EitherR
If "Either e r
" is the error monad, then "EitherR r e
" is the
corresponding success monad, where:
EitherR | |
|
Operations in the EitherR monad
Conversions to the Either monad
Flip alternative
EitherRT
EitherR
converted into a monad transformer
EitherRT | |
|
Operations in the EitherRT monad
Conversions to the EitherT monad
handleT :: Monad m => (a -> EitherT b m r) -> EitherT a m r -> EitherT b m rSource
catchT
with the arguments flipped