src/HOL/Number_Theory/Binomial.thy
author paulson <lp15@cam.ac.uk>
Thu Oct 30 16:36:44 2014 +0000 (2014-10-30)
changeset 58833 09974789e483
parent 58713 572a5a870c84
child 58841 e16712bb1d41
permissions -rw-r--r--
choose_reduce_nat: re-ordered operands
wenzelm@41959
     1
(*  Title:      HOL/Number_Theory/Binomial.thy
nipkow@31719
     2
    Authors:    Lawrence C. Paulson, Jeremy Avigad, Tobias Nipkow
nipkow@31719
     3
avigad@32036
     4
Defines the "choose" function, and establishes basic properties.
nipkow@31719
     5
*)
nipkow@31719
     6
nipkow@31719
     7
header {* Binomial *}
nipkow@31719
     8
nipkow@31719
     9
theory Binomial
lp15@55130
    10
imports Cong Fact Complex_Main
nipkow@31719
    11
begin
nipkow@31719
    12
nipkow@31719
    13
lp15@55130
    14
text {* This development is based on the work of Andy Gordon and
lp15@55130
    15
  Florian Kammueller. *}
lp15@55130
    16
lp15@55130
    17
subsection {* Basic definitions and lemmas *}
lp15@55130
    18
lp15@55130
    19
primrec binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat" (infixl "choose" 65)
lp15@55130
    20
where
lp15@55130
    21
  "0 choose k = (if k = 0 then 1 else 0)"
lp15@55130
    22
| "Suc n choose k = (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"
lp15@55130
    23
lp15@55130
    24
lemma binomial_n_0 [simp]: "(n choose 0) = 1"
lp15@55130
    25
  by (cases n) simp_all
lp15@55130
    26
lp15@55130
    27
lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
lp15@55130
    28
  by simp
lp15@55130
    29
lp15@55130
    30
lemma binomial_Suc_Suc [simp]: "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
lp15@55130
    31
  by simp
lp15@55130
    32
lp15@55130
    33
lemma choose_reduce_nat: 
lp15@55130
    34
  "0 < (n::nat) \<Longrightarrow> 0 < k \<Longrightarrow>
lp15@58833
    35
    (n choose k) = ((n - 1) choose (k - 1)) + ((n - 1) choose k)"
lp15@58833
    36
  by (metis Suc_diff_1 binomial.simps(2) neq0_conv)
lp15@55130
    37
lp15@55130
    38
lemma binomial_eq_0: "n < k \<Longrightarrow> n choose k = 0"
lp15@55130
    39
  by (induct n arbitrary: k) auto
lp15@55130
    40
lp15@55130
    41
declare binomial.simps [simp del]
nipkow@31719
    42
lp15@55130
    43
lemma binomial_n_n [simp]: "n choose n = 1"
lp15@55130
    44
  by (induct n) (simp_all add: binomial_eq_0)
lp15@55130
    45
lp15@55130
    46
lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n"
lp15@55130
    47
  by (induct n) simp_all
lp15@55130
    48
lp15@55130
    49
lemma binomial_1 [simp]: "n choose Suc 0 = n"
lp15@55130
    50
  by (induct n) simp_all
lp15@55130
    51
lp15@55130
    52
lemma zero_less_binomial: "k \<le> n \<Longrightarrow> n choose k > 0"
lp15@55130
    53
  by (induct n k rule: diff_induct) simp_all
lp15@55130
    54
lp15@55130
    55
lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> n < k"
lp15@55130
    56
  by (metis binomial_eq_0 less_numeral_extra(3) not_less zero_less_binomial)
nipkow@31719
    57
lp15@55130
    58
lemma zero_less_binomial_iff [simp]: "n choose k > 0 \<longleftrightarrow> k \<le> n"
lp15@55130
    59
  by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial)
lp15@55130
    60
lp15@55130
    61
(*Might be more useful if re-oriented*)
lp15@55130
    62
lemma Suc_times_binomial_eq:
lp15@55130
    63
  "k \<le> n \<Longrightarrow> Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
lp15@55130
    64
  apply (induct n arbitrary: k)
lp15@55130
    65
   apply (simp add: binomial.simps)
lp15@55130
    66
   apply (case_tac k)
lp15@55130
    67
  apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0)
lp15@55130
    68
  done
lp15@55130
    69
lp15@55130
    70
text{*This is the well-known version, but it's harder to use because of the
lp15@55130
    71
  need to reason about division.*}
lp15@55130
    72
lemma binomial_Suc_Suc_eq_times:
lp15@55130
    73
    "k \<le> n \<Longrightarrow> (Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
lp15@55130
    74
  by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)
nipkow@31719
    75
lp15@55130
    76
text{*Another version, with -1 instead of Suc.*}
lp15@55130
    77
lemma times_binomial_minus1_eq:
lp15@55130
    78
  "k \<le> n \<Longrightarrow> 0 < k \<Longrightarrow> (n choose k) * k = n * ((n - 1) choose (k - 1))"
lp15@55130
    79
  using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"]
lp15@55130
    80
  by (auto split add: nat_diff_split)
lp15@55130
    81
lp15@55130
    82
lp15@55130
    83
subsection {* Combinatorial theorems involving @{text "choose"} *}
lp15@55130
    84
lp15@55130
    85
