src/HOL/Power.thy
author wenzelm
Fri Dec 17 17:43:54 2010 +0100 (2010-12-17)
changeset 41229 d797baa3d57c
parent 39438 c5ece2a7a86e
child 41550 efa734d9b221
permissions -rw-r--r--
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
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
haftmann@21413
     9
imports Nat
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
haftmann@30960
    27
end
paulson@14348
    28
haftmann@30996
    29
context monoid_mult
haftmann@30996
    30
begin
paulson@14348
    31
wenzelm@39438
    32
subclass power .
paulson@14348
    33
haftmann@30996
    34
lemma power_one [simp]:
haftmann@30996
    35
  "1 ^ n = 1"
huffman@30273
    36
  by (induct n) simp_all
paulson@14348
    37
haftmann@30996
    38
lemma power_one_right [simp]:
haftmann@31001
    39
  "a ^ 1 = a"
haftmann@30996
    40
  by simp
paulson@14348
    41
haftmann@30996
    42
lemma power_commutes:
haftmann@30996
    43
  "a ^ n * a = a * a ^ n"
huffman@30273
    44
  by (induct n) (simp_all add: mult_assoc)
krauss@21199
    45
haftmann@30996
    46
lemma power_Suc2:
haftmann@30996
    47
  "a ^ Suc n = a ^ n * a"
huffman@30273
    48
  by (simp add: power_commutes)
huffman@28131
    49
haftmann@30996
    50
lemma power_add:
haftmann@30996
    51
  "a ^ (m + n) = a ^ m * a ^ n"
haftmann@30996
    52
  by (induct m) (simp_all add: algebra_simps)
paulson@14348
    53
haftmann@30996
    54
lemma power_mult:
haftmann@30996
    55
  "a ^ (m * n) = (a ^ m) ^ n"
huffman@30273
    56
  by (induct n) (simp_all add: power_add)
paulson@14348
    57
haftmann@30996
    58
end
haftmann@30996
    59
haftmann@30996
    60
context comm_monoid_mult
haftmann@30996
    61
begin
haftmann@30996
    62
haftmann@30996
    63
lemma power_mult_distrib:
haftmann@30996
    64
  "(a * b) ^ n = (a ^ n) * (b ^ n)"
huffman@30273
    65
  by (induct n) (simp_all add: mult_ac)
paulson@14348
    66
haftmann@30996
    67
end
haftmann@30996
    68
haftmann@30996
    69
context semiring_1
haftmann@30996
    70
begin
haftmann@30996
    71
haftmann@30996
    72
lemma of_nat_power:
haftmann@30996
    73
  "of_nat (m ^ n) = of_nat m ^ n"
haftmann@30996
    74
  by (induct n) (simp_all add: of_nat_mult)
haftmann@30996
    75
haftmann@30996
    76
end
haftmann@30996
    77
haftmann@30996
    78
context comm_semiring_1
haftmann@30996
    79
begin
haftmann@30996
    80
haftmann@30996
    81
text {* The divides relation *}
haftmann@30996
    82
haftmann@30996
    83
lemma le_imp_power_dvd:
haftmann@30996
    84
  assumes "m \<le> n" shows "a ^ m dvd a ^ n"
haftmann@30996
    85
proof
haftmann@30996
    86
  have "a ^ n = a ^ (m + (n - m))"
haftmann@30996
    87
    using `m \<le> n` by simp
haftmann@30996
    88
  also have "\<dots> = a ^ m * a ^ (n - m)"
haftmann@30996
    89
    by (rule power_add)
haftmann@30996
    90
  finally show "a ^ n = a ^ m * a ^ (n - m)" .
haftmann@30996
    91
qed
haftmann@30996
    92
haftmann@30996
    93
lemma power_le_dvd:
haftmann@30996
    94
  "a ^ n dvd b \<Longrightarrow> m \<le> n \<Longrightarrow> a ^ m dvd b"
haftmann@30996
    95
  by (rule dvd_trans [OF le_imp_power_dvd])
haftmann@30996
    96
haftmann@30996
    97
lemma dvd_power_same:
haftmann@30996
    98
  "x dvd y \<Longrightarrow> x ^ n dvd y ^ n"
