src/HOL/Library/Binomial.thy
author haftmann
Fri Nov 01 18:51:14 2013 +0100 (2013-11-01)
changeset 54230 b1d955791529
parent 52903 6c89225ddeba
child 54489 03ff4d1e6784
permissions -rw-r--r--
more simplification rules on unary and binary minus
haftmann@35372
     1
(*  Title:      HOL/Library/Binomial.thy
chaieb@29694
     2
    Author:     Lawrence C Paulson, Amine Chaieb
wenzelm@21256
     3
    Copyright   1997  University of Cambridge
wenzelm@21256
     4
*)
wenzelm@21256
     5
wenzelm@21263
     6
header {* Binomial Coefficients *}
wenzelm@21256
     7
wenzelm@21256
     8
theory Binomial
haftmann@35372
     9
imports Complex_Main
wenzelm@21256
    10
begin
wenzelm@21256
    11
wenzelm@21263
    12
text {* This development is based on the work of Andy Gordon and
wenzelm@21263
    13
  Florian Kammueller. *}
wenzelm@21256
    14
wenzelm@52903
    15
primrec binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat" (infixl "choose" 65)
wenzelm@52903
    16
where
wenzelm@52903
    17
  "0 choose k = (if k = 0 then 1 else 0)"
wenzelm@52903
    18
| "Suc n choose k = (if k = 0 then 1 else (n choose (k - 1)) + (n choose k))"
wenzelm@21256
    19
wenzelm@21256
    20
lemma binomial_n_0 [simp]: "(n choose 0) = 1"
wenzelm@48830
    21
  by (cases n) simp_all
wenzelm@21256
    22
wenzelm@21256
    23
lemma binomial_0_Suc [simp]: "(0 choose Suc k) = 0"
wenzelm@48830
    24
  by simp
wenzelm@21256
    25
wenzelm@52903
    26
lemma binomial_Suc_Suc [simp]: "(Suc n choose Suc k) = (n choose k) + (n choose Suc k)"
wenzelm@48830
    27
  by simp
wenzelm@21256
    28
wenzelm@52903
    29
lemma binomial_eq_0: "n < k \<Longrightarrow> n choose k = 0"
wenzelm@52903
    30
  by (induct n arbitrary: k) auto
wenzelm@21256
    31
wenzelm@52903
    32
declare binomial.simps [simp del]
wenzelm@21256
    33
wenzelm@52903
    34
lemma binomial_n_n [simp]: "n choose n = 1"
wenzelm@48830
    35
  by (induct n) (simp_all add: binomial_eq_0)
wenzelm@21256
    36
wenzelm@52903
    37
lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n"
wenzelm@48830
    38
  by (induct n) simp_all
wenzelm@21256
    39
wenzelm@52903
    40
lemma binomial_1 [simp]: "n choose Suc 0 = n"
wenzelm@48830
    41
  by (induct n) simp_all
wenzelm@21256
    42
wenzelm@52903
    43
lemma zero_less_binomial: "k \<le> n \<Longrightarrow> n choose k > 0"
wenzelm@48830
    44
  by (induct n k rule: diff_induct) simp_all
wenzelm@21256
    45
wenzelm@52903
    46
lemma binomial_eq_0_iff: "n choose k = 0 \<longleftrightarrow> n < k"
wenzelm@48830
    47
  apply (safe intro!: binomial_eq_0)
wenzelm@48830
    48
  apply (erule contrapos_pp)
wenzelm@48830
    49
  apply (simp add: zero_less_binomial)
wenzelm@48830
    50
  done
wenzelm@21256
    51
wenzelm@52903
    52
lemma zero_less_binomial_iff: "n choose k > 0 \<longleftrightarrow> k \<le> n"
wenzelm@48830
    53
  by (simp add: linorder_not_less binomial_eq_0_iff neq0_conv[symmetric] del: neq0_conv)
wenzelm@21256
    54
wenzelm@21256
    55
(*Might be more useful if re-oriented*)
wenzelm@21263
    56
lemma Suc_times_binomial_eq:
wenzelm@52903
    57
  "k \<le> n \<Longrightarrow> Suc n * (n choose k) = (Suc n choose Suc k) * Suc k"
wenzelm@52903
    58
  apply (induct n arbitrary: k)
wenzelm@52903
    59
   apply (simp add: binomial.simps)
wenzelm@48830
    60
   apply (case_tac k)
wenzelm@48830
    61
  apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0)
wenzelm@48830
    62
  done
wenzelm@21256
    63
wenzelm@21256
    64
text{*This is the well-known version, but it's harder to use because of the
wenzelm@21256
    65
  need to reason about division.*}
wenzelm@21256
    66
lemma binomial_Suc_Suc_eq_times:
wenzelm@52903
    67
    "k \<le> n \<Longrightarrow> (Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k"
wenzelm@46507
    68
  by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right)
wenzelm@21256
    69
wenzelm@21256
    70
text{*Another version, with -1 instead of Suc.*}
wenzelm@21256
    71
lemma times_binomial_minus1_eq:
wenzelm@52903
    72
  "k \<le> n \<Longrightarrow> 0 < k \<Longrightarrow> (n choose k) * k = n * ((n - 1) choose (k - 1))"
wenzelm@52903
    73
  using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"]
wenzelm@52903
    74
  by (auto split add: nat_diff_split)
wenzelm@21263
    75
wenzelm@21256
    76
wenzelm@25378
    77
subsection {* Theorems about @{text "choose"} *}
wenzelm@21256
    78
wenzelm@21256
    79
