author  nipkow 
Fri, 01 Feb 2008 18:01:06 +0100  
changeset 26033  278025d5282d 
parent 26016  f9d1bf2fc59c 
child 26143  314c0bcb7df7 
permissions  rwrr 
10249  1 
(* Title: HOL/Library/Multiset.thy 
2 
ID: $Id$ 

15072  3 
Author: Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker 
10249  4 
*) 
5 

14706  6 
header {* Multisets *} 
10249  7 

15131  8 
theory Multiset 
25595  9 
imports List 
15131  10 
begin 
10249  11 

12 
subsection {* The type of multisets *} 

13 

25162  14 
typedef 'a multiset = "{f::'a => nat. finite {x . f x > 0}}" 
10249  15 
proof 
11464  16 
show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp 
10249  17 
qed 
18 

19 
lemmas multiset_typedef [simp] = 

10277  20 
Abs_multiset_inverse Rep_multiset_inverse Rep_multiset 
21 
and [simp] = Rep_multiset_inject [symmetric] 

10249  22 

19086  23 
definition 
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

24 
Mempty :: "'a multiset" ("{#}") where 
19086  25 
"{#} = Abs_multiset (\<lambda>a. 0)" 
10249  26 

21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

27 
definition 
25507  28 
single :: "'a => 'a multiset" where 
29 
"single a = Abs_multiset (\<lambda>b. if b = a then 1 else 0)" 

10249  30 

26016  31 
declare 
32 
Mempty_def[code func del] single_def[code func del] 

33 

21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

34 
definition 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

35 
count :: "'a multiset => 'a => nat" where 
19086  36 
"count = Rep_multiset" 
10249  37 

21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

38 
definition 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

39 
MCollect :: "'a multiset => ('a => bool) => 'a multiset" where 
19086  40 
"MCollect M P = Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)" 
41 

19363  42 
abbreviation 
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

43 
Melem :: "'a => 'a multiset => bool" ("(_/ :# _)" [50, 51] 50) where 
25610  44 
"a :# M == 0 < count M a" 
45 

46 
notation (xsymbols) Melem (infix "\<in>#" 50) 

10249  47 

48 
syntax 

26033  49 
"_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset" ("(1{# _ :# _./ _#})") 
10249  50 
translations 
26033  51 
"{#x :# M. P#}" == "CONST MCollect M (\<lambda>x. P)" 
10249  52 

19086  53 
definition 
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

54 
set_of :: "'a multiset => 'a set" where 
19086  55 
"set_of M = {x. x :# M}" 
10249  56 

25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

57 
instantiation multiset :: (type) "{plus, minus, zero, size}" 
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

58 
begin 
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

59 

c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

60 
definition 
26016  61 
union_def[code func del]: 
62 
"M + N == Abs_multiset (\<lambda>a. Rep_multiset M a + Rep_multiset N a)" 

25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

63 

c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

64 
definition 
11464  65 
diff_def: "M  N == Abs_multiset (\<lambda>a. Rep_multiset M a  Rep_multiset N a)" 
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

66 

c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

67 
definition 
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset

68 
Zero_multiset_def [simp]: "0 == {#}" 
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

69 

c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

70 
definition 
26016  71 
size_def[code func del]: "size M == setsum (count M) (set_of M)" 
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

72 

c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

73 
instance .. 
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

74 

c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset

75 
end 
10249  76 

19086  77 
definition 
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

78 
multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where 
19086  79 
"multiset_inter A B = A  (A  B)" 
15869  80 

25507  81 
syntax  "Multiset Enumeration" 
82 
"@multiset" :: "args => 'a multiset" ("{#(_)#}") 

83 

84 
translations 

85 
"{#x, xs#}" == "{#x#} + {#xs#}" 

86 
"{#x#}" == "CONST single x" 

87 

10249  88 

89 
text {* 

90 
\medskip Preservation of the representing set @{term multiset}. 

91 
*} 

92 

26016  93 
lemma const0_in_multiset: "(\<lambda>a. 0) \<in> multiset" 
17161  94 
by (simp add: multiset_def) 
10249  95 

26016  96 
lemma only1_in_multiset: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset" 
17161  97 
by (simp add: multiset_def) 
10249  98 

26016  99 
lemma union_preserves_multiset: 
11464  100 
"M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset" 
17161  101 
apply (simp add: multiset_def) 
102 
apply (drule (1) finite_UnI) 

10249  103 
apply (simp del: finite_Un add: Un_def) 
104 
done 

105 

26016  106 
lemma diff_preserves_multiset: 
11464  107 
"M \<in> multiset ==> (\<lambda>a. M a  N a) \<in> multiset" 
17161  108 
apply (simp add: multiset_def) 
10249  109 
apply (rule finite_subset) 
17161  110 
apply auto 
10249  111 
done 
112 

26016  113 
lemma MCollect_preserves_multiset: 
114 
"M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset" 

115 
apply (simp add: multiset_def) 

116 
apply (rule finite_subset, auto) 

117 
done 

10249  118 

26016  119 
lemmas in_multiset = const0_in_multiset only1_in_multiset 
120 
union_preserves_multiset diff_preserves_multiset MCollect_preserves_multiset 

121 

122 
subsection {* Algebraic properties *} 

10249  123 

124 
subsubsection {* Union *} 

125 

17161  126 
lemma union_empty [simp]: "M + {#} = M \<and> {#} + M = M" 
26016  127 
by (simp add: union_def Mempty_def in_multiset) 
10249  128 

17161  129 
lemma union_commute: "M + N = N + (M::'a multiset)" 
26016  130 
by (simp add: union_def add_ac in_multiset) 
17161  131 

132 
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))" 

26016  133 
by (simp add: union_def add_ac in_multiset) 
10249  134 

17161  135 
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))" 
136 
proof  

137 
have "M + (N + K) = (N + K) + M" 

138 
by (rule union_commute) 

139 
also have "\<dots> = N + (K + M)" 

140 
by (rule union_assoc) 

141 
also have "K + M = M + K" 

142 
by (rule union_commute) 

143 
finally show ?thesis . 

144 
qed 

10249  145 

17161  146 
lemmas union_ac = union_assoc union_commute union_lcomm 
10249  147 

14738  148 
instance multiset :: (type) comm_monoid_add 
17200  149 
proof 
14722
8e739a6eaf11
replaced applystyle proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset

150 
fix a b c :: "'a multiset" 
8e739a6eaf11
replaced applystyle proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset

151 
show "(a + b) + c = a + (b + c)" by (rule union_assoc) 
8e739a6eaf11
replaced applystyle proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset

152 
show "a + b = b + a" by (rule union_commute) 
8e739a6eaf11
replaced applystyle proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset

153 
show "0 + a = a" by simp 
8e739a6eaf11
replaced applystyle proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset

154 
qed 
10277  155 

10249  156 

157 
subsubsection {* Difference *} 

158 

17161  159 
lemma diff_empty [simp]: "M  {#} = M \<and> {#}  M = {#}" 
26016  160 
by (simp add: Mempty_def diff_def in_multiset) 
10249  161 

17161  162 
lemma diff_union_inverse2 [simp]: "M + {#a#}  {#a#} = M" 
26016  163 
by (simp add: union_def diff_def in_multiset) 
10249  164 

165 

166 
subsubsection {* Count of elements *} 

167 

17161  168 
lemma count_empty [simp]: "count {#} a = 0" 
26016  169 
by (simp add: count_def Mempty_def in_multiset) 
10249  170 

17161  171 
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)" 
26016  172 
by (simp add: count_def single_def in_multiset) 
10249  173 

17161  174 
lemma count_union [simp]: "count (M + N) a = count M a + count N a" 
26016  175 
by (simp add: count_def union_def in_multiset) 
10249  176 