text {*By Florian Kamm\"uller, tidied by LCP.*}
lp15@55130
    86
lp15@55130
    87
lemma card_s_0_eq_empty: "finite A \<Longrightarrow> card {B. B \<subseteq> A & card B = 0} = 1"
lp15@55130
    88
  by (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
lp15@55130
    89
lp15@55130
    90
lemma choose_deconstruct: "finite M \<Longrightarrow> x \<notin> M \<Longrightarrow>
lp15@55130
    91
    {s. s \<subseteq> insert x M \<and> card s = Suc k} =
lp15@55130
    92
    {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@55130
    93
  apply safe
lp15@55130
    94
     apply (auto intro: finite_subset [THEN card_insert_disjoint])
lp15@55130
    95
  by (metis (full_types) Diff_insert_absorb Set.set_insert Zero_neq_Suc card_Diff_singleton_if 
lp15@55130
    96
     card_eq_0_iff diff_Suc_1 in_mono subset_insert_iff)
lp15@55130
    97
lp15@55130
    98
lemma finite_bex_subset [simp]:
lp15@55130
    99
  assumes "finite B"
lp15@55130
   100
    and "\<And>A. A \<subseteq> B \<Longrightarrow> finite {x. P x A}"
lp15@55130
   101
  shows "finite {x. \<exists>A \<subseteq> B. P x A}"
lp15@55130
   102
  by (metis (no_types) assms finite_Collect_bounded_ex finite_Collect_subsets)
nipkow@31719
   103
lp15@55130
   104
text{*There are as many subsets of @{term A} having cardinality @{term k}
lp15@55130
   105
 as there are sets obtained from the former by inserting a fixed element
lp15@55130
   106
 @{term x} into each.*}
lp15@55130
   107
lemma constr_bij:
lp15@55130
   108
   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow>
lp15@55130
   109
    card {B. \<exists>C. C \<subseteq> A \<and> card C = k \<and> B = insert x C} =
lp15@55130
   110
    card {B. B \<subseteq> A & card(B) = k}"
lp15@55130
   111
  apply (rule card_bij_eq [where f = "\<lambda>s. s - {x}" and g = "insert x"])
lp15@55130
   112
  apply (auto elim!: equalityE simp add: inj_on_def)
lp15@55130
   113
  apply (metis card_Diff_singleton_if finite_subset in_mono)
lp15@55130
   114
  done
lp15@55130
   115
lp15@55130
   116
text {*
lp15@55130
   117
  Main theorem: combinatorial statement about number of subsets of a set.
lp15@55130
   118
*}
lp15@55130
   119
lp15@55130
   120
theorem n_subsets: "finite A \<Longrightarrow> card {B. B \<subseteq> A \<and> card B = k} = (card A choose k)"
lp15@55130
   121
proof (induct k arbitrary: A)
lp15@55130
   122
  case 0 then show ?case by (simp add: card_s_0_eq_empty)
lp15@55130
   123
next
lp15@55130
   124
  case (Suc k)
lp15@55130
   125
  show ?case using `finite A`
lp15@55130
   126
  proof (induct A)
lp15@55130
   127
    case empty show ?case by (simp add: card_s_0_eq_empty)
lp15@55130
   128
  next
lp15@55130
   129
    case (insert x A)
lp15@55130
   130
    then show ?case using Suc.hyps
lp15@55130
   131
      apply (simp add: card_s_0_eq_empty choose_deconstruct)
lp15@55130
   132
      apply (subst card_Un_disjoint)
lp15@55130
   133
         prefer 4 apply (force simp add: constr_bij)
lp15@55130
   134
        prefer 3 apply force
lp15@55130
   135
       prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
wenzelm@55143
   136
         finite_subset [of _ "Pow (insert x F)" for F])
lp15@55130
   137
      apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
lp15@55130
   138
      done
lp15@55130
   139
  qed
lp15@55130
   140
qed
lp15@55130
   141
lp15@55130
   142
lp15@55130
   143
subsection {* The binomial theorem (courtesy of Tobias Nipkow): *}
nipkow@31719
   144
lp15@55130
   145
text{* Avigad's version, generalized to any commutative ring *}
lp15@55130
   146
theorem binomial_ring: "(a+b::'a::{comm_ring_1,power})^n = 
lp15@55130
   147
  (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))" (is "?P n")
lp15@55130
   148
proof (induct n)
lp15@55130
   149
  case 0 then show "?P 0" by simp
lp15@55130
   150
next
lp15@55130
   151
  case (Suc n)
lp15@55130
   152
  have decomp: "{0..n+1} = {0} Un {n+1} Un {1..n}"
lp15@55130
   153
    by auto
lp15@55130
   154
  have decomp2: "{0..n} = {0} Un {1..n}"
lp15@55130
   155
    by auto
lp15@55130
   156
  have "(a+b)^(n+1) = 
lp15@55130
   157
      (a+b) * (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
lp15@55130
   158
    using Suc.hyps by simp
lp15@55130
   159
  also have "\<dots> = a*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k)) +
lp15@55130
   160
                   b*(\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
lp15@55130
   161
    by (rule distrib)
lp15@55130
   162
  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^(k+1) * b^(n-k)) +
lp15@55130
   163
                  (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k+1))"
haftmann@57514
   164
    by (auto simp add: setsum_right_distrib ac_simps)
lp15@55130
   165
  also have "\<dots> = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n+1-k)) +
lp15@55130
   166
                  (\<Sum>k=1..n+1. of_nat (n choose (k - 1)) * a^k * b^(n+1-k))"
lp15@55130
   167
    by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le field_simps  
lp15@55130
   168
        del:setsum_cl_ivl_Suc)
lp15@55130
   169
  also have "\<dots> = a^(n+1) + b^(n+1) +
lp15@55130
   170
                  (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n+1-k)) +
lp15@55130
   171
                  (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n+1-k))"
lp15@55130
   172
    by (simp add: decomp2)
lp15@55130
   173
  also have
lp15@55130
   174
      "\<dots> = a^(n+1) + b^(n+1) + 
lp15@55130
   175
            (\<Sum>k=1..n. of_nat(n+1 choose k) * a^k * b^(n+1-k))"
haftmann@57418
   176
    by (auto simp add: field_simps setsum.distrib [symmetric] choose_reduce_nat)
lp15@55130
   177
  also have "\<dots> = (\<Sum>k=0..n+1. of_nat (n+1 choose k) * a^k * b^(n+1-k))"
lp15@55130
   178
    using decomp by (simp add: field_simps)
lp15@55130
   179
  finally show "?P (Suc n)" by simp
lp15@55130
   180
qed
nipkow@31719
   181
lp15@55130
   182
text{* Original version for the naturals *}
lp15@55130
   183
corollary binomial: "(a+b::nat)^n = (\<Sum>k=0..n. (of_nat (n choose k)) * a^k * b^(n-k))"
lp15@55130
   184
    using binomial_ring [of "int a" "int b" n]
lp15@55130
   185
  by (simp only: of_nat_add [symmetric] of_nat_mult [symmetric] of_nat_power [symmetric]
lp15@55130
   186
           of_nat_setsum [symmetric]
lp15@55130
   187
           of_nat_eq_iff of_nat_id)
lp15@55130
   188
lp15@56178
   189
lemma choose_row_sum: "(\<Sum>k=0..n. n choose k) = 2^n"
lp15@56178
   190
  using binomial [of 1 "1" n]
lp15@56178
   191
  by (simp add: numeral_2_eq_2)
lp15@56178
   192
lp15@56178
   193
lemma sum_choose_lower: "(\<Sum>k=0..n. (r+k) choose k) = Suc (r+n) choose n"
lp15@56178
   194
  by (induct n) auto
lp15@56178
   195
lp15@56178
   196
lemma sum_choose_upper: "(\<Sum>k=0..n. k choose m) = Suc n choose Suc m"
lp15@56178
   197
  by (induct n) auto
lp15@56178
   198
lp15@56178
   199
lemma natsum_reverse_index:
lp15@56178
   200
  fixes m::nat
hoelzl@57129
   201
  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)"
hoelzl@57129
   202
  by (rule setsum.reindex_bij_witness[where i="\<lambda>k. m+n-k" and j="\<lambda>k. m+n-k"]) auto
lp15@56178
   203
lp15@56178
   204
lemma sum_choose_diagonal:
lp15@56178
   205
  assumes "m\<le>n" shows "(\<Sum>k=0..m. (n-k) choose (m-k)) = Suc n choose m"
lp15@56178
   206
proof -
lp15@56178
   207
  have "(\<Sum>k=0..m. (n-k) choose (m-k)) = (\<Sum>k=0..m. (n-m+k) choose k)"
lp15@56178
   208
    by (rule natsum_reverse_index) (simp add: assms)
lp15@56178
   209
  also have "... = Suc (n-m+m) choose m"
lp15@56178
   210
    by (rule sum_choose_lower)
lp15@56178
   211
  also have "... = Suc n choose m" using assms
lp15@56178
   212
    by simp
lp15@56178
   213
  finally show ?thesis .
lp15@56178
   214
qed
lp15@56178
   215
lp15@55130
   216
