lean4-htt/src/Init/Data/Option/Basic.lean
2020-12-17 17:48:23 -08:00

89 lines
2.4 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
import Init.Control.Basic
import Init.Coe
namespace Option
def toMonad [Monad m] [Alternative m] : Option α → m α
| none => failure
| some a => pure a
@[macroInline] def getD : Option ααα
| some x, _ => x
| none, e => e
@[inline] def toBool : Option α → Bool
| some _ => true
| none => false
@[inline] def isSome : Option α → Bool
| some _ => true
| none => false
@[inline] def isNone : Option α → Bool
| some _ => false
| none => true
@[inline] protected def bind : 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
}
@[inline] protected def filter (p : α → Bool) : Option α → Option α
| some a => if p a then some a else none
| none => none
@[inline] protected def all (p : α → Bool) : Option α → Bool
| some a => p a
| none => true
@[inline] protected def any (p : α → Bool) : Option α → Bool
| some a => p a
| none => false
@[macroInline] protected def orElse : 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 where
failure := none
orElse := Option.orElse
@[inline] protected def lt (r : αα → Prop) : Option α → Option α → Prop
| none, some x => True
| some x, some y => r x y
| _, _ => False
instance (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
deriving instance DecidableEq for Option
deriving instance BEq for Option
instance [HasLess α] : HasLess (Option α) := {
Less := Option.lt (· < ·)
}