| author | haftmann | 
| Wed, 01 May 2019 10:40:42 +0000 | |
| changeset 70226 | accbd801fefa | 
| parent 69593 | 3dda49e08b9d | 
| child 70338 | c832d431636b | 
| permissions | -rw-r--r-- | 
| 41959 | 1  | 
(* Title: HOL/Parity.thy  | 
2  | 
Author: Jeremy Avigad  | 
|
3  | 
Author: Jacques D. Fleuriot  | 
|
| 21256 | 4  | 
*)  | 
5  | 
||
| 60758 | 6  | 
section \<open>Parity in rings and semirings\<close>  | 
| 21256 | 7  | 
|
8  | 
theory Parity  | 
|
| 66815 | 9  | 
imports Euclidean_Division  | 
| 21256 | 10  | 
begin  | 
11  | 
||
| 61799 | 12  | 
subsection \<open>Ring structures with parity and \<open>even\<close>/\<open>odd\<close> predicates\<close>  | 
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
13  | 
|
| 67905 | 14  | 
class semiring_parity = semidom + semiring_char_0 + unique_euclidean_semiring +  | 
| 66815 | 15  | 
assumes of_nat_div: "of_nat (m div n) = of_nat m div of_nat n"  | 
| 66839 | 16  | 
and division_segment_of_nat [simp]: "division_segment (of_nat n) = 1"  | 
17  | 
and division_segment_euclidean_size [simp]: "division_segment a * of_nat (euclidean_size a) = a"  | 
|
18  | 
begin  | 
|
19  | 
||
20  | 
lemma division_segment_eq_iff:  | 
|
21  | 
"a = b" if "division_segment a = division_segment b"  | 
|
22  | 
and "euclidean_size a = euclidean_size b"  | 
|
23  | 
using that division_segment_euclidean_size [of a] by simp  | 
|
24  | 
||
25  | 
lemma euclidean_size_of_nat [simp]:  | 
|
26  | 
"euclidean_size (of_nat n) = n"  | 
|
27  | 
proof -  | 
|
28  | 
have "division_segment (of_nat n) * of_nat (euclidean_size (of_nat n)) = of_nat n"  | 
|
29  | 
by (fact division_segment_euclidean_size)  | 
|
30  | 
then show ?thesis by simp  | 
|
31  | 
qed  | 
|
| 66815 | 32  | 
|
| 66839 | 33  | 
lemma of_nat_euclidean_size:  | 
34  | 
"of_nat (euclidean_size a) = a div division_segment a"  | 
|
35  | 
proof -  | 
|
36  | 
have "of_nat (euclidean_size a) = division_segment a * of_nat (euclidean_size a) div division_segment a"  | 
|
37  | 
by (subst nonzero_mult_div_cancel_left) simp_all  | 
|
38  | 
also have "\<dots> = a div division_segment a"  | 
|
39  | 
by simp  | 
|
40  | 
finally show ?thesis .  | 
|
41  | 
qed  | 
|
42  | 
||
43  | 
lemma division_segment_1 [simp]:  | 
|
44  | 
"division_segment 1 = 1"  | 
|
45  | 
using division_segment_of_nat [of 1] by simp  | 
|
46  | 
||
47  | 
lemma division_segment_numeral [simp]:  | 
|
48  | 
"division_segment (numeral k) = 1"  | 
|
49  | 
using division_segment_of_nat [of "numeral k"] by simp  | 
|
50  | 
||
51  | 
lemma euclidean_size_1 [simp]:  | 
|
52  | 
"euclidean_size 1 = 1"  | 
|
53  | 
using euclidean_size_of_nat [of 1] by simp  | 
|
54  | 
||
55  | 
lemma euclidean_size_numeral [simp]:  | 
|
56  | 
"euclidean_size (numeral k) = numeral k"  | 
|
57  | 
using euclidean_size_of_nat [of "numeral k"] by simp  | 
|
| 21256 | 58  | 
|
| 66815 | 59  | 
lemma of_nat_dvd_iff:  | 
60  | 
"of_nat m dvd of_nat n \<longleftrightarrow> m dvd n" (is "?P \<longleftrightarrow> ?Q")  | 
|
61  | 
proof (cases "m = 0")  | 
|
62  | 
case True  | 
|
63  | 
then show ?thesis  | 
|
64  | 
by simp  | 
|
65  | 
next  | 
|
66  | 
case False  | 
|
67  | 
show ?thesis  | 
|
68  | 
proof  | 
|
69  | 
assume ?Q  | 
|
70  | 
then show ?P  | 
|
71  | 
by (auto elim: dvd_class.dvdE)  | 
|
72  | 
next  | 
|
73  | 
assume ?P  | 
|
74  | 
with False have "of_nat n = of_nat n div of_nat m * of_nat m"  | 
|
75  | 
by simp  | 
|
76  | 
then have "of_nat n = of_nat (n div m * m)"  | 
|
77  | 
by (simp add: of_nat_div)  | 
|
78  | 
then have "n = n div m * m"  | 
|
79  | 
by (simp only: of_nat_eq_iff)  | 
|
80  | 
then have "n = m * (n div m)"  | 
|
81  | 
by (simp add: ac_simps)  | 
|
82  | 
then show ?Q ..  | 
|
83  | 
qed  | 
|
84  | 
qed  | 
|
85  | 
||
86  | 
lemma of_nat_mod:  | 
|
87  | 
"of_nat (m mod n) = of_nat m mod of_nat n"  | 
|
88  | 
proof -  | 
|
89  | 
have "of_nat m div of_nat n * of_nat n + of_nat m mod of_nat n = of_nat m"  | 
|
90  | 
by (simp add: div_mult_mod_eq)  | 
|
91  | 
also have "of_nat m = of_nat (m div n * n + m mod n)"  | 
|
92  | 
by simp  | 
|
93  | 
finally show ?thesis  | 
|
94  | 
by (simp only: of_nat_div of_nat_mult of_nat_add) simp  | 
|
95  | 
qed  | 
|
96  | 
||
97  | 
lemma one_div_two_eq_zero [simp]:  | 
|
98  | 
"1 div 2 = 0"  | 
|
99  | 
proof -  | 
|
100  | 
from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0"  | 
|
101  | 
by (simp only:) simp  | 
|
102  | 
then show ?thesis  | 
|
103  | 
by simp  | 
|
104  | 
qed  | 
|
105  | 
||
106  | 
lemma one_mod_two_eq_one [simp]:  | 
|
107  | 
"1 mod 2 = 1"  | 
|
108  | 
proof -  | 
|
109  | 
from of_nat_mod [symmetric] have "of_nat 1 mod of_nat 2 = of_nat 1"  | 
|
110  | 
by (simp only:) simp  | 
|
111  | 
then show ?thesis  | 
|
112  | 
by simp  | 
|
113  | 
qed  | 
|
| 
59816
 
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
 
haftmann 
parents: 
58889 
diff
changeset
 | 
