| author | haftmann | 
| Thu, 23 Nov 2017 13:00:00 +0000 | |
| changeset 67083 | 6b2c0681ef28 | 
| parent 67051 | e7e54a0b9197 | 
| child 67371 | 2d9cf74943e1 | 
| 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  | 
|
| 66815 | 14  | 
class semiring_parity = linordered_semidom + unique_euclidean_semiring +  | 
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  | 
|
| 66815 | 157  | 
lemma parity_cases [case_names even odd]:  | 
158  | 
assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P"  | 
|
159  | 
assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P"  | 
|
160  | 
shows P  | 
|
161  | 
using assms by (cases "even a") (simp_all add: odd_iff_mod_2_eq_one)  | 
|
162  | 
||
163  | 
lemma not_mod_2_eq_1_eq_0 [simp]:  | 
|
164  | 
"a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"  | 
|
165  | 
by (cases a rule: parity_cases) simp_all  | 
|
166  | 
||
167  | 
lemma not_mod_2_eq_0_eq_1 [simp]:  | 
|
168  | 
"a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"  | 
|
169  | 
by (cases a rule: parity_cases) simp_all  | 
|
| 58787 | 170  | 
|
| 58690 | 171  | 
lemma evenE [elim?]:  | 
172  | 
assumes "even a"  | 
|
173  | 
obtains b where "a = 2 * b"  | 
|
| 58740 | 174  | 
using assms by (rule dvdE)  | 
| 58690 | 175  | 
|
| 58681 | 176  | 
lemma oddE [elim?]:  | 
| 58680 | 177  | 
assumes "odd a"  | 
178  | 
obtains b where "a = 2 * b + 1"  | 
|
| 58787 | 179  | 
proof -  | 
| 66815 | 180  | 
have "a = 2 * (a div 2) + a mod 2"  | 
181  | 
by (simp add: mult_div_mod_eq)  | 
|
182  | 
with assms have "a = 2 * (a div 2) + 1"  | 
|
183  | 
by (simp add: odd_iff_mod_2_eq_one)  | 
|
184  | 
then show ?thesis ..  | 
|
185  | 
qed  | 
|
186  | 
||
187  | 
lemma mod_2_eq_odd:  | 
|
188  | 
"a mod 2 = of_bool (odd a)"  | 
|
189  | 
by (auto elim: oddE)  | 
|
190  | 
||
191  | 
lemma one_mod_2_pow_eq [simp]:  | 
|
192  | 
"1 mod (2 ^ n) = of_bool (n > 0)"  | 
|
193  | 
proof -  | 
|
| 67083 | 194  | 
have "1 mod (2 ^ n) = of_nat (1 mod (2 ^ n))"  | 
195  | 
using of_nat_mod [of 1 "2 ^ n"] by simp  | 
|
196  | 
also have "\<dots> = of_bool (n > 0)"  | 
|
| 66815 | 197  | 
by simp  | 
| 67083 | 198  | 
finally show ?thesis .  | 
| 66815 | 199  | 
qed  | 
200  | 
||
201  | 
lemma even_of_nat [simp]:  | 
|
202  | 
"even (of_nat a) \<longleftrightarrow> even a"  | 
|
203  | 
proof -  | 
|
204  | 
have "even (of_nat a) \<longleftrightarrow> of_nat 2 dvd of_nat a"  | 
|
205  | 
by simp  | 
|
206  | 
also have "\<dots> \<longleftrightarrow> even a"  | 
|
207  | 
by (simp only: of_nat_dvd_iff)  | 
|
208  | 
finally show ?thesis .  | 
|
209  | 
qed  | 
|
210  | 
||
211  | 
lemma even_zero [simp]:  | 
|
212  | 
"even 0"  | 
|
213  | 
by (fact dvd_0_right)  | 
|
214  | 
||
215  | 
lemma odd_one [simp]:  | 
|
216  | 
"odd 1"  | 
|
217  | 
proof -  | 
|
218  | 
have "\<not> (2 :: nat) dvd 1"  | 
|
219  | 
by simp  | 
|
220  | 
then have "\<not> of_nat 2 dvd of_nat 1"  | 
|
221  | 
unfolding of_nat_dvd_iff by simp  | 
|
222  | 
then show ?thesis  | 
|
223  | 
by simp  | 
|
| 58787 | 224  | 
qed  | 
| 63654 | 225  | 
|
| 66815 | 226  | 
lemma odd_even_add:  | 
227  | 
"even (a + b)" if "odd a" and "odd b"  | 
|
228  | 
proof -  | 
|
229  | 
from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1"  | 
|
230  | 
by (blast elim: oddE)  | 
|
231  | 
then have "a + b = 2 * c + 2 * d + (1 + 1)"  | 
|
232  | 
by (simp only: ac_simps)  | 
|
233  | 
also have "\<dots> = 2 * (c + d + 1)"  | 
|
234  | 
by (simp add: algebra_simps)  | 
|
235  | 
finally show ?thesis ..  | 
|
236  | 
qed  | 
|
237  | 
||
238  | 
lemma even_add [simp]:  | 
|
239  | 
"even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"  | 
|
240  | 
by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add)  | 
|
241  | 
||
242  | 
lemma odd_add [simp]:  | 
|
243  | 
"odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)"  | 
|
244  | 
by simp  | 
|
245  | 
||
246  | 
lemma even_plus_one_iff [simp]:  | 
|
247  | 
"even (a + 1) \<longleftrightarrow> odd a"  | 
|
248  | 
by (auto simp add: dvd_add_right_iff intro: odd_even_add)  | 
|
249  | 
||
250  | 
lemma even_mult_iff [simp]:  | 
|
251  | 
"even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q")  | 
|
252  | 
proof  | 
|
253  | 
assume ?Q  | 
|
254  | 
then show ?P  | 
|
255  | 
by auto  | 
|
256  | 
next  | 
|
257  | 
assume ?P  | 
|
258  | 
show ?Q  | 
|
259  | 
proof (rule ccontr)  | 
|
260  | 
assume "\<not> (even a \<or> even b)"  | 
|
261  | 
then have "odd a" and "odd b"  | 
|
262  | 
by auto  | 
|
263  | 
then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1"  | 
|
264  | 
by (blast elim: oddE)  | 
|
265  | 
then have "a * b = (2 * r + 1) * (2 * s + 1)"  | 
|
266  | 
by simp  | 
|
267  | 
also have "\<dots> = 2 * (2 * r * s + r + s) + 1"  | 
|
268  | 
by (simp add: algebra_simps)  | 
|
269  | 
finally have "odd (a * b)"  | 
|
270  | 
by simp  | 
|
271  | 
with \<open>?P\<close> show False  | 
|
272  | 
by auto  | 
|
273  | 
qed  | 
|
274  | 
qed  | 
|
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
275  | 
|
| 63654 | 276  | 
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"  | 
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
277  | 
proof -  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
278  | 
have "even (2 * numeral n)"  | 
| 66815 | 279  | 
unfolding even_mult_iff by simp  | 
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
280  | 
then have "even (numeral n + numeral n)"  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
281  | 
unfolding mult_2 .  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
282  | 
then show ?thesis  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
283  | 
unfolding numeral.simps .  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
284  | 
qed  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
285  | 
|
| 63654 | 286  | 
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"  | 
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
287  | 
proof  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
288  | 
assume "even (numeral (num.Bit1 n))"  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
289  | 
then have "even (numeral n + numeral n + 1)"  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
290  | 
unfolding numeral.simps .  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
291  | 
then have "even (2 * numeral n + 1)"  | 
| 
 
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 have "2 dvd numeral n * 2 + 1"  | 
| 58740 | 294  | 
by (simp add: ac_simps)  | 
| 63654 | 295  | 
then have "2 dvd 1"  | 
296  | 
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
 | 
