renaming HOL/Fact.thy -> Binomial.thy
authorpaulson <lp15@cam.ac.uk>
Tue, 10 Mar 2015 16:12:35 +0000
changeset 59669 de7792ea4090
parent 59668 1c937d56a70a
child 59670 dee043d19729
child 59730 b7c394c7a619
renaming HOL/Fact.thy -> Binomial.thy
src/HOL/Binomial.thy
src/HOL/Fact.thy
src/HOL/Import/Import_Setup.thy
src/HOL/Library/Permutations.thy
src/HOL/Number_Theory/Primes.thy
src/HOL/Transcendental.thy
src/HOL/ex/Birthday_Paradox.thy
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Binomial.thy	Tue Mar 10 16:12:35 2015 +0000
@@ -0,0 +1,1180 @@
+(*  Title       : Binomial.thy
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Conversion to Isar and new proofs by Lawrence C Paulson, 2004
+    The integer version of factorial and other additions by Jeremy Avigad.
+*)
+
+section{*Factorial Function, Binomial Coefficients and Binomial Theorem*}
+
+theory Binomial
+imports Main
+begin
+
+class fact =
+  fixes fact :: "'a \<Rightarrow> 'a"
+
+instantiation nat :: fact
+begin
+
+fun
+  fact_nat :: "nat \<Rightarrow> nat"
+where
+  fact_0_nat: "fact_nat 0 = Suc 0"
+| fact_Suc: "fact_nat (Suc x) = Suc x * fact x"
+
+instance ..
+
+end
+
+(* definitions for the integers *)
+
+instantiation int :: fact
+
+begin
+
+definition
+  fact_int :: "int \<Rightarrow> int"
+where
+  "fact_int x = (if x >= 0 then int (fact (nat x)) else 0)"
+
+instance proof qed
+
+end
+
+
+subsection {* Set up Transfer *}
+
+lemma transfer_nat_int_factorial:
+  "(x::int) >= 0 \<Longrightarrow> fact (nat x) = nat (fact x)"
+  unfolding fact_int_def
+  by auto
+
+
+lemma transfer_nat_int_factorial_closure:
+  "x >= (0::int) \<Longrightarrow> fact x >= 0"
+  by (auto simp add: fact_int_def)
+
+declare transfer_morphism_nat_int[transfer add return:
+    transfer_nat_int_factorial transfer_nat_int_factorial_closure]
+
+lemma transfer_int_nat_factorial:
+  "fact (int x) = int (fact x)"
+  unfolding fact_int_def by auto
+
+lemma transfer_int_nat_factorial_closure:
+  "is_nat x \<Longrightarrow> fact x >= 0"
+  by (auto simp add: fact_int_def)
+
+declare transfer_morphism_int_nat[transfer add return:
+    transfer_int_nat_factorial transfer_int_nat_factorial_closure]
+
+
+subsection {* Factorial *}
+
+lemma fact_0_int [simp]: "fact (0::int) = 1"
+  by (simp add: fact_int_def)
+
+lemma fact_1_nat [simp]: "fact (1::nat) = 1"
+  by simp
+
+lemma fact_Suc_0_nat [simp]: "fact (Suc 0) = Suc 0"
+  by simp
+
+lemma fact_1_int [simp]: "fact (1::int) = 1"
+  by (simp add: fact_int_def)
+
+lemma fact_plus_one_nat: "fact ((n::nat) + 1) = (n + 1) * fact n"
+  by simp
+
+lemma fact_plus_one_int:
+  assumes "n >= 0"
+  shows "fact ((n::int) + 1) = (n + 1) * fact n"
+  using assms unfolding fact_int_def
+  by (simp add: nat_add_distrib algebra_simps int_mult)
+
+lemma fact_reduce_nat: "(n::nat) > 0 \<Longrightarrow> fact n = n * fact (n - 1)"
+  apply (subgoal_tac "n = Suc (n - 1)")
+  apply (erule ssubst)
+  apply (subst fact_Suc)
+  apply simp_all
+  done
+
+lemma fact_reduce_int: "(n::int) > 0 \<Longrightarrow> fact n = n * fact (n - 1)"
+  apply (subgoal_tac "n = (n - 1) + 1")
+  apply (erule ssubst)
+  apply (subst fact_plus_one_int)
+  apply simp_all
+  done
+
+lemma fact_nonzero_nat [simp]: "fact (n::nat) \<noteq> 0"
+  apply (induct n)
+  apply (auto simp add: fact_plus_one_nat)
+  done
+
+lemma fact_nonzero_int [simp]: "n >= 0 \<Longrightarrow> fact (n::int) ~= 0"
+  by (simp add: fact_int_def)
+
+lemma fact_gt_zero_nat [simp]: "fact (n :: nat) > 0"
+  by (insert fact_nonzero_nat [of n], arith)
+
+lemma fact_gt_zero_int [simp]: "n >= 0 \<Longrightarrow> fact (n :: int) > 0"
+  by (auto simp add: fact_int_def)
+
+lemma fact_ge_one_nat [simp]: "fact (n :: nat) >= 1"
+  by (insert fact_nonzero_nat [of n], arith)
+
+lemma fact_ge_Suc_0_nat [simp]: "fact (n :: nat) >= Suc 0"
+  by (insert fact_nonzero_nat [of n], arith)
+
+lemma fact_ge_one_int [simp]: "n >= 0 \<Longrightarrow> fact (n :: int) >= 1"
+  apply (auto simp add: fact_int_def)
+  apply (subgoal_tac "1 = int 1")
+  apply (erule ssubst)
+  apply (subst zle_int)
+  apply auto
+  done
+
+lemma dvd_fact_nat [rule_format]: "1 <= m \<longrightarrow> m <= n \<longrightarrow> m dvd fact (n::nat)"
+  apply (induct n)
+  apply force
+  apply (auto simp only: fact_Suc)
+  apply (subgoal_tac "m = Suc n")
+  apply (erule ssubst)
+  apply (rule dvd_triv_left)
+  apply auto
+  done
+
+lemma dvd_fact_int [rule_format]: "1 <= m \<longrightarrow> m <= n \<longrightarrow> m dvd fact (n::int)"
+  apply (case_tac "1 <= n")
+  apply (induct n rule: int_ge_induct)
+  apply (auto simp add: fact_plus_one_int)
+  apply (subgoal_tac "m = i + 1")
+  apply auto
+  done
+
+lemma interval_plus_one_nat: "(i::nat) <= j + 1 \<Longrightarrow>
+  {i..j+1} = {i..j} Un {j+1}"
+  by auto
+
+lemma interval_Suc: "i <= Suc j \<Longrightarrow> {i..Suc j} = {i..j} Un {Suc j}"
+  by auto
+
+lemma interval_plus_one_int: "(i::int) <= j + 1 \<Longrightarrow> {i..j+1} = {i..j} Un {j+1}"
+  by auto
+
+lemma fact_altdef_nat: "fact (n::nat) = (PROD i:{1..n}. i)"
+  apply (induct n)
+  apply force
+  apply (subst fact_Suc)
+  apply (subst interval_Suc)
+  apply auto
+done
+
+lemma fact_altdef_int: "n >= 0 \<Longrightarrow> fact (n::int) = (PROD i:{1..n}. i)"
+  apply (induct n rule: int_ge_induct)
+  apply force
+  apply (subst fact_plus_one_int, assumption)
+  apply (subst interval_plus_one_int)
+  apply auto
+done
+
+lemma fact_dvd: "n \<le> m \<Longrightarrow> fact n dvd fact (m::nat)"
+  by (auto simp add: fact_altdef_nat intro!: setprod_dvd_setprod_subset)
+
+lemma fact_mod: "m \<le> (n::nat) \<Longrightarrow> fact n mod fact m = 0"
+  by (auto simp add: dvd_imp_mod_0 fact_dvd)
+
+lemma fact_div_fact:
+  assumes "m \<ge> (n :: nat)"
+  shows "(fact m) div (fact n) = \<Prod>{n + 1..m}"
+proof -
+  obtain d where "d = m - n" by auto
+  from assms this have "m = n + d" by auto
+  have "fact (n + d) div (fact n) = \<Prod>{n + 1..n + d}"
+  proof (induct d)
+    case 0
+    show ?case by simp
+  next
+    case (Suc d')
+    have "fact (n + Suc d') div fact n = Suc (n + d') * fact (n + d') div fact n"
+      by simp
+    also from Suc.hyps have "... = Suc (n + d') * \<Prod>{n + 1..n + d'}"
+      unfolding div_mult1_eq[of _ "fact (n + d')"] by (simp add: fact_mod)
+    also have "... = \<Prod>{n + 1..n + Suc d'}"
+      by (simp add: atLeastAtMostSuc_conv setprod.insert)
+    finally show ?case .
+  qed
+  from this `m = n + d` show ?thesis by simp
+qed
+
+lemma fact_mono_nat: "(m::nat) \<le> n \<Longrightarrow> fact m \<le> fact n"
+apply (drule le_imp_less_or_eq)
+apply (auto dest!: less_imp_Suc_add)
+apply (induct_tac k, auto)
+done
+
+lemma fact_neg_int [simp]: "m < (0::int) \<Longrightarrow> fact m = 0"
+  unfolding fact_int_def by auto
+
+lemma fact_ge_zero_int [simp]: "fact m >= (0::int)"
+  apply (case_tac "m >= 0")
+  apply auto
+  apply (frule fact_gt_zero_int)
+  apply arith
+done
+
+lemma fact_mono_int_aux [rule_format]: "k >= (0::int) \<Longrightarrow>
+    fact (m + k) >= fact m"
+  apply (case_tac "m < 0")
+  apply auto
+  apply (induct k rule: int_ge_induct)
+  apply auto
+  apply (subst add.assoc [symmetric])
+  apply (subst fact_plus_one_int)
+  apply auto
+  apply (erule order_trans)
+  apply (subst mult_le_cancel_right1)
+  apply (subgoal_tac "fact (m + i) >= 0")
+  apply arith
+  apply auto
+done
+
+lemma fact_mono_int: "(m::int) <= n \<Longrightarrow> fact m <= fact n"
+  apply (insert fact_mono_int_aux [of "n - m" "m"])
+  apply auto
+done
+
+text{*Note that @{term "fact 0 = fact 1"}*}
+lemma fact_less_mono_nat: "[| (0::nat) < m; m < n |] ==> fact m < fact n"
+apply (drule_tac m = m in less_imp_Suc_add, auto)
+apply (induct_tac k, auto)
+done
+
+lemma fact_less_mono_int_aux: "k >= 0 \<Longrightarrow> (0::int) < m \<Longrightarrow>
+    fact m < fact ((m + 1) + k)"
+  apply (induct k rule: int_ge_induct)
+  apply (simp add: fact_plus_one_int)
+  apply (subst (2) fact_reduce_int)
+  apply (auto simp add: ac_simps)
+  apply (erule order_less_le_trans)
+  apply auto
+  done
+
+lemma fact_less_mono_int: "(0::int) < m \<Longrightarrow> m < n \<Longrightarrow> fact m < fact n"
+  apply (insert fact_less_mono_int_aux [of "n - (m + 1)" "m"])
+  apply auto
+done
+
+lemma fact_num_eq_if_nat: "fact (m::nat) =
+  (if m=0 then 1 else m * fact (m - 1))"
+by (cases m) auto
+
+lemma fact_add_num_eq_if_nat:
+  "fact ((m::nat) + n) = (if m + n = 0 then 1 else (m + n) * fact (m + n - 1))"
+by (cases "m + n") auto
+
+lemma fact_add_num_eq_if2_nat:
+  "fact ((m::nat) + n) =
+    (if m = 0 then fact n else (m + n) * fact ((m - 1) + n))"
+by (cases m) auto
+
+lemma fact_le_power: "fact n \<le> n^n"
+proof (induct n)
+  case (Suc n)
+  then have "fact n \<le> Suc n ^ n" by (rule le_trans) (simp add: power_mono)
+  then show ?case by (simp add: add_le_mono)
+qed simp
+
+subsection {* @{term fact} and @{term of_nat} *}
+
+lemma of_nat_fact_not_zero [simp]: "of_nat (fact n) \<noteq> (0::'a::semiring_char_0)"
+by auto
+
+lemma of_nat_fact_gt_zero [simp]: "(0::'a::{linordered_semidom}) < of_nat(fact n)" by auto
+
+lemma of_nat_fact_ge_zero [simp]: "(0::'a::linordered_semidom) \<le> of_nat(fact n)"
+by simp
+
+lemma inv_of_nat_fact_gt_zero [simp]: "(0::'a::linordered_field) < inverse (of_nat (fact n))"
+by (auto simp add: positive_imp_inverse_positive)
+
+lemma inv_of_nat_fact_ge_zero [simp]: "(0::'a::linordered_field) \<le> inverse (of_nat (fact n))"
+by (auto intro: order_less_imp_le)
+
+lemma fact_eq_rev_setprod_nat: "fact (k::nat) = (\<Prod>i<k. k - i)"
+  unfolding fact_altdef_nat
+  by (rule setprod.reindex_bij_witness[where i="\<lambda>i. k - i" and j="\<lambda>i. k - i"]) auto
+
+lemma fact_div_fact_le_pow:
+  assumes "r \<le> n" shows "fact n div fact (n - r) \<le> n ^ r"
+proof -
+  have "\<And>r. r \<le> n \<Longrightarrow> \<Prod>{n - r..n} = (n - r) * \<Prod>{Suc (n - r)..n}"
+    by (subst setprod.insert[symmetric]) (auto simp: atLeastAtMost_insertL)
+  with assms show ?thesis
+    by (induct r rule: nat.induct) (auto simp add: fact_div_fact Suc_diff_Suc mult_le_mono)
+qed
+
+lemma fact_numeral:  --{*Evaluation for specific numerals*}
+  "fact (numeral k) = (numeral k) * (fact (pred_numeral k))"
+  by (simp add: numeral_eq_Suc)
+
+
+text {* This development is based on the work of Andy Gordon and
+  Florian Kammueller. *}
+
+subsection {* Basic definitions and lemmas *}
+
+primrec binomial :: "nat \<Rightarrow> nat \<Rightarrow> 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) \<Longrightarrow> 0 < k \<Longrightarrow>
+    (n choose k) = ((n - 1) choose (k - 1)) + ((n - 1) choose k)"
+  by (metis Suc_diff_1 binomial.simps(2) neq0_conv)
+
+lemma binomial_eq_0: "n < k \<Longrightarrow> 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 \<le> n \<Longrightarrow> n choose k > 0"
+  by (induct n k rule: diff_induct) simp_all
+
+lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> 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 \<longleftrightarrow> k \<le> n"
+  by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)
+
+lemma Suc_times_binomial_eq:
+  "Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
+  apply (induct n arbitrary: k, 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{*The absorption property*}
+lemma Suc_times_binomial:
+  "Suc k * (Suc n choose Suc k) = Suc n * (n choose k)"
+  using Suc_times_binomial_eq by auto
+
+text{*This is the well-known version of absorption, but it's harder to use because of the
+  need to reason about division.*}
+lemma binomial_Suc_Suc_eq_times:
+    "(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 of absorption, with -1 instead of Suc.*}
+lemma times_binomial_minus1_eq:
+  "0 < k \<Longrightarrow> k * (n choose 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 \<Longrightarrow> card {B. B \<subseteq> A & card B = 0} = 1"
+  by (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
+
+lemma choose_deconstruct: "finite M \<Longrightarrow> x \<notin> M \<Longrightarrow>
+    {s. s \<subseteq> insert x M \<and> card s = Suc k} =
+    {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}"
+  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 "\<And>A. A \<subseteq> B \<Longrightarrow> finite {x. P x A}"
+  shows "finite {x. \<exists>A \<subseteq> 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 \<Longrightarrow> x \<notin> A \<Longrightarrow>
+    card {B. \<exists>C. C \<subseteq> A \<and> card C = k \<and> B = insert x C} =
+    card {B. B \<subseteq> A & card(B) = k}"
+  apply (rule card_bij_eq [where f = "\<lambda>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 \<Longrightarrow> card {B. B \<subseteq> A \<and> 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 =
+  (\<Sum>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) * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
+    using Suc.hyps by simp
+  also have "\<dots> = a*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +
+                   b*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
+    by (rule distrib_right)
+  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +
+                  (\<Sum>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 "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n+1-k)) +
+                  (\<Sum>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 "\<dots> = a^(n+1) + b^(n+1) +
+                  (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n+1-k)) +
+                  (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n+1-k))"
+    by (simp add: decomp2)
+  also have
+      "\<dots> = a^(n+1) + b^(n+1) +
+            (\<Sum>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 "\<dots> = (\<Sum>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 = (\<Sum>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 binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
+proof (induct n arbitrary: k rule: nat_less_induct)
+  fix n k assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) =
+                      fact m" and kn: "k \<le> 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 \<le> m" by arith
+    from hm h n kn have km: "k \<le> 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 "\<dots> = (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 \<or> k = 0 \<or> k = n \<or> (\<exists>m h. n = Suc m \<and> k = Suc h \<and> h < m)"
+    using kn by presburger
+  ultimately show ?ths by blast
+qed
+
+lemma binomial_fact:
+  assumes kn: "k \<le> n"
+  shows "(of_nat (n choose k) :: 'a::{field,ring_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 choose_row_sum: "(\<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: "(\<Sum>k=0..n. (r+k) choose k) = Suc (r+n) choose n"
+  by (induct n) auto
+
+lemma sum_choose_upper: "(\<Sum>k=0..n. k choose m) = Suc n choose Suc m"
+  by (induct n) auto
+
+lemma natsum_reverse_index:
+  fixes m::nat
+  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)"
+  by (rule setsum.reindex_bij_witness[where i="\<lambda>k. m+n-k" and j="\<lambda>k. m+n-k"]) auto
+
+text{*NW diagonal sum property*}
+lemma sum_choose_diagonal:
+  assumes "m\<le>n" shows "(\<Sum>k=0..m. (n-k) choose (m-k)) = Suc n choose m"
+proof -
+  have "(\<Sum>k=0..m. (n-k) choose (m-k)) = (\<Sum>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 (\<lambda>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 (\<lambda>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} \<union> {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} \<union> {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 \<notin> {1 .. n}" by auto
+  have eq: "insert 0 {1 .. n} = {0..n}" by auto
+  have **: "(\<Prod>n\<in>{1\<Colon>nat..n}. a + of_nat n) = (\<Prod>n\<in>{0\<Colon>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 \<le> n"
+  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k \<noteq> 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 \<longleftrightarrow> 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) \<longleftrightarrow> (\<exists>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) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"
+  unfolding pochhammer_eq_0_iff by auto
+
+lemma pochhammer_neq_0_mono:
+  "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"
+  unfolding pochhammer_eq_0_iff by auto
+
+lemma pochhammer_minus:
+  assumes kn: "k \<le> 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) = (\<Prod>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 \<le> 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 \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
+  where "a gchoose n =
+    (if n = 0 then 1 else (setprod (\<lambda>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 "(\<Prod>i\<Colon>nat\<in>{0\<Colon>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\<Colon>'a)) ^ n = setprod (\<lambda>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_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 \<le> n" and k0: "k\<noteq> 0"
+    from k0 obtain h where h: "k = Suc h" by (cases k) auto
+    from h
+    have eq:"(- 1 :: 'a) ^ k = setprod (\<lambda>i. - 1) {0..h}"
+      by (subst setprod_constant) auto
+    have eq': "(\<Prod>i\<in>{0..h}. of_nat n + - (of_nat i :: 'a)) = (\<Prod>i\<in>{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} \<inter> {n - h .. n} = {}" and
+        eq3: "{1..n - Suc h} \<union> {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 \<Rightarrow> '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 \<or> k = 0 \<or> (k \<le> n \<and> k \<noteq> 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 "\<dots> = ?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 (\<lambda>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 (\<lambda>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 (\<lambda>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: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{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)) + (\<Prod>i\<in>{0\<Colon>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 "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
+    unfolding gbinomial_mult_fact' setprod_nat_ivl_Suc
+    by (simp add: field_simps Suc)
+  also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
+    using eq0
+    by (simp add: Suc setprod_nat_ivl_1_Suc)
+  also have "\<dots> = of_nat (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
+    unfolding gbinomial_mult_fact ..
+  finally show ?thesis by (simp del: fact_Suc)
+qed
+
+lemma gbinomial_reduce_nat:
+  "0 < k \<Longrightarrow> (a::'a::field_char_0) gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)"
+by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc)
+
+
+lemma binomial_symmetric:
+  assumes kn: "k \<le> n"
+  shows "n choose k = n choose (n - k)"
+proof-
+  from kn have kn': "n - k \<le> 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
+
+text{*Contributed by Manuel Eberl, generalised by LCP.
+  Alternative definition of the binomial coefficient as @{term "\<Prod>i<k. (n - i) / (k - i)"} *}
+lemma gbinomial_altdef_of_nat:
+  fixes k :: nat
+    and x :: "'a :: {field_char_0,field_inverse_zero}"
+  shows "x gchoose k = (\<Prod>i<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
+proof -
+  have "(x gchoose k) = (\<Prod>i<k. x - of_nat i) / of_nat (fact k)"
+    unfolding gbinomial_def
+    by (auto simp: gr0_conv_Suc lessThan_Suc_atMost atLeast0AtMost)
+  also have "\<dots> = (\<Prod>i<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
+    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 .
+qed
+
+lemma gbinomial_ge_n_over_k_pow_k:
+  fixes k :: nat
+    and x :: "'a :: linordered_field_inverse_zero"
+  assumes "of_nat k \<le> x"
+  shows "(x / of_nat k :: 'a) ^ k \<le> x gchoose k"
+proof -
+  have x: "0 \<le> x"
+    using assms of_nat_0_le_iff order_trans by blast
+  have "(x / of_nat k :: 'a) ^ k = (\<Prod>i<k. x / of_nat k :: 'a)"
+    by (simp add: setprod_constant)
+  also have "\<dots> \<le> x gchoose k"
+    unfolding gbinomial_altdef_of_nat
+  proof (safe intro!: setprod_mono)
+    fix i :: nat
+    assume ik: "i < k"
+    from assms have "x * of_nat i \<ge> of_nat (i * k)"
+      by (metis mult.commute mult_le_cancel_right of_nat_less_0_iff of_nat_mult)
+    then have "x * of_nat k - x * of_nat i \<le> x * of_nat k - of_nat (i * k)" by arith
+    then have "x * of_nat (k - i) \<le> (x - of_nat i) * of_nat k"
+      using ik
+      by (simp add: algebra_simps zero_less_mult_iff of_nat_diff of_nat_mult)
+    then have "x * of_nat (k - i) \<le> (x - of_nat i) * (of_nat k :: 'a)"
+      unfolding of_nat_mult[symmetric] of_nat_le_iff .
+    with assms show "x / of_nat k \<le> (x - of_nat i) / (of_nat (k - i) :: 'a)"
+      using `i < k` by (simp add: field_simps)
+  qed (simp add: x zero_le_divide_iff)
+  finally show ?thesis .
+qed
+
+text{*Versions of the theorems above for the natural-number version of "choose"*}
+lemma binomial_altdef_of_nat:
+  fixes n k :: nat
+    and x :: "'a :: {field_char_0,field_inverse_zero}"  --{*the point is to constrain @{typ 'a}*}
+  assumes "k \<le> n"
+  shows "of_nat (n choose k) = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
+using assms
+by (simp add: gbinomial_altdef_of_nat binomial_gbinomial of_nat_diff)
+
+lemma binomial_ge_n_over_k_pow_k:
+  fixes k n :: nat
+    and x :: "'a :: linordered_field_inverse_zero"
+  assumes "k \<le> n"
+  shows "(of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)"
+by (simp add: assms gbinomial_ge_n_over_k_pow_k binomial_gbinomial of_nat_diff)
+
+lemma binomial_le_pow:
+  assumes "r \<le> n"
+  shows "n choose r \<le> n ^ r"
+proof -
+  have "n choose r \<le> fact n div fact (n - r)"
+    using `r \<le> 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) \<le> n \<Longrightarrow>
+    n choose k = fact n div (fact k * fact (n - k))"
+ by (subst binomial_fact_lemma [symmetric]) auto
+
+lemma choose_dvd_nat: "(k::nat) \<le> n \<Longrightarrow> 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 fact_fact_dvd_fact: fixes k::nat shows "fact k * fact n dvd fact (n + k)"
+by (metis add.commute add_diff_cancel_left' choose_dvd_nat 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 add.assoc 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
+
+text{*The "Subset of a Subset" identity*}
+lemma choose_mult:
+  assumes "k\<le>m" "m\<le>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_one: "(n::nat) choose 1 = n"
+  by simp
+
+(*FIXME: messy and apparently unused*)
+lemma binomial_induct [rule_format]: "(ALL (n::nat). P n n) \<longrightarrow>
+    (ALL n. P (Suc n) 0) \<longrightarrow> (ALL n. (ALL k < n. P n k \<longrightarrow> P n (Suc k) \<longrightarrow>
+    P (Suc n) (Suc k))) \<longrightarrow> (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 card_UNION:
+  assumes "finite A" and "\<forall>k \<in> A. finite k"
+  shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))"
+  (is "?lhs = ?rhs")
+proof -
+  have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))" by simp
+  also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))" (is "_ = nat ?rhs")
+    by(subst setsum_right_distrib) simp
+  also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))"
+    using assms by(subst setsum.Sigma)(auto)
+  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))"
+    by (rule setsum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)
+  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))"
+    using assms by(auto intro!: setsum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])
+  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)))"
+    using assms by(subst setsum.Sigma) auto
+  also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "setsum ?lhs _ = _")
+  proof(rule setsum.cong[OF refl])
+    fix x
+    assume x: "x \<in> \<Union>A"
+    def K \<equiv> "{X \<in> A. x \<in> X}"
+    with `finite A` have K: "finite K" by auto
+    let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<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 \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<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 = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))"
+      by (rule setsum.reindex_cong [where l = snd]) fastforce
+    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)))"
+      using assms by(subst setsum.Sigma) auto
+    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))"
+      by(subst setsum_right_distrib) simp
+    also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))" (is "_ = ?rhs")
+    proof(rule setsum.mono_neutral_cong_right[rule_format])
+      show "{1..card K} \<subseteq> {1..card A}" using `finite A`
+        by(auto simp add: K_def intro: card_mono)
+    next
+      fix i
+      assume "i \<in> {1..card A} - {1..card K}"
+      hence i: "i \<le> card A" "card K < i" by auto
+      have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}"
+        by(auto simp add: K_def)
+      also have "\<dots> = {}" using `finite A` i
+        by(auto simp add: K_def dest: card_mono[rotated 1])
+      finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0"
+        by(simp only:) simp
+    next
+      fix i
+      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)"
+        (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 \<subseteq> K \<and> card I = 0} = {{}}" using assms
+      by(auto simp add: card_eq_0_iff K_def dest: finite_subset)
+    hence "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1"
+      by(subst (2) setsum_head_Suc)(simp_all )
+    also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1"
+      using K by(subst n_subsets[symmetric]) simp_all
+    also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1"
+      by(subst setsum_right_distrib[symmetric]) simp
+    also have "\<dots> =  - ((-1 + 1) ^ card K) + 1"
+      by(subst binomial_ring)(simp add: ac_simps)
+    also have "\<dots> = 1" using x K by(auto simp add: K_def card_gt_0_iff)
+    finally show "?lhs x = 1" .
+  qed
+  also have "nat \<dots> = card (\<Union>A)" by simp
+  finally show ?thesis ..
+qed
+
+text{* The number of nat lists of length @{text m} summing to @{text N} is
+@{term "(N + m - 1) choose N"}: *}
+
+lemma card_length_listsum_rec:
+  assumes "m\<ge>1"
+  shows "card {l::nat list. length l = m \<and> listsum l = N} =
+    (card {l. length l = (m - 1) \<and> listsum l = N} +
+    card {l. length l = m \<and> listsum l + 1 =  N})"
+    (is "card ?C = (card ?A + card ?B)")
+proof -
+  let ?A'="{l. length l = m \<and> listsum l = N \<and> hd l = 0}"
+  let ?B'="{l. length l = m \<and> listsum l = N \<and> hd l \<noteq> 0}"
+  let ?f ="\<lambda> l. 0#l"
+  let ?g ="\<lambda> l. (hd l + 1) # tl l"
+  have 1: "\<And>xs x. xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" by simp
+  have 2: "\<And>xs. (xs::nat list) \<noteq> [] \<Longrightarrow> listsum(tl xs) = listsum xs - hd xs"
+    by(auto simp add: neq_Nil_conv)
+  have f: "bij_betw ?f ?A ?A'"
+    apply(rule bij_betw_byWitness[where f' = tl])
+    using assms
+    by (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv)
+  have 3: "\<And>xs:: nat list. xs \<noteq> [] \<Longrightarrow> hd xs + (listsum xs - hd xs) = listsum xs"
+    by (metis 1 listsum_simps(2) 2)
+  have g: "bij_betw ?g ?B ?B'"
+    apply(rule bij_betw_byWitness[where f' = "\<lambda> l. (hd l - 1) # tl l"])
+    using assms
+    by (auto simp: 2 length_0_conv[symmetric] intro!: 3
+      simp del: length_greater_0_conv length_0_conv)
+  { fix M N :: nat have "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}"
+    using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto }
+    note fin = this
+  have fin_A: "finite ?A" using fin[of _ "N+1"]
+    by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"],
+      auto simp: member_le_listsum_nat less_Suc_eq_le)
+  have fin_B: "finite ?B"
+    by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"],
+      auto simp: member_le_listsum_nat less_Suc_eq_le fin)
+  have uni: "?C = ?A' \<union> ?B'" by auto
+  have disj: "?A' \<inter> ?B' = {}" by auto
+  have "card ?C = card(?A' \<union> ?B')" using uni by simp
+  also have "\<dots> = card ?A + card ?B"
+    using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g]
+      bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B
+    by presburger
+  finally show ?thesis .
+qed
+
+lemma card_length_listsum: --"By Holden Lee, tidied by Tobias Nipkow"
+  "card {l::nat list. size l = m \<and> listsum l = N} = (N + m - 1) choose N"
+proof (cases m)
+  case 0 then show ?thesis
+    by (cases N) (auto simp: cong: conj_cong)
+next
+  case (Suc m')
+    have m: "m\<ge>1" by (simp add: Suc)
+    then show ?thesis
+    proof (induct "N + m - 1" arbitrary: N m)
+      case 0   -- "In the base case, the only solution is [0]."
+      have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {[0]}"
+        by (auto simp: length_Suc_conv)
+      have "m=1 \<and> N=0" using 0 by linarith
+      then show ?case by simp
+    next
+      case (Suc k)
+
+      have c1: "card {l::nat list. size l = (m - 1) \<and> listsum l =  N} =
+        (N + (m - 1) - 1) choose N"
+      proof cases
+        assume "m = 1"
+        with Suc.hyps have "N\<ge>1" by auto
+        with `m = 1` show ?thesis by (simp add: binomial_eq_0)
+      next
+        assume "m \<noteq> 1" thus ?thesis using Suc by fastforce
+      qed
+
+      from Suc have c2: "card {l::nat list. size l = m \<and> listsum l + 1 = N} =
+        (if N>0 then ((N - 1) + m - 1) choose (N - 1) else 0)"
+      proof -
+        have aux: "\<And>m n. n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" by arith
+        from Suc have "N>0 \<Longrightarrow>
+          card {l::nat list. size l = m \<and> listsum l + 1 = N} =
+          ((N - 1) + m - 1) choose (N - 1)" by (simp add: aux)
+        thus ?thesis by auto
+      qed
+
+      from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> listsum l = N} +
+          card {l::nat list. size l = m \<and> listsum l + 1 = N}) = (N + m - 1) choose N"
+        by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def)
+      thus ?case using card_length_listsum_rec[OF Suc.prems] by auto
+    qed
+qed
+
+end
--- a/src/HOL/Fact.thy	Tue Mar 10 15:21:26 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1180 +0,0 @@
-(*  Title       : Fact.thy
-    Author      : Jacques D. Fleuriot
-    Copyright   : 1998  University of Cambridge
-    Conversion to Isar and new proofs by Lawrence C Paulson, 2004
-    The integer version of factorial and other additions by Jeremy Avigad.
-*)
-
-section{*Factorial Function*}
-
-theory Fact
-imports Main
-begin
-
-class fact =
-  fixes fact :: "'a \<Rightarrow> 'a"
-
-instantiation nat :: fact
-begin
-
-fun
-  fact_nat :: "nat \<Rightarrow> nat"
-where
-  fact_0_nat: "fact_nat 0 = Suc 0"
-| fact_Suc: "fact_nat (Suc x) = Suc x * fact x"
-
-instance ..
-
-end
-
-(* definitions for the integers *)
-
-instantiation int :: fact
-
-begin
-
-definition
-  fact_int :: "int \<Rightarrow> int"
-where
-  "fact_int x = (if x >= 0 then int (fact (nat x)) else 0)"
-
-instance proof qed
-
-end
-
-
-subsection {* Set up Transfer *}
-
-lemma transfer_nat_int_factorial:
-  "(x::int) >= 0 \<Longrightarrow> fact (nat x) = nat (fact x)"
-  unfolding fact_int_def
-  by auto
-
-
-lemma transfer_nat_int_factorial_closure:
-  "x >= (0::int) \<Longrightarrow> fact x >= 0"
-  by (auto simp add: fact_int_def)
-
-declare transfer_morphism_nat_int[transfer add return:
-    transfer_nat_int_factorial transfer_nat_int_factorial_closure]
-
-lemma transfer_int_nat_factorial:
-  "fact (int x) = int (fact x)"
-  unfolding fact_int_def by auto
-
-lemma transfer_int_nat_factorial_closure:
-  "is_nat x \<Longrightarrow> fact x >= 0"
-  by (auto simp add: fact_int_def)
-
-declare transfer_morphism_int_nat[transfer add return:
-    transfer_int_nat_factorial transfer_int_nat_factorial_closure]
-
-
-subsection {* Factorial *}
-
-lemma fact_0_int [simp]: "fact (0::int) = 1"
-  by (simp add: fact_int_def)
-
-lemma fact_1_nat [simp]: "fact (1::nat) = 1"
-  by simp
-
-lemma fact_Suc_0_nat [simp]: "fact (Suc 0) = Suc 0"
-  by simp
-
-lemma fact_1_int [simp]: "fact (1::int) = 1"
-  by (simp add: fact_int_def)
-
-lemma fact_plus_one_nat: "fact ((n::nat) + 1) = (n + 1) * fact n"
-  by simp
-
-lemma fact_plus_one_int:
-  assumes "n >= 0"
-  shows "fact ((n::int) + 1) = (n + 1) * fact n"
-  using assms unfolding fact_int_def
-  by (simp add: nat_add_distrib algebra_simps int_mult)
-
-lemma fact_reduce_nat: "(n::nat) > 0 \<Longrightarrow> fact n = n * fact (n - 1)"
-  apply (subgoal_tac "n = Suc (n - 1)")
-  apply (erule ssubst)
-  apply (subst fact_Suc)
-  apply simp_all
-  done
-
-lemma fact_reduce_int: "(n::int) > 0 \<Longrightarrow> fact n = n * fact (n - 1)"
-  apply (subgoal_tac "n = (n - 1) + 1")
-  apply (erule ssubst)
-  apply (subst fact_plus_one_int)
-  apply simp_all
-  done
-
-lemma fact_nonzero_nat [simp]: "fact (n::nat) \<noteq> 0"
-  apply (induct n)
-  apply (auto simp add: fact_plus_one_nat)
-  done
-
-lemma fact_nonzero_int [simp]: "n >= 0 \<Longrightarrow> fact (n::int) ~= 0"
-  by (simp add: fact_int_def)
-
-lemma fact_gt_zero_nat [simp]: "fact (n :: nat) > 0"
-  by (insert fact_nonzero_nat [of n], arith)
-
-lemma fact_gt_zero_int [simp]: "n >= 0 \<Longrightarrow> fact (n :: int) > 0"
-  by (auto simp add: fact_int_def)
-
-lemma fact_ge_one_nat [simp]: "fact (n :: nat) >= 1"
-  by (insert fact_nonzero_nat [of n], arith)
-
-lemma fact_ge_Suc_0_nat [simp]: "fact (n :: nat) >= Suc 0"
-  by (insert fact_nonzero_nat [of n], arith)
-
-lemma fact_ge_one_int [simp]: "n >= 0 \<Longrightarrow> fact (n :: int) >= 1"
-  apply (auto simp add: fact_int_def)
-  apply (subgoal_tac "1 = int 1")
-  apply (erule ssubst)
-  apply (subst zle_int)
-  apply auto
-  done
-
-lemma dvd_fact_nat [rule_format]: "1 <= m \<longrightarrow> m <= n \<longrightarrow> m dvd fact (n::nat)"
-  apply (induct n)
-  apply force
-  apply (auto simp only: fact_Suc)
-  apply (subgoal_tac "m = Suc n")
-  apply (erule ssubst)
-  apply (rule dvd_triv_left)
-  apply auto
-  done
-
-lemma dvd_fact_int [rule_format]: "1 <= m \<longrightarrow> m <= n \<longrightarrow> m dvd fact (n::int)"
-  apply (case_tac "1 <= n")
-  apply (induct n rule: int_ge_induct)
-  apply (auto simp add: fact_plus_one_int)
-  apply (subgoal_tac "m = i + 1")
-  apply auto
-  done
-
-lemma interval_plus_one_nat: "(i::nat) <= j + 1 \<Longrightarrow>
-  {i..j+1} = {i..j} Un {j+1}"
-  by auto
-
-lemma interval_Suc: "i <= Suc j \<Longrightarrow> {i..Suc j} = {i..j} Un {Suc j}"
-  by auto
-
-lemma interval_plus_one_int: "(i::int) <= j + 1 \<Longrightarrow> {i..j+1} = {i..j} Un {j+1}"
-  by auto
-
-lemma fact_altdef_nat: "fact (n::nat) = (PROD i:{1..n}. i)"
-  apply (induct n)
-  apply force
-  apply (subst fact_Suc)
-  apply (subst interval_Suc)
-  apply auto
-done
-
-lemma fact_altdef_int: "n >= 0 \<Longrightarrow> fact (n::int) = (PROD i:{1..n}. i)"
-  apply (induct n rule: int_ge_induct)
-  apply force
-  apply (subst fact_plus_one_int, assumption)
-  apply (subst interval_plus_one_int)
-  apply auto
-done
-
-lemma fact_dvd: "n \<le> m \<Longrightarrow> fact n dvd fact (m::nat)"
-  by (auto simp add: fact_altdef_nat intro!: setprod_dvd_setprod_subset)
-
-lemma fact_mod: "m \<le> (n::nat) \<Longrightarrow> fact n mod fact m = 0"
-  by (auto simp add: dvd_imp_mod_0 fact_dvd)
-
-lemma fact_div_fact:
-  assumes "m \<ge> (n :: nat)"
-  shows "(fact m) div (fact n) = \<Prod>{n + 1..m}"
-proof -
-  obtain d where "d = m - n" by auto
-  from assms this have "m = n + d" by auto
-  have "fact (n + d) div (fact n) = \<Prod>{n + 1..n + d}"
-  proof (induct d)
-    case 0
-    show ?case by simp
-  next
-    case (Suc d')
-    have "fact (n + Suc d') div fact n = Suc (n + d') * fact (n + d') div fact n"
-      by simp
-    also from Suc.hyps have "... = Suc (n + d') * \<Prod>{n + 1..n + d'}"
-      unfolding div_mult1_eq[of _ "fact (n + d')"] by (simp add: fact_mod)
-    also have "... = \<Prod>{n + 1..n + Suc d'}"
-      by (simp add: atLeastAtMostSuc_conv setprod.insert)
-    finally show ?case .
-  qed
-  from this `m = n + d` show ?thesis by simp
-qed
-
-lemma fact_mono_nat: "(m::nat) \<le> n \<Longrightarrow> fact m \<le> fact n"
-apply (drule le_imp_less_or_eq)
-apply (auto dest!: less_imp_Suc_add)
-apply (induct_tac k, auto)
-done
-
-lemma fact_neg_int [simp]: "m < (0::int) \<Longrightarrow> fact m = 0"
-  unfolding fact_int_def by auto
-
-lemma fact_ge_zero_int [simp]: "fact m >= (0::int)"
-  apply (case_tac "m >= 0")
-  apply auto
-  apply (frule fact_gt_zero_int)
-  apply arith
-done
-
-lemma fact_mono_int_aux [rule_format]: "k >= (0::int) \<Longrightarrow>
-    fact (m + k) >= fact m"
-  apply (case_tac "m < 0")
-  apply auto
-  apply (induct k rule: int_ge_induct)
-  apply auto
-  apply (subst add.assoc [symmetric])
-  apply (subst fact_plus_one_int)
-  apply auto
-  apply (erule order_trans)
-  apply (subst mult_le_cancel_right1)
-  apply (subgoal_tac "fact (m + i) >= 0")
-  apply arith
-  apply auto
-done
-
-lemma fact_mono_int: "(m::int) <= n \<Longrightarrow> fact m <= fact n"
-  apply (insert fact_mono_int_aux [of "n - m" "m"])
-  apply auto
-done
-
-text{*Note that @{term "fact 0 = fact 1"}*}
-lemma fact_less_mono_nat: "[| (0::nat) < m; m < n |] ==> fact m < fact n"
-apply (drule_tac m = m in less_imp_Suc_add, auto)
-apply (induct_tac k, auto)
-done
-
-lemma fact_less_mono_int_aux: "k >= 0 \<Longrightarrow> (0::int) < m \<Longrightarrow>
-    fact m < fact ((m + 1) + k)"
-  apply (induct k rule: int_ge_induct)
-  apply (simp add: fact_plus_one_int)
-  apply (subst (2) fact_reduce_int)
-  apply (auto simp add: ac_simps)
-  apply (erule order_less_le_trans)
-  apply auto
-  done
-
-lemma fact_less_mono_int: "(0::int) < m \<Longrightarrow> m < n \<Longrightarrow> fact m < fact n"
-  apply (insert fact_less_mono_int_aux [of "n - (m + 1)" "m"])
-  apply auto
-done
-
-lemma fact_num_eq_if_nat: "fact (m::nat) =
-  (if m=0 then 1 else m * fact (m - 1))"
-by (cases m) auto
-
-lemma fact_add_num_eq_if_nat:
-  "fact ((m::nat) + n) = (if m + n = 0 then 1 else (m + n) * fact (m + n - 1))"
-by (cases "m + n") auto
-
-lemma fact_add_num_eq_if2_nat:
-  "fact ((m::nat) + n) =
-    (if m = 0 then fact n else (m + n) * fact ((m - 1) + n))"
-by (cases m) auto
-
-lemma fact_le_power: "fact n \<le> n^n"
-proof (induct n)
-  case (Suc n)
-  then have "fact n \<le> Suc n ^ n" by (rule le_trans) (simp add: power_mono)
-  then show ?case by (simp add: add_le_mono)
-qed simp
-
-subsection {* @{term fact} and @{term of_nat} *}
-
-lemma of_nat_fact_not_zero [simp]: "of_nat (fact n) \<noteq> (0::'a::semiring_char_0)"
-by auto
-
-lemma of_nat_fact_gt_zero [simp]: "(0::'a::{linordered_semidom}) < of_nat(fact n)" by auto
-
-lemma of_nat_fact_ge_zero [simp]: "(0::'a::linordered_semidom) \<le> of_nat(fact n)"
-by simp
-
-lemma inv_of_nat_fact_gt_zero [simp]: "(0::'a::linordered_field) < inverse (of_nat (fact n))"
-by (auto simp add: positive_imp_inverse_positive)
-
-lemma inv_of_nat_fact_ge_zero [simp]: "(0::'a::linordered_field) \<le> inverse (of_nat (fact n))"
-by (auto intro: order_less_imp_le)
-
-lemma fact_eq_rev_setprod_nat: "fact (k::nat) = (\<Prod>i<k. k - i)"
-  unfolding fact_altdef_nat
-  by (rule setprod.reindex_bij_witness[where i="\<lambda>i. k - i" and j="\<lambda>i. k - i"]) auto
-
-lemma fact_div_fact_le_pow:
-  assumes "r \<le> n" shows "fact n div fact (n - r) \<le> n ^ r"
-proof -
-  have "\<And>r. r \<le> n \<Longrightarrow> \<Prod>{n - r..n} = (n - r) * \<Prod>{Suc (n - r)..n}"
-    by (subst setprod.insert[symmetric]) (auto simp: atLeastAtMost_insertL)
-  with assms show ?thesis
-    by (induct r rule: nat.induct) (auto simp add: fact_div_fact Suc_diff_Suc mult_le_mono)
-qed
-
-lemma fact_numeral:  --{*Evaluation for specific numerals*}
-  "fact (numeral k) = (numeral k) * (fact (pred_numeral k))"
-  by (simp add: numeral_eq_Suc)
-
-
-text {* This development is based on the work of Andy Gordon and
-  Florian Kammueller. *}
-
-subsection {* Basic definitions and lemmas *}
-
-primrec binomial :: "nat \<Rightarrow> nat \<Rightarrow> 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) \<Longrightarrow> 0 < k \<Longrightarrow>
-    (n choose k) = ((n - 1) choose (k - 1)) + ((n - 1) choose k)"
-  by (metis Suc_diff_1 binomial.simps(2) neq0_conv)
-
-lemma binomial_eq_0: "n < k \<Longrightarrow> 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 \<le> n \<Longrightarrow> n choose k > 0"
-  by (induct n k rule: diff_induct) simp_all
-
-lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> 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 \<longleftrightarrow> k \<le> n"
-  by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)
-
-lemma Suc_times_binomial_eq:
-  "Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
-  apply (induct n arbitrary: k, 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{*The absorption property*}
-lemma Suc_times_binomial:
-  "Suc k * (Suc n choose Suc k) = Suc n * (n choose k)"
-  using Suc_times_binomial_eq by auto
-
-text{*This is the well-known version of absorption, but it's harder to use because of the
-  need to reason about division.*}
-lemma binomial_Suc_Suc_eq_times:
-    "(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 of absorption, with -1 instead of Suc.*}
-lemma times_binomial_minus1_eq:
-  "0 < k \<Longrightarrow> k * (n choose 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 \<Longrightarrow> card {B. B \<subseteq> A & card B = 0} = 1"
-  by (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
-
-lemma choose_deconstruct: "finite M \<Longrightarrow> x \<notin> M \<Longrightarrow>
-    {s. s \<subseteq> insert x M \<and> card s = Suc k} =
-    {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}"
-  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 "\<And>A. A \<subseteq> B \<Longrightarrow> finite {x. P x A}"
-  shows "finite {x. \<exists>A \<subseteq> 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 \<Longrightarrow> x \<notin> A \<Longrightarrow>
-    card {B. \<exists>C. C \<subseteq> A \<and> card C = k \<and> B = insert x C} =
-    card {B. B \<subseteq> A & card(B) = k}"
-  apply (rule card_bij_eq [where f = "\<lambda>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 \<Longrightarrow> card {B. B \<subseteq> A \<and> 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 =
-  (\<Sum>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) * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
-    using Suc.hyps by simp
-  also have "\<dots> = a*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +
-                   b*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
-    by (rule distrib_right)
-  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +
-                  (\<Sum>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 "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n+1-k)) +
-                  (\<Sum>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 "\<dots> = a^(n+1) + b^(n+1) +
-                  (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n+1-k)) +
-                  (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n+1-k))"
-    by (simp add: decomp2)
-  also have
-      "\<dots> = a^(n+1) + b^(n+1) +
-            (\<Sum>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 "\<dots> = (\<Sum>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 = (\<Sum>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 binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
-proof (induct n arbitrary: k rule: nat_less_induct)
-  fix n k assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) =
-                      fact m" and kn: "k \<le> 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 \<le> m" by arith
-    from hm h n kn have km: "k \<le> 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 "\<dots> = (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 \<or> k = 0 \<or> k = n \<or> (\<exists>m h. n = Suc m \<and> k = Suc h \<and> h < m)"
-    using kn by presburger
-  ultimately show ?ths by blast
-qed
-
-lemma binomial_fact:
-  assumes kn: "k \<le> n"
-  shows "(of_nat (n choose k) :: 'a::{field,ring_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 choose_row_sum: "(\<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: "(\<Sum>k=0..n. (r+k) choose k) = Suc (r+n) choose n"
-  by (induct n) auto
-
-lemma sum_choose_upper: "(\<Sum>k=0..n. k choose m) = Suc n choose Suc m"
-  by (induct n) auto
-
-lemma natsum_reverse_index:
-  fixes m::nat
-  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)"
-  by (rule setsum.reindex_bij_witness[where i="\<lambda>k. m+n-k" and j="\<lambda>k. m+n-k"]) auto
-
-text{*NW diagonal sum property*}
-lemma sum_choose_diagonal:
-  assumes "m\<le>n" shows "(\<Sum>k=0..m. (n-k) choose (m-k)) = Suc n choose m"
-proof -
-  have "(\<Sum>k=0..m. (n-k) choose (m-k)) = (\<Sum>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 (\<lambda>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 (\<lambda>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} \<union> {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} \<union> {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 \<notin> {1 .. n}" by auto
-  have eq: "insert 0 {1 .. n} = {0..n}" by auto
-  have **: "(\<Prod>n\<in>{1\<Colon>nat..n}. a + of_nat n) = (\<Prod>n\<in>{0\<Colon>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 \<le> n"
-  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k \<noteq> 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 \<longleftrightarrow> 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) \<longleftrightarrow> (\<exists>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) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"
-  unfolding pochhammer_eq_0_iff by auto
-
-lemma pochhammer_neq_0_mono:
-  "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"
-  unfolding pochhammer_eq_0_iff by auto
-
-lemma pochhammer_minus:
-  assumes kn: "k \<le> 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) = (\<Prod>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 \<le> 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 \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
-  where "a gchoose n =
-    (if n = 0 then 1 else (setprod (\<lambda>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 "(\<Prod>i\<Colon>nat\<in>{0\<Colon>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\<Colon>'a)) ^ n = setprod (\<lambda>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_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 \<le> n" and k0: "k\<noteq> 0"
-    from k0 obtain h where h: "k = Suc h" by (cases k) auto
-    from h
-    have eq:"(- 1 :: 'a) ^ k = setprod (\<lambda>i. - 1) {0..h}"
-      by (subst setprod_constant) auto
-    have eq': "(\<Prod>i\<in>{0..h}. of_nat n + - (of_nat i :: 'a)) = (\<Prod>i\<in>{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} \<inter> {n - h .. n} = {}" and
-        eq3: "{1..n - Suc h} \<union> {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 \<Rightarrow> '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 \<or> k = 0 \<or> (k \<le> n \<and> k \<noteq> 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 "\<dots> = ?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 (\<lambda>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 (\<lambda>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 (\<lambda>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: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{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)) + (\<Prod>i\<in>{0\<Colon>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 "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
-    unfolding gbinomial_mult_fact' setprod_nat_ivl_Suc
-    by (simp add: field_simps Suc)
-  also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
-    using eq0
-    by (simp add: Suc setprod_nat_ivl_1_Suc)
-  also have "\<dots> = of_nat (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
-    unfolding gbinomial_mult_fact ..
-  finally show ?thesis by (simp del: fact_Suc)
-qed
-
-lemma gbinomial_reduce_nat:
-  "0 < k \<Longrightarrow> (a::'a::field_char_0) gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)"
-by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc)
-
-
-lemma binomial_symmetric:
-  assumes kn: "k \<le> n"
-  shows "n choose k = n choose (n - k)"
-proof-
-  from kn have kn': "n - k \<le> 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
-
-text{*Contributed by Manuel Eberl, generalised by LCP.
-  Alternative definition of the binomial coefficient as @{term "\<Prod>i<k. (n - i) / (k - i)"} *}
-lemma gbinomial_altdef_of_nat:
-  fixes k :: nat
-    and x :: "'a :: {field_char_0,field_inverse_zero}"
-  shows "x gchoose k = (\<Prod>i<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
-proof -
-  have "(x gchoose k) = (\<Prod>i<k. x - of_nat i) / of_nat (fact k)"
-    unfolding gbinomial_def
-    by (auto simp: gr0_conv_Suc lessThan_Suc_atMost atLeast0AtMost)
-  also have "\<dots> = (\<Prod>i<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
-    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 .
-qed
-
-lemma gbinomial_ge_n_over_k_pow_k:
-  fixes k :: nat
-    and x :: "'a :: linordered_field_inverse_zero"
-  assumes "of_nat k \<le> x"
-  shows "(x / of_nat k :: 'a) ^ k \<le> x gchoose k"
-proof -
-  have x: "0 \<le> x"
-    using assms of_nat_0_le_iff order_trans by blast
-  have "(x / of_nat k :: 'a) ^ k = (\<Prod>i<k. x / of_nat k :: 'a)"
-    by (simp add: setprod_constant)
-  also have "\<dots> \<le> x gchoose k"
-    unfolding gbinomial_altdef_of_nat
-  proof (safe intro!: setprod_mono)
-    fix i :: nat
-    assume ik: "i < k"
-    from assms have "x * of_nat i \<ge> of_nat (i * k)"
-      by (metis mult.commute mult_le_cancel_right of_nat_less_0_iff of_nat_mult)
-    then have "x * of_nat k - x * of_nat i \<le> x * of_nat k - of_nat (i * k)" by arith
-    then have "x * of_nat (k - i) \<le> (x - of_nat i) * of_nat k"
-      using ik
-      by (simp add: algebra_simps zero_less_mult_iff of_nat_diff of_nat_mult)
-    then have "x * of_nat (k - i) \<le> (x - of_nat i) * (of_nat k :: 'a)"
-      unfolding of_nat_mult[symmetric] of_nat_le_iff .
-    with assms show "x / of_nat k \<le> (x - of_nat i) / (of_nat (k - i) :: 'a)"
-      using `i < k` by (simp add: field_simps)
-  qed (simp add: x zero_le_divide_iff)
-  finally show ?thesis .
-qed
-
-text{*Versions of the theorems above for the natural-number version of "choose"*}
-lemma binomial_altdef_of_nat:
-  fixes n k :: nat
-    and x :: "'a :: {field_char_0,field_inverse_zero}"  --{*the point is to constrain @{typ 'a}*}
-  assumes "k \<le> n"
-  shows "of_nat (n choose k) = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
-using assms
-by (simp add: gbinomial_altdef_of_nat binomial_gbinomial of_nat_diff)
-
-lemma binomial_ge_n_over_k_pow_k:
-  fixes k n :: nat
-    and x :: "'a :: linordered_field_inverse_zero"
-  assumes "k \<le> n"
-  shows "(of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)"
-by (simp add: assms gbinomial_ge_n_over_k_pow_k binomial_gbinomial of_nat_diff)
-
-lemma binomial_le_pow:
-  assumes "r \<le> n"
-  shows "n choose r \<le> n ^ r"
-proof -
-  have "n choose r \<le> fact n div fact (n - r)"
-    using `r \<le> 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) \<le> n \<Longrightarrow>
-    n choose k = fact n div (fact k * fact (n - k))"
- by (subst binomial_fact_lemma [symmetric]) auto
-
-lemma choose_dvd_nat: "(k::nat) \<le> n \<Longrightarrow> 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 fact_fact_dvd_fact: fixes k::nat shows "fact k * fact n dvd fact (n + k)"
-by (metis add.commute add_diff_cancel_left' choose_dvd_nat 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 add.assoc 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
-
-text{*The "Subset of a Subset" identity*}
-lemma choose_mult:
-  assumes "k\<le>m" "m\<le>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_one: "(n::nat) choose 1 = n"
-  by simp
-
-(*FIXME: messy and apparently unused*)
-lemma binomial_induct [rule_format]: "(ALL (n::nat). P n n) \<longrightarrow>
-    (ALL n. P (Suc n) 0) \<longrightarrow> (ALL n. (ALL k < n. P n k \<longrightarrow> P n (Suc k) \<longrightarrow>
-    P (Suc n) (Suc k))) \<longrightarrow> (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 card_UNION:
-  assumes "finite A" and "\<forall>k \<in> A. finite k"
-  shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))"
-  (is "?lhs = ?rhs")
-proof -
-  have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))" by simp
-  also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))" (is "_ = nat ?rhs")
-    by(subst setsum_right_distrib) simp
-  also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))"
-    using assms by(subst setsum.Sigma)(auto)
-  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))"
-    by (rule setsum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)
-  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))"
-    using assms by(auto intro!: setsum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])
-  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)))"
-    using assms by(subst setsum.Sigma) auto
-  also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "setsum ?lhs _ = _")
-  proof(rule setsum.cong[OF refl])
-    fix x
-    assume x: "x \<in> \<Union>A"
-    def K \<equiv> "{X \<in> A. x \<in> X}"
-    with `finite A` have K: "finite K" by auto
-    let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<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 \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<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 = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))"
-      by (rule setsum.reindex_cong [where l = snd]) fastforce
-    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)))"
-      using assms by(subst setsum.Sigma) auto
-    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))"
-      by(subst setsum_right_distrib) simp
-    also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))" (is "_ = ?rhs")
-    proof(rule setsum.mono_neutral_cong_right[rule_format])
-      show "{1..card K} \<subseteq> {1..card A}" using `finite A`
-        by(auto simp add: K_def intro: card_mono)
-    next
-      fix i
-      assume "i \<in> {1..card A} - {1..card K}"
-      hence i: "i \<le> card A" "card K < i" by auto
-      have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}"
-        by(auto simp add: K_def)
-      also have "\<dots> = {}" using `finite A` i
-        by(auto simp add: K_def dest: card_mono[rotated 1])
-      finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0"
-        by(simp only:) simp
-    next
-      fix i
-      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)"
-        (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 \<subseteq> K \<and> card I = 0} = {{}}" using assms
-      by(auto simp add: card_eq_0_iff K_def dest: finite_subset)
-    hence "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1"
-      by(subst (2) setsum_head_Suc)(simp_all )
-    also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1"
-      using K by(subst n_subsets[symmetric]) simp_all
-    also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1"
-      by(subst setsum_right_distrib[symmetric]) simp
-    also have "\<dots> =  - ((-1 + 1) ^ card K) + 1"
-      by(subst binomial_ring)(simp add: ac_simps)
-    also have "\<dots> = 1" using x K by(auto simp add: K_def card_gt_0_iff)
-    finally show "?lhs x = 1" .
-  qed
-  also have "nat \<dots> = card (\<Union>A)" by simp
-  finally show ?thesis ..
-qed
-
-text{* The number of nat lists of length @{text m} summing to @{text N} is
-@{term "(N + m - 1) choose N"}: *}
-
-lemma card_length_listsum_rec:
-  assumes "m\<ge>1"
-  shows "card {l::nat list. length l = m \<and> listsum l = N} =
-    (card {l. length l = (m - 1) \<and> listsum l = N} +
-    card {l. length l = m \<and> listsum l + 1 =  N})"
-    (is "card ?C = (card ?A + card ?B)")
-proof -
-  let ?A'="{l. length l = m \<and> listsum l = N \<and> hd l = 0}"
-  let ?B'="{l. length l = m \<and> listsum l = N \<and> hd l \<noteq> 0}"
-  let ?f ="\<lambda> l. 0#l"
-  let ?g ="\<lambda> l. (hd l + 1) # tl l"
-  have 1: "\<And>xs x. xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" by simp
-  have 2: "\<And>xs. (xs::nat list) \<noteq> [] \<Longrightarrow> listsum(tl xs) = listsum xs - hd xs"
-    by(auto simp add: neq_Nil_conv)
-  have f: "bij_betw ?f ?A ?A'"
-    apply(rule bij_betw_byWitness[where f' = tl])
-    using assms
-    by (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv)
-  have 3: "\<And>xs:: nat list. xs \<noteq> [] \<Longrightarrow> hd xs + (listsum xs - hd xs) = listsum xs"
-    by (metis 1 listsum_simps(2) 2)
-  have g: "bij_betw ?g ?B ?B'"
-    apply(rule bij_betw_byWitness[where f' = "\<lambda> l. (hd l - 1) # tl l"])
-    using assms
-    by (auto simp: 2 length_0_conv[symmetric] intro!: 3
-      simp del: length_greater_0_conv length_0_conv)
-  { fix M N :: nat have "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}"
-    using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto }
-    note fin = this
-  have fin_A: "finite ?A" using fin[of _ "N+1"]
-    by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"],
-      auto simp: member_le_listsum_nat less_Suc_eq_le)
-  have fin_B: "finite ?B"
-    by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"],
-      auto simp: member_le_listsum_nat less_Suc_eq_le fin)
-  have uni: "?C = ?A' \<union> ?B'" by auto
-  have disj: "?A' \<inter> ?B' = {}" by auto
-  have "card ?C = card(?A' \<union> ?B')" using uni by simp
-  also have "\<dots> = card ?A + card ?B"
-    using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g]
-      bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B
-    by presburger
-  finally show ?thesis .
-qed
-
-lemma card_length_listsum: --"By Holden Lee, tidied by Tobias Nipkow"
-  "card {l::nat list. size l = m \<and> listsum l = N} = (N + m - 1) choose N"
-proof (cases m)
-  case 0 then show ?thesis
-    by (cases N) (auto simp: cong: conj_cong)
-next
-  case (Suc m')
-    have m: "m\<ge>1" by (simp add: Suc)
-    then show ?thesis
-    proof (induct "N + m - 1" arbitrary: N m)
-      case 0   -- "In the base case, the only solution is [0]."
-      have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {[0]}"
-        by (auto simp: length_Suc_conv)
-      have "m=1 \<and> N=0" using 0 by linarith
-      then show ?case by simp
-    next
-      case (Suc k)
-
-      have c1: "card {l::nat list. size l = (m - 1) \<and> listsum l =  N} =
-        (N + (m - 1) - 1) choose N"
-      proof cases
-        assume "m = 1"
-        with Suc.hyps have "N\<ge>1" by auto
-        with `m = 1` show ?thesis by (simp add: binomial_eq_0)
-      next
-        assume "m \<noteq> 1" thus ?thesis using Suc by fastforce
-      qed
-
-      from Suc have c2: "card {l::nat list. size l = m \<and> listsum l + 1 = N} =
-        (if N>0 then ((N - 1) + m - 1) choose (N - 1) else 0)"
-      proof -
-        have aux: "\<And>m n. n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" by arith
-        from Suc have "N>0 \<Longrightarrow>
-          card {l::nat list. size l = m \<and> listsum l + 1 = N} =
-          ((N - 1) + m - 1) choose (N - 1)" by (simp add: aux)
-        thus ?thesis by auto
-      qed
-
-      from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> listsum l = N} +
-          card {l::nat list. size l = m \<and> listsum l + 1 = N}) = (N + m - 1) choose N"
-        by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def)
-      thus ?case using card_length_listsum_rec[OF Suc.prems] by auto
-    qed
-qed
-
-end
--- a/src/HOL/Import/Import_Setup.thy	Tue Mar 10 15:21:26 2015 +0000
+++ b/src/HOL/Import/Import_Setup.thy	Tue Mar 10 16:12:35 2015 +0000
@@ -6,7 +6,7 @@
 section {* Importer machinery and required theorems *}
 
 theory Import_Setup
