src/HOL/Parity.thy
author nipkow
Thu May 14 15:39:15 2009 +0200 (2009-05-14)
changeset 31148 7ba7c1f8bc22
parent 31017 2c227493ea56
child 31718 7715d4d3586f
permissions -rw-r--r--
Cleaned up Parity a little
wenzelm@21263
     1
(*  Title:      HOL/Library/Parity.thy
haftmann@25600
     2
    Author:     Jeremy Avigad, Jacques D. Fleuriot
wenzelm@21256
     3
*)
wenzelm@21256
     4
wenzelm@21256
     5
header {* Even and Odd for int and nat *}
wenzelm@21256
     6
wenzelm@21256
     7
theory Parity
haftmann@30738
     8
imports Main
wenzelm@21256
     9
begin
wenzelm@21256
    10
haftmann@29608
    11
class even_odd = 
haftmann@22390
    12
  fixes even :: "'a \<Rightarrow> bool"
wenzelm@21256
    13
wenzelm@21256
    14
abbreviation
haftmann@22390
    15
  odd :: "'a\<Colon>even_odd \<Rightarrow> bool" where
haftmann@22390
    16
  "odd x \<equiv> \<not> even x"
haftmann@22390
    17
haftmann@26259
    18
instantiation nat and int  :: even_odd
haftmann@25571
    19
begin
haftmann@25571
    20
haftmann@25571
    21
definition
haftmann@25571
    22
  even_def [presburger]: "even x \<longleftrightarrow> (x\<Colon>int) mod 2 = 0"
haftmann@22390
    23
haftmann@25571
    24
definition
haftmann@25571
    25
  even_nat_def [presburger]: "even x \<longleftrightarrow> even (int x)"
haftmann@25571
    26
haftmann@25571
    27
instance ..
haftmann@25571
    28
haftmann@25571
    29
end
wenzelm@21256
    30
wenzelm@21256
    31
nipkow@31148
    32
lemma even_zero_int[simp]: "even (0::int)" by presburger
nipkow@31148
    33
nipkow@31148
    34
lemma odd_one_int[simp]: "odd (1::int)" by presburger
nipkow@31148
    35
nipkow@31148
    36
lemma even_zero_nat[simp]: "even (0::nat)" by presburger
nipkow@31148
    37
nipkow@31148
    38
lemma odd_zero_nat [simp]: "odd (1::nat)" by presburger
nipkow@31148
    39
nipkow@31148
    40
declare even_def[of "number_of v", standard, simp]
nipkow@31148
    41
nipkow@31148
    42
declare even_nat_def[of "number_of v", standard, simp]
nipkow@31148
    43
wenzelm@21256
    44
subsection {* Even and odd are mutually exclusive *}
wenzelm@21256
    45
wenzelm@21263
    46
lemma int_pos_lt_two_imp_zero_or_one:
wenzelm@21256
    47
    "0 <= x ==> (x::int) < 2 ==> x = 0 | x = 1"
chaieb@23522
    48
  by presburger
wenzelm@21256
    49
chaieb@23522
    50
lemma neq_one_mod_two [simp, presburger]: 
chaieb@23522
    51
  "((x::int) mod 2 ~= 0) = (x mod 2 = 1)" by presburger
wenzelm@21256
    52
haftmann@25600
    53
wenzelm@21256
    54
subsection {* Behavior under integer arithmetic operations *}
chaieb@27668
    55
declare dvd_def[algebra]
chaieb@27668
    56
lemma nat_even_iff_2_dvd[algebra]: "even (x::nat) \<longleftrightarrow> 2 dvd x"
chaieb@27668
    57
  by (presburger add: even_nat_def even_def)
chaieb@27668
    58
lemma int_even_iff_2_dvd[algebra]: "even (x::int) \<longleftrightarrow> 2 dvd x"
chaieb@27668
    59
  by presburger
wenzelm@21256
    60
wenzelm@21256
    61
lemma even_times_anything: "even (x::int) ==> even (x * y)"
chaieb@27668
    62
  by algebra
wenzelm@21256
    63
chaieb@27668
    64
lemma anything_times_even: "even (y::int) ==> even (x * y)" by algebra
wenzelm@21256
    65