114  | 
|
| 58740 | 115  | 
abbreviation even :: "'a \<Rightarrow> bool"  | 
| 63654 | 116  | 
where "even a \<equiv> 2 dvd a"  | 
| 54228 | 117  | 
|
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
118  | 
abbreviation odd :: "'a \<Rightarrow> bool"  | 
| 63654 | 119  | 
where "odd a \<equiv> \<not> 2 dvd a"  | 
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
120  | 
|
| 66815 | 121  | 
lemma even_iff_mod_2_eq_zero:  | 
122  | 
"even a \<longleftrightarrow> a mod 2 = 0"  | 
|
123  | 
by (fact dvd_eq_mod_eq_0)  | 
|
124  | 
||
125  | 
lemma odd_iff_mod_2_eq_one:  | 
|
126  | 
"odd a \<longleftrightarrow> a mod 2 = 1"  | 
|
| 66839 | 127  | 
proof  | 
128  | 
assume "a mod 2 = 1"  | 
|
129  | 
then show "odd a"  | 
|
130  | 
by auto  | 
|
131  | 
next  | 
|
132  | 
assume "odd a"  | 
|
133  | 
have eucl: "euclidean_size (a mod 2) = 1"  | 
|
134  | 
proof (rule order_antisym)  | 
|
135  | 
show "euclidean_size (a mod 2) \<le> 1"  | 
|
136  | 
using mod_size_less [of 2 a] by simp  | 
|
137  | 
show "1 \<le> euclidean_size (a mod 2)"  | 
|
| 66840 | 138  | 
using \<open>odd a\<close> by (simp add: Suc_le_eq dvd_eq_mod_eq_0)  | 
| 66839 | 139  | 
qed  | 
140  | 
from \<open>odd a\<close> have "\<not> of_nat 2 dvd division_segment a * of_nat (euclidean_size a)"  | 
|
141  | 
by simp  | 
|
142  | 
then have "\<not> of_nat 2 dvd of_nat (euclidean_size a)"  | 
|
143  | 
by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment)  | 
|
144  | 
then have "\<not> 2 dvd euclidean_size a"  | 
|
145  | 
using of_nat_dvd_iff [of 2] by simp  | 
|
146  | 
then have "euclidean_size a mod 2 = 1"  | 
|
147  | 
by (simp add: semidom_modulo_class.dvd_eq_mod_eq_0)  | 
|
148  | 
then have "of_nat (euclidean_size a mod 2) = of_nat 1"  | 
|
149  | 
by simp  | 
|
150  | 
then have "of_nat (euclidean_size a) mod 2 = 1"  | 
|
151  | 
by (simp add: of_nat_mod)  | 
|
152  | 
from \<open>odd a\<close> eucl  | 
|
153  | 
show "a mod 2 = 1"  | 
|
154  | 
by (auto intro: division_segment_eq_iff simp add: division_segment_mod)  | 
|
155  | 
qed  | 
|
| 58787 | 156  | 
|
| 69198 | 157  | 
lemma mod2_eq_if: "x mod 2 = (if even x then 0 else 1)"  | 
158  | 
by (simp add: odd_iff_mod_2_eq_one)  | 
|
159  | 
||
| 66815 | 160  | 
lemma parity_cases [case_names even odd]:  | 
161  | 
assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P"  | 
|
162  | 
assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P"  | 
|
163  | 
shows P  | 
|
164  | 
using assms by (cases "even a") (simp_all add: odd_iff_mod_2_eq_one)  | 
|
165  | 
||
166  | 
lemma not_mod_2_eq_1_eq_0 [simp]:  | 
|
167  | 
"a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"  | 
|
168  | 
by (cases a rule: parity_cases) simp_all  | 
|
169  | 
||
170  | 
lemma not_mod_2_eq_0_eq_1 [simp]:  | 
|
171  | 
"a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"  | 
|
172  | 
by (cases a rule: parity_cases) simp_all  | 
|
| 58787 | 173  | 
|
| 58690 | 174  | 
lemma evenE [elim?]:  | 
175  | 
assumes "even a"  | 
|
176  | 
obtains b where "a = 2 * b"  | 
|
| 58740 | 177  | 
using assms by (rule dvdE)  | 
| 58690 | 178  | 
|
| 58681 | 179  | 
lemma oddE [elim?]:  | 
| 58680 | 180  | 
assumes "odd a"  | 
181  | 
obtains b where "a = 2 * b + 1"  | 
|
| 58787 | 182  | 
proof -  | 
| 66815 | 183  | 
have "a = 2 * (a div 2) + a mod 2"  | 
184  | 
by (simp add: mult_div_mod_eq)  | 
|
185  | 
with assms have "a = 2 * (a div 2) + 1"  | 
|
186  | 
by (simp add: odd_iff_mod_2_eq_one)  | 
|
187  | 
then show ?thesis ..  | 
|
188  | 
qed  | 
|
189  | 
||
190  | 
lemma mod_2_eq_odd:  | 
|
191  | 
"a mod 2 = of_bool (odd a)"  | 
|
192  | 
by (auto elim: oddE)  | 
|
193  | 
||
| 67816 | 194  | 
lemma of_bool_odd_eq_mod_2:  | 
195  | 
"of_bool (odd a) = a mod 2"  | 
|
196  | 
by (simp add: mod_2_eq_odd)  | 
|
197  | 
||
| 66815 | 198  | 
lemma one_mod_2_pow_eq [simp]:  | 
199  | 
"1 mod (2 ^ n) = of_bool (n > 0)"  | 
|
200  | 
proof -  | 
|
| 67083 | 201  | 
have "1 mod (2 ^ n) = of_nat (1 mod (2 ^ n))"  | 
202  | 
using of_nat_mod [of 1 "2 ^ n"] by simp  | 
|
203  | 
also have "\<dots> = of_bool (n > 0)"  | 
|
| 66815 | 204  | 
by simp  | 
| 67083 | 205  | 
finally show ?thesis .  | 
| 66815 | 206  | 
qed  | 
207  | 
||
| 67816 | 208  | 
lemma one_div_2_pow_eq [simp]:  | 
209  | 
"1 div (2 ^ n) = of_bool (n = 0)"  | 
|
210  | 
using div_mult_mod_eq [of 1 "2 ^ n"] by auto  | 
|
211  | 
||
| 66815 | 212  | 
lemma even_of_nat [simp]:  | 
213  | 
"even (of_nat a) \<longleftrightarrow> even a"  | 
|
214  | 
proof -  | 
|
215  | 
have "even (of_nat a) \<longleftrightarrow> of_nat 2 dvd of_nat a"  | 
|
216  | 
by simp  | 
|
217  | 
also have "\<dots> \<longleftrightarrow> even a"  | 
|
218  | 
by (simp only: of_nat_dvd_iff)  | 
|
219  | 
finally show ?thesis .  | 
|
220  | 
qed  | 
|
221  | 
||
222  | 
lemma even_zero [simp]:  | 
|
223  | 
"even 0"  | 
|
224  | 
by (fact dvd_0_right)  | 
|
225  | 
||
226  | 
lemma odd_one [simp]:  | 
|
227  | 
"odd 1"  | 
|
228  | 
proof -  | 
|
229  | 
have "\<not> (2 :: nat) dvd 1"  | 
|
230  | 
by simp  | 
|
231  | 
then have "\<not> of_nat 2 dvd of_nat 1"  | 
|
232  | 
unfolding of_nat_dvd_iff by simp  | 
|
233  | 
then show ?thesis  | 
|
234  | 
by simp  | 
|
| 58787 | 235  | 
qed  | 
| 63654 | 236  | 
|
| 66815 | 237  | 
lemma odd_even_add:  | 
238  | 
"even (a + b)" if "odd a" and "odd b"  | 
|
239  | 
proof -  | 
|
240  | 
from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1"  | 
|
241  | 
by (blast elim: oddE)  | 
|
242  | 
then have "a + b = 2 * c + 2 * d + (1 + 1)"  | 
|
243  | 
by (simp only: ac_simps)  | 
|
244  | 
also have "\<dots> = 2 * (c + d + 1)"  | 
|
245  | 
by (simp add: algebra_simps)  | 
|
246  | 
finally show ?thesis ..  | 
|
247  | 
qed  | 
|
248  | 
||
249  | 
lemma even_add [simp]:  | 
|
250  | 
"even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"  | 
|
251  | 
by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add)  | 
|
252  | 
||
253  | 
lemma odd_add [simp]:  | 
|
254  | 
"odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)"  | 
|
255  | 
by simp  | 
|
256  | 
||
257  | 
lemma even_plus_one_iff [simp]:  | 
|
258  | 
"even (a + 1) \<longleftrightarrow> odd a"  | 
|
259  | 
by (auto simp add: dvd_add_right_iff intro: odd_even_add)  | 
|
260  | 
||
261  | 
lemma even_mult_iff [simp]:  | 
|
262  | 
"even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q")  | 
|
263  | 
proof  | 
|
264  | 
assume ?Q  | 
|
265  | 
then show ?P  | 
|
266  | 
by auto  | 
|
267  | 
next  | 
|
268  | 
assume ?P  | 
|
269  | 
show ?Q  | 
|
270  | 
proof (rule ccontr)  | 
|
271  | 
assume "\<not> (even a \<or> even b)"  | 
|
272  | 
then have "odd a" and "odd b"  | 
|
273  | 
by auto  | 
|
274  | 
then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1"  | 
|
275  | 
by (blast elim: oddE)  | 
|
276  | 
then have "a * b = (2 * r + 1) * (2 * s + 1)"  | 
|
277  | 
by simp  | 
|
278  | 
also have "\<dots> = 2 * (2 * r * s + r + s) + 1"  | 
|
279  | 
by (simp add: algebra_simps)  | 
|
280  | 
finally have "odd (a * b)"  | 
|
281  | 
by simp  | 
|
282  | 
with \<open>?P\<close> show False  | 
|
283  | 
by auto  | 
|
284  | 
qed  | 
|
285  | 
qed  | 
|
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
286  | 
|
| 63654 | 287  | 
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"  | 
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
288  | 
proof -  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
289  | 
have "even (2 * numeral n)"  | 
| 66815 | 290  | 
unfolding even_mult_iff by simp  | 
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
291  | 
then have "even (numeral n + numeral n)"  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
292  | 
unfolding mult_2 .  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
293  | 
then show ?thesis  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
294  | 
unfolding numeral.simps .  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
295  | 
qed  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
296  | 
|
| 63654 | 297  | 
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"  | 
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
298  | 
proof  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
299  | 
assume "even (numeral (num.Bit1 n))"  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
300  | 
then have "even (numeral n + numeral n + 1)"  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
301  | 
unfolding numeral.simps .  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
302  | 
then have "even (2 * numeral n + 1)"  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
303  | 
unfolding mult_2 .  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
304  | 
then have "2 dvd numeral n * 2 + 1"  | 
| 58740 | 305  | 
by (simp add: ac_simps)  | 
| 63654 | 306  | 
then have "2 dvd 1"  | 
307  | 
using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp  | 
|
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
308  | 
then show False by simp  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
309  | 
qed  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
310  | 
|
| 63654 | 311  | 
lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"  | 
| 58680 | 312  | 
by (induct n) auto  | 
313  | 
||
| 66815 | 314  | 
lemma even_succ_div_two [simp]:  | 
315  | 
"even a \<Longrightarrow> (a + 1) div 2 = a div 2"  | 
|
316  | 
by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)  | 
|
317  | 
||
318  | 
lemma odd_succ_div_two [simp]:  | 
|
319  | 
"odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"  | 
|
320  | 
by (auto elim!: oddE simp add: add.assoc)  | 
|
321  | 
||
322  | 
lemma even_two_times_div_two:  | 
|
323  | 
"even a \<Longrightarrow> 2 * (a div 2) = a"  | 
|
324  | 
by (fact dvd_mult_div_cancel)  | 
|
325  | 
||
326  | 
lemma odd_two_times_div_two_succ [simp]:  | 
|
327  | 
"odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"  | 
|
328  | 
using mult_div_mod_eq [of 2 a]  | 
|
329  | 
by (simp add: even_iff_mod_2_eq_zero)  | 
|
330  | 
||
| 67051 | 331  | 
lemma coprime_left_2_iff_odd [simp]:  | 
332  | 
"coprime 2 a \<longleftrightarrow> odd a"  | 
|
333  | 
proof  | 
|
334  | 
assume "odd a"  | 
|
335  | 
show "coprime 2 a"  | 
|
336  | 
proof (rule coprimeI)  | 
|
337  | 
fix b  | 
|
338  | 
assume "b dvd 2" "b dvd a"  | 
|
339  | 
then have "b dvd a mod 2"  | 
|
340  | 
by (auto intro: dvd_mod)  | 
|
341  | 
with \<open>odd a\<close> show "is_unit b"  | 
|
342  | 
by (simp add: mod_2_eq_odd)  | 
|
343  | 
qed  | 
|
344  | 
next  | 
|
345  | 
assume "coprime 2 a"  | 
|
346  | 
show "odd a"  | 
|
347  | 
proof (rule notI)  | 
|
348  | 
assume "even a"  | 
|
349  | 
then obtain b where "a = 2 * b" ..  | 
|
350  | 
with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)"  | 
|
351  | 
by simp  | 
|
352  | 
moreover have "\<not> coprime 2 (2 * b)"  | 
|
353  | 
by (rule not_coprimeI [of 2]) simp_all  | 
|
354  | 
ultimately show False  | 
|
355  | 
by blast  | 
|
356  | 
qed  | 
|
357  | 
qed  | 
|
358  | 
||
359  | 
lemma coprime_right_2_iff_odd [simp]:  | 
|
360  | 
"coprime a 2 \<longleftrightarrow> odd a"  | 
|
361  | 
using coprime_left_2_iff_odd [of a] by (simp add: ac_simps)  | 
|
362  | 
||
| 67828 | 363  | 
lemma div_mult2_eq':  | 
364  | 
"a div (of_nat m * of_nat n) = a div of_nat m div of_nat n"  | 
|
365  | 
proof (cases a "of_nat m * of_nat n" rule: divmod_cases)  | 
|
366  | 
case (divides q)  | 
|
367  | 
then show ?thesis  | 
|
368  | 
using nonzero_mult_div_cancel_right [of "of_nat m" "q * of_nat n"]  | 
|
369  | 
by (simp add: ac_simps)  | 
|
370  | 
next  | 
|
371  | 
case (remainder q r)  | 
|
372  | 
then have "division_segment r = 1"  | 
|
373  | 
using division_segment_of_nat [of "m * n"] by simp  | 
|
374  | 
with division_segment_euclidean_size [of r]  | 
|
375  | 
have "of_nat (euclidean_size r) = r"  | 
|
376  | 
by simp  | 
|
| 67908 | 377  | 
have "a mod (of_nat m * of_nat n) div (of_nat m * of_nat n) = 0"  | 
378  | 
by simp  | 
|
379  | 
with remainder(6) have "r div (of_nat m * of_nat n) = 0"  | 
|
| 67828 | 380  | 
by simp  | 
| 67908 | 381  | 
with \<open>of_nat (euclidean_size r) = r\<close>  | 
382  | 
have "of_nat (euclidean_size r) div (of_nat m * of_nat n) = 0"  | 
|
383  | 
by simp  | 
|
384  | 
then have "of_nat (euclidean_size r div (m * n)) = 0"  | 
|
| 67828 | 385  | 
by (simp add: of_nat_div)  | 
| 67908 | 386  | 
then have "of_nat (euclidean_size r div m div n) = 0"  | 
387  | 
by (simp add: div_mult2_eq)  | 
|
388  | 
with \<open>of_nat (euclidean_size r) = r\<close> have "r div of_nat m div of_nat n = 0"  | 
|
389  | 
by (simp add: of_nat_div)  | 
|
| 67828 | 390  | 
with remainder(1)  | 
391  | 
have "q = (r div of_nat m + q * of_nat n * of_nat m div of_nat m) div of_nat n"  | 
|
392  | 
by simp  | 
|
| 67908 | 393  | 
with remainder(5) remainder(7) show ?thesis  | 
| 67828 | 394  | 
using div_plus_div_distrib_dvd_right [of "of_nat m" "q * (of_nat m * of_nat n)" r]  | 
395  | 
by (simp add: ac_simps)  | 
|
396  | 
next  | 
|
397  | 
case by0  | 
|
398  | 
then show ?thesis  | 
|
399  | 
by auto  | 
|
400  | 
qed  | 
|
401  | 
||
402  | 
lemma mod_mult2_eq':  | 
|
403  | 
"a mod (of_nat m * of_nat n) = of_nat m * (a div of_nat m mod of_nat n) + a mod of_nat m"  | 
|
404  | 
proof -  | 
|
405  | 
have "a div (of_nat m * of_nat n) * (of_nat m * of_nat n) + a mod (of_nat m * of_nat n) = a div of_nat m div of_nat n * of_nat n * of_nat m + (a div of_nat m mod of_nat n * of_nat m + a mod of_nat m)"  | 
|
406  | 
by (simp add: combine_common_factor div_mult_mod_eq)  | 
|
407  | 
moreover have "a div of_nat m div of_nat n * of_nat n * of_nat m = of_nat n * of_nat m * (a div of_nat m div of_nat n)"  | 
|
408  | 
by (simp add: ac_simps)  | 
|
409  | 
ultimately show ?thesis  | 
|
410  | 
by (simp add: div_mult2_eq' mult_commute)  | 
|
411  | 
qed  | 
|
412  | 
||
| 68028 | 413  | 
lemma div_mult2_numeral_eq:  | 
414  | 
"a div numeral k div numeral l = a div numeral (k * l)" (is "?A = ?B")  | 
|
415  | 
proof -  | 
|
416  | 
have "?A = a div of_nat (numeral k) div of_nat (numeral l)"  | 
|
417  | 
by simp  | 
|
418  | 
also have "\<dots> = a div (of_nat (numeral k) * of_nat (numeral l))"  | 
|
419  | 
by (fact div_mult2_eq' [symmetric])  | 
|
420  | 
also have "\<dots> = ?B"  | 
|
421  | 
by simp  | 
|
422  | 
finally show ?thesis .  | 
|
423  | 
qed  | 
|
424  | 
||
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
425  | 
end  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
426  | 
|
| 
59816
 
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
 
haftmann 
parents: 
58889 
diff
changeset
 | 
427  | 
class ring_parity = ring + semiring_parity  | 
| 58679 | 428  | 
begin  | 
429  | 
||
| 
59816
 
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
 
haftmann 
parents: 
58889 
diff
changeset
 | 
430  | 
subclass comm_ring_1 ..  | 
| 
 
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
 
haftmann 
parents: 
58889 
diff
changeset
 | 
431  | 
|
| 67816 | 432  | 
lemma even_minus:  | 
| 66815 | 433  | 
"even (- a) \<longleftrightarrow> even a"  | 
| 58740 | 434  | 
by (fact dvd_minus_iff)  | 
| 58679 | 435  | 
|
| 66815 | 436  | 
lemma even_diff [simp]:  | 
437  | 
"even (a - b) \<longleftrightarrow> even (a + b)"  | 
|
| 58680 | 438  | 
using even_add [of a "- b"] by simp  | 
439  | 
||
| 67906 | 440  | 
lemma minus_1_mod_2_eq [simp]:  | 
441  | 
"- 1 mod 2 = 1"  | 
|
442  | 
by (simp add: mod_2_eq_odd)  | 
|
443  | 
||
444  | 
lemma minus_1_div_2_eq [simp]:  | 
|
445  | 
"- 1 div 2 = - 1"  | 
|
446  | 
proof -  | 
|
447  | 
from div_mult_mod_eq [of "- 1" 2]  | 
|
448  | 
have "- 1 div 2 * 2 = - 1 * 2"  | 
|
449  | 
using local.add_implies_diff by fastforce  | 
|
450  | 
then show ?thesis  | 
|
451  | 
using mult_right_cancel [of 2 "- 1 div 2" "- 1"] by simp  | 
|
452  | 
qed  | 
|
453  | 
||
| 58679 | 454  | 
end  | 
455  | 
||
| 
66808
 
1907167b6038
elementary definition of division on natural numbers
 
haftmann 
parents: 
66582 
diff
changeset
 | 
456  | 
|
| 69593 | 457  | 
subsection \<open>Instance for \<^typ>\<open>nat\<close>\<close>  | 
| 
66808
 
1907167b6038
elementary definition of division on natural numbers
 
haftmann 
parents: 
66582 
diff
changeset
 | 
458  | 
|
| 66815 | 459  | 
instance nat :: semiring_parity  | 
460  | 
by standard (simp_all add: dvd_eq_mod_eq_0)  | 
|
| 
66808
 
1907167b6038
elementary definition of division on natural numbers
 
haftmann 
parents: 
66582 
diff
changeset
 | 
461  | 
|
| 66815 | 462  | 
lemma even_Suc_Suc_iff [simp]:  | 
463  | 
"even (Suc (Suc n)) \<longleftrightarrow> even n"  | 
|
| 58787 | 464  | 
using dvd_add_triv_right_iff [of 2 n] by simp  | 
| 58687 | 465  | 
|
| 66815 | 466  | 
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"  | 
467  | 
using even_plus_one_iff [of n] by simp  | 
|
| 58787 | 468  | 
|
| 66815 | 469  | 
lemma even_diff_nat [simp]:  | 
470  | 
"even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat  | 
|
| 58787 | 471  | 
proof (cases "n \<le> m")  | 
472  | 
case True  | 
|
473  | 
then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)  | 
|
| 66815 | 474  | 
moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp  | 
475  | 
ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)  | 
|
| 58787 | 476  | 
then show ?thesis by auto  | 
477  | 
next  | 
|
478  | 
case False  | 
|
479  | 
then show ?thesis by simp  | 
|
| 63654 | 480  | 
qed  | 
481  | 
||
| 66815 | 482  | 
lemma odd_pos:  | 
483  | 
"odd n \<Longrightarrow> 0 < n" for n :: nat  | 
|
| 58690 | 484  | 
by (auto elim: oddE)  | 
| 
60343
 