297  | 
then show False by simp  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
298  | 
qed  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
299  | 
|
| 63654 | 300  | 
lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"  | 
| 58680 | 301  | 
by (induct n) auto  | 
302  | 
||
| 66815 | 303  | 
lemma even_succ_div_two [simp]:  | 
304  | 
"even a \<Longrightarrow> (a + 1) div 2 = a div 2"  | 
|
305  | 
by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)  | 
|
306  | 
||
307  | 
lemma odd_succ_div_two [simp]:  | 
|
308  | 
"odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"  | 
|
309  | 
by (auto elim!: oddE simp add: add.assoc)  | 
|
310  | 
||
311  | 
lemma even_two_times_div_two:  | 
|
312  | 
"even a \<Longrightarrow> 2 * (a div 2) = a"  | 
|
313  | 
by (fact dvd_mult_div_cancel)  | 
|
314  | 
||
315  | 
lemma odd_two_times_div_two_succ [simp]:  | 
|
316  | 
"odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"  | 
|
317  | 
using mult_div_mod_eq [of 2 a]  | 
|
318  | 
by (simp add: even_iff_mod_2_eq_zero)  | 
|
319  | 
||
| 67051 | 320  | 
lemma coprime_left_2_iff_odd [simp]:  | 
321  | 
"coprime 2 a \<longleftrightarrow> odd a"  | 
|
322  | 
proof  | 
|
323  | 
assume "odd a"  | 
|
324  | 
show "coprime 2 a"  | 
|
325  | 
proof (rule coprimeI)  | 
|
326  | 
fix b  | 
|
327  | 
assume "b dvd 2" "b dvd a"  | 
|
328  | 
then have "b dvd a mod 2"  | 
|
329  | 
by (auto intro: dvd_mod)  | 
|
330  | 
with \<open>odd a\<close> show "is_unit b"  | 
|
331  | 
by (simp add: mod_2_eq_odd)  | 
|
332  | 
qed  | 
|
333  | 
next  | 
|
334  | 
assume "coprime 2 a"  | 
|
335  | 
show "odd a"  | 
|
336  | 
proof (rule notI)  | 
|
337  | 
assume "even a"  | 
|
338  | 
then obtain b where "a = 2 * b" ..  | 
|
339  | 
with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)"  | 
|
340  | 
by simp  | 
|
341  | 
moreover have "\<not> coprime 2 (2 * b)"  | 
|
342  | 
by (rule not_coprimeI [of 2]) simp_all  | 
|
343  | 
ultimately show False  | 
|
344  | 
by blast  | 
|
345  | 
qed  | 
|
346  | 
qed  | 
|
347  | 
||
348  | 
lemma coprime_right_2_iff_odd [simp]:  | 
|
349  | 
"coprime a 2 \<longleftrightarrow> odd a"  | 
|
350  | 
using coprime_left_2_iff_odd [of a] by (simp add: ac_simps)  | 
|
351  | 
||
| 
58678
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
352  | 
end  | 
| 
 
