src/HOL/Parity.thy
author haftmann
Sun Oct 08 22:28:22 2017 +0200 (21 months ago)
changeset 66815 93c6632ddf44
parent 66808 1907167b6038
child 66816 212a3334e7da
permissions -rw-r--r--
one uniform type class for parity structures
wenzelm@41959
     1
(*  Title:      HOL/Parity.thy
wenzelm@41959
     2
    Author:     Jeremy Avigad
wenzelm@41959
     3
    Author:     Jacques D. Fleuriot
wenzelm@21256
     4
*)
wenzelm@21256
     5
wenzelm@60758
     6
section \<open>Parity in rings and semirings\<close>
wenzelm@21256
     7
wenzelm@21256
     8
theory Parity
haftmann@66815
     9
  imports Euclidean_Division
wenzelm@21256
    10
begin
wenzelm@21256
    11
wenzelm@61799
    12
subsection \<open>Ring structures with parity and \<open>even\<close>/\<open>odd\<close> predicates\<close>
haftmann@58678
    13
haftmann@66815
    14
class semiring_parity = linordered_semidom + unique_euclidean_semiring +
haftmann@66815
    15
  assumes of_nat_div: "of_nat (m div n) = of_nat m div of_nat n"
haftmann@66815
    16
    and odd_imp_mod_2_eq_1: "\<not> 2 dvd a \<Longrightarrow> a mod 2 = 1"
haftmann@66815
    17
haftmann@66815
    18
context semiring_parity
haftmann@54227
    19
begin
wenzelm@21256
    20
haftmann@66815
    21
lemma of_nat_dvd_iff:
haftmann@66815
    22
  "of_nat m dvd of_nat n \<longleftrightarrow> m dvd n" (is "?P \<longleftrightarrow> ?Q")
haftmann@66815
    23
proof (cases "m = 0")
haftmann@66815
    24
  case True
haftmann@66815
    25
  then show ?thesis
haftmann@66815
    26
    by simp
haftmann@66815
    27
next
haftmann@66815
    28
  case False
haftmann@66815
    29
  show ?thesis
haftmann@66815
    30
  proof
haftmann@66815
    31
    assume ?Q
haftmann@66815
    32
    then show ?P
haftmann@66815
    33
      by (auto elim: dvd_class.dvdE)
haftmann@66815
    34
  next
haftmann@66815
    35
    assume ?P
haftmann@66815
    36
    with False have "of_nat n = of_nat n div of_nat m * of_nat m"
haftmann@66815
    37
      by simp
haftmann@66815
    38
    then have "of_nat n = of_nat (n div m * m)"
haftmann@66815
    39
      by (simp add: of_nat_div)
haftmann@66815
    40
    then have "n = n div m * m"
haftmann@66815
    41
      by (simp only: of_nat_eq_iff)
haftmann@66815
    42
    then have "n = m * (n div m)"
haftmann@66815
    43
      by (simp add: ac_simps)
haftmann@66815
    44
    then show ?Q ..
haftmann@66815
    45
  qed
haftmann@66815
    46
qed
haftmann@66815
    47
haftmann@66815
    48
lemma of_nat_mod:
haftmann@66815
    49
  "of_nat (m mod n) = of_nat m mod of_nat n"
haftmann@66815
    50
proof -
haftmann@66815
    51
  have "of_nat m div of_nat n * of_nat n + of_nat m mod of_nat n = of_nat m"
haftmann@66815
    52
    by (simp add: div_mult_mod_eq)
haftmann@66815
    53
  also have "of_nat m = of_nat (m div n * n + m mod n)"
haftmann@66815
    54
    by simp
haftmann@66815
    55
  finally show ?thesis
haftmann@66815
    56
    by (simp only: of_nat_div of_nat_mult of_nat_add) simp
haftmann@66815
    57
qed
haftmann@66815
    58
haftmann@66815
    59
