Documentation

Init.Data.Option.Basic

instance Option.instBEq {α✝ : Type u_1} [BEq α✝] :
BEq (Option α✝)
Equations
def Option.getM {m : Type u_1 → Type u_2} {α : Type u_1} [Alternative m] :
Option αm α

Lifts an optional value to any Alternative, sending none to failure.

Equations
@[inline]
def Option.isSome {α : Type u_1} :
Option αBool

Returns true on some x and false on none.

Equations
@[simp]
@[simp]
theorem Option.isSome_some {α✝ : Type u_1} {a : α✝} :
@[inline]
def Option.isNone {α : Type u_1} :
Option αBool

Returns true on none and false on some x.

Equations
@[simp]
theorem Option.isNone_none {α : Type u_1} :
@[simp]
theorem Option.isNone_some {α✝ : Type u_1} {a : α✝} :
@[inline]
def Option.isEqSome {α : Type u_1} [BEq α] :
Option ααBool

x?.isEqSome y is equivalent to x? == some y, but avoids an allocation.

Equations
@[inline]
def Option.bind {α : Type u_1} {β : Type u_2} :
Option α(αOption β)Option β
Equations
@[inline]
def Option.bindM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} [Monad m] (f : αm (Option β)) (o : Option α) :
m (Option β)

Runs f on o's value, if any, and returns its result, or else returns none.

Equations
@[inline]
def Option.mapM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} [Monad m] (f : αm β) (o : Option α) :
m (Option β)

Runs a monadic function f on an optional value. If the optional value is none the function is not called.

Equations
@[inline]
def Option.filter {α : Type u_1} (p : αBool) :
Option αOption α

Keeps an optional value only if it satisfies the predicate p.

Equations
@[inline]
def Option.all {α : Type u_1} (p : αBool) :
Option αBool

Checks that an optional value satisfies a predicate p or is none.

Equations
@[inline]
def Option.any {α : Type u_1} (p : αBool) :
Option αBool

Checks that an optional value is not none and the value satisfies a predicate p.

Equations
@[macro_inline]
def Option.orElse {α : Type u_1} :
Option α(UnitOption α)Option α

Implementation of OrElse's <|> syntax for Option. If the first argument is some a, returns some a, otherwise evaluates and returns the second argument. See also or for a version that is strict in the second argument.

Equations
instance Option.instOrElse {α : Type u_1} :
Equations
@[macro_inline]
def Option.or {α : Type u_1} :
Option αOption αOption α

If the first argument is some a, returns some a, otherwise returns the second argument. This is similar to <|>/orElse, but it is strict in the second argument.

Equations
Instances For
@[inline]
def Option.lt {α : Type u_1} {β : Type u_2} (r : αβProp) :
Option αOption βProp
Equations
Instances For
@[inline]
def Option.le {α : Type u_1} {β : Type u_2} (r : αβProp) :
Option αOption βProp
Equations
def Option.merge {α : Type u_1} (fn : ααα) :
Option αOption αOption α

Take a pair of options and if they are both some, apply the given fn to produce an output. Otherwise act like orElse.

Equations
@[simp]
theorem Option.getD_none {α✝ : Type u_1} {a : α✝} :
none.getD a = a
@[simp]
theorem Option.getD_some {α✝ : Type u_1} {a b : α✝} :
(some a).getD b = a
@[simp]
theorem Option.map_none' {α : Type u_1} {β : Type u_2} (f : αβ) :
@[simp]
theorem Option.map_some' {α : Type u_1} {β : Type u_2} (a : α) (f : αβ) :
Option.map f (some a) = some (f a)
@[simp]
theorem Option.none_bind {α : Type u_1} {β : Type u_2} (f : αOption β) :
@[simp]
theorem Option.some_bind {α : Type u_1} {β : Type u_2} (a : α) (f : αOption β) :
(some a).bind f = f a
@[inline]
def Option.elim {α : Type u_1} {β : Sort u_2} :
Option αβ(αβ)β

An elimination principle for Option. It is a nondependent version of Option.recOn.

Equations
@[inline]
def Option.get {α : Type u} (o : Option α) :
o.isSome = trueα

Extracts the value a from an option that is known to be some a for some a.

Equations
@[simp]
theorem Option.some_get {α : Type u_1} {x : Option α} (h : x.isSome = true) :
some (x.get h) = x
@[simp]
theorem Option.get_some {α : Type u_1} (x : α) (h : (some x).isSome = true) :
(some x).get h = x
@[inline]
def Option.guard {α : Type u_1} (p : αProp) [DecidablePred p] (a : α) :

