errors-1.4.7: Simplified error-handling

Safe HaskellSafe-Inferred

Data.EitherR

Contents

Description

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.

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
 

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

MonadTrans (EitherRT r) 
Monad m => Monad (EitherRT r m) 
Monad m => Functor (EitherRT r m) 
(Monad m, Monoid r) => MonadPlus (EitherRT r m) 
Monad m => Applicative (EitherRT r m) 
(Monad m, Monoid r) => Alternative (EitherRT r m) 
MonadIO m => MonadIO (EitherRT r m) 

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