forked from favonia/homotopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrelude.agda
346 lines (232 loc) · 8.19 KB
/
Prelude.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
------------------------------------------------------------------------
-- A small prelude
------------------------------------------------------------------------
-- Copyright (c) 2012 Favonia
-- Copyright (c) 2011-2012 Nils Anders Danielsson
{-# OPTIONS --without-K #-}
-- Note that parts of Agda's standard library make use of the K rule.
module Prelude where
------------------------------------------------------------------------
-- Support for universe polymorphism
-- Universe levels.
infixl 6 _⊔_
postulate
Level : Set
lzero : Level
lsuc : Level → Level
_⊔_ : Level → Level → Level
{-# BUILTIN LEVEL Level #-}
{-# BUILTIN LEVELZERO lzero #-}
{-# BUILTIN LEVELSUC lsuc #-}
{-# BUILTIN LEVELMAX _⊔_ #-}
-- Lifting.
record ↑ {a} ℓ (A : Set a) : Set (a ⊔ ℓ) where
constructor lift
field lower : A
open ↑ public
------------------------------------------------------------------------
-- Some finite types
-- The empty type.
data ⊥ {ℓ} : Set ℓ where
⊥-elim : ∀ {w ℓ} {Whatever : Set w} → ⊥ {ℓ = ℓ} → Whatever
⊥-elim ()
-- Negation.
infix 3 ¬_
¬_ : ∀ {ℓ} → Set ℓ → Set ℓ
¬ P = P → ⊥ {ℓ = lzero}
-- The unit type.
record ⊤ : Set where
constructor tt
-- Booleans.
data Bool : Set where
true false : Bool
{-# BUILTIN BOOL Bool #-}
{-# BUILTIN TRUE true #-}
{-# BUILTIN FALSE false #-}
-- Conditional.
if_then_else_ : ∀ {a} {A : Set a} → Bool → A → A → A
if true then t else f = t
if false then t else f = f
-- Not.
not : Bool → Bool
not b = if b then false else true
------------------------------------------------------------------------
-- Natural numbers
data ℕ : Set where
zero : ℕ
suc : (n : ℕ) → ℕ
-- Support for natural number literals.
{-# BUILTIN NATURAL ℕ #-}
{-# BUILTIN ZERO zero #-}
{-# BUILTIN SUC suc #-}
-- Dependent eliminator.
ℕ-rec : ∀ {p} {P : ℕ → Set p} →
P 0 → (∀ n → P n → P (suc n)) → ∀ n → P n
ℕ-rec z s zero = z
ℕ-rec z s (suc n) = s n (ℕ-rec z s n)
-- Addition.
infixl 6 _+_
_+_ : ℕ → ℕ → ℕ
zero + n = n
suc m + n = suc (m + n)
------------------------------------------------------------------------
-- Simple combinators working solely on and with functions
infixr 9 _∘_
infixr 0 _$_
-- The identity function.
id : ∀ {a} {A : Set a} → A → A
id x = x
-- Composition.
_∘_ : ∀ {a b c}
{A : Set a} {B : A → Set b} {C : {x : A} → B x → Set c} →
(∀ {x} (y : B x) → C y) → (g : (x : A) → B x) →
((x : A) → C (g x))
f ∘ g = λ x → f (g x)
-- Application.
_$_ : ∀ {a b} {A : Set a} {B : A → Set b} →
((x : A) → B x) → ((x : A) → B x)
f $ x = f x
-- Constant functions.
const : ∀ {a b} {A : Set a} {B : Set b} → A → (B → A)
const x = λ _ → x
-- Flips the first two arguments.
flip : ∀ {a b c} {A : Set a} {B : Set b} {C : A → B → Set c} →
((x : A) (y : B) → C x y) → ((y : B) (x : A) → C x y)
flip f = λ x y → f y x
------------------------------------------------------------------------
-- Σ-types
infixr 4 _,_
infixr 2 _×_
record Σ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
constructor _,_
field
proj₁ : A
proj₂ : B proj₁
open Σ public
-- A variant where the first argument is implicit.
∃ : ∀ {a b} {A : Set a} → (A → Set b) → Set (a ⊔ b)
∃ = Σ _
-- Binary products.
_×_ : ∀ {a b} (A : Set a) (B : Set b) → Set (a ⊔ b)
A × B = Σ A (const B)
-- A map function.
Σ-map : ∀ {a b p q}
{A : Set a} {B : Set b} {P : A → Set p} {Q : B → Set q} →
(f : A → B) → (∀ {x} → P x → Q (f x)) →
Σ A P → Σ B Q
Σ-map f g = λ p → (f (proj₁ p) , g (proj₂ p))
-- Curry and uncurry.
curry : ∀ {a b c} {A : Set a} {B : A → Set b} {C : Σ A B → Set c} →
((p : Σ A B) → C p) →
((x : A) (y : B x) → C (x , y))
curry f x y = f (x , y)
uncurry : ∀ {a b c} {A : Set a} {B : A → Set b} {C : Σ A B → Set c} →
((x : A) (y : B x) → C (x , y)) →
((p : Σ A B) → C p)
uncurry f (x , y) = f x y
------------------------------------------------------------------------
-- W-types
data W {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
sup : (x : A) (f : B x → W A B) → W A B
-- Projections.
head : ∀ {a b} {A : Set a} {B : A → Set b} →
W A B → A
head (sup x f) = x
tail : ∀ {a b} {A : Set a} {B : A → Set b} →
(x : W A B) → B (head x) → W A B
tail (sup x f) = f
-- If B is always inhabited, then W A B is empty.
abstract
inhabited⇒W-empty : ∀ {a b} {A : Set a} {B : A → Set b} →
(∀ x → B x) → ¬ W A B
inhabited⇒W-empty b (sup x f) = inhabited⇒W-empty b (f (b x))
------------------------------------------------------------------------
-- Support for coinduction
infix 1000 ♯_
postulate
∞ : ∀ {a} (A : Set a) → Set a
♯_ : ∀ {a} {A : Set a} → A → ∞ A
♭ : ∀ {a} {A : Set a} → ∞ A → A
{-# BUILTIN INFINITY ∞ #-}
{-# BUILTIN SHARP ♯_ #-}
{-# BUILTIN FLAT ♭ #-}
------------------------------------------------------------------------
-- M-types
data M {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
dns : (x : A) (f : B x → ∞ (M A B)) → M A B
-- Projections.
pɐǝɥ : ∀ {a b} {A : Set a} {B : A → Set b} →
M A B → A
pɐǝɥ (dns x f) = x
lıɐʇ : ∀ {a b} {A : Set a} {B : A → Set b} →
(x : M A B) → B (pɐǝɥ x) → M A B
lıɐʇ (dns x f) y = ♭ (f y)
------------------------------------------------------------------------
-- Binary sums
infixr 1 _⊎_
data _⊎_ {a b} (A : Set a) (B : Set b) : Set (a ⊔ b) where
inj₁ : (x : A) → A ⊎ B
inj₂ : (y : B) → A ⊎ B
-- Eliminator for binary sums.
[_,_] : ∀ {a b c} {A : Set a} {B : Set b} {C : A ⊎ B → Set c} →
((x : A) → C (inj₁ x)) → ((x : B) → C (inj₂ x)) →
((x : A ⊎ B) → C x)
[ f , g ] (inj₁ x) = f x
[ f , g ] (inj₂ y) = g y
-- A map function.
⊎-map : ∀ {a₁ a₂ b₁ b₂}
{A₁ : Set a₁} {A₂ : Set a₂} {B₁ : Set b₁} {B₂ : Set b₂} →
(A₁ → A₂) → (B₁ → B₂) → A₁ ⊎ B₁ → A₂ ⊎ B₂
⊎-map f g = [ inj₁ ∘ f , inj₂ ∘ g ]
-- A special case of binary sums: decided predicates.
Dec : ∀ {p} → Set p → Set p
Dec P = P ⊎ ¬ P
-- Decidable relations.
Decidable : ∀ {a b ℓ} {A : Set a} {B : Set b} →
(A → B → Set ℓ) → Set (a ⊔ b ⊔ ℓ)
Decidable _∼_ = ∀ x y → Dec (x ∼ y)
------------------------------------------------------------------------
-- Lists
infixr 5 _∷_
data List {a} (A : Set a) : Set a where
[] : List A
_∷_ : (x : A) (xs : List A) → List A
{-# BUILTIN LIST List #-}
{-# BUILTIN NIL [] #-}
{-# BUILTIN CONS _∷_ #-}
-- Right fold.
foldr : ∀ {a b} {A : Set a} {B : Set b} →
(A → B → B) → B → List A → B
foldr _⊕_ ε [] = ε
foldr _⊕_ ε (x ∷ xs) = x ⊕ foldr _⊕_ ε xs
-- The length of a list.
length : ∀ {a} {A : Set a} → List A → ℕ
length = foldr (const suc) 0
-- Appends two lists.
infixr 5 _++_
_++_ : ∀ {a} {A : Set a} → List A → List A → List A
xs ++ ys = foldr _∷_ ys xs
-- Maps a function over a list.
map : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → List A → List B
map f = foldr (λ x ys → f x ∷ ys) []
-- Concatenates a list of lists.
concat : ∀ {a} {A : Set a} → List (List A) → List A
concat = foldr _++_ []
-- The list monad's bind operation.
infixl 5 _>>=_
_>>=_ : ∀ {a b} {A : Set a} {B : Set b} →
List A → (A → List B) → List B
xs >>= f = concat (map f xs)
-- A filter function.
filter : ∀ {a} {A : Set a} → (A → Bool) → List A → List A
filter p = foldr (λ x xs → if p x then x ∷ xs else xs) []
------------------------------------------------------------------------
-- Finite sets
Fin : ℕ → Set
Fin zero = ⊥
Fin (suc n) = ⊤ ⊎ Fin n
-- A lookup function.
lookup : ∀ {a} {A : Set a} (xs : List A) → Fin (length xs) → A
lookup [] ()
lookup (x ∷ xs) (inj₁ tt) = x
lookup (x ∷ xs) (inj₂ i) = lookup xs i