haftmann@30996
    99
  by (induct n) (auto simp add: mult_dvd_mono)
haftmann@30996
   100
haftmann@30996
   101
lemma dvd_power_le:
haftmann@30996
   102
  "x dvd y \<Longrightarrow> m \<ge> n \<Longrightarrow> x ^ n dvd y ^ m"
haftmann@30996
   103
  by (rule power_le_dvd [OF dvd_power_same])
paulson@14348
   104
haftmann@30996
   105
lemma dvd_power [simp]:
haftmann@30996
   106
  assumes "n > (0::nat) \<or> x = 1"
haftmann@30996
   107
  shows "x dvd (x ^ n)"
haftmann@30996
   108
using assms proof
haftmann@30996
   109
  assume "0 < n"
haftmann@30996
   110
  then have "x ^ n = x ^ Suc (n - 1)" by simp
haftmann@30996
   111
  then show "x dvd (x ^ n)" by simp
haftmann@30996
   112
next
haftmann@30996
   113
  assume "x = 1"
haftmann@30996
   114
  then show "x dvd (x ^ n)" by simp
haftmann@30996
   115
qed
haftmann@30996
   116
haftmann@30996
   117
end
haftmann@30996
   118
haftmann@30996
   119
context ring_1
haftmann@30996
   120
begin
haftmann@30996
   121
haftmann@30996
   122
lemma power_minus:
haftmann@30996
   123
  "(- a) ^ n = (- 1) ^ n * a ^ n"
haftmann@30996
   124
proof (induct n)
haftmann@30996
   125
  case 0 show ?case by simp
haftmann@30996
   126
next
haftmann@30996
   127
  case (Suc n) then show ?case
haftmann@30996
   128
    by (simp del: power_Suc add: power_Suc2 mult_assoc)
haftmann@30996
   129
qed
haftmann@30996
   130
haftmann@30996
   131
end
haftmann@30996
   132
haftmann@35028
   133
context linordered_semidom
haftmann@30996
   134
begin
haftmann@30996
   135
haftmann@30996
   136
lemma zero_less_power [simp]:
haftmann@30996
   137
  "0 < a \<Longrightarrow> 0 < a ^ n"
haftmann@30996
   138
  by (induct n) (simp_all add: mult_pos_pos)
haftmann@30996
   139
haftmann@30996
   140
lemma zero_le_power [simp]:
haftmann@30996
   141
  "0 \<le> a \<Longrightarrow> 0 \<le> a ^ n"
haftmann@30996
   142
  by (induct n) (simp_all add: mult_nonneg_nonneg)
paulson@14348
   143
nipkow@25874
   144
lemma one_le_power[simp]:
haftmann@30996
   145
  "1 \<le> a \<Longrightarrow> 1 \<le> a ^ n"
haftmann@30996
   146
  apply (induct n)
haftmann@30996
   147
  apply simp_all
haftmann@30996
   148
  apply (rule order_trans [OF _ mult_mono [of 1 _ 1]])
haftmann@30996
   149
  apply (simp_all add: order_trans [OF zero_le_one])
haftmann@30996
   150
  done
paulson@14348
   151
paulson@14348
   152
lemma power_gt1_lemma:
haftmann@30996
   153
  assumes gt1: "1 < a"
haftmann@30996
   154
  shows "1 < a * a ^ n"
paulson@14348
   155
proof -
haftmann@30996
   156
  from gt1 have "0 \<le> a"
haftmann@30996
   157
    by (fact order_trans [OF zero_le_one less_imp_le])
haftmann@30996
   158
  have "1 * 1 < a * 1" using gt1 by simp
haftmann@30996
   159
  also have "\<dots> \<le> a * a ^ n" using gt1
haftmann@30996
   160
    by (simp only: mult_mono `0 \<le> a` one_le_power order_less_imp_le
wenzelm@14577
   161
        zero_le_one order_refl)
wenzelm@14577
   162
  finally show ?thesis by simp
paulson@14348
   163
qed
paulson@14348
   164
haftmann@30996
   165
