# HG changeset patch # User Manuel Eberl # Date 1467353993 -7200 # Node ID a500677d4cecb29aa92263795dd9bcdfc718d452 # Parent bf2cf06537411435fc18e7c0fdb7d363b8fab2c8 Conditionally complete lattice of multisets diff -r bf2cf0653741 -r a500677d4cec src/HOL/Library/Multiset.thy --- a/src/HOL/Library/Multiset.thy Sun Jun 26 01:03:03 2016 +0200 +++ b/src/HOL/Library/Multiset.thy Fri Jul 01 08:19:53 2016 +0200 @@ -779,6 +779,228 @@ interpretation subset_mset: order "op \#" "op <#" by unfold_locales auto +subsubsection \Conditionally complete lattice\ + +instantiation multiset :: (type) Inf +begin + +lift_definition Inf_multiset :: "'a multiset set \ 'a multiset" is + "\A i. if A = {} then 0 else Inf ((\f. f i) ` A)" +proof - + fix A :: "('a \ nat) set" assume *: "\x. x \ A \ x \ multiset" + have "finite {i. (if A = {} then 0 else Inf ((\f. f i) ` A)) > 0}" unfolding multiset_def + proof (cases "A = {}") + case False + then obtain f where "f \ A" by blast + hence "{i. Inf ((\f. f i) ` A) > 0} \ {i. f i > 0}" + by (auto intro: less_le_trans[OF _ cInf_lower]) + moreover from \f \ A\ * have "finite \" by (simp add: multiset_def) + ultimately have "finite {i. Inf ((\f. f i) ` A) > 0}" by (rule finite_subset) + with False show ?thesis by simp + qed simp_all + thus "(\i. if A = {} then 0 else INF f:A. f i) \ multiset" by (simp add: multiset_def) +qed + +instance .. + +end + +lemma Inf_multiset_empty: "Inf {} = {#}" + by transfer simp_all + +lemma count_Inf_multiset_nonempty: "A \ {} \ count (Inf A) x = Inf ((\X. count X x) ` A)" + by transfer simp_all + + +instantiation multiset :: (type) Sup +begin + +lift_definition Sup_multiset :: "'a multiset set \ 'a multiset" is + "\A i. if A \ {} \ (\i. bdd_above ((\f. f i) ` A)) \ finite (\f\A. {i. f i > 0}) then + Sup ((\f. f i) ` A) else 0" +proof - + fix A :: "('a \ nat) set" assume *: "\x. x \ A \ x \ multiset" + show "(\i. if A \ {} \ (\i. bdd_above ((\f. f i) ` A)) \ finite (\f\A. {i. 0 < f i}) + then SUP f:A. f i else 0) \ multiset" + proof (cases "A \ {} \ (\i. bdd_above ((\f. f i) ` A)) \ finite (\f\A. {i. 0 < f i})") + case True + have "{i. Sup ((\f. f i) ` A) > 0} \ (\f\A. {i. 0 < f i})" + proof safe + fix i assume pos: "(SUP f:A. f i) > 0" + show "i \ (\f\A. {i. 0 < f i})" + proof (rule ccontr) + assume "i \ (\f\A. {i. 0 < f i})" + hence "\f\A. f i \ 0" by auto + with True have "(SUP f:A. f i) \ 0" + by (intro cSup_least) auto + with pos show False by simp + qed + qed + moreover from True have "finite \" by blast + ultimately have "finite {i. Sup ((\f. f i) ` A) > 0}" by (rule finite_subset) + with True show ?thesis by (simp add: multiset_def) + qed (simp_all add: multiset_def) +qed + +instance .. + +end + +lemma Sup_multiset_empty: "Sup {} = {#}" + by transfer simp_all + +lemma bdd_below_multiset [simp]: "subset_mset.bdd_below A" + by (intro subset_mset.bdd_belowI[of _ "{#}"]) simp_all + +lemma bdd_above_multiset_imp_bdd_above_count: + assumes "subset_mset.bdd_above (A :: 'a multiset set)" + shows "bdd_above ((\X. count X x) ` A)" +proof - + from assms obtain Y where Y: "\X\A. X \# Y" + by (auto simp: subset_mset.bdd_above_def) + hence "count X x \ count Y x" if "X \ A" for X + using that by (auto intro: mset_subset_eq_count) + thus ?thesis by (intro bdd_aboveI[of _ "count Y x"]) auto +qed + +lemma bdd_above_multiset_imp_finite_support: + assumes "A \ {}" "subset_mset.bdd_above (A :: 'a multiset set)" + shows "finite (\X\A. {x. count X x > 0})" +proof - + from assms obtain Y where Y: "\X\A. X \# Y" + by (auto simp: subset_mset.bdd_above_def) + hence "count X x \ count Y x" if "X \ A" for X x + using that by (auto intro: mset_subset_eq_count) + hence "(\X\A. {x. count X x > 0}) \ {x. count Y x > 0}" + by safe (erule less_le_trans) + moreover have "finite \" by simp + ultimately show ?thesis by (rule finite_subset) +qed + +lemma count_Sup_multiset_nonempty: + assumes "A \ {}" "subset_mset.bdd_above A" + shows "count (Sup A) x = (SUP X:A. count X x)" +proof - + from bdd_above_multiset_imp_bdd_above_count[OF assms(2)] + have "\x. bdd_above ((\X. count X x) ` A)" .. + moreover from bdd_above_multiset_imp_finite_support[OF assms] + have "finite (\X\A. {x. count X x > 0})" . + ultimately show ?thesis using assms(1) by transfer simp +qed + + +interpretation subset_mset: conditionally_complete_lattice Inf Sup "op #\" "op \#" "op \#" "op #\" +proof + fix X :: "'a multiset" and A + assume "X \ A" + show "Inf A \# X" + proof (rule mset_subset_eqI) + fix x + from \X \ A\ have "A \ {}" by auto + hence "count (Inf A) x = (INF X:A. count X x)" + by (simp add: count_Inf_multiset_nonempty) + also from \X \ A\ have "\ \ count X x" + by (intro cInf_lower) simp_all + finally show "count (Inf A) x \ count X x" . + qed +next + fix X :: "'a multiset" and A + assume nonempty: "A \ {}" and le: "\Y. Y \ A \ X \# Y" + show "X \# Inf A" + proof (rule mset_subset_eqI) + fix x + from nonempty have "count X x \ (INF X:A. count X x)" + by (intro cInf_greatest) (auto intro: mset_subset_eq_count le) + also from nonempty have "\ = count (Inf A) x" by (simp add: count_Inf_multiset_nonempty) + finally show "count X x \ count (Inf A) x" . + qed +next + fix X :: "'a multiset" and A + assume X: "X \ A" and bdd: "subset_mset.bdd_above A" + show "X \# Sup A" + proof (rule mset_subset_eqI) + fix x + from X have "A \ {}" by auto + have "count X x \ (SUP X:A. count X x)" + by (intro cSUP_upper X bdd_above_multiset_imp_bdd_above_count bdd) + also from count_Sup_multiset_nonempty[OF \A \ {}\ bdd] + have "(SUP X:A. count X x) = count (Sup A) x" by simp + finally show "count X x \ count (Sup A) x" . + qed +next + fix X :: "'a multiset" and A + assume nonempty: "A \ {}" and ge: "\Y. Y \ A \ Y \# X" + from ge have bdd: "subset_mset.bdd_above A" by (rule subset_mset.bdd_aboveI[of _ X]) + show "Sup A \# X" + proof (rule mset_subset_eqI) + fix x + from count_Sup_multiset_nonempty[OF \A \ {}\ bdd] + have "count (Sup A) x = (SUP X:A. count X x)" . + also from nonempty have "\ \ count X x" + by (intro cSup_least) (auto intro: mset_subset_eq_count ge) + finally show "count (Sup A) x \ count X x" . + qed +qed + +lemma set_mset_Inf: + assumes "A \ {}" + shows "set_mset (Inf A) = (\X\A. set_mset X)" +proof safe + fix x X assume "x \# Inf A" "X \ A" + hence nonempty: "A \ {}" by (auto simp: Inf_multiset_empty) + from \x \# Inf A\ have "{#x#} \# Inf A" by auto + also from \X \ A\ have "\ \# X" by (rule subset_mset.cInf_lower) simp_all + finally show "x \# X" by simp +next + fix x assume x: "x \ (\X\A. set_mset X)" + hence "{#x#} \# X" if "X \ A" for X using that by auto + from assms and this have "{#x#} \# Inf A" by (rule subset_mset.cInf_greatest) + thus "x \# Inf A" by simp +qed + +lemma in_Inf_multiset_iff: + assumes "A \ {}" + shows "x \# Inf A \ (\X\A. x \# X)" +proof - + from assms have "set_mset (Inf A) = (\X\A. set_mset X)" by (rule set_mset_Inf) + also have "x \ \ \ (\X\A. x \# X)" by simp + finally show ?thesis . +qed + +lemma set_mset_Sup: + assumes "subset_mset.bdd_above A" + shows "set_mset (Sup A) = (\X\A. set_mset X)" +proof safe + fix x assume "x \# Sup A" + hence nonempty: "A \ {}" by (auto simp: Sup_multiset_empty) + show "x \ (\X\A. set_mset X)" + proof (rule ccontr) + assume x: "x \ (\X\A. set_mset X)" + have "count X x \ count (Sup A) x" if "X \ A" for X x + using that by (intro mset_subset_eq_count subset_mset.cSup_upper assms) + with x have "X \# Sup A - {#x#}" if "X \ A" for X + using that by (auto simp: subseteq_mset_def algebra_simps not_in_iff) + hence "Sup A \# Sup A - {#x#}" by (intro subset_mset.cSup_least nonempty) + with \x \# Sup A\ show False + by (auto simp: subseteq_mset_def count_greater_zero_iff [symmetric] + simp del: count_greater_zero_iff dest!: spec[of _ x]) + qed +next + fix x X assume "x \ set_mset X" "X \ A" + hence "{#x#} \# X" by auto + also have "X \# Sup A" by (intro subset_mset.cSup_upper \X \ A\ assms) + finally show "x \ set_mset (Sup A)" by simp +qed + +lemma in_Sup_multiset_iff: + assumes "subset_mset.bdd_above A" + shows "x \# Sup A \ (\X\A. x \# X)" +proof - + from assms have "set_mset (Sup A) = (\X\A. set_mset X)" by (rule set_mset_Sup) + also have "x \ \ \ (\X\A. x \# X)" by simp + finally show ?thesis . +qed + subsubsection \Filter (with comprehension syntax)\