text {*
wenzelm@21256
    80
  \medskip Basic theorem about @{text "choose"}.  By Florian
wenzelm@21256
    81
  Kamm\"uller, tidied by LCP.
wenzelm@21256
    82
*}
wenzelm@21256
    83
wenzelm@52903
    84
lemma card_s_0_eq_empty: "finite A \<Longrightarrow> card {B. B \<subseteq> A & card B = 0} = 1"
wenzelm@48830
    85
  by (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
wenzelm@21256
    86
wenzelm@52903
    87
lemma choose_deconstruct: "finite M \<Longrightarrow> x \<notin> M \<Longrightarrow>
wenzelm@52903
    88
    {s. s \<subseteq> insert x M \<and> card s = Suc k} =
wenzelm@52903
    89
    {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}"
wenzelm@21256
    90
  apply safe
wenzelm@48830
    91
     apply (auto intro: finite_subset [THEN card_insert_disjoint])
wenzelm@21256
    92
  apply (drule_tac x = "xa - {x}" in spec)
wenzelm@52903
    93
  apply (subgoal_tac "x \<notin> xa")
wenzelm@52903
    94
   apply auto
wenzelm@21256
    95
  apply (erule rev_mp, subst card_Diff_singleton)
wenzelm@48830
    96
    apply (auto intro: finite_subset)
wenzelm@21256
    97
  done
nipkow@29918
    98
(*
nipkow@29918
    99
lemma "finite(UN y. {x. P x y})"
nipkow@29918
   100
apply simp
nipkow@29918
   101
lemma Collect_ex_eq
nipkow@29918
   102
wenzelm@52903
   103
lemma "{x. \<exists>y. P x y} = (UN y. {x. P x y})"
nipkow@29918
   104
apply blast
nipkow@29918
   105
*)
nipkow@29918
   106
wenzelm@52903
   107
lemma finite_bex_subset [simp]:
wenzelm@52903
   108
  assumes "finite B"
wenzelm@52903
   109
    and "\<And>A. A \<subseteq> B \<Longrightarrow> finite {x. P x A}"
wenzelm@52903
   110
  shows "finite {x. \<exists>A \<subseteq> B. P x A}"
wenzelm@52903
   111
proof -
wenzelm@52903
   112
  have "{x. \<exists>A\<subseteq>B. P x A} = (\<Union>A \<in> Pow B. {x. P x A})" by blast
wenzelm@52903
   113
  with assms show ?thesis by simp
wenzelm@52903
   114
qed
wenzelm@21256
   115
wenzelm@21256
   116
text{*There are as many subsets of @{term A} having cardinality @{term k}
wenzelm@21256
   117
 as there are sets obtained from the former by inserting a fixed element
wenzelm@21256
   118
 @{term x} into each.*}
wenzelm@21256
   119
lemma constr_bij:
wenzelm@52903
   120
   "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow>
wenzelm@52903
   121
    card {B. \<exists>C. C \<subseteq> A \<and> card C = k \<and> B = insert x C} =
wenzelm@52903
   122
    card {B. B \<subseteq> A & card(B) = k}"
wenzelm@52903
   123
  apply (rule_tac f = "\<lambda>s. s - {x}" and g = "insert x" in card_bij_eq)
wenzelm@48830
   124
       apply (auto elim!: equalityE simp add: inj_on_def)
wenzelm@52903
   125
  apply (subst Diff_insert0)
wenzelm@52903
   126
   apply auto
wenzelm@48830
   127
  done
wenzelm@21256
   128
wenzelm@21256
   129
text {*
wenzelm@21256
   130
  Main theorem: combinatorial statement about number of subsets of a set.
wenzelm@21256
   131
*}
wenzelm@21256
   132
wenzelm@21256
   133
lemma n_sub_lemma:
wenzelm@52903
   134
    "finite A \<Longrightarrow> card {B. B \<subseteq> A \<and> card B = k} = (card A choose k)"
wenzelm@52903
   135
  apply (induct k arbitrary: A)
wenzelm@52903
   136
   apply (simp add: card_s_0_eq_empty)
wenzelm@52903
   137
   apply atomize
wenzelm@52903
   138
  apply (rotate_tac -1)
wenzelm@52903
   139
  apply (erule finite_induct)
wenzelm@21256
   140
   apply (simp_all (no_asm_simp) cong add: conj_cong
wenzelm@21256
   141
     add: card_s_0_eq_empty choose_deconstruct)
wenzelm@21256
   142
  apply (subst card_Un_disjoint)
wenzelm@21256
   143
     prefer 4 apply (force simp add: constr_bij)
wenzelm@21256
   144
    prefer 3 apply force
wenzelm@21256
   145
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
wenzelm@21256
   146
     finite_subset [of _ "Pow (insert x F)", standard])
wenzelm@21256
   147
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
wenzelm@21256
   148
  done
wenzelm@21256
   149
wenzelm@52903
   150
theorem n_subsets: "finite A \<Longrightarrow> card {B. B \<subseteq> A \<and> card B = k} = (card A choose k)"
wenzelm@21256
   151
  by (simp add: n_sub_lemma)
wenzelm@21256
   152
wenzelm@21256
   153
wenzelm@21256
   154
text{* The binomial theorem (courtesy of Tobias Nipkow): *}
wenzelm@21256
   155
wenzelm@52903
   156
theorem binomial: "(a + b::nat)^n = (\<Sum>k=0..n. (n choose k) * a^k * b^(n - k))"
wenzelm@21256
   157
proof (induct n)
wenzelm@52903
   158
  case 0
wenzelm@52903
   159
  then show ?case by simp
wenzelm@21256
   160
next
wenzelm@21256
   161
  case (Suc n)
