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