17161  177 
lemma count_diff [simp]: "count (M  N) a = count M a  count N a" 
26016  178 
by (simp add: count_def diff_def in_multiset) 
179 

180 
lemma count_MCollect [simp]: 

26033  181 
"count {# x:#M. P x #} a = (if P a then count M a else 0)" 
26016  182 
by (simp add: count_def MCollect_def in_multiset) 
10249  183 

184 

185 
subsubsection {* Set of elements *} 

186 

17161  187 
lemma set_of_empty [simp]: "set_of {#} = {}" 
26016  188 
by (simp add: set_of_def) 
10249  189 

17161  190 
lemma set_of_single [simp]: "set_of {#b#} = {b}" 
26016  191 
by (simp add: set_of_def) 
10249  192 

17161  193 
lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N" 
26016  194 
by (auto simp add: set_of_def) 
10249  195 

17161  196 
lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})" 
26016  197 
by(auto simp: set_of_def Mempty_def in_multiset count_def expand_fun_eq) 
10249  198 

17161  199 
lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)" 
26016  200 
by (auto simp add: set_of_def) 
201 

26033  202 
lemma set_of_MCollect [simp]: "set_of {# x:#M. P x #} = set_of M \<inter> {x. P x}" 
26016  203 
by (auto simp add: set_of_def) 
10249  204 

205 

206 
subsubsection {* Size *} 

207 

26016  208 
lemma size_empty [simp,code func]: "size {#} = 0" 
209 
by (simp add: size_def) 

10249  210 

26016  211 
lemma size_single [simp,code func]: "size {#b#} = 1" 
212 
by (simp add: size_def) 

10249  213 

17161  214 
lemma finite_set_of [iff]: "finite (set_of M)" 
215 
using Rep_multiset [of M] 

216 
by (simp add: multiset_def set_of_def count_def) 

10249  217 

17161  218 
lemma setsum_count_Int: 
11464  219 
"finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A" 
18258  220 
apply (induct rule: finite_induct) 
17161  221 
apply simp 
10249  222 
apply (simp add: Int_insert_left set_of_def) 
223 
done 

224 

26016  225 
lemma size_union[simp,code func]: "size (M + N::'a multiset) = size M + size N" 
10249  226 
apply (unfold size_def) 
11464  227 
apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)") 
10249  228 
prefer 2 
15072  229 
apply (rule ext, simp) 
15402  230 
apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int) 
10249  231 
apply (subst Int_commute) 
232 
apply (simp (no_asm_simp) add: setsum_count_Int) 

233 
done 

234 

17161  235 
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})" 
26016  236 
apply (unfold size_def Mempty_def count_def, auto simp: in_multiset) 
237 
apply (simp add: set_of_def count_def in_multiset expand_fun_eq) 

238 
done 

239 

240 
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)" 

241 
by(metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty) 

10249  242 

17161  243 
lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M" 
10249  244 
apply (unfold size_def) 
15072  245 
apply (drule setsum_SucD, auto) 
10249  246 
done 
247 

248 
subsubsection {* Equality of multisets *} 

249 

17161  250 
lemma multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)" 
251 
by (simp add: count_def expand_fun_eq) 

10249  252 

17161  253 
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}" 
26016  254 
by (simp add: single_def Mempty_def in_multiset expand_fun_eq) 
10249  255 

17161  256 
lemma single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)" 
26016  257 
by (auto simp add: single_def in_multiset expand_fun_eq) 
10249  258 

17161  259 
lemma union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})" 
26016  260 
by (auto simp add: union_def Mempty_def in_multiset expand_fun_eq) 
10249  261 

17161  262 
lemma empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})" 
26016  263 
by (auto simp add: union_def Mempty_def in_multiset expand_fun_eq) 
10249  264 

17161  265 
lemma union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))" 
26016  266 
by (simp add: union_def in_multiset expand_fun_eq) 
10249  267 

17161  268 
lemma union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))" 
26016  269 
by (simp add: union_def in_multiset expand_fun_eq) 
10249  270 

17161  271 
lemma union_is_single: 
26016  272 
"(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})" 
273 
apply (simp add: Mempty_def single_def union_def in_multiset add_is_1 expand_fun_eq) 

274 
apply blast 

275 
done 

10249  276 

17161  277 
lemma single_is_union: 
15072  278 
"({#a#} = M + N) = ({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)" 
26016  279 
apply (unfold Mempty_def single_def union_def) 
280 
apply (simp add: add_is_1 one_is_add in_multiset expand_fun_eq) 

281 
apply (blast dest: sym) 

282 
done 

10249  283 

17161  284 
lemma add_eq_conv_diff: 
10249  285 
"(M + {#a#} = N + {#b#}) = 
15072  286 
(M = N \<and> a = b \<or> M = N  {#a#} + {#b#} \<and> N = M  {#b#} + {#a#})" 
24035  287 
using [[simproc del: neq]] 
10249  288 
apply (unfold single_def union_def diff_def) 
26016  289 
apply (simp (no_asm) add: in_multiset expand_fun_eq) 
15072  290 
apply (rule conjI, force, safe, simp_all) 
13601  291 
apply (simp add: eq_sym_conv) 
10249  292 
done 
293 

15869  294 
declare Rep_multiset_inject [symmetric, simp del] 
295 

23611  296 
instance multiset :: (type) cancel_ab_semigroup_add 
297 
proof 

298 
fix a b c :: "'a multiset" 

299 
show "a + b = a + c \<Longrightarrow> b = c" by simp 

300 
qed 

15869  301 

25610  302 
lemma insert_DiffM: 
303 
"x \<in># M \<Longrightarrow> {#x#} + (M  {#x#}) = M" 

304 
by (clarsimp simp: multiset_eq_conv_count_eq) 

305 

306 
lemma insert_DiffM2[simp]: 

307 
"x \<in># M \<Longrightarrow> M  {#x#} + {#x#} = M" 

308 
by (clarsimp simp: multiset_eq_conv_count_eq) 

309 

310 
lemma multi_union_self_other_eq: 

311 
"(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y" 

312 
by (induct A arbitrary: X Y, auto) 

313 

314 
lemma multi_self_add_other_not_self[simp]: "(A = A + {#x#}) = False" 

26016  315 
by (metis single_not_empty union_empty union_left_cancel) 
25610  316 

317 
lemma insert_noteq_member: 

318 
assumes BC: "B + {#b#} = C + {#c#}" 

319 
and bnotc: "b \<noteq> c" 

320 
shows "c \<in># B" 

321 
proof  

322 
have "c \<in># C + {#c#}" by simp 

323 
have nc: "\<not> c \<in># {#b#}" using bnotc by simp 

324 
hence "c \<in># B + {#b#}" using BC by simp 

325 
thus "c \<in># B" using nc by simp 

326 
qed 

327 

328 

26016  329 
lemma add_eq_conv_ex: 
330 
"(M + {#a#} = N + {#b#}) = 

331 
(M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))" 

332 
by (auto simp add: add_eq_conv_diff) 

333 

334 

335 
lemma empty_multiset_count: 

336 
"(\<forall>x. count A x = 0) = (A = {#})" 

337 
by(metis count_empty multiset_eq_conv_count_eq) 

338 

339 

15869  340 
subsubsection {* Intersection *} 
341 

342 
lemma multiset_inter_count: 

26016  343 
"count (A #\<inter> B) x = min (count A x) (count B x)" 
344 
by (simp add: multiset_inter_def min_def) 

15869  345 

346 
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A" 

26016  347 
by (simp add: multiset_eq_conv_count_eq multiset_inter_count 
21214
a91bab12b2bd
adjusted two lemma names due to name change in interpretation
haftmann
parents:
20770
diff
changeset

348 
min_max.inf_commute) 
15869  349 

350 
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C" 

26016  351 
by (simp add: multiset_eq_conv_count_eq multiset_inter_count 
21214
a91bab12b2bd
adjusted two lemma names due to name change in interpretation
haftmann
parents:
20770
diff
changeset

352 
min_max.inf_assoc) 
15869  353 

354 
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)" 