063698416239
correct sort constraints for abbreviations in type classes
 
haftmann 
parents: 
59816 
diff
changeset
 | 
485  | 
|
| 66815 | 486  | 
lemma Suc_double_not_eq_double:  | 
487  | 
"Suc (2 * m) \<noteq> 2 * n"  | 
|
| 62597 | 488  | 
proof  | 
489  | 
assume "Suc (2 * m) = 2 * n"  | 
|
490  | 
moreover have "odd (Suc (2 * m))" and "even (2 * n)"  | 
|
491  | 
by simp_all  | 
|
492  | 
ultimately show False by simp  | 
|
493  | 
qed  | 
|
494  | 
||
| 66815 | 495  | 
lemma double_not_eq_Suc_double:  | 
496  | 
"2 * m \<noteq> Suc (2 * n)"  | 
|
| 62597 | 497  | 
using Suc_double_not_eq_double [of n m] by simp  | 
498  | 
||
| 66815 | 499  | 
lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n"  | 
500  | 
by (auto elim: oddE)  | 
|
| 
60343
 
063698416239
correct sort constraints for abbreviations in type classes
 
haftmann 
parents: 
59816 
diff
changeset
 | 
501  | 
|
| 66815 | 502  | 
lemma even_Suc_div_two [simp]:  | 
503  | 
"even n \<Longrightarrow> Suc n div 2 = n div 2"  | 
|
504  | 
using even_succ_div_two [of n] by simp  | 
|
| 
60343
 
