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