src/HOL/Binomial.thy
author wenzelm
Tue Sep 01 22:32:58 2015 +0200 (2015-09-01)
changeset 61076 bdc1e2f0a86a
parent 60758 d8d85a8172b5
child 61531 ab2e862263e7
permissions -rw-r--r--
eliminated \<Colon>;
lp15@59669
     1
(*  Title       : Binomial.thy
paulson@12196
     2
    Author      : Jacques D. Fleuriot
paulson@12196
     3
    Copyright   : 1998  University of Cambridge
paulson@15094
     4
    Conversion to Isar and new proofs by Lawrence C Paulson, 2004
avigad@32036
     5
    The integer version of factorial and other additions by Jeremy Avigad.
paulson@12196
     6
*)
paulson@12196
     7
wenzelm@60758
     8
section\<open>Factorial Function, Binomial Coefficients and Binomial Theorem\<close>
paulson@15094
     9
lp15@59669
    10
theory Binomial
haftmann@33319
    11
imports Main
nipkow@15131
    12
begin
paulson@15094
    13
wenzelm@60758
    14
subsection \<open>Factorial\<close>
lp15@59730
    15
lp15@59733
    16
fun fact :: "nat \<Rightarrow> ('a::semiring_char_0)"
lp15@59730
    17
  where "fact 0 = 1" | "fact (Suc n) = of_nat (Suc n) * fact n"
avigad@32036
    18
lp15@59730
    19
lemmas fact_Suc = fact.simps(2)
lp15@59730
    20
lp15@59730
    21
lemma fact_1 [simp]: "fact 1 = 1"
lp15@59730
    22
  by simp
lp15@59730
    23
lp15@59730
    24
lemma fact_Suc_0 [simp]: "fact (Suc 0) = Suc 0"
lp15@59730
    25
  by simp
avigad@32036
    26
lp15@59730
    27
lemma of_nat_fact [simp]: "of_nat (fact n) = fact n"
lp15@59730
    28
  by (induct n) (auto simp add: algebra_simps of_nat_mult)
lp15@59730
    29
 
lp15@59730
    30
lemma fact_reduce: "n > 0 \<Longrightarrow> fact n = of_nat n * fact (n - 1)"
lp15@59730
    31
  by (cases n) auto
avigad@32036
    32
lp15@59733
    33
lemma fact_nonzero [simp]: "fact n \<noteq> (0::'a::{semiring_char_0,semiring_no_zero_divisors})"
lp15@59730
    34
  apply (induct n)
lp15@59730
    35
  apply auto
lp15@59730
    36
  using of_nat_eq_0_iff by fastforce
lp15@59730
    37
lp15@59730
    38
lemma fact_mono_nat: "m \<le> n \<Longrightarrow> fact m \<le> (fact n :: nat)"
lp15@59730
    39
  by (induct n) (auto simp: le_Suc_eq)
avigad@32036
    40
lp15@59730
    41
context
wenzelm@60241
    42
  assumes "SORT_CONSTRAINT('a::linordered_semidom)"
lp15@59667
    43
begin
lp15@59730
    44
  
lp15@59730
    45
  lemma fact_mono: "m \<le> n \<Longrightarrow> fact m \<le> (fact n :: 'a)"
lp15@59730
    46
    by (metis of_nat_fact of_nat_le_iff fact_mono_nat)
lp15@59730
    47
  
lp15@59730
    48
  lemma fact_ge_1 [simp]: "fact n \<ge> (1 :: 'a)"
lp15@59730
    49
    by (metis le0 fact.simps(1) fact_mono)
lp15@59730
    50
  
lp15@59730
    51
  lemma fact_gt_zero [simp]: "fact n > (0 :: 'a)"
lp15@59730
    52
    using fact_ge_1 less_le_trans zero_less_one by blast
lp15@59730
    53
  
lp15@59730
    54
  lemma fact_ge_zero [simp]: "fact n \<ge> (0 :: 'a)"
lp15@59730
    55
    by (simp add: less_imp_le)
avigad@32036
    56
lp15@59730
    57
  lemma fact_not_neg [simp]: "~ (fact n < (0 :: 'a))"
lp15@59730
    58
    by (simp add: not_less_iff_gr_or_eq)
lp15@59730
    59
    
lp15@59730
    60
  lemma fact_le_power:
lp15@59730
    61
      "fact n \<le> (of_nat (n^n) ::'a)"
lp15@59730
    62
  proof (induct n)
lp15@59730
    63
    case (Suc n)
lp15@59730
    64
    then have *: "fact n \<le> (of_nat (Suc n ^ n) ::'a)"
lp15@59730
    65
      by (rule order_trans) (simp add: power_mono)
lp15@59730
    66
    have "fact (Suc n) = (of_nat (Suc n) * fact n ::'a)"
lp15@59730
    67
      by (simp add: algebra_simps)
lp15@59730
    68
    also have "... \<le> (of_nat (Suc n) * of_nat (Suc n ^ n) ::'a)"
lp15@59730
    69
      by (simp add: "*" ordered_comm_semiring_class.comm_mult_left_mono)
lp15@59730
    70
    also have "... \<le> (of_nat (Suc n ^ Suc n) ::'a)"
lp15@59730
    71
      by (metis of_nat_mult order_refl power_Suc)
lp15@59730
    72
    finally show ?case .
lp15@59730
    73
  qed simp
avigad@32036
    74
avigad@32036
    75
end
avigad@32036
    76
wenzelm@60758
    77
text\<open>Note that @{term "fact 0 = fact 1"}\<close>
lp15@59730
    78
lemma fact_less_mono_nat: "\<lbrakk>0 < m; m < n\<rbrakk> \<Longrightarrow> fact m < (fact n :: nat)"
lp15@59730
    79
  by (induct n) (auto simp: less_Suc_eq)
avigad@32036
    80
lp15@59730
    81
lemma fact_less_mono:
wenzelm@60241
    82
  "\<lbrakk>0 < m; m < n\<rbrakk> \<Longrightarrow> fact m < (fact n :: 'a::linordered_semidom)"
lp15@59730
    83
  by (metis of_nat_fact of_nat_less_iff fact_less_mono_nat)
avigad@32036
    84
lp15@59730
    85
lemma fact_ge_Suc_0_nat [simp]: "fact n \<ge> Suc 0"
lp15@59730
    86
  by (metis One_nat_def fact_ge_1)
avigad@32036
    87
lp15@59730
    88
lemma dvd_fact: 
lp15@59730
    89
  shows "1 \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> m dvd fact n"
lp15@59730
    90
  by (induct n) (auto simp: dvdI le_Suc_eq)
avigad@32036
    91
lp15@59730
    92
lemma fact_altdef_nat: "fact n = \<Prod>{1..n}"
lp15@59730
    93
  by (induct n) (auto simp: atLeastAtMostSuc_conv)
avigad@32036
    94
lp15@59730
    95
lemma fact_altdef: "fact n = setprod of_nat {1..n}"
lp15@59730
    96
  by (induct n) (auto simp: atLeastAtMostSuc_conv)
paulson@15094
    97
lp15@59730
    98
lemma fact_dvd: "n \<le> m \<Longrightarrow> fact n dvd (fact m :: 'a :: {semiring_div,linordered_semidom})"
lp15@59730
    99
  by (induct m) (auto simp: le_Suc_eq)
avigad@32036
   100
lp15@59730
   101
lemma fact_mod: "m \<le> n \<Longrightarrow> fact n mod (fact m :: 'a :: {semiring_div,linordered_semidom}) = 0"
lp15@59730
   102
  by (auto simp add: fact_dvd)
bulwahn@40033
   103
bulwahn@40033
   104
lemma fact_div_fact:
lp15@59730
   105
  assumes "m \<ge> n"
bulwahn@40033
   106
  shows "(fact m) div (fact n) = \<Prod>{n + 1..m}"
bulwahn@40033
   107
proof -
bulwahn@40033
   108
  obtain d where "d = m - n" by auto
bulwahn@40033
   109
  from assms this have "m = n + d" by auto
bulwahn@40033
   110
  have "fact (n + d) div (fact n) = \<Prod>{n + 1..n + d}"
bulwahn@40033
   111
  proof (induct d)
bulwahn@40033
   112
    case 0
bulwahn@40033
   113
    show ?case by simp
bulwahn@40033
   114
  next
