paulson@14430
|
1 |
(* Title: Parity.thy
|
paulson@14450
|
2 |
ID: $Id$
|
paulson@14430
|
3 |
Author: Jeremy Avigad
|
paulson@14430
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE)
|
paulson@14430
|
5 |
*)
|
paulson@14430
|
6 |
|
paulson@14430
|
7 |
header {* Parity: Even and Odd for ints and nats*}
|
paulson@14430
|
8 |
|
paulson@14430
|
9 |
theory Parity = Divides + IntDiv + NatSimprocs:
|
paulson@14430
|
10 |
|
paulson@14430
|
11 |
axclass even_odd < type
|
paulson@14430
|
12 |
|
paulson@14430
|
13 |
instance int :: even_odd ..
|
paulson@14430
|
14 |
instance nat :: even_odd ..
|
paulson@14430
|
15 |
|
paulson@14430
|
16 |
consts
|
paulson@14430
|
17 |
even :: "'a::even_odd => bool"
|
paulson@14430
|
18 |
|
paulson@14430
|
19 |
syntax
|
paulson@14430
|
20 |
odd :: "'a::even_odd => bool"
|
paulson@14430
|
21 |
|
paulson@14430
|
22 |
translations
|
paulson@14430
|
23 |
"odd x" == "~even x"
|
paulson@14430
|
24 |
|
paulson@14430
|
25 |
defs (overloaded)
|
paulson@14430
|
26 |
even_def: "even (x::int) == x mod 2 = 0"
|
paulson@14430
|
27 |
even_nat_def: "even (x::nat) == even (int x)"
|
paulson@14430
|
28 |
|
paulson@14430
|
29 |
|
paulson@14430
|
30 |
subsection {* Casting a nat power to an integer *}
|
paulson@14430
|
31 |
|
paulson@14430
|
32 |
lemma zpow_int: "int (x^y) = (int x)^y"
|
paulson@14430
|
33 |
apply (induct_tac y)
|
paulson@14430
|
34 |
apply (simp, simp add: zmult_int [THEN sym])
|
paulson@14430
|
35 |
done
|
paulson@14430
|
36 |
|
paulson@14430
|
37 |
subsection {* Even and odd are mutually exclusive *}
|
paulson@14430
|
38 |
|
paulson@14430
|
39 |
lemma int_pos_lt_two_imp_zero_or_one:
|
paulson@14430
|
40 |
"0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"
|
paulson@14430
|
41 |
by auto
|
paulson@14430
|
42 |
|
paulson@14430
|
43 |
lemma neq_one_mod_two [simp]: "((x::int) mod 2 ~= 0) = (x mod 2 = 1)"
|
paulson@14430
|
44 |
apply (subgoal_tac "x mod 2 = 0 | x mod 2 = 1", force)
|
paulson@14430
|
45 |
apply (rule int_pos_lt_two_imp_zero_or_one, auto)
|
paulson@14430
|
46 |
done
|
paulson@14430
|
47 |
|
paulson@14430
|
48 |
subsection {* Behavior under integer arithmetic operations *}
|
paulson@14430
|
49 |
|
paulson@14430
|
50 |
lemma even_times_anything: "even (x::int) ==> even (x * y)"
|
paulson@14430
|
51 |
by (simp add: even_def zmod_zmult1_eq')
|
paulson@14430
|
52 |
|
paulson@14430
|
53 |
lemma anything_times_even: "even (y::int) ==> even (x * y)"
|
paulson@14430
|
54 |
by (simp add: even_def zmod_zmult1_eq)
|
paulson@14430
|
55 |
|
paulson@14430
|
56 |
lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)"
|
paulson@14430
|
57 |
by (simp add: even_def zmod_zmult1_eq)
|
paulson@14430
|
58 |
|
paulson@14430
|
59 |
lemma even_product: "even((x::int) * y) = (even x | even y)"
|
paulson@14430
|
60 |
apply (auto simp add: even_times_anything anything_times_even)
|
paulson@14430
|
61 |
apply (rule ccontr)
|
paulson@14430
|
62 |
apply (auto simp add: odd_times_odd)
|
paulson@14430
|
63 |
done
|
paulson@14430
|
64 |
|
paulson@14430
|
65 |
lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
|
paulson@14430
|
66 |
by (simp add: even_def zmod_zadd1_eq)
|
paulson@14430
|
67 |
|
paulson@14430
|
68 |
lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
|
paulson@14430
|
69 |
by (simp add: even_def zmod_zadd1_eq)
|
paulson@14430
|
70 |
|
paulson@14430
|
71 |
lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
|
paulson@14430
|
72 |
by (simp add: even_def zmod_zadd1_eq)
|
paulson@14430
|
73 |
|
paulson@14430
|
74 |
lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)"
|
paulson@14430
|
75 |
by (simp add: even_def zmod_zadd1_eq)
|
paulson@14430
|
76 |
|
paulson@14430
|
77 |
lemma even_sum: "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
|
paulson@14430
|
78 |
apply (auto intro: even_plus_even odd_plus_odd)
|
paulson@14430
|
79 |
apply (rule ccontr, simp add: even_plus_odd)
|
paulson@14430
|
80 |
apply (rule ccontr, simp add: odd_plus_even)
|
paulson@14430
|
81 |
done
|
paulson@14430
|
82 |
|
paulson@14430
|
83 |
lemma even_neg: "even (-(x::int)) = even x"
|
paulson@14430
|
84 |
by (auto simp add: even_def zmod_zminus1_eq_if)
|
paulson@14430
|
85 |
|
paulson@14430
|
86 |
lemma even_difference:
|
paulson@14430
|
87 |
"even ((x::int) - y) = ((even x & even y) | (odd x & odd y))"
|
paulson@14430
|
88 |
by (simp only: diff_minus even_sum even_neg)
|
paulson@14430
|
89 |
|
paulson@14430
|
90 |
lemma even_pow_gt_zero [rule_format]:
|
paulson@14430
|
91 |
"even (x::int) ==> 0 < n --> even (x^n)"
|
paulson@14430
|
92 |
apply (induct_tac n)
|
paulson@14430
|
93 |
apply (auto simp add: even_product)
|
paulson@14430
|
94 |
done
|
paulson@14430
|
95 |
|
paulson@14430
|
96 |
lemma odd_pow: "odd x ==> odd((x::int)^n)"
|
paulson@14430
|
97 |
apply (induct_tac n)
|
paulson@14430
|
98 |
apply (simp add: even_def)
|
paulson@14430
|
99 |
apply (simp add: even_product)
|
paulson@14430
|
100 |
done
|
paulson@14430
|
101 |
|
paulson@14430
|
102 |
lemma even_power: "even ((x::int)^n) = (even x & 0 < n)"
|
paulson@14430
|
103 |
apply (auto simp add: even_pow_gt_zero)
|
paulson@14430
|
104 |
apply (erule contrapos_pp, erule odd_pow)
|
paulson@14430
|
105 |
apply (erule contrapos_pp, simp add: even_def)
|
paulson@14430
|
106 |
done
|
paulson@14430
|
107 |
|
paulson@14430
|
108 |
lemma even_zero: "even (0::int)"
|
paulson@14430
|
109 |
by (simp add: even_def)
|
paulson@14430
|
110 |
|
paulson@14430
|
111 |
lemma odd_one: "odd (1::int)"
|
paulson@14430
|
112 |
by (simp add: even_def)
|
paulson@14430
|
113 |
|
paulson@14430
|
114 |
lemmas even_odd_simps [simp] = even_def[of "number_of v",standard] even_zero
|
paulson@14430
|
115 |
odd_one even_product even_sum even_neg even_difference even_power
|
paulson@14430
|
116 |
|
paulson@14430
|
117 |
|
paulson@14430
|
118 |
subsection {* Equivalent definitions *}
|
paulson@14430
|
119 |
|
paulson@14430
|
120 |
lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x"
|
paulson@14430
|
121 |
by (auto simp add: even_def)
|
paulson@14430
|
122 |
|
paulson@14430
|
123 |
lemma two_times_odd_div_two_plus_one: "odd (x::int) ==>
|
paulson@14430
|
124 |
2 * (x div 2) + 1 = x"
|
paulson@14430
|
125 |
apply (insert zmod_zdiv_equality [of x 2, THEN sym])
|
paulson@14430
|
126 |
by (simp add: even_def)
|
paulson@14430
|
127 |
|
paulson@14430
|
128 |
lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)"
|
paulson@14430
|
129 |
apply auto
|
paulson@14430
|
130 |
apply (rule exI)
|
paulson@14430
|
131 |
by (erule two_times_even_div_two [THEN sym])
|
paulson@14430
|
132 |
|
paulson@14430
|
133 |
lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)"
|
paulson@14430
|
134 |
apply auto
|
paulson@14430
|
135 |
apply (rule exI)
|
paulson@14430
|
136 |
by (erule two_times_odd_div_two_plus_one [THEN sym])
|
paulson@14430
|
137 |
|
paulson@14430
|
138 |
|
paulson@14430
|
139 |
subsection {* even and odd for nats *}
|
paulson@14430
|
140 |
|
paulson@14430
|
141 |
lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
|
paulson@14430
|
142 |
by (simp add: even_nat_def)
|
paulson@14430
|
143 |
|
paulson@14430
|
144 |
lemma even_nat_product: "even((x::nat) * y) = (even x | even y)"
|
paulson@14430
|
145 |
by (simp add: even_nat_def zmult_int [THEN sym])
|
paulson@14430
|
146 |
|
paulson@14430
|
147 |
lemma even_nat_sum: "even ((x::nat) + y) =
|
paulson@14430
|
148 |
((even x & even y) | (odd x & odd y))"
|
paulson@14430
|
149 |
by (unfold even_nat_def, simp)
|
paulson@14430
|
150 |
|
paulson@14430
|
151 |
lemma even_nat_difference:
|
paulson@14430
|
152 |
"even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
|
paulson@14430
|
153 |
apply (auto simp add: even_nat_def zdiff_int [THEN sym])
|
paulson@14430
|
154 |
apply (case_tac "x < y", auto simp add: zdiff_int [THEN sym])
|
paulson@14430
|
155 |
apply (case_tac "x < y", auto simp add: zdiff_int [THEN sym])
|
paulson@14430
|
156 |
done
|
paulson@14430
|
157 |
|
paulson@14436
|
158 |
lemma even_nat_Suc: "even (Suc x) = odd x"
|
paulson@14430
|
159 |
by (simp add: even_nat_def)
|
paulson@14430
|
160 |
|
paulson@14436
|
161 |
text{*Compatibility, in case Avigad uses this*}
|
paulson@14436
|
162 |
lemmas even_nat_suc = even_nat_Suc
|
paulson@14436
|
163 |
|
paulson@14430
|
164 |
lemma even_nat_power: "even ((x::nat)^y) = (even x & 0 < y)"
|
paulson@14430
|
165 |
by (simp add: even_nat_def zpow_int)
|
paulson@14430
|
166 |
|
paulson@14430
|
167 |
lemma even_nat_zero: "even (0::nat)"
|
paulson@14430
|
168 |
by (simp add: even_nat_def)
|
paulson@14430
|
169 |
|
paulson@14430
|
170 |
lemmas even_odd_nat_simps [simp] = even_nat_def[of "number_of v",standard]
|
paulson@14436
|
171 |
even_nat_zero even_nat_Suc even_nat_product even_nat_sum even_nat_power
|
paulson@14430
|
172 |
|
paulson@14430
|
173 |
|
paulson@14430
|
174 |
subsection {* Equivalent definitions *}
|
paulson@14430
|
175 |
|
paulson@14430
|
176 |
lemma nat_lt_two_imp_zero_or_one: "(x::nat) < Suc (Suc 0) ==>
|
paulson@14430
|
177 |
x = 0 | x = Suc 0"
|
paulson@14430
|
178 |
by auto
|
paulson@14430
|
179 |
|
paulson@14430
|
180 |
lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
|
paulson@14430
|
181 |
apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
|
paulson@14430
|
182 |
apply (drule subst, assumption)
|
paulson@14430
|
183 |
apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
|
paulson@14430
|
184 |
apply force
|
paulson@14430
|
185 |
apply (subgoal_tac "0 < Suc (Suc 0)")
|
paulson@14430
|
186 |
apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
|
paulson@14430
|
187 |
apply (erule nat_lt_two_imp_zero_or_one, auto)
|
paulson@14430
|
188 |
done
|
paulson@14430
|
189 |
|
paulson@14430
|
190 |
lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
|
paulson@14430
|
191 |
apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
|
paulson@14430
|
192 |
apply (drule subst, assumption)
|
paulson@14430
|
193 |
apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
|
paulson@14430
|
194 |
apply force
|
paulson@14430
|
195 |
apply (subgoal_tac "0 < Suc (Suc 0)")
|
paulson@14430
|
196 |
apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
|
paulson@14430
|
197 |
apply (erule nat_lt_two_imp_zero_or_one, auto)
|
paulson@14430
|
198 |
done
|
paulson@14430
|
199 |
|
paulson@14430
|
200 |
lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
|
paulson@14430
|
201 |
apply (rule iffI)
|
paulson@14430
|
202 |
apply (erule even_nat_mod_two_eq_zero)
|
paulson@14430
|
203 |
apply (insert odd_nat_mod_two_eq_one [of x], auto)
|
paulson@14430
|
204 |
done
|
paulson@14430
|
205 |
|
paulson@14430
|
206 |
lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
|
paulson@14430
|
207 |
apply (auto simp add: even_nat_equiv_def)
|
paulson@14430
|
208 |
apply (subgoal_tac "x mod (Suc (Suc 0)) < Suc (Suc 0)")
|
paulson@14430
|
209 |
apply (frule nat_lt_two_imp_zero_or_one, auto)
|
paulson@14430
|
210 |
done
|
paulson@14430
|
211 |
|
paulson@14430
|
212 |
lemma even_nat_div_two_times_two: "even (x::nat) ==>
|
paulson@14430
|
213 |
Suc (Suc 0) * (x div Suc (Suc 0)) = x"
|
paulson@14430
|
214 |
apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
|
paulson@14430
|
215 |
apply (drule even_nat_mod_two_eq_zero, simp)
|
paulson@14430
|
216 |
done
|
paulson@14430
|
217 |
|
paulson@14430
|
218 |
lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
|
paulson@14430
|
219 |
Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x"
|
paulson@14430
|
220 |
apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
|
paulson@14430
|
221 |
apply (drule odd_nat_mod_two_eq_one, simp)
|
paulson@14430
|
222 |
done
|
paulson@14430
|
223 |
|
paulson@14430
|
224 |
lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
|
paulson@14430
|
225 |
apply (rule iffI, rule exI)
|
paulson@14430
|
226 |
apply (erule even_nat_div_two_times_two [THEN sym], auto)
|
paulson@14430
|
227 |
done
|
paulson@14430
|
228 |
|
paulson@14430
|
229 |
lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
|
paulson@14430
|
230 |
apply (rule iffI, rule exI)
|
paulson@14430
|
231 |
apply (erule odd_nat_div_two_times_two_plus_one [THEN sym], auto)
|
paulson@14430
|
232 |
done
|
paulson@14430
|
233 |
|
paulson@14430
|
234 |
subsection {* Powers of negative one *}
|
paulson@14430
|
235 |
|
paulson@14430
|
236 |
lemma neg_one_even_odd_power:
|
paulson@14430
|
237 |
"(even x --> (-1::'a::{number_ring,ringpower})^x = 1) &
|
paulson@14430
|
238 |
(odd x --> (-1::'a)^x = -1)"
|
paulson@14430
|
239 |
apply (induct_tac x)
|
paulson@14430
|
240 |
apply (simp, simp add: power_Suc)
|
paulson@14430
|
241 |
done
|
paulson@14430
|
242 |
|
paulson@14436
|
243 |
lemma neg_one_even_power [simp]:
|
paulson@14436
|
244 |
"even x ==> (-1::'a::{number_ring,ringpower})^x = 1"
|
paulson@14430
|
245 |
by (rule neg_one_even_odd_power [THEN conjunct1, THEN mp], assumption)
|
paulson@14430
|
246 |
|
paulson@14436
|
247 |
lemma neg_one_odd_power [simp]:
|
paulson@14436
|
248 |
"odd x ==> (-1::'a::{number_ring,ringpower})^x = -1"
|
paulson@14430
|
249 |
by (rule neg_one_even_odd_power [THEN conjunct2, THEN mp], assumption)
|
paulson@14430
|
250 |
|
paulson@14443
|
251 |
lemma neg_power_if:
|
obua@14738
|
252 |
"(-x::'a::{comm_ring_1,ringpower}) ^ n =
|
paulson@14443
|
253 |
(if even n then (x ^ n) else -(x ^ n))"
|
paulson@14443
|
254 |
by (induct n, simp_all split: split_if_asm add: power_Suc)
|
paulson@14443
|
255 |
|
paulson@14430
|
256 |
|
paulson@14450
|
257 |
subsection {* An Equivalence for @{term "0 \<le> a^n"} *}
|
paulson@14450
|
258 |
|
paulson@14450
|
259 |
lemma even_power_le_0_imp_0:
|
obua@14738
|
260 |
"a ^ (2*k) \<le> (0::'a::{ordered_idom,ringpower}) ==> a=0"
|
paulson@14450
|
261 |
apply (induct k)
|
paulson@14450
|
262 |
apply (auto simp add: zero_le_mult_iff mult_le_0_iff power_Suc)
|
paulson@14450
|
263 |
done
|
paulson@14450
|
264 |
|
paulson@14450
|
265 |
lemma zero_le_power_iff:
|
obua@14738
|
266 |
"(0 \<le> a^n) = (0 \<le> (a::'a::{ordered_idom,ringpower}) | even n)"
|
paulson@14450
|
267 |
(is "?P n")
|
paulson@14450
|
268 |
proof cases
|
paulson@14450
|
269 |
assume even: "even n"
|
paulson@14473
|
270 |
then obtain k where "n = 2*k"
|
paulson@14450
|
271 |
by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
|
paulson@14450
|
272 |
thus ?thesis by (simp add: zero_le_even_power even)
|
paulson@14450
|
273 |
next
|
paulson@14450
|
274 |
assume odd: "odd n"
|
paulson@14473
|
275 |
then obtain k where "n = Suc(2*k)"
|
paulson@14450
|
276 |
by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
|
paulson@14450
|
277 |
thus ?thesis
|
paulson@14450
|
278 |
by (auto simp add: power_Suc zero_le_mult_iff zero_le_even_power
|
paulson@14450
|
279 |
dest!: even_power_le_0_imp_0)
|
paulson@14450
|
280 |
qed
|
paulson@14450
|
281 |
|
paulson@14430
|
282 |
subsection {* Miscellaneous *}
|
paulson@14430
|
283 |
|
paulson@14430
|
284 |
lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"
|
paulson@14430
|
285 |
apply (subst zdiv_zadd1_eq)
|
paulson@14430
|
286 |
apply (simp add: even_def)
|
paulson@14430
|
287 |
done
|
paulson@14430
|
288 |
|
paulson@14430
|
289 |
lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1"
|
paulson@14430
|
290 |
apply (subst zdiv_zadd1_eq)
|
paulson@14430
|
291 |
apply (simp add: even_def)
|
paulson@14430
|
292 |
done
|
paulson@14430
|
293 |
|
paulson@14430
|
294 |
lemma div_Suc: "Suc a div c = a div c + Suc 0 div c +
|
paulson@14430
|
295 |
(a mod c + Suc 0 mod c) div c"
|
paulson@14430
|
296 |
apply (subgoal_tac "Suc a = a + Suc 0")
|
paulson@14430
|
297 |
apply (erule ssubst)
|
paulson@14430
|
298 |
apply (rule div_add1_eq, simp)
|
paulson@14430
|
299 |
done
|
paulson@14430
|
300 |
|
paulson@14430
|
301 |
lemma even_nat_plus_one_div_two: "even (x::nat) ==>
|
paulson@14430
|
302 |
(Suc x) div Suc (Suc 0) = x div Suc (Suc 0)"
|
paulson@14430
|
303 |
apply (subst div_Suc)
|
paulson@14430
|
304 |
apply (simp add: even_nat_equiv_def)
|
paulson@14430
|
305 |
done
|
paulson@14430
|
306 |
|
paulson@14430
|
307 |
lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
|
paulson@14430
|
308 |
(Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))"
|
paulson@14430
|
309 |
apply (subst div_Suc)
|
paulson@14430
|
310 |
apply (simp add: odd_nat_equiv_def)
|
paulson@14430
|
311 |
done
|
paulson@14430
|
312 |
|
paulson@14430
|
313 |
end
|