~ubuntu-branches/ubuntu/utopic/agda-stdlib/utopic-proposed

« back to all changes in this revision

Viewing changes to src/Data/Maybe.agda

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-08-05 09:46:46 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140805094646-zqd0c4m8ndngqg6x
Tags: 0.8-1
* [4ca6fd0] Update debian/watch to fetch tarballs from github
* [84d4313] Imported Upstream version 0.8
* [7b08243] debian/control: Require agda 2.4.x per upstream
* [37e7e10] debian/control: Standards-Version → 3.9.5, no changes required.
* [9051b9d] Run upstream's "GenerateEverything" script

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
------------------------------------------------------------------------
17
17
-- Some operations
18
18
 
19
 
open import Data.Bool using (Bool; true; false)
 
19
open import Data.Bool using (Bool; true; false; not)
20
20
open import Data.Unit using (⊤)
 
21
open import Function
 
22
open import Relation.Nullary
21
23
 
22
24
boolToMaybe : Bool → Maybe ⊤
23
25
boolToMaybe true  = just _
24
26
boolToMaybe false = nothing
25
27
 
26
 
maybeToBool : ∀ {a} {A : Set a} → Maybe A → Bool
27
 
maybeToBool (just _) = true
28
 
maybeToBool nothing  = false
 
28
is-just : ∀ {a} {A : Set a} → Maybe A → Bool
 
29
is-just (just _) = true
 
30
is-just nothing  = false
 
31
 
 
32
is-nothing : ∀ {a} {A : Set a} → Maybe A → Bool
 
33
is-nothing = not ∘ is-just
 
34
 
 
35
decToMaybe : ∀ {a} {A : Set a} → Dec A → Maybe A
 
36
decToMaybe (yes x) = just x
 
37
decToMaybe (no _)  = nothing
29
38
 
30
39
-- A dependent eliminator.
31
40
 
53
62
------------------------------------------------------------------------
54
63
-- Maybe monad
55
64
 
56
 
open import Function
57
65
open import Category.Functor
58
66
open import Category.Monad
59
67
open import Category.Monad.Identity
60
68
 
 
69
map : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → Maybe A → Maybe B
 
70
map f = maybe (just ∘ f) nothing
 
71
 
61
72
functor : ∀ {f} → RawFunctor (Maybe {a = f})
62
73
functor = record
63
 
  { _<$>_ = λ f → maybe (just ∘ f) nothing
 
74
  { _<$>_ = map
64
75
  }
65
76
 
66
77
monadT : ∀ {f} {M : Set f → Set f} →
93
104
------------------------------------------------------------------------
94
105
-- Equality
95
106
 
96
 
open import Relation.Nullary
97
107
open import Relation.Binary as B
98
108
 
99
109
data Eq {a ℓ} {A : Set a} (_≈_ : Rel A ℓ) : Rel (Maybe A) (a ⊔ ℓ) where
149
159
------------------------------------------------------------------------
150
160
-- Any and All
151
161
 
 
162
open Data.Bool using (T)
152
163
open import Data.Empty using (⊥)
153
164
import Relation.Nullary.Decidable as Dec
154
165
open import Relation.Unary as U
155
166
 
156
 
data Any {a} {A : Set a} (P : A → Set a) : Maybe A → Set a where
 
167
data Any {a p} {A : Set a} (P : A → Set p) : Maybe A → Set (a ⊔ p) where
157
168
  just : ∀ {x} (px : P x) → Any P (just x)
158
169
 
159
 
data All {a} {A : Set a} (P : A → Set a) : Maybe A → Set a where
 
170
data All {a p} {A : Set a} (P : A → Set p) : Maybe A → Set (a ⊔ p) where
160
171
  just    : ∀ {x} (px : P x) → All P (just x)
161
172
  nothing : All P nothing
162
173
 
163
 
IsJust : ∀ {a} {A : Set a} → Maybe A → Set a
164
 
IsJust = Any (λ _ → Lift ⊤)
165
 
 
166
 
IsNothing : ∀ {a} {A : Set a} → Maybe A → Set a
167
 
IsNothing = All (λ _ → Lift ⊥)
168
 
 
169
 
private
170
 
 
171
 
  drop-just-Any : ∀ {A} {P : A → Set} {x} → Any P (just x) → P x
172
 
  drop-just-Any (just px) = px
173
 
 
174
 
  drop-just-All : ∀ {A} {P : A → Set} {x} → All P (just x) → P x
175
 
  drop-just-All (just px) = px
176
 
 
177
 
anyDec : ∀ {A} {P : A → Set} → U.Decidable P → U.Decidable (Any P)
 
174
Is-just : ∀ {a} {A : Set a} → Maybe A → Set a
 
175
Is-just = Any (λ _ → ⊤)
 
176
 
 
177
Is-nothing : ∀ {a} {A : Set a} → Maybe A → Set a
 
178
Is-nothing = All (λ _ → ⊥)
 
179
 
 
180
to-witness : ∀ {p} {P : Set p} {m : Maybe P} → Is-just m → P
 
181
to-witness (just {x = p} _) = p
 
182
 
 
183
to-witness-T : ∀ {p} {P : Set p} (m : Maybe P) → T (is-just m) → P
 
184
to-witness-T (just p) _  = p
 
185
to-witness-T nothing  ()
 
186
 
 
187
anyDec : ∀ {a p} {A : Set a} {P : A → Set p} →
 
188
         U.Decidable P → U.Decidable (Any P)
178
189
anyDec p nothing  = no λ()
179
 
anyDec p (just x) = Dec.map′ just drop-just-Any (p x)
 
190
anyDec p (just x) = Dec.map′ just (λ { (Any.just px) → px }) (p x)
180
191
 
181
 
allDec : ∀ {A} {P : A → Set} → U.Decidable P → U.Decidable (All P)
 
192
allDec : ∀ {a p} {A : Set a} {P : A → Set p} →
 
193
         U.Decidable P → U.Decidable (All P)
182
194
allDec p nothing  = yes nothing
183
 
allDec p (just x) = Dec.map′ just drop-just-All (p x)
 
195
allDec p (just x) = Dec.map′ just (λ { (All.just px) → px }) (p x)