398e05aa84d4
purely algebraic characterization of even and odd
 
haftmann 
parents: 
58645 
diff
changeset
 | 
353  | 
|
| 
59816
 
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
 
haftmann 
parents: 
58889 
diff
changeset
 | 
354  | 
class ring_parity = ring + semiring_parity  | 
| 58679 | 355  | 
begin  | 
356  | 
||
| 
59816
 
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
 
haftmann 
parents: 
58889 
diff
changeset
 | 
357  | 
subclass comm_ring_1 ..  | 
| 
 
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
 
haftmann 
parents: 
58889 
diff
changeset
 | 
358  | 
|
| 66815 | 359  | 
lemma even_minus [simp]:  | 
360  | 
"even (- a) \<longleftrightarrow> even a"  | 
|
| 58740 | 361  | 
by (fact dvd_minus_iff)  | 
| 58679 | 362  | 
|
| 66815 | 363  | 
lemma even_diff [simp]:  | 
364  | 
"even (a - b) \<longleftrightarrow> even (a + b)"  | 
|
| 58680 | 365  | 
using even_add [of a "- b"] by simp  | 
366  | 
||
| 58679 | 367  | 
end  | 
368  | 
||
| 
66808
 
1907167b6038
elementary definition of division on natural numbers
 
haftmann 
parents: 
66582 
diff
changeset
 | 
369  | 
|
| 66815 | 370  | 
subsection \<open>Instance for @{typ nat}\<close>
 | 
| 
66808
 
1907167b6038
elementary definition of division on natural numbers
 
haftmann 
parents: 
66582 
diff
changeset
 | 
371  | 
|
| 66815 | 372  | 
instance nat :: semiring_parity  | 
373  | 
by standard (simp_all add: dvd_eq_mod_eq_0)  | 
|
| 
66808
 
1907167b6038
elementary definition of division on natural numbers
 
haftmann 
parents: 
66582 
diff
changeset
 | 
