| 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 |
(** * Hexadecimal numbers *) |
| 12 |
|
| 13 |
(** These numbers coded in base 16 will be used for parsing and printing |
| 14 |
other Rocq numeral datatypes in an human-readable way. |
| 15 |
See the [Number Notation] command. |
| 16 |
We represent numbers in base 16 as lists of hexadecimal digits, |
| 17 |
in big-endian order (most significant digit comes first). *) |
| 18 |
|
| 19 |
Require Import Datatypes Specif Decimal. |
| 20 |
|
| 21 |
(** Unsigned integers are just lists of digits. |
| 22 |
For instance, sixteen is (D1 (D0 Nil)) *) |
| 23 |
|
| 24 |
Inductive uint := |
| 25 |
| Nil |
| 26 |
| D0 (_:uint) |
| 27 |
| D1 (_:uint) |
| 28 |
| D2 (_:uint) |
| 29 |
| D3 (_:uint) |
| 30 |
| D4 (_:uint) |
| 31 |
| D5 (_:uint) |
| 32 |
| D6 (_:uint) |
| 33 |
| D7 (_:uint) |
| 34 |
| D8 (_:uint) |
| 35 |
| D9 (_:uint) |
| 36 |
| Da (_:uint) |
| 37 |
| Db (_:uint) |
| 38 |
| Dc (_:uint) |
| 39 |
| Dd (_:uint) |
| 40 |
| De (_:uint) |
| 41 |
| Df (_:uint). |
| 42 |
|
| 43 |
(** [Nil] is the number terminator. Taken alone, it behaves as zero, |
| 44 |
but rather use [D0 Nil] instead, since this form will be denoted |
| 45 |
as [0], while [Nil] will be printed as [Nil]. *) |
| 46 |
|
| 47 |
Abbreviation zero := (D0 Nil). |
| 48 |
|
| 49 |
(** For signed integers, we use two constructors [Pos] and [Neg]. *) |
| 50 |
|
| 51 |
Variant signed_int := Pos (d:uint) | Neg (d:uint). |
| 52 |
Abbreviation int := signed_int. |
| 53 |
|
| 54 |
(** For decimal numbers, we use two constructors [Hexadecimal] and |
| 55 |
[HexadecimalExp], depending on whether or not they are given with an |
| 56 |
exponent (e.g., 0x1.a2p+01). [i] is the integral part while [f] is |
| 57 |
the fractional part (beware that leading zeroes do matter). *) |
| 58 |
|
| 59 |
Variant hexadecimal := |
| 60 |
| Hexadecimal (i:int) (f:uint) |
| 61 |
| HexadecimalExp (i:int) (f:uint) (e:Decimal.int). |
| 62 |
|
| 63 |
Scheme Equality for uint. |
| 64 |
Scheme Equality for int. |
| 65 |
Scheme Equality for hexadecimal. |
| 66 |
Abbreviation int_eq_dec := signed_int_eq_dec. |
| 67 |
Abbreviation int_beq := signed_int_beq. |
| 68 |
Abbreviation internal_int_dec_lb := internal_signed_int_dec_lb. |
| 69 |
Abbreviation internal_int_dec_bl := internal_signed_int_dec_bl. |
| 70 |
|
| 71 |
Declare Scope hex_uint_scope. |
| 72 |
Delimit Scope hex_uint_scope with huint. |
| 73 |
Bind Scope hex_uint_scope with uint. |
| 74 |
|
| 75 |
Declare Scope hex_int_scope. |
| 76 |
Delimit Scope hex_int_scope with hint. |
| 77 |
Bind Scope hex_int_scope with int. |
| 78 |
|
| 79 |
Register uint as num.hexadecimal_uint.type. |
| 80 |
Register int as num.hexadecimal_int.type. |
| 81 |
Register hexadecimal as num.hexadecimal.type. |
| 82 |
|
| 83 |
Fixpoint nb_digits d := |
| 84 |
match d with |
| 85 |
| Nil => O |
| 86 |
| D0 d | D1 d | D2 d | D3 d | D4 d | D5 d | D6 d | D7 d | D8 d | D9 d |
| 87 |
| Da d | Db d | Dc d | Dd d | De d | Df d => |
| 88 |
S (nb_digits d) |
| 89 |
end. |
| 90 |
|
| 91 |
(** This representation favors simplicity over canonicity. |
| 92 |
For normalizing numbers, we need to remove head zero digits, |
| 93 |
and choose our canonical representation of 0 (here [D0 Nil] |
| 94 |
for unsigned numbers and [Pos (D0 Nil)] for signed numbers). *) |
| 95 |
|
| 96 |
(** [nzhead] removes all head zero digits *) |
| 97 |
|
| 98 |
Fixpoint nzhead d := |
| 99 |
match d with |
| 100 |
| D0 d => nzhead d |
| 101 |
| _ => d |
| 102 |
end. |
| 103 |
|
| 104 |
(** [unorm] : normalization of unsigned integers *) |
| 105 |
|
| 106 |
Definition unorm d := |
| 107 |
match nzhead d with |
| 108 |
| Nil => zero |
| 109 |
| d => d |
| 110 |
end. |
| 111 |
|
| 112 |
(** [norm] : normalization of signed integers *) |
| 113 |
|
| 114 |
Definition norm d := |
| 115 |
match d with |
| 116 |
| Pos d => Pos (unorm d) |
| 117 |
| Neg d => |
| 118 |
match nzhead d with |
| 119 |
| Nil => Pos zero |
| 120 |
| d => Neg d |
| 121 |
end |
| 122 |
end. |
| 123 |
|
| 124 |
(** A few easy operations. For more advanced computations, use the conversions |
| 125 |
with other Rocq numeral datatypes (e.g. Z) and the operations on them. *) |
| 126 |
|
| 127 |
Definition opp (d:int) := |
| 128 |
match d with |
| 129 |
| Pos d => Neg d |
| 130 |
| Neg d => Pos d |
| 131 |
end. |
| 132 |
|
| 133 |
Definition abs (d:int) : uint := |
| 134 |
match d with |
| 135 |
| Pos d => d |
| 136 |
| Neg d => d |
| 137 |
end. |
| 138 |
|
| 139 |
(** For conversions with binary numbers, it is easier to operate |
| 140 |
on little-endian numbers. *) |
| 141 |
|
| 142 |
Fixpoint revapp (d d' : uint) := |
| 143 |
match d with |
| 144 |
| Nil => d' |
| 145 |
| D0 d => revapp d (D0 d') |
| 146 |
| D1 d => revapp d (D1 d') |
| 147 |
| D2 d => revapp d (D2 d') |
| 148 |
| D3 d => revapp d (D3 d') |
| 149 |
| D4 d => revapp d (D4 d') |
| 150 |
| D5 d => revapp d (D5 d') |
| 151 |
| D6 d => revapp d (D6 d') |
| 152 |
| D7 d => revapp d (D7 d') |
| 153 |
| D8 d => revapp d (D8 d') |
| 154 |
| D9 d => revapp d (D9 d') |
| 155 |
| Da d => revapp d (Da d') |
| 156 |
| Db d => revapp d (Db d') |
| 157 |
| Dc d => revapp d (Dc d') |
| 158 |
| Dd d => revapp d (Dd d') |
| 159 |
| De d => revapp d (De d') |
| 160 |
| Df d => revapp d (Df d') |
| 161 |
end. |
| 162 |
|
| 163 |
Definition rev d := revapp d Nil. |
| 164 |
|
| 165 |
Definition app d d' := revapp (rev d) d'. |
| 166 |
|
| 167 |
Definition app_int d1 d2 := |
| 168 |
match d1 with Pos d1 => Pos (app d1 d2) | Neg d1 => Neg (app d1 d2) end. |
| 169 |
|
| 170 |
(** [nztail] removes all trailing zero digits and return both the |
| 171 |
result and the number of removed digits. *) |
| 172 |
|
| 173 |
Definition nztail d := |
| 174 |
let fix aux d_rev := |
| 175 |
match d_rev with |
| 176 |
| D0 d_rev => let (r, n) := aux d_rev in pair r (S n) |
| 177 |
| _ => pair d_rev O |
| 178 |
end in |
| 179 |
let (r, n) := aux (rev d) in pair (rev r) n. |
| 180 |
|
| 181 |
Definition nztail_int d := |
| 182 |
match d with |
| 183 |
| Pos d => let (r, n) := nztail d in pair (Pos r) n |
| 184 |
| Neg d => let (r, n) := nztail d in pair (Neg r) n |
| 185 |
end. |
| 186 |
|
| 187 |
(** [del_head n d] removes [n] digits at beginning of [d] |
| 188 |
or returns [zero] if [d] has less than [n] digits. *) |
| 189 |
|
| 190 |
Fixpoint del_head n d := |
| 191 |
match n with |
| 192 |
| O => d |
| 193 |
| S n => |
| 194 |
match d with |
| 195 |
| Nil => zero |
| 196 |
| D0 d | D1 d | D2 d | D3 d | D4 d | D5 d | D6 d | D7 d | D8 d | D9 d |
| 197 |
| Da d | Db d | Dc d | Dd d | De d | Df d => |
| 198 |
del_head n d |
| 199 |
end |
| 200 |
end. |
| 201 |
|
| 202 |
Definition del_head_int n d := |
| 203 |
match d with |
| 204 |
| Pos d => del_head n d |
| 205 |
| Neg d => del_head n d |
| 206 |
end. |
| 207 |
|
| 208 |
(** [del_tail n d] removes [n] digits at end of [d] |
| 209 |
or returns [zero] if [d] has less than [n] digits. *) |
| 210 |
|
| 211 |
Definition del_tail n d := rev (del_head n (rev d)). |
| 212 |
|
| 213 |
Definition del_tail_int n d := |
| 214 |
match d with |
| 215 |
| Pos d => Pos (del_tail n d) |
| 216 |
| Neg d => Neg (del_tail n d) |
| 217 |
end. |
| 218 |
|
| 219 |
Module Little. |
| 220 |
|
| 221 |
(** Successor of little-endian numbers *) |
| 222 |
|
| 223 |
Fixpoint succ d := |
| 224 |
match d with |
| 225 |
| Nil => D1 Nil |
| 226 |
| D0 d => D1 d |
| 227 |
| D1 d => D2 d |
| 228 |
| D2 d => D3 d |
| 229 |
| D3 d => D4 d |
| 230 |
| D4 d => D5 d |
| 231 |
| D5 d => D6 d |
| 232 |
| D6 d => D7 d |
| 233 |
| D7 d => D8 d |
| 234 |
| D8 d => D9 d |
| 235 |
| D9 d => Da d |
| 236 |
| Da d => Db d |
| 237 |
| Db d => Dc d |
| 238 |
| Dc d => Dd d |
| 239 |
| Dd d => De d |
| 240 |
| De d => Df d |
| 241 |
| Df d => D0 (succ d) |
| 242 |
end. |
| 243 |
|
| 244 |
(** Doubling little-endian numbers *) |
| 245 |
|
| 246 |
Fixpoint double d := |
| 247 |
match d with |
| 248 |
| Nil => Nil |
| 249 |
| D0 d => D0 (double d) |
| 250 |
| D1 d => D2 (double d) |
| 251 |
| D2 d => D4 (double d) |
| 252 |
| D3 d => D6 (double d) |
| 253 |
| D4 d => D8 (double d) |
| 254 |
| D5 d => Da (double d) |
| 255 |
| D6 d => Dc (double d) |
| 256 |
| D7 d => De (double d) |
| 257 |
| D8 d => D0 (succ_double d) |
| 258 |
| D9 d => D2 (succ_double d) |
| 259 |
| Da d => D4 (succ_double d) |
| 260 |
| Db d => D6 (succ_double d) |
| 261 |
| Dc d => D8 (succ_double d) |
| 262 |
| Dd d => Da (succ_double d) |
| 263 |
| De d => Dc (succ_double d) |
| 264 |
| Df d => De (succ_double d) |
| 265 |
end |
| 266 |
|
| 267 |
with succ_double d := |
| 268 |
match d with |
| 269 |
| Nil => D1 Nil |
| 270 |
| D0 d => D1 (double d) |
| 271 |
| D1 d => D3 (double d) |
| 272 |
| D2 d => D5 (double d) |
| 273 |
| D3 d => D7 (double d) |
| 274 |
| D4 d => D9 (double d) |
| 275 |
| D5 d => Db (double d) |
| 276 |
| D6 d => Dd (double d) |
| 277 |
| D7 d => Df (double d) |
| 278 |
| D8 d => D1 (succ_double d) |
| 279 |
| D9 d => D3 (succ_double d) |
| 280 |
| Da d => D5 (succ_double d) |
| 281 |
| Db d => D7 (succ_double d) |
| 282 |
| Dc d => D9 (succ_double d) |
| 283 |
| Dd d => Db (succ_double d) |
| 284 |
| De d => Dd (succ_double d) |
| 285 |
| Df d => Df (succ_double d) |
| 286 |
end. |
| 287 |
|
| 288 |
End Little. |