bulwahn@40033
   115
    case (Suc d')
bulwahn@40033
   116
    have "fact (n + Suc d') div fact n = Suc (n + d') * fact (n + d') div fact n"
bulwahn@40033
   117
      by simp
lp15@59667
   118
    also from Suc.hyps have "... = Suc (n + d') * \<Prod>{n + 1..n + d'}"
bulwahn@40033
   119
      unfolding div_mult1_eq[of _ "fact (n + d')"] by (simp add: fact_mod)
bulwahn@40033
   120
    also have "... = \<Prod>{n + 1..n + Suc d'}"
lp15@59730
   121
      by (simp add: atLeastAtMostSuc_conv)
bulwahn@40033
   122
    finally show ?case .
bulwahn@40033
   123
  qed
wenzelm@60758
   124
  from this \<open>m = n + d\<close> show ?thesis by simp
bulwahn@40033
   125
qed
bulwahn@40033
   126
lp15@59730
   127
lemma fact_num_eq_if: 
lp15@59730
   128
    "fact m = (if m=0 then 1 else of_nat m * fact (m - 1))"
avigad@32036
   129
by (cases m) auto
avigad@32036
   130
lp15@59730
   131
lemma fact_eq_rev_setprod_nat: "fact k = (\<Prod>i<k. k - i)"
hoelzl@50224
   132
  unfolding fact_altdef_nat
hoelzl@57129
   133
  by (rule setprod.reindex_bij_witness[where i="\<lambda>i. k - i" and j="\<lambda>i. k - i"]) auto
hoelzl@50224
   134
hoelzl@50240
   135
lemma fact_div_fact_le_pow:
hoelzl@50240
   136
  assumes "r \<le> n" shows "fact n div fact (n - r) \<le> n ^ r"
hoelzl@50240
   137
proof -
hoelzl@50240
   138
  have "\<And>r. r \<le> n \<Longrightarrow> \<Prod>{n - r..n} = (n - r) * \<Prod>{Suc (n - r)..n}"
haftmann@57418
   139
    by (subst setprod.insert[symmetric]) (auto simp: atLeastAtMost_insertL)
hoelzl@50240
   140
  with assms show ?thesis
hoelzl@50240
   141
    by (induct r rule: nat.induct) (auto simp add: fact_div_fact Suc_diff_Suc mult_le_mono)
hoelzl@50240
   142
qed
hoelzl@50240
   143
wenzelm@60758
   144
lemma fact_numeral:  --\<open>Evaluation for specific numerals\<close>
lp15@57113
   145
  "fact (numeral k) = (numeral k) * (fact (pred_numeral k))"
lp15@59730
   146
  by (metis fact.simps(2) numeral_eq_Suc of_nat_numeral)
lp15@57113
   147
lp15@59658
   148
wenzelm@60758
   149
text \<open>This development is based on the work of Andy Gordon and
wenzelm@60758
   150
  Florian Kammueller.\<close>
lp15@59658
   151
wenzelm@60758
   152
subsection \<open>Basic definitions and lemmas\<close>
lp15@59658
   153
lp15@59658
   154
primrec binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat" (infixl "choose" 65)
lp15@59658
   155
where
lp15@59658
   156
  "0 choose k = (if k = 0 then 1 else 0)"
lp15@59658
   157
| "Suc n choose k = (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"
lp15@59658
   158
lp15@59658
   159
lemma binomial_n_0 [simp]: "(n choose 0) = 1"
lp15@59658
   160
  by (cases n) simp_all
lp15@59658
   161
lp15@59658
   162
lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
lp15@59658
   163
  by simp
lp15@59658
   164
lp15@59658
   165
lemma binomial_Suc_Suc [simp]: "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
lp15@59658
   166
  by simp
lp15@59658
   167
lp15@59667
   168
lemma choose_reduce_nat:
lp15@59658
   169
  "0 < (n::nat) \<Longrightarrow> 0 < k \<Longrightarrow>
lp15@59658
   170
    (n choose k) = ((n - 1) choose (k - 1)) + ((n - 1) choose k)"
lp15@59658
   171
  by (metis Suc_diff_1 binomial.simps(2) neq0_conv)
lp15@59658
   172
lp15@59658
   173
lemma binomial_eq_0: "n < k \<Longrightarrow> n choose k = 0"
lp15@59658
   174
  by (induct n arbitrary: k) auto
lp15@59658
   175
lp15@59658
   176
declare binomial.simps [simp del]
lp15@59658
   177
lp15@59658
   178
lemma binomial_n_n [simp]: "n choose n = 1"
lp15@59658
   179
  by (induct n) (simp_all add: binomial_eq_0)
lp15@59658
   180
lp15@59658
   181
lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n"
lp15@59658
   182
  by (induct n) simp_all
lp15@59658
   183
lp15@59658
   184
lemma binomial_1 [simp]: "n choose Suc 0 = n"
lp15@59658
   185
  by (induct n) simp_all
lp15@59658
   186
lp15@59658
   187
lemma zero_less_binomial: "k \<le> n \<Longrightarrow> n choose k > 0"
lp15@59658
   188
  by (induct n k rule: diff_induct) simp_all
lp15@59658
   189
lp15@59658
   190
lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> n < k"
lp15@59658
   191
  by (metis binomial_eq_0 less_numeral_extra(3) not_less zero_less_binomial)
lp15@59658
   192
lp15@59658
   193
lemma zero_less_binomial_iff [simp]: "n choose k > 0 \<longleftrightarrow> k \<le> n"
lp15@59658
   194
  by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)
lp15@59658
   195
lp15@59658
   196
lemma Suc_times_binomial_eq:
lp15@59658
   197
  "Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
lp15@59658
   198
  apply (induct n arbitrary: k, simp add: binomial.simps)
lp15@59658
   199
  apply (case_tac k)
lp15@59658
   200
   apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0)
lp15@59658
   201
  done
lp15@59658
   202
lp15@60141
   203
lemma binomial_le_pow2: "n choose k \<le> 2^n"
lp15@60141
   204
  apply (induction n arbitrary: k)
lp15@60141
   205
  apply (simp add: binomial.simps)
lp15@60141
   206
  apply (case_tac k)
lp15@60141
   207
  apply (auto simp: power_Suc)
lp15@60141
   208
  by (simp add: add_le_mono mult_2)
lp15@60141
   209
wenzelm@60758
   210
text\<open>The absorption property\<close>
lp15@59658
   211
lemma Suc_times_binomial:
lp15@59658
   212
  "Suc k * (Suc n choose Suc k) = Suc n * (n choose k)"
lp15@59658
   213
  using Suc_times_binomial_eq by auto
lp15@59658
   214
wenzelm@60758
   215
text\<open>This is the well-known version of absorption, but it's harder to use because of the
wenzelm@60758
   216
  need to reason about division.\<close>
lp15@59658
   217
lemma binomial_Suc_Suc_eq_times:
lp15@59658
   218
    "(Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
lp15@59658
   219
  by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)
lp15@59658
   220
wenzelm@60758
   221
text\<open>Another version of absorption, with -1 instead of Suc.\<close>
lp15@59658
   222
lemma times_binomial_minus1_eq:
lp15@59658
   223
  "0 < k \<Longrightarrow> k * (n choose k) = n * ((n - 1) choose (k - 1))"
lp15@59658
   224
  using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"]
lp15@59658
   225
  by (auto split add: nat_diff_split)
lp15@59658
   226
lp15@59658
   227
wenzelm@60758
   228
subsection \<open>Combinatorial theorems involving @{text "choose"}\<close>
lp15@59658
   229
wenzelm@60758
   230
text \<open>By Florian Kamm\"uller, tidied by LCP.\<close>
lp15@59658
   231
lp15@59658
   232
lemma card_s_0_eq_empty: "finite A \<Longrightarrow> card {B. B \<subseteq> A & card B = 0} = 1"
lp15@59658
   233
  by (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
lp15@59658
   234
lp15@59658
   235
lemma choose_deconstruct: "finite M \<Longrightarrow> x \<notin> M \<Longrightarrow>
lp15@59658
   236
    {s. s \<subseteq> insert x M \<and> card s = Suc k} =
lp15@59658
   237
    {s. s \<subseteq> M \<and> card s = Suc k} \<union> {s. \<exists>t. t \<subseteq> M \<and> card t = k \<and> s = insert x t}"
lp15@59658
   238
  apply safe
lp15@59658
   239
     apply (auto intro: finite_subset [THEN card_insert_disjoint])
lp15@59667
   240
  by (metis (full_types) Diff_insert_absorb Set.set_insert Zero_neq_Suc card_Diff_singleton_if
lp15@59658
   241
     card_eq_0_iff diff_Suc_1 in_mono subset_insert_iff)
lp15@59658
   242
lp15@59658
   243
lemma finite_bex_subset [simp]:
lp15@59658
   244
  assumes "finite B"
lp15@59658
   245
    and "\<And>A. A \<subseteq> B \<Longrightarrow> finite {x. P x A}"
lp15@59658
   246
  shows "finite {x. \<exists>A \<subseteq> B. P x A}"
lp15@59658
   247
  by (metis (no_types) assms finite_Collect_bounded_ex finite_Collect_subsets)
lp15@59658
   248
wenzelm@60758
   249
text\<open>There are as many subsets of @{term A} having cardinality @{term k}
lp15@59658
   250
 as there are sets obtained from the former by inserting a fixed element
wenzelm@60758
   251
 @{term x} into each.\<close>
lp15@59658
   252
lemma constr_bij:
lp15@59658
   253
   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow>
lp15@59658
   254
    card {B. \<exists>C. C \<subseteq> A \<and> card C = k \<and> B = insert x C} =
lp15@59658
   255
    card {B. B \<subseteq> A & card(B) = k}"
lp15@59658
   256
  apply (rule card_bij_eq [where f = "\<lambda>s. s - {x}" and g = "insert x"])
lp15@59658
   257
  apply (auto elim!: equalityE simp add: inj_on_def)
lp15@59658
   258
  apply (metis card_Diff_singleton_if finite_subset in_mono)
lp15@59658
   259
  done
lp15@59658
   260
wenzelm@60758
   261
text \<open>
lp15@59658
   262
  Main theorem: combinatorial statement about number of subsets of a set.
wenzelm@60758
   263
\<close>
lp15@59658
   264
lp15@59658
   265
theorem n_subsets: "finite A \<Longrightarrow> card {B. B \<subseteq> A \<and> card B = k} = (card A choose k)"
lp15@59658
   266
proof (induct k arbitrary: A)
lp15@59658
   267
  case 0 then show ?case by (simp add: card_s_0_eq_empty)
lp15@59658
   268
next
lp15@59658
   269
  case (Suc k)
wenzelm@60758
   270
  show ?case using \<open>finite A\<close>
lp15@59658
   271
  proof (induct A)
lp15@59658
   272
    case empty show ?case by (simp add: card_s_0_eq_empty)
lp15@59658
   273
  next
lp15@59658
   274
    case (insert x A)
lp15@59658
   275
    then show ?case using Suc.hyps
lp15@59658
   276
      apply (simp add: card_s_0_eq_empty choose_deconstruct)
lp15@59658
   277
      apply (subst card_Un_disjoint)
lp15@59658
   278
         prefer 4 apply (force simp add: constr_bij)
lp15@59658
   279
        prefer 3 apply force
lp15@59658
   280
       prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
lp15@59658
   281
         finite_subset [of _ "Pow (insert x F)" for F])
