Debian Codegraph @

src/Data/Maybe/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The Maybe type and some operations
5 ------------------------------------------------------------------------
6
7 -- The definitions in this file are reexported by Data.Maybe.
8
9 {-# OPTIONS --cubical-compatible --safe #-}
10
11 module Data.Maybe.Base where
12
13 open import Level using (Level; Lift)
14 open import Data.Bool.Base using (Bool; true; false; not)
15 open import Data.Unit.Base using (⊤)
16 open import Data.These.Base using (These; this; that; these)
17 open import Data.Product.Base as Prod using (_×_; _,_)
18 open import Function.Base using (_∘_; id; const)
19 import Relation.Nullary.Decidable.Core as Dec
20
21 private
22 variable
23 a b c : Level
24 A : Set a
25 B : Set b
26 C : Set c
27
28 ------------------------------------------------------------------------
29 -- Definition
30
31 open import Agda.Builtin.Maybe public
32 using (Maybe; just; nothing)
33
34 ------------------------------------------------------------------------
35 -- Some operations
36
37 boolToMaybe : Bool → Maybe ⊤
38 boolToMaybe true = just _
39 boolToMaybe false = nothing
40
41 is-just : Maybe A → Bool
42 is-just (just _) = true
43 is-just nothing = false
44
45 is-nothing : Maybe A → Bool
46 is-nothing = not ∘ is-just
47
48 -- A dependent eliminator.
49
50 maybe : ∀ {A : Set a} {B : Maybe A → Set b} →
51 ((x : A) → B (just x)) → B nothing → (x : Maybe A) → B x
52 maybe j n (just x) = j x
53 maybe j n nothing = n
54
55 -- A non-dependent eliminator.
56
57 maybe′ : (A → B) → B → Maybe A → B
58 maybe′ = maybe
59
60 -- A defaulting mechanism
61
62 fromMaybe : A → Maybe A → A
63 fromMaybe = maybe′ id
64
65 -- A safe variant of "fromJust". If the value is nothing, then the
66 -- return type is the unit type.
67
68 module _ {a} {A : Set a} where
69
70 From-just : Maybe A → Set a
71 From-just (just _) = A
72 From-just nothing = Lift a ⊤
73
74 from-just : (x : Maybe A) → From-just x
75 from-just (just x) = x
76 from-just nothing = _
77
78 -- Functoriality: map
79
80 map : (A → B) → Maybe A → Maybe B
81 map f = maybe (just ∘ f) nothing
82
83 -- Applicative: ap
84
85 ap : Maybe (A → B) → Maybe A → Maybe B
86 ap nothing = const nothing
87 ap (just f) = map f
88
89 -- Monad: bind
90
91 infixl 1 _>>=_
92 _>>=_ : Maybe A → (A → Maybe B) → Maybe B
93 nothing >>= f = nothing
94 just a >>= f = f a
95
96 -- Alternative: <∣>
97
98 infixr 6 _<∣>_
99 _<∣>_ : Maybe A → Maybe A → Maybe A
100 just x <∣> my = just x
101 nothing <∣> my = my
102
103 -- Just when the boolean is true
104
105 when : Bool → A → Maybe A
106 when b c = map (const c) (boolToMaybe b)
107
108 ------------------------------------------------------------------------
109 -- Aligning and zipping
110
111 alignWith : (These A B → C) → Maybe A → Maybe B → Maybe C
112 alignWith f (just a) (just b) = just (f (these a b))
113 alignWith f (just a) nothing = just (f (this a))
114 alignWith f nothing (just b) = just (f (that b))
115 alignWith f nothing nothing = nothing
116
117 zipWith : (A → B → C) → Maybe A → Maybe B → Maybe C
118 zipWith f (just a) (just b) = just (f a b)
119 zipWith _ _ _ = nothing
120
121 align : Maybe A → Maybe B → Maybe (These A B)
122 align = alignWith id
123
124 zip : Maybe A → Maybe B → Maybe (A × B)
125 zip = zipWith _,_
126
127 ------------------------------------------------------------------------
128 -- Injections.
129
130 thisM : A → Maybe B → These A B
131 thisM a = maybe′ (these a) (this a)
132
133 thatM : Maybe A → B → These A B
134 thatM = maybe′ these that
135
136 ------------------------------------------------------------------------
137 -- DEPRECATED NAMES
138 ------------------------------------------------------------------------
139 -- Please use the new names as continuing support for the old names is
140 -- not guaranteed.
141
142 -- Version 2.1
143 -- decToMaybe
144
145 open Dec using (decToMaybe) public