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