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