374  | 
|
| 66815 | 375  | 
lemma even_Suc_Suc_iff [simp]:  | 
376  | 
"even (Suc (Suc n)) \<longleftrightarrow> even n"  | 
|
| 58787 | 377  | 
using dvd_add_triv_right_iff [of 2 n] by simp  | 
| 58687 | 378  | 
|
| 66815 | 379  | 
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"  | 
380  | 
using even_plus_one_iff [of n] by simp  | 
|
| 58787 | 381  | 
|
| 66815 | 382  | 
lemma even_diff_nat [simp]:  | 
383  | 
"even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat  | 
|
| 58787 | 384  | 
proof (cases "n \<le> m")  | 
385  | 
case True  | 
|
386  | 
then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)  | 
|
| 66815 | 387  | 
moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp  | 
388  | 
ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)  | 
|
| 58787 | 389  | 
then show ?thesis by auto  | 
390  | 
next  | 
|
391  | 
case False  | 
|
392  | 
then show ?thesis by simp  | 
|
| 63654 | 393  | 
qed  | 
394  | 
||
| 66815 | 395  | 
lemma odd_pos:  | 
396  | 
"odd n \<Longrightarrow> 0 < n" for n :: nat  | 
|
| 58690 | 397  | 
by (auto elim: oddE)  | 
| 
60343
 
063698416239
correct sort constraints for abbreviations in type classes
 
haftmann 
parents: 
59816 
diff
changeset
 | 
398  | 
|
| 66815 | 399  | 
lemma Suc_double_not_eq_double:  | 
400  | 
"Suc (2 * m) \<noteq> 2 * n"  | 
|
| 62597 | 401  | 
proof  | 
402  | 
assume "Suc (2 * m) = 2 * n"  | 
|
403  | 
moreover have "odd (Suc (2 * m))" and "even (2 * n)"  | 
|
404  | 
by simp_all  | 
|
405  | 
ultimately show False by simp  | 
|
406  | 
qed  | 
|
407  | 
||
| 66815 | 408  | 
lemma double_not_eq_Suc_double:  | 
409  | 
"2 * m \<noteq> Suc (2 * n)"  | 
|
| 62597 | 410  | 
using Suc_double_not_eq_double [of n m] by simp  | 
411  | 
||
| 66815 | 412  | 
lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n"  | 
413  | 
by (auto elim: oddE)  | 
|
| 
60343
 
063698416239
correct sort constraints for abbreviations in type classes
 
haftmann 
parents: 
59816 
diff
changeset
 | 
414  | 
|
| 66815 | 415  | 
lemma even_Suc_div_two [simp]:  | 
416  | 
"even n \<Longrightarrow> Suc n div 2 = n div 2"  | 
|
417  | 
using even_succ_div_two [of n] by simp  | 
|
| 
60343
 
063698416239
correct sort constraints for abbreviations in type classes
 
haftmann 
parents: 
59816 
diff
changeset
 | 
418  | 
|
| 66815 | 419  | 
lemma odd_Suc_div_two [simp]:  | 
420  | 
"odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"  | 
|
421  | 
using odd_succ_div_two [of n] by simp  | 
|
| 
60343
 
063698416239
correct sort constraints for abbreviations in type classes
 
haftmann 
parents: 
59816 
diff
changeset
 | 
