src/HOL/Power.thy
author huffman
Fri Mar 30 09:08:29 2012 +0200 (2012-03-30)
changeset 47209 4893907fe872
parent 47192 0c0501cb6da6
child 47220 52426c62b5d0
permissions -rw-r--r--
add constant pred_numeral k = numeral k - (1::nat);
replace several simp rules from Nat_Numeral.thy with new ones that use pred_numeral
paulson@3390
     1
(*  Title:      HOL/Power.thy
paulson@3390
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@3390
     3
    Copyright   1997  University of Cambridge
paulson@3390
     4
*)
paulson@3390
     5
haftmann@30960
     6
header {* Exponentiation *}
paulson@14348
     7
nipkow@15131
     8
theory Power
huffman@47191
     9
imports Num
nipkow@15131
    10
begin
paulson@14348
    11
haftmann@30960
    12
subsection {* Powers for Arbitrary Monoids *}
haftmann@30960
    13
haftmann@30996
    14
class power = one + times
haftmann@30960
    15
begin
haftmann@24996
    16
haftmann@30960
    17
primrec power :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixr "^" 80) where
haftmann@30960
    18
    power_0: "a ^ 0 = 1"
haftmann@30960
    19
  | power_Suc: "a ^ Suc n = a * a ^ n"
paulson@14348
    20
haftmann@30996
    21
notation (latex output)
haftmann@30996
    22
  power ("(_\<^bsup>_\<^esup>)" [1000] 1000)
haftmann@30996
    23
haftmann@30996
    24
notation (HTML output)
haftmann@30996
    25
  power ("(_\<^bsup>_\<^esup>)" [1000] 1000)
haftmann@30996
    26
huffman@47192
    27
text {* Special syntax for squares. *}
huffman@47192
    28
huffman@47192
    29
abbreviation (xsymbols)
huffman@47192
    30
  power2 :: "'a \<Rightarrow> 'a"  ("(_\<twosuperior>)" [1000] 999) where
huffman@47192
    31
  "x\<twosuperior> \<equiv> x ^ 2"
huffman@47192
    32
huffman@47192
    33
notation (latex output)
huffman@47192
    34
  power2  ("(_\<twosuperior>)" [1000] 999)
huffman@47192
    35
huffman@47192
    36
notation (HTML output)
huffman@47192
    37
  power2  ("(_\<twosuperior>)" [1000] 999)
huffman@47192
    38
haftmann@30960
    39
end
paulson@14348
    40
haftmann@30996
    41
context monoid_mult
haftmann@30996
    42
begin
paulson@14348
    43
wenzelm@39438
    44
subclass power .
paulson@14348
    45
haftmann@30996
    46
lemma power_one [simp]:
haftmann@30996
    47
  "1 ^ n = 1"
huffman@30273
    48
  by (induct n) simp_all
paulson@14348
    49
haftmann@30996
    50
lemma power_one_right [simp]:
haftmann@31001
    51
  "a ^ 1 = a"
haftmann@30996
    52
  by simp
paulson@14348
    53
haftmann@30996
    54
lemma power_commutes:
haftmann@30996
    55
  "a ^ n * a = a * a ^ n"
huffman@30273
    56
  by (induct n) (simp_all add: mult_assoc)
krauss@21199
    57
haftmann@30996
    58
lemma power_Suc2:
haftmann@30996
    59
  "a ^ Suc n = a ^ n * a"
huffman@30273
    60
  by (simp add: power_commutes)
huffman@28131
    61
haftmann@30996
    62
lemma power_add:
haftmann@30996
    63
  "a ^ (m + n) = a ^ m * a ^ n"
haftmann@30996
    64
  by (induct m) (simp_all add: algebra_simps)
paulson@14348
    65
haftmann@30996
    66
lemma power_mult:
haftmann@30996
    67
  "a ^ (m * n) = (a ^ m) ^ n"
huffman@30273
    68
  by (induct n) (simp_all add: power_add)
paulson@14348
    69
huffman@47192
    70
lemma power2_eq_square: "a\<twosuperior> = a * a"
huffman@47192
    71
  by (simp add: numeral_2_eq_2)
huffman@47192
    72
huffman@47192
    73
lemma power3_eq_cube: "a ^ 3 = a * a * a"
huffman@47192
    74
  by (simp add: numeral_3_eq_3 mult_assoc)
huffman@47192
    75
huffman@47192
    76
lemma power_even_eq:
huffman@47192
    77
  "a ^ (2*n) = (a ^ n) ^ 2"
huffman@47192
    78
  by (subst mult_commute) (simp add: power_mult)
huffman@47192
    79
huffman@47192
    80
lemma power_odd_eq:
huffman@47192
    81
  "a ^ Suc (2*n) = a * (a ^ n) ^ 2"
huffman@47192
    82
  by (simp add: power_even_eq)
huffman@47192
    83
haftmann@30996
    84
end
haftmann@30996
    85
haftmann@30996
    86
context comm_monoid_mult
haftmann@30996
    87
begin
haftmann@30996
    88
haftmann@30996
    89
lemma power_mult_distrib:
haftmann@30996
    90
  "(a * b) ^ n = (a ^ n) * (b ^ n)"
huffman@30273
    91
  by (induct n) (simp_all add: mult_ac)
paulson@14348
    92
haftmann@30996
    93
end
haftmann@30996
    94
huffman@47191
    95
context semiring_numeral
huffman@47191
    96
begin
huffman@47191
    97
huffman@47191
    98
