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