subsection{* Pochhammer's symbol : generalized rising factorial *}
lp15@55130
   217
lp15@55130
   218
text {* See @{url "http://en.wikipedia.org/wiki/Pochhammer_symbol"} *}
lp15@55130
   219
lp15@55130
   220
definition "pochhammer (a::'a::comm_semiring_1) n =
lp15@55130
   221
  (if n = 0 then 1 else setprod (\<lambda>n. a + of_nat n) {0 .. n - 1})"
lp15@55130
   222
lp15@55130
   223
lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"
lp15@55130
   224
  by (simp add: pochhammer_def)
nipkow@31719
   225
lp15@55130
   226
lemma pochhammer_1 [simp]: "pochhammer a 1 = a"
lp15@55130
   227
  by (simp add: pochhammer_def)
lp15@55130
   228
lp15@55130
   229
lemma pochhammer_Suc0 [simp]: "pochhammer a (Suc 0) = a"
lp15@55130
   230
  by (simp add: pochhammer_def)
lp15@55130
   231
lp15@55130
   232
lemma pochhammer_Suc_setprod: "pochhammer a (Suc n) = setprod (\<lambda>n. a + of_nat n) {0 .. n}"
lp15@55130
   233
  by (simp add: pochhammer_def)
lp15@55130
   234
lp15@55130
   235
lemma setprod_nat_ivl_Suc: "setprod f {0 .. Suc n} = setprod f {0..n} * f (Suc n)"
lp15@55130
   236
proof -
lp15@55130
   237
  have "{0..Suc n} = {0..n} \<union> {Suc n}" by auto
lp15@55130
   238
  then show ?thesis by (simp add: field_simps)
lp15@55130
   239
qed
lp15@55130
   240
lp15@55130
   241
lemma setprod_nat_ivl_1_Suc: "setprod f {0 .. Suc n} = f 0 * setprod f {1.. Suc n}"
lp15@55130
   242
proof -
lp15@55130
   243
  have "{0..Suc n} = {0} \<union> {1 .. Suc n}" by auto
lp15@55130
   244
  then show ?thesis by simp
lp15@55130
   245
qed
lp15@55130
   246
nipkow@31719
   247
lp15@55130
   248
lemma pochhammer_Suc: "pochhammer a (Suc n) = pochhammer a n * (a + of_nat n)"
lp15@55130
   249
proof (cases n)
lp15@55130
   250
  case 0
lp15@55130
   251
  then show ?thesis by simp
lp15@55130
   252
next
lp15@55130
   253
  case (Suc n)
lp15@55130
   254
  show ?thesis unfolding Suc pochhammer_Suc_setprod setprod_nat_ivl_Suc ..
lp15@55130
   255
qed
lp15@55130
   256
lp15@55130
   257
lemma pochhammer_rec: "pochhammer a (Suc n) = a * pochhammer (a + 1) n"
lp15@55130
   258
proof (cases "n = 0")
lp15@55130
   259
  case True
lp15@55130
   260
  then show ?thesis by (simp add: pochhammer_Suc_setprod)
lp15@55130
   261
next
lp15@55130
   262
  case False
lp15@55130
   263
  have *: "finite {1 .. n}" "0 \<notin> {1 .. n}" by auto
lp15@55130
   264
  have eq: "insert 0 {1 .. n} = {0..n}" by auto
lp15@55130
   265
  have **: "(\<Prod>n\<in>{1\<Colon>nat..n}. a + of_nat n) = (\<Prod>n\<in>{0\<Colon>nat..n - 1}. a + 1 + of_nat n)"
haftmann@57418
   266
    apply (rule setprod.reindex_cong [where l = Suc])
lp15@55130
   267
    using False
lp15@55130
   268
    apply (auto simp add: fun_eq_iff field_simps)
lp15@55130
   269
    done
lp15@55130
   270
  show ?thesis
lp15@55130
   271
    apply (simp add: pochhammer_def)
haftmann@57418
   272
    unfolding setprod.insert [OF *, unfolded eq]
lp15@55130
   273
    using ** apply (simp add: field_simps)
lp15@55130
   274
    done
lp15@55130
   275
qed
lp15@55130
   276
lp15@55130
   277
lemma pochhammer_fact: "of_nat (fact n) = pochhammer 1 n"
lp15@55130
   278
  unfolding fact_altdef_nat
lp15@55130
   279
  apply (cases n)
lp15@55130
   280
   apply (simp_all add: of_nat_setprod pochhammer_Suc_setprod)
haftmann@57418
   281
  apply (rule setprod.reindex_cong [where l = Suc])
lp15@55130
   282
    apply (auto simp add: fun_eq_iff)
lp15@55130
   283
  done
wenzelm@44872
   284
lp15@55130
   285
lemma pochhammer_of_nat_eq_0_lemma:
lp15@55130
   286
  assumes "k > n"
lp15@55130
   287
  shows "pochhammer (- (of_nat n :: 'a:: idom)) k = 0"
lp15@55130
   288
proof (cases "n = 0")
lp15@55130
   289
  case True
lp15@55130
   290
  then show ?thesis
lp15@55130
   291
    using assms by (cases k) (simp_all add: pochhammer_rec)
lp15@55130
   292
next
lp15@55130
   293
  case False
lp15@55130
   294
  from assms obtain h where "k = Suc h" by (cases k) auto
lp15@55130
   295
  then show ?thesis
lp15@55130
   296
    by (simp add: pochhammer_Suc_setprod)
lp15@55130
   297
       (metis Suc_leI Suc_le_mono assms atLeastAtMost_iff less_eq_nat.simps(1))
lp15@55130
   298
qed
nipkow@31719
   299
lp15@55130
   300
lemma pochhammer_of_nat_eq_0_lemma':
lp15@55130
   301
  assumes kn: "k \<le> n"
lp15@55130
   302
  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k \<noteq> 0"
lp15@55130
   303
proof (cases k)
lp15@55130
   304
  case 0
lp15@55130
   305
  then show ?thesis by simp
lp15@55130
   306
next
lp15@55130
   307
  case (Suc h)
lp15@55130
   308
  then show ?thesis
lp15@55130
   309
    apply (simp add: pochhammer_Suc_setprod)
lp15@55130
   310
    using Suc kn apply (auto simp add: algebra_simps)
lp15@55130
   311
    done
lp15@55130
   312
qed
lp15@55130
   313
lp15@55130
   314
lemma pochhammer_of_nat_eq_0_iff:
lp15@55130
   315
  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k = 0 \<longleftrightarrow> k > n"
lp15@55130
   316
  (is "?l = ?r")
lp15@55130
   317
  using pochhammer_of_nat_eq_0_lemma[of n k, where ?'a='a]
lp15@55130
   318
    pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]
lp15@55130
   319
  by (auto simp add: not_le[symmetric])
lp15@55130
   320
lp15@55130
   321
lp15@55130
   322
lemma pochhammer_eq_0_iff: "pochhammer a n = (0::'a::field_char_0) \<longleftrightarrow> (\<exists>k < n. a = - of_nat k)"
lp15@55130
   323
  apply (auto simp add: pochhammer_of_nat_eq_0_iff)
lp15@55130
   324
  apply (cases n)
lp15@55130
   325
   apply (auto simp add: pochhammer_def algebra_simps group_add_class.eq_neg_iff_add_eq_0)
lp15@55130
   326
  apply (metis leD not_less_eq)
lp15@55130
   327
  done
nipkow@31719
   328
nipkow@31719
   329
lp15@55130
   330
lemma pochhammer_eq_0_mono:
lp15@55130
   331
  "pochhammer a n = (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"
lp15@55130
   332
  unfolding pochhammer_eq_0_iff by auto
lp15@55130
   333
lp15@55130
   334
lemma pochhammer_neq_0_mono:
lp15@55130
   335
  "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"
lp15@55130
   336
  unfolding pochhammer_eq_0_iff by auto
