| 1 |
(* -*- coding: utf-8 -*- *) |
| 2 |
(************************************************************************) |
| 3 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 4 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 5 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 6 |
(* \VV/ **************************************************************) |
| 7 |
(* // * This file is distributed under the terms of the *) |
| 8 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 9 |
(* * (see LICENSE file for the text of the license) *) |
| 10 |
(************************************************************************) |
| 11 |
|
| 12 |
(**********************************************************************) |
| 13 |
(** * Binary positive numbers, operations *) |
| 14 |
(**********************************************************************) |
| 15 |
|
| 16 |
(** Initial development by Pierre Crégut, CNET, Lannion, France *) |
| 17 |
|
| 18 |
(** The type [positive] and its constructors [xI] and [xO] and [xH] |
| 19 |
are now defined in [BinNums.v] *) |
| 20 |
|
| 21 |
From Corelib Require Export BinNums. |
| 22 |
|
| 23 |
Local Open Scope positive_scope. |
| 24 |
|
| 25 |
(** Postfix notation for positive numbers, which allows mimicking |
| 26 |
the position of bits in a big-endian representation. |
| 27 |
For instance, we can write [1~1~0] instead of [(xO (xI xH))] |
| 28 |
for the number 6 (which is 110 in binary notation). |
| 29 |
*) |
| 30 |
|
| 31 |
Notation "p ~ 1" := (xI p) (format "p '~' '1'") : positive_scope. |
| 32 |
Notation "p ~ 0" := (xO p) (format "p '~' '0'") : positive_scope. |
| 33 |
|
| 34 |
Notation "1" := xH : positive_scope. |
| 35 |
Notation "2" := 1~0 : positive_scope. |
| 36 |
Notation "3" := 1~1 : positive_scope. |
| 37 |
|
| 38 |
Module Pos. |
| 39 |
|
| 40 |
(** * Operations over positive numbers *) |
| 41 |
|
| 42 |
(** ** Successor *) |
| 43 |
|
| 44 |
Fixpoint succ x := |
| 45 |
match x with |
| 46 |
| p~1 => (succ p)~0 |
| 47 |
| p~0 => p~1 |
| 48 |
| 1 => 1~0 |
| 49 |
end. |
| 50 |
|
| 51 |
(** ** Addition *) |
| 52 |
|
| 53 |
Fixpoint add x y := |
| 54 |
match x, y with |
| 55 |
| p~1, q~1 => (add_carry p q)~0 |
| 56 |
| p~1, q~0 => (add p q)~1 |
| 57 |
| p~1, 1 => (succ p)~0 |
| 58 |
| p~0, q~1 => (add p q)~1 |
| 59 |
| p~0, q~0 => (add p q)~0 |
| 60 |
| p~0, 1 => p~1 |
| 61 |
| 1, q~1 => (succ q)~0 |
| 62 |
| 1, q~0 => q~1 |
| 63 |
| 1, 1 => 1~0 |
| 64 |
end |
| 65 |
|
| 66 |
with add_carry x y := |
| 67 |
match x, y with |
| 68 |
| p~1, q~1 => (add_carry p q)~1 |
| 69 |
| p~1, q~0 => (add_carry p q)~0 |
| 70 |
| p~1, 1 => (succ p)~1 |
| 71 |
| p~0, q~1 => (add_carry p q)~0 |
| 72 |
| p~0, q~0 => (add p q)~1 |
| 73 |
| p~0, 1 => (succ p)~0 |
| 74 |
| 1, q~1 => (succ q)~1 |
| 75 |
| 1, q~0 => (succ q)~0 |
| 76 |
| 1, 1 => 1~1 |
| 77 |
end. |
| 78 |
|
| 79 |
(** ** Operation [x -> 2*x-1] *) |
| 80 |
|
| 81 |
Fixpoint pred_double x := |
| 82 |
match x with |
| 83 |
| p~1 => p~0~1 |
| 84 |
| p~0 => (pred_double p)~1 |
| 85 |
| 1 => 1 |
| 86 |
end. |
| 87 |
|
| 88 |
(** ** The predecessor of a positive number can be seen as a [N] *) |
| 89 |
|
| 90 |
Definition pred_N x := |
| 91 |
match x with |
| 92 |
| p~1 => Npos (p~0) |
| 93 |
| p~0 => Npos (pred_double p) |
| 94 |
| 1 => N0 |
| 95 |
end. |
| 96 |
|
| 97 |
(** ** An auxiliary type for subtraction *) |
| 98 |
|
| 99 |
Inductive mask : Set := |
| 100 |
| IsNul : mask |
| 101 |
| IsPos : positive -> mask |
| 102 |
| IsNeg : mask. |
| 103 |
|
| 104 |
(** ** Operation [x -> 2*x+1] *) |
| 105 |
|
| 106 |
Definition succ_double_mask (x:mask) : mask := |
| 107 |
match x with |
| 108 |
| IsNul => IsPos 1 |
| 109 |
| IsNeg => IsNeg |
| 110 |
| IsPos p => IsPos p~1 |
| 111 |
end. |
| 112 |
|
| 113 |
(** ** Operation [x -> 2*x] *) |
| 114 |
|
| 115 |
Definition double_mask (x:mask) : mask := |
| 116 |
match x with |
| 117 |
| IsNul => IsNul |
| 118 |
| IsNeg => IsNeg |
| 119 |
| IsPos p => IsPos p~0 |
| 120 |
end. |
| 121 |
|
| 122 |
(** ** Operation [x -> 2*x-2] *) |
| 123 |
|
| 124 |
Definition double_pred_mask x : mask := |
| 125 |
match x with |
| 126 |
| p~1 => IsPos p~0~0 |
| 127 |
| p~0 => IsPos (pred_double p)~0 |
| 128 |
| 1 => IsNul |
| 129 |
end. |
| 130 |
|
| 131 |
(** ** Subtraction, result as a mask *) |
| 132 |
|
| 133 |
Fixpoint sub_mask (x y:positive) {struct y} : mask := |
| 134 |
match x, y with |
| 135 |
| p~1, q~1 => double_mask (sub_mask p q) |
| 136 |
| p~1, q~0 => succ_double_mask (sub_mask p q) |
| 137 |
| p~1, 1 => IsPos p~0 |
| 138 |
| p~0, q~1 => succ_double_mask (sub_mask_carry p q) |
| 139 |
| p~0, q~0 => double_mask (sub_mask p q) |
| 140 |
| p~0, 1 => IsPos (pred_double p) |
| 141 |
| 1, 1 => IsNul |
| 142 |
| 1, _ => IsNeg |
| 143 |
end |
| 144 |
|
| 145 |
with sub_mask_carry (x y:positive) {struct y} : mask := |
| 146 |
match x, y with |
| 147 |
| p~1, q~1 => succ_double_mask (sub_mask_carry p q) |
| 148 |
| p~1, q~0 => double_mask (sub_mask p q) |
| 149 |
| p~1, 1 => IsPos (pred_double p) |
| 150 |
| p~0, q~1 => double_mask (sub_mask_carry p q) |
| 151 |
| p~0, q~0 => succ_double_mask (sub_mask_carry p q) |
| 152 |
| p~0, 1 => double_pred_mask p |
| 153 |
| 1, _ => IsNeg |
| 154 |
end. |
| 155 |
|
| 156 |
(** ** Subtraction, result as a positive, returning 1 if [x<=y] *) |
| 157 |
|
| 158 |
Definition sub x y := |
| 159 |
match sub_mask x y with |
| 160 |
| IsPos z => z |
| 161 |
| _ => 1 |
| 162 |
end. |
| 163 |
|
| 164 |
(** ** Multiplication *) |
| 165 |
|
| 166 |
Fixpoint mul x y := |
| 167 |
match x with |
| 168 |
| p~1 => add y (mul p y)~0 |
| 169 |
| p~0 => (mul p y)~0 |
| 170 |
| 1 => y |
| 171 |
end. |
| 172 |
|
| 173 |
(** ** Iteration over a positive number *) |
| 174 |
|
| 175 |
Definition iter {A} (f:A -> A) : A -> positive -> A := |
| 176 |
fix iter_fix x n := match n with |
| 177 |
| xH => f x |
| 178 |
| xO n' => iter_fix (iter_fix x n') n' |
| 179 |
| xI n' => f (iter_fix (iter_fix x n') n') |
| 180 |
end. |
| 181 |
|
| 182 |
(** ** Division by 2 rounded below but for 1 *) |
| 183 |
|
| 184 |
Definition div2 p := |
| 185 |
match p with |
| 186 |
| 1 => 1 |
| 187 |
| p~0 => p |
| 188 |
| p~1 => p |
| 189 |
end. |
| 190 |
|
| 191 |
(** Division by 2 rounded up *) |
| 192 |
|
| 193 |
Definition div2_up p := |
| 194 |
match p with |
| 195 |
| 1 => 1 |
| 196 |
| p~0 => p |
| 197 |
| p~1 => succ p |
| 198 |
end. |
| 199 |
|
| 200 |
(** ** Comparison on binary positive numbers *) |
| 201 |
|
| 202 |
Fixpoint compare_cont (r:comparison) (x y:positive) {struct y} : comparison := |
| 203 |
match x, y with |
| 204 |
| p~1, q~1 => compare_cont r p q |
| 205 |
| p~1, q~0 => compare_cont Gt p q |
| 206 |
| p~1, 1 => Gt |
| 207 |
| p~0, q~1 => compare_cont Lt p q |
| 208 |
| p~0, q~0 => compare_cont r p q |
| 209 |
| p~0, 1 => Gt |
| 210 |
| 1, q~1 => Lt |
| 211 |
| 1, q~0 => Lt |
| 212 |
| 1, 1 => r |
| 213 |
end. |
| 214 |
|
| 215 |
Definition compare := compare_cont Eq. |
| 216 |
|
| 217 |
(** ** Boolean equality and comparisons *) |
| 218 |
|
| 219 |
Fixpoint eqb p q {struct q} := |
| 220 |
match p, q with |
| 221 |
| p~1, q~1 => eqb p q |
| 222 |
| p~0, q~0 => eqb p q |
| 223 |
| 1, 1 => true |
| 224 |
| _, _ => false |
| 225 |
end. |
| 226 |
|
| 227 |
Definition leb x y := |
| 228 |
match compare x y with Gt => false | _ => true end. |
| 229 |
|
| 230 |
(** ** A Square Root function for positive numbers *) |
| 231 |
|
| 232 |
(** We proceed by blocks of two digits : if p is written qbb' |
| 233 |
then sqrt(p) will be sqrt(q)~0 or sqrt(q)~1. |
| 234 |
For deciding easily in which case we are, we store the remainder |
| 235 |
(as a mask, since it can be null). |
| 236 |
Instead of copy-pasting the following code four times, we |
| 237 |
factorize as an auxiliary function, with f and g being either |
| 238 |
xO or xI depending of the initial digits. |
| 239 |
NB: (sub_mask (g (f 1)) 4) is a hack, morally it's g (f 0). |
| 240 |
*) |
| 241 |
|
| 242 |
Definition sqrtrem_step (f g:positive->positive) p := |
| 243 |
match p with |
| 244 |
| (s, IsPos r) => |
| 245 |
let s' := s~0~1 in |
| 246 |
let r' := g (f r) in |
| 247 |
if leb s' r' then (s~1, sub_mask r' s') |
| 248 |
else (s~0, IsPos r') |
| 249 |
| (s,_) => (s~0, sub_mask (g (f 1)) 1~0~0) |
| 250 |
end. |
| 251 |
|
| 252 |
Fixpoint sqrtrem p : positive * mask := |
| 253 |
match p with |
| 254 |
| 1 => (1,IsNul) |
| 255 |
| 1~0 => (1,IsPos 1) |
| 256 |
| 1~1 => (1,IsPos 1~0) |
| 257 |
| p~0~0 => sqrtrem_step xO xO (sqrtrem p) |
| 258 |
| p~0~1 => sqrtrem_step xO xI (sqrtrem p) |
| 259 |
| p~1~0 => sqrtrem_step xI xO (sqrtrem p) |
| 260 |
| p~1~1 => sqrtrem_step xI xI (sqrtrem p) |
| 261 |
end. |
| 262 |
|
| 263 |
Definition sqrt p := fst (sqrtrem p). |
| 264 |
|
| 265 |
Definition Nsucc_double x := |
| 266 |
match x with |
| 267 |
| N0 => Npos 1 |
| 268 |
| Npos p => Npos p~1 |
| 269 |
end. |
| 270 |
|
| 271 |
Definition Ndouble n := |
| 272 |
match n with |
| 273 |
| N0 => N0 |
| 274 |
| Npos p => Npos p~0 |
| 275 |
end. |
| 276 |
|
| 277 |
(** Operation over bits. *) |
| 278 |
|
| 279 |
(** Logical [or] *) |
| 280 |
|
| 281 |
Fixpoint lor (p q : positive) : positive := |
| 282 |
match p, q with |
| 283 |
| 1, q~0 => q~1 |
| 284 |
| 1, _ => q |
| 285 |
| p~0, 1 => p~1 |
| 286 |
| _, 1 => p |
| 287 |
| p~0, q~0 => (lor p q)~0 |
| 288 |
| p~0, q~1 => (lor p q)~1 |
| 289 |
| p~1, q~0 => (lor p q)~1 |
| 290 |
| p~1, q~1 => (lor p q)~1 |
| 291 |
end. |
| 292 |
|
| 293 |
(** Logical [and] *) |
| 294 |
|
| 295 |
Fixpoint land (p q : positive) : N := |
| 296 |
match p, q with |
| 297 |
| 1, q~0 => N0 |
| 298 |
| 1, _ => Npos 1 |
| 299 |
| p~0, 1 => N0 |
| 300 |
| _, 1 => Npos 1 |
| 301 |
| p~0, q~0 => Ndouble (land p q) |
| 302 |
| p~0, q~1 => Ndouble (land p q) |
| 303 |
| p~1, q~0 => Ndouble (land p q) |
| 304 |
| p~1, q~1 => Nsucc_double (land p q) |
| 305 |
end. |
| 306 |
|
| 307 |
(** Logical [diff] *) |
| 308 |
|
| 309 |
Fixpoint ldiff (p q:positive) : N := |
| 310 |
match p, q with |
| 311 |
| 1, q~0 => Npos 1 |
| 312 |
| 1, _ => N0 |
| 313 |
| _~0, 1 => Npos p |
| 314 |
| p~1, 1 => Npos (p~0) |
| 315 |
| p~0, q~0 => Ndouble (ldiff p q) |
| 316 |
| p~0, q~1 => Ndouble (ldiff p q) |
| 317 |
| p~1, q~1 => Ndouble (ldiff p q) |
| 318 |
| p~1, q~0 => Nsucc_double (ldiff p q) |
| 319 |
end. |
| 320 |
|
| 321 |
(** [xor] *) |
| 322 |
|
| 323 |
Fixpoint lxor (p q:positive) : N := |
| 324 |
match p, q with |
| 325 |
| 1, 1 => N0 |
| 326 |
| 1, q~0 => Npos (q~1) |
| 327 |
| 1, q~1 => Npos (q~0) |
| 328 |
| p~0, 1 => Npos (p~1) |
| 329 |
| p~0, q~0 => Ndouble (lxor p q) |
| 330 |
| p~0, q~1 => Nsucc_double (lxor p q) |
| 331 |
| p~1, 1 => Npos (p~0) |
| 332 |
| p~1, q~0 => Nsucc_double (lxor p q) |
| 333 |
| p~1, q~1 => Ndouble (lxor p q) |
| 334 |
end. |
| 335 |
|
| 336 |
(** ** From binary positive numbers to Peano natural numbers *) |
| 337 |
|
| 338 |
Definition iter_op {A}(op:A->A->A) := |
| 339 |
fix iter (p:positive)(a:A) : A := |
| 340 |
match p with |
| 341 |
| 1 => a |
| 342 |
| p~0 => iter p (op a a) |
| 343 |
| p~1 => op a (iter p (op a a)) |
| 344 |
end. |
| 345 |
|
| 346 |
Definition to_nat (x:positive) : nat := iter_op plus x (S O). |
| 347 |
Arguments to_nat x: simpl never. |
| 348 |
|
| 349 |
(** ** From Peano natural numbers to binary positive numbers *) |
| 350 |
|
| 351 |
(* A version that converts [n] into [n+1] *) |
| 352 |
|
| 353 |
Fixpoint of_succ_nat (n:nat) : positive := |
| 354 |
match n with |
| 355 |
| O => 1 |
| 356 |
| S x => succ (of_succ_nat x) |
| 357 |
end. |
| 358 |
|
| 359 |
End Pos. |