wenzelm@21256
   162
  have decomp: "{0..n+1} = {0} \<union> {n+1} \<union> {1..n}"
wenzelm@21256
   163
    by (auto simp add:atLeastAtMost_def atLeast_def atMost_def)
wenzelm@21256
   164
  have decomp2: "{0..n} = {0} \<union> {1..n}"
wenzelm@21256
   165
    by (auto simp add:atLeastAtMost_def atLeast_def atMost_def)
wenzelm@52903
   166
  have "(a + b)^(n + 1) = (a + b) * (\<Sum>k=0..n. (n choose k) * a^k * b^(n - k))"
wenzelm@21256
   167
    using Suc by simp
wenzelm@21256
   168
  also have "\<dots> =  a*(\<Sum>k=0..n. (n choose k) * a^k * b^(n-k)) +
wenzelm@21256
   169
                   b*(\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
wenzelm@21263
   170
    by (rule nat_distrib)
wenzelm@21256
   171
  also have "\<dots> = (\<Sum>k=0..n. (n choose k) * a^(k+1) * b^(n-k)) +
wenzelm@21256
   172
                  (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k+1))"
wenzelm@21263
   173
    by (simp add: setsum_right_distrib mult_ac)
wenzelm@21256
   174
  also have "\<dots> = (\<Sum>k=0..n. (n choose k) * a^k * b^(n+1-k)) +
wenzelm@21256
   175
                  (\<Sum>k=1..n+1. (n choose (k - 1)) * a^k * b^(n+1-k))"
wenzelm@21256
   176
    by (simp add:setsum_shift_bounds_cl_Suc_ivl Suc_diff_le
wenzelm@21256
   177
             del:setsum_cl_ivl_Suc)
wenzelm@21256
   178
  also have "\<dots> = a^(n+1) + b^(n+1) +
wenzelm@21256
   179
                  (\<Sum>k=1..n. (n choose (k - 1)) * a^k * b^(n+1-k)) +
wenzelm@21256
   180
                  (\<Sum>k=1..n. (n choose k) * a^k * b^(n+1-k))"
wenzelm@21263
   181
    by (simp add: decomp2)
wenzelm@21256
   182
  also have
wenzelm@21263
   183
      "\<dots> = a^(n+1) + b^(n+1) + (\<Sum>k=1..n. (n+1 choose k) * a^k * b^(n+1-k))"
wenzelm@21263
   184
    by (simp add: nat_distrib setsum_addf binomial.simps)
wenzelm@21256
   185
  also have "\<dots> = (\<Sum>k=0..n+1. (n+1 choose k) * a^k * b^(n+1-k))"
wenzelm@21256
   186
    using decomp by simp
wenzelm@21256
   187
  finally show ?case by simp
wenzelm@21256
   188
qed
wenzelm@21256
   189
huffman@29906
   190
subsection{* Pochhammer's symbol : generalized raising factorial*}
chaieb@29694
   191
wenzelm@52903
   192
definition "pochhammer (a::'a::comm_semiring_1) n =
wenzelm@52903
   193
  (if n = 0 then 1 else setprod (\<lambda>n. a + of_nat n) {0 .. n - 1})"
chaieb@29694
   194
wenzelm@52903
   195
lemma pochhammer_0 [simp]: "pochhammer a 0 = 1"
chaieb@29694
   196
  by (simp add: pochhammer_def)
chaieb@29694
   197
wenzelm@52903
   198
lemma pochhammer_1 [simp]: "pochhammer a 1 = a"
wenzelm@52903
   199
  by (simp add: pochhammer_def)
wenzelm@52903
   200
wenzelm@52903
   201
lemma pochhammer_Suc0 [simp]: "pochhammer a (Suc 0) = a"
chaieb@29694
   202
  by (simp add: pochhammer_def)
chaieb@29694
   203
chaieb@29694
   204
lemma pochhammer_Suc_setprod: "pochhammer a (Suc n) = setprod (\<lambda>n. a + of_nat n) {0 .. n}"
chaieb@29694
   205
  by (simp add: pochhammer_def)
chaieb@29694
   206
chaieb@29694
   207
lemma setprod_nat_ivl_Suc: "setprod f {0 .. Suc n} = setprod f {0..n} * f (Suc n)"
wenzelm@52903
   208
proof -
wenzelm@52903
   209
  have "{0..Suc n} = {0..n} \<union> {Suc n}" by auto
wenzelm@52903
   210
  then show ?thesis by (simp add: field_simps)
chaieb@29694
   211
qed
chaieb@29694
   212
chaieb@29694
   213
lemma setprod_nat_ivl_1_Suc: "setprod f {0 .. Suc n} = f 0 * setprod f {1.. Suc n}"
wenzelm@52903
   214
proof -
wenzelm@52903
   215
  have "{0..Suc n} = {0} \<union> {1 .. Suc n}" by auto
wenzelm@52903
   216
  then show ?thesis by simp
chaieb@29694
   217
qed
chaieb@29694
   218
chaieb@29694
   219
chaieb@29694
   220
lemma pochhammer_Suc: "pochhammer a (Suc n) = pochhammer a n * (a + of_nat n)"
wenzelm@52903
   221
proof (cases n)
wenzelm@52903
   222
  case 0
wenzelm@52903
   223
  then show ?thesis by simp
wenzelm@52903
   224
next
wenzelm@52903
   225
  case (Suc n)
wenzelm@52903
   226
  show ?thesis unfolding Suc pochhammer_Suc_setprod setprod_nat_ivl_Suc ..
wenzelm@48830
   227
qed
chaieb@29694
   228
chaieb@29694
   229
lemma pochhammer_rec: "pochhammer a (Suc n) = a * pochhammer (a + 1) n"
wenzelm@52903
   230
proof (cases "n = 0")
wenzelm@52903
   231
  case True
wenzelm@52903
   232
  then show ?thesis by (simp add: pochhammer_Suc_setprod)
wenzelm@52903
   233
next
wenzelm@52903
   234
  case False
wenzelm@52903
   235
  have *: "finite {1 .. n}" "0 \<notin> {1 .. n}" by auto
wenzelm@52903
   236
  have eq: "insert 0 {1 .. n} = {0..n}" by auto
wenzelm@52903
   237
  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)"
