src/HOL/Parity.thy
author haftmann
Tue Oct 14 08:23:23 2014 +0200 (2014-10-14)
changeset 58679 33c90658448a
parent 58678 398e05aa84d4
child 58680 6b2fa479945f
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 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 ring_parity
   193 begin
   194 
   195 lemma even_minus [simp, presburger, algebra]:
   196   "even (- a) \<longleftrightarrow> even a"
   197   by (simp add: even_def)
   198 
   199 end
   200 
   201 context semiring_div_parity
   202 begin
   203 
   204 lemma even_iff_mod_2_eq_zero [presburger]:
   205   "even a \<longleftrightarrow> a mod 2 = 0"
   206   by (simp add: even_def dvd_eq_mod_eq_0)
   207 
   208 end
   209 
   210 lemma even_int_iff:
   211   "even (int n) \<longleftrightarrow> even n"
   212   by (simp add: even_def dvd_int_iff)
   213 
   214 declare transfer_morphism_int_nat [transfer add return:
   215   even_int_iff
   216 ]
   217 
   218 lemma [presburger]:
   219   "even n \<longleftrightarrow> even (int n)"
   220   using even_int_iff [of n] by simp
   221   
   222 
   223 subsection {* Behavior under integer arithmetic operations *}
   224 
   225 lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
   226 by presburger
   227 
   228 lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
   229 by presburger
   230 
   231 lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
   232 by presburger
   233 
   234 lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger
   235 
   236 lemma even_sum[simp,presburger]:
   237   "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
   238 by presburger
   239 
   240 lemma even_difference[simp]:
   241     "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger
   242 
   243 lemma even_power[simp,presburger]: "even ((x::int)^n) = (even x & n \<noteq> 0)"
   244 by (induct n) auto
   245 
   246 lemma odd_pow: "odd x ==> odd((x::int)^n)" by simp
   247 
   248 
   249 subsection {* Equivalent definitions *}
   250 
   251 lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" 
   252 by presburger
   253 
   254 lemma two_times_odd_div_two_plus_one:
   255   "odd (x::int) ==> 2 * (x div 2) + 1 = x"
   256 by presburger
   257   
   258 lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger
   259 
   260 lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger
   261 
   262 subsection {* even and odd for nats *}
   263 
   264 lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
   265 by (simp add: even_int_iff [symmetric])
   266 
   267 lemma even_sum_nat[simp,presburger,algebra]:
   268   "even ((x::nat) + y) = ((even x & even y) | (odd x & odd y))"
   269 by presburger
   270 
   271 lemma even_difference_nat[simp,presburger,algebra]:
   272   "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
   273 by presburger
   274 
   275 lemma even_Suc[simp,presburger,algebra]: "even (Suc x) = odd x"
   276 by presburger
   277 
   278 lemma even_power_nat[simp,presburger,algebra]:
   279   "even ((x::nat)^y) = (even x & 0 < y)"
   280 by (simp add: even_int_iff [symmetric] int_power)
   281 
   282 
   283 subsection {* Equivalent definitions *}
   284 
   285 lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
   286 by presburger
   287 
   288 lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
   289 by presburger
   290 
   291 lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
   292 by presburger
   293 
   294 lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
   295 by presburger
   296 
   297 lemma even_nat_div_two_times_two: "even (x::nat) ==>
   298     Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger
   299 
   300 lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
   301     Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger
   302 
   303 lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
   304 by presburger
   305 
   306 lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
   307 by presburger
   308 
   309 
   310 subsection {* Parity and powers *}
   311 
   312 lemma (in comm_ring_1) neg_power_if:
   313   "(- a) ^ n = (if even n then (a ^ n) else - (a ^ n))"
   314   by (induct n) simp_all
   315 
   316 lemma (in comm_ring_1)
   317   shows neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
   318   and neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
   319   by (simp_all add: neg_power_if)
   320 
   321 lemma zero_le_even_power: "even n ==>
   322     0 <= (x::'a::{linordered_ring,monoid_mult}) ^ n"
   323   apply (simp add: even_nat_equiv_def2)
   324   apply (erule exE)
   325   apply (erule ssubst)
   326   apply (subst power_add)
   327   apply (rule zero_le_square)
   328   done
   329 
   330 lemma zero_le_odd_power: "odd n ==>
   331     (0 <= (x::'a::{linordered_idom}) ^ n) = (0 <= x)"
   332 apply (auto simp: odd_nat_equiv_def2 power_add zero_le_mult_iff)
   333 apply (metis field_power_not_zero divisors_zero order_antisym_conv zero_le_square)
   334 done
   335 
   336 lemma zero_le_power_eq [presburger]: "(0 <= (x::'a::{linordered_idom}) ^ n) =
   337     (even n | (odd n & 0 <= x))"
   338   apply auto
   339   apply (subst zero_le_odd_power [symmetric])
   340   apply assumption+
   341   apply (erule zero_le_even_power)
   342   done
   343 
   344 lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{linordered_idom}) ^ n) =
   345     (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
   346 
   347   unfolding order_less_le zero_le_power_eq by auto
   348 
   349 lemma power_less_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n < 0) =
   350     (odd n & x < 0)"
   351   apply (subst linorder_not_le [symmetric])+
   352   apply (subst zero_le_power_eq)
   353   apply auto
   354   done
   355 
   356 lemma power_le_zero_eq[presburger]: "((x::'a::{linordered_idom}) ^ n <= 0) =
   357     (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
   358   apply (subst linorder_not_less [symmetric])+
   359   apply (subst zero_less_power_eq)
   360   apply auto
   361   done
   362 
   363 lemma power_even_abs: "even n ==>
   364     (abs (x::'a::{linordered_idom}))^n = x^n"
   365   apply (subst power_abs [symmetric])
   366   apply (simp add: zero_le_even_power)
   367   done
   368 
   369 lemma power_minus_even [simp]: "even n ==>
   370     (- x)^n = (x^n::'a::{comm_ring_1})"
   371   apply (subst power_minus)
   372   apply simp
   373   done
   374 
   375 lemma power_minus_odd [simp]: "odd n ==>
   376     (- x)^n = - (x^n::'a::{comm_ring_1})"
   377   apply (subst power_minus)
   378   apply simp
   379   done
   380 
   381 lemma power_mono_even: fixes x y :: "'a :: {linordered_idom}"
   382   assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
   383   shows "x^n \<le> y^n"
   384 proof -
   385   have "0 \<le> \<bar>x\<bar>" by auto
   386   with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
   387   have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
   388   thus ?thesis unfolding power_even_abs[OF `even n`] .
   389 qed
   390 
   391 lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
   392 
   393 lemma power_mono_odd: fixes x y :: "'a :: {linordered_idom}"
   394   assumes "odd n" and "x \<le> y"
   395   shows "x^n \<le> y^n"
   396 proof (cases "y < 0")
   397   case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
   398   hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
   399   thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
   400 next
   401   case False
   402   show ?thesis
   403   proof (cases "x < 0")
   404     case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
   405     hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
   406     moreover
   407     from `\<not> y < 0` have "0 \<le> y" by auto
   408     hence "0 \<le> y^n" by auto
   409     ultimately show ?thesis by auto
   410   next
   411     case False hence "0 \<le> x" by auto
   412     with `x \<le> y` show ?thesis using power_mono by auto
   413   qed
   414 qed
   415 
   416 
   417 subsection {* More Even/Odd Results *}
   418  
   419 lemma even_mult_two_ex: "even(n) = (\<exists>m::nat. n = 2*m)" by presburger
   420 lemma odd_Suc_mult_two_ex: "odd(n) = (\<exists>m. n = Suc (2*m))" by presburger
   421 lemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburger
   422 
   423 lemma odd_add [simp]: "odd(m + n::nat) = (odd m \<noteq> odd n)" by presburger
   424 
   425 lemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburger
   426 
   427 lemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"
   428 by presburger
   429 
   430 lemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburger
   431 lemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburger
   432 
   433 lemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburger
   434 
   435 lemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"
   436   by presburger
   437 
   438 text {* Simplify, when the exponent is a numeral *}
   439 
   440 lemmas zero_le_power_eq_numeral [simp] =
   441   zero_le_power_eq [of _ "numeral w"] for w
   442 
   443 lemmas zero_less_power_eq_numeral [simp] =
   444   zero_less_power_eq [of _ "numeral w"] for w
   445 
   446 lemmas power_le_zero_eq_numeral [simp] =
   447   power_le_zero_eq [of _ "numeral w"] for w
   448 
   449 lemmas power_less_zero_eq_numeral [simp] =
   450   power_less_zero_eq [of _ "numeral w"] for w
   451 
   452 lemmas zero_less_power_nat_eq_numeral [simp] =
   453   nat_zero_less_power_iff [of _ "numeral w"] for w
   454 
   455 lemmas power_eq_0_iff_numeral [simp] =
   456   power_eq_0_iff [of _ "numeral w"] for w
   457 
   458 lemmas power_even_abs_numeral [simp] =
   459   power_even_abs [of "numeral w" _] for w
   460 
   461 
   462 subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
   463 
   464 lemma zero_le_power_iff[presburger]:
   465   "(0 \<le> a^n) = (0 \<le> (a::'a::{linordered_idom}) | even n)"
   466 proof cases
   467   assume even: "even n"
   468   then obtain k where "n = 2*k"
   469     by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
   470   thus ?thesis by (simp add: zero_le_even_power even)
   471 next
   472   assume odd: "odd n"
   473   then obtain k where "n = Suc(2*k)"
   474     by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
   475   moreover have "a ^ (2 * k) \<le> 0 \<Longrightarrow> a = 0"
   476     by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)
   477   ultimately show ?thesis
   478     by (auto simp add: zero_le_mult_iff zero_le_even_power)
   479 qed
   480 
   481 
   482 subsection {* Miscellaneous *}
   483 
   484 lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
   485 lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
   486 lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
   487 lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger
   488 
   489 lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
   490 lemma even_nat_plus_one_div_two: "even (x::nat) ==>
   491     (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger
   492 
   493 lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
   494     (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger
   495 
   496 end
   497