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