wenzelm@52903
   238
    apply (rule setprod_reindex_cong [where f = Suc])
wenzelm@52903
   239
    using False
wenzelm@52903
   240
    apply (auto simp add: fun_eq_iff field_simps)
wenzelm@52903
   241
    done
wenzelm@52903
   242
  show ?thesis
wenzelm@52903
   243
    apply (simp add: pochhammer_def)
wenzelm@52903
   244
    unfolding setprod_insert [OF *, unfolded eq]
wenzelm@52903
   245
    using ** apply (simp add: field_simps)
wenzelm@52903
   246
    done
chaieb@29694
   247
qed
chaieb@29694
   248
chaieb@29694
   249
lemma pochhammer_fact: "of_nat (fact n) = pochhammer 1 n"
avigad@32042
   250
  unfolding fact_altdef_nat
wenzelm@48830
   251
  apply (cases n)
wenzelm@48830
   252
   apply (simp_all add: of_nat_setprod pochhammer_Suc_setprod)
chaieb@29694
   253
  apply (rule setprod_reindex_cong[where f=Suc])
wenzelm@48830
   254
    apply (auto simp add: fun_eq_iff)
wenzelm@48830
   255
  done
chaieb@29694
   256
wenzelm@48830
   257
lemma pochhammer_of_nat_eq_0_lemma:
wenzelm@52903
   258
  assumes "k > n"
chaieb@29694
   259
  shows "pochhammer (- (of_nat n :: 'a:: idom)) k = 0"
wenzelm@52903
   260
proof (cases "n = 0")
wenzelm@52903
   261
  case True
wenzelm@52903
   262
  then show ?thesis
wenzelm@52903
   263
    using assms by (cases k) (simp_all add: pochhammer_rec)
wenzelm@52903
   264
next
wenzelm@52903
   265
  case False
wenzelm@52903
   266
  from assms obtain h where "k = Suc h" by (cases k) auto
wenzelm@52903
   267
  then show ?thesis
wenzelm@52903
   268
    apply (simp add: pochhammer_Suc_setprod)
wenzelm@52903
   269
    apply (rule_tac x="n" in bexI)
wenzelm@52903
   270
    using assms
wenzelm@52903
   271
    apply auto
wenzelm@52903
   272
    done
chaieb@29694
   273
qed
chaieb@29694
   274
wenzelm@52903
   275
lemma pochhammer_of_nat_eq_0_lemma':
wenzelm@52903
   276
  assumes kn: "k \<le> n"
wenzelm@52903
   277
  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k \<noteq> 0"
wenzelm@52903
   278
proof (cases k)
wenzelm@52903
   279
  case 0
wenzelm@52903
   280
  then show ?thesis by simp
wenzelm@52903
   281
next
wenzelm@52903
   282
  case (Suc h)
wenzelm@52903
   283
  then show ?thesis
wenzelm@52903
   284
    apply (simp add: pochhammer_Suc_setprod)
wenzelm@52903
   285
    using Suc kn apply (auto simp add: algebra_simps)
wenzelm@52903
   286
    done
chaieb@29694
   287
qed
chaieb@29694
   288
wenzelm@48830
   289
lemma pochhammer_of_nat_eq_0_iff:
wenzelm@52903
   290
  shows "pochhammer (- (of_nat n :: 'a:: {idom,ring_char_0})) k = 0 \<longleftrightarrow> k > n"
chaieb@29694
   291
  (is "?l = ?r")
wenzelm@48830
   292
  using pochhammer_of_nat_eq_0_lemma[of n k, where ?'a='a]
chaieb@29694
   293
    pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]
chaieb@29694
   294
  by (auto simp add: not_le[symmetric])
chaieb@29694
   295
chaieb@32159
   296
wenzelm@52903
   297
lemma pochhammer_eq_0_iff: "pochhammer a n = (0::'a::field_char_0) \<longleftrightarrow> (\<exists>k < n. a = - of_nat k)"
chaieb@32159
   298
  apply (auto simp add: pochhammer_of_nat_eq_0_iff)
wenzelm@48830
   299
  apply (cases n)
wenzelm@48830
   300
   apply (auto simp add: pochhammer_def algebra_simps group_add_class.eq_neg_iff_add_eq_0)
chaieb@32159
   301
  apply (rule_tac x=x in exI)
chaieb@32159
   302
  apply auto
chaieb@32159
   303
  done
chaieb@32159
   304
chaieb@32159
   305
wenzelm@48830
   306
lemma pochhammer_eq_0_mono:
chaieb@32159
   307
  "pochhammer a n = (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a m = 0"
wenzelm@48830
   308
  unfolding pochhammer_eq_0_iff by auto
chaieb@32159
   309
wenzelm@48830
   310
lemma pochhammer_neq_0_mono:
chaieb@32159
   311
  "pochhammer a m \<noteq> (0::'a::field_char_0) \<Longrightarrow> m \<ge> n \<Longrightarrow> pochhammer a n \<noteq> 0"
