src/HOL/Binomial.thy
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (19 months ago)
changeset 67022 49309fe530fd
parent 66806 a4e82b58d833
child 67299 ba52a058942f
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
wenzelm@63466
     1
(*  Title:      HOL/Binomial.thy
wenzelm@63466
     2
    Author:     Jacques D. Fleuriot
wenzelm@63466
     3
    Author:     Lawrence C Paulson
wenzelm@63466
     4
    Author:     Jeremy Avigad
wenzelm@63466
     5
    Author:     Chaitanya Mangla
wenzelm@63466
     6
    Author:     Manuel Eberl
paulson@12196
     7
*)
paulson@12196
     8
haftmann@65812
     9
section \<open>Binomial Coefficients and Binomial Theorem\<close>
paulson@15094
    10
lp15@59669
    11
theory Binomial
haftmann@65813
    12
  imports Presburger Factorial
lp15@59667
    13
begin
hoelzl@62378
    14
haftmann@63373
    15
subsection \<open>Binomial coefficients\<close>
lp15@59658
    16
wenzelm@63466
    17
text \<open>This development is based on the work of Andy Gordon and Florian Kammueller.\<close>
wenzelm@63466
    18
haftmann@63366
    19
text \<open>Combinatorial definition\<close>
haftmann@63366
    20
wenzelm@63466
    21
definition binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat"  (infixl "choose" 65)
wenzelm@63466
    22
  where "n choose k = card {K\<in>Pow {0..<n}. card K = k}"
lp15@59658
    23
haftmann@63366
    24
theorem n_subsets:
haftmann@63366
    25
  assumes "finite A"
haftmann@63366
    26
  shows "card {B. B \<subseteq> A \<and> card B = k} = card A choose k"
haftmann@63366
    27
proof -
haftmann@63417
    28
  from assms obtain f where bij: "bij_betw f {0..<card A} A"
haftmann@63417
    29
    by (blast dest: ex_bij_betw_nat_finite)
haftmann@63417
    30
  then have [simp]: "card (f ` C) = card C" if "C \<subseteq> {0..<card A}" for C
haftmann@63366
    31
    by (meson bij_betw_imp_inj_on bij_betw_subset card_image that)
haftmann@63417
    32
  from bij have "bij_betw (image f) (Pow {0..<card A}) (Pow A)"
haftmann@63366
    33
    by (rule bij_betw_Pow)
haftmann@63417
    34
  then have "inj_on (image f) (Pow {0..<card A})"
haftmann@63366
    35
    by (rule bij_betw_imp_inj_on)
haftmann@63417
    36
  moreover have "{K. K \<subseteq> {0..<card A} \<and> card K = k} \<subseteq> Pow {0..<card A}"
haftmann@63366
    37
    by auto
haftmann@63417
    38
  ultimately have "inj_on (image f) {K. K \<subseteq> {0..<card A} \<and> card K = k}"
haftmann@63366
    39
    by (rule inj_on_subset)
haftmann@63417
    40
  then have "card {K. K \<subseteq> {0..<card A} \<and> card K = k} =
wenzelm@63466
    41
      card (image f ` {K. K \<subseteq> {0..<card A} \<and> card K = k})" (is "_ = card ?C")
haftmann@63366
    42
    by (simp add: card_image)
haftmann@63417
    43
  also have "?C = {K. K \<subseteq> f ` {0..<card A} \<and> card K = k}"
haftmann@63366
    44
    by (auto elim!: subset_imageE)
haftmann@63417
    45
  also have "f ` {0..<card A} = A"
haftmann@63366
    46
    by (meson bij bij_betw_def)
haftmann@63366
    47
  finally show ?thesis
haftmann@63366
    48
    by (simp add: binomial_def)
haftmann@63366
    49
qed
wenzelm@63466
    50
haftmann@63366
    51
text \<open>Recursive characterization\<close>
lp15@59658
    52
wenzelm@63466
    53
lemma binomial_n_0 [simp, code]: "n choose 0 = 1"
haftmann@63366
    54
proof -
haftmann@63417
    55
  have "{K \<in> Pow {0..<n}. card K = 0} = {{}}"
haftmann@63417
    56
    by (auto dest: finite_subset)
haftmann@63366
    57
  then show ?thesis
haftmann@63366
    58
    by (simp add: binomial_def)
haftmann@63366
    59
qed
haftmann@63366
    60
wenzelm@63466
    61
lemma binomial_0_Suc [simp, code]: "0 choose Suc k = 0"
haftmann@63366
    62
  by (simp add: binomial_def)
lp15@59658
    63
wenzelm@63466
    64
lemma binomial_Suc_Suc [simp, code]: "Suc n choose Suc k = (n choose k) + (n choose Suc k)"
haftmann@63366
    65
proof -
haftmann@63417
    66
  let ?P = "\<lambda>n k. {K. K \<subseteq> {0..<n} \<and> card K = k}"
haftmann@63366
    67
  let ?Q = "?P (Suc n) (Suc k)"
haftmann@63366
    68
  have inj: "inj_on (insert n) (?P n k)"
wenzelm@63466
    69
    by rule (auto; metis atLeastLessThan_iff insert_iff less_irrefl subsetCE)
haftmann@63366
    70
  have disjoint: "insert n ` ?P n k \<inter> ?P n (Suc k) = {}"
haftmann@63366
    71
    by auto
haftmann@63366
    72
  have "?Q = {K\<in>?Q. n \<in> K} \<union> {K\<in>?Q. n \<notin> K}"
haftmann@63366
    73
    by auto
haftmann@63366
    74
  also have "{K\<in>?Q. n \<in> K} = insert n ` ?P n k" (is "?A = ?B")
haftmann@63366
    75
  proof (rule set_eqI)
haftmann@63366
    76
    fix K
haftmann@63417
    77
    have K_finite: "finite K" if "K \<subseteq> insert n {0..<n}"
haftmann@63366
    78
      using that by (rule finite_subset) simp_all
haftmann@63366
    79
    have Suc_card_K: "Suc (card K - Suc 0) = card K" if "n \<in> K"
haftmann@63366
    80
      and "finite K"
haftmann@63366
    81
    proof -
haftmann@63366
    82
      from \<open>n \<in> K\<close> obtain L where "K = insert n L" and "n \<notin> L"
haftmann@63366
    83
        by (blast elim: Set.set_insert)
haftmann@63366
    84
      with that show ?thesis by (simp add: card_insert)
haftmann@63366
    85
    qed
haftmann@63366
    86
    show "K \<in> ?A \<longleftrightarrow> K \<in> ?B"
haftmann@63366
    87
      by (subst in_image_insert_iff)
wenzelm@63466
    88
        (auto simp add: card_insert subset_eq_atLeast0_lessThan_finite
wenzelm@63466
    89
          Diff_subset_conv K_finite Suc_card_K)
wenzelm@63466
    90
  qed
haftmann@63366
    91
  also have "{K\<in>?Q. n \<notin> K} = ?P n (Suc k)"
haftmann@63417
    92
    by (auto simp add: atLeast0_lessThan_Suc)
haftmann@63366
    93
  finally show ?thesis using inj disjoint
haftmann@63366
    94
    by (simp add: binomial_def card_Un_disjoint card_image)
haftmann@63366
    95
qed
lp15@59658
    96
wenzelm@63466
    97
lemma binomial_eq_0: "n < k \<Longrightarrow> n choose k = 0"
haftmann@63417
    98
  by (auto simp add: binomial_def dest: subset_eq_atLeast0_lessThan_card)
haftmann@63366
    99
haftmann@63366
   100
lemma zero_less_binomial: "k \<le> n \<Longrightarrow> n choose k > 0"
haftmann@63366
   101
  by (induct n k rule: diff_induct) simp_all
lp15@59658
   102
wenzelm@63466
   103
lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> n < k"
haftmann@63366
   104
  by (metis binomial_eq_0 less_numeral_extra(3) not_less zero_less_binomial)
lp15@59658
   105
wenzelm@63466
   106
lemma zero_less_binomial_iff [simp]: "n choose k > 0 \<longleftrightarrow> k \<le> n"
haftmann@63366
   107
  by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)
lp15@59658
   108
lp15@59658
   109
lemma binomial_n_n [simp]: "n choose n = 1"
lp15@59658
   110
  by (induct n) (simp_all add: binomial_eq_0)
lp15@59658
   111
lp15@59658
   112
lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n"
lp15@59658
   113
  by (induct n) simp_all
lp15@59658
   114
lp15@59658
   115
lemma binomial_1 [simp]: "n choose Suc 0 = n"
lp15@59658
   116
  by (induct n) simp_all
lp15@59658
   117
haftmann@63366
   118
lemma choose_reduce_nat:
wenzelm@63466
   119
  "0 < n \<Longrightarrow> 0 < k \<Longrightarrow>
wenzelm@63466
   120
    n choose k = ((n - 1) choose (k - 1)) + ((n - 1) choose k)"
haftmann@63366
   121
  using binomial_Suc_Suc [of "n - 1" "k - 1"] by simp
lp15@59658
   122
wenzelm@63466
   123
lemma Suc_times_binomial_eq: "Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
haftmann@63366
   124
  apply (induct n arbitrary: k)
wenzelm@63466
   125
   apply simp
wenzelm@63466
   126
   apply arith
lp15@59658
   127
  apply (case_tac k)
lp15@59658
   128
   apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0)
lp15@59658
   129
  done
lp15@59658
   130
lp15@60141
   131
lemma binomial_le_pow2: "n choose k \<le> 2^n"
haftmann@63366
   132
  apply (induct n arbitrary: k)
wenzelm@63466
   133
   apply (case_tac k)
wenzelm@63466
   134
    apply simp_all
lp15@60141
   135
  apply (case_tac k)
wenzelm@63466
   136
   apply auto
haftmann@63366
   137
  apply (simp add: add_le_mono mult_2)
haftmann@63366
   138
  done
lp15@60141
   139
wenzelm@63466
   140
text \<open>The absorption property.\<close>
wenzelm@63466
   141
lemma Suc_times_binomial: "Suc k * (Suc n choose Suc k) = Suc n * (n choose k)"
lp15@59658
   142
  using Suc_times_binomial_eq by auto
lp15@59658
   143
wenzelm@63466
   144
text \<open>This is the well-known version of absorption, but it's harder to use
wenzelm@63466
   145
  because of the need to reason about division.\<close>
wenzelm@63466
   146
lemma binomial_Suc_Suc_eq_times: "(Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
lp15@59658
   147
  by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)
lp15@59658
   148
wenzelm@63466
   149
text \<open>Another version of absorption, with \<open>-1\<close> instead of \<open>Suc\<close>.\<close>
wenzelm@63466
   150
lemma times_binomial_minus1_eq: "0 < k \<Longrightarrow> k * (n choose k) = n * ((n - 1) choose (k - 1))"
lp15@59658
   151
  using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"]
nipkow@63648
   152
  by (auto split: nat_diff_split)
lp15@59658
   153
lp15@59658
   154
wenzelm@60758
   155
subsection \<open>The binomial theorem (courtesy of Tobias Nipkow):\<close>
lp15@59658
   156
wenzelm@63466
   157
text \<open>Avigad's version, generalized to any commutative ring\<close>
wenzelm@63466
   158
theorem binomial_ring: "(a + b :: 'a::{comm_ring_1,power})^n =
wenzelm@63466
   159
  (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))"
lp15@59658
   160
proof (induct n)
wenzelm@63466
   161
  case 0
wenzelm@63466
   162
  then show ?case by simp
lp15@59658
   163
next
lp15@59658
   164
  case (Suc n)
wenzelm@63466
   165
  have decomp: "{0..n+1} = {0} \<union> {n + 1} \<union> {1..n}"
lp15@59658
   166
    by auto
wenzelm@63466
   167
  have decomp2: "{0..n} = {0} \<union> {1..n}"
lp15@59658
   168
    by auto
wenzelm@63466
   169
  have "(a + b)^(n+1) = (a + b) * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n - k))"
lp15@59658
   170
    using Suc.hyps by simp
wenzelm@63466
   171
  also have "\<dots> = a * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +
wenzelm@63466
   172
      b * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
lp15@59658
   173
    by (rule distrib_right)
lp15@59658
   174
  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +
wenzelm@63466
   175
      (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n - k + 1))"
nipkow@64267
   176
    by (auto simp add: sum_distrib_left ac_simps)
wenzelm@63466
   177
  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n + 1 - k)) +
wenzelm@63466
   178
      (\<Sum>k=1..n+1. of_nat (n choose (k - 1)) * a^k * b^(n + 1 - k))"
