1 (* Title: HOL/Library/Multiset.thy
3 Author: Tobias Nipkow, TU Muenchen
4 Author: Markus Wenzel, TU Muenchen
5 Author: Lawrence C Paulson, Cambridge University Computer Laboratory
10 \author{Tobias Nipkow, Markus Wenzel, and Lawrence C Paulson}
13 theory Multiset = Accessible_Part:
15 subsection {* The type of multisets *}
17 typedef 'a multiset = "{f::'a => nat. finite {x . 0 < f x}}"
19 show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp
22 lemmas multiset_typedef [simp] =
23 Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
24 and [simp] = Rep_multiset_inject [symmetric]
27 Mempty :: "'a multiset" ("{#}")
28 "{#} == Abs_multiset (\<lambda>a. 0)"
30 single :: "'a => 'a multiset" ("{#_#}")
31 "{#a#} == Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
33 count :: "'a multiset => 'a => nat"
34 "count == Rep_multiset"
36 MCollect :: "'a multiset => ('a => bool) => 'a multiset"
37 "MCollect M P == Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)"
40 "_Melem" :: "'a => 'a multiset => bool" ("(_/ :# _)" [50, 51] 50)
41 "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset" ("(1{# _ : _./ _#})")
43 "a :# M" == "0 < count M a"
44 "{#x:M. P#}" == "MCollect M (\<lambda>x. P)"
47 set_of :: "'a multiset => 'a set"
48 "set_of M == {x. x :# M}"
50 instance multiset :: ("term") plus ..
51 instance multiset :: ("term") minus ..
52 instance multiset :: ("term") zero ..
55 union_def: "M + N == Abs_multiset (\<lambda>a. Rep_multiset M a + Rep_multiset N a)"
56 diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)"
57 Zero_multiset_def [simp]: "0 == {#}"
58 size_def: "size M == setsum (count M) (set_of M)"
62 \medskip Preservation of the representing set @{term multiset}.
65 lemma const0_in_multiset [simp]: "(\<lambda>a. 0) \<in> multiset"
66 apply (simp add: multiset_def)
69 lemma only1_in_multiset [simp]: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset"
70 apply (simp add: multiset_def)
73 lemma union_preserves_multiset [simp]:
74 "M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset"
75 apply (unfold multiset_def)
77 apply (drule finite_UnI)
79 apply (simp del: finite_Un add: Un_def)
82 lemma diff_preserves_multiset [simp]:
83 "M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset"
84 apply (unfold multiset_def)
86 apply (rule finite_subset)
93 subsection {* Algebraic properties of multisets *}
95 subsubsection {* Union *}
97 theorem union_empty [simp]: "M + {#} = M \<and> {#} + M = M"
98 apply (simp add: union_def Mempty_def)
101 theorem union_commute: "M + N = N + (M::'a multiset)"
102 apply (simp add: union_def add_ac)
105 theorem union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
106 apply (simp add: union_def add_ac)
109 theorem union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
110 apply (rule union_commute [THEN trans])
111 apply (rule union_assoc [THEN trans])
112 apply (rule union_commute [THEN arg_cong])
115 theorems union_ac = union_assoc union_commute union_lcomm
117 instance multiset :: ("term") plus_ac0
119 apply (rule union_commute)
120 apply (rule union_assoc)
125 subsubsection {* Difference *}
127 theorem diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
128 apply (simp add: Mempty_def diff_def)
131 theorem diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M"
132 apply (simp add: union_def diff_def)
136 subsubsection {* Count of elements *}
138 theorem count_empty [simp]: "count {#} a = 0"
139 apply (simp add: count_def Mempty_def)
142 theorem count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
143 apply (simp add: count_def single_def)
146 theorem count_union [simp]: "count (M + N) a = count M a + count N a"
147 apply (simp add: count_def union_def)
150 theorem count_diff [simp]: "count (M - N) a = count M a - count N a"
151 apply (simp add: count_def diff_def)
155 subsubsection {* Set of elements *}
157 theorem set_of_empty [simp]: "set_of {#} = {}"
158 apply (simp add: set_of_def)
161 theorem set_of_single [simp]: "set_of {#b#} = {b}"
162 apply (simp add: set_of_def)
165 theorem set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
166 apply (auto simp add: set_of_def)
169 theorem set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
170 apply (auto simp add: set_of_def Mempty_def count_def expand_fun_eq)
173 theorem mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
174 apply (auto simp add: set_of_def)
178 subsubsection {* Size *}
180 theorem size_empty [simp]: "size {#} = 0"
181 apply (simp add: size_def)
184 theorem size_single [simp]: "size {#b#} = 1"
185 apply (simp add: size_def)
188 theorem finite_set_of [iff]: "finite (set_of M)"
189 apply (cut_tac x = M in Rep_multiset)
190 apply (simp add: multiset_def set_of_def count_def)
193 theorem setsum_count_Int:
194 "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
195 apply (erule finite_induct)
197 apply (simp add: Int_insert_left set_of_def)
200 theorem size_union [simp]: "size (M + N::'a multiset) = size M + size N"
201 apply (unfold size_def)
202 apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
206 apply (simp (no_asm_simp) add: setsum_Un setsum_addf setsum_count_Int)
207 apply (subst Int_commute)
208 apply (simp (no_asm_simp) add: setsum_count_Int)
211 theorem size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
212 apply (unfold size_def Mempty_def count_def)
214 apply (simp add: set_of_def count_def expand_fun_eq)
217 theorem size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
218 apply (unfold size_def)
219 apply (drule setsum_SucD)
224 subsubsection {* Equality of multisets *}
226 theorem multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)"
227 apply (simp add: count_def expand_fun_eq)
230 theorem single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
231 apply (simp add: single_def Mempty_def expand_fun_eq)
234 theorem single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)"
235 apply (auto simp add: single_def expand_fun_eq)
238 theorem union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})"
239 apply (auto simp add: union_def Mempty_def expand_fun_eq)
242 theorem empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})"
243 apply (auto simp add: union_def Mempty_def expand_fun_eq)
246 theorem union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))"
247 apply (simp add: union_def expand_fun_eq)
250 theorem union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))"
251 apply (simp add: union_def expand_fun_eq)
254 theorem union_is_single:
255 "(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})"
256 apply (unfold Mempty_def single_def union_def)
257 apply (simp add: add_is_1 expand_fun_eq)
261 theorem single_is_union:
263 ({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)"
264 apply (unfold Mempty_def single_def union_def)
265 apply (simp add: add_is_1 one_is_add expand_fun_eq)
266 apply (blast dest: sym)
269 theorem add_eq_conv_diff:
270 "(M + {#a#} = N + {#b#}) =
271 (M = N \<and> a = b \<or>
272 M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})"
273 apply (unfold single_def union_def diff_def)
274 apply (simp (no_asm) add: expand_fun_eq)
278 apply (simp_all add: eq_sym_conv)
283 "[| !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> finite F --> P F";
284 by (res_inst_tac [("a","F"),("f","\<lambda>A. if finite A then card A else 0")]
287 by (resolve_tac prems 1);
290 by (subgoal_tac "finite G" 1);
291 by (fast_tac (claset() addDs [finite_subset,order_less_le RS iffD1]) 2);
295 by (asm_simp_tac (simpset() addsimps [psubset_card]) 1);
297 val lemma = result();
300 "[| finite F; !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> P F";
301 by (rtac (lemma RS mp) 1);
302 by (REPEAT(ares_tac prems 1));
303 qed "finite_psubset_induct";
305 Better: use wf_finite_psubset in WF_Rel
309 subsection {* Induction over multisets *}
312 "finite F ==> (0::nat) < f a ==>
313 setsum (f (a := f a - 1)) F = (if a \<in> F then setsum f F - 1 else setsum f F)"
314 apply (erule finite_induct)
316 apply (drule_tac a = a in mk_disjoint_insert)
320 lemma rep_multiset_induct_aux:
321 "P (\<lambda>a. (0::nat)) ==> (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1)))
322 ==> \<forall>f. f \<in> multiset --> setsum f {x. 0 < f x} = n --> P f"
325 note premises = this [unfolded multiset_def]
327 apply (unfold multiset_def)
331 apply (subgoal_tac "f = (\<lambda>a.0)")
333 apply (rule premises)
337 apply (frule setsum_SucD)
340 apply (subgoal_tac "finite {x. 0 < (f (a := f a - 1)) x}")
342 apply (rule finite_subset)
347 apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
350 apply (simp (no_asm_simp))
351 apply (erule ssubst, rule premises)
353 apply (erule allE, erule impE, erule_tac [2] mp)
355 apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
356 apply (subgoal_tac "{x. x \<noteq> a --> 0 < f x} = {x. 0 < f x}")
359 apply (subgoal_tac "{x. x \<noteq> a \<and> 0 < f x} = {x. 0 < f x} - {a}")
362 apply (simp add: le_imp_diff_is_add setsum_diff1 cong: conj_cong)
366 theorem rep_multiset_induct:
367 "f \<in> multiset ==> P (\<lambda>a. 0) ==>
368 (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
369 apply (insert rep_multiset_induct_aux)
373 theorem multiset_induct [induct type: multiset]:
374 "P {#} ==> (!!M x. P M ==> P (M + {#x#})) ==> P M"
376 note defns = union_def single_def Mempty_def
377 assume prem1 [unfolded defns]: "P {#}"
378 assume prem2 [unfolded defns]: "!!M x. P M ==> P (M + {#x#})"
380 apply (rule Rep_multiset_inverse [THEN subst])
381 apply (rule Rep_multiset [THEN rep_multiset_induct])
383 apply (subgoal_tac "f (b := f b + 1) = (\<lambda>a. f a + (if a = b then 1 else 0))")
385 apply (simp add: expand_fun_eq)
387 apply (erule Abs_multiset_inverse [THEN subst])
388 apply (erule prem2 [simplified])
393 lemma MCollect_preserves_multiset:
394 "M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset"
395 apply (simp add: multiset_def)
396 apply (rule finite_subset)
400 theorem count_MCollect [simp]:
401 "count {# x:M. P x #} a = (if P a then count M a else 0)"
402 apply (unfold count_def MCollect_def)
403 apply (simp add: MCollect_preserves_multiset)
406 theorem set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}"
407 apply (auto simp add: set_of_def)
410 theorem multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}"
411 apply (subst multiset_eq_conv_count_eq)
415 declare Rep_multiset_inject [symmetric, simp del]
416 declare multiset_typedef [simp del]
418 theorem add_eq_conv_ex:
419 "(M + {#a#} = N + {#b#}) =
420 (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
421 apply (auto simp add: add_eq_conv_diff)
425 subsection {* Multiset orderings *}
427 subsubsection {* Well-foundedness *}
430 mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
432 {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
433 (\<forall>b. b :# K --> (b, a) \<in> r)}"
435 mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
436 "mult r == (mult1 r)\<^sup>+"
438 lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
439 by (simp add: mult1_def)
441 lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
442 (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
443 (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
444 (concl is "?case1 (mult1 r) \<or> ?case2")
445 proof (unfold mult1_def)
446 let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
447 let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
448 let ?case1 = "?case1 {(N, M). ?R N M}"
450 assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
451 hence "\<exists>a' M0' K.
452 M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
453 thus "?case1 \<or> ?case2"
454 proof (elim exE conjE)
456 assume N: "N = M0' + K" and r: "?r K a'"
457 assume "M0 + {#a#} = M0' + {#a'#}"
458 hence "M0 = M0' \<and> a = a' \<or>
459 (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
460 by (simp only: add_eq_conv_ex)
462 proof (elim disjE conjE exE)
463 assume "M0 = M0'" "a = a'"
464 with N r have "?r K a \<and> N = M0 + K" by simp
465 hence ?case2 .. thus ?thesis ..
468 assume "M0' = K' + {#a#}"
469 with N have n: "N = K' + K + {#a#}" by (simp add: union_ac)
471 assume "M0 = K' + {#a'#}"
472 with r have "?R (K' + K) M0" by blast
473 with n have ?case1 by simp thus ?thesis ..
478 lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
484 assume M0: "M0 \<in> ?W"
485 and wf_hyp: "\<forall>b. (b, a) \<in> r --> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
486 and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
487 have "M0 + {#a#} \<in> ?W"
488 proof (rule accI [of "M0 + {#a#}"])
490 assume "(N, M0 + {#a#}) \<in> ?R"
491 hence "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
492 (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
495 proof (elim exE disjE conjE)
496 fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
497 from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
498 hence "M + {#a#} \<in> ?W" ..
499 thus "N \<in> ?W" by (simp only: N)
502 assume N: "N = M0 + K"
503 assume "\<forall>b. b :# K --> (b, a) \<in> r"
504 have "?this --> M0 + K \<in> ?W" (is "?P K")
506 from M0 have "M0 + {#} \<in> ?W" by simp
509 fix K x assume hyp: "?P K"
510 show "?P (K + {#x#})"
512 assume a: "\<forall>b. b :# (K + {#x#}) --> (b, a) \<in> r"
513 hence "(x, a) \<in> r" by simp
514 with wf_hyp have b: "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
516 from a hyp have "M0 + K \<in> ?W" by simp
517 with b have "(M0 + K) + {#x#} \<in> ?W" ..
518 thus "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc)
521 hence "M0 + K \<in> ?W" ..
522 thus "N \<in> ?W" by (simp only: N)
525 } note tedious_reasoning = this
533 fix b assume "(b, {#}) \<in> ?R"
534 with not_less_empty show "b \<in> ?W" by contradiction
537 fix M a assume "M \<in> ?W"
538 from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
541 assume "\<forall>b. (b, a) \<in> r --> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
542 show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
544 fix M assume "M \<in> ?W"
545 thus "M + {#a#} \<in> ?W"
546 by (rule acc_induct) (rule tedious_reasoning)
549 thus "M + {#a#} \<in> ?W" ..
553 theorem wf_mult1: "wf r ==> wf (mult1 r)"
554 by (rule acc_wfI, rule all_accessible)
556 theorem wf_mult: "wf r ==> wf (mult r)"
557 by (unfold mult_def, rule wf_trancl, rule wf_mult1)
560 subsubsection {* Closure-free presentation *}
562 (*Badly needed: a linear arithmetic procedure for multisets*)
564 lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})"
565 apply (simp add: multiset_eq_conv_count_eq)
568 text {* One direction. *}
570 lemma mult_implies_one_step:
571 "trans r ==> (M, N) \<in> mult r ==>
572 \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
573 (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
574 apply (unfold mult_def mult1_def set_of_def)
575 apply (erule converse_trancl_induct)
577 apply (rule_tac x = M0 in exI)
580 apply (case_tac "a :# K")
581 apply (rule_tac x = I in exI)
582 apply (simp (no_asm))
583 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
584 apply (simp (no_asm_simp) add: union_assoc [symmetric])
585 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
586 apply (simp add: diff_union_single_conv)
587 apply (simp (no_asm_use) add: trans_def)
589 apply (subgoal_tac "a :# I")
590 apply (rule_tac x = "I - {#a#}" in exI)
591 apply (rule_tac x = "J + {#a#}" in exI)
592 apply (rule_tac x = "K + Ka" in exI)
594 apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
596 apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
598 apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
599 apply (simp (no_asm_use) add: trans_def)
601 apply (subgoal_tac "a :# (M0 + {#a#})")
603 apply (simp (no_asm))
606 lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}"
607 apply (simp add: multiset_eq_conv_count_eq)
610 lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}"
611 apply (erule size_eq_Suc_imp_elem [THEN exE])
612 apply (drule elem_imp_eq_diff_union)
616 lemma one_step_implies_mult_aux:
618 \<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))
619 --> (I + K, I + J) \<in> mult r"
622 apply (frule size_eq_Suc_imp_eq_union)
624 apply (rename_tac "J'")
628 apply (case_tac "J' = {#}")
629 apply (simp add: mult_def)
630 apply (rule r_into_trancl)
631 apply (simp add: mult1_def set_of_def)
633 txt {* Now we know @{term "J' \<noteq> {#}"}. *}
634 apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
635 apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
637 apply (simp add: Ball_def)
640 "((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #},
641 (I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r")
644 apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def)
645 apply (erule trancl_trans)
646 apply (rule r_into_trancl)
647 apply (simp add: mult1_def set_of_def)
648 apply (rule_tac x = a in exI)
649 apply (rule_tac x = "I + J'" in exI)
650 apply (simp add: union_ac)
653 theorem one_step_implies_mult:
654 "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
655 ==> (I + K, I + J) \<in> mult r"
656 apply (insert one_step_implies_mult_aux)
661 subsubsection {* Partial-order properties *}
663 instance multiset :: ("term") ord ..
666 less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}"
667 le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)"
669 lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}"
670 apply (unfold trans_def)
671 apply (blast intro: order_less_trans)
675 \medskip Irreflexivity.
678 lemma mult_irrefl_aux:
679 "finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) --> A = {}"
680 apply (erule finite_induct)
681 apply (auto intro: order_less_trans)
684 theorem mult_less_not_refl: "\<not> M < (M::'a::order multiset)"
685 apply (unfold less_multiset_def)
687 apply (drule trans_base_order [THEN mult_implies_one_step])
689 apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]])
690 apply (simp add: set_of_eq_empty_iff)
693 lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R"
694 apply (insert mult_less_not_refl)
699 text {* Transitivity. *}
701 theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)"
702 apply (unfold less_multiset_def mult_def)
703 apply (blast intro: trancl_trans)
706 text {* Asymmetry. *}
708 theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)"
710 apply (rule mult_less_not_refl [THEN notE])
711 apply (erule mult_less_trans)
715 theorem mult_less_asym:
716 "M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P"
717 apply (insert mult_less_not_sym)
721 theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)"
722 apply (unfold le_multiset_def)
726 text {* Anti-symmetry. *}
728 theorem mult_le_antisym:
729 "M <= N ==> N <= M ==> M = (N::'a::order multiset)"
730 apply (unfold le_multiset_def)
731 apply (blast dest: mult_less_not_sym)
734 text {* Transitivity. *}
736 theorem mult_le_trans:
737 "K <= M ==> M <= N ==> K <= (N::'a::order multiset)"
738 apply (unfold le_multiset_def)
739 apply (blast intro: mult_less_trans)
742 theorem mult_less_le: "(M < N) = (M <= N \<and> M \<noteq> (N::'a::order multiset))"
743 apply (unfold le_multiset_def)
747 text {* Partial order. *}
749 instance multiset :: (order) order
751 apply (rule mult_le_refl)
752 apply (erule mult_le_trans)
754 apply (erule mult_le_antisym)
756 apply (rule mult_less_le)
760 subsubsection {* Monotonicity of multiset union *}
763 "(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r"
764 apply (unfold mult1_def)
766 apply (rule_tac x = a in exI)
767 apply (rule_tac x = "C + M0" in exI)
768 apply (simp add: union_assoc)
771 lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)"
772 apply (unfold less_multiset_def mult_def)
773 apply (erule trancl_induct)
774 apply (blast intro: mult1_union transI order_less_trans r_into_trancl)
775 apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
778 lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)"
779 apply (subst union_commute [of B C])
780 apply (subst union_commute [of D C])
781 apply (erule union_less_mono2)
784 theorem union_less_mono:
785 "A < C ==> B < D ==> A + B < C + (D::'a::order multiset)"
786 apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans)
789 theorem union_le_mono:
790 "A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)"
791 apply (unfold le_multiset_def)
792 apply (blast intro: union_less_mono union_less_mono1 union_less_mono2)
795 theorem empty_leI [iff]: "{#} <= (M::'a::order multiset)"
796 apply (unfold le_multiset_def less_multiset_def)
797 apply (case_tac "M = {#}")
799 apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))")
801 apply (rule one_step_implies_mult)
802 apply (simp only: trans_def)
804 apply (blast intro: order_less_trans)
807 theorem union_upper1: "A <= A + (B::'a::order multiset)"
808 apply (subgoal_tac "A + {#} <= A + B")
810 apply (rule union_le_mono)
814 theorem union_upper2: "B <= A + (B::'a::order multiset)"
815 apply (subst union_commute, rule union_upper1)