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