nipkow@64267
   179
    by (simp add:sum_shift_bounds_cl_Suc_ivl Suc_diff_le field_simps del: sum_cl_ivl_Suc)
wenzelm@63466
   180
  also have "\<dots> = a^(n + 1) + b^(n + 1) +
wenzelm@63466
   181
      (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n + 1 - k)) +
wenzelm@63466
   182
      (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n + 1 - k))"
lp15@59658
   183
    by (simp add: decomp2)
wenzelm@63466
   184
  also have "\<dots> = a^(n + 1) + b^(n + 1) +
wenzelm@63466
   185
      (\<Sum>k=1..n. of_nat (n + 1 choose k) * a^k * b^(n + 1 - k))"
nipkow@64267
   186
    by (auto simp add: field_simps sum.distrib [symmetric] choose_reduce_nat)
wenzelm@63466
   187
  also have "\<dots> = (\<Sum>k=0..n+1. of_nat (n + 1 choose k) * a^k * b^(n + 1 - k))"
lp15@59658
   188
    using decomp by (simp add: field_simps)
wenzelm@63466
   189
  finally show ?case
wenzelm@63466
   190
    by simp
lp15@59658
   191
qed
lp15@59658
   192
wenzelm@63466
   193
text \<open>Original version for the naturals.\<close>
wenzelm@63466
   194
corollary binomial: "(a + b :: nat)^n = (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n - k))"
wenzelm@63466
   195
  using binomial_ring [of "int a" "int b" n]
lp15@59658
   196
  by (simp only: of_nat_add [symmetric] of_nat_mult [symmetric] of_nat_power [symmetric]
nipkow@64267
   197
      of_nat_sum [symmetric] of_nat_eq_iff of_nat_id)
lp15@59658
   198
lp15@59658
   199
lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
lp15@59658
   200
proof (induct n arbitrary: k rule: nat_less_induct)
wenzelm@63466
   201
  fix n k
wenzelm@63466
   202
  assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) = fact m"
wenzelm@63466
   203
  assume kn: "k \<le> n"
lp15@59658
   204
  let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"
wenzelm@63466
   205
  consider "n = 0 \<or> k = 0 \<or> n = k" | m h where "n = Suc m" "k = Suc h" "h < m"
wenzelm@63466
   206
    using kn by atomize_elim presburger
wenzelm@63466
   207
  then show "fact k * fact (n - k) * (n choose k) = fact n"
wenzelm@63466
   208
  proof cases
wenzelm@63466
   209
    case 1
wenzelm@63466
   210
    with kn show ?thesis by auto
wenzelm@63466
   211
  next
wenzelm@63466
   212
    case 2
wenzelm@63466
   213
    note n = \<open>n = Suc m\<close>
wenzelm@63466
   214
    note k = \<open>k = Suc h\<close>
wenzelm@63466
   215
    note hm = \<open>h < m\<close>
wenzelm@63466
   216
    have mn: "m < n"
wenzelm@63466
   217
      using n by arith
wenzelm@63466
   218
    have hm': "h \<le> m"
wenzelm@63466
   219
      using hm by arith
wenzelm@63466
   220
    have km: "k \<le> m"
wenzelm@63466
   221
      using hm k n kn by arith
wenzelm@63466
   222
    have "m - h = Suc (m - Suc h)"
wenzelm@63466
   223
      using  k km hm by arith
wenzelm@63466
   224
    with km k have "fact (m - h) = (m - h) * fact (m - k)"
lp15@59658
   225
      by simp
wenzelm@63466
   226
    with n k have "fact k * fact (n - k) * (n choose k) =
lp15@59667
   227
        k * (fact h * fact (m - h) * (m choose h)) +
lp15@59658
   228
        (m - h) * (fact k * fact (m - k) * (m choose k))"
lp15@59658
   229
      by (simp add: field_simps)
lp15@59658
   230
    also have "\<dots> = (k + (m - h)) * fact m"
lp15@59658
   231
      using H[rule_format, OF mn hm'] H[rule_format, OF mn km]
lp15@59658
   232
      by (simp add: field_simps)
wenzelm@63466
   233
    finally show ?thesis
wenzelm@63466
   234
      using k n km by simp
wenzelm@63466
   235
  qed
lp15@59658
   236
qed
lp15@59658
   237
haftmann@63372
   238
lemma binomial_fact':
haftmann@63372
   239
  assumes "k \<le> n"
haftmann@63372
   240
  shows "n choose k = fact n div (fact k * fact (n - k))"
haftmann@63372
   241
  using binomial_fact_lemma [OF assms]
haftmann@64240
   242
  by (metis fact_nonzero mult_eq_0_iff nonzero_mult_div_cancel_left)
haftmann@63372
   243
lp15@59658
   244
lemma binomial_fact:
lp15@59658
   245
  assumes kn: "k \<le> n"
wenzelm@63466
   246
  shows "(of_nat (n choose k) :: 'a::field_char_0) = fact n / (fact k * fact (n - k))"
lp15@59658
   247
  using binomial_fact_lemma[OF kn]
lp15@59730
   248
  apply (simp add: field_simps)
wenzelm@63466
   249
  apply (metis mult.commute of_nat_fact of_nat_mult)
wenzelm@63466
   250
  done
lp15@59658
   251
haftmann@63417
   252
lemma fact_binomial:
haftmann@63417
   253
  assumes "k \<le> n"
haftmann@63417
   254
  shows "fact k * of_nat (n choose k) = (fact n / fact (n - k) :: 'a::field_char_0)"
haftmann@63417
   255
  unfolding binomial_fact [OF assms] by (simp add: field_simps)
haftmann@63417
   256
wenzelm@63466
   257
lemma choose_two: "n choose 2 = n * (n - 1) div 2"
haftmann@63417
   258
proof (cases "n \<ge> 2")
haftmann@63417
   259
  case False
haftmann@63417
   260
  then have "n = 0 \<or> n = 1"
haftmann@63417
   261
    by auto
haftmann@63417
   262
  then show ?thesis by auto
haftmann@63417
   263
next
haftmann@63417
   264
  case True
haftmann@63417
   265
  define m where "m = n - 2"
haftmann@63417
   266
  with True have "n = m + 2"
haftmann@63417
   267
    by simp
haftmann@63417
   268
  then have "fact n = n * (n - 1) * fact (n - 2)"
nipkow@64272
   269
    by (simp add: fact_prod_Suc atLeast0_lessThan_Suc algebra_simps)
haftmann@63417
   270
  with True show ?thesis
haftmann@63417
   271
    by (simp add: binomial_fact')
haftmann@63417
   272
qed
haftmann@63417
   273
lp15@59667
   274
lemma choose_row_sum: "(\<Sum>k=0..n. n choose k) = 2^n"
wenzelm@63466
   275
  using binomial [of 1 "1" n] by (simp add: numeral_2_eq_2)
lp15@59667
   276
lp15@59667
   277
lemma sum_choose_lower: "(\<Sum>k=0..n. (r+k) choose k) = Suc (r+n) choose n"
lp15@59667
   278
  by (induct n) auto
lp15@59667
   279
lp15@59667
   280
lemma sum_choose_upper: "(\<Sum>k=0..n. k choose m) = Suc n choose Suc m"
lp15@59667
   281
  by (induct n) auto
lp15@59667
   282
hoelzl@62378
   283
lemma choose_alternating_sum:
wenzelm@63466
   284
  "n > 0 \<Longrightarrow> (\<Sum>i\<le>n. (-1)^i * of_nat (n choose i)) = (0 :: 'a::comm_ring_1)"
wenzelm@63466
   285
  using binomial_ring[of "-1 :: 'a" 1 n]
wenzelm@63466
   286
  by (simp add: atLeast0AtMost mult_of_nat_commute zero_power)
eberlm@61531
   287
eberlm@61531
   288
lemma choose_even_sum:
eberlm@61531
   289
  assumes "n > 0"
wenzelm@63466
   290
  shows "2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0) = (2 ^ n :: 'a::comm_ring_1)"
hoelzl@62378
   291
proof -
eberlm@61531
   292
  have "2 ^ n = (\<Sum>i\<le>n. of_nat (n choose i)) + (\<Sum>i\<le>n. (-1) ^ i * of_nat (n choose i) :: 'a)"
eberlm@61531
   293
    using choose_row_sum[of n]
nipkow@64267
   294
    by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_sum[symmetric])
eberlm@61531
   295
  also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) + (-1) ^ i * of_nat (n choose i))"
nipkow@64267
   296
    by (simp add: sum.distrib)
hoelzl@62378
   297
  also have "\<dots> = 2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0)"
nipkow@64267
   298
    by (subst sum_distrib_left, intro sum.cong) simp_all
eberlm@61531
   299
  finally show ?thesis ..
eberlm@61531
   300
qed
eberlm@61531
   301
eberlm@61531
   302
lemma choose_odd_sum:
eberlm@61531
   303
  assumes "n > 0"
wenzelm@63466
   304
  shows "2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0) = (2 ^ n :: 'a::comm_ring_1)"
hoelzl@62378
   305
proof -
eberlm@61531
   306
  have "2 ^ n = (\<Sum>i\<le>n. of_nat (n choose i)) - (\<Sum>i\<le>n. (-1) ^ i * of_nat (n choose i) :: 'a)"
eberlm@61531
   307
    using choose_row_sum[of n]
nipkow@64267
   308
    by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_sum[symmetric])
eberlm@61531
   309
  also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) - (-1) ^ i * of_nat (n choose i))"
nipkow@64267
   310
    by (simp add: sum_subtractf)
hoelzl@62378
   311
  also have "\<dots> = 2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0)"
nipkow@64267
   312
    by (subst sum_distrib_left, intro sum.cong) simp_all
eberlm@61531
   313
  finally show ?thesis ..
eberlm@61531
   314
qed
eberlm@61531
   315
eberlm@61531
   316
lemma choose_row_sum': "(\<Sum>k\<le>n. (n choose k)) = 2 ^ n"
eberlm@61531
   317
  using choose_row_sum[of n] by (simp add: atLeast0AtMost)
eberlm@61531
   318
wenzelm@60758
   319
text\<open>NW diagonal sum property\<close>
lp15@59667
   320
lemma sum_choose_diagonal:
wenzelm@63466
   321
  assumes "m \<le> n"
wenzelm@63466
   322
  shows "(\<Sum>k=0..m. (n - k) choose (m - k)) = Suc n choose m"
lp15@59667
   323
proof -
wenzelm@63466
   324
  have "(\<Sum>k=0..m. (n-k) choose (m - k)) = (\<Sum>k=0..m. (n - m + k) choose k)"
nipkow@64267
   325
    using sum.atLeast_atMost_rev [of "\<lambda>k. (n - k) choose (m - k)" 0 m] assms
haftmann@63417
   326
      by simp
wenzelm@63466
   327
  also have "\<dots> = Suc (n - m + m) choose m"
lp15@59667
   328
    by (rule sum_choose_lower)
wenzelm@63466
   329
  also have "\<dots> = Suc n choose m"
wenzelm@63466
   330
    using assms by simp
lp15@59667
   331
  finally show ?thesis .
lp15@59667
   332
qed
lp15@59667
   333
haftmann@63373
   334
haftmann@63372
   335
subsection \<open>Generalized binomial coefficients\<close>
lp15@59667
   336
wenzelm@63466
   337
definition gbinomial :: "'a::{semidom_divide,semiring_char_0} \<Rightarrow> nat \<Rightarrow> 'a"  (infixl "gchoose" 65)
nipkow@64272
   338
  where gbinomial_prod_rev: "a gchoose n = prod (\<lambda>i. a - of_nat i) {0..<n} div fact n"
haftmann@63417
   339
haftmann@63417
   340
lemma gbinomial_0 [simp]:
haftmann@63417
   341
  "a gchoose 0 = 1"
haftmann@63417
   342
  "0 gchoose (Suc n) = 0"
nipkow@64272
   343
  by (simp_all add: gbinomial_prod_rev prod.atLeast0_lessThan_Suc_shift)
haftmann@63367
   344
nipkow@64272
   345
lemma gbinomial_Suc: "a gchoose (Suc k) = prod (\<lambda>i. a - of_nat i) {0..k} div fact (Suc k)"
nipkow@64272
   346
  by (simp add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost)
lp15@59667
   347
wenzelm@63466
   348
lemma gbinomial_mult_fact: "fact n * (a gchoose n) = (\<Prod>i = 0..<n. a - of_nat i)"
wenzelm@63466
   349
  for a :: "'a::field_char_0"