lemma power_gt1:
haftmann@30996
   166
  "1 < a \<Longrightarrow> 1 < a ^ Suc n"
haftmann@30996
   167
  by (simp add: power_gt1_lemma)
huffman@24376
   168
haftmann@30996
   169
lemma one_less_power [simp]:
haftmann@30996
   170
  "1 < a \<Longrightarrow> 0 < n \<Longrightarrow> 1 < a ^ n"
haftmann@30996
   171
  by (cases n) (simp_all add: power_gt1_lemma)
paulson@14348
   172
paulson@14348
   173
lemma power_le_imp_le_exp:
haftmann@30996
   174
  assumes gt1: "1 < a"
haftmann@30996
   175
  shows "a ^ m \<le> a ^ n \<Longrightarrow> m \<le> n"
haftmann@30996
   176
proof (induct m arbitrary: n)
paulson@14348
   177
  case 0
wenzelm@14577
   178
  show ?case by simp
paulson@14348
   179
next
paulson@14348
   180
  case (Suc m)
wenzelm@14577
   181
  show ?case
wenzelm@14577
   182
  proof (cases n)
wenzelm@14577
   183
    case 0
haftmann@30996
   184
    with Suc.prems Suc.hyps have "a * a ^ m \<le> 1" by simp
wenzelm@14577
   185
    with gt1 show ?thesis
wenzelm@14577
   186
      by (force simp only: power_gt1_lemma
haftmann@30996
   187
          not_less [symmetric])
wenzelm@14577
   188
  next
wenzelm@14577
   189
    case (Suc n)
haftmann@30996
   190
    with Suc.prems Suc.hyps show ?thesis
wenzelm@14577
   191
      by (force dest: mult_left_le_imp_le
haftmann@30996
   192
          simp add: less_trans [OF zero_less_one gt1])
wenzelm@14577
   193
  qed
paulson@14348
   194
qed
paulson@14348
   195
wenzelm@14577
   196
text{*Surely we can strengthen this? It holds for @{text "0<a<1"} too.*}
paulson@14348
   197
lemma power_inject_exp [simp]:
haftmann@30996
   198
  "1 < a \<Longrightarrow> a ^ m = a ^ n \<longleftrightarrow> m = n"
wenzelm@14577
   199
  by (force simp add: order_antisym power_le_imp_le_exp)
paulson@14348
   200
paulson@14348
   201
text{*Can relax the first premise to @{term "0<a"} in the case of the
paulson@14348
   202
natural numbers.*}
paulson@14348
   203
lemma power_less_imp_less_exp:
haftmann@30996
   204
  "1 < a \<Longrightarrow> a ^ m < a ^ n \<Longrightarrow> m < n"
haftmann@30996
   205
  by (simp add: order_less_le [of m n] less_le [of "a^m" "a^n"]
haftmann@30996
   206
    power_le_imp_le_exp)
paulson@14348
   207
paulson@14348
   208
lemma power_mono:
haftmann@30996
   209
  "a \<le> b \<Longrightarrow> 0 \<le> a \<Longrightarrow> a ^ n \<le> b ^ n"
haftmann@30996
   210
  by (induct n)
haftmann@30996
   211
    (auto intro: mult_mono order_trans [of 0 a b])
paulson@14348
   212
paulson@14348
   213
lemma power_strict_mono [rule_format]:
haftmann@30996
   214
  "a < b \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 < n \<longrightarrow> a ^ n < b ^ n"
haftmann@30996
   215
  by (induct n)
haftmann@30996
   216
   (auto simp add: mult_strict_mono le_less_trans [of 0 a b])
paulson@14348
   217
paulson@14348
   218
text{*Lemma for @{text power_strict_decreasing}*}
paulson@14348
   219
lemma power_Suc_less:
haftmann@30996
   220
  "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a * a ^ n < a ^ n"
haftmann@30996
   221
  by (induct n)
haftmann@30996
   222
    (auto simp add: mult_strict_left_mono)
paulson@14348
   223
haftmann@30996
   224
lemma power_strict_decreasing [rule_format]:
haftmann@30996
   225
  "n < N \<Longrightarrow> 0 < a \<Longrightarrow> a < 1 \<longrightarrow> a ^ N < a ^ n"
