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