26016  355 
by (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def) 
15869  356 

17161  357 
lemmas multiset_inter_ac = 
358 
multiset_inter_commute 

359 
multiset_inter_assoc 

360 
multiset_inter_left_commute 

15869  361 

362 
lemma multiset_union_diff_commute: "B #\<inter> C = {#} \<Longrightarrow> A + B  C = A  C + B" 

17200  363 
apply (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def 
17161  364 
split: split_if_asm) 
15869  365 
apply clarsimp 
17161  366 
apply (erule_tac x = a in allE) 
15869  367 
apply auto 
368 
done 

369 

10249  370 

26016  371 
subsubsection {* Comprehension (filter) *} 
372 

373 
lemma MCollect_empty[simp, code func]: "MCollect {#} P = {#}" 

374 
by(simp add:MCollect_def Mempty_def Abs_multiset_inject in_multiset expand_fun_eq) 

375 

376 
lemma MCollect_single[simp, code func]: 

377 
"MCollect {#x#} P = (if P x then {#x#} else {#})" 

378 
by(simp add:MCollect_def Mempty_def single_def Abs_multiset_inject in_multiset expand_fun_eq) 

379 

380 
lemma MCollect_union[simp, code func]: 

381 
"MCollect (M+N) f = MCollect M f + MCollect N f" 

382 
by(simp add:MCollect_def union_def Abs_multiset_inject in_multiset expand_fun_eq) 

383 

384 

385 
subsection {* Induction and case splits *} 

10249  386 

387 
lemma setsum_decr: 

11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset

388 
"finite F ==> (0::nat) < f a ==> 
15072  389 
setsum (f (a := f a  1)) F = (if a\<in>F then setsum f F  1 else setsum f F)" 
18258  390 
apply (induct rule: finite_induct) 
391 
apply auto 

15072  392 
apply (drule_tac a = a in mk_disjoint_insert, auto) 
10249  393 
done 
394 

10313  395 
lemma rep_multiset_induct_aux: 
18730  396 
assumes 1: "P (\<lambda>a. (0::nat))" 
397 
and 2: "!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))" 

25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
24035
diff
changeset

398 
shows "\<forall>f. f \<in> multiset > setsum f {x. f x \<noteq> 0} = n > P f" 
18730  399 
apply (unfold multiset_def) 
400 
apply (induct_tac n, simp, clarify) 

401 
apply (subgoal_tac "f = (\<lambda>a.0)") 

402 
apply simp 

403 
apply (rule 1) 

404 
apply (rule ext, force, clarify) 

405 
apply (frule setsum_SucD, clarify) 

406 
apply (rename_tac a) 

25162  407 
apply (subgoal_tac "finite {x. (f (a := f a  1)) x > 0}") 
18730  408 
prefer 2 
409 
apply (rule finite_subset) 

410 
prefer 2 

411 
apply assumption 

412 
apply simp 

413 
apply blast 

414 
apply (subgoal_tac "f = (f (a := f a  1))(a := (f (a := f a  1)) a + 1)") 

415 
prefer 2 

416 
apply (rule ext) 

417 
apply (simp (no_asm_simp)) 

418 
apply (erule ssubst, rule 2 [unfolded multiset_def], blast) 

419 
apply (erule allE, erule impE, erule_tac [2] mp, blast) 

420 
apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def) 

25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
24035
diff
changeset

421 
apply (subgoal_tac "{x. x \<noteq> a > f x \<noteq> 0} = {x. f x \<noteq> 0}") 
18730  422 
prefer 2 
423 
apply blast 

25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
24035
diff
changeset

424 
apply (subgoal_tac "{x. x \<noteq> a \<and> f x \<noteq> 0} = {x. f x \<noteq> 0}  {a}") 
18730  425 
prefer 2 
426 
apply blast 

427 
apply (simp add: le_imp_diff_is_add setsum_diff1_nat cong: conj_cong) 

428 
done 

10249  429 

10313  430 
theorem rep_multiset_induct: 
11464  431 
"f \<in> multiset ==> P (\<lambda>a. 0) ==> 
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset

432 
(!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f" 
17161  433 
using rep_multiset_induct_aux by blast 
10249  434 

18258  435 
theorem multiset_induct [case_names empty add, induct type: multiset]: 
436 
assumes empty: "P {#}" 

437 
and add: "!!M x. P M ==> P (M + {#x#})" 

17161  438 
shows "P M" 
10249  439 
proof  
440 
note defns = union_def single_def Mempty_def 

441 
show ?thesis 

442 
apply (rule Rep_multiset_inverse [THEN subst]) 

10313  443 
apply (rule Rep_multiset [THEN rep_multiset_induct]) 
18258  444 
apply (rule empty [unfolded defns]) 
15072  445 
apply (subgoal_tac "f(b := f b + 1) = (\<lambda>a. f a + (if a=b then 1 else 0))") 
10249  446 
prefer 2 
447 
apply (simp add: expand_fun_eq) 

448 
apply (erule ssubst) 

17200  449 
apply (erule Abs_multiset_inverse [THEN subst]) 
26016  450 
apply (drule add [unfolded defns, simplified]) 
451 
apply(simp add:in_multiset) 

10249  452 
done 
453 
qed 

454 

25610  455 
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}" 
456 
by (induct M, auto) 

457 

458 
lemma multiset_cases [cases type, case_names empty add]: 

459 
assumes em: "M = {#} \<Longrightarrow> P" 

460 
assumes add: "\<And>N x. M = N + {#x#} \<Longrightarrow> P" 

461 
shows "P" 

462 
proof (cases "M = {#}") 

463 
assume "M = {#}" thus ?thesis using em by simp 

464 
next 

465 
assume "M \<noteq> {#}" 

466 
then obtain M' m where "M = M' + {#m#}" 

467 
by (blast dest: multi_nonempty_split) 

468 
thus ?thesis using add by simp 

469 
qed 

470 

471 
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}" 

472 
apply (cases M, simp) 

473 
apply (rule_tac x="M  {#x#}" in exI, simp) 

474 
done 

475 

26033  476 
lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#M. \<not> P x #}" 
17161  477 
by (subst multiset_eq_conv_count_eq, auto) 
10249  478 

15869  479 
declare multiset_typedef [simp del] 
10249  480 

