src/HOL/Library/Multiset.thy
 author nipkow Mon Sep 13 11:13:15 2010 +0200 (2010-09-13) changeset 39302 d7728f65b353 parent 39301 e1bd8a54c40f child 39314 aecb239a2bbc permissions -rw-r--r--
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
```     1 (*  Title:      HOL/Library/Multiset.thy
```
```     2     Author:     Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
```
```     3 *)
```
```     4
```
```     5 header {* (Finite) multisets *}
```
```     6
```
```     7 theory Multiset
```
```     8 imports Main
```
```     9 begin
```
```    10
```
```    11 subsection {* The type of multisets *}
```
```    12
```
```    13 typedef 'a multiset = "{f :: 'a => nat. finite {x. f x > 0}}"
```
```    14   morphisms count Abs_multiset
```
```    15 proof
```
```    16   show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp
```
```    17 qed
```
```    18
```
```    19 lemmas multiset_typedef = Abs_multiset_inverse count_inverse count
```
```    20
```
```    21 abbreviation Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
```
```    22   "a :# M == 0 < count M a"
```
```    23
```
```    24 notation (xsymbols)
```
```    25   Melem (infix "\<in>#" 50)
```
```    26
```
```    27 lemma multiset_eq_iff:
```
```    28   "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
```
```    29   by (simp only: count_inject [symmetric] fun_eq_iff)
```
```    30
```
```    31 lemma multiset_eqI:
```
```    32   "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
```
```    33   using multiset_eq_iff by auto
```
```    34
```
```    35 text {*
```
```    36  \medskip Preservation of the representing set @{term multiset}.
```
```    37 *}
```
```    38
```
```    39 lemma const0_in_multiset:
```
```    40   "(\<lambda>a. 0) \<in> multiset"
```
```    41   by (simp add: multiset_def)
```
```    42
```
```    43 lemma only1_in_multiset:
```
```    44   "(\<lambda>b. if b = a then n else 0) \<in> multiset"
```
```    45   by (simp add: multiset_def)
```
```    46
```
```    47 lemma union_preserves_multiset:
```
```    48   "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
```
```    49   by (simp add: multiset_def)
```
```    50
```
```    51 lemma diff_preserves_multiset:
```
```    52   assumes "M \<in> multiset"
```
```    53   shows "(\<lambda>a. M a - N a) \<in> multiset"
```
```    54 proof -
```
```    55   have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
```
```    56     by auto
```
```    57   with assms show ?thesis
```
```    58     by (auto simp add: multiset_def intro: finite_subset)
```
```    59 qed
```
```    60
```
```    61 lemma MCollect_preserves_multiset:
```
```    62   assumes "M \<in> multiset"
```
```    63   shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
```
```    64 proof -
```
```    65   have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
```
```    66     by auto
```
```    67   with assms show ?thesis
```
```    68     by (auto simp add: multiset_def intro: finite_subset)
```
```    69 qed
```
```    70
```
```    71 lemmas in_multiset = const0_in_multiset only1_in_multiset
```
```    72   union_preserves_multiset diff_preserves_multiset MCollect_preserves_multiset
```
```    73
```
```    74
```
```    75 subsection {* Representing multisets *}
```
```    76
```
```    77 text {* Multiset comprehension *}
```
```    78
```
```    79 definition MCollect :: "'a multiset => ('a => bool) => 'a multiset" where
```
```    80   "MCollect M P = Abs_multiset (\<lambda>x. if P x then count M x else 0)"
```
```    81
```
```    82 syntax
```
```    83   "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ :# _./ _#})")
```
```    84 translations
```
```    85   "{#x :# M. P#}" == "CONST MCollect M (\<lambda>x. P)"
```
```    86
```
```    87
```
```    88 text {* Multiset enumeration *}
```
```    89
```
```    90 instantiation multiset :: (type) "{zero, plus}"
```
```    91 begin
```
```    92
```
```    93 definition Mempty_def:
```
```    94   "0 = Abs_multiset (\<lambda>a. 0)"
```
```    95
```
```    96 abbreviation Mempty :: "'a multiset" ("{#}") where
```
```    97   "Mempty \<equiv> 0"
```
```    98
```
```    99 definition union_def:
```
```   100   "M + N = Abs_multiset (\<lambda>a. count M a + count N a)"
```
```   101
```
```   102 instance ..
```
```   103
```
```   104 end
```
```   105
```
```   106 definition single :: "'a => 'a multiset" where
```
```   107   "single a = Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
```
```   108
```
```   109 syntax
```
```   110   "_multiset" :: "args => 'a multiset"    ("{#(_)#}")
```
```   111 translations
```
```   112   "{#x, xs#}" == "{#x#} + {#xs#}"
```
```   113   "{#x#}" == "CONST single x"
```
```   114
```
```   115 lemma count_empty [simp]: "count {#} a = 0"
```
```   116   by (simp add: Mempty_def in_multiset multiset_typedef)
```
```   117
```
```   118 lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
```
```   119   by (simp add: single_def in_multiset multiset_typedef)
```
```   120
```
```   121
```
```   122 subsection {* Basic operations *}
```
```   123
```
```   124 subsubsection {* Union *}
```
```   125
```
```   126 lemma count_union [simp]: "count (M + N) a = count M a + count N a"
```
```   127   by (simp add: union_def in_multiset multiset_typedef)
```
```   128
```
```   129 instance multiset :: (type) cancel_comm_monoid_add proof
```
```   130 qed (simp_all add: multiset_eq_iff)
```
```   131
```
```   132
```
```   133 subsubsection {* Difference *}
```
```   134
```
```   135 instantiation multiset :: (type) minus
```
```   136 begin
```
```   137
```
```   138 definition diff_def:
```
```   139   "M - N = Abs_multiset (\<lambda>a. count M a - count N a)"
```
```   140
```
```   141 instance ..
```
```   142
```
```   143 end
```
```   144
```
```   145 lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
```
```   146   by (simp add: diff_def in_multiset multiset_typedef)
```
```   147
```
```   148 lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
```
```   149 by(simp add: multiset_eq_iff)
```
```   150
```
```   151 lemma diff_cancel[simp]: "A - A = {#}"
```
```   152 by (rule multiset_eqI) simp
```
```   153
```
```   154 lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
```
```   155 by(simp add: multiset_eq_iff)
```
```   156
```
```   157 lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
```
```   158 by(simp add: multiset_eq_iff)
```
```   159
```
```   160 lemma insert_DiffM:
```
```   161   "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
```
```   162   by (clarsimp simp: multiset_eq_iff)
```
```   163
```
```   164 lemma insert_DiffM2 [simp]:
```
```   165   "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
```
```   166   by (clarsimp simp: multiset_eq_iff)
```
```   167
```
```   168 lemma diff_right_commute:
```
```   169   "(M::'a multiset) - N - Q = M - Q - N"
```
```   170   by (auto simp add: multiset_eq_iff)
```
```   171
```
```   172 lemma diff_add:
```
```   173   "(M::'a multiset) - (N + Q) = M - N - Q"
```
```   174 by (simp add: multiset_eq_iff)
```
```   175
```
```   176 lemma diff_union_swap:
```
```   177   "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
```
```   178   by (auto simp add: multiset_eq_iff)
```
```   179
```
```   180 lemma diff_union_single_conv:
```
```   181   "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
```
```   182   by (simp add: multiset_eq_iff)
```
```   183
```
```   184
```
```   185 subsubsection {* Equality of multisets *}
```
```   186
```
```   187 lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
```
```   188   by (simp add: multiset_eq_iff)
```
```   189
```
```   190 lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
```
```   191   by (auto simp add: multiset_eq_iff)
```
```   192
```
```   193 lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
```
```   194   by (auto simp add: multiset_eq_iff)
```
```   195
```
```   196 lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
```
```   197   by (auto simp add: multiset_eq_iff)
```
```   198
```
```   199 lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
```
```   200   by (auto simp add: multiset_eq_iff)
```
```   201
```
```   202 lemma diff_single_trivial:
```
```   203   "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
```
```   204   by (auto simp add: multiset_eq_iff)
```
```   205
```
```   206 lemma diff_single_eq_union:
```
```   207   "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
```
```   208   by auto
```
```   209
```
```   210 lemma union_single_eq_diff:
```
```   211   "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
```
```   212   by (auto dest: sym)
```
```   213
```
```   214 lemma union_single_eq_member:
```
```   215   "M + {#x#} = N \<Longrightarrow> x \<in># N"
```
```   216   by auto
```
```   217
```
```   218 lemma union_is_single:
```
```   219   "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}" (is "?lhs = ?rhs")proof
```
```   220   assume ?rhs then show ?lhs by auto
```
```   221 next
```
```   222   assume ?lhs thus ?rhs
```
```   223     by(simp add: multiset_eq_iff split:if_splits) (metis add_is_1)
```
```   224 qed
```
```   225
```
```   226 lemma single_is_union:
```
```   227   "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
```
```   228   by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
```
```   229
```
```   230 lemma add_eq_conv_diff:
```
```   231   "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"  (is "?lhs = ?rhs")
```
```   232 (* shorter: by (simp add: multiset_eq_iff) fastsimp *)
```
```   233 proof
```
```   234   assume ?rhs then show ?lhs
```
```   235   by (auto simp add: add_assoc add_commute [of "{#b#}"])
```
```   236     (drule sym, simp add: add_assoc [symmetric])
```
```   237 next
```
```   238   assume ?lhs
```
```   239   show ?rhs
```
```   240   proof (cases "a = b")
```
```   241     case True with `?lhs` show ?thesis by simp
```
```   242   next
```
```   243     case False
```
```   244     from `?lhs` have "a \<in># N + {#b#}" by (rule union_single_eq_member)
```
```   245     with False have "a \<in># N" by auto
```
```   246     moreover from `?lhs` have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
```
```   247     moreover note False
```
```   248     ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
```
```   249   qed
```
```   250 qed
```
```   251
```
```   252 lemma insert_noteq_member:
```
```   253   assumes BC: "B + {#b#} = C + {#c#}"
```
```   254    and bnotc: "b \<noteq> c"
```
```   255   shows "c \<in># B"
```
```   256 proof -
```
```   257   have "c \<in># C + {#c#}" by simp
```
```   258   have nc: "\<not> c \<in># {#b#}" using bnotc by simp
```
```   259   then have "c \<in># B + {#b#}" using BC by simp
```
```   260   then show "c \<in># B" using nc by simp
```
```   261 qed
```
```   262
```
```   263 lemma add_eq_conv_ex:
```
```   264   "(M + {#a#} = N + {#b#}) =
```
```   265     (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
```
```   266   by (auto simp add: add_eq_conv_diff)
```
```   267
```
```   268
```
```   269 subsubsection {* Pointwise ordering induced by count *}
```
```   270
```
```   271 instantiation multiset :: (type) ordered_ab_semigroup_add_imp_le
```
```   272 begin
```
```   273
```
```   274 definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
```
```   275   mset_le_def: "A \<le> B \<longleftrightarrow> (\<forall>a. count A a \<le> count B a)"
```
```   276
```
```   277 definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
```
```   278   mset_less_def: "(A::'a multiset) < B \<longleftrightarrow> A \<le> B \<and> A \<noteq> B"
```
```   279
```
```   280 instance proof
```
```   281 qed (auto simp add: mset_le_def mset_less_def multiset_eq_iff intro: order_trans antisym)
```
```   282
```
```   283 end
```
```   284
```
```   285 lemma mset_less_eqI:
```
```   286   "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le> B"
```
```   287   by (simp add: mset_le_def)
```
```   288
```
```   289 lemma mset_le_exists_conv:
```
```   290   "(A::'a multiset) \<le> B \<longleftrightarrow> (\<exists>C. B = A + C)"
```
```   291 apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI)
```
```   292 apply (auto intro: multiset_eq_iff [THEN iffD2])
```
```   293 done
```
```   294
```
```   295 lemma mset_le_mono_add_right_cancel [simp]:
```
```   296   "(A::'a multiset) + C \<le> B + C \<longleftrightarrow> A \<le> B"
```
```   297   by (fact add_le_cancel_right)
```
```   298
```
```   299 lemma mset_le_mono_add_left_cancel [simp]:
```
```   300   "C + (A::'a multiset) \<le> C + B \<longleftrightarrow> A \<le> B"
```
```   301   by (fact add_le_cancel_left)
```
```   302
```
```   303 lemma mset_le_mono_add:
```
```   304   "(A::'a multiset) \<le> B \<Longrightarrow> C \<le> D \<Longrightarrow> A + C \<le> B + D"
```
```   305   by (fact add_mono)
```
```   306
```
```   307 lemma mset_le_add_left [simp]:
```
```   308   "(A::'a multiset) \<le> A + B"
```
```   309   unfolding mset_le_def by auto
```
```   310
```
```   311 lemma mset_le_add_right [simp]:
```
```   312   "B \<le> (A::'a multiset) + B"
```
```   313   unfolding mset_le_def by auto
```
```   314
```
```   315 lemma mset_le_single:
```
```   316   "a :# B \<Longrightarrow> {#a#} \<le> B"
```
```   317   by (simp add: mset_le_def)
```
```   318
```
```   319 lemma multiset_diff_union_assoc:
```
```   320   "C \<le> B \<Longrightarrow> (A::'a multiset) + B - C = A + (B - C)"
```
```   321   by (simp add: multiset_eq_iff mset_le_def)
```
```   322
```
```   323 lemma mset_le_multiset_union_diff_commute:
```
```   324   "B \<le> A \<Longrightarrow> (A::'a multiset) - B + C = A + C - B"
```
```   325 by (simp add: multiset_eq_iff mset_le_def)
```
```   326
```
```   327 lemma diff_le_self[simp]: "(M::'a multiset) - N \<le> M"
```
```   328 by(simp add: mset_le_def)
```
```   329
```
```   330 lemma mset_lessD: "A < B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
```
```   331 apply (clarsimp simp: mset_le_def mset_less_def)
```
```   332 apply (erule_tac x=x in allE)
```
```   333 apply auto
```
```   334 done
```
```   335
```
```   336 lemma mset_leD: "A \<le> B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
```
```   337 apply (clarsimp simp: mset_le_def mset_less_def)
```
```   338 apply (erule_tac x = x in allE)
```
```   339 apply auto
```
```   340 done
```
```   341
```
```   342 lemma mset_less_insertD: "(A + {#x#} < B) \<Longrightarrow> (x \<in># B \<and> A < B)"
```
```   343 apply (rule conjI)
```
```   344  apply (simp add: mset_lessD)
```
```   345 apply (clarsimp simp: mset_le_def mset_less_def)
```
```   346 apply safe
```
```   347  apply (erule_tac x = a in allE)
```
```   348  apply (auto split: split_if_asm)
```
```   349 done
```
```   350
```
```   351 lemma mset_le_insertD: "(A + {#x#} \<le> B) \<Longrightarrow> (x \<in># B \<and> A \<le> B)"
```
```   352 apply (rule conjI)
```
```   353  apply (simp add: mset_leD)
```
```   354 apply (force simp: mset_le_def mset_less_def split: split_if_asm)
```
```   355 done
```
```   356
```
```   357 lemma mset_less_of_empty[simp]: "A < {#} \<longleftrightarrow> False"
```
```   358   by (auto simp add: mset_less_def mset_le_def multiset_eq_iff)
```
```   359
```
```   360 lemma multi_psub_of_add_self[simp]: "A < A + {#x#}"
```
```   361   by (auto simp: mset_le_def mset_less_def)
```
```   362
```
```   363 lemma multi_psub_self[simp]: "(A::'a multiset) < A = False"
```
```   364   by simp
```
```   365
```
```   366 lemma mset_less_add_bothsides:
```
```   367   "T + {#x#} < S + {#x#} \<Longrightarrow> T < S"
```
```   368   by (fact add_less_imp_less_right)
```
```   369
```
```   370 lemma mset_less_empty_nonempty:
```
```   371   "{#} < S \<longleftrightarrow> S \<noteq> {#}"
```
```   372   by (auto simp: mset_le_def mset_less_def)
```
```   373
```
```   374 lemma mset_less_diff_self:
```
```   375   "c \<in># B \<Longrightarrow> B - {#c#} < B"
```
```   376   by (auto simp: mset_le_def mset_less_def multiset_eq_iff)
```
```   377
```
```   378
```
```   379 subsubsection {* Intersection *}
```
```   380
```
```   381 instantiation multiset :: (type) semilattice_inf
```
```   382 begin
```
```   383
```
```   384 definition inf_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
```
```   385   multiset_inter_def: "inf_multiset A B = A - (A - B)"
```
```   386
```
```   387 instance proof -
```
```   388   have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" by arith
```
```   389   show "OFCLASS('a multiset, semilattice_inf_class)" proof
```
```   390   qed (auto simp add: multiset_inter_def mset_le_def aux)
```
```   391 qed
```
```   392
```
```   393 end
```
```   394
```
```   395 abbreviation multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
```
```   396   "multiset_inter \<equiv> inf"
```
```   397
```
```   398 lemma multiset_inter_count:
```
```   399   "count (A #\<inter> B) x = min (count A x) (count B x)"
```
```   400   by (simp add: multiset_inter_def multiset_typedef)
```
```   401
```
```   402 lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
```
```   403   by (rule multiset_eqI) (auto simp add: multiset_inter_count)
```
```   404
```
```   405 lemma multiset_union_diff_commute:
```
```   406   assumes "B #\<inter> C = {#}"
```
```   407   shows "A + B - C = A - C + B"
```
```   408 proof (rule multiset_eqI)
```
```   409   fix x
```
```   410   from assms have "min (count B x) (count C x) = 0"
```
```   411     by (auto simp add: multiset_inter_count multiset_eq_iff)
```
```   412   then have "count B x = 0 \<or> count C x = 0"
```
```   413     by auto
```
```   414   then show "count (A + B - C) x = count (A - C + B) x"
```
```   415     by auto
```
```   416 qed
```
```   417
```
```   418
```
```   419 subsubsection {* Comprehension (filter) *}
```
```   420
```
```   421 lemma count_MCollect [simp]:
```
```   422   "count {# x:#M. P x #} a = (if P a then count M a else 0)"
```
```   423   by (simp add: MCollect_def in_multiset multiset_typedef)
```
```   424
```
```   425 lemma MCollect_empty [simp]: "MCollect {#} P = {#}"
```
```   426   by (rule multiset_eqI) simp
```
```   427
```
```   428 lemma MCollect_single [simp]:
```
```   429   "MCollect {#x#} P = (if P x then {#x#} else {#})"
```
```   430   by (rule multiset_eqI) simp
```
```   431
```
```   432 lemma MCollect_union [simp]:
```
```   433   "MCollect (M + N) f = MCollect M f + MCollect N f"
```
```   434   by (rule multiset_eqI) simp
```
```   435
```
```   436
```
```   437 subsubsection {* Set of elements *}
```
```   438
```
```   439 definition set_of :: "'a multiset => 'a set" where
```
```   440   "set_of M = {x. x :# M}"
```
```   441
```
```   442 lemma set_of_empty [simp]: "set_of {#} = {}"
```
```   443 by (simp add: set_of_def)
```
```   444
```
```   445 lemma set_of_single [simp]: "set_of {#b#} = {b}"
```
```   446 by (simp add: set_of_def)
```
```   447
```
```   448 lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
```
```   449 by (auto simp add: set_of_def)
```
```   450
```
```   451 lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
```
```   452 by (auto simp add: set_of_def multiset_eq_iff)
```
```   453
```
```   454 lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
```
```   455 by (auto simp add: set_of_def)
```
```   456
```
```   457 lemma set_of_MCollect [simp]: "set_of {# x:#M. P x #} = set_of M \<inter> {x. P x}"
```
```   458 by (auto simp add: set_of_def)
```
```   459
```
```   460 lemma finite_set_of [iff]: "finite (set_of M)"
```
```   461   using count [of M] by (simp add: multiset_def set_of_def)
```
```   462
```
```   463
```
```   464 subsubsection {* Size *}
```
```   465
```
```   466 instantiation multiset :: (type) size
```
```   467 begin
```
```   468
```
```   469 definition size_def:
```
```   470   "size M = setsum (count M) (set_of M)"
```
```   471
```
```   472 instance ..
```
```   473
```
```   474 end
```
```   475
```
```   476 lemma size_empty [simp]: "size {#} = 0"
```
```   477 by (simp add: size_def)
```
```   478
```
```   479 lemma size_single [simp]: "size {#b#} = 1"
```
```   480 by (simp add: size_def)
```
```   481
```
```   482 lemma setsum_count_Int:
```
```   483   "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A"
```
```   484 apply (induct rule: finite_induct)
```
```   485  apply simp
```
```   486 apply (simp add: Int_insert_left set_of_def)
```
```   487 done
```
```   488
```
```   489 lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
```
```   490 apply (unfold size_def)
```
```   491 apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)")
```
```   492  prefer 2
```
```   493  apply (rule ext, simp)
```
```   494 apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
```
```   495 apply (subst Int_commute)
```
```   496 apply (simp (no_asm_simp) add: setsum_count_Int)
```
```   497 done
```
```   498
```
```   499 lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
```
```   500 by (auto simp add: size_def multiset_eq_iff)
```
```   501
```
```   502 lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
```
```   503 by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)
```
```   504
```
```   505 lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
```
```   506 apply (unfold size_def)
```
```   507 apply (drule setsum_SucD)
```
```   508 apply auto
```
```   509 done
```
```   510
```
```   511 lemma size_eq_Suc_imp_eq_union:
```
```   512   assumes "size M = Suc n"
```
```   513   shows "\<exists>a N. M = N + {#a#}"
```
```   514 proof -
```
```   515   from assms obtain a where "a \<in># M"
```
```   516     by (erule size_eq_Suc_imp_elem [THEN exE])
```
```   517   then have "M = M - {#a#} + {#a#}" by simp
```
```   518   then show ?thesis by blast
```
```   519 qed
```
```   520
```
```   521
```
```   522 subsection {* Induction and case splits *}
```
```   523
```
```   524 lemma setsum_decr:
```
```   525   "finite F ==> (0::nat) < f a ==>
```
```   526     setsum (f (a := f a - 1)) F = (if a\<in>F then setsum f F - 1 else setsum f F)"
```
```   527 apply (induct rule: finite_induct)
```
```   528  apply auto
```
```   529 apply (drule_tac a = a in mk_disjoint_insert, auto)
```
```   530 done
```
```   531
```
```   532 lemma rep_multiset_induct_aux:
```
```   533 assumes 1: "P (\<lambda>a. (0::nat))"
```
```   534   and 2: "!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))"
```
```   535 shows "\<forall>f. f \<in> multiset --> setsum f {x. f x \<noteq> 0} = n --> P f"
```
```   536 apply (unfold multiset_def)
```
```   537 apply (induct_tac n, simp, clarify)
```
```   538  apply (subgoal_tac "f = (\<lambda>a.0)")
```
```   539   apply simp
```
```   540   apply (rule 1)
```
```   541  apply (rule ext, force, clarify)
```
```   542 apply (frule setsum_SucD, clarify)
```
```   543 apply (rename_tac a)
```
```   544 apply (subgoal_tac "finite {x. (f (a := f a - 1)) x > 0}")
```
```   545  prefer 2
```
```   546  apply (rule finite_subset)
```
```   547   prefer 2
```
```   548   apply assumption
```
```   549  apply simp
```
```   550  apply blast
```
```   551 apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
```
```   552  prefer 2
```
```   553  apply (rule ext)
```
```   554  apply (simp (no_asm_simp))
```
```   555  apply (erule ssubst, rule 2 [unfolded multiset_def], blast)
```
```   556 apply (erule allE, erule impE, erule_tac  mp, blast)
```
```   557 apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
```
```   558 apply (subgoal_tac "{x. x \<noteq> a --> f x \<noteq> 0} = {x. f x \<noteq> 0}")
```
```   559  prefer 2
```
```   560  apply blast
```
```   561 apply (subgoal_tac "{x. x \<noteq> a \<and> f x \<noteq> 0} = {x. f x \<noteq> 0} - {a}")
```
```   562  prefer 2
```
```   563  apply blast
```
```   564 apply (simp add: le_imp_diff_is_add setsum_diff1_nat cong: conj_cong)
```
```   565 done
```
```   566
```
```   567 theorem rep_multiset_induct:
```
```   568   "f \<in> multiset ==> P (\<lambda>a. 0) ==>
```
```   569     (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
```
```   570 using rep_multiset_induct_aux by blast
```
```   571
```
```   572 theorem multiset_induct [case_names empty add, induct type: multiset]:
```
```   573 assumes empty: "P {#}"
```
```   574   and add: "!!M x. P M ==> P (M + {#x#})"
```
```   575 shows "P M"
```
```   576 proof -
```
```   577   note defns = union_def single_def Mempty_def
```
```   578   note add' = add [unfolded defns, simplified]
```
```   579   have aux: "\<And>a::'a. count (Abs_multiset (\<lambda>b. if b = a then 1 else 0)) =
```
```   580     (\<lambda>b. if b = a then 1 else 0)" by (simp add: Abs_multiset_inverse in_multiset)
```
```   581   show ?thesis
```
```   582     apply (rule count_inverse [THEN subst])
```
```   583     apply (rule count [THEN rep_multiset_induct])
```
```   584      apply (rule empty [unfolded defns])
```
```   585     apply (subgoal_tac "f(b := f b + 1) = (\<lambda>a. f a + (if a=b then 1 else 0))")
```
```   586      prefer 2
```
```   587      apply (simp add: fun_eq_iff)
```
```   588     apply (erule ssubst)
```
```   589     apply (erule Abs_multiset_inverse [THEN subst])
```
```   590     apply (drule add')
```
```   591     apply (simp add: aux)
```
```   592     done
```
```   593 qed
```
```   594
```
```   595 lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
```
```   596 by (induct M) auto
```
```   597
```
```   598 lemma multiset_cases [cases type, case_names empty add]:
```
```   599 assumes em:  "M = {#} \<Longrightarrow> P"
```
```   600 assumes add: "\<And>N x. M = N + {#x#} \<Longrightarrow> P"
```
```   601 shows "P"
```
```   602 proof (cases "M = {#}")
```
```   603   assume "M = {#}" then show ?thesis using em by simp
```
```   604 next
```
```   605   assume "M \<noteq> {#}"
```
```   606   then obtain M' m where "M = M' + {#m#}"
```
```   607     by (blast dest: multi_nonempty_split)
```
```   608   then show ?thesis using add by simp
```
```   609 qed
```
```   610
```
```   611 lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
```
```   612 apply (cases M)
```
```   613  apply simp
```
```   614 apply (rule_tac x="M - {#x#}" in exI, simp)
```
```   615 done
```
```   616
```
```   617 lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
```
```   618 by (cases "B = {#}") (auto dest: multi_member_split)
```
```   619
```
```   620 lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#M. \<not> P x #}"
```
```   621 apply (subst multiset_eq_iff)
```
```   622 apply auto
```
```   623 done
```
```   624
```
```   625 lemma mset_less_size: "(A::'a multiset) < B \<Longrightarrow> size A < size B"
```
```   626 proof (induct A arbitrary: B)
```
```   627   case (empty M)
```
```   628   then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
```
```   629   then obtain M' x where "M = M' + {#x#}"
```
```   630     by (blast dest: multi_nonempty_split)
```
```   631   then show ?case by simp
```
```   632 next
```
```   633   case (add S x T)
```
```   634   have IH: "\<And>B. S < B \<Longrightarrow> size S < size B" by fact
```
```   635   have SxsubT: "S + {#x#} < T" by fact
```
```   636   then have "x \<in># T" and "S < T" by (auto dest: mset_less_insertD)
```
```   637   then obtain T' where T: "T = T' + {#x#}"
```
```   638     by (blast dest: multi_member_split)
```
```   639   then have "S < T'" using SxsubT
```
```   640     by (blast intro: mset_less_add_bothsides)
```
```   641   then have "size S < size T'" using IH by simp
```
```   642   then show ?case using T by simp
```
```   643 qed
```
```   644
```
```   645
```
```   646 subsubsection {* Strong induction and subset induction for multisets *}
```
```   647
```
```   648 text {* Well-foundedness of proper subset operator: *}
```
```   649
```
```   650 text {* proper multiset subset *}
```
```   651
```
```   652 definition
```
```   653   mset_less_rel :: "('a multiset * 'a multiset) set" where
```
```   654   "mset_less_rel = {(A,B). A < B}"
```
```   655
```
```   656 lemma multiset_add_sub_el_shuffle:
```
```   657   assumes "c \<in># B" and "b \<noteq> c"
```
```   658   shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
```
```   659 proof -
```
```   660   from `c \<in># B` obtain A where B: "B = A + {#c#}"
```
```   661     by (blast dest: multi_member_split)
```
```   662   have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
```
```   663   then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}"
```
```   664     by (simp add: add_ac)
```
```   665   then show ?thesis using B by simp
```
```   666 qed
```
```   667
```
```   668 lemma wf_mset_less_rel: "wf mset_less_rel"
```
```   669 apply (unfold mset_less_rel_def)
```
```   670 apply (rule wf_measure [THEN wf_subset, where f1=size])
```
```   671 apply (clarsimp simp: measure_def inv_image_def mset_less_size)
```
```   672 done
```
```   673
```
```   674 text {* The induction rules: *}
```
```   675
```
```   676 lemma full_multiset_induct [case_names less]:
```
```   677 assumes ih: "\<And>B. \<forall>(A::'a multiset). A < B \<longrightarrow> P A \<Longrightarrow> P B"
```
```   678 shows "P B"
```
```   679 apply (rule wf_mset_less_rel [THEN wf_induct])
```
```   680 apply (rule ih, auto simp: mset_less_rel_def)
```
```   681 done
```
```   682
```
```   683 lemma multi_subset_induct [consumes 2, case_names empty add]:
```
```   684 assumes "F \<le> A"
```
```   685   and empty: "P {#}"
```
```   686   and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
```
```   687 shows "P F"
```
```   688 proof -
```
```   689   from `F \<le> A`
```
```   690   show ?thesis
```
```   691   proof (induct F)
```
```   692     show "P {#}" by fact
```
```   693   next
```
```   694     fix x F
```
```   695     assume P: "F \<le> A \<Longrightarrow> P F" and i: "F + {#x#} \<le> A"
```
```   696     show "P (F + {#x#})"
```
```   697     proof (rule insert)
```
```   698       from i show "x \<in># A" by (auto dest: mset_le_insertD)
```
```   699       from i have "F \<le> A" by (auto dest: mset_le_insertD)
```
```   700       with P show "P F" .
```
```   701     qed
```
```   702   qed
```
```   703 qed
```
```   704
```
```   705
```
```   706 subsection {* Alternative representations *}
```
```   707
```
```   708 subsubsection {* Lists *}
```
```   709
```
```   710 primrec multiset_of :: "'a list \<Rightarrow> 'a multiset" where
```
```   711   "multiset_of [] = {#}" |
```
```   712   "multiset_of (a # x) = multiset_of x + {# a #}"
```
```   713
```
```   714 lemma in_multiset_in_set:
```
```   715   "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
```
```   716   by (induct xs) simp_all
```
```   717
```
```   718 lemma count_multiset_of:
```
```   719   "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
```
```   720   by (induct xs) simp_all
```
```   721
```
```   722 lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
```
```   723 by (induct x) auto
```
```   724
```
```   725 lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
```
```   726 by (induct x) auto
```
```   727
```
```   728 lemma set_of_multiset_of[simp]: "set_of(multiset_of x) = set x"
```
```   729 by (induct x) auto
```
```   730
```
```   731 lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)"
```
```   732 by (induct xs) auto
```
```   733
```
```   734 lemma multiset_of_append [simp]:
```
```   735   "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
```
```   736   by (induct xs arbitrary: ys) (auto simp: add_ac)
```
```   737
```
```   738 lemma surj_multiset_of: "surj multiset_of"
```
```   739 apply (unfold surj_def)
```
```   740 apply (rule allI)
```
```   741 apply (rule_tac M = y in multiset_induct)
```
```   742  apply auto
```
```   743 apply (rule_tac x = "x # xa" in exI)
```
```   744 apply auto
```
```   745 done
```
```   746
```
```   747 lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
```
```   748 by (induct x) auto
```
```   749
```
```   750 lemma distinct_count_atmost_1:
```
```   751   "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))"
```
```   752 apply (induct x, simp, rule iffI, simp_all)
```
```   753 apply (rule conjI)
```
```   754 apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
```
```   755 apply (erule_tac x = a in allE, simp, clarify)
```
```   756 apply (erule_tac x = aa in allE, simp)
```
```   757 done
```
```   758
```
```   759 lemma multiset_of_eq_setD:
```
```   760   "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys"
```
```   761 by (rule) (auto simp add:multiset_eq_iff set_count_greater_0)
```
```   762
```
```   763 lemma set_eq_iff_multiset_of_eq_distinct:
```
```   764   "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
```
```   765     (set x = set y) = (multiset_of x = multiset_of y)"
```
```   766 by (auto simp: multiset_eq_iff distinct_count_atmost_1)
```
```   767
```
```   768 lemma set_eq_iff_multiset_of_remdups_eq:
```
```   769    "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
```
```   770 apply (rule iffI)
```
```   771 apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
```
```   772 apply (drule distinct_remdups [THEN distinct_remdups
```
```   773       [THEN set_eq_iff_multiset_of_eq_distinct [THEN iffD2]]])
```
```   774 apply simp
```
```   775 done
```
```   776
```
```   777 lemma multiset_of_compl_union [simp]:
```
```   778   "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs"
```
```   779   by (induct xs) (auto simp: add_ac)
```
```   780
```
```   781 lemma count_filter:
```
```   782   "count (multiset_of xs) x = length [y \<leftarrow> xs. y = x]"
```
```   783 by (induct xs) auto
```
```   784
```
```   785 lemma nth_mem_multiset_of: "i < length ls \<Longrightarrow> (ls ! i) :# multiset_of ls"
```
```   786 apply (induct ls arbitrary: i)
```
```   787  apply simp
```
```   788 apply (case_tac i)
```
```   789  apply auto
```
```   790 done
```
```   791
```
```   792 lemma multiset_of_remove1[simp]:
```
```   793   "multiset_of (remove1 a xs) = multiset_of xs - {#a#}"
```
```   794 by (induct xs) (auto simp add: multiset_eq_iff)
```
```   795
```
```   796 lemma multiset_of_eq_length:
```
```   797   assumes "multiset_of xs = multiset_of ys"
```
```   798   shows "length xs = length ys"
```
```   799 using assms proof (induct xs arbitrary: ys)
```
```   800   case Nil then show ?case by simp
```
```   801 next
```
```   802   case (Cons x xs)
```
```   803   then have "x \<in># multiset_of ys" by (simp add: union_single_eq_member)
```
```   804   then have "x \<in> set ys" by (simp add: in_multiset_in_set)
```
```   805   from Cons.prems [symmetric] have "multiset_of xs = multiset_of (remove1 x ys)"
```
```   806     by simp
```
```   807   with Cons.hyps have "length xs = length (remove1 x ys)" .
```
```   808   with `x \<in> set ys` show ?case
```
```   809     by (auto simp add: length_remove1 dest: length_pos_if_in_set)
```
```   810 qed
```
```   811
```
```   812 lemma (in linorder) multiset_of_insort [simp]:
```
```   813   "multiset_of (insort x xs) = {#x#} + multiset_of xs"
```
```   814   by (induct xs) (simp_all add: ac_simps)
```
```   815
```
```   816 lemma (in linorder) multiset_of_sort [simp]:
```
```   817   "multiset_of (sort xs) = multiset_of xs"
```
```   818   by (induct xs) (simp_all add: ac_simps)
```
```   819
```
```   820 text {*
```
```   821   This lemma shows which properties suffice to show that a function
```
```   822   @{text "f"} with @{text "f xs = ys"} behaves like sort.
```
```   823 *}
```
```   824
```
```   825 lemma (in linorder) properties_for_sort:
```
```   826   "multiset_of ys = multiset_of xs \<Longrightarrow> sorted ys \<Longrightarrow> sort xs = ys"
```
```   827 proof (induct xs arbitrary: ys)
```
```   828   case Nil then show ?case by simp
```
```   829 next
```
```   830   case (Cons x xs)
```
```   831   then have "x \<in> set ys"
```
```   832     by (auto simp add:  mem_set_multiset_eq intro!: ccontr)
```
```   833   with Cons.prems Cons.hyps [of "remove1 x ys"] show ?case
```
```   834     by (simp add: sorted_remove1 multiset_of_remove1 insort_remove1)
```
```   835 qed
```
```   836
```
```   837 lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le> multiset_of xs"
```
```   838   by (induct xs) (auto intro: order_trans)
```
```   839
```
```   840 lemma multiset_of_update:
```
```   841   "i < length ls \<Longrightarrow> multiset_of (ls[i := v]) = multiset_of ls - {#ls ! i#} + {#v#}"
```
```   842 proof (induct ls arbitrary: i)
```
```   843   case Nil then show ?case by simp
```
```   844 next
```
```   845   case (Cons x xs)
```
```   846   show ?case
```
```   847   proof (cases i)
```
```   848     case 0 then show ?thesis by simp
```
```   849   next
```
```   850     case (Suc i')
```
```   851     with Cons show ?thesis
```
```   852       apply simp
```
```   853       apply (subst add_assoc)
```
```   854       apply (subst add_commute [of "{#v#}" "{#x#}"])
```
```   855       apply (subst add_assoc [symmetric])
```
```   856       apply simp
```
```   857       apply (rule mset_le_multiset_union_diff_commute)
```
```   858       apply (simp add: mset_le_single nth_mem_multiset_of)
```
```   859       done
```
```   860   qed
```
```   861 qed
```
```   862
```
```   863 lemma multiset_of_swap:
```
```   864   "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
```
```   865     multiset_of (ls[j := ls ! i, i := ls ! j]) = multiset_of ls"
```
```   866   by (cases "i = j") (simp_all add: multiset_of_update nth_mem_multiset_of)
```
```   867
```
```   868
```
```   869 subsubsection {* Association lists -- including rudimentary code generation *}
```
```   870
```
```   871 definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat" where
```
```   872   "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)"
```
```   873
```
```   874 lemma count_of_multiset:
```
```   875   "count_of xs \<in> multiset"
```
```   876 proof -
```
```   877   let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)}"
```
```   878   have "?A \<subseteq> dom (map_of xs)"
```
```   879   proof
```
```   880     fix x
```
```   881     assume "x \<in> ?A"
```
```   882     then have "0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)" by simp
```
```   883     then have "map_of xs x \<noteq> None" by (cases "map_of xs x") auto
```
```   884     then show "x \<in> dom (map_of xs)" by auto
```
```   885   qed
```
```   886   with finite_dom_map_of [of xs] have "finite ?A"
```
```   887     by (auto intro: finite_subset)
```
```   888   then show ?thesis
```
```   889     by (simp add: count_of_def fun_eq_iff multiset_def)
```
```   890 qed
```
```   891
```
```   892 lemma count_simps [simp]:
```
```   893   "count_of [] = (\<lambda>_. 0)"
```
```   894   "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)"
```
```   895   by (simp_all add: count_of_def fun_eq_iff)
```
```   896
```
```   897 lemma count_of_empty:
```
```   898   "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 0"
```
```   899   by (induct xs) (simp_all add: count_of_def)
```
```   900
```
```   901 lemma count_of_filter:
```
```   902   "count_of (filter (P \<circ> fst) xs) x = (if P x then count_of xs x else 0)"
```
```   903   by (induct xs) auto
```
```   904
```
```   905 definition Bag :: "('a \<times> nat) list \<Rightarrow> 'a multiset" where
```
```   906   "Bag xs = Abs_multiset (count_of xs)"
```
```   907
```
```   908 code_datatype Bag
```
```   909
```
```   910 lemma count_Bag [simp, code]:
```
```   911   "count (Bag xs) = count_of xs"
```
```   912   by (simp add: Bag_def count_of_multiset Abs_multiset_inverse)
```
```   913
```
```   914 lemma Mempty_Bag [code]:
```
```   915   "{#} = Bag []"
```
```   916   by (simp add: multiset_eq_iff)
```
```   917
```
```   918 lemma single_Bag [code]:
```
```   919   "{#x#} = Bag [(x, 1)]"
```
```   920   by (simp add: multiset_eq_iff)
```
```   921
```
```   922 lemma MCollect_Bag [code]:
```
```   923   "MCollect (Bag xs) P = Bag (filter (P \<circ> fst) xs)"
```
```   924   by (simp add: multiset_eq_iff count_of_filter)
```
```   925
```
```   926 lemma mset_less_eq_Bag [code]:
```
```   927   "Bag xs \<le> A \<longleftrightarrow> (\<forall>(x, n) \<in> set xs. count_of xs x \<le> count A x)"
```
```   928     (is "?lhs \<longleftrightarrow> ?rhs")
```
```   929 proof
```
```   930   assume ?lhs then show ?rhs
```
```   931     by (auto simp add: mset_le_def count_Bag)
```
```   932 next
```
```   933   assume ?rhs
```
```   934   show ?lhs
```
```   935   proof (rule mset_less_eqI)
```
```   936     fix x
```
```   937     from `?rhs` have "count_of xs x \<le> count A x"
```
```   938       by (cases "x \<in> fst ` set xs") (auto simp add: count_of_empty)
```
```   939     then show "count (Bag xs) x \<le> count A x"
```
```   940       by (simp add: mset_le_def count_Bag)
```
```   941   qed
```
```   942 qed
```
```   943
```
```   944 instantiation multiset :: (equal) equal
```
```   945 begin
```
```   946
```
```   947 definition
```
```   948   "HOL.equal A B \<longleftrightarrow> (A::'a multiset) \<le> B \<and> B \<le> A"
```
```   949
```
```   950 instance proof
```
```   951 qed (simp add: equal_multiset_def eq_iff)
```
```   952
```
```   953 end
```
```   954
```
```   955 lemma [code nbe]:
```
```   956   "HOL.equal (A :: 'a::equal multiset) A \<longleftrightarrow> True"
```
```   957   by (fact equal_refl)
```
```   958
```
```   959 definition (in term_syntax)
```
```   960   bagify :: "('a\<Colon>typerep \<times> nat) list \<times> (unit \<Rightarrow> Code_Evaluation.term)
```
```   961     \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
```
```   962   [code_unfold]: "bagify xs = Code_Evaluation.valtermify Bag {\<cdot>} xs"
```
```   963
```
```   964 notation fcomp (infixl "\<circ>>" 60)
```
```   965 notation scomp (infixl "\<circ>\<rightarrow>" 60)
```
```   966
```
```   967 instantiation multiset :: (random) random
```
```   968 begin
```
```   969
```
```   970 definition
```
```   971   "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (bagify xs))"
```
```   972
```
```   973 instance ..
```
```   974
```
```   975 end
```
```   976
```
```   977 no_notation fcomp (infixl "\<circ>>" 60)
```
```   978 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
```
```   979
```
```   980 hide_const (open) bagify
```
```   981
```
```   982
```
```   983 subsection {* The multiset order *}
```
```   984
```
```   985 subsubsection {* Well-foundedness *}
```
```   986
```
```   987 definition mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
```
```   988   "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
```
```   989       (\<forall>b. b :# K --> (b, a) \<in> r)}"
```
```   990
```
```   991 definition mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
```
```   992   "mult r = (mult1 r)\<^sup>+"
```
```   993
```
```   994 lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
```
```   995 by (simp add: mult1_def)
```
```   996
```
```   997 lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
```
```   998     (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
```
```   999     (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
```
```  1000   (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
```
```  1001 proof (unfold mult1_def)
```
```  1002   let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
```
```  1003   let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
```
```  1004   let ?case1 = "?case1 {(N, M). ?R N M}"
```
```  1005
```
```  1006   assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
```
```  1007   then have "\<exists>a' M0' K.
```
```  1008       M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
```
```  1009   then show "?case1 \<or> ?case2"
```
```  1010   proof (elim exE conjE)
```
```  1011     fix a' M0' K
```
```  1012     assume N: "N = M0' + K" and r: "?r K a'"
```
```  1013     assume "M0 + {#a#} = M0' + {#a'#}"
```
```  1014     then have "M0 = M0' \<and> a = a' \<or>
```
```  1015         (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
```
```  1016       by (simp only: add_eq_conv_ex)
```
```  1017     then show ?thesis
```
```  1018     proof (elim disjE conjE exE)
```
```  1019       assume "M0 = M0'" "a = a'"
```
```  1020       with N r have "?r K a \<and> N = M0 + K" by simp
```
```  1021       then have ?case2 .. then show ?thesis ..
```
```  1022     next
```
```  1023       fix K'
```
```  1024       assume "M0' = K' + {#a#}"
```
```  1025       with N have n: "N = K' + K + {#a#}" by (simp add: add_ac)
```
```  1026
```
```  1027       assume "M0 = K' + {#a'#}"
```
```  1028       with r have "?R (K' + K) M0" by blast
```
```  1029       with n have ?case1 by simp then show ?thesis ..
```
```  1030     qed
```
```  1031   qed
```
```  1032 qed
```
```  1033
```
```  1034 lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)"
```
```  1035 proof
```
```  1036   let ?R = "mult1 r"
```
```  1037   let ?W = "acc ?R"
```
```  1038   {
```
```  1039     fix M M0 a
```
```  1040     assume M0: "M0 \<in> ?W"
```
```  1041       and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
```
```  1042       and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
```
```  1043     have "M0 + {#a#} \<in> ?W"
```
```  1044     proof (rule accI [of "M0 + {#a#}"])
```
```  1045       fix N
```
```  1046       assume "(N, M0 + {#a#}) \<in> ?R"
```
```  1047       then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
```
```  1048           (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
```
```  1049         by (rule less_add)
```
```  1050       then show "N \<in> ?W"
```
```  1051       proof (elim exE disjE conjE)
```
```  1052         fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
```
```  1053         from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
```
```  1054         from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
```
```  1055         then show "N \<in> ?W" by (simp only: N)
```
```  1056       next
```
```  1057         fix K
```
```  1058         assume N: "N = M0 + K"
```
```  1059         assume "\<forall>b. b :# K --> (b, a) \<in> r"
```
```  1060         then have "M0 + K \<in> ?W"
```
```  1061         proof (induct K)
```
```  1062           case empty
```
```  1063           from M0 show "M0 + {#} \<in> ?W" by simp
```
```  1064         next
```
```  1065           case (add K x)
```
```  1066           from add.prems have "(x, a) \<in> r" by simp
```
```  1067           with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
```
```  1068           moreover from add have "M0 + K \<in> ?W" by simp
```
```  1069           ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
```
```  1070           then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add_assoc)
```
```  1071         qed
```
```  1072         then show "N \<in> ?W" by (simp only: N)
```
```  1073       qed
```
```  1074     qed
```
```  1075   } note tedious_reasoning = this
```
```  1076
```
```  1077   assume wf: "wf r"
```
```  1078   fix M
```
```  1079   show "M \<in> ?W"
```
```  1080   proof (induct M)
```
```  1081     show "{#} \<in> ?W"
```
```  1082     proof (rule accI)
```
```  1083       fix b assume "(b, {#}) \<in> ?R"
```
```  1084       with not_less_empty show "b \<in> ?W" by contradiction
```
```  1085     qed
```
```  1086
```
```  1087     fix M a assume "M \<in> ?W"
```
```  1088     from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
```
```  1089     proof induct
```
```  1090       fix a
```
```  1091       assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
```
```  1092       show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
```
```  1093       proof
```
```  1094         fix M assume "M \<in> ?W"
```
```  1095         then show "M + {#a#} \<in> ?W"
```
```  1096           by (rule acc_induct) (rule tedious_reasoning [OF _ r])
```
```  1097       qed
```
```  1098     qed
```
```  1099     from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
```
```  1100   qed
```
```  1101 qed
```
```  1102
```
```  1103 theorem wf_mult1: "wf r ==> wf (mult1 r)"
```
```  1104 by (rule acc_wfI) (rule all_accessible)
```
```  1105
```
```  1106 theorem wf_mult: "wf r ==> wf (mult r)"
```
```  1107 unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
```
```  1108
```
```  1109
```
```  1110 subsubsection {* Closure-free presentation *}
```
```  1111
```
```  1112 text {* One direction. *}
```
```  1113
```
```  1114 lemma mult_implies_one_step:
```
```  1115   "trans r ==> (M, N) \<in> mult r ==>
```
```  1116     \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
```
```  1117     (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
```
```  1118 apply (unfold mult_def mult1_def set_of_def)
```
```  1119 apply (erule converse_trancl_induct, clarify)
```
```  1120  apply (rule_tac x = M0 in exI, simp, clarify)
```
```  1121 apply (case_tac "a :# K")
```
```  1122  apply (rule_tac x = I in exI)
```
```  1123  apply (simp (no_asm))
```
```  1124  apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
```
```  1125  apply (simp (no_asm_simp) add: add_assoc [symmetric])
```
```  1126  apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
```
```  1127  apply (simp add: diff_union_single_conv)
```
```  1128  apply (simp (no_asm_use) add: trans_def)
```
```  1129  apply blast
```
```  1130 apply (subgoal_tac "a :# I")
```
```  1131  apply (rule_tac x = "I - {#a#}" in exI)
```
```  1132  apply (rule_tac x = "J + {#a#}" in exI)
```
```  1133  apply (rule_tac x = "K + Ka" in exI)
```
```  1134  apply (rule conjI)
```
```  1135   apply (simp add: multiset_eq_iff split: nat_diff_split)
```
```  1136  apply (rule conjI)
```
```  1137   apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp)
```
```  1138   apply (simp add: multiset_eq_iff split: nat_diff_split)
```
```  1139  apply (simp (no_asm_use) add: trans_def)
```
```  1140  apply blast
```
```  1141 apply (subgoal_tac "a :# (M0 + {#a#})")
```
```  1142  apply simp
```
```  1143 apply (simp (no_asm))
```
```  1144 done
```
```  1145
```
```  1146 lemma one_step_implies_mult_aux:
```
```  1147   "trans r ==>
```
```  1148     \<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))
```
```  1149       --> (I + K, I + J) \<in> mult r"
```
```  1150 apply (induct_tac n, auto)
```
```  1151 apply (frule size_eq_Suc_imp_eq_union, clarify)
```
```  1152 apply (rename_tac "J'", simp)
```
```  1153 apply (erule notE, auto)
```
```  1154 apply (case_tac "J' = {#}")
```
```  1155  apply (simp add: mult_def)
```
```  1156  apply (rule r_into_trancl)
```
```  1157  apply (simp add: mult1_def set_of_def, blast)
```
```  1158 txt {* Now we know @{term "J' \<noteq> {#}"}. *}
```
```  1159 apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
```
```  1160 apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
```
```  1161 apply (erule ssubst)
```
```  1162 apply (simp add: Ball_def, auto)
```
```  1163 apply (subgoal_tac
```
```  1164   "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
```
```  1165     (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
```
```  1166  prefer 2
```
```  1167  apply force
```
```  1168 apply (simp (no_asm_use) add: add_assoc [symmetric] mult_def)
```
```  1169 apply (erule trancl_trans)
```
```  1170 apply (rule r_into_trancl)
```
```  1171 apply (simp add: mult1_def set_of_def)
```
```  1172 apply (rule_tac x = a in exI)
```
```  1173 apply (rule_tac x = "I + J'" in exI)
```
```  1174 apply (simp add: add_ac)
```
```  1175 done
```
```  1176
```
```  1177 lemma one_step_implies_mult:
```
```  1178   "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
```
```  1179     ==> (I + K, I + J) \<in> mult r"
```
```  1180 using one_step_implies_mult_aux by blast
```
```  1181
```
```  1182
```
```  1183 subsubsection {* Partial-order properties *}
```
```  1184
```
```  1185 definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where
```
```  1186   "M' <# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
```
```  1187
```
```  1188 definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where
```
```  1189   "M' <=# M \<longleftrightarrow> M' <# M \<or> M' = M"
```
```  1190
```
```  1191 notation (xsymbols) less_multiset (infix "\<subset>#" 50)
```
```  1192 notation (xsymbols) le_multiset (infix "\<subseteq>#" 50)
```
```  1193
```
```  1194 interpretation multiset_order: order le_multiset less_multiset
```
```  1195 proof -
```
```  1196   have irrefl: "\<And>M :: 'a multiset. \<not> M \<subset># M"
```
```  1197   proof
```
```  1198     fix M :: "'a multiset"
```
```  1199     assume "M \<subset># M"
```
```  1200     then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def)
```
```  1201     have "trans {(x'::'a, x). x' < x}"
```
```  1202       by (rule transI) simp
```
```  1203     moreover note MM
```
```  1204     ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
```
```  1205       \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})"
```
```  1206       by (rule mult_implies_one_step)
```
```  1207     then obtain I J K where "M = I + J" and "M = I + K"
```
```  1208       and "J \<noteq> {#}" and "(\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})" by blast
```
```  1209     then have aux1: "K \<noteq> {#}" and aux2: "\<forall>k\<in>set_of K. \<exists>j\<in>set_of K. k < j" by auto
```
```  1210     have "finite (set_of K)" by simp
```
```  1211     moreover note aux2
```
```  1212     ultimately have "set_of K = {}"
```
```  1213       by (induct rule: finite_induct) (auto intro: order_less_trans)
```
```  1214     with aux1 show False by simp
```
```  1215   qed
```
```  1216   have trans: "\<And>K M N :: 'a multiset. K \<subset># M \<Longrightarrow> M \<subset># N \<Longrightarrow> K \<subset># N"
```
```  1217     unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
```
```  1218   show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset" proof
```
```  1219   qed (auto simp add: le_multiset_def irrefl dest: trans)
```
```  1220 qed
```
```  1221
```
```  1222 lemma mult_less_irrefl [elim!]:
```
```  1223   "M \<subset># (M::'a::order multiset) ==> R"
```
```  1224   by (simp add: multiset_order.less_irrefl)
```
```  1225
```
```  1226
```
```  1227 subsubsection {* Monotonicity of multiset union *}
```
```  1228
```
```  1229 lemma mult1_union:
```
```  1230   "(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r"
```
```  1231 apply (unfold mult1_def)
```
```  1232 apply auto
```
```  1233 apply (rule_tac x = a in exI)
```
```  1234 apply (rule_tac x = "C + M0" in exI)
```
```  1235 apply (simp add: add_assoc)
```
```  1236 done
```
```  1237
```
```  1238 lemma union_less_mono2: "B \<subset># D ==> C + B \<subset># C + (D::'a::order multiset)"
```
```  1239 apply (unfold less_multiset_def mult_def)
```
```  1240 apply (erule trancl_induct)
```
```  1241  apply (blast intro: mult1_union transI order_less_trans r_into_trancl)
```
```  1242 apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
```
```  1243 done
```
```  1244
```
```  1245 lemma union_less_mono1: "B \<subset># D ==> B + C \<subset># D + (C::'a::order multiset)"
```
```  1246 apply (subst add_commute [of B C])
```
```  1247 apply (subst add_commute [of D C])
```
```  1248 apply (erule union_less_mono2)
```
```  1249 done
```
```  1250
```
```  1251 lemma union_less_mono:
```
```  1252   "A \<subset># C ==> B \<subset># D ==> A + B \<subset># C + (D::'a::order multiset)"
```
```  1253   by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)
```
```  1254
```
```  1255 interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
```
```  1256 proof
```
```  1257 qed (auto simp add: le_multiset_def intro: union_less_mono2)
```
```  1258
```
```  1259
```
```  1260 subsection {* The fold combinator *}
```
```  1261
```
```  1262 text {*
```
```  1263   The intended behaviour is
```
```  1264   @{text "fold_mset f z {#x\<^isub>1, ..., x\<^isub>n#} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
```
```  1265   if @{text f} is associative-commutative.
```
```  1266 *}
```
```  1267
```
```  1268 text {*
```
```  1269   The graph of @{text "fold_mset"}, @{text "z"}: the start element,
```
```  1270   @{text "f"}: folding function, @{text "A"}: the multiset, @{text
```
```  1271   "y"}: the result.
```
```  1272 *}
```
```  1273 inductive
```
```  1274   fold_msetG :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b \<Rightarrow> bool"
```
```  1275   for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
```
```  1276   and z :: 'b
```
```  1277 where
```
```  1278   emptyI [intro]:  "fold_msetG f z {#} z"
```
```  1279 | insertI [intro]: "fold_msetG f z A y \<Longrightarrow> fold_msetG f z (A + {#x#}) (f x y)"
```
```  1280
```
```  1281 inductive_cases empty_fold_msetGE [elim!]: "fold_msetG f z {#} x"
```
```  1282 inductive_cases insert_fold_msetGE: "fold_msetG f z (A + {#}) y"
```
```  1283
```
```  1284 definition
```
```  1285   fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b" where
```
```  1286   "fold_mset f z A = (THE x. fold_msetG f z A x)"
```
```  1287
```
```  1288 lemma Diff1_fold_msetG:
```
```  1289   "fold_msetG f z (A - {#x#}) y \<Longrightarrow> x \<in># A \<Longrightarrow> fold_msetG f z A (f x y)"
```
```  1290 apply (frule_tac x = x in fold_msetG.insertI)
```
```  1291 apply auto
```
```  1292 done
```
```  1293
```
```  1294 lemma fold_msetG_nonempty: "\<exists>x. fold_msetG f z A x"
```
```  1295 apply (induct A)
```
```  1296  apply blast
```
```  1297 apply clarsimp
```
```  1298 apply (drule_tac x = x in fold_msetG.insertI)
```
```  1299 apply auto
```
```  1300 done
```
```  1301
```
```  1302 lemma fold_mset_empty[simp]: "fold_mset f z {#} = z"
```
```  1303 unfolding fold_mset_def by blast
```
```  1304
```
```  1305 context fun_left_comm
```
```  1306 begin
```
```  1307
```
```  1308 lemma fold_msetG_determ:
```
```  1309   "fold_msetG f z A x \<Longrightarrow> fold_msetG f z A y \<Longrightarrow> y = x"
```
```  1310 proof (induct arbitrary: x y z rule: full_multiset_induct)
```
```  1311   case (less M x\<^isub>1 x\<^isub>2 Z)
```
```  1312   have IH: "\<forall>A. A < M \<longrightarrow>
```
```  1313     (\<forall>x x' x''. fold_msetG f x'' A x \<longrightarrow> fold_msetG f x'' A x'
```
```  1314                \<longrightarrow> x' = x)" by fact
```
```  1315   have Mfoldx\<^isub>1: "fold_msetG f Z M x\<^isub>1" and Mfoldx\<^isub>2: "fold_msetG f Z M x\<^isub>2" by fact+
```
```  1316   show ?case
```
```  1317   proof (rule fold_msetG.cases [OF Mfoldx\<^isub>1])
```
```  1318     assume "M = {#}" and "x\<^isub>1 = Z"
```
```  1319     then show ?case using Mfoldx\<^isub>2 by auto
```
```  1320   next
```
```  1321     fix B b u
```
```  1322     assume "M = B + {#b#}" and "x\<^isub>1 = f b u" and Bu: "fold_msetG f Z B u"
```
```  1323     then have MBb: "M = B + {#b#}" and x\<^isub>1: "x\<^isub>1 = f b u" by auto
```
```  1324     show ?case
```
```  1325     proof (rule fold_msetG.cases [OF Mfoldx\<^isub>2])
```
```  1326       assume "M = {#}" "x\<^isub>2 = Z"
```
```  1327       then show ?case using Mfoldx\<^isub>1 by auto
```
```  1328     next
```
```  1329       fix C c v
```
```  1330       assume "M = C + {#c#}" and "x\<^isub>2 = f c v" and Cv: "fold_msetG f Z C v"
```
```  1331       then have MCc: "M = C + {#c#}" and x\<^isub>2: "x\<^isub>2 = f c v" by auto
```
```  1332       then have CsubM: "C < M" by simp
```
```  1333       from MBb have BsubM: "B < M" by simp
```
```  1334       show ?case
```
```  1335       proof cases
```
```  1336         assume "b=c"
```
```  1337         then moreover have "B = C" using MBb MCc by auto
```
```  1338         ultimately show ?thesis using Bu Cv x\<^isub>1 x\<^isub>2 CsubM IH by auto
```
```  1339       next
```
```  1340         assume diff: "b \<noteq> c"
```
```  1341         let ?D = "B - {#c#}"
```
```  1342         have cinB: "c \<in># B" and binC: "b \<in># C" using MBb MCc diff
```
```  1343           by (auto intro: insert_noteq_member dest: sym)
```
```  1344         have "B - {#c#} < B" using cinB by (rule mset_less_diff_self)
```
```  1345         then have DsubM: "?D < M" using BsubM by (blast intro: order_less_trans)
```
```  1346         from MBb MCc have "B + {#b#} = C + {#c#}" by blast
```
```  1347         then have [simp]: "B + {#b#} - {#c#} = C"
```
```  1348           using MBb MCc binC cinB by auto
```
```  1349         have B: "B = ?D + {#c#}" and C: "C = ?D + {#b#}"
```
```  1350           using MBb MCc diff binC cinB
```
```  1351           by (auto simp: multiset_add_sub_el_shuffle)
```
```  1352         then obtain d where Dfoldd: "fold_msetG f Z ?D d"
```
```  1353           using fold_msetG_nonempty by iprover
```
```  1354         then have "fold_msetG f Z B (f c d)" using cinB
```
```  1355           by (rule Diff1_fold_msetG)
```
```  1356         then have "f c d = u" using IH BsubM Bu by blast
```
```  1357         moreover
```
```  1358         have "fold_msetG f Z C (f b d)" using binC cinB diff Dfoldd
```
```  1359           by (auto simp: multiset_add_sub_el_shuffle
```
```  1360             dest: fold_msetG.insertI [where x=b])
```
```  1361         then have "f b d = v" using IH CsubM Cv by blast
```
```  1362         ultimately show ?thesis using x\<^isub>1 x\<^isub>2
```
```  1363           by (auto simp: fun_left_comm)
```
```  1364       qed
```
```  1365     qed
```
```  1366   qed
```
```  1367 qed
```
```  1368
```
```  1369 lemma fold_mset_insert_aux:
```
```  1370   "(fold_msetG f z (A + {#x#}) v) =
```
```  1371     (\<exists>y. fold_msetG f z A y \<and> v = f x y)"
```
```  1372 apply (rule iffI)
```
```  1373  prefer 2
```
```  1374  apply blast
```
```  1375 apply (rule_tac A=A and f=f in fold_msetG_nonempty [THEN exE, standard])
```
```  1376 apply (blast intro: fold_msetG_determ)
```
```  1377 done
```
```  1378
```
```  1379 lemma fold_mset_equality: "fold_msetG f z A y \<Longrightarrow> fold_mset f z A = y"
```
```  1380 unfolding fold_mset_def by (blast intro: fold_msetG_determ)
```
```  1381
```
```  1382 lemma fold_mset_insert:
```
```  1383   "fold_mset f z (A + {#x#}) = f x (fold_mset f z A)"
```
```  1384 apply (simp add: fold_mset_def fold_mset_insert_aux add_commute)
```
```  1385 apply (rule the_equality)
```
```  1386  apply (auto cong add: conj_cong
```
```  1387      simp add: fold_mset_def [symmetric] fold_mset_equality fold_msetG_nonempty)
```
```  1388 done
```
```  1389
```
```  1390 lemma fold_mset_insert_idem:
```
```  1391   "fold_mset f z (A + {#a#}) = f a (fold_mset f z A)"
```
```  1392 apply (simp add: fold_mset_def fold_mset_insert_aux)
```
```  1393 apply (rule the_equality)
```
```  1394  apply (auto cong add: conj_cong
```
```  1395      simp add: fold_mset_def [symmetric] fold_mset_equality fold_msetG_nonempty)
```
```  1396 done
```
```  1397
```
```  1398 lemma fold_mset_commute: "f x (fold_mset f z A) = fold_mset f (f x z) A"
```
```  1399 by (induct A) (auto simp: fold_mset_insert fun_left_comm [of x])
```
```  1400
```
```  1401 lemma fold_mset_single [simp]: "fold_mset f z {#x#} = f x z"
```
```  1402 using fold_mset_insert [of z "{#}"] by simp
```
```  1403
```
```  1404 lemma fold_mset_union [simp]:
```
```  1405   "fold_mset f z (A+B) = fold_mset f (fold_mset f z A) B"
```
```  1406 proof (induct A)
```
```  1407   case empty then show ?case by simp
```
```  1408 next
```
```  1409   case (add A x)
```
```  1410   have "A + {#x#} + B = (A+B) + {#x#}" by (simp add: add_ac)
```
```  1411   then have "fold_mset f z (A + {#x#} + B) = f x (fold_mset f z (A + B))"
```
```  1412     by (simp add: fold_mset_insert)
```
```  1413   also have "\<dots> = fold_mset f (fold_mset f z (A + {#x#})) B"
```
```  1414     by (simp add: fold_mset_commute[of x,symmetric] add fold_mset_insert)
```
```  1415   finally show ?case .
```
```  1416 qed
```
```  1417
```
```  1418 lemma fold_mset_fusion:
```
```  1419   assumes "fun_left_comm g"
```
```  1420   shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold_mset g w A) = fold_mset f (h w) A" (is "PROP ?P")
```
```  1421 proof -
```
```  1422   interpret fun_left_comm g by (fact assms)
```
```  1423   show "PROP ?P" by (induct A) auto
```
```  1424 qed
```
```  1425
```
```  1426 lemma fold_mset_rec:
```
```  1427   assumes "a \<in># A"
```
```  1428   shows "fold_mset f z A = f a (fold_mset f z (A - {#a#}))"
```
```  1429 proof -
```
```  1430   from assms obtain A' where "A = A' + {#a#}"
```
```  1431     by (blast dest: multi_member_split)
```
```  1432   then show ?thesis by simp
```
```  1433 qed
```
```  1434
```
```  1435 end
```
```  1436
```
```  1437 text {*
```
```  1438   A note on code generation: When defining some function containing a
```
```  1439   subterm @{term"fold_mset F"}, code generation is not automatic. When
```
```  1440   interpreting locale @{text left_commutative} with @{text F}, the
```
```  1441   would be code thms for @{const fold_mset} become thms like
```
```  1442   @{term"fold_mset F z {#} = z"} where @{text F} is not a pattern but
```
```  1443   contains defined symbols, i.e.\ is not a code thm. Hence a separate
```
```  1444   constant with its own code thms needs to be introduced for @{text
```
```  1445   F}. See the image operator below.
```
```  1446 *}
```
```  1447
```
```  1448
```
```  1449 subsection {* Image *}
```
```  1450
```
```  1451 definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
```
```  1452   "image_mset f = fold_mset (op + o single o f) {#}"
```
```  1453
```
```  1454 interpretation image_left_comm: fun_left_comm "op + o single o f"
```
```  1455 proof qed (simp add: add_ac)
```
```  1456
```
```  1457 lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
```
```  1458 by (simp add: image_mset_def)
```
```  1459
```
```  1460 lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
```
```  1461 by (simp add: image_mset_def)
```
```  1462
```
```  1463 lemma image_mset_insert:
```
```  1464   "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
```
```  1465 by (simp add: image_mset_def add_ac)
```
```  1466
```
```  1467 lemma image_mset_union [simp]:
```
```  1468   "image_mset f (M+N) = image_mset f M + image_mset f N"
```
```  1469 apply (induct N)
```
```  1470  apply simp
```
```  1471 apply (simp add: add_assoc [symmetric] image_mset_insert)
```
```  1472 done
```
```  1473
```
```  1474 lemma size_image_mset [simp]: "size (image_mset f M) = size M"
```
```  1475 by (induct M) simp_all
```
```  1476
```
```  1477 lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
```
```  1478 by (cases M) auto
```
```  1479
```
```  1480 syntax
```
```  1481   "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
```
```  1482       ("({#_/. _ :# _#})")
```
```  1483 translations
```
```  1484   "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
```
```  1485
```
```  1486 syntax
```
```  1487   "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
```
```  1488       ("({#_/ | _ :# _./ _#})")
```
```  1489 translations
```
```  1490   "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
```
```  1491
```
```  1492 text {*
```
```  1493   This allows to write not just filters like @{term "{#x:#M. x<c#}"}
```
```  1494   but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
```
```  1495   "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
```
```  1496   @{term "{#x+x|x:#M. x<c#}"}.
```
```  1497 *}
```
```  1498
```
```  1499
```
```  1500 subsection {* Termination proofs with multiset orders *}
```
```  1501
```
```  1502 lemma multi_member_skip: "x \<in># XS \<Longrightarrow> x \<in># {# y #} + XS"
```
```  1503   and multi_member_this: "x \<in># {# x #} + XS"
```
```  1504   and multi_member_last: "x \<in># {# x #}"
```
```  1505   by auto
```
```  1506
```
```  1507 definition "ms_strict = mult pair_less"
```
```  1508 definition "ms_weak = ms_strict \<union> Id"
```
```  1509
```
```  1510 lemma ms_reduction_pair: "reduction_pair (ms_strict, ms_weak)"
```
```  1511 unfolding reduction_pair_def ms_strict_def ms_weak_def pair_less_def
```
```  1512 by (auto intro: wf_mult1 wf_trancl simp: mult_def)
```
```  1513
```
```  1514 lemma smsI:
```
```  1515   "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict"
```
```  1516   unfolding ms_strict_def
```
```  1517 by (rule one_step_implies_mult) (auto simp add: max_strict_def pair_less_def elim!:max_ext.cases)
```
```  1518
```
```  1519 lemma wmsI:
```
```  1520   "(set_of A, set_of B) \<in> max_strict \<or> A = {#} \<and> B = {#}
```
```  1521   \<Longrightarrow> (Z + A, Z + B) \<in> ms_weak"
```
```  1522 unfolding ms_weak_def ms_strict_def
```
```  1523 by (auto simp add: pair_less_def max_strict_def elim!:max_ext.cases intro: one_step_implies_mult)
```
```  1524
```
```  1525 inductive pw_leq
```
```  1526 where
```
```  1527   pw_leq_empty: "pw_leq {#} {#}"
```
```  1528 | pw_leq_step:  "\<lbrakk>(x,y) \<in> pair_leq; pw_leq X Y \<rbrakk> \<Longrightarrow> pw_leq ({#x#} + X) ({#y#} + Y)"
```
```  1529
```
```  1530 lemma pw_leq_lstep:
```
```  1531   "(x, y) \<in> pair_leq \<Longrightarrow> pw_leq {#x#} {#y#}"
```
```  1532 by (drule pw_leq_step) (rule pw_leq_empty, simp)
```
```  1533
```
```  1534 lemma pw_leq_split:
```
```  1535   assumes "pw_leq X Y"
```
```  1536   shows "\<exists>A B Z. X = A + Z \<and> Y = B + Z \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
```
```  1537   using assms
```
```  1538 proof (induct)
```
```  1539   case pw_leq_empty thus ?case by auto
```
```  1540 next
```
```  1541   case (pw_leq_step x y X Y)
```
```  1542   then obtain A B Z where
```
```  1543     [simp]: "X = A + Z" "Y = B + Z"
```
```  1544       and 1[simp]: "(set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#})"
```
```  1545     by auto
```
```  1546   from pw_leq_step have "x = y \<or> (x, y) \<in> pair_less"
```
```  1547     unfolding pair_leq_def by auto
```
```  1548   thus ?case
```
```  1549   proof
```
```  1550     assume [simp]: "x = y"
```
```  1551     have
```
```  1552       "{#x#} + X = A + ({#y#}+Z)
```
```  1553       \<and> {#y#} + Y = B + ({#y#}+Z)
```
```  1554       \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
```
```  1555       by (auto simp: add_ac)
```
```  1556     thus ?case by (intro exI)
```
```  1557   next
```
```  1558     assume A: "(x, y) \<in> pair_less"
```
```  1559     let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
```
```  1560     have "{#x#} + X = ?A' + Z"
```
```  1561       "{#y#} + Y = ?B' + Z"
```
```  1562       by (auto simp add: add_ac)
```
```  1563     moreover have
```
```  1564       "(set_of ?A', set_of ?B') \<in> max_strict"
```
```  1565       using 1 A unfolding max_strict_def
```
```  1566       by (auto elim!: max_ext.cases)
```
```  1567     ultimately show ?thesis by blast
```
```  1568   qed
```
```  1569 qed
```
```  1570
```
```  1571 lemma
```
```  1572   assumes pwleq: "pw_leq Z Z'"
```
```  1573   shows ms_strictI: "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
```
```  1574   and   ms_weakI1:  "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
```
```  1575   and   ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
```
```  1576 proof -
```
```  1577   from pw_leq_split[OF pwleq]
```
```  1578   obtain A' B' Z''
```
```  1579     where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
```
```  1580     and mx_or_empty: "(set_of A', set_of B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
```
```  1581     by blast
```
```  1582   {
```
```  1583     assume max: "(set_of A, set_of B) \<in> max_strict"
```
```  1584     from mx_or_empty
```
```  1585     have "(Z'' + (A + A'), Z'' + (B + B')) \<in> ms_strict"
```
```  1586     proof
```
```  1587       assume max': "(set_of A', set_of B') \<in> max_strict"
```
```  1588       with max have "(set_of (A + A'), set_of (B + B')) \<in> max_strict"
```
```  1589         by (auto simp: max_strict_def intro: max_ext_additive)
```
```  1590       thus ?thesis by (rule smsI)
```
```  1591     next
```
```  1592       assume [simp]: "A' = {#} \<and> B' = {#}"
```
```  1593       show ?thesis by (rule smsI) (auto intro: max)
```
```  1594     qed
```
```  1595     thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add:add_ac)
```
```  1596     thus "(Z + A, Z' + B) \<in> ms_weak" by (simp add: ms_weak_def)
```
```  1597   }
```
```  1598   from mx_or_empty
```
```  1599   have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
```
```  1600   thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:add_ac)
```
```  1601 qed
```
```  1602
```
```  1603 lemma empty_neutral: "{#} + x = x" "x + {#} = x"
```
```  1604 and nonempty_plus: "{# x #} + rs \<noteq> {#}"
```
```  1605 and nonempty_single: "{# x #} \<noteq> {#}"
```
```  1606 by auto
```
```  1607
```
```  1608 setup {*
```
```  1609 let
```
```  1610   fun msetT T = Type (@{type_name multiset}, [T]);
```
```  1611
```
```  1612   fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
```
```  1613     | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) \$ x
```
```  1614     | mk_mset T (x :: xs) =
```
```  1615           Const (@{const_name plus}, msetT T --> msetT T --> msetT T) \$
```
```  1616                 mk_mset T [x] \$ mk_mset T xs
```
```  1617
```
```  1618   fun mset_member_tac m i =
```
```  1619       (if m <= 0 then
```
```  1620            rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
```
```  1621        else
```
```  1622            rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i)
```
```  1623
```
```  1624   val mset_nonempty_tac =
```
```  1625       rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
```
```  1626
```
```  1627   val regroup_munion_conv =
```
```  1628       Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
```
```  1629         (map (fn t => t RS eq_reflection) (@{thms add_ac} @ @{thms empty_neutral}))
```
```  1630
```
```  1631   fun unfold_pwleq_tac i =
```
```  1632     (rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
```
```  1633       ORELSE (rtac @{thm pw_leq_lstep} i)
```
```  1634       ORELSE (rtac @{thm pw_leq_empty} i)
```
```  1635
```
```  1636   val set_of_simps = [@{thm set_of_empty}, @{thm set_of_single}, @{thm set_of_union},
```
```  1637                       @{thm Un_insert_left}, @{thm Un_empty_left}]
```
```  1638 in
```
```  1639   ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset
```
```  1640   {
```
```  1641     msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
```
```  1642     mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
```
```  1643     mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_of_simps,
```
```  1644     smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
```
```  1645     reduction_pair= @{thm ms_reduction_pair}
```
```  1646   })
```
```  1647 end
```
```  1648 *}
```
```  1649
```
```  1650
```
```  1651 subsection {* Legacy theorem bindings *}
```
```  1652
```
```  1653 lemmas multi_count_eq = multiset_eq_iff [symmetric]
```
```  1654
```
```  1655 lemma union_commute: "M + N = N + (M::'a multiset)"
```
```  1656   by (fact add_commute)
```
```  1657
```
```  1658 lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
```
```  1659   by (fact add_assoc)
```
```  1660
```
```  1661 lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
```
```  1662   by (fact add_left_commute)
```
```  1663
```
```  1664 lemmas union_ac = union_assoc union_commute union_lcomm
```
```  1665
```
```  1666 lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)"
```
```  1667   by (fact add_right_cancel)
```
```  1668
```
```  1669 lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)"
```
```  1670   by (fact add_left_cancel)
```
```  1671
```
```  1672 lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
```
```  1673   by (fact add_imp_eq)
```
```  1674
```
```  1675 lemma mset_less_trans: "(M::'a multiset) < K \<Longrightarrow> K < N \<Longrightarrow> M < N"
```
```  1676   by (fact order_less_trans)
```
```  1677
```
```  1678 lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
```
```  1679   by (fact inf.commute)
```
```  1680
```
```  1681 lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
```
```  1682   by (fact inf.assoc [symmetric])
```
```  1683
```
```  1684 lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
```
```  1685   by (fact inf.left_commute)
```
```  1686
```
```  1687 lemmas multiset_inter_ac =
```
```  1688   multiset_inter_commute
```
```  1689   multiset_inter_assoc
```
```  1690   multiset_inter_left_commute
```
```  1691
```
```  1692 lemma mult_less_not_refl:
```
```  1693   "\<not> M \<subset># (M::'a::order multiset)"
```
```  1694   by (fact multiset_order.less_irrefl)
```
```  1695
```
```  1696 lemma mult_less_trans:
```
```  1697   "K \<subset># M ==> M \<subset># N ==> K \<subset># (N::'a::order multiset)"
```
```  1698   by (fact multiset_order.less_trans)
```
```  1699
```
```  1700 lemma mult_less_not_sym:
```
```  1701   "M \<subset># N ==> \<not> N \<subset># (M::'a::order multiset)"
```
```  1702   by (fact multiset_order.less_not_sym)
```
```  1703
```
```  1704 lemma mult_less_asym:
```
```  1705   "M \<subset># N ==> (\<not> P ==> N \<subset># (M::'a::order multiset)) ==> P"
```
```  1706   by (fact multiset_order.less_asym)
```
```  1707
```
```  1708 ML {*
```
```  1709 fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T]))
```
```  1710                       (Const _ \$ t') =
```
```  1711     let
```
```  1712       val (maybe_opt, ps) =
```
```  1713         Nitpick_Model.dest_plain_fun t' ||> op ~~
```
```  1714         ||> map (apsnd (snd o HOLogic.dest_number))
```
```  1715       fun elems_for t =
```
```  1716         case AList.lookup (op =) ps t of
```
```  1717           SOME n => replicate n t
```
```  1718         | NONE => [Const (maybe_name, elem_T --> elem_T) \$ t]
```
```  1719     in
```
```  1720       case maps elems_for (all_values elem_T) @
```
```  1721            (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
```
```  1722             else []) of
```
```  1723         [] => Const (@{const_name zero_class.zero}, T)
```
```  1724       | ts => foldl1 (fn (t1, t2) =>
```
```  1725                          Const (@{const_name plus_class.plus}, T --> T --> T)
```
```  1726                          \$ t1 \$ t2)
```
```  1727                      (map (curry (op \$) (Const (@{const_name single},
```
```  1728                                                 elem_T --> T))) ts)
```
```  1729     end
```
```  1730   | multiset_postproc _ _ _ _ t = t
```
```  1731 *}
```
```  1732
```
```  1733 declaration {*
```
```  1734 Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
```
```  1735     multiset_postproc
```
```  1736 *}
```
```  1737
```
```  1738 end
```