lp15@55130
   337
lp15@55130
   338
lemma pochhammer_minus:
lp15@55130
   339
  assumes kn: "k \<le> n"
lp15@55130
   340
  shows "pochhammer (- b) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (b - of_nat k + 1) k"
lp15@55130
   341
proof (cases k)
lp15@55130
   342
  case 0
lp15@55130
   343
  then show ?thesis by simp
lp15@55130
   344
next
lp15@55130
   345
  case (Suc h)
hoelzl@57129
   346
  have eq: "((- 1) ^ Suc h :: 'a) = (\<Prod>i=0..h. - 1)"
lp15@55130
   347
    using setprod_constant[where A="{0 .. h}" and y="- 1 :: 'a"]
lp15@55130
   348
    by auto
lp15@55130
   349
  show ?thesis
haftmann@57418
   350
    unfolding Suc pochhammer_Suc_setprod eq setprod.distrib[symmetric]
hoelzl@57129
   351
    by (rule setprod.reindex_bij_witness[where i="op - h" and j="op - h"])
hoelzl@57129
   352
       (auto simp: of_nat_diff)
lp15@55130
   353
qed
lp15@55130
   354
lp15@55130
   355
lemma pochhammer_minus':
lp15@55130
   356
  assumes kn: "k \<le> n"
lp15@55130
   357
  shows "pochhammer (b - of_nat k + 1) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (- b) k"
lp15@55130
   358
  unfolding pochhammer_minus[OF kn, where b=b]
haftmann@57512
   359
  unfolding mult.assoc[symmetric]
lp15@55130
   360
  unfolding power_add[symmetric]
lp15@55130
   361
  by simp
lp15@55130
   362
lp15@55130
   363
lemma pochhammer_same: "pochhammer (- of_nat n) n =
lp15@55130
   364
    ((- 1) ^ n :: 'a::comm_ring_1) * of_nat (fact n)"
lp15@55130
   365
  unfolding pochhammer_minus[OF le_refl[of n]]
lp15@55130
   366
  by (simp add: of_nat_diff pochhammer_fact)
lp15@55130
   367
lp15@55130
   368
lp15@55130
   369
subsection{* Generalized binomial coefficients *}
lp15@55130
   370
lp15@55130
   371
definition gbinomial :: "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
lp15@55130
   372
  where "a gchoose n =
lp15@55130
   373
    (if n = 0 then 1 else (setprod (\<lambda>i. a - of_nat i) {0 .. n - 1}) / of_nat (fact n))"
nipkow@31719
   374
lp15@55130
   375
lemma gbinomial_0 [simp]: "a gchoose 0 = 1" "0 gchoose (Suc n) = 0"
lp15@55130
   376
  apply (simp_all add: gbinomial_def)
lp15@55130
   377
  apply (subgoal_tac "(\<Prod>i\<Colon>nat\<in>{0\<Colon>nat..n}. - of_nat i) = (0::'b)")
lp15@55130
   378
   apply (simp del:setprod_zero_iff)
lp15@55130
   379
  apply simp
lp15@55130
   380
  done
lp15@55130
   381
lp15@55130
   382
lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / of_nat (fact n)"
lp15@55130
   383
proof (cases "n = 0")
lp15@55130
   384
  case True
lp15@55130
   385
  then show ?thesis by simp
lp15@55130
   386
next
lp15@55130
   387
  case False
lp15@55130
   388
  from this setprod_constant[of "{0 .. n - 1}" "- (1:: 'a)"]
lp15@55130
   389
  have eq: "(- (1\<Colon>'a)) ^ n = setprod (\<lambda>i. - 1) {0 .. n - 1}"
lp15@55130
   390
    by auto
lp15@55130
   391
  from False show ?thesis
lp15@55130
   392
    by (simp add: pochhammer_def gbinomial_def field_simps
haftmann@57418
   393
      eq setprod.distrib[symmetric])
lp15@55130
   394
qed
nipkow@31719
   395
lp15@55130
   396
lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
lp15@55130
   397
proof (induct n arbitrary: k rule: nat_less_induct)
lp15@55130
   398
  fix n k assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) =
lp15@55130
   399
                      fact m" and kn: "k \<le> n"
lp15@55130
   400
  let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"
lp15@55130
   401
  { assume "n=0" then have ?ths using kn by simp }
lp15@55130
   402
  moreover
lp15@55130
   403
  { assume "k=0" then have ?ths using kn by simp }
lp15@55130
   404
  moreover
lp15@55130
   405
  { assume nk: "n=k" then have ?ths by simp }
lp15@55130
   406
  moreover
lp15@55130
   407
  { fix m h assume n: "n = Suc m" and h: "k = Suc h" and hm: "h < m"
lp15@55130
   408
    from n have mn: "m < n" by arith
lp15@55130
   409
    from hm have hm': "h \<le> m" by arith
lp15@55130
   410
    from hm h n kn have km: "k \<le> m" by arith
lp15@55130
   411
    have "m - h = Suc (m - Suc h)" using  h km hm by arith
lp15@55130
   412
    with km h have th0: "fact (m - h) = (m - h) * fact (m - k)"
lp15@55130
   413
      by simp
lp15@55130
   414
    from n h th0
lp15@55130
   415
    have "fact k * fact (n - k) * (n choose k) =
lp15@55130
   416
        k * (fact h * fact (m - h) * (m choose h)) + 
lp15@55130
   417
        (m - h) * (fact k * fact (m - k) * (m choose k))"
lp15@55130
   418
      by (simp add: field_simps)
lp15@55130
   419
    also have "\<dots> = (k + (m - h)) * fact m"
lp15@55130
   420
      using H[rule_format, OF mn hm'] H[rule_format, OF mn km]
lp15@55130
   421
      by (simp add: field_simps)
lp15@55130
   422
    finally have ?ths using h n km by simp }
lp15@55130
   423
  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@55130
   424
    using kn by presburger
lp15@55130
   425
  ultimately show ?ths by blast
lp15@55130
   426
qed
lp15@55130
   427
lp15@55130
   428
lemma binomial_fact:
lp15@55130
   429
  assumes kn: "k \<le> n"
lp15@55130
   430
  shows "(of_nat (n choose k) :: 'a::field_char_0) =
lp15@55130
   431
    of_nat (fact n) / (of_nat (fact k) * of_nat (fact (n - k)))"
lp15@55130
   432
  using binomial_fact_lemma[OF kn]
lp15@55130
   433
  by (simp add: field_simps of_nat_mult [symmetric])
nipkow@31719
   434
lp15@55130
   435
lemma binomial_gbinomial: "of_nat (n choose k) = of_nat n gchoose k"
lp15@55130
   436
proof -
lp15@55130
   437
  { assume kn: "k > n"
lp15@55130
   438
    then have ?thesis
lp15@55130
   439
      by (subst binomial_eq_0[OF kn]) 
lp15@55130
   440
         (simp add: gbinomial_pochhammer field_simps  pochhammer_of_nat_eq_0_iff) }
lp15@55130
   441
  moreover
lp15@55130
   442
  { assume "k=0" then have ?thesis by simp }
lp15@55130
   443
  moreover
lp15@55130
   444
  { assume kn: "k \<le> n" and k0: "k\<noteq> 0"
lp15@55130
   445
    from k0 obtain h where h: "k = Suc h" by (cases k) auto
lp15@55130
   446
    from h
lp15@55130
   447
    have eq:"(- 1 :: 'a) ^ k = setprod (\<lambda>i. - 1) {0..h}"
lp15@55130
   448
      by (subst setprod_constant) auto
lp15@55130
   449
    have eq': "(\<Prod>i\<in>{0..h}. of_nat n + - (of_nat i :: 'a)) = (\<Prod>i\<in>{n - h..n}. of_nat i)"
