src/HOL/Parity.thy
changeset 28952 15a4b2cf8c34
parent 27668 6eb20b2cecf8
child 29608 564ea783ace8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Parity.thy	Wed Dec 03 15:58:44 2008 +0100
     1.3 @@ -0,0 +1,423 @@
     1.4 +(*  Title:      HOL/Library/Parity.thy
     1.5 +    Author:     Jeremy Avigad, Jacques D. Fleuriot
     1.6 +*)
     1.7 +
     1.8 +header {* Even and Odd for int and nat *}
     1.9 +
    1.10 +theory Parity
    1.11 +imports Plain Presburger
    1.12 +begin
    1.13 +
    1.14 +class even_odd = type + 
    1.15 +  fixes even :: "'a \<Rightarrow> bool"
    1.16 +
    1.17 +abbreviation
    1.18 +  odd :: "'a\<Colon>even_odd \<Rightarrow> bool" where
    1.19 +  "odd x \<equiv> \<not> even x"
    1.20 +
    1.21 +instantiation nat and int  :: even_odd
    1.22 +begin
    1.23 +
    1.24 +definition
    1.25 +  even_def [presburger]: "even x \<longleftrightarrow> (x\<Colon>int) mod 2 = 0"
    1.26 +
    1.27 +definition
    1.28 +  even_nat_def [presburger]: "even x \<longleftrightarrow> even (int x)"
    1.29 +
    1.30 +instance ..
    1.31 +
    1.32 +end
    1.33 +
    1.34 +
    1.35 +subsection {* Even and odd are mutually exclusive *}
    1.36 +
    1.37 +lemma int_pos_lt_two_imp_zero_or_one:
    1.38 +    "0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"
    1.39 +  by presburger
    1.40 +
    1.41 +lemma neq_one_mod_two [simp, presburger]: 
    1.42 +  "((x::int) mod 2 ~= 0) = (x mod 2 = 1)" by presburger
    1.43 +
    1.44 +
    1.45 +subsection {* Behavior under integer arithmetic operations *}
    1.46 +declare dvd_def[algebra]
    1.47 +lemma nat_even_iff_2_dvd[algebra]: "even (x::nat) \<longleftrightarrow> 2 dvd x"
    1.48 +  by (presburger add: even_nat_def even_def)
    1.49 +lemma int_even_iff_2_dvd[algebra]: "even (x::int) \<longleftrightarrow> 2 dvd x"
    1.50 +  by presburger
    1.51 +
    1.52 +lemma even_times_anything: "even (x::int) ==> even (x * y)"
    1.53 +  by algebra
    1.54 +
    1.55 +lemma anything_times_even: "even (y::int) ==> even (x * y)" by algebra
    1.56 +
    1.57 +lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)" 
    1.58 +  by (simp add: even_def zmod_zmult1_eq)
    1.59 +
    1.60 +lemma even_product[presburger]: "even((x::int) * y) = (even x | even y)"
    1.61 +  apply (auto simp add: even_times_anything anything_times_even)
    1.62 +  apply (rule ccontr)
    1.63 +  apply (auto simp add: odd_times_odd)
    1.64 +  done
    1.65 +
    1.66 +lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
    1.67 +  by presburger
    1.68 +
    1.69 +lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
    1.70 +  by presburger
    1.71 +
    1.72 +lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
    1.73 +  by presburger
    1.74 +
    1.75 +lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger
    1.76 +
    1.77 +lemma even_sum[presburger]: "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
    1.78 +  by presburger
    1.79 +
    1.80 +lemma even_neg[presburger, algebra]: "even (-(x::int)) = even x" by presburger
    1.81 +
    1.82 +lemma even_difference:
    1.83 +    "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger
    1.84 +
    1.85 +lemma even_pow_gt_zero:
    1.86 +    "even (x::int) ==> 0 < n ==> even (x^n)"
    1.87 +  by (induct n) (auto simp add: even_product)
    1.88 +
    1.89 +lemma odd_pow_iff[presburger, algebra]: 
    1.90 +  "odd ((x::int) ^ n) \<longleftrightarrow> (n = 0 \<or> odd x)"
    1.91 +  apply (induct n, simp_all)
    1.92 +  apply presburger
    1.93 +  apply (case_tac n, auto)
    1.94 +  apply (simp_all add: even_product)
    1.95 +  done
    1.96 +
    1.97 +lemma odd_pow: "odd x ==> odd((x::int)^n)" by (simp add: odd_pow_iff)
    1.98 +
    1.99 +lemma even_power[presburger]: "even ((x::int)^n) = (even x & 0 < n)"
   1.100 +  apply (auto simp add: even_pow_gt_zero)
   1.101 +  apply (erule contrapos_pp, erule odd_pow)
   1.102 +  apply (erule contrapos_pp, simp add: even_def)
   1.103 +  done
   1.104 +
   1.105 +lemma even_zero[presburger]: "even (0::int)" by presburger
   1.106 +
   1.107 +lemma odd_one[presburger]: "odd (1::int)" by presburger
   1.108 +
   1.109 +lemmas even_odd_simps [simp] = even_def[of "number_of v",standard] even_zero
   1.110 +  odd_one even_product even_sum even_neg even_difference even_power
   1.111 +
   1.112 +
   1.113 +subsection {* Equivalent definitions *}
   1.114 +
   1.115 +lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" 
   1.116 +  by presburger
   1.117 +
   1.118 +lemma two_times_odd_div_two_plus_one: "odd (x::int) ==>
   1.119 +    2 * (x div 2) + 1 = x" by presburger
   1.120 +
   1.121 +lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger
   1.122 +
   1.123 +lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger
   1.124 +
   1.125 +subsection {* even and odd for nats *}
   1.126 +
   1.127 +lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
   1.128 +  by (simp add: even_nat_def)
   1.129 +
   1.130 +lemma even_nat_product[presburger, algebra]: "even((x::nat) * y) = (even x | even y)"
   1.131 +  by (simp add: even_nat_def int_mult)
   1.132 +
   1.133 +lemma even_nat_sum[presburger, algebra]: "even ((x::nat) + y) =
   1.134 +    ((even x & even y) | (odd x & odd y))" by presburger
   1.135 +
   1.136 +lemma even_nat_difference[presburger, algebra]:
   1.137 +    "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
   1.138 +by presburger
   1.139 +
   1.140 +lemma even_nat_Suc[presburger, algebra]: "even (Suc x) = odd x" by presburger
   1.141 +
   1.142 +lemma even_nat_power[presburger, algebra]: "even ((x::nat)^y) = (even x & 0 < y)"
   1.143 +  by (simp add: even_nat_def int_power)
   1.144 +
   1.145 +lemma even_nat_zero[presburger]: "even (0::nat)" by presburger
   1.146 +
   1.147 +lemmas even_odd_nat_simps [simp] = even_nat_def[of "number_of v",standard]
   1.148 +  even_nat_zero even_nat_Suc even_nat_product even_nat_sum even_nat_power
   1.149 +
   1.150 +
   1.151 +subsection {* Equivalent definitions *}
   1.152 +
   1.153 +lemma nat_lt_two_imp_zero_or_one: "(x::nat) < Suc (Suc 0) ==>
   1.154 +    x = 0 | x = Suc 0" by presburger
   1.155 +
   1.156 +lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
   1.157 +  by presburger
   1.158 +
   1.159 +lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
   1.160 +by presburger
   1.161 +
   1.162 +lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
   1.163 +  by presburger
   1.164 +
   1.165 +lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
   1.166 +  by presburger
   1.167 +
   1.168 +lemma even_nat_div_two_times_two: "even (x::nat) ==>
   1.169 +    Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger
   1.170 +
   1.171 +lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
   1.172 +    Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger
   1.173 +
   1.174 +lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
   1.175 +  by presburger
   1.176 +
   1.177 +lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
   1.178 +  by presburger
   1.179 +
   1.180 +
   1.181 +subsection {* Parity and powers *}
   1.182 +
   1.183 +lemma  minus_one_even_odd_power:
   1.184 +     "(even x --> (- 1::'a::{comm_ring_1,recpower})^x = 1) &
   1.185 +      (odd x --> (- 1::'a)^x = - 1)"
   1.186 +  apply (induct x)
   1.187 +  apply (rule conjI)
   1.188 +  apply simp
   1.189 +  apply (insert even_nat_zero, blast)
   1.190 +  apply (simp add: power_Suc)
   1.191 +  done
   1.192 +
   1.193 +lemma minus_one_even_power [simp]:
   1.194 +    "even x ==> (- 1::'a::{comm_ring_1,recpower})^x = 1"
   1.195 +  using minus_one_even_odd_power by blast
   1.196 +
   1.197 +lemma minus_one_odd_power [simp]:
   1.198 +    "odd x ==> (- 1::'a::{comm_ring_1,recpower})^x = - 1"
   1.199 +  using minus_one_even_odd_power by blast
   1.200 +
   1.201 +lemma neg_one_even_odd_power:
   1.202 +     "(even x --> (-1::'a::{number_ring,recpower})^x = 1) &
   1.203 +      (odd x --> (-1::'a)^x = -1)"
   1.204 +  apply (induct x)
   1.205 +  apply (simp, simp add: power_Suc)
   1.206 +  done
   1.207 +
   1.208 +lemma neg_one_even_power [simp]:
   1.209 +    "even x ==> (-1::'a::{number_ring,recpower})^x = 1"
   1.210 +  using neg_one_even_odd_power by blast
   1.211 +
   1.212 +lemma neg_one_odd_power [simp]:
   1.213 +    "odd x ==> (-1::'a::{number_ring,recpower})^x = -1"
   1.214 +  using neg_one_even_odd_power by blast
   1.215 +
   1.216 +lemma neg_power_if:
   1.217 +     "(-x::'a::{comm_ring_1,recpower}) ^ n =
   1.218 +      (if even n then (x ^ n) else -(x ^ n))"
   1.219 +  apply (induct n)
   1.220 +  apply (simp_all split: split_if_asm add: power_Suc)
   1.221 +  done
   1.222 +
   1.223 +lemma zero_le_even_power: "even n ==>
   1.224 +    0 <= (x::'a::{recpower,ordered_ring_strict}) ^ n"
   1.225 +  apply (simp add: even_nat_equiv_def2)
   1.226 +  apply (erule exE)
   1.227 +  apply (erule ssubst)
   1.228 +  apply (subst power_add)
   1.229 +  apply (rule zero_le_square)
   1.230 +  done
   1.231 +
   1.232 +lemma zero_le_odd_power: "odd n ==>
   1.233 +    (0 <= (x::'a::{recpower,ordered_idom}) ^ n) = (0 <= x)"
   1.234 +  apply (simp add: odd_nat_equiv_def2)
   1.235 +  apply (erule exE)
   1.236 +  apply (erule ssubst)
   1.237 +  apply (subst power_Suc)
   1.238 +  apply (subst power_add)
   1.239 +  apply (subst zero_le_mult_iff)
   1.240 +  apply auto
   1.241 +  apply (subgoal_tac "x = 0 & y > 0")
   1.242 +  apply (erule conjE, assumption)
   1.243 +  apply (subst power_eq_0_iff [symmetric])
   1.244 +  apply (subgoal_tac "0 <= x^y * x^y")
   1.245 +  apply simp
   1.246 +  apply (rule zero_le_square)+
   1.247 +  done
   1.248 +
   1.249 +lemma zero_le_power_eq[presburger]: "(0 <= (x::'a::{recpower,ordered_idom}) ^ n) =
   1.250 +    (even n | (odd n & 0 <= x))"
   1.251 +  apply auto
   1.252 +  apply (subst zero_le_odd_power [symmetric])
   1.253 +  apply assumption+
   1.254 +  apply (erule zero_le_even_power)
   1.255 +  done
   1.256 +
   1.257 +lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{recpower,ordered_idom}) ^ n) =
   1.258 +    (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
   1.259 +
   1.260 +  unfolding order_less_le zero_le_power_eq by auto
   1.261 +
   1.262 +lemma power_less_zero_eq[presburger]: "((x::'a::{recpower,ordered_idom}) ^ n < 0) =
   1.263 +    (odd n & x < 0)"
   1.264 +  apply (subst linorder_not_le [symmetric])+
   1.265 +  apply (subst zero_le_power_eq)
   1.266 +  apply auto
   1.267 +  done
   1.268 +
   1.269 +lemma power_le_zero_eq[presburger]: "((x::'a::{recpower,ordered_idom}) ^ n <= 0) =
   1.270 +    (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
   1.271 +  apply (subst linorder_not_less [symmetric])+
   1.272 +  apply (subst zero_less_power_eq)
   1.273 +  apply auto
   1.274 +  done
   1.275 +
   1.276 +lemma power_even_abs: "even n ==>
   1.277 +    (abs (x::'a::{recpower,ordered_idom}))^n = x^n"
   1.278 +  apply (subst power_abs [symmetric])
   1.279 +  apply (simp add: zero_le_even_power)
   1.280 +  done
   1.281 +
   1.282 +lemma zero_less_power_nat_eq[presburger]: "(0 < (x::nat) ^ n) = (n = 0 | 0 < x)"
   1.283 +  by (induct n) auto
   1.284 +
   1.285 +lemma power_minus_even [simp]: "even n ==>
   1.286 +    (- x)^n = (x^n::'a::{recpower,comm_ring_1})"
   1.287 +  apply (subst power_minus)
   1.288 +  apply simp
   1.289 +  done
   1.290 +
   1.291 +lemma power_minus_odd [simp]: "odd n ==>
   1.292 +    (- x)^n = - (x^n::'a::{recpower,comm_ring_1})"
   1.293 +  apply (subst power_minus)
   1.294 +  apply simp
   1.295 +  done
   1.296 +
   1.297 +
   1.298 +subsection {* General Lemmas About Division *}
   1.299 +
   1.300 +lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1" 
   1.301 +apply (induct "m")
   1.302 +apply (simp_all add: mod_Suc)
   1.303 +done
   1.304 +
   1.305 +declare Suc_times_mod_eq [of "number_of w", standard, simp]
   1.306 +
   1.307 +lemma [simp]: "n div k \<le> (Suc n) div k"
   1.308 +by (simp add: div_le_mono) 
   1.309 +
   1.310 +lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"
   1.311 +by arith
   1.312 +
   1.313 +lemma div_2_gt_zero [simp]: "(1::nat) < n ==> 0 < n div 2" 
   1.314 +by arith
   1.315 +
   1.316 +  (* Potential use of algebra : Equality modulo n*)
   1.317 +lemma mod_mult_self3 [simp]: "(k*n + m) mod n = m mod (n::nat)"
   1.318 +by (simp add: mult_ac add_ac)
   1.319 +
   1.320 +lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"
   1.321 +proof -
   1.322 +  have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp
   1.323 +  also have "... = Suc m mod n" by (rule mod_mult_self3) 
   1.324 +  finally show ?thesis .
   1.325 +qed
   1.326 +
   1.327 +lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"
   1.328 +apply (subst mod_Suc [of m]) 
   1.329 +apply (subst mod_Suc [of "m mod n"], simp) 
   1.330 +done
   1.331 +
   1.332 +
   1.333 +subsection {* More Even/Odd Results *}
   1.334 + 
   1.335 +lemma even_mult_two_ex: "even(n) = (\<exists>m::nat. n = 2*m)" by presburger
   1.336 +lemma odd_Suc_mult_two_ex: "odd(n) = (\<exists>m. n = Suc (2*m))" by presburger
   1.337 +lemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburger
   1.338 +
   1.339 +lemma odd_add [simp]: "odd(m + n::nat) = (odd m \<noteq> odd n)" by presburger
   1.340 +
   1.341 +lemma div_Suc: "Suc a div c = a div c + Suc 0 div c +
   1.342 +    (a mod c + Suc 0 mod c) div c" 
   1.343 +  apply (subgoal_tac "Suc a = a + Suc 0")
   1.344 +  apply (erule ssubst)
   1.345 +  apply (rule div_add1_eq, simp)
   1.346 +  done
   1.347 +
   1.348 +lemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburger
   1.349 +
   1.350 +lemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"
   1.351 +by presburger
   1.352 +
   1.353 +lemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburger
   1.354 +lemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburger
   1.355 +
   1.356 +lemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburger
   1.357 +
   1.358 +lemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"
   1.359 +  by presburger
   1.360 +
   1.361 +text {* Simplify, when the exponent is a numeral *}
   1.362 +
   1.363 +lemmas power_0_left_number_of = power_0_left [of "number_of w", standard]
   1.364 +declare power_0_left_number_of [simp]
   1.365 +
   1.366 +lemmas zero_le_power_eq_number_of [simp] =
   1.367 +    zero_le_power_eq [of _ "number_of w", standard]
   1.368 +
   1.369 +lemmas zero_less_power_eq_number_of [simp] =
   1.370 +    zero_less_power_eq [of _ "number_of w", standard]
   1.371 +
   1.372 +lemmas power_le_zero_eq_number_of [simp] =
   1.373 +    power_le_zero_eq [of _ "number_of w", standard]
   1.374 +
   1.375 +lemmas power_less_zero_eq_number_of [simp] =
   1.376 +    power_less_zero_eq [of _ "number_of w", standard]
   1.377 +
   1.378 +lemmas zero_less_power_nat_eq_number_of [simp] =
   1.379 +    zero_less_power_nat_eq [of _ "number_of w", standard]
   1.380 +
   1.381 +lemmas power_eq_0_iff_number_of [simp] = power_eq_0_iff [of _ "number_of w", standard]
   1.382 +
   1.383 +lemmas power_even_abs_number_of [simp] = power_even_abs [of "number_of w" _, standard]
   1.384 +
   1.385 +
   1.386 +subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
   1.387 +
   1.388 +lemma even_power_le_0_imp_0:
   1.389 +    "a ^ (2*k) \<le> (0::'a::{ordered_idom,recpower}) ==> a=0"
   1.390 +  by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff power_Suc)
   1.391 +
   1.392 +lemma zero_le_power_iff[presburger]:
   1.393 +  "(0 \<le> a^n) = (0 \<le> (a::'a::{ordered_idom,recpower}) | even n)"
   1.394 +proof cases
   1.395 +  assume even: "even n"
   1.396 +  then obtain k where "n = 2*k"
   1.397 +    by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
   1.398 +  thus ?thesis by (simp add: zero_le_even_power even)
   1.399 +next
   1.400 +  assume odd: "odd n"
   1.401 +  then obtain k where "n = Suc(2*k)"
   1.402 +    by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
   1.403 +  thus ?thesis
   1.404 +    by (auto simp add: power_Suc zero_le_mult_iff zero_le_even_power
   1.405 +             dest!: even_power_le_0_imp_0)
   1.406 +qed
   1.407 +
   1.408 +
   1.409 +subsection {* Miscellaneous *}
   1.410 +
   1.411 +lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
   1.412 +
   1.413 +lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
   1.414 +lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
   1.415 +lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
   1.416 +lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger
   1.417 +
   1.418 +lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
   1.419 +lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
   1.420 +lemma even_nat_plus_one_div_two: "even (x::nat) ==>
   1.421 +    (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger
   1.422 +
   1.423 +lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
   1.424 +    (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger
   1.425 +
   1.426 +end