1 (* Title: HOL/Library/Multiset.thy
2 Author: Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
5 header {* (Finite) multisets *}
11 subsection {* The type of multisets *}
13 definition "multiset = {f :: 'a => nat. finite {x. f x > 0}}"
15 typedef 'a multiset = "multiset :: ('a => nat) set"
16 morphisms count Abs_multiset
17 unfolding multiset_def
19 show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp
22 setup_lifting type_definition_multiset
24 abbreviation Melem :: "'a => 'a multiset => bool" ("(_/ :# _)" [50, 51] 50) where
25 "a :# M == 0 < count M a"
28 Melem (infix "\<in>#" 50)
30 lemma multiset_eq_iff:
31 "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
32 by (simp only: count_inject [symmetric] fun_eq_iff)
35 "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
36 using multiset_eq_iff by auto
39 \medskip Preservation of the representing set @{term multiset}.
42 lemma const0_in_multiset:
43 "(\<lambda>a. 0) \<in> multiset"
44 by (simp add: multiset_def)
46 lemma only1_in_multiset:
47 "(\<lambda>b. if b = a then n else 0) \<in> multiset"
48 by (simp add: multiset_def)
50 lemma union_preserves_multiset:
51 "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
52 by (simp add: multiset_def)
54 lemma diff_preserves_multiset:
55 assumes "M \<in> multiset"
56 shows "(\<lambda>a. M a - N a) \<in> multiset"
58 have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
60 with assms show ?thesis
61 by (auto simp add: multiset_def intro: finite_subset)
64 lemma filter_preserves_multiset:
65 assumes "M \<in> multiset"
66 shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
68 have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
70 with assms show ?thesis
71 by (auto simp add: multiset_def intro: finite_subset)
74 lemmas in_multiset = const0_in_multiset only1_in_multiset
75 union_preserves_multiset diff_preserves_multiset filter_preserves_multiset
78 subsection {* Representing multisets *}
80 text {* Multiset enumeration *}
82 instantiation multiset :: (type) cancel_comm_monoid_add
85 lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
86 by (rule const0_in_multiset)
88 abbreviation Mempty :: "'a multiset" ("{#}") where
91 lift_definition plus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
92 by (rule union_preserves_multiset)
95 by default (transfer, simp add: fun_eq_iff)+
99 lift_definition single :: "'a => 'a multiset" is "\<lambda>a b. if b = a then 1 else 0"
100 by (rule only1_in_multiset)
103 "_multiset" :: "args => 'a multiset" ("{#(_)#}")
105 "{#x, xs#}" == "{#x#} + {#xs#}"
106 "{#x#}" == "CONST single x"
108 lemma count_empty [simp]: "count {#} a = 0"
109 by (simp add: zero_multiset.rep_eq)
111 lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
112 by (simp add: single.rep_eq)
115 subsection {* Basic operations *}
117 subsubsection {* Union *}
119 lemma count_union [simp]: "count (M + N) a = count M a + count N a"
120 by (simp add: plus_multiset.rep_eq)
123 subsubsection {* Difference *}
125 instantiation multiset :: (type) comm_monoid_diff
128 lift_definition minus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
129 by (rule diff_preserves_multiset)
132 by default (transfer, simp add: fun_eq_iff)+
136 lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
137 by (simp add: minus_multiset.rep_eq)
139 lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
140 by rule (fact Groups.diff_zero, fact Groups.zero_diff)
142 lemma diff_cancel[simp]: "A - A = {#}"
143 by (fact Groups.diff_cancel)
145 lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
146 by (fact add_diff_cancel_right')
148 lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
149 by (fact add_diff_cancel_left')
151 lemma diff_right_commute:
152 "(M::'a multiset) - N - Q = M - Q - N"
153 by (fact diff_right_commute)
156 "(M::'a multiset) - (N + Q) = M - N - Q"
157 by (rule sym) (fact diff_diff_add)
160 "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
161 by (clarsimp simp: multiset_eq_iff)
163 lemma insert_DiffM2 [simp]:
164 "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
165 by (clarsimp simp: multiset_eq_iff)
167 lemma diff_union_swap:
168 "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
169 by (auto simp add: multiset_eq_iff)
171 lemma diff_union_single_conv:
172 "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
173 by (simp add: multiset_eq_iff)
176 subsubsection {* Equality of multisets *}
178 lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
179 by (simp add: multiset_eq_iff)
181 lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
182 by (auto simp add: multiset_eq_iff)
184 lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
185 by (auto simp add: multiset_eq_iff)
187 lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
188 by (auto simp add: multiset_eq_iff)
190 lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
191 by (auto simp add: multiset_eq_iff)
193 lemma diff_single_trivial:
194 "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
195 by (auto simp add: multiset_eq_iff)
197 lemma diff_single_eq_union:
198 "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
201 lemma union_single_eq_diff:
202 "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
205 lemma union_single_eq_member:
206 "M + {#x#} = N \<Longrightarrow> x \<in># N"
209 lemma union_is_single:
210 "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}" (is "?lhs = ?rhs")
212 assume ?rhs then show ?lhs by auto
214 assume ?lhs then show ?rhs
215 by (simp add: multiset_eq_iff split:if_splits) (metis add_is_1)
218 lemma single_is_union:
219 "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
220 by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
222 lemma add_eq_conv_diff:
223 "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}" (is "?lhs = ?rhs")
224 (* shorter: by (simp add: multiset_eq_iff) fastforce *)
226 assume ?rhs then show ?lhs
227 by (auto simp add: add_assoc add_commute [of "{#b#}"])
228 (drule sym, simp add: add_assoc [symmetric])
232 proof (cases "a = b")
233 case True with `?lhs` show ?thesis by simp
236 from `?lhs` have "a \<in># N + {#b#}" by (rule union_single_eq_member)
237 with False have "a \<in># N" by auto
238 moreover from `?lhs` have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
240 ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
244 lemma insert_noteq_member:
245 assumes BC: "B + {#b#} = C + {#c#}"
246 and bnotc: "b \<noteq> c"
249 have "c \<in># C + {#c#}" by simp
250 have nc: "\<not> c \<in># {#b#}" using bnotc by simp
251 then have "c \<in># B + {#b#}" using BC by simp
252 then show "c \<in># B" using nc by simp
255 lemma add_eq_conv_ex:
256 "(M + {#a#} = N + {#b#}) =
257 (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
258 by (auto simp add: add_eq_conv_diff)
260 lemma multi_member_split:
261 "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
262 by (rule_tac x = "M - {#x#}" in exI, simp)
265 subsubsection {* Pointwise ordering induced by count *}
267 instantiation multiset :: (type) ordered_ab_semigroup_add_imp_le
270 lift_definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" is "\<lambda> A B. (\<forall>a. A a \<le> B a)"
272 lemmas mset_le_def = less_eq_multiset_def
274 definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
275 mset_less_def: "(A::'a multiset) < B \<longleftrightarrow> A \<le> B \<and> A \<noteq> B"
278 by default (auto simp add: mset_le_def mset_less_def multiset_eq_iff intro: order_trans antisym)
283 "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le> B"
284 by (simp add: mset_le_def)
286 lemma mset_le_exists_conv:
287 "(A::'a multiset) \<le> B \<longleftrightarrow> (\<exists>C. B = A + C)"
288 apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI)
289 apply (auto intro: multiset_eq_iff [THEN iffD2])
292 instance multiset :: (type) ordered_cancel_comm_monoid_diff
293 by default (simp, fact mset_le_exists_conv)
295 lemma mset_le_mono_add_right_cancel [simp]:
296 "(A::'a multiset) + C \<le> B + C \<longleftrightarrow> A \<le> B"
297 by (fact add_le_cancel_right)
299 lemma mset_le_mono_add_left_cancel [simp]:
300 "C + (A::'a multiset) \<le> C + B \<longleftrightarrow> A \<le> B"
301 by (fact add_le_cancel_left)
303 lemma mset_le_mono_add:
304 "(A::'a multiset) \<le> B \<Longrightarrow> C \<le> D \<Longrightarrow> A + C \<le> B + D"
307 lemma mset_le_add_left [simp]:
308 "(A::'a multiset) \<le> A + B"
309 unfolding mset_le_def by auto
311 lemma mset_le_add_right [simp]:
312 "B \<le> (A::'a multiset) + B"
313 unfolding mset_le_def by auto
315 lemma mset_le_single:
316 "a :# B \<Longrightarrow> {#a#} \<le> B"
317 by (simp add: mset_le_def)
319 lemma multiset_diff_union_assoc:
320 "C \<le> B \<Longrightarrow> (A::'a multiset) + B - C = A + (B - C)"
321 by (simp add: multiset_eq_iff mset_le_def)
323 lemma mset_le_multiset_union_diff_commute:
324 "B \<le> A \<Longrightarrow> (A::'a multiset) - B + C = A + C - B"
325 by (simp add: multiset_eq_iff mset_le_def)
327 lemma diff_le_self[simp]: "(M::'a multiset) - N \<le> M"
328 by(simp add: mset_le_def)
330 lemma mset_lessD: "A < B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
331 apply (clarsimp simp: mset_le_def mset_less_def)
332 apply (erule_tac x=x in allE)
336 lemma mset_leD: "A \<le> B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
337 apply (clarsimp simp: mset_le_def mset_less_def)
338 apply (erule_tac x = x in allE)
342 lemma mset_less_insertD: "(A + {#x#} < B) \<Longrightarrow> (x \<in># B \<and> A < B)"
344 apply (simp add: mset_lessD)
345 apply (clarsimp simp: mset_le_def mset_less_def)
347 apply (erule_tac x = a in allE)
348 apply (auto split: split_if_asm)
351 lemma mset_le_insertD: "(A + {#x#} \<le> B) \<Longrightarrow> (x \<in># B \<and> A \<le> B)"
353 apply (simp add: mset_leD)
354 apply (force simp: mset_le_def mset_less_def split: split_if_asm)
357 lemma mset_less_of_empty[simp]: "A < {#} \<longleftrightarrow> False"
358 by (auto simp add: mset_less_def mset_le_def multiset_eq_iff)
360 lemma multi_psub_of_add_self[simp]: "A < A + {#x#}"
361 by (auto simp: mset_le_def mset_less_def)
363 lemma multi_psub_self[simp]: "(A::'a multiset) < A = False"
366 lemma mset_less_add_bothsides:
367 "T + {#x#} < S + {#x#} \<Longrightarrow> T < S"
368 by (fact add_less_imp_less_right)
370 lemma mset_less_empty_nonempty:
371 "{#} < S \<longleftrightarrow> S \<noteq> {#}"
372 by (auto simp: mset_le_def mset_less_def)
374 lemma mset_less_diff_self:
375 "c \<in># B \<Longrightarrow> B - {#c#} < B"
376 by (auto simp: mset_le_def mset_less_def multiset_eq_iff)
379 subsubsection {* Intersection *}
381 instantiation multiset :: (type) semilattice_inf
384 definition inf_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
385 multiset_inter_def: "inf_multiset A B = A - (A - B)"
389 have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" by arith
390 show "OFCLASS('a multiset, semilattice_inf_class)"
391 by default (auto simp add: multiset_inter_def mset_le_def aux)
396 abbreviation multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
397 "multiset_inter \<equiv> inf"
399 lemma multiset_inter_count [simp]:
400 "count (A #\<inter> B) x = min (count A x) (count B x)"
401 by (simp add: multiset_inter_def)
403 lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
404 by (rule multiset_eqI) auto
406 lemma multiset_union_diff_commute:
407 assumes "B #\<inter> C = {#}"
408 shows "A + B - C = A - C + B"
409 proof (rule multiset_eqI)
411 from assms have "min (count B x) (count C x) = 0"
412 by (auto simp add: multiset_eq_iff)
413 then have "count B x = 0 \<or> count C x = 0"
415 then show "count (A + B - C) x = count (A - C + B) x"
419 lemma empty_inter [simp]:
420 "{#} #\<inter> M = {#}"
421 by (simp add: multiset_eq_iff)
423 lemma inter_empty [simp]:
424 "M #\<inter> {#} = {#}"
425 by (simp add: multiset_eq_iff)
427 lemma inter_add_left1:
428 "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
429 by (simp add: multiset_eq_iff)
431 lemma inter_add_left2:
432 "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
433 by (simp add: multiset_eq_iff)
435 lemma inter_add_right1:
436 "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
437 by (simp add: multiset_eq_iff)
439 lemma inter_add_right2:
440 "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
441 by (simp add: multiset_eq_iff)
444 subsubsection {* Bounded union *}
446 instantiation multiset :: (type) semilattice_sup
449 definition sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
450 "sup_multiset A B = A + (B - A)"
454 have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" by arith
455 show "OFCLASS('a multiset, semilattice_sup_class)"
456 by default (auto simp add: sup_multiset_def mset_le_def aux)
461 abbreviation sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<union>" 70) where
462 "sup_multiset \<equiv> sup"
464 lemma sup_multiset_count [simp]:
465 "count (A #\<union> B) x = max (count A x) (count B x)"
466 by (simp add: sup_multiset_def)
468 lemma empty_sup [simp]:
469 "{#} #\<union> M = M"
470 by (simp add: multiset_eq_iff)
472 lemma sup_empty [simp]:
473 "M #\<union> {#} = M"
474 by (simp add: multiset_eq_iff)
477 "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
478 by (simp add: multiset_eq_iff)
481 "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
482 by (simp add: multiset_eq_iff)
484 lemma sup_add_right1:
485 "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
486 by (simp add: multiset_eq_iff)
488 lemma sup_add_right2:
489 "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
490 by (simp add: multiset_eq_iff)
493 subsubsection {* Filter (with comprehension syntax) *}
495 text {* Multiset comprehension *}
497 lift_definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
498 by (rule filter_preserves_multiset)
500 hide_const (open) filter
502 lemma count_filter [simp]:
503 "count (Multiset.filter P M) a = (if P a then count M a else 0)"
504 by (simp add: filter.rep_eq)
506 lemma filter_empty [simp]:
507 "Multiset.filter P {#} = {#}"
508 by (rule multiset_eqI) simp
510 lemma filter_single [simp]:
511 "Multiset.filter P {#x#} = (if P x then {#x#} else {#})"
512 by (rule multiset_eqI) simp
514 lemma filter_union [simp]:
515 "Multiset.filter P (M + N) = Multiset.filter P M + Multiset.filter P N"
516 by (rule multiset_eqI) simp
518 lemma filter_diff [simp]:
519 "Multiset.filter P (M - N) = Multiset.filter P M - Multiset.filter P N"
520 by (rule multiset_eqI) simp
522 lemma filter_inter [simp]:
523 "Multiset.filter P (M #\<inter> N) = Multiset.filter P M #\<inter> Multiset.filter P N"
524 by (rule multiset_eqI) simp
527 "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset" ("(1{# _ :# _./ _#})")
529 "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset" ("(1{# _ \<in># _./ _#})")
531 "{#x \<in># M. P#}" == "CONST Multiset.filter (\<lambda>x. P) M"
534 subsubsection {* Set of elements *}
536 definition set_of :: "'a multiset => 'a set" where
537 "set_of M = {x. x :# M}"
539 lemma set_of_empty [simp]: "set_of {#} = {}"
540 by (simp add: set_of_def)
542 lemma set_of_single [simp]: "set_of {#b#} = {b}"
543 by (simp add: set_of_def)
545 lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
546 by (auto simp add: set_of_def)
548 lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
549 by (auto simp add: set_of_def multiset_eq_iff)
551 lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
552 by (auto simp add: set_of_def)
554 lemma set_of_filter [simp]: "set_of {# x:#M. P x #} = set_of M \<inter> {x. P x}"
555 by (auto simp add: set_of_def)
557 lemma finite_set_of [iff]: "finite (set_of M)"
558 using count [of M] by (simp add: multiset_def set_of_def)
560 lemma finite_Collect_mem [iff]: "finite {x. x :# M}"
561 unfolding set_of_def[symmetric] by simp
563 subsubsection {* Size *}
565 instantiation multiset :: (type) size
569 "size M = setsum (count M) (set_of M)"
575 lemma size_empty [simp]: "size {#} = 0"
576 by (simp add: size_def)
578 lemma size_single [simp]: "size {#b#} = 1"
579 by (simp add: size_def)
581 lemma setsum_count_Int:
582 "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
583 apply (induct rule: finite_induct)
585 apply (simp add: Int_insert_left set_of_def)
588 lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
589 apply (unfold size_def)
590 apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
592 apply (rule ext, simp)
593 apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
594 apply (subst Int_commute)
595 apply (simp (no_asm_simp) add: setsum_count_Int)
598 lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
599 by (auto simp add: size_def multiset_eq_iff)
601 lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
602 by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)
604 lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
605 apply (unfold size_def)
606 apply (drule setsum_SucD)
610 lemma size_eq_Suc_imp_eq_union:
611 assumes "size M = Suc n"
612 shows "\<exists>a N. M = N + {#a#}"
614 from assms obtain a where "a \<in># M"
615 by (erule size_eq_Suc_imp_elem [THEN exE])
616 then have "M = M - {#a#} + {#a#}" by simp
617 then show ?thesis by blast
621 subsection {* Induction and case splits *}
623 theorem multiset_induct [case_names empty add, induct type: multiset]:
624 assumes empty: "P {#}"
625 assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
627 proof (induct n \<equiv> "size M" arbitrary: M)
628 case 0 thus "P M" by (simp add: empty)
631 obtain N x where "M = N + {#x#}"
632 using `Suc k = size M` [symmetric]
633 using size_eq_Suc_imp_eq_union by fast
634 with Suc add show "P M" by simp
637 lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
640 lemma multiset_cases [cases type, case_names empty add]:
641 assumes em: "M = {#} \<Longrightarrow> P"
642 assumes add: "\<And>N x. M = N + {#x#} \<Longrightarrow> P"
644 using assms by (induct M) simp_all
646 lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
647 by (cases "B = {#}") (auto dest: multi_member_split)
649 lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#M. \<not> P x #}"
650 apply (subst multiset_eq_iff)
654 lemma mset_less_size: "(A::'a multiset) < B \<Longrightarrow> size A < size B"
655 proof (induct A arbitrary: B)
657 then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
658 then obtain M' x where "M = M' + {#x#}"
659 by (blast dest: multi_nonempty_split)
660 then show ?case by simp
663 have IH: "\<And>B. S < B \<Longrightarrow> size S < size B" by fact
664 have SxsubT: "S + {#x#} < T" by fact
665 then have "x \<in># T" and "S < T" by (auto dest: mset_less_insertD)
666 then obtain T' where T: "T = T' + {#x#}"
667 by (blast dest: multi_member_split)
668 then have "S < T'" using SxsubT
669 by (blast intro: mset_less_add_bothsides)
670 then have "size S < size T'" using IH by simp
671 then show ?case using T by simp
675 subsubsection {* Strong induction and subset induction for multisets *}
677 text {* Well-foundedness of proper subset operator: *}
679 text {* proper multiset subset *}
682 mset_less_rel :: "('a multiset * 'a multiset) set" where
683 "mset_less_rel = {(A,B). A < B}"
685 lemma multiset_add_sub_el_shuffle:
686 assumes "c \<in># B" and "b \<noteq> c"
687 shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
689 from `c \<in># B` obtain A where B: "B = A + {#c#}"
690 by (blast dest: multi_member_split)
691 have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
692 then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}"
693 by (simp add: add_ac)
694 then show ?thesis using B by simp
697 lemma wf_mset_less_rel: "wf mset_less_rel"
698 apply (unfold mset_less_rel_def)
699 apply (rule wf_measure [THEN wf_subset, where f1=size])
700 apply (clarsimp simp: measure_def inv_image_def mset_less_size)
703 text {* The induction rules: *}
705 lemma full_multiset_induct [case_names less]:
706 assumes ih: "\<And>B. \<forall>(A::'a multiset). A < B \<longrightarrow> P A \<Longrightarrow> P B"
708 apply (rule wf_mset_less_rel [THEN wf_induct])
709 apply (rule ih, auto simp: mset_less_rel_def)
712 lemma multi_subset_induct [consumes 2, case_names empty add]:
715 and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
724 assume P: "F \<le> A \<Longrightarrow> P F" and i: "F + {#x#} \<le> A"
727 from i show "x \<in># A" by (auto dest: mset_le_insertD)
728 from i have "F \<le> A" by (auto dest: mset_le_insertD)
735 subsection {* The fold combinator *}
737 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
739 "fold f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_of M)"
741 lemma fold_mset_empty [simp]:
743 by (simp add: fold_def)
745 context comp_fun_commute
748 lemma fold_mset_insert:
749 "fold f s (M + {#x#}) = f x (fold f s M)"
751 interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y"
752 by (fact comp_fun_commute_funpow)
753 interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
754 by (fact comp_fun_commute_funpow)
756 proof (cases "x \<in> set_of M")
758 then have *: "count (M + {#x#}) x = 1" by simp
759 from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_of M) =
760 Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_of M)"
761 by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
762 with False * show ?thesis
763 by (simp add: fold_def del: count_union)
766 def N \<equiv> "set_of M - {x}"
767 from N_def True have *: "set_of M = insert x N" "x \<notin> N" "finite N" by auto
768 then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
769 Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N"
770 by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
771 with * show ?thesis by (simp add: fold_def del: count_union) simp
775 corollary fold_mset_single [simp]:
776 "fold f s {#x#} = f x s"
778 have "fold f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
779 then show ?thesis by simp
782 lemma fold_mset_fun_left_comm:
783 "f x (fold f s M) = fold f (f x s) M"
784 by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
786 lemma fold_mset_union [simp]:
787 "fold f s (M + N) = fold f (fold f s M) N"
789 case empty then show ?case by simp
792 have "M + {#x#} + N = (M + N) + {#x#}"
793 by (simp add: add_ac)
794 with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
797 lemma fold_mset_fusion:
798 assumes "comp_fun_commute g"
799 shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold g w A) = fold f (h w) A" (is "PROP ?P")
801 interpret comp_fun_commute g by (fact assms)
802 show "PROP ?P" by (induct A) auto
808 A note on code generation: When defining some function containing a
809 subterm @{term "fold F"}, code generation is not automatic. When
810 interpreting locale @{text left_commutative} with @{text F}, the
811 would be code thms for @{const fold} become thms like
812 @{term "fold F z {#} = z"} where @{text F} is not a pattern but
813 contains defined symbols, i.e.\ is not a code thm. Hence a separate
814 constant with its own code thms needs to be introduced for @{text
815 F}. See the image operator below.
819 subsection {* Image *}
821 definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
822 "image_mset f = fold (plus o single o f) {#}"
824 lemma comp_fun_commute_mset_image:
825 "comp_fun_commute (plus o single o f)"
827 qed (simp add: add_ac fun_eq_iff)
829 lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
830 by (simp add: image_mset_def)
832 lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
834 interpret comp_fun_commute "plus o single o f"
835 by (fact comp_fun_commute_mset_image)
836 show ?thesis by (simp add: image_mset_def)
839 lemma image_mset_union [simp]:
840 "image_mset f (M + N) = image_mset f M + image_mset f N"
842 interpret comp_fun_commute "plus o single o f"
843 by (fact comp_fun_commute_mset_image)
844 show ?thesis by (induct N) (simp_all add: image_mset_def add_ac)
847 corollary image_mset_insert:
848 "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
851 lemma set_of_image_mset [simp]:
852 "set_of (image_mset f M) = image f (set_of M)"
853 by (induct M) simp_all
855 lemma size_image_mset [simp]:
856 "size (image_mset f M) = size M"
857 by (induct M) simp_all
859 lemma image_mset_is_empty_iff [simp]:
860 "image_mset f M = {#} \<longleftrightarrow> M = {#}"
864 "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
867 "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
870 "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
871 ("({#_/ | _ :# _./ _#})")
873 "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
876 This allows to write not just filters like @{term "{#x:#M. x<c#}"}
877 but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
878 "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
879 @{term "{#x+x|x:#M. x<c#}"}.
882 enriched_type image_mset: image_mset
884 fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
887 show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
888 by (induct A) simp_all
890 show "image_mset id = id"
893 show "image_mset id A = id A"
894 by (induct A) simp_all
898 declare image_mset.identity [simp]
901 subsection {* Further conversions *}
903 primrec multiset_of :: "'a list \<Rightarrow> 'a multiset" where
904 "multiset_of [] = {#}" |
905 "multiset_of (a # x) = multiset_of x + {# a #}"
907 lemma in_multiset_in_set:
908 "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
909 by (induct xs) simp_all
911 lemma count_multiset_of:
912 "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
913 by (induct xs) simp_all
915 lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
918 lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
921 lemma set_of_multiset_of[simp]: "set_of (multiset_of x) = set x"
924 lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)"
927 lemma size_multiset_of [simp]: "size (multiset_of xs) = length xs"
928 by (induct xs) simp_all
930 lemma multiset_of_append [simp]:
931 "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
932 by (induct xs arbitrary: ys) (auto simp: add_ac)
934 lemma multiset_of_filter:
935 "multiset_of (filter P xs) = {#x :# multiset_of xs. P x #}"
936 by (induct xs) simp_all
938 lemma multiset_of_rev [simp]:
939 "multiset_of (rev xs) = multiset_of xs"
940 by (induct xs) simp_all
942 lemma surj_multiset_of: "surj multiset_of"
943 apply (unfold surj_def)
945 apply (rule_tac M = y in multiset_induct)
947 apply (rule_tac x = "x # xa" in exI)
951 lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
954 lemma distinct_count_atmost_1:
955 "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))"
956 apply (induct x, simp, rule iffI, simp_all)
958 apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
959 apply (erule_tac x = a in allE, simp, clarify)
960 apply (erule_tac x = aa in allE, simp)
963 lemma multiset_of_eq_setD:
964 "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys"
965 by (rule) (auto simp add:multiset_eq_iff set_count_greater_0)
967 lemma set_eq_iff_multiset_of_eq_distinct:
968 "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
969 (set x = set y) = (multiset_of x = multiset_of y)"
970 by (auto simp: multiset_eq_iff distinct_count_atmost_1)
972 lemma set_eq_iff_multiset_of_remdups_eq:
973 "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
975 apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
976 apply (drule distinct_remdups [THEN distinct_remdups
977 [THEN set_eq_iff_multiset_of_eq_distinct [THEN iffD2]]])
981 lemma multiset_of_compl_union [simp]:
982 "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs"
983 by (induct xs) (auto simp: add_ac)
985 lemma count_multiset_of_length_filter:
986 "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
989 lemma nth_mem_multiset_of: "i < length ls \<Longrightarrow> (ls ! i) :# multiset_of ls"
990 apply (induct ls arbitrary: i)
996 lemma multiset_of_remove1[simp]:
997 "multiset_of (remove1 a xs) = multiset_of xs - {#a#}"
998 by (induct xs) (auto simp add: multiset_eq_iff)
1000 lemma multiset_of_eq_length:
1001 assumes "multiset_of xs = multiset_of ys"
1002 shows "length xs = length ys"
1003 using assms by (metis size_multiset_of)
1005 lemma multiset_of_eq_length_filter:
1006 assumes "multiset_of xs = multiset_of ys"
1007 shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
1008 using assms by (metis count_multiset_of)
1010 lemma fold_multiset_equiv:
1011 assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
1012 and equiv: "multiset_of xs = multiset_of ys"
1013 shows "List.fold f xs = List.fold f ys"
1014 using f equiv [symmetric]
1015 proof (induct xs arbitrary: ys)
1016 case Nil then show ?case by simp
1019 then have *: "set ys = set (x # xs)" by (blast dest: multiset_of_eq_setD)
1020 have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
1021 by (rule Cons.prems(1)) (simp_all add: *)
1022 moreover from * have "x \<in> set ys" by simp
1023 ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
1024 moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" by (auto intro: Cons.hyps)
1025 ultimately show ?case by simp
1028 lemma multiset_of_insort [simp]:
1029 "multiset_of (insort x xs) = multiset_of xs + {#x#}"
1030 by (induct xs) (simp_all add: ac_simps)
1032 lemma in_multiset_of:
1033 "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
1034 by (induct xs) simp_all
1036 lemma multiset_of_map:
1037 "multiset_of (map f xs) = image_mset f (multiset_of xs)"
1038 by (induct xs) simp_all
1040 definition multiset_of_set :: "'a set \<Rightarrow> 'a multiset"
1042 "multiset_of_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
1044 interpretation multiset_of_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
1046 "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set"
1048 interpret comp_fun_commute "\<lambda>x M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
1049 show "folding (\<lambda>x M. {#x#} + M)" by default (fact comp_fun_commute)
1050 from multiset_of_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set" ..
1053 lemma count_multiset_of_set [simp]:
1054 "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (multiset_of_set A) x = 1" (is "PROP ?P")
1055 "\<not> finite A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?Q")
1056 "x \<notin> A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?R")
1059 assume "x \<notin> A"
1060 have "count (multiset_of_set A) x = 0"
1061 proof (cases "finite A")
1062 case False then show ?thesis by simp
1064 case True from True `x \<notin> A` show ?thesis by (induct A) auto
1067 then show "PROP ?P" "PROP ?Q" "PROP ?R"
1068 by (auto elim!: Set.set_insert)
1069 qed -- {* TODO: maybe define @{const multiset_of_set} also in terms of @{const Abs_multiset} *}
1074 definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
1076 "sorted_list_of_multiset M = fold insort [] M"
1078 lemma sorted_list_of_multiset_empty [simp]:
1079 "sorted_list_of_multiset {#} = []"
1080 by (simp add: sorted_list_of_multiset_def)
1082 lemma sorted_list_of_multiset_singleton [simp]:
1083 "sorted_list_of_multiset {#x#} = [x]"
1085 interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
1086 show ?thesis by (simp add: sorted_list_of_multiset_def)
1089 lemma sorted_list_of_multiset_insert [simp]:
1090 "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
1092 interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
1093 show ?thesis by (simp add: sorted_list_of_multiset_def)
1098 lemma multiset_of_sorted_list_of_multiset [simp]:
1099 "multiset_of (sorted_list_of_multiset M) = M"
1100 by (induct M) simp_all
1102 lemma sorted_list_of_multiset_multiset_of [simp]:
1103 "sorted_list_of_multiset (multiset_of xs) = sort xs"
1104 by (induct xs) simp_all
1106 lemma finite_set_of_multiset_of_set:
1108 shows "set_of (multiset_of_set A) = A"
1109 using assms by (induct A) simp_all
1111 lemma infinite_set_of_multiset_of_set:
1112 assumes "\<not> finite A"
1113 shows "set_of (multiset_of_set A) = {}"
1116 lemma set_sorted_list_of_multiset [simp]:
1117 "set (sorted_list_of_multiset M) = set_of M"
1118 by (induct M) (simp_all add: set_insort)
1120 lemma sorted_list_of_multiset_of_set [simp]:
1121 "sorted_list_of_multiset (multiset_of_set A) = sorted_list_of_set A"
1122 by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
1125 subsection {* Big operators *}
1127 no_notation times (infixl "*" 70)
1128 no_notation Groups.one ("1")
1130 locale comm_monoid_mset = comm_monoid
1133 definition F :: "'a multiset \<Rightarrow> 'a"
1135 eq_fold: "F M = Multiset.fold f 1 M"
1139 by (simp add: eq_fold)
1141 lemma singleton [simp]:
1144 interpret comp_fun_commute
1145 by default (simp add: fun_eq_iff left_commute)
1146 show ?thesis by (simp add: eq_fold)
1150 "F (M + N) = F M * F N"
1152 interpret comp_fun_commute f
1153 by default (simp add: fun_eq_iff left_commute)
1154 show ?thesis by (induct N) (simp_all add: left_commute eq_fold)
1159 notation times (infixl "*" 70)
1160 notation Groups.one ("1")
1162 definition (in comm_monoid_add) msetsum :: "'a multiset \<Rightarrow> 'a"
1164 "msetsum = comm_monoid_mset.F plus 0"
1166 definition (in comm_monoid_mult) msetprod :: "'a multiset \<Rightarrow> 'a"
1168 "msetprod = comm_monoid_mset.F times 1"
1170 sublocale comm_monoid_add < msetsum!: comm_monoid_mset plus 0
1172 "comm_monoid_mset.F plus 0 = msetsum"
1174 show "comm_monoid_mset plus 0" ..
1175 from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
1178 context comm_monoid_add
1181 lemma setsum_unfold_msetsum:
1182 "setsum f A = msetsum (image_mset f (multiset_of_set A))"
1183 by (cases "finite A") (induct A rule: finite_induct, simp_all)
1185 abbreviation msetsum_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
1187 "msetsum_image f M \<equiv> msetsum (image_mset f M)"
1192 "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
1193 ("(3SUM _:#_. _)" [0, 51, 10] 10)
1196 "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
1197 ("(3\<Sum>_:#_. _)" [0, 51, 10] 10)
1199 syntax (HTML output)
1200 "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
1201 ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
1204 "SUM i :# A. b" == "CONST msetsum_image (\<lambda>i. b) A"
1206 sublocale comm_monoid_mult < msetprod!: comm_monoid_mset times 1
1208 "comm_monoid_mset.F times 1 = msetprod"
1210 show "comm_monoid_mset times 1" ..
1211 from msetprod_def show "comm_monoid_mset.F times 1 = msetprod" ..
1214 context comm_monoid_mult
1217 lemma msetprod_empty:
1219 by (fact msetprod.empty)
1221 lemma msetprod_singleton:
1222 "msetprod {#x#} = x"
1223 by (fact msetprod.singleton)
1226 "msetprod (A + B) = msetprod A * msetprod B"
1227 by (fact msetprod.union)
1229 lemma setprod_unfold_msetprod:
1230 "setprod f A = msetprod (image_mset f (multiset_of_set A))"
1231 by (cases "finite A") (induct A rule: finite_induct, simp_all)
1233 lemma msetprod_multiplicity:
1234 "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_of M)"
1235 by (simp add: Multiset.fold_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
1237 abbreviation msetprod_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
1239 "msetprod_image f M \<equiv> msetprod (image_mset f M)"
1244 "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
1245 ("(3PROD _:#_. _)" [0, 51, 10] 10)
1248 "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
1249 ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
1251 syntax (HTML output)
1252 "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
1253 ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
1256 "PROD i :# A. b" == "CONST msetprod_image (\<lambda>i. b) A"
1258 lemma (in comm_semiring_1) dvd_msetprod:
1259 assumes "x \<in># A"
1260 shows "x dvd msetprod A"
1262 from assms have "A = (A - {#x#}) + {#x#}" by simp
1263 then obtain B where "A = B + {#x#}" ..
1264 then show ?thesis by simp
1268 subsection {* Cardinality *}
1270 definition mcard :: "'a multiset \<Rightarrow> nat"
1272 "mcard = msetsum \<circ> image_mset (\<lambda>_. 1)"
1274 lemma mcard_empty [simp]:
1276 by (simp add: mcard_def)
1278 lemma mcard_singleton [simp]:
1279 "mcard {#a#} = Suc 0"
1280 by (simp add: mcard_def)
1282 lemma mcard_plus [simp]:
1283 "mcard (M + N) = mcard M + mcard N"
1284 by (simp add: mcard_def)
1286 lemma mcard_empty_iff [simp]:
1287 "mcard M = 0 \<longleftrightarrow> M = {#}"
1288 by (induct M) simp_all
1290 lemma mcard_unfold_setsum:
1291 "mcard M = setsum (count M) (set_of M)"
1293 case empty then show ?case by simp
1295 case (add M x) then show ?case
1296 by (cases "x \<in> set_of M")
1297 (simp_all del: mem_set_of_iff add: setsum.distrib setsum.delta' insert_absorb, simp)
1300 lemma size_eq_mcard:
1302 by (simp add: fun_eq_iff size_def mcard_unfold_setsum)
1304 lemma mcard_multiset_of:
1305 "mcard (multiset_of xs) = length xs"
1306 by (induct xs) simp_all
1309 subsection {* Alternative representations *}
1311 subsubsection {* Lists *}
1316 lemma multiset_of_insort [simp]:
1317 "multiset_of (insort_key k x xs) = {#x#} + multiset_of xs"
1318 by (induct xs) (simp_all add: ac_simps)
1320 lemma multiset_of_sort [simp]:
1321 "multiset_of (sort_key k xs) = multiset_of xs"
1322 by (induct xs) (simp_all add: ac_simps)
1325 This lemma shows which properties suffice to show that a function
1326 @{text "f"} with @{text "f xs = ys"} behaves like sort.
1329 lemma properties_for_sort_key:
1330 assumes "multiset_of ys = multiset_of xs"
1331 and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs"
1332 and "sorted (map f ys)"
1333 shows "sort_key f xs = ys"
1335 proof (induct xs arbitrary: ys)
1336 case Nil then show ?case by simp
1339 from Cons.prems(2) have
1340 "\<forall>k \<in> set ys. filter (\<lambda>x. f k = f x) (remove1 x ys) = filter (\<lambda>x. f k = f x) xs"
1341 by (simp add: filter_remove1)
1342 with Cons.prems have "sort_key f xs = remove1 x ys"
1343 by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
1344 moreover from Cons.prems have "x \<in> set ys"
1345 by (auto simp add: mem_set_multiset_eq intro!: ccontr)
1346 ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
1349 lemma properties_for_sort:
1350 assumes multiset: "multiset_of ys = multiset_of xs"
1352 shows "sort xs = ys"
1353 proof (rule properties_for_sort_key)
1354 from multiset show "multiset_of ys = multiset_of xs" .
1355 from `sorted ys` show "sorted (map (\<lambda>x. x) ys)" by simp
1356 from multiset have "\<And>k. length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)"
1357 by (rule multiset_of_eq_length_filter)
1358 then have "\<And>k. replicate (length (filter (\<lambda>y. k = y) ys)) k = replicate (length (filter (\<lambda>x. k = x) xs)) k"
1360 then show "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs"
1361 by (simp add: replicate_length_filter)
1364 lemma sort_key_by_quicksort:
1365 "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
1366 @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
1367 @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
1368 proof (rule properties_for_sort_key)
1369 show "multiset_of ?rhs = multiset_of ?lhs"
1370 by (rule multiset_eqI) (auto simp add: multiset_of_filter)
1372 show "sorted (map f ?rhs)"
1373 by (auto simp add: sorted_append intro: sorted_map_same)
1376 assume "l \<in> set ?rhs"
1377 let ?pivot = "f (xs ! (length xs div 2))"
1378 have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
1379 have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
1380 unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
1381 with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
1382 have "\<And>x P. P (f x) ?pivot \<and> f l = f x \<longleftrightarrow> P (f l) ?pivot \<and> f l = f x" by auto
1383 then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] =
1384 [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp
1385 note *** = this [of "op <"] this [of "op >"] this [of "op ="]
1386 show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
1387 proof (cases "f l" ?pivot rule: linorder_cases)
1389 then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
1390 with less show ?thesis
1391 by (simp add: filter_sort [symmetric] ** ***)
1393 case equal then show ?thesis
1394 by (simp add: * less_le)
1397 then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
1398 with greater show ?thesis
1399 by (simp add: filter_sort [symmetric] ** ***)
1403 lemma sort_by_quicksort:
1404 "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
1405 @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
1406 @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
1407 using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp
1409 text {* A stable parametrized quicksort *}
1411 definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
1412 "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"
1414 lemma part_code [code]:
1415 "part f pivot [] = ([], [], [])"
1416 "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
1417 if x' < pivot then (x # lts, eqs, gts)
1418 else if x' > pivot then (lts, eqs, x # gts)
1419 else (lts, x # eqs, gts))"
1420 by (auto simp add: part_def Let_def split_def)
1422 lemma sort_key_by_quicksort_code [code]:
1423 "sort_key f xs = (case xs of [] \<Rightarrow> []
1424 | [x] \<Rightarrow> xs
1425 | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
1426 | _ \<Rightarrow> (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
1427 in sort_key f lts @ eqs @ sort_key f gts))"
1429 case Nil then show ?thesis by simp
1431 case (Cons _ ys) note hyps = Cons show ?thesis
1433 case Nil with hyps show ?thesis by simp
1435 case (Cons _ zs) note hyps = hyps Cons show ?thesis
1437 case Nil with hyps show ?thesis by auto
1440 from sort_key_by_quicksort [of f xs]
1441 have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
1442 in sort_key f lts @ eqs @ sort_key f gts)"
1443 by (simp only: split_def Let_def part_def fst_conv snd_conv)
1444 with hyps Cons show ?thesis by (simp only: list.cases)
1451 hide_const (open) part
1453 lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le> multiset_of xs"
1454 by (induct xs) (auto intro: order_trans)
1456 lemma multiset_of_update:
1457 "i < length ls \<Longrightarrow> multiset_of (ls[i := v]) = multiset_of ls - {#ls ! i#} + {#v#}"
1458 proof (induct ls arbitrary: i)
1459 case Nil then show ?case by simp
1464 case 0 then show ?thesis by simp
1467 with Cons show ?thesis
1469 apply (subst add_assoc)
1470 apply (subst add_commute [of "{#v#}" "{#x#}"])
1471 apply (subst add_assoc [symmetric])
1473 apply (rule mset_le_multiset_union_diff_commute)
1474 apply (simp add: mset_le_single nth_mem_multiset_of)
1479 lemma multiset_of_swap:
1480 "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
1481 multiset_of (ls[j := ls ! i, i := ls ! j]) = multiset_of ls"
1482 by (cases "i = j") (simp_all add: multiset_of_update nth_mem_multiset_of)
1485 subsection {* The multiset order *}
1487 subsubsection {* Well-foundedness *}
1489 definition mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
1490 "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
1491 (\<forall>b. b :# K --> (b, a) \<in> r)}"
1493 definition mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
1494 "mult r = (mult1 r)\<^sup>+"
1496 lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
1497 by (simp add: mult1_def)
1499 lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
1500 (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
1501 (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
1502 (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
1503 proof (unfold mult1_def)
1504 let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
1505 let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
1506 let ?case1 = "?case1 {(N, M). ?R N M}"
1508 assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
1509 then have "\<exists>a' M0' K.
1510 M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
1511 then show "?case1 \<or> ?case2"
1512 proof (elim exE conjE)
1514 assume N: "N = M0' + K" and r: "?r K a'"
1515 assume "M0 + {#a#} = M0' + {#a'#}"
1516 then have "M0 = M0' \<and> a = a' \<or>
1517 (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
1518 by (simp only: add_eq_conv_ex)
1520 proof (elim disjE conjE exE)
1521 assume "M0 = M0'" "a = a'"
1522 with N r have "?r K a \<and> N = M0 + K" by simp
1523 then have ?case2 .. then show ?thesis ..
1526 assume "M0' = K' + {#a#}"
1527 with N have n: "N = K' + K + {#a#}" by (simp add: add_ac)
1529 assume "M0 = K' + {#a'#}"
1530 with r have "?R (K' + K) M0" by blast
1531 with n have ?case1 by simp then show ?thesis ..
1536 lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
1542 assume M0: "M0 \<in> ?W"
1543 and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
1544 and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
1545 have "M0 + {#a#} \<in> ?W"
1546 proof (rule accI [of "M0 + {#a#}"])
1548 assume "(N, M0 + {#a#}) \<in> ?R"
1549 then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
1550 (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
1552 then show "N \<in> ?W"
1553 proof (elim exE disjE conjE)
1554 fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
1555 from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
1556 from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
1557 then show "N \<in> ?W" by (simp only: N)
1560 assume N: "N = M0 + K"
1561 assume "\<forall>b. b :# K --> (b, a) \<in> r"
1562 then have "M0 + K \<in> ?W"
1565 from M0 show "M0 + {#} \<in> ?W" by simp
1568 from add.prems have "(x, a) \<in> r" by simp
1569 with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
1570 moreover from add have "M0 + K \<in> ?W" by simp
1571 ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
1572 then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add_assoc)
1574 then show "N \<in> ?W" by (simp only: N)
1577 } note tedious_reasoning = this
1585 fix b assume "(b, {#}) \<in> ?R"
1586 with not_less_empty show "b \<in> ?W" by contradiction
1589 fix M a assume "M \<in> ?W"
1590 from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
1593 assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
1594 show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
1596 fix M assume "M \<in> ?W"
1597 then show "M + {#a#} \<in> ?W"
1598 by (rule acc_induct) (rule tedious_reasoning [OF _ r])
1601 from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
1605 theorem wf_mult1: "wf r ==> wf (mult1 r)"
1606 by (rule acc_wfI) (rule all_accessible)
1608 theorem wf_mult: "wf r ==> wf (mult r)"
1609 unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
1612 subsubsection {* Closure-free presentation *}
1614 text {* One direction. *}
1616 lemma mult_implies_one_step:
1617 "trans r ==> (M, N) \<in> mult r ==>
1618 \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
1619 (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
1620 apply (unfold mult_def mult1_def set_of_def)
1621 apply (erule converse_trancl_induct, clarify)
1622 apply (rule_tac x = M0 in exI, simp, clarify)
1623 apply (case_tac "a :# K")
1624 apply (rule_tac x = I in exI)
1625 apply (simp (no_asm))
1626 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
1627 apply (simp (no_asm_simp) add: add_assoc [symmetric])
1628 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
1629 apply (simp add: diff_union_single_conv)
1630 apply (simp (no_asm_use) add: trans_def)
1632 apply (subgoal_tac "a :# I")
1633 apply (rule_tac x = "I - {#a#}" in exI)
1634 apply (rule_tac x = "J + {#a#}" in exI)
1635 apply (rule_tac x = "K + Ka" in exI)
1637 apply (simp add: multiset_eq_iff split: nat_diff_split)
1639 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp)
1640 apply (simp add: multiset_eq_iff split: nat_diff_split)
1641 apply (simp (no_asm_use) add: trans_def)
1643 apply (subgoal_tac "a :# (M0 + {#a#})")
1645 apply (simp (no_asm))
1648 lemma one_step_implies_mult_aux:
1650 \<forall>I J K. (size J = n \<and> J \<noteq> {#} \<and> (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r))
1651 --> (I + K, I + J) \<in> mult r"
1652 apply (induct_tac n, auto)
1653 apply (frule size_eq_Suc_imp_eq_union, clarify)
1654 apply (rename_tac "J'", simp)
1655 apply (erule notE, auto)
1656 apply (case_tac "J' = {#}")
1657 apply (simp add: mult_def)
1658 apply (rule r_into_trancl)
1659 apply (simp add: mult1_def set_of_def, blast)
1660 txt {* Now we know @{term "J' \<noteq> {#}"}. *}
1661 apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
1662 apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
1663 apply (erule ssubst)
1664 apply (simp add: Ball_def, auto)
1666 "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
1667 (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
1670 apply (simp (no_asm_use) add: add_assoc [symmetric] mult_def)
1671 apply (erule trancl_trans)
1672 apply (rule r_into_trancl)
1673 apply (simp add: mult1_def set_of_def)
1674 apply (rule_tac x = a in exI)
1675 apply (rule_tac x = "I + J'" in exI)
1676 apply (simp add: add_ac)
1679 lemma one_step_implies_mult:
1680 "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
1681 ==> (I + K, I + J) \<in> mult r"
1682 using one_step_implies_mult_aux by blast
1685 subsubsection {* Partial-order properties *}
1687 definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where
1688 "M' <# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
1690 definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where
1691 "M' <=# M \<longleftrightarrow> M' <# M \<or> M' = M"
1693 notation (xsymbols) less_multiset (infix "\<subset>#" 50)
1694 notation (xsymbols) le_multiset (infix "\<subseteq>#" 50)
1696 interpretation multiset_order: order le_multiset less_multiset
1698 have irrefl: "\<And>M :: 'a multiset. \<not> M \<subset># M"
1700 fix M :: "'a multiset"
1701 assume "M \<subset># M"
1702 then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def)
1703 have "trans {(x'::'a, x). x' < x}"
1704 by (rule transI) simp
1706 ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
1707 \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})"
1708 by (rule mult_implies_one_step)
1709 then obtain I J K where "M = I + J" and "M = I + K"
1710 and "J \<noteq> {#}" and "(\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})" by blast
1711 then have aux1: "K \<noteq> {#}" and aux2: "\<forall>k\<in>set_of K. \<exists>j\<in>set_of K. k < j" by auto
1712 have "finite (set_of K)" by simp
1714 ultimately have "set_of K = {}"
1715 by (induct rule: finite_induct) (auto intro: order_less_trans)
1716 with aux1 show False by simp
1718 have trans: "\<And>K M N :: 'a multiset. K \<subset># M \<Longrightarrow> M \<subset># N \<Longrightarrow> K \<subset># N"
1719 unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
1720 show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
1721 by default (auto simp add: le_multiset_def irrefl dest: trans)
1724 lemma mult_less_irrefl [elim!]: "M \<subset># (M::'a::order multiset) ==> R"
1728 subsubsection {* Monotonicity of multiset union *}
1730 lemma mult1_union: "(B, D) \<in> mult1 r ==> (C + B, C + D) \<in> mult1 r"
1731 apply (unfold mult1_def)
1733 apply (rule_tac x = a in exI)
1734 apply (rule_tac x = "C + M0" in exI)
1735 apply (simp add: add_assoc)
1738 lemma union_less_mono2: "B \<subset># D ==> C + B \<subset># C + (D::'a::order multiset)"
1739 apply (unfold less_multiset_def mult_def)
1740 apply (erule trancl_induct)
1741 apply (blast intro: mult1_union)
1742 apply (blast intro: mult1_union trancl_trans)
1745 lemma union_less_mono1: "B \<subset># D ==> B + C \<subset># D + (C::'a::order multiset)"
1746 apply (subst add_commute [of B C])
1747 apply (subst add_commute [of D C])
1748 apply (erule union_less_mono2)
1751 lemma union_less_mono:
1752 "A \<subset># C ==> B \<subset># D ==> A + B \<subset># C + (D::'a::order multiset)"
1753 by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)
1755 interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
1757 qed (auto simp add: le_multiset_def intro: union_less_mono2)
1760 subsection {* Termination proofs with multiset orders *}
1762 lemma multi_member_skip: "x \<in># XS \<Longrightarrow> x \<in># {# y #} + XS"
1763 and multi_member_this: "x \<in># {# x #} + XS"
1764 and multi_member_last: "x \<in># {# x #}"
1767 definition "ms_strict = mult pair_less"
1768 definition "ms_weak = ms_strict \<union> Id"
1770 lemma ms_reduction_pair: "reduction_pair (ms_strict, ms_weak)"
1771 unfolding reduction_pair_def ms_strict_def ms_weak_def pair_less_def
1772 by (auto intro: wf_mult1 wf_trancl simp: mult_def)
1775 "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict"
1776 unfolding ms_strict_def
1777 by (rule one_step_implies_mult) (auto simp add: max_strict_def pair_less_def elim!:max_ext.cases)
1780 "(set_of A, set_of B) \<in> max_strict \<or> A = {#} \<and> B = {#}
1781 \<Longrightarrow> (Z + A, Z + B) \<in> ms_weak"
1782 unfolding ms_weak_def ms_strict_def
1783 by (auto simp add: pair_less_def max_strict_def elim!:max_ext.cases intro: one_step_implies_mult)
1787 pw_leq_empty: "pw_leq {#} {#}"
1788 | pw_leq_step: "\<lbrakk>(x,y) \<in> pair_leq; pw_leq X Y \<rbrakk> \<Longrightarrow> pw_leq ({#x#} + X) ({#y#} + Y)"
1791 "(x, y) \<in> pair_leq \<Longrightarrow> pw_leq {#x#} {#y#}"
1792 by (drule pw_leq_step) (rule pw_leq_empty, simp)
1795 assumes "pw_leq X Y"
1796 shows "\<exists>A B Z. X = A + Z \<and> Y = B + Z \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
1799 case pw_leq_empty thus ?case by auto
1801 case (pw_leq_step x y X Y)
1802 then obtain A B Z where
1803 [simp]: "X = A + Z" "Y = B + Z"
1804 and 1[simp]: "(set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#})"
1806 from pw_leq_step have "x = y \<or> (x, y) \<in> pair_less"
1807 unfolding pair_leq_def by auto
1810 assume [simp]: "x = y"
1812 "{#x#} + X = A + ({#y#}+Z)
1813 \<and> {#y#} + Y = B + ({#y#}+Z)
1814 \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
1815 by (auto simp: add_ac)
1816 thus ?case by (intro exI)
1818 assume A: "(x, y) \<in> pair_less"
1819 let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
1820 have "{#x#} + X = ?A' + Z"
1821 "{#y#} + Y = ?B' + Z"
1822 by (auto simp add: add_ac)
1824 "(set_of ?A', set_of ?B') \<in> max_strict"
1825 using 1 A unfolding max_strict_def
1826 by (auto elim!: max_ext.cases)
1827 ultimately show ?thesis by blast
1832 assumes pwleq: "pw_leq Z Z'"
1833 shows ms_strictI: "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
1834 and ms_weakI1: "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
1835 and ms_weakI2: "(Z + {#}, Z' + {#}) \<in> ms_weak"
1837 from pw_leq_split[OF pwleq]
1839 where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
1840 and mx_or_empty: "(set_of A', set_of B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
1843 assume max: "(set_of A, set_of B) \<in> max_strict"
1845 have "(Z'' + (A + A'), Z'' + (B + B')) \<in> ms_strict"
1847 assume max': "(set_of A', set_of B') \<in> max_strict"
1848 with max have "(set_of (A + A'), set_of (B + B')) \<in> max_strict"
1849 by (auto simp: max_strict_def intro: max_ext_additive)
1850 thus ?thesis by (rule smsI)
1852 assume [simp]: "A' = {#} \<and> B' = {#}"
1853 show ?thesis by (rule smsI) (auto intro: max)
1855 thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add:add_ac)
1856 thus "(Z + A, Z' + B) \<in> ms_weak" by (simp add: ms_weak_def)
1859 have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
1860 thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:add_ac)
1863 lemma empty_neutral: "{#} + x = x" "x + {#} = x"
1864 and nonempty_plus: "{# x #} + rs \<noteq> {#}"
1865 and nonempty_single: "{# x #} \<noteq> {#}"
1870 fun msetT T = Type (@{type_name multiset}, [T]);
1872 fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
1873 | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x
1874 | mk_mset T (x :: xs) =
1875 Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
1876 mk_mset T [x] $ mk_mset T xs
1878 fun mset_member_tac m i =
1880 rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
1882 rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i)
1884 val mset_nonempty_tac =
1885 rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
1887 val regroup_munion_conv =
1888 Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
1889 (map (fn t => t RS eq_reflection) (@{thms add_ac} @ @{thms empty_neutral}))
1891 fun unfold_pwleq_tac i =
1892 (rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
1893 ORELSE (rtac @{thm pw_leq_lstep} i)
1894 ORELSE (rtac @{thm pw_leq_empty} i)
1896 val set_of_simps = [@{thm set_of_empty}, @{thm set_of_single}, @{thm set_of_union},
1897 @{thm Un_insert_left}, @{thm Un_empty_left}]
1899 ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset
1901 msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
1902 mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
1903 mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_of_simps,
1904 smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
1905 reduction_pair= @{thm ms_reduction_pair}
1911 subsection {* Legacy theorem bindings *}
1913 lemmas multi_count_eq = multiset_eq_iff [symmetric]
1915 lemma union_commute: "M + N = N + (M::'a multiset)"
1916 by (fact add_commute)
1918 lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
1921 lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
1922 by (fact add_left_commute)
1924 lemmas union_ac = union_assoc union_commute union_lcomm
1926 lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)"
1927 by (fact add_right_cancel)
1929 lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)"
1930 by (fact add_left_cancel)
1932 lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
1933 by (fact add_imp_eq)
1935 lemma mset_less_trans: "(M::'a multiset) < K \<Longrightarrow> K < N \<Longrightarrow> M < N"
1936 by (fact order_less_trans)
1938 lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
1939 by (fact inf.commute)
1941 lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
1942 by (fact inf.assoc [symmetric])
1944 lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
1945 by (fact inf.left_commute)
1947 lemmas multiset_inter_ac =
1948 multiset_inter_commute
1949 multiset_inter_assoc
1950 multiset_inter_left_commute
1952 lemma mult_less_not_refl:
1953 "\<not> M \<subset># (M::'a::order multiset)"
1954 by (fact multiset_order.less_irrefl)
1956 lemma mult_less_trans:
1957 "K \<subset># M ==> M \<subset># N ==> K \<subset># (N::'a::order multiset)"
1958 by (fact multiset_order.less_trans)
1960 lemma mult_less_not_sym:
1961 "M \<subset># N ==> \<not> N \<subset># (M::'a::order multiset)"
1962 by (fact multiset_order.less_not_sym)
1964 lemma mult_less_asym:
1965 "M \<subset># N ==> (\<not> P ==> N \<subset># (M::'a::order multiset)) ==> P"
1966 by (fact multiset_order.less_asym)
1969 fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T]))
1972 val (maybe_opt, ps) =
1973 Nitpick_Model.dest_plain_fun t' ||> op ~~
1974 ||> map (apsnd (snd o HOLogic.dest_number))
1976 case AList.lookup (op =) ps t of
1977 SOME n => replicate n t
1978 | NONE => [Const (maybe_name, elem_T --> elem_T) $ t]
1980 case maps elems_for (all_values elem_T) @
1981 (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
1983 [] => Const (@{const_name zero_class.zero}, T)
1984 | ts => foldl1 (fn (t1, t2) =>
1985 Const (@{const_name plus_class.plus}, T --> T --> T)
1987 (map (curry (op $) (Const (@{const_name single},
1990 | multiset_postproc _ _ _ _ t = t
1994 Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
1998 hide_const (open) fold
2001 subsection {* Naive implementation using lists *}
2003 code_datatype multiset_of
2006 "{#} = multiset_of []"
2010 "{#x#} = multiset_of [x]"
2013 lemma union_code [code]:
2014 "multiset_of xs + multiset_of ys = multiset_of (xs @ ys)"
2018 "image_mset f (multiset_of xs) = multiset_of (map f xs)"
2019 by (simp add: multiset_of_map)
2022 "Multiset.filter f (multiset_of xs) = multiset_of (filter f xs)"
2023 by (simp add: multiset_of_filter)
2026 "multiset_of xs - multiset_of ys = multiset_of (fold remove1 ys xs)"
2027 by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute)
2030 "multiset_of xs #\<inter> multiset_of ys =
2031 multiset_of (snd (fold (\<lambda>x (ys, zs).
2032 if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, [])))"
2034 have "\<And>zs. multiset_of (snd (fold (\<lambda>x (ys, zs).
2035 if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) =
2036 (multiset_of xs #\<inter> multiset_of ys) + multiset_of zs"
2037 by (induct xs arbitrary: ys)
2038 (auto simp add: mem_set_multiset_eq inter_add_right1 inter_add_right2 ac_simps)
2039 then show ?thesis by simp
2043 "multiset_of xs #\<union> multiset_of ys =
2044 multiset_of (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))"
2046 have "\<And>zs. multiset_of (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) =
2047 (multiset_of xs #\<union> multiset_of ys) + multiset_of zs"
2048 by (induct xs arbitrary: ys) (simp_all add: multiset_eq_iff)
2049 then show ?thesis by simp
2052 lemma [code_unfold]:
2053 "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
2054 by (simp add: in_multiset_of)
2057 "count (multiset_of xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0"
2059 have "\<And>n. fold (\<lambda>y. if x = y then Suc else id) xs n = count (multiset_of xs) x + n"
2060 by (induct xs) simp_all
2061 then show ?thesis by simp
2065 "set_of (multiset_of xs) = set xs"
2069 "sorted_list_of_multiset (multiset_of xs) = sort xs"
2070 by (induct xs) simp_all
2072 lemma [code]: -- {* not very efficient, but representation-ignorant! *}
2073 "multiset_of_set A = multiset_of (sorted_list_of_set A)"
2074 apply (cases "finite A")
2076 apply (induct A rule: finite_induct)
2077 apply (simp_all add: union_commute)
2081 "mcard (multiset_of xs) = length xs"
2082 by (simp add: mcard_multiset_of)
2085 "A \<le> B \<longleftrightarrow> A #\<inter> B = A"
2086 by (auto simp add: inf.order_iff)
2088 instantiation multiset :: (equal) equal
2092 [code]: "HOL.equal A B \<longleftrightarrow> (A::'a multiset) \<le> B \<and> B \<le> A"
2095 by default (simp add: equal_multiset_def eq_iff)
2100 "(A::'a multiset) < B \<longleftrightarrow> A \<le> B \<and> A \<noteq> B"
2104 "msetsum (multiset_of xs) = listsum xs"
2105 by (induct xs) (simp_all add: add.commute)
2108 "msetprod (multiset_of xs) = fold times xs 1"
2110 have "\<And>x. fold times xs x = msetprod (multiset_of xs) * x"
2111 by (induct xs) (simp_all add: mult.assoc)
2112 then show ?thesis by simp
2117 by (fact size_eq_mcard)
2120 Exercise for the casual reader: add implementations for @{const le_multiset}
2121 and @{const less_multiset} (multiset order).
2124 text {* Quickcheck generators *}
2126 definition (in term_syntax)
2127 msetify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
2128 \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
2129 [code_unfold]: "msetify xs = Code_Evaluation.valtermify multiset_of {\<cdot>} xs"
2131 notation fcomp (infixl "\<circ>>" 60)
2132 notation scomp (infixl "\<circ>\<rightarrow>" 60)
2134 instantiation multiset :: (random) random
2138 "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (msetify xs))"
2144 no_notation fcomp (infixl "\<circ>>" 60)
2145 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
2147 instantiation multiset :: (full_exhaustive) full_exhaustive
2150 definition full_exhaustive_multiset :: "('a multiset \<times> (unit \<Rightarrow> term) \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option"
2152 "full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (\<lambda>xs. f (msetify xs)) i"
2158 hide_const (open) msetify