src/HOL/Binomial.thy
 author hoelzl Fri Feb 19 13:40:50 2016 +0100 (2016-02-19) changeset 62378 85ed00c1fe7c parent 62347 2230b7047376 child 62481 b5d8e57826df permissions -rw-r--r--
generalize more theorems to support enat and ennreal
1 (*  Title       : Binomial.thy
2     Author      : Jacques D. Fleuriot
3     Copyright   : 1998  University of Cambridge
4     Conversion to Isar and new proofs by Lawrence C Paulson, 2004
5     The integer version of factorial and other additions by Jeremy Avigad.
6     Additional binomial identities by Chaitanya Mangla and Manuel Eberl
7 *)
9 section\<open>Factorial Function, Binomial Coefficients and Binomial Theorem\<close>
11 theory Binomial
12 imports Main
13 begin
15 subsection \<open>Factorial\<close>
17 fun (in semiring_char_0) fact :: "nat \<Rightarrow> 'a"
18   where "fact 0 = 1" | "fact (Suc n) = of_nat (Suc n) * fact n"
20 lemmas fact_Suc = fact.simps(2)
22 lemma fact_1 [simp]: "fact 1 = 1"
23   by simp
25 lemma fact_Suc_0 [simp]: "fact (Suc 0) = Suc 0"
26   by simp
28 lemma of_nat_fact [simp]:
29   "of_nat (fact n) = fact n"
30   by (induct n) (auto simp add: algebra_simps of_nat_mult)
32 lemma of_int_fact [simp]:
33   "of_int (fact n) = fact n"
34 proof -
35   have "of_int (of_nat (fact n)) = fact n"
36     unfolding of_int_of_nat_eq by simp
37   then show ?thesis
38     by simp
39 qed
41 lemma fact_reduce: "n > 0 \<Longrightarrow> fact n = of_nat n * fact (n - 1)"
42   by (cases n) auto
44 lemma fact_nonzero [simp]: "fact n \<noteq> (0::'a::{semiring_char_0,semiring_no_zero_divisors})"
45   apply (induct n)
46   apply auto
47   using of_nat_eq_0_iff by fastforce
49 lemma fact_mono_nat: "m \<le> n \<Longrightarrow> fact m \<le> (fact n :: nat)"
50   by (induct n) (auto simp: le_Suc_eq)
52 lemma fact_in_Nats: "fact n \<in> \<nat>" by (induction n) auto
54 lemma fact_in_Ints: "fact n \<in> \<int>" by (induction n) auto
56 context
57   assumes "SORT_CONSTRAINT('a::linordered_semidom)"
58 begin
60   lemma fact_mono: "m \<le> n \<Longrightarrow> fact m \<le> (fact n :: 'a)"
61     by (metis of_nat_fact of_nat_le_iff fact_mono_nat)
63   lemma fact_ge_1 [simp]: "fact n \<ge> (1 :: 'a)"
64     by (metis le0 fact.simps(1) fact_mono)
66   lemma fact_gt_zero [simp]: "fact n > (0 :: 'a)"
67     using fact_ge_1 less_le_trans zero_less_one by blast
69   lemma fact_ge_zero [simp]: "fact n \<ge> (0 :: 'a)"
70     by (simp add: less_imp_le)
72   lemma fact_not_neg [simp]: "~ (fact n < (0 :: 'a))"
73     by (simp add: not_less_iff_gr_or_eq)
75   lemma fact_le_power:
76       "fact n \<le> (of_nat (n^n) ::'a)"
77   proof (induct n)
78     case (Suc n)
79     then have *: "fact n \<le> (of_nat (Suc n ^ n) ::'a)"
80       by (rule order_trans) (simp add: power_mono del: of_nat_power)
81     have "fact (Suc n) = (of_nat (Suc n) * fact n ::'a)"
82       by (simp add: algebra_simps)
83     also have "... \<le> (of_nat (Suc n) * of_nat (Suc n ^ n) ::'a)"
84       by (simp add: "*" ordered_comm_semiring_class.comm_mult_left_mono del: of_nat_power)
85     also have "... \<le> (of_nat (Suc n ^ Suc n) ::'a)"
86       by (metis of_nat_mult order_refl power_Suc)
87     finally show ?case .
88   qed simp
90 end
92 text\<open>Note that @{term "fact 0 = fact 1"}\<close>
93 lemma fact_less_mono_nat: "\<lbrakk>0 < m; m < n\<rbrakk> \<Longrightarrow> fact m < (fact n :: nat)"
94   by (induct n) (auto simp: less_Suc_eq)
96 lemma fact_less_mono:
97   "\<lbrakk>0 < m; m < n\<rbrakk> \<Longrightarrow> fact m < (fact n :: 'a::linordered_semidom)"
98   by (metis of_nat_fact of_nat_less_iff fact_less_mono_nat)
100 lemma fact_ge_Suc_0_nat [simp]: "fact n \<ge> Suc 0"
101   by (metis One_nat_def fact_ge_1)
103 lemma dvd_fact:
104   shows "1 \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> m dvd fact n"
105   by (induct n) (auto simp: dvdI le_Suc_eq)
107 lemma fact_ge_self: "fact n \<ge> n"
108   by (cases "n = 0") (simp_all add: dvd_imp_le dvd_fact)
110 lemma fact_altdef_nat: "fact n = \<Prod>{1..n}"
111   by (induct n) (auto simp: atLeastAtMostSuc_conv)
113 lemma fact_altdef: "fact n = (\<Prod>i=1..n. of_nat i)"
114   by (induct n) (auto simp: atLeastAtMostSuc_conv)
116 lemma fact_altdef': "fact n = of_nat (\<Prod>{1..n})"
117   by (subst fact_altdef_nat [symmetric]) simp
119 lemma fact_dvd: "n \<le> m \<Longrightarrow> fact n dvd (fact m :: 'a :: {semiring_div,linordered_semidom})"
120   by (induct m) (auto simp: le_Suc_eq)
122 lemma fact_mod: "m \<le> n \<Longrightarrow> fact n mod (fact m :: 'a :: {semiring_div,linordered_semidom}) = 0"
123   by (auto simp add: fact_dvd)
125 lemma fact_div_fact:
126   assumes "m \<ge> n"
127   shows "(fact m) div (fact n) = \<Prod>{n + 1..m}"
128 proof -
129   obtain d where "d = m - n" by auto
130   from assms this have "m = n + d" by auto
131   have "fact (n + d) div (fact n) = \<Prod>{n + 1..n + d}"
132   proof (induct d)
133     case 0
134     show ?case by simp
135   next
136     case (Suc d')
137     have "fact (n + Suc d') div fact n = Suc (n + d') * fact (n + d') div fact n"
138       by simp
139     also from Suc.hyps have "... = Suc (n + d') * \<Prod>{n + 1..n + d'}"
140       unfolding div_mult1_eq[of _ "fact (n + d')"] by (simp add: fact_mod)
141     also have "... = \<Prod>{n + 1..n + Suc d'}"
142       by (simp add: atLeastAtMostSuc_conv)
143     finally show ?case .
144   qed
145   from this \<open>m = n + d\<close> show ?thesis by simp
146 qed
148 lemma fact_num_eq_if:
149     "fact m = (if m=0 then 1 else of_nat m * fact (m - 1))"
150 by (cases m) auto
152 lemma fact_eq_rev_setprod_nat: "fact k = (\<Prod>i<k. k - i)"
153   unfolding fact_altdef_nat
154   by (rule setprod.reindex_bij_witness[where i="\<lambda>i. k - i" and j="\<lambda>i. k - i"]) auto
156 lemma fact_div_fact_le_pow:
157   assumes "r \<le> n" shows "fact n div fact (n - r) \<le> n ^ r"
158 proof -
159   have "\<And>r. r \<le> n \<Longrightarrow> \<Prod>{n - r..n} = (n - r) * \<Prod>{Suc (n - r)..n}"
160     by (subst setprod.insert[symmetric]) (auto simp: atLeastAtMost_insertL)
161   with assms show ?thesis
162     by (induct r rule: nat.induct) (auto simp add: fact_div_fact Suc_diff_Suc mult_le_mono)
163 qed
165 lemma fact_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>
166   "fact (numeral k) = (numeral k) * (fact (pred_numeral k))"
167   by (metis fact.simps(2) numeral_eq_Suc of_nat_numeral)
170 text \<open>This development is based on the work of Andy Gordon and
171   Florian Kammueller.\<close>
173 subsection \<open>Basic definitions and lemmas\<close>
175 primrec binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat" (infixl "choose" 65)
176 where
177   "0 choose k = (if k = 0 then 1 else 0)"
178 | "Suc n choose k = (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"
180 lemma binomial_n_0 [simp]: "(n choose 0) = 1"
181   by (cases n) simp_all
183 lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
184   by simp
186 lemma binomial_Suc_Suc [simp]: "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
187   by simp
189 lemma choose_reduce_nat:
190   "0 < (n::nat) \<Longrightarrow> 0 < k \<Longrightarrow>
191     (n choose k) = ((n - 1) choose (k - 1)) + ((n - 1) choose k)"
192   by (metis Suc_diff_1 binomial.simps(2) neq0_conv)
194 lemma binomial_eq_0: "n < k \<Longrightarrow> n choose k = 0"
195   by (induct n arbitrary: k) auto
197 declare binomial.simps [simp del]
199 lemma binomial_n_n [simp]: "n choose n = 1"
200   by (induct n) (simp_all add: binomial_eq_0)
202 lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n"
203   by (induct n) simp_all
205 lemma binomial_1 [simp]: "n choose Suc 0 = n"
206   by (induct n) simp_all
208 lemma zero_less_binomial: "k \<le> n \<Longrightarrow> n choose k > 0"
209   by (induct n k rule: diff_induct) simp_all
211 lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> n < k"
212   by (metis binomial_eq_0 less_numeral_extra(3) not_less zero_less_binomial)
214 lemma zero_less_binomial_iff [simp]: "n choose k > 0 \<longleftrightarrow> k \<le> n"
215   by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)
217 lemma Suc_times_binomial_eq:
218   "Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
219   apply (induct n arbitrary: k, simp add: binomial.simps)
220   apply (case_tac k)
222   done
224 lemma binomial_le_pow2: "n choose k \<le> 2^n"
225   apply (induction n arbitrary: k)
226   apply (simp add: binomial.simps)
227   apply (case_tac k)
228   apply (auto simp: power_Suc)
231 text\<open>The absorption property\<close>
232 lemma Suc_times_binomial:
233   "Suc k * (Suc n choose Suc k) = Suc n * (n choose k)"
234   using Suc_times_binomial_eq by auto
236 text\<open>This is the well-known version of absorption, but it's harder to use because of the
237   need to reason about division.\<close>
238 lemma binomial_Suc_Suc_eq_times:
239     "(Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
240   by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)
242 text\<open>Another version of absorption, with -1 instead of Suc.\<close>
243 lemma times_binomial_minus1_eq:
244   "0 < k \<Longrightarrow> k * (n choose k) = n * ((n - 1) choose (k - 1))"
245   using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"]
246   by (auto split add: nat_diff_split)
249 subsection \<open>Combinatorial theorems involving \<open>choose\<close>\<close>
251 text \<open>By Florian Kamm\"uller, tidied by LCP.\<close>
253 lemma card_s_0_eq_empty: "finite A \<Longrightarrow> card {B. B \<subseteq> A & card B = 0} = 1"
254   by (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
256 lemma choose_deconstruct: "finite M \<Longrightarrow> x \<notin> M \<Longrightarrow>
257     {s. s \<subseteq> insert x M \<and> card s = Suc k} =
258     {s. s \<subseteq> M \<and> card s = Suc k} \<union> {s. \<exists>t. t \<subseteq> M \<and> card t = k \<and> s = insert x t}"
259   apply safe
260      apply (auto intro: finite_subset [THEN card_insert_disjoint])
261   by (metis (full_types) Diff_insert_absorb Set.set_insert Zero_neq_Suc card_Diff_singleton_if
262      card_eq_0_iff diff_Suc_1 in_mono subset_insert_iff)
264 lemma finite_bex_subset [simp]:
265   assumes "finite B"
266     and "\<And>A. A \<subseteq> B \<Longrightarrow> finite {x. P x A}"
267   shows "finite {x. \<exists>A \<subseteq> B. P x A}"
268   by (metis (no_types) assms finite_Collect_bounded_ex finite_Collect_subsets)
270 text\<open>There are as many subsets of @{term A} having cardinality @{term k}
271  as there are sets obtained from the former by inserting a fixed element
272  @{term x} into each.\<close>
273 lemma constr_bij:
274    "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow>
275     card {B. \<exists>C. C \<subseteq> A \<and> card C = k \<and> B = insert x C} =
276     card {B. B \<subseteq> A & card(B) = k}"
277   apply (rule card_bij_eq [where f = "\<lambda>s. s - {x}" and g = "insert x"])
278   apply (auto elim!: equalityE simp add: inj_on_def)
279   apply (metis card_Diff_singleton_if finite_subset in_mono)
280   done
282 text \<open>
283   Main theorem: combinatorial statement about number of subsets of a set.
284 \<close>
286 theorem n_subsets: "finite A \<Longrightarrow> card {B. B \<subseteq> A \<and> card B = k} = (card A choose k)"
287 proof (induct k arbitrary: A)
288   case 0 then show ?case by (simp add: card_s_0_eq_empty)
289 next
290   case (Suc k)
291   show ?case using \<open>finite A\<close>
292   proof (induct A)
293     case empty show ?case by (simp add: card_s_0_eq_empty)
294   next
295     case (insert x A)
296     then show ?case using Suc.hyps
297       apply (simp add: card_s_0_eq_empty choose_deconstruct)
298       apply (subst card_Un_disjoint)
299          prefer 4 apply (force simp add: constr_bij)
300         prefer 3 apply force
301        prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
302          finite_subset [of _ "Pow (insert x F)" for F])
303       apply (blast intro: finite_Pow_iff [THEN iffD2, THEN  finite_subset])
304       done
305   qed
306 qed
309 subsection \<open>The binomial theorem (courtesy of Tobias Nipkow):\<close>
311 text\<open>Avigad's version, generalized to any commutative ring\<close>
312 theorem binomial_ring: "(a+b::'a::{comm_ring_1,power})^n =
313   (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))" (is "?P n")
314 proof (induct n)
315   case 0 then show "?P 0" by simp
316 next
317   case (Suc n)
318   have decomp: "{0..n+1} = {0} Un {n+1} Un {1..n}"
319     by auto
320   have decomp2: "{0..n} = {0} Un {1..n}"
321     by auto
322   have "(a+b)^(n+1) =
323       (a+b) * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
324     using Suc.hyps by simp
325   also have "\<dots> = a*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +
326                    b*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
327     by (rule distrib_right)
328   also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +
329                   (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k+1))"
330     by (auto simp add: setsum_right_distrib ac_simps)
331   also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n+1-k)) +
332                   (\<Sum>k=1..n+1. of_nat (n choose (k - 1)) * a^k * b^(n+1-k))"
333     by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le field_simps
334         del:setsum_cl_ivl_Suc)
335   also have "\<dots> = a^(n+1) + b^(n+1) +
336                   (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n+1-k)) +
337                   (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n+1-k))"
338     by (simp add: decomp2)
339   also have
340       "\<dots> = a^(n+1) + b^(n+1) +
341             (\<Sum>k=1..n. of_nat(n+1 choose k) * a^k * b^(n+1-k))"
342     by (auto simp add: field_simps setsum.distrib [symmetric] choose_reduce_nat)
343   also have "\<dots> = (\<Sum>k=0..n+1. of_nat (n+1 choose k) * a^k * b^(n+1-k))"
344     using decomp by (simp add: field_simps)
345   finally show "?P (Suc n)" by simp
346 qed
348 text\<open>Original version for the naturals\<close>
349 corollary binomial: "(a+b::nat)^n = (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))"
350     using binomial_ring [of "int a" "int b" n]
351   by (simp only: of_nat_add [symmetric] of_nat_mult [symmetric] of_nat_power [symmetric]
352            of_nat_setsum [symmetric]
353            of_nat_eq_iff of_nat_id)
355 lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
356 proof (induct n arbitrary: k rule: nat_less_induct)
357   fix n k assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) =
358                       fact m" and kn: "k \<le> n"
359   let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"
360   { assume "n=0" then have ?ths using kn by simp }
361   moreover
362   { assume "k=0" then have ?ths using kn by simp }
363   moreover
364   { assume nk: "n=k" then have ?ths by simp }
365   moreover
366   { fix m h assume n: "n = Suc m" and h: "k = Suc h" and hm: "h < m"
367     from n have mn: "m < n" by arith
368     from hm have hm': "h \<le> m" by arith
369     from hm h n kn have km: "k \<le> m" by arith
370     have "m - h = Suc (m - Suc h)" using  h km hm by arith
371     with km h have th0: "fact (m - h) = (m - h) * fact (m - k)"
372       by simp
373     from n h th0
374     have "fact k * fact (n - k) * (n choose k) =
375         k * (fact h * fact (m - h) * (m choose h)) +
376         (m - h) * (fact k * fact (m - k) * (m choose k))"
377       by (simp add: field_simps)
378     also have "\<dots> = (k + (m - h)) * fact m"
379       using H[rule_format, OF mn hm'] H[rule_format, OF mn km]
380       by (simp add: field_simps)
381     finally have ?ths using h n km by simp }
382   moreover have "n=0 \<or> k = 0 \<or> k = n \<or> (\<exists>m h. n = Suc m \<and> k = Suc h \<and> h < m)"
383     using kn by presburger
384   ultimately show ?ths by blast
385 qed
387 lemma binomial_fact:
388   assumes kn: "k \<le> n"
389   shows "(of_nat (n choose k) :: 'a::field_char_0) =
390          (fact n) / (fact k * fact(n - k))"
391   using binomial_fact_lemma[OF kn]
392   apply (simp add: field_simps)
393   by (metis mult.commute of_nat_fact of_nat_mult)
395 lemma choose_row_sum: "(\<Sum>k=0..n. n choose k) = 2^n"
396   using binomial [of 1 "1" n]
397   by (simp add: numeral_2_eq_2)
399 lemma sum_choose_lower: "(\<Sum>k=0..n. (r+k) choose k) = Suc (r+n) choose n"
400   by (induct n) auto
402 lemma sum_choose_upper: "(\<Sum>k=0..n. k choose m) = Suc n choose Suc m"
403   by (induct n) auto
405 lemma choose_alternating_sum:
406   "n > 0 \<Longrightarrow> (\<Sum>i\<le>n. (-1)^i * of_nat (n choose i)) = (0 :: 'a :: comm_ring_1)"
407   using binomial_ring[of "-1 :: 'a" 1 n] by (simp add: atLeast0AtMost mult_of_nat_commute zero_power)
409 lemma choose_even_sum:
410   assumes "n > 0"
411   shows   "2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0) = (2 ^ n :: 'a :: comm_ring_1)"
412 proof -
413   have "2 ^ n = (\<Sum>i\<le>n. of_nat (n choose i)) + (\<Sum>i\<le>n. (-1) ^ i * of_nat (n choose i) :: 'a)"
414     using choose_row_sum[of n]
415     by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_setsum[symmetric] of_nat_power)
416   also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) + (-1) ^ i * of_nat (n choose i))"
417     by (simp add: setsum.distrib)
418   also have "\<dots> = 2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0)"
419     by (subst setsum_right_distrib, intro setsum.cong) simp_all
420   finally show ?thesis ..
421 qed
423 lemma choose_odd_sum:
424   assumes "n > 0"
425   shows   "2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0) = (2 ^ n :: 'a :: comm_ring_1)"
426 proof -
427   have "2 ^ n = (\<Sum>i\<le>n. of_nat (n choose i)) - (\<Sum>i\<le>n. (-1) ^ i * of_nat (n choose i) :: 'a)"
428     using choose_row_sum[of n]
429     by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_setsum[symmetric] of_nat_power)
430   also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) - (-1) ^ i * of_nat (n choose i))"
431     by (simp add: setsum_subtractf)
432   also have "\<dots> = 2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0)"
433     by (subst setsum_right_distrib, intro setsum.cong) simp_all
434   finally show ?thesis ..
435 qed
437 lemma choose_row_sum': "(\<Sum>k\<le>n. (n choose k)) = 2 ^ n"
438   using choose_row_sum[of n] by (simp add: atLeast0AtMost)
440 lemma natsum_reverse_index:
441   fixes m::nat
442   shows "(\<And>k. m \<le> k \<Longrightarrow> k \<le> n \<Longrightarrow> g k = f (m + n - k)) \<Longrightarrow> (\<Sum>k=m..n. f k) = (\<Sum>k=m..n. g k)"
443   by (rule setsum.reindex_bij_witness[where i="\<lambda>k. m+n-k" and j="\<lambda>k. m+n-k"]) auto
445 text\<open>NW diagonal sum property\<close>
446 lemma sum_choose_diagonal:
447   assumes "m\<le>n" shows "(\<Sum>k=0..m. (n-k) choose (m-k)) = Suc n choose m"
448 proof -
449   have "(\<Sum>k=0..m. (n-k) choose (m-k)) = (\<Sum>k=0..m. (n-m+k) choose k)"
450     by (rule natsum_reverse_index) (simp add: assms)
451   also have "... = Suc (n-m+m) choose m"
452     by (rule sum_choose_lower)
453   also have "... = Suc n choose m" using assms
454     by simp
455   finally show ?thesis .
456 qed
458 subsection\<open>Pochhammer's symbol : generalized rising factorial\<close>
460 text \<open>See @{url "http://en.wikipedia.org/wiki/Pochhammer_symbol"}\<close>
462 definition (in comm_semiring_1) "pochhammer (a :: 'a) n =
463   (if n = 0 then 1 else setprod (\<lambda>n. a + of_nat n) {0 .. n - 1})"
465 lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"
466   by (simp add: pochhammer_def)
468 lemma pochhammer_1 [simp]: "pochhammer a 1 = a"
469   by (simp add: pochhammer_def)
471 lemma pochhammer_Suc0 [simp]: "pochhammer a (Suc 0) = a"
472   by (simp add: pochhammer_def)
474 lemma pochhammer_Suc_setprod: "pochhammer a (Suc n) = setprod (\<lambda>n. a + of_nat n) {0 .. n}"
475   by (simp add: pochhammer_def)
477 lemma pochhammer_of_nat: "pochhammer (of_nat x) n = of_nat (pochhammer x n)"
478   by (simp add: pochhammer_def of_nat_setprod)
480 lemma pochhammer_of_int: "pochhammer (of_int x) n = of_int (pochhammer x n)"
481   by (simp add: pochhammer_def of_int_setprod)
483 lemma setprod_nat_ivl_Suc: "setprod f {0 .. Suc n} = setprod f {0..n} * f (Suc n)"
484 proof -
485   have "{0..Suc n} = {0..n} \<union> {Suc n}" by auto
486   then show ?thesis by (simp add: field_simps)
487 qed
489 lemma setprod_nat_ivl_1_Suc: "setprod f {0 .. Suc n} = f 0 * setprod f {1.. Suc n}"
490 proof -
491   have "{0..Suc n} = {0} \<union> {1 .. Suc n}" by auto
492   then show ?thesis by simp
493 qed
496 lemma pochhammer_Suc: "pochhammer a (Suc n) = pochhammer a n * (a + of_nat n)"
497 proof (cases n)
498   case 0
499   then show ?thesis by simp
500 next
501   case (Suc n)
502   show ?thesis unfolding Suc pochhammer_Suc_setprod setprod_nat_ivl_Suc ..
503 qed
505 lemma pochhammer_rec: "pochhammer a (Suc n) = a * pochhammer (a + 1) n"
506 proof (cases "n = 0")
507   case True
508   then show ?thesis by (simp add: pochhammer_Suc_setprod)
509 next
510   case False
511   have *: "finite {1 .. n}" "0 \<notin> {1 .. n}" by auto
512   have eq: "insert 0 {1 .. n} = {0..n}" by auto
513   have **: "(\<Prod>n\<in>{1::nat..n}. a + of_nat n) = (\<Prod>n\<in>{0::nat..n - 1}. a + 1 + of_nat n)"
514     apply (rule setprod.reindex_cong [where l = Suc])
515     using False
516     apply (auto simp add: fun_eq_iff field_simps)
517     done
518   show ?thesis
519     apply (simp add: pochhammer_def)
520     unfolding setprod.insert [OF *, unfolded eq]
521     using ** apply (simp add: field_simps)
522     done
523 qed
525 lemma pochhammer_rec': "pochhammer z (Suc n) = (z + of_nat n) * pochhammer z n"
526 proof (induction n arbitrary: z)
527   case (Suc n z)
528   have "pochhammer z (Suc (Suc n)) = z * pochhammer (z + 1) (Suc n)"
529     by (simp add: pochhammer_rec)
530   also note Suc
531   also have "z * ((z + 1 + of_nat n) * pochhammer (z + 1) n) =
532                (z + of_nat (Suc n)) * pochhammer z (Suc n)"
533     by (simp_all add: pochhammer_rec algebra_simps)
534   finally show ?case .
535 qed simp_all
537 lemma pochhammer_fact: "fact n = pochhammer 1 n"
538   unfolding fact_altdef
539   apply (cases n)
540    apply (simp_all add: of_nat_setprod pochhammer_Suc_setprod)
541   apply (rule setprod.reindex_cong [where l = Suc])
542     apply (auto simp add: fun_eq_iff)
543   done
545 lemma pochhammer_of_nat_eq_0_lemma:
546   assumes "k > n"
547   shows "pochhammer (- (of_nat n :: 'a:: idom)) k = 0"
548 proof (cases "n = 0")
549   case True
550   then show ?thesis
551     using assms by (cases k) (simp_all add: pochhammer_rec)
552 next
553   case False
554   from assms obtain h where "k = Suc h" by (cases k) auto
555   then show ?thesis
556     by (simp add: pochhammer_Suc_setprod)
557        (metis Suc_leI Suc_le_mono assms atLeastAtMost_iff less_eq_nat.simps(1))
558 qed
560 lemma pochhammer_of_nat_eq_0_lemma':
561   assumes kn: "k \<le> n"
562   shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k \<noteq> 0"
563 proof (cases k)
564   case 0
565   then show ?thesis by simp
566 next
567   case (Suc h)
568   then show ?thesis
569     apply (simp add: pochhammer_Suc_setprod)
570     using Suc kn apply (auto simp add: algebra_simps)
571     done
572 qed
574 lemma pochhammer_of_nat_eq_0_iff:
575   shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k = 0 \<longleftrightarrow> k > n"
576   (is "?l = ?r")
577   using pochhammer_of_nat_eq_0_lemma[of n k, where ?'a='a]
578     pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]
579   by (auto simp add: not_le[symmetric])
581 lemma pochhammer_eq_0_iff: "pochhammer a n = (0::'a::field_char_0) \<longleftrightarrow> (\<exists>k < n. a = - of_nat k)"
582   apply (auto simp add: pochhammer_of_nat_eq_0_iff)
583   apply (cases n)
585   apply (metis leD not_less_eq)
586   done
588 lemma pochhammer_eq_0_mono:
589   "pochhammer a n = (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"
590   unfolding pochhammer_eq_0_iff by auto
592 lemma pochhammer_neq_0_mono:
593   "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"
594   unfolding pochhammer_eq_0_iff by auto
596 lemma pochhammer_minus:
597     "pochhammer (- b) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (b - of_nat k + 1) k"
598 proof (cases k)
599   case 0
600   then show ?thesis by simp
601 next
602   case (Suc h)
603   have eq: "((- 1) ^ Suc h :: 'a) = (\<Prod>i=0..h. - 1)"
604     using setprod_constant[where A="{0 .. h}" and y="- 1 :: 'a"]
605     by auto
606   show ?thesis
607     unfolding Suc pochhammer_Suc_setprod eq setprod.distrib[symmetric]
608     by (rule setprod.reindex_bij_witness[where i="op - h" and j="op - h"])
609        (auto simp: of_nat_diff)
610 qed
612 lemma pochhammer_minus':
613     "pochhammer (b - of_nat k + 1) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (- b) k"
614   unfolding pochhammer_minus[where b=b]
615   unfolding mult.assoc[symmetric]
617   by simp
619 lemma pochhammer_same: "pochhammer (- of_nat n) n =
620     ((- 1) ^ n :: 'a::{semiring_char_0,comm_ring_1,semiring_no_zero_divisors}) * (fact n)"
621   unfolding pochhammer_minus
622   by (simp add: of_nat_diff pochhammer_fact)
624 lemma pochhammer_product':
625   "pochhammer z (n + m) = pochhammer z n * pochhammer (z + of_nat n) m"
626 proof (induction n arbitrary: z)
627   case (Suc n z)
628   have "pochhammer z (Suc n) * pochhammer (z + of_nat (Suc n)) m =
629             z * (pochhammer (z + 1) n * pochhammer (z + 1 + of_nat n) m)"
630     by (simp add: pochhammer_rec ac_simps)
631   also note Suc[symmetric]
632   also have "z * pochhammer (z + 1) (n + m) = pochhammer z (Suc (n + m))"
633     by (subst pochhammer_rec) simp
634   finally show ?case by simp
635 qed simp
637 lemma pochhammer_product:
638   "m \<le> n \<Longrightarrow> pochhammer z n = pochhammer z m * pochhammer (z + of_nat m) (n - m)"
639   using pochhammer_product'[of z m "n - m"] by simp
641 lemma pochhammer_times_pochhammer_half:
642   fixes z :: "'a :: field_char_0"
643   shows "pochhammer z (Suc n) * pochhammer (z + 1/2) (Suc n) = (\<Prod>k=0..2*n+1. z + of_nat k / 2)"
644 proof (induction n)
645   case (Suc n)
646   def n' \<equiv> "Suc n"
647   have "pochhammer z (Suc n') * pochhammer (z + 1 / 2) (Suc n') =
648           (pochhammer z n' * pochhammer (z + 1 / 2) n') *
649           ((z + of_nat n') * (z + 1/2 + of_nat n'))" (is "_ = _ * ?A")
650      by (simp_all add: pochhammer_rec' mult_ac)
651   also have "?A = (z + of_nat (Suc (2 * n + 1)) / 2) * (z + of_nat (Suc (Suc (2 * n + 1))) / 2)"
652     (is "_ = ?A") by (simp add: field_simps n'_def of_nat_mult)
653   also note Suc[folded n'_def]
654   also have "(\<Prod>k = 0..2 * n + 1. z + of_nat k / 2) * ?A = (\<Prod>k = 0..2 * Suc n + 1. z + of_nat k / 2)"
655     by (simp add: setprod_nat_ivl_Suc)
656   finally show ?case by (simp add: n'_def)
657 qed (simp add: setprod_nat_ivl_Suc)
659 lemma pochhammer_double:
660   fixes z :: "'a :: field_char_0"
661   shows "pochhammer (2 * z) (2 * n) = of_nat (2^(2*n)) * pochhammer z n * pochhammer (z+1/2) n"
662 proof (induction n)
663   case (Suc n)
664   have "pochhammer (2 * z) (2 * (Suc n)) = pochhammer (2 * z) (2 * n) *
665           (2 * (z + of_nat n)) * (2 * (z + of_nat n) + 1)"
666     by (simp add: pochhammer_rec' ac_simps of_nat_mult)
667   also note Suc
668   also have "of_nat (2 ^ (2 * n)) * pochhammer z n * pochhammer (z + 1/2) n *
669                  (2 * (z + of_nat n)) * (2 * (z + of_nat n) + 1) =
670              of_nat (2 ^ (2 * (Suc n))) * pochhammer z (Suc n) * pochhammer (z + 1/2) (Suc n)"
671     by (simp add: of_nat_mult field_simps pochhammer_rec')
672   finally show ?case .
673 qed simp
675 lemma pochhammer_absorb_comp:
676   "((r :: 'a :: comm_ring_1) - of_nat k) * pochhammer (- r) k = r * pochhammer (-r + 1) k"
677   (is "?lhs = ?rhs")
678 proof -
679   have "?lhs = -pochhammer (-r) (Suc k)" by (subst pochhammer_rec') (simp add: algebra_simps)
680   also have "\<dots> = ?rhs" by (subst pochhammer_rec) simp
681   finally show ?thesis .
682 qed
685 subsection\<open>Generalized binomial coefficients\<close>
687 definition (in field_char_0) gbinomial :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
688   where "a gchoose n =
689     (if n = 0 then 1 else (setprod (\<lambda>i. a - of_nat i) {0 .. n - 1}) / (fact n))"
691 lemma gbinomial_0 [simp]: "a gchoose 0 = 1" "0 gchoose (Suc n) = 0"
692   by (simp_all add: gbinomial_def)
694 lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / (fact n)"
695 proof (cases "n = 0")
696   case True
697   then show ?thesis by simp
698 next
699   case False
700   from this setprod_constant[of "{0 .. n - 1}" "- (1:: 'a)"]
701   have eq: "(- (1::'a)) ^ n = setprod (\<lambda>i. - 1) {0 .. n - 1}"
702     by auto
703   from False show ?thesis
704     by (simp add: pochhammer_def gbinomial_def field_simps
705       eq setprod.distrib[symmetric])
706 qed
708 lemma gbinomial_pochhammer':
709   "(s :: 'a :: field_char_0) gchoose n = pochhammer (s - of_nat n + 1) n / fact n"
710 proof -
711   have "s gchoose n = ((-1)^n * (-1)^n) * pochhammer (s - of_nat n + 1) n / fact n"
712     by (simp add: gbinomial_pochhammer pochhammer_minus mult_ac)
713   also have "(-1 :: 'a)^n * (-1)^n = 1" by (subst power_add [symmetric]) simp
714   finally show ?thesis by simp
715 qed
717 lemma binomial_gbinomial:
718     "of_nat (n choose k) = (of_nat n gchoose k :: 'a::field_char_0)"
719 proof -
720   { assume kn: "k > n"
721     then have ?thesis
722       by (subst binomial_eq_0[OF kn])
723          (simp add: gbinomial_pochhammer field_simps  pochhammer_of_nat_eq_0_iff) }
724   moreover
725   { assume "k=0" then have ?thesis by simp }
726   moreover
727   { assume kn: "k \<le> n" and k0: "k\<noteq> 0"
728     from k0 obtain h where h: "k = Suc h" by (cases k) auto
729     from h
730     have eq:"(- 1 :: 'a) ^ k = setprod (\<lambda>i. - 1) {0..h}"
731       by (subst setprod_constant) auto
732     have eq': "(\<Prod>i\<in>{0..h}. of_nat n + - (of_nat i :: 'a)) = (\<Prod>i\<in>{n - h..n}. of_nat i)"
733         using h kn
734       by (intro setprod.reindex_bij_witness[where i="op - n" and j="op - n"])
735          (auto simp: of_nat_diff)
736     have th0: "finite {1..n - Suc h}" "finite {n - h .. n}"
737         "{1..n - Suc h} \<inter> {n - h .. n} = {}" and
738         eq3: "{1..n - Suc h} \<union> {n - h .. n} = {1..n}"
739       using h kn by auto
740     from eq[symmetric]
741     have ?thesis using kn
742       apply (simp add: binomial_fact[OF kn, where ?'a = 'a]
743         gbinomial_pochhammer field_simps pochhammer_Suc_setprod)
744       apply (simp add: pochhammer_Suc_setprod fact_altdef h
745         of_nat_setprod setprod.distrib[symmetric] eq' del: One_nat_def power_Suc)
746       unfolding setprod.union_disjoint[OF th0, unfolded eq3, of "of_nat:: nat \<Rightarrow> 'a"] eq[unfolded h]
747       unfolding mult.assoc
748       unfolding setprod.distrib[symmetric]
749       apply simp
750       apply (intro setprod.reindex_bij_witness[where i="op - n" and j="op - n"])
751       apply (auto simp: of_nat_diff)
752       done
753   }
754   moreover
755   have "k > n \<or> k = 0 \<or> (k \<le> n \<and> k \<noteq> 0)" by arith
756   ultimately show ?thesis by blast
757 qed
759 lemma gbinomial_1[simp]: "a gchoose 1 = a"
760   by (simp add: gbinomial_def)
762 lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"
763   by (simp add: gbinomial_def)
765 lemma gbinomial_mult_1:
766   fixes a :: "'a :: field_char_0"
767   shows "a * (a gchoose n) =
768     of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"  (is "?l = ?r")
769 proof -
770   have "?r = ((- 1) ^n * pochhammer (- a) n / (fact n)) * (of_nat n - (- a + of_nat n))"
771     unfolding gbinomial_pochhammer
772       pochhammer_Suc of_nat_mult right_diff_distrib power_Suc
773     apply (simp del: of_nat_Suc fact.simps)
774     apply (auto simp add: field_simps simp del: of_nat_Suc)
775     done
776   also have "\<dots> = ?l" unfolding gbinomial_pochhammer
777     by (simp add: field_simps)
778   finally show ?thesis ..
779 qed
781 lemma gbinomial_mult_1':
782   fixes a :: "'a :: field_char_0"
783   shows "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
784   by (simp add: mult.commute gbinomial_mult_1)
786 lemma gbinomial_Suc:
787     "a gchoose (Suc k) = (setprod (\<lambda>i. a - of_nat i) {0 .. k}) / (fact (Suc k))"
788   by (simp add: gbinomial_def)
790 lemma gbinomial_mult_fact:
791   fixes a :: "'a::field_char_0"
792   shows
793    "fact (Suc k) * (a gchoose (Suc k)) =
794     (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
795   by (simp_all add: gbinomial_Suc field_simps del: fact.simps)
797 lemma gbinomial_mult_fact':
798   fixes a :: "'a::field_char_0"
799   shows "(a gchoose (Suc k)) * fact (Suc k) = (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
800   using gbinomial_mult_fact[of k a]
801   by (subst mult.commute)
803 lemma gbinomial_Suc_Suc:
804   fixes a :: "'a :: field_char_0"
805   shows "(a + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"
806 proof (cases k)
807   case 0
808   then show ?thesis by simp
809 next
810   case (Suc h)
811   have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)"
812     apply (rule setprod.reindex_cong [where l = Suc])
813       using Suc
814       apply auto
815     done
816   have "fact (Suc k) * (a gchoose k + (a gchoose (Suc k))) =
817         (a gchoose Suc h) * (fact (Suc (Suc h))) +
818         (a gchoose Suc (Suc h)) * (fact (Suc (Suc h)))"
819     by (simp add: Suc field_simps del: fact.simps)
820   also have "... = (a gchoose Suc h) * of_nat (Suc (Suc h) * fact (Suc h)) +
821                    (\<Prod>i = 0..Suc h. a - of_nat i)"
822     by (metis fact.simps(2) gbinomial_mult_fact' of_nat_fact of_nat_id)
823   also have "... = (fact (Suc h) * (a gchoose Suc h)) * of_nat (Suc (Suc h)) +
824                    (\<Prod>i = 0..Suc h. a - of_nat i)"
825     by (simp only: fact.simps(2) mult.commute mult.left_commute of_nat_fact of_nat_id of_nat_mult)
826   also have "... =  of_nat (Suc (Suc h)) * (\<Prod>i = 0..h. a - of_nat i) +
827                     (\<Prod>i = 0..Suc h. a - of_nat i)"
828     by (metis gbinomial_mult_fact mult.commute)
829   also have "... = (\<Prod>i = 0..Suc h. a - of_nat i) +
830                    (of_nat h * (\<Prod>i = 0..h. a - of_nat i) + 2 * (\<Prod>i = 0..h. a - of_nat i))"
831     by (simp add: field_simps)
832   also have "... =
833     ((a gchoose Suc h) * (fact (Suc h)) * of_nat (Suc k)) + (\<Prod>i\<in>{0::nat..Suc h}. a - of_nat i)"
834     unfolding gbinomial_mult_fact'
835     by (simp add: comm_semiring_class.distrib field_simps Suc)
836   also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
837     unfolding gbinomial_mult_fact' setprod_nat_ivl_Suc
838     by (simp add: field_simps Suc)
839   also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
840     using eq0
841     by (simp add: Suc setprod_nat_ivl_1_Suc)
842   also have "\<dots> = (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
843     unfolding gbinomial_mult_fact ..
844   finally show ?thesis
845     by (metis fact_nonzero mult_cancel_left)
846 qed
848 lemma gbinomial_reduce_nat:
849   fixes a :: "'a :: field_char_0"
850   shows "0 < k \<Longrightarrow> a gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)"
851   by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc)
853 lemma gchoose_row_sum_weighted:
854   fixes r :: "'a::field_char_0"
855   shows "(\<Sum>k = 0..m. (r gchoose k) * (r/2 - of_nat k)) = of_nat(Suc m) / 2 * (r gchoose (Suc m))"
856 proof (induct m)
857   case 0 show ?case by simp
858 next
859   case (Suc m)
860   from Suc show ?case
861     by (simp add: field_simps distrib gbinomial_mult_1)
862 qed
864 lemma binomial_symmetric:
865   assumes kn: "k \<le> n"
866   shows "n choose k = n choose (n - k)"
867 proof-
868   from kn have kn': "n - k \<le> n" by arith
869   from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']
870   have "fact k * fact (n - k) * (n choose k) =
871     fact (n - k) * fact (n - (n - k)) * (n choose (n - k))" by simp
872   then show ?thesis using kn by simp
873 qed
875 lemma choose_rising_sum:
876   "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))"
877   "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose m)"
878 proof -
879   show "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))" by (induction m) simp_all
880   also have "... = ((n + m + 1) choose m)" by (subst binomial_symmetric) simp_all
881   finally show "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose m)" .
882 qed
884 lemma choose_linear_sum:
885   "(\<Sum>i\<le>n. i * (n choose i)) = n * 2 ^ (n - 1)"
886 proof (cases n)
887   case (Suc m)
888   have "(\<Sum>i\<le>n. i * (n choose i)) = (\<Sum>i\<le>Suc m. i * (Suc m choose i))" by (simp add: Suc)
889   also have "... = Suc m * 2 ^ m"
890     by (simp only: setsum_atMost_Suc_shift Suc_times_binomial setsum_right_distrib[symmetric])
891        (simp add: choose_row_sum')
892   finally show ?thesis using Suc by simp
893 qed simp_all
895 lemma choose_alternating_linear_sum:
896   assumes "n \<noteq> 1"
897   shows   "(\<Sum>i\<le>n. (-1)^i * of_nat i * of_nat (n choose i) :: 'a :: comm_ring_1) = 0"
898 proof (cases n)
899   case (Suc m)
900   with assms have "m > 0" by simp
901   have "(\<Sum>i\<le>n. (-1) ^ i * of_nat i * of_nat (n choose i) :: 'a) =
902             (\<Sum>i\<le>Suc m. (-1) ^ i * of_nat i * of_nat (Suc m choose i))" by (simp add: Suc)
903   also have "... = (\<Sum>i\<le>m. (-1) ^ (Suc i) * of_nat (Suc i * (Suc m choose Suc i)))"
904     by (simp only: setsum_atMost_Suc_shift setsum_right_distrib[symmetric] of_nat_mult mult_ac) simp
905   also have "... = -of_nat (Suc m) * (\<Sum>i\<le>m. (-1) ^ i * of_nat ((m choose i)))"
906     by (subst setsum_right_distrib, rule setsum.cong[OF refl], subst Suc_times_binomial)
907        (simp add: algebra_simps of_nat_mult)
908   also have "(\<Sum>i\<le>m. (-1 :: 'a) ^ i * of_nat ((m choose i))) = 0"
909     using choose_alternating_sum[OF \<open>m > 0\<close>] by simp
910   finally show ?thesis by simp
911 qed simp
913 lemma vandermonde:
914   "(\<Sum>k\<le>r. (m choose k) * (n choose (r - k))) = (m + n) choose r"
915 proof (induction n arbitrary: r)
916   case 0
917   have "(\<Sum>k\<le>r. (m choose k) * (0 choose (r - k))) = (\<Sum>k\<le>r. if k = r then (m choose k) else 0)"
918     by (intro setsum.cong) simp_all
919   also have "... = m choose r" by (simp add: setsum.delta)
920   finally show ?case by simp
921 next
922   case (Suc n r)
923   show ?case by (cases r) (simp_all add: Suc [symmetric] algebra_simps setsum.distrib Suc_diff_le)
924 qed
926 lemma choose_square_sum:
927   "(\<Sum>k\<le>n. (n choose k)^2) = ((2*n) choose n)"
928   using vandermonde[of n n n] by (simp add: power2_eq_square mult_2 binomial_symmetric [symmetric])
930 lemma pochhammer_binomial_sum:
931   fixes a b :: "'a :: comm_ring_1"
932   shows "pochhammer (a + b) n = (\<Sum>k\<le>n. of_nat (n choose k) * pochhammer a k * pochhammer b (n - k))"
933 proof (induction n arbitrary: a b)
934   case (Suc n a b)
935   have "(\<Sum>k\<le>Suc n. of_nat (Suc n choose k) * pochhammer a k * pochhammer b (Suc n - k)) =
936             (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) +
937             ((\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) +
938             pochhammer b (Suc n))"
939     by (subst setsum_atMost_Suc_shift) (simp add: ring_distribs setsum.distrib)
940   also have "(\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) =
941                a * pochhammer ((a + 1) + b) n"
942     by (subst Suc) (simp add: setsum_right_distrib pochhammer_rec mult_ac)
943   also have "(\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) + pochhammer b (Suc n) =
944                (\<Sum>i=0..Suc n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))"
945     by (subst setsum_head_Suc, simp, subst setsum_shift_bounds_cl_Suc_ivl) (simp add: atLeast0AtMost)
946   also have "... = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))"
947     using Suc by (intro setsum.mono_neutral_right) (auto simp: not_le binomial_eq_0)
948   also have "... = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc (n - i)))"
949     by (intro setsum.cong) (simp_all add: Suc_diff_le)
950   also have "... = b * pochhammer (a + (b + 1)) n"
951     by (subst Suc) (simp add: setsum_right_distrib mult_ac pochhammer_rec)
952   also have "a * pochhammer ((a + 1) + b) n + b * pochhammer (a + (b + 1)) n =
953                pochhammer (a + b) (Suc n)" by (simp add: pochhammer_rec algebra_simps)
954   finally show ?case ..
955 qed simp_all
958 text\<open>Contributed by Manuel Eberl, generalised by LCP.
959   Alternative definition of the binomial coefficient as @{term "\<Prod>i<k. (n - i) / (k - i)"}\<close>
960 lemma gbinomial_altdef_of_nat:
961   fixes k :: nat
962     and x :: "'a :: {field_char_0,field}"
963   shows "x gchoose k = (\<Prod>i<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
964 proof -
965   have "(x gchoose k) = (\<Prod>i<k. x - of_nat i) / of_nat (fact k)"
966     unfolding gbinomial_def
967     by (auto simp: gr0_conv_Suc lessThan_Suc_atMost atLeast0AtMost)
968   also have "\<dots> = (\<Prod>i<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
969     unfolding fact_eq_rev_setprod_nat of_nat_setprod
970     by (auto simp add: setprod_dividef intro!: setprod.cong of_nat_diff[symmetric])
971   finally show ?thesis .
972 qed
974 lemma gbinomial_ge_n_over_k_pow_k:
975   fixes k :: nat
976     and x :: "'a :: linordered_field"
977   assumes "of_nat k \<le> x"
978   shows "(x / of_nat k :: 'a) ^ k \<le> x gchoose k"
979 proof -
980   have x: "0 \<le> x"
981     using assms of_nat_0_le_iff order_trans by blast
982   have "(x / of_nat k :: 'a) ^ k = (\<Prod>i<k. x / of_nat k :: 'a)"
983     by (simp add: setprod_constant)
984   also have "\<dots> \<le> x gchoose k"
985     unfolding gbinomial_altdef_of_nat
986   proof (safe intro!: setprod_mono)
987     fix i :: nat
988     assume ik: "i < k"
989     from assms have "x * of_nat i \<ge> of_nat (i * k)"
990       by (metis mult.commute mult_le_cancel_right of_nat_less_0_iff of_nat_mult)
991     then have "x * of_nat k - x * of_nat i \<le> x * of_nat k - of_nat (i * k)" by arith
992     then have "x * of_nat (k - i) \<le> (x - of_nat i) * of_nat k"
993       using ik
994       by (simp add: algebra_simps zero_less_mult_iff of_nat_diff of_nat_mult)
995     then have "x * of_nat (k - i) \<le> (x - of_nat i) * (of_nat k :: 'a)"
996       unfolding of_nat_mult[symmetric] of_nat_le_iff .
997     with assms show "x / of_nat k \<le> (x - of_nat i) / (of_nat (k - i) :: 'a)"
998       using \<open>i < k\<close> by (simp add: field_simps)
999   qed (simp add: x zero_le_divide_iff)
1000   finally show ?thesis .
1001 qed
1003 lemma gbinomial_negated_upper: "(a gchoose b) = (-1) ^ b * ((of_nat b - a - 1) gchoose b)"
1004   by (simp add: gbinomial_pochhammer pochhammer_minus algebra_simps)
1006 lemma gbinomial_minus: "((-a) gchoose b) = (-1) ^ b * ((a + of_nat b - 1) gchoose b)"
1007   by (subst gbinomial_negated_upper) (simp add: add_ac)
1009 lemma Suc_times_gbinomial:
1010   "of_nat (Suc b) * ((a + 1) gchoose (Suc b)) = (a + 1) * (a gchoose b)"
1011 proof (cases b)
1012   case (Suc b)
1013   hence "((a + 1) gchoose (Suc (Suc b))) =
1014              (\<Prod>i = 0..Suc b. a + (1 - of_nat i)) / fact (b + 2)"
1015     by (simp add: field_simps gbinomial_def)
1016   also have "(\<Prod>i = 0..Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)"
1017     by (simp add: setprod_nat_ivl_1_Suc setprod_shift_bounds_cl_Suc_ivl)
1018   also have "... / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)"
1019     by (simp_all add: gbinomial_def setprod_nat_ivl_1_Suc setprod_shift_bounds_cl_Suc_ivl)
1020   finally show ?thesis by (simp add: Suc field_simps del: of_nat_Suc)
1021 qed simp
1023 lemma gbinomial_factors:
1024   "((a + 1) gchoose (Suc b)) = (a + 1) / of_nat (Suc b) * (a gchoose b)"
1025 proof (cases b)
1026   case (Suc b)
1027   hence "((a + 1) gchoose (Suc (Suc b))) =
1028              (\<Prod>i = 0..Suc b. a + (1 - of_nat i)) / fact (b + 2)"
1029     by (simp add: field_simps gbinomial_def)
1030   also have "(\<Prod>i = 0..Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)"
1031     by (simp add: setprod_nat_ivl_1_Suc setprod_shift_bounds_cl_Suc_ivl)
1032   also have "... / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)"
1033     by (simp_all add: gbinomial_def setprod_nat_ivl_1_Suc setprod_shift_bounds_cl_Suc_ivl)
1034   finally show ?thesis by (simp add: Suc)
1035 qed simp
1037 lemma gbinomial_rec: "((r + 1) gchoose (Suc k)) = (r gchoose k) * ((r + 1) / of_nat (Suc k))"
1038   using gbinomial_mult_1[of r k]
1039   by (subst gbinomial_Suc_Suc) (simp add: field_simps del: of_nat_Suc, simp add: algebra_simps)
1041 lemma gbinomial_of_nat_symmetric: "k \<le> n \<Longrightarrow> (of_nat n) gchoose k = (of_nat n) gchoose (n - k)"
1042   using binomial_symmetric[of k n] by (simp add: binomial_gbinomial [symmetric])
1045 text \<open>The absorption identity (equation 5.5 \cite[p.~157]{GKP}):$1046 {r \choose k} = \frac{r}{k}{r - 1 \choose k - 1},\quad \textnormal{integer } k \neq 0. 1047$\<close>
1048 lemma gbinomial_absorption':
1049   "k > 0 \<Longrightarrow> (r gchoose k) = (r / of_nat(k)) * (r - 1 gchoose (k - 1))"
1050   using gbinomial_rec[of "r - 1" "k - 1"]
1051   by (simp_all add: gbinomial_rec field_simps del: of_nat_Suc)
1053 text \<open>The absorption identity is written in the following form to avoid
1054 division by $k$ (the lower index) and therefore remove the $k \neq 0$
1055 restriction\cite[p.~157]{GKP}:$1056 k{r \choose k} = r{r - 1 \choose k - 1}, \quad \textnormal{integer } k. 1057$\<close>
1058 lemma gbinomial_absorption:
1059   "of_nat (Suc k) * (r gchoose Suc k) = r * ((r - 1) gchoose k)"
1060   using gbinomial_absorption'[of "Suc k" r] by (simp add: field_simps del: of_nat_Suc)
1062 text \<open>The absorption identity for natural number binomial coefficients:\<close>
1063 lemma binomial_absorption:
1064   "Suc k * (n choose Suc k) = n * ((n - 1) choose k)"
1065   by (cases n) (simp_all add: binomial_eq_0 Suc_times_binomial del: binomial_Suc_Suc mult_Suc)
1067 text \<open>The absorption companion identity for natural number coefficients,
1068 following the proof by GKP \cite[p.~157]{GKP}:\<close>
1069 lemma binomial_absorb_comp:
1070   "(n - k) * (n choose k) = n * ((n - 1) choose k)" (is "?lhs = ?rhs")
1071 proof (cases "n \<le> k")
1072   case False
1073   then have "?rhs = Suc ((n - 1) - k) * (n choose Suc ((n - 1) - k))"
1074     using binomial_symmetric[of k "n - 1"] binomial_absorption[of "(n - 1) - k" n]
1075     by simp
1076   also from False have "Suc ((n - 1) - k) = n - k" by simp
1077   also from False have "n choose \<dots> = n choose k" by (intro binomial_symmetric [symmetric]) simp_all
1078   finally show ?thesis ..
1079 qed auto
1081 text \<open>The generalised absorption companion identity:\<close>
1082 lemma gbinomial_absorb_comp: "(r - of_nat k) * (r gchoose k) = r * ((r - 1) gchoose k)"
1083   using pochhammer_absorb_comp[of r k] by (simp add: gbinomial_pochhammer)
1086   "r gchoose (Suc k) = ((r - 1) gchoose (Suc k)) + ((r - 1) gchoose k)"
1087   using gbinomial_Suc_Suc[of "r - 1" k] by (simp add: algebra_simps)
1090   "0 < n \<Longrightarrow> n choose (Suc k) = ((n - 1) choose (Suc k)) + ((n - 1) choose k)"
1091   by (subst choose_reduce_nat) simp_all
1094 text \<open>
1095   Equation 5.9 of the reference material \cite[p.~159]{GKP} is a useful
1096   summation formula, operating on both indices:$1097 \sum\limits_{k \leq n}{r + k \choose k} = {r + n + 1 \choose n}, 1098 \quad \textnormal{integer } n. 1099$
1100 \<close>
1101 lemma gbinomial_parallel_sum:
1102 "(\<Sum>k\<le>n. (r + of_nat k) gchoose k) = (r + of_nat n + 1) gchoose n"
1103 proof (induction n)
1104   case (Suc m)
1105   thus ?case using gbinomial_Suc_Suc[of "(r + of_nat m + 1)" m] by (simp add: add_ac)
1106 qed auto
1108 subsection \<open>Summation on the upper index\<close>
1109 text \<open>
1110   Another summation formula is equation 5.10 of the reference material \cite[p.~160]{GKP},
1111   aptly named \emph{summation on the upper index}:$\sum_{0 \leq k \leq n} {k \choose m} = 1112 {n + 1 \choose m + 1}, \quad \textnormal{integers } m, n \geq 0.$
1113 \<close>
1114 lemma gbinomial_sum_up_index:
1115   "(\<Sum>k = 0..n. (of_nat k gchoose m) :: 'a :: field_char_0) = (of_nat n + 1) gchoose (m + 1)"
1116 proof (induction n)
1117   case 0
1118   show ?case using gbinomial_Suc_Suc[of 0 m] by (cases m) auto
1119 next
1120   case (Suc n)
1121   thus ?case using gbinomial_Suc_Suc[of "of_nat (Suc n) :: 'a" m] by (simp add: add_ac)
1122 qed
1124 lemma gbinomial_index_swap:
1125   "((-1) ^ m) * ((- (of_nat n) - 1) gchoose m) = ((-1) ^ n) * ((- (of_nat m) - 1) gchoose n)"
1126   (is "?lhs = ?rhs")
1127 proof -
1128   have "?lhs = (of_nat (m + n) gchoose m)"
1129     by (subst gbinomial_negated_upper) (simp add: power_mult_distrib [symmetric])
1130   also have "\<dots> = (of_nat (m + n) gchoose n)" by (subst gbinomial_of_nat_symmetric) simp_all
1131   also have "\<dots> = ?rhs" by (subst gbinomial_negated_upper) simp
1132   finally show ?thesis .
1133 qed
1135 lemma gbinomial_sum_lower_neg:
1136   "(\<Sum>k\<le>m. (r gchoose k) * (- 1) ^ k) = (- 1) ^ m * (r - 1 gchoose m)" (is "?lhs = ?rhs")
1137 proof -
1138   have "?lhs = (\<Sum>k\<le>m. -(r + 1) + of_nat k gchoose k)"
1139     by (intro setsum.cong[OF refl]) (subst gbinomial_negated_upper, simp add: power_mult_distrib)
1140   also have "\<dots>  = -r + of_nat m gchoose m" by (subst gbinomial_parallel_sum) simp
1141   also have "\<dots> = ?rhs" by (subst gbinomial_negated_upper) (simp add: power_mult_distrib)
1142   finally show ?thesis .
1143 qed
1145 lemma gbinomial_partial_row_sum:
1146 "(\<Sum>k\<le>m. (r gchoose k) * ((r / 2) - of_nat k)) = ((of_nat m + 1)/2) * (r gchoose (m + 1))"
1147 proof (induction m)
1148   case (Suc mm)
1149   hence "(\<Sum>k\<le>Suc mm. (r gchoose k) * (r / 2 - of_nat k)) =
1150              (r - of_nat (Suc mm)) * (r gchoose Suc mm) / 2" by (simp add: field_simps)
1151   also have "\<dots> = r * (r - 1 gchoose Suc mm) / 2" by (subst gbinomial_absorb_comp) (rule refl)
1152   also have "\<dots> = (of_nat (Suc mm) + 1) / 2 * (r gchoose (Suc mm + 1))"
1153     by (subst gbinomial_absorption [symmetric]) simp
1154   finally show ?case .
1155 qed simp_all
1157 lemma setsum_bounds_lt_plus1: "(\<Sum>k<mm. f (Suc k)) = (\<Sum>k=1..mm. f k)"
1158   by (induction mm) simp_all
1160 lemma gbinomial_partial_sum_poly:
1161   "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) =
1162        (\<Sum>k\<le>m. (-r gchoose k) * (-x)^k * (x + y)^(m-k))" (is "?lhs m = ?rhs m")
1163 proof (induction m)
1164   case (Suc mm)
1165   def G \<equiv> "\<lambda>i k. (of_nat i + r gchoose k) * x^k * y^(i-k)" and S \<equiv> ?lhs
1166   have SG_def: "S = (\<lambda>i. (\<Sum>k\<le>i. (G i k)))" unfolding S_def G_def ..
1168   have "S (Suc mm) = G (Suc mm) 0 + (\<Sum>k=Suc 0..Suc mm. G (Suc mm) k)"
1169     using SG_def by (simp add: setsum_head_Suc atLeast0AtMost [symmetric])
1170   also have "(\<Sum>k=Suc 0..Suc mm. G (Suc mm) k) = (\<Sum>k=0..mm. G (Suc mm) (Suc k))"
1171     by (subst setsum_shift_bounds_cl_Suc_ivl) simp
1172   also have "\<dots> = (\<Sum>k=0..mm. ((of_nat mm + r gchoose (Suc k))
1173                     + (of_nat mm + r gchoose k)) * x^(Suc k) * y^(mm - k))"
1174     unfolding G_def by (subst gbinomial_addition_formula) simp
1175   also have "\<dots> = (\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k))
1176                   + (\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k))"
1177     by (subst setsum.distrib [symmetric]) (simp add: algebra_simps)
1178   also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) =
1179                (\<Sum>k<Suc mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k))"
1180     by (simp only: atLeast0AtMost lessThan_Suc_atMost)
1181   also have "\<dots> = (\<Sum>k<mm. (of_nat mm + r gchoose Suc k) * x^(Suc k) * y^(mm-k))
1182                       + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)" (is "_ = ?A + ?B")
1183     by (subst setsum_lessThan_Suc) simp
1184   also have "?A = (\<Sum>k=1..mm. (of_nat mm + r gchoose k) * x^k * y^(mm - k + 1))"
1185   proof (subst setsum_bounds_lt_plus1 [symmetric], intro setsum.cong[OF refl], clarify)
1186     fix k assume "k < mm"
1187     hence "mm - k = mm - Suc k + 1" by linarith
1188     thus "(of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - k) =
1189             (of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - Suc k + 1)" by (simp only:)
1190   qed
1191   also have "\<dots> + ?B = y * (\<Sum>k=1..mm. (G mm k)) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"
1192     unfolding G_def by (subst setsum_right_distrib) (simp add: algebra_simps)
1193   also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k)) = x * (S mm)"
1194       unfolding S_def by (subst setsum_right_distrib) (simp add: atLeast0AtMost algebra_simps)
1195   also have "(G (Suc mm) 0) = y * (G mm 0)" by (simp add: G_def)
1196   finally have "S (Suc mm) = y * ((G mm 0) + (\<Sum>k=1..mm. (G mm k)))
1197                 + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm) + x * (S mm)"
1198     by (simp add: ring_distribs)
1199   also have "(G mm 0) + (\<Sum>k=1..mm. (G mm k)) = S mm"
1200     by (simp add: setsum_head_Suc[symmetric] SG_def atLeast0AtMost)
1201   finally have "S (Suc mm) = (x + y) * (S mm) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"
1202     by (simp add: algebra_simps)
1203   also have "(of_nat mm + r gchoose (Suc mm)) = (-1) ^ (Suc mm) * (-r gchoose (Suc mm))"
1204     by (subst gbinomial_negated_upper) simp
1205   also have "(-1) ^ Suc mm * (- r gchoose Suc mm) * x ^ Suc mm =
1206                  (-r gchoose (Suc mm)) * (-x) ^ Suc mm" by (simp add: power_minus[of x])
1207   also have "(x + y) * S mm + \<dots> = (x + y) * ?rhs mm + (-r gchoose (Suc mm)) * (-x)^Suc mm"
1208     unfolding S_def by (subst Suc.IH) simp
1209   also have "(x + y) * ?rhs mm = (\<Sum>n\<le>mm. ((- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n)))"
1210     by (subst setsum_right_distrib, rule setsum.cong) (simp_all add: Suc_diff_le)
1211   also have "\<dots> + (-r gchoose (Suc mm)) * (-x)^Suc mm =
1212                  (\<Sum>n\<le>Suc mm. (- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n))" by simp
1213   finally show ?case unfolding S_def .
1214 qed simp_all
1216 lemma gbinomial_partial_sum_poly_xpos:
1217   "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) =
1218      (\<Sum>k\<le>m. (of_nat k + r - 1 gchoose k) * x^k * (x + y)^(m-k))"
1219   apply (subst gbinomial_partial_sum_poly)
1220   apply (subst gbinomial_negated_upper)
1221   apply (intro setsum.cong, rule refl)
1222   apply (simp add: power_mult_distrib [symmetric])
1223   done
1225 lemma setsum_nat_symmetry:
1226   "(\<Sum>k = 0..(m::nat). f k) = (\<Sum>k = 0..m. f (m - k))"
1227   by (rule setsum.reindex_bij_witness[where i="\<lambda>i. m - i" and j="\<lambda>i. m - i"]) auto
1229 lemma binomial_r_part_sum: "(\<Sum>k\<le>m. (2 * m + 1 choose k)) = 2 ^ (2 * m)"
1230 proof -
1231   have "2 * 2^(2*m) = (\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k))"
1232     using choose_row_sum[where n="2 * m + 1"] by simp
1233   also have "(\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k)) = (\<Sum>k = 0..m. (2 * m + 1 choose k))
1234                 + (\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k))"
1235     using setsum_ub_add_nat[of 0 m "\<lambda>k. 2 * m + 1 choose k" "m+1"] by (simp add: mult_2)
1236   also have "(\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k)) =
1237                  (\<Sum>k = 0..m. (2 * m + 1 choose (k + (m + 1))))"
1238     by (subst setsum_shift_bounds_cl_nat_ivl [symmetric]) (simp add: mult_2)
1239   also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose (m - k)))"
1240     by (intro setsum.cong[OF refl], subst binomial_symmetric) simp_all
1241   also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose k))"
1242     by (subst (2) setsum_nat_symmetry) (rule refl)
1243   also have "\<dots> + \<dots> = 2 * \<dots>" by simp
1244   finally show ?thesis by (subst (asm) mult_cancel1) (simp add: atLeast0AtMost)
1245 qed
1247 lemma gbinomial_r_part_sum:
1248   "(\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k)) = 2 ^ (2 * m)" (is "?lhs = ?rhs")
1249 proof -
1250   have "?lhs = of_nat (\<Sum>k\<le>m. (2 * m + 1) choose k)"
1251     by (simp add: binomial_gbinomial of_nat_mult add_ac of_nat_setsum)
1252   also have "\<dots> = of_nat (2 ^ (2 * m))" by (subst binomial_r_part_sum) (rule refl)
1253   finally show ?thesis by (simp add: of_nat_power)
1254 qed
1256 lemma gbinomial_sum_nat_pow2:
1257    "(\<Sum>k\<le>m. (of_nat (m + k) gchoose k :: 'a :: field_char_0) / 2 ^ k) = 2 ^ m" (is "?lhs = ?rhs")
1258 proof -
1259   have "2 ^ m * 2 ^ m = (2 ^ (2*m) :: 'a)" by (induction m) simp_all
1260   also have "\<dots> = (\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k))" using gbinomial_r_part_sum ..
1261   also have "\<dots> = (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) * 2 ^ (m - k))"
1262     using gbinomial_partial_sum_poly_xpos[where x="1" and y="1" and r="of_nat m + 1" and m="m"]
1264   also have "\<dots> = 2 ^ m * (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) / 2 ^ k)"
1265     by (subst setsum_right_distrib) (simp add: algebra_simps power_diff)
1266   finally show ?thesis by (subst (asm) mult_left_cancel) simp_all
1267 qed
1269 lemma gbinomial_trinomial_revision:
1270   assumes "k \<le> m"
1271   shows   "(r gchoose m) * (of_nat m gchoose k) = (r gchoose k) * (r - of_nat k gchoose (m - k))"
1272 proof -
1273   have "(r gchoose m) * (of_nat m gchoose k) =
1274             (r gchoose m) * fact m / (fact k * fact (m - k))"
1275     using assms by (simp add: binomial_gbinomial [symmetric] binomial_fact)
1276   also have "\<dots> = (r gchoose k) * (r - of_nat k gchoose (m - k))" using assms
1277     by (simp add: gbinomial_pochhammer power_diff pochhammer_product)
1278   finally show ?thesis .
1279 qed
1282 text\<open>Versions of the theorems above for the natural-number version of "choose"\<close>
1283 lemma binomial_altdef_of_nat:
1284   fixes n k :: nat
1285     and x :: "'a :: {field_char_0,field}"  \<comment>\<open>the point is to constrain @{typ 'a}\<close>
1286   assumes "k \<le> n"
1287   shows "of_nat (n choose k) = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
1288 using assms
1289 by (simp add: gbinomial_altdef_of_nat binomial_gbinomial of_nat_diff)
1291 lemma binomial_ge_n_over_k_pow_k:
1292   fixes k n :: nat
1293     and x :: "'a :: linordered_field"
1294   assumes "k \<le> n"
1295   shows "(of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)"
1296 by (simp add: assms gbinomial_ge_n_over_k_pow_k binomial_gbinomial of_nat_diff)
1298 lemma binomial_le_pow:
1299   assumes "r \<le> n"
1300   shows "n choose r \<le> n ^ r"
1301 proof -
1302   have "n choose r \<le> fact n div fact (n - r)"
1303     using \<open>r \<le> n\<close> by (subst binomial_fact_lemma[symmetric]) auto
1304   with fact_div_fact_le_pow [OF assms] show ?thesis by auto
1305 qed
1307 lemma binomial_altdef_nat: "(k::nat) \<le> n \<Longrightarrow>
1308     n choose k = fact n div (fact k * fact (n - k))"
1309  by (subst binomial_fact_lemma [symmetric]) auto
1311 lemma choose_dvd: "k \<le> n \<Longrightarrow> fact k * fact (n - k) dvd (fact n :: 'a :: {semiring_div,linordered_semidom})"
1312   unfolding dvd_def
1313   apply (rule exI [where x="of_nat (n choose k)"])
1314   using binomial_fact_lemma [of k n, THEN arg_cong [where f = of_nat and 'b='a]]
1315   apply (auto simp: of_nat_mult)
1316   done
1318 lemma fact_fact_dvd_fact:
1319     "fact k * fact n dvd (fact (k+n) :: 'a :: {semiring_div,linordered_semidom})"
1322 lemma choose_mult_lemma:
1323      "((m+r+k) choose (m+k)) * ((m+k) choose k) = ((m+r+k) choose k) * ((m+r) choose m)"
1324 proof -
1325   have "((m+r+k) choose (m+k)) * ((m+k) choose k) =
1326         fact (m+r + k) div (fact (m + k) * fact (m+r - m)) * (fact (m + k) div (fact k * fact m))"
1327     by (simp add: assms binomial_altdef_nat)
1328   also have "... = fact (m+r+k) div (fact r * (fact k * fact m))"
1329     apply (subst div_mult_div_if_dvd)
1330     apply (auto simp: algebra_simps fact_fact_dvd_fact)
1332     done
1333   also have "... = (fact (m+r+k) * fact (m+r)) div (fact r * (fact k * fact m) * fact (m+r))"
1334     apply (subst div_mult_div_if_dvd [symmetric])
1335     apply (auto simp add: algebra_simps)
1336     apply (metis fact_fact_dvd_fact dvd_trans nat_mult_dvd_cancel_disj)
1337     done
1338   also have "... = (fact (m+r+k) div (fact k * fact (m+r)) * (fact (m+r) div (fact r * fact m)))"
1339     apply (subst div_mult_div_if_dvd)
1340     apply (auto simp: fact_fact_dvd_fact algebra_simps)
1341     done
1342   finally show ?thesis
1343     by (simp add: binomial_altdef_nat mult.commute)
1344 qed
1346 text\<open>The "Subset of a Subset" identity\<close>
1347 lemma choose_mult:
1348   assumes "k\<le>m" "m\<le>n"
1349     shows "(n choose m) * (m choose k) = (n choose k) * ((n-k) choose (m-k))"
1350 using assms choose_mult_lemma [of "m-k" "n-m" k]
1351 by simp
1354 subsection \<open>Binomial coefficients\<close>
1356 lemma choose_one: "(n::nat) choose 1 = n"
1357   by simp
1359 (*FIXME: messy and apparently unused*)
1360 lemma binomial_induct [rule_format]: "(ALL (n::nat). P n n) \<longrightarrow>
1361     (ALL n. P (Suc n) 0) \<longrightarrow> (ALL n. (ALL k < n. P n k \<longrightarrow> P n (Suc k) \<longrightarrow>
1362     P (Suc n) (Suc k))) \<longrightarrow> (ALL k <= n. P n k)"
1363   apply (induct n)
1364   apply auto
1365   apply (case_tac "k = 0")
1366   apply auto
1367   apply (case_tac "k = Suc n")
1368   apply auto
1369   apply (metis Suc_le_eq fact.cases le_Suc_eq le_eq_less_or_eq)
1370   done
1372 lemma card_UNION:
1373   assumes "finite A" and "\<forall>k \<in> A. finite k"
1374   shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))"
1375   (is "?lhs = ?rhs")
1376 proof -
1377   have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))" by simp
1378   also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))" (is "_ = nat ?rhs")
1379     by(subst setsum_right_distrib) simp
1380   also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))"
1381     using assms by(subst setsum.Sigma)(auto)
1382   also have "\<dots> = (\<Sum>(x, I)\<in>(SIGMA x:UNIV. {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}). (- 1) ^ (card I + 1))"
1383     by (rule setsum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)
1384   also have "\<dots> = (\<Sum>(x, I)\<in>(SIGMA x:\<Union>A. {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}). (- 1) ^ (card I + 1))"
1385     using assms by(auto intro!: setsum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])
1386   also have "\<dots> = (\<Sum>x\<in>\<Union>A. (\<Sum>I|I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I. (- 1) ^ (card I + 1)))"
1387     using assms by(subst setsum.Sigma) auto
1388   also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "setsum ?lhs _ = _")
1389   proof(rule setsum.cong[OF refl])
1390     fix x
1391     assume x: "x \<in> \<Union>A"
1392     def K \<equiv> "{X \<in> A. x \<in> X}"
1393     with \<open>finite A\<close> have K: "finite K" by auto
1394     let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I}"
1395     have "inj_on snd (SIGMA i:{1..card A}. ?I i)"
1396       using assms by(auto intro!: inj_onI)
1397     moreover have [symmetric]: "snd ` (SIGMA i:{1..card A}. ?I i) = {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}"
1398       using assms by(auto intro!: rev_image_eqI[where x="(card a, a)" for a]
1399         simp add: card_gt_0_iff[folded Suc_le_eq]
1400         dest: finite_subset intro: card_mono)
1401     ultimately have "?lhs x = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))"
1402       by (rule setsum.reindex_cong [where l = snd]) fastforce
1403     also have "\<dots> = (\<Sum>i=1..card A. (\<Sum>I|I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. (- 1) ^ (i + 1)))"
1404       using assms by(subst setsum.Sigma) auto
1405     also have "\<dots> = (\<Sum>i=1..card A. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1))"
1406       by(subst setsum_right_distrib) simp
1407     also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))" (is "_ = ?rhs")
1408     proof(rule setsum.mono_neutral_cong_right[rule_format])
1409       show "{1..card K} \<subseteq> {1..card A}" using \<open>finite A\<close>
1410         by(auto simp add: K_def intro: card_mono)
1411     next
1412       fix i
1413       assume "i \<in> {1..card A} - {1..card K}"
1414       hence i: "i \<le> card A" "card K < i" by auto
1415       have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}"
1416         by(auto simp add: K_def)
1417       also have "\<dots> = {}" using \<open>finite A\<close> i
1418         by(auto simp add: K_def dest: card_mono[rotated 1])
1419       finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0"
1420         by(simp only:) simp
1421     next
1422       fix i
1423       have "(\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1) = (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)"
1424         (is "?lhs = ?rhs")
1425         by(rule setsum.cong)(auto simp add: K_def)
1426       thus "(- 1) ^ (i + 1) * ?lhs = (- 1) ^ (i + 1) * ?rhs" by simp
1427     qed simp
1428     also have "{I. I \<subseteq> K \<and> card I = 0} = {{}}" using assms
1429       by(auto simp add: card_eq_0_iff K_def dest: finite_subset)
1430     hence "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1"
1431       by(subst (2) setsum_head_Suc)(simp_all )
1432     also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1"
1433       using K by(subst n_subsets[symmetric]) simp_all
1434     also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1"
1435       by(subst setsum_right_distrib[symmetric]) simp
1436     also have "\<dots> =  - ((-1 + 1) ^ card K) + 1"
1437       by(subst binomial_ring)(simp add: ac_simps)
1438     also have "\<dots> = 1" using x K by(auto simp add: K_def card_gt_0_iff)
1439     finally show "?lhs x = 1" .
1440   qed
1441   also have "nat \<dots> = card (\<Union>A)" by simp
1442   finally show ?thesis ..
1443 qed
1445 text\<open>The number of nat lists of length \<open>m\<close> summing to \<open>N\<close> is
1446 @{term "(N + m - 1) choose N"}:\<close>
1448 lemma card_length_listsum_rec:
1449   assumes "m\<ge>1"
1450   shows "card {l::nat list. length l = m \<and> listsum l = N} =
1451     (card {l. length l = (m - 1) \<and> listsum l = N} +
1452     card {l. length l = m \<and> listsum l + 1 =  N})"
1453     (is "card ?C = (card ?A + card ?B)")
1454 proof -
1455   let ?A'="{l. length l = m \<and> listsum l = N \<and> hd l = 0}"
1456   let ?B'="{l. length l = m \<and> listsum l = N \<and> hd l \<noteq> 0}"
1457   let ?f ="\<lambda> l. 0#l"
1458   let ?g ="\<lambda> l. (hd l + 1) # tl l"
1459   have 1: "\<And>xs x. xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" by simp
1460   have 2: "\<And>xs. (xs::nat list) \<noteq> [] \<Longrightarrow> listsum(tl xs) = listsum xs - hd xs"
1461     by(auto simp add: neq_Nil_conv)
1462   have f: "bij_betw ?f ?A ?A'"
1463     apply(rule bij_betw_byWitness[where f' = tl])
1464     using assms
1465     by (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv)
1466   have 3: "\<And>xs:: nat list. xs \<noteq> [] \<Longrightarrow> hd xs + (listsum xs - hd xs) = listsum xs"
1467     by (metis 1 listsum_simps(2) 2)
1468   have g: "bij_betw ?g ?B ?B'"
1469     apply(rule bij_betw_byWitness[where f' = "\<lambda> l. (hd l - 1) # tl l"])
1470     using assms
1471     by (auto simp: 2 length_0_conv[symmetric] intro!: 3
1472       simp del: length_greater_0_conv length_0_conv)
1473   { fix M N :: nat have "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}"
1474     using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto }
1475     note fin = this
1476   have fin_A: "finite ?A" using fin[of _ "N+1"]
1477     by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"],
1478       auto simp: member_le_listsum_nat less_Suc_eq_le)
1479   have fin_B: "finite ?B"
1480     by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"],
1481       auto simp: member_le_listsum_nat less_Suc_eq_le fin)
1482   have uni: "?C = ?A' \<union> ?B'" by auto
1483   have disj: "?A' \<inter> ?B' = {}" by auto
1484   have "card ?C = card(?A' \<union> ?B')" using uni by simp
1485   also have "\<dots> = card ?A + card ?B"
1486     using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g]
1487       bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B
1488     by presburger
1489   finally show ?thesis .
1490 qed
1492 lemma card_length_listsum: \<comment>"By Holden Lee, tidied by Tobias Nipkow"
1493   "card {l::nat list. size l = m \<and> listsum l = N} = (N + m - 1) choose N"
1494 proof (cases m)
1495   case 0 then show ?thesis
1496     by (cases N) (auto simp: cong: conj_cong)
1497 next
1498   case (Suc m')
1499     have m: "m\<ge>1" by (simp add: Suc)
1500     then show ?thesis
1501     proof (induct "N + m - 1" arbitrary: N m)
1502       case 0   \<comment> "In the base case, the only solution is ."
1503       have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {}"
1504         by (auto simp: length_Suc_conv)
1505       have "m=1 \<and> N=0" using 0 by linarith
1506       then show ?case by simp
1507     next
1508       case (Suc k)
1510       have c1: "card {l::nat list. size l = (m - 1) \<and> listsum l =  N} =
1511         (N + (m - 1) - 1) choose N"
1512       proof cases
1513         assume "m = 1"
1514         with Suc.hyps have "N\<ge>1" by auto
1515         with \<open>m = 1\<close> show ?thesis by (simp add: binomial_eq_0)
1516       next
1517         assume "m \<noteq> 1" thus ?thesis using Suc by fastforce
1518       qed
1520       from Suc have c2: "card {l::nat list. size l = m \<and> listsum l + 1 = N} =
1521         (if N>0 then ((N - 1) + m - 1) choose (N - 1) else 0)"
1522       proof -
1523         have aux: "\<And>m n. n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" by arith
1524         from Suc have "N>0 \<Longrightarrow>
1525           card {l::nat list. size l = m \<and> listsum l + 1 = N} =
1526           ((N - 1) + m - 1) choose (N - 1)" by (simp add: aux)
1527         thus ?thesis by auto
1528       qed
1530       from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> listsum l = N} +
1531           card {l::nat list. size l = m \<and> listsum l + 1 = N}) = (N + m - 1) choose N"
1532         by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def)
1533       thus ?case using card_length_listsum_rec[OF Suc.prems] by auto
1534     qed
1535 qed
1538 lemma Suc_times_binomial_add: \<comment> \<open>by Lukas Bulwahn\<close>
1539   "Suc a * (Suc (a + b) choose Suc a) = Suc b * (Suc (a + b) choose a)"
1540 proof -
1541   have dvd: "Suc a * (fact a * fact b) dvd fact (Suc (a + b))" for a b
1542     using fact_fact_dvd_fact[of "Suc a" "b", where 'a=nat]
1543     by (simp only: fact_Suc add_Suc[symmetric] of_nat_id mult.assoc)
1545   have "Suc a * (fact (Suc (a + b)) div (Suc a * fact a * fact b)) =
1546       Suc a * fact (Suc (a + b)) div (Suc a * (fact a * fact b))"
1547     by (subst div_mult_swap[symmetric]; simp only: mult.assoc dvd)
1548   also have "\<dots> = Suc b * fact (Suc (a + b)) div (Suc b * (fact a * fact b))"
1549     by (simp only: div_mult_mult1)
1550   also have "\<dots> = Suc b * (fact (Suc (a + b)) div (Suc b * (fact a * fact b)))"
1551     using dvd[of b a] by (subst div_mult_swap[symmetric]; simp only: ac_simps dvd)
1552   finally show ?thesis
1553     by (subst (1 2) binomial_altdef_nat)
1554        (simp_all only: ac_simps diff_Suc_Suc Suc_diff_le diff_add_inverse fact_Suc of_nat_id)
1555 qed
1559 lemma fact_code [code]:
1560   "fact n = (of_nat (fold_atLeastAtMost_nat (op *) 2 n 1) :: 'a :: semiring_char_0)"
1561 proof -
1562   have "fact n = (of_nat (\<Prod>{1..n}) :: 'a)" by (simp add: fact_altdef')
1563   also have "\<Prod>{1..n} = \<Prod>{2..n}"
1564     by (intro setprod.mono_neutral_right) auto
1565   also have "\<dots> = fold_atLeastAtMost_nat (op *) 2 n 1"
1566     by (simp add: setprod_atLeastAtMost_code)
1567   finally show ?thesis .
1568 qed
1570 lemma pochhammer_code [code]:
1571   "pochhammer a n = (if n = 0 then 1 else
1572        fold_atLeastAtMost_nat (\<lambda>n acc. (a + of_nat n) * acc) 0 (n - 1) 1)"
1573   by (simp add: setprod_atLeastAtMost_code pochhammer_def)
1575 lemma gbinomial_code [code]:
1576   "a gchoose n = (if n = 0 then 1 else
1577      fold_atLeastAtMost_nat (\<lambda>n acc. (a - of_nat n) * acc) 0 (n - 1) 1 / fact n)"
1578   by (simp add: setprod_atLeastAtMost_code gbinomial_def)
1580 (*TODO: This code equation breaks Scala code generation in HOL-Codegenerator_Test. We have to figure out why and how to prevent that. *)
1582 (*
1583 lemma binomial_code [code]:
1584   "(n choose k) =
1585       (if k > n then 0
1586        else if 2 * k > n then (n choose (n - k))
1587        else (fold_atLeastAtMost_nat (op * ) (n-k+1) n 1 div fact k))"
1588 proof -
1589   {
1590     assume "k \<le> n"
1591     hence "{1..n} = {1..n-k} \<union> {n-k+1..n}" by auto
1592     hence "(fact n :: nat) = fact (n-k) * \<Prod>{n-k+1..n}"
1593       by (simp add: setprod.union_disjoint fact_altdef_nat)
1594   }
1595   thus ?thesis by (auto simp: binomial_altdef_nat mult_ac setprod_atLeastAtMost_code)
1596 qed
1597 *)
1599 end