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