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