| 21256 |      1 | (*  Title:      HOL/Binomial.thy
 | 
|  |      2 |     ID:         $Id$
 | 
|  |      3 |     Author:     Lawrence C Paulson
 | 
|  |      4 |     Copyright   1997  University of Cambridge
 | 
|  |      5 | *)
 | 
|  |      6 | 
 | 
| 21263 |      7 | header {* Binomial Coefficients *}
 | 
| 21256 |      8 | 
 | 
|  |      9 | theory Binomial
 | 
|  |     10 | imports Main
 | 
|  |     11 | begin
 | 
|  |     12 | 
 | 
| 21263 |     13 | text {* This development is based on the work of Andy Gordon and
 | 
|  |     14 |   Florian Kammueller. *}
 | 
| 21256 |     15 | 
 | 
|  |     16 | consts
 | 
|  |     17 |   binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat"      (infixl "choose" 65)
 | 
|  |     18 | primrec
 | 
| 21263 |     19 |   binomial_0: "(0 choose k) = (if k = 0 then 1 else 0)"
 | 
| 21256 |     20 |   binomial_Suc: "(Suc n choose k) =
 | 
|  |     21 |                  (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"
 | 
|  |     22 | 
 | 
|  |     23 | lemma binomial_n_0 [simp]: "(n choose 0) = 1"
 | 
| 21263 |     24 |   by (cases n) simp_all
 | 
| 21256 |     25 | 
 | 
|  |     26 | lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
 | 
| 21263 |     27 |   by simp
 | 
| 21256 |     28 | 
 | 
|  |     29 | lemma binomial_Suc_Suc [simp]:
 | 
| 21263 |     30 |     "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
 | 
|  |     31 |   by simp
 | 
| 21256 |     32 | 
 | 
| 21263 |     33 | lemma binomial_eq_0: "!!k. n < k ==> (n choose k) = 0"
 | 
|  |     34 |   by (induct n) auto
 | 
| 21256 |     35 | 
 | 
|  |     36 | declare binomial_0 [simp del] binomial_Suc [simp del]
 | 
|  |     37 | 
 | 
|  |     38 | lemma binomial_n_n [simp]: "(n choose n) = 1"
 | 
| 21263 |     39 |   by (induct n) (simp_all add: binomial_eq_0)
 | 
| 21256 |     40 | 
 | 
|  |     41 | lemma binomial_Suc_n [simp]: "(Suc n choose n) = Suc n"
 | 
| 21263 |     42 |   by (induct n) simp_all
 | 
| 21256 |     43 | 
 | 
|  |     44 | lemma binomial_1 [simp]: "(n choose Suc 0) = n"
 | 
| 21263 |     45 |   by (induct n) simp_all
 | 
| 21256 |     46 | 
 | 
| 21263 |     47 | lemma zero_less_binomial: "k \<le> n ==> 0 < (n choose k)"
 | 
|  |     48 |   by (induct n k rule: diff_induct) simp_all
 | 
| 21256 |     49 | 
 | 
|  |     50 | lemma binomial_eq_0_iff: "(n choose k = 0) = (n<k)"
 | 
| 21263 |     51 |   apply (safe intro!: binomial_eq_0)
 | 
|  |     52 |   apply (erule contrapos_pp)
 | 
|  |     53 |   apply (simp add: zero_less_binomial)
 | 
|  |     54 |   done
 | 
| 21256 |     55 | 
 | 
|  |     56 | lemma zero_less_binomial_iff: "(0 < n choose k) = (k\<le>n)"
 | 
| 21263 |     57 |   by (simp add: linorder_not_less [symmetric] binomial_eq_0_iff [symmetric])
 | 
| 21256 |     58 | 
 | 
|  |     59 | (*Might be more useful if re-oriented*)
 | 
| 21263 |     60 | lemma Suc_times_binomial_eq:
 | 
|  |     61 |     "!!k. k \<le> n ==> Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
 | 
|  |     62 |   apply (induct n)
 | 
|  |     63 |   apply (simp add: binomial_0)
 | 
|  |     64 |   apply (case_tac k)
 | 
|  |     65 |   apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq
 | 
|  |     66 |     binomial_eq_0)
 | 
|  |     67 |   done
 | 
