src/HOL/Binomial.thy
author wenzelm
Sun Sep 18 20:33:48 2016 +0200 (2016-09-18)
changeset 63915 bab633745c7f
parent 63882 018998c00003
child 63918 6bf55e6e0b75
permissions -rw-r--r--
tuned 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@63373
     9
section \<open>Combinatorial Functions: Factorial Function, Rising Factorials, Binomial Coefficients and Binomial Theorem\<close>
paulson@15094
    10
lp15@59669
    11
theory Binomial
wenzelm@63466
    12
  imports Main
nipkow@15131
    13
begin
paulson@15094
    14
wenzelm@60758
    15
subsection \<open>Factorial\<close>
lp15@59730
    16
haftmann@63417
    17
context semiring_char_0
haftmann@63417
    18
begin
haftmann@63417
    19
haftmann@63417
    20
definition fact :: "nat \<Rightarrow> 'a"
wenzelm@63466
    21
  where fact_setprod: "fact n = of_nat (\<Prod>{1..n})"
haftmann@63367
    22
wenzelm@63466
    23
lemma fact_setprod_Suc: "fact n = of_nat (setprod Suc {0..<n})"
wenzelm@63466
    24
  by (cases n)
wenzelm@63466
    25
    (simp_all add: fact_setprod setprod.atLeast_Suc_atMost_Suc_shift
wenzelm@63466
    26
      atLeastLessThanSuc_atLeastAtMost)
avigad@32036
    27
wenzelm@63466
    28
lemma fact_setprod_rev: "fact n = of_nat (\<Prod>i = 0..<n. n - i)"
haftmann@63417
    29
  using setprod.atLeast_atMost_rev [of "\<lambda>i. i" 1 n]
wenzelm@63466
    30
  by (cases n)
wenzelm@63466
    31
    (simp_all add: fact_setprod_Suc setprod.atLeast_Suc_atMost_Suc_shift
wenzelm@63466
    32
      atLeastLessThanSuc_atLeastAtMost)
haftmann@63367
    33
haftmann@63367
    34
lemma fact_0 [simp]: "fact 0 = 1"
haftmann@63417
    35
  by (simp add: fact_setprod)
lp15@59730
    36
lp15@59730
    37
lemma fact_1 [simp]: "fact 1 = 1"
haftmann@63417
    38
  by (simp add: fact_setprod)
lp15@59730
    39
haftmann@63417
    40
lemma fact_Suc_0 [simp]: "fact (Suc 0) = 1"
haftmann@63417
    41
  by (simp add: fact_setprod)
haftmann@63367
    42
haftmann@63367
    43
lemma fact_Suc [simp]: "fact (Suc n) = of_nat (Suc n) * fact n"
haftmann@63417
    44
  by (simp add: fact_setprod atLeastAtMostSuc_conv algebra_simps)
haftmann@63417
    45
wenzelm@63466
    46
lemma fact_2 [simp]: "fact 2 = 2"
haftmann@63417
    47
  by (simp add: numeral_2_eq_2)
haftmann@63417
    48
wenzelm@63466
    49
lemma fact_split: "k \<le> n \<Longrightarrow> fact n = of_nat (setprod Suc {n - k..<n}) * fact (n - k)"
wenzelm@63466
    50
  by (simp add: fact_setprod_Suc setprod.union_disjoint [symmetric]
wenzelm@63466
    51
    ivl_disj_un ac_simps of_nat_mult [symmetric])
haftmann@63417
    52
haftmann@63417
    53
end
avigad@32036
    54
wenzelm@63466
    55
lemma of_nat_fact [simp]: "of_nat (fact n) = fact n"
haftmann@63417
    56
  by (simp add: fact_setprod)
hoelzl@62378
    57
wenzelm@63466
    58
lemma of_int_fact [simp]: "of_int (fact n) = fact n"
haftmann@63417
    59
  by (simp only: fact_setprod of_int_of_nat_eq)
haftmann@62347
    60
lp15@59730
    61
lemma fact_reduce: "n > 0 \<Longrightarrow> fact n = of_nat n * fact (n - 1)"
lp15@59730
    62
  by (cases n) auto
avigad@32036
    63
lp15@59733
    64
lemma fact_nonzero [simp]: "fact n \<noteq> (0::'a::{semiring_char_0,semiring_no_zero_divisors})"
lp15@59730
    65
  apply (induct n)
lp15@59730
    66
  apply auto
wenzelm@63466
    67
  using of_nat_eq_0_iff
wenzelm@63466
    68
  apply fastforce
wenzelm@63466
    69
  done
lp15@59730
    70
lp15@59730
    71
lemma fact_mono_nat: "m \<le> n \<Longrightarrow> fact m \<le> (fact n :: nat)"
lp15@59730
    72
  by (induct n) (auto simp: le_Suc_eq)
avigad@32036
    73
wenzelm@63466
    74
lemma fact_in_Nats: "fact n \<in> \<nat>"
wenzelm@63466
    75
  by (induct n) auto
eberlm@61531
    76
wenzelm@63466
    77
lemma fact_in_Ints: "fact n \<in> \<int>"
wenzelm@63466
    78
  by (induct n) auto
eberlm@61531
    79
lp15@59730
    80
context
wenzelm@60241
    81
  assumes "SORT_CONSTRAINT('a::linordered_semidom)"
lp15@59667
    82
begin
hoelzl@62378
    83
wenzelm@63466
    84
lemma fact_mono: "m \<le> n \<Longrightarrow> fact m \<le> (fact n :: 'a)"
wenzelm@63466
    85
  by (metis of_nat_fact of_nat_le_iff fact_mono_nat)
hoelzl@62378
    86
wenzelm@63466
    87
lemma fact_ge_1 [simp]: "fact n \<ge> (1 :: 'a)"
wenzelm@63466
    88
  by (metis le0 fact_0 fact_mono)
hoelzl@62378
    89
wenzelm@63466
    90
lemma fact_gt_zero [simp]: "fact n > (0 :: 'a)"
wenzelm@63466
    91
  using fact_ge_1 less_le_trans zero_less_one by blast
hoelzl@62378
    92
wenzelm@63466
    93
lemma fact_ge_zero [simp]: "fact n \<ge> (0 :: 'a)"
wenzelm@63466
    94
  by (simp add: less_imp_le)
avigad@32036
    95
wenzelm@63466
    96
lemma fact_not_neg [simp]: "\<not> fact n < (0 :: 'a)"
wenzelm@63466
    97
  by (simp add: not_less_iff_gr_or_eq)
hoelzl@62378
    98
wenzelm@63466
    99
lemma fact_le_power: "fact n \<le> (of_nat (n^n) :: 'a)"
wenzelm@63466
   100
proof (induct n)
wenzelm@63466
   101
  case 0
wenzelm@63466
   102
  then show ?case by simp
wenzelm@63466
   103
next
wenzelm@63466
   104
  case (Suc n)
wenzelm@63466
   105
  then have *: "fact n \<le> (of_nat (Suc n ^ n) ::'a)"
wenzelm@63466
   106
    by (rule order_trans) (simp add: power_mono del: of_nat_power)
wenzelm@63466
   107
  have "fact (Suc n) = (of_nat (Suc n) * fact n ::'a)"
wenzelm@63466
   108
    by (simp add: algebra_simps)
wenzelm@63466
   109
  also have "\<dots> \<le> of_nat (Suc n) * of_nat (Suc n ^ n)"
wenzelm@63466
   110
    by (simp add: * ordered_comm_semiring_class.comm_mult_left_mono del: of_nat_power)
wenzelm@63466
   111
  also have "\<dots> \<le> of_nat (Suc n ^ Suc n)"
wenzelm@63466
   112
    by (metis of_nat_mult order_refl power_Suc)
wenzelm@63466
   113
  finally show ?case .
wenzelm@63466
   114
qed
hoelzl@62378
   115
avigad@32036
   116
end
avigad@32036
   117
wenzelm@63466
   118
text \<open>Note that @{term "fact 0 = fact 1"}\<close>
wenzelm@63466
   119
lemma fact_less_mono_nat: "0 < m \<Longrightarrow> m < n \<Longrightarrow> fact m < (fact n :: nat)"
lp15@59730
   120
  by (induct n) (auto simp: less_Suc_eq)
avigad@32036
   121
wenzelm@63466
   122
lemma fact_less_mono: "0 < m \<Longrightarrow> m < n \<Longrightarrow> fact m < (fact n :: 'a::linordered_semidom)"
lp15@59730
   123
  by (metis of_nat_fact of_nat_less_iff fact_less_mono_nat)
avigad@32036
   124
lp15@59730
   125
lemma fact_ge_Suc_0_nat [simp]: "fact n \<ge> Suc 0"
lp15@59730
   126
  by (metis One_nat_def fact_ge_1)
avigad@32036
   127
wenzelm@63466
   128
lemma dvd_fact: "1 \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> m dvd fact n"
lp15@59730
   129
  by (induct n) (auto simp: dvdI le_Suc_eq)
avigad@32036
   130
eberlm@62128
   131
lemma fact_ge_self: "fact n \<ge> n"
eberlm@62128
   132
  by (cases "n = 0") (simp_all add: dvd_imp_le dvd_fact)
eberlm@62128
   133
wenzelm@63466
   134
lemma fact_dvd: "n \<le> m \<Longrightarrow> fact n dvd (fact m :: 'a::{semiring_div,linordered_semidom})"
lp15@59730
   135
  by (induct m) (auto simp: le_Suc_eq)
avigad@32036
   136
wenzelm@63466
   137
lemma fact_mod: "m \<le> n \<Longrightarrow> fact n mod (fact m :: 'a::{semiring_div,linordered_semidom}) = 0"
lp15@59730
   138
  by (auto simp add: fact_dvd)
bulwahn@40033
   139
bulwahn@40033
   140
lemma fact_div_fact:
lp15@59730
   141
  assumes "m \<ge> n"
wenzelm@63466
   142
  shows "fact m div fact n = \<Prod>{n + 1..m}"
bulwahn@40033
   143
proof -
wenzelm@63466
   144
  obtain d where "d = m - n"
wenzelm@63466
   145
    by auto
wenzelm@63466
   146
  with assms have "m = n + d"
wenzelm@63466
   147
    by auto
bulwahn@40033
   148
  have "fact (n + d) div (fact n) = \<Prod>{n + 1..n + d}"
bulwahn@40033
   149
  proof (induct d)
bulwahn@40033
   150
    case 0
bulwahn@40033
   151
    show ?case by simp
bulwahn@40033
   152
  next
bulwahn@40033
   153
    case (Suc d')
bulwahn@40033
   154
    have "fact (n + Suc d') div fact n = Suc (n + d') * fact (n + d') div fact n"
bulwahn@40033
   155
      by simp
wenzelm@63466
   156
    also from Suc.hyps have "\<dots> = Suc (n + d') * \<Prod>{n + 1..n + d'}"
bulwahn@40033
   157
      unfolding div_mult1_eq[of _ "fact (n + d')"] by (simp add: fact_mod)
wenzelm@63466
   158
    also have "\<dots> = \<Prod>{n + 1..n + Suc d'}"
lp15@59730
   159
      by (simp add: atLeastAtMostSuc_conv)
bulwahn@40033
   160
    finally show ?case .
bulwahn@40033
   161
  qed
wenzelm@63466
   162
  with \<open>m = n + d\<close> show ?thesis by simp
bulwahn@40033
   163
qed
bulwahn@40033
   164
wenzelm@63466
   165
lemma fact_num_eq_if: "fact m = (if m = 0 then 1 else of_nat m * fact (m - 1))"
wenzelm@63466
   166
  by (cases m) auto
avigad@32036
   167
hoelzl@50240
   168
lemma fact_div_fact_le_pow:
wenzelm@63466
   169
  assumes "r \<le> n"
wenzelm@63466
   170
  shows "fact n div fact (n - r) \<le> n ^ r"
hoelzl@50240
   171
proof -
wenzelm@63466
   172
  have "r \<le> n \<Longrightarrow> \<Prod>{n - r..n} = (n - r) * \<Prod>{Suc (n - r)..n}" for r
haftmann@57418
   173
    by (subst setprod.insert[symmetric]) (auto simp: atLeastAtMost_insertL)
hoelzl@50240
   174
  with assms show ?thesis
hoelzl@50240
   175
    by (induct r rule: nat.induct) (auto simp add: fact_div_fact Suc_diff_Suc mult_le_mono)
hoelzl@50240
   176
qed
hoelzl@50240
   177
wenzelm@63466
   178
lemma fact_numeral: "fact (numeral k) = numeral k * fact (pred_numeral k)"
wenzelm@63466
   179
  \<comment> \<open>Evaluation for specific numerals\<close>
haftmann@63367
   180
  by (metis fact_Suc numeral_eq_Suc of_nat_numeral)
lp15@57113
   181
lp15@59658
   182
haftmann@63373
   183
subsection \<open>Binomial coefficients\<close>
lp15@59658
   184
wenzelm@63466
   185
text \<open>This development is based on the work of Andy Gordon and Florian Kammueller.\<close>
wenzelm@63466
   186
haftmann@63366
   187
text \<open>Combinatorial definition\<close>
haftmann@63366
   188
wenzelm@63466
   189
definition binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat"  (infixl "choose" 65)
wenzelm@63466
   190
  where "n choose k = card {K\<in>Pow {0..<n}. card K = k}"
lp15@59658
   191
haftmann@63366
   192
theorem n_subsets:
haftmann@63366
   193
  assumes "finite A"
haftmann@63366
   194
  shows "card {B. B \<subseteq> A \<and> card B = k} = card A choose k"
haftmann@63366
   195
proof -
haftmann@63417
   196
  from assms obtain f where bij: "bij_betw f {0..<card A} A"
haftmann@63417
   197
    by (blast dest: ex_bij_betw_nat_finite)
haftmann@63417
   198
  then have [simp]: "card (f ` C) = card C" if "C \<subseteq> {0..<card A}" for C
haftmann@63366
   199
    by (meson bij_betw_imp_inj_on bij_betw_subset card_image that)
haftmann@63417
   200
  from bij have "bij_betw (image f) (Pow {0..<card A}) (Pow A)"
haftmann@63366
   201
    by (rule bij_betw_Pow)
haftmann@63417
   202
  then have "inj_on (image f) (Pow {0..<card A})"
haftmann@63366
   203
    by (rule bij_betw_imp_inj_on)
haftmann@63417
   204
  moreover have "{K. K \<subseteq> {0..<card A} \<and> card K = k} \<subseteq> Pow {0..<card A}"
haftmann@63366
   205
    by auto
haftmann@63417
   206
  ultimately have "inj_on (image f) {K. K \<subseteq> {0..<card A} \<and> card K = k}"
haftmann@63366
   207
    by (rule inj_on_subset)
haftmann@63417
   208
  then have "card {K. K \<subseteq> {0..<card A} \<and> card K = k} =
wenzelm@63466
   209
      card (image f ` {K. K \<subseteq> {0..<card A} \<and> card K = k})" (is "_ = card ?C")
haftmann@63366
   210
    by (simp add: card_image)
haftmann@63417
   211
  also have "?C = {K. K \<subseteq> f ` {0..<card A} \<and> card K = k}"
haftmann@63366
   212
    by (auto elim!: subset_imageE)
haftmann@63417
   213
  also have "f ` {0..<card A} = A"
haftmann@63366
   214
    by (meson bij bij_betw_def)
haftmann@63366
   215
  finally show ?thesis
haftmann@63366
   216
    by (simp add: binomial_def)
haftmann@63366
   217
qed
wenzelm@63466
   218
haftmann@63366
   219
text \<open>Recursive characterization\<close>
lp15@59658
   220
wenzelm@63466
   221
lemma binomial_n_0 [simp, code]: "n choose 0 = 1"
haftmann@63366
   222
proof -
haftmann@63417
   223
  have "{K \<in> Pow {0..<n}. card K = 0} = {{}}"
haftmann@63417
   224
    by (auto dest: finite_subset)
haftmann@63366
   225
  then show ?thesis
haftmann@63366
   226
    by (simp add: binomial_def)
haftmann@63366
   227
qed
haftmann@63366
   228
wenzelm@63466
   229
lemma binomial_0_Suc [simp, code]: "0 choose Suc k = 0"
haftmann@63366
   230
  by (simp add: binomial_def)
lp15@59658
   231
wenzelm@63466
   232
lemma binomial_Suc_Suc [simp, code]: "Suc n choose Suc k = (n choose k) + (n choose Suc k)"
haftmann@63366
   233
proof -
haftmann@63417
   234
  let ?P = "\<lambda>n k. {K. K \<subseteq> {0..<n} \<and> card K = k}"
haftmann@63366
   235
  let ?Q = "?P (Suc n) (Suc k)"
haftmann@63366
   236
  have inj: "inj_on (insert n) (?P n k)"
wenzelm@63466
   237
    by rule (auto; metis atLeastLessThan_iff insert_iff less_irrefl subsetCE)
haftmann@63366
   238
  have disjoint: "insert n ` ?P n k \<inter> ?P n (Suc k) = {}"
haftmann@63366
   239
    by auto
haftmann@63366
   240
  have "?Q = {K\<in>?Q. n \<in> K} \<union> {K\<in>?Q. n \<notin> K}"
haftmann@63366
   241
    by auto
haftmann@63366
   242
  also have "{K\<in>?Q. n \<in> K} = insert n ` ?P n k" (is "?A = ?B")
haftmann@63366
   243
  proof (rule set_eqI)
haftmann@63366
   244
    fix K
haftmann@63417
   245
    have K_finite: "finite K" if "K \<subseteq> insert n {0..<n}"
haftmann@63366
   246
      using that by (rule finite_subset) simp_all
haftmann@63366
   247
    have Suc_card_K: "Suc (card K - Suc 0) = card K" if "n \<in> K"
haftmann@63366
   248
      and "finite K"
haftmann@63366
   249
    proof -
haftmann@63366
   250
      from \<open>n \<in> K\<close> obtain L where "K = insert n L" and "n \<notin> L"
haftmann@63366
   251
        by (blast elim: Set.set_insert)
haftmann@63366
   252
      with that show ?thesis by (simp add: card_insert)
haftmann@63366
   253
    qed
haftmann@63366
   254
    show "K \<in> ?A \<longleftrightarrow> K \<in> ?B"
haftmann@63366
   255
      by (subst in_image_insert_iff)
wenzelm@63466
   256
        (auto simp add: card_insert subset_eq_atLeast0_lessThan_finite
wenzelm@63466
   257
          Diff_subset_conv K_finite Suc_card_K)
wenzelm@63466
   258
  qed
haftmann@63366
   259
  also have "{K\<in>?Q. n \<notin> K} = ?P n (Suc k)"
haftmann@63417
   260
    by (auto simp add: atLeast0_lessThan_Suc)
haftmann@63366
   261
  finally show ?thesis using inj disjoint
haftmann@63366
   262
    by (simp add: binomial_def card_Un_disjoint card_image)
haftmann@63366
   263
qed
lp15@59658
   264
wenzelm@63466
   265
lemma binomial_eq_0: "n < k \<Longrightarrow> n choose k = 0"
haftmann@63417
   266
  by (auto simp add: binomial_def dest: subset_eq_atLeast0_lessThan_card)
haftmann@63366
   267
haftmann@63366
   268
lemma zero_less_binomial: "k \<le> n \<Longrightarrow> n choose k > 0"
haftmann@63366
   269
  by (induct n k rule: diff_induct) simp_all
lp15@59658
   270
wenzelm@63466
   271
lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> n < k"
haftmann@63366
   272
  by (metis binomial_eq_0 less_numeral_extra(3) not_less zero_less_binomial)
lp15@59658
   273
wenzelm@63466
   274
lemma zero_less_binomial_iff [simp]: "n choose k > 0 \<longleftrightarrow> k \<le> n"
haftmann@63366
   275
  by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)
