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