nipkow@64272
   350
  by (simp_all add: gbinomial_prod_rev field_simps)
haftmann@63417
   351
wenzelm@63466
   352
lemma gbinomial_mult_fact': "(a gchoose n) * fact n = (\<Prod>i = 0..<n. a - of_nat i)"
wenzelm@63466
   353
  for a :: "'a::field_char_0"
haftmann@63417
   354
  using gbinomial_mult_fact [of n a] by (simp add: ac_simps)
lp15@59667
   355
wenzelm@63466
   356
lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / fact n"
wenzelm@63466
   357
  for a :: "'a::field_char_0"
haftmann@63417
   358
  by (cases n)
wenzelm@63466
   359
    (simp_all add: pochhammer_minus,
nipkow@64272
   360
     simp_all add: gbinomial_prod_rev pochhammer_prod_rev
wenzelm@63466
   361
       power_mult_distrib [symmetric] atLeastLessThanSuc_atLeastAtMost
nipkow@64272
   362
       prod.atLeast_Suc_atMost_Suc_shift of_nat_diff)
lp15@59667
   363
wenzelm@63466
   364
lemma gbinomial_pochhammer': "s gchoose n = pochhammer (s - of_nat n + 1) n / fact n"
wenzelm@63466
   365
  for s :: "'a::field_char_0"
eberlm@61552
   366
proof -
eberlm@61552
   367
  have "s gchoose n = ((-1)^n * (-1)^n) * pochhammer (s - of_nat n + 1) n / fact n"
eberlm@61552
   368
    by (simp add: gbinomial_pochhammer pochhammer_minus mult_ac)
wenzelm@63466
   369
  also have "(-1 :: 'a)^n * (-1)^n = 1"
wenzelm@63466
   370
    by (subst power_add [symmetric]) simp
wenzelm@63466
   371
  finally show ?thesis
wenzelm@63466
   372
    by simp
eberlm@61552
   373
qed
eberlm@61552
   374
wenzelm@63466
   375
lemma gbinomial_binomial: "n gchoose k = n choose k"
haftmann@63372
   376
proof (cases "k \<le> n")
haftmann@63372
   377
  case False
wenzelm@63466
   378
  then have "n < k"
wenzelm@63466
   379
    by (simp add: not_le)
haftmann@63417
   380
  then have "0 \<in> (op - n) ` {0..<k}"
haftmann@63372
   381
    by auto
nipkow@64272
   382
  then have "prod (op - n) {0..<k} = 0"
nipkow@64272
   383
    by (auto intro: prod_zero)
haftmann@63372
   384
  with \<open>n < k\<close> show ?thesis
nipkow@64272
   385
    by (simp add: binomial_eq_0 gbinomial_prod_rev prod_zero)
haftmann@63372
   386
next
haftmann@63372
   387
  case True
eberlm@65350
   388
  from True have *: "prod (op - n) {0..<k} = \<Prod>{Suc (n - k)..n}"
eberlm@65350
   389
    by (intro prod.reindex_bij_witness[of _ "\<lambda>i. n - i" "\<lambda>i. n - i"]) auto
wenzelm@63466
   390
  from True have "n choose k = fact n div (fact k * fact (n - k))"
haftmann@63372
   391
    by (rule binomial_fact')
haftmann@63372
   392
  with * show ?thesis
nipkow@64272
   393
    by (simp add: gbinomial_prod_rev mult.commute [of "fact k"] div_mult2_eq fact_div_fact)
haftmann@63417
   394
qed
haftmann@63417
   395
wenzelm@63466
   396
lemma of_nat_gbinomial: "of_nat (n gchoose k) = (of_nat n gchoose k :: 'a::field_char_0)"
haftmann@63417
   397
proof (cases "k \<le> n")
wenzelm@63466
   398
  case False
wenzelm@63466
   399
  then show ?thesis
nipkow@64272
   400
    by (simp add: not_le gbinomial_binomial binomial_eq_0 gbinomial_prod_rev)
haftmann@63417
   401
next
wenzelm@63466
   402
  case True
wenzelm@63466
   403
  define m where "m = n - k"
wenzelm@63466
   404
  with True have n: "n = m + k"
haftmann@63417
   405
    by arith
haftmann@63417
   406
  from n have "fact n = ((\<Prod>i = 0..<m + k. of_nat (m + k - i) ):: 'a)"
nipkow@64272
   407
    by (simp add: fact_prod_rev)
haftmann@63417
   408
  also have "\<dots> = ((\<Prod>i\<in>{0..<k} \<union> {k..<m + k}. of_nat (m + k - i)) :: 'a)"
haftmann@63417
   409
    by (simp add: ivl_disj_un)
wenzelm@63466
   410
  finally have "fact n = (fact m * (\<Prod>i = 0..<k. of_nat m + of_nat k - of_nat i) :: 'a)"
nipkow@64272
   411
    using prod_shift_bounds_nat_ivl [of "\<lambda>i. of_nat (m + k - i) :: 'a" 0 k m]
nipkow@64272
   412
    by (simp add: fact_prod_rev [of m] prod.union_disjoint of_nat_diff)
wenzelm@63466
   413
  then have "fact n / fact (n - k) = ((\<Prod>i = 0..<k. of_nat n - of_nat i) :: 'a)"
haftmann@63417
   414
    by (simp add: n)
haftmann@63417
   415
  with True have "fact k * of_nat (n gchoose k) = (fact k * (of_nat n gchoose k) :: 'a)"
wenzelm@63466
   416
    by (simp only: gbinomial_mult_fact [of k "of_nat n"] gbinomial_binomial [of n k] fact_binomial)
wenzelm@63466
   417
  then show ?thesis
wenzelm@63466
   418
    by simp
haftmann@63372
   419
qed
haftmann@63372
   420
wenzelm@63466
   421
lemma binomial_gbinomial: "of_nat (n choose k) = (of_nat n gchoose k :: 'a::field_char_0)"
haftmann@63417
   422
  by (simp add: gbinomial_binomial [symmetric] of_nat_gbinomial)
lp15@59667
   423
wenzelm@63466
   424
setup
wenzelm@63466
   425
  \<open>Sign.add_const_constraint (@{const_name gbinomial}, SOME @{typ "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a"})\<close>
haftmann@63372
   426
lp15@59667
   427
lemma gbinomial_1[simp]: "a gchoose 1 = a"
nipkow@64272
   428
  by (simp add: gbinomial_prod_rev lessThan_Suc)
lp15@59667
   429
lp15@59667
   430
lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"
nipkow@64272
   431
  by (simp add: gbinomial_prod_rev lessThan_Suc)
lp15@59667
   432
lp15@59667
   433
lemma gbinomial_mult_1:
wenzelm@63466
   434
  fixes a :: "'a::field_char_0"
wenzelm@63466
   435
  shows "a * (a gchoose n) = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
wenzelm@63466
   436
  (is "?l = ?r")
lp15@59667
   437
proof -
lp15@59730
   438
  have "?r = ((- 1) ^n * pochhammer (- a) n / (fact n)) * (of_nat n - (- a + of_nat n))"
wenzelm@63466
   439
    apply (simp only: gbinomial_pochhammer pochhammer_Suc right_diff_distrib power_Suc)
haftmann@63367
   440
    apply (simp del: of_nat_Suc fact_Suc)
lp15@59730
   441
    apply (auto simp add: field_simps simp del: of_nat_Suc)
lp15@59730
   442
    done
wenzelm@63466
   443
  also have "\<dots> = ?l"
wenzelm@63466
   444
    by (simp add: field_simps gbinomial_pochhammer)
lp15@59667
   445
  finally show ?thesis ..
lp15@59667
   446
qed
lp15@59667
   447
lp15@59667
   448
lemma gbinomial_mult_1':
wenzelm@63466
   449
  "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
wenzelm@63466
   450
  for a :: "'a::field_char_0"
lp15@59667
   451
  by (simp add: mult.commute gbinomial_mult_1)
lp15@59667
   452
wenzelm@63466
   453
lemma gbinomial_Suc_Suc: "(a + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"
wenzelm@63466
   454
  for a :: "'a::field_char_0"
lp15@59667
   455
proof (cases k)
lp15@59667
   456
  case 0
lp15@59667
   457
  then show ?thesis by simp
lp15@59667
   458
next
lp15@59667
   459
  case (Suc h)
haftmann@63417
   460
  have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)"
nipkow@64272
   461
    apply (rule prod.reindex_cong [where l = Suc])
lp15@59667
   462
      using Suc
haftmann@63367
   463
      apply (auto simp add: image_Suc_atMost)
lp15@59667
   464
    done
lp15@59730
   465
  have "fact (Suc k) * (a gchoose k + (a gchoose (Suc k))) =
wenzelm@63466
   466
      (a gchoose Suc h) * (fact (Suc (Suc h))) +
wenzelm@63466
   467
      (a gchoose Suc (Suc h)) * (fact (Suc (Suc h)))"
haftmann@63367
   468
    by (simp add: Suc field_simps del: fact_Suc)
wenzelm@63466
   469
  also have "\<dots> =
wenzelm@63466
   470
    (a gchoose Suc h) * of_nat (Suc (Suc h) * fact (Suc h)) + (\<Prod>i=0..Suc h. a - of_nat i)"
haftmann@63417
   471
    apply (simp del: fact_Suc add: gbinomial_mult_fact field_simps mult.left_commute [of _ "2"])
wenzelm@63466
   472
    apply (simp del: fact_Suc add: fact_Suc [of "Suc h"] field_simps gbinomial_mult_fact
wenzelm@63466
   473
      mult.left_commute [of _ "2"] atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   474
    done
wenzelm@63466
   475
  also have "\<dots> =
wenzelm@63466
   476
    (fact (Suc h) * (a gchoose Suc h)) * of_nat (Suc (Suc h)) + (\<Prod>i=0..Suc h. a - of_nat i)"
haftmann@63367
   477
    by (simp only: fact_Suc mult.commute mult.left_commute of_nat_fact of_nat_id of_nat_mult)
wenzelm@63466
   478
  also have "\<dots> =
wenzelm@63466
   479
    of_nat (Suc (Suc h)) * (\<Prod>i=0..h. a - of_nat i) + (\<Prod>i=0..Suc h. a - of_nat i)"
haftmann@63417
   480
    unfolding gbinomial_mult_fact atLeastLessThanSuc_atLeastAtMost by auto
wenzelm@63466
   481
  also have "\<dots> =
wenzelm@63466
   482
    (\<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))"
lp15@59730
   483
    by (simp add: field_simps)
wenzelm@63466
   484
  also have "\<dots> =
haftmann@63417
   485
    ((a gchoose Suc h) * (fact (Suc h)) * of_nat (Suc k)) + (\<Prod>i\<in>{0..Suc h}. a - of_nat i)"
lp15@59667
   486
    unfolding gbinomial_mult_fact'
