Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data ResultT msg (err :: [*]) m a
- runResultT :: Monad m => ResultT msg '[] m a -> m a
- type Result e a = ResultT e a Identity
- runResult :: Result msg '[] a -> a
- abort :: ('[e] :| err, Monad m) => e -> ResultT msg err m a
- achieve :: Monad m => msg -> ResultT msg err m a -> ResultT msg err m a
- (<?>) :: Monad m => ResultT msg err m a -> msg -> ResultT msg err m a
- finally :: Monad m => ResultT msg err m a -> ResultT msg err m () -> ResultT msg err m a
- recover :: forall e m msg err a. Monad m => ResultT msg (e ': err) m a -> (e -> [msg] -> ResultT msg err m a) -> ResultT msg err m a
- recoverWhile :: forall e m msg err a. Monad m => msg -> ResultT msg (e ': err) m a -> (e -> [msg] -> ResultT msg err m a) -> ResultT msg err m a
- recoverMany :: forall plus err m msg a. (Split plus err, Monad m) => ResultT msg (Join plus err) m a -> Handler plus ([msg] -> ResultT msg err m a) -> ResultT msg err m a
- recoverManyWith :: forall plus c err msg m a. (HaveInstance c plus, Split plus err, Monad m) => ResultT msg (Join plus err) m a -> (forall e. c e => e -> [msg] -> ResultT msg err m a) -> ResultT msg err m a
- recoverManyDescriptive :: forall plus err msg m a. (HaveInstance DescriptiveError plus, Split plus err, Monad m) => ResultT msg (Join plus err) m a -> (forall e. DescriptiveError e => e -> [msg] -> ResultT msg err m a) -> ResultT msg err m a
- repeatUntil :: forall e err msg m a. Monad m => ResultT msg (e ': err) m () -> (e -> [msg] -> ResultT msg err m a) -> ResultT msg err m ()
- repeatUntil' :: forall e err msg m. Monad m => ResultT msg (e ': err) m () -> ResultT msg err m ()
- foldUntil :: forall e err msg m a. Monad m => a -> (a -> ResultT msg (e ': err) m a) -> (a -> e -> [msg] -> ResultT msg err m a) -> ResultT msg err m a
- foldUntil' :: forall e err msg m a. Monad m => a -> (a -> ResultT msg (e ': err) m a) -> ResultT msg err m a
- eitherOr :: Monad m => Either e a -> a -> ResultT msg err m a
- exceptOr :: (Monad m, MonadError e m) => m a -> a -> ResultT msg err m a
- orAbort :: ('[e] :| err, Monad m) => Maybe a -> e -> ResultT msg err m a
- orAbortM :: ('[e] :| err, Monad m) => m (Maybe a) -> e -> ResultT msg err m a
- orElse :: Monad m => Maybe a -> a -> ResultT msg err m a
- orElseM :: Monad m => m (Maybe a) -> a -> ResultT msg err m a
- eitherAbort :: ('[e] :| err, Monad m) => Either e a -> ResultT msg err m a
- exceptAbort :: ('[e] :| err, MonadError e m) => m a -> ResultT msg err m a
- data Handler set a
- type family set1 :| set2 :: Constraint where ...
- (+>) :: (e -> a) -> Handler set a -> Handler (e ': set) a
- eoh :: Handler '[] a
- class HaveInstance c set where
- class DescriptiveError err where
ResultT
data ResultT msg (err :: [*]) m a Source #
MonadState s m => MonadState s (ResultT msg err m) Source # | |
MonadReader env m => MonadReader env (ResultT msg err m) Source # | |
MonadTrans (ResultT msg err) Source # | |
Monad m => Monad (ResultT msg err m) Source # | |
Functor m => Functor (ResultT msg err m) Source # | |
Monad m => Applicative (ResultT msg err m) Source # | |
MonadIO m => MonadIO (ResultT msg err m) Source # | |
runResultT :: Monad m => ResultT msg '[] m a -> m a Source #
Escape from the ResultT monad.
The type signature of runResultT
obliges the library user to deal with
errors, with functions such as recover
, recoverMany
, etc.
Result
Monadic Operations
:: ('[e] :| err, Monad m) | |
=> e | A symbolic value which describes why the computation would not be completed. This symbolic value is of a type inside the row of errors. |
-> ResultT msg err m a |
Abort the current computation and raise an error to describe the reason
Similarly to, e.g. Nothing
for Maybe
, abort
is a computation
shortcut. The rest of the monadic computation is not executed, and the
error is transmitted to the caller computation. Using the recover
,
recoverWhile
or recoverMany
functions to stop the chain.
:: Monad m | |
=> msg | A description of the computation, to act as context in case of error. |
-> ResultT msg err m a | |
-> ResultT msg err m a |
Declaratively describe the purpose of a computation.
Using achieve in various places, it becomes possible, once an error is
raised, to determine more easily its context. achieve
has an operator
counterpart: '(?)'. The former should be used to contextualise a
do-block, whereas the latter can be prefered for monadic function calls.
achieve "try to get configuration" $ do f <- readParseFile "main" <?> "read main file" f' <- readParseFile "aux" <?> "read aux file" pure $ buildConfiguration f f'
These contextual messages are made available, in addition to the error,
when using functions such as recover
, recoverMany
, repeatUntil
etc.
:: Monad m | |
=> ResultT msg err m a | The try block |
-> ResultT msg err m () | The |
-> ResultT msg err m a |
Always execute a given computation (called finally block thereafter) after a first computation (try block) has been completed.
The finally
block is executed even if the try
block is aborted. This
allows to deal with clean-up code that has to be executed no matter what
happened.
:: Monad m | |
=> ResultT msg (e ': err) m a | The try block |
-> (e -> [msg] -> ResultT msg err m a) | The error handler |
-> ResultT msg err m a |
Temporally allows one given error type by providing an error handler to execute in case of failure.
recoverWhile :: forall e m msg err a. Monad m => msg -> ResultT msg (e ': err) m a -> (e -> [msg] -> ResultT msg err m a) -> ResultT msg err m a Source #
:: (HaveInstance c plus, Split plus err, Monad m) | |
=> ResultT msg (Join plus err) m a | The try block |
-> (forall e. c e => e -> [msg] -> ResultT msg err m a) | The typeclass-based error handler |
-> ResultT msg err m a |
Similarly to recoverMany
, but use the same error handler for every error
types.
All the error types has to implement a given typeclass.
recoverManyDescriptive :: forall plus err msg m a. (HaveInstance DescriptiveError plus, Split plus err, Monad m) => ResultT msg (Join plus err) m a -> (forall e. DescriptiveError e => e -> [msg] -> ResultT msg err m a) -> ResultT msg err m a Source #
repeatUntil :: forall e err msg m a. Monad m => ResultT msg (e ': err) m () -> (e -> [msg] -> ResultT msg err m a) -> ResultT msg err m () Source #
Repeat a computation which may fail until it fails with a given error.
Typical use case is reading a file line by line, until reaching its end.
If you want to carry some state, you can have a look at foldUntil
.
repeatUntil' :: forall e err msg m. Monad m => ResultT msg (e ': err) m () -> ResultT msg err m () Source #
Same as repeatUntil
, but without an error handler.
:: Monad m | |
=> a | Initial state |
-> (a -> ResultT msg (e ': err) m a) | |
-> (a -> e -> [msg] -> ResultT msg err m a) | Error handler |
-> ResultT msg err m a |
Similarly to repeatUntil
, repeat a computation until a given error; in
addition, carry an accumulator.
Same as foldUntil
, but without the error handler part.
Leverage Existing Error Handling
exceptAbort :: ('[e] :| err, MonadError e m) => m a -> ResultT msg err m a Source #
Set of Errors
type family set1 :| set2 :: Constraint where ... Source #
class HaveInstance c set where Source #
generalize :: (forall e. c e => e -> a) -> Handler set a Source #
HaveInstance c ([] *) Source # | |
(HaveInstance c rst, c e) => HaveInstance c ((:) * e rst) Source # | |