src/HOL/Parity.thy
author haftmann
Thu Oct 09 22:43:48 2014 +0200 (2014-10-09)
changeset 58645 94bef115c08f
parent 54489 03ff4d1e6784
child 58678 398e05aa84d4
permissions -rw-r--r--
more foundational definition for predicate even
     1 (*  Title:      HOL/Parity.thy
     2     Author:     Jeremy Avigad
     3     Author:     Jacques D. Fleuriot
     4 *)
     5 
     6 header {* Even and Odd for int and nat *}
     7 
     8 theory Parity
     9 imports Main
    10 begin
    11 
    12 class even_odd = semiring_div_parity
    13 begin
    14 
    15 definition even :: "'a \<Rightarrow> bool"
    16 where
    17   [algebra]: "even a \<longleftrightarrow> 2 dvd a"
    18 
    19 lemmas even_iff_2_dvd = even_def
    20 
    21 lemma even_iff_mod_2_eq_zero [presburger]:
    22   "even a \<longleftrightarrow> a mod 2 = 0"
    23   by (simp add: even_def dvd_eq_mod_eq_0)
    24 
    25 lemma even_zero [simp]:
    26   "even 0"
    27   by (simp add: even_iff_mod_2_eq_zero)
    28 
    29 lemma even_times_anything:
    30   "even a \<Longrightarrow> even (a * b)"
    31   by (simp add: even_iff_2_dvd)
    32 
    33 lemma anything_times_even:
    34   "even a \<Longrightarrow> even (b * a)"
    35   by (simp add: even_iff_2_dvd)
    36 
    37 abbreviation odd :: "'a \<Rightarrow> bool"
    38 where
    39   "odd a \<equiv> \<not> even a"
    40 
    41 lemma odd_times_odd:
    42   "odd a \<Longrightarrow> odd b \<Longrightarrow> odd (a * b)" 
    43   by (auto simp add: even_iff_mod_2_eq_zero mod_mult_left_eq)
    44 
    45 lemma even_product [simp, presburger]:
    46   "even (a * b) \<longleftrightarrow> even a \<or> even b"
    47   apply (auto simp add: even_times_anything anything_times_even)
    48   apply (rule ccontr)
    49   apply (auto simp add: odd_times_odd)
    50   done
    51 
    52 end
    53 
    54 instance nat and int  :: even_odd ..
    55 
    56 lemma even_nat_def [presburger]:
    57   "even x \<longleftrightarrow> even (int x)"
    58   by (auto simp add: even_iff_mod_2_eq_zero int_eq_iff int_mult nat_mult_distrib)
    59   
    60 lemma transfer_int_nat_relations:
    61   "even (int x) \<longleftrightarrow> even x"
    62   by (simp add: even_nat_def)
    63 
    64 declare transfer_morphism_int_nat[transfer add return:
    65   transfer_int_nat_relations
    66 ]
    67 
    68 lemma odd_one_int [simp]:
    69   "odd (1::int)"
    70   by presburger
    71 
    72 lemma odd_1_nat [simp]:
    73   "odd (1::nat)"
    74   by presburger
    75 
    76 lemma even_numeral_int [simp]: "even (numeral (Num.Bit0 k) :: int)"
    77   unfolding even_iff_mod_2_eq_zero by simp
    78 
    79 lemma odd_numeral_int [simp]: "odd (numeral (Num.Bit1 k) :: int)"
    80   unfolding even_iff_mod_2_eq_zero by simp
    81 
    82 (* TODO: proper simp rules for Num.Bit0, Num.Bit1 *)
    83 declare even_iff_mod_2_eq_zero [of "- numeral v", simp] for v
    84 
    85 lemma even_numeral_nat [simp]: "even (numeral (Num.Bit0 k) :: nat)"
    86   unfolding even_nat_def by simp
    87 
    88 lemma odd_numeral_nat [simp]: "odd (numeral (Num.Bit1 k) :: nat)"
    89   unfolding even_nat_def by simp
    90 
    91 subsection {* Even and odd are mutually exclusive *}
    92 
    93 
    94 subsection {* Behavior under integer arithmetic operations *}
    95 
    96 lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
    97 by presburger
    98 
    99 lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
   100 by presburger
   101 
   102 lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
   103 by presburger
   104 
   105 lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger
   106 
   107 lemma even_sum[simp,presburger]:
   108   "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
   109 by presburger
   110 
   111 lemma even_neg[simp,presburger,algebra]: "even (-(x::int)) = even x"
   112 by presburger
   113 
   114 lemma even_difference[simp]:
   115     "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger
   116 
   117 lemma even_power[simp,presburger]: "even ((x::int)^n) = (even x & n \<noteq> 0)"
   118 by (induct n) auto
   119 
   120 lemma odd_pow: "odd x ==> odd((x::int)^n)" by simp
   121 
   122 
   123 subsection {* Equivalent definitions *}
   124 
   125 lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" 
   126 by presburger
   127 
   128 lemma two_times_odd_div_two_plus_one:
   129   "odd (x::int) ==> 2 * (x div 2) + 1 = x"
   130 by presburger
   131 
   132 lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger
   133 
   134 lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger
   135 
   136 subsection {* even and odd for nats *}
   137 
   138 lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
   139 by (simp add: even_nat_def)
   140 
   141 lemma even_product_nat[simp,presburger,algebra]:
   142   "even((x::nat) * y) = (even x | even y)"
   143 by (simp add: even_nat_def int_mult)
   144 
   145 lemma even_sum_nat[simp,presburger,algebra]:
   146   "even ((x::nat) + y) = ((even x & even y) | (odd x & odd y))"
   147 by presburger
   148 
   149 lemma even_difference_nat[simp,presburger,algebra]:
   150   "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
   151 by presburger
   152 
   153 lemma even_Suc[simp,presburger,algebra]: "even (Suc x) = odd x"
   154 by presburger
   155 
   156 lemma even_power_nat[simp,presburger,algebra]:
   157   "even ((x::nat)^y) = (even x & 0 < y)"
   158 by (simp add: even_nat_def int_power)
   159 
   160 
   161 subsection {* Equivalent definitions *}
   162 
   163 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
   164 by presburger
   165 
   166 lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
   167 by presburger
   168 
   169 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
   170 by presburger
   171 
   172 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
   173 by presburger
   174 
   175 lemma even_nat_div_two_times_two: "even (x::nat) ==>
   176     Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger
   177 
   178 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
   179     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger
   180 
   181 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
   182 by presburger
   183 
   184 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
   185 by presburger
   186 
   187 
   188 subsection {* Parity and powers *}
   189 
   190 lemma (in comm_ring_1) neg_power_if:
   191   "(- a) ^ n = (if even n then (a ^ n) else - (a ^ n))"
   192   by (induct n) simp_all
   193 
   194 lemma (in comm_ring_1)
   195   shows neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
   196   and neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
   197   by (simp_all add: neg_power_if)
   198 
   199 lemma zero_le_even_power: "even n ==>
   200     0 <= (x::'a::{linordered_ring,monoid_mult}) ^ n"
   201   apply (simp add: even_nat_equiv_def2)
   202   apply (erule exE)
   203   apply (erule ssubst)
   204   apply (subst power_add)
   205   apply (rule zero_le_square)
   206   done
   207 
   208 lemma zero_le_odd_power: "odd n ==>
   209     (0 <= (x::'a::{linordered_idom}) ^ n) = (0 <= x)"
   210 apply (auto simp: odd_nat_equiv_def2 power_add zero_le_mult_iff)
   211 apply (metis field_power_not_zero divisors_zero order_antisym_conv zero_le_square)
   212 done
   213 
   214 lemma zero_le_power_eq [presburger]: "(0 <= (x::'a::{linordered_idom}) ^ n) =
   215     (even n | (odd n & 0 <= x))"
   216   apply auto
   217   apply (subst zero_le_odd_power [symmetric])
   218   apply assumption+
   219   apply (erule zero_le_even_power)
   220   done
   221 
   222 lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{linordered_idom}) ^ n) =
   223     (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
   224 
   225   unfolding order_less_le zero_le_power_eq by auto
   226 
   227 lemma power_less_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n < 0) =
   228     (odd n & x < 0)"
   229   apply (subst linorder_not_le [symmetric])+
   230   apply (subst zero_le_power_eq)
   231   apply auto
   232   done
   233 
   234 lemma power_le_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n <= 0) =
   235     (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
   236   apply (subst linorder_not_less [symmetric])+
   237   apply (subst zero_less_power_eq)
   238   apply auto
   239   done
   240 
   241 lemma power_even_abs: "even n ==>
   242     (abs (x::'a::{linordered_idom}))^n = x^n"
   243   apply (subst power_abs [symmetric])
   244   apply (simp add: zero_le_even_power)
   245   done
   246 
   247 lemma power_minus_even [simp]: "even n ==>
   248     (- x)^n = (x^n::'a::{comm_ring_1})"
   249   apply (subst power_minus)
   250   apply simp
   251   done
   252 
   253 lemma power_minus_odd [simp]: "odd n ==>
   254     (- x)^n = - (x^n::'a::{comm_ring_1})"
   255   apply (subst power_minus)
   256   apply simp
   257   done
   258 
   259 lemma power_mono_even: fixes x y :: "'a :: {linordered_idom}"
   260   assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
   261   shows "x^n \<le> y^n"
   262 proof -
   263   have "0 \<le> \<bar>x\<bar>" by auto
   264   with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
   265   have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
   266   thus ?thesis unfolding power_even_abs[OF `even n`] .
   267 qed
   268 
   269 lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
   270 
   271 lemma power_mono_odd: fixes x y :: "'a :: {linordered_idom}"
   272   assumes "odd n" and "x \<le> y"
   273   shows "x^n \<le> y^n"
   274 proof (cases "y < 0")
   275   case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
   276   hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
   277   thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
   278 next
   279   case False
   280   show ?thesis
   281   proof (cases "x < 0")
   282     case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
   283     hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
   284     moreover
   285     from `\<not> y < 0` have "0 \<le> y" by auto
   286     hence "0 \<le> y^n" by auto
   287     ultimately show ?thesis by auto
   288   next
   289     case False hence "0 \<le> x" by auto
   290     with `x \<le> y` show ?thesis using power_mono by auto
   291   qed
   292 qed
   293 
   294 
   295 subsection {* More Even/Odd Results *}
   296  
   297 lemma even_mult_two_ex: "even(n) = (\<exists>m::nat. n = 2*m)" by presburger
   298 lemma odd_Suc_mult_two_ex: "odd(n) = (\<exists>m. n = Suc (2*m))" by presburger
   299 lemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburger
   300 
   301 lemma odd_add [simp]: "odd(m + n::nat) = (odd m \<noteq> odd n)" by presburger
   302 
   303 lemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburger
   304 
   305 lemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"
   306 by presburger
   307 
   308 lemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburger
   309 lemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburger
   310 
   311 lemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburger
   312 
   313 lemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"
   314   by presburger
   315 
   316 text {* Simplify, when the exponent is a numeral *}
   317 
   318 lemmas zero_le_power_eq_numeral [simp] =
   319   zero_le_power_eq [of _ "numeral w"] for w
   320 
   321 lemmas zero_less_power_eq_numeral [simp] =
   322   zero_less_power_eq [of _ "numeral w"] for w
   323 
   324 lemmas power_le_zero_eq_numeral [simp] =
   325   power_le_zero_eq [of _ "numeral w"] for w
   326 
   327 lemmas power_less_zero_eq_numeral [simp] =
   328   power_less_zero_eq [of _ "numeral w"] for w
   329 
   330 lemmas zero_less_power_nat_eq_numeral [simp] =
   331   nat_zero_less_power_iff [of _ "numeral w"] for w
   332 
   333 lemmas power_eq_0_iff_numeral [simp] =
   334   power_eq_0_iff [of _ "numeral w"] for w
   335 
   336 lemmas power_even_abs_numeral [simp] =
   337   power_even_abs [of "numeral w" _] for w
   338 
   339 
   340 subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
   341 
   342 lemma zero_le_power_iff[presburger]:
   343   "(0 \<le> a^n) = (0 \<le> (a::'a::{linordered_idom}) | even n)"
   344 proof cases
   345   assume even: "even n"
   346   then obtain k where "n = 2*k"
   347     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
   348   thus ?thesis by (simp add: zero_le_even_power even)
   349 next
   350   assume odd: "odd n"
   351   then obtain k where "n = Suc(2*k)"
   352     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
   353   moreover have "a ^ (2 * k) \<le> 0 \<Longrightarrow> a = 0"
   354     by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)
   355   ultimately show ?thesis
   356     by (auto simp add: zero_le_mult_iff zero_le_even_power)
   357 qed
   358 
   359 
   360 subsection {* Miscellaneous *}
   361 
   362 lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
   363 lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
   364 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
   365 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger
   366 
   367 lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
   368 lemma even_nat_plus_one_div_two: "even (x::nat) ==>
   369     (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger
   370 
   371 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
   372     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger
   373 
   374 end
   375