src/HOL/Library/Binomial.thy
author haftmann
Tue Feb 19 19:44:10 2013 +0100 (2013-02-19)
changeset 51188 9b5bf1a9a710
parent 50240 019d642d422d
child 52903 6c89225ddeba
permissions -rw-r--r--
dropped spurious left-over from 0a2371e7ced3
haftmann@35372
     1
(*  Title:      HOL/Library/Binomial.thy
chaieb@29694
     2
    Author:     Lawrence C Paulson, Amine Chaieb
wenzelm@21256
     3
    Copyright   1997  University of Cambridge
wenzelm@21256
     4
*)
wenzelm@21256
     5
wenzelm@21263
     6
header {* Binomial Coefficients *}
wenzelm@21256
     7
wenzelm@21256
     8
theory Binomial
haftmann@35372
     9
imports Complex_Main
wenzelm@21256
    10
begin
wenzelm@21256
    11
wenzelm@21263
    12
text {* This development is based on the work of Andy Gordon and
wenzelm@21263
    13
  Florian Kammueller. *}
wenzelm@21256
    14
haftmann@29931
    15
primrec binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat" (infixl "choose" 65) where
wenzelm@21263
    16
  binomial_0: "(0 choose k) = (if k = 0 then 1 else 0)"
wenzelm@48830
    17
| binomial_Suc: "(Suc n choose k) =
wenzelm@21256
    18
                 (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"
wenzelm@21256
    19
wenzelm@21256
    20
lemma binomial_n_0 [simp]: "(n choose 0) = 1"
wenzelm@48830
    21
  by (cases n) simp_all
wenzelm@21256
    22
wenzelm@21256
    23
lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
wenzelm@48830
    24
  by simp
wenzelm@21256
    25
wenzelm@21256
    26
lemma binomial_Suc_Suc [simp]:
nipkow@25134
    27
  "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
wenzelm@48830
    28
  by simp
wenzelm@21256
    29
wenzelm@21263
    30
lemma binomial_eq_0: "!!k. n < k ==> (n choose k) = 0"
wenzelm@48830
    31
  by (induct n) auto
wenzelm@21256
    32
wenzelm@21256
    33
declare binomial_0 [simp del] binomial_Suc [simp del]
wenzelm@21256
    34
wenzelm@21256
    35
lemma binomial_n_n [simp]: "(n choose n) = 1"
wenzelm@48830
    36
  by (induct n) (simp_all add: binomial_eq_0)
wenzelm@21256
    37
wenzelm@21256
    38
lemma binomial_Suc_n [simp]: "(Suc n choose n) = Suc n"
wenzelm@48830
    39
  by (induct n) simp_all
wenzelm@21256
    40
wenzelm@21256
    41
lemma binomial_1 [simp]: "(n choose Suc 0) = n"
wenzelm@48830
    42
  by (induct n) simp_all
wenzelm@21256
    43
nipkow@25162
    44
lemma zero_less_binomial: "k \<le> n ==> (n choose k) > 0"
wenzelm@48830
    45
  by (induct n k rule: diff_induct) simp_all
wenzelm@21256
    46
wenzelm@21256
    47
lemma binomial_eq_0_iff: "(n choose k = 0) = (n<k)"
wenzelm@48830
    48
  apply (safe intro!: binomial_eq_0)
wenzelm@48830
    49
  apply (erule contrapos_pp)
wenzelm@48830
    50
  apply (simp add: zero_less_binomial)
wenzelm@48830
    51
  done
wenzelm@21256
    52
nipkow@25162
    53
lemma zero_less_binomial_iff: "(n choose k > 0) = (k\<le>n)"
wenzelm@48830
    54
  by (simp add: linorder_not_less binomial_eq_0_iff neq0_conv[symmetric] del: neq0_conv)
wenzelm@21256
    55
wenzelm@21256
    56
(*Might be more useful if re-oriented*)
wenzelm@21263
    57
lemma Suc_times_binomial_eq:
nipkow@25134
    58
  "!!k. k \<le> n ==> Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
wenzelm@48830
    59
  apply (induct n)
wenzelm@48830
    60
   apply (simp add: binomial_0)
wenzelm@48830
    61
   apply (case_tac k)
wenzelm@48830
    62
  apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0)
wenzelm@48830
    63
  done
wenzelm@21256
    64
wenzelm@21256
    65
text{*This is the well-known version, but it's harder to use because of the
wenzelm@21256
    66
  need to reason about division.*}
wenzelm@21256
    67
lemma binomial_Suc_Suc_eq_times:
wenzelm@21263
    68
    "k \<le> n ==> (Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
wenzelm@46507
    69
  by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)
wenzelm@21256
    70
wenzelm@21256
    71
text{*Another version, with -1 instead of Suc.*}
wenzelm@21256
    72
lemma times_binomial_minus1_eq:
wenzelm@21263
    73
    "[|k \<le> n;  0<k|] ==> (n choose k) * k = n * ((n - 1) choose (k - 1))"
wenzelm@21263
    74
  apply (cut_tac n = "n - 1" and k = "k - 1" in Suc_times_binomial_eq)
wenzelm@48830
    75
   apply (simp split add: nat_diff_split, auto)
wenzelm@21263
    76
  done
wenzelm@21263
    77
wenzelm@21256
    78
wenzelm@25378
    79
subsection {* Theorems about @{text "choose"} *}
wenzelm@21256
    80
wenzelm@21256
    81
