errors-1.3.1: Simplified error-handling

Safe HaskellSafe-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.

Synopsis

EitherR

newtype EitherR r e Source

If "Either e r" is the error monad, then "EitherR r e" is the corresponding success monad, where:

Constructors

EitherR 

Fields

runEitherR :: Either e r
 

Instances

Operations in the EitherR monad

succeed :: r -> EitherR r eSource

Complete error handling, returning a result

Conversions to the Either monad

throwE :: e -> Either e rSource

throwE in the error monad corresponds to return in the success monad

catchE :: Either a r -> (a -> Either b r) -> Either b rSource

catchE in the error monad corresponds to (>>=) in the success monad

handleE :: (a -> Either b r) -> Either a r -> Either b rSource

catchE with the arguments flipped

fmapL :: (a -> b) -> Either a r -> Either b rSource

Map a function over the Left value of an Either

Flip alternative

flipE :: Either a b -> Either b aSource

Flip the type variables of Either

EitherRT

newtype EitherRT r m e Source

EitherR converted into a monad transformer

Constructors

EitherRT 

Fields

runEitherRT :: EitherT e m r
 

Instances

Operations in the EitherRT monad

succeedT :: Monad m => r -> EitherRT r m eSource

Complete error handling, returning a result

Conversions to the EitherT monad

throwT :: Monad m => e -> EitherT e m rSource

throwT in the error monad corresponds to return in the success monad

catchT :: Monad m => EitherT a m r -> (a -> EitherT b m r) -> EitherT b m rSource

catchT in the error monad corresponds to (>>=) in the success monad

handleT :: Monad m => (a -> EitherT b m r) -> EitherT a m r -> EitherT b m rSource

catchT with the arguments flipped

fmapLT :: Monad m => (a -> b) -> EitherT a m r -> EitherT b m rSource

Map a function over the Left value of an EitherT

Flip alternative

flipET :: Monad m => EitherT a m b -> EitherT b m aSource

Flip the type variables of an EitherT