| author | haftmann | 
| Wed, 24 Oct 2007 07:19:56 +0200 | |
| changeset 25166 | d813a98a5a36 | 
| parent 25145 | d432105e5bd0 | 
| child 25491 | 5760991891dd | 
| permissions | -rw-r--r-- | 
| 23164 | 1  | 
(* Title: HOL/Numeral.thy  | 
2  | 
ID: $Id$  | 
|
3  | 
Author: Lawrence C Paulson, Cambridge University Computer Laboratory  | 
|
4  | 
Copyright 1994 University of Cambridge  | 
|
5  | 
*)  | 
|
6  | 
||
7  | 
header {* Arithmetic on Binary Integers *}
 | 
|
8  | 
||
9  | 
theory Numeral  | 
|
| 23708 | 10  | 
imports Datatype IntDef  | 
| 23574 | 11  | 
uses  | 
12  | 
  ("Tools/numeral.ML")
 | 
|
13  | 
  ("Tools/numeral_syntax.ML")
 | 
|
| 23164 | 14  | 
begin  | 
15  | 
||
16  | 
subsection {* Binary representation *}
 | 
|
17  | 
||
18  | 
text {*
 | 
|
19  | 
This formalization defines binary arithmetic in terms of the integers  | 
|
20  | 
rather than using a datatype. This avoids multiple representations (leading  | 
|
21  | 
  zeroes, etc.)  See @{text "ZF/Tools/twos-compl.ML"}, function @{text
 | 
|
22  | 
int_of_binary}, for the numerical interpretation.  | 
|
23  | 
||
24  | 
  The representation expects that @{text "(m mod 2)"} is 0 or 1,
 | 
|
25  | 
even if m is negative;  | 
|
26  | 
  For instance, @{text "-5 div 2 = -3"} and @{text "-5 mod 2 = 1"}; thus
 | 
|
27  | 
  @{text "-5 = (-3)*2 + 1"}.
 | 
|
28  | 
*}  | 
|
29  | 
||
30  | 
datatype bit = B0 | B1  | 
|
31  | 
||
32  | 
text{*
 | 
|
33  | 
  Type @{typ bit} avoids the use of type @{typ bool}, which would make
 | 
|
34  | 
all of the rewrite rules higher-order.  | 
|
35  | 
*}  | 
|
36  | 
||
37  | 
definition  | 
|
38  | 
Pls :: int where  | 
|
| 24166 | 39  | 
[code func del]: "Pls = 0"  | 
| 23164 | 40  | 
|
41  | 
definition  | 
|
42  | 
Min :: int where  | 
|
| 24166 | 43  | 
[code func del]: "Min = - 1"  | 
| 23164 | 44  | 
|
45  | 
definition  | 
|
46  | 
Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where  | 
|
47  | 
[code func del]: "k BIT b = (case b of B0 \<Rightarrow> 0 | B1 \<Rightarrow> 1) + k + k"  | 
|
48  | 
||
49  | 
class number = type + -- {* for numeric types: nat, int, real, \dots *}
 | 
|
50  | 
fixes number_of :: "int \<Rightarrow> 'a"  | 
|
51  | 
||
| 23574 | 52  | 
use "Tools/numeral.ML"  | 
53  | 
||
| 23164 | 54  | 
syntax  | 
55  | 
  "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
 | 
|
56  | 
||
57  | 
use "Tools/numeral_syntax.ML"  | 
|
58  | 
setup NumeralSyntax.setup  | 
|
59  | 
||
60  | 
abbreviation  | 
|
61  | 
"Numeral0 \<equiv> number_of Pls"  | 
|
62  | 
||
63  | 
abbreviation  | 
|
64  | 
"Numeral1 \<equiv> number_of (Pls BIT B1)"  | 
|
65  | 
||
66  | 
lemma Let_number_of [simp]: "Let (number_of v) f = f (number_of v)"  | 
|
67  | 
  -- {* Unfold all @{text let}s involving constants *}
 | 
|
68  | 
unfolding Let_def ..  | 
|
69  | 
||
70  | 
definition  | 
|
71  | 
succ :: "int \<Rightarrow> int" where  | 
|
72  | 
[code func del]: "succ k = k + 1"  | 
|
73  | 
||
74  | 
definition  | 
|
75  | 
pred :: "int \<Rightarrow> int" where  | 
|
76  | 
[code func del]: "pred k = k - 1"  | 
|
77  | 
||
78  | 
lemmas  | 
|
79  | 
max_number_of [simp] = max_def  | 
|
80  | 
[of "number_of u" "number_of v", standard, simp]  | 
|
81  | 
and  | 
|
82  | 
min_number_of [simp] = min_def  | 
|
83  | 
[of "number_of u" "number_of v", standard, simp]  | 
|
84  | 
  -- {* unfolding @{text minx} and @{text max} on numerals *}
 | 
