| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Natural numbers, basic types and operations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- See README.Data.Nat for examples of how to use and reason about |
| 8 |
-- naturals. |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
|
| 12 |
module Data.Nat.Base where |
| 13 |
|
| 14 |
open import Algebra.Bundles.Raw using (RawMagma; RawMonoid; RawNearSemiring; RawSemiring) |
| 15 |
open import Algebra.Definitions.RawMagma using (_∣ˡ_; _,_) |
| 16 |
open import Data.Bool.Base using (Bool; true; false; T; not) |
| 17 |
open import Data.Parity.Base using (Parity; 0ℙ; 1ℙ) |
| 18 |
open import Level using (0ℓ) |
| 19 |
open import Relation.Binary.Core using (Rel) |
| 20 |
open import Relation.Binary.PropositionalEquality.Core |
| 21 |
using (_≡_; _≢_; refl; cong) |
| 22 |
open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 23 |
open import Relation.Unary using (Pred) |
| 24 |
|
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- Types |
| 27 |
|
| 28 |
open import Agda.Builtin.Nat public |
| 29 |
using (zero; suc) renaming (Nat to ℕ) |
| 30 |
|
| 31 |
--smart constructor |
| 32 |
pattern 2+ n = suc (suc n) |
| 33 |
|
| 34 |
------------------------------------------------------------------------ |
| 35 |
-- Boolean equality relation |
| 36 |
|
| 37 |
open import Agda.Builtin.Nat public |
| 38 |
using () renaming (_==_ to _≡ᵇ_) |
| 39 |
|
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- Boolean ordering relation |
| 42 |
|
| 43 |
open import Agda.Builtin.Nat public |
| 44 |
using () renaming (_<_ to _<ᵇ_) |
| 45 |
|
| 46 |
infix 4 _≤ᵇ_ |
| 47 |
_≤ᵇ_ : (m n : ℕ) → Bool |
| 48 |
zero ≤ᵇ n = true |
| 49 |
suc m ≤ᵇ n = m <ᵇ n |
| 50 |
|
| 51 |
------------------------------------------------------------------------ |
| 52 |
-- Standard ordering relations |
| 53 |
|
| 54 |
infix 4 _≤_ _<_ _≥_ _>_ _≰_ _≮_ _≱_ _≯_ |
| 55 |
|
| 56 |
data _≤_ : Rel ℕ 0ℓ where |
| 57 |
z≤n : ∀ {n} → zero ≤ n |
| 58 |
s≤s : ∀ {m n} (m≤n : m ≤ n) → suc m ≤ suc n |
| 59 |
|
| 60 |
_<_ : Rel ℕ 0ℓ |
| 61 |
m < n = suc m ≤ n |
| 62 |
|
| 63 |
-- Smart constructors of _<_ |
| 64 |
|
| 65 |
pattern z<s {n} = s≤s (z≤n {n}) |
| 66 |
pattern s<s {m} {n} m<n = s≤s {m} {n} m<n |
| 67 |
pattern sz<ss {n} = s<s (z<s {n}) |
| 68 |
|
| 69 |
-- Smart destructors of _≤_, _<_ |
| 70 |
|
| 71 |
s≤s⁻¹ : ∀ {m n} → suc m ≤ suc n → m ≤ n |
| 72 |
s≤s⁻¹ (s≤s m≤n) = m≤n |
| 73 |
|
| 74 |
s<s⁻¹ : ∀ {m n} → suc m < suc n → m < n |
| 75 |
s<s⁻¹ (s<s m<n) = m<n |
| 76 |
|
| 77 |
|
| 78 |
------------------------------------------------------------------------ |
| 79 |
-- Other derived ordering relations |
| 80 |
|
| 81 |
_≥_ : Rel ℕ 0ℓ |
| 82 |
m ≥ n = n ≤ m |
| 83 |
|
| 84 |
_>_ : Rel ℕ 0ℓ |
| 85 |
m > n = n < m |
| 86 |
|
| 87 |
_≰_ : Rel ℕ 0ℓ |
| 88 |
a ≰ b = ¬ a ≤ b |
| 89 |
|
| 90 |
_≮_ : Rel ℕ 0ℓ |
| 91 |
a ≮ b = ¬ a < b |
| 92 |
|
| 93 |
_≱_ : Rel ℕ 0ℓ |
| 94 |
a ≱ b = ¬ a ≥ b |
| 95 |
|
| 96 |
_≯_ : Rel ℕ 0ℓ |
| 97 |
a ≯ b = ¬ a > b |
| 98 |
|
| 99 |
------------------------------------------------------------------------ |
| 100 |
-- Simple predicates |
| 101 |
|
| 102 |
-- Defining these predicates in terms of `T` and therefore ultimately |
| 103 |
-- `⊤` and `⊥` allows Agda to automatically infer them for any natural |
| 104 |
-- of the correct form. Consequently in many circumstances this |
| 105 |
-- eliminates the need to explicitly pass a proof when the predicate |
| 106 |
-- argument is either an implicit or an instance argument. See `_/_` |
| 107 |
-- and `_%_` further down this file for examples. |
| 108 |
-- |
| 109 |
-- Furthermore, defining these predicates as single-field records |
| 110 |
-- (rather defining them directly as the type of their field) is |
| 111 |
-- necessary as the current version of Agda is far better at |
| 112 |
-- reconstructing meta-variable values for the record parameters. |
| 113 |
|
| 114 |
-- A predicate saying that a number is not equal to 0. |
| 115 |
|
| 116 |
record NonZero (n : ℕ) : Set where |
| 117 |
field |
| 118 |
nonZero : T (not (n ≡ᵇ 0)) |
| 119 |
|
| 120 |
-- Instances |
| 121 |
|
| 122 |
instance |
| 123 |
nonZero : ∀ {n} → NonZero (suc n) |
| 124 |
nonZero = _ |
| 125 |
|
| 126 |
-- Constructors |
| 127 |
|
| 128 |
≢-nonZero : ∀ {n} → n ≢ 0 → NonZero n |
| 129 |
≢-nonZero {zero} 0≢0 = contradiction refl 0≢0 |
| 130 |
≢-nonZero {suc n} n≢0 = _ |
| 131 |
|
| 132 |
>-nonZero : ∀ {n} → n > 0 → NonZero n |
| 133 |
>-nonZero z<s = _ |
| 134 |
|
| 135 |
-- Destructors |
| 136 |
|
| 137 |
≢-nonZero⁻¹ : ∀ n → .{{NonZero n}} → n ≢ 0 |
| 138 |
≢-nonZero⁻¹ (suc n) () |
| 139 |
|
| 140 |
>-nonZero⁻¹ : ∀ n → .{{NonZero n}} → n > 0 |
| 141 |
>-nonZero⁻¹ (suc n) = z<s |
| 142 |
|
| 143 |
-- The property of being a non-zero, non-unit |
| 144 |
|
| 145 |
record NonTrivial (n : ℕ) : Set where |
| 146 |
field |
| 147 |
nonTrivial : T (1 <ᵇ n) |
| 148 |
|
| 149 |
-- Instances |
| 150 |
|
| 151 |
instance |
| 152 |
nonTrivial : ∀ {n} → NonTrivial (2+ n) |
| 153 |
nonTrivial = _ |
| 154 |
|
| 155 |
-- Constructors |
| 156 |
|
| 157 |
n>1⇒nonTrivial : ∀ {n} → n > 1 → NonTrivial n |
| 158 |
n>1⇒nonTrivial sz<ss = _ |
| 159 |
|
| 160 |
-- Destructors |
| 161 |
|
| 162 |
nonTrivial⇒nonZero : ∀ n → .{{NonTrivial n}} → NonZero n |
| 163 |
nonTrivial⇒nonZero (2+ _) = _ |
| 164 |
|
| 165 |
nonTrivial⇒n>1 : ∀ n → .{{NonTrivial n}} → n > 1 |
| 166 |
nonTrivial⇒n>1 (2+ _) = sz<ss |
| 167 |
|
| 168 |
nonTrivial⇒≢1 : ∀ {n} → .{{NonTrivial n}} → n ≢ 1 |
| 169 |
nonTrivial⇒≢1 {{()}} refl |
| 170 |
|
| 171 |
------------------------------------------------------------------------ |
| 172 |
-- Raw bundles |
| 173 |
|
| 174 |
open import Agda.Builtin.Nat public |
| 175 |
using (_+_; _*_) renaming (_-_ to _∸_) |
| 176 |
|
| 177 |
+-rawMagma : RawMagma 0ℓ 0ℓ |
| 178 |
+-rawMagma = record |
| 179 |
{ _≈_ = _≡_ |
| 180 |
; _∙_ = _+_ |
| 181 |
} |
| 182 |
|
| 183 |
+-0-rawMonoid : RawMonoid 0ℓ 0ℓ |
| 184 |
+-0-rawMonoid = record |
| 185 |
{ _≈_ = _≡_ |
| 186 |
; _∙_ = _+_ |
| 187 |
; ε = 0 |
| 188 |
} |
| 189 |
|
| 190 |
*-rawMagma : RawMagma 0ℓ 0ℓ |
| 191 |
*-rawMagma = record |
| 192 |
{ _≈_ = _≡_ |
| 193 |
; _∙_ = _*_ |
| 194 |
} |
| 195 |
|
| 196 |
*-1-rawMonoid : RawMonoid 0ℓ 0ℓ |
| 197 |
*-1-rawMonoid = record |
| 198 |
{ _≈_ = _≡_ |
| 199 |
; _∙_ = _*_ |
| 200 |
; ε = 1 |
| 201 |
} |
| 202 |
|
| 203 |
+-*-rawNearSemiring : RawNearSemiring 0ℓ 0ℓ |
| 204 |
+-*-rawNearSemiring = record |
| 205 |
{ Carrier = _ |
| 206 |
; _≈_ = _≡_ |
| 207 |
; _+_ = _+_ |
| 208 |
; _*_ = _*_ |
| 209 |
; 0# = 0 |
| 210 |
} |
| 211 |
|
| 212 |
+-*-rawSemiring : RawSemiring 0ℓ 0ℓ |
| 213 |
+-*-rawSemiring = record |
| 214 |
{ Carrier = _ |
| 215 |
; _≈_ = _≡_ |
| 216 |
; _+_ = _+_ |
| 217 |
; _*_ = _*_ |
| 218 |
; 0# = 0 |
| 219 |
; 1# = 1 |
| 220 |
} |
| 221 |
|
| 222 |
------------------------------------------------------------------------ |
| 223 |
-- Arithmetic |
| 224 |
|
| 225 |
open import Agda.Builtin.Nat |
| 226 |
using (div-helper; mod-helper) |
| 227 |
|
| 228 |
pred : ℕ → ℕ |
| 229 |
pred n = n ∸ 1 |
| 230 |
|
| 231 |
infix 8 _! |
| 232 |
infixl 7 _⊓_ _⊓′_ _/_ _%_ |
| 233 |
infixl 6 _+⋎_ _⊔_ _⊔′_ |
| 234 |
|
| 235 |
-- Argument-swapping addition. Used by Data.Vec._⋎_. |
| 236 |
|
| 237 |
_+⋎_ : ℕ → ℕ → ℕ |
| 238 |
zero +⋎ n = n |
| 239 |
suc m +⋎ n = suc (n +⋎ m) |
| 240 |
|
| 241 |
-- Max. |
| 242 |
|
| 243 |
_⊔_ : ℕ → ℕ → ℕ |
| 244 |
zero ⊔ n = n |
| 245 |
suc m ⊔ zero = suc m |
| 246 |
suc m ⊔ suc n = suc (m ⊔ n) |
| 247 |
|
| 248 |
-- Max defined in terms of primitive operations. |
| 249 |
-- This is much faster than `_⊔_` but harder to reason about. For proofs |
| 250 |
-- involving this function, convert it to `_⊔_` with `Data.Nat.Properties.⊔≡⊔‵`. |
| 251 |
_⊔′_ : ℕ → ℕ → ℕ |
| 252 |
m ⊔′ n with m <ᵇ n |
| 253 |
... | false = m |
| 254 |
... | true = n |
| 255 |
|
| 256 |
-- Min. |
| 257 |
|
| 258 |
_⊓_ : ℕ → ℕ → ℕ |
| 259 |
zero ⊓ n = zero |
| 260 |
suc m ⊓ zero = zero |
| 261 |
suc m ⊓ suc n = suc (m ⊓ n) |
| 262 |
|
| 263 |
-- Min defined in terms of primitive operations. |
| 264 |
-- This is much faster than `_⊓_` but harder to reason about. For proofs |
| 265 |
-- involving this function, convert it to `_⊓_` wtih `Data.Nat.properties.⊓≡⊓′`. |
| 266 |
_⊓′_ : ℕ → ℕ → ℕ |
| 267 |
m ⊓′ n with m <ᵇ n |
| 268 |
... | false = n |
| 269 |
... | true = m |
| 270 |
|
| 271 |
-- Parity |
| 272 |
|
| 273 |
parity : ℕ → Parity |
| 274 |
parity 0 = 0ℙ |
| 275 |
parity 1 = 1ℙ |
| 276 |
parity (suc (suc n)) = parity n |
| 277 |
|
| 278 |
-- Division by 2, rounded downwards. |
| 279 |
|
| 280 |
⌊_/2⌋ : ℕ → ℕ |
| 281 |
⌊ 0 /2⌋ = 0 |
| 282 |
⌊ 1 /2⌋ = 0 |
| 283 |
⌊ suc (suc n) /2⌋ = suc ⌊ n /2⌋ |
| 284 |
|
| 285 |
-- Division by 2, rounded upwards. |
| 286 |
|
| 287 |
⌈_/2⌉ : ℕ → ℕ |
| 288 |
⌈ n /2⌉ = ⌊ suc n /2⌋ |
| 289 |
|
| 290 |
-- Naïve exponentiation |
| 291 |
|
| 292 |
infixr 8 _^_ |
| 293 |
|
| 294 |
_^_ : ℕ → ℕ → ℕ |
| 295 |
x ^ zero = 1 |
| 296 |
x ^ suc n = x * x ^ n |
| 297 |
|
| 298 |
-- Distance |
| 299 |
|
| 300 |
∣_-_∣ : ℕ → ℕ → ℕ |
| 301 |
∣ zero - y ∣ = y |
| 302 |
∣ x - zero ∣ = x |
| 303 |
∣ suc x - suc y ∣ = ∣ x - y ∣ |
| 304 |
|
| 305 |
-- Distance in terms of primitive operations. |
| 306 |
-- This is much faster than `∣_-_∣` but harder to reason about. |
| 307 |
-- For proofs involving this function, convert it to `∣_-_∣` with |
| 308 |
-- `Data.Nat.Properties.∣-∣≡∣-∣′`. |
| 309 |
∣_-_∣′ : ℕ → ℕ → ℕ |
| 310 |
∣ x - y ∣′ with x <ᵇ y |
| 311 |
... | false = x ∸ y |
| 312 |
... | true = y ∸ x |
| 313 |
|
| 314 |
-- Division |
| 315 |
-- Note properties of these are in `Nat.DivMod` not `Nat.Properties` |
| 316 |
|
| 317 |
_/_ : (dividend divisor : ℕ) .{{_ : NonZero divisor}} → ℕ |
| 318 |
m / (suc n) = div-helper 0 n m n |
| 319 |
|
| 320 |
-- Remainder/modulus |
| 321 |
-- Note properties of these are in `Nat.DivMod` not `Nat.Properties` |
| 322 |
|
| 323 |
_%_ : (dividend divisor : ℕ) .{{_ : NonZero divisor}} → ℕ |
| 324 |
m % (suc n) = mod-helper 0 n m n |
| 325 |
|
| 326 |
-- Factorial |
| 327 |
|
| 328 |
_! : ℕ → ℕ |
| 329 |
zero ! = 1 |
| 330 |
suc n ! = suc n * n ! |
| 331 |
|
| 332 |
------------------------------------------------------------------------ |
| 333 |
-- Extensionally equivalent alternative definitions of _≤_/_<_ etc. |
| 334 |
|
| 335 |
-- _≤′_: this definition is more suitable for well-founded induction |
| 336 |
-- (see Data.Nat.Induction) |
| 337 |
|
| 338 |
infix 4 _≤′_ _<′_ _≥′_ _>′_ |
| 339 |
|
| 340 |
data _≤′_ (m : ℕ) : ℕ → Set where |
| 341 |
≤′-reflexive : ∀ {n} → m ≡ n → m ≤′ n |
| 342 |
≤′-step : ∀ {n} → m ≤′ n → m ≤′ suc n |
| 343 |
|
| 344 |
pattern ≤′-refl {m} = ≤′-reflexive {n = m} refl |
| 345 |
|
| 346 |
_<′_ : Rel ℕ 0ℓ |
| 347 |
m <′ n = suc m ≤′ n |
| 348 |
|
| 349 |
-- Smart constructors of _<′_ |
| 350 |
|
| 351 |
pattern <′-base = ≤′-refl |
| 352 |
pattern <′-step {n} m<′n = ≤′-step {n} m<′n |
| 353 |
|
| 354 |
_≥′_ : Rel ℕ 0ℓ |
| 355 |
m ≥′ n = n ≤′ m |
| 356 |
|
| 357 |
_>′_ : Rel ℕ 0ℓ |
| 358 |
m >′ n = n <′ m |
| 359 |
|
| 360 |
-- _≤″_: this definition of _≤_ is used for proof-irrelevant ‵DivMod` |
| 361 |
-- and is a specialised instance of a general algebraic construction |
| 362 |
|
| 363 |
infix 4 _≤″_ _<″_ _≥″_ _>″_ |
| 364 |
|
| 365 |
_≤″_ : (m n : ℕ) → Set |
| 366 |
_≤″_ = _∣ˡ_ +-rawMagma |
| 367 |
|
| 368 |
_<″_ : Rel ℕ 0ℓ |
| 369 |
m <″ n = suc m ≤″ n |
| 370 |
|
| 371 |
_≥″_ : Rel ℕ 0ℓ |
| 372 |
m ≥″ n = n ≤″ m |
| 373 |
|
| 374 |
_>″_ : Rel ℕ 0ℓ |
| 375 |
m >″ n = n <″ m |
| 376 |
|
| 377 |
-- Smart destructor of _<″_ |
| 378 |
|
| 379 |
s<″s⁻¹ : ∀ {m n} → suc m <″ suc n → m <″ n |
| 380 |
s<″s⁻¹ (k , eq) = k , cong pred eq |
| 381 |
|
| 382 |
-- _≤‴_: this definition is useful for induction with an upper bound. |
| 383 |
|
| 384 |
infix 4 _≤‴_ _<‴_ _≥‴_ _>‴_ |
| 385 |
|
| 386 |
data _≤‴_ (m n : ℕ) : Set |
| 387 |
|
| 388 |
_<‴_ : Rel ℕ 0ℓ |
| 389 |
m <‴ n = suc m ≤‴ n |
| 390 |
|
| 391 |
data _≤‴_ m n where |
| 392 |
≤‴-reflexive : m ≡ n → m ≤‴ n |
| 393 |
≤‴-step : m <‴ n → m ≤‴ n |
| 394 |
|
| 395 |
pattern ≤‴-refl = ≤‴-reflexive refl |
| 396 |
|
| 397 |
_≥‴_ : Rel ℕ 0ℓ |
| 398 |
m ≥‴ n = n ≤‴ m |
| 399 |
|
| 400 |
_>‴_ : Rel ℕ 0ℓ |
| 401 |
m >‴ n = n <‴ m |
| 402 |
|
| 403 |
------------------------------------------------------------------------ |
| 404 |
-- A comparison view. Taken from "View from the left" |
| 405 |
-- (McBride/McKinna); details may differ. |
| 406 |
|
| 407 |
data Ordering : Rel ℕ 0ℓ where |
| 408 |
less : ∀ m k → Ordering m (suc (m + k)) |
| 409 |
equal : ∀ m → Ordering m m |
| 410 |
greater : ∀ m k → Ordering (suc (m + k)) m |
| 411 |
|
| 412 |
compare : ∀ m n → Ordering m n |
| 413 |
compare zero zero = equal zero |
| 414 |
compare (suc m) zero = greater zero m |
| 415 |
compare zero (suc n) = less zero n |
| 416 |
compare (suc m) (suc n) with compare m n |
| 417 |
... | less m k = less (suc m) k |
| 418 |
... | equal m = equal (suc m) |
| 419 |
... | greater n k = greater (suc n) k |
| 420 |
|
| 421 |
|
| 422 |
------------------------------------------------------------------------ |
| 423 |
-- DEPRECATED NAMES |
| 424 |
------------------------------------------------------------------------ |
| 425 |
-- Please use the new names as continuing support for the old names is |
| 426 |
-- not guaranteed. |
| 427 |
|
| 428 |
-- Version 2.1 |
| 429 |
|
| 430 |
-- Smart constructors of _≤″_ and _<″_ |
| 431 |
pattern less-than-or-equal {k} eq = k , eq |
| 432 |
{-# WARNING_ON_USAGE less-than-or-equal |
| 433 |
"Warning: less-than-or-equal was deprecated in v2.1. Please match directly on proofs of ≤″ using constructor Algebra.Definitions.RawMagma._∣ˡ_._,_ instead. " |
| 434 |
#-} |
| 435 |
pattern ≤″-offset k = k , refl |
| 436 |
{-# WARNING_ON_USAGE ≤″-offset |
| 437 |
"Warning: ≤″-offset was deprecated in v2.1. Please match directly on proofs of ≤″ using pattern (_, refl) from Algebra.Definitions.RawMagma._∣ˡ_ instead. " |
| 438 |
#-} |
| 439 |
pattern <″-offset k = k , refl |
| 440 |
{-# WARNING_ON_USAGE <″-offset |
| 441 |
"Warning: <″-offset was deprecated in v2.1. Please match directly on proofs of ≤″ using pattern (_, refl) from Algebra.Definitions.RawMagma._∣ˡ_ instead. " |
| 442 |
#-} |
| 443 |
|
| 444 |
-- Smart destructor of _≤″_ |
| 445 |
|
| 446 |
s≤″s⁻¹ : ∀ {m n} → suc m ≤″ suc n → m ≤″ n |
| 447 |
s≤″s⁻¹ (k , eq) = k , cong pred eq |
| 448 |
{-# WARNING_ON_USAGE s≤″s⁻¹ |
| 449 |
"Warning: s≤″s⁻¹ was deprecated in v2.1. Please match directly on proofs of ≤″ using constructor Algebra.Definitions.RawMagma._∣ˡ_._,_ instead. " |
| 450 |
#-} |