Theory Binomial

theory Binomial
imports Cong Complex_Main
(*  Title:      HOL/Number_Theory/Binomial.thy
    Authors:    Lawrence C. Paulson, Jeremy Avigad, Tobias Nipkow

Defines the "choose" function, and establishes basic properties.
*)

header {* Binomial *}

theory Binomial
imports Cong Fact Complex_Main
begin


text {* This development is based on the work of Andy Gordon and
  Florian Kammueller. *}

subsection {* Basic definitions and lemmas *}

primrec binomial :: "nat => nat => nat" (infixl "choose" 65)
where
  "0 choose k = (if k = 0 then 1 else 0)"
| "Suc n choose k = (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"

lemma binomial_n_0 [simp]: "(n choose 0) = 1"
  by (cases n) simp_all

lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
  by simp

lemma binomial_Suc_Suc [simp]: "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
  by simp

lemma choose_reduce_nat: 
  "0 < (n::nat) ==> 0 < k ==>
    (n choose k) = ((n - 1) choose k) + ((n - 1) choose (k - 1))"
  by (metis Suc_diff_1 binomial.simps(2) add.commute neq0_conv)

lemma binomial_eq_0: "n < k ==> n choose k = 0"
  by (induct n arbitrary: k) auto

declare binomial.simps [simp del]

lemma binomial_n_n [simp]: "n choose n = 1"
  by (induct n) (simp_all add: binomial_eq_0)

lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n"
  by (induct n) simp_all

lemma binomial_1 [simp]: "n choose Suc 0 = n"
  by (induct n) simp_all

lemma zero_less_binomial: "k ≤ n ==> n choose k > 0"
  by (induct n k rule: diff_induct) simp_all

lemma binomial_eq_0_iff [simp]: "n choose k = 0 <-> n < k"
  by (metis binomial_eq_0 less_numeral_extra(3) not_less zero_less_binomial)

lemma zero_less_binomial_iff [simp]: "n choose k > 0 <-> k ≤ n"
  by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)

(*Might be more useful if re-oriented*)
lemma Suc_times_binomial_eq:
  "k ≤ n ==> Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
  apply (induct n arbitrary: k)
   apply (simp add: binomial.simps)
   apply (case_tac k)
  apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0)
  done

text{*This is the well-known version, but it's harder to use because of the
  need to reason about division.*}
lemma binomial_Suc_Suc_eq_times:
    "k ≤ n ==> (Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
  by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)

text{*Another version, with -1 instead of Suc.*}
lemma times_binomial_minus1_eq:
  "k ≤ n ==> 0 < k ==> (n choose k) * k = n * ((n - 1) choose (k - 1))"
  using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"]
  by (auto split add: nat_diff_split)


subsection {* Combinatorial theorems involving @{text "choose"} *}

text {*By Florian Kamm\"uller, tidied by LCP.*}

lemma card_s_0_eq_empty: "finite A ==> card {B. B ⊆ A & card B = 0} = 1"
  by (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])

lemma choose_deconstruct: "finite M ==> x ∉ M ==>
    {s. s ⊆ insert x M ∧ card s = Suc k} =
    {s. s ⊆ M ∧ card s = Suc k} ∪ {s. ∃t. t ⊆ M ∧ card t = k ∧ s = insert x t}"
  apply safe
     apply (auto intro: finite_subset [THEN card_insert_disjoint])
  by (metis (full_types) Diff_insert_absorb Set.set_insert Zero_neq_Suc card_Diff_singleton_if 
     card_eq_0_iff diff_Suc_1 in_mono subset_insert_iff)

lemma finite_bex_subset [simp]:
  assumes "finite B"
    and "!!A. A ⊆ B ==> finite {x. P x A}"
  shows "finite {x. ∃A ⊆ B. P x A}"
  by (metis (no_types) assms finite_Collect_bounded_ex finite_Collect_subsets)

text{*There are as many subsets of @{term A} having cardinality @{term k}
 as there are sets obtained from the former by inserting a fixed element
 @{term x} into each.*}
lemma constr_bij:
   "finite A ==> x ∉ A ==>
    card {B. ∃C. C ⊆ A ∧ card C = k ∧ B = insert x C} =
    card {B. B ⊆ A & card(B) = k}"
  apply (rule card_bij_eq [where f = "λs. s - {x}" and g = "insert x"])
  apply (auto elim!: equalityE simp add: inj_on_def)
  apply (metis card_Diff_singleton_if finite_subset in_mono)
  done

text {*
  Main theorem: combinatorial statement about number of subsets of a set.
*}

