| Copyright | (C) 2026 - Eitan Chatav |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | Eitan Chatav <eitan.chatav@gmail.com> |
| Stability | provisional |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Control.Monad.Fail.Try
Description
Synopsis
- class (MonadFail m, MonadPlus m, Filterable m, forall x. BackusNaurForm (m x)) => MonadTry (m :: Type -> Type) where
- try :: m a -> m a
- type MonadicTry (p :: Type -> Type -> Type) = (Profunctor p, forall x. MonadTry (p x))
- class Monad m => MonadFail (m :: Type -> Type) where
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where
- class Applicative f => Alternative (f :: Type -> Type) where
- class Functor f => Filterable (f :: Type -> Type) where
MonadTry
class (MonadFail m, MonadPlus m, Filterable m, forall x. BackusNaurForm (m x)) => MonadTry (m :: Type -> Type) where Source #
MonadTry is a failure handling interface, with fail & try
and redundant alternation & filtration operators.
prop> empty = mzero prop> (<|>) = mplus prop> filter = mfilter
MonadTry also supports the BackusNaurForm interface
for tracing failures and the following invariant should hold.
prop> fail label = rule label empty
Minimal complete definition
Nothing
Methods
A handler for failures.
Used for backtracking state on failure in
Parsector.
type MonadicTry (p :: Type -> Type -> Type) = (Profunctor p, forall x. MonadTry (p x)) Source #
A Profunctor which is also a MonadTry.
MonadFail
class Monad m => MonadFail (m :: Type -> Type) where #
When a value is bound in do-notation, the pattern on the left
hand side of <- might not match. In this case, this class
provides a function to recover.
A Monad without a MonadFail instance may only be used in conjunction
with pattern that always match, such as newtypes, tuples, data types with
only a single data constructor, and irrefutable patterns (~pat).
Instances of MonadFail should satisfy the following law: fail s should
be a left zero for >>=,
fail s >>= f = fail s
If your Monad is also MonadPlus, a popular definition is
fail _ = mzero
fail s should be an action that runs in the monad itself, not an
exception (except in instances of MonadIO). In particular,
fail should not be implemented in terms of error.
Since: base-4.9.0.0
Instances
MonadPlus
class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where #
Monads that also support choice and failure.
Minimal complete definition
Nothing
Methods
The identity of mplus. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
The default definition is
mzero = empty
An associative operation. The default definition is
mplus = (<|>)
Instances
Alternative
class Applicative f => Alternative (f :: Type -> Type) where #
A monoid on applicative functors.
If defined, some and many should be the least solutions
of the equations:
Examples
>>>Nothing <|> Just 42Just 42
>>>[1, 2] <|> [3, 4][1,2,3,4]
>>>empty <|> print (2^15)32768
Methods
The identity of <|>
empty <|> a == a a <|> empty == a
(<|>) :: f a -> f a -> f a infixl 3 #
An associative binary operation
One or more.
Examples
>>>some (putStr "la")lalalalalalalalala... * goes on forever *
>>>some Nothingnothing
>>>take 5 <$> some (Just 1)* hangs forever *
Note that this function can be used with Parsers based on
Applicatives. In that case some parser will attempt to
parse parser one or more times until it fails.
Zero or more.
Examples
>>>many (putStr "la")lalalalalalalalala... * goes on forever *
>>>many NothingJust []
>>>take 5 <$> many (Just 1)* hangs forever *
Note that this function can be used with Parsers based on
Applicatives. In that case many parser will attempt to
parse parser zero or more times until it fails.
Instances
Filterable
class Functor f => Filterable (f :: Type -> Type) where #
Like Functor, but you can remove elements instead of updating them.
Formally, the class Filterable represents a functor from Kleisli Maybe to Hask.
A definition of mapMaybe must satisfy the following laws:
Methods
mapMaybe :: (a -> Maybe b) -> f a -> f b #
Like mapMaybe.
catMaybes :: f (Maybe a) -> f a #