lemma numeral_sqr: "numeral (Num.sqr k) = numeral k * numeral k"
huffman@47191
    99
  by (simp only: sqr_conv_mult numeral_mult)
huffman@47191
   100
huffman@47191
   101
lemma numeral_pow: "numeral (Num.pow k l) = numeral k ^ numeral l"
huffman@47191
   102
  by (induct l, simp_all only: numeral_class.numeral.simps pow.simps
huffman@47191
   103
    numeral_sqr numeral_mult power_add power_one_right)
huffman@47191
   104
huffman@47191
   105
lemma power_numeral [simp]: "numeral k ^ numeral l = numeral (Num.pow k l)"
huffman@47191
   106
  by (rule numeral_pow [symmetric])
huffman@47191
   107
huffman@47191
   108
end
huffman@47191
   109
haftmann@30996
   110
context semiring_1
haftmann@30996
   111
begin
haftmann@30996
   112
haftmann@30996
   113
lemma of_nat_power:
haftmann@30996
   114
  "of_nat (m ^ n) = of_nat m ^ n"
haftmann@30996
   115
  by (induct n) (simp_all add: of_nat_mult)
haftmann@30996
   116
huffman@47191
   117
lemma power_zero_numeral [simp]: "(0::'a) ^ numeral k = 0"
huffman@47209
   118
  by (simp add: numeral_eq_Suc)
huffman@47191
   119
huffman@47192
   120
lemma zero_power2: "0\<twosuperior> = 0" (* delete? *)
huffman@47192
   121
  by (rule power_zero_numeral)
huffman@47192
   122
huffman@47192
   123
lemma one_power2: "1\<twosuperior> = 1" (* delete? *)
huffman@47192
   124
  by (rule power_one)
huffman@47192
   125
haftmann@30996
   126
end
haftmann@30996
   127
haftmann@30996
   128
context comm_semiring_1
haftmann@30996
   129
begin
haftmann@30996
   130
haftmann@30996
   131
text {* The divides relation *}
haftmann@30996
   132
haftmann@30996
   133
lemma le_imp_power_dvd:
haftmann@30996
   134
  assumes "m \<le> n" shows "a ^ m dvd a ^ n"
haftmann@30996
   135
proof
haftmann@30996
   136
  have "a ^ n = a ^ (m + (n - m))"
haftmann@30996
   137
    using `m \<le> n` by simp
haftmann@30996
   138
  also have "\<dots> = a ^ m * a ^ (n - m)"
haftmann@30996
   139
    by (rule power_add)
haftmann@30996
   140
  finally show "a ^ n = a ^ m * a ^ (n - m)" .
haftmann@30996
   141
qed
haftmann@30996
   142
haftmann@30996
   143
lemma power_le_dvd:
haftmann@30996
   144
  "a ^ n dvd b \<Longrightarrow> m \<le> n \<Longrightarrow> a ^ m dvd b"
haftmann@30996
   145
  by (rule dvd_trans [OF le_imp_power_dvd])
haftmann@30996
   146
haftmann@30996
   147
lemma dvd_power_same:
haftmann@30996
   148
  "x dvd y \<Longrightarrow> x ^ n dvd y ^ n"
haftmann@30996
   149
  by (induct n) (auto simp add: mult_dvd_mono)
haftmann@30996
   150
haftmann@30996
   151
lemma dvd_power_le:
haftmann@30996
   152
  "x dvd y \<Longrightarrow> m \<ge> n \<Longrightarrow> x ^ n dvd y ^ m"
haftmann@30996
   153
  by (rule power_le_dvd [OF dvd_power_same])
paulson@14348
   154
haftmann@30996
   155
lemma dvd_power [simp]:
haftmann@30996
   156
  assumes "n > (0::nat) \<or> x = 1"
haftmann@30996
   157
  shows "x dvd (x ^ n)"
haftmann@30996
   158
using assms proof
haftmann@30996
   159
  assume "0 < n"
haftmann@30996
   160
  then have "x ^ n = x ^ Suc (n - 1)" by simp
haftmann@30996
   161
  then show "x dvd (x ^ n)" by simp
haftmann@30996
   162
next
haftmann@30996
   163
  assume "x = 1"
haftmann@30996
   164
  then show "x dvd (x ^ n)" by simp
haftmann@30996
   165
qed
haftmann@30996
   166
haftmann@30996
   167
end
haftmann@30996
   168
haftmann@30996
   169
context ring_1
haftmann@30996
   170
begin
haftmann@30996
   171
haftmann@30996
   172
lemma power_minus:
haftmann@30996
   173
  "(- a) ^ n = (- 1) ^ n * a ^ n"
haftmann@30996
   174
proof (induct n)
haftmann@30996
   175
  case 0 show ?case by simp
haftmann@30996
   176
next
haftmann@30996
   177
  case (Suc n) then show ?case
haftmann@30996
   178
    by (simp del: power_Suc add: power_Suc2 mult_assoc)
haftmann@30996
   179
qed
haftmann@30996
   180
huffman@47191
   181
lemma power_minus_Bit0:
huffman@47191
   182
  "(- x) ^ numeral (Num.Bit0 k) = x ^ numeral (Num.Bit0 k)"
huffman@47191
   183
  by (induct k, simp_all only: numeral_class.numeral.simps power_add
huffman@47191
   184
    power_one_right mult_minus_left mult_minus_right minus_minus)
huffman@47191
   185
huffman@47191
   186
lemma power_minus_Bit1:
huffman@47191
   187
  "(- x) ^ numeral (Num.Bit1 k) = - (x ^ numeral (Num.Bit1 k))"
