Debian Codegraph @

theories/Corelib/Init/Decimal.v

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