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