| 21256 |     68 | 
 | 
|  |     69 | text{*This is the well-known version, but it's harder to use because of the
 | 
|  |     70 |   need to reason about division.*}
 | 
|  |     71 | lemma binomial_Suc_Suc_eq_times:
 | 
| 21263 |     72 |     "k \<le> n ==> (Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
 | 
|  |     73 |   by (simp add: Suc_times_binomial_eq div_mult_self_is_m zero_less_Suc
 | 
|  |     74 |     del: mult_Suc mult_Suc_right)
 | 
| 21256 |     75 | 
 | 
|  |     76 | text{*Another version, with -1 instead of Suc.*}
 | 
|  |     77 | lemma times_binomial_minus1_eq:
 | 
| 21263 |     78 |     "[|k \<le> n;  0<k|] ==> (n choose k) * k = n * ((n - 1) choose (k - 1))"
 | 
|  |     79 |   apply (cut_tac n = "n - 1" and k = "k - 1" in Suc_times_binomial_eq)
 | 
|  |     80 |   apply (simp split add: nat_diff_split, auto)
 | 
|  |     81 |   done
 | 
|  |     82 | 
 | 
| 21256 |     83 | 
 | 
|  |     84 | subsubsection {* Theorems about @{text "choose"} *}
 | 
|  |     85 | 
 | 
|  |     86 | text {*
 | 
|  |     87 |   \medskip Basic theorem about @{text "choose"}.  By Florian
 | 
|  |     88 |   Kamm\"uller, tidied by LCP.
 | 
|  |     89 | *}
 | 
|  |     90 | 
 | 
|  |     91 | lemma card_s_0_eq_empty:
 | 
|  |     92 |     "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
 | 
|  |     93 |   apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
 | 
|  |     94 |   apply (simp cong add: rev_conj_cong)
 | 
|  |     95 |   done
 | 
|  |     96 | 
 | 
|  |     97 | lemma choose_deconstruct: "finite M ==> x \<notin> M
 | 
|  |     98 |   ==> {s. s <= insert x M & card(s) = Suc k}
 | 
|  |     99 |        = {s. s <= M & card(s) = Suc k} Un
 | 
|  |    100 |          {s. EX t. t <= M & card(t) = k & s = insert x t}"
 | 
|  |    101 |   apply safe
 | 
|  |    102 |    apply (auto intro: finite_subset [THEN card_insert_disjoint])
 | 
|  |    103 |   apply (drule_tac x = "xa - {x}" in spec)
 | 
|  |    104 |   apply (subgoal_tac "x \<notin> xa", auto)
 | 
|  |    105 |   apply (erule rev_mp, subst card_Diff_singleton)
 | 
|  |    106 |   apply (auto intro: finite_subset)
 | 
|  |    107 |   done
 | 
|  |    108 | 
 | 
|  |    109 | text{*There are as many subsets of @{term A} having cardinality @{term k}
 | 
|  |    110 |  as there are sets obtained from the former by inserting a fixed element
 | 
|  |    111 |  @{term x} into each.*}
 | 
|  |    112 | lemma constr_bij:
 | 
|  |    113 |    "[|finite A; x \<notin> A|] ==>
 | 
|  |    114 |     card {B. EX C. C <= A & card(C) = k & B = insert x C} =
 | 
|  |    115 |     card {B. B <= A & card(B) = k}"
 | 
|  |    116 |   apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
 | 
|  |    117 |        apply (auto elim!: equalityE simp add: inj_on_def)
 | 
|  |    118 |     apply (subst Diff_insert0, auto)
 | 
|  |    119 |    txt {* finiteness of the two sets *}
 | 
|  |    120 |    apply (rule_tac [2] B = "Pow (A)" in finite_subset)
 | 
|  |    121 |    apply (rule_tac B = "Pow (insert x A)" in finite_subset)
 | 
|  |    122 |    apply fast+
 | 
|  |    123 |   done
 | 
|  |    124 | 
 | 
|  |    125 | text {*
 | 
|  |    126 |   Main theorem: combinatorial statement about number of subsets of a set.
 | 
|  |    127 | *}
 | 
|  |    128 | 
 | 
|  |    129 | lemma n_sub_lemma:
 | 