chaieb@27668
    66
lemma odd_times_odd: "odd (x::int) ==> odd y ==> odd (x * y)" 
wenzelm@21256
    67
  by (simp add: even_def zmod_zmult1_eq)
wenzelm@21256
    68
nipkow@31148
    69
lemma even_product[simp,presburger]: "even((x::int) * y) = (even x | even y)"
wenzelm@21263
    70
  apply (auto simp add: even_times_anything anything_times_even)
wenzelm@21256
    71
  apply (rule ccontr)
wenzelm@21256
    72
  apply (auto simp add: odd_times_odd)
wenzelm@21256
    73
  done
wenzelm@21256
    74
wenzelm@21256
    75
lemma even_plus_even: "even (x::int) ==> even y ==> even (x + y)"
nipkow@31148
    76
by presburger
wenzelm@21256
    77
wenzelm@21256
    78
lemma even_plus_odd: "even (x::int) ==> odd y ==> odd (x + y)"
nipkow@31148
    79
by presburger
wenzelm@21256
    80
wenzelm@21256
    81
lemma odd_plus_even: "odd (x::int) ==> even y ==> odd (x + y)"
nipkow@31148
    82
by presburger
wenzelm@21256
    83
chaieb@23522
    84
lemma odd_plus_odd: "odd (x::int) ==> odd y ==> even (x + y)" by presburger
wenzelm@21256
    85
nipkow@31148
    86
lemma even_sum[simp,presburger]:
nipkow@31148
    87
  "even ((x::int) + y) = ((even x & even y) | (odd x & odd y))"
nipkow@31148
    88
by presburger
wenzelm@21256
    89
nipkow@31148
    90
lemma even_neg[simp,presburger,algebra]: "even (-(x::int)) = even x"
nipkow@31148
    91
by presburger
wenzelm@21256
    92
nipkow@31148
    93
lemma even_difference[simp]:
chaieb@23522
    94
    "even ((x::int) - y) = ((even x & even y) | (odd x & odd y))" by presburger
wenzelm@21256
    95
nipkow@31148
    96
lemma even_power[simp,presburger]: "even ((x::int)^n) = (even x & n \<noteq> 0)"
nipkow@31148
    97
by (induct n) auto
wenzelm@21256
    98
nipkow@31148
    99
lemma odd_pow: "odd x ==> odd((x::int)^n)" by simp
wenzelm@21256
   100
wenzelm@21256
   101
wenzelm@21256
   102
subsection {* Equivalent definitions *}
wenzelm@21256
   103
chaieb@23522
   104
lemma two_times_even_div_two: "even (x::int) ==> 2 * (x div 2) = x" 
nipkow@31148
   105
by presburger
wenzelm@21256
   106
nipkow@31148
   107
lemma two_times_odd_div_two_plus_one:
nipkow@31148
   108
  "odd (x::int) ==> 2 * (x div 2) + 1 = x"
nipkow@31148
   109
by presburger
wenzelm@21256
   110
chaieb@23522
   111
lemma even_equiv_def: "even (x::int) = (EX y. x = 2 * y)" by presburger
wenzelm@21256
   112
chaieb@23522
   113
lemma odd_equiv_def: "odd (x::int) = (EX y. x = 2 * y + 1)" by presburger
wenzelm@21256
   114
wenzelm@21256
   115
subsection {* even and odd for nats *}
wenzelm@21256
   116
wenzelm@21256
   117
lemma pos_int_even_equiv_nat_even: "0 \<le> x ==> even x = even (nat x)"
nipkow@31148
   118
by (simp add: even_nat_def)
wenzelm@21256
   119
nipkow@31148
   120
lemma even_product_nat[simp,presburger,algebra]:
nipkow@31148
   121
  "even((x::nat) * y) = (even x | even y)"
nipkow@31148
   122
by (simp add: even_nat_def int_mult)
wenzelm@21256
   123
nipkow@31148
   124
lemma even_sum_nat[simp,presburger,algebra]:
nipkow@31148
   125
  "even ((x::nat) + y) = ((even x & even y) | (odd x & odd y))"
chaieb@23522
   126
by presburger
wenzelm@21256
   127