-imports Main "~~/src/HOL/Fact"
+imports Main "~~/src/HOL/Binomial"
 keywords "import_type_map" "import_const_map" "import_file" :: thy_decl
 begin
 
--- a/src/HOL/Library/Permutations.thy	Tue Mar 10 15:21:26 2015 +0000
+++ b/src/HOL/Library/Permutations.thy	Tue Mar 10 16:12:35 2015 +0000
@@ -5,7 +5,7 @@
 section {* Permutations, both general and specifically on finite sets.*}
 
 theory Permutations
-imports Fact
+imports Binomial
 begin
 
 subsection {* Transpositions *}
@@ -46,7 +46,7 @@
 
 lemma permutes_imp_bij: "p permutes S \<Longrightarrow> bij_betw p S S"
   by (metis UNIV_I bij_betw_def permutes_image permutes_inj subsetI subset_inj_on)
-   
+
 lemma bij_imp_permutes: "bij_betw p S S \<Longrightarrow> (\<And>x. x \<notin> S \<Longrightarrow> p x = x) \<Longrightarrow> p permutes S"
   unfolding permutes_def bij_betw_def inj_on_def
   by auto (metis image_iff)+
--- a/src/HOL/Number_Theory/Primes.thy	Tue Mar 10 15:21:26 2015 +0000
+++ b/src/HOL/Number_Theory/Primes.thy	Tue Mar 10 16:12:35 2015 +0000
@@ -28,7 +28,7 @@
 section {* Primes *}
 
 theory Primes
