A successful computation in the Except ε
monad: a
is returned, and no exception is thrown.
Equations
- Except.pure a = Except.ok a
Transforms a successful result with a function, doing nothing when an exception is thrown.
Examples:
(pure 2 : Except String Nat).map toString = pure 2
(throw "Error" : Except String Nat).map toString = throw "Error"
Equations
- Except.map f (Except.error err) = Except.error err
- Except.map f (Except.ok v) = Except.ok (f v)
Transforms exceptions with a function, doing nothing on successful results.
Examples:
(pure 2 : Except String Nat).mapError (·.length) = pure 2
(throw "Error" : Except String Nat).mapError (·.length) = throw 5
Equations
- Except.mapError f (Except.error err) = Except.error (f err)
- Except.mapError f (Except.ok v) = Except.ok v
Sequences two operations that may throw exceptions, allowing the second to depend on the value returned by the first.
If the first operation throws an exception, then it is the result of the computation. If the first succeeds but the second throws an exception, then that exception is the result. If both succeed, then the result is the result of the second computation.
This is the implementation of the >>=
operator for Except ε
.
Equations
- (Except.error err).bind f = Except.error err
- (Except.ok v).bind f = f v
Returns none
if an exception was thrown, or some
around the value on success.
Examples:
Recovers from exceptions thrown in the Except ε
monad. Typically used via the <|>
operator.
Except.tryCatch
is a related operator that allows the recovery procedure to depend on which
exception was thrown.
Equations
- (Except.ok a).orElseLazy y = Except.ok a
- (Except.error a).orElseLazy y = y ()
Equations
- One or more equations did not get rendered due to their size.
Adds exceptions of type ε
to a monad m
.
Instances For
- ExceptT.finally
- ExceptT.instLawfulMonad
- ExceptT.instMonad
- ExceptT.instMonadFunctor
- ExceptT.instMonadLift
- ExceptT.instMonadLiftExcept
- Lake.instMonadLiftTExceptTOfMonadOfMonadExceptOf_lake
- Lean.Order.instCCPOExceptTOfMonadOfPartialOrder
- Lean.Order.instMonoBindExceptTOfCCPO
- Lean.Order.instPartialOrderExceptTOfMonad
- Lean.instMonadBacktrackExceptTOfMonad
- Lean.instMonadCacheExceptTOfMonad
- instInhabitedExceptTOfMonad
- instMonadControlExceptTOfMonad
- instMonadExceptOfExceptT
- instMonadExceptOfExceptTOfMonad
Use a monadic action that may return an exception's value as an action in the transformed monad that may throw the corresponding exception.
This is the inverse of ExceptT.run
.
Equations
- ExceptT.mk x = x
Returns the value a
without throwing exceptions or having any other effect.
Equations
- ExceptT.pure a = ExceptT.mk (pure (Except.ok a))
Handles exceptions thrown by an action that can have no effects other than throwing exceptions.
Equations
- ExceptT.bindCont f (Except.ok a) = f a
- ExceptT.bindCont f (Except.error e) = pure (Except.error e)
Sequences two actions that may throw exceptions. Typically used via do
-notation or the >>=
operator.
Equations
- ma.bind f = ExceptT.mk (ma >>= ExceptT.bindCont f)
Transforms a successful computation's value using f
. Typically used via the <$>
operator.
Equations
- ExceptT.map f x = ExceptT.mk do let a ← x match a with | Except.ok a => pure (Except.ok (f a)) | Except.error e => pure (Except.error e)
Runs a computation from an underlying monad in the transformed monad with exceptions.
Equations
- ExceptT.lift t = ExceptT.mk (Except.ok <$> t)
Equations
- ExceptT.instMonadLiftExcept = { monadLift := fun {α : Type ?u.20} (e : Except ε α) => ExceptT.mk (pure e) }
Equations
- ExceptT.instMonadLift = { monadLift := fun {α : Type ?u.18} => ExceptT.lift }
Handles exceptions produced in the ExceptT ε
transformer.
Equations
- ma.tryCatch handle = ExceptT.mk do let res ← ma match res with | Except.ok a => pure (Except.ok a) | Except.error e => handle e
Equations
- ExceptT.instMonadFunctor = { monadMap := fun {α : Type ?u.18} (f : {β : Type ?u.18} → m β → m β) (x : ExceptT ε m α) => f x }
Transforms exceptions using the function f
.
This is the ExceptT
version of Except.mapError
.
Equations
- ExceptT.adapt f x = ExceptT.mk (Except.mapError f <$> x)
Equations
- One or more equations did not get rendered due to their size.
Equations
- instMonadExceptOfExceptTOfMonad m ε = { throw := fun {α : Type ?u.20} (e : ε) => ExceptT.mk (pure (Except.error e)), tryCatch := fun {α : Type ?u.20} => ExceptT.tryCatch }
Equations
- instMonadExceptOfExcept ε = { throw := fun {α : Type ?u.9} => Except.error, tryCatch := fun {α : Type ?u.9} => Except.tryCatch }
An alternative unconditional error recovery operator that allows callers to specify which exception to throw in cases where both operations throw exceptions.
By default, the first is thrown, because the <|>
operator throws the second.
Equations
- liftExcept (Except.ok a) = pure a
- liftExcept (Except.error a) = throw a
Equations
- One or more equations did not get rendered due to their size.
Monads that provide the ability to ensure an action happens, regardless of exceptions or other failures.
MonadFinally.tryFinally'
is used to desugar try ... finally ...
syntax.
Runs an action, ensuring that some other action always happens afterward.
More specifically,
tryFinally' x f
runsx
and then the “finally” computationf
. Ifx
succeeds with some valuea : α
,f (some a)
is returned. Ifx
fails form
's definition of failure,f none
is returned.tryFinally'
can be thought of as performing the same role as afinally
block in an imperative programming language.
Instances
Execute x
and then execute finalizer
even if x
threw an exception
Equations
- tryFinally x finalizer = (fun (x : α × β) => x.fst) <$> tryFinally' x fun (x : Option α) => finalizer
Equations
- One or more equations did not get rendered due to their size.