lemma one_div_two_eq_zero [simp]:
haftmann@66815
    60
  "1 div 2 = 0"
haftmann@66815
    61
proof -
haftmann@66815
    62
  from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0"
haftmann@66815
    63
    by (simp only:) simp
haftmann@66815
    64
  then show ?thesis
haftmann@66815
    65
    by simp
haftmann@66815
    66
qed
haftmann@66815
    67
haftmann@66815
    68
lemma one_mod_two_eq_one [simp]:
haftmann@66815
    69
  "1 mod 2 = 1"
haftmann@66815
    70
proof -
haftmann@66815
    71
  from of_nat_mod [symmetric] have "of_nat 1 mod of_nat 2 = of_nat 1"
haftmann@66815
    72
    by (simp only:) simp
haftmann@66815
    73
  then show ?thesis
haftmann@66815
    74
    by simp
haftmann@66815
    75
qed
haftmann@59816
    76
haftmann@58740
    77
abbreviation even :: "'a \<Rightarrow> bool"
wenzelm@63654
    78
  where "even a \<equiv> 2 dvd a"
haftmann@54228
    79
haftmann@58678
    80
abbreviation odd :: "'a \<Rightarrow> bool"
wenzelm@63654
    81
  where "odd a \<equiv> \<not> 2 dvd a"
haftmann@58678
    82
haftmann@66815
    83
lemma even_iff_mod_2_eq_zero:
haftmann@66815
    84
  "even a \<longleftrightarrow> a mod 2 = 0"
haftmann@66815
    85
  by (fact dvd_eq_mod_eq_0)
haftmann@66815
    86
haftmann@66815
    87
lemma odd_iff_mod_2_eq_one:
haftmann@66815
    88
  "odd a \<longleftrightarrow> a mod 2 = 1"
haftmann@66815
    89
  by (auto dest: odd_imp_mod_2_eq_1)
haftmann@58787
    90
haftmann@66815
    91
lemma parity_cases [case_names even odd]:
haftmann@66815
    92
  assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P"
haftmann@66815
    93
  assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P"
haftmann@66815
    94
  shows P
haftmann@66815
    95
  using assms by (cases "even a") (simp_all add: odd_iff_mod_2_eq_one)
haftmann@66815
    96
haftmann@66815
    97
lemma not_mod_2_eq_1_eq_0 [simp]:
haftmann@66815
    98
  "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
haftmann@66815
    99
  by (cases a rule: parity_cases) simp_all
haftmann@66815
   100
haftmann@66815
   101
lemma not_mod_2_eq_0_eq_1 [simp]:
haftmann@66815
   102
  "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"
haftmann@66815
   103
  by (cases a rule: parity_cases) simp_all
haftmann@58787
   104
haftmann@58690
   105
lemma evenE [elim?]:
haftmann@58690
   106
  assumes "even a"
haftmann@58690
   107
  obtains b where "a = 2 * b"
haftmann@58740
   108
  using assms by (rule dvdE)
haftmann@58690
   109
haftmann@58681
   110
lemma oddE [elim?]:
haftmann@58680
   111
  assumes "odd a"
haftmann@58680
   112
  obtains b where "a = 2 * b + 1"
haftmann@58787
   113
proof -
haftmann@66815
   114
  have "a = 2 * (a div 2) + a mod 2"
haftmann@66815
   115
    by (simp add: mult_div_mod_eq)
haftmann@66815
   116
  with assms have "a = 2 * (a div 2) + 1"
haftmann@66815
   117
    by (simp add: odd_iff_mod_2_eq_one)
haftmann@66815
   118
  then show ?thesis ..
haftmann@66815
   119
qed
haftmann@66815
   120
haftmann@66815
   121
lemma mod_2_eq_odd:
haftmann@66815
   122
  "a mod 2 = of_bool (odd a)"
haftmann@66815
   123
  by (auto elim: oddE)
