module Data.Maybe.Base where
The Maybe typeπ
data Maybe {β} (A : Type β) : Type β where nothing : Maybe A just : A β Maybe A {-# BUILTIN MAYBE Maybe #-}
instance Map-Maybe : Map (eff Maybe) Map-Maybe .map f (just x) = just (f x) Map-Maybe .map f nothing = nothing Idiom-Maybe : Idiom (eff Maybe) Idiom-Maybe .Idiom.Map-idiom = Map-Maybe Idiom-Maybe .Idiom.pure = just Idiom-Maybe .Idiom._<*>_ (just f) (just x) = just (f x) Idiom-Maybe .Idiom._<*>_ (just f) nothing = nothing Idiom-Maybe .Idiom._<*>_ nothing _ = nothing Bind-Maybe : Bind (eff Maybe) Bind-Maybe .Bind.Idiom-bind = Idiom-Maybe Bind-Maybe ._>>=_ (just x) f = f x Bind-Maybe ._>>=_ nothing f = nothing Alt-Maybe : Alt (eff Maybe) Alt-Maybe .Alt.fail = nothing Alt-Maybe .Alt._<|>_ (just x) y = just x Alt-Maybe .Alt._<|>_ nothing y = y Traversable-Maybe : Traversable (eff Maybe) Traversable-Maybe .Traversable.traverse f nothing = pure nothing Traversable-Maybe .Traversable.traverse f (just x) = just <$> f x Maybe-rec : (A β B) β B β Maybe A β B Maybe-rec f b (just x) = f x Maybe-rec f b nothing = b
from-just : A β Maybe A β A from-just def (just x) = x from-just def nothing = def
is-just : Maybe A β Type is-just (just _) = β€ is-just nothing = β₯ is-nothing : Maybe A β Type is-nothing (just _) = β₯ is-nothing nothing = β€ nothingβ just : {x : A} β Β¬ (nothing β‘ just x) nothingβ just p = subst is-nothing p tt justβ nothing : {x : A} β Β¬ (just x β‘ nothing) justβ nothing p = subst is-just p tt just-inj : β {x y : A} β just x β‘ just y β x β‘ y just-inj {x = x} = ap (from-just x) instance Discrete-Maybe : β¦ d : Discrete A β¦ β Discrete (Maybe A) Discrete-Maybe {x = just x} {just y} = invmap (ap just) just-inj (x β‘? y) Discrete-Maybe {x = just x} {nothing} = no justβ nothing Discrete-Maybe {x = nothing} {just x} = no nothingβ just Discrete-Maybe {x = nothing} {nothing} = yes refl
Pointed types as Maybe-algebrasπ
maybeβalt : β {M : Effect} {β} {A : Type β} β β¦ _ : Alt M β¦ β¦ _ : Idiom M β¦ β Maybe A β M .Effect.β A maybeβalt (just x) = pure x maybeβalt nothing = fail