The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V01. The language of Mathlib

Mathlib.Data.Option.Basic

45 declarations. Mathlib docs · Source

Option of a type

This file develops the basic theory of option types.

If α is a type, then Option α can be understood as the type with one more element than α. Option α has terms some a, where a : α, and none, which is the added element. This is useful in multiple ways:

  • It is the prototype of addition of terms to a type. See for example WithBot α which uses none as an element smaller than all others.
  • It can be used to define failsafe partial functions, which return some the_result_we_expect if we can find the_result_we_expect, and none if there is no meaningful result. This forces any subsequent use of the partial function to explicitly deal with the exceptions that make it return none.
  • Option is a monad. We love monads.

Part is an alternative to Option that can be seen as the type of True/False values along with a term a : α if the value is True.

theorem Option.pmap.hcongr_6 E1 #

∀ (α α' : Type u_1),
  α = α' →
    ∀ (β β' : Type u_2),
      β = β' →
        ∀ (p : α → Prop) (p' : α' → Prop),
          p ≍ p' →
            ∀ (f : (a : α) → p a → β) (f' : (a : α') → p' a → β'),
              f ≍ f' →
                ∀ (o : Option α) (o' : Option α'),
                  o ≍ o' →
                    ∀ (a : ∀ (a : α), o = some a → p a) (a' : ∀ (a : α'), o' = some a → p' a),
                      a ≍ a' → Option.pmap f o a ≍ Option.pmap f' o' a'

theorem Option.coe_def E0 #

∀ {α : Type u_1}, (fun a ↦ some a) = some

theorem Option.mem_map E0 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {y : β} {o : Option α}, y ∈ Option.map f o ↔ ∃ x ∈ o, f x = y

theorem Option.mem_map_of_injective E0 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β},
  Function.Injective f → ∀ {a : α} {o : Option α}, f a ∈ Option.map f o ↔ a ∈ o

theorem Option.forall_mem_map E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {o : Option α} {p : β → Prop},
  (∀ y ∈ Option.map f o, p y) ↔ ∀ x ∈ o, p (f x)

theorem Option.exists_mem_map E1 #

∀ {α : Type u_1} {β : Type u_2} {f : α → β} {o : Option α} {p : β → Prop},
  (∃ y ∈ Option.map f o, p y) ↔ ∃ x ∈ o, p (f x)

theorem Option.coe_get E0 #

∀ {α : Type u_1} {o : Option α} (h : o.isSome = true), some (o.get h) = o

theorem Option.Mem.leftUnique E1 #

∀ {α : Type u_1}, Relator.LeftUnique fun x1 x2 ↦ x1 ∈ x2

theorem Option.some_injective E1 #

∀ (α : Type u_5), Function.Injective some

theorem Option.map_comp_some E0 #

∀ {α : Type u_1} {β : Type u_2} (f : α → β), Option.map f ∘ some = some ∘ f

theorem Option.bind_congr' E1 #

∀ {α : Type u_1} {β : Type u_2} {f g : α → Option β} {x y : Option α},
  x = y → (∀ a ∈ y, f a = g a) → x.bind f = y.bind g

theorem Option.joinM_eq_join E1 #

∀ {α : Type u_1}, joinM = Option.join

theorem Option.bind_eq_bind' E1 #

∀ {α β : Type u} {f : α → Option β} {x : Option α}, x >>= f = x.bind f

theorem Option.map_coe E0 #

∀ {α β : Type u_5} {a : α} {f : α → β}, f <$> some a = some (f a)

theorem Option.map_coe' E0 #

∀ {α : Type u_1} {β : Type u_2} {a : α} {f : α → β}, Option.map f (some a) = some (f a)

theorem Option.map_injective' E0 #

∀ {α : Type u_1} {β : Type u_2}, Function.Injective Option.map

Option.map as a function between functions is injective.

theorem Option.map_inj E0 #

∀ {α : Type u_1} {β : Type u_2} {f g : α → β}, Option.map f = Option.map g ↔ f = g

theorem Option.map_eq_id E0 #

∀ {α : Type u_1} {f : α → α}, Option.map f = id ↔ f = id

theorem Option.map_comm E0 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {f₁ : α → β} {f₂ : α → γ} {g₁ : β → δ} {g₂ : γ → δ},
  g₁ ∘ f₁ = g₂ ∘ f₂ → ∀ (a : α), Option.map g₁ (Option.map f₁ (some a)) = Option.map g₂ (Option.map f₂ (some a))

theorem Option.mem_pmem E0 #

∀ {α : Type u_1} {β : Type u_2} {p : α → Prop} (f : (a : α) → p a → β) (x : Option α) {a : α} (h : ∀ a ∈ x, p a)
  (ha : a ∈ x), f a ⋯ ∈ Option.pmap f x h

theorem Option.pmap_bind E1 #