haftmann@30996
   226
proof (induct N)
haftmann@30996
   227
  case 0 then show ?case by simp
haftmann@30996
   228
next
haftmann@30996
   229
  case (Suc N) then show ?case 
haftmann@30996
   230
  apply (auto simp add: power_Suc_less less_Suc_eq)
haftmann@30996
   231
  apply (subgoal_tac "a * a^N < 1 * a^n")
haftmann@30996
   232
  apply simp
haftmann@30996
   233
  apply (rule mult_strict_mono) apply auto
haftmann@30996
   234
  done
haftmann@30996
   235
qed
paulson@14348
   236
paulson@14348
   237
text{*Proof resembles that of @{text power_strict_decreasing}*}
haftmann@30996
   238
lemma power_decreasing [rule_format]:
haftmann@30996
   239
  "n \<le> N \<Longrightarrow> 0 \<le> a \<Longrightarrow> a \<le> 1 \<longrightarrow> a ^ N \<le> a ^ n"
haftmann@30996
   240
proof (induct N)
haftmann@30996
   241
  case 0 then show ?case by simp
haftmann@30996
   242
next
haftmann@30996
   243
  case (Suc N) then show ?case 
haftmann@30996
   244
  apply (auto simp add: le_Suc_eq)
haftmann@30996
   245
  apply (subgoal_tac "a * a^N \<le> 1 * a^n", simp)
haftmann@30996
   246
  apply (rule mult_mono) apply auto
haftmann@30996
   247
  done
haftmann@30996
   248
qed
paulson@14348
   249
paulson@14348
   250
lemma power_Suc_less_one:
haftmann@30996
   251
  "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a ^ Suc n < 1"
haftmann@30996
   252
  using power_strict_decreasing [of 0 "Suc n" a] by simp
paulson@14348
   253
paulson@14348
   254
text{*Proof again resembles that of @{text power_strict_decreasing}*}
haftmann@30996
   255
lemma power_increasing [rule_format]:
haftmann@30996
   256
  "n \<le> N \<Longrightarrow> 1 \<le> a \<Longrightarrow> a ^ n \<le> a ^ N"
haftmann@30996
   257
proof (induct N)
haftmann@30996
   258
  case 0 then show ?case by simp
haftmann@30996
   259
next
haftmann@30996
   260
  case (Suc N) then show ?case 
haftmann@30996
   261
  apply (auto simp add: le_Suc_eq)
haftmann@30996
   262
  apply (subgoal_tac "1 * a^n \<le> a * a^N", simp)
haftmann@30996
   263
  apply (rule mult_mono) apply (auto simp add: order_trans [OF zero_le_one])
haftmann@30996
   264
  done
haftmann@30996
   265
qed
paulson@14348
   266
paulson@14348
   267
text{*Lemma for @{text power_strict_increasing}*}
paulson@14348
   268
lemma power_less_power_Suc:
haftmann@30996
   269
  "1 < a \<Longrightarrow> a ^ n < a * a ^ n"
haftmann@30996
   270
  by (induct n) (auto simp add: mult_strict_left_mono less_trans [OF zero_less_one])
paulson@14348
   271
haftmann@30996
   272
lemma power_strict_increasing [rule_format]:
haftmann@30996
   273
  "n < N \<Longrightarrow> 1 < a \<longrightarrow> a ^ n < a ^ N"
haftmann@30996
   274
proof (induct N)
haftmann@30996
   275
  case 0 then show ?case by simp
haftmann@30996
   276
next
haftmann@30996
   277
  case (Suc N) then show ?case 
haftmann@30996
   278
  apply (auto simp add: power_less_power_Suc less_Suc_eq)
haftmann@30996
   279
  apply (subgoal_tac "1 * a^n < a * a^N", simp)
haftmann@30996
   280
  apply (rule mult_strict_mono) apply (auto simp add: less_trans [OF zero_less_one] less_imp_le)
haftmann@30996
   281
  done
haftmann@30996
   282
qed
paulson@14348
   283
nipkow@25134
   284