huffman@47191
   188
  by (simp only: nat_number(4) power_Suc power_minus_Bit0 mult_minus_left)
huffman@47191
   189
huffman@47191
   190
lemma power_neg_numeral_Bit0 [simp]:
huffman@47191
   191
  "neg_numeral k ^ numeral (Num.Bit0 l) = numeral (Num.pow k (Num.Bit0 l))"
huffman@47191
   192
  by (simp only: neg_numeral_def power_minus_Bit0 power_numeral)
huffman@47191
   193
huffman@47191
   194
lemma power_neg_numeral_Bit1 [simp]:
huffman@47191
   195
  "neg_numeral k ^ numeral (Num.Bit1 l) = neg_numeral (Num.pow k (Num.Bit1 l))"
huffman@47191
   196
  by (simp only: neg_numeral_def power_minus_Bit1 power_numeral pow.simps)
huffman@47191
   197
huffman@47192
   198
lemma power2_minus [simp]:
huffman@47192
   199
  "(- a)\<twosuperior> = a\<twosuperior>"
huffman@47192
   200
  by (rule power_minus_Bit0)
huffman@47192
   201
huffman@47192
   202
lemma power_minus1_even [simp]:
huffman@47192
   203
  "-1 ^ (2*n) = 1"
huffman@47192
   204
proof (induct n)
huffman@47192
   205
  case 0 show ?case by simp
huffman@47192
   206
next
huffman@47192
   207
  case (Suc n) then show ?case by (simp add: power_add power2_eq_square)
huffman@47192
   208
qed
huffman@47192
   209
huffman@47192
   210
lemma power_minus1_odd:
huffman@47192
   211
  "-1 ^ Suc (2*n) = -1"
huffman@47192
   212
  by simp
huffman@47192
   213
huffman@47192
   214
lemma power_minus_even [simp]:
huffman@47192
   215
  "(-a) ^ (2*n) = a ^ (2*n)"
huffman@47192
   216
  by (simp add: power_minus [of a])
huffman@47192
   217
huffman@47192
   218
end
huffman@47192
   219
huffman@47192
   220
context ring_1_no_zero_divisors
huffman@47192
   221
begin
huffman@47192
   222
huffman@47192
   223
lemma field_power_not_zero:
huffman@47192
   224
  "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
huffman@47192
   225
  by (induct n) auto
huffman@47192
   226
huffman@47192
   227
lemma zero_eq_power2 [simp]:
huffman@47192
   228
  "a\<twosuperior> = 0 \<longleftrightarrow> a = 0"
huffman@47192
   229
  unfolding power2_eq_square by simp
huffman@47192
   230
huffman@47192
   231
lemma power2_eq_1_iff:
huffman@47192
   232
  "a\<twosuperior> = 1 \<longleftrightarrow> a = 1 \<or> a = - 1"
huffman@47192
   233
  unfolding power2_eq_square by (rule square_eq_1_iff)
huffman@47192
   234
huffman@47192
   235
end
huffman@47192
   236
huffman@47192
   237
context idom
huffman@47192
   238
begin
huffman@47192
   239
huffman@47192
   240
lemma power2_eq_iff: "x\<twosuperior> = y\<twosuperior> \<longleftrightarrow> x = y \<or> x = - y"
huffman@47192
   241
  unfolding power2_eq_square by (rule square_eq_iff)
huffman@47192
   242
huffman@47192
   243
end
huffman@47192
   244
huffman@47192
   245
context division_ring
huffman@47192
   246
begin
huffman@47192
   247
huffman@47192
   248
text {* FIXME reorient or rename to @{text nonzero_inverse_power} *}
huffman@47192
   249
lemma nonzero_power_inverse:
huffman@47192
   250
  "a \<noteq> 0 \<Longrightarrow> inverse (a ^ n) = (inverse a) ^ n"
huffman@47192
   251
  by (induct n)
huffman@47192
   252
    (simp_all add: nonzero_inverse_mult_distrib power_commutes field_power_not_zero)
huffman@47192
   253
huffman@47192
   254
end
huffman@47192
   255
huffman@47192
   256
context field
huffman@47192
   257
begin
huffman@47192
   258
huffman@47192
   259
lemma nonzero_power_divide:
huffman@47192
   260
  "b \<noteq> 0 \<Longrightarrow> (a / b) ^ n = a ^ n / b ^ n"
huffman@47192
   261
  by (simp add: divide_inverse power_mult_distrib nonzero_power_inverse)
huffman@47192
   262
huffman@47192
   263
end
huffman@47192
   264
huffman@47192
   265
huffman@47192
   266
subsection {* Exponentiation on ordered types *}
huffman@47192
   267
huffman@47192
   268
context linordered_ring (* TODO: move *)
huffman@47192
   269
begin
huffman@47192
   270
huffman@47192
   271
lemma sum_squares_ge_zero:
huffman@47192
   272
  "0 \<le> x * x + y * y"
huffman@47192
   273
  by (intro add_nonneg_nonneg zero_le_square)
huffman@47192
   274
huffman@47192
   275
lemma not_sum_squares_lt_zero:
huffman@47192
   276
  "\<not> x * x + y * y < 0"
huffman@47192
   277
  by (simp add: not_less sum_squares_ge_zero)
huffman@47192
   278
haftmann@30996
   279
end
haftmann@30996
   280
haftmann@35028
   281
context linordered_semidom
haftmann@30996
   282
begin
haftmann@30996
   283
haftmann@30996
   284
lemma zero_less_power [simp]:
haftmann@30996
   285
  "0 < a \<Longrightarrow> 0 < a ^ n"
