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