author | haftmann |
Thu, 19 Jun 2025 17:15:40 +0200 | |
changeset 82734 | 89347c0cc6a3 |
parent 82596 | 267db8c321c4 |
permissions | -rw-r--r-- |
51599 | 1 |
(* Title: HOL/Library/DAList_Multiset.thy |
2 |
Author: Lukas Bulwahn, TU Muenchen |
|
3 |
*) |
|
4 |
||
58881 | 5 |
section \<open>Multisets partially implemented by association lists\<close> |
51599 | 6 |
|
7 |
theory DAList_Multiset |
|
8 |
imports Multiset DAList |
|
9 |
begin |
|
10 |
||
58806 | 11 |
text \<open>Delete prexisting code equations\<close> |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
12 |
|
66148 | 13 |
declare [[code drop: "{#}" Multiset.is_empty add_mset |
14 |
"plus :: 'a multiset \<Rightarrow> _" "minus :: 'a multiset \<Rightarrow> _" |
|
73393 | 15 |
inter_mset union_mset image_mset filter_mset count |
66148 | 16 |
"size :: _ multiset \<Rightarrow> nat" sum_mset prod_mset |
17 |
set_mset sorted_list_of_multiset subset_mset subseteq_mset |
|
18 |
equal_multiset_inst.equal_multiset]] |
|
19 |
||
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
20 |
|
58806 | 21 |
text \<open>Raw operations on lists\<close> |
51599 | 22 |
|
58806 | 23 |
definition join_raw :: |
24 |
"('key \<Rightarrow> 'val \<times> 'val \<Rightarrow> 'val) \<Rightarrow> |
|
25 |
('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
|
26 |
where "join_raw f xs ys = foldr (\<lambda>(k, v). map_default k v (\<lambda>v'. f k (v', v))) ys xs" |
|
51599 | 27 |
|
58806 | 28 |
lemma join_raw_Nil [simp]: "join_raw f xs [] = xs" |
29 |
by (simp add: join_raw_def) |
|
51599 | 30 |
|
31 |
lemma join_raw_Cons [simp]: |
|
58806 | 32 |
"join_raw f xs ((k, v) # ys) = map_default k v (\<lambda>v'. f k (v', v)) (join_raw f xs ys)" |
33 |
by (simp add: join_raw_def) |
|
51599 | 34 |
|
35 |
lemma map_of_join_raw: |
|
36 |
assumes "distinct (map fst ys)" |
|
58806 | 37 |
shows "map_of (join_raw f xs ys) x = |
38 |
(case map_of xs x of |
|
39 |
None \<Rightarrow> map_of ys x |
|
40 |
| Some v \<Rightarrow> (case map_of ys x of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (f x (v, v'))))" |
|
41 |
using assms |
|
42 |
apply (induct ys) |
|
43 |
apply (auto simp add: map_of_map_default split: option.split) |
|
44 |
apply (metis map_of_eq_None_iff option.simps(2) weak_map_of_SomeI) |
|
45 |
apply (metis Some_eq_map_of_iff map_of_eq_None_iff option.simps(2)) |
|
46 |
done |
|
51599 | 47 |
|
48 |
lemma distinct_join_raw: |
|
49 |
assumes "distinct (map fst xs)" |
|
50 |
shows "distinct (map fst (join_raw f xs ys))" |
|
58806 | 51 |
using assms |
51599 | 52 |
proof (induct ys) |
58806 | 53 |
case Nil |
54 |
then show ?case by simp |
|
55 |
next |
|
51599 | 56 |
case (Cons y ys) |
58806 | 57 |
then show ?case by (cases y) (simp add: distinct_map_default) |
58 |
qed |
|
51599 | 59 |
|
58806 | 60 |
definition "subtract_entries_raw xs ys = foldr (\<lambda>(k, v). AList.map_entry k (\<lambda>v'. v' - v)) ys xs" |
51599 | 61 |
|
62 |
lemma map_of_subtract_entries_raw: |
|
63 |
assumes "distinct (map fst ys)" |
|
58806 | 64 |
shows "map_of (subtract_entries_raw xs ys) x = |
65 |
(case map_of xs x of |
|
66 |
None \<Rightarrow> None |
|
67 |
| Some v \<Rightarrow> (case map_of ys x of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (v - v')))" |
|
68 |
using assms |
|
69 |
unfolding subtract_entries_raw_def |
|
70 |
apply (induct ys) |
|
71 |
apply auto |
|
72 |
apply (simp split: option.split) |
|
73 |
apply (simp add: map_of_map_entry) |
|
74 |
apply (auto split: option.split) |
|
75 |
apply (metis map_of_eq_None_iff option.simps(3) option.simps(4)) |
|
76 |
apply (metis map_of_eq_None_iff option.simps(4) option.simps(5)) |
|
77 |
done |
|
51599 | 78 |
|
79 |
lemma distinct_subtract_entries_raw: |
|
80 |
assumes "distinct (map fst xs)" |
|
81 |
shows "distinct (map fst (subtract_entries_raw xs ys))" |
|
58806 | 82 |
using assms |
83 |
unfolding subtract_entries_raw_def |
|
84 |
by (induct ys) (auto simp add: distinct_map_entry) |
|
51599 | 85 |
|
86 |
||
58806 | 87 |
text \<open>Operations on alists with distinct keys\<close> |
51599 | 88 |
|
58806 | 89 |
lift_definition join :: "('a \<Rightarrow> 'b \<times> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist" |
90 |
is join_raw |
|
91 |
by (simp add: distinct_join_raw) |
|
51599 | 92 |
|
93 |
lift_definition subtract_entries :: "('a, ('b :: minus)) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist" |
|
58806 | 94 |
is subtract_entries_raw |
95 |
by (simp add: distinct_subtract_entries_raw) |
|
51599 | 96 |
|
97 |
||
58806 | 98 |
text \<open>Implementing multisets by means of association lists\<close> |
51599 | 99 |
|
58806 | 100 |
definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat" |
101 |
where "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)" |
|
102 |
||
73270 | 103 |
lemma count_of_multiset: "finite {x. 0 < count_of xs x}" |
51599 | 104 |
proof - |
58806 | 105 |
let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0::nat | Some n \<Rightarrow> n)}" |
51599 | 106 |
have "?A \<subseteq> dom (map_of xs)" |
107 |
proof |
|
108 |
fix x |
|
109 |
assume "x \<in> ?A" |
|
58806 | 110 |
then have "0 < (case map_of xs x of None \<Rightarrow> 0::nat | Some n \<Rightarrow> n)" |
111 |
by simp |
|
112 |
then have "map_of xs x \<noteq> None" |
|
113 |
by (cases "map_of xs x") auto |
|
114 |
then show "x \<in> dom (map_of xs)" |
|
115 |
by auto |
|
51599 | 116 |
qed |
117 |
with finite_dom_map_of [of xs] have "finite ?A" |
|
118 |
by (auto intro: finite_subset) |
|
119 |
then show ?thesis |
|
73270 | 120 |
by (simp add: count_of_def fun_eq_iff) |
51599 | 121 |
qed |
122 |
||
123 |
lemma count_simps [simp]: |
|
124 |
"count_of [] = (\<lambda>_. 0)" |
|
125 |
"count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)" |
|
126 |
by (simp_all add: count_of_def fun_eq_iff) |
|
127 |
||
58806 | 128 |
lemma count_of_empty: "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 0" |
51599 | 129 |
by (induct xs) (simp_all add: count_of_def) |
130 |
||
58806 | 131 |
lemma count_of_filter: "count_of (List.filter (P \<circ> fst) xs) x = (if P x then count_of xs x else 0)" |
51599 | 132 |
by (induct xs) auto |
133 |
||
134 |
lemma count_of_map_default [simp]: |
|
58806 | 135 |
"count_of (map_default x b (\<lambda>x. x + b) xs) y = |
136 |
(if x = y then count_of xs x + b else count_of xs y)" |
|
137 |
unfolding count_of_def by (simp add: map_of_map_default split: option.split) |
|
51599 | 138 |
|
139 |
lemma count_of_join_raw: |
|
58806 | 140 |
"distinct (map fst ys) \<Longrightarrow> |
141 |
count_of xs x + count_of ys x = count_of (join_raw (\<lambda>x (x, y). x + y) xs ys) x" |
|
142 |
unfolding count_of_def by (simp add: map_of_join_raw split: option.split) |
|
51599 | 143 |
|
144 |
lemma count_of_subtract_entries_raw: |
|
58806 | 145 |
"distinct (map fst ys) \<Longrightarrow> |
146 |
count_of xs x - count_of ys x = count_of (subtract_entries_raw xs ys) x" |
|
147 |
unfolding count_of_def by (simp add: map_of_subtract_entries_raw split: option.split) |
|
51599 | 148 |
|
149 |
||
58806 | 150 |
text \<open>Code equations for multiset operations\<close> |
51599 | 151 |
|
58806 | 152 |
definition Bag :: "('a, nat) alist \<Rightarrow> 'a multiset" |
153 |
where "Bag xs = Abs_multiset (count_of (DAList.impl_of xs))" |
|
51599 | 154 |
|
155 |
code_datatype Bag |
|
156 |
||
58806 | 157 |
lemma count_Bag [simp, code]: "count (Bag xs) = count_of (DAList.impl_of xs)" |
158 |
by (simp add: Bag_def count_of_multiset) |
|
51599 | 159 |
|
82596 | 160 |
lemma Bag_eq: |
161 |
\<open>Bag ms = (\<Sum>(a, n)\<leftarrow>alist.impl_of ms. replicate_mset n a)\<close> |
|
162 |
for ms :: \<open>('a, nat) alist\<close> |
|
163 |
proof - |
|
164 |
have *: \<open>count_of xs a = count (\<Sum>(a, n)\<leftarrow>xs. replicate_mset n a) a\<close> |
|
165 |
if \<open>distinct (map fst xs)\<close> |
|
166 |
for xs and a :: 'a |
|
167 |
using that proof (induction xs) |
|
168 |
case Nil |
|
169 |
then show ?case |
|
170 |
by simp |
|
171 |
next |
|
172 |
case (Cons xn xs) |
|
173 |
then show ?case by (cases xn) |
|
174 |
(auto simp add: count_eq_zero_iff if_split_mem2 image_iff) |
|
175 |
qed |
|
176 |
show ?thesis |
|
177 |
by (rule multiset_eqI) (simp add: *) |
|
178 |
qed |
|
179 |
||
58806 | 180 |
lemma Mempty_Bag [code]: "{#} = Bag (DAList.empty)" |
51599 | 181 |
by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def) |
182 |
||
63195 | 183 |
lift_definition is_empty_Bag_impl :: "('a, nat) alist \<Rightarrow> bool" is |
184 |
"\<lambda>xs. list_all (\<lambda>x. snd x = 0) xs" . |
|
185 |
||
186 |
lemma is_empty_Bag [code]: "Multiset.is_empty (Bag xs) \<longleftrightarrow> is_empty_Bag_impl xs" |
|
187 |
proof - |
|
188 |
have "Multiset.is_empty (Bag xs) \<longleftrightarrow> (\<forall>x. count (Bag xs) x = 0)" |
|
189 |
unfolding Multiset.is_empty_def multiset_eq_iff by simp |
|
190 |
also have "\<dots> \<longleftrightarrow> (\<forall>x\<in>fst ` set (alist.impl_of xs). count (Bag xs) x = 0)" |
|
191 |
proof (intro iffI allI ballI) |
|
192 |
fix x assume A: "\<forall>x\<in>fst ` set (alist.impl_of xs). count (Bag xs) x = 0" |
|
193 |
thus "count (Bag xs) x = 0" |
|
194 |
proof (cases "x \<in> fst ` set (alist.impl_of xs)") |
|
195 |
case False |
|
196 |
thus ?thesis by (force simp: count_of_def split: option.splits) |
|
197 |
qed (insert A, auto) |
|
198 |
qed simp_all |
|
199 |
also have "\<dots> \<longleftrightarrow> list_all (\<lambda>x. snd x = 0) (alist.impl_of xs)" |
|
200 |
by (auto simp: count_of_def list_all_def) |
|
201 |
finally show ?thesis by (simp add: is_empty_Bag_impl.rep_eq) |
|
202 |
qed |
|
203 |
||
58806 | 204 |
lemma union_Bag [code]: "Bag xs + Bag ys = Bag (join (\<lambda>x (n1, n2). n1 + n2) xs ys)" |
205 |
by (rule multiset_eqI) |
|
206 |
(simp add: count_of_join_raw alist.Alist_inverse distinct_join_raw join_def) |
|
51599 | 207 |
|
63793
e68a0b651eb5
add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
63310
diff
changeset
|
208 |
lemma add_mset_Bag [code]: "add_mset x (Bag xs) = |
e68a0b651eb5
add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
63310
diff
changeset
|
209 |
Bag (join (\<lambda>x (n1, n2). n1 + n2) (DAList.update x 1 DAList.empty) xs)" |
e68a0b651eb5
add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
63310
diff
changeset
|
210 |
unfolding add_mset_add_single[of x "Bag xs"] union_Bag[symmetric] |
e68a0b651eb5
add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
63310
diff
changeset
|
211 |
by (simp add: multiset_eq_iff update.rep_eq empty.rep_eq) |
e68a0b651eb5
add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
63310
diff
changeset
|
212 |
|
58806 | 213 |
lemma minus_Bag [code]: "Bag xs - Bag ys = Bag (subtract_entries xs ys)" |
214 |
by (rule multiset_eqI) |
|
215 |
(simp add: count_of_subtract_entries_raw alist.Alist_inverse |
|
216 |
distinct_subtract_entries_raw subtract_entries_def) |
|
51599 | 217 |
|
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59949
diff
changeset
|
218 |
lemma filter_Bag [code]: "filter_mset P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)" |
58806 | 219 |
by (rule multiset_eqI) (simp add: count_of_filter DAList.filter.rep_eq) |
51599 | 220 |
|
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
51623
diff
changeset
|
221 |
|
64587 | 222 |
lemma mset_eq [code]: "HOL.equal (m1::'a::equal multiset) m2 \<longleftrightarrow> m1 \<subseteq># m2 \<and> m2 \<subseteq># m1" |
73411 | 223 |
by (metis equal_multiset_def subset_mset.order_eq_iff) |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
51623
diff
changeset
|
224 |
|
69593 | 225 |
text \<open>By default the code for \<open><\<close> is \<^prop>\<open>xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> xs = ys\<close>. |
61585 | 226 |
With equality implemented by \<open>\<le>\<close>, this leads to three calls of \<open>\<le>\<close>. |
58806 | 227 |
Here is a more efficient version:\<close> |
64587 | 228 |
lemma mset_less[code]: "xs \<subset># (ys :: 'a multiset) \<longleftrightarrow> xs \<subseteq># ys \<and> \<not> ys \<subseteq># xs" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59998
diff
changeset
|
229 |
by (rule subset_mset.less_le_not_le) |
55887 | 230 |
|
231 |
lemma mset_less_eq_Bag0: |
|
64587 | 232 |
"Bag xs \<subseteq># A \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). count_of (DAList.impl_of xs) x \<le> count A x)" |
51599 | 233 |
(is "?lhs \<longleftrightarrow> ?rhs") |
234 |
proof |
|
58806 | 235 |
assume ?lhs |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59998
diff
changeset
|
236 |
then show ?rhs by (auto simp add: subseteq_mset_def) |
51599 | 237 |
next |
238 |
assume ?rhs |
|
239 |
show ?lhs |
|
63310
caaacf37943f
normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
63195
diff
changeset
|
240 |
proof (rule mset_subset_eqI) |
51599 | 241 |
fix x |
58806 | 242 |
from \<open>?rhs\<close> have "count_of (DAList.impl_of xs) x \<le> count A x" |
51599 | 243 |
by (cases "x \<in> fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty) |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59998
diff
changeset
|
244 |
then show "count (Bag xs) x \<le> count A x" by (simp add: subset_mset_def) |
51599 | 245 |
qed |
246 |
qed |
|
247 |
||
55887 | 248 |
lemma mset_less_eq_Bag [code]: |
64587 | 249 |
"Bag xs \<subseteq># (A :: 'a multiset) \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). n \<le> count A x)" |
55887 | 250 |
proof - |
251 |
{ |
|
252 |
fix x n |
|
253 |
assume "(x,n) \<in> set (DAList.impl_of xs)" |
|
58806 | 254 |
then have "count_of (DAList.impl_of xs) x = n" |
255 |
proof transfer |
|
256 |
fix x n |
|
257 |
fix xs :: "('a \<times> nat) list" |
|
55887 | 258 |
show "(distinct \<circ> map fst) xs \<Longrightarrow> (x, n) \<in> set xs \<Longrightarrow> count_of xs x = n" |
58806 | 259 |
proof (induct xs) |
260 |
case Nil |
|
261 |
then show ?case by simp |
|
262 |
next |
|
263 |
case (Cons ym ys) |
|
55887 | 264 |
obtain y m where ym: "ym = (y,m)" by force |
265 |
note Cons = Cons[unfolded ym] |
|
266 |
show ?case |
|
267 |
proof (cases "x = y") |
|
268 |
case False |
|
58806 | 269 |
with Cons show ?thesis |
270 |
unfolding ym by auto |
|
55887 | 271 |
next |
272 |
case True |
|
273 |
with Cons(2-3) have "m = n" by force |
|
58806 | 274 |
with True show ?thesis |
275 |
unfolding ym by auto |
|
55887 | 276 |
qed |
58806 | 277 |
qed |
55887 | 278 |
qed |
279 |
} |
|
58806 | 280 |
then show ?thesis |
281 |
unfolding mset_less_eq_Bag0 by auto |
|
55887 | 282 |
qed |
283 |
||
73393 | 284 |
declare inter_mset_def [code] |
285 |
declare union_mset_def [code] |
|
60515 | 286 |
declare mset.simps [code] |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
287 |
|
55887 | 288 |
|
58806 | 289 |
fun fold_impl :: "('a \<Rightarrow> nat \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> ('a \<times> nat) list \<Rightarrow> 'b" |
290 |
where |
|
55887 | 291 |
"fold_impl fn e ((a,n) # ms) = (fold_impl fn ((fn a n) e) ms)" |
292 |
| "fold_impl fn e [] = e" |
|
293 |
||
61115
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
294 |
context |
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
295 |
begin |
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
296 |
|
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
297 |
qualified definition fold :: "('a \<Rightarrow> nat \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> ('a, nat) alist \<Rightarrow> 'b" |
58806 | 298 |
where "fold f e al = fold_impl f e (DAList.impl_of al)" |
55887 | 299 |
|
61115
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
300 |
end |
55887 | 301 |
|
302 |
context comp_fun_commute |
|
303 |
begin |
|
304 |
||
58806 | 305 |
lemma DAList_Multiset_fold: |
306 |
assumes fn: "\<And>a n x. fn a n x = (f a ^^ n) x" |
|
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59949
diff
changeset
|
307 |
shows "fold_mset f e (Bag al) = DAList_Multiset.fold fn e al" |
58806 | 308 |
unfolding DAList_Multiset.fold_def |
55887 | 309 |
proof (induct al) |
310 |
fix ys |
|
58806 | 311 |
let ?inv = "{xs :: ('a \<times> nat) list. (distinct \<circ> map fst) xs}" |
55887 | 312 |
note cs[simp del] = count_simps |
58806 | 313 |
have count[simp]: "\<And>x. count (Abs_multiset (count_of x)) = count_of x" |
73270 | 314 |
by (rule Abs_multiset_inverse) (simp add: count_of_multiset) |
55887 | 315 |
assume ys: "ys \<in> ?inv" |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59949
diff
changeset
|
316 |
then show "fold_mset f e (Bag (Alist ys)) = fold_impl fn e (DAList.impl_of (Alist ys))" |
55887 | 317 |
unfolding Bag_def unfolding Alist_inverse[OF ys] |
318 |
proof (induct ys arbitrary: e rule: list.induct) |
|
319 |
case Nil |
|
320 |
show ?case |
|
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59949
diff
changeset
|
321 |
by (rule trans[OF arg_cong[of _ "{#}" "fold_mset f e", OF multiset_eqI]]) |
55887 | 322 |
(auto, simp add: cs) |
323 |
next |
|
324 |
case (Cons pair ys e) |
|
58806 | 325 |
obtain a n where pair: "pair = (a,n)" |
326 |
by force |
|
327 |
from fn[of a n] have [simp]: "fn a n = (f a ^^ n)" |
|
328 |
by auto |
|
329 |
have inv: "ys \<in> ?inv" |
|
330 |
using Cons(2) by auto |
|
55887 | 331 |
note IH = Cons(1)[OF inv] |
63040 | 332 |
define Ys where "Ys = Abs_multiset (count_of ys)" |
67399 | 333 |
have id: "Abs_multiset (count_of ((a, n) # ys)) = (((+) {# a #}) ^^ n) Ys" |
55887 | 334 |
unfolding Ys_def |
335 |
proof (rule multiset_eqI, unfold count) |
|
58806 | 336 |
fix c |
337 |
show "count_of ((a, n) # ys) c = |
|
67399 | 338 |
count (((+) {#a#} ^^ n) (Abs_multiset (count_of ys))) c" (is "?l = ?r") |
55887 | 339 |
proof (cases "c = a") |
58806 | 340 |
case False |
341 |
then show ?thesis |
|
342 |
unfolding cs by (induct n) auto |
|
55887 | 343 |
next |
344 |
case True |
|
58806 | 345 |
then have "?l = n" by (simp add: cs) |
55887 | 346 |
also have "n = ?r" unfolding True |
347 |
proof (induct n) |
|
348 |
case 0 |
|
349 |
from Cons(2)[unfolded pair] have "a \<notin> fst ` set ys" by auto |
|
58806 | 350 |
then show ?case by (induct ys) (simp, auto simp: cs) |
351 |
next |
|
352 |
case Suc |
|
353 |
then show ?case by simp |
|
354 |
qed |
|
55887 | 355 |
finally show ?thesis . |
356 |
qed |
|
357 |
qed |
|
58806 | 358 |
show ?case |
359 |
unfolding pair |
|
360 |
apply (simp add: IH[symmetric]) |
|
361 |
unfolding id Ys_def[symmetric] |
|
362 |
apply (induct n) |
|
363 |
apply (auto simp: fold_mset_fun_left_comm[symmetric]) |
|
364 |
done |
|
55887 | 365 |
qed |
366 |
qed |
|
367 |
||
58806 | 368 |
end |
55887 | 369 |
|
61115
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
370 |
context |
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
371 |
begin |
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
372 |
|
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
373 |
private lift_definition single_alist_entry :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) alist" is "\<lambda>a b. [(a, b)]" |
58806 | 374 |
by auto |
55887 | 375 |
|
58806 | 376 |
lemma image_mset_Bag [code]: |
55887 | 377 |
"image_mset f (Bag ms) = |
58806 | 378 |
DAList_Multiset.fold (\<lambda>a n m. Bag (single_alist_entry (f a) n) + m) {#} ms" |
379 |
unfolding image_mset_def |
|
55887 | 380 |
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, (auto simp: ac_simps)[1]) |
381 |
fix a n m |
|
63793
e68a0b651eb5
add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
63310
diff
changeset
|
382 |
show "Bag (single_alist_entry (f a) n) + m = ((add_mset \<circ> f) a ^^ n) m" (is "?l = ?r") |
55887 | 383 |
proof (rule multiset_eqI) |
384 |
fix x |
|
385 |
have "count ?r x = (if x = f a then n + count m x else count m x)" |
|
58806 | 386 |
by (induct n) auto |
387 |
also have "\<dots> = count ?l x" |
|
388 |
by (simp add: single_alist_entry.rep_eq) |
|
55887 | 389 |
finally show "count ?l x = count ?r x" .. |
390 |
qed |
|
391 |
qed |
|
392 |
||
61115
3a4400985780
modernized name space management -- more uniform qualification;
wenzelm
parents:
60679
diff
changeset
|
393 |
end |
55887 | 394 |
|
69064
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents:
67408
diff
changeset
|
395 |
\<comment> \<open>we cannot use \<open>\<lambda>a n. (+) (a * n)\<close> for folding, since \<open>(*)\<close> is not defined in \<open>comm_monoid_add\<close>\<close> |
67399 | 396 |
lemma sum_mset_Bag[code]: "sum_mset (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (((+) a) ^^ n)) 0 ms" |
63830 | 397 |
unfolding sum_mset.eq_fold |
58806 | 398 |
apply (rule comp_fun_commute.DAList_Multiset_fold) |
399 |
apply unfold_locales |
|
400 |
apply (auto simp: ac_simps) |
|
401 |
done |
|
55887 | 402 |
|
69064
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents:
67408
diff
changeset
|
403 |
\<comment> \<open>we cannot use \<open>\<lambda>a n. (*) (a ^ n)\<close> for folding, since \<open>(^)\<close> is not defined in \<open>comm_monoid_mult\<close>\<close> |
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents:
67408
diff
changeset
|
404 |
lemma prod_mset_Bag[code]: "prod_mset (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (((*) a) ^^ n)) 1 ms" |
63830 | 405 |
unfolding prod_mset.eq_fold |
58806 | 406 |
apply (rule comp_fun_commute.DAList_Multiset_fold) |
407 |
apply unfold_locales |
|
408 |
apply (auto simp: ac_simps) |
|
409 |
done |
|
55887 | 410 |
|
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59949
diff
changeset
|
411 |
lemma size_fold: "size A = fold_mset (\<lambda>_. Suc) 0 A" (is "_ = fold_mset ?f _ _") |
55887 | 412 |
proof - |
60679 | 413 |
interpret comp_fun_commute ?f by standard auto |
55887 | 414 |
show ?thesis by (induct A) auto |
415 |
qed |
|
416 |
||
67399 | 417 |
lemma size_Bag[code]: "size (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (+) n) 0 ms" |
59949 | 418 |
unfolding size_fold |
55887 | 419 |
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, simp) |
420 |
fix a n x |
|
58806 | 421 |
show "n + x = (Suc ^^ n) x" |
422 |
by (induct n) auto |
|
55887 | 423 |
qed |
424 |
||
425 |
||
60495 | 426 |
lemma set_mset_fold: "set_mset A = fold_mset insert {} A" (is "_ = fold_mset ?f _ _") |
55887 | 427 |
proof - |
60679 | 428 |
interpret comp_fun_commute ?f by standard auto |
58806 | 429 |
show ?thesis by (induct A) auto |
55887 | 430 |
qed |
431 |
||
60495 | 432 |
lemma set_mset_Bag[code]: |
433 |
"set_mset (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (if n = 0 then (\<lambda>m. m) else insert a)) {} ms" |
|
434 |
unfolding set_mset_fold |
|
55887 | 435 |
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, (auto simp: ac_simps)[1]) |
436 |
fix a n x |
|
437 |
show "(if n = 0 then \<lambda>m. m else insert a) x = (insert a ^^ n) x" (is "?l n = ?r n") |
|
438 |
proof (cases n) |
|
58806 | 439 |
case 0 |
440 |
then show ?thesis by simp |
|
441 |
next |
|
55887 | 442 |
case (Suc m) |
58806 | 443 |
then have "?l n = insert a x" by simp |
55887 | 444 |
moreover have "?r n = insert a x" unfolding Suc by (induct m) auto |
445 |
ultimately show ?thesis by auto |
|
58806 | 446 |
qed |
55887 | 447 |
qed |
448 |
||
82596 | 449 |
lemma sorted_list_of_multiset_Bag [code]: |
450 |
\<open>sorted_list_of_multiset (Bag ms) = concat (map (\<lambda>(a, n). replicate n a) |
|
451 |
(sort_key fst (DAList.impl_of ms)))\<close> (is \<open>?lhs = ?rhs\<close>) |
|
452 |
proof - |
|
453 |
have *: \<open>sorted (concat (map (\<lambda>(a, n). replicate n a) ans))\<close> |
|
454 |
if \<open>sorted (map fst ans)\<close> |
|
455 |
for ans :: \<open>('a \<times> nat) list\<close> |
|
456 |
using that by (induction ans) (auto simp add: sorted_append) |
|
457 |
have \<open>mset ?rhs = mset ?lhs\<close> |
|
458 |
by (simp add: Bag_eq mset_concat comp_def split_def flip: sum_mset_sum_list) |
|
459 |
moreover have \<open>sorted ?rhs\<close> |
|
460 |
by (rule *) simp |
|
461 |
ultimately have \<open>sort ?lhs = ?rhs\<close> |
|
462 |
by (rule properties_for_sort) |
|
463 |
then show ?thesis |
|
464 |
by simp |
|
465 |
qed |
|
55887 | 466 |
|
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
467 |
instantiation multiset :: (exhaustive) exhaustive |
51599 | 468 |
begin |
469 |
||
58806 | 470 |
definition exhaustive_multiset :: |
471 |
"('a multiset \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option" |
|
472 |
where "exhaustive_multiset f i = Quickcheck_Exhaustive.exhaustive (\<lambda>xs. f (Bag xs)) i" |
|
51599 | 473 |
|
474 |
instance .. |
|
475 |
||
476 |
end |
|
477 |
||
478 |
end |
|
479 |