src/HOL/Integ/Parity.thy
author paulson
Tue Jun 28 15:27:45 2005 +0200 (2005-06-28)
changeset 16587 b34c8aa657a5
parent 16413 47ffc49c7d7b
child 16775 c1b87ef4a1c3
permissions -rw-r--r--
Constant "If" is now local
     1 (*  Title:      Parity.thy
     2     ID:         $Id$
     3     Author:     Jeremy Avigad
     4 *)
     5 
     6 header {* Parity: 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 text{*Compatibility, in case Avigad uses this*}
   156 lemmas even_nat_suc = even_nat_Suc
   157 
   158 lemma even_nat_power: "even ((x::nat)^y) = (even x & 0 < y)"
   159   by (simp add: even_nat_def int_power)
   160 
   161 lemma even_nat_zero: "even (0::nat)"
   162   by (simp add: even_nat_def)
   163 
   164 lemmas even_odd_nat_simps [simp] = even_nat_def[of "number_of v",standard] 
   165   even_nat_zero even_nat_Suc even_nat_product even_nat_sum even_nat_power
   166 
   167 
   168 subsection {* Equivalent definitions *}
   169 
   170 lemma nat_lt_two_imp_zero_or_one: "(x::nat) < Suc (Suc 0) ==> 
   171     x = 0 | x = Suc 0"
   172   by auto
   173 
   174 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
   175   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
   176   apply (drule subst, assumption)
   177   apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
   178   apply force
   179   apply (subgoal_tac "0 < Suc (Suc 0)")
   180   apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
   181   apply (erule nat_lt_two_imp_zero_or_one, auto)
   182   done
   183 
   184 lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
   185   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
   186   apply (drule subst, assumption)
   187   apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
   188   apply force 
   189   apply (subgoal_tac "0 < Suc (Suc 0)")
   190   apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
   191   apply (erule nat_lt_two_imp_zero_or_one, auto)
   192   done
   193 
   194 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)" 
   195   apply (rule iffI)
   196   apply (erule even_nat_mod_two_eq_zero)
   197   apply (insert odd_nat_mod_two_eq_one [of x], auto)
   198   done
   199 
   200 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
   201   apply (auto simp add: even_nat_equiv_def)
   202   apply (subgoal_tac "x mod (Suc (Suc 0)) < Suc (Suc 0)")
   203   apply (frule nat_lt_two_imp_zero_or_one, auto)
   204   done
   205 
   206 lemma even_nat_div_two_times_two: "even (x::nat) ==> 
   207     Suc (Suc 0) * (x div Suc (Suc 0)) = x"
   208   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
   209   apply (drule even_nat_mod_two_eq_zero, simp)
   210   done
   211 
   212 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==> 
   213     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x"  
   214   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
   215   apply (drule odd_nat_mod_two_eq_one, simp)
   216   done
   217 
   218 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
   219   apply (rule iffI, rule exI)
   220   apply (erule even_nat_div_two_times_two [THEN sym], auto)
   221   done
   222 
   223 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
   224   apply (rule iffI, rule exI)
   225   apply (erule odd_nat_div_two_times_two_plus_one [THEN sym], auto)
   226   done
   227 
   228 subsection {* Powers of negative one *}
   229 
   230 lemma neg_one_even_odd_power:
   231      "(even x --> (-1::'a::{number_ring,recpower})^x = 1) & 
   232       (odd x --> (-1::'a)^x = -1)"
   233   apply (induct x)
   234   apply (simp, simp add: power_Suc)
   235   done
   236 
   237 lemma neg_one_even_power [simp]:
   238      "even x ==> (-1::'a::{number_ring,recpower})^x = 1"
   239   by (rule neg_one_even_odd_power [THEN conjunct1, THEN mp], assumption)
   240 
   241 lemma neg_one_odd_power [simp]:
   242      "odd x ==> (-1::'a::{number_ring,recpower})^x = -1"
   243   by (rule neg_one_even_odd_power [THEN conjunct2, THEN mp], assumption)
   244 
   245 lemma neg_power_if:
   246      "(-x::'a::{comm_ring_1,recpower}) ^ n = 
   247       (if even n then (x ^ n) else -(x ^ n))"
   248   by (induct n, simp_all split: split_if_asm add: power_Suc) 
   249 
   250 
   251 subsection {* An Equivalence for @{term "0 \<le> a^n"} *}
   252 
   253 lemma even_power_le_0_imp_0:
   254      "a ^ (2*k) \<le> (0::'a::{ordered_idom,recpower}) ==> a=0"
   255 apply (induct k) 
   256 apply (auto simp add: zero_le_mult_iff mult_le_0_iff power_Suc)  
   257 done
   258 
   259 lemma zero_le_power_iff:
   260      "(0 \<le> a^n) = (0 \<le> (a::'a::{ordered_idom,recpower}) | even n)"
   261       (is "?P n")
   262 proof cases
   263   assume even: "even n"
   264   then obtain k where "n = 2*k"
   265     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
   266   thus ?thesis by (simp add: zero_le_even_power even) 
   267 next
   268   assume odd: "odd n"
   269   then obtain k where "n = Suc(2*k)"
   270     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
   271   thus ?thesis
   272     by (auto simp add: power_Suc zero_le_mult_iff zero_le_even_power 
   273              dest!: even_power_le_0_imp_0) 
   274 qed 
   275 
   276 subsection {* Miscellaneous *}
   277 
   278 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"
   279   apply (subst zdiv_zadd1_eq)
   280   apply (simp add: even_def)
   281   done
   282 
   283 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1"
   284   apply (subst zdiv_zadd1_eq)
   285   apply (simp add: even_def)
   286   done
   287 
   288 lemma div_Suc: "Suc a div c = a div c + Suc 0 div c + 
   289     (a mod c + Suc 0 mod c) div c"
   290   apply (subgoal_tac "Suc a = a + Suc 0")
   291   apply (erule ssubst)
   292   apply (rule div_add1_eq, simp)
   293   done
   294 
   295 lemma even_nat_plus_one_div_two: "even (x::nat) ==> 
   296    (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)"
   297   apply (subst div_Suc)
   298   apply (simp add: even_nat_equiv_def)
   299   done
   300 
   301 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==> 
   302     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))"
   303   apply (subst div_Suc)
   304   apply (simp add: odd_nat_equiv_def)
   305   done
   306 
   307 end