063698416239
correct sort constraints for abbreviations in type classes
 
haftmann 
parents: 
59816 
diff
changeset
 | 
505  | 
|
| 66815 | 506  | 
lemma odd_Suc_div_two [simp]:  | 
507  | 
"odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"  | 
|
508  | 
using odd_succ_div_two [of n] by simp  | 
|
| 
60343
 
063698416239
correct sort constraints for abbreviations in type classes
 
haftmann 
parents: 
59816 
diff
changeset
 | 
509  | 
|
| 66815 | 510  | 
lemma odd_two_times_div_two_nat [simp]:  | 
511  | 
assumes "odd n"  | 
|
512  | 
shows "2 * (n div 2) = n - (1 :: nat)"  | 
|
513  | 
proof -  | 
|
514  | 
from assms have "2 * (n div 2) + 1 = n"  | 
|
515  | 
by (rule odd_two_times_div_two_succ)  | 
|
516  | 
then have "Suc (2 * (n div 2)) - 1 = n - 1"  | 
|
| 58787 | 517  | 
by simp  | 
| 66815 | 518  | 
then show ?thesis  | 
519  | 
by simp  | 
|
| 58787 | 520  | 
qed  | 
| 58680 | 521  | 
|
| 70226 | 522  | 
lemma nat_parity_induct [case_names zero even odd]:  | 
523  | 
"P n" if zero: "P 0"  | 
|
524  | 
and even: "\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)"  | 
|
525  | 
and odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"  | 
|
526  | 
proof (induction n rule: less_induct)  | 
|
| 66815 | 527  | 
case (less n)  | 
528  | 
show "P n"  | 
|
529  | 
proof (cases "n = 0")  | 
|
530  | 
case True with zero show ?thesis by simp  | 
|
531  | 
next  | 
|
532  | 
case False  | 
|
533  | 
with less have hyp: "P (n div 2)" by simp  | 
|
534  | 
show ?thesis  | 
|
535  | 
proof (cases "even n")  | 
|
536  | 
case True  | 
|
| 70226 | 537  | 
then have "n \<noteq> 1"  | 
538  | 
by auto  | 
|
539  | 
with \<open>n \<noteq> 0\<close> have "n div 2 > 0"  | 
|
540  | 
by simp  | 
|
541  | 
with \<open>even n\<close> hyp even [of "n div 2"] show ?thesis  | 
|
| 66815 | 542  | 
by simp  | 
543  | 
next  | 
|
544  | 
case False  | 
|
545  | 
with hyp odd [of "n div 2"] show ?thesis  | 
|
546  | 
by simp  | 
|
547  | 
qed  | 
|
548  | 
qed  | 
|
549  | 
qed  | 
|
| 58687 | 550  | 
|
| 70226 | 551  | 
lemma int_parity_induct [case_names zero minus even odd]:  | 
552  | 
"P k" if zero_int: "P 0"  | 
|
553  | 
and minus_int: "P (- 1)"  | 
|
554  | 
and even_int: "\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)"  | 
|
555  | 
and odd_int: "\<And>k. P k \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> P (1 + (k * 2))" for k :: int  | 
|
556  | 
proof (cases "k \<ge> 0")  | 
|
557  | 
case True  | 
|
558  | 
define n where "n = nat k"  | 
|
559  | 
with True have "k = int n"  | 
|
560  | 
by simp  | 
|
561  | 
then show "P k"  | 
|
562  | 
proof (induction n arbitrary: k rule: nat_parity_induct)  | 
|
563  | 
case zero  | 
|
564  | 
then show ?case  | 
|
565  | 
by (simp add: zero_int)  | 
|
566  | 
next  | 
|
567  | 
case (even n)  | 
|
568  | 
have "P (int n * 2)"  | 
|
569  | 
by (rule even_int) (use even in simp_all)  | 
|
570  | 
with even show ?case  | 
|
571  | 
by (simp add: ac_simps)  | 
|
572  | 
next  | 
|
573  | 
case (odd n)  | 
|
574  | 
have "P (1 + (int n * 2))"  | 
|
575  | 
by (rule odd_int) (use odd in simp_all)  | 
|
576  | 
with odd show ?case  | 
|
577  | 
by (simp add: ac_simps)  | 
|
578  | 
qed  | 
|
579  | 
next  | 
|
580  | 
case False  | 
|
581  | 
define n where "n = nat (- k - 1)"  | 
|
582  | 
with False have "k = - int n - 1"  | 
|
583  | 
by simp  | 
|
584  | 
then show "P k"  | 
|
585  | 
proof (induction n arbitrary: k rule: nat_parity_induct)  | 
|
586  | 
case zero  | 
|
587  | 
then show ?case  | 
|
588  | 
by (simp add: minus_int)  | 
|
589  | 
next  | 
|
590  | 
case (even n)  | 
|
591  | 
have "P (1 + (- int (Suc n) * 2))"  | 
|
592  | 
by (rule odd_int) (use even in \<open>simp_all add: algebra_simps\<close>)  | 
|
593  | 
also have "\<dots> = - int (2 * n) - 1"  | 
|
594  | 
by (simp add: algebra_simps)  | 
|
595  | 
finally show ?case  | 
|
596  | 
using even by simp  | 
|
597  | 
next  | 
|
598  | 
case (odd n)  | 
|
599  | 
have "P (- int (Suc n) * 2)"  | 
|
600  | 
by (rule even_int) (use odd in \<open>simp_all add: algebra_simps\<close>)  | 
|
601  | 
also have "\<dots> = - int (Suc (2 * n)) - 1"  | 
|
602  | 
by (simp add: algebra_simps)  | 
|
603  | 
finally show ?case  | 
|
604  | 
using odd by simp  | 
|
605  | 
qed  | 
|
606  | 
qed  | 
|
607  | 
||
| 68157 | 608  | 
lemma not_mod2_eq_Suc_0_eq_0 [simp]:  | 
609  | 
"n mod 2 \<noteq> Suc 0 \<longleftrightarrow> n mod 2 = 0"  | 
|
610  | 
using not_mod_2_eq_1_eq_0 [of n] by simp  | 
|
611  | 
||
| 69502 | 612  | 
lemma odd_card_imp_not_empty:  | 
613  | 
  \<open>A \<noteq> {}\<close> if \<open>odd (card A)\<close>
 | 
