src/HOL/Power.thy
author huffman
Fri Mar 13 10:14:47 2009 -0700 (2009-03-13)
changeset 30516 68b4a06cbd5c
parent 30313 b2441b0c8d38
child 30730 4d3565f2cb0e
permissions -rw-r--r--
remove legacy ML bindings
     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 class power =
    15   fixes power :: "'a \<Rightarrow> nat \<Rightarrow> 'a"            (infixr "^" 80)
    16 
    17 subsection{*Powers for Arbitrary Monoids*}
    18 
    19 class recpower = monoid_mult + power +
    20   assumes power_0 [simp]: "a ^ 0       = 1"
    21   assumes power_Suc [simp]: "a ^ Suc n = a * (a ^ n)"
    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 zero_less_power_abs_iff [simp,noatp]:
   190      "(0 < (abs a)^n) = (a \<noteq> (0::'a::{ordered_idom,recpower}) | n=0)"
   191 proof (induct "n")
   192   case 0
   193     show ?case by simp
   194 next
   195   case (Suc n)
   196     show ?case by (auto simp add: prems zero_less_mult_iff)
   197 qed
   198 
   199 lemma zero_le_power_abs [simp]:
   200      "(0::'a::{ordered_idom,recpower}) \<le> (abs a)^n"
   201 by (rule zero_le_power [OF abs_ge_zero])
   202 
   203 lemma power_minus: "(-a) ^ n = (- 1)^n * (a::'a::{ring_1,recpower}) ^ n"
   204 proof (induct n)
   205   case 0 show ?case by simp
   206 next
   207   case (Suc n) then show ?case
   208     by (simp del: power_Suc add: power_Suc2 mult_assoc)
   209 qed
   210 
   211 text{*Lemma for @{text power_strict_decreasing}*}
   212 lemma power_Suc_less:
   213      "[|(0::'a::{ordered_semidom,recpower}) < a; a < 1|]
   214       ==> a * a^n < a^n"
   215 apply (induct n)
   216 apply (auto simp add: mult_strict_left_mono)
   217 done
   218 
   219 lemma power_strict_decreasing:
   220      "[|n < N; 0 < a; a < (1::'a::{ordered_semidom,recpower})|]
   221       ==> a^N < a^n"
   222 apply (erule rev_mp)
   223 apply (induct "N")
   224 apply (auto simp add: power_Suc_less less_Suc_eq)
   225 apply (rename_tac m)
   226 apply (subgoal_tac "a * a^m < 1 * a^n", simp)
   227 apply (rule mult_strict_mono)
   228 apply (auto simp add: order_less_imp_le)
   229 done
   230 
   231 text{*Proof resembles that of @{text power_strict_decreasing}*}
   232 lemma power_decreasing:
   233      "[|n \<le> N; 0 \<le> a; a \<le> (1::'a::{ordered_semidom,recpower})|]
   234       ==> a^N \<le> a^n"
   235 apply (erule rev_mp)
   236 apply (induct "N")
   237 apply (auto simp add: le_Suc_eq)
   238 apply (rename_tac m)
   239 apply (subgoal_tac "a * a^m \<le> 1 * a^n", simp)
   240 apply (rule mult_mono)
   241 apply auto
   242 done
   243 
   244 lemma power_Suc_less_one:
   245      "[| 0 < a; a < (1::'a::{ordered_semidom,recpower}) |] ==> a ^ Suc n < 1"
   246 apply (insert power_strict_decreasing [of 0 "Suc n" a], simp)
   247 done
   248 
   249 text{*Proof again resembles that of @{text power_strict_decreasing}*}
   250 lemma power_increasing:
   251      "[|n \<le> N; (1::'a::{ordered_semidom,recpower}) \<le> a|] ==> a^n \<le> a^N"
   252 apply (erule rev_mp)
   253 apply (induct "N")
   254 apply (auto simp add: le_Suc_eq)
   255 apply (rename_tac m)
   256 apply (subgoal_tac "1 * a^n \<le> a * a^m", simp)
   257 apply (rule mult_mono)
   258 apply (auto simp add: order_trans [OF zero_le_one])
   259 done
   260 
   261 text{*Lemma for @{text power_strict_increasing}*}
   262 lemma power_less_power_Suc:
   263      "(1::'a::{ordered_semidom,recpower}) < a ==> a^n < a * a^n"
   264 apply (induct n)
   265 apply (auto simp add: mult_strict_left_mono order_less_trans [OF zero_less_one])
   266 done
   267 
   268 lemma power_strict_increasing:
   269      "[|n < N; (1::'a::{ordered_semidom,recpower}) < a|] ==> a^n < a^N"
   270 apply (erule rev_mp)
   271 apply (induct "N")
   272 apply (auto simp add: power_less_power_Suc less_Suc_eq)
   273 apply (rename_tac m)
   274 apply (subgoal_tac "1 * a^n < a * a^m", simp)
   275 apply (rule mult_strict_mono)
   276 apply (auto simp add: order_less_trans [OF zero_less_one] order_less_imp_le)
   277 done
   278 
   279 lemma power_increasing_iff [simp]:
   280   "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x \<le> b ^ y) = (x \<le> y)"
   281 by (blast intro: power_le_imp_le_exp power_increasing order_less_imp_le) 
   282 
   283 lemma power_strict_increasing_iff [simp]:
   284   "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x < b ^ y) = (x < y)"
   285 by (blast intro: power_less_imp_less_exp power_strict_increasing) 
   286 
   287 lemma power_le_imp_le_base:
   288 assumes le: "a ^ Suc n \<le> b ^ Suc n"
   289     and ynonneg: "(0::'a::{ordered_semidom,recpower}) \<le> b"
   290 shows "a \<le> b"
   291 proof (rule ccontr)
   292   assume "~ a \<le> b"
   293   then have "b < a" by (simp only: linorder_not_le)
   294   then have "b ^ Suc n < a ^ Suc n"
   295     by (simp only: prems power_strict_mono)
   296   from le and this show "False"
   297     by (simp add: linorder_not_less [symmetric])
   298 qed
   299 
   300 lemma power_less_imp_less_base:
   301   fixes a b :: "'a::{ordered_semidom,recpower}"
   302   assumes less: "a ^ n < b ^ n"
   303   assumes nonneg: "0 \<le> b"
   304   shows "a < b"
   305 proof (rule contrapos_pp [OF less])
   306   assume "~ a < b"
   307   hence "b \<le> a" by (simp only: linorder_not_less)
   308   hence "b ^ n \<le> a ^ n" using nonneg by (rule power_mono)
   309   thus "~ a ^ n < b ^ n" by (simp only: linorder_not_less)
   310 qed
   311 
   312 lemma power_inject_base:
   313      "[| a ^ Suc n = b ^ Suc n; 0 \<le> a; 0 \<le> b |]
   314       ==> a = (b::'a::{ordered_semidom,recpower})"
   315 by (blast intro: power_le_imp_le_base order_antisym order_eq_refl sym)
   316 
   317 lemma power_eq_imp_eq_base:
   318   fixes a b :: "'a::{ordered_semidom,recpower}"
   319   shows "\<lbrakk>a ^ n = b ^ n; 0 \<le> a; 0 \<le> b; 0 < n\<rbrakk> \<Longrightarrow> a = b"
   320 by (cases n, simp_all del: power_Suc, rule power_inject_base)
   321 
   322 text {* The divides relation *}
   323 
   324 lemma le_imp_power_dvd:
   325   fixes a :: "'a::{comm_semiring_1,recpower}"
   326   assumes "m \<le> n" shows "a^m dvd a^n"
   327 proof
   328   have "a^n = a^(m + (n - m))"
   329     using `m \<le> n` by simp
   330   also have "\<dots> = a^m * a^(n - m)"
   331     by (rule power_add)
   332   finally show "a^n = a^m * a^(n - m)" .
   333 qed
   334 
   335 lemma power_le_dvd:
   336   fixes a b :: "'a::{comm_semiring_1,recpower}"
   337   shows "a^n dvd b \<Longrightarrow> m \<le> n \<Longrightarrow> a^m dvd b"
   338   by (rule dvd_trans [OF le_imp_power_dvd])
   339 
   340 
   341 lemma dvd_power_same:
   342   "(x::'a::{comm_semiring_1,recpower}) dvd y \<Longrightarrow> x^n dvd y^n"
   343 by (induct n) (auto simp add: mult_dvd_mono)
   344 
   345 lemma dvd_power_le:
   346   "(x::'a::{comm_semiring_1,recpower}) dvd y \<Longrightarrow> m >= n \<Longrightarrow> x^n dvd y^m"
   347 by(rule power_le_dvd[OF dvd_power_same])
   348 
   349 lemma dvd_power [simp]:
   350   "n > 0 | (x::'a::{comm_semiring_1,recpower}) = 1 \<Longrightarrow> x dvd x^n"
   351 apply (erule disjE)
   352  apply (subgoal_tac "x ^ n = x^(Suc (n - 1))")
   353   apply (erule ssubst)
   354   apply (subst power_Suc)
   355   apply auto
   356 done
   357 
   358 
   359 subsection{*Exponentiation for the Natural Numbers*}
   360 
   361 instantiation nat :: recpower
   362 begin
   363 
   364 primrec power_nat where
   365   "p ^ 0 = (1\<Colon>nat)"
   366   | "p ^ (Suc n) = (p\<Colon>nat) * (p ^ n)"
   367 
   368 instance proof
   369   fix z n :: nat
   370   show "z^0 = 1" by simp
   371   show "z^(Suc n) = z * (z^n)" by simp
   372 qed
   373 
   374 declare power_nat.simps [simp del]
   375 
   376 end
   377 
   378 lemma of_nat_power:
   379   "of_nat (m ^ n) = (of_nat m::'a::{semiring_1,recpower}) ^ n"
   380 by (induct n, simp_all add: of_nat_mult)
   381 
   382 lemma nat_one_le_power [simp]: "Suc 0 \<le> i ==> Suc 0 \<le> i^n"
   383 by (rule one_le_power [of i n, unfolded One_nat_def])
   384 
   385 lemma nat_zero_less_power_iff [simp]: "(x^n > 0) = (x > (0::nat) | n=0)"
   386 by (induct "n", auto)
   387 
   388 lemma nat_power_eq_Suc_0_iff [simp]: 
   389   "((x::nat)^m = Suc 0) = (m = 0 | x = Suc 0)"
   390 by (induct_tac m, auto)
   391 
   392 lemma power_Suc_0[simp]: "(Suc 0)^n = Suc 0"
   393 by simp
   394 
   395 text{*Valid for the naturals, but what if @{text"0<i<1"}?
   396 Premises cannot be weakened: consider the case where @{term "i=0"},
   397 @{term "m=1"} and @{term "n=0"}.*}
   398 lemma nat_power_less_imp_less:
   399   assumes nonneg: "0 < (i\<Colon>nat)"
   400   assumes less: "i^m < i^n"
   401   shows "m < n"
   402 proof (cases "i = 1")
   403   case True with less power_one [where 'a = nat] show ?thesis by simp
   404 next
   405   case False with nonneg have "1 < i" by auto
   406   from power_strict_increasing_iff [OF this] less show ?thesis ..
   407 qed
   408 
   409 lemma power_diff:
   410   assumes nz: "a ~= 0"
   411   shows "n <= m ==> (a::'a::{recpower, field}) ^ (m-n) = (a^m) / (a^n)"
   412   by (induct m n rule: diff_induct)
   413     (simp_all add: nonzero_mult_divide_cancel_left nz)
   414 
   415 end