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