haftmann@66815
   124
haftmann@66815
   125
lemma one_mod_2_pow_eq [simp]:
haftmann@66815
   126
  "1 mod (2 ^ n) = of_bool (n > 0)"
haftmann@66815
   127
proof -
haftmann@66815
   128
  have "1 mod (2 ^ n) = (of_bool (n > 0) :: nat)"
haftmann@66815
   129
    by (induct n) (simp_all add: mod_mult2_eq)
haftmann@66815
   130
  then have "of_nat (1 mod (2 ^ n)) = of_bool (n > 0)"
haftmann@66815
   131
    by simp
haftmann@66815
   132
  then show ?thesis
haftmann@66815
   133
    by (simp add: of_nat_mod)
haftmann@66815
   134
qed
haftmann@66815
   135
haftmann@66815
   136
lemma even_of_nat [simp]:
haftmann@66815
   137
  "even (of_nat a) \<longleftrightarrow> even a"
haftmann@66815
   138
proof -
haftmann@66815
   139
  have "even (of_nat a) \<longleftrightarrow> of_nat 2 dvd of_nat a"
haftmann@66815
   140
    by simp
haftmann@66815
   141
  also have "\<dots> \<longleftrightarrow> even a"
haftmann@66815
   142
    by (simp only: of_nat_dvd_iff)
haftmann@66815
   143
  finally show ?thesis .
haftmann@66815
   144
qed
haftmann@66815
   145
haftmann@66815
   146
lemma even_zero [simp]:
haftmann@66815
   147
  "even 0"
haftmann@66815
   148
  by (fact dvd_0_right)
haftmann@66815
   149
haftmann@66815
   150
lemma odd_one [simp]:
haftmann@66815
   151
  "odd 1"
haftmann@66815
   152
proof -
haftmann@66815
   153
  have "\<not> (2 :: nat) dvd 1"
haftmann@66815
   154
    by simp
haftmann@66815
   155
  then have "\<not> of_nat 2 dvd of_nat 1"
haftmann@66815
   156
    unfolding of_nat_dvd_iff by simp
haftmann@66815
   157
  then show ?thesis
haftmann@66815
   158
    by simp
haftmann@58787
   159
qed
wenzelm@63654
   160
haftmann@66815
   161
lemma odd_even_add:
haftmann@66815
   162
  "even (a + b)" if "odd a" and "odd b"
haftmann@66815
   163
proof -
haftmann@66815
   164
  from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1"
haftmann@66815
   165
    by (blast elim: oddE)
haftmann@66815
   166
  then have "a + b = 2 * c + 2 * d + (1 + 1)"
haftmann@66815
   167
    by (simp only: ac_simps)
haftmann@66815
   168
  also have "\<dots> = 2 * (c + d + 1)"
haftmann@66815
   169
    by (simp add: algebra_simps)
haftmann@66815
   170
  finally show ?thesis ..
haftmann@66815
   171
qed
haftmann@66815
   172
haftmann@66815
   173
lemma even_add [simp]:
haftmann@66815
   174
  "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
haftmann@66815
   175
  by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add)
haftmann@66815
   176
haftmann@66815
   177
lemma odd_add [simp]:
haftmann@66815
   178
  "odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)"
haftmann@66815
   179
  by simp
haftmann@66815
   180
haftmann@66815
   181
lemma even_plus_one_iff [simp]:
haftmann@66815
   182
  "even (a + 1) \<longleftrightarrow> odd a"
haftmann@66815
   183
  by (auto simp add: dvd_add_right_iff intro: odd_even_add)
haftmann@66815
   184
haftmann@66815
   185
lemma even_mult_iff [simp]:
haftmann@66815
   186
  "even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q")
haftmann@66815
   187
proof
haftmann@66815
   188
  assume ?Q
haftmann@66815
   189
  then show ?P
haftmann@66815
   190
    by auto
haftmann@66815
   191
