src/HOL/Power.thy
author paulson
Mon Jan 12 16:51:45 2004 +0100 (2004-01-12)
changeset 14353 79f9fbef9106
parent 14348 744c868ee0b7
child 14438 6b41e98931f8
permissions -rw-r--r--
Added lemmas to Ring_and_Field with slightly modified simplification rules

Deleted some little-used integer theorems, replacing them by the generic ones
in Ring_and_Field

Consolidated integer powers
     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 and Binomial Coefficients*}
     9 
    10 theory Power = Divides:
    11 
    12 subsection{*Powers for Arbitrary (Semi)Rings*}
    13 
    14 axclass ringpower \<subseteq> semiring, power
    15   power_0 [simp]:   "a ^ 0       = 1"
    16   power_Suc: "a ^ (Suc n) = a * (a ^ n)"
    17 
    18 lemma power_0_Suc [simp]: "(0::'a::ringpower) ^ (Suc n) = 0"
    19 by (simp add: power_Suc)
    20 
    21 text{*It looks plausible as a simprule, but its effect can be strange.*}
    22 lemma power_0_left: "0^n = (if n=0 then 1 else (0::'a::ringpower))"
    23 by (induct_tac "n", auto)
    24 
    25 lemma power_one [simp]: "1^n = (1::'a::ringpower)"
    26 apply (induct_tac "n")
    27 apply (auto simp add: power_Suc)  
    28 done
    29 
    30 lemma power_one_right [simp]: "(a::'a::ringpower) ^ 1 = a"
    31 by (simp add: power_Suc)
    32 
    33 lemma power_add: "(a::'a::ringpower) ^ (m+n) = (a^m) * (a^n)"
    34 apply (induct_tac "n")
    35 apply (simp_all add: power_Suc mult_ac)
    36 done
    37 
    38 lemma power_mult: "(a::'a::ringpower) ^ (m*n) = (a^m) ^ n"
    39 apply (induct_tac "n")
    40 apply (simp_all add: power_Suc power_add)
    41 done
    42 
    43 lemma power_mult_distrib: "((a::'a::ringpower) * b) ^ n = (a^n) * (b^n)"
    44 apply (induct_tac "n") 
    45 apply (auto simp add: power_Suc mult_ac)
    46 done
    47 
    48 lemma zero_less_power:
    49      "0 < (a::'a::{ordered_semiring,ringpower}) ==> 0 < a^n"
    50 apply (induct_tac "n")
    51 apply (simp_all add: power_Suc zero_less_one mult_pos)
    52 done
    53 
    54 lemma zero_le_power:
    55      "0 \<le> (a::'a::{ordered_semiring,ringpower}) ==> 0 \<le> a^n"
    56 apply (simp add: order_le_less)
    57 apply (erule disjE) 
    58 apply (simp_all add: zero_less_power zero_less_one power_0_left)
    59 done
    60 
    61 lemma one_le_power:
    62      "1 \<le> (a::'a::{ordered_semiring,ringpower}) ==> 1 \<le> a^n"
    63 apply (induct_tac "n")
    64 apply (simp_all add: power_Suc)
    65 apply (rule order_trans [OF _ mult_mono [of 1 _ 1]]) 
    66 apply (simp_all add: zero_le_one order_trans [OF zero_le_one]) 
    67 done
    68 
    69 lemma gt1_imp_ge0: "1 < a ==> 0 \<le> (a::'a::ordered_semiring)"
    70   by (simp add: order_trans [OF zero_le_one order_less_imp_le])
    71 
    72 lemma power_gt1_lemma:
    73      assumes gt1: "1 < (a::'a::{ordered_semiring,ringpower})"
    74         shows     "1 < a * a^n"
    75 proof -
    76   have "1*1 < a * a^n"
    77     proof (rule order_less_le_trans) 
    78       show "1*1 < a*1" by (simp add: gt1)
    79       show  "a*1 \<le> a * a^n"
    80    by (simp only: mult_mono gt1 gt1_imp_ge0 one_le_power order_less_imp_le 
    81                   zero_le_one order_refl)
    82     qed
    83   thus ?thesis by simp
    84 qed
    85 
    86 lemma power_gt1:
    87      "1 < (a::'a::{ordered_semiring,ringpower}) ==> 1 < a ^ (Suc n)"
    88 by (simp add: power_gt1_lemma power_Suc)
    89 
    90 lemma power_le_imp_le_exp:
    91      assumes gt1: "(1::'a::{ringpower,ordered_semiring}) < a"
    92        shows      "!!n. a^m \<le> a^n ==> m \<le> n"
    93 proof (induct "m")
    94   case 0
    95     show ?case by simp
    96 next
    97   case (Suc m)
    98     show ?case 
    99       proof (cases n)
   100         case 0
   101           from prems have "a * a^m \<le> 1" by (simp add: power_Suc)
   102           with gt1 show ?thesis
   103             by (force simp only: power_gt1_lemma 
   104                                  linorder_not_less [symmetric])
   105       next
   106         case (Suc n)
   107           from prems show ?thesis 
   108 	    by (force dest: mult_left_le_imp_le
   109 	          simp add: power_Suc order_less_trans [OF zero_less_one gt1]) 
   110       qed
   111 qed
   112 
   113 text{*Surely we can strengthen this? It holds for 0<a<1 too.*}
   114 lemma power_inject_exp [simp]:
   115      "1 < (a::'a::{ordered_semiring,ringpower}) ==> (a^m = a^n) = (m=n)"
   116   by (force simp add: order_antisym power_le_imp_le_exp)  
   117 
   118 text{*Can relax the first premise to @{term "0<a"} in the case of the
   119 natural numbers.*}
   120 lemma power_less_imp_less_exp:
   121      "[| (1::'a::{ringpower,ordered_semiring}) < a; a^m < a^n |] ==> m < n"
   122 by (simp add: order_less_le [of m n] order_less_le [of "a^m" "a^n"] 
   123               power_le_imp_le_exp) 
   124 
   125 
   126 lemma power_mono:
   127      "[|a \<le> b; (0::'a::{ringpower,ordered_semiring}) \<le> a|] ==> a^n \<le> b^n"
   128 apply (induct_tac "n") 
   129 apply (simp_all add: power_Suc)
   130 apply (auto intro: mult_mono zero_le_power order_trans [of 0 a b])
   131 done
   132 
   133 lemma power_strict_mono [rule_format]:
   134      "[|a < b; (0::'a::{ringpower,ordered_semiring}) \<le> a|] 
   135       ==> 0 < n --> a^n < b^n" 
   136 apply (induct_tac "n") 
   137 apply (auto simp add: mult_strict_mono zero_le_power power_Suc
   138                       order_le_less_trans [of 0 a b])
   139 done
   140 
   141 lemma power_eq_0_iff [simp]:
   142      "(a^n = 0) = (a = (0::'a::{ordered_ring,ringpower}) & 0<n)"
   143 apply (induct_tac "n")
   144 apply (auto simp add: power_Suc zero_neq_one [THEN not_sym])
   145 done
   146 
   147 lemma field_power_eq_0_iff [simp]:
   148      "(a^n = 0) = (a = (0::'a::{field,ringpower}) & 0<n)"
   149 apply (induct_tac "n")
   150 apply (auto simp add: power_Suc field_mult_eq_0_iff zero_neq_one[THEN not_sym])
   151 done
   152 
   153 lemma field_power_not_zero: "a \<noteq> (0::'a::{field,ringpower}) ==> a^n \<noteq> 0"
   154 by force
   155 
   156 lemma nonzero_power_inverse:
   157   "a \<noteq> 0 ==> inverse ((a::'a::{field,ringpower}) ^ n) = (inverse a) ^ n"
   158 apply (induct_tac "n")
   159 apply (auto simp add: power_Suc nonzero_inverse_mult_distrib mult_commute)
   160 done
   161 
   162 text{*Perhaps these should be simprules.*}
   163 lemma power_inverse:
   164   "inverse ((a::'a::{field,division_by_zero,ringpower}) ^ n) = (inverse a) ^ n"
   165 apply (induct_tac "n")
   166 apply (auto simp add: power_Suc inverse_mult_distrib)
   167 done
   168 
   169 lemma nonzero_power_divide: 
   170     "b \<noteq> 0 ==> (a/b) ^ n = ((a::'a::{field,ringpower}) ^ n) / (b ^ n)"
   171 by (simp add: divide_inverse power_mult_distrib nonzero_power_inverse)
   172 
   173 lemma power_divide: 
   174     "(a/b) ^ n = ((a::'a::{field,division_by_zero,ringpower}) ^ n/ b ^ n)"
   175 apply (case_tac "b=0", simp add: power_0_left)
   176 apply (rule nonzero_power_divide) 
   177 apply assumption 
   178 done
   179 
   180 lemma power_abs: "abs(a ^ n) = abs(a::'a::{ordered_field,ringpower}) ^ n"
   181 apply (induct_tac "n")
   182 apply (auto simp add: power_Suc abs_mult)
   183 done
   184 
   185 lemma zero_less_power_abs_iff [simp]:
   186      "(0 < (abs a)^n) = (a \<noteq> (0::'a::{ordered_ring,ringpower}) | n=0)" 
   187 proof (induct "n")
   188   case 0
   189     show ?case by (simp add: zero_less_one)
   190 next
   191   case (Suc n)
   192     show ?case by (force simp add: prems power_Suc zero_less_mult_iff)
   193 qed
   194 
   195 lemma zero_le_power_abs [simp]:
   196      "(0::'a::{ordered_ring,ringpower}) \<le> (abs a)^n"
   197 apply (induct_tac "n")
   198 apply (auto simp add: zero_le_one zero_le_power)
   199 done
   200 
   201 lemma power_minus: "(-a) ^ n = (- 1)^n * (a::'a::{ring,ringpower}) ^ n"
   202 proof -
   203   have "-a = (- 1) * a"  by (simp add: minus_mult_left [symmetric])
   204   thus ?thesis by (simp only: power_mult_distrib)
   205 qed
   206 
   207 text{*Lemma for @{text power_strict_decreasing}*}
   208 lemma power_Suc_less:
   209      "[|(0::'a::{ordered_semiring,ringpower}) < a; a < 1|]
   210       ==> a * a^n < a^n"
   211 apply (induct_tac n) 
   212 apply (auto simp add: power_Suc mult_strict_left_mono) 
   213 done
   214 
   215 lemma power_strict_decreasing:
   216      "[|n < N; 0 < a; a < (1::'a::{ordered_semiring,ringpower})|]
   217       ==> a^N < a^n"
   218 apply (erule rev_mp) 
   219 apply (induct_tac "N")  
   220 apply (auto simp add: power_Suc power_Suc_less less_Suc_eq) 
   221 apply (rename_tac m)  
   222 apply (subgoal_tac "a * a^m < 1 * a^n", simp)
   223 apply (rule mult_strict_mono) 
   224 apply (auto simp add: zero_le_power zero_less_one order_less_imp_le)
   225 done
   226 
   227 text{*Proof resembles that of @{text power_strict_decreasing}*}
   228 lemma power_decreasing:
   229      "[|n \<le> N; 0 \<le> a; a \<le> (1::'a::{ordered_semiring,ringpower})|]
   230       ==> a^N \<le> a^n"
   231 apply (erule rev_mp) 
   232 apply (induct_tac "N") 
   233 apply (auto simp add: power_Suc  le_Suc_eq) 
   234 apply (rename_tac m)  
   235 apply (subgoal_tac "a * a^m \<le> 1 * a^n", simp)
   236 apply (rule mult_mono) 
   237 apply (auto simp add: zero_le_power zero_le_one)
   238 done
   239 
   240 lemma power_Suc_less_one:
   241      "[| 0 < a; a < (1::'a::{ordered_semiring,ringpower}) |] ==> a ^ Suc n < 1"
   242 apply (insert power_strict_decreasing [of 0 "Suc n" a], simp) 
   243 done
   244 
   245 text{*Proof again resembles that of @{text power_strict_decreasing}*}
   246 lemma power_increasing:
   247      "[|n \<le> N; (1::'a::{ordered_semiring,ringpower}) \<le> a|] ==> a^n \<le> a^N"
   248 apply (erule rev_mp) 
   249 apply (induct_tac "N") 
   250 apply (auto simp add: power_Suc le_Suc_eq) 
   251 apply (rename_tac m)
   252 apply (subgoal_tac "1 * a^n \<le> a * a^m", simp)
   253 apply (rule mult_mono) 
   254 apply (auto simp add: order_trans [OF zero_le_one] zero_le_power)
   255 done
   256 
   257 text{*Lemma for @{text power_strict_increasing}*}
   258 lemma power_less_power_Suc:
   259      "(1::'a::{ordered_semiring,ringpower}) < a ==> a^n < a * a^n"
   260 apply (induct_tac n) 
   261 apply (auto simp add: power_Suc mult_strict_left_mono order_less_trans [OF zero_less_one]) 
   262 done
   263 
   264 lemma power_strict_increasing:
   265      "[|n < N; (1::'a::{ordered_semiring,ringpower}) < a|] ==> a^n < a^N"
   266 apply (erule rev_mp) 
   267 apply (induct_tac "N")  
   268 apply (auto simp add: power_less_power_Suc power_Suc less_Suc_eq) 
   269 apply (rename_tac m)
   270 apply (subgoal_tac "1 * a^n < a * a^m", simp)
   271 apply (rule mult_strict_mono)  
   272 apply (auto simp add: order_less_trans [OF zero_less_one] zero_le_power
   273                  order_less_imp_le)
   274 done
   275 
   276 lemma power_le_imp_le_base:
   277   assumes le: "a ^ Suc n \<le> b ^ Suc n"
   278       and xnonneg: "(0::'a::{ordered_semiring,ringpower}) \<le> a"
   279       and ynonneg: "0 \<le> b"
   280   shows "a \<le> b"
   281  proof (rule ccontr)
   282    assume "~ a \<le> b"
   283    then have "b < a" by (simp only: linorder_not_le)
   284    then have "b ^ Suc n < a ^ Suc n"
   285      by (simp only: prems power_strict_mono) 
   286    from le and this show "False"
   287       by (simp add: linorder_not_less [symmetric])
   288  qed
   289   
   290 lemma power_inject_base:
   291      "[| a ^ Suc n = b ^ Suc n; 0 \<le> a; 0 \<le> b |] 
   292       ==> a = (b::'a::{ordered_semiring,ringpower})"
   293 by (blast intro: power_le_imp_le_base order_antisym order_eq_refl sym)
   294 
   295 
   296 subsection{*Exponentiation for the Natural Numbers*}
   297 
   298 primrec (power)
   299   "p ^ 0 = 1"
   300   "p ^ (Suc n) = (p::nat) * (p ^ n)"
   301   
   302 instance nat :: ringpower
   303 proof
   304   fix z :: nat
   305   fix n :: nat
   306   show "z^0 = 1" by simp
   307   show "z^(Suc n) = z * (z^n)" by simp
   308 qed
   309 
   310 lemma nat_one_le_power [simp]: "1 \<le> i ==> Suc 0 \<le> i^n"
   311 by (insert one_le_power [of i n], simp)
   312 
   313 lemma le_imp_power_dvd: "!!i::nat. m \<le> n ==> i^m dvd i^n"
   314 apply (unfold dvd_def)
   315 apply (erule not_less_iff_le [THEN iffD2, THEN add_diff_inverse, THEN subst])
   316 apply (simp add: power_add)
   317 done
   318 
   319 text{*Valid for the naturals, but what if @{text"0<i<1"}?
   320 Premises cannot be weakened: consider the case where @{term "i=0"},
   321 @{term "m=1"} and @{term "n=0"}.*}
   322 lemma nat_power_less_imp_less: "!!i::nat. [| 0 < i; i^m < i^n |] ==> m < n"
   323 apply (rule ccontr)
   324 apply (drule leI [THEN le_imp_power_dvd, THEN dvd_imp_le, THEN leD])
   325 apply (erule zero_less_power, auto) 
   326 done
   327 
   328 lemma nat_zero_less_power_iff [simp]: "(0 < x^n) = (x \<noteq> (0::nat) | n=0)"
   329 by (induct_tac "n", auto)
   330 
   331 lemma power_le_dvd [rule_format]: "k^j dvd n --> i\<le>j --> k^i dvd (n::nat)"
   332 apply (induct_tac "j")
   333 apply (simp_all add: le_Suc_eq)
   334 apply (blast dest!: dvd_mult_right)
   335 done
   336 
   337 lemma power_dvd_imp_le: "[|i^m dvd i^n;  (1::nat) < i|] ==> m \<le> n"
   338 apply (rule power_le_imp_le_exp, assumption)
   339 apply (erule dvd_imp_le, simp)
   340 done
   341 
   342 
   343 subsection{*Binomial Coefficients*}
   344 
   345 text{*This development is based on the work of Andy Gordon and 
   346 Florian Kammueller*}
   347 
   348 consts
   349   binomial :: "[nat,nat] => nat"      (infixl "choose" 65)
   350 
   351 primrec
   352   binomial_0:   "(0     choose k) = (if k = 0 then 1 else 0)"
   353 
   354   binomial_Suc: "(Suc n choose k) =
   355                  (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"
   356 
   357 lemma binomial_n_0 [simp]: "(n choose 0) = 1"
   358 by (case_tac "n", simp_all)
   359 
   360 lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
   361 by simp
   362 
   363 lemma binomial_Suc_Suc [simp]:
   364      "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
   365 by simp
   366 
   367 lemma binomial_eq_0 [rule_format]: "\<forall>k. n < k --> (n choose k) = 0"
   368 apply (induct_tac "n", auto)
   369 apply (erule allE)
   370 apply (erule mp, arith)
   371 done
   372 
   373 declare binomial_0 [simp del] binomial_Suc [simp del]
   374 
   375 lemma binomial_n_n [simp]: "(n choose n) = 1"
   376 apply (induct_tac "n")
   377 apply (simp_all add: binomial_eq_0)
   378 done
   379 
   380 lemma binomial_Suc_n [simp]: "(Suc n choose n) = Suc n"
   381 by (induct_tac "n", simp_all)
   382 
   383 lemma binomial_1 [simp]: "(n choose Suc 0) = n"
   384 by (induct_tac "n", simp_all)
   385 
   386 lemma zero_less_binomial [rule_format]: "k \<le> n --> 0 < (n choose k)"
   387 by (rule_tac m = n and n = k in diff_induct, simp_all)
   388 
   389 lemma binomial_eq_0_iff: "(n choose k = 0) = (n<k)"
   390 apply (safe intro!: binomial_eq_0)
   391 apply (erule contrapos_pp)
   392 apply (simp add: zero_less_binomial)
   393 done
   394 
   395 lemma zero_less_binomial_iff: "(0 < n choose k) = (k\<le>n)"
   396 by (simp add: linorder_not_less [symmetric] binomial_eq_0_iff [symmetric])
   397 
   398 (*Might be more useful if re-oriented*)
   399 lemma Suc_times_binomial_eq [rule_format]:
   400      "\<forall>k. k \<le> n --> Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
   401 apply (induct_tac "n")
   402 apply (simp add: binomial_0, clarify)
   403 apply (case_tac "k")
   404 apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq 
   405                       binomial_eq_0)
   406 done
   407 
   408 text{*This is the well-known version, but it's harder to use because of the
   409   need to reason about division.*}
   410 lemma binomial_Suc_Suc_eq_times:
   411      "k \<le> n ==> (Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
   412 by (simp add: Suc_times_binomial_eq div_mult_self_is_m zero_less_Suc 
   413         del: mult_Suc mult_Suc_right)
   414 
   415 text{*Another version, with -1 instead of Suc.*}
   416 lemma times_binomial_minus1_eq:
   417      "[|k \<le> n;  0<k|] ==> (n choose k) * k = n * ((n - 1) choose (k - 1))"
   418 apply (cut_tac n = "n - 1" and k = "k - 1" in Suc_times_binomial_eq)
   419 apply (simp split add: nat_diff_split, auto)
   420 done
   421 
   422 text{*ML bindings for the general exponentiation theorems*}
   423 ML
   424 {*
   425 val power_0 = thm"power_0";
   426 val power_Suc = thm"power_Suc";
   427 val power_0_Suc = thm"power_0_Suc";
   428 val power_0_left = thm"power_0_left";
   429 val power_one = thm"power_one";
   430 val power_one_right = thm"power_one_right";
   431 val power_add = thm"power_add";
   432 val power_mult = thm"power_mult";
   433 val power_mult_distrib = thm"power_mult_distrib";
   434 val zero_less_power = thm"zero_less_power";
   435 val zero_le_power = thm"zero_le_power";
   436 val one_le_power = thm"one_le_power";
   437 val gt1_imp_ge0 = thm"gt1_imp_ge0";
   438 val power_gt1_lemma = thm"power_gt1_lemma";
   439 val power_gt1 = thm"power_gt1";
   440 val power_le_imp_le_exp = thm"power_le_imp_le_exp";
   441 val power_inject_exp = thm"power_inject_exp";
   442 val power_less_imp_less_exp = thm"power_less_imp_less_exp";
   443 val power_mono = thm"power_mono";
   444 val power_strict_mono = thm"power_strict_mono";
   445 val power_eq_0_iff = thm"power_eq_0_iff";
   446 val field_power_eq_0_iff = thm"field_power_eq_0_iff";
   447 val field_power_not_zero = thm"field_power_not_zero";
   448 val power_inverse = thm"power_inverse";
   449 val nonzero_power_divide = thm"nonzero_power_divide";
   450 val power_divide = thm"power_divide";
   451 val power_abs = thm"power_abs";
   452 val zero_less_power_abs_iff = thm"zero_less_power_abs_iff";
   453 val zero_le_power_abs = thm "zero_le_power_abs";
   454 val power_minus = thm"power_minus";
   455 val power_Suc_less = thm"power_Suc_less";
   456 val power_strict_decreasing = thm"power_strict_decreasing";
   457 val power_decreasing = thm"power_decreasing";
   458 val power_Suc_less_one = thm"power_Suc_less_one";
   459 val power_increasing = thm"power_increasing";
   460 val power_strict_increasing = thm"power_strict_increasing";
   461 val power_le_imp_le_base = thm"power_le_imp_le_base";
   462 val power_inject_base = thm"power_inject_base";
   463 *}
   464  
   465 text{*ML bindings for the remaining theorems*}
   466 ML
   467 {*
   468 val nat_one_le_power = thm"nat_one_le_power";
   469 val le_imp_power_dvd = thm"le_imp_power_dvd";
   470 val nat_power_less_imp_less = thm"nat_power_less_imp_less";
   471 val nat_zero_less_power_iff = thm"nat_zero_less_power_iff";
   472 val power_le_dvd = thm"power_le_dvd";
   473 val power_dvd_imp_le = thm"power_dvd_imp_le";
   474 val binomial_n_0 = thm"binomial_n_0";
   475 val binomial_0_Suc = thm"binomial_0_Suc";
   476 val binomial_Suc_Suc = thm"binomial_Suc_Suc";
   477 val binomial_eq_0 = thm"binomial_eq_0";
   478 val binomial_n_n = thm"binomial_n_n";
   479 val binomial_Suc_n = thm"binomial_Suc_n";
   480 val binomial_1 = thm"binomial_1";
   481 val zero_less_binomial = thm"zero_less_binomial";
   482 val binomial_eq_0_iff = thm"binomial_eq_0_iff";
   483 val zero_less_binomial_iff = thm"zero_less_binomial_iff";
   484 val Suc_times_binomial_eq = thm"Suc_times_binomial_eq";
   485 val binomial_Suc_Suc_eq_times = thm"binomial_Suc_Suc_eq_times";
   486 val times_binomial_minus1_eq = thm"times_binomial_minus1_eq";
   487 *}
   488 
   489 end
   490