|
85  | 
||
86  | 
lemmas numeral_simps =  | 
|
87  | 
succ_def pred_def Pls_def Min_def Bit_def  | 
|
88  | 
||
89  | 
text {* Removal of leading zeroes *}
 | 
|
90  | 
||
| 24166 | 91  | 
lemma Pls_0_eq [simp, code post]:  | 
| 23164 | 92  | 
"Pls BIT B0 = Pls"  | 
93  | 
unfolding numeral_simps by simp  | 
|
94  | 
||
| 24166 | 95  | 
lemma Min_1_eq [simp, code post]:  | 
| 23164 | 96  | 
"Min BIT B1 = Min"  | 
97  | 
unfolding numeral_simps by simp  | 
|
98  | 
||
99  | 
||
100  | 
subsection {* The Functions @{term succ}, @{term pred} and @{term uminus} *}
 | 
|
101  | 
||
102  | 
lemma succ_Pls [simp]:  | 
|
103  | 
"succ Pls = Pls BIT B1"  | 
|
104  | 
unfolding numeral_simps by simp  | 
|
105  | 
||
106  | 
lemma succ_Min [simp]:  | 
|
107  | 
"succ Min = Pls"  | 
|
108  | 
unfolding numeral_simps by simp  | 
|
109  | 
||
110  | 
lemma succ_1 [simp]:  | 
|
111  | 
"succ (k BIT B1) = succ k BIT B0"  | 
|
112  | 
unfolding numeral_simps by simp  | 
|
113  | 
||
114  | 
lemma succ_0 [simp]:  | 
|
115  | 
"succ (k BIT B0) = k BIT B1"  | 
|
116  | 
unfolding numeral_simps by simp  | 
|
117  | 
||
118  | 
lemma pred_Pls [simp]:  | 
|
119  | 
"pred Pls = Min"  | 
|
120  | 
unfolding numeral_simps by simp  | 
|
121  | 
||
122  | 
lemma pred_Min [simp]:  | 
|
123  | 
"pred Min = Min BIT B0"  | 
|
124  | 
unfolding numeral_simps by simp  | 
|
125  | 
||
126  | 
lemma pred_1 [simp]:  | 
|
127  | 
"pred (k BIT B1) = k BIT B0"  | 
|
128  | 
unfolding numeral_simps by simp  | 
|
129  | 
||
130  | 
lemma pred_0 [simp]:  | 
|
131  | 
"pred (k BIT B0) = pred k BIT B1"  | 
|
132  | 
unfolding numeral_simps by simp  | 
|
133  | 
||
134  | 
lemma minus_Pls [simp]:  | 
|
135  | 
"- Pls = Pls"  | 
|
136  | 
unfolding numeral_simps by simp  | 
|
137  | 
||
138  | 
lemma minus_Min [simp]:  | 
|
139  | 
"- Min = Pls BIT B1"  | 
|
140  | 
unfolding numeral_simps by simp  | 
|
141  | 
||
142  | 
lemma minus_1 [simp]:  | 
|
143  | 
"- (k BIT B1) = pred (- k) BIT B1"  | 
|
144  | 
unfolding numeral_simps by simp  | 
|
145  | 
||
146  | 
lemma minus_0 [simp]:  | 
|
147  | 
"- (k BIT B0) = (- k) BIT B0"  | 
|
148  | 
unfolding numeral_simps by simp  | 
|
149  | 
||
150  | 
||
151  | 
subsection {*
 | 
|
152  | 
  Binary Addition and Multiplication: @{term "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
 | 
|
153  | 
    and @{term "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
 | 
|
154  | 
*}  | 
|
155  | 
||
156  | 
lemma add_Pls [simp]:  | 
|
157  | 
"Pls + k = k"  | 
|
158  | 
unfolding numeral_simps by simp  | 
|
159  | 
||
160  | 
lemma add_Min [simp]:  | 
|
161  | 
"Min + k = pred k"  | 
|
162  | 
unfolding numeral_simps by simp  | 
|
163  | 
||
164  | 
lemma add_BIT_11 [simp]:  | 
|
165  | 
"(k BIT B1) + (l BIT B1) = (k + succ l) BIT B0"  | 
|
166  | 
unfolding numeral_simps by simp  | 
|
167  | 
||
168  | 
lemma add_BIT_10 [simp]:  | 
|
169  | 
"(k BIT B1) + (l BIT B0) = (k + l) BIT B1"  | 
|
170  | 
unfolding numeral_simps by simp  | 
|
171  | 
||
172  | 
lemma add_BIT_0 [simp]:  | 
|
173  | 
"(k BIT B0) + (l BIT b) = (k + l) BIT b"  | 
|
174  | 
unfolding numeral_simps by simp  | 
|
175  | 
||
176  | 
lemma add_Pls_right [simp]:  | 
|
177  | 
"k + Pls = k"  | 
|
178  | 
unfolding numeral_simps by simp  | 
|
179  | 
||
180  | 
lemma add_Min_right [simp]:  | 
|
181  | 
"k + Min = pred k"  | 
|
182  | 
unfolding numeral_simps by simp  | 
|
183  | 
||
184  | 
lemma mult_Pls [simp]:  | 
|
185  | 
"Pls * w = Pls"  | 
|
186  | 
unfolding numeral_simps by simp  | 
|
187  | 
||
188  | 
lemma mult_Min [simp]:  | 
|
189  | 
"Min * k = - k"  | 
|
190  | 
unfolding numeral_simps by simp  | 
|
191  | 
||
192  | 
lemma mult_num1 [simp]:  | 
|
193  | 
"(k BIT B1) * l = ((k * l) BIT B0) + l"  | 
|
194  | 
unfolding numeral_simps int_distrib by simp  | 
|
195  | 
||
196  | 
lemma mult_num0 [simp]:  | 
|
197  | 
"(k BIT B0) * l = (k * l) BIT B0"  | 
|
198  | 
unfolding numeral_simps int_distrib by simp  | 
|
199  | 
||
200  | 
||
201  | 
||
202  | 
subsection {* Converting Numerals to Rings: @{term number_of} *}
 | 
|
203  | 
||
204  | 
axclass number_ring \<subseteq> number, comm_ring_1  | 
|
205  | 
number_of_eq: "number_of k = of_int k"  | 
|
206  | 
||
| 23855 | 207  | 
text {* self-embedding of the integers *}
 | 
| 23164 | 208  | 
|
209  | 
instance int :: number_ring  | 
|
210  | 
int_number_of_def: "number_of w \<equiv> of_int w"  | 
|
211  | 
by intro_classes (simp only: int_number_of_def)  | 
|
212  | 
||
213  | 
lemmas [code func del] = int_number_of_def  | 
|
214  | 
||
215  | 
lemma number_of_is_id:  | 
|
216  | 
"number_of (k::int) = k"  | 
|
217  | 
unfolding int_number_of_def by simp  | 
|
218  | 
||
219  | 
lemma number_of_succ:  | 
|
220  | 
"number_of (succ k) = (1 + number_of k ::'a::number_ring)"  | 
|
221  | 
unfolding number_of_eq numeral_simps by simp  | 
|
222  | 
||
223  | 
lemma number_of_pred:  | 
|
224  | 
"number_of (pred w) = (- 1 + number_of w ::'a::number_ring)"  | 
|
225  | 
unfolding number_of_eq numeral_simps by simp  | 
|
226  | 
||
227  | 
lemma number_of_minus:  | 
|
228  | 
"number_of (uminus w) = (- (number_of w)::'a::number_ring)"  | 
|
229  | 
unfolding number_of_eq numeral_simps by simp  | 
|
230  | 
||
231  | 
lemma number_of_add:  | 
|
232  | 
"number_of (v + w) = (number_of v + number_of w::'a::number_ring)"  | 
|
233  | 
unfolding number_of_eq numeral_simps by simp  | 
|
234  | 
||
235  | 
lemma number_of_mult:  | 
|
236  | 
"number_of (v * w) = (number_of v * number_of w::'a::number_ring)"  | 
|
237  | 
unfolding number_of_eq numeral_simps by simp  | 
|
238  | 
||
239  | 
text {*
 | 
|
240  | 
The correctness of shifting.  | 
|
241  | 
But it doesn't seem to give a measurable speed-up.  | 
|
242  | 
*}  | 
|
243  | 
||
244  | 
lemma double_number_of_BIT:  | 
|
245  | 
"(1 + 1) * number_of w = (number_of (w BIT B0) ::'a::number_ring)"  | 
|
246  | 
unfolding number_of_eq numeral_simps left_distrib by simp  | 
|
247  | 
||
248  | 
text {*
 | 
|
249  | 
Converting numerals 0 and 1 to their abstract versions.  | 
|
250  | 
*}  | 
|
251  | 
||
252  | 
lemma numeral_0_eq_0 [simp]:  | 
|
253  | 
"Numeral0 = (0::'a::number_ring)"  | 
|
254  | 
unfolding number_of_eq numeral_simps by simp  | 
|
255  | 
||
256  | 
lemma numeral_1_eq_1 [simp]:  | 
|
257  | 
"Numeral1 = (1::'a::number_ring)"  | 
|
258  | 
unfolding number_of_eq numeral_simps by simp  | 
|
259  | 
||
260  | 
text {*
 | 
|
261  | 
Special-case simplification for small constants.  | 
|
262  | 
*}  | 
|
263  | 
||
264  | 
text{*
 | 
|
265  | 
Unary minus for the abstract constant 1. Cannot be inserted  | 
|
266  | 
  as a simprule until later: it is @{text number_of_Min} re-oriented!
 | 
|
267  | 
*}  | 
|
268  | 
||
269  | 
lemma numeral_m1_eq_minus_1:  | 
|
270  | 
"(-1::'a::number_ring) = - 1"  | 
|
271  | 
unfolding number_of_eq numeral_simps by simp  | 
|
272  | 
||
273  | 
lemma mult_minus1 [simp]:  | 
|
274  | 
"-1 * z = -(z::'a::number_ring)"  | 
|
275  | 
unfolding number_of_eq numeral_simps by simp  | 
|
276  | 
||
277  | 
lemma mult_minus1_right [simp]:  | 
|
278  | 
"z * -1 = -(z::'a::number_ring)"  | 
|
279  | 
unfolding number_of_eq numeral_simps by simp  | 
|
280  | 
||
281  | 
(*Negation of a coefficient*)  | 
|
282  | 
lemma minus_number_of_mult [simp]:  | 
|
283  | 
"- (number_of w) * z = number_of (uminus w) * (z::'a::number_ring)"  | 
|
284  | 
unfolding number_of_eq by simp  | 
|
285  | 
||
286  | 
text {* Subtraction *}
 | 
|
287  | 
||
288  | 
lemma diff_number_of_eq:  | 
|
289  | 
"number_of v - number_of w =  | 
|
290  | 
(number_of (v + uminus w)::'a::number_ring)"  | 
|
291  | 
unfolding number_of_eq by simp  | 
|
292  | 
||
293  | 
lemma number_of_Pls:  | 
|
294  | 
"number_of Pls = (0::'a::number_ring)"  | 
|
295  | 
unfolding number_of_eq numeral_simps by simp  | 
|
296  | 
||
297  | 
lemma number_of_Min:  | 
|
298  | 
"number_of Min = (- 1::'a::number_ring)"  | 
|
299  | 
unfolding number_of_eq numeral_simps by simp  | 
|
300  | 
||
301  | 
lemma number_of_BIT:  | 
|
302  | 
"number_of(w BIT x) = (case x of B0 => 0 | B1 => (1::'a::number_ring))  | 
|
303  | 
+ (number_of w) + (number_of w)"  | 
|
304  | 
unfolding number_of_eq numeral_simps by (simp split: bit.split)  | 
|
305  | 
||
306  | 
||
307  | 
subsection {* Equality of Binary Numbers *}
 | 
|
308  | 
||
309  | 
text {* First version by Norbert Voelker *}
 | 
|
310  | 
||
311  | 
lemma eq_number_of_eq:  | 
|
312  | 
"((number_of x::'a::number_ring) = number_of y) =  | 
|
313  | 
iszero (number_of (x + uminus y) :: 'a)"  | 
|
314  | 
unfolding iszero_def number_of_add number_of_minus  | 
|
315  | 
by (simp add: compare_rls)  | 
|
316  | 
||
317  | 
lemma iszero_number_of_Pls:  | 
|
318  | 
"iszero ((number_of Pls)::'a::number_ring)"  | 
|
319  | 
unfolding iszero_def numeral_0_eq_0 ..  | 
|
320  | 
||
321  | 
lemma nonzero_number_of_Min:  | 
|
322  | 
"~ iszero ((number_of Min)::'a::number_ring)"  | 
|
323  | 
unfolding iszero_def numeral_m1_eq_minus_1 by simp  | 
|
324  | 
||
325  | 
||
326  | 
subsection {* Comparisons, for Ordered Rings *}
 | 
|
327  | 
||
| 25089 | 328  | 
lemmas double_eq_0_iff = double_zero  | 
| 23164 | 329  | 
|
330  | 
lemma le_imp_0_less:  | 
|
331  | 
assumes le: "0 \<le> z"  | 
|
332  | 
shows "(0::int) < 1 + z"  | 
|
333  | 
proof -  | 
|
| 23389 | 334  | 
have "0 \<le> z" by fact  | 
| 23164 | 335  | 
also have "... < z + 1" by (rule less_add_one)  | 
336  | 
also have "... = 1 + z" by (simp add: add_ac)  | 
|
337  | 
finally show "0 < 1 + z" .  | 
|
338  | 
qed  | 
|
339  | 
||
340  | 
lemma odd_nonzero:  | 
|
341  | 
"1 + z + z \<noteq> (0::int)";  | 
|
342  | 
proof (cases z rule: int_cases)  | 
|
343  | 
case (nonneg n)  | 
|
344  | 
have le: "0 \<le> z+z" by (simp add: nonneg add_increasing)  | 
|
345  | 
thus ?thesis using le_imp_0_less [OF le]  | 
|
346  | 
by (auto simp add: add_assoc)  | 
|
347  | 
next  | 
|
348  | 
case (neg n)  | 
|
349  | 
show ?thesis  | 
|
350  | 
proof  | 
|
351  | 
assume eq: "1 + z + z = 0"  | 
|
352  | 
have "0 < 1 + (int n + int n)"  | 
|
353  | 
by (simp add: le_imp_0_less add_increasing)  | 
|
354  | 
also have "... = - (1 + z + z)"  | 
|
355  | 
by (simp add: neg add_assoc [symmetric])  | 
|
356  | 
also have "... = 0" by (simp add: eq)  | 
|
357  | 
finally have "0<0" ..  | 
|
358  | 
thus False by blast  | 
|
359  | 
qed  | 
|
360  | 
qed  | 
|
361  | 
||
362  | 
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
 | 
|
363  | 
||
364  | 
lemma Ints_double_eq_0_iff:  | 
|
365  | 
assumes in_Ints: "a \<in> Ints"  | 
|
366  | 
shows "(a + a = 0) = (a = (0::'a::ring_char_0))"  | 
|
367  | 
proof -  | 
|
368  | 
from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .  | 
|
369  | 
then obtain z where a: "a = of_int z" ..  | 
|
370  | 
show ?thesis  | 
|
371  | 
proof  | 
|
372  | 
assume "a = 0"  | 
|
373  | 
thus "a + a = 0" by simp  | 
|
374  | 
next  | 
|
375  | 
assume eq: "a + a = 0"  | 
|
376  | 
hence "of_int (z + z) = (of_int 0 :: 'a)" by (simp add: a)  | 
|
377  | 
hence "z + z = 0" by (simp only: of_int_eq_iff)  | 
|
378  | 
hence "z = 0" by (simp only: double_eq_0_iff)  | 
|
379  | 
thus "a = 0" by (simp add: a)  | 
|
380  | 
qed  | 
|
381  | 
qed  | 
|
382  | 
||
383  | 
lemma Ints_odd_nonzero:  | 
|
384  | 
assumes in_Ints: "a \<in> Ints"  | 
|
385  | 
shows "1 + a + a \<noteq> (0::'a::ring_char_0)"  | 
|
386  | 
proof -  | 
|
387  | 
from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .  | 
|
388  | 
then obtain z where a: "a = of_int z" ..  | 
|
389  | 
show ?thesis  | 
|
390  | 
proof  | 
|
391  | 
assume eq: "1 + a + a = 0"  | 
|
392  | 
hence "of_int (1 + z + z) = (of_int 0 :: 'a)" by (simp add: a)  | 
|
393  | 
hence "1 + z + z = 0" by (simp only: of_int_eq_iff)  | 
|
394  | 
with odd_nonzero show False by blast  | 
|
395  | 
qed  | 
|
396  | 
qed  | 
|
397  | 
||
398  | 
lemma Ints_number_of:  | 
|
399  | 
"(number_of w :: 'a::number_ring) \<in> Ints"  | 
|
400  | 
unfolding number_of_eq Ints_def by simp  | 
|
401  | 
||
402  | 
lemma iszero_number_of_BIT:  | 
|
403  | 
"iszero (number_of (w BIT x)::'a) =  | 
|
404  | 
   (x = B0 \<and> iszero (number_of w::'a::{ring_char_0,number_ring}))"
 | 
|
405  | 
by (simp add: iszero_def number_of_eq numeral_simps Ints_double_eq_0_iff  | 
|
406  | 
Ints_odd_nonzero Ints_def split: bit.split)  | 
|
407  | 
||
408  | 
lemma iszero_number_of_0:  | 
|
409  | 
  "iszero (number_of (w BIT B0) :: 'a::{ring_char_0,number_ring}) = 
 | 
|
410  | 
iszero (number_of w :: 'a)"  | 
|
411  | 
by (simp only: iszero_number_of_BIT simp_thms)  | 
|
412  | 
||
413  | 
lemma iszero_number_of_1:  | 
|
414  | 
  "~ iszero (number_of (w BIT B1)::'a::{ring_char_0,number_ring})"
 | 
|
415  | 
by (simp add: iszero_number_of_BIT)  | 
|
416  | 
||
417  | 
||
418  | 
subsection {* The Less-Than Relation *}
 | 
|
419  | 
||
420  | 
lemma less_number_of_eq_neg:  | 
|
421  | 
  "((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
 | 
|
422  | 
= neg (number_of (x + uminus y) :: 'a)"  | 
|
423  | 
apply (subst less_iff_diff_less_0)  | 
|
424  | 
apply (simp add: neg_def diff_minus number_of_add number_of_minus)  | 
|
425  | 
done  | 
|
426  | 
||
427  | 
text {*
 | 
|
428  | 
  If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
 | 
|
429  | 
  @{term Numeral0} IS @{term "number_of Pls"}
 | 
|
430  | 
*}  | 
|
431  | 
||
432  | 
lemma not_neg_number_of_Pls:  | 
|
433  | 
  "~ neg (number_of Pls ::'a::{ordered_idom,number_ring})"
 | 
|
434  | 
by (simp add: neg_def numeral_0_eq_0)  | 
|
435  | 
||
436  | 
lemma neg_number_of_Min:  | 
|
437  | 
  "neg (number_of Min ::'a::{ordered_idom,number_ring})"
 | 
|
438  | 
by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)  | 
|
439  | 
||
440  | 
lemma double_less_0_iff:  | 
|
441  | 
"(a + a < 0) = (a < (0::'a::ordered_idom))"  | 
|
442  | 
proof -  | 
|
443  | 
have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)  | 
|
444  | 
also have "... = (a < 0)"  | 
|
445  | 
by (simp add: mult_less_0_iff zero_less_two  | 
|
446  | 
order_less_not_sym [OF zero_less_two])  | 
|
447  | 
finally show ?thesis .  | 
|
448  | 
qed  | 
|
449  | 
||
450  | 
lemma odd_less_0:  | 
|
451  | 
"(1 + z + z < 0) = (z < (0::int))";  | 
|
| 23365 | 452  | 
proof (cases z rule: int_cases)  | 
| 23164 | 453  | 
case (nonneg n)  | 
454  | 
thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing  | 
|
455  | 
le_imp_0_less [THEN order_less_imp_le])  | 
|
456  | 
next  | 
|
457  | 
case (neg n)  | 
|
| 
23307
 
2fe3345035c7
modify proofs to avoid referring to int::nat=>int
 
huffman 
parents: 
23164 
diff
changeset
 | 
458  | 
thus ?thesis by (simp del: of_nat_Suc of_nat_add  | 
| 
 
2fe3345035c7
modify proofs to avoid referring to int::nat=>int
 
huffman 
parents: 
23164 
diff
changeset
 | 
459  | 
add: compare_rls of_nat_1 [symmetric] of_nat_add [symmetric])  | 
| 23164 | 460  | 
qed  | 
461  | 
||
462  | 
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
 | 
|
463  | 
||
464  | 
lemma Ints_odd_less_0:  | 
|
465  | 
assumes in_Ints: "a \<in> Ints"  | 
|
466  | 
shows "(1 + a + a < 0) = (a < (0::'a::ordered_idom))";  | 
|
467  | 
proof -  | 
|
468  | 
from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] .  | 
|
469  | 
then obtain z where a: "a = of_int z" ..  | 
|
470  | 
hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))"  | 
|
471  | 
by (simp add: a)  | 
|
472  | 
also have "... = (z < 0)" by (simp only: of_int_less_iff odd_less_0)  | 
|
473  | 
also have "... = (a < 0)" by (simp add: a)  | 
|
474  | 
finally show ?thesis .  | 
|
475  | 
qed  | 
|
476  | 
||
477  | 
lemma neg_number_of_BIT:  | 
|
478  | 
"neg (number_of (w BIT x)::'a) =  | 
|
479  | 
  neg (number_of w :: 'a::{ordered_idom,number_ring})"
 | 
|
480  | 
by (simp add: neg_def number_of_eq numeral_simps double_less_0_iff  | 
|
481  | 
Ints_odd_less_0 Ints_def split: bit.split)  | 
|
482  | 
||
483  | 
||
484  | 
text {* Less-Than or Equals *}
 | 
|
485  | 
||
486  | 
text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
 | 
|
487  | 
||
488  | 
lemmas le_number_of_eq_not_less =  | 
|
489  | 
linorder_not_less [of "number_of w" "number_of v", symmetric,  | 
|
490  | 
standard]  | 
|
491  | 
||
492  | 
lemma le_number_of_eq:  | 
|
493  | 
    "((number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y)
 | 
|
494  | 
= (~ (neg (number_of (y + uminus x) :: 'a)))"  | 
|
495  | 
by (simp add: le_number_of_eq_not_less less_number_of_eq_neg)  | 
|
496  | 
||
497  | 
||
498  | 
text {* Absolute value (@{term abs}) *}
 | 
|
499  | 
||
500  | 
lemma abs_number_of:  | 
|
501  | 
  "abs(number_of x::'a::{ordered_idom,number_ring}) =
 | 
|
502  | 
(if number_of x < (0::'a) then -number_of x else number_of x)"  | 
|
503  | 
by (simp add: abs_if)  | 
|
504  | 
||
505  | 
||
506  | 
text {* Re-orientation of the equation nnn=x *}
 | 
|
507  | 
||
508  | 
lemma number_of_reorient:  | 
|
509  | 
"(number_of w = x) = (x = number_of w)"  | 
|
510  | 
by auto  | 
|
511  | 
||
512  | 
||
513  | 
subsection {* Simplification of arithmetic operations on integer constants. *}
 | 
|
514  | 
||
515  | 
lemmas arith_extra_simps [standard, simp] =  | 
|
516  | 
number_of_add [symmetric]  | 
|
517  | 
number_of_minus [symmetric] numeral_m1_eq_minus_1 [symmetric]  | 
|
518  | 
number_of_mult [symmetric]  | 
|
519  | 
diff_number_of_eq abs_number_of  | 
|
520  | 
||
521  | 
text {*
 | 
|
522  | 
For making a minimal simpset, one must include these default simprules.  | 
|
523  | 
  Also include @{text simp_thms}.
 | 
|
524  | 
*}  | 
|
525  | 
||
526  | 
lemmas arith_simps =  | 
|
527  | 
bit.distinct  | 
|
528  | 
Pls_0_eq Min_1_eq  | 
|
529  | 
pred_Pls pred_Min pred_1 pred_0  | 
|
530  | 
succ_Pls succ_Min succ_1 succ_0  | 
|
531  | 
add_Pls add_Min add_BIT_0 add_BIT_10 add_BIT_11  | 
|
532  | 
minus_Pls minus_Min minus_1 minus_0  | 
|
533  | 
mult_Pls mult_Min mult_num1 mult_num0  | 
|
534  | 
add_Pls_right add_Min_right  | 
|
535  | 
abs_zero abs_one arith_extra_simps  | 
|
536  | 
||
537  | 
text {* Simplification of relational operations *}
 | 
|
538  | 
||
539  | 
lemmas rel_simps [simp] =  | 
|
| 
24392
 
000327cea3eb
replace iszero_number_of_Pls with iszero_0 in rel_simps
 
huffman 
parents: 
24166 
diff
changeset
 | 
540  | 
eq_number_of_eq iszero_0 nonzero_number_of_Min  | 
| 23164 | 541  | 
iszero_number_of_0 iszero_number_of_1  | 
542  | 
less_number_of_eq_neg  | 
|
543  | 
not_neg_number_of_Pls not_neg_0 not_neg_1 not_iszero_1  | 
|
544  | 
neg_number_of_Min neg_number_of_BIT  | 
|
545  | 
le_number_of_eq  | 
|
| 
24392
 
000327cea3eb
replace iszero_number_of_Pls with iszero_0 in rel_simps
 
huffman 
parents: 
24166 
diff
changeset
 | 
546  | 
(* iszero_number_of_Pls would never be used  | 
| 
 
000327cea3eb
replace iszero_number_of_Pls with iszero_0 in rel_simps
 
huffman 
parents: 
24166 
diff
changeset
 | 
547  | 
because its lhs simplifies to "iszero 0" *)  | 
| 23164 | 548  | 
|
549  | 
||
550  | 
subsection {* Simplification of arithmetic when nested to the right. *}
 | 
|
551  | 
||
552  | 
lemma add_number_of_left [simp]:  | 
|
553  | 
"number_of v + (number_of w + z) =  | 
|
554  | 
(number_of(v + w) + z::'a::number_ring)"  | 
|
555  | 
by (simp add: add_assoc [symmetric])  | 
|
556  | 
||
557  | 
lemma mult_number_of_left [simp]:  | 
|
558  | 
"number_of v * (number_of w * z) =  | 
|
559  | 
(number_of(v * w) * z::'a::number_ring)"  | 
|
560  | 
by (simp add: mult_assoc [symmetric])  | 
|
561  | 
||
562  | 
lemma add_number_of_diff1:  | 
|
563  | 
"number_of v + (number_of w - c) =  | 
|
564  | 
number_of(v + w) - (c::'a::number_ring)"  | 
|
565  | 
by (simp add: diff_minus add_number_of_left)  | 
|
566  | 
||
567  | 
lemma add_number_of_diff2 [simp]:  | 
|
568  | 
"number_of v + (c - number_of w) =  | 
|
569  | 
number_of (v + uminus w) + (c::'a::number_ring)"  | 
|
570  | 
apply (subst diff_number_of_eq [symmetric])  | 
|
571  | 
apply (simp only: compare_rls)  | 
|
572  | 
done  | 
|
573  | 
||
574  | 
||
575  | 
subsection {* Configuration of the code generator *}
 | 
|
576  | 
||
577  | 
instance int :: eq ..  | 
|
578  | 
||
579  | 
code_datatype Pls Min Bit "number_of \<Colon> int \<Rightarrow> int"  | 
|
580  | 
||
581  | 
definition  | 
|
| 23855 | 582  | 
int_aux :: "nat \<Rightarrow> int \<Rightarrow> int" where  | 
583  | 
"int_aux n i = int n + i"  | 
|
| 23164 | 584  | 
|
585  | 
lemma [code]:  | 
|
| 23855 | 586  | 
"int_aux 0 i = i"  | 
587  | 
  "int_aux (Suc n) i = int_aux n (i + 1)" -- {* tail recursive *}
 | 
|
| 23164 | 588  | 
by (simp add: int_aux_def)+  | 
589  | 
||
| 25145 | 590  | 
lemma [code, code unfold, code inline del]:  | 
| 23855 | 591  | 
"int n = int_aux n 0"  | 
| 23164 | 592  | 
by (simp add: int_aux_def)  | 
593  | 
||
594  | 
definition  | 
|
| 23855 | 595  | 
nat_aux :: "int \<Rightarrow> nat \<Rightarrow> nat" where  | 
596  | 
"nat_aux i n = nat i + n"  | 
|
| 23164 | 597  | 
|
| 23855 | 598  | 
lemma [code]:  | 
599  | 
  "nat_aux i n = (if i \<le> 0 then n else nat_aux (i - 1) (Suc n))"  -- {* tail recursive *}
 | 
|
| 23164 | 600  | 
by (auto simp add: nat_aux_def nat_eq_iff linorder_not_le order_less_imp_le  | 
601  | 
dest: zless_imp_add1_zle)  | 
|
602  | 
||
| 23855 | 603  | 
lemma [code]: "nat i = nat_aux i 0"  | 
| 23164 | 604  | 
by (simp add: nat_aux_def)  | 
605  | 
||
| 24166 | 606  | 
lemma zero_is_num_zero [code func, code inline, symmetric, code post]:  | 
| 23855 | 607  | 
"(0\<Colon>int) = Numeral0"  | 
| 23164 | 608  | 
by simp  | 
609  | 
||
| 24166 | 610  | 
lemma one_is_num_one [code func, code inline, symmetric, code post]:  | 
| 23855 | 611  | 
"(1\<Colon>int) = Numeral1"  | 
| 23164 | 612  | 
by simp  | 
613  | 
||
614  | 
code_modulename SML  | 
|
615  | 
IntDef Integer  | 
|
616  | 
||
617  | 
code_modulename OCaml  | 
|
618  | 
IntDef Integer  | 
|
619  | 
||
620  | 
code_modulename Haskell  | 
|
621  | 
IntDef Integer  | 
|
622  | 
||
623  | 
code_modulename SML  | 
|
624  | 
Numeral Integer  | 
|
625  | 
||
626  | 
code_modulename OCaml  | 
|
627  | 
Numeral Integer  | 
|
628  | 
||
629  | 
code_modulename Haskell  | 
|
630  | 
Numeral Integer  | 
|
631  | 
||
632  | 
types_code  | 
|
633  | 
  "int" ("int")
 | 
|
634  | 
attach (term_of) {*
 | 
|
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24541 
diff
changeset
 | 
635  | 
val term_of_int = HOLogic.mk_number HOLogic.intT;  | 
| 23164 | 636  | 
*}  | 
637  | 
attach (test) {*
 | 
|
638  | 
fun gen_int i = one_of [~1, 1] * random_range 0 i;  | 
|
639  | 
*}  | 
|
640  | 
||
641  | 
setup {*
 | 
|
642  | 
let  | 
|
643  | 
||
| 24541 | 644  | 
fun strip_number_of (@{term "Numeral.number_of :: int => int"} $ t) = t
 | 
645  | 
| strip_number_of t = t;  | 
|
646  | 
||
647  | 
fun numeral_codegen thy defs gr dep module b t =  | 
|
648  | 
let val i = HOLogic.dest_numeral (strip_number_of t)  | 
|
649  | 
in  | 
|
650  | 
SOME (fst (Codegen.invoke_tycodegen thy defs dep module false (gr, HOLogic.intT)),  | 
|
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24541 
diff
changeset
 | 
651  | 
Pretty.str (string_of_int i))  | 
| 24541 | 652  | 
end handle TERM _ => NONE;  | 
| 23164 | 653  | 
|
654  | 
in  | 
|
655  | 
||
| 24541 | 656  | 
Codegen.add_codegen "numeral_codegen" numeral_codegen  | 
| 23164 | 657  | 
|
658  | 
end  | 
|
659  | 
*}  | 
|
660  | 
||
661  | 
consts_code  | 
|
| 24541 | 662  | 
  "number_of :: int \<Rightarrow> int"    ("(_)")
 | 
| 23164 | 663  | 
  "0 :: int"                   ("0")
 | 
664  | 
  "1 :: int"                   ("1")
 | 
|
665  | 
  "uminus :: int => int"       ("~")
 | 
|
666  | 
  "op + :: int => int => int"  ("(_ +/ _)")
 | 
|
667  | 
  "op * :: int => int => int"  ("(_ */ _)")
 | 
|
668  | 
  "op \<le> :: int => int => bool" ("(_ <=/ _)")
 | 
|
669  | 
  "op < :: int => int => bool" ("(_ </ _)")
 | 
|
670  | 
||
671  | 
quickcheck_params [default_type = int]  | 
|
672  | 
||
673  | 
(*setup continues in theory Presburger*)  | 
|
674  | 
||
675  | 
hide (open) const Pls Min B0 B1 succ pred  | 
|
676  | 
||
677  | 
end  |