haftmann@63417
   487
    by (simp add: comm_semiring_class.distrib field_simps Suc atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   488
  also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
haftmann@63417
   489
    unfolding gbinomial_mult_fact' atLeast0_atMost_Suc
haftmann@63417
   490
    by (simp add: field_simps Suc atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   491
  also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
haftmann@63417
   492
    using eq0
nipkow@64272
   493
    by (simp add: Suc prod.atLeast0_atMost_Suc_shift)
lp15@59730
   494
  also have "\<dots> = (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
wenzelm@63466
   495
    by (simp only: gbinomial_mult_fact atLeastLessThanSuc_atLeastAtMost)
lp15@59730
   496
  finally show ?thesis
haftmann@63417
   497
    using fact_nonzero [of "Suc k"] by auto
lp15@59667
   498
qed
lp15@59667
   499
wenzelm@63466
   500
lemma gbinomial_reduce_nat: "0 < k \<Longrightarrow> a gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)"
wenzelm@63466
   501
  for a :: "'a::field_char_0"
lp15@59730
   502
  by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc)
lp15@59667
   503
lp15@60141
   504
lemma gchoose_row_sum_weighted:
wenzelm@63466
   505
  "(\<Sum>k = 0..m. (r gchoose k) * (r/2 - of_nat k)) = of_nat(Suc m) / 2 * (r gchoose (Suc m))"
wenzelm@63466
   506
  for r :: "'a::field_char_0"
wenzelm@63466
   507
  by (induct m) (simp_all add: field_simps distrib gbinomial_mult_1)
lp15@59667
   508
lp15@59667
   509
lemma binomial_symmetric:
lp15@59667
   510
  assumes kn: "k \<le> n"
lp15@59667
   511
  shows "n choose k = n choose (n - k)"
wenzelm@63466
   512
proof -
wenzelm@63466
   513
  have kn': "n - k \<le> n"
wenzelm@63466
   514
    using kn by arith
lp15@59667
   515
  from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']
wenzelm@63466
   516
  have "fact k * fact (n - k) * (n choose k) = fact (n - k) * fact (n - (n - k)) * (n choose (n - k))"
wenzelm@63466
   517
    by simp
wenzelm@63466
   518
  then show ?thesis
wenzelm@63466
   519
    using kn by simp
lp15@59667
   520
qed
lp15@59667
   521
eberlm@61531
   522
lemma choose_rising_sum:
eberlm@61531
   523
  "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))"
eberlm@61531
   524
  "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose m)"
eberlm@61531
   525
proof -
wenzelm@63466
   526
  show "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))"
wenzelm@63466
   527
    by (induct m) simp_all
wenzelm@63466
   528
  also have "\<dots> = (n + m + 1) choose m"
wenzelm@63466
   529
    by (subst binomial_symmetric) simp_all
wenzelm@63466
   530
  finally show "(\<Sum>j\<le>m. ((n + j) choose n)) = (n + m + 1) choose m" .
eberlm@61531
   531
qed
eberlm@61531
   532
wenzelm@63466
   533
lemma choose_linear_sum: "(\<Sum>i\<le>n. i * (n choose i)) = n * 2 ^ (n - 1)"
eberlm@61531
   534
proof (cases n)
wenzelm@63466
   535
  case 0
wenzelm@63466
   536
  then show ?thesis by simp
wenzelm@63466
   537
next
eberlm@61531
   538
  case (Suc m)
wenzelm@63466
   539
  have "(\<Sum>i\<le>n. i * (n choose i)) = (\<Sum>i\<le>Suc m. i * (Suc m choose i))"
wenzelm@63466
   540
    by (simp add: Suc)
wenzelm@63466
   541
  also have "\<dots> = Suc m * 2 ^ m"
nipkow@64267
   542
    by (simp only: sum_atMost_Suc_shift Suc_times_binomial sum_distrib_left[symmetric])
eberlm@61531
   543
       (simp add: choose_row_sum')
wenzelm@63466
   544
  finally show ?thesis
wenzelm@63466
   545
    using Suc by simp
wenzelm@63466
   546
qed
eberlm@61531
   547
eberlm@61531
   548
lemma choose_alternating_linear_sum:
eberlm@61531
   549
  assumes "n \<noteq> 1"
wenzelm@63466
   550
  shows "(\<Sum>i\<le>n. (-1)^i * of_nat i * of_nat (n choose i) :: 'a::comm_ring_1) = 0"
eberlm@61531
   551
proof (cases n)
wenzelm@63466
   552
  case 0
wenzelm@63466
   553
  then show ?thesis by simp
wenzelm@63466
   554
next
eberlm@61531
   555
  case (Suc m)
wenzelm@63466
   556
  with assms have "m > 0"
wenzelm@63466
   557
    by simp
hoelzl@62378
   558
  have "(\<Sum>i\<le>n. (-1) ^ i * of_nat i * of_nat (n choose i) :: 'a) =
wenzelm@63466
   559
      (\<Sum>i\<le>Suc m. (-1) ^ i * of_nat i * of_nat (Suc m choose i))"
wenzelm@63466
   560
    by (simp add: Suc)
wenzelm@63466
   561
  also have "\<dots> = (\<Sum>i\<le>m. (-1) ^ (Suc i) * of_nat (Suc i * (Suc m choose Suc i)))"
nipkow@64267
   562
    by (simp only: sum_atMost_Suc_shift sum_distrib_left[symmetric] mult_ac of_nat_mult) simp
wenzelm@63466
   563
  also have "\<dots> = - of_nat (Suc m) * (\<Sum>i\<le>m. (-1) ^ i * of_nat (m choose i))"
nipkow@64267
   564
    by (subst sum_distrib_left, rule sum.cong[OF refl], subst Suc_times_binomial)
haftmann@63366
   565
       (simp add: algebra_simps)
eberlm@61531
   566
  also have "(\<Sum>i\<le>m. (-1 :: 'a) ^ i * of_nat ((m choose i))) = 0"
wenzelm@61799
   567
    using choose_alternating_sum[OF \<open>m > 0\<close>] by simp
wenzelm@63466
   568
  finally show ?thesis
wenzelm@63466
   569
    by simp
wenzelm@63466
   570
qed
eberlm@61531
   571
wenzelm@63466
   572
lemma vandermonde: "(\<Sum>k\<le>r. (m choose k) * (n choose (r - k))) = (m + n) choose r"
wenzelm@63466
   573
proof (induct n arbitrary: r)
eberlm@61531
   574
  case 0
eberlm@61531
   575
  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)"
nipkow@64267
   576
    by (intro sum.cong) simp_all
wenzelm@63466
   577
  also have "\<dots> = m choose r"
nipkow@64267
   578
    by (simp add: sum.delta)
wenzelm@63466
   579
  finally show ?case
wenzelm@63466
   580
    by simp
eberlm@61531
   581
next
eberlm@61531
   582
  case (Suc n r)
wenzelm@63466
   583
  show ?case
nipkow@64267
   584
    by (cases r) (simp_all add: Suc [symmetric] algebra_simps sum.distrib Suc_diff_le)
eberlm@61531
   585
qed
eberlm@61531
   586
wenzelm@63466
   587
lemma choose_square_sum: "(\<Sum>k\<le>n. (n choose k)^2) = ((2*n) choose n)"
wenzelm@63466
   588
  using vandermonde[of n n n]
wenzelm@63466
   589
  by (simp add: power2_eq_square mult_2 binomial_symmetric [symmetric])
eberlm@61531
   590
eberlm@61531
   591
lemma pochhammer_binomial_sum:
wenzelm@63466
   592
  fixes a b :: "'a::comm_ring_1"
eberlm@61531
   593
  shows "pochhammer (a + b) n = (\<Sum>k\<le>n. of_nat (n choose k) * pochhammer a k * pochhammer b (n - k))"
eberlm@61531
   594
proof (induction n arbitrary: a b)
wenzelm@63466
   595
  case 0
wenzelm@63466
   596
  then show ?case by simp
wenzelm@63466
   597
next
eberlm@61531
   598
  case (Suc n a b)
eberlm@61531
   599
  have "(\<Sum>k\<le>Suc n. of_nat (Suc n choose k) * pochhammer a k * pochhammer b (Suc n - k)) =
wenzelm@63466
   600
      (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) +
wenzelm@63466
   601
      ((\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) +
wenzelm@63466
   602
      pochhammer b (Suc n))"
nipkow@64267
   603
    by (subst sum_atMost_Suc_shift) (simp add: ring_distribs sum.distrib)
eberlm@61531
   604
  also have "(\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) =
wenzelm@63466
   605
      a * pochhammer ((a + 1) + b) n"
nipkow@64267
   606
    by (subst Suc) (simp add: sum_distrib_left pochhammer_rec mult_ac)
wenzelm@63466
   607
  also have "(\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) +
wenzelm@63466
   608
        pochhammer b (Suc n) =
wenzelm@63466
   609
      (\<Sum>i=0..Suc n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))"
nipkow@64267
   610
    apply (subst sum_head_Suc)
wenzelm@63466
   611
    apply simp
nipkow@64267
   612
    apply (subst sum_shift_bounds_cl_Suc_ivl)
wenzelm@63466
   613
    apply (simp add: atLeast0AtMost)
wenzelm@63466
   614
    done
wenzelm@63466
   615
  also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))"
nipkow@64267
   616
    using Suc by (intro sum.mono_neutral_right) (auto simp: not_le binomial_eq_0)
wenzelm@63466
   617
  also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc (n - i)))"
nipkow@64267
   618
    by (intro sum.cong) (simp_all add: Suc_diff_le)
wenzelm@63466
   619
  also have "\<dots> = b * pochhammer (a + (b + 1)) n"
nipkow@64267
   620
    by (subst Suc) (simp add: sum_distrib_left mult_ac pochhammer_rec)
eberlm@61531
   621
  also have "a * pochhammer ((a + 1) + b) n + b * pochhammer (a + (b + 1)) n =
wenzelm@63466
   622
      pochhammer (a + b) (Suc n)"
wenzelm@63466
   623
    by (simp add: pochhammer_rec algebra_simps)
eberlm@61531
   624
  finally show ?case ..
wenzelm@63466
   625
qed
eberlm@61531
   626
wenzelm@63466
   627
text \<open>Contributed by Manuel Eberl, generalised by LCP.
wenzelm@63466
   628
  Alternative definition of the binomial coefficient as @{term "\<Prod>i<k. (n - i) / (k - i)"}.\<close>
wenzelm@63466
   629
lemma gbinomial_altdef_of_nat: "x gchoose k = (\<Prod>i = 0..<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
wenzelm@63466
   630
  for k :: nat and x :: "'a::field_char_0"
nipkow@64272
   631
  by (simp add: prod_dividef gbinomial_prod_rev fact_prod_rev)
lp15@59667
   632
lp15@59667
   633
lemma gbinomial_ge_n_over_k_pow_k:
lp15@59667
   634
  fixes k :: nat
wenzelm@63466
   635
    and x :: "'a::linordered_field"
lp15@59667
   636
  assumes "of_nat k \<le> x"
lp15@59667
   637
  shows "(x / of_nat k :: 'a) ^ k \<le> x gchoose k"
lp15@59667
   638
proof -
lp15@59667
   639
  have x: "0 \<le> x"
lp15@59667
   640
    using assms of_nat_0_le_iff order_trans by blast
haftmann@63417
   641
  have "(x / of_nat k :: 'a) ^ k = (\<Prod>i = 0..<k. x / of_nat k :: 'a)"
nipkow@64272
   642
    by (simp add: prod_constant)
wenzelm@63466
   643
  also have "\<dots> \<le> x gchoose k" (* FIXME *)
lp15@59667
   644
    unfolding gbinomial_altdef_of_nat
nipkow@64272
   645
    apply (safe intro!: prod_mono)
wenzelm@63466
   646
    apply simp_all
wenzelm@63466
   647
    prefer 2
wenzelm@63466
   648
    subgoal premises for i
wenzelm@63466
   649
    proof -
wenzelm@63466
   650
      from assms have "x * of_nat i \<ge> of_nat (i * k)"
wenzelm@63466
   651
        by (metis mult.commute mult_le_cancel_right of_nat_less_0_iff of_nat_mult)
wenzelm@63466
   652
      then have "x * of_nat k - x * of_nat i \<le> x * of_nat k - of_nat (i * k)"
wenzelm@63466
   653
        by arith
wenzelm@63466
   654
      then have "x * of_nat (k - i) \<le> (x - of_nat i) * of_nat k"
wenzelm@63466
   655
        using \<open>i < k\<close> by (simp add: algebra_simps zero_less_mult_iff of_nat_diff)
wenzelm@63466
   656
      then have "x * of_nat (k - i) \<le> (x - of_nat i) * (of_nat k :: 'a)"
wenzelm@63466
   657
        by (simp only: of_nat_mult[symmetric] of_nat_le_iff)
wenzelm@63466
   658
      with assms show ?thesis
wenzelm@63466
   659
        using \<open>i < k\<close> by (simp add: field_simps)
wenzelm@63466
   660
    qed
wenzelm@63466
   661
    apply (simp add: x zero_le_divide_iff)
wenzelm@63466
   662
    done
lp15@59667
   663
  finally show ?thesis .
lp15@59667
   664
qed
lp15@59667
   665
eberlm@61531
   666
lemma gbinomial_negated_upper: "(a gchoose b) = (-1) ^ b * ((of_nat b - a - 1) gchoose b)"
eberlm@61531
   667
  by (simp add: gbinomial_pochhammer pochhammer_minus algebra_simps)
eberlm@61531
   668
eberlm@61531
   669
lemma gbinomial_minus: "((-a) gchoose b) = (-1) ^ b * ((a + of_nat b - 1) gchoose b)"
eberlm@61531
   670
  by (subst gbinomial_negated_upper) (simp add: add_ac)
eberlm@61531
   671
wenzelm@63466
   672
lemma Suc_times_gbinomial: "of_nat (Suc b) * ((a + 1) gchoose (Suc b)) = (a + 1) * (a gchoose b)"
eberlm@61531
   673
