src/HOL/Binomial.thy
 author paulson Tue Apr 25 16:39:54 2017 +0100 (2017-04-25) changeset 65578 e4997c181cce parent 65552 f533820e7248 child 65581 baf96277ee76 permissions -rw-r--r--
New material from PNT proof, as well as more default [simp] declarations. Also removed duplicate theorems about geometric series
     1 (*  Title:      HOL/Binomial.thy

     2     Author:     Jacques D. Fleuriot

     3     Author:     Lawrence C Paulson

     4     Author:     Jeremy Avigad

     5     Author:     Chaitanya Mangla

     6     Author:     Manuel Eberl

     7 *)

     8

     9 section \<open>Combinatorial Functions: Factorial Function, Rising Factorials, Binomial Coefficients and Binomial Theorem\<close>

    10

    11 theory Binomial

    12   imports Pre_Main

    13 begin

    14

    15 subsection \<open>Factorial\<close>

    16

    17 context semiring_char_0

    18 begin

    19

    20 definition fact :: "nat \<Rightarrow> 'a"

    21   where fact_prod: "fact n = of_nat (\<Prod>{1..n})"

    22

    23 lemma fact_prod_Suc: "fact n = of_nat (prod Suc {0..<n})"

    24   by (cases n)

    25     (simp_all add: fact_prod prod.atLeast_Suc_atMost_Suc_shift

    26       atLeastLessThanSuc_atLeastAtMost)

    27

    28 lemma fact_prod_rev: "fact n = of_nat (\<Prod>i = 0..<n. n - i)"

    29   using prod.atLeast_atMost_rev [of "\<lambda>i. i" 1 n]

    30   by (cases n)

    31     (simp_all add: fact_prod_Suc prod.atLeast_Suc_atMost_Suc_shift

    32       atLeastLessThanSuc_atLeastAtMost)

    33

    34 lemma fact_0 [simp]: "fact 0 = 1"

    35   by (simp add: fact_prod)

    36

    37 lemma fact_1 [simp]: "fact 1 = 1"

    38   by (simp add: fact_prod)

    39

    40 lemma fact_Suc_0 [simp]: "fact (Suc 0) = 1"

    41   by (simp add: fact_prod)

    42

    43 lemma fact_Suc [simp]: "fact (Suc n) = of_nat (Suc n) * fact n"

    44   by (simp add: fact_prod atLeastAtMostSuc_conv algebra_simps)

    45

    46 lemma fact_2 [simp]: "fact 2 = 2"

    47   by (simp add: numeral_2_eq_2)

    48

    49 lemma fact_split: "k \<le> n \<Longrightarrow> fact n = of_nat (prod Suc {n - k..<n}) * fact (n - k)"

    50   by (simp add: fact_prod_Suc prod.union_disjoint [symmetric]

    51     ivl_disj_un ac_simps of_nat_mult [symmetric])

    52

    53 end

    54

    55 lemma of_nat_fact [simp]: "of_nat (fact n) = fact n"

    56   by (simp add: fact_prod)

    57

    58 lemma of_int_fact [simp]: "of_int (fact n) = fact n"

    59   by (simp only: fact_prod of_int_of_nat_eq)

    60

    61 lemma fact_reduce: "n > 0 \<Longrightarrow> fact n = of_nat n * fact (n - 1)"

    62   by (cases n) auto

    63

    64 lemma fact_nonzero [simp]: "fact n \<noteq> (0::'a::{semiring_char_0,semiring_no_zero_divisors})"

    65   apply (induct n)

    66   apply auto

    67   using of_nat_eq_0_iff

    68   apply fastforce

    69   done

    70

    71 lemma fact_mono_nat: "m \<le> n \<Longrightarrow> fact m \<le> (fact n :: nat)"

    72   by (induct n) (auto simp: le_Suc_eq)

    73

    74 lemma fact_in_Nats: "fact n \<in> \<nat>"

    75   by (induct n) auto

    76

    77 lemma fact_in_Ints: "fact n \<in> \<int>"

    78   by (induct n) auto

    79

    80 context

    81   assumes "SORT_CONSTRAINT('a::linordered_semidom)"

    82 begin

    83

    84 lemma fact_mono: "m \<le> n \<Longrightarrow> fact m \<le> (fact n :: 'a)"

    85   by (metis of_nat_fact of_nat_le_iff fact_mono_nat)

    86

    87 lemma fact_ge_1 [simp]: "fact n \<ge> (1 :: 'a)"

    88   by (metis le0 fact_0 fact_mono)

    89

    90 lemma fact_gt_zero [simp]: "fact n > (0 :: 'a)"

    91   using fact_ge_1 less_le_trans zero_less_one by blast

    92

    93 lemma fact_ge_zero [simp]: "fact n \<ge> (0 :: 'a)"

    94   by (simp add: less_imp_le)

    95

    96 lemma fact_not_neg [simp]: "\<not> fact n < (0 :: 'a)"

    97   by (simp add: not_less_iff_gr_or_eq)

    98

    99 lemma fact_le_power: "fact n \<le> (of_nat (n^n) :: 'a)"

   100 proof (induct n)

   101   case 0

   102   then show ?case by simp

   103 next

   104   case (Suc n)

   105   then have *: "fact n \<le> (of_nat (Suc n ^ n) ::'a)"

   106     by (rule order_trans) (simp add: power_mono del: of_nat_power)

   107   have "fact (Suc n) = (of_nat (Suc n) * fact n ::'a)"

   108     by (simp add: algebra_simps)

   109   also have "\<dots> \<le> of_nat (Suc n) * of_nat (Suc n ^ n)"

   110     by (simp add: * ordered_comm_semiring_class.comm_mult_left_mono del: of_nat_power)

   111   also have "\<dots> \<le> of_nat (Suc n ^ Suc n)"

   112     by (metis of_nat_mult order_refl power_Suc)

   113   finally show ?case .

   114 qed

   115

   116 end

   117

   118 text \<open>Note that @{term "fact 0 = fact 1"}\<close>

   119 lemma fact_less_mono_nat: "0 < m \<Longrightarrow> m < n \<Longrightarrow> fact m < (fact n :: nat)"

   120   by (induct n) (auto simp: less_Suc_eq)

   121

   122 lemma fact_less_mono: "0 < m \<Longrightarrow> m < n \<Longrightarrow> fact m < (fact n :: 'a::linordered_semidom)"

   123   by (metis of_nat_fact of_nat_less_iff fact_less_mono_nat)

   124

   125 lemma fact_ge_Suc_0_nat [simp]: "fact n \<ge> Suc 0"

   126   by (metis One_nat_def fact_ge_1)

   127

   128 lemma dvd_fact: "1 \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> m dvd fact n"

   129   by (induct n) (auto simp: dvdI le_Suc_eq)

   130

   131 lemma fact_ge_self: "fact n \<ge> n"

   132   by (cases "n = 0") (simp_all add: dvd_imp_le dvd_fact)

   133

   134 lemma fact_dvd: "n \<le> m \<Longrightarrow> fact n dvd (fact m :: 'a::{semiring_div,linordered_semidom})"

   135   by (induct m) (auto simp: le_Suc_eq)

   136

   137 lemma fact_mod: "m \<le> n \<Longrightarrow> fact n mod (fact m :: 'a::{semiring_div,linordered_semidom}) = 0"

   138   by (auto simp add: fact_dvd)

   139

   140 lemma fact_div_fact:

   141   assumes "m \<ge> n"

   142   shows "fact m div fact n = \<Prod>{n + 1..m}"

   143 proof -

   144   obtain d where "d = m - n"

   145     by auto

   146   with assms have "m = n + d"

   147     by auto

   148   have "fact (n + d) div (fact n) = \<Prod>{n + 1..n + d}"

   149   proof (induct d)

   150     case 0

   151     show ?case by simp

   152   next

   153     case (Suc d')

   154     have "fact (n + Suc d') div fact n = Suc (n + d') * fact (n + d') div fact n"

   155       by simp

   156     also from Suc.hyps have "\<dots> = Suc (n + d') * \<Prod>{n + 1..n + d'}"

   157       unfolding div_mult1_eq[of _ "fact (n + d')"] by (simp add: fact_mod)

   158     also have "\<dots> = \<Prod>{n + 1..n + Suc d'}"

   159       by (simp add: atLeastAtMostSuc_conv)

   160     finally show ?case .

   161   qed

   162   with \<open>m = n + d\<close> show ?thesis by simp

   163 qed

   164

   165 lemma fact_num_eq_if: "fact m = (if m = 0 then 1 else of_nat m * fact (m - 1))"

   166   by (cases m) auto

   167

   168 lemma fact_div_fact_le_pow:

   169   assumes "r \<le> n"

   170   shows "fact n div fact (n - r) \<le> n ^ r"

   171 proof -

   172   have "r \<le> n \<Longrightarrow> \<Prod>{n - r..n} = (n - r) * \<Prod>{Suc (n - r)..n}" for r

   173     by (subst prod.insert[symmetric]) (auto simp: atLeastAtMost_insertL)

   174   with assms show ?thesis

   175     by (induct r rule: nat.induct) (auto simp add: fact_div_fact Suc_diff_Suc mult_le_mono)

   176 qed

   177

   178 lemma fact_numeral: "fact (numeral k) = numeral k * fact (pred_numeral k)"

   179   \<comment> \<open>Evaluation for specific numerals\<close>

   180   by (metis fact_Suc numeral_eq_Suc of_nat_numeral)

   181

   182

   183 subsection \<open>Binomial coefficients\<close>

   184

   185 text \<open>This development is based on the work of Andy Gordon and Florian Kammueller.\<close>

   186

   187 text \<open>Combinatorial definition\<close>

   188

   189 definition binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat"  (infixl "choose" 65)

   190   where "n choose k = card {K\<in>Pow {0..<n}. card K = k}"

   191

   192 theorem n_subsets:

   193   assumes "finite A"

   194   shows "card {B. B \<subseteq> A \<and> card B = k} = card A choose k"

   195 proof -

   196   from assms obtain f where bij: "bij_betw f {0..<card A} A"

   197     by (blast dest: ex_bij_betw_nat_finite)

   198   then have [simp]: "card (f  C) = card C" if "C \<subseteq> {0..<card A}" for C

   199     by (meson bij_betw_imp_inj_on bij_betw_subset card_image that)

   200   from bij have "bij_betw (image f) (Pow {0..<card A}) (Pow A)"

   201     by (rule bij_betw_Pow)

   202   then have "inj_on (image f) (Pow {0..<card A})"

   203     by (rule bij_betw_imp_inj_on)

   204   moreover have "{K. K \<subseteq> {0..<card A} \<and> card K = k} \<subseteq> Pow {0..<card A}"

   205     by auto

   206   ultimately have "inj_on (image f) {K. K \<subseteq> {0..<card A} \<and> card K = k}"

   207     by (rule inj_on_subset)

   208   then have "card {K. K \<subseteq> {0..<card A} \<and> card K = k} =

   209       card (image f  {K. K \<subseteq> {0..<card A} \<and> card K = k})" (is "_ = card ?C")

   210     by (simp add: card_image)

   211   also have "?C = {K. K \<subseteq> f  {0..<card A} \<and> card K = k}"

   212     by (auto elim!: subset_imageE)

   213   also have "f  {0..<card A} = A"

   214     by (meson bij bij_betw_def)

   215   finally show ?thesis

   216     by (simp add: binomial_def)

   217 qed

   218

   219 text \<open>Recursive characterization\<close>

   220

   221 lemma binomial_n_0 [simp, code]: "n choose 0 = 1"

   222 proof -

   223   have "{K \<in> Pow {0..<n}. card K = 0} = {{}}"

   224     by (auto dest: finite_subset)

   225   then show ?thesis

   226     by (simp add: binomial_def)

   227 qed

   228

   229 lemma binomial_0_Suc [simp, code]: "0 choose Suc k = 0"

   230   by (simp add: binomial_def)

   231

   232 lemma binomial_Suc_Suc [simp, code]: "Suc n choose Suc k = (n choose k) + (n choose Suc k)"

   233 proof -

   234   let ?P = "\<lambda>n k. {K. K \<subseteq> {0..<n} \<and> card K = k}"

   235   let ?Q = "?P (Suc n) (Suc k)"

   236   have inj: "inj_on (insert n) (?P n k)"

   237     by rule (auto; metis atLeastLessThan_iff insert_iff less_irrefl subsetCE)

   238   have disjoint: "insert n  ?P n k \<inter> ?P n (Suc k) = {}"

   239     by auto

   240   have "?Q = {K\<in>?Q. n \<in> K} \<union> {K\<in>?Q. n \<notin> K}"

   241     by auto

   242   also have "{K\<in>?Q. n \<in> K} = insert n  ?P n k" (is "?A = ?B")

   243   proof (rule set_eqI)

   244     fix K

   245     have K_finite: "finite K" if "K \<subseteq> insert n {0..<n}"

   246       using that by (rule finite_subset) simp_all

   247     have Suc_card_K: "Suc (card K - Suc 0) = card K" if "n \<in> K"

   248       and "finite K"

   249     proof -

   250       from \<open>n \<in> K\<close> obtain L where "K = insert n L" and "n \<notin> L"

   251         by (blast elim: Set.set_insert)

   252       with that show ?thesis by (simp add: card_insert)

   253     qed

   254     show "K \<in> ?A \<longleftrightarrow> K \<in> ?B"

   255       by (subst in_image_insert_iff)

   256         (auto simp add: card_insert subset_eq_atLeast0_lessThan_finite

   257           Diff_subset_conv K_finite Suc_card_K)

   258   qed

   259   also have "{K\<in>?Q. n \<notin> K} = ?P n (Suc k)"

   260     by (auto simp add: atLeast0_lessThan_Suc)

   261   finally show ?thesis using inj disjoint

   262     by (simp add: binomial_def card_Un_disjoint card_image)

   263 qed

   264

   265 lemma binomial_eq_0: "n < k \<Longrightarrow> n choose k = 0"

   266   by (auto simp add: binomial_def dest: subset_eq_atLeast0_lessThan_card)

   267

   268 lemma zero_less_binomial: "k \<le> n \<Longrightarrow> n choose k > 0"

   269   by (induct n k rule: diff_induct) simp_all

   270

   271 lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> n < k"

   272   by (metis binomial_eq_0 less_numeral_extra(3) not_less zero_less_binomial)

   273

   274 lemma zero_less_binomial_iff [simp]: "n choose k > 0 \<longleftrightarrow> k \<le> n"

   275   by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)

   276

   277 lemma binomial_n_n [simp]: "n choose n = 1"

   278   by (induct n) (simp_all add: binomial_eq_0)

   279

   280 lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n"

   281   by (induct n) simp_all

   282

   283 lemma binomial_1 [simp]: "n choose Suc 0 = n"

   284   by (induct n) simp_all

   285

   286 lemma choose_reduce_nat:

   287   "0 < n \<Longrightarrow> 0 < k \<Longrightarrow>

   288     n choose k = ((n - 1) choose (k - 1)) + ((n - 1) choose k)"

   289   using binomial_Suc_Suc [of "n - 1" "k - 1"] by simp

   290

   291 lemma Suc_times_binomial_eq: "Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"

   292   apply (induct n arbitrary: k)

   293    apply simp

   294    apply arith

   295   apply (case_tac k)

   296    apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0)

   297   done

   298

   299 lemma binomial_le_pow2: "n choose k \<le> 2^n"

   300   apply (induct n arbitrary: k)

   301    apply (case_tac k)

   302     apply simp_all

   303   apply (case_tac k)

   304    apply auto

   305   apply (simp add: add_le_mono mult_2)

   306   done

   307

   308 text \<open>The absorption property.\<close>

   309 lemma Suc_times_binomial: "Suc k * (Suc n choose Suc k) = Suc n * (n choose k)"

   310   using Suc_times_binomial_eq by auto

   311

   312 text \<open>This is the well-known version of absorption, but it's harder to use

   313   because of the need to reason about division.\<close>

   314 lemma binomial_Suc_Suc_eq_times: "(Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"

   315   by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)

   316

   317 text \<open>Another version of absorption, with \<open>-1\<close> instead of \<open>Suc\<close>.\<close>

   318 lemma times_binomial_minus1_eq: "0 < k \<Longrightarrow> k * (n choose k) = n * ((n - 1) choose (k - 1))"

   319   using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"]

   320   by (auto split: nat_diff_split)

   321

   322

   323 subsection \<open>The binomial theorem (courtesy of Tobias Nipkow):\<close>

   324

   325 text \<open>Avigad's version, generalized to any commutative ring\<close>

   326 theorem binomial_ring: "(a + b :: 'a::{comm_ring_1,power})^n =

   327   (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))"

   328 proof (induct n)

   329   case 0

   330   then show ?case by simp

   331 next

   332   case (Suc n)

   333   have decomp: "{0..n+1} = {0} \<union> {n + 1} \<union> {1..n}"

   334     by auto

   335   have decomp2: "{0..n} = {0} \<union> {1..n}"

   336     by auto

   337   have "(a + b)^(n+1) = (a + b) * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n - k))"

   338     using Suc.hyps by simp

   339   also have "\<dots> = a * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +

   340       b * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"

   341     by (rule distrib_right)

   342   also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +

   343       (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n - k + 1))"

   344     by (auto simp add: sum_distrib_left ac_simps)

   345   also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n + 1 - k)) +

   346       (\<Sum>k=1..n+1. of_nat (n choose (k - 1)) * a^k * b^(n + 1 - k))"

   347     by (simp add:sum_shift_bounds_cl_Suc_ivl Suc_diff_le field_simps del: sum_cl_ivl_Suc)

   348   also have "\<dots> = a^(n + 1) + b^(n + 1) +

   349       (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n + 1 - k)) +

   350       (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n + 1 - k))"

   351     by (simp add: decomp2)

   352   also have "\<dots> = a^(n + 1) + b^(n + 1) +

   353       (\<Sum>k=1..n. of_nat (n + 1 choose k) * a^k * b^(n + 1 - k))"

   354     by (auto simp add: field_simps sum.distrib [symmetric] choose_reduce_nat)

   355   also have "\<dots> = (\<Sum>k=0..n+1. of_nat (n + 1 choose k) * a^k * b^(n + 1 - k))"

   356     using decomp by (simp add: field_simps)

   357   finally show ?case

   358     by simp

   359 qed

   360

   361 text \<open>Original version for the naturals.\<close>

   362 corollary binomial: "(a + b :: nat)^n = (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n - k))"

   363   using binomial_ring [of "int a" "int b" n]

   364   by (simp only: of_nat_add [symmetric] of_nat_mult [symmetric] of_nat_power [symmetric]

   365       of_nat_sum [symmetric] of_nat_eq_iff of_nat_id)

   366

   367 lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"

   368 proof (induct n arbitrary: k rule: nat_less_induct)

   369   fix n k

   370   assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) = fact m"

   371   assume kn: "k \<le> n"

   372   let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"

   373   consider "n = 0 \<or> k = 0 \<or> n = k" | m h where "n = Suc m" "k = Suc h" "h < m"

   374     using kn by atomize_elim presburger

   375   then show "fact k * fact (n - k) * (n choose k) = fact n"

   376   proof cases

   377     case 1

   378     with kn show ?thesis by auto

   379   next

   380     case 2

   381     note n = \<open>n = Suc m\<close>

   382     note k = \<open>k = Suc h\<close>

   383     note hm = \<open>h < m\<close>

   384     have mn: "m < n"

   385       using n by arith

   386     have hm': "h \<le> m"

   387       using hm by arith

   388     have km: "k \<le> m"

   389       using hm k n kn by arith

   390     have "m - h = Suc (m - Suc h)"

   391       using  k km hm by arith

   392     with km k have "fact (m - h) = (m - h) * fact (m - k)"

   393       by simp

   394     with n k have "fact k * fact (n - k) * (n choose k) =

   395         k * (fact h * fact (m - h) * (m choose h)) +

   396         (m - h) * (fact k * fact (m - k) * (m choose k))"

   397       by (simp add: field_simps)

   398     also have "\<dots> = (k + (m - h)) * fact m"

   399       using H[rule_format, OF mn hm'] H[rule_format, OF mn km]

   400       by (simp add: field_simps)

   401     finally show ?thesis

   402       using k n km by simp

   403   qed

   404 qed

   405

   406 lemma binomial_fact':

   407   assumes "k \<le> n"

   408   shows "n choose k = fact n div (fact k * fact (n - k))"

   409   using binomial_fact_lemma [OF assms]

   410   by (metis fact_nonzero mult_eq_0_iff nonzero_mult_div_cancel_left)

   411

   412 lemma binomial_fact:

   413   assumes kn: "k \<le> n"

   414   shows "(of_nat (n choose k) :: 'a::field_char_0) = fact n / (fact k * fact (n - k))"

   415   using binomial_fact_lemma[OF kn]

   416   apply (simp add: field_simps)

   417   apply (metis mult.commute of_nat_fact of_nat_mult)

   418   done

   419

   420 lemma fact_binomial:

   421   assumes "k \<le> n"

   422   shows "fact k * of_nat (n choose k) = (fact n / fact (n - k) :: 'a::field_char_0)"

   423   unfolding binomial_fact [OF assms] by (simp add: field_simps)

   424

   425 lemma choose_two: "n choose 2 = n * (n - 1) div 2"

   426 proof (cases "n \<ge> 2")

   427   case False

   428   then have "n = 0 \<or> n = 1"

   429     by auto

   430   then show ?thesis by auto

   431 next

   432   case True

   433   define m where "m = n - 2"

   434   with True have "n = m + 2"

   435     by simp

   436   then have "fact n = n * (n - 1) * fact (n - 2)"

   437     by (simp add: fact_prod_Suc atLeast0_lessThan_Suc algebra_simps)

   438   with True show ?thesis

   439     by (simp add: binomial_fact')

   440 qed

   441

   442 lemma choose_row_sum: "(\<Sum>k=0..n. n choose k) = 2^n"

   443   using binomial [of 1 "1" n] by (simp add: numeral_2_eq_2)

   444

   445 lemma sum_choose_lower: "(\<Sum>k=0..n. (r+k) choose k) = Suc (r+n) choose n"

   446   by (induct n) auto

   447

   448 lemma sum_choose_upper: "(\<Sum>k=0..n. k choose m) = Suc n choose Suc m"

   449   by (induct n) auto

   450

   451 lemma choose_alternating_sum:

   452   "n > 0 \<Longrightarrow> (\<Sum>i\<le>n. (-1)^i * of_nat (n choose i)) = (0 :: 'a::comm_ring_1)"

   453   using binomial_ring[of "-1 :: 'a" 1 n]

   454   by (simp add: atLeast0AtMost mult_of_nat_commute zero_power)

   455

   456 lemma choose_even_sum:

   457   assumes "n > 0"

   458   shows "2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0) = (2 ^ n :: 'a::comm_ring_1)"

   459 proof -

   460   have "2 ^ n = (\<Sum>i\<le>n. of_nat (n choose i)) + (\<Sum>i\<le>n. (-1) ^ i * of_nat (n choose i) :: 'a)"

   461     using choose_row_sum[of n]

   462     by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_sum[symmetric])

   463   also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) + (-1) ^ i * of_nat (n choose i))"

   464     by (simp add: sum.distrib)

   465   also have "\<dots> = 2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0)"

   466     by (subst sum_distrib_left, intro sum.cong) simp_all

   467   finally show ?thesis ..

   468 qed

   469

   470 lemma choose_odd_sum:

   471   assumes "n > 0"

   472   shows "2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0) = (2 ^ n :: 'a::comm_ring_1)"

   473 proof -

   474   have "2 ^ n = (\<Sum>i\<le>n. of_nat (n choose i)) - (\<Sum>i\<le>n. (-1) ^ i * of_nat (n choose i) :: 'a)"

   475     using choose_row_sum[of n]

   476     by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_sum[symmetric])

   477   also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) - (-1) ^ i * of_nat (n choose i))"

   478     by (simp add: sum_subtractf)

   479   also have "\<dots> = 2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0)"

   480     by (subst sum_distrib_left, intro sum.cong) simp_all

   481   finally show ?thesis ..

   482 qed

   483

   484 lemma choose_row_sum': "(\<Sum>k\<le>n. (n choose k)) = 2 ^ n"

   485   using choose_row_sum[of n] by (simp add: atLeast0AtMost)

   486

   487 text\<open>NW diagonal sum property\<close>

   488 lemma sum_choose_diagonal:

   489   assumes "m \<le> n"

   490   shows "(\<Sum>k=0..m. (n - k) choose (m - k)) = Suc n choose m"

   491 proof -

   492   have "(\<Sum>k=0..m. (n-k) choose (m - k)) = (\<Sum>k=0..m. (n - m + k) choose k)"

   493     using sum.atLeast_atMost_rev [of "\<lambda>k. (n - k) choose (m - k)" 0 m] assms

   494       by simp

   495   also have "\<dots> = Suc (n - m + m) choose m"

   496     by (rule sum_choose_lower)

   497   also have "\<dots> = Suc n choose m"

   498     using assms by simp

   499   finally show ?thesis .

   500 qed

   501

   502

   503 subsection \<open>Pochhammer's symbol: generalized rising factorial\<close>

   504

   505 text \<open>See \<^url>\<open>http://en.wikipedia.org/wiki/Pochhammer_symbol\<close>.\<close>

   506

   507 context comm_semiring_1

   508 begin

   509

   510 definition pochhammer :: "'a \<Rightarrow> nat \<Rightarrow> 'a"

   511   where pochhammer_prod: "pochhammer a n = prod (\<lambda>i. a + of_nat i) {0..<n}"

   512

   513 lemma pochhammer_prod_rev: "pochhammer a n = prod (\<lambda>i. a + of_nat (n - i)) {1..n}"

   514   using prod.atLeast_lessThan_rev_at_least_Suc_atMost [of "\<lambda>i. a + of_nat i" 0 n]

   515   by (simp add: pochhammer_prod)

   516

   517 lemma pochhammer_Suc_prod: "pochhammer a (Suc n) = prod (\<lambda>i. a + of_nat i) {0..n}"

   518   by (simp add: pochhammer_prod atLeastLessThanSuc_atLeastAtMost)

   519

   520 lemma pochhammer_Suc_prod_rev: "pochhammer a (Suc n) = prod (\<lambda>i. a + of_nat (n - i)) {0..n}"

   521   by (simp add: pochhammer_prod_rev prod.atLeast_Suc_atMost_Suc_shift)

   522

   523 lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"

   524   by (simp add: pochhammer_prod)

   525

   526 lemma pochhammer_1 [simp]: "pochhammer a 1 = a"

   527   by (simp add: pochhammer_prod lessThan_Suc)

   528

   529 lemma pochhammer_Suc0 [simp]: "pochhammer a (Suc 0) = a"

   530   by (simp add: pochhammer_prod lessThan_Suc)

   531

   532 lemma pochhammer_Suc: "pochhammer a (Suc n) = pochhammer a n * (a + of_nat n)"

   533   by (simp add: pochhammer_prod atLeast0_lessThan_Suc ac_simps)

   534

   535 end

   536

   537 lemma pochhammer_nonneg:

   538   fixes x :: "'a :: linordered_semidom"

   539   shows "x > 0 \<Longrightarrow> pochhammer x n \<ge> 0"

   540   by (induction n) (auto simp: pochhammer_Suc intro!: mult_nonneg_nonneg add_nonneg_nonneg)

   541

   542 lemma pochhammer_pos:

   543   fixes x :: "'a :: linordered_semidom"

   544   shows "x > 0 \<Longrightarrow> pochhammer x n > 0"

   545   by (induction n) (auto simp: pochhammer_Suc intro!: mult_pos_pos add_pos_nonneg)

   546

   547 lemma pochhammer_of_nat: "pochhammer (of_nat x) n = of_nat (pochhammer x n)"

   548   by (simp add: pochhammer_prod)

   549

   550 lemma pochhammer_of_int: "pochhammer (of_int x) n = of_int (pochhammer x n)"

   551   by (simp add: pochhammer_prod)

   552

   553 lemma pochhammer_rec: "pochhammer a (Suc n) = a * pochhammer (a + 1) n"

   554   by (simp add: pochhammer_prod prod.atLeast0_lessThan_Suc_shift ac_simps)

   555

   556 lemma pochhammer_rec': "pochhammer z (Suc n) = (z + of_nat n) * pochhammer z n"

   557   by (simp add: pochhammer_prod prod.atLeast0_lessThan_Suc ac_simps)

   558

   559 lemma pochhammer_fact: "fact n = pochhammer 1 n"

   560   by (simp add: pochhammer_prod fact_prod_Suc)

   561

   562 lemma pochhammer_of_nat_eq_0_lemma: "k > n \<Longrightarrow> pochhammer (- (of_nat n :: 'a:: idom)) k = 0"

   563   by (auto simp add: pochhammer_prod)

   564

   565 lemma pochhammer_of_nat_eq_0_lemma':

   566   assumes kn: "k \<le> n"

   567   shows "pochhammer (- (of_nat n :: 'a::{idom,ring_char_0})) k \<noteq> 0"

   568 proof (cases k)

   569   case 0

   570   then show ?thesis by simp

   571 next

   572   case (Suc h)

   573   then show ?thesis

   574     apply (simp add: pochhammer_Suc_prod)

   575     using Suc kn

   576     apply (auto simp add: algebra_simps)

   577     done

   578 qed

   579

   580 lemma pochhammer_of_nat_eq_0_iff:

   581   "pochhammer (- (of_nat n :: 'a::{idom,ring_char_0})) k = 0 \<longleftrightarrow> k > n"

   582   (is "?l = ?r")

   583   using pochhammer_of_nat_eq_0_lemma[of n k, where ?'a='a]

   584     pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]

   585   by (auto simp add: not_le[symmetric])

   586

   587 lemma pochhammer_eq_0_iff: "pochhammer a n = (0::'a::field_char_0) \<longleftrightarrow> (\<exists>k < n. a = - of_nat k)"

   588   by (auto simp add: pochhammer_prod eq_neg_iff_add_eq_0)

   589

   590 lemma pochhammer_eq_0_mono:

   591   "pochhammer a n = (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"

   592   unfolding pochhammer_eq_0_iff by auto

   593

   594 lemma pochhammer_neq_0_mono:

   595   "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"

   596   unfolding pochhammer_eq_0_iff by auto

   597

   598 lemma pochhammer_minus:

   599   "pochhammer (- b) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (b - of_nat k + 1) k"

   600 proof (cases k)

   601   case 0

   602   then show ?thesis by simp

   603 next

   604   case (Suc h)

   605   have eq: "((- 1) ^ Suc h :: 'a) = (\<Prod>i = 0..h. - 1)"

   606     using prod_constant [where A="{0.. h}" and y="- 1 :: 'a"]

   607     by auto

   608   with Suc show ?thesis

   609     using pochhammer_Suc_prod_rev [of "b - of_nat k + 1"]

   610     by (auto simp add: pochhammer_Suc_prod prod.distrib [symmetric] eq of_nat_diff)

   611 qed

   612

   613 lemma pochhammer_minus':

   614   "pochhammer (b - of_nat k + 1) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (- b) k"

   615   apply (simp only: pochhammer_minus [where b = b])

   616   apply (simp only: mult.assoc [symmetric])

   617   apply (simp only: power_add [symmetric])

   618   apply simp

   619   done

   620

   621 lemma pochhammer_same: "pochhammer (- of_nat n) n =

   622     ((- 1) ^ n :: 'a::{semiring_char_0,comm_ring_1,semiring_no_zero_divisors}) * fact n"

   623   unfolding pochhammer_minus

   624   by (simp add: of_nat_diff pochhammer_fact)

   625

   626 lemma pochhammer_product': "pochhammer z (n + m) = pochhammer z n * pochhammer (z + of_nat n) m"

   627 proof (induct n arbitrary: z)

   628   case 0

   629   then show ?case by simp

   630 next

   631   case (Suc n z)

   632   have "pochhammer z (Suc n) * pochhammer (z + of_nat (Suc n)) m =

   633       z * (pochhammer (z + 1) n * pochhammer (z + 1 + of_nat n) m)"

   634     by (simp add: pochhammer_rec ac_simps)

   635   also note Suc[symmetric]

   636   also have "z * pochhammer (z + 1) (n + m) = pochhammer z (Suc (n + m))"

   637     by (subst pochhammer_rec) simp

   638   finally show ?case

   639     by simp

   640 qed

   641

   642 lemma pochhammer_product:

   643   "m \<le> n \<Longrightarrow> pochhammer z n = pochhammer z m * pochhammer (z + of_nat m) (n - m)"

   644   using pochhammer_product'[of z m "n - m"] by simp

   645

   646 lemma pochhammer_times_pochhammer_half:

   647   fixes z :: "'a::field_char_0"

   648   shows "pochhammer z (Suc n) * pochhammer (z + 1/2) (Suc n) = (\<Prod>k=0..2*n+1. z + of_nat k / 2)"

   649 proof (induct n)

   650   case 0

   651   then show ?case

   652     by (simp add: atLeast0_atMost_Suc)

   653 next

   654   case (Suc n)

   655   define n' where "n' = Suc n"

   656   have "pochhammer z (Suc n') * pochhammer (z + 1 / 2) (Suc n') =

   657       (pochhammer z n' * pochhammer (z + 1 / 2) n') * ((z + of_nat n') * (z + 1/2 + of_nat n'))"

   658     (is "_ = _ * ?A")

   659     by (simp_all add: pochhammer_rec' mult_ac)

   660   also have "?A = (z + of_nat (Suc (2 * n + 1)) / 2) * (z + of_nat (Suc (Suc (2 * n + 1))) / 2)"

   661     (is "_ = ?B")

   662     by (simp add: field_simps n'_def)

   663   also note Suc[folded n'_def]

   664   also have "(\<Prod>k=0..2 * n + 1. z + of_nat k / 2) * ?B = (\<Prod>k=0..2 * Suc n + 1. z + of_nat k / 2)"

   665     by (simp add: atLeast0_atMost_Suc)

   666   finally show ?case

   667     by (simp add: n'_def)

   668 qed

   669

   670 lemma pochhammer_double:

   671   fixes z :: "'a::field_char_0"

   672   shows "pochhammer (2 * z) (2 * n) = of_nat (2^(2*n)) * pochhammer z n * pochhammer (z+1/2) n"

   673 proof (induct n)

   674   case 0

   675   then show ?case by simp

   676 next

   677   case (Suc n)

   678   have "pochhammer (2 * z) (2 * (Suc n)) = pochhammer (2 * z) (2 * n) *

   679       (2 * (z + of_nat n)) * (2 * (z + of_nat n) + 1)"

   680     by (simp add: pochhammer_rec' ac_simps)

   681   also note Suc

   682   also have "of_nat (2 ^ (2 * n)) * pochhammer z n * pochhammer (z + 1/2) n *

   683         (2 * (z + of_nat n)) * (2 * (z + of_nat n) + 1) =

   684       of_nat (2 ^ (2 * (Suc n))) * pochhammer z (Suc n) * pochhammer (z + 1/2) (Suc n)"

   685     by (simp add: field_simps pochhammer_rec')

   686   finally show ?case .

   687 qed

   688

   689 lemma fact_double:

   690   "fact (2 * n) = (2 ^ (2 * n) * pochhammer (1 / 2) n * fact n :: 'a::field_char_0)"

   691   using pochhammer_double[of "1/2::'a" n] by (simp add: pochhammer_fact)

   692

   693 lemma pochhammer_absorb_comp: "(r - of_nat k) * pochhammer (- r) k = r * pochhammer (-r + 1) k"

   694   (is "?lhs = ?rhs")

   695   for r :: "'a::comm_ring_1"

   696 proof -

   697   have "?lhs = - pochhammer (- r) (Suc k)"

   698     by (subst pochhammer_rec') (simp add: algebra_simps)

   699   also have "\<dots> = ?rhs"

   700     by (subst pochhammer_rec) simp

   701   finally show ?thesis .

   702 qed

   703

   704

   705 subsection \<open>Generalized binomial coefficients\<close>

   706

   707 definition gbinomial :: "'a::{semidom_divide,semiring_char_0} \<Rightarrow> nat \<Rightarrow> 'a"  (infixl "gchoose" 65)

   708   where gbinomial_prod_rev: "a gchoose n = prod (\<lambda>i. a - of_nat i) {0..<n} div fact n"

   709

   710 lemma gbinomial_0 [simp]:

   711   "a gchoose 0 = 1"

   712   "0 gchoose (Suc n) = 0"

   713   by (simp_all add: gbinomial_prod_rev prod.atLeast0_lessThan_Suc_shift)

   714

   715 lemma gbinomial_Suc: "a gchoose (Suc k) = prod (\<lambda>i. a - of_nat i) {0..k} div fact (Suc k)"

   716   by (simp add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost)

   717

   718 lemma gbinomial_mult_fact: "fact n * (a gchoose n) = (\<Prod>i = 0..<n. a - of_nat i)"

   719   for a :: "'a::field_char_0"

   720   by (simp_all add: gbinomial_prod_rev field_simps)

   721

   722 lemma gbinomial_mult_fact': "(a gchoose n) * fact n = (\<Prod>i = 0..<n. a - of_nat i)"

   723   for a :: "'a::field_char_0"

   724   using gbinomial_mult_fact [of n a] by (simp add: ac_simps)

   725

   726 lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / fact n"

   727   for a :: "'a::field_char_0"

   728   by (cases n)

   729     (simp_all add: pochhammer_minus,

   730      simp_all add: gbinomial_prod_rev pochhammer_prod_rev

   731        power_mult_distrib [symmetric] atLeastLessThanSuc_atLeastAtMost

   732        prod.atLeast_Suc_atMost_Suc_shift of_nat_diff)

   733

   734 lemma gbinomial_pochhammer': "s gchoose n = pochhammer (s - of_nat n + 1) n / fact n"

   735   for s :: "'a::field_char_0"

   736 proof -

   737   have "s gchoose n = ((-1)^n * (-1)^n) * pochhammer (s - of_nat n + 1) n / fact n"

   738     by (simp add: gbinomial_pochhammer pochhammer_minus mult_ac)

   739   also have "(-1 :: 'a)^n * (-1)^n = 1"

   740     by (subst power_add [symmetric]) simp

   741   finally show ?thesis

   742     by simp

   743 qed

   744

   745 lemma gbinomial_binomial: "n gchoose k = n choose k"

   746 proof (cases "k \<le> n")

   747   case False

   748   then have "n < k"

   749     by (simp add: not_le)

   750   then have "0 \<in> (op - n)  {0..<k}"

   751     by auto

   752   then have "prod (op - n) {0..<k} = 0"

   753     by (auto intro: prod_zero)

   754   with \<open>n < k\<close> show ?thesis

   755     by (simp add: binomial_eq_0 gbinomial_prod_rev prod_zero)

   756 next

   757   case True

   758   from True have *: "prod (op - n) {0..<k} = \<Prod>{Suc (n - k)..n}"

   759     by (intro prod.reindex_bij_witness[of _ "\<lambda>i. n - i" "\<lambda>i. n - i"]) auto

   760   from True have "n choose k = fact n div (fact k * fact (n - k))"

   761     by (rule binomial_fact')

   762   with * show ?thesis

   763     by (simp add: gbinomial_prod_rev mult.commute [of "fact k"] div_mult2_eq fact_div_fact)

   764 qed

   765

   766 lemma of_nat_gbinomial: "of_nat (n gchoose k) = (of_nat n gchoose k :: 'a::field_char_0)"

   767 proof (cases "k \<le> n")

   768   case False

   769   then show ?thesis

   770     by (simp add: not_le gbinomial_binomial binomial_eq_0 gbinomial_prod_rev)

   771 next

   772   case True

   773   define m where "m = n - k"

   774   with True have n: "n = m + k"

   775     by arith

   776   from n have "fact n = ((\<Prod>i = 0..<m + k. of_nat (m + k - i) ):: 'a)"

   777     by (simp add: fact_prod_rev)

   778   also have "\<dots> = ((\<Prod>i\<in>{0..<k} \<union> {k..<m + k}. of_nat (m + k - i)) :: 'a)"

   779     by (simp add: ivl_disj_un)

   780   finally have "fact n = (fact m * (\<Prod>i = 0..<k. of_nat m + of_nat k - of_nat i) :: 'a)"

   781     using prod_shift_bounds_nat_ivl [of "\<lambda>i. of_nat (m + k - i) :: 'a" 0 k m]

   782     by (simp add: fact_prod_rev [of m] prod.union_disjoint of_nat_diff)

   783   then have "fact n / fact (n - k) = ((\<Prod>i = 0..<k. of_nat n - of_nat i) :: 'a)"

   784     by (simp add: n)

   785   with True have "fact k * of_nat (n gchoose k) = (fact k * (of_nat n gchoose k) :: 'a)"

   786     by (simp only: gbinomial_mult_fact [of k "of_nat n"] gbinomial_binomial [of n k] fact_binomial)

   787   then show ?thesis

   788     by simp

   789 qed

   790

   791 lemma binomial_gbinomial: "of_nat (n choose k) = (of_nat n gchoose k :: 'a::field_char_0)"

   792   by (simp add: gbinomial_binomial [symmetric] of_nat_gbinomial)

   793

   794 setup

   795   \<open>Sign.add_const_constraint (@{const_name gbinomial}, SOME @{typ "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a"})\<close>

   796

   797 lemma gbinomial_1[simp]: "a gchoose 1 = a"

   798   by (simp add: gbinomial_prod_rev lessThan_Suc)

   799

   800 lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"

   801   by (simp add: gbinomial_prod_rev lessThan_Suc)

   802

   803 lemma gbinomial_mult_1:

   804   fixes a :: "'a::field_char_0"

   805   shows "a * (a gchoose n) = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"

   806   (is "?l = ?r")

   807 proof -

   808   have "?r = ((- 1) ^n * pochhammer (- a) n / (fact n)) * (of_nat n - (- a + of_nat n))"

   809     apply (simp only: gbinomial_pochhammer pochhammer_Suc right_diff_distrib power_Suc)

   810     apply (simp del: of_nat_Suc fact_Suc)

   811     apply (auto simp add: field_simps simp del: of_nat_Suc)

   812     done

   813   also have "\<dots> = ?l"

   814     by (simp add: field_simps gbinomial_pochhammer)

   815   finally show ?thesis ..

   816 qed

   817

   818 lemma gbinomial_mult_1':

   819   "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"

   820   for a :: "'a::field_char_0"

   821   by (simp add: mult.commute gbinomial_mult_1)

   822

   823 lemma gbinomial_Suc_Suc: "(a + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"

   824   for a :: "'a::field_char_0"

   825 proof (cases k)

   826   case 0

   827   then show ?thesis by simp

   828 next

   829   case (Suc h)

   830   have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)"

   831     apply (rule prod.reindex_cong [where l = Suc])

   832       using Suc

   833       apply (auto simp add: image_Suc_atMost)

   834     done

   835   have "fact (Suc k) * (a gchoose k + (a gchoose (Suc k))) =

   836       (a gchoose Suc h) * (fact (Suc (Suc h))) +

   837       (a gchoose Suc (Suc h)) * (fact (Suc (Suc h)))"

   838     by (simp add: Suc field_simps del: fact_Suc)

   839   also have "\<dots> =

   840     (a gchoose Suc h) * of_nat (Suc (Suc h) * fact (Suc h)) + (\<Prod>i=0..Suc h. a - of_nat i)"

   841     apply (simp del: fact_Suc add: gbinomial_mult_fact field_simps mult.left_commute [of _ "2"])

   842     apply (simp del: fact_Suc add: fact_Suc [of "Suc h"] field_simps gbinomial_mult_fact

   843       mult.left_commute [of _ "2"] atLeastLessThanSuc_atLeastAtMost)

   844     done

   845   also have "\<dots> =

   846     (fact (Suc h) * (a gchoose Suc h)) * of_nat (Suc (Suc h)) + (\<Prod>i=0..Suc h. a - of_nat i)"

   847     by (simp only: fact_Suc mult.commute mult.left_commute of_nat_fact of_nat_id of_nat_mult)

   848   also have "\<dots> =

   849     of_nat (Suc (Suc h)) * (\<Prod>i=0..h. a - of_nat i) + (\<Prod>i=0..Suc h. a - of_nat i)"

   850     unfolding gbinomial_mult_fact atLeastLessThanSuc_atLeastAtMost by auto

   851   also have "\<dots> =

   852     (\<Prod>i=0..Suc h. a - of_nat i) + (of_nat h * (\<Prod>i=0..h. a - of_nat i) + 2 * (\<Prod>i=0..h. a - of_nat i))"

   853     by (simp add: field_simps)

   854   also have "\<dots> =

   855     ((a gchoose Suc h) * (fact (Suc h)) * of_nat (Suc k)) + (\<Prod>i\<in>{0..Suc h}. a - of_nat i)"

   856     unfolding gbinomial_mult_fact'

   857     by (simp add: comm_semiring_class.distrib field_simps Suc atLeastLessThanSuc_atLeastAtMost)

   858   also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"

   859     unfolding gbinomial_mult_fact' atLeast0_atMost_Suc

   860     by (simp add: field_simps Suc atLeastLessThanSuc_atLeastAtMost)

   861   also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"

   862     using eq0

   863     by (simp add: Suc prod.atLeast0_atMost_Suc_shift)

   864   also have "\<dots> = (fact (Suc k)) * ((a + 1) gchoose (Suc k))"

   865     by (simp only: gbinomial_mult_fact atLeastLessThanSuc_atLeastAtMost)

   866   finally show ?thesis

   867     using fact_nonzero [of "Suc k"] by auto

   868 qed

   869

   870 lemma gbinomial_reduce_nat: "0 < k \<Longrightarrow> a gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)"

   871   for a :: "'a::field_char_0"

   872   by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc)

   873

   874 lemma gchoose_row_sum_weighted:

   875   "(\<Sum>k = 0..m. (r gchoose k) * (r/2 - of_nat k)) = of_nat(Suc m) / 2 * (r gchoose (Suc m))"

   876   for r :: "'a::field_char_0"

   877   by (induct m) (simp_all add: field_simps distrib gbinomial_mult_1)

   878

   879 lemma binomial_symmetric:

   880   assumes kn: "k \<le> n"

   881   shows "n choose k = n choose (n - k)"

   882 proof -

   883   have kn': "n - k \<le> n"

   884     using kn by arith

   885   from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']

   886   have "fact k * fact (n - k) * (n choose k) = fact (n - k) * fact (n - (n - k)) * (n choose (n - k))"

   887     by simp

   888   then show ?thesis

   889     using kn by simp

   890 qed

   891

   892 lemma choose_rising_sum:

   893   "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))"

   894   "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose m)"

   895 proof -

   896   show "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))"

   897     by (induct m) simp_all

   898   also have "\<dots> = (n + m + 1) choose m"

   899     by (subst binomial_symmetric) simp_all

   900   finally show "(\<Sum>j\<le>m. ((n + j) choose n)) = (n + m + 1) choose m" .

   901 qed

   902

   903 lemma choose_linear_sum: "(\<Sum>i\<le>n. i * (n choose i)) = n * 2 ^ (n - 1)"

   904 proof (cases n)

   905   case 0

   906   then show ?thesis by simp

   907 next

   908   case (Suc m)

   909   have "(\<Sum>i\<le>n. i * (n choose i)) = (\<Sum>i\<le>Suc m. i * (Suc m choose i))"

   910     by (simp add: Suc)

   911   also have "\<dots> = Suc m * 2 ^ m"

   912     by (simp only: sum_atMost_Suc_shift Suc_times_binomial sum_distrib_left[symmetric])

   913        (simp add: choose_row_sum')

   914   finally show ?thesis

   915     using Suc by simp

   916 qed

   917

   918 lemma choose_alternating_linear_sum:

   919   assumes "n \<noteq> 1"

   920   shows "(\<Sum>i\<le>n. (-1)^i * of_nat i * of_nat (n choose i) :: 'a::comm_ring_1) = 0"

   921 proof (cases n)

   922   case 0

   923   then show ?thesis by simp

   924 next

   925   case (Suc m)

   926   with assms have "m > 0"

   927     by simp

   928   have "(\<Sum>i\<le>n. (-1) ^ i * of_nat i * of_nat (n choose i) :: 'a) =

   929       (\<Sum>i\<le>Suc m. (-1) ^ i * of_nat i * of_nat (Suc m choose i))"

   930     by (simp add: Suc)

   931   also have "\<dots> = (\<Sum>i\<le>m. (-1) ^ (Suc i) * of_nat (Suc i * (Suc m choose Suc i)))"

   932     by (simp only: sum_atMost_Suc_shift sum_distrib_left[symmetric] mult_ac of_nat_mult) simp

   933   also have "\<dots> = - of_nat (Suc m) * (\<Sum>i\<le>m. (-1) ^ i * of_nat (m choose i))"

   934     by (subst sum_distrib_left, rule sum.cong[OF refl], subst Suc_times_binomial)

   935        (simp add: algebra_simps)

   936   also have "(\<Sum>i\<le>m. (-1 :: 'a) ^ i * of_nat ((m choose i))) = 0"

   937     using choose_alternating_sum[OF \<open>m > 0\<close>] by simp

   938   finally show ?thesis

   939     by simp

   940 qed

   941

   942 lemma vandermonde: "(\<Sum>k\<le>r. (m choose k) * (n choose (r - k))) = (m + n) choose r"

   943 proof (induct n arbitrary: r)

   944   case 0

   945   have "(\<Sum>k\<le>r. (m choose k) * (0 choose (r - k))) = (\<Sum>k\<le>r. if k = r then (m choose k) else 0)"

   946     by (intro sum.cong) simp_all

   947   also have "\<dots> = m choose r"

   948     by (simp add: sum.delta)

   949   finally show ?case

   950     by simp

   951 next

   952   case (Suc n r)

   953   show ?case

   954     by (cases r) (simp_all add: Suc [symmetric] algebra_simps sum.distrib Suc_diff_le)

   955 qed

   956

   957 lemma choose_square_sum: "(\<Sum>k\<le>n. (n choose k)^2) = ((2*n) choose n)"

   958   using vandermonde[of n n n]

   959   by (simp add: power2_eq_square mult_2 binomial_symmetric [symmetric])

   960

   961 lemma pochhammer_binomial_sum:

   962   fixes a b :: "'a::comm_ring_1"

   963   shows "pochhammer (a + b) n = (\<Sum>k\<le>n. of_nat (n choose k) * pochhammer a k * pochhammer b (n - k))"

   964 proof (induction n arbitrary: a b)

   965   case 0

   966   then show ?case by simp

   967 next

   968   case (Suc n a b)

   969   have "(\<Sum>k\<le>Suc n. of_nat (Suc n choose k) * pochhammer a k * pochhammer b (Suc n - k)) =

   970       (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) +

   971       ((\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) +

   972       pochhammer b (Suc n))"

   973     by (subst sum_atMost_Suc_shift) (simp add: ring_distribs sum.distrib)

   974   also have "(\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) =

   975       a * pochhammer ((a + 1) + b) n"

   976     by (subst Suc) (simp add: sum_distrib_left pochhammer_rec mult_ac)

   977   also have "(\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) +

   978         pochhammer b (Suc n) =

   979       (\<Sum>i=0..Suc n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))"

   980     apply (subst sum_head_Suc)

   981     apply simp

   982     apply (subst sum_shift_bounds_cl_Suc_ivl)

   983     apply (simp add: atLeast0AtMost)

   984     done

   985   also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))"

   986     using Suc by (intro sum.mono_neutral_right) (auto simp: not_le binomial_eq_0)

   987   also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc (n - i)))"

   988     by (intro sum.cong) (simp_all add: Suc_diff_le)

   989   also have "\<dots> = b * pochhammer (a + (b + 1)) n"

   990     by (subst Suc) (simp add: sum_distrib_left mult_ac pochhammer_rec)

   991   also have "a * pochhammer ((a + 1) + b) n + b * pochhammer (a + (b + 1)) n =

   992       pochhammer (a + b) (Suc n)"

   993     by (simp add: pochhammer_rec algebra_simps)

   994   finally show ?case ..

   995 qed

   996

   997 text \<open>Contributed by Manuel Eberl, generalised by LCP.

   998   Alternative definition of the binomial coefficient as @{term "\<Prod>i<k. (n - i) / (k - i)"}.\<close>

   999 lemma gbinomial_altdef_of_nat: "x gchoose k = (\<Prod>i = 0..<k. (x - of_nat i) / of_nat (k - i) :: 'a)"

  1000   for k :: nat and x :: "'a::field_char_0"

  1001   by (simp add: prod_dividef gbinomial_prod_rev fact_prod_rev)

  1002

  1003 lemma gbinomial_ge_n_over_k_pow_k:

  1004   fixes k :: nat

  1005     and x :: "'a::linordered_field"

  1006   assumes "of_nat k \<le> x"

  1007   shows "(x / of_nat k :: 'a) ^ k \<le> x gchoose k"

  1008 proof -

  1009   have x: "0 \<le> x"

  1010     using assms of_nat_0_le_iff order_trans by blast

  1011   have "(x / of_nat k :: 'a) ^ k = (\<Prod>i = 0..<k. x / of_nat k :: 'a)"

  1012     by (simp add: prod_constant)

  1013   also have "\<dots> \<le> x gchoose k" (* FIXME *)

  1014     unfolding gbinomial_altdef_of_nat

  1015     apply (safe intro!: prod_mono)

  1016     apply simp_all

  1017     prefer 2

  1018     subgoal premises for i

  1019     proof -

  1020       from assms have "x * of_nat i \<ge> of_nat (i * k)"

  1021         by (metis mult.commute mult_le_cancel_right of_nat_less_0_iff of_nat_mult)

  1022       then have "x * of_nat k - x * of_nat i \<le> x * of_nat k - of_nat (i * k)"

  1023         by arith

  1024       then have "x * of_nat (k - i) \<le> (x - of_nat i) * of_nat k"

  1025         using \<open>i < k\<close> by (simp add: algebra_simps zero_less_mult_iff of_nat_diff)

  1026       then have "x * of_nat (k - i) \<le> (x - of_nat i) * (of_nat k :: 'a)"

  1027         by (simp only: of_nat_mult[symmetric] of_nat_le_iff)

  1028       with assms show ?thesis

  1029         using \<open>i < k\<close> by (simp add: field_simps)

  1030     qed

  1031     apply (simp add: x zero_le_divide_iff)

  1032     done

  1033   finally show ?thesis .

  1034 qed

  1035

  1036 lemma gbinomial_negated_upper: "(a gchoose b) = (-1) ^ b * ((of_nat b - a - 1) gchoose b)"

  1037   by (simp add: gbinomial_pochhammer pochhammer_minus algebra_simps)

  1038

  1039 lemma gbinomial_minus: "((-a) gchoose b) = (-1) ^ b * ((a + of_nat b - 1) gchoose b)"

  1040   by (subst gbinomial_negated_upper) (simp add: add_ac)

  1041

  1042 lemma Suc_times_gbinomial: "of_nat (Suc b) * ((a + 1) gchoose (Suc b)) = (a + 1) * (a gchoose b)"

  1043 proof (cases b)

  1044   case 0

  1045   then show ?thesis by simp

  1046 next

  1047   case (Suc b)

  1048   then have "((a + 1) gchoose (Suc (Suc b))) = (\<Prod>i = 0..Suc b. a + (1 - of_nat i)) / fact (b + 2)"

  1049     by (simp add: field_simps gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost)

  1050   also have "(\<Prod>i = 0..Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)"

  1051     by (simp add: prod.atLeast0_atMost_Suc_shift)

  1052   also have "\<dots> / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)"

  1053     by (simp_all add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost)

  1054   finally show ?thesis by (simp add: Suc field_simps del: of_nat_Suc)

  1055 qed

  1056

  1057 lemma gbinomial_factors: "((a + 1) gchoose (Suc b)) = (a + 1) / of_nat (Suc b) * (a gchoose b)"

  1058 proof (cases b)

  1059   case 0

  1060   then show ?thesis by simp

  1061 next

  1062   case (Suc b)

  1063   then have "((a + 1) gchoose (Suc (Suc b))) = (\<Prod>i = 0 .. Suc b. a + (1 - of_nat i)) / fact (b + 2)"

  1064     by (simp add: field_simps gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost)

  1065   also have "(\<Prod>i = 0 .. Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)"

  1066     by (simp add: prod.atLeast0_atMost_Suc_shift)

  1067   also have "\<dots> / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)"

  1068     by (simp_all add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost atLeast0AtMost)

  1069   finally show ?thesis

  1070     by (simp add: Suc)

  1071 qed

  1072

  1073 lemma gbinomial_rec: "((r + 1) gchoose (Suc k)) = (r gchoose k) * ((r + 1) / of_nat (Suc k))"

  1074   using gbinomial_mult_1[of r k]

  1075   by (subst gbinomial_Suc_Suc) (simp add: field_simps del: of_nat_Suc, simp add: algebra_simps)

  1076

  1077 lemma gbinomial_of_nat_symmetric: "k \<le> n \<Longrightarrow> (of_nat n) gchoose k = (of_nat n) gchoose (n - k)"

  1078   using binomial_symmetric[of k n] by (simp add: binomial_gbinomial [symmetric])

  1079

  1080

  1081 text \<open>The absorption identity (equation 5.5 \cite[p.~157]{GKP}):

  1082 $  1083 {r \choose k} = \frac{r}{k}{r - 1 \choose k - 1},\quad \textnormal{integer } k \neq 0.   1084$\<close>

  1085 lemma gbinomial_absorption': "k > 0 \<Longrightarrow> r gchoose k = (r / of_nat k) * (r - 1 gchoose (k - 1))"

  1086   using gbinomial_rec[of "r - 1" "k - 1"]

  1087   by (simp_all add: gbinomial_rec field_simps del: of_nat_Suc)

  1088

  1089 text \<open>The absorption identity is written in the following form to avoid

  1090 division by $k$ (the lower index) and therefore remove the $k \neq 0$

  1091 restriction\cite[p.~157]{GKP}:

  1092 $  1093 k{r \choose k} = r{r - 1 \choose k - 1}, \quad \textnormal{integer } k.   1094$\<close>

  1095 lemma gbinomial_absorption: "of_nat (Suc k) * (r gchoose Suc k) = r * ((r - 1) gchoose k)"

  1096   using gbinomial_absorption'[of "Suc k" r] by (simp add: field_simps del: of_nat_Suc)

  1097

  1098 text \<open>The absorption identity for natural number binomial coefficients:\<close>

  1099 lemma binomial_absorption: "Suc k * (n choose Suc k) = n * ((n - 1) choose k)"

  1100   by (cases n) (simp_all add: binomial_eq_0 Suc_times_binomial del: binomial_Suc_Suc mult_Suc)

  1101

  1102 text \<open>The absorption companion identity for natural number coefficients,

  1103   following the proof by GKP \cite[p.~157]{GKP}:\<close>

  1104 lemma binomial_absorb_comp: "(n - k) * (n choose k) = n * ((n - 1) choose k)"

  1105   (is "?lhs = ?rhs")

  1106 proof (cases "n \<le> k")

  1107   case True

  1108   then show ?thesis by auto

  1109 next

  1110   case False

  1111   then have "?rhs = Suc ((n - 1) - k) * (n choose Suc ((n - 1) - k))"

  1112     using binomial_symmetric[of k "n - 1"] binomial_absorption[of "(n - 1) - k" n]

  1113     by simp

  1114   also have "Suc ((n - 1) - k) = n - k"

  1115     using False by simp

  1116   also have "n choose \<dots> = n choose k"

  1117     using False by (intro binomial_symmetric [symmetric]) simp_all

  1118   finally show ?thesis ..

  1119 qed

  1120

  1121 text \<open>The generalised absorption companion identity:\<close>

  1122 lemma gbinomial_absorb_comp: "(r - of_nat k) * (r gchoose k) = r * ((r - 1) gchoose k)"

  1123   using pochhammer_absorb_comp[of r k] by (simp add: gbinomial_pochhammer)

  1124

  1125 lemma gbinomial_addition_formula:

  1126   "r gchoose (Suc k) = ((r - 1) gchoose (Suc k)) + ((r - 1) gchoose k)"

  1127   using gbinomial_Suc_Suc[of "r - 1" k] by (simp add: algebra_simps)

  1128

  1129 lemma binomial_addition_formula:

  1130   "0 < n \<Longrightarrow> n choose (Suc k) = ((n - 1) choose (Suc k)) + ((n - 1) choose k)"

  1131   by (subst choose_reduce_nat) simp_all

  1132

  1133 text \<open>

  1134   Equation 5.9 of the reference material \cite[p.~159]{GKP} is a useful

  1135   summation formula, operating on both indices:

  1136   $  1137 \sum\limits_{k \leq n}{r + k \choose k} = {r + n + 1 \choose n},   1138 \quad \textnormal{integer } n.   1139$

  1140 \<close>

  1141 lemma gbinomial_parallel_sum: "(\<Sum>k\<le>n. (r + of_nat k) gchoose k) = (r + of_nat n + 1) gchoose n"

  1142 proof (induct n)

  1143   case 0

  1144   then show ?case by simp

  1145 next

  1146   case (Suc m)

  1147   then show ?case

  1148     using gbinomial_Suc_Suc[of "(r + of_nat m + 1)" m]

  1149     by (simp add: add_ac)

  1150 qed

  1151

  1152

  1153 subsubsection \<open>Summation on the upper index\<close>

  1154

  1155 text \<open>

  1156   Another summation formula is equation 5.10 of the reference material \cite[p.~160]{GKP},

  1157   aptly named \emph{summation on the upper index}:$\sum_{0 \leq k \leq n} {k \choose m} =   1158 {n + 1 \choose m + 1}, \quad \textnormal{integers } m, n \geq 0.$

  1159 \<close>

  1160 lemma gbinomial_sum_up_index:

  1161   "(\<Sum>k = 0..n. (of_nat k gchoose m) :: 'a::field_char_0) = (of_nat n + 1) gchoose (m + 1)"

  1162 proof (induct n)

  1163   case 0

  1164   show ?case

  1165     using gbinomial_Suc_Suc[of 0 m]

  1166     by (cases m) auto

  1167 next

  1168   case (Suc n)

  1169   then show ?case

  1170     using gbinomial_Suc_Suc[of "of_nat (Suc n) :: 'a" m]

  1171     by (simp add: add_ac)

  1172 qed

  1173

  1174 lemma gbinomial_index_swap:

  1175   "((-1) ^ m) * ((- (of_nat n) - 1) gchoose m) = ((-1) ^ n) * ((- (of_nat m) - 1) gchoose n)"

  1176   (is "?lhs = ?rhs")

  1177 proof -

  1178   have "?lhs = (of_nat (m + n) gchoose m)"

  1179     by (subst gbinomial_negated_upper) (simp add: power_mult_distrib [symmetric])

  1180   also have "\<dots> = (of_nat (m + n) gchoose n)"

  1181     by (subst gbinomial_of_nat_symmetric) simp_all

  1182   also have "\<dots> = ?rhs"

  1183     by (subst gbinomial_negated_upper) simp

  1184   finally show ?thesis .

  1185 qed

  1186

  1187 lemma gbinomial_sum_lower_neg: "(\<Sum>k\<le>m. (r gchoose k) * (- 1) ^ k) = (- 1) ^ m * (r - 1 gchoose m)"

  1188   (is "?lhs = ?rhs")

  1189 proof -

  1190   have "?lhs = (\<Sum>k\<le>m. -(r + 1) + of_nat k gchoose k)"

  1191     by (intro sum.cong[OF refl]) (subst gbinomial_negated_upper, simp add: power_mult_distrib)

  1192   also have "\<dots>  = - r + of_nat m gchoose m"

  1193     by (subst gbinomial_parallel_sum) simp

  1194   also have "\<dots> = ?rhs"

  1195     by (subst gbinomial_negated_upper) (simp add: power_mult_distrib)

  1196   finally show ?thesis .

  1197 qed

  1198

  1199 lemma gbinomial_partial_row_sum:

  1200   "(\<Sum>k\<le>m. (r gchoose k) * ((r / 2) - of_nat k)) = ((of_nat m + 1)/2) * (r gchoose (m + 1))"

  1201 proof (induct m)

  1202   case 0

  1203   then show ?case by simp

  1204 next

  1205   case (Suc mm)

  1206   then have "(\<Sum>k\<le>Suc mm. (r gchoose k) * (r / 2 - of_nat k)) =

  1207       (r - of_nat (Suc mm)) * (r gchoose Suc mm) / 2"

  1208     by (simp add: field_simps)

  1209   also have "\<dots> = r * (r - 1 gchoose Suc mm) / 2"

  1210     by (subst gbinomial_absorb_comp) (rule refl)

  1211   also have "\<dots> = (of_nat (Suc mm) + 1) / 2 * (r gchoose (Suc mm + 1))"

  1212     by (subst gbinomial_absorption [symmetric]) simp

  1213   finally show ?case .

  1214 qed

  1215

  1216 lemma sum_bounds_lt_plus1: "(\<Sum>k<mm. f (Suc k)) = (\<Sum>k=1..mm. f k)"

  1217   by (induct mm) simp_all

  1218

  1219 lemma gbinomial_partial_sum_poly:

  1220   "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) =

  1221     (\<Sum>k\<le>m. (-r gchoose k) * (-x)^k * (x + y)^(m-k))"

  1222   (is "?lhs m = ?rhs m")

  1223 proof (induction m)

  1224   case 0

  1225   then show ?case by simp

  1226 next

  1227   case (Suc mm)

  1228   define G where "G i k = (of_nat i + r gchoose k) * x^k * y^(i - k)" for i k

  1229   define S where "S = ?lhs"

  1230   have SG_def: "S = (\<lambda>i. (\<Sum>k\<le>i. (G i k)))"

  1231     unfolding S_def G_def ..

  1232

  1233   have "S (Suc mm) = G (Suc mm) 0 + (\<Sum>k=Suc 0..Suc mm. G (Suc mm) k)"

  1234     using SG_def by (simp add: sum_head_Suc atLeast0AtMost [symmetric])

  1235   also have "(\<Sum>k=Suc 0..Suc mm. G (Suc mm) k) = (\<Sum>k=0..mm. G (Suc mm) (Suc k))"

  1236     by (subst sum_shift_bounds_cl_Suc_ivl) simp

  1237   also have "\<dots> = (\<Sum>k=0..mm. ((of_nat mm + r gchoose (Suc k)) +

  1238       (of_nat mm + r gchoose k)) * x^(Suc k) * y^(mm - k))"

  1239     unfolding G_def by (subst gbinomial_addition_formula) simp

  1240   also have "\<dots> = (\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) +

  1241       (\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k))"

  1242     by (subst sum.distrib [symmetric]) (simp add: algebra_simps)

  1243   also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) =

  1244       (\<Sum>k<Suc mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k))"

  1245     by (simp only: atLeast0AtMost lessThan_Suc_atMost)

  1246   also have "\<dots> = (\<Sum>k<mm. (of_nat mm + r gchoose Suc k) * x^(Suc k) * y^(mm-k)) +

  1247       (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"

  1248     (is "_ = ?A + ?B")

  1249     by (subst sum_lessThan_Suc) simp

  1250   also have "?A = (\<Sum>k=1..mm. (of_nat mm + r gchoose k) * x^k * y^(mm - k + 1))"

  1251   proof (subst sum_bounds_lt_plus1 [symmetric], intro sum.cong[OF refl], clarify)

  1252     fix k

  1253     assume "k < mm"

  1254     then have "mm - k = mm - Suc k + 1"

  1255       by linarith

  1256     then show "(of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - k) =

  1257         (of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - Suc k + 1)"

  1258       by (simp only:)

  1259   qed

  1260   also have "\<dots> + ?B = y * (\<Sum>k=1..mm. (G mm k)) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"

  1261     unfolding G_def by (subst sum_distrib_left) (simp add: algebra_simps)

  1262   also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k)) = x * (S mm)"

  1263     unfolding S_def by (subst sum_distrib_left) (simp add: atLeast0AtMost algebra_simps)

  1264   also have "(G (Suc mm) 0) = y * (G mm 0)"

  1265     by (simp add: G_def)

  1266   finally have "S (Suc mm) =

  1267       y * (G mm 0 + (\<Sum>k=1..mm. (G mm k))) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm) + x * (S mm)"

  1268     by (simp add: ring_distribs)

  1269   also have "G mm 0 + (\<Sum>k=1..mm. (G mm k)) = S mm"

  1270     by (simp add: sum_head_Suc[symmetric] SG_def atLeast0AtMost)

  1271   finally have "S (Suc mm) = (x + y) * (S mm) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"

  1272     by (simp add: algebra_simps)

  1273   also have "(of_nat mm + r gchoose (Suc mm)) = (-1) ^ (Suc mm) * (- r gchoose (Suc mm))"

  1274     by (subst gbinomial_negated_upper) simp

  1275   also have "(-1) ^ Suc mm * (- r gchoose Suc mm) * x ^ Suc mm =

  1276       (- r gchoose (Suc mm)) * (-x) ^ Suc mm"

  1277     by (simp add: power_minus[of x])

  1278   also have "(x + y) * S mm + \<dots> = (x + y) * ?rhs mm + (- r gchoose (Suc mm)) * (- x)^Suc mm"

  1279     unfolding S_def by (subst Suc.IH) simp

  1280   also have "(x + y) * ?rhs mm = (\<Sum>n\<le>mm. ((- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n)))"

  1281     by (subst sum_distrib_left, rule sum.cong) (simp_all add: Suc_diff_le)

  1282   also have "\<dots> + (-r gchoose (Suc mm)) * (-x)^Suc mm =

  1283       (\<Sum>n\<le>Suc mm. (- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n))"

  1284     by simp

  1285   finally show ?case

  1286     by (simp only: S_def)

  1287 qed

  1288

  1289 lemma gbinomial_partial_sum_poly_xpos:

  1290   "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) =

  1291      (\<Sum>k\<le>m. (of_nat k + r - 1 gchoose k) * x^k * (x + y)^(m-k))"

  1292   apply (subst gbinomial_partial_sum_poly)

  1293   apply (subst gbinomial_negated_upper)

  1294   apply (intro sum.cong, rule refl)

  1295   apply (simp add: power_mult_distrib [symmetric])

  1296   done

  1297

  1298 lemma binomial_r_part_sum: "(\<Sum>k\<le>m. (2 * m + 1 choose k)) = 2 ^ (2 * m)"

  1299 proof -

  1300   have "2 * 2^(2*m) = (\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k))"

  1301     using choose_row_sum[where n="2 * m + 1"] by simp

  1302   also have "(\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k)) =

  1303       (\<Sum>k = 0..m. (2 * m + 1 choose k)) +

  1304       (\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k))"

  1305     using sum_ub_add_nat[of 0 m "\<lambda>k. 2 * m + 1 choose k" "m+1"]

  1306     by (simp add: mult_2)

  1307   also have "(\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k)) =

  1308       (\<Sum>k = 0..m. (2 * m + 1 choose (k + (m + 1))))"

  1309     by (subst sum_shift_bounds_cl_nat_ivl [symmetric]) (simp add: mult_2)

  1310   also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose (m - k)))"

  1311     by (intro sum.cong[OF refl], subst binomial_symmetric) simp_all

  1312   also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose k))"

  1313     using sum.atLeast_atMost_rev [of "\<lambda>k. 2 * m + 1 choose (m - k)" 0 m]

  1314     by simp

  1315   also have "\<dots> + \<dots> = 2 * \<dots>"

  1316     by simp

  1317   finally show ?thesis

  1318     by (subst (asm) mult_cancel1) (simp add: atLeast0AtMost)

  1319 qed

  1320

  1321 lemma gbinomial_r_part_sum: "(\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k)) = 2 ^ (2 * m)"

  1322   (is "?lhs = ?rhs")

  1323 proof -

  1324   have "?lhs = of_nat (\<Sum>k\<le>m. (2 * m + 1) choose k)"

  1325     by (simp add: binomial_gbinomial add_ac)

  1326   also have "\<dots> = of_nat (2 ^ (2 * m))"

  1327     by (subst binomial_r_part_sum) (rule refl)

  1328   finally show ?thesis by simp

  1329 qed

  1330

  1331 lemma gbinomial_sum_nat_pow2:

  1332   "(\<Sum>k\<le>m. (of_nat (m + k) gchoose k :: 'a::field_char_0) / 2 ^ k) = 2 ^ m"

  1333   (is "?lhs = ?rhs")

  1334 proof -

  1335   have "2 ^ m * 2 ^ m = (2 ^ (2*m) :: 'a)"

  1336     by (induct m) simp_all

  1337   also have "\<dots> = (\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k))"

  1338     using gbinomial_r_part_sum ..

  1339   also have "\<dots> = (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) * 2 ^ (m - k))"

  1340     using gbinomial_partial_sum_poly_xpos[where x="1" and y="1" and r="of_nat m + 1" and m="m"]

  1341     by (simp add: add_ac)

  1342   also have "\<dots> = 2 ^ m * (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) / 2 ^ k)"

  1343     by (subst sum_distrib_left) (simp add: algebra_simps power_diff)

  1344   finally show ?thesis

  1345     by (subst (asm) mult_left_cancel) simp_all

  1346 qed

  1347

  1348 lemma gbinomial_trinomial_revision:

  1349   assumes "k \<le> m"

  1350   shows "(r gchoose m) * (of_nat m gchoose k) = (r gchoose k) * (r - of_nat k gchoose (m - k))"

  1351 proof -

  1352   have "(r gchoose m) * (of_nat m gchoose k) = (r gchoose m) * fact m / (fact k * fact (m - k))"

  1353     using assms by (simp add: binomial_gbinomial [symmetric] binomial_fact)

  1354   also have "\<dots> = (r gchoose k) * (r - of_nat k gchoose (m - k))"

  1355     using assms by (simp add: gbinomial_pochhammer power_diff pochhammer_product)

  1356   finally show ?thesis .

  1357 qed

  1358

  1359 text \<open>Versions of the theorems above for the natural-number version of "choose"\<close>

  1360 lemma binomial_altdef_of_nat:

  1361   "k \<le> n \<Longrightarrow> of_nat (n choose k) = (\<Prod>i = 0..<k. of_nat (n - i) / of_nat (k - i) :: 'a)"

  1362   for n k :: nat and x :: "'a::field_char_0"

  1363   by (simp add: gbinomial_altdef_of_nat binomial_gbinomial of_nat_diff)

  1364

  1365 lemma binomial_ge_n_over_k_pow_k: "k \<le> n \<Longrightarrow> (of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)"

  1366   for k n :: nat and x :: "'a::linordered_field"

  1367   by (simp add: gbinomial_ge_n_over_k_pow_k binomial_gbinomial of_nat_diff)

  1368

  1369 lemma binomial_le_pow:

  1370   assumes "r \<le> n"

  1371   shows "n choose r \<le> n ^ r"

  1372 proof -

  1373   have "n choose r \<le> fact n div fact (n - r)"

  1374     using assms by (subst binomial_fact_lemma[symmetric]) auto

  1375   with fact_div_fact_le_pow [OF assms] show ?thesis

  1376     by auto

  1377 qed

  1378

  1379 lemma binomial_altdef_nat: "k \<le> n \<Longrightarrow> n choose k = fact n div (fact k * fact (n - k))"

  1380   for k n :: nat

  1381   by (subst binomial_fact_lemma [symmetric]) auto

  1382

  1383 lemma choose_dvd:

  1384   "k \<le> n \<Longrightarrow> fact k * fact (n - k) dvd (fact n :: 'a::{semiring_div,linordered_semidom})"

  1385   unfolding dvd_def

  1386   apply (rule exI [where x="of_nat (n choose k)"])

  1387   using binomial_fact_lemma [of k n, THEN arg_cong [where f = of_nat and 'b='a]]

  1388   apply auto

  1389   done

  1390

  1391 lemma fact_fact_dvd_fact:

  1392   "fact k * fact n dvd (fact (k + n) :: 'a::{semiring_div,linordered_semidom})"

  1393   by (metis add.commute add_diff_cancel_left' choose_dvd le_add2)

  1394

  1395 lemma choose_mult_lemma:

  1396   "((m + r + k) choose (m + k)) * ((m + k) choose k) = ((m + r + k) choose k) * ((m + r) choose m)"

  1397   (is "?lhs = _")

  1398 proof -

  1399   have "?lhs =

  1400       fact (m + r + k) div (fact (m + k) * fact (m + r - m)) * (fact (m + k) div (fact k * fact m))"

  1401     by (simp add: binomial_altdef_nat)

  1402   also have "\<dots> = fact (m + r + k) div (fact r * (fact k * fact m))"

  1403     apply (subst div_mult_div_if_dvd)

  1404     apply (auto simp: algebra_simps fact_fact_dvd_fact)

  1405     apply (metis add.assoc add.commute fact_fact_dvd_fact)

  1406     done

  1407   also have "\<dots> = (fact (m + r + k) * fact (m + r)) div (fact r * (fact k * fact m) * fact (m + r))"

  1408     apply (subst div_mult_div_if_dvd [symmetric])

  1409     apply (auto simp add: algebra_simps)

  1410     apply (metis fact_fact_dvd_fact dvd_trans nat_mult_dvd_cancel_disj)

  1411     done

  1412   also have "\<dots> =

  1413       (fact (m + r + k) div (fact k * fact (m + r)) * (fact (m + r) div (fact r * fact m)))"

  1414     apply (subst div_mult_div_if_dvd)

  1415     apply (auto simp: fact_fact_dvd_fact algebra_simps)

  1416     done

  1417   finally show ?thesis

  1418     by (simp add: binomial_altdef_nat mult.commute)

  1419 qed

  1420

  1421 text \<open>The "Subset of a Subset" identity.\<close>

  1422 lemma choose_mult:

  1423   "k \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> (n choose m) * (m choose k) = (n choose k) * ((n - k) choose (m - k))"

  1424   using choose_mult_lemma [of "m-k" "n-m" k] by simp

  1425

  1426

  1427 subsection \<open>More on Binomial Coefficients\<close>

  1428

  1429 lemma choose_one: "n choose 1 = n" for n :: nat

  1430   by simp

  1431

  1432 lemma card_UNION:

  1433   assumes "finite A"

  1434     and "\<forall>k \<in> A. finite k"

  1435   shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))"

  1436   (is "?lhs = ?rhs")

  1437 proof -

  1438   have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))"

  1439     by simp

  1440   also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))"

  1441     (is "_ = nat ?rhs")

  1442     by (subst sum_distrib_left) simp

  1443   also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))"

  1444     using assms by (subst sum.Sigma) auto

  1445   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))"

  1446     by (rule sum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)

  1447   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))"

  1448     using assms

  1449     by (auto intro!: sum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])

  1450   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)))"

  1451     using assms by (subst sum.Sigma) auto

  1452   also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "sum ?lhs _ = _")

  1453   proof (rule sum.cong[OF refl])

  1454     fix x

  1455     assume x: "x \<in> \<Union>A"

  1456     define K where "K = {X \<in> A. x \<in> X}"

  1457     with \<open>finite A\<close> have K: "finite K"

  1458       by auto

  1459     let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I}"

  1460     have "inj_on snd (SIGMA i:{1..card A}. ?I i)"

  1461       using assms by (auto intro!: inj_onI)

  1462     moreover have [symmetric]: "snd  (SIGMA i:{1..card A}. ?I i) = {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}"

  1463       using assms

  1464       by (auto intro!: rev_image_eqI[where x="(card a, a)" for a]

  1465         simp add: card_gt_0_iff[folded Suc_le_eq]

  1466         dest: finite_subset intro: card_mono)

  1467     ultimately have "?lhs x = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))"

  1468       by (rule sum.reindex_cong [where l = snd]) fastforce

  1469     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)))"

  1470       using assms by (subst sum.Sigma) auto

  1471     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))"

  1472       by (subst sum_distrib_left) simp

  1473     also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))"

  1474       (is "_ = ?rhs")

  1475     proof (rule sum.mono_neutral_cong_right[rule_format])

  1476       show "finite {1..card A}"

  1477         by simp

  1478       show "{1..card K} \<subseteq> {1..card A}"

  1479         using \<open>finite A\<close> by (auto simp add: K_def intro: card_mono)

  1480     next

  1481       fix i

  1482       assume "i \<in> {1..card A} - {1..card K}"

  1483       then have i: "i \<le> card A" "card K < i"

  1484         by auto

  1485       have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}"

  1486         by (auto simp add: K_def)

  1487       also have "\<dots> = {}"

  1488         using \<open>finite A\<close> i by (auto simp add: K_def dest: card_mono[rotated 1])

  1489       finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0"

  1490         by (simp only:) simp

  1491     next

  1492       fix i

  1493       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)"

  1494         (is "?lhs = ?rhs")

  1495         by (rule sum.cong) (auto simp add: K_def)

  1496       then show "(- 1) ^ (i + 1) * ?lhs = (- 1) ^ (i + 1) * ?rhs"

  1497         by simp

  1498     qed

  1499     also have "{I. I \<subseteq> K \<and> card I = 0} = {{}}"

  1500       using assms by (auto simp add: card_eq_0_iff K_def dest: finite_subset)

  1501     then have "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1"

  1502       by (subst (2) sum_head_Suc) simp_all

  1503     also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1"

  1504       using K by (subst n_subsets[symmetric]) simp_all

  1505     also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1"

  1506       by (subst sum_distrib_left[symmetric]) simp

  1507     also have "\<dots> =  - ((-1 + 1) ^ card K) + 1"

  1508       by (subst binomial_ring) (simp add: ac_simps)

  1509     also have "\<dots> = 1"

  1510       using x K by (auto simp add: K_def card_gt_0_iff)

  1511     finally show "?lhs x = 1" .

  1512   qed

  1513   also have "nat \<dots> = card (\<Union>A)"

  1514     by simp

  1515   finally show ?thesis ..

  1516 qed

  1517

  1518 text \<open>The number of nat lists of length \<open>m\<close> summing to \<open>N\<close> is @{term "(N + m - 1) choose N"}:\<close>

  1519 lemma card_length_sum_list_rec:

  1520   assumes "m \<ge> 1"

  1521   shows "card {l::nat list. length l = m \<and> sum_list l = N} =

  1522       card {l. length l = (m - 1) \<and> sum_list l = N} +

  1523       card {l. length l = m \<and> sum_list l + 1 = N}"

  1524     (is "card ?C = card ?A + card ?B")

  1525 proof -

  1526   let ?A' = "{l. length l = m \<and> sum_list l = N \<and> hd l = 0}"

  1527   let ?B' = "{l. length l = m \<and> sum_list l = N \<and> hd l \<noteq> 0}"

  1528   let ?f = "\<lambda>l. 0 # l"

  1529   let ?g = "\<lambda>l. (hd l + 1) # tl l"

  1530   have 1: "xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" for x xs

  1531     by simp

  1532   have 2: "xs \<noteq> [] \<Longrightarrow> sum_list(tl xs) = sum_list xs - hd xs" for xs :: "nat list"

  1533     by (auto simp add: neq_Nil_conv)

  1534   have f: "bij_betw ?f ?A ?A'"

  1535     apply (rule bij_betw_byWitness[where f' = tl])

  1536     using assms

  1537     apply (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv)

  1538     done

  1539   have 3: "xs \<noteq> [] \<Longrightarrow> hd xs + (sum_list xs - hd xs) = sum_list xs" for xs :: "nat list"

  1540     by (metis 1 sum_list_simps(2) 2)

  1541   have g: "bij_betw ?g ?B ?B'"

  1542     apply (rule bij_betw_byWitness[where f' = "\<lambda>l. (hd l - 1) # tl l"])

  1543     using assms

  1544     by (auto simp: 2 length_0_conv[symmetric] intro!: 3

  1545         simp del: length_greater_0_conv length_0_conv)

  1546   have fin: "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}" for M N :: nat

  1547     using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto

  1548   have fin_A: "finite ?A" using fin[of _ "N+1"]

  1549     by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"])

  1550       (auto simp: member_le_sum_list_nat less_Suc_eq_le)

  1551   have fin_B: "finite ?B"

  1552     by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"])

  1553       (auto simp: member_le_sum_list_nat less_Suc_eq_le fin)

  1554   have uni: "?C = ?A' \<union> ?B'"

  1555     by auto

  1556   have disj: "?A' \<inter> ?B' = {}" by blast

  1557   have "card ?C = card(?A' \<union> ?B')"

  1558     using uni by simp

  1559   also have "\<dots> = card ?A + card ?B"

  1560     using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g]

  1561       bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B

  1562     by presburger

  1563   finally show ?thesis .

  1564 qed

  1565

  1566 lemma card_length_sum_list: "card {l::nat list. size l = m \<and> sum_list l = N} = (N + m - 1) choose N"

  1567   \<comment> "by Holden Lee, tidied by Tobias Nipkow"

  1568 proof (cases m)

  1569   case 0

  1570   then show ?thesis

  1571     by (cases N) (auto cong: conj_cong)

  1572 next

  1573   case (Suc m')

  1574   have m: "m \<ge> 1"

  1575     by (simp add: Suc)

  1576   then show ?thesis

  1577   proof (induct "N + m - 1" arbitrary: N m)

  1578     case 0  \<comment> "In the base case, the only solution is [0]."

  1579     have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {[0]}"

  1580       by (auto simp: length_Suc_conv)

  1581     have "m = 1 \<and> N = 0"

  1582       using 0 by linarith

  1583     then show ?case

  1584       by simp

  1585   next

  1586     case (Suc k)

  1587     have c1: "card {l::nat list. size l = (m - 1) \<and> sum_list l =  N} = (N + (m - 1) - 1) choose N"

  1588     proof (cases "m = 1")

  1589       case True

  1590       with Suc.hyps have "N \<ge> 1"

  1591         by auto

  1592       with True show ?thesis

  1593         by (simp add: binomial_eq_0)

  1594     next

  1595       case False

  1596       then show ?thesis

  1597         using Suc by fastforce

  1598     qed

  1599     from Suc have c2: "card {l::nat list. size l = m \<and> sum_list l + 1 = N} =

  1600       (if N > 0 then ((N - 1) + m - 1) choose (N - 1) else 0)"

  1601     proof -

  1602       have *: "n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" for m n

  1603         by arith

  1604       from Suc have "N > 0 \<Longrightarrow>

  1605         card {l::nat list. size l = m \<and> sum_list l + 1 = N} =

  1606           ((N - 1) + m - 1) choose (N - 1)"

  1607         by (simp add: *)

  1608       then show ?thesis

  1609         by auto

  1610     qed

  1611     from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> sum_list l = N} +

  1612           card {l::nat list. size l = m \<and> sum_list l + 1 = N}) = (N + m - 1) choose N"

  1613       by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def)

  1614     then show ?case

  1615       using card_length_sum_list_rec[OF Suc.prems] by auto

  1616   qed

  1617 qed

  1618

  1619 lemma card_disjoint_shuffle:

  1620   assumes "set xs \<inter> set ys = {}"

  1621   shows   "card (shuffle xs ys) = (length xs + length ys) choose length xs"

  1622 using assms

  1623 proof (induction xs ys rule: shuffle.induct)

  1624   case (3 x xs y ys)

  1625   have "shuffle (x # xs) (y # ys) = op # x  shuffle xs (y # ys) \<union> op # y  shuffle (x # xs) ys"

  1626     by (rule shuffle.simps)

  1627   also have "card \<dots> = card (op # x  shuffle xs (y # ys)) + card (op # y  shuffle (x # xs) ys)"

  1628     by (rule card_Un_disjoint) (insert "3.prems", auto)

  1629   also have "card (op # x  shuffle xs (y # ys)) = card (shuffle xs (y # ys))"

  1630     by (rule card_image) auto

  1631   also have "\<dots> = (length xs + length (y # ys)) choose length xs"

  1632     using "3.prems" by (intro "3.IH") auto

  1633   also have "card (op # y  shuffle (x # xs) ys) = card (shuffle (x # xs) ys)"

  1634     by (rule card_image) auto

  1635   also have "\<dots> = (length (x # xs) + length ys) choose length (x # xs)"

  1636     using "3.prems" by (intro "3.IH") auto

  1637   also have "length xs + length (y # ys) choose length xs + \<dots> =

  1638                (length (x # xs) + length (y # ys)) choose length (x # xs)" by simp

  1639   finally show ?case .

  1640 qed auto

  1641

  1642 lemma Suc_times_binomial_add: "Suc a * (Suc (a + b) choose Suc a) = Suc b * (Suc (a + b) choose a)"

  1643   \<comment> \<open>by Lukas Bulwahn\<close>

  1644 proof -

  1645   have dvd: "Suc a * (fact a * fact b) dvd fact (Suc (a + b))" for a b

  1646     using fact_fact_dvd_fact[of "Suc a" "b", where 'a=nat]

  1647     by (simp only: fact_Suc add_Suc[symmetric] of_nat_id mult.assoc)

  1648   have "Suc a * (fact (Suc (a + b)) div (Suc a * fact a * fact b)) =

  1649       Suc a * fact (Suc (a + b)) div (Suc a * (fact a * fact b))"

  1650     by (subst div_mult_swap[symmetric]; simp only: mult.assoc dvd)

  1651   also have "\<dots> = Suc b * fact (Suc (a + b)) div (Suc b * (fact a * fact b))"

  1652     by (simp only: div_mult_mult1)

  1653   also have "\<dots> = Suc b * (fact (Suc (a + b)) div (Suc b * (fact a * fact b)))"

  1654     using dvd[of b a] by (subst div_mult_swap[symmetric]; simp only: ac_simps dvd)

  1655   finally show ?thesis

  1656     by (subst (1 2) binomial_altdef_nat)

  1657       (simp_all only: ac_simps diff_Suc_Suc Suc_diff_le diff_add_inverse fact_Suc of_nat_id)

  1658 qed

  1659

  1660

  1661 subsection \<open>Misc\<close>

  1662

  1663 lemma fact_code [code]:

  1664   "fact n = (of_nat (fold_atLeastAtMost_nat (op *) 2 n 1) :: 'a::semiring_char_0)"

  1665 proof -

  1666   have "fact n = (of_nat (\<Prod>{1..n}) :: 'a)"

  1667     by (simp add: fact_prod)

  1668   also have "\<Prod>{1..n} = \<Prod>{2..n}"

  1669     by (intro prod.mono_neutral_right) auto

  1670   also have "\<dots> = fold_atLeastAtMost_nat (op *) 2 n 1"

  1671     by (simp add: prod_atLeastAtMost_code)

  1672   finally show ?thesis .

  1673 qed

  1674

  1675 lemma pochhammer_code [code]:

  1676   "pochhammer a n =

  1677     (if n = 0 then 1

  1678      else fold_atLeastAtMost_nat (\<lambda>n acc. (a + of_nat n) * acc) 0 (n - 1) 1)"

  1679   by (cases n)

  1680     (simp_all add: pochhammer_prod prod_atLeastAtMost_code [symmetric]

  1681       atLeastLessThanSuc_atLeastAtMost)

  1682

  1683 lemma gbinomial_code [code]:

  1684   "a gchoose n =

  1685     (if n = 0 then 1

  1686      else fold_atLeastAtMost_nat (\<lambda>n acc. (a - of_nat n) * acc) 0 (n - 1) 1 / fact n)"

  1687   by (cases n)

  1688     (simp_all add: gbinomial_prod_rev prod_atLeastAtMost_code [symmetric]

  1689       atLeastLessThanSuc_atLeastAtMost)

  1690

  1691 (* FIXME *)

  1692 (*TODO: This code equation breaks Scala code generation in HOL-Codegenerator_Test. We have to figure out why and how to prevent that. *)

  1693

  1694 (*

  1695 lemma binomial_code [code]:

  1696   "(n choose k) =

  1697       (if k > n then 0

  1698        else if 2 * k > n then (n choose (n - k))

  1699        else (fold_atLeastAtMost_nat (op * ) (n-k+1) n 1 div fact k))"

  1700 proof -

  1701   {

  1702     assume "k \<le> n"

  1703     then have "{1..n} = {1..n-k} \<union> {n-k+1..n}" by auto

  1704     then have "(fact n :: nat) = fact (n-k) * \<Prod>{n-k+1..n}"

  1705       by (simp add: prod.union_disjoint fact_altdef_nat)

  1706   }

  1707   then show ?thesis by (auto simp: binomial_altdef_nat mult_ac prod_atLeastAtMost_code)

  1708 qed

  1709 *)

  1710

  1711 end