next
haftmann@66815
   192
  assume ?P
haftmann@66815
   193
  show ?Q
haftmann@66815
   194
  proof (rule ccontr)
haftmann@66815
   195
    assume "\<not> (even a \<or> even b)"
haftmann@66815
   196
    then have "odd a" and "odd b"
haftmann@66815
   197
      by auto
haftmann@66815
   198
    then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1"
haftmann@66815
   199
      by (blast elim: oddE)
haftmann@66815
   200
    then have "a * b = (2 * r + 1) * (2 * s + 1)"
haftmann@66815
   201
      by simp
haftmann@66815
   202
    also have "\<dots> = 2 * (2 * r * s + r + s) + 1"
haftmann@66815
   203
      by (simp add: algebra_simps)
haftmann@66815
   204
    finally have "odd (a * b)"
haftmann@66815
   205
      by simp
haftmann@66815
   206
    with \<open>?P\<close> show False
haftmann@66815
   207
      by auto
haftmann@66815
   208
  qed
haftmann@66815
   209
qed
haftmann@58678
   210
wenzelm@63654
   211
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
haftmann@58678
   212
proof -
haftmann@58678
   213
  have "even (2 * numeral n)"
haftmann@66815
   214
    unfolding even_mult_iff by simp
haftmann@58678
   215
  then have "even (numeral n + numeral n)"
haftmann@58678
   216
    unfolding mult_2 .
haftmann@58678
   217
  then show ?thesis
haftmann@58678
   218
    unfolding numeral.simps .
haftmann@58678
   219
qed
haftmann@58678
   220
wenzelm@63654
   221
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
haftmann@58678
   222
proof
haftmann@58678
   223
  assume "even (numeral (num.Bit1 n))"
haftmann@58678
   224
  then have "even (numeral n + numeral n + 1)"
haftmann@58678
   225
    unfolding numeral.simps .
haftmann@58678
   226
  then have "even (2 * numeral n + 1)"
haftmann@58678
   227
    unfolding mult_2 .
haftmann@58678
   228
  then have "2 dvd numeral n * 2 + 1"
haftmann@58740
   229
    by (simp add: ac_simps)
wenzelm@63654
   230
  then have "2 dvd 1"
wenzelm@63654
   231
    using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp
haftmann@58678
   232
  then show False by simp
haftmann@58678
   233
qed
haftmann@58678
   234
wenzelm@63654
   235
lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"
haftmann@58680
   236
  by (induct n) auto
haftmann@58680
   237
haftmann@66815
   238
lemma even_succ_div_two [simp]:
haftmann@66815
   239
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
haftmann@66815
   240
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
haftmann@66815
   241
haftmann@66815
   242
lemma odd_succ_div_two [simp]:
haftmann@66815
   243
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
haftmann@66815
   244
  by (auto elim!: oddE simp add: add.assoc)
haftmann@66815
   245
haftmann@66815
   246
lemma even_two_times_div_two:
haftmann@66815
   247
  "even a \<Longrightarrow> 2 * (a div 2) = a"
haftmann@66815
   248
  by (fact dvd_mult_div_cancel)
haftmann@66815
   249
haftmann@66815
   250
lemma odd_two_times_div_two_succ [simp]:
haftmann@66815
   251
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
haftmann@66815
   252
  using mult_div_mod_eq [of 2 a]
haftmann@66815
   253
  by (simp add: even_iff_mod_2_eq_zero)
haftmann@66815
   254
haftmann@58678
   255
end
haftmann@58678
   256
haftmann@59816
   257
class ring_parity = ring + semiring_parity
haftmann@58679
   258
begin
haftmann@58679
   259
haftmann@59816
   260
subclass comm_ring_1 ..
haftmann@59816
   261
haftmann@66815
   262
lemma even_minus [simp]:
haftmann@66815
   263
  "even (- a) \<longleftrightarrow> even a"
