lean4-htt/library/init/data/option/basic.lean
2019-07-02 13:22:11 -07:00

88 lines
2.8 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) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.core init.control.monad init.control.alternative init.coe
open Decidable
universes u v
namespace Option
def toMonad {m : Type → Type} [Monad m] [Alternative m] {A} : Option A → m A
| none := failure
| (some a) := pure a
@[macroInline] def getOrElse {α : Type u} : Option ααα
| (some x) _ := x
| none e := e
@[inline] def get {α : Type u} [Inhabited α] : Option αα
| (some x) := x
| none := default α
@[inline] def toBool {α : Type u} : Option α → Bool
| (some _) := true
| none := false
@[inline] def isSome {α : Type u} : Option α → Bool
| (some _) := true
| none := false
@[inline] def isNone {α : Type u} : Option α → Bool
| (some _) := false
| none := true
@[inline] protected def bind {α : Type u} {β : Type v} : Option α → (α → Option β) → Option β
| none b := none
| (some a) b := b a
@[inline] protected def map {α β} (f : α → β) (o : Option α) : Option β :=
Option.bind o (some ∘ f)
theorem mapId {α} : (Option.map id : Option α → Option α) = id :=
funext (fun o => match o with | none := rfl | some x := rfl)
instance : Monad Option :=
{pure := @some, bind := @Option.bind, map := @Option.map}
@[macroInline] protected def orelse {α : Type u} : Option α → Option α → Option α
| (some a) _ := some a
| none b := b
/- Remark: when using the polymorphic notation `a <|> b` is not a `[macroInline]`.
Thus, `a <|> b` will make `Option.orelse` to behave like it was marked as `[inline]`. -/
instance : Alternative Option :=
{ failure := @none,
orelse := @Option.orelse,
..Option.Monad }
@[inline] protected def lt {α : Type u} (r : αα → Prop) : Option α → Option α → Prop
| none (some x) := True
| (some x) (some y) := r x y
| _ _ := False
instance decidableRelLt {α : Type u} (r : αα → Prop) [s : DecidableRel r] : DecidableRel (Option.lt r)
| none (some y) := isTrue trivial
| (some x) (some y) := s x y
| (some x) none := isFalse notFalse
| none none := isFalse notFalse
end Option
instance (α : Type u) : Inhabited (Option α) :=
⟨none⟩
instance {α : Type u} [DecidableEq α] : DecidableEq (Option α) :=
{decEq := fun a b => match a, b with
| none, none := isTrue rfl
| none, (some v₂) := isFalse (fun h => Option.noConfusion h)
| (some v₁), none := isFalse (fun h => Option.noConfusion h)
| (some v₁), (some v₂) :=
match decEq v₁ v₂ with
| (isTrue e) := isTrue (congrArg (@some α) e)
| (isFalse n) := isFalse (fun h => Option.noConfusion h (fun e => absurd e n))}
instance {α : Type u} [HasLess α] : HasLess (Option α) := ⟨Option.lt HasLess.Less⟩