src/HOL/Parity.thy
author huffman
Tue Mar 27 15:53:48 2012 +0200 (2012-03-27)
changeset 47163 248376f8881d
parent 47108 2a1953f0d20d
child 47224 773fe2754b8c
permissions -rw-r--r--
remove redundant lemma
     1 (*  Title:      HOL/Parity.thy
     2     Author:     Jeremy Avigad
     3     Author:     Jacques D. Fleuriot
     4 *)
     5 
     6 header {* Even and Odd for int and nat *}
     7 
     8 theory Parity
     9 imports Main
    10 begin
    11 
    12 class even_odd = 
    13   fixes even :: "'a \<Rightarrow> bool"
    14 
    15 abbreviation
    16   odd :: "'a\<Colon>even_odd \<Rightarrow> bool" where
    17   "odd x \<equiv> \<not> even x"
    18 
    19 instantiation nat and int  :: even_odd
    20 begin
    21 
    22 definition
    23   even_def [presburger]: "even x \<longleftrightarrow> (x\<Colon>int) mod 2 = 0"
    24 
    25 definition
    26   even_nat_def [presburger]: "even x \<longleftrightarrow> even (int x)"
    27 
    28 instance ..
    29 
    30 end
    31 
    32 lemma transfer_int_nat_relations:
    33   "even (int x) \<longleftrightarrow> even x"
    34   by (simp add: even_nat_def)
    35 
    36 declare transfer_morphism_int_nat[transfer add return:
    37   transfer_int_nat_relations
    38 ]
    39 
    40 lemma even_zero_int[simp]: "even (0::int)" by presburger
    41 
    42 lemma odd_one_int[simp]: "odd (1::int)" by presburger
    43 
    44 lemma even_zero_nat[simp]: "even (0::nat)" by presburger
    45 
    46 lemma odd_1_nat [simp]: "odd (1::nat)" by presburger
    47 
    48 (* TODO: proper simp rules for Num.Bit0, Num.Bit1 *)
    49 declare even_def[of "numeral v", simp] for v
    50 declare even_def[of "neg_numeral v", simp] for v
    51 
    52 declare even_nat_def[of "numeral v", simp] for v
    53 
    54 subsection {* Even and odd are mutually exclusive *}
    55 
    56 lemma int_pos_lt_two_imp_zero_or_one:
    57     "0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"
    58   by presburger
    59 
    60 lemma neq_one_mod_two [simp, presburger]: 
    61   "((x::int) mod 2 ~= 0) = (x mod 2 = 1)" by presburger
    62 
    63 
    64 subsection {* Behavior under integer arithmetic operations *}
    65 declare dvd_def[algebra]
    66 lemma nat_even_iff_2_dvd[algebra]: "even (x::nat) \<longleftrightarrow> 2 dvd x"
    67   by presburger
    68 lemma int_even_iff_2_dvd[algebra]: "even (x::int) \<longleftrightarrow> 2 dvd x"
    69   by presburger
    70 
    71 lemma even_times_anything: "even (x::int) ==> even (x * y)"
    72   by algebra
    73 
    74 lemma anything_times_even: "even (y::int) ==> even (x * y)" by algebra
    75 
    76 lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)" 
    77   by (simp add: even_def mod_mult_right_eq)
    78 
    79 lemma even_product[simp,presburger]: "even((x::int) * y) = (even x | even y)"
    80   apply (auto simp add: even_times_anything anything_times_even)
    81   apply (rule ccontr)
    82   apply (auto simp add: odd_times_odd)
    83   done
    84 
    85 lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
    86 by presburger
    87 
    88 lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
    89 by presburger
    90 
    91 lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
    92 by presburger
    93 
    94 lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger
    95 
    96 lemma even_sum[simp,presburger]:
    97   "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
    98 by presburger
    99 
   100 lemma even_neg[simp,presburger,algebra]: "even (-(x::int)) = even x"
   101 by presburger
   102 
   103 lemma even_difference[simp]:
   104     "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger
   105 
   106 lemma even_power[simp,presburger]: "even ((x::int)^n) = (even x & n \<noteq> 0)"
   107 by (induct n) auto
   108 
   109 lemma odd_pow: "odd x ==> odd((x::int)^n)" by simp
   110 
   111 
   112 subsection {* Equivalent definitions *}
   113 
   114 lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" 
   115 by presburger
   116 
   117 lemma two_times_odd_div_two_plus_one:
   118   "odd (x::int) ==> 2 * (x div 2) + 1 = x"
   119 by presburger
   120 
   121 lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger
   122 
   123 lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger
   124 
   125 subsection {* even and odd for nats *}
   126 
   127 lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
   128 by (simp add: even_nat_def)
   129 
   130 lemma even_product_nat[simp,presburger,algebra]:
   131   "even((x::nat) * y) = (even x | even y)"
   132 by (simp add: even_nat_def int_mult)
   133 
   134 lemma even_sum_nat[simp,presburger,algebra]:
   135   "even ((x::nat) + y) = ((even x & even y) | (odd x & odd y))"
   136 by presburger
   137 
   138 lemma even_difference_nat[simp,presburger,algebra]:
   139   "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
   140 by presburger
   141 
   142 lemma even_Suc[simp,presburger,algebra]: "even (Suc x) = odd x"
   143 by presburger
   144 
   145 lemma even_power_nat[simp,presburger,algebra]:
   146   "even ((x::nat)^y) = (even x & 0 < y)"
   147 by (simp add: even_nat_def int_power)
   148 
   149 
   150 subsection {* Equivalent definitions *}
   151 
   152 lemma nat_lt_two_imp_zero_or_one:
   153   "(x::nat) < Suc (Suc 0) ==> x = 0 | x = Suc 0"
   154 by presburger
   155 
   156 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
   157 by presburger
   158 
   159 lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
   160 by presburger
   161 
   162 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
   163 by presburger
   164 
   165 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
   166 by presburger
   167 
   168 lemma even_nat_div_two_times_two: "even (x::nat) ==>
   169     Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger
   170 
   171 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
   172     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger
   173 
   174 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
   175 by presburger
   176 
   177 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
   178 by presburger
   179 
   180 
   181 subsection {* Parity and powers *}
   182 
   183 lemma  minus_one_even_odd_power:
   184      "(even x --> (- 1::'a::{comm_ring_1})^x = 1) &
   185       (odd x --> (- 1::'a)^x = - 1)"
   186   apply (induct x)
   187   apply (rule conjI)
   188   apply simp
   189   apply (insert even_zero_nat, blast)
   190   apply simp
   191   done
   192 
   193 lemma minus_one_even_power [simp]:
   194     "even x ==> (- 1::'a::{comm_ring_1})^x = 1"
   195   using minus_one_even_odd_power by blast
   196 
   197 lemma minus_one_odd_power [simp]:
   198     "odd x ==> (- 1::'a::{comm_ring_1})^x = - 1"
   199   using minus_one_even_odd_power by blast
   200 
   201 lemma neg_one_even_odd_power:
   202      "(even x --> (-1::'a::{comm_ring_1})^x = 1) &
   203       (odd x --> (-1::'a)^x = -1)"
   204   apply (induct x)
   205   apply (simp, simp)
   206   done
   207 
   208 lemma neg_one_even_power [simp]:
   209     "even x ==> (-1::'a::{comm_ring_1})^x = 1"
   210   using neg_one_even_odd_power by blast
   211 
   212 lemma neg_one_odd_power [simp]:
   213     "odd x ==> (-1::'a::{comm_ring_1})^x = -1"
   214   using neg_one_even_odd_power by blast
   215 
   216 lemma neg_power_if:
   217      "(-x::'a::{comm_ring_1}) ^ n =
   218       (if even n then (x ^ n) else -(x ^ n))"
   219   apply (induct n)
   220   apply simp_all
   221   done
   222 
   223 lemma zero_le_even_power: "even n ==>
   224     0 <= (x::'a::{linordered_ring,monoid_mult}) ^ n"
   225   apply (simp add: even_nat_equiv_def2)
   226   apply (erule exE)
   227   apply (erule ssubst)
   228   apply (subst power_add)
   229   apply (rule zero_le_square)
   230   done
   231 
   232 lemma zero_le_odd_power: "odd n ==>
   233     (0 <= (x::'a::{linordered_idom}) ^ n) = (0 <= x)"
   234 apply (auto simp: odd_nat_equiv_def2 power_add zero_le_mult_iff)
   235 apply (metis field_power_not_zero divisors_zero order_antisym_conv zero_le_square)
   236 done
   237 
   238 lemma zero_le_power_eq[presburger]: "(0 <= (x::'a::{linordered_idom}) ^ n) =
   239     (even n | (odd n & 0 <= x))"
   240   apply auto
   241   apply (subst zero_le_odd_power [symmetric])
   242   apply assumption+
   243   apply (erule zero_le_even_power)
   244   done
   245 
   246 lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{linordered_idom}) ^ n) =
   247     (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
   248 
   249   unfolding order_less_le zero_le_power_eq by auto
   250 
   251 lemma power_less_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n < 0) =
   252     (odd n & x < 0)"
   253   apply (subst linorder_not_le [symmetric])+
   254   apply (subst zero_le_power_eq)
   255   apply auto
   256   done
   257 
   258 lemma power_le_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n <= 0) =
   259     (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
   260   apply (subst linorder_not_less [symmetric])+
   261   apply (subst zero_less_power_eq)
   262   apply auto
   263   done
   264 
   265 lemma power_even_abs: "even n ==>
   266     (abs (x::'a::{linordered_idom}))^n = x^n"
   267   apply (subst power_abs [symmetric])
   268   apply (simp add: zero_le_even_power)
   269   done
   270 
   271 lemma zero_less_power_nat_eq[presburger]: "(0 < (x::nat) ^ n) = (n = 0 | 0 < x)"
   272   by (induct n) auto
   273 
   274 lemma power_minus_even [simp]: "even n ==>
   275     (- x)^n = (x^n::'a::{comm_ring_1})"
   276   apply (subst power_minus)
   277   apply simp
   278   done
   279 
   280 lemma power_minus_odd [simp]: "odd n ==>
   281     (- x)^n = - (x^n::'a::{comm_ring_1})"
   282   apply (subst power_minus)
   283   apply simp
   284   done
   285 
   286 lemma power_mono_even: fixes x y :: "'a :: {linordered_idom}"
   287   assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
   288   shows "x^n \<le> y^n"
   289 proof -
   290   have "0 \<le> \<bar>x\<bar>" by auto
   291   with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
   292   have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
   293   thus ?thesis unfolding power_even_abs[OF `even n`] .
   294 qed
   295 
   296 lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
   297 
   298 lemma power_mono_odd: fixes x y :: "'a :: {linordered_idom}"
   299   assumes "odd n" and "x \<le> y"
   300   shows "x^n \<le> y^n"
   301 proof (cases "y < 0")
   302   case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
   303   hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
   304   thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
   305 next
   306   case False
   307   show ?thesis
   308   proof (cases "x < 0")
   309     case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
   310     hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
   311     moreover
   312     from `\<not> y < 0` have "0 \<le> y" by auto
   313     hence "0 \<le> y^n" by auto
   314     ultimately show ?thesis by auto
   315   next
   316     case False hence "0 \<le> x" by auto
   317     with `x \<le> y` show ?thesis using power_mono by auto
   318   qed
   319 qed
   320 
   321 
   322 subsection {* More Even/Odd Results *}
   323  
   324 lemma even_mult_two_ex: "even(n) = (\<exists>m::nat. n = 2*m)" by presburger
   325 lemma odd_Suc_mult_two_ex: "odd(n) = (\<exists>m. n = Suc (2*m))" by presburger
   326 lemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburger
   327 
   328 lemma odd_add [simp]: "odd(m + n::nat) = (odd m \<noteq> odd n)" by presburger
   329 
   330 lemma div_Suc: "Suc a div c = a div c + Suc 0 div c +
   331     (a mod c + Suc 0 mod c) div c" 
   332   apply (subgoal_tac "Suc a = a + Suc 0")
   333   apply (erule ssubst)
   334   apply (rule div_add1_eq, simp)
   335   done
   336 
   337 lemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburger
   338 
   339 lemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"
   340 by presburger
   341 
   342 lemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburger
   343 lemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburger
   344 
   345 lemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburger
   346 
   347 lemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"
   348   by presburger
   349 
   350 text {* Simplify, when the exponent is a numeral *}
   351 
   352 lemma power_0_left_numeral [simp]:
   353   "0 ^ numeral w = (0::'a::{power,semiring_0})"
   354 by (simp add: power_0_left)
   355 
   356 lemmas zero_le_power_eq_numeral [simp] =
   357     zero_le_power_eq [of _ "numeral w"] for w
   358 
   359 lemmas zero_less_power_eq_numeral [simp] =
   360     zero_less_power_eq [of _ "numeral w"] for w
   361 
   362 lemmas power_le_zero_eq_numeral [simp] =
   363     power_le_zero_eq [of _ "numeral w"] for w
   364 
   365 lemmas power_less_zero_eq_numeral [simp] =
   366     power_less_zero_eq [of _ "numeral w"] for w
   367 
   368 lemmas zero_less_power_nat_eq_numeral [simp] =
   369     zero_less_power_nat_eq [of _ "numeral w"] for w
   370 
   371 lemmas power_eq_0_iff_numeral [simp] = power_eq_0_iff [of _ "numeral w"] for w
   372 
   373 lemmas power_even_abs_numeral [simp] = power_even_abs [of "numeral w" _] for w
   374 
   375 
   376 subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
   377 
   378 lemma even_power_le_0_imp_0:
   379     "a ^ (2*k) \<le> (0::'a::{linordered_idom}) ==> a=0"
   380   by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)
   381 
   382 lemma zero_le_power_iff[presburger]:
   383   "(0 \<le> a^n) = (0 \<le> (a::'a::{linordered_idom}) | even n)"
   384 proof cases
   385   assume even: "even n"
   386   then obtain k where "n = 2*k"
   387     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
   388   thus ?thesis by (simp add: zero_le_even_power even)
   389 next
   390   assume odd: "odd n"
   391   then obtain k where "n = Suc(2*k)"
   392     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
   393   thus ?thesis
   394     by (auto simp add: zero_le_mult_iff zero_le_even_power
   395              dest!: even_power_le_0_imp_0)
   396 qed
   397 
   398 
   399 subsection {* Miscellaneous *}
   400 
   401 lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
   402 lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
   403 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
   404 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger
   405 
   406 lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
   407 lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
   408 lemma even_nat_plus_one_div_two: "even (x::nat) ==>
   409     (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger
   410 
   411 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
   412     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger
   413 
   414 end