25610  481 
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B  {#c#} \<noteq> B" 
482 
by (cases "B={#}", auto dest: multi_member_split) 

17161  483 

26016  484 
subsection {* Orderings *} 
10249  485 

486 
subsubsection {* Wellfoundedness *} 

487 

19086  488 
definition 
23751  489 
mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where 
19086  490 
"mult1 r = 
23751  491 
{(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> 
492 
(\<forall>b. b :# K > (b, a) \<in> r)}" 

10249  493 

21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21214
diff
changeset

494 
definition 
23751  495 
mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where 
496 
"mult r = (mult1 r)\<^sup>+" 

10249  497 

23751  498 
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r" 
10277  499 
by (simp add: mult1_def) 
10249  500 

23751  501 
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==> 
502 
(\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or> 

503 
(\<exists>K. (\<forall>b. b :# K > (b, a) \<in> r) \<and> N = M0 + K)" 

19582  504 
(is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2") 
10249  505 
proof (unfold mult1_def) 
23751  506 
let ?r = "\<lambda>K a. \<forall>b. b :# K > (b, a) \<in> r" 
11464  507 
let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a" 
23751  508 
let ?case1 = "?case1 {(N, M). ?R N M}" 
10249  509 

23751  510 
assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}" 
18258  511 
then have "\<exists>a' M0' K. 
11464  512 
M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp 
18258  513 
then show "?case1 \<or> ?case2" 
10249  514 
proof (elim exE conjE) 
515 
fix a' M0' K 

516 
assume N: "N = M0' + K" and r: "?r K a'" 

517 
assume "M0 + {#a#} = M0' + {#a'#}" 

18258  518 
then have "M0 = M0' \<and> a = a' \<or> 
11464  519 
(\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})" 
10249  520 
by (simp only: add_eq_conv_ex) 
18258  521 
then show ?thesis 
10249  522 
proof (elim disjE conjE exE) 
523 
assume "M0 = M0'" "a = a'" 

11464  524 
with N r have "?r K a \<and> N = M0 + K" by simp 
18258  525 
then have ?case2 .. then show ?thesis .. 
10249  526 
next 
527 
fix K' 

528 
assume "M0' = K' + {#a#}" 

529 
with N have n: "N = K' + K + {#a#}" by (simp add: union_ac) 

530 

531 
assume "M0 = K' + {#a'#}" 

532 
with r have "?R (K' + K) M0" by blast 

18258  533 
with n have ?case1 by simp then show ?thesis .. 
10249  534 
qed 
535 
qed 

536 
qed 

537 

23751  538 
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)" 
10249  539 
proof 
540 
let ?R = "mult1 r" 

541 
let ?W = "acc ?R" 

542 
{ 

543 
fix M M0 a 

23751  544 
assume M0: "M0 \<in> ?W" 
545 
and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)" 

546 
and acc_hyp: "\<forall>M. (M, M0) \<in> ?R > M + {#a#} \<in> ?W" 

547 
have "M0 + {#a#} \<in> ?W" 

548 
proof (rule accI [of "M0 + {#a#}"]) 

10249  549 
fix N 
23751  550 
assume "(N, M0 + {#a#}) \<in> ?R" 
551 
then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or> 

552 
(\<exists>K. (\<forall>b. b :# K > (b, a) \<in> r) \<and> N = M0 + K))" 

10249  553 
by (rule less_add) 
23751  554 
then show "N \<in> ?W" 
10249  555 
proof (elim exE disjE conjE) 
23751  556 
fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}" 
557 
from acc_hyp have "(M, M0) \<in> ?R > M + {#a#} \<in> ?W" .. 

558 
from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" .. 

559 
then show "N \<in> ?W" by (simp only: N) 

10249  560 
next 
561 
fix K 

562 
assume N: "N = M0 + K" 

23751  563 
assume "\<forall>b. b :# K > (b, a) \<in> r" 
564 
then have "M0 + K \<in> ?W" 

10249  565 
proof (induct K) 
18730  566 
case empty 
23751  567 
from M0 show "M0 + {#} \<in> ?W" by simp 
18730  568 
next 
569 
case (add K x) 

23751  570 
from add.prems have "(x, a) \<in> r" by simp 
571 
with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast 

572 
moreover from add have "M0 + K \<in> ?W" by simp 

573 
ultimately have "(M0 + K) + {#x#} \<in> ?W" .. 

574 
then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc) 

10249  575 
qed 
23751  576 
then show "N \<in> ?W" by (simp only: N) 
10249  577 
qed 
578 
qed 

579 
} note tedious_reasoning = this 

580 

23751  581 
assume wf: "wf r" 
10249  582 
fix M 
23751  583 
show "M \<in> ?W" 
10249  584 
proof (induct M) 
23751  585 
show "{#} \<in> ?W" 
10249  586 
proof (rule accI) 
23751  587 
fix b assume "(b, {#}) \<in> ?R" 
588 
with not_less_empty show "b \<in> ?W" by contradiction 

10249  589 
qed 
590 

23751  591 
fix M a assume "M \<in> ?W" 
592 
from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W" 

10249  593 
proof induct 
594 
fix a 

23751  595 
assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)" 
596 
show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W" 

10249  597 
proof 
23751  598 
fix M assume "M \<in> ?W" 
599 
then show "M + {#a#} \<in> ?W" 

23373  600 
by (rule acc_induct) (rule tedious_reasoning [OF _ r]) 
10249  601 
qed 
602 
qed 

23751  603 
from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" .. 
10249  604 
qed 
605 
qed 

606 

23751  607 
theorem wf_mult1: "wf r ==> wf (mult1 r)" 
23373  608 
by (rule acc_wfI) (rule all_accessible) 
10249  609 

23751  610 
theorem wf_mult: "wf r ==> wf (mult r)" 
611 
unfolding mult_def by (rule wf_trancl) (rule wf_mult1) 

10249  612 

613 

614 
subsubsection {* Closurefree presentation *} 

615 

616 
(*Badly needed: a linear arithmetic procedure for multisets*) 

617 

618 
lemma diff_union_single_conv: "a :# J ==> I + J  {#a#} = I + (J  {#a#})" 

23373  619 
by (simp add: multiset_eq_conv_count_eq) 
10249  620 

621 
text {* One direction. *} 

622 

623 
lemma mult_implies_one_step: 

23751  624 
"trans r ==> (M, N) \<in> mult r ==> 
11464  625 
\<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and> 
23751  626 
(\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)" 
10249  627 
apply (unfold mult_def mult1_def set_of_def) 
23751  628 
apply (erule converse_trancl_induct, clarify) 
15072  629 
apply (rule_tac x = M0 in exI, simp, clarify) 
23751  630 
apply (case_tac "a :# K") 
10249  631 
apply (rule_tac x = I in exI) 
632 
apply (simp (no_asm)) 

23751  633 
apply (rule_tac x = "(K  {#a#}) + Ka" in exI) 
10249  634 
apply (simp (no_asm_simp) add: union_assoc [symmetric]) 
11464  635 
apply (drule_tac f = "\<lambda>M. M  {#a#}" in arg_cong) 
10249  636 
apply (simp add: diff_union_single_conv) 
637 
apply (simp (no_asm_use) add: trans_def) 

638 
apply blast 

639 
apply (subgoal_tac "a :# I") 

640 
apply (rule_tac x = "I  {#a#}" in exI) 

641 
apply (rule_tac x = "J + {#a#}" in exI) 

642 
apply (rule_tac x = "K + Ka" in exI) 

643 
apply (rule conjI) 

644 
apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split) 

645 
apply (rule conjI) 

15072  646 
apply (drule_tac f = "\<lambda>M. M  {#a#}" in arg_cong, simp) 
10249  647 
apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split) 
648 
apply (simp (no_asm_use) add: trans_def) 

649 
apply blast 

10277  650 
apply (subgoal_tac "a :# (M0 + {#a#})") 
10249  651 
apply simp 
652 
apply (simp (no_asm)) 

653 
done 

654 

655 
lemma elem_imp_eq_diff_union: "a :# M ==> M = M  {#a#} + {#a#}" 

23373  656 
by (simp add: multiset_eq_conv_count_eq) 
10249  657 

11464  658 
lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}" 
10249  659 
apply (erule size_eq_Suc_imp_elem [THEN exE]) 
15072  660 
apply (drule elem_imp_eq_diff_union, auto) 
10249  661 
done 
662 

663 
lemma one_step_implies_mult_aux: 

23751  664 
"trans r ==> 
665 
\<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)) 

666 
> (I + K, I + J) \<in> mult r" 

15072  667 
apply (induct_tac n, auto) 
668 
apply (frule size_eq_Suc_imp_eq_union, clarify) 

669 
apply (rename_tac "J'", simp) 

670 
apply (erule notE, auto) 

10249  671 
apply (case_tac "J' = {#}") 
672 
apply (simp add: mult_def) 