lp15@55130
   450
        using h kn
hoelzl@57129
   451
      by (intro setprod.reindex_bij_witness[where i="op - n" and j="op - n"])
hoelzl@57129
   452
         (auto simp: of_nat_diff)
lp15@55130
   453
    have th0: "finite {1..n - Suc h}" "finite {n - h .. n}"
lp15@55130
   454
        "{1..n - Suc h} \<inter> {n - h .. n} = {}" and
lp15@55130
   455
        eq3: "{1..n - Suc h} \<union> {n - h .. n} = {1..n}"
lp15@55130
   456
      using h kn by auto
lp15@55130
   457
    from eq[symmetric]
lp15@55130
   458
    have ?thesis using kn
lp15@55130
   459
      apply (simp add: binomial_fact[OF kn, where ?'a = 'a]
lp15@55130
   460
        gbinomial_pochhammer field_simps pochhammer_Suc_setprod)
lp15@55130
   461
      apply (simp add: pochhammer_Suc_setprod fact_altdef_nat h
haftmann@57418
   462
        of_nat_setprod setprod.distrib[symmetric] eq' del: One_nat_def power_Suc)
haftmann@57418
   463
      unfolding setprod.union_disjoint[OF th0, unfolded eq3, of "of_nat:: nat \<Rightarrow> 'a"] eq[unfolded h]
haftmann@57512
   464
      unfolding mult.assoc[symmetric]
haftmann@57418
   465
      unfolding setprod.distrib[symmetric]
lp15@55130
   466
      apply simp
hoelzl@57129
   467
      apply (intro setprod.reindex_bij_witness[where i="op - n" and j="op - n"])
hoelzl@57129
   468
      apply (auto simp: of_nat_diff)
lp15@55130
   469
      done
lp15@55130
   470
  }
lp15@55130
   471
  moreover
lp15@55130
   472
  have "k > n \<or> k = 0 \<or> (k \<le> n \<and> k \<noteq> 0)" by arith
lp15@55130
   473
  ultimately show ?thesis by blast
lp15@55130
   474
qed
nipkow@31719
   475
lp15@55130
   476
lemma gbinomial_1[simp]: "a gchoose 1 = a"
lp15@55130
   477
  by (simp add: gbinomial_def)
lp15@55130
   478
lp15@55130
   479
lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"
lp15@55130
   480
  by (simp add: gbinomial_def)
lp15@55130
   481
lp15@55130
   482
lemma gbinomial_mult_1:
lp15@55130
   483
  "a * (a gchoose n) =
lp15@55130
   484
    of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"  (is "?l = ?r")
lp15@55130
   485
proof -
lp15@55130
   486
  have "?r = ((- 1) ^n * pochhammer (- a) n / of_nat (fact n)) * (of_nat n - (- a + of_nat n))"
lp15@55130
   487
    unfolding gbinomial_pochhammer
lp15@55130
   488
      pochhammer_Suc fact_Suc of_nat_mult right_diff_distrib power_Suc
lp15@55130
   489
    by (simp add:  field_simps del: of_nat_Suc)
lp15@55130
   490
  also have "\<dots> = ?l" unfolding gbinomial_pochhammer
lp15@55130
   491
    by (simp add: field_simps)
lp15@55130
   492
  finally show ?thesis ..
lp15@55130
   493
qed
lp15@55130
   494
lp15@55130
   495
lemma gbinomial_mult_1':
lp15@55130
   496
    "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
haftmann@57512
   497
  by (simp add: mult.commute gbinomial_mult_1)
lp15@55130
   498
lp15@55130
   499
lemma gbinomial_Suc:
lp15@55130
   500
    "a gchoose (Suc k) = (setprod (\<lambda>i. a - of_nat i) {0 .. k}) / of_nat (fact (Suc k))"
lp15@55130
   501
  by (simp add: gbinomial_def)
lp15@55130
   502
lp15@55130
   503
lemma gbinomial_mult_fact:
lp15@55130
   504
  "(of_nat (fact (Suc k)) :: 'a) * ((a::'a::field_char_0) gchoose (Suc k)) =
lp15@55130
   505
    (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
lp15@55130
   506
  by (simp_all add: gbinomial_Suc field_simps del: fact_Suc)
lp15@55130
   507
lp15@55130
   508
lemma gbinomial_mult_fact':
lp15@55130
   509
  "((a::'a::field_char_0) gchoose (Suc k)) * (of_nat (fact (Suc k)) :: 'a) =
lp15@55130
   510
    (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
lp15@55130
   511
  using gbinomial_mult_fact[of k a]
haftmann@57512
   512
  by (subst mult.commute)
lp15@55130
   513
nipkow@31719
   514
lp15@55130
   515
lemma gbinomial_Suc_Suc:
lp15@55130
   516
  "((a::'a::field_char_0) + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"
lp15@55130
   517
proof (cases k)
lp15@55130
   518
  case 0
lp15@55130
   519
  then show ?thesis by simp
lp15@55130
   520
next
lp15@55130
   521
  case (Suc h)
lp15@55130
   522
  have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)"
haftmann@57418
   523
    apply (rule setprod.reindex_cong [where l = Suc])
lp15@55130
   524
      using Suc
lp15@55130
   525
      apply auto
lp15@55130
   526
    done
lp15@55130
   527
  have "of_nat (fact (Suc k)) * (a gchoose k + (a gchoose (Suc k))) =
lp15@55130
   528
    ((a gchoose Suc h) * of_nat (fact (Suc h)) * of_nat (Suc k)) + (\<Prod>i\<in>{0\<Colon>nat..Suc h}. a - of_nat i)"
lp15@55130
   529
    apply (simp add: Suc field_simps del: fact_Suc)
lp15@55130
   530
    unfolding gbinomial_mult_fact'
lp15@55130
   531
    apply (subst fact_Suc)
lp15@55130
   532
    unfolding of_nat_mult
haftmann@57512
   533
    apply (subst mult.commute)
haftmann@57512
   534
    unfolding mult.assoc
lp15@55130
   535
    unfolding gbinomial_mult_fact
lp15@55130
   536
    apply (simp add: field_simps)
lp15@55130
   537
    done
lp15@55130
   538
  also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
lp15@55130
   539
    unfolding gbinomial_mult_fact' setprod_nat_ivl_Suc
lp15@55130
   540
    by (simp add: field_simps Suc)
lp15@55130
   541
  also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
lp15@55130
   542
    using eq0
lp15@55130
   543
    by (simp add: Suc setprod_nat_ivl_1_Suc)
lp15@55130
   544
  also have "\<dots> = of_nat (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
lp15@55130
   545
    unfolding gbinomial_mult_fact ..
lp15@55130
   546
  finally show ?thesis by (simp del: fact_Suc)
lp15@55130
   547
qed
lp15@55130
   548
lp15@58833
   549
lemma gbinomial_reduce_nat:
lp15@58833
   550
  "0 < k \<Longrightarrow> (a::'a::field_char_0) gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)"
lp15@58833
   551
by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc)
lp15@58833
   552
lp15@55130
   553