text {*
wenzelm@21256
    82
  \medskip Basic theorem about @{text "choose"}.  By Florian
wenzelm@21256
    83
  Kamm\"uller, tidied by LCP.
wenzelm@21256
    84
*}
wenzelm@21256
    85
wenzelm@48830
    86
lemma card_s_0_eq_empty: "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
wenzelm@48830
    87
  by (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
wenzelm@21256
    88
wenzelm@21256
    89
lemma choose_deconstruct: "finite M ==> x \<notin> M
wenzelm@21256
    90
  ==> {s. s <= insert x M & card(s) = Suc k}
wenzelm@21256
    91
       = {s. s <= M & card(s) = Suc k} Un
wenzelm@21256
    92
         {s. EX t. t <= M & card(t) = k & s = insert x t}"
wenzelm@21256
    93
  apply safe
wenzelm@48830
    94
     apply (auto intro: finite_subset [THEN card_insert_disjoint])
wenzelm@21256
    95
  apply (drule_tac x = "xa - {x}" in spec)
wenzelm@21256
    96
  apply (subgoal_tac "x \<notin> xa", auto)
wenzelm@21256
    97
  apply (erule rev_mp, subst card_Diff_singleton)
wenzelm@48830
    98
    apply (auto intro: finite_subset)
wenzelm@21256
    99
  done
nipkow@29918
   100
(*
nipkow@29918
   101
lemma "finite(UN y. {x. P x y})"
nipkow@29918
   102
apply simp
nipkow@29918
   103
lemma Collect_ex_eq
nipkow@29918
   104
nipkow@29918
   105
lemma "{x. EX y. P x y} = (UN y. {x. P x y})"
nipkow@29918
   106
apply blast
nipkow@29918
   107
*)
nipkow@29918
   108
nipkow@29918
   109
lemma finite_bex_subset[simp]:
nipkow@29918
   110
  "finite B \<Longrightarrow> (!!A. A<=B \<Longrightarrow> finite{x. P x A}) \<Longrightarrow> finite{x. EX A<=B. P x A}"
wenzelm@48830
   111
  apply (subgoal_tac "{x. EX A<=B. P x A} = (UN A:Pow B. {x. P x A})")
wenzelm@48830
   112
   apply simp
wenzelm@48830
   113
  apply blast
wenzelm@48830
   114
  done
wenzelm@21256
   115
wenzelm@21256
   116
text{*There are as many subsets of @{term A} having cardinality @{term k}
wenzelm@21256
   117
 as there are sets obtained from the former by inserting a fixed element
wenzelm@21256
   118
 @{term x} into each.*}
wenzelm@21256
   119
lemma constr_bij:
wenzelm@21256
   120
   "[|finite A; x \<notin> A|] ==>
wenzelm@21256
   121
    card {B. EX C. C <= A & card(C) = k & B = insert x C} =
wenzelm@21256
   122
    card {B. B <= A & card(B) = k}"
wenzelm@48830
   123
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
wenzelm@48830
   124
       apply (auto elim!: equalityE simp add: inj_on_def)
wenzelm@48830
   125
  apply (subst Diff_insert0, auto)
wenzelm@48830
   126
  done
wenzelm@21256
   127
wenzelm@21256
   128
text {*
wenzelm@21256
   129
  Main theorem: combinatorial statement about number of subsets of a set.
wenzelm@21256
   130
*}
wenzelm@21256
   131
wenzelm@21256
   132
lemma n_sub_lemma:
wenzelm@21263
   133
    "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@21256
   134
  apply (induct k)
wenzelm@21256
   135
   apply (simp add: card_s_0_eq_empty, atomize)
wenzelm@21256
   136
  apply (rotate_tac -1, erule finite_induct)
wenzelm@21256
   137
   apply (simp_all (no_asm_simp) cong add: conj_cong
wenzelm@21256
   138
     add: card_s_0_eq_empty choose_deconstruct)
wenzelm@21256
   139
  apply (subst card_Un_disjoint)
wenzelm@21256
   140
     prefer 4 apply (force simp add: constr_bij)
wenzelm@21256
   141
    prefer 3 apply force
wenzelm@21256
   142
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
wenzelm@21256
   143
     finite_subset [of _ "Pow (insert x F)", standard])
wenzelm@21256
   144
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
wenzelm@21256
   145
  done
wenzelm@21256
   146
wenzelm@21256
   147
theorem n_subsets:
wenzelm@21256
   148
    "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@21256
   149
  by (simp add: n_sub_lemma)
wenzelm@21256
   150
wenzelm@21256
   151
wenzelm@21256
   152
text{* The binomial theorem (courtesy of Tobias Nipkow): *}
wenzelm@21256
   153
wenzelm@21256
   154
theorem binomial: "(a+b::nat)^n = (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
wenzelm@21256
   155
proof (induct n)
wenzelm@21256
   156
  case 0 thus ?case by simp
wenzelm@21256
   157
next
wenzelm@21256
   158
  case (Suc n)
wenzelm@21256
   159
  have decomp: "{0..n+1} = {0} \<union> {n+1} \<union> {1..n}"
wenzelm@21256
   160
    by (auto simp add:atLeastAtMost_def atLeast_def atMost_def)
wenzelm@21256
   161
  have decomp2: "{0..n} = {0} \<union> {1..n}"
wenzelm@21256
   162
    by (auto simp add:atLeastAtMost_def atLeast_def atMost_def)
wenzelm@21256
   163
  have "(a+b::nat)^(n+1) = (a+b) * (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