lp15@59658
   282
      apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
lp15@59658
   283
      done
lp15@59658
   284
  qed
lp15@59658
   285
qed
lp15@59658
   286
lp15@59658
   287
wenzelm@60758
   288
subsection \<open>The binomial theorem (courtesy of Tobias Nipkow):\<close>
lp15@59658
   289
wenzelm@60758
   290
text\<open>Avigad's version, generalized to any commutative ring\<close>
lp15@59667
   291
theorem binomial_ring: "(a+b::'a::{comm_ring_1,power})^n =
lp15@59658
   292
  (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))" (is "?P n")
lp15@59658
   293
proof (induct n)
lp15@59658
   294
  case 0 then show "?P 0" by simp
lp15@59658
   295
next
lp15@59658
   296
  case (Suc n)
lp15@59658
   297
  have decomp: "{0..n+1} = {0} Un {n+1} Un {1..n}"
lp15@59658
   298
    by auto
lp15@59658
   299
  have decomp2: "{0..n} = {0} Un {1..n}"
lp15@59658
   300
    by auto
lp15@59667
   301
  have "(a+b)^(n+1) =
lp15@59658
   302
      (a+b) * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
lp15@59658
   303
    using Suc.hyps by simp
lp15@59658
   304
  also have "\<dots> = a*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +
lp15@59658
   305
                   b*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
lp15@59658
   306
    by (rule distrib_right)
lp15@59658
   307
  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +
lp15@59658
   308
                  (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k+1))"
lp15@59658
   309
    by (auto simp add: setsum_right_distrib ac_simps)
lp15@59658
   310
  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n+1-k)) +
lp15@59658
   311
                  (\<Sum>k=1..n+1. of_nat (n choose (k - 1)) * a^k * b^(n+1-k))"
lp15@59667
   312
    by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le field_simps
lp15@59658
   313
        del:setsum_cl_ivl_Suc)
lp15@59658
   314
  also have "\<dots> = a^(n+1) + b^(n+1) +
lp15@59658
   315
                  (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n+1-k)) +
lp15@59658
   316
                  (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n+1-k))"
lp15@59658
   317
    by (simp add: decomp2)
lp15@59658
   318
  also have
lp15@59667
   319
      "\<dots> = a^(n+1) + b^(n+1) +
lp15@59658
   320
            (\<Sum>k=1..n. of_nat(n+1 choose k) * a^k * b^(n+1-k))"
lp15@59658
   321
    by (auto simp add: field_simps setsum.distrib [symmetric] choose_reduce_nat)
lp15@59658
   322
  also have "\<dots> = (\<Sum>k=0..n+1. of_nat (n+1 choose k) * a^k * b^(n+1-k))"
lp15@59658
   323
    using decomp by (simp add: field_simps)
lp15@59658
   324
  finally show "?P (Suc n)" by simp
lp15@59658
   325
qed
lp15@59658
   326
wenzelm@60758
   327
text\<open>Original version for the naturals\<close>
lp15@59658
   328
corollary binomial: "(a+b::nat)^n = (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))"
lp15@59658
   329
    using binomial_ring [of "int a" "int b" n]
lp15@59658
   330
  by (simp only: of_nat_add [symmetric] of_nat_mult [symmetric] of_nat_power [symmetric]
lp15@59658
   331
           of_nat_setsum [symmetric]
lp15@59658
   332
           of_nat_eq_iff of_nat_id)
lp15@59658
   333
lp15@59658
   334
lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
lp15@59658
   335
proof (induct n arbitrary: k rule: nat_less_induct)
lp15@59658
   336
  fix n k assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) =
lp15@59658
   337
                      fact m" and kn: "k \<le> n"
lp15@59658
   338
  let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"
lp15@59658
   339
  { assume "n=0" then have ?ths using kn by simp }
lp15@59658
   340
  moreover
lp15@59658
   341
  { assume "k=0" then have ?ths using kn by simp }
lp15@59658
   342
  moreover
lp15@59658
   343
  { assume nk: "n=k" then have ?ths by simp }
lp15@59658
   344
  moreover