proof (cases b)
wenzelm@63466
   674
  case 0
wenzelm@63466
   675
  then show ?thesis by simp
wenzelm@63466
   676
next
eberlm@61531
   677
  case (Suc b)
wenzelm@63466
   678
  then have "((a + 1) gchoose (Suc (Suc b))) = (\<Prod>i = 0..Suc b. a + (1 - of_nat i)) / fact (b + 2)"
nipkow@64272
   679
    by (simp add: field_simps gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   680
  also have "(\<Prod>i = 0..Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)"
nipkow@64272
   681
    by (simp add: prod.atLeast0_atMost_Suc_shift)
wenzelm@63466
   682
  also have "\<dots> / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)"
nipkow@64272
   683
    by (simp_all add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost)
eberlm@61531
   684
  finally show ?thesis by (simp add: Suc field_simps del: of_nat_Suc)
wenzelm@63466
   685
qed
eberlm@61531
   686
wenzelm@63466
   687
lemma gbinomial_factors: "((a + 1) gchoose (Suc b)) = (a + 1) / of_nat (Suc b) * (a gchoose b)"
eberlm@61531
   688
proof (cases b)
wenzelm@63466
   689
  case 0
wenzelm@63466
   690
  then show ?thesis by simp
wenzelm@63466
   691
next
eberlm@61531
   692
  case (Suc b)
wenzelm@63466
   693
  then have "((a + 1) gchoose (Suc (Suc b))) = (\<Prod>i = 0 .. Suc b. a + (1 - of_nat i)) / fact (b + 2)"
nipkow@64272
   694
    by (simp add: field_simps gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   695
  also have "(\<Prod>i = 0 .. Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)"
nipkow@64272
   696
    by (simp add: prod.atLeast0_atMost_Suc_shift)
wenzelm@63466
   697
  also have "\<dots> / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)"
nipkow@64272
   698
    by (simp_all add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost atLeast0AtMost)
wenzelm@63466
   699
  finally show ?thesis
wenzelm@63466
   700
    by (simp add: Suc)
wenzelm@63466
   701
qed
eberlm@61531
   702
eberlm@61531
   703
lemma gbinomial_rec: "((r + 1) gchoose (Suc k)) = (r gchoose k) * ((r + 1) / of_nat (Suc k))"
eberlm@61531
   704
  using gbinomial_mult_1[of r k]
eberlm@61531
   705
  by (subst gbinomial_Suc_Suc) (simp add: field_simps del: of_nat_Suc, simp add: algebra_simps)
eberlm@61531
   706
eberlm@61531
   707
lemma gbinomial_of_nat_symmetric: "k \<le> n \<Longrightarrow> (of_nat n) gchoose k = (of_nat n) gchoose (n - k)"
eberlm@61531
   708
  using binomial_symmetric[of k n] by (simp add: binomial_gbinomial [symmetric])
eberlm@61531
   709
eberlm@61531
   710
wenzelm@63466
   711
text \<open>The absorption identity (equation 5.5 \cite[p.~157]{GKP}):
wenzelm@63466
   712
\[
eberlm@61531
   713
{r \choose k} = \frac{r}{k}{r - 1 \choose k - 1},\quad \textnormal{integer } k \neq 0.
eberlm@61531
   714
\]\<close>
wenzelm@63466
   715
lemma gbinomial_absorption': "k > 0 \<Longrightarrow> r gchoose k = (r / of_nat k) * (r - 1 gchoose (k - 1))"
hoelzl@62378
   716
  using gbinomial_rec[of "r - 1" "k - 1"]
eberlm@61531
   717
  by (simp_all add: gbinomial_rec field_simps del: of_nat_Suc)
eberlm@61531
   718
eberlm@61531
   719
text \<open>The absorption identity is written in the following form to avoid
eberlm@61531
   720
division by $k$ (the lower index) and therefore remove the $k \neq 0$
wenzelm@63466
   721
restriction\cite[p.~157]{GKP}:
wenzelm@63466
   722
\[
eberlm@61531
   723
k{r \choose k} = r{r - 1 \choose k - 1}, \quad \textnormal{integer } k.
eberlm@61531
   724
\]\<close>
wenzelm@63466
   725
lemma gbinomial_absorption: "of_nat (Suc k) * (r gchoose Suc k) = r * ((r - 1) gchoose k)"
eberlm@61531
   726
  using gbinomial_absorption'[of "Suc k" r] by (simp add: field_simps del: of_nat_Suc)
eberlm@61531
   727
eberlm@61531
   728
text \<open>The absorption identity for natural number binomial coefficients:\<close>
wenzelm@63466
   729
lemma binomial_absorption: "Suc k * (n choose Suc k) = n * ((n - 1) choose k)"
eberlm@61531
   730
  by (cases n) (simp_all add: binomial_eq_0 Suc_times_binomial del: binomial_Suc_Suc mult_Suc)
eberlm@61531
   731
eberlm@61531
   732
text \<open>The absorption companion identity for natural number coefficients,
wenzelm@63466
   733
  following the proof by GKP \cite[p.~157]{GKP}:\<close>
wenzelm@63466
   734
lemma binomial_absorb_comp: "(n - k) * (n choose k) = n * ((n - 1) choose k)"
wenzelm@63466
   735
  (is "?lhs = ?rhs")
eberlm@61531
   736
proof (cases "n \<le> k")
wenzelm@63466
   737
  case True
wenzelm@63466
   738
  then show ?thesis by auto
wenzelm@63466
   739
next
eberlm@61531
   740
  case False
eberlm@61531
   741
  then have "?rhs = Suc ((n - 1) - k) * (n choose Suc ((n - 1) - k))"
eberlm@61531
   742
    using binomial_symmetric[of k "n - 1"] binomial_absorption[of "(n - 1) - k" n]
eberlm@61531
   743
    by simp
wenzelm@63466
   744
  also have "Suc ((n - 1) - k) = n - k"
wenzelm@63466
   745
    using False by simp
wenzelm@63466
   746
  also have "n choose \<dots> = n choose k"
wenzelm@63466
   747
    using False by (intro binomial_symmetric [symmetric]) simp_all
eberlm@61531
   748
  finally show ?thesis ..
wenzelm@63466
   749
qed
eberlm@61531
   750
eberlm@61531
   751
text \<open>The generalised absorption companion identity:\<close>
eberlm@61531
   752
lemma gbinomial_absorb_comp: "(r - of_nat k) * (r gchoose k) = r * ((r - 1) gchoose k)"
eberlm@61531
   753
  using pochhammer_absorb_comp[of r k] by (simp add: gbinomial_pochhammer)
eberlm@61531
   754
eberlm@61531
   755
lemma gbinomial_addition_formula:
eberlm@61531
   756
  "r gchoose (Suc k) = ((r - 1) gchoose (Suc k)) + ((r - 1) gchoose k)"
eberlm@61531
   757
  using gbinomial_Suc_Suc[of "r - 1" k] by (simp add: algebra_simps)
eberlm@61531
   758
eberlm@61531
   759
lemma binomial_addition_formula:
eberlm@61531
   760
  "0 < n \<Longrightarrow> n choose (Suc k) = ((n - 1) choose (Suc k)) + ((n - 1) choose k)"
eberlm@61531
   761
  by (subst choose_reduce_nat) simp_all
eberlm@61531
   762
eberlm@61531
   763
text \<open>
eberlm@61531
   764
  Equation 5.9 of the reference material \cite[p.~159]{GKP} is a useful
wenzelm@63466
   765
  summation formula, operating on both indices:
wenzelm@63466
   766
  \[
wenzelm@63466
   767
   \sum\limits_{k \leq n}{r + k \choose k} = {r + n + 1 \choose n},
eberlm@61531
   768
   \quad \textnormal{integer } n.
hoelzl@62378
   769
  \]
eberlm@61531
   770
\<close>
wenzelm@63466
   771
lemma gbinomial_parallel_sum: "(\<Sum>k\<le>n. (r + of_nat k) gchoose k) = (r + of_nat n + 1) gchoose n"
wenzelm@63466
   772
proof (induct n)
wenzelm@63466
   773
  case 0
wenzelm@63466
   774
  then show ?case by simp
wenzelm@63466
   775
next
eberlm@61531
   776
  case (Suc m)
wenzelm@63466
   777
  then show ?case
wenzelm@63466
   778
    using gbinomial_Suc_Suc[of "(r + of_nat m + 1)" m]
wenzelm@63466
   779
    by (simp add: add_ac)
wenzelm@63466
   780
qed
wenzelm@63466
   781
eberlm@61531
   782
haftmann@63373
   783
subsubsection \<open>Summation on the upper index\<close>
wenzelm@63466
   784
eberlm@61531
   785
text \<open>
eberlm@61531
   786
  Another summation formula is equation 5.10 of the reference material \cite[p.~160]{GKP},
hoelzl@62378
   787
  aptly named \emph{summation on the upper index}:\[\sum_{0 \leq k \leq n} {k \choose m} =
eberlm@61531
   788
  {n + 1 \choose m + 1}, \quad \textnormal{integers } m, n \geq 0.\]
eberlm@61531
   789
\<close>
eberlm@61531
   790
lemma gbinomial_sum_up_index:
wenzelm@63466
   791
  "(\<Sum>k = 0..n. (of_nat k gchoose m) :: 'a::field_char_0) = (of_nat n + 1) gchoose (m + 1)"
wenzelm@63466
   792
proof (induct n)
eberlm@61531
   793
  case 0
wenzelm@63466
   794
  show ?case
wenzelm@63466
   795
    using gbinomial_Suc_Suc[of 0 m]
wenzelm@63466
   796
    by (cases m) auto
eberlm@61531
   797
next
eberlm@61531
   798
  case (Suc n)
wenzelm@63466
   799
  then show ?case
wenzelm@63466
   800
    using gbinomial_Suc_Suc[of "of_nat (Suc n) :: 'a" m]
wenzelm@63466
   801
    by (simp add: add_ac)
eberlm@61531
   802
qed
eberlm@61531
   803
hoelzl@62378
   804
lemma gbinomial_index_swap:
eberlm@61531
   805
  "((-1) ^ m) * ((- (of_nat n) - 1) gchoose m) = ((-1) ^ n) * ((- (of_nat m) - 1) gchoose n)"
eberlm@61531
   806
  (is "?lhs = ?rhs")
eberlm@61531
   807
proof -
eberlm@61531
   808
  have "?lhs = (of_nat (m + n) gchoose m)"
eberlm@61531
   809
    by (subst gbinomial_negated_upper) (simp add: power_mult_distrib [symmetric])
wenzelm@63466
   810
  also have "\<dots> = (of_nat (m + n) gchoose n)"
wenzelm@63466
   811
    by (subst gbinomial_of_nat_symmetric) simp_all
wenzelm@63466
   812
  also have "\<dots> = ?rhs"
wenzelm@63466
   813
    by (subst gbinomial_negated_upper) simp
eberlm@61531
   814
  finally show ?thesis .
eberlm@61531
   815
qed
eberlm@61531
   816
wenzelm@63466
   817
lemma gbinomial_sum_lower_neg: "(\<Sum>k\<le>m. (r gchoose k) * (- 1) ^ k) = (- 1) ^ m * (r - 1 gchoose m)"
wenzelm@63466
   818
  (is "?lhs = ?rhs")
eberlm@61531
   819
proof -
eberlm@61531
   820
  have "?lhs = (\<Sum>k\<le>m. -(r + 1) + of_nat k gchoose k)"
nipkow@64267
   821
    by (intro sum.cong[OF refl]) (subst gbinomial_negated_upper, simp add: power_mult_distrib)
wenzelm@63466
   822
  also have "\<dots>  = - r + of_nat m gchoose m"
wenzelm@63466
   823
    by (subst gbinomial_parallel_sum) simp
wenzelm@63466
   824
  also have "\<dots> = ?rhs"
wenzelm@63466
   825
    by (subst gbinomial_negated_upper) (simp add: power_mult_distrib)
eberlm@61531
   826
  finally show ?thesis .
eberlm@61531
   827
qed
eberlm@61531
   828
eberlm@61531
   829
lemma gbinomial_partial_row_sum:
wenzelm@63466
   830
  "(\<Sum>k\<le>m. (r gchoose k) * ((r / 2) - of_nat k)) = ((of_nat m + 1)/2) * (r gchoose (m + 1))"
wenzelm@63466
   831
proof (induct m)
wenzelm@63466
   832
  case 0
wenzelm@63466
   833
  then show ?case by simp
wenzelm@63466
   834
next
eberlm@61531
   835
  case (Suc mm)
wenzelm@63466
   836
  then have "(\<Sum>k\<le>Suc mm. (r gchoose k) * (r / 2 - of_nat k)) =
wenzelm@63466
   837
      (r - of_nat (Suc mm)) * (r gchoose Suc mm) / 2"
wenzelm@63466
   838
    by (simp add: field_simps)
wenzelm@63466
   839
  also have "\<dots> = r * (r - 1 gchoose Suc mm) / 2"
wenzelm@63466
   840
    by (subst gbinomial_absorb_comp) (rule refl)
eberlm@61531
   841
  also have "\<dots> = (of_nat (Suc mm) + 1) / 2 * (r gchoose (Suc mm + 1))"
eberlm@61531
   842
    by (subst gbinomial_absorption [symmetric]) simp
eberlm@61531
   843
  finally show ?case .
wenzelm@63466
   844
qed
eberlm@61531
   845
nipkow@64267
   846
lemma sum_bounds_lt_plus1: "(\<Sum>k<mm. f (Suc k)) = (\<Sum>k=1..mm. f k)"
wenzelm@63466
   847
  by (induct mm) simp_all
eberlm@61531
   848
eberlm@61531
   849
lemma gbinomial_partial_sum_poly:
eberlm@61531
   850
  "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) =
wenzelm@63466
   851
    (\<Sum>k\<le>m. (-r gchoose k) * (-x)^k * (x + y)^(m-k))"
wenzelm@63466
   852
  (is "?lhs m = ?rhs m")
eberlm@61531
   853
proof (induction m)
wenzelm@63466
   854
  case 0
wenzelm@63466
   855
  then show ?case by simp
wenzelm@63466
   856
next
eberlm@61531
   857
  case (Suc mm)
wenzelm@63466
   858
  define G where "G i k = (of_nat i + r gchoose k) * x^k * y^(i - k)" for i k
wenzelm@63040
   859
  define S where "S = ?lhs"
wenzelm@63466
   860
  have SG_def: "S = (\<lambda>i. (\<Sum>k\<le>i. (G i k)))"
wenzelm@63466
   861
    unfolding S_def G_def ..
eberlm@61531
   862
eberlm@61531
   863
  have "S (Suc mm) = G (Suc mm) 0 + (\<Sum>k=Suc 0..Suc mm. G (Suc mm) k)"
nipkow@64267
   864
    using SG_def by (simp add: sum_head_Suc atLeast0AtMost [symmetric])
eberlm@61531
   865
  also have "(\<Sum>k=Suc 0..Suc mm. G (Suc mm) k) = (\<Sum>k=0..mm. G (Suc mm) (Suc k))"
nipkow@64267
   866
    by (subst sum_shift_bounds_cl_Suc_ivl) simp
wenzelm@63466
   867
  also have "\<dots> = (\<Sum>k=0..mm. ((of_nat mm + r gchoose (Suc k)) +
wenzelm@63466
   868
      (of_nat mm + r gchoose k)) * x^(Suc k) * y^(mm - k))"