-imports "~~/src/HOL/GCD" "~~/src/HOL/Fact"
+imports "~~/src/HOL/GCD" "~~/src/HOL/Binomial"
 begin
 
 declare [[coercion int]]
@@ -72,7 +72,7 @@
   apply (metis gcd_dvd1_nat gcd_dvd2_nat)
   done
 
-lemma prime_int_altdef: 
+lemma prime_int_altdef:
   "prime p = (1 < p \<and> (\<forall>m::int. m \<ge> 0 \<longrightarrow> m dvd p \<longrightarrow>
     m = 1 \<or> m = p))"
   apply (simp add: prime_def)
@@ -90,7 +90,7 @@
 lemma prime_dvd_mult_nat: "prime p \<Longrightarrow> p dvd m * n \<Longrightarrow> p dvd m \<or> p dvd n"
   by (blast intro: coprime_dvd_mult_nat prime_imp_coprime_nat)
 
-lemma prime_dvd_mult_int: 
+lemma prime_dvd_mult_int:
   fixes n::int shows "prime p \<Longrightarrow> p dvd m * n \<Longrightarrow> p dvd m \<or> p dvd n"
   by (blast intro: coprime_dvd_mult_int prime_imp_coprime_int)
 
@@ -99,7 +99,7 @@
   by (rule iffI, rule prime_dvd_mult_nat, auto)
 
 lemma prime_dvd_mult_eq_int [simp]:
-  fixes n::int 
+  fixes n::int
   shows "prime p \<Longrightarrow> p dvd m * n = (p dvd m \<or> p dvd n)"
   by (rule iffI, rule prime_dvd_mult_int, auto)
 
@@ -121,7 +121,7 @@
   by (cases n) (auto elim: prime_dvd_power_nat)
 
 lemma prime_dvd_power_int_iff:
-  fixes x::int 
+  fixes x::int
   shows "prime p \<Longrightarrow> n > 0 \<Longrightarrow> p dvd x^n \<longleftrightarrow> p dvd x"
   by (cases n) (auto elim: prime_dvd_power_int)
 
@@ -226,14 +226,14 @@
 
 lemma next_prime_bound: "\<exists>p. prime p \<and> n < p \<and> p <= fact n + 1"
 proof-
-  have f1: "fact n + 1 \<noteq> 1" using fact_ge_one_nat [of n] by arith 
+  have f1: "fact n + 1 \<noteq> 1" using fact_ge_one_nat [of n] by arith
   from prime_factor_nat [OF f1]
   obtain p where "prime p" and "p dvd fact n + 1" by auto
   then have "p \<le> fact n + 1" apply (intro dvd_imp_le) apply auto done
   { assume "p \<le> n"
-    from `prime p` have "p \<ge> 1" 
+    from `prime p` have "p \<ge> 1"
       by (cases p, simp_all)
-    with `p <= n` have "p dvd fact n" 
+    with `p <= n` have "p dvd fact n"
       by (intro dvd_fact_nat)
     with `p dvd fact n + 1` have "p dvd fact n + 1 - fact n"
       by (rule dvd_diff_nat)
@@ -245,7 +245,7 @@
   with `prime p` and `p <= fact n + 1` show ?thesis by auto
 qed
 
-lemma bigger_prime: "\<exists>p. prime p \<and> p > (n::nat)" 
+lemma bigger_prime: "\<exists>p. prime p \<and> p > (n::nat)"
   using next_prime_bound by auto
 
 lemma primes_infinite: "\<not> (finite {(p::nat). prime p})"
@@ -263,12 +263,12 @@
 
 text{*Versions for type nat only*}
 
-lemma prime_product: 
+lemma prime_product:
   fixes p::nat
   assumes "prime (p * q)"
   shows "p = 1 \<or> q = 1"
 proof -
-  from assms have 
+  from assms have
     "1 < p * q" and P: "\<And>m. m dvd p * q \<Longrightarrow> m = 1 \<or> m = p * q"
     unfolding prime_nat_def by auto
   from `1 < p * q` have "p \<noteq> 0" by (cases p) auto
@@ -278,7 +278,7 @@
   then show ?thesis by (simp add: Q)
 qed
 
-lemma prime_exp: 
+lemma prime_exp:
   fixes p::nat
   shows "prime (p^n) \<longleftrightarrow> prime p \<and> n = 1"
 proof(induct n)
@@ -301,7 +301,7 @@
   ultimately show ?case by blast
 qed
 
-lemma prime_power_mult: 
+lemma prime_power_mult:
   fixes p::nat
   assumes p: "prime p" and xy: "x * y = p ^ k"
   shows "\<exists>i j. x = p ^i \<and> y = p^ j"
@@ -312,28 +312,28 @@
   case (Suc k x y)
   from Suc.prems have pxy: "p dvd x*y" by auto
   from Primes.prime_dvd_mult_nat [OF p pxy] have pxyc: "p dvd x \<or> p dvd y" .
-  from p have p0: "p \<noteq> 0" by - (rule ccontr, simp) 
+  from p have p0: "p \<noteq> 0" by - (rule ccontr, simp)
   {assume px: "p dvd x"
     then obtain d where d: "x = p*d" unfolding dvd_def by blast
     from Suc.prems d  have "p*d*y = p^Suc k" by simp
     hence th: "d*y = p^k" using p0 by simp
     from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "y = p^j" by blast
-    with d have "x = p^Suc i" by simp 
+    with d have "x = p^Suc i" by simp
     with ij(2) have ?case by blast}
-  moreover 
+  moreover
   {assume px: "p dvd y"
     then obtain d where d: "y = p*d" unfolding dvd_def by blast
     from Suc.prems d  have "p*d*x = p^Suc k" by (simp add: mult.commute)
     hence th: "d*x = p^k" using p0 by simp
     from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "x = p^j" by blast
-    with d have "y = p^Suc i" by simp 
+    with d have "y = p^Suc i" by simp
     with ij(2) have ?case by blast}
   ultimately show ?case  using pxyc by blast
 qed
 
-lemma prime_power_exp: 
+lemma prime_power_exp:
   fixes p::nat
-  assumes p: "prime p" and n: "n \<noteq> 0" 
+  assumes p: "prime p" and n: "n \<noteq> 0"
     and xn: "x^n = p^k" shows "\<exists>i. x = p^i"
   using n xn
 proof(induct n arbitrary: k)
@@ -343,7 +343,7 @@
   {assume "n = 0" with Suc have ?case by simp (rule exI[where x="k"], simp)}
   moreover
   {assume n: "n \<noteq> 0"
-    from prime_power_mult[OF p th] 
+    from prime_power_mult[OF p th]
     obtain i j where ij: "x = p^i" "x^n = p^j"by blast
     from Suc.hyps[OF n ij(2)] have ?case .}
   ultimately show ?case by blast
@@ -351,14 +351,14 @@
 
 lemma divides_primepow:
   fixes p::nat
-  assumes p: "prime p" 
+  assumes p: "prime p"
   shows "d dvd p^k \<longleftrightarrow> (\<exists> i. i \<le> k \<and> d = p ^i)"
 proof
-  assume H: "d dvd p^k" then obtain e where e: "d*e = p^k" 
+  assume H: "d dvd p^k" then obtain e where e: "d*e = p^k"
     unfolding dvd_def  apply (auto simp add: mult.commute) by blast
   from prime_power_mult[OF p e] obtain i j where ij: "d = p^i" "e=p^j" by blast
   from e ij have "p^(i + j) = p^k" by (simp add: power_add)
-  hence "i + j = k" using p prime_gt_1_nat power_inject_exp[of p "i+j" k] by simp 
+  hence "i + j = k" using p prime_gt_1_nat power_inject_exp[of p "i+j" k] by simp
   hence "i \<le> k" by arith
   with ij(1) show "\<exists>i\<le>k. d = p ^ i" by blast
 next
@@ -375,16 +375,16 @@
 lemma bezout_gcd_nat:
   fixes a::nat shows "\<exists>x y. a * x - b * y = gcd a b \<or> b * x - a * y = gcd a b"
   using bezout_nat[of a b]
-by (metis bezout_nat diff_add_inverse gcd_add_mult_nat gcd_nat.commute 
-  gcd_nat.right_neutral mult_0) 
+by (metis bezout_nat diff_add_inverse gcd_add_mult_nat gcd_nat.commute
+  gcd_nat.right_neutral mult_0)
 
 lemma gcd_bezout_sum_nat:
-  fixes a::nat 
-  assumes "a * x + b * y = d" 
+  fixes a::nat
+  assumes "a * x + b * y = d"
   shows "gcd a b dvd d"
 proof-
   let ?g = "gcd a b"
-    have dv: "?g dvd a*x" "?g dvd b * y" 
+    have dv: "?g dvd a*x" "?g dvd b * y"
       by simp_all
     from dvd_add[OF dv] assms
     show ?thesis by auto
@@ -393,19 +393,19 @@
 
 text {* A binary form of the Chinese Remainder Theorem. *}
 
-lemma chinese_remainder: 
+lemma chinese_remainder:
   fixes a::nat  assumes ab: "coprime a b" and a: "a \<noteq> 0" and b: "b \<noteq> 0"
   shows "\<exists>x q1 q2. x = u + q1 * a \<and> x = v + q2 * b"
 proof-
   from bezout_add_strong_nat[OF a, of b] bezout_add_strong_nat[OF b, of a]
-  obtain d1 x1 y1 d2 x2 y2 where dxy1: "d1 dvd a" "d1 dvd b" "a * x1 = b * y1 + d1" 
+  obtain d1 x1 y1 d2 x2 y2 where dxy1: "d1 dvd a" "d1 dvd b" "a * x1 = b * y1 + d1"
     and dxy2: "d2 dvd b" "d2 dvd a" "b * x2 = a * y2 + d2" by blast
   then have d12: "d1 = 1" "d2 =1"
     by (metis ab coprime_nat)+
   let ?x = "v * a * x1 + u * b * x2"
   let ?q1 = "v * x1 + u * y2"
   let ?q2 = "v * y1 + u * x2"
-  from dxy2(3)[simplified d12] dxy1(3)[simplified d12] 
+  from dxy2(3)[simplified d12] dxy1(3)[simplified d12]
   have "?x = u + ?q1 * a" "?x = v + ?q2 * b"
     by algebra+
   thus ?thesis by blast
@@ -418,14 +418,14 @@
   shows "\<exists>x y. a * x = b * y + 1"
 by (metis assms bezout_nat gcd_nat.left_neutral)
 
-lemma bezout_prime: 
+lemma bezout_prime:
   assumes p: "prime p" and pa: "\<not> p dvd a"
   shows "\<exists>x y. a*x = Suc (p*y)"
 proof-
   have ap: "coprime a p"
-    by (metis gcd_nat.commute p pa prime_imp_coprime_nat) 
+    by (metis gcd_nat.commute p pa prime_imp_coprime_nat)
   from coprime_bezout_strong[OF ap] show ?thesis
-    by (metis Suc_eq_plus1 gcd_lcm_complete_lattice_nat.bot.extremum pa) 
+    by (metis Suc_eq_plus1 gcd_lcm_complete_lattice_nat.bot.extremum pa)
 qed
 
 end
--- a/src/HOL/Transcendental.thy	Tue Mar 10 15:21:26 2015 +0000
+++ b/src/HOL/Transcendental.thy	Tue Mar 10 16:12:35 2015 +0000
@@ -7,7 +7,7 @@
 section{*Power Series, Transcendental Functions etc.*}
 
 theory Transcendental
-imports Fact Series Deriv NthRoot
+imports Binomial Series Deriv NthRoot
 begin
 
 lemma root_test_convergence:
@@ -81,13 +81,13 @@
 lemma power_diff_1_eq:
   fixes x :: "'a::{comm_ring,monoid_mult}"
   shows "n \<noteq> 0 \<Longrightarrow> x^n - 1 = (x - 1) * (\<Sum>i<n. (x^i))"
-using lemma_realpow_diff_sumr2 [of x _ 1] 
+using lemma_realpow_diff_sumr2 [of x _ 1]
   by (cases n) auto
 
 lemma one_diff_power_eq':
   fixes x :: "'a::{comm_ring,monoid_mult}"
   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^(n - Suc i))"
