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