wenzelm@21256
   164
    using Suc by simp
wenzelm@21256
   165
  also have "\<dots> =  a*(\<Sum>k=0..n. (n choose k) * a^k * b^(n-k)) +
wenzelm@21256
   166
                   b*(\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
wenzelm@21263
   167
    by (rule nat_distrib)
wenzelm@21256
   168
  also have "\<dots> = (\<Sum>k=0..n. (n choose k) * a^(k+1) * b^(n-k)) +
wenzelm@21256
   169
                  (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k+1))"
wenzelm@21263
   170
    by (simp add: setsum_right_distrib mult_ac)
wenzelm@21256
   171
  also have "\<dots> = (\<Sum>k=0..n. (n choose k) * a^k * b^(n+1-k)) +
wenzelm@21256
   172
                  (\<Sum>k=1..n+1. (n choose (k - 1)) * a^k * b^(n+1-k))"
wenzelm@21256
   173
    by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le
wenzelm@21256
   174
             del:setsum_cl_ivl_Suc)
wenzelm@21256
   175
  also have "\<dots> = a^(n+1) + b^(n+1) +
wenzelm@21256
   176
                  (\<Sum>k=1..n. (n choose (k - 1)) * a^k * b^(n+1-k)) +
wenzelm@21256
   177
                  (\<Sum>k=1..n. (n choose k) * a^k * b^(n+1-k))"
wenzelm@21263
   178
    by (simp add: decomp2)
wenzelm@21256
   179
  also have
wenzelm@21263
   180
      "\<dots> = a^(n+1) + b^(n+1) + (\<Sum>k=1..n. (n+1 choose k) * a^k * b^(n+1-k))"
wenzelm@21263
   181
    by (simp add: nat_distrib setsum_addf binomial.simps)
wenzelm@21256
   182
  also have "\<dots> = (\<Sum>k=0..n+1. (n+1 choose k) * a^k * b^(n+1-k))"
wenzelm@21256
   183
    using decomp by simp
wenzelm@21256
   184
  finally show ?case by simp
wenzelm@21256
   185
qed
wenzelm@21256
   186
huffman@29906
   187
