lean4-htt/library/init/data/option/instances.lean
Mario Carneiro f5253fd060 fix(init/data/option/instances): Use option.* instead of option_*
This enables use of projection notation. Note that the notations are not always available here since they require one universe instead of two.
2017-09-05 08:35:26 +02:00

48 lines
1.7 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.data.option.basic
import init.meta.tactic
universes u v
@[inline] def option.bind {α : Type u} {β : Type v} : option α → (α → option β) → option β
| none b := none
| (some a) b := b a
def option.map {α β} (f : α → β) (o : option α) : option β :=
option.bind o (some ∘ f)
@[simp] theorem option.map_id {α} : (option.map id : option α → option α) = id :=
funext (λo, match o with | none := rfl | some x := rfl end)
instance : monad option :=
{pure := @some, bind := @option.bind, map := @option.map,
id_map := λ α x, option.rec rfl (λ x, rfl) x,
pure_bind := λ α β x f, rfl,
bind_assoc := λ α β γ x f g, option.rec rfl (λ x, rfl) x}
def option.orelse {α : Type u} : option α → option α → option α
| (some a) o := some a
| none (some a) := some a
| none none := none
instance : alternative option :=
{ option.monad with
failure := @none,
orelse := @option.orelse }
lemma option.eq_of_eq_some {α : Type u} : Π {x y : option α}, (∀z, x = some z ↔ y = some z) → x = y
| none none h := rfl
| none (some z) h := option.no_confusion ((h z).2 rfl)
| (some z) none h := option.no_confusion ((h z).1 rfl)
| (some z) (some w) h := option.no_confusion ((h w).2 rfl) (congr_arg some)
lemma option.eq_some_of_is_some {α : Type u} : Π {o : option α} (h : option.is_some o), o = some (option.get h)
| (some x) h := rfl
lemma option.eq_none_of_is_none {α : Type u} : Π {o : option α}, o.is_none → o = none
| none h := rfl