fact stringlengths 10 4.79k | type stringclasses 9
values | library stringclasses 44
values | imports listlengths 0 13 | filename stringclasses 718
values | symbolic_name stringlengths 1 76 | docstring stringlengths 10 64.6k ⌀ |
|---|---|---|---|---|---|---|
@[always_inline, inline]
protected mapError (f : ε → ε') : Except ε α → Except ε' α
| Except.error err => Except.error <| f err
| Except.ok v => Except.ok v | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | mapError | 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` |
@[always_inline, inline]
protected bind (ma : Except ε α) (f : α → Except ε β) : Except ε β :=
match ma with
| Except.error err => Except.error err
| Except.ok v => f v | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | bind | 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 ... |
@[always_inline, inline]
protected toBool : Except ε α → Bool
| Except.ok _ => true
| Except.error _ => false
@[inherit_doc Except.toBool] | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | toBool | Returns `true` if the value is `Except.ok`, `false` otherwise. |
isOk : Except ε α → Bool := Except.toBool | abbrev | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | isOk | null |
@[always_inline, inline]
protected toOption : Except ε α → Option α
| Except.ok a => some a
| Except.error _ => none | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | toOption | Returns `none` if an exception was thrown, or `some` around the value on success.
Examples:
* `(pure 10 : Except String Nat).toOption = some 10`
* `(throw "Failure" : Except String Nat).toOption = none` |
@[always_inline, inline]
protected tryCatch (ma : Except ε α) (handle : ε → Except ε α) : Except ε α :=
match ma with
| Except.ok a => Except.ok a
| Except.error e => handle e | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | tryCatch | Handles exceptions thrown in the `Except ε` monad.
If `ma` is successful, its result is returned. If it throws an exception, then `handle` is invoked
on the exception's value.
Examples:
* `(pure 2 : Except String Nat).tryCatch (pure ·.length) = pure 2`
* `(throw "Error" : Except String Nat).tryCatch (pure ·.length)... |
orElseLazy (x : Except ε α) (y : Unit → Except ε α) : Except ε α :=
match x with
| Except.ok a => Except.ok a
| Except.error _ => y ()
@[always_inline] | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | orElseLazy | 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. |
@[expose] ExceptT (ε : Type u) (m : Type u → Type v) (α : Type u) : Type v :=
m (Except ε α) | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | ExceptT | Adds exceptions of type `ε` to a monad `m`. |
@[always_inline, inline, expose]
ExceptT.mk {ε : Type u} {m : Type u → Type v} {α : Type u} (x : m (Except ε α)) : ExceptT ε m α := x | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | ExceptT.mk | 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`. |
@[always_inline, inline, expose]
ExceptT.run {ε : Type u} {m : Type u → Type v} {α : Type u} (x : ExceptT ε m α) : m (Except ε α) := x | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | ExceptT.run | Use a monadic action that may throw an exception as an action that may return an exception's value.
This is the inverse of `ExceptT.mk`. |
@[always_inline, inline, expose]
ExceptT.runK [Monad m] (x : ExceptT ε m α) (ok : α → m β) (error : ε → m β) : m β :=
x.run >>= (·.casesOn error ok) | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | ExceptT.runK | Use a monadic action that may throw an exception by providing explicit success and failure
continuations. |
@[always_inline, inline, expose]
ExceptT.runCatch [Monad m] (x : ExceptT α m α) : m α :=
x.runK pure pure | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | ExceptT.runCatch | Returns the value of a computation, forgetting whether it was an exception or a success.
This corresponds to early return. |
@[always_inline, inline, expose]
protected pure {α : Type u} (a : α) : ExceptT ε m α :=
ExceptT.mk <| pure (Except.ok a) | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | pure | Returns the value `a` without throwing exceptions or having any other effect. |
@[always_inline, inline, expose]
protected bindCont {α β : Type u} (f : α → ExceptT ε m β) : Except ε α → m (Except ε β)
| Except.ok a => f a
| Except.error e => pure (Except.error e) | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | bindCont | Handles exceptions thrown by an action that can have no effects _other_ than throwing exceptions. |
@[always_inline, inline, expose]
protected bind {α β : Type u} (ma : ExceptT ε m α) (f : α → ExceptT ε m β) : ExceptT ε m β :=
ExceptT.mk <| ma >>= ExceptT.bindCont f | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | bind | Sequences two actions that may throw exceptions. Typically used via `do`-notation or the `>>=`
operator. |
@[always_inline, inline, expose]
protected map {α β : Type u} (f : α → β) (x : ExceptT ε m α) : ExceptT ε m β :=
ExceptT.mk <| x >>= fun a => match a with
| (Except.ok a) => pure <| Except.ok (f a)
| (Except.error e) => pure <| Except.error e | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | map | Transforms a successful computation's value using `f`. Typically used via the `<$>` operator. |
@[always_inline, inline, expose]
protected lift {α : Type u} (t : m α) : ExceptT ε m α :=
ExceptT.mk <| Except.ok <$> t
@[always_inline] | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | lift | Runs a computation from an underlying monad in the transformed monad with exceptions. |
@[always_inline, inline, expose]
protected tryCatch {α : Type u} (ma : ExceptT ε m α) (handle : ε → ExceptT ε m α) : ExceptT ε m α :=
ExceptT.mk <| ma >>= fun res => match res with
| Except.ok a => pure (Except.ok a)
| Except.error e => (handle e) | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | tryCatch | Handles exceptions produced in the `ExceptT ε` transformer. |
@[always_inline, inline]
protected adapt {ε' α : Type u} (f : ε → ε') : ExceptT ε m α → ExceptT ε' m α := fun x =>
ExceptT.mk <| Except.mapError f <$> x | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | adapt | Transforms exceptions using the function `f`.
This is the `ExceptT` version of `Except.mapError`. |
@[always_inline, inline]
orelse' [MonadExcept ε m] {α : Type v} (t₁ t₂ : m α) (useFirstEx := true) : m α :=
tryCatch t₁ fun e₁ => tryCatch t₂ fun e₂ => throw (if useFirstEx then e₁ else e₂) | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | orelse' | 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. |
@[always_inline, inline]
observing {ε α : Type u} {m : Type u → Type v} [Monad m] [MonadExcept ε m] (x : m α) : m (Except ε α) :=
tryCatch (do let a ← x; pure (Except.ok a)) (fun ex => pure (Except.error ex)) | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | observing | null |
liftExcept [MonadExceptOf ε m] [Pure m] : Except ε α → m α
| Except.ok a => pure a
| Except.error e => throw e | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | liftExcept | null |
MonadFinally (m : Type u → Type v) where
/--
Runs an action, ensuring that some other action always happens afterward.
More specifically, `tryFinally' x f` runs `x` and then the “finally” computation `f`. If `x`
succeeds with some value `a : α`, `f (some a)` is returned. If `x` fails for `m`'s definition of
... | class | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | MonadFinally | 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. |
@[always_inline, inline]
tryFinally {m : Type u → Type v} {α β : Type u} [MonadFinally m] [Functor m] (x : m α) (finalizer : m β) : m α :=
let y := tryFinally' x (fun _ => finalizer)
(·.1) <$> y
@[always_inline] | def | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | tryFinally | Execute `x` and then execute `finalizer` even if `x` threw an exception |
Id.finally : MonadFinally Id where
tryFinally' := fun x h =>
let a := x
let b := h (some x)
pure (a, b)
@[always_inline] | instance | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | Id.finally | null |
ExceptT.finally {m : Type u → Type v} {ε : Type u} [MonadFinally m] [Monad m] : MonadFinally (ExceptT ε m) where
tryFinally' := fun x h => ExceptT.mk do
let r ← tryFinally' x fun e? => match e? with
| some (.ok a) => h (some a)
| _ => h none
match r with
| (.ok a, .ok b) =... | instance | Init.Control | [
"Init.Control.Basic",
"Init.Control.Id"
] | Init/Control/Except.lean | ExceptT.finally | null |
@[expose] ExceptCpsT (ε : Type u) (m : Type u → Type v) (α : Type u) := (β : Type u) → (α → m β) → (ε → m β) → m β | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/ExceptCps.lean | ExceptCpsT | Adds exceptions of type `ε` to a monad `m`.
Instead of using `Except ε` to model exceptions, this implementation uses continuation passing
style. This has different performance characteristics from `ExceptT ε`. |
@[always_inline, inline, expose]
run {ε α : Type u} [Monad m] (x : ExceptCpsT ε m α) : m (Except ε α) :=
x _ (fun a => pure (Except.ok a)) (fun e => pure (Except.error e))
set_option linter.unusedVariables false in -- `s` unused | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/ExceptCps.lean | run | Use a monadic action that may throw an exception as an action that may return an exception's value. |
@[always_inline, inline]
runK {ε α : Type u} (x : ExceptCpsT ε m α) (s : ε) (ok : α → m β) (error : ε → m β) : m β :=
x _ ok error | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/ExceptCps.lean | runK | Use a monadic action that may throw an exception by providing explicit success and failure
continuations. |
@[always_inline, inline, expose]
runCatch [Monad m] (x : ExceptCpsT α m α) : m α :=
x α pure pure
@[always_inline] | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/ExceptCps.lean | runCatch | Returns the value of a computation, forgetting whether it was an exception or a success.
This corresponds to early return. |
@[always_inline, inline, expose]
lift [Monad m] (x : m α) : ExceptCpsT ε m α :=
fun _ k _ => x >>= k | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/ExceptCps.lean | lift | Run an action from the transformed monad in the exception monad. |
@[simp] run_pure [Monad m] : run (pure x : ExceptCpsT ε m α) = pure (Except.ok x) := rfl
@[simp] theorem run_lift {α ε : Type u} [Monad m] (x : m α) : run (ExceptCpsT.lift x : ExceptCpsT ε m α) = (x >>= fun a => pure (Except.ok a) : m (Except ε α)) := rfl
@[simp] theorem run_throw [Monad m] : run (throw e : ExceptCps... | theorem | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/ExceptCps.lean | run_pure | null |
@[expose] Id (type : Type u) : Type u := type | def | Init.Control | [
"Init.Core",
"Init.Control.MonadAttach"
] | Init/Control/Id.lean | Id | The identity function on types, used primarily for its `Monad` instance.
The identity monad is useful together with monad transformers to construct monads for particular
purposes. Additionally, it can be used with `do`-notation in order to use control structures such as
local mutability, `for`-loops, and early returns... |
hasBind : Bind Id :=
inferInstance | def | Init.Control | [
"Init.Core",
"Init.Control.MonadAttach"
] | Init/Control/Id.lean | hasBind | The identity monad has a `bind` operator. |
@[always_inline, inline, expose]
protected run (x : Id α) : α := x | def | Init.Control | [
"Init.Core",
"Init.Control.MonadAttach"
] | Init/Control/Id.lean | run | Runs a computation in the identity monad.
This function is the identity function. Because its parameter has type `Id α`, it causes
`do`-notation in its arguments to use the `Monad Id` instance. |
public MonadAttach (m : Type u → Type v) where
/--
A predicate that can be assumed to be true for all return values {name}`a` of actions {name}`x`
in {name}`m`, in all situations.
-/
CanReturn {α : Type u} : (x : m α) → (a : α) → Prop
/--
Attaches a proof of {name}`MonadAttach.CanReturn` to the return val... | class | Init.Control | [
"Init.Control.Basic"
] | Init/Control/MonadAttach.lean | MonadAttach | null |
public WeaklyLawfulMonadAttach (m : Type u → Type v) [Monad m] [MonadAttach m] where
map_attach {α : Type u} {x : m α} : Subtype.val <$> MonadAttach.attach x = x | class | Init.Control | [
"Init.Control.Basic"
] | Init/Control/MonadAttach.lean | WeaklyLawfulMonadAttach | null |
public LawfulMonadAttach (m : Type u → Type v) [Monad m] [MonadAttach m] extends
WeaklyLawfulMonadAttach m where
canReturn_map_imp {α : Type u} {P : α → Prop} {x : m (Subtype P)} {a : α} :
MonadAttach.CanReturn (Subtype.val <$> x) a → P a | class | Init.Control | [
"Init.Control.Basic"
] | Init/Control/MonadAttach.lean | LawfulMonadAttach | This type class ensures that {name}`MonadAttach.CanReturn` is the unique strongest possible
postcondition. |
public MonadAttach.pbind [Monad m] [MonadAttach m]
(x : m α) (f : (a : α) → MonadAttach.CanReturn x a → m β) : m β :=
MonadAttach.attach x >>= (fun ⟨a, ha⟩ => f a ha) | def | Init.Control | [
"Init.Control.Basic"
] | Init/Control/MonadAttach.lean | MonadAttach.pbind | Like {name}`Bind.bind`, {name}`pbind` sequences two computations {lean}`x : m α` and {lean}`f`,
allowing the second to depend on the value computed by the first.
But other than with {name}`Bind.bind`, the second computation can also depend on a proof that
the return value {given}`a` of {name}`x` satisfies {lean}`MonadA... |
@[expose]
public protected MonadAttach.trivial {m : Type u → Type v} [Monad m] : MonadAttach m where
CanReturn _ _ := True
attach x := (⟨·, .intro⟩) <$> x | def | Init.Control | [
"Init.Control.Basic"
] | Init/Control/MonadAttach.lean | MonadAttach.trivial | A {lean}`MonadAttach` instance where all return values are possible and {name}`attach` adds no
information to the return value, except a trivial proof of {name}`True`.
This instance is used whenever no more useful {name}`MonadAttach` instance can be implemented.
It always has a {name}`WeaklyLawfulMonadAttach`, but usu... |
@[expose] OptionT (m : Type u → Type v) (α : Type u) : Type v :=
m (Option α) | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | OptionT | Adds the ability to fail to a monad. Unlike ordinary exceptions, there is no way to signal why a
failure occurred. |
@[always_inline, inline, expose]
OptionT.run {m : Type u → Type v} {α : Type u} (x : OptionT m α) : m (Option α) :=
x | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | OptionT.run | Executes an action that might fail in the underlying monad `m`, returning `none` in case of failure. |
@[always_inline, inline, expose]
protected mk (x : m (Option α)) : OptionT m α :=
x | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | mk | Converts an action that returns an `Option` into one that might fail, with `none` indicating
failure. |
@[always_inline, inline, expose]
protected bind (x : OptionT m α) (f : α → OptionT m β) : OptionT m β := OptionT.mk do
match (← x) with
| some a => f a
| none => pure none | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | bind | Sequences two potentially-failing actions. The second action is run only if the first succeeds. |
@[always_inline, inline, expose]
protected pure (a : α) : OptionT m α := OptionT.mk do
pure (some a)
@[always_inline] | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | pure | Succeeds with the provided value. |
@[always_inline, inline, expose] protected orElse (x : OptionT m α) (y : Unit → OptionT m α) : OptionT m α := OptionT.mk do
match (← x) with
| some a => pure (some a)
| _ => y () | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | orElse | Recovers from failures. Typically used via the `<|>` operator. |
@[always_inline, inline, expose] protected fail : OptionT m α := OptionT.mk do
pure none | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | fail | A recoverable failure. |
@[always_inline, inline, expose] protected lift (x : m α) : OptionT m α := OptionT.mk do
return some (← x) | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | lift | Converts a computation from the underlying monad into one that could fail, even though it does not.
This function is typically implicitly accessed via a `MonadLiftT` instance as part of [automatic
lifting](lean-manual://section/monad-lifting). |
@[always_inline, inline, expose] protected tryCatch (x : OptionT m α) (handle : PUnit → OptionT m α) : OptionT m α := OptionT.mk do
let some a ← x | handle ⟨⟩
pure <| some a | def | Init.Control | [
"Init.Data.Option.Basic",
"Init.Control.Except"
] | Init/Control/Option.lean | tryCatch | Handles failures by treating them as exceptions of type `Unit`. |
@[always_inline, inline]
protected orElse [Alternative m] (x₁ : ReaderT ρ m α) (x₂ : Unit → ReaderT ρ m α) : ReaderT ρ m α :=
fun s => x₁ s <|> x₂ () s | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/Reader.lean | orElse | Recovers from errors. The same local value is provided to both branches. Typically used via the
`<|>` operator. |
@[always_inline, inline]
protected failure [Alternative m] : ReaderT ρ m α :=
fun _ => failure | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/Reader.lean | failure | Fails with a recoverable error. |
@[always_inline]
ReaderT.tryFinally [MonadFinally m] : MonadFinally (ReaderT ρ m) where
tryFinally' x h ctx := tryFinally' (x ctx) (fun a? => h a? ctx) | instance | Init.Control | [
"Init.Control.Except"
] | Init/Control/Reader.lean | ReaderT.tryFinally | null |
ReaderM (ρ : Type u) := ReaderT ρ Id | abbrev | Init.Control | [
"Init.Control.Except"
] | Init/Control/Reader.lean | ReaderM | A monad with access to a read-only value of type `ρ`. The value can be locally overridden by
`withReader`, but it cannot be mutated. |
@[expose] StateT (σ : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) :=
σ → m (α × σ) | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | StateT | Adds a mutable state of type `σ` to a monad.
Actions in the resulting monad are functions that take an initial state and return, in `m`, a tuple
of a value and a state. |
@[always_inline, inline, expose]
StateT.mk {σ : Type u} {m : Type u → Type v} {α : Type u} (x : σ → m (α × σ)) : StateT σ m α := x | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | StateT.mk | Interpret `σ → m (α × σ)` as an element of `StateT σ m α`. |
@[always_inline, inline, expose]
StateT.run {σ : Type u} {m : Type u → Type v} {α : Type u} (x : StateT σ m α) (s : σ) : m (α × σ) :=
x s | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | StateT.run | Executes an action from a monad with added state in the underlying monad `m`. Given an initial
state, it returns a value paired with the final state. |
@[always_inline, inline, expose]
StateT.run' {σ : Type u} {m : Type u → Type v} [Functor m] {α : Type u} (x : StateT σ m α) (s : σ) : m α :=
(·.1) <$> x s | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | StateT.run' | Executes an action from a monad with added state in the underlying monad `m`. Given an initial
state, it returns a value, discarding the final state. |
@[expose, reducible]
StateM (σ α : Type u) : Type u := StateT σ Id α | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | StateM | A tuple-based state monad.
Actions in `StateM σ` are functions that take an initial state and return a value paired with a
final state. |
@[always_inline, inline, expose]
protected pure (a : α) : StateT σ m α :=
fun s => pure (a, s) | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | pure | Returns the given value without modifying the state. Typically used via `Pure.pure`. |
@[always_inline, inline, expose]
protected bind (x : StateT σ m α) (f : α → StateT σ m β) : StateT σ m β :=
fun s => do let (a, s) ← x s; f a s | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | bind | Sequences two actions. Typically used via the `>>=` operator. |
@[always_inline, inline, expose]
protected map (f : α → β) (x : StateT σ m α) : StateT σ m β :=
fun s => do let (a, s) ← x s; pure (f a, s)
@[always_inline] | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | map | Modifies the value returned by a computation. Typically used via the `<$>` operator. |
@[always_inline, inline]
protected orElse [Alternative m] {α : Type u} (x₁ : StateT σ m α) (x₂ : Unit → StateT σ m α) : StateT σ m α :=
fun s => x₁ s <|> x₂ () s | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | orElse | Recovers from errors. The state is rolled back on error recovery. Typically used via the `<|>`
operator. |
@[always_inline, inline]
protected failure [Alternative m] {α : Type u} : StateT σ m α :=
fun _ => failure | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | failure | Fails with a recoverable error. The state is rolled back on error recovery. |
@[always_inline, inline, expose]
protected get : StateT σ m σ :=
fun s => pure (s, s) | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | get | Retrieves the current value of the monad's mutable state.
This increments the reference count of the state, which may inhibit in-place updates. |
@[always_inline, inline, expose]
protected set : σ → StateT σ m PUnit :=
fun s' _ => pure (⟨⟩, s') | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | set | Replaces the mutable state with a new value. |
@[always_inline, inline, expose]
protected modifyGet (f : σ → α × σ) : StateT σ m α :=
fun s => pure (f s) | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | modifyGet | Applies a function to the current state that both computes a new state and a value. The new state
replaces the current state, and the value is returned.
It is equivalent to `do let (a, s) := f (← StateT.get); StateT.set s; pure a`. However, using
`StateT.modifyGet` may lead to better performance because it doesn't add... |
@[always_inline, inline, expose]
protected lift {α : Type u} (t : m α) : StateT σ m α :=
fun s => do let a ← t; pure (a, s) | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | lift | Runs an action from the underlying monad in the monad with state. The state is not modified.
This function is typically implicitly accessed via a `MonadLiftT` instance as part of [automatic
lifting](lean-manual://section/monad-lifting). |
@[always_inline, inline]
ForM.forIn [Monad m] [ForM (StateT β (ExceptT β m)) ρ α]
(x : ρ) (b : β) (f : α → β → m (ForInStep β)) : m β := do
let g a b := .mk do
match ← f a b with
| .yield b' => pure (.ok (⟨⟩, b'))
| .done b' => pure (.error b')
match ← forM (m := StateT β (ExceptT β m)) (α := α) x g... | def | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | ForM.forIn | Creates a suitable implementation of `ForIn.forIn` from a `ForM` instance. |
@[always_inline]
StateT.monadControl (σ : Type u) (m : Type u → Type v) [Monad m] : MonadControl m (StateT σ m) where
stM := fun α => α × σ
liftWith := fun f => do let s ← get; liftM (f (fun x => x.run s))
restoreM := fun x => do let (a, s) ← liftM x; set s; pure a
@[always_inline] | instance | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | StateT.monadControl | null |
StateT.tryFinally {m : Type u → Type v} {σ : Type u} [MonadFinally m] [Monad m] : MonadFinally (StateT σ m) where
tryFinally' := fun x h s => do
let ((a, _), (b, s'')) ← tryFinally' (x s) fun
| some (a, s') => h (some a) s'
| none => h none s
pure ((a, b), s'') | instance | Init.Control | [
"Init.Control.Except"
] | Init/Control/State.lean | StateT.tryFinally | null |
@[expose] StateCpsT (σ : Type u) (m : Type u → Type v) (α : Type u) := (δ : Type u) → σ → (α → σ → m δ) → m δ | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/StateCps.lean | StateCpsT | An alternative implementation of a state monad transformer that internally uses continuation passing
style instead of tuples. |
@[always_inline, inline, expose]
runK (x : StateCpsT σ m α) (s : σ) (k : α → σ → m β) : m β :=
x _ s k | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/StateCps.lean | runK | Runs a stateful computation that's represented using continuation passing style by providing it with
an initial state and a continuation. |
@[always_inline, inline, expose]
run [Monad m] (x : StateCpsT σ m α) (s : σ) : m (α × σ) :=
runK x s (fun a s => pure (a, s)) | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/StateCps.lean | run | Executes an action from a monad with added state in the underlying monad `m`. Given an initial
state, it returns a value paired with the final state.
While the state is internally represented in continuation passing style, the resulting value is the
same as for a non-CPS state monad. |
@[always_inline, inline, expose]
run' [Monad m] (x : StateCpsT σ m α) (s : σ) : m α :=
runK x s (fun a _ => pure a)
@[always_inline] | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/StateCps.lean | run' | Executes an action from a monad with added state in the underlying monad `m`. Given an initial
state, it returns a value, discarding the final state. |
@[always_inline, inline, expose]
protected lift [Monad m] (x : m α) : StateCpsT σ m α :=
fun _ s k => x >>= (k . s) | def | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/StateCps.lean | lift | For continuation monads, it is not possible to provide a computable `MonadAttach` instance that
actually adds information about the return value. Therefore, this instance always attaches a proof
of `True`.
-/
instance : MonadAttach (StateCpsT ε m) := .trivial
/--
Runs an action from the underlying monad in the monad w... |
@[simp] runK_pure (a : α) (s : σ) (k : α → σ → m β) : (pure a : StateCpsT σ m α).runK s k = k a s := rfl
@[simp] theorem runK_get (s : σ) (k : σ → σ → m β) : (get : StateCpsT σ m σ).runK s k = k s s := rfl
@[simp] theorem runK_set (s s' : σ) (k : PUnit → σ → m β) : (set s' : StateCpsT σ m PUnit).runK s k = k ⟨⟩ s' :=... | theorem | Init.Control | [
"Init.Control.Lawful.Basic"
] | Init/Control/StateCps.lean | runK_pure | null |
@[expose] StateRefT' (ω : Type) (σ : Type) (m : Type → Type) (α : Type) : Type := ReaderT (ST.Ref ω σ) m α
/-! Recall that `StateRefT` is a macro that infers `ω` from the `m`. -/ | def | Init.Control | [
"Init.System.ST"
] | Init/Control/StateRef.lean | StateRefT' | A state monad that uses an actual mutable reference cell (i.e. an `ST.Ref ω σ`).
The macro `StateRefT σ m α` infers `ω` from `m`. It should normally be used instead. |
@[always_inline, inline]
StateRefT'.run {ω σ : Type} {m : Type → Type} [Monad m] [MonadLiftT (ST ω) m] {α : Type} (x : StateRefT' ω σ m α) (s : σ) : m (α × σ) := do
let ref ← ST.mkRef s
let a ← x ref
let s ← ref.get
pure (a, s) | def | Init.Control | [
"Init.System.ST"
] | Init/Control/StateRef.lean | StateRefT'.run | Executes an action from a monad with added state in the underlying monad `m`. Given an initial
state, it returns a value paired with the final state.
The monad `m` must support `ST` effects in order to create and mutate reference cells. |
@[always_inline, inline]
StateRefT'.run' {ω σ : Type} {m : Type → Type} [Monad m] [MonadLiftT (ST ω) m] {α : Type} (x : StateRefT' ω σ m α) (s : σ) : m α := do
let (a, _) ← x.run s
pure a | def | Init.Control | [
"Init.System.ST"
] | Init/Control/StateRef.lean | StateRefT'.run' | Executes an action from a monad with added state in the underlying monad `m`. Given an initial
state, it returns a value, discarding the final state.
The monad `m` must support `ST` effects in order to create and mutate reference cells. |
@[always_inline, inline]
protected lift (x : m α) : StateRefT' ω σ m α :=
fun _ => x | def | Init.Control | [
"Init.System.ST"
] | Init/Control/StateRef.lean | lift | Runs an action from the underlying monad in the monad with state. The state is not modified.
This function is typically implicitly accessed via a `MonadLiftT` instance as part of [automatic
lifting](lean-manual://section/monad-lifting). |
@[inline]
protected get [MonadLiftT (ST ω) m] : StateRefT' ω σ m σ :=
fun ref => ref.get | def | Init.Control | [
"Init.System.ST"
] | Init/Control/StateRef.lean | get | Retrieves the current value of the monad's mutable state.
This increments the reference count of the state, which may inhibit in-place updates. |
@[inline]
protected set [MonadLiftT (ST ω) m] (s : σ) : StateRefT' ω σ m PUnit :=
fun ref => ref.set s | def | Init.Control | [
"Init.System.ST"
] | Init/Control/StateRef.lean | set | Replaces the mutable state with a new value. |
@[inline]
protected modifyGet [MonadLiftT (ST ω) m] (f : σ → α × σ) : StateRefT' ω σ m α :=
fun ref => ref.modifyGet f | def | Init.Control | [
"Init.System.ST"
] | Init/Control/StateRef.lean | modifyGet | Applies a function to the current state that both computes a new state and a value. The new state
replaces the current state, and the value is returned.
It is equivalent to a `get` followed by a `set`. However, using `modifyGet` may lead to higher
performance because it doesn't add a new reference to the state value. ... |
Expr
| var (x : Nat)
| op (lhs rhs : Expr)
deriving Inhabited, Repr, BEq
open Std | inductive | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | Expr | null |
Variable {α : Sort u} (op : α → α → α) : Type u where
value : α
neutral : Option $ PLift (LawfulIdentity op value) | structure | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | Variable | null |
Context (α : Sort u) where
op : α → α → α
assoc : Associative op
comm : Option $ PLift $ Commutative op
idem : Option $ PLift $ IdempotentOp op
vars : List (Variable op)
arbitrary : α | structure | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | Context | null |
ContextInformation (α : Sort u) where
isNeutral : α → Nat → Bool
isComm : α → Bool
isIdem : α → Bool | class | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | ContextInformation | null |
EvalInformation (α : Sort u) (β : Sort v) where
arbitrary : α → β
evalOp : α → β → β → β
evalVar : α → Nat → β | class | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | EvalInformation | null |
Context.var (ctx : Context α) (idx : Nat) : Variable ctx.op :=
ctx.vars[idx]?.getD ⟨ctx.arbitrary, none⟩ | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | Context.var | null |
eval (β : Sort u) [EvalInformation α β] (ctx : α) : (ex : Expr) → β
| Expr.var idx => EvalInformation.evalVar ctx idx
| Expr.op l r => EvalInformation.evalOp ctx (eval β ctx l) (eval β ctx r) | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | eval | null |
Expr.toList : Expr → List Nat
| Expr.var idx => [idx]
| Expr.op l r => l.toList.append r.toList | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | Expr.toList | null |
evalList (β : Sort u) [EvalInformation α β] (ctx : α) : List Nat → β
| [] => EvalInformation.arbitrary ctx
| [x] => EvalInformation.evalVar ctx x
| x :: xs => EvalInformation.evalOp ctx (EvalInformation.evalVar ctx x) (evalList β ctx xs) | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | evalList | null |
insert (x : Nat) : List Nat → List Nat
| [] => [x]
| a :: as => if x < a then x :: a :: as else a :: insert x as | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | insert | null |
sort (xs : List Nat) : List Nat :=
let rec loop : List Nat → List Nat → List Nat
| acc, [] => acc
| acc, x :: xs => loop (insert x acc) xs
loop [] xs | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | sort | null |
mergeIdem (xs : List Nat) : List Nat :=
let rec loop : Nat → List Nat → List Nat
| curr, next :: rest =>
if curr = next then
loop curr rest
else
curr :: loop next rest
| curr, [] => [curr]
match xs with
| [] => []
| x :: xs => loop x xs | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | mergeIdem | null |
removeNeutrals [info : ContextInformation α] (ctx : α) : List Nat → List Nat
| x :: xs =>
match loop (x :: xs) with
| [] => [x]
| ys => ys
| [] => []
where loop : List Nat → List Nat
| x :: xs =>
match info.isNeutral ctx x with
| true => loop xs
| false => x :: loop xs
| [] =... | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | removeNeutrals | null |
norm [info : ContextInformation α] (ctx : α) (e : Expr) : List Nat :=
let xs := e.toList
let xs := removeNeutrals ctx xs
let xs := if info.isComm ctx then sort xs else xs
if info.isIdem ctx then mergeIdem xs else xs | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | norm | null |
noncomputable List.two_step_induction
{motive : List Nat → Sort u}
(l : List Nat)
(empty : motive [])
(single : ∀ a, motive [a])
(step : ∀ a b l, motive (b :: l) → motive (a :: b :: l))
: motive l := by
induction l with
| nil => assumption
| cons a l => cases l; apply single; apply step; assumption | def | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | List.two_step_induction | null |
Context.mergeIdem_nonEmpty (e : List Nat) (h : e ≠ []) : mergeIdem e ≠ [] := by
induction e using List.two_step_induction with
| empty => simp_all
| single => simp [mergeIdem, mergeIdem.loop]
| step => simp [mergeIdem, mergeIdem.loop] at *; split <;> simp_all | theorem | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | Context.mergeIdem_nonEmpty | null |
Context.mergeIdem_head : mergeIdem (x :: x :: xs) = mergeIdem (x :: xs) := by
simp [mergeIdem, mergeIdem.loop] | theorem | Init.Data | [
"Init.ByCases"
] | Init/Data/AC.lean | Context.mergeIdem_head | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.