nipkow@31148
   128
lemma even_difference_nat[simp,presburger,algebra]:
nipkow@31148
   129
  "even ((x::nat) - y) = (x < y | (even x & even y) | (odd x & odd y))"
nipkow@31148
   130
by presburger
wenzelm@21256
   131
nipkow@31148
   132
lemma even_Suc[simp,presburger,algebra]: "even (Suc x) = odd x"
nipkow@31148
   133
by presburger
wenzelm@21256
   134
nipkow@31148
   135
lemma even_power_nat[simp,presburger,algebra]:
nipkow@31148
   136
  "even ((x::nat)^y) = (even x & 0 < y)"
nipkow@31148
   137
by (simp add: even_nat_def int_power)
wenzelm@21256
   138
wenzelm@21256
   139
wenzelm@21256
   140
subsection {* Equivalent definitions *}
wenzelm@21256
   141
nipkow@31148
   142
lemma nat_lt_two_imp_zero_or_one:
nipkow@31148
   143
  "(x::nat) < Suc (Suc 0) ==> x = 0 | x = Suc 0"
nipkow@31148
   144
by presburger
wenzelm@21256
   145
wenzelm@21256
   146
lemma even_nat_mod_two_eq_zero: "even (x::nat) ==> x mod (Suc (Suc 0)) = 0"
nipkow@31148
   147
by presburger
wenzelm@21256
   148
wenzelm@21256
   149
lemma odd_nat_mod_two_eq_one: "odd (x::nat) ==> x mod (Suc (Suc 0)) = Suc 0"
chaieb@23522
   150
by presburger
wenzelm@21256
   151
wenzelm@21263
   152
lemma even_nat_equiv_def: "even (x::nat) = (x mod Suc (Suc 0) = 0)"
nipkow@31148
   153
by presburger
wenzelm@21256
   154
wenzelm@21256
   155
lemma odd_nat_equiv_def: "odd (x::nat) = (x mod Suc (Suc 0) = Suc 0)"
nipkow@31148
   156
by presburger
wenzelm@21256
   157
wenzelm@21263
   158
lemma even_nat_div_two_times_two: "even (x::nat) ==>
chaieb@23522
   159
    Suc (Suc 0) * (x div Suc (Suc 0)) = x" by presburger
wenzelm@21256
   160
wenzelm@21263
   161
lemma odd_nat_div_two_times_two_plus_one: "odd (x::nat) ==>
chaieb@23522
   162
    Suc( Suc (Suc 0) * (x div Suc (Suc 0))) = x" by presburger
wenzelm@21256
   163
wenzelm@21256
   164
lemma even_nat_equiv_def2: "even (x::nat) = (EX y. x = Suc (Suc 0) * y)"
nipkow@31148
   165
by presburger
wenzelm@21256
   166
wenzelm@21256
   167
lemma odd_nat_equiv_def2: "odd (x::nat) = (EX y. x = Suc(Suc (Suc 0) * y))"
nipkow@31148
   168
by presburger
wenzelm@21256
   169
haftmann@25600
   170
wenzelm@21256
   171
subsection {* Parity and powers *}
wenzelm@21256
   172
wenzelm@21263
   173
lemma  minus_one_even_odd_power:
haftmann@31017
   174
     "(even x --> (- 1::'a::{comm_ring_1})^x = 1) &
