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