lp15@59658
   345
  { fix m h assume n: "n = Suc m" and h: "k = Suc h" and hm: "h < m"
lp15@59658
   346
    from n have mn: "m < n" by arith
lp15@59658
   347
    from hm have hm': "h \<le> m" by arith
lp15@59658
   348
    from hm h n kn have km: "k \<le> m" by arith
lp15@59658
   349
    have "m - h = Suc (m - Suc h)" using  h km hm by arith
lp15@59658
   350
    with km h have th0: "fact (m - h) = (m - h) * fact (m - k)"
lp15@59658
   351
      by simp
lp15@59658
   352
    from n h th0
lp15@59658
   353
    have "fact k * fact (n - k) * (n choose k) =
lp15@59667
   354
        k * (fact h * fact (m - h) * (m choose h)) +
lp15@59658
   355
        (m - h) * (fact k * fact (m - k) * (m choose k))"
lp15@59658
   356
      by (simp add: field_simps)
lp15@59658
   357
    also have "\<dots> = (k + (m - h)) * fact m"
lp15@59658
   358
      using H[rule_format, OF mn hm'] H[rule_format, OF mn km]
lp15@59658
   359
      by (simp add: field_simps)
lp15@59658
   360
    finally have ?ths using h n km by simp }
lp15@59658
   361
  moreover have "n=0 \<or> k = 0 \<or> k = n \<or> (\<exists>m h. n = Suc m \<and> k = Suc h \<and> h < m)"
lp15@59658
   362
    using kn by presburger
lp15@59658
   363
  ultimately show ?ths by blast
lp15@59658
   364
qed
lp15@59658
   365
lp15@59658
   366
lemma binomial_fact:
lp15@59658
   367
  assumes kn: "k \<le> n"
lp15@59730
   368
  shows "(of_nat (n choose k) :: 'a::field_char_0) =
lp15@59730
   369
         (fact n) / (fact k * fact(n - k))"
lp15@59658
   370
  using binomial_fact_lemma[OF kn]
lp15@59730
   371
  apply (simp add: field_simps)
lp15@59730
   372
  by (metis mult.commute of_nat_fact of_nat_mult)
lp15@59658
   373
lp15@59667
   374
lemma choose_row_sum: "(\<Sum>k=0..n. n choose k) = 2^n"
lp15@59667
   375
  using binomial [of 1 "1" n]
lp15@59667
   376
  by (simp add: numeral_2_eq_2)
lp15@59667
   377
lp15@59667
   378
lemma sum_choose_lower: "(\<Sum>k=0..n. (r+k) choose k) = Suc (r+n) choose n"
lp15@59667
   379
  by (induct n) auto
lp15@59667
   380
lp15@59667
   381
lemma sum_choose_upper: "(\<Sum>k=0..n. k choose m) = Suc n choose Suc m"
lp15@59667
   382
  by (induct n) auto
lp15@59667
   383
lp15@59667
   384
lemma natsum_reverse_index:
lp15@59667
   385
  fixes m::nat
lp15@59667
   386
  shows "(\<And>k. m \<le> k \<Longrightarrow> k \<le> n \<Longrightarrow> g k = f (m + n - k)) \<Longrightarrow> (\<Sum>k=m..n. f k) = (\<Sum>k=m..n. g k)"
lp15@59667
   387
  by (rule setsum.reindex_bij_witness[where i="\<lambda>k. m+n-k" and j="\<lambda>k. m+n-k"]) auto
lp15@59667
   388
wenzelm@60758
   389
text\<open>NW diagonal sum property\<close>
lp15@59667
   390
lemma sum_choose_diagonal:
lp15@59667
   391
  assumes "m\<le>n" shows "(\<Sum>k=0..m. (n-k) choose (m-k)) = Suc n choose m"
lp15@59667
   392
proof -
lp15@59667
   393
  have "(\<Sum>k=0..m. (n-k) choose (m-k)) = (\<Sum>k=0..m. (n-m+k) choose k)"
lp15@59667
   394
    by (rule natsum_reverse_index) (simp add: assms)
lp15@59667
   395
  also have "... = Suc (n-m+m) choose m"
lp15@59667
   396
    by (rule sum_choose_lower)
lp15@59667
   397
  also have "... = Suc n choose m" using assms
lp15@59667
   398
    by simp
lp15@59667
   399
  finally show ?thesis .
lp15@59667
   400
qed
lp15@59667
   401
wenzelm@60758
   402
subsection\<open>Pochhammer's symbol : generalized rising factorial\<close>
lp15@59667
   403
wenzelm@60758
   404
text \<open>See @{url "http://en.wikipedia.org/wiki/Pochhammer_symbol"}\<close>
lp15@59667
   405
lp15@59667
   406
definition "pochhammer (a::'a::comm_semiring_1) n =
lp15@59667
   407
  (if n = 0 then 1 else setprod (\<lambda>n. a + of_nat n) {0 .. n - 1})"
lp15@59667
   408
lp15@59667
   409
lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"
lp15@59667
   410
  by (simp add: pochhammer_def)
lp15@59667
   411
lp15@59667
   412
lemma pochhammer_1 [simp]: "pochhammer a 1 = a"
lp15@59667
   413
  by (simp add: pochhammer_def)
lp15@59667
   414
lp15@59667
   415
lemma pochhammer_Suc0 [simp]: "pochhammer a (Suc 0) = a"
lp15@59667
   416
  by (simp add: pochhammer_def)
lp15@59667
   417
lp15@59667
   418
lemma pochhammer_Suc_setprod: "pochhammer a (Suc n) = setprod (\<lambda>n. a + of_nat n) {0 .. n}"
lp15@59667
   419
  by (simp add: pochhammer_def)
lp15@59667
   420
lp15@59667
   421
lemma setprod_nat_ivl_Suc: "setprod f {0 .. Suc n} = setprod f {0..n} * f (Suc n)"
lp15@59667
   422
proof -
lp15@59667
   423
  have "{0..Suc n} = {0..n} \<union> {Suc n}" by auto
lp15@59667
   424
  then show ?thesis by (simp add: field_simps)
lp15@59667
   425
qed
lp15@59667
   426
lp15@59667
   427
lemma setprod_nat_ivl_1_Suc: "setprod f {0 .. Suc n} = f 0 * setprod f {1.. Suc n}"
lp15@59667
   428
proof -
lp15@59667
   429
  have "{0..Suc n} = {0} \<union> {1 .. Suc n}" by auto
lp15@59667
   430
  then show ?thesis by simp
lp15@59667
   431
qed
lp15@59667
   432
lp15@59667
   433
lp15@59667
   434
lemma pochhammer_Suc: "pochhammer a (Suc n) = pochhammer a n * (a + of_nat n)"
lp15@59667
   435
proof (cases n)
lp15@59667
   436
  case 0
lp15@59667
   437
  then show ?thesis by simp
lp15@59667
   438
next
lp15@59667
   439
  case (Suc n)
lp15@59667
   440
  show ?thesis unfolding Suc pochhammer_Suc_setprod setprod_nat_ivl_Suc ..
lp15@59667
   441
qed
lp15@59667
   442
lp15@59667
   443
lemma pochhammer_rec: "pochhammer a (Suc n) = a * pochhammer (a + 1) n"
lp15@59667
   444
proof (cases "n = 0")
lp15@59667
   445
  case True
lp15@59667
   446
  then show ?thesis by (simp add: pochhammer_Suc_setprod)
lp15@59667
   447
next
lp15@59667
   448
  case False
lp15@59667
   449
  have *: "finite {1 .. n}" "0 \<notin> {1 .. n}" by auto
lp15@59667
   450
  have eq: "insert 0 {1 .. n} = {0..n}" by auto
wenzelm@61076
   451
  have **: "(\<Prod>n\<in>{1::nat..n}. a + of_nat n) = (\<Prod>n\<in>{0::nat..n - 1}. a + 1 + of_nat n)"
lp15@59667
   452
    apply (rule setprod.reindex_cong [where l = Suc])
lp15@59667
   453
    using False
lp15@59667
   454
    apply (auto simp add: fun_eq_iff field_simps)
lp15@59667
   455
    done
lp15@59667
   456
  show ?thesis
lp15@59667
   457
    apply (simp add: pochhammer_def)
lp15@59667
   458
    unfolding setprod.insert [OF *, unfolded eq]
lp15@59667
   459
    using ** apply (simp add: field_simps)
lp15@59667
   460
    done
lp15@59667
   461
qed
lp15@59667
   462
lp15@59730
   463
lemma pochhammer_fact: "fact n = pochhammer 1 n"
lp15@59730
   464
  unfolding fact_altdef
lp15@59667
   465
  apply (cases n)
lp15@59667
   466
   apply (simp_all add: of_nat_setprod pochhammer_Suc_setprod)
lp15@59667
   467
  apply (rule setprod.reindex_cong [where l = Suc])
lp15@59667
   468
    apply (auto simp add: fun_eq_iff)
lp15@59667
   469
  done
lp15@59667
   470
lp15@59667
   471
lemma pochhammer_of_nat_eq_0_lemma:
lp15@59667
   472
  assumes "k > n"
lp15@59667
   473
  shows "pochhammer (- (of_nat n :: 'a:: idom)) k = 0"
lp15@59667
   474
proof (cases "n = 0")
lp15@59667
   475
  case True
lp15@59667
   476
  then show ?thesis
lp15@59667
   477
    using assms by (cases k) (simp_all add: pochhammer_rec)
lp15@59667
   478
next
lp15@59667
   479
  case False
lp15@59667
   480
  from assms obtain h where "k = Suc h" by (cases k) auto
lp15@59667
   481
  then show ?thesis
lp15@59667
   482
    by (simp add: pochhammer_Suc_setprod)
lp15@59667
   483
       (metis Suc_leI Suc_le_mono assms atLeastAtMost_iff less_eq_nat.simps(1))
lp15@59667
   484
qed
lp15@59667
   485
lp15@59667
   486
lemma pochhammer_of_nat_eq_0_lemma':
lp15@59667
   487
  assumes kn: "k \<le> n"
lp15@59667
   488
  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k \<noteq> 0"
lp15@59667
   489
proof (cases k)
lp15@59667
   490
  case 0
lp15@59667
   491
  then show ?thesis by simp
lp15@59667
   492
next
lp15@59667
   493
  case (Suc h)
lp15@59667
   494
  then show ?thesis
lp15@59667
   495
    apply (simp add: pochhammer_Suc_setprod)
lp15@59667
   496
    using Suc kn apply (auto simp add: algebra_simps)
lp15@59667
   497
    done
lp15@59667
   498
qed
lp15@59667
   499
lp15@59667
   500
lemma pochhammer_of_nat_eq_0_iff:
lp15@59667
   501
  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k = 0 \<longleftrightarrow> k > n"
lp15@59667
   502
  (is "?l = ?r")
lp15@59667
   503
  using pochhammer_of_nat_eq_0_lemma[of n k, where ?'a='a]
lp15@59667
   504
    pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]
lp15@59667
   505
  by (auto simp add: not_le[symmetric])
lp15@59667
   506
lp15@59667
   507
lemma pochhammer_eq_0_iff: "pochhammer a n = (0::'a::field_char_0) \<longleftrightarrow> (\<exists>k < n. a = - of_nat k)"
lp15@59667
   508
  apply (auto simp add: pochhammer_of_nat_eq_0_iff)
lp15@59667
   509
  apply (cases n)
lp15@59667
   510
   apply (auto simp add: pochhammer_def algebra_simps group_add_class.eq_neg_iff_add_eq_0)
lp15@59667
   511
  apply (metis leD not_less_eq)
lp15@59667
   512
  done
lp15@59667
   513
lp15@59667
   514
lemma pochhammer_eq_0_mono:
lp15@59667
   515
  "pochhammer a n = (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"
lp15@59667
   516
  unfolding pochhammer_eq_0_iff by auto
lp15@59667
   517
lp15@59667
   518
lemma pochhammer_neq_0_mono:
lp15@59667
   519
  "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"
lp15@59667
   520
  unfolding pochhammer_eq_0_iff by auto
lp15@59667
   521
lp15@59667
   522
lemma pochhammer_minus:
lp15@59862
   523
    "pochhammer (- b) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (b - of_nat k + 1) k"
lp15@59667
   524
proof (cases k)
lp15@59667
   525
  case 0
lp15@59667
   526
  then show ?thesis by simp
lp15@59667
   527
next
lp15@59667
   528
  case (Suc h)
lp15@59667
   529
  have eq: "((- 1) ^ Suc h :: 'a) = (\<Prod>i=0..h. - 1)"
lp15@59667
   530
    using setprod_constant[where A="{0 .. h}" and y="- 1 :: 'a"]
lp15@59667
   531
    by auto
lp15@59667
   532
  show ?thesis
lp15@59667
   533
    unfolding Suc pochhammer_Suc_setprod eq setprod.distrib[symmetric]
lp15@59667
   534
    by (rule setprod.reindex_bij_witness[where i="op - h" and j="op - h"])
lp15@59667
   535
       (auto simp: of_nat_diff)
lp15@59667
   536
qed
lp15@59667
   537
lp15@59667
   538
lemma pochhammer_minus':
lp15@59862
   539
    "pochhammer (b - of_nat k + 1) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (- b) k"
lp15@59862
   540
  unfolding pochhammer_minus[where b=b]
lp15@59667
   541
  unfolding mult.assoc[symmetric]
