129 |
129 |
130 lemma cong: |
130 lemma cong: |
131 assumes "A = B" |
131 assumes "A = B" |
132 assumes g_h: "\<And>x. x \<in> B \<Longrightarrow> g x = h x" |
132 assumes g_h: "\<And>x. x \<in> B \<Longrightarrow> g x = h x" |
133 shows "F g A = F h B" |
133 shows "F g A = F h B" |
134 proof (cases "finite A") |
134 using g_h unfolding `A = B` |
135 case True |
135 by (induct B rule: infinite_finite_induct) auto |
136 then have "\<And>C. C \<subseteq> A \<longrightarrow> (\<forall>x\<in>C. g x = h x) \<longrightarrow> F g C = F h C" |
|
137 proof induct |
|
138 case empty then show ?case by simp |
|
139 next |
|
140 case (insert x F) then show ?case apply - |
|
141 apply (simp add: subset_insert_iff, clarify) |
|
142 apply (subgoal_tac "finite C") |
|
143 prefer 2 apply (blast dest: finite_subset [rotated]) |
|
144 apply (subgoal_tac "C = insert x (C - {x})") |
|
145 prefer 2 apply blast |
|
146 apply (erule ssubst) |
|
147 apply (simp add: Ball_def del: insert_Diff_single) |
|
148 done |
|
149 qed |
|
150 with `A = B` g_h show ?thesis by simp |
|
151 next |
|
152 case False |
|
153 with `A = B` show ?thesis by simp |
|
154 qed |
|
155 |
136 |
156 lemma strong_cong [cong]: |
137 lemma strong_cong [cong]: |
157 assumes "A = B" "\<And>x. x \<in> B =simp=> g x = h x" |
138 assumes "A = B" "\<And>x. x \<in> B =simp=> g x = h x" |
158 shows "F (\<lambda>x. g x) A = F (\<lambda>x. h x) B" |
139 shows "F (\<lambda>x. g x) A = F (\<lambda>x. h x) B" |
159 by (rule cong) (insert assms, simp_all add: simp_implies_def) |
140 by (rule cong) (insert assms, simp_all add: simp_implies_def) |
204 and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" |
185 and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" |
205 and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)" |
186 and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)" |
206 shows "R (F h S) (F g S)" |
187 shows "R (F h S) (F g S)" |
207 using fS by (rule finite_subset_induct) (insert assms, auto) |
188 using fS by (rule finite_subset_induct) (insert assms, auto) |
208 |
189 |
209 lemma eq_general: |
|
210 assumes h: "\<forall>y\<in>S'. \<exists>!x. x \<in> S \<and> h x = y" |
|
211 and f12: "\<forall>x\<in>S. h x \<in> S' \<and> f2 (h x) = f1 x" |
|
212 shows "F f1 S = F f2 S'" |
|
213 proof- |
|
214 from h f12 have hS: "h ` S = S'" by blast |
|
215 {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y" |
|
216 from f12 h H have "x = y" by auto } |
|
217 hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast |
|
218 from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto |
|
219 from hS have "F f2 S' = F f2 (h ` S)" by simp |
|
220 also have "\<dots> = F (f2 o h) S" using reindex [OF hinj, of f2] . |
|
221 also have "\<dots> = F f1 S " using th cong [of _ _ "f2 o h" f1] |
|
222 by blast |
|
223 finally show ?thesis .. |
|
224 qed |
|
225 |
|
226 lemma eq_general_reverses: |
|
227 assumes kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y" |
|
228 and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = j x" |
|
229 shows "F j S = F g T" |
|
230 (* metis solves it, but not yet available here *) |
|
231 apply (rule eq_general [of T S h g j]) |
|
232 apply (rule ballI) |
|
233 apply (frule kh) |
|
234 apply (rule ex1I[]) |
|
235 apply blast |
|
236 apply clarsimp |
|
237 apply (drule hk) apply simp |
|
238 apply (rule sym) |
|
239 apply (erule conjunct1[OF conjunct2[OF hk]]) |
|
240 apply (rule ballI) |
|
241 apply (drule hk) |
|
242 apply blast |
|
243 done |
|
244 |
|
245 lemma mono_neutral_cong_left: |
190 lemma mono_neutral_cong_left: |
246 assumes "finite T" and "S \<subseteq> T" and "\<forall>i \<in> T - S. h i = 1" |
191 assumes "finite T" and "S \<subseteq> T" and "\<forall>i \<in> T - S. h i = 1" |
247 and "\<And>x. x \<in> S \<Longrightarrow> g x = h x" shows "F g S = F h T" |
192 and "\<And>x. x \<in> S \<Longrightarrow> g x = h x" shows "F g S = F h T" |
248 proof- |
193 proof- |
249 have eq: "T = S \<union> (T - S)" using `S \<subseteq> T` by blast |
194 have eq: "T = S \<union> (T - S)" using `S \<subseteq> T` by blast |
264 by (blast intro: mono_neutral_cong_left) |
209 by (blast intro: mono_neutral_cong_left) |
265 |
210 |
266 lemma mono_neutral_right: |
211 lemma mono_neutral_right: |
267 "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g T = F g S" |
212 "\<lbrakk> finite T; S \<subseteq> T; \<forall>i \<in> T - S. g i = 1 \<rbrakk> \<Longrightarrow> F g T = F g S" |
268 by (blast intro!: mono_neutral_left [symmetric]) |
213 by (blast intro!: mono_neutral_left [symmetric]) |
|
214 |
|
215 lemma reindex_bij_betw: "bij_betw h S T \<Longrightarrow> F (\<lambda>x. g (h x)) S = F g T" |
|
216 by (auto simp: bij_betw_def reindex) |
|
217 |
|
218 lemma reindex_bij_witness: |
|
219 assumes witness: |
|
220 "\<And>a. a \<in> S \<Longrightarrow> i (j a) = a" |
|
221 "\<And>a. a \<in> S \<Longrightarrow> j a \<in> T" |
|
222 "\<And>b. b \<in> T \<Longrightarrow> j (i b) = b" |
|
223 "\<And>b. b \<in> T \<Longrightarrow> i b \<in> S" |
|
224 assumes eq: |
|
225 "\<And>a. a \<in> S \<Longrightarrow> h (j a) = g a" |
|
226 shows "F g S = F h T" |
|
227 proof - |
|
228 have "bij_betw j S T" |
|
229 using bij_betw_byWitness[where A=S and f=j and f'=i and A'=T] witness by auto |
|
230 moreover have "F g S = F (\<lambda>x. h (j x)) S" |
|
231 by (intro cong) (auto simp: eq) |
|
232 ultimately show ?thesis |
|
233 by (simp add: reindex_bij_betw) |
|
234 qed |
|
235 |
|
236 lemma reindex_bij_betw_not_neutral: |
|
237 assumes fin: "finite S'" "finite T'" |
|
238 assumes bij: "bij_betw h (S - S') (T - T')" |
|
239 assumes nn: |
|
240 "\<And>a. a \<in> S' \<Longrightarrow> g (h a) = z" |
|
241 "\<And>b. b \<in> T' \<Longrightarrow> g b = z" |
|
242 shows "F (\<lambda>x. g (h x)) S = F g T" |
|
243 proof - |
|
244 have [simp]: "finite S \<longleftrightarrow> finite T" |
|
245 using bij_betw_finite[OF bij] fin by auto |
|
246 |
|
247 show ?thesis |
|
248 proof cases |
|
249 assume "finite S" |
|
250 with nn have "F (\<lambda>x. g (h x)) S = F (\<lambda>x. g (h x)) (S - S')" |
|
251 by (intro mono_neutral_cong_right) auto |
|
252 also have "\<dots> = F g (T - T')" |
|
253 using bij by (rule reindex_bij_betw) |
|
254 also have "\<dots> = F g T" |
|
255 using nn `finite S` by (intro mono_neutral_cong_left) auto |
|
256 finally show ?thesis . |
|
257 qed simp |
|
258 qed |
|
259 |
|
260 lemma reindex_bij_witness_not_neutral: |
|
261 assumes fin: "finite S'" "finite T'" |
|
262 assumes witness: |
|
263 "\<And>a. a \<in> S - S' \<Longrightarrow> i (j a) = a" |
|
264 "\<And>a. a \<in> S - S' \<Longrightarrow> j a \<in> T - T'" |
|
265 "\<And>b. b \<in> T - T' \<Longrightarrow> j (i b) = b" |
|
266 "\<And>b. b \<in> T - T' \<Longrightarrow> i b \<in> S - S'" |
|
267 assumes nn: |
|
268 "\<And>a. a \<in> S' \<Longrightarrow> g a = z" |
|
269 "\<And>b. b \<in> T' \<Longrightarrow> h b = z" |
|
270 assumes eq: |
|
271 "\<And>a. a \<in> S \<Longrightarrow> h (j a) = g a" |
|
272 shows "F g S = F h T" |
|
273 proof - |
|
274 have bij: "bij_betw j (S - (S' \<inter> S)) (T - (T' \<inter> T))" |
|
275 using witness by (intro bij_betw_byWitness[where f'=i]) auto |
|
276 have F_eq: "F g S = F (\<lambda>x. h (j x)) S" |
|
277 by (intro cong) (auto simp: eq) |
|
278 show ?thesis |
|
279 unfolding F_eq using fin nn eq |
|
280 by (intro reindex_bij_betw_not_neutral[OF _ _ bij]) auto |
|
281 qed |
269 |
282 |
270 lemma delta: |
283 lemma delta: |
271 assumes fS: "finite S" |
284 assumes fS: "finite S" |
272 shows "F (\<lambda>k. if k = a then b k else 1) S = (if a \<in> S then b a else 1)" |
285 shows "F (\<lambda>k. if k = a then b k else 1) S = (if a \<in> S then b a else 1)" |
273 proof- |
286 proof- |
401 |
414 |
402 context comm_monoid_add |
415 context comm_monoid_add |
403 begin |
416 begin |
404 |
417 |
405 lemma setsum_reindex_id: |
418 lemma setsum_reindex_id: |
406 "inj_on f B ==> setsum f B = setsum id (f ` B)" |
419 "inj_on f B \<Longrightarrow> setsum f B = setsum id (f ` B)" |
407 by (simp add: setsum.reindex) |
420 by (simp add: setsum.reindex) |
408 |
421 |
409 lemma setsum_reindex_nonzero: |
422 lemma setsum_reindex_nonzero: |
410 assumes fS: "finite S" |
423 assumes fS: "finite S" |
411 and nz: "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x \<noteq> y \<Longrightarrow> f x = f y \<Longrightarrow> h (f x) = 0" |
424 and nz: "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x \<noteq> y \<Longrightarrow> f x = f y \<Longrightarrow> h (f x) = 0" |
412 shows "setsum h (f ` S) = setsum (h \<circ> f) S" |
425 shows "setsum h (f ` S) = setsum (h \<circ> f) S" |
413 using nz proof (induct rule: finite_induct [OF fS]) |
426 proof (subst setsum.reindex_bij_betw_not_neutral[symmetric]) |
414 case 1 thus ?case by simp |
427 show "bij_betw f (S - {x\<in>S. h (f x) = 0}) (f`S - f`{x\<in>S. h (f x) = 0})" |
415 next |
428 using nz by (auto intro!: inj_onI simp: bij_betw_def) |
416 case (2 x F) |
429 qed (insert fS, auto) |
417 { assume fxF: "f x \<in> f ` F" hence "\<exists>y \<in> F . f y = f x" by auto |
|
418 then obtain y where y: "y \<in> F" "f x = f y" by auto |
|
419 from "2.hyps" y have xy: "x \<noteq> y" by auto |
|
420 from "2.prems" [of x y] "2.hyps" xy y have h0: "h (f x) = 0" by simp |
|
421 have "setsum h (f ` insert x F) = setsum h (f ` F)" using fxF by auto |
|
422 also have "\<dots> = setsum (h o f) (insert x F)" |
|
423 unfolding setsum.insert[OF `finite F` `x\<notin>F`] |
|
424 using h0 |
|
425 apply (simp cong del: setsum.strong_cong) |
|
426 apply (rule "2.hyps"(3)) |
|
427 apply (rule_tac y="y" in "2.prems") |
|
428 apply simp_all |
|
429 done |
|
430 finally have ?case . } |
|
431 moreover |
|
432 { assume fxF: "f x \<notin> f ` F" |
|
433 have "setsum h (f ` insert x F) = h (f x) + setsum h (f ` F)" |
|
434 using fxF "2.hyps" by simp |
|
435 also have "\<dots> = setsum (h o f) (insert x F)" |
|
436 unfolding setsum.insert[OF `finite F` `x\<notin>F`] |
|
437 apply (simp cong del: setsum.strong_cong) |
|
438 apply (rule cong [OF refl [of "op + (h (f x))"]]) |
|
439 apply (rule "2.hyps"(3)) |
|
440 apply (rule_tac y="y" in "2.prems") |
|
441 apply simp_all |
|
442 done |
|
443 finally have ?case . } |
|
444 ultimately show ?case by blast |
|
445 qed |
|
446 |
430 |
447 lemma setsum_cong2: |
431 lemma setsum_cong2: |
448 "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> setsum f A = setsum g A" |
432 "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> setsum f A = setsum g A" |
449 by (auto intro: setsum.cong) |
433 by (auto intro: setsum.cong) |
450 |
434 |
492 |
476 |
493 text {* Commuting outer and inner summation *} |
477 text {* Commuting outer and inner summation *} |
494 |
478 |
495 lemma setsum_commute: |
479 lemma setsum_commute: |
496 "(\<Sum>i\<in>A. \<Sum>j\<in>B. f i j) = (\<Sum>j\<in>B. \<Sum>i\<in>A. f i j)" |
480 "(\<Sum>i\<in>A. \<Sum>j\<in>B. f i j) = (\<Sum>j\<in>B. \<Sum>i\<in>A. f i j)" |
497 proof (simp add: setsum_cartesian_product) |
481 unfolding setsum_cartesian_product |
498 have "(\<Sum>(x,y) \<in> A <*> B. f x y) = |
482 by (rule setsum.reindex_bij_witness[where i="\<lambda>(i, j). (j, i)" and j="\<lambda>(i, j). (j, i)"]) auto |
499 (\<Sum>(y,x) \<in> (%(i, j). (j, i)) ` (A \<times> B). f x y)" |
|
500 (is "?s = _") |
|
501 apply (simp add: setsum.reindex [where h = "%(i, j). (j, i)"] swap_inj_on) |
|
502 apply (simp add: split_def) |
|
503 done |
|
504 also have "... = (\<Sum>(y,x)\<in>B \<times> A. f x y)" |
|
505 (is "_ = ?t") |
|
506 apply (simp add: swap_product) |
|
507 done |
|
508 finally show "?s = ?t" . |
|
509 qed |
|
510 |
483 |
511 lemma setsum_Plus: |
484 lemma setsum_Plus: |
512 fixes A :: "'a set" and B :: "'b set" |
485 fixes A :: "'a set" and B :: "'b set" |
513 assumes fin: "finite A" "finite B" |
486 assumes fin: "finite A" "finite B" |
514 shows "setsum f (A <+> B) = setsum (f \<circ> Inl) A + setsum (f \<circ> Inr) B" |
487 shows "setsum f (A <+> B) = setsum (f \<circ> Inl) A + setsum (f \<circ> Inr) B" |
613 |
586 |
614 lemma setsum_Un_zero: |
587 lemma setsum_Un_zero: |
615 "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 0 \<rbrakk> \<Longrightarrow> |
588 "\<lbrakk> finite S; finite T; \<forall>x \<in> S\<inter>T. f x = 0 \<rbrakk> \<Longrightarrow> |
616 setsum f (S \<union> T) = setsum f S + setsum f T" |
589 setsum f (S \<union> T) = setsum f S + setsum f T" |
617 by (fact setsum.union_inter_neutral) |
590 by (fact setsum.union_inter_neutral) |
618 |
|
619 lemma setsum_eq_general_reverses: |
|
620 assumes fS: "finite S" and fT: "finite T" |
|
621 and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y" |
|
622 and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = f x" |
|
623 shows "setsum f S = setsum g T" |
|
624 using kh hk by (fact setsum.eq_general_reverses) |
|
625 |
|
626 |
591 |
627 subsubsection {* Properties in more restricted classes of structures *} |
592 subsubsection {* Properties in more restricted classes of structures *} |
628 |
593 |
629 lemma setsum_Un: "finite A ==> finite B ==> |
594 lemma setsum_Un: "finite A ==> finite B ==> |
630 (setsum f (A Un B) :: 'a :: ab_group_add) = |
595 (setsum f (A Un B) :: 'a :: ab_group_add) = |
1122 lemma setprod_reindex_cong: |
1087 lemma setprod_reindex_cong: |
1123 "inj_on f A ==> B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A" |
1088 "inj_on f A ==> B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A" |
1124 by (frule setprod.reindex, simp) |
1089 by (frule setprod.reindex, simp) |
1125 |
1090 |
1126 lemma strong_setprod_reindex_cong: |
1091 lemma strong_setprod_reindex_cong: |
1127 assumes i: "inj_on f A" |
1092 "inj_on f A \<Longrightarrow> B = f ` A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> g x = (h \<circ> f) x) \<Longrightarrow> setprod h B = setprod g A" |
1128 and B: "B = f ` A" and eq: "\<And>x. x \<in> A \<Longrightarrow> g x = (h \<circ> f) x" |
1093 by (subst setprod.reindex_bij_betw[symmetric, where h=f]) |
1129 shows "setprod h B = setprod g A" |
1094 (auto simp: bij_betw_def) |
1130 proof- |
|
1131 have "setprod h B = setprod (h o f) A" |
|
1132 by (simp add: B setprod.reindex [OF i, of h]) |
|
1133 then show ?thesis apply simp |
|
1134 apply (rule setprod.cong) |
|
1135 apply simp |
|
1136 by (simp add: eq) |
|
1137 qed |
|
1138 |
1095 |
1139 lemma setprod_Union_disjoint: |
1096 lemma setprod_Union_disjoint: |
1140 assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}" |
1097 assumes "\<forall>A\<in>C. finite A" "\<forall>A\<in>C. \<forall>B\<in>C. A \<noteq> B \<longrightarrow> A Int B = {}" |
1141 shows "setprod f (Union C) = setprod (setprod f) C" |
1098 shows "setprod f (Union C) = setprod (setprod f) C" |
1142 using assms by (fact setprod.Union_disjoint) |
1099 using assms by (fact setprod.Union_disjoint) |