subsection{* Pochhammer's symbol : generalized raising factorial*}
chaieb@29694
   188
chaieb@29694
   189
definition "pochhammer (a::'a::comm_semiring_1) n = (if n = 0 then 1 else setprod (\<lambda>n. a + of_nat n) {0 .. n - 1})"
chaieb@29694
   190
wenzelm@48830
   191
lemma pochhammer_0[simp]: "pochhammer a 0 = 1"
chaieb@29694
   192
  by (simp add: pochhammer_def)
chaieb@29694
   193
chaieb@29694
   194
lemma pochhammer_1[simp]: "pochhammer a 1 = a" by (simp add: pochhammer_def)
wenzelm@48830
   195
lemma pochhammer_Suc0[simp]: "pochhammer a (Suc 0) = a"
chaieb@29694
   196
  by (simp add: pochhammer_def)
chaieb@29694
   197
chaieb@29694
   198
lemma pochhammer_Suc_setprod: "pochhammer a (Suc n) = setprod (\<lambda>n. a + of_nat n) {0 .. n}"
chaieb@29694
   199
  by (simp add: pochhammer_def)
chaieb@29694
   200
chaieb@29694
   201
lemma setprod_nat_ivl_Suc: "setprod f {0 .. Suc n} = setprod f {0..n} * f (Suc n)"
chaieb@29694
   202
proof-
chaieb@29694
   203
  have eq: "{0..Suc n} = {0..n} \<union> {Suc n}" by auto
bulwahn@46757
   204
  show ?thesis unfolding eq by (simp add: field_simps)
chaieb@29694
   205
qed
chaieb@29694
   206
chaieb@29694
   207
lemma setprod_nat_ivl_1_Suc: "setprod f {0 .. Suc n} = f 0 * setprod f {1.. Suc n}"
chaieb@29694
   208
proof-
chaieb@29694
   209
  have eq: "{0..Suc n} = {0} \<union> {1 .. Suc n}" by auto
bulwahn@46757
   210
  show ?thesis unfolding eq by simp
chaieb@29694
   211
qed
chaieb@29694
   212
chaieb@29694
   213
chaieb@29694
   214
lemma pochhammer_Suc: "pochhammer a (Suc n) = pochhammer a n * (a + of_nat n)"
chaieb@29694
   215
proof-
wenzelm@48830
   216
  { assume "n=0" then have ?thesis by simp }
chaieb@29694
   217
  moreover
wenzelm@48830
   218
  { fix m assume m: "n = Suc m"
wenzelm@48830
   219
    have ?thesis unfolding m pochhammer_Suc_setprod setprod_nat_ivl_Suc .. }
wenzelm@48830
   220
  ultimately show ?thesis by (cases n) auto
wenzelm@48830
   221
qed
chaieb@29694
   222
chaieb@29694
   223
lemma pochhammer_rec: "pochhammer a (Suc n) = a * pochhammer (a + 1) n"
chaieb@29694
   224
proof-
wenzelm@48830
   225
  { assume "n=0" then have ?thesis by (simp add: pochhammer_Suc_setprod) }
chaieb@29694
   226
  moreover
wenzelm@48830
   227
  { assume n0: "n \<noteq> 0"
chaieb@29694
   228
    have th0: "finite {1 .. n}" "0 \<notin> {1 .. n}" by auto
chaieb@29694
   229
    have eq: "insert 0 {1 .. n} = {0..n}" by auto
chaieb@29694
   230
    have th1: "(\<Prod>n\<in>{1\<Colon>nat..n}. a + of_nat n) =
chaieb@29694
   231
      (\<Prod>n\<in>{0\<Colon>nat..n - 1}. a + 1 + of_nat n)"
haftmann@37388
   232
      apply (rule setprod_reindex_cong [where f = Suc])
nipkow@39302
   233
      using n0 by (auto simp add: fun_eq_iff field_simps)
chaieb@29694
   234
    have ?thesis apply (simp add: pochhammer_def)
chaieb@29694
   235
    unfolding setprod_insert[OF th0, unfolded eq]
wenzelm@48830
   236
    using th1 by (simp add: field_simps) }
wenzelm@48830
   237
  ultimately show ?thesis by blast
chaieb@29694
   238
qed
chaieb@29694
   239
chaieb@29694
   240
lemma pochhammer_fact: "of_nat (fact n) = pochhammer 1 n"
avigad@32042
   241
  unfolding fact_altdef_nat
wenzelm@48830
   242
  apply (cases n)
wenzelm@48830
   243
   apply (simp_all add: of_nat_setprod pochhammer_Suc_setprod)
chaieb@29694
   244
  apply (rule setprod_reindex_cong[where f=Suc])
wenzelm@48830
   245
    apply (auto simp add: fun_eq_iff)
wenzelm@48830
   246
  done
chaieb@29694
   247
wenzelm@48830
   248
lemma pochhammer_of_nat_eq_0_lemma:
wenzelm@48830
   249
  assumes kn: "k > n"
chaieb@29694
   250
  shows "pochhammer (- (of_nat n :: 'a:: idom)) k = 0"
chaieb@29694
   251
proof-
wenzelm@48830
   252
  from kn obtain h where h: "k = Suc h" by (cases k) auto
wenzelm@48830
   253
  { assume n0: "n=0" then have ?thesis using kn
wenzelm@48830
   254
      by (cases k) (simp_all add: pochhammer_rec) }
chaieb@29694
   255
  moreover
wenzelm@48830
   256
  { assume n0: "n \<noteq> 0"
wenzelm@48830
   257
    then have ?thesis
wenzelm@48830
   258
      apply (simp add: h pochhammer_Suc_setprod)
wenzelm@48830
   259
      apply (rule_tac x="n" in bexI)
wenzelm@48830
   260
      using h kn
wenzelm@48830
   261
      apply auto
wenzelm@48830
   262
      done }
wenzelm@48830
   263
  ultimately show ?thesis by blast
chaieb@29694
   264
qed
chaieb@29694
   265
chaieb@29694
   266
lemma pochhammer_of_nat_eq_0_lemma': assumes kn: "k \<le> n"
chaieb@29694
   267
  shows "pochhammer (- (of_nat n :: 'a:: {idom, ring_char_0})) k \<noteq> 0"
chaieb@29694
   268
proof-
wenzelm@48830
   269
  { assume "k=0" then have ?thesis by simp }
chaieb@29694
   270
  moreover
wenzelm@48830
   271
  { fix h assume h: "k = Suc h"
chaieb@29694
   272
    then have ?thesis apply (simp add: pochhammer_Suc_setprod)
wenzelm@48830
   273
      using h kn by (auto simp add: algebra_simps) }
wenzelm@48830
   274
  ultimately show ?thesis by (cases k) auto
chaieb@29694
   275
qed
chaieb@29694
   276
wenzelm@48830
   277
lemma pochhammer_of_nat_eq_0_iff:
chaieb@29694
   278
  shows "pochhammer (- (of_nat n :: 'a:: {idom, ring_char_0})) k = 0 \<longleftrightarrow> k > n"
chaieb@29694
   279
  (is "?l = ?r")
wenzelm@48830
   280
  using pochhammer_of_nat_eq_0_lemma[of n k, where ?'a='a]
chaieb@29694
   281
    pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]
chaieb@29694
   282
  by (auto simp add: not_le[symmetric])
chaieb@29694
   283
chaieb@32159
   284
wenzelm@48830
   285
lemma pochhammer_eq_0_iff:
chaieb@32159
   286
  "pochhammer a n = (0::'a::field_char_0) \<longleftrightarrow> (EX k < n . a = - of_nat k) "
chaieb@32159
   287
  apply (auto simp add: pochhammer_of_nat_eq_0_iff)
wenzelm@48830
   288
  apply (cases n)
wenzelm@48830
   289
   apply (auto simp add: pochhammer_def algebra_simps group_add_class.eq_neg_iff_add_eq_0)
chaieb@32159
   290
  apply (rule_tac x=x in exI)
chaieb@32159
   291
  apply auto
chaieb@32159
   292
  done
chaieb@32159
   293
chaieb@32159
   294
wenzelm@48830
   295
lemma pochhammer_eq_0_mono:
chaieb@32159
   296
  "pochhammer a n = (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"
wenzelm@48830
   297
  unfolding pochhammer_eq_0_iff by auto
chaieb@32159
   298
wenzelm@48830
   299
lemma pochhammer_neq_0_mono:
chaieb@32159
   300
  "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"
wenzelm@48830
   301
  unfolding pochhammer_eq_0_iff by auto
chaieb@32159
   302
chaieb@32159
   303
lemma pochhammer_minus:
wenzelm@48830
   304
  assumes kn: "k \<le> n"
