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