forked from agda/agda-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.agda
366 lines (267 loc) · 8.81 KB
/
Base.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
------------------------------------------------------------------------
-- The Agda standard library
--
-- Natural numbers, basic types and operations
------------------------------------------------------------------------
-- See README.Data.Nat for examples of how to use and reason about
-- naturals.
{-# OPTIONS --cubical-compatible --safe #-}
module Data.Nat.Base where
open import Algebra.Bundles.Raw using (RawMagma; RawMonoid; RawNearSemiring; RawSemiring)
open import Data.Bool.Base using (Bool; true; false; T; not)
open import Data.Parity.Base using (Parity; 0ℙ; 1ℙ)
open import Level using (0ℓ)
open import Relation.Binary.Core using (Rel)
open import Relation.Binary.PropositionalEquality.Core
using (_≡_; _≢_; refl)
open import Relation.Nullary.Negation.Core using (¬_; contradiction)
------------------------------------------------------------------------
-- Types
open import Agda.Builtin.Nat public
using (zero; suc) renaming (Nat to ℕ)
------------------------------------------------------------------------
-- Boolean equality relation
open import Agda.Builtin.Nat public
using () renaming (_==_ to _≡ᵇ_)
------------------------------------------------------------------------
-- Boolean ordering relation
open import Agda.Builtin.Nat public
using () renaming (_<_ to _<ᵇ_)
infix 4 _≤ᵇ_
_≤ᵇ_ : (m n : ℕ) → Bool
zero ≤ᵇ n = true
suc m ≤ᵇ n = m <ᵇ n
------------------------------------------------------------------------
-- Standard ordering relations
infix 4 _≤_ _<_ _≥_ _>_ _≰_ _≮_ _≱_ _≯_
data _≤_ : Rel ℕ 0ℓ where
z≤n : ∀ {n} → zero ≤ n
s≤s : ∀ {m n} (m≤n : m ≤ n) → suc m ≤ suc n
_<_ : Rel ℕ 0ℓ
m < n = suc m ≤ n
-- Smart constructors of _<_
pattern z<s {n} = s≤s (z≤n {n})
pattern s<s {m} {n} m<n = s≤s {m} {n} m<n
------------------------------------------------------------------------
-- other ordering relations
_≥_ : Rel ℕ 0ℓ
m ≥ n = n ≤ m
_>_ : Rel ℕ 0ℓ
m > n = n < m
_≰_ : Rel ℕ 0ℓ
a ≰ b = ¬ a ≤ b
_≮_ : Rel ℕ 0ℓ
a ≮ b = ¬ a < b
_≱_ : Rel ℕ 0ℓ
a ≱ b = ¬ a ≥ b
_≯_ : Rel ℕ 0ℓ
a ≯ b = ¬ a > b
------------------------------------------------------------------------
-- Simple predicates
-- Defining `NonZero` in terms of `T` and therefore ultimately `⊤` and
-- `⊥` allows Agda to automatically infer nonZero-ness for any natural
-- of the form `suc n`. Consequently in many circumstances this
-- eliminates the need to explicitly pass a proof when the NonZero
-- argument is either an implicit or an instance argument.
--
-- See `Data.Nat.DivMod` for an example.
record NonZero (n : ℕ) : Set where
field
nonZero : T (not (n ≡ᵇ 0))
-- Instances
instance
nonZero : ∀ {n} → NonZero (suc n)
nonZero = _
-- Constructors
≢-nonZero : ∀ {n} → n ≢ 0 → NonZero n
≢-nonZero {zero} 0≢0 = contradiction refl 0≢0
≢-nonZero {suc n} n≢0 = _
>-nonZero : ∀ {n} → n > 0 → NonZero n
>-nonZero z<s = _
-- Destructors
≢-nonZero⁻¹ : ∀ n → .{{NonZero n}} → n ≢ 0
≢-nonZero⁻¹ (suc n) ()
>-nonZero⁻¹ : ∀ n → .{{NonZero n}} → n > 0
>-nonZero⁻¹ (suc n) = z<s
------------------------------------------------------------------------
-- Arithmetic
open import Agda.Builtin.Nat public
using (_+_; _*_) renaming (_-_ to _∸_)
open import Agda.Builtin.Nat
using (div-helper; mod-helper)
pred : ℕ → ℕ
pred n = n ∸ 1
infix 8 _!
infixl 7 _⊓_ _⊓′_ _/_ _%_
infixl 6 _+⋎_ _⊔_ _⊔′_
-- Argument-swapping addition. Used by Data.Vec._⋎_.
_+⋎_ : ℕ → ℕ → ℕ
zero +⋎ n = n
suc m +⋎ n = suc (n +⋎ m)
-- Max.
_⊔_ : ℕ → ℕ → ℕ
zero ⊔ n = n
suc m ⊔ zero = suc m
suc m ⊔ suc n = suc (m ⊔ n)
-- Max defined in terms of primitive operations.
-- This is much faster than `_⊔_` but harder to reason about. For proofs
-- involving this function, convert it to `_⊔_` with `Data.Nat.Properties.⊔≡⊔‵`.
_⊔′_ : ℕ → ℕ → ℕ
m ⊔′ n with m <ᵇ n
... | false = m
... | true = n
-- Min.
_⊓_ : ℕ → ℕ → ℕ
zero ⊓ n = zero
suc m ⊓ zero = zero
suc m ⊓ suc n = suc (m ⊓ n)
-- Min defined in terms of primitive operations.
-- This is much faster than `_⊓_` but harder to reason about. For proofs
-- involving this function, convert it to `_⊓_` wtih `Data.Nat.properties.⊓≡⊓′`.
_⊓′_ : ℕ → ℕ → ℕ
m ⊓′ n with m <ᵇ n
... | false = n
... | true = m
-- Parity
parity : ℕ → Parity
parity 0 = 0ℙ
parity 1 = 1ℙ
parity (suc (suc n)) = parity n
-- Division by 2, rounded downwards.
⌊_/2⌋ : ℕ → ℕ
⌊ 0 /2⌋ = 0
⌊ 1 /2⌋ = 0
⌊ suc (suc n) /2⌋ = suc ⌊ n /2⌋
-- Division by 2, rounded upwards.
⌈_/2⌉ : ℕ → ℕ
⌈ n /2⌉ = ⌊ suc n /2⌋
-- Naïve exponentiation
infixr 8 _^_
_^_ : ℕ → ℕ → ℕ
x ^ zero = 1
x ^ suc n = x * x ^ n
-- Distance
∣_-_∣ : ℕ → ℕ → ℕ
∣ zero - y ∣ = y
∣ x - zero ∣ = x
∣ suc x - suc y ∣ = ∣ x - y ∣
-- Distance in terms of primitive operations.
-- This is much faster than `∣_-_∣` but harder to reason about. For proofs
-- involving this function, convert it to `∣_-_∣` with
-- `Data.Nat.Properties.∣-∣≡∣-∣′`.
∣_-_∣′ : ℕ → ℕ → ℕ
∣ x - y ∣′ with x <ᵇ y
... | false = x ∸ y
... | true = y ∸ x
-- Division
-- Note properties of these are in `Nat.DivMod` not `Nat.Properties`
_/_ : (dividend divisor : ℕ) .{{_ : NonZero divisor}} → ℕ
m / (suc n) = div-helper 0 n m n
-- Remainder/modulus
-- Note properties of these are in `Nat.DivMod` not `Nat.Properties`
_%_ : (dividend divisor : ℕ) .{{_ : NonZero divisor}} → ℕ
m % (suc n) = mod-helper 0 n m n
-- Factorial
_! : ℕ → ℕ
zero ! = 1
suc n ! = suc n * n !
------------------------------------------------------------------------
-- Alternative definition of _≤_
-- The following definition of _≤_ is more suitable for well-founded
-- induction (see Data.Nat.Induction)
infix 4 _≤′_ _<′_ _≥′_ _>′_
data _≤′_ (m : ℕ) : ℕ → Set where
≤′-refl : m ≤′ m
≤′-step : ∀ {n} (m≤′n : m ≤′ n) → m ≤′ suc n
_<′_ : Rel ℕ 0ℓ
m <′ n = suc m ≤′ n
-- Smart constructors of _<′_
pattern <′-base = ≤′-refl
pattern <′-step {n} m<′n = ≤′-step {n} m<′n
_≥′_ : Rel ℕ 0ℓ
m ≥′ n = n ≤′ m
_>′_ : Rel ℕ 0ℓ
m >′ n = n <′ m
------------------------------------------------------------------------
-- Another alternative definition of _≤_
record _≤″_ (m n : ℕ) : Set where
constructor less-than-or-equal
field
{k} : ℕ
proof : m + k ≡ n
infix 4 _≤″_ _<″_ _≥″_ _>″_
_<″_ : Rel ℕ 0ℓ
m <″ n = suc m ≤″ n
_≥″_ : Rel ℕ 0ℓ
m ≥″ n = n ≤″ m
_>″_ : Rel ℕ 0ℓ
m >″ n = n <″ m
------------------------------------------------------------------------
-- Another alternative definition of _≤_
-- Useful for induction when you have an upper bound.
data _≤‴_ : ℕ → ℕ → Set where
≤‴-refl : ∀{m} → m ≤‴ m
≤‴-step : ∀{m n} → suc m ≤‴ n → m ≤‴ n
infix 4 _≤‴_ _<‴_ _≥‴_ _>‴_
_<‴_ : Rel ℕ 0ℓ
m <‴ n = suc m ≤‴ n
_≥‴_ : Rel ℕ 0ℓ
m ≥‴ n = n ≤‴ m
_>‴_ : Rel ℕ 0ℓ
m >‴ n = n <‴ m
------------------------------------------------------------------------
-- A comparison view. Taken from "View from the left"
-- (McBride/McKinna); details may differ.
data Ordering : Rel ℕ 0ℓ where
less : ∀ m k → Ordering m (suc (m + k))
equal : ∀ m → Ordering m m
greater : ∀ m k → Ordering (suc (m + k)) m
compare : ∀ m n → Ordering m n
compare zero zero = equal zero
compare (suc m) zero = greater zero m
compare zero (suc n) = less zero n
compare (suc m) (suc n) with compare m n
... | less m k = less (suc m) k
... | equal m = equal (suc m)
... | greater n k = greater (suc n) k
------------------------------------------------------------------------
-- Raw bundles
+-rawMagma : RawMagma 0ℓ 0ℓ
+-rawMagma = record
{ _≈_ = _≡_
; _∙_ = _+_
}
+-0-rawMonoid : RawMonoid 0ℓ 0ℓ
+-0-rawMonoid = record
{ _≈_ = _≡_
; _∙_ = _+_
; ε = 0
}
*-rawMagma : RawMagma 0ℓ 0ℓ
*-rawMagma = record
{ _≈_ = _≡_
; _∙_ = _*_
}
*-1-rawMonoid : RawMonoid 0ℓ 0ℓ
*-1-rawMonoid = record
{ _≈_ = _≡_
; _∙_ = _*_
; ε = 1
}
+-*-rawNearSemiring : RawNearSemiring 0ℓ 0ℓ
+-*-rawNearSemiring = record
{ Carrier = _
; _≈_ = _≡_
; _+_ = _+_
; _*_ = _*_
; 0# = 0
}
+-*-rawSemiring : RawSemiring 0ℓ 0ℓ
+-*-rawSemiring = record
{ Carrier = _
; _≈_ = _≡_
; _+_ = _+_
; _*_ = _*_
; 0# = 0
; 1# = 1
}