chaieb@32159
   305
  shows "pochhammer (- b) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (b - of_nat k + 1) k"
chaieb@32159
   306
proof-
wenzelm@48830
   307
  { assume k0: "k = 0" then have ?thesis by simp }
wenzelm@48830
   308
  moreover
wenzelm@48830
   309
  { fix h assume h: "k = Suc h"
chaieb@32159
   310
    have eq: "((- 1) ^ Suc h :: 'a) = setprod (%i. - 1) {0 .. h}"
chaieb@32159
   311
      using setprod_constant[where A="{0 .. h}" and y="- 1 :: 'a"]
chaieb@32159
   312
      by auto
chaieb@32159
   313
    have ?thesis
wenzelm@46507
   314
      unfolding h pochhammer_Suc_setprod eq setprod_timesf[symmetric]
chaieb@32159
   315
      apply (rule strong_setprod_reindex_cong[where f = "%i. h - i"])
chaieb@32159
   316
      apply (auto simp add: inj_on_def image_def h )
chaieb@32159
   317
      apply (rule_tac x="h - x" in bexI)
wenzelm@48830
   318
      apply (auto simp add: fun_eq_iff h of_nat_diff)
wenzelm@48830
   319
      done }
wenzelm@48830
   320
  ultimately show ?thesis by (cases k) auto
chaieb@32159
   321
qed
chaieb@32159
   322
chaieb@32159
   323
lemma pochhammer_minus':
wenzelm@48830
   324
  assumes kn: "k \<le> n"
chaieb@32159
   325
  shows "pochhammer (b - of_nat k + 1) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (- b) k"
chaieb@32159
   326
  unfolding pochhammer_minus[OF kn, where b=b]
chaieb@32159
   327
  unfolding mult_assoc[symmetric]
chaieb@32159
   328
  unfolding power_add[symmetric]
chaieb@32159
   329
  apply simp
chaieb@32159
   330
  done
chaieb@32159
   331
chaieb@32159
   332
lemma pochhammer_same: "pochhammer (- of_nat n) n = ((- 1) ^ n :: 'a::comm_ring_1) * of_nat (fact n)"
chaieb@32159
   333
  unfolding pochhammer_minus[OF le_refl[of n]]
chaieb@32159
   334
  by (simp add: of_nat_diff pochhammer_fact)
chaieb@32159
   335
huffman@29906
   336
subsection{* Generalized binomial coefficients *}
chaieb@29694
   337
huffman@31287
   338
definition gbinomial :: "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
wenzelm@48830
   339
  where "a gchoose n =
wenzelm@48830
   340
    (if n = 0 then 1 else (setprod (\<lambda>i. a - of_nat i) {0 .. n - 1}) / of_nat (fact n))"
chaieb@29694
   341
chaieb@29694
   342
lemma gbinomial_0[simp]: "a gchoose 0 = 1" "0 gchoose (Suc n) = 0"
wenzelm@48830
   343
  apply (simp_all add: gbinomial_def)
wenzelm@48830
   344
  apply (subgoal_tac "(\<Prod>i\<Colon>nat\<in>{0\<Colon>nat..n}. - of_nat i) = (0::'b)")
wenzelm@48830
   345
   apply (simp del:setprod_zero_iff)
wenzelm@48830
   346
  apply simp
wenzelm@48830
   347
  done
chaieb@29694
   348
chaieb@29694
   349
lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / of_nat (fact n)"
wenzelm@48830
   350
proof -
wenzelm@48830
   351
  { assume "n=0" then have ?thesis by simp }
chaieb@29694
   352
  moreover
wenzelm@48830
   353
  { assume n0: "n\<noteq>0"
chaieb@29694
   354
    from n0 setprod_constant[of "{0 .. n - 1}" "- (1:: 'a)"]
chaieb@29694
   355
    have eq: "(- (1\<Colon>'a)) ^ n = setprod (\<lambda>i. - 1) {0 .. n - 1}"
chaieb@29694
   356
      by auto
wenzelm@48830
   357
    from n0 have ?thesis
wenzelm@48830
   358
      by (simp add: pochhammer_def gbinomial_def field_simps
wenzelm@48830
   359
        eq setprod_timesf[symmetric] del: minus_one) (* FIXME: del: minus_one *) }
chaieb@29694
   360
  ultimately show ?thesis by blast
chaieb@29694
   361
qed
chaieb@29694
   362
wenzelm@48830
   363
lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
wenzelm@48830
   364
proof (induct n arbitrary: k rule: nat_less_induct)
chaieb@29694
   365
  fix n k assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) =
chaieb@29694
   366
                      fact m" and kn: "k \<le> n"
wenzelm@48830
   367
  let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"
wenzelm@48830
   368
  { assume "n=0" then have ?ths using kn by simp }
chaieb@29694
   369
  moreover
wenzelm@48830
   370
  { assume "k=0" then have ?ths using kn by simp }
chaieb@29694
   371
  moreover
wenzelm@48830
   372
  { assume nk: "n=k" then have ?ths by simp }
chaieb@29694
   373
  moreover
