wenzelm@12396: (* Title: HOL/Finite_Set.thy wenzelm@12396: ID: $Id$ wenzelm@12396: Author: Tobias Nipkow, Lawrence C Paulson and Markus Wenzel avigad@16775: with contributions by Jeremy Avigad wenzelm@12396: *) wenzelm@12396: wenzelm@12396: header {* Finite sets *} wenzelm@12396: nipkow@15131: theory Finite_Set haftmann@24728: imports Divides nipkow@15131: begin wenzelm@12396: nipkow@15392: subsection {* Definition and basic properties *} wenzelm@12396: berghofe@23736: inductive finite :: "'a set => bool" berghofe@22262: where berghofe@22262: emptyI [simp, intro!]: "finite {}" berghofe@22262: | insertI [simp, intro!]: "finite A ==> finite (insert a A)" 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: berghofe@22262: lemma finite_induct [case_names empty insert, induct set: finite]: 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@23389: show "P {}" by fact 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 wenzelm@23389: assume "F = {}" wenzelm@23389: thus ?thesis using `P {x}` by simp nipkow@15484: next wenzelm@23389: assume "F \ {}" wenzelm@23389: 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@23389: assumes "finite F" and "F \ A" wenzelm@23389: and empty: "P {}" wenzelm@23389: and insert: "!!a F. finite F ==> a \ A ==> a \ F ==> P F ==> P (insert a F)" wenzelm@23389: shows "P F" wenzelm@12396: proof - wenzelm@23389: from `finite F` and `F \ A` wenzelm@23389: show ?thesis wenzelm@12396: proof induct wenzelm@23389: show "P {}" by fact wenzelm@23389: next wenzelm@23389: fix x F wenzelm@23389: assume "finite F" and "x \ F" and wenzelm@23389: 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@23389: show "finite F" by fact wenzelm@23389: show "x \ F" by fact wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: qed wenzelm@12396: haftmann@23878: 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) wenzelm@23389: have notinA: "a \ A" by fact 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. *} berghofe@22262: by (induct set: finite) 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@23389: have A: "A \ insert x F" and r: "A - {x} \ F ==> finite (A - {x})" by fact+ 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@23389: also have "insert x (A - {x}) = A" using x by (rule insert_Diff) wenzelm@12396: finally show ?thesis . wenzelm@12396: next wenzelm@23389: show "A \ F ==> ?thesis" by fact 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: nipkow@18423: lemma finite_Collect_subset[simp]: "finite A \ finite{x \ A. P x}" nipkow@17761: using finite_subset[of "{x \ A. P x}" "A"] by blast nipkow@17761: 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@23389: assumes "finite A" wenzelm@23389: and "P A" wenzelm@23389: and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})" wenzelm@23389: shows "P {}" wenzelm@12396: proof - wenzelm@12396: have "P (A - A)" wenzelm@12396: proof - wenzelm@23389: { wenzelm@23389: fix c b :: "'a set" wenzelm@23389: assume c: "finite c" and b: "finite b" wenzelm@23389: and P1: "P b" and P2: "!!x y. finite y ==> x \ y ==> P y ==> P (y - {x})" wenzelm@23389: have "c \ b ==> P (b - c)" wenzelm@23389: using c wenzelm@23389: proof induct wenzelm@23389: case empty wenzelm@23389: from P1 show ?case by simp wenzelm@23389: next wenzelm@23389: case (insert x F) wenzelm@23389: have "P (b - F - {x})" wenzelm@23389: proof (rule P2) wenzelm@23389: from _ b show "finite (b - F)" by (rule finite_subset) blast wenzelm@23389: from insert show "x \ b - F" by simp wenzelm@23389: from insert show "P (b - F)" by simp wenzelm@23389: qed wenzelm@23389: also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric]) wenzelm@23389: finally show ?case . wenzelm@12396: qed wenzelm@23389: } wenzelm@23389: then show ?thesis by this (simp_all add: assms) wenzelm@12396: qed wenzelm@23389: then show ?thesis 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: paulson@19870: lemma finite_Diff_singleton [simp]: "finite (A - {a}) = finite A" paulson@19870: by simp paulson@19870: 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. *} berghofe@22262: by (induct set: finite) 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. *} berghofe@22262: apply (induct set: finite) wenzelm@21575: apply 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)" berghofe@22262: by (induct set: finite) 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@17022: lemma finite_Plus: "[| finite A; finite B |] ==> finite (A <+> B)" nipkow@17022: by (simp add: Plus_def) nipkow@17022: 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. kA) \ 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"}. *} berghofe@22262: apply (induct set: finite) 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: haftmann@26041: subsection {* Class @{text finite} and code generation *} haftmann@26041: haftmann@26041: lemma finite_code [code func]: haftmann@26041: "finite {} \ True" haftmann@26041: "finite (insert a A) \ finite A" haftmann@26041: by auto haftmann@26041: haftmann@26041: setup {* Sign.add_path "finite" *} -- {*FIXME: name tweaking*} haftmann@26041: class finite (attach UNIV) = type + haftmann@26041: fixes itself :: "'a itself" haftmann@26041: assumes finite_UNIV: "finite (UNIV \ 'a set)" haftmann@26041: setup {* Sign.parent_path *} haftmann@26041: hide const finite haftmann@26041: haftmann@26041: lemma finite [simp]: "finite (A \ 'a\finite set)" haftmann@26041: by (rule finite_subset [OF subset_UNIV finite_UNIV]) haftmann@26041: haftmann@26041: lemma univ_unit [noatp]: haftmann@26041: "UNIV = {()}" by auto haftmann@26041: haftmann@26041: instantiation unit :: finite haftmann@26041: begin haftmann@26041: haftmann@26041: definition haftmann@26041: "itself = TYPE(unit)" haftmann@26041: haftmann@26041: instance proof haftmann@26041: have "finite {()}" by simp haftmann@26041: also note univ_unit [symmetric] haftmann@26041: finally show "finite (UNIV :: unit set)" . haftmann@26041: qed haftmann@26041: haftmann@26041: end haftmann@26041: haftmann@26041: lemmas [code func] = univ_unit haftmann@26041: haftmann@26041: lemma univ_bool [noatp]: haftmann@26041: "UNIV = {False, True}" by auto haftmann@26041: haftmann@26041: instantiation bool :: finite haftmann@26041: begin haftmann@26041: haftmann@26041: definition haftmann@26041: "itself = TYPE(bool)" haftmann@26041: haftmann@26041: instance proof haftmann@26041: have "finite {False, True}" by simp haftmann@26041: also note univ_bool [symmetric] haftmann@26041: finally show "finite (UNIV :: bool set)" . haftmann@26041: qed haftmann@26041: haftmann@26041: end haftmann@26041: haftmann@26041: lemmas [code func] = univ_bool haftmann@26041: haftmann@26041: instantiation * :: (finite, finite) finite haftmann@26041: begin haftmann@26041: haftmann@26041: definition haftmann@26041: "itself = TYPE('a \ 'b)" haftmann@26041: haftmann@26041: instance proof haftmann@26041: show "finite (UNIV :: ('a \ 'b) set)" haftmann@26041: proof (rule finite_Prod_UNIV) haftmann@26041: show "finite (UNIV :: 'a set)" by (rule finite) haftmann@26041: show "finite (UNIV :: 'b set)" by (rule finite) haftmann@26041: qed haftmann@26041: qed haftmann@26041: haftmann@26041: end haftmann@26041: haftmann@26041: lemma univ_prod [noatp, code func]: haftmann@26041: "UNIV = (UNIV \ 'a\finite set) \ (UNIV \ 'b\finite set)" haftmann@26041: unfolding UNIV_Times_UNIV .. haftmann@26041: haftmann@26041: instantiation "+" :: (finite, finite) finite haftmann@26041: begin haftmann@26041: haftmann@26041: definition haftmann@26041: "itself = TYPE('a + 'b)" haftmann@26041: haftmann@26041: instance proof haftmann@26041: have a: "finite (UNIV :: 'a set)" by (rule finite) haftmann@26041: have b: "finite (UNIV :: 'b set)" by (rule finite) haftmann@26041: from a b have "finite ((UNIV :: 'a set) <+> (UNIV :: 'b set))" haftmann@26041: by (rule finite_Plus) haftmann@26041: thus "finite (UNIV :: ('a + 'b) set)" by simp haftmann@26041: qed haftmann@26041: haftmann@26041: end haftmann@26041: haftmann@26041: lemma univ_sum [noatp, code func]: haftmann@26041: "UNIV = (UNIV \ 'a\finite set) <+> (UNIV \ 'b\finite set)" haftmann@26041: unfolding UNIV_Plus_UNIV .. haftmann@26041: haftmann@26041: instantiation set :: (finite) finite haftmann@26041: begin haftmann@26041: haftmann@26041: definition haftmann@26041: "itself = TYPE('a set)" haftmann@26041: haftmann@26041: instance proof haftmann@26041: have "finite (UNIV :: 'a set)" by (rule finite) haftmann@26041: hence "finite (Pow (UNIV :: 'a set))" haftmann@26041: by (rule finite_Pow_iff [THEN iffD2]) haftmann@26041: thus "finite (UNIV :: 'a set set)" by simp haftmann@26041: qed haftmann@26041: haftmann@26041: end haftmann@26041: haftmann@26041: lemma univ_set [noatp, code func]: haftmann@26041: "UNIV = Pow (UNIV \ 'a\finite set)" unfolding Pow_UNIV .. haftmann@26041: haftmann@26041: lemma inj_graph: "inj (%f. {(x, y). y = f x})" haftmann@26041: by (rule inj_onI, auto simp add: expand_set_eq expand_fun_eq) haftmann@26041: haftmann@26041: instantiation "fun" :: (finite, finite) finite haftmann@26041: begin haftmann@26041: haftmann@26041: definition haftmann@26041: "itself \ TYPE('a \ 'b)" haftmann@26041: haftmann@26041: instance proof haftmann@26041: show "finite (UNIV :: ('a => 'b) set)" haftmann@26041: proof (rule finite_imageD) haftmann@26041: let ?graph = "%f::'a => 'b. {(x, y). y = f x}" haftmann@26041: show "finite (range ?graph)" by (rule finite) haftmann@26041: show "inj ?graph" by (rule inj_graph) haftmann@26041: qed haftmann@26041: qed haftmann@26041: haftmann@26041: end haftmann@26041: haftmann@26041: hide (open) const itself haftmann@26041: haftmann@26041: subsection {* Equality and order on functions *} haftmann@26041: haftmann@26041: instance "fun" :: (finite, eq) eq .. haftmann@26041: haftmann@26041: lemma eq_fun [code func]: haftmann@26041: fixes f g :: "'a\finite \ 'b\eq" haftmann@26041: shows "f = g \ (\x\UNIV. f x = g x)" haftmann@26041: unfolding expand_fun_eq by auto haftmann@26041: haftmann@26041: lemma order_fun [code func]: haftmann@26041: fixes f g :: "'a\finite \ 'b\order" haftmann@26041: shows "f \ g \ (\x\UNIV. f x \ g x)" haftmann@26041: and "f < g \ f \ g \ (\x\UNIV. f x \ g x)" haftmann@26041: by (auto simp add: expand_fun_eq le_fun_def less_fun_def order_less_le) haftmann@26041: haftmann@26041: 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: berghofe@23736: inductive berghofe@22262: foldSet :: "('a => 'a => 'a) => ('b => 'a) => 'a => 'b set => 'a => bool" berghofe@22262: for f :: "'a => 'a => 'a" berghofe@22262: and g :: "'b => 'a" berghofe@22262: and z :: 'a berghofe@22262: where berghofe@22262: emptyI [intro]: "foldSet f g z {} z" berghofe@22262: | insertI [intro]: berghofe@22262: "\ x \ A; foldSet f g z A y \ berghofe@22262: \ foldSet f g z (insert x A) (f (g x) y)" berghofe@22262: berghofe@23736: inductive_cases empty_foldSetE [elim!]: "foldSet f g z {} x" nipkow@15392: nipkow@15392: constdefs nipkow@21733: fold :: "('a => 'a => 'a) => ('b => 'a) => 'a => 'b set => 'a" berghofe@22262: "fold f g z A == THE x. foldSet f g z A x" nipkow@15392: paulson@15498: text{*A tempting alternative for the definiens is berghofe@22262: @{term "if finite A then THE x. foldSet f g e A x 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: berghofe@22262: "foldSet f g z (A - {x}) y ==> x: A ==> foldSet f g z A (f (g x) y)" nipkow@15392: by (erule insert_Diff [THEN subst], rule foldSet.intros, auto) nipkow@15392: berghofe@22262: lemma foldSet_imp_finite: "foldSet f g z A x==> finite A" nipkow@15392: by (induct set: foldSet) auto nipkow@15392: berghofe@22262: lemma finite_imp_foldSet: "finite A ==> EX x. foldSet f g z A x" berghofe@22262: by (induct set: finite) auto nipkow@15392: nipkow@15392: 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})" wenzelm@19868: 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: haftmann@26041: context ab_semigroup_mult haftmann@26041: begin haftmann@26041: haftmann@26041: lemma 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 \ x' = x" by fact haftmann@26041: have Afoldx: "foldSet times g z A x" and Afoldx': "foldSet times g z A x'" wenzelm@23389: and A: "A = h`{i. i B" and Bu: "foldSet times g z B u" paulson@15510: show "x'=x" paulson@15510: proof (rule foldSet.cases [OF Afoldx']) berghofe@22262: assume "A = {}" and "x' = z" paulson@15510: with AbB show "x' = x" by blast nipkow@15392: next paulson@15510: fix C c v haftmann@26041: assume AcC: "A = insert c C" and x': "x' = g c * v" haftmann@26041: and notinC: "c \ C" and Cv: "foldSet times g z C v" 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 haftmann@26041: then obtain d where Dfoldd: "foldSet times g z ?D d" nipkow@17589: using finite_imp_foldSet by iprover paulson@15506: moreover have cinB: "c \ B" using B by auto haftmann@26041: ultimately have "foldSet times g z B (g c * d)" nipkow@15392: by(rule Diff1_foldSet) haftmann@26041: then have "g c * d = u" by (rule IH [OF lessB Beq inj_onB Bu]) haftmann@26041: then have "u = g c * d" .. haftmann@26041: moreover have "v = g b * d" haftmann@26041: proof (rule sym, rule IH [OF lessC Ceq inj_onC Cv]) haftmann@26041: show "foldSet times g z C (g b * d)" using C notinB Dfoldd nipkow@15392: by fastsimp nipkow@15392: qed haftmann@26041: ultimately show ?thesis using x x' haftmann@26041: by (simp add: mult_left_commute) nipkow@15392: qed nipkow@15392: qed nipkow@15392: qed nipkow@15392: qed nipkow@15392: haftmann@26041: lemma foldSet_determ: haftmann@26041: "foldSet times g z A x ==> foldSet times g z A y ==> 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: haftmann@26041: lemma fold_equality: "foldSet times g z A y ==> fold times 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: haftmann@26041: lemma (in -) fold_empty [simp]: "fold f g z {} = z" nipkow@15392: by (unfold fold_def) blast nipkow@15392: haftmann@26041: lemma fold_insert_aux: "x \ A ==> haftmann@26041: (foldSet times g z (insert x A) v) = haftmann@26041: (EX y. foldSet times g z A y & v = g x * y)" nipkow@15392: apply auto haftmann@26041: apply (rule_tac A1 = A and f1 = times 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: haftmann@26041: lemma fold_insert [simp]: haftmann@26041: "finite A ==> x \ A ==> fold times g z (insert x A) = g x * fold times 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: haftmann@26041: lemma fold_rec: nipkow@15535: assumes fin: "finite A" and a: "a:A" haftmann@26041: shows "fold times g z A = g a * fold times g z (A - {a})" nipkow@15535: proof- nipkow@15535: have A: "A = insert a (A - {a})" using a by blast haftmann@26041: hence "fold times g z A = fold times g z (insert a (A - {a}))" by simp haftmann@26041: also have "\ = g a * fold times g z (A - {a})" nipkow@15535: by(rule fold_insert) (simp add:fin)+ nipkow@15535: finally show ?thesis . nipkow@15535: qed nipkow@15535: haftmann@26041: end nipkow@15392: nipkow@15480: text{* A simplified version for idempotent functions: *} nipkow@15480: haftmann@26041: context ab_semigroup_idem_mult haftmann@26041: begin haftmann@26041: haftmann@26041: lemma fold_insert_idem: nipkow@15480: assumes finA: "finite A" haftmann@26041: shows "fold times g z (insert a A) = g a * fold times 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) haftmann@26041: have "fold times g z (insert a A) = fold times g z (insert a B)" using A by simp haftmann@26041: also have "\ = g a * fold times g z B" paulson@15506: using finB disj by simp haftmann@26041: also have "\ = g a * fold times g z A" haftmann@26041: using A finB disj haftmann@26041: by (simp add: mult_idem mult_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: haftmann@26041: lemma foldI_conv_id: haftmann@26041: "finite A \ fold times g z A = fold times id z (g ` A)" paulson@15509: by(erule finite_induct)(simp_all add: fold_insert_idem del: fold_insert) nipkow@15484: haftmann@26041: end haftmann@26041: nipkow@15392: subsubsection{*Lemmas about @{text fold}*} nipkow@15392: haftmann@26041: context ab_semigroup_mult haftmann@26041: begin haftmann@26041: haftmann@26041: lemma fold_commute: haftmann@26041: "finite A ==> (!!z. x * (fold times g z A) = fold times g (x * z) A)" berghofe@22262: apply (induct set: finite) wenzelm@21575: apply simp haftmann@26041: apply (simp add: mult_left_commute [of x]) nipkow@15392: done nipkow@15392: haftmann@26041: lemma fold_nest_Un_Int: nipkow@15392: "finite A ==> finite B haftmann@26041: ==> fold times g (fold times g z B) A = fold times g (fold times g z (A Int B)) (A Un B)" berghofe@22262: apply (induct set: finite) wenzelm@21575: apply simp nipkow@15392: apply (simp add: fold_commute Int_insert_left insert_absorb) nipkow@15392: done nipkow@15392: haftmann@26041: lemma fold_nest_Un_disjoint: nipkow@15392: "finite A ==> finite B ==> A Int B = {} haftmann@26041: ==> fold times g z (A Un B) = fold times g (fold times g z B) A" nipkow@15392: by (simp add: fold_nest_Un_Int) nipkow@15392: haftmann@26041: lemma fold_reindex: paulson@15487: assumes fin: "finite A" haftmann@26041: shows "inj_on h A \ fold times g z (h ` A) = fold times (g \ h) z A" paulson@15506: using fin apply induct nipkow@15392: apply simp nipkow@15392: apply simp nipkow@15392: done nipkow@15392: haftmann@26041: text{* haftmann@26041: Fusion theorem, as described in Graham Hutton's paper, haftmann@26041: A Tutorial on the Universality and Expressiveness of Fold, haftmann@26041: JFP 9:4 (355-372), 1999. haftmann@26041: *} haftmann@26041: haftmann@26041: lemma fold_fusion: haftmann@26041: includes ab_semigroup_mult g haftmann@26041: assumes fin: "finite A" haftmann@26041: and hyp: "\x y. h (g x y) = times x (h y)" haftmann@26041: shows "h (fold g j w A) = fold times j (h w) A" haftmann@26041: using fin hyp by (induct set: finite) simp_all haftmann@26041: haftmann@26041: lemma fold_cong: haftmann@26041: "finite A \ (!!x. x:A ==> g x = h x) ==> fold times g z A = fold times h z A" haftmann@26041: apply (subgoal_tac "ALL C. C <= A --> (ALL x:C. g x = h x) --> fold times g z C = fold times 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: haftmann@26041: end haftmann@26041: haftmann@26041: context comm_monoid_mult haftmann@26041: begin haftmann@26041: haftmann@26041: lemma fold_Un_Int: haftmann@26041: "finite A ==> finite B ==> haftmann@26041: fold times g 1 A * fold times g 1 B = haftmann@26041: fold times g 1 (A Un B) * fold times g 1 (A Int B)" haftmann@26041: by (induct set: finite) haftmann@26041: (auto simp add: mult_ac insert_absorb Int_insert_left) haftmann@26041: haftmann@26041: corollary fold_Un_disjoint: haftmann@26041: "finite A ==> finite B ==> A Int B = {} ==> haftmann@26041: fold times g 1 (A Un B) = fold times g 1 A * fold times g 1 B" haftmann@26041: by (simp add: fold_Un_Int) haftmann@26041: haftmann@26041: lemma fold_UN_disjoint: haftmann@26041: "\ finite I; ALL i:I. finite (A i); haftmann@26041: ALL i:I. ALL j:I. i \ j --> A i Int A j = {} \ haftmann@26041: \ fold times g 1 (UNION I A) = haftmann@26041: fold times (%i. fold times g 1 (A i)) 1 I" haftmann@26041: apply (induct set: finite, simp, atomize) haftmann@26041: apply (subgoal_tac "ALL i:F. x \ i") haftmann@26041: prefer 2 apply blast haftmann@26041: apply (subgoal_tac "A x Int UNION F A = {}") haftmann@26041: prefer 2 apply blast haftmann@26041: apply (simp add: fold_Un_disjoint) haftmann@26041: done haftmann@26041: haftmann@26041: lemma fold_Sigma: "finite A ==> ALL x:A. finite (B x) ==> haftmann@26041: fold times (%x. fold times (g x) 1 (B x)) 1 A = haftmann@26041: fold times (split g) 1 (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: haftmann@26041: lemma fold_distrib: "finite A \ haftmann@26041: fold times (%x. g x * h x) 1 A = fold times g 1 A * fold times h 1 A" haftmann@26041: by (erule finite_induct) (simp_all add: mult_ac) haftmann@26041: haftmann@26041: end haftmann@22917: haftmann@22917: nipkow@15402: subsection {* Generalized summation over a set *} nipkow@15402: haftmann@26041: interpretation comm_monoid_add: comm_monoid_mult ["0::'a::comm_monoid_add" "op +"] haftmann@26041: by unfold_locales (auto intro: add_assoc add_commute) haftmann@26041: 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: wenzelm@19535: abbreviation wenzelm@21404: Setsum ("\_" [1000] 999) where wenzelm@19535: "\A == setsum (%x. x) A" wenzelm@19535: 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 paulson@17189: "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add" ("(3SUM _:_. _)" [0, 51, 10] 10) nipkow@15402: syntax (xsymbols) paulson@17189: "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add" ("(3\_\_. _)" [0, 51, 10] 10) nipkow@15402: syntax (HTML output) paulson@17189: "_setsum" :: "pttrn => '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 paulson@17189: "_qsetsum" :: "pttrn \ bool \ 'a \ 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10) nipkow@15402: syntax (xsymbols) paulson@17189: "_qsetsum" :: "pttrn \ bool \ 'a \ 'a" ("(3\_ | (_)./ _)" [0,0,10] 10) nipkow@15402: syntax (HTML output) paulson@17189: "_qsetsum" :: "pttrn \ 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: print_translation {* nipkow@15402: let wenzelm@19535: fun setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = wenzelm@19535: if x<>y then raise Match wenzelm@19535: else let val x' = Syntax.mark_bound x wenzelm@19535: val t' = subst_bound(x',t) wenzelm@19535: val P' = subst_bound(x',P) wenzelm@19535: in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end wenzelm@19535: in [("setsum", setsum_tr')] end nipkow@15402: *} nipkow@15402: wenzelm@19535: 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" ballarin@15765: by (simp add: setsum_def) 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" haftmann@26041: by(auto simp add: setsum_def comm_monoid_add.fold_reindex 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" haftmann@26041: by(fastsimp simp: setsum_def intro: comm_monoid_add.fold_cong) nipkow@15402: nipkow@16733: lemma strong_setsum_cong[cong]: nipkow@16733: "A = B ==> (!!x. x:B =simp=> f x = g x) nipkow@16733: ==> setsum (%x. f x) A = setsum (%x. g x) B" haftmann@26041: by(fastsimp simp: simp_implies_def setsum_def intro: comm_monoid_add.fold_cong) berghofe@16632: nipkow@15554: lemma setsum_cong2: "\\x. x \ A \ f x = g x\ \ setsum f A = setsum g A"; nipkow@15554: by (rule setsum_cong[OF refl], auto); nipkow@15554: nipkow@15402: lemma setsum_reindex_cong: nipkow@15554: "[|inj_on f A; B = f ` A; !!a. 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@15542: lemma setsum_0[simp]: "setsum (%i. 0) A = 0" nipkow@15402: apply (clarsimp simp: setsum_def) ballarin@15765: apply (erule finite_induct, auto) nipkow@15402: done nipkow@15402: nipkow@15543: lemma setsum_0': "ALL a:A. f a = 0 ==> setsum f A = 0" nipkow@15543: by(simp add:setsum_cong) 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! *} haftmann@26041: by(simp add: setsum_def comm_monoid_add.fold_Un_Int [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))" haftmann@26041: by(simp add: setsum_def comm_monoid_add.fold_UN_disjoint 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) ==> paulson@17189: (\x\A. (\y\B x. f x y)) = (\(x,y)\(SIGMA x:A. B x). f x y)" haftmann@26041: by(simp add:setsum_def comm_monoid_add.fold_Sigma 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@17189: "(\x\A. (\y\B. f x y)) = (\(x,y) \ A <*> B. f x y)" 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) nipkow@15543: apply (simp) 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)" haftmann@26041: by(simp add:setsum_def comm_monoid_add.fold_distrib) 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))" berghofe@22262: by (induct set: finite) 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@23477: by (subst setsum_Un_Int [symmetric], auto simp add: ring_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@23477: by (subst setsum_Un_Int [symmetric], auto simp add: ring_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: obua@15552: lemma setsum_diff1'[rule_format]: "finite A \ a \ A \ (\ x \ A. f x) = f a + (\ x \ (A - {a}). f x)" obua@15552: apply (erule finite_induct[where F=A and P="% A. (a \ A \ (\ x \ A. f x) = f a + (\ x \ (A - {a}). f x))"]) obua@15552: apply (auto simp add: insert_Diff_if add_ac) obua@15552: done obua@15552: nipkow@15402: (* By Jeremy Siek: *) nipkow@15402: nipkow@15402: lemma setsum_diff_nat: wenzelm@19535: assumes "finite B" wenzelm@19535: and "B \ A" wenzelm@19535: shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)" wenzelm@19535: using prems wenzelm@19535: 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 wenzelm@21575: proof induct wenzelm@19535: case empty wenzelm@19535: thus ?case by auto wenzelm@19535: next wenzelm@19535: case (insert x F) wenzelm@19535: thus ?case using le finiteB wenzelm@19535: by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb) nipkow@15402: qed wenzelm@19535: 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 wenzelm@19535: proof induct nipkow@15402: case empty nipkow@15402: thus ?case by simp nipkow@15402: next nipkow@15402: case insert wenzelm@19535: thus ?case using add_mono by fastsimp 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@15554: lemma setsum_strict_mono: wenzelm@19535: fixes f :: "'a \ 'b::{pordered_cancel_ab_semigroup_add,comm_monoid_add}" wenzelm@19535: assumes "finite A" "A \ {}" wenzelm@19535: and "!!x. x:A \ f x < g x" wenzelm@19535: shows "setsum f A < setsum g A" wenzelm@19535: using prems nipkow@15554: proof (induct rule: finite_ne_induct) nipkow@15554: case singleton thus ?case by simp nipkow@15554: next nipkow@15554: case insert thus ?case by (auto simp: add_strict_mono) nipkow@15554: qed nipkow@15554: nipkow@15535: lemma setsum_negf: wenzelm@19535: "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A" nipkow@15535: proof (cases "finite A") berghofe@22262: case True thus ?thesis by (induct set: finite) 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: wenzelm@19535: "setsum (%x. ((f x)::'a::ab_group_add) - g x) A = wenzelm@19535: 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: wenzelm@19535: assumes nn: "\x\A. (0::'a::{pordered_ab_semigroup_add,comm_monoid_add}) \ f x" wenzelm@19535: shows "0 \ setsum f A" nipkow@15535: proof (cases "finite A") nipkow@15535: case True thus ?thesis using nn wenzelm@21575: proof induct wenzelm@19535: case empty then show ?case by simp wenzelm@19535: next wenzelm@19535: case (insert x F) wenzelm@19535: then have "0 + 0 \ f x + setsum f F" by (blast intro: add_mono) wenzelm@19535: with insert show ?case by simp wenzelm@19535: qed nipkow@15535: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15535: qed nipkow@15402: nipkow@15535: lemma setsum_nonpos: wenzelm@19535: assumes np: "\x\A. f x \ (0::'a::{pordered_ab_semigroup_add,comm_monoid_add})" wenzelm@19535: shows "setsum f A \ 0" nipkow@15535: proof (cases "finite A") nipkow@15535: case True thus ?thesis using np wenzelm@21575: proof induct wenzelm@19535: case empty then show ?case by simp wenzelm@19535: next wenzelm@19535: case (insert x F) wenzelm@19535: then have "f x + setsum f F \ 0 + 0" by (blast intro: add_mono) wenzelm@19535: with insert show ?case by simp wenzelm@19535: qed nipkow@15535: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15535: qed nipkow@15402: nipkow@15539: lemma setsum_mono2: nipkow@15539: fixes f :: "'a \ 'b :: {pordered_ab_semigroup_add_imp_le,comm_monoid_add}" nipkow@15539: assumes fin: "finite B" and sub: "A \ B" and nn: "\b. b \ B-A \ 0 \ f b" nipkow@15539: shows "setsum f A \ setsum f B" nipkow@15539: proof - nipkow@15539: have "setsum f A \ setsum f A + setsum f (B-A)" nipkow@15539: by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def) nipkow@15539: also have "\ = setsum f (A \ (B-A))" using fin finite_subset[OF sub fin] nipkow@15539: by (simp add:setsum_Un_disjoint del:Un_Diff_cancel) nipkow@15539: also have "A \ (B-A) = B" using sub by blast nipkow@15539: finally show ?thesis . nipkow@15539: qed nipkow@15542: avigad@16775: lemma setsum_mono3: "finite B ==> A <= B ==> avigad@16775: ALL x: B - A. avigad@16775: 0 <= ((f x)::'a::{comm_monoid_add,pordered_ab_semigroup_add}) ==> avigad@16775: setsum f A <= setsum f B" avigad@16775: apply (subgoal_tac "setsum f B = setsum f A + setsum f (B - A)") avigad@16775: apply (erule ssubst) avigad@16775: apply (subgoal_tac "setsum f A + 0 <= setsum f A + setsum f (B - A)") avigad@16775: apply simp avigad@16775: apply (rule add_left_mono) avigad@16775: apply (erule setsum_nonneg) avigad@16775: apply (subst setsum_Un_disjoint [THEN sym]) avigad@16775: apply (erule finite_subset, assumption) avigad@16775: apply (rule finite_subset) avigad@16775: prefer 2 avigad@16775: apply assumption avigad@16775: apply auto avigad@16775: apply (rule setsum_cong) avigad@16775: apply auto avigad@16775: done avigad@16775: ballarin@19279: lemma setsum_right_distrib: huffman@22934: fixes f :: "'a => ('b::semiring_0)" 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 wenzelm@21575: 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: ballarin@17149: lemma setsum_left_distrib: huffman@22934: "setsum f A * (r::'a::semiring_0) = (\n\A. f n * r)" ballarin@17149: proof (cases "finite A") ballarin@17149: case True ballarin@17149: then show ?thesis ballarin@17149: proof induct ballarin@17149: case empty thus ?case by simp ballarin@17149: next ballarin@17149: case (insert x A) thus ?case by (simp add: left_distrib) ballarin@17149: qed ballarin@17149: next ballarin@17149: case False thus ?thesis by (simp add: setsum_def) ballarin@17149: qed ballarin@17149: ballarin@17149: lemma setsum_divide_distrib: ballarin@17149: "setsum f A / (r::'a::field) = (\n\A. f n / r)" ballarin@17149: proof (cases "finite A") ballarin@17149: case True ballarin@17149: then show ?thesis ballarin@17149: proof induct ballarin@17149: case empty thus ?case by simp ballarin@17149: next ballarin@17149: case (insert x A) thus ?case by (simp add: add_divide_distrib) ballarin@17149: qed ballarin@17149: next ballarin@17149: case False thus ?thesis by (simp add: setsum_def) ballarin@17149: qed ballarin@17149: nipkow@15535: lemma setsum_abs[iff]: haftmann@25303: fixes f :: "'a => ('b::pordered_ab_group_add_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 wenzelm@21575: 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]: haftmann@25303: fixes f :: "'a => ('b::pordered_ab_group_add_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 wenzelm@21575: proof induct nipkow@15535: case empty thus ?case by simp nipkow@15535: next nipkow@21733: case (insert x A) thus ?case by (auto simp: add_nonneg_nonneg) nipkow@15535: qed nipkow@15402: next nipkow@15535: case False thus ?thesis by (simp add: setsum_def) nipkow@15402: qed nipkow@15402: nipkow@15539: lemma abs_setsum_abs[simp]: haftmann@25303: fixes f :: "'a => ('b::pordered_ab_group_add_abs)" nipkow@15539: shows "abs (\a\A. abs(f a)) = (\a\A. abs(f a))" nipkow@15539: proof (cases "finite A") nipkow@15539: case True nipkow@15539: thus ?thesis wenzelm@21575: proof induct nipkow@15539: case empty thus ?case by simp nipkow@15539: next nipkow@15539: case (insert a A) nipkow@15539: hence "\\a\insert a A. \f a\\ = \\f a\ + (\a\A. \f a\)\" by simp nipkow@15539: also have "\ = \\f a\ + \\a\A. \f a\\\" using insert by simp avigad@16775: also have "\ = \f a\ + \\a\A. \f a\\" avigad@16775: by (simp del: abs_of_nonneg) nipkow@15539: also have "\ = (\a\insert a A. \f a\)" using insert by simp nipkow@15539: finally show ?case . nipkow@15539: qed nipkow@15539: next nipkow@15539: case False thus ?thesis by (simp add: setsum_def) nipkow@15539: qed nipkow@15539: nipkow@15402: ballarin@17149: text {* Commuting outer and inner summation *} ballarin@17149: ballarin@17149: lemma swap_inj_on: ballarin@17149: "inj_on (%(i, j). (j, i)) (A \ B)" ballarin@17149: by (unfold inj_on_def) fast ballarin@17149: ballarin@17149: lemma swap_product: ballarin@17149: "(%(i, j). (j, i)) ` (A \ B) = B \ A" ballarin@17149: by (simp add: split_def image_def) blast ballarin@17149: ballarin@17149: lemma setsum_commute: ballarin@17149: "(\i\A. \j\B. f i j) = (\j\B. \i\A. f i j)" ballarin@17149: proof (simp add: setsum_cartesian_product) paulson@17189: have "(\(x,y) \ A <*> B. f x y) = paulson@17189: (\(y,x) \ (%(i, j). (j, i)) ` (A \ B). f x y)" ballarin@17149: (is "?s = _") ballarin@17149: apply (simp add: setsum_reindex [where f = "%(i, j). (j, i)"] swap_inj_on) ballarin@17149: apply (simp add: split_def) ballarin@17149: done paulson@17189: also have "... = (\(y,x)\B \ A. f x y)" ballarin@17149: (is "_ = ?t") ballarin@17149: apply (simp add: swap_product) ballarin@17149: done ballarin@17149: finally show "?s = ?t" . ballarin@17149: qed ballarin@17149: ballarin@19279: lemma setsum_product: huffman@22934: fixes f :: "'a => ('b::semiring_0)" ballarin@19279: shows "setsum f A * setsum g B = (\i\A. \j\B. f i * g j)" ballarin@19279: by (simp add: setsum_right_distrib setsum_left_distrib) (rule setsum_commute) ballarin@19279: ballarin@17149: 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: wenzelm@19535: abbreviation wenzelm@21404: Setprod ("\_" [1000] 999) where wenzelm@19535: "\A == setprod (%x. x) A" wenzelm@19535: nipkow@15402: syntax paulson@17189: "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult" ("(3PROD _:_. _)" [0, 51, 10] 10) nipkow@15402: syntax (xsymbols) paulson@17189: "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult" ("(3\_\_. _)" [0, 51, 10] 10) nipkow@15402: syntax (HTML output) paulson@17189: "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult" ("(3\_\_. _)" [0, 51, 10] 10) nipkow@16550: nipkow@16550: translations -- {* Beware of argument permutation! *} nipkow@16550: "PROD i:A. b" == "setprod (%i. b) A" nipkow@16550: "\i\A. b" == "setprod (%i. b) A" nipkow@16550: nipkow@16550: text{* Instead of @{term"\x\{x. P}. e"} we introduce the shorter nipkow@16550: @{text"\x|P. e"}. *} nipkow@16550: nipkow@16550: syntax paulson@17189: "_qsetprod" :: "pttrn \ bool \ 'a \ 'a" ("(3PROD _ |/ _./ _)" [0,0,10] 10) nipkow@16550: syntax (xsymbols) paulson@17189: "_qsetprod" :: "pttrn \ bool \ 'a \ 'a" ("(3\_ | (_)./ _)" [0,0,10] 10) nipkow@16550: syntax (HTML output) paulson@17189: "_qsetprod" :: "pttrn \ bool \ 'a \ 'a" ("(3\_ | (_)./ _)" [0,0,10] 10) nipkow@16550: nipkow@15402: translations nipkow@16550: "PROD x|P. t" => "setprod (%x. t) {x. P}" nipkow@16550: "\x|P. t" => "setprod (%x. t) {x. P}" nipkow@16550: 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" ballarin@19931: by (simp add: setprod_def) 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" haftmann@26041: by(auto simp: setprod_def fold_reindex 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" haftmann@26041: by(fastsimp simp: setprod_def intro: fold_cong) nipkow@15402: berghofe@16632: lemma strong_setprod_cong: berghofe@16632: "A = B ==> (!!x. x:B =simp=> f x = g x) ==> setprod f A = setprod g B" haftmann@26041: by(fastsimp simp: simp_implies_def setprod_def intro: fold_cong) berghofe@16632: 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" haftmann@26041: by(simp add: setprod_def fold_Un_Int[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" haftmann@26041: by(simp add: setprod_def fold_UN_disjoint 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@16550: (\x\A. (\y\ B x. f x y)) = paulson@17189: (\(x,y)\(SIGMA x:A. B x). f x y)" haftmann@26041: by(simp add:setprod_def fold_Sigma 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@17189: "(\x\A. (\y\ B. f x y)) = (\(x,y)\(A <*> B). f x y)" 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)" haftmann@26041: by(simp add:setprod_def fold_distrib) 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))" berghofe@22262: by (induct set: finite) auto nipkow@15402: nipkow@15402: lemma setprod_zero: huffman@23277: "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1) ==> setprod f A = 0" berghofe@22262: apply (induct set: finite, 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") berghofe@22262: apply (induct set: finite, 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") berghofe@22262: apply (induct set: finite, 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]: huffman@23277: "(ALL x y. (x::'a::comm_semiring_1) * 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: huffman@23277: "(ALL x y. (x::'a::comm_semiring_1) * 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: huffman@23277: "finite A ==> (ALL x: A. f x \ (0::'a::idom)) ==> setprod f A \ 0" nipkow@15402: apply (rule setprod_nonzero, auto) nipkow@15402: done nipkow@15402: nipkow@15402: lemma setprod_zero_eq_field: huffman@23277: "finite A ==> (setprod f A = (0::'a::idom)) = (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@23398: apply (subst times_divide_eq_right [THEN sym], auto) 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@23413: by (erule finite_induct) (auto simp add: insert_Diff_if) 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: haftmann@25459: definition haftmann@25459: card :: "'a set \ nat" haftmann@25459: where haftmann@25459: [code func del]: "card A = setsum (\x. 1) A" wenzelm@12396: wenzelm@12396: lemma card_empty [simp]: "card {} = 0" nipkow@24853: by (simp add: card_def) nipkow@15402: paulson@24427: lemma card_infinite [simp]: "~ finite A ==> card A = 0" nipkow@24853: 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)" ballarin@15765: by(simp add: card_def) 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: paulson@24286: lemma card_0_eq [simp,noatp]: "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: nipkow@24853: 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: nipkow@24853: "finite A ==> x: A ==> card (A - {x}) = card A - 1" nipkow@24853: by (simp add: card_Suc_Diff1 [symmetric]) wenzelm@12396: wenzelm@12396: lemma card_Diff_singleton_if: nipkow@24853: "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)" nipkow@24853: by (simp add: card_Diff_singleton) nipkow@24853: nipkow@24853: lemma card_Diff_insert[simp]: nipkow@24853: assumes "finite A" and "a:A" and "a ~: B" nipkow@24853: shows "card(A - insert a B) = card(A - B) - 1" nipkow@24853: proof - nipkow@24853: have "A - insert a B = (A - B) - {a}" using assms by blast nipkow@24853: then show ?thesis using assms by(simp add:card_Diff_singleton) nipkow@24853: qed wenzelm@12396: wenzelm@12396: lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))" nipkow@24853: by (simp add: card_insert_if card_Suc_Diff1 del:card_Diff_insert) wenzelm@12396: haftmann@26041: lemma card_code [code func]: haftmann@26041: "card {} = 0" haftmann@26041: "card (insert a A) = haftmann@26041: (if finite A then Suc (card (A - {a})) else card (insert a A))" haftmann@26041: by (auto simp add: card_insert) haftmann@26041: wenzelm@12396: lemma card_insert_le: "finite A ==> card A <= card (insert x A)" nipkow@24853: by (simp add: card_insert_if) wenzelm@12396: nipkow@15402: lemma card_mono: "\ finite B; A \ B \ \ card A \ card B" nipkow@15539: by (simp add: card_def setsum_mono2) nipkow@15402: wenzelm@12396: lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)" berghofe@22262: apply (induct set: finite, 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" nipkow@24853: apply (simp add: psubset_def linorder_not_le [symmetric]) nipkow@24853: apply (blast dest: card_seteq) nipkow@24853: 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" nipkow@24853: 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) nipkow@24853: apply (simp add: card_Suc_Diff1 del:card_Diff_insert) 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") nipkow@24853: apply (simp add: card_Diff1_less del:card_Diff_insert) wenzelm@12396: apply (rule less_trans) nipkow@24853: prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert) 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@19793: text{* 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@19793: text{*The form of a finite set of given cardinality*} paulson@19793: paulson@19793: lemma card_eq_SucD: nipkow@24853: assumes "card A = Suc k" nipkow@24853: shows "\b B. A = insert b B & b \ B & card B = k & (k=0 \ B={})" paulson@19793: proof - nipkow@24853: have fin: "finite A" using assms by (auto intro: ccontr) nipkow@24853: moreover have "card A \ 0" using assms by auto nipkow@24853: ultimately obtain b where b: "b \ A" by auto paulson@19793: show ?thesis paulson@19793: proof (intro exI conjI) paulson@19793: show "A = insert b (A-{b})" using b by blast paulson@19793: show "b \ A - {b}" by blast nipkow@24853: show "card (A - {b}) = k" and "k = 0 \ A - {b} = {}" nipkow@24853: using assms b fin by(fastsimp dest:mk_disjoint_insert)+ paulson@19793: qed paulson@19793: qed paulson@19793: paulson@19793: lemma card_Suc_eq: nipkow@24853: "(card A = Suc k) = nipkow@24853: (\b B. A = insert b B & b \ B & card B = k & (k=0 \ B={}))" nipkow@24853: apply(rule iffI) nipkow@24853: apply(erule card_eq_SucD) nipkow@24853: apply(auto) nipkow@24853: apply(subst card_insert) nipkow@24853: apply(auto intro:ccontr) nipkow@24853: done paulson@19793: nipkow@15539: lemma setsum_constant [simp]: "(\x \ A. y) = of_nat(card A) * y" nipkow@15539: apply (cases "finite A") nipkow@15539: apply (erule finite_induct) nipkow@23477: apply (auto simp add: ring_simps) paulson@15409: done nipkow@15402: krauss@21199: lemma setprod_constant: "finite A ==> (\x\ A. (y::'a::{recpower, comm_monoid_mult})) = y^(card A)" nipkow@15402: apply (erule finite_induct) nipkow@15402: apply (auto simp add: power_Suc) nipkow@15402: done nipkow@15402: nipkow@15542: lemma setsum_bounded: huffman@23277: assumes le: "\i. i\A \ f i \ (K::'a::{semiring_1, pordered_ab_semigroup_add})" nipkow@15542: shows "setsum f A \ of_nat(card A) * K" nipkow@15542: proof (cases "finite A") nipkow@15542: case True nipkow@15542: thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simp nipkow@15542: next nipkow@15542: case False thus ?thesis by (simp add: setsum_def) nipkow@15542: qed nipkow@15542: 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@15539: apply (simp add: card_def del: setsum_constant) nipkow@15402: apply (subgoal_tac nipkow@15402: "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I") nipkow@15539: apply (simp add: setsum_UN_disjoint del: setsum_constant) nipkow@15539: apply (simp 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" haftmann@26041: proof (induct rule: finite_induct) haftmann@26041: case empty then show ?case by simp haftmann@26041: next haftmann@26041: invoke ab_semigroup_mult ["op Un"] haftmann@26041: by unfold_locales auto haftmann@26041: case insert haftmann@26041: then show ?case by simp haftmann@26041: qed paulson@15447: wenzelm@12396: lemma card_image_le: "finite A ==> card (f ` A) <= card A" berghofe@22262: apply (induct set: finite) wenzelm@21575: apply 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@15539: by(simp add:card_def setsum_reindex o_def del:setsum_constant) wenzelm@12396: wenzelm@12396: lemma endo_inj_surj: "finite A ==> f ` A \ A ==> inj_on f A ==> f ` A = A" nipkow@25162: 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" wenzelm@21575: apply (induct rule:finite_induct) wenzelm@21575: apply 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@15539: by(simp add:card_def setsum_Sigma del:setsum_constant) 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 (auto simp add: card_eq_0_iff nipkow@15539: dest: finite_cartesian_productD1 finite_cartesian_productD2) paulson@15409: done nipkow@15402: nipkow@15402: lemma card_cartesian_product_singleton: "card({x} <*> A) = card(A)" nipkow@15539: 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 (!?) *) berghofe@22262: apply (induct set: finite) 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: haftmann@24342: text {* Relates to equivalence classes. Based on a theorem of F. Kammüller. *} 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) berghofe@22262: apply (induct set: finite, 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@25162: subsubsection {* Relating injectivity and surjectivity *} nipkow@25162: nipkow@25162: lemma finite_surj_inj: "finite(A) \ A <= f`A \ inj_on f A" nipkow@25162: apply(rule eq_card_imp_inj_on, assumption) nipkow@25162: apply(frule finite_imageI) nipkow@25162: apply(drule (1) card_seteq) nipkow@25162: apply(erule card_image_le) nipkow@25162: apply simp nipkow@25162: done nipkow@25162: nipkow@25162: lemma finite_UNIV_surj_inj: fixes f :: "'a \ 'a" nipkow@25162: shows "finite(UNIV:: 'a set) \ surj f \ inj f" nipkow@25162: by (blast intro: finite_surj_inj subset_UNIV dest:surj_range) nipkow@25162: nipkow@25162: lemma finite_UNIV_inj_surj: fixes f :: "'a \ 'a" nipkow@25162: shows "finite(UNIV:: 'a set) \ inj f \ surj f" nipkow@25162: by(fastsimp simp:surj_def dest!: endo_inj_surj) nipkow@25162: nipkow@25162: corollary infinite_UNIV_nat: "~finite(UNIV::nat set)" nipkow@25162: proof nipkow@25162: assume "finite(UNIV::nat set)" nipkow@25162: with finite_UNIV_inj_surj[of Suc] nipkow@25162: show False by simp (blast dest: Suc_neq_Zero surjD) nipkow@25162: qed nipkow@25162: nipkow@25162: nipkow@15392: subsection{* A fold functional for non-empty sets *} nipkow@15392: nipkow@15392: text{* Does not require start value. *} wenzelm@12396: berghofe@23736: inductive berghofe@22262: fold1Set :: "('a => 'a => 'a) => 'a set => 'a => bool" berghofe@22262: for f :: "'a => 'a => 'a" berghofe@22262: where paulson@15506: fold1Set_insertI [intro]: berghofe@22262: "\ foldSet f id a A x; a \ A \ \ fold1Set f (insert a A) x" wenzelm@12396: nipkow@15392: constdefs nipkow@15392: fold1 :: "('a => 'a => 'a) => 'a set => 'a" berghofe@22262: "fold1 f A == THE x. fold1Set f A x" paulson@15506: paulson@15506: lemma fold1Set_nonempty: haftmann@22917: "fold1Set f A x \ A \ {}" haftmann@22917: by(erule fold1Set.cases, simp_all) nipkow@15392: berghofe@23736: inductive_cases empty_fold1SetE [elim!]: "fold1Set f {} x" berghofe@23736: berghofe@23736: inductive_cases insert_fold1SetE [elim!]: "fold1Set f (insert a X) x" berghofe@22262: berghofe@22262: berghofe@22262: lemma fold1Set_sing [iff]: "(fold1Set f {a} b) = (a = b)" paulson@15506: by (blast intro: foldSet.intros elim: foldSet.cases) nipkow@15392: haftmann@22917: lemma fold1_singleton [simp]: "fold1 f {a} = a" paulson@15508: by (unfold fold1_def) blast wenzelm@12396: paulson@15508: lemma finite_nonempty_imp_fold1Set: berghofe@22262: "\ finite A; A \ {} \ \ EX x. fold1Set f A x" 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: haftmann@26041: context ab_semigroup_mult haftmann@26041: begin haftmann@26041: haftmann@26041: lemma foldSet_insert_swap: haftmann@26041: assumes fold: "foldSet times id b A y" haftmann@26041: shows "b \ A \ foldSet times id z (insert b A) (z * y)" paulson@15508: using fold paulson@15508: proof (induct rule: foldSet.induct) haftmann@26041: case emptyI thus ?case by (force simp add: fold_insert_aux mult_commute) paulson@15508: next berghofe@22262: case (insertI x A y) haftmann@26041: have "foldSet times (\u. u) z (insert x (insert b A)) (x * (z * y))" paulson@15521: using insertI by force --{*how does @{term id} get unfolded?*} haftmann@26041: thus ?case by (simp add: insert_commute mult_ac) paulson@15508: qed paulson@15508: haftmann@26041: lemma foldSet_permute_diff: haftmann@26041: assumes fold: "foldSet times id b A x" haftmann@26041: shows "!!a. \a \ A; b \ A\ \ foldSet times id a (insert b (A-{a})) x" paulson@15508: using fold paulson@15508: proof (induct rule: foldSet.induct) paulson@15508: case emptyI thus ?case by simp paulson@15508: next berghofe@22262: case (insertI x A 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" haftmann@26041: hence "foldSet times id a (insert x (insert b (A - {a}))) (x * y)" 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: haftmann@26041: lemma fold1_eq_fold: haftmann@26041: "[|finite A; a \ A|] ==> fold1 times (insert a A) = fold times id a A" paulson@15508: apply (simp add: fold1_def fold_def) paulson@15508: apply (rule the_equality) haftmann@26041: apply (best intro: foldSet_determ theI dest: finite_imp_foldSet [of _ times id]) paulson@15508: apply (rule sym, clarify) paulson@15508: apply (case_tac "Aa=A") paulson@15508: apply (best intro: the_equality foldSet_determ) haftmann@26041: apply (subgoal_tac "foldSet times id a A x") 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: haftmann@26041: lemma fold1_insert: paulson@15521: assumes nonempty: "A \ {}" and A: "finite A" "x \ A" haftmann@26041: shows "fold1 times (insert x A) = x * fold1 times 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: haftmann@26041: end haftmann@26041: haftmann@26041: context ab_semigroup_idem_mult haftmann@26041: begin haftmann@26041: haftmann@26041: lemma fold1_insert_idem [simp]: paulson@15521: assumes nonempty: "A \ {}" and A: "finite A" haftmann@26041: shows "fold1 times (insert x A) = x * fold1 times 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' = {}" haftmann@26041: with prems show ?thesis by (simp add: mult_idem) paulson@15521: next paulson@15521: assume "A' \ {}" paulson@15521: with prems show ?thesis haftmann@26041: by (simp add: fold1_insert mult_assoc [symmetric] mult_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: haftmann@26041: lemma hom_fold1_commute: haftmann@26041: assumes hom: "!!x y. h (x * y) = h x * h y" haftmann@26041: and N: "finite N" "N \ {}" shows "h (fold1 times N) = fold1 times (h ` N)" haftmann@22917: using N proof (induct rule: finite_ne_induct) haftmann@22917: case singleton thus ?case by simp haftmann@22917: next haftmann@22917: case (insert n N) haftmann@26041: then have "h (fold1 times (insert n N)) = h (n * fold1 times N)" by simp haftmann@26041: also have "\ = h n * h (fold1 times N)" by(rule hom) haftmann@26041: also have "h (fold1 times N) = fold1 times (h ` N)" by(rule insert) haftmann@26041: also have "times (h n) \ = fold1 times (insert (h n) (h ` N))" haftmann@22917: using insert by(simp) haftmann@22917: also have "insert (h n) (h ` N) = h ` insert n N" by simp haftmann@22917: finally show ?case . haftmann@22917: qed haftmann@22917: haftmann@26041: end haftmann@26041: paulson@15506: paulson@15508: text{* Now the recursion rules for definitions: *} paulson@15508: haftmann@22917: lemma fold1_singleton_def: "g = fold1 f \ g {a} = a" paulson@15508: by(simp add:fold1_singleton) paulson@15508: haftmann@26041: lemma (in ab_semigroup_mult) fold1_insert_def: haftmann@26041: "\ g = fold1 times; finite A; x \ A; A \ {} \ \ g (insert x A) = x * g A" haftmann@26041: by (simp add:fold1_insert) haftmann@26041: haftmann@26041: lemma (in ab_semigroup_idem_mult) fold1_insert_idem_def: haftmann@26041: "\ g = fold1 times; finite A; A \ {} \ \ g (insert x A) = x * g A" haftmann@26041: by simp paulson@15508: paulson@15508: subsubsection{* Determinacy for @{term fold1Set} *} paulson@15508: paulson@15508: text{*Not actually used!!*} wenzelm@12396: haftmann@26041: context ab_semigroup_mult haftmann@26041: begin haftmann@26041: haftmann@26041: lemma foldSet_permute: haftmann@26041: "[|foldSet times id b (insert a A) x; a \ A; b \ A|] haftmann@26041: ==> foldSet times id a (insert b A) x" haftmann@26041: apply (cases "a=b") paulson@15506: apply (auto dest: foldSet_permute_diff) paulson@15506: done nipkow@15376: haftmann@26041: lemma fold1Set_determ: haftmann@26041: "fold1Set times A x ==> fold1Set times A y ==> y = x" paulson@15506: proof (clarify elim!: fold1Set.cases) paulson@15506: fix A x B y a b haftmann@26041: assume Ax: "foldSet times id a A x" haftmann@26041: assume By: "foldSet times id b B y" 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 haftmann@26041: have "foldSet times id a (insert b ?D) y" paulson@15506: by (auto intro: foldSet_permute simp add: insert_absorb) paulson@15506: moreover haftmann@26041: have "foldSet times id a (insert b ?D) x" 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: haftmann@26041: lemma fold1Set_equality: "fold1Set times A y ==> fold1 times A = y" paulson@15506: by (unfold fold1_def) (blast intro: fold1Set_determ) paulson@15506: haftmann@26041: end haftmann@26041: paulson@15506: declare paulson@15506: empty_foldSetE [rule del] foldSet.intros [rule del] paulson@15506: empty_fold1SetE [rule del] insert_fold1SetE [rule del] ballarin@19931: -- {* No more proofs involve these relations. *} nipkow@15376: haftmann@26041: subsubsection {* Lemmas about @{text fold1} *} haftmann@26041: haftmann@26041: context ab_semigroup_mult haftmann@22917: begin haftmann@22917: haftmann@26041: lemma fold1_Un: nipkow@15484: assumes A: "finite A" "A \ {}" nipkow@15484: shows "finite B \ B \ {} \ A Int B = {} \ haftmann@26041: fold1 times (A Un B) = fold1 times A * fold1 times B" haftmann@26041: using A by (induct rule: finite_ne_induct) haftmann@26041: (simp_all add: fold1_insert mult_assoc) haftmann@26041: haftmann@26041: lemma fold1_in: haftmann@26041: assumes A: "finite (A)" "A \ {}" and elem: "\x y. x * y \ {x,y}" haftmann@26041: shows "fold1 times 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: haftmann@26041: end haftmann@26041: haftmann@26041: lemma (in ab_semigroup_idem_mult) fold1_Un2: nipkow@15497: assumes A: "finite A" "A \ {}" haftmann@26041: shows "finite B \ B \ {} \ haftmann@26041: fold1 times (A Un B) = fold1 times A * fold1 times B" nipkow@15497: using A haftmann@26041: proof(induct rule:finite_ne_induct) nipkow@15497: case singleton thus ?case by simp nipkow@15484: next haftmann@26041: case insert thus ?case by (simp add: mult_assoc) nipkow@18423: qed nipkow@18423: nipkow@18423: haftmann@22917: subsubsection {* Fold1 in lattices with @{const inf} and @{const sup} *} haftmann@22917: haftmann@22917: text{* haftmann@22917: As an application of @{text fold1} we define infimum haftmann@22917: and supremum in (not necessarily complete!) lattices haftmann@22917: over (non-empty) sets by means of @{text fold1}. haftmann@22917: *} haftmann@22917: haftmann@26041: context lower_semilattice haftmann@26041: begin haftmann@26041: haftmann@26041: lemma ab_semigroup_idem_mult_inf: haftmann@26041: "ab_semigroup_idem_mult inf" haftmann@26041: apply unfold_locales haftmann@26041: apply (rule inf_assoc) haftmann@26041: apply (rule inf_commute) haftmann@26041: apply (rule inf_idem) haftmann@26041: done haftmann@26041: haftmann@26041: lemma below_fold1_iff: haftmann@26041: assumes "finite A" "A \ {}" haftmann@26041: shows "x \ fold1 inf A \ (\a\A. x \ a)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [inf] haftmann@26041: by (rule ab_semigroup_idem_mult_inf) haftmann@26041: show ?thesis using assms by (induct rule: finite_ne_induct) simp_all haftmann@26041: qed haftmann@26041: haftmann@26041: lemma fold1_belowI: haftmann@26041: assumes "finite A" "A \ {}" haftmann@26041: and "a \ A" haftmann@26041: shows "fold1 inf A \ a" haftmann@26041: using assms proof (induct rule: finite_ne_induct) haftmann@26041: case singleton thus ?case by simp haftmann@26041: next haftmann@26041: invoke ab_semigroup_idem_mult [inf] haftmann@26041: by (rule ab_semigroup_idem_mult_inf) haftmann@26041: case (insert x F) haftmann@26041: from insert(5) have "a = x \ a \ F" by simp haftmann@26041: thus ?case haftmann@26041: proof haftmann@26041: assume "a = x" thus ?thesis using insert haftmann@26041: by (simp add: mult_ac_idem) haftmann@26041: next haftmann@26041: assume "a \ F" haftmann@26041: hence bel: "fold1 inf F \ a" by (rule insert) haftmann@26041: have "inf (fold1 inf (insert x F)) a = inf x (inf (fold1 inf F) a)" haftmann@26041: using insert by (simp add: mult_ac_idem) haftmann@26041: also have "inf (fold1 inf F) a = fold1 inf F" haftmann@26041: using bel by (auto intro: antisym) haftmann@26041: also have "inf x \ = fold1 inf (insert x F)" haftmann@26041: using insert by (simp add: mult_ac_idem) haftmann@26041: finally have aux: "inf (fold1 inf (insert x F)) a = fold1 inf (insert x F)" . haftmann@26041: moreover have "inf (fold1 inf (insert x F)) a \ a" by simp haftmann@26041: ultimately show ?thesis by simp haftmann@26041: qed haftmann@26041: qed haftmann@26041: haftmann@26041: end haftmann@26041: haftmann@26041: lemma (in upper_semilattice) ab_semigroup_idem_mult_sup: haftmann@26041: "ab_semigroup_idem_mult sup" haftmann@26041: by (rule lower_semilattice.ab_semigroup_idem_mult_inf) haftmann@26041: (rule dual_lattice) nipkow@15500: haftmann@24342: context lattice haftmann@22917: begin haftmann@22917: haftmann@22917: definition haftmann@24342: Inf_fin :: "'a set \ 'a" ("\\<^bsub>fin\<^esub>_" [900] 900) haftmann@22917: where haftmann@25062: "Inf_fin = fold1 inf" haftmann@22917: haftmann@22917: definition haftmann@24342: Sup_fin :: "'a set \ 'a" ("\\<^bsub>fin\<^esub>_" [900] 900) haftmann@22917: where haftmann@25062: "Sup_fin = fold1 sup" haftmann@25062: haftmann@25062: lemma Inf_le_Sup [simp]: "\ finite A; A \ {} \ \ \\<^bsub>fin\<^esub>A \ \\<^bsub>fin\<^esub>A" haftmann@24342: apply(unfold Sup_fin_def Inf_fin_def) nipkow@15500: apply(subgoal_tac "EX a. a:A") nipkow@15500: prefer 2 apply blast nipkow@15500: apply(erule exE) haftmann@22388: apply(rule order_trans) haftmann@26041: apply(erule (2) fold1_belowI) haftmann@26041: apply(erule (2) lower_semilattice.fold1_belowI [OF dual_lattice]) nipkow@15500: done nipkow@15500: haftmann@24342: lemma sup_Inf_absorb [simp]: haftmann@25062: "\ finite A; A \ {}; a \ A \ \ (sup a (\\<^bsub>fin\<^esub>A)) = a" nipkow@15512: apply(subst sup_commute) haftmann@26041: apply(simp add: Inf_fin_def sup_absorb2 fold1_belowI) nipkow@15504: done nipkow@15504: haftmann@24342: lemma inf_Sup_absorb [simp]: haftmann@25062: "\ finite A; A \ {}; a \ A \ \ (inf a (\\<^bsub>fin\<^esub>A)) = a" haftmann@26041: by (simp add: Sup_fin_def inf_absorb1 haftmann@26041: lower_semilattice.fold1_belowI [OF dual_lattice]) haftmann@24342: haftmann@24342: end haftmann@24342: haftmann@24342: context distrib_lattice haftmann@24342: begin haftmann@24342: haftmann@24342: lemma sup_Inf1_distrib: haftmann@26041: assumes "finite A" haftmann@26041: and "A \ {}" haftmann@26041: shows "sup x (\\<^bsub>fin\<^esub>A) = \\<^bsub>fin\<^esub>{sup x a|a. a \ A}" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [inf] haftmann@26041: by (rule ab_semigroup_idem_mult_inf) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Inf_fin_def image_def haftmann@26041: hom_fold1_commute [where h="sup x", OF sup_inf_distrib1]) haftmann@26041: (rule arg_cong, blast) haftmann@26041: qed nipkow@18423: haftmann@24342: lemma sup_Inf2_distrib: haftmann@24342: assumes A: "finite A" "A \ {}" and B: "finite B" "B \ {}" haftmann@25062: shows "sup (\\<^bsub>fin\<^esub>A) (\\<^bsub>fin\<^esub>B) = \\<^bsub>fin\<^esub>{sup a b|a b. a \ A \ b \ B}" haftmann@24342: using A proof (induct rule: finite_ne_induct) nipkow@15500: case singleton thus ?case haftmann@24342: by (simp add: sup_Inf1_distrib [OF B] fold1_singleton_def [OF Inf_fin_def]) nipkow@15500: next haftmann@26041: invoke ab_semigroup_idem_mult [inf] haftmann@26041: by (rule ab_semigroup_idem_mult_inf) nipkow@15500: case (insert x A) haftmann@25062: have finB: "finite {sup x b |b. b \ B}" haftmann@25062: by(rule finite_surj[where f = "sup x", OF B(1)], auto) haftmann@25062: have finAB: "finite {sup a b |a b. a \ A \ b \ B}" nipkow@15500: proof - haftmann@25062: have "{sup a b |a b. a \ A \ b \ B} = (UN a:A. UN b:B. {sup a b})" nipkow@15500: by blast berghofe@15517: thus ?thesis by(simp add: insert(1) B(1)) nipkow@15500: qed haftmann@25062: have ne: "{sup a b |a b. a \ A \ b \ B} \ {}" using insert B by blast haftmann@25062: have "sup (\\<^bsub>fin\<^esub>(insert x A)) (\\<^bsub>fin\<^esub>B) = sup (inf x (\\<^bsub>fin\<^esub>A)) (\\<^bsub>fin\<^esub>B)" haftmann@26041: using insert by (simp add: fold1_insert_idem_def [OF Inf_fin_def]) haftmann@25062: also have "\ = inf (sup x (\\<^bsub>fin\<^esub>B)) (sup (\\<^bsub>fin\<^esub>A) (\\<^bsub>fin\<^esub>B))" by(rule sup_inf_distrib2) haftmann@25062: also have "\ = inf (\\<^bsub>fin\<^esub>{sup x b|b. b \ B}) (\\<^bsub>fin\<^esub>{sup a b|a b. a \ A \ b \ B})" nipkow@15500: using insert by(simp add:sup_Inf1_distrib[OF B]) haftmann@25062: also have "\ = \\<^bsub>fin\<^esub>({sup x b |b. b \ B} \ {sup a b |a b. a \ A \ b \ B})" haftmann@24342: (is "_ = \\<^bsub>fin\<^esub>?M") nipkow@15500: using B insert haftmann@26041: by (simp add: Inf_fin_def fold1_Un2 [OF finB _ finAB ne]) haftmann@25062: also have "?M = {sup a b |a b. a \ insert x A \ b \ B}" nipkow@15500: by blast nipkow@15500: finally show ?case . nipkow@15500: qed nipkow@15500: haftmann@24342: lemma inf_Sup1_distrib: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "inf x (\\<^bsub>fin\<^esub>A) = \\<^bsub>fin\<^esub>{inf x a|a. a \ A}" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [sup] haftmann@26041: by (rule ab_semigroup_idem_mult_sup) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Sup_fin_def image_def hom_fold1_commute [where h="inf x", OF inf_sup_distrib1]) haftmann@26041: (rule arg_cong, blast) haftmann@26041: qed nipkow@18423: haftmann@24342: lemma inf_Sup2_distrib: haftmann@24342: assumes A: "finite A" "A \ {}" and B: "finite B" "B \ {}" haftmann@25062: shows "inf (\\<^bsub>fin\<^esub>A) (\\<^bsub>fin\<^esub>B) = \\<^bsub>fin\<^esub>{inf a b|a b. a \ A \ b \ B}" haftmann@24342: using A proof (induct rule: finite_ne_induct) nipkow@18423: case singleton thus ?case haftmann@24342: by(simp add: inf_Sup1_distrib [OF B] fold1_singleton_def [OF Sup_fin_def]) nipkow@18423: next nipkow@18423: case (insert x A) haftmann@25062: have finB: "finite {inf x b |b. b \ B}" haftmann@25062: by(rule finite_surj[where f = "%b. inf x b", OF B(1)], auto) haftmann@25062: have finAB: "finite {inf a b |a b. a \ A \ b \ B}" nipkow@18423: proof - haftmann@25062: have "{inf a b |a b. a \ A \ b \ B} = (UN a:A. UN b:B. {inf a b})" nipkow@18423: by blast nipkow@18423: thus ?thesis by(simp add: insert(1) B(1)) nipkow@18423: qed haftmann@25062: have ne: "{inf a b |a b. a \ A \ b \ B} \ {}" using insert B by blast haftmann@26041: invoke ab_semigroup_idem_mult [sup] haftmann@26041: by (rule ab_semigroup_idem_mult_sup) haftmann@25062: have "inf (\\<^bsub>fin\<^esub>(insert x A)) (\\<^bsub>fin\<^esub>B) = inf (sup x (\\<^bsub>fin\<^esub>A)) (\\<^bsub>fin\<^esub>B)" haftmann@26041: using insert by (simp add: fold1_insert_idem_def [OF Sup_fin_def]) haftmann@25062: also have "\ = sup (inf x (\\<^bsub>fin\<^esub>B)) (inf (\\<^bsub>fin\<^esub>A) (\\<^bsub>fin\<^esub>B))" by(rule inf_sup_distrib2) haftmann@25062: also have "\ = sup (\\<^bsub>fin\<^esub>{inf x b|b. b \ B}) (\\<^bsub>fin\<^esub>{inf a b|a b. a \ A \ b \ B})" nipkow@18423: using insert by(simp add:inf_Sup1_distrib[OF B]) haftmann@25062: also have "\ = \\<^bsub>fin\<^esub>({inf x b |b. b \ B} \ {inf a b |a b. a \ A \ b \ B})" haftmann@24342: (is "_ = \\<^bsub>fin\<^esub>?M") nipkow@18423: using B insert haftmann@26041: by (simp add: Sup_fin_def fold1_Un2 [OF finB _ finAB ne]) haftmann@25062: also have "?M = {inf a b |a b. a \ insert x A \ b \ B}" nipkow@18423: by blast nipkow@18423: finally show ?case . nipkow@18423: qed nipkow@18423: haftmann@24342: end haftmann@24342: haftmann@24342: context complete_lattice haftmann@24342: begin haftmann@24342: haftmann@22917: text {* haftmann@24342: Coincidence on finite sets in complete lattices: haftmann@22917: *} haftmann@22917: haftmann@24342: lemma Inf_fin_Inf: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "\\<^bsub>fin\<^esub>A = Inf A" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [inf] haftmann@26041: by (rule ab_semigroup_idem_mult_inf) haftmann@26041: from assms show ?thesis haftmann@26041: unfolding Inf_fin_def by (induct A set: finite) haftmann@26041: (simp_all add: Inf_insert_simp) haftmann@26041: qed haftmann@22917: haftmann@24342: lemma Sup_fin_Sup: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "\\<^bsub>fin\<^esub>A = Sup A" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [sup] haftmann@26041: by (rule ab_semigroup_idem_mult_sup) haftmann@26041: from assms show ?thesis haftmann@26041: unfolding Sup_fin_def by (induct A set: finite) haftmann@26041: (simp_all add: Sup_insert_simp) haftmann@26041: qed haftmann@22917: haftmann@24342: end haftmann@24342: haftmann@22917: haftmann@22917: subsubsection {* Fold1 in linear orders with @{const min} and @{const max} *} haftmann@22917: haftmann@22917: text{* haftmann@22917: As an application of @{text fold1} we define minimum haftmann@22917: and maximum in (not necessarily complete!) linear orders haftmann@22917: over (non-empty) sets by means of @{text fold1}. haftmann@22917: *} haftmann@22917: haftmann@24342: context linorder haftmann@22917: begin haftmann@22917: haftmann@26041: lemma ab_semigroup_idem_mult_min: haftmann@26041: "ab_semigroup_idem_mult min" haftmann@26041: by unfold_locales (auto simp add: min_def) haftmann@26041: haftmann@26041: lemma ab_semigroup_idem_mult_max: haftmann@26041: "ab_semigroup_idem_mult max" haftmann@26041: by unfold_locales (auto simp add: max_def) haftmann@26041: haftmann@26041: lemma min_lattice: haftmann@26041: "lower_semilattice (op \) (op <) min" haftmann@26041: by unfold_locales (auto simp add: min_def) haftmann@26041: haftmann@26041: lemma max_lattice: haftmann@26041: "lower_semilattice (op \) (op >) max" haftmann@26041: by unfold_locales (auto simp add: max_def) haftmann@26041: haftmann@26041: lemma dual_max: haftmann@26041: "ord.max (op \) = min" haftmann@26041: by (auto simp add: ord.max_def_raw min_def_raw expand_fun_eq) haftmann@26041: haftmann@26041: lemma dual_min: haftmann@26041: "ord.min (op \) = max" haftmann@26041: by (auto simp add: ord.min_def_raw max_def_raw expand_fun_eq) haftmann@26041: haftmann@26041: lemma strict_below_fold1_iff: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "x < fold1 min A \ (\a\A. x < a)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [min] haftmann@26041: by (rule ab_semigroup_idem_mult_min) haftmann@26041: from assms show ?thesis haftmann@26041: by (induct rule: finite_ne_induct) haftmann@26041: (simp_all add: fold1_insert) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma fold1_below_iff: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "fold1 min A \ x \ (\a\A. a \ x)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [min] haftmann@26041: by (rule ab_semigroup_idem_mult_min) haftmann@26041: from assms show ?thesis haftmann@26041: by (induct rule: finite_ne_induct) haftmann@26041: (simp_all add: fold1_insert min_le_iff_disj) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma fold1_strict_below_iff: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "fold1 min A < x \ (\a\A. a < x)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [min] haftmann@26041: by (rule ab_semigroup_idem_mult_min) haftmann@26041: from assms show ?thesis haftmann@26041: by (induct rule: finite_ne_induct) haftmann@26041: (simp_all add: fold1_insert min_less_iff_disj) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma fold1_antimono: haftmann@26041: assumes "A \ {}" and "A \ B" and "finite B" haftmann@26041: shows "fold1 min B \ fold1 min A" haftmann@26041: proof cases haftmann@26041: assume "A = B" thus ?thesis by simp haftmann@26041: next haftmann@26041: invoke ab_semigroup_idem_mult [min] haftmann@26041: by (rule ab_semigroup_idem_mult_min) haftmann@26041: assume "A \ B" haftmann@26041: have B: "B = A \ (B-A)" using `A \ B` by blast haftmann@26041: have "fold1 min B = fold1 min (A \ (B-A))" by(subst B)(rule refl) haftmann@26041: also have "\ = min (fold1 min A) (fold1 min (B-A))" haftmann@26041: proof - haftmann@26041: have "finite A" by(rule finite_subset[OF `A \ B` `finite B`]) haftmann@26041: moreover have "finite(B-A)" by(rule finite_Diff[OF `finite B`]) (* by(blast intro:finite_Diff prems) fails *) haftmann@26041: moreover have "(B-A) \ {}" using prems by blast haftmann@26041: moreover have "A Int (B-A) = {}" using prems by blast haftmann@26041: ultimately show ?thesis using `A \ {}` by (rule_tac fold1_Un) haftmann@26041: qed haftmann@26041: also have "\ \ fold1 min A" by (simp add: min_le_iff_disj) haftmann@26041: finally show ?thesis . haftmann@26041: qed haftmann@26041: haftmann@22917: definition haftmann@22917: Min :: "'a set \ 'a" haftmann@22917: where haftmann@22917: "Min = fold1 min" haftmann@22917: haftmann@22917: definition haftmann@22917: Max :: "'a set \ 'a" haftmann@22917: where haftmann@22917: "Max = fold1 max" haftmann@22917: haftmann@22917: lemmas Min_singleton [simp] = fold1_singleton_def [OF Min_def] haftmann@22917: lemmas Max_singleton [simp] = fold1_singleton_def [OF Max_def] haftmann@26041: haftmann@26041: lemma Min_insert [simp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "Min (insert x A) = min x (Min A)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [min] haftmann@26041: by (rule ab_semigroup_idem_mult_min) haftmann@26041: from assms show ?thesis by (rule fold1_insert_idem_def [OF Min_def]) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Max_insert [simp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "Max (insert x A) = max x (Max A)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [max] haftmann@26041: by (rule ab_semigroup_idem_mult_max) haftmann@26041: from assms show ?thesis by (rule fold1_insert_idem_def [OF Max_def]) haftmann@26041: qed nipkow@15392: paulson@24427: lemma Min_in [simp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "Min A \ A" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [min] haftmann@26041: by (rule ab_semigroup_idem_mult_min) haftmann@26041: from assms fold1_in show ?thesis by (fastsimp simp: Min_def min_def) haftmann@26041: qed nipkow@15392: paulson@24427: lemma Max_in [simp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "Max A \ A" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [max] haftmann@26041: by (rule ab_semigroup_idem_mult_max) haftmann@26041: from assms fold1_in [of A] show ?thesis by (fastsimp simp: Max_def max_def) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Min_Un: haftmann@26041: assumes "finite A" and "A \ {}" and "finite B" and "B \ {}" haftmann@26041: shows "Min (A \ B) = min (Min A) (Min B)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [min] haftmann@26041: by (rule ab_semigroup_idem_mult_min) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Min_def fold1_Un2) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Max_Un: haftmann@26041: assumes "finite A" and "A \ {}" and "finite B" and "B \ {}" haftmann@26041: shows "Max (A \ B) = max (Max A) (Max B)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [max] haftmann@26041: by (rule ab_semigroup_idem_mult_max) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Max_def fold1_Un2) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma hom_Min_commute: haftmann@26041: assumes "\x y. h (min x y) = min (h x) (h y)" haftmann@26041: and "finite N" and "N \ {}" haftmann@26041: shows "h (Min N) = Min (h ` N)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [min] haftmann@26041: by (rule ab_semigroup_idem_mult_min) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Min_def hom_fold1_commute) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma hom_Max_commute: haftmann@26041: assumes "\x y. h (max x y) = max (h x) (h y)" haftmann@26041: and "finite N" and "N \ {}" haftmann@26041: shows "h (Max N) = Max (h ` N)" haftmann@26041: proof - haftmann@26041: invoke ab_semigroup_idem_mult [max] haftmann@26041: by (rule ab_semigroup_idem_mult_max) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Max_def hom_fold1_commute [of h]) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Min_le [simp]: haftmann@26041: assumes "finite A" and "A \ {}" and "x \ A" haftmann@26041: shows "Min A \ x" haftmann@26041: proof - haftmann@26041: invoke lower_semilattice [_ _ min] haftmann@26041: by (rule min_lattice) haftmann@26041: from assms show ?thesis by (simp add: Min_def fold1_belowI) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Max_ge [simp]: haftmann@26041: assumes "finite A" and "A \ {}" and "x \ A" haftmann@26041: shows "x \ Max A" haftmann@26041: proof - haftmann@26041: invoke lower_semilattice [_ _ max] haftmann@26041: by (rule max_lattice) haftmann@26041: from assms show ?thesis by (simp add: Max_def fold1_belowI) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Min_ge_iff [simp, noatp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "x \ Min A \ (\a\A. x \ a)" haftmann@26041: proof - haftmann@26041: invoke lower_semilattice [_ _ min] haftmann@26041: by (rule min_lattice) haftmann@26041: from assms show ?thesis by (simp add: Min_def below_fold1_iff) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Max_le_iff [simp, noatp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "Max A \ x \ (\a\A. a \ x)" haftmann@26041: proof - haftmann@26041: invoke lower_semilattice [_ _ max] haftmann@26041: by (rule max_lattice) haftmann@26041: from assms show ?thesis by (simp add: Max_def below_fold1_iff) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Min_gr_iff [simp, noatp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "x < Min A \ (\a\A. x < a)" haftmann@26041: proof - haftmann@26041: invoke lower_semilattice [_ _ min] haftmann@26041: by (rule min_lattice) haftmann@26041: from assms show ?thesis by (simp add: Min_def strict_below_fold1_iff) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Max_less_iff [simp, noatp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "Max A < x \ (\a\A. a < x)" haftmann@26041: proof - haftmann@26041: note Max = Max_def haftmann@26041: invoke linorder ["op \" "op >"] haftmann@26041: by (rule dual_linorder) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Max strict_below_fold1_iff [folded dual_max]) haftmann@26041: qed nipkow@18493: paulson@24286: lemma Min_le_iff [noatp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "Min A \ x \ (\a\A. a \ x)" haftmann@26041: proof - haftmann@26041: invoke lower_semilattice [_ _ min] haftmann@26041: by (rule min_lattice) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Min_def fold1_below_iff) haftmann@26041: qed nipkow@15497: paulson@24286: lemma Max_ge_iff [noatp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "x \ Max A \ (\a\A. x \ a)" haftmann@26041: proof - haftmann@26041: note Max = Max_def haftmann@26041: invoke linorder ["op \" "op >"] haftmann@26041: by (rule dual_linorder) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Max fold1_below_iff [folded dual_max]) haftmann@26041: qed haftmann@22917: paulson@24286: lemma Min_less_iff [noatp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "Min A < x \ (\a\A. a < x)" haftmann@26041: proof - haftmann@26041: invoke lower_semilattice [_ _ min] haftmann@26041: by (rule min_lattice) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Min_def fold1_strict_below_iff) haftmann@26041: qed haftmann@22917: paulson@24286: lemma Max_gr_iff [noatp]: haftmann@26041: assumes "finite A" and "A \ {}" haftmann@26041: shows "x < Max A \ (\a\A. x < a)" haftmann@26041: proof - haftmann@26041: note Max = Max_def haftmann@26041: invoke linorder ["op \" "op >"] haftmann@26041: by (rule dual_linorder) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Max fold1_strict_below_iff [folded dual_max]) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Min_antimono: haftmann@26041: assumes "M \ N" and "M \ {}" and "finite N" haftmann@26041: shows "Min N \ Min M" haftmann@26041: proof - haftmann@26041: invoke distrib_lattice ["op \" "op <" min max] haftmann@26041: by (rule distrib_lattice_min_max) haftmann@26041: from assms show ?thesis by (simp add: Min_def fold1_antimono) haftmann@26041: qed haftmann@26041: haftmann@26041: lemma Max_mono: haftmann@26041: assumes "M \ N" and "M \ {}" and "finite N" haftmann@26041: shows "Max M \ Max N" haftmann@26041: proof - haftmann@26041: note Max = Max_def haftmann@26041: invoke linorder ["op \" "op >"] haftmann@26041: by (rule dual_linorder) haftmann@26041: from assms show ?thesis haftmann@26041: by (simp add: Max fold1_antimono [folded dual_max]) haftmann@26041: qed haftmann@22917: haftmann@22917: end haftmann@22917: haftmann@24380: context ordered_ab_semigroup_add haftmann@22917: begin haftmann@22917: haftmann@22917: lemma add_Min_commute: haftmann@22917: fixes k haftmann@25062: assumes "finite N" and "N \ {}" haftmann@25062: shows "k + Min N = Min {k + m | m. m \ N}" haftmann@25062: proof - haftmann@25062: have "\x y. k + min x y = min (k + x) (k + y)" haftmann@25062: by (simp add: min_def not_le) haftmann@25062: (blast intro: antisym less_imp_le add_left_mono) haftmann@25062: with assms show ?thesis haftmann@25062: using hom_Min_commute [of "plus k" N] haftmann@25062: by simp (blast intro: arg_cong [where f = Min]) haftmann@25062: qed haftmann@22917: haftmann@22917: lemma add_Max_commute: haftmann@22917: fixes k haftmann@25062: assumes "finite N" and "N \ {}" haftmann@25062: shows "k + Max N = Max {k + m | m. m \ N}" haftmann@25062: proof - haftmann@25062: have "\x y. k + max x y = max (k + x) (k + y)" haftmann@25062: by (simp add: max_def not_le) haftmann@25062: (blast intro: antisym less_imp_le add_left_mono) haftmann@25062: with assms show ?thesis haftmann@25062: using hom_Max_commute [of "plus k" N] haftmann@25062: by simp (blast intro: arg_cong [where f = Max]) haftmann@25062: qed haftmann@22917: haftmann@22917: end haftmann@22917: haftmann@25571: end