lp15@59667
   542
  unfolding power_add[symmetric]
lp15@59667
   543
  by simp
lp15@59667
   544
lp15@59667
   545
lemma pochhammer_same: "pochhammer (- of_nat n) n =
lp15@59730
   546
    ((- 1) ^ n :: 'a::{semiring_char_0,comm_ring_1,semiring_no_zero_divisors}) * (fact n)"
lp15@59862
   547
  unfolding pochhammer_minus
lp15@59667
   548
  by (simp add: of_nat_diff pochhammer_fact)
lp15@59667
   549
lp15@59667
   550
wenzelm@60758
   551
subsection\<open>Generalized binomial coefficients\<close>
lp15@59667
   552
lp15@59667
   553
definition gbinomial :: "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
lp15@59667
   554
  where "a gchoose n =
lp15@59730
   555
    (if n = 0 then 1 else (setprod (\<lambda>i. a - of_nat i) {0 .. n - 1}) / (fact n))"
lp15@59667
   556
lp15@59667
   557
lemma gbinomial_0 [simp]: "a gchoose 0 = 1" "0 gchoose (Suc n) = 0"
haftmann@59867
   558
  by (simp_all add: gbinomial_def)
lp15@59667
   559
lp15@59730
   560
lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / (fact n)"
lp15@59667
   561
proof (cases "n = 0")
lp15@59667
   562
  case True
lp15@59667
   563
  then show ?thesis by simp
lp15@59667
   564
next
lp15@59667
   565
  case False
lp15@59667
   566
  from this setprod_constant[of "{0 .. n - 1}" "- (1:: 'a)"]
wenzelm@61076
   567
  have eq: "(- (1::'a)) ^ n = setprod (\<lambda>i. - 1) {0 .. n - 1}"
lp15@59667
   568
    by auto
lp15@59667
   569
  from False show ?thesis
lp15@59667
   570
    by (simp add: pochhammer_def gbinomial_def field_simps
lp15@59667
   571
      eq setprod.distrib[symmetric])
lp15@59667
   572
qed
lp15@59667
   573
lp15@59730
   574
lemma binomial_gbinomial: 
lp15@59730
   575
    "of_nat (n choose k) = (of_nat n gchoose k :: 'a::field_char_0)"
lp15@59667
   576
proof -
lp15@59667
   577
  { assume kn: "k > n"
lp15@59667
   578
    then have ?thesis
lp15@59667
   579
      by (subst binomial_eq_0[OF kn])
lp15@59667
   580
         (simp add: gbinomial_pochhammer field_simps  pochhammer_of_nat_eq_0_iff) }
lp15@59667
   581
  moreover
lp15@59667
   582
  { assume "k=0" then have ?thesis by simp }
lp15@59667
   583
  moreover
lp15@59667
   584
  { assume kn: "k \<le> n" and k0: "k\<noteq> 0"
lp15@59667
   585
    from k0 obtain h where h: "k = Suc h" by (cases k) auto
lp15@59667
   586
    from h
lp15@59667
   587
    have eq:"(- 1 :: 'a) ^ k = setprod (\<lambda>i. - 1) {0..h}"
lp15@59667
   588
      by (subst setprod_constant) auto
lp15@59667
   589
    have eq': "(\<Prod>i\<in>{0..h}. of_nat n + - (of_nat i :: 'a)) = (\<Prod>i\<in>{n - h..n}. of_nat i)"
lp15@59667
   590
        using h kn
lp15@59667
   591
      by (intro setprod.reindex_bij_witness[where i="op - n" and j="op - n"])
lp15@59667
   592
         (auto simp: of_nat_diff)
lp15@59667
   593
    have th0: "finite {1..n - Suc h}" "finite {n - h .. n}"
lp15@59667
   594
        "{1..n - Suc h} \<inter> {n - h .. n} = {}" and
lp15@59667
   595
        eq3: "{1..n - Suc h} \<union> {n - h .. n} = {1..n}"
lp15@59667
   596
      using h kn by auto
lp15@59667
   597
    from eq[symmetric]
lp15@59667
   598
    have ?thesis using kn
lp15@59667
   599
      apply (simp add: binomial_fact[OF kn, where ?'a = 'a]
lp15@59667
   600
        gbinomial_pochhammer field_simps pochhammer_Suc_setprod)
lp15@59730
   601
      apply (simp add: pochhammer_Suc_setprod fact_altdef h 
lp15@59667
   602
        of_nat_setprod setprod.distrib[symmetric] eq' del: One_nat_def power_Suc)
lp15@59667
   603
      unfolding setprod.union_disjoint[OF th0, unfolded eq3, of "of_nat:: nat \<Rightarrow> 'a"] eq[unfolded h]
lp15@59730
   604
      unfolding mult.assoc
lp15@59667
   605
      unfolding setprod.distrib[symmetric]
lp15@59667
   606
      apply simp
lp15@59667
   607
      apply (intro setprod.reindex_bij_witness[where i="op - n" and j="op - n"])
lp15@59667
   608
      apply (auto simp: of_nat_diff)
lp15@59667
   609
      done
lp15@59667
   610
  }
lp15@59667
   611
  moreover
lp15@59667
   612
  have "k > n \<or> k = 0 \<or> (k \<le> n \<and> k \<noteq> 0)" by arith
lp15@59667
   613
  ultimately show ?thesis by blast
lp15@59667
   614
qed
lp15@59667
   615
lp15@59667
   616
lemma gbinomial_1[simp]: "a gchoose 1 = a"
lp15@59667
   617
  by (simp add: gbinomial_def)
lp15@59667
   618
lp15@59667
   619
lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"
lp15@59667
   620
  by (simp add: gbinomial_def)
lp15@59667
   621
lp15@59667
   622
lemma gbinomial_mult_1:
lp15@59730
   623
  fixes a :: "'a :: field_char_0"
lp15@59730
   624
  shows "a * (a gchoose n) =
lp15@59667
   625
    of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"  (is "?l = ?r")
lp15@59667
   626
proof -
lp15@59730
   627
  have "?r = ((- 1) ^n * pochhammer (- a) n / (fact n)) * (of_nat n - (- a + of_nat n))"
lp15@59667
   628
    unfolding gbinomial_pochhammer
lp15@59730
   629
      pochhammer_Suc of_nat_mult right_diff_distrib power_Suc
lp15@59730
   630
    apply (simp del: of_nat_Suc fact.simps)
lp15@59730
   631
    apply (auto simp add: field_simps simp del: of_nat_Suc)
lp15@59730
   632
    done
lp15@59667
   633
  also have "\<dots> = ?l" unfolding gbinomial_pochhammer
lp15@59667
   634
    by (simp add: field_simps)
lp15@59667
   635
  finally show ?thesis ..
lp15@59667
   636
qed
lp15@59667
   637
lp15@59667
   638
lemma gbinomial_mult_1':
lp15@59730
   639
  fixes a :: "'a :: field_char_0"
lp15@59730
   640
  shows "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
lp15@59667
   641
  by (simp add: mult.commute gbinomial_mult_1)
lp15@59667
   642
lp15@59667
   643
lemma gbinomial_Suc:
lp15@59730
   644
    "a gchoose (Suc k) = (setprod (\<lambda>i. a - of_nat i) {0 .. k}) / (fact (Suc k))"
lp15@59667
   645
  by (simp add: gbinomial_def)
lp15@59667
   646
lp15@59667
   647
lemma gbinomial_mult_fact:
lp15@59730
   648
  fixes a :: "'a::field_char_0"
lp15@59730
   649
  shows
lp15@59730
   650
   "fact (Suc k) * (a gchoose (Suc k)) =
lp15@59667
   651
    (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
lp15@59730
   652
  by (simp_all add: gbinomial_Suc field_simps del: fact.simps)
lp15@59667
   653
lp15@59667
   654
lemma gbinomial_mult_fact':
lp15@59730
   655
  fixes a :: "'a::field_char_0"
lp15@59730
   656
  shows "(a gchoose (Suc k)) * fact (Suc k) = (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
lp15@59667
   657
  using gbinomial_mult_fact[of k a]
lp15@59667
   658
  by (subst mult.commute)
lp15@59667
   659
lp15@59667
   660
lemma gbinomial_Suc_Suc:
lp15@59730
   661
  fixes a :: "'a :: field_char_0"
lp15@59730
   662
  shows "(a + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"
lp15@59667
   663
proof (cases k)
lp15@59667
   664
  case 0
lp15@59667
   665
  then show ?thesis by simp
lp15@59667
   666
next
lp15@59667
   667
  case (Suc h)
lp15@59667
   668
  have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)"
lp15@59667
   669
    apply (rule setprod.reindex_cong [where l = Suc])
lp15@59667
   670
      using Suc
lp15@59667
   671
      apply auto
lp15@59667
   672
    done
lp15@59730
   673
  have "fact (Suc k) * (a gchoose k + (a gchoose (Suc k))) =
lp15@59730
   674
        (a gchoose Suc h) * (fact (Suc (Suc h))) +
lp15@59730
   675
        (a gchoose Suc (Suc h)) * (fact (Suc (Suc h)))"
lp15@59730
   676
    by (simp add: Suc field_simps del: fact.simps)
lp15@59730
   677
  also have "... = (a gchoose Suc h) * of_nat (Suc (Suc h) * fact (Suc h)) + 