wenzelm@48830
   374
  { fix m h assume n: "n = Suc m" and h: "k = Suc h" and hm: "h < m"
chaieb@29694
   375
    from n have mn: "m < n" by arith
chaieb@29694
   376
    from hm have hm': "h \<le> m" by arith
chaieb@29694
   377
    from hm h n kn have km: "k \<le> m" by arith
wenzelm@48830
   378
    have "m - h = Suc (m - Suc h)" using  h km hm by arith
chaieb@29694
   379
    with km h have th0: "fact (m - h) = (m - h) * fact (m - k)"
chaieb@29694
   380
      by simp
wenzelm@48830
   381
    from n h th0
wenzelm@48830
   382
    have "fact k * fact (n - k) * (n choose k) =
wenzelm@48830
   383
        k * (fact h * fact (m - h) * (m choose h)) +  (m - h) * (fact k * fact (m - k) * (m choose k))"
haftmann@36350
   384
      by (simp add: field_simps)
chaieb@29694
   385
    also have "\<dots> = (k + (m - h)) * fact m"
chaieb@29694
   386
      using H[rule_format, OF mn hm'] H[rule_format, OF mn km]
haftmann@36350
   387
      by (simp add: field_simps)
wenzelm@48830
   388
    finally have ?ths using h n km by simp }
wenzelm@48830
   389
  moreover have "n=0 \<or> k = 0 \<or> k = n \<or> (EX m h. n=Suc m \<and> k = Suc h \<and> h < m)"
wenzelm@48830
   390
    using kn by presburger
chaieb@29694
   391
  ultimately show ?ths by blast
chaieb@29694
   392
qed
wenzelm@48830
   393
wenzelm@48830
   394
lemma binomial_fact:
wenzelm@48830
   395
  assumes kn: "k \<le> n"
wenzelm@48830
   396
  shows "(of_nat (n choose k) :: 'a::field_char_0) =
wenzelm@48830
   397
    of_nat (fact n) / (of_nat (fact k) * of_nat (fact (n - k)))"
chaieb@29694
   398
  using binomial_fact_lemma[OF kn]
haftmann@36350
   399
  by (simp add: field_simps of_nat_mult [symmetric])
chaieb@29694
   400
chaieb@29694
   401
lemma binomial_gbinomial: "of_nat (n choose k) = of_nat n gchoose k"
wenzelm@48830
   402
proof -
wenzelm@48830
   403
  { assume kn: "k > n"
wenzelm@48830
   404
    from kn binomial_eq_0[OF kn] have ?thesis
wenzelm@48830
   405
      by (simp add: gbinomial_pochhammer field_simps  pochhammer_of_nat_eq_0_iff) }
chaieb@29694
   406
  moreover
wenzelm@48830
   407
  { assume "k=0" then have ?thesis by simp }
chaieb@29694
   408
  moreover
wenzelm@48830
   409
  { assume kn: "k \<le> n" and k0: "k\<noteq> 0"
wenzelm@48830
   410
    from k0 obtain h where h: "k = Suc h" by (cases k) auto
chaieb@29694
   411
    from h
chaieb@29694
   412
    have eq:"(- 1 :: 'a) ^ k = setprod (\<lambda>i. - 1) {0..h}"
chaieb@29694
   413
      by (subst setprod_constant, auto)
chaieb@29694
   414
    have eq': "(\<Prod>i\<in>{0..h}. of_nat n + - (of_nat i :: 'a)) = (\<Prod>i\<in>{n - h..n}. of_nat i)"
chaieb@29694
   415
      apply (rule strong_setprod_reindex_cong[where f="op - n"])
wenzelm@48830
   416
        using h kn
wenzelm@48830
   417
        apply (simp_all add: inj_on_def image_iff Bex_def set_eq_iff)
wenzelm@48830
   418
        apply clarsimp
wenzelm@48830
   419
        apply presburger
wenzelm@48830
   420
       apply presburger
wenzelm@48830
   421
      apply (simp add: fun_eq_iff field_simps of_nat_add[symmetric] del: of_nat_add)
wenzelm@48830
   422
      done
wenzelm@48830
   423
    have th0: "finite {1..n - Suc h}" "finite {n - h .. n}"
wenzelm@48830
   424
        "{1..n - Suc h} \<inter> {n - h .. n} = {}" and
wenzelm@48830
   425
        eq3: "{1..n - Suc h} \<union> {n - h .. n} = {1..n}"
wenzelm@48830
   426
      using h kn by auto
chaieb@29694
   427
    from eq[symmetric]
chaieb@29694
   428
    have ?thesis using kn
wenzelm@48830
   429
      apply (simp add: binomial_fact[OF kn, where ?'a = 'a]
huffman@47108
   430
        gbinomial_pochhammer field_simps pochhammer_Suc_setprod del: minus_one)
wenzelm@48830
   431
      apply (simp add: pochhammer_Suc_setprod fact_altdef_nat h
wenzelm@48830
   432
        of_nat_setprod setprod_timesf[symmetric] eq' del: One_nat_def power_Suc del: minus_one)
chaieb@29694
   433
      unfolding setprod_Un_disjoint[OF th0, unfolded eq3, of "of_nat:: nat \<Rightarrow> 'a"] eq[unfolded h]
wenzelm@48830
   434
      unfolding mult_assoc[symmetric]
chaieb@29694
   435
      unfolding setprod_timesf[symmetric]
chaieb@29694
   436
      apply simp
chaieb@29694
   437
      apply (rule strong_setprod_reindex_cong[where f= "op - n"])
wenzelm@48830
   438
        apply (auto simp add: inj_on_def image_iff Bex_def)
wenzelm@48830
   439
       apply presburger
chaieb@29694
   440
      apply (subgoal_tac "(of_nat (n - x) :: 'a) = of_nat n - of_nat x")
wenzelm@48830
   441
       apply simp
wenzelm@48830
   442
      apply (rule of_nat_diff)
chaieb@29694
   443
      apply simp
wenzelm@48830
   444
      done
chaieb@29694
   445
  }