eberlm@61531
   869
    unfolding G_def by (subst gbinomial_addition_formula) simp
wenzelm@63466
   870
  also have "\<dots> = (\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) +
wenzelm@63466
   871
      (\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k))"
nipkow@64267
   872
    by (subst sum.distrib [symmetric]) (simp add: algebra_simps)
hoelzl@62378
   873
  also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) =
wenzelm@63466
   874
      (\<Sum>k<Suc mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k))"
eberlm@61531
   875
    by (simp only: atLeast0AtMost lessThan_Suc_atMost)
wenzelm@63466
   876
  also have "\<dots> = (\<Sum>k<mm. (of_nat mm + r gchoose Suc k) * x^(Suc k) * y^(mm-k)) +
wenzelm@63466
   877
      (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"
wenzelm@63466
   878
    (is "_ = ?A + ?B")
nipkow@64267
   879
    by (subst sum_lessThan_Suc) simp
eberlm@61531
   880
  also have "?A = (\<Sum>k=1..mm. (of_nat mm + r gchoose k) * x^k * y^(mm - k + 1))"
nipkow@64267
   881
  proof (subst sum_bounds_lt_plus1 [symmetric], intro sum.cong[OF refl], clarify)
wenzelm@63466
   882
    fix k
wenzelm@63466
   883
    assume "k < mm"
wenzelm@63466
   884
    then have "mm - k = mm - Suc k + 1"
wenzelm@63466
   885
      by linarith
wenzelm@63466
   886
    then show "(of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - k) =
wenzelm@63466
   887
        (of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - Suc k + 1)"
wenzelm@63466
   888
      by (simp only:)
eberlm@61531
   889
  qed
hoelzl@62378
   890
  also have "\<dots> + ?B = y * (\<Sum>k=1..mm. (G mm k)) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"
nipkow@64267
   891
    unfolding G_def by (subst sum_distrib_left) (simp add: algebra_simps)
eberlm@61531
   892
  also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k)) = x * (S mm)"
nipkow@64267
   893
    unfolding S_def by (subst sum_distrib_left) (simp add: atLeast0AtMost algebra_simps)
wenzelm@63466
   894
  also have "(G (Suc mm) 0) = y * (G mm 0)"
wenzelm@63466
   895
    by (simp add: G_def)
wenzelm@63466
   896
  finally have "S (Suc mm) =
wenzelm@63466
   897
      y * (G mm 0 + (\<Sum>k=1..mm. (G mm k))) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm) + x * (S mm)"
eberlm@61531
   898
    by (simp add: ring_distribs)
wenzelm@63466
   899
  also have "G mm 0 + (\<Sum>k=1..mm. (G mm k)) = S mm"
nipkow@64267
   900
    by (simp add: sum_head_Suc[symmetric] SG_def atLeast0AtMost)
hoelzl@62378
   901
  finally have "S (Suc mm) = (x + y) * (S mm) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"
eberlm@61531
   902
    by (simp add: algebra_simps)
wenzelm@63466
   903
  also have "(of_nat mm + r gchoose (Suc mm)) = (-1) ^ (Suc mm) * (- r gchoose (Suc mm))"
eberlm@61531
   904
    by (subst gbinomial_negated_upper) simp
eberlm@61531
   905
  also have "(-1) ^ Suc mm * (- r gchoose Suc mm) * x ^ Suc mm =
wenzelm@63466
   906
      (- r gchoose (Suc mm)) * (-x) ^ Suc mm"
wenzelm@63466
   907
    by (simp add: power_minus[of x])
wenzelm@63466
   908
  also have "(x + y) * S mm + \<dots> = (x + y) * ?rhs mm + (- r gchoose (Suc mm)) * (- x)^Suc mm"
eberlm@61531
   909
    unfolding S_def by (subst Suc.IH) simp
eberlm@61531
   910
  also have "(x + y) * ?rhs mm = (\<Sum>n\<le>mm. ((- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n)))"
nipkow@64267
   911
    by (subst sum_distrib_left, rule sum.cong) (simp_all add: Suc_diff_le)
hoelzl@62378
   912
  also have "\<dots> + (-r gchoose (Suc mm)) * (-x)^Suc mm =
wenzelm@63466
   913
      (\<Sum>n\<le>Suc mm. (- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n))"
wenzelm@63466
   914
    by simp
wenzelm@63466
   915
  finally show ?case
wenzelm@63466
   916
    by (simp only: S_def)
wenzelm@63466
   917
qed
eberlm@61531
   918
eberlm@61531
   919
lemma gbinomial_partial_sum_poly_xpos:
hoelzl@62378
   920
  "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) =
eberlm@61531
   921
     (\<Sum>k\<le>m. (of_nat k + r - 1 gchoose k) * x^k * (x + y)^(m-k))"
eberlm@61531
   922
  apply (subst gbinomial_partial_sum_poly)
eberlm@61531
   923
  apply (subst gbinomial_negated_upper)
nipkow@64267
   924
  apply (intro sum.cong, rule refl)
eberlm@61531
   925
  apply (simp add: power_mult_distrib [symmetric])
eberlm@61531
   926
  done
eberlm@61531
   927
eberlm@61531
   928
lemma binomial_r_part_sum: "(\<Sum>k\<le>m. (2 * m + 1 choose k)) = 2 ^ (2 * m)"
eberlm@61531
   929
proof -
eberlm@61531
   930
  have "2 * 2^(2*m) = (\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k))"
eberlm@61531
   931
    using choose_row_sum[where n="2 * m + 1"] by simp
wenzelm@63466
   932
  also have "(\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k)) =
wenzelm@63466
   933
      (\<Sum>k = 0..m. (2 * m + 1 choose k)) +
wenzelm@63466
   934
      (\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k))"
nipkow@64267
   935
    using sum_ub_add_nat[of 0 m "\<lambda>k. 2 * m + 1 choose k" "m+1"]
wenzelm@63466
   936
    by (simp add: mult_2)
eberlm@61531
   937
  also have "(\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k)) =
wenzelm@63466
   938
      (\<Sum>k = 0..m. (2 * m + 1 choose (k + (m + 1))))"
nipkow@64267
   939
    by (subst sum_shift_bounds_cl_nat_ivl [symmetric]) (simp add: mult_2)
eberlm@61531
   940
  also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose (m - k)))"
nipkow@64267
   941
    by (intro sum.cong[OF refl], subst binomial_symmetric) simp_all
eberlm@61531
   942
  also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose k))"
nipkow@64267
   943
    using sum.atLeast_atMost_rev [of "\<lambda>k. 2 * m + 1 choose (m - k)" 0 m]
haftmann@63417
   944
    by simp
wenzelm@63466
   945
  also have "\<dots> + \<dots> = 2 * \<dots>"
wenzelm@63466
   946
    by simp
wenzelm@63466
   947
  finally show ?thesis
wenzelm@63466
   948
    by (subst (asm) mult_cancel1) (simp add: atLeast0AtMost)
eberlm@61531
   949
qed
eberlm@61531
   950
wenzelm@63466
   951
lemma gbinomial_r_part_sum: "(\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k)) = 2 ^ (2 * m)"
wenzelm@63466
   952
  (is "?lhs = ?rhs")
eberlm@61531
   953
proof -
hoelzl@62378
   954
  have "?lhs = of_nat (\<Sum>k\<le>m. (2 * m + 1) choose k)"
haftmann@63366
   955
    by (simp add: binomial_gbinomial add_ac)
wenzelm@63466
   956
  also have "\<dots> = of_nat (2 ^ (2 * m))"
wenzelm@63466
   957
    by (subst binomial_r_part_sum) (rule refl)
haftmann@63366
   958
  finally show ?thesis by simp
eberlm@61531
   959
qed
eberlm@61531
   960
eberlm@61531
   961
lemma gbinomial_sum_nat_pow2:
wenzelm@63466
   962
  "(\<Sum>k\<le>m. (of_nat (m + k) gchoose k :: 'a::field_char_0) / 2 ^ k) = 2 ^ m"
wenzelm@63466
   963
  (is "?lhs = ?rhs")
eberlm@61531
   964
proof -
wenzelm@63466
   965
  have "2 ^ m * 2 ^ m = (2 ^ (2*m) :: 'a)"
wenzelm@63466
   966
    by (induct m) simp_all
wenzelm@63466
   967
  also have "\<dots> = (\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k))"
wenzelm@63466
   968
    using gbinomial_r_part_sum ..
eberlm@61531
   969
  also have "\<dots> = (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) * 2 ^ (m - k))"
eberlm@61531
   970
    using gbinomial_partial_sum_poly_xpos[where x="1" and y="1" and r="of_nat m + 1" and m="m"]
eberlm@61531
   971
    by (simp add: add_ac)
eberlm@61531
   972
  also have "\<dots> = 2 ^ m * (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) / 2 ^ k)"
nipkow@64267
   973
    by (subst sum_distrib_left) (simp add: algebra_simps power_diff)
wenzelm@63466
   974
  finally show ?thesis
wenzelm@63466
   975
    by (subst (asm) mult_left_cancel) simp_all
eberlm@61531
   976
qed
eberlm@61531
   977
eberlm@61531
   978
lemma gbinomial_trinomial_revision:
eberlm@61531
   979
  assumes "k \<le> m"
wenzelm@63466
   980
  shows "(r gchoose m) * (of_nat m gchoose k) = (r gchoose k) * (r - of_nat k gchoose (m - k))"
eberlm@61531
   981
proof -
wenzelm@63466
   982
  have "(r gchoose m) * (of_nat m gchoose k) = (r gchoose m) * fact m / (fact k * fact (m - k))"
