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