haftmann@58740
   264
  by (fact dvd_minus_iff)
haftmann@58679
   265
haftmann@66815
   266
lemma even_diff [simp]:
haftmann@66815
   267
  "even (a - b) \<longleftrightarrow> even (a + b)"
haftmann@58680
   268
  using even_add [of a "- b"] by simp
haftmann@58680
   269
haftmann@58679
   270
end
haftmann@58679
   271
haftmann@66808
   272
haftmann@66815
   273
subsection \<open>Instance for @{typ nat}\<close>
haftmann@66808
   274
haftmann@66815
   275
instance nat :: semiring_parity
haftmann@66815
   276
  by standard (simp_all add: dvd_eq_mod_eq_0)
haftmann@66808
   277
haftmann@66815
   278
lemma even_Suc_Suc_iff [simp]:
haftmann@66815
   279
  "even (Suc (Suc n)) \<longleftrightarrow> even n"
haftmann@58787
   280
  using dvd_add_triv_right_iff [of 2 n] by simp
haftmann@58687
   281
haftmann@66815
   282
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"
haftmann@66815
   283
  using even_plus_one_iff [of n] by simp
haftmann@58787
   284
haftmann@66815
   285
lemma even_diff_nat [simp]:
haftmann@66815
   286
  "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat
haftmann@58787
   287
proof (cases "n \<le> m")
haftmann@58787
   288
  case True
haftmann@58787
   289
  then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)
haftmann@66815
   290
  moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp
haftmann@66815
   291
  ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)
haftmann@58787
   292
  then show ?thesis by auto
haftmann@58787
   293
next
haftmann@58787
   294
  case False
haftmann@58787
   295
  then show ?thesis by simp
wenzelm@63654
   296
qed
wenzelm@63654
   297
haftmann@66815
   298
lemma odd_pos:
haftmann@66815
   299
  "odd n \<Longrightarrow> 0 < n" for n :: nat
haftmann@58690
   300
  by (auto elim: oddE)
haftmann@60343
   301
haftmann@66815
   302
lemma Suc_double_not_eq_double:
haftmann@66815
   303
  "Suc (2 * m) \<noteq> 2 * n"
haftmann@62597
   304
proof
haftmann@62597
   305
  assume "Suc (2 * m) = 2 * n"
haftmann@62597
   306
  moreover have "odd (Suc (2 * m))" and "even (2 * n)"
haftmann@62597
   307
    by simp_all
haftmann@62597
   308
  ultimately show False by simp
haftmann@62597
   309
qed
haftmann@62597
   310
haftmann@66815
   311
lemma double_not_eq_Suc_double:
haftmann@66815
   312
  "2 * m \<noteq> Suc (2 * n)"
haftmann@62597
   313
  using Suc_double_not_eq_double [of n m] by simp
haftmann@62597
   314
haftmann@66815
   315
lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n"
haftmann@66815
   316
  by (auto elim: oddE)
haftmann@60343
   317
haftmann@66815
   318
lemma even_Suc_div_two [simp]:
haftmann@66815
   319
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
haftmann@66815
   320
  using even_succ_div_two [of n] by simp
haftmann@60343
   321
haftmann@66815
   322
lemma odd_Suc_div_two [simp]:
haftmann@66815
   323
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
haftmann@66815
   324
  using odd_succ_div_two [of n] by simp
haftmann@60343
   325
haftmann@66815
   326
lemma odd_two_times_div_two_nat [simp]:
haftmann@66815
   327
  assumes "odd n"
haftmann@66815
   328
  shows "2 * (n div 2) = n - (1 :: nat)"
haftmann@66815
   329
proof -
haftmann@66815
   330
  from assms have "2 * (n div 2) + 1 = n"
haftmann@66815
   331
    by (rule odd_two_times_div_two_succ)
haftmann@66815
   332
  then have "Suc (2 * (n div 2)) - 1 = n - 1"
