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