lp15@59730
   678
                   (\<Prod>i = 0..Suc h. a - of_nat i)"
lp15@59730
   679
    by (metis fact.simps(2) gbinomial_mult_fact' of_nat_fact of_nat_id)
lp15@59730
   680
  also have "... = (fact (Suc h) * (a gchoose Suc h)) * of_nat (Suc (Suc h)) + 
lp15@59730
   681
                   (\<Prod>i = 0..Suc h. a - of_nat i)"
lp15@59730
   682
    by (simp only: fact.simps(2) mult.commute mult.left_commute of_nat_fact of_nat_id of_nat_mult)
lp15@59730
   683
  also have "... =  of_nat (Suc (Suc h)) * (\<Prod>i = 0..h. a - of_nat i) + 
lp15@59730
   684
                    (\<Prod>i = 0..Suc h. a - of_nat i)"
lp15@59730
   685
    by (metis gbinomial_mult_fact mult.commute)
lp15@59730
   686
  also have "... = (\<Prod>i = 0..Suc h. a - of_nat i) +
lp15@59730
   687
                   (of_nat h * (\<Prod>i = 0..h. a - of_nat i) + 2 * (\<Prod>i = 0..h. a - of_nat i))"
lp15@59730
   688
    by (simp add: field_simps)
lp15@59730
   689
  also have "... = 
wenzelm@61076
   690
    ((a gchoose Suc h) * (fact (Suc h)) * of_nat (Suc k)) + (\<Prod>i\<in>{0::nat..Suc h}. a - of_nat i)"
lp15@59667
   691
    unfolding gbinomial_mult_fact'
lp15@59730
   692
    by (simp add: comm_semiring_class.distrib field_simps Suc)
lp15@59667
   693
  also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
lp15@59667
   694
    unfolding gbinomial_mult_fact' setprod_nat_ivl_Suc
lp15@59667
   695
    by (simp add: field_simps Suc)
lp15@59667
   696
  also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
lp15@59667
   697
    using eq0
lp15@59667
   698
    by (simp add: Suc setprod_nat_ivl_1_Suc)
lp15@59730
   699
  also have "\<dots> = (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
lp15@59667
   700
    unfolding gbinomial_mult_fact ..
lp15@59730
   701
  finally show ?thesis
lp15@59730
   702
    by (metis fact_nonzero mult_cancel_left) 
lp15@59667
   703
qed
lp15@59667
   704
lp15@59667
   705
lemma gbinomial_reduce_nat:
lp15@59730
   706
  fixes a :: "'a :: field_char_0"
lp15@59730
   707
  shows "0 < k \<Longrightarrow> a gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)"
lp15@59730
   708
  by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc)
lp15@59667
   709
lp15@60141
   710
lemma gchoose_row_sum_weighted:
lp15@60141
   711
  fixes r :: "'a::field_char_0"
lp15@60141
   712
  shows "(\<Sum>k = 0..m. (r gchoose k) * (r/2 - of_nat k)) = of_nat(Suc m) / 2 * (r gchoose (Suc m))"
lp15@60141
   713
proof (induct m)
lp15@60141
   714
  case 0 show ?case by simp
lp15@60141
   715
next
lp15@60141
   716
  case (Suc m)
lp15@60141
   717
  from Suc show ?case
lp15@60141
   718
    by (simp add: algebra_simps distrib gbinomial_mult_1)
lp15@60141
   719
qed
lp15@59667
   720
lp15@59667
   721
lemma binomial_symmetric:
lp15@59667
   722
  assumes kn: "k \<le> n"
lp15@59667
   723
  shows "n choose k = n choose (n - k)"
lp15@59667
   724
proof-
lp15@59667
   725
  from kn have kn': "n - k \<le> n" by arith
lp15@59667
   726
  from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']
lp15@59667
   727
  have "fact k * fact (n - k) * (n choose k) =
lp15@59667
   728
    fact (n - k) * fact (n - (n - k)) * (n choose (n - k))" by simp
lp15@59667
   729
  then show ?thesis using kn by simp
lp15@59667
   730
qed
lp15@59667
   731
wenzelm@60758
   732
text\<open>Contributed by Manuel Eberl, generalised by LCP.
wenzelm@60758
   733
  Alternative definition of the binomial coefficient as @{term "\<Prod>i<k. (n - i) / (k - i)"}\<close>
lp15@59667
   734
lemma gbinomial_altdef_of_nat:
lp15@59667
   735
  fixes k :: nat
haftmann@59867
   736
    and x :: "'a :: {field_char_0,field}"
lp15@59667
   737
  shows "x gchoose k = (\<Prod>i<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
lp15@59667
   738
proof -
lp15@59667
   739
  have "(x gchoose k) = (\<Prod>i<k. x - of_nat i) / of_nat (fact k)"
lp15@59667
   740
    unfolding gbinomial_def
lp15@59667
   741
    by (auto simp: gr0_conv_Suc lessThan_Suc_atMost atLeast0AtMost)
lp15@59667
   742
  also have "\<dots> = (\<Prod>i<k. (x - of_nat i) / of_nat (k - i) :: 'a)"
lp15@59667
   743
    unfolding fact_eq_rev_setprod_nat of_nat_setprod
lp15@59667
   744
    by (auto simp add: setprod_dividef intro!: setprod.cong of_nat_diff[symmetric])
lp15@59667
   745
  finally show ?thesis .
lp15@59667
   746
qed
lp15@59667
   747
lp15@59667
   748
lemma gbinomial_ge_n_over_k_pow_k:
lp15@59667
   749
  fixes k :: nat
haftmann@59867
   750
    and x :: "'a :: linordered_field"
lp15@59667
   751
  assumes "of_nat k \<le> x"
lp15@59667
   752
  shows "(x / of_nat k :: 'a) ^ k \<le> x gchoose k"
lp15@59667
   753
proof -
lp15@59667
   754
  have x: "0 \<le> x"
lp15@59667
   755
    using assms of_nat_0_le_iff order_trans by blast
lp15@59667
   756
  have "(x / of_nat k :: 'a) ^ k = (\<Prod>i<k. x / of_nat k :: 'a)"
lp15@59667
   757
    by (simp add: setprod_constant)
lp15@59667
   758
  also have "\<dots> \<le> x gchoose k"
lp15@59667
   759
    unfolding gbinomial_altdef_of_nat
lp15@59667
   760
  proof (safe intro!: setprod_mono)
lp15@59667
   761
    fix i :: nat
lp15@59667
   762
    assume ik: "i < k"
lp15@59667
   763
    from assms have "x * of_nat i \<ge> of_nat (i * k)"
lp15@59667
   764
      by (metis mult.commute mult_le_cancel_right of_nat_less_0_iff of_nat_mult)
lp15@59667
   765
    then have "x * of_nat k - x * of_nat i \<le> x * of_nat k - of_nat (i * k)" by arith
lp15@59667
   766
    then have "x * of_nat (k - i) \<le> (x - of_nat i) * of_nat k"
lp15@59667
   767
      using ik
lp15@59667
   768
      by (simp add: algebra_simps zero_less_mult_iff of_nat_diff of_nat_mult)
lp15@59667
   769
    then have "x * of_nat (k - i) \<le> (x - of_nat i) * (of_nat k :: 'a)"
lp15@59667
   770
      unfolding of_nat_mult[symmetric] of_nat_le_iff .
lp15@59667
   771
    with assms show "x / of_nat k \<le> (x - of_nat i) / (of_nat (k - i) :: 'a)"
wenzelm@60758
   772
      using \<open>i < k\<close> by (simp add: field_simps)
lp15@59667
   773
  qed (simp add: x zero_le_divide_iff)
lp15@59667
   774
  finally show ?thesis .
lp15@59667
   775
qed
lp15@59667
   776
wenzelm@60758
   777
text\<open>Versions of the theorems above for the natural-number version of "choose"\<close>
lp15@59667
   778
lemma binomial_altdef_of_nat:
lp15@59667
   779
  fixes n k :: nat