lp15@59658
   276
lp15@59658
   277
lemma binomial_n_n [simp]: "n choose n = 1"
lp15@59658
   278
  by (induct n) (simp_all add: binomial_eq_0)
lp15@59658
   279
lp15@59658
   280
lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n"
lp15@59658
   281
  by (induct n) simp_all
lp15@59658
   282
lp15@59658
   283
lemma binomial_1 [simp]: "n choose Suc 0 = n"
lp15@59658
   284
  by (induct n) simp_all
lp15@59658
   285
haftmann@63366
   286
lemma choose_reduce_nat:
wenzelm@63466
   287
  "0 < n \<Longrightarrow> 0 < k \<Longrightarrow>
wenzelm@63466
   288
    n choose k = ((n - 1) choose (k - 1)) + ((n - 1) choose k)"
haftmann@63366
   289
  using binomial_Suc_Suc [of "n - 1" "k - 1"] by simp
lp15@59658
   290
wenzelm@63466
   291
lemma Suc_times_binomial_eq: "Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
haftmann@63366
   292
  apply (induct n arbitrary: k)
wenzelm@63466
   293
   apply simp
wenzelm@63466
   294
   apply arith
lp15@59658
   295
  apply (case_tac k)
lp15@59658
   296
   apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0)
lp15@59658
   297
  done
lp15@59658
   298
lp15@60141
   299
lemma binomial_le_pow2: "n choose k \<le> 2^n"
haftmann@63366
   300
  apply (induct n arbitrary: k)
wenzelm@63466
   301
   apply (case_tac k)
wenzelm@63466
   302
    apply simp_all
lp15@60141
   303
  apply (case_tac k)
wenzelm@63466
   304
   apply auto
haftmann@63366
   305
  apply (simp add: add_le_mono mult_2)
haftmann@63366
   306
  done
lp15@60141
   307
wenzelm@63466
   308
text \<open>The absorption property.\<close>
wenzelm@63466
   309
lemma Suc_times_binomial: "Suc k * (Suc n choose Suc k) = Suc n * (n choose k)"
lp15@59658
   310
  using Suc_times_binomial_eq by auto
lp15@59658
   311
wenzelm@63466
   312
text \<open>This is the well-known version of absorption, but it's harder to use
wenzelm@63466
   313
  because of the need to reason about division.\<close>
wenzelm@63466
   314
lemma binomial_Suc_Suc_eq_times: "(Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
lp15@59658
   315
  by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)
lp15@59658
   316
wenzelm@63466
   317
text \<open>Another version of absorption, with \<open>-1\<close> instead of \<open>Suc\<close>.\<close>
wenzelm@63466
   318
lemma times_binomial_minus1_eq: "0 < k \<Longrightarrow> k * (n choose k) = n * ((n - 1) choose (k - 1))"
lp15@59658
   319
  using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"]
nipkow@63648
   320
  by (auto split: nat_diff_split)
lp15@59658
   321
lp15@59658
   322
wenzelm@60758
   323
subsection \<open>The binomial theorem (courtesy of Tobias Nipkow):\<close>
lp15@59658
   324
wenzelm@63466
   325
text \<open>Avigad's version, generalized to any commutative ring\<close>
wenzelm@63466
   326
theorem binomial_ring: "(a + b :: 'a::{comm_ring_1,power})^n =
wenzelm@63466
   327
  (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))"
lp15@59658
   328
proof (induct n)
wenzelm@63466
   329
  case 0
wenzelm@63466
   330
  then show ?case by simp
lp15@59658
   331
next
lp15@59658
   332
  case (Suc n)
wenzelm@63466
   333
  have decomp: "{0..n+1} = {0} \<union> {n + 1} \<union> {1..n}"
lp15@59658
   334
    by auto
wenzelm@63466
   335
  have decomp2: "{0..n} = {0} \<union> {1..n}"
lp15@59658
   336
    by auto
wenzelm@63466
   337
  have "(a + b)^(n+1) = (a + b) * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n - k))"
lp15@59658
   338
    using Suc.hyps by simp
wenzelm@63466
   339
  also have "\<dots> = a * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +
wenzelm@63466
   340
      b * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
lp15@59658
   341
    by (rule distrib_right)
lp15@59658
   342
  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +
wenzelm@63466
   343
      (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n - k + 1))"
lp15@59658
   344
    by (auto simp add: setsum_right_distrib ac_simps)
wenzelm@63466
   345
  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n + 1 - k)) +
wenzelm@63466
   346
      (\<Sum>k=1..n+1. of_nat (n choose (k - 1)) * a^k * b^(n + 1 - k))"
wenzelm@63466
   347
    by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le field_simps del: setsum_cl_ivl_Suc)
wenzelm@63466
   348
  also have "\<dots> = a^(n + 1) + b^(n + 1) +
wenzelm@63466
   349
      (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n + 1 - k)) +
wenzelm@63466
   350
      (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n + 1 - k))"
lp15@59658
   351
    by (simp add: decomp2)
wenzelm@63466
   352
  also have "\<dots> = a^(n + 1) + b^(n + 1) +
wenzelm@63466
   353
      (\<Sum>k=1..n. of_nat (n + 1 choose k) * a^k * b^(n + 1 - k))"
lp15@59658
   354
    by (auto simp add: field_simps setsum.distrib [symmetric] choose_reduce_nat)
wenzelm@63466
   355
  also have "\<dots> = (\<Sum>k=0..n+1. of_nat (n + 1 choose k) * a^k * b^(n + 1 - k))"
lp15@59658
   356
    using decomp by (simp add: field_simps)
wenzelm@63466
   357
  finally show ?case
wenzelm@63466
   358
    by simp
lp15@59658
   359
qed
lp15@59658
   360
wenzelm@63466
   361
text \<open>Original version for the naturals.\<close>
wenzelm@63466
   362
corollary binomial: "(a + b :: nat)^n = (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n - k))"
wenzelm@63466
   363
  using binomial_ring [of "int a" "int b" n]
lp15@59658
   364
  by (simp only: of_nat_add [symmetric] of_nat_mult [symmetric] of_nat_power [symmetric]
wenzelm@63466
   365
      of_nat_setsum [symmetric] of_nat_eq_iff of_nat_id)
lp15@59658
   366
lp15@59658
   367
lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
lp15@59658
   368
proof (induct n arbitrary: k rule: nat_less_induct)
wenzelm@63466
   369
  fix n k
wenzelm@63466
   370
  assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) = fact m"
wenzelm@63466
   371
  assume kn: "k \<le> n"
lp15@59658
   372
  let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"
wenzelm@63466
   373
  consider "n = 0 \<or> k = 0 \<or> n = k" | m h where "n = Suc m" "k = Suc h" "h < m"
wenzelm@63466
   374
    using kn by atomize_elim presburger
wenzelm@63466
   375
  then show "fact k * fact (n - k) * (n choose k) = fact n"
wenzelm@63466
   376
  proof cases
wenzelm@63466
   377
    case 1
wenzelm@63466
   378
    with kn show ?thesis by auto
wenzelm@63466
   379
  next
wenzelm@63466
   380
    case 2
wenzelm@63466
   381
    note n = \<open>n = Suc m\<close>
wenzelm@63466
   382
    note k = \<open>k = Suc h\<close>
wenzelm@63466
   383
    note hm = \<open>h < m\<close>
wenzelm@63466
   384
    have mn: "m < n"
wenzelm@63466
   385
      using n by arith
wenzelm@63466
   386
    have hm': "h \<le> m"
wenzelm@63466
   387
      using hm by arith
wenzelm@63466
   388
    have km: "k \<le> m"
wenzelm@63466
   389
      using hm k n kn by arith
wenzelm@63466
   390
    have "m - h = Suc (m - Suc h)"
wenzelm@63466
   391
      using  k km hm by arith
wenzelm@63466
   392
    with km k have "fact (m - h) = (m - h) * fact (m - k)"
lp15@59658
   393
      by simp
wenzelm@63466
   394
    with n k have "fact k * fact (n - k) * (n choose k) =
lp15@59667
   395
        k * (fact h * fact (m - h) * (m choose h)) +
lp15@59658
   396
        (m - h) * (fact k * fact (m - k) * (m choose k))"
lp15@59658
   397
      by (simp add: field_simps)
lp15@59658
   398
    also have "\<dots> = (k + (m - h)) * fact m"
lp15@59658
   399
      using H[rule_format, OF mn hm'] H[rule_format, OF mn km]
lp15@59658
   400
      by (simp add: field_simps)
wenzelm@63466
   401
    finally show ?thesis
wenzelm@63466
   402
      using k n km by simp
wenzelm@63466
   403
  qed
lp15@59658
   404
qed
lp15@59658
   405
haftmann@63372
   406
lemma binomial_fact':
haftmann@63372
   407
  assumes "k \<le> n"
haftmann@63372
   408
  shows "n choose k = fact n div (fact k * fact (n - k))"
haftmann@63372
   409
  using binomial_fact_lemma [OF assms]
haftmann@63372
   410
  by (metis fact_nonzero mult_eq_0_iff nonzero_mult_divide_cancel_left)
haftmann@63372
   411
lp15@59658
   412
lemma binomial_fact:
lp15@59658
   413
  assumes kn: "k \<le> n"
wenzelm@63466
   414
  shows "(of_nat (n choose k) :: 'a::field_char_0) = fact n / (fact k * fact (n - k))"
lp15@59658
   415
  using binomial_fact_lemma[OF kn]
lp15@59730
   416
  apply (simp add: field_simps)
wenzelm@63466
   417
  apply (metis mult.commute of_nat_fact of_nat_mult)
wenzelm@63466
   418
  done
lp15@59658
   419
haftmann@63417
   420
lemma fact_binomial:
haftmann@63417
   421
  assumes "k \<le> n"
haftmann@63417
   422
  shows "fact k * of_nat (n choose k) = (fact n / fact (n - k) :: 'a::field_char_0)"
haftmann@63417
   423
  unfolding binomial_fact [OF assms] by (simp add: field_simps)
haftmann@63417
   424
wenzelm@63466
   425
lemma choose_two: "n choose 2 = n * (n - 1) div 2"
haftmann@63417
   426
proof (cases "n \<ge> 2")
haftmann@63417
   427
  case False
haftmann@63417
   428
  then have "n = 0 \<or> n = 1"
haftmann@63417
   429
    by auto
haftmann@63417
   430
  then show ?thesis by auto
haftmann@63417
   431
next
haftmann@63417
   432
  case True
haftmann@63417
   433
  define m where "m = n - 2"
haftmann@63417
   434
  with True have "n = m + 2"
haftmann@63417
   435
    by simp
haftmann@63417
   436
  then have "fact n = n * (n - 1) * fact (n - 2)"
haftmann@63417
   437
    by (simp add: fact_setprod_Suc atLeast0_lessThan_Suc algebra_simps)
haftmann@63417
   438
  with True show ?thesis
haftmann@63417
   439
    by (simp add: binomial_fact')
haftmann@63417
   440
qed
haftmann@63417
   441
lp15@59667
   442
lemma choose_row_sum: "(\<Sum>k=0..n. n choose k) = 2^n"
wenzelm@63466
   443
  using binomial [of 1 "1" n] by (simp add: numeral_2_eq_2)
lp15@59667
   444
lp15@59667
   445
lemma sum_choose_lower: "(\<Sum>k=0..n. (r+k) choose k) = Suc (r+n) choose n"
lp15@59667
   446
  by (induct n) auto
lp15@59667
   447
lp15@59667
   448
lemma sum_choose_upper: "(\<Sum>k=0..n. k choose m) = Suc n choose Suc m"
lp15@59667
   449
  by (induct n) auto
lp15@59667
   450
hoelzl@62378
   451
lemma choose_alternating_sum:
wenzelm@63466
   452
  "n > 0 \<Longrightarrow> (\<Sum>i\<le>n. (-1)^i * of_nat (n choose i)) = (0 :: 'a::comm_ring_1)"
wenzelm@63466
   453
  using binomial_ring[of "-1 :: 'a" 1 n]
wenzelm@63466
   454
  by (simp add: atLeast0AtMost mult_of_nat_commute zero_power)
eberlm@61531
   455
eberlm@61531
   456
lemma choose_even_sum:
eberlm@61531
   457
  assumes "n > 0"
wenzelm@63466
   458
  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
   459
proof -
eberlm@61531
   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)"
eberlm@61531
   461
    using choose_row_sum[of n]
haftmann@63366
   462
    by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_setsum[symmetric])