23751  673 
apply (rule r_into_trancl) 
15072  674 
apply (simp add: mult1_def set_of_def, blast) 
11464  675 
txt {* Now we know @{term "J' \<noteq> {#}"}. *} 
23751  676 
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition) 
11464  677 
apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp) 
10249  678 
apply (erule ssubst) 
15072  679 
apply (simp add: Ball_def, auto) 
10249  680 
apply (subgoal_tac 
26033  681 
"((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #}, 
682 
(I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r") 

10249  683 
prefer 2 
684 
apply force 

685 
apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def) 

23751  686 
apply (erule trancl_trans) 
687 
apply (rule r_into_trancl) 

10249  688 
apply (simp add: mult1_def set_of_def) 
689 
apply (rule_tac x = a in exI) 

690 
apply (rule_tac x = "I + J'" in exI) 

691 
apply (simp add: union_ac) 

692 
done 

693 

17161  694 
lemma one_step_implies_mult: 
23751  695 
"trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r 
696 
==> (I + K, I + J) \<in> mult r" 

23373  697 
using one_step_implies_mult_aux by blast 
10249  698 

699 

700 
subsubsection {* Partialorder properties *} 

701 

12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
11868
diff
changeset

702 
instance multiset :: (type) ord .. 
10249  703 

704 
defs (overloaded) 

23751  705 
less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}" 
11464  706 
le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)" 
10249  707 

23751  708 
lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}" 
18730  709 
unfolding trans_def by (blast intro: order_less_trans) 
10249  710 

711 
text {* 

712 
\medskip Irreflexivity. 

713 
*} 

714 

715 
lemma mult_irrefl_aux: 

18258  716 
"finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) \<Longrightarrow> A = {}" 
23373  717 
by (induct rule: finite_induct) (auto intro: order_less_trans) 
10249  718 

17161  719 
lemma mult_less_not_refl: "\<not> M < (M::'a::order multiset)" 
15072  720 
apply (unfold less_multiset_def, auto) 
721 
apply (drule trans_base_order [THEN mult_implies_one_step], auto) 

10249  722 
apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]]) 
723 
apply (simp add: set_of_eq_empty_iff) 

724 
done 

725 

726 
lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R" 

23373  727 
using insert mult_less_not_refl by fast 
10249  728 

729 

730 
text {* Transitivity. *} 

731 

732 
theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)" 

23751  733 
unfolding less_multiset_def mult_def by (blast intro: trancl_trans) 
10249  734 

735 
text {* Asymmetry. *} 

736 

11464  737 
theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)" 
10249  738 
apply auto 
739 
apply (rule mult_less_not_refl [THEN notE]) 

15072  740 
apply (erule mult_less_trans, assumption) 
10249  741 
done 
742 

743 
theorem mult_less_asym: 

11464  744 
"M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P" 
15072  745 
by (insert mult_less_not_sym, blast) 
10249  746 

747 
theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)" 

18730  748 
unfolding le_multiset_def by auto 
10249  749 

750 
text {* Antisymmetry. *} 

751 

752 
theorem mult_le_antisym: 

753 
"M <= N ==> N <= M ==> M = (N::'a::order multiset)" 

18730  754 
unfolding le_multiset_def by (blast dest: mult_less_not_sym) 
10249  755 

756 
text {* Transitivity. *} 

757 

758 
theorem mult_le_trans: 

759 
"K <= M ==> M <= N ==> K <= (N::'a::order multiset)" 

18730  760 
unfolding le_multiset_def by (blast intro: mult_less_trans) 
10249  761 

11655  762 
theorem mult_less_le: "(M < N) = (M <= N \<and> M \<noteq> (N::'a::order multiset))" 
18730  763 
unfolding le_multiset_def by auto 
10249  764 

10277  765 
text {* Partial order. *} 
766 

767 
instance multiset :: (order) order 

768 
apply intro_classes 

23751  769 
apply (rule mult_less_le) 
770 
apply (rule mult_le_refl) 

771 
apply (erule mult_le_trans, assumption) 

772 
apply (erule mult_le_antisym, assumption) 

10277  773 
done 
774 

10249  775 

776 
subsubsection {* Monotonicity of multiset union *} 

777 

17161  778 
lemma mult1_union: 
23751  779 
"(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r" 
15072  780 
apply (unfold mult1_def, auto) 
10249  781 
apply (rule_tac x = a in exI) 
782 
apply (rule_tac x = "C + M0" in exI) 

783 
apply (simp add: union_assoc) 

784 
done 

785 

786 
lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)" 

787 
apply (unfold less_multiset_def mult_def) 

23751  788 
apply (erule trancl_induct) 
789 
apply (blast intro: mult1_union transI order_less_trans r_into_trancl) 

790 
apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans) 

10249  791 
done 
792 

793 
lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)" 

794 
apply (subst union_commute [of B C]) 

795 
apply (subst union_commute [of D C]) 

796 
apply (erule union_less_mono2) 

797 
done 

798 

17161  799 
lemma union_less_mono: 
10249  800 
"A < C ==> B < D ==> A + B < C + (D::'a::order multiset)" 
801 
apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans) 

802 
done 

803 

17161  804 
lemma union_le_mono: 
10249  805 
"A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)" 
18730  806 
unfolding le_multiset_def 
807 
by (blast intro: union_less_mono union_less_mono1 union_less_mono2) 

10249  808 

17161  809 
lemma empty_leI [iff]: "{#} <= (M::'a::order multiset)" 
10249  810 
apply (unfold le_multiset_def less_multiset_def) 
811 
apply (case_tac "M = {#}") 

812 
prefer 2 

23751  813 
apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))") 
10249  814 
prefer 2 
815 
apply (rule one_step_implies_mult) 

23751  816 
apply (simp only: trans_def, auto) 
10249  817 
done 
818 

17161  819 
lemma union_upper1: "A <= A + (B::'a::order multiset)" 
15072  820 
proof  
17200  821 
have "A + {#} <= A + B" by (blast intro: union_le_mono) 
18258  822 
then show ?thesis by simp 
15072  823 
qed 
824 

17161  825 
lemma union_upper2: "B <= A + (B::'a::order multiset)" 
18258  826 
by (subst union_commute) (rule union_upper1) 
15072  827 

23611  828 
instance multiset :: (order) pordered_ab_semigroup_add 
829 
apply intro_classes 

830 
apply(erule union_le_mono[OF mult_le_refl]) 

831 
done 

15072  832 

17200  833 
subsection {* Link with lists *} 
15072  834 

26016  835 
primrec multiset_of :: "'a list \<Rightarrow> 'a multiset" where 
836 
"multiset_of [] = {#}"  

837 
"multiset_of (a # x) = multiset_of x + {# a #}" 

15072  838 

839 
lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])" 

18258  840 
by (induct x) auto 
15072  841 

842 
lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])" 

18258  843 
by (induct x) auto 
15072  844 

845 
lemma set_of_multiset_of[simp]: "set_of(multiset_of x) = set x" 

18258  846 
by (induct x) auto 
15867  847 

848 
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)" 

849 
by (induct xs) auto 

15072  850 

18258  851 
lemma multiset_of_append [simp]: 
852 
"multiset_of (xs @ ys) = multiset_of xs + multiset_of ys" 

20503  853 
by (induct xs arbitrary: ys) (auto simp: union_ac) 
18730  854 

15072  855 
lemma surj_multiset_of: "surj multiset_of" 
17200  856 
apply (unfold surj_def, rule allI) 
857 
apply (rule_tac M=y in multiset_induct, auto) 

858 
apply (rule_tac x = "x # xa" in exI, auto) 

10249  859 
done 
860 

25162  861 
lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}" 
18258  862 
by (induct x) auto 
15072  863 

17200  864 
lemma distinct_count_atmost_1: 
15072  865 
"distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))" 
18258  866 
apply (induct x, simp, rule iffI, simp_all) 
17200  867 
apply (rule conjI) 
868 
apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of) 