lemma power_increasing_iff [simp]:
haftmann@30996
   285
  "1 < b \<Longrightarrow> b ^ x \<le> b ^ y \<longleftrightarrow> x \<le> y"
haftmann@30996
   286
  by (blast intro: power_le_imp_le_exp power_increasing less_imp_le)
paulson@15066
   287
paulson@15066
   288
lemma power_strict_increasing_iff [simp]:
haftmann@30996
   289
  "1 < b \<Longrightarrow> b ^ x < b ^ y \<longleftrightarrow> x < y"
nipkow@25134
   290
by (blast intro: power_less_imp_less_exp power_strict_increasing) 
paulson@15066
   291
paulson@14348
   292
lemma power_le_imp_le_base:
haftmann@30996
   293
  assumes le: "a ^ Suc n \<le> b ^ Suc n"
haftmann@30996
   294
    and ynonneg: "0 \<le> b"
haftmann@30996
   295
  shows "a \<le> b"
nipkow@25134
   296
proof (rule ccontr)
nipkow@25134
   297
  assume "~ a \<le> b"
nipkow@25134
   298
  then have "b < a" by (simp only: linorder_not_le)
nipkow@25134
   299
  then have "b ^ Suc n < a ^ Suc n"
nipkow@25134
   300
    by (simp only: prems power_strict_mono)
haftmann@30996
   301
  from le and this show False
nipkow@25134
   302
    by (simp add: linorder_not_less [symmetric])
nipkow@25134
   303
qed
wenzelm@14577
   304
huffman@22853
   305
lemma power_less_imp_less_base:
huffman@22853
   306
  assumes less: "a ^ n < b ^ n"
huffman@22853
   307
  assumes nonneg: "0 \<le> b"
huffman@22853
   308
  shows "a < b"
huffman@22853
   309
proof (rule contrapos_pp [OF less])
huffman@22853
   310
  assume "~ a < b"
huffman@22853
   311
  hence "b \<le> a" by (simp only: linorder_not_less)
huffman@22853
   312
  hence "b ^ n \<le> a ^ n" using nonneg by (rule power_mono)
haftmann@30996
   313
  thus "\<not> a ^ n < b ^ n" by (simp only: linorder_not_less)
huffman@22853
   314
qed
huffman@22853
   315
paulson@14348
   316
lemma power_inject_base:
haftmann@30996
   317
  "a ^ Suc n = b ^ Suc n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a = b"
haftmann@30996
   318
by (blast intro: power_le_imp_le_base antisym eq_refl sym)
paulson@14348
   319
huffman@22955
   320
lemma power_eq_imp_eq_base:
haftmann@30996
   321
  "a ^ n = b ^ n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 < n \<Longrightarrow> a = b"
haftmann@30996
   322
  by (cases n) (simp_all del: power_Suc, rule power_inject_base)
huffman@22955
   323
haftmann@30996
   324
end
haftmann@30996
   325
haftmann@35028
   326
context linordered_idom
haftmann@30996
   327
begin
huffman@29978
   328
haftmann@30996
   329
lemma power_abs:
haftmann@30996
   330
  "abs (a ^ n) = abs a ^ n"
haftmann@30996
   331
  by (induct n) (auto simp add: abs_mult)
haftmann@30996
   332
haftmann@30996
   333
lemma abs_power_minus [simp]:
haftmann@30996
   334
  "abs ((-a) ^ n) = abs (a ^ n)"
huffman@35216
   335
  by (simp add: power_abs)
haftmann@30996
   336
blanchet@35828
   337
lemma zero_less_power_abs_iff [simp, no_atp]:
haftmann@30996
   338
  "0 < abs a ^ n \<longleftrightarrow> a \<noteq> 0 \<or> n = 0"
haftmann@30996
   339
proof (induct n)
haftmann@30996
   340
  case 0 show ?case by simp
haftmann@30996
   341
next
haftmann@30996
   342
  case (Suc n) show ?case by (auto simp add: Suc zero_less_mult_iff)
huffman@29978
   343
qed
huffman@29978
   344
haftmann@30996
   345
lemma zero_le_power_abs [simp]:
haftmann@30996
   346
  "0 \<le> abs a ^ n"