haftmann@30996
   286
  by (induct n) (simp_all add: mult_pos_pos)
haftmann@30996
   287
haftmann@30996
   288
lemma zero_le_power [simp]:
haftmann@30996
   289
  "0 \<le> a \<Longrightarrow> 0 \<le> a ^ n"
haftmann@30996
   290
  by (induct n) (simp_all add: mult_nonneg_nonneg)
paulson@14348
   291
nipkow@25874
   292
lemma one_le_power[simp]:
haftmann@30996
   293
  "1 \<le> a \<Longrightarrow> 1 \<le> a ^ n"
haftmann@30996
   294
  apply (induct n)
haftmann@30996
   295
  apply simp_all
haftmann@30996
   296
  apply (rule order_trans [OF _ mult_mono [of 1 _ 1]])
haftmann@30996
   297
  apply (simp_all add: order_trans [OF zero_le_one])
haftmann@30996
   298
  done
paulson@14348
   299
paulson@14348
   300
lemma power_gt1_lemma:
haftmann@30996
   301
  assumes gt1: "1 < a"
haftmann@30996
   302
  shows "1 < a * a ^ n"
paulson@14348
   303
proof -
haftmann@30996
   304
  from gt1 have "0 \<le> a"
haftmann@30996
   305
    by (fact order_trans [OF zero_le_one less_imp_le])
haftmann@30996
   306
  have "1 * 1 < a * 1" using gt1 by simp
haftmann@30996
   307
  also have "\<dots> \<le> a * a ^ n" using gt1
haftmann@30996
   308
    by (simp only: mult_mono `0 \<le> a` one_le_power order_less_imp_le
wenzelm@14577
   309
        zero_le_one order_refl)
wenzelm@14577
   310
  finally show ?thesis by simp
paulson@14348
   311
qed
paulson@14348
   312
haftmann@30996
   313
lemma power_gt1:
haftmann@30996
   314
  "1 < a \<Longrightarrow> 1 < a ^ Suc n"
haftmann@30996
   315
  by (simp add: power_gt1_lemma)
huffman@24376
   316
haftmann@30996
   317
lemma one_less_power [simp]:
haftmann@30996
   318
  "1 < a \<Longrightarrow> 0 < n \<Longrightarrow> 1 < a ^ n"
haftmann@30996
   319
  by (cases n) (simp_all add: power_gt1_lemma)
paulson@14348
   320
paulson@14348
   321
lemma power_le_imp_le_exp:
haftmann@30996
   322
  assumes gt1: "1 < a"
haftmann@30996
   323
  shows "a ^ m \<le> a ^ n \<Longrightarrow> m \<le> n"
haftmann@30996
   324
proof (induct m arbitrary: n)
paulson@14348
   325
  case 0
wenzelm@14577
   326
  show ?case by simp
paulson@14348
   327
next
paulson@14348
   328
  case (Suc m)
wenzelm@14577
   329
  show ?case
wenzelm@14577
   330
  proof (cases n)
wenzelm@14577
   331
    case 0
haftmann@30996
   332
    with Suc.prems Suc.hyps have "a * a ^ m \<le> 1" by simp
wenzelm@14577
   333
    with gt1 show ?thesis
wenzelm@14577
   334
      by (force simp only: power_gt1_lemma
haftmann@30996
   335
          not_less [symmetric])
wenzelm@14577
   336
  next
wenzelm@14577
   337
    case (Suc n)
haftmann@30996
   338
    with Suc.prems Suc.hyps show ?thesis
wenzelm@14577
   339
      by (force dest: mult_left_le_imp_le
haftmann@30996
   340
          simp add: less_trans [OF zero_less_one gt1])
wenzelm@14577
   341
  qed
paulson@14348
   342
qed
paulson@14348
   343
wenzelm@14577
   344
text{*Surely we can strengthen this? It holds for @{text "0<a<1"} too.*}
paulson@14348
   345
lemma power_inject_exp [simp]:
haftmann@30996
   346
  "1 < a \<Longrightarrow> a ^ m = a ^ n \<longleftrightarrow> m = n"
wenzelm@14577
   347
  by (force simp add: order_antisym power_le_imp_le_exp)
paulson@14348
   348
paulson@14348
   349
text{*Can relax the first premise to @{term "0<a"} in the case of the
paulson@14348
   350
natural numbers.*}
paulson@14348
   351
lemma power_less_imp_less_exp:
haftmann@30996
   352
  "1 < a \<Longrightarrow> a ^ m < a ^ n \<Longrightarrow> m < n"
haftmann@30996
   353
  by (simp add: order_less_le [of m n] less_le [of "a^m" "a^n"]
haftmann@30996
   354
    power_le_imp_le_exp)
paulson@14348
   355
paulson@14348
   356
lemma power_mono:
haftmann@30996
   357
  "a \<le> b \<Longrightarrow> 0 \<le> a \<Longrightarrow> a ^ n \<le> b ^ n"
haftmann@30996
   358
  by (induct n)
haftmann@30996
   359
    (auto intro: mult_mono order_trans [of 0 a b])
paulson@14348
   360
paulson@14348
   361
lemma power_strict_mono [rule_format]:
haftmann@30996
   362
  "a < b \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 < n \<longrightarrow> a ^ n < b ^ n"
haftmann@30996
   363
  by (induct n)
haftmann@30996
   364
   (auto simp add: mult_strict_mono le_less_trans [of 0 a b])
paulson@14348
   365
paulson@14348
   366
