1 (* Title: HOL/Library/Multiset.thy
3 Author: Tobias Nipkow, Markus Wenzel, and Lawrence C Paulson
4 License: GPL (GNU GENERAL PUBLIC LICENSE)
9 theory Multiset = Accessible_Part:
11 subsection {* The type of multisets *}
13 typedef 'a multiset = "{f::'a => nat. finite {x . 0 < f x}}"
15 show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp
18 lemmas multiset_typedef [simp] =
19 Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
20 and [simp] = Rep_multiset_inject [symmetric]
23 Mempty :: "'a multiset" ("{#}")
24 "{#} == Abs_multiset (\<lambda>a. 0)"
26 single :: "'a => 'a multiset" ("{#_#}")
27 "{#a#} == Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
29 count :: "'a multiset => 'a => nat"
30 "count == Rep_multiset"
32 MCollect :: "'a multiset => ('a => bool) => 'a multiset"
33 "MCollect M P == Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)"
36 "_Melem" :: "'a => 'a multiset => bool" ("(_/ :# _)" [50, 51] 50)
37 "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset" ("(1{# _ : _./ _#})")
39 "a :# M" == "0 < count M a"
40 "{#x:M. P#}" == "MCollect M (\<lambda>x. P)"
43 set_of :: "'a multiset => 'a set"
44 "set_of M == {x. x :# M}"
46 instance multiset :: (type) "{plus, minus, zero}" ..
49 union_def: "M + N == Abs_multiset (\<lambda>a. Rep_multiset M a + Rep_multiset N a)"
50 diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)"
51 Zero_multiset_def [simp]: "0 == {#}"
52 size_def: "size M == setsum (count M) (set_of M)"
56 \medskip Preservation of the representing set @{term multiset}.
59 lemma const0_in_multiset [simp]: "(\<lambda>a. 0) \<in> multiset"
60 apply (simp add: multiset_def)
63 lemma only1_in_multiset [simp]: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset"
64 apply (simp add: multiset_def)
67 lemma union_preserves_multiset [simp]:
68 "M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset"
69 apply (unfold multiset_def)
71 apply (drule finite_UnI)
73 apply (simp del: finite_Un add: Un_def)
76 lemma diff_preserves_multiset [simp]:
77 "M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset"
78 apply (unfold multiset_def)
80 apply (rule finite_subset)
87 subsection {* Algebraic properties of multisets *}
89 subsubsection {* Union *}
91 theorem union_empty [simp]: "M + {#} = M \<and> {#} + M = M"
92 apply (simp add: union_def Mempty_def)
95 theorem union_commute: "M + N = N + (M::'a multiset)"
96 apply (simp add: union_def add_ac)
99 theorem union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
100 apply (simp add: union_def add_ac)
103 theorem union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
104 apply (rule union_commute [THEN trans])
105 apply (rule union_assoc [THEN trans])
106 apply (rule union_commute [THEN arg_cong])
109 theorems union_ac = union_assoc union_commute union_lcomm
111 instance multiset :: (type) comm_monoid_add
113 fix a b c :: "'a multiset"
114 show "(a + b) + c = a + (b + c)" by (rule union_assoc)
115 show "a + b = b + a" by (rule union_commute)
116 show "0 + a = a" by simp
120 subsubsection {* Difference *}
122 theorem diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
123 apply (simp add: Mempty_def diff_def)
126 theorem diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M"
127 apply (simp add: union_def diff_def)
131 subsubsection {* Count of elements *}
133 theorem count_empty [simp]: "count {#} a = 0"
134 apply (simp add: count_def Mempty_def)
137 theorem count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
138 apply (simp add: count_def single_def)
141 theorem count_union [simp]: "count (M + N) a = count M a + count N a"
142 apply (simp add: count_def union_def)
145 theorem count_diff [simp]: "count (M - N) a = count M a - count N a"
146 apply (simp add: count_def diff_def)
150 subsubsection {* Set of elements *}
152 theorem set_of_empty [simp]: "set_of {#} = {}"
153 apply (simp add: set_of_def)
156 theorem set_of_single [simp]: "set_of {#b#} = {b}"
157 apply (simp add: set_of_def)
160 theorem set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
161 apply (auto simp add: set_of_def)
164 theorem set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
165 apply (auto simp add: set_of_def Mempty_def count_def expand_fun_eq)
168 theorem mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
169 apply (auto simp add: set_of_def)
173 subsubsection {* Size *}
175 theorem size_empty [simp]: "size {#} = 0"
176 apply (simp add: size_def)
179 theorem size_single [simp]: "size {#b#} = 1"
180 apply (simp add: size_def)
183 theorem finite_set_of [iff]: "finite (set_of M)"
184 apply (cut_tac x = M in Rep_multiset)
185 apply (simp add: multiset_def set_of_def count_def)
188 theorem setsum_count_Int:
189 "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
190 apply (erule finite_induct)
192 apply (simp add: Int_insert_left set_of_def)
195 theorem size_union [simp]: "size (M + N::'a multiset) = size M + size N"
196 apply (unfold size_def)
197 apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
201 apply (simp (no_asm_simp) add: setsum_Un setsum_addf setsum_count_Int)
202 apply (subst Int_commute)
203 apply (simp (no_asm_simp) add: setsum_count_Int)
206 theorem size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
207 apply (unfold size_def Mempty_def count_def)
209 apply (simp add: set_of_def count_def expand_fun_eq)
212 theorem size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
213 apply (unfold size_def)
214 apply (drule setsum_SucD)
219 subsubsection {* Equality of multisets *}
221 theorem multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)"
222 apply (simp add: count_def expand_fun_eq)
225 theorem single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
226 apply (simp add: single_def Mempty_def expand_fun_eq)
229 theorem single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)"
230 apply (auto simp add: single_def expand_fun_eq)
233 theorem union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})"
234 apply (auto simp add: union_def Mempty_def expand_fun_eq)
237 theorem empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})"
238 apply (auto simp add: union_def Mempty_def expand_fun_eq)
241 theorem union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))"
242 apply (simp add: union_def expand_fun_eq)
245 theorem union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))"
246 apply (simp add: union_def expand_fun_eq)
249 theorem union_is_single:
250 "(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})"
251 apply (unfold Mempty_def single_def union_def)
252 apply (simp add: add_is_1 expand_fun_eq)
256 theorem single_is_union:
258 ({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)"
259 apply (unfold Mempty_def single_def union_def)
260 apply (simp add: add_is_1 one_is_add expand_fun_eq)
261 apply (blast dest: sym)
264 theorem add_eq_conv_diff:
265 "(M + {#a#} = N + {#b#}) =
266 (M = N \<and> a = b \<or>
267 M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})"
268 apply (unfold single_def union_def diff_def)
269 apply (simp (no_asm) add: expand_fun_eq)
274 apply (simp add: eq_sym_conv)
279 "[| !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> finite F --> P F";
280 by (res_inst_tac [("a","F"),("f","\<lambda>A. if finite A then card A else 0")]
283 by (resolve_tac prems 1);
286 by (subgoal_tac "finite G" 1);
287 by (fast_tac (claset() addDs [finite_subset,order_less_le RS iffD1]) 2);
291 by (asm_simp_tac (simpset() addsimps [psubset_card]) 1);
293 val lemma = result();
296 "[| finite F; !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> P F";
297 by (rtac (lemma RS mp) 1);
298 by (REPEAT(ares_tac prems 1));
299 qed "finite_psubset_induct";
301 Better: use wf_finite_psubset in WF_Rel
305 subsection {* Induction over multisets *}
308 "finite F ==> (0::nat) < f a ==>
309 setsum (f (a := f a - 1)) F = (if a \<in> F then setsum f F - 1 else setsum f F)"
310 apply (erule finite_induct)
312 apply (drule_tac a = a in mk_disjoint_insert)
316 lemma rep_multiset_induct_aux:
317 "P (\<lambda>a. (0::nat)) ==> (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1)))
318 ==> \<forall>f. f \<in> multiset --> setsum f {x. 0 < f x} = n --> P f"
321 note premises = this [unfolded multiset_def]
323 apply (unfold multiset_def)
327 apply (subgoal_tac "f = (\<lambda>a.0)")
329 apply (rule premises)
333 apply (frule setsum_SucD)
336 apply (subgoal_tac "finite {x. 0 < (f (a := f a - 1)) x}")
338 apply (rule finite_subset)
343 apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
346 apply (simp (no_asm_simp))
347 apply (erule ssubst, rule premises)
349 apply (erule allE, erule impE, erule_tac [2] mp)
351 apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
352 apply (subgoal_tac "{x. x \<noteq> a --> 0 < f x} = {x. 0 < f x}")
355 apply (subgoal_tac "{x. x \<noteq> a \<and> 0 < f x} = {x. 0 < f x} - {a}")
358 apply (simp add: le_imp_diff_is_add setsum_diff1 cong: conj_cong)
362 theorem rep_multiset_induct:
363 "f \<in> multiset ==> P (\<lambda>a. 0) ==>
364 (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
365 apply (insert rep_multiset_induct_aux)
369 theorem multiset_induct [induct type: multiset]:
370 "P {#} ==> (!!M x. P M ==> P (M + {#x#})) ==> P M"
372 note defns = union_def single_def Mempty_def
373 assume prem1 [unfolded defns]: "P {#}"
374 assume prem2 [unfolded defns]: "!!M x. P M ==> P (M + {#x#})"
376 apply (rule Rep_multiset_inverse [THEN subst])
377 apply (rule Rep_multiset [THEN rep_multiset_induct])
379 apply (subgoal_tac "f (b := f b + 1) = (\<lambda>a. f a + (if a = b then 1 else 0))")
381 apply (simp add: expand_fun_eq)
383 apply (erule Abs_multiset_inverse [THEN subst])
384 apply (erule prem2 [simplified])
389 lemma MCollect_preserves_multiset:
390 "M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset"
391 apply (simp add: multiset_def)
392 apply (rule finite_subset)
396 theorem count_MCollect [simp]:
397 "count {# x:M. P x #} a = (if P a then count M a else 0)"
398 apply (unfold count_def MCollect_def)
399 apply (simp add: MCollect_preserves_multiset)
402 theorem set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}"
403 apply (auto simp add: set_of_def)
406 theorem multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}"
407 apply (subst multiset_eq_conv_count_eq)
411 declare Rep_multiset_inject [symmetric, simp del]
412 declare multiset_typedef [simp del]
414 theorem add_eq_conv_ex:
415 "(M + {#a#} = N + {#b#}) =
416 (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
417 apply (auto simp add: add_eq_conv_diff)
421 subsection {* Multiset orderings *}
423 subsubsection {* Well-foundedness *}
426 mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
428 {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
429 (\<forall>b. b :# K --> (b, a) \<in> r)}"
431 mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
432 "mult r == (mult1 r)\<^sup>+"
434 lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
435 by (simp add: mult1_def)
437 lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
438 (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
439 (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
440 (concl is "?case1 (mult1 r) \<or> ?case2")
441 proof (unfold mult1_def)
442 let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
443 let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
444 let ?case1 = "?case1 {(N, M). ?R N M}"
446 assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
447 hence "\<exists>a' M0' K.
448 M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
449 thus "?case1 \<or> ?case2"
450 proof (elim exE conjE)
452 assume N: "N = M0' + K" and r: "?r K a'"
453 assume "M0 + {#a#} = M0' + {#a'#}"
454 hence "M0 = M0' \<and> a = a' \<or>
455 (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
456 by (simp only: add_eq_conv_ex)
458 proof (elim disjE conjE exE)
459 assume "M0 = M0'" "a = a'"
460 with N r have "?r K a \<and> N = M0 + K" by simp
461 hence ?case2 .. thus ?thesis ..
464 assume "M0' = K' + {#a#}"
465 with N have n: "N = K' + K + {#a#}" by (simp add: union_ac)
467 assume "M0 = K' + {#a'#}"
468 with r have "?R (K' + K) M0" by blast
469 with n have ?case1 by simp thus ?thesis ..
474 lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
480 assume M0: "M0 \<in> ?W"
481 and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
482 and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
483 have "M0 + {#a#} \<in> ?W"
484 proof (rule accI [of "M0 + {#a#}"])
486 assume "(N, M0 + {#a#}) \<in> ?R"
487 hence "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
488 (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
491 proof (elim exE disjE conjE)
492 fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
493 from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
494 hence "M + {#a#} \<in> ?W" ..
495 thus "N \<in> ?W" by (simp only: N)
498 assume N: "N = M0 + K"
499 assume "\<forall>b. b :# K --> (b, a) \<in> r"
500 have "?this --> M0 + K \<in> ?W" (is "?P K")
502 from M0 have "M0 + {#} \<in> ?W" by simp
505 fix K x assume hyp: "?P K"
506 show "?P (K + {#x#})"
508 assume a: "\<forall>b. b :# (K + {#x#}) --> (b, a) \<in> r"
509 hence "(x, a) \<in> r" by simp
510 with wf_hyp have b: "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
512 from a hyp have "M0 + K \<in> ?W" by simp
513 with b have "(M0 + K) + {#x#} \<in> ?W" ..
514 thus "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc)
517 hence "M0 + K \<in> ?W" ..
518 thus "N \<in> ?W" by (simp only: N)
521 } note tedious_reasoning = this
529 fix b assume "(b, {#}) \<in> ?R"
530 with not_less_empty show "b \<in> ?W" by contradiction
533 fix M a assume "M \<in> ?W"
534 from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
537 assume "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
538 show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
540 fix M assume "M \<in> ?W"
541 thus "M + {#a#} \<in> ?W"
542 by (rule acc_induct) (rule tedious_reasoning)
545 thus "M + {#a#} \<in> ?W" ..
549 theorem wf_mult1: "wf r ==> wf (mult1 r)"
550 by (rule acc_wfI, rule all_accessible)
552 theorem wf_mult: "wf r ==> wf (mult r)"
553 by (unfold mult_def, rule wf_trancl, rule wf_mult1)
556 subsubsection {* Closure-free presentation *}
558 (*Badly needed: a linear arithmetic procedure for multisets*)
560 lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})"
561 apply (simp add: multiset_eq_conv_count_eq)
564 text {* One direction. *}
566 lemma mult_implies_one_step:
567 "trans r ==> (M, N) \<in> mult r ==>
568 \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
569 (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
570 apply (unfold mult_def mult1_def set_of_def)
571 apply (erule converse_trancl_induct)
573 apply (rule_tac x = M0 in exI)
576 apply (case_tac "a :# K")
577 apply (rule_tac x = I in exI)
578 apply (simp (no_asm))
579 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
580 apply (simp (no_asm_simp) add: union_assoc [symmetric])
581 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
582 apply (simp add: diff_union_single_conv)
583 apply (simp (no_asm_use) add: trans_def)
585 apply (subgoal_tac "a :# I")
586 apply (rule_tac x = "I - {#a#}" in exI)
587 apply (rule_tac x = "J + {#a#}" in exI)
588 apply (rule_tac x = "K + Ka" in exI)
590 apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
592 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
594 apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
595 apply (simp (no_asm_use) add: trans_def)
597 apply (subgoal_tac "a :# (M0 + {#a#})")
599 apply (simp (no_asm))
602 lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}"
603 apply (simp add: multiset_eq_conv_count_eq)
606 lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}"
607 apply (erule size_eq_Suc_imp_elem [THEN exE])
608 apply (drule elem_imp_eq_diff_union)
612 lemma one_step_implies_mult_aux:
614 \<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))
615 --> (I + K, I + J) \<in> mult r"
618 apply (frule size_eq_Suc_imp_eq_union)
620 apply (rename_tac "J'")
624 apply (case_tac "J' = {#}")
625 apply (simp add: mult_def)
626 apply (rule r_into_trancl)
627 apply (simp add: mult1_def set_of_def)
629 txt {* Now we know @{term "J' \<noteq> {#}"}. *}
630 apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
631 apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
633 apply (simp add: Ball_def)
636 "((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #},
637 (I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r")
640 apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def)
641 apply (erule trancl_trans)
642 apply (rule r_into_trancl)
643 apply (simp add: mult1_def set_of_def)
644 apply (rule_tac x = a in exI)
645 apply (rule_tac x = "I + J'" in exI)
646 apply (simp add: union_ac)
649 theorem one_step_implies_mult:
650 "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
651 ==> (I + K, I + J) \<in> mult r"
652 apply (insert one_step_implies_mult_aux)
657 subsubsection {* Partial-order properties *}
659 instance multiset :: (type) ord ..
662 less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}"
663 le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)"
665 lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}"
666 apply (unfold trans_def)
667 apply (blast intro: order_less_trans)
671 \medskip Irreflexivity.
674 lemma mult_irrefl_aux:
675 "finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) --> A = {}"
676 apply (erule finite_induct)
677 apply (auto intro: order_less_trans)
680 theorem mult_less_not_refl: "\<not> M < (M::'a::order multiset)"
681 apply (unfold less_multiset_def)
683 apply (drule trans_base_order [THEN mult_implies_one_step])
685 apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]])
686 apply (simp add: set_of_eq_empty_iff)
689 lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R"
690 apply (insert mult_less_not_refl)
695 text {* Transitivity. *}
697 theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)"
698 apply (unfold less_multiset_def mult_def)
699 apply (blast intro: trancl_trans)
702 text {* Asymmetry. *}
704 theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)"
706 apply (rule mult_less_not_refl [THEN notE])
707 apply (erule mult_less_trans)
711 theorem mult_less_asym:
712 "M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P"
713 apply (insert mult_less_not_sym)
717 theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)"
718 apply (unfold le_multiset_def)
722 text {* Anti-symmetry. *}
724 theorem mult_le_antisym:
725 "M <= N ==> N <= M ==> M = (N::'a::order multiset)"
726 apply (unfold le_multiset_def)
727 apply (blast dest: mult_less_not_sym)
730 text {* Transitivity. *}
732 theorem mult_le_trans:
733 "K <= M ==> M <= N ==> K <= (N::'a::order multiset)"
734 apply (unfold le_multiset_def)
735 apply (blast intro: mult_less_trans)
738 theorem mult_less_le: "(M < N) = (M <= N \<and> M \<noteq> (N::'a::order multiset))"
739 apply (unfold le_multiset_def)
743 text {* Partial order. *}
745 instance multiset :: (order) order
747 apply (rule mult_le_refl)
748 apply (erule mult_le_trans)
750 apply (erule mult_le_antisym)
752 apply (rule mult_less_le)
756 subsubsection {* Monotonicity of multiset union *}
759 "(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r"
760 apply (unfold mult1_def)
762 apply (rule_tac x = a in exI)
763 apply (rule_tac x = "C + M0" in exI)
764 apply (simp add: union_assoc)
767 lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)"
768 apply (unfold less_multiset_def mult_def)
769 apply (erule trancl_induct)
770 apply (blast intro: mult1_union transI order_less_trans r_into_trancl)
771 apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
774 lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)"
775 apply (subst union_commute [of B C])
776 apply (subst union_commute [of D C])
777 apply (erule union_less_mono2)
780 theorem union_less_mono:
781 "A < C ==> B < D ==> A + B < C + (D::'a::order multiset)"
782 apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans)
785 theorem union_le_mono:
786 "A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)"
787 apply (unfold le_multiset_def)
788 apply (blast intro: union_less_mono union_less_mono1 union_less_mono2)
791 theorem empty_leI [iff]: "{#} <= (M::'a::order multiset)"
792 apply (unfold le_multiset_def less_multiset_def)
793 apply (case_tac "M = {#}")
795 apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))")
797 apply (rule one_step_implies_mult)
798 apply (simp only: trans_def)
802 theorem union_upper1: "A <= A + (B::'a::order multiset)"
803 apply (subgoal_tac "A + {#} <= A + B")
805 apply (rule union_le_mono)
809 theorem union_upper2: "B <= A + (B::'a::order multiset)"
810 apply (subst union_commute, rule union_upper1)