haftmann@30996
   347
  by (rule zero_le_power [OF abs_ge_zero])
haftmann@30996
   348
haftmann@30996
   349
end
haftmann@30996
   350
haftmann@30996
   351
context ring_1_no_zero_divisors
haftmann@30996
   352
begin
haftmann@30996
   353
haftmann@30996
   354
lemma field_power_not_zero:
haftmann@30996
   355
  "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
haftmann@30996
   356
  by (induct n) auto
haftmann@30996
   357
haftmann@30996
   358
end
haftmann@30996
   359
haftmann@30996
   360
context division_ring
haftmann@30996
   361
begin
huffman@29978
   362
haftmann@30997
   363
text {* FIXME reorient or rename to @{text nonzero_inverse_power} *}
haftmann@30996
   364
lemma nonzero_power_inverse:
haftmann@30996
   365
  "a \<noteq> 0 \<Longrightarrow> inverse (a ^ n) = (inverse a) ^ n"
haftmann@30996
   366
  by (induct n)
haftmann@30996
   367
    (simp_all add: nonzero_inverse_mult_distrib power_commutes field_power_not_zero)
paulson@14348
   368
haftmann@30996
   369
end
haftmann@30996
   370
haftmann@30996
   371
context field
haftmann@30996
   372
begin
haftmann@30996
   373
haftmann@30996
   374
lemma nonzero_power_divide:
haftmann@30996
   375
  "b \<noteq> 0 \<Longrightarrow> (a / b) ^ n = a ^ n / b ^ n"
haftmann@30996
   376
  by (simp add: divide_inverse power_mult_distrib nonzero_power_inverse)
haftmann@30996
   377
haftmann@30996
   378
end
haftmann@30996
   379
haftmann@30996
   380
lemma power_0_Suc [simp]:
haftmann@30996
   381
  "(0::'a::{power, semiring_0}) ^ Suc n = 0"
haftmann@30996
   382
  by simp
nipkow@30313
   383
haftmann@30996
   384
text{*It looks plausible as a simprule, but its effect can be strange.*}
haftmann@30996
   385
lemma power_0_left:
haftmann@30996
   386
  "0 ^ n = (if n = 0 then 1 else (0::'a::{power, semiring_0}))"
haftmann@30996
   387
  by (induct n) simp_all
haftmann@30996
   388
haftmann@30996
   389
lemma power_eq_0_iff [simp]:
haftmann@30996
   390
  "a ^ n = 0 \<longleftrightarrow>