theorem n_subsets: "finite A ==> card {B. B ⊆ A ∧ card B = k} = (card A choose k)"
proof (induct k arbitrary: A)
  case 0 then show ?case by (simp add: card_s_0_eq_empty)
next
  case (Suc k)
  show ?case using `finite A`
  proof (induct A)
    case empty show ?case by (simp add: card_s_0_eq_empty)
  next
    case (insert x A)
    then show ?case using Suc.hyps
      apply (simp add: card_s_0_eq_empty choose_deconstruct)
      apply (subst card_Un_disjoint)
         prefer 4 apply (force simp add: constr_bij)
        prefer 3 apply force
       prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
         finite_subset [of _ "Pow (insert x F)" for F])
      apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
      done
  qed
qed


subsection {* The binomial theorem (courtesy of Tobias Nipkow): *}

text{* Avigad's version, generalized to any commutative ring *}
theorem binomial_ring: "(a+b::'a::{comm_ring_1,power})^n = 
  (∑k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))" (is "?P n")
proof (induct n)
  case 0 then show "?P 0" by simp
next
  case (Suc n)
  have decomp: "{0..n+1} = {0} Un {n+1} Un {1..n}"
    by auto
  have decomp2: "{0..n} = {0} Un {1..n}"
    by auto
  have "(a+b)^(n+1) = 
      (a+b) * (∑k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
    using Suc.hyps by simp
  also have "… = a*(∑k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +
                   b*(∑k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
    by (rule distrib)
  also have "… = (∑k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +
                  (∑k=0..n. of_nat (n choose k) * a^k * b^(n-k+1))"
    by (auto simp add: setsum_right_distrib ac_simps)
  also have "… = (∑k=0..n. of_nat (n choose k) * a^k * b^(n+1-k)) +
                  (∑k=1..n+1. of_nat (n choose (k - 1)) * a^k * b^(n+1-k))"
    by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le field_simps  
        del:setsum_cl_ivl_Suc)
  also have "… = a^(n+1) + b^(n+1) +
                  (∑k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n+1-k)) +
                  (∑k=1..n. of_nat (n choose k) * a^k * b^(n+1-k))"
    by (simp add: decomp2)
  also have
      "… = a^(n+1) + b^(n+1) + 
            (∑k=1..n. of_nat(n+1 choose k) * a^k * b^(n+1-k))"
    by (auto simp add: field_simps setsum.distrib [symmetric] choose_reduce_nat)
  also have "… = (∑k=0..n+1. of_nat (n+1 choose k) * a^k * b^(n+1-k))"
    using decomp by (simp add: field_simps)
  finally show "?P (Suc n)" by simp
qed

text{* Original version for the naturals *}
corollary binomial: "(a+b::nat)^n = (∑k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))"
    using binomial_ring [of "int a" "int b" n]
  by (simp only: of_nat_add [symmetric] of_nat_mult [symmetric] of_nat_power [symmetric]
           of_nat_setsum [symmetric]
           of_nat_eq_iff of_nat_id)

lemma choose_row_sum: "(∑k=0..n. n choose k) = 2^n"
  using binomial [of 1 "1" n]
  by (simp add: numeral_2_eq_2)

lemma sum_choose_lower: "(∑k=0..n. (r+k) choose k) = Suc (r+n) choose n"
  by (induct n) auto

lemma sum_choose_upper: "(∑k=0..n. k choose m) = Suc n choose Suc m"
  by (induct n) auto

lemma natsum_reverse_index:
  fixes m::nat
  shows "(!!k. m ≤ k ==> k ≤ n ==> g k = f (m + n - k)) ==> (∑k=m..n. f k) = (∑k=m..n. g k)"
  by (rule setsum.reindex_bij_witness[where i="λk. m+n-k" and j="λk. m+n-k"]) auto

lemma sum_choose_diagonal:
  assumes "m≤n" shows "(∑k=0..m. (n-k) choose (m-k)) = Suc n choose m"
proof -
  have "(∑k=0..m. (n-k) choose (m-k)) = (∑k=0..m. (n-m+k) choose k)"
    by (rule natsum_reverse_index) (simp add: assms)
  also have "... = Suc (n-m+m) choose m"
    by (rule sum_choose_lower)
  also have "... = Suc n choose m" using assms
    by simp
  finally show ?thesis .
qed

