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