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