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