wenzelm@60758
   780
    and x :: "'a :: {field_char_0,field}"  --\<open>the point is to constrain @{typ 'a}\<close>
lp15@59667
   781
  assumes "k \<le> n"
lp15@59667
   782
  shows "of_nat (n choose k) = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
lp15@59667
   783
using assms
lp15@59667
   784
by (simp add: gbinomial_altdef_of_nat binomial_gbinomial of_nat_diff)
lp15@59667
   785
lp15@59667
   786
lemma binomial_ge_n_over_k_pow_k:
lp15@59667
   787
  fixes k n :: nat
haftmann@59867
   788
    and x :: "'a :: linordered_field"
lp15@59667
   789
  assumes "k \<le> n"
lp15@59667
   790
  shows "(of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)"
lp15@59667
   791
by (simp add: assms gbinomial_ge_n_over_k_pow_k binomial_gbinomial of_nat_diff)
lp15@59667
   792
lp15@59667
   793
lemma binomial_le_pow:
lp15@59667
   794
  assumes "r \<le> n"
lp15@59667
   795
  shows "n choose r \<le> n ^ r"
lp15@59667
   796
proof -
lp15@59667
   797
  have "n choose r \<le> fact n div fact (n - r)"
wenzelm@60758
   798
    using \<open>r \<le> n\<close> by (subst binomial_fact_lemma[symmetric]) auto
lp15@59667
   799
  with fact_div_fact_le_pow [OF assms] show ?thesis by auto
lp15@59667
   800
qed
lp15@59667
   801
lp15@59667
   802
lemma binomial_altdef_nat: "(k::nat) \<le> n \<Longrightarrow>
lp15@59667
   803
    n choose k = fact n div (fact k * fact (n - k))"
lp15@59667
   804
 by (subst binomial_fact_lemma [symmetric]) auto
lp15@59667
   805
lp15@59730
   806
lemma choose_dvd: "k \<le> n \<Longrightarrow> fact k * fact (n - k) dvd (fact n :: 'a :: {semiring_div,linordered_semidom})"
lp15@59730
   807
  unfolding dvd_def
lp15@59730
   808
  apply (rule exI [where x="of_nat (n choose k)"])
lp15@59730
   809
  using binomial_fact_lemma [of k n, THEN arg_cong [where f = of_nat and 'b='a]]
lp15@59730
   810
  apply (auto simp: of_nat_mult)
lp15@59667
   811
  done
lp15@59667
   812
lp15@59730
   813
lemma fact_fact_dvd_fact: 
lp15@59730
   814
    "fact k * fact n dvd (fact (k+n) :: 'a :: {semiring_div,linordered_semidom})"
lp15@59730
   815
by (metis add.commute add_diff_cancel_left' choose_dvd le_add2)
lp15@59667
   816
lp15@59667
   817
lemma choose_mult_lemma:
lp15@59667
   818
     "((m+r+k) choose (m+k)) * ((m+k) choose k) = ((m+r+k) choose k) * ((m+r) choose m)"
lp15@59667
   819
proof -
lp15@59667
   820
  have "((m+r+k) choose (m+k)) * ((m+k) choose k) =
lp15@59667
   821
        fact (m+r + k) div (fact (m + k) * fact (m+r - m)) * (fact (m + k) div (fact k * fact m))"
lp15@59667
   822
    by (simp add: assms binomial_altdef_nat)
lp15@59667
   823
  also have "... = fact (m+r+k) div (fact r * (fact k * fact m))"
lp15@59667
   824
    apply (subst div_mult_div_if_dvd)
lp15@59730
   825
    apply (auto simp: algebra_simps fact_fact_dvd_fact)
lp15@59667
   826
    apply (metis add.assoc add.commute fact_fact_dvd_fact)
lp15@59667
   827
    done
lp15@59667
   828
  also have "... = (fact (m+r+k) * fact (m+r)) div (fact r * (fact k * fact m) * fact (m+r))"
lp15@59667
   829
    apply (subst div_mult_div_if_dvd [symmetric])
lp15@59730
   830
    apply (auto simp add: algebra_simps)
lp15@59730
   831
    apply (metis fact_fact_dvd_fact dvd.order.trans nat_mult_dvd_cancel_disj)
lp15@59667
   832
    done
lp15@59667
   833
  also have "... = (fact (m+r+k) div (fact k * fact (m+r)) * (fact (m+r) div (fact r * fact m)))"
lp15@59667
   834
    apply (subst div_mult_div_if_dvd)
lp15@59730
   835
    apply (auto simp: fact_fact_dvd_fact algebra_simps)
lp15@59667
   836
    done
lp15@59667
   837
  finally show ?thesis
lp15@59667
   838
    by (simp add: binomial_altdef_nat mult.commute)
lp15@59667
   839
qed
lp15@59667
   840
wenzelm@60758
   841
text\<open>The "Subset of a Subset" identity\<close>
lp15@59667
   842
lemma choose_mult:
lp15@59667
   843
  assumes "k\<le>m" "m\<le>n"
lp15@59667
   844
    shows "(n choose m) * (m choose k) = (n choose k) * ((n-k) choose (m-k))"
lp15@59667
   845
using assms choose_mult_lemma [of "m-k" "n-m" k]
lp15@59667
   846
by simp
lp15@59667
   847
lp15@59667
   848
wenzelm@60758
   849
subsection \<open>Binomial coefficients\<close>
lp15@59667
   850
lp15@59667
   851
lemma choose_one: "(n::nat) choose 1 = n"
lp15@59667
   852
  by simp
lp15@59667
   853
lp15@59667
   854
(*FIXME: messy and apparently unused*)
lp15@59667
   855
lemma binomial_induct [rule_format]: "(ALL (n::nat). P n n) \<longrightarrow>
lp15@59667
   856
    (ALL n. P (Suc n) 0) \<longrightarrow> (ALL n. (ALL k < n. P n k \<longrightarrow> P n (Suc k) \<longrightarrow>
lp15@59667
   857
    P (Suc n) (Suc k))) \<longrightarrow> (ALL k <= n. P n k)"
lp15@59667
   858
  apply (induct n)
lp15@59667
   859
  apply auto
lp15@59667
   860
  apply (case_tac "k = 0")
lp15@59667
   861
  apply auto
lp15@59667
   862
  apply (case_tac "k = Suc n")
lp15@59667
   863
  apply auto
lp15@59730
   864
  apply (metis Suc_le_eq fact.cases le_Suc_eq le_eq_less_or_eq)
lp15@59667
   865
  done
lp15@59667
   866
lp15@59667
   867
lemma card_UNION:
lp15@59667
   868
  assumes "finite A" and "\<forall>k \<in> A. finite k"
lp15@59667
   869
  shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))"
lp15@59667
   870
  (is "?lhs = ?rhs")
lp15@59667
   871
proof -
lp15@59667
   872
  have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))" by simp
lp15@59667
   873
  also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))" (is "_ = nat ?rhs")
lp15@59667
   874
    by(subst setsum_right_distrib) simp
lp15@59667
   875
  also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))"
lp15@59667
   876
    using assms by(subst setsum.Sigma)(auto)
lp15@59667
   877
  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
   878
    by (rule setsum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)
lp15@59667
   879
  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))"
lp15@59667
   880
    using assms by(auto intro!: setsum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])
lp15@59667
   881
  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)))"
lp15@59667
   882
    using assms by(subst setsum.Sigma) auto
lp15@59667
   883
  also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "setsum ?lhs _ = _")
lp15@59667
   884
  proof(rule setsum.cong[OF refl])
lp15@59667
   885
    fix x
lp15@59667
   886
    assume x: "x \<in> \<Union>A"
lp15@59667
   887
    def K \<equiv> "{X \<in> A. x \<in> X}"
wenzelm@60758
   888
    with \<open>finite A\<close> have K: "finite K" by auto
lp15@59667
   889
    let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I}"
lp15@59667
   890
    have "inj_on snd (SIGMA i:{1..card A}. ?I i)"
lp15@59667
   891
      using assms by(auto intro!: inj_onI)
lp15@59667
   892
    moreover have [symmetric]: "snd ` (SIGMA i:{1..card A}. ?I i) = {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}"
lp15@59667
   893
      using assms by(auto intro!: rev_image_eqI[where x="(card a, a)" for a]
lp15@59667
   894
        simp add: card_gt_0_iff[folded Suc_le_eq]
lp15@59667
   895
        dest: finite_subset intro: card_mono)
lp15@59667
   896
    ultimately have "?lhs x = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))"
lp15@59667
   897
      by (rule setsum.reindex_cong [where l = snd]) fastforce
lp15@59667
   898
    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)))"
lp15@59667
   899
      using assms by(subst setsum.Sigma) auto
lp15@59667
   900
    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))"
lp15@59667
   901
      by(subst setsum_right_distrib) simp
lp15@59667
   902
    also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))" (is "_ = ?rhs")
lp15@59667
   903
    proof(rule setsum.mono_neutral_cong_right[rule_format])
wenzelm@60758
   904
      show "{1..card K} \<subseteq> {1..card A}" using \<open>finite A\<close>
lp15@59667
   905
        by(auto simp add: K_def intro: card_mono)
lp15@59667
   906
    next
lp15@59667
   907
      fix i
lp15@59667
   908
      assume "i \<in> {1..card A} - {1..card K}"
lp15@59667
   909
      hence i: "i \<le> card A" "card K < i" by auto
lp15@59667
   910
      have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}"
lp15@59667
   911
        by(auto simp add: K_def)
wenzelm@60758
   912
      also have "\<dots> = {}" using \<open>finite A\<close> i
lp15@59667
   913
        by(auto simp add: K_def dest: card_mono[rotated 1])
lp15@59667
   914
      finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0"
lp15@59667
   915
        by(simp only:) simp
lp15@59667
   916
    next
lp15@59667
   917
      fix i
lp15@59667
   918
      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
   919
        (is "?lhs = ?rhs")
lp15@59667
   920
        by(rule setsum.cong)(auto simp add: K_def)
lp15@59667
   921
      thus "(- 1) ^ (i + 1) * ?lhs = (- 1) ^ (i + 1) * ?rhs" by simp
lp15@59667
   922
    qed simp
lp15@59667
   923
    also have "{I. I \<subseteq> K \<and> card I = 0} = {{}}" using assms
lp15@59667
   924
      by(auto simp add: card_eq_0_iff K_def dest: finite_subset)
lp15@59667
   925
    hence "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1"
lp15@59667
   926
      by(subst (2) setsum_head_Suc)(simp_all )
lp15@59667
   927
    also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1"
lp15@59667
   928
      using K by(subst n_subsets[symmetric]) simp_all
lp15@59667
   929
    also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1"
