Module docstring
{}
{}
Option.instDecidableEq
instance
{α✝} [DecidableEq✝ α✝] : DecidableEq✝ (@Option✝ α✝)
DecidableEq for Option
Option.instBEq
instance
{α✝} [BEq✝ α✝] : BEq✝ (@Option✝ α✝)
Option.getM
definition
[Alternative m] : Option α → m α
/-- Lifts an optional value to any `Alternative`, sending `none` to `failure`. -/
def getM [Alternative m] : Option α → m α
| none => failure
| some a => pure a
Option.isSome
definition
: Option α → Bool
Option.isSome_none
theorem
: @isSome α none = false
@[simp] theorem isSome_none : @isSome α none = false := rfl
Option.isSome_some
theorem
: isSome (some a) = true
@[simp] theorem isSome_some : isSome (some a) = true := rfl
Option.isNone
definition
: Option α → Bool
/--
Returns `true` on `none` and `false` on `some x`.
This function is more flexible than `(· == none)` because it does not require a `BEq α` instance.
Examples:
* `(none : Option Nat).isNone = true`
* `(some Nat.add).isNone = false`
-/
@[inline] def isNone : Option α → Bool
| some _ => false
| none => true
Option.isNone_none
theorem
: @isNone α none = true
@[simp] theorem isNone_none : @isNone α none = true := rfl
Option.isNone_some
theorem
: isNone (some a) = false
@[simp] theorem isNone_some : isNone (some a) = false := rfl
Option.isEqSome
definition
[BEq α] : Option α → α → Bool
/--
Checks whether an optional value is both present and equal to some other value.
Given `x? : Option α` and `y : α`, `x?.isEqSome y` is equivalent to `x? == some y`. It is more
efficient because it avoids an allocation.
-/
@[inline] def isEqSome [BEq α] : Option α → α → Bool
| some a, b => a == b
| none, _ => false
Option.bind
definition
: Option α → (α → Option β) → Option β
/--
Sequencing of `Option` computations.
From the perspective of `Option` as computations that might fail, this function sequences
potentially-failing computations, failing if either fails. From the perspective of `Option` as a
collection with at most one element, the function is applied to the element if present, and the
final result is empty if either the initial or the resulting collections are empty.
This function is often accessed via the `>>=` operator from the `Bind (Option α)` instance, or
implicitly via `do`-notation, but it is also idiomatic to call it using [generalized field
notation](lean-manual://section/generalized-field-notation).
Examples:
* `none.bind (fun x => some x) = none`
* `(some 4).bind (fun x => some x) = some 4`
* `none.bind (Option.guard (· > 2)) = none`
* `(some 2).bind (Option.guard (· > 2)) = none`
* `(some 4).bind (Option.guard (· > 2)) = some 4`
-/
@[inline] protected def bind : Option α → (α → Option β) → Option β
| none, _ => none
| some a, f => f a
Option.bindM
definition
[Monad m] (f : α → m (Option β)) (o : Option α) : m (Option β)
/--
Runs the monadic action `f` on `o`'s value, if any, and returns the result, or `none` if there is
no value.
From the perspective of `Option` as a collection with at most one element, the monadic the function
is applied to the element if present, and the final result is empty if either the initial or the
resulting collections are empty.
-/
@[inline] protected def bindM [Monad m] (f : α → m (Option β)) (o : Option α) : m (Option β) := do
if let some a := o then
return (← f a)
else
return none
Option.mapM
definition
[Monad m] (f : α → m β) (o : Option α) : m (Option β)
/--
Runs a monadic function `f` on an optional value, returning the result. If the optional value is
`none`, the function is not called and the result is also `none`.
From the perspective of `Option` as a container with at most one element, this is analogous to
`List.mapM`, returning the result of running the monadic function on all elements of the container.
`Option.mapA` is the corresponding operation for applicative functors.
-/
@[inline] protected def mapM [Monad m] (f : α → m β) (o : Option α) : m (Option β) := do
if let some a := o then
return some (← f a)
else
return none
Option.map_id
theorem
: (Option.map id : Option α → Option α) = id
Option.filter
definition
(p : α → Bool) : Option α → Option α
/--
Keeps an optional value only if it satisfies a Boolean predicate.
If `Option` is thought of as a collection that contains at most one element, then `Option.filter` is
analogous to `List.filter` or `Array.filter`.
Examples:
* `(some 5).filter (· % 2 == 0) = none`
* `(some 4).filter (· % 2 == 0) = some 4`
* `none.filter (fun x : Nat => x % 2 == 0) = none`
* `none.filter (fun x : Nat => true) = none`
-/
@[always_inline, inline] protected def filter (p : α → Bool) : Option α → Option α
| some a => if p a then some a else none
| none => none
Option.all
definition
(p : α → Bool) : Option α → Bool
/--
Checks whether an optional value either satisfies a Boolean predicate or is `none`.
Examples:
* `(some 33).all (· % 2 == 0) = false
* `(some 22).all (· % 2 == 0) = true
* `none.all (fun x : Nat => x % 2 == 0) = true
-/
@[always_inline, inline] protected def all (p : α → Bool) : Option α → Bool
| some a => p a
| none => true
Option.any
definition
(p : α → Bool) : Option α → Bool
/--
Checks whether an optional value is not `none` and satisfies a Boolean predicate.
Examples:
* `(some 33).any (· % 2 == 0) = false
* `(some 22).any (· % 2 == 0) = true
* `none.any (fun x : Nat => true) = false
-/
@[always_inline, inline] protected def any (p : α → Bool) : Option α → Bool
| some a => p a
| none => false
Option.orElse
definition
: Option α → (Unit → Option α) → 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.
-/
@[always_inline, macro_inline] protected def orElse : Option α → (Unit → Option α) → Option α
| some a, _ => some a
| none, b => b ()
Option.instOrElse
instance
: OrElse (Option α)
instance : OrElse (Option α) where
orElse := Option.orElse
Option.or
definition
: Option α → Option α → Option α
/--
Returns the first of its arguments that is `some`, or `none` if neither is `some`.
This is similar to the `<|>` operator, also known as `OrElse.orElse`, but both arguments are always
evaluated without short-circuiting.
-/
@[always_inline, macro_inline] def or : Option α → Option α → Option α
| some a, _ => some a
| none, b => b
Option.lt
definition
(r : α → β → Prop) : Option α → Option β → Prop
/--
Lifts an ordering relation to `Option`, such that `none` is the least element.
It can be understood as adding a distinguished least element, represented by `none`, to both `α` and
`β`.
This definition is part of the implementation of the `LT (Option α)` instance. However, because it
can be used with heterogeneous relations, it is sometimes useful on its own.
Examples:
* `Option.lt (fun n k : Nat => n < k) none none = False`
* `Option.lt (fun n k : Nat => n < k) none (some 3) = True`
* `Option.lt (fun n k : Nat => n < k) (some 3) none = False`
* `Option.lt (fun n k : Nat => n < k) (some 4) (some 5) = True`
* `Option.lt (fun n k : Nat => n < k) (some 4) (some 4) = False`
-/
@[inline] protected def lt (r : α → β → Prop) : Option α → Option β → Prop
| none, some _ => True
| some x, some y => r x y
| _, _ => False
Option.le
definition
(r : α → β → Prop) : Option α → Option β → Prop
Option.instDecidableRelLt
instance
(r : α → β → Prop) [s : DecidableRel r] : DecidableRel (Option.lt r)
Option.merge
definition
(fn : α → α → α) : Option α → Option α → Option α
/--
Applies a function to a two optional values if both are present. Otherwise, if one value is present,
it is returned and the function is not used.
The value is `some (fn a b)` if the inputs are `some a` and `some b`. Otherwise, the behavior is
equivalent to `Option.orElse`: if only one input is `some x`, then the value is `some x`, and if
both are `none`, then the value is `none`.
Examples:
* `Option.merge (· + ·) none (some 3) = some 3`
* `Option.merge (· + ·) (some 2) (some 3) = some 5`
* `Option.merge (· + ·) (some 2) none = some 2`
* `Option.merge (· + ·) none none = none`
-/
def merge (fn : α → α → α) : Option α → Option α → Option α
| none , none => none
| some x, none => some x
| none , some y => some y
| some x, some y => some <| fn x y
Option.getD_none
theorem
: getD none a = a
Option.getD_some
theorem
: getD (some a) b = a
Option.map_none'
theorem
(f : α → β) : none.map f = none
Option.map_some'
theorem
(a) (f : α → β) : (some a).map f = some (f a)
Option.none_bind
theorem
(f : α → Option β) : none.bind f = none
Option.some_bind
theorem
(a) (f : α → Option β) : (some a).bind f = f a
Option.elim
definition
: Option α → β → (α → β) → β
/--
A case analysis function for `Option`.
Given a value for `none` and a function to apply to the contents of `some`, `Option.elim` checks
which constructor a given `Option` consists of, and uses the appropriate argument.
`Option.elim` is an elimination principle for `Option`. In particular, it is a non-dependent version
of `Option.recOn`. It can also be seen as a combination of `Option.map` and `Option.getD`.
Examples:
* `(some "hello").elim 0 String.length = 5`
* `none.elim 0 String.length = 0`
-/
@[inline] protected def elim : Option α → β → (α → β) → β
| some x, _, f => f x
| none, y, _ => y
Option.get
definition
{α : Type u} : (o : Option α) → isSome o → α
Option.some_get
theorem
: ∀ {x : Option α} (h : isSome x), some (x.get h) = x
Option.get_some
theorem
(x : α) (h : isSome (some x)) : (some x).get h = x
Option.guard
definition
(p : α → Prop) [DecidablePred p] (a : α) : Option α
/--
Returns `none` if a value doesn't satisfy a predicate, or the value itself otherwise.
From the perspective of `Option` as computations that might fail, this function is a run-time
assertion operator in the `Option` monad.
Examples:
* `Option.guard (· > 2) 1 = none`
* `Option.guard (· > 2) 5 = some 5`
-/
@[inline] def guard (p : α → Prop) [DecidablePred p] (a : α) : Option α :=
if p a then some a else none
Option.toList
definition
: Option α → List α
Option.toArray
definition
: Option α → Array α
Option.liftOrGet
definition
(f : α → α → α) : Option α → Option α → Option α
/--
Applies a function to a two optional values if both are present. Otherwise, if one value is present,
it is returned and the function is not used.
The value is `some (f a b)` if the inputs are `some a` and `some b`. Otherwise, the behavior is
equivalent to `Option.orElse`. If only one input is `some x`, then the value is `some x`. If both
are `none`, then the value is `none`.
Examples:
* `Option.liftOrGet (· + ·) none (some 3) = some 3`
* `Option.liftOrGet (· + ·) (some 2) (some 3) = some 5`
* `Option.liftOrGet (· + ·) (some 2) none = some 2`
* `Option.liftOrGet (· + ·) none none = none`
-/
def liftOrGet (f : α → α → α) : Option α → Option α → Option α
| none, none => none
| some a, none => some a
| none, some b => some b
| some a, some b => some (f a b)
Option.Rel
inductive
(r : α → β → Prop) : Option α → Option β → Prop
/-- Lifts a relation `α → β → Prop` to a relation `Option α → Option β → Prop` by just adding
`none ~ none`. -/
inductive Rel (r : α → β → Prop) : Option α → Option β → Prop
/-- If `a ~ b`, then `some a ~ some b` -/
| some {a b} : r a b → Rel r (some a) (some b)
/-- `none ~ none` -/
| none : Rel r none none
Option.join
definition
(x : Option (Option α)) : Option α
Option.mapA
definition
[Applicative m] {α β} (f : α → m β) : Option α → m (Option β)
/--
Applies a function in some applicative functor to an optional value, returning `none` with no
effects if the value is missing.
This is analogous to `Option.mapM` for monads.
-/
@[inline] protected def mapA [Applicative m] {α β} (f : α → m β) : Option α → m (Option β)
| none => pure none
| some x => some <$> f x
Option.sequence
definition
[Monad m] {α : Type u} : Option (m α) → m (Option α)
/--
Converts an optional monadic computation into a monadic computation of an optional value.
Example:
```lean example
#eval show IO (Option String) from
Option.sequence <| some do
IO.println "hello"
return "world"
```
```output
hello
```
```output
some "world"
```
-/
@[inline] def sequence [Monad m] {α : Type u} : Option (m α) → m (Option α)
| none => pure none
| some fn => some <$> fn
Option.elimM
definition
[Monad m] (x : m (Option α)) (y : m β) (z : α → m β) : m β
/--
A monadic case analysis function for `Option`.
Given a fallback computation for `none` and a monadic operation to apply to the contents of `some`,
`Option.elimM` checks which constructor a given `Option` consists of, and uses the appropriate
argument.
`Option.elimM` can also be seen as a combination of `Option.mapM` and `Option.getDM`. It is a
monadic analogue of `Option.elim`.
-/
@[inline] def elimM [Monad m] (x : m (Option α)) (y : m β) (z : α → m β) : m β :=
do (← x).elim y z
Option.getDM
definition
[Monad m] (x : Option α) (y : m α) : m α
Option.instLawfulBEq
instance
(α) [BEq α] [LawfulBEq α] : LawfulBEq (Option α)
Option.all_none
theorem
: Option.all p none = true
@[simp] theorem all_none : Option.all p none = true := rfl
Option.all_some
theorem
: Option.all p (some x) = p x
@[simp] theorem all_some : Option.all p (some x) = p x := rfl
Option.any_none
theorem
: Option.any p none = false
@[simp] theorem any_none : Option.any p none = false := rfl
Option.any_some
theorem
: Option.any p (some x) = p x
@[simp] theorem any_some : Option.any p (some x) = p x := rfl
Option.min
definition
[Min α] : Option α → Option α → Option α
/--
The minimum of two optional values, with `none` treated as the least element. This function is
usually accessed through the `Min (Option α)` instance, rather than directly.
Prior to `nightly-2025-02-27`, `none` was treated as the greatest element, so
`min none (some x) = min (some x) none = some x`.
Examples:
* `Option.min (some 2) (some 5) = some 2`
* `Option.min (some 5) (some 2) = some 2`
* `Option.min (some 2) none = none`
* `Option.min none (some 5) = none`
* `Option.min none none = none`
-/
protected def min [Min α] : Option α → Option α → Option α
| some x, some y => some (Min.min x y)
| some _, none => none
| none, some _ => none
| none, none => none
Option.instMin
instance
[Min α] : Min (Option α)
instance [Min α] : Min (Option α) where min := Option.min
Option.min_some_some
theorem
[Min α] {a b : α} : min (some a) (some b) = some (min a b)
Option.min_some_none
theorem
[Min α] {a : α} : min (some a) none = none
Option.min_none_some
theorem
[Min α] {b : α} : min none (some b) = none
Option.min_none_none
theorem
[Min α] : min (none : Option α) none = none
Option.max
definition
[Max α] : Option α → Option α → Option α
/--
The maximum of two optional values.
This function is usually accessed through the `Max (Option α)` instance, rather than directly.
Examples:
* `Option.max (some 2) (some 5) = some 5`
* `Option.max (some 5) (some 2) = some 5`
* `Option.max (some 2) none = some 2`
* `Option.max none (some 5) = some 5`
* `Option.max none none = none`
-/
protected def max [Max α] : Option α → Option α → Option α
| some x, some y => some (Max.max x y)
| some x, none => some x
| none, some y => some y
| none, none => none
Option.instMax
instance
[Max α] : Max (Option α)
instance [Max α] : Max (Option α) where max := Option.max
Option.max_some_some
theorem
[Max α] {a b : α} : max (some a) (some b) = some (max a b)
Option.max_some_none
theorem
[Max α] {a : α} : max (some a) none = some a
Option.max_none_some
theorem
[Max α] {b : α} : max none (some b) = some b
Option.max_none_none
theorem
[Max α] : max (none : Option α) none = none
instLTOption
instance
[LT α] : LT (Option α)
instLEOption
instance
[LE α] : LE (Option α)
instFunctorOption
instance
: Functor Option
@[always_inline]
instance : Functor Option where
map := Option.map
instMonadOption
instance
: Monad Option
@[always_inline]
instance : Monad Option where
pure := Option.some
bind := Option.bind
instAlternativeOption
instance
: Alternative Option
@[always_inline]
instance : Alternative Option where
failure := Option.none
orElse := Option.orElse
liftOption
definition
[Alternative m] : Option α → m α
def liftOption [Alternative m] : Option α → m α
| some a => pure a
| none => failure
Option.tryCatch
definition
(x : Option α) (handle : Unit → Option α) : Option α
/--
Recover from failing `Option` computations with a handler function.
This function is usually accessed through the `MonadExceptOf Unit Option` instance.
Examples:
* `Option.tryCatch none (fun () => some "handled") = some "handled"`
* `Option.tryCatch (some "succeeded") (fun () => some "handled") = some "succeeded"`
-/
@[always_inline, inline] protected def Option.tryCatch (x : Option α) (handle : Unit → Option α) : Option α :=
match x with
| some _ => x
| none => handle ()
instMonadExceptOfUnitOption
instance
: MonadExceptOf Unit Option
instance : MonadExceptOf Unit Option where
throw := fun _ => Option.none
tryCatch := Option.tryCatch