15072  869 
apply (erule_tac x=a in allE, simp, clarify) 
17200  870 
apply (erule_tac x=aa in allE, simp) 
15072  871 
done 
872 

17200  873 
lemma multiset_of_eq_setD: 
15867  874 
"multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys" 
875 
by (rule) (auto simp add:multiset_eq_conv_count_eq set_count_greater_0) 

876 

17200  877 
lemma set_eq_iff_multiset_of_eq_distinct: 
878 
"\<lbrakk>distinct x; distinct y\<rbrakk> 

15072  879 
\<Longrightarrow> (set x = set y) = (multiset_of x = multiset_of y)" 
17200  880 
by (auto simp: multiset_eq_conv_count_eq distinct_count_atmost_1) 
15072  881 

17200  882 
lemma set_eq_iff_multiset_of_remdups_eq: 
15072  883 
"(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))" 
17200  884 
apply (rule iffI) 
885 
apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1]) 

886 
apply (drule distinct_remdups[THEN distinct_remdups 

887 
[THEN set_eq_iff_multiset_of_eq_distinct[THEN iffD2]]]) 

15072  888 
apply simp 
10249  889 
done 
890 

18258  891 
lemma multiset_of_compl_union [simp]: 
23281  892 
"multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs" 
15630  893 
by (induct xs) (auto simp: union_ac) 
15072  894 

17200  895 
lemma count_filter: 
23281  896 
"count (multiset_of xs) x = length [y \<leftarrow> xs. y = x]" 
18258  897 
by (induct xs) auto 
15867  898 

899 

15072  900 
subsection {* Pointwise ordering induced by count *} 
901 

19086  902 
definition 
25610  903 
mset_le :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<le>#" 50) where 
904 
"(A \<le># B) = (\<forall>a. count A a \<le> count B a)" 

23611  905 
definition 
25610  906 
mset_less :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where 
907 
"(A <# B) = (A \<le># B \<and> A \<noteq> B)" 

908 

909 
notation mset_le (infix "\<subseteq>#" 50) 

910 
notation mset_less (infix "\<subset>#" 50) 

15072  911 

23611  912 
lemma mset_le_refl[simp]: "A \<le># A" 
18730  913 
unfolding mset_le_def by auto 
15072  914 

23611  915 
lemma mset_le_trans: "\<lbrakk> A \<le># B; B \<le># C \<rbrakk> \<Longrightarrow> A \<le># C" 
18730  916 
unfolding mset_le_def by (fast intro: order_trans) 
15072  917 

23611  918 
lemma mset_le_antisym: "\<lbrakk> A \<le># B; B \<le># A \<rbrakk> \<Longrightarrow> A = B" 
17200  919 
apply (unfold mset_le_def) 
920 
apply (rule multiset_eq_conv_count_eq[THEN iffD2]) 

15072  921 
apply (blast intro: order_antisym) 
922 
done 

923 

17200  924 
lemma mset_le_exists_conv: 
23611  925 
"(A \<le># B) = (\<exists>C. B = A + C)" 
926 
apply (unfold mset_le_def, rule iffI, rule_tac x = "B  A" in exI) 

15072  927 
apply (auto intro: multiset_eq_conv_count_eq [THEN iffD2]) 
928 
done 

929 

23611  930 
lemma mset_le_mono_add_right_cancel[simp]: "(A + C \<le># B + C) = (A \<le># B)" 
18730  931 
unfolding mset_le_def by auto 
15072  932 

23611  933 
lemma mset_le_mono_add_left_cancel[simp]: "(C + A \<le># C + B) = (A \<le># B)" 
18730  934 
unfolding mset_le_def by auto 
15072  935 

23611  936 
lemma mset_le_mono_add: "\<lbrakk> A \<le># B; C \<le># D \<rbrakk> \<Longrightarrow> A + C \<le># B + D" 
17200  937 
apply (unfold mset_le_def) 
938 
apply auto 

15072  939 
apply (erule_tac x=a in allE)+ 
940 
apply auto 

941 
done 

942 

23611  943 
lemma mset_le_add_left[simp]: "A \<le># A + B" 
18730  944 
unfolding mset_le_def by auto 
15072  945 

23611  946 
lemma mset_le_add_right[simp]: "B \<le># A + B" 
18730  947 
unfolding mset_le_def by auto 
15072  948 

23611  949 
lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le># multiset_of xs" 
950 
apply (induct xs) 

951 
apply auto 

952 
apply (rule mset_le_trans) 

953 
apply auto 

954 
done 

955 

25208  956 
interpretation mset_order: 
957 
order ["op \<le>#" "op <#"] 

958 
by (auto intro: order.intro mset_le_refl mset_le_antisym 

959 
mset_le_trans simp: mset_less_def) 

23611  960 

961 
interpretation mset_order_cancel_semigroup: 

25622  962 
pordered_cancel_ab_semigroup_add ["op +" "op \<le>#" "op <#"] 
25208  963 
by unfold_locales (erule mset_le_mono_add [OF mset_le_refl]) 
23611  964 

965 
interpretation mset_order_semigroup_cancel: 

25622  966 
pordered_ab_semigroup_add_imp_le ["op +" "op \<le>#" "op <#"] 
25208  967 
by (unfold_locales) simp 
15072  968 

25610  969 

970 
lemma mset_lessD: 

971 
"\<lbrakk> A \<subset># B ; x \<in># A \<rbrakk> \<Longrightarrow> x \<in># B" 

972 
apply (clarsimp simp: mset_le_def mset_less_def) 

973 
apply (erule_tac x=x in allE) 

974 
apply auto 

975 
done 

976 

977 
lemma mset_leD: 

978 
"\<lbrakk> A \<subseteq># B ; x \<in># A \<rbrakk> \<Longrightarrow> x \<in># B" 

979 
apply (clarsimp simp: mset_le_def mset_less_def) 

980 
apply (erule_tac x=x in allE) 

981 
apply auto 

982 
done 

983 

984 
lemma mset_less_insertD: 

985 
"(A + {#x#} \<subset># B) \<Longrightarrow> (x \<in># B \<and> A \<subset># B)" 

986 
apply (rule conjI) 

987 
apply (simp add: mset_lessD) 

988 
apply (clarsimp simp: mset_le_def mset_less_def) 

989 
apply safe 

990 
apply (erule_tac x=a in allE) 

991 
apply (auto split: split_if_asm) 

992 
done 

993 

994 
lemma mset_le_insertD: 

995 
"(A + {#x#} \<subseteq># B) \<Longrightarrow> (x \<in># B \<and> A \<subseteq># B)" 

996 
apply (rule conjI) 

997 
apply (simp add: mset_leD) 

998 
apply (force simp: mset_le_def mset_less_def split: split_if_asm) 

999 
done 

1000 

1001 
lemma mset_less_of_empty[simp]: "A \<subset># {#} = False" 

1002 
by (induct A, auto simp: mset_le_def mset_less_def) 

1003 

1004 
lemma multi_psub_of_add_self[simp]: "A \<subset># A + {#x#}" 

1005 
by (clarsimp simp: mset_le_def mset_less_def) 

1006 

1007 
lemma multi_psub_self[simp]: "A \<subset># A = False" 

1008 
by (clarsimp simp: mset_le_def mset_less_def) 

1009 

1010 
lemma mset_less_add_bothsides: 

1011 
"T + {#x#} \<subset># S + {#x#} \<Longrightarrow> T \<subset># S" 

1012 
by (clarsimp simp: mset_le_def mset_less_def) 

1013 

1014 
lemma mset_less_empty_nonempty: "({#} \<subset># S) = (S \<noteq> {#})" 

1015 
by (auto simp: mset_le_def mset_less_def) 

1016 

