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