wenzelm@48830
   312
  unfolding pochhammer_eq_0_iff by auto
chaieb@32159
   313
chaieb@32159
   314
lemma pochhammer_minus:
wenzelm@48830
   315
  assumes kn: "k \<le> n"
chaieb@32159
   316
  shows "pochhammer (- b) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (b - of_nat k + 1) k"
wenzelm@52903
   317
proof (cases k)
wenzelm@52903
   318
  case 0
wenzelm@52903
   319
  then show ?thesis by simp
wenzelm@52903
   320
next
wenzelm@52903
   321
  case (Suc h)
wenzelm@52903
   322
  have eq: "((- 1) ^ Suc h :: 'a) = setprod (%i. - 1) {0 .. h}"
wenzelm@52903
   323
    using setprod_constant[where A="{0 .. h}" and y="- 1 :: 'a"]
wenzelm@52903
   324
    by auto
wenzelm@52903
   325
  show ?thesis
wenzelm@52903
   326
    unfolding Suc pochhammer_Suc_setprod eq setprod_timesf[symmetric]
wenzelm@52903
   327
    apply (rule strong_setprod_reindex_cong[where f = "%i. h - i"])
wenzelm@52903
   328
    using Suc
wenzelm@52903
   329
    apply (auto simp add: inj_on_def image_def)
wenzelm@52903
   330
    apply (rule_tac x="h - x" in bexI)
wenzelm@52903
   331
    apply (auto simp add: fun_eq_iff of_nat_diff)
wenzelm@52903
   332
    done
chaieb@32159
   333
qed
chaieb@32159
   334
chaieb@32159
   335
lemma pochhammer_minus':
wenzelm@48830
   336
  assumes kn: "k \<le> n"
chaieb@32159
   337
  shows "pochhammer (b - of_nat k + 1) k = ((- 1) ^ k :: 'a::comm_ring_1) * pochhammer (- b) k"
chaieb@32159
   338
  unfolding pochhammer_minus[OF kn, where b=b]
chaieb@32159
   339
  unfolding mult_assoc[symmetric]
chaieb@32159
   340
  unfolding power_add[symmetric]
wenzelm@52903
   341
  by simp
chaieb@32159
   342
wenzelm@52903
   343
lemma pochhammer_same: "pochhammer (- of_nat n) n =
wenzelm@52903
   344
    ((- 1) ^ n :: 'a::comm_ring_1) * of_nat (fact n)"
chaieb@32159
   345
  unfolding pochhammer_minus[OF le_refl[of n]]
chaieb@32159
   346
  by (simp add: of_nat_diff pochhammer_fact)
chaieb@32159
   347
wenzelm@52903
   348
huffman@29906
   349
subsection{* Generalized binomial coefficients *}
chaieb@29694
   350
huffman@31287
   351
definition gbinomial :: "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
wenzelm@48830
   352
  where "a gchoose n =
wenzelm@48830
   353
    (if n = 0 then 1 else (setprod (\<lambda>i. a - of_nat i) {0 .. n - 1}) / of_nat (fact n))"
chaieb@29694
   354
wenzelm@52903
   355
lemma gbinomial_0 [simp]: "a gchoose 0 = 1" "0 gchoose (Suc n) = 0"
wenzelm@48830
   356
  apply (simp_all add: gbinomial_def)
wenzelm@48830
   357
  apply (subgoal_tac "(\<Prod>i\<Colon>nat\<in>{0\<Colon>nat..n}. - of_nat i) = (0::'b)")
wenzelm@48830
   358
   apply (simp del:setprod_zero_iff)
wenzelm@48830
   359
  apply simp
wenzelm@48830
   360
  done
chaieb@29694
   361
chaieb@29694
   362
lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / of_nat (fact n)"
wenzelm@52903
   363
proof (cases "n = 0")
wenzelm@52903
   364
  case True
wenzelm@52903
   365
  then show ?thesis by simp
wenzelm@52903
   366
next
wenzelm@52903
   367
  case False
wenzelm@52903
   368
  from this setprod_constant[of "{0 .. n - 1}" "- (1:: 'a)"]
wenzelm@52903
   369
  have eq: "(- (1\<Colon>'a)) ^ n = setprod (\<lambda>i. - 1) {0 .. n - 1}"
wenzelm@52903
   370
    by auto
wenzelm@52903
   371
  from False show ?thesis
wenzelm@52903
   372
    by (simp add: pochhammer_def gbinomial_def field_simps
wenzelm@52903
   373
      eq setprod_timesf[symmetric] del: minus_one)
chaieb@29694
   374
qed
chaieb@29694
   375
wenzelm@48830
   376
lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n"
wenzelm@48830
   377
proof (induct n arbitrary: k rule: nat_less_induct)
chaieb@29694
   378
  fix n k assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) =
chaieb@29694
   379
                      fact m" and kn: "k \<le> n"
wenzelm@48830
   380
  let ?ths = "fact k * fact (n - k) * (n choose k) = fact n"
wenzelm@48830
   381
  { assume "n=0" then have ?ths using kn by simp }
chaieb@29694
   382
  moreover
wenzelm@48830
   383
  { assume "k=0" then have ?ths using kn by simp }
chaieb@29694
   384
  moreover
wenzelm@48830
   385
  { assume nk: "n=k" then have ?ths by simp }
chaieb@29694
   386
  moreover
