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