src/HOL/Library/Multiset.thy
author wenzelm
Thu May 06 14:14:18 2004 +0200 (2004-05-06)
changeset 14706 71590b7733b7
parent 14691 e1eedc8cad37
child 14722 8e739a6eaf11
permissions -rw-r--r--
tuned document;
     1 (*  Title:      HOL/Library/Multiset.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, Markus Wenzel, and Lawrence C Paulson
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 *)
     6 
     7 header {* Multisets *}
     8 
     9 theory Multiset = Accessible_Part:
    10 
    11 subsection {* The type of multisets *}
    12 
    13 typedef 'a multiset = "{f::'a => nat. finite {x . 0 < f x}}"
    14 proof
    15   show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp
    16 qed
    17 
    18 lemmas multiset_typedef [simp] =
    19     Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
    20   and [simp] = Rep_multiset_inject [symmetric]
    21 
    22 constdefs
    23   Mempty :: "'a multiset"    ("{#}")
    24   "{#} == Abs_multiset (\<lambda>a. 0)"
    25 
    26   single :: "'a => 'a multiset"    ("{#_#}")
    27   "{#a#} == Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
    28 
    29   count :: "'a multiset => 'a => nat"
    30   "count == Rep_multiset"
    31 
    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)"
    34 
    35 syntax
    36   "_Melem" :: "'a => 'a multiset => bool"    ("(_/ :# _)" [50, 51] 50)
    37   "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ : _./ _#})")
    38 translations
    39   "a :# M" == "0 < count M a"
    40   "{#x:M. P#}" == "MCollect M (\<lambda>x. P)"
    41 
    42 constdefs
    43   set_of :: "'a multiset => 'a set"
    44   "set_of M == {x. x :# M}"
    45 
    46 instance multiset :: (type) "{plus, minus, zero}" ..
    47 
    48 defs (overloaded)
    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)"
    53 
    54 
    55 text {*
    56  \medskip Preservation of the representing set @{term multiset}.
    57 *}
    58 
    59 lemma const0_in_multiset [simp]: "(\<lambda>a. 0) \<in> multiset"
    60   apply (simp add: multiset_def)
    61   done
    62 
    63 lemma only1_in_multiset [simp]: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset"
    64   apply (simp add: multiset_def)
    65   done
    66 
    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)
    70   apply simp
    71   apply (drule finite_UnI)
    72    apply assumption
    73   apply (simp del: finite_Un add: Un_def)
    74   done
    75 
    76 lemma diff_preserves_multiset [simp]:
    77     "M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset"
    78   apply (unfold multiset_def)
    79   apply simp
    80   apply (rule finite_subset)
    81    prefer 2
    82    apply assumption
    83   apply auto
    84   done
    85 
    86 
    87 subsection {* Algebraic properties of multisets *}
    88 
    89 subsubsection {* Union *}
    90 
    91 theorem union_empty [simp]: "M + {#} = M \<and> {#} + M = M"
    92   apply (simp add: union_def Mempty_def)
    93   done
    94 
    95 theorem union_commute: "M + N = N + (M::'a multiset)"
    96   apply (simp add: union_def add_ac)
    97   done
    98 
    99 theorem union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
   100   apply (simp add: union_def add_ac)
   101   done
   102 
   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])
   107   done
   108 
   109 theorems union_ac = union_assoc union_commute union_lcomm
   110 
   111 instance multiset :: (type) plus_ac0
   112   apply intro_classes
   113     apply (rule union_commute)
   114    apply (rule union_assoc)
   115   apply simp
   116   done
   117 
   118 
   119 subsubsection {* Difference *}
   120 
   121 theorem diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
   122   apply (simp add: Mempty_def diff_def)
   123   done
   124 
   125 theorem diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M"
   126   apply (simp add: union_def diff_def)
   127   done
   128 
   129 
   130 subsubsection {* Count of elements *}
   131 
   132 theorem count_empty [simp]: "count {#} a = 0"
   133   apply (simp add: count_def Mempty_def)
   134   done
   135 
   136 theorem count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
   137   apply (simp add: count_def single_def)
   138   done
   139 
   140 theorem count_union [simp]: "count (M + N) a = count M a + count N a"
   141   apply (simp add: count_def union_def)
   142   done
   143 
   144 theorem count_diff [simp]: "count (M - N) a = count M a - count N a"
   145   apply (simp add: count_def diff_def)
   146   done
   147 
   148 
   149 subsubsection {* Set of elements *}
   150 
   151 theorem set_of_empty [simp]: "set_of {#} = {}"
   152   apply (simp add: set_of_def)
   153   done
   154 
   155 theorem set_of_single [simp]: "set_of {#b#} = {b}"
   156   apply (simp add: set_of_def)
   157   done
   158 
   159 theorem set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
   160   apply (auto simp add: set_of_def)
   161   done
   162 
   163 theorem set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
   164   apply (auto simp add: set_of_def Mempty_def count_def expand_fun_eq)
   165   done
   166 
   167 theorem mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
   168   apply (auto simp add: set_of_def)
   169   done
   170 
   171 
   172 subsubsection {* Size *}
   173 
   174 theorem size_empty [simp]: "size {#} = 0"
   175   apply (simp add: size_def)
   176   done
   177 
   178 theorem size_single [simp]: "size {#b#} = 1"
   179   apply (simp add: size_def)
   180   done
   181 
   182 theorem finite_set_of [iff]: "finite (set_of M)"
   183   apply (cut_tac x = M in Rep_multiset)
   184   apply (simp add: multiset_def set_of_def count_def)
   185   done
   186 
   187 theorem setsum_count_Int:
   188     "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
   189   apply (erule finite_induct)
   190    apply simp
   191   apply (simp add: Int_insert_left set_of_def)
   192   done
   193 
   194 theorem size_union [simp]: "size (M + N::'a multiset) = size M + size N"
   195   apply (unfold size_def)
   196   apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
   197    prefer 2
   198    apply (rule ext)
   199    apply simp
   200   apply (simp (no_asm_simp) add: setsum_Un setsum_addf setsum_count_Int)
   201   apply (subst Int_commute)
   202   apply (simp (no_asm_simp) add: setsum_count_Int)
   203   done
   204 
   205 theorem size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
   206   apply (unfold size_def Mempty_def count_def)
   207   apply auto
   208   apply (simp add: set_of_def count_def expand_fun_eq)
   209   done
   210 
   211 theorem size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
   212   apply (unfold size_def)
   213   apply (drule setsum_SucD)
   214   apply auto
   215   done
   216 
   217 
   218 subsubsection {* Equality of multisets *}
   219 
   220 theorem multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)"
   221   apply (simp add: count_def expand_fun_eq)
   222   done
   223 
   224 theorem single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
   225   apply (simp add: single_def Mempty_def expand_fun_eq)
   226   done
   227 
   228 theorem single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)"
   229   apply (auto simp add: single_def expand_fun_eq)
   230   done
   231 
   232 theorem union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})"
   233   apply (auto simp add: union_def Mempty_def expand_fun_eq)
   234   done
   235 
   236 theorem empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})"
   237   apply (auto simp add: union_def Mempty_def expand_fun_eq)
   238   done
   239 
   240 theorem union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))"
   241   apply (simp add: union_def expand_fun_eq)
   242   done
   243 
   244 theorem union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))"
   245   apply (simp add: union_def expand_fun_eq)
   246   done
   247 
   248 theorem union_is_single:
   249     "(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})"
   250   apply (unfold Mempty_def single_def union_def)
   251   apply (simp add: add_is_1 expand_fun_eq)
   252   apply blast
   253   done
   254 
   255 theorem single_is_union:
   256   "({#a#} = M + N) =
   257     ({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)"
   258   apply (unfold Mempty_def single_def union_def)
   259   apply (simp add: add_is_1 one_is_add expand_fun_eq)
   260   apply (blast dest: sym)
   261   done
   262 
   263 theorem add_eq_conv_diff:
   264   "(M + {#a#} = N + {#b#}) =
   265     (M = N \<and> a = b \<or>
   266       M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})"
   267   apply (unfold single_def union_def diff_def)
   268   apply (simp (no_asm) add: expand_fun_eq)
   269   apply (rule conjI)
   270    apply force
   271   apply safe
   272   apply simp_all
   273   apply (simp add: eq_sym_conv)
   274   done
   275 
   276 (*
   277 val prems = Goal
   278  "[| !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> finite F --> P F";
   279 by (res_inst_tac [("a","F"),("f","\<lambda>A. if finite A then card A else 0")]
   280      measure_induct 1);
   281 by (Clarify_tac 1);
   282 by (resolve_tac prems 1);
   283  by (assume_tac 1);
   284 by (Clarify_tac 1);
   285 by (subgoal_tac "finite G" 1);
   286  by (fast_tac (claset() addDs [finite_subset,order_less_le RS iffD1]) 2);
   287 by (etac allE 1);
   288 by (etac impE 1);
   289  by (Blast_tac 2);
   290 by (asm_simp_tac (simpset() addsimps [psubset_card]) 1);
   291 no_qed();
   292 val lemma = result();
   293 
   294 val prems = Goal
   295  "[| finite F; !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> P F";
   296 by (rtac (lemma RS mp) 1);
   297 by (REPEAT(ares_tac prems 1));
   298 qed "finite_psubset_induct";
   299 
   300 Better: use wf_finite_psubset in WF_Rel
   301 *)
   302 
   303 
   304 subsection {* Induction over multisets *}
   305 
   306 lemma setsum_decr:
   307   "finite F ==> (0::nat) < f a ==>
   308     setsum (f (a := f a - 1)) F = (if a \<in> F then setsum f F - 1 else setsum f F)"
   309   apply (erule finite_induct)
   310    apply auto
   311   apply (drule_tac a = a in mk_disjoint_insert)
   312   apply auto
   313   done
   314 
   315 lemma rep_multiset_induct_aux:
   316   "P (\<lambda>a. (0::nat)) ==> (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1)))
   317     ==> \<forall>f. f \<in> multiset --> setsum f {x. 0 < f x} = n --> P f"
   318 proof -
   319   case rule_context
   320   note premises = this [unfolded multiset_def]
   321   show ?thesis
   322     apply (unfold multiset_def)
   323     apply (induct_tac n)
   324      apply simp
   325      apply clarify
   326      apply (subgoal_tac "f = (\<lambda>a.0)")
   327       apply simp
   328       apply (rule premises)
   329      apply (rule ext)
   330      apply force
   331     apply clarify
   332     apply (frule setsum_SucD)
   333     apply clarify
   334     apply (rename_tac a)
   335     apply (subgoal_tac "finite {x. 0 < (f (a := f a - 1)) x}")
   336      prefer 2
   337      apply (rule finite_subset)
   338       prefer 2
   339       apply assumption
   340      apply simp
   341      apply blast
   342     apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
   343      prefer 2
   344      apply (rule ext)
   345      apply (simp (no_asm_simp))
   346      apply (erule ssubst, rule premises)
   347      apply blast
   348     apply (erule allE, erule impE, erule_tac [2] mp)
   349      apply blast
   350     apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
   351     apply (subgoal_tac "{x. x \<noteq> a --> 0 < f x} = {x. 0 < f x}")
   352      prefer 2
   353      apply blast
   354     apply (subgoal_tac "{x. x \<noteq> a \<and> 0 < f x} = {x. 0 < f x} - {a}")
   355      prefer 2
   356      apply blast
   357     apply (simp add: le_imp_diff_is_add setsum_diff1 cong: conj_cong)
   358     done
   359 qed
   360 
   361 theorem rep_multiset_induct:
   362   "f \<in> multiset ==> P (\<lambda>a. 0) ==>
   363     (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
   364   apply (insert rep_multiset_induct_aux)
   365   apply blast
   366   done
   367 
   368 theorem multiset_induct [induct type: multiset]:
   369   "P {#} ==> (!!M x. P M ==> P (M + {#x#})) ==> P M"
   370 proof -
   371   note defns = union_def single_def Mempty_def
   372   assume prem1 [unfolded defns]: "P {#}"
   373   assume prem2 [unfolded defns]: "!!M x. P M ==> P (M + {#x#})"
   374   show ?thesis
   375     apply (rule Rep_multiset_inverse [THEN subst])
   376     apply (rule Rep_multiset [THEN rep_multiset_induct])
   377      apply (rule prem1)
   378     apply (subgoal_tac "f (b := f b + 1) = (\<lambda>a. f a + (if a = b then 1 else 0))")
   379      prefer 2
   380      apply (simp add: expand_fun_eq)
   381     apply (erule ssubst)
   382     apply (erule Abs_multiset_inverse [THEN subst])
   383     apply (erule prem2 [simplified])
   384     done
   385 qed
   386 
   387 
   388 lemma MCollect_preserves_multiset:
   389     "M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset"
   390   apply (simp add: multiset_def)
   391   apply (rule finite_subset)
   392    apply auto
   393   done
   394 
   395 theorem count_MCollect [simp]:
   396     "count {# x:M. P x #} a = (if P a then count M a else 0)"
   397   apply (unfold count_def MCollect_def)
   398   apply (simp add: MCollect_preserves_multiset)
   399   done
   400 
   401 theorem set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}"
   402   apply (auto simp add: set_of_def)
   403   done
   404 
   405 theorem multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}"
   406   apply (subst multiset_eq_conv_count_eq)
   407   apply auto
   408   done
   409 
   410 declare Rep_multiset_inject [symmetric, simp del]
   411 declare multiset_typedef [simp del]
   412 
   413 theorem add_eq_conv_ex:
   414   "(M + {#a#} = N + {#b#}) =
   415     (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
   416   apply (auto simp add: add_eq_conv_diff)
   417   done
   418 
   419 
   420 subsection {* Multiset orderings *}
   421 
   422 subsubsection {* Well-foundedness *}
   423 
   424 constdefs
   425   mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
   426   "mult1 r ==
   427     {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
   428       (\<forall>b. b :# K --> (b, a) \<in> r)}"
   429 
   430   mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
   431   "mult r == (mult1 r)\<^sup>+"
   432 
   433 lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
   434   by (simp add: mult1_def)
   435 
   436 lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
   437     (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
   438     (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
   439   (concl is "?case1 (mult1 r) \<or> ?case2")
   440 proof (unfold mult1_def)
   441   let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
   442   let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
   443   let ?case1 = "?case1 {(N, M). ?R N M}"
   444 
   445   assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
   446   hence "\<exists>a' M0' K.
   447       M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
   448   thus "?case1 \<or> ?case2"
   449   proof (elim exE conjE)
   450     fix a' M0' K
   451     assume N: "N = M0' + K" and r: "?r K a'"
   452     assume "M0 + {#a#} = M0' + {#a'#}"
   453     hence "M0 = M0' \<and> a = a' \<or>
   454         (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
   455       by (simp only: add_eq_conv_ex)
   456     thus ?thesis
   457     proof (elim disjE conjE exE)
   458       assume "M0 = M0'" "a = a'"
   459       with N r have "?r K a \<and> N = M0 + K" by simp
   460       hence ?case2 .. thus ?thesis ..
   461     next
   462       fix K'
   463       assume "M0' = K' + {#a#}"
   464       with N have n: "N = K' + K + {#a#}" by (simp add: union_ac)
   465 
   466       assume "M0 = K' + {#a'#}"
   467       with r have "?R (K' + K) M0" by blast
   468       with n have ?case1 by simp thus ?thesis ..
   469     qed
   470   qed
   471 qed
   472 
   473 lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
   474 proof
   475   let ?R = "mult1 r"
   476   let ?W = "acc ?R"
   477   {
   478     fix M M0 a
   479     assume M0: "M0 \<in> ?W"
   480       and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
   481       and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
   482     have "M0 + {#a#} \<in> ?W"
   483     proof (rule accI [of "M0 + {#a#}"])
   484       fix N
   485       assume "(N, M0 + {#a#}) \<in> ?R"
   486       hence "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
   487           (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
   488         by (rule less_add)
   489       thus "N \<in> ?W"
   490       proof (elim exE disjE conjE)
   491         fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
   492         from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
   493         hence "M + {#a#} \<in> ?W" ..
   494         thus "N \<in> ?W" by (simp only: N)
   495       next
   496         fix K
   497         assume N: "N = M0 + K"
   498         assume "\<forall>b. b :# K --> (b, a) \<in> r"
   499         have "?this --> M0 + K \<in> ?W" (is "?P K")
   500         proof (induct K)
   501           from M0 have "M0 + {#} \<in> ?W" by simp
   502           thus "?P {#}" ..
   503 
   504           fix K x assume hyp: "?P K"
   505           show "?P (K + {#x#})"
   506           proof
   507             assume a: "\<forall>b. b :# (K + {#x#}) --> (b, a) \<in> r"
   508             hence "(x, a) \<in> r" by simp
   509             with wf_hyp have b: "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
   510 
   511             from a hyp have "M0 + K \<in> ?W" by simp
   512             with b have "(M0 + K) + {#x#} \<in> ?W" ..
   513             thus "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc)
   514           qed
   515         qed
   516         hence "M0 + K \<in> ?W" ..
   517         thus "N \<in> ?W" by (simp only: N)
   518       qed
   519     qed
   520   } note tedious_reasoning = this
   521 
   522   assume wf: "wf r"
   523   fix M
   524   show "M \<in> ?W"
   525   proof (induct M)
   526     show "{#} \<in> ?W"
   527     proof (rule accI)
   528       fix b assume "(b, {#}) \<in> ?R"
   529       with not_less_empty show "b \<in> ?W" by contradiction
   530     qed
   531 
   532     fix M a assume "M \<in> ?W"
   533     from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
   534     proof induct
   535       fix a
   536       assume "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
   537       show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
   538       proof
   539         fix M assume "M \<in> ?W"
   540         thus "M + {#a#} \<in> ?W"
   541           by (rule acc_induct) (rule tedious_reasoning)
   542       qed
   543     qed
   544     thus "M + {#a#} \<in> ?W" ..
   545   qed
   546 qed
   547 
   548 theorem wf_mult1: "wf r ==> wf (mult1 r)"
   549   by (rule acc_wfI, rule all_accessible)
   550 
   551 theorem wf_mult: "wf r ==> wf (mult r)"
   552   by (unfold mult_def, rule wf_trancl, rule wf_mult1)
   553 
   554 
   555 subsubsection {* Closure-free presentation *}
   556 
   557 (*Badly needed: a linear arithmetic procedure for multisets*)
   558 
   559 lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})"
   560   apply (simp add: multiset_eq_conv_count_eq)
   561   done
   562 
   563 text {* One direction. *}
   564 
   565 lemma mult_implies_one_step:
   566   "trans r ==> (M, N) \<in> mult r ==>
   567     \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
   568     (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
   569   apply (unfold mult_def mult1_def set_of_def)
   570   apply (erule converse_trancl_induct)
   571   apply clarify
   572    apply (rule_tac x = M0 in exI)
   573    apply simp
   574   apply clarify
   575   apply (case_tac "a :# K")
   576    apply (rule_tac x = I in exI)
   577    apply (simp (no_asm))
   578    apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
   579    apply (simp (no_asm_simp) add: union_assoc [symmetric])
   580    apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
   581    apply (simp add: diff_union_single_conv)
   582    apply (simp (no_asm_use) add: trans_def)
   583    apply blast
   584   apply (subgoal_tac "a :# I")
   585    apply (rule_tac x = "I - {#a#}" in exI)
   586    apply (rule_tac x = "J + {#a#}" in exI)
   587    apply (rule_tac x = "K + Ka" in exI)
   588    apply (rule conjI)
   589     apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
   590    apply (rule conjI)
   591     apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
   592     apply simp
   593     apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
   594    apply (simp (no_asm_use) add: trans_def)
   595    apply blast
   596   apply (subgoal_tac "a :# (M0 + {#a#})")
   597    apply simp
   598   apply (simp (no_asm))
   599   done
   600 
   601 lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}"
   602   apply (simp add: multiset_eq_conv_count_eq)
   603   done
   604 
   605 lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}"
   606   apply (erule size_eq_Suc_imp_elem [THEN exE])
   607   apply (drule elem_imp_eq_diff_union)
   608   apply auto
   609   done
   610 
   611 lemma one_step_implies_mult_aux:
   612   "trans r ==>
   613     \<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))
   614       --> (I + K, I + J) \<in> mult r"
   615   apply (induct_tac n)
   616    apply auto
   617   apply (frule size_eq_Suc_imp_eq_union)
   618   apply clarify
   619   apply (rename_tac "J'")
   620   apply simp
   621   apply (erule notE)
   622    apply auto
   623   apply (case_tac "J' = {#}")
   624    apply (simp add: mult_def)
   625    apply (rule r_into_trancl)
   626    apply (simp add: mult1_def set_of_def)
   627    apply blast
   628   txt {* Now we know @{term "J' \<noteq> {#}"}. *}
   629   apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
   630   apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
   631   apply (erule ssubst)
   632   apply (simp add: Ball_def)
   633   apply auto
   634   apply (subgoal_tac
   635     "((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #},
   636       (I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r")
   637    prefer 2
   638    apply force
   639   apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def)
   640   apply (erule trancl_trans)
   641   apply (rule r_into_trancl)
   642   apply (simp add: mult1_def set_of_def)
   643   apply (rule_tac x = a in exI)
   644   apply (rule_tac x = "I + J'" in exI)
   645   apply (simp add: union_ac)
   646   done
   647 
   648 theorem one_step_implies_mult:
   649   "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
   650     ==> (I + K, I + J) \<in> mult r"
   651   apply (insert one_step_implies_mult_aux)
   652   apply blast
   653   done
   654 
   655 
   656 subsubsection {* Partial-order properties *}
   657 
   658 instance multiset :: (type) ord ..
   659 
   660 defs (overloaded)
   661   less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}"
   662   le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)"
   663 
   664 lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}"
   665   apply (unfold trans_def)
   666   apply (blast intro: order_less_trans)
   667   done
   668 
   669 text {*
   670  \medskip Irreflexivity.
   671 *}
   672 
   673 lemma mult_irrefl_aux:
   674     "finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) --> A = {}"
   675   apply (erule finite_induct)
   676    apply (auto intro: order_less_trans)
   677   done
   678 
   679 theorem mult_less_not_refl: "\<not> M < (M::'a::order multiset)"
   680   apply (unfold less_multiset_def)
   681   apply auto
   682   apply (drule trans_base_order [THEN mult_implies_one_step])
   683   apply auto
   684   apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]])
   685   apply (simp add: set_of_eq_empty_iff)
   686   done
   687 
   688 lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R"
   689   apply (insert mult_less_not_refl)
   690   apply fast
   691   done
   692 
   693 
   694 text {* Transitivity. *}
   695 
   696 theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)"
   697   apply (unfold less_multiset_def mult_def)
   698   apply (blast intro: trancl_trans)
   699   done
   700 
   701 text {* Asymmetry. *}
   702 
   703 theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)"
   704   apply auto
   705   apply (rule mult_less_not_refl [THEN notE])
   706   apply (erule mult_less_trans)
   707   apply assumption
   708   done
   709 
   710 theorem mult_less_asym:
   711     "M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P"
   712   apply (insert mult_less_not_sym)
   713   apply blast
   714   done
   715 
   716 theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)"
   717   apply (unfold le_multiset_def)
   718   apply auto
   719   done
   720 
   721 text {* Anti-symmetry. *}
   722 
   723 theorem mult_le_antisym:
   724     "M <= N ==> N <= M ==> M = (N::'a::order multiset)"
   725   apply (unfold le_multiset_def)
   726   apply (blast dest: mult_less_not_sym)
   727   done
   728 
   729 text {* Transitivity. *}
   730 
   731 theorem mult_le_trans:
   732     "K <= M ==> M <= N ==> K <= (N::'a::order multiset)"
   733   apply (unfold le_multiset_def)
   734   apply (blast intro: mult_less_trans)
   735   done
   736 
   737 theorem mult_less_le: "(M < N) = (M <= N \<and> M \<noteq> (N::'a::order multiset))"
   738   apply (unfold le_multiset_def)
   739   apply auto
   740   done
   741 
   742 text {* Partial order. *}
   743 
   744 instance multiset :: (order) order
   745   apply intro_classes
   746      apply (rule mult_le_refl)
   747     apply (erule mult_le_trans)
   748     apply assumption
   749    apply (erule mult_le_antisym)
   750    apply assumption
   751   apply (rule mult_less_le)
   752   done
   753 
   754 
   755 subsubsection {* Monotonicity of multiset union *}
   756 
   757 theorem mult1_union:
   758     "(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r"
   759   apply (unfold mult1_def)
   760   apply auto
   761   apply (rule_tac x = a in exI)
   762   apply (rule_tac x = "C + M0" in exI)
   763   apply (simp add: union_assoc)
   764   done
   765 
   766 lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)"
   767   apply (unfold less_multiset_def mult_def)
   768   apply (erule trancl_induct)
   769    apply (blast intro: mult1_union transI order_less_trans r_into_trancl)
   770   apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
   771   done
   772 
   773 lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)"
   774   apply (subst union_commute [of B C])
   775   apply (subst union_commute [of D C])
   776   apply (erule union_less_mono2)
   777   done
   778 
   779 theorem union_less_mono:
   780     "A < C ==> B < D ==> A + B < C + (D::'a::order multiset)"
   781   apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans)
   782   done
   783 
   784 theorem union_le_mono:
   785     "A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)"
   786   apply (unfold le_multiset_def)
   787   apply (blast intro: union_less_mono union_less_mono1 union_less_mono2)
   788   done
   789 
   790 theorem empty_leI [iff]: "{#} <= (M::'a::order multiset)"
   791   apply (unfold le_multiset_def less_multiset_def)
   792   apply (case_tac "M = {#}")
   793    prefer 2
   794    apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))")
   795     prefer 2
   796     apply (rule one_step_implies_mult)
   797       apply (simp only: trans_def)
   798       apply auto
   799   done
   800 
   801 theorem union_upper1: "A <= A + (B::'a::order multiset)"
   802   apply (subgoal_tac "A + {#} <= A + B")
   803    prefer 2
   804    apply (rule union_le_mono)
   805     apply auto
   806   done
   807 
   808 theorem union_upper2: "B <= A + (B::'a::order multiset)"
   809   apply (subst union_commute, rule union_upper1)
   810   done
   811 
   812 end