subsection{* Pochhammer's symbol : generalized rising factorial *}

text {* See @{url "http://en.wikipedia.org/wiki/Pochhammer_symbol"} *}

definition "pochhammer (a::'a::comm_semiring_1) n =
  (if n = 0 then 1 else setprod (λn. a + of_nat n) {0 .. n - 1})"

lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"
  by (simp add: pochhammer_def)

lemma pochhammer_1 [simp]: "pochhammer a 1 = a"
  by (simp add: pochhammer_def)

lemma pochhammer_Suc0 [simp]: "pochhammer a (Suc 0) = a"
  by (simp add: pochhammer_def)

lemma pochhammer_Suc_setprod: "pochhammer a (Suc n) = setprod (λn. a + of_nat n) {0 .. n}"
  by (simp add: pochhammer_def)

lemma setprod_nat_ivl_Suc: "setprod f {0 .. Suc n} = setprod f {0..n} * f (Suc n)"
proof -
  have "{0..Suc n} = {0..n} ∪ {Suc n}" by auto
  then show ?thesis by (simp add: field_simps)
qed

lemma setprod_nat_ivl_1_Suc: "setprod f {0 .. Suc n} = f 0 * setprod f {1.. Suc n}"
proof -
  have "{0..Suc n} = {0} ∪ {1 .. Suc n}" by auto
  then show ?thesis by simp
qed


lemma pochhammer_Suc: "pochhammer a (Suc n) = pochhammer a n * (a + of_nat n)"
proof (cases n)
  case 0
  then show ?thesis by simp
next
  case (Suc n)
  show ?thesis unfolding Suc pochhammer_Suc_setprod setprod_nat_ivl_Suc ..
qed

lemma pochhammer_rec: "pochhammer a (Suc n) = a * pochhammer (a + 1) n"
proof (cases "n = 0")
  case True
  then show ?thesis by (simp add: pochhammer_Suc_setprod)
next
  case False
  have *: "finite {1 .. n}" "0 ∉ {1 .. n}" by auto
  have eq: "insert 0 {1 .. n} = {0..n}" by auto
  have **: "(∏n∈{1::nat..n}. a + of_nat n) = (∏n∈{0::nat..n - 1}. a + 1 + of_nat n)"
    apply (rule setprod.reindex_cong [where l = Suc])
    using False
    apply (auto simp add: fun_eq_iff field_simps)
    done
  show ?thesis
    apply (simp add: pochhammer_def)
    unfolding setprod.insert [OF *, unfolded eq]
    using ** apply (simp add: field_simps)
    done
qed

lemma pochhammer_fact: "of_nat (fact n) = pochhammer 1 n"
  unfolding fact_altdef_nat
  apply (cases n)
   apply (simp_all add: of_nat_setprod pochhammer_Suc_setprod)
  apply (rule setprod.reindex_cong [where l = Suc])
    apply (auto simp add: fun_eq_iff)
  done

lemma pochhammer_of_nat_eq_0_lemma:
  assumes "k > n"
  shows "pochhammer (- (of_nat n :: 'a:: idom)) k = 0"
proof (cases "n = 0")
  case True
  then show ?thesis
    using assms by (cases k) (simp_all add: pochhammer_rec)
next
  case False
  from assms obtain h where "k = Suc h" by (cases k) auto
  then show ?thesis
    by (simp add: pochhammer_Suc_setprod)
       (metis Suc_leI Suc_le_mono assms atLeastAtMost_iff less_eq_nat.simps(1))
qed

lemma pochhammer_of_nat_eq_0_lemma':
  assumes kn: "k ≤ n"
  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k ≠ 0"
proof (cases k)
  case 0
  then show ?thesis by simp
next
  case (Suc h)
  then show ?thesis
    apply (simp add: pochhammer_Suc_setprod)
    using Suc kn apply (auto simp add: algebra_simps)
    done
qed

lemma pochhammer_of_nat_eq_0_iff:
  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k = 0 <-> k > n"
  (is "?l = ?r")
  using pochhammer_of_nat_eq_0_lemma[of n k, where ?'a='a]
    pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]
  by (auto simp add: not_le[symmetric])


lemma pochhammer_eq_0_iff: "pochhammer a n = (0::'a::field_char_0) <-> (∃k < n. a = - of_nat k)"
  apply (auto simp add: pochhammer_of_nat_eq_0_iff)
  apply (cases n)
   apply (auto simp add: pochhammer_def algebra_simps group_add_class.eq_neg_iff_add_eq_0)
  apply (metis leD not_less_eq)
  done


lemma pochhammer_eq_0_mono:
  "pochhammer a n = (0::'a::field_char_0) ==> m ≥ n ==> pochhammer a m = 0"
  unfolding pochhammer_eq_0_iff by auto