1017 
lemma mset_less_size: "A \<subset># B \<Longrightarrow> size A < size B" 

1018 
proof (induct A arbitrary: B) 

1019 
case (empty M) 

1020 
hence "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty) 

1021 
then obtain M' x where "M = M' + {#x#}" 

1022 
by (blast dest: multi_nonempty_split) 

1023 
thus ?case by simp 

1024 
next 

1025 
case (add S x T) 

1026 
have IH: "\<And>B. S \<subset># B \<Longrightarrow> size S < size B" by fact 

1027 
have SxsubT: "S + {#x#} \<subset># T" by fact 

1028 
hence "x \<in># T" and "S \<subset># T" by (auto dest: mset_less_insertD) 

1029 
then obtain T' where T: "T = T' + {#x#}" 

1030 
by (blast dest: multi_member_split) 

1031 
hence "S \<subset># T'" using SxsubT 

1032 
by (blast intro: mset_less_add_bothsides) 

1033 
hence "size S < size T'" using IH by simp 

1034 
thus ?case using T by simp 

1035 
qed 

1036 

1037 
lemmas mset_less_trans = mset_order.less_eq_less.less_trans 

1038 

1039 
lemma mset_less_diff_self: "c \<in># B \<Longrightarrow> B  {#c#} \<subset># B" 

1040 
by (auto simp: mset_le_def mset_less_def multi_drop_mem_not_eq) 

1041 

1042 
subsection {* Strong induction and subset induction for multisets *} 

1043 

26016  1044 
text {* Wellfoundedness of proper subset operator: *} 
25610  1045 

1046 
definition 

1047 
mset_less_rel :: "('a multiset * 'a multiset) set" 

1048 
where 

1049 
{* proper multiset subset *} 

1050 
"mset_less_rel \<equiv> {(A,B). A \<subset># B}" 

1051 

1052 
lemma multiset_add_sub_el_shuffle: 

1053 
assumes cinB: "c \<in># B" and bnotc: "b \<noteq> c" 

1054 
shows "B  {#c#} + {#b#} = B + {#b#}  {#c#}" 

1055 
proof  

1056 
from cinB obtain A where B: "B = A + {#c#}" 

1057 
by (blast dest: multi_member_split) 

1058 
have "A + {#b#} = A + {#b#} + {#c#}  {#c#}" by simp 

1059 
hence "A + {#b#} = A + {#c#} + {#b#}  {#c#}" 

1060 
by (simp add: union_ac) 

1061 
thus ?thesis using B by simp 

1062 
qed 

1063 

1064 
lemma wf_mset_less_rel: "wf mset_less_rel" 

1065 
apply (unfold mset_less_rel_def) 

1066 
apply (rule wf_measure [THEN wf_subset, where f1=size]) 

1067 
apply (clarsimp simp: measure_def inv_image_def mset_less_size) 

1068 
done 

1069 

26016  1070 
text {* The induction rules: *} 
25610  1071 

1072 
lemma full_multiset_induct [case_names less]: 

1073 
assumes ih: "\<And>B. \<forall>A. A \<subset># B \<longrightarrow> P A \<Longrightarrow> P B" 

1074 
shows "P B" 

1075 
apply (rule wf_mset_less_rel [THEN wf_induct]) 

1076 
apply (rule ih, auto simp: mset_less_rel_def) 

1077 
done 

1078 

1079 
lemma multi_subset_induct [consumes 2, case_names empty add]: 

1080 
assumes "F \<subseteq># A" 

1081 
and empty: "P {#}" 

1082 
and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})" 

1083 
shows "P F" 

1084 
proof  

1085 
from `F \<subseteq># A` 

1086 
show ?thesis 

1087 
proof (induct F) 

1088 
show "P {#}" by fact 

1089 
next 

1090 
fix x F 

1091 
assume P: "F \<subseteq># A \<Longrightarrow> P F" and i: "F + {#x#} \<subseteq># A" 

1092 
show "P (F + {#x#})" 

1093 
proof (rule insert) 

1094 
from i show "x \<in># A" by (auto dest: mset_le_insertD) 

1095 
from i have "F \<subseteq># A" by (auto simp: mset_le_insertD) 

1096 
with P show "P F" . 

1097 
qed 

1098 
qed 

1099 
qed 

1100 

26016  1101 
text{* A consequence: Extensionality. *} 
25610  1102 

1103 
lemma multi_count_eq: 

1104 
"(\<forall>x. count A x = count B x) = (A = B)" 

1105 
apply (rule iffI) 

1106 
prefer 2 

1107 
apply clarsimp 

1108 
apply (induct A arbitrary: B rule: full_multiset_induct) 

1109 
apply (rename_tac C) 

1110 
apply (case_tac B rule: multiset_cases) 

1111 
apply (simp add: empty_multiset_count) 

1112 
apply simp 

1113 
apply (case_tac "x \<in># C") 

1114 
apply (force dest: multi_member_split) 

1115 
apply (erule_tac x=x in allE) 

1116 
apply simp 

1117 
done 

1118 

1119 
lemmas multi_count_ext = multi_count_eq [THEN iffD1, rule_format] 

1120 

1121 
subsection {* The fold combinator *} 

1122 

1123 
text {* The intended behaviour is 

25759  1124 
@{text "fold_mset f z {#x\<^isub>1, ..., x\<^isub>n#} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"} 
25610  1125 
if @{text f} is associativecommutative. 
1126 
*} 

1127 

25759  1128 
(* the graph of fold_mset, z = the start element, f = folding function, 
25610  1129 
A the multiset, y the result *) 
1130 
inductive 

25759  1131 
fold_msetG :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b \<Rightarrow> bool" 
25610  1132 
for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" 
1133 
and z :: 'b 

1134 
where 

25759  1135 
emptyI [intro]: "fold_msetG f z {#} z" 
1136 
 insertI [intro]: "fold_msetG f z A y \<Longrightarrow> fold_msetG f z (A + {#x#}) (f x y)" 

25610  1137 

25759  1138 
inductive_cases empty_fold_msetGE [elim!]: "fold_msetG f z {#} x" 
1139 
inductive_cases insert_fold_msetGE: "fold_msetG f z (A + {#}) y" 

25610  1140 

1141 
definition 

25759  1142 
fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b" 
25610  1143 
where 
25759  1144 
"fold_mset f z A \<equiv> THE x. fold_msetG f z A x" 
25610  1145 

25759  1146 
lemma Diff1_fold_msetG: 
1147 
"\<lbrakk> fold_msetG f z (A  {#x#}) y; x \<in># A \<rbrakk> \<Longrightarrow> fold_msetG f z A (f x y)" 

1148 
by (frule_tac x=x in fold_msetG.insertI, auto) 

25610  1149 

25759  1150 
lemma fold_msetG_nonempty: "\<exists>x. fold_msetG f z A x" 
25610  1151 
apply (induct A) 
1152 
apply blast 

1153 
apply clarsimp 

25759  1154 
apply (drule_tac x=x in fold_msetG.insertI) 
25610  1155 
apply auto 
1156 
done 

1157 

25759  1158 
lemma fold_mset_empty[simp]: "fold_mset f z {#} = z" 
1159 
by (unfold fold_mset_def, blast) 

25610  1160 

1161 
locale left_commutative = 

25623  1162 
fixes f :: "'a => 'b => 'b" 
1163 
assumes left_commute: "f x (f y z) = f y (f x z)" 

25610  1164 

25759  1165 
lemma (in left_commutative) fold_msetG_determ: 
1166 
"\<lbrakk>fold_msetG f z A x; fold_msetG f z A y\<rbrakk> \<Longrightarrow> y = x" 

25610  1167 
proof (induct arbitrary: x y z rule: full_multiset_induct) 
1168 
case (less M x\<^isub>1 x\<^isub>2 Z) 

