src/HOL/Power.thy
 author haftmann Wed Apr 22 19:09:21 2009 +0200 (2009-04-22) changeset 30960 fec1a04b7220 parent 30730 4d3565f2cb0e child 30996 648d02b124d8 permissions -rw-r--r--
power operation defined generic
```     1 (*  Title:      HOL/Power.thy
```
```     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     3     Copyright   1997  University of Cambridge
```
```     4 *)
```
```     5
```
```     6 header {* Exponentiation *}
```
```     7
```
```     8 theory Power
```
```     9 imports Nat
```
```    10 begin
```
```    11
```
```    12 subsection {* Powers for Arbitrary Monoids *}
```
```    13
```
```    14 class recpower = monoid_mult
```
```    15 begin
```
```    16
```
```    17 primrec power :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixr "^" 80) where
```
```    18     power_0: "a ^ 0 = 1"
```
```    19   | power_Suc: "a ^ Suc n = a * a ^ n"
```
```    20
```
```    21 end
```
```    22
```
```    23 lemma power_0_Suc [simp]: "(0::'a::{recpower,semiring_0}) ^ (Suc n) = 0"
```
```    24   by simp
```
```    25
```
```    26 text{*It looks plausible as a simprule, but its effect can be strange.*}
```
```    27 lemma power_0_left: "0^n = (if n=0 then 1 else (0::'a::{recpower,semiring_0}))"
```
```    28   by (induct n) simp_all
```
```    29
```
```    30 lemma power_one [simp]: "1^n = (1::'a::recpower)"
```
```    31   by (induct n) simp_all
```
```    32
```
```    33 lemma power_one_right [simp]: "(a::'a::recpower) ^ 1 = a"
```
```    34   unfolding One_nat_def by simp
```
```    35
```
```    36 lemma power_commutes: "(a::'a::recpower) ^ n * a = a * a ^ n"
```
```    37   by (induct n) (simp_all add: mult_assoc)
```
```    38
```
```    39 lemma power_Suc2: "(a::'a::recpower) ^ Suc n = a ^ n * a"
```
```    40   by (simp add: power_commutes)
```
```    41
```
```    42 lemma power_add: "(a::'a::recpower) ^ (m+n) = (a^m) * (a^n)"
```
```    43   by (induct m) (simp_all add: mult_ac)
```
```    44
```
```    45 lemma power_mult: "(a::'a::recpower) ^ (m*n) = (a^m) ^ n"
```
```    46   by (induct n) (simp_all add: power_add)
```
```    47
```
```    48 lemma power_mult_distrib: "((a::'a::{recpower,comm_monoid_mult}) * b) ^ n = (a^n) * (b^n)"
```
```    49   by (induct n) (simp_all add: mult_ac)
```
```    50
```
```    51 lemma zero_less_power[simp]:
```
```    52      "0 < (a::'a::{ordered_semidom,recpower}) ==> 0 < a^n"
```
```    53 by (induct n) (simp_all add: mult_pos_pos)
```
```    54
```
```    55 lemma zero_le_power[simp]:
```
```    56      "0 \<le> (a::'a::{ordered_semidom,recpower}) ==> 0 \<le> a^n"
```
```    57 by (induct n) (simp_all add: mult_nonneg_nonneg)
```
```    58
```
```    59 lemma one_le_power[simp]:
```
```    60      "1 \<le> (a::'a::{ordered_semidom,recpower}) ==> 1 \<le> a^n"
```
```    61 apply (induct "n")
```
```    62 apply simp_all
```
```    63 apply (rule order_trans [OF _ mult_mono [of 1 _ 1]])
```
```    64 apply (simp_all add: order_trans [OF zero_le_one])
```
```    65 done
```
```    66
```
```    67 lemma gt1_imp_ge0: "1 < a ==> 0 \<le> (a::'a::ordered_semidom)"
```
```    68   by (simp add: order_trans [OF zero_le_one order_less_imp_le])
```
```    69
```
```    70 lemma power_gt1_lemma:
```
```    71   assumes gt1: "1 < (a::'a::{ordered_semidom,recpower})"
```
```    72   shows "1 < a * a^n"
```
```    73 proof -
```
```    74   have "1*1 < a*1" using gt1 by simp
```
```    75   also have "\<dots> \<le> a * a^n" using gt1
```
```    76     by (simp only: mult_mono gt1_imp_ge0 one_le_power order_less_imp_le
```
```    77         zero_le_one order_refl)
```
```    78   finally show ?thesis by simp
```
```    79 qed
```
```    80
```
```    81 lemma one_less_power[simp]:
```
```    82   "\<lbrakk>1 < (a::'a::{ordered_semidom,recpower}); 0 < n\<rbrakk> \<Longrightarrow> 1 < a ^ n"
```
```    83 by (cases n, simp_all add: power_gt1_lemma)
```
```    84
```
```    85 lemma power_gt1:
```
```    86      "1 < (a::'a::{ordered_semidom,recpower}) ==> 1 < a ^ (Suc n)"
```
```    87 by (simp add: power_gt1_lemma)
```
```    88
```
```    89 lemma power_le_imp_le_exp:
```
```    90   assumes gt1: "(1::'a::{recpower,ordered_semidom}) < a"
```
```    91   shows "!!n. a^m \<le> a^n ==> m \<le> n"
```
```    92 proof (induct m)
```
```    93   case 0
```
```    94   show ?case by simp
```
```    95 next
```
```    96   case (Suc m)
```
```    97   show ?case
```
```    98   proof (cases n)
```
```    99     case 0
```
```   100     from prems have "a * a^m \<le> 1" by simp
```
```   101     with gt1 show ?thesis
```
```   102       by (force simp only: power_gt1_lemma
```
```   103           linorder_not_less [symmetric])
```
```   104   next
```
```   105     case (Suc n)
```
```   106     from prems show ?thesis
```
```   107       by (force dest: mult_left_le_imp_le
```
```   108           simp add: order_less_trans [OF zero_less_one gt1])
```
```   109   qed
```
```   110 qed
```
```   111
```
```   112 text{*Surely we can strengthen this? It holds for @{text "0<a<1"} too.*}
```
```   113 lemma power_inject_exp [simp]:
```
```   114      "1 < (a::'a::{ordered_semidom,recpower}) ==> (a^m = a^n) = (m=n)"
```
```   115   by (force simp add: order_antisym power_le_imp_le_exp)
```
```   116
```
```   117 text{*Can relax the first premise to @{term "0<a"} in the case of the
```
```   118 natural numbers.*}
```
```   119 lemma power_less_imp_less_exp:
```
```   120      "[| (1::'a::{recpower,ordered_semidom}) < a; a^m < a^n |] ==> m < n"
```
```   121 by (simp add: order_less_le [of m n] order_less_le [of "a^m" "a^n"]
```
```   122               power_le_imp_le_exp)
```
```   123
```
```   124
```
```   125 lemma power_mono:
```
```   126      "[|a \<le> b; (0::'a::{recpower,ordered_semidom}) \<le> a|] ==> a^n \<le> b^n"
```
```   127 apply (induct "n")
```
```   128 apply simp_all
```
```   129 apply (auto intro: mult_mono order_trans [of 0 a b])
```
```   130 done
```
```   131
```
```   132 lemma power_strict_mono [rule_format]:
```
```   133      "[|a < b; (0::'a::{recpower,ordered_semidom}) \<le> a|]
```
```   134       ==> 0 < n --> a^n < b^n"
```
```   135 apply (induct "n")
```
```   136 apply (auto simp add: mult_strict_mono order_le_less_trans [of 0 a b])
```
```   137 done
```
```   138
```
```   139 lemma power_eq_0_iff [simp]:
```
```   140   "(a^n = 0) \<longleftrightarrow>
```
```   141    (a = (0::'a::{mult_zero,zero_neq_one,no_zero_divisors,recpower}) & n\<noteq>0)"
```
```   142 apply (induct "n")
```
```   143 apply (auto simp add: no_zero_divisors)
```
```   144 done
```
```   145
```
```   146
```
```   147 lemma field_power_not_zero:
```
```   148   "a \<noteq> (0::'a::{ring_1_no_zero_divisors,recpower}) ==> a^n \<noteq> 0"
```
```   149 by force
```
```   150
```
```   151 lemma nonzero_power_inverse:
```
```   152   fixes a :: "'a::{division_ring,recpower}"
```
```   153   shows "a \<noteq> 0 ==> inverse (a ^ n) = (inverse a) ^ n"
```
```   154 apply (induct "n")
```
```   155 apply (auto simp add: nonzero_inverse_mult_distrib power_commutes)
```
```   156 done (* TODO: reorient or rename to nonzero_inverse_power *)
```
```   157
```
```   158 text{*Perhaps these should be simprules.*}
```
```   159 lemma power_inverse:
```
```   160   fixes a :: "'a::{division_ring,division_by_zero,recpower}"
```
```   161   shows "inverse (a ^ n) = (inverse a) ^ n"
```
```   162 apply (cases "a = 0")
```
```   163 apply (simp add: power_0_left)
```
```   164 apply (simp add: nonzero_power_inverse)
```
```   165 done (* TODO: reorient or rename to inverse_power *)
```
```   166
```
```   167 lemma power_one_over: "1 / (a::'a::{field,division_by_zero,recpower})^n =
```
```   168     (1 / a)^n"
```
```   169 apply (simp add: divide_inverse)
```
```   170 apply (rule power_inverse)
```
```   171 done
```
```   172
```
```   173 lemma nonzero_power_divide:
```
```   174     "b \<noteq> 0 ==> (a/b) ^ n = ((a::'a::{field,recpower}) ^ n) / (b ^ n)"
```
```   175 by (simp add: divide_inverse power_mult_distrib nonzero_power_inverse)
```
```   176
```
```   177 lemma power_divide:
```
```   178     "(a/b) ^ n = ((a::'a::{field,division_by_zero,recpower}) ^ n / b ^ n)"
```
```   179 apply (case_tac "b=0", simp add: power_0_left)
```
```   180 apply (rule nonzero_power_divide)
```
```   181 apply assumption
```
```   182 done
```
```   183
```
```   184 lemma power_abs: "abs(a ^ n) = abs(a::'a::{ordered_idom,recpower}) ^ n"
```
```   185 apply (induct "n")
```
```   186 apply (auto simp add: abs_mult)
```
```   187 done
```
```   188
```
```   189 lemma abs_power_minus [simp]:
```
```   190   fixes a:: "'a::{ordered_idom,recpower}" shows "abs((-a) ^ n) = abs(a ^ n)"
```
```   191   by (simp add: abs_minus_cancel power_abs)
```
```   192
```
```   193 lemma zero_less_power_abs_iff [simp,noatp]:
```
```   194      "(0 < (abs a)^n) = (a \<noteq> (0::'a::{ordered_idom,recpower}) | n=0)"
```
```   195 proof (induct "n")
```
```   196   case 0
```
```   197     show ?case by simp
```
```   198 next
```
```   199   case (Suc n)
```
```   200     show ?case by (auto simp add: prems zero_less_mult_iff)
```
```   201 qed
```
```   202
```
```   203 lemma zero_le_power_abs [simp]:
```
```   204      "(0::'a::{ordered_idom,recpower}) \<le> (abs a)^n"
```
```   205 by (rule zero_le_power [OF abs_ge_zero])
```
```   206
```
```   207 lemma power_minus: "(-a) ^ n = (- 1)^n * (a::'a::{ring_1,recpower}) ^ n"
```
```   208 proof (induct n)
```
```   209   case 0 show ?case by simp
```
```   210 next
```
```   211   case (Suc n) then show ?case
```
```   212     by (simp del: power_Suc add: power_Suc2 mult_assoc)
```
```   213 qed
```
```   214
```
```   215 text{*Lemma for @{text power_strict_decreasing}*}
```
```   216 lemma power_Suc_less:
```
```   217      "[|(0::'a::{ordered_semidom,recpower}) < a; a < 1|]
```
```   218       ==> a * a^n < a^n"
```
```   219 apply (induct n)
```
```   220 apply (auto simp add: mult_strict_left_mono)
```
```   221 done
```
```   222
```
```   223 lemma power_strict_decreasing:
```
```   224      "[|n < N; 0 < a; a < (1::'a::{ordered_semidom,recpower})|]
```
```   225       ==> a^N < a^n"
```
```   226 apply (erule rev_mp)
```
```   227 apply (induct "N")
```
```   228 apply (auto simp add: power_Suc_less less_Suc_eq)
```
```   229 apply (rename_tac m)
```
```   230 apply (subgoal_tac "a * a^m < 1 * a^n", simp)
```
```   231 apply (rule mult_strict_mono)
```
```   232 apply (auto simp add: order_less_imp_le)
```
```   233 done
```
```   234
```
```   235 text{*Proof resembles that of @{text power_strict_decreasing}*}
```
```   236 lemma power_decreasing:
```
```   237      "[|n \<le> N; 0 \<le> a; a \<le> (1::'a::{ordered_semidom,recpower})|]
```
```   238       ==> a^N \<le> a^n"
```
```   239 apply (erule rev_mp)
```
```   240 apply (induct "N")
```
```   241 apply (auto simp add: le_Suc_eq)
```
```   242 apply (rename_tac m)
```
```   243 apply (subgoal_tac "a * a^m \<le> 1 * a^n", simp)
```
```   244 apply (rule mult_mono)
```
```   245 apply auto
```
```   246 done
```
```   247
```
```   248 lemma power_Suc_less_one:
```
```   249      "[| 0 < a; a < (1::'a::{ordered_semidom,recpower}) |] ==> a ^ Suc n < 1"
```
```   250 apply (insert power_strict_decreasing [of 0 "Suc n" a], simp)
```
```   251 done
```
```   252
```
```   253 text{*Proof again resembles that of @{text power_strict_decreasing}*}
```
```   254 lemma power_increasing:
```
```   255      "[|n \<le> N; (1::'a::{ordered_semidom,recpower}) \<le> a|] ==> a^n \<le> a^N"
```
```   256 apply (erule rev_mp)
```
```   257 apply (induct "N")
```
```   258 apply (auto simp add: le_Suc_eq)
```
```   259 apply (rename_tac m)
```
```   260 apply (subgoal_tac "1 * a^n \<le> a * a^m", simp)
```
```   261 apply (rule mult_mono)
```
```   262 apply (auto simp add: order_trans [OF zero_le_one])
```
```   263 done
```
```   264
```
```   265 text{*Lemma for @{text power_strict_increasing}*}
```
```   266 lemma power_less_power_Suc:
```
```   267      "(1::'a::{ordered_semidom,recpower}) < a ==> a^n < a * a^n"
```
```   268 apply (induct n)
```
```   269 apply (auto simp add: mult_strict_left_mono order_less_trans [OF zero_less_one])
```
```   270 done
```
```   271
```
```   272 lemma power_strict_increasing:
```
```   273      "[|n < N; (1::'a::{ordered_semidom,recpower}) < a|] ==> a^n < a^N"
```
```   274 apply (erule rev_mp)
```
```   275 apply (induct "N")
```
```   276 apply (auto simp add: power_less_power_Suc less_Suc_eq)
```
```   277 apply (rename_tac m)
```
```   278 apply (subgoal_tac "1 * a^n < a * a^m", simp)
```
```   279 apply (rule mult_strict_mono)
```
```   280 apply (auto simp add: order_less_trans [OF zero_less_one] order_less_imp_le)
```
```   281 done
```
```   282
```
```   283 lemma power_increasing_iff [simp]:
```
```   284   "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x \<le> b ^ y) = (x \<le> y)"
```
```   285 by (blast intro: power_le_imp_le_exp power_increasing order_less_imp_le)
```
```   286
```
```   287 lemma power_strict_increasing_iff [simp]:
```
```   288   "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x < b ^ y) = (x < y)"
```
```   289 by (blast intro: power_less_imp_less_exp power_strict_increasing)
```
```   290
```
```   291 lemma power_le_imp_le_base:
```
```   292 assumes le: "a ^ Suc n \<le> b ^ Suc n"
```
```   293     and ynonneg: "(0::'a::{ordered_semidom,recpower}) \<le> b"
```
```   294 shows "a \<le> b"
```
```   295 proof (rule ccontr)
```
```   296   assume "~ a \<le> b"
```
```   297   then have "b < a" by (simp only: linorder_not_le)
```
```   298   then have "b ^ Suc n < a ^ Suc n"
```
```   299     by (simp only: prems power_strict_mono)
```
```   300   from le and this show "False"
```
```   301     by (simp add: linorder_not_less [symmetric])
```
```   302 qed
```
```   303
```
```   304 lemma power_less_imp_less_base:
```
```   305   fixes a b :: "'a::{ordered_semidom,recpower}"
```
```   306   assumes less: "a ^ n < b ^ n"
```
```   307   assumes nonneg: "0 \<le> b"
```
```   308   shows "a < b"
```
```   309 proof (rule contrapos_pp [OF less])
```
```   310   assume "~ a < b"
```
```   311   hence "b \<le> a" by (simp only: linorder_not_less)
```
```   312   hence "b ^ n \<le> a ^ n" using nonneg by (rule power_mono)
```
```   313   thus "~ a ^ n < b ^ n" by (simp only: linorder_not_less)
```
```   314 qed
```
```   315
```
```   316 lemma power_inject_base:
```
```   317      "[| a ^ Suc n = b ^ Suc n; 0 \<le> a; 0 \<le> b |]
```
```   318       ==> a = (b::'a::{ordered_semidom,recpower})"
```
```   319 by (blast intro: power_le_imp_le_base order_antisym order_eq_refl sym)
```
```   320
```
```   321 lemma power_eq_imp_eq_base:
```
```   322   fixes a b :: "'a::{ordered_semidom,recpower}"
```
```   323   shows "\<lbrakk>a ^ n = b ^ n; 0 \<le> a; 0 \<le> b; 0 < n\<rbrakk> \<Longrightarrow> a = b"
```
```   324 by (cases n, simp_all del: power_Suc, rule power_inject_base)
```
```   325
```
```   326 text {* The divides relation *}
```
```   327
```
```   328 lemma le_imp_power_dvd:
```
```   329   fixes a :: "'a::{comm_semiring_1,recpower}"
```
```   330   assumes "m \<le> n" shows "a^m dvd a^n"
```
```   331 proof
```
```   332   have "a^n = a^(m + (n - m))"
```
```   333     using `m \<le> n` by simp
```
```   334   also have "\<dots> = a^m * a^(n - m)"
```
```   335     by (rule power_add)
```
```   336   finally show "a^n = a^m * a^(n - m)" .
```
```   337 qed
```
```   338
```
```   339 lemma power_le_dvd:
```
```   340   fixes a b :: "'a::{comm_semiring_1,recpower}"
```
```   341   shows "a^n dvd b \<Longrightarrow> m \<le> n \<Longrightarrow> a^m dvd b"
```
```   342   by (rule dvd_trans [OF le_imp_power_dvd])
```
```   343
```
```   344
```
```   345 lemma dvd_power_same:
```
```   346   "(x::'a::{comm_semiring_1,recpower}) dvd y \<Longrightarrow> x^n dvd y^n"
```
```   347 by (induct n) (auto simp add: mult_dvd_mono)
```
```   348
```
```   349 lemma dvd_power_le:
```
```   350   "(x::'a::{comm_semiring_1,recpower}) dvd y \<Longrightarrow> m >= n \<Longrightarrow> x^n dvd y^m"
```
```   351 by(rule power_le_dvd[OF dvd_power_same])
```
```   352
```
```   353 lemma dvd_power [simp]:
```
```   354   "n > 0 | (x::'a::{comm_semiring_1,recpower}) = 1 \<Longrightarrow> x dvd x^n"
```
```   355 apply (erule disjE)
```
```   356  apply (subgoal_tac "x ^ n = x^(Suc (n - 1))")
```
```   357   apply (erule ssubst)
```
```   358   apply (subst power_Suc)
```
```   359   apply auto
```
```   360 done
```
```   361
```
```   362
```
```   363 subsection {* Exponentiation for the Natural Numbers *}
```
```   364
```
```   365 instance nat :: recpower ..
```
```   366
```
```   367 lemma of_nat_power:
```
```   368   "of_nat (m ^ n) = (of_nat m::'a::{semiring_1,recpower}) ^ n"
```
```   369 by (induct n, simp_all add: of_nat_mult)
```
```   370
```
```   371 lemma nat_one_le_power [simp]: "Suc 0 \<le> i ==> Suc 0 \<le> i^n"
```
```   372 by (rule one_le_power [of i n, unfolded One_nat_def])
```
```   373
```
```   374 lemma nat_zero_less_power_iff [simp]: "(x^n > 0) = (x > (0::nat) | n=0)"
```
```   375 by (induct "n", auto)
```
```   376
```
```   377 lemma nat_power_eq_Suc_0_iff [simp]:
```
```   378   "((x::nat)^m = Suc 0) = (m = 0 | x = Suc 0)"
```
```   379 by (induct m, auto)
```
```   380
```
```   381 lemma power_Suc_0[simp]: "(Suc 0)^n = Suc 0"
```
```   382 by simp
```
```   383
```
```   384 text{*Valid for the naturals, but what if @{text"0<i<1"}?
```
```   385 Premises cannot be weakened: consider the case where @{term "i=0"},
```
```   386 @{term "m=1"} and @{term "n=0"}.*}
```
```   387 lemma nat_power_less_imp_less:
```
```   388   assumes nonneg: "0 < (i\<Colon>nat)"
```
```   389   assumes less: "i^m < i^n"
```
```   390   shows "m < n"
```
```   391 proof (cases "i = 1")
```
```   392   case True with less power_one [where 'a = nat] show ?thesis by simp
```
```   393 next
```
```   394   case False with nonneg have "1 < i" by auto
```
```   395   from power_strict_increasing_iff [OF this] less show ?thesis ..
```
```   396 qed
```
```   397
```
```   398 lemma power_diff:
```
```   399   assumes nz: "a ~= 0"
```
```   400   shows "n <= m ==> (a::'a::{recpower, field}) ^ (m-n) = (a^m) / (a^n)"
```
```   401   by (induct m n rule: diff_induct)
```
```   402     (simp_all add: nonzero_mult_divide_cancel_left nz)
```
```   403
```
```   404 end
```