| 21263 |    130 |     "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
 | 
| 21256 |    131 |   apply (induct k)
 | 
|  |    132 |    apply (simp add: card_s_0_eq_empty, atomize)
 | 
|  |    133 |   apply (rotate_tac -1, erule finite_induct)
 | 
|  |    134 |    apply (simp_all (no_asm_simp) cong add: conj_cong
 | 
|  |    135 |      add: card_s_0_eq_empty choose_deconstruct)
 | 
|  |    136 |   apply (subst card_Un_disjoint)
 | 
|  |    137 |      prefer 4 apply (force simp add: constr_bij)
 | 
|  |    138 |     prefer 3 apply force
 | 
|  |    139 |    prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
 | 
|  |    140 |      finite_subset [of _ "Pow (insert x F)", standard])
 | 
|  |    141 |   apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
 | 
|  |    142 |   done
 | 
|  |    143 | 
 | 
|  |    144 | theorem n_subsets:
 | 
|  |    145 |     "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
 | 
|  |    146 |   by (simp add: n_sub_lemma)
 | 
|  |    147 | 
 | 
|  |    148 | 
 | 
|  |    149 | text{* The binomial theorem (courtesy of Tobias Nipkow): *}
 | 
|  |    150 | 
 | 
|  |    151 | theorem binomial: "(a+b::nat)^n = (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
 | 
|  |    152 | proof (induct n)
 | 
|  |    153 |   case 0 thus ?case by simp
 | 
|  |    154 | next
 | 
|  |    155 |   case (Suc n)
 | 
|  |    156 |   have decomp: "{0..n+1} = {0} \<union> {n+1} \<union> {1..n}"
 | 
|  |    157 |     by (auto simp add:atLeastAtMost_def atLeast_def atMost_def)
 | 
|  |    158 |   have decomp2: "{0..n} = {0} \<union> {1..n}"
 | 
|  |    159 |     by (auto simp add:atLeastAtMost_def atLeast_def atMost_def)
 | 
|  |    160 |   have "(a+b::nat)^(n+1) = (a+b) * (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
 | 
|  |    161 |     using Suc by simp
 | 
|  |    162 |   also have "\<dots> =  a*(\<Sum>k=0..n. (n choose k) * a^k * b^(n-k)) +
 | 
|  |    163 |                    b*(\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
 | 
| 21263 |    164 |     by (rule nat_distrib)
 | 
| 21256 |    165 |   also have "\<dots> = (\<Sum>k=0..n. (n choose k) * a^(k+1) * b^(n-k)) +
 | 
|  |    166 |                   (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k+1))"
 | 
| 21263 |    167 |     by (simp add: setsum_right_distrib mult_ac)
 | 
| 21256 |    168 |   also have "\<dots> = (\<Sum>k=0..n. (n choose k) * a^k * b^(n+1-k)) +
 | 
|  |    169 |                   (\<Sum>k=1..n+1. (n choose (k - 1)) * a^k * b^(n+1-k))"
 | 
|  |    170 |     by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le
 | 
|  |    171 |              del:setsum_cl_ivl_Suc)
 | 
|  |    172 |   also have "\<dots> = a^(n+1) + b^(n+1) +
 | 
|  |    173 |                   (\<Sum>k=1..n. (n choose (k - 1)) * a^k * b^(n+1-k)) +
 | 
|  |    174 |                   (\<Sum>k=1..n. (n choose k) * a^k * b^(n+1-k))"
 | 
| 21263 |    175 |     by (simp add: decomp2)
 | 
| 21256 |    176 |   also have
 | 
| 21263 |    177 |       "\<dots> = a^(n+1) + b^(n+1) + (\<Sum>k=1..n. (n+1 choose k) * a^k * b^(n+1-k))"
 | 
|  |    178 |     by (simp add: nat_distrib setsum_addf binomial.simps)
 | 
| 21256 |    179 |   also have "\<dots> = (\<Sum>k=0..n+1. (n+1 choose k) * a^k * b^(n+1-k))"
 | 
|  |    180 |     using decomp by simp
 | 
|  |    181 |   finally show ?case by simp
 | 
|  |    182 | qed
 | 
|  |    183 | 
 | 
|  |    184 | end
 |