|
614  | 
using that by auto  | 
|
615  | 
||
| 58687 | 616  | 
|
| 60758 | 617  | 
subsection \<open>Parity and powers\<close>  | 
| 58689 | 618  | 
|
| 
61531
 
ab2e862263e7
Rounding function, uniform limits, cotangent, binomial identities
 
eberlm 
parents: 
60867 
diff
changeset
 | 
619  | 
context ring_1  | 
| 58689 | 620  | 
begin  | 
621  | 
||
| 63654 | 622  | 
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"  | 
| 58690 | 623  | 
by (auto elim: evenE)  | 
| 58689 | 624  | 
|
| 63654 | 625  | 
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"  | 
| 58690 | 626  | 
by (auto elim: oddE)  | 
627  | 
||
| 66815 | 628  | 
lemma uminus_power_if:  | 
629  | 
"(- a) ^ n = (if even n then a ^ n else - (a ^ n))"  | 
|
630  | 
by auto  | 
|
631  | 
||
| 63654 | 632  | 
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"  | 
| 58690 | 633  | 
by simp  | 
| 58689 | 634  | 
|
| 63654 | 635  | 
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"  | 
| 58690 | 636  | 
by simp  | 
| 58689 | 637  | 
|
| 66582 | 638  | 
lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)"  | 
639  | 
by (cases "even (n + k)") auto  | 
|
640  | 
||
| 
67371
 
2d9cf74943e1
moved in some material from Euler-MacLaurin
 
paulson <lp15@cam.ac.uk> 
parents: 
67083 
diff
changeset
 | 
641  | 
lemma minus_one_power_iff: "(- 1) ^ n = (if even n then 1 else - 1)"  | 
| 
 
2d9cf74943e1
moved in some material from Euler-MacLaurin
 
