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