-using lemma_realpow_diff_sumr2 [of 1 _ x] 
+using lemma_realpow_diff_sumr2 [of 1 _ x]
   by (cases n) auto
 
 lemma one_diff_power_eq:
@@ -419,7 +419,7 @@
       by auto
     ultimately show ?thesis by auto
   qed
-  then show ?summable and ?pos and ?neg and ?f and ?g 
+  then show ?summable and ?pos and ?neg and ?f and ?g
     by safe
 qed
 
@@ -1171,16 +1171,16 @@
 lemma abs_exp_cancel [simp]: "\<bar>exp x::real\<bar> = exp x"
   by simp
 
-(*FIXME: superseded by exp_of_nat_mult*) 
-lemma exp_real_of_nat_mult: "exp(real n * x) = exp(x) ^ n" 
+(*FIXME: superseded by exp_of_nat_mult*)
+lemma exp_real_of_nat_mult: "exp(real n * x) = exp(x) ^ n"
   by (induct n) (auto simp add: real_of_nat_Suc distrib_left exp_add mult.commute)
-  
+
 text {* Strict monotonicity of exponential. *}
 
-lemma exp_ge_add_one_self_aux: 
+lemma exp_ge_add_one_self_aux:
   assumes "0 \<le> (x::real)" shows "1+x \<le> exp(x)"
 using order_le_imp_less_or_eq [OF assms]
