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