author | blanchet |
Wed, 06 Jan 2016 13:04:31 +0100 | |
changeset 62082 | 614ef6d7a6b6 |
parent 61955 | e96292f32c3c |
child 62208 | ad43b3ab06e4 |
permissions | -rw-r--r-- |
10249 | 1 |
(* Title: HOL/Library/Multiset.thy |
15072 | 2 |
Author: Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
3 |
Author: Andrei Popescu, TU Muenchen |
59813 | 4 |
Author: Jasmin Blanchette, Inria, LORIA, MPII |
5 |
Author: Dmitriy Traytel, TU Muenchen |
|
6 |
Author: Mathias Fleury, MPII |
|
10249 | 7 |
*) |
8 |
||
60500 | 9 |
section \<open>(Finite) multisets\<close> |
10249 | 10 |
|
15131 | 11 |
theory Multiset |
51599
1559e9266280
optionalized very specific code setup for multisets
haftmann
parents:
51548
diff
changeset
|
12 |
imports Main |
15131 | 13 |
begin |
10249 | 14 |
|
60500 | 15 |
subsection \<open>The type of multisets\<close> |
10249 | 16 |
|
60606 | 17 |
definition "multiset = {f :: 'a \<Rightarrow> nat. finite {x. f x > 0}}" |
18 |
||
19 |
typedef 'a multiset = "multiset :: ('a \<Rightarrow> nat) set" |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
20 |
morphisms count Abs_multiset |
45694
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
45608
diff
changeset
|
21 |
unfolding multiset_def |
10249 | 22 |
proof |
45694
4a8743618257
prefer typedef without extra definition and alternative name;
wenzelm
parents:
45608
diff
changeset
|
23 |
show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp |
10249 | 24 |
qed |
25 |
||
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
26 |
setup_lifting type_definition_multiset |
19086 | 27 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
28 |
abbreviation Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<in>#" 50) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
29 |
where "a \<in># M \<equiv> 0 < count M a" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
30 |
|
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
31 |
notation (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
32 |
Melem ("(_/ :# _)" [50, 51] 50) (* FIXME !? *) |
10249 | 33 |
|
60606 | 34 |
lemma multiset_eq_iff: "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
35 |
by (simp only: count_inject [symmetric] fun_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
36 |
|
60606 | 37 |
lemma multiset_eqI: "(\<And>x. count A x = count B x) \<Longrightarrow> A = B" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
38 |
using multiset_eq_iff by auto |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
39 |
|
60606 | 40 |
text \<open>Preservation of the representing set @{term multiset}.\<close> |
41 |
||
42 |
lemma const0_in_multiset: "(\<lambda>a. 0) \<in> multiset" |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
43 |
by (simp add: multiset_def) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
44 |
|
60606 | 45 |
lemma only1_in_multiset: "(\<lambda>b. if b = a then n else 0) \<in> multiset" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
46 |
by (simp add: multiset_def) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
47 |
|
60606 | 48 |
lemma union_preserves_multiset: "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
49 |
by (simp add: multiset_def) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
50 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
51 |
lemma diff_preserves_multiset: |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
52 |
assumes "M \<in> multiset" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
53 |
shows "(\<lambda>a. M a - N a) \<in> multiset" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
54 |
proof - |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
55 |
have "{x. N x < M x} \<subseteq> {x. 0 < M x}" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
56 |
by auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
57 |
with assms show ?thesis |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
58 |
by (auto simp add: multiset_def intro: finite_subset) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
59 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
60 |
|
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
61 |
lemma filter_preserves_multiset: |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
62 |
assumes "M \<in> multiset" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
63 |
shows "(\<lambda>x. if P x then M x else 0) \<in> multiset" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
64 |
proof - |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
65 |
have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
66 |
by auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
67 |
with assms show ?thesis |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
68 |
by (auto simp add: multiset_def intro: finite_subset) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
69 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
70 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
71 |
lemmas in_multiset = const0_in_multiset only1_in_multiset |
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
72 |
union_preserves_multiset diff_preserves_multiset filter_preserves_multiset |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
73 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
74 |
|
60500 | 75 |
subsection \<open>Representing multisets\<close> |
76 |
||
77 |
text \<open>Multiset enumeration\<close> |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
78 |
|
48008 | 79 |
instantiation multiset :: (type) cancel_comm_monoid_add |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset
|
80 |
begin |
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset
|
81 |
|
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
82 |
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0" |
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
83 |
by (rule const0_in_multiset) |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset
|
84 |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
85 |
abbreviation Mempty :: "'a multiset" ("{#}") where |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
86 |
"Mempty \<equiv> 0" |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset
|
87 |
|
60606 | 88 |
lift_definition plus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)" |
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
89 |
by (rule union_preserves_multiset) |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset
|
90 |
|
60606 | 91 |
lift_definition minus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a" |
59815
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59813
diff
changeset
|
92 |
by (rule diff_preserves_multiset) |
cce82e360c2f
explicit commutative additive inverse operation;
haftmann
parents:
59813
diff
changeset
|
93 |
|
48008 | 94 |
instance |
60678 | 95 |
by (standard; transfer; simp add: fun_eq_iff) |
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset
|
96 |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25507
diff
changeset
|
97 |
end |
10249 | 98 |
|
60606 | 99 |
lift_definition single :: "'a \<Rightarrow> 'a multiset" is "\<lambda>a b. if b = a then 1 else 0" |
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
100 |
by (rule only1_in_multiset) |
15869 | 101 |
|
26145 | 102 |
syntax |
60606 | 103 |
"_multiset" :: "args \<Rightarrow> 'a multiset" ("{#(_)#}") |
25507 | 104 |
translations |
105 |
"{#x, xs#}" == "{#x#} + {#xs#}" |
|
106 |
"{#x#}" == "CONST single x" |
|
107 |
||
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
108 |
lemma count_empty [simp]: "count {#} a = 0" |
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
109 |
by (simp add: zero_multiset.rep_eq) |
10249 | 110 |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
111 |
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)" |
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
112 |
by (simp add: single.rep_eq) |
29901 | 113 |
|
10249 | 114 |
|
60500 | 115 |
subsection \<open>Basic operations\<close> |
116 |
||
117 |
subsubsection \<open>Union\<close> |
|
10249 | 118 |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
119 |
lemma count_union [simp]: "count (M + N) a = count M a + count N a" |
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
120 |
by (simp add: plus_multiset.rep_eq) |
10249 | 121 |
|
122 |
||
60500 | 123 |
subsubsection \<open>Difference\<close> |
10249 | 124 |
|
49388 | 125 |
instantiation multiset :: (type) comm_monoid_diff |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
126 |
begin |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
127 |
|
49388 | 128 |
instance |
60678 | 129 |
by (standard; transfer; simp add: fun_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
130 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
131 |
end |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
132 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
133 |
lemma count_diff [simp]: "count (M - N) a = count M a - count N a" |
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
134 |
by (simp add: minus_multiset.rep_eq) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
135 |
|
17161 | 136 |
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}" |
52289 | 137 |
by rule (fact Groups.diff_zero, fact Groups.zero_diff) |
36903 | 138 |
|
139 |
lemma diff_cancel[simp]: "A - A = {#}" |
|
52289 | 140 |
by (fact Groups.diff_cancel) |
10249 | 141 |
|
36903 | 142 |
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)" |
52289 | 143 |
by (fact add_diff_cancel_right') |
10249 | 144 |
|
36903 | 145 |
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)" |
52289 | 146 |
by (fact add_diff_cancel_left') |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
147 |
|
52289 | 148 |
lemma diff_right_commute: |
60606 | 149 |
fixes M N Q :: "'a multiset" |
150 |
shows "M - N - Q = M - Q - N" |
|
52289 | 151 |
by (fact diff_right_commute) |
152 |
||
153 |
lemma diff_add: |
|
60606 | 154 |
fixes M N Q :: "'a multiset" |
155 |
shows "M - (N + Q) = M - N - Q" |
|
52289 | 156 |
by (rule sym) (fact diff_diff_add) |
58425 | 157 |
|
60606 | 158 |
lemma insert_DiffM: "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
159 |
by (clarsimp simp: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
160 |
|
60606 | 161 |
lemma insert_DiffM2 [simp]: "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
162 |
by (clarsimp simp: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
163 |
|
60606 | 164 |
lemma diff_union_swap: "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
165 |
by (auto simp add: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
166 |
|
60606 | 167 |
lemma diff_union_single_conv: "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
168 |
by (simp add: multiset_eq_iff) |
26143
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
bulwahn
parents:
26033
diff
changeset
|
169 |
|
10249 | 170 |
|
60500 | 171 |
subsubsection \<open>Equality of multisets\<close> |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
172 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
173 |
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
174 |
by (simp add: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
175 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
176 |
lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
177 |
by (auto simp add: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
178 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
179 |
lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
180 |
by (auto simp add: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
181 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
182 |
lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
183 |
by (auto simp add: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
184 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
185 |
lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
186 |
by (auto simp add: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
187 |
|
60606 | 188 |
lemma diff_single_trivial: "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
189 |
by (auto simp add: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
190 |
|
60606 | 191 |
lemma diff_single_eq_union: "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
192 |
by auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
193 |
|
60606 | 194 |
lemma union_single_eq_diff: "M + {#x#} = N \<Longrightarrow> M = N - {#x#}" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
195 |
by (auto dest: sym) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
196 |
|
60606 | 197 |
lemma union_single_eq_member: "M + {#x#} = N \<Longrightarrow> x \<in># N" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
198 |
by auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
199 |
|
60606 | 200 |
lemma union_is_single: "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}" |
201 |
(is "?lhs = ?rhs") |
|
46730 | 202 |
proof |
60606 | 203 |
show ?lhs if ?rhs using that by auto |
204 |
show ?rhs if ?lhs |
|
205 |
using that by (simp add: multiset_eq_iff split: if_splits) (metis add_is_1) |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
206 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
207 |
|
60606 | 208 |
lemma single_is_union: "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
209 |
by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
210 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
211 |
lemma add_eq_conv_diff: |
60606 | 212 |
"M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}" |
213 |
(is "?lhs \<longleftrightarrow> ?rhs") |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44339
diff
changeset
|
214 |
(* shorter: by (simp add: multiset_eq_iff) fastforce *) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
215 |
proof |
60606 | 216 |
show ?lhs if ?rhs |
217 |
using that |
|
218 |
by (auto simp add: add.assoc add.commute [of "{#b#}"]) |
|
219 |
(drule sym, simp add: add.assoc [symmetric]) |
|
220 |
show ?rhs if ?lhs |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
221 |
proof (cases "a = b") |
60500 | 222 |
case True with \<open>?lhs\<close> show ?thesis by simp |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
223 |
next |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
224 |
case False |
60500 | 225 |
from \<open>?lhs\<close> have "a \<in># N + {#b#}" by (rule union_single_eq_member) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
226 |
with False have "a \<in># N" by auto |
60500 | 227 |
moreover from \<open>?lhs\<close> have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
228 |
moreover note False |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
229 |
ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
230 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
231 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
232 |
|
58425 | 233 |
lemma insert_noteq_member: |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
234 |
assumes BC: "B + {#b#} = C + {#c#}" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
235 |
and bnotc: "b \<noteq> c" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
236 |
shows "c \<in># B" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
237 |
proof - |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
238 |
have "c \<in># C + {#c#}" by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
239 |
have nc: "\<not> c \<in># {#b#}" using bnotc by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
240 |
then have "c \<in># B + {#b#}" using BC by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
241 |
then show "c \<in># B" using nc by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
242 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
243 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
244 |
lemma add_eq_conv_ex: |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
245 |
"(M + {#a#} = N + {#b#}) = |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
246 |
(M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
247 |
by (auto simp add: add_eq_conv_diff) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
248 |
|
60606 | 249 |
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}" |
60678 | 250 |
by (rule exI [where x = "M - {#x#}"]) simp |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
251 |
|
58425 | 252 |
lemma multiset_add_sub_el_shuffle: |
60606 | 253 |
assumes "c \<in># B" |
254 |
and "b \<noteq> c" |
|
58098 | 255 |
shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}" |
256 |
proof - |
|
60500 | 257 |
from \<open>c \<in># B\<close> obtain A where B: "B = A + {#c#}" |
58098 | 258 |
by (blast dest: multi_member_split) |
259 |
have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp |
|
58425 | 260 |
then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}" |
58098 | 261 |
by (simp add: ac_simps) |
262 |
then show ?thesis using B by simp |
|
263 |
qed |
|
264 |
||
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
265 |
|
60500 | 266 |
subsubsection \<open>Pointwise ordering induced by count\<close> |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
267 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
268 |
definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subseteq>#" 50) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
269 |
where "A \<subseteq># B = (\<forall>a. count A a \<le> count B a)" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
270 |
|
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
271 |
definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subset>#" 50) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
272 |
where "A \<subset># B = (A \<subseteq># B \<and> A \<noteq> B)" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
273 |
|
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
274 |
notation (input) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
275 |
subseteq_mset (infix "\<le>#" 50) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
276 |
|
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
277 |
notation (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
278 |
subseteq_mset (infix "<=#" 50) and |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
279 |
subset_mset (infix "<#" 50) |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
280 |
|
60606 | 281 |
interpretation subset_mset: ordered_ab_semigroup_add_imp_le "op +" "op -" "op \<subseteq>#" "op \<subset>#" |
60678 | 282 |
by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order_trans antisym) |
59986
f38b94549dc8
introduced new abbreviations for multiset operations (in the hope of getting rid of the old names <, <=, etc.)
blanchet
parents:
59958
diff
changeset
|
283 |
|
60606 | 284 |
lemma mset_less_eqI: "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le># B" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
285 |
by (simp add: subseteq_mset_def) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
286 |
|
60606 | 287 |
lemma mset_le_exists_conv: "(A::'a multiset) \<le># B \<longleftrightarrow> (\<exists>C. B = A + C)" |
60678 | 288 |
unfolding subseteq_mset_def |
289 |
apply (rule iffI) |
|
290 |
apply (rule exI [where x = "B - A"]) |
|
291 |
apply (auto intro: multiset_eq_iff [THEN iffD2]) |
|
292 |
done |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
293 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
294 |
interpretation subset_mset: ordered_cancel_comm_monoid_diff "op +" "op -" 0 "op \<le>#" "op <#" |
60678 | 295 |
by standard (simp, fact mset_le_exists_conv) |
52289 | 296 |
|
60606 | 297 |
lemma mset_le_mono_add_right_cancel [simp]: "(A::'a multiset) + C \<le># B + C \<longleftrightarrow> A \<le># B" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
298 |
by (fact subset_mset.add_le_cancel_right) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
299 |
|
60606 | 300 |
lemma mset_le_mono_add_left_cancel [simp]: "C + (A::'a multiset) \<le># C + B \<longleftrightarrow> A \<le># B" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
301 |
by (fact subset_mset.add_le_cancel_left) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
302 |
|
60606 | 303 |
lemma mset_le_mono_add: "(A::'a multiset) \<le># B \<Longrightarrow> C \<le># D \<Longrightarrow> A + C \<le># B + D" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
304 |
by (fact subset_mset.add_mono) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
305 |
|
60606 | 306 |
lemma mset_le_add_left [simp]: "(A::'a multiset) \<le># A + B" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
307 |
unfolding subseteq_mset_def by auto |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
308 |
|
60606 | 309 |
lemma mset_le_add_right [simp]: "B \<le># (A::'a multiset) + B" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
310 |
unfolding subseteq_mset_def by auto |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
311 |
|
60607 | 312 |
lemma mset_le_single: "a \<in># B \<Longrightarrow> {#a#} \<le># B" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
313 |
by (simp add: subseteq_mset_def) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
314 |
|
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
315 |
lemma multiset_diff_union_assoc: |
60606 | 316 |
fixes A B C D :: "'a multiset" |
317 |
shows "C \<le># B \<Longrightarrow> A + B - C = A + (B - C)" |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
318 |
by (simp add: subset_mset.diff_add_assoc) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
319 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
320 |
lemma mset_le_multiset_union_diff_commute: |
60606 | 321 |
fixes A B C D :: "'a multiset" |
322 |
shows "B \<le># A \<Longrightarrow> A - B + C = A + C - B" |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
323 |
by (simp add: subset_mset.add_diff_assoc2) |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
324 |
|
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
325 |
lemma diff_le_self[simp]: "(M::'a multiset) - N \<le># M" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
326 |
by(simp add: subseteq_mset_def) |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
327 |
|
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
328 |
lemma mset_lessD: "A <# B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
329 |
apply (clarsimp simp: subset_mset_def subseteq_mset_def) |
60678 | 330 |
apply (erule allE [where x = x]) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
331 |
apply auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
332 |
done |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
333 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
334 |
lemma mset_leD: "A \<le># B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
335 |
apply (clarsimp simp: subset_mset_def subseteq_mset_def) |
60678 | 336 |
apply (erule allE [where x = x]) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
337 |
apply auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
338 |
done |
58425 | 339 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
340 |
lemma mset_less_insertD: "(A + {#x#} <# B) \<Longrightarrow> (x \<in># B \<and> A <# B)" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
341 |
apply (rule conjI) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
342 |
apply (simp add: mset_lessD) |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
343 |
apply (clarsimp simp: subset_mset_def subseteq_mset_def) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
344 |
apply safe |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
345 |
apply (erule_tac x = a in allE) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
346 |
apply (auto split: split_if_asm) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
347 |
done |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
348 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
349 |
lemma mset_le_insertD: "(A + {#x#} \<le># B) \<Longrightarrow> (x \<in># B \<and> A \<le># B)" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
350 |
apply (rule conjI) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
351 |
apply (simp add: mset_leD) |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
352 |
apply (force simp: subset_mset_def subseteq_mset_def split: split_if_asm) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
353 |
done |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
354 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
355 |
lemma mset_less_of_empty[simp]: "A <# {#} \<longleftrightarrow> False" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
356 |
by (auto simp add: subseteq_mset_def subset_mset_def multiset_eq_iff) |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
357 |
|
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
358 |
lemma empty_le[simp]: "{#} \<le># A" |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
359 |
unfolding mset_le_exists_conv by auto |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
360 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
361 |
lemma le_empty[simp]: "(M \<le># {#}) = (M = {#})" |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
362 |
unfolding mset_le_exists_conv by auto |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
363 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
364 |
lemma multi_psub_of_add_self[simp]: "A <# A + {#x#}" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
365 |
by (auto simp: subset_mset_def subseteq_mset_def) |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
366 |
|
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
367 |
lemma multi_psub_self[simp]: "(A::'a multiset) <# A = False" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
368 |
by simp |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
369 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
370 |
lemma mset_less_add_bothsides: "N + {#x#} <# M + {#x#} \<Longrightarrow> N <# M" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
371 |
by (fact subset_mset.add_less_imp_less_right) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
372 |
|
60606 | 373 |
lemma mset_less_empty_nonempty: "{#} <# S \<longleftrightarrow> S \<noteq> {#}" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
374 |
by (auto simp: subset_mset_def subseteq_mset_def) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
375 |
|
60606 | 376 |
lemma mset_less_diff_self: "c \<in># B \<Longrightarrow> B - {#c#} <# B" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
377 |
by (auto simp: subset_mset_def subseteq_mset_def multiset_eq_iff) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
378 |
|
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
379 |
|
60500 | 380 |
subsubsection \<open>Intersection\<close> |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
381 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
382 |
definition inf_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
383 |
multiset_inter_def: "inf_subset_mset A B = A - (A - B)" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
384 |
|
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
385 |
interpretation subset_mset: semilattice_inf inf_subset_mset "op \<le>#" "op <#" |
46921 | 386 |
proof - |
60678 | 387 |
have [simp]: "m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" for m n q :: nat |
388 |
by arith |
|
389 |
show "class.semilattice_inf op #\<inter> op \<le># op <#" |
|
390 |
by standard (auto simp add: multiset_inter_def subseteq_mset_def) |
|
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
391 |
qed |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
392 |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
393 |
|
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
394 |
lemma multiset_inter_count [simp]: |
60606 | 395 |
fixes A B :: "'a multiset" |
396 |
shows "count (A #\<inter> B) x = min (count A x) (count B x)" |
|
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
397 |
by (simp add: multiset_inter_def) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
398 |
|
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
399 |
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}" |
46730 | 400 |
by (rule multiset_eqI) auto |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
401 |
|
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
402 |
lemma multiset_union_diff_commute: |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
403 |
assumes "B #\<inter> C = {#}" |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
404 |
shows "A + B - C = A - C + B" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
405 |
proof (rule multiset_eqI) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
406 |
fix x |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
407 |
from assms have "min (count B x) (count C x) = 0" |
46730 | 408 |
by (auto simp add: multiset_eq_iff) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
409 |
then have "count B x = 0 \<or> count C x = 0" |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
410 |
by auto |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
411 |
then show "count (A + B - C) x = count (A - C + B) x" |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
412 |
by auto |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
413 |
qed |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
414 |
|
60606 | 415 |
lemma empty_inter [simp]: "{#} #\<inter> M = {#}" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
416 |
by (simp add: multiset_eq_iff) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
417 |
|
60606 | 418 |
lemma inter_empty [simp]: "M #\<inter> {#} = {#}" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
419 |
by (simp add: multiset_eq_iff) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
420 |
|
60606 | 421 |
lemma inter_add_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
422 |
by (simp add: multiset_eq_iff) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
423 |
|
60606 | 424 |
lemma inter_add_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
425 |
by (simp add: multiset_eq_iff) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
426 |
|
60606 | 427 |
lemma inter_add_right1: "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
428 |
by (simp add: multiset_eq_iff) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
429 |
|
60606 | 430 |
lemma inter_add_right2: "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
431 |
by (simp add: multiset_eq_iff) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
432 |
|
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
433 |
|
60500 | 434 |
subsubsection \<open>Bounded union\<close> |
60678 | 435 |
|
436 |
definition sup_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"(infixl "#\<union>" 70) |
|
437 |
where "sup_subset_mset A B = A + (B - A)" |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
438 |
|
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
439 |
interpretation subset_mset: semilattice_sup sup_subset_mset "op \<le>#" "op <#" |
51623 | 440 |
proof - |
60678 | 441 |
have [simp]: "m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" for m n q :: nat |
442 |
by arith |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
443 |
show "class.semilattice_sup op #\<union> op \<le># op <#" |
60678 | 444 |
by standard (auto simp add: sup_subset_mset_def subseteq_mset_def) |
51623 | 445 |
qed |
446 |
||
60606 | 447 |
lemma sup_subset_mset_count [simp]: "count (A #\<union> B) x = max (count A x) (count B x)" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
448 |
by (simp add: sup_subset_mset_def) |
51623 | 449 |
|
60606 | 450 |
lemma empty_sup [simp]: "{#} #\<union> M = M" |
51623 | 451 |
by (simp add: multiset_eq_iff) |
452 |
||
60606 | 453 |
lemma sup_empty [simp]: "M #\<union> {#} = M" |
51623 | 454 |
by (simp add: multiset_eq_iff) |
455 |
||
60606 | 456 |
lemma sup_add_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}" |
51623 | 457 |
by (simp add: multiset_eq_iff) |
458 |
||
60606 | 459 |
lemma sup_add_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}" |
51623 | 460 |
by (simp add: multiset_eq_iff) |
461 |
||
60606 | 462 |
lemma sup_add_right1: "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}" |
51623 | 463 |
by (simp add: multiset_eq_iff) |
464 |
||
60606 | 465 |
lemma sup_add_right2: "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}" |
51623 | 466 |
by (simp add: multiset_eq_iff) |
467 |
||
60500 | 468 |
subsubsection \<open>Subset is an order\<close> |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
469 |
interpretation subset_mset: order "op \<le>#" "op <#" by unfold_locales auto |
51623 | 470 |
|
60500 | 471 |
subsubsection \<open>Filter (with comprehension syntax)\<close> |
472 |
||
473 |
text \<open>Multiset comprehension\<close> |
|
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
474 |
|
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
475 |
lift_definition filter_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" |
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
476 |
is "\<lambda>P M. \<lambda>x. if P x then M x else 0" |
47429
ec64d94cbf9c
multiset operations are defined with lift_definitions;
bulwahn
parents:
47308
diff
changeset
|
477 |
by (rule filter_preserves_multiset) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
478 |
|
60606 | 479 |
lemma count_filter_mset [simp]: "count (filter_mset P M) a = (if P a then count M a else 0)" |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
480 |
by (simp add: filter_mset.rep_eq) |
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
481 |
|
60606 | 482 |
lemma filter_empty_mset [simp]: "filter_mset P {#} = {#}" |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
483 |
by (rule multiset_eqI) simp |
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
484 |
|
60606 | 485 |
lemma filter_single_mset [simp]: "filter_mset P {#x#} = (if P x then {#x#} else {#})" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
486 |
by (rule multiset_eqI) simp |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
487 |
|
60606 | 488 |
lemma filter_union_mset [simp]: "filter_mset P (M + N) = filter_mset P M + filter_mset P N" |
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
489 |
by (rule multiset_eqI) simp |
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
490 |
|
60606 | 491 |
lemma filter_diff_mset [simp]: "filter_mset P (M - N) = filter_mset P M - filter_mset P N" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
492 |
by (rule multiset_eqI) simp |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
493 |
|
60606 | 494 |
lemma filter_inter_mset [simp]: "filter_mset P (M #\<inter> N) = filter_mset P M #\<inter> filter_mset P N" |
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
495 |
by (rule multiset_eqI) simp |
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
496 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
497 |
lemma multiset_filter_subset[simp]: "filter_mset f M \<le># M" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
498 |
by (simp add: mset_less_eqI) |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
499 |
|
60606 | 500 |
lemma multiset_filter_mono: |
501 |
assumes "A \<le># B" |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
502 |
shows "filter_mset f A \<le># filter_mset f B" |
58035 | 503 |
proof - |
504 |
from assms[unfolded mset_le_exists_conv] |
|
505 |
obtain C where B: "B = A + C" by auto |
|
506 |
show ?thesis unfolding B by auto |
|
507 |
qed |
|
508 |
||
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
509 |
syntax (ASCII) |
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
510 |
"_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset" ("(1{# _ :# _./ _#})") |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
511 |
syntax |
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
512 |
"_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset" ("(1{# _ \<in># _./ _#})") |
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
513 |
translations |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
514 |
"{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M" |
41069
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
haftmann
parents:
40968
diff
changeset
|
515 |
|
10249 | 516 |
|
60500 | 517 |
subsubsection \<open>Set of elements\<close> |
10249 | 518 |
|
60606 | 519 |
definition set_mset :: "'a multiset \<Rightarrow> 'a set" |
60607 | 520 |
where "set_mset M = {x. x \<in># M}" |
60495 | 521 |
|
522 |
lemma set_mset_empty [simp]: "set_mset {#} = {}" |
|
523 |
by (simp add: set_mset_def) |
|
524 |
||
525 |
lemma set_mset_single [simp]: "set_mset {#b#} = {b}" |
|
526 |
by (simp add: set_mset_def) |
|
527 |
||
528 |
lemma set_mset_union [simp]: "set_mset (M + N) = set_mset M \<union> set_mset N" |
|
529 |
by (auto simp add: set_mset_def) |
|
530 |
||
531 |
lemma set_mset_eq_empty_iff [simp]: "(set_mset M = {}) = (M = {#})" |
|
532 |
by (auto simp add: set_mset_def multiset_eq_iff) |
|
533 |
||
60607 | 534 |
lemma mem_set_mset_iff [simp]: "(x \<in> set_mset M) = (x \<in># M)" |
60495 | 535 |
by (auto simp add: set_mset_def) |
536 |
||
60607 | 537 |
lemma set_mset_filter [simp]: "set_mset {# x\<in>#M. P x #} = set_mset M \<inter> {x. P x}" |
60495 | 538 |
by (auto simp add: set_mset_def) |
539 |
||
540 |
lemma finite_set_mset [iff]: "finite (set_mset M)" |
|
541 |
using count [of M] by (simp add: multiset_def set_mset_def) |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
542 |
|
60607 | 543 |
lemma finite_Collect_mem [iff]: "finite {x. x \<in># M}" |
60495 | 544 |
unfolding set_mset_def[symmetric] by simp |
545 |
||
546 |
lemma set_mset_mono: "A \<le># B \<Longrightarrow> set_mset A \<subseteq> set_mset B" |
|
547 |
by (metis mset_leD subsetI mem_set_mset_iff) |
|
548 |
||
549 |
lemma ball_set_mset_iff: "(\<forall>x \<in> set_mset M. P x) \<longleftrightarrow> (\<forall>x. x \<in># M \<longrightarrow> P x)" |
|
59813 | 550 |
by auto |
551 |
||
552 |
||
60500 | 553 |
subsubsection \<open>Size\<close> |
10249 | 554 |
|
56656 | 555 |
definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))" |
556 |
||
557 |
lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a" |
|
558 |
by (auto simp: wcount_def add_mult_distrib) |
|
559 |
||
560 |
definition size_multiset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a multiset \<Rightarrow> nat" where |
|
60495 | 561 |
"size_multiset f M = setsum (wcount f M) (set_mset M)" |
56656 | 562 |
|
563 |
lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def] |
|
564 |
||
60606 | 565 |
instantiation multiset :: (type) size |
566 |
begin |
|
567 |
||
56656 | 568 |
definition size_multiset where |
569 |
size_multiset_overloaded_def: "size_multiset = Multiset.size_multiset (\<lambda>_. 0)" |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
570 |
instance .. |
60606 | 571 |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
572 |
end |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
573 |
|
56656 | 574 |
lemmas size_multiset_overloaded_eq = |
575 |
size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified] |
|
576 |
||
577 |
lemma size_multiset_empty [simp]: "size_multiset f {#} = 0" |
|
578 |
by (simp add: size_multiset_def) |
|
579 |
||
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
580 |
lemma size_empty [simp]: "size {#} = 0" |
56656 | 581 |
by (simp add: size_multiset_overloaded_def) |
582 |
||
583 |
lemma size_multiset_single [simp]: "size_multiset f {#b#} = Suc (f b)" |
|
584 |
by (simp add: size_multiset_eq) |
|
10249 | 585 |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
586 |
lemma size_single [simp]: "size {#b#} = 1" |
56656 | 587 |
by (simp add: size_multiset_overloaded_def) |
588 |
||
589 |
lemma setsum_wcount_Int: |
|
60495 | 590 |
"finite A \<Longrightarrow> setsum (wcount f N) (A \<inter> set_mset N) = setsum (wcount f N) A" |
26178 | 591 |
apply (induct rule: finite_induct) |
592 |
apply simp |
|
60495 | 593 |
apply (simp add: Int_insert_left set_mset_def wcount_def) |
56656 | 594 |
done |
595 |
||
596 |
lemma size_multiset_union [simp]: |
|
597 |
"size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N" |
|
57418 | 598 |
apply (simp add: size_multiset_def setsum_Un_nat setsum.distrib setsum_wcount_Int wcount_union) |
56656 | 599 |
apply (subst Int_commute) |
600 |
apply (simp add: setsum_wcount_Int) |
|
26178 | 601 |
done |
10249 | 602 |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
603 |
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N" |
56656 | 604 |
by (auto simp add: size_multiset_overloaded_def) |
605 |
||
606 |
lemma size_multiset_eq_0_iff_empty [iff]: "(size_multiset f M = 0) = (M = {#})" |
|
607 |
by (auto simp add: size_multiset_eq multiset_eq_iff) |
|
10249 | 608 |
|
17161 | 609 |
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})" |
56656 | 610 |
by (auto simp add: size_multiset_overloaded_def) |
26016 | 611 |
|
612 |
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)" |
|
26178 | 613 |
by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty) |
10249 | 614 |
|
60607 | 615 |
lemma size_eq_Suc_imp_elem: "size M = Suc n \<Longrightarrow> \<exists>a. a \<in># M" |
56656 | 616 |
apply (unfold size_multiset_overloaded_eq) |
26178 | 617 |
apply (drule setsum_SucD) |
618 |
apply auto |
|
619 |
done |
|
10249 | 620 |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
621 |
lemma size_eq_Suc_imp_eq_union: |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
622 |
assumes "size M = Suc n" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
623 |
shows "\<exists>a N. M = N + {#a#}" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
624 |
proof - |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
625 |
from assms obtain a where "a \<in># M" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
626 |
by (erule size_eq_Suc_imp_elem [THEN exE]) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
627 |
then have "M = M - {#a#} + {#a#}" by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
628 |
then show ?thesis by blast |
23611 | 629 |
qed |
15869 | 630 |
|
60606 | 631 |
lemma size_mset_mono: |
632 |
fixes A B :: "'a multiset" |
|
633 |
assumes "A \<le># B" |
|
634 |
shows "size A \<le> size B" |
|
59949 | 635 |
proof - |
636 |
from assms[unfolded mset_le_exists_conv] |
|
637 |
obtain C where B: "B = A + C" by auto |
|
60606 | 638 |
show ?thesis unfolding B by (induct C) auto |
59949 | 639 |
qed |
640 |
||
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
641 |
lemma size_filter_mset_lesseq[simp]: "size (filter_mset f M) \<le> size M" |
59949 | 642 |
by (rule size_mset_mono[OF multiset_filter_subset]) |
643 |
||
644 |
lemma size_Diff_submset: |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
645 |
"M \<le># M' \<Longrightarrow> size (M' - M) = size M' - size(M::'a multiset)" |
59949 | 646 |
by (metis add_diff_cancel_left' size_union mset_le_exists_conv) |
26016 | 647 |
|
60500 | 648 |
subsection \<open>Induction and case splits\<close> |
10249 | 649 |
|
18258 | 650 |
theorem multiset_induct [case_names empty add, induct type: multiset]: |
48009 | 651 |
assumes empty: "P {#}" |
652 |
assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})" |
|
653 |
shows "P M" |
|
654 |
proof (induct n \<equiv> "size M" arbitrary: M) |
|
655 |
case 0 thus "P M" by (simp add: empty) |
|
656 |
next |
|
657 |
case (Suc k) |
|
658 |
obtain N x where "M = N + {#x#}" |
|
60500 | 659 |
using \<open>Suc k = size M\<close> [symmetric] |
48009 | 660 |
using size_eq_Suc_imp_eq_union by fast |
661 |
with Suc add show "P M" by simp |
|
10249 | 662 |
qed |
663 |
||
25610 | 664 |
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}" |
26178 | 665 |
by (induct M) auto |
25610 | 666 |
|
55913 | 667 |
lemma multiset_cases [cases type]: |
668 |
obtains (empty) "M = {#}" |
|
669 |
| (add) N x where "M = N + {#x#}" |
|
670 |
using assms by (induct M) simp_all |
|
25610 | 671 |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
672 |
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
673 |
by (cases "B = {#}") (auto dest: multi_member_split) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
674 |
|
60607 | 675 |
lemma multiset_partition: "M = {# x\<in>#M. P x #} + {# x\<in>#M. \<not> P x #}" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
676 |
apply (subst multiset_eq_iff) |
26178 | 677 |
apply auto |
678 |
done |
|
10249 | 679 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
680 |
lemma mset_less_size: "(A::'a multiset) <# B \<Longrightarrow> size A < size B" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
681 |
proof (induct A arbitrary: B) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
682 |
case (empty M) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
683 |
then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty) |
58425 | 684 |
then obtain M' x where "M = M' + {#x#}" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
685 |
by (blast dest: multi_nonempty_split) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
686 |
then show ?case by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
687 |
next |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
688 |
case (add S x T) |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
689 |
have IH: "\<And>B. S <# B \<Longrightarrow> size S < size B" by fact |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
690 |
have SxsubT: "S + {#x#} <# T" by fact |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
691 |
then have "x \<in># T" and "S <# T" by (auto dest: mset_less_insertD) |
58425 | 692 |
then obtain T' where T: "T = T' + {#x#}" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
693 |
by (blast dest: multi_member_split) |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
694 |
then have "S <# T'" using SxsubT |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
695 |
by (blast intro: mset_less_add_bothsides) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
696 |
then have "size S < size T'" using IH by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
697 |
then show ?case using T by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
698 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
699 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
700 |
|
59949 | 701 |
lemma size_1_singleton_mset: "size M = 1 \<Longrightarrow> \<exists>a. M = {#a#}" |
702 |
by (cases M) auto |
|
703 |
||
60500 | 704 |
subsubsection \<open>Strong induction and subset induction for multisets\<close> |
705 |
||
706 |
text \<open>Well-foundedness of strict subset relation\<close> |
|
58098 | 707 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
708 |
lemma wf_less_mset_rel: "wf {(M, N :: 'a multiset). M <# N}" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
709 |
apply (rule wf_measure [THEN wf_subset, where f1=size]) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
710 |
apply (clarsimp simp: measure_def inv_image_def mset_less_size) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
711 |
done |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
712 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
713 |
lemma full_multiset_induct [case_names less]: |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
714 |
assumes ih: "\<And>B. \<forall>(A::'a multiset). A <# B \<longrightarrow> P A \<Longrightarrow> P B" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
715 |
shows "P B" |
58098 | 716 |
apply (rule wf_less_mset_rel [THEN wf_induct]) |
717 |
apply (rule ih, auto) |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
718 |
done |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
719 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
720 |
lemma multi_subset_induct [consumes 2, case_names empty add]: |
60606 | 721 |
assumes "F \<le># A" |
722 |
and empty: "P {#}" |
|
723 |
and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})" |
|
724 |
shows "P F" |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
725 |
proof - |
60500 | 726 |
from \<open>F \<le># A\<close> |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
727 |
show ?thesis |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
728 |
proof (induct F) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
729 |
show "P {#}" by fact |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
730 |
next |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
731 |
fix x F |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
732 |
assume P: "F \<le># A \<Longrightarrow> P F" and i: "F + {#x#} \<le># A" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
733 |
show "P (F + {#x#})" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
734 |
proof (rule insert) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
735 |
from i show "x \<in># A" by (auto dest: mset_le_insertD) |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
736 |
from i have "F \<le># A" by (auto dest: mset_le_insertD) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
737 |
with P show "P F" . |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
738 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
739 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
740 |
qed |
26145 | 741 |
|
17161 | 742 |
|
60500 | 743 |
subsection \<open>The fold combinator\<close> |
48023 | 744 |
|
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
745 |
definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b" |
48023 | 746 |
where |
60495 | 747 |
"fold_mset f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_mset M)" |
48023 | 748 |
|
60606 | 749 |
lemma fold_mset_empty [simp]: "fold_mset f s {#} = s" |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
750 |
by (simp add: fold_mset_def) |
48023 | 751 |
|
752 |
context comp_fun_commute |
|
753 |
begin |
|
754 |
||
60606 | 755 |
lemma fold_mset_insert: "fold_mset f s (M + {#x#}) = f x (fold_mset f s M)" |
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
756 |
proof - |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
757 |
interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y" |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
758 |
by (fact comp_fun_commute_funpow) |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
759 |
interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y" |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
760 |
by (fact comp_fun_commute_funpow) |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
761 |
show ?thesis |
60495 | 762 |
proof (cases "x \<in> set_mset M") |
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
763 |
case False |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
764 |
then have *: "count (M + {#x#}) x = 1" by simp |
60495 | 765 |
from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_mset M) = |
766 |
Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_mset M)" |
|
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
767 |
by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow) |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
768 |
with False * show ?thesis |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
769 |
by (simp add: fold_mset_def del: count_union) |
48023 | 770 |
next |
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
771 |
case True |
60495 | 772 |
def N \<equiv> "set_mset M - {x}" |
773 |
from N_def True have *: "set_mset M = insert x N" "x \<notin> N" "finite N" by auto |
|
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
774 |
then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N = |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
775 |
Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N" |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
776 |
by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow) |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
777 |
with * show ?thesis by (simp add: fold_mset_def del: count_union) simp |
48023 | 778 |
qed |
779 |
qed |
|
780 |
||
60606 | 781 |
corollary fold_mset_single [simp]: "fold_mset f s {#x#} = f x s" |
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
782 |
proof - |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
783 |
have "fold_mset f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp |
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
784 |
then show ?thesis by simp |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
785 |
qed |
48023 | 786 |
|
60606 | 787 |
lemma fold_mset_fun_left_comm: "f x (fold_mset f s M) = fold_mset f (f x s) M" |
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
788 |
by (induct M) (simp_all add: fold_mset_insert fun_left_comm) |
48023 | 789 |
|
60606 | 790 |
lemma fold_mset_union [simp]: "fold_mset f s (M + N) = fold_mset f (fold_mset f s M) N" |
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
791 |
proof (induct M) |
48023 | 792 |
case empty then show ?case by simp |
793 |
next |
|
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
794 |
case (add M x) |
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
795 |
have "M + {#x#} + N = (M + N) + {#x#}" |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
796 |
by (simp add: ac_simps) |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
797 |
with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm) |
48023 | 798 |
qed |
799 |
||
800 |
lemma fold_mset_fusion: |
|
801 |
assumes "comp_fun_commute g" |
|
60606 | 802 |
and *: "\<And>x y. h (g x y) = f x (h y)" |
803 |
shows "h (fold_mset g w A) = fold_mset f (h w) A" |
|
48023 | 804 |
proof - |
805 |
interpret comp_fun_commute g by (fact assms) |
|
60606 | 806 |
from * show ?thesis by (induct A) auto |
48023 | 807 |
qed |
808 |
||
809 |
end |
|
810 |
||
60500 | 811 |
text \<open> |
48023 | 812 |
A note on code generation: When defining some function containing a |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
813 |
subterm @{term "fold_mset F"}, code generation is not automatic. When |
61585 | 814 |
interpreting locale \<open>left_commutative\<close> with \<open>F\<close>, the |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
815 |
would be code thms for @{const fold_mset} become thms like |
61585 | 816 |
@{term "fold_mset F z {#} = z"} where \<open>F\<close> is not a pattern but |
48023 | 817 |
contains defined symbols, i.e.\ is not a code thm. Hence a separate |
61585 | 818 |
constant with its own code thms needs to be introduced for \<open>F\<close>. See the image operator below. |
60500 | 819 |
\<close> |
820 |
||
821 |
||
822 |
subsection \<open>Image\<close> |
|
48023 | 823 |
|
824 |
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where |
|
60607 | 825 |
"image_mset f = fold_mset (plus \<circ> single \<circ> f) {#}" |
826 |
||
827 |
lemma comp_fun_commute_mset_image: "comp_fun_commute (plus \<circ> single \<circ> f)" |
|
49823 | 828 |
proof |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
829 |
qed (simp add: ac_simps fun_eq_iff) |
48023 | 830 |
|
831 |
lemma image_mset_empty [simp]: "image_mset f {#} = {#}" |
|
49823 | 832 |
by (simp add: image_mset_def) |
48023 | 833 |
|
834 |
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}" |
|
49823 | 835 |
proof - |
60607 | 836 |
interpret comp_fun_commute "plus \<circ> single \<circ> f" |
49823 | 837 |
by (fact comp_fun_commute_mset_image) |
838 |
show ?thesis by (simp add: image_mset_def) |
|
839 |
qed |
|
48023 | 840 |
|
60606 | 841 |
lemma image_mset_union [simp]: "image_mset f (M + N) = image_mset f M + image_mset f N" |
49823 | 842 |
proof - |
60607 | 843 |
interpret comp_fun_commute "plus \<circ> single \<circ> f" |
49823 | 844 |
by (fact comp_fun_commute_mset_image) |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
845 |
show ?thesis by (induct N) (simp_all add: image_mset_def ac_simps) |
49823 | 846 |
qed |
847 |
||
60606 | 848 |
corollary image_mset_insert: "image_mset f (M + {#a#}) = image_mset f M + {#f a#}" |
49823 | 849 |
by simp |
48023 | 850 |
|
60606 | 851 |
lemma set_image_mset [simp]: "set_mset (image_mset f M) = image f (set_mset M)" |
49823 | 852 |
by (induct M) simp_all |
48040 | 853 |
|
60606 | 854 |
lemma size_image_mset [simp]: "size (image_mset f M) = size M" |
49823 | 855 |
by (induct M) simp_all |
48023 | 856 |
|
60606 | 857 |
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}" |
49823 | 858 |
by (cases M) auto |
48023 | 859 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
860 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
861 |
"_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset" ("({#_/. _ :# _#})") |
48023 | 862 |
syntax |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
863 |
"_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset" ("({#_/. _ \<in># _#})") |
59813 | 864 |
translations |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
865 |
"{#e. x \<in># M#}" \<rightleftharpoons> "CONST image_mset (\<lambda>x. e) M" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
866 |
|
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
867 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
868 |
"_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset" ("({#_/ | _ :# _./ _#})") |
48023 | 869 |
syntax |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
870 |
"_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset" ("({#_/ | _ \<in># _./ _#})") |
59813 | 871 |
translations |
60606 | 872 |
"{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}" |
59813 | 873 |
|
60500 | 874 |
text \<open> |
60607 | 875 |
This allows to write not just filters like @{term "{#x\<in>#M. x<c#}"} |
876 |
but also images like @{term "{#x+x. x\<in>#M #}"} and @{term [source] |
|
877 |
"{#x+x|x\<in>#M. x<c#}"}, where the latter is currently displayed as |
|
878 |
@{term "{#x+x|x\<in>#M. x<c#}"}. |
|
60500 | 879 |
\<close> |
48023 | 880 |
|
60495 | 881 |
lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M" |
60498 | 882 |
by (metis mem_set_mset_iff set_image_mset) |
59813 | 883 |
|
55467
a5c9002bc54d
renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
blanchet
parents:
55417
diff
changeset
|
884 |
functor image_mset: image_mset |
48023 | 885 |
proof - |
886 |
fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)" |
|
887 |
proof |
|
888 |
fix A |
|
889 |
show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A" |
|
890 |
by (induct A) simp_all |
|
891 |
qed |
|
892 |
show "image_mset id = id" |
|
893 |
proof |
|
894 |
fix A |
|
895 |
show "image_mset id A = id A" |
|
896 |
by (induct A) simp_all |
|
897 |
qed |
|
898 |
qed |
|
899 |
||
59813 | 900 |
declare |
901 |
image_mset.id [simp] |
|
902 |
image_mset.identity [simp] |
|
903 |
||
904 |
lemma image_mset_id[simp]: "image_mset id x = x" |
|
905 |
unfolding id_def by auto |
|
906 |
||
907 |
lemma image_mset_cong: "(\<And>x. x \<in># M \<Longrightarrow> f x = g x) \<Longrightarrow> {#f x. x \<in># M#} = {#g x. x \<in># M#}" |
|
908 |
by (induct M) auto |
|
909 |
||
910 |
lemma image_mset_cong_pair: |
|
911 |
"(\<forall>x y. (x, y) \<in># M \<longrightarrow> f x y = g x y) \<Longrightarrow> {#f x y. (x, y) \<in># M#} = {#g x y. (x, y) \<in># M#}" |
|
912 |
by (metis image_mset_cong split_cong) |
|
49717 | 913 |
|
48023 | 914 |
|
60500 | 915 |
subsection \<open>Further conversions\<close> |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
916 |
|
60515 | 917 |
primrec mset :: "'a list \<Rightarrow> 'a multiset" where |
918 |
"mset [] = {#}" | |
|
919 |
"mset (a # x) = mset x + {# a #}" |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
920 |
|
37107 | 921 |
lemma in_multiset_in_set: |
60515 | 922 |
"x \<in># mset xs \<longleftrightarrow> x \<in> set xs" |
37107 | 923 |
by (induct xs) simp_all |
924 |
||
60515 | 925 |
lemma count_mset: |
926 |
"count (mset xs) x = length (filter (\<lambda>y. x = y) xs)" |
|
37107 | 927 |
by (induct xs) simp_all |
928 |
||
60515 | 929 |
lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])" |
59813 | 930 |
by (induct x) auto |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
931 |
|
60515 | 932 |
lemma mset_zero_iff_right[simp]: "({#} = mset x) = (x = [])" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
933 |
by (induct x) auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
934 |
|
60515 | 935 |
lemma set_mset_mset[simp]: "set_mset (mset x) = set x" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
936 |
by (induct x) auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
937 |
|
60607 | 938 |
lemma mem_set_multiset_eq: "x \<in> set xs = (x \<in># mset xs)" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
939 |
by (induct xs) auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
940 |
|
60515 | 941 |
lemma size_mset [simp]: "size (mset xs) = length xs" |
48012 | 942 |
by (induct xs) simp_all |
943 |
||
60606 | 944 |
lemma mset_append [simp]: "mset (xs @ ys) = mset xs + mset ys" |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
945 |
by (induct xs arbitrary: ys) (auto simp: ac_simps) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
946 |
|
60607 | 947 |
lemma mset_filter: "mset (filter P xs) = {#x \<in># mset xs. P x #}" |
40303
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
948 |
by (induct xs) simp_all |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
949 |
|
60515 | 950 |
lemma mset_rev [simp]: |
951 |
"mset (rev xs) = mset xs" |
|
40950 | 952 |
by (induct xs) simp_all |
953 |
||
60515 | 954 |
lemma surj_mset: "surj mset" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
955 |
apply (unfold surj_def) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
956 |
apply (rule allI) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
957 |
apply (rule_tac M = y in multiset_induct) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
958 |
apply auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
959 |
apply (rule_tac x = "x # xa" in exI) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
960 |
apply auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
961 |
done |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
962 |
|
60515 | 963 |
lemma set_count_greater_0: "set x = {a. count (mset x) a > 0}" |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
964 |
by (induct x) auto |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
965 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
966 |
lemma distinct_count_atmost_1: |
60606 | 967 |
"distinct x = (\<forall>a. count (mset x) a = (if a \<in> set x then 1 else 0))" |
60678 | 968 |
apply (induct x, simp, rule iffI, simp_all) |
969 |
subgoal for a b |
|
970 |
apply (rule conjI) |
|
971 |
apply (simp_all add: set_mset_mset [symmetric] del: set_mset_mset) |
|
972 |
apply (erule_tac x = a in allE, simp) |
|
973 |
apply clarify |
|
974 |
apply (erule_tac x = aa in allE, simp) |
|
975 |
done |
|
976 |
done |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
977 |
|
60606 | 978 |
lemma mset_eq_setD: "mset xs = mset ys \<Longrightarrow> set xs = set ys" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
979 |
by (rule) (auto simp add:multiset_eq_iff set_count_greater_0) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
980 |
|
60515 | 981 |
lemma set_eq_iff_mset_eq_distinct: |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
982 |
"distinct x \<Longrightarrow> distinct y \<Longrightarrow> |
60515 | 983 |
(set x = set y) = (mset x = mset y)" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
984 |
by (auto simp: multiset_eq_iff distinct_count_atmost_1) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
985 |
|
60515 | 986 |
lemma set_eq_iff_mset_remdups_eq: |
987 |
"(set x = set y) = (mset (remdups x) = mset (remdups y))" |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
988 |
apply (rule iffI) |
60515 | 989 |
apply (simp add: set_eq_iff_mset_eq_distinct[THEN iffD1]) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
990 |
apply (drule distinct_remdups [THEN distinct_remdups |
60515 | 991 |
[THEN set_eq_iff_mset_eq_distinct [THEN iffD2]]]) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
992 |
apply simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
993 |
done |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
994 |
|
60606 | 995 |
lemma mset_compl_union [simp]: "mset [x\<leftarrow>xs. P x] + mset [x\<leftarrow>xs. \<not>P x] = mset xs" |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
996 |
by (induct xs) (auto simp: ac_simps) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
997 |
|
60607 | 998 |
lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) \<in># mset ls" |
60678 | 999 |
proof (induct ls arbitrary: i) |
1000 |
case Nil |
|
1001 |
then show ?case by simp |
|
1002 |
next |
|
1003 |
case Cons |
|
1004 |
then show ?case by (cases i) auto |
|
1005 |
qed |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1006 |
|
60606 | 1007 |
lemma mset_remove1[simp]: "mset (remove1 a xs) = mset xs - {#a#}" |
60678 | 1008 |
by (induct xs) (auto simp add: multiset_eq_iff) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1009 |
|
60515 | 1010 |
lemma mset_eq_length: |
1011 |
assumes "mset xs = mset ys" |
|
37107 | 1012 |
shows "length xs = length ys" |
60515 | 1013 |
using assms by (metis size_mset) |
1014 |
||
1015 |
lemma mset_eq_length_filter: |
|
1016 |
assumes "mset xs = mset ys" |
|
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1017 |
shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)" |
60515 | 1018 |
using assms by (metis count_mset) |
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1019 |
|
45989
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
haftmann
parents:
45866
diff
changeset
|
1020 |
lemma fold_multiset_equiv: |
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
haftmann
parents:
45866
diff
changeset
|
1021 |
assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" |
60515 | 1022 |
and equiv: "mset xs = mset ys" |
49822
0cfc1651be25
simplified construction of fold combinator on multisets;
haftmann
parents:
49717
diff
changeset
|
1023 |
shows "List.fold f xs = List.fold f ys" |
60606 | 1024 |
using f equiv [symmetric] |
46921 | 1025 |
proof (induct xs arbitrary: ys) |
60678 | 1026 |
case Nil |
1027 |
then show ?case by simp |
|
45989
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
haftmann
parents:
45866
diff
changeset
|
1028 |
next |
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
haftmann
parents:
45866
diff
changeset
|
1029 |
case (Cons x xs) |
60678 | 1030 |
then have *: "set ys = set (x # xs)" |
1031 |
by (blast dest: mset_eq_setD) |
|
58425 | 1032 |
have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" |
45989
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
haftmann
parents:
45866
diff
changeset
|
1033 |
by (rule Cons.prems(1)) (simp_all add: *) |
60678 | 1034 |
moreover from * have "x \<in> set ys" |
1035 |
by simp |
|
1036 |
ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" |
|
1037 |
by (fact fold_remove1_split) |
|
1038 |
moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" |
|
1039 |
by (auto intro: Cons.hyps) |
|
45989
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
haftmann
parents:
45866
diff
changeset
|
1040 |
ultimately show ?case by simp |
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
haftmann
parents:
45866
diff
changeset
|
1041 |
qed |
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
haftmann
parents:
45866
diff
changeset
|
1042 |
|
60606 | 1043 |
lemma mset_insort [simp]: "mset (insort x xs) = mset xs + {#x#}" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1044 |
by (induct xs) (simp_all add: ac_simps) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1045 |
|
60606 | 1046 |
lemma mset_map: "mset (map f xs) = image_mset f (mset xs)" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
1047 |
by (induct xs) simp_all |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
1048 |
|
61890
f6ded81f5690
abandoned attempt to unify sublocale and interpretation into global theories
haftmann
parents:
61832
diff
changeset
|
1049 |
global_interpretation mset_set: folding "\<lambda>x M. {#x#} + M" "{#}" |
61832 | 1050 |
defines mset_set = "folding.F (\<lambda>x M. {#x#} + M) {#}" |
1051 |
by standard (simp add: fun_eq_iff ac_simps) |
|
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1052 |
|
60513 | 1053 |
lemma count_mset_set [simp]: |
1054 |
"finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (mset_set A) x = 1" (is "PROP ?P") |
|
1055 |
"\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q") |
|
1056 |
"x \<notin> A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?R") |
|
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
1057 |
proof - |
60606 | 1058 |
have *: "count (mset_set A) x = 0" if "x \<notin> A" for A |
1059 |
proof (cases "finite A") |
|
1060 |
case False then show ?thesis by simp |
|
1061 |
next |
|
1062 |
case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto |
|
1063 |
qed |
|
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
1064 |
then show "PROP ?P" "PROP ?Q" "PROP ?R" |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
1065 |
by (auto elim!: Set.set_insert) |
61585 | 1066 |
qed \<comment> \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close> |
60513 | 1067 |
|
1068 |
lemma elem_mset_set[simp, intro]: "finite A \<Longrightarrow> x \<in># mset_set A \<longleftrightarrow> x \<in> A" |
|
59813 | 1069 |
by (induct A rule: finite_induct) simp_all |
1070 |
||
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1071 |
context linorder |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1072 |
begin |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1073 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1074 |
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list" |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1075 |
where |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
1076 |
"sorted_list_of_multiset M = fold_mset insort [] M" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1077 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1078 |
lemma sorted_list_of_multiset_empty [simp]: |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1079 |
"sorted_list_of_multiset {#} = []" |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1080 |
by (simp add: sorted_list_of_multiset_def) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1081 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1082 |
lemma sorted_list_of_multiset_singleton [simp]: |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1083 |
"sorted_list_of_multiset {#x#} = [x]" |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1084 |
proof - |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1085 |
interpret comp_fun_commute insort by (fact comp_fun_commute_insort) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1086 |
show ?thesis by (simp add: sorted_list_of_multiset_def) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1087 |
qed |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1088 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1089 |
lemma sorted_list_of_multiset_insert [simp]: |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1090 |
"sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)" |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1091 |
proof - |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1092 |
interpret comp_fun_commute insort by (fact comp_fun_commute_insort) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1093 |
show ?thesis by (simp add: sorted_list_of_multiset_def) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1094 |
qed |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1095 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1096 |
end |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1097 |
|
60515 | 1098 |
lemma mset_sorted_list_of_multiset [simp]: |
1099 |
"mset (sorted_list_of_multiset M) = M" |
|
60513 | 1100 |
by (induct M) simp_all |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1101 |
|
60515 | 1102 |
lemma sorted_list_of_multiset_mset [simp]: |
1103 |
"sorted_list_of_multiset (mset xs) = sort xs" |
|
60513 | 1104 |
by (induct xs) simp_all |
1105 |
||
1106 |
lemma finite_set_mset_mset_set[simp]: |
|
1107 |
"finite A \<Longrightarrow> set_mset (mset_set A) = A" |
|
1108 |
by (induct A rule: finite_induct) simp_all |
|
1109 |
||
1110 |
lemma infinite_set_mset_mset_set: |
|
1111 |
"\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}" |
|
1112 |
by simp |
|
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1113 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1114 |
lemma set_sorted_list_of_multiset [simp]: |
60495 | 1115 |
"set (sorted_list_of_multiset M) = set_mset M" |
60513 | 1116 |
by (induct M) (simp_all add: set_insort) |
1117 |
||
1118 |
lemma sorted_list_of_mset_set [simp]: |
|
1119 |
"sorted_list_of_multiset (mset_set A) = sorted_list_of_set A" |
|
1120 |
by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps) |
|
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1121 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1122 |
|
60804 | 1123 |
subsection \<open>Replicate operation\<close> |
1124 |
||
1125 |
definition replicate_mset :: "nat \<Rightarrow> 'a \<Rightarrow> 'a multiset" where |
|
1126 |
"replicate_mset n x = ((op + {#x#}) ^^ n) {#}" |
|
1127 |
||
1128 |
lemma replicate_mset_0[simp]: "replicate_mset 0 x = {#}" |
|
1129 |
unfolding replicate_mset_def by simp |
|
1130 |
||
1131 |
lemma replicate_mset_Suc[simp]: "replicate_mset (Suc n) x = replicate_mset n x + {#x#}" |
|
1132 |
unfolding replicate_mset_def by (induct n) (auto intro: add.commute) |
|
1133 |
||
1134 |
lemma in_replicate_mset[simp]: "x \<in># replicate_mset n y \<longleftrightarrow> n > 0 \<and> x = y" |
|
1135 |
unfolding replicate_mset_def by (induct n) simp_all |
|
1136 |
||
1137 |
lemma count_replicate_mset[simp]: "count (replicate_mset n x) y = (if y = x then n else 0)" |
|
1138 |
unfolding replicate_mset_def by (induct n) simp_all |
|
1139 |
||
1140 |
lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})" |
|
1141 |
by (auto split: if_splits) |
|
1142 |
||
1143 |
lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n" |
|
1144 |
by (induct n, simp_all) |
|
1145 |
||
1146 |
lemma count_le_replicate_mset_le: "n \<le> count M x \<longleftrightarrow> replicate_mset n x \<le># M" |
|
1147 |
by (auto simp add: assms mset_less_eqI) (metis count_replicate_mset subseteq_mset_def) |
|
1148 |
||
1149 |
lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x" |
|
1150 |
by (induct D) simp_all |
|
1151 |
||
61031
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1152 |
lemma replicate_count_mset_eq_filter_eq: |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1153 |
"replicate (count (mset xs) k) k = filter (HOL.eq k) xs" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1154 |
by (induct xs) auto |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1155 |
|
60804 | 1156 |
|
60500 | 1157 |
subsection \<open>Big operators\<close> |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1158 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1159 |
no_notation times (infixl "*" 70) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1160 |
no_notation Groups.one ("1") |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1161 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1162 |
locale comm_monoid_mset = comm_monoid |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1163 |
begin |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1164 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1165 |
definition F :: "'a multiset \<Rightarrow> 'a" |
60606 | 1166 |
where eq_fold: "F M = fold_mset f 1 M" |
1167 |
||
1168 |
lemma empty [simp]: "F {#} = 1" |
|
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1169 |
by (simp add: eq_fold) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1170 |
|
60678 | 1171 |
lemma singleton [simp]: "F {#x#} = x" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1172 |
proof - |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1173 |
interpret comp_fun_commute |
60678 | 1174 |
by standard (simp add: fun_eq_iff left_commute) |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1175 |
show ?thesis by (simp add: eq_fold) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1176 |
qed |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1177 |
|
60606 | 1178 |
lemma union [simp]: "F (M + N) = F M * F N" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1179 |
proof - |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1180 |
interpret comp_fun_commute f |
60678 | 1181 |
by standard (simp add: fun_eq_iff left_commute) |
1182 |
show ?thesis |
|
1183 |
by (induct N) (simp_all add: left_commute eq_fold) |
|
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1184 |
qed |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1185 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1186 |
end |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1187 |
|
61076 | 1188 |
lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)" |
60678 | 1189 |
by standard (simp add: add_ac comp_def) |
59813 | 1190 |
|
1191 |
declare comp_fun_commute.fold_mset_insert[OF comp_fun_commute_plus_mset, simp] |
|
1192 |
||
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
1193 |
lemma in_mset_fold_plus_iff[iff]: "x \<in># fold_mset (op +) M NN \<longleftrightarrow> x \<in># M \<or> (\<exists>N. N \<in># NN \<and> x \<in># N)" |
59813 | 1194 |
by (induct NN) auto |
1195 |
||
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1196 |
notation times (infixl "*" 70) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1197 |
notation Groups.one ("1") |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1198 |
|
54868 | 1199 |
context comm_monoid_add |
1200 |
begin |
|
1201 |
||
61605 | 1202 |
sublocale msetsum: comm_monoid_mset plus 0 |
61832 | 1203 |
defines msetsum = msetsum.F .. |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1204 |
|
60804 | 1205 |
lemma (in semiring_1) msetsum_replicate_mset [simp]: |
1206 |
"msetsum (replicate_mset n a) = of_nat n * a" |
|
1207 |
by (induct n) (simp_all add: algebra_simps) |
|
1208 |
||
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1209 |
lemma setsum_unfold_msetsum: |
60513 | 1210 |
"setsum f A = msetsum (image_mset f (mset_set A))" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1211 |
by (cases "finite A") (induct A rule: finite_induct, simp_all) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1212 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1213 |
end |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1214 |
|
59813 | 1215 |
lemma msetsum_diff: |
61076 | 1216 |
fixes M N :: "('a :: ordered_cancel_comm_monoid_diff) multiset" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
1217 |
shows "N \<le># M \<Longrightarrow> msetsum (M - N) = msetsum M - msetsum N" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
1218 |
by (metis add_diff_cancel_right' msetsum.union subset_mset.diff_add) |
59813 | 1219 |
|
59949 | 1220 |
lemma size_eq_msetsum: "size M = msetsum (image_mset (\<lambda>_. 1) M)" |
1221 |
proof (induct M) |
|
1222 |
case empty then show ?case by simp |
|
1223 |
next |
|
1224 |
case (add M x) then show ?case |
|
60495 | 1225 |
by (cases "x \<in> set_mset M") |
1226 |
(simp_all del: mem_set_mset_iff add: size_multiset_overloaded_eq setsum.distrib setsum.delta' insert_absorb, simp) |
|
59949 | 1227 |
qed |
1228 |
||
1229 |
||
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1230 |
abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset" ("\<Union>#_" [900] 900) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1231 |
where "\<Union># MM \<equiv> msetsum MM" |
59813 | 1232 |
|
60495 | 1233 |
lemma set_mset_Union_mset[simp]: "set_mset (\<Union># MM) = (\<Union>M \<in> set_mset MM. set_mset M)" |
59813 | 1234 |
by (induct MM) auto |
1235 |
||
1236 |
lemma in_Union_mset_iff[iff]: "x \<in># \<Union># MM \<longleftrightarrow> (\<exists>M. M \<in># MM \<and> x \<in># M)" |
|
1237 |
by (induct MM) auto |
|
1238 |
||
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1239 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1240 |
"_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" ("(3SUM _:#_. _)" [0, 51, 10] 10) |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1241 |
syntax |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1242 |
"_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10) |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1243 |
translations |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1244 |
"\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST msetsum (CONST image_mset (\<lambda>i. b) A)" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1245 |
|
54868 | 1246 |
context comm_monoid_mult |
1247 |
begin |
|
1248 |
||
61605 | 1249 |
sublocale msetprod: comm_monoid_mset times 1 |
61832 | 1250 |
defines msetprod = msetprod.F .. |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1251 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1252 |
lemma msetprod_empty: |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1253 |
"msetprod {#} = 1" |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1254 |
by (fact msetprod.empty) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1255 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1256 |
lemma msetprod_singleton: |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1257 |
"msetprod {#x#} = x" |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1258 |
by (fact msetprod.singleton) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1259 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1260 |
lemma msetprod_Un: |
58425 | 1261 |
"msetprod (A + B) = msetprod A * msetprod B" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1262 |
by (fact msetprod.union) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1263 |
|
60804 | 1264 |
lemma msetprod_replicate_mset [simp]: |
1265 |
"msetprod (replicate_mset n a) = a ^ n" |
|
1266 |
by (induct n) (simp_all add: ac_simps) |
|
1267 |
||
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1268 |
lemma setprod_unfold_msetprod: |
60513 | 1269 |
"setprod f A = msetprod (image_mset f (mset_set A))" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1270 |
by (cases "finite A") (induct A rule: finite_induct, simp_all) |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1271 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1272 |
lemma msetprod_multiplicity: |
60495 | 1273 |
"msetprod M = setprod (\<lambda>x. x ^ count M x) (set_mset M)" |
59998
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents:
59986
diff
changeset
|
1274 |
by (simp add: fold_mset_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def) |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1275 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1276 |
end |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1277 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1278 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1279 |
"_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" ("(3PROD _:#_. _)" [0, 51, 10] 10) |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1280 |
syntax |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1281 |
"_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10) |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1282 |
translations |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1283 |
"\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST msetprod (CONST image_mset (\<lambda>i. b) A)" |
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1284 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1285 |
lemma (in comm_semiring_1) dvd_msetprod: |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1286 |
assumes "x \<in># A" |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1287 |
shows "x dvd msetprod A" |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1288 |
proof - |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1289 |
from assms have "A = (A - {#x#}) + {#x#}" by simp |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1290 |
then obtain B where "A = B + {#x#}" .. |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1291 |
then show ?thesis by simp |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1292 |
qed |
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1293 |
|
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1294 |
|
60500 | 1295 |
subsection \<open>Alternative representations\<close> |
1296 |
||
1297 |
subsubsection \<open>Lists\<close> |
|
51548
757fa47af981
centralized various multiset operations in theory multiset;
haftmann
parents:
51161
diff
changeset
|
1298 |
|
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1299 |
context linorder |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1300 |
begin |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1301 |
|
60515 | 1302 |
lemma mset_insort [simp]: |
1303 |
"mset (insort_key k x xs) = {#x#} + mset xs" |
|
37107 | 1304 |
by (induct xs) (simp_all add: ac_simps) |
58425 | 1305 |
|
60515 | 1306 |
lemma mset_sort [simp]: |
1307 |
"mset (sort_key k xs) = mset xs" |
|
37107 | 1308 |
by (induct xs) (simp_all add: ac_simps) |
1309 |
||
60500 | 1310 |
text \<open> |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1311 |
This lemma shows which properties suffice to show that a function |
61585 | 1312 |
\<open>f\<close> with \<open>f xs = ys\<close> behaves like sort. |
60500 | 1313 |
\<close> |
37074 | 1314 |
|
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1315 |
lemma properties_for_sort_key: |
60515 | 1316 |
assumes "mset ys = mset xs" |
60606 | 1317 |
and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs" |
1318 |
and "sorted (map f ys)" |
|
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1319 |
shows "sort_key f xs = ys" |
60606 | 1320 |
using assms |
46921 | 1321 |
proof (induct xs arbitrary: ys) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1322 |
case Nil then show ?case by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1323 |
next |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1324 |
case (Cons x xs) |
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1325 |
from Cons.prems(2) have |
40305
41833242cc42
tuned lemma proposition of properties_for_sort_key
haftmann
parents:
40303
diff
changeset
|
1326 |
"\<forall>k \<in> set ys. filter (\<lambda>x. f k = f x) (remove1 x ys) = filter (\<lambda>x. f k = f x) xs" |
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1327 |
by (simp add: filter_remove1) |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1328 |
with Cons.prems have "sort_key f xs = remove1 x ys" |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1329 |
by (auto intro!: Cons.hyps simp add: sorted_map_remove1) |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1330 |
moreover from Cons.prems have "x \<in> set ys" |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1331 |
by (auto simp add: mem_set_multiset_eq intro!: ccontr) |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1332 |
ultimately show ?case using Cons.prems by (simp add: insort_key_remove1) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1333 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1334 |
|
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1335 |
lemma properties_for_sort: |
60515 | 1336 |
assumes multiset: "mset ys = mset xs" |
60606 | 1337 |
and "sorted ys" |
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1338 |
shows "sort xs = ys" |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1339 |
proof (rule properties_for_sort_key) |
60515 | 1340 |
from multiset show "mset ys = mset xs" . |
60500 | 1341 |
from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp |
60678 | 1342 |
from multiset have "length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" for k |
60515 | 1343 |
by (rule mset_eq_length_filter) |
60678 | 1344 |
then have "replicate (length (filter (\<lambda>y. k = y) ys)) k = |
1345 |
replicate (length (filter (\<lambda>x. k = x) xs)) k" for k |
|
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1346 |
by simp |
60678 | 1347 |
then show "k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs" for k |
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1348 |
by (simp add: replicate_length_filter) |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1349 |
qed |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1350 |
|
61031
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1351 |
lemma sort_key_inj_key_eq: |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1352 |
assumes mset_equal: "mset xs = mset ys" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1353 |
and "inj_on f (set xs)" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1354 |
and "sorted (map f ys)" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1355 |
shows "sort_key f xs = ys" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1356 |
proof (rule properties_for_sort_key) |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1357 |
from mset_equal |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1358 |
show "mset ys = mset xs" by simp |
61188 | 1359 |
from \<open>sorted (map f ys)\<close> |
61031
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1360 |
show "sorted (map f ys)" . |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1361 |
show "[x\<leftarrow>ys . f k = f x] = [x\<leftarrow>xs . f k = f x]" if "k \<in> set ys" for k |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1362 |
proof - |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1363 |
from mset_equal |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1364 |
have set_equal: "set xs = set ys" by (rule mset_eq_setD) |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1365 |
with that have "insert k (set ys) = set ys" by auto |
61188 | 1366 |
with \<open>inj_on f (set xs)\<close> have inj: "inj_on f (insert k (set ys))" |
61031
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1367 |
by (simp add: set_equal) |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1368 |
from inj have "[x\<leftarrow>ys . f k = f x] = filter (HOL.eq k) ys" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1369 |
by (auto intro!: inj_on_filter_key_eq) |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1370 |
also have "\<dots> = replicate (count (mset ys) k) k" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1371 |
by (simp add: replicate_count_mset_eq_filter_eq) |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1372 |
also have "\<dots> = replicate (count (mset xs) k) k" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1373 |
using mset_equal by simp |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1374 |
also have "\<dots> = filter (HOL.eq k) xs" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1375 |
by (simp add: replicate_count_mset_eq_filter_eq) |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1376 |
also have "\<dots> = [x\<leftarrow>xs . f k = f x]" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1377 |
using inj by (auto intro!: inj_on_filter_key_eq [symmetric] simp add: set_equal) |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1378 |
finally show ?thesis . |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1379 |
qed |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1380 |
qed |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1381 |
|
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1382 |
lemma sort_key_eq_sort_key: |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1383 |
assumes "mset xs = mset ys" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1384 |
and "inj_on f (set xs)" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1385 |
shows "sort_key f xs = sort_key f ys" |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1386 |
by (rule sort_key_inj_key_eq) (simp_all add: assms) |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60804
diff
changeset
|
1387 |
|
40303
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1388 |
lemma sort_key_by_quicksort: |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1389 |
"sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))] |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1390 |
@ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))] |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1391 |
@ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs") |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1392 |
proof (rule properties_for_sort_key) |
60515 | 1393 |
show "mset ?rhs = mset ?lhs" |
1394 |
by (rule multiset_eqI) (auto simp add: mset_filter) |
|
40303
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1395 |
show "sorted (map f ?rhs)" |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1396 |
by (auto simp add: sorted_append intro: sorted_map_same) |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1397 |
next |
40305
41833242cc42
tuned lemma proposition of properties_for_sort_key
haftmann
parents:
40303
diff
changeset
|
1398 |
fix l |
41833242cc42
tuned lemma proposition of properties_for_sort_key
haftmann
parents:
40303
diff
changeset
|
1399 |
assume "l \<in> set ?rhs" |
40346 | 1400 |
let ?pivot = "f (xs ! (length xs div 2))" |
1401 |
have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto |
|
40306 | 1402 |
have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]" |
40305
41833242cc42
tuned lemma proposition of properties_for_sort_key
haftmann
parents:
40303
diff
changeset
|
1403 |
unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same) |
40346 | 1404 |
with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp |
1405 |
have "\<And>x P. P (f x) ?pivot \<and> f l = f x \<longleftrightarrow> P (f l) ?pivot \<and> f l = f x" by auto |
|
1406 |
then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] = |
|
1407 |
[x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp |
|
1408 |
note *** = this [of "op <"] this [of "op >"] this [of "op ="] |
|
40306 | 1409 |
show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]" |
40305
41833242cc42
tuned lemma proposition of properties_for_sort_key
haftmann
parents:
40303
diff
changeset
|
1410 |
proof (cases "f l" ?pivot rule: linorder_cases) |
46730 | 1411 |
case less |
1412 |
then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto |
|
1413 |
with less show ?thesis |
|
40346 | 1414 |
by (simp add: filter_sort [symmetric] ** ***) |
40305
41833242cc42
tuned lemma proposition of properties_for_sort_key
haftmann
parents:
40303
diff
changeset
|
1415 |
next |
40306 | 1416 |
case equal then show ?thesis |
40346 | 1417 |
by (simp add: * less_le) |
40305
41833242cc42
tuned lemma proposition of properties_for_sort_key
haftmann
parents:
40303
diff
changeset
|
1418 |
next |
46730 | 1419 |
case greater |
1420 |
then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto |
|
1421 |
with greater show ?thesis |
|
40346 | 1422 |
by (simp add: filter_sort [symmetric] ** ***) |
40306 | 1423 |
qed |
40303
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1424 |
qed |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1425 |
|
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1426 |
lemma sort_by_quicksort: |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1427 |
"sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)] |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1428 |
@ [x\<leftarrow>xs. x = xs ! (length xs div 2)] |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1429 |
@ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs") |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1430 |
using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp |
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
haftmann
parents:
40250
diff
changeset
|
1431 |
|
60500 | 1432 |
text \<open>A stable parametrized quicksort\<close> |
40347
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1433 |
|
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1434 |
definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1435 |
"part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])" |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1436 |
|
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1437 |
lemma part_code [code]: |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1438 |
"part f pivot [] = ([], [], [])" |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1439 |
"part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1440 |
if x' < pivot then (x # lts, eqs, gts) |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1441 |
else if x' > pivot then (lts, eqs, x # gts) |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1442 |
else (lts, x # eqs, gts))" |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1443 |
by (auto simp add: part_def Let_def split_def) |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1444 |
|
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1445 |
lemma sort_key_by_quicksort_code [code]: |
60606 | 1446 |
"sort_key f xs = |
1447 |
(case xs of |
|
1448 |
[] \<Rightarrow> [] |
|
40347
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1449 |
| [x] \<Rightarrow> xs |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1450 |
| [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x]) |
60606 | 1451 |
| _ \<Rightarrow> |
1452 |
let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs |
|
1453 |
in sort_key f lts @ eqs @ sort_key f gts)" |
|
40347
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1454 |
proof (cases xs) |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1455 |
case Nil then show ?thesis by simp |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1456 |
next |
46921 | 1457 |
case (Cons _ ys) note hyps = Cons show ?thesis |
1458 |
proof (cases ys) |
|
40347
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1459 |
case Nil with hyps show ?thesis by simp |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1460 |
next |
46921 | 1461 |
case (Cons _ zs) note hyps = hyps Cons show ?thesis |
1462 |
proof (cases zs) |
|
40347
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1463 |
case Nil with hyps show ?thesis by auto |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1464 |
next |
58425 | 1465 |
case Cons |
40347
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1466 |
from sort_key_by_quicksort [of f xs] |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1467 |
have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1468 |
in sort_key f lts @ eqs @ sort_key f gts)" |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1469 |
by (simp only: split_def Let_def part_def fst_conv snd_conv) |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1470 |
with hyps Cons show ?thesis by (simp only: list.cases) |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1471 |
qed |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1472 |
qed |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1473 |
qed |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1474 |
|
39533
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1475 |
end |
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
haftmann
parents:
39314
diff
changeset
|
1476 |
|
40347
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1477 |
hide_const (open) part |
429bf4388b2f
added code lemmas for stable parametrized quicksort
haftmann
parents:
40346
diff
changeset
|
1478 |
|
60515 | 1479 |
lemma mset_remdups_le: "mset (remdups xs) \<le># mset xs" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
1480 |
by (induct xs) (auto intro: subset_mset.order_trans) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1481 |
|
60515 | 1482 |
lemma mset_update: |
1483 |
"i < length ls \<Longrightarrow> mset (ls[i := v]) = mset ls - {#ls ! i#} + {#v#}" |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1484 |
proof (induct ls arbitrary: i) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1485 |
case Nil then show ?case by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1486 |
next |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1487 |
case (Cons x xs) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1488 |
show ?case |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1489 |
proof (cases i) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1490 |
case 0 then show ?thesis by simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1491 |
next |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1492 |
case (Suc i') |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1493 |
with Cons show ?thesis |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1494 |
apply simp |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1495 |
apply (subst add.assoc) |
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1496 |
apply (subst add.commute [of "{#v#}" "{#x#}"]) |
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1497 |
apply (subst add.assoc [symmetric]) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1498 |
apply simp |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1499 |
apply (rule mset_le_multiset_union_diff_commute) |
60515 | 1500 |
apply (simp add: mset_le_single nth_mem_mset) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1501 |
done |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1502 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1503 |
qed |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1504 |
|
60515 | 1505 |
lemma mset_swap: |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1506 |
"i < length ls \<Longrightarrow> j < length ls \<Longrightarrow> |
60515 | 1507 |
mset (ls[j := ls ! i, i := ls ! j]) = mset ls" |
1508 |
by (cases "i = j") (simp_all add: mset_update nth_mem_mset) |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1509 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1510 |
|
60500 | 1511 |
subsection \<open>The multiset order\<close> |
1512 |
||
1513 |
subsubsection \<open>Well-foundedness\<close> |
|
10249 | 1514 |
|
60606 | 1515 |
definition mult1 :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where |
37765 | 1516 |
"mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> |
60607 | 1517 |
(\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r)}" |
60606 | 1518 |
|
1519 |
definition mult :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where |
|
37765 | 1520 |
"mult r = (mult1 r)\<^sup>+" |
10249 | 1521 |
|
23751 | 1522 |
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r" |
26178 | 1523 |
by (simp add: mult1_def) |
10249 | 1524 |
|
60608 | 1525 |
lemma less_add: |
1526 |
assumes mult1: "(N, M0 + {#a#}) \<in> mult1 r" |
|
1527 |
shows |
|
1528 |
"(\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or> |
|
1529 |
(\<exists>K. (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r) \<and> N = M0 + K)" |
|
1530 |
proof - |
|
60607 | 1531 |
let ?r = "\<lambda>K a. \<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" |
11464 | 1532 |
let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a" |
60608 | 1533 |
obtain a' M0' K where M0: "M0 + {#a#} = M0' + {#a'#}" |
1534 |
and N: "N = M0' + K" |
|
1535 |
and r: "?r K a'" |
|
1536 |
using mult1 unfolding mult1_def by auto |
|
1537 |
show ?thesis (is "?case1 \<or> ?case2") |
|
60606 | 1538 |
proof - |
1539 |
from M0 consider "M0 = M0'" "a = a'" |
|
1540 |
| K' where "M0 = K' + {#a'#}" "M0' = K' + {#a#}" |
|
1541 |
by atomize_elim (simp only: add_eq_conv_ex) |
|
18258 | 1542 |
then show ?thesis |
60606 | 1543 |
proof cases |
1544 |
case 1 |
|
11464 | 1545 |
with N r have "?r K a \<and> N = M0 + K" by simp |
60606 | 1546 |
then have ?case2 .. |
1547 |
then show ?thesis .. |
|
10249 | 1548 |
next |
60606 | 1549 |
case 2 |
1550 |
from N 2(2) have n: "N = K' + K + {#a#}" by (simp add: ac_simps) |
|
1551 |
with r 2(1) have "?R (K' + K) M0" by blast |
|
60608 | 1552 |
with n have ?case1 by (simp add: mult1_def) |
60606 | 1553 |
then show ?thesis .. |
10249 | 1554 |
qed |
1555 |
qed |
|
1556 |
qed |
|
1557 |
||
60608 | 1558 |
lemma all_accessible: |
1559 |
assumes "wf r" |
|
1560 |
shows "\<forall>M. M \<in> Wellfounded.acc (mult1 r)" |
|
10249 | 1561 |
proof |
1562 |
let ?R = "mult1 r" |
|
54295 | 1563 |
let ?W = "Wellfounded.acc ?R" |
10249 | 1564 |
{ |
1565 |
fix M M0 a |
|
23751 | 1566 |
assume M0: "M0 \<in> ?W" |
60606 | 1567 |
and wf_hyp: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)" |
1568 |
and acc_hyp: "\<forall>M. (M, M0) \<in> ?R \<longrightarrow> M + {#a#} \<in> ?W" |
|
23751 | 1569 |
have "M0 + {#a#} \<in> ?W" |
1570 |
proof (rule accI [of "M0 + {#a#}"]) |
|
10249 | 1571 |
fix N |
23751 | 1572 |
assume "(N, M0 + {#a#}) \<in> ?R" |
60608 | 1573 |
then consider M where "(M, M0) \<in> ?R" "N = M + {#a#}" |
1574 |
| K where "\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" "N = M0 + K" |
|
1575 |
by atomize_elim (rule less_add) |
|
23751 | 1576 |
then show "N \<in> ?W" |
60608 | 1577 |
proof cases |
1578 |
case 1 |
|
60606 | 1579 |
from acc_hyp have "(M, M0) \<in> ?R \<longrightarrow> M + {#a#} \<in> ?W" .. |
60500 | 1580 |
from this and \<open>(M, M0) \<in> ?R\<close> have "M + {#a#} \<in> ?W" .. |
60608 | 1581 |
then show "N \<in> ?W" by (simp only: \<open>N = M + {#a#}\<close>) |
10249 | 1582 |
next |
60608 | 1583 |
case 2 |
1584 |
from this(1) have "M0 + K \<in> ?W" |
|
10249 | 1585 |
proof (induct K) |
18730 | 1586 |
case empty |
23751 | 1587 |
from M0 show "M0 + {#} \<in> ?W" by simp |
18730 | 1588 |
next |
1589 |
case (add K x) |
|
23751 | 1590 |
from add.prems have "(x, a) \<in> r" by simp |
1591 |
with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast |
|
1592 |
moreover from add have "M0 + K \<in> ?W" by simp |
|
1593 |
ultimately have "(M0 + K) + {#x#} \<in> ?W" .. |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1594 |
then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add.assoc) |
10249 | 1595 |
qed |
60608 | 1596 |
then show "N \<in> ?W" by (simp only: 2(2)) |
10249 | 1597 |
qed |
1598 |
qed |
|
1599 |
} note tedious_reasoning = this |
|
1600 |
||
60608 | 1601 |
show "M \<in> ?W" for M |
10249 | 1602 |
proof (induct M) |
23751 | 1603 |
show "{#} \<in> ?W" |
10249 | 1604 |
proof (rule accI) |
23751 | 1605 |
fix b assume "(b, {#}) \<in> ?R" |
1606 |
with not_less_empty show "b \<in> ?W" by contradiction |
|
10249 | 1607 |
qed |
1608 |
||
23751 | 1609 |
fix M a assume "M \<in> ?W" |
60608 | 1610 |
from \<open>wf r\<close> have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W" |
10249 | 1611 |
proof induct |
1612 |
fix a |
|
60606 | 1613 |
assume r: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)" |
23751 | 1614 |
show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W" |
10249 | 1615 |
proof |
23751 | 1616 |
fix M assume "M \<in> ?W" |
1617 |
then show "M + {#a#} \<in> ?W" |
|
23373 | 1618 |
by (rule acc_induct) (rule tedious_reasoning [OF _ r]) |
10249 | 1619 |
qed |
1620 |
qed |
|
60500 | 1621 |
from this and \<open>M \<in> ?W\<close> show "M + {#a#} \<in> ?W" .. |
10249 | 1622 |
qed |
1623 |
qed |
|
1624 |
||
60606 | 1625 |
theorem wf_mult1: "wf r \<Longrightarrow> wf (mult1 r)" |
26178 | 1626 |
by (rule acc_wfI) (rule all_accessible) |
10249 | 1627 |
|
60606 | 1628 |
theorem wf_mult: "wf r \<Longrightarrow> wf (mult r)" |
26178 | 1629 |
unfolding mult_def by (rule wf_trancl) (rule wf_mult1) |
10249 | 1630 |
|
1631 |
||
60500 | 1632 |
subsubsection \<open>Closure-free presentation\<close> |
1633 |
||
1634 |
text \<open>One direction.\<close> |
|
10249 | 1635 |
|
1636 |
lemma mult_implies_one_step: |
|
60606 | 1637 |
"trans r \<Longrightarrow> (M, N) \<in> mult r \<Longrightarrow> |
11464 | 1638 |
\<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and> |
60495 | 1639 |
(\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r)" |
1640 |
apply (unfold mult_def mult1_def set_mset_def) |
|
26178 | 1641 |
apply (erule converse_trancl_induct, clarify) |
1642 |
apply (rule_tac x = M0 in exI, simp, clarify) |
|
60607 | 1643 |
apply (case_tac "a \<in># K") |
26178 | 1644 |
apply (rule_tac x = I in exI) |
1645 |
apply (simp (no_asm)) |
|
1646 |
apply (rule_tac x = "(K - {#a#}) + Ka" in exI) |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1647 |
apply (simp (no_asm_simp) add: add.assoc [symmetric]) |
59807 | 1648 |
apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong) |
26178 | 1649 |
apply (simp add: diff_union_single_conv) |
1650 |
apply (simp (no_asm_use) add: trans_def) |
|
1651 |
apply blast |
|
60607 | 1652 |
apply (subgoal_tac "a \<in># I") |
26178 | 1653 |
apply (rule_tac x = "I - {#a#}" in exI) |
1654 |
apply (rule_tac x = "J + {#a#}" in exI) |
|
1655 |
apply (rule_tac x = "K + Ka" in exI) |
|
1656 |
apply (rule conjI) |
|
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
1657 |
apply (simp add: multiset_eq_iff split: nat_diff_split) |
26178 | 1658 |
apply (rule conjI) |
59807 | 1659 |
apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong, simp) |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
1660 |
apply (simp add: multiset_eq_iff split: nat_diff_split) |
26178 | 1661 |
apply (simp (no_asm_use) add: trans_def) |
1662 |
apply blast |
|
60607 | 1663 |
apply (subgoal_tac "a \<in># (M0 + {#a#})") |
26178 | 1664 |
apply simp |
1665 |
apply (simp (no_asm)) |
|
1666 |
done |
|
10249 | 1667 |
|
1668 |
lemma one_step_implies_mult_aux: |
|
60678 | 1669 |
"\<forall>I J K. size J = n \<and> J \<noteq> {#} \<and> (\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r) |
1670 |
\<longrightarrow> (I + K, I + J) \<in> mult r" |
|
1671 |
apply (induct n) |
|
1672 |
apply auto |
|
26178 | 1673 |
apply (frule size_eq_Suc_imp_eq_union, clarify) |
1674 |
apply (rename_tac "J'", simp) |
|
1675 |
apply (erule notE, auto) |
|
1676 |
apply (case_tac "J' = {#}") |
|
1677 |
apply (simp add: mult_def) |
|
1678 |
apply (rule r_into_trancl) |
|
60495 | 1679 |
apply (simp add: mult1_def set_mset_def, blast) |
60500 | 1680 |
txt \<open>Now we know @{term "J' \<noteq> {#}"}.\<close> |
26178 | 1681 |
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition) |
60495 | 1682 |
apply (erule_tac P = "\<forall>k \<in> set_mset K. P k" for P in rev_mp) |
26178 | 1683 |
apply (erule ssubst) |
1684 |
apply (simp add: Ball_def, auto) |
|
1685 |
apply (subgoal_tac |
|
60607 | 1686 |
"((I + {# x \<in># K. (x, a) \<in> r #}) + {# x \<in># K. (x, a) \<notin> r #}, |
1687 |
(I + {# x \<in># K. (x, a) \<in> r #}) + J') \<in> mult r") |
|
26178 | 1688 |
prefer 2 |
1689 |
apply force |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1690 |
apply (simp (no_asm_use) add: add.assoc [symmetric] mult_def) |
26178 | 1691 |
apply (erule trancl_trans) |
1692 |
apply (rule r_into_trancl) |
|
60495 | 1693 |
apply (simp add: mult1_def set_mset_def) |
26178 | 1694 |
apply (rule_tac x = a in exI) |
1695 |
apply (rule_tac x = "I + J'" in exI) |
|
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
1696 |
apply (simp add: ac_simps) |
26178 | 1697 |
done |
10249 | 1698 |
|
17161 | 1699 |
lemma one_step_implies_mult: |
60606 | 1700 |
"trans r \<Longrightarrow> J \<noteq> {#} \<Longrightarrow> \<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r |
1701 |
\<Longrightarrow> (I + K, I + J) \<in> mult r" |
|
26178 | 1702 |
using one_step_implies_mult_aux by blast |
10249 | 1703 |
|
1704 |
||
60500 | 1705 |
subsubsection \<open>Partial-order properties\<close> |
10249 | 1706 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1707 |
definition less_multiset :: "'a::order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "#\<subset>#" 50) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1708 |
where "M' #\<subset># M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1709 |
|
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1710 |
definition le_multiset :: "'a::order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "#\<subseteq>#" 50) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1711 |
where "M' #\<subseteq># M \<longleftrightarrow> M' #\<subset># M \<or> M' = M" |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1712 |
|
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1713 |
notation (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1714 |
less_multiset (infix "#<#" 50) and |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61890
diff
changeset
|
1715 |
le_multiset (infix "#<=#" 50) |
10249 | 1716 |
|
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1717 |
interpretation multiset_order: order le_multiset less_multiset |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1718 |
proof - |
60606 | 1719 |
have irrefl: "\<not> M #\<subset># M" for M :: "'a multiset" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1720 |
proof |
59958
4538d41e8e54
renamed multiset ordering to free up nice <# etc. symbols for the standard subset
blanchet
parents:
59949
diff
changeset
|
1721 |
assume "M #\<subset># M" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1722 |
then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def) |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1723 |
have "trans {(x'::'a, x). x' < x}" |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1724 |
by (rule transI) simp |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1725 |
moreover note MM |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1726 |
ultimately have "\<exists>I J K. M = I + J \<and> M = I + K |
60495 | 1727 |
\<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1728 |
by (rule mult_implies_one_step) |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1729 |
then obtain I J K where "M = I + J" and "M = I + K" |
60495 | 1730 |
and "J \<noteq> {#}" and "(\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})" by blast |
60678 | 1731 |
then have *: "K \<noteq> {#}" and **: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. k < j" by auto |
60495 | 1732 |
have "finite (set_mset K)" by simp |
60678 | 1733 |
moreover note ** |
60495 | 1734 |
ultimately have "set_mset K = {}" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1735 |
by (induct rule: finite_induct) (auto intro: order_less_trans) |
60678 | 1736 |
with * show False by simp |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1737 |
qed |
60678 | 1738 |
have trans: "K #\<subset># M \<Longrightarrow> M #\<subset># N \<Longrightarrow> K #\<subset># N" for K M N :: "'a multiset" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1739 |
unfolding less_multiset_def mult_def by (blast intro: trancl_trans) |
46921 | 1740 |
show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset" |
60678 | 1741 |
by standard (auto simp add: le_multiset_def irrefl dest: trans) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1742 |
qed |
10249 | 1743 |
|
60678 | 1744 |
lemma mult_less_irrefl [elim!]: |
1745 |
fixes M :: "'a::order multiset" |
|
1746 |
shows "M #\<subset># M \<Longrightarrow> R" |
|
46730 | 1747 |
by simp |
26567
7bcebb8c2d33
instantiation replacing primitive instance plus overloaded defs; more conservative type arities
haftmann
parents:
26178
diff
changeset
|
1748 |
|
10249 | 1749 |
|
60500 | 1750 |
subsubsection \<open>Monotonicity of multiset union\<close> |
10249 | 1751 |
|
60606 | 1752 |
lemma mult1_union: "(B, D) \<in> mult1 r \<Longrightarrow> (C + B, C + D) \<in> mult1 r" |
26178 | 1753 |
apply (unfold mult1_def) |
1754 |
apply auto |
|
1755 |
apply (rule_tac x = a in exI) |
|
1756 |
apply (rule_tac x = "C + M0" in exI) |
|
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1757 |
apply (simp add: add.assoc) |
26178 | 1758 |
done |
10249 | 1759 |
|
60606 | 1760 |
lemma union_less_mono2: "B #\<subset># D \<Longrightarrow> C + B #\<subset># C + (D::'a::order multiset)" |
26178 | 1761 |
apply (unfold less_multiset_def mult_def) |
1762 |
apply (erule trancl_induct) |
|
40249
cd404ecb9107
Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents:
39533
diff
changeset
|
1763 |
apply (blast intro: mult1_union) |
cd404ecb9107
Remove unnecessary premise of mult1_union
Lars Noschinski <noschinl@in.tum.de>
parents:
39533
diff
changeset
|
1764 |
apply (blast intro: mult1_union trancl_trans) |
26178 | 1765 |
done |
10249 | 1766 |
|
60606 | 1767 |
lemma union_less_mono1: "B #\<subset># D \<Longrightarrow> B + C #\<subset># D + (C::'a::order multiset)" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1768 |
apply (subst add.commute [of B C]) |
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1769 |
apply (subst add.commute [of D C]) |
26178 | 1770 |
apply (erule union_less_mono2) |
1771 |
done |
|
10249 | 1772 |
|
17161 | 1773 |
lemma union_less_mono: |
60606 | 1774 |
fixes A B C D :: "'a::order multiset" |
1775 |
shows "A #\<subset># C \<Longrightarrow> B #\<subset># D \<Longrightarrow> A + B #\<subset># C + D" |
|
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1776 |
by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans) |
10249 | 1777 |
|
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1778 |
interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset |
60678 | 1779 |
by standard (auto simp add: le_multiset_def intro: union_less_mono2) |
26145 | 1780 |
|
15072 | 1781 |
|
60500 | 1782 |
subsubsection \<open>Termination proofs with multiset orders\<close> |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1783 |
|
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1784 |
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
|
1785 |
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
|
1786 |
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
|
1787 |
by auto |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1788 |
|
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1789 |
definition "ms_strict = mult pair_less" |
37765 | 1790 |
definition "ms_weak = ms_strict \<union> Id" |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1791 |
|
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1792 |
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
|
1793 |
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
|
1794 |
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
|
1795 |
|
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1796 |
lemma smsI: |
60495 | 1797 |
"(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict" |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1798 |
unfolding ms_strict_def |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1799 |
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
|
1800 |
|
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1801 |
lemma wmsI: |
60495 | 1802 |
"(set_mset A, set_mset B) \<in> max_strict \<or> A = {#} \<and> B = {#} |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1803 |
\<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
|
1804 |
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
|
1805 |
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
|
1806 |
|
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1807 |
inductive pw_leq |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1808 |
where |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1809 |
pw_leq_empty: "pw_leq {#} {#}" |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1810 |
| 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
|
1811 |
|
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1812 |
lemma pw_leq_lstep: |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1813 |
"(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
|
1814 |
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
|
1815 |
|
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1816 |
lemma pw_leq_split: |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1817 |
assumes "pw_leq X Y" |
60495 | 1818 |
shows "\<exists>A B Z. X = A + Z \<and> Y = B + Z \<and> ((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))" |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1819 |
using assms |
60606 | 1820 |
proof induct |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1821 |
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
|
1822 |
next |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1823 |
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
|
1824 |
then obtain A B Z where |
58425 | 1825 |
[simp]: "X = A + Z" "Y = B + Z" |
60495 | 1826 |
and 1[simp]: "(set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#})" |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1827 |
by auto |
60606 | 1828 |
from pw_leq_step consider "x = y" | "(x, y) \<in> pair_less" |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1829 |
unfolding pair_leq_def by auto |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1830 |
thus ?case |
60606 | 1831 |
proof cases |
1832 |
case [simp]: 1 |
|
1833 |
have "{#x#} + X = A + ({#y#}+Z) \<and> {#y#} + Y = B + ({#y#}+Z) \<and> |
|
1834 |
((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))" |
|
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
1835 |
by (auto simp: ac_simps) |
60606 | 1836 |
thus ?thesis by blast |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1837 |
next |
60606 | 1838 |
case 2 |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1839 |
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
|
1840 |
have "{#x#} + X = ?A' + Z" |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1841 |
"{#y#} + Y = ?B' + Z" |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
1842 |
by (auto simp add: ac_simps) |
58425 | 1843 |
moreover have |
60495 | 1844 |
"(set_mset ?A', set_mset ?B') \<in> max_strict" |
60606 | 1845 |
using 1 2 unfolding max_strict_def |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1846 |
by (auto elim!: max_ext.cases) |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1847 |
ultimately show ?thesis by blast |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1848 |
qed |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1849 |
qed |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1850 |
|
58425 | 1851 |
lemma |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1852 |
assumes pwleq: "pw_leq Z Z'" |
60495 | 1853 |
shows ms_strictI: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict" |
60606 | 1854 |
and ms_weakI1: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak" |
1855 |
and ms_weakI2: "(Z + {#}, Z' + {#}) \<in> ms_weak" |
|
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1856 |
proof - |
58425 | 1857 |
from pw_leq_split[OF pwleq] |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1858 |
obtain A' B' Z'' |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1859 |
where [simp]: "Z = A' + Z''" "Z' = B' + Z''" |
60495 | 1860 |
and mx_or_empty: "(set_mset A', set_mset B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})" |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1861 |
by blast |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1862 |
{ |
60495 | 1863 |
assume max: "(set_mset A, set_mset B) \<in> max_strict" |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1864 |
from mx_or_empty |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1865 |
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
|
1866 |
proof |
60495 | 1867 |
assume max': "(set_mset A', set_mset B') \<in> max_strict" |
1868 |
with max have "(set_mset (A + A'), set_mset (B + B')) \<in> max_strict" |
|
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1869 |
by (auto simp: max_strict_def intro: max_ext_additive) |
58425 | 1870 |
thus ?thesis by (rule smsI) |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1871 |
next |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1872 |
assume [simp]: "A' = {#} \<and> B' = {#}" |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1873 |
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
|
1874 |
qed |
60606 | 1875 |
thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add: ac_simps) |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1876 |
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
|
1877 |
} |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1878 |
from mx_or_empty |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1879 |
have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI) |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
1880 |
thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:ac_simps) |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1881 |
qed |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1882 |
|
39301 | 1883 |
lemma empty_neutral: "{#} + x = x" "x + {#} = x" |
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1884 |
and nonempty_plus: "{# x #} + rs \<noteq> {#}" |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1885 |
and nonempty_single: "{# x #} \<noteq> {#}" |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1886 |
by auto |
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1887 |
|
60500 | 1888 |
setup \<open> |
60606 | 1889 |
let |
1890 |
fun msetT T = Type (@{type_name multiset}, [T]); |
|
1891 |
||
1892 |
fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T) |
|
1893 |
| mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x |
|
1894 |
| mk_mset T (x :: xs) = |
|
1895 |
Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $ |
|
1896 |
mk_mset T [x] $ mk_mset T xs |
|
1897 |
||
60752 | 1898 |
fun mset_member_tac ctxt m i = |
60606 | 1899 |
if m <= 0 then |
60752 | 1900 |
resolve_tac ctxt @{thms multi_member_this} i ORELSE |
1901 |
resolve_tac ctxt @{thms multi_member_last} i |
|
60606 | 1902 |
else |
60752 | 1903 |
resolve_tac ctxt @{thms multi_member_skip} i THEN mset_member_tac ctxt (m - 1) i |
1904 |
||
1905 |
fun mset_nonempty_tac ctxt = |
|
1906 |
resolve_tac ctxt @{thms nonempty_plus} ORELSE' |
|
1907 |
resolve_tac ctxt @{thms nonempty_single} |
|
29125
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
krauss
parents:
28823
diff
changeset
|
1908 |
|
60606 | 1909 |
fun regroup_munion_conv ctxt = |
1910 |
Function_Lib.regroup_conv ctxt @{const_abbrev Mempty} @{const_name plus} |
|
1911 |
(map (fn t => t RS eq_reflection) (@{thms ac_simps} @ @{thms empty_neutral})) |
|
1912 |
||
60752 | 1913 |
fun unfold_pwleq_tac ctxt i = |
1914 |
(resolve_tac ctxt @{thms pw_leq_step} i THEN (fn st => unfold_pwleq_tac ctxt (i + 1) st)) |
|
1915 |
ORELSE (resolve_tac ctxt @{thms pw_leq_lstep} i) |
|
1916 |
ORELSE (resolve_tac ctxt @{thms pw_leq_empty} i) |
|
60606 | 1917 |
|
1918 |
val set_mset_simps = [@{thm set_mset_empty}, @{thm set_mset_single}, @{thm set_mset_union}, |
|
1919 |
@{thm Un_insert_left}, @{thm Un_empty_left}] |
|
1920 |
in |
|
1921 |
ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset |
|
1922 |
{ |
|
1923 |
msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv, |
|
1924 |
mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac, |
|
1925 |
mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_mset_simps, |
|
1926 |
smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1}, |
|
60752 | 1927 |
reduction_pair = @{thm ms_reduction_pair} |
60606 | 1928 |
}) |
1929 |
end |
|
60500 | 1930 |
\<close> |
1931 |
||
1932 |
||
1933 |
subsection \<open>Legacy theorem bindings\<close> |
|
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1934 |
|
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39301
diff
changeset
|
1935 |
lemmas multi_count_eq = multiset_eq_iff [symmetric] |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1936 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1937 |
lemma union_commute: "M + N = N + (M::'a multiset)" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1938 |
by (fact add.commute) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1939 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1940 |
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1941 |
by (fact add.assoc) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1942 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1943 |
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
1944 |
by (fact add.left_commute) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1945 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1946 |
lemmas union_ac = union_assoc union_commute union_lcomm |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1947 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1948 |
lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1949 |
by (fact add_right_cancel) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1950 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1951 |
lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)" |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1952 |
by (fact add_left_cancel) |
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1953 |
|
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1954 |
lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y" |
59557 | 1955 |
by (fact add_left_imp_eq) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1956 |
|
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
1957 |
lemma mset_less_trans: "(M::'a multiset) <# K \<Longrightarrow> K <# N \<Longrightarrow> M <# N" |
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
1958 |
by (fact subset_mset.less_trans) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1959 |
|
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1960 |
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
1961 |
by (fact subset_mset.inf.commute) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1962 |
|
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1963 |
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
1964 |
by (fact subset_mset.inf.assoc [symmetric]) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1965 |
|
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1966 |
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)" |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
1967 |
by (fact subset_mset.inf.left_commute) |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1968 |
|
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1969 |
lemmas multiset_inter_ac = |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1970 |
multiset_inter_commute |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1971 |
multiset_inter_assoc |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1972 |
multiset_inter_left_commute |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1973 |
|
60606 | 1974 |
lemma mult_less_not_refl: "\<not> M #\<subset># (M::'a::order multiset)" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1975 |
by (fact multiset_order.less_irrefl) |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1976 |
|
60606 | 1977 |
lemma mult_less_trans: "K #\<subset># M \<Longrightarrow> M #\<subset># N \<Longrightarrow> K #\<subset># (N::'a::order multiset)" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1978 |
by (fact multiset_order.less_trans) |
58425 | 1979 |
|
60606 | 1980 |
lemma mult_less_not_sym: "M #\<subset># N \<Longrightarrow> \<not> N #\<subset># (M::'a::order multiset)" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1981 |
by (fact multiset_order.less_not_sym) |
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1982 |
|
60606 | 1983 |
lemma mult_less_asym: "M #\<subset># N \<Longrightarrow> (\<not> P \<Longrightarrow> N #\<subset># (M::'a::order multiset)) \<Longrightarrow> P" |
35268
04673275441a
switched notations for pointwise and multiset order
haftmann
parents:
35028
diff
changeset
|
1984 |
by (fact multiset_order.less_asym) |
34943
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
haftmann
parents:
33102
diff
changeset
|
1985 |
|
60500 | 1986 |
declaration \<open> |
60606 | 1987 |
let |
1988 |
fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T])) (Const _ $ t') = |
|
1989 |
let |
|
1990 |
val (maybe_opt, ps) = |
|
1991 |
Nitpick_Model.dest_plain_fun t' |
|
1992 |
||> op ~~ |
|
1993 |
||> map (apsnd (snd o HOLogic.dest_number)) |
|
1994 |
fun elems_for t = |
|
1995 |
(case AList.lookup (op =) ps t of |
|
1996 |
SOME n => replicate n t |
|
1997 |
| NONE => [Const (maybe_name, elem_T --> elem_T) $ t]) |
|
1998 |
in |
|
1999 |
(case maps elems_for (all_values elem_T) @ |
|
61333 | 2000 |
(if maybe_opt then [Const (Nitpick_Model.unrep_mixfix (), elem_T)] else []) of |
60606 | 2001 |
[] => Const (@{const_name zero_class.zero}, T) |
2002 |
| ts => |
|
2003 |
foldl1 (fn (t1, t2) => |
|
2004 |
Const (@{const_name plus_class.plus}, T --> T --> T) $ t1 $ t2) |
|
2005 |
(map (curry (op $) (Const (@{const_name single}, elem_T --> T))) ts)) |
|
2006 |
end |
|
2007 |
| multiset_postproc _ _ _ _ t = t |
|
2008 |
in Nitpick_Model.register_term_postprocessor @{typ "'a multiset"} multiset_postproc end |
|
60500 | 2009 |
\<close> |
2010 |
||
2011 |
||
2012 |
subsection \<open>Naive implementation using lists\<close> |
|
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2013 |
|
60515 | 2014 |
code_datatype mset |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2015 |
|
60606 | 2016 |
lemma [code]: "{#} = mset []" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2017 |
by simp |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2018 |
|
60606 | 2019 |
lemma [code]: "{#x#} = mset [x]" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2020 |
by simp |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2021 |
|
60606 | 2022 |
lemma union_code [code]: "mset xs + mset ys = mset (xs @ ys)" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2023 |
by simp |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2024 |
|
60606 | 2025 |
lemma [code]: "image_mset f (mset xs) = mset (map f xs)" |
60515 | 2026 |
by (simp add: mset_map) |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2027 |
|
60606 | 2028 |
lemma [code]: "filter_mset f (mset xs) = mset (filter f xs)" |
60515 | 2029 |
by (simp add: mset_filter) |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2030 |
|
60606 | 2031 |
lemma [code]: "mset xs - mset ys = mset (fold remove1 ys xs)" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2032 |
by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2033 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2034 |
lemma [code]: |
60515 | 2035 |
"mset xs #\<inter> mset ys = |
2036 |
mset (snd (fold (\<lambda>x (ys, zs). |
|
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2037 |
if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, [])))" |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2038 |
proof - |
60515 | 2039 |
have "\<And>zs. mset (snd (fold (\<lambda>x (ys, zs). |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2040 |
if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) = |
60515 | 2041 |
(mset xs #\<inter> mset ys) + mset zs" |
51623 | 2042 |
by (induct xs arbitrary: ys) |
2043 |
(auto simp add: mem_set_multiset_eq inter_add_right1 inter_add_right2 ac_simps) |
|
2044 |
then show ?thesis by simp |
|
2045 |
qed |
|
2046 |
||
2047 |
lemma [code]: |
|
60515 | 2048 |
"mset xs #\<union> mset ys = |
61424
c3658c18b7bc
prod_case as canonical name for product type eliminator
haftmann
parents:
61378
diff
changeset
|
2049 |
mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))" |
51623 | 2050 |
proof - |
61424
c3658c18b7bc
prod_case as canonical name for product type eliminator
haftmann
parents:
61378
diff
changeset
|
2051 |
have "\<And>zs. mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) = |
60515 | 2052 |
(mset xs #\<union> mset ys) + mset zs" |
51623 | 2053 |
by (induct xs arbitrary: ys) (simp_all add: multiset_eq_iff) |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2054 |
then show ?thesis by simp |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2055 |
qed |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2056 |
|
59813 | 2057 |
declare in_multiset_in_set [code_unfold] |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2058 |
|
60606 | 2059 |
lemma [code]: "count (mset xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2060 |
proof - |
60515 | 2061 |
have "\<And>n. fold (\<lambda>y. if x = y then Suc else id) xs n = count (mset xs) x + n" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2062 |
by (induct xs) simp_all |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2063 |
then show ?thesis by simp |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2064 |
qed |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2065 |
|
60515 | 2066 |
declare set_mset_mset [code] |
2067 |
||
2068 |
declare sorted_list_of_multiset_mset [code] |
|
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2069 |
|
61585 | 2070 |
lemma [code]: \<comment> \<open>not very efficient, but representation-ignorant!\<close> |
60515 | 2071 |
"mset_set A = mset (sorted_list_of_set A)" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2072 |
apply (cases "finite A") |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2073 |
apply simp_all |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2074 |
apply (induct A rule: finite_induct) |
59813 | 2075 |
apply (simp_all add: add.commute) |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2076 |
done |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2077 |
|
60515 | 2078 |
declare size_mset [code] |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2079 |
|
58425 | 2080 |
fun ms_lesseq_impl :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool option" where |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2081 |
"ms_lesseq_impl [] ys = Some (ys \<noteq> [])" |
58425 | 2082 |
| "ms_lesseq_impl (Cons x xs) ys = (case List.extract (op = x) ys of |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2083 |
None \<Rightarrow> None |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2084 |
| Some (ys1,_,ys2) \<Rightarrow> ms_lesseq_impl xs (ys1 @ ys2))" |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2085 |
|
60515 | 2086 |
lemma ms_lesseq_impl: "(ms_lesseq_impl xs ys = None \<longleftrightarrow> \<not> mset xs \<le># mset ys) \<and> |
2087 |
(ms_lesseq_impl xs ys = Some True \<longleftrightarrow> mset xs <# mset ys) \<and> |
|
2088 |
(ms_lesseq_impl xs ys = Some False \<longrightarrow> mset xs = mset ys)" |
|
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2089 |
proof (induct xs arbitrary: ys) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2090 |
case (Nil ys) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2091 |
show ?case by (auto simp: mset_less_empty_nonempty) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2092 |
next |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2093 |
case (Cons x xs ys) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2094 |
show ?case |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2095 |
proof (cases "List.extract (op = x) ys") |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2096 |
case None |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2097 |
hence x: "x \<notin> set ys" by (simp add: extract_None_iff) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2098 |
{ |
60515 | 2099 |
assume "mset (x # xs) \<le># mset ys" |
60495 | 2100 |
from set_mset_mono[OF this] x have False by simp |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2101 |
} note nle = this |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2102 |
moreover |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2103 |
{ |
60515 | 2104 |
assume "mset (x # xs) <# mset ys" |
2105 |
hence "mset (x # xs) \<le># mset ys" by auto |
|
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2106 |
from nle[OF this] have False . |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2107 |
} |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2108 |
ultimately show ?thesis using None by auto |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2109 |
next |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2110 |
case (Some res) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2111 |
obtain ys1 y ys2 where res: "res = (ys1,y,ys2)" by (cases res, auto) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2112 |
note Some = Some[unfolded res] |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2113 |
from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp |
60515 | 2114 |
hence id: "mset ys = mset (ys1 @ ys2) + {#x#}" |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2115 |
by (auto simp: ac_simps) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2116 |
show ?thesis unfolding ms_lesseq_impl.simps |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2117 |
unfolding Some option.simps split |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2118 |
unfolding id |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2119 |
using Cons[of "ys1 @ ys2"] |
60397
f8a513fedb31
Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
59999
diff
changeset
|
2120 |
unfolding subset_mset_def subseteq_mset_def by auto |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2121 |
qed |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2122 |
qed |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2123 |
|
60515 | 2124 |
lemma [code]: "mset xs \<le># mset ys \<longleftrightarrow> ms_lesseq_impl xs ys \<noteq> None" |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2125 |
using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto) |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2126 |
|
60515 | 2127 |
lemma [code]: "mset xs <# mset ys \<longleftrightarrow> ms_lesseq_impl xs ys = Some True" |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2128 |
using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto) |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2129 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2130 |
instantiation multiset :: (equal) equal |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2131 |
begin |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2132 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2133 |
definition |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2134 |
[code del]: "HOL.equal A (B :: 'a multiset) \<longleftrightarrow> A = B" |
60515 | 2135 |
lemma [code]: "HOL.equal (mset xs) (mset ys) \<longleftrightarrow> ms_lesseq_impl xs ys = Some False" |
55808
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2136 |
unfolding equal_multiset_def |
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents:
55565
diff
changeset
|
2137 |
using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto) |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2138 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2139 |
instance |
60678 | 2140 |
by standard (simp add: equal_multiset_def) |
2141 |
||
37169
f69efa106feb
make Nitpick "show_all" option behave less surprisingly
blanchet
parents:
37107
diff
changeset
|
2142 |
end |
49388 | 2143 |
|
60606 | 2144 |
lemma [code]: "msetsum (mset xs) = listsum xs" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2145 |
by (induct xs) (simp_all add: add.commute) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2146 |
|
60606 | 2147 |
lemma [code]: "msetprod (mset xs) = fold times xs 1" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2148 |
proof - |
60515 | 2149 |
have "\<And>x. fold times xs x = msetprod (mset xs) * x" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2150 |
by (induct xs) (simp_all add: mult.assoc) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2151 |
then show ?thesis by simp |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2152 |
qed |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2153 |
|
60500 | 2154 |
text \<open> |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2155 |
Exercise for the casual reader: add implementations for @{const le_multiset} |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2156 |
and @{const less_multiset} (multiset order). |
60500 | 2157 |
\<close> |
2158 |
||
2159 |
text \<open>Quickcheck generators\<close> |
|
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2160 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2161 |
definition (in term_syntax) |
61076 | 2162 |
msetify :: "'a::typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term) |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2163 |
\<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where |
60515 | 2164 |
[code_unfold]: "msetify xs = Code_Evaluation.valtermify mset {\<cdot>} xs" |
51600
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2165 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2166 |
notation fcomp (infixl "\<circ>>" 60) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2167 |
notation scomp (infixl "\<circ>\<rightarrow>" 60) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2168 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2169 |
instantiation multiset :: (random) random |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2170 |
begin |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2171 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2172 |
definition |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2173 |
"Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (msetify xs))" |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2174 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2175 |
instance .. |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2176 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2177 |
end |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2178 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2179 |
no_notation fcomp (infixl "\<circ>>" 60) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2180 |
no_notation scomp (infixl "\<circ>\<rightarrow>" 60) |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2181 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2182 |
instantiation multiset :: (full_exhaustive) full_exhaustive |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2183 |
begin |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2184 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2185 |
definition full_exhaustive_multiset :: "('a multiset \<times> (unit \<Rightarrow> term) \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option" |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2186 |
where |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2187 |
"full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (\<lambda>xs. f (msetify xs)) i" |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2188 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2189 |
instance .. |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2190 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2191 |
end |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2192 |
|
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2193 |
hide_const (open) msetify |
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents:
51599
diff
changeset
|
2194 |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2195 |
|
60500 | 2196 |
subsection \<open>BNF setup\<close> |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2197 |
|
57966 | 2198 |
definition rel_mset where |
60515 | 2199 |
"rel_mset R X Y \<longleftrightarrow> (\<exists>xs ys. mset xs = X \<and> mset ys = Y \<and> list_all2 R xs ys)" |
2200 |
||
2201 |
lemma mset_zip_take_Cons_drop_twice: |
|
57966 | 2202 |
assumes "length xs = length ys" "j \<le> length xs" |
60515 | 2203 |
shows "mset (zip (take j xs @ x # drop j xs) (take j ys @ y # drop j ys)) = |
2204 |
mset (zip xs ys) + {#(x, y)#}" |
|
60606 | 2205 |
using assms |
57966 | 2206 |
proof (induct xs ys arbitrary: x y j rule: list_induct2) |
2207 |
case Nil |
|
2208 |
thus ?case |
|
2209 |
by simp |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2210 |
next |
57966 | 2211 |
case (Cons x xs y ys) |
2212 |
thus ?case |
|
2213 |
proof (cases "j = 0") |
|
2214 |
case True |
|
2215 |
thus ?thesis |
|
2216 |
by simp |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2217 |
next |
57966 | 2218 |
case False |
2219 |
then obtain k where k: "j = Suc k" |
|
60678 | 2220 |
by (cases j) simp |
57966 | 2221 |
hence "k \<le> length xs" |
2222 |
using Cons.prems by auto |
|
60515 | 2223 |
hence "mset (zip (take k xs @ x # drop k xs) (take k ys @ y # drop k ys)) = |
2224 |
mset (zip xs ys) + {#(x, y)#}" |
|
57966 | 2225 |
by (rule Cons.hyps(2)) |
2226 |
thus ?thesis |
|
2227 |
unfolding k by (auto simp: add.commute union_lcomm) |
|
58425 | 2228 |
qed |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2229 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2230 |
|
60515 | 2231 |
lemma ex_mset_zip_left: |
2232 |
assumes "length xs = length ys" "mset xs' = mset xs" |
|
2233 |
shows "\<exists>ys'. length ys' = length xs' \<and> mset (zip xs' ys') = mset (zip xs ys)" |
|
58425 | 2234 |
using assms |
57966 | 2235 |
proof (induct xs ys arbitrary: xs' rule: list_induct2) |
2236 |
case Nil |
|
2237 |
thus ?case |
|
2238 |
by auto |
|
2239 |
next |
|
2240 |
case (Cons x xs y ys xs') |
|
2241 |
obtain j where j_len: "j < length xs'" and nth_j: "xs' ! j = x" |
|
60515 | 2242 |
by (metis Cons.prems in_set_conv_nth list.set_intros(1) mset_eq_setD) |
58425 | 2243 |
|
2244 |
def xsa \<equiv> "take j xs' @ drop (Suc j) xs'" |
|
60515 | 2245 |
have "mset xs' = {#x#} + mset xsa" |
57966 | 2246 |
unfolding xsa_def using j_len nth_j |
58247
98d0f85d247f
enamed drop_Suc_conv_tl and nth_drop' to Cons_nth_drop_Suc
nipkow
parents:
58098
diff
changeset
|
2247 |
by (metis (no_types) ab_semigroup_add_class.add_ac(1) append_take_drop_id Cons_nth_drop_Suc |
60515 | 2248 |
mset.simps(2) union_code add.commute) |
2249 |
hence ms_x: "mset xsa = mset xs" |
|
2250 |
by (metis Cons.prems add.commute add_right_imp_eq mset.simps(2)) |
|
57966 | 2251 |
then obtain ysa where |
60515 | 2252 |
len_a: "length ysa = length xsa" and ms_a: "mset (zip xsa ysa) = mset (zip xs ys)" |
57966 | 2253 |
using Cons.hyps(2) by blast |
2254 |
||
2255 |
def ys' \<equiv> "take j ysa @ y # drop j ysa" |
|
2256 |
have xs': "xs' = take j xsa @ x # drop j xsa" |
|
2257 |
using ms_x j_len nth_j Cons.prems xsa_def |
|
58247
98d0f85d247f
enamed drop_Suc_conv_tl and nth_drop' to Cons_nth_drop_Suc
nipkow
parents:
58098
diff
changeset
|
2258 |
by (metis append_eq_append_conv append_take_drop_id diff_Suc_Suc Cons_nth_drop_Suc length_Cons |
60515 | 2259 |
length_drop size_mset) |
57966 | 2260 |
have j_len': "j \<le> length xsa" |
2261 |
using j_len xs' xsa_def |
|
2262 |
by (metis add_Suc_right append_take_drop_id length_Cons length_append less_eq_Suc_le not_less) |
|
2263 |
have "length ys' = length xs'" |
|
2264 |
unfolding ys'_def using Cons.prems len_a ms_x |
|
60515 | 2265 |
by (metis add_Suc_right append_take_drop_id length_Cons length_append mset_eq_length) |
2266 |
moreover have "mset (zip xs' ys') = mset (zip (x # xs) (y # ys))" |
|
57966 | 2267 |
unfolding xs' ys'_def |
60515 | 2268 |
by (rule trans[OF mset_zip_take_Cons_drop_twice]) |
57966 | 2269 |
(auto simp: len_a ms_a j_len' add.commute) |
2270 |
ultimately show ?case |
|
2271 |
by blast |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2272 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2273 |
|
57966 | 2274 |
lemma list_all2_reorder_left_invariance: |
60515 | 2275 |
assumes rel: "list_all2 R xs ys" and ms_x: "mset xs' = mset xs" |
2276 |
shows "\<exists>ys'. list_all2 R xs' ys' \<and> mset ys' = mset ys" |
|
57966 | 2277 |
proof - |
2278 |
have len: "length xs = length ys" |
|
2279 |
using rel list_all2_conv_all_nth by auto |
|
2280 |
obtain ys' where |
|
60515 | 2281 |
len': "length xs' = length ys'" and ms_xy: "mset (zip xs' ys') = mset (zip xs ys)" |
2282 |
using len ms_x by (metis ex_mset_zip_left) |
|
57966 | 2283 |
have "list_all2 R xs' ys'" |
60515 | 2284 |
using assms(1) len' ms_xy unfolding list_all2_iff by (blast dest: mset_eq_setD) |
2285 |
moreover have "mset ys' = mset ys" |
|
2286 |
using len len' ms_xy map_snd_zip mset_map by metis |
|
57966 | 2287 |
ultimately show ?thesis |
2288 |
by blast |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2289 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2290 |
|
60515 | 2291 |
lemma ex_mset: "\<exists>xs. mset xs = X" |
2292 |
by (induct X) (simp, metis mset.simps(2)) |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2293 |
|
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2294 |
bnf "'a multiset" |
57966 | 2295 |
map: image_mset |
60495 | 2296 |
sets: set_mset |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2297 |
bd: natLeq |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2298 |
wits: "{#}" |
57966 | 2299 |
rel: rel_mset |
2300 |
proof - |
|
2301 |
show "image_mset id = id" |
|
2302 |
by (rule image_mset.id) |
|
60606 | 2303 |
show "image_mset (g \<circ> f) = image_mset g \<circ> image_mset f" for f g |
59813 | 2304 |
unfolding comp_def by (rule ext) (simp add: comp_def image_mset.compositionality) |
60606 | 2305 |
show "(\<And>z. z \<in> set_mset X \<Longrightarrow> f z = g z) \<Longrightarrow> image_mset f X = image_mset g X" for f g X |
2306 |
by (induct X) (simp_all (no_asm), |
|
60495 | 2307 |
metis One_nat_def Un_iff count_single mem_set_mset_iff set_mset_union zero_less_Suc) |
60606 | 2308 |
show "set_mset \<circ> image_mset f = op ` f \<circ> set_mset" for f |
57966 | 2309 |
by auto |
2310 |
show "card_order natLeq" |
|
2311 |
by (rule natLeq_card_order) |
|
2312 |
show "BNF_Cardinal_Arithmetic.cinfinite natLeq" |
|
2313 |
by (rule natLeq_cinfinite) |
|
60606 | 2314 |
show "ordLeq3 (card_of (set_mset X)) natLeq" for X |
57966 | 2315 |
by transfer |
2316 |
(auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def) |
|
60606 | 2317 |
show "rel_mset R OO rel_mset S \<le> rel_mset (R OO S)" for R S |
57966 | 2318 |
unfolding rel_mset_def[abs_def] OO_def |
2319 |
apply clarify |
|
60678 | 2320 |
subgoal for X Z Y xs ys' ys zs |
2321 |
apply (drule list_all2_reorder_left_invariance [where xs = ys' and ys = zs and xs' = ys]) |
|
2322 |
apply (auto intro: list_all2_trans) |
|
2323 |
done |
|
60606 | 2324 |
done |
2325 |
show "rel_mset R = |
|
60495 | 2326 |
(BNF_Def.Grp {x. set_mset x \<subseteq> {(x, y). R x y}} (image_mset fst))\<inverse>\<inverse> OO |
60606 | 2327 |
BNF_Def.Grp {x. set_mset x \<subseteq> {(x, y). R x y}} (image_mset snd)" for R |
57966 | 2328 |
unfolding rel_mset_def[abs_def] BNF_Def.Grp_def OO_def |
2329 |
apply (rule ext)+ |
|
2330 |
apply auto |
|
60515 | 2331 |
apply (rule_tac x = "mset (zip xs ys)" in exI; auto) |
2332 |
apply (metis list_all2_lengthD map_fst_zip mset_map) |
|
57966 | 2333 |
apply (auto simp: list_all2_iff)[1] |
60515 | 2334 |
apply (metis list_all2_lengthD map_snd_zip mset_map) |
57966 | 2335 |
apply (auto simp: list_all2_iff)[1] |
2336 |
apply (rename_tac XY) |
|
60515 | 2337 |
apply (cut_tac X = XY in ex_mset) |
57966 | 2338 |
apply (erule exE) |
2339 |
apply (rename_tac xys) |
|
2340 |
apply (rule_tac x = "map fst xys" in exI) |
|
60515 | 2341 |
apply (auto simp: mset_map) |
57966 | 2342 |
apply (rule_tac x = "map snd xys" in exI) |
60515 | 2343 |
apply (auto simp: mset_map list_all2I subset_eq zip_map_fst_snd) |
59997 | 2344 |
done |
60606 | 2345 |
show "z \<in> set_mset {#} \<Longrightarrow> False" for z |
57966 | 2346 |
by auto |
2347 |
qed |
|
2348 |
||
60606 | 2349 |
inductive rel_mset' |
2350 |
where |
|
57966 | 2351 |
Zero[intro]: "rel_mset' R {#} {#}" |
2352 |
| Plus[intro]: "\<lbrakk>R a b; rel_mset' R M N\<rbrakk> \<Longrightarrow> rel_mset' R (M + {#a#}) (N + {#b#})" |
|
2353 |
||
2354 |
lemma rel_mset_Zero: "rel_mset R {#} {#}" |
|
2355 |
unfolding rel_mset_def Grp_def by auto |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2356 |
|
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2357 |
declare multiset.count[simp] |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2358 |
declare Abs_multiset_inverse[simp] |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2359 |
declare multiset.count_inverse[simp] |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2360 |
declare union_preserves_multiset[simp] |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2361 |
|
57966 | 2362 |
lemma rel_mset_Plus: |
60606 | 2363 |
assumes ab: "R a b" |
2364 |
and MN: "rel_mset R M N" |
|
2365 |
shows "rel_mset R (M + {#a#}) (N + {#b#})" |
|
2366 |
proof - |
|
2367 |
have "\<exists>ya. image_mset fst y + {#a#} = image_mset fst ya \<and> |
|
2368 |
image_mset snd y + {#b#} = image_mset snd ya \<and> |
|
2369 |
set_mset ya \<subseteq> {(x, y). R x y}" |
|
2370 |
if "R a b" and "set_mset y \<subseteq> {(x, y). R x y}" for y |
|
2371 |
using that by (intro exI[of _ "y + {#(a,b)#}"]) auto |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2372 |
thus ?thesis |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2373 |
using assms |
57966 | 2374 |
unfolding multiset.rel_compp_Grp Grp_def by blast |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2375 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2376 |
|
60606 | 2377 |
lemma rel_mset'_imp_rel_mset: "rel_mset' R M N \<Longrightarrow> rel_mset R M N" |
60678 | 2378 |
by (induct rule: rel_mset'.induct) (auto simp: rel_mset_Zero rel_mset_Plus) |
57966 | 2379 |
|
60606 | 2380 |
lemma rel_mset_size: "rel_mset R M N \<Longrightarrow> size M = size N" |
60678 | 2381 |
unfolding multiset.rel_compp_Grp Grp_def by auto |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2382 |
|
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2383 |
lemma multiset_induct2[case_names empty addL addR]: |
60678 | 2384 |
assumes empty: "P {#} {#}" |
2385 |
and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N" |
|
2386 |
and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})" |
|
2387 |
shows "P M N" |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2388 |
apply(induct N rule: multiset_induct) |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2389 |
apply(induct M rule: multiset_induct, rule empty, erule addL) |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2390 |
apply(induct M rule: multiset_induct, erule addR, erule addR) |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2391 |
done |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2392 |
|
59949 | 2393 |
lemma multiset_induct2_size[consumes 1, case_names empty add]: |
60606 | 2394 |
assumes c: "size M = size N" |
2395 |
and empty: "P {#} {#}" |
|
2396 |
and add: "\<And>M N a b. P M N \<Longrightarrow> P (M + {#a#}) (N + {#b#})" |
|
2397 |
shows "P M N" |
|
60678 | 2398 |
using c |
2399 |
proof (induct M arbitrary: N rule: measure_induct_rule[of size]) |
|
60606 | 2400 |
case (less M) |
2401 |
show ?case |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2402 |
proof(cases "M = {#}") |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2403 |
case True hence "N = {#}" using less.prems by auto |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2404 |
thus ?thesis using True empty by auto |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2405 |
next |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2406 |
case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split) |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2407 |
have "N \<noteq> {#}" using False less.prems by auto |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2408 |
then obtain N1 b where N: "N = N1 + {#b#}" by (metis multi_nonempty_split) |
59949 | 2409 |
have "size M1 = size N1" using less.prems unfolding M N by auto |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2410 |
thus ?thesis using M N less.hyps add by auto |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2411 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2412 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2413 |
|
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2414 |
lemma msed_map_invL: |
60606 | 2415 |
assumes "image_mset f (M + {#a#}) = N" |
2416 |
shows "\<exists>N1. N = N1 + {#f a#} \<and> image_mset f M = N1" |
|
2417 |
proof - |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2418 |
have "f a \<in># N" |
60606 | 2419 |
using assms multiset.set_map[of f "M + {#a#}"] by auto |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2420 |
then obtain N1 where N: "N = N1 + {#f a#}" using multi_member_split by metis |
57966 | 2421 |
have "image_mset f M = N1" using assms unfolding N by simp |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2422 |
thus ?thesis using N by blast |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2423 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2424 |
|
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2425 |
lemma msed_map_invR: |
60606 | 2426 |
assumes "image_mset f M = N + {#b#}" |
2427 |
shows "\<exists>M1 a. M = M1 + {#a#} \<and> f a = b \<and> image_mset f M1 = N" |
|
2428 |
proof - |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2429 |
obtain a where a: "a \<in># M" and fa: "f a = b" |
60606 | 2430 |
using multiset.set_map[of f M] unfolding assms |
2431 |
by (metis image_iff mem_set_mset_iff union_single_eq_member) |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2432 |
then obtain M1 where M: "M = M1 + {#a#}" using multi_member_split by metis |
57966 | 2433 |
have "image_mset f M1 = N" using assms unfolding M fa[symmetric] by simp |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2434 |
thus ?thesis using M fa by blast |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2435 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2436 |
|
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2437 |
lemma msed_rel_invL: |
60606 | 2438 |
assumes "rel_mset R (M + {#a#}) N" |
2439 |
shows "\<exists>N1 b. N = N1 + {#b#} \<and> R a b \<and> rel_mset R M N1" |
|
2440 |
proof - |
|
57966 | 2441 |
obtain K where KM: "image_mset fst K = M + {#a#}" |
60606 | 2442 |
and KN: "image_mset snd K = N" and sK: "set_mset K \<subseteq> {(a, b). R a b}" |
2443 |
using assms |
|
2444 |
unfolding multiset.rel_compp_Grp Grp_def by auto |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2445 |
obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a" |
60606 | 2446 |
and K1M: "image_mset fst K1 = M" using msed_map_invR[OF KM] by auto |
57966 | 2447 |
obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "image_mset snd K1 = N1" |
60606 | 2448 |
using msed_map_invL[OF KN[unfolded K]] by auto |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2449 |
have Rab: "R a (snd ab)" using sK a unfolding K by auto |
57966 | 2450 |
have "rel_mset R M N1" using sK K1M K1N1 |
60606 | 2451 |
unfolding K multiset.rel_compp_Grp Grp_def by auto |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2452 |
thus ?thesis using N Rab by auto |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2453 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2454 |
|
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2455 |
lemma msed_rel_invR: |
60606 | 2456 |
assumes "rel_mset R M (N + {#b#})" |
2457 |
shows "\<exists>M1 a. M = M1 + {#a#} \<and> R a b \<and> rel_mset R M1 N" |
|
2458 |
proof - |
|
57966 | 2459 |
obtain K where KN: "image_mset snd K = N + {#b#}" |
60606 | 2460 |
and KM: "image_mset fst K = M" and sK: "set_mset K \<subseteq> {(a, b). R a b}" |
2461 |
using assms |
|
2462 |
unfolding multiset.rel_compp_Grp Grp_def by auto |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2463 |
obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b" |
60606 | 2464 |
and K1N: "image_mset snd K1 = N" using msed_map_invR[OF KN] by auto |
57966 | 2465 |
obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "image_mset fst K1 = M1" |
60606 | 2466 |
using msed_map_invL[OF KM[unfolded K]] by auto |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2467 |
have Rab: "R (fst ab) b" using sK b unfolding K by auto |
57966 | 2468 |
have "rel_mset R M1 N" using sK K1N K1M1 |
60606 | 2469 |
unfolding K multiset.rel_compp_Grp Grp_def by auto |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2470 |
thus ?thesis using M Rab by auto |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2471 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2472 |
|
57966 | 2473 |
lemma rel_mset_imp_rel_mset': |
60606 | 2474 |
assumes "rel_mset R M N" |
2475 |
shows "rel_mset' R M N" |
|
59949 | 2476 |
using assms proof(induct M arbitrary: N rule: measure_induct_rule[of size]) |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2477 |
case (less M) |
59949 | 2478 |
have c: "size M = size N" using rel_mset_size[OF less.prems] . |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2479 |
show ?case |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2480 |
proof(cases "M = {#}") |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2481 |
case True hence "N = {#}" using c by simp |
57966 | 2482 |
thus ?thesis using True rel_mset'.Zero by auto |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2483 |
next |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2484 |
case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split) |
57966 | 2485 |
obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "rel_mset R M1 N1" |
60606 | 2486 |
using msed_rel_invL[OF less.prems[unfolded M]] by auto |
57966 | 2487 |
have "rel_mset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp |
2488 |
thus ?thesis using rel_mset'.Plus[of R a b, OF R] unfolding M N by simp |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2489 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2490 |
qed |
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2491 |
|
60606 | 2492 |
lemma rel_mset_rel_mset': "rel_mset R M N = rel_mset' R M N" |
60678 | 2493 |
using rel_mset_imp_rel_mset' rel_mset'_imp_rel_mset by auto |
57966 | 2494 |
|
60613 | 2495 |
text \<open>The main end product for @{const rel_mset}: inductive characterization:\<close> |
61337 | 2496 |
lemmas rel_mset_induct[case_names empty add, induct pred: rel_mset] = |
60606 | 2497 |
rel_mset'.induct[unfolded rel_mset_rel_mset'[symmetric]] |
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2498 |
|
56656 | 2499 |
|
60500 | 2500 |
subsection \<open>Size setup\<close> |
56656 | 2501 |
|
57966 | 2502 |
lemma multiset_size_o_map: "size_multiset g \<circ> image_mset f = size_multiset (g \<circ> f)" |
60678 | 2503 |
apply (rule ext) |
2504 |
subgoal for x by (induct x) auto |
|
2505 |
done |
|
56656 | 2506 |
|
60500 | 2507 |
setup \<open> |
60606 | 2508 |
BNF_LFP_Size.register_size_global @{type_name multiset} @{const_name size_multiset} |
62082 | 2509 |
@{thm size_multiset_overloaded_def} |
60606 | 2510 |
@{thms size_multiset_empty size_multiset_single size_multiset_union size_empty size_single |
2511 |
size_union} |
|
2512 |
@{thms multiset_size_o_map} |
|
60500 | 2513 |
\<close> |
56656 | 2514 |
|
2515 |
hide_const (open) wcount |
|
2516 |
||
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54868
diff
changeset
|
2517 |
end |