lemma pochhammer_neq_0_mono:
  "pochhammer a m ≠ (0::'a::field_char_0) ==> m ≥ n ==> pochhammer a n ≠ 0"
  unfolding pochhammer_eq_0_iff by auto

lemma pochhammer_minus:
  assumes kn: "k ≤ n"
  shows "pochhammer (- b) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (b - of_nat k + 1) k"
proof (cases k)
  case 0
  then show ?thesis by simp
next
  case (Suc h)
  have eq: "((- 1) ^ Suc h :: 'a) = (∏i=0..h. - 1)"
    using setprod_constant[where A="{0 .. h}" and y="- 1 :: 'a"]
    by auto
  show ?thesis
    unfolding Suc pochhammer_Suc_setprod eq setprod.distrib[symmetric]
    by (rule setprod.reindex_bij_witness[where i="op - h" and j="op - h"])
       (auto simp: of_nat_diff)
qed

lemma pochhammer_minus':
  assumes kn: "k ≤ n"
  shows "pochhammer (b - of_nat k + 1) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (- b) k"
  unfolding pochhammer_minus[OF kn, where b=b]
  unfolding mult.assoc[symmetric]
  unfolding power_add[symmetric]
  by simp

lemma pochhammer_same: "pochhammer (- of_nat n) n =
    ((- 1) ^ n :: 'a::comm_ring_1) * of_nat (fact n)"
  unfolding pochhammer_minus[OF le_refl[of n]]
  by (simp add: of_nat_diff pochhammer_fact)


subsection{* Generalized binomial coefficients *}

definition gbinomial :: "'a::field_char_0 => nat => 'a" (infixl "gchoose" 65)
  where "a gchoose n =
    (if n = 0 then 1 else (setprod (λi. a - of_nat i) {0 .. n - 1}) / of_nat (fact n))"

lemma gbinomial_0 [simp]: "a gchoose 0 = 1" "0 gchoose (Suc n) = 0"
  apply (simp_all add: gbinomial_def)
  apply (subgoal_tac "(∏i::nat∈{0::nat..n}. - of_nat i) = (0::'b)")
   apply (simp del:setprod_zero_iff)
  apply simp
  done

lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / of_nat (fact n)"
proof (cases "n = 0")
  case True
  then show ?thesis by simp
next
  case False
  from this setprod_constant[of "{0 .. n - 1}" "- (1:: 'a)"]
  have eq: "(- (1::'a)) ^ n = setprod (λi. - 1) {0 .. n - 1}"
    by auto
  from False show ?thesis
    by (simp add: pochhammer_def gbinomial_def field_simps
      eq setprod.distrib[symmetric])
qed

lemma binomial_fact_lemma: "k ≤ n ==> fact k * fact (n - k) * (n choose k) = fact n"
proof (induct n arbitrary: k rule: nat_less_induct)
  fix n k assume H: "∀m<n. ∀x≤m. fact x * fact (m - x) * (m choose x) =
                      fact m" and kn: "k ≤ n"
  let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"
  { assume "n=0" then have ?ths using kn by simp }
  moreover
  { assume "k=0" then have ?ths using kn by simp }
  moreover
  { assume nk: "n=k" then have ?ths by simp }
  moreover
  { fix m h assume n: "n = Suc m" and h: "k = Suc h" and hm: "h < m"
    from n have mn: "m < n" by arith
    from hm have hm': "h ≤ m" by arith
    from hm h n kn have km: "k ≤ m" by arith
    have "m - h = Suc (m - Suc h)" using  h km hm by arith
    with km h have th0: "fact (m - h) = (m - h) * fact (m - k)"
      by simp
    from n h th0
    have "fact k * fact (n - k) * (n choose k) =
        k * (fact h * fact (m - h) * (m choose h)) + 
        (m - h) * (fact k * fact (m - k) * (m choose k))"
      by (simp add: field_simps)
    also have "… = (k + (m - h)) * fact m"
      using H[rule_format, OF mn hm'] H[rule_format, OF mn km]
      by (simp add: field_simps)
    finally have ?ths using h n km by simp }
  moreover have "n=0 ∨ k = 0 ∨ k = n ∨ (∃m h. n = Suc m ∧ k = Suc h ∧ h < m)"
    using kn by presburger
  ultimately show ?ths by blast
qed

lemma binomial_fact:
  assumes kn: "k ≤ n"
  shows "(of_nat (n choose k) :: 'a::field_char_0) =
    of_nat (fact n) / (of_nat (fact k) * of_nat (fact (n - k)))"
  using binomial_fact_lemma[OF kn]
  by (simp add: field_simps of_nat_mult [symmetric])

lemma binomial_gbinomial: "of_nat (n choose k) = of_nat n gchoose k"
proof -
  { assume kn: "k > n"
    then have ?thesis
      by (subst binomial_eq_0[OF kn]) 
         (simp add: gbinomial_pochhammer field_simps  pochhammer_of_nat_eq_0_iff) }
  moreover
  { assume "k=0" then have ?thesis by simp }
  moreover
  { assume kn: "k ≤ n" and k0: "k≠ 0"
    from k0 obtain h where h: "k = Suc h" by (cases k) auto
    from h
    have eq:"(- 1 :: 'a) ^ k = setprod (λi. - 1) {0..h}"
      by (subst setprod_constant) auto
    have eq': "(∏i∈{0..h}. of_nat n + - (of_nat i :: 'a)) = (∏i∈{n - h..n}. of_nat i)"
        using h kn
      by (intro setprod.reindex_bij_witness[where i="op - n" and j="op - n"])
         (auto simp: of_nat_diff)
    have th0: "finite {1..n - Suc h}" "finite {n - h .. n}"
        "{1..n - Suc h} ∩ {n - h .. n} = {}" and
        eq3: "{1..n - Suc h} ∪ {n - h .. n} = {1..n}"
      using h kn by auto
    from eq[symmetric]
    have ?thesis using kn
      apply (simp add: binomial_fact[OF kn, where ?'a = 'a]
        gbinomial_pochhammer field_simps pochhammer_Suc_setprod)
      apply (simp add: pochhammer_Suc_setprod fact_altdef_nat h
        of_nat_setprod setprod.distrib[symmetric] eq' del: One_nat_def power_Suc)
      unfolding setprod.union_disjoint[OF th0, unfolded eq3, of "of_nat:: nat => 'a"] eq[unfolded h]
      unfolding mult.assoc[symmetric]
      unfolding setprod.distrib[symmetric]
      apply simp
      apply (intro setprod.reindex_bij_witness[where i="op - n" and j="op - n"])
      apply (auto simp: of_nat_diff)
      done
  }
  moreover
  have "k > n ∨ k = 0 ∨ (k ≤ n ∧ k ≠ 0)" by arith
  ultimately show ?thesis by blast
qed

lemma gbinomial_1[simp]: "a gchoose 1 = a"
  by (simp add: gbinomial_def)

lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"
  by (simp add: gbinomial_def)

lemma gbinomial_mult_1:
  "a * (a gchoose n) =
    of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"  (is "?l = ?r")
proof -
  have "?r = ((- 1) ^n * pochhammer (- a) n / of_nat (fact n)) * (of_nat n - (- a + of_nat n))"
    unfolding gbinomial_pochhammer
      pochhammer_Suc fact_Suc of_nat_mult right_diff_distrib power_Suc
    by (simp add:  field_simps del: of_nat_Suc)
  also have "… = ?l" unfolding gbinomial_pochhammer
    by (simp add: field_simps)
  finally show ?thesis ..
qed

lemma gbinomial_mult_1':
    "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
  by (simp add: mult.commute gbinomial_mult_1)

lemma gbinomial_Suc:
    "a gchoose (Suc k) = (setprod (λi. a - of_nat i) {0 .. k}) / of_nat (fact (Suc k))"
  by (simp add: gbinomial_def)

lemma gbinomial_mult_fact:
  "(of_nat (fact (Suc k)) :: 'a) * ((a::'a::field_char_0) gchoose (Suc k)) =
    (setprod (λi. a - of_nat i) {0 .. k})"
  by (simp_all add: gbinomial_Suc field_simps del: fact_Suc)

lemma gbinomial_mult_fact':
  "((a::'a::field_char_0) gchoose (Suc k)) * (of_nat (fact (Suc k)) :: 'a) =
    (setprod (λi. a - of_nat i) {0 .. k})"
  using gbinomial_mult_fact[of k a]
  by (subst mult.commute)


lemma gbinomial_Suc_Suc:
  "((a::'a::field_char_0) + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"
proof (cases k)
  case 0
  then show ?thesis by simp
next
  case (Suc h)
  have eq0: "(∏i∈{1..k}. (a + 1) - of_nat i) = (∏i∈{0..h}. a - of_nat i)"
    apply (rule setprod.reindex_cong [where l = Suc])
      using Suc
      apply auto
    done

  have "of_nat (fact (Suc k)) * (a gchoose k + (a gchoose (Suc k))) =
    ((a gchoose Suc h) * of_nat (fact (Suc h)) * of_nat (Suc k)) + (∏i∈{0::nat..Suc h}. a - of_nat i)"
    apply (simp add: Suc field_simps del: fact_Suc)
    unfolding gbinomial_mult_fact'
    apply (subst fact_Suc)
    unfolding of_nat_mult
    apply (subst mult.commute)
    unfolding mult.assoc
    unfolding gbinomial_mult_fact
    apply (simp add: field_simps)
    done
  also have "… = (∏i∈{0..h}. a - of_nat i) * (a + 1)"
    unfolding gbinomial_mult_fact' setprod_nat_ivl_Suc
    by (simp add: field_simps Suc)
  also have "… = (∏i∈{0..k}. (a + 1) - of_nat i)"
    using eq0
    by (simp add: Suc setprod_nat_ivl_1_Suc)
  also have "… = of_nat (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
    unfolding gbinomial_mult_fact ..
  finally show ?thesis by (simp del: fact_Suc)
qed


lemma binomial_symmetric:
  assumes kn: "k ≤ n"
  shows "n choose k = n choose (n - k)"
proof-
  from kn have kn': "n - k ≤ n" by arith
  from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']
  have "fact k * fact (n - k) * (n choose k) =
    fact (n - k) * fact (n - (n - k)) * (n choose (n - k))" by simp
  then show ?thesis using kn by simp
qed

(* Contributed by Manuel Eberl *)
(* Alternative definition of the binomial coefficient as ∏i<k. (n - i) / (k - i) *)
lemma binomial_altdef_of_nat:
  fixes n k :: nat
    and x :: "'a :: {field_char_0,field_inverse_zero}"
  assumes "k ≤ n"
  shows "of_nat (n choose k) = (∏i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
proof (cases "0 < k")
  case True
  then have "(of_nat (n choose k) :: 'a) = (∏i<k. of_nat n - of_nat i) / of_nat (fact k)"
    unfolding binomial_gbinomial gbinomial_def
    by (auto simp: gr0_conv_Suc lessThan_Suc_atMost atLeast0AtMost)
  also have "… = (∏i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
    using `k ≤ n` unfolding fact_eq_rev_setprod_nat of_nat_setprod
    by (auto simp add: setprod_dividef intro!: setprod.cong of_nat_diff[symmetric])
  finally show ?thesis .
next
  case False
  then show ?thesis by simp
qed

lemma binomial_ge_n_over_k_pow_k:
  fixes k n :: nat
    and x :: "'a :: linordered_field_inverse_zero"
  assumes "0 < k"
    and "k ≤ n"
  shows "(of_nat n / of_nat k :: 'a) ^ k ≤ of_nat (n choose k)"
proof -
  have "(of_nat n / of_nat k :: 'a) ^ k = (∏i<k. of_nat n / of_nat k :: 'a)"
    by (simp add: setprod_constant)
  also have "… ≤ of_nat (n choose k)"
    unfolding binomial_altdef_of_nat[OF `k≤n`]
  proof (safe intro!: setprod_mono)
    fix i :: nat
    assume  "i < k"
    from assms have "n * i ≥ i * k" by simp
    then have "n * k - n * i ≤ n * k - i * k" by arith
    then have "n * (k - i) ≤ (n - i) * k"
      by (simp add: diff_mult_distrib2 mult.commute)
    then have "of_nat n * of_nat (k - i) ≤ of_nat (n - i) * (of_nat k :: 'a)"
      unfolding of_nat_mult[symmetric] of_nat_le_iff .
    with assms show "of_nat n / of_nat k ≤ of_nat (n - i) / (of_nat (k - i) :: 'a)"
      using `i < k` by (simp add: field_simps)
  qed (simp add: zero_le_divide_iff)
  finally show ?thesis .
qed

lemma binomial_le_pow:
  assumes "r ≤ n"
  shows "n choose r ≤ n ^ r"
proof -
  have "n choose r ≤ fact n div fact (n - r)"
    using `r ≤ n` by (subst binomial_fact_lemma[symmetric]) auto
  with fact_div_fact_le_pow [OF assms] show ?thesis by auto
qed

lemma binomial_altdef_nat: "(k::nat) ≤ n ==>
    n choose k = fact n div (fact k * fact (n - k))"
 by (subst binomial_fact_lemma [symmetric]) auto

lemma fact_fact_dvd_fact_m: fixes k::nat shows "k ≤ n ==> fact k * fact (n - k) dvd fact n"
  by (metis binomial_fact_lemma dvd_def)

lemma fact_fact_dvd_fact: fixes k::nat shows "fact k * fact n dvd fact (n + k)"
  by (metis fact_fact_dvd_fact_m diff_add_inverse2 le_add2)

lemma choose_mult_lemma:
     "((m+r+k) choose (m+k)) * ((m+k) choose k) = ((m+r+k) choose k) * ((m+r) choose m)"
proof -
  have "((m+r+k) choose (m+k)) * ((m+k) choose k) =
        fact (m+r + k) div (fact (m + k) * fact (m+r - m)) * (fact (m + k) div (fact k * fact m))"
    by (simp add: assms binomial_altdef_nat)
  also have "... = fact (m+r+k) div (fact r * (fact k * fact m))"
    apply (subst div_mult_div_if_dvd)
    apply (auto simp: fact_fact_dvd_fact)
    apply (metis ac_simps add.commute fact_fact_dvd_fact)
    done
  also have "... = (fact (m+r+k) * fact (m+r)) div (fact r * (fact k * fact m) * fact (m+r))"
    apply (subst div_mult_div_if_dvd [symmetric])
    apply (auto simp: fact_fact_dvd_fact)
    apply (metis dvd_trans dvd.dual_order.refl fact_fact_dvd_fact mult_dvd_mono mult.left_commute)
    done
  also have "... = (fact (m+r+k) div (fact k * fact (m+r)) * (fact (m+r) div (fact r * fact m)))"
    apply (subst div_mult_div_if_dvd)
    apply (auto simp: fact_fact_dvd_fact)
    apply(metis mult.left_commute)
    done
  finally show ?thesis
    by (simp add: binomial_altdef_nat mult.commute)
qed

lemma choose_mult:
  assumes "k≤m" "m≤n"
    shows "(n choose m) * (m choose k) = (n choose k) * ((n-k) choose (m-k))"
using assms choose_mult_lemma [of "m-k" "n-m" k]
by simp


subsection {* Binomial coefficients *}

lemma choose_plus_one_nat:
     "((n::nat) + 1) choose (k + 1) =(n choose (k + 1)) + (n choose k)"
  by (simp add: choose_reduce_nat)

lemma choose_Suc_nat: 
     "(Suc n) choose (Suc k) = (n choose (Suc k)) + (n choose k)"
  by (simp add: choose_reduce_nat)

lemma choose_one: "(n::nat) choose 1 = n"
  by simp

lemma binomial_induct [rule_format]: "(ALL (n::nat). P n n) --> 
    (ALL n. P (Suc n) 0) --> (ALL n. (ALL k < n. P n k --> P n (Suc k) -->
    P (Suc n) (Suc k))) --> (ALL k <= n. P n k)"
  apply (induct n)
  apply auto
  apply (case_tac "k = 0")
  apply auto
  apply (case_tac "k = Suc n")
  apply auto
  apply (metis Suc_le_eq fact_nat.cases le_Suc_eq le_eq_less_or_eq)
  done

lemma choose_dvd_nat: "(k::nat) ≤ n ==> fact k * fact (n - k) dvd fact n"
by (metis binomial_fact_lemma dvd_def)

lemma choose_dvd_int: 
  assumes "(0::int) <= k" and "k <= n"
  shows "fact k * fact (n - k) dvd fact n"
  apply (subst tsub_eq [symmetric], rule assms)
  apply (rule choose_dvd_nat [transferred])
  using assms apply auto
  done

lemma card_UNION:
  assumes "finite A" and "∀k ∈ A. finite k"
  shows "card (\<Union>A) = nat (∑I | I ⊆ A ∧ I ≠ {}. -1 ^ (card I + 1) * int (card (\<Inter>I)))"
  (is "?lhs = ?rhs")
proof -
  have "?rhs = nat (∑I | I ⊆ A ∧ I ≠ {}. -1 ^ (card I + 1) * (∑_∈\<Inter>I. 1))" by simp
  also have "… = nat (∑I | I ⊆ A ∧ I ≠ {}. (∑_∈\<Inter>I. -1 ^ (card I + 1)))" (is "_ = nat ?rhs")
    by(subst setsum_right_distrib) simp
  also have "?rhs = (∑(I, _)∈Sigma {I. I ⊆ A ∧ I ≠ {}} Inter. -1 ^ (card I + 1))"
    using assms by(subst setsum.Sigma)(auto)
  also have "… = (∑(x, I)∈(SIGMA x:UNIV. {I. I ⊆ A ∧ I ≠ {} ∧ x ∈ \<Inter>I}). -1 ^ (card I + 1))"
    by (rule setsum.reindex_cong [where l = "λ(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)
  also have "… = (∑(x, I)∈(SIGMA x:\<Union>A. {I. I ⊆ A ∧ I ≠ {} ∧ x ∈ \<Inter>I}). -1 ^ (card I + 1))"
    using assms by(auto intro!: setsum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])
  also have "… = (∑x∈\<Union>A. (∑I|I ⊆ A ∧ I ≠ {} ∧ x ∈ \<Inter>I. -1 ^ (card I + 1)))" 
    using assms by(subst setsum.Sigma) auto
  also have "… = (∑_∈\<Union>A. 1)" (is "setsum ?lhs _ = _")
  proof(rule setsum.cong[OF refl])
    fix x
    assume x: "x ∈ \<Union>A"
    def K  "{X ∈ A. x ∈ X}"
    with `finite A` have K: "finite K" by auto
    let ?I = "λi. {I. I ⊆ A ∧ card I = i ∧ x ∈ \<Inter>I}"
    have "inj_on snd (SIGMA i:{1..card A}. ?I i)"
      using assms by(auto intro!: inj_onI)
    moreover have [symmetric]: "snd ` (SIGMA i:{1..card A}. ?I i) = {I. I ⊆ A ∧ I ≠ {} ∧ x ∈ \<Inter>I}"
      using assms by(auto intro!: rev_image_eqI[where x="(card a, a)" for a]
        simp add: card_gt_0_iff[folded Suc_le_eq]
        dest: finite_subset intro: card_mono)
    ultimately have "?lhs x = (∑(i, I)∈(SIGMA i:{1..card A}. ?I i). -1 ^ (i + 1))"
      by (rule setsum.reindex_cong [where l = snd]) fastforce
    also have "… = (∑i=1..card A. (∑I|I ⊆ A ∧ card I = i ∧ x ∈ \<Inter>I. -1 ^ (i + 1)))"
      using assms by(subst setsum.Sigma) auto
    also have "… = (∑i=1..card A. -1 ^ (i + 1) * (∑I|I ⊆ A ∧ card I = i ∧ x ∈ \<Inter>I. 1))"
      by(subst setsum_right_distrib) simp
    also have "… = (∑i=1..card K. -1 ^ (i + 1) * (∑I|I ⊆ K ∧ card I = i. 1))" (is "_ = ?rhs")
    proof(rule setsum.mono_neutral_cong_right[rule_format])
      show "{1..card K} ⊆ {1..card A}" using `finite A`
        by(auto simp add: K_def intro: card_mono)
    next
      fix i
      assume "i ∈ {1..card A} - {1..card K}"
      hence i: "i ≤ card A" "card K < i" by auto
      have "{I. I ⊆ A ∧ card I = i ∧ x ∈ \<Inter>I} = {I. I ⊆ K ∧ card I = i}" 
        by(auto simp add: K_def)
      also have "… = {}" using `finite A` i
        by(auto simp add: K_def dest: card_mono[rotated 1])
      finally show "-1 ^ (i + 1) * (∑I | I ⊆ A ∧ card I = i ∧ x ∈ \<Inter>I. 1 :: int) = 0"
        by(simp only:) simp
    next
      fix i
      have "(∑I | I ⊆ A ∧ card I = i ∧ x ∈ \<Inter>I. 1) = (∑I | I ⊆ K ∧ card I = i. 1 :: int)"
        (is "?lhs = ?rhs")
        by(rule setsum.cong)(auto simp add: K_def)
      thus "-1 ^ (i + 1) * ?lhs = -1 ^ (i + 1) * ?rhs" by simp
    qed simp
    also have "{I. I ⊆ K ∧ card I = 0} = {{}}" using assms
      by(auto simp add: card_eq_0_iff K_def dest: finite_subset)
    hence "?rhs = (∑i = 0..card K. -1 ^ (i + 1) * (∑I | I ⊆ K ∧ card I = i. 1 :: int)) + 1"
      by(subst (2) setsum_head_Suc)(simp_all )
    also have "… = (∑i = 0..card K. -1 * (-1 ^ i * int (card K choose i))) + 1"
      using K by(subst n_subsets[symmetric]) simp_all
    also have "… = - (∑i = 0..card K. -1 ^ i * int (card K choose i)) + 1"
      by(subst setsum_right_distrib[symmetric]) simp
    also have "… =  - ((-1 + 1) ^ card K) + 1"
      by(subst binomial_ring)(simp add: ac_simps)
    also have "… = 1" using x K by(auto simp add: K_def card_gt_0_iff)
    finally show "?lhs x = 1" .
  qed
  also have "nat … = card (\<Union>A)" by simp
  finally show ?thesis ..
qed

end