eberlm@61531
   463
  also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) + (-1) ^ i * of_nat (n choose i))"
eberlm@61531
   464
    by (simp add: setsum.distrib)
hoelzl@62378
   465
  also have "\<dots> = 2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0)"
eberlm@61531
   466
    by (subst setsum_right_distrib, intro setsum.cong) simp_all
eberlm@61531
   467
  finally show ?thesis ..
eberlm@61531
   468
qed
eberlm@61531
   469
eberlm@61531
   470
lemma choose_odd_sum:
eberlm@61531
   471
  assumes "n > 0"
wenzelm@63466
   472
  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
   473
proof -
eberlm@61531
   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)"
eberlm@61531
   475
    using choose_row_sum[of n]
haftmann@63366
   476
    by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_setsum[symmetric])
eberlm@61531
   477
  also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) - (-1) ^ i * of_nat (n choose i))"
eberlm@61531
   478
    by (simp add: setsum_subtractf)
hoelzl@62378
   479
  also have "\<dots> = 2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0)"
eberlm@61531
   480
    by (subst setsum_right_distrib, intro setsum.cong) simp_all
eberlm@61531
   481
  finally show ?thesis ..
eberlm@61531
   482
qed
eberlm@61531
   483
eberlm@61531
   484
lemma choose_row_sum': "(\<Sum>k\<le>n. (n choose k)) = 2 ^ n"
eberlm@61531
   485
  using choose_row_sum[of n] by (simp add: atLeast0AtMost)
eberlm@61531
   486
wenzelm@60758
   487
text\<open>NW diagonal sum property\<close>
lp15@59667
   488
lemma sum_choose_diagonal:
wenzelm@63466
   489
  assumes "m \<le> n"
wenzelm@63466
   490
  shows "(\<Sum>k=0..m. (n - k) choose (m - k)) = Suc n choose m"
lp15@59667
   491
proof -
wenzelm@63466
   492
  have "(\<Sum>k=0..m. (n-k) choose (m - k)) = (\<Sum>k=0..m. (n - m + k) choose k)"
haftmann@63417
   493
    using setsum.atLeast_atMost_rev [of "\<lambda>k. (n - k) choose (m - k)" 0 m] assms
haftmann@63417
   494
      by simp
wenzelm@63466
   495
  also have "\<dots> = Suc (n - m + m) choose m"
lp15@59667
   496
    by (rule sum_choose_lower)
wenzelm@63466
   497
  also have "\<dots> = Suc n choose m"
wenzelm@63466
   498
    using assms by simp
lp15@59667
   499
  finally show ?thesis .
lp15@59667
   500
qed
lp15@59667
   501
haftmann@63373
   502
wenzelm@63466
   503
subsection \<open>Pochhammer's symbol: generalized rising factorial\<close>
lp15@59667
   504
wenzelm@63680
   505
text \<open>See \<^url>\<open>http://en.wikipedia.org/wiki/Pochhammer_symbol\<close>.\<close>
lp15@59667
   506
haftmann@63417
   507
context comm_semiring_1
haftmann@63417
   508
begin
haftmann@63417
   509
haftmann@63417
   510
definition pochhammer :: "'a \<Rightarrow> nat \<Rightarrow> 'a"
wenzelm@63466
   511
  where pochhammer_setprod: "pochhammer a n = setprod (\<lambda>i. a + of_nat i) {0..<n}"
haftmann@63417
   512
wenzelm@63466
   513
lemma pochhammer_setprod_rev: "pochhammer a n = setprod (\<lambda>i. a + of_nat (n - i)) {1..n}"
haftmann@63417
   514
  using setprod.atLeast_lessThan_rev_at_least_Suc_atMost [of "\<lambda>i. a + of_nat i" 0 n]
haftmann@63417
   515
  by (simp add: pochhammer_setprod)
lp15@59667
   516
wenzelm@63466
   517
lemma pochhammer_Suc_setprod: "pochhammer a (Suc n) = setprod (\<lambda>i. a + of_nat i) {0..n}"
haftmann@63417
   518
  by (simp add: pochhammer_setprod atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   519
wenzelm@63466
   520
lemma pochhammer_Suc_setprod_rev: "pochhammer a (Suc n) = setprod (\<lambda>i. a + of_nat (n - i)) {0..n}"
haftmann@63417
   521
  by (simp add: pochhammer_setprod_rev setprod.atLeast_Suc_atMost_Suc_shift)
haftmann@63417
   522
lp15@59667
   523
lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"
haftmann@63417
   524
  by (simp add: pochhammer_setprod)
wenzelm@63466
   525
lp15@59667
   526
lemma pochhammer_1 [simp]: "pochhammer a 1 = a"
haftmann@63417
   527
  by (simp add: pochhammer_setprod lessThan_Suc)
wenzelm@63466
   528
lp15@59667
   529
lemma pochhammer_Suc0 [simp]: "pochhammer a (Suc 0) = a"
haftmann@63417
   530
  by (simp add: pochhammer_setprod lessThan_Suc)
wenzelm@63466
   531
haftmann@63367
   532
lemma pochhammer_Suc: "pochhammer a (Suc n) = pochhammer a n * (a + of_nat n)"
haftmann@63417
   533
  by (simp add: pochhammer_setprod atLeast0_lessThan_Suc ac_simps)
wenzelm@63466
   534
haftmann@63417
   535
end
haftmann@63417
   536
eberlm@63725
   537
lemma pochhammer_nonneg: 
eberlm@63725
   538
  fixes x :: "'a :: linordered_semidom"
eberlm@63725
   539
  shows "x > 0 \<Longrightarrow> pochhammer x n \<ge> 0"
eberlm@63725
   540
  by (induction n) (auto simp: pochhammer_Suc intro!: mult_nonneg_nonneg add_nonneg_nonneg)
eberlm@63725
   541
eberlm@63725
   542
lemma pochhammer_pos: 
eberlm@63725
   543
  fixes x :: "'a :: linordered_semidom"
eberlm@63725
   544
  shows "x > 0 \<Longrightarrow> pochhammer x n > 0"
eberlm@63725
   545
  by (induction n) (auto simp: pochhammer_Suc intro!: mult_pos_pos add_pos_nonneg)
eberlm@63725
   546
eberlm@61531
   547
lemma pochhammer_of_nat: "pochhammer (of_nat x) n = of_nat (pochhammer x n)"
haftmann@63417
   548
  by (simp add: pochhammer_setprod)
eberlm@61531
   549
eberlm@61531
   550
lemma pochhammer_of_int: "pochhammer (of_int x) n = of_int (pochhammer x n)"
haftmann@63417
   551
  by (simp add: pochhammer_setprod)
lp15@59667
   552
lp15@59667
   553
lemma pochhammer_rec: "pochhammer a (Suc n) = a * pochhammer (a + 1) n"
haftmann@63417
   554
  by (simp add: pochhammer_setprod setprod.atLeast0_lessThan_Suc_shift ac_simps)
lp15@59667
   555
eberlm@61531
   556
lemma pochhammer_rec': "pochhammer z (Suc n) = (z + of_nat n) * pochhammer z n"
haftmann@63417
   557
  by (simp add: pochhammer_setprod setprod.atLeast0_lessThan_Suc ac_simps)
eberlm@61531
   558
lp15@59730
   559
lemma pochhammer_fact: "fact n = pochhammer 1 n"
haftmann@63417
   560
  by (simp add: pochhammer_setprod fact_setprod_Suc)
lp15@59667
   561
wenzelm@63466
   562
lemma pochhammer_of_nat_eq_0_lemma: "k > n \<Longrightarrow> pochhammer (- (of_nat n :: 'a:: idom)) k = 0"
wenzelm@63466
   563
  by (auto simp add: pochhammer_setprod)
lp15@59667
   564
lp15@59667
   565
lemma pochhammer_of_nat_eq_0_lemma':
lp15@59667
   566
  assumes kn: "k \<le> n"
wenzelm@63466
   567
  shows "pochhammer (- (of_nat n :: 'a::{idom,ring_char_0})) k \<noteq> 0"
lp15@59667
   568
proof (cases k)
lp15@59667
   569
  case 0
lp15@59667
   570
  then show ?thesis by simp
lp15@59667
   571
next
lp15@59667
   572
  case (Suc h)
lp15@59667
   573
  then show ?thesis
lp15@59667
   574
    apply (simp add: pochhammer_Suc_setprod)
wenzelm@63466
   575
    using Suc kn
wenzelm@63466
   576
    apply (auto simp add: algebra_simps)
lp15@59667
   577
    done
lp15@59667
   578
qed
lp15@59667
   579
lp15@59667
   580
lemma pochhammer_of_nat_eq_0_iff:
wenzelm@63466
   581
  "pochhammer (- (of_nat n :: 'a::{idom,ring_char_0})) k = 0 \<longleftrightarrow> k > n"
lp15@59667
   582
  (is "?l = ?r")
lp15@59667
   583
  using pochhammer_of_nat_eq_0_lemma[of n k, where ?'a='a]
lp15@59667
   584
    pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]
lp15@59667
   585
  by (auto simp add: not_le[symmetric])
lp15@59667
   586
lp15@59667
   587
lemma pochhammer_eq_0_iff: "pochhammer a n = (0::'a::field_char_0) \<longleftrightarrow> (\<exists>k < n. a = - of_nat k)"
haftmann@63417
   588
  by (auto simp add: pochhammer_setprod eq_neg_iff_add_eq_0)
lp15@59667
   589
lp15@59667
   590
lemma pochhammer_eq_0_mono:
lp15@59667
   591
  "pochhammer a n = (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"
lp15@59667
   592
  unfolding pochhammer_eq_0_iff by auto
lp15@59667
   593
lp15@59667
   594
lemma pochhammer_neq_0_mono:
lp15@59667
   595
  "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"
lp15@59667
   596
  unfolding pochhammer_eq_0_iff by auto
lp15@59667
   597
lp15@59667
   598
lemma pochhammer_minus:
haftmann@63417
   599
  "pochhammer (- b) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (b - of_nat k + 1) k"
lp15@59667
   600
proof (cases k)
lp15@59667
   601
  case 0
lp15@59667
   602
  then show ?thesis by simp
lp15@59667
   603
next
lp15@59667
   604
  case (Suc h)
haftmann@63417
   605
  have eq: "((- 1) ^ Suc h :: 'a) = (\<Prod>i = 0..h. - 1)"
haftmann@63417
   606
    using setprod_constant [where A="{0.. h}" and y="- 1 :: 'a"]
lp15@59667
   607
    by auto
haftmann@63417
   608
  with Suc show ?thesis
haftmann@63417
   609
    using pochhammer_Suc_setprod_rev [of "b - of_nat k + 1"]
haftmann@63417
   610
    by (auto simp add: pochhammer_Suc_setprod setprod.distrib [symmetric] eq of_nat_diff)
lp15@59667
   611
qed
lp15@59667
   612
lp15@59667
   613
lemma pochhammer_minus':
wenzelm@63466
   614
  "pochhammer (b - of_nat k + 1) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (- b) k"
wenzelm@63466
   615
  apply (simp only: pochhammer_minus [where b = b])
wenzelm@63466
   616
  apply (simp only: mult.assoc [symmetric])
wenzelm@63466
   617
  apply (simp only: power_add [symmetric])
wenzelm@63466
   618
  apply simp
wenzelm@63466
   619
  done
lp15@59667
   620
lp15@59667
   621