-proof 
+proof
   assume "0 < x"
   have "1+x \<le> (\<Sum>n<2. inverse (real (fact n)) * x ^ n)"
     by (auto simp add: numeral_2_eq_2)
@@ -1189,7 +1189,7 @@
     using `0 < x`
     apply (auto  simp add:  zero_le_mult_iff)
     done
-  finally show "1+x \<le> exp x" 
+  finally show "1+x \<le> exp x"
     by (simp add: exp_def)
 next
   assume "0 = x"
@@ -1443,7 +1443,7 @@
 proof -
   have "exp x = suminf (\<lambda>n. inverse(fact n) * (x ^ n))"
     by (simp add: exp_def)
-  also from summable_exp have "... = (\<Sum> n. inverse(fact(n+2)) * (x ^ (n+2))) + 
+  also from summable_exp have "... = (\<Sum> n. inverse(fact(n+2)) * (x ^ (n+2))) +
     (\<Sum> n::nat<2. inverse(fact n) * (x ^ n))" (is "_ = _ + ?a")
     by (rule suminf_split_initial_segment)
   also have "?a = 1 + x"
@@ -1536,7 +1536,7 @@
   ultimately have "1 - x <= 1 / (1 + x + x\<^sup>2)"
     by (elim mult_imp_le_div_pos)
   also have "... <= 1 / exp x"
