author | wenzelm |
Sat, 21 Jan 2006 23:02:21 +0100 | |
changeset 18730 | 843da46f89ac |
parent 18258 | 836491e9b7d5 |
child 19086 | 1b3780be6cc2 |
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 |
||
17200 | 55 |
constdefs |
15869 | 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 |
17200 | 112 |
proof |
14722
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" |
18258 | 176 |
apply (induct rule: finite_induct) |
17161 | 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 |
||
17778 | 238 |
ML"reset use_neq_simproc" |
17161 | 239 |
lemma add_eq_conv_diff: |
10249 | 240 |
"(M + {#a#} = N + {#b#}) = |
15072 | 241 |
(M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#})" |
10249 | 242 |
apply (unfold single_def union_def diff_def) |
243 |
apply (simp (no_asm) add: expand_fun_eq) |
|
15072 | 244 |
apply (rule conjI, force, safe, simp_all) |
13601 | 245 |
apply (simp add: eq_sym_conv) |
10249 | 246 |
done |
17778 | 247 |
ML"set use_neq_simproc" |
10249 | 248 |
|
15869 | 249 |
declare Rep_multiset_inject [symmetric, simp del] |
250 |
||
251 |
||
252 |
subsubsection {* Intersection *} |
|
253 |
||
254 |
lemma multiset_inter_count: |
|
17161 | 255 |
"count (A #\<inter> B) x = min (count A x) (count B x)" |
256 |
by (simp add: multiset_inter_def min_def) |
|
15869 | 257 |
|
258 |
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A" |
|
17200 | 259 |
by (simp add: multiset_eq_conv_count_eq multiset_inter_count |
17161 | 260 |
min_max.below_inf.inf_commute) |
15869 | 261 |
|
262 |
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C" |
|
17200 | 263 |
by (simp add: multiset_eq_conv_count_eq multiset_inter_count |
17161 | 264 |
min_max.below_inf.inf_assoc) |
15869 | 265 |
|
266 |
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)" |
|
267 |
by (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def) |
|
268 |
||
17161 | 269 |
lemmas multiset_inter_ac = |
270 |
multiset_inter_commute |
|
271 |
multiset_inter_assoc |
|
272 |
multiset_inter_left_commute |
|
15869 | 273 |
|
274 |
lemma multiset_union_diff_commute: "B #\<inter> C = {#} \<Longrightarrow> A + B - C = A - C + B" |
|
17200 | 275 |
apply (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def |
17161 | 276 |
split: split_if_asm) |
15869 | 277 |
apply clarsimp |
17161 | 278 |
apply (erule_tac x = a in allE) |
15869 | 279 |
apply auto |
280 |
done |
|
281 |
||
10249 | 282 |
|
283 |
subsection {* Induction over multisets *} |
|
284 |
||
285 |
lemma setsum_decr: |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11655
diff
changeset
|
286 |
"finite F ==> (0::nat) < f a ==> |
15072 | 287 |
setsum (f (a := f a - 1)) F = (if a\<in>F then setsum f F - 1 else setsum f F)" |
18258 | 288 |
apply (induct rule: finite_induct) |
289 |
apply auto |
|
15072 | 290 |
apply (drule_tac a = a in mk_disjoint_insert, auto) |
10249 | 291 |
done |
292 |
||
10313 | 293 |
lemma rep_multiset_induct_aux: |
18730 | 294 |
assumes 1: "P (\<lambda>a. (0::nat))" |
295 |
and 2: "!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))" |
|
17161 | 296 |
shows "\<forall>f. f \<in> multiset --> setsum f {x. 0 < f x} = n --> P f" |
18730 | 297 |
apply (unfold multiset_def) |
298 |
apply (induct_tac n, simp, clarify) |
|
299 |
apply (subgoal_tac "f = (\<lambda>a.0)") |
|
300 |
apply simp |
|
301 |
apply (rule 1) |
|
302 |
apply (rule ext, force, clarify) |
|
303 |
apply (frule setsum_SucD, clarify) |
|
304 |
apply (rename_tac a) |
|
305 |
apply (subgoal_tac "finite {x. 0 < (f (a := f a - 1)) x}") |
|
306 |
prefer 2 |
|
307 |
apply (rule finite_subset) |
|
308 |
prefer 2 |
|
309 |
apply assumption |
|
310 |
apply simp |
|
311 |
apply blast |
|
312 |
apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)") |
|
313 |
prefer 2 |
|
314 |
apply (rule ext) |
|
315 |
apply (simp (no_asm_simp)) |
|
316 |
apply (erule ssubst, rule 2 [unfolded multiset_def], blast) |
|
317 |
apply (erule allE, erule impE, erule_tac [2] mp, blast) |
|
318 |
apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def) |
|
319 |
apply (subgoal_tac "{x. x \<noteq> a --> 0 < f x} = {x. 0 < f x}") |
|
320 |
prefer 2 |
|
321 |
apply blast |
|
322 |
apply (subgoal_tac "{x. x \<noteq> a \<and> 0 < f x} = {x. 0 < f x} - {a}") |
|
323 |
prefer 2 |
|
324 |
apply blast |
|
325 |
apply (simp add: le_imp_diff_is_add setsum_diff1_nat cong: conj_cong) |
|
326 |
done |
|
10249 | 327 |
|
10313 | 328 |
theorem rep_multiset_induct: |
11464 | 329 |
"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
|
330 |
(!!f b. f \<in> multiset ==> P f ==> P (f (b := f b + 1))) ==> P f" |
17161 | 331 |
using rep_multiset_induct_aux by blast |
10249 | 332 |
|
18258 | 333 |
theorem multiset_induct [case_names empty add, induct type: multiset]: |
334 |
assumes empty: "P {#}" |
|
335 |
and add: "!!M x. P M ==> P (M + {#x#})" |
|
17161 | 336 |
shows "P M" |
10249 | 337 |
proof - |
338 |
note defns = union_def single_def Mempty_def |
|
339 |
show ?thesis |
|
340 |
apply (rule Rep_multiset_inverse [THEN subst]) |
|
10313 | 341 |
apply (rule Rep_multiset [THEN rep_multiset_induct]) |
18258 | 342 |
apply (rule empty [unfolded defns]) |
15072 | 343 |
apply (subgoal_tac "f(b := f b + 1) = (\<lambda>a. f a + (if a=b then 1 else 0))") |
10249 | 344 |
prefer 2 |
345 |
apply (simp add: expand_fun_eq) |
|
346 |
apply (erule ssubst) |
|
17200 | 347 |
apply (erule Abs_multiset_inverse [THEN subst]) |
18258 | 348 |
apply (erule add [unfolded defns, simplified]) |
10249 | 349 |
done |
350 |
qed |
|
351 |
||
352 |
lemma MCollect_preserves_multiset: |
|
11464 | 353 |
"M \<in> multiset ==> (\<lambda>x. if P x then M x else 0) \<in> multiset" |
10249 | 354 |
apply (simp add: multiset_def) |
15072 | 355 |
apply (rule finite_subset, auto) |
10249 | 356 |
done |
357 |
||
17161 | 358 |
lemma count_MCollect [simp]: |
10249 | 359 |
"count {# x:M. P x #} a = (if P a then count M a else 0)" |
15072 | 360 |
by (simp add: count_def MCollect_def MCollect_preserves_multiset) |
10249 | 361 |
|
17161 | 362 |
lemma set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M \<inter> {x. P x}" |
363 |
by (auto simp add: set_of_def) |
|
10249 | 364 |
|
17161 | 365 |
lemma multiset_partition: "M = {# x:M. P x #} + {# x:M. \<not> P x #}" |
366 |
by (subst multiset_eq_conv_count_eq, auto) |
|
10249 | 367 |
|
17161 | 368 |
lemma add_eq_conv_ex: |
369 |
"(M + {#a#} = N + {#b#}) = |
|
370 |
(M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))" |
|
15072 | 371 |
by (auto simp add: add_eq_conv_diff) |
10249 | 372 |
|
15869 | 373 |
declare multiset_typedef [simp del] |
10249 | 374 |
|
17161 | 375 |
|
10249 | 376 |
subsection {* Multiset orderings *} |
377 |
||
378 |
subsubsection {* Well-foundedness *} |
|
379 |
||
380 |
constdefs |
|
11464 | 381 |
mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" |
10249 | 382 |
"mult1 r == |
11464 | 383 |
{(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> |
384 |
(\<forall>b. b :# K --> (b, a) \<in> r)}" |
|
10249 | 385 |
|
11464 | 386 |
mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" |
10392 | 387 |
"mult r == (mult1 r)\<^sup>+" |
10249 | 388 |
|
11464 | 389 |
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r" |
10277 | 390 |
by (simp add: mult1_def) |
10249 | 391 |
|
11464 | 392 |
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==> |
393 |
(\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or> |
|
394 |
(\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)" |
|
395 |
(concl is "?case1 (mult1 r) \<or> ?case2") |
|
10249 | 396 |
proof (unfold mult1_def) |
11464 | 397 |
let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r" |
398 |
let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a" |
|
10249 | 399 |
let ?case1 = "?case1 {(N, M). ?R N M}" |
400 |
||
11464 | 401 |
assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}" |
18258 | 402 |
then have "\<exists>a' M0' K. |
11464 | 403 |
M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp |
18258 | 404 |
then show "?case1 \<or> ?case2" |
10249 | 405 |
proof (elim exE conjE) |
406 |
fix a' M0' K |
|
407 |
assume N: "N = M0' + K" and r: "?r K a'" |
|
408 |
assume "M0 + {#a#} = M0' + {#a'#}" |
|
18258 | 409 |
then have "M0 = M0' \<and> a = a' \<or> |
11464 | 410 |
(\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})" |
10249 | 411 |
by (simp only: add_eq_conv_ex) |
18258 | 412 |
then show ?thesis |
10249 | 413 |
proof (elim disjE conjE exE) |
414 |
assume "M0 = M0'" "a = a'" |
|
11464 | 415 |
with N r have "?r K a \<and> N = M0 + K" by simp |
18258 | 416 |
then have ?case2 .. then show ?thesis .. |
10249 | 417 |
next |
418 |
fix K' |
|
419 |
assume "M0' = K' + {#a#}" |
|
420 |
with N have n: "N = K' + K + {#a#}" by (simp add: union_ac) |
|
421 |
||
422 |
assume "M0 = K' + {#a'#}" |
|
423 |
with r have "?R (K' + K) M0" by blast |
|
18258 | 424 |
with n have ?case1 by simp then show ?thesis .. |
10249 | 425 |
qed |
426 |
qed |
|
427 |
qed |
|
428 |
||
11464 | 429 |
lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)" |
10249 | 430 |
proof |
431 |
let ?R = "mult1 r" |
|
432 |
let ?W = "acc ?R" |
|
433 |
{ |
|
434 |
fix M M0 a |
|
11464 | 435 |
assume M0: "M0 \<in> ?W" |
12399 | 436 |
and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)" |
11464 | 437 |
and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W" |
438 |
have "M0 + {#a#} \<in> ?W" |
|
10249 | 439 |
proof (rule accI [of "M0 + {#a#}"]) |
440 |
fix N |
|
11464 | 441 |
assume "(N, M0 + {#a#}) \<in> ?R" |
18258 | 442 |
then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or> |
11464 | 443 |
(\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))" |
10249 | 444 |
by (rule less_add) |
18258 | 445 |
then show "N \<in> ?W" |
10249 | 446 |
proof (elim exE disjE conjE) |
11464 | 447 |
fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}" |
448 |
from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" .. |
|
18258 | 449 |
then have "M + {#a#} \<in> ?W" .. |
450 |
then show "N \<in> ?W" by (simp only: N) |
|
10249 | 451 |
next |
452 |
fix K |
|
453 |
assume N: "N = M0 + K" |
|
11464 | 454 |
assume "\<forall>b. b :# K --> (b, a) \<in> r" |
18730 | 455 |
then have "M0 + K \<in> ?W" |
10249 | 456 |
proof (induct K) |
18730 | 457 |
case empty |
18258 | 458 |
from M0 show "M0 + {#} \<in> ?W" by simp |
18730 | 459 |
next |
460 |
case (add K x) |
|
461 |
from add.prems have "(x, a) \<in> r" by simp |
|
18258 | 462 |
with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast |
18730 | 463 |
moreover from add have "M0 + K \<in> ?W" by simp |
18258 | 464 |
ultimately have "(M0 + K) + {#x#} \<in> ?W" .. |
465 |
then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: union_assoc) |
|
10249 | 466 |
qed |
18730 | 467 |
then show "N \<in> ?W" by (simp only: N) |
10249 | 468 |
qed |
469 |
qed |
|
470 |
} note tedious_reasoning = this |
|
471 |
||
472 |
assume wf: "wf r" |
|
473 |
fix M |
|
11464 | 474 |
show "M \<in> ?W" |
10249 | 475 |
proof (induct M) |
11464 | 476 |
show "{#} \<in> ?W" |
10249 | 477 |
proof (rule accI) |
11464 | 478 |
fix b assume "(b, {#}) \<in> ?R" |
479 |
with not_less_empty show "b \<in> ?W" by contradiction |
|
10249 | 480 |
qed |
481 |
||
11464 | 482 |
fix M a assume "M \<in> ?W" |
483 |
from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W" |
|
10249 | 484 |
proof induct |
485 |
fix a |
|
12399 | 486 |
assume "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)" |
11464 | 487 |
show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W" |
10249 | 488 |
proof |
11464 | 489 |
fix M assume "M \<in> ?W" |
18258 | 490 |
then show "M + {#a#} \<in> ?W" |
10249 | 491 |
by (rule acc_induct) (rule tedious_reasoning) |
492 |
qed |
|
493 |
qed |
|
18258 | 494 |
then show "M + {#a#} \<in> ?W" .. |
10249 | 495 |
qed |
496 |
qed |
|
497 |
||
498 |
theorem wf_mult1: "wf r ==> wf (mult1 r)" |
|
499 |
by (rule acc_wfI, rule all_accessible) |
|
500 |
||
501 |
theorem wf_mult: "wf r ==> wf (mult r)" |
|
502 |
by (unfold mult_def, rule wf_trancl, rule wf_mult1) |
|
503 |
||
504 |
||
505 |
subsubsection {* Closure-free presentation *} |
|
506 |
||
507 |
(*Badly needed: a linear arithmetic procedure for multisets*) |
|
508 |
||
509 |
lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})" |
|
15072 | 510 |
by (simp add: multiset_eq_conv_count_eq) |
10249 | 511 |
|
512 |
text {* One direction. *} |
|
513 |
||
514 |
lemma mult_implies_one_step: |
|
11464 | 515 |
"trans r ==> (M, N) \<in> mult r ==> |
516 |
\<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and> |
|
517 |
(\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)" |
|
10249 | 518 |
apply (unfold mult_def mult1_def set_of_def) |
15072 | 519 |
apply (erule converse_trancl_induct, clarify) |
520 |
apply (rule_tac x = M0 in exI, simp, clarify) |
|
10249 | 521 |
apply (case_tac "a :# K") |
522 |
apply (rule_tac x = I in exI) |
|
523 |
apply (simp (no_asm)) |
|
524 |
apply (rule_tac x = "(K - {#a#}) + Ka" in exI) |
|
525 |
apply (simp (no_asm_simp) add: union_assoc [symmetric]) |
|
11464 | 526 |
apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong) |
10249 | 527 |
apply (simp add: diff_union_single_conv) |
528 |
apply (simp (no_asm_use) add: trans_def) |
|
529 |
apply blast |
|
530 |
apply (subgoal_tac "a :# I") |
|
531 |
apply (rule_tac x = "I - {#a#}" in exI) |
|
532 |
apply (rule_tac x = "J + {#a#}" in exI) |
|
533 |
apply (rule_tac x = "K + Ka" in exI) |
|
534 |
apply (rule conjI) |
|
535 |
apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split) |
|
536 |
apply (rule conjI) |
|
15072 | 537 |
apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp) |
10249 | 538 |
apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split) |
539 |
apply (simp (no_asm_use) add: trans_def) |
|
540 |
apply blast |
|
10277 | 541 |
apply (subgoal_tac "a :# (M0 + {#a#})") |
10249 | 542 |
apply simp |
543 |
apply (simp (no_asm)) |
|
544 |
done |
|
545 |
||
546 |
lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}" |
|
15072 | 547 |
by (simp add: multiset_eq_conv_count_eq) |
10249 | 548 |
|
11464 | 549 |
lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> \<exists>a N. M = N + {#a#}" |
10249 | 550 |
apply (erule size_eq_Suc_imp_elem [THEN exE]) |
15072 | 551 |
apply (drule elem_imp_eq_diff_union, auto) |
10249 | 552 |
done |
553 |
||
554 |
lemma one_step_implies_mult_aux: |
|
555 |
"trans r ==> |
|
11464 | 556 |
\<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)) |
557 |
--> (I + K, I + J) \<in> mult r" |
|
15072 | 558 |
apply (induct_tac n, auto) |
559 |
apply (frule size_eq_Suc_imp_eq_union, clarify) |
|
560 |
apply (rename_tac "J'", simp) |
|
561 |
apply (erule notE, auto) |
|
10249 | 562 |
apply (case_tac "J' = {#}") |
563 |
apply (simp add: mult_def) |
|
564 |
apply (rule r_into_trancl) |
|
15072 | 565 |
apply (simp add: mult1_def set_of_def, blast) |
11464 | 566 |
txt {* Now we know @{term "J' \<noteq> {#}"}. *} |
567 |
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition) |
|
568 |
apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp) |
|
10249 | 569 |
apply (erule ssubst) |
15072 | 570 |
apply (simp add: Ball_def, auto) |
10249 | 571 |
apply (subgoal_tac |
11464 | 572 |
"((I + {# x : K. (x, a) \<in> r #}) + {# x : K. (x, a) \<notin> r #}, |
573 |
(I + {# x : K. (x, a) \<in> r #}) + J') \<in> mult r") |
|
10249 | 574 |
prefer 2 |
575 |
apply force |
|
576 |
apply (simp (no_asm_use) add: union_assoc [symmetric] mult_def) |
|
577 |
apply (erule trancl_trans) |
|
578 |
apply (rule r_into_trancl) |
|
579 |
apply (simp add: mult1_def set_of_def) |
|
580 |
apply (rule_tac x = a in exI) |
|
581 |
apply (rule_tac x = "I + J'" in exI) |
|
582 |
apply (simp add: union_ac) |
|
583 |
done |
|
584 |
||
17161 | 585 |
lemma one_step_implies_mult: |
11464 | 586 |
"trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r |
587 |
==> (I + K, I + J) \<in> mult r" |
|
15072 | 588 |
apply (insert one_step_implies_mult_aux, blast) |
10249 | 589 |
done |
590 |
||
591 |
||
592 |
subsubsection {* Partial-order properties *} |
|
593 |
||
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
11868
diff
changeset
|
594 |
instance multiset :: (type) ord .. |
10249 | 595 |
|
596 |
defs (overloaded) |
|
11464 | 597 |
less_multiset_def: "M' < M == (M', M) \<in> mult {(x', x). x' < x}" |
598 |
le_multiset_def: "M' <= M == M' = M \<or> M' < (M::'a multiset)" |
|
10249 | 599 |
|
600 |
lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}" |
|
18730 | 601 |
unfolding trans_def by (blast intro: order_less_trans) |
10249 | 602 |
|
603 |
text {* |
|
604 |
\medskip Irreflexivity. |
|
605 |
*} |
|
606 |
||
607 |
lemma mult_irrefl_aux: |
|
18258 | 608 |
"finite A ==> (\<forall>x \<in> A. \<exists>y \<in> A. x < (y::'a::order)) \<Longrightarrow> A = {}" |
609 |
apply (induct rule: finite_induct) |
|
10249 | 610 |
apply (auto intro: order_less_trans) |
611 |
done |
|
612 |
||
17161 | 613 |
lemma mult_less_not_refl: "\<not> M < (M::'a::order multiset)" |
15072 | 614 |
apply (unfold less_multiset_def, auto) |
615 |
apply (drule trans_base_order [THEN mult_implies_one_step], auto) |
|
10249 | 616 |
apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]]) |
617 |
apply (simp add: set_of_eq_empty_iff) |
|
618 |
done |
|
619 |
||
620 |
lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R" |
|
15072 | 621 |
by (insert mult_less_not_refl, fast) |
10249 | 622 |
|
623 |
||
624 |
text {* Transitivity. *} |
|
625 |
||
626 |
theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)" |
|
627 |
apply (unfold less_multiset_def mult_def) |
|
628 |
apply (blast intro: trancl_trans) |
|
629 |
done |
|
630 |
||
631 |
text {* Asymmetry. *} |
|
632 |
||
11464 | 633 |
theorem mult_less_not_sym: "M < N ==> \<not> N < (M::'a::order multiset)" |
10249 | 634 |
apply auto |
635 |
apply (rule mult_less_not_refl [THEN notE]) |
|
15072 | 636 |
apply (erule mult_less_trans, assumption) |
10249 | 637 |
done |
638 |
||
639 |
theorem mult_less_asym: |
|
11464 | 640 |
"M < N ==> (\<not> P ==> N < (M::'a::order multiset)) ==> P" |
15072 | 641 |
by (insert mult_less_not_sym, blast) |
10249 | 642 |
|
643 |
theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)" |
|
18730 | 644 |
unfolding le_multiset_def by auto |
10249 | 645 |
|
646 |
text {* Anti-symmetry. *} |
|
647 |
||
648 |
theorem mult_le_antisym: |
|
649 |
"M <= N ==> N <= M ==> M = (N::'a::order multiset)" |
|
18730 | 650 |
unfolding le_multiset_def by (blast dest: mult_less_not_sym) |
10249 | 651 |
|
652 |
text {* Transitivity. *} |
|
653 |
||
654 |
theorem mult_le_trans: |
|
655 |
"K <= M ==> M <= N ==> K <= (N::'a::order multiset)" |
|
18730 | 656 |
unfolding le_multiset_def by (blast intro: mult_less_trans) |
10249 | 657 |
|
11655 | 658 |
theorem mult_less_le: "(M < N) = (M <= N \<and> M \<noteq> (N::'a::order multiset))" |
18730 | 659 |
unfolding le_multiset_def by auto |
10249 | 660 |
|
10277 | 661 |
text {* Partial order. *} |
662 |
||
663 |
instance multiset :: (order) order |
|
664 |
apply intro_classes |
|
665 |
apply (rule mult_le_refl) |
|
15072 | 666 |
apply (erule mult_le_trans, assumption) |
667 |
apply (erule mult_le_antisym, assumption) |
|
10277 | 668 |
apply (rule mult_less_le) |
669 |
done |
|
670 |
||
10249 | 671 |
|
672 |
subsubsection {* Monotonicity of multiset union *} |
|
673 |
||
17161 | 674 |
lemma mult1_union: |
11464 | 675 |
"(B, D) \<in> mult1 r ==> trans r ==> (C + B, C + D) \<in> mult1 r" |
15072 | 676 |
apply (unfold mult1_def, auto) |
10249 | 677 |
apply (rule_tac x = a in exI) |
678 |
apply (rule_tac x = "C + M0" in exI) |
|
679 |
apply (simp add: union_assoc) |
|
680 |
done |
|
681 |
||
682 |
lemma union_less_mono2: "B < D ==> C + B < C + (D::'a::order multiset)" |
|
683 |
apply (unfold less_multiset_def mult_def) |
|
684 |
apply (erule trancl_induct) |
|
685 |
apply (blast intro: mult1_union transI order_less_trans r_into_trancl) |
|
686 |
apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans) |
|
687 |
done |
|
688 |
||
689 |
lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)" |
|
690 |
apply (subst union_commute [of B C]) |
|
691 |
apply (subst union_commute [of D C]) |
|
692 |
apply (erule union_less_mono2) |
|
693 |
done |
|
694 |
||
17161 | 695 |
lemma union_less_mono: |
10249 | 696 |
"A < C ==> B < D ==> A + B < C + (D::'a::order multiset)" |
697 |
apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans) |
|
698 |
done |
|
699 |
||
17161 | 700 |
lemma union_le_mono: |
10249 | 701 |
"A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)" |
18730 | 702 |
unfolding le_multiset_def |
703 |
by (blast intro: union_less_mono union_less_mono1 union_less_mono2) |
|
10249 | 704 |
|
17161 | 705 |
lemma empty_leI [iff]: "{#} <= (M::'a::order multiset)" |
10249 | 706 |
apply (unfold le_multiset_def less_multiset_def) |
707 |
apply (case_tac "M = {#}") |
|
708 |
prefer 2 |
|
11464 | 709 |
apply (subgoal_tac "({#} + {#}, {#} + M) \<in> mult (Collect (split op <))") |
10249 | 710 |
prefer 2 |
711 |
apply (rule one_step_implies_mult) |
|
15072 | 712 |
apply (simp only: trans_def, auto) |
10249 | 713 |
done |
714 |
||
17161 | 715 |
lemma union_upper1: "A <= A + (B::'a::order multiset)" |
15072 | 716 |
proof - |
17200 | 717 |
have "A + {#} <= A + B" by (blast intro: union_le_mono) |
18258 | 718 |
then show ?thesis by simp |
15072 | 719 |
qed |
720 |
||
17161 | 721 |
lemma union_upper2: "B <= A + (B::'a::order multiset)" |
18258 | 722 |
by (subst union_commute) (rule union_upper1) |
15072 | 723 |
|
724 |
||
17200 | 725 |
subsection {* Link with lists *} |
15072 | 726 |
|
17200 | 727 |
consts |
15072 | 728 |
multiset_of :: "'a list \<Rightarrow> 'a multiset" |
729 |
primrec |
|
730 |
"multiset_of [] = {#}" |
|
731 |
"multiset_of (a # x) = multiset_of x + {# a #}" |
|
732 |
||
733 |
lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])" |
|
18258 | 734 |
by (induct x) auto |
15072 | 735 |
|
736 |
lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])" |
|
18258 | 737 |
by (induct x) auto |
15072 | 738 |
|
739 |
lemma set_of_multiset_of[simp]: "set_of(multiset_of x) = set x" |
|
18258 | 740 |
by (induct x) auto |
15867 | 741 |
|
742 |
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)" |
|
743 |
by (induct xs) auto |
|
15072 | 744 |
|
18258 | 745 |
lemma multiset_of_append [simp]: |
746 |
"multiset_of (xs @ ys) = multiset_of xs + multiset_of ys" |
|
747 |
by (induct xs fixing: ys) (auto simp: union_ac) |
|
18730 | 748 |
|
15072 | 749 |
lemma surj_multiset_of: "surj multiset_of" |
17200 | 750 |
apply (unfold surj_def, rule allI) |
751 |
apply (rule_tac M=y in multiset_induct, auto) |
|
752 |
apply (rule_tac x = "x # xa" in exI, auto) |
|
10249 | 753 |
done |
754 |
||
15072 | 755 |
lemma set_count_greater_0: "set x = {a. 0 < count (multiset_of x) a}" |
18258 | 756 |
by (induct x) auto |
15072 | 757 |
|
17200 | 758 |
lemma distinct_count_atmost_1: |
15072 | 759 |
"distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))" |
18258 | 760 |
apply (induct x, simp, rule iffI, simp_all) |
17200 | 761 |
apply (rule conjI) |
762 |
apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of) |
|
15072 | 763 |
apply (erule_tac x=a in allE, simp, clarify) |
17200 | 764 |
apply (erule_tac x=aa in allE, simp) |
15072 | 765 |
done |
766 |
||
17200 | 767 |
lemma multiset_of_eq_setD: |
15867 | 768 |
"multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys" |
769 |
by (rule) (auto simp add:multiset_eq_conv_count_eq set_count_greater_0) |
|
770 |
||
17200 | 771 |
lemma set_eq_iff_multiset_of_eq_distinct: |
772 |
"\<lbrakk>distinct x; distinct y\<rbrakk> |
|
15072 | 773 |
\<Longrightarrow> (set x = set y) = (multiset_of x = multiset_of y)" |
17200 | 774 |
by (auto simp: multiset_eq_conv_count_eq distinct_count_atmost_1) |
15072 | 775 |
|
17200 | 776 |
lemma set_eq_iff_multiset_of_remdups_eq: |
15072 | 777 |
"(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))" |
17200 | 778 |
apply (rule iffI) |
779 |
apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1]) |
|
780 |
apply (drule distinct_remdups[THEN distinct_remdups |
|
781 |
[THEN set_eq_iff_multiset_of_eq_distinct[THEN iffD2]]]) |
|
15072 | 782 |
apply simp |
10249 | 783 |
done |
784 |
||
18258 | 785 |
lemma multiset_of_compl_union [simp]: |
786 |
"multiset_of [x\<in>xs. P x] + multiset_of [x\<in>xs. \<not>P x] = multiset_of xs" |
|
15630 | 787 |
by (induct xs) (auto simp: union_ac) |
15072 | 788 |
|
17200 | 789 |
lemma count_filter: |
18258 | 790 |
"count (multiset_of xs) x = length [y \<in> xs. y = x]" |
791 |
by (induct xs) auto |
|
15867 | 792 |
|
793 |
||
15072 | 794 |
subsection {* Pointwise ordering induced by count *} |
795 |
||
17200 | 796 |
consts |
15072 | 797 |
mset_le :: "['a multiset, 'a multiset] \<Rightarrow> bool" |
798 |
||
17200 | 799 |
syntax |
800 |
"_mset_le" :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" ("_ \<le># _" [50,51] 50) |
|
801 |
translations |
|
15072 | 802 |
"x \<le># y" == "mset_le x y" |
803 |
||
17200 | 804 |
defs |
805 |
mset_le_def: "xs \<le># ys == (\<forall>a. count xs a \<le> count ys a)" |
|
15072 | 806 |
|
807 |
lemma mset_le_refl[simp]: "xs \<le># xs" |
|
18730 | 808 |
unfolding mset_le_def by auto |
15072 | 809 |
|
810 |
lemma mset_le_trans: "\<lbrakk> xs \<le># ys; ys \<le># zs \<rbrakk> \<Longrightarrow> xs \<le># zs" |
|
18730 | 811 |
unfolding mset_le_def by (fast intro: order_trans) |
15072 | 812 |
|
813 |
lemma mset_le_antisym: "\<lbrakk> xs\<le># ys; ys \<le># xs\<rbrakk> \<Longrightarrow> xs = ys" |
|
17200 | 814 |
apply (unfold mset_le_def) |
815 |
apply (rule multiset_eq_conv_count_eq[THEN iffD2]) |
|
15072 | 816 |
apply (blast intro: order_antisym) |
817 |
done |
|
818 |
||
17200 | 819 |
lemma mset_le_exists_conv: |
820 |
"(xs \<le># ys) = (\<exists>zs. ys = xs + zs)" |
|
821 |
apply (unfold mset_le_def, rule iffI, rule_tac x = "ys - xs" in exI) |
|
15072 | 822 |
apply (auto intro: multiset_eq_conv_count_eq [THEN iffD2]) |
823 |
done |
|
824 |
||
825 |
lemma mset_le_mono_add_right_cancel[simp]: "(xs + zs \<le># ys + zs) = (xs \<le># ys)" |
|
18730 | 826 |
unfolding mset_le_def by auto |
15072 | 827 |
|
828 |
lemma mset_le_mono_add_left_cancel[simp]: "(zs + xs \<le># zs + ys) = (xs \<le># ys)" |
|
18730 | 829 |
unfolding mset_le_def by auto |
15072 | 830 |
|
17200 | 831 |
lemma mset_le_mono_add: "\<lbrakk> xs \<le># ys; vs \<le># ws \<rbrakk> \<Longrightarrow> xs + vs \<le># ys + ws" |
832 |
apply (unfold mset_le_def) |
|
833 |
apply auto |
|
15072 | 834 |
apply (erule_tac x=a in allE)+ |
835 |
apply auto |
|
836 |
done |
|
837 |
||
838 |
lemma mset_le_add_left[simp]: "xs \<le># xs + ys" |
|
18730 | 839 |
unfolding mset_le_def by auto |
15072 | 840 |
|
841 |
lemma mset_le_add_right[simp]: "ys \<le># xs + ys" |
|
18730 | 842 |
unfolding mset_le_def by auto |
15072 | 843 |
|
844 |
lemma multiset_of_remdups_le: "multiset_of (remdups x) \<le># multiset_of x" |
|
17200 | 845 |
apply (induct x) |
846 |
apply auto |
|
847 |
apply (rule mset_le_trans) |
|
848 |
apply auto |
|
849 |
done |
|
15072 | 850 |
|
10249 | 851 |
end |