1(* Title: HOL/Binomial.thy 2 Author: Jacques D. Fleuriot 3 Author: Lawrence C Paulson 4 Author: Jeremy Avigad 5 Author: Chaitanya Mangla 6 Author: Manuel Eberl 7*) 8 9section \<open>Binomial Coefficients and Binomial Theorem\<close> 10 11theory Binomial 12 imports Presburger Factorial 13begin 14 15subsection \<open>Binomial coefficients\<close> 16 17text \<open>This development is based on the work of Andy Gordon and Florian Kammueller.\<close> 18 19text \<open>Combinatorial definition\<close> 20 21definition binomial :: "nat \<Rightarrow> nat \<Rightarrow> nat" (infixl "choose" 65) 22 where "n choose k = card {K\<in>Pow {0..<n}. card K = k}" 23 24theorem n_subsets: 25 assumes "finite A" 26 shows "card {B. B \<subseteq> A \<and> card B = k} = card A choose k" 27proof - 28 from assms obtain f where bij: "bij_betw f {0..<card A} A" 29 by (blast dest: ex_bij_betw_nat_finite) 30 then have [simp]: "card (f ` C) = card C" if "C \<subseteq> {0..<card A}" for C 31 by (meson bij_betw_imp_inj_on bij_betw_subset card_image that) 32 from bij have "bij_betw (image f) (Pow {0..<card A}) (Pow A)" 33 by (rule bij_betw_Pow) 34 then have "inj_on (image f) (Pow {0..<card A})" 35 by (rule bij_betw_imp_inj_on) 36 moreover have "{K. K \<subseteq> {0..<card A} \<and> card K = k} \<subseteq> Pow {0..<card A}" 37 by auto 38 ultimately have "inj_on (image f) {K. K \<subseteq> {0..<card A} \<and> card K = k}" 39 by (rule inj_on_subset) 40 then have "card {K. K \<subseteq> {0..<card A} \<and> card K = k} = 41 card (image f ` {K. K \<subseteq> {0..<card A} \<and> card K = k})" (is "_ = card ?C") 42 by (simp add: card_image) 43 also have "?C = {K. K \<subseteq> f ` {0..<card A} \<and> card K = k}" 44 by (auto elim!: subset_imageE) 45 also have "f ` {0..<card A} = A" 46 by (meson bij bij_betw_def) 47 finally show ?thesis 48 by (simp add: binomial_def) 49qed 50 51text \<open>Recursive characterization\<close> 52 53lemma binomial_n_0 [simp, code]: "n choose 0 = 1" 54proof - 55 have "{K \<in> Pow {0..<n}. card K = 0} = {{}}" 56 by (auto dest: finite_subset) 57 then show ?thesis 58 by (simp add: binomial_def) 59qed 60 61lemma binomial_0_Suc [simp, code]: "0 choose Suc k = 0" 62 by (simp add: binomial_def) 63 64lemma binomial_Suc_Suc [simp, code]: "Suc n choose Suc k = (n choose k) + (n choose Suc k)" 65proof - 66 let ?P = "\<lambda>n k. {K. K \<subseteq> {0..<n} \<and> card K = k}" 67 let ?Q = "?P (Suc n) (Suc k)" 68 have inj: "inj_on (insert n) (?P n k)" 69 by rule (auto; metis atLeastLessThan_iff insert_iff less_irrefl subsetCE) 70 have disjoint: "insert n ` ?P n k \<inter> ?P n (Suc k) = {}" 71 by auto 72 have "?Q = {K\<in>?Q. n \<in> K} \<union> {K\<in>?Q. n \<notin> K}" 73 by auto 74 also have "{K\<in>?Q. n \<in> K} = insert n ` ?P n k" (is "?A = ?B") 75 proof (rule set_eqI) 76 fix K 77 have K_finite: "finite K" if "K \<subseteq> insert n {0..<n}" 78 using that by (rule finite_subset) simp_all 79 have Suc_card_K: "Suc (card K - Suc 0) = card K" if "n \<in> K" 80 and "finite K" 81 proof - 82 from \<open>n \<in> K\<close> obtain L where "K = insert n L" and "n \<notin> L" 83 by (blast elim: Set.set_insert) 84 with that show ?thesis by (simp add: card_insert) 85 qed 86 show "K \<in> ?A \<longleftrightarrow> K \<in> ?B" 87 by (subst in_image_insert_iff) 88 (auto simp add: card_insert subset_eq_atLeast0_lessThan_finite 89 Diff_subset_conv K_finite Suc_card_K) 90 qed 91 also have "{K\<in>?Q. n \<notin> K} = ?P n (Suc k)" 92 by (auto simp add: atLeast0_lessThan_Suc) 93 finally show ?thesis using inj disjoint 94 by (simp add: binomial_def card_Un_disjoint card_image) 95qed 96 97lemma binomial_eq_0: "n < k \<Longrightarrow> n choose k = 0" 98 by (auto simp add: binomial_def dest: subset_eq_atLeast0_lessThan_card) 99 100lemma zero_less_binomial: "k \<le> n \<Longrightarrow> n choose k > 0" 101 by (induct n k rule: diff_induct) simp_all 102 103lemma binomial_eq_0_iff [simp]: "n choose k = 0 \<longleftrightarrow> n < k" 104 by (metis binomial_eq_0 less_numeral_extra(3) not_less zero_less_binomial) 105 106lemma zero_less_binomial_iff [simp]: "n choose k > 0 \<longleftrightarrow> k \<le> n" 107 by (metis binomial_eq_0_iff not_less0 not_less zero_less_binomial) 108 109lemma binomial_n_n [simp]: "n choose n = 1" 110 by (induct n) (simp_all add: binomial_eq_0) 111 112lemma binomial_Suc_n [simp]: "Suc n choose n = Suc n" 113 by (induct n) simp_all 114 115lemma binomial_1 [simp]: "n choose Suc 0 = n" 116 by (induct n) simp_all 117 118lemma choose_reduce_nat: 119 "0 < n \<Longrightarrow> 0 < k \<Longrightarrow> 120 n choose k = ((n - 1) choose (k - 1)) + ((n - 1) choose k)" 121 using binomial_Suc_Suc [of "n - 1" "k - 1"] by simp 122 123lemma Suc_times_binomial_eq: "Suc n * (n choose k) = (Suc n choose Suc k) * Suc k" 124 apply (induct n arbitrary: k) 125 apply simp 126 apply arith 127 apply (case_tac k) 128 apply (auto simp add: add_mult_distrib add_mult_distrib2 le_Suc_eq binomial_eq_0) 129 done 130 131lemma binomial_le_pow2: "n choose k \<le> 2^n" 132 apply (induct n arbitrary: k) 133 apply (case_tac k) 134 apply simp_all 135 apply (case_tac k) 136 apply auto 137 apply (simp add: add_le_mono mult_2) 138 done 139 140text \<open>The absorption property.\<close> 141lemma Suc_times_binomial: "Suc k * (Suc n choose Suc k) = Suc n * (n choose k)" 142 using Suc_times_binomial_eq by auto 143 144text \<open>This is the well-known version of absorption, but it's harder to use 145 because of the need to reason about division.\<close> 146lemma binomial_Suc_Suc_eq_times: "(Suc n choose Suc k) = (Suc n * (n choose k)) div Suc k" 147 by (simp add: Suc_times_binomial_eq del: mult_Suc mult_Suc_right) 148 149text \<open>Another version of absorption, with \<open>-1\<close> instead of \<open>Suc\<close>.\<close> 150lemma times_binomial_minus1_eq: "0 < k \<Longrightarrow> k * (n choose k) = n * ((n - 1) choose (k - 1))" 151 using Suc_times_binomial_eq [where n = "n - 1" and k = "k - 1"] 152 by (auto split: nat_diff_split) 153 154 155subsection \<open>The binomial theorem (courtesy of Tobias Nipkow):\<close> 156 157text \<open>Avigad's version, generalized to any commutative ring\<close> 158theorem binomial_ring: "(a + b :: 'a::{comm_ring_1,power})^n = 159 (\<Sum>k\<le>n. (of_nat (n choose k)) * a^k * b^(n-k))" 160proof (induct n) 161 case 0 162 then show ?case by simp 163next 164 case (Suc n) 165 have decomp: "{0..n+1} = {0} \<union> {n + 1} \<union> {1..n}" 166 by auto 167 have decomp2: "{0..n} = {0} \<union> {1..n}" 168 by auto 169 have "(a + b)^(n+1) = (a + b) * (\<Sum>k\<le>n. of_nat (n choose k) * a^k * b^(n - k))" 170 using Suc.hyps by simp 171 also have "\<dots> = a * (\<Sum>k\<le>n. of_nat (n choose k) * a^k * b^(n-k)) + 172 b * (\<Sum>k\<le>n. of_nat (n choose k) * a^k * b^(n-k))" 173 by (rule distrib_right) 174 also have "\<dots> = (\<Sum>k\<le>n. of_nat (n choose k) * a^(k+1) * b^(n-k)) + 175 (\<Sum>k\<le>n. of_nat (n choose k) * a^k * b^(n - k + 1))" 176 by (auto simp add: sum_distrib_left ac_simps) 177 also have "\<dots> = (\<Sum>k\<le>n. of_nat (n choose k) * a^k * b^(n + 1 - k)) + 178 (\<Sum>k=1..n+1. of_nat (n choose (k - 1)) * a^k * b^(n + 1 - k))" 179 by (simp add: atMost_atLeast0 sum_shift_bounds_cl_Suc_ivl Suc_diff_le field_simps del: sum_cl_ivl_Suc) 180 also have "\<dots> = a^(n + 1) + b^(n + 1) + 181 (\<Sum>k=1..n. of_nat (n choose (k - 1)) * a^k * b^(n + 1 - k)) + 182 (\<Sum>k=1..n. of_nat (n choose k) * a^k * b^(n + 1 - k))" 183 by (simp add: atMost_atLeast0 decomp2) 184 also have "\<dots> = a^(n + 1) + b^(n + 1) + 185 (\<Sum>k=1..n. of_nat (n + 1 choose k) * a^k * b^(n + 1 - k))" 186 by (auto simp add: field_simps sum.distrib [symmetric] choose_reduce_nat) 187 also have "\<dots> = (\<Sum>k\<le>n+1. of_nat (n + 1 choose k) * a^k * b^(n + 1 - k))" 188 using decomp by (simp add: atMost_atLeast0 field_simps) 189 finally show ?case 190 by simp 191qed 192 193text \<open>Original version for the naturals.\<close> 194corollary binomial: "(a + b :: nat)^n = (\<Sum>k\<le>n. (of_nat (n choose k)) * a^k * b^(n - k))" 195 using binomial_ring [of "int a" "int b" n] 196 by (simp only: of_nat_add [symmetric] of_nat_mult [symmetric] of_nat_power [symmetric] 197 of_nat_sum [symmetric] of_nat_eq_iff of_nat_id) 198 199lemma binomial_fact_lemma: "k \<le> n \<Longrightarrow> fact k * fact (n - k) * (n choose k) = fact n" 200proof (induct n arbitrary: k rule: nat_less_induct) 201 fix n k 202 assume H: "\<forall>m<n. \<forall>x\<le>m. fact x * fact (m - x) * (m choose x) = fact m" 203 assume kn: "k \<le> n" 204 let ?ths = "fact k * fact (n - k) * (n choose k) = fact n" 205 consider "n = 0 \<or> k = 0 \<or> n = k" | m h where "n = Suc m" "k = Suc h" "h < m" 206 using kn by atomize_elim presburger 207 then show "fact k * fact (n - k) * (n choose k) = fact n" 208 proof cases 209 case 1 210 with kn show ?thesis by auto 211 next 212 case 2 213 note n = \<open>n = Suc m\<close> 214 note k = \<open>k = Suc h\<close> 215 note hm = \<open>h < m\<close> 216 have mn: "m < n" 217 using n by arith 218 have hm': "h \<le> m" 219 using hm by arith 220 have km: "k \<le> m" 221 using hm k n kn by arith 222 have "m - h = Suc (m - Suc h)" 223 using k km hm by arith 224 with km k have "fact (m - h) = (m - h) * fact (m - k)" 225 by simp 226 with n k have "fact k * fact (n - k) * (n choose k) = 227 k * (fact h * fact (m - h) * (m choose h)) + 228 (m - h) * (fact k * fact (m - k) * (m choose k))" 229 by (simp add: field_simps) 230 also have "\<dots> = (k + (m - h)) * fact m" 231 using H[rule_format, OF mn hm'] H[rule_format, OF mn km] 232 by (simp add: field_simps) 233 finally show ?thesis 234 using k n km by simp 235 qed 236qed 237 238lemma binomial_fact': 239 assumes "k \<le> n" 240 shows "n choose k = fact n div (fact k * fact (n - k))" 241 using binomial_fact_lemma [OF assms] 242 by (metis fact_nonzero mult_eq_0_iff nonzero_mult_div_cancel_left) 243 244lemma binomial_fact: 245 assumes kn: "k \<le> n" 246 shows "(of_nat (n choose k) :: 'a::field_char_0) = fact n / (fact k * fact (n - k))" 247 using binomial_fact_lemma[OF kn] 248 apply (simp add: field_simps) 249 apply (metis mult.commute of_nat_fact of_nat_mult) 250 done 251 252lemma fact_binomial: 253 assumes "k \<le> n" 254 shows "fact k * of_nat (n choose k) = (fact n / fact (n - k) :: 'a::field_char_0)" 255 unfolding binomial_fact [OF assms] by (simp add: field_simps) 256 257lemma choose_two: "n choose 2 = n * (n - 1) div 2" 258proof (cases "n \<ge> 2") 259 case False 260 then have "n = 0 \<or> n = 1" 261 by auto 262 then show ?thesis by auto 263next 264 case True 265 define m where "m = n - 2" 266 with True have "n = m + 2" 267 by simp 268 then have "fact n = n * (n - 1) * fact (n - 2)" 269 by (simp add: fact_prod_Suc atLeast0_lessThan_Suc algebra_simps) 270 with True show ?thesis 271 by (simp add: binomial_fact') 272qed 273 274lemma choose_row_sum: "(\<Sum>k\<le>n. n choose k) = 2^n" 275 using binomial [of 1 "1" n] by (simp add: numeral_2_eq_2) 276 277lemma sum_choose_lower: "(\<Sum>k\<le>n. (r+k) choose k) = Suc (r+n) choose n" 278 by (induct n) auto 279 280lemma sum_choose_upper: "(\<Sum>k\<le>n. k choose m) = Suc n choose Suc m" 281 by (induct n) auto 282 283lemma choose_alternating_sum: 284 "n > 0 \<Longrightarrow> (\<Sum>i\<le>n. (-1)^i * of_nat (n choose i)) = (0 :: 'a::comm_ring_1)" 285 using binomial_ring[of "-1 :: 'a" 1 n] 286 by (simp add: atLeast0AtMost mult_of_nat_commute zero_power) 287 288lemma choose_even_sum: 289 assumes "n > 0" 290 shows "2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0) = (2 ^ n :: 'a::comm_ring_1)" 291proof - 292 have "2 ^ n = (\<Sum>i\<le>n. of_nat (n choose i)) + (\<Sum>i\<le>n. (-1) ^ i * of_nat (n choose i) :: 'a)" 293 using choose_row_sum[of n] 294 by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_sum[symmetric]) 295 also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) + (-1) ^ i * of_nat (n choose i))" 296 by (simp add: sum.distrib) 297 also have "\<dots> = 2 * (\<Sum>i\<le>n. if even i then of_nat (n choose i) else 0)" 298 by (subst sum_distrib_left, intro sum.cong) simp_all 299 finally show ?thesis .. 300qed 301 302lemma choose_odd_sum: 303 assumes "n > 0" 304 shows "2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0) = (2 ^ n :: 'a::comm_ring_1)" 305proof - 306 have "2 ^ n = (\<Sum>i\<le>n. of_nat (n choose i)) - (\<Sum>i\<le>n. (-1) ^ i * of_nat (n choose i) :: 'a)" 307 using choose_row_sum[of n] 308 by (simp add: choose_alternating_sum assms atLeast0AtMost of_nat_sum[symmetric]) 309 also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) - (-1) ^ i * of_nat (n choose i))" 310 by (simp add: sum_subtractf) 311 also have "\<dots> = 2 * (\<Sum>i\<le>n. if odd i then of_nat (n choose i) else 0)" 312 by (subst sum_distrib_left, intro sum.cong) simp_all 313 finally show ?thesis .. 314qed 315 316text\<open>NW diagonal sum property\<close> 317lemma sum_choose_diagonal: 318 assumes "m \<le> n" 319 shows "(\<Sum>k\<le>m. (n - k) choose (m - k)) = Suc n choose m" 320proof - 321 have "(\<Sum>k\<le>m. (n-k) choose (m - k)) = (\<Sum>k\<le>m. (n - m + k) choose k)" 322 using sum.atLeastAtMost_rev [of "\<lambda>k. (n - k) choose (m - k)" 0 m] assms 323 by (simp add: atMost_atLeast0) 324 also have "\<dots> = Suc (n - m + m) choose m" 325 by (rule sum_choose_lower) 326 also have "\<dots> = Suc n choose m" 327 using assms by simp 328 finally show ?thesis . 329qed 330 331 332subsection \<open>Generalized binomial coefficients\<close> 333 334definition gbinomial :: "'a::{semidom_divide,semiring_char_0} \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65) 335 where gbinomial_prod_rev: "a gchoose n = prod (\<lambda>i. a - of_nat i) {0..<n} div fact n" 336 337lemma gbinomial_0 [simp]: 338 "a gchoose 0 = 1" 339 "0 gchoose (Suc n) = 0" 340 by (simp_all add: gbinomial_prod_rev prod.atLeast0_lessThan_Suc_shift) 341 342lemma gbinomial_Suc: "a gchoose (Suc k) = prod (\<lambda>i. a - of_nat i) {0..k} div fact (Suc k)" 343 by (simp add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost) 344 345lemma gbinomial_mult_fact: "fact n * (a gchoose n) = (\<Prod>i = 0..<n. a - of_nat i)" 346 for a :: "'a::field_char_0" 347 by (simp_all add: gbinomial_prod_rev field_simps) 348 349lemma gbinomial_mult_fact': "(a gchoose n) * fact n = (\<Prod>i = 0..<n. a - of_nat i)" 350 for a :: "'a::field_char_0" 351 using gbinomial_mult_fact [of n a] by (simp add: ac_simps) 352 353lemma gbinomial_pochhammer: "a gchoose n = (- 1) ^ n * pochhammer (- a) n / fact n" 354 for a :: "'a::field_char_0" 355 by (cases n) 356 (simp_all add: pochhammer_minus, 357 simp_all add: gbinomial_prod_rev pochhammer_prod_rev 358 power_mult_distrib [symmetric] atLeastLessThanSuc_atLeastAtMost 359 prod.atLeast_Suc_atMost_Suc_shift of_nat_diff) 360 361lemma gbinomial_pochhammer': "s gchoose n = pochhammer (s - of_nat n + 1) n / fact n" 362 for s :: "'a::field_char_0" 363proof - 364 have "s gchoose n = ((-1)^n * (-1)^n) * pochhammer (s - of_nat n + 1) n / fact n" 365 by (simp add: gbinomial_pochhammer pochhammer_minus mult_ac) 366 also have "(-1 :: 'a)^n * (-1)^n = 1" 367 by (subst power_add [symmetric]) simp 368 finally show ?thesis 369 by simp 370qed 371 372lemma gbinomial_binomial: "n gchoose k = n choose k" 373proof (cases "k \<le> n") 374 case False 375 then have "n < k" 376 by (simp add: not_le) 377 then have "0 \<in> ((-) n) ` {0..<k}" 378 by auto 379 then have "prod ((-) n) {0..<k} = 0" 380 by (auto intro: prod_zero) 381 with \<open>n < k\<close> show ?thesis 382 by (simp add: binomial_eq_0 gbinomial_prod_rev prod_zero) 383next 384 case True 385 from True have *: "prod ((-) n) {0..<k} = \<Prod>{Suc (n - k)..n}" 386 by (intro prod.reindex_bij_witness[of _ "\<lambda>i. n - i" "\<lambda>i. n - i"]) auto 387 from True have "n choose k = fact n div (fact k * fact (n - k))" 388 by (rule binomial_fact') 389 with * show ?thesis 390 by (simp add: gbinomial_prod_rev mult.commute [of "fact k"] div_mult2_eq fact_div_fact) 391qed 392 393lemma of_nat_gbinomial: "of_nat (n gchoose k) = (of_nat n gchoose k :: 'a::field_char_0)" 394proof (cases "k \<le> n") 395 case False 396 then show ?thesis 397 by (simp add: not_le gbinomial_binomial binomial_eq_0 gbinomial_prod_rev) 398next 399 case True 400 define m where "m = n - k" 401 with True have n: "n = m + k" 402 by arith 403 from n have "fact n = ((\<Prod>i = 0..<m + k. of_nat (m + k - i) ):: 'a)" 404 by (simp add: fact_prod_rev) 405 also have "\<dots> = ((\<Prod>i\<in>{0..<k} \<union> {k..<m + k}. of_nat (m + k - i)) :: 'a)" 406 by (simp add: ivl_disj_un) 407 finally have "fact n = (fact m * (\<Prod>i = 0..<k. of_nat m + of_nat k - of_nat i) :: 'a)" 408 using prod_shift_bounds_nat_ivl [of "\<lambda>i. of_nat (m + k - i) :: 'a" 0 k m] 409 by (simp add: fact_prod_rev [of m] prod.union_disjoint of_nat_diff) 410 then have "fact n / fact (n - k) = ((\<Prod>i = 0..<k. of_nat n - of_nat i) :: 'a)" 411 by (simp add: n) 412 with True have "fact k * of_nat (n gchoose k) = (fact k * (of_nat n gchoose k) :: 'a)" 413 by (simp only: gbinomial_mult_fact [of k "of_nat n"] gbinomial_binomial [of n k] fact_binomial) 414 then show ?thesis 415 by simp 416qed 417 418lemma binomial_gbinomial: "of_nat (n choose k) = (of_nat n gchoose k :: 'a::field_char_0)" 419 by (simp add: gbinomial_binomial [symmetric] of_nat_gbinomial) 420 421setup 422 \<open>Sign.add_const_constraint (@{const_name gbinomial}, SOME @{typ "'a::field_char_0 \<Rightarrow> nat \<Rightarrow> 'a"})\<close> 423 424lemma gbinomial_1[simp]: "a gchoose 1 = a" 425 by (simp add: gbinomial_prod_rev lessThan_Suc) 426 427lemma gbinomial_Suc0[simp]: "a gchoose (Suc 0) = a" 428 by (simp add: gbinomial_prod_rev lessThan_Suc) 429 430lemma gbinomial_mult_1: 431 fixes a :: "'a::field_char_0" 432 shows "a * (a gchoose n) = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))" 433 (is "?l = ?r") 434proof - 435 have "?r = ((- 1) ^n * pochhammer (- a) n / (fact n)) * (of_nat n - (- a + of_nat n))" 436 apply (simp only: gbinomial_pochhammer pochhammer_Suc right_diff_distrib power_Suc) 437 apply (simp del: of_nat_Suc fact_Suc) 438 apply (auto simp add: field_simps simp del: of_nat_Suc) 439 done 440 also have "\<dots> = ?l" 441 by (simp add: field_simps gbinomial_pochhammer) 442 finally show ?thesis .. 443qed 444 445lemma gbinomial_mult_1': 446 "(a gchoose n) * a = of_nat n * (a gchoose n) + of_nat (Suc n) * (a gchoose (Suc n))" 447 for a :: "'a::field_char_0" 448 by (simp add: mult.commute gbinomial_mult_1) 449 450lemma gbinomial_Suc_Suc: "(a + 1) gchoose (Suc k) = a gchoose k + (a gchoose (Suc k))" 451 for a :: "'a::field_char_0" 452proof (cases k) 453 case 0 454 then show ?thesis by simp 455next 456 case (Suc h) 457 have eq0: "(\<Prod>i\<in>{1..k}. (a + 1) - of_nat i) = (\<Prod>i\<in>{0..h}. a - of_nat i)" 458 apply (rule prod.reindex_cong [where l = Suc]) 459 using Suc 460 apply (auto simp add: image_Suc_atMost) 461 done 462 have "fact (Suc k) * (a gchoose k + (a gchoose (Suc k))) = 463 (a gchoose Suc h) * (fact (Suc (Suc h))) + 464 (a gchoose Suc (Suc h)) * (fact (Suc (Suc h)))" 465 by (simp add: Suc field_simps del: fact_Suc) 466 also have "\<dots> = 467 (a gchoose Suc h) * of_nat (Suc (Suc h) * fact (Suc h)) + (\<Prod>i=0..Suc h. a - of_nat i)" 468 apply (simp del: fact_Suc add: gbinomial_mult_fact field_simps mult.left_commute [of _ "2"]) 469 apply (simp del: fact_Suc add: fact_Suc [of "Suc h"] field_simps gbinomial_mult_fact 470 mult.left_commute [of _ "2"] atLeastLessThanSuc_atLeastAtMost) 471 done 472 also have "\<dots> = 473 (fact (Suc h) * (a gchoose Suc h)) * of_nat (Suc (Suc h)) + (\<Prod>i=0..Suc h. a - of_nat i)" 474 by (simp only: fact_Suc mult.commute mult.left_commute of_nat_fact of_nat_id of_nat_mult) 475 also have "\<dots> = 476 of_nat (Suc (Suc h)) * (\<Prod>i=0..h. a - of_nat i) + (\<Prod>i=0..Suc h. a - of_nat i)" 477 unfolding gbinomial_mult_fact atLeastLessThanSuc_atLeastAtMost by auto 478 also have "\<dots> = 479 (\<Prod>i=0..Suc h. a - of_nat i) + (of_nat h * (\<Prod>i=0..h. a - of_nat i) + 2 * (\<Prod>i=0..h. a - of_nat i))" 480 by (simp add: field_simps) 481 also have "\<dots> = 482 ((a gchoose Suc h) * (fact (Suc h)) * of_nat (Suc k)) + (\<Prod>i\<in>{0..Suc h}. a - of_nat i)" 483 unfolding gbinomial_mult_fact' 484 by (simp add: comm_semiring_class.distrib field_simps Suc atLeastLessThanSuc_atLeastAtMost) 485 also have "\<dots> = (\<Prod>i\<in>{0..h}. a - of_nat i) * (a + 1)" 486 unfolding gbinomial_mult_fact' atLeast0_atMost_Suc 487 by (simp add: field_simps Suc atLeastLessThanSuc_atLeastAtMost) 488 also have "\<dots> = (\<Prod>i\<in>{0..k}. (a + 1) - of_nat i)" 489 using eq0 490 by (simp add: Suc prod.atLeast0_atMost_Suc_shift) 491 also have "\<dots> = (fact (Suc k)) * ((a + 1) gchoose (Suc k))" 492 by (simp only: gbinomial_mult_fact atLeastLessThanSuc_atLeastAtMost) 493 finally show ?thesis 494 using fact_nonzero [of "Suc k"] by auto 495qed 496 497lemma gbinomial_reduce_nat: "0 < k \<Longrightarrow> a gchoose k = (a - 1) gchoose (k - 1) + ((a - 1) gchoose k)" 498 for a :: "'a::field_char_0" 499 by (metis Suc_pred' diff_add_cancel gbinomial_Suc_Suc) 500 501lemma gchoose_row_sum_weighted: 502 "(\<Sum>k = 0..m. (r gchoose k) * (r/2 - of_nat k)) = of_nat(Suc m) / 2 * (r gchoose (Suc m))" 503 for r :: "'a::field_char_0" 504 by (induct m) (simp_all add: field_simps distrib gbinomial_mult_1) 505 506lemma binomial_symmetric: 507 assumes kn: "k \<le> n" 508 shows "n choose k = n choose (n - k)" 509proof - 510 have kn': "n - k \<le> n" 511 using kn by arith 512 from binomial_fact_lemma[OF kn] binomial_fact_lemma[OF kn'] 513 have "fact k * fact (n - k) * (n choose k) = fact (n - k) * fact (n - (n - k)) * (n choose (n - k))" 514 by simp 515 then show ?thesis 516 using kn by simp 517qed 518 519lemma choose_rising_sum: 520 "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))" 521 "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose m)" 522proof - 523 show "(\<Sum>j\<le>m. ((n + j) choose n)) = ((n + m + 1) choose (n + 1))" 524 by (induct m) simp_all 525 also have "\<dots> = (n + m + 1) choose m" 526 by (subst binomial_symmetric) simp_all 527 finally show "(\<Sum>j\<le>m. ((n + j) choose n)) = (n + m + 1) choose m" . 528qed 529 530lemma choose_linear_sum: "(\<Sum>i\<le>n. i * (n choose i)) = n * 2 ^ (n - 1)" 531proof (cases n) 532 case 0 533 then show ?thesis by simp 534next 535 case (Suc m) 536 have "(\<Sum>i\<le>n. i * (n choose i)) = (\<Sum>i\<le>Suc m. i * (Suc m choose i))" 537 by (simp add: Suc) 538 also have "\<dots> = Suc m * 2 ^ m" 539 unfolding sum_atMost_Suc_shift Suc_times_binomial sum_distrib_left[symmetric] 540 by (simp add: choose_row_sum) 541 finally show ?thesis 542 using Suc by simp 543qed 544 545lemma choose_alternating_linear_sum: 546 assumes "n \<noteq> 1" 547 shows "(\<Sum>i\<le>n. (-1)^i * of_nat i * of_nat (n choose i) :: 'a::comm_ring_1) = 0" 548proof (cases n) 549 case 0 550 then show ?thesis by simp 551next 552 case (Suc m) 553 with assms have "m > 0" 554 by simp 555 have "(\<Sum>i\<le>n. (-1) ^ i * of_nat i * of_nat (n choose i) :: 'a) = 556 (\<Sum>i\<le>Suc m. (-1) ^ i * of_nat i * of_nat (Suc m choose i))" 557 by (simp add: Suc) 558 also have "\<dots> = (\<Sum>i\<le>m. (-1) ^ (Suc i) * of_nat (Suc i * (Suc m choose Suc i)))" 559 by (simp only: sum_atMost_Suc_shift sum_distrib_left[symmetric] mult_ac of_nat_mult) simp 560 also have "\<dots> = - of_nat (Suc m) * (\<Sum>i\<le>m. (-1) ^ i * of_nat (m choose i))" 561 by (subst sum_distrib_left, rule sum.cong[OF refl], subst Suc_times_binomial) 562 (simp add: algebra_simps) 563 also have "(\<Sum>i\<le>m. (-1 :: 'a) ^ i * of_nat ((m choose i))) = 0" 564 using choose_alternating_sum[OF \<open>m > 0\<close>] by simp 565 finally show ?thesis 566 by simp 567qed 568 569lemma vandermonde: "(\<Sum>k\<le>r. (m choose k) * (n choose (r - k))) = (m + n) choose r" 570proof (induct n arbitrary: r) 571 case 0 572 have "(\<Sum>k\<le>r. (m choose k) * (0 choose (r - k))) = (\<Sum>k\<le>r. if k = r then (m choose k) else 0)" 573 by (intro sum.cong) simp_all 574 also have "\<dots> = m choose r" 575 by (simp add: sum.delta) 576 finally show ?case 577 by simp 578next 579 case (Suc n r) 580 show ?case 581 by (cases r) (simp_all add: Suc [symmetric] algebra_simps sum.distrib Suc_diff_le) 582qed 583 584lemma choose_square_sum: "(\<Sum>k\<le>n. (n choose k)^2) = ((2*n) choose n)" 585 using vandermonde[of n n n] 586 by (simp add: power2_eq_square mult_2 binomial_symmetric [symmetric]) 587 588lemma pochhammer_binomial_sum: 589 fixes a b :: "'a::comm_ring_1" 590 shows "pochhammer (a + b) n = (\<Sum>k\<le>n. of_nat (n choose k) * pochhammer a k * pochhammer b (n - k))" 591proof (induction n arbitrary: a b) 592 case 0 593 then show ?case by simp 594next 595 case (Suc n a b) 596 have "(\<Sum>k\<le>Suc n. of_nat (Suc n choose k) * pochhammer a k * pochhammer b (Suc n - k)) = 597 (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) + 598 ((\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) + 599 pochhammer b (Suc n))" 600 by (subst sum_atMost_Suc_shift) (simp add: ring_distribs sum.distrib) 601 also have "(\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a (Suc i) * pochhammer b (n - i)) = 602 a * pochhammer ((a + 1) + b) n" 603 by (subst Suc) (simp add: sum_distrib_left pochhammer_rec mult_ac) 604 also have "(\<Sum>i\<le>n. of_nat (n choose Suc i) * pochhammer a (Suc i) * pochhammer b (n - i)) + 605 pochhammer b (Suc n) = 606 (\<Sum>i=0..Suc n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))" 607 apply (subst sum_head_Suc) 608 apply simp 609 apply (subst sum_shift_bounds_cl_Suc_ivl) 610 apply (simp add: atLeast0AtMost) 611 done 612 also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc n - i))" 613 using Suc by (intro sum.mono_neutral_right) (auto simp: not_le binomial_eq_0) 614 also have "\<dots> = (\<Sum>i\<le>n. of_nat (n choose i) * pochhammer a i * pochhammer b (Suc (n - i)))" 615 by (intro sum.cong) (simp_all add: Suc_diff_le) 616 also have "\<dots> = b * pochhammer (a + (b + 1)) n" 617 by (subst Suc) (simp add: sum_distrib_left mult_ac pochhammer_rec) 618 also have "a * pochhammer ((a + 1) + b) n + b * pochhammer (a + (b + 1)) n = 619 pochhammer (a + b) (Suc n)" 620 by (simp add: pochhammer_rec algebra_simps) 621 finally show ?case .. 622qed 623 624text \<open>Contributed by Manuel Eberl, generalised by LCP. 625 Alternative definition of the binomial coefficient as @{term "\<Prod>i<k. (n - i) / (k - i)"}.\<close> 626lemma gbinomial_altdef_of_nat: "x gchoose k = (\<Prod>i = 0..<k. (x - of_nat i) / of_nat (k - i) :: 'a)" 627 for k :: nat and x :: "'a::field_char_0" 628 by (simp add: prod_dividef gbinomial_prod_rev fact_prod_rev) 629 630lemma gbinomial_ge_n_over_k_pow_k: 631 fixes k :: nat 632 and x :: "'a::linordered_field" 633 assumes "of_nat k \<le> x" 634 shows "(x / of_nat k :: 'a) ^ k \<le> x gchoose k" 635proof - 636 have x: "0 \<le> x" 637 using assms of_nat_0_le_iff order_trans by blast 638 have "(x / of_nat k :: 'a) ^ k = (\<Prod>i = 0..<k. x / of_nat k :: 'a)" 639 by (simp add: prod_constant) 640 also have "\<dots> \<le> x gchoose k" (* FIXME *) 641 unfolding gbinomial_altdef_of_nat 642 apply (safe intro!: prod_mono) 643 apply simp_all 644 prefer 2 645 subgoal premises for i 646 proof - 647 from assms have "x * of_nat i \<ge> of_nat (i * k)" 648 by (metis mult.commute mult_le_cancel_right of_nat_less_0_iff of_nat_mult) 649 then have "x * of_nat k - x * of_nat i \<le> x * of_nat k - of_nat (i * k)" 650 by arith 651 then have "x * of_nat (k - i) \<le> (x - of_nat i) * of_nat k" 652 using \<open>i < k\<close> by (simp add: algebra_simps zero_less_mult_iff of_nat_diff) 653 then have "x * of_nat (k - i) \<le> (x - of_nat i) * (of_nat k :: 'a)" 654 by (simp only: of_nat_mult[symmetric] of_nat_le_iff) 655 with assms show ?thesis 656 using \<open>i < k\<close> by (simp add: field_simps) 657 qed 658 apply (simp add: x zero_le_divide_iff) 659 done 660 finally show ?thesis . 661qed 662 663lemma gbinomial_negated_upper: "(a gchoose b) = (-1) ^ b * ((of_nat b - a - 1) gchoose b)" 664 by (simp add: gbinomial_pochhammer pochhammer_minus algebra_simps) 665 666lemma gbinomial_minus: "((-a) gchoose b) = (-1) ^ b * ((a + of_nat b - 1) gchoose b)" 667 by (subst gbinomial_negated_upper) (simp add: add_ac) 668 669lemma Suc_times_gbinomial: "of_nat (Suc b) * ((a + 1) gchoose (Suc b)) = (a + 1) * (a gchoose b)" 670proof (cases b) 671 case 0 672 then show ?thesis by simp 673next 674 case (Suc b) 675 then have "((a + 1) gchoose (Suc (Suc b))) = (\<Prod>i = 0..Suc b. a + (1 - of_nat i)) / fact (b + 2)" 676 by (simp add: field_simps gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost) 677 also have "(\<Prod>i = 0..Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)" 678 by (simp add: prod.atLeast0_atMost_Suc_shift) 679 also have "\<dots> / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)" 680 by (simp_all add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost) 681 finally show ?thesis by (simp add: Suc field_simps del: of_nat_Suc) 682qed 683 684lemma gbinomial_factors: "((a + 1) gchoose (Suc b)) = (a + 1) / of_nat (Suc b) * (a gchoose b)" 685proof (cases b) 686 case 0 687 then show ?thesis by simp 688next 689 case (Suc b) 690 then have "((a + 1) gchoose (Suc (Suc b))) = (\<Prod>i = 0 .. Suc b. a + (1 - of_nat i)) / fact (b + 2)" 691 by (simp add: field_simps gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost) 692 also have "(\<Prod>i = 0 .. Suc b. a + (1 - of_nat i)) = (a + 1) * (\<Prod>i = 0..b. a - of_nat i)" 693 by (simp add: prod.atLeast0_atMost_Suc_shift) 694 also have "\<dots> / fact (b + 2) = (a + 1) / of_nat (Suc (Suc b)) * (a gchoose Suc b)" 695 by (simp_all add: gbinomial_prod_rev atLeastLessThanSuc_atLeastAtMost atLeast0AtMost) 696 finally show ?thesis 697 by (simp add: Suc) 698qed 699 700lemma gbinomial_rec: "((r + 1) gchoose (Suc k)) = (r gchoose k) * ((r + 1) / of_nat (Suc k))" 701 using gbinomial_mult_1[of r k] 702 by (subst gbinomial_Suc_Suc) (simp add: field_simps del: of_nat_Suc, simp add: algebra_simps) 703 704lemma gbinomial_of_nat_symmetric: "k \<le> n \<Longrightarrow> (of_nat n) gchoose k = (of_nat n) gchoose (n - k)" 705 using binomial_symmetric[of k n] by (simp add: binomial_gbinomial [symmetric]) 706 707 708text \<open>The absorption identity (equation 5.5 @{cite \<open>p.~157\<close> GKP_CM}): 709\[ 710{r \choose k} = \frac{r}{k}{r - 1 \choose k - 1},\quad \textnormal{integer } k \neq 0. 711\]\<close> 712lemma gbinomial_absorption': "k > 0 \<Longrightarrow> r gchoose k = (r / of_nat k) * (r - 1 gchoose (k - 1))" 713 using gbinomial_rec[of "r - 1" "k - 1"] 714 by (simp_all add: gbinomial_rec field_simps del: of_nat_Suc) 715 716text \<open>The absorption identity is written in the following form to avoid 717division by $k$ (the lower index) and therefore remove the $k \neq 0$ 718restriction @{cite \<open>p.~157\<close> GKP_CM}: 719\[ 720k{r \choose k} = r{r - 1 \choose k - 1}, \quad \textnormal{integer } k. 721\]\<close> 722lemma gbinomial_absorption: "of_nat (Suc k) * (r gchoose Suc k) = r * ((r - 1) gchoose k)" 723 using gbinomial_absorption'[of "Suc k" r] by (simp add: field_simps del: of_nat_Suc) 724 725text \<open>The absorption identity for natural number binomial coefficients:\<close> 726lemma binomial_absorption: "Suc k * (n choose Suc k) = n * ((n - 1) choose k)" 727 by (cases n) (simp_all add: binomial_eq_0 Suc_times_binomial del: binomial_Suc_Suc mult_Suc) 728 729text \<open>The absorption companion identity for natural number coefficients, 730 following the proof by GKP @{cite \<open>p.~157\<close> GKP_CM}:\<close> 731lemma binomial_absorb_comp: "(n - k) * (n choose k) = n * ((n - 1) choose k)" 732 (is "?lhs = ?rhs") 733proof (cases "n \<le> k") 734 case True 735 then show ?thesis by auto 736next 737 case False 738 then have "?rhs = Suc ((n - 1) - k) * (n choose Suc ((n - 1) - k))" 739 using binomial_symmetric[of k "n - 1"] binomial_absorption[of "(n - 1) - k" n] 740 by simp 741 also have "Suc ((n - 1) - k) = n - k" 742 using False by simp 743 also have "n choose \<dots> = n choose k" 744 using False by (intro binomial_symmetric [symmetric]) simp_all 745 finally show ?thesis .. 746qed 747 748text \<open>The generalised absorption companion identity:\<close> 749lemma gbinomial_absorb_comp: "(r - of_nat k) * (r gchoose k) = r * ((r - 1) gchoose k)" 750 using pochhammer_absorb_comp[of r k] by (simp add: gbinomial_pochhammer) 751 752lemma gbinomial_addition_formula: 753 "r gchoose (Suc k) = ((r - 1) gchoose (Suc k)) + ((r - 1) gchoose k)" 754 using gbinomial_Suc_Suc[of "r - 1" k] by (simp add: algebra_simps) 755 756lemma binomial_addition_formula: 757 "0 < n \<Longrightarrow> n choose (Suc k) = ((n - 1) choose (Suc k)) + ((n - 1) choose k)" 758 by (subst choose_reduce_nat) simp_all 759 760text \<open> 761 Equation 5.9 of the reference material @{cite \<open>p.~159\<close> GKP_CM} is a useful 762 summation formula, operating on both indices: 763 \[ 764 \sum\limits_{k \leq n}{r + k \choose k} = {r + n + 1 \choose n}, 765 \quad \textnormal{integer } n. 766 \] 767\<close> 768lemma gbinomial_parallel_sum: "(\<Sum>k\<le>n. (r + of_nat k) gchoose k) = (r + of_nat n + 1) gchoose n" 769proof (induct n) 770 case 0 771 then show ?case by simp 772next 773 case (Suc m) 774 then show ?case 775 using gbinomial_Suc_Suc[of "(r + of_nat m + 1)" m] 776 by (simp add: add_ac) 777qed 778 779 780subsubsection \<open>Summation on the upper index\<close> 781 782text \<open> 783 Another summation formula is equation 5.10 of the reference material @{cite \<open>p.~160\<close> GKP_CM}, 784 aptly named \emph{summation on the upper index}:\[\sum_{0 \leq k \leq n} {k \choose m} = 785 {n + 1 \choose m + 1}, \quad \textnormal{integers } m, n \geq 0.\] 786\<close> 787lemma gbinomial_sum_up_index: 788 "(\<Sum>k = 0..n. (of_nat k gchoose m) :: 'a::field_char_0) = (of_nat n + 1) gchoose (m + 1)" 789proof (induct n) 790 case 0 791 show ?case 792 using gbinomial_Suc_Suc[of 0 m] 793 by (cases m) auto 794next 795 case (Suc n) 796 then show ?case 797 using gbinomial_Suc_Suc[of "of_nat (Suc n) :: 'a" m] 798 by (simp add: add_ac) 799qed 800 801lemma gbinomial_index_swap: 802 "((-1) ^ m) * ((- (of_nat n) - 1) gchoose m) = ((-1) ^ n) * ((- (of_nat m) - 1) gchoose n)" 803 (is "?lhs = ?rhs") 804proof - 805 have "?lhs = (of_nat (m + n) gchoose m)" 806 by (subst gbinomial_negated_upper) (simp add: power_mult_distrib [symmetric]) 807 also have "\<dots> = (of_nat (m + n) gchoose n)" 808 by (subst gbinomial_of_nat_symmetric) simp_all 809 also have "\<dots> = ?rhs" 810 by (subst gbinomial_negated_upper) simp 811 finally show ?thesis . 812qed 813 814lemma gbinomial_sum_lower_neg: "(\<Sum>k\<le>m. (r gchoose k) * (- 1) ^ k) = (- 1) ^ m * (r - 1 gchoose m)" 815 (is "?lhs = ?rhs") 816proof - 817 have "?lhs = (\<Sum>k\<le>m. -(r + 1) + of_nat k gchoose k)" 818 by (intro sum.cong[OF refl]) (subst gbinomial_negated_upper, simp add: power_mult_distrib) 819 also have "\<dots> = - r + of_nat m gchoose m" 820 by (subst gbinomial_parallel_sum) simp 821 also have "\<dots> = ?rhs" 822 by (subst gbinomial_negated_upper) (simp add: power_mult_distrib) 823 finally show ?thesis . 824qed 825 826lemma gbinomial_partial_row_sum: 827 "(\<Sum>k\<le>m. (r gchoose k) * ((r / 2) - of_nat k)) = ((of_nat m + 1)/2) * (r gchoose (m + 1))" 828proof (induct m) 829 case 0 830 then show ?case by simp 831next 832 case (Suc mm) 833 then have "(\<Sum>k\<le>Suc mm. (r gchoose k) * (r / 2 - of_nat k)) = 834 (r - of_nat (Suc mm)) * (r gchoose Suc mm) / 2" 835 by (simp add: field_simps) 836 also have "\<dots> = r * (r - 1 gchoose Suc mm) / 2" 837 by (subst gbinomial_absorb_comp) (rule refl) 838 also have "\<dots> = (of_nat (Suc mm) + 1) / 2 * (r gchoose (Suc mm + 1))" 839 by (subst gbinomial_absorption [symmetric]) simp 840 finally show ?case . 841qed 842 843lemma sum_bounds_lt_plus1: "(\<Sum>k<mm. f (Suc k)) = (\<Sum>k=1..mm. f k)" 844 by (induct mm) simp_all 845 846lemma gbinomial_partial_sum_poly: 847 "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) = 848 (\<Sum>k\<le>m. (-r gchoose k) * (-x)^k * (x + y)^(m-k))" 849 (is "?lhs m = ?rhs m") 850proof (induction m) 851 case 0 852 then show ?case by simp 853next 854 case (Suc mm) 855 define G where "G i k = (of_nat i + r gchoose k) * x^k * y^(i - k)" for i k 856 define S where "S = ?lhs" 857 have SG_def: "S = (\<lambda>i. (\<Sum>k\<le>i. (G i k)))" 858 unfolding S_def G_def .. 859 860 have "S (Suc mm) = G (Suc mm) 0 + (\<Sum>k=Suc 0..Suc mm. G (Suc mm) k)" 861 using SG_def by (simp add: sum_head_Suc atLeast0AtMost [symmetric]) 862 also have "(\<Sum>k=Suc 0..Suc mm. G (Suc mm) k) = (\<Sum>k=0..mm. G (Suc mm) (Suc k))" 863 by (subst sum_shift_bounds_cl_Suc_ivl) simp 864 also have "\<dots> = (\<Sum>k=0..mm. ((of_nat mm + r gchoose (Suc k)) + 865 (of_nat mm + r gchoose k)) * x^(Suc k) * y^(mm - k))" 866 unfolding G_def by (subst gbinomial_addition_formula) simp 867 also have "\<dots> = (\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) + 868 (\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k))" 869 by (subst sum.distrib [symmetric]) (simp add: algebra_simps) 870 also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k)) = 871 (\<Sum>k<Suc mm. (of_nat mm + r gchoose (Suc k)) * x^(Suc k) * y^(mm - k))" 872 by (simp only: atLeast0AtMost lessThan_Suc_atMost) 873 also have "\<dots> = (\<Sum>k<mm. (of_nat mm + r gchoose Suc k) * x^(Suc k) * y^(mm-k)) + 874 (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)" 875 (is "_ = ?A + ?B") 876 by (subst sum_lessThan_Suc) simp 877 also have "?A = (\<Sum>k=1..mm. (of_nat mm + r gchoose k) * x^k * y^(mm - k + 1))" 878 proof (subst sum_bounds_lt_plus1 [symmetric], intro sum.cong[OF refl], clarify) 879 fix k 880 assume "k < mm" 881 then have "mm - k = mm - Suc k + 1" 882 by linarith 883 then show "(of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - k) = 884 (of_nat mm + r gchoose Suc k) * x ^ Suc k * y ^ (mm - Suc k + 1)" 885 by (simp only:) 886 qed 887 also have "\<dots> + ?B = y * (\<Sum>k=1..mm. (G mm k)) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)" 888 unfolding G_def by (subst sum_distrib_left) (simp add: algebra_simps) 889 also have "(\<Sum>k=0..mm. (of_nat mm + r gchoose k) * x^(Suc k) * y^(mm - k)) = x * (S mm)" 890 unfolding S_def by (subst sum_distrib_left) (simp add: atLeast0AtMost algebra_simps) 891 also have "(G (Suc mm) 0) = y * (G mm 0)" 892 by (simp add: G_def) 893 finally have "S (Suc mm) = 894 y * (G mm 0 + (\<Sum>k=1..mm. (G mm k))) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm) + x * (S mm)" 895 by (simp add: ring_distribs) 896 also have "G mm 0 + (\<Sum>k=1..mm. (G mm k)) = S mm" 897 by (simp add: sum_head_Suc[symmetric] SG_def atLeast0AtMost) 898 finally have "S (Suc mm) = (x + y) * (S mm) + (of_nat mm + r gchoose (Suc mm)) * x^(Suc mm)" 899 by (simp add: algebra_simps) 900 also have "(of_nat mm + r gchoose (Suc mm)) = (-1) ^ (Suc mm) * (- r gchoose (Suc mm))" 901 by (subst gbinomial_negated_upper) simp 902 also have "(-1) ^ Suc mm * (- r gchoose Suc mm) * x ^ Suc mm = 903 (- r gchoose (Suc mm)) * (-x) ^ Suc mm" 904 by (simp add: power_minus[of x]) 905 also have "(x + y) * S mm + \<dots> = (x + y) * ?rhs mm + (- r gchoose (Suc mm)) * (- x)^Suc mm" 906 unfolding S_def by (subst Suc.IH) simp 907 also have "(x + y) * ?rhs mm = (\<Sum>n\<le>mm. ((- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n)))" 908 by (subst sum_distrib_left, rule sum.cong) (simp_all add: Suc_diff_le) 909 also have "\<dots> + (-r gchoose (Suc mm)) * (-x)^Suc mm = 910 (\<Sum>n\<le>Suc mm. (- r gchoose n) * (- x) ^ n * (x + y) ^ (Suc mm - n))" 911 by simp 912 finally show ?case 913 by (simp only: S_def) 914qed 915 916lemma gbinomial_partial_sum_poly_xpos: 917 "(\<Sum>k\<le>m. (of_nat m + r gchoose k) * x^k * y^(m-k)) = 918 (\<Sum>k\<le>m. (of_nat k + r - 1 gchoose k) * x^k * (x + y)^(m-k))" 919 apply (subst gbinomial_partial_sum_poly) 920 apply (subst gbinomial_negated_upper) 921 apply (intro sum.cong, rule refl) 922 apply (simp add: power_mult_distrib [symmetric]) 923 done 924 925lemma binomial_r_part_sum: "(\<Sum>k\<le>m. (2 * m + 1 choose k)) = 2 ^ (2 * m)" 926proof - 927 have "2 * 2^(2*m) = (\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k))" 928 using choose_row_sum[where n="2 * m + 1"] by (simp add: atMost_atLeast0) 929 also have "(\<Sum>k = 0..(2 * m + 1). (2 * m + 1 choose k)) = 930 (\<Sum>k = 0..m. (2 * m + 1 choose k)) + 931 (\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k))" 932 using sum_ub_add_nat[of 0 m "\<lambda>k. 2 * m + 1 choose k" "m+1"] 933 by (simp add: mult_2) 934 also have "(\<Sum>k = m+1..2*m+1. (2 * m + 1 choose k)) = 935 (\<Sum>k = 0..m. (2 * m + 1 choose (k + (m + 1))))" 936 by (subst sum_shift_bounds_cl_nat_ivl [symmetric]) (simp add: mult_2) 937 also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose (m - k)))" 938 by (intro sum.cong[OF refl], subst binomial_symmetric) simp_all 939 also have "\<dots> = (\<Sum>k = 0..m. (2 * m + 1 choose k))" 940 using sum.atLeastAtMost_rev [of "\<lambda>k. 2 * m + 1 choose (m - k)" 0 m] 941 by simp 942 also have "\<dots> + \<dots> = 2 * \<dots>" 943 by simp 944 finally show ?thesis 945 by (subst (asm) mult_cancel1) (simp add: atLeast0AtMost) 946qed 947 948lemma gbinomial_r_part_sum: "(\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k)) = 2 ^ (2 * m)" 949 (is "?lhs = ?rhs") 950proof - 951 have "?lhs = of_nat (\<Sum>k\<le>m. (2 * m + 1) choose k)" 952 by (simp add: binomial_gbinomial add_ac) 953 also have "\<dots> = of_nat (2 ^ (2 * m))" 954 by (subst binomial_r_part_sum) (rule refl) 955 finally show ?thesis by simp 956qed 957 958lemma gbinomial_sum_nat_pow2: 959 "(\<Sum>k\<le>m. (of_nat (m + k) gchoose k :: 'a::field_char_0) / 2 ^ k) = 2 ^ m" 960 (is "?lhs = ?rhs") 961proof - 962 have "2 ^ m * 2 ^ m = (2 ^ (2*m) :: 'a)" 963 by (induct m) simp_all 964 also have "\<dots> = (\<Sum>k\<le>m. (2 * (of_nat m) + 1 gchoose k))" 965 using gbinomial_r_part_sum .. 966 also have "\<dots> = (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) * 2 ^ (m - k))" 967 using gbinomial_partial_sum_poly_xpos[where x="1" and y="1" and r="of_nat m + 1" and m="m"] 968 by (simp add: add_ac) 969 also have "\<dots> = 2 ^ m * (\<Sum>k\<le>m. (of_nat (m + k) gchoose k) / 2 ^ k)" 970 by (subst sum_distrib_left) (simp add: algebra_simps power_diff) 971 finally show ?thesis 972 by (subst (asm) mult_left_cancel) simp_all 973qed 974 975lemma gbinomial_trinomial_revision: 976 assumes "k \<le> m" 977 shows "(r gchoose m) * (of_nat m gchoose k) = (r gchoose k) * (r - of_nat k gchoose (m - k))" 978proof - 979 have "(r gchoose m) * (of_nat m gchoose k) = (r gchoose m) * fact m / (fact k * fact (m - k))" 980 using assms by (simp add: binomial_gbinomial [symmetric] binomial_fact) 981 also have "\<dots> = (r gchoose k) * (r - of_nat k gchoose (m - k))" 982 using assms by (simp add: gbinomial_pochhammer power_diff pochhammer_product) 983 finally show ?thesis . 984qed 985 986text \<open>Versions of the theorems above for the natural-number version of "choose"\<close> 987lemma binomial_altdef_of_nat: 988 "k \<le> n \<Longrightarrow> of_nat (n choose k) = (\<Prod>i = 0..<k. of_nat (n - i) / of_nat (k - i) :: 'a)" 989 for n k :: nat and x :: "'a::field_char_0" 990 by (simp add: gbinomial_altdef_of_nat binomial_gbinomial of_nat_diff) 991 992lemma binomial_ge_n_over_k_pow_k: "k \<le> n \<Longrightarrow> (of_nat n / of_nat k :: 'a) ^ k \<le> of_nat (n choose k)" 993 for k n :: nat and x :: "'a::linordered_field" 994 by (simp add: gbinomial_ge_n_over_k_pow_k binomial_gbinomial of_nat_diff) 995 996lemma binomial_le_pow: 997 assumes "r \<le> n" 998 shows "n choose r \<le> n ^ r" 999proof - 1000 have "n choose r \<le> fact n div fact (n - r)" 1001 using assms by (subst binomial_fact_lemma[symmetric]) auto 1002 with fact_div_fact_le_pow [OF assms] show ?thesis 1003 by auto 1004qed 1005 1006lemma binomial_altdef_nat: "k \<le> n \<Longrightarrow> n choose k = fact n div (fact k * fact (n - k))" 1007 for k n :: nat 1008 by (subst binomial_fact_lemma [symmetric]) auto 1009 1010lemma choose_dvd: 1011 "k \<le> n \<Longrightarrow> fact k * fact (n - k) dvd (fact n :: 'a::linordered_semidom)" 1012 unfolding dvd_def 1013 apply (rule exI [where x="of_nat (n choose k)"]) 1014 using binomial_fact_lemma [of k n, THEN arg_cong [where f = of_nat and 'b='a]] 1015 apply auto 1016 done 1017 1018lemma fact_fact_dvd_fact: 1019 "fact k * fact n dvd (fact (k + n) :: 'a::linordered_semidom)" 1020 by (metis add.commute add_diff_cancel_left' choose_dvd le_add2) 1021 1022lemma choose_mult_lemma: 1023 "((m + r + k) choose (m + k)) * ((m + k) choose k) = ((m + r + k) choose k) * ((m + r) choose m)" 1024 (is "?lhs = _") 1025proof - 1026 have "?lhs = 1027 fact (m + r + k) div (fact (m + k) * fact (m + r - m)) * (fact (m + k) div (fact k * fact m))" 1028 by (simp add: binomial_altdef_nat) 1029 also have "\<dots> = fact (m + r + k) div (fact r * (fact k * fact m))" 1030 apply (subst div_mult_div_if_dvd) 1031 apply (auto simp: algebra_simps fact_fact_dvd_fact) 1032 apply (metis add.assoc add.commute fact_fact_dvd_fact) 1033 done 1034 also have "\<dots> = (fact (m + r + k) * fact (m + r)) div (fact r * (fact k * fact m) * fact (m + r))" 1035 apply (subst div_mult_div_if_dvd [symmetric]) 1036 apply (auto simp add: algebra_simps) 1037 apply (metis fact_fact_dvd_fact dvd_trans nat_mult_dvd_cancel_disj) 1038 done 1039 also have "\<dots> = 1040 (fact (m + r + k) div (fact k * fact (m + r)) * (fact (m + r) div (fact r * fact m)))" 1041 apply (subst div_mult_div_if_dvd) 1042 apply (auto simp: fact_fact_dvd_fact algebra_simps) 1043 done 1044 finally show ?thesis 1045 by (simp add: binomial_altdef_nat mult.commute) 1046qed 1047 1048text \<open>The "Subset of a Subset" identity.\<close> 1049lemma choose_mult: 1050 "k \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> (n choose m) * (m choose k) = (n choose k) * ((n - k) choose (m - k))" 1051 using choose_mult_lemma [of "m-k" "n-m" k] by simp 1052 1053 1054subsection \<open>More on Binomial Coefficients\<close> 1055 1056lemma choose_one: "n choose 1 = n" for n :: nat 1057 by simp 1058 1059lemma card_UNION: 1060 assumes "finite A" 1061 and "\<forall>k \<in> A. finite k" 1062 shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))" 1063 (is "?lhs = ?rhs") 1064proof - 1065 have "?rhs = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))" 1066 by simp 1067 also have "\<dots> = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))" 1068 (is "_ = nat ?rhs") 1069 by (subst sum_distrib_left) simp 1070 also have "?rhs = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))" 1071 using assms by (subst sum.Sigma) auto 1072 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))" 1073 by (rule sum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI simp add: split_beta) 1074 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))" 1075 using assms 1076 by (auto intro!: sum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"]) 1077 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)))" 1078 using assms by (subst sum.Sigma) auto 1079 also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "sum ?lhs _ = _") 1080 proof (rule sum.cong[OF refl]) 1081 fix x 1082 assume x: "x \<in> \<Union>A" 1083 define K where "K = {X \<in> A. x \<in> X}" 1084 with \<open>finite A\<close> have K: "finite K" 1085 by auto 1086 let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I}" 1087 have "inj_on snd (SIGMA i:{1..card A}. ?I i)" 1088 using assms by (auto intro!: inj_onI) 1089 moreover have [symmetric]: "snd ` (SIGMA i:{1..card A}. ?I i) = {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}" 1090 using assms 1091 by (auto intro!: rev_image_eqI[where x="(card a, a)" for a] 1092 simp add: card_gt_0_iff[folded Suc_le_eq] 1093 dest: finite_subset intro: card_mono) 1094 ultimately have "?lhs x = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))" 1095 by (rule sum.reindex_cong [where l = snd]) fastforce 1096 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)))" 1097 using assms by (subst sum.Sigma) auto 1098 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))" 1099 by (subst sum_distrib_left) simp 1100 also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))" 1101 (is "_ = ?rhs") 1102 proof (rule sum.mono_neutral_cong_right[rule_format]) 1103 show "finite {1..card A}" 1104 by simp 1105 show "{1..card K} \<subseteq> {1..card A}" 1106 using \<open>finite A\<close> by (auto simp add: K_def intro: card_mono) 1107 next 1108 fix i 1109 assume "i \<in> {1..card A} - {1..card K}" 1110 then have i: "i \<le> card A" "card K < i" 1111 by auto 1112 have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}" 1113 by (auto simp add: K_def) 1114 also have "\<dots> = {}" 1115 using \<open>finite A\<close> i by (auto simp add: K_def dest: card_mono[rotated 1]) 1116 finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0" 1117 by (simp only:) simp 1118 next 1119 fix i 1120 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)" 1121 (is "?lhs = ?rhs") 1122 by (rule sum.cong) (auto simp add: K_def) 1123 then show "(- 1) ^ (i + 1) * ?lhs = (- 1) ^ (i + 1) * ?rhs" 1124 by simp 1125 qed 1126 also have "{I. I \<subseteq> K \<and> card I = 0} = {{}}" 1127 using assms by (auto simp add: card_eq_0_iff K_def dest: finite_subset) 1128 then have "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1" 1129 by (subst (2) sum_head_Suc) simp_all 1130 also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1" 1131 using K by (subst n_subsets[symmetric]) simp_all 1132 also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1" 1133 by (subst sum_distrib_left[symmetric]) simp 1134 also have "\<dots> = - ((-1 + 1) ^ card K) + 1" 1135 by (subst binomial_ring) (simp add: ac_simps atMost_atLeast0) 1136 also have "\<dots> = 1" 1137 using x K by (auto simp add: K_def card_gt_0_iff) 1138 finally show "?lhs x = 1" . 1139 qed 1140 also have "nat \<dots> = card (\<Union>A)" 1141 by simp 1142 finally show ?thesis .. 1143qed 1144 1145text \<open>The number of nat lists of length \<open>m\<close> summing to \<open>N\<close> is @{term "(N + m - 1) choose N"}:\<close> 1146lemma card_length_sum_list_rec: 1147 assumes "m \<ge> 1" 1148 shows "card {l::nat list. length l = m \<and> sum_list l = N} = 1149 card {l. length l = (m - 1) \<and> sum_list l = N} + 1150 card {l. length l = m \<and> sum_list l + 1 = N}" 1151 (is "card ?C = card ?A + card ?B") 1152proof - 1153 let ?A' = "{l. length l = m \<and> sum_list l = N \<and> hd l = 0}" 1154 let ?B' = "{l. length l = m \<and> sum_list l = N \<and> hd l \<noteq> 0}" 1155 let ?f = "\<lambda>l. 0 # l" 1156 let ?g = "\<lambda>l. (hd l + 1) # tl l" 1157 have 1: "xs \<noteq> [] \<Longrightarrow> x = hd xs \<Longrightarrow> x # tl xs = xs" for x :: nat and xs 1158 by simp 1159 have 2: "xs \<noteq> [] \<Longrightarrow> sum_list(tl xs) = sum_list xs - hd xs" for xs :: "nat list" 1160 by (auto simp add: neq_Nil_conv) 1161 have f: "bij_betw ?f ?A ?A'" 1162 apply (rule bij_betw_byWitness[where f' = tl]) 1163 using assms 1164 apply (auto simp: 2 length_0_conv[symmetric] 1 simp del: length_0_conv) 1165 done 1166 have 3: "xs \<noteq> [] \<Longrightarrow> hd xs + (sum_list xs - hd xs) = sum_list xs" for xs :: "nat list" 1167 by (metis 1 sum_list_simps(2) 2) 1168 have g: "bij_betw ?g ?B ?B'" 1169 apply (rule bij_betw_byWitness[where f' = "\<lambda>l. (hd l - 1) # tl l"]) 1170 using assms 1171 by (auto simp: 2 length_0_conv[symmetric] intro!: 3 1172 simp del: length_greater_0_conv length_0_conv) 1173 have fin: "finite {xs. size xs = M \<and> set xs \<subseteq> {0..<N}}" for M N :: nat 1174 using finite_lists_length_eq[OF finite_atLeastLessThan] conj_commute by auto 1175 have fin_A: "finite ?A" using fin[of _ "N+1"] 1176 by (intro finite_subset[where ?A = "?A" and ?B = "{xs. size xs = m - 1 \<and> set xs \<subseteq> {0..<N+1}}"]) 1177 (auto simp: member_le_sum_list less_Suc_eq_le) 1178 have fin_B: "finite ?B" 1179 by (intro finite_subset[where ?A = "?B" and ?B = "{xs. size xs = m \<and> set xs \<subseteq> {0..<N}}"]) 1180 (auto simp: member_le_sum_list less_Suc_eq_le fin) 1181 have uni: "?C = ?A' \<union> ?B'" 1182 by auto 1183 have disj: "?A' \<inter> ?B' = {}" by blast 1184 have "card ?C = card(?A' \<union> ?B')" 1185 using uni by simp 1186 also have "\<dots> = card ?A + card ?B" 1187 using card_Un_disjoint[OF _ _ disj] bij_betw_finite[OF f] bij_betw_finite[OF g] 1188 bij_betw_same_card[OF f] bij_betw_same_card[OF g] fin_A fin_B 1189 by presburger 1190 finally show ?thesis . 1191qed 1192 1193lemma card_length_sum_list: "card {l::nat list. size l = m \<and> sum_list l = N} = (N + m - 1) choose N" 1194 \<comment> \<open>by Holden Lee, tidied by Tobias Nipkow\<close> 1195proof (cases m) 1196 case 0 1197 then show ?thesis 1198 by (cases N) (auto cong: conj_cong) 1199next 1200 case (Suc m') 1201 have m: "m \<ge> 1" 1202 by (simp add: Suc) 1203 then show ?thesis 1204 proof (induct "N + m - 1" arbitrary: N m) 1205 case 0 \<comment> \<open>In the base case, the only solution is [0].\<close> 1206 have [simp]: "{l::nat list. length l = Suc 0 \<and> (\<forall>n\<in>set l. n = 0)} = {[0]}" 1207 by (auto simp: length_Suc_conv) 1208 have "m = 1 \<and> N = 0" 1209 using 0 by linarith 1210 then show ?case 1211 by simp 1212 next 1213 case (Suc k) 1214 have c1: "card {l::nat list. size l = (m - 1) \<and> sum_list l = N} = (N + (m - 1) - 1) choose N" 1215 proof (cases "m = 1") 1216 case True 1217 with Suc.hyps have "N \<ge> 1" 1218 by auto 1219 with True show ?thesis 1220 by (simp add: binomial_eq_0) 1221 next 1222 case False 1223 then show ?thesis 1224 using Suc by fastforce 1225 qed 1226 from Suc have c2: "card {l::nat list. size l = m \<and> sum_list l + 1 = N} = 1227 (if N > 0 then ((N - 1) + m - 1) choose (N - 1) else 0)" 1228 proof - 1229 have *: "n > 0 \<Longrightarrow> Suc m = n \<longleftrightarrow> m = n - 1" for m n 1230 by arith 1231 from Suc have "N > 0 \<Longrightarrow> 1232 card {l::nat list. size l = m \<and> sum_list l + 1 = N} = 1233 ((N - 1) + m - 1) choose (N - 1)" 1234 by (simp add: *) 1235 then show ?thesis 1236 by auto 1237 qed 1238 from Suc.prems have "(card {l::nat list. size l = (m - 1) \<and> sum_list l = N} + 1239 card {l::nat list. size l = m \<and> sum_list l + 1 = N}) = (N + m - 1) choose N" 1240 by (auto simp: c1 c2 choose_reduce_nat[of "N + m - 1" N] simp del: One_nat_def) 1241 then show ?case 1242 using card_length_sum_list_rec[OF Suc.prems] by auto 1243 qed 1244qed 1245 1246lemma card_disjoint_shuffle: 1247 assumes "set xs \<inter> set ys = {}" 1248 shows "card (shuffle xs ys) = (length xs + length ys) choose length xs" 1249using assms 1250proof (induction xs ys rule: shuffle.induct) 1251 case (3 x xs y ys) 1252 have "shuffle (x # xs) (y # ys) = (#) x ` shuffle xs (y # ys) \<union> (#) y ` shuffle (x # xs) ys" 1253 by (rule shuffle.simps) 1254 also have "card \<dots> = card ((#) x ` shuffle xs (y # ys)) + card ((#) y ` shuffle (x # xs) ys)" 1255 by (rule card_Un_disjoint) (insert "3.prems", auto) 1256 also have "card ((#) x ` shuffle xs (y # ys)) = card (shuffle xs (y # ys))" 1257 by (rule card_image) auto 1258 also have "\<dots> = (length xs + length (y # ys)) choose length xs" 1259 using "3.prems" by (intro "3.IH") auto 1260 also have "card ((#) y ` shuffle (x # xs) ys) = card (shuffle (x # xs) ys)" 1261 by (rule card_image) auto 1262 also have "\<dots> = (length (x # xs) + length ys) choose length (x # xs)" 1263 using "3.prems" by (intro "3.IH") auto 1264 also have "length xs + length (y # ys) choose length xs + \<dots> = 1265 (length (x # xs) + length (y # ys)) choose length (x # xs)" by simp 1266 finally show ?case . 1267qed auto 1268 1269lemma Suc_times_binomial_add: "Suc a * (Suc (a + b) choose Suc a) = Suc b * (Suc (a + b) choose a)" 1270 \<comment> \<open>by Lukas Bulwahn\<close> 1271proof - 1272 have dvd: "Suc a * (fact a * fact b) dvd fact (Suc (a + b))" for a b 1273 using fact_fact_dvd_fact[of "Suc a" "b", where 'a=nat] 1274 by (simp only: fact_Suc add_Suc[symmetric] of_nat_id mult.assoc) 1275 have "Suc a * (fact (Suc (a + b)) div (Suc a * fact a * fact b)) = 1276 Suc a * fact (Suc (a + b)) div (Suc a * (fact a * fact b))" 1277 by (subst div_mult_swap[symmetric]; simp only: mult.assoc dvd) 1278 also have "\<dots> = Suc b * fact (Suc (a + b)) div (Suc b * (fact a * fact b))" 1279 by (simp only: div_mult_mult1) 1280 also have "\<dots> = Suc b * (fact (Suc (a + b)) div (Suc b * (fact a * fact b)))" 1281 using dvd[of b a] by (subst div_mult_swap[symmetric]; simp only: ac_simps dvd) 1282 finally show ?thesis 1283 by (subst (1 2) binomial_altdef_nat) 1284 (simp_all only: ac_simps diff_Suc_Suc Suc_diff_le diff_add_inverse fact_Suc of_nat_id) 1285qed 1286 1287 1288subsection \<open>Misc\<close> 1289 1290lemma gbinomial_code [code]: 1291 "a gchoose n = 1292 (if n = 0 then 1 1293 else fold_atLeastAtMost_nat (\<lambda>n acc. (a - of_nat n) * acc) 0 (n - 1) 1 / fact n)" 1294 by (cases n) 1295 (simp_all add: gbinomial_prod_rev prod_atLeastAtMost_code [symmetric] 1296 atLeastLessThanSuc_atLeastAtMost) 1297 1298declare [[code drop: binomial]] 1299 1300lemma binomial_code [code]: 1301 "(n choose k) = 1302 (if k > n then 0 1303 else if 2 * k > n then (n choose (n - k)) 1304 else (fold_atLeastAtMost_nat (( * ) ) (n-k+1) n 1 div fact k))" 1305proof - 1306 { 1307 assume "k \<le> n" 1308 then have "{1..n} = {1..n-k} \<union> {n-k+1..n}" by auto 1309 then have "(fact n :: nat) = fact (n-k) * \<Prod>{n-k+1..n}" 1310 by (simp add: prod.union_disjoint fact_prod) 1311 } 1312 then show ?thesis by (auto simp: binomial_altdef_nat mult_ac prod_atLeastAtMost_code) 1313qed 1314 1315end 1316