chaieb@29694
   446
  moreover
chaieb@29694
   447
  have "k > n \<or> k = 0 \<or> (k \<le> n \<and> k \<noteq> 0)" by arith
chaieb@29694
   448
  ultimately show ?thesis by blast
chaieb@29694
   449
qed
chaieb@29694
   450
chaieb@29694
   451
lemma gbinomial_1[simp]: "a gchoose 1 = a"
chaieb@29694
   452
  by (simp add: gbinomial_def)
chaieb@29694
   453
chaieb@29694
   454
lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"
chaieb@29694
   455
  by (simp add: gbinomial_def)
chaieb@29694
   456
wenzelm@48830
   457
lemma gbinomial_mult_1:
wenzelm@48830
   458
  "a * (a gchoose n) =
wenzelm@48830
   459
    of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"  (is "?l = ?r")
wenzelm@48830
   460
proof -
chaieb@29694
   461
  have "?r = ((- 1) ^n * pochhammer (- a) n / of_nat (fact n)) * (of_nat n - (- a + of_nat n))"
chaieb@29694
   462
    unfolding gbinomial_pochhammer
wenzelm@48830
   463
      pochhammer_Suc fact_Suc of_nat_mult right_diff_distrib power_Suc
haftmann@36350
   464
    by (simp add:  field_simps del: of_nat_Suc)
chaieb@29694
   465
  also have "\<dots> = ?l" unfolding gbinomial_pochhammer
haftmann@36350
   466
    by (simp add: field_simps)
chaieb@29694
   467
  finally show ?thesis ..
chaieb@29694
   468
qed
chaieb@29694
   469
wenzelm@48830
   470
lemma gbinomial_mult_1':
wenzelm@48830
   471
    "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
chaieb@29694
   472
  by (simp add: mult_commute gbinomial_mult_1)
chaieb@29694
   473
wenzelm@48830
   474
lemma gbinomial_Suc:
wenzelm@48830
   475
    "a gchoose (Suc k) = (setprod (\<lambda>i. a - of_nat i) {0 .. k}) / of_nat (fact (Suc k))"
chaieb@29694
   476
  by (simp add: gbinomial_def)
wenzelm@48830
   477
chaieb@29694
   478
lemma gbinomial_mult_fact:
wenzelm@48830
   479
  "(of_nat (fact (Suc k)) :: 'a) * ((a::'a::field_char_0) gchoose (Suc k)) =