lemma pochhammer_same: "pochhammer (- of_nat n) n =
wenzelm@63466
   622
    ((- 1) ^ n :: 'a::{semiring_char_0,comm_ring_1,semiring_no_zero_divisors}) * fact n"
lp15@59862
   623
  unfolding pochhammer_minus
lp15@59667
   624
  by (simp add: of_nat_diff pochhammer_fact)
lp15@59667
   625
wenzelm@63466
   626
lemma pochhammer_product': "pochhammer z (n + m) = pochhammer z n * pochhammer (z + of_nat n) m"
wenzelm@63466
   627
proof (induct n arbitrary: z)
wenzelm@63466
   628
  case 0
wenzelm@63466
   629
  then show ?case by simp
wenzelm@63466
   630
next
eberlm@61531
   631
  case (Suc n z)
hoelzl@62378
   632
  have "pochhammer z (Suc n) * pochhammer (z + of_nat (Suc n)) m =
wenzelm@63466
   633
      z * (pochhammer (z + 1) n * pochhammer (z + 1 + of_nat n) m)"
eberlm@61531
   634
    by (simp add: pochhammer_rec ac_simps)
eberlm@61531
   635
  also note Suc[symmetric]
eberlm@61531
   636
  also have "z * pochhammer (z + 1) (n + m) = pochhammer z (Suc (n + m))"
eberlm@61531
   637
    by (subst pochhammer_rec) simp
wenzelm@63466
   638
  finally show ?case
wenzelm@63466
   639
    by simp
wenzelm@63466
   640
qed
eberlm@61531
   641
hoelzl@62378
   642
lemma pochhammer_product:
eberlm@61531
   643
  "m \<le> n \<Longrightarrow> pochhammer z n = pochhammer z m * pochhammer (z + of_nat m) (n - m)"
eberlm@61531
   644
  using pochhammer_product'[of z m "n - m"] by simp
eberlm@61531
   645
eberlm@61552
   646
lemma pochhammer_times_pochhammer_half:
wenzelm@63466
   647
  fixes z :: "'a::field_char_0"
haftmann@63417
   648
  shows "pochhammer z (Suc n) * pochhammer (z + 1/2) (Suc n) = (\<Prod>k=0..2*n+1. z + of_nat k / 2)"
wenzelm@63466
   649
proof (induct n)
wenzelm@63466
   650
  case 0
wenzelm@63466
   651
  then show ?case
wenzelm@63466
   652
    by (simp add: atLeast0_atMost_Suc)
wenzelm@63466
   653
next
eberlm@61552
   654
  case (Suc n)
wenzelm@63040
   655
  define n' where "n' = Suc n"
eberlm@61552
   656
  have "pochhammer z (Suc n') * pochhammer (z + 1 / 2) (Suc n') =
wenzelm@63466
   657
      (pochhammer z n' * pochhammer (z + 1 / 2) n') * ((z + of_nat n') * (z + 1/2 + of_nat n'))"
wenzelm@63466
   658
    (is "_ = _ * ?A")
wenzelm@63466
   659
    by (simp_all add: pochhammer_rec' mult_ac)
eberlm@61552
   660
  also have "?A = (z + of_nat (Suc (2 * n + 1)) / 2) * (z + of_nat (Suc (Suc (2 * n + 1))) / 2)"
wenzelm@63466
   661
    (is "_ = ?B")
wenzelm@63466
   662
    by (simp add: field_simps n'_def)
eberlm@61552
   663
  also note Suc[folded n'_def]
wenzelm@63466
   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)"
haftmann@63417
   665
    by (simp add: atLeast0_atMost_Suc)
wenzelm@63466
   666
  finally show ?case
wenzelm@63466
   667
    by (simp add: n'_def)
wenzelm@63466
   668
qed
eberlm@61552
   669
eberlm@61552
   670
lemma pochhammer_double:
wenzelm@63466
   671
  fixes z :: "'a::field_char_0"
eberlm@61552
   672
  shows "pochhammer (2 * z) (2 * n) = of_nat (2^(2*n)) * pochhammer z n * pochhammer (z+1/2) n"
wenzelm@63466
   673
proof (induct n)
wenzelm@63466
   674
  case 0
wenzelm@63466
   675
  then show ?case by simp
wenzelm@63466
   676
next
eberlm@61552
   677
  case (Suc n)
hoelzl@62378
   678
  have "pochhammer (2 * z) (2 * (Suc n)) = pochhammer (2 * z) (2 * n) *
wenzelm@63466
   679
      (2 * (z + of_nat n)) * (2 * (z + of_nat n) + 1)"
haftmann@63366
   680
    by (simp add: pochhammer_rec' ac_simps)
eberlm@61552
   681
  also note Suc
eberlm@61552
   682
  also have "of_nat (2 ^ (2 * n)) * pochhammer z n * pochhammer (z + 1/2) n *
wenzelm@63466
   683
        (2 * (z + of_nat n)) * (2 * (z + of_nat n) + 1) =
wenzelm@63466
   684
      of_nat (2 ^ (2 * (Suc n))) * pochhammer z (Suc n) * pochhammer (z + 1/2) (Suc n)"
haftmann@63366
   685
    by (simp add: field_simps pochhammer_rec')
eberlm@61552
   686
  finally show ?case .
wenzelm@63466
   687
qed
eberlm@61552
   688
eberlm@63317
   689
lemma fact_double:
wenzelm@63466
   690
  "fact (2 * n) = (2 ^ (2 * n) * pochhammer (1 / 2) n * fact n :: 'a::field_char_0)"
eberlm@63317
   691
  using pochhammer_double[of "1/2::'a" n] by (simp add: pochhammer_fact)
eberlm@63317
   692
wenzelm@63466
   693
lemma pochhammer_absorb_comp: "(r - of_nat k) * pochhammer (- r) k = r * pochhammer (-r + 1) k"
eberlm@61531
   694
  (is "?lhs = ?rhs")
wenzelm@63466
   695
  for r :: "'a::comm_ring_1"
eberlm@61531
   696
proof -
wenzelm@63466
   697
  have "?lhs = - pochhammer (- r) (Suc k)"
wenzelm@63466
   698
    by (subst pochhammer_rec') (simp add: algebra_simps)
wenzelm@63466
   699
  also have "\<dots> = ?rhs"
wenzelm@63466
   700
    by (subst pochhammer_rec) simp
eberlm@61531
   701
  finally show ?thesis .
eberlm@61531
   702
qed
eberlm@61531
   703
lp15@59667
   704
haftmann@63372
   705
subsection \<open>Generalized binomial coefficients\<close>
lp15@59667
   706
wenzelm@63466
   707
definition gbinomial :: "'a::{semidom_divide,semiring_char_0} \<Rightarrow> nat \<Rightarrow> 'a"  (infixl "gchoose" 65)
wenzelm@63466
   708
  where gbinomial_setprod_rev: "a gchoose n = setprod (\<lambda>i. a - of_nat i) {0..<n} div fact n"
haftmann@63417
   709
haftmann@63417
   710
lemma gbinomial_0 [simp]:
haftmann@63417
   711
  "a gchoose 0 = 1"
haftmann@63417
   712
  "0 gchoose (Suc n) = 0"
haftmann@63417
   713
  by (simp_all add: gbinomial_setprod_rev setprod.atLeast0_lessThan_Suc_shift)
haftmann@63367
   714
wenzelm@63466
   715
lemma gbinomial_Suc: "a gchoose (Suc k) = setprod (\<lambda>i. a - of_nat i) {0..k} div fact (Suc k)"
haftmann@63417
   716
  by (simp add: gbinomial_setprod_rev atLeastLessThanSuc_atLeastAtMost)
lp15@59667
   717
wenzelm@63466
   718
lemma gbinomial_mult_fact: "fact n * (a gchoose n) = (\<Prod>i = 0..<n. a - of_nat i)"
wenzelm@63466
   719
  for a :: "'a::field_char_0"
haftmann@63417
   720
  by (simp_all add: gbinomial_setprod_rev field_simps)
haftmann@63417
   721
wenzelm@63466
   722
lemma gbinomial_mult_fact': "(a gchoose n) * fact n = (\<Prod>i = 0..<n. a - of_nat i)"
wenzelm@63466
   723
  for a :: "'a::field_char_0"
haftmann@63417
   724
  using gbinomial_mult_fact [of n a] by (simp add: ac_simps)
lp15@59667
   725
wenzelm@63466
   726
lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / fact n"
wenzelm@63466
   727
  for a :: "'a::field_char_0"
haftmann@63417
   728
  by (cases n)
wenzelm@63466
   729
    (simp_all add: pochhammer_minus,
wenzelm@63466
   730
     simp_all add: gbinomial_setprod_rev pochhammer_setprod_rev
wenzelm@63466
   731
       power_mult_distrib [symmetric] atLeastLessThanSuc_atLeastAtMost
wenzelm@63466
   732
       setprod.atLeast_Suc_atMost_Suc_shift of_nat_diff)
lp15@59667
   733
wenzelm@63466
   734
lemma gbinomial_pochhammer': "s gchoose n = pochhammer (s - of_nat n + 1) n / fact n"
wenzelm@63466
   735
  for s :: "'a::field_char_0"
eberlm@61552
   736
proof -
eberlm@61552
   737
  have "s gchoose n = ((-1)^n * (-1)^n) * pochhammer (s - of_nat n + 1) n / fact n"
eberlm@61552
   738
    by (simp add: gbinomial_pochhammer pochhammer_minus mult_ac)
wenzelm@63466
   739
  also have "(-1 :: 'a)^n * (-1)^n = 1"
wenzelm@63466
   740
    by (subst power_add [symmetric]) simp
wenzelm@63466
   741
  finally show ?thesis
wenzelm@63466
   742
    by simp
eberlm@61552
   743
qed
eberlm@61552
   744
wenzelm@63466
   745
lemma gbinomial_binomial: "n gchoose k = n choose k"
haftmann@63372
   746
proof (cases "k \<le> n")
haftmann@63372
   747
  case False
wenzelm@63466
   748
  then have "n < k"
wenzelm@63466
   749
    by (simp add: not_le)
haftmann@63417
   750
  then have "0 \<in> (op - n) ` {0..<k}"
haftmann@63372
   751
    by auto
haftmann@63417
   752
  then have "setprod (op - n) {0..<k} = 0"
haftmann@63372
   753
    by (auto intro: setprod_zero)
haftmann@63372
   754
  with \<open>n < k\<close> show ?thesis
haftmann@63417
   755
    by (simp add: binomial_eq_0 gbinomial_setprod_rev setprod_zero)
haftmann@63372
   756
next
haftmann@63372
   757
  case True
haftmann@63417
   758
  then have "inj_on (op - n) {0..<k}"
haftmann@63372
   759
    by (auto intro: inj_onI)
haftmann@63417
   760
  then have "\<Prod>(op - n ` {0..<k}) = setprod (op - n) {0..<k}"
haftmann@63372
   761
    by (auto dest: setprod.reindex)
haftmann@63417
   762
  also have "op - n ` {0..<k} = {Suc (n - k)..n}"
wenzelm@63466
   763
    using True by (auto simp add: image_def Bex_def) presburger  (* FIXME slow *)
haftmann@63417
   764
  finally have *: "setprod (\<lambda>q. n - q) {0..<k} = \<Prod>{Suc (n - k)..n}" ..
wenzelm@63466
   765
  from True have "n choose k = fact n div (fact k * fact (n - k))"
haftmann@63372
   766
    by (rule binomial_fact')
haftmann@63372
   767
  with * show ?thesis
haftmann@63417
   768
    by (simp add: gbinomial_setprod_rev mult.commute [of "fact k"] div_mult2_eq fact_div_fact)
haftmann@63417
   769
qed
haftmann@63417
   770
wenzelm@63466
   771
lemma of_nat_gbinomial: "of_nat (n gchoose k) = (of_nat n gchoose k :: 'a::field_char_0)"
haftmann@63417
   772
proof (cases "k \<le> n")
wenzelm@63466
   773
  case False
wenzelm@63466
   774
  then show ?thesis
haftmann@63417
   775
    by (simp add: not_le gbinomial_binomial binomial_eq_0 gbinomial_setprod_rev)
haftmann@63417
   776
next
wenzelm@63466
   777
  case True
wenzelm@63466
   778
  define m where "m = n - k"
wenzelm@63466
   779
  with True have n: "n = m + k"
haftmann@63417
   780
    by arith
haftmann@63417
   781
  from n have "fact n = ((\<Prod>i = 0..<m + k. of_nat (m + k - i) ):: 'a)"
haftmann@63417
   782
    by (simp add: fact_setprod_rev)
haftmann@63417
   783
  also have "\<dots> = ((\<Prod>i\<in>{0..<k} \<union> {k..<m + k}. of_nat (m + k - i)) :: 'a)"
haftmann@63417
   784
    by (simp add: ivl_disj_un)
wenzelm@63466
   785
  finally have "fact n = (fact m * (\<Prod>i = 0..<k. of_nat m + of_nat k - of_nat i) :: 'a)"
haftmann@63417
   786
    using setprod_shift_bounds_nat_ivl [of "\<lambda>i. of_nat (m + k - i) :: 'a" 0 k m]
haftmann@63417
   787
    by (simp add: fact_setprod_rev [of m] setprod.union_disjoint of_nat_diff)
wenzelm@63466
   788
  then have "fact n / fact (n - k) = ((\<Prod>i = 0..<k. of_nat n - of_nat i) :: 'a)"
haftmann@63417
   789
    by (simp add: n)
haftmann@63417
   790
  with True have "fact k * of_nat (n gchoose k) = (fact k * (of_nat n gchoose k) :: 'a)"
wenzelm@63466
   791
    by (simp only: gbinomial_mult_fact [of k "of_nat n"] gbinomial_binomial [of n k] fact_binomial)
wenzelm@63466
   792
  then show ?thesis
wenzelm@63466
   793
    by simp
haftmann@63372
   794
qed
haftmann@63372
   795
wenzelm@63466
   796
lemma binomial_gbinomial: "of_nat (n choose k) = (of_nat n gchoose k :: 'a::field_char_0)"
haftmann@63417
   797
  by (simp add: gbinomial_binomial [symmetric] of_nat_gbinomial)
lp15@59667
   798
wenzelm@63466
   799
setup
wenzelm@63466
   800
  \<open>Sign.add_const_constraint (@{const_name gbinomial}, SOME @{typ "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a"})\<close>
haftmann@63372
   801
lp15@59667
   802
lemma gbinomial_1[simp]: "a gchoose 1 = a"
haftmann@63417
   803
  by (simp add: gbinomial_setprod_rev lessThan_Suc)
lp15@59667
   804
lp15@59667
   805
lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"
haftmann@63417
   806
  by (simp add: gbinomial_setprod_rev lessThan_Suc)
lp15@59667
   807
lp15@59667
   808
lemma gbinomial_mult_1:
wenzelm@63466
   809
  fixes a :: "'a::field_char_0"
wenzelm@63466
   810
  shows "a * (a gchoose n) = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
wenzelm@63466
   811
  (is "?l = ?r")
lp15@59667
   812
proof -
lp15@59730
   813
  have "?r = ((- 1) ^n * pochhammer (- a) n / (fact n)) * (of_nat n - (- a + of_nat n))"
wenzelm@63466
   814
    apply (simp only: gbinomial_pochhammer pochhammer_Suc right_diff_distrib power_Suc)
haftmann@63367
   815
    apply (simp del: of_nat_Suc fact_Suc)
lp15@59730
   816
    apply (auto simp add: field_simps simp del: of_nat_Suc)
lp15@59730
   817
    done
wenzelm@63466
   818
  also have "\<dots> = ?l"
wenzelm@63466
   819
    by (simp add: field_simps gbinomial_pochhammer)
lp15@59667
   820
  finally show ?thesis ..
lp15@59667
   821
qed
lp15@59667
   822
lp15@59667
   823
lemma gbinomial_mult_1':
wenzelm@63466
   824
  "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
wenzelm@63466
   825
  for a :: "'a::field_char_0"
lp15@59667
   826
  by (simp add: mult.commute gbinomial_mult_1)
lp15@59667
   827
wenzelm@63466
   828
lemma gbinomial_Suc_Suc: "(a + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"
wenzelm@63466
   829
  for a :: "'a::field_char_0"
lp15@59667
   830
proof (cases k)
lp15@59667
   831
  case 0
lp15@59667
   832
  then show ?thesis by simp
lp15@59667
   833
next
lp15@59667
   834
  case (Suc h)
haftmann@63417
   835
  have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)"
lp15@59667
   836
    apply (rule setprod.reindex_cong [where l = Suc])
lp15@59667
   837
      using Suc
haftmann@63367
   838
      apply (auto simp add: image_Suc_atMost)
lp15@59667
   839
    done
lp15@59730
   840
  have "fact (Suc k) * (a gchoose k + (a gchoose (Suc k))) =
wenzelm@63466
   841
      (a gchoose Suc h) * (fact (Suc (Suc h))) +
wenzelm@63466
   842
      (a gchoose Suc (Suc h)) * (fact (Suc (Suc h)))"
haftmann@63367
   843
    by (simp add: Suc field_simps del: fact_Suc)
wenzelm@63466
   844
  also have "\<dots> =
wenzelm@63466
   845
    (a gchoose Suc h) * of_nat (Suc (Suc h) * fact (Suc h)) + (\<Prod>i=0..Suc h. a - of_nat i)"
haftmann@63417
   846
    apply (simp del: fact_Suc add: gbinomial_mult_fact field_simps mult.left_commute [of _ "2"])
wenzelm@63466
   847
    apply (simp del: fact_Suc add: fact_Suc [of "Suc h"] field_simps gbinomial_mult_fact
wenzelm@63466
   848
      mult.left_commute [of _ "2"] atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   849
    done
wenzelm@63466
   850
  also have "\<dots> =
wenzelm@63466
   851
    (fact (Suc h) * (a gchoose Suc h)) * of_nat (Suc (Suc h)) + (\<Prod>i=0..Suc h. a - of_nat i)"
haftmann@63367
   852
    by (simp only: fact_Suc mult.commute mult.left_commute of_nat_fact of_nat_id of_nat_mult)
wenzelm@63466
   853
  also have "\<dots> =
wenzelm@63466
   854
    of_nat (Suc (Suc h)) * (\<Prod>i=0..h. a - of_nat i) + (\<Prod>i=0..Suc h. a - of_nat i)"
haftmann@63417
   855
    unfolding gbinomial_mult_fact atLeastLessThanSuc_atLeastAtMost by auto
wenzelm@63466
   856
  also have "\<dots> =
wenzelm@63466
   857
    (\<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
   858
    by (simp add: field_simps)
wenzelm@63466
   859
  also have "\<dots> =
haftmann@63417
   860
    ((a gchoose Suc h) * (fact (Suc h)) * of_nat (Suc k)) + (\<Prod>i\<in>{0..Suc h}. a - of_nat i)"
lp15@59667
   861
    unfolding gbinomial_mult_fact'
haftmann@63417
   862
    by (simp add: comm_semiring_class.distrib field_simps Suc atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   863
  also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
haftmann@63417
   864
    unfolding gbinomial_mult_fact' atLeast0_atMost_Suc
haftmann@63417
   865
    by (simp add: field_simps Suc atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
   866
  also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
haftmann@63417
   867
    using eq0
haftmann@63417
   868
    by (simp add: Suc setprod.atLeast0_atMost_Suc_shift)
lp15@59730
   869
  also have "\<dots> = (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
wenzelm@63466
   870
    by (simp only: gbinomial_mult_fact atLeastLessThanSuc_atLeastAtMost)
lp15@59730
   871
  finally show ?thesis
haftmann@63417
   872
    using fact_nonzero [of "Suc k"] by auto
lp15@59667
   873
qed
lp15@59667
   874
wenzelm@63466
   875
lemma gbinomial_reduce_nat: "0 < k \<Longrightarrow> a gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)"
wenzelm@63466
   876
  for a :: "'a::field_char_0"
lp15@59730
   877
  by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc)
lp15@59667
   878
lp15@60141
   879
lemma gchoose_row_sum_weighted:
wenzelm@63466
   880
  "(\<Sum>k = 0..m. (r gchoose k) * (r/2 - of_nat k)) = of_nat(Suc m) / 2 * (r gchoose (Suc m))"
wenzelm@63466
   881
  for r :: "'a::field_char_0"
wenzelm@63466
   882
  by (induct m) (simp_all add: field_simps distrib gbinomial_mult_1)
lp15@59667
   883
lp15@59667
   884
lemma binomial_symmetric:
lp15@59667
   885
  assumes kn: "k \<le> n"
lp15@59667
   886
  shows "n choose k = n choose (n - k)"
wenzelm@63466
   887
proof -
wenzelm@63466
   888
  have kn': "n - k \<le> n"
wenzelm@63466
   889
    using kn by arith
lp15@59667
   890
  from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']
wenzelm@63466
   891
  have "fact k * fact (n - k) * (n choose k) = fact (n - k) * fact (n - (n - k)) * (n choose (n - k))"
wenzelm@63466
   892
    by simp
wenzelm@63466
   893
  then show ?thesis
wenzelm@63466
   894
    using kn by simp
lp15@59667
   895
qed
lp15@59667
   896
eberlm@61531
   897
lemma choose_rising_sum:
eberlm@61531
   898
  "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))"
eberlm@61531
   899
  "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose m)"
eberlm@61531
   900
proof -
wenzelm@63466
   901
  show "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))"
wenzelm@63466
   902
    by (induct m) simp_all
wenzelm@63466
   903
  also have "\<dots> = (n + m + 1) choose m"
wenzelm@63466
   904
    by (subst binomial_symmetric) simp_all
wenzelm@63466
   905
  finally show "(\<Sum>j\<le>m. ((n + j) choose n)) = (n + m + 1) choose m" .
eberlm@61531
   906
qed
eberlm@61531
   907
wenzelm@63466
   908
lemma choose_linear_sum: "(\<Sum>i\<le>n. i * (n choose i)) = n * 2 ^ (n - 1)"
eberlm@61531
   909
proof (cases n)
wenzelm@63466
   910
  case 0
wenzelm@63466
   911
  then show ?thesis by simp
wenzelm@63466
   912
next
eberlm@61531
   913
  case (Suc m)
wenzelm@63466
   914
  have "(\<Sum>i\<le>n. i * (n choose i)) = (\<Sum>i\<le>Suc m. i * (Suc m choose i))"
wenzelm@63466
   915
    by (simp add: Suc)
wenzelm@63466
   916
  also have "\<dots> = Suc m * 2 ^ m"
hoelzl@62378
   917
    by (simp only: setsum_atMost_Suc_shift Suc_times_binomial setsum_right_distrib[symmetric])
eberlm@61531
   918
       (simp add: choose_row_sum')
wenzelm@63466
   919
  finally show ?thesis
wenzelm@63466
   920
    using Suc by simp
wenzelm@63466
   921
qed
eberlm@61531
   922
eberlm@61531
   923
lemma choose_alternating_linear_sum:
eberlm@61531
   924
  assumes "n \<noteq> 1"
wenzelm@63466
   925
  shows "(\<Sum>i\<le>n. (-1)^i * of_nat i * of_nat (n choose i) :: 'a::comm_ring_1) = 0"
eberlm@61531
   926
proof (cases n)
wenzelm@63466
   927
  case 0
wenzelm@63466
   928
  then show ?thesis by simp
wenzelm@63466
   929
next
eberlm@61531
   930
  case (Suc m)
wenzelm@63466
   931
  with assms have "m > 0"
wenzelm@63466
   932
    by simp
hoelzl@62378
   933
  have "(\<Sum>i\<le>n. (-1) ^ i * of_nat i * of_nat (n choose i) :: 'a) =
wenzelm@63466
   934
      (\<Sum>i\<le>Suc m. (-1) ^ i * of_nat i * of_nat (Suc m choose i))"
wenzelm@63466
   935
    by (simp add: Suc)
wenzelm@63466
   936
  also have "\<dots> = (\<Sum>i\<le>m. (-1) ^ (Suc i) * of_nat (Suc i * (Suc m choose Suc i)))"
haftmann@63366
   937
    by (simp only: setsum_atMost_Suc_shift setsum_right_distrib[symmetric] mult_ac of_nat_mult) simp
wenzelm@63466
   938
  also have "\<dots> = - of_nat (Suc m) * (\<Sum>i\<le>m. (-1) ^ i * of_nat (m choose i))"
eberlm@61531
   939
    by (subst setsum_right_distrib, rule setsum.cong[OF refl], subst Suc_times_binomial)
haftmann@63366
   940
       (simp add: algebra_simps)
eberlm@61531
   941
  also have "(\<Sum>i\<le>m. (-1 :: 'a) ^ i * of_nat ((m choose i))) = 0"
wenzelm@61799
   942
    using choose_alternating_sum[OF \<open>m > 0\<close>] by simp
wenzelm@63466
   943
  finally show ?thesis
wenzelm@63466
   944
    by simp
wenzelm@63466
   945
qed
eberlm@61531
   946
wenzelm@63466
   947
lemma vandermonde: "(\<Sum>k\<le>r. (m choose k) * (n choose (r - k))) = (m + n) choose r"
wenzelm@63466
   948
proof (induct n arbitrary: r)
eberlm@61531
   949
  case 0
eberlm@61531
   950
  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)"
eberlm@61531
   951
    by (intro setsum.cong) simp_all
wenzelm@63466
   952
  also have "\<dots> = m choose r"
wenzelm@63466
   953
    by (simp add: setsum.delta)
wenzelm@63466
   954
  finally show ?case
wenzelm@63466
   955
    by simp
eberlm@61531
   956
next
eberlm@61531
   957
  case (Suc n r)
wenzelm@63466
   958
  show ?case
wenzelm@63466
   959
    by (cases r) (simp_all add: Suc [symmetric] algebra_simps setsum.distrib Suc_diff_le)
eberlm@61531
   960
qed
eberlm@61531
   961
wenzelm@63466
   962
lemma choose_square_sum: "(\<Sum>k\<le>n. (n choose k)^2) = ((2*n) choose n)"
wenzelm@63466
   963
  using vandermonde[of n n n]
wenzelm@63466
   964
  by (simp add: power2_eq_square mult_2 binomial_symmetric [symmetric])
eberlm@61531
   965
eberlm@61531
   966
lemma pochhammer_binomial_sum:
wenzelm@63466
   967
  fixes a b :: "'a::comm_ring_1"
eberlm@61531
   968
  shows "pochhammer (a + b) n = (\<Sum>k\<le>n. of_nat (n choose k) * pochhammer a k * pochhammer b (n - k))"
eberlm@61531
   969
proof (induction n arbitrary: a b)
wenzelm@63466
   970
  case 0
wenzelm@63466
   971
  then show ?case by simp
wenzelm@63466
   972
next
eberlm@61531
   973
  case (Suc n a b)
eberlm@61531
   974
  have "(\<Sum>k\<le>Suc n. of_nat (Suc n choose k) * pochhammer a k * pochhammer b (Suc n - k)) =
wenzelm@63466
   975
      (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) +
wenzelm@63466
   976
      ((\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) +
wenzelm@63466
   977
      pochhammer b (Suc n))"
eberlm@61531
   978
    by (subst setsum_atMost_Suc_shift) (simp add: ring_distribs setsum.distrib)
eberlm@61531
   979
  also have "(\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) =
wenzelm@63466
   980
      a * pochhammer ((a + 1) + b) n"
eberlm@61531
   981
    by (subst Suc) (simp add: setsum_right_distrib pochhammer_rec mult_ac)
wenzelm@63466
   982
  also have "(\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) +
wenzelm@63466
   983
        pochhammer b (Suc n) =
wenzelm@63466
   984
      (\<Sum>i=0..Suc n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))"
wenzelm@63466
   985
    apply (subst setsum_head_Suc)
wenzelm@63466
   986
    apply simp
wenzelm@63466
   987
    apply (subst setsum_shift_bounds_cl_Suc_ivl)
wenzelm@63466
   988
    apply (simp add: atLeast0AtMost)
wenzelm@63466
   989
    done
wenzelm@63466
   990
  also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))"
eberlm@61531
   991
    using Suc by (intro setsum.mono_neutral_right) (auto simp: not_le binomial_eq_0)
wenzelm@63466
   992
  also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc (n - i)))"
eberlm@61531
   993
    by (intro setsum.cong) (simp_all add: Suc_diff_le)
wenzelm@63466
   994
  also have "\<dots> = b * pochhammer (a + (b + 1)) n"
eberlm@61531
   995
    by (subst Suc) (simp add: setsum_right_distrib mult_ac pochhammer_rec)
eberlm@61531
   996
  also have "a * pochhammer ((a + 1) + b) n + b * pochhammer (a + (b + 1)) n =
wenzelm@63466
   997
      pochhammer (a + b) (Suc n)"
wenzelm@63466
   998
    by (simp add: pochhammer_rec algebra_simps)
eberlm@61531
   999
  finally show ?case ..
wenzelm@63466
  1000
qed
eberlm@61531
  1001
wenzelm@63466
  1002
text \<open>Contributed by Manuel Eberl, generalised by LCP.
wenzelm@63466
  1003
  Alternative definition of the binomial coefficient as @{term "\<Prod>i<k. (n - i) / (k - i)"}.\<close>
wenzelm@63466
  1004
lemma gbinomial_altdef_of_nat: "x gchoose k = (\<Prod>i = 0..<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
wenzelm@63466
  1005
  for k :: nat and x :: "'a::field_char_0"
haftmann@63417
  1006
  by (simp add: setprod_dividef gbinomial_setprod_rev fact_setprod_rev)
lp15@59667
  1007
lp15@59667
  1008
lemma gbinomial_ge_n_over_k_pow_k:
lp15@59667
  1009
  fixes k :: nat
wenzelm@63466
  1010
    and x :: "'a::linordered_field"
lp15@59667
  1011
  assumes "of_nat k \<le> x"
lp15@59667
  1012
  shows "(x / of_nat k :: 'a) ^ k \<le> x gchoose k"
lp15@59667
  1013
proof -
lp15@59667
  1014
  have x: "0 \<le> x"
lp15@59667
  1015
    using assms of_nat_0_le_iff order_trans by blast
haftmann@63417
  1016
  have "(x / of_nat k :: 'a) ^ k = (\<Prod>i = 0..<k. x / of_nat k :: 'a)"
lp15@59667
  1017
    by (simp add: setprod_constant)
wenzelm@63466
  1018
  also have "\<dots> \<le> x gchoose k" (* FIXME *)
lp15@59667
  1019
    unfolding gbinomial_altdef_of_nat
wenzelm@63466
  1020
    apply (safe intro!: setprod_mono)
wenzelm@63466
  1021
    apply simp_all
wenzelm@63466
  1022
    prefer 2
wenzelm@63466
  1023
    subgoal premises for i
wenzelm@63466
  1024
    proof -
wenzelm@63466
  1025
      from assms have "x * of_nat i \<ge> of_nat (i * k)"
wenzelm@63466
  1026
        by (metis mult.commute mult_le_cancel_right of_nat_less_0_iff of_nat_mult)
wenzelm@63466
  1027
      then have "x * of_nat k - x * of_nat i \<le> x * of_nat k - of_nat (i * k)"
wenzelm@63466
  1028
        by arith
wenzelm@63466
  1029
      then have "x * of_nat (k - i) \<le> (x - of_nat i) * of_nat k"
wenzelm@63466
  1030
        using \<open>i < k\<close> by (simp add: algebra_simps zero_less_mult_iff of_nat_diff)
wenzelm@63466
  1031
      then have "x * of_nat (k - i) \<le> (x - of_nat i) * (of_nat k :: 'a)"
wenzelm@63466
  1032
        by (simp only: of_nat_mult[symmetric] of_nat_le_iff)
wenzelm@63466
  1033
      with assms show ?thesis
wenzelm@63466
  1034
        using \<open>i < k\<close> by (simp add: field_simps)
wenzelm@63466
  1035
    qed
wenzelm@63466
  1036
    apply (simp add: x zero_le_divide_iff)
wenzelm@63466
  1037
    done
lp15@59667
  1038
  finally show ?thesis .
lp15@59667
  1039
qed
lp15@59667
  1040
eberlm@61531
  1041
lemma gbinomial_negated_upper: "(a gchoose b) = (-1) ^ b * ((of_nat b - a - 1) gchoose b)"
eberlm@61531
  1042
  by (simp add: gbinomial_pochhammer pochhammer_minus algebra_simps)
eberlm@61531
  1043
eberlm@61531
  1044
lemma gbinomial_minus: "((-a) gchoose b) = (-1) ^ b * ((a + of_nat b - 1) gchoose b)"
eberlm@61531
  1045
  by (subst gbinomial_negated_upper) (simp add: add_ac)
eberlm@61531
  1046
wenzelm@63466
  1047
lemma Suc_times_gbinomial: "of_nat (Suc b) * ((a + 1) gchoose (Suc b)) = (a + 1) * (a gchoose b)"
eberlm@61531
  1048
proof (cases b)
wenzelm@63466
  1049
  case 0
wenzelm@63466
  1050
  then show ?thesis by simp
wenzelm@63466
  1051
next
eberlm@61531
  1052
  case (Suc b)
wenzelm@63466
  1053
  then have "((a + 1) gchoose (Suc (Suc b))) = (\<Prod>i = 0..Suc b. a + (1 - of_nat i)) / fact (b + 2)"
haftmann@63417
  1054
    by (simp add: field_simps gbinomial_setprod_rev atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
  1055
  also have "(\<Prod>i = 0..Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)"
haftmann@63417
  1056
    by (simp add: setprod.atLeast0_atMost_Suc_shift)
wenzelm@63466
  1057
  also have "\<dots> / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)"
haftmann@63417
  1058
    by (simp_all add: gbinomial_setprod_rev atLeastLessThanSuc_atLeastAtMost)
eberlm@61531
  1059
  finally show ?thesis by (simp add: Suc field_simps del: of_nat_Suc)
wenzelm@63466
  1060
qed
eberlm@61531
  1061
wenzelm@63466
  1062
lemma gbinomial_factors: "((a + 1) gchoose (Suc b)) = (a + 1) / of_nat (Suc b) * (a gchoose b)"
eberlm@61531
  1063
proof (cases b)
wenzelm@63466
  1064
  case 0
wenzelm@63466
  1065
  then show ?thesis by simp
wenzelm@63466
  1066
next
eberlm@61531
  1067
  case (Suc b)
wenzelm@63466
  1068
  then have "((a + 1) gchoose (Suc (Suc b))) = (\<Prod>i = 0 .. Suc b. a + (1 - of_nat i)) / fact (b + 2)"
haftmann@63417
  1069
    by (simp add: field_simps gbinomial_setprod_rev atLeastLessThanSuc_atLeastAtMost)
haftmann@63417
  1070
  also have "(\<Prod>i = 0 .. Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)"
haftmann@63417
  1071
    by (simp add: setprod.atLeast0_atMost_Suc_shift)
wenzelm@63466
  1072
  also have "\<dots> / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)"
haftmann@63417
  1073
    by (simp_all add: gbinomial_setprod_rev atLeastLessThanSuc_atLeastAtMost atLeast0AtMost)
wenzelm@63466
  1074
  finally show ?thesis
wenzelm@63466
  1075
    by (simp add: Suc)
wenzelm@63466
  1076
qed
eberlm@61531
  1077
eberlm@61531
  1078
lemma gbinomial_rec: "((r + 1) gchoose (Suc k)) = (r gchoose k) * ((r + 1) / of_nat (Suc k))"
eberlm@61531
  1079
  using gbinomial_mult_1[of r k]
eberlm@61531
  1080
  by (subst gbinomial_Suc_Suc) (simp add: field_simps del: of_nat_Suc, simp add: algebra_simps)
eberlm@61531
  1081
eberlm@61531
  1082
lemma gbinomial_of_nat_symmetric: "k \<le> n \<Longrightarrow> (of_nat n) gchoose k = (of_nat n) gchoose (n - k)"
eberlm@61531
  1083
  using binomial_symmetric[of k n] by (simp add: binomial_gbinomial [symmetric])
eberlm@61531
  1084
eberlm@61531
  1085
wenzelm@63466
  1086
text \<open>The absorption identity (equation 5.5 \cite[p.~157]{GKP}):
wenzelm@63466
  1087
\[
eberlm@61531
  1088
{r \choose k} = \frac{r}{k}{r - 1 \choose k - 1},\quad \textnormal{integer } k \neq 0.
eberlm@61531
  1089
\]\<close>
wenzelm@63466
  1090
lemma gbinomial_absorption': "k > 0 \<Longrightarrow> r gchoose k = (r / of_nat k) * (r - 1 gchoose (k - 1))"
hoelzl@62378
  1091
  using gbinomial_rec[of "r - 1" "k - 1"]
eberlm@61531
  1092
  by (simp_all add: gbinomial_rec field_simps del: of_nat_Suc)
eberlm@61531
  1093
eberlm@61531
  1094
text \<open>The absorption identity is written in the following form to avoid
eberlm@61531
  1095
division by $k$ (the lower index) and therefore remove the $k \neq 0$
wenzelm@63466
  1096
restriction\cite[p.~157]{GKP}:
wenzelm@63466
  1097
\[
eberlm@61531
  1098
k{r \choose k} = r{r - 1 \choose k - 1}, \quad \textnormal{integer } k.
eberlm@61531
  1099
\]\<close>
wenzelm@63466
  1100
lemma gbinomial_absorption: "of_nat (Suc k) * (r gchoose Suc k) = r * ((r - 1) gchoose k)"
eberlm@61531
  1101
  using gbinomial_absorption'[of "Suc k" r] by (simp add: field_simps del: of_nat_Suc)
eberlm@61531
  1102
eberlm@61531
  1103
text \<open>The absorption identity for natural number binomial coefficients:\<close>
wenzelm@63466
  1104
lemma binomial_absorption: "Suc k * (n choose Suc k) = n * ((n - 1) choose k)"
eberlm@61531
  1105
  by (cases n) (simp_all add: binomial_eq_0 Suc_times_binomial del: binomial_Suc_Suc mult_Suc)
eberlm@61531
  1106
eberlm@61531
  1107
text \<open>The absorption companion identity for natural number coefficients,
wenzelm@63466
  1108
  following the proof by GKP \cite[p.~157]{GKP}:\<close>
wenzelm@63466
  1109
lemma binomial_absorb_comp: "(n - k) * (n choose k) = n * ((n - 1) choose k)"
wenzelm@63466
  1110
  (is "?lhs = ?rhs")
eberlm@61531
  1111
proof (cases "n \<le> k")
wenzelm@63466
  1112
  case True
wenzelm@63466
  1113
  then show ?thesis by auto
wenzelm@63466
  1114
next
eberlm@61531
  1115
  case False
eberlm@61531
  1116
  then have "?rhs = Suc ((n - 1) - k) * (n choose Suc ((n - 1) - k))"
eberlm@61531
  1117
    using binomial_symmetric[of k "n - 1"] binomial_absorption[of "(n - 1) - k" n]
eberlm@61531
  1118
    by simp
wenzelm@63466
  1119
  also have "Suc ((n - 1) - k) = n - k"
wenzelm@63466
  1120
    using False by simp
wenzelm@63466
  1121
  also have "n choose \<dots> = n choose k"
wenzelm@63466
  1122
    using False by (intro binomial_symmetric [symmetric]) simp_all
eberlm@61531
  1123
  finally show ?thesis ..
wenzelm@63466
  1124
qed
eberlm@61531
  1125
eberlm@61531
  1126
text \<open>The generalised absorption companion identity:\<close>
eberlm@61531
  1127
lemma gbinomial_absorb_comp: "(r - of_nat k) * (r gchoose k) = r * ((r - 1) gchoose k)"
eberlm@61531
  1128
  using pochhammer_absorb_comp[of r k] by (simp add: gbinomial_pochhammer)
eberlm@61531
  1129
eberlm@61531
  1130
lemma gbinomial_addition_formula:
eberlm@61531
  1131
  "r gchoose (Suc k) = ((r - 1) gchoose (Suc k)) + ((r - 1) gchoose k)"
eberlm@61531
  1132
  using gbinomial_Suc_Suc[of "r - 1" k] by (simp add: algebra_simps)
eberlm@61531
  1133
eberlm@61531
  1134
lemma binomial_addition_formula:
eberlm@61531
  1135
  "0 < n \<Longrightarrow> n choose (Suc k) = ((n - 1) choose (Suc k)) + ((n - 1) choose k)"
eberlm@61531
  1136
  by (subst choose_reduce_nat) simp_all
eberlm@61531
  1137
eberlm@61531
  1138
text \<open>
eberlm@61531
  1139
  Equation 5.9 of the reference material \cite[p.~159]{GKP} is a useful
wenzelm@63466
  1140
  summation formula, operating on both indices:
wenzelm@63466
  1141
  \[
wenzelm@63466
  1142
   \sum\limits_{k \leq n}{r + k \choose k} = {r + n + 1 \choose n},
eberlm@61531
  1143
   \quad \textnormal{integer } n.
hoelzl@62378
  1144
  \]
eberlm@61531
  1145
\<close>
wenzelm@63466
  1146
lemma gbinomial_parallel_sum: "(\<Sum>k\<le>n. (r + of_nat k) gchoose k) = (r + of_nat n + 1) gchoose n"
wenzelm@63466
  1147
proof (induct n)
wenzelm@63466
  1148
  case 0
wenzelm@63466
  1149
  then show ?case by simp
wenzelm@63466
  1150
next
eberlm@61531
  1151
  case (Suc m)
wenzelm@63466
  1152
  then show ?case
wenzelm@63466
  1153
    using gbinomial_Suc_Suc[of "(r + of_nat m + 1)" m]
wenzelm@63466
  1154
    by (simp add: add_ac)
wenzelm@63466
  1155
qed
wenzelm@63466
  1156
eberlm@61531
  1157
haftmann@63373
  1158
subsubsection \<open>Summation on the upper index\<close>
wenzelm@63466
  1159
eberlm@61531
  1160
text \<open>
eberlm@61531
  1161
  Another summation formula is equation 5.10 of the reference material \cite[p.~160]{GKP},
hoelzl@62378
  1162
  aptly named \emph{summation on the upper index}:\[\sum_{0 \leq k \leq n} {k \choose m} =
eberlm@61531
  1163
  {n + 1 \choose m + 1}, \quad \textnormal{integers } m, n \geq 0.\]
eberlm@61531
  1164
\<close>
eberlm@61531
  1165
lemma gbinomial_sum_up_index:
wenzelm@63466
  1166
  "(\<Sum>k = 0..n. (of_nat k gchoose m) :: 'a::field_char_0) = (of_nat n + 1) gchoose (m + 1)"
wenzelm@63466
  1167
proof (induct n)
eberlm@61531
  1168
  case 0
wenzelm@63466
  1169
  show ?case
wenzelm@63466
  1170
    using gbinomial_Suc_Suc[of 0 m]
wenzelm@63466
  1171
    by (cases m) auto
eberlm@61531
  1172
next
eberlm@61531
  1173
  case (Suc n)
wenzelm@63466
  1174
  then show ?case
wenzelm@63466
  1175
    using gbinomial_Suc_Suc[of "of_nat (Suc n) :: 'a" m]
wenzelm@63466
  1176
    by (simp add: add_ac)
eberlm@61531
  1177
qed
eberlm@61531
  1178
hoelzl@62378
  1179
lemma gbinomial_index_swap:
eberlm@61531
  1180
  "((-1) ^ m) * ((- (of_nat n) - 1) gchoose m) = ((-1) ^ n) * ((- (of_nat m) - 1) gchoose n)"
eberlm@61531
  1181
  (is "?lhs = ?rhs")
eberlm@61531
  1182
proof -
eberlm@61531
  1183
  have "?lhs = (of_nat (m + n) gchoose m)"
eberlm@61531
  1184
    by (subst gbinomial_negated_upper) (simp add: power_mult_distrib [symmetric])
wenzelm@63466
  1185
  also have "\<dots> = (of_nat (m + n) gchoose n)"
wenzelm@63466
  1186
    by (subst gbinomial_of_nat_symmetric) simp_all
wenzelm@63466
  1187
  also have "\<dots> = ?rhs"
wenzelm@63466
  1188
    by (subst gbinomial_negated_upper) simp
eberlm@61531
  1189
  finally show ?thesis .
eberlm@61531
  1190
qed
eberlm@61531
  1191
wenzelm@63466
  1192
lemma gbinomial_sum_lower_neg: "(\<Sum>k\<le>m. (r gchoose k) * (- 1) ^ k) = (- 1) ^ m * (r - 1 gchoose m)"
wenzelm@63466
  1193
  (is "?lhs = ?rhs")
eberlm@61531
  1194
proof -
eberlm@61531
  1195
  have "?lhs = (\<Sum>k\<le>m. -(r + 1) + of_nat k gchoose k)"
eberlm@61531
  1196
    by (intro setsum.cong[OF refl]) (subst gbinomial_negated_upper, simp add: power_mult_distrib)
wenzelm@63466
  1197
  also have "\<dots>  = - r + of_nat m gchoose m"
wenzelm@63466
  1198
    by (subst gbinomial_parallel_sum) simp
wenzelm@63466
  1199
  also have "\<dots> = ?rhs"
wenzelm@63466
  1200
    by (subst gbinomial_negated_upper) (simp add: power_mult_distrib)
eberlm@61531
  1201
  finally show ?thesis .
eberlm@61531
  1202
qed
eberlm@61531
  1203
eberlm@61531
  1204
lemma gbinomial_partial_row_sum:
wenzelm@63466
  1205
  "(\<Sum>k\<le>m. (r gchoose k) * ((r / 2) - of_nat k)) = ((of_nat m + 1)/2) * (r gchoose (m + 1))"
wenzelm@63466
  1206
proof (induct m)
wenzelm@63466
  1207
  case 0
wenzelm@63466
  1208
  then show ?case by simp
wenzelm@63466
  1209
next
eberlm@61531
  1210
  case (Suc mm)
wenzelm@63466
  1211
  then have "(\<Sum>k\<le>Suc mm. (r gchoose k) * (r / 2 - of_nat k)) =
wenzelm@63466
  1212
      (r - of_nat (Suc mm)) * (r gchoose Suc mm) / 2"
wenzelm@63466
  1213
    by (simp add: field_simps)
wenzelm@63466
  1214
  also have "\<dots> = r * (r - 1 gchoose Suc mm) / 2"
wenzelm@63466
  1215
    by (subst gbinomial_absorb_comp) (rule refl)
eberlm@61531
  1216
  also have "\<dots> = (of_nat (Suc mm) + 1) / 2 * (r gchoose (Suc mm + 1))"
eberlm@61531
  1217
    by (subst gbinomial_absorption [symmetric]) simp
eberlm@61531
  1218
  finally show ?case .
wenzelm@63466
  1219
qed
eberlm@61531
  1220
eberlm@61531
  1221
lemma setsum_bounds_lt_plus1: "(\<Sum>k<mm. f (Suc k)) = (\<Sum>k=1..mm. f k)"
wenzelm@63466
  1222
  by (induct mm) simp_all
eberlm@61531
  1223
eberlm@61531
  1224
lemma gbinomial_partial_sum_poly:
eberlm@61531
  1225
  "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) =
wenzelm@63466
  1226
    (\<Sum>k\<le>m. (-r gchoose k) * (-x)^k * (x + y)^(m-k))"
wenzelm@63466
  1227
  (is "?lhs m = ?rhs m")
eberlm@61531
  1228
proof (induction m)
wenzelm@63466
  1229
  case 0
wenzelm@63466
  1230
  then show ?case by simp
wenzelm@63466
  1231
next
eberlm@61531
  1232
  case (Suc mm)
wenzelm@63466
  1233
  define G where "G i k = (of_nat i + r gchoose k) * x^k * y^(i - k)" for i k
wenzelm@63040
  1234
  define S where "S = ?lhs"
wenzelm@63466
  1235
  have SG_def: "S = (\<lambda>i. (\<Sum>k\<le>i. (G i k)))"
wenzelm@63466
  1236
    unfolding S_def G_def ..
eberlm@61531
  1237
eberlm@61531
  1238
  have "S (Suc mm) = G (Suc mm) 0 + (\<Sum>k=Suc 0..Suc mm. G (Suc mm) k)"
eberlm@61531
  1239
    using SG_def by (simp add: setsum_head_Suc atLeast0AtMost [symmetric])
eberlm@61531
  1240
  also have "(\<Sum>k=Suc 0..Suc mm. G (Suc mm) k) = (\<Sum>k=0..mm. G (Suc mm) (Suc k))"
eberlm@61531
  1241
    by (subst setsum_shift_bounds_cl_Suc_ivl) simp
wenzelm@63466
  1242
  also have "\<dots> = (\<Sum>k=0..mm. ((of_nat mm + r gchoose (Suc k)) +
wenzelm@63466
  1243
      (of_nat mm + r gchoose k)) * x^(Suc k) * y^(mm - k))"
eberlm@61531
  1244
    unfolding G_def by (subst gbinomial_addition_formula) simp
wenzelm@63466
  1245
  also have "\<dots> = (\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) +
wenzelm@63466
  1246
      (\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k))"
eberlm@61531
  1247
    by (subst setsum.distrib [symmetric]) (simp add: algebra_simps)
hoelzl@62378
  1248
  also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) =
wenzelm@63466
  1249
      (\<Sum>k<Suc mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k))"
eberlm@61531
  1250
    by (simp only: atLeast0AtMost lessThan_Suc_atMost)
wenzelm@63466
  1251
  also have "\<dots> = (\<Sum>k<mm. (of_nat mm + r gchoose Suc k) * x^(Suc k) * y^(mm-k)) +
wenzelm@63466
  1252
      (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"
wenzelm@63466
  1253
    (is "_ = ?A + ?B")
eberlm@61531
  1254
    by (subst setsum_lessThan_Suc) simp
eberlm@61531
  1255
  also have "?A = (\<Sum>k=1..mm. (of_nat mm + r gchoose k) * x^k * y^(mm - k + 1))"
eberlm@61531
  1256
  proof (subst setsum_bounds_lt_plus1 [symmetric], intro setsum.cong[OF refl], clarify)
wenzelm@63466
  1257
    fix k
wenzelm@63466
  1258
    assume "k < mm"
wenzelm@63466
  1259
    then have "mm - k = mm - Suc k + 1"
wenzelm@63466
  1260
      by linarith
wenzelm@63466
  1261
    then show "(of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - k) =
wenzelm@63466
  1262
        (of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - Suc k + 1)"
wenzelm@63466
  1263
      by (simp only:)
eberlm@61531
  1264
  qed
hoelzl@62378
  1265
  also have "\<dots> + ?B = y * (\<Sum>k=1..mm. (G mm k)) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"
eberlm@61531
  1266
    unfolding G_def by (subst setsum_right_distrib) (simp add: algebra_simps)
eberlm@61531
  1267
  also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k)) = x * (S mm)"