text{*Lemma for @{text power_strict_decreasing}*}
paulson@14348
   367
lemma power_Suc_less:
haftmann@30996
   368
  "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a * a ^ n < a ^ n"
haftmann@30996
   369
  by (induct n)
haftmann@30996
   370
    (auto simp add: mult_strict_left_mono)
paulson@14348
   371
haftmann@30996
   372
lemma power_strict_decreasing [rule_format]:
haftmann@30996
   373
  "n < N \<Longrightarrow> 0 < a \<Longrightarrow> a < 1 \<longrightarrow> a ^ N < a ^ n"
haftmann@30996
   374
proof (induct N)
haftmann@30996
   375
  case 0 then show ?case by simp
haftmann@30996
   376
next
haftmann@30996
   377
  case (Suc N) then show ?case 
haftmann@30996
   378
  apply (auto simp add: power_Suc_less less_Suc_eq)
haftmann@30996
   379
  apply (subgoal_tac "a * a^N < 1 * a^n")
haftmann@30996
   380
  apply simp
haftmann@30996
   381
  apply (rule mult_strict_mono) apply auto
haftmann@30996
   382
  done
haftmann@30996
   383
qed
paulson@14348
   384
paulson@14348
   385
text{*Proof resembles that of @{text power_strict_decreasing}*}
haftmann@30996
   386
lemma power_decreasing [rule_format]:
haftmann@30996
   387
  "n \<le> N \<Longrightarrow> 0 \<le> a \<Longrightarrow> a \<le> 1 \<longrightarrow> a ^ N \<le> a ^ n"
haftmann@30996
   388
proof (induct N)
haftmann@30996
   389
  case 0 then show ?case by simp
haftmann@30996
   390
next
haftmann@30996
   391
  case (Suc N) then show ?case 
haftmann@30996
   392
  apply (auto simp add: le_Suc_eq)
haftmann@30996
   393
  apply (subgoal_tac "a * a^N \<le> 1 * a^n", simp)
haftmann@30996
   394
  apply (rule mult_mono) apply auto
haftmann@30996
   395
  done
haftmann@30996
   396
qed
paulson@14348
   397
paulson@14348
   398
lemma power_Suc_less_one:
haftmann@30996
   399
  "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a ^ Suc n < 1"
haftmann@30996
   400
  using power_strict_decreasing [of 0 "Suc n" a] by simp
paulson@14348
   401
paulson@14348
   402
text{*Proof again resembles that of @{text power_strict_decreasing}*}
haftmann@30996
   403
lemma power_increasing [rule_format]:
haftmann@30996
   404
  "n \<le> N \<Longrightarrow> 1 \<le> a \<Longrightarrow> a ^ n \<le> a ^ N"
haftmann@30996
   405
proof (induct N)
haftmann@30996
   406
  case 0 then show ?case by simp
haftmann@30996
   407
next
haftmann@30996
   408
  case (Suc N) then show ?case 
haftmann@30996
   409
  apply (auto simp add: le_Suc_eq)
haftmann@30996
   410
  apply (subgoal_tac "1 * a^n \<le> a * a^N", simp)
haftmann@30996
   411
  apply (rule mult_mono) apply (auto simp add: order_trans [OF zero_le_one])
haftmann@30996
   412
  done
haftmann@30996
   413
qed
paulson@14348
   414
paulson@14348
   415
text{*Lemma for @{text power_strict_increasing}*}
paulson@14348
   416
lemma power_less_power_Suc:
haftmann@30996
   417
  "1 < a \<Longrightarrow> a ^ n < a * a ^ n"
haftmann@30996
   418
  by (induct n) (auto simp add: mult_strict_left_mono less_trans [OF zero_less_one])
paulson@14348
   419
haftmann@30996
   420
lemma power_strict_increasing [rule_format]:
haftmann@30996
   421
  "n < N \<Longrightarrow> 1 < a \<longrightarrow> a ^ n < a ^ N"
haftmann@30996
   422
proof (induct N)
haftmann@30996
   423
  case 0 then show ?case by simp
haftmann@30996
   424
next
haftmann@30996
   425
  case (Suc N) then show ?case 
haftmann@30996
   426
  apply (auto simp add: power_less_power_Suc less_Suc_eq)
haftmann@30996
   427
  apply (subgoal_tac "1 * a^n < a * a^N", simp)
haftmann@30996
   428
  apply (rule mult_strict_mono) apply (auto simp add: less_trans [OF zero_less_one] less_imp_le)
haftmann@30996
   429
  done
haftmann@30996
   430
qed
paulson@14348
   431
nipkow@25134
   432
lemma power_increasing_iff [simp]:
haftmann@30996
   433
  "1 < b \<Longrightarrow> b ^ x \<le> b ^ y \<longleftrightarrow> x \<le> y"
haftmann@30996
   434
  by (blast intro: power_le_imp_le_exp power_increasing less_imp_le)
paulson@15066
   435
paulson@15066
   436
lemma power_strict_increasing_iff [simp]:
haftmann@30996
   437
  "1 < b \<Longrightarrow> b ^ x < b ^ y \<longleftrightarrow> x < y"
nipkow@25134
   438
by (blast intro: power_less_imp_less_exp power_strict_increasing) 
paulson@15066
   439
paulson@14348
   440
lemma power_le_imp_le_base:
haftmann@30996
   441
  assumes le: "a ^ Suc n \<le> b ^ Suc n"
haftmann@30996
   442
    and ynonneg: "0 \<le> b"
haftmann@30996
   443
  shows "a \<le> b"
nipkow@25134
   444
