1082 with assms show ?thesis |
1085 with assms show ?thesis |
1083 by (simp add: binomial_altdef_of_nat prod_dividef) |
1086 by (simp add: binomial_altdef_of_nat prod_dividef) |
1084 qed |
1087 qed |
1085 |
1088 |
1086 |
1089 |
1087 subsection \<open>More on Binomial Coefficients\<close> |
1090 subsection \<open>Inclusion-exclusion principle\<close> |
1088 |
1091 |
1089 lemma choose_one: "n choose 1 = n" for n :: nat |
1092 lemma Inter_over_Union: |
1090 by simp |
1093 "\<Inter> {\<Union> (\<F> x) |x. x \<in> S} = \<Union> {\<Inter> (G ` S) |G. \<forall>x\<in>S. G x \<in> \<F> x}" |
|
1094 proof - |
|
1095 have "\<And>x. \<forall>s\<in>S. \<exists>X \<in> \<F> s. x \<in> X \<Longrightarrow> \<exists>G. (\<forall>x\<in>S. G x \<in> \<F> x) \<and> (\<forall>s\<in>S. x \<in> G s)" |
|
1096 by metis |
|
1097 then show ?thesis |
|
1098 by (auto simp flip: all_simps ex_simps) |
|
1099 qed |
|
1100 |
|
1101 lemma subset_insert_lemma: |
|
1102 "{T. T \<subseteq> (insert a S) \<and> P T} = {T. T \<subseteq> S \<and> P T} \<union> {insert a T |T. T \<subseteq> S \<and> P(insert a T)}" (is "?L=?R") |
|
1103 proof |
|
1104 show "?L \<subseteq> ?R" |
|
1105 by (smt (verit) UnI1 UnI2 insert_Diff mem_Collect_eq subsetI subset_insert_iff) |
|
1106 qed blast |
|
1107 |
|
1108 |
|
1109 text\<open>Versions for additive real functions, where the additivity applies only to some |
|
1110 specific subsets (e.g. cardinality of finite sets, measurable sets with bounded measure. |
|
1111 (From HOL Light)\<close> |
|
1112 |
|
1113 locale Incl_Excl = |
|
1114 fixes P :: "'a set \<Rightarrow> bool" and f :: "'a set \<Rightarrow> 'b::ring_1" |
|
1115 assumes disj_add: "\<lbrakk>P S; P T; disjnt S T\<rbrakk> \<Longrightarrow> f(S \<union> T) = f S + f T" |
|
1116 and empty: "P{}" |
|
1117 and Int: "\<lbrakk>P S; P T\<rbrakk> \<Longrightarrow> P(S \<inter> T)" |
|
1118 and Un: "\<lbrakk>P S; P T\<rbrakk> \<Longrightarrow> P(S \<union> T)" |
|
1119 and Diff: "\<lbrakk>P S; P T\<rbrakk> \<Longrightarrow> P(S - T)" |
|
1120 |
|
1121 begin |
|
1122 |
|
1123 lemma f_empty [simp]: "f{} = 0" |
|
1124 using disj_add empty by fastforce |
|
1125 |
|
1126 lemma f_Un_Int: "\<lbrakk>P S; P T\<rbrakk> \<Longrightarrow> f(S \<union> T) + f(S \<inter> T) = f S + f T" |
|
1127 by (smt (verit, ccfv_threshold) Groups.add_ac(2) Incl_Excl.Diff Incl_Excl.Int Incl_Excl_axioms Int_Diff_Un Int_Diff_disjoint Int_absorb Un_Diff Un_Int_eq(2) disj_add disjnt_def group_cancel.add2 sup_bot.right_neutral) |
|
1128 |
|
1129 lemma restricted_indexed: |
|
1130 assumes "finite A" and X: "\<And>a. a \<in> A \<Longrightarrow> P(X a)" |
|
1131 shows "f(\<Union>(X ` A)) = (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (- 1) ^ (card B + 1) * f (\<Inter> (X ` B)))" |
|
1132 proof - |
|
1133 have "\<lbrakk>finite A; card A = n; \<forall>a \<in> A. P (X a)\<rbrakk> |
|
1134 \<Longrightarrow> f(\<Union>(X ` A)) = (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (- 1) ^ (card B + 1) * f (\<Inter> (X ` B)))" for n X and A :: "'c set" |
|
1135 proof (induction n arbitrary: A X rule: less_induct) |
|
1136 case (less n0 A0 X) |
|
1137 show ?case |
|
1138 proof (cases "n0=0") |
|
1139 case True |
|
1140 with less show ?thesis |
|
1141 by fastforce |
|
1142 next |
|
1143 case False |
|
1144 with less.prems obtain A n a where *: "n0 = Suc n" "A0 = insert a A" "a \<notin> A" "card A = n" "finite A" |
|
1145 by (metis card_Suc_eq_finite not0_implies_Suc) |
|
1146 with less have "P (X a)" by blast |
|
1147 have APX: "\<forall>a \<in> A. P (X a)" |
|
1148 by (simp add: "*" less.prems) |
|
1149 have PUXA: "P (\<Union> (X ` A))" |
|
1150 using \<open>finite A\<close> APX |
|
1151 by (induction) (auto simp: empty Un) |
|
1152 have "f (\<Union> (X ` A0)) = f (X a \<union> \<Union> (X ` A))" |
|
1153 by (simp add: *) |
|
1154 also have "... = f (X a) + f (\<Union> (X ` A)) - f (X a \<inter> \<Union> (X ` A))" |
|
1155 using f_Un_Int add_diff_cancel PUXA \<open>P (X a)\<close> by metis |
|
1156 also have "... = f (X a) - (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (- 1) ^ card B * f (\<Inter> (X ` B))) + |
|
1157 (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (- 1) ^ card B * f (X a \<inter> \<Inter> (X ` B)))" |
|
1158 proof - |
|
1159 have 1: "f (\<Union>i\<in>A. X a \<inter> X i) = (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (- 1) ^ (card B + 1) * f (\<Inter>b\<in>B. X a \<inter> X b))" |
|
1160 using less.IH [of n A "\<lambda>i. X a \<inter> X i"] APX Int \<open>P (X a)\<close> by (simp add: *) |
|
1161 have 2: "X a \<inter> \<Union> (X ` A) = (\<Union>i\<in>A. X a \<inter> X i)" |
|
1162 by auto |
|
1163 have 3: "f (\<Union> (X ` A)) = (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (- 1) ^ (card B + 1) * f (\<Inter> (X ` B)))" |
|
1164 using less.IH [of n A X] APX Int \<open>P (X a)\<close> by (simp add: *) |
|
1165 show ?thesis |
|
1166 unfolding 3 2 1 |
|
1167 by (simp add: sum_negf) |
|
1168 qed |
|
1169 also have "... = (\<Sum>B | B \<subseteq> A0 \<and> B \<noteq> {}. (- 1) ^ (card B + 1) * f (\<Inter> (X ` B)))" |
|
1170 proof - |
|
1171 have F: "{insert a B |B. B \<subseteq> A} = insert a ` Pow A \<and> {B. B \<subseteq> A \<and> B \<noteq> {}} = Pow A - {{}}" |
|
1172 by auto |
|
1173 have G: "(\<Sum>B\<in>Pow A. (- 1) ^ card (insert a B) * f (X a \<inter> \<Inter> (X ` B))) = (\<Sum>B\<in>Pow A. - ((- 1) ^ card B * f (X a \<inter> \<Inter> (X ` B))))" |
|
1174 proof (rule sum.cong [OF refl]) |
|
1175 fix B |
|
1176 assume B: "B \<in> Pow A" |
|
1177 then have "finite B" |
|
1178 using \<open>finite A\<close> finite_subset by auto |
|
1179 show "(- 1) ^ card (insert a B) * f (X a \<inter> \<Inter> (X ` B)) = - ((- 1) ^ card B * f (X a \<inter> \<Inter> (X ` B)))" |
|
1180 using B * by (auto simp add: card_insert_if \<open>finite B\<close>) |
|
1181 qed |
|
1182 have disj: "{B. B \<subseteq> A \<and> B \<noteq> {}} \<inter> {insert a B |B. B \<subseteq> A} = {}" |
|
1183 using * by blast |
|
1184 have inj: "inj_on (insert a) (Pow A)" |
|
1185 using "*" inj_on_def by fastforce |
|
1186 show ?thesis |
|
1187 apply (simp add: * subset_insert_lemma sum.union_disjoint disj sum_negf) |
|
1188 apply (simp add: F G sum_negf sum.reindex [OF inj] o_def sum_diff *) |
|
1189 done |
|
1190 qed |
|
1191 finally show ?thesis . |
|
1192 qed |
|
1193 qed |
|
1194 then show ?thesis |
|
1195 by (meson assms) |
|
1196 qed |
|
1197 |
|
1198 lemma restricted: |
|
1199 assumes "finite A" "\<And>a. a \<in> A \<Longrightarrow> P a" |
|
1200 shows "f(\<Union> A) = (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (- 1) ^ (card B + 1) * f (\<Inter> B))" |
|
1201 using restricted_indexed [of A "\<lambda>x. x"] assms by auto |
|
1202 |
|
1203 end |
|
1204 |
|
1205 subsection\<open>Versions for unrestrictedly additive functions\<close> |
|
1206 |
|
1207 lemma Incl_Excl_UN: |
|
1208 fixes f :: "'a set \<Rightarrow> 'b::ring_1" |
|
1209 assumes "\<And>S T. disjnt S T \<Longrightarrow> f(S \<union> T) = f S + f T" "finite A" |
|
1210 shows "f(\<Union>(G ` A)) = (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (-1) ^ (card B + 1) * f (\<Inter> (G ` B)))" |
|
1211 proof - |
|
1212 interpret Incl_Excl "\<lambda>x. True" f |
|
1213 by (simp add: Incl_Excl.intro assms(1)) |
|
1214 show ?thesis |
|
1215 using restricted_indexed assms by blast |
|
1216 qed |
|
1217 |
|
1218 lemma Incl_Excl_Union: |
|
1219 fixes f :: "'a set \<Rightarrow> 'b::ring_1" |
|
1220 assumes "\<And>S T. disjnt S T \<Longrightarrow> f(S \<union> T) = f S + f T" "finite A" |
|
1221 shows "f(\<Union> A) = (\<Sum>B | B \<subseteq> A \<and> B \<noteq> {}. (- 1) ^ (card B + 1) * f (\<Inter> B))" |
|
1222 using Incl_Excl_UN[of f A "\<lambda>X. X"] assms by simp |
1091 |
1223 |
1092 text \<open>The famous inclusion-exclusion formula for the cardinality of a union\<close> |
1224 text \<open>The famous inclusion-exclusion formula for the cardinality of a union\<close> |
1093 lemma int_card_UNION: |
1225 lemma int_card_UNION: |
1094 assumes "finite A" |
1226 assumes "finite A" "\<And>K. K \<in> A \<Longrightarrow> finite K" |
1095 and "\<forall>k \<in> A. finite k" |
|
1096 shows "int (card (\<Union>A)) = (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))" |
1227 shows "int (card (\<Union>A)) = (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))" |
1097 (is "?lhs = ?rhs") |
1228 proof - |
1098 proof - |
1229 interpret Incl_Excl finite "int o card" |
1099 have "?rhs = (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * (\<Sum>_\<in>\<Inter>I. 1))" |
1230 proof qed (auto simp add: card_Un_disjnt) |
1100 by simp |
1231 show ?thesis |
1101 also have "\<dots> = (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (\<Sum>_\<in>\<Inter>I. (- 1) ^ (card I + 1)))" |
1232 using restricted assms by auto |
1102 by (subst sum_distrib_left) simp |
1233 qed |
1103 also have "\<dots> = (\<Sum>(I, _)\<in>Sigma {I. I \<subseteq> A \<and> I \<noteq> {}} Inter. (- 1) ^ (card I + 1))" |
1234 |
1104 using assms by (subst sum.Sigma) auto |
1235 text\<open>A more conventional form\<close> |
1105 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))" |
1236 lemma inclusion_exclusion: |
1106 by (rule sum.reindex_cong [where l = "\<lambda>(x, y). (y, x)"]) (auto intro: inj_onI) |
1237 assumes "finite A" "\<And>K. K \<in> A \<Longrightarrow> finite K" |
1107 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))" |
1238 shows "int(card(\<Union> A)) = |
1108 using assms |
1239 (\<Sum>n=1..card A. (-1) ^ (Suc n) * (\<Sum>B | B \<subseteq> A \<and> card B = n. int (card (\<Inter> B))))" (is "_=?R") |
1109 by (auto intro!: sum.mono_neutral_cong_right finite_SigmaI2 intro: finite_subset[where B="\<Union>A"]) |
1240 proof - |
1110 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)))" |
1241 have fin: "finite {I. I \<subseteq> A \<and> I \<noteq> {}}" |
1111 using assms by (subst sum.Sigma) auto |
1242 by (simp add: assms) |
1112 also have "\<dots> = (\<Sum>_\<in>\<Union>A. 1)" (is "sum ?lhs _ = _") |
1243 have "\<And>k. \<lbrakk>Suc 0 \<le> k; k \<le> card A\<rbrakk> \<Longrightarrow> \<exists>B\<subseteq>A. B \<noteq> {} \<and> k = card B" |
1113 proof (rule sum.cong[OF refl]) |
1244 by (metis (mono_tags, lifting) Suc_le_D Zero_neq_Suc card_eq_0_iff obtain_subset_with_card_n) |
1114 fix x |
1245 with \<open>finite A\<close> finite_subset |
1115 assume x: "x \<in> \<Union>A" |
1246 have card_eq: "card ` {I. I \<subseteq> A \<and> I \<noteq> {}} = {1..card A}" |
1116 define K where "K = {X \<in> A. x \<in> X}" |
1247 using not_less_eq_eq card_mono by (fastforce simp: image_iff) |
1117 with \<open>finite A\<close> have K: "finite K" |
1248 have "int(card(\<Union> A)) |
1118 by auto |
1249 = (\<Sum>y = 1..card A. \<Sum>I\<in>{x. x \<subseteq> A \<and> x \<noteq> {} \<and> card x = y}. - ((- 1) ^ y * int (card (\<Inter> I))))" |
1119 let ?I = "\<lambda>i. {I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I}" |
1250 by (simp add: int_card_UNION assms sum.image_gen [OF fin, where g=card] card_eq) |
1120 have "inj_on snd (SIGMA i:{1..card A}. ?I i)" |
1251 also have "... = ?R" |
1121 using assms by (auto intro!: inj_onI) |
1252 proof - |
1122 moreover have [symmetric]: "snd ` (SIGMA i:{1..card A}. ?I i) = {I. I \<subseteq> A \<and> I \<noteq> {} \<and> x \<in> \<Inter>I}" |
1253 have "{B. B \<subseteq> A \<and> B \<noteq> {} \<and> card B = k} = {B. B \<subseteq> A \<and> card B = k}" |
1123 using assms |
1254 if "Suc 0 \<le> k" and "k \<le> card A" for k |
1124 by (auto intro!: rev_image_eqI[where x="(card a, a)" for a] |
1255 using that by auto |
1125 simp add: card_gt_0_iff[folded Suc_le_eq] |
1256 then show ?thesis |
1126 dest: finite_subset intro: card_mono) |
1257 by (clarsimp simp add: sum_negf simp flip: sum_distrib_left) |
1127 ultimately have "?lhs x = (\<Sum>(i, I)\<in>(SIGMA i:{1..card A}. ?I i). (- 1) ^ (i + 1))" |
|
1128 by (rule sum.reindex_cong [where l = snd]) fastforce |
|
1129 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)))" |
|
1130 using assms by (subst sum.Sigma) auto |
|
1131 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))" |
|
1132 by (subst sum_distrib_left) simp |
|
1133 also have "\<dots> = (\<Sum>i=1..card K. (- 1) ^ (i + 1) * (\<Sum>I|I \<subseteq> K \<and> card I = i. 1))" |
|
1134 (is "_ = ?rhs") |
|
1135 proof (rule sum.mono_neutral_cong_right[rule_format]) |
|
1136 show "finite {1..card A}" |
|
1137 by simp |
|
1138 show "{1..card K} \<subseteq> {1..card A}" |
|
1139 using \<open>finite A\<close> by (auto simp add: K_def intro: card_mono) |
|
1140 next |
|
1141 fix i |
|
1142 assume "i \<in> {1..card A} - {1..card K}" |
|
1143 then have i: "i \<le> card A" "card K < i" |
|
1144 by auto |
|
1145 have "{I. I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I} = {I. I \<subseteq> K \<and> card I = i}" |
|
1146 by (auto simp add: K_def) |
|
1147 also have "\<dots> = {}" |
|
1148 using \<open>finite A\<close> i by (auto simp add: K_def dest: card_mono[rotated 1]) |
|
1149 finally show "(- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> A \<and> card I = i \<and> x \<in> \<Inter>I. 1 :: int) = 0" |
|
1150 by (metis mult_zero_right sum.empty) |
|
1151 next |
|
1152 fix i |
|
1153 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)" |
|
1154 (is "?lhs = ?rhs") |
|
1155 by (rule sum.cong) (auto simp add: K_def) |
|
1156 then show "(- 1) ^ (i + 1) * ?lhs = (- 1) ^ (i + 1) * ?rhs" |
|
1157 by simp |
|
1158 qed |
|
1159 also have "{I. I \<subseteq> K \<and> card I = 0} = {{}}" |
|
1160 using assms by (auto simp add: card_eq_0_iff K_def dest: finite_subset) |
|
1161 then have "?rhs = (\<Sum>i = 0..card K. (- 1) ^ (i + 1) * (\<Sum>I | I \<subseteq> K \<and> card I = i. 1 :: int)) + 1" |
|
1162 by (subst (2) sum.atLeast_Suc_atMost) simp_all |
|
1163 also have "\<dots> = (\<Sum>i = 0..card K. (- 1) * ((- 1) ^ i * int (card K choose i))) + 1" |
|
1164 using K by (subst n_subsets[symmetric]) simp_all |
|
1165 also have "\<dots> = - (\<Sum>i = 0..card K. (- 1) ^ i * int (card K choose i)) + 1" |
|
1166 by (subst sum_distrib_left[symmetric]) simp |
|
1167 also have "\<dots> = - ((-1 + 1) ^ card K) + 1" |
|
1168 by (subst binomial_ring) (simp add: ac_simps atMost_atLeast0) |
|
1169 also have "\<dots> = 1" |
|
1170 using x K by (auto simp add: K_def card_gt_0_iff) |
|
1171 finally show "?lhs x = 1" . |
|
1172 qed |
1258 qed |
1173 also have "\<dots> = int (card (\<Union>A))" |
1259 finally show ?thesis . |
1174 by simp |
|
1175 finally show ?thesis .. |
|
1176 qed |
1260 qed |
1177 |
1261 |
1178 lemma card_UNION: |
1262 lemma card_UNION: |
1179 assumes "finite A" |
1263 assumes "finite A" and "\<And>K. K \<in> A \<Longrightarrow> finite K" |
1180 and "\<forall>k \<in> A. finite k" |
|
1181 shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))" |
1264 shows "card (\<Union>A) = nat (\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I)))" |
1182 by (simp only: flip: int_card_UNION [OF assms]) |
1265 by (simp only: flip: int_card_UNION [OF assms]) |
1183 |
1266 |
1184 lemma card_UNION_nonneg: |
1267 lemma card_UNION_nonneg: |
1185 assumes "finite A" |
1268 assumes "finite A" and "\<And>K. K \<in> A \<Longrightarrow> finite K" |
1186 and "\<forall>k \<in> A. finite k" |
|
1187 shows "(\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I))) \<ge> 0" |
1269 shows "(\<Sum>I | I \<subseteq> A \<and> I \<noteq> {}. (- 1) ^ (card I + 1) * int (card (\<Inter>I))) \<ge> 0" |
1188 using int_card_UNION [OF assms] by presburger |
1270 using int_card_UNION [OF assms] by presburger |
|
1271 |
|
1272 subsection \<open>More on Binomial Coefficients\<close> |
1189 |
1273 |
1190 text \<open>The number of nat lists of length \<open>m\<close> summing to \<open>N\<close> is \<^term>\<open>(N + m - 1) choose N\<close>:\<close> |
1274 text \<open>The number of nat lists of length \<open>m\<close> summing to \<open>N\<close> is \<^term>\<open>(N + m - 1) choose N\<close>:\<close> |
1191 lemma card_length_sum_list_rec: |
1275 lemma card_length_sum_list_rec: |
1192 assumes "m \<ge> 1" |
1276 assumes "m \<ge> 1" |
1193 shows "card {l::nat list. length l = m \<and> sum_list l = N} = |
1277 shows "card {l::nat list. length l = m \<and> sum_list l = N} = |