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