lp15@55130
   554
lemma binomial_symmetric:
lp15@55130
   555
  assumes kn: "k \<le> n"
lp15@55130
   556
  shows "n choose k = n choose (n - k)"
lp15@55130
   557
proof-
lp15@55130
   558
  from kn have kn': "n - k \<le> n" by arith
lp15@55130
   559
  from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']
lp15@55130
   560
  have "fact k * fact (n - k) * (n choose k) =
lp15@55130
   561
    fact (n - k) * fact (n - (n - k)) * (n choose (n - k))" by simp
lp15@55130
   562
  then show ?thesis using kn by simp
lp15@55130
   563
qed
nipkow@31719
   564
lp15@55130
   565
(* Contributed by Manuel Eberl *)
lp15@55130
   566
(* Alternative definition of the binomial coefficient as \<Prod>i<k. (n - i) / (k - i) *)
lp15@55130
   567
lemma binomial_altdef_of_nat:
lp15@55130
   568
  fixes n k :: nat
lp15@55130
   569
    and x :: "'a :: {field_char_0,field_inverse_zero}"
lp15@55130
   570
  assumes "k \<le> n"
lp15@55130
   571
  shows "of_nat (n choose k) = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
lp15@55130
   572
proof (cases "0 < k")
lp15@55130
   573
  case True
lp15@55130
   574
  then have "(of_nat (n choose k) :: 'a) = (\<Prod>i<k. of_nat n - of_nat i) / of_nat (fact k)"
lp15@55130
   575
    unfolding binomial_gbinomial gbinomial_def
lp15@55130
   576
    by (auto simp: gr0_conv_Suc lessThan_Suc_atMost atLeast0AtMost)
lp15@55130
   577
  also have "\<dots> = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
lp15@55130
   578
    using `k \<le> n` unfolding fact_eq_rev_setprod_nat of_nat_setprod
haftmann@57418
   579
    by (auto simp add: setprod_dividef intro!: setprod.cong of_nat_diff[symmetric])
lp15@55130
   580
  finally show ?thesis .
lp15@55130
   581
next
lp15@55130
   582
  case False
lp15@55130
   583
  then show ?thesis by simp
lp15@55130
   584
qed
lp15@55130
   585
lp15@55130
   586
lemma binomial_ge_n_over_k_pow_k:
lp15@55130
   587
  fixes k n :: nat
lp15@55130
   588
    and x :: "'a :: linordered_field_inverse_zero"
lp15@55130
   589
  assumes "0 < k"
lp15@55130
   590
    and "k \<le> n"
lp15@55130
   591
  shows "(of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)"
lp15@55130
   592
proof -
lp15@55130
   593
  have "(of_nat n / of_nat k :: 'a) ^ k = (\<Prod>i<k. of_nat n / of_nat k :: 'a)"
lp15@55130
   594
    by (simp add: setprod_constant)
lp15@55130
   595
  also have "\<dots> \<le> of_nat (n choose k)"
lp15@55130
   596
    unfolding binomial_altdef_of_nat[OF `k\<le>n`]
lp15@55130
   597
  proof (safe intro!: setprod_mono)
lp15@55130
   598
    fix i :: nat
lp15@55130
   599
    assume  "i < k"
lp15@55130
   600
    from assms have "n * i \<ge> i * k" by simp
lp15@55130
   601
    then have "n * k - n * i \<le> n * k - i * k" by arith
lp15@55130
   602
    then have "n * (k - i) \<le> (n - i) * k"
haftmann@57512
   603
      by (simp add: diff_mult_distrib2 mult.commute)
lp15@55130
   604
    then have "of_nat n * of_nat (k - i) \<le> of_nat (n - i) * (of_nat k :: 'a)"
lp15@55130
   605
      unfolding of_nat_mult[symmetric] of_nat_le_iff .
lp15@55130
   606
    with assms show "of_nat n / of_nat k \<le> of_nat (n - i) / (of_nat (k - i) :: 'a)"
lp15@55130
   607
      using `i < k` by (simp add: field_simps)
lp15@55130
   608
  qed (simp add: zero_le_divide_iff)
lp15@55130
   609
  finally show ?thesis .
lp15@55130
   610
qed
lp15@55130
   611
lp15@55130
   612
lemma binomial_le_pow:
lp15@55130
   613
  assumes "r \<le> n"
lp15@55130
   614
  shows "n choose r \<le> n ^ r"
lp15@55130
   615
proof -
lp15@55130
   616
  have "n choose r \<le> fact n div fact (n - r)"
lp15@55130
   617
    using `r \<le> n` by (subst binomial_fact_lemma[symmetric]) auto
lp15@55130
   618
  with fact_div_fact_le_pow [OF assms] show ?thesis by auto
lp15@55130
   619
qed
lp15@55130
   620
lp15@55130
   621
lemma binomial_altdef_nat: "(k::nat) \<le> n \<Longrightarrow>
lp15@55130
   622
    n choose k = fact n div (fact k * fact (n - k))"
lp15@55130
   623
 by (subst binomial_fact_lemma [symmetric]) auto
lp15@55130
   624
lp15@58713
   625
lemma choose_dvd_nat: "(k::nat) \<le> n \<Longrightarrow> fact k * fact (n - k) dvd fact n"
lp15@58713
   626
by (metis binomial_fact_lemma dvd_def)
lp15@58713
   627
lp15@58713
   628
lemma choose_dvd_int: 
lp15@58713
   629
  assumes "(0::int) <= k" and "k <= n"
lp15@58713
   630
  shows "fact k * fact (n - k) dvd fact n"
lp15@58713
   631
  apply (subst tsub_eq [symmetric], rule assms)
lp15@58713
   632
  apply (rule choose_dvd_nat [transferred])
lp15@58713
   633
  using assms apply auto
lp15@58713
   634
  done
lp15@56178
   635
lp15@56178
   636
lemma fact_fact_dvd_fact: fixes k::nat shows "fact k * fact n dvd fact (n + k)"
lp15@58713
   637