wenzelm@63466
  1268
    unfolding S_def by (subst setsum_right_distrib) (simp add: atLeast0AtMost algebra_simps)
wenzelm@63466
  1269
  also have "(G (Suc mm) 0) = y * (G mm 0)"
wenzelm@63466
  1270
    by (simp add: G_def)
wenzelm@63466
  1271
  finally have "S (Suc mm) =
wenzelm@63466
  1272
      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
  1273
    by (simp add: ring_distribs)
wenzelm@63466
  1274
  also have "G mm 0 + (\<Sum>k=1..mm. (G mm k)) = S mm"
eberlm@61531
  1275
    by (simp add: setsum_head_Suc[symmetric] SG_def atLeast0AtMost)
hoelzl@62378
  1276
  finally have "S (Suc mm) = (x + y) * (S mm) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)"
eberlm@61531
  1277
    by (simp add: algebra_simps)
wenzelm@63466
  1278
  also have "(of_nat mm + r gchoose (Suc mm)) = (-1) ^ (Suc mm) * (- r gchoose (Suc mm))"
eberlm@61531
  1279
    by (subst gbinomial_negated_upper) simp
eberlm@61531
  1280
  also have "(-1) ^ Suc mm * (- r gchoose Suc mm) * x ^ Suc mm =
wenzelm@63466
  1281
      (- r gchoose (Suc mm)) * (-x) ^ Suc mm"