wenzelm@48830
   387
  { fix m h assume n: "n = Suc m" and h: "k = Suc h" and hm: "h < m"
chaieb@29694
   388
    from n have mn: "m < n" by arith
chaieb@29694
   389
    from hm have hm': "h \<le> m" by arith
chaieb@29694
   390
    from hm h n kn have km: "k \<le> m" by arith
wenzelm@48830
   391
    have "m - h = Suc (m - Suc h)" using  h km hm by arith
chaieb@29694
   392
    with km h have th0: "fact (m - h) = (m - h) * fact (m - k)"
chaieb@29694
   393
      by simp
wenzelm@48830
   394
    from n h th0
wenzelm@48830
   395
    have "fact k * fact (n - k) * (n choose k) =
wenzelm@52903
   396
        k * (fact h * fact (m - h) * (m choose h)) + 
wenzelm@52903
   397
        (m - h) * (fact k * fact (m - k) * (m choose k))"
haftmann@36350
   398
      by (simp add: field_simps)
chaieb@29694
   399
    also have "\<dots> = (k + (m - h)) * fact m"
chaieb@29694
   400
      using H[rule_format, OF mn hm'] H[rule_format, OF mn km]
haftmann@36350
   401
      by (simp add: field_simps)
wenzelm@48830
   402
    finally have ?ths using h n km by simp }
wenzelm@52903
   403
  moreover have "n=0 \<or> k = 0 \<or> k = n \<or> (\<exists>m h. n = Suc m \<and> k = Suc h \<and> h < m)"
wenzelm@48830
   404
    using kn by presburger
chaieb@29694
   405
  ultimately show ?ths by blast
chaieb@29694
   406
qed
wenzelm@48830
   407
wenzelm@48830
   408
lemma binomial_fact:
wenzelm@48830
   409
  assumes kn: "k \<le> n"
wenzelm@48830
   410
  shows "(of_nat (n choose k) :: 'a::field_char_0) =
wenzelm@48830
   411
    of_nat (fact n) / (of_nat (fact k) * of_nat (fact (n - k)))"
chaieb@29694
   412
  using binomial_fact_lemma[OF kn]
haftmann@36350
   413
  by (simp add: field_simps of_nat_mult [symmetric])
chaieb@29694
   414
chaieb@29694
   415
lemma binomial_gbinomial: "of_nat (n choose k) = of_nat n gchoose k"
wenzelm@48830
   416
proof -
wenzelm@48830
   417
  { assume kn: "k > n"
wenzelm@48830
   418
    from kn binomial_eq_0[OF kn] have ?thesis
wenzelm@48830
   419
      by (simp add: gbinomial_pochhammer field_simps  pochhammer_of_nat_eq_0_iff) }
chaieb@29694
   420
  moreover
wenzelm@48830
   421
  { assume "k=0" then have ?thesis by simp }
chaieb@29694
   422
  moreover
wenzelm@48830
   423
  { assume kn: "k \<le> n" and k0: "k\<noteq> 0"
wenzelm@48830
   424
    from k0 obtain h where h: "k = Suc h" by (cases k) auto
chaieb@29694
   425
    from h
chaieb@29694
   426
    have eq:"(- 1 :: 'a) ^ k = setprod (\<lambda>i. - 1) {0..h}"
wenzelm@52903
   427
      by (subst setprod_constant) auto
chaieb@29694
   428
    have eq': "(\<Prod>i\<in>{0..h}. of_nat n + - (of_nat i :: 'a)) = (\<Prod>i\<in>{n - h..n}. of_nat i)"
chaieb@29694
   429
      apply (rule strong_setprod_reindex_cong[where f="op - n"])
wenzelm@48830
   430
        using h kn
wenzelm@48830
   431
        apply (simp_all add: inj_on_def image_iff Bex_def set_eq_iff)
wenzelm@48830
   432
        apply clarsimp
wenzelm@48830
   433
        apply presburger
wenzelm@48830
   434
       apply presburger
wenzelm@48830
   435
      apply (simp add: fun_eq_iff field_simps of_nat_add[symmetric] del: of_nat_add)
wenzelm@48830
   436
      done
wenzelm@48830
   437
    have th0: "finite {1..n - Suc h}" "finite {n - h .. n}"
wenzelm@48830
   438
        "{1..n - Suc h} \<inter> {n - h .. n} = {}" and
wenzelm@48830
   439
        eq3: "{1..n - Suc h} \<union> {n - h .. n} = {1..n}"
wenzelm@48830
   440
      using h kn by auto
chaieb@29694
   441
    from eq[symmetric]
chaieb@29694
   442
    have ?thesis using kn
wenzelm@48830
   443
      apply (simp add: binomial_fact[OF kn, where ?'a = 'a]
huffman@47108
   444
        gbinomial_pochhammer field_simps pochhammer_Suc_setprod del: minus_one)
wenzelm@48830
   445
      apply (simp add: pochhammer_Suc_setprod fact_altdef_nat h
wenzelm@48830
   446
        of_nat_setprod setprod_timesf[symmetric] eq' del: One_nat_def power_Suc del: minus_one)
chaieb@29694
   447
      unfolding setprod_Un_disjoint[OF th0, unfolded eq3, of "of_nat:: nat \<Rightarrow> 'a"] eq[unfolded h]
wenzelm@48830
   448
      unfolding mult_assoc[symmetric]
chaieb@29694
   449
      unfolding setprod_timesf[symmetric]
chaieb@29694
   450
      apply simp
chaieb@29694
   451
      apply (rule strong_setprod_reindex_cong[where f= "op - n"])
wenzelm@48830
   452
        apply (auto simp add: inj_on_def image_iff Bex_def)
wenzelm@48830
   453
       apply presburger
chaieb@29694
   454
      apply (subgoal_tac "(of_nat (n - x) :: 'a) = of_nat n - of_nat x")
wenzelm@48830
   455
       apply simp
wenzelm@48830
   456
      apply (rule of_nat_diff)
chaieb@29694
   457
      apply simp
wenzelm@48830
   458
      done
chaieb@29694
   459
  }
