| 1 |
(************************************************************************) |
| 2 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 3 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 4 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 |
(* \VV/ **************************************************************) |
| 6 |
(* // * This file is distributed under the terms of the *) |
| 7 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 8 |
(* * (see LICENSE file for the text of the license) *) |
| 9 |
(************************************************************************) |
| 10 |
|
| 11 |
Require Import Notations Logic Datatypes. |
| 12 |
Require Decimal Hexadecimal Number. |
| 13 |
Local Open Scope nat_scope. |
| 14 |
|
| 15 |
(**********************************************************************) |
| 16 |
(** * Peano natural numbers, definitions of operations *) |
| 17 |
(**********************************************************************) |
| 18 |
|
| 19 |
(** This file is meant to be used as a whole module, |
| 20 |
without importing it, leading to qualified definitions |
| 21 |
(e.g. Nat.pred) *) |
| 22 |
|
| 23 |
Definition t := nat. |
| 24 |
|
| 25 |
(** ** Constants *) |
| 26 |
|
| 27 |
Local Notation "0" := O. |
| 28 |
Local Notation "1" := (S O). |
| 29 |
Local Notation "2" := (S (S O)). |
| 30 |
|
| 31 |
Definition zero := 0. |
| 32 |
Definition one := 1. |
| 33 |
Definition two := 2. |
| 34 |
|
| 35 |
(** ** Basic operations *) |
| 36 |
|
| 37 |
Definition succ := S. |
| 38 |
|
| 39 |
Definition pred n := |
| 40 |
match n with |
| 41 |
| 0 => n |
| 42 |
| S u => u |
| 43 |
end. |
| 44 |
|
| 45 |
Register pred as num.nat.pred. |
| 46 |
|
| 47 |
Fixpoint add n m := |
| 48 |
match n with |
| 49 |
| 0 => m |
| 50 |
| S p => S (p + m) |
| 51 |
end |
| 52 |
|
| 53 |
where "n + m" := (add n m) : nat_scope. |
| 54 |
|
| 55 |
Register add as num.nat.add. |
| 56 |
|
| 57 |
Definition double n := n + n. |
| 58 |
|
| 59 |
Fixpoint mul n m := |
| 60 |
match n with |
| 61 |
| 0 => 0 |
| 62 |
| S p => m + p * m |
| 63 |
end |
| 64 |
|
| 65 |
where "n * m" := (mul n m) : nat_scope. |
| 66 |
|
| 67 |
Register mul as num.nat.mul. |
| 68 |
|
| 69 |
(** Truncated subtraction: [n-m] is [0] if [n<=m] *) |
| 70 |
|
| 71 |
Fixpoint sub n m := |
| 72 |
match n, m with |
| 73 |
| S k, S l => k - l |
| 74 |
| _, _ => n |
| 75 |
end |
| 76 |
|
| 77 |
where "n - m" := (sub n m) : nat_scope. |
| 78 |
|
| 79 |
Register sub as num.nat.sub. |
| 80 |
|
| 81 |
(** ** Comparisons *) |
| 82 |
|
| 83 |
Fixpoint eqb n m : bool := |
| 84 |
match n, m with |
| 85 |
| 0, 0 => true |
| 86 |
| 0, S _ => false |
| 87 |
| S _, 0 => false |
| 88 |
| S n', S m' => eqb n' m' |
| 89 |
end. |
| 90 |
|
| 91 |
Fixpoint leb n m : bool := |
| 92 |
match n, m with |
| 93 |
| 0, _ => true |
| 94 |
| _, 0 => false |
| 95 |
| S n', S m' => leb n' m' |
| 96 |
end. |
| 97 |
|
| 98 |
Definition ltb n m := leb (S n) m. |
| 99 |
|
| 100 |
Infix "=?" := eqb (at level 70) : nat_scope. |
| 101 |
Infix "<=?" := leb (at level 70) : nat_scope. |
| 102 |
Infix "<?" := ltb (at level 70) : nat_scope. |
| 103 |
|
| 104 |
Fixpoint compare n m : comparison := |
| 105 |
match n, m with |
| 106 |
| 0, 0 => Eq |
| 107 |
| 0, S _ => Lt |
| 108 |
| S _, 0 => Gt |
| 109 |
| S n', S m' => compare n' m' |
| 110 |
end. |
| 111 |
|
| 112 |
Infix "?=" := compare (at level 70) : nat_scope. |
| 113 |
|
| 114 |
(** ** Minimum, maximum *) |
| 115 |
|
| 116 |
Fixpoint max n m := |
| 117 |
match n, m with |
| 118 |
| 0, _ => m |
| 119 |
| S n', 0 => n |
| 120 |
| S n', S m' => S (max n' m') |
| 121 |
end. |
| 122 |
|
| 123 |
Fixpoint min n m := |
| 124 |
match n, m with |
| 125 |
| 0, _ => 0 |
| 126 |
| S n', 0 => 0 |
| 127 |
| S n', S m' => S (min n' m') |
| 128 |
end. |
| 129 |
|
| 130 |
(** ** Parity tests *) |
| 131 |
|
| 132 |
Fixpoint even n : bool := |
| 133 |
match n with |
| 134 |
| 0 => true |
| 135 |
| 1 => false |
| 136 |
| S (S n') => even n' |
| 137 |
end. |
| 138 |
|
| 139 |
Definition odd n := negb (even n). |
| 140 |
|
| 141 |
(** ** Power *) |
| 142 |
|
| 143 |
Fixpoint pow n m := |
| 144 |
match m with |
| 145 |
| 0 => 1 |
| 146 |
| S m => n * (n^m) |
| 147 |
end |
| 148 |
|
| 149 |
where "n ^ m" := (pow n m) : nat_scope. |
| 150 |
|
| 151 |
(** ** Tail-recursive versions of [add] and [mul] *) |
| 152 |
|
| 153 |
Fixpoint tail_add n m := |
| 154 |
match n with |
| 155 |
| O => m |
| 156 |
| S n => tail_add n (S m) |
| 157 |
end. |
| 158 |
|
| 159 |
(** [tail_addmul r n m] is [r + n * m]. *) |
| 160 |
|
| 161 |
Fixpoint tail_addmul r n m := |
| 162 |
match n with |
| 163 |
| O => r |
| 164 |
| S n => tail_addmul (tail_add m r) n m |
| 165 |
end. |
| 166 |
|
| 167 |
Definition tail_mul n m := tail_addmul 0 n m. |
| 168 |
|
| 169 |
(** ** Conversion with a decimal representation for printing/parsing *) |
| 170 |
|
| 171 |
Local Abbreviation ten := (S (S (S (S (S (S (S (S (S (S O)))))))))). |
| 172 |
|
| 173 |
Fixpoint of_uint_acc (d:Decimal.uint)(acc:nat) := |
| 174 |
match d with |
| 175 |
| Decimal.Nil => acc |
| 176 |
| Decimal.D0 d => of_uint_acc d (tail_mul ten acc) |
| 177 |
| Decimal.D1 d => of_uint_acc d (S (tail_mul ten acc)) |
| 178 |
| Decimal.D2 d => of_uint_acc d (S (S (tail_mul ten acc))) |
| 179 |
| Decimal.D3 d => of_uint_acc d (S (S (S (tail_mul ten acc)))) |
| 180 |
| Decimal.D4 d => of_uint_acc d (S (S (S (S (tail_mul ten acc))))) |
| 181 |
| Decimal.D5 d => of_uint_acc d (S (S (S (S (S (tail_mul ten acc)))))) |
| 182 |
| Decimal.D6 d => of_uint_acc d (S (S (S (S (S (S (tail_mul ten acc))))))) |
| 183 |
| Decimal.D7 d => of_uint_acc d (S (S (S (S (S (S (S (tail_mul ten acc)))))))) |
| 184 |
| Decimal.D8 d => of_uint_acc d (S (S (S (S (S (S (S (S (tail_mul ten acc))))))))) |
| 185 |
| Decimal.D9 d => of_uint_acc d (S (S (S (S (S (S (S (S (S (tail_mul ten acc)))))))))) |
| 186 |
end. |
| 187 |
|
| 188 |
Definition of_uint (d:Decimal.uint) := of_uint_acc d O. |
| 189 |
|
| 190 |
Local Abbreviation sixteen := (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S O)))))))))))))))). |
| 191 |
|
| 192 |
Fixpoint of_hex_uint_acc (d:Hexadecimal.uint)(acc:nat) := |
| 193 |
match d with |
| 194 |
| Hexadecimal.Nil => acc |
| 195 |
| Hexadecimal.D0 d => of_hex_uint_acc d (tail_mul sixteen acc) |
| 196 |
| Hexadecimal.D1 d => of_hex_uint_acc d (S (tail_mul sixteen acc)) |
| 197 |
| Hexadecimal.D2 d => of_hex_uint_acc d (S (S (tail_mul sixteen acc))) |
| 198 |
| Hexadecimal.D3 d => of_hex_uint_acc d (S (S (S (tail_mul sixteen acc)))) |
| 199 |
| Hexadecimal.D4 d => of_hex_uint_acc d (S (S (S (S (tail_mul sixteen acc))))) |
| 200 |
| Hexadecimal.D5 d => of_hex_uint_acc d (S (S (S (S (S (tail_mul sixteen acc)))))) |
| 201 |
| Hexadecimal.D6 d => of_hex_uint_acc d (S (S (S (S (S (S (tail_mul sixteen acc))))))) |
| 202 |
| Hexadecimal.D7 d => of_hex_uint_acc d (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))) |
| 203 |
| Hexadecimal.D8 d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (tail_mul sixteen acc))))))))) |
| 204 |
| Hexadecimal.D9 d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))) |
| 205 |
| Hexadecimal.Da d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc))))))))))) |
| 206 |
| Hexadecimal.Db d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))))) |
| 207 |
| Hexadecimal.Dc d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc))))))))))))) |
| 208 |
| Hexadecimal.Dd d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))))))) |
| 209 |
| Hexadecimal.De d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc))))))))))))))) |
| 210 |
| Hexadecimal.Df d => of_hex_uint_acc d (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (tail_mul sixteen acc)))))))))))))))) |
| 211 |
end. |
| 212 |
|
| 213 |
Definition of_hex_uint (d:Hexadecimal.uint) := of_hex_uint_acc d O. |
| 214 |
|
| 215 |
Definition of_num_uint (d:Number.uint) := |
| 216 |
match d with |
| 217 |
| Number.UIntDecimal d => of_uint d |
| 218 |
| Number.UIntHexadecimal d => of_hex_uint d |
| 219 |
end. |
| 220 |
|
| 221 |
Fixpoint to_little_uint n acc := |
| 222 |
match n with |
| 223 |
| O => acc |
| 224 |
| S n => to_little_uint n (Decimal.Little.succ acc) |
| 225 |
end. |
| 226 |
|
| 227 |
Definition to_uint n := |
| 228 |
Decimal.rev (to_little_uint n Decimal.zero). |
| 229 |
|
| 230 |
Fixpoint to_little_hex_uint n acc := |
| 231 |
match n with |
| 232 |
| O => acc |
| 233 |
| S n => to_little_hex_uint n (Hexadecimal.Little.succ acc) |
| 234 |
end. |
| 235 |
|
| 236 |
Definition to_hex_uint n := |
| 237 |
Hexadecimal.rev (to_little_hex_uint n Hexadecimal.zero). |
| 238 |
|
| 239 |
Definition to_num_uint n := Number.UIntDecimal (to_uint n). |
| 240 |
|
| 241 |
Definition to_num_hex_uint n := Number.UIntHexadecimal (to_hex_uint n). |
| 242 |
|
| 243 |
Definition of_int (d:Decimal.int) : option nat := |
| 244 |
match Decimal.norm d with |
| 245 |
| Decimal.Pos u => Some (of_uint u) |
| 246 |
| _ => None |
| 247 |
end. |
| 248 |
|
| 249 |
Definition of_hex_int (d:Hexadecimal.int) : option nat := |
| 250 |
match Hexadecimal.norm d with |
| 251 |
| Hexadecimal.Pos u => Some (of_hex_uint u) |
| 252 |
| _ => None |
| 253 |
end. |
| 254 |
|
| 255 |
Definition of_num_int (d:Number.int) : option nat := |
| 256 |
match d with |
| 257 |
| Number.IntDecimal d => of_int d |
| 258 |
| Number.IntHexadecimal d => of_hex_int d |
| 259 |
end. |
| 260 |
|
| 261 |
Definition to_int n := Decimal.Pos (to_uint n). |
| 262 |
|
| 263 |
Definition to_hex_int n := Hexadecimal.Pos (to_hex_uint n). |
| 264 |
|
| 265 |
Definition to_num_int n := Number.IntDecimal (to_int n). |
| 266 |
|
| 267 |
(** ** Euclidean division *) |
| 268 |
|
| 269 |
(** This division is linear and tail-recursive. |
| 270 |
In [divmod], [y] is the predecessor of the actual divisor, |
| 271 |
and [u] is [y] minus the real remainder |
| 272 |
*) |
| 273 |
|
| 274 |
Fixpoint divmod x y q u := |
| 275 |
match x with |
| 276 |
| 0 => (q,u) |
| 277 |
| S x' => match u with |
| 278 |
| 0 => divmod x' y (S q) y |
| 279 |
| S u' => divmod x' y q u' |
| 280 |
end |
| 281 |
end. |
| 282 |
|
| 283 |
Definition div x y := |
| 284 |
match y with |
| 285 |
| 0 => y |
| 286 |
| S y' => fst (divmod x y' 0 y') |
| 287 |
end. |
| 288 |
|
| 289 |
Definition modulo x y := |
| 290 |
match y with |
| 291 |
| 0 => x |
| 292 |
| S y' => y' - snd (divmod x y' 0 y') |
| 293 |
end. |
| 294 |
|
| 295 |
Infix "/" := div : nat_scope. |
| 296 |
Infix "mod" := modulo (at level 40, no associativity) : nat_scope. |
| 297 |
|
| 298 |
|
| 299 |
(** ** Greatest common divisor *) |
| 300 |
|
| 301 |
(** We use Euclid algorithm, which is normally not structural, |
| 302 |
but Rocq is now clever enough to accept this (behind modulo |
| 303 |
there is a subtraction, which now preserves being a subterm) |
| 304 |
*) |
| 305 |
|
| 306 |
Fixpoint gcd a b := |
| 307 |
match a with |
| 308 |
| O => b |
| 309 |
| S a' => gcd (b mod (S a')) (S a') |
| 310 |
end. |
| 311 |
|
| 312 |
(** ** Square *) |
| 313 |
|
| 314 |
Definition square n := n * n. |
| 315 |
|
| 316 |
(** ** Square root *) |
| 317 |
|
| 318 |
(** The following square root function is linear (and tail-recursive). |
| 319 |
With Peano representation, we can't do better. For faster algorithm, |
| 320 |
see Psqrt/Zsqrt/Nsqrt... |
| 321 |
|
| 322 |
We search the square root of n = k + p^2 + (q - r) |
| 323 |
with q = 2p and 0<=r<=q. We start with p=q=r=0, hence |
| 324 |
looking for the square root of n = k. Then we progressively |
| 325 |
decrease k and r. When k = S k' and r=0, it means we can use (S p) |
| 326 |
as new sqrt candidate, since (S k')+p^2+2p = k'+(S p)^2. |
| 327 |
When k reaches 0, we have found the biggest p^2 square contained |
| 328 |
in n, hence the square root of n is p. |
| 329 |
*) |
| 330 |
|
| 331 |
Fixpoint sqrt_iter k p q r := |
| 332 |
match k with |
| 333 |
| O => p |
| 334 |
| S k' => match r with |
| 335 |
| O => sqrt_iter k' (S p) (S (S q)) (S (S q)) |
| 336 |
| S r' => sqrt_iter k' p q r' |
| 337 |
end |
| 338 |
end. |
| 339 |
|
| 340 |
Definition sqrt n := sqrt_iter n 0 0 0. |
| 341 |
|
| 342 |
(** ** Log2 *) |
| 343 |
|
| 344 |
(** This base-2 logarithm is linear and tail-recursive. |
| 345 |
|
| 346 |
In [log2_iter], we maintain the logarithm [p] of the counter [q], |
| 347 |
while [r] is the distance between [q] and the next power of 2, |
| 348 |
more precisely [q + S r = 2^(S p)] and [r<2^p]. At each |
| 349 |
recursive call, [q] goes up while [r] goes down. When [r] |
| 350 |
is 0, we know that [q] has almost reached a power of 2, |
| 351 |
and we increase [p] at the next call, while resetting [r] |
| 352 |
to [q]. |
| 353 |
|
| 354 |
Graphically (numbers are [q], stars are [r]) : |
| 355 |
|
| 356 |
<< |
| 357 |
10 |
| 358 |
9 |
| 359 |
8 |
| 360 |
7 * |
| 361 |
6 * |
| 362 |
5 ... |
| 363 |
4 |
| 364 |
3 * |
| 365 |
2 * |
| 366 |
1 * * |
| 367 |
0 * * * |
| 368 |
>> |
| 369 |
|
| 370 |
We stop when [k], the global downward counter reaches 0. |
| 371 |
At that moment, [q] is the number we're considering (since |
| 372 |
[k+q] is invariant), and [p] its logarithm. |
| 373 |
*) |
| 374 |
|
| 375 |
Fixpoint log2_iter k p q r := |
| 376 |
match k with |
| 377 |
| O => p |
| 378 |
| S k' => match r with |
| 379 |
| O => log2_iter k' (S p) (S q) q |
| 380 |
| S r' => log2_iter k' p (S q) r' |
| 381 |
end |
| 382 |
end. |
| 383 |
|
| 384 |
Definition log2 n := log2_iter (pred n) 0 1 0. |
| 385 |
|
| 386 |
(** Iterator on natural numbers *) |
| 387 |
|
| 388 |
Definition iter (n:nat) {A} (f:A->A) (x:A) : A := |
| 389 |
nat_rect (fun _ => A) x (fun _ => f) n. |
| 390 |
|
| 391 |
(** Bitwise operations *) |
| 392 |
|
| 393 |
(** We provide here some bitwise operations for unary numbers. |
| 394 |
Some might be really naive, they are just there for fulfilling |
| 395 |
the same interface as other for natural representations. As |
| 396 |
soon as binary representations such as NArith are available, |
| 397 |
it is clearly better to convert to/from them and use their ops. |
| 398 |
*) |
| 399 |
|
| 400 |
Fixpoint div2 n := |
| 401 |
match n with |
| 402 |
| 0 => 0 |
| 403 |
| S 0 => 0 |
| 404 |
| S (S n') => S (div2 n') |
| 405 |
end. |
| 406 |
|
| 407 |
Fixpoint testbit a n : bool := |
| 408 |
match n with |
| 409 |
| 0 => odd a |
| 410 |
| S n => testbit (div2 a) n |
| 411 |
end. |
| 412 |
|
| 413 |
Definition shiftl a := nat_rect _ a (fun _ => double). |
| 414 |
Definition shiftr a := nat_rect _ a (fun _ => div2). |
| 415 |
|
| 416 |
Fixpoint bitwise (op:bool->bool->bool) n a b := |
| 417 |
match n with |
| 418 |
| 0 => 0 |
| 419 |
| S n' => |
| 420 |
(if op (odd a) (odd b) then 1 else 0) + |
| 421 |
2*(bitwise op n' (div2 a) (div2 b)) |
| 422 |
end. |
| 423 |
|
| 424 |
Definition land a b := bitwise andb a a b. |
| 425 |
Definition lor a b := bitwise orb (max a b) a b. |
| 426 |
Definition ldiff a b := bitwise (fun b b' => andb b (negb b')) a a b. |
| 427 |
Definition lxor a b := bitwise xorb (max a b) a b. |