∀ {α β γ : Type u_5} {x : Option α} {g : α → Option β} {p : β → Prop} {f : (b : β) → p b → γ}
  (H : ∀ (a : β), x >>= g = some a → p a) (H' : ∀ (a : α), ∀ b ∈ g a, b ∈ x >>= g),
  Option.pmap f (x >>= g) H = do
    let a ← x
    Option.pmap f (g a) ⋯

theorem Option.bind_pmap E1 #

∀ {α : Type u_5} {β γ : Type u_6} {p : α → Prop} (f : (a : α) → p a → β) (x : Option α) (g : β → Option γ)
  (H : ∀ (a : α), x = some a → p a), Option.pmap f x H >>= g = x.pbind fun a h ↦ g (f a ⋯)

theorem Option.pbind_eq_none E1 #

∀ {α : Type u_1} {β : Type u_2} {x : Option α} {f : (a : α) → a ∈ x → Option β},
  (∀ (a : α) (H : a ∈ x), f a H = none → x = none) → (x.pbind f = none ↔ x = none)

theorem Option.join_pmap_eq_pmap_join E1 #

∀ {α : Type u_1} {β : Type u_2} {p : α → Prop} {f : (a : α) → p a → β} {x : Option (Option α)}
  (H : ∀ (a : Option α), x = some a → ∀ (a_2 : α), a = some a_2 → p a_2),
  (Option.pmap (Option.pmap f) x H).join = Option.pmap f x.join ⋯

theorem Option.pmap_bind_id_eq_pmap_join E1 #

∀ {α : Type u_1} {β : Type u_2} {p : α → Prop} {f : (a : α) → p a → β} {x : Option (Option α)}
  (H : ∀ (a : Option α), x = some a → ∀ (a_2 : α), a = some a_2 → p a_2),
  ((Option.pmap (Option.pmap f) x H).bind fun a ↦ a) = Option.pmap f x.join ⋯

theorem Option.seq_some E0 #

∀ {α β : Type u_5} {a : α} {f : α → β}, some f <*> some a = some (f a)

theorem Option.failure_eq_none E0 #

∀ {α : Type u_5}, failure = none

theorem Option.guard_eq_some' E0 #

∀ {p : Prop} [inst : Decidable p] (u : Unit), guard p = some u ↔ p

def Option.casesOn' E1 #

{α : Type u_1} → {β : Type u_2} → Option α → β → (α → β) → β

Given an element of a : Option α, a default element b : β and a function α → β, apply this function to a if it comes from α, and return b otherwise.

theorem Option.casesOn'_none E0 #

∀ {α : Type u_1} {β : Type u_2} (x : β) (f : α → β), none.casesOn' x f = x

theorem Option.casesOn'_some E0 #

∀ {α : Type u_1} {β : Type u_2} (x : β) (f : α → β) (a : α), (some a).casesOn' x f = f a

theorem Option.casesOn'_coe E0 #

∀ {α : Type u_1} {β : Type u_2} (x : β) (f : α → β) (a : α), (some a).casesOn' x f = f a

theorem Option.casesOn'_none_coe E0 #

∀ {α : Type u_1} {β : Type u_2} (f : Option α → β) (o : Option α), o.casesOn' (f none) (f ∘ fun a ↦ some a) = f o

theorem Option.casesOn'_eq_elim E1 #

∀ {α : Type u_1} {β : Type u_2} (b : β) (f : α → β) (a : Option α), a.casesOn' b f = a.elim b f

theorem Option.orElse_eq_some E1 #

∀ {α : Type u_1} (o o' : Option α) (x : α), (o <|> o') = some x ↔ o = some x ∨ o = none ∧ o' = some x

theorem Option.orElse_eq_none E1 #

∀ {α : Type u_1} (o o' : Option α), (o <|> o') = none ↔ o = none ∧ o' = none

theorem Option.choice_eq_none E1 #

∀ (α : Type u_5) [IsEmpty α], Option.choice α = none

theorem Option.elim_none_some E0 #

∀ {α : Type u_1} {β : Type u_2} (f : Option α → β) (i : Option α), i.elim (f none) (f ∘ some) = f i

theorem Option.elim_comp E1 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} (h : α → β) {f : γ → α} {x : α} {i : Option γ},
  (i.elim (h x) fun j ↦ h (f j)) = h (i.elim x f)

theorem Option.elim_comp₂ E1 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} (h : α → β → γ) {f : γ → α} {x : α} {g : γ → β} {y : β} {i : Option γ},
  (i.elim (h x y) fun j ↦ h (f j) (g j)) = h (i.elim x f) (i.elim y g)

theorem Option.elim_apply E0 #

∀ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : γ → α → β} {x : α → β} {i : Option γ} {y : α},
  i.elim x f y = i.elim (x y) fun j ↦ f j y

theorem Option.elim'_update E0 #

∀ {α : Type u_5} {β : Type u_6} [inst : DecidableEq α] (f : β) (g : α → β) (a : α) (x : β),
  Option.elim' f (Function.update g a x) = Function.update (Option.elim' f g) (some a) x

theorem Option.getD_comp_some E0 #

∀ {α : Type u_1} (d : α), (fun x ↦ x.getD d) ∘ some = id

theorem Option.none_eq_map_iff E0 #

∀ {α : Type u_1} {β : Type u_2} {x : Option α} {f : α → β}, none = Option.map f x ↔ x = none

theorem Option.some_eq_map_iff E0 #

∀ {α : Type u_1} {β : Type u_2} {b : β} {x : Option α} {f : α → β}, some b = Option.map f x ↔ ∃ a, x = some a ∧ f a = b