Safe Haskell | Safe-Infered |
---|
Data.EitherR
Contents
Description
This modules provides newtypes which flip the type variables of Either
and EitherT
to access the symmetric monad over the opposite type variable.
This module provides the following simple benefits to the casual user:
- A type-class free alternative to
MonadError
- No
UndecidableInstances
or any other extensions, for that matter - A more powerful
catchE
statement that allows you to change the type of error value returned
More advanced users can take advantage of the fact that EitherR
and
EitherRT
define an entirely symmetric "success monad" where
error-handling computations are the default and successful results terminate
the monad. This allows you to chain error-handlers and pass around values
other than exceptions 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:
Constructors
EitherR | |
Fields
|
Operations in the EitherR monad
Conversions to the Either monad
Flip alternative
EitherRT
EitherR
converted into a monad transformer
Constructors
EitherRT | |
Fields
|
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