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