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