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