author | nipkow |
Tue, 23 Oct 2007 23:27:23 +0200 | |
changeset 25162 | ad4d5365d9d8 |
parent 25134 | 3d4953e88449 |
child 25502 | 9200b36280c0 |
permissions | -rw-r--r-- |
21263 | 1 |
(* Title: HOL/Library/Parity.thy |
21256 | 2 |
ID: $Id$ |
3 |
Author: Jeremy Avigad |
|
4 |
*) |
|
5 |
||
6 |
header {* Even and Odd for int and nat *} |
|
7 |
||
8 |
theory Parity |
|
9 |
imports Main |
|
10 |
begin |
|
11 |
||
22473 | 12 |
class even_odd = type + |
22390 | 13 |
fixes even :: "'a \<Rightarrow> bool" |
21256 | 14 |
|
15 |
abbreviation |
|
22390 | 16 |
odd :: "'a\<Colon>even_odd \<Rightarrow> bool" where |
17 |
"odd x \<equiv> \<not> even x" |
|
18 |
||
19 |
instance int :: even_odd |
|
23522 | 20 |
even_def[presburger]: "even x \<equiv> x mod 2 = 0" .. |
22390 | 21 |
|
22 |
instance nat :: even_odd |
|
23522 | 23 |
even_nat_def[presburger]: "even x \<equiv> even (int x)" .. |
21256 | 24 |
|
25 |
||
26 |
subsection {* Even and odd are mutually exclusive *} |
|
27 |
||
21263 | 28 |
lemma int_pos_lt_two_imp_zero_or_one: |
21256 | 29 |
"0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1" |
23522 | 30 |
by presburger |
21256 | 31 |
|
23522 | 32 |
lemma neq_one_mod_two [simp, presburger]: |
33 |
"((x::int) mod 2 ~= 0) = (x mod 2 = 1)" by presburger |
|
21256 | 34 |
|
35 |
subsection {* Behavior under integer arithmetic operations *} |
|
36 |
||
37 |
lemma even_times_anything: "even (x::int) ==> even (x * y)" |
|
38 |
by (simp add: even_def zmod_zmult1_eq') |
|
39 |
||
40 |
lemma anything_times_even: "even (y::int) ==> even (x * y)" |
|
41 |
by (simp add: even_def zmod_zmult1_eq) |
|
42 |
||
43 |
lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)" |
|
44 |
by (simp add: even_def zmod_zmult1_eq) |
|
45 |
||
23522 | 46 |
lemma even_product[presburger]: "even((x::int) * y) = (even x | even y)" |
21263 | 47 |
apply (auto simp add: even_times_anything anything_times_even) |
21256 | 48 |
apply (rule ccontr) |
49 |
apply (auto simp add: odd_times_odd) |
|
50 |
done |
|
51 |
||
52 |
lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)" |
|
23522 | 53 |
by presburger |
21256 | 54 |
|
55 |
lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)" |
|
23522 | 56 |
by presburger |
21256 | 57 |
|
58 |
lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)" |
|
23522 | 59 |
by presburger |
21256 | 60 |
|
23522 | 61 |
lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger |
21256 | 62 |
|
23522 | 63 |
lemma even_sum[presburger]: "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))" |
64 |
by presburger |
|
21256 | 65 |
|
23522 | 66 |
lemma even_neg[presburger]: "even (-(x::int)) = even x" by presburger |
21256 | 67 |
|
21263 | 68 |
lemma even_difference: |
23522 | 69 |
"even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger |
21256 | 70 |
|
21263 | 71 |
lemma even_pow_gt_zero: |
72 |
"even (x::int) ==> 0 < n ==> even (x^n)" |
|
73 |
by (induct n) (auto simp add: even_product) |
|
21256 | 74 |
|
23522 | 75 |
lemma odd_pow_iff[presburger]: "odd ((x::int) ^ n) \<longleftrightarrow> (n = 0 \<or> odd x)" |
76 |
apply (induct n, simp_all) |
|
77 |
apply presburger |
|
78 |
apply (case_tac n, auto) |
|
79 |
apply (simp_all add: even_product) |
|
21256 | 80 |
done |
81 |
||
23522 | 82 |
lemma odd_pow: "odd x ==> odd((x::int)^n)" by (simp add: odd_pow_iff) |
83 |
||
84 |
lemma even_power[presburger]: "even ((x::int)^n) = (even x & 0 < n)" |
|
21263 | 85 |
apply (auto simp add: even_pow_gt_zero) |
21256 | 86 |
apply (erule contrapos_pp, erule odd_pow) |
87 |
apply (erule contrapos_pp, simp add: even_def) |
|
88 |
done |
|
89 |
||
23522 | 90 |
lemma even_zero[presburger]: "even (0::int)" by presburger |
21256 | 91 |
|
23522 | 92 |
lemma odd_one[presburger]: "odd (1::int)" by presburger |
21256 | 93 |
|
21263 | 94 |
lemmas even_odd_simps [simp] = even_def[of "number_of v",standard] even_zero |
21256 | 95 |
odd_one even_product even_sum even_neg even_difference even_power |
96 |
||
97 |
||
98 |
subsection {* Equivalent definitions *} |
|
99 |
||
23522 | 100 |
lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" |
101 |
by presburger |
|
21256 | 102 |
|
21263 | 103 |
lemma two_times_odd_div_two_plus_one: "odd (x::int) ==> |
23522 | 104 |
2 * (x div 2) + 1 = x" by presburger |
21256 | 105 |
|
23522 | 106 |
lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger |
21256 | 107 |
|
23522 | 108 |
lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger |
21256 | 109 |
|
110 |
subsection {* even and odd for nats *} |
|
111 |
||
112 |
lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)" |
|
113 |
by (simp add: even_nat_def) |
|
114 |
||
23522 | 115 |
lemma even_nat_product[presburger]: "even((x::nat) * y) = (even x | even y)" |
23431
25ca91279a9b
change simp rules for of_nat to work like int did previously (reorient of_nat_Suc, remove of_nat_mult [simp]); preserve original variable names in legacy int theorems
huffman
parents:
23309
diff
changeset
|
116 |
by (simp add: even_nat_def int_mult) |
21256 | 117 |
|
23522 | 118 |
lemma even_nat_sum[presburger]: "even ((x::nat) + y) = |
119 |
((even x & even y) | (odd x & odd y))" by presburger |
|
21256 | 120 |
|
23522 | 121 |
lemma even_nat_difference[presburger]: |
21256 | 122 |
"even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))" |
23522 | 123 |
by presburger |
21256 | 124 |
|
23522 | 125 |
lemma even_nat_Suc[presburger]: "even (Suc x) = odd x" by presburger |
21256 | 126 |
|
23522 | 127 |
lemma even_nat_power[presburger]: "even ((x::nat)^y) = (even x & 0 < y)" |
23431
25ca91279a9b
change simp rules for of_nat to work like int did previously (reorient of_nat_Suc, remove of_nat_mult [simp]); preserve original variable names in legacy int theorems
huffman
parents:
23309
diff
changeset
|
128 |
by (simp add: even_nat_def int_power) |
21256 | 129 |
|
23522 | 130 |
lemma even_nat_zero[presburger]: "even (0::nat)" by presburger |
21256 | 131 |
|
21263 | 132 |
lemmas even_odd_nat_simps [simp] = even_nat_def[of "number_of v",standard] |
21256 | 133 |
even_nat_zero even_nat_Suc even_nat_product even_nat_sum even_nat_power |
134 |
||
135 |
||
136 |
subsection {* Equivalent definitions *} |
|
137 |
||
21263 | 138 |
lemma nat_lt_two_imp_zero_or_one: "(x::nat) < Suc (Suc 0) ==> |
23522 | 139 |
x = 0 | x = Suc 0" by presburger |
21256 | 140 |
|
141 |
lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0" |
|
23522 | 142 |
by presburger |
21256 | 143 |
|
144 |
lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0" |
|
23522 | 145 |
by presburger |
21256 | 146 |
|
21263 | 147 |
lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)" |
23522 | 148 |
by presburger |
21256 | 149 |
|
150 |
lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)" |
|
23522 | 151 |
by presburger |
21256 | 152 |
|
21263 | 153 |
lemma even_nat_div_two_times_two: "even (x::nat) ==> |
23522 | 154 |
Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger |
21256 | 155 |
|
21263 | 156 |
lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==> |
23522 | 157 |
Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger |
21256 | 158 |
|
159 |
lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)" |
|
23522 | 160 |
by presburger |
21256 | 161 |
|
162 |
lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))" |
|
23522 | 163 |
by presburger |
21256 | 164 |
|
165 |
subsection {* Parity and powers *} |
|
166 |
||
21263 | 167 |
lemma minus_one_even_odd_power: |
168 |
"(even x --> (- 1::'a::{comm_ring_1,recpower})^x = 1) & |
|
21256 | 169 |
(odd x --> (- 1::'a)^x = - 1)" |
170 |
apply (induct x) |
|
171 |
apply (rule conjI) |
|
172 |
apply simp |
|
173 |
apply (insert even_nat_zero, blast) |
|
174 |
apply (simp add: power_Suc) |
|
21263 | 175 |
done |
21256 | 176 |
|
177 |
lemma minus_one_even_power [simp]: |
|
21263 | 178 |
"even x ==> (- 1::'a::{comm_ring_1,recpower})^x = 1" |
179 |
using minus_one_even_odd_power by blast |
|
21256 | 180 |
|
181 |
lemma minus_one_odd_power [simp]: |
|
21263 | 182 |
"odd x ==> (- 1::'a::{comm_ring_1,recpower})^x = - 1" |
183 |
using minus_one_even_odd_power by blast |
|
21256 | 184 |
|
185 |
lemma neg_one_even_odd_power: |
|
21263 | 186 |
"(even x --> (-1::'a::{number_ring,recpower})^x = 1) & |
21256 | 187 |
(odd x --> (-1::'a)^x = -1)" |
188 |
apply (induct x) |
|
189 |
apply (simp, simp add: power_Suc) |
|
190 |
done |
|
191 |
||
192 |
lemma neg_one_even_power [simp]: |
|
21263 | 193 |
"even x ==> (-1::'a::{number_ring,recpower})^x = 1" |
194 |
using neg_one_even_odd_power by blast |
|
21256 | 195 |
|
196 |
lemma neg_one_odd_power [simp]: |
|
21263 | 197 |
"odd x ==> (-1::'a::{number_ring,recpower})^x = -1" |
198 |
using neg_one_even_odd_power by blast |
|
21256 | 199 |
|
200 |
lemma neg_power_if: |
|
21263 | 201 |
"(-x::'a::{comm_ring_1,recpower}) ^ n = |
21256 | 202 |
(if even n then (x ^ n) else -(x ^ n))" |
21263 | 203 |
apply (induct n) |
204 |
apply (simp_all split: split_if_asm add: power_Suc) |
|
205 |
done |
|
21256 | 206 |
|
21263 | 207 |
lemma zero_le_even_power: "even n ==> |
21256 | 208 |
0 <= (x::'a::{recpower,ordered_ring_strict}) ^ n" |
209 |
apply (simp add: even_nat_equiv_def2) |
|
210 |
apply (erule exE) |
|
211 |
apply (erule ssubst) |
|
212 |
apply (subst power_add) |
|
213 |
apply (rule zero_le_square) |
|
214 |
done |
|
215 |
||
21263 | 216 |
lemma zero_le_odd_power: "odd n ==> |
21256 | 217 |
(0 <= (x::'a::{recpower,ordered_idom}) ^ n) = (0 <= x)" |
218 |
apply (simp add: odd_nat_equiv_def2) |
|
219 |
apply (erule exE) |
|
220 |
apply (erule ssubst) |
|
221 |
apply (subst power_Suc) |
|
222 |
apply (subst power_add) |
|
223 |
apply (subst zero_le_mult_iff) |
|
224 |
apply auto |
|
25162 | 225 |
apply (subgoal_tac "x = 0 & y > 0") |
21256 | 226 |
apply (erule conjE, assumption) |
21263 | 227 |
apply (subst power_eq_0_iff [symmetric]) |
21256 | 228 |
apply (subgoal_tac "0 <= x^y * x^y") |
229 |
apply simp |
|
230 |
apply (rule zero_le_square)+ |
|
21263 | 231 |
done |
21256 | 232 |
|
23522 | 233 |
lemma zero_le_power_eq[presburger]: "(0 <= (x::'a::{recpower,ordered_idom}) ^ n) = |
21256 | 234 |
(even n | (odd n & 0 <= x))" |
235 |
apply auto |
|
21263 | 236 |
apply (subst zero_le_odd_power [symmetric]) |
21256 | 237 |
apply assumption+ |
238 |
apply (erule zero_le_even_power) |
|
21263 | 239 |
apply (subst zero_le_odd_power) |
21256 | 240 |
apply assumption+ |
21263 | 241 |
done |
21256 | 242 |
|
23522 | 243 |
lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{recpower,ordered_idom}) ^ n) = |
21256 | 244 |
(n = 0 | (even n & x ~= 0) | (odd n & 0 < x))" |
245 |
apply (rule iffI) |
|
246 |
apply clarsimp |
|
247 |
apply (rule conjI) |
|
248 |
apply clarsimp |
|
249 |
apply (rule ccontr) |
|
250 |
apply (subgoal_tac "~ (0 <= x^n)") |
|
251 |
apply simp |
|
252 |
apply (subst zero_le_odd_power) |
|
21263 | 253 |
apply assumption |
21256 | 254 |
apply simp |
255 |
apply (rule notI) |
|
256 |
apply (simp add: power_0_left) |
|
257 |
apply (rule notI) |
|
258 |
apply (simp add: power_0_left) |
|
259 |
apply auto |
|
260 |
apply (subgoal_tac "0 <= x^n") |
|
261 |
apply (frule order_le_imp_less_or_eq) |
|
262 |
apply simp |
|
263 |
apply (erule zero_le_even_power) |
|
264 |
apply (subgoal_tac "0 <= x^n") |
|
265 |
apply (frule order_le_imp_less_or_eq) |
|
266 |
apply auto |
|
267 |
apply (subst zero_le_odd_power) |
|
268 |
apply assumption |
|
269 |
apply (erule order_less_imp_le) |
|
21263 | 270 |
done |
21256 | 271 |
|
23522 | 272 |
lemma power_less_zero_eq[presburger]: "((x::'a::{recpower,ordered_idom}) ^ n < 0) = |
273 |
(odd n & x < 0)" |
|
21263 | 274 |
apply (subst linorder_not_le [symmetric])+ |
21256 | 275 |
apply (subst zero_le_power_eq) |
276 |
apply auto |
|
21263 | 277 |
done |
21256 | 278 |
|
23522 | 279 |
lemma power_le_zero_eq[presburger]: "((x::'a::{recpower,ordered_idom}) ^ n <= 0) = |
21256 | 280 |
(n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))" |
21263 | 281 |
apply (subst linorder_not_less [symmetric])+ |
21256 | 282 |
apply (subst zero_less_power_eq) |
283 |
apply auto |
|
21263 | 284 |
done |
21256 | 285 |
|
21263 | 286 |
lemma power_even_abs: "even n ==> |
21256 | 287 |
(abs (x::'a::{recpower,ordered_idom}))^n = x^n" |
21263 | 288 |
apply (subst power_abs [symmetric]) |
21256 | 289 |
apply (simp add: zero_le_even_power) |
21263 | 290 |
done |
21256 | 291 |
|
23522 | 292 |
lemma zero_less_power_nat_eq[presburger]: "(0 < (x::nat) ^ n) = (n = 0 | 0 < x)" |
21263 | 293 |
by (induct n) auto |
21256 | 294 |
|
21263 | 295 |
lemma power_minus_even [simp]: "even n ==> |
21256 | 296 |
(- x)^n = (x^n::'a::{recpower,comm_ring_1})" |
297 |
apply (subst power_minus) |
|
298 |
apply simp |
|
21263 | 299 |
done |
21256 | 300 |
|
21263 | 301 |
lemma power_minus_odd [simp]: "odd n ==> |
21256 | 302 |
(- x)^n = - (x^n::'a::{recpower,comm_ring_1})" |
303 |
apply (subst power_minus) |
|
304 |
apply simp |
|
21263 | 305 |
done |
21256 | 306 |
|
21263 | 307 |
|
308 |
text {* Simplify, when the exponent is a numeral *} |
|
21256 | 309 |
|
310 |
lemmas power_0_left_number_of = power_0_left [of "number_of w", standard] |
|
311 |
declare power_0_left_number_of [simp] |
|
312 |
||
21263 | 313 |
lemmas zero_le_power_eq_number_of [simp] = |
21256 | 314 |
zero_le_power_eq [of _ "number_of w", standard] |
315 |
||
21263 | 316 |
lemmas zero_less_power_eq_number_of [simp] = |
21256 | 317 |
zero_less_power_eq [of _ "number_of w", standard] |
318 |
||
21263 | 319 |
lemmas power_le_zero_eq_number_of [simp] = |
21256 | 320 |
power_le_zero_eq [of _ "number_of w", standard] |
321 |
||
21263 | 322 |
lemmas power_less_zero_eq_number_of [simp] = |
21256 | 323 |
power_less_zero_eq [of _ "number_of w", standard] |
324 |
||
21263 | 325 |
lemmas zero_less_power_nat_eq_number_of [simp] = |
21256 | 326 |
zero_less_power_nat_eq [of _ "number_of w", standard] |
327 |
||
21263 | 328 |
lemmas power_eq_0_iff_number_of [simp] = power_eq_0_iff [of _ "number_of w", standard] |
21256 | 329 |
|
21263 | 330 |
lemmas power_even_abs_number_of [simp] = power_even_abs [of "number_of w" _, standard] |
21256 | 331 |
|
332 |
||
333 |
subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *} |
|
334 |
||
335 |
lemma even_power_le_0_imp_0: |
|
21263 | 336 |
"a ^ (2*k) \<le> (0::'a::{ordered_idom,recpower}) ==> a=0" |
337 |
by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff power_Suc) |
|
21256 | 338 |
|
23522 | 339 |
lemma zero_le_power_iff[presburger]: |
21263 | 340 |
"(0 \<le> a^n) = (0 \<le> (a::'a::{ordered_idom,recpower}) | even n)" |
21256 | 341 |
proof cases |
342 |
assume even: "even n" |
|
343 |
then obtain k where "n = 2*k" |
|
344 |
by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2) |
|
21263 | 345 |
thus ?thesis by (simp add: zero_le_even_power even) |
21256 | 346 |
next |
347 |
assume odd: "odd n" |
|
348 |
then obtain k where "n = Suc(2*k)" |
|
349 |
by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2) |
|
350 |
thus ?thesis |
|
21263 | 351 |
by (auto simp add: power_Suc zero_le_mult_iff zero_le_even_power |
352 |
dest!: even_power_le_0_imp_0) |
|
353 |
qed |
|
354 |
||
21256 | 355 |
|
356 |
subsection {* Miscellaneous *} |
|
357 |
||
23522 | 358 |
lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger |
359 |
lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger |
|
360 |
lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2" by presburger |
|
361 |
lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger |
|
21256 | 362 |
|
21263 | 363 |
lemma div_Suc: "Suc a div c = a div c + Suc 0 div c + |
23522 | 364 |
(a mod c + Suc 0 mod c) div c" |
21256 | 365 |
apply (subgoal_tac "Suc a = a + Suc 0") |
366 |
apply (erule ssubst) |
|
367 |
apply (rule div_add1_eq, simp) |
|
368 |
done |
|
369 |
||
23522 | 370 |
lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger |
371 |
lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger |
|
21263 | 372 |
lemma even_nat_plus_one_div_two: "even (x::nat) ==> |
23522 | 373 |
(Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger |
21256 | 374 |
|
21263 | 375 |
lemma odd_nat_plus_one_div_two: "odd (x::nat) ==> |
23522 | 376 |
(Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger |
21256 | 377 |
|
378 |
end |