Safe Haskell | Safe |
---|---|
Language | Haskell98 |
Synchronous exceptions immediately abort a series of computations.
We provide monads for describing this behaviour.
In contrast to ErrorT from mtl
or transformers
package
we do not pose restrictions on the exception type.
How to tell, that a function can possibly throw more than one (kind of) exception?
If you would use the exception type (Either ParserException IOError)
then this is different from (Either IOError ParserException)
.
Thus we recommned using type classes for exceptions.
Then you can use one type containing all exceptions in an application,
but the type signature still tells which exceptions are actually possible.
Examples:
parser :: ParserException e => ExceptionalT e ParserMonad a getLine :: IOException e => ExceptionalT e IO String fileParser :: (ParserException e, IOException e) => ExceptionalT e IO String
Unfortunately, this way you cannot remove single exceptions
from the constraints by catching them.
You can only remove all of them using resolve
or none.
For a more advanced approach,
that allows removing exceptions constraints
by some non-Haskell-98 type hackery,
see the exception package by Joseph Iborra.
- data Exceptional e a
- fromMaybe :: e -> Maybe a -> Exceptional e a
- toMaybe :: Exceptional e a -> Maybe a
- fromEither :: Either e a -> Exceptional e a
- toEither :: Exceptional e a -> Either e a
- fromExitCode :: ExitCode -> Exceptional Int ()
- toExitCode :: Exceptional Int () -> ExitCode
- getExceptionNull :: Exceptional e () -> Maybe e
- switch :: (e -> b) -> (a -> b) -> Exceptional e a -> b
- force :: Exceptional e a -> Exceptional e a
- mapException :: (e0 -> e1) -> Exceptional e0 a -> Exceptional e1 a
- mapExceptional :: (e0 -> e1) -> (a -> b) -> Exceptional e0 a -> Exceptional e1 b
- throw :: e -> Exceptional e a
- assert :: e -> Bool -> Exceptional e ()
- catch :: Exceptional e0 a -> (e0 -> Exceptional e1 a) -> Exceptional e1 a
- resolve :: (e -> a) -> Exceptional e a -> a
- merge :: Monoid e => Exceptional e (a -> b) -> Exceptional e a -> Exceptional e b
- alternative :: Exceptional e a -> Exceptional e a -> Exceptional e a
- newtype ExceptionalT e m a = ExceptionalT {
- runExceptionalT :: m (Exceptional e a)
- fromMaybeT :: Monad m => e -> MaybeT m a -> ExceptionalT e m a
- toMaybeT :: Monad m => ExceptionalT e m a -> MaybeT m a
- fromErrorT :: Monad m => ErrorT e m a -> ExceptionalT e m a
- toErrorT :: Monad m => ExceptionalT e m a -> ErrorT e m a
- fromEitherT :: Monad m => m (Either e a) -> ExceptionalT e m a
- toEitherT :: Monad m => ExceptionalT e m a -> m (Either e a)
- fromExitCodeT :: Functor m => m ExitCode -> ExceptionalT Int m ()
- toExitCodeT :: Functor m => ExceptionalT Int m () -> m ExitCode
- liftT :: Monad m => Exceptional e a -> ExceptionalT e m a
- switchT :: Monad m => (e -> m b) -> (a -> m b) -> ExceptionalT e m a -> m b
- forceT :: Monad m => ExceptionalT e m a -> ExceptionalT e m a
- mapExceptionT :: Monad m => (e0 -> e1) -> ExceptionalT e0 m a -> ExceptionalT e1 m a
- mapExceptionalT :: (m (Exceptional e0 a) -> n (Exceptional e1 b)) -> ExceptionalT e0 m a -> ExceptionalT e1 n b
- throwT :: Monad m => e -> ExceptionalT e m a
- assertT :: Monad m => e -> Bool -> ExceptionalT e m ()
- catchT :: Monad m => ExceptionalT e0 m a -> (e0 -> ExceptionalT e1 m a) -> ExceptionalT e1 m a
- bracketT :: Monad m => ExceptionalT e m h -> (h -> ExceptionalT e m ()) -> (h -> ExceptionalT e m a) -> ExceptionalT e m a
- resolveT :: Monad m => (e -> m a) -> ExceptionalT e m a -> m a
- tryT :: Monad m => ExceptionalT e m a -> m (Exceptional e a)
- manyT :: Monad m => (e0 -> Maybe e1) -> (a -> b -> b) -> b -> ExceptionalT e0 m a -> ExceptionalT e1 m b
- manyMonoidT :: (Monad m, Monoid a) => (e0 -> Maybe e1) -> ExceptionalT e0 m a -> ExceptionalT e1 m a
- mergeT :: (Monoid e, Monad m) => ExceptionalT e m (a -> b) -> ExceptionalT e m a -> ExceptionalT e m b
- alternativeT :: Monad m => ExceptionalT e m a -> ExceptionalT e m a -> ExceptionalT e m a
Documentation
data Exceptional e a Source #
Like Either
, but explicitly intended for handling of exceptional results.
In contrast to Either
we do not support fail
.
Calling fail
in the Exceptional
monad is an error.
This way, we do not require that an exception can be derived from a String
,
yet, we require no constraint on the exception type at all.
Monad (Exceptional e) Source # | |
Functor (Exceptional e) Source # | |
MonadFix (Exceptional e) Source # | I think it is not a good idea to use this instance,
maybe we shoul remove it.
It expects that the constructor is |
Applicative (Exceptional e) Source # | |
(Eq e, Eq a) => Eq (Exceptional e a) Source # | |
(Show e, Show a) => Show (Exceptional e a) Source # | |
(NFData e, NFData a) => NFData (Exceptional e a) Source # | |
fromMaybe :: e -> Maybe a -> Exceptional e a Source #
toMaybe :: Exceptional e a -> Maybe a Source #
fromEither :: Either e a -> Exceptional e a Source #
toEither :: Exceptional e a -> Either e a Source #
fromExitCode :: ExitCode -> Exceptional Int () Source #
toExitCode :: Exceptional Int () -> ExitCode Source #
getExceptionNull :: Exceptional e () -> Maybe e Source #
useful in connection with continue
force :: Exceptional e a -> Exceptional e a Source #
mapException :: (e0 -> e1) -> Exceptional e0 a -> Exceptional e1 a Source #
mapExceptional :: (e0 -> e1) -> (a -> b) -> Exceptional e0 a -> Exceptional e1 b Source #
throw :: e -> Exceptional e a Source #
assert :: e -> Bool -> Exceptional e () Source #
catch :: Exceptional e0 a -> (e0 -> Exceptional e1 a) -> Exceptional e1 a Source #
resolve :: (e -> a) -> Exceptional e a -> a Source #
merge :: Monoid e => Exceptional e (a -> b) -> Exceptional e a -> Exceptional e b infixl 4 Source #
see mergeT
alternative :: Exceptional e a -> Exceptional e a -> Exceptional e a infixl 3 Source #
newtype ExceptionalT e m a Source #
like ErrorT, but ExceptionalT is the better name in order to distinguish from real (programming) errors
ExceptionalT | |
|
MonadTrans (ExceptionalT e) Source # | |
Monad m => Monad (ExceptionalT e m) Source # | |
Functor m => Functor (ExceptionalT e m) Source # | |
MonadFix m => MonadFix (ExceptionalT e m) Source # | Same restrictions applies as for |
Applicative m => Applicative (ExceptionalT e m) Source # | |
fromMaybeT :: Monad m => e -> MaybeT m a -> ExceptionalT e m a Source #
fromErrorT :: Monad m => ErrorT e m a -> ExceptionalT e m a Source #
fromEitherT :: Monad m => m (Either e a) -> ExceptionalT e m a Source #
fromExitCodeT :: Functor m => m ExitCode -> ExceptionalT Int m () Source #
toExitCodeT :: Functor m => ExceptionalT Int m () -> m ExitCode Source #
liftT :: Monad m => Exceptional e a -> ExceptionalT e m a Source #
switchT :: Monad m => (e -> m b) -> (a -> m b) -> ExceptionalT e m a -> m b Source #
forceT :: Monad m => ExceptionalT e m a -> ExceptionalT e m a Source #
see force
mapExceptionT :: Monad m => (e0 -> e1) -> ExceptionalT e0 m a -> ExceptionalT e1 m a Source #
mapExceptionalT :: (m (Exceptional e0 a) -> n (Exceptional e1 b)) -> ExceptionalT e0 m a -> ExceptionalT e1 n b Source #
throwT :: Monad m => e -> ExceptionalT e m a Source #
catchT :: Monad m => ExceptionalT e0 m a -> (e0 -> ExceptionalT e1 m a) -> ExceptionalT e1 m a Source #
bracketT :: Monad m => ExceptionalT e m h -> (h -> ExceptionalT e m ()) -> (h -> ExceptionalT e m a) -> ExceptionalT e m a Source #
If the enclosed monad has custom exception facilities, they could skip the cleanup code. Make sure, that this cannot happen by choosing an appropriate monad.
resolveT :: Monad m => (e -> m a) -> ExceptionalT e m a -> m a Source #
tryT :: Monad m => ExceptionalT e m a -> m (Exceptional e a) Source #
:: Monad m | |
=> (e0 -> Maybe e1) | exception handler |
-> (a -> b -> b) |
|
-> b | empty |
-> ExceptionalT e0 m a | atomic action to repeat |
-> ExceptionalT e1 m b |
:: (Monad m, Monoid a) | |
=> (e0 -> Maybe e1) | exception handler |
-> ExceptionalT e0 m a | atomic action to repeat |
-> ExceptionalT e1 m a |
mergeT :: (Monoid e, Monad m) => ExceptionalT e m (a -> b) -> ExceptionalT e m a -> ExceptionalT e m b infixl 4 Source #
This combines two actions similar to Applicative's *
.
The result action fails if one of the input action fails,
but both actions are executed.
E.g. consider a compiler that emits all errors
that can be detected independently,
but eventually aborts if there is at least one error.
The exception type e
might be a list type,
or an Endo
type that implements a difflist.
alternativeT :: Monad m => ExceptionalT e m a -> ExceptionalT e m a -> ExceptionalT e m a infixl 3 Source #