proof (rule ccontr)
nipkow@25134
   445
  assume "~ a \<le> b"
nipkow@25134
   446
  then have "b < a" by (simp only: linorder_not_le)
nipkow@25134
   447
  then have "b ^ Suc n < a ^ Suc n"
wenzelm@41550
   448
    by (simp only: assms power_strict_mono)
haftmann@30996
   449
  from le and this show False
nipkow@25134
   450
    by (simp add: linorder_not_less [symmetric])
nipkow@25134
   451
qed
wenzelm@14577
   452
huffman@22853
   453
lemma power_less_imp_less_base:
huffman@22853
   454
  assumes less: "a ^ n < b ^ n"
huffman@22853
   455
  assumes nonneg: "0 \<le> b"
huffman@22853
   456
  shows "a < b"
huffman@22853
   457
proof (rule contrapos_pp [OF less])
huffman@22853
   458
  assume "~ a < b"
huffman@22853
   459
  hence "b \<le> a" by (simp only: linorder_not_less)
huffman@22853
   460
  hence "b ^ n \<le> a ^ n" using nonneg by (rule power_mono)
haftmann@30996
   461
  thus "\<not> a ^ n < b ^ n" by (simp only: linorder_not_less)
huffman@22853
   462
qed
huffman@22853
   463
paulson@14348
   464
lemma power_inject_base:
haftmann@30996
   465
  "a ^ Suc n = b ^ Suc n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a = b"
haftmann@30996
   466
by (blast intro: power_le_imp_le_base antisym eq_refl sym)
paulson@14348
   467
huffman@22955
   468
lemma power_eq_imp_eq_base:
haftmann@30996
   469
  "a ^ n = b ^ n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 < n \<Longrightarrow> a = b"
haftmann@30996
   470
  by (cases n) (simp_all del: power_Suc, rule power_inject_base)
huffman@22955
   471
huffman@47192
   472
lemma power2_le_imp_le:
huffman@47192
   473
  "x\<twosuperior> \<le> y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y"
huffman@47192
   474
  unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
huffman@47192
   475
huffman@47192
   476
lemma power2_less_imp_less:
huffman@47192
   477
  "x\<twosuperior> < y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x < y"
huffman@47192
   478
  by (rule power_less_imp_less_base)
huffman@47192
   479
huffman@47192
   480
lemma power2_eq_imp_eq:
huffman@47192
   481
  "x\<twosuperior> = y\<twosuperior> \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x = y"
huffman@47192
   482
  unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base) simp
huffman@47192
   483
huffman@47192
   484
end
huffman@47192
   485
huffman@47192
   486
context linordered_ring_strict
huffman@47192
   487
begin
huffman@47192
   488
huffman@47192
   489
lemma sum_squares_eq_zero_iff:
huffman@47192
   490
  "x * x + y * y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
huffman@47192
   491
  by (simp add: add_nonneg_eq_0_iff)
huffman@47192
   492
huffman@47192
   493
lemma sum_squares_le_zero_iff:
huffman@47192
   494
  "x * x + y * y \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
huffman@47192
   495
  by (simp add: le_less not_sum_squares_lt_zero sum_squares_eq_zero_iff)
huffman@47192
   496
huffman@47192
   497
lemma sum_squares_gt_zero_iff:
huffman@47192
   498
  "0 < x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
huffman@47192
   499
  by (simp add: not_le [symmetric] sum_squares_le_zero_iff)
huffman@47192
   500
haftmann@30996
   501
end
haftmann@30996
   502
haftmann@35028
   503
context linordered_idom
haftmann@30996
   504
begin
huffman@29978
   505
haftmann@30996
   506
lemma power_abs:
haftmann@30996
   507
  "abs (a ^ n) = abs a ^ n"
haftmann@30996
   508
  by (induct n) (auto simp add: abs_mult)
haftmann@30996
   509
haftmann@30996
   510
lemma abs_power_minus [simp]:
haftmann@30996
   511
  "abs ((-a) ^ n) = abs (a ^ n)"
huffman@35216
   512
  by (simp add: power_abs)
haftmann@30996
   513
blanchet@35828
   514
lemma zero_less_power_abs_iff [simp, no_atp]:
haftmann@30996
   515
  "0 < abs a ^ n \<longleftrightarrow> a \<noteq> 0 \<or> n = 0"
haftmann@30996
   516
proof (induct n)
haftmann@30996
   517
  case 0 show ?case by simp
haftmann@30996
   518
next
haftmann@30996
   519
  case (Suc n) show ?case by (auto simp add: Suc zero_less_mult_iff)
huffman@29978
   520
qed
huffman@29978
   521
haftmann@30996
   522
lemma zero_le_power_abs [simp]:
haftmann@30996
   523
  "0 \<le> abs a ^ n"
haftmann@30996
   524
  by (rule zero_le_power [OF abs_ge_zero])
haftmann@30996
   525
huffman@47192
   526
lemma zero_le_power2 [simp]:
huffman@47192
   527
  "0 \<le> a\<twosuperior>"
huffman@47192
   528
  by (simp add: power2_eq_square)
huffman@47192
   529
huffman@47192
   530
lemma zero_less_power2 [simp]:
huffman@47192
   531
  "0 < a\<twosuperior> \<longleftrightarrow> a \<noteq> 0"
huffman@47192
   532
  by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
huffman@47192
   533
huffman@47192
   534
lemma power2_less_0 [simp]:
huffman@47192
   535
  "\<not> a\<twosuperior> < 0"
