src/HOL/Parity.thy
 author haftmann Tue Oct 21 21:10:44 2014 +0200 (2014-10-21) changeset 58740 cb9d84d3e7f2 parent 58718 48395c763059 child 58769 70fff47875cd permissions -rw-r--r--
turn even into an abbreviation
```     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 abbreviation even :: "'a \<Rightarrow> bool"
```
```   193 where
```
```   194   "even a \<equiv> 2 dvd a"
```
```   195
```
```   196 abbreviation odd :: "'a \<Rightarrow> bool"
```
```   197 where
```
```   198   "odd a \<equiv> \<not> 2 dvd a"
```
```   199
```
```   200 lemma evenE [elim?]:
```
```   201   assumes "even a"
```
```   202   obtains b where "a = 2 * b"
```
```   203   using assms by (rule dvdE)
```
```   204
```
```   205 lemma oddE [elim?]:
```
```   206   assumes "odd a"
```
```   207   obtains b where "a = 2 * b + 1"
```
```   208   using assms by (rule not_two_dvdE)
```
```   209
```
```   210 lemma even_times_iff [simp, presburger, algebra]:
```
```   211   "even (a * b) \<longleftrightarrow> even a \<or> even b"
```
```   212   by (auto simp add: dest: two_is_prime)
```
```   213
```
```   214 lemma even_zero [simp]:
```
```   215   "even 0"
```
```   216   by simp
```
```   217
```
```   218 lemma odd_one [simp]:
```
```   219   "odd 1"
```
```   220   by simp
```
```   221
```
```   222 lemma even_numeral [simp]:
```
```   223   "even (numeral (Num.Bit0 n))"
```
```   224 proof -
```
```   225   have "even (2 * numeral n)"
```
```   226     unfolding even_times_iff by simp
```
```   227   then have "even (numeral n + numeral n)"
```
```   228     unfolding mult_2 .
```
```   229   then show ?thesis
```
```   230     unfolding numeral.simps .
```
```   231 qed
```
```   232
```
```   233 lemma odd_numeral [simp]:
```
```   234   "odd (numeral (Num.Bit1 n))"
```
```   235 proof
```
```   236   assume "even (numeral (num.Bit1 n))"
```
```   237   then have "even (numeral n + numeral n + 1)"
```
```   238     unfolding numeral.simps .
```
```   239   then have "even (2 * numeral n + 1)"
```
```   240     unfolding mult_2 .
```
```   241   then have "2 dvd numeral n * 2 + 1"
```
```   242     by (simp add: ac_simps)
```
```   243   with dvd_add_times_triv_left_iff [of 2 "numeral n" 1]
```
```   244     have "2 dvd 1"
```
```   245     by simp
```
```   246   then show False by simp
```
```   247 qed
```
```   248
```
```   249 lemma even_add [simp]:
```
```   250   "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
```
```   251   by (auto simp add: dvd_add_right_iff dvd_add_left_iff not_dvd_not_dvd_dvd_add)
```
```   252
```
```   253 lemma odd_add [simp]:
```
```   254   "odd (a + b) \<longleftrightarrow> (\<not> (odd a \<longleftrightarrow> odd b))"
```
```   255   by simp
```
```   256
```
```   257 lemma even_power [simp, presburger]:
```
```   258   "even (a ^ n) \<longleftrightarrow> even a \<and> n \<noteq> 0"
```
```   259   by (induct n) auto
```
```   260
```
```   261 end
```
```   262
```
```   263 context ring_parity
```
```   264 begin
```
```   265
```
```   266 lemma even_minus [simp, presburger, algebra]:
```
```   267   "even (- a) \<longleftrightarrow> even a"
```
```   268   by (fact dvd_minus_iff)
```
```   269
```
```   270 lemma even_diff [simp]:
```
```   271   "even (a - b) \<longleftrightarrow> even (a + b)"
```
```   272   using even_add [of a "- b"] by simp
```
```   273
```
```   274 end
```
```   275
```
```   276
```
```   277 subsubsection {* Parity and division *}
```
```   278
```
```   279 context semiring_div_parity
```
```   280 begin
```
```   281
```
```   282 lemma one_div_two_eq_zero [simp]: -- \<open>FIXME move\<close>
```
```   283   "1 div 2 = 0"
```
```   284 proof (cases "2 = 0")
```
```   285   case True then show ?thesis by simp
```
```   286 next
```
```   287   case False
```
```   288   from mod_div_equality have "1 div 2 * 2 + 1 mod 2 = 1" .
```
```   289   with one_mod_two_eq_one have "1 div 2 * 2 + 1 = 1" by simp
```
```   290   then have "1 div 2 * 2 = 0" by (simp add: ac_simps add_left_imp_eq)
```
```   291   then have "1 div 2 = 0 \<or> 2 = 0" by (rule divisors_zero)
```
```   292   with False show ?thesis by auto
```
```   293 qed
```
```   294
```
```   295 lemma even_iff_mod_2_eq_zero:
```
```   296   "even a \<longleftrightarrow> a mod 2 = 0"
```
```   297   by (fact dvd_eq_mod_eq_0)
```
```   298
```
```   299 lemma even_succ_div_two [simp]:
```
```   300   "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
```
```   301   by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
```
```   302
```
```   303 lemma odd_succ_div_two [simp]:
```
```   304   "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
```
```   305   by (auto elim!: oddE simp add: zero_not_eq_two [symmetric] add.assoc)
```
```   306
```
```   307 lemma even_two_times_div_two:
```
```   308   "even a \<Longrightarrow> 2 * (a div 2) = a"
```
```   309   by (fact dvd_mult_div_cancel)
```
```   310
```
```   311 lemma odd_two_times_div_two_succ:
```
```   312   "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
```
```   313   using mod_div_equality2 [of 2 a] by (simp add: even_iff_mod_2_eq_zero)
```
```   314
```
```   315 end
```
```   316
```
```   317
```
```   318 subsubsection {* Particularities for @{typ nat} and @{typ int} *}
```
```   319
```
```   320 lemma even_Suc [simp, presburger, algebra]:
```
```   321   "even (Suc n) = odd n"
```
```   322   by (fact two_dvd_Suc_iff)
```
```   323
```
```   324 lemma odd_pos:
```
```   325   "odd (n :: nat) \<Longrightarrow> 0 < n"
```
```   326   by (auto elim: oddE)
```
```   327
```
```   328 lemma even_diff_nat [simp]:
```
```   329   fixes m n :: nat
```
```   330   shows "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)"
```
```   331   by (fact two_dvd_diff_nat_iff)
```
```   332
```
```   333 lemma even_int_iff:
```
```   334   "even (int n) \<longleftrightarrow> even n"
```
```   335   by (simp add: dvd_int_iff)
```
```   336
```
```   337 lemma even_nat_iff:
```
```   338   "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"
```
```   339   by (simp add: even_int_iff [symmetric])
```
```   340
```
```   341 lemma even_num_iff:
```
```   342   "0 < n \<Longrightarrow> even n = odd (n - 1 :: nat)"
```
```   343   by simp
```
```   344
```
```   345 lemma even_Suc_div_two [simp]:
```
```   346   "even n \<Longrightarrow> Suc n div 2 = n div 2"
```
```   347   using even_succ_div_two [of n] by simp
```
```   348
```
```   349 lemma odd_Suc_div_two [simp]:
```
```   350   "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
```
```   351   using odd_succ_div_two [of n] by simp
```
```   352
```
```   353 lemma odd_two_times_div_two_Suc:
```
```   354   "odd n \<Longrightarrow> Suc (2 * (n div 2)) = n"
```
```   355   using odd_two_times_div_two_succ [of n] by simp
```
```   356
```
```   357 text {* Nice facts about division by @{term 4} *}
```
```   358
```
```   359 lemma even_even_mod_4_iff:
```
```   360   "even (n::nat) \<longleftrightarrow> even (n mod 4)"
```
```   361   by presburger
```
```   362
```
```   363 lemma odd_mod_4_div_2:
```
```   364   "n mod 4 = (3::nat) \<Longrightarrow> odd ((n - 1) div 2)"
```
```   365   by presburger
```
```   366
```
```   367 lemma even_mod_4_div_2:
```
```   368   "n mod 4 = (1::nat) \<Longrightarrow> even ((n - 1) div 2)"
```
```   369   by presburger
```
```   370
```
```   371 text {* Parity and powers *}
```
```   372
```
```   373 context comm_ring_1
```
```   374 begin
```
```   375
```
```   376 lemma power_minus_even [simp]:
```
```   377   "even n \<Longrightarrow> (- a) ^ n = a ^ n"
```
```   378   by (auto elim: evenE)
```
```   379
```
```   380 lemma power_minus_odd [simp]:
```
```   381   "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
```
```   382   by (auto elim: oddE)
```
```   383
```
```   384 lemma neg_power_if:
```
```   385   "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
```
```   386   by simp
```
```   387
```
```   388 lemma neg_one_even_power [simp]:
```
```   389   "even n \<Longrightarrow> (- 1) ^ n = 1"
```
```   390   by simp
```
```   391
```
```   392 lemma neg_one_odd_power [simp]:
```
```   393   "odd n \<Longrightarrow> (- 1) ^ n = - 1"
```
```   394   by simp
```
```   395
```
```   396 end
```
```   397
```
```   398 lemma zero_less_power_nat_eq_numeral [simp]: -- \<open>FIXME move\<close>
```
```   399   "0 < (n :: nat) ^ numeral w \<longleftrightarrow> 0 < n \<or> numeral w = (0 :: nat)"
```
```   400   by (fact nat_zero_less_power_iff)
```
```   401
```
```   402 context linordered_idom
```
```   403 begin
```
```   404
```
```   405 lemma power_eq_0_iff' [simp]: -- \<open>FIXME move\<close>
```
```   406   "a ^ n = 0 \<longleftrightarrow> a = 0 \<and> n > 0"
```
```   407   by (induct n) auto
```
```   408
```
```   409 lemma power2_less_eq_zero_iff [simp]: -- \<open>FIXME move\<close>
```
```   410   "a\<^sup>2 \<le> 0 \<longleftrightarrow> a = 0"
```
```   411 proof (cases "a = 0")
```
```   412   case True then show ?thesis by simp
```
```   413 next
```
```   414   case False then have "a < 0 \<or> a > 0" by auto
```
```   415   then have "a\<^sup>2 > 0" by auto
```
```   416   then have "\<not> a\<^sup>2 \<le> 0" by (simp add: not_le)
```
```   417   with False show ?thesis by simp
```
```   418 qed
```
```   419
```
```   420 lemma zero_le_even_power:
```
```   421   "even n \<Longrightarrow> 0 \<le> a ^ n"
```
```   422   by (auto elim: evenE)
```
```   423
```
```   424 lemma zero_le_odd_power:
```
```   425   "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
```
```   426   by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)
```
```   427
```
```   428 lemma zero_le_power_iff [presburger]: -- \<open>FIXME cf. @{text zero_le_power_eq}\<close>
```
```   429   "0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a \<or> even n"
```
```   430 proof (cases "even n")
```
```   431   case True
```
```   432   then obtain k where "n = 2 * k" ..
```
```   433   then show ?thesis by simp
```
```   434 next
```
```   435   case False
```
```   436   then obtain k where "n = 2 * k + 1" ..
```
```   437   moreover have "a ^ (2 * k) \<le> 0 \<Longrightarrow> a = 0"
```
```   438     by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)
```
```   439   ultimately show ?thesis
```
```   440     by (auto simp add: zero_le_mult_iff zero_le_even_power)
```
```   441 qed
```
```   442
```
```   443 lemma zero_le_power_eq [presburger]:
```
```   444   "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
```
```   445   using zero_le_power_iff [of a n] by auto
```
```   446
```
```   447 lemma zero_less_power_eq [presburger]:
```
```   448   "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
```
```   449 proof -
```
```   450   have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
```
```   451     unfolding power_eq_0_iff' [of a n, symmetric] by blast
```
```   452   show ?thesis
```
```   453   unfolding less_le zero_le_power_eq by auto
```
```   454 qed
```
```   455
```
```   456 lemma power_less_zero_eq [presburger]:
```
```   457   "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
```
```   458   unfolding not_le [symmetric] zero_le_power_eq by auto
```
```   459
```
```   460 lemma power_le_zero_eq [presburger]:
```
```   461   "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)"
```
```   462   unfolding not_less [symmetric] zero_less_power_eq by auto
```
```   463
```
```   464 lemma power_even_abs:
```
```   465   "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
```
```   466   using power_abs [of a n] by (simp add: zero_le_even_power)
```
```   467
```
```   468 lemma power_mono_even:
```
```   469   assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
```
```   470   shows "a ^ n \<le> b ^ n"
```
```   471 proof -
```
```   472   have "0 \<le> \<bar>a\<bar>" by auto
```
```   473   with `\<bar>a\<bar> \<le> \<bar>b\<bar>`
```
```   474   have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n" by (rule power_mono)
```
```   475   with `even n` show ?thesis by (simp add: power_even_abs)
```
```   476 qed
```
```   477
```
```   478 lemma power_mono_odd:
```
```   479   assumes "odd n" and "a \<le> b"
```
```   480   shows "a ^ n \<le> b ^ n"
```
```   481 proof (cases "b < 0")
```
```   482   case True with `a \<le> b` have "- b \<le> - a" and "0 \<le> - b" by auto
```
```   483   hence "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
```
```   484   with `odd n` show ?thesis by simp
```
```   485 next
```
```   486   case False then have "0 \<le> b" by auto
```
```   487   show ?thesis
```
```   488   proof (cases "a < 0")
```
```   489     case True then have "n \<noteq> 0" and "a \<le> 0" using `odd n` [THEN odd_pos] by auto
```
```   490     then have "a ^ n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
```
```   491     moreover
```
```   492     from `0 \<le> b` have "0 \<le> b ^ n" by auto
```
```   493     ultimately show ?thesis by auto
```
```   494   next
```
```   495     case False then have "0 \<le> a" by auto
```
```   496     with `a \<le> b` show ?thesis using power_mono by auto
```
```   497   qed
```
```   498 qed
```
```   499
```
```   500 text {* Simplify, when the exponent is a numeral *}
```
```   501
```
```   502 lemma zero_le_power_eq_numeral [simp]:
```
```   503   "0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"
```
```   504   by (fact zero_le_power_eq)
```
```   505
```
```   506 lemma zero_less_power_eq_numeral [simp]:
```
```   507   "0 < a ^ numeral w \<longleftrightarrow> numeral w = (0 :: nat)
```
```   508     \<or> even (numeral w :: nat) \<and> a \<noteq> 0 \<or> odd (numeral w :: nat) \<and> 0 < a"
```
```   509   by (fact zero_less_power_eq)
```
```   510
```
```   511 lemma power_le_zero_eq_numeral [simp]:
```
```   512   "a ^ numeral w \<le> 0 \<longleftrightarrow> (0 :: nat) < numeral w
```
```   513     \<and> (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
```
```   514   by (fact power_le_zero_eq)
```
```   515
```
```   516 lemma power_less_zero_eq_numeral [simp]:
```
```   517   "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
```
```   518   by (fact power_less_zero_eq)
```
```   519
```
```   520 lemma power_eq_0_iff_numeral [simp]:
```
```   521   "a ^ numeral w = (0 :: nat) \<longleftrightarrow> a = 0 \<and> numeral w \<noteq> (0 :: nat)"
```
```   522   by (fact power_eq_0_iff)
```
```   523
```
```   524 lemma power_even_abs_numeral [simp]:
```
```   525   "even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"
```
```   526   by (fact power_even_abs)
```
```   527
```
```   528 end
```
```   529
```
```   530
```
```   531 subsubsection {* Tools setup *}
```
```   532
```
```   533 declare transfer_morphism_int_nat [transfer add return:
```
```   534   even_int_iff
```
```   535 ]
```
```   536
```
```   537 lemma [presburger]:
```
```   538   "even n \<longleftrightarrow> even (int n)"
```
```   539   using even_int_iff [of n] by simp
```
```   540
```
```   541 lemma (in semiring_parity) [presburger]:
```
```   542   "even (a + b) \<longleftrightarrow> even a \<and> even b \<or> odd a \<and> odd b"
```
```   543   by auto
```
```   544
```
```   545 lemma [presburger, algebra]:
```
```   546   fixes m n :: nat
```
```   547   shows "even (m - n) \<longleftrightarrow> m < n \<or> even m \<and> even n \<or> odd m \<and> odd n"
```
```   548   by auto
```
```   549
```
```   550 lemma [presburger, algebra]:
```
```   551   fixes m n :: nat
```
```   552   shows "even (m ^ n) \<longleftrightarrow> even m \<and> 0 < n"
```
```   553   by simp
```
```   554
```
```   555 lemma [presburger]:
```
```   556   fixes k :: int
```
```   557   shows "(k + 1) div 2 = k div 2 \<longleftrightarrow> even k"
```
```   558   by presburger
```
```   559
```
```   560 lemma [presburger]:
```
```   561   fixes k :: int
```
```   562   shows "(k + 1) div 2 = k div 2 + 1 \<longleftrightarrow> odd k"
```
```   563   by presburger
```
```   564
```
```   565 lemma [presburger]:
```
```   566   "Suc n div Suc (Suc 0) = n div Suc (Suc 0) \<longleftrightarrow> even n"
```
```   567   by presburger
```
```   568
```
```   569 end
```
```   570
```