chaieb@29694
   460
  moreover
chaieb@29694
   461
  have "k > n \<or> k = 0 \<or> (k \<le> n \<and> k \<noteq> 0)" by arith
chaieb@29694
   462
  ultimately show ?thesis by blast
chaieb@29694
   463
qed
chaieb@29694
   464
chaieb@29694
   465
lemma gbinomial_1[simp]: "a gchoose 1 = a"
chaieb@29694
   466
  by (simp add: gbinomial_def)
chaieb@29694
   467
chaieb@29694
   468
lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a"
chaieb@29694
   469
  by (simp add: gbinomial_def)
chaieb@29694
   470
wenzelm@48830
   471
lemma gbinomial_mult_1:
wenzelm@48830
   472
  "a * (a gchoose n) =
wenzelm@48830
   473
    of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"  (is "?l = ?r")
wenzelm@48830
   474
proof -
chaieb@29694
   475
  have "?r = ((- 1) ^n * pochhammer (- a) n / of_nat (fact n)) * (of_nat n - (- a + of_nat n))"
chaieb@29694
   476
    unfolding gbinomial_pochhammer
wenzelm@48830
   477
      pochhammer_Suc fact_Suc of_nat_mult right_diff_distrib power_Suc
haftmann@36350
   478
    by (simp add:  field_simps del: of_nat_Suc)
chaieb@29694
   479
  also have "\<dots> = ?l" unfolding gbinomial_pochhammer
haftmann@36350
   480
    by (simp add: field_simps)
chaieb@29694
   481
  finally show ?thesis ..
chaieb@29694
   482
qed
chaieb@29694
   483
wenzelm@48830
   484
lemma gbinomial_mult_1':
wenzelm@48830
   485
    "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))"
chaieb@29694
   486
  by (simp add: mult_commute gbinomial_mult_1)
chaieb@29694
   487
wenzelm@48830
   488
lemma gbinomial_Suc:
wenzelm@48830
   489
    "a gchoose (Suc k) = (setprod (\<lambda>i. a - of_nat i) {0 .. k}) / of_nat (fact (Suc k))"
chaieb@29694
   490
  by (simp add: gbinomial_def)
wenzelm@48830
   491
chaieb@29694
   492
lemma gbinomial_mult_fact:
wenzelm@48830
   493
  "(of_nat (fact (Suc k)) :: 'a) * ((a::'a::field_char_0) gchoose (Suc k)) =
wenzelm@48830
   494
    (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
wenzelm@48830
   495
  by (simp_all add: gbinomial_Suc field_simps del: fact_Suc)
chaieb@29694
   496
chaieb@29694
   497
lemma gbinomial_mult_fact':
wenzelm@48830
   498
  "((a::'a::field_char_0) gchoose (Suc k)) * (of_nat (fact (Suc k)) :: 'a) =
wenzelm@48830
   499
    (setprod (\<lambda>i. a - of_nat i) {0 .. k})"
chaieb@29694
   500
  using gbinomial_mult_fact[of k a]
wenzelm@52903
   501
  by (subst mult_commute)
chaieb@29694
   502
wenzelm@48830
   503
wenzelm@48830
   504
lemma gbinomial_Suc_Suc:
wenzelm@48830
   505
  "((a::'a::field_char_0) + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))"
wenzelm@52903
   506
proof (cases k)
wenzelm@52903
   507
  case 0
wenzelm@52903
   508
  then show ?thesis by simp
wenzelm@52903
   509
next
wenzelm@52903
   510
  case (Suc h)
wenzelm@52903
   511
  have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)"
wenzelm@52903
   512
    apply (rule strong_setprod_reindex_cong[where f = Suc])
wenzelm@52903
   513
      using Suc
wenzelm@52903
   514
      apply auto
wenzelm@52903
   515
    done
chaieb@29694
   516
wenzelm@52903
   517
  have "of_nat (fact (Suc k)) * (a gchoose k + (a gchoose (Suc k))) =
wenzelm@52903
   518
    ((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)"
wenzelm@52903
   519
    apply (simp add: Suc field_simps del: fact_Suc)
wenzelm@52903
   520
    unfolding gbinomial_mult_fact'
wenzelm@52903
   521
    apply (subst fact_Suc)
wenzelm@52903
   522
    unfolding of_nat_mult
wenzelm@52903
   523
    apply (subst mult_commute)
wenzelm@52903
   524
    unfolding mult_assoc
wenzelm@52903
   525
    unfolding gbinomial_mult_fact
wenzelm@52903
   526
    apply (simp add: field_simps)
wenzelm@52903
   527
    done
wenzelm@52903
   528
  also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)"
wenzelm@52903
   529
    unfolding gbinomial_mult_fact' setprod_nat_ivl_Suc
wenzelm@52903
   530
    by (simp add: field_simps Suc)
wenzelm@52903
   531
  also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)"
wenzelm@52903
   532
    using eq0
wenzelm@52903
   533
    by (simp add: Suc setprod_nat_ivl_1_Suc)
wenzelm@52903
   534
  also have "\<dots> = of_nat (fact (Suc k)) * ((a + 1) gchoose (Suc k))"
wenzelm@52903
   535
    unfolding gbinomial_mult_fact ..
wenzelm@52903
   536
  finally show ?thesis by (simp del: fact_Suc)
chaieb@29694
   537
