wenzelm@12396: (* Title: HOL/Finite_Set.thy wenzelm@12396: ID: $Id$ wenzelm@12396: Author: Tobias Nipkow, Lawrence C Paulson and Markus Wenzel paulson@14430: Additions by Jeremy Avigad in Feb 2004 wenzelm@12396: *) wenzelm@12396: wenzelm@12396: header {* Finite sets *} wenzelm@12396: nipkow@15131: theory Finite_Set nipkow@15512: imports Divides Power Inductive Lattice_Locales nipkow@15131: begin wenzelm@12396: nipkow@15392: subsection {* Definition and basic properties *} wenzelm@12396: wenzelm@12396: consts Finites :: "'a set set" nipkow@13737: syntax nipkow@13737: finite :: "'a set => bool" nipkow@13737: translations nipkow@13737: "finite A" == "A : Finites" wenzelm@12396: wenzelm@12396: inductive Finites wenzelm@12396: intros wenzelm@12396: emptyI [simp, intro!]: "{} : Finites" wenzelm@12396: insertI [simp, intro!]: "A : Finites ==> insert a A : Finites" wenzelm@12396: wenzelm@12396: axclass finite \ type wenzelm@12396: finite: "finite UNIV" wenzelm@12396: nipkow@13737: lemma ex_new_if_finite: -- "does not depend on def of finite at all" wenzelm@14661: assumes "\ finite (UNIV :: 'a set)" and "finite A" wenzelm@14661: shows "\a::'a. a \ A" wenzelm@14661: proof - wenzelm@14661: from prems have "A \ UNIV" by blast wenzelm@14661: thus ?thesis by blast wenzelm@14661: qed wenzelm@12396: wenzelm@12396: lemma finite_induct [case_names empty insert, induct set: Finites]: wenzelm@12396: "finite F ==> nipkow@15327: P {} ==> (!!x F. finite F ==> x \ F ==> P F ==> P (insert x F)) ==> P F" wenzelm@12396: -- {* Discharging @{text "x \ F"} entails extra work. *} wenzelm@12396: proof - wenzelm@13421: assume "P {}" and nipkow@15327: insert: "!!x F. finite F ==> x \ F ==> P F ==> P (insert x F)" wenzelm@12396: assume "finite F" wenzelm@12396: thus "P F" wenzelm@12396: proof induct wenzelm@12396: show "P {}" . nipkow@15327: fix x F assume F: "finite F" and P: "P F" wenzelm@12396: show "P (insert x F)" wenzelm@12396: proof cases wenzelm@12396: assume "x \ F" wenzelm@12396: hence "insert x F = F" by (rule insert_absorb) wenzelm@12396: with P show ?thesis by (simp only:) wenzelm@12396: next wenzelm@12396: assume "x \ F" wenzelm@12396: from F this P show ?thesis by (rule insert) wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: nipkow@15484: lemma finite_ne_induct[case_names singleton insert, consumes 2]: nipkow@15484: assumes fin: "finite F" shows "F \ {} \ nipkow@15484: \ \x. P{x}; nipkow@15484: \x F. \ finite F; F \ {}; x \ F; P F \ \ P (insert x F) \ nipkow@15484: \ P F" nipkow@15484: using fin nipkow@15484: proof induct nipkow@15484: case empty thus ?case by simp nipkow@15484: next nipkow@15484: case (insert x F) nipkow@15484: show ?case nipkow@15484: proof cases nipkow@15484: assume "F = {}" thus ?thesis using insert(4) by simp nipkow@15484: next nipkow@15484: assume "F \ {}" thus ?thesis using insert by blast nipkow@15484: qed nipkow@15484: qed nipkow@15484: wenzelm@12396: lemma finite_subset_induct [consumes 2, case_names empty insert]: wenzelm@12396: "finite F ==> F \ A ==> nipkow@15327: P {} ==> (!!a F. finite F ==> a \ A ==> a \ F ==> P F ==> P (insert a F)) ==> wenzelm@12396: P F" wenzelm@12396: proof - wenzelm@13421: assume "P {}" and insert: nipkow@15327: "!!a F. finite F ==> a \ A ==> a \ F ==> P F ==> P (insert a F)" wenzelm@12396: assume "finite F" wenzelm@12396: thus "F \ A ==> P F" wenzelm@12396: proof induct wenzelm@12396: show "P {}" . nipkow@15327: fix x F assume "finite F" and "x \ F" wenzelm@12396: and P: "F \ A ==> P F" and i: "insert x F \ A" wenzelm@12396: show "P (insert x F)" wenzelm@12396: proof (rule insert) wenzelm@12396: from i show "x \ A" by blast wenzelm@12396: from i have "F \ A" by blast wenzelm@12396: with P show "P F" . wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: nipkow@15392: text{* Finite sets are the images of initial segments of natural numbers: *} nipkow@15392: paulson@15510: lemma finite_imp_nat_seg_image_inj_on: paulson@15510: assumes fin: "finite A" paulson@15510: shows "\ (n::nat) f. A = f ` {i. if. {} = f ` {i::nat. i < 0} & inj_on f {i. i<0}" by simp paulson@15510: qed nipkow@15392: next nipkow@15392: case (insert a A) paulson@15510: have notinA: "a \ A" . paulson@15510: from insert.hyps obtain n f paulson@15510: where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast paulson@15510: hence "insert a A = f(n:=a) ` {i. i < Suc n}" paulson@15510: "inj_on (f(n:=a)) {i. i < Suc n}" using notinA paulson@15510: by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq) nipkow@15392: thus ?case by blast nipkow@15392: qed nipkow@15392: nipkow@15392: lemma nat_seg_image_imp_finite: nipkow@15392: "!!f A. A = f ` {i::nat. i finite A" nipkow@15392: proof (induct n) nipkow@15392: case 0 thus ?case by simp nipkow@15392: next nipkow@15392: case (Suc n) nipkow@15392: let ?B = "f ` {i. i < n}" nipkow@15392: have finB: "finite ?B" by(rule Suc.hyps[OF refl]) nipkow@15392: show ?case nipkow@15392: proof cases nipkow@15392: assume "\k(\ k (n::nat) f. A = f ` {i::nat. i finite G ==> finite (F Un G)" wenzelm@12396: -- {* The union of two finite sets is finite. *} wenzelm@12396: by (induct set: Finites) simp_all wenzelm@12396: wenzelm@12396: lemma finite_subset: "A \ B ==> finite B ==> finite A" wenzelm@12396: -- {* Every subset of a finite set is finite. *} wenzelm@12396: proof - wenzelm@12396: assume "finite B" wenzelm@12396: thus "!!A. A \ B ==> finite A" wenzelm@12396: proof induct wenzelm@12396: case empty wenzelm@12396: thus ?case by simp wenzelm@12396: next nipkow@15327: case (insert x F A) wenzelm@12396: have A: "A \ insert x F" and r: "A - {x} \ F ==> finite (A - {x})" . wenzelm@12396: show "finite A" wenzelm@12396: proof cases wenzelm@12396: assume x: "x \ A" wenzelm@12396: with A have "A - {x} \ F" by (simp add: subset_insert_iff) wenzelm@12396: with r have "finite (A - {x})" . wenzelm@12396: hence "finite (insert x (A - {x}))" .. wenzelm@12396: also have "insert x (A - {x}) = A" by (rule insert_Diff) wenzelm@12396: finally show ?thesis . wenzelm@12396: next wenzelm@12396: show "A \ F ==> ?thesis" . wenzelm@12396: assume "x \ A" wenzelm@12396: with A show "A \ F" by (simp add: subset_insert_iff) wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: wenzelm@12396: lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)" wenzelm@12396: by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI) wenzelm@12396: wenzelm@12396: lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)" wenzelm@12396: -- {* The converse obviously fails. *} wenzelm@12396: by (blast intro: finite_subset) wenzelm@12396: wenzelm@12396: lemma finite_insert [simp]: "finite (insert a A) = finite A" wenzelm@12396: apply (subst insert_is_Un) paulson@14208: apply (simp only: finite_Un, blast) wenzelm@12396: done wenzelm@12396: nipkow@15281: lemma finite_Union[simp, intro]: nipkow@15281: "\ finite A; !!M. M \ A \ finite M \ \ finite(\A)" nipkow@15281: by (induct rule:finite_induct) simp_all nipkow@15281: wenzelm@12396: lemma finite_empty_induct: wenzelm@12396: "finite A ==> wenzelm@12396: P A ==> (!!a A. finite A ==> a:A ==> P A ==> P (A - {a})) ==> P {}" wenzelm@12396: proof - wenzelm@12396: assume "finite A" wenzelm@12396: and "P A" and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})" wenzelm@12396: have "P (A - A)" wenzelm@12396: proof - wenzelm@12396: fix c b :: "'a set" wenzelm@12396: presume c: "finite c" and b: "finite b" wenzelm@12396: and P1: "P b" and P2: "!!x y. finite y ==> x \ y ==> P y ==> P (y - {x})" wenzelm@12396: from c show "c \ b ==> P (b - c)" wenzelm@12396: proof induct wenzelm@12396: case empty wenzelm@12396: from P1 show ?case by simp wenzelm@12396: next nipkow@15327: case (insert x F) wenzelm@12396: have "P (b - F - {x})" wenzelm@12396: proof (rule P2) wenzelm@12396: from _ b show "finite (b - F)" by (rule finite_subset) blast wenzelm@12396: from insert show "x \ b - F" by simp wenzelm@12396: from insert show "P (b - F)" by simp wenzelm@12396: qed wenzelm@12396: also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric]) wenzelm@12396: finally show ?case . wenzelm@12396: qed wenzelm@12396: next wenzelm@12396: show "A \ A" .. wenzelm@12396: qed wenzelm@12396: thus "P {}" by simp wenzelm@12396: qed wenzelm@12396: wenzelm@12396: lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)" wenzelm@12396: by (rule Diff_subset [THEN finite_subset]) wenzelm@12396: wenzelm@12396: lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)" wenzelm@12396: apply (subst Diff_insert) wenzelm@12396: apply (case_tac "a : A - B") wenzelm@12396: apply (rule finite_insert [symmetric, THEN trans]) paulson@14208: apply (subst insert_Diff, simp_all) wenzelm@12396: done wenzelm@12396: wenzelm@12396: nipkow@15392: text {* Image and Inverse Image over Finite Sets *} paulson@13825: paulson@13825: lemma finite_imageI[simp]: "finite F ==> finite (h ` F)" paulson@13825: -- {* The image of a finite set is finite. *} paulson@13825: by (induct set: Finites) simp_all paulson@13825: paulson@14430: lemma finite_surj: "finite A ==> B <= f ` A ==> finite B" paulson@14430: apply (frule finite_imageI) paulson@14430: apply (erule finite_subset, assumption) paulson@14430: done paulson@14430: paulson@13825: lemma finite_range_imageI: paulson@13825: "finite (range g) ==> finite (range (%x. f (g x)))" paulson@14208: apply (drule finite_imageI, simp) paulson@13825: done paulson@13825: wenzelm@12396: lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A" wenzelm@12396: proof - wenzelm@12396: have aux: "!!A. finite (A - {}) = finite A" by simp wenzelm@12396: fix B :: "'a set" wenzelm@12396: assume "finite B" wenzelm@12396: thus "!!A. f`A = B ==> inj_on f A ==> finite A" wenzelm@12396: apply induct wenzelm@12396: apply simp wenzelm@12396: apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})") wenzelm@12396: apply clarify wenzelm@12396: apply (simp (no_asm_use) add: inj_on_def) paulson@14208: apply (blast dest!: aux [THEN iffD1], atomize) wenzelm@12396: apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl) paulson@14208: apply (frule subsetD [OF equalityD2 insertI1], clarify) wenzelm@12396: apply (rule_tac x = xa in bexI) wenzelm@12396: apply (simp_all add: inj_on_image_set_diff) wenzelm@12396: done wenzelm@12396: qed (rule refl) wenzelm@12396: wenzelm@12396: paulson@13825: lemma inj_vimage_singleton: "inj f ==> f-`{a} \ {THE x. f x = a}" paulson@13825: -- {* The inverse image of a singleton under an injective function paulson@13825: is included in a singleton. *} paulson@14430: apply (auto simp add: inj_on_def) paulson@14430: apply (blast intro: the_equality [symmetric]) paulson@13825: done paulson@13825: paulson@13825: lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)" paulson@13825: -- {* The inverse image of a finite set under an injective function paulson@13825: is finite. *} paulson@14430: apply (induct set: Finites, simp_all) paulson@14430: apply (subst vimage_insert) paulson@14430: apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton]) paulson@13825: done paulson@13825: paulson@13825: nipkow@15392: text {* The finite UNION of finite sets *} wenzelm@12396: wenzelm@12396: lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)" wenzelm@12396: by (induct set: Finites) simp_all wenzelm@12396: wenzelm@12396: text {* wenzelm@12396: Strengthen RHS to paulson@14430: @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \ {}})"}? wenzelm@12396: wenzelm@12396: We'd need to prove paulson@14430: @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \ {}}"} wenzelm@12396: by induction. *} wenzelm@12396: wenzelm@12396: lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))" wenzelm@12396: by (blast intro: finite_UN_I finite_subset) wenzelm@12396: wenzelm@12396: nipkow@15392: text {* Sigma of finite sets *} wenzelm@12396: wenzelm@12396: lemma finite_SigmaI [simp]: wenzelm@12396: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)" wenzelm@12396: by (unfold Sigma_def) (blast intro!: finite_UN_I) wenzelm@12396: nipkow@15402: lemma finite_cartesian_product: "[| finite A; finite B |] ==> nipkow@15402: finite (A <*> B)" nipkow@15402: by (rule finite_SigmaI) nipkow@15402: wenzelm@12396: lemma finite_Prod_UNIV: wenzelm@12396: "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)" wenzelm@12396: apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)") wenzelm@12396: apply (erule ssubst) paulson@14208: apply (erule finite_SigmaI, auto) wenzelm@12396: done wenzelm@12396: paulson@15409: lemma finite_cartesian_productD1: paulson@15409: "[| finite (A <*> B); B \ {} |] ==> finite A" paulson@15409: apply (auto simp add: finite_conv_nat_seg_image) paulson@15409: apply (drule_tac x=n in spec) paulson@15409: apply (drule_tac x="fst o f" in spec) paulson@15409: apply (auto simp add: o_def) paulson@15409: prefer 2 apply (force dest!: equalityD2) paulson@15409: apply (drule equalityD1) paulson@15409: apply (rename_tac y x) paulson@15409: apply (subgoal_tac "\k. k B); A \ {} |] ==> finite B" paulson@15409: apply (auto simp add: finite_conv_nat_seg_image) paulson@15409: apply (drule_tac x=n in spec) paulson@15409: apply (drule_tac x="snd o f" in spec) paulson@15409: apply (auto simp add: o_def) paulson@15409: prefer 2 apply (force dest!: equalityD2) paulson@15409: apply (drule equalityD1) paulson@15409: apply (rename_tac x y) paulson@15409: apply (subgoal_tac "\k. k 'b) set)" wenzelm@12396: proof (rule finite_Prod_UNIV) wenzelm@12396: show "finite (UNIV :: 'a set)" by (rule finite) wenzelm@12396: show "finite (UNIV :: 'b set)" by (rule finite) wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: wenzelm@12396: nipkow@15392: text {* The powerset of a finite set *} wenzelm@12396: wenzelm@12396: lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A" wenzelm@12396: proof wenzelm@12396: assume "finite (Pow A)" wenzelm@12396: with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast wenzelm@12396: thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp wenzelm@12396: next wenzelm@12396: assume "finite A" wenzelm@12396: thus "finite (Pow A)" wenzelm@12396: by induct (simp_all add: finite_UnI finite_imageI Pow_insert) wenzelm@12396: qed wenzelm@12396: nipkow@15392: nipkow@15392: lemma finite_UnionD: "finite(\A) \ finite A" nipkow@15392: by(blast intro: finite_subset[OF subset_Pow_Union]) nipkow@15392: nipkow@15392: wenzelm@12396: lemma finite_converse [iff]: "finite (r^-1) = finite r" wenzelm@12396: apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r") wenzelm@12396: apply simp wenzelm@12396: apply (rule iffI) wenzelm@12396: apply (erule finite_imageD [unfolded inj_on_def]) wenzelm@12396: apply (simp split add: split_split) wenzelm@12396: apply (erule finite_imageI) paulson@14208: apply (simp add: converse_def image_def, auto) wenzelm@12396: apply (rule bexI) wenzelm@12396: prefer 2 apply assumption wenzelm@12396: apply simp wenzelm@12396: done wenzelm@12396: paulson@14430: nipkow@15392: text {* \paragraph{Finiteness of transitive closure} (Thanks to Sidi nipkow@15392: Ehmety) *} wenzelm@12396: wenzelm@12396: lemma finite_Field: "finite r ==> finite (Field r)" wenzelm@12396: -- {* A finite relation has a finite field (@{text "= domain \ range"}. *} wenzelm@12396: apply (induct set: Finites) wenzelm@12396: apply (auto simp add: Field_def Domain_insert Range_insert) wenzelm@12396: done wenzelm@12396: wenzelm@12396: lemma trancl_subset_Field2: "r^+ <= Field r \ Field r" wenzelm@12396: apply clarify wenzelm@12396: apply (erule trancl_induct) wenzelm@12396: apply (auto simp add: Field_def) wenzelm@12396: done wenzelm@12396: wenzelm@12396: lemma finite_trancl: "finite (r^+) = finite r" wenzelm@12396: apply auto wenzelm@12396: prefer 2 wenzelm@12396: apply (rule trancl_subset_Field2 [THEN finite_subset]) wenzelm@12396: apply (rule finite_SigmaI) wenzelm@12396: prefer 3 berghofe@13704: apply (blast intro: r_into_trancl' finite_subset) wenzelm@12396: apply (auto simp add: finite_Field) wenzelm@12396: done wenzelm@12396: wenzelm@12396: nipkow@15392: subsection {* A fold functional for finite sets *} nipkow@15392: nipkow@15392: text {* The intended behaviour is nipkow@15480: @{text "fold f g z {x\<^isub>1, ..., x\<^isub>n} = f (g x\<^isub>1) (\ (f (g x\<^isub>n) z)\)"} nipkow@15392: if @{text f} is associative-commutative. For an application of @{text fold} nipkow@15392: se the definitions of sums and products over finite sets. nipkow@15392: *} nipkow@15392: nipkow@15392: consts nipkow@15392: foldSet :: "('a => 'a => 'a) => ('b => 'a) => 'a => ('b set \ 'a) set" nipkow@15392: nipkow@15480: inductive "foldSet f g z" nipkow@15392: intros nipkow@15480: emptyI [intro]: "({}, z) : foldSet f g z" paulson@15506: insertI [intro]: paulson@15506: "\ x \ A; (A, y) : foldSet f g z \ paulson@15506: \ (insert x A, f (g x) y) : foldSet f g z" nipkow@15392: nipkow@15480: inductive_cases empty_foldSetE [elim!]: "({}, x) : foldSet f g z" nipkow@15392: nipkow@15392: constdefs nipkow@15392: fold :: "('a => 'a => 'a) => ('b => 'a) => 'a => 'b set => 'a" nipkow@15480: "fold f g z A == THE x. (A, x) : foldSet f g z" nipkow@15392: paulson@15498: text{*A tempting alternative for the definiens is paulson@15498: @{term "if finite A then THE x. (A, x) : foldSet f g e else e"}. paulson@15498: It allows the removal of finiteness assumptions from the theorems paulson@15498: @{text fold_commute}, @{text fold_reindex} and @{text fold_distrib}. paulson@15498: The proofs become ugly, with @{text rule_format}. It is not worth the effort.*} paulson@15498: paulson@15498: nipkow@15392: lemma Diff1_foldSet: nipkow@15480: "(A - {x}, y) : foldSet f g z ==> x: A ==> (A, f (g x) y) : foldSet f g z" nipkow@15392: by (erule insert_Diff [THEN subst], rule foldSet.intros, auto) nipkow@15392: nipkow@15480: lemma foldSet_imp_finite: "(A, x) : foldSet f g z ==> finite A" nipkow@15392: by (induct set: foldSet) auto nipkow@15392: nipkow@15480: lemma finite_imp_foldSet: "finite A ==> EX x. (A, x) : foldSet f g z" nipkow@15392: by (induct set: Finites) auto nipkow@15392: nipkow@15392: nipkow@15392: subsubsection {* Commutative monoids *} nipkow@15480: nipkow@15392: locale ACf = nipkow@15392: fixes f :: "'a => 'a => 'a" (infixl "\" 70) nipkow@15392: assumes commute: "x \ y = y \ x" nipkow@15392: and assoc: "(x \ y) \ z = x \ (y \ z)" nipkow@15392: nipkow@15392: locale ACe = ACf + nipkow@15392: fixes e :: 'a nipkow@15392: assumes ident [simp]: "x \ e = x" nipkow@15392: nipkow@15480: locale ACIf = ACf + nipkow@15480: assumes idem: "x \ x = x" nipkow@15480: nipkow@15392: lemma (in ACf) left_commute: "x \ (y \ z) = y \ (x \ z)" nipkow@15392: proof - nipkow@15392: have "x \ (y \ z) = (y \ z) \ x" by (simp only: commute) nipkow@15392: also have "... = y \ (z \ x)" by (simp only: assoc) nipkow@15392: also have "z \ x = x \ z" by (simp only: commute) nipkow@15392: finally show ?thesis . nipkow@15392: qed nipkow@15392: nipkow@15392: lemmas (in ACf) AC = assoc commute left_commute nipkow@15392: nipkow@15392: lemma (in ACe) left_ident [simp]: "e \ x = x" nipkow@15392: proof - nipkow@15392: have "x \ e = x" by (rule ident) nipkow@15392: thus ?thesis by (subst commute) nipkow@15392: qed nipkow@15392: nipkow@15497: lemma (in ACIf) idem2: "x \ (x \ y) = x \ y" nipkow@15497: proof - nipkow@15497: have "x \ (x \ y) = (x \ x) \ y" by(simp add:assoc) nipkow@15497: also have "\ = x \ y" by(simp add:idem) nipkow@15497: finally show ?thesis . nipkow@15497: qed nipkow@15497: nipkow@15497: lemmas (in ACIf) ACI = AC idem idem2 nipkow@15497: nipkow@15402: text{* Instantiation of locales: *} nipkow@15402: nipkow@15402: lemma ACf_add: "ACf (op + :: 'a::comm_monoid_add \ 'a \ 'a)" nipkow@15402: by(fastsimp intro: ACf.intro add_assoc add_commute) nipkow@15402: nipkow@15402: lemma ACe_add: "ACe (op +) (0::'a::comm_monoid_add)" nipkow@15402: by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_add) nipkow@15402: nipkow@15402: nipkow@15402: lemma ACf_mult: "ACf (op * :: 'a::comm_monoid_mult \ 'a \ 'a)" nipkow@15402: by(fast intro: ACf.intro mult_assoc ab_semigroup_mult.mult_commute) nipkow@15402: nipkow@15402: lemma ACe_mult: "ACe (op *) (1::'a::comm_monoid_mult)" nipkow@15402: by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_mult) nipkow@15402: nipkow@15402: nipkow@15392: subsubsection{*From @{term foldSet} to @{term fold}*} nipkow@15392: paulson@15510: lemma image_less_Suc: "h ` {i. i < Suc m} = insert (h m) (h ` {i. i < m})" paulson@15510: by (auto simp add: less_Suc_eq) paulson@15510: paulson@15510: lemma insert_image_inj_on_eq: paulson@15510: "[|insert (h m) A = h ` {i. i < Suc m}; h m \ A; paulson@15510: inj_on h {i. i < Suc m}|] paulson@15510: ==> A = h ` {i. i < m}" paulson@15510: apply (auto simp add: image_less_Suc inj_on_def) paulson@15510: apply (blast intro: less_trans) paulson@15510: done paulson@15510: paulson@15510: lemma insert_inj_onE: paulson@15510: assumes aA: "insert a A = h`{i::nat. i A" paulson@15510: and inj_on: "inj_on h {i::nat. ihm m. inj_on hm {i::nat. i A" by (simp add: swap_def hkeq anot) paulson@15520: show "insert (?hm m) A = ?hm ` {i. i < Suc m}" paulson@15520: using aA hkeq nSuc klessn paulson@15520: by (auto simp add: swap_def image_less_Suc fun_upd_image paulson@15520: less_Suc_eq inj_on_image_set_diff [OF inj_on]) nipkow@15479: qed nipkow@15479: qed nipkow@15479: qed nipkow@15479: nipkow@15392: lemma (in ACf) foldSet_determ_aux: paulson@15510: "!!A x x' h. \ A = h`{i::nat. i nipkow@15392: \ x' = x" paulson@15510: proof (induct n rule: less_induct) paulson@15510: case (less n) paulson@15510: have IH: "!!m h A x x'. paulson@15510: \m foldSet f g z; (A, x') \ foldSet f g z\ \ x' = x" . paulson@15510: have Afoldx: "(A,x) \ foldSet f g z" and Afoldx': "(A,x') \ foldSet f g z" paulson@15510: and A: "A = h`{i. i u)" and notinB: "b \ B" paulson@15510: and Bu: "(B,u) \ foldSet f g z" paulson@15510: hence AbB: "A = insert b B" and x: "x = g b \ u" by auto paulson@15510: show "x'=x" paulson@15510: proof (rule foldSet.cases [OF Afoldx']) paulson@15510: assume "(A, x') = ({}, z)" paulson@15510: with AbB show "x' = x" by blast nipkow@15392: next paulson@15510: fix C c v paulson@15510: assume "(A,x') = (insert c C, g c \ v)" and notinC: "c \ C" paulson@15510: and Cv: "(C,v) \ foldSet f g z" paulson@15510: hence AcC: "A = insert c C" and x': "x' = g c \ v" by auto paulson@15510: from A AbB have Beq: "insert b B = h`{i. i c" nipkow@15392: let ?D = "B - {c}" nipkow@15392: have B: "B = insert c ?D" and C: "C = insert b ?D" paulson@15510: using AbB AcC notinB notinC diff by(blast elim!:equalityE)+ nipkow@15402: have "finite A" by(rule foldSet_imp_finite[OF Afoldx]) paulson@15510: with AbB have "finite ?D" by simp nipkow@15480: then obtain d where Dfoldd: "(?D,d) \ foldSet f g z" nipkow@15392: using finite_imp_foldSet by rules paulson@15506: moreover have cinB: "c \ B" using B by auto nipkow@15480: ultimately have "(B,g c \ d) \ foldSet f g z" nipkow@15392: by(rule Diff1_foldSet) paulson@15510: hence "g c \ d = u" by (rule IH [OF lessB Beq inj_onB Bu]) paulson@15510: moreover have "g b \ d = v" paulson@15510: proof (rule IH[OF lessC Ceq inj_onC Cv]) paulson@15510: show "(C, g b \ d) \ foldSet f g z" using C notinB Dfoldd nipkow@15392: by fastsimp nipkow@15392: qed paulson@15510: ultimately show ?thesis using x x' by (auto simp: AC) nipkow@15392: qed nipkow@15392: qed nipkow@15392: qed nipkow@15392: qed nipkow@15392: nipkow@15392: nipkow@15392: lemma (in ACf) foldSet_determ: paulson@15510: "(A,x) : foldSet f g z ==> (A,y) : foldSet f g z ==> y = x" paulson@15510: apply (frule foldSet_imp_finite [THEN finite_imp_nat_seg_image_inj_on]) paulson@15510: apply (blast intro: foldSet_determ_aux [rule_format]) nipkow@15392: done nipkow@15392: nipkow@15480: lemma (in ACf) fold_equality: "(A, y) : foldSet f g z ==> fold f g z A = y" nipkow@15392: by (unfold fold_def) (blast intro: foldSet_determ) nipkow@15392: nipkow@15392: text{* The base case for @{text fold}: *} nipkow@15392: nipkow@15480: lemma fold_empty [simp]: "fold f g z {} = z" nipkow@15392: by (unfold fold_def) blast nipkow@15392: nipkow@15392: lemma (in ACf) fold_insert_aux: "x \ A ==> nipkow@15480: ((insert x A, v) : foldSet f g z) = nipkow@15480: (EX y. (A, y) : foldSet f g z & v = f (g x) y)" nipkow@15392: apply auto nipkow@15392: apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE]) nipkow@15392: apply (fastsimp dest: foldSet_imp_finite) nipkow@15392: apply (blast intro: foldSet_determ) nipkow@15392: done nipkow@15392: nipkow@15392: text{* The recursion equation for @{text fold}: *} nipkow@15392: nipkow@15392: lemma (in ACf) fold_insert[simp]: nipkow@15480: "finite A ==> x \ A ==> fold f g z (insert x A) = f (g x) (fold f g z A)" nipkow@15392: apply (unfold fold_def) nipkow@15392: apply (simp add: fold_insert_aux) nipkow@15392: apply (rule the_equality) nipkow@15392: apply (auto intro: finite_imp_foldSet nipkow@15392: cong add: conj_cong simp add: fold_def [symmetric] fold_equality) nipkow@15392: done nipkow@15392: nipkow@15535: lemma (in ACf) fold_rec: nipkow@15535: assumes fin: "finite A" and a: "a:A" nipkow@15535: shows "fold f g z A = f (g a) (fold f g z (A - {a}))" nipkow@15535: proof- nipkow@15535: have A: "A = insert a (A - {a})" using a by blast nipkow@15535: hence "fold f g z A = fold f g z (insert a (A - {a}))" by simp nipkow@15535: also have "\ = f (g a) (fold f g z (A - {a}))" nipkow@15535: by(rule fold_insert) (simp add:fin)+ nipkow@15535: finally show ?thesis . nipkow@15535: qed nipkow@15535: nipkow@15392: nipkow@15480: text{* A simplified version for idempotent functions: *} nipkow@15480: paulson@15509: lemma (in ACIf) fold_insert_idem: nipkow@15480: assumes finA: "finite A" paulson@15508: shows "fold f g z (insert a A) = g a \ fold f g z A" nipkow@15480: proof cases nipkow@15480: assume "a \ A" nipkow@15480: then obtain B where A: "A = insert a B" and disj: "a \ B" nipkow@15480: by(blast dest: mk_disjoint_insert) nipkow@15480: show ?thesis nipkow@15480: proof - nipkow@15480: from finA A have finB: "finite B" by(blast intro: finite_subset) nipkow@15480: have "fold f g z (insert a A) = fold f g z (insert a B)" using A by simp nipkow@15480: also have "\ = (g a) \ (fold f g z B)" paulson@15506: using finB disj by simp nipkow@15480: also have "\ = g a \ fold f g z A" nipkow@15480: using A finB disj by(simp add:idem assoc[symmetric]) nipkow@15480: finally show ?thesis . nipkow@15480: qed nipkow@15480: next nipkow@15480: assume "a \ A" nipkow@15480: with finA show ?thesis by simp nipkow@15480: qed nipkow@15480: nipkow@15484: lemma (in ACIf) foldI_conv_id: nipkow@15484: "finite A \ fold f g z A = fold f id z (g ` A)" paulson@15509: by(erule finite_induct)(simp_all add: fold_insert_idem del: fold_insert) nipkow@15484: nipkow@15392: subsubsection{*Lemmas about @{text fold}*} nipkow@15392: nipkow@15392: lemma (in ACf) fold_commute: paulson@15487: "finite A ==> (!!z. f x (fold f g z A) = fold f g (f x z) A)" nipkow@15392: apply (induct set: Finites, simp) paulson@15487: apply (simp add: left_commute [of x]) nipkow@15392: done nipkow@15392: nipkow@15392: lemma (in ACf) fold_nest_Un_Int: nipkow@15392: "finite A ==> finite B nipkow@15480: ==> fold f g (fold f g z B) A = fold f g (fold f g z (A Int B)) (A Un B)" nipkow@15392: apply (induct set: Finites, simp) nipkow@15392: apply (simp add: fold_commute Int_insert_left insert_absorb) nipkow@15392: done nipkow@15392: nipkow@15392: lemma (in ACf) fold_nest_Un_disjoint: nipkow@15392: "finite A ==> finite B ==> A Int B = {} nipkow@15480: ==> fold f g z (A Un B) = fold f g (fold f g z B) A" nipkow@15392: by (simp add: fold_nest_Un_Int) nipkow@15392: nipkow@15392: lemma (in ACf) fold_reindex: paulson@15487: assumes fin: "finite A" paulson@15487: shows "inj_on h A \ fold f g z (h ` A) = fold f (g \ h) z A" paulson@15506: using fin apply induct nipkow@15392: apply simp nipkow@15392: apply simp nipkow@15392: done nipkow@15392: nipkow@15392: lemma (in ACe) fold_Un_Int: nipkow@15392: "finite A ==> finite B ==> nipkow@15392: fold f g e A \ fold f g e B = nipkow@15392: fold f g e (A Un B) \ fold f g e (A Int B)" nipkow@15392: apply (induct set: Finites, simp) nipkow@15392: apply (simp add: AC insert_absorb Int_insert_left) nipkow@15392: done nipkow@15392: nipkow@15392: corollary (in ACe) fold_Un_disjoint: nipkow@15392: "finite A ==> finite B ==> A Int B = {} ==> nipkow@15392: fold f g e (A Un B) = fold f g e A \ fold f g e B" nipkow@15392: by (simp add: fold_Un_Int) nipkow@15392: nipkow@15392: lemma (in ACe) fold_UN_disjoint: nipkow@15392: "\ finite I; ALL i:I. finite (A i); nipkow@15392: ALL i:I. ALL j:I. i \ j --> A i Int A j = {} \ nipkow@15392: \ fold f g e (UNION I A) = nipkow@15392: fold f (%i. fold f g e (A i)) e I" nipkow@15392: apply (induct set: Finites, simp, atomize) nipkow@15392: apply (subgoal_tac "ALL i:F. x \ i") nipkow@15392: prefer 2 apply blast nipkow@15392: apply (subgoal_tac "A x Int UNION F A = {}") nipkow@15392: prefer 2 apply blast nipkow@15392: apply (simp add: fold_Un_disjoint) nipkow@15392: done nipkow@15392: paulson@15506: text{*Fusion theorem, as described in paulson@15506: Graham Hutton's paper, paulson@15506: A Tutorial on the Universality and Expressiveness of Fold, paulson@15506: JFP 9:4 (355-372), 1999.*} paulson@15506: lemma (in ACf) fold_fusion: paulson@15506: includes ACf g paulson@15506: shows paulson@15506: "finite A ==> paulson@15506: (!!x y. h (g x y) = f x (h y)) ==> paulson@15506: h (fold g j w A) = fold f j (h w) A" paulson@15506: by (induct set: Finites, simp_all) paulson@15506: nipkow@15392: lemma (in ACf) fold_cong: nipkow@15480: "finite A \ (!!x. x:A ==> g x = h x) ==> fold f g z A = fold f h z A" nipkow@15480: apply (subgoal_tac "ALL C. C <= A --> (ALL x:C. g x = h x) --> fold f g z C = fold f h z C") nipkow@15392: apply simp nipkow@15392: apply (erule finite_induct, simp) nipkow@15392: apply (simp add: subset_insert_iff, clarify) nipkow@15392: apply (subgoal_tac "finite C") nipkow@15392: prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl]) nipkow@15392: apply (subgoal_tac "C = insert x (C - {x})") nipkow@15392: prefer 2 apply blast nipkow@15392: apply (erule ssubst) nipkow@15392: apply (drule spec) nipkow@15392: apply (erule (1) notE impE) nipkow@15392: apply (simp add: Ball_def del: insert_Diff_single) nipkow@15392: done nipkow@15392: nipkow@15392: lemma (in ACe) fold_Sigma: "finite A ==> ALL x:A. finite (B x) ==> nipkow@15392: fold f (%x. fold f (g x) e (B x)) e A = nipkow@15392: fold f (split g) e (SIGMA x:A. B x)" nipkow@15392: apply (subst Sigma_def) paulson@15506: apply (subst fold_UN_disjoint, assumption, simp) nipkow@15392: apply blast nipkow@15392: apply (erule fold_cong) paulson@15506: apply (subst fold_UN_disjoint, simp, simp) nipkow@15392: apply blast paulson@15506: apply simp nipkow@15392: done nipkow@15392: nipkow@15392: lemma (in ACe) fold_distrib: "finite A \ nipkow@15392: fold f (%x. f (g x) (h x)) e A = f (fold f g e A) (fold f h e A)" paulson@15506: apply (erule finite_induct, simp) nipkow@15392: apply (simp add:AC) nipkow@15392: done nipkow@15392: nipkow@15392: nipkow@15402: subsection {* Generalized summation over a set *} nipkow@15402: nipkow@15402: constdefs nipkow@15402: setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add" nipkow@15402: "setsum f A == if finite A then fold (op +) f 0 A else 0" nipkow@15402: nipkow@15402: text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is nipkow@15402: written @{text"\x\A. e"}. *} nipkow@15402: nipkow@15402: syntax nipkow@15402: "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add" ("(3SUM _:_. _)" [0, 51, 10] 10) nipkow@15402: syntax (xsymbols) nipkow@15402: "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add" ("(3\_\_. _)" [0, 51, 10] 10) nipkow@15402: syntax (HTML output) nipkow@15402: "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add" ("(3\_\_. _)" [0, 51, 10] 10) nipkow@15402: nipkow@15402: translations -- {* Beware of argument permutation! *} nipkow@15402: "SUM i:A. b" == "setsum (%i. b) A" nipkow@15402: "\i\A. b" == "setsum (%i. b) A" nipkow@15402: nipkow@15402: text{* Instead of @{term"\x\{x. P}. e"} we introduce the shorter nipkow@15402: @{text"\x|P. e"}. *} nipkow@15402: nipkow@15402: syntax nipkow@15402: "_qsetsum" :: "idt \ bool \ 'a \ 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10) nipkow@15402: syntax (xsymbols) nipkow@15402: "_qsetsum" :: "idt \ bool \ 'a \ 'a" ("(3\_ | (_)./ _)" [0,0,10] 10) nipkow@15402: syntax (HTML output) nipkow@15402: "_qsetsum" :: "idt \ bool \ 'a \ 'a" ("(3\_ | (_)./ _)" [0,0,10] 10) nipkow@15402: nipkow@15402: translations nipkow@15402: "SUM x|P. t" => "setsum (%x. t) {x. P}" nipkow@15402: "\x|P. t" => "setsum (%x. t) {x. P}" nipkow@15402: nipkow@15402: text{* Finally we abbreviate @{term"\x\A. x"} by @{text"\A"}. *} nipkow@15402: nipkow@15402: syntax nipkow@15402: "_Setsum" :: "'a set => 'a::comm_monoid_mult" ("\_" [1000] 999) nipkow@15402: nipkow@15402: parse_translation {* nipkow@15402: let nipkow@15402: fun Setsum_tr [A] = Syntax.const "setsum" $ Abs ("", dummyT, Bound 0) $ A nipkow@15402: in [("_Setsum", Setsum_tr)] end; nipkow@15402: *} nipkow@15402: nipkow@15402: print_translation {* nipkow@15402: let nipkow@15402: fun setsum_tr' [Abs(_,_,Bound 0), A] = Syntax.const "_Setsum" $ A nipkow@15402: | setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = nipkow@15402: if x<>y then raise Match nipkow@15402: else let val x' = Syntax.mark_bound x nipkow@15402: val t' = subst_bound(x',t) nipkow@15402: val P' = subst_bound(x',P) nipkow@15402: in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end nipkow@15402: in nipkow@15402: [("setsum", setsum_tr')] nipkow@15402: end nipkow@15402: *} nipkow@15402: nipkow@15402: lemma setsum_empty [simp]: "setsum f {} = 0" nipkow@15402: by (simp add: setsum_def) nipkow@15402: nipkow@15402: lemma setsum_insert [simp]: nipkow@15402: "finite F ==> a \ F ==> setsum f (insert a F) = f a + setsum f F" nipkow@15402: by (simp add: setsum_def ACf.fold_insert [OF ACf_add]) nipkow@15402: paulson@15409: lemma setsum_infinite [simp]: "~ finite A ==> setsum f A = 0" paulson@15409: by (simp add: setsum_def) paulson@15409: nipkow@15402: lemma setsum_reindex: nipkow@15402: "inj_on f B ==> setsum h (f ` B) = setsum (h \ f) B" nipkow@15402: by(auto simp add: setsum_def ACf.fold_reindex[OF ACf_add] dest!:finite_imageD) nipkow@15402: nipkow@15402: lemma setsum_reindex_id: nipkow@15402: "inj_on f B ==> setsum f B = setsum id (f ` B)" nipkow@15402: by (auto simp add: setsum_reindex) nipkow@15402: nipkow@15402: lemma setsum_cong: nipkow@15402: "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B" nipkow@15402: by(fastsimp simp: setsum_def intro: ACf.fold_cong[OF ACf_add]) nipkow@15402: nipkow@15402: lemma setsum_reindex_cong: nipkow@15402: "[|inj_on f A; B = f ` A; !!a. g a = h (f a)|] nipkow@15402: ==> setsum h B = setsum g A" nipkow@15402: by (simp add: setsum_reindex cong: setsum_cong) nipkow@15402: nipkow@15402: lemma setsum_0: "setsum (%i. 0) A = 0" nipkow@15402: apply (clarsimp simp: setsum_def) nipkow@15402: apply (erule finite_induct, auto simp:ACf.fold_insert [OF ACf_add]) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0" nipkow@15402: apply (subgoal_tac "setsum f F = setsum (%x. 0) F") nipkow@15402: apply (erule ssubst, rule setsum_0) nipkow@15402: apply (rule setsum_cong, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setsum_Un_Int: "finite A ==> finite B ==> nipkow@15402: setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B" nipkow@15402: -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *} nipkow@15402: by(simp add: setsum_def ACe.fold_Un_Int[OF ACe_add,symmetric]) nipkow@15402: nipkow@15402: lemma setsum_Un_disjoint: "finite A ==> finite B nipkow@15402: ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B" nipkow@15402: by (subst setsum_Un_Int [symmetric], auto) nipkow@15402: paulson@15409: (*But we can't get rid of finite I. If infinite, although the rhs is 0, paulson@15409: the lhs need not be, since UNION I A could still be finite.*) nipkow@15402: lemma setsum_UN_disjoint: nipkow@15402: "finite I ==> (ALL i:I. finite (A i)) ==> nipkow@15402: (ALL i:I. ALL j:I. i \ j --> A i Int A j = {}) ==> nipkow@15402: setsum f (UNION I A) = (\i\I. setsum f (A i))" nipkow@15402: by(simp add: setsum_def ACe.fold_UN_disjoint[OF ACe_add] cong: setsum_cong) nipkow@15402: paulson@15409: text{*No need to assume that @{term C} is finite. If infinite, the rhs is paulson@15409: directly 0, and @{term "Union C"} is also infinite, hence the lhs is also 0.*} nipkow@15402: lemma setsum_Union_disjoint: paulson@15409: "[| (ALL A:C. finite A); paulson@15409: (ALL A:C. ALL B:C. A \ B --> A Int B = {}) |] paulson@15409: ==> setsum f (Union C) = setsum (setsum f) C" paulson@15409: apply (cases "finite C") paulson@15409: prefer 2 apply (force dest: finite_UnionD simp add: setsum_def) nipkow@15402: apply (frule setsum_UN_disjoint [of C id f]) paulson@15409: apply (unfold Union_def id_def, assumption+) paulson@15409: done nipkow@15402: paulson@15409: (*But we can't get rid of finite A. If infinite, although the lhs is 0, paulson@15409: the rhs need not be, since SIGMA A B could still be finite.*) nipkow@15402: lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==> nipkow@15402: (\x\A. (\y\B x. f x y)) = nipkow@15402: (\z\(SIGMA x:A. B x). f (fst z) (snd z))" nipkow@15402: by(simp add:setsum_def ACe.fold_Sigma[OF ACe_add] split_def cong:setsum_cong) nipkow@15402: paulson@15409: text{*Here we can eliminate the finiteness assumptions, by cases.*} paulson@15409: lemma setsum_cartesian_product: paulson@15409: "(\x\A. (\y\B. f x y)) = (\z\A <*> B. f (fst z) (snd z))" paulson@15409: apply (cases "finite A") paulson@15409: apply (cases "finite B") paulson@15409: apply (simp add: setsum_Sigma) paulson@15409: apply (cases "A={}", simp) paulson@15409: apply (simp add: setsum_0) paulson@15409: apply (auto simp add: setsum_def paulson@15409: dest: finite_cartesian_productD1 finite_cartesian_productD2) paulson@15409: done nipkow@15402: nipkow@15402: lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)" nipkow@15402: by(simp add:setsum_def ACe.fold_distrib[OF ACe_add]) nipkow@15402: nipkow@15402: nipkow@15402: subsubsection {* Properties in more restricted classes of structures *} nipkow@15402: nipkow@15402: lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a" nipkow@15402: apply (case_tac "finite A") nipkow@15402: prefer 2 apply (simp add: setsum_def) nipkow@15402: apply (erule rev_mp) nipkow@15402: apply (erule finite_induct, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setsum_eq_0_iff [simp]: nipkow@15402: "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))" nipkow@15402: by (induct set: Finites) auto nipkow@15402: nipkow@15402: lemma setsum_Un_nat: "finite A ==> finite B ==> nipkow@15402: (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)" nipkow@15402: -- {* For the natural numbers, we have subtraction. *} nipkow@15402: by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps) nipkow@15402: nipkow@15402: lemma setsum_Un: "finite A ==> finite B ==> nipkow@15402: (setsum f (A Un B) :: 'a :: ab_group_add) = nipkow@15402: setsum f A + setsum f B - setsum f (A Int B)" nipkow@15402: by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps) nipkow@15402: nipkow@15402: lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) = nipkow@15402: (if a:A then setsum f A - f a else setsum f A)" nipkow@15402: apply (case_tac "finite A") nipkow@15402: prefer 2 apply (simp add: setsum_def) nipkow@15402: apply (erule finite_induct) nipkow@15402: apply (auto simp add: insert_Diff_if) nipkow@15402: apply (drule_tac a = a in mk_disjoint_insert, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setsum_diff1: "finite A \ nipkow@15402: (setsum f (A - {a}) :: ('a::ab_group_add)) = nipkow@15402: (if a:A then setsum f A - f a else setsum f A)" nipkow@15402: by (erule finite_induct) (auto simp add: insert_Diff_if) nipkow@15402: nipkow@15402: (* By Jeremy Siek: *) nipkow@15402: nipkow@15402: lemma setsum_diff_nat: nipkow@15402: assumes finB: "finite B" nipkow@15402: shows "B \ A \ (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)" nipkow@15402: using finB nipkow@15402: proof (induct) nipkow@15402: show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp nipkow@15402: next nipkow@15402: fix F x assume finF: "finite F" and xnotinF: "x \ F" nipkow@15402: and xFinA: "insert x F \ A" nipkow@15402: and IH: "F \ A \ setsum f (A - F) = setsum f A - setsum f F" nipkow@15402: from xnotinF xFinA have xinAF: "x \ (A - F)" by simp nipkow@15402: from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x" nipkow@15402: by (simp add: setsum_diff1_nat) nipkow@15402: from xFinA have "F \ A" by simp nipkow@15402: with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp nipkow@15402: with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x" nipkow@15402: by simp nipkow@15402: from xnotinF have "A - insert x F = (A - F) - {x}" by auto nipkow@15402: with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x" nipkow@15402: by simp nipkow@15402: from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp nipkow@15402: with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" nipkow@15402: by simp nipkow@15402: thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp nipkow@15402: qed nipkow@15402: nipkow@15402: lemma setsum_diff: nipkow@15402: assumes le: "finite A" "B \ A" nipkow@15402: shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))" nipkow@15402: proof - nipkow@15402: from le have finiteB: "finite B" using finite_subset by auto nipkow@15402: show ?thesis using finiteB le nipkow@15402: proof (induct) nipkow@15402: case empty nipkow@15402: thus ?case by auto nipkow@15402: next nipkow@15402: case (insert x F) nipkow@15402: thus ?case using le finiteB nipkow@15402: by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb) nipkow@15402: qed nipkow@15402: qed nipkow@15402: nipkow@15402: lemma setsum_mono: nipkow@15402: assumes le: "\i. i\K \ f (i::'a) \ ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))" nipkow@15402: shows "(\i\K. f i) \ (\i\K. g i)" nipkow@15402: proof (cases "finite K") nipkow@15402: case True nipkow@15402: thus ?thesis using le nipkow@15402: proof (induct) nipkow@15402: case empty nipkow@15402: thus ?case by simp nipkow@15402: next nipkow@15402: case insert nipkow@15402: thus ?case using add_mono nipkow@15402: by force nipkow@15402: qed nipkow@15402: next nipkow@15402: case False nipkow@15402: thus ?thesis nipkow@15402: by (simp add: setsum_def) nipkow@15402: qed nipkow@15402: nipkow@15402: lemma setsum_mono2_nat: nipkow@15402: assumes fin: "finite B" and sub: "A \ B" nipkow@15402: shows "setsum f A \ (setsum f B :: nat)" nipkow@15402: proof - nipkow@15402: have "setsum f A \ setsum f A + setsum f (B-A)" by arith nipkow@15402: also have "\ = setsum f (A \ (B-A))" using fin finite_subset[OF sub fin] nipkow@15402: by (simp add:setsum_Un_disjoint del:Un_Diff_cancel) nipkow@15402: also have "A \ (B-A) = B" using sub by blast nipkow@15402: finally show ?thesis . nipkow@15402: qed nipkow@15402: nipkow@15535: lemma setsum_negf: nipkow@15535: "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A" nipkow@15535: proof (cases "finite A") nipkow@15535: case True thus ?thesis by (induct set: Finites, auto) nipkow@15535: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15535: qed nipkow@15402: nipkow@15535: lemma setsum_subtractf: nipkow@15535: "setsum (%x. ((f x)::'a::ab_group_add) - g x) A = nipkow@15402: setsum f A - setsum g A" nipkow@15535: proof (cases "finite A") nipkow@15535: case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf) nipkow@15535: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15535: qed nipkow@15402: nipkow@15535: lemma setsum_nonneg: nipkow@15535: assumes nn: "\x\A. (0::'a::{pordered_ab_semigroup_add,comm_monoid_add}) \ f x" nipkow@15535: shows "0 \ setsum f A" nipkow@15535: proof (cases "finite A") nipkow@15535: case True thus ?thesis using nn nipkow@15402: apply (induct set: Finites, auto) nipkow@15402: apply (subgoal_tac "0 + 0 \ f x + setsum f F", simp) nipkow@15402: apply (blast intro: add_mono) nipkow@15402: done nipkow@15535: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15535: qed nipkow@15402: nipkow@15535: lemma setsum_nonpos: nipkow@15535: assumes np: "\x\A. f x \ (0::'a::{pordered_ab_semigroup_add,comm_monoid_add})" nipkow@15535: shows "setsum f A \ 0" nipkow@15535: proof (cases "finite A") nipkow@15535: case True thus ?thesis using np nipkow@15402: apply (induct set: Finites, auto) nipkow@15402: apply (subgoal_tac "f x + setsum f F \ 0 + 0", simp) nipkow@15402: apply (blast intro: add_mono) nipkow@15402: done nipkow@15535: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15535: qed nipkow@15402: nipkow@15402: lemma setsum_mult: nipkow@15402: fixes f :: "'a => ('b::semiring_0_cancel)" nipkow@15402: shows "r * setsum f A = setsum (%n. r * f n) A" nipkow@15402: proof (cases "finite A") nipkow@15402: case True nipkow@15402: thus ?thesis nipkow@15402: proof (induct) nipkow@15402: case empty thus ?case by simp nipkow@15402: next nipkow@15402: case (insert x A) thus ?case by (simp add: right_distrib) nipkow@15402: qed nipkow@15402: next nipkow@15402: case False thus ?thesis by (simp add: setsum_def) nipkow@15402: qed nipkow@15402: nipkow@15535: lemma setsum_abs[iff]: nipkow@15402: fixes f :: "'a => ('b::lordered_ab_group_abs)" nipkow@15402: shows "abs (setsum f A) \ setsum (%i. abs(f i)) A" nipkow@15535: proof (cases "finite A") nipkow@15535: case True nipkow@15535: thus ?thesis nipkow@15535: proof (induct) nipkow@15535: case empty thus ?case by simp nipkow@15535: next nipkow@15535: case (insert x A) nipkow@15535: thus ?case by (auto intro: abs_triangle_ineq order_trans) nipkow@15535: qed nipkow@15402: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15402: qed nipkow@15402: nipkow@15535: lemma setsum_abs_ge_zero[iff]: nipkow@15402: fixes f :: "'a => ('b::lordered_ab_group_abs)" nipkow@15402: shows "0 \ setsum (%i. abs(f i)) A" nipkow@15535: proof (cases "finite A") nipkow@15535: case True nipkow@15535: thus ?thesis nipkow@15535: proof (induct) nipkow@15535: case empty thus ?case by simp nipkow@15535: next nipkow@15535: case (insert x A) thus ?case by (auto intro: order_trans) nipkow@15535: qed nipkow@15402: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15402: qed nipkow@15402: nipkow@15402: nipkow@15402: subsection {* Generalized product over a set *} nipkow@15402: nipkow@15402: constdefs nipkow@15402: setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult" nipkow@15402: "setprod f A == if finite A then fold (op *) f 1 A else 1" nipkow@15402: nipkow@15402: syntax nipkow@15402: "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult" ("(3\_:_. _)" [0, 51, 10] 10) nipkow@15402: nipkow@15402: syntax (xsymbols) nipkow@15402: "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult" ("(3\_\_. _)" [0, 51, 10] 10) nipkow@15402: syntax (HTML output) nipkow@15402: "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult" ("(3\_\_. _)" [0, 51, 10] 10) nipkow@15402: translations nipkow@15402: "\i:A. b" == "setprod (%i. b) A" -- {* Beware of argument permutation! *} nipkow@15402: nipkow@15402: syntax nipkow@15402: "_Setprod" :: "'a set => 'a::comm_monoid_mult" ("\_" [1000] 999) nipkow@15402: nipkow@15402: parse_translation {* nipkow@15402: let nipkow@15402: fun Setprod_tr [A] = Syntax.const "setprod" $ Abs ("", dummyT, Bound 0) $ A nipkow@15402: in [("_Setprod", Setprod_tr)] end; nipkow@15402: *} nipkow@15402: print_translation {* nipkow@15402: let fun setprod_tr' [Abs(x,Tx,t), A] = nipkow@15402: if t = Bound 0 then Syntax.const "_Setprod" $ A else raise Match nipkow@15402: in nipkow@15402: [("setprod", setprod_tr')] nipkow@15402: end nipkow@15402: *} nipkow@15402: nipkow@15402: nipkow@15402: lemma setprod_empty [simp]: "setprod f {} = 1" nipkow@15402: by (auto simp add: setprod_def) nipkow@15402: nipkow@15402: lemma setprod_insert [simp]: "[| finite A; a \ A |] ==> nipkow@15402: setprod f (insert a A) = f a * setprod f A" nipkow@15402: by (simp add: setprod_def ACf.fold_insert [OF ACf_mult]) nipkow@15402: paulson@15409: lemma setprod_infinite [simp]: "~ finite A ==> setprod f A = 1" paulson@15409: by (simp add: setprod_def) paulson@15409: nipkow@15402: lemma setprod_reindex: nipkow@15402: "inj_on f B ==> setprod h (f ` B) = setprod (h \ f) B" nipkow@15402: by(auto simp: setprod_def ACf.fold_reindex[OF ACf_mult] dest!:finite_imageD) nipkow@15402: nipkow@15402: lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)" nipkow@15402: by (auto simp add: setprod_reindex) nipkow@15402: nipkow@15402: lemma setprod_cong: nipkow@15402: "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B" nipkow@15402: by(fastsimp simp: setprod_def intro: ACf.fold_cong[OF ACf_mult]) nipkow@15402: nipkow@15402: lemma setprod_reindex_cong: "inj_on f A ==> nipkow@15402: B = f ` A ==> g = h \ f ==> setprod h B = setprod g A" nipkow@15402: by (frule setprod_reindex, simp) nipkow@15402: nipkow@15402: nipkow@15402: lemma setprod_1: "setprod (%i. 1) A = 1" nipkow@15402: apply (case_tac "finite A") nipkow@15402: apply (erule finite_induct, auto simp add: mult_ac) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1" nipkow@15402: apply (subgoal_tac "setprod f F = setprod (%x. 1) F") nipkow@15402: apply (erule ssubst, rule setprod_1) nipkow@15402: apply (rule setprod_cong, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_Un_Int: "finite A ==> finite B nipkow@15402: ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B" nipkow@15402: by(simp add: setprod_def ACe.fold_Un_Int[OF ACe_mult,symmetric]) nipkow@15402: nipkow@15402: lemma setprod_Un_disjoint: "finite A ==> finite B nipkow@15402: ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B" nipkow@15402: by (subst setprod_Un_Int [symmetric], auto) nipkow@15402: nipkow@15402: lemma setprod_UN_disjoint: nipkow@15402: "finite I ==> (ALL i:I. finite (A i)) ==> nipkow@15402: (ALL i:I. ALL j:I. i \ j --> A i Int A j = {}) ==> nipkow@15402: setprod f (UNION I A) = setprod (%i. setprod f (A i)) I" nipkow@15402: by(simp add: setprod_def ACe.fold_UN_disjoint[OF ACe_mult] cong: setprod_cong) nipkow@15402: nipkow@15402: lemma setprod_Union_disjoint: paulson@15409: "[| (ALL A:C. finite A); paulson@15409: (ALL A:C. ALL B:C. A \ B --> A Int B = {}) |] paulson@15409: ==> setprod f (Union C) = setprod (setprod f) C" paulson@15409: apply (cases "finite C") paulson@15409: prefer 2 apply (force dest: finite_UnionD simp add: setprod_def) nipkow@15402: apply (frule setprod_UN_disjoint [of C id f]) paulson@15409: apply (unfold Union_def id_def, assumption+) paulson@15409: done nipkow@15402: nipkow@15402: lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==> nipkow@15402: (\x:A. (\y: B x. f x y)) = nipkow@15402: (\z:(SIGMA x:A. B x). f (fst z) (snd z))" nipkow@15402: by(simp add:setprod_def ACe.fold_Sigma[OF ACe_mult] split_def cong:setprod_cong) nipkow@15402: paulson@15409: text{*Here we can eliminate the finiteness assumptions, by cases.*} paulson@15409: lemma setprod_cartesian_product: paulson@15409: "(\x:A. (\y: B. f x y)) = (\z:(A <*> B). f (fst z) (snd z))" paulson@15409: apply (cases "finite A") paulson@15409: apply (cases "finite B") paulson@15409: apply (simp add: setprod_Sigma) paulson@15409: apply (cases "A={}", simp) paulson@15409: apply (simp add: setprod_1) paulson@15409: apply (auto simp add: setprod_def paulson@15409: dest: finite_cartesian_productD1 finite_cartesian_productD2) paulson@15409: done nipkow@15402: nipkow@15402: lemma setprod_timesf: paulson@15409: "setprod (%x. f x * g x) A = (setprod f A * setprod g A)" nipkow@15402: by(simp add:setprod_def ACe.fold_distrib[OF ACe_mult]) nipkow@15402: nipkow@15402: nipkow@15402: subsubsection {* Properties in more restricted classes of structures *} nipkow@15402: nipkow@15402: lemma setprod_eq_1_iff [simp]: nipkow@15402: "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))" nipkow@15402: by (induct set: Finites) auto nipkow@15402: nipkow@15402: lemma setprod_zero: nipkow@15402: "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0" nipkow@15402: apply (induct set: Finites, force, clarsimp) nipkow@15402: apply (erule disjE, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_nonneg [rule_format]: nipkow@15402: "(ALL x: A. (0::'a::ordered_idom) \ f x) --> 0 \ setprod f A" nipkow@15402: apply (case_tac "finite A") nipkow@15402: apply (induct set: Finites, force, clarsimp) nipkow@15402: apply (subgoal_tac "0 * 0 \ f x * setprod f F", force) nipkow@15402: apply (rule mult_mono, assumption+) nipkow@15402: apply (auto simp add: setprod_def) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x) nipkow@15402: --> 0 < setprod f A" nipkow@15402: apply (case_tac "finite A") nipkow@15402: apply (induct set: Finites, force, clarsimp) nipkow@15402: apply (subgoal_tac "0 * 0 < f x * setprod f F", force) nipkow@15402: apply (rule mult_strict_mono, assumption+) nipkow@15402: apply (auto simp add: setprod_def) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_nonzero [rule_format]: nipkow@15402: "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==> nipkow@15402: finite A ==> (ALL x: A. f x \ (0::'a)) --> setprod f A \ 0" nipkow@15402: apply (erule finite_induct, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_zero_eq: nipkow@15402: "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==> nipkow@15402: finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)" nipkow@15402: apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_nonzero_field: nipkow@15402: "finite A ==> (ALL x: A. f x \ (0::'a::field)) ==> setprod f A \ 0" nipkow@15402: apply (rule setprod_nonzero, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_zero_eq_field: nipkow@15402: "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)" nipkow@15402: apply (rule setprod_zero_eq, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \ 0) ==> nipkow@15402: (setprod f (A Un B) :: 'a ::{field}) nipkow@15402: = setprod f A * setprod f B / setprod f (A Int B)" nipkow@15402: apply (subst setprod_Un_Int [symmetric], auto) nipkow@15402: apply (subgoal_tac "finite (A Int B)") nipkow@15402: apply (frule setprod_nonzero_field [of "A Int B" f], assumption) nipkow@15402: apply (subst times_divide_eq_right [THEN sym], auto simp add: divide_self) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_diff1: "finite A ==> f a \ 0 ==> nipkow@15402: (setprod f (A - {a}) :: 'a :: {field}) = nipkow@15402: (if a:A then setprod f A / f a else setprod f A)" nipkow@15402: apply (erule finite_induct) nipkow@15402: apply (auto simp add: insert_Diff_if) nipkow@15402: apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a") nipkow@15402: apply (erule ssubst) nipkow@15402: apply (subst times_divide_eq_right [THEN sym]) nipkow@15402: apply (auto simp add: mult_ac times_divide_eq_right divide_self) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_inversef: "finite A ==> nipkow@15402: ALL x: A. f x \ (0::'a::{field,division_by_zero}) ==> nipkow@15402: setprod (inverse \ f) A = inverse (setprod f A)" nipkow@15402: apply (erule finite_induct) nipkow@15402: apply (simp, simp) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_dividef: nipkow@15402: "[|finite A; nipkow@15402: \x \ A. g x \ (0::'a::{field,division_by_zero})|] nipkow@15402: ==> setprod (%x. f x / g x) A = setprod f A / setprod g A" nipkow@15402: apply (subgoal_tac nipkow@15402: "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \ g) x) A") nipkow@15402: apply (erule ssubst) nipkow@15402: apply (subst divide_inverse) nipkow@15402: apply (subst setprod_timesf) nipkow@15402: apply (subst setprod_inversef, assumption+, rule refl) nipkow@15402: apply (rule setprod_cong, rule refl) nipkow@15402: apply (subst divide_inverse, auto) nipkow@15402: done nipkow@15402: wenzelm@12396: subsection {* Finite cardinality *} wenzelm@12396: nipkow@15402: text {* This definition, although traditional, is ugly to work with: nipkow@15402: @{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}. nipkow@15402: But now that we have @{text setsum} things are easy: wenzelm@12396: *} wenzelm@12396: wenzelm@12396: constdefs wenzelm@12396: card :: "'a set => nat" nipkow@15402: "card A == setsum (%x. 1::nat) A" wenzelm@12396: wenzelm@12396: lemma card_empty [simp]: "card {} = 0" nipkow@15402: by (simp add: card_def) nipkow@15402: paulson@15409: lemma card_infinite [simp]: "~ finite A ==> card A = 0" paulson@15409: by (simp add: card_def) paulson@15409: nipkow@15402: lemma card_eq_setsum: "card A = setsum (%x. 1) A" nipkow@15402: by (simp add: card_def) wenzelm@12396: wenzelm@12396: lemma card_insert_disjoint [simp]: wenzelm@12396: "finite A ==> x \ A ==> card (insert x A) = Suc(card A)" nipkow@15402: by(simp add: card_def ACf.fold_insert[OF ACf_add]) nipkow@15402: nipkow@15402: lemma card_insert_if: nipkow@15402: "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))" nipkow@15402: by (simp add: insert_absorb) wenzelm@12396: wenzelm@12396: lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})" wenzelm@12396: apply auto paulson@15506: apply (drule_tac a = x in mk_disjoint_insert, clarify, auto) wenzelm@12396: done wenzelm@12396: paulson@15409: lemma card_eq_0_iff: "(card A = 0) = (A = {} | ~ finite A)" paulson@15409: by auto paulson@15409: wenzelm@12396: lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A" nipkow@14302: apply(rule_tac t = A in insert_Diff [THEN subst], assumption) nipkow@14302: apply(simp del:insert_Diff_single) nipkow@14302: done wenzelm@12396: wenzelm@12396: lemma card_Diff_singleton: wenzelm@12396: "finite A ==> x: A ==> card (A - {x}) = card A - 1" wenzelm@12396: by (simp add: card_Suc_Diff1 [symmetric]) wenzelm@12396: wenzelm@12396: lemma card_Diff_singleton_if: wenzelm@12396: "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)" wenzelm@12396: by (simp add: card_Diff_singleton) wenzelm@12396: wenzelm@12396: lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))" wenzelm@12396: by (simp add: card_insert_if card_Suc_Diff1) wenzelm@12396: wenzelm@12396: lemma card_insert_le: "finite A ==> card A <= card (insert x A)" wenzelm@12396: by (simp add: card_insert_if) wenzelm@12396: nipkow@15402: lemma card_mono: "\ finite B; A \ B \ \ card A \ card B" nipkow@15402: by (simp add: card_def setsum_mono2_nat) nipkow@15402: wenzelm@12396: lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)" paulson@14208: apply (induct set: Finites, simp, clarify) wenzelm@12396: apply (subgoal_tac "finite A & A - {x} <= F") paulson@14208: prefer 2 apply (blast intro: finite_subset, atomize) wenzelm@12396: apply (drule_tac x = "A - {x}" in spec) wenzelm@12396: apply (simp add: card_Diff_singleton_if split add: split_if_asm) paulson@14208: apply (case_tac "card A", auto) wenzelm@12396: done wenzelm@12396: wenzelm@12396: lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B" wenzelm@12396: apply (simp add: psubset_def linorder_not_le [symmetric]) wenzelm@12396: apply (blast dest: card_seteq) wenzelm@12396: done wenzelm@12396: wenzelm@12396: lemma card_Un_Int: "finite A ==> finite B wenzelm@12396: ==> card A + card B = card (A Un B) + card (A Int B)" nipkow@15402: by(simp add:card_def setsum_Un_Int) wenzelm@12396: wenzelm@12396: lemma card_Un_disjoint: "finite A ==> finite B wenzelm@12396: ==> A Int B = {} ==> card (A Un B) = card A + card B" wenzelm@12396: by (simp add: card_Un_Int) wenzelm@12396: wenzelm@12396: lemma card_Diff_subset: nipkow@15402: "finite B ==> B <= A ==> card (A - B) = card A - card B" nipkow@15402: by(simp add:card_def setsum_diff_nat) wenzelm@12396: wenzelm@12396: lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A" wenzelm@12396: apply (rule Suc_less_SucD) wenzelm@12396: apply (simp add: card_Suc_Diff1) wenzelm@12396: done wenzelm@12396: wenzelm@12396: lemma card_Diff2_less: wenzelm@12396: "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A" wenzelm@12396: apply (case_tac "x = y") wenzelm@12396: apply (simp add: card_Diff1_less) wenzelm@12396: apply (rule less_trans) wenzelm@12396: prefer 2 apply (auto intro!: card_Diff1_less) wenzelm@12396: done wenzelm@12396: wenzelm@12396: lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A" wenzelm@12396: apply (case_tac "x : A") wenzelm@12396: apply (simp_all add: card_Diff1_less less_imp_le) wenzelm@12396: done wenzelm@12396: wenzelm@12396: lemma card_psubset: "finite B ==> A \ B ==> card A < card B ==> A < B" paulson@14208: by (erule psubsetI, blast) wenzelm@12396: paulson@14889: lemma insert_partition: nipkow@15402: "\ x \ F; \c1 \ insert x F. \c2 \ insert x F. c1 \ c2 \ c1 \ c2 = {} \ nipkow@15402: \ x \ \ F = {}" paulson@14889: by auto paulson@14889: paulson@14889: (* main cardinality theorem *) paulson@14889: lemma card_partition [rule_format]: paulson@14889: "finite C ==> paulson@14889: finite (\ C) --> paulson@14889: (\c\C. card c = k) --> paulson@14889: (\c1 \ C. \c2 \ C. c1 \ c2 --> c1 \ c2 = {}) --> paulson@14889: k * card(C) = card (\ C)" paulson@14889: apply (erule finite_induct, simp) paulson@14889: apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition paulson@14889: finite_subset [of _ "\ (insert x F)"]) paulson@14889: done paulson@14889: wenzelm@12396: paulson@15409: lemma setsum_constant_nat: "(\x\A. y) = (card A) * y" nipkow@15402: -- {* Generalized to any @{text comm_semiring_1_cancel} in nipkow@15402: @{text IntDef} as @{text setsum_constant}. *} paulson@15409: apply (cases "finite A") paulson@15409: apply (erule finite_induct, auto) paulson@15409: done nipkow@15402: nipkow@15402: lemma setprod_constant: "finite A ==> (\x: A. (y::'a::recpower)) = y^(card A)" nipkow@15402: apply (erule finite_induct) nipkow@15402: apply (auto simp add: power_Suc) nipkow@15402: done nipkow@15402: nipkow@15402: nipkow@15402: subsubsection {* Cardinality of unions *} nipkow@15402: nipkow@15402: lemma card_UN_disjoint: nipkow@15402: "finite I ==> (ALL i:I. finite (A i)) ==> nipkow@15402: (ALL i:I. ALL j:I. i \ j --> A i Int A j = {}) ==> nipkow@15402: card (UNION I A) = (\i\I. card(A i))" nipkow@15402: apply (simp add: card_def) nipkow@15402: apply (subgoal_tac nipkow@15402: "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I") nipkow@15402: apply (simp add: setsum_UN_disjoint) nipkow@15402: apply (simp add: setsum_constant_nat cong: setsum_cong) nipkow@15402: done nipkow@15402: nipkow@15402: lemma card_Union_disjoint: nipkow@15402: "finite C ==> (ALL A:C. finite A) ==> nipkow@15402: (ALL A:C. ALL B:C. A \ B --> A Int B = {}) ==> nipkow@15402: card (Union C) = setsum card C" nipkow@15402: apply (frule card_UN_disjoint [of C id]) nipkow@15402: apply (unfold Union_def id_def, assumption+) nipkow@15402: done nipkow@15402: wenzelm@12396: subsubsection {* Cardinality of image *} wenzelm@12396: paulson@15447: text{*The image of a finite set can be expressed using @{term fold}.*} paulson@15447: lemma image_eq_fold: "finite A ==> f ` A = fold (op Un) (%x. {f x}) {} A" paulson@15447: apply (erule finite_induct, simp) paulson@15447: apply (subst ACf.fold_insert) paulson@15447: apply (auto simp add: ACf_def) paulson@15447: done paulson@15447: wenzelm@12396: lemma card_image_le: "finite A ==> card (f ` A) <= card A" paulson@14208: apply (induct set: Finites, simp) wenzelm@12396: apply (simp add: le_SucI finite_imageI card_insert_if) wenzelm@12396: done wenzelm@12396: nipkow@15402: lemma card_image: "inj_on f A ==> card (f ` A) = card A" nipkow@15402: by(simp add:card_def setsum_reindex o_def) wenzelm@12396: wenzelm@12396: lemma endo_inj_surj: "finite A ==> f ` A \ A ==> inj_on f A ==> f ` A = A" wenzelm@12396: by (simp add: card_seteq card_image) wenzelm@12396: nipkow@15111: lemma eq_card_imp_inj_on: nipkow@15111: "[| finite A; card(f ` A) = card A |] ==> inj_on f A" paulson@15506: apply (induct rule:finite_induct, simp) nipkow@15111: apply(frule card_image_le[where f = f]) nipkow@15111: apply(simp add:card_insert_if split:if_splits) nipkow@15111: done nipkow@15111: nipkow@15111: lemma inj_on_iff_eq_card: nipkow@15111: "finite A ==> inj_on f A = (card(f ` A) = card A)" nipkow@15111: by(blast intro: card_image eq_card_imp_inj_on) nipkow@15111: wenzelm@12396: nipkow@15402: lemma card_inj_on_le: nipkow@15402: "[|inj_on f A; f ` A \ B; finite B |] ==> card A \ card B" nipkow@15402: apply (subgoal_tac "finite A") nipkow@15402: apply (force intro: card_mono simp add: card_image [symmetric]) nipkow@15402: apply (blast intro: finite_imageD dest: finite_subset) nipkow@15402: done nipkow@15402: nipkow@15402: lemma card_bij_eq: nipkow@15402: "[|inj_on f A; f ` A \ B; inj_on g B; g ` B \ A; nipkow@15402: finite A; finite B |] ==> card A = card B" nipkow@15402: by (auto intro: le_anti_sym card_inj_on_le) nipkow@15402: nipkow@15402: nipkow@15402: subsubsection {* Cardinality of products *} nipkow@15402: nipkow@15402: (* nipkow@15402: lemma SigmaI_insert: "y \ A ==> nipkow@15402: (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \ (SIGMA x: A. B x))" nipkow@15402: by auto nipkow@15402: *) nipkow@15402: nipkow@15402: lemma card_SigmaI [simp]: nipkow@15402: "\ finite A; ALL a:A. finite (B a) \ nipkow@15402: \ card (SIGMA x: A. B x) = (\a\A. card (B a))" nipkow@15402: by(simp add:card_def setsum_Sigma) nipkow@15402: paulson@15409: lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)" paulson@15409: apply (cases "finite A") paulson@15409: apply (cases "finite B") paulson@15409: apply (simp add: setsum_constant_nat) paulson@15409: apply (auto simp add: card_eq_0_iff paulson@15409: dest: finite_cartesian_productD1 finite_cartesian_productD2) paulson@15409: done nipkow@15402: nipkow@15402: lemma card_cartesian_product_singleton: "card({x} <*> A) = card(A)" paulson@15409: by (simp add: card_cartesian_product) paulson@15409: nipkow@15402: nipkow@15402: wenzelm@12396: subsubsection {* Cardinality of the Powerset *} wenzelm@12396: wenzelm@12396: lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A" (* FIXME numeral 2 (!?) *) wenzelm@12396: apply (induct set: Finites) wenzelm@12396: apply (simp_all add: Pow_insert) paulson@14208: apply (subst card_Un_disjoint, blast) paulson@14208: apply (blast intro: finite_imageI, blast) wenzelm@12396: apply (subgoal_tac "inj_on (insert x) (Pow F)") wenzelm@12396: apply (simp add: card_image Pow_insert) wenzelm@12396: apply (unfold inj_on_def) wenzelm@12396: apply (blast elim!: equalityE) wenzelm@12396: done wenzelm@12396: nipkow@15392: text {* Relates to equivalence classes. Based on a theorem of nipkow@15392: F. Kammüller's. *} wenzelm@12396: wenzelm@12396: lemma dvd_partition: nipkow@15392: "finite (Union C) ==> wenzelm@12396: ALL c : C. k dvd card c ==> paulson@14430: (ALL c1: C. ALL c2: C. c1 \ c2 --> c1 Int c2 = {}) ==> wenzelm@12396: k dvd card (Union C)" nipkow@15392: apply(frule finite_UnionD) nipkow@15392: apply(rotate_tac -1) paulson@14208: apply (induct set: Finites, simp_all, clarify) wenzelm@12396: apply (subst card_Un_disjoint) wenzelm@12396: apply (auto simp add: dvd_add disjoint_eq_subset_Compl) wenzelm@12396: done wenzelm@12396: wenzelm@12396: nipkow@15392: subsubsection {* Theorems about @{text "choose"} *} wenzelm@12396: wenzelm@12396: text {* nipkow@15392: \medskip Basic theorem about @{text "choose"}. By Florian nipkow@15392: Kamm\"uller, tidied by LCP. wenzelm@12396: *} wenzelm@12396: nipkow@15392: lemma card_s_0_eq_empty: nipkow@15392: "finite A ==> card {B. B \ A & card B = 0} = 1" nipkow@15392: apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq]) nipkow@15392: apply (simp cong add: rev_conj_cong) nipkow@15392: done wenzelm@12396: nipkow@15392: lemma choose_deconstruct: "finite M ==> x \ M nipkow@15392: ==> {s. s <= insert x M & card(s) = Suc k} nipkow@15392: = {s. s <= M & card(s) = Suc k} Un nipkow@15392: {s. EX t. t <= M & card(t) = k & s = insert x t}" nipkow@15392: apply safe nipkow@15392: apply (auto intro: finite_subset [THEN card_insert_disjoint]) nipkow@15392: apply (drule_tac x = "xa - {x}" in spec) nipkow@15392: apply (subgoal_tac "x \ xa", auto) nipkow@15392: apply (erule rev_mp, subst card_Diff_singleton) nipkow@15392: apply (auto intro: finite_subset) wenzelm@12396: done wenzelm@12396: nipkow@15392: text{*There are as many subsets of @{term A} having cardinality @{term k} nipkow@15392: as there are sets obtained from the former by inserting a fixed element nipkow@15392: @{term x} into each.*} nipkow@15392: lemma constr_bij: nipkow@15392: "[|finite A; x \ A|] ==> nipkow@15392: card {B. EX C. C <= A & card(C) = k & B = insert x C} = nipkow@15392: card {B. B <= A & card(B) = k}" nipkow@15392: apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq) nipkow@15392: apply (auto elim!: equalityE simp add: inj_on_def) nipkow@15392: apply (subst Diff_insert0, auto) nipkow@15392: txt {* finiteness of the two sets *} nipkow@15392: apply (rule_tac [2] B = "Pow (A)" in finite_subset) nipkow@15392: apply (rule_tac B = "Pow (insert x A)" in finite_subset) nipkow@15392: apply fast+ wenzelm@12396: done wenzelm@12396: nipkow@15392: text {* nipkow@15392: Main theorem: combinatorial statement about number of subsets of a set. nipkow@15392: *} wenzelm@12396: nipkow@15392: lemma n_sub_lemma: nipkow@15392: "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)" nipkow@15392: apply (induct k) nipkow@15392: apply (simp add: card_s_0_eq_empty, atomize) nipkow@15392: apply (rotate_tac -1, erule finite_induct) nipkow@15392: apply (simp_all (no_asm_simp) cong add: conj_cong nipkow@15392: add: card_s_0_eq_empty choose_deconstruct) nipkow@15392: apply (subst card_Un_disjoint) nipkow@15392: prefer 4 apply (force simp add: constr_bij) nipkow@15392: prefer 3 apply force nipkow@15392: prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2] nipkow@15392: finite_subset [of _ "Pow (insert x F)", standard]) nipkow@15392: apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset]) wenzelm@12396: done wenzelm@12396: nipkow@15392: theorem n_subsets: nipkow@15392: "finite A ==> card {B. B <= A & card B = k} = (card A choose k)" nipkow@15392: by (simp add: n_sub_lemma) nipkow@15392: nipkow@15392: nipkow@15392: subsection{* A fold functional for non-empty sets *} nipkow@15392: nipkow@15392: text{* Does not require start value. *} wenzelm@12396: nipkow@15392: consts paulson@15506: fold1Set :: "('a => 'a => 'a) => ('a set \ 'a) set" nipkow@15392: paulson@15506: inductive "fold1Set f" nipkow@15392: intros paulson@15506: fold1Set_insertI [intro]: paulson@15506: "\ (A,x) \ foldSet f id a; a \ A \ \ (insert a A, x) \ fold1Set f" wenzelm@12396: nipkow@15392: constdefs nipkow@15392: fold1 :: "('a => 'a => 'a) => 'a set => 'a" paulson@15506: "fold1 f A == THE x. (A, x) : fold1Set f" paulson@15506: paulson@15506: lemma fold1Set_nonempty: paulson@15506: "(A, x) : fold1Set f \ A \ {}" paulson@15506: by(erule fold1Set.cases, simp_all) paulson@15506: nipkow@15392: paulson@15506: inductive_cases empty_fold1SetE [elim!]: "({}, x) : fold1Set f" paulson@15506: paulson@15506: inductive_cases insert_fold1SetE [elim!]: "(insert a X, x) : fold1Set f" paulson@15506: paulson@15506: paulson@15506: lemma fold1Set_sing [iff]: "(({a},b) : fold1Set f) = (a = b)" paulson@15506: by (blast intro: foldSet.intros elim: foldSet.cases) nipkow@15392: paulson@15508: lemma fold1_singleton[simp]: "fold1 f {a} = a" paulson@15508: by (unfold fold1_def) blast wenzelm@12396: paulson@15508: lemma finite_nonempty_imp_fold1Set: paulson@15508: "\ finite A; A \ {} \ \ EX x. (A, x) : fold1Set f" paulson@15508: apply (induct A rule: finite_induct) paulson@15508: apply (auto dest: finite_imp_foldSet [of _ f id]) paulson@15508: done paulson@15506: paulson@15506: text{*First, some lemmas about @{term foldSet}.*} nipkow@15392: paulson@15508: lemma (in ACf) foldSet_insert_swap: paulson@15508: assumes fold: "(A,y) \ foldSet f id b" paulson@15521: shows "b \ A \ (insert b A, z \ y) \ foldSet f id z" paulson@15508: using fold paulson@15508: proof (induct rule: foldSet.induct) paulson@15508: case emptyI thus ?case by (force simp add: fold_insert_aux commute) paulson@15508: next paulson@15508: case (insertI A x y) paulson@15508: have "(insert x (insert b A), x \ (z \ y)) \ foldSet f (\u. u) z" paulson@15521: using insertI by force --{*how does @{term id} get unfolded?*} paulson@15508: thus ?case by (simp add: insert_commute AC) paulson@15508: qed paulson@15508: paulson@15508: lemma (in ACf) foldSet_permute_diff: paulson@15508: assumes fold: "(A,x) \ foldSet f id b" paulson@15508: shows "!!a. \a \ A; b \ A\ \ (insert b (A-{a}), x) \ foldSet f id a" paulson@15508: using fold paulson@15508: proof (induct rule: foldSet.induct) paulson@15508: case emptyI thus ?case by simp paulson@15508: next paulson@15508: case (insertI A x y) paulson@15521: have "a = x \ a \ A" using insertI by simp paulson@15521: thus ?case paulson@15521: proof paulson@15521: assume "a = x" paulson@15521: with insertI show ?thesis paulson@15521: by (simp add: id_def [symmetric], blast intro: foldSet_insert_swap) paulson@15521: next paulson@15521: assume ainA: "a \ A" paulson@15521: hence "(insert x (insert b (A - {a})), x \ y) \ foldSet f id a" paulson@15521: using insertI by (force simp: id_def) paulson@15521: moreover paulson@15521: have "insert x (insert b (A - {a})) = insert b (insert x A - {a})" paulson@15521: using ainA insertI by blast paulson@15521: ultimately show ?thesis by (simp add: id_def) paulson@15508: qed paulson@15508: qed paulson@15508: paulson@15508: lemma (in ACf) fold1_eq_fold: paulson@15508: "[|finite A; a \ A|] ==> fold1 f (insert a A) = fold f id a A" paulson@15508: apply (simp add: fold1_def fold_def) paulson@15508: apply (rule the_equality) paulson@15508: apply (best intro: foldSet_determ theI dest: finite_imp_foldSet [of _ f id]) paulson@15508: apply (rule sym, clarify) paulson@15508: apply (case_tac "Aa=A") paulson@15508: apply (best intro: the_equality foldSet_determ) paulson@15508: apply (subgoal_tac "(A,x) \ foldSet f id a") paulson@15508: apply (best intro: the_equality foldSet_determ) paulson@15508: apply (subgoal_tac "insert aa (Aa - {a}) = A") paulson@15508: prefer 2 apply (blast elim: equalityE) paulson@15508: apply (auto dest: foldSet_permute_diff [where a=a]) paulson@15508: done paulson@15508: paulson@15521: lemma nonempty_iff: "(A \ {}) = (\x B. A = insert x B & x \ B)" paulson@15521: apply safe paulson@15521: apply simp paulson@15521: apply (drule_tac x=x in spec) paulson@15521: apply (drule_tac x="A-{x}" in spec, auto) paulson@15508: done paulson@15508: paulson@15521: lemma (in ACf) fold1_insert: paulson@15521: assumes nonempty: "A \ {}" and A: "finite A" "x \ A" paulson@15521: shows "fold1 f (insert x A) = f x (fold1 f A)" paulson@15521: proof - paulson@15521: from nonempty obtain a A' where "A = insert a A' & a ~: A'" paulson@15521: by (auto simp add: nonempty_iff) paulson@15521: with A show ?thesis paulson@15521: by (simp add: insert_commute [of x] fold1_eq_fold eq_commute) paulson@15521: qed paulson@15521: paulson@15509: lemma (in ACIf) fold1_insert_idem [simp]: paulson@15521: assumes nonempty: "A \ {}" and A: "finite A" paulson@15521: shows "fold1 f (insert x A) = f x (fold1 f A)" paulson@15521: proof - paulson@15521: from nonempty obtain a A' where A': "A = insert a A' & a ~: A'" paulson@15521: by (auto simp add: nonempty_iff) paulson@15521: show ?thesis paulson@15521: proof cases paulson@15521: assume "a = x" paulson@15521: thus ?thesis paulson@15521: proof cases paulson@15521: assume "A' = {}" paulson@15521: with prems show ?thesis by (simp add: idem) paulson@15521: next paulson@15521: assume "A' \ {}" paulson@15521: with prems show ?thesis paulson@15521: by (simp add: fold1_insert assoc [symmetric] idem) paulson@15521: qed paulson@15521: next paulson@15521: assume "a \ x" paulson@15521: with prems show ?thesis paulson@15521: by (simp add: insert_commute fold1_eq_fold fold_insert_idem) paulson@15521: qed paulson@15521: qed paulson@15506: paulson@15506: paulson@15508: text{* Now the recursion rules for definitions: *} paulson@15508: paulson@15508: lemma fold1_singleton_def: "g \ fold1 f \ g {a} = a" paulson@15508: by(simp add:fold1_singleton) paulson@15508: paulson@15508: lemma (in ACf) fold1_insert_def: paulson@15508: "\ g \ fold1 f; finite A; x \ A; A \ {} \ \ g(insert x A) = x \ (g A)" paulson@15508: by(simp add:fold1_insert) paulson@15508: paulson@15509: lemma (in ACIf) fold1_insert_idem_def: paulson@15508: "\ g \ fold1 f; finite A; A \ {} \ \ g(insert x A) = x \ (g A)" paulson@15509: by(simp add:fold1_insert_idem) paulson@15508: paulson@15508: subsubsection{* Determinacy for @{term fold1Set} *} paulson@15508: paulson@15508: text{*Not actually used!!*} wenzelm@12396: paulson@15506: lemma (in ACf) foldSet_permute: paulson@15506: "[|(insert a A, x) \ foldSet f id b; a \ A; b \ A|] paulson@15506: ==> (insert b A, x) \ foldSet f id a" paulson@15506: apply (case_tac "a=b") paulson@15506: apply (auto dest: foldSet_permute_diff) paulson@15506: done nipkow@15376: paulson@15506: lemma (in ACf) fold1Set_determ: paulson@15506: "(A, x) \ fold1Set f ==> (A, y) \ fold1Set f ==> y = x" paulson@15506: proof (clarify elim!: fold1Set.cases) paulson@15506: fix A x B y a b paulson@15506: assume Ax: "(A, x) \ foldSet f id a" paulson@15506: assume By: "(B, y) \ foldSet f id b" paulson@15506: assume anotA: "a \ A" paulson@15506: assume bnotB: "b \ B" paulson@15506: assume eq: "insert a A = insert b B" paulson@15506: show "y=x" paulson@15506: proof cases paulson@15506: assume same: "a=b" paulson@15506: hence "A=B" using anotA bnotB eq by (blast elim!: equalityE) paulson@15506: thus ?thesis using Ax By same by (blast intro: foldSet_determ) nipkow@15392: next paulson@15506: assume diff: "a\b" paulson@15506: let ?D = "B - {a}" paulson@15506: have B: "B = insert a ?D" and A: "A = insert b ?D" paulson@15506: and aB: "a \ B" and bA: "b \ A" paulson@15506: using eq anotA bnotB diff by (blast elim!:equalityE)+ paulson@15506: with aB bnotB By paulson@15506: have "(insert b ?D, y) \ foldSet f id a" paulson@15506: by (auto intro: foldSet_permute simp add: insert_absorb) paulson@15506: moreover paulson@15506: have "(insert b ?D, x) \ foldSet f id a" paulson@15506: by (simp add: A [symmetric] Ax) paulson@15506: ultimately show ?thesis by (blast intro: foldSet_determ) nipkow@15392: qed wenzelm@12396: qed wenzelm@12396: paulson@15506: lemma (in ACf) fold1Set_equality: "(A, y) : fold1Set f ==> fold1 f A = y" paulson@15506: by (unfold fold1_def) (blast intro: fold1Set_determ) paulson@15506: paulson@15506: declare paulson@15506: empty_foldSetE [rule del] foldSet.intros [rule del] paulson@15506: empty_fold1SetE [rule del] insert_fold1SetE [rule del] paulson@15506: -- {* No more proves involve these relations. *} nipkow@15376: nipkow@15497: subsubsection{* Semi-Lattices *} nipkow@15497: nipkow@15497: locale ACIfSL = ACIf + nipkow@15500: fixes below :: "'a \ 'a \ bool" (infixl "\" 50) nipkow@15500: assumes below_def: "(x \ y) = (x\y = x)" nipkow@15497: nipkow@15497: locale ACIfSLlin = ACIfSL + nipkow@15497: assumes lin: "x\y \ {x,y}" nipkow@15497: nipkow@15500: lemma (in ACIfSL) below_refl[simp]: "x \ x" nipkow@15497: by(simp add: below_def idem) nipkow@15497: nipkow@15500: lemma (in ACIfSL) below_f_conv[simp]: "x \ y \ z = (x \ y \ x \ z)" nipkow@15497: proof nipkow@15500: assume "x \ y \ z" nipkow@15497: hence xyzx: "x \ (y \ z) = x" by(simp add: below_def) nipkow@15497: have "x \ y = x" nipkow@15497: proof - nipkow@15497: have "x \ y = (x \ (y \ z)) \ y" by(rule subst[OF xyzx], rule refl) nipkow@15497: also have "\ = x \ (y \ z)" by(simp add:ACI) nipkow@15497: also have "\ = x" by(rule xyzx) nipkow@15497: finally show ?thesis . nipkow@15497: qed nipkow@15497: moreover have "x \ z = x" nipkow@15497: proof - nipkow@15497: have "x \ z = (x \ (y \ z)) \ z" by(rule subst[OF xyzx], rule refl) nipkow@15497: also have "\ = x \ (y \ z)" by(simp add:ACI) nipkow@15497: also have "\ = x" by(rule xyzx) nipkow@15497: finally show ?thesis . nipkow@15497: qed nipkow@15500: ultimately show "x \ y \ x \ z" by(simp add: below_def) nipkow@15497: next nipkow@15500: assume a: "x \ y \ x \ z" nipkow@15497: hence y: "x \ y = x" and z: "x \ z = x" by(simp_all add: below_def) nipkow@15497: have "x \ (y \ z) = (x \ y) \ z" by(simp add:assoc) nipkow@15497: also have "x \ y = x" using a by(simp_all add: below_def) nipkow@15497: also have "x \ z = x" using a by(simp_all add: below_def) nipkow@15500: finally show "x \ y \ z" by(simp_all add: below_def) nipkow@15497: qed nipkow@15497: nipkow@15497: lemma (in ACIfSLlin) above_f_conv: nipkow@15500: "x \ y \ z = (x \ z \ y \ z)" nipkow@15497: proof nipkow@15500: assume a: "x \ y \ z" nipkow@15497: have "x \ y = x \ x \ y = y" using lin[of x y] by simp nipkow@15500: thus "x \ z \ y \ z" nipkow@15497: proof nipkow@15500: assume "x \ y = x" hence "x \ z" by(rule subst)(rule a) thus ?thesis .. nipkow@15497: next nipkow@15500: assume "x \ y = y" hence "y \ z" by(rule subst)(rule a) thus ?thesis .. nipkow@15497: qed nipkow@15497: next nipkow@15500: assume "x \ z \ y \ z" nipkow@15500: thus "x \ y \ z" nipkow@15497: proof nipkow@15500: assume a: "x \ z" nipkow@15497: have "(x \ y) \ z = (x \ z) \ y" by(simp add:ACI) nipkow@15497: also have "x \ z = x" using a by(simp add:below_def) nipkow@15500: finally show "x \ y \ z" by(simp add:below_def) nipkow@15497: next nipkow@15500: assume a: "y \ z" nipkow@15497: have "(x \ y) \ z = x \ (y \ z)" by(simp add:ACI) nipkow@15497: also have "y \ z = y" using a by(simp add:below_def) nipkow@15500: finally show "x \ y \ z" by(simp add:below_def) nipkow@15497: qed nipkow@15497: qed nipkow@15497: nipkow@15497: nipkow@15502: subsubsection{* Lemmas about @{text fold1} *} nipkow@15484: nipkow@15484: lemma (in ACf) fold1_Un: nipkow@15484: assumes A: "finite A" "A \ {}" nipkow@15484: shows "finite B \ B \ {} \ A Int B = {} \ nipkow@15484: fold1 f (A Un B) = f (fold1 f A) (fold1 f B)" nipkow@15484: using A nipkow@15484: proof(induct rule:finite_ne_induct) nipkow@15484: case singleton thus ?case by(simp add:fold1_insert) nipkow@15484: next nipkow@15484: case insert thus ?case by (simp add:fold1_insert assoc) nipkow@15484: qed nipkow@15484: nipkow@15484: lemma (in ACIf) fold1_Un2: nipkow@15484: assumes A: "finite A" "A \ {}" nipkow@15484: shows "finite B \ B \ {} \ nipkow@15484: fold1 f (A Un B) = f (fold1 f A) (fold1 f B)" nipkow@15484: using A nipkow@15484: proof(induct rule:finite_ne_induct) paulson@15509: case singleton thus ?case by(simp add:fold1_insert_idem) nipkow@15484: next paulson@15509: case insert thus ?case by (simp add:fold1_insert_idem assoc) nipkow@15484: qed nipkow@15484: nipkow@15484: lemma (in ACf) fold1_in: nipkow@15484: assumes A: "finite (A)" "A \ {}" and elem: "\x y. x\y \ {x,y}" nipkow@15484: shows "fold1 f A \ A" nipkow@15484: using A nipkow@15484: proof (induct rule:finite_ne_induct) paulson@15506: case singleton thus ?case by simp nipkow@15484: next nipkow@15484: case insert thus ?case using elem by (force simp add:fold1_insert) nipkow@15484: qed nipkow@15484: nipkow@15497: lemma (in ACIfSL) below_fold1_iff: nipkow@15497: assumes A: "finite A" "A \ {}" nipkow@15500: shows "x \ fold1 f A = (\a\A. x \ a)" nipkow@15497: using A nipkow@15497: by(induct rule:finite_ne_induct) simp_all nipkow@15497: nipkow@15497: lemma (in ACIfSL) fold1_belowI: nipkow@15497: assumes A: "finite A" "A \ {}" nipkow@15500: shows "a \ A \ fold1 f A \ a" nipkow@15484: using A nipkow@15484: proof (induct rule:finite_ne_induct) nipkow@15497: case singleton thus ?case by simp nipkow@15484: next nipkow@15497: case (insert x F) berghofe@15517: from insert(5) have "a = x \ a \ F" by simp nipkow@15497: thus ?case nipkow@15497: proof nipkow@15497: assume "a = x" thus ?thesis using insert by(simp add:below_def ACI) nipkow@15497: next nipkow@15497: assume "a \ F" paulson@15508: hence bel: "fold1 f F \ a" by(rule insert) paulson@15508: have "fold1 f (insert x F) \ a = x \ (fold1 f F \ a)" nipkow@15497: using insert by(simp add:below_def ACI) paulson@15508: also have "fold1 f F \ a = fold1 f F" nipkow@15497: using bel by(simp add:below_def ACI) paulson@15508: also have "x \ \ = fold1 f (insert x F)" nipkow@15497: using insert by(simp add:below_def ACI) nipkow@15497: finally show ?thesis by(simp add:below_def) nipkow@15497: qed nipkow@15484: qed nipkow@15484: nipkow@15497: lemma (in ACIfSLlin) fold1_below_iff: nipkow@15497: assumes A: "finite A" "A \ {}" nipkow@15500: shows "fold1 f A \ x = (\a\A. a \ x)" nipkow@15484: using A nipkow@15497: by(induct rule:finite_ne_induct)(simp_all add:above_f_conv) nipkow@15484: nipkow@15512: nipkow@15500: subsubsection{* Lattices *} nipkow@15500: nipkow@15512: locale Lattice = lattice + nipkow@15512: fixes Inf :: "'a set \ 'a" ("\_" [900] 900) nipkow@15500: and Sup :: "'a set \ 'a" ("\_" [900] 900) nipkow@15500: defines "Inf == fold1 inf" and "Sup == fold1 sup" nipkow@15500: nipkow@15512: locale Distrib_Lattice = distrib_lattice + Lattice nipkow@15504: nipkow@15500: text{* Lattices are semilattices *} nipkow@15500: nipkow@15500: lemma (in Lattice) ACf_inf: "ACf inf" nipkow@15512: by(blast intro: ACf.intro inf_commute inf_assoc) nipkow@15500: nipkow@15500: lemma (in Lattice) ACf_sup: "ACf sup" nipkow@15512: by(blast intro: ACf.intro sup_commute sup_assoc) nipkow@15500: nipkow@15500: lemma (in Lattice) ACIf_inf: "ACIf inf" nipkow@15500: apply(rule ACIf.intro) nipkow@15500: apply(rule ACf_inf) nipkow@15500: apply(rule ACIf_axioms.intro) nipkow@15500: apply(rule inf_idem) nipkow@15500: done nipkow@15500: nipkow@15500: lemma (in Lattice) ACIf_sup: "ACIf sup" nipkow@15500: apply(rule ACIf.intro) nipkow@15500: apply(rule ACf_sup) nipkow@15500: apply(rule ACIf_axioms.intro) nipkow@15500: apply(rule sup_idem) nipkow@15500: done nipkow@15500: nipkow@15500: lemma (in Lattice) ACIfSL_inf: "ACIfSL inf (op \)" nipkow@15500: apply(rule ACIfSL.intro) nipkow@15500: apply(rule ACf_inf) nipkow@15500: apply(rule ACIf.axioms[OF ACIf_inf]) nipkow@15500: apply(rule ACIfSL_axioms.intro) nipkow@15500: apply(rule iffI) nipkow@15500: apply(blast intro: antisym inf_le1 inf_le2 inf_least refl) nipkow@15500: apply(erule subst) nipkow@15500: apply(rule inf_le2) nipkow@15500: done nipkow@15500: nipkow@15500: lemma (in Lattice) ACIfSL_sup: "ACIfSL sup (%x y. y \ x)" nipkow@15500: apply(rule ACIfSL.intro) nipkow@15500: apply(rule ACf_sup) nipkow@15500: apply(rule ACIf.axioms[OF ACIf_sup]) nipkow@15500: apply(rule ACIfSL_axioms.intro) nipkow@15500: apply(rule iffI) nipkow@15500: apply(blast intro: antisym sup_ge1 sup_ge2 sup_greatest refl) nipkow@15500: apply(erule subst) nipkow@15500: apply(rule sup_ge2) nipkow@15500: done nipkow@15500: nipkow@15505: nipkow@15505: subsubsection{* Fold laws in lattices *} nipkow@15500: nipkow@15500: lemma (in Lattice) Inf_le_Sup: "\ finite A; A \ {} \ \ \A \ \A" nipkow@15500: apply(unfold Sup_def Inf_def) nipkow@15500: apply(subgoal_tac "EX a. a:A") nipkow@15500: prefer 2 apply blast nipkow@15500: apply(erule exE) nipkow@15500: apply(rule trans) nipkow@15500: apply(erule (2) ACIfSL.fold1_belowI[OF ACIfSL_inf]) nipkow@15500: apply(erule (2) ACIfSL.fold1_belowI[OF ACIfSL_sup]) nipkow@15500: done nipkow@15500: nipkow@15504: lemma (in Lattice) sup_Inf_absorb: nipkow@15504: "\ finite A; A \ {}; a \ A \ \ (a \ \A) = a" nipkow@15512: apply(subst sup_commute) nipkow@15504: apply(simp add:Inf_def sup_absorb ACIfSL.fold1_belowI[OF ACIfSL_inf]) nipkow@15504: done nipkow@15504: nipkow@15504: lemma (in Lattice) inf_Sup_absorb: nipkow@15504: "\ finite A; A \ {}; a \ A \ \ (a \ \A) = a" nipkow@15504: by(simp add:Sup_def inf_absorb ACIfSL.fold1_belowI[OF ACIfSL_sup]) nipkow@15504: nipkow@15504: nipkow@15512: lemma (in Distrib_Lattice) sup_Inf1_distrib: nipkow@15500: assumes A: "finite A" "A \ {}" nipkow@15500: shows "(x \ \A) = \{x \ a|a. a \ A}" nipkow@15500: using A nipkow@15500: proof (induct rule: finite_ne_induct) nipkow@15500: case singleton thus ?case by(simp add:Inf_def) nipkow@15500: next nipkow@15500: case (insert y A) nipkow@15500: have fin: "finite {x \ a |a. a \ A}" berghofe@15517: by(fast intro: finite_surj[where f = "%a. x \ a", OF insert(1)]) nipkow@15500: have "x \ \ (insert y A) = x \ (y \ \ A)" nipkow@15500: using insert by(simp add:ACf.fold1_insert_def[OF ACf_inf Inf_def]) nipkow@15500: also have "\ = (x \ y) \ (x \ \ A)" by(rule sup_inf_distrib1) nipkow@15500: also have "x \ \ A = \{x \ a|a. a \ A}" using insert by simp nipkow@15500: also have "(x \ y) \ \ = \ (insert (x \ y) {x \ a |a. a \ A})" paulson@15509: using insert by(simp add:ACIf.fold1_insert_idem_def[OF ACIf_inf Inf_def fin]) nipkow@15500: also have "insert (x\y) {x\a |a. a \ A} = {x\a |a. a \ insert y A}" nipkow@15500: by blast nipkow@15500: finally show ?case . nipkow@15500: qed nipkow@15500: nipkow@15512: lemma (in Distrib_Lattice) sup_Inf2_distrib: nipkow@15500: assumes A: "finite A" "A \ {}" and B: "finite B" "B \ {}" nipkow@15500: shows "(\A \ \B) = \{a \ b|a b. a \ A \ b \ B}" nipkow@15500: using A nipkow@15500: proof (induct rule: finite_ne_induct) nipkow@15500: case singleton thus ?case nipkow@15500: by(simp add: sup_Inf1_distrib[OF B] fold1_singleton_def[OF Inf_def]) nipkow@15500: next nipkow@15500: case (insert x A) nipkow@15500: have finB: "finite {x \ b |b. b \ B}" berghofe@15517: by(fast intro: finite_surj[where f = "%b. x \ b", OF B(1)]) nipkow@15500: have finAB: "finite {a \ b |a b. a \ A \ b \ B}" nipkow@15500: proof - nipkow@15500: have "{a \ b |a b. a \ A \ b \ B} = (UN a:A. UN b:B. {a \ b})" nipkow@15500: by blast berghofe@15517: thus ?thesis by(simp add: insert(1) B(1)) nipkow@15500: qed nipkow@15500: have ne: "{a \ b |a b. a \ A \ b \ B} \ {}" using insert B by blast nipkow@15500: have "\(insert x A) \ \B = (x \ \A) \ \B" paulson@15509: using insert by(simp add:ACIf.fold1_insert_idem_def[OF ACIf_inf Inf_def]) nipkow@15500: also have "\ = (x \ \B) \ (\A \ \B)" by(rule sup_inf_distrib2) nipkow@15500: also have "\ = \{x \ b|b. b \ B} \ \{a \ b|a b. a \ A \ b \ B}" nipkow@15500: using insert by(simp add:sup_Inf1_distrib[OF B]) nipkow@15500: also have "\ = \({x\b |b. b \ B} \ {a\b |a b. a \ A \ b \ B})" nipkow@15500: (is "_ = \?M") nipkow@15500: using B insert nipkow@15500: by(simp add:Inf_def ACIf.fold1_Un2[OF ACIf_inf finB _ finAB ne]) nipkow@15500: also have "?M = {a \ b |a b. a \ insert x A \ b \ B}" nipkow@15500: by blast nipkow@15500: finally show ?case . nipkow@15500: qed nipkow@15500: nipkow@15484: nipkow@15392: subsection{*Min and Max*} nipkow@15392: nipkow@15392: text{* As an application of @{text fold1} we define the minimal and nipkow@15497: maximal element of a (non-empty) set over a linear order. *} nipkow@15497: nipkow@15497: constdefs nipkow@15497: Min :: "('a::linorder)set => 'a" nipkow@15497: "Min == fold1 min" nipkow@15497: nipkow@15497: Max :: "('a::linorder)set => 'a" nipkow@15497: "Max == fold1 max" nipkow@15497: nipkow@15497: nipkow@15497: text{* Before we can do anything, we need to show that @{text min} and nipkow@15497: @{text max} are ACI and the ordering is linear: *} nipkow@15392: nipkow@15392: lemma ACf_min: "ACf(min :: 'a::linorder \ 'a \ 'a)" nipkow@15392: apply(rule ACf.intro) nipkow@15392: apply(auto simp:min_def) nipkow@15392: done nipkow@15392: nipkow@15392: lemma ACIf_min: "ACIf(min:: 'a::linorder \ 'a \ 'a)" nipkow@15392: apply(rule ACIf.intro[OF ACf_min]) nipkow@15392: apply(rule ACIf_axioms.intro) nipkow@15392: apply(auto simp:min_def) nipkow@15376: done nipkow@15376: nipkow@15392: lemma ACf_max: "ACf(max :: 'a::linorder \ 'a \ 'a)" nipkow@15392: apply(rule ACf.intro) nipkow@15392: apply(auto simp:max_def) nipkow@15392: done nipkow@15392: nipkow@15392: lemma ACIf_max: "ACIf(max:: 'a::linorder \ 'a \ 'a)" nipkow@15392: apply(rule ACIf.intro[OF ACf_max]) nipkow@15392: apply(rule ACIf_axioms.intro) nipkow@15392: apply(auto simp:max_def) nipkow@15376: done wenzelm@12396: nipkow@15497: lemma ACIfSL_min: "ACIfSL(min :: 'a::linorder \ 'a \ 'a) (op \)" nipkow@15497: apply(rule ACIfSL.intro) nipkow@15497: apply(rule ACf_min) nipkow@15497: apply(rule ACIf.axioms[OF ACIf_min]) nipkow@15497: apply(rule ACIfSL_axioms.intro) nipkow@15497: apply(auto simp:min_def) nipkow@15497: done nipkow@15497: nipkow@15497: lemma ACIfSLlin_min: "ACIfSLlin(min :: 'a::linorder \ 'a \ 'a) (op \)" nipkow@15497: apply(rule ACIfSLlin.intro) nipkow@15497: apply(rule ACf_min) nipkow@15497: apply(rule ACIf.axioms[OF ACIf_min]) nipkow@15497: apply(rule ACIfSL.axioms[OF ACIfSL_min]) nipkow@15497: apply(rule ACIfSLlin_axioms.intro) nipkow@15497: apply(auto simp:min_def) nipkow@15497: done nipkow@15392: nipkow@15497: lemma ACIfSL_max: "ACIfSL(max :: 'a::linorder \ 'a \ 'a) (%x y. y\x)" nipkow@15497: apply(rule ACIfSL.intro) nipkow@15497: apply(rule ACf_max) nipkow@15497: apply(rule ACIf.axioms[OF ACIf_max]) nipkow@15497: apply(rule ACIfSL_axioms.intro) nipkow@15497: apply(auto simp:max_def) nipkow@15497: done nipkow@15392: nipkow@15497: lemma ACIfSLlin_max: "ACIfSLlin(max :: 'a::linorder \ 'a \ 'a) (%x y. y\x)" nipkow@15497: apply(rule ACIfSLlin.intro) nipkow@15497: apply(rule ACf_max) nipkow@15497: apply(rule ACIf.axioms[OF ACIf_max]) nipkow@15497: apply(rule ACIfSL.axioms[OF ACIfSL_max]) nipkow@15497: apply(rule ACIfSLlin_axioms.intro) nipkow@15497: apply(auto simp:max_def) nipkow@15497: done nipkow@15392: nipkow@15500: lemma Lattice_min_max: "Lattice (op \) (min :: 'a::linorder \ 'a \ 'a) max" nipkow@15507: apply(rule Lattice.intro) nipkow@15512: apply(rule partial_order_order) nipkow@15512: apply(rule lower_semilattice.axioms[OF lower_semilattice_lin_min]) nipkow@15526: apply(rule upper_semilattice.axioms[OF upper_semilattice_lin_max]) nipkow@15507: done nipkow@15500: nipkow@15512: lemma Distrib_Lattice_min_max: nipkow@15512: "Distrib_Lattice (op \) (min :: 'a::linorder \ 'a \ 'a) max" nipkow@15512: apply(rule Distrib_Lattice.intro) nipkow@15512: apply(rule partial_order_order) nipkow@15512: apply(rule lower_semilattice.axioms[OF lower_semilattice_lin_min]) nipkow@15526: apply(rule upper_semilattice.axioms[OF upper_semilattice_lin_max]) nipkow@15526: apply(rule distrib_lattice.axioms[OF distrib_lattice_min_max]) nipkow@15500: done nipkow@15500: nipkow@15402: text{* Now we instantiate the recursion equations and declare them nipkow@15392: simplification rules: *} nipkow@15392: nipkow@15392: declare nipkow@15392: fold1_singleton_def[OF Min_def, simp] paulson@15509: ACIf.fold1_insert_idem_def[OF ACIf_min Min_def, simp] nipkow@15392: fold1_singleton_def[OF Max_def, simp] paulson@15509: ACIf.fold1_insert_idem_def[OF ACIf_max Max_def, simp] nipkow@15392: nipkow@15484: text{* Now we instantiate some @{text fold1} properties: *} nipkow@15392: nipkow@15392: lemma Min_in [simp]: nipkow@15484: shows "finite A \ A \ {} \ Min A \ A" nipkow@15484: using ACf.fold1_in[OF ACf_min] nipkow@15484: by(fastsimp simp: Min_def min_def) nipkow@15392: nipkow@15392: lemma Max_in [simp]: nipkow@15484: shows "finite A \ A \ {} \ Max A \ A" nipkow@15484: using ACf.fold1_in[OF ACf_max] nipkow@15484: by(fastsimp simp: Max_def max_def) nipkow@15392: nipkow@15484: lemma Min_le [simp]: "\ finite A; A \ {}; x \ A \ \ Min A \ x" nipkow@15497: by(simp add: Min_def ACIfSL.fold1_belowI[OF ACIfSL_min]) nipkow@15392: nipkow@15484: lemma Max_ge [simp]: "\ finite A; A \ {}; x \ A \ \ x \ Max A" nipkow@15497: by(simp add: Max_def ACIfSL.fold1_belowI[OF ACIfSL_max]) nipkow@15497: nipkow@15497: lemma Min_ge_iff[simp]: nipkow@15497: "\ finite A; A \ {} \ \ (x \ Min A) = (\a\A. x \ a)" nipkow@15497: by(simp add: Min_def ACIfSL.below_fold1_iff[OF ACIfSL_min]) nipkow@15497: nipkow@15497: lemma Max_le_iff[simp]: nipkow@15497: "\ finite A; A \ {} \ \ (Max A \ x) = (\a\A. a \ x)" nipkow@15497: by(simp add: Max_def ACIfSL.below_fold1_iff[OF ACIfSL_max]) nipkow@15497: nipkow@15497: lemma Min_le_iff: nipkow@15497: "\ finite A; A \ {} \ \ (Min A \ x) = (\a\A. a \ x)" nipkow@15497: by(simp add: Min_def ACIfSLlin.fold1_below_iff[OF ACIfSLlin_min]) nipkow@15497: nipkow@15497: lemma Max_ge_iff: nipkow@15497: "\ finite A; A \ {} \ \ (x \ Max A) = (\a\A. x \ a)" nipkow@15497: by(simp add: Max_def ACIfSLlin.fold1_below_iff[OF ACIfSLlin_max]) wenzelm@12396: nipkow@15500: lemma Min_le_Max: nipkow@15500: "\ finite A; A \ {} \ \ Min A \ Max A" nipkow@15500: by(simp add: Min_def Max_def Lattice.Inf_le_Sup[OF Lattice_min_max]) paulson@15506: nipkow@15500: lemma max_Min2_distrib: nipkow@15500: "\ finite A; A \ {}; finite B; B \ {} \ \ nipkow@15500: max (Min A) (Min B) = Min{ max a b |a b. a \ A \ b \ B}" nipkow@15512: by(simp add: Min_def Distrib_Lattice.sup_Inf2_distrib[OF Distrib_Lattice_min_max]) paulson@15506: nipkow@15042: end