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