src/HOL/Power.thy
author ballarin
Fri Aug 26 10:01:06 2005 +0200 (2005-08-26)
changeset 17149 e2b19c92ef51
parent 16796 140f1e0ea846
child 21199 2d83f93c3580
permissions -rw-r--r--
Lemmas on dvd, power and finite summation added or strengthened.
     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 Divides
    12 begin
    13 
    14 subsection{*Powers for Arbitrary Semirings*}
    15 
    16 axclass recpower \<subseteq> comm_semiring_1_cancel, power
    17   power_0 [simp]: "a ^ 0       = 1"
    18   power_Suc:      "a ^ (Suc n) = a * (a ^ n)"
    19 
    20 lemma power_0_Suc [simp]: "(0::'a::recpower) ^ (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))"
    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_add: "(a::'a::recpower) ^ (m+n) = (a^m) * (a^n)"
    36 apply (induct "n")
    37 apply (simp_all add: power_Suc mult_ac)
    38 done
    39 
    40 lemma power_mult: "(a::'a::recpower) ^ (m*n) = (a^m) ^ n"
    41 apply (induct "n")
    42 apply (simp_all add: power_Suc power_add)
    43 done
    44 
    45 lemma power_mult_distrib: "((a::'a::recpower) * b) ^ n = (a^n) * (b^n)"
    46 apply (induct "n")
    47 apply (auto simp add: power_Suc mult_ac)
    48 done
    49 
    50 lemma zero_less_power:
    51      "0 < (a::'a::{ordered_semidom,recpower}) ==> 0 < a^n"
    52 apply (induct "n")
    53 apply (simp_all add: power_Suc zero_less_one mult_pos_pos)
    54 done
    55 
    56 lemma zero_le_power:
    57      "0 \<le> (a::'a::{ordered_semidom,recpower}) ==> 0 \<le> a^n"
    58 apply (simp add: order_le_less)
    59 apply (erule disjE)
    60 apply (simp_all add: zero_less_power zero_less_one power_0_left)
    61 done
    62 
    63 lemma one_le_power:
    64      "1 \<le> (a::'a::{ordered_semidom,recpower}) ==> 1 \<le> a^n"
    65 apply (induct "n")
    66 apply (simp_all add: power_Suc)
    67 apply (rule order_trans [OF _ mult_mono [of 1 _ 1]])
    68 apply (simp_all add: zero_le_one order_trans [OF zero_le_one])
    69 done
    70 
    71 lemma gt1_imp_ge0: "1 < a ==> 0 \<le> (a::'a::ordered_semidom)"
    72   by (simp add: order_trans [OF zero_le_one order_less_imp_le])
    73 
    74 lemma power_gt1_lemma:
    75   assumes gt1: "1 < (a::'a::{ordered_semidom,recpower})"
    76   shows "1 < a * a^n"
    77 proof -
    78   have "1*1 < a*1" using gt1 by simp
    79   also have "\<dots> \<le> a * a^n" using gt1
    80     by (simp only: mult_mono gt1_imp_ge0 one_le_power order_less_imp_le
    81         zero_le_one order_refl)
    82   finally show ?thesis by simp
    83 qed
    84 
    85 lemma power_gt1:
    86      "1 < (a::'a::{ordered_semidom,recpower}) ==> 1 < a ^ (Suc n)"
    87 by (simp add: power_gt1_lemma power_Suc)
    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 add: power_Suc)
   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: power_Suc 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 add: power_Suc)
   129 apply (auto intro: mult_mono zero_le_power 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 zero_le_power power_Suc
   137                       order_le_less_trans [of 0 a b])
   138 done
   139 
   140 lemma power_eq_0_iff [simp]:
   141      "(a^n = 0) = (a = (0::'a::{ordered_idom,recpower}) & 0<n)"
   142 apply (induct "n")
   143 apply (auto simp add: power_Suc zero_neq_one [THEN not_sym])
   144 done
   145 
   146 lemma field_power_eq_0_iff [simp]:
   147      "(a^n = 0) = (a = (0::'a::{field,recpower}) & 0<n)"
   148 apply (induct "n")
   149 apply (auto simp add: power_Suc field_mult_eq_0_iff zero_neq_one[THEN not_sym])
   150 done
   151 
   152 lemma field_power_not_zero: "a \<noteq> (0::'a::{field,recpower}) ==> a^n \<noteq> 0"
   153 by force
   154 
   155 lemma nonzero_power_inverse:
   156   "a \<noteq> 0 ==> inverse ((a::'a::{field,recpower}) ^ n) = (inverse a) ^ n"
   157 apply (induct "n")
   158 apply (auto simp add: power_Suc nonzero_inverse_mult_distrib mult_commute)
   159 done
   160 
   161 text{*Perhaps these should be simprules.*}
   162 lemma power_inverse:
   163   "inverse ((a::'a::{field,division_by_zero,recpower}) ^ n) = (inverse a) ^ n"
   164 apply (induct "n")
   165 apply (auto simp add: power_Suc inverse_mult_distrib)
   166 done
   167 
   168 lemma power_one_over: "1 / (a::'a::{field,division_by_zero,recpower})^n = 
   169     (1 / a)^n"
   170 apply (simp add: divide_inverse)
   171 apply (rule power_inverse)
   172 done
   173 
   174 lemma nonzero_power_divide:
   175     "b \<noteq> 0 ==> (a/b) ^ n = ((a::'a::{field,recpower}) ^ n) / (b ^ n)"
   176 by (simp add: divide_inverse power_mult_distrib nonzero_power_inverse)
   177 
   178 lemma power_divide:
   179     "(a/b) ^ n = ((a::'a::{field,division_by_zero,recpower}) ^ n / b ^ n)"
   180 apply (case_tac "b=0", simp add: power_0_left)
   181 apply (rule nonzero_power_divide)
   182 apply assumption
   183 done
   184 
   185 lemma power_abs: "abs(a ^ n) = abs(a::'a::{ordered_idom,recpower}) ^ n"
   186 apply (induct "n")
   187 apply (auto simp add: power_Suc abs_mult)
   188 done
   189 
   190 lemma zero_less_power_abs_iff [simp]:
   191      "(0 < (abs a)^n) = (a \<noteq> (0::'a::{ordered_idom,recpower}) | n=0)"
   192 proof (induct "n")
   193   case 0
   194     show ?case by (simp add: zero_less_one)
   195 next
   196   case (Suc n)
   197     show ?case by (force simp add: prems power_Suc zero_less_mult_iff)
   198 qed
   199 
   200 lemma zero_le_power_abs [simp]:
   201      "(0::'a::{ordered_idom,recpower}) \<le> (abs a)^n"
   202 apply (induct "n")
   203 apply (auto simp add: zero_le_one zero_le_power)
   204 done
   205 
   206 lemma power_minus: "(-a) ^ n = (- 1)^n * (a::'a::{comm_ring_1,recpower}) ^ n"
   207 proof -
   208   have "-a = (- 1) * a"  by (simp add: minus_mult_left [symmetric])
   209   thus ?thesis by (simp only: power_mult_distrib)
   210 qed
   211 
   212 text{*Lemma for @{text power_strict_decreasing}*}
   213 lemma power_Suc_less:
   214      "[|(0::'a::{ordered_semidom,recpower}) < a; a < 1|]
   215       ==> a * a^n < a^n"
   216 apply (induct n)
   217 apply (auto simp add: power_Suc mult_strict_left_mono)
   218 done
   219 
   220 lemma power_strict_decreasing:
   221      "[|n < N; 0 < a; a < (1::'a::{ordered_semidom,recpower})|]
   222       ==> a^N < a^n"
   223 apply (erule rev_mp)
   224 apply (induct "N")
   225 apply (auto simp add: power_Suc power_Suc_less less_Suc_eq)
   226 apply (rename_tac m)
   227 apply (subgoal_tac "a * a^m < 1 * a^n", simp)
   228 apply (rule mult_strict_mono)
   229 apply (auto simp add: zero_le_power zero_less_one order_less_imp_le)
   230 done
   231 
   232 text{*Proof resembles that of @{text power_strict_decreasing}*}
   233 lemma power_decreasing:
   234      "[|n \<le> N; 0 \<le> a; a \<le> (1::'a::{ordered_semidom,recpower})|]
   235       ==> a^N \<le> a^n"
   236 apply (erule rev_mp)
   237 apply (induct "N")
   238 apply (auto simp add: power_Suc  le_Suc_eq)
   239 apply (rename_tac m)
   240 apply (subgoal_tac "a * a^m \<le> 1 * a^n", simp)
   241 apply (rule mult_mono)
   242 apply (auto simp add: zero_le_power zero_le_one)
   243 done
   244 
   245 lemma power_Suc_less_one:
   246      "[| 0 < a; a < (1::'a::{ordered_semidom,recpower}) |] ==> a ^ Suc n < 1"
   247 apply (insert power_strict_decreasing [of 0 "Suc n" a], simp)
   248 done
   249 
   250 text{*Proof again resembles that of @{text power_strict_decreasing}*}
   251 lemma power_increasing:
   252      "[|n \<le> N; (1::'a::{ordered_semidom,recpower}) \<le> a|] ==> a^n \<le> a^N"
   253 apply (erule rev_mp)
   254 apply (induct "N")
   255 apply (auto simp add: power_Suc le_Suc_eq)
   256 apply (rename_tac m)
   257 apply (subgoal_tac "1 * a^n \<le> a * a^m", simp)
   258 apply (rule mult_mono)
   259 apply (auto simp add: order_trans [OF zero_le_one] zero_le_power)
   260 done
   261 
   262 text{*Lemma for @{text power_strict_increasing}*}
   263 lemma power_less_power_Suc:
   264      "(1::'a::{ordered_semidom,recpower}) < a ==> a^n < a * a^n"
   265 apply (induct n)
   266 apply (auto simp add: power_Suc mult_strict_left_mono order_less_trans [OF zero_less_one])
   267 done
   268 
   269 lemma power_strict_increasing:
   270      "[|n < N; (1::'a::{ordered_semidom,recpower}) < a|] ==> a^n < a^N"
   271 apply (erule rev_mp)
   272 apply (induct "N")
   273 apply (auto simp add: power_less_power_Suc power_Suc less_Suc_eq)
   274 apply (rename_tac m)
   275 apply (subgoal_tac "1 * a^n < a * a^m", simp)
   276 apply (rule mult_strict_mono)
   277 apply (auto simp add: order_less_trans [OF zero_less_one] zero_le_power
   278                  order_less_imp_le)
   279 done
   280 
   281 lemma power_increasing_iff [simp]: 
   282      "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x \<le> b ^ y) = (x \<le> y)"
   283   by (blast intro: power_le_imp_le_exp power_increasing order_less_imp_le) 
   284 
   285 lemma power_strict_increasing_iff [simp]:
   286      "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x < b ^ y) = (x < y)"
   287   by (blast intro: power_less_imp_less_exp power_strict_increasing) 
   288 
   289 lemma power_le_imp_le_base:
   290   assumes le: "a ^ Suc n \<le> b ^ Suc n"
   291       and xnonneg: "(0::'a::{ordered_semidom,recpower}) \<le> a"
   292       and ynonneg: "0 \<le> b"
   293   shows "a \<le> b"
   294  proof (rule ccontr)
   295    assume "~ a \<le> b"
   296    then have "b < a" by (simp only: linorder_not_le)
   297    then have "b ^ Suc n < a ^ Suc n"
   298      by (simp only: prems power_strict_mono)
   299    from le and this show "False"
   300       by (simp add: linorder_not_less [symmetric])
   301  qed
   302 
   303 lemma power_inject_base:
   304      "[| a ^ Suc n = b ^ Suc n; 0 \<le> a; 0 \<le> b |]
   305       ==> a = (b::'a::{ordered_semidom,recpower})"
   306 by (blast intro: power_le_imp_le_base order_antisym order_eq_refl sym)
   307 
   308 
   309 subsection{*Exponentiation for the Natural Numbers*}
   310 
   311 primrec (power)
   312   "p ^ 0 = 1"
   313   "p ^ (Suc n) = (p::nat) * (p ^ n)"
   314 
   315 instance nat :: recpower
   316 proof
   317   fix z n :: nat
   318   show "z^0 = 1" by simp
   319   show "z^(Suc n) = z * (z^n)" by simp
   320 qed
   321 
   322 lemma nat_one_le_power [simp]: "1 \<le> i ==> Suc 0 \<le> i^n"
   323 by (insert one_le_power [of i n], simp)
   324 
   325 lemma le_imp_power_dvd: "!!i::nat. m \<le> n ==> i^m dvd i^n"
   326 apply (unfold dvd_def)
   327 apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
   328 apply (simp add: power_add)
   329 done
   330 
   331 text{*Valid for the naturals, but what if @{text"0<i<1"}?
   332 Premises cannot be weakened: consider the case where @{term "i=0"},
   333 @{term "m=1"} and @{term "n=0"}.*}
   334 lemma nat_power_less_imp_less: "!!i::nat. [| 0 < i; i^m < i^n |] ==> m < n"
   335 apply (rule ccontr)
   336 apply (drule leI [THEN le_imp_power_dvd, THEN dvd_imp_le, THEN leD])
   337 apply (erule zero_less_power, auto)
   338 done
   339 
   340 lemma nat_zero_less_power_iff [simp]: "(0 < x^n) = (x \<noteq> (0::nat) | n=0)"
   341 by (induct "n", auto)
   342 
   343 lemma power_le_dvd [rule_format]: "k^j dvd n --> i\<le>j --> k^i dvd (n::nat)"
   344 apply (induct "j")
   345 apply (simp_all add: le_Suc_eq)
   346 apply (blast dest!: dvd_mult_right)
   347 done
   348 
   349 lemma power_dvd_imp_le: "[|i^m dvd i^n;  (1::nat) < i|] ==> m \<le> n"
   350 apply (rule power_le_imp_le_exp, assumption)
   351 apply (erule dvd_imp_le, simp)
   352 done
   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 le_imp_power_dvd = thm"le_imp_power_dvd";
   409 val nat_power_less_imp_less = thm"nat_power_less_imp_less";
   410 val nat_zero_less_power_iff = thm"nat_zero_less_power_iff";
   411 val power_le_dvd = thm"power_le_dvd";
   412 val power_dvd_imp_le = thm"power_dvd_imp_le";
   413 *}
   414 
   415 end
   416