wenzelm@63466
  1282
    by (simp add: power_minus[of x])
wenzelm@63466
  1283
  also have "(x + y) * S mm + \<dots> = (x + y) * ?rhs mm + (- r gchoose (Suc mm)) * (- x)^Suc mm"
eberlm@61531
  1284
    unfolding S_def by (subst Suc.IH) simp
eberlm@61531
  1285
  also have "(x + y) * ?rhs mm = (\<Sum>n\<le>mm. ((- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n)))"
eberlm@61531
  1286
    by (subst setsum_right_distrib, rule setsum.cong) (simp_all add: Suc_diff_le)
hoelzl@62378
  1287
  also have "\<dots> + (-r gchoose (Suc mm)) * (-x)^Suc mm =
wenzelm@63466
  1288
      (\<Sum>n\<le>Suc mm. (- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n))"
wenzelm@63466
  1289
    by simp
wenzelm@63466
  1290
  finally show ?case
wenzelm@63466
  1291
    by (simp only: S_def)
wenzelm@63466
  1292
qed
eberlm@61531
  1293
eberlm@61531
  1294
lemma gbinomial_partial_sum_poly_xpos:
hoelzl@62378
  1295
  "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) =
eberlm@61531
  1296
     (\<Sum>k\<le>m. (of_nat k + r - 1 gchoose k) * x^k * (x + y)^(m-k))"
