author | wenzelm |
Tue, 07 Apr 2015 10:13:33 +0200 | |
changeset 59943 | e83ecf0a0ee1 |
parent 59816 | 034b13f4efae |
child 60343 | 063698416239 |
permissions | -rw-r--r-- |
41959 | 1 |
(* Title: HOL/Parity.thy |
2 |
Author: Jeremy Avigad |
|
3 |
Author: Jacques D. Fleuriot |
|
21256 | 4 |
*) |
5 |
||
58889 | 6 |
section {* Parity in rings and semirings *} |
21256 | 7 |
|
8 |
theory Parity |
|
58778
e29cae8eab1f
even further downshift of theory Parity in the hierarchy
haftmann
parents:
58777
diff
changeset
|
9 |
imports Nat_Transfer |
21256 | 10 |
begin |
11 |
||
58787 | 12 |
subsection {* Ring structures with parity and @{text even}/@{text odd} predicates *} |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
13 |
|
59816
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
14 |
class semiring_parity = comm_semiring_1_diff_distrib + numeral + |
58787 | 15 |
assumes odd_one [simp]: "\<not> 2 dvd 1" |
16 |
assumes odd_even_add: "\<not> 2 dvd a \<Longrightarrow> \<not> 2 dvd b \<Longrightarrow> 2 dvd a + b" |
|
17 |
assumes even_multD: "2 dvd a * b \<Longrightarrow> 2 dvd a \<or> 2 dvd b" |
|
18 |
assumes odd_ex_decrement: "\<not> 2 dvd a \<Longrightarrow> \<exists>b. a = b + 1" |
|
54227
63b441f49645
moving generic lemmas out of theory parity, disregarding some unused auxiliary lemmas;
haftmann
parents:
47225
diff
changeset
|
19 |
begin |
21256 | 20 |
|
59816
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
21 |
subclass semiring_numeral .. |
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
22 |
|
58740 | 23 |
abbreviation even :: "'a \<Rightarrow> bool" |
54228 | 24 |
where |
58740 | 25 |
"even a \<equiv> 2 dvd a" |
54228 | 26 |
|
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
27 |
abbreviation odd :: "'a \<Rightarrow> bool" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
28 |
where |
58740 | 29 |
"odd a \<equiv> \<not> 2 dvd a" |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
30 |
|
58787 | 31 |
lemma even_zero [simp]: |
32 |
"even 0" |
|
33 |
by (fact dvd_0_right) |
|
34 |
||
35 |
lemma even_plus_one_iff [simp]: |
|
36 |
"even (a + 1) \<longleftrightarrow> odd a" |
|
37 |
by (auto simp add: dvd_add_right_iff intro: odd_even_add) |
|
38 |
||
58690 | 39 |
lemma evenE [elim?]: |
40 |
assumes "even a" |
|
41 |
obtains b where "a = 2 * b" |
|
58740 | 42 |
using assms by (rule dvdE) |
58690 | 43 |
|
58681 | 44 |
lemma oddE [elim?]: |
58680 | 45 |
assumes "odd a" |
46 |
obtains b where "a = 2 * b + 1" |
|
58787 | 47 |
proof - |
48 |
from assms obtain b where *: "a = b + 1" |
|
49 |
by (blast dest: odd_ex_decrement) |
|
50 |
with assms have "even (b + 2)" by simp |
|
51 |
then have "even b" by simp |
|
52 |
then obtain c where "b = 2 * c" .. |
|
53 |
with * have "a = 2 * c + 1" by simp |
|
54 |
with that show thesis . |
|
55 |
qed |
|
56 |
||
58770 | 57 |
lemma even_times_iff [simp]: |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
58 |
"even (a * b) \<longleftrightarrow> even a \<or> even b" |
58787 | 59 |
by (auto dest: even_multD) |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
60 |
|
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
61 |
lemma even_numeral [simp]: |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
62 |
"even (numeral (Num.Bit0 n))" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
63 |
proof - |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
64 |
have "even (2 * numeral n)" |
58740 | 65 |
unfolding even_times_iff by simp |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
66 |
then have "even (numeral n + numeral n)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
67 |
unfolding mult_2 . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
68 |
then show ?thesis |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
69 |
unfolding numeral.simps . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
70 |
qed |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
71 |
|
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
72 |
lemma odd_numeral [simp]: |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
73 |
"odd (numeral (Num.Bit1 n))" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
74 |
proof |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
75 |
assume "even (numeral (num.Bit1 n))" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
76 |
then have "even (numeral n + numeral n + 1)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
77 |
unfolding numeral.simps . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
78 |
then have "even (2 * numeral n + 1)" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
79 |
unfolding mult_2 . |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
80 |
then have "2 dvd numeral n * 2 + 1" |
58740 | 81 |
by (simp add: ac_simps) |
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
82 |
with dvd_add_times_triv_left_iff [of 2 "numeral n" 1] |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
83 |
have "2 dvd 1" |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
84 |
by simp |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
85 |
then show False by simp |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
86 |
qed |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
87 |
|
58680 | 88 |
lemma even_add [simp]: |
89 |
"even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)" |
|
58787 | 90 |
by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add) |
58680 | 91 |
|
92 |
lemma odd_add [simp]: |
|
93 |
"odd (a + b) \<longleftrightarrow> (\<not> (odd a \<longleftrightarrow> odd b))" |
|
94 |
by simp |
|
95 |
||
58770 | 96 |
lemma even_power [simp]: |
58771
0997ea62e868
slight generalization and unification of simp rules for algebraic procedures
haftmann
parents:
58770
diff
changeset
|
97 |
"even (a ^ n) \<longleftrightarrow> even a \<and> n > 0" |
58680 | 98 |
by (induct n) auto |
99 |
||
58678
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
100 |
end |
398e05aa84d4
purely algebraic characterization of even and odd
haftmann
parents:
58645
diff
changeset
|
101 |
|
59816
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
102 |
class ring_parity = ring + semiring_parity |
58679 | 103 |
begin |
104 |
||
59816
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
105 |
subclass comm_ring_1 .. |
034b13f4efae
distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents:
58889
diff
changeset
|
106 |
|
58770 | 107 |
lemma even_minus [simp]: |
58679 | 108 |
"even (- a) \<longleftrightarrow> even a" |
58740 | 109 |
by (fact dvd_minus_iff) |
58679 | 110 |
|
58680 | 111 |
lemma even_diff [simp]: |
112 |
"even (a - b) \<longleftrightarrow> even (a + b)" |
|
113 |
using even_add [of a "- b"] by simp |
|
114 |
||
58679 | 115 |
end |
116 |
||
58710
7216a10d69ba
augmented and tuned facts on even/odd and division
haftmann
parents:
58709
diff
changeset
|
117 |
|
58787 | 118 |
subsection {* Instances for @{typ nat} and @{typ int} *} |
119 |
||
120 |
lemma even_Suc_Suc_iff [simp]: |
|
121 |
"even (Suc (Suc n)) \<longleftrightarrow> even n" |
|
122 |
using dvd_add_triv_right_iff [of 2 n] by simp |
|
58687 | 123 |
|
58770 | 124 |
lemma even_Suc [simp]: |
58787 | 125 |
"even (Suc n) \<longleftrightarrow> odd n" |
126 |
by (induct n) auto |
|
127 |
||
128 |
lemma even_diff_nat [simp]: |
|
129 |
fixes m n :: nat |
|
130 |
shows "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" |
|
131 |
proof (cases "n \<le> m") |
|
132 |
case True |
|
133 |
then have "m - n + n * 2 = m + n" by (simp add: mult_2_right) |
|
134 |
moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp |
|
135 |
ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:) |
|
136 |
then show ?thesis by auto |
|
137 |
next |
|
138 |
case False |
|
139 |
then show ?thesis by simp |
|
140 |
qed |
|
141 |
||
142 |
lemma even_diff_iff [simp]: |
|
143 |
fixes k l :: int |
|
144 |
shows "even (k - l) \<longleftrightarrow> even (k + l)" |
|
145 |
using dvd_add_times_triv_right_iff [of 2 "k - l" l] by (simp add: mult_2_right) |
|
146 |
||
147 |
lemma even_abs_add_iff [simp]: |
|
148 |
fixes k l :: int |
|
149 |
shows "even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" |
|
150 |
by (cases "k \<ge> 0") (simp_all add: ac_simps) |
|
151 |
||
152 |
lemma even_add_abs_iff [simp]: |
|
153 |
fixes k l :: int |
|
154 |
shows "even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" |
|
155 |
using even_abs_add_iff [of l k] by (simp add: ac_simps) |
|
156 |
||
157 |
instance nat :: semiring_parity |
|
158 |
proof |
|
159 |
show "odd (1 :: nat)" |
|
160 |
by (rule notI, erule dvdE) simp |
|
161 |
next |
|
162 |
fix m n :: nat |
|
163 |
assume "odd m" |
|
164 |
moreover assume "odd n" |
|
165 |
ultimately have *: "even (Suc m) \<and> even (Suc n)" |
|
166 |
by simp |
|
167 |
then have "even (Suc m + Suc n)" |
|
168 |
by (blast intro: dvd_add) |
|
169 |
also have "Suc m + Suc n = m + n + 2" |
|
170 |
by simp |
|
171 |
finally show "even (m + n)" |
|
172 |
using dvd_add_triv_right_iff [of 2 "m + n"] by simp |
|
173 |
next |
|
174 |
fix m n :: nat |
|
175 |
assume *: "even (m * n)" |
|
176 |
show "even m \<or> even n" |
|
177 |
proof (rule disjCI) |
|
178 |
assume "odd n" |
|
179 |
then have "even (Suc n)" by simp |
|
180 |
then obtain r where "Suc n = 2 * r" .. |
|
181 |
moreover from * obtain s where "m * n = 2 * s" .. |
|
182 |
then have "2 * s + m = m * Suc n" by simp |
|
183 |
ultimately have " 2 * s + m = 2 * (m * r)" by (simp add: algebra_simps) |
|
184 |
then have "m = 2 * (m * r - s)" by simp |
|
185 |
then show "even m" .. |
|
186 |
qed |
|
187 |
next |
|
188 |
fix n :: nat |
|
189 |
assume "odd n" |
|
190 |
then show "\<exists>m. n = m + 1" |
|
191 |
by (cases n) simp_all |
|
192 |
qed |
|
58687 | 193 |
|
58689 | 194 |
lemma odd_pos: |
195 |
"odd (n :: nat) \<Longrightarrow> 0 < n" |
|
58690 | 196 |
by (auto elim: oddE) |
58689 | 197 |
|
58787 | 198 |
instance int :: ring_parity |
199 |
proof |
|
200 |
show "odd (1 :: int)" by (simp add: dvd_int_unfold_dvd_nat) |
|
201 |
fix k l :: int |
|
202 |
assume "odd k" |
|
203 |
moreover assume "odd l" |
|
204 |
ultimately have "even (nat \<bar>k\<bar> + nat \<bar>l\<bar>)" |
|
205 |
by (auto simp add: dvd_int_unfold_dvd_nat intro: odd_even_add) |
|
206 |
then have "even (\<bar>k\<bar> + \<bar>l\<bar>)" |
|
207 |
by (simp add: dvd_int_unfold_dvd_nat nat_add_distrib) |
|
208 |
then show "even (k + l)" |
|
209 |
by simp |
|
210 |
next |
|
211 |
fix k l :: int |
|
212 |
assume "even (k * l)" |
|
213 |
then show "even k \<or> even l" |
|
214 |
by (simp add: dvd_int_unfold_dvd_nat even_multD nat_abs_mult_distrib) |
|
215 |
next |
|
216 |
fix k :: int |
|
217 |
have "k = (k - 1) + 1" by simp |
|
218 |
then show "\<exists>l. k = l + 1" .. |
|
219 |
qed |
|
58680 | 220 |
|
58787 | 221 |
lemma even_int_iff [simp]: |
58679 | 222 |
"even (int n) \<longleftrightarrow> even n" |
58740 | 223 |
by (simp add: dvd_int_iff) |
33318
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents:
31718
diff
changeset
|
224 |
|
58687 | 225 |
lemma even_nat_iff: |
226 |
"0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k" |
|
227 |
by (simp add: even_int_iff [symmetric]) |
|
228 |
||
229 |
||
58787 | 230 |
subsection {* Parity and powers *} |
58689 | 231 |
|
232 |
context comm_ring_1 |
|
233 |
begin |
|
234 |
||
235 |
lemma power_minus_even [simp]: |
|
236 |
"even n \<Longrightarrow> (- a) ^ n = a ^ n" |
|
58690 | 237 |
by (auto elim: evenE) |
58689 | 238 |
|
239 |
lemma power_minus_odd [simp]: |
|
240 |
"odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)" |
|
58690 | 241 |
by (auto elim: oddE) |
242 |
||
58689 | 243 |
lemma neg_one_even_power [simp]: |
244 |
"even n \<Longrightarrow> (- 1) ^ n = 1" |
|
58690 | 245 |
by simp |
58689 | 246 |
|
247 |
lemma neg_one_odd_power [simp]: |
|
248 |
"odd n \<Longrightarrow> (- 1) ^ n = - 1" |
|
58690 | 249 |
by simp |
58689 | 250 |
|
251 |
end |
|
252 |
||
253 |
context linordered_idom |
|
254 |
begin |
|
255 |
||
256 |
lemma zero_le_even_power: |
|
257 |
"even n \<Longrightarrow> 0 \<le> a ^ n" |
|
58690 | 258 |
by (auto elim: evenE) |
58689 | 259 |
|
260 |
lemma zero_le_odd_power: |
|
261 |
"odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a" |
|
262 |
by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE) |
|
263 |
||
58770 | 264 |
lemma zero_le_power_eq: |
58689 | 265 |
"0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a" |
58787 | 266 |
by (auto simp add: zero_le_even_power zero_le_odd_power) |
267 |
||
58770 | 268 |
lemma zero_less_power_eq: |
58689 | 269 |
"0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a" |
270 |
proof - |
|
271 |
have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0" |
|
58787 | 272 |
unfolding power_eq_0_iff [of a n, symmetric] by blast |
58689 | 273 |
show ?thesis |
58710
7216a10d69ba
augmented and tuned facts on even/odd and division
haftmann
parents:
58709
diff
changeset
|
274 |
unfolding less_le zero_le_power_eq by auto |
58689 | 275 |
qed |
276 |
||
58787 | 277 |
lemma power_less_zero_eq [simp]: |
58689 | 278 |
"a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0" |
279 |
unfolding not_le [symmetric] zero_le_power_eq by auto |
|
280 |
||
58770 | 281 |
lemma power_le_zero_eq: |
58689 | 282 |
"a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)" |
283 |
unfolding not_less [symmetric] zero_less_power_eq by auto |
|
284 |
||
285 |
lemma power_even_abs: |
|
286 |
"even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n" |
|
287 |
using power_abs [of a n] by (simp add: zero_le_even_power) |
|
288 |
||
289 |
lemma power_mono_even: |
|
290 |
assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>" |
|
291 |
shows "a ^ n \<le> b ^ n" |
|
292 |
proof - |
|
293 |
have "0 \<le> \<bar>a\<bar>" by auto |
|
294 |
with `\<bar>a\<bar> \<le> \<bar>b\<bar>` |
|
295 |
have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n" by (rule power_mono) |
|
296 |
with `even n` show ?thesis by (simp add: power_even_abs) |
|
297 |
qed |
|
298 |
||
299 |
lemma power_mono_odd: |
|
300 |
assumes "odd n" and "a \<le> b" |
|
301 |
shows "a ^ n \<le> b ^ n" |
|
302 |
proof (cases "b < 0") |
|
303 |
case True with `a \<le> b` have "- b \<le> - a" and "0 \<le> - b" by auto |
|
304 |
hence "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono) |
|
305 |
with `odd n` show ?thesis by simp |
|
306 |
next |
|
307 |
case False then have "0 \<le> b" by auto |
|
308 |
show ?thesis |
|
309 |
proof (cases "a < 0") |
|
310 |
case True then have "n \<noteq> 0" and "a \<le> 0" using `odd n` [THEN odd_pos] by auto |
|
311 |
then have "a ^ n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto |
|
312 |
moreover |
|
313 |
from `0 \<le> b` have "0 \<le> b ^ n" by auto |
|
314 |
ultimately show ?thesis by auto |
|
315 |
next |
|
316 |
case False then have "0 \<le> a" by auto |
|
317 |
with `a \<le> b` show ?thesis using power_mono by auto |
|
318 |
qed |
|
319 |
qed |
|
320 |
||
321 |
text {* Simplify, when the exponent is a numeral *} |
|
322 |
||
323 |
lemma zero_le_power_eq_numeral [simp]: |
|
324 |
"0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a" |
|
325 |
by (fact zero_le_power_eq) |
|
326 |
||
327 |
lemma zero_less_power_eq_numeral [simp]: |
|
328 |
"0 < a ^ numeral w \<longleftrightarrow> numeral w = (0 :: nat) |
|
329 |
\<or> even (numeral w :: nat) \<and> a \<noteq> 0 \<or> odd (numeral w :: nat) \<and> 0 < a" |
|
330 |
by (fact zero_less_power_eq) |
|
331 |
||
332 |
lemma power_le_zero_eq_numeral [simp]: |
|
333 |
"a ^ numeral w \<le> 0 \<longleftrightarrow> (0 :: nat) < numeral w |
|
334 |
\<and> (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)" |
|
335 |
by (fact power_le_zero_eq) |
|
336 |
||
337 |
lemma power_less_zero_eq_numeral [simp]: |
|
338 |
"a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0" |
|
339 |
by (fact power_less_zero_eq) |
|
340 |
||
341 |
lemma power_even_abs_numeral [simp]: |
|
342 |
"even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w" |
|
343 |
by (fact power_even_abs) |
|
344 |
||
345 |
end |
|
346 |
||
347 |
||
58687 | 348 |
subsubsection {* Tools setup *} |
349 |
||
58679 | 350 |
declare transfer_morphism_int_nat [transfer add return: |
351 |
even_int_iff |
|
33318
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents:
31718
diff
changeset
|
352 |
] |
21256 | 353 |
|
58770 | 354 |
end |
355 |