422  | 
|
| 66815 | 423  | 
lemma odd_two_times_div_two_nat [simp]:  | 
424  | 
assumes "odd n"  | 
|
425  | 
shows "2 * (n div 2) = n - (1 :: nat)"  | 
|
426  | 
proof -  | 
|
427  | 
from assms have "2 * (n div 2) + 1 = n"  | 
|
428  | 
by (rule odd_two_times_div_two_succ)  | 
|
429  | 
then have "Suc (2 * (n div 2)) - 1 = n - 1"  | 
|
| 58787 | 430  | 
by simp  | 
| 66815 | 431  | 
then show ?thesis  | 
432  | 
by simp  | 
|
| 58787 | 433  | 
qed  | 
| 58680 | 434  | 
|
| 66815 | 435  | 
lemma parity_induct [case_names zero even odd]:  | 
436  | 
assumes zero: "P 0"  | 
|
437  | 
assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)"  | 
|
438  | 
assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"  | 
|
439  | 
shows "P n"  | 
|
440  | 
proof (induct n rule: less_induct)  | 
|
441  | 
case (less n)  | 
|
442  | 
show "P n"  | 
|
443  | 
proof (cases "n = 0")  | 
|
444  | 
case True with zero show ?thesis by simp  | 
|
445  | 
next  | 
|
446  | 
case False  | 
|
447  | 
with less have hyp: "P (n div 2)" by simp  | 
|
448  | 
show ?thesis  | 
|
449  | 
proof (cases "even n")  | 
|
450  | 
case True  | 
|
451  | 
with hyp even [of "n div 2"] show ?thesis  | 
|
452  | 
by simp  | 
|
453  | 
next  | 
|
454  | 
case False  | 
|
455  | 
with hyp odd [of "n div 2"] show ?thesis  | 
|
456  | 
by simp  | 
|
457  | 
qed  | 
|
458  | 
qed  | 
|
459  | 
qed  | 
|
| 58687 | 460  | 
|
461  | 
||
| 60758 | 462  | 
subsection \<open>Parity and powers\<close>  | 
| 58689 | 463  | 
|
| 
61531
 
ab2e862263e7
Rounding function, uniform limits, cotangent, binomial identities
 
eberlm 
parents: 
60867 
diff
changeset
 | 