eberlm@61531
  1297
  apply (subst gbinomial_partial_sum_poly)
eberlm@61531
  1298
  apply (subst gbinomial_negated_upper)
eberlm@61531
  1299
  apply (intro setsum.cong, rule refl)
eberlm@61531
  1300
  apply (simp add: power_mult_distrib [symmetric])
eberlm@61531
  1301
  done
eberlm@61531
  1302
eberlm@61531
  1303
lemma binomial_r_part_sum: "(\<Sum>k\<le>m. (2 * m + 1 choose k)) = 2 ^ (2 * m)"
eberlm@61531
  1304
proof -
eberlm@61531
  1305
  have "2 * 2^(2*m) = (\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k))"
eberlm@61531
  1306
    using choose_row_sum[where n="2 * m + 1"] by simp
wenzelm@63466
  1307
  also have "(\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k)) =
wenzelm@63466
  1308
      (\<Sum>k = 0..m. (2 * m + 1 choose k)) +
wenzelm@63466
  1309
      (\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k))"
wenzelm@63466
  1310
    using setsum_ub_add_nat[of 0 m "\<lambda>k. 2 * m + 1 choose k" "m+1"]
wenzelm@63466
  1311
    by (simp add: mult_2)
eberlm@61531
  1312
  also have "(\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k)) =
wenzelm@63466
  1313
      (\<Sum>k = 0..m. (2 * m + 1 choose (k + (m + 1))))"
eberlm@61531
  1314
    by (subst setsum_shift_bounds_cl_nat_ivl [symmetric]) (simp add: mult_2)
eberlm@61531
  1315
  also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose (m - k)))"
eberlm@61531
  1316
    by (intro setsum.cong[OF refl], subst binomial_symmetric) simp_all
eberlm@61531
  1317
  also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose k))"
haftmann@63417
  1318
    using setsum.atLeast_atMost_rev [of "\<lambda>k. 2 * m + 1 choose (m - k)" 0 m]
haftmann@63417
  1319
    by simp
wenzelm@63466
  1320
  also have "\<dots> + \<dots> = 2 * \<dots>"
wenzelm@63466
  1321
    by simp
wenzelm@63466
  1322
  finally show ?thesis
wenzelm@63466
  1323
    by (subst (asm) mult_cancel1) (simp add: atLeast0AtMost)
eberlm@61531
  1324
qed
eberlm@61531
  1325
wenzelm@63466
  1326
lemma gbinomial_r_part_sum: "(\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k)) = 2 ^ (2 * m)"
wenzelm@63466
  1327
  (is "?lhs = ?rhs")
eberlm@61531
  1328
proof -
hoelzl@62378
  1329
  have "?lhs = of_nat (\<Sum>k\<le>m. (2 * m + 1) choose k)"
haftmann@63366
  1330
    by (simp add: binomial_gbinomial add_ac)
wenzelm@63466
  1331
  also have "\<dots> = of_nat (2 ^ (2 * m))"
wenzelm@63466
  1332
    by (subst binomial_r_part_sum) (rule refl)
haftmann@63366
  1333
  finally show ?thesis by simp
eberlm@61531
  1334
qed
eberlm@61531
  1335
eberlm@61531
  1336
lemma gbinomial_sum_nat_pow2:
wenzelm@63466
  1337
  "(\<Sum>k\<le>m. (of_nat (m + k) gchoose k :: 'a::field_char_0) / 2 ^ k) = 2 ^ m"
wenzelm@63466
  1338
  (is "?lhs = ?rhs")
eberlm@61531
  1339
proof -
wenzelm@63466
  1340
  have "2 ^ m * 2 ^ m = (2 ^ (2*m) :: 'a)"
wenzelm@63466
  1341
    by (induct m) simp_all
wenzelm@63466
  1342
  also have "\<dots> = (\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k))"
wenzelm@63466
  1343
    using gbinomial_r_part_sum ..
eberlm@61531
  1344
  also have "\<dots> = (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) * 2 ^ (m - k))"
eberlm@61531
  1345
    using gbinomial_partial_sum_poly_xpos[where x="1" and y="1" and r="of_nat m + 1" and m="m"]
eberlm@61531
  1346
    by (simp add: add_ac)
eberlm@61531
  1347
  also have "\<dots> = 2 ^ m * (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) / 2 ^ k)"
eberlm@61531
  1348
    by (subst setsum_right_distrib) (simp add: algebra_simps power_diff)
wenzelm@63466
  1349
  finally show ?thesis
wenzelm@63466
  1350
    by (subst (asm) mult_left_cancel) simp_all
eberlm@61531
  1351
qed
eberlm@61531
  1352
eberlm@61531
  1353
lemma gbinomial_trinomial_revision:
eberlm@61531
  1354
  assumes "k \<le> m"
wenzelm@63466
  1355
  shows "(r gchoose m) * (of_nat m gchoose k) = (r gchoose k) * (r - of_nat k gchoose (m - k))"
eberlm@61531
  1356
proof -
wenzelm@63466
  1357
  have "(r gchoose m) * (of_nat m gchoose k) = (r gchoose m) * fact m / (fact k * fact (m - k))"
eberlm@61531
  1358
    using assms by (simp add: binomial_gbinomial [symmetric] binomial_fact)
wenzelm@63466
  1359
  also have "\<dots> = (r gchoose k) * (r - of_nat k gchoose (m - k))"
wenzelm@63466
  1360
    using assms by (simp add: gbinomial_pochhammer power_diff pochhammer_product)
eberlm@61531
  1361
  finally show ?thesis .
eberlm@61531
  1362
qed
eberlm@61531
  1363
wenzelm@63466
  1364
text \<open>Versions of the theorems above for the natural-number version of "choose"\<close>
lp15@59667
  1365
lemma binomial_altdef_of_nat:
wenzelm@63466
  1366
  "k \<le> n \<Longrightarrow> of_nat (n choose k) = (\<Prod>i = 0..<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
wenzelm@63466
  1367
  for n k :: nat and x :: "'a::field_char_0"
wenzelm@63466
  1368
  by (simp add: gbinomial_altdef_of_nat binomial_gbinomial of_nat_diff)
lp15@59667
  1369
wenzelm@63466
  1370
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
  1371
  for k n :: nat and x :: "'a::linordered_field"
wenzelm@63466
  1372
  by (simp add: gbinomial_ge_n_over_k_pow_k binomial_gbinomial of_nat_diff)
lp15@59667
  1373
lp15@59667
  1374
lemma binomial_le_pow:
lp15@59667
  1375
  assumes "r \<le> n"
lp15@59667
  1376
  shows "n choose r \<le> n ^ r"
lp15@59667
  1377
proof -
lp15@59667
  1378
  have "n choose r \<le> fact n div fact (n - r)"
wenzelm@63466
  1379
    using assms by (subst binomial_fact_lemma[symmetric]) auto
wenzelm@63466
  1380
  with fact_div_fact_le_pow [OF assms] show ?thesis
wenzelm@63466
  1381
    by auto
lp15@59667
  1382
qed
lp15@59667
  1383
wenzelm@63466
  1384
lemma binomial_altdef_nat: "k \<le> n \<Longrightarrow> n choose k = fact n div (fact k * fact (n - k))"
wenzelm@63466
  1385
  for k n :: nat
wenzelm@63466
  1386
  by (subst binomial_fact_lemma [symmetric]) auto
lp15@59667
  1387
wenzelm@63466
  1388
lemma choose_dvd:
wenzelm@63466
  1389
  "k \<le> n \<Longrightarrow> fact k * fact (n - k) dvd (fact n :: 'a::{semiring_div,linordered_semidom})"
lp15@59730
  1390
  unfolding dvd_def
lp15@59730
  1391
  apply (rule exI [where x="of_nat (n choose k)"])
lp15@59730
  1392
  using binomial_fact_lemma [of k n, THEN arg_cong [where f = of_nat and 'b='a]]
haftmann@63366
  1393
  apply auto
lp15@59667
  1394
  done
lp15@59667
  1395
hoelzl@62378
  1396
lemma fact_fact_dvd_fact:
wenzelm@63466
  1397
  "fact k * fact n dvd (fact (k + n) :: 'a::{semiring_div,linordered_semidom})"
wenzelm@63466
  1398
  by (metis add.commute add_diff_cancel_left' choose_dvd le_add2)
lp15@59667
  1399
lp15@59667
  1400
lemma choose_mult_lemma:
wenzelm@63466
  1401
  "((m + r + k) choose (m + k)) * ((m + k) choose k) = ((m + r + k) choose k) * ((m + r) choose m)"
wenzelm@63466
  1402
  (is "?lhs = _")
lp15@59667
  1403
proof -
wenzelm@63466
  1404
  have "?lhs =
wenzelm@63466
  1405
      fact (m + r + k) div (fact (m + k) * fact (m + r - m)) * (fact (m + k) div (fact k * fact m))"
wenzelm@63092
  1406
    by (simp add: binomial_altdef_nat)
wenzelm@63466
  1407
  also have "\<dots> = fact (m + r + k) div (fact r * (fact k * fact m))"
lp15@59667
  1408
    apply (subst div_mult_div_if_dvd)
lp15@59730
  1409
    apply (auto simp: algebra_simps fact_fact_dvd_fact)
lp15@59667
  1410
    apply (metis add.assoc add.commute fact_fact_dvd_fact)
lp15@59667
  1411
    done
wenzelm@63466
  1412
  also have "\<dots> = (fact (m + r + k) * fact (m + r)) div (fact r * (fact k * fact m) * fact (m + r))"
lp15@59667
  1413
    apply (subst div_mult_div_if_dvd [symmetric])
lp15@59730
  1414
    apply (auto simp add: algebra_simps)
haftmann@62344
  1415
    apply (metis fact_fact_dvd_fact dvd_trans nat_mult_dvd_cancel_disj)
lp15@59667
  1416
    done
wenzelm@63466
  1417
  also have "\<dots> =
wenzelm@63466
  1418
      (fact (m + r + k) div (fact k * fact (m + r)) * (fact (m + r) div (fact r * fact m)))"
lp15@59667
  1419
    apply (subst div_mult_div_if_dvd)
lp15@59730
  1420
    apply (auto simp: fact_fact_dvd_fact algebra_simps)
lp15@59667
  1421
    done
lp15@59667
  1422
  finally show ?thesis
lp15@59667
  1423
    by (simp add: binomial_altdef_nat mult.commute)
lp15@59667
  1424
qed
lp15@59667
  1425
wenzelm@63466
  1426
text \<open>The "Subset of a Subset" identity.\<close>
lp15@59667
  1427
lemma choose_mult:
wenzelm@63466
  1428
  "k \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> (n choose m) * (m choose k) = (n choose k) * ((n - k) choose (m - k))"
wenzelm@63466
  1429
  using choose_mult_lemma [of "m-k" "n-m" k] by simp
lp15@59667
  1430
lp15@59667
  1431
haftmann@63373
  1432
subsection \<open>More on Binomial Coefficients\<close>
lp15@59667
  1433
wenzelm@63466
  1434
lemma choose_one: "n choose 1 = n" for n :: nat
lp15@59667
  1435
  by simp
lp15@59667
  1436
lp15@59667
  1437
lemma card_UNION:
wenzelm@63466
  1438
  assumes "finite A"
wenzelm@63466
  1439
    and "\<forall>k \<in> A. finite k"
lp15@59667
  1440
  shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))"
lp15@59667
  1441
  (is "?lhs = ?rhs")
lp15@59667
  1442
proof -
wenzelm@63466
  1443
  have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))"
wenzelm@63466
  1444
    by simp
wenzelm@63466
  1445
  also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))"
wenzelm@63466
  1446
    (is "_ = nat ?rhs")
wenzelm@63466
  1447
    by (subst setsum_right_distrib) simp
lp15@59667
  1448
  also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))"
wenzelm@63466
  1449
    using assms by (subst setsum.Sigma) auto
lp15@59667
  1450
  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))"
lp15@59667
  1451
    by (rule setsum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)
lp15@59667
  1452
  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
  1453
    using assms
wenzelm@63466
  1454
    by (auto intro!: setsum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])
lp15@59667
  1455
  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)))"
wenzelm@63466
  1456
    using assms by (subst setsum.Sigma) auto
lp15@59667
  1457
  also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "setsum ?lhs _ = _")
wenzelm@63466
  1458
  proof (rule setsum.cong[OF refl])
lp15@59667
  1459
    fix x
lp15@59667
  1460
    assume x: "x \<in> \<Union>A"
wenzelm@63040
  1461
    define K where "K = {X \<in> A. x \<in> X}"
wenzelm@63466
  1462
    with \<open>finite A\<close> have K: "finite K"
wenzelm@63466
  1463
      by auto
lp15@59667
  1464
    let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I}"
lp15@59667
  1465
    have "inj_on snd (SIGMA i:{1..card A}. ?I i)"
wenzelm@63466
  1466
      using assms by (auto intro!: inj_onI)
lp15@59667
  1467
    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
  1468
      using assms
wenzelm@63466
  1469
      by (auto intro!: rev_image_eqI[where x="(card a, a)" for a]
lp15@59667
  1470
        simp add: card_gt_0_iff[folded Suc_le_eq]
lp15@59667
  1471
        dest: finite_subset intro: card_mono)
lp15@59667
  1472
    ultimately have "?lhs x = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))"
lp15@59667
  1473
      by (rule setsum.reindex_cong [where l = snd]) fastforce
lp15@59667
  1474
    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)))"
wenzelm@63466
  1475
      using assms by (subst setsum.Sigma) auto
lp15@59667
  1476
    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))"
wenzelm@63466
  1477
      by (subst setsum_right_distrib) simp
wenzelm@63466
  1478
    also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))"
wenzelm@63466
  1479
      (is "_ = ?rhs")
