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