author | huffman |
Mon, 16 Feb 2009 19:35:52 -0800 | |
changeset 29947 | 0a51765d2084 |
parent 29946 | cfec0c2982b2 |
child 29954 | 8a95050c7044 |
permissions | -rw-r--r-- |
28021 | 1 |
(* Title: HOL/ex/Numeral.thy |
2 |
Author: Florian Haftmann |
|
29947 | 3 |
*) |
28021 | 4 |
|
29947 | 5 |
header {* An experimental alternative numeral representation. *} |
28021 | 6 |
|
7 |
theory Numeral |
|
8 |
imports Int Inductive |
|
9 |
begin |
|
10 |
||
11 |
subsection {* The @{text num} type *} |
|
12 |
||
29943 | 13 |
datatype num = One | Dig0 num | Dig1 num |
14 |
||
15 |
text {* Increment function for type @{typ num} *} |
|
16 |
||
17 |
primrec |
|
18 |
inc :: "num \<Rightarrow> num" |
|
19 |
where |
|
20 |
"inc One = Dig0 One" |
|
21 |
| "inc (Dig0 x) = Dig1 x" |
|
22 |
| "inc (Dig1 x) = Dig0 (inc x)" |
|
23 |
||
24 |
text {* Converting between type @{typ num} and type @{typ nat} *} |
|
25 |
||
26 |
primrec |
|
27 |
nat_of_num :: "num \<Rightarrow> nat" |
|
28 |
where |
|
29 |
"nat_of_num One = Suc 0" |
|
30 |
| "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x" |
|
31 |
| "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)" |
|
32 |
||
33 |
primrec |
|
34 |
num_of_nat :: "nat \<Rightarrow> num" |
|
35 |
where |
|
36 |
"num_of_nat 0 = One" |
|
37 |
| "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)" |
|
38 |
||
29945 | 39 |
lemma nat_of_num_pos: "0 < nat_of_num x" |
29943 | 40 |
by (induct x) simp_all |
41 |
||
42 |
lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0" |
|
43 |
by (induct x) simp_all |
|
44 |
||
45 |
lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)" |
|
46 |
by (induct x) simp_all |
|
47 |
||
48 |
lemma num_of_nat_double: |
|
49 |
"0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)" |
|
50 |
by (induct n) simp_all |
|
51 |
||
28021 | 52 |
text {* |
29943 | 53 |
Type @{typ num} is isomorphic to the strictly positive |
28021 | 54 |
natural numbers. |
55 |
*} |
|
56 |
||
29943 | 57 |
lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x" |
29945 | 58 |
by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos) |
28021 | 59 |
|
29943 | 60 |
lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n" |
61 |
by (induct n) (simp_all add: nat_of_num_inc) |
|
28021 | 62 |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
63 |
lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
64 |
apply safe |
29943 | 65 |
apply (drule arg_cong [where f=num_of_nat]) |
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
66 |
apply (simp add: nat_of_num_inverse) |
28021 | 67 |
done |
68 |
||
29943 | 69 |
lemma num_induct [case_names One inc]: |
70 |
fixes P :: "num \<Rightarrow> bool" |
|
71 |
assumes One: "P One" |
|
72 |
and inc: "\<And>x. P x \<Longrightarrow> P (inc x)" |
|
73 |
shows "P x" |
|
74 |
proof - |
|
75 |
obtain n where n: "Suc n = nat_of_num x" |
|
76 |
by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0) |
|
77 |
have "P (num_of_nat (Suc n))" |
|
78 |
proof (induct n) |
|
79 |
case 0 show ?case using One by simp |
|
28021 | 80 |
next |
29943 | 81 |
case (Suc n) |
82 |
then have "P (inc (num_of_nat (Suc n)))" by (rule inc) |
|
83 |
then show "P (num_of_nat (Suc (Suc n)))" by simp |
|
28021 | 84 |
qed |
29943 | 85 |
with n show "P x" |
86 |
by (simp add: nat_of_num_inverse) |
|
28021 | 87 |
qed |
88 |
||
89 |
text {* |
|
90 |
From now on, there are two possible models for @{typ num}: |
|
29943 | 91 |
as positive naturals (rule @{text "num_induct"}) |
28021 | 92 |
and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}). |
93 |
||
94 |
It is not entirely clear in which context it is better to use |
|
95 |
the one or the other, or whether the construction should be reversed. |
|
96 |
*} |
|
97 |
||
98 |
||
29945 | 99 |
subsection {* Numeral operations *} |
28021 | 100 |
|
101 |
ML {* |
|
102 |
structure DigSimps = |
|
103 |
NamedThmsFun(val name = "numeral"; val description = "Simplification rules for numerals") |
|
104 |
*} |
|
105 |
||
106 |
setup DigSimps.setup |
|
107 |
||
29945 | 108 |
instantiation num :: "{plus,times,ord}" |
109 |
begin |
|
28021 | 110 |
|
111 |
definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where |
|
28562 | 112 |
[code del]: "m + n = num_of_nat (nat_of_num m + nat_of_num n)" |
28021 | 113 |
|
114 |
definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where |
|
28562 | 115 |
[code del]: "m * n = num_of_nat (nat_of_num m * nat_of_num n)" |
28021 | 116 |
|
29945 | 117 |
definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where |
118 |
[code del]: "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n" |
|
28021 | 119 |
|
29945 | 120 |
definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where |
121 |
[code del]: "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n" |
|
28021 | 122 |
|
29945 | 123 |
instance .. |
28021 | 124 |
|
125 |
end |
|
126 |
||
29945 | 127 |
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y" |
128 |
unfolding plus_num_def |
|
129 |
by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos) |
|
130 |
||
131 |
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y" |
|
132 |
unfolding times_num_def |
|
133 |
by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos) |
|
28021 | 134 |
|
29945 | 135 |
lemma Dig_plus [numeral, simp, code]: |
136 |
"One + One = Dig0 One" |
|
137 |
"One + Dig0 m = Dig1 m" |
|
138 |
"One + Dig1 m = Dig0 (m + One)" |
|
139 |
"Dig0 n + One = Dig1 n" |
|
140 |
"Dig0 n + Dig0 m = Dig0 (n + m)" |
|
141 |
"Dig0 n + Dig1 m = Dig1 (n + m)" |
|
142 |
"Dig1 n + One = Dig0 (n + One)" |
|
143 |
"Dig1 n + Dig0 m = Dig1 (n + m)" |
|
144 |
"Dig1 n + Dig1 m = Dig0 (n + m + One)" |
|
145 |
by (simp_all add: num_eq_iff nat_of_num_add) |
|
28021 | 146 |
|
29945 | 147 |
lemma Dig_times [numeral, simp, code]: |
148 |
"One * One = One" |
|
149 |
"One * Dig0 n = Dig0 n" |
|
150 |
"One * Dig1 n = Dig1 n" |
|
151 |
"Dig0 n * One = Dig0 n" |
|
152 |
"Dig0 n * Dig0 m = Dig0 (n * Dig0 m)" |
|
153 |
"Dig0 n * Dig1 m = Dig0 (n * Dig1 m)" |
|
154 |
"Dig1 n * One = Dig1 n" |
|
155 |
"Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)" |
|
156 |
"Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)" |
|
157 |
by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult |
|
158 |
left_distrib right_distrib) |
|
28021 | 159 |
|
29945 | 160 |
lemma less_eq_num_code [numeral, simp, code]: |
161 |
"One \<le> n \<longleftrightarrow> True" |
|
162 |
"Dig0 m \<le> One \<longleftrightarrow> False" |
|
163 |
"Dig1 m \<le> One \<longleftrightarrow> False" |
|
164 |
"Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n" |
|
165 |
"Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n" |
|
166 |
"Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n" |
|
167 |
"Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n" |
|
168 |
using nat_of_num_pos [of n] nat_of_num_pos [of m] |
|
169 |
by (auto simp add: less_eq_num_def less_num_def) |
|
170 |
||
171 |
lemma less_num_code [numeral, simp, code]: |
|
172 |
"m < One \<longleftrightarrow> False" |
|
173 |
"One < One \<longleftrightarrow> False" |
|
174 |
"One < Dig0 n \<longleftrightarrow> True" |
|
175 |
"One < Dig1 n \<longleftrightarrow> True" |
|
176 |
"Dig0 m < Dig0 n \<longleftrightarrow> m < n" |
|
177 |
"Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n" |
|
178 |
"Dig1 m < Dig1 n \<longleftrightarrow> m < n" |
|
179 |
"Dig1 m < Dig0 n \<longleftrightarrow> m < n" |
|
180 |
using nat_of_num_pos [of n] nat_of_num_pos [of m] |
|
181 |
by (auto simp add: less_eq_num_def less_num_def) |
|
182 |
||
183 |
text {* Rules using @{text One} and @{text inc} as constructors *} |
|
28021 | 184 |
|
29945 | 185 |
lemma add_One: "x + One = inc x" |
186 |
by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc) |
|
187 |
||
188 |
lemma add_inc: "x + inc y = inc (x + y)" |
|
189 |
by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc) |
|
190 |
||
191 |
lemma mult_One: "x * One = x" |
|
192 |
by (simp add: num_eq_iff nat_of_num_mult) |
|
193 |
||
194 |
lemma mult_inc: "x * inc y = x * y + x" |
|
195 |
by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc) |
|
196 |
||
197 |
text {* A double-and-decrement function *} |
|
28021 | 198 |
|
29945 | 199 |
primrec DigM :: "num \<Rightarrow> num" where |
200 |
"DigM One = One" |
|
201 |
| "DigM (Dig0 n) = Dig1 (DigM n)" |
|
202 |
| "DigM (Dig1 n) = Dig1 (Dig0 n)" |
|
28021 | 203 |
|
29945 | 204 |
lemma DigM_plus_one: "DigM n + One = Dig0 n" |
205 |
by (induct n) simp_all |
|
206 |
||
207 |
lemma add_One_commute: "One + n = n + One" |
|
208 |
by (induct n) simp_all |
|
209 |
||
210 |
lemma one_plus_DigM: "One + DigM n = Dig0 n" |
|
211 |
unfolding add_One_commute DigM_plus_one .. |
|
28021 | 212 |
|
29947 | 213 |
text {* A squaring function *} |
214 |
||
215 |
primrec square :: "num \<Rightarrow> num" where |
|
216 |
"square One = One" |
|
217 |
| "square (Dig0 n) = Dig0 (Dig0 (square n))" |
|
218 |
| "square (Dig1 n) = Dig1 (Dig0 (square n + n))" |
|
219 |
||
220 |
lemma nat_of_num_square: |
|
221 |
"nat_of_num (square n) = nat_of_num n * nat_of_num n" |
|
222 |
by (induct n) (simp_all add: nat_of_num_add algebra_simps) |
|
223 |
||
28021 | 224 |
|
225 |
subsection {* Binary numerals *} |
|
226 |
||
227 |
text {* |
|
228 |
We embed binary representations into a generic algebraic |
|
29934 | 229 |
structure using @{text of_num}. |
28021 | 230 |
*} |
231 |
||
232 |
class semiring_numeral = semiring + monoid_mult |
|
233 |
begin |
|
234 |
||
235 |
primrec of_num :: "num \<Rightarrow> 'a" where |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
236 |
of_num_one [numeral]: "of_num One = 1" |
28021 | 237 |
| "of_num (Dig0 n) = of_num n + of_num n" |
238 |
| "of_num (Dig1 n) = of_num n + of_num n + 1" |
|
239 |
||
29943 | 240 |
lemma of_num_inc: "of_num (inc x) = of_num x + 1" |
241 |
by (induct x) (simp_all add: add_ac) |
|
242 |
||
28021 | 243 |
declare of_num.simps [simp del] |
244 |
||
245 |
end |
|
246 |
||
247 |
text {* |
|
248 |
ML stuff and syntax. |
|
249 |
*} |
|
250 |
||
251 |
ML {* |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
252 |
fun mk_num 1 = @{term One} |
28021 | 253 |
| mk_num k = |
254 |
let |
|
255 |
val (l, b) = Integer.div_mod k 2; |
|
256 |
val bit = (if b = 0 then @{term Dig0} else @{term Dig1}); |
|
257 |
in bit $ (mk_num l) end; |
|
258 |
||
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
259 |
fun dest_num @{term One} = 1 |
28021 | 260 |
| dest_num (@{term Dig0} $ n) = 2 * dest_num n |
261 |
| dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1; |
|
262 |
||
263 |
(*FIXME these have to gain proper context via morphisms phi*) |
|
264 |
||
265 |
fun mk_numeral T k = Const (@{const_name of_num}, @{typ num} --> T) |
|
266 |
$ mk_num k |
|
267 |
||
268 |
fun dest_numeral (Const (@{const_name of_num}, Type ("fun", [@{typ num}, T])) $ t) = |
|
269 |
(T, dest_num t) |
|
270 |
*} |
|
271 |
||
272 |
syntax |
|
273 |
"_Numerals" :: "xnum \<Rightarrow> 'a" ("_") |
|
274 |
||
275 |
parse_translation {* |
|
276 |
let |
|
277 |
fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2) |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
278 |
of (0, 1) => Const (@{const_name One}, dummyT) |
28021 | 279 |
| (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n |
280 |
| (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n |
|
281 |
else raise Match; |
|
282 |
fun numeral_tr [Free (num, _)] = |
|
283 |
let |
|
284 |
val {leading_zeros, value, ...} = Syntax.read_xnum num; |
|
285 |
val _ = leading_zeros = 0 andalso value > 0 |
|
286 |
orelse error ("Bad numeral: " ^ num); |
|
287 |
in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end |
|
288 |
| numeral_tr ts = raise TERM ("numeral_tr", ts); |
|
289 |
in [("_Numerals", numeral_tr)] end |
|
290 |
*} |
|
291 |
||
292 |
typed_print_translation {* |
|
293 |
let |
|
294 |
fun dig b n = b + 2 * n; |
|
295 |
fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) = |
|
296 |
dig 0 (int_of_num' n) |
|
297 |
| int_of_num' (Const (@{const_syntax Dig1}, _) $ n) = |
|
298 |
dig 1 (int_of_num' n) |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
299 |
| int_of_num' (Const (@{const_syntax One}, _)) = 1; |
28021 | 300 |
fun num_tr' show_sorts T [n] = |
301 |
let |
|
302 |
val k = int_of_num' n; |
|
303 |
val t' = Syntax.const "_Numerals" $ Syntax.free ("#" ^ string_of_int k); |
|
304 |
in case T |
|
305 |
of Type ("fun", [_, T']) => |
|
306 |
if not (! show_types) andalso can Term.dest_Type T' then t' |
|
307 |
else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T' |
|
308 |
| T' => if T' = dummyT then t' else raise Match |
|
309 |
end; |
|
310 |
in [(@{const_syntax of_num}, num_tr')] end |
|
311 |
*} |
|
312 |
||
29945 | 313 |
subsection {* Class-specific numeral rules *} |
28021 | 314 |
|
315 |
text {* |
|
316 |
@{const of_num} is a morphism. |
|
317 |
*} |
|
318 |
||
29945 | 319 |
subsubsection {* Class @{text semiring_numeral} *} |
320 |
||
28021 | 321 |
context semiring_numeral |
322 |
begin |
|
323 |
||
29943 | 324 |
abbreviation "Num1 \<equiv> of_num One" |
28021 | 325 |
|
326 |
text {* |
|
327 |
Alas, there is still the duplication of @{term 1}, |
|
328 |
thought the duplicated @{term 0} has disappeared. |
|
329 |
We could get rid of it by replacing the constructor |
|
330 |
@{term 1} in @{typ num} by two constructors |
|
331 |
@{text two} and @{text three}, resulting in a further |
|
332 |
blow-up. But it could be worth the effort. |
|
333 |
*} |
|
334 |
||
335 |
lemma of_num_plus_one [numeral]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
336 |
"of_num n + 1 = of_num (n + One)" |
29943 | 337 |
by (rule sym, induct n) (simp_all add: of_num.simps add_ac) |
28021 | 338 |
|
339 |
lemma of_num_one_plus [numeral]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
340 |
"1 + of_num n = of_num (n + One)" |
28021 | 341 |
unfolding of_num_plus_one [symmetric] add_commute .. |
342 |
||
343 |
lemma of_num_plus [numeral]: |
|
344 |
"of_num m + of_num n = of_num (m + n)" |
|
345 |
by (induct n rule: num_induct) |
|
29943 | 346 |
(simp_all add: add_One add_inc of_num_one of_num_inc add_ac) |
28021 | 347 |
|
348 |
lemma of_num_times_one [numeral]: |
|
349 |
"of_num n * 1 = of_num n" |
|
350 |
by simp |
|
351 |
||
352 |
lemma of_num_one_times [numeral]: |
|
353 |
"1 * of_num n = of_num n" |
|
354 |
by simp |
|
355 |
||
356 |
lemma of_num_times [numeral]: |
|
357 |
"of_num m * of_num n = of_num (m * n)" |
|
358 |
by (induct n rule: num_induct) |
|
29943 | 359 |
(simp_all add: of_num_plus [symmetric] mult_One mult_inc |
360 |
semiring_class.right_distrib right_distrib of_num_one of_num_inc) |
|
28021 | 361 |
|
362 |
end |
|
363 |
||
29945 | 364 |
subsubsection {* |
29947 | 365 |
Structures with a zero: class @{text semiring_1} |
28021 | 366 |
*} |
367 |
||
368 |
context semiring_1 |
|
369 |
begin |
|
370 |
||
371 |
subclass semiring_numeral .. |
|
372 |
||
373 |
lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n" |
|
374 |
by (induct n) |
|
375 |
(simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac) |
|
376 |
||
377 |
declare of_nat_1 [numeral] |
|
378 |
||
379 |
lemma Dig_plus_zero [numeral]: |
|
380 |
"0 + 1 = 1" |
|
381 |
"0 + of_num n = of_num n" |
|
382 |
"1 + 0 = 1" |
|
383 |
"of_num n + 0 = of_num n" |
|
384 |
by simp_all |
|
385 |
||
386 |
lemma Dig_times_zero [numeral]: |
|
387 |
"0 * 1 = 0" |
|
388 |
"0 * of_num n = 0" |
|
389 |
"1 * 0 = 0" |
|
390 |
"of_num n * 0 = 0" |
|
391 |
by simp_all |
|
392 |
||
393 |
end |
|
394 |
||
395 |
lemma nat_of_num_of_num: "nat_of_num = of_num" |
|
396 |
proof |
|
397 |
fix n |
|
29943 | 398 |
have "of_num n = nat_of_num n" |
399 |
by (induct n) (simp_all add: of_num.simps) |
|
28021 | 400 |
then show "nat_of_num n = of_num n" by simp |
401 |
qed |
|
402 |
||
29945 | 403 |
subsubsection {* |
404 |
Equality: class @{text semiring_char_0} |
|
28021 | 405 |
*} |
406 |
||
407 |
context semiring_char_0 |
|
408 |
begin |
|
409 |
||
410 |
lemma of_num_eq_iff [numeral]: |
|
411 |
"of_num m = of_num n \<longleftrightarrow> m = n" |
|
412 |
unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric] |
|
29943 | 413 |
of_nat_eq_iff num_eq_iff .. |
28021 | 414 |
|
415 |
lemma of_num_eq_one_iff [numeral]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
416 |
"of_num n = 1 \<longleftrightarrow> n = One" |
28021 | 417 |
proof - |
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
418 |
have "of_num n = of_num One \<longleftrightarrow> n = One" unfolding of_num_eq_iff .. |
28021 | 419 |
then show ?thesis by (simp add: of_num_one) |
420 |
qed |
|
421 |
||
422 |
lemma one_eq_of_num_iff [numeral]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
423 |
"1 = of_num n \<longleftrightarrow> n = One" |
28021 | 424 |
unfolding of_num_eq_one_iff [symmetric] by auto |
425 |
||
426 |
end |
|
427 |
||
29945 | 428 |
subsubsection {* |
429 |
Comparisons: class @{text ordered_semidom} |
|
28021 | 430 |
*} |
431 |
||
29945 | 432 |
text {* Could be perhaps more general than here. *} |
433 |
||
28021 | 434 |
lemma (in ordered_semidom) of_num_pos: "0 < of_num n" |
435 |
proof - |
|
436 |
have "(0::nat) < of_num n" |
|
437 |
by (induct n) (simp_all add: semiring_numeral_class.of_num.simps) |
|
438 |
then have "of_nat 0 \<noteq> of_nat (of_num n)" |
|
439 |
by (cases n) (simp_all only: semiring_numeral_class.of_num.simps of_nat_eq_iff) |
|
440 |
then have "0 \<noteq> of_num n" |
|
441 |
by (simp add: of_nat_of_num) |
|
442 |
moreover have "0 \<le> of_nat (of_num n)" by simp |
|
443 |
ultimately show ?thesis by (simp add: of_nat_of_num) |
|
444 |
qed |
|
445 |
||
446 |
context ordered_semidom |
|
447 |
begin |
|
448 |
||
449 |
lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n" |
|
450 |
proof - |
|
451 |
have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n" |
|
452 |
unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff .. |
|
453 |
then show ?thesis by (simp add: of_nat_of_num) |
|
454 |
qed |
|
455 |
||
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
456 |
lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n = One" |
28021 | 457 |
proof - |
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
458 |
have "of_num n \<le> of_num One \<longleftrightarrow> n = One" |
28021 | 459 |
by (cases n) (simp_all add: of_num_less_eq_iff) |
460 |
then show ?thesis by (simp add: of_num_one) |
|
461 |
qed |
|
462 |
||
463 |
lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n" |
|
464 |
proof - |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
465 |
have "of_num One \<le> of_num n" |
28021 | 466 |
by (cases n) (simp_all add: of_num_less_eq_iff) |
467 |
then show ?thesis by (simp add: of_num_one) |
|
468 |
qed |
|
469 |
||
470 |
lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n" |
|
471 |
proof - |
|
472 |
have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n" |
|
473 |
unfolding less_num_def nat_of_num_of_num of_nat_less_iff .. |
|
474 |
then show ?thesis by (simp add: of_nat_of_num) |
|
475 |
qed |
|
476 |
||
477 |
lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1" |
|
478 |
proof - |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
479 |
have "\<not> of_num n < of_num One" |
28021 | 480 |
by (cases n) (simp_all add: of_num_less_iff) |
481 |
then show ?thesis by (simp add: of_num_one) |
|
482 |
qed |
|
483 |
||
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
484 |
lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> n \<noteq> One" |
28021 | 485 |
proof - |
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
486 |
have "of_num One < of_num n \<longleftrightarrow> n \<noteq> One" |
28021 | 487 |
by (cases n) (simp_all add: of_num_less_iff) |
488 |
then show ?thesis by (simp add: of_num_one) |
|
489 |
qed |
|
490 |
||
491 |
end |
|
492 |
||
29945 | 493 |
subsubsection {* |
29947 | 494 |
Structures with subtraction: class @{text semiring_1_minus} |
28021 | 495 |
*} |
496 |
||
497 |
class semiring_minus = semiring + minus + zero + |
|
498 |
assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a" |
|
499 |
assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a" |
|
500 |
begin |
|
501 |
||
502 |
lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b" |
|
503 |
by (simp add: add_ac minus_inverts_plus1 [of b a]) |
|
504 |
||
505 |
lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b" |
|
506 |
by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a]) |
|
507 |
||
508 |
end |
|
509 |
||
510 |
class semiring_1_minus = semiring_1 + semiring_minus |
|
511 |
begin |
|
512 |
||
513 |
lemma Dig_of_num_pos: |
|
514 |
assumes "k + n = m" |
|
515 |
shows "of_num m - of_num n = of_num k" |
|
516 |
using assms by (simp add: of_num_plus minus_inverts_plus1) |
|
517 |
||
518 |
lemma Dig_of_num_zero: |
|
519 |
shows "of_num n - of_num n = 0" |
|
520 |
by (rule minus_inverts_plus1) simp |
|
521 |
||
522 |
lemma Dig_of_num_neg: |
|
523 |
assumes "k + m = n" |
|
524 |
shows "of_num m - of_num n = 0 - of_num k" |
|
525 |
by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms) |
|
526 |
||
527 |
lemmas Dig_plus_eval = |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
528 |
of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject |
28021 | 529 |
|
530 |
simproc_setup numeral_minus ("of_num m - of_num n") = {* |
|
531 |
let |
|
532 |
(*TODO proper implicit use of morphism via pattern antiquotations*) |
|
533 |
fun cdest_of_num ct = (snd o split_last o snd o Drule.strip_comb) ct; |
|
534 |
fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n); |
|
535 |
fun attach_num ct = (dest_num (Thm.term_of ct), ct); |
|
536 |
fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t; |
|
537 |
val simplify = MetaSimplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval}); |
|
538 |
fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq} OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"}, |
|
539 |
[Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]]; |
|
540 |
in fn phi => fn _ => fn ct => case try cdifference ct |
|
541 |
of NONE => (NONE) |
|
542 |
| SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0 |
|
543 |
then MetaSimplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct |
|
544 |
else mk_meta_eq (let |
|
545 |
val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j)); |
|
546 |
in |
|
547 |
(if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck] |
|
548 |
else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl]) |
|
549 |
end) end) |
|
550 |
end |
|
551 |
*} |
|
552 |
||
553 |
lemma Dig_of_num_minus_zero [numeral]: |
|
554 |
"of_num n - 0 = of_num n" |
|
555 |
by (simp add: minus_inverts_plus1) |
|
556 |
||
557 |
lemma Dig_one_minus_zero [numeral]: |
|
558 |
"1 - 0 = 1" |
|
559 |
by (simp add: minus_inverts_plus1) |
|
560 |
||
561 |
lemma Dig_one_minus_one [numeral]: |
|
562 |
"1 - 1 = 0" |
|
563 |
by (simp add: minus_inverts_plus1) |
|
564 |
||
565 |
lemma Dig_of_num_minus_one [numeral]: |
|
29941 | 566 |
"of_num (Dig0 n) - 1 = of_num (DigM n)" |
28021 | 567 |
"of_num (Dig1 n) - 1 = of_num (Dig0 n)" |
29941 | 568 |
by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one) |
28021 | 569 |
|
570 |
lemma Dig_one_minus_of_num [numeral]: |
|
29941 | 571 |
"1 - of_num (Dig0 n) = 0 - of_num (DigM n)" |
28021 | 572 |
"1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)" |
29941 | 573 |
by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one) |
28021 | 574 |
|
575 |
end |
|
576 |
||
29945 | 577 |
subsubsection {* |
29947 | 578 |
Structures with negation: class @{text ring_1} |
29945 | 579 |
*} |
580 |
||
28021 | 581 |
context ring_1 |
582 |
begin |
|
583 |
||
584 |
subclass semiring_1_minus |
|
29667 | 585 |
proof qed (simp_all add: algebra_simps) |
28021 | 586 |
|
587 |
lemma Dig_zero_minus_of_num [numeral]: |
|
588 |
"0 - of_num n = - of_num n" |
|
589 |
by simp |
|
590 |
||
591 |
lemma Dig_zero_minus_one [numeral]: |
|
592 |
"0 - 1 = - 1" |
|
593 |
by simp |
|
594 |
||
595 |
lemma Dig_uminus_uminus [numeral]: |
|
596 |
"- (- of_num n) = of_num n" |
|
597 |
by simp |
|
598 |
||
599 |
lemma Dig_plus_uminus [numeral]: |
|
600 |
"of_num m + - of_num n = of_num m - of_num n" |
|
601 |
"- of_num m + of_num n = of_num n - of_num m" |
|
602 |
"- of_num m + - of_num n = - (of_num m + of_num n)" |
|
603 |
"of_num m - - of_num n = of_num m + of_num n" |
|
604 |
"- of_num m - of_num n = - (of_num m + of_num n)" |
|
605 |
"- of_num m - - of_num n = of_num n - of_num m" |
|
606 |
by (simp_all add: diff_minus add_commute) |
|
607 |
||
608 |
lemma Dig_times_uminus [numeral]: |
|
609 |
"- of_num n * of_num m = - (of_num n * of_num m)" |
|
610 |
"of_num n * - of_num m = - (of_num n * of_num m)" |
|
611 |
"- of_num n * - of_num m = of_num n * of_num m" |
|
612 |
by (simp_all add: minus_mult_left [symmetric] minus_mult_right [symmetric]) |
|
613 |
||
614 |
lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n" |
|
615 |
by (induct n) |
|
616 |
(simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all) |
|
617 |
||
618 |
declare of_int_1 [numeral] |
|
619 |
||
620 |
end |
|
621 |
||
29945 | 622 |
subsubsection {* |
28021 | 623 |
Greetings to @{typ nat}. |
624 |
*} |
|
625 |
||
626 |
instance nat :: semiring_1_minus proof qed simp_all |
|
627 |
||
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
628 |
lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)" |
28021 | 629 |
unfolding of_num_plus_one [symmetric] by simp |
630 |
||
631 |
lemma nat_number: |
|
632 |
"1 = Suc 0" |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
633 |
"of_num One = Suc 0" |
29941 | 634 |
"of_num (Dig0 n) = Suc (of_num (DigM n))" |
28021 | 635 |
"of_num (Dig1 n) = Suc (of_num (Dig0 n))" |
29941 | 636 |
by (simp_all add: of_num.simps DigM_plus_one Suc_of_num) |
28021 | 637 |
|
638 |
declare diff_0_eq_0 [numeral] |
|
639 |
||
640 |
||
641 |
subsection {* Code generator setup for @{typ int} *} |
|
642 |
||
643 |
definition Pls :: "num \<Rightarrow> int" where |
|
644 |
[simp, code post]: "Pls n = of_num n" |
|
645 |
||
646 |
definition Mns :: "num \<Rightarrow> int" where |
|
647 |
[simp, code post]: "Mns n = - of_num n" |
|
648 |
||
649 |
code_datatype "0::int" Pls Mns |
|
650 |
||
651 |
lemmas [code inline] = Pls_def [symmetric] Mns_def [symmetric] |
|
652 |
||
653 |
definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where |
|
28562 | 654 |
[simp, code del]: "sub m n = (of_num m - of_num n)" |
28021 | 655 |
|
656 |
definition dup :: "int \<Rightarrow> int" where |
|
28562 | 657 |
[code del]: "dup k = 2 * k" |
28021 | 658 |
|
659 |
lemma Dig_sub [code]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
660 |
"sub One One = 0" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
661 |
"sub (Dig0 m) One = of_num (DigM m)" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
662 |
"sub (Dig1 m) One = of_num (Dig0 m)" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
663 |
"sub One (Dig0 n) = - of_num (DigM n)" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
664 |
"sub One (Dig1 n) = - of_num (Dig0 n)" |
28021 | 665 |
"sub (Dig0 m) (Dig0 n) = dup (sub m n)" |
666 |
"sub (Dig1 m) (Dig1 n) = dup (sub m n)" |
|
667 |
"sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1" |
|
668 |
"sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1" |
|
29667 | 669 |
apply (simp_all add: dup_def algebra_simps) |
29941 | 670 |
apply (simp_all add: of_num_plus one_plus_DigM)[4] |
28021 | 671 |
apply (simp_all add: of_num.simps) |
672 |
done |
|
673 |
||
674 |
lemma dup_code [code]: |
|
675 |
"dup 0 = 0" |
|
676 |
"dup (Pls n) = Pls (Dig0 n)" |
|
677 |
"dup (Mns n) = Mns (Dig0 n)" |
|
678 |
by (simp_all add: dup_def of_num.simps) |
|
679 |
||
28562 | 680 |
lemma [code, code del]: |
28021 | 681 |
"(1 :: int) = 1" |
682 |
"(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +" |
|
683 |
"(uminus :: int \<Rightarrow> int) = uminus" |
|
684 |
"(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -" |
|
685 |
"(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *" |
|
28367 | 686 |
"(eq_class.eq :: int \<Rightarrow> int \<Rightarrow> bool) = eq_class.eq" |
28021 | 687 |
"(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>" |
688 |
"(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <" |
|
689 |
by rule+ |
|
690 |
||
691 |
lemma one_int_code [code]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
692 |
"1 = Pls One" |
28021 | 693 |
by (simp add: of_num_one) |
694 |
||
695 |
lemma plus_int_code [code]: |
|
696 |
"k + 0 = (k::int)" |
|
697 |
"0 + l = (l::int)" |
|
698 |
"Pls m + Pls n = Pls (m + n)" |
|
699 |
"Pls m - Pls n = sub m n" |
|
700 |
"Mns m + Mns n = Mns (m + n)" |
|
701 |
"Mns m - Mns n = sub n m" |
|
702 |
by (simp_all add: of_num_plus [symmetric]) |
|
703 |
||
704 |
lemma uminus_int_code [code]: |
|
705 |
"uminus 0 = (0::int)" |
|
706 |
"uminus (Pls m) = Mns m" |
|
707 |
"uminus (Mns m) = Pls m" |
|
708 |
by simp_all |
|
709 |
||
710 |
lemma minus_int_code [code]: |
|
711 |
"k - 0 = (k::int)" |
|
712 |
"0 - l = uminus (l::int)" |
|
713 |
"Pls m - Pls n = sub m n" |
|
714 |
"Pls m - Mns n = Pls (m + n)" |
|
715 |
"Mns m - Pls n = Mns (m + n)" |
|
716 |
"Mns m - Mns n = sub n m" |
|
717 |
by (simp_all add: of_num_plus [symmetric]) |
|
718 |
||
719 |
lemma times_int_code [code]: |
|
720 |
"k * 0 = (0::int)" |
|
721 |
"0 * l = (0::int)" |
|
722 |
"Pls m * Pls n = Pls (m * n)" |
|
723 |
"Pls m * Mns n = Mns (m * n)" |
|
724 |
"Mns m * Pls n = Mns (m * n)" |
|
725 |
"Mns m * Mns n = Pls (m * n)" |
|
726 |
by (simp_all add: of_num_times [symmetric]) |
|
727 |
||
728 |
lemma eq_int_code [code]: |
|
28367 | 729 |
"eq_class.eq 0 (0::int) \<longleftrightarrow> True" |
730 |
"eq_class.eq 0 (Pls l) \<longleftrightarrow> False" |
|
731 |
"eq_class.eq 0 (Mns l) \<longleftrightarrow> False" |
|
732 |
"eq_class.eq (Pls k) 0 \<longleftrightarrow> False" |
|
733 |
"eq_class.eq (Pls k) (Pls l) \<longleftrightarrow> eq_class.eq k l" |
|
734 |
"eq_class.eq (Pls k) (Mns l) \<longleftrightarrow> False" |
|
735 |
"eq_class.eq (Mns k) 0 \<longleftrightarrow> False" |
|
736 |
"eq_class.eq (Mns k) (Pls l) \<longleftrightarrow> False" |
|
737 |
"eq_class.eq (Mns k) (Mns l) \<longleftrightarrow> eq_class.eq k l" |
|
28021 | 738 |
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] |
28367 | 739 |
by (simp_all add: of_num_eq_iff eq) |
28021 | 740 |
|
741 |
lemma less_eq_int_code [code]: |
|
742 |
"0 \<le> (0::int) \<longleftrightarrow> True" |
|
743 |
"0 \<le> Pls l \<longleftrightarrow> True" |
|
744 |
"0 \<le> Mns l \<longleftrightarrow> False" |
|
745 |
"Pls k \<le> 0 \<longleftrightarrow> False" |
|
746 |
"Pls k \<le> Pls l \<longleftrightarrow> k \<le> l" |
|
747 |
"Pls k \<le> Mns l \<longleftrightarrow> False" |
|
748 |
"Mns k \<le> 0 \<longleftrightarrow> True" |
|
749 |
"Mns k \<le> Pls l \<longleftrightarrow> True" |
|
750 |
"Mns k \<le> Mns l \<longleftrightarrow> l \<le> k" |
|
751 |
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] |
|
752 |
by (simp_all add: of_num_less_eq_iff) |
|
753 |
||
754 |
lemma less_int_code [code]: |
|
755 |
"0 < (0::int) \<longleftrightarrow> False" |
|
756 |
"0 < Pls l \<longleftrightarrow> True" |
|
757 |
"0 < Mns l \<longleftrightarrow> False" |
|
758 |
"Pls k < 0 \<longleftrightarrow> False" |
|
759 |
"Pls k < Pls l \<longleftrightarrow> k < l" |
|
760 |
"Pls k < Mns l \<longleftrightarrow> False" |
|
761 |
"Mns k < 0 \<longleftrightarrow> True" |
|
762 |
"Mns k < Pls l \<longleftrightarrow> True" |
|
763 |
"Mns k < Mns l \<longleftrightarrow> l < k" |
|
764 |
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] |
|
765 |
by (simp_all add: of_num_less_iff) |
|
766 |
||
767 |
lemma [code inline del]: "(0::int) \<equiv> Numeral0" by simp |
|
768 |
lemma [code inline del]: "(1::int) \<equiv> Numeral1" by simp |
|
769 |
declare zero_is_num_zero [code inline del] |
|
770 |
declare one_is_num_one [code inline del] |
|
771 |
||
772 |
hide (open) const sub dup |
|
773 |
||
774 |
||
775 |
subsection {* Numeral equations as default simplification rules *} |
|
776 |
||
29934 | 777 |
text {* TODO. Be more precise here with respect to subsumed facts. Or use named theorems anyway. *} |
28021 | 778 |
declare (in semiring_numeral) numeral [simp] |
779 |
declare (in semiring_1) numeral [simp] |
|
780 |
declare (in semiring_char_0) numeral [simp] |
|
781 |
declare (in ring_1) numeral [simp] |
|
782 |
thm numeral |
|
783 |
||
784 |
||
785 |
text {* Toy examples *} |
|
786 |
||
787 |
definition "bar \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat) \<and> #4 * #2 + #7 \<ge> (#8 :: int) - #3" |
|
788 |
code_thms bar |
|
789 |
export_code bar in Haskell file - |
|
790 |
export_code bar in OCaml module_name Foo file - |
|
791 |
ML {* @{code bar} *} |
|
792 |
||
793 |
end |