-    by (metis a abs_one b exp_bound exp_gt_zero frac_le less_eq_real_def real_sqrt_abs 
+    by (metis a abs_one b exp_bound exp_gt_zero frac_le less_eq_real_def real_sqrt_abs
               real_sqrt_pow2_iff real_sqrt_power)
   also have "... = exp (-x)"
     by (auto simp add: exp_minus divide_inverse)
@@ -1584,7 +1584,7 @@
   qed
   finally have "exp (x - x\<^sup>2) <= exp (ln (1 + x))" .
   thus ?thesis
-    by (metis exp_le_cancel_iff) 
+    by (metis exp_le_cancel_iff)
 qed
 
 lemma ln_one_minus_pos_lower_bound:
@@ -1690,7 +1690,7 @@
   also have "... = 1 + (y - x) / x"
     using x a by (simp add: field_simps)
   also have "x * ln(1 + (y - x) / x) <= x * ((y - x) / x)"
-    using x a 
+    using x a
     by (intro mult_left_mono ln_add_one_self_le_self) simp_all
   also have "... = y - x" using a by simp
   also have "... = (y - x) * ln (exp 1)" by simp
@@ -2204,7 +2204,7 @@
   unfolding powr_def exp_inj_iff by simp
 
 lemma ln_powr_bound: "1 <= x ==> 0 < a ==> ln x <= (x powr a) / a"
-  by (metis less_eq_real_def ln_less_self mult_imp_le_div_pos ln_powr mult.commute 
+  by (metis less_eq_real_def ln_less_self mult_imp_le_div_pos ln_powr mult.commute
             order.strict_trans2 powr_gt_zero zero_less_one)
 
 lemma ln_powr_bound2:
@@ -2302,7 +2302,7 @@
   have "((\<lambda>y. ln (1 + x * y)::real) has_real_derivative 1 * x) (at 0)"
     by (auto intro!: derivative_eq_intros)
   then have "((\<lambda>y. ln (1 + x * y) / y) ---> x) (at 0)"
-    by (auto simp add: has_field_derivative_def field_has_derivative_at) 
+    by (auto simp add: has_field_derivative_def field_has_derivative_at)
   then have *: "((\<lambda>y. exp (ln (1 + x * y) / y)) ---> exp x) (at 0)"
     by (rule tendsto_intros)
   then show ?thesis
@@ -2367,15 +2367,15 @@
   unfolding cos_coeff_def sin_coeff_def
   by (simp del: mult_Suc) (auto elim: oddE)
 
-lemma summable_norm_sin: 
+lemma summable_norm_sin:
   fixes x :: "'a::{real_normed_algebra_1,banach}"
   shows "summable (\<lambda>n. norm (sin_coeff n *\<^sub>R x^n))"
-  unfolding sin_coeff_def 
+  unfolding sin_coeff_def
   apply (rule summable_comparison_test [OF _ summable_norm_exp [where x=x]])
   apply (auto simp: divide_inverse abs_mult power_abs [symmetric] zero_le_mult_iff)
   done
 
-lemma summable_norm_cos: 
+lemma summable_norm_cos:
   fixes x :: "'a::{real_normed_algebra_1,banach}"
   shows "summable (\<lambda>n. norm (cos_coeff n *\<^sub>R x ^ n))"
   unfolding cos_coeff_def
@@ -2405,7 +2405,7 @@
     by (rule sin_converges)
   finally have "(\<lambda>n. of_real (sin_coeff n *\<^sub>R x^n)) sums (sin (of_real x))" .
   then show ?thesis
-    using sums_unique2 sums_of_real [OF sin_converges] 
+    using sums_unique2 sums_of_real [OF sin_converges]
     by blast
 qed
 
@@ -2423,7 +2423,7 @@
     by (rule cos_converges)
   finally have "(\<lambda>n. of_real (cos_coeff n *\<^sub>R x^n)) sums (cos (of_real x))" .
   then show ?thesis
-    using sums_unique2 sums_of_real [OF cos_converges]  
+    using sums_unique2 sums_of_real [OF cos_converges]
     by blast
 qed
 
@@ -2441,22 +2441,22 @@
   unfolding sin_def cos_def scaleR_conv_of_real
   apply (rule DERIV_cong)
   apply (rule termdiffs [where K="of_real (norm x) + 1 :: 'a"])
-  apply (simp_all add: norm_less_p1 diffs_of_real diffs_sin_coeff diffs_cos_coeff 
+  apply (simp_all add: norm_less_p1 diffs_of_real diffs_sin_coeff diffs_cos_coeff
               summable_minus_iff scaleR_conv_of_real [symmetric]
               summable_norm_sin [THEN summable_norm_cancel]
               summable_norm_cos [THEN summable_norm_cancel])
   done
-  
+
 declare DERIV_sin[THEN DERIV_chain2, derivative_intros]
 
-lemma DERIV_cos [simp]: 
+lemma DERIV_cos [simp]:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "DERIV cos x :> -sin(x)"
   unfolding sin_def cos_def scaleR_conv_of_real
   apply (rule DERIV_cong)
   apply (rule termdiffs [where K="of_real (norm x) + 1 :: 'a"])
-  apply (simp_all add: norm_less_p1 diffs_of_real diffs_minus suminf_minus 
-              diffs_sin_coeff diffs_cos_coeff 
+  apply (simp_all add: norm_less_p1 diffs_of_real diffs_minus suminf_minus
+              diffs_sin_coeff diffs_cos_coeff
               summable_minus_iff scaleR_conv_of_real [symmetric]
               summable_norm_sin [THEN summable_norm_cancel]
               summable_norm_cos [THEN summable_norm_cancel])
@@ -2469,7 +2469,7 @@
   shows "isCont sin x"
   by (rule DERIV_sin [THEN DERIV_isCont])
 
-lemma isCont_cos: 
+lemma isCont_cos:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "isCont cos x"
   by (rule DERIV_cos [THEN DERIV_isCont])
@@ -2481,7 +2481,7 @@
 
 (*FIXME A CONTEXT FOR F WOULD BE BETTER*)
 
-lemma isCont_cos' [simp]: 
+lemma isCont_cos' [simp]:
   fixes f:: "_ \<Rightarrow> 'a::{real_normed_field,banach}"
   shows "isCont f a \<Longrightarrow> isCont (\<lambda>x. cos (f x)) a"
   by (rule isCont_o2 [OF _ isCont_cos])
@@ -2545,23 +2545,23 @@
 subsection {*Deriving the Addition Formulas*}
 
 text{*The the product of two cosine series*}
-lemma cos_x_cos_y: 
+lemma cos_x_cos_y:
   fixes x :: "'a::{real_normed_field,banach}"
-  shows "(\<lambda>p. \<Sum>n\<le>p. 
-          if even p \<and> even n 
-          then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) 
+  shows "(\<lambda>p. \<Sum>n\<le>p.
+          if even p \<and> even n
+          then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)
          sums (cos x * cos y)"
 proof -
   { fix n p::nat
     assume "n\<le>p"
     then have *: "even n \<Longrightarrow> even p \<Longrightarrow> (-1) ^ (n div 2) * (-1) ^ ((p - n) div 2) = (-1 :: real) ^ (p div 2)"
       by (metis div_add power_add le_add_diff_inverse odd_add)
-    have "(cos_coeff n * cos_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)) = 
+    have "(cos_coeff n * cos_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)) =
           (if even p \<and> even n then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)"
     using `n\<le>p`
       by (auto simp: * algebra_simps cos_coeff_def binomial_fact real_of_nat_def)
-  } 
-  then have "(\<lambda>p. \<Sum>n\<le>p. if even p \<and> even n 
+  }
+  then have "(\<lambda>p. \<Sum>n\<le>p. if even p \<and> even n
                   then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) =
              (\<lambda>p. \<Sum>n\<le>p. (cos_coeff n * cos_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)))"
     by simp
@@ -2574,11 +2574,11 @@
 qed
 
 text{*The product of two sine series*}
-lemma sin_x_sin_y: 
+lemma sin_x_sin_y:
   fixes x :: "'a::{real_normed_field,banach}"
-  shows "(\<lambda>p. \<Sum>n\<le>p. 
-          if even p \<and> odd n 
-               then - ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) 
+  shows "(\<lambda>p. \<Sum>n\<le>p.
+          if even p \<and> odd n
+               then - ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)
          sums (sin x * sin y)"
 proof -
   { fix n p::nat
@@ -2594,13 +2594,13 @@
         apply (metis (no_types) One_nat_def Suc_1 le_div_geq minus_minus mult.left_neutral mult_minus_left power.simps(2) zero_less_Suc)
         done
     } then
-    have "(sin_coeff n * sin_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)) = 
-          (if even p \<and> odd n 
+    have "(sin_coeff n * sin_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)) =
+          (if even p \<and> odd n
           then -((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)"
     using `n\<le>p`
       by (auto simp:  algebra_simps sin_coeff_def binomial_fact real_of_nat_def)
-  } 
-  then have "(\<lambda>p. \<Sum>n\<le>p. if even p \<and> odd n 
+  }
+  then have "(\<lambda>p. \<Sum>n\<le>p. if even p \<and> odd n
                then - ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) =
              (\<lambda>p. \<Sum>n\<le>p. (sin_coeff n * sin_coeff (p - n)) *\<^sub>R (x^n * y^(p-n)))"
     by simp
@@ -2612,18 +2612,18 @@
   finally show ?thesis .
 qed
 
-lemma sums_cos_x_plus_y: 
+lemma sums_cos_x_plus_y:
   fixes x :: "'a::{real_normed_field,banach}"
   shows
-  "(\<lambda>p. \<Sum>n\<le>p. if even p 
-               then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) 
-               else 0) 
+  "(\<lambda>p. \<Sum>n\<le>p. if even p
+               then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n)
+               else 0)
         sums cos (x + y)"
 proof -
   { fix p::nat
     have "(\<Sum>n\<le>p. if even p
                   then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n)
-                  else 0) = 
+                  else 0) =
           (if even p
                   then \<Sum>n\<le>p. ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n)
                   else 0)"
@@ -2637,11 +2637,11 @@
     finally have "(\<Sum>n\<le>p. if even p
                   then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n)
                   else 0) = cos_coeff p *\<^sub>R ((x + y) ^ p)" .
-  }  
-  then have "(\<lambda>p. \<Sum>n\<le>p. 
-               if even p 
-               then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) 
-               else 0) 
+  }
+  then have "(\<lambda>p. \<Sum>n\<le>p.
+               if even p
+               then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n)
+               else 0)
         = (\<lambda>p. cos_coeff p *\<^sub>R ((x+y)^p))"
         by simp
    also have "... sums cos (x + y)"
@@ -2649,22 +2649,22 @@
    finally show ?thesis .
 qed
 
-theorem cos_add: 
+theorem cos_add:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "cos (x + y) = cos x * cos y - sin x * sin y"
 proof -
   { fix n p::nat
     assume "n\<le>p"
-    then have "(if even p \<and> even n 
+    then have "(if even p \<and> even n
                then ((- 1) ^ (p div 2) * int (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0) -
-          (if even p \<and> odd n 
+          (if even p \<and> odd n
                then - ((- 1) ^ (p div 2) * int (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)
-          = (if even p 
+          = (if even p
                then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)"
       by simp
-  }   
-  then have "(\<lambda>p. \<Sum>n\<le>p. (if even p 
-               then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0)) 
+  }
+  then have "(\<lambda>p. \<Sum>n\<le>p. (if even p
+               then ((-1) ^ (p div 2) * (p choose n) / of_nat (fact p)) *\<^sub>R (x^n) * y^(p-n) else 0))
         sums (cos x * cos y - sin x * sin y)"
     using sums_diff [OF cos_x_cos_y [of x y] sin_x_sin_y [of x y]]
     by (simp add: setsum_subtractf [symmetric])
@@ -2683,7 +2683,7 @@
 lemma sin_minus [simp]:
   fixes x :: "'a::{real_normed_algebra_1,banach}"
   shows "sin (-x) = -sin(x)"
-using sin_minus_converges [of x] 
+using sin_minus_converges [of x]
 by (auto simp: sin_def summable_norm_sin [THEN summable_norm_cancel] suminf_minus sums_iff equation_minus_iff)
 
 lemma cos_minus_converges: "(\<lambda>n. (cos_coeff n *\<^sub>R (-x)^n)) sums cos(x)"
@@ -2698,72 +2698,72 @@
   fixes x :: "'a::{real_normed_algebra_1,banach}"
   shows "cos (-x) = cos(x)"
 using cos_minus_converges [of x]