guard p a returns some a if p a holds, otherwise none.

Equations
@[inline]
def Option.toList {α : Type u_1} :
Option αList α

Cast of Option to List. Returns [a] if the input is some a, and [] if it is none.

Equations
@[inline]
def Option.toArray {α : Type u_1} :
Option αArray α

Cast of Option to Array. Returns #[a] if the input is some a, and #[] if it is none.

Equations
def Option.liftOrGet {α : Type u_1} (f : ααα) :
Option αOption αOption α

Two arguments failsafe function. Returns f a b if the inputs are some a and some b, and "does nothing" otherwise.

Equations
Instances For
inductive Option.Rel {α : Type u_1} {β : Type u_2} (r : αβProp) :
Option αOption βProp

Lifts a relation α → β → Prop to a relation Option α → Option β → Prop by just adding none ~ none.

@[inline]
def Option.join {α : Type u_1} (x : Option (Option α)) :

Flatten an Option of Option, a specialization of joinM.

Equations
@[inline]
def Option.mapA {m : Type u_1 → Type u_2} [Applicative m] {α : Type u_3} {β : Type u_1} (f : αm β) :
Option αm (Option β)

Like Option.mapM but for applicative functors.

Equations
@[inline]
def Option.sequence {m : Type u → Type u_1} [Monad m] {α : Type u} :
Option (m α)m (Option α)

If you maybe have a monadic computation in a [Monad m] which produces a term of type α, then there is a naturally associated way to always perform a computation in m which maybe produces a result.

Equations
@[inline]
def Option.elimM {m : Type u_1 → Type u_2} {α β : Type u_1} [Monad m] (x : m (Option α)) (y : m β) (z : αm β) :
m β

A monadic analogue of Option.elim.

Equations
@[inline]
def Option.getDM {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] (x : Option α) (y : m α) :
m α

A monadic analogue of Option.getD.

Equations
instance Option.instLawfulBEq (α : Type u_1) [BEq α] [LawfulBEq α] :
@[simp]
theorem Option.all_none {α✝ : Type u_1} {p : α✝Bool} :
@[simp]
theorem Option.all_some {α✝ : Type u_1} {p : α✝Bool} {x : α✝} :
Option.all p (some x) = p x
@[simp]
theorem Option.any_none {α✝ : Type u_1} {p : α✝Bool} :
@[simp]
theorem Option.any_some {α✝ : Type u_1} {p : α✝Bool} {x : α✝} :
Option.any p (some x) = p x
def Option.min {α : Type u_1} [Min α] :
Option αOption αOption α

The minimum of two optional values.

Note this treats none as the least element, so min none x = min x none = none for all x : Option α. Prior to nightly-2025-02-27, we instead had min none (some x) = min (some x) none = some x.

Equations
instance Option.instMin {α : Type u_1} [Min α] :
Min (Option α)
Equations
@[simp]
theorem Option.min_some_some {α : Type u_1} [Min α] {a b : α} :
min (some a) (some b) = some (min a b)
@[simp]
theorem Option.min_some_none {α : Type u_1} [Min α] {a : α} :
@[simp]
theorem Option.min_none_some {α : Type u_1} [Min α] {b : α} :
@[simp]
theorem Option.min_none_none {α : Type u_1} [Min α] :
def Option.max {α : Type u_1} [Max α] :
Option αOption αOption α

The maximum of two optional values.

Equations
instance Option.instMax {α : Type u_1} [Max α] :
Max (Option α)
Equations
@[simp]
theorem Option.max_some_some {α : Type u_1} [Max α] {a b : α} :
max (some a) (some b) = some (max a b)
@[simp]
theorem Option.max_some_none {α : Type u_1} [Max α] {a : α} :
@[simp]
theorem Option.max_none_some {α : Type u_1} [Max α] {b : α} :
@[simp]
theorem Option.max_none_none {α : Type u_1} [Max α] :
instance instLTOption {α : Type u_1} [LT α] :
LT (Option α)
Equations
instance instLEOption {α : Type u_1} [LE α] :
LE (Option α)
Equations
@[always_inline]
Equations
@[always_inline]
Equations
@[always_inline]
Equations
def liftOption {m : Type u_1 → Type u_2} {α : Type u_1} [Alternative m] :
Option αm α
Equations
@[inline]
def Option.tryCatch {α : Type u_1} (x : Option α) (handle : UnitOption α) :
Equations
Equations