Module

Control.Monad.Except

Package
purescript-transformers
Repository
purescript/purescript-transformers

#Except Source

type Except e = ExceptT e Identity

A parametrizable exception monad; computations are either exceptions or pure values. If an exception is thrown (see throwError), the computation terminates with that value. Exceptions may also be caught with catchError, allowing the computation to resume and exit successfully.

The type parameter e is the type of exceptions, and a is the type of successful results.

A mechanism for trying many different computations until one succeeds is provided via the Alt instance, specifically the (<|>) function. The first computation to succeed is returned; if all fail, the exceptions are combined using their Semigroup instance. The Plus instance goes further and adds the possibility of a computation failing with an 'empty' exception; naturally, this requires the stronger constraint of a Monoid instance for the exception type.

#runExcept Source

runExcept :: forall a e. Except e a -> Either e a

Run a computation in the Except monad. The inverse of except.

#mapExcept Source

mapExcept :: forall b a e' e. (Either e a -> Either e' b) -> Except e a -> Except e' b

Transform the unwrapped computation using the given function.

#withExcept Source

withExcept :: forall a e' e. (e -> e') -> Except e a -> Except e' a

Transform any exceptions thrown by an Except computation using the given function.

Re-exports from Control.Monad.Error.Class

#MonadError Source

class (MonadThrow e m) <= MonadError e m | m -> e where

The MonadError type class represents those monads which support catching errors.

  • catchError x f calls the error handler f if an error is thrown during the evaluation of x.

An implementation is provided for ExceptT, and for other monad transformers defined in this library.

Laws:

  • Catch: catchError (throwError e) f = f e
  • Pure: catchError (pure a) f = pure a

Members

Instances

#throwError

throwError :: forall e m a. MonadThrow e m => e -> m a

#catchJust Source

catchJust :: forall b a m e. MonadError e m => (e -> Maybe b) -> m a -> (b -> m a) -> m a

This function allows you to provide a predicate for selecting the exceptions that you're interested in, and handle only those exceptons. If the inner computation throws an exception, and the predicate returns Nothing, then the whole computation will still fail with that exception.

Re-exports from Control.Monad.Except.Trans

#ExceptT Source

newtype ExceptT e m a

A monad transformer which adds exceptions to other monads, in the same way as Except. As before, e is the type of exceptions, and a is the type of successful results. The new type parameter m is the inner monad that computations run in.

Constructors

Instances

#MonadTrans Source

class MonadTrans t  where

The MonadTrans type class represents monad transformers.

A monad transformer is a type constructor of kind (* -> *) -> * -> *, which takes a Monad as its first argument, and returns another Monad.

This allows us to add additional effects to an existing monad. By iterating this process, we create monad transformer stacks, which contain all of the effects required for a particular computation.

The laws state that lift is a Monad morphism.

Laws:

  • lift (pure a) = pure a
  • lift (do { x <- m ; y }) = do { x <- lift m ; lift y }

Members

#withExceptT Source

withExceptT :: forall a m e' e. Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a

Transform any exceptions thrown by an ExceptT computation using the given function.

#runExceptT Source

runExceptT :: forall a m e. ExceptT e m a -> m (Either e a)

The inverse of ExceptT. Run a computation in the ExceptT monad.

#mapExceptT Source

mapExceptT :: forall b a n m e' e. (m (Either e a) -> n (Either e' b)) -> ExceptT e m a -> ExceptT e' n b

Transform the unwrapped computation using the given function.

#except Source

except :: forall a m e. Applicative m => Either e a -> ExceptT e m a

Construct a computation in the ExceptT transformer from an Either value.