qed
chaieb@29694
   538
chaieb@32158
   539
wenzelm@48830
   540
lemma binomial_symmetric:
wenzelm@48830
   541
  assumes kn: "k \<le> n"
chaieb@32158
   542
  shows "n choose k = n choose (n - k)"
chaieb@32158
   543
proof-
chaieb@32158
   544
  from kn have kn': "n - k \<le> n" by arith
chaieb@32158
   545
  from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn']
wenzelm@48830
   546
  have "fact k * fact (n - k) * (n choose k) =
wenzelm@48830
   547
    fact (n - k) * fact (n - (n - k)) * (n choose (n - k))" by simp
chaieb@32158
   548
  then show ?thesis using kn by simp
chaieb@32158
   549
qed
chaieb@32158
   550
hoelzl@50224
   551
(* Contributed by Manuel Eberl *)
hoelzl@50224
   552
(* Alternative definition of the binomial coefficient as \<Prod>i<k. (n - i) / (k - i) *)
hoelzl@50224
   553
lemma binomial_altdef_of_nat:
wenzelm@52903
   554
  fixes n k :: nat
wenzelm@52903
   555
    and x :: "'a :: {field_char_0,field_inverse_zero}"
wenzelm@52903
   556
  assumes "k \<le> n"
wenzelm@52903
   557
  shows "of_nat (n choose k) = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
wenzelm@52903
   558
proof (cases "0 < k")
wenzelm@52903
   559
  case True
hoelzl@50224
   560
  then have "(of_nat (n choose k) :: 'a) = (\<Prod>i<k. of_nat n - of_nat i) / of_nat (fact k)"
hoelzl@50224
   561
    unfolding binomial_gbinomial gbinomial_def
hoelzl@50224
   562
    by (auto simp: gr0_conv_Suc lessThan_Suc_atMost atLeast0AtMost)
hoelzl@50224
   563
  also have "\<dots> = (\<Prod>i<k. of_nat (n - i) / of_nat (k - i) :: 'a)"
hoelzl@50224
   564
    using `k \<le> n` unfolding fact_eq_rev_setprod_nat of_nat_setprod
hoelzl@50224
   565
    by (auto simp add: setprod_dividef intro!: setprod_cong of_nat_diff[symmetric])
hoelzl@50224
   566
  finally show ?thesis .
wenzelm@52903
   567
next
wenzelm@52903
   568
  case False
wenzelm@52903
   569
  then show ?thesis by simp
wenzelm@52903
   570
qed
hoelzl@50224
   571
hoelzl@50224
   572
lemma binomial_ge_n_over_k_pow_k:
wenzelm@52903
   573
  fixes k n :: nat
wenzelm@52903
   574
    and x :: "'a :: linordered_field_inverse_zero"
wenzelm@52903
   575
  assumes "0 < k"
wenzelm@52903
   576
    and "k \<le> n"
wenzelm@52903
   577
  shows "(of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)"
hoelzl@50224
   578
proof -
hoelzl@50224
   579
  have "(of_nat n / of_nat k :: 'a) ^ k = (\<Prod>i<k. of_nat n / of_nat k :: 'a)"
hoelzl@50224
   580
    by (simp add: setprod_constant)
hoelzl@50224
   581
  also have "\<dots> \<le> of_nat (n choose k)"
hoelzl@50224
   582
    unfolding binomial_altdef_of_nat[OF `k\<le>n`]
hoelzl@50224
   583
  proof (safe intro!: setprod_mono)
wenzelm@52903
   584
    fix i :: nat
wenzelm@52903
   585
    assume  "i < k"
hoelzl@50224
   586
    from assms have "n * i \<ge> i * k" by simp
wenzelm@52903
   587
    then have "n * k - n * i \<le> n * k - i * k" by arith
wenzelm@52903
   588
    then have "n * (k - i) \<le> (n - i) * k"
hoelzl@50224
   589
      by (simp add: diff_mult_distrib2 nat_mult_commute)
wenzelm@52903
   590
    then have "of_nat n * of_nat (k - i) \<le> of_nat (n - i) * (of_nat k :: 'a)"
hoelzl@50224
   591
      unfolding of_nat_mult[symmetric] of_nat_le_iff .
hoelzl@50224
   592
    with assms show "of_nat n / of_nat k \<le> of_nat (n - i) / (of_nat (k - i) :: 'a)"
hoelzl@50224
   593
      using `i < k` by (simp add: field_simps)
hoelzl@50224
   594
  qed (simp add: zero_le_divide_iff)
hoelzl@50224
   595
  finally show ?thesis .
hoelzl@50224
   596
qed
hoelzl@50224
   597
hoelzl@50240
   598
lemma binomial_le_pow:
wenzelm@52903
   599
  assumes "r \<le> n"
wenzelm@52903
   600
  shows "n choose r \<le> n ^ r"
hoelzl@50240
   601
proof -
hoelzl@50240
   602
  have "n choose r \<le> fact n div fact (n - r)"
hoelzl@50240
   603
    using `r \<le> n` by (subst binomial_fact_lemma[symmetric]) auto
wenzelm@52903
   604
  with fact_div_fact_le_pow [OF assms] show ?thesis by auto
hoelzl@50240
   605
qed
hoelzl@50240
   606
hoelzl@50240
   607
lemma binomial_altdef_nat: "(k::nat) \<le> n \<Longrightarrow>
hoelzl@50240
   608
    n choose k = fact n div (fact k * fact (n - k))"
wenzelm@52903
   609
 by (subst binomial_fact_lemma [symmetric]) auto
hoelzl@50240
   610
wenzelm@21256
   611
end