wenzelm@48830
   480
    (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
wenzelm@48830
   481
  by (simp_all add: gbinomial_Suc field_simps del: fact_Suc)
chaieb@29694
   482
chaieb@29694
   483
lemma gbinomial_mult_fact':
wenzelm@48830
   484
  "((a::'a::field_char_0) gchoose (Suc k)) * (of_nat (fact (Suc k)) :: 'a) =
wenzelm@48830
   485
    (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
chaieb@29694
   486
  using gbinomial_mult_fact[of k a]
wenzelm@48830
   487
  apply (subst mult_commute)
wenzelm@48830
   488
  apply assumption
wenzelm@48830
   489
  done
chaieb@29694
   490
wenzelm@48830
   491
wenzelm@48830
   492
lemma gbinomial_Suc_Suc:
wenzelm@48830
   493
  "((a::'a::field_char_0) + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"
wenzelm@48830
   494
proof -
wenzelm@48830
   495
  { assume "k = 0" then have ?thesis by simp }
chaieb@29694
   496
  moreover
wenzelm@48830
   497
  { fix h assume h: "k = Suc h"
wenzelm@48830
   498
    have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)"
wenzelm@48830
   499
      apply (rule strong_setprod_reindex_cong[where f = Suc])
wenzelm@48830
   500
        using h
wenzelm@48830
   501
        apply auto
wenzelm@48830
   502
      done
chaieb@29694
   503
wenzelm@48830
   504
    have "of_nat (fact (Suc k)) * (a gchoose k + (a gchoose (Suc k))) =
wenzelm@48830
   505
      ((a gchoose Suc h) * of_nat (fact (Suc h)) * of_nat (Suc k)) + (\<Prod>i\<in>{0\<Colon>nat..Suc h}. a - of_nat i)"
wenzelm@48830
   506
      apply (simp add: h field_simps del: fact_Suc)
chaieb@29694
   507
      unfolding gbinomial_mult_fact'
chaieb@29694
   508
      apply (subst fact_Suc)
wenzelm@48830
   509
      unfolding of_nat_mult
chaieb@29694
   510
      apply (subst mult_commute)
chaieb@29694
   511
      unfolding mult_assoc
chaieb@29694
   512
      unfolding gbinomial_mult_fact
wenzelm@48830
   513
      apply (simp add: field_simps)
wenzelm@48830
   514
      done
chaieb@29694
   515
    also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
chaieb@29694
   516
      unfolding gbinomial_mult_fact' setprod_nat_ivl_Suc
haftmann@36350
   517
      by (simp add: field_simps h)
chaieb@29694
   518
    also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
chaieb@29694
   519
      using eq0
wenzelm@48830
   520
      by (simp add: h setprod_nat_ivl_1_Suc)
chaieb@29694
   521
    also have "\<dots> = of_nat (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
chaieb@29694
   522
      unfolding gbinomial_mult_fact ..
wenzelm@48830
   523
    finally have ?thesis by (simp del: fact_Suc)
wenzelm@48830
   524
  }
wenzelm@48830
   525
  ultimately show ?thesis by (cases k) auto
chaieb@29694
   526
qed
chaieb@29694
   527
chaieb@32158
   528
wenzelm@48830
   529
lemma binomial_symmetric:
wenzelm@48830
   530
  assumes kn: "k \<le> n"
chaieb@32158
   531
  shows "n choose k = n choose (n - k)"
chaieb@32158
   532
proof-
chaieb@32158
   533
  from kn have kn': "n - k \<le> n" by arith
chaieb@32158
   534
  from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']
wenzelm@48830
   535
  have "fact k * fact (n - k) * (n choose k) =
wenzelm@48830
   536
    fact (n - k) * fact (n - (n - k)) * (n choose (n - k))" by simp
chaieb@32158
   537
  then show ?thesis using kn by simp
chaieb@32158
   538
qed
chaieb@32158
   539
hoelzl@50224
   540
(* Contributed by Manuel Eberl *)
hoelzl@50224
   541
(* Alternative definition of the binomial coefficient as \<Prod>i<k. (n - i) / (k - i) *)
hoelzl@50224
   542
lemma binomial_altdef_of_nat:
hoelzl@50224
   543
  fixes n k :: nat and x :: "'a :: {field_char_0, field_inverse_zero}"
hoelzl@50224
   544
  assumes "k \<le> n" shows "of_nat (n choose k) = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
hoelzl@50224
   545
proof cases
hoelzl@50224
   546
  assume "0 < k"
hoelzl@50224
   547
  then have "(of_nat (n choose k) :: 'a) = (\<Prod>i<k. of_nat n - of_nat i) / of_nat (fact k)"
hoelzl@50224
   548
    unfolding binomial_gbinomial gbinomial_def
hoelzl@50224
   549
    by (auto simp: gr0_conv_Suc lessThan_Suc_atMost atLeast0AtMost)
hoelzl@50224
   550
  also have "\<dots> = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
hoelzl@50224
   551
    using `k \<le> n` unfolding fact_eq_rev_setprod_nat of_nat_setprod
hoelzl@50224
   552
    by (auto simp add: setprod_dividef intro!: setprod_cong of_nat_diff[symmetric])
hoelzl@50224
   553
  finally show ?thesis .
hoelzl@50224
   554
qed simp
hoelzl@50224
   555
hoelzl@50224
   556
lemma binomial_ge_n_over_k_pow_k:
hoelzl@50224
   557
  fixes k n :: nat and x :: "'a :: linordered_field_inverse_zero"
hoelzl@50224
   558
  assumes "0 < k" and "k \<le> n" shows "(of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)"
hoelzl@50224
   559
proof -
hoelzl@50224
   560
  have "(of_nat n / of_nat k :: 'a) ^ k = (\<Prod>i<k. of_nat n / of_nat k :: 'a)"
hoelzl@50224
   561
    by (simp add: setprod_constant)
hoelzl@50224
   562
  also have "\<dots> \<le> of_nat (n choose k)"
hoelzl@50224
   563
    unfolding binomial_altdef_of_nat[OF `k\<le>n`]
hoelzl@50224
   564
  proof (safe intro!: setprod_mono)
hoelzl@50224
   565
    fix i::nat  assume  "i < k"
hoelzl@50224
   566
    from assms have "n * i \<ge> i * k" by simp
hoelzl@50224
   567
    hence "n * k - n * i \<le> n * k - i * k" by arith
hoelzl@50224
   568
    hence "n * (k - i) \<le> (n - i) * k"
hoelzl@50224
   569
      by (simp add: diff_mult_distrib2 nat_mult_commute)
hoelzl@50224
   570
    hence "of_nat n * of_nat (k - i) \<le> of_nat (n - i) * (of_nat k :: 'a)"
hoelzl@50224
   571
      unfolding of_nat_mult[symmetric] of_nat_le_iff .
hoelzl@50224
   572
    with assms show "of_nat n / of_nat k \<le> of_nat (n - i) / (of_nat (k - i) :: 'a)"
hoelzl@50224
   573
      using `i < k` by (simp add: field_simps)
hoelzl@50224
   574
  qed (simp add: zero_le_divide_iff)
hoelzl@50224
   575
  finally show ?thesis .
hoelzl@50224
   576
qed
hoelzl@50224
   577
hoelzl@50240
   578
lemma binomial_le_pow:
hoelzl@50240
   579
  assumes "r \<le> n" shows "n choose r \<le> n ^ r"
hoelzl@50240
   580
proof -
hoelzl@50240
   581
  have "n choose r \<le> fact n div fact (n - r)"
hoelzl@50240
   582
    using `r \<le> n` by (subst binomial_fact_lemma[symmetric]) auto
hoelzl@50240
   583
  with fact_div_fact_le_pow[OF assms] show ?thesis by auto
hoelzl@50240
   584
qed
hoelzl@50240
   585
hoelzl@50240
   586
lemma binomial_altdef_nat: "(k::nat) \<le> n \<Longrightarrow>
hoelzl@50240
   587
    n choose k = fact n div (fact k * fact (n - k))"
hoelzl@50240
   588
 by (subst binomial_fact_lemma[symmetric]) auto
hoelzl@50240
   589
wenzelm@21256
   590
end