author | obua |
Tue, 11 May 2004 20:11:08 +0200 | |
changeset 14738 | 83f1a514dcb4 |
parent 14722 | 8e739a6eaf11 |
child 14981 | e73f8140af78 |
permissions | -rw-r--r-- |
10249 | 1 |
(* Title: HOL/Library/Multiset.thy |
2 |
ID: $Id$ |
|
12399 | 3 |
Author: Tobias Nipkow, Markus Wenzel, and Lawrence C Paulson |
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
|
10249 | 5 |
*) |
6 |
||
14706 | 7 |
header {* Multisets *} |
10249 | 8 |
|
9 |
theory Multiset = Accessible_Part: |
|
10 |
||
11 |
subsection {* The type of multisets *} |
|
12 |
||
13 |
typedef 'a multiset = "{f::'a => nat. finite {x . 0 < f x}}" |
|
14 |
proof |
|
11464 | 15 |
show "(\<lambda>x. 0::nat) \<in> ?multiset" by simp |
10249 | 16 |
qed |
17 |
||
18 |
lemmas multiset_typedef [simp] = |
|
10277 | 19 |
Abs_multiset_inverse Rep_multiset_inverse Rep_multiset |
20 |
and [simp] = Rep_multiset_inject [symmetric] |
|
10249 | 21 |
|
22 |
constdefs |
|
23 |
Mempty :: "'a multiset" ("{#}") |
|
11464 | 24 |
"{#} == Abs_multiset (\<lambda>a. 0)" |
10249 | 25 |
|
26 |
single :: "'a => 'a multiset" ("{#_#}") |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
27 |
"{#a#} == Abs_multiset (\<lambda>b. if b = a then 1 else 0)" |
10249 | 28 |
|
29 |
count :: "'a multiset => 'a => nat" |
|
30 |
"count == Rep_multiset" |
|
31 |
||
32 |
MCollect :: "'a multiset => ('a => bool) => 'a multiset" |
|
11464 | 33 |
"MCollect M P == Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)" |
10249 | 34 |
|
35 |
syntax |
|
36 |
"_Melem" :: "'a => 'a multiset => bool" ("(_/ :# _)" [50, 51] 50) |
|
37 |
"_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset" ("(1{# _ : _./ _#})") |
|
38 |
translations |
|
39 |
"a :# M" == "0 < count M a" |
|
11464 | 40 |
"{#x:M. P#}" == "MCollect M (\<lambda>x. P)" |
10249 | 41 |
|
42 |
constdefs |
|
43 |
set_of :: "'a multiset => 'a set" |
|
44 |
"set_of M == {x. x :# M}" |
|
45 |
||
14691 | 46 |
instance multiset :: (type) "{plus, minus, zero}" .. |
10249 | 47 |
|
48 |
defs (overloaded) |
|
11464 | 49 |
union_def: "M + N == Abs_multiset (\<lambda>a. Rep_multiset M a + Rep_multiset N a)" |
50 |
diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)" |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
51 |
Zero_multiset_def [simp]: "0 == {#}" |
10249 | 52 |
size_def: "size M == setsum (count M) (set_of M)" |
53 |
||
54 |
||
55 |
text {* |
|
56 |
\medskip Preservation of the representing set @{term multiset}. |
|
57 |
*} |
|
58 |
||
11464 | 59 |
lemma const0_in_multiset [simp]: "(\<lambda>a. 0) \<in> multiset" |
10249 | 60 |
apply (simp add: multiset_def) |
61 |
done |
|
62 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
63 |
lemma only1_in_multiset [simp]: "(\<lambda>b. if b = a then 1 else 0) \<in> multiset" |
10249 | 64 |
apply (simp add: multiset_def) |
65 |
done |
|
66 |
||
67 |
lemma union_preserves_multiset [simp]: |
|
11464 | 68 |
"M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset" |
10249 | 69 |
apply (unfold multiset_def) |
70 |
apply simp |
|
71 |
apply (drule finite_UnI) |
|
72 |
apply assumption |
|
73 |
apply (simp del: finite_Un add: Un_def) |
|
74 |
done |
|
75 |
||
76 |
lemma diff_preserves_multiset [simp]: |
|
11464 | 77 |
"M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset" |
10249 | 78 |
apply (unfold multiset_def) |
79 |
apply simp |
|
80 |
apply (rule finite_subset) |
|
81 |
prefer 2 |
|
82 |
apply assumption |
|
83 |
apply auto |
|
84 |
done |
|
85 |
||
86 |
||
87 |
subsection {* Algebraic properties of multisets *} |
|
88 |
||
89 |
subsubsection {* Union *} |
|
90 |
||
11464 | 91 |
theorem union_empty [simp]: "M + {#} = M \<and> {#} + M = M" |
10249 | 92 |
apply (simp add: union_def Mempty_def) |
93 |
done |
|
94 |
||
95 |
theorem union_commute: "M + N = N + (M::'a multiset)" |
|
96 |
apply (simp add: union_def add_ac) |
|
97 |
done |
|
98 |
||
99 |
theorem union_assoc: "(M + N) + K = M + (N + (K::'a multiset))" |
|
100 |
apply (simp add: union_def add_ac) |
|
101 |
done |
|
102 |
||
103 |
theorem union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))" |
|
104 |
apply (rule union_commute [THEN trans]) |
|
105 |
apply (rule union_assoc [THEN trans]) |
|
106 |
apply (rule union_commute [THEN arg_cong]) |
|
107 |
done |
|
108 |
||
109 |
theorems union_ac = union_assoc union_commute union_lcomm |
|
110 |
||
14738 | 111 |
instance multiset :: (type) comm_monoid_add |
14722
8e739a6eaf11
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset
|
112 |
proof |
8e739a6eaf11
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset
|
113 |
fix a b c :: "'a multiset" |
8e739a6eaf11
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset
|
114 |
show "(a + b) + c = a + (b + c)" by (rule union_assoc) |
8e739a6eaf11
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset
|
115 |
show "a + b = b + a" by (rule union_commute) |
8e739a6eaf11
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset
|
116 |
show "0 + a = a" by simp |
8e739a6eaf11
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
obua
parents:
14706
diff
changeset
|
117 |
qed |
10277 | 118 |
|
10249 | 119 |
|
120 |
subsubsection {* Difference *} |
|
121 |
||
11464 | 122 |
theorem diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}" |
10249 | 123 |
apply (simp add: Mempty_def diff_def) |
124 |
done |
|
125 |
||
126 |
theorem diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M" |
|
127 |
apply (simp add: union_def diff_def) |
|
128 |
done |
|
129 |
||
130 |
||
131 |
subsubsection {* Count of elements *} |
|
132 |
||
133 |
theorem count_empty [simp]: "count {#} a = 0" |
|
134 |
apply (simp add: count_def Mempty_def) |
|
135 |
done |
|
136 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
137 |
theorem count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)" |
10249 | 138 |
apply (simp add: count_def single_def) |
139 |
done |
|
140 |
||
141 |
theorem count_union [simp]: "count (M + N) a = count M a + count N a" |
|
142 |
apply (simp add: count_def union_def) |
|
143 |
done |
|
144 |
||
145 |
theorem count_diff [simp]: "count (M - N) a = count M a - count N a" |
|
146 |
apply (simp add: count_def diff_def) |
|
147 |
done |
|
148 |
||
149 |
||
150 |
subsubsection {* Set of elements *} |
|
151 |
||
152 |
theorem set_of_empty [simp]: "set_of {#} = {}" |
|
153 |
apply (simp add: set_of_def) |
|
154 |
done |
|
155 |
||
156 |
theorem set_of_single [simp]: "set_of {#b#} = {b}" |
|
157 |
apply (simp add: set_of_def) |
|
158 |
done |
|
159 |
||
11464 | 160 |
theorem set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N" |
10249 | 161 |
apply (auto simp add: set_of_def) |
162 |
done |
|
163 |
||
164 |
theorem set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})" |
|
165 |
apply (auto simp add: set_of_def Mempty_def count_def expand_fun_eq) |
|
166 |
done |
|
167 |
||
11464 | 168 |
theorem mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)" |
10249 | 169 |
apply (auto simp add: set_of_def) |
170 |
done |
|
171 |
||
172 |
||
173 |
subsubsection {* Size *} |
|
174 |
||
175 |
theorem size_empty [simp]: "size {#} = 0" |
|
176 |
apply (simp add: size_def) |
|
177 |
done |
|
178 |
||
179 |
theorem size_single [simp]: "size {#b#} = 1" |
|
180 |
apply (simp add: size_def) |
|
181 |
done |
|
182 |
||
183 |
theorem finite_set_of [iff]: "finite (set_of M)" |
|
184 |
apply (cut_tac x = M in Rep_multiset) |
|
185 |
apply (simp add: multiset_def set_of_def count_def) |
|
186 |
done |
|
187 |
||
188 |
theorem setsum_count_Int: |
|
11464 | 189 |
"finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A" |
10249 | 190 |
apply (erule finite_induct) |
191 |
apply simp |
|
192 |
apply (simp add: Int_insert_left set_of_def) |
|
193 |
done |
|
194 |
||
195 |
theorem size_union [simp]: "size (M + N::'a multiset) = size M + size N" |
|
196 |
apply (unfold size_def) |
|
11464 | 197 |
apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)") |
10249 | 198 |
prefer 2 |
199 |
apply (rule ext) |
|
200 |
apply simp |
|
201 |
apply (simp (no_asm_simp) add: setsum_Un setsum_addf setsum_count_Int) |
|
202 |
apply (subst Int_commute) |
|
203 |
apply (simp (no_asm_simp) add: setsum_count_Int) |
|
204 |
done |
|
205 |
||
206 |
theorem size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})" |
|
207 |
apply (unfold size_def Mempty_def count_def) |
|
208 |
apply auto |
|
209 |
apply (simp add: set_of_def count_def expand_fun_eq) |
|
210 |
done |
|
211 |
||
11464 | 212 |
theorem size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M" |
10249 | 213 |
apply (unfold size_def) |
214 |
apply (drule setsum_SucD) |
|
215 |
apply auto |
|
216 |
done |
|
217 |
||
218 |
||
219 |
subsubsection {* Equality of multisets *} |
|
220 |
||
11464 | 221 |
theorem multiset_eq_conv_count_eq: "(M = N) = (\<forall>a. count M a = count N a)" |
10249 | 222 |
apply (simp add: count_def expand_fun_eq) |
223 |
done |
|
224 |
||
11464 | 225 |
theorem single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}" |
10249 | 226 |
apply (simp add: single_def Mempty_def expand_fun_eq) |
227 |
done |
|
228 |
||
229 |
theorem single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)" |
|
230 |
apply (auto simp add: single_def expand_fun_eq) |
|
231 |
done |
|
232 |
||
11464 | 233 |
theorem union_eq_empty [iff]: "(M + N = {#}) = (M = {#} \<and> N = {#})" |
10249 | 234 |
apply (auto simp add: union_def Mempty_def expand_fun_eq) |
235 |
done |
|
236 |
||
11464 | 237 |
theorem empty_eq_union [iff]: "({#} = M + N) = (M = {#} \<and> N = {#})" |
10249 | 238 |
apply (auto simp add: union_def Mempty_def expand_fun_eq) |
239 |
done |
|
240 |
||
241 |
theorem union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))" |
|
242 |
apply (simp add: union_def expand_fun_eq) |
|
243 |
done |
|
244 |
||
245 |
theorem union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))" |
|
246 |
apply (simp add: union_def expand_fun_eq) |
|
247 |
done |
|
248 |
||
249 |
theorem union_is_single: |
|
11464 | 250 |
"(M + N = {#a#}) = (M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#})" |
10249 | 251 |
apply (unfold Mempty_def single_def union_def) |
252 |
apply (simp add: add_is_1 expand_fun_eq) |
|
253 |
apply blast |
|
254 |
done |
|
255 |
||
256 |
theorem single_is_union: |
|
257 |
"({#a#} = M + N) = |
|
11464 | 258 |
({#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N)" |
10249 | 259 |
apply (unfold Mempty_def single_def union_def) |
11464 | 260 |
apply (simp add: add_is_1 one_is_add expand_fun_eq) |
10249 | 261 |
apply (blast dest: sym) |
262 |
done |
|
263 |
||
264 |
theorem add_eq_conv_diff: |
|
265 |
"(M + {#a#} = N + {#b#}) = |
|
11464 | 266 |
(M = N \<and> a = b \<or> |
267 |
M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})" |
|
10249 | 268 |
apply (unfold single_def union_def diff_def) |
269 |
apply (simp (no_asm) add: expand_fun_eq) |
|
270 |
apply (rule conjI) |
|
271 |
apply force |
|
11868
56db9f3a6b3e
Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents:
11701
diff
changeset
|
272 |
apply safe |
13601 | 273 |
apply simp_all |
274 |
apply (simp add: eq_sym_conv) |
|
10249 | 275 |
done |
276 |
||
277 |
(* |
|
278 |
val prems = Goal |
|
279 |
"[| !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> finite F --> P F"; |
|
11464 | 280 |
by (res_inst_tac [("a","F"),("f","\<lambda>A. if finite A then card A else 0")] |
10249 | 281 |
measure_induct 1); |
282 |
by (Clarify_tac 1); |
|
283 |
by (resolve_tac prems 1); |
|
284 |
by (assume_tac 1); |
|
285 |
by (Clarify_tac 1); |
|
286 |
by (subgoal_tac "finite G" 1); |
|
287 |
by (fast_tac (claset() addDs [finite_subset,order_less_le RS iffD1]) 2); |
|
288 |
by (etac allE 1); |
|
289 |
by (etac impE 1); |
|
290 |
by (Blast_tac 2); |
|
291 |
by (asm_simp_tac (simpset() addsimps [psubset_card]) 1); |
|
292 |
no_qed(); |
|
293 |
val lemma = result(); |
|
294 |
||
295 |
val prems = Goal |
|
296 |
"[| finite F; !!F. [| finite F; !G. G < F --> P G |] ==> P F |] ==> P F"; |
|
297 |
by (rtac (lemma RS mp) 1); |
|
298 |
by (REPEAT(ares_tac prems 1)); |
|
299 |
qed "finite_psubset_induct"; |
|
300 |
||
301 |
Better: use wf_finite_psubset in WF_Rel |
|
302 |
*) |
|
303 |
||
304 |
||
305 |
subsection {* Induction over multisets *} |
|
306 |
||
307 |
lemma setsum_decr: |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
308 |
"finite F ==> (0::nat) < f a ==> |
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
309 |
setsum (f (a := f a - 1)) F = (if a \<in> F then setsum f F - 1 else setsum f F)" |
10249 | 310 |
apply (erule finite_induct) |
311 |
apply auto |
|
312 |
apply (drule_tac a = a in mk_disjoint_insert) |
|
313 |
apply auto |
|
314 |
done |
|
315 |
||
10313 | 316 |
lemma rep_multiset_induct_aux: |
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
317 |
"P (\<lambda>a. (0::nat)) ==> (!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) |
11464 | 318 |
==> \<forall>f. f \<in> multiset --> setsum f {x. 0 < f x} = n --> P f" |
10249 | 319 |
proof - |
11549 | 320 |
case rule_context |
321 |
note premises = this [unfolded multiset_def] |
|
10249 | 322 |
show ?thesis |
323 |
apply (unfold multiset_def) |
|
324 |
apply (induct_tac n) |
|
325 |
apply simp |
|
326 |
apply clarify |
|
11464 | 327 |
apply (subgoal_tac "f = (\<lambda>a.0)") |
10249 | 328 |
apply simp |
11549 | 329 |
apply (rule premises) |
10249 | 330 |
apply (rule ext) |
331 |
apply force |
|
332 |
apply clarify |
|
333 |
apply (frule setsum_SucD) |
|
334 |
apply clarify |
|
335 |
apply (rename_tac a) |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
336 |
apply (subgoal_tac "finite {x. 0 < (f (a := f a - 1)) x}") |
10249 | 337 |
prefer 2 |
338 |
apply (rule finite_subset) |
|
339 |
prefer 2 |
|
340 |
apply assumption |
|
341 |
apply simp |
|
342 |
apply blast |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
343 |
apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)") |
10249 | 344 |
prefer 2 |
345 |
apply (rule ext) |
|
346 |
apply (simp (no_asm_simp)) |
|
11549 | 347 |
apply (erule ssubst, rule premises) |
10249 | 348 |
apply blast |
349 |
apply (erule allE, erule impE, erule_tac [2] mp) |
|
350 |
apply blast |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
351 |
apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def) |
11464 | 352 |
apply (subgoal_tac "{x. x \<noteq> a --> 0 < f x} = {x. 0 < f x}") |
10249 | 353 |
prefer 2 |
354 |
apply blast |
|
11464 | 355 |
apply (subgoal_tac "{x. x \<noteq> a \<and> 0 < f x} = {x. 0 < f x} - {a}") |
10249 | 356 |
prefer 2 |
357 |
apply blast |
|
358 |
apply (simp add: le_imp_diff_is_add setsum_diff1 cong: conj_cong) |
|
359 |
done |
|
360 |
qed |
|
361 |
||
10313 | 362 |
theorem rep_multiset_induct: |
11464 | 363 |
"f \<in> multiset ==> P (\<lambda>a. 0) ==> |
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
364 |
(!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f" |
10313 | 365 |
apply (insert rep_multiset_induct_aux) |
10249 | 366 |
apply blast |
367 |
done |
|
368 |
||
369 |
theorem multiset_induct [induct type: multiset]: |
|
370 |
"P {#} ==> (!!M x. P M ==> P (M + {#x#})) ==> P M" |
|
371 |
proof - |
|
372 |
note defns = union_def single_def Mempty_def |
|
373 |
assume prem1 [unfolded defns]: "P {#}" |
|
374 |
assume prem2 [unfolded defns]: "!!M x. P M ==> P (M + {#x#})" |
|
375 |
show ?thesis |
|
376 |
apply (rule Rep_multiset_inverse [THEN subst]) |
|
10313 | 377 |
apply (rule Rep_multiset [THEN rep_multiset_induct]) |
10249 | 378 |
apply (rule prem1) |
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
379 |
apply (subgoal_tac "f (b := f b + 1) = (\<lambda>a. f a + (if a = b then 1 else 0))") |
10249 | 380 |
prefer 2 |
381 |
apply (simp add: expand_fun_eq) |
|
382 |
apply (erule ssubst) |
|
383 |
apply (erule Abs_multiset_inverse [THEN subst]) |
|
384 |
apply (erule prem2 [simplified]) |
|
385 |
done |
|
386 |
qed |
|
387 |
||
388 |
||
389 |
lemma MCollect_preserves_multiset: |
|
11464 | 390 |
"M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset" |
10249 | 391 |
apply (simp add: multiset_def) |
392 |
apply (rule finite_subset) |
|
393 |
apply auto |
|
394 |
done |
|
395 |
||
396 |
theorem count_MCollect [simp]: |
|
397 |
"count {# x:M. P x #} a = (if P a then count M a else 0)" |
|
398 |
apply (unfold count_def MCollect_def) |
|
399 |
apply (simp add: MCollect_preserves_multiset) |
|
400 |
done |
|
401 |
||
11464 | 402 |
theorem set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}" |
10249 | 403 |
apply (auto simp add: set_of_def) |
404 |
done |
|
405 |
||
11464 | 406 |
theorem multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}" |
10249 | 407 |
apply (subst multiset_eq_conv_count_eq) |
408 |
apply auto |
|
409 |
done |
|
410 |
||
10277 | 411 |
declare Rep_multiset_inject [symmetric, simp del] |
10249 | 412 |
declare multiset_typedef [simp del] |
413 |
||
414 |
theorem add_eq_conv_ex: |
|
415 |
"(M + {#a#} = N + {#b#}) = |
|
11464 | 416 |
(M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))" |
10249 | 417 |
apply (auto simp add: add_eq_conv_diff) |
418 |
done |
|
419 |
||
420 |
||
421 |
subsection {* Multiset orderings *} |
|
422 |
||
423 |
subsubsection {* Well-foundedness *} |
|
424 |
||
425 |
constdefs |
|
11464 | 426 |
mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" |
10249 | 427 |
"mult1 r == |
11464 | 428 |
{(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> |
429 |
(\<forall>b. b :# K --> (b, a) \<in> r)}" |
|
10249 | 430 |
|
11464 | 431 |
mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" |
10392 | 432 |
"mult r == (mult1 r)\<^sup>+" |
10249 | 433 |
|
11464 | 434 |
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r" |
10277 | 435 |
by (simp add: mult1_def) |
10249 | 436 |
|
11464 | 437 |
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==> |
438 |
(\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or> |
|
439 |
(\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)" |
|
440 |
(concl is "?case1 (mult1 r) \<or> ?case2") |
|
10249 | 441 |
proof (unfold mult1_def) |
11464 | 442 |
let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r" |
443 |
let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a" |
|
10249 | 444 |
let ?case1 = "?case1 {(N, M). ?R N M}" |
445 |
||
11464 | 446 |
assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}" |
447 |
hence "\<exists>a' M0' K. |
|
448 |
M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp |
|
449 |
thus "?case1 \<or> ?case2" |
|
10249 | 450 |
proof (elim exE conjE) |
451 |
fix a' M0' K |
|
452 |
assume N: "N = M0' + K" and r: "?r K a'" |
|
453 |
assume "M0 + {#a#} = M0' + {#a'#}" |
|
11464 | 454 |
hence "M0 = M0' \<and> a = a' \<or> |
455 |
(\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})" |
|
10249 | 456 |
by (simp only: add_eq_conv_ex) |
457 |
thus ?thesis |
|
458 |
proof (elim disjE conjE exE) |
|
459 |
assume "M0 = M0'" "a = a'" |
|
11464 | 460 |
with N r have "?r K a \<and> N = M0 + K" by simp |
10249 | 461 |
hence ?case2 .. thus ?thesis .. |
462 |
next |
|
463 |
fix K' |
|
464 |
assume "M0' = K' + {#a#}" |
|
465 |
with N have n: "N = K' + K + {#a#}" by (simp add: union_ac) |
|
466 |
||
467 |
assume "M0 = K' + {#a'#}" |
|
468 |
with r have "?R (K' + K) M0" by blast |
|
469 |
with n have ?case1 by simp thus ?thesis .. |
|
470 |
qed |
|
471 |
qed |
|
472 |
qed |
|
473 |
||
11464 | 474 |
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)" |
10249 | 475 |
proof |
476 |
let ?R = "mult1 r" |
|
477 |
let ?W = "acc ?R" |
|
478 |
{ |
|
479 |
fix M M0 a |
|
11464 | 480 |
assume M0: "M0 \<in> ?W" |
12399 | 481 |
and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)" |
11464 | 482 |
and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W" |
483 |
have "M0 + {#a#} \<in> ?W" |
|
10249 | 484 |
proof (rule accI [of "M0 + {#a#}"]) |
485 |
fix N |
|
11464 | 486 |
assume "(N, M0 + {#a#}) \<in> ?R" |
487 |
hence "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or> |
|
488 |
(\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))" |
|
10249 | 489 |
by (rule less_add) |
11464 | 490 |
thus "N \<in> ?W" |
10249 | 491 |
proof (elim exE disjE conjE) |
11464 | 492 |
fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}" |
493 |
from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" .. |
|
494 |
hence "M + {#a#} \<in> ?W" .. |
|
495 |
thus "N \<in> ?W" by (simp only: N) |
|
10249 | 496 |
next |
497 |
fix K |
|
498 |
assume N: "N = M0 + K" |
|
11464 | 499 |
assume "\<forall>b. b :# K --> (b, a) \<in> r" |
500 |
have "?this --> M0 + K \<in> ?W" (is "?P K") |
|
10249 | 501 |
proof (induct K) |
11464 | 502 |
from M0 have "M0 + {#} \<in> ?W" by simp |
10249 | 503 |
thus "?P {#}" .. |
504 |
||
505 |
fix K x assume hyp: "?P K" |
|
506 |
show "?P (K + {#x#})" |
|
507 |
proof |
|
11464 | 508 |
assume a: "\<forall>b. b :# (K + {#x#}) --> (b, a) \<in> r" |
509 |
hence "(x, a) \<in> r" by simp |
|
510 |
with wf_hyp have b: "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast |
|
10249 | 511 |
|
11464 | 512 |
from a hyp have "M0 + K \<in> ?W" by simp |
513 |
with b have "(M0 + K) + {#x#} \<in> ?W" .. |
|
514 |
thus "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc) |
|
10249 | 515 |
qed |
516 |
qed |
|
11464 | 517 |
hence "M0 + K \<in> ?W" .. |
518 |
thus "N \<in> ?W" by (simp only: N) |
|
10249 | 519 |
qed |
520 |
qed |
|
521 |
} note tedious_reasoning = this |
|
522 |
||
523 |
assume wf: "wf r" |
|
524 |
fix M |
|
11464 | 525 |
show "M \<in> ?W" |
10249 | 526 |
proof (induct M) |
11464 | 527 |
show "{#} \<in> ?W" |
10249 | 528 |
proof (rule accI) |
11464 | 529 |
fix b assume "(b, {#}) \<in> ?R" |
530 |
with not_less_empty show "b \<in> ?W" by contradiction |
|
10249 | 531 |
qed |
532 |
||
11464 | 533 |
fix M a assume "M \<in> ?W" |
534 |
from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W" |
|
10249 | 535 |
proof induct |
536 |
fix a |
|
12399 | 537 |
assume "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)" |
11464 | 538 |
show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W" |
10249 | 539 |
proof |
11464 | 540 |
fix M assume "M \<in> ?W" |
541 |
thus "M + {#a#} \<in> ?W" |
|
10249 | 542 |
by (rule acc_induct) (rule tedious_reasoning) |
543 |
qed |
|
544 |
qed |
|
11464 | 545 |
thus "M + {#a#} \<in> ?W" .. |
10249 | 546 |
qed |
547 |
qed |
|
548 |
||
549 |
theorem wf_mult1: "wf r ==> wf (mult1 r)" |
|
550 |
by (rule acc_wfI, rule all_accessible) |
|
551 |
||
552 |
theorem wf_mult: "wf r ==> wf (mult r)" |
|
553 |
by (unfold mult_def, rule wf_trancl, rule wf_mult1) |
|
554 |
||
555 |
||
556 |
subsubsection {* Closure-free presentation *} |
|
557 |
||
558 |
(*Badly needed: a linear arithmetic procedure for multisets*) |
|
559 |
||
560 |
lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})" |
|
561 |
apply (simp add: multiset_eq_conv_count_eq) |
|
562 |
done |
|
563 |
||
564 |
text {* One direction. *} |
|
565 |
||
566 |
lemma mult_implies_one_step: |
|
11464 | 567 |
"trans r ==> (M, N) \<in> mult r ==> |
568 |
\<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and> |
|
569 |
(\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)" |
|
10249 | 570 |
apply (unfold mult_def mult1_def set_of_def) |
571 |
apply (erule converse_trancl_induct) |
|
572 |
apply clarify |
|
573 |
apply (rule_tac x = M0 in exI) |
|
574 |
apply simp |
|
575 |
apply clarify |
|
576 |
apply (case_tac "a :# K") |
|
577 |
apply (rule_tac x = I in exI) |
|
578 |
apply (simp (no_asm)) |
|
579 |
apply (rule_tac x = "(K - {#a#}) + Ka" in exI) |
|
580 |
apply (simp (no_asm_simp) add: union_assoc [symmetric]) |
|
11464 | 581 |
apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong) |
10249 | 582 |
apply (simp add: diff_union_single_conv) |
583 |
apply (simp (no_asm_use) add: trans_def) |
|
584 |
apply blast |
|
585 |
apply (subgoal_tac "a :# I") |
|
586 |
apply (rule_tac x = "I - {#a#}" in exI) |
|
587 |
apply (rule_tac x = "J + {#a#}" in exI) |
|
588 |
apply (rule_tac x = "K + Ka" in exI) |
|
589 |
apply (rule conjI) |
|
590 |
apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split) |
|
591 |
apply (rule conjI) |
|
11464 | 592 |
apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong) |
10249 | 593 |
apply simp |
594 |
apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split) |
|
595 |
apply (simp (no_asm_use) add: trans_def) |
|
596 |
apply blast |
|
10277 | 597 |
apply (subgoal_tac "a :# (M0 + {#a#})") |
10249 | 598 |
apply simp |
599 |
apply (simp (no_asm)) |
|
600 |
done |
|
601 |
||
602 |
lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}" |
|
603 |
apply (simp add: multiset_eq_conv_count_eq) |
|
604 |
done |
|
605 |
||
11464 | 606 |
lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}" |
10249 | 607 |
apply (erule size_eq_Suc_imp_elem [THEN exE]) |
608 |
apply (drule elem_imp_eq_diff_union) |
|
609 |
apply auto |
|
610 |
done |
|
611 |
||
612 |
lemma one_step_implies_mult_aux: |
|
613 |
"trans r ==> |
|
11464 | 614 |
\<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)) |
615 |
--> (I + K, I + J) \<in> mult r" |
|
10249 | 616 |
apply (induct_tac n) |
617 |
apply auto |
|
618 |
apply (frule size_eq_Suc_imp_eq_union) |
|
619 |
apply clarify |
|
620 |
apply (rename_tac "J'") |
|
621 |
apply simp |
|
622 |
apply (erule notE) |
|
623 |
apply auto |
|
624 |
apply (case_tac "J' = {#}") |
|
625 |
apply (simp add: mult_def) |
|
626 |
apply (rule r_into_trancl) |
|
627 |
apply (simp add: mult1_def set_of_def) |
|
628 |
apply blast |
|
11464 | 629 |
txt {* Now we know @{term "J' \<noteq> {#}"}. *} |
630 |
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition) |
|
631 |
apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp) |
|
10249 | 632 |
apply (erule ssubst) |
633 |
apply (simp add: Ball_def) |
|
634 |
apply auto |
|
635 |
apply (subgoal_tac |
|
11464 | 636 |
"((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #}, |
637 |
(I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r") |
|
10249 | 638 |
prefer 2 |
639 |
apply force |
|
640 |
apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def) |
|
641 |
apply (erule trancl_trans) |
|
642 |
apply (rule r_into_trancl) |
|
643 |
apply (simp add: mult1_def set_of_def) |
|
644 |
apply (rule_tac x = a in exI) |
|
645 |
apply (rule_tac x = "I + J'" in exI) |
|
646 |
apply (simp add: union_ac) |
|
647 |
done |
|
648 |
||
649 |
theorem one_step_implies_mult: |
|
11464 | 650 |
"trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r |
651 |
==> (I + K, I + J) \<in> mult r" |
|
10249 | 652 |
apply (insert one_step_implies_mult_aux) |
653 |
apply blast |
|
654 |
done |
|
655 |
||
656 |
||
657 |
subsubsection {* Partial-order properties *} |
|
658 |
||
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
11868
diff
changeset
|
659 |
instance multiset :: (type) ord .. |
10249 | 660 |
|
661 |
defs (overloaded) |
|
11464 | 662 |
less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}" |
663 |
le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)" |
|
10249 | 664 |
|
665 |
lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}" |
|
666 |
apply (unfold trans_def) |
|
667 |
apply (blast intro: order_less_trans) |
|
668 |
done |
|
669 |
||
670 |
text {* |
|
671 |
\medskip Irreflexivity. |
|
672 |
*} |
|
673 |
||
674 |
lemma mult_irrefl_aux: |
|
11464 | 675 |
"finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) --> A = {}" |
10249 | 676 |
apply (erule finite_induct) |
677 |
apply (auto intro: order_less_trans) |
|
678 |
done |
|
679 |
||
11464 | 680 |
theorem mult_less_not_refl: "\<not> M < (M::'a::order multiset)" |
10249 | 681 |
apply (unfold less_multiset_def) |
682 |
apply auto |
|
683 |
apply (drule trans_base_order [THEN mult_implies_one_step]) |
|
684 |
apply auto |
|
685 |
apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]]) |
|
686 |
apply (simp add: set_of_eq_empty_iff) |
|
687 |
done |
|
688 |
||
689 |
lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R" |
|
690 |
apply (insert mult_less_not_refl) |
|
13596 | 691 |
apply fast |
10249 | 692 |
done |
693 |
||
694 |
||
695 |
text {* Transitivity. *} |
|
696 |
||
697 |
theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)" |
|
698 |
apply (unfold less_multiset_def mult_def) |
|
699 |
apply (blast intro: trancl_trans) |
|
700 |
done |
|
701 |
||
702 |
text {* Asymmetry. *} |
|
703 |
||
11464 | 704 |
theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)" |
10249 | 705 |
apply auto |
706 |
apply (rule mult_less_not_refl [THEN notE]) |
|
707 |
apply (erule mult_less_trans) |
|
708 |
apply assumption |
|
709 |
done |
|
710 |
||
711 |
theorem mult_less_asym: |
|
11464 | 712 |
"M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P" |
10249 | 713 |
apply (insert mult_less_not_sym) |
714 |
apply blast |
|
715 |
done |
|
716 |
||
717 |
theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)" |
|
718 |
apply (unfold le_multiset_def) |
|
719 |
apply auto |
|
720 |
done |
|
721 |
||
722 |
text {* Anti-symmetry. *} |
|
723 |
||
724 |
theorem mult_le_antisym: |
|
725 |
"M <= N ==> N <= M ==> M = (N::'a::order multiset)" |
|
726 |
apply (unfold le_multiset_def) |
|
727 |
apply (blast dest: mult_less_not_sym) |
|
728 |
done |
|
729 |
||
730 |
text {* Transitivity. *} |
|
731 |
||
732 |
theorem mult_le_trans: |
|
733 |
"K <= M ==> M <= N ==> K <= (N::'a::order multiset)" |
|
734 |
apply (unfold le_multiset_def) |
|
735 |
apply (blast intro: mult_less_trans) |
|
736 |
done |
|
737 |
||
11655 | 738 |
theorem mult_less_le: "(M < N) = (M <= N \<and> M \<noteq> (N::'a::order multiset))" |
10249 | 739 |
apply (unfold le_multiset_def) |
740 |
apply auto |
|
741 |
done |
|
742 |
||
10277 | 743 |
text {* Partial order. *} |
744 |
||
745 |
instance multiset :: (order) order |
|
746 |
apply intro_classes |
|
747 |
apply (rule mult_le_refl) |
|
748 |
apply (erule mult_le_trans) |
|
749 |
apply assumption |
|
750 |
apply (erule mult_le_antisym) |
|
751 |
apply assumption |
|
752 |
apply (rule mult_less_le) |
|
753 |
done |
|
754 |
||
10249 | 755 |
|
756 |
subsubsection {* Monotonicity of multiset union *} |
|
757 |
||
758 |
theorem mult1_union: |
|
11464 | 759 |
"(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r" |
10249 | 760 |
apply (unfold mult1_def) |
761 |
apply auto |
|
762 |
apply (rule_tac x = a in exI) |
|
763 |
apply (rule_tac x = "C + M0" in exI) |
|
764 |
apply (simp add: union_assoc) |
|
765 |
done |
|
766 |
||
767 |
lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)" |
|
768 |
apply (unfold less_multiset_def mult_def) |
|
769 |
apply (erule trancl_induct) |
|
770 |
apply (blast intro: mult1_union transI order_less_trans r_into_trancl) |
|
771 |
apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans) |
|
772 |
done |
|
773 |
||
774 |
lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)" |
|
775 |
apply (subst union_commute [of B C]) |
|
776 |
apply (subst union_commute [of D C]) |
|
777 |
apply (erule union_less_mono2) |
|
778 |
done |
|
779 |
||
780 |
theorem union_less_mono: |
|
781 |
"A < C ==> B < D ==> A + B < C + (D::'a::order multiset)" |
|
782 |
apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans) |
|
783 |
done |
|
784 |
||
785 |
theorem union_le_mono: |
|
786 |
"A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)" |
|
787 |
apply (unfold le_multiset_def) |
|
788 |
apply (blast intro: union_less_mono union_less_mono1 union_less_mono2) |
|
789 |
done |
|
790 |
||
791 |
theorem empty_leI [iff]: "{#} <= (M::'a::order multiset)" |
|
792 |
apply (unfold le_multiset_def less_multiset_def) |
|
793 |
apply (case_tac "M = {#}") |
|
794 |
prefer 2 |
|
11464 | 795 |
apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))") |
10249 | 796 |
prefer 2 |
797 |
apply (rule one_step_implies_mult) |
|
798 |
apply (simp only: trans_def) |
|
799 |
apply auto |
|
800 |
done |
|
801 |
||
802 |
theorem union_upper1: "A <= A + (B::'a::order multiset)" |
|
803 |
apply (subgoal_tac "A + {#} <= A + B") |
|
804 |
prefer 2 |
|
805 |
apply (rule union_le_mono) |
|
806 |
apply auto |
|
807 |
done |
|
808 |
||
809 |
theorem union_upper2: "B <= A + (B::'a::order multiset)" |
|
810 |
apply (subst union_commute, rule union_upper1) |
|
811 |
done |
|
812 |
||
813 |
end |