src/HOL/Parity.thy
author haftmann
Tue Oct 14 08:23:23 2014 +0200 (2014-10-14)
changeset 58678 398e05aa84d4
parent 58645 94bef115c08f
child 58679 33c90658448a
permissions -rw-r--r--
purely algebraic characterization of even and odd
     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 subsection {* Preliminaries about divisibility on @{typ nat} and @{typ int} *}
    13 
    14 lemma two_dvd_Suc_Suc_iff [simp]:
    15   "2 dvd Suc (Suc n) \<longleftrightarrow> 2 dvd n"
    16   using dvd_add_triv_right_iff [of 2 n] by simp
    17 
    18 lemma two_dvd_Suc_iff:
    19   "2 dvd Suc n \<longleftrightarrow> \<not> 2 dvd n"
    20   by (induct n) auto
    21 
    22 lemma two_dvd_diff_iff:
    23   fixes k l :: int
    24   shows "2 dvd k - l \<longleftrightarrow> 2 dvd k + l"
    25   using dvd_add_times_triv_right_iff [of 2 "k - l" l] by (simp add: ac_simps)
    26 
    27 lemma two_dvd_abs_add_iff:
    28   fixes k l :: int
    29   shows "2 dvd \<bar>k\<bar> + l \<longleftrightarrow> 2 dvd k + l"
    30   by (cases "k \<ge> 0") (simp_all add: two_dvd_diff_iff ac_simps)
    31 
    32 lemma two_dvd_add_abs_iff:
    33   fixes k l :: int
    34   shows "2 dvd k + \<bar>l\<bar> \<longleftrightarrow> 2 dvd k + l"
    35   using two_dvd_abs_add_iff [of l k] by (simp add: ac_simps)
    36 
    37 
    38 subsection {* Ring structures with parity *}
    39 
    40 class semiring_parity = semiring_dvd + semiring_numeral +
    41   assumes two_not_dvd_one [simp]: "\<not> 2 dvd 1"
    42   assumes not_dvd_not_dvd_dvd_add: "\<not> 2 dvd a \<Longrightarrow> \<not> 2 dvd b \<Longrightarrow> 2 dvd a + b"
    43   assumes two_is_prime: "2 dvd a * b \<Longrightarrow> 2 dvd a \<or> 2 dvd b"
    44 begin
    45 
    46 lemma two_dvd_plus_one_iff [simp]:
    47   "2 dvd a + 1 \<longleftrightarrow> \<not> 2 dvd a"
    48   by (auto simp add: dvd_add_right_iff intro: not_dvd_not_dvd_dvd_add)
    49 
    50 end
    51 
    52 instance nat :: semiring_parity
    53 proof
    54   show "\<not> (2 :: nat) dvd 1"
    55     by (rule notI, erule dvdE) simp
    56 next
    57   fix m n :: nat
    58   assume "\<not> 2 dvd m"
    59   moreover assume "\<not> 2 dvd n"
    60   ultimately have *: "2 dvd Suc m \<and> 2 dvd Suc n"
    61     by (simp add: two_dvd_Suc_iff)
    62   then have "2 dvd Suc m + Suc n"
    63     by (blast intro: dvd_add)
    64   also have "Suc m + Suc n = m + n + 2"
    65     by simp
    66   finally show "2 dvd m + n"
    67     using dvd_add_triv_right_iff [of 2 "m + n"] by simp
    68 next
    69   fix m n :: nat
    70   assume *: "2 dvd m * n"
    71   show "2 dvd m \<or> 2 dvd n"
    72   proof (rule disjCI)
    73     assume "\<not> 2 dvd n"
    74     then have "2 dvd Suc n" by (simp add: two_dvd_Suc_iff)
    75     then obtain r where "Suc n = 2 * r" ..
    76     moreover from * obtain s where "m * n = 2 * s" ..
    77     then have "2 * s + m = m * Suc n" by simp
    78     ultimately have " 2 * s + m = 2 * (m * r)" by simp
    79     then have "m = 2 * (m * r - s)" by simp
    80     then show "2 dvd m" ..
    81   qed
    82 qed
    83 
    84 class ring_parity = comm_ring_1 + semiring_parity
    85 
    86 instance int :: ring_parity
    87 proof
    88   show "\<not> (2 :: int) dvd 1" by (simp add: dvd_int_unfold_dvd_nat)
    89   fix k l :: int
    90   assume "\<not> 2 dvd k"
    91   moreover assume "\<not> 2 dvd l"
    92   ultimately have "2 dvd nat \<bar>k\<bar> + nat \<bar>l\<bar>" 
    93     by (auto simp add: dvd_int_unfold_dvd_nat intro: not_dvd_not_dvd_dvd_add)
    94   then have "2 dvd \<bar>k\<bar> + \<bar>l\<bar>"
    95     by (simp add: dvd_int_unfold_dvd_nat nat_add_distrib)
    96   then show "2 dvd k + l"
    97     by (simp add: two_dvd_abs_add_iff two_dvd_add_abs_iff)
    98 qed (simp add: dvd_int_unfold_dvd_nat two_is_prime nat_abs_mult_distrib)
    99 
   100 context semiring_div_parity
   101 begin
   102 
   103 subclass semiring_parity
   104 proof (unfold_locales, unfold dvd_eq_mod_eq_0 not_mod_2_eq_0_eq_1)
   105   fix a b c
   106   show "(c * a + b) mod a = 0 \<longleftrightarrow> b mod a = 0"
   107     by simp
   108 next
   109   fix a b c
   110   assume "(b + c) mod a = 0"
   111   with mod_add_eq [of b c a]
   112   have "(b mod a + c mod a) mod a = 0"
   113     by simp
   114   moreover assume "b mod a = 0"
   115   ultimately show "c mod a = 0"
   116     by simp
   117 next
   118   show "1 mod 2 = 1"
   119     by (fact one_mod_two_eq_one)
   120 next
   121   fix a b
   122   assume "a mod 2 = 1"
   123   moreover assume "b mod 2 = 1"
   124   ultimately show "(a + b) mod 2 = 0"
   125     using mod_add_eq [of a b 2] by simp
   126 next
   127   fix a b
   128   assume "(a * b) mod 2 = 0"
   129   then have "(a mod 2) * (b mod 2) = 0"
   130     by (cases "a mod 2 = 0") (simp_all add: mod_mult_eq [of a b 2])
   131   then show "a mod 2 = 0 \<or> b mod 2 = 0"
   132     by (rule divisors_zero)
   133 qed
   134 
   135 end
   136 
   137 
   138 subsection {* Dedicated @{text even}/@{text odd} predicate *}
   139 
   140 context semiring_parity
   141 begin
   142 
   143 definition even :: "'a \<Rightarrow> bool"
   144 where
   145   [algebra]: "even a \<longleftrightarrow> 2 dvd a"
   146 
   147 abbreviation odd :: "'a \<Rightarrow> bool"
   148 where
   149   "odd a \<equiv> \<not> even a"
   150 
   151 lemma even_times_iff [simp, presburger, algebra]:
   152   "even (a * b) \<longleftrightarrow> even a \<or> even b"
   153   by (auto simp add: even_def dest: two_is_prime)
   154 
   155 lemma even_zero [simp]:
   156   "even 0"
   157   by (simp add: even_def)
   158 
   159 lemma odd_one [simp]:
   160   "odd 1"
   161   by (simp add: even_def)
   162 
   163 lemma even_numeral [simp]:
   164   "even (numeral (Num.Bit0 n))"
   165 proof -
   166   have "even (2 * numeral n)"
   167     unfolding even_times_iff by (simp add: even_def)
   168   then have "even (numeral n + numeral n)"
   169     unfolding mult_2 .
   170   then show ?thesis
   171     unfolding numeral.simps .
   172 qed
   173 
   174 lemma odd_numeral [simp]:
   175   "odd (numeral (Num.Bit1 n))"
   176 proof
   177   assume "even (numeral (num.Bit1 n))"
   178   then have "even (numeral n + numeral n + 1)"
   179     unfolding numeral.simps .
   180   then have "even (2 * numeral n + 1)"
   181     unfolding mult_2 .
   182   then have "2 dvd numeral n * 2 + 1"
   183     unfolding even_def by (simp add: ac_simps)
   184   with dvd_add_times_triv_left_iff [of 2 "numeral n" 1]
   185     have "2 dvd 1"
   186     by simp
   187   then show False by simp
   188 qed
   189 
   190 end
   191 
   192 context semiring_div_parity
   193 begin
   194 
   195 lemma even_iff_mod_2_eq_zero [presburger]:
   196   "even a \<longleftrightarrow> a mod 2 = 0"
   197   by (simp add: even_def dvd_eq_mod_eq_0)
   198 
   199 lemma even_times_anything:
   200   "even a \<Longrightarrow> even (a * b)"
   201   by (simp add: even_def)
   202 
   203 lemma anything_times_even:
   204   "even a \<Longrightarrow> even (b * a)"
   205   by (simp add: even_def)
   206 
   207 lemma odd_times_odd:
   208   "odd a \<Longrightarrow> odd b \<Longrightarrow> odd (a * b)" 
   209   by (auto simp add: even_iff_mod_2_eq_zero mod_mult_left_eq)
   210 
   211 lemma even_product:
   212   "even (a * b) \<longleftrightarrow> even a \<or> even b"
   213   by (fact even_times_iff)
   214 
   215 end
   216 
   217 lemma even_nat_def [presburger]:
   218   "even x \<longleftrightarrow> even (int x)"
   219   by (auto simp add: even_iff_mod_2_eq_zero int_eq_iff int_mult nat_mult_distrib)
   220   
   221 lemma transfer_int_nat_relations:
   222   "even (int x) \<longleftrightarrow> even x"
   223   by (simp add: even_nat_def)
   224 
   225 declare transfer_morphism_int_nat[transfer add return:
   226   transfer_int_nat_relations
   227 ]
   228 
   229 declare even_iff_mod_2_eq_zero [of "- numeral v", simp] for v
   230 
   231 
   232 subsection {* Behavior under integer arithmetic operations *}
   233 
   234 lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
   235 by presburger
   236 
   237 lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
   238 by presburger
   239 
   240 lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
   241 by presburger
   242 
   243 lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger
   244 
   245 lemma even_sum[simp,presburger]:
   246   "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
   247 by presburger
   248 
   249 lemma even_neg[simp,presburger,algebra]: "even (-(x::int)) = even x"
   250 by presburger
   251 
   252 lemma even_difference[simp]:
   253     "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger
   254 
   255 lemma even_power[simp,presburger]: "even ((x::int)^n) = (even x & n \<noteq> 0)"
   256 by (induct n) auto
   257 
   258 lemma odd_pow: "odd x ==> odd((x::int)^n)" by simp
   259 
   260 
   261 subsection {* Equivalent definitions *}
   262 
   263 lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" 
   264 by presburger
   265 
   266 lemma two_times_odd_div_two_plus_one:
   267   "odd (x::int) ==> 2 * (x div 2) + 1 = x"
   268 by presburger
   269   
   270 lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger
   271 
   272 lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger
   273 
   274 subsection {* even and odd for nats *}
   275 
   276 lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
   277 by (simp add: even_nat_def)
   278 
   279 lemma even_product_nat:
   280   "even((x::nat) * y) = (even x | even y)"
   281   by (fact even_times_iff)
   282 
   283 lemma even_sum_nat[simp,presburger,algebra]:
   284   "even ((x::nat) + y) = ((even x & even y) | (odd x & odd y))"
   285 by presburger
   286 
   287 lemma even_difference_nat[simp,presburger,algebra]:
   288   "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
   289 by presburger
   290 
   291 lemma even_Suc[simp,presburger,algebra]: "even (Suc x) = odd x"
   292 by presburger
   293 
   294 lemma even_power_nat[simp,presburger,algebra]:
   295   "even ((x::nat)^y) = (even x & 0 < y)"
   296 by (simp add: even_nat_def int_power)
   297 
   298 
   299 subsection {* Equivalent definitions *}
   300 
   301 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
   302 by presburger
   303 
   304 lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
   305 by presburger
   306 
   307 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
   308 by presburger
   309 
   310 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
   311 by presburger
   312 
   313 lemma even_nat_div_two_times_two: "even (x::nat) ==>
   314     Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger
   315 
   316 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
   317     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger
   318 
   319 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
   320 by presburger
   321 
   322 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
   323 by presburger
   324 
   325 
   326 subsection {* Parity and powers *}
   327 
   328 lemma (in comm_ring_1) neg_power_if:
   329   "(- a) ^ n = (if even n then (a ^ n) else - (a ^ n))"
   330   by (induct n) simp_all
   331 
   332 lemma (in comm_ring_1)
   333   shows neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
   334   and neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
   335   by (simp_all add: neg_power_if)
   336 
   337 lemma zero_le_even_power: "even n ==>
   338     0 <= (x::'a::{linordered_ring,monoid_mult}) ^ n"
   339   apply (simp add: even_nat_equiv_def2)
   340   apply (erule exE)
   341   apply (erule ssubst)
   342   apply (subst power_add)
   343   apply (rule zero_le_square)
   344   done
   345 
   346 lemma zero_le_odd_power: "odd n ==>
   347     (0 <= (x::'a::{linordered_idom}) ^ n) = (0 <= x)"
   348 apply (auto simp: odd_nat_equiv_def2 power_add zero_le_mult_iff)
   349 apply (metis field_power_not_zero divisors_zero order_antisym_conv zero_le_square)
   350 done
   351 
   352 lemma zero_le_power_eq [presburger]: "(0 <= (x::'a::{linordered_idom}) ^ n) =
   353     (even n | (odd n & 0 <= x))"
   354   apply auto
   355   apply (subst zero_le_odd_power [symmetric])
   356   apply assumption+
   357   apply (erule zero_le_even_power)
   358   done
   359 
   360 lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{linordered_idom}) ^ n) =
   361     (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
   362 
   363   unfolding order_less_le zero_le_power_eq by auto
   364 
   365 lemma power_less_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n < 0) =
   366     (odd n & x < 0)"
   367   apply (subst linorder_not_le [symmetric])+
   368   apply (subst zero_le_power_eq)
   369   apply auto
   370   done
   371 
   372 lemma power_le_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n <= 0) =
   373     (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
   374   apply (subst linorder_not_less [symmetric])+
   375   apply (subst zero_less_power_eq)
   376   apply auto
   377   done
   378 
   379 lemma power_even_abs: "even n ==>
   380     (abs (x::'a::{linordered_idom}))^n = x^n"
   381   apply (subst power_abs [symmetric])
   382   apply (simp add: zero_le_even_power)
   383   done
   384 
   385 lemma power_minus_even [simp]: "even n ==>
   386     (- x)^n = (x^n::'a::{comm_ring_1})"
   387   apply (subst power_minus)
   388   apply simp
   389   done
   390 
   391 lemma power_minus_odd [simp]: "odd n ==>
   392     (- x)^n = - (x^n::'a::{comm_ring_1})"
   393   apply (subst power_minus)
   394   apply simp
   395   done
   396 
   397 lemma power_mono_even: fixes x y :: "'a :: {linordered_idom}"
   398   assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
   399   shows "x^n \<le> y^n"
   400 proof -
   401   have "0 \<le> \<bar>x\<bar>" by auto
   402   with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
   403   have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
   404   thus ?thesis unfolding power_even_abs[OF `even n`] .
   405 qed
   406 
   407 lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
   408 
   409 lemma power_mono_odd: fixes x y :: "'a :: {linordered_idom}"
   410   assumes "odd n" and "x \<le> y"
   411   shows "x^n \<le> y^n"
   412 proof (cases "y < 0")
   413   case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
   414   hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
   415   thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
   416 next
   417   case False
   418   show ?thesis
   419   proof (cases "x < 0")
   420     case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
   421     hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
   422     moreover
   423     from `\<not> y < 0` have "0 \<le> y" by auto
   424     hence "0 \<le> y^n" by auto
   425     ultimately show ?thesis by auto
   426   next
   427     case False hence "0 \<le> x" by auto
   428     with `x \<le> y` show ?thesis using power_mono by auto
   429   qed
   430 qed
   431 
   432 
   433 subsection {* More Even/Odd Results *}
   434  
   435 lemma even_mult_two_ex: "even(n) = (\<exists>m::nat. n = 2*m)" by presburger
   436 lemma odd_Suc_mult_two_ex: "odd(n) = (\<exists>m. n = Suc (2*m))" by presburger
   437 lemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburger
   438 
   439 lemma odd_add [simp]: "odd(m + n::nat) = (odd m \<noteq> odd n)" by presburger
   440 
   441 lemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburger
   442 
   443 lemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"
   444 by presburger
   445 
   446 lemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburger
   447 lemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburger
   448 
   449 lemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburger
   450 
   451 lemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"
   452   by presburger
   453 
   454 text {* Simplify, when the exponent is a numeral *}
   455 
   456 lemmas zero_le_power_eq_numeral [simp] =
   457   zero_le_power_eq [of _ "numeral w"] for w
   458 
   459 lemmas zero_less_power_eq_numeral [simp] =
   460   zero_less_power_eq [of _ "numeral w"] for w
   461 
   462 lemmas power_le_zero_eq_numeral [simp] =
   463   power_le_zero_eq [of _ "numeral w"] for w
   464 
   465 lemmas power_less_zero_eq_numeral [simp] =
   466   power_less_zero_eq [of _ "numeral w"] for w
   467 
   468 lemmas zero_less_power_nat_eq_numeral [simp] =
   469   nat_zero_less_power_iff [of _ "numeral w"] for w
   470 
   471 lemmas power_eq_0_iff_numeral [simp] =
   472   power_eq_0_iff [of _ "numeral w"] for w
   473 
   474 lemmas power_even_abs_numeral [simp] =
   475   power_even_abs [of "numeral w" _] for w
   476 
   477 
   478 subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
   479 
   480 lemma zero_le_power_iff[presburger]:
   481   "(0 \<le> a^n) = (0 \<le> (a::'a::{linordered_idom}) | even n)"
   482 proof cases
   483   assume even: "even n"
   484   then obtain k where "n = 2*k"
   485     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
   486   thus ?thesis by (simp add: zero_le_even_power even)
   487 next
   488   assume odd: "odd n"
   489   then obtain k where "n = Suc(2*k)"
   490     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
   491   moreover have "a ^ (2 * k) \<le> 0 \<Longrightarrow> a = 0"
   492     by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)
   493   ultimately show ?thesis
   494     by (auto simp add: zero_le_mult_iff zero_le_even_power)
   495 qed
   496 
   497 
   498 subsection {* Miscellaneous *}
   499 
   500 lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
   501 lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
   502 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
   503 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger
   504 
   505 lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
   506 lemma even_nat_plus_one_div_two: "even (x::nat) ==>
   507     (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger
   508 
   509 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
   510     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger
   511 
   512 end
   513