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