226 |
226 |
227 lemma filter_Bag [code]: "filter_mset P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)" |
227 lemma filter_Bag [code]: "filter_mset P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)" |
228 by (rule multiset_eqI) (simp add: count_of_filter DAList.filter.rep_eq) |
228 by (rule multiset_eqI) (simp add: count_of_filter DAList.filter.rep_eq) |
229 |
229 |
230 |
230 |
231 lemma mset_eq [code]: "HOL.equal (m1::'a::equal multiset) m2 \<longleftrightarrow> m1 \<le># m2 \<and> m2 \<le># m1" |
231 lemma mset_eq [code]: "HOL.equal (m1::'a::equal multiset) m2 \<longleftrightarrow> m1 \<subseteq># m2 \<and> m2 \<subseteq># m1" |
232 by (metis equal_multiset_def subset_mset.eq_iff) |
232 by (metis equal_multiset_def subset_mset.eq_iff) |
233 |
233 |
234 text \<open>By default the code for \<open><\<close> is @{prop"xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> xs = ys"}. |
234 text \<open>By default the code for \<open><\<close> is @{prop"xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> xs = ys"}. |
235 With equality implemented by \<open>\<le>\<close>, this leads to three calls of \<open>\<le>\<close>. |
235 With equality implemented by \<open>\<le>\<close>, this leads to three calls of \<open>\<le>\<close>. |
236 Here is a more efficient version:\<close> |
236 Here is a more efficient version:\<close> |
237 lemma mset_less[code]: "xs <# (ys :: 'a multiset) \<longleftrightarrow> xs \<le># ys \<and> \<not> ys \<le># xs" |
237 lemma mset_less[code]: "xs \<subset># (ys :: 'a multiset) \<longleftrightarrow> xs \<subseteq># ys \<and> \<not> ys \<subseteq># xs" |
238 by (rule subset_mset.less_le_not_le) |
238 by (rule subset_mset.less_le_not_le) |
239 |
239 |
240 lemma mset_less_eq_Bag0: |
240 lemma mset_less_eq_Bag0: |
241 "Bag xs \<le># A \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). count_of (DAList.impl_of xs) x \<le> count A x)" |
241 "Bag xs \<subseteq># A \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). count_of (DAList.impl_of xs) x \<le> count A x)" |
242 (is "?lhs \<longleftrightarrow> ?rhs") |
242 (is "?lhs \<longleftrightarrow> ?rhs") |
243 proof |
243 proof |
244 assume ?lhs |
244 assume ?lhs |
245 then show ?rhs by (auto simp add: subseteq_mset_def) |
245 then show ?rhs by (auto simp add: subseteq_mset_def) |
246 next |
246 next |
253 then show "count (Bag xs) x \<le> count A x" by (simp add: subset_mset_def) |
253 then show "count (Bag xs) x \<le> count A x" by (simp add: subset_mset_def) |
254 qed |
254 qed |
255 qed |
255 qed |
256 |
256 |
257 lemma mset_less_eq_Bag [code]: |
257 lemma mset_less_eq_Bag [code]: |
258 "Bag xs \<le># (A :: 'a multiset) \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). n \<le> count A x)" |
258 "Bag xs \<subseteq># (A :: 'a multiset) \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). n \<le> count A x)" |
259 proof - |
259 proof - |
260 { |
260 { |
261 fix x n |
261 fix x n |
262 assume "(x,n) \<in> set (DAList.impl_of xs)" |
262 assume "(x,n) \<in> set (DAList.impl_of xs)" |
263 then have "count_of (DAList.impl_of xs) x = n" |
263 then have "count_of (DAList.impl_of xs) x = n" |