haftmann@30996
   391
     a = (0::'a::{mult_zero,zero_neq_one,no_zero_divisors,power}) \<and> n \<noteq> 0"
haftmann@30996
   392
  by (induct n)
haftmann@30996
   393
    (auto simp add: no_zero_divisors elim: contrapos_pp)
haftmann@30996
   394
haftmann@36409
   395
lemma (in field) power_diff:
haftmann@30996
   396
  assumes nz: "a \<noteq> 0"
haftmann@30996
   397
  shows "n \<le> m \<Longrightarrow> a ^ (m - n) = a ^ m / a ^ n"
haftmann@36409
   398
  by (induct m n rule: diff_induct) (simp_all add: nz field_power_not_zero)
nipkow@30313
   399
haftmann@30996
   400
text{*Perhaps these should be simprules.*}
haftmann@30996
   401
lemma power_inverse:
haftmann@36409
   402
  fixes a :: "'a::division_ring_inverse_zero"
haftmann@36409
   403
  shows "inverse (a ^ n) = inverse a ^ n"
haftmann@30996
   404
apply (cases "a = 0")
haftmann@30996
   405
apply (simp add: power_0_left)
haftmann@30996
   406
apply (simp add: nonzero_power_inverse)
haftmann@30996
   407
done (* TODO: reorient or rename to inverse_power *)
haftmann@30996
   408
haftmann@30996
   409
lemma power_one_over:
haftmann@36409
   410
  "1 / (a::'a::{field_inverse_zero, power}) ^ n =  (1 / a) ^ n"
haftmann@30996
   411
  by (simp add: divide_inverse) (rule power_inverse)
haftmann@30996
   412
haftmann@30996
   413
lemma power_divide:
haftmann@36409
   414
  "(a / b) ^ n = (a::'a::field_inverse_zero) ^ n / b ^ n"
haftmann@30996
   415
apply (cases "b = 0")
haftmann@30996
   416
apply (simp add: power_0_left)
haftmann@30996
   417
apply (rule nonzero_power_divide)
haftmann@30996
   418
apply assumption
nipkow@30313
   419
done
nipkow@30313
   420
nipkow@30313
   421
haftmann@30960
   422
subsection {* Exponentiation for the Natural Numbers *}
wenzelm@14577
   423
haftmann@30996
   424
lemma nat_one_le_power [simp]:
haftmann@30996
   425
  "Suc 0 \<le> i \<Longrightarrow> Suc 0 \<le> i ^ n"
haftmann@30996
   426
  by (rule one_le_power [of i n, unfolded One_nat_def])
huffman@23305
   427
haftmann@30996
   428
lemma nat_zero_less_power_iff [simp]:
haftmann@30996
   429
  "x ^ n > 0 \<longleftrightarrow> x > (0::nat) \<or> n = 0"
haftmann@30996
   430
  by (induct n) auto
paulson@14348
   431
nipkow@30056
   432
lemma nat_power_eq_Suc_0_iff [simp]: 
haftmann@30996
   433
  "x ^ m = Suc 0 \<longleftrightarrow> m = 0 \<or> x = Suc 0"
haftmann@30996
   434
  by (induct m) auto
nipkow@30056
   435
haftmann@30996
   436
lemma power_Suc_0 [simp]:
haftmann@30996
   437
  "Suc 0 ^ n = Suc 0"
haftmann@30996
   438
  by simp
nipkow@30056
   439
paulson@14348
   440
text{*Valid for the naturals, but what if @{text"0<i<1"}?
paulson@14348
   441
Premises cannot be weakened: consider the case where @{term "i=0"},
paulson@14348
   442
@{term "m=1"} and @{term "n=0"}.*}
haftmann@21413
   443
lemma nat_power_less_imp_less:
haftmann@21413
   444
  assumes nonneg: "0 < (i\<Colon>nat)"
haftmann@30996
   445
  assumes less: "i ^ m < i ^ n"
haftmann@21413
   446
  shows "m < n"
haftmann@21413
   447
proof (cases "i = 1")
haftmann@21413
   448
  case True with less power_one [where 'a = nat] show ?thesis by simp
haftmann@21413
   449
next
haftmann@21413
   450
  case False with nonneg have "1 < i" by auto
haftmann@21413
   451
  from power_strict_increasing_iff [OF this] less show ?thesis ..
haftmann@21413
   452
qed
paulson@14348
   453
haftmann@33274
   454
lemma power_dvd_imp_le:
haftmann@33274
   455
  "i ^ m dvd i ^ n \<Longrightarrow> (1::nat) < i \<Longrightarrow> m \<le> n"
haftmann@33274
   456
  apply (rule power_le_imp_le_exp, assumption)
haftmann@33274
   457
  apply (erule dvd_imp_le, simp)
haftmann@33274
   458
  done
haftmann@33274
   459
haftmann@31155
   460
haftmann@31155
   461
subsection {* Code generator tweak *}
haftmann@31155
   462
haftmann@31998
   463
lemma power_power_power [code, code_unfold, code_inline del]:
haftmann@31155
   464
  "power = power.power (1::'a::{power}) (op *)"
haftmann@31155
   465
  unfolding power_def power.power_def ..
haftmann@31155
   466
haftmann@31155
   467
declare power.power.simps [code]
haftmann@31155
   468
haftmann@33364
   469
code_modulename SML
haftmann@33364
   470
  Power Arith
haftmann@33364
   471
haftmann@33364
   472
code_modulename OCaml
haftmann@33364
   473
  Power Arith
haftmann@33364
   474
haftmann@33364
   475
code_modulename Haskell
haftmann@33364
   476
  Power Arith
haftmann@33364
   477
paulson@3390
   478
end