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