huffman@47192
   536
  by (force simp add: power2_eq_square mult_less_0_iff)
huffman@47192
   537
huffman@47192
   538
lemma abs_power2 [simp]:
huffman@47192
   539
  "abs (a\<twosuperior>) = a\<twosuperior>"
huffman@47192
   540
  by (simp add: power2_eq_square abs_mult abs_mult_self)
huffman@47192
   541
huffman@47192
   542
lemma power2_abs [simp]:
huffman@47192
   543
  "(abs a)\<twosuperior> = a\<twosuperior>"
huffman@47192
   544
  by (simp add: power2_eq_square abs_mult_self)
huffman@47192
   545
huffman@47192
   546
lemma odd_power_less_zero:
huffman@47192
   547
  "a < 0 \<Longrightarrow> a ^ Suc (2*n) < 0"
huffman@47192
   548
proof (induct n)
huffman@47192
   549
  case 0
huffman@47192
   550
  then show ?case by simp
huffman@47192
   551
next
huffman@47192
   552
  case (Suc n)
huffman@47192
   553
  have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
huffman@47192
   554
    by (simp add: mult_ac power_add power2_eq_square)
huffman@47192
   555
  thus ?case
huffman@47192
   556
    by (simp del: power_Suc add: Suc mult_less_0_iff mult_neg_neg)
huffman@47192
   557
qed
haftmann@30996
   558
huffman@47192
   559
lemma odd_0_le_power_imp_0_le:
huffman@47192
   560
  "0 \<le> a ^ Suc (2*n) \<Longrightarrow> 0 \<le> a"
huffman@47192
   561
  using odd_power_less_zero [of a n]
huffman@47192
   562
    by (force simp add: linorder_not_less [symmetric]) 
huffman@47192
   563
huffman@47192
   564
lemma zero_le_even_power'[simp]:
huffman@47192
   565
  "0 \<le> a ^ (2*n)"
huffman@47192
   566
proof (induct n)
huffman@47192
   567
  case 0
huffman@47192
   568
    show ?case by simp
huffman@47192
   569
next
huffman@47192
   570
  case (Suc n)
huffman@47192
   571
    have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
huffman@47192
   572
      by (simp add: mult_ac power_add power2_eq_square)
huffman@47192
   573
    thus ?case
huffman@47192
   574
      by (simp add: Suc zero_le_mult_iff)
huffman@47192
   575
qed
haftmann@30996
   576
huffman@47192
   577
lemma sum_power2_ge_zero:
huffman@47192
   578
  "0 \<le> x\<twosuperior> + y\<twosuperior>"
huffman@47192
   579
  by (intro add_nonneg_nonneg zero_le_power2)
huffman@47192
   580
huffman@47192
   581
lemma not_sum_power2_lt_zero:
huffman@47192
   582
  "\<not> x\<twosuperior> + y\<twosuperior> < 0"
huffman@47192
   583
  unfolding not_less by (rule sum_power2_ge_zero)
huffman@47192
   584
huffman@47192
   585
lemma sum_power2_eq_zero_iff:
huffman@47192
   586
  "x\<twosuperior> + y\<twosuperior> = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
huffman@47192
   587
  unfolding power2_eq_square by (simp add: add_nonneg_eq_0_iff)
huffman@47192
   588
huffman@47192
   589
lemma sum_power2_le_zero_iff:
huffman@47192
   590
  "x\<twosuperior> + y\<twosuperior> \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
huffman@47192
   591
  by (simp add: le_less sum_power2_eq_zero_iff not_sum_power2_lt_zero)
huffman@47192
   592
huffman@47192
   593
lemma sum_power2_gt_zero_iff:
huffman@47192
   594
  "0 < x\<twosuperior> + y\<twosuperior> \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
huffman@47192
   595
  unfolding not_le [symmetric] by (simp add: sum_power2_le_zero_iff)
haftmann@30996
   596
haftmann@30996
   597
end
haftmann@30996
   598
huffman@29978
   599
huffman@47192
   600
subsection {* Miscellaneous rules *}
paulson@14348
   601
huffman@47192
   602
lemma power2_sum:
huffman@47192
   603
  fixes x y :: "'a::comm_semiring_1"
huffman@47192
   604
  shows "(x + y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> + 2 * x * y"
huffman@47192
   605
  by (simp add: algebra_simps power2_eq_square mult_2_right)
haftmann@30996
   606
huffman@47192
   607
lemma power2_diff:
huffman@47192
   608
  fixes x y :: "'a::comm_ring_1"
huffman@47192
   609
  shows "(x - y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> - 2 * x * y"
huffman@47192
   610
  by (simp add: ring_distribs power2_eq_square mult_2) (rule mult_commute)
haftmann@30996
   611
haftmann@30996
   612
lemma power_0_Suc [simp]:
haftmann@30996
   613
  "(0::'a::{power, semiring_0}) ^ Suc n = 0"
haftmann@30996
   614
  by simp
nipkow@30313
   615
haftmann@30996
   616
text{*It looks plausible as a simprule, but its effect can be strange.*}
haftmann@30996
   617
lemma power_0_left:
haftmann@30996
   618
  "0 ^ n = (if n = 0 then 1 else (0::'a::{power, semiring_0}))"
haftmann@30996
   619
  by (induct n) simp_all
haftmann@30996
   620
haftmann@30996
   621
lemma power_eq_0_iff [simp]:
haftmann@30996
   622
  "a ^ n = 0 \<longleftrightarrow>
