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