src/HOL/Library/Binomial.thy
author nipkow
Sun Oct 21 14:53:44 2007 +0200 (2007-10-21)
changeset 25134 3d4953e88449
parent 25112 98824cc791c0
child 25162 ad4d5365d9d8
permissions -rw-r--r--
Eliminated most of the neq0_conv occurrences. As a result, many
theorems had to be rephrased with ~= 0 instead of > 0.
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
wenzelm@21256
    10
imports Main
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@25134
    47
lemma zero_less_binomial: "k \<le> n ==> (n choose k) \<noteq> 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@25134
    56
lemma zero_less_binomial_iff: "(n choose k \<noteq> 0) = (k\<le>n)"
nipkow@25134
    57
by (simp add: linorder_not_less binomial_eq_0_iff)
wenzelm@21256
    58
wenzelm@21256
    59
(*Might be more useful if re-oriented*)
wenzelm@21263
    60
lemma Suc_times_binomial_eq:
nipkow@25134
    61
  "!!k. k \<le> n ==> Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
nipkow@25134
    62
apply (induct n)
nipkow@25134
    63
apply (simp add: binomial_0)
nipkow@25134
    64
apply (case_tac k)
nipkow@25134
    65
apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq
wenzelm@21263
    66
    binomial_eq_0)
nipkow@25134
    67
done
wenzelm@21256
    68
wenzelm@21256
    69
text{*This is the well-known version, but it's harder to use because of the
wenzelm@21256
    70
  need to reason about division.*}
wenzelm@21256
    71
lemma binomial_Suc_Suc_eq_times:
wenzelm@21263
    72
    "k \<le> n ==> (Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
wenzelm@21263
    73
  by (simp add: Suc_times_binomial_eq div_mult_self_is_m zero_less_Suc
wenzelm@21263
    74
    del: mult_Suc mult_Suc_right)
wenzelm@21256
    75
wenzelm@21256
    76
text{*Another version, with -1 instead of Suc.*}
wenzelm@21256
    77
lemma times_binomial_minus1_eq:
wenzelm@21263
    78
    "[|k \<le> n;  0<k|] ==> (n choose k) * k = n * ((n - 1) choose (k - 1))"
wenzelm@21263
    79
  apply (cut_tac n = "n - 1" and k = "k - 1" in Suc_times_binomial_eq)
wenzelm@21263
    80
  apply (simp split add: nat_diff_split, auto)
wenzelm@21263
    81
  done
wenzelm@21263
    82
wenzelm@21256
    83
wenzelm@21256
    84
subsubsection {* Theorems about @{text "choose"} *}
wenzelm@21256
    85
wenzelm@21256
    86
text {*
wenzelm@21256
    87
  \medskip Basic theorem about @{text "choose"}.  By Florian
wenzelm@21256
    88
  Kamm\"uller, tidied by LCP.
wenzelm@21256
    89
*}
wenzelm@21256
    90
wenzelm@21256
    91
lemma card_s_0_eq_empty:
wenzelm@21256
    92
    "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
wenzelm@21256
    93
  apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
wenzelm@21256
    94
  apply (simp cong add: rev_conj_cong)
wenzelm@21256
    95
  done
wenzelm@21256
    96
wenzelm@21256
    97
lemma choose_deconstruct: "finite M ==> x \<notin> M
wenzelm@21256
    98
  ==> {s. s <= insert x M & card(s) = Suc k}
wenzelm@21256
    99
       = {s. s <= M & card(s) = Suc k} Un
wenzelm@21256
   100
         {s. EX t. t <= M & card(t) = k & s = insert x t}"
wenzelm@21256
   101
  apply safe
wenzelm@21256
   102
   apply (auto intro: finite_subset [THEN card_insert_disjoint])
wenzelm@21256
   103
  apply (drule_tac x = "xa - {x}" in spec)
wenzelm@21256
   104
  apply (subgoal_tac "x \<notin> xa", auto)
wenzelm@21256
   105
  apply (erule rev_mp, subst card_Diff_singleton)
wenzelm@21256
   106
  apply (auto intro: finite_subset)
wenzelm@21256
   107
  done
wenzelm@21256
   108
wenzelm@21256
   109
text{*There are as many subsets of @{term A} having cardinality @{term k}
wenzelm@21256
   110
 as there are sets obtained from the former by inserting a fixed element
wenzelm@21256
   111
 @{term x} into each.*}
wenzelm@21256
   112
lemma constr_bij:
wenzelm@21256
   113
   "[|finite A; x \<notin> A|] ==>
wenzelm@21256
   114
    card {B. EX C. C <= A & card(C) = k & B = insert x C} =
wenzelm@21256
   115
    card {B. B <= A & card(B) = k}"
wenzelm@21256
   116
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
wenzelm@21256
   117
       apply (auto elim!: equalityE simp add: inj_on_def)