464  | 
context ring_1  | 
| 58689 | 465  | 
begin  | 
466  | 
||
| 63654 | 467  | 
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"  | 
| 58690 | 468  | 
by (auto elim: evenE)  | 
| 58689 | 469  | 
|
| 63654 | 470  | 
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"  | 
| 58690 | 471  | 
by (auto elim: oddE)  | 
472  | 
||
| 66815 | 473  | 
lemma uminus_power_if:  | 
474  | 
"(- a) ^ n = (if even n then a ^ n else - (a ^ n))"  | 
|
475  | 
by auto  | 
|
476  | 
||
| 63654 | 477  | 
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"  | 
| 58690 | 478  | 
by simp  | 
| 58689 | 479  | 
|
| 63654 | 480  | 
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"  | 
| 58690 | 481  | 
by simp  | 
| 58689 | 482  | 
|
| 66582 | 483  | 
lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)"  | 
484  | 
by (cases "even (n + k)") auto  | 
|
485  | 
||
| 63654 | 486  | 
end  | 
| 58689 | 487  | 
|
488  | 
context linordered_idom  | 
|
489  | 
begin  | 
|
490  | 
||
| 63654 | 491  | 
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"  | 
| 58690 | 492  | 
by (auto elim: evenE)  | 
| 58689 | 493  | 
|
| 63654 | 494  | 
lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"  | 
| 58689 | 495  | 
by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)  | 
496  | 
||
| 63654 | 497  | 
lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"  | 
| 58787 | 498  | 
by (auto simp add: zero_le_even_power zero_le_odd_power)  | 
| 63654 | 499  | 
|
500  | 
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 | 501  | 
proof -  | 
502  | 
have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"  | 
|
| 58787 | 503  | 
unfolding power_eq_0_iff [of a n, symmetric] by blast  | 
| 58689 | 504  | 
show ?thesis  | 
| 63654 | 505  | 
unfolding less_le zero_le_power_eq by auto  | 
| 58689 | 506  | 
qed  | 
507  | 
||
| 63654 | 508  | 
lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"  | 
| 58689 | 509  | 
unfolding not_le [symmetric] zero_le_power_eq by auto  | 
510  | 
||
| 63654 | 511  | 
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)"  | 
512  | 
unfolding not_less [symmetric] zero_less_power_eq by auto  | 
|
513  | 
||
514  | 
lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"  | 
|
| 58689 | 515  | 
using power_abs [of a n] by (simp add: zero_le_even_power)  | 
516  | 
||
517  | 
lemma power_mono_even:  | 
|
518  | 
assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"  | 
|
519  | 
shows "a ^ n \<le> b ^ n"  | 
|
520  | 
proof -  | 
|
521  | 
have "0 \<le> \<bar>a\<bar>" by auto  | 
|
| 63654 | 522  | 
with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n"  | 
523  | 
by (rule power_mono)  | 
|
524  | 
with \<open>even n\<close> show ?thesis  | 
|
525  | 
by (simp add: power_even_abs)  | 
|
| 58689 | 526  | 
qed  | 
527  | 
||
528  | 
lemma power_mono_odd:  | 
|
529  | 
assumes "odd n" and "a \<le> b"  | 
|
530  | 
shows "a ^ n \<le> b ^ n"  | 
|
531  | 
proof (cases "b < 0")  | 
|
| 63654 | 532  | 
case True  | 
533  | 
with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto  | 
|
534  | 
then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)  | 
|
| 60758 | 535  | 
with \<open>odd n\<close> show ?thesis by simp  | 
| 58689 | 536  | 
next  | 
| 63654 | 537  | 
case False  | 
538  | 
then have "0 \<le> b" by auto  | 
|
| 58689 | 539  | 
show ?thesis  | 
540  | 
proof (cases "a < 0")  | 
|
| 63654 | 541  | 
case True  | 
542  | 
then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto  | 
|
| 60758 | 543  | 
then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto  | 
| 63654 | 544  | 
moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto  | 
| 58689 | 545  | 
ultimately show ?thesis by auto  | 
546  | 
next  | 
|
| 63654 | 547  | 
case False  | 
548  | 
then have "0 \<le> a" by auto  | 
|
549  | 
with \<open>a \<le> b\<close> show ?thesis  | 
|
550  | 
using power_mono by auto  | 
|
| 58689 | 551  | 
qed  | 
552  | 
qed  | 
|
| 62083 | 553  | 
|
| 60758 | 554  | 
text \<open>Simplify, when the exponent is a numeral\<close>  | 
| 58689 | 555  | 
|
556  | 
lemma zero_le_power_eq_numeral [simp]:  | 
|
557  | 
"0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"  | 
|
558  | 
by (fact zero_le_power_eq)  | 
|
559  | 
||
560  | 
lemma zero_less_power_eq_numeral [simp]:  | 
|
| 63654 | 561  | 
"0 < a ^ numeral w \<longleftrightarrow>  | 
562  | 
numeral w = (0 :: nat) \<or>  | 
|
563  | 
even (numeral w :: nat) \<and> a \<noteq> 0 \<or>  | 
|
564  | 
odd (numeral w :: nat) \<and> 0 < a"  | 
|
| 58689 | 565  | 
by (fact zero_less_power_eq)  | 
566  | 
||
567  | 
lemma power_le_zero_eq_numeral [simp]:  | 
|
| 63654 | 568  | 
"a ^ numeral w \<le> 0 \<longleftrightarrow>  | 
569  | 
(0 :: nat) < numeral w \<and>  | 
|
570  | 
(odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"  | 
|
| 58689 | 571  | 
by (fact power_le_zero_eq)  | 
572  | 
||
573  | 
lemma power_less_zero_eq_numeral [simp]:  | 
|
574  | 
"a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"  | 
|
575  | 
by (fact power_less_zero_eq)  | 
|
576  | 
||
577  | 
lemma power_even_abs_numeral [simp]:  | 
|
578  | 
"even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"  | 
|
579  | 
by (fact power_even_abs)  | 
|
580  | 
||
581  | 
end  | 
|
582  | 
||
| 
66816
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
583  | 
|
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
584  | 
subsection \<open>Instance for @{typ int}\<close>
 | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
585  | 
|
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
586  | 
instance int :: ring_parity  | 
| 66839 | 587  | 
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
 | 
588  | 
|
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
589  | 
lemma even_diff_iff [simp]:  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
590  | 
"even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
591  | 
using dvd_add_times_triv_right_iff [of 2 "k - l" l] by (simp add: mult_2_right)  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
592  | 
|
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
593  | 
lemma even_abs_add_iff [simp]:  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
594  | 
"even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
595  | 
by (cases "k \<ge> 0") (simp_all add: ac_simps)  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
596  | 
|
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
597  | 
lemma even_add_abs_iff [simp]:  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
598  | 
"even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
599  | 
using even_abs_add_iff [of l k] by (simp add: ac_simps)  | 
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
600  | 
|
| 
 
212a3334e7da
more fundamental definition of div and mod on int
 
haftmann 
parents: 
66815 
diff
changeset
 | 
601  | 
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
 | 
602  | 
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
 | 
603  | 
|
| 58770 | 604  | 
end  |