| 1 |
From Corelib Require Import BinNums PosDef NatDef. |
| 2 |
|
| 3 |
Local Open Scope Z_scope. |
| 4 |
|
| 5 |
Local Notation "0" := Z0. |
| 6 |
Local Notation "1" := (Zpos 1). |
| 7 |
Local Notation "2" := (Zpos 2). |
| 8 |
|
| 9 |
(***********************************************************) |
| 10 |
(** * Binary Integers, Definitions of Operations *) |
| 11 |
(***********************************************************) |
| 12 |
|
| 13 |
(** Initial author: Pierre Crégut, CNET, Lannion, France *) |
| 14 |
|
| 15 |
Module Z. |
| 16 |
|
| 17 |
(** ** Doubling and variants *) |
| 18 |
|
| 19 |
Definition double x := |
| 20 |
match x with |
| 21 |
| 0 => 0 |
| 22 |
| Zpos p => Zpos p~0 |
| 23 |
| Zneg p => Zneg p~0 |
| 24 |
end. |
| 25 |
|
| 26 |
Definition succ_double x := |
| 27 |
match x with |
| 28 |
| 0 => 1 |
| 29 |
| Zpos p => Zpos p~1 |
| 30 |
| Zneg p => Zneg (Pos.pred_double p) |
| 31 |
end. |
| 32 |
|
| 33 |
Definition pred_double x := |
| 34 |
match x with |
| 35 |
| 0 => Zneg 1 |
| 36 |
| Zneg p => Zneg p~1 |
| 37 |
| Zpos p => Zpos (Pos.pred_double p) |
| 38 |
end. |
| 39 |
|
| 40 |
(** ** Subtraction of positive into Z *) |
| 41 |
|
| 42 |
Fixpoint pos_sub (x y:positive) {struct y} : Z := |
| 43 |
match x, y with |
| 44 |
| p~1, q~1 => double (pos_sub p q) |
| 45 |
| p~1, q~0 => succ_double (pos_sub p q) |
| 46 |
| p~1, xH => Zpos p~0 |
| 47 |
| p~0, q~1 => pred_double (pos_sub p q) |
| 48 |
| p~0, q~0 => double (pos_sub p q) |
| 49 |
| p~0, xH => Zpos (Pos.pred_double p) |
| 50 |
| xH, q~1 => Zneg q~0 |
| 51 |
| xH, q~0 => Zneg (Pos.pred_double q) |
| 52 |
| xH, xH => Z0 |
| 53 |
end%positive. |
| 54 |
|
| 55 |
(** ** Addition *) |
| 56 |
|
| 57 |
Definition add x y := |
| 58 |
match x, y with |
| 59 |
| 0, y => y |
| 60 |
| x, 0 => x |
| 61 |
| Zpos x', Zpos y' => Zpos (Pos.add x' y') |
| 62 |
| Zpos x', Zneg y' => pos_sub x' y' |
| 63 |
| Zneg x', Zpos y' => pos_sub y' x' |
| 64 |
| Zneg x', Zneg y' => Zneg (Pos.add x' y') |
| 65 |
end. |
| 66 |
|
| 67 |
Infix "+" := add : Z_scope. |
| 68 |
|
| 69 |
(** ** Opposite *) |
| 70 |
|
| 71 |
Definition opp x := |
| 72 |
match x with |
| 73 |
| 0 => 0 |
| 74 |
| Zpos x => Zneg x |
| 75 |
| Zneg x => Zpos x |
| 76 |
end. |
| 77 |
|
| 78 |
Notation "- x" := (opp x) : Z_scope. |
| 79 |
|
| 80 |
(** ** Subtraction *) |
| 81 |
|
| 82 |
Definition sub m n := m + -n. |
| 83 |
|
| 84 |
Infix "-" := sub : Z_scope. |
| 85 |
|
| 86 |
(** ** Multiplication *) |
| 87 |
|
| 88 |
Definition mul x y := |
| 89 |
match x, y with |
| 90 |
| 0, _ => 0 |
| 91 |
| _, 0 => 0 |
| 92 |
| Zpos x', Zpos y' => Zpos (Pos.mul x' y') |
| 93 |
| Zpos x', Zneg y' => Zneg (Pos.mul x' y') |
| 94 |
| Zneg x', Zpos y' => Zneg (Pos.mul x' y') |
| 95 |
| Zneg x', Zneg y' => Zpos (Pos.mul x' y') |
| 96 |
end. |
| 97 |
|
| 98 |
Infix "*" := mul : Z_scope. |
| 99 |
|
| 100 |
(** ** Power function *) |
| 101 |
|
| 102 |
Definition pow_pos (z:Z) := Pos.iter (mul z) 1. |
| 103 |
|
| 104 |
Definition pow x y := |
| 105 |
match y with |
| 106 |
| Zpos p => pow_pos x p |
| 107 |
| 0 => 1 |
| 108 |
| Zneg _ => 0 |
| 109 |
end. |
| 110 |
|
| 111 |
Infix "^" := pow : Z_scope. |
| 112 |
|
| 113 |
(** ** Comparison *) |
| 114 |
|
| 115 |
Definition compare x y := |
| 116 |
match x, y with |
| 117 |
| 0, 0 => Eq |
| 118 |
| 0, Zpos y' => Lt |
| 119 |
| 0, Zneg y' => Gt |
| 120 |
| Zpos x', 0 => Gt |
| 121 |
| Zpos x', Zpos y' => Pos.compare x' y' |
| 122 |
| Zpos x', Zneg y' => Gt |
| 123 |
| Zneg x', 0 => Lt |
| 124 |
| Zneg x', Zpos y' => Lt |
| 125 |
| Zneg x', Zneg y' => CompOpp (Pos.compare x' y') |
| 126 |
end. |
| 127 |
|
| 128 |
Infix "?=" := compare (at level 70, no associativity) : Z_scope. |
| 129 |
|
| 130 |
Definition lt x y := (x ?= y) = Lt. |
| 131 |
Definition gt x y := (x ?= y) = Gt. |
| 132 |
Definition le x y := (x ?= y) <> Gt. |
| 133 |
Definition ge x y := (x ?= y) <> Lt. |
| 134 |
|
| 135 |
Infix "<=" := le : Z_scope. |
| 136 |
Infix "<" := lt : Z_scope. |
| 137 |
Infix ">=" := ge : Z_scope. |
| 138 |
Infix ">" := gt : Z_scope. |
| 139 |
|
| 140 |
(** Boolean equality and comparisons *) |
| 141 |
|
| 142 |
Definition leb x y := |
| 143 |
match compare x y with |
| 144 |
| Gt => false |
| 145 |
| _ => true |
| 146 |
end. |
| 147 |
|
| 148 |
Definition ltb x y := |
| 149 |
match compare x y with |
| 150 |
| Lt => true |
| 151 |
| _ => false |
| 152 |
end. |
| 153 |
|
| 154 |
Definition eqb x y := |
| 155 |
match x, y with |
| 156 |
| 0, 0 => true |
| 157 |
| Zpos p, Zpos q => Pos.eqb p q |
| 158 |
| Zneg p, Zneg q => Pos.eqb p q |
| 159 |
| _, _ => false |
| 160 |
end. |
| 161 |
|
| 162 |
(** ** Minimum and maximum *) |
| 163 |
|
| 164 |
Definition max n m := |
| 165 |
match compare n m with |
| 166 |
| Eq | Gt => n |
| 167 |
| Lt => m |
| 168 |
end. |
| 169 |
|
| 170 |
Definition min n m := |
| 171 |
match compare n m with |
| 172 |
| Eq | Lt => n |
| 173 |
| Gt => m |
| 174 |
end. |
| 175 |
|
| 176 |
(** ** Conversions *) |
| 177 |
|
| 178 |
(** From [Z] to [nat] by rounding negative numbers to 0 *) |
| 179 |
|
| 180 |
Definition to_nat (z:Z) : nat := |
| 181 |
match z with |
| 182 |
| Zpos p => Pos.to_nat p |
| 183 |
| _ => O |
| 184 |
end. |
| 185 |
|
| 186 |
(** From [nat] to [Z] *) |
| 187 |
|
| 188 |
Definition of_nat (n:nat) : Z := |
| 189 |
match n with |
| 190 |
| O => 0 |
| 191 |
| S n => Zpos (Pos.of_succ_nat n) |
| 192 |
end. |
| 193 |
|
| 194 |
(** From [N] to [Z] *) |
| 195 |
|
| 196 |
Definition of_N (n:N) : Z := |
| 197 |
match n with |
| 198 |
| N0 => 0 |
| 199 |
| Npos p => Zpos p |
| 200 |
end. |
| 201 |
|
| 202 |
(** From [Z] to [positive] by rounding nonpositive numbers to 1 *) |
| 203 |
|
| 204 |
Definition to_pos (z:Z) : positive := |
| 205 |
match z with |
| 206 |
| Zpos p => p |
| 207 |
| _ => 1%positive |
| 208 |
end. |
| 209 |
|
| 210 |
(** ** Euclidean divisions for binary integers *) |
| 211 |
|
| 212 |
(** ** Floor division *) |
| 213 |
|
| 214 |
(** [div_eucl] provides a Truncated-Toward-Bottom (a.k.a Floor) |
| 215 |
Euclidean division. Its projections are named [div] (noted "/") |
| 216 |
and [modulo] (noted with an infix "mod"). |
| 217 |
These functions correspond to the `div` and `mod` of Haskell. |
| 218 |
This is the historical convention of Rocq. |
| 219 |
|
| 220 |
The main properties of this convention are : |
| 221 |
- we have [sgn (a mod b) = sgn (b)] |
| 222 |
- [div a b] is the greatest integer smaller or equal to the exact |
| 223 |
fraction [a/b]. |
| 224 |
- there is no easy sign rule. |
| 225 |
|
| 226 |
In addition, note that we take [a/0 = 0] and [a mod 0 = a]. |
| 227 |
This choice is motivated by the div-mod equation |
| 228 |
[a = (a / b) * b + (a mod b)] for [b = 0]. |
| 229 |
*) |
| 230 |
|
| 231 |
(** First, a division for positive numbers. Even if the second |
| 232 |
argument is a Z, the answer is arbitrary if it isn't a Zpos. *) |
| 233 |
|
| 234 |
Fixpoint pos_div_eucl (a:positive) (b:Z) : Z * Z := |
| 235 |
match a with |
| 236 |
| xH => if leb 2 b then (0, 1) else (1, 0) |
| 237 |
| xO a' => |
| 238 |
let (q, r) := pos_div_eucl a' b in |
| 239 |
let r' := 2 * r in |
| 240 |
if ltb r' b then (2 * q, r') else (2 * q + 1, r' - b) |
| 241 |
| xI a' => |
| 242 |
let (q, r) := pos_div_eucl a' b in |
| 243 |
let r' := 2 * r + 1 in |
| 244 |
if ltb r' b then (2 * q, r') else (2 * q + 1, r' - b) |
| 245 |
end. |
| 246 |
|
| 247 |
(** Then the general euclidean division *) |
| 248 |
|
| 249 |
Definition div_eucl (a b:Z) : Z * Z := |
| 250 |
match a, b with |
| 251 |
| 0, _ => (0, 0) |
| 252 |
| _, 0 => (0, a) |
| 253 |
| Zpos a', Zpos _ => pos_div_eucl a' b |
| 254 |
| Zneg a', Zpos _ => |
| 255 |
let (q, r) := pos_div_eucl a' b in |
| 256 |
match r with |
| 257 |
| 0 => (- q, 0) |
| 258 |
| _ => (- (q + 1), b - r) |
| 259 |
end |
| 260 |
| Zneg a', Zneg b' => |
| 261 |
let (q, r) := pos_div_eucl a' (Zpos b') in (q, - r) |
| 262 |
| Zpos a', Zneg b' => |
| 263 |
let (q, r) := pos_div_eucl a' (Zpos b') in |
| 264 |
match r with |
| 265 |
| 0 => (- q, 0) |
| 266 |
| _ => (- (q + 1), b + r) |
| 267 |
end |
| 268 |
end. |
| 269 |
|
| 270 |
Definition div (a b:Z) : Z := let (q, _) := div_eucl a b in q. |
| 271 |
Definition modulo (a b:Z) : Z := let (_, r) := div_eucl a b in r. |
| 272 |
|
| 273 |
(** ** Trunc Division *) |
| 274 |
|
| 275 |
(** [quotrem] provides a Truncated-Toward-Zero Euclidean division. |
| 276 |
Its projections are named [quot] (noted "÷") and [rem]. |
| 277 |
These functions correspond to the `quot` and `rem` of Haskell. |
| 278 |
This division convention is used in most programming languages, |
| 279 |
e.g. Ocaml. |
| 280 |
|
| 281 |
With this convention: |
| 282 |
- we have [sgn(a rem b) = sgn(a)] |
| 283 |
- sign rule for division: [quot (-a) b = quot a (-b) = -(quot a b)] |
| 284 |
- and for modulo: [a rem (-b) = a rem b] and [(-a) rem b = -(a rem b)] |
| 285 |
|
| 286 |
Note that we take here [quot a 0 = 0] and [a rem 0 = a]. |
| 287 |
This choice is motivated by the quot-rem equation |
| 288 |
[a = (quot a b) * b + (a rem b)] for [b = 0]. |
| 289 |
*) |
| 290 |
|
| 291 |
Definition quotrem (a b:Z) : Z * Z := |
| 292 |
match a, b with |
| 293 |
| 0, _ => (0, 0) |
| 294 |
| _, 0 => (0, a) |
| 295 |
| Zpos a, Zpos b => |
| 296 |
let (q, r) := N.pos_div_eucl a (Npos b) in (of_N q, of_N r) |
| 297 |
| Zneg a, Zpos b => |
| 298 |
let (q, r) := N.pos_div_eucl a (Npos b) in (-of_N q, - of_N r) |
| 299 |
| Zpos a, Zneg b => |
| 300 |
let (q, r) := N.pos_div_eucl a (Npos b) in (-of_N q, of_N r) |
| 301 |
| Zneg a, Zneg b => |
| 302 |
let (q, r) := N.pos_div_eucl a (Npos b) in (of_N q, - of_N r) |
| 303 |
end. |
| 304 |
|
| 305 |
Definition quot a b := fst (quotrem a b). |
| 306 |
Definition rem a b := snd (quotrem a b). |
| 307 |
|
| 308 |
Infix "÷" := quot (at level 40, left associativity) : Z_scope. |
| 309 |
(** No infix notation for rem, otherwise it becomes a keyword *) |
| 310 |
|
| 311 |
(** ** Parity functions *) |
| 312 |
|
| 313 |
Definition even z := |
| 314 |
match z with |
| 315 |
| 0 => true |
| 316 |
| Zpos (xO _) => true |
| 317 |
| Zneg (xO _) => true |
| 318 |
| _ => false |
| 319 |
end. |
| 320 |
|
| 321 |
(** ** Division by two *) |
| 322 |
|
| 323 |
(** [div2] performs rounding toward bottom, it is hence a particular |
| 324 |
case of [div], and for all relative number [n] we have: |
| 325 |
[n = 2 * div2 n + if odd n then 1 else 0]. *) |
| 326 |
|
| 327 |
Definition div2 z := |
| 328 |
match z with |
| 329 |
| 0 => 0 |
| 330 |
| Zpos 1 => 0 |
| 331 |
| Zpos p => Zpos (Pos.div2 p) |
| 332 |
| Zneg p => Zneg (Pos.div2_up p) |
| 333 |
end. |
| 334 |
|
| 335 |
(** ** Square root *) |
| 336 |
|
| 337 |
Definition sqrtrem n := |
| 338 |
match n with |
| 339 |
| 0 => (0, 0) |
| 340 |
| Zpos p => |
| 341 |
match Pos.sqrtrem p with |
| 342 |
| (s, Pos.IsPos r) => (Zpos s, Zpos r) |
| 343 |
| (s, _) => (Zpos s, 0) |
| 344 |
end |
| 345 |
| Zneg _ => (0,0) |
| 346 |
end. |
| 347 |
|
| 348 |
(** Shifts |
| 349 |
|
| 350 |
Nota: a shift to the right by [-n] will be a shift to the left |
| 351 |
by [n], and vice-versa. |
| 352 |
|
| 353 |
For fulfilling the two's complement convention, shifting to |
| 354 |
the right a negative number should correspond to a division |
| 355 |
by 2 with rounding toward bottom, hence the use of [div2] |
| 356 |
instead of [quot2]. |
| 357 |
*) |
| 358 |
|
| 359 |
Definition shiftl a n := |
| 360 |
match n with |
| 361 |
| 0 => a |
| 362 |
| Zpos p => Pos.iter (mul 2) a p |
| 363 |
| Zneg p => Pos.iter div2 a p |
| 364 |
end. |
| 365 |
|
| 366 |
Definition shiftr a n := shiftl a (-n). |
| 367 |
|
| 368 |
(** Bitwise operations [lor] [land] [ldiff] [lxor] *) |
| 369 |
|
| 370 |
Definition lor a b := |
| 371 |
match a, b with |
| 372 |
| 0, _ => b |
| 373 |
| _, 0 => a |
| 374 |
| Zpos a, Zpos b => Zpos (Pos.lor a b) |
| 375 |
| Zneg a, Zpos b => Zneg (N.succ_pos (N.ldiff (Pos.pred_N a) (Npos b))) |
| 376 |
| Zpos a, Zneg b => Zneg (N.succ_pos (N.ldiff (Pos.pred_N b) (Npos a))) |
| 377 |
| Zneg a, Zneg b => Zneg (N.succ_pos (N.land (Pos.pred_N a) (Pos.pred_N b))) |
| 378 |
end. |
| 379 |
|
| 380 |
Definition land a b := |
| 381 |
match a, b with |
| 382 |
| 0, _ => 0 |
| 383 |
| _, 0 => 0 |
| 384 |
| Zpos a, Zpos b => of_N (Pos.land a b) |
| 385 |
| Zneg a, Zpos b => of_N (N.ldiff (Npos b) (Pos.pred_N a)) |
| 386 |
| Zpos a, Zneg b => of_N (N.ldiff (Npos a) (Pos.pred_N b)) |
| 387 |
| Zneg a, Zneg b => Zneg (N.succ_pos (N.lor (Pos.pred_N a) (Pos.pred_N b))) |
| 388 |
end. |
| 389 |
|
| 390 |
Definition lxor a b := |
| 391 |
match a, b with |
| 392 |
| 0, _ => b |
| 393 |
| _, 0 => a |
| 394 |
| Zpos a, Zpos b => of_N (Pos.lxor a b) |
| 395 |
| Zneg a, Zpos b => Zneg (N.succ_pos (N.lxor (Pos.pred_N a) (Npos b))) |
| 396 |
| Zpos a, Zneg b => Zneg (N.succ_pos (N.lxor (Npos a) (Pos.pred_N b))) |
| 397 |
| Zneg a, Zneg b => of_N (N.lxor (Pos.pred_N a) (Pos.pred_N b)) |
| 398 |
end. |
| 399 |
|
| 400 |
End Z. |