src/HOL/Integ/Parity.thy
author nipkow
Mon Aug 16 14:22:27 2004 +0200 (2004-08-16)
changeset 15131 c69542757a4d
parent 15003 6145dd7538d7
child 15140 322485b816ac
permissions -rw-r--r--
New theory header syntax.
     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 import 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 {* Casting a nat power to an integer *}
    32 
    33 lemma zpow_int: "int (x^y) = (int x)^y"
    34   apply (induct_tac y)
    35   apply (simp, simp add: zmult_int [THEN sym])
    36   done
    37 
    38 subsection {* Even and odd are mutually exclusive *}
    39 
    40 lemma int_pos_lt_two_imp_zero_or_one: 
    41     "0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"
    42   by auto
    43 
    44 lemma neq_one_mod_two [simp]: "((x::int) mod 2 ~= 0) = (x mod 2 = 1)"
    45   apply (subgoal_tac "x mod 2 = 0 | x mod 2 = 1", force)
    46   apply (rule int_pos_lt_two_imp_zero_or_one, auto)
    47   done
    48 
    49 subsection {* Behavior under integer arithmetic operations *}
    50 
    51 lemma even_times_anything: "even (x::int) ==> even (x * y)"
    52   by (simp add: even_def zmod_zmult1_eq')
    53 
    54 lemma anything_times_even: "even (y::int) ==> even (x * y)"
    55   by (simp add: even_def zmod_zmult1_eq)
    56 
    57 lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)"
    58   by (simp add: even_def zmod_zmult1_eq)
    59 
    60 lemma even_product: "even((x::int) * y) = (even x | even y)"
    61   apply (auto simp add: even_times_anything anything_times_even) 
    62   apply (rule ccontr)
    63   apply (auto simp add: odd_times_odd)
    64   done
    65 
    66 lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
    67   by (simp add: even_def zmod_zadd1_eq)
    68 
    69 lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
    70   by (simp add: even_def zmod_zadd1_eq)
    71 
    72 lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
    73   by (simp add: even_def zmod_zadd1_eq)
    74 
    75 lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)"
    76   by (simp add: even_def zmod_zadd1_eq)
    77 
    78 lemma even_sum: "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
    79   apply (auto intro: even_plus_even odd_plus_odd)
    80   apply (rule ccontr, simp add: even_plus_odd)
    81   apply (rule ccontr, simp add: odd_plus_even)
    82   done
    83 
    84 lemma even_neg: "even (-(x::int)) = even x"
    85   by (auto simp add: even_def zmod_zminus1_eq_if)
    86 
    87 lemma even_difference: 
    88   "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))"
    89   by (simp only: diff_minus even_sum even_neg)
    90 
    91 lemma even_pow_gt_zero [rule_format]: 
    92     "even (x::int) ==> 0 < n --> even (x^n)"
    93   apply (induct_tac n)
    94   apply (auto simp add: even_product)
    95   done
    96 
    97 lemma odd_pow: "odd x ==> odd((x::int)^n)"
    98   apply (induct_tac n)
    99   apply (simp add: even_def)
   100   apply (simp add: even_product)
   101   done
   102 
   103 lemma even_power: "even ((x::int)^n) = (even x & 0 < n)"
   104   apply (auto simp add: even_pow_gt_zero) 
   105   apply (erule contrapos_pp, erule odd_pow)
   106   apply (erule contrapos_pp, simp add: even_def)
   107   done
   108 
   109 lemma even_zero: "even (0::int)"
   110   by (simp add: even_def)
   111 
   112 lemma odd_one: "odd (1::int)"
   113   by (simp add: even_def)
   114 
   115 lemmas even_odd_simps [simp] = even_def[of "number_of v",standard] even_zero 
   116   odd_one even_product even_sum even_neg even_difference even_power
   117 
   118 
   119 subsection {* Equivalent definitions *}
   120 
   121 lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" 
   122   by (auto simp add: even_def)
   123 
   124 lemma two_times_odd_div_two_plus_one: "odd (x::int) ==> 
   125     2 * (x div 2) + 1 = x"
   126   apply (insert zmod_zdiv_equality [of x 2, THEN sym])
   127   by (simp add: even_def)
   128 
   129 lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)"
   130   apply auto
   131   apply (rule exI)
   132   by (erule two_times_even_div_two [THEN sym])
   133 
   134 lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)"
   135   apply auto
   136   apply (rule exI)
   137   by (erule two_times_odd_div_two_plus_one [THEN sym])
   138 
   139 
   140 subsection {* even and odd for nats *}
   141 
   142 lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
   143   by (simp add: even_nat_def)
   144 
   145 lemma even_nat_product: "even((x::nat) * y) = (even x | even y)"
   146   by (simp add: even_nat_def zmult_int [THEN sym])
   147 
   148 lemma even_nat_sum: "even ((x::nat) + y) = 
   149     ((even x & even y) | (odd x & odd y))"
   150   by (unfold even_nat_def, simp)
   151 
   152 lemma even_nat_difference: 
   153     "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
   154   apply (auto simp add: even_nat_def zdiff_int [THEN sym])
   155   apply (case_tac "x < y", auto simp add: zdiff_int [THEN sym])
   156   apply (case_tac "x < y", auto simp add: zdiff_int [THEN sym])
   157   done
   158 
   159 lemma even_nat_Suc: "even (Suc x) = odd x"
   160   by (simp add: even_nat_def)
   161 
   162 text{*Compatibility, in case Avigad uses this*}
   163 lemmas even_nat_suc = even_nat_Suc
   164 
   165 lemma even_nat_power: "even ((x::nat)^y) = (even x & 0 < y)"
   166   by (simp add: even_nat_def zpow_int)
   167 
   168 lemma even_nat_zero: "even (0::nat)"
   169   by (simp add: even_nat_def)
   170 
   171 lemmas even_odd_nat_simps [simp] = even_nat_def[of "number_of v",standard] 
   172   even_nat_zero even_nat_Suc even_nat_product even_nat_sum even_nat_power
   173 
   174 
   175 subsection {* Equivalent definitions *}
   176 
   177 lemma nat_lt_two_imp_zero_or_one: "(x::nat) < Suc (Suc 0) ==> 
   178     x = 0 | x = Suc 0"
   179   by auto
   180 
   181 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 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 odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
   192   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
   193   apply (drule subst, assumption)
   194   apply (subgoal_tac "x mod Suc (Suc 0) = 0 | x mod Suc (Suc 0) = Suc 0")
   195   apply force 
   196   apply (subgoal_tac "0 < Suc (Suc 0)")
   197   apply (frule mod_less_divisor [of "Suc (Suc 0)" x])
   198   apply (erule nat_lt_two_imp_zero_or_one, auto)
   199   done
   200 
   201 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)" 
   202   apply (rule iffI)
   203   apply (erule even_nat_mod_two_eq_zero)
   204   apply (insert odd_nat_mod_two_eq_one [of x], auto)
   205   done
   206 
   207 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
   208   apply (auto simp add: even_nat_equiv_def)
   209   apply (subgoal_tac "x mod (Suc (Suc 0)) < Suc (Suc 0)")
   210   apply (frule nat_lt_two_imp_zero_or_one, auto)
   211   done
   212 
   213 lemma even_nat_div_two_times_two: "even (x::nat) ==> 
   214     Suc (Suc 0) * (x div Suc (Suc 0)) = x"
   215   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
   216   apply (drule even_nat_mod_two_eq_zero, simp)
   217   done
   218 
   219 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==> 
   220     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x"  
   221   apply (insert mod_div_equality [of x "Suc (Suc 0)", THEN sym])
   222   apply (drule odd_nat_mod_two_eq_one, simp)
   223   done
   224 
   225 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
   226   apply (rule iffI, rule exI)
   227   apply (erule even_nat_div_two_times_two [THEN sym], auto)
   228   done
   229 
   230 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
   231   apply (rule iffI, rule exI)
   232   apply (erule odd_nat_div_two_times_two_plus_one [THEN sym], auto)
   233   done
   234 
   235 subsection {* Powers of negative one *}
   236 
   237 lemma neg_one_even_odd_power:
   238      "(even x --> (-1::'a::{number_ring,recpower})^x = 1) & 
   239       (odd x --> (-1::'a)^x = -1)"
   240   apply (induct_tac x)
   241   apply (simp, simp add: power_Suc)
   242   done
   243 
   244 lemma neg_one_even_power [simp]:
   245      "even x ==> (-1::'a::{number_ring,recpower})^x = 1"
   246   by (rule neg_one_even_odd_power [THEN conjunct1, THEN mp], assumption)
   247 
   248 lemma neg_one_odd_power [simp]:
   249      "odd x ==> (-1::'a::{number_ring,recpower})^x = -1"
   250   by (rule neg_one_even_odd_power [THEN conjunct2, THEN mp], assumption)
   251 
   252 lemma neg_power_if:
   253      "(-x::'a::{comm_ring_1,recpower}) ^ n = 
   254       (if even n then (x ^ n) else -(x ^ n))"
   255   by (induct n, simp_all split: split_if_asm add: power_Suc) 
   256 
   257 
   258 subsection {* An Equivalence for @{term "0 \<le> a^n"} *}
   259 
   260 lemma even_power_le_0_imp_0:
   261      "a ^ (2*k) \<le> (0::'a::{ordered_idom,recpower}) ==> a=0"
   262 apply (induct k) 
   263 apply (auto simp add: zero_le_mult_iff mult_le_0_iff power_Suc)  
   264 done
   265 
   266 lemma zero_le_power_iff:
   267      "(0 \<le> a^n) = (0 \<le> (a::'a::{ordered_idom,recpower}) | even n)"
   268       (is "?P n")
   269 proof cases
   270   assume even: "even n"
   271   then obtain k where "n = 2*k"
   272     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
   273   thus ?thesis by (simp add: zero_le_even_power even) 
   274 next
   275   assume odd: "odd n"
   276   then obtain k where "n = Suc(2*k)"
   277     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
   278   thus ?thesis
   279     by (auto simp add: power_Suc zero_le_mult_iff zero_le_even_power 
   280              dest!: even_power_le_0_imp_0) 
   281 qed 
   282 
   283 subsection {* Miscellaneous *}
   284 
   285 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"
   286   apply (subst zdiv_zadd1_eq)
   287   apply (simp add: even_def)
   288   done
   289 
   290 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1"
   291   apply (subst zdiv_zadd1_eq)
   292   apply (simp add: even_def)
   293   done
   294 
   295 lemma div_Suc: "Suc a div c = a div c + Suc 0 div c + 
   296     (a mod c + Suc 0 mod c) div c"
   297   apply (subgoal_tac "Suc a = a + Suc 0")
   298   apply (erule ssubst)
   299   apply (rule div_add1_eq, simp)
   300   done
   301 
   302 lemma even_nat_plus_one_div_two: "even (x::nat) ==> 
   303    (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)"
   304   apply (subst div_Suc)
   305   apply (simp add: even_nat_equiv_def)
   306   done
   307 
   308 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==> 
   309     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))"
   310   apply (subst div_Suc)
   311   apply (simp add: odd_nat_equiv_def)
   312   done
   313 
   314 end