-by (simp add: cos_def summable_norm_cos [THEN summable_norm_cancel] 
+by (simp add: cos_def summable_norm_cos [THEN summable_norm_cancel]
               suminf_minus sums_iff equation_minus_iff)
 
-    
-lemma sin_cos_squared_add [simp]: 
+
+lemma sin_cos_squared_add [simp]:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "(sin x)\<^sup>2 + (cos x)\<^sup>2 = 1"
 using cos_add [of x "-x"]
 by (simp add: power2_eq_square algebra_simps)
 
-lemma sin_cos_squared_add2 [simp]:  
+lemma sin_cos_squared_add2 [simp]:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "(cos x)\<^sup>2 + (sin x)\<^sup>2 = 1"
   by (subst add.commute, rule sin_cos_squared_add)
 
-lemma sin_cos_squared_add3 [simp]:  
+lemma sin_cos_squared_add3 [simp]:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "cos x * cos x + sin x * sin x = 1"
   using sin_cos_squared_add2 [unfolded power2_eq_square] .
 
-lemma sin_squared_eq:  
+lemma sin_squared_eq:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "(sin x)\<^sup>2 = 1 - (cos x)\<^sup>2"
   unfolding eq_diff_eq by (rule sin_cos_squared_add)
 
-lemma cos_squared_eq:  
+lemma cos_squared_eq:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "(cos x)\<^sup>2 = 1 - (sin x)\<^sup>2"
   unfolding eq_diff_eq by (rule sin_cos_squared_add2)
 
-lemma abs_sin_le_one [simp]:  
+lemma abs_sin_le_one [simp]:
   fixes x :: real
   shows "\<bar>sin x\<bar> \<le> 1"
   by (rule power2_le_imp_le, simp_all add: sin_squared_eq)
 
-lemma sin_ge_minus_one [simp]: 
+lemma sin_ge_minus_one [simp]:
   fixes x :: real
   shows "-1 \<le> sin x"
   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
 
-lemma sin_le_one [simp]: 
+lemma sin_le_one [simp]:
   fixes x :: real
   shows "sin x \<le> 1"
   using abs_sin_le_one [of x] unfolding abs_le_iff by simp
 
-lemma abs_cos_le_one [simp]: 
+lemma abs_cos_le_one [simp]:
   fixes x :: real
   shows "\<bar>cos x\<bar> \<le> 1"
   by (rule power2_le_imp_le, simp_all add: cos_squared_eq)
 
-lemma cos_ge_minus_one [simp]: 
+lemma cos_ge_minus_one [simp]:
   fixes x :: real
   shows "-1 \<le> cos x"
   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
 
-lemma cos_le_one [simp]: 
+lemma cos_le_one [simp]:
   fixes x :: real
   shows "cos x \<le> 1"
   using abs_cos_le_one [of x] unfolding abs_le_iff by simp
 
-lemma cos_diff: 
+lemma cos_diff:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "cos (x - y) = cos x * cos y + sin x * sin y"
   using cos_add [of x "- y"] by simp
 
-lemma cos_double: 
+lemma cos_double:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "cos(2*x) = (cos x)\<^sup>2 - (sin x)\<^sup>2"
   using cos_add [where x=x and y=x]
@@ -2786,7 +2786,7 @@
    hence define pi.*}
 
 lemma sin_paired:
-  fixes x :: real 
+  fixes x :: real
   shows "(\<lambda>n. (- 1) ^ n /(real (fact (2 * n + 1))) * x ^ (2 * n + 1)) sums  sin x"
 proof -
   have "(\<lambda>n. \<Sum>k = n*2..<n * 2 + 2. sin_coeff k * x ^ k) sums sin x"
@@ -2797,7 +2797,7 @@
 qed
 
 lemma sin_gt_zero_02:
-  fixes x :: real 
+  fixes x :: real
   assumes "0 < x" and "x < 2"
   shows "0 < sin x"
 proof -
@@ -2824,12 +2824,12 @@
 qed
 
 lemma cos_double_less_one:
-  fixes x :: real 
+  fixes x :: real
   shows "0 < x \<Longrightarrow> x < 2 \<Longrightarrow> cos (2 * x) < 1"
   using sin_gt_zero_02 [where x = x] by (auto simp: cos_squared_eq cos_double)
 
 lemma cos_paired:
-  fixes x :: real 
+  fixes x :: real
   shows "(\<lambda>n. (- 1) ^ n /(real (fact (2 * n))) * x ^ (2 * n)) sums cos x"
 proof -
   have "(\<lambda>n. \<Sum>k = n * 2..<n * 2 + 2. cos_coeff k * x ^ k) sums cos x"
@@ -2927,7 +2927,7 @@
 lemma cos_pi_half [simp]: "cos (pi / 2) = 0"
   by (simp add: pi_half cos_is_zero [THEN theI'])
 
-lemma cos_of_real_pi_half [simp]: 
+lemma cos_of_real_pi_half [simp]:
   fixes x :: "'a :: {real_field,banach,real_normed_algebra_1}"
   shows "cos ((of_real pi / 2) :: 'a) = 0"
 by (metis cos_pi_half cos_of_real eq_numeral_simps(4) nonzero_of_real_divide of_real_0 of_real_numeral)
@@ -2976,7 +2976,7 @@
 lemma sin_of_real_pi_half [simp]:
   fixes x :: "'a :: {real_field,banach,real_normed_algebra_1}"
   shows "sin ((of_real pi / 2) :: 'a) = 1"
-  using sin_pi_half 
+  using sin_pi_half
 by (metis sin_pi_half eq_numeral_simps(4) nonzero_of_real_divide of_real_1 of_real_numeral sin_of_real)
 
 lemma sin_cos_eq:
@@ -2995,7 +2995,7 @@
   using sin_cos_eq [of "of_real pi / 2 - x"]
   by simp
 
-lemma sin_add: 
+lemma sin_add:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "sin (x + y) = sin x * cos y + cos x * sin y"
   using cos_add [of "of_real pi / 2 - x" "-y"]
@@ -3006,7 +3006,7 @@
   shows "sin (x - y) = sin x * cos y - cos x * sin y"
   using sin_add [of x "- y"] by simp
 
-lemma sin_double: 
+lemma sin_double:
   fixes x :: "'a::{real_normed_field,banach}"
   shows "sin(2 * x) = 2 * sin x * cos x"
   using sin_add [where x=x and y=x] by simp
@@ -3017,9 +3017,9 @@
   by (simp add: cos_of_real)
 
 lemma sin_of_real_pi [simp]: "sin (of_real pi) = 0"
-  using sin_add [where x = "pi/2" and y = "pi/2"] 
+  using sin_add [where x = "pi/2" and y = "pi/2"]
   by (simp add: sin_of_real)
-  
+
 lemma cos_pi [simp]: "cos pi = -1"
   using cos_add [where x = "pi/2" and y = "pi/2"] by simp
 
@@ -3241,7 +3241,7 @@
     done
 next
   fix n::int
-  assume "odd n" 
+  assume "odd n"
   then show "cos (real n * (pi / 2)) = 0"
     apply (simp add: cos_zero_iff)
     apply (case_tac n rule: int_cases2, simp)
@@ -3250,7 +3250,7 @@
     done
 qed
 
-lemma sin_zero_iff_int:  
+lemma sin_zero_iff_int:
      "sin x = 0 \<longleftrightarrow> (\<exists>n::int. even n & (x = real n * (pi/2)))"
 proof safe
   assume "sin x = 0"
@@ -3261,7 +3261,7 @@
     done
 next
   fix n::int
-  assume "even n" 
+  assume "even n"
   then show "sin (real n * (pi / 2)) = 0"
     apply (simp add: sin_zero_iff)
     apply (case_tac n rule: int_cases2, simp)
@@ -3271,8 +3271,8 @@
 qed
 
 lemma sin_zero_iff_int2: "sin x = 0 \<longleftrightarrow> (\<exists>n::int. x = real n * pi)"
-  apply (simp only: sin_zero_iff_int)     
-  apply (safe elim!: evenE)     
+  apply (simp only: sin_zero_iff_int)
+  apply (safe elim!: evenE)
   apply (simp_all add: field_simps)
   using dvd_triv_left by fastforce
 
@@ -3337,7 +3337,7 @@
     using pi_ge_two and assms by auto
   from cos_monotone_0_pi'[OF this] show ?thesis
     unfolding minus_sin_cos_eq[symmetric]
-    by (metis minus_sin_cos_eq mult.right_neutral neg_le_iff_le of_real_def real_scaleR_def) 
+    by (metis minus_sin_cos_eq mult.right_neutral neg_le_iff_le of_real_def real_scaleR_def)
 qed
 
 lemma sin_x_le_x:
@@ -3401,14 +3401,14 @@
 
 lemma tan_add:
   fixes x :: "'a::{real_normed_field,banach}"
-  shows 
+  shows
      "\<lbrakk>cos x \<noteq> 0; cos y \<noteq> 0; cos (x + y) \<noteq> 0\<rbrakk>
       \<Longrightarrow> tan(x + y) = (tan(x) + tan(y))/(1 - tan(x) * tan(y))"
       by (simp add: add_tan_eq lemma_tan_add1 field_simps) (simp add: tan_def)
 
 lemma tan_double:
   fixes x :: "'a::{real_normed_field,banach}"
-  shows 
+  shows
      "\<lbrakk>cos x \<noteq> 0; cos (2 * x) \<noteq> 0\<rbrakk>
       \<Longrightarrow> tan (2 * x) = (2 * tan x) / (1 - (tan x)\<^sup>2)"
   using tan_add [of x x] by (simp add: power2_eq_square)
@@ -3463,7 +3463,7 @@
 
 lemma continuous_within_tan [continuous_intros]:
   fixes f :: "'a \<Rightarrow> 'a::{real_normed_field,banach}"
-  shows 
+  shows
   "continuous (at x within s) f \<Longrightarrow> cos (f x) \<noteq> 0 \<Longrightarrow> continuous (at x within s) (\<lambda>x. tan (f x))"
   unfolding continuous_within by (rule tendsto_tan)
 
@@ -4200,7 +4200,7 @@
   shows "x\<^sup>2 < 1"
 proof -
   have "\<bar>x\<^sup>2\<bar> < 1"
-    by (metis abs_power2 assms pos2 power2_abs power_0 power_strict_decreasing zero_eq_power2 zero_less_abs_iff) 
+    by (metis abs_power2 assms pos2 power2_abs power_0 power_strict_decreasing zero_eq_power2 zero_less_abs_iff)
   thus ?thesis using zero_le_power2 by auto
 qed
 
@@ -4594,7 +4594,7 @@
     done
   show ?thesis
   proof (cases "0::real" y rule: linorder_cases)
-    case less 
+    case less
       then show ?thesis by (rule polar_ex1)
   next
     case equal
@@ -4602,7 +4602,7 @@
         by (force simp add: intro!: cos_zero sin_zero)
   next
     case greater
-      then show ?thesis 
+      then show ?thesis
      using polar_ex1 [where y="-y"]
     by auto (metis cos_minus minus_minus minus_mult_right sin_minus)
   qed
--- a/src/HOL/ex/Birthday_Paradox.thy	Tue Mar 10 15:21:26 2015 +0000
+++ b/src/HOL/ex/Birthday_Paradox.thy	Tue Mar 10 16:12:35 2015 +0000
@@ -5,14 +5,14 @@
 section {* A Formulation of the Birthday Paradox *}
 
 theory Birthday_Paradox
-imports Main "~~/src/HOL/Fact" "~~/src/HOL/Library/FuncSet"
+imports Main "~~/src/HOL/Binomial" "~~/src/HOL/Library/FuncSet"
 begin
 
 section {* Cardinality *}
 
 lemma card_product_dependent:
   assumes "finite S"
-  assumes "\<forall>x \<in> S. finite (T x)" 
+  assumes "\<forall>x \<in> S. finite (T x)"
   shows "card {(x, y). x \<in> S \<and> y \<in> T x} = (\<Sum>x \<in> S. card (T x))"
   using card_SigmaI[OF assms, symmetric] by (auto intro!: arg_cong[where f=card] simp add: Sigma_def)
 
@@ -30,7 +30,7 @@
     from `finite S` this have "finite (extensional_funcset S (T - {x}))"
       by (rule finite_PiE)
     moreover
-    have "{f : extensional_funcset S (T - {x}). inj_on f S} \<subseteq> (extensional_funcset S (T - {x}))" by auto    
+    have "{f : extensional_funcset S (T - {x}). inj_on f S} \<subseteq> (extensional_funcset S (T - {x}))" by auto
     ultimately have "finite {f : extensional_funcset S (T - {x}). inj_on f S}"
       by (auto intro: finite_subset)
   } note finite_delete = this
@@ -62,7 +62,7 @@
   have subset: "{f : extensional_funcset S T. inj_on f S} <= extensional_funcset S T" by auto
   from finite_subset[OF subset] assms have finite: "finite {f : extensional_funcset S T. inj_on f S}"
     by (auto intro!: finite_PiE)
-  have "{f \<in> extensional_funcset S T. \<not> inj_on f S} = extensional_funcset S T - {f \<in> extensional_funcset S T. inj_on f S}" by auto 
+  have "{f \<in> extensional_funcset S T. \<not> inj_on f S} = extensional_funcset S T - {f \<in> extensional_funcset S T. inj_on f S}" by auto
   from assms this finite subset show ?thesis
     by (simp add: card_Diff_subset card_PiE card_extensional_funcset_inj_on setprod_constant)
 qed