errors-1.4.1: Simplified error-handling

Safe HaskellNone

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

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 e

Complete error handling, returning a result

Conversions to the Either monad

throwE :: e -> Either e r

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

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

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

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

catchE with the arguments flipped

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

Map a function over the Left value of an Either

Flip alternative

flipE :: Either a b -> Either b a

Flip the type variables of Either

EitherRT

newtype EitherRT r m e

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 e

Complete error handling, returning a result

Conversions to the EitherT monad

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

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 r

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 r

catchT with the arguments flipped

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

Map a function over the Left value of an EitherT

Flip alternative

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

Flip the type variables of an EitherT