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