haftmann@58787
   333
    by simp
haftmann@66815
   334
  then show ?thesis
haftmann@66815
   335
    by simp
haftmann@58787
   336
qed
haftmann@58680
   337
haftmann@66815
   338
lemma parity_induct [case_names zero even odd]:
haftmann@66815
   339
  assumes zero: "P 0"
haftmann@66815
   340
  assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)"
haftmann@66815
   341
  assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
haftmann@66815
   342
  shows "P n"
haftmann@66815
   343
proof (induct n rule: less_induct)
haftmann@66815
   344
  case (less n)
haftmann@66815
   345
  show "P n"
haftmann@66815
   346
  proof (cases "n = 0")
haftmann@66815
   347
    case True with zero show ?thesis by simp
haftmann@66815
   348
  next
haftmann@66815
   349
    case False
haftmann@66815
   350
    with less have hyp: "P (n div 2)" by simp
haftmann@66815
   351
    show ?thesis
haftmann@66815
   352
    proof (cases "even n")
haftmann@66815
   353
      case True
haftmann@66815
   354
      with hyp even [of "n div 2"] show ?thesis
haftmann@66815
   355
        by simp
haftmann@66815
   356
    next
haftmann@66815
   357
      case False
haftmann@66815
   358
      with hyp odd [of "n div 2"] show ?thesis
haftmann@66815
   359
        by simp
haftmann@66815
   360
    qed
haftmann@66815
   361
  qed
haftmann@66815
   362
qed
haftmann@58687
   363
haftmann@58687
   364
wenzelm@60758
   365
subsection \<open>Parity and powers\<close>
haftmann@58689
   366
eberlm@61531
   367
context ring_1
haftmann@58689
   368
begin
haftmann@58689
   369
wenzelm@63654
   370
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
haftmann@58690
   371
  by (auto elim: evenE)
haftmann@58689
   372
wenzelm@63654
   373
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
haftmann@58690
   374
  by (auto elim: oddE)
haftmann@58690
   375
haftmann@66815
   376
lemma uminus_power_if:
haftmann@66815
   377
  "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
haftmann@66815
   378
  by auto
haftmann@66815
   379
wenzelm@63654
   380
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
haftmann@58690
   381
  by simp
haftmann@58689
   382
wenzelm@63654
   383
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
haftmann@58690
   384
  by simp
haftmann@58689
   385
bulwahn@66582
   386
lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)"
bulwahn@66582
   387
  by (cases "even (n + k)") auto
bulwahn@66582
   388
wenzelm@63654
   389
end
haftmann@58689
   390
haftmann@58689
   391
context linordered_idom
haftmann@58689
   392
begin
haftmann@58689
   393
wenzelm@63654
   394
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
haftmann@58690
   395
  by (auto elim: evenE)
haftmann@58689
   396
wenzelm@63654
   397
lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
haftmann@58689
   398
  by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)
haftmann@58689
   399
wenzelm@63654
   400
lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
haftmann@58787
   401
  by (auto simp add: zero_le_even_power zero_le_odd_power)
wenzelm@63654
   402
wenzelm@63654
   403
lemma zero_less_power_eq: "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
haftmann@58689
   404
proof -
haftmann@58689
   405
  have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
haftmann@58787
   406
    unfolding power_eq_0_iff [of a n, symmetric] by blast
haftmann@58689
   407
  show ?thesis
wenzelm@63654
   408
    unfolding less_le zero_le_power_eq by auto
haftmann@58689
   409
qed
haftmann@58689
   410
wenzelm@63654
   411
lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
haftmann@58689
   412
  unfolding not_le [symmetric] zero_le_power_eq by auto
haftmann@58689
   413
wenzelm@63654
   414
lemma power_le_zero_eq: "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)"
wenzelm@63654
   415
  unfolding not_less [symmetric] zero_less_power_eq by auto
wenzelm@63654
   416
