Debian Codegraph @

src/Util/Strings/Decimal.v

raw
1 From Coq Require Import Ascii String.
2 From Coq Require Import BinNums.
3 From Coq Require Import DecimalString.
4 From Coq Require DecimalN.
5 From Coq Require DecimalZ.
6 Import BinPosDef.
7 Import BinIntDef.
8 Import BinNatDef.
9
10 Local Open Scope positive_scope.
11 Local Open Scope string_scope.
12
13 Module N.
14 Definition of_string (s : string) : option N
15 := option_map N.of_uint (NilEmpty.uint_of_string s).
16
17 Definition to_string (v : N) : string
18 := NilEmpty.string_of_uint (N.to_uint v).
19
20 Lemma of_to v
21 : of_string (to_string v) = Some v.
22 Proof.
23 cbv [of_string to_string option_map].
24 now rewrite NilEmpty.usu, DecimalN.Unsigned.of_to.
25 Qed.
26
27 (** Going the other ways strips leading zeros; we don't prove anything about it *)
28 End N.
29
30 Module Pos.
31 Definition of_string (s : string) : option positive
32 := match N.of_string s with
33 | Some N0 | None => None
34 | Some (Npos p) => Some p
35 end.
36
37 Definition to_string (v : positive) : string
38 := N.to_string (Npos v).
39
40 Lemma of_to v
41 : of_string (to_string v) = Some v.
42 Proof.
43 cbv [of_string to_string option_map].
44 now rewrite N.of_to.
45 Qed.
46
47 (** Going the other ways strips leading zeros; we don't prove anything about it *)
48 End Pos.
49
50 Module Z.
51 Definition of_string (s : string) : option Z
52 := option_map Z.of_int (NilEmpty.int_of_string s).
53
54 Definition to_string (v : Z) : string
55 := NilEmpty.string_of_int (Z.to_int v).
56
57 Lemma of_to v
58 : of_string (to_string v) = Some v.
59 Proof.
60 cbv [of_string to_string option_map].
61 now rewrite NilEmpty.isi, DecimalZ.of_to.
62 Qed.
63
64 (** Going the other ways strips leading zeros; we don't prove anything about it *)
65 End Z.