by (metis add.commute add_diff_cancel_left' choose_dvd_nat le_add2)
lp15@56178
   638
lp15@56178
   639
lemma choose_mult_lemma:
lp15@56178
   640
     "((m+r+k) choose (m+k)) * ((m+k) choose k) = ((m+r+k) choose k) * ((m+r) choose m)"
lp15@56178
   641
proof -
lp15@56178
   642
  have "((m+r+k) choose (m+k)) * ((m+k) choose k) =
lp15@56178
   643
        fact (m+r + k) div (fact (m + k) * fact (m+r - m)) * (fact (m + k) div (fact k * fact m))"
lp15@56178
   644
    by (simp add: assms binomial_altdef_nat)
lp15@56178
   645
  also have "... = fact (m+r+k) div (fact r * (fact k * fact m))"
lp15@56178
   646
    apply (subst div_mult_div_if_dvd)
lp15@56178
   647
    apply (auto simp: fact_fact_dvd_fact)
lp15@58713
   648
    apply (metis add.assoc add.commute fact_fact_dvd_fact)
lp15@56178
   649
    done
lp15@56178
   650
  also have "... = (fact (m+r+k) * fact (m+r)) div (fact r * (fact k * fact m) * fact (m+r))"
lp15@56178
   651
    apply (subst div_mult_div_if_dvd [symmetric])
lp15@56178
   652
    apply (auto simp: fact_fact_dvd_fact)
haftmann@57512
   653
    apply (metis dvd_trans dvd.dual_order.refl fact_fact_dvd_fact mult_dvd_mono mult.left_commute)
lp15@56178
   654
    done
lp15@56178
   655
  also have "... = (fact (m+r+k) div (fact k * fact (m+r)) * (fact (m+r) div (fact r * fact m)))"
lp15@56178
   656
    apply (subst div_mult_div_if_dvd)
lp15@56178
   657
    apply (auto simp: fact_fact_dvd_fact)
haftmann@57512
   658
    apply(metis mult.left_commute)
lp15@56178
   659
    done
lp15@56178
   660
  finally show ?thesis
haftmann@57512
   661
    by (simp add: binomial_altdef_nat mult.commute)
lp15@56178
   662
qed
lp15@56178
   663
lp15@56178
   664
lemma choose_mult:
lp15@56178
   665
  assumes "k\<le>m" "m\<le>n"
lp15@56178
   666
    shows "(n choose m) * (m choose k) = (n choose k) * ((n-k) choose (m-k))"
lp15@56178
   667
using assms choose_mult_lemma [of "m-k" "n-m" k]
lp15@56178
   668
by simp
nipkow@31719
   669
nipkow@31719
   670
nipkow@31719
   671
subsection {* Binomial coefficients *}
nipkow@31719
   672
lp15@55130
   673
lemma choose_plus_one_nat:
lp15@55130
   674
     "((n::nat) + 1) choose (k + 1) =(n choose (k + 1)) + (n choose k)"
lp15@55130
   675
  by (simp add: choose_reduce_nat)
nipkow@31719
   676
lp15@55130
   677
lemma choose_Suc_nat: 
lp15@55130
   678
     "(Suc n) choose (Suc k) = (n choose (Suc k)) + (n choose k)"
nipkow@31952
   679
  by (simp add: choose_reduce_nat)
nipkow@31719
   680
lp15@55130
   681
lemma choose_one: "(n::nat) choose 1 = n"
lp15@55130
   682
  by simp
nipkow@31719
   683
lp15@58713
   684
(*FIXME: messy and apparently unused*)
nipkow@31719
   685
lemma binomial_induct [rule_format]: "(ALL (n::nat). P n n) \<longrightarrow> 
lp15@55130
   686
    (ALL n. P (Suc n) 0) \<longrightarrow> (ALL n. (ALL k < n. P n k \<longrightarrow> P n (Suc k) \<longrightarrow>
lp15@55130
   687
    P (Suc n) (Suc k))) \<longrightarrow> (ALL k <= n. P n k)"
lp15@55130
   688
  apply (induct n)
nipkow@31719
   689
  apply auto
nipkow@31719
   690
  apply (case_tac "k = 0")
nipkow@31719
   691
  apply auto
lp15@55130
   692
  apply (case_tac "k = Suc n")
nipkow@31719
   693
  apply auto
lp15@55130
   694
  apply (metis Suc_le_eq fact_nat.cases le_Suc_eq le_eq_less_or_eq)
wenzelm@41541
   695
  done
nipkow@31719
   696
Andreas@51291
   697
lemma card_UNION:
Andreas@51292
   698
  assumes "finite A" and "\<forall>k \<in> A. finite k"
haftmann@58410
   699
  shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))"
Andreas@51291
   700
  (is "?lhs = ?rhs")
Andreas@51291
   701
proof -
haftmann@58410
   702
  have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))" by simp
haftmann@58410
   703
  also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))" (is "_ = nat ?rhs")
Andreas@51292
   704
    by(subst setsum_right_distrib) simp
haftmann@58410
   705
  also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))"
haftmann@57418
   706
    using assms by(subst setsum.Sigma)(auto)
haftmann@58410
   707
  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))"
haftmann@57418
   708
    by (rule setsum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta)
haftmann@58410
   709
  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))"
haftmann@57418
   710
    using assms by(auto intro!: setsum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"])
haftmann@58410
   711
  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)))" 
haftmann@57418
   712
    using assms by(subst setsum.Sigma) auto
Andreas@51291
   713
  also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "setsum ?lhs _ = _")
haftmann@57418
   714
  proof(rule setsum.cong[OF refl])
Andreas@51291
   715
    fix x
Andreas@51291
   716
    assume x: "x \<in> \<Union>A"
Andreas@51291
   717
    def K \<equiv> "{X \<in> A. x \<in> X}"
Andreas@51291
   718
    with `finite A` have K: "finite K" by auto
Andreas@51291
   719
    let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I}"
Andreas@51291
   720
    have "inj_on snd (SIGMA i:{1..card A}. ?I i)"
Andreas@51291
   721
      using assms by(auto intro!: inj_onI)
Andreas@51291
   722
    moreover have [symmetric]: "snd ` (SIGMA i:{1..card A}. ?I i) = {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}"
wenzelm@55143
   723
      using assms by(auto intro!: rev_image_eqI[where x="(card a, a)" for a]
wenzelm@55143
   724
        simp add: card_gt_0_iff[folded Suc_le_eq]
wenzelm@55143
   725
        dest: finite_subset intro: card_mono)
haftmann@58410
   726
    ultimately have "?lhs x = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))"
haftmann@57418
   727
      by (rule setsum.reindex_cong [where l = snd]) fastforce
haftmann@58410
   728
    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)))"
haftmann@57418
   729
      using assms by(subst setsum.Sigma) auto
haftmann@58410
   730
    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))"
Andreas@51292
   731
      by(subst setsum_right_distrib) simp
haftmann@58410
   732
    also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))" (is "_ = ?rhs")
haftmann@57418
   733
    proof(rule setsum.mono_neutral_cong_right[rule_format])
Andreas@51291
   734
      show "{1..card K} \<subseteq> {1..card A}" using `finite A`
Andreas@51291
   735
        by(auto simp add: K_def intro: card_mono)
Andreas@51291
   736
    next
Andreas@51291
   737
      fix i
Andreas@51291
   738
      assume "i \<in> {1..card A} - {1..card K}"
Andreas@51291
   739
      hence i: "i \<le> card A" "card K < i" by auto
Andreas@51291
   740
      have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}" 
Andreas@51291
   741
        by(auto simp add: K_def)
Andreas@51291
   742
      also have "\<dots> = {}" using `finite A` i
Andreas@51291
   743
        by(auto simp add: K_def dest: card_mono[rotated 1])
haftmann@58410
   744
      finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0"
Andreas@51291
   745
        by(simp only:) simp
Andreas@51291
   746
    next
Andreas@51291
   747
      fix i
Andreas@51291
   748
      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)"
Andreas@51291
   749
        (is "?lhs = ?rhs")
haftmann@57418
   750
        by(rule setsum.cong)(auto simp add: K_def)
haftmann@58410
   751
      thus "(- 1) ^ (i + 1) * ?lhs = (- 1) ^ (i + 1) * ?rhs" by simp
Andreas@51291
   752
    qed simp
Andreas@51291
   753
    also have "{I. I \<subseteq> K \<and> card I = 0} = {{}}" using assms
Andreas@51291
   754
      by(auto simp add: card_eq_0_iff K_def dest: finite_subset)
haftmann@58410
   755
    hence "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1"
lp15@55130
   756
      by(subst (2) setsum_head_Suc)(simp_all )
haftmann@58410
   757
    also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1"
lp15@55130
   758
      using K by(subst n_subsets[symmetric]) simp_all
haftmann@58410
   759
    also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1"