wenzelm@63654
   417
lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
haftmann@58689
   418
  using power_abs [of a n] by (simp add: zero_le_even_power)
haftmann@58689
   419
haftmann@58689
   420
lemma power_mono_even:
haftmann@58689
   421
  assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
haftmann@58689
   422
  shows "a ^ n \<le> b ^ n"
haftmann@58689
   423
proof -
haftmann@58689
   424
  have "0 \<le> \<bar>a\<bar>" by auto
wenzelm@63654
   425
  with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n"
wenzelm@63654
   426
    by (rule power_mono)
wenzelm@63654
   427
  with \<open>even n\<close> show ?thesis
wenzelm@63654
   428
    by (simp add: power_even_abs)
haftmann@58689
   429
qed
haftmann@58689
   430
haftmann@58689
   431
lemma power_mono_odd:
haftmann@58689
   432
  assumes "odd n" and "a \<le> b"
haftmann@58689
   433
  shows "a ^ n \<le> b ^ n"
haftmann@58689
   434
proof (cases "b < 0")
wenzelm@63654
   435
  case True
wenzelm@63654
   436
  with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto
wenzelm@63654
   437
  then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
wenzelm@60758
   438
  with \<open>odd n\<close> show ?thesis by simp
haftmann@58689
   439
next
wenzelm@63654
   440
  case False
wenzelm@63654
   441
  then have "0 \<le> b" by auto
haftmann@58689
   442
  show ?thesis
haftmann@58689
   443
  proof (cases "a < 0")
wenzelm@63654
   444
    case True
wenzelm@63654
   445
    then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto
wenzelm@60758
   446
    then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto
wenzelm@63654
   447
    moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto
haftmann@58689
   448
    ultimately show ?thesis by auto
haftmann@58689
   449
  next
wenzelm@63654
   450
    case False
wenzelm@63654
   451
    then have "0 \<le> a" by auto
wenzelm@63654
   452
    with \<open>a \<le> b\<close> show ?thesis
wenzelm@63654
   453
      using power_mono by auto
haftmann@58689
   454
  qed
haftmann@58689
   455
qed
hoelzl@62083
   456
wenzelm@60758
   457
text \<open>Simplify, when the exponent is a numeral\<close>
haftmann@58689
   458
haftmann@58689
   459
lemma zero_le_power_eq_numeral [simp]:
haftmann@58689
   460
  "0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"
haftmann@58689
   461
  by (fact zero_le_power_eq)
haftmann@58689
   462
haftmann@58689
   463
lemma zero_less_power_eq_numeral [simp]:
wenzelm@63654
   464
  "0 < a ^ numeral w \<longleftrightarrow>
wenzelm@63654
   465
    numeral w = (0 :: nat) \<or>
wenzelm@63654
   466
    even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
wenzelm@63654
   467
    odd (numeral w :: nat) \<and> 0 < a"
haftmann@58689
   468
  by (fact zero_less_power_eq)
haftmann@58689
   469
haftmann@58689
   470
lemma power_le_zero_eq_numeral [simp]:
wenzelm@63654
   471
  "a ^ numeral w \<le> 0 \<longleftrightarrow>
wenzelm@63654
   472
    (0 :: nat) < numeral w \<and>
wenzelm@63654
   473
    (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
haftmann@58689
   474
  by (fact power_le_zero_eq)
haftmann@58689
   475
haftmann@58689
   476
lemma power_less_zero_eq_numeral [simp]:
haftmann@58689
   477
  "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
haftmann@58689
   478
  by (fact power_less_zero_eq)
haftmann@58689
   479
haftmann@58689
   480
lemma power_even_abs_numeral [simp]:
haftmann@58689
   481
  "even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"
haftmann@58689
   482
  by (fact power_even_abs)
haftmann@58689
   483
haftmann@58689
   484
end
haftmann@58689
   485
haftmann@58770
   486
end