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