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