eberlm@61531
   983
    using assms by (simp add: binomial_gbinomial [symmetric] binomial_fact)
wenzelm@63466
   984
  also have "\<dots> = (r gchoose k) * (r - of_nat k gchoose (m - k))"
wenzelm@63466
   985
    using assms by (simp add: gbinomial_pochhammer power_diff pochhammer_product)
eberlm@61531
   986
  finally show ?thesis .
eberlm@61531
   987
qed
eberlm@61531
   988
wenzelm@63466
   989
text \<open>Versions of the theorems above for the natural-number version of "choose"\<close>
lp15@59667
   990
lemma binomial_altdef_of_nat:
wenzelm@63466
   991
  "k \<le> n \<Longrightarrow> of_nat (n choose k) = (\<Prod>i = 0..<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
wenzelm@63466
   992
  for n k :: nat and x :: "'a::field_char_0"
wenzelm@63466
   993
  by (simp add: gbinomial_altdef_of_nat binomial_gbinomial of_nat_diff)
lp15@59667
   994
wenzelm@63466
   995
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)"
wenzelm@63466
   996
  for k n :: nat and x :: "'a::linordered_field"
wenzelm@63466
   997
  by (simp add: gbinomial_ge_n_over_k_pow_k binomial_gbinomial of_nat_diff)
lp15@59667
   998
lp15@59667
   999
lemma binomial_le_pow:
lp15@59667
  1000
  assumes "r \<le> n"
lp15@59667
  1001
  shows "n choose r \<le> n ^ r"
lp15@59667
  1002
proof -
lp15@59667
  1003
  have "n choose r \<le> fact n div fact (n - r)"
wenzelm@63466
  1004
    using assms by (subst binomial_fact_lemma[symmetric]) auto
wenzelm@63466
  1005
  with fact_div_fact_le_pow [OF assms] show ?thesis
wenzelm@63466
  1006
    by auto
lp15@59667
  1007
qed
lp15@59667
  1008
wenzelm@63466
  1009
lemma binomial_altdef_nat: "k \<le> n \<Longrightarrow> n choose k = fact n div (fact k * fact (n - k))"
wenzelm@63466
  1010
  for k n :: nat
wenzelm@63466
  1011
  by (subst binomial_fact_lemma [symmetric]) auto
lp15@59667
  1012
wenzelm@63466
  1013
lemma choose_dvd:
haftmann@66806
  1014
  "k \<le> n \<Longrightarrow> fact k * fact (n - k) dvd (fact n :: 'a::linordered_semidom)"
lp15@59730
  1015
  unfolding dvd_def
lp15@59730
  1016
  apply (rule exI [where x="of_nat (n choose k)"])
lp15@59730
  1017
  using binomial_fact_lemma [of k n, THEN arg_cong [where f = of_nat and 'b='a]]
haftmann@63366
  1018
  apply auto
lp15@59667
  1019
  done
lp15@59667
  1020
hoelzl@62378
  1021
lemma fact_fact_dvd_fact:
haftmann@66806
  1022
  "fact k * fact n dvd (fact (k + n) :: 'a::linordered_semidom)"
wenzelm@63466
  1023
  by (metis add.commute add_diff_cancel_left' choose_dvd le_add2)
lp15@59667
  1024
lp15@59667
  1025
lemma choose_mult_lemma:
wenzelm@63466
  1026
  "((m + r + k) choose (m + k)) * ((m + k) choose k) = ((m + r + k) choose k) * ((m + r) choose m)"
wenzelm@63466
  1027
  (is "?lhs = _")
lp15@59667
  1028
proof -
wenzelm@63466
  1029
  have "?lhs =
wenzelm@63466
  1030
      fact (m + r + k) div (fact (m + k) * fact (m + r - m)) * (fact (m + k) div (fact k * fact m))"
wenzelm@63092
  1031
    by (simp add: binomial_altdef_nat)
wenzelm@63466
  1032
  also have "\<dots> = fact (m + r + k) div (fact r * (fact k * fact m))"
lp15@59667
  1033
    apply (subst div_mult_div_if_dvd)
lp15@59730
  1034
    apply (auto simp: algebra_simps fact_fact_dvd_fact)
lp15@59667
  1035
    apply (metis add.assoc add.commute fact_fact_dvd_fact)
lp15@59667
  1036
    done
wenzelm@63466
  1037
  also have "\<dots> = (fact (m + r + k) * fact (m + r)) div (fact r * (fact k * fact m) * fact (m + r))"
lp15@59667
  1038
    apply (subst div_mult_div_if_dvd [symmetric])
lp15@59730
  1039
    apply (auto simp add: algebra_simps)
haftmann@62344
  1040
    apply (metis fact_fact_dvd_fact dvd_trans nat_mult_dvd_cancel_disj)
lp15@59667
  1041
    done
wenzelm@63466
  1042
  also have "\<dots> =
wenzelm@63466
  1043
      (fact (m + r + k) div (fact k * fact (m + r)) * (fact (m + r) div (fact r * fact m)))"
lp15@59667
  1044
    apply (subst div_mult_div_if_dvd)
lp15@59730
  1045
    apply (auto simp: fact_fact_dvd_fact algebra_simps)
lp15@59667
  1046
    done
lp15@59667
  1047
  finally show ?thesis
lp15@59667
  1048
    by (simp add: binomial_altdef_nat mult.commute)
lp15@59667
  1049
qed
lp15@59667
  1050
wenzelm@63466
  1051
text \<open>The "Subset of a Subset" identity.\<close>
lp15@59667
  1052
lemma choose_mult:
wenzelm@63466
  1053
  "k \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> (n choose m) * (m choose k) = (n choose k) * ((n - k) choose (m - k))"
wenzelm@63466
  1054
  using choose_mult_lemma [of "m-k" "n-m" k] by simp
lp15@59667
  1055
lp15@59667
  1056
haftmann@63373
  1057
subsection \<open>More on Binomial Coefficients\<close>
lp15@59667
  1058
wenzelm@63466
  1059
lemma choose_one: "n choose 1 = n" for n :: nat
lp15@59667
  1060
  by simp
lp15@59667
  1061
lp15@59667
  1062
lemma card_UNION:
wenzelm@63466
  1063
  assumes "finite A"
wenzelm@63466
  1064
    and "\<forall>k \<in> A. finite k"
lp15@59667
  1065
  shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))"
lp15@59667
  1066
  (is "?lhs = ?rhs")
lp15@59667
  1067
proof -
wenzelm@63466
  1068
  have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))"
wenzelm@63466
  1069
    by simp
wenzelm@63466
  1070
  also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))"
wenzelm@63466
  1071
    (is "_ = nat ?rhs")
nipkow@64267
  1072
    by (subst sum_distrib_left) simp
lp15@59667
  1073
  also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))"
nipkow@64267
  1074
    using assms by (subst sum.Sigma) auto
lp15@59667
  1075
  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))"
nipkow@64267
  1076
    by (rule sum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)
lp15@59667
  1077
  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))"
wenzelm@63466
  1078
    using assms
nipkow@64267
  1079
    by (auto intro!: sum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])
lp15@59667
  1080
  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)))"
nipkow@64267
  1081
    using assms by (subst sum.Sigma) auto
nipkow@64267
  1082
  also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "sum ?lhs _ = _")
nipkow@64267
  1083
  proof (rule sum.cong[OF refl])
lp15@59667
  1084
    fix x
lp15@59667
  1085
    assume x: "x \<in> \<Union>A"
wenzelm@63040
  1086
    define K where "K = {X \<in> A. x \<in> X}"
wenzelm@63466
  1087
    with \<open>finite A\<close> have K: "finite K"
wenzelm@63466
  1088
      by auto
lp15@59667
  1089
    let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I}"
lp15@59667
  1090
    have "inj_on snd (SIGMA i:{1..card A}. ?I i)"
wenzelm@63466
  1091
      using assms by (auto intro!: inj_onI)
lp15@59667
  1092
    moreover have [symmetric]: "snd ` (SIGMA i:{1..card A}. ?I i) = {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}"
wenzelm@63466
  1093
      using assms
wenzelm@63466
  1094
      by (auto intro!: rev_image_eqI[where x="(card a, a)" for a]
lp15@59667
  1095
        simp add: card_gt_0_iff[folded Suc_le_eq]
lp15@59667
  1096
        dest: finite_subset intro: card_mono)
lp15@59667
  1097
    ultimately have "?lhs x = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))"
nipkow@64267
  1098
      by (rule sum.reindex_cong [where l = snd]) fastforce
lp15@59667
  1099
    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)))"
nipkow@64267
  1100
      using assms by (subst sum.Sigma) auto
lp15@59667
  1101
    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))"
nipkow@64267
  1102
      by (subst sum_distrib_left) simp
wenzelm@63466
  1103
    also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))"
wenzelm@63466
  1104
      (is "_ = ?rhs")
nipkow@64267
  1105
    proof (rule sum.mono_neutral_cong_right[rule_format])
wenzelm@63466
  1106
      show "finite {1..card A}"
wenzelm@63466
  1107
        by simp
wenzelm@63466
  1108
      show "{1..card K} \<subseteq> {1..card A}"
wenzelm@63466
  1109
        using \<open>finite A\<close> by (auto simp add: K_def intro: card_mono)
lp15@59667
  1110
    next
lp15@59667
  1111
      fix i
lp15@59667
  1112
      assume "i \<in> {1..card A} - {1..card K}"
wenzelm@63466
  1113
      then have i: "i \<le> card A" "card K < i"
wenzelm@63466
  1114
        by auto
lp15@59667
  1115
      have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}"
wenzelm@63466
  1116
        by (auto simp add: K_def)
wenzelm@63466
  1117
      also have "\<dots> = {}"
wenzelm@63466
  1118
        using \<open>finite A\<close> i by (auto simp add: K_def dest: card_mono[rotated 1])
lp15@59667
  1119
      finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0"
wenzelm@63466
  1120
        by (simp only:) simp
lp15@59667
  1121
    next
lp15@59667
  1122
      fix i
lp15@59667
  1123
      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)"
lp15@59667
  1124
        (is "?lhs = ?rhs")
nipkow@64267
  1125
        by (rule sum.cong) (auto simp add: K_def)
wenzelm@63466
  1126
      then show "(- 1) ^ (i + 1) * ?lhs = (- 1) ^ (i + 1) * ?rhs"
wenzelm@63466
  1127
        by simp
wenzelm@63466
  1128
    qed
wenzelm@63466
  1129
    also have "{I. I \<subseteq> K \<and> card I = 0} = {{}}"
wenzelm@63466
  1130
      using assms by (auto simp add: card_eq_0_iff K_def dest: finite_subset)
wenzelm@63466
  1131
    then have "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1"
nipkow@64267
  1132
      by (subst (2) sum_head_Suc) simp_all
lp15@59667
  1133
    also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1"
wenzelm@63466
  1134
      using K by (subst n_subsets[symmetric]) simp_all
lp15@59667
  1135
    also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1"
nipkow@64267
  1136
      by (subst sum_distrib_left[symmetric]) simp
lp15@59667
  1137
    also have "\<dots> =  - ((-1 + 1) ^ card K) + 1"
wenzelm@63466
  1138
      by (subst binomial_ring) (simp add: ac_simps)
wenzelm@63466
  1139
    also have "\<dots> = 1"
wenzelm@63466
  1140
      using x K by (auto simp add: K_def card_gt_0_iff)
lp15@59667
  1141
    finally show "?lhs x = 1" .
lp15@59667
  1142
  qed
wenzelm@63466
  1143
  also have "nat \<dots> = card (\<Union>A)"
wenzelm@63466
  1144
    by simp
lp15@59667
  1145
  finally show ?thesis ..
lp15@59667
  1146
qed
lp15@59667
  1147
wenzelm@63466
  1148
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>
nipkow@63882
  1149
lemma card_length_sum_list_rec:
wenzelm@63466
  1150
  assumes "m \<ge> 1"
nipkow@63882
  1151
  shows "card {l::nat list. length l = m \<and> sum_list l = N} =
nipkow@63882
  1152
      card {l. length l = (m - 1) \<and> sum_list l = N} +
nipkow@63882
  1153
      card {l. length l = m \<and> sum_list l + 1 = N}"
wenzelm@63466
  1154
    (is "card ?C = card ?A + card ?B")
lp15@59667
  1155
proof -
nipkow@63882
  1156
  let ?A' = "{l. length l = m \<and> sum_list l = N \<and> hd l = 0}"
