src/HOL/Library/Multiset.thy
 changeset 10249 e4d13d8a9011 child 10277 081c8641aa11
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Library/Multiset.thy	Wed Oct 18 23:28:33 2000 +0200
1.3 @@ -0,0 +1,854 @@
1.4 +(*  Title:      HOL/Library/Multiset.thy
1.5 +    ID:         \$Id\$
1.6 +    Author:     Tobias Nipkow, TU Muenchen
1.7 +    Author:     Markus Wenzel, TU Muenchen
1.8 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
1.9 +*)
1.10 +
1.12 + \title{Multisets}
1.13 + \author{Tobias Nipkow, Markus Wenzel, and Lawrence C Paulson}
1.14 +*}
1.15 +
1.16 +theory Multiset = Accessible_Part:
1.17 +
1.18 +subsection {* The type of multisets *}
1.19 +
1.20 +typedef 'a multiset = "{f::'a => nat. finite {x . 0 < f x}}"
1.21 +proof
1.22 +  show "(\<lambda>x. 0::nat) \<in> {f. finite {x. 0 < f x}}"
1.23 +    by simp
1.24 +qed
1.25 +
1.26 +lemmas multiset_typedef [simp] =
1.27 +  Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
1.28 +
1.29 +constdefs
1.30 +  Mempty :: "'a multiset"    ("{#}")
1.31 +  "{#} == Abs_multiset (\<lambda>a. 0)"
1.32 +
1.33 +  single :: "'a => 'a multiset"    ("{#_#}")
1.34 +  "{#a#} == Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
1.35 +
1.36 +  count :: "'a multiset => 'a => nat"
1.37 +  "count == Rep_multiset"
1.38 +
1.39 +  MCollect :: "'a multiset => ('a => bool) => 'a multiset"
1.40 +  "MCollect M P == Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)"
1.41 +
1.42 +syntax
1.43 +  "_Melem" :: "'a => 'a multiset => bool"    ("(_/ :# _)" [50, 51] 50)
1.44 +  "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ : _./ _#})")
1.45 +translations
1.46 +  "a :# M" == "0 < count M a"
1.47 +  "{#x:M. P#}" == "MCollect M (\<lambda>x. P)"
1.48 +
1.49 +constdefs
1.50 +  set_of :: "'a multiset => 'a set"
1.51 +  "set_of M == {x. x :# M}"
1.52 +
1.53 +instance multiset :: ("term") plus by intro_classes
1.54 +instance multiset :: ("term") minus by intro_classes
1.55 +instance multiset :: ("term") zero by intro_classes
1.56 +
1.58 +  union_def: "M + N == Abs_multiset (\<lambda>a. Rep_multiset M a + Rep_multiset N a)"
1.59 +  diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)"
1.60 +  Zero_def [simp]: "0 == {#}"
1.61 +  size_def: "size M == setsum (count M) (set_of M)"
1.62 +
1.63 +
1.64 +text {*
1.65 + \medskip Preservation of the representing set @{term multiset}.
1.66 +*}
1.67 +
1.68 +lemma const0_in_multiset [simp]: "(\<lambda>a. 0) \<in> multiset"
1.69 +  apply (simp add: multiset_def)
1.70 +  done
1.71 +
1.72 +lemma only1_in_multiset [simp]: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset"
1.73 +  apply (simp add: multiset_def)
1.74 +  done
1.75 +
1.76 +lemma union_preserves_multiset [simp]:
1.77 +    "M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset"
1.78 +  apply (unfold multiset_def)
1.79 +  apply simp
1.80 +  apply (drule finite_UnI)
1.81 +   apply assumption
1.82 +  apply (simp del: finite_Un add: Un_def)
1.83 +  done
1.84 +
1.85 +lemma diff_preserves_multiset [simp]:
1.86 +    "M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset"
1.87 +  apply (unfold multiset_def)
1.88 +  apply simp
1.89 +  apply (rule finite_subset)
1.90 +   prefer 2
1.91 +   apply assumption
1.92 +  apply auto
1.93 +  done
1.94 +
1.95 +text {*
1.96 + \medskip Injectivity of @{term Rep_multiset}.
1.97 +*}  (* FIXME typedef package (!?) *)
1.98 +
1.99 +lemma multiset_eq_conv_Rep_eq [simp]:
1.100 +    "(M = N) = (Rep_multiset M = Rep_multiset N)"
1.101 +  apply (rule iffI)
1.102 +   apply simp
1.103 +  apply (drule_tac f = Abs_multiset in arg_cong)
1.104 +  apply simp
1.105 +  done
1.106 +
1.107 +(* FIXME
1.108 +Goal
1.109 + "[| f : multiset; g : multiset |] ==> \
1.110 +\ (Abs_multiset f = Abs_multiset g) = (!x. f x = g x)";
1.111 +by (rtac iffI 1);
1.112 + by (dres_inst_tac [("f","Rep_multiset")] arg_cong 1);
1.113 + by (Asm_full_simp_tac 1);
1.114 +by (subgoal_tac "f = g" 1);
1.115 + by (Asm_simp_tac 1);
1.116 +by (rtac ext 1);
1.117 +by (Blast_tac 1);
1.118 +qed "Abs_multiset_eq";
1.120 +*)
1.121 +
1.122 +
1.123 +subsection {* Algebraic properties of multisets *}
1.124 +
1.125 +subsubsection {* Union *}
1.126 +
1.127 +theorem union_empty [simp]: "M + {#} = M \<and> {#} + M = M"
1.128 +  apply (simp add: union_def Mempty_def)
1.129 +  done
1.130 +
1.131 +theorem union_commute: "M + N = N + (M::'a multiset)"
1.133 +  done
1.134 +
1.135 +theorem union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
1.137 +  done
1.138 +
1.139 +theorem union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
1.140 +  apply (rule union_commute [THEN trans])
1.141 +  apply (rule union_assoc [THEN trans])
1.142 +  apply (rule union_commute [THEN arg_cong])
1.143 +  done
1.144 +
1.145 +theorems union_ac = union_assoc union_commute union_lcomm
1.146 +
1.147 +
1.148 +subsubsection {* Difference *}
1.149 +
1.150 +theorem diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
1.151 +  apply (simp add: Mempty_def diff_def)
1.152 +  done
1.153 +
1.154 +theorem diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M"
1.155 +  apply (simp add: union_def diff_def)
1.156 +  done
1.157 +
1.158 +
1.159 +subsubsection {* Count of elements *}
1.160 +
1.161 +theorem count_empty [simp]: "count {#} a = 0"
1.162 +  apply (simp add: count_def Mempty_def)
1.163 +  done
1.164 +
1.165 +theorem count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
1.166 +  apply (simp add: count_def single_def)
1.167 +  done
1.168 +
1.169 +theorem count_union [simp]: "count (M + N) a = count M a + count N a"
1.170 +  apply (simp add: count_def union_def)
1.171 +  done
1.172 +
1.173 +theorem count_diff [simp]: "count (M - N) a = count M a - count N a"
1.174 +  apply (simp add: count_def diff_def)
1.175 +  done
1.176 +
1.177 +
1.178 +subsubsection {* Set of elements *}
1.179 +
1.180 +theorem set_of_empty [simp]: "set_of {#} = {}"
1.181 +  apply (simp add: set_of_def)
1.182 +  done
1.183 +
1.184 +theorem set_of_single [simp]: "set_of {#b#} = {b}"
1.185 +  apply (simp add: set_of_def)
1.186 +  done
1.187 +
1.188 +theorem set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
1.189 +  apply (auto simp add: set_of_def)
1.190 +  done
1.191 +
1.192 +theorem set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
1.193 +  apply (auto simp add: set_of_def Mempty_def count_def expand_fun_eq)
1.194 +  done
1.195 +
1.196 +theorem mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
1.197 +  apply (auto simp add: set_of_def)
1.198 +  done
1.199 +
1.200 +
1.201 +subsubsection {* Size *}
1.202 +
1.203 +theorem size_empty [simp]: "size {#} = 0"
1.204 +  apply (simp add: size_def)
1.205 +  done
1.206 +
1.207 +theorem size_single [simp]: "size {#b#} = 1"
1.208 +  apply (simp add: size_def)
1.209 +  done
1.210 +
1.211 +theorem finite_set_of [iff]: "finite (set_of M)"
1.212 +  apply (cut_tac x = M in Rep_multiset)
1.213 +  apply (simp add: multiset_def set_of_def count_def)
1.214 +  done
1.215 +
1.216 +theorem setsum_count_Int:
1.217 +    "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
1.218 +  apply (erule finite_induct)
1.219 +   apply simp
1.220 +  apply (simp add: Int_insert_left set_of_def)
1.221 +  done
1.222 +
1.223 +theorem size_union [simp]: "size (M + N::'a multiset) = size M + size N"
1.224 +  apply (unfold size_def)
1.225 +  apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
1.226 +   prefer 2
1.227 +   apply (rule ext)
1.228 +   apply simp
1.230 +  apply (subst Int_commute)
1.231 +  apply (simp (no_asm_simp) add: setsum_count_Int)
1.232 +  done
1.233 +
1.234 +theorem size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
1.235 +  apply (unfold size_def Mempty_def count_def)
1.236 +  apply auto
1.237 +  apply (simp add: set_of_def count_def expand_fun_eq)
1.238 +  done
1.239 +
1.240 +theorem size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
1.241 +  apply (unfold size_def)
1.242 +  apply (drule setsum_SucD)
1.243 +  apply auto
1.244 +  done
1.245 +
1.246 +
1.247 +subsubsection {* Equality of multisets *}
1.248 +
1.249 +theorem multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)"
1.250 +  apply (simp add: count_def expand_fun_eq)
1.251 +  done
1.252 +
1.253 +theorem single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
1.254 +  apply (simp add: single_def Mempty_def expand_fun_eq)
1.255 +  done
1.256 +
1.257 +theorem single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)"
1.258 +  apply (auto simp add: single_def expand_fun_eq)
1.259 +  done
1.260 +
1.261 +theorem union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})"
1.262 +  apply (auto simp add: union_def Mempty_def expand_fun_eq)
1.263 +  done
1.264 +
1.265 +theorem empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})"
1.266 +  apply (auto simp add: union_def Mempty_def expand_fun_eq)
1.267 +  done
1.268 +
1.269 +theorem union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))"
1.270 +  apply (simp add: union_def expand_fun_eq)
1.271 +  done
1.272 +
1.273 +theorem union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))"
1.274 +  apply (simp add: union_def expand_fun_eq)
1.275 +  done
1.276 +
1.277 +theorem union_is_single:
1.278 +    "(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})"
1.279 +  apply (unfold Mempty_def single_def union_def)
1.281 +  apply blast
1.282 +  done
1.283 +
1.284 +theorem single_is_union:
1.285 +  "({#a#} = M + N) =
1.286 +    ({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)"
1.287 +  apply (unfold Mempty_def single_def union_def)
1.289 +  apply (blast dest: sym)
1.290 +  done
1.291 +
1.293 +  "(M + {#a#} = N + {#b#}) =
1.294 +    (M = N \<and> a = b \<or>
1.295 +      M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})"
1.296 +  apply (unfold single_def union_def diff_def)
1.297 +  apply (simp (no_asm) add: expand_fun_eq)
1.298 +  apply (rule conjI)
1.299 +   apply force
1.300 +  apply clarify
1.301 +  apply (rule conjI)
1.302 +   apply blast
1.303 +  apply clarify
1.304 +  apply (rule iffI)
1.305 +   apply (rule conjI)
1.306 +    apply clarify
1.307 +    apply (rule conjI)
1.308 +     apply (simp add: eq_sym_conv)   (* FIXME blast fails !? *)
1.309 +    apply fast
1.310 +   apply simp
1.311 +  apply force
1.312 +  done
1.313 +
1.314 +(*
1.315 +val prems = Goal
1.316 + "[| !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> finite F --> P F";
1.317 +by (res_inst_tac [("a","F"),("f","\<lambda>A. if finite A then card A else 0")]
1.318 +     measure_induct 1);
1.319 +by (Clarify_tac 1);
1.320 +by (resolve_tac prems 1);
1.321 + by (assume_tac 1);
1.322 +by (Clarify_tac 1);
1.323 +by (subgoal_tac "finite G" 1);
1.324 + by (fast_tac (claset() addDs [finite_subset,order_less_le RS iffD1]) 2);
1.325 +by (etac allE 1);
1.326 +by (etac impE 1);
1.327 + by (Blast_tac 2);
1.328 +by (asm_simp_tac (simpset() addsimps [psubset_card]) 1);
1.329 +no_qed();
1.330 +val lemma = result();
1.331 +
1.332 +val prems = Goal
1.333 + "[| finite F; !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> P F";
1.334 +by (rtac (lemma RS mp) 1);
1.335 +by (REPEAT(ares_tac prems 1));
1.336 +qed "finite_psubset_induct";
1.337 +
1.338 +Better: use wf_finite_psubset in WF_Rel
1.339 +*)
1.340 +
1.341 +
1.342 +subsection {* Induction over multisets *}
1.343 +
1.344 +lemma setsum_decr:
1.345 +  "finite F ==> 0 < f a ==>
1.346 +    setsum (f (a := f a - 1)) F = (if a \<in> F then setsum f F - 1 else setsum f F)"
1.347 +  apply (erule finite_induct)
1.348 +   apply auto
1.349 +  apply (drule_tac a = a in mk_disjoint_insert)
1.350 +  apply auto
1.351 +  done
1.352 +
1.353 +lemma Rep_multiset_induct_aux:
1.354 +  "P (\<lambda>a. 0) ==> (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1)))
1.355 +    ==> \<forall>f. f \<in> multiset --> setsum f {x. 0 < f x} = n --> P f"
1.356 +proof -
1.357 +  case antecedent
1.358 +  note prems = this [unfolded multiset_def]
1.359 +  show ?thesis
1.360 +    apply (unfold multiset_def)
1.361 +    apply (induct_tac n)
1.362 +     apply simp
1.363 +     apply clarify
1.364 +     apply (subgoal_tac "f = (\<lambda>a.0)")
1.365 +      apply simp
1.366 +      apply (rule prems)
1.367 +     apply (rule ext)
1.368 +     apply force
1.369 +    apply clarify
1.370 +    apply (frule setsum_SucD)
1.371 +    apply clarify
1.372 +    apply (rename_tac a)
1.373 +    apply (subgoal_tac "finite {x. 0 < (f (a := f a - 1)) x}")
1.374 +     prefer 2
1.375 +     apply (rule finite_subset)
1.376 +      prefer 2
1.377 +      apply assumption
1.378 +     apply simp
1.379 +     apply blast
1.380 +    apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
1.381 +     prefer 2
1.382 +     apply (rule ext)
1.383 +     apply (simp (no_asm_simp))
1.384 +     apply (erule ssubst, rule prems)
1.385 +     apply blast
1.386 +    apply (erule allE, erule impE, erule_tac [2] mp)
1.387 +     apply blast
1.388 +    apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply)
1.389 +    apply (subgoal_tac "{x. x \<noteq> a --> 0 < f x} = {x. 0 < f x}")
1.390 +     prefer 2
1.391 +     apply blast
1.392 +    apply (subgoal_tac "{x. x \<noteq> a \<and> 0 < f x} = {x. 0 < f x} - {a}")
1.393 +     prefer 2
1.394 +     apply blast
1.396 +    done
1.397 +qed
1.398 +
1.399 +theorem Rep_multiset_induct:
1.400 +  "f \<in> multiset ==> P (\<lambda>a. 0) ==>
1.401 +    (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
1.402 +  apply (insert Rep_multiset_induct_aux)
1.403 +  apply blast
1.404 +  done
1.405 +
1.406 +theorem multiset_induct [induct type: multiset]:
1.407 +  "P {#} ==> (!!M x. P M ==> P (M + {#x#})) ==> P M"
1.408 +proof -
1.409 +  note defns = union_def single_def Mempty_def
1.410 +  assume prem1 [unfolded defns]: "P {#}"
1.411 +  assume prem2 [unfolded defns]: "!!M x. P M ==> P (M + {#x#})"
1.412 +  show ?thesis
1.413 +    apply (rule Rep_multiset_inverse [THEN subst])
1.414 +    apply (rule Rep_multiset [THEN Rep_multiset_induct])
1.415 +     apply (rule prem1)
1.416 +    apply (subgoal_tac "f (b := f b + 1) = (\<lambda>a. f a + (if a = b then 1 else 0))")
1.417 +     prefer 2
1.418 +     apply (simp add: expand_fun_eq)
1.419 +    apply (erule ssubst)
1.420 +    apply (erule Abs_multiset_inverse [THEN subst])
1.421 +    apply (erule prem2 [simplified])
1.422 +    done
1.423 +qed
1.424 +
1.425 +
1.426 +lemma MCollect_preserves_multiset:
1.427 +    "M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset"
1.428 +  apply (simp add: multiset_def)
1.429 +  apply (rule finite_subset)
1.430 +   apply auto
1.431 +  done
1.432 +
1.433 +theorem count_MCollect [simp]:
1.434 +    "count {# x:M. P x #} a = (if P a then count M a else 0)"
1.435 +  apply (unfold count_def MCollect_def)
1.436 +  apply (simp add: MCollect_preserves_multiset)
1.437 +  done
1.438 +
1.439 +theorem set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}"
1.440 +  apply (auto simp add: set_of_def)
1.441 +  done
1.442 +
1.443 +theorem multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}"
1.444 +  apply (subst multiset_eq_conv_count_eq)
1.445 +  apply auto
1.446 +  done
1.447 +
1.448 +declare multiset_eq_conv_Rep_eq [simp del]
1.449 +declare multiset_typedef [simp del]
1.450 +
1.452 +  "(M + {#a#} = N + {#b#}) =
1.453 +    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
1.455 +  done
1.456 +
1.457 +
1.458 +subsection {* Multiset orderings *}
1.459 +
1.460 +subsubsection {* Well-foundedness *}
1.461 +
1.462 +constdefs
1.463 +  mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
1.464 +  "mult1 r ==
1.465 +    {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
1.466 +      (\<forall>b. b :# K --> (b, a) \<in> r)}"
1.467 +
1.468 +  mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
1.469 +  "mult r == (mult1 r)^+"
1.470 +
1.471 +lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
1.472 +  apply (simp add: mult1_def)
1.473 +  done
1.474 +
1.475 +lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
1.476 +    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
1.477 +    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
1.478 +  (concl is "?case1 (mult1 r) \<or> ?case2")
1.479 +proof (unfold mult1_def)
1.480 +  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
1.481 +  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
1.482 +  let ?case1 = "?case1 {(N, M). ?R N M}"
1.483 +
1.484 +  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
1.485 +  hence "\<exists>a' M0' K.
1.486 +      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
1.487 +  thus "?case1 \<or> ?case2"
1.488 +  proof (elim exE conjE)
1.489 +    fix a' M0' K
1.490 +    assume N: "N = M0' + K" and r: "?r K a'"
1.491 +    assume "M0 + {#a#} = M0' + {#a'#}"
1.492 +    hence "M0 = M0' \<and> a = a' \<or>
1.493 +        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
1.494 +      by (simp only: add_eq_conv_ex)
1.495 +    thus ?thesis
1.496 +    proof (elim disjE conjE exE)
1.497 +      assume "M0 = M0'" "a = a'"
1.498 +      with N r have "?r K a \<and> N = M0 + K" by simp
1.499 +      hence ?case2 .. thus ?thesis ..
1.500 +    next
1.501 +      fix K'
1.502 +      assume "M0' = K' + {#a#}"
1.503 +      with N have n: "N = K' + K + {#a#}" by (simp add: union_ac)
1.504 +
1.505 +      assume "M0 = K' + {#a'#}"
1.506 +      with r have "?R (K' + K) M0" by blast
1.507 +      with n have ?case1 by simp thus ?thesis ..
1.508 +    qed
1.509 +  qed
1.510 +qed
1.511 +
1.512 +lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
1.513 +proof
1.514 +  let ?R = "mult1 r"
1.515 +  let ?W = "acc ?R"
1.516 +  {
1.517 +    fix M M0 a
1.518 +    assume M0: "M0 \<in> ?W"
1.519 +      and wf_hyp: "\<forall>b. (b, a) \<in> r --> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
1.520 +      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
1.521 +    have "M0 + {#a#} \<in> ?W"
1.522 +    proof (rule accI [of "M0 + {#a#}"])
1.523 +      fix N
1.524 +      assume "(N, M0 + {#a#}) \<in> ?R"
1.525 +      hence "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
1.526 +          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
1.528 +      thus "N \<in> ?W"
1.529 +      proof (elim exE disjE conjE)
1.530 +        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
1.531 +        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
1.532 +        hence "M + {#a#} \<in> ?W" ..
1.533 +        thus "N \<in> ?W" by (simp only: N)
1.534 +      next
1.535 +        fix K
1.536 +        assume N: "N = M0 + K"
1.537 +        assume "\<forall>b. b :# K --> (b, a) \<in> r"
1.538 +        have "?this --> M0 + K \<in> ?W" (is "?P K")
1.539 +        proof (induct K)
1.540 +          from M0 have "M0 + {#} \<in> ?W" by simp
1.541 +          thus "?P {#}" ..
1.542 +
1.543 +          fix K x assume hyp: "?P K"
1.544 +          show "?P (K + {#x#})"
1.545 +          proof
1.546 +            assume a: "\<forall>b. b :# (K + {#x#}) --> (b, a) \<in> r"
1.547 +            hence "(x, a) \<in> r" by simp
1.548 +            with wf_hyp have b: "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
1.549 +
1.550 +            from a hyp have "M0 + K \<in> ?W" by simp
1.551 +            with b have "(M0 + K) + {#x#} \<in> ?W" ..
1.552 +            thus "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc)
1.553 +          qed
1.554 +        qed
1.555 +        hence "M0 + K \<in> ?W" ..
1.556 +        thus "N \<in> ?W" by (simp only: N)
1.557 +      qed
1.558 +    qed
1.559 +  } note tedious_reasoning = this
1.560 +
1.561 +  assume wf: "wf r"
1.562 +  fix M
1.563 +  show "M \<in> ?W"
1.564 +  proof (induct M)
1.565 +    show "{#} \<in> ?W"
1.566 +    proof (rule accI)
1.567 +      fix b assume "(b, {#}) \<in> ?R"
1.568 +      with not_less_empty show "b \<in> ?W" by contradiction
1.569 +    qed
1.570 +
1.571 +    fix M a assume "M \<in> ?W"
1.572 +    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
1.573 +    proof induct
1.574 +      fix a
1.575 +      assume "\<forall>b. (b, a) \<in> r --> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
1.576 +      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
1.577 +      proof
1.578 +        fix M assume "M \<in> ?W"
1.579 +        thus "M + {#a#} \<in> ?W"
1.580 +          by (rule acc_induct) (rule tedious_reasoning)
1.581 +      qed
1.582 +    qed
1.583 +    thus "M + {#a#} \<in> ?W" ..
1.584 +  qed
1.585 +qed
1.586 +
1.587 +theorem wf_mult1: "wf r ==> wf (mult1 r)"
1.588 +  by (rule acc_wfI, rule all_accessible)
1.589 +
1.590 +theorem wf_mult: "wf r ==> wf (mult r)"
1.591 +  by (unfold mult_def, rule wf_trancl, rule wf_mult1)
1.592 +
1.593 +
1.594 +subsubsection {* Closure-free presentation *}
1.595 +
1.596 +(*Badly needed: a linear arithmetic procedure for multisets*)
1.597 +
1.598 +lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})"
1.599 +  apply (simp add: multiset_eq_conv_count_eq)
1.600 +  done
1.601 +
1.602 +text {* One direction. *}
1.603 +
1.604 +lemma mult_implies_one_step:
1.605 +  "trans r ==> (M, N) \<in> mult r ==>
1.606 +    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
1.607 +    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
1.608 +  apply (unfold mult_def mult1_def set_of_def)
1.609 +  apply (erule converse_trancl_induct)
1.610 +  apply clarify
1.611 +   apply (rule_tac x = M0 in exI)
1.612 +   apply simp
1.613 +  apply clarify
1.614 +  apply (case_tac "a :# K")
1.615 +   apply (rule_tac x = I in exI)
1.616 +   apply (simp (no_asm))
1.617 +   apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
1.618 +   apply (simp (no_asm_simp) add: union_assoc [symmetric])
1.619 +   apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
1.620 +   apply (simp add: diff_union_single_conv)
1.621 +   apply (simp (no_asm_use) add: trans_def)
1.622 +   apply blast
1.623 +  apply (subgoal_tac "a :# I")
1.624 +   apply (rule_tac x = "I - {#a#}" in exI)
1.625 +   apply (rule_tac x = "J + {#a#}" in exI)
1.626 +   apply (rule_tac x = "K + Ka" in exI)
1.627 +   apply (rule conjI)
1.628 +    apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
1.629 +   apply (rule conjI)
1.630 +    apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
1.631 +    apply simp
1.632 +    apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
1.633 +   apply (simp (no_asm_use) add: trans_def)
1.634 +   apply blast
1.635 +  apply (subgoal_tac "a :# (M0 +{#a#})")
1.636 +   apply simp
1.637 +  apply (simp (no_asm))
1.638 +  done
1.639 +
1.640 +lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}"
1.641 +  apply (simp add: multiset_eq_conv_count_eq)
1.642 +  done
1.643 +
1.644 +lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}"
1.645 +  apply (erule size_eq_Suc_imp_elem [THEN exE])
1.646 +  apply (drule elem_imp_eq_diff_union)
1.647 +  apply auto
1.648 +  done
1.649 +
1.650 +lemma one_step_implies_mult_aux:
1.651 +  "trans r ==>
1.652 +    \<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))
1.653 +      --> (I + K, I + J) \<in> mult r"
1.654 +  apply (induct_tac n)
1.655 +   apply auto
1.656 +  apply (frule size_eq_Suc_imp_eq_union)
1.657 +  apply clarify
1.658 +  apply (rename_tac "J'")
1.659 +  apply simp
1.660 +  apply (erule notE)
1.661 +   apply auto
1.662 +  apply (case_tac "J' = {#}")
1.663 +   apply (simp add: mult_def)
1.664 +   apply (rule r_into_trancl)
1.665 +   apply (simp add: mult1_def set_of_def)
1.666 +   apply blast
1.667 +  txt {* Now we know @{term "J' \<noteq> {#}"}. *}
1.668 +  apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
1.669 +  apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
1.670 +  apply (erule ssubst)
1.671 +  apply (simp add: Ball_def)
1.672 +  apply auto
1.673 +  apply (subgoal_tac
1.674 +    "((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #},
1.675 +      (I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r")
1.676 +   prefer 2
1.677 +   apply force
1.678 +  apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def)
1.679 +  apply (erule trancl_trans)
1.680 +  apply (rule r_into_trancl)
1.681 +  apply (simp add: mult1_def set_of_def)
1.682 +  apply (rule_tac x = a in exI)
1.683 +  apply (rule_tac x = "I + J'" in exI)
1.684 +  apply (simp add: union_ac)
1.685 +  done
1.686 +
1.687 +theorem one_step_implies_mult:
1.688 +  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
1.689 +    ==> (I + K, I + J) \<in> mult r"
1.690 +  apply (insert one_step_implies_mult_aux)
1.691 +  apply blast
1.692 +  done
1.693 +
1.694 +
1.695 +subsubsection {* Partial-order properties *}
1.696 +
1.697 +instance multiset :: ("term") ord by intro_classes
1.698 +
1.700 +  less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}"
1.701 +  le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)"
1.702 +
1.703 +lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}"
1.704 +  apply (unfold trans_def)
1.705 +  apply (blast intro: order_less_trans)
1.706 +  done
1.707 +
1.708 +text {*
1.709 + \medskip Irreflexivity.
1.710 +*}
1.711 +
1.712 +lemma mult_irrefl_aux:
1.713 +    "finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) --> A = {}"
1.714 +  apply (erule finite_induct)
1.715 +   apply (auto intro: order_less_trans)
1.716 +  done
1.717 +
1.718 +theorem mult_less_not_refl: "\<not> M < (M::'a::order multiset)"
1.719 +  apply (unfold less_multiset_def)
1.720 +  apply auto
1.721 +  apply (drule trans_base_order [THEN mult_implies_one_step])
1.722 +  apply auto
1.723 +  apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]])
1.724 +  apply (simp add: set_of_eq_empty_iff)
1.725 +  done
1.726 +
1.727 +lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R"
1.728 +  apply (insert mult_less_not_refl)
1.729 +  apply blast
1.730 +  done
1.731 +
1.732 +
1.733 +text {* Transitivity. *}
1.734 +
1.735 +theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)"
1.736 +  apply (unfold less_multiset_def mult_def)
1.737 +  apply (blast intro: trancl_trans)
1.738 +  done
1.739 +
1.740 +text {* Asymmetry. *}
1.741 +
1.742 +theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)"
1.743 +  apply auto
1.744 +  apply (rule mult_less_not_refl [THEN notE])
1.745 +  apply (erule mult_less_trans)
1.746 +  apply assumption
1.747 +  done
1.748 +
1.749 +theorem mult_less_asym:
1.750 +    "M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P"
1.751 +  apply (insert mult_less_not_sym)
1.752 +  apply blast
1.753 +  done
1.754 +
1.755 +theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)"
1.756 +  apply (unfold le_multiset_def)
1.757 +  apply auto
1.758 +  done
1.759 +
1.760 +text {* Anti-symmetry. *}
1.761 +
1.762 +theorem mult_le_antisym:
1.763 +    "M <= N ==> N <= M ==> M = (N::'a::order multiset)"
1.764 +  apply (unfold le_multiset_def)
1.765 +  apply (blast dest: mult_less_not_sym)
1.766 +  done
1.767 +
1.768 +text {* Transitivity. *}
1.769 +
1.770 +theorem mult_le_trans:
1.771 +    "K <= M ==> M <= N ==> K <= (N::'a::order multiset)"
1.772 +  apply (unfold le_multiset_def)
1.773 +  apply (blast intro: mult_less_trans)
1.774 +  done
1.775 +
1.776 +theorem mult_less_le: "M < N = (M <= N \<and> M \<noteq> (N::'a::order multiset))"
1.777 +  apply (unfold le_multiset_def)
1.778 +  apply auto
1.779 +  done
1.780 +
1.781 +
1.782 +subsubsection {* Monotonicity of multiset union *}
1.783 +
1.784 +theorem mult1_union:
1.785 +    "(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r"
1.786 +  apply (unfold mult1_def)
1.787 +  apply auto
1.788 +  apply (rule_tac x = a in exI)
1.789 +  apply (rule_tac x = "C + M0" in exI)
1.790 +  apply (simp add: union_assoc)
1.791 +  done
1.792 +
1.793 +lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)"
1.794 +  apply (unfold less_multiset_def mult_def)
1.795 +  apply (erule trancl_induct)
1.796 +   apply (blast intro: mult1_union transI order_less_trans r_into_trancl)
1.797 +  apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
1.798 +  done
1.799 +
1.800 +lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)"
1.801 +  apply (subst union_commute [of B C])
1.802 +  apply (subst union_commute [of D C])
1.803 +  apply (erule union_less_mono2)
1.804 +  done
1.805 +
1.806 +theorem union_less_mono:
1.807 +    "A < C ==> B < D ==> A + B < C + (D::'a::order multiset)"
1.808 +  apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans)
1.809 +  done
1.810 +
1.811 +theorem union_le_mono:
1.812 +    "A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)"
1.813 +  apply (unfold le_multiset_def)
1.814 +  apply (blast intro: union_less_mono union_less_mono1 union_less_mono2)
1.815 +  done
1.816 +
1.817 +theorem empty_leI [iff]: "{#} <= (M::'a::order multiset)"
1.818 +  apply (unfold le_multiset_def less_multiset_def)
1.819 +  apply (case_tac "M = {#}")
1.820 +   prefer 2
1.821 +   apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))")
1.822 +    prefer 2
1.823 +    apply (rule one_step_implies_mult)
1.824 +      apply (simp only: trans_def)
1.825 +      apply auto
1.826 +  apply (blast intro: order_less_trans)
1.827 +  done
1.828 +
1.829 +theorem union_upper1: "A <= A + (B::'a::order multiset)"
1.830 +  apply (subgoal_tac "A + {#} <= A + B")
1.831 +   prefer 2
1.832 +   apply (rule union_le_mono)
1.833 +    apply auto
1.834 +  done
1.835 +
1.836 +theorem union_upper2: "B <= A + (B::'a::order multiset)"
1.837 +  apply (subst union_commute, rule union_upper1)
1.838 +  done
1.839 +
1.840 +instance multiset :: (order) order
1.841 +  apply intro_classes
1.842 +     apply (rule mult_le_refl)
1.843 +    apply (erule mult_le_trans)
1.844 +    apply assumption
1.845 +   apply (erule mult_le_antisym)
1.846 +   apply assumption
1.847 +  apply (rule mult_less_le)
1.848 +  done
1.849 +
1.850 +instance multiset :: ("term") plus_ac0
1.851 +  apply intro_classes
1.852 +    apply (rule union_commute)
1.853 +   apply (rule union_assoc)
1.854 +  apply simp
1.855 +  done
1.856 +
1.857 +end
```