paulson <lp15@cam.ac.uk> 
parents: 
67083 
diff
changeset
 | 
642  | 
by (induct n) auto  | 
| 
 
2d9cf74943e1
moved in some material from Euler-MacLaurin
 
paulson <lp15@cam.ac.uk> 
parents: 
67083 
diff
changeset
 | 
643  | 
|
| 63654 | 644  | 
end  | 
| 58689 | 645  | 
|
646  | 
context linordered_idom  | 
|
647  | 
begin  | 
|
648  | 
||
| 63654 | 649  | 
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"  | 
| 58690 | 650  | 
by (auto elim: evenE)  | 
| 58689 | 651  | 
|
| 63654 | 652  | 
lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"  | 
| 58689 | 653  | 
by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)  | 
654  | 
||
| 63654 | 655  | 
lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"  | 
| 58787 | 656  | 
by (auto simp add: zero_le_even_power zero_le_odd_power)  | 
| 63654 | 657  | 
|
658  | 
lemma zero_less_power_eq: "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"  | 
|
| 58689 | 659  | 
proof -  | 
660  | 
have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"  | 
|
| 58787 | 661  | 
unfolding power_eq_0_iff [of a n, symmetric] by blast  | 
| 58689 | 662  | 
show ?thesis  | 
| 63654 | 663  | 
unfolding less_le zero_le_power_eq by auto  | 
| 58689 | 664  | 
qed  | 
665  | 
||
| 63654 | 666  | 
lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"  | 
| 58689 | 667  | 
unfolding not_le [symmetric] zero_le_power_eq by auto  | 
668  | 
||
| 63654 | 669  | 
lemma power_le_zero_eq: "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)"  | 
670  | 
unfolding not_less [symmetric] zero_less_power_eq by auto  | 
|
671  | 
||
672  | 
lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"  | 
|
| 58689 | 673  | 
using power_abs [of a n] by (simp add: zero_le_even_power)  | 
674  | 
||
675  | 
lemma power_mono_even:  | 
|
676  | 
assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"  | 
|
677  | 
shows "a ^ n \<le> b ^ n"  | 
|
678  | 
proof -  | 
|
679  | 
have "0 \<le> \<bar>a\<bar>" by auto  | 
|
| 63654 | 680  | 
with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n"  | 
681  | 
by (rule power_mono)  | 
|
682  | 
with \<open>even n\<close> show ?thesis  | 
|
683  | 
by (simp add: power_even_abs)  | 
|
| 58689 | 684  | 
qed  | 
685  | 
||
686  | 
lemma power_mono_odd:  | 
|
687  | 
assumes "odd n" and "a \<le> b"  | 
|
688  | 
shows "a ^ n \<le> b ^ n"  | 
|
689  | 
proof (cases "b < 0")  | 
|
| 63654 | 690  | 
case True  | 
691  | 
with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto  | 
|
692  | 
then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)  | 
|
| 60758 | 693  | 
with \<open>odd n\<close> show ?thesis by simp  | 
| 58689 | 694  | 
next  | 
| 63654 | 695  | 
case False  | 
696  | 
then have "0 \<le> b" by auto  | 
|
| 58689 | 697  | 
show ?thesis  | 
698  | 
proof (cases "a < 0")  | 
|
| 63654 | 699  | 
case True  | 
700  | 
then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto  | 
|
| 60758 | 701  | 
then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto  | 
| 63654 | 702  | 
moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto  | 
| 58689 | 703  | 
ultimately show ?thesis by auto  | 
704  | 
next  | 
|
| 63654 | 705  | 
case False  | 
706  | 
then have "0 \<le> a" by auto  | 
|
707  | 
with \<open>a \<le> b\<close> show ?thesis  | 
|
708  | 
using power_mono by auto  | 
|
| 58689 | 709  | 
qed  | 
710  | 
qed  | 
|
| 62083 | 711  | 
|
| 60758 | 712  | 
text \<open>Simplify, when the exponent is a numeral\<close>  | 
| 58689 | 713  | 
|
714  | 
lemma zero_le_power_eq_numeral [simp]:  | 
|
715  | 
"0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"  | 
|
716  | 
by (fact zero_le_power_eq)  | 
|
717  | 
||
718  | 
lemma zero_less_power_eq_numeral [simp]:  | 
|
| 63654 | 719  | 
"0 < a ^ numeral w \<longleftrightarrow>  | 
720  | 
numeral w = (0 :: nat) \<or>  | 
|
721  | 
even (numeral w :: nat) \<and> a \<noteq> 0 \<or>  | 
|
722  | 
odd (numeral w :: nat) \<and> 0 < a"  | 
|
| 58689 | 723  | 
by (fact zero_less_power_eq)  | 
724  | 
||
725  | 
lemma power_le_zero_eq_numeral [simp]:  | 
|
| 63654 | 726  | 
"a ^ numeral w \<le> 0 \<longleftrightarrow>  | 
727  | 
(0 :: nat) < numeral w \<and>  | 
|
728  | 
(odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"  | 
|
| 58689 | 729  | 
by (fact power_le_zero_eq)  | 
730  | 
||
731  | 
lemma power_less_zero_eq_numeral [simp]:  | 
|
732  | 
"a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"  | 
|
733  | 
by (fact power_less_zero_eq)  | 
|
734  | 
||
735  | 
lemma power_even_abs_numeral [simp]:  | 
|
736  | 
"even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"  | 
|
737  | 
by (fact power_even_abs)  | 
|
738  | 
||
739  | 
end  | 
|
740  | 
||
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
741  | 
|
| 69593 | 742  | 
subsection \<open>Instance for \<^typ>\<open>int\<close>\<close>  | 
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
743  | 
|
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
744  | 
instance int :: ring_parity  | 
| 66839 | 745  | 
by standard (simp_all add: dvd_eq_mod_eq_0 divide_int_def division_segment_int_def)  | 
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
746  | 
|
| 67816 | 747  | 
lemma even_diff_iff:  | 
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
748  | 
"even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int  | 
| 67816 | 749  | 
by (fact even_diff)  | 
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
750  | 
|
| 67816 | 751  | 
lemma even_abs_add_iff:  | 
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
752  | 
"even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int  | 
| 67816 | 753  | 
by simp  | 
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
754  | 
|
| 67816 | 755  | 
lemma even_add_abs_iff:  | 
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
756  | 
"even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int  | 
| 67816 | 757  | 
by simp  | 
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
758  | 
|
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
759  | 
lemma even_nat_iff: "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
760  | 
by (simp add: even_of_nat [of "nat k", where ?'a = int, symmetric])  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
761  | 
|
| 67816 | 762  | 
|
| 67828 | 763  | 
subsection \<open>Abstract bit operations\<close>  | 
764  | 
||
765  | 
context semiring_parity  | 
|
| 67816 | 766  | 
begin  | 
767  | 
||
768  | 
text \<open>The primary purpose of the following operations is  | 
|
| 69593 | 769  | 
to avoid ad-hoc simplification of concrete expressions \<^term>\<open>2 ^ n\<close>\<close>  | 
| 67816 | 770  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
771  | 
definition push_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
772  | 
where push_bit_eq_mult: "push_bit n a = a * 2 ^ n"  | 
| 67816 | 773  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
774  | 
definition take_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"  | 
| 68010 | 775  | 
where take_bit_eq_mod: "take_bit n a = a mod 2 ^ n"  | 
| 67816 | 776  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
777  | 
definition drop_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"  | 
| 68010 | 778  | 
where drop_bit_eq_div: "drop_bit n a = a div 2 ^ n"  | 
| 67816 | 779  | 
|
780  | 
lemma bit_ident:  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
781  | 
"push_bit n (drop_bit n a) + take_bit n a = a"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
782  | 
using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div)  | 
| 67816 | 783  | 
|
| 67960 | 784  | 
lemma push_bit_push_bit [simp]:  | 
785  | 
"push_bit m (push_bit n a) = push_bit (m + n) a"  | 
|
786  | 
by (simp add: push_bit_eq_mult power_add ac_simps)  | 
|
787  | 
||
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
788  | 
lemma take_bit_take_bit [simp]:  | 
| 67960 | 789  | 
"take_bit m (take_bit n a) = take_bit (min m n) a"  | 
790  | 
proof (cases "m \<le> n")  | 
|
791  | 
case True  | 
|
792  | 
then show ?thesis  | 
|
793  | 
by (simp add: take_bit_eq_mod not_le min_def mod_mod_cancel le_imp_power_dvd)  | 
|
794  | 
next  | 
|
795  | 
case False  | 
|
796  | 
then have "n < m" and "min m n = n"  | 
|
797  | 
by simp_all  | 
|
798  | 
then have "2 ^ m = of_nat (2 ^ n) * of_nat (2 ^ (m - n))"  | 
|
799  | 
by (simp add: power_add [symmetric])  | 
|
800  | 
then have "a mod 2 ^ n mod 2 ^ m = a mod 2 ^ n mod (of_nat (2 ^ n) * of_nat (2 ^ (m - n)))"  | 
|
801  | 
by simp  | 
|
802  | 
also have "\<dots> = of_nat (2 ^ n) * (a mod 2 ^ n div of_nat (2 ^ n) mod of_nat (2 ^ (m - n))) + a mod 2 ^ n mod of_nat (2 ^ n)"  | 
|
803  | 
by (simp only: mod_mult2_eq')  | 
|
804  | 
finally show ?thesis  | 
|
805  | 
using \<open>min m n = n\<close> by (simp add: take_bit_eq_mod)  | 
|
806  | 
qed  | 
|
807  | 
||
808  | 
lemma drop_bit_drop_bit [simp]:  | 
|
809  | 
"drop_bit m (drop_bit n a) = drop_bit (m + n) a"  | 
|
810  | 
proof -  | 
|
811  | 
have "a div (2 ^ m * 2 ^ n) = a div (of_nat (2 ^ n) * of_nat (2 ^ m))"  | 
|
812  | 
by (simp add: ac_simps)  | 
|
813  | 
also have "\<dots> = a div of_nat (2 ^ n) div of_nat (2 ^ m)"  | 
|
814  | 
by (simp only: div_mult2_eq')  | 
|
815  | 
finally show ?thesis  | 
|
816  | 
by (simp add: drop_bit_eq_div power_add)  | 
|
817  | 
qed  | 
|
818  | 
||
819  | 
lemma push_bit_take_bit:  | 
|
820  | 
"push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)"  | 
|
821  | 
by (simp add: push_bit_eq_mult take_bit_eq_mod power_add mult_mod_right ac_simps)  | 
|
822  | 
||
823  | 
lemma take_bit_push_bit:  | 
|
824  | 
"take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)"  | 
|
825  | 
proof (cases "m \<le> n")  | 
|
826  | 
case True  | 
|
827  | 
then show ?thesis  | 
|
828  | 
by (simp_all add: push_bit_eq_mult take_bit_eq_mod mod_eq_0_iff_dvd dvd_power_le)  | 
|
829  | 
next  | 
|
830  | 
case False  | 
|
831  | 
then show ?thesis  | 
|
832  | 
using push_bit_take_bit [of n "m - n" a]  | 
|
833  | 
by simp  | 
|
834  | 
qed  | 
|
835  | 
||
836  | 
lemma take_bit_drop_bit:  | 
|
837  | 
"take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)"  | 
|
838  | 
using mod_mult2_eq' [of a "2 ^ n" "2 ^ m"]  | 
|
839  | 
by (simp add: drop_bit_eq_div take_bit_eq_mod power_add ac_simps)  | 
|
840  | 
||
841  | 
lemma drop_bit_take_bit:  | 
|
842  | 
"drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)"  | 
|
843  | 
proof (cases "m \<le> n")  | 
|
844  | 
case True  | 
|
845  | 
then show ?thesis  | 
|
846  | 
using take_bit_drop_bit [of "n - m" m a] by simp  | 
|
847  | 
next  | 
|
848  | 
case False  | 
|
849  | 
then have "a mod 2 ^ n div 2 ^ m = a mod 2 ^ n div 2 ^ (n + (m - n))"  | 
|
850  | 
by simp  | 
|
851  | 
also have "\<dots> = a mod 2 ^ n div (2 ^ n * 2 ^ (m - n))"  | 
|
852  | 
by (simp add: power_add)  | 
|
853  | 
also have "\<dots> = a mod 2 ^ n div (of_nat (2 ^ n) * of_nat (2 ^ (m - n)))"  | 
|
854  | 
by simp  | 
|
855  | 
also have "\<dots> = a mod 2 ^ n div of_nat (2 ^ n) div of_nat (2 ^ (m - n))"  | 
|
856  | 
by (simp only: div_mult2_eq')  | 
|
857  | 
finally show ?thesis  | 
|
858  | 
using False by (simp add: take_bit_eq_mod drop_bit_eq_div)  | 
|
859  | 
qed  | 
|
860  | 
||
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
861  | 
lemma push_bit_0_id [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
862  | 
"push_bit 0 = id"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
863  | 
by (simp add: fun_eq_iff push_bit_eq_mult)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
864  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
865  | 
lemma push_bit_of_0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
866  | 
"push_bit n 0 = 0"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
867  | 
by (simp add: push_bit_eq_mult)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
868  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
869  | 
lemma push_bit_of_1:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
870  | 
"push_bit n 1 = 2 ^ n"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
871  | 
by (simp add: push_bit_eq_mult)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
872  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
873  | 
lemma push_bit_Suc [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
874  | 
"push_bit (Suc n) a = push_bit n (a * 2)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
875  | 
by (simp add: push_bit_eq_mult ac_simps)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
876  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
877  | 
lemma push_bit_double:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
878  | 
"push_bit n (a * 2) = push_bit n a * 2"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
879  | 
by (simp add: push_bit_eq_mult ac_simps)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
880  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
881  | 
lemma push_bit_eq_0_iff [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
882  | 
"push_bit n a = 0 \<longleftrightarrow> a = 0"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
883  | 
by (simp add: push_bit_eq_mult)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
884  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
885  | 
lemma push_bit_add:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
886  | 
"push_bit n (a + b) = push_bit n a + push_bit n b"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
887  | 
by (simp add: push_bit_eq_mult algebra_simps)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
888  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
889  | 
lemma push_bit_numeral [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
890  | 
"push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
891  | 
by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric]) (simp add: ac_simps)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
892  | 
|
| 68010 | 893  | 
lemma push_bit_of_nat:  | 
894  | 
"push_bit n (of_nat m) = of_nat (push_bit n m)"  | 
|
895  | 
by (simp add: push_bit_eq_mult Parity.push_bit_eq_mult)  | 
|
896  | 
||
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
897  | 
lemma take_bit_0 [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
898  | 
"take_bit 0 a = 0"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
899  | 
by (simp add: take_bit_eq_mod)  | 
| 67816 | 900  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
901  | 
lemma take_bit_Suc [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
902  | 
"take_bit (Suc n) a = take_bit n (a div 2) * 2 + of_bool (odd a)"  | 
| 67816 | 903  | 
proof -  | 
904  | 
have "1 + 2 * (a div 2) mod (2 * 2 ^ n) = (a div 2 * 2 + a mod 2) mod (2 * 2 ^ n)"  | 
|
905  | 
if "odd a"  | 
|
906  | 
using that mod_mult2_eq' [of "1 + 2 * (a div 2)" 2 "2 ^ n"]  | 
|
907  | 
by (simp add: ac_simps odd_iff_mod_2_eq_one mult_mod_right)  | 
|
908  | 
also have "\<dots> = a mod (2 * 2 ^ n)"  | 
|
909  | 
by (simp only: div_mult_mod_eq)  | 
|
910  | 
finally show ?thesis  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
911  | 
by (simp add: take_bit_eq_mod algebra_simps mult_mod_right)  | 
| 67816 | 912  | 
qed  | 
913  | 
||
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
914  | 
lemma take_bit_of_0 [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
915  | 
"take_bit n 0 = 0"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
916  | 
by (simp add: take_bit_eq_mod)  | 
| 67816 | 917  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
918  | 
lemma take_bit_of_1 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
919  | 
"take_bit n 1 = of_bool (n > 0)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
920  | 
by (simp add: take_bit_eq_mod)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
921  | 
|
| 67961 | 922  | 
lemma take_bit_add:  | 
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
923  | 
"take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
924  | 
by (simp add: take_bit_eq_mod mod_simps)  | 
| 67816 | 925  | 
|
| 67961 | 926  | 
lemma take_bit_eq_0_iff:  | 
927  | 
"take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a"  | 
|
928  | 
by (simp add: take_bit_eq_mod mod_eq_0_iff_dvd)  | 
|
929  | 
||
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
930  | 
lemma take_bit_of_1_eq_0_iff [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
931  | 
"take_bit n 1 = 0 \<longleftrightarrow> n = 0"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
932  | 
by (simp add: take_bit_eq_mod)  | 
| 67816 | 933  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
934  | 
lemma even_take_bit_eq [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
935  | 
"even (take_bit n a) \<longleftrightarrow> n = 0 \<or> even a"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
936  | 
by (cases n) (simp_all add: take_bit_eq_mod dvd_mod_iff)  | 
| 67816 | 937  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
938  | 
lemma take_bit_numeral_bit0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
939  | 
"take_bit (numeral l) (numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (numeral k) * 2"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
940  | 
by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric] take_bit_Suc  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
941  | 
ac_simps even_mult_iff nonzero_mult_div_cancel_right [OF numeral_neq_zero]) simp  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
942  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
943  | 
lemma take_bit_numeral_bit1 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
944  | 
"take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
945  | 
by (simp only: numeral_eq_Suc power_Suc numeral_Bit1 [of k] mult_2 [symmetric] take_bit_Suc  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
946  | 
ac_simps even_add even_mult_iff div_mult_self1 [OF numeral_neq_zero]) (simp add: ac_simps)  | 
| 67961 | 947  | 
|
| 68010 | 948  | 
lemma take_bit_of_nat:  | 
949  | 
"take_bit n (of_nat m) = of_nat (take_bit n m)"  | 
|
950  | 
by (simp add: take_bit_eq_mod Parity.take_bit_eq_mod of_nat_mod [of m "2 ^ n"])  | 
|
951  | 
||
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
952  | 
lemma drop_bit_0 [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
953  | 
"drop_bit 0 = id"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
954  | 
by (simp add: fun_eq_iff drop_bit_eq_div)  | 
| 67816 | 955  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
956  | 
lemma drop_bit_of_0 [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
957  | 
"drop_bit n 0 = 0"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
958  | 
by (simp add: drop_bit_eq_div)  | 
| 67816 | 959  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
960  | 
lemma drop_bit_of_1 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
961  | 
"drop_bit n 1 = of_bool (n = 0)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
962  | 
by (simp add: drop_bit_eq_div)  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
963  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
964  | 
lemma drop_bit_Suc [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
965  | 
"drop_bit (Suc n) a = drop_bit n (a div 2)"  | 
| 67816 | 966  | 
proof (cases "even a")  | 
967  | 
case True  | 
|
968  | 
then obtain b where "a = 2 * b" ..  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
969  | 
moreover have "drop_bit (Suc n) (2 * b) = drop_bit n b"  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
970  | 
by (simp add: drop_bit_eq_div)  | 
| 67816 | 971  | 
ultimately show ?thesis  | 
972  | 
by simp  | 
|
973  | 
next  | 
|
974  | 
case False  | 
|
975  | 
then obtain b where "a = 2 * b + 1" ..  | 
|
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
976  | 
moreover have "drop_bit (Suc n) (2 * b + 1) = drop_bit n b"  | 
| 67816 | 977  | 
using div_mult2_eq' [of "1 + b * 2" 2 "2 ^ n"]  | 
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
978  | 
by (auto simp add: drop_bit_eq_div ac_simps)  | 
| 67816 | 979  | 
ultimately show ?thesis  | 
980  | 
by simp  | 
|
981  | 
qed  | 
|
982  | 
||
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
983  | 
lemma drop_bit_half:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
984  | 
"drop_bit n (a div 2) = drop_bit n a div 2"  | 
| 67816 | 985  | 
by (induction n arbitrary: a) simp_all  | 
986  | 
||
| 
67907
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
987  | 
lemma drop_bit_of_bool [simp]:  | 
| 
 
02a14c1cb917
prefer convention to place operation name before type name
 
haftmann 
parents: 
67906 
diff
changeset
 | 
988  | 
"drop_bit n (of_bool d) = of_bool (n = 0 \<and> d)"  | 
| 67816 | 989  | 
by (cases n) simp_all  | 
990  | 
||
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
991  | 
lemma drop_bit_numeral_bit0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
992  | 
"drop_bit (numeral l) (numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (numeral k)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
993  | 
by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric] drop_bit_Suc  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
994  | 
nonzero_mult_div_cancel_left [OF numeral_neq_zero])  | 
| 67816 | 995  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
996  | 
lemma drop_bit_numeral_bit1 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
997  | 
"drop_bit (numeral l) (numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (numeral k)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
998  | 
by (simp only: numeral_eq_Suc power_Suc numeral_Bit1 [of k] mult_2 [symmetric] drop_bit_Suc  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
999  | 
div_mult_self4 [OF numeral_neq_zero]) simp  | 
| 67816 | 1000  | 
|
| 68010 | 1001  | 
lemma drop_bit_of_nat:  | 
1002  | 
"drop_bit n (of_nat m) = of_nat (drop_bit n m)"  | 
|
| 68389 | 1003  | 
by (simp add: drop_bit_eq_div Parity.drop_bit_eq_div of_nat_div [of m "2 ^ n"])  | 
| 68010 | 1004  | 
|
| 58770 | 1005  | 
end  | 
| 67816 | 1006  | 
|
| 
67988
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1007  | 
lemma push_bit_of_Suc_0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1008  | 
"push_bit n (Suc 0) = 2 ^ n"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1009  | 
using push_bit_of_1 [where ?'a = nat] by simp  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1010  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1011  | 
lemma take_bit_of_Suc_0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1012  | 
"take_bit n (Suc 0) = of_bool (0 < n)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1013  | 
using take_bit_of_1 [where ?'a = nat] by simp  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1014  | 
|
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1015  | 
lemma drop_bit_of_Suc_0 [simp]:  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1016  | 
"drop_bit n (Suc 0) = of_bool (n = 0)"  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1017  | 
using drop_bit_of_1 [where ?'a = nat] by simp  | 
| 
 
01c651412081
explicit simp rules for computing abstract bit operations
 
haftmann 
parents: 
67961 
diff
changeset
 | 
1018  | 
|
| 70226 | 1019  | 
|
1020  | 
subsection \<open>Legacy\<close>  | 
|
1021  | 
||
1022  | 
lemma parity_induct [case_names zero even odd]:  | 
|
1023  | 
assumes zero: "P 0"  | 
|
1024  | 
assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)"  | 
|
1025  | 
assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"  | 
|
1026  | 
shows "P n"  | 
|
1027  | 
using assms by (rule nat_parity_induct)  | 
|
1028  | 
||
| 67816 | 1029  | 
end  |