wenzelm@21256
   175
      (odd x --> (- 1::'a)^x = - 1)"
wenzelm@21256
   176
  apply (induct x)
wenzelm@21256
   177
  apply (rule conjI)
wenzelm@21256
   178
  apply simp
nipkow@31148
   179
  apply (insert even_zero_nat, blast)
wenzelm@21256
   180
  apply (simp add: power_Suc)
wenzelm@21263
   181
  done
wenzelm@21256
   182
wenzelm@21256
   183
lemma minus_one_even_power [simp]:
haftmann@31017
   184
    "even x ==> (- 1::'a::{comm_ring_1})^x = 1"
wenzelm@21263
   185
  using minus_one_even_odd_power by blast
wenzelm@21256
   186
wenzelm@21256
   187
lemma minus_one_odd_power [simp]:
haftmann@31017
   188
    "odd x ==> (- 1::'a::{comm_ring_1})^x = - 1"
wenzelm@21263
   189
  using minus_one_even_odd_power by blast
wenzelm@21256
   190
wenzelm@21256
   191
lemma neg_one_even_odd_power:
haftmann@31017
   192
     "(even x --> (-1::'a::{number_ring})^x = 1) &
wenzelm@21256
   193
      (odd x --> (-1::'a)^x = -1)"
wenzelm@21256
   194
  apply (induct x)
wenzelm@21256
   195
  apply (simp, simp add: power_Suc)
wenzelm@21256
   196
  done
wenzelm@21256
   197
wenzelm@21256
   198
lemma neg_one_even_power [simp]:
haftmann@31017
   199
    "even x ==> (-1::'a::{number_ring})^x = 1"
wenzelm@21263
   200
  using neg_one_even_odd_power by blast
wenzelm@21256
   201
wenzelm@21256
   202
lemma neg_one_odd_power [simp]:
haftmann@31017
   203
    "odd x ==> (-1::'a::{number_ring})^x = -1"
wenzelm@21263
   204
  using neg_one_even_odd_power by blast
wenzelm@21256
   205
wenzelm@21256
   206
lemma neg_power_if:
haftmann@31017
   207
     "(-x::'a::{comm_ring_1}) ^ n =
wenzelm@21256
   208
      (if even n then (x ^ n) else -(x ^ n))"
wenzelm@21263
   209
  apply (induct n)
wenzelm@21263
   210
  apply (simp_all split: split_if_asm add: power_Suc)
wenzelm@21263
   211
  done
wenzelm@21256
   212
wenzelm@21263
   213
lemma zero_le_even_power: "even n ==>
haftmann@31017
   214
    0 <= (x::'a::{ordered_ring_strict,monoid_mult}) ^ n"
wenzelm@21256
   215
  apply (simp add: even_nat_equiv_def2)
wenzelm@21256
   216
  apply (erule exE)
wenzelm@21256
   217
  apply (erule ssubst)
wenzelm@21256
   218
  apply (subst power_add)
wenzelm@21256
   219
  apply (rule zero_le_square)
wenzelm@21256
   220
  done
wenzelm@21256
   221
wenzelm@21263
   222
lemma zero_le_odd_power: "odd n ==>
haftmann@31017
   223
    (0 <= (x::'a::{ordered_idom}) ^ n) = (0 <= x)"
nipkow@30056
   224
apply (auto simp: odd_nat_equiv_def2 power_Suc power_add zero_le_mult_iff)
nipkow@30056
   225
apply (metis field_power_not_zero no_zero_divirors_neq0 order_antisym_conv zero_le_square)
nipkow@30056
   226
done
wenzelm@21256
   227
haftmann@31017
   228
lemma zero_le_power_eq[presburger]: "(0 <= (x::'a::{ordered_idom}) ^ n) =
wenzelm@21256
   229
    (even n | (odd n & 0 <= x))"
wenzelm@21256
   230
  apply auto
wenzelm@21263
   231
  apply (subst zero_le_odd_power [symmetric])
wenzelm@21256
   232
  apply assumption+
wenzelm@21256
   233
  apply (erule zero_le_even_power)
wenzelm@21263
   234
  done
wenzelm@21256
   235
haftmann@31017
   236
lemma zero_less_power_eq[presburger]: "(0 < (x::'a::{ordered_idom}) ^ n) =
wenzelm@21256
   237
    (n = 0 | (even n & x ~= 0) | (odd n & 0 < x))"
chaieb@27668
   238
chaieb@27668
   239
  unfolding order_less_le zero_le_power_eq by auto
wenzelm@21256
   240
haftmann@31017
   241
lemma power_less_zero_eq[presburger]: "((x::'a::{ordered_idom}) ^ n < 0) =
chaieb@27668
   242
    (odd n & x < 0)"
wenzelm@21263
   243
  apply (subst linorder_not_le [symmetric])+
wenzelm@21256
   244
  apply (subst zero_le_power_eq)
wenzelm@21256
   245
  apply auto
wenzelm@21263
   246
  done
wenzelm@21256
   247
haftmann@31017
   248
lemma power_le_zero_eq[presburger]: "((x::'a::{ordered_idom}) ^ n <= 0) =
wenzelm@21256
   249
    (n ~= 0 & ((odd n & x <= 0) | (even n & x = 0)))"
wenzelm@21263
   250
  apply (subst linorder_not_less [symmetric])+
wenzelm@21256
   251
  apply (subst zero_less_power_eq)
wenzelm@21256
   252
  apply auto
wenzelm@21263
   253
  done
wenzelm@21256
   254
wenzelm@21263
   255
lemma power_even_abs: "even n ==>
haftmann@31017
   256
    (abs (x::'a::{ordered_idom}))^n = x^n"
wenzelm@21263
   257
  apply (subst power_abs [symmetric])
wenzelm@21256
   258
  apply (simp add: zero_le_even_power)
wenzelm@21263
   259
  done
wenzelm@21256
   260
chaieb@23522
   261
lemma zero_less_power_nat_eq[presburger]: "(0 < (x::nat) ^ n) = (n = 0 | 0 < x)"
wenzelm@21263
   262
  by (induct n) auto
wenzelm@21256
   263
wenzelm@21263
   264
lemma power_minus_even [simp]: "even n ==>
haftmann@31017
   265
    (- x)^n = (x^n::'a::{comm_ring_1})"
wenzelm@21256
   266
  apply (subst power_minus)
wenzelm@21256
   267
  apply simp
wenzelm@21263
   268
  done
wenzelm@21256
   269
wenzelm@21263
   270
lemma power_minus_odd [simp]: "odd n ==>
haftmann@31017
   271
    (- x)^n = - (x^n::'a::{comm_ring_1})"
wenzelm@21256
   272
  apply (subst power_minus)
wenzelm@21256
   273
  apply simp
wenzelm@21263
   274
  done
wenzelm@21256
   275
haftmann@31017
   276
lemma power_mono_even: fixes x y :: "'a :: {ordered_idom}"
hoelzl@29803
   277
  assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
hoelzl@29803
   278
  shows "x^n \<le> y^n"
hoelzl@29803
   279
proof -
hoelzl@29803
   280
  have "0 \<le> \<bar>x\<bar>" by auto
hoelzl@29803
   281
  with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
hoelzl@29803
   282
  have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
hoelzl@29803
   283
  thus ?thesis unfolding power_even_abs[OF `even n`] .
hoelzl@29803
   284
qed
hoelzl@29803
   285
hoelzl@29803
   286
lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
hoelzl@29803
   287
haftmann@31017
   288
lemma power_mono_odd: fixes x y :: "'a :: {ordered_idom}"
hoelzl@29803
   289
  assumes "odd n" and "x \<le> y"
hoelzl@29803
   290
  shows "x^n \<le> y^n"
hoelzl@29803
   291
proof (cases "y < 0")
hoelzl@29803
   292
  case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
hoelzl@29803
   293
  hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
hoelzl@29803
   294
  thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
hoelzl@29803
   295
next
hoelzl@29803
   296
  case False
hoelzl@29803
   297
  show ?thesis
hoelzl@29803
   298
  proof (cases "x < 0")
hoelzl@29803
   299
    case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
hoelzl@29803
   300
    hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
hoelzl@29803
   301
    moreover
hoelzl@29803
   302
    from `\<not> y < 0` have "0 \<le> y" by auto
hoelzl@29803
   303
    hence "0 \<le> y^n" by auto
hoelzl@29803
   304
    ultimately show ?thesis by auto
hoelzl@29803
   305
  next
hoelzl@29803
   306
    case False hence "0 \<le> x" by auto
hoelzl@29803
   307
    with `x \<le> y` show ?thesis using power_mono by auto
hoelzl@29803
   308
  qed
hoelzl@29803
   309
qed
wenzelm@21263
   310
haftmann@25600
   311
subsection {* General Lemmas About Division *}
haftmann@25600
   312
haftmann@25600
   313
lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1" 
haftmann@25600
   314
apply (induct "m")
haftmann@25600
   315
apply (simp_all add: mod_Suc)
haftmann@25600
   316
done
haftmann@25600
   317
haftmann@25600
   318
declare Suc_times_mod_eq [of "number_of w", standard, simp]
haftmann@25600
   319
haftmann@25600
   320
lemma [simp]: "n div k \<le> (Suc n) div k"
haftmann@25600
   321
by (simp add: div_le_mono) 
haftmann@25600
   322
haftmann@25600
   323
lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"
haftmann@25600
   324
by arith
haftmann@25600
   325
haftmann@25600
   326
lemma div_2_gt_zero [simp]: "(1::nat) < n ==> 0 < n div 2" 
haftmann@25600
   327
by arith
haftmann@25600
   328
chaieb@27668
   329
  (* Potential use of algebra : Equality modulo n*)
haftmann@25600
   330
lemma mod_mult_self3 [simp]: "(k*n + m) mod n = m mod (n::nat)"
haftmann@25600
   331
by (simp add: mult_ac add_ac)
haftmann@25600
   332
haftmann@25600
   333
lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"
haftmann@25600
   334
proof -
haftmann@25600
   335
  have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp
haftmann@25600
   336
  also have "... = Suc m mod n" by (rule mod_mult_self3) 
haftmann@25600
   337
  finally show ?thesis .
haftmann@25600
   338
qed
haftmann@25600
   339
haftmann@25600
   340
lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"
haftmann@25600
   341
apply (subst mod_Suc [of m]) 
haftmann@25600
   342
apply (subst mod_Suc [of "m mod n"], simp) 
haftmann@25600
   343
done
haftmann@25600
   344
haftmann@25600
   345
haftmann@25600
   346
subsection {* More Even/Odd Results *}
haftmann@25600
   347
 
chaieb@27668
   348
lemma even_mult_two_ex: "even(n) = (\<exists>m::nat. n = 2*m)" by presburger
chaieb@27668
   349
lemma odd_Suc_mult_two_ex: "odd(n) = (\<exists>m. n = Suc (2*m))" by presburger
chaieb@27668
   350
lemma even_add [simp]: "even(m + n::nat) = (even m = even n)"  by presburger
haftmann@25600
   351
chaieb@27668
   352
lemma odd_add [simp]: "odd(m + n::nat) = (odd m \<noteq> odd n)" by presburger
haftmann@25600
   353
haftmann@25600
   354
lemma div_Suc: "Suc a div c = a div c + Suc 0 div c +
haftmann@25600
   355
    (a mod c + Suc 0 mod c) div c" 
haftmann@25600
   356
  apply (subgoal_tac "Suc a = a + Suc 0")
haftmann@25600
   357
  apply (erule ssubst)
haftmann@25600
   358
  apply (rule div_add1_eq, simp)
haftmann@25600
   359
  done
haftmann@25600
   360
chaieb@27668
   361
lemma lemma_even_div2 [simp]: "even (n::nat) ==> (n + 1) div 2 = n div 2" by presburger
haftmann@25600
   362
haftmann@25600
   363
lemma lemma_not_even_div2 [simp]: "~even n ==> (n + 1) div 2 = Suc (n div 2)"
chaieb@27668
   364
by presburger
haftmann@25600
   365
chaieb@27668
   366
lemma even_num_iff: "0 < n ==> even n = (~ even(n - 1 :: nat))"  by presburger
chaieb@27668
   367
lemma even_even_mod_4_iff: "even (n::nat) = even (n mod 4)" by presburger
haftmann@25600
   368
chaieb@27668
   369
lemma lemma_odd_mod_4_div_2: "n mod 4 = (3::nat) ==> odd((n - 1) div 2)" by presburger
haftmann@25600
   370
haftmann@25600
   371
lemma lemma_even_mod_4_div_2: "n mod 4 = (1::nat) ==> even ((n - 1) div 2)"
chaieb@27668
   372
  by presburger
haftmann@25600
   373
wenzelm@21263
   374
text {* Simplify, when the exponent is a numeral *}
wenzelm@21256
   375
wenzelm@21256
   376
lemmas power_0_left_number_of = power_0_left [of "number_of w", standard]
wenzelm@21256
   377
declare power_0_left_number_of [simp]
wenzelm@21256
   378
wenzelm@21263
   379
lemmas zero_le_power_eq_number_of [simp] =
wenzelm@21256
   380
    zero_le_power_eq [of _ "number_of w", standard]
wenzelm@21256
   381
wenzelm@21263
   382
lemmas zero_less_power_eq_number_of [simp] =
wenzelm@21256
   383
    zero_less_power_eq [of _ "number_of w", standard]
wenzelm@21256
   384
wenzelm@21263
   385
lemmas power_le_zero_eq_number_of [simp] =
wenzelm@21256
   386
    power_le_zero_eq [of _ "number_of w", standard]
wenzelm@21256
   387
wenzelm@21263
   388
lemmas power_less_zero_eq_number_of [simp] =
wenzelm@21256
   389
    power_less_zero_eq [of _ "number_of w", standard]
wenzelm@21256
   390
wenzelm@21263
   391
lemmas zero_less_power_nat_eq_number_of [simp] =
wenzelm@21256
   392
    zero_less_power_nat_eq [of _ "number_of w", standard]
wenzelm@21256
   393
wenzelm@21263
   394
lemmas power_eq_0_iff_number_of [simp] = power_eq_0_iff [of _ "number_of w", standard]
wenzelm@21256
   395
wenzelm@21263
   396
lemmas power_even_abs_number_of [simp] = power_even_abs [of "number_of w" _, standard]
wenzelm@21256
   397
wenzelm@21256
   398
wenzelm@21256
   399
subsection {* An Equivalence for @{term [source] "0 \<le> a^n"} *}
wenzelm@21256
   400
wenzelm@21256
   401
lemma even_power_le_0_imp_0:
haftmann@31017
   402
    "a ^ (2*k) \<le> (0::'a::{ordered_idom}) ==> a=0"
wenzelm@21263
   403
  by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff power_Suc)
wenzelm@21256
   404
chaieb@23522
   405
lemma zero_le_power_iff[presburger]:
haftmann@31017
   406
  "(0 \<le> a^n) = (0 \<le> (a::'a::{ordered_idom}) | even n)"
wenzelm@21256
   407
proof cases
wenzelm@21256
   408
  assume even: "even n"
wenzelm@21256
   409
  then obtain k where "n = 2*k"
wenzelm@21256
   410
    by (auto simp add: even_nat_equiv_def2 numeral_2_eq_2)
wenzelm@21263
   411
  thus ?thesis by (simp add: zero_le_even_power even)
wenzelm@21256
   412
next
wenzelm@21256
   413
  assume odd: "odd n"
wenzelm@21256
   414
  then obtain k where "n = Suc(2*k)"
wenzelm@21256
   415
    by (auto simp add: odd_nat_equiv_def2 numeral_2_eq_2)
wenzelm@21256
   416
  thus ?thesis
wenzelm@21263
   417
    by (auto simp add: power_Suc zero_le_mult_iff zero_le_even_power
wenzelm@21263
   418
             dest!: even_power_le_0_imp_0)
wenzelm@21263
   419
qed
wenzelm@21263
   420
wenzelm@21256
   421
wenzelm@21256
   422
subsection {* Miscellaneous *}
wenzelm@21256
   423
chaieb@23522
   424
lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
chaieb@23522
   425
lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
chaieb@23522
   426
lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2"  by presburger
chaieb@23522
   427
lemma odd_plus_one_div_two: "odd (x::int) ==> (x + 1) div 2 = x div 2 + 1" by presburger
wenzelm@21256
   428
chaieb@23522
   429
lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
chaieb@23522
   430
lemma [presburger]: "(Suc x) div Suc (Suc 0) = x div Suc (Suc 0) \<longleftrightarrow> even x" by presburger
wenzelm@21263
   431
lemma even_nat_plus_one_div_two: "even (x::nat) ==>
chaieb@23522
   432
    (Suc x) div Suc (Suc 0) = x div Suc (Suc 0)" by presburger
wenzelm@21256
   433
wenzelm@21263
   434
lemma odd_nat_plus_one_div_two: "odd (x::nat) ==>
chaieb@23522
   435
    (Suc x) div Suc (Suc 0) = Suc (x div Suc (Suc 0))" by presburger
wenzelm@21256
   436
wenzelm@21256
   437
end