Equations
- Option.instMembership = { mem := fun (b : Option α) (a : α) => b = some a }
Equations
- Option.instDecidableMemOfDecidableEq j o = inferInstanceAs (Decidable (o = some j))
Equality with none
is decidable even if the wrapped type does not have decidable equality.
This is not an instance because it is not definitionally equal to the standard instance of
DecidableEq (Option α)
, which can cause problems. It can be locally bound if needed.
Try to use the Boolean comparisons Option.isNone
or Option.isSome
instead.
Equations
- none.instDecidableForallForallMemOfDecidablePred = isTrue ⋯
- (some a).instDecidableForallForallMemOfDecidablePred = if h : p a then isTrue ⋯ else isFalse ⋯
Equations
- none.instDecidableExistsAndMemOfDecidablePred = isFalse ⋯
- (some a).instDecidableExistsAndMemOfDecidablePred = if h : p a then isTrue ⋯ else isFalse ⋯
Given an optional value and a function that can be applied when the value is some
, returns the
result of applying the function if this is possible.
The function f
is partial because it is only defined for the values a : α
such a ∈ o
, which
is equivalent to o = some a
. This restriction allows the function to use the fact that it can only
be called when o
is not none
: it can relate its argument to the optional value o
. Its runtime
behavior is equivalent to that of Option.bind
.
Examples:
def attach (v : Option α) : Option { y : α // y ∈ v } :=
v.pbind fun x h => some ⟨x, h⟩
#reduce attach (some 3)
some ⟨3, ⋯⟩
#reduce attach none
none
Given a function from the elements of α
that satisfy p
to β
and a proof that an optional value
satisfies p
if it's present, applies the function to the value.
Examples:
def attach (v : Option α) : Option { y : α // y ∈ v } :=
v.pmap (fun a (h : a ∈ v) => ⟨_, h⟩) (fun _ h => h)
#reduce attach (some 3)
some ⟨3, ⋯⟩
#reduce attach none
none
Equations
- Option.pmap f none x_2 = none
- Option.pmap f (some a) H = some (f a ⋯)
Given an optional value and a function that can be applied when the value is some
, returns the
result of applying the function if this is possible, or a fallback value otherwise.
The function f
is partial because it is only defined for the values a : α
such a ∈ o
, which
is equivalent to o = some a
. This restriction allows the function to use the fact that it can only
be called when o
is not none
: it can relate its argument to the optional value o
. Its runtime
behavior is equivalent to that of Option.elim
.
Examples:
def attach (v : Option α) : Option { y : α // y ∈ v } :=
v.pelim none fun x h => some ⟨x, h⟩
#reduce attach (some 3)
some ⟨3, ⋯⟩
#reduce attach none
none
Executes a monadic action on an optional value if it is present, or does nothing if there is no value.
Examples:
#eval ((some 5).forM set : StateM Nat Unit).run 0
((), 5)
#eval (none.forM (fun x : Nat => set x) : StateM Nat Unit).run 0
((), 0)
Equations
- Option.instForM = { forM := fun [Monad m] => Option.forM }
Equations
- One or more equations did not get rendered due to their size.