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