src/HOL/Power.thy
author paulson
Tue Oct 19 18:18:45 2004 +0200 (2004-10-19)
changeset 15251 bb6f072c8d10
parent 15140 322485b816ac
child 16733 236dfafbeb63
permissions -rw-r--r--
converted some induct_tac to induct
     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
    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)
    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 nonzero_power_divide:
   169     "b \<noteq> 0 ==> (a/b) ^ n = ((a::'a::{field,recpower}) ^ n) / (b ^ n)"
   170 by (simp add: divide_inverse power_mult_distrib nonzero_power_inverse)
   171 
   172 lemma power_divide:
   173     "(a/b) ^ n = ((a::'a::{field,division_by_zero,recpower}) ^ n / b ^ n)"
   174 apply (case_tac "b=0", simp add: power_0_left)
   175 apply (rule nonzero_power_divide)
   176 apply assumption
   177 done
   178 
   179 lemma power_abs: "abs(a ^ n) = abs(a::'a::{ordered_idom,recpower}) ^ n"
   180 apply (induct "n")
   181 apply (auto simp add: power_Suc abs_mult)
   182 done
   183 
   184 lemma zero_less_power_abs_iff [simp]:
   185      "(0 < (abs a)^n) = (a \<noteq> (0::'a::{ordered_idom,recpower}) | n=0)"
   186 proof (induct "n")
   187   case 0
   188     show ?case by (simp add: zero_less_one)
   189 next
   190   case (Suc n)
   191     show ?case by (force simp add: prems power_Suc zero_less_mult_iff)
   192 qed
   193 
   194 lemma zero_le_power_abs [simp]:
   195      "(0::'a::{ordered_idom,recpower}) \<le> (abs a)^n"
   196 apply (induct "n")
   197 apply (auto simp add: zero_le_one zero_le_power)
   198 done
   199 
   200 lemma power_minus: "(-a) ^ n = (- 1)^n * (a::'a::{comm_ring_1,recpower}) ^ n"
   201 proof -
   202   have "-a = (- 1) * a"  by (simp add: minus_mult_left [symmetric])
   203   thus ?thesis by (simp only: power_mult_distrib)
   204 qed
   205 
   206 text{*Lemma for @{text power_strict_decreasing}*}
   207 lemma power_Suc_less:
   208      "[|(0::'a::{ordered_semidom,recpower}) < a; a < 1|]
   209       ==> a * a^n < a^n"
   210 apply (induct n)
   211 apply (auto simp add: power_Suc mult_strict_left_mono)
   212 done
   213 
   214 lemma power_strict_decreasing:
   215      "[|n < N; 0 < a; a < (1::'a::{ordered_semidom,recpower})|]
   216       ==> a^N < a^n"
   217 apply (erule rev_mp)
   218 apply (induct "N")
   219 apply (auto simp add: power_Suc power_Suc_less less_Suc_eq)
   220 apply (rename_tac m)
   221 apply (subgoal_tac "a * a^m < 1 * a^n", simp)
   222 apply (rule mult_strict_mono)
   223 apply (auto simp add: zero_le_power zero_less_one order_less_imp_le)
   224 done
   225 
   226 text{*Proof resembles that of @{text power_strict_decreasing}*}
   227 lemma power_decreasing:
   228      "[|n \<le> N; 0 \<le> a; a \<le> (1::'a::{ordered_semidom,recpower})|]
   229       ==> a^N \<le> a^n"
   230 apply (erule rev_mp)
   231 apply (induct "N")
   232 apply (auto simp add: power_Suc  le_Suc_eq)
   233 apply (rename_tac m)
   234 apply (subgoal_tac "a * a^m \<le> 1 * a^n", simp)
   235 apply (rule mult_mono)
   236 apply (auto simp add: zero_le_power zero_le_one)
   237 done
   238 
   239 lemma power_Suc_less_one:
   240      "[| 0 < a; a < (1::'a::{ordered_semidom,recpower}) |] ==> a ^ Suc n < 1"
   241 apply (insert power_strict_decreasing [of 0 "Suc n" a], simp)
   242 done
   243 
   244 text{*Proof again resembles that of @{text power_strict_decreasing}*}
   245 lemma power_increasing:
   246      "[|n \<le> N; (1::'a::{ordered_semidom,recpower}) \<le> a|] ==> a^n \<le> a^N"
   247 apply (erule rev_mp)
   248 apply (induct "N")
   249 apply (auto simp add: power_Suc le_Suc_eq)
   250 apply (rename_tac m)
   251 apply (subgoal_tac "1 * a^n \<le> a * a^m", simp)
   252 apply (rule mult_mono)
   253 apply (auto simp add: order_trans [OF zero_le_one] zero_le_power)
   254 done
   255 
   256 text{*Lemma for @{text power_strict_increasing}*}
   257 lemma power_less_power_Suc:
   258      "(1::'a::{ordered_semidom,recpower}) < a ==> a^n < a * a^n"
   259 apply (induct n)
   260 apply (auto simp add: power_Suc mult_strict_left_mono order_less_trans [OF zero_less_one])
   261 done
   262 
   263 lemma power_strict_increasing:
   264      "[|n < N; (1::'a::{ordered_semidom,recpower}) < a|] ==> a^n < a^N"
   265 apply (erule rev_mp)
   266 apply (induct "N")
   267 apply (auto simp add: power_less_power_Suc power_Suc less_Suc_eq)
   268 apply (rename_tac m)
   269 apply (subgoal_tac "1 * a^n < a * a^m", simp)
   270 apply (rule mult_strict_mono)
   271 apply (auto simp add: order_less_trans [OF zero_less_one] zero_le_power
   272                  order_less_imp_le)
   273 done
   274 
   275 lemma power_increasing_iff [simp]: 
   276      "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x \<le> b ^ y) = (x \<le> y)"
   277   by (blast intro: power_le_imp_le_exp power_increasing order_less_imp_le) 
   278 
   279 lemma power_strict_increasing_iff [simp]:
   280      "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x < b ^ y) = (x < y)"
   281   by (blast intro: power_less_imp_less_exp power_strict_increasing) 
   282 
   283 lemma power_le_imp_le_base:
   284   assumes le: "a ^ Suc n \<le> b ^ Suc n"
   285       and xnonneg: "(0::'a::{ordered_semidom,recpower}) \<le> a"
   286       and ynonneg: "0 \<le> b"
   287   shows "a \<le> b"
   288  proof (rule ccontr)
   289    assume "~ a \<le> b"
   290    then have "b < a" by (simp only: linorder_not_le)
   291    then have "b ^ Suc n < a ^ Suc n"
   292      by (simp only: prems power_strict_mono)
   293    from le and this show "False"
   294       by (simp add: linorder_not_less [symmetric])
   295  qed
   296 
   297 lemma power_inject_base:
   298      "[| a ^ Suc n = b ^ Suc n; 0 \<le> a; 0 \<le> b |]
   299       ==> a = (b::'a::{ordered_semidom,recpower})"
   300 by (blast intro: power_le_imp_le_base order_antisym order_eq_refl sym)
   301 
   302 
   303 subsection{*Exponentiation for the Natural Numbers*}
   304 
   305 primrec (power)
   306   "p ^ 0 = 1"
   307   "p ^ (Suc n) = (p::nat) * (p ^ n)"
   308 
   309 instance nat :: recpower
   310 proof
   311   fix z n :: nat
   312   show "z^0 = 1" by simp
   313   show "z^(Suc n) = z * (z^n)" by simp
   314 qed
   315 
   316 lemma nat_one_le_power [simp]: "1 \<le> i ==> Suc 0 \<le> i^n"
   317 by (insert one_le_power [of i n], simp)
   318 
   319 lemma le_imp_power_dvd: "!!i::nat. m \<le> n ==> i^m dvd i^n"
   320 apply (unfold dvd_def)
   321 apply (erule not_less_iff_le [THEN iffD2, THEN add_diff_inverse, THEN subst])
   322 apply (simp add: power_add)
   323 done
   324 
   325 text{*Valid for the naturals, but what if @{text"0<i<1"}?
   326 Premises cannot be weakened: consider the case where @{term "i=0"},
   327 @{term "m=1"} and @{term "n=0"}.*}
   328 lemma nat_power_less_imp_less: "!!i::nat. [| 0 < i; i^m < i^n |] ==> m < n"
   329 apply (rule ccontr)
   330 apply (drule leI [THEN le_imp_power_dvd, THEN dvd_imp_le, THEN leD])
   331 apply (erule zero_less_power, auto)
   332 done
   333 
   334 lemma nat_zero_less_power_iff [simp]: "(0 < x^n) = (x \<noteq> (0::nat) | n=0)"
   335 by (induct "n", auto)
   336 
   337 lemma power_le_dvd [rule_format]: "k^j dvd n --> i\<le>j --> k^i dvd (n::nat)"
   338 apply (induct "j")
   339 apply (simp_all add: le_Suc_eq)
   340 apply (blast dest!: dvd_mult_right)
   341 done
   342 
   343 lemma power_dvd_imp_le: "[|i^m dvd i^n;  (1::nat) < i|] ==> m \<le> n"
   344 apply (rule power_le_imp_le_exp, assumption)
   345 apply (erule dvd_imp_le, simp)
   346 done
   347 
   348 
   349 subsection{*Binomial Coefficients*}
   350 
   351 text{*This development is based on the work of Andy Gordon and
   352 Florian Kammueller*}
   353 
   354 consts
   355   binomial :: "[nat,nat] => nat"      (infixl "choose" 65)
   356 
   357 primrec
   358   binomial_0:   "(0     choose k) = (if k = 0 then 1 else 0)"
   359 
   360   binomial_Suc: "(Suc n choose k) =
   361                  (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"
   362 
   363 lemma binomial_n_0 [simp]: "(n choose 0) = 1"
   364 by (case_tac "n", simp_all)
   365 
   366 lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
   367 by simp
   368 
   369 lemma binomial_Suc_Suc [simp]:
   370      "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
   371 by simp
   372 
   373 lemma binomial_eq_0 [rule_format]: "\<forall>k. n < k --> (n choose k) = 0"
   374 apply (induct "n", auto)
   375 apply (erule allE)
   376 apply (erule mp, arith)
   377 done
   378 
   379 declare binomial_0 [simp del] binomial_Suc [simp del]
   380 
   381 lemma binomial_n_n [simp]: "(n choose n) = 1"
   382 apply (induct "n")
   383 apply (simp_all add: binomial_eq_0)
   384 done
   385 
   386 lemma binomial_Suc_n [simp]: "(Suc n choose n) = Suc n"
   387 by (induct "n", simp_all)
   388 
   389 lemma binomial_1 [simp]: "(n choose Suc 0) = n"
   390 by (induct "n", simp_all)
   391 
   392 lemma zero_less_binomial [rule_format]: "k \<le> n --> 0 < (n choose k)"
   393 by (rule_tac m = n and n = k in diff_induct, simp_all)
   394 
   395 lemma binomial_eq_0_iff: "(n choose k = 0) = (n<k)"
   396 apply (safe intro!: binomial_eq_0)
   397 apply (erule contrapos_pp)
   398 apply (simp add: zero_less_binomial)
   399 done
   400 
   401 lemma zero_less_binomial_iff: "(0 < n choose k) = (k\<le>n)"
   402 by (simp add: linorder_not_less [symmetric] binomial_eq_0_iff [symmetric])
   403 
   404 (*Might be more useful if re-oriented*)
   405 lemma Suc_times_binomial_eq [rule_format]:
   406      "\<forall>k. k \<le> n --> Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
   407 apply (induct "n")
   408 apply (simp add: binomial_0, clarify)
   409 apply (case_tac "k")
   410 apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq
   411                       binomial_eq_0)
   412 done
   413 
   414 text{*This is the well-known version, but it's harder to use because of the
   415   need to reason about division.*}
   416 lemma binomial_Suc_Suc_eq_times:
   417      "k \<le> n ==> (Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
   418 by (simp add: Suc_times_binomial_eq div_mult_self_is_m zero_less_Suc
   419         del: mult_Suc mult_Suc_right)
   420 
   421 text{*Another version, with -1 instead of Suc.*}
   422 lemma times_binomial_minus1_eq:
   423      "[|k \<le> n;  0<k|] ==> (n choose k) * k = n * ((n - 1) choose (k - 1))"
   424 apply (cut_tac n = "n - 1" and k = "k - 1" in Suc_times_binomial_eq)
   425 apply (simp split add: nat_diff_split, auto)
   426 done
   427 
   428 text{*ML bindings for the general exponentiation theorems*}
   429 ML
   430 {*
   431 val power_0 = thm"power_0";
   432 val power_Suc = thm"power_Suc";
   433 val power_0_Suc = thm"power_0_Suc";
   434 val power_0_left = thm"power_0_left";
   435 val power_one = thm"power_one";
   436 val power_one_right = thm"power_one_right";
   437 val power_add = thm"power_add";
   438 val power_mult = thm"power_mult";
   439 val power_mult_distrib = thm"power_mult_distrib";
   440 val zero_less_power = thm"zero_less_power";
   441 val zero_le_power = thm"zero_le_power";
   442 val one_le_power = thm"one_le_power";
   443 val gt1_imp_ge0 = thm"gt1_imp_ge0";
   444 val power_gt1_lemma = thm"power_gt1_lemma";
   445 val power_gt1 = thm"power_gt1";
   446 val power_le_imp_le_exp = thm"power_le_imp_le_exp";
   447 val power_inject_exp = thm"power_inject_exp";
   448 val power_less_imp_less_exp = thm"power_less_imp_less_exp";
   449 val power_mono = thm"power_mono";
   450 val power_strict_mono = thm"power_strict_mono";
   451 val power_eq_0_iff = thm"power_eq_0_iff";
   452 val field_power_eq_0_iff = thm"field_power_eq_0_iff";
   453 val field_power_not_zero = thm"field_power_not_zero";
   454 val power_inverse = thm"power_inverse";
   455 val nonzero_power_divide = thm"nonzero_power_divide";
   456 val power_divide = thm"power_divide";
   457 val power_abs = thm"power_abs";
   458 val zero_less_power_abs_iff = thm"zero_less_power_abs_iff";
   459 val zero_le_power_abs = thm "zero_le_power_abs";
   460 val power_minus = thm"power_minus";
   461 val power_Suc_less = thm"power_Suc_less";
   462 val power_strict_decreasing = thm"power_strict_decreasing";
   463 val power_decreasing = thm"power_decreasing";
   464 val power_Suc_less_one = thm"power_Suc_less_one";
   465 val power_increasing = thm"power_increasing";
   466 val power_strict_increasing = thm"power_strict_increasing";
   467 val power_le_imp_le_base = thm"power_le_imp_le_base";
   468 val power_inject_base = thm"power_inject_base";
   469 *}
   470 
   471 text{*ML bindings for the remaining theorems*}
   472 ML
   473 {*
   474 val nat_one_le_power = thm"nat_one_le_power";
   475 val le_imp_power_dvd = thm"le_imp_power_dvd";
   476 val nat_power_less_imp_less = thm"nat_power_less_imp_less";
   477 val nat_zero_less_power_iff = thm"nat_zero_less_power_iff";
   478 val power_le_dvd = thm"power_le_dvd";
   479 val power_dvd_imp_le = thm"power_dvd_imp_le";
   480 val binomial_n_0 = thm"binomial_n_0";
   481 val binomial_0_Suc = thm"binomial_0_Suc";
   482 val binomial_Suc_Suc = thm"binomial_Suc_Suc";
   483 val binomial_eq_0 = thm"binomial_eq_0";
   484 val binomial_n_n = thm"binomial_n_n";
   485 val binomial_Suc_n = thm"binomial_Suc_n";
   486 val binomial_1 = thm"binomial_1";
   487 val zero_less_binomial = thm"zero_less_binomial";
   488 val binomial_eq_0_iff = thm"binomial_eq_0_iff";
   489 val zero_less_binomial_iff = thm"zero_less_binomial_iff";
   490 val Suc_times_binomial_eq = thm"Suc_times_binomial_eq";
   491 val binomial_Suc_Suc_eq_times = thm"binomial_Suc_Suc_eq_times";
   492 val times_binomial_minus1_eq = thm"times_binomial_minus1_eq";
   493 *}
   494 
   495 end
   496