wenzelm@21256
   118
    apply (subst Diff_insert0, auto)
wenzelm@21256
   119
   txt {* finiteness of the two sets *}
wenzelm@21256
   120
   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
wenzelm@21256
   121
   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
wenzelm@21256
   122
   apply fast+
wenzelm@21256
   123
  done
wenzelm@21256
   124
wenzelm@21256
   125
text {*
wenzelm@21256
   126
  Main theorem: combinatorial statement about number of subsets of a set.
wenzelm@21256
   127
*}
wenzelm@21256
   128
wenzelm@21256
   129
lemma n_sub_lemma:
wenzelm@21263
   130
    "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@21256
   131
  apply (induct k)
wenzelm@21256
   132
   apply (simp add: card_s_0_eq_empty, atomize)
wenzelm@21256
   133
  apply (rotate_tac -1, erule finite_induct)
wenzelm@21256
   134
   apply (simp_all (no_asm_simp) cong add: conj_cong
wenzelm@21256
   135
     add: card_s_0_eq_empty choose_deconstruct)
wenzelm@21256
   136
  apply (subst card_Un_disjoint)
wenzelm@21256
   137
     prefer 4 apply (force simp add: constr_bij)
wenzelm@21256
   138
    prefer 3 apply force
wenzelm@21256
   139
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
wenzelm@21256
   140
     finite_subset [of _ "Pow (insert x F)", standard])
wenzelm@21256
   141
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
wenzelm@21256
   142
  done
wenzelm@21256
   143
wenzelm@21256
   144
theorem n_subsets:
wenzelm@21256
   145
    "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@21256
   146
  by (simp add: n_sub_lemma)
wenzelm@21256
   147
wenzelm@21256
   148
wenzelm@21256
   149
text{* The binomial theorem (courtesy of Tobias Nipkow): *}
wenzelm@21256
   150
wenzelm@21256
   151
theorem binomial: "(a+b::nat)^n = (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
wenzelm@21256
   152
proof (induct n)
wenzelm@21256
   153
  case 0 thus ?case by simp
wenzelm@21256
   154
next
wenzelm@21256
   155
  case (Suc n)
wenzelm@21256
   156
  have decomp: "{0..n+1} = {0} \<union> {n+1} \<union> {1..n}"
wenzelm@21256
   157
    by (auto simp add:atLeastAtMost_def atLeast_def atMost_def)
wenzelm@21256
   158
  have decomp2: "{0..n} = {0} \<union> {1..n}"
wenzelm@21256
   159
    by (auto simp add:atLeastAtMost_def atLeast_def atMost_def)
wenzelm@21256
   160
  have "(a+b::nat)^(n+1) = (a+b) * (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
wenzelm@21256
   161
    using Suc by simp
wenzelm@21256
   162
  also have "\<dots> =  a*(\<Sum>k=0..n. (n choose k) * a^k * b^(n-k)) +
wenzelm@21256
   163
                   b*(\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
wenzelm@21263
   164
    by (rule nat_distrib)
wenzelm@21256
   165
  also have "\<dots> = (\<Sum>k=0..n. (n choose k) * a^(k+1) * b^(n-k)) +
wenzelm@21256
   166
                  (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k+1))"
wenzelm@21263
   167
    by (simp add: setsum_right_distrib mult_ac)
wenzelm@21256
   168
  also have "\<dots> = (\<Sum>k=0..n. (n choose k) * a^k * b^(n+1-k)) +
wenzelm@21256
   169
                  (\<Sum>k=1..n+1. (n choose (k - 1)) * a^k * b^(n+1-k))"
wenzelm@21256
   170
    by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le
wenzelm@21256
   171
             del:setsum_cl_ivl_Suc)
wenzelm@21256
   172
  also have "\<dots> = a^(n+1) + b^(n+1) +
wenzelm@21256
   173
                  (\<Sum>k=1..n. (n choose (k - 1)) * a^k * b^(n+1-k)) +
wenzelm@21256
   174
                  (\<Sum>k=1..n. (n choose k) * a^k * b^(n+1-k))"
wenzelm@21263
   175
    by (simp add: decomp2)
wenzelm@21256
   176
  also have
wenzelm@21263
   177
      "\<dots> = a^(n+1) + b^(n+1) + (\<Sum>k=1..n. (n+1 choose k) * a^k * b^(n+1-k))"
wenzelm@21263
   178
    by (simp add: nat_distrib setsum_addf binomial.simps)
wenzelm@21256
   179
  also have "\<dots> = (\<Sum>k=0..n+1. (n+1 choose k) * a^k * b^(n+1-k))"
wenzelm@21256
   180
    using decomp by simp
wenzelm@21256
   181
  finally show ?case by simp
wenzelm@21256
   182
qed
wenzelm@21256
   183
wenzelm@21256
   184
end