Andreas@51292
   760
      by(subst setsum_right_distrib[symmetric]) simp
Andreas@51291
   761
    also have "\<dots> =  - ((-1 + 1) ^ card K) + 1"
haftmann@57514
   762
      by(subst binomial_ring)(simp add: ac_simps)
Andreas@51291
   763
    also have "\<dots> = 1" using x K by(auto simp add: K_def card_gt_0_iff)
Andreas@51291
   764
    finally show "?lhs x = 1" .
Andreas@51291
   765
  qed
Andreas@51291
   766
  also have "nat \<dots> = card (\<Union>A)" by simp
Andreas@51291
   767
  finally show ?thesis ..
Andreas@51291
   768
qed
Andreas@51291
   769
nipkow@58193
   770
text{* The number of nat lists of length @{text m} summing to @{text N} is
nipkow@58193
   771
@{term "(N + m - 1) choose N"}: *} 
nipkow@58193
   772
nipkow@58193
   773
lemma card_length_listsum_rec:
nipkow@58193
   774
  assumes "m\<ge>1"
nipkow@58193
   775
  shows "card {l::nat list. length l = m \<and> listsum l = N} =
nipkow@58193
   776
    (card {l. length l = (m - 1) \<and> listsum l = N} +
nipkow@58193
   777
    card {l. length l = m \<and> listsum l + 1 =  N})"
nipkow@58193
   778
    (is "card ?C = (card ?A + card ?B)")
nipkow@58193
   779
proof - 
nipkow@58193
   780
  let ?A'="{l. length l = m \<and> listsum l = N \<and> hd l = 0}"
nipkow@58193
   781
  let ?B'="{l. length l = m \<and> listsum l = N \<and> hd l \<noteq> 0}"
nipkow@58193
   782
  let ?f ="\<lambda> l. 0#l"
nipkow@58193
   783
  let ?g ="\<lambda> l. (hd l + 1) # tl l"
nipkow@58193
   784
  have 1: "\<And>xs x. xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" by simp
nipkow@58193
   785
  have 2: "\<And>xs. (xs::nat list) \<noteq> [] \<Longrightarrow> listsum(tl xs) = listsum xs - hd xs"
nipkow@58193
   786
    by(auto simp add: neq_Nil_conv)
nipkow@58193
   787
  have f: "bij_betw ?f ?A ?A'"
nipkow@58193
   788
    apply(rule bij_betw_byWitness[where f' = tl])
nipkow@58193
   789
    using assms 
nipkow@58193
   790
    by (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv)
nipkow@58193
   791
  have 3: "\<And>xs:: nat list. xs \<noteq> [] \<Longrightarrow> hd xs + (listsum xs - hd xs) = listsum xs"
nipkow@58193
   792
    by (metis 1 listsum_simps(2) 2)
nipkow@58193
   793
  have g: "bij_betw ?g ?B ?B'"
nipkow@58193
   794
    apply(rule bij_betw_byWitness[where f' = "\<lambda> l. (hd l - 1) # tl l"])
nipkow@58193
   795
    using assms
nipkow@58193
   796
    by (auto simp: 2 length_0_conv[symmetric] intro!: 3
nipkow@58193
   797
      simp del: length_greater_0_conv length_0_conv)
nipkow@58193
   798
  { fix M N :: nat have "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}"
nipkow@58193
   799
    using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto }
nipkow@58193
   800
    note fin = this
nipkow@58193
   801
  have fin_A: "finite ?A" using fin[of _ "N+1"]
nipkow@58193
   802
    by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"], 
nipkow@58193
   803
      auto simp: member_le_listsum_nat less_Suc_eq_le)
nipkow@58193
   804
  have fin_B: "finite ?B"
nipkow@58193
   805
    by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"], 
nipkow@58193
   806
      auto simp: member_le_listsum_nat less_Suc_eq_le fin)
nipkow@58193
   807
  have uni: "?C = ?A' \<union> ?B'" by auto
nipkow@58193
   808
  have disj: "?A' \<inter> ?B' = {}" by auto
nipkow@58193
   809
  have "card ?C = card(?A' \<union> ?B')" using uni by simp
nipkow@58193
   810
  also have "\<dots> = card ?A + card ?B"
nipkow@58193
   811
    using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g]
nipkow@58193
   812
      bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B
nipkow@58193
   813
    by presburger
nipkow@58193
   814
  finally show ?thesis .
nipkow@58193
   815
qed
nipkow@58193
   816
lp15@58194
   817
lemma card_length_listsum: --"By Holden Lee, tidied by Tobias Nipkow"
lp15@58194
   818
  "card {l::nat list. size l = m \<and> listsum l = N} = (N + m - 1) choose N"
lp15@58194
   819
proof (cases m)
lp15@58194
   820
  case 0 then show ?thesis
lp15@58194
   821
    by (cases N) (auto simp: cong: conj_cong)
nipkow@58193
   822
next
lp15@58194
   823
  case (Suc m')
lp15@58194
   824
    have m: "m\<ge>1" by (simp add: Suc)
lp15@58194
   825
    then show ?thesis
lp15@58194
   826
    proof (induct "N + m - 1" arbitrary: N m)
lp15@58194
   827
      case 0   -- "In the base case, the only solution is [0]."
lp15@58194
   828
      have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {[0]}"
lp15@58194
   829
        by (auto simp: length_Suc_conv)
lp15@58194
   830
      have "m=1 \<and> N=0" using 0 by linarith
lp15@58194
   831
      then show ?case by simp
lp15@58194
   832
    next
lp15@58194
   833
      case (Suc k)
lp15@58194
   834
      
lp15@58194
   835
      have c1: "card {l::nat list. size l = (m - 1) \<and> listsum l =  N} = 
lp15@58194
   836
        (N + (m - 1) - 1) choose N"
lp15@58194
   837
      proof cases
lp15@58194
   838
        assume "m = 1"
lp15@58194
   839
        with Suc.hyps have "N\<ge>1" by auto
lp15@58194
   840
        with `m = 1` show ?thesis by (simp add: binomial_eq_0)
lp15@58194
   841
      next
lp15@58194
   842
        assume "m \<noteq> 1" thus ?thesis using Suc by fastforce
lp15@58194
   843
      qed
lp15@58194
   844
    
lp15@58194
   845
      from Suc have c2: "card {l::nat list. size l = m \<and> listsum l + 1 = N} = 
lp15@58194
   846
        (if N>0 then ((N - 1) + m - 1) choose (N - 1) else 0)"
lp15@58194
   847
      proof -
lp15@58194
   848
        have aux: "\<And>m n. n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" by arith
lp15@58194
   849
        from Suc have "N>0 \<Longrightarrow>
lp15@58194
   850
          card {l::nat list. size l = m \<and> listsum l + 1 = N} = 
lp15@58194
   851
          ((N - 1) + m - 1) choose (N - 1)" by (simp add: aux)
lp15@58194
   852
        thus ?thesis by auto
lp15@58194
   853
      qed
lp15@58194
   854
    
lp15@58194
   855
      from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> listsum l = N} + 
lp15@58194
   856
          card {l::nat list. size l = m \<and> listsum l + 1 = N}) = (N + m - 1) choose N"
lp15@58194
   857
        by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def)
lp15@58194
   858
      thus ?case using card_length_listsum_rec[OF Suc.prems] by auto
lp15@58194
   859
    qed
nipkow@58193
   860
qed
nipkow@58193
   861
nipkow@31719
   862
end