-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeci.v
More file actions
203 lines (158 loc) · 3.66 KB
/
Copy pathDeci.v
File metadata and controls
203 lines (158 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Require Import Lib BinNatDef BinIntDef.
Open Scope list_scope.
(** We represent here number in base 10 by lists of decimal digits,
in big endian order (most significant digit comes first). *)
Inductive digit := D0 | D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | D9.
Definition dec := list digit. (** big endian *)
Definition ten := D1 :: D0 :: nil. (** For example... *)
(** This representation favors simplicity over canonicity :
we might need later to normalize by removing the leading zeros *)
Fixpoint norm l :=
match l with
| D0 :: l => norm l
| _ => l
end.
(** Conversion between decimal and Peano nat representations *)
Module DecNat.
Definition digit2nat d :=
match d with
| D0 => 0
| D1 => 1
| D2 => 2
| D3 => 3
| D4 => 4
| D5 => 5
| D6 => 6
| D7 => 7
| D8 => 8
| D9 => 9
end.
Definition nat2digit n :=
match n with
| 0 => D0
| 1 => D1
| 2 => D2
| 3 => D3
| 4 => D4
| 5 => D5
| 6 => D6
| 7 => D7
| 8 => D8
| _ => D9 (* n>9 shouldn't happen *)
end.
Fixpoint d2n (d:dec)(acc:nat) :=
match d with
| nil => acc
| d :: l => d2n l (TailNat.addmul (digit2nat d) 10 acc)
end.
Definition dec2nat d := d2n d 0.
Fixpoint n2d (n:nat)(acc:dec)(count:nat) :=
match count, n with
| 0, _ => acc
| _, 0 => acc
| S count', _ =>
let (q,r) := diveucl n 10 in
n2d q (nat2digit r :: acc) count'
end.
Definition nat2dec n := n2d n nil n.
End DecNat.
(** Same for decimal and binary N numbers *)
Module DecN.
Local Open Scope N.
Definition digit2n d : N :=
match d with
| D0 => 0
| D1 => 1
| D2 => 2
| D3 => 3
| D4 => 4
| D5 => 5
| D6 => 6
| D7 => 7
| D8 => 8
| D9 => 9
end.
Definition n2digit (n:N) :=
match n with
| 0 => D0
| 1 => D1
| 2 => D2
| 3 => D3
| 4 => D4
| 5 => D5
| 6 => D6
| 7 => D7
| 8 => D8
| _ => D9 (* n>9 shouldn't happen *)
end.
Fixpoint d2n (d:dec)(acc:N) :=
match d with
| nil => acc
| d :: l => d2n l (N.add (digit2n d) (N.mul 10 acc))
end.
Definition dec2n d := d2n d 0.
Fixpoint n2d (n:N)(acc:dec)(count:positive) :=
match count, n with
| xH, _ => acc
| _, 0 => acc
| xO count', _ | xI count', _ =>
let (q,r) := N.div_eucl n 10 in
n2d q (n2digit r :: acc) count'
end.
Definition n2dec n :=
n2d n nil (match n with 0 => xH | Npos p => xO p end).
End DecN.
(** For positive and Z numbers, we simply go through N for the moment. *)
Module DecPos.
Definition dec2pos d :=
match DecN.dec2n d with
| N0 => None
| Npos p => Some p
end.
Definition pos2dec p := DecN.n2dec (Npos p).
End DecPos.
Module DecZ.
Definition dec2z d :=
match DecN.dec2n d with
| N0 => Z0
| Npos p => Zpos p
end.
Definition z2dec z :=
match z with
| Zpos p => DecN.n2dec (Npos p)
| _ => nil (* TODO : for now, we discard negative numbers *)
end.
End DecZ.
(** A successor on decimal. Not really mandatory, just to state
that our conversions preserve the order of numbers *)
Fixpoint bounded_succ l :=
match l with
| nil => Carry nil
| d::l =>
match bounded_succ l with
| NoCarry l' => NoCarry (d::l')
| Carry l' =>
match d with
| D0 => NoCarry (D1::l')
| D1 => NoCarry (D2::l')
| D2 => NoCarry (D3::l')
| D3 => NoCarry (D4::l')
| D4 => NoCarry (D5::l')
| D5 => NoCarry (D6::l')
| D6 => NoCarry (D7::l')
| D7 => NoCarry (D8::l')
| D8 => NoCarry (D9::l')
| D9 => Carry (D0::l')
end
end
end.
Definition succ l :=
match bounded_succ l with
| NoCarry l' => l'
| Carry l' => D1::l'
end.
(** The strict order on decimal numbers is the transitive
closure of the successor *)
Inductive lt : dec -> dec -> Prop :=
| Succ x : lt x (succ x)
| Trans x y z : lt x y -> lt y z -> lt x z.