1 (* Title: HOL/Finite_Set.thy
2 Author: Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
3 with contributions by Jeremy Avigad
6 header {* Finite sets *}
12 subsection {* Definition and basic properties *}
14 inductive finite :: "'a set => bool"
16 emptyI [simp, intro!]: "finite {}"
17 | insertI [simp, intro!]: "finite A ==> finite (insert a A)"
19 lemma ex_new_if_finite: -- "does not depend on def of finite at all"
20 assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
21 shows "\<exists>a::'a. a \<notin> A"
23 from assms have "A \<noteq> UNIV" by blast
27 lemma finite_induct [case_names empty insert, induct set: finite]:
29 P {} ==> (!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
30 -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
33 insert: "!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
38 fix x F assume F: "finite F" and P: "P F"
42 hence "insert x F = F" by (rule insert_absorb)
43 with P show ?thesis by (simp only:)
46 from F this P show ?thesis by (rule insert)
51 lemma finite_ne_induct[case_names singleton insert, consumes 2]:
52 assumes fin: "finite F" shows "F \<noteq> {} \<Longrightarrow>
53 \<lbrakk> \<And>x. P{x};
54 \<And>x F. \<lbrakk> finite F; F \<noteq> {}; x \<notin> F; P F \<rbrakk> \<Longrightarrow> P (insert x F) \<rbrakk>
55 \<Longrightarrow> P F"
58 case empty thus ?case by simp
64 thus ?thesis using `P {x}` by simp
66 assume "F \<noteq> {}"
67 thus ?thesis using insert by blast
71 lemma finite_subset_induct [consumes 2, case_names empty insert]:
72 assumes "finite F" and "F \<subseteq> A"
74 and insert: "!!a F. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
77 from `finite F` and `F \<subseteq> A`
83 assume "finite F" and "x \<notin> F" and
84 P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
87 from i show "x \<in> A" by blast
88 from i have "F \<subseteq> A" by blast
90 show "finite F" by fact
91 show "x \<notin> F" by fact
97 text{* A finite choice principle. Does not need the SOME choice operator. *}
98 lemma finite_set_choice:
99 "finite A \<Longrightarrow> ALL x:A. (EX y. P x y) \<Longrightarrow> EX f. ALL x:A. P x (f x)"
100 proof (induct set: finite)
101 case empty thus ?case by simp
104 then obtain f b where f: "ALL x:A. P x (f x)" and ab: "P a b" by auto
105 show ?case (is "EX f. ?P f")
107 show "?P(%x. if x = a then b else f x)" using f ab by auto
112 text{* Finite sets are the images of initial segments of natural numbers: *}
114 lemma finite_imp_nat_seg_image_inj_on:
115 assumes fin: "finite A"
116 shows "\<exists> (n::nat) f. A = f ` {i. i<n} & inj_on f {i. i<n}"
121 proof show "\<exists>f. {} = f ` {i::nat. i < 0} & inj_on f {i. i<0}" by simp
125 have notinA: "a \<notin> A" by fact
126 from insert.hyps obtain n f
127 where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast
128 hence "insert a A = f(n:=a) ` {i. i < Suc n}"
129 "inj_on (f(n:=a)) {i. i < Suc n}" using notinA
130 by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
134 lemma nat_seg_image_imp_finite:
135 "!!f A. A = f ` {i::nat. i<n} \<Longrightarrow> finite A"
137 case 0 thus ?case by simp
140 let ?B = "f ` {i. i < n}"
141 have finB: "finite ?B" by(rule Suc.hyps[OF refl])
144 assume "\<exists>k<n. f n = f k"
145 hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
146 thus ?thesis using finB by simp
148 assume "\<not>(\<exists> k<n. f n = f k)"
149 hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
150 thus ?thesis using finB by simp
154 lemma finite_conv_nat_seg_image:
155 "finite A = (\<exists> (n::nat) f. A = f ` {i::nat. i<n})"
156 by(blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
158 lemma finite_imp_inj_to_nat_seg:
160 shows "EX f n::nat. f`A = {i. i<n} & inj_on f A"
162 from finite_imp_nat_seg_image_inj_on[OF `finite A`]
163 obtain f and n::nat where bij: "bij_betw f {i. i<n} A"
164 by (auto simp:bij_betw_def)
165 let ?f = "the_inv_into {i. i<n} f"
166 have "inj_on ?f A & ?f ` A = {i. i<n}"
167 by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij])
168 thus ?thesis by blast
171 lemma finite_Collect_less_nat[iff]: "finite{n::nat. n<k}"
172 by(fastsimp simp: finite_conv_nat_seg_image)
175 subsubsection{* Finiteness and set theoretic constructions *}
177 lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
178 by (induct set: finite) simp_all
180 lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
181 -- {* Every subset of a finite set is finite. *}
184 thus "!!A. A \<subseteq> B ==> finite A"
190 have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" by fact+
193 assume x: "x \<in> A"
194 with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
195 with r have "finite (A - {x})" .
196 hence "finite (insert x (A - {x}))" ..
197 also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
198 finally show ?thesis .
200 show "A \<subseteq> F ==> ?thesis" by fact
201 assume "x \<notin> A"
202 with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
207 lemma rev_finite_subset: "finite B ==> A \<subseteq> B ==> finite A"
208 by (rule finite_subset)
210 lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
211 by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
213 lemma finite_Collect_disjI[simp]:
214 "finite{x. P x | Q x} = (finite{x. P x} & finite{x. Q x})"
215 by(simp add:Collect_disj_eq)
217 lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
218 -- {* The converse obviously fails. *}
219 by (blast intro: finite_subset)
221 lemma finite_Collect_conjI [simp, intro]:
222 "finite{x. P x} | finite{x. Q x} ==> finite{x. P x & Q x}"
223 -- {* The converse obviously fails. *}
224 by(simp add:Collect_conj_eq)
226 lemma finite_Collect_le_nat[iff]: "finite{n::nat. n<=k}"
227 by(simp add: le_eq_less_or_eq)
229 lemma finite_insert [simp]: "finite (insert a A) = finite A"
230 apply (subst insert_is_Un)
231 apply (simp only: finite_Un, blast)
234 lemma finite_Union[simp, intro]:
235 "\<lbrakk> finite A; !!M. M \<in> A \<Longrightarrow> finite M \<rbrakk> \<Longrightarrow> finite(\<Union>A)"
236 by (induct rule:finite_induct) simp_all
238 lemma finite_Inter[intro]: "EX A:M. finite(A) \<Longrightarrow> finite(Inter M)"
239 by (blast intro: Inter_lower finite_subset)
241 lemma finite_INT[intro]: "EX x:I. finite(A x) \<Longrightarrow> finite(INT x:I. A x)"
242 by (blast intro: INT_lower finite_subset)
244 lemma finite_empty_induct:
247 and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
254 assume c: "finite c" and b: "finite b"
255 and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
256 have "c \<subseteq> b ==> P (b - c)"
260 from P1 show ?case by simp
263 have "P (b - F - {x})"
265 from _ b show "finite (b - F)" by (rule finite_subset) blast
266 from insert show "x \<in> b - F" by simp
267 from insert show "P (b - F)" by simp
269 also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
273 then show ?thesis by this (simp_all add: assms)
275 then show ?thesis by simp
278 lemma finite_Diff [simp]: "finite A ==> finite (A - B)"
279 by (rule Diff_subset [THEN finite_subset])
281 lemma finite_Diff2 [simp]:
282 assumes "finite B" shows "finite (A - B) = finite A"
284 have "finite A \<longleftrightarrow> finite((A-B) Un (A Int B))" by(simp add: Un_Diff_Int)
285 also have "\<dots> \<longleftrightarrow> finite(A-B)" using `finite B` by(simp)
286 finally show ?thesis ..
289 lemma finite_compl[simp]:
290 "finite(A::'a set) \<Longrightarrow> finite(-A) = finite(UNIV::'a set)"
291 by(simp add:Compl_eq_Diff_UNIV)
293 lemma finite_Collect_not[simp]:
294 "finite{x::'a. P x} \<Longrightarrow> finite{x. ~P x} = finite(UNIV::'a set)"
295 by(simp add:Collect_neg_eq)
297 lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
298 apply (subst Diff_insert)
299 apply (case_tac "a : A - B")
300 apply (rule finite_insert [symmetric, THEN trans])
301 apply (subst insert_Diff, simp_all)
305 text {* Image and Inverse Image over Finite Sets *}
307 lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
308 -- {* The image of a finite set is finite. *}
309 by (induct set: finite) simp_all
311 lemma finite_image_set [simp]:
312 "finite {x. P x} \<Longrightarrow> finite { f x | x. P x }"
313 by (simp add: image_Collect [symmetric])
315 lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
316 apply (frule finite_imageI)
317 apply (erule finite_subset, assumption)
320 lemma finite_range_imageI:
321 "finite (range g) ==> finite (range (%x. f (g x)))"
322 apply (drule finite_imageI, simp add: range_composition)
325 lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
327 have aux: "!!A. finite (A - {}) = finite A" by simp
330 thus "!!A. f`A = B ==> inj_on f A ==> finite A"
333 apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
335 apply (simp (no_asm_use) add: inj_on_def)
336 apply (blast dest!: aux [THEN iffD1], atomize)
337 apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
338 apply (frule subsetD [OF equalityD2 insertI1], clarify)
339 apply (rule_tac x = xa in bexI)
340 apply (simp_all add: inj_on_image_set_diff)
345 lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
346 -- {* The inverse image of a singleton under an injective function
347 is included in a singleton. *}
348 apply (auto simp add: inj_on_def)
349 apply (blast intro: the_equality [symmetric])
352 lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
353 -- {* The inverse image of a finite set under an injective function
355 apply (induct set: finite)
357 apply (subst vimage_insert)
358 apply (simp add: finite_subset [OF inj_vimage_singleton])
361 lemma finite_vimageD:
362 assumes fin: "finite (h -` F)" and surj: "surj h"
365 have "finite (h ` (h -` F))" using fin by (rule finite_imageI)
366 also have "h ` (h -` F) = F" using surj by (rule surj_image_vimage_eq)
367 finally show "finite F" .
370 lemma finite_vimage_iff: "bij h \<Longrightarrow> finite (h -` F) \<longleftrightarrow> finite F"
371 unfolding bij_def by (auto elim: finite_vimageD finite_vimageI)
374 text {* The finite UNION of finite sets *}
376 lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
377 by (induct set: finite) simp_all
381 @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \<noteq> {}})"}?
384 @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
387 lemma finite_UN [simp]:
388 "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
389 by (blast intro: finite_UN_I finite_subset)
391 lemma finite_Collect_bex[simp]: "finite A \<Longrightarrow>
392 finite{x. EX y:A. Q x y} = (ALL y:A. finite{x. Q x y})"
393 apply(subgoal_tac "{x. EX y:A. Q x y} = UNION A (%y. {x. Q x y})")
397 lemma finite_Collect_bounded_ex[simp]: "finite{y. P y} \<Longrightarrow>
398 finite{x. EX y. P y & Q x y} = (ALL y. P y \<longrightarrow> finite{x. Q x y})"
399 apply(subgoal_tac "{x. EX y. P y & Q x y} = UNION {y. P y} (%y. {x. Q x y})")
404 lemma finite_Plus: "[| finite A; finite B |] ==> finite (A <+> B)"
405 by (simp add: Plus_def)
408 fixes A :: "'a set" and B :: "'b set"
409 assumes fin: "finite (A <+> B)"
410 shows "finite A" "finite B"
412 have "Inl ` A \<subseteq> A <+> B" by auto
413 hence "finite (Inl ` A :: ('a + 'b) set)" using fin by(rule finite_subset)
414 thus "finite A" by(rule finite_imageD)(auto intro: inj_onI)
416 have "Inr ` B \<subseteq> A <+> B" by auto
417 hence "finite (Inr ` B :: ('a + 'b) set)" using fin by(rule finite_subset)
418 thus "finite B" by(rule finite_imageD)(auto intro: inj_onI)
421 lemma finite_Plus_iff[simp]: "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B"
422 by(auto intro: finite_PlusD finite_Plus)
424 lemma finite_Plus_UNIV_iff[simp]:
425 "finite (UNIV :: ('a + 'b) set) =
426 (finite (UNIV :: 'a set) & finite (UNIV :: 'b set))"
427 by(subst UNIV_Plus_UNIV[symmetric])(rule finite_Plus_iff)
430 text {* Sigma of finite sets *}
432 lemma finite_SigmaI [simp]:
433 "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
434 by (unfold Sigma_def) (blast intro!: finite_UN_I)
436 lemma finite_cartesian_product: "[| finite A; finite B |] ==>
438 by (rule finite_SigmaI)
440 lemma finite_Prod_UNIV:
441 "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
442 apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
444 apply (erule finite_SigmaI, auto)
447 lemma finite_cartesian_productD1:
448 "[| finite (A <*> B); B \<noteq> {} |] ==> finite A"
449 apply (auto simp add: finite_conv_nat_seg_image)
450 apply (drule_tac x=n in spec)
451 apply (drule_tac x="fst o f" in spec)
452 apply (auto simp add: o_def)
453 prefer 2 apply (force dest!: equalityD2)
454 apply (drule equalityD1)
455 apply (rename_tac y x)
456 apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)")
459 apply (rule_tac x=k in image_eqI, auto)
462 lemma finite_cartesian_productD2:
463 "[| finite (A <*> B); A \<noteq> {} |] ==> finite B"
464 apply (auto simp add: finite_conv_nat_seg_image)
465 apply (drule_tac x=n in spec)
466 apply (drule_tac x="snd o f" in spec)
467 apply (auto simp add: o_def)
468 prefer 2 apply (force dest!: equalityD2)
469 apply (drule equalityD1)
470 apply (rename_tac x y)
471 apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)")
474 apply (rule_tac x=k in image_eqI, auto)
478 text {* The powerset of a finite set *}
480 lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
482 assume "finite (Pow A)"
483 with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
484 thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
487 thus "finite (Pow A)"
488 by induct (simp_all add: Pow_insert)
491 lemma finite_Collect_subsets[simp,intro]: "finite A \<Longrightarrow> finite{B. B \<subseteq> A}"
492 by(simp add: Pow_def[symmetric])
495 lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
496 by(blast intro: finite_subset[OF subset_Pow_Union])
499 lemma finite_subset_image:
501 shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C"
502 using assms proof(induct)
503 case empty thus ?case by simp
505 case insert thus ?case
506 by (clarsimp simp del: image_insert simp add: image_insert[symmetric])
511 subsection {* Class @{text finite} *}
513 setup {* Sign.add_path "finite" *} -- {*FIXME: name tweaking*}
515 assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
516 setup {* Sign.parent_path *}
522 lemma finite [simp]: "finite (A \<Colon> 'a set)"
523 by (rule subset_UNIV finite_UNIV finite_subset)+
527 lemma UNIV_unit [noatp]:
528 "UNIV = {()}" by auto
530 instance unit :: finite proof
531 qed (simp add: UNIV_unit)
533 lemma UNIV_bool [noatp]:
534 "UNIV = {False, True}" by auto
536 instance bool :: finite proof
537 qed (simp add: UNIV_bool)
539 instance * :: (finite, finite) finite proof
540 qed (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
542 lemma finite_option_UNIV [simp]:
543 "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
544 by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
546 instance option :: (finite) finite proof
547 qed (simp add: UNIV_option_conv)
549 lemma inj_graph: "inj (%f. {(x, y). y = f x})"
550 by (rule inj_onI, auto simp add: expand_set_eq expand_fun_eq)
552 instance "fun" :: (finite, finite) finite
554 show "finite (UNIV :: ('a => 'b) set)"
555 proof (rule finite_imageD)
556 let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
557 have "range ?graph \<subseteq> Pow UNIV" by simp
558 moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
559 by (simp only: finite_Pow_iff finite)
560 ultimately show "finite (range ?graph)"
561 by (rule finite_subset)
562 show "inj ?graph" by (rule inj_graph)
566 instance "+" :: (finite, finite) finite proof
567 qed (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
570 subsection {* A fold functional for finite sets *}
572 text {* The intended behaviour is
573 @{text "fold f z {x\<^isub>1, ..., x\<^isub>n} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
574 if @{text f} is ``left-commutative'':
577 locale fun_left_comm =
578 fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
579 assumes fun_left_comm: "f x (f y z) = f y (f x z)"
582 text{* On a functional level it looks much nicer: *}
584 lemma fun_comp_comm: "f x \<circ> f y = f y \<circ> f x"
585 by (simp add: fun_left_comm expand_fun_eq)
589 inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
590 for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
591 emptyI [intro]: "fold_graph f z {} z" |
592 insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
593 \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
595 inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
597 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
598 [code del]: "fold f z A = (THE y. fold_graph f z A y)"
600 text{*A tempting alternative for the definiens is
601 @{term "if finite A then THE y. fold_graph f z A y else e"}.
602 It allows the removal of finiteness assumptions from the theorems
603 @{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
604 The proofs become ugly. It is not worth the effort. (???) *}
607 lemma Diff1_fold_graph:
608 "fold_graph f z (A - {x}) y \<Longrightarrow> x \<in> A \<Longrightarrow> fold_graph f z A (f x y)"
609 by (erule insert_Diff [THEN subst], rule fold_graph.intros, auto)
611 lemma fold_graph_imp_finite: "fold_graph f z A x \<Longrightarrow> finite A"
612 by (induct set: fold_graph) auto
614 lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
615 by (induct set: finite) auto
618 subsubsection{*From @{const fold_graph} to @{term fold}*}
620 lemma image_less_Suc: "h ` {i. i < Suc m} = insert (h m) (h ` {i. i < m})"
621 by (auto simp add: less_Suc_eq)
623 lemma insert_image_inj_on_eq:
624 "[|insert (h m) A = h ` {i. i < Suc m}; h m \<notin> A;
625 inj_on h {i. i < Suc m}|]
626 ==> A = h ` {i. i < m}"
627 apply (auto simp add: image_less_Suc inj_on_def)
628 apply (blast intro: less_trans)
631 lemma insert_inj_onE:
632 assumes aA: "insert a A = h`{i::nat. i<n}" and anot: "a \<notin> A"
633 and inj_on: "inj_on h {i::nat. i<n}"
634 shows "\<exists>hm m. inj_on hm {i::nat. i<m} & A = hm ` {i. i<m} & m < n"
636 case 0 thus ?thesis using aA by auto
639 have nSuc: "n = Suc m" by fact
640 have mlessn: "m<n" by (simp add: nSuc)
641 from aA obtain k where hkeq: "h k = a" and klessn: "k<n" by (blast elim!: equalityE)
642 let ?hm = "Fun.swap k m h"
643 have inj_hm: "inj_on ?hm {i. i < n}" using klessn mlessn
644 by (simp add: inj_on)
646 proof (intro exI conjI)
647 show "inj_on ?hm {i. i < m}" using inj_hm
648 by (auto simp add: nSuc less_Suc_eq intro: subset_inj_on)
649 show "m<n" by (rule mlessn)
650 show "A = ?hm ` {i. i < m}"
651 proof (rule insert_image_inj_on_eq)
652 show "inj_on (Fun.swap k m h) {i. i < Suc m}" using inj_hm nSuc by simp
653 show "?hm m \<notin> A" by (simp add: swap_def hkeq anot)
654 show "insert (?hm m) A = ?hm ` {i. i < Suc m}"
655 using aA hkeq nSuc klessn
656 by (auto simp add: swap_def image_less_Suc fun_upd_image
657 less_Suc_eq inj_on_image_set_diff [OF inj_on])
662 context fun_left_comm
665 lemma fold_graph_determ_aux:
666 "A = h`{i::nat. i<n} \<Longrightarrow> inj_on h {i. i<n}
667 \<Longrightarrow> fold_graph f z A x \<Longrightarrow> fold_graph f z A x'
668 \<Longrightarrow> x' = x"
669 proof (induct n arbitrary: A x x' h rule: less_induct)
671 have IH: "\<And>m h A x x'. m < n \<Longrightarrow> A = h ` {i. i<m}
672 \<Longrightarrow> inj_on h {i. i<m} \<Longrightarrow> fold_graph f z A x
673 \<Longrightarrow> fold_graph f z A x' \<Longrightarrow> x' = x" by fact
674 have Afoldx: "fold_graph f z A x" and Afoldx': "fold_graph f z A x'"
675 and A: "A = h`{i. i<n}" and injh: "inj_on h {i. i<n}" by fact+
677 proof (rule fold_graph.cases [OF Afoldx])
678 assume "A = {}" and "x = z"
679 with Afoldx' show "x' = x" by auto
682 assume AbB: "A = insert b B" and x: "x = f b u"
683 and notinB: "b \<notin> B" and Bu: "fold_graph f z B u"
685 proof (rule fold_graph.cases [OF Afoldx'])
686 assume "A = {}" and "x' = z"
687 with AbB show "x' = x" by blast
690 assume AcC: "A = insert c C" and x': "x' = f c v"
691 and notinC: "c \<notin> C" and Cv: "fold_graph f z C v"
692 from A AbB have Beq: "insert b B = h`{i. i<n}" by simp
693 from insert_inj_onE [OF Beq notinB injh]
694 obtain hB mB where inj_onB: "inj_on hB {i. i < mB}"
695 and Beq: "B = hB ` {i. i < mB}" and lessB: "mB < n" by auto
696 from A AcC have Ceq: "insert c C = h`{i. i<n}" by simp
697 from insert_inj_onE [OF Ceq notinC injh]
698 obtain hC mC where inj_onC: "inj_on hC {i. i < mC}"
699 and Ceq: "C = hC ` {i. i < mC}" and lessC: "mC < n" by auto
703 then moreover have "B = C" using AbB AcC notinB notinC by auto
704 ultimately show ?thesis using Bu Cv x x' IH [OF lessC Ceq inj_onC]
707 assume diff: "b \<noteq> c"
709 have B: "B = insert c ?D" and C: "C = insert b ?D"
710 using AbB AcC notinB notinC diff by(blast elim!:equalityE)+
711 have "finite A" by(rule fold_graph_imp_finite [OF Afoldx])
712 with AbB have "finite ?D" by simp
713 then obtain d where Dfoldd: "fold_graph f z ?D d"
714 using finite_imp_fold_graph by iprover
715 moreover have cinB: "c \<in> B" using B by auto
716 ultimately have "fold_graph f z B (f c d)" by(rule Diff1_fold_graph)
717 hence "f c d = u" by (rule IH [OF lessB Beq inj_onB Bu])
718 moreover have "f b d = v"
719 proof (rule IH[OF lessC Ceq inj_onC Cv])
720 show "fold_graph f z C (f b d)" using C notinB Dfoldd by fastsimp
722 ultimately show ?thesis
723 using fun_left_comm [of c b] x x' by (auto simp add: o_def)
729 lemma fold_graph_determ:
730 "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
731 apply (frule fold_graph_imp_finite [THEN finite_imp_nat_seg_image_inj_on])
732 apply (blast intro: fold_graph_determ_aux [rule_format])
736 "fold_graph f z A y \<Longrightarrow> fold f z A = y"
737 by (unfold fold_def) (blast intro: fold_graph_determ)
739 text{* The base case for @{text fold}: *}
741 lemma (in -) fold_empty [simp]: "fold f z {} = z"
742 by (unfold fold_def) blast
744 text{* The various recursion equations for @{const fold}: *}
746 lemma fold_insert_aux: "x \<notin> A
747 \<Longrightarrow> fold_graph f z (insert x A) v \<longleftrightarrow>
748 (\<exists>y. fold_graph f z A y \<and> v = f x y)"
750 apply (rule_tac A1 = A and f1 = f in finite_imp_fold_graph [THEN exE])
751 apply (fastsimp dest: fold_graph_imp_finite)
752 apply (blast intro: fold_graph_determ)
755 lemma fold_insert [simp]:
756 "finite A ==> x \<notin> A ==> fold f z (insert x A) = f x (fold f z A)"
757 apply (simp add: fold_def fold_insert_aux)
758 apply (rule the_equality)
759 apply (auto intro: finite_imp_fold_graph
760 cong add: conj_cong simp add: fold_def[symmetric] fold_equality)
764 "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
765 proof (induct rule: finite_induct)
766 case empty then show ?case by simp
768 case (insert y A) then show ?case
769 by (simp add: fun_left_comm[of x])
773 "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
774 by (simp add: fold_fun_comm)
777 assumes "finite A" and "x \<in> A"
778 shows "fold f z A = f x (fold f z (A - {x}))"
780 have A: "A = insert x (A - {x})" using `x \<in> A` by blast
781 then have "fold f z A = fold f z (insert x (A - {x}))" by simp
782 also have "\<dots> = f x (fold f z (A - {x}))"
783 by (rule fold_insert) (simp add: `finite A`)+
784 finally show ?thesis .
787 lemma fold_insert_remove:
789 shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
791 from `finite A` have "finite (insert x A)" by auto
792 moreover have "x \<in> insert x A" by auto
793 ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
795 then show ?thesis by simp
800 text{* A simplified version for idempotent functions: *}
802 locale fun_left_comm_idem = fun_left_comm +
803 assumes fun_left_idem: "f x (f x z) = f x z"
806 text{* The nice version: *}
807 lemma fun_comp_idem : "f x o f x = f x"
808 by (simp add: fun_left_idem expand_fun_eq)
810 lemma fold_insert_idem:
811 assumes fin: "finite A"
812 shows "fold f z (insert x A) = f x (fold f z A)"
815 then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
816 then show ?thesis using assms by (simp add:fun_left_idem)
818 assume "x \<notin> A" then show ?thesis using assms by simp
821 declare fold_insert[simp del] fold_insert_idem[simp]
823 lemma fold_insert_idem2:
824 "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
825 by(simp add:fold_fun_comm)
829 context ab_semigroup_idem_mult
832 lemma fun_left_comm_idem: "fun_left_comm_idem(op *)"
834 apply (rule mult_left_commute)
835 apply (rule mult_left_idem)
840 context semilattice_inf
843 lemma ab_semigroup_idem_mult_inf: "ab_semigroup_idem_mult inf"
844 proof qed (rule inf_assoc inf_commute inf_idem)+
846 lemma fold_inf_insert[simp]: "finite A \<Longrightarrow> fold inf b (insert a A) = inf a (fold inf b A)"
847 by(rule fun_left_comm_idem.fold_insert_idem[OF ab_semigroup_idem_mult.fun_left_comm_idem[OF ab_semigroup_idem_mult_inf]])
849 lemma inf_le_fold_inf: "finite A \<Longrightarrow> ALL a:A. b \<le> a \<Longrightarrow> inf b c \<le> fold inf c A"
850 by (induct pred: finite) (auto intro: le_infI1)
852 lemma fold_inf_le_inf: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> fold inf b A \<le> inf a b"
853 proof(induct arbitrary: a pred:finite)
854 case empty thus ?case by simp
859 assume "A = {}" thus ?thesis using insert by simp
861 assume "A \<noteq> {}" thus ?thesis using insert by (auto intro: le_infI2)
867 context semilattice_sup
870 lemma ab_semigroup_idem_mult_sup: "ab_semigroup_idem_mult sup"
871 by (rule semilattice_inf.ab_semigroup_idem_mult_inf)(rule dual_semilattice)
873 lemma fold_sup_insert[simp]: "finite A \<Longrightarrow> fold sup b (insert a A) = sup a (fold sup b A)"
874 by(rule semilattice_inf.fold_inf_insert)(rule dual_semilattice)
876 lemma fold_sup_le_sup: "finite A \<Longrightarrow> ALL a:A. a \<le> b \<Longrightarrow> fold sup c A \<le> sup b c"
877 by(rule semilattice_inf.inf_le_fold_inf)(rule dual_semilattice)
879 lemma sup_le_fold_sup: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> sup a b \<le> fold sup b A"
880 by(rule semilattice_inf.fold_inf_le_inf)(rule dual_semilattice)
885 subsubsection{* The derived combinator @{text fold_image} *}
887 definition fold_image :: "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
888 where "fold_image f g = fold (%x y. f (g x) y)"
890 lemma fold_image_empty[simp]: "fold_image f g z {} = z"
891 by(simp add:fold_image_def)
893 context ab_semigroup_mult
896 lemma fold_image_insert[simp]:
897 assumes "finite A" and "a \<notin> A"
898 shows "fold_image times g z (insert a A) = g a * (fold_image times g z A)"
900 interpret I: fun_left_comm "%x y. (g x) * y"
901 by unfold_locales (simp add: mult_ac)
902 show ?thesis using assms by(simp add:fold_image_def)
907 "finite A ==> (!!z. x * (fold times g z A) = fold times g (x * z) A)"
908 apply (induct set: finite)
910 apply (simp add: mult_left_commute [of x])
913 lemma fold_nest_Un_Int:
914 "finite A ==> finite B
915 ==> fold times g (fold times g z B) A = fold times g (fold times g z (A Int B)) (A Un B)"
916 apply (induct set: finite)
918 apply (simp add: fold_commute Int_insert_left insert_absorb)
921 lemma fold_nest_Un_disjoint:
922 "finite A ==> finite B ==> A Int B = {}
923 ==> fold times g z (A Un B) = fold times g (fold times g z B) A"
924 by (simp add: fold_nest_Un_Int)
927 lemma fold_image_reindex:
928 assumes fin: "finite A"
929 shows "inj_on h A \<Longrightarrow> fold_image times g z (h`A) = fold_image times (g\<circ>h) z A"
930 using fin by induct auto
934 Fusion theorem, as described in Graham Hutton's paper,
935 A Tutorial on the Universality and Expressiveness of Fold,
936 JFP 9:4 (355-372), 1999.
940 assumes "ab_semigroup_mult g"
941 assumes fin: "finite A"
942 and hyp: "\<And>x y. h (g x y) = times x (h y)"
943 shows "h (fold g j w A) = fold times j (h w) A"
945 class_interpret ab_semigroup_mult [g] by fact
946 show ?thesis using fin hyp by (induct set: finite) simp_all
950 lemma fold_image_cong:
951 "finite A \<Longrightarrow>
952 (!!x. x:A ==> g x = h x) ==> fold_image times g z A = fold_image times h z A"
953 apply (subgoal_tac "ALL C. C <= A --> (ALL x:C. g x = h x) --> fold_image times g z C = fold_image times h z C")
955 apply (erule finite_induct, simp)
956 apply (simp add: subset_insert_iff, clarify)
957 apply (subgoal_tac "finite C")
958 prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
959 apply (subgoal_tac "C = insert x (C - {x})")
963 apply (erule (1) notE impE)
964 apply (simp add: Ball_def del: insert_Diff_single)
969 context comm_monoid_mult
972 lemma fold_image_Un_Int:
973 "finite A ==> finite B ==>
974 fold_image times g 1 A * fold_image times g 1 B =
975 fold_image times g 1 (A Un B) * fold_image times g 1 (A Int B)"
976 by (induct set: finite)
977 (auto simp add: mult_ac insert_absorb Int_insert_left)
979 corollary fold_Un_disjoint:
980 "finite A ==> finite B ==> A Int B = {} ==>
981 fold_image times g 1 (A Un B) =
982 fold_image times g 1 A * fold_image times g 1 B"
983 by (simp add: fold_image_Un_Int)
985 lemma fold_image_UN_disjoint:
986 "\<lbrakk> finite I; ALL i:I. finite (A i);
987 ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
988 \<Longrightarrow> fold_image times g 1 (UNION I A) =
989 fold_image times (%i. fold_image times g 1 (A i)) 1 I"
990 apply (induct set: finite, simp, atomize)
991 apply (subgoal_tac "ALL i:F. x \<noteq> i")
993 apply (subgoal_tac "A x Int UNION F A = {}")
995 apply (simp add: fold_Un_disjoint)
998 lemma fold_image_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
999 fold_image times (%x. fold_image times (g x) 1 (B x)) 1 A =
1000 fold_image times (split g) 1 (SIGMA x:A. B x)"
1001 apply (subst Sigma_def)
1002 apply (subst fold_image_UN_disjoint, assumption, simp)
1004 apply (erule fold_image_cong)
1005 apply (subst fold_image_UN_disjoint, simp, simp)
1010 lemma fold_image_distrib: "finite A \<Longrightarrow>
1011 fold_image times (%x. g x * h x) 1 A =
1012 fold_image times g 1 A * fold_image times h 1 A"
1013 by (erule finite_induct) (simp_all add: mult_ac)
1015 lemma fold_image_related:
1017 and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)"
1018 and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
1019 shows "R (fold_image (op *) h e S) (fold_image (op *) g e S)"
1020 using fS by (rule finite_subset_induct) (insert assms, auto)
1022 lemma fold_image_eq_general:
1023 assumes fS: "finite S"
1024 and h: "\<forall>y\<in>S'. \<exists>!x. x\<in> S \<and> h(x) = y"
1025 and f12: "\<forall>x\<in>S. h x \<in> S' \<and> f2(h x) = f1 x"
1026 shows "fold_image (op *) f1 e S = fold_image (op *) f2 e S'"
1028 from h f12 have hS: "h ` S = S'" by auto
1029 {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
1030 from f12 h H have "x = y" by auto }
1031 hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
1032 from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto
1033 from hS have "fold_image (op *) f2 e S' = fold_image (op *) f2 e (h ` S)" by simp
1034 also have "\<dots> = fold_image (op *) (f2 o h) e S"
1035 using fold_image_reindex[OF fS hinj, of f2 e] .
1036 also have "\<dots> = fold_image (op *) f1 e S " using th fold_image_cong[OF fS, of "f2 o h" f1 e]
1038 finally show ?thesis ..
1041 lemma fold_image_eq_general_inverses:
1042 assumes fS: "finite S"
1043 and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
1044 and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = f x"
1045 shows "fold_image (op *) f e S = fold_image (op *) g e T"
1046 (* metis solves it, but not yet available here *)
1047 apply (rule fold_image_eq_general[OF fS, of T h g f e])
1053 apply (drule hk) apply simp
1055 apply (erule conjunct1[OF conjunct2[OF hk]])
1064 subsection{* A fold functional for non-empty sets *}
1066 text{* Does not require start value. *}
1069 fold1Set :: "('a => 'a => 'a) => 'a set => 'a => bool"
1070 for f :: "'a => 'a => 'a"
1072 fold1Set_insertI [intro]:
1073 "\<lbrakk> fold_graph f a A x; a \<notin> A \<rbrakk> \<Longrightarrow> fold1Set f (insert a A) x"
1075 definition fold1 :: "('a => 'a => 'a) => 'a set => 'a" where
1076 "fold1 f A == THE x. fold1Set f A x"
1078 lemma fold1Set_nonempty:
1079 "fold1Set f A x \<Longrightarrow> A \<noteq> {}"
1080 by(erule fold1Set.cases, simp_all)
1082 inductive_cases empty_fold1SetE [elim!]: "fold1Set f {} x"
1084 inductive_cases insert_fold1SetE [elim!]: "fold1Set f (insert a X) x"
1087 lemma fold1Set_sing [iff]: "(fold1Set f {a} b) = (a = b)"
1088 by (blast elim: fold_graph.cases)
1090 lemma fold1_singleton [simp]: "fold1 f {a} = a"
1091 by (unfold fold1_def) blast
1093 lemma finite_nonempty_imp_fold1Set:
1094 "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. fold1Set f A x"
1095 apply (induct A rule: finite_induct)
1096 apply (auto dest: finite_imp_fold_graph [of _ f])
1099 text{*First, some lemmas about @{const fold_graph}.*}
1101 context ab_semigroup_mult
1104 lemma fun_left_comm: "fun_left_comm(op *)"
1105 by unfold_locales (simp add: mult_ac)
1107 lemma fold_graph_insert_swap:
1108 assumes fold: "fold_graph times (b::'a) A y" and "b \<notin> A"
1109 shows "fold_graph times z (insert b A) (z * y)"
1111 interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
1112 from assms show ?thesis
1113 proof (induct rule: fold_graph.induct)
1114 case emptyI thus ?case by (force simp add: fold_insert_aux mult_commute)
1116 case (insertI x A y)
1117 have "fold_graph times z (insert x (insert b A)) (x * (z * y))"
1118 using insertI by force --{*how does @{term id} get unfolded?*}
1119 thus ?case by (simp add: insert_commute mult_ac)
1123 lemma fold_graph_permute_diff:
1124 assumes fold: "fold_graph times b A x"
1125 shows "!!a. \<lbrakk>a \<in> A; b \<notin> A\<rbrakk> \<Longrightarrow> fold_graph times a (insert b (A-{a})) x"
1127 proof (induct rule: fold_graph.induct)
1128 case emptyI thus ?case by simp
1130 case (insertI x A y)
1131 have "a = x \<or> a \<in> A" using insertI by simp
1135 with insertI show ?thesis
1136 by (simp add: id_def [symmetric], blast intro: fold_graph_insert_swap)
1138 assume ainA: "a \<in> A"
1139 hence "fold_graph times a (insert x (insert b (A - {a}))) (x * y)"
1140 using insertI by force
1142 have "insert x (insert b (A - {a})) = insert b (insert x A - {a})"
1143 using ainA insertI by blast
1144 ultimately show ?thesis by simp
1148 lemma fold1_eq_fold:
1149 assumes "finite A" "a \<notin> A" shows "fold1 times (insert a A) = fold times a A"
1151 interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
1152 from assms show ?thesis
1153 apply (simp add: fold1_def fold_def)
1154 apply (rule the_equality)
1155 apply (best intro: fold_graph_determ theI dest: finite_imp_fold_graph [of _ times])
1156 apply (rule sym, clarify)
1157 apply (case_tac "Aa=A")
1158 apply (best intro: fold_graph_determ)
1159 apply (subgoal_tac "fold_graph times a A x")
1160 apply (best intro: fold_graph_determ)
1161 apply (subgoal_tac "insert aa (Aa - {a}) = A")
1162 prefer 2 apply (blast elim: equalityE)
1163 apply (auto dest: fold_graph_permute_diff [where a=a])
1167 lemma nonempty_iff: "(A \<noteq> {}) = (\<exists>x B. A = insert x B & x \<notin> B)"
1170 apply (drule_tac x=x in spec)
1171 apply (drule_tac x="A-{x}" in spec, auto)
1175 assumes nonempty: "A \<noteq> {}" and A: "finite A" "x \<notin> A"
1176 shows "fold1 times (insert x A) = x * fold1 times A"
1178 interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
1179 from nonempty obtain a A' where "A = insert a A' & a ~: A'"
1180 by (auto simp add: nonempty_iff)
1182 by (simp add: insert_commute [of x] fold1_eq_fold eq_commute)
1187 context ab_semigroup_idem_mult
1190 lemma fold1_insert_idem [simp]:
1191 assumes nonempty: "A \<noteq> {}" and A: "finite A"
1192 shows "fold1 times (insert x A) = x * fold1 times A"
1194 interpret fun_left_comm_idem "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a"
1195 by (rule fun_left_comm_idem)
1196 from nonempty obtain a A' where A': "A = insert a A' & a ~: A'"
1197 by (auto simp add: nonempty_iff)
1204 with prems show ?thesis by simp
1206 assume "A' \<noteq> {}"
1207 with prems show ?thesis
1208 by (simp add: fold1_insert mult_assoc [symmetric])
1211 assume "a \<noteq> x"
1212 with prems show ?thesis
1213 by (simp add: insert_commute fold1_eq_fold)
1217 lemma hom_fold1_commute:
1218 assumes hom: "!!x y. h (x * y) = h x * h y"
1219 and N: "finite N" "N \<noteq> {}" shows "h (fold1 times N) = fold1 times (h ` N)"
1220 using N proof (induct rule: finite_ne_induct)
1221 case singleton thus ?case by simp
1224 then have "h (fold1 times (insert n N)) = h (n * fold1 times N)" by simp
1225 also have "\<dots> = h n * h (fold1 times N)" by(rule hom)
1226 also have "h (fold1 times N) = fold1 times (h ` N)" by(rule insert)
1227 also have "times (h n) \<dots> = fold1 times (insert (h n) (h ` N))"
1228 using insert by(simp)
1229 also have "insert (h n) (h ` N) = h ` insert n N" by simp
1230 finally show ?case .
1233 lemma fold1_eq_fold_idem:
1235 shows "fold1 times (insert a A) = fold times a A"
1236 proof (cases "a \<in> A")
1238 with assms show ?thesis by (simp add: fold1_eq_fold)
1240 interpret fun_left_comm_idem times by (fact fun_left_comm_idem)
1241 case True then obtain b B
1242 where A: "A = insert a B" and "a \<notin> B" by (rule set_insert)
1243 with assms have "finite B" by auto
1244 then have "fold times a (insert a B) = fold times (a * a) B"
1245 using `a \<notin> B` by (rule fold_insert2)
1247 using `a \<notin> B` `finite B` by (simp add: fold1_eq_fold A)
1253 text{* Now the recursion rules for definitions: *}
1255 lemma fold1_singleton_def: "g = fold1 f \<Longrightarrow> g {a} = a"
1258 lemma (in ab_semigroup_mult) fold1_insert_def:
1259 "\<lbrakk> g = fold1 times; finite A; x \<notin> A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
1260 by (simp add:fold1_insert)
1262 lemma (in ab_semigroup_idem_mult) fold1_insert_idem_def:
1263 "\<lbrakk> g = fold1 times; finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
1266 subsubsection{* Determinacy for @{term fold1Set} *}
1268 (*Not actually used!!*)
1270 context ab_semigroup_mult
1273 lemma fold_graph_permute:
1274 "[|fold_graph times id b (insert a A) x; a \<notin> A; b \<notin> A|]
1275 ==> fold_graph times id a (insert b A) x"
1277 apply (auto dest: fold_graph_permute_diff)
1280 lemma fold1Set_determ:
1281 "fold1Set times A x ==> fold1Set times A y ==> y = x"
1282 proof (clarify elim!: fold1Set.cases)
1284 assume Ax: "fold_graph times id a A x"
1285 assume By: "fold_graph times id b B y"
1286 assume anotA: "a \<notin> A"
1287 assume bnotB: "b \<notin> B"
1288 assume eq: "insert a A = insert b B"
1292 hence "A=B" using anotA bnotB eq by (blast elim!: equalityE)
1293 thus ?thesis using Ax By same by (blast intro: fold_graph_determ)
1295 assume diff: "a\<noteq>b"
1297 have B: "B = insert a ?D" and A: "A = insert b ?D"
1298 and aB: "a \<in> B" and bA: "b \<in> A"
1299 using eq anotA bnotB diff by (blast elim!:equalityE)+
1301 have "fold_graph times id a (insert b ?D) y"
1302 by (auto intro: fold_graph_permute simp add: insert_absorb)
1304 have "fold_graph times id a (insert b ?D) x"
1305 by (simp add: A [symmetric] Ax)
1306 ultimately show ?thesis by (blast intro: fold_graph_determ)
1310 lemma fold1Set_equality: "fold1Set times A y ==> fold1 times A = y"
1311 by (unfold fold1_def) (blast intro: fold1Set_determ)
1317 empty_fold_graphE [rule del] fold_graph.intros [rule del]
1318 empty_fold1SetE [rule del] insert_fold1SetE [rule del]
1319 -- {* No more proofs involve these relations. *}
1321 subsubsection {* Lemmas about @{text fold1} *}
1323 context ab_semigroup_mult
1327 assumes A: "finite A" "A \<noteq> {}"
1328 shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow> A Int B = {} \<Longrightarrow>
1329 fold1 times (A Un B) = fold1 times A * fold1 times B"
1330 using A by (induct rule: finite_ne_induct)
1331 (simp_all add: fold1_insert mult_assoc)
1334 assumes A: "finite (A)" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x,y}"
1335 shows "fold1 times A \<in> A"
1337 proof (induct rule:finite_ne_induct)
1338 case singleton thus ?case by simp
1340 case insert thus ?case using elem by (force simp add:fold1_insert)
1345 lemma (in ab_semigroup_idem_mult) fold1_Un2:
1346 assumes A: "finite A" "A \<noteq> {}"
1347 shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow>
1348 fold1 times (A Un B) = fold1 times A * fold1 times B"
1350 proof(induct rule:finite_ne_induct)
1351 case singleton thus ?case by simp
1353 case insert thus ?case by (simp add: mult_assoc)
1357 subsection {* Expressing set operations via @{const fold} *}
1359 lemma (in fun_left_comm) fun_left_comm_apply:
1360 "fun_left_comm (\<lambda>x. f (g x))"
1362 qed (simp_all add: fun_left_comm)
1364 lemma (in fun_left_comm_idem) fun_left_comm_idem_apply:
1365 "fun_left_comm_idem (\<lambda>x. f (g x))"
1366 by (rule fun_left_comm_idem.intro, rule fun_left_comm_apply, unfold_locales)
1367 (simp_all add: fun_left_idem)
1369 lemma fun_left_comm_idem_insert:
1370 "fun_left_comm_idem insert"
1374 lemma fun_left_comm_idem_remove:
1375 "fun_left_comm_idem (\<lambda>x A. A - {x})"
1379 lemma (in semilattice_inf) fun_left_comm_idem_inf:
1380 "fun_left_comm_idem inf"
1382 qed (auto simp add: inf_left_commute)
1384 lemma (in semilattice_sup) fun_left_comm_idem_sup:
1385 "fun_left_comm_idem sup"
1387 qed (auto simp add: sup_left_commute)
1389 lemma union_fold_insert:
1391 shows "A \<union> B = fold insert B A"
1393 interpret fun_left_comm_idem insert by (fact fun_left_comm_idem_insert)
1394 from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
1397 lemma minus_fold_remove:
1399 shows "B - A = fold (\<lambda>x A. A - {x}) B A"
1401 interpret fun_left_comm_idem "\<lambda>x A. A - {x}" by (fact fun_left_comm_idem_remove)
1402 from `finite A` show ?thesis by (induct A arbitrary: B) auto
1405 context complete_lattice
1408 lemma inf_Inf_fold_inf:
1410 shows "inf B (Inf A) = fold inf B A"
1412 interpret fun_left_comm_idem inf by (fact fun_left_comm_idem_inf)
1413 from `finite A` show ?thesis by (induct A arbitrary: B)
1414 (simp_all add: Inf_empty Inf_insert inf_commute fold_fun_comm)
1417 lemma sup_Sup_fold_sup:
1419 shows "sup B (Sup A) = fold sup B A"
1421 interpret fun_left_comm_idem sup by (fact fun_left_comm_idem_sup)
1422 from `finite A` show ?thesis by (induct A arbitrary: B)
1423 (simp_all add: Sup_empty Sup_insert sup_commute fold_fun_comm)
1428 shows "Inf A = fold inf top A"
1429 using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
1433 shows "Sup A = fold sup bot A"
1434 using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
1436 lemma inf_INFI_fold_inf:
1438 shows "inf B (INFI A f) = fold (\<lambda>A. inf (f A)) B A" (is "?inf = ?fold")
1440 interpret fun_left_comm_idem inf by (fact fun_left_comm_idem_inf)
1441 interpret fun_left_comm_idem "\<lambda>A. inf (f A)" by (fact fun_left_comm_idem_apply)
1442 from `finite A` show "?fold = ?inf"
1443 by (induct A arbitrary: B)
1444 (simp_all add: INFI_def Inf_empty Inf_insert inf_left_commute)
1447 lemma sup_SUPR_fold_sup:
1449 shows "sup B (SUPR A f) = fold (\<lambda>A. sup (f A)) B A" (is "?sup = ?fold")
1451 interpret fun_left_comm_idem sup by (fact fun_left_comm_idem_sup)
1452 interpret fun_left_comm_idem "\<lambda>A. sup (f A)" by (fact fun_left_comm_idem_apply)
1453 from `finite A` show "?fold = ?sup"
1454 by (induct A arbitrary: B)
1455 (simp_all add: SUPR_def Sup_empty Sup_insert sup_left_commute)
1458 lemma INFI_fold_inf:
1460 shows "INFI A f = fold (\<lambda>A. inf (f A)) top A"
1461 using assms inf_INFI_fold_inf [of A top] by simp
1463 lemma SUPR_fold_sup:
1465 shows "SUPR A f = fold (\<lambda>A. sup (f A)) bot A"
1466 using assms sup_SUPR_fold_sup [of A bot] by simp
1471 subsection {* Locales as mini-packages *}
1474 fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
1475 fixes F :: "'a set \<Rightarrow> 'b \<Rightarrow> 'b"
1476 assumes commute_comp: "f x \<circ> f y = f y \<circ> f x"
1477 assumes eq_fold: "F A s = Finite_Set.fold f s A"
1480 lemma fun_left_commute:
1481 "f x (f y s) = f y (f x s)"
1482 using commute_comp [of x y] by (simp add: expand_fun_eq)
1484 lemma fun_left_comm:
1487 qed (fact fun_left_commute)
1491 by (simp add: eq_fold expand_fun_eq)
1493 lemma insert [simp]:
1494 assumes "finite A" and "x \<notin> A"
1495 shows "F (insert x A) = F A \<circ> f x"
1497 interpret fun_left_comm f by (fact fun_left_comm)
1498 from fold_insert2 assms
1499 have "\<And>s. Finite_Set.fold f s (insert x A) = Finite_Set.fold f (f x s) A" .
1500 then show ?thesis by (simp add: eq_fold expand_fun_eq)
1504 assumes "finite A" and "x \<in> A"
1505 shows "F A = F (A - {x}) \<circ> f x"
1507 from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
1508 by (auto dest: mk_disjoint_insert)
1509 moreover from `finite A` this have "finite B" by simp
1510 ultimately show ?thesis by simp
1513 lemma insert_remove:
1515 shows "F (insert x A) = F (A - {x}) \<circ> f x"
1516 proof (cases "x \<in> A")
1517 case True with assms show ?thesis by (simp add: remove insert_absorb)
1519 case False with assms show ?thesis by simp
1522 lemma commute_comp':
1524 shows "f x \<circ> F A = F A \<circ> f x"
1527 from assms show "(f x \<circ> F A) s = (F A \<circ> f x) s"
1528 by (induct A arbitrary: s) (simp_all add: fun_left_commute)
1531 lemma fun_left_commute':
1533 shows "f x (F A s) = F A (f x s)"
1534 using commute_comp' assms by (simp add: expand_fun_eq)
1537 assumes "finite A" and "finite B"
1538 and "A \<inter> B = {}"
1539 shows "F (A \<union> B) = F A \<circ> F B"
1540 using `finite A` `A \<inter> B = {}` proof (induct A)
1541 case empty show ?case by simp
1544 then have "A \<inter> B = {}" by auto
1545 with insert(3) have "F (A \<union> B) = F A \<circ> F B" .
1546 moreover from insert have "x \<notin> B" by simp
1547 moreover from `finite A` `finite B` have fin: "finite (A \<union> B)" by simp
1548 moreover from `x \<notin> A` `x \<notin> B` have "x \<notin> A \<union> B" by simp
1549 ultimately show ?case by (simp add: fun_left_commute')
1554 locale folding_idem = folding +
1555 assumes idem_comp: "f x \<circ> f x = f x"
1558 declare insert [simp del]
1561 "f x (f x s) = f x s"
1562 using idem_comp [of x] by (simp add: expand_fun_eq)
1564 lemma fun_left_comm_idem:
1565 "fun_left_comm_idem f"
1567 qed (fact fun_left_commute fun_idem)+
1569 lemma insert_idem [simp]:
1571 shows "F (insert x A) = F A \<circ> f x"
1573 interpret fun_left_comm_idem f by (fact fun_left_comm_idem)
1574 from fold_insert_idem2 assms
1575 have "\<And>s. Finite_Set.fold f s (insert x A) = Finite_Set.fold f (f x s) A" .
1576 then show ?thesis by (simp add: eq_fold expand_fun_eq)
1580 assumes "finite A" and "finite B"
1581 shows "F (A \<union> B) = F A \<circ> F B"
1582 using `finite A` proof (induct A)
1583 case empty show ?case by simp
1586 from insert(3) have "F (A \<union> B) = F A \<circ> F B" .
1587 moreover from `finite A` `finite B` have fin: "finite (A \<union> B)" by simp
1588 ultimately show ?case by (simp add: fun_left_commute')