lp15@59667
   930
      by(subst setsum_right_distrib[symmetric]) simp
lp15@59667
   931
    also have "\<dots> =  - ((-1 + 1) ^ card K) + 1"
lp15@59667
   932
      by(subst binomial_ring)(simp add: ac_simps)
lp15@59667
   933
    also have "\<dots> = 1" using x K by(auto simp add: K_def card_gt_0_iff)
lp15@59667
   934
    finally show "?lhs x = 1" .
lp15@59667
   935
  qed
lp15@59667
   936
  also have "nat \<dots> = card (\<Union>A)" by simp
lp15@59667
   937
  finally show ?thesis ..
lp15@59667
   938
qed
lp15@59667
   939
wenzelm@60758
   940
text\<open>The number of nat lists of length @{text m} summing to @{text N} is
wenzelm@60758
   941
@{term "(N + m - 1) choose N"}:\<close>
lp15@59667
   942
lp15@59667
   943
lemma card_length_listsum_rec:
lp15@59667
   944
  assumes "m\<ge>1"
lp15@59667
   945
  shows "card {l::nat list. length l = m \<and> listsum l = N} =
lp15@59667
   946
    (card {l. length l = (m - 1) \<and> listsum l = N} +
lp15@59667
   947
    card {l. length l = m \<and> listsum l + 1 =  N})"
lp15@59667
   948
    (is "card ?C = (card ?A + card ?B)")
lp15@59667
   949
proof -
lp15@59667
   950
  let ?A'="{l. length l = m \<and> listsum l = N \<and> hd l = 0}"
lp15@59667
   951
  let ?B'="{l. length l = m \<and> listsum l = N \<and> hd l \<noteq> 0}"
lp15@59667
   952
  let ?f ="\<lambda> l. 0#l"
lp15@59667
   953
  let ?g ="\<lambda> l. (hd l + 1) # tl l"
lp15@59667
   954
  have 1: "\<And>xs x. xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" by simp
lp15@59667
   955
  have 2: "\<And>xs. (xs::nat list) \<noteq> [] \<Longrightarrow> listsum(tl xs) = listsum xs - hd xs"
lp15@59667
   956
    by(auto simp add: neq_Nil_conv)
lp15@59667
   957
  have f: "bij_betw ?f ?A ?A'"
lp15@59667
   958
    apply(rule bij_betw_byWitness[where f' = tl])
lp15@59667
   959
    using assms
lp15@59667
   960
    by (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv)
lp15@59667
   961
  have 3: "\<And>xs:: nat list. xs \<noteq> [] \<Longrightarrow> hd xs + (listsum xs - hd xs) = listsum xs"
lp15@59667
   962
    by (metis 1 listsum_simps(2) 2)
lp15@59667
   963
  have g: "bij_betw ?g ?B ?B'"
lp15@59667
   964
    apply(rule bij_betw_byWitness[where f' = "\<lambda> l. (hd l - 1) # tl l"])
lp15@59667
   965
    using assms
lp15@59667
   966
    by (auto simp: 2 length_0_conv[symmetric] intro!: 3
lp15@59667
   967
      simp del: length_greater_0_conv length_0_conv)
lp15@59667
   968
  { fix M N :: nat have "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}"
lp15@59667
   969
    using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto }
lp15@59667
   970
    note fin = this
lp15@59667
   971
  have fin_A: "finite ?A" using fin[of _ "N+1"]
lp15@59667
   972
    by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"],
lp15@59667
   973
      auto simp: member_le_listsum_nat less_Suc_eq_le)
lp15@59667
   974
  have fin_B: "finite ?B"
lp15@59667
   975
    by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"],
lp15@59667
   976
      auto simp: member_le_listsum_nat less_Suc_eq_le fin)
lp15@59667
   977
  have uni: "?C = ?A' \<union> ?B'" by auto
lp15@59667
   978
  have disj: "?A' \<inter> ?B' = {}" by auto
lp15@59667
   979
  have "card ?C = card(?A' \<union> ?B')" using uni by simp
lp15@59667
   980
  also have "\<dots> = card ?A + card ?B"
lp15@59667
   981
    using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g]
lp15@59667
   982
      bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B
lp15@59667
   983
    by presburger
lp15@59667
   984
  finally show ?thesis .
lp15@59667
   985
qed
lp15@59667
   986
lp15@59667
   987
lemma card_length_listsum: --"By Holden Lee, tidied by Tobias Nipkow"
lp15@59667
   988
  "card {l::nat list. size l = m \<and> listsum l = N} = (N + m - 1) choose N"
lp15@59667
   989
proof (cases m)
lp15@59667
   990
  case 0 then show ?thesis
lp15@59667
   991
    by (cases N) (auto simp: cong: conj_cong)
lp15@59667
   992
next
lp15@59667
   993
  case (Suc m')
lp15@59667
   994
    have m: "m\<ge>1" by (simp add: Suc)
lp15@59667
   995
    then show ?thesis
lp15@59667
   996
    proof (induct "N + m - 1" arbitrary: N m)
lp15@59667
   997
      case 0   -- "In the base case, the only solution is [0]."
lp15@59667
   998
      have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {[0]}"
lp15@59667
   999
        by (auto simp: length_Suc_conv)
lp15@59667
  1000
      have "m=1 \<and> N=0" using 0 by linarith
lp15@59667
  1001
      then show ?case by simp
lp15@59667
  1002
    next
lp15@59667
  1003
      case (Suc k)
lp15@59667
  1004
lp15@59667
  1005
      have c1: "card {l::nat list. size l = (m - 1) \<and> listsum l =  N} =
lp15@59667
  1006
        (N + (m - 1) - 1) choose N"
lp15@59667
  1007
      proof cases
lp15@59667
  1008
        assume "m = 1"
lp15@59667
  1009
        with Suc.hyps have "N\<ge>1" by auto
wenzelm@60758
  1010
        with \<open>m = 1\<close> show ?thesis by (simp add: binomial_eq_0)
lp15@59667
  1011
      next
lp15@59667
  1012
        assume "m \<noteq> 1" thus ?thesis using Suc by fastforce
lp15@59667
  1013
      qed
lp15@59667
  1014
lp15@59667
  1015
      from Suc have c2: "card {l::nat list. size l = m \<and> listsum l + 1 = N} =
lp15@59667
  1016
        (if N>0 then ((N - 1) + m - 1) choose (N - 1) else 0)"
lp15@59667
  1017
      proof -
lp15@59667
  1018
        have aux: "\<And>m n. n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" by arith
lp15@59667
  1019
        from Suc have "N>0 \<Longrightarrow>
lp15@59667
  1020
          card {l::nat list. size l = m \<and> listsum l + 1 = N} =
lp15@59667
  1021
          ((N - 1) + m - 1) choose (N - 1)" by (simp add: aux)
lp15@59667
  1022
        thus ?thesis by auto
lp15@59667
  1023
      qed
lp15@59667
  1024
lp15@59667
  1025
      from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> listsum l = N} +
lp15@59667
  1026
          card {l::nat list. size l = m \<and> listsum l + 1 = N}) = (N + m - 1) choose N"
lp15@59667
  1027
        by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def)
lp15@59667
  1028
      thus ?case using card_length_listsum_rec[OF Suc.prems] by auto
lp15@59667
  1029
    qed
lp15@59667
  1030
qed
lp15@59667
  1031
hoelzl@60604
  1032
hoelzl@60604
  1033
lemma Suc_times_binomial_add: -- \<open>by Lukas Bulwahn\<close>
hoelzl@60604
  1034
  "Suc a * (Suc (a + b) choose Suc a) = Suc b * (Suc (a + b) choose a)"
hoelzl@60604
  1035
proof -
hoelzl@60604
  1036
  have dvd: "Suc a * (fact a * fact b) dvd fact (Suc (a + b))" for a b
hoelzl@60604
  1037
    using fact_fact_dvd_fact[of "Suc a" "b", where 'a=nat]
hoelzl@60604
  1038
    by (simp only: fact_Suc add_Suc[symmetric] of_nat_id mult.assoc)
hoelzl@60604
  1039
hoelzl@60604
  1040
  have "Suc a * (fact (Suc (a + b)) div (Suc a * fact a * fact b)) =
hoelzl@60604
  1041
      Suc a * fact (Suc (a + b)) div (Suc a * (fact a * fact b))"
hoelzl@60604
  1042
    by (subst div_mult_swap[symmetric]; simp only: mult.assoc dvd)
hoelzl@60604
  1043
  also have "\<dots> = Suc b * fact (Suc (a + b)) div (Suc b * (fact a * fact b))"
hoelzl@60604
  1044
    by (simp only: div_mult_mult1)
hoelzl@60604
  1045
  also have "\<dots> = Suc b * (fact (Suc (a + b)) div (Suc b * (fact a * fact b)))"
hoelzl@60604
  1046
    using dvd[of b a] by (subst div_mult_swap[symmetric]; simp only: ac_simps dvd)
hoelzl@60604
  1047
  finally show ?thesis
hoelzl@60604
  1048
    by (subst (1 2) binomial_altdef_nat)
hoelzl@60604
  1049
       (simp_all only: ac_simps diff_Suc_Suc Suc_diff_le diff_add_inverse fact_Suc of_nat_id)
hoelzl@60604
  1050
qed
hoelzl@60604
  1051
nipkow@15131
  1052
end