haftmann@30996
   623
     a = (0::'a::{mult_zero,zero_neq_one,no_zero_divisors,power}) \<and> n \<noteq> 0"
haftmann@30996
   624
  by (induct n)
haftmann@30996
   625
    (auto simp add: no_zero_divisors elim: contrapos_pp)
haftmann@30996
   626
haftmann@36409
   627
lemma (in field) power_diff:
haftmann@30996
   628
  assumes nz: "a \<noteq> 0"
haftmann@30996
   629
  shows "n \<le> m \<Longrightarrow> a ^ (m - n) = a ^ m / a ^ n"
haftmann@36409
   630
  by (induct m n rule: diff_induct) (simp_all add: nz field_power_not_zero)
nipkow@30313
   631
haftmann@30996
   632
text{*Perhaps these should be simprules.*}
haftmann@30996
   633
lemma power_inverse:
haftmann@36409
   634
  fixes a :: "'a::division_ring_inverse_zero"
haftmann@36409
   635
  shows "inverse (a ^ n) = inverse a ^ n"
haftmann@30996
   636
apply (cases "a = 0")
haftmann@30996
   637
apply (simp add: power_0_left)
haftmann@30996
   638
apply (simp add: nonzero_power_inverse)
haftmann@30996
   639
done (* TODO: reorient or rename to inverse_power *)
haftmann@30996
   640
haftmann@30996
   641
lemma power_one_over:
haftmann@36409
   642
  "1 / (a::'a::{field_inverse_zero, power}) ^ n =  (1 / a) ^ n"
haftmann@30996
   643
  by (simp add: divide_inverse) (rule power_inverse)
haftmann@30996
   644
haftmann@30996
   645
lemma power_divide:
haftmann@36409
   646
  "(a / b) ^ n = (a::'a::field_inverse_zero) ^ n / b ^ n"
haftmann@30996
   647
apply (cases "b = 0")
haftmann@30996
   648
apply (simp add: power_0_left)
haftmann@30996
   649
apply (rule nonzero_power_divide)
haftmann@30996
   650
apply assumption
nipkow@30313
   651
done
nipkow@30313
   652
nipkow@30313
   653
haftmann@30960
   654
subsection {* Exponentiation for the Natural Numbers *}
wenzelm@14577
   655
haftmann@30996
   656
lemma nat_one_le_power [simp]:
haftmann@30996
   657
  "Suc 0 \<le> i \<Longrightarrow> Suc 0 \<le> i ^ n"
haftmann@30996
   658
  by (rule one_le_power [of i n, unfolded One_nat_def])
huffman@23305
   659
haftmann@30996
   660
lemma nat_zero_less_power_iff [simp]:
haftmann@30996
   661
  "x ^ n > 0 \<longleftrightarrow> x > (0::nat) \<or> n = 0"
haftmann@30996
   662
  by (induct n) auto
paulson@14348
   663
nipkow@30056
   664
lemma nat_power_eq_Suc_0_iff [simp]: 
haftmann@30996
   665
  "x ^ m = Suc 0 \<longleftrightarrow> m = 0 \<or> x = Suc 0"
haftmann@30996
   666
  by (induct m) auto
nipkow@30056
   667
haftmann@30996
   668
lemma power_Suc_0 [simp]:
haftmann@30996
   669
  "Suc 0 ^ n = Suc 0"
haftmann@30996
   670
  by simp
nipkow@30056
   671
paulson@14348
   672
text{*Valid for the naturals, but what if @{text"0<i<1"}?
paulson@14348
   673
Premises cannot be weakened: consider the case where @{term "i=0"},
paulson@14348
   674
@{term "m=1"} and @{term "n=0"}.*}
haftmann@21413
   675
lemma nat_power_less_imp_less:
haftmann@21413
   676
  assumes nonneg: "0 < (i\<Colon>nat)"
haftmann@30996
   677
  assumes less: "i ^ m < i ^ n"
haftmann@21413
   678
  shows "m < n"
haftmann@21413
   679
proof (cases "i = 1")
haftmann@21413
   680
  case True with less power_one [where 'a = nat] show ?thesis by simp
haftmann@21413
   681
next
haftmann@21413
   682
  case False with nonneg have "1 < i" by auto
haftmann@21413
   683
  from power_strict_increasing_iff [OF this] less show ?thesis ..
haftmann@21413
   684
qed
paulson@14348
   685
haftmann@33274
   686
lemma power_dvd_imp_le:
haftmann@33274
   687
  "i ^ m dvd i ^ n \<Longrightarrow> (1::nat) < i \<Longrightarrow> m \<le> n"
haftmann@33274
   688
  apply (rule power_le_imp_le_exp, assumption)
haftmann@33274
   689
  apply (erule dvd_imp_le, simp)
haftmann@33274
   690
  done
haftmann@33274
   691
haftmann@31155
   692
haftmann@31155
   693
subsection {* Code generator tweak *}
haftmann@31155
   694
bulwahn@45231
   695
lemma power_power_power [code]:
haftmann@31155
   696
  "power = power.power (1::'a::{power}) (op *)"
haftmann@31155
   697
  unfolding power_def power.power_def ..
haftmann@31155
   698
haftmann@31155
   699
declare power.power.simps [code]
haftmann@31155
   700
haftmann@33364
   701
code_modulename SML
haftmann@33364
   702
  Power Arith
haftmann@33364
   703
haftmann@33364
   704
code_modulename OCaml
haftmann@33364
   705
  Power Arith
haftmann@33364
   706
haftmann@33364
   707
code_modulename Haskell
haftmann@33364
   708
  Power Arith
haftmann@33364
   709
paulson@3390
   710
end