author | huffman |
Wed, 29 Apr 2009 20:33:52 -0700 | |
changeset 31026 | 020efcbfe2d8 |
parent 31025 | 6d2188134536 |
child 31027 | b5a35bfb3dab |
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 |
||
31021 | 17 |
primrec inc :: "num \<Rightarrow> num" where |
29943 | 18 |
"inc One = Dig0 One" |
19 |
| "inc (Dig0 x) = Dig1 x" |
|
20 |
| "inc (Dig1 x) = Dig0 (inc x)" |
|
21 |
||
22 |
text {* Converting between type @{typ num} and type @{typ nat} *} |
|
23 |
||
31021 | 24 |
primrec nat_of_num :: "num \<Rightarrow> nat" where |
29943 | 25 |
"nat_of_num One = Suc 0" |
26 |
| "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x" |
|
27 |
| "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)" |
|
28 |
||
31021 | 29 |
primrec num_of_nat :: "nat \<Rightarrow> num" where |
29943 | 30 |
"num_of_nat 0 = One" |
31 |
| "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)" |
|
32 |
||
29945 | 33 |
lemma nat_of_num_pos: "0 < nat_of_num x" |
29943 | 34 |
by (induct x) simp_all |
35 |
||
36 |
lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0" |
|
37 |
by (induct x) simp_all |
|
38 |
||
39 |
lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)" |
|
40 |
by (induct x) simp_all |
|
41 |
||
42 |
lemma num_of_nat_double: |
|
43 |
"0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)" |
|
44 |
by (induct n) simp_all |
|
45 |
||
28021 | 46 |
text {* |
29943 | 47 |
Type @{typ num} is isomorphic to the strictly positive |
28021 | 48 |
natural numbers. |
49 |
*} |
|
50 |
||
29943 | 51 |
lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x" |
29945 | 52 |
by (induct x) (simp_all add: num_of_nat_double nat_of_num_pos) |
28021 | 53 |
|
29943 | 54 |
lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n" |
55 |
by (induct n) (simp_all add: nat_of_num_inc) |
|
28021 | 56 |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
57 |
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
|
58 |
apply safe |
29943 | 59 |
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
|
60 |
apply (simp add: nat_of_num_inverse) |
28021 | 61 |
done |
62 |
||
29943 | 63 |
lemma num_induct [case_names One inc]: |
64 |
fixes P :: "num \<Rightarrow> bool" |
|
65 |
assumes One: "P One" |
|
66 |
and inc: "\<And>x. P x \<Longrightarrow> P (inc x)" |
|
67 |
shows "P x" |
|
68 |
proof - |
|
69 |
obtain n where n: "Suc n = nat_of_num x" |
|
70 |
by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0) |
|
71 |
have "P (num_of_nat (Suc n))" |
|
72 |
proof (induct n) |
|
73 |
case 0 show ?case using One by simp |
|
28021 | 74 |
next |
29943 | 75 |
case (Suc n) |
76 |
then have "P (inc (num_of_nat (Suc n)))" by (rule inc) |
|
77 |
then show "P (num_of_nat (Suc (Suc n)))" by simp |
|
28021 | 78 |
qed |
29943 | 79 |
with n show "P x" |
80 |
by (simp add: nat_of_num_inverse) |
|
28021 | 81 |
qed |
82 |
||
83 |
text {* |
|
84 |
From now on, there are two possible models for @{typ num}: |
|
29943 | 85 |
as positive naturals (rule @{text "num_induct"}) |
28021 | 86 |
and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}). |
87 |
||
88 |
It is not entirely clear in which context it is better to use |
|
89 |
the one or the other, or whether the construction should be reversed. |
|
90 |
*} |
|
91 |
||
92 |
||
29945 | 93 |
subsection {* Numeral operations *} |
28021 | 94 |
|
95 |
ML {* |
|
96 |
structure DigSimps = |
|
97 |
NamedThmsFun(val name = "numeral"; val description = "Simplification rules for numerals") |
|
98 |
*} |
|
99 |
||
100 |
setup DigSimps.setup |
|
101 |
||
29945 | 102 |
instantiation num :: "{plus,times,ord}" |
103 |
begin |
|
28021 | 104 |
|
105 |
definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where |
|
28562 | 106 |
[code del]: "m + n = num_of_nat (nat_of_num m + nat_of_num n)" |
28021 | 107 |
|
108 |
definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where |
|
28562 | 109 |
[code del]: "m * n = num_of_nat (nat_of_num m * nat_of_num n)" |
28021 | 110 |
|
29945 | 111 |
definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where |
112 |
[code del]: "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n" |
|
28021 | 113 |
|
29945 | 114 |
definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where |
115 |
[code del]: "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n" |
|
28021 | 116 |
|
29945 | 117 |
instance .. |
28021 | 118 |
|
119 |
end |
|
120 |
||
29945 | 121 |
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y" |
122 |
unfolding plus_num_def |
|
123 |
by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos) |
|
124 |
||
125 |
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y" |
|
126 |
unfolding times_num_def |
|
127 |
by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos) |
|
28021 | 128 |
|
29945 | 129 |
lemma Dig_plus [numeral, simp, code]: |
130 |
"One + One = Dig0 One" |
|
131 |
"One + Dig0 m = Dig1 m" |
|
132 |
"One + Dig1 m = Dig0 (m + One)" |
|
133 |
"Dig0 n + One = Dig1 n" |
|
134 |
"Dig0 n + Dig0 m = Dig0 (n + m)" |
|
135 |
"Dig0 n + Dig1 m = Dig1 (n + m)" |
|
136 |
"Dig1 n + One = Dig0 (n + One)" |
|
137 |
"Dig1 n + Dig0 m = Dig1 (n + m)" |
|
138 |
"Dig1 n + Dig1 m = Dig0 (n + m + One)" |
|
139 |
by (simp_all add: num_eq_iff nat_of_num_add) |
|
28021 | 140 |
|
29945 | 141 |
lemma Dig_times [numeral, simp, code]: |
142 |
"One * One = One" |
|
143 |
"One * Dig0 n = Dig0 n" |
|
144 |
"One * Dig1 n = Dig1 n" |
|
145 |
"Dig0 n * One = Dig0 n" |
|
146 |
"Dig0 n * Dig0 m = Dig0 (n * Dig0 m)" |
|
147 |
"Dig0 n * Dig1 m = Dig0 (n * Dig1 m)" |
|
148 |
"Dig1 n * One = Dig1 n" |
|
149 |
"Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)" |
|
150 |
"Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)" |
|
151 |
by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult |
|
152 |
left_distrib right_distrib) |
|
28021 | 153 |
|
29991 | 154 |
lemma Dig_eq: |
155 |
"One = One \<longleftrightarrow> True" |
|
156 |
"One = Dig0 n \<longleftrightarrow> False" |
|
157 |
"One = Dig1 n \<longleftrightarrow> False" |
|
158 |
"Dig0 m = One \<longleftrightarrow> False" |
|
159 |
"Dig1 m = One \<longleftrightarrow> False" |
|
160 |
"Dig0 m = Dig0 n \<longleftrightarrow> m = n" |
|
161 |
"Dig0 m = Dig1 n \<longleftrightarrow> False" |
|
162 |
"Dig1 m = Dig0 n \<longleftrightarrow> False" |
|
163 |
"Dig1 m = Dig1 n \<longleftrightarrow> m = n" |
|
164 |
by simp_all |
|
165 |
||
29945 | 166 |
lemma less_eq_num_code [numeral, simp, code]: |
167 |
"One \<le> n \<longleftrightarrow> True" |
|
168 |
"Dig0 m \<le> One \<longleftrightarrow> False" |
|
169 |
"Dig1 m \<le> One \<longleftrightarrow> False" |
|
170 |
"Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n" |
|
171 |
"Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n" |
|
172 |
"Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n" |
|
173 |
"Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n" |
|
174 |
using nat_of_num_pos [of n] nat_of_num_pos [of m] |
|
175 |
by (auto simp add: less_eq_num_def less_num_def) |
|
176 |
||
177 |
lemma less_num_code [numeral, simp, code]: |
|
178 |
"m < One \<longleftrightarrow> False" |
|
179 |
"One < One \<longleftrightarrow> False" |
|
180 |
"One < Dig0 n \<longleftrightarrow> True" |
|
181 |
"One < Dig1 n \<longleftrightarrow> True" |
|
182 |
"Dig0 m < Dig0 n \<longleftrightarrow> m < n" |
|
183 |
"Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n" |
|
184 |
"Dig1 m < Dig1 n \<longleftrightarrow> m < n" |
|
185 |
"Dig1 m < Dig0 n \<longleftrightarrow> m < n" |
|
186 |
using nat_of_num_pos [of n] nat_of_num_pos [of m] |
|
187 |
by (auto simp add: less_eq_num_def less_num_def) |
|
188 |
||
189 |
text {* Rules using @{text One} and @{text inc} as constructors *} |
|
28021 | 190 |
|
29945 | 191 |
lemma add_One: "x + One = inc x" |
192 |
by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc) |
|
193 |
||
194 |
lemma add_inc: "x + inc y = inc (x + y)" |
|
195 |
by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc) |
|
196 |
||
197 |
lemma mult_One: "x * One = x" |
|
198 |
by (simp add: num_eq_iff nat_of_num_mult) |
|
199 |
||
200 |
lemma mult_inc: "x * inc y = x * y + x" |
|
201 |
by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc) |
|
202 |
||
203 |
text {* A double-and-decrement function *} |
|
28021 | 204 |
|
29945 | 205 |
primrec DigM :: "num \<Rightarrow> num" where |
206 |
"DigM One = One" |
|
207 |
| "DigM (Dig0 n) = Dig1 (DigM n)" |
|
208 |
| "DigM (Dig1 n) = Dig1 (Dig0 n)" |
|
28021 | 209 |
|
29945 | 210 |
lemma DigM_plus_one: "DigM n + One = Dig0 n" |
211 |
by (induct n) simp_all |
|
212 |
||
213 |
lemma add_One_commute: "One + n = n + One" |
|
214 |
by (induct n) simp_all |
|
215 |
||
216 |
lemma one_plus_DigM: "One + DigM n = Dig0 n" |
|
217 |
unfolding add_One_commute DigM_plus_one .. |
|
28021 | 218 |
|
29954 | 219 |
text {* Squaring and exponentiation *} |
29947 | 220 |
|
221 |
primrec square :: "num \<Rightarrow> num" where |
|
222 |
"square One = One" |
|
223 |
| "square (Dig0 n) = Dig0 (Dig0 (square n))" |
|
224 |
| "square (Dig1 n) = Dig1 (Dig0 (square n + n))" |
|
225 |
||
29954 | 226 |
primrec pow :: "num \<Rightarrow> num \<Rightarrow> num" |
227 |
where |
|
228 |
"pow x One = x" |
|
229 |
| "pow x (Dig0 y) = square (pow x y)" |
|
230 |
| "pow x (Dig1 y) = x * square (pow x y)" |
|
29947 | 231 |
|
28021 | 232 |
|
233 |
subsection {* Binary numerals *} |
|
234 |
||
235 |
text {* |
|
236 |
We embed binary representations into a generic algebraic |
|
29934 | 237 |
structure using @{text of_num}. |
28021 | 238 |
*} |
239 |
||
240 |
class semiring_numeral = semiring + monoid_mult |
|
241 |
begin |
|
242 |
||
243 |
primrec of_num :: "num \<Rightarrow> 'a" where |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
244 |
of_num_one [numeral]: "of_num One = 1" |
28021 | 245 |
| "of_num (Dig0 n) = of_num n + of_num n" |
246 |
| "of_num (Dig1 n) = of_num n + of_num n + 1" |
|
247 |
||
29943 | 248 |
lemma of_num_inc: "of_num (inc x) = of_num x + 1" |
249 |
by (induct x) (simp_all add: add_ac) |
|
250 |
||
28021 | 251 |
declare of_num.simps [simp del] |
252 |
||
253 |
end |
|
254 |
||
255 |
text {* |
|
256 |
ML stuff and syntax. |
|
257 |
*} |
|
258 |
||
259 |
ML {* |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
260 |
fun mk_num 1 = @{term One} |
28021 | 261 |
| mk_num k = |
262 |
let |
|
263 |
val (l, b) = Integer.div_mod k 2; |
|
264 |
val bit = (if b = 0 then @{term Dig0} else @{term Dig1}); |
|
265 |
in bit $ (mk_num l) end; |
|
266 |
||
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
267 |
fun dest_num @{term One} = 1 |
28021 | 268 |
| dest_num (@{term Dig0} $ n) = 2 * dest_num n |
269 |
| dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1; |
|
270 |
||
271 |
(*FIXME these have to gain proper context via morphisms phi*) |
|
272 |
||
273 |
fun mk_numeral T k = Const (@{const_name of_num}, @{typ num} --> T) |
|
274 |
$ mk_num k |
|
275 |
||
276 |
fun dest_numeral (Const (@{const_name of_num}, Type ("fun", [@{typ num}, T])) $ t) = |
|
277 |
(T, dest_num t) |
|
278 |
*} |
|
279 |
||
280 |
syntax |
|
281 |
"_Numerals" :: "xnum \<Rightarrow> 'a" ("_") |
|
282 |
||
283 |
parse_translation {* |
|
284 |
let |
|
285 |
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
|
286 |
of (0, 1) => Const (@{const_name One}, dummyT) |
28021 | 287 |
| (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n |
288 |
| (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n |
|
289 |
else raise Match; |
|
290 |
fun numeral_tr [Free (num, _)] = |
|
291 |
let |
|
292 |
val {leading_zeros, value, ...} = Syntax.read_xnum num; |
|
293 |
val _ = leading_zeros = 0 andalso value > 0 |
|
294 |
orelse error ("Bad numeral: " ^ num); |
|
295 |
in Const (@{const_name of_num}, @{typ num} --> dummyT) $ num_of_int value end |
|
296 |
| numeral_tr ts = raise TERM ("numeral_tr", ts); |
|
297 |
in [("_Numerals", numeral_tr)] end |
|
298 |
*} |
|
299 |
||
300 |
typed_print_translation {* |
|
301 |
let |
|
302 |
fun dig b n = b + 2 * n; |
|
303 |
fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) = |
|
304 |
dig 0 (int_of_num' n) |
|
305 |
| int_of_num' (Const (@{const_syntax Dig1}, _) $ n) = |
|
306 |
dig 1 (int_of_num' n) |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
307 |
| int_of_num' (Const (@{const_syntax One}, _)) = 1; |
28021 | 308 |
fun num_tr' show_sorts T [n] = |
309 |
let |
|
310 |
val k = int_of_num' n; |
|
311 |
val t' = Syntax.const "_Numerals" $ Syntax.free ("#" ^ string_of_int k); |
|
312 |
in case T |
|
313 |
of Type ("fun", [_, T']) => |
|
314 |
if not (! show_types) andalso can Term.dest_Type T' then t' |
|
315 |
else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T' |
|
316 |
| T' => if T' = dummyT then t' else raise Match |
|
317 |
end; |
|
318 |
in [(@{const_syntax of_num}, num_tr')] end |
|
319 |
*} |
|
320 |
||
29945 | 321 |
subsection {* Class-specific numeral rules *} |
28021 | 322 |
|
323 |
text {* |
|
324 |
@{const of_num} is a morphism. |
|
325 |
*} |
|
326 |
||
29945 | 327 |
subsubsection {* Class @{text semiring_numeral} *} |
328 |
||
28021 | 329 |
context semiring_numeral |
330 |
begin |
|
331 |
||
29943 | 332 |
abbreviation "Num1 \<equiv> of_num One" |
28021 | 333 |
|
334 |
text {* |
|
335 |
Alas, there is still the duplication of @{term 1}, |
|
336 |
thought the duplicated @{term 0} has disappeared. |
|
337 |
We could get rid of it by replacing the constructor |
|
338 |
@{term 1} in @{typ num} by two constructors |
|
339 |
@{text two} and @{text three}, resulting in a further |
|
340 |
blow-up. But it could be worth the effort. |
|
341 |
*} |
|
342 |
||
343 |
lemma of_num_plus_one [numeral]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
344 |
"of_num n + 1 = of_num (n + One)" |
29943 | 345 |
by (rule sym, induct n) (simp_all add: of_num.simps add_ac) |
28021 | 346 |
|
347 |
lemma of_num_one_plus [numeral]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
348 |
"1 + of_num n = of_num (n + One)" |
28021 | 349 |
unfolding of_num_plus_one [symmetric] add_commute .. |
350 |
||
351 |
lemma of_num_plus [numeral]: |
|
352 |
"of_num m + of_num n = of_num (m + n)" |
|
353 |
by (induct n rule: num_induct) |
|
29943 | 354 |
(simp_all add: add_One add_inc of_num_one of_num_inc add_ac) |
28021 | 355 |
|
356 |
lemma of_num_times_one [numeral]: |
|
357 |
"of_num n * 1 = of_num n" |
|
358 |
by simp |
|
359 |
||
360 |
lemma of_num_one_times [numeral]: |
|
361 |
"1 * of_num n = of_num n" |
|
362 |
by simp |
|
363 |
||
364 |
lemma of_num_times [numeral]: |
|
365 |
"of_num m * of_num n = of_num (m * n)" |
|
366 |
by (induct n rule: num_induct) |
|
29943 | 367 |
(simp_all add: of_num_plus [symmetric] mult_One mult_inc |
368 |
semiring_class.right_distrib right_distrib of_num_one of_num_inc) |
|
28021 | 369 |
|
370 |
end |
|
371 |
||
29945 | 372 |
subsubsection {* |
29947 | 373 |
Structures with a zero: class @{text semiring_1} |
28021 | 374 |
*} |
375 |
||
376 |
context semiring_1 |
|
377 |
begin |
|
378 |
||
379 |
subclass semiring_numeral .. |
|
380 |
||
381 |
lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n" |
|
382 |
by (induct n) |
|
383 |
(simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac) |
|
384 |
||
385 |
declare of_nat_1 [numeral] |
|
386 |
||
387 |
lemma Dig_plus_zero [numeral]: |
|
388 |
"0 + 1 = 1" |
|
389 |
"0 + of_num n = of_num n" |
|
390 |
"1 + 0 = 1" |
|
391 |
"of_num n + 0 = of_num n" |
|
392 |
by simp_all |
|
393 |
||
394 |
lemma Dig_times_zero [numeral]: |
|
395 |
"0 * 1 = 0" |
|
396 |
"0 * of_num n = 0" |
|
397 |
"1 * 0 = 0" |
|
398 |
"of_num n * 0 = 0" |
|
399 |
by simp_all |
|
400 |
||
401 |
end |
|
402 |
||
403 |
lemma nat_of_num_of_num: "nat_of_num = of_num" |
|
404 |
proof |
|
405 |
fix n |
|
29943 | 406 |
have "of_num n = nat_of_num n" |
407 |
by (induct n) (simp_all add: of_num.simps) |
|
28021 | 408 |
then show "nat_of_num n = of_num n" by simp |
409 |
qed |
|
410 |
||
29945 | 411 |
subsubsection {* |
412 |
Equality: class @{text semiring_char_0} |
|
28021 | 413 |
*} |
414 |
||
415 |
context semiring_char_0 |
|
416 |
begin |
|
417 |
||
418 |
lemma of_num_eq_iff [numeral]: |
|
419 |
"of_num m = of_num n \<longleftrightarrow> m = n" |
|
420 |
unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric] |
|
29943 | 421 |
of_nat_eq_iff num_eq_iff .. |
28021 | 422 |
|
423 |
lemma of_num_eq_one_iff [numeral]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
424 |
"of_num n = 1 \<longleftrightarrow> n = One" |
28021 | 425 |
proof - |
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
426 |
have "of_num n = of_num One \<longleftrightarrow> n = One" unfolding of_num_eq_iff .. |
28021 | 427 |
then show ?thesis by (simp add: of_num_one) |
428 |
qed |
|
429 |
||
430 |
lemma one_eq_of_num_iff [numeral]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
431 |
"1 = of_num n \<longleftrightarrow> n = One" |
28021 | 432 |
unfolding of_num_eq_one_iff [symmetric] by auto |
433 |
||
434 |
end |
|
435 |
||
29945 | 436 |
subsubsection {* |
437 |
Comparisons: class @{text ordered_semidom} |
|
28021 | 438 |
*} |
439 |
||
29945 | 440 |
text {* Could be perhaps more general than here. *} |
441 |
||
28021 | 442 |
context ordered_semidom |
443 |
begin |
|
444 |
||
29991 | 445 |
lemma of_num_pos [numeral]: "0 < of_num n" |
446 |
by (induct n) (simp_all add: of_num.simps add_pos_pos) |
|
447 |
||
28021 | 448 |
lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n" |
449 |
proof - |
|
450 |
have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n" |
|
451 |
unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff .. |
|
452 |
then show ?thesis by (simp add: of_nat_of_num) |
|
453 |
qed |
|
454 |
||
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
455 |
lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n = One" |
28021 | 456 |
proof - |
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
457 |
have "of_num n \<le> of_num One \<longleftrightarrow> n = One" |
28021 | 458 |
by (cases n) (simp_all add: of_num_less_eq_iff) |
459 |
then show ?thesis by (simp add: of_num_one) |
|
460 |
qed |
|
461 |
||
462 |
lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n" |
|
463 |
proof - |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
464 |
have "of_num One \<le> of_num n" |
28021 | 465 |
by (cases n) (simp_all add: of_num_less_eq_iff) |
466 |
then show ?thesis by (simp add: of_num_one) |
|
467 |
qed |
|
468 |
||
469 |
lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n" |
|
470 |
proof - |
|
471 |
have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n" |
|
472 |
unfolding less_num_def nat_of_num_of_num of_nat_less_iff .. |
|
473 |
then show ?thesis by (simp add: of_nat_of_num) |
|
474 |
qed |
|
475 |
||
476 |
lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1" |
|
477 |
proof - |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
478 |
have "\<not> of_num n < of_num One" |
28021 | 479 |
by (cases n) (simp_all add: of_num_less_iff) |
480 |
then show ?thesis by (simp add: of_num_one) |
|
481 |
qed |
|
482 |
||
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
483 |
lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> n \<noteq> One" |
28021 | 484 |
proof - |
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
485 |
have "of_num One < of_num n \<longleftrightarrow> n \<noteq> One" |
28021 | 486 |
by (cases n) (simp_all add: of_num_less_iff) |
487 |
then show ?thesis by (simp add: of_num_one) |
|
488 |
qed |
|
489 |
||
29991 | 490 |
lemma of_num_nonneg [numeral]: "0 \<le> of_num n" |
491 |
by (induct n) (simp_all add: of_num.simps add_nonneg_nonneg) |
|
492 |
||
493 |
lemma of_num_less_zero_iff [numeral]: "\<not> of_num n < 0" |
|
494 |
by (simp add: not_less of_num_nonneg) |
|
495 |
||
496 |
lemma of_num_le_zero_iff [numeral]: "\<not> of_num n \<le> 0" |
|
497 |
by (simp add: not_le of_num_pos) |
|
498 |
||
499 |
end |
|
500 |
||
501 |
context ordered_idom |
|
502 |
begin |
|
503 |
||
30792 | 504 |
lemma minus_of_num_less_of_num_iff: "- of_num m < of_num n" |
29991 | 505 |
proof - |
506 |
have "- of_num m < 0" by (simp add: of_num_pos) |
|
507 |
also have "0 < of_num n" by (simp add: of_num_pos) |
|
508 |
finally show ?thesis . |
|
509 |
qed |
|
510 |
||
30792 | 511 |
lemma minus_of_num_less_one_iff: "- of_num n < 1" |
512 |
using minus_of_num_less_of_num_iff [of n One] by (simp add: of_num_one) |
|
29991 | 513 |
|
30792 | 514 |
lemma minus_one_less_of_num_iff: "- 1 < of_num n" |
515 |
using minus_of_num_less_of_num_iff [of One n] by (simp add: of_num_one) |
|
29991 | 516 |
|
30792 | 517 |
lemma minus_one_less_one_iff: "- 1 < 1" |
518 |
using minus_of_num_less_of_num_iff [of One One] by (simp add: of_num_one) |
|
519 |
||
520 |
lemma minus_of_num_le_of_num_iff: "- of_num m \<le> of_num n" |
|
29991 | 521 |
by (simp add: less_imp_le minus_of_num_less_of_num_iff) |
522 |
||
30792 | 523 |
lemma minus_of_num_le_one_iff: "- of_num n \<le> 1" |
29991 | 524 |
by (simp add: less_imp_le minus_of_num_less_one_iff) |
525 |
||
30792 | 526 |
lemma minus_one_le_of_num_iff: "- 1 \<le> of_num n" |
29991 | 527 |
by (simp add: less_imp_le minus_one_less_of_num_iff) |
528 |
||
30792 | 529 |
lemma minus_one_le_one_iff: "- 1 \<le> 1" |
530 |
by (simp add: less_imp_le minus_one_less_one_iff) |
|
531 |
||
532 |
lemma of_num_le_minus_of_num_iff: "\<not> of_num m \<le> - of_num n" |
|
29991 | 533 |
by (simp add: not_le minus_of_num_less_of_num_iff) |
534 |
||
30792 | 535 |
lemma one_le_minus_of_num_iff: "\<not> 1 \<le> - of_num n" |
29991 | 536 |
by (simp add: not_le minus_of_num_less_one_iff) |
537 |
||
30792 | 538 |
lemma of_num_le_minus_one_iff: "\<not> of_num n \<le> - 1" |
29991 | 539 |
by (simp add: not_le minus_one_less_of_num_iff) |
540 |
||
30792 | 541 |
lemma one_le_minus_one_iff: "\<not> 1 \<le> - 1" |
542 |
by (simp add: not_le minus_one_less_one_iff) |
|
543 |
||
544 |
lemma of_num_less_minus_of_num_iff: "\<not> of_num m < - of_num n" |
|
29991 | 545 |
by (simp add: not_less minus_of_num_le_of_num_iff) |
546 |
||
30792 | 547 |
lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n" |
29991 | 548 |
by (simp add: not_less minus_of_num_le_one_iff) |
549 |
||
30792 | 550 |
lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1" |
29991 | 551 |
by (simp add: not_less minus_one_le_of_num_iff) |
552 |
||
30792 | 553 |
lemma one_less_minus_one_iff: "\<not> 1 < - 1" |
554 |
by (simp add: not_less minus_one_le_one_iff) |
|
555 |
||
556 |
lemmas le_signed_numeral_special [numeral] = |
|
557 |
minus_of_num_le_of_num_iff |
|
558 |
minus_of_num_le_one_iff |
|
559 |
minus_one_le_of_num_iff |
|
560 |
minus_one_le_one_iff |
|
561 |
of_num_le_minus_of_num_iff |
|
562 |
one_le_minus_of_num_iff |
|
563 |
of_num_le_minus_one_iff |
|
564 |
one_le_minus_one_iff |
|
565 |
||
566 |
lemmas less_signed_numeral_special [numeral] = |
|
567 |
minus_of_num_less_of_num_iff |
|
568 |
minus_of_num_less_one_iff |
|
569 |
minus_one_less_of_num_iff |
|
570 |
minus_one_less_one_iff |
|
571 |
of_num_less_minus_of_num_iff |
|
572 |
one_less_minus_of_num_iff |
|
573 |
of_num_less_minus_one_iff |
|
574 |
one_less_minus_one_iff |
|
575 |
||
28021 | 576 |
end |
577 |
||
29945 | 578 |
subsubsection {* |
29947 | 579 |
Structures with subtraction: class @{text semiring_1_minus} |
28021 | 580 |
*} |
581 |
||
582 |
class semiring_minus = semiring + minus + zero + |
|
583 |
assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c - b = a" |
|
584 |
assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b - c = 0 - a" |
|
585 |
begin |
|
586 |
||
587 |
lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c - a = b" |
|
588 |
by (simp add: add_ac minus_inverts_plus1 [of b a]) |
|
589 |
||
590 |
lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a - c = 0 - b" |
|
591 |
by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a]) |
|
592 |
||
593 |
end |
|
594 |
||
595 |
class semiring_1_minus = semiring_1 + semiring_minus |
|
596 |
begin |
|
597 |
||
598 |
lemma Dig_of_num_pos: |
|
599 |
assumes "k + n = m" |
|
600 |
shows "of_num m - of_num n = of_num k" |
|
601 |
using assms by (simp add: of_num_plus minus_inverts_plus1) |
|
602 |
||
603 |
lemma Dig_of_num_zero: |
|
604 |
shows "of_num n - of_num n = 0" |
|
605 |
by (rule minus_inverts_plus1) simp |
|
606 |
||
607 |
lemma Dig_of_num_neg: |
|
608 |
assumes "k + m = n" |
|
609 |
shows "of_num m - of_num n = 0 - of_num k" |
|
610 |
by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms) |
|
611 |
||
612 |
lemmas Dig_plus_eval = |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
613 |
of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject |
28021 | 614 |
|
615 |
simproc_setup numeral_minus ("of_num m - of_num n") = {* |
|
616 |
let |
|
617 |
(*TODO proper implicit use of morphism via pattern antiquotations*) |
|
618 |
fun cdest_of_num ct = (snd o split_last o snd o Drule.strip_comb) ct; |
|
619 |
fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n); |
|
620 |
fun attach_num ct = (dest_num (Thm.term_of ct), ct); |
|
621 |
fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t; |
|
622 |
val simplify = MetaSimplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval}); |
|
623 |
fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq} OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"}, |
|
624 |
[Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]]; |
|
625 |
in fn phi => fn _ => fn ct => case try cdifference ct |
|
626 |
of NONE => (NONE) |
|
627 |
| SOME ((k, ck), (l, cl)) => SOME (let val j = k - l in if j = 0 |
|
628 |
then MetaSimplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct |
|
629 |
else mk_meta_eq (let |
|
630 |
val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j)); |
|
631 |
in |
|
632 |
(if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck] |
|
633 |
else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl]) |
|
634 |
end) end) |
|
635 |
end |
|
636 |
*} |
|
637 |
||
638 |
lemma Dig_of_num_minus_zero [numeral]: |
|
639 |
"of_num n - 0 = of_num n" |
|
640 |
by (simp add: minus_inverts_plus1) |
|
641 |
||
642 |
lemma Dig_one_minus_zero [numeral]: |
|
643 |
"1 - 0 = 1" |
|
644 |
by (simp add: minus_inverts_plus1) |
|
645 |
||
646 |
lemma Dig_one_minus_one [numeral]: |
|
647 |
"1 - 1 = 0" |
|
648 |
by (simp add: minus_inverts_plus1) |
|
649 |
||
650 |
lemma Dig_of_num_minus_one [numeral]: |
|
29941 | 651 |
"of_num (Dig0 n) - 1 = of_num (DigM n)" |
28021 | 652 |
"of_num (Dig1 n) - 1 = of_num (Dig0 n)" |
29941 | 653 |
by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one) |
28021 | 654 |
|
655 |
lemma Dig_one_minus_of_num [numeral]: |
|
29941 | 656 |
"1 - of_num (Dig0 n) = 0 - of_num (DigM n)" |
28021 | 657 |
"1 - of_num (Dig1 n) = 0 - of_num (Dig0 n)" |
29941 | 658 |
by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one) |
28021 | 659 |
|
660 |
end |
|
661 |
||
29945 | 662 |
subsubsection {* |
29947 | 663 |
Structures with negation: class @{text ring_1} |
29945 | 664 |
*} |
665 |
||
28021 | 666 |
context ring_1 |
667 |
begin |
|
668 |
||
669 |
subclass semiring_1_minus |
|
29667 | 670 |
proof qed (simp_all add: algebra_simps) |
28021 | 671 |
|
672 |
lemma Dig_zero_minus_of_num [numeral]: |
|
673 |
"0 - of_num n = - of_num n" |
|
674 |
by simp |
|
675 |
||
676 |
lemma Dig_zero_minus_one [numeral]: |
|
677 |
"0 - 1 = - 1" |
|
678 |
by simp |
|
679 |
||
680 |
lemma Dig_uminus_uminus [numeral]: |
|
681 |
"- (- of_num n) = of_num n" |
|
682 |
by simp |
|
683 |
||
684 |
lemma Dig_plus_uminus [numeral]: |
|
685 |
"of_num m + - of_num n = of_num m - of_num n" |
|
686 |
"- of_num m + of_num n = of_num n - of_num m" |
|
687 |
"- of_num m + - of_num n = - (of_num m + of_num n)" |
|
688 |
"of_num m - - of_num n = of_num m + of_num n" |
|
689 |
"- of_num m - of_num n = - (of_num m + of_num n)" |
|
690 |
"- of_num m - - of_num n = of_num n - of_num m" |
|
691 |
by (simp_all add: diff_minus add_commute) |
|
692 |
||
693 |
lemma Dig_times_uminus [numeral]: |
|
694 |
"- of_num n * of_num m = - (of_num n * of_num m)" |
|
695 |
"of_num n * - of_num m = - (of_num n * of_num m)" |
|
696 |
"- of_num n * - of_num m = of_num n * of_num m" |
|
697 |
by (simp_all add: minus_mult_left [symmetric] minus_mult_right [symmetric]) |
|
698 |
||
699 |
lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n" |
|
700 |
by (induct n) |
|
701 |
(simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all) |
|
702 |
||
703 |
declare of_int_1 [numeral] |
|
704 |
||
705 |
end |
|
706 |
||
29945 | 707 |
subsubsection {* |
29954 | 708 |
Structures with exponentiation |
709 |
*} |
|
710 |
||
711 |
lemma of_num_square: "of_num (square x) = of_num x * of_num x" |
|
712 |
by (induct x) |
|
713 |
(simp_all add: of_num.simps of_num_plus [symmetric] algebra_simps) |
|
714 |
||
715 |
lemma of_num_pow: |
|
31021 | 716 |
"(of_num (pow x y)::'a::{semiring_numeral}) = of_num x ^ of_num y" |
29954 | 717 |
by (induct y) |
718 |
(simp_all add: of_num.simps of_num_square of_num_times [symmetric] |
|
719 |
power_Suc power_add) |
|
720 |
||
721 |
lemma power_of_num [numeral]: |
|
31021 | 722 |
"of_num x ^ of_num y = (of_num (pow x y)::'a::{semiring_numeral})" |
29954 | 723 |
by (rule of_num_pow [symmetric]) |
724 |
||
725 |
lemma power_zero_of_num [numeral]: |
|
31021 | 726 |
"0 ^ of_num n = (0::'a::{semiring_0,power})" |
29954 | 727 |
using of_num_pos [where n=n and ?'a=nat] |
728 |
by (simp add: power_0_left) |
|
729 |
||
730 |
lemma power_minus_one_double: |
|
31021 | 731 |
"(- 1) ^ (n + n) = (1::'a::{ring_1})" |
29954 | 732 |
by (induct n) (simp_all add: power_Suc) |
733 |
||
734 |
lemma power_minus_Dig0 [numeral]: |
|
31021 | 735 |
fixes x :: "'a::{ring_1}" |
29954 | 736 |
shows "(- x) ^ of_num (Dig0 n) = x ^ of_num (Dig0 n)" |
737 |
by (subst power_minus) |
|
738 |
(simp add: of_num.simps power_minus_one_double) |
|
739 |
||
740 |
lemma power_minus_Dig1 [numeral]: |
|
31021 | 741 |
fixes x :: "'a::{ring_1}" |
29954 | 742 |
shows "(- x) ^ of_num (Dig1 n) = - (x ^ of_num (Dig1 n))" |
743 |
by (subst power_minus) |
|
744 |
(simp add: of_num.simps power_Suc power_minus_one_double) |
|
745 |
||
746 |
declare power_one [numeral] |
|
747 |
||
748 |
||
749 |
subsubsection {* |
|
28021 | 750 |
Greetings to @{typ nat}. |
751 |
*} |
|
752 |
||
753 |
instance nat :: semiring_1_minus proof qed simp_all |
|
754 |
||
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
755 |
lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)" |
28021 | 756 |
unfolding of_num_plus_one [symmetric] by simp |
757 |
||
758 |
lemma nat_number: |
|
759 |
"1 = Suc 0" |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
760 |
"of_num One = Suc 0" |
29941 | 761 |
"of_num (Dig0 n) = Suc (of_num (DigM n))" |
28021 | 762 |
"of_num (Dig1 n) = Suc (of_num (Dig0 n))" |
29941 | 763 |
by (simp_all add: of_num.simps DigM_plus_one Suc_of_num) |
28021 | 764 |
|
765 |
declare diff_0_eq_0 [numeral] |
|
766 |
||
767 |
||
768 |
subsection {* Code generator setup for @{typ int} *} |
|
769 |
||
770 |
definition Pls :: "num \<Rightarrow> int" where |
|
771 |
[simp, code post]: "Pls n = of_num n" |
|
772 |
||
773 |
definition Mns :: "num \<Rightarrow> int" where |
|
774 |
[simp, code post]: "Mns n = - of_num n" |
|
775 |
||
776 |
code_datatype "0::int" Pls Mns |
|
777 |
||
778 |
lemmas [code inline] = Pls_def [symmetric] Mns_def [symmetric] |
|
779 |
||
780 |
definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where |
|
28562 | 781 |
[simp, code del]: "sub m n = (of_num m - of_num n)" |
28021 | 782 |
|
783 |
definition dup :: "int \<Rightarrow> int" where |
|
28562 | 784 |
[code del]: "dup k = 2 * k" |
28021 | 785 |
|
786 |
lemma Dig_sub [code]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
787 |
"sub One One = 0" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
788 |
"sub (Dig0 m) One = of_num (DigM m)" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
789 |
"sub (Dig1 m) One = of_num (Dig0 m)" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
790 |
"sub One (Dig0 n) = - of_num (DigM n)" |
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
791 |
"sub One (Dig1 n) = - of_num (Dig0 n)" |
28021 | 792 |
"sub (Dig0 m) (Dig0 n) = dup (sub m n)" |
793 |
"sub (Dig1 m) (Dig1 n) = dup (sub m n)" |
|
794 |
"sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1" |
|
795 |
"sub (Dig0 m) (Dig1 n) = dup (sub m n) - 1" |
|
29667 | 796 |
apply (simp_all add: dup_def algebra_simps) |
29941 | 797 |
apply (simp_all add: of_num_plus one_plus_DigM)[4] |
28021 | 798 |
apply (simp_all add: of_num.simps) |
799 |
done |
|
800 |
||
801 |
lemma dup_code [code]: |
|
802 |
"dup 0 = 0" |
|
803 |
"dup (Pls n) = Pls (Dig0 n)" |
|
804 |
"dup (Mns n) = Mns (Dig0 n)" |
|
805 |
by (simp_all add: dup_def of_num.simps) |
|
806 |
||
28562 | 807 |
lemma [code, code del]: |
28021 | 808 |
"(1 :: int) = 1" |
809 |
"(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +" |
|
810 |
"(uminus :: int \<Rightarrow> int) = uminus" |
|
811 |
"(op - :: int \<Rightarrow> int \<Rightarrow> int) = op -" |
|
812 |
"(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *" |
|
28367 | 813 |
"(eq_class.eq :: int \<Rightarrow> int \<Rightarrow> bool) = eq_class.eq" |
28021 | 814 |
"(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>" |
815 |
"(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <" |
|
816 |
by rule+ |
|
817 |
||
818 |
lemma one_int_code [code]: |
|
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset
|
819 |
"1 = Pls One" |
28021 | 820 |
by (simp add: of_num_one) |
821 |
||
822 |
lemma plus_int_code [code]: |
|
823 |
"k + 0 = (k::int)" |
|
824 |
"0 + l = (l::int)" |
|
825 |
"Pls m + Pls n = Pls (m + n)" |
|
826 |
"Pls m - Pls n = sub m n" |
|
827 |
"Mns m + Mns n = Mns (m + n)" |
|
828 |
"Mns m - Mns n = sub n m" |
|
829 |
by (simp_all add: of_num_plus [symmetric]) |
|
830 |
||
831 |
lemma uminus_int_code [code]: |
|
832 |
"uminus 0 = (0::int)" |
|
833 |
"uminus (Pls m) = Mns m" |
|
834 |
"uminus (Mns m) = Pls m" |
|
835 |
by simp_all |
|
836 |
||
837 |
lemma minus_int_code [code]: |
|
838 |
"k - 0 = (k::int)" |
|
839 |
"0 - l = uminus (l::int)" |
|
840 |
"Pls m - Pls n = sub m n" |
|
841 |
"Pls m - Mns n = Pls (m + n)" |
|
842 |
"Mns m - Pls n = Mns (m + n)" |
|
843 |
"Mns m - Mns n = sub n m" |
|
844 |
by (simp_all add: of_num_plus [symmetric]) |
|
845 |
||
846 |
lemma times_int_code [code]: |
|
847 |
"k * 0 = (0::int)" |
|
848 |
"0 * l = (0::int)" |
|
849 |
"Pls m * Pls n = Pls (m * n)" |
|
850 |
"Pls m * Mns n = Mns (m * n)" |
|
851 |
"Mns m * Pls n = Mns (m * n)" |
|
852 |
"Mns m * Mns n = Pls (m * n)" |
|
853 |
by (simp_all add: of_num_times [symmetric]) |
|
854 |
||
855 |
lemma eq_int_code [code]: |
|
28367 | 856 |
"eq_class.eq 0 (0::int) \<longleftrightarrow> True" |
857 |
"eq_class.eq 0 (Pls l) \<longleftrightarrow> False" |
|
858 |
"eq_class.eq 0 (Mns l) \<longleftrightarrow> False" |
|
859 |
"eq_class.eq (Pls k) 0 \<longleftrightarrow> False" |
|
860 |
"eq_class.eq (Pls k) (Pls l) \<longleftrightarrow> eq_class.eq k l" |
|
861 |
"eq_class.eq (Pls k) (Mns l) \<longleftrightarrow> False" |
|
862 |
"eq_class.eq (Mns k) 0 \<longleftrightarrow> False" |
|
863 |
"eq_class.eq (Mns k) (Pls l) \<longleftrightarrow> False" |
|
864 |
"eq_class.eq (Mns k) (Mns l) \<longleftrightarrow> eq_class.eq k l" |
|
28021 | 865 |
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] |
28367 | 866 |
by (simp_all add: of_num_eq_iff eq) |
28021 | 867 |
|
868 |
lemma less_eq_int_code [code]: |
|
869 |
"0 \<le> (0::int) \<longleftrightarrow> True" |
|
870 |
"0 \<le> Pls l \<longleftrightarrow> True" |
|
871 |
"0 \<le> Mns l \<longleftrightarrow> False" |
|
872 |
"Pls k \<le> 0 \<longleftrightarrow> False" |
|
873 |
"Pls k \<le> Pls l \<longleftrightarrow> k \<le> l" |
|
874 |
"Pls k \<le> Mns l \<longleftrightarrow> False" |
|
875 |
"Mns k \<le> 0 \<longleftrightarrow> True" |
|
876 |
"Mns k \<le> Pls l \<longleftrightarrow> True" |
|
877 |
"Mns k \<le> Mns l \<longleftrightarrow> l \<le> k" |
|
878 |
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] |
|
879 |
by (simp_all add: of_num_less_eq_iff) |
|
880 |
||
881 |
lemma less_int_code [code]: |
|
882 |
"0 < (0::int) \<longleftrightarrow> False" |
|
883 |
"0 < Pls l \<longleftrightarrow> True" |
|
884 |
"0 < Mns l \<longleftrightarrow> False" |
|
885 |
"Pls k < 0 \<longleftrightarrow> False" |
|
886 |
"Pls k < Pls l \<longleftrightarrow> k < l" |
|
887 |
"Pls k < Mns l \<longleftrightarrow> False" |
|
888 |
"Mns k < 0 \<longleftrightarrow> True" |
|
889 |
"Mns k < Pls l \<longleftrightarrow> True" |
|
890 |
"Mns k < Mns l \<longleftrightarrow> l < k" |
|
891 |
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] |
|
892 |
by (simp_all add: of_num_less_iff) |
|
893 |
||
894 |
lemma [code inline del]: "(0::int) \<equiv> Numeral0" by simp |
|
895 |
lemma [code inline del]: "(1::int) \<equiv> Numeral1" by simp |
|
896 |
declare zero_is_num_zero [code inline del] |
|
897 |
declare one_is_num_one [code inline del] |
|
898 |
||
899 |
hide (open) const sub dup |
|
900 |
||
901 |
||
902 |
subsection {* Numeral equations as default simplification rules *} |
|
903 |
||
29934 | 904 |
text {* TODO. Be more precise here with respect to subsumed facts. Or use named theorems anyway. *} |
28021 | 905 |
declare (in semiring_numeral) numeral [simp] |
906 |
declare (in semiring_1) numeral [simp] |
|
907 |
declare (in semiring_char_0) numeral [simp] |
|
908 |
declare (in ring_1) numeral [simp] |
|
909 |
thm numeral |
|
910 |
||
911 |
||
31025 | 912 |
subsection {* Simplification Procedures *} |
913 |
||
31026
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
914 |
subsubsection {* Reorientation of equalities *} |
31025 | 915 |
|
916 |
setup {* |
|
917 |
ReorientProc.add |
|
918 |
(fn Const(@{const_name of_num}, _) $ _ => true |
|
919 |
| Const(@{const_name uminus}, _) $ |
|
920 |
(Const(@{const_name of_num}, _) $ _) => true |
|
921 |
| _ => false) |
|
922 |
*} |
|
923 |
||
924 |
simproc_setup reorient_num ("of_num n = x" | "- of_num m = y") = ReorientProc.proc |
|
925 |
||
31026
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
926 |
subsubsection {* Constant folding for multiplication in semirings *} |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
927 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
928 |
context semiring_numeral |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
929 |
begin |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
930 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
931 |
lemma mult_of_num_commute: "x * of_num n = of_num n * x" |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
932 |
by (induct n) |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
933 |
(simp_all only: of_num.simps left_distrib right_distrib mult_1_left mult_1_right) |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
934 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
935 |
definition |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
936 |
"commutes_with a b \<longleftrightarrow> a * b = b * a" |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
937 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
938 |
lemma commutes_with_commute: "commutes_with a b \<Longrightarrow> a * b = b * a" |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
939 |
unfolding commutes_with_def . |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
940 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
941 |
lemma commutes_with_left_commute: "commutes_with a b \<Longrightarrow> a * (b * c) = b * (a * c)" |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
942 |
unfolding commutes_with_def by (simp only: mult_assoc [symmetric]) |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
943 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
944 |
lemma commutes_with_numeral: "commutes_with x (of_num n)" "commutes_with (of_num n) x" |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
945 |
unfolding commutes_with_def by (simp_all add: mult_of_num_commute) |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
946 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
947 |
lemmas mult_ac_numeral = |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
948 |
mult_assoc |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
949 |
commutes_with_commute |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
950 |
commutes_with_left_commute |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
951 |
commutes_with_numeral |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
952 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
953 |
end |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
954 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
955 |
ML {* |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
956 |
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA = |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
957 |
struct |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
958 |
val assoc_ss = HOL_ss addsimps @{thms mult_ac_numeral} |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
959 |
val eq_reflection = eq_reflection |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
960 |
fun is_numeral (Const(@{const_name of_num}, _) $ _) = true |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
961 |
| is_numeral _ = false; |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
962 |
end; |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
963 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
964 |
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data); |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
965 |
*} |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
966 |
|
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
967 |
simproc_setup semiring_assoc_fold' ("(a::'a::semiring_numeral) * b") = |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
968 |
{* fn phi => fn ss => fn ct => |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
969 |
Semiring_Times_Assoc.proc ss (Thm.term_of ct) *} |
020efcbfe2d8
add semiring_assoc_fold simproc for unsigned numerals
huffman
parents:
31025
diff
changeset
|
970 |
|
31025 | 971 |
|
972 |
subsection {* Toy examples *} |
|
28021 | 973 |
|
974 |
definition "bar \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat) \<and> #4 * #2 + #7 \<ge> (#8 :: int) - #3" |
|
975 |
code_thms bar |
|
976 |
export_code bar in Haskell file - |
|
977 |
export_code bar in OCaml module_name Foo file - |
|
978 |
ML {* @{code bar} *} |
|
979 |
||
980 |
end |