1169 
have IH: "\<forall>A. A \<subset># M \<longrightarrow> 

25759  1170 
(\<forall>x x' x''. fold_msetG f x'' A x \<longrightarrow> fold_msetG f x'' A x' 
25610  1171 
\<longrightarrow> x' = x)" by fact 
25759  1172 
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+ 
25610  1173 
show ?case 
25759  1174 
proof (rule fold_msetG.cases [OF Mfoldx\<^isub>1]) 
25610  1175 
assume "M = {#}" and "x\<^isub>1 = Z" 
1176 
thus ?case using Mfoldx\<^isub>2 by auto 

1177 
next 

1178 
fix B b u 

25759  1179 
assume "M = B + {#b#}" and "x\<^isub>1 = f b u" and Bu: "fold_msetG f Z B u" 
25623  1180 
hence MBb: "M = B + {#b#}" and x\<^isub>1: "x\<^isub>1 = f b u" by auto 
25610  1181 
show ?case 
25759  1182 
proof (rule fold_msetG.cases [OF Mfoldx\<^isub>2]) 
25610  1183 
assume "M = {#}" "x\<^isub>2 = Z" 
1184 
thus ?case using Mfoldx\<^isub>1 by auto 

1185 
next 

1186 
fix C c v 

25759  1187 
assume "M = C + {#c#}" and "x\<^isub>2 = f c v" and Cv: "fold_msetG f Z C v" 
25623  1188 
hence MCc: "M = C + {#c#}" and x\<^isub>2: "x\<^isub>2 = f c v" by auto 
25610  1189 
hence CsubM: "C \<subset># M" by simp 
1190 
from MBb have BsubM: "B \<subset># M" by simp 

1191 
show ?case 

1192 
proof cases 

1193 
assume "b=c" 

1194 
then moreover have "B = C" using MBb MCc by auto 

1195 
ultimately show ?thesis using Bu Cv x\<^isub>1 x\<^isub>2 CsubM IH by auto 

1196 
next 

1197 
assume diff: "b \<noteq> c" 

1198 
let ?D = "B  {#c#}" 

1199 
have cinB: "c \<in># B" and binC: "b \<in># C" using MBb MCc diff 

1200 
by (auto intro: insert_noteq_member dest: sym) 

1201 
have "B  {#c#} \<subset># B" using cinB by (rule mset_less_diff_self) 

1202 
hence DsubM: "?D \<subset># M" using BsubM by (blast intro: mset_less_trans) 

1203 
from MBb MCc have "B + {#b#} = C + {#c#}" by blast 

1204 
hence [simp]: "B + {#b#}  {#c#} = C" 

1205 
using MBb MCc binC cinB by auto 

1206 
have B: "B = ?D + {#c#}" and C: "C = ?D + {#b#}" 

1207 
using MBb MCc diff binC cinB 

1208 
by (auto simp: multiset_add_sub_el_shuffle) 

25759  1209 
then obtain d where Dfoldd: "fold_msetG f Z ?D d" 
1210 
using fold_msetG_nonempty by iprover 

1211 
hence "fold_msetG f Z B (f c d)" using cinB 

1212 
by (rule Diff1_fold_msetG) 

25623  1213 
hence "f c d = u" using IH BsubM Bu by blast 
25610  1214 
moreover 
25759  1215 
have "fold_msetG f Z C (f b d)" using binC cinB diff Dfoldd 
25610  1216 
by (auto simp: multiset_add_sub_el_shuffle 
25759  1217 
dest: fold_msetG.insertI [where x=b]) 
25623  1218 
hence "f b d = v" using IH CsubM Cv by blast 
25610  1219 
ultimately show ?thesis using x\<^isub>1 x\<^isub>2 
1220 
by (auto simp: left_commute) 

1221 
qed 

1222 
qed 

1223 
qed 

1224 
qed 

1225 

25759  1226 
lemma (in left_commutative) fold_mset_insert_aux: " 
1227 
(fold_msetG f z (A + {#x#}) v) = 

1228 
(\<exists>y. fold_msetG f z A y \<and> v = f x y)" 

25610  1229 
apply (rule iffI) 
1230 
prefer 2 

1231 
apply blast 

25759  1232 
apply (rule_tac A=A and f=f in fold_msetG_nonempty [THEN exE, standard]) 
1233 
apply (blast intro: fold_msetG_determ) 

25610  1234 
done 
1235 

25759  1236 
lemma (in left_commutative) fold_mset_equality: "fold_msetG f z A y \<Longrightarrow> fold_mset f z A = y" 
1237 
by (unfold fold_mset_def) (blast intro: fold_msetG_determ) 

25610  1238 

25759  1239 
lemma (in left_commutative) fold_mset_insert: 
1240 
"fold_mset f z (A + {#x#}) = f x (fold_mset f z A)" 

1241 
apply (simp add: fold_mset_def fold_mset_insert_aux union_commute) 

25610  1242 
apply (rule the_equality) 
1243 
apply (auto cong add: conj_cong 

25759  1244 
simp add: fold_mset_def [symmetric] fold_mset_equality fold_msetG_nonempty) 
1245 
done 

1246 

1247 
lemma (in left_commutative) fold_mset_insert_idem: 

1248 
shows "fold_mset f z (A + {#a#}) = f a (fold_mset f z A)" 

1249 
apply (simp add: fold_mset_def fold_mset_insert_aux) 

1250 
apply (rule the_equality) 

1251 
apply (auto cong add: conj_cong 

1252 
simp add: fold_mset_def [symmetric] fold_mset_equality fold_msetG_nonempty) 

25610  1253 
done 
1254 

25759  1255 
lemma (in left_commutative) fold_mset_commute: 
1256 
"f x (fold_mset f z A) = fold_mset f (f x z) A" 

1257 
by (induct A, auto simp: fold_mset_insert left_commute [of x]) 

1258 

1259 
lemma (in left_commutative) fold_mset_single [simp]: 

1260 
"fold_mset f z {#x#} = f x z" 

1261 
using fold_mset_insert[of z "{#}"] by simp 

25610  1262 

25759  1263 
lemma (in left_commutative) fold_mset_union [simp]: 
1264 
"fold_mset f z (A+B) = fold_mset f (fold_mset f z A) B" 

1265 
proof (induct A) 

1266 
case empty thus ?case by simp 

1267 
next 

1268 
case (add A x) 

1269 
have "A + {#x#} + B = (A+B) + {#x#}" by(simp add:union_ac) 

1270 
hence "fold_mset f z (A + {#x#} + B) = f x (fold_mset f z (A + B))" 

1271 
by (simp add: fold_mset_insert) 

1272 
also have "\<dots> = fold_mset f (fold_mset f z (A + {#x#})) B" 

1273 
by (simp add: fold_mset_commute[of x,symmetric] add fold_mset_insert) 

1274 
finally show ?case . 

1275 
qed 

1276 

1277 
lemma (in left_commutative) fold_mset_fusion: 

25610  1278 
includes left_commutative g 
25759  1279 
shows "\<lbrakk>\<And>x y. h (g x y) = f x (h y) \<rbrakk> \<Longrightarrow> h (fold_mset g w A) = fold_mset f (h w) A" 
25610  1280 
by (induct A, auto) 
1281 

25759  1282 
lemma (in left_commutative) fold_mset_rec: 
25610  1283 
assumes a: "a \<in># A" 
25759  1284 
shows "fold_mset f z A = f a (fold_mset f z (A  {#a#}))" 
25610  1285 
proof  
1286 
from a obtain A' where "A = A' + {#a#}" by (blast dest: multi_member_split) 

1287 
thus ?thesis by simp 

1288 
qed 

1289 

26016  1290 
text{* A note on code generation: When defining some 
1291 
function containing a subterm @{term"fold_mset F"}, code generation is 