wenzelm@63466
  1480
    proof (rule setsum.mono_neutral_cong_right[rule_format])
wenzelm@63466
  1481
      show "finite {1..card A}"
wenzelm@63466
  1482
        by simp
wenzelm@63466
  1483
      show "{1..card K} \<subseteq> {1..card A}"
wenzelm@63466
  1484
        using \<open>finite A\<close> by (auto simp add: K_def intro: card_mono)
lp15@59667
  1485
    next
lp15@59667
  1486
      fix i
lp15@59667
  1487
      assume "i \<in> {1..card A} - {1..card K}"
wenzelm@63466
  1488
      then have i: "i \<le> card A" "card K < i"
wenzelm@63466
  1489
        by auto
lp15@59667
  1490
      have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}"
wenzelm@63466
  1491
        by (auto simp add: K_def)
wenzelm@63466
  1492
      also have "\<dots> = {}"
wenzelm@63466
  1493
        using \<open>finite A\<close> i by (auto simp add: K_def dest: card_mono[rotated 1])
lp15@59667
  1494
      finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0"
wenzelm@63466
  1495
        by (simp only:) simp
lp15@59667
  1496
    next
lp15@59667
  1497
      fix i
lp15@59667
  1498
      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
  1499
        (is "?lhs = ?rhs")
wenzelm@63466
  1500
        by (rule setsum.cong) (auto simp add: K_def)
wenzelm@63466
  1501
      then show "(- 1) ^ (i + 1) * ?lhs = (- 1) ^ (i + 1) * ?rhs"
wenzelm@63466
  1502
        by simp
wenzelm@63466
  1503
    qed
wenzelm@63466
  1504
    also have "{I. I \<subseteq> K \<and> card I = 0} = {{}}"
wenzelm@63466
  1505
      using assms by (auto simp add: card_eq_0_iff K_def dest: finite_subset)
wenzelm@63466
  1506
    then have "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1"
wenzelm@63466
  1507
      by (subst (2) setsum_head_Suc) simp_all
lp15@59667
  1508
    also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1"
wenzelm@63466
  1509
      using K by (subst n_subsets[symmetric]) simp_all
lp15@59667
  1510
    also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1"
wenzelm@63466
  1511
      by (subst setsum_right_distrib[symmetric]) simp
lp15@59667
  1512
    also have "\<dots> =  - ((-1 + 1) ^ card K) + 1"
wenzelm@63466
  1513
      by (subst binomial_ring) (simp add: ac_simps)
wenzelm@63466
  1514
    also have "\<dots> = 1"
wenzelm@63466
  1515
      using x K by (auto simp add: K_def card_gt_0_iff)
lp15@59667
  1516
    finally show "?lhs x = 1" .
lp15@59667
  1517
  qed
wenzelm@63466
  1518
  also have "nat \<dots> = card (\<Union>A)"
wenzelm@63466
  1519
    by simp
lp15@59667
  1520
  finally show ?thesis ..
lp15@59667
  1521
qed
lp15@59667
  1522
wenzelm@63466
  1523
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
  1524
lemma card_length_sum_list_rec:
wenzelm@63466
  1525
  assumes "m \<ge> 1"
nipkow@63882
  1526
  shows "card {l::nat list. length l = m \<and> sum_list l = N} =
nipkow@63882
  1527
      card {l. length l = (m - 1) \<and> sum_list l = N} +
nipkow@63882
  1528
      card {l. length l = m \<and> sum_list l + 1 = N}"
wenzelm@63466
  1529
    (is "card ?C = card ?A + card ?B")
lp15@59667
  1530
proof -
nipkow@63882
  1531
  let ?A' = "{l. length l = m \<and> sum_list l = N \<and> hd l = 0}"
nipkow@63882
  1532
  let ?B' = "{l. length l = m \<and> sum_list l = N \<and> hd l \<noteq> 0}"
wenzelm@63466
  1533
  let ?f = "\<lambda>l. 0 # l"
wenzelm@63466
  1534
  let ?g = "\<lambda>l. (hd l + 1) # tl l"
wenzelm@63466
  1535
  have 1: "xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" for x xs
wenzelm@63466
  1536
    by simp
nipkow@63882
  1537
  have 2: "xs \<noteq> [] \<Longrightarrow> sum_list(tl xs) = sum_list xs - hd xs" for xs :: "nat list"
wenzelm@63466
  1538
    by (auto simp add: neq_Nil_conv)
lp15@59667
  1539
  have f: "bij_betw ?f ?A ?A'"
wenzelm@63466
  1540
    apply (rule bij_betw_byWitness[where f' = tl])
lp15@59667
  1541
    using assms
wenzelm@63466
  1542
    apply (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv)
wenzelm@63466
  1543
    done
nipkow@63882
  1544
  have 3: "xs \<noteq> [] \<Longrightarrow> hd xs + (sum_list xs - hd xs) = sum_list xs" for xs :: "nat list"
nipkow@63882
  1545
    by (metis 1 sum_list_simps(2) 2)
lp15@59667
  1546
  have g: "bij_betw ?g ?B ?B'"
wenzelm@63466
  1547
    apply (rule bij_betw_byWitness[where f' = "\<lambda>l. (hd l - 1) # tl l"])
lp15@59667
  1548
    using assms
lp15@59667
  1549
    by (auto simp: 2 length_0_conv[symmetric] intro!: 3
wenzelm@63466
  1550
        simp del: length_greater_0_conv length_0_conv)
wenzelm@63466
  1551
  have fin: "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}" for M N :: nat
wenzelm@63466
  1552
    using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto
lp15@59667
  1553
  have fin_A: "finite ?A" using fin[of _ "N+1"]
wenzelm@63466
  1554
    by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"])
nipkow@63882
  1555
      (auto simp: member_le_sum_list_nat less_Suc_eq_le)
lp15@59667
  1556
  have fin_B: "finite ?B"
wenzelm@63466
  1557
    by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"])
nipkow@63882
  1558
      (auto simp: member_le_sum_list_nat less_Suc_eq_le fin)
wenzelm@63466
  1559
  have uni: "?C = ?A' \<union> ?B'"
wenzelm@63466
  1560
    by auto
wenzelm@63466
  1561
  have disj: "?A' \<inter> ?B' = {}"
wenzelm@63466
  1562
    by auto
wenzelm@63466
  1563
  have "card ?C = card(?A' \<union> ?B')"
wenzelm@63466
  1564
    using uni by simp
lp15@59667
  1565
  also have "\<dots> = card ?A + card ?B"
lp15@59667
  1566
    using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g]
lp15@59667
  1567
      bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B
lp15@59667
  1568
    by presburger
lp15@59667
  1569
  finally show ?thesis .
lp15@59667
  1570
qed
lp15@59667
  1571
nipkow@63882
  1572
lemma card_length_sum_list: "card {l::nat list. size l = m \<and> sum_list l = N} = (N + m - 1) choose N"
wenzelm@63466
  1573
  \<comment> "by Holden Lee, tidied by Tobias Nipkow"
lp15@59667
  1574
proof (cases m)
wenzelm@63466
  1575
  case 0
wenzelm@63466
  1576
  then show ?thesis
wenzelm@63466
  1577
    by (cases N) (auto cong: conj_cong)
lp15@59667
  1578
next
lp15@59667
  1579
  case (Suc m')
wenzelm@63466
  1580
  have m: "m \<ge> 1"
wenzelm@63466
  1581
    by (simp add: Suc)
wenzelm@63466
  1582
  then show ?thesis
wenzelm@63466
  1583
  proof (induct "N + m - 1" arbitrary: N m)
wenzelm@63466
  1584
    case 0  \<comment> "In the base case, the only solution is [0]."
wenzelm@63466
  1585
    have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {[0]}"
wenzelm@63466
  1586
      by (auto simp: length_Suc_conv)
wenzelm@63466
  1587
    have "m = 1 \<and> N = 0"
wenzelm@63466
  1588
      using 0 by linarith
wenzelm@63466
  1589
    then show ?case
wenzelm@63466
  1590
      by simp
wenzelm@63466
  1591
  next
wenzelm@63466
  1592
    case (Suc k)
nipkow@63882
  1593
    have c1: "card {l::nat list. size l = (m - 1) \<and> sum_list l =  N} = (N + (m - 1) - 1) choose N"
wenzelm@63466
  1594
    proof (cases "m = 1")
wenzelm@63466
  1595
      case True
wenzelm@63466
  1596
      with Suc.hyps have "N \<ge> 1"
wenzelm@63466
  1597
        by auto
wenzelm@63466
  1598
      with True show ?thesis
wenzelm@63466
  1599
        by (simp add: binomial_eq_0)
lp15@59667
  1600
    next
wenzelm@63466
  1601
      case False
wenzelm@63466
  1602
      then show ?thesis
wenzelm@63466
  1603
        using Suc by fastforce
wenzelm@63466
  1604
    qed
nipkow@63882
  1605
    from Suc have c2: "card {l::nat list. size l = m \<and> sum_list l + 1 = N} =
wenzelm@63466
  1606
      (if N > 0 then ((N - 1) + m - 1) choose (N - 1) else 0)"
wenzelm@63466
  1607
    proof -
wenzelm@63466
  1608
      have *: "n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" for m n
wenzelm@63466
  1609
        by arith
wenzelm@63466
  1610
      from Suc have "N > 0 \<Longrightarrow>
nipkow@63882
  1611
        card {l::nat list. size l = m \<and> sum_list l + 1 = N} =
wenzelm@63466
  1612
          ((N - 1) + m - 1) choose (N - 1)"
wenzelm@63466
  1613
        by (simp add: *)
wenzelm@63466
  1614
      then show ?thesis
wenzelm@63466
  1615
        by auto
wenzelm@63466
  1616
    qed
nipkow@63882
  1617
    from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> sum_list l = N} +
nipkow@63882
  1618
          card {l::nat list. size l = m \<and> sum_list l + 1 = N}) = (N + m - 1) choose N"
wenzelm@63466
  1619
      by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def)
wenzelm@63466
  1620
    then show ?case
nipkow@63882
  1621
      using card_length_sum_list_rec[OF Suc.prems] by auto
wenzelm@63466
  1622
  qed
lp15@59667
  1623
qed
lp15@59667
  1624
wenzelm@63466
  1625
lemma Suc_times_binomial_add: "Suc a * (Suc (a + b) choose Suc a) = Suc b * (Suc (a + b) choose a)"
wenzelm@63466
  1626
  \<comment> \<open>by Lukas Bulwahn\<close>
hoelzl@60604
  1627
proof -
hoelzl@60604
  1628
  have dvd: "Suc a * (fact a * fact b) dvd fact (Suc (a + b))" for a b
hoelzl@60604
  1629
    using fact_fact_dvd_fact[of "Suc a" "b", where 'a=nat]
hoelzl@60604
  1630
    by (simp only: fact_Suc add_Suc[symmetric] of_nat_id mult.assoc)
hoelzl@60604
  1631
  have "Suc a * (fact (Suc (a + b)) div (Suc a * fact a * fact b)) =
hoelzl@60604
  1632
      Suc a * fact (Suc (a + b)) div (Suc a * (fact a * fact b))"
hoelzl@60604
  1633
    by (subst div_mult_swap[symmetric]; simp only: mult.assoc dvd)
hoelzl@60604
  1634
  also have "\<dots> = Suc b * fact (Suc (a + b)) div (Suc b * (fact a * fact b))"
hoelzl@60604
  1635
    by (simp only: div_mult_mult1)
hoelzl@60604
  1636
  also have "\<dots> = Suc b * (fact (Suc (a + b)) div (Suc b * (fact a * fact b)))"
hoelzl@60604
  1637
    using dvd[of b a] by (subst div_mult_swap[symmetric]; simp only: ac_simps dvd)
hoelzl@60604
  1638
  finally show ?thesis
hoelzl@60604
  1639
    by (subst (1 2) binomial_altdef_nat)
wenzelm@63466
  1640
      (simp_all only: ac_simps diff_Suc_Suc Suc_diff_le diff_add_inverse fact_Suc of_nat_id)
hoelzl@60604
  1641
qed
hoelzl@60604
  1642
haftmann@63373
  1643
haftmann@63373
  1644
subsection \<open>Misc\<close>
haftmann@63373
  1645
eberlm@62128
  1646
lemma fact_code [code]:
wenzelm@63466
  1647
  "fact n = (of_nat (fold_atLeastAtMost_nat (op *) 2 n 1) :: 'a::semiring_char_0)"
eberlm@62128
  1648
proof -
haftmann@63417
  1649
  have "fact n = (of_nat (\<Prod>{1..n}) :: 'a)"
haftmann@63417
  1650
    by (simp add: fact_setprod)
eberlm@62128
  1651
  also have "\<Prod>{1..n} = \<Prod>{2..n}"
eberlm@62128
  1652
    by (intro setprod.mono_neutral_right) auto
eberlm@62128
  1653
  also have "\<dots> = fold_atLeastAtMost_nat (op *) 2 n 1"
eberlm@62128
  1654
    by (simp add: setprod_atLeastAtMost_code)
eberlm@62128
  1655
  finally show ?thesis .
eberlm@62128
  1656
qed
eberlm@62128
  1657
eberlm@62128
  1658
lemma pochhammer_code [code]:
wenzelm@63466
  1659
  "pochhammer a n =
wenzelm@63466
  1660
    (if n = 0 then 1
wenzelm@63466
  1661
     else fold_atLeastAtMost_nat (\<lambda>n acc. (a + of_nat n) * acc) 0 (n - 1) 1)"
wenzelm@63466
  1662
  by (cases n)
wenzelm@63466
  1663
    (simp_all add: pochhammer_setprod setprod_atLeastAtMost_code [symmetric]
wenzelm@63466
  1664
      atLeastLessThanSuc_atLeastAtMost)
eberlm@62128
  1665
eberlm@62128
  1666
lemma gbinomial_code [code]:
wenzelm@63466
  1667
  "a gchoose n =
wenzelm@63466
  1668
    (if n = 0 then 1
wenzelm@63466
  1669
     else fold_atLeastAtMost_nat (\<lambda>n acc. (a - of_nat n) * acc) 0 (n - 1) 1 / fact n)"
wenzelm@63466
  1670
  by (cases n)
wenzelm@63466
  1671
    (simp_all add: gbinomial_setprod_rev setprod_atLeastAtMost_code [symmetric]
wenzelm@63466
  1672
      atLeastLessThanSuc_atLeastAtMost)
eberlm@62128
  1673
wenzelm@63466
  1674
(* FIXME *)
eberlm@62142
  1675
(*TODO: This code equation breaks Scala code generation in HOL-Codegenerator_Test. We have to figure out why and how to prevent that. *)
eberlm@62142
  1676
eberlm@62142
  1677
(*
eberlm@62128
  1678
lemma binomial_code [code]:
eberlm@62128
  1679
  "(n choose k) =
eberlm@62128
  1680
      (if k > n then 0
eberlm@62128
  1681
       else if 2 * k > n then (n choose (n - k))
eberlm@62142
  1682
       else (fold_atLeastAtMost_nat (op * ) (n-k+1) n 1 div fact k))"
eberlm@62128
  1683
proof -
eberlm@62128
  1684
  {
eberlm@62128
  1685
    assume "k \<le> n"
wenzelm@63466
  1686
    then have "{1..n} = {1..n-k} \<union> {n-k+1..n}" by auto
wenzelm@63466
  1687
    then have "(fact n :: nat) = fact (n-k) * \<Prod>{n-k+1..n}"
eberlm@62128
  1688
      by (simp add: setprod.union_disjoint fact_altdef_nat)
eberlm@62128
  1689
  }
wenzelm@63466
  1690
  then show ?thesis by (auto simp: binomial_altdef_nat mult_ac setprod_atLeastAtMost_code)
hoelzl@62378
  1691
qed
eberlm@62142
  1692
*)
eberlm@62128
  1693
nipkow@15131
  1694
end