nipkow@63882
  1157
  let ?B' = "{l. length l = m \<and> sum_list l = N \<and> hd l \<noteq> 0}"
wenzelm@63466
  1158
  let ?f = "\<lambda>l. 0 # l"
wenzelm@63466
  1159
  let ?g = "\<lambda>l. (hd l + 1) # tl l"
haftmann@65812
  1160
  have 1: "xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" for x :: nat and xs
wenzelm@63466
  1161
    by simp
nipkow@63882
  1162
  have 2: "xs \<noteq> [] \<Longrightarrow> sum_list(tl xs) = sum_list xs - hd xs" for xs :: "nat list"
wenzelm@63466
  1163
    by (auto simp add: neq_Nil_conv)
lp15@59667
  1164
  have f: "bij_betw ?f ?A ?A'"
wenzelm@63466
  1165
    apply (rule bij_betw_byWitness[where f' = tl])
lp15@59667
  1166
    using assms
wenzelm@63466
  1167
    apply (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv)
wenzelm@63466
  1168
    done
nipkow@63882
  1169
  have 3: "xs \<noteq> [] \<Longrightarrow> hd xs + (sum_list xs - hd xs) = sum_list xs" for xs :: "nat list"
nipkow@63882
  1170
    by (metis 1 sum_list_simps(2) 2)
lp15@59667
  1171
  have g: "bij_betw ?g ?B ?B'"
wenzelm@63466
  1172
    apply (rule bij_betw_byWitness[where f' = "\<lambda>l. (hd l - 1) # tl l"])
lp15@59667
  1173
    using assms
lp15@59667
  1174
    by (auto simp: 2 length_0_conv[symmetric] intro!: 3
wenzelm@63466
  1175
        simp del: length_greater_0_conv length_0_conv)
wenzelm@63466
  1176
  have fin: "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}" for M N :: nat
wenzelm@63466
  1177
    using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto
lp15@59667
  1178
  have fin_A: "finite ?A" using fin[of _ "N+1"]
wenzelm@63466
  1179
    by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"])
nipkow@66311
  1180
      (auto simp: member_le_sum_list less_Suc_eq_le)
lp15@59667
  1181
  have fin_B: "finite ?B"
wenzelm@63466
  1182
    by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"])
nipkow@66311
  1183
      (auto simp: member_le_sum_list less_Suc_eq_le fin)
wenzelm@63466
  1184
  have uni: "?C = ?A' \<union> ?B'"
wenzelm@63466
  1185
    by auto
eberlm@65350
  1186
  have disj: "?A' \<inter> ?B' = {}" by blast
wenzelm@63466
  1187
  have "card ?C = card(?A' \<union> ?B')"
wenzelm@63466
  1188
    using uni by simp
lp15@59667
  1189
  also have "\<dots> = card ?A + card ?B"
lp15@59667
  1190
    using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g]
lp15@59667
  1191
      bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B
lp15@59667
  1192
    by presburger
lp15@59667
  1193
  finally show ?thesis .
lp15@59667
  1194
qed
lp15@59667
  1195
nipkow@63882
  1196
lemma card_length_sum_list: "card {l::nat list. size l = m \<and> sum_list l = N} = (N + m - 1) choose N"
wenzelm@63466
  1197
  \<comment> "by Holden Lee, tidied by Tobias Nipkow"
lp15@59667
  1198
proof (cases m)
wenzelm@63466
  1199
  case 0
wenzelm@63466
  1200
  then show ?thesis
wenzelm@63466
  1201
    by (cases N) (auto cong: conj_cong)
lp15@59667
  1202
next
lp15@59667
  1203
  case (Suc m')
wenzelm@63466
  1204
  have m: "m \<ge> 1"
wenzelm@63466
  1205
    by (simp add: Suc)
wenzelm@63466
  1206
  then show ?thesis
wenzelm@63466
  1207
  proof (induct "N + m - 1" arbitrary: N m)
wenzelm@63466
  1208
    case 0  \<comment> "In the base case, the only solution is [0]."
wenzelm@63466
  1209
    have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {[0]}"
wenzelm@63466
  1210
      by (auto simp: length_Suc_conv)
wenzelm@63466
  1211
    have "m = 1 \<and> N = 0"
wenzelm@63466
  1212
      using 0 by linarith
wenzelm@63466
  1213
    then show ?case
wenzelm@63466
  1214
      by simp
wenzelm@63466
  1215
  next
wenzelm@63466
  1216
    case (Suc k)
nipkow@63882
  1217
    have c1: "card {l::nat list. size l = (m - 1) \<and> sum_list l =  N} = (N + (m - 1) - 1) choose N"
wenzelm@63466
  1218
    proof (cases "m = 1")
wenzelm@63466
  1219
      case True
wenzelm@63466
  1220
      with Suc.hyps have "N \<ge> 1"
wenzelm@63466
  1221
        by auto
wenzelm@63466
  1222
      with True show ?thesis
wenzelm@63466
  1223
        by (simp add: binomial_eq_0)
lp15@59667
  1224
    next
wenzelm@63466
  1225
      case False
wenzelm@63466
  1226
      then show ?thesis
wenzelm@63466
  1227
        using Suc by fastforce
wenzelm@63466
  1228
    qed
nipkow@63882
  1229
    from Suc have c2: "card {l::nat list. size l = m \<and> sum_list l + 1 = N} =
wenzelm@63466
  1230
      (if N > 0 then ((N - 1) + m - 1) choose (N - 1) else 0)"
wenzelm@63466
  1231
    proof -
wenzelm@63466
  1232
      have *: "n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" for m n
wenzelm@63466
  1233
        by arith
wenzelm@63466
  1234
      from Suc have "N > 0 \<Longrightarrow>
nipkow@63882
  1235
        card {l::nat list. size l = m \<and> sum_list l + 1 = N} =
wenzelm@63466
  1236
          ((N - 1) + m - 1) choose (N - 1)"
wenzelm@63466
  1237
        by (simp add: *)
wenzelm@63466
  1238
      then show ?thesis
wenzelm@63466
  1239
        by auto
wenzelm@63466
  1240
    qed
nipkow@63882
  1241
    from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> sum_list l = N} +
nipkow@63882
  1242
          card {l::nat list. size l = m \<and> sum_list l + 1 = N}) = (N + m - 1) choose N"
wenzelm@63466
  1243
      by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def)
wenzelm@63466
  1244
    then show ?case
nipkow@63882
  1245
      using card_length_sum_list_rec[OF Suc.prems] by auto
wenzelm@63466
  1246
  qed
lp15@59667
  1247
qed
lp15@59667
  1248
wenzelm@65552
  1249
lemma card_disjoint_shuffle:
eberlm@65350
  1250
  assumes "set xs \<inter> set ys = {}"
eberlm@65350
  1251
  shows   "card (shuffle xs ys) = (length xs + length ys) choose length xs"
eberlm@65350
  1252
using assms
eberlm@65350
  1253
proof (induction xs ys rule: shuffle.induct)
eberlm@65350
  1254
  case (3 x xs y ys)
eberlm@65350
  1255
  have "shuffle (x # xs) (y # ys) = op # x ` shuffle xs (y # ys) \<union> op # y ` shuffle (x # xs) ys"
eberlm@65350
  1256
    by (rule shuffle.simps)
eberlm@65350
  1257
  also have "card \<dots> = card (op # x ` shuffle xs (y # ys)) + card (op # y ` shuffle (x # xs) ys)"
eberlm@65350
  1258
    by (rule card_Un_disjoint) (insert "3.prems", auto)
eberlm@65350
  1259
  also have "card (op # x ` shuffle xs (y # ys)) = card (shuffle xs (y # ys))"
eberlm@65350
  1260
    by (rule card_image) auto
eberlm@65350
  1261
  also have "\<dots> = (length xs + length (y # ys)) choose length xs"
eberlm@65350
  1262
    using "3.prems" by (intro "3.IH") auto
eberlm@65350
  1263
  also have "card (op # y ` shuffle (x # xs) ys) = card (shuffle (x # xs) ys)"
eberlm@65350
  1264
    by (rule card_image) auto
eberlm@65350
  1265
  also have "\<dots> = (length (x # xs) + length ys) choose length (x # xs)"
eberlm@65350
  1266
    using "3.prems" by (intro "3.IH") auto
wenzelm@65552
  1267
  also have "length xs + length (y # ys) choose length xs + \<dots> =
eberlm@65350
  1268
               (length (x # xs) + length (y # ys)) choose length (x # xs)" by simp
eberlm@65350
  1269
  finally show ?case .
eberlm@65350
  1270
qed auto
eberlm@65350
  1271
wenzelm@63466
  1272
lemma Suc_times_binomial_add: "Suc a * (Suc (a + b) choose Suc a) = Suc b * (Suc (a + b) choose a)"
wenzelm@63466
  1273
  \<comment> \<open>by Lukas Bulwahn\<close>
hoelzl@60604
  1274
proof -
hoelzl@60604
  1275
  have dvd: "Suc a * (fact a * fact b) dvd fact (Suc (a + b))" for a b
hoelzl@60604
  1276
    using fact_fact_dvd_fact[of "Suc a" "b", where 'a=nat]
hoelzl@60604
  1277
    by (simp only: fact_Suc add_Suc[symmetric] of_nat_id mult.assoc)
hoelzl@60604
  1278
  have "Suc a * (fact (Suc (a + b)) div (Suc a * fact a * fact b)) =
hoelzl@60604
  1279
      Suc a * fact (Suc (a + b)) div (Suc a * (fact a * fact b))"
hoelzl@60604
  1280
    by (subst div_mult_swap[symmetric]; simp only: mult.assoc dvd)
hoelzl@60604
  1281
  also have "\<dots> = Suc b * fact (Suc (a + b)) div (Suc b * (fact a * fact b))"
hoelzl@60604
  1282
    by (simp only: div_mult_mult1)
hoelzl@60604
  1283
  also have "\<dots> = Suc b * (fact (Suc (a + b)) div (Suc b * (fact a * fact b)))"
hoelzl@60604
  1284
    using dvd[of b a] by (subst div_mult_swap[symmetric]; simp only: ac_simps dvd)
hoelzl@60604
  1285
  finally show ?thesis
hoelzl@60604
  1286
    by (subst (1 2) binomial_altdef_nat)
wenzelm@63466
  1287
      (simp_all only: ac_simps diff_Suc_Suc Suc_diff_le diff_add_inverse fact_Suc of_nat_id)
hoelzl@60604
  1288
qed
hoelzl@60604
  1289
haftmann@63373
  1290
haftmann@63373
  1291
subsection \<open>Misc\<close>
haftmann@63373
  1292
eberlm@62128
  1293
lemma gbinomial_code [code]:
wenzelm@63466
  1294
  "a gchoose n =
wenzelm@63466
  1295
    (if n = 0 then 1
wenzelm@63466
  1296
     else fold_atLeastAtMost_nat (\<lambda>n acc. (a - of_nat n) * acc) 0 (n - 1) 1 / fact n)"
wenzelm@63466
  1297
  by (cases n)
nipkow@64272
  1298
    (simp_all add: gbinomial_prod_rev prod_atLeastAtMost_code [symmetric]
wenzelm@63466
  1299
      atLeastLessThanSuc_atLeastAtMost)
eberlm@62128
  1300
haftmann@65812
  1301
declare [[code drop: binomial]]
eberlm@65581
  1302
    
eberlm@62128
  1303
lemma binomial_code [code]:
eberlm@62128
  1304
  "(n choose k) =
eberlm@62128
  1305
      (if k > n then 0
eberlm@62128
  1306
       else if 2 * k > n then (n choose (n - k))
eberlm@62142
  1307
       else (fold_atLeastAtMost_nat (op * ) (n-k+1) n 1 div fact k))"
eberlm@62128
  1308
proof -
eberlm@62128
  1309
  {
eberlm@62128
  1310
    assume "k \<le> n"
wenzelm@63466
  1311
    then have "{1..n} = {1..n-k} \<union> {n-k+1..n}" by auto
wenzelm@63466
  1312
    then have "(fact n :: nat) = fact (n-k) * \<Prod>{n-k+1..n}"
eberlm@65581
  1313
      by (simp add: prod.union_disjoint fact_prod)
eberlm@62128
  1314
  }
nipkow@64272
  1315
  then show ?thesis by (auto simp: binomial_altdef_nat mult_ac prod_atLeastAtMost_code)
hoelzl@62378
  1316
qed
eberlm@62128
  1317
nipkow@15131
  1318
end