| author | wenzelm | 
| Sun, 23 Jun 2013 17:14:20 +0200 | |
| changeset 52425 | de8a85aad216 | 
| parent 52289 | 83ce5d2841e7 | 
| child 54295 | 45a5523d4a63 | 
| 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 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 5 | header {* (Finite) multisets *}
 | 
| 10249 | 6 | |
| 15131 | 7 | theory Multiset | 
| 51599 
1559e9266280
optionalized very specific code setup for multisets
 haftmann parents: 
51548diff
changeset | 8 | imports Main | 
| 15131 | 9 | begin | 
| 10249 | 10 | |
| 11 | subsection {* The type of multisets *}
 | |
| 12 | ||
| 45694 
4a8743618257
prefer typedef without extra definition and alternative name;
 wenzelm parents: 
45608diff
changeset | 13 | definition "multiset = {f :: 'a => nat. finite {x. f x > 0}}"
 | 
| 
4a8743618257
prefer typedef without extra definition and alternative name;
 wenzelm parents: 
45608diff
changeset | 14 | |
| 49834 | 15 | typedef 'a multiset = "multiset :: ('a => nat) set"
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 16 | morphisms count Abs_multiset | 
| 45694 
4a8743618257
prefer typedef without extra definition and alternative name;
 wenzelm parents: 
45608diff
changeset | 17 | unfolding multiset_def | 
| 10249 | 18 | proof | 
| 45694 
4a8743618257
prefer typedef without extra definition and alternative name;
 wenzelm parents: 
45608diff
changeset | 19 |   show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp
 | 
| 10249 | 20 | qed | 
| 21 | ||
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 22 | setup_lifting type_definition_multiset | 
| 19086 | 23 | |
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 24 | abbreviation Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
 | 
| 25610 | 25 | "a :# M == 0 < count M a" | 
| 26 | ||
| 26145 | 27 | notation (xsymbols) | 
| 28 | Melem (infix "\<in>#" 50) | |
| 10249 | 29 | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 30 | lemma multiset_eq_iff: | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 31 | "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 32 | by (simp only: count_inject [symmetric] fun_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 33 | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 34 | lemma multiset_eqI: | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 35 | "(\<And>x. count A x = count B x) \<Longrightarrow> A = B" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 36 | using multiset_eq_iff by auto | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 37 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 38 | text {*
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 39 |  \medskip Preservation of the representing set @{term multiset}.
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 40 | *} | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 41 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 42 | lemma const0_in_multiset: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 43 | "(\<lambda>a. 0) \<in> multiset" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 44 | by (simp add: multiset_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 45 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 46 | lemma only1_in_multiset: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 47 | "(\<lambda>b. if b = a then n else 0) \<in> multiset" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 48 | by (simp add: multiset_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 49 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 50 | lemma union_preserves_multiset: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 51 | "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 52 | by (simp add: multiset_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 53 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 54 | lemma diff_preserves_multiset: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 55 | assumes "M \<in> multiset" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 56 | shows "(\<lambda>a. M a - N a) \<in> multiset" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 57 | proof - | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 58 |   have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 59 | by auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 60 | with assms show ?thesis | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 61 | by (auto simp add: multiset_def intro: finite_subset) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 62 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 63 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 64 | lemma filter_preserves_multiset: | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 65 | assumes "M \<in> multiset" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 66 | shows "(\<lambda>x. if P x then M x else 0) \<in> multiset" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 67 | proof - | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 68 |   have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 69 | by auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 70 | with assms show ?thesis | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 71 | by (auto simp add: multiset_def intro: finite_subset) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 72 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 73 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 74 | lemmas in_multiset = const0_in_multiset only1_in_multiset | 
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 75 | union_preserves_multiset diff_preserves_multiset filter_preserves_multiset | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 76 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 77 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 78 | subsection {* Representing multisets *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 79 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 80 | text {* Multiset enumeration *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 81 | |
| 48008 | 82 | instantiation multiset :: (type) cancel_comm_monoid_add | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 83 | begin | 
| 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 84 | |
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 85 | lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0" | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 86 | by (rule const0_in_multiset) | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 87 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 88 | abbreviation Mempty :: "'a multiset" ("{#}") where
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 89 | "Mempty \<equiv> 0" | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 90 | |
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 91 | lift_definition plus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)" | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 92 | by (rule union_preserves_multiset) | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 93 | |
| 48008 | 94 | instance | 
| 95 | by default (transfer, simp add: fun_eq_iff)+ | |
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 96 | |
| 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 97 | end | 
| 10249 | 98 | |
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 99 | lift_definition single :: "'a => 'a multiset" is "\<lambda>a b. if b = a then 1 else 0" | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 100 | by (rule only1_in_multiset) | 
| 15869 | 101 | |
| 26145 | 102 | syntax | 
| 26176 | 103 |   "_multiset" :: "args => 'a multiset"    ("{#(_)#}")
 | 
| 25507 | 104 | translations | 
| 105 |   "{#x, xs#}" == "{#x#} + {#xs#}"
 | |
| 106 |   "{#x#}" == "CONST single x"
 | |
| 107 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 108 | lemma count_empty [simp]: "count {#} a = 0"
 | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 109 | by (simp add: zero_multiset.rep_eq) | 
| 10249 | 110 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 111 | lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
 | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 112 | by (simp add: single.rep_eq) | 
| 29901 | 113 | |
| 10249 | 114 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 115 | subsection {* Basic operations *}
 | 
| 10249 | 116 | |
| 117 | subsubsection {* Union *}
 | |
| 118 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 119 | lemma count_union [simp]: "count (M + N) a = count M a + count N a" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 120 | by (simp add: plus_multiset.rep_eq) | 
| 10249 | 121 | |
| 122 | ||
| 123 | subsubsection {* Difference *}
 | |
| 124 | ||
| 49388 | 125 | instantiation multiset :: (type) comm_monoid_diff | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 126 | begin | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 127 | |
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 128 | lift_definition minus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a" | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 129 | by (rule diff_preserves_multiset) | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 130 | |
| 49388 | 131 | instance | 
| 132 | by default (transfer, simp add: fun_eq_iff)+ | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 133 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 134 | end | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 135 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 136 | lemma count_diff [simp]: "count (M - N) a = count M a - count N a" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 137 | by (simp add: minus_multiset.rep_eq) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 138 | |
| 17161 | 139 | lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
 | 
| 52289 | 140 | by rule (fact Groups.diff_zero, fact Groups.zero_diff) | 
| 36903 | 141 | |
| 142 | lemma diff_cancel[simp]: "A - A = {#}"
 | |
| 52289 | 143 | by (fact Groups.diff_cancel) | 
| 10249 | 144 | |
| 36903 | 145 | lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)" | 
| 52289 | 146 | by (fact add_diff_cancel_right') | 
| 10249 | 147 | |
| 36903 | 148 | lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)" | 
| 52289 | 149 | by (fact add_diff_cancel_left') | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 150 | |
| 52289 | 151 | lemma diff_right_commute: | 
| 152 | "(M::'a multiset) - N - Q = M - Q - N" | |
| 153 | by (fact diff_right_commute) | |
| 154 | ||
| 155 | lemma diff_add: | |
| 156 | "(M::'a multiset) - (N + Q) = M - N - Q" | |
| 157 | by (rule sym) (fact diff_diff_add) | |
| 158 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 159 | lemma insert_DiffM: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 160 |   "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 161 | by (clarsimp simp: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 162 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 163 | lemma insert_DiffM2 [simp]: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 164 |   "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 165 | by (clarsimp simp: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 166 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 167 | lemma diff_union_swap: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 168 |   "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 169 | by (auto simp add: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 170 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 171 | lemma diff_union_single_conv: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 172 |   "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 173 | by (simp add: multiset_eq_iff) | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26033diff
changeset | 174 | |
| 10249 | 175 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 176 | subsubsection {* Equality of multisets *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 177 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 178 | lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 179 | by (simp add: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 180 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 181 | lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 182 | by (auto simp add: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 183 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 184 | lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 185 | by (auto simp add: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 186 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 187 | lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 188 | by (auto simp add: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 189 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 190 | lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 191 | by (auto simp add: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 192 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 193 | lemma diff_single_trivial: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 194 |   "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 195 | by (auto simp add: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 196 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 197 | lemma diff_single_eq_union: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 198 |   "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 199 | by auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 200 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 201 | lemma union_single_eq_diff: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 202 |   "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 203 | by (auto dest: sym) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 204 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 205 | lemma union_single_eq_member: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 206 |   "M + {#x#} = N \<Longrightarrow> x \<in># N"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 207 | by auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 208 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 209 | lemma union_is_single: | 
| 46730 | 210 |   "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}" (is "?lhs = ?rhs")
 | 
| 211 | proof | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 212 | assume ?rhs then show ?lhs by auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 213 | next | 
| 46730 | 214 | assume ?lhs then show ?rhs | 
| 215 | by (simp add: multiset_eq_iff split:if_splits) (metis add_is_1) | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 216 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 217 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 218 | lemma single_is_union: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 219 |   "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 220 |   by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 221 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 222 | lemma add_eq_conv_diff: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 223 |   "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"  (is "?lhs = ?rhs")
 | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44339diff
changeset | 224 | (* shorter: by (simp add: multiset_eq_iff) fastforce *) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 225 | proof | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 226 | assume ?rhs then show ?lhs | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 227 |   by (auto simp add: add_assoc add_commute [of "{#b#}"])
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 228 | (drule sym, simp add: add_assoc [symmetric]) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 229 | next | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 230 | assume ?lhs | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 231 | show ?rhs | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 232 | proof (cases "a = b") | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 233 | case True with `?lhs` show ?thesis by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 234 | next | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 235 | case False | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 236 |     from `?lhs` have "a \<in># N + {#b#}" by (rule union_single_eq_member)
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 237 | with False have "a \<in># N" by auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 238 |     moreover from `?lhs` have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 239 | moreover note False | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 240 |     ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 241 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 242 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 243 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 244 | lemma insert_noteq_member: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 245 |   assumes BC: "B + {#b#} = C + {#c#}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 246 | and bnotc: "b \<noteq> c" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 247 | shows "c \<in># B" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 248 | proof - | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 249 |   have "c \<in># C + {#c#}" by simp
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 250 |   have nc: "\<not> c \<in># {#b#}" using bnotc by simp
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 251 |   then have "c \<in># B + {#b#}" using BC by simp
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 252 | then show "c \<in># B" using nc by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 253 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 254 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 255 | lemma add_eq_conv_ex: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 256 |   "(M + {#a#} = N + {#b#}) =
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 257 |     (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 258 | by (auto simp add: add_eq_conv_diff) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 259 | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 260 | lemma multi_member_split: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 261 |   "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 262 |   by (rule_tac x = "M - {#x#}" in exI, simp)
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 263 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 264 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 265 | subsubsection {* Pointwise ordering induced by count *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 266 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 267 | instantiation multiset :: (type) ordered_ab_semigroup_add_imp_le | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 268 | begin | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 269 | |
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 270 | lift_definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" is "\<lambda> A B. (\<forall>a. A a \<le> B a)" | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 271 | by simp | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 272 | lemmas mset_le_def = less_eq_multiset_def | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 273 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 274 | definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 275 | mset_less_def: "(A::'a multiset) < B \<longleftrightarrow> A \<le> B \<and> A \<noteq> B" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 276 | |
| 46921 | 277 | instance | 
| 278 | by default (auto simp add: mset_le_def mset_less_def multiset_eq_iff intro: order_trans antisym) | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 279 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 280 | end | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 281 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 282 | lemma mset_less_eqI: | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 283 | "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le> B" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 284 | by (simp add: mset_le_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 285 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 286 | lemma mset_le_exists_conv: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 287 | "(A::'a multiset) \<le> B \<longleftrightarrow> (\<exists>C. B = A + C)" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 288 | apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI) | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 289 | apply (auto intro: multiset_eq_iff [THEN iffD2]) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 290 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 291 | |
| 52289 | 292 | instance multiset :: (type) ordered_cancel_comm_monoid_diff | 
| 293 | by default (simp, fact mset_le_exists_conv) | |
| 294 | ||
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 295 | lemma mset_le_mono_add_right_cancel [simp]: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 296 | "(A::'a multiset) + C \<le> B + C \<longleftrightarrow> A \<le> B" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 297 | by (fact add_le_cancel_right) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 298 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 299 | lemma mset_le_mono_add_left_cancel [simp]: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 300 | "C + (A::'a multiset) \<le> C + B \<longleftrightarrow> A \<le> B" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 301 | by (fact add_le_cancel_left) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 302 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 303 | lemma mset_le_mono_add: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 304 | "(A::'a multiset) \<le> B \<Longrightarrow> C \<le> D \<Longrightarrow> A + C \<le> B + D" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 305 | by (fact add_mono) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 306 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 307 | lemma mset_le_add_left [simp]: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 308 | "(A::'a multiset) \<le> A + B" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 309 | unfolding mset_le_def by auto | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 310 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 311 | lemma mset_le_add_right [simp]: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 312 | "B \<le> (A::'a multiset) + B" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 313 | unfolding mset_le_def by auto | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 314 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 315 | lemma mset_le_single: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 316 |   "a :# B \<Longrightarrow> {#a#} \<le> B"
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 317 | by (simp add: mset_le_def) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 318 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 319 | lemma multiset_diff_union_assoc: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 320 | "C \<le> B \<Longrightarrow> (A::'a multiset) + B - C = A + (B - C)" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 321 | by (simp add: multiset_eq_iff mset_le_def) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 322 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 323 | lemma mset_le_multiset_union_diff_commute: | 
| 36867 | 324 | "B \<le> A \<Longrightarrow> (A::'a multiset) - B + C = A + C - B" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 325 | by (simp add: multiset_eq_iff mset_le_def) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 326 | |
| 39301 | 327 | lemma diff_le_self[simp]: "(M::'a multiset) - N \<le> M" | 
| 328 | by(simp add: mset_le_def) | |
| 329 | ||
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 330 | lemma mset_lessD: "A < B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 331 | apply (clarsimp simp: mset_le_def mset_less_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 332 | apply (erule_tac x=x in allE) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 333 | apply auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 334 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 335 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 336 | lemma mset_leD: "A \<le> B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 337 | apply (clarsimp simp: mset_le_def mset_less_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 338 | apply (erule_tac x = x in allE) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 339 | apply auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 340 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 341 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 342 | lemma mset_less_insertD: "(A + {#x#} < B) \<Longrightarrow> (x \<in># B \<and> A < B)"
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 343 | apply (rule conjI) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 344 | apply (simp add: mset_lessD) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 345 | apply (clarsimp simp: mset_le_def mset_less_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 346 | apply safe | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 347 | apply (erule_tac x = a in allE) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 348 | apply (auto split: split_if_asm) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 349 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 350 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 351 | lemma mset_le_insertD: "(A + {#x#} \<le> B) \<Longrightarrow> (x \<in># B \<and> A \<le> B)"
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 352 | apply (rule conjI) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 353 | apply (simp add: mset_leD) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 354 | apply (force simp: mset_le_def mset_less_def split: split_if_asm) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 355 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 356 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 357 | lemma mset_less_of_empty[simp]: "A < {#} \<longleftrightarrow> False"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 358 | by (auto simp add: mset_less_def mset_le_def multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 359 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 360 | lemma multi_psub_of_add_self[simp]: "A < A + {#x#}"
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 361 | by (auto simp: mset_le_def mset_less_def) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 362 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 363 | lemma multi_psub_self[simp]: "(A::'a multiset) < A = False" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 364 | by simp | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 365 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 366 | lemma mset_less_add_bothsides: | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 367 |   "T + {#x#} < S + {#x#} \<Longrightarrow> T < S"
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 368 | by (fact add_less_imp_less_right) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 369 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 370 | lemma mset_less_empty_nonempty: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 371 |   "{#} < S \<longleftrightarrow> S \<noteq> {#}"
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 372 | by (auto simp: mset_le_def mset_less_def) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 373 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 374 | lemma mset_less_diff_self: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 375 |   "c \<in># B \<Longrightarrow> B - {#c#} < B"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 376 | by (auto simp: mset_le_def mset_less_def multiset_eq_iff) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 377 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 378 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 379 | subsubsection {* Intersection *}
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 380 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 381 | instantiation multiset :: (type) semilattice_inf | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 382 | begin | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 383 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 384 | definition inf_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 385 | multiset_inter_def: "inf_multiset A B = A - (A - B)" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 386 | |
| 46921 | 387 | instance | 
| 388 | proof - | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 389 | have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" by arith | 
| 46921 | 390 |   show "OFCLASS('a multiset, semilattice_inf_class)"
 | 
| 391 | by default (auto simp add: multiset_inter_def mset_le_def aux) | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 392 | qed | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 393 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 394 | end | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 395 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 396 | abbreviation multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 397 | "multiset_inter \<equiv> inf" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 398 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 399 | lemma multiset_inter_count [simp]: | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 400 | "count (A #\<inter> B) x = min (count A x) (count B x)" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 401 | by (simp add: multiset_inter_def) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 402 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 403 | lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
 | 
| 46730 | 404 | by (rule multiset_eqI) auto | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 405 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 406 | lemma multiset_union_diff_commute: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 407 |   assumes "B #\<inter> C = {#}"
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 408 | shows "A + B - C = A - C + B" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 409 | proof (rule multiset_eqI) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 410 | fix x | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 411 | from assms have "min (count B x) (count C x) = 0" | 
| 46730 | 412 | by (auto simp add: multiset_eq_iff) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 413 | then have "count B x = 0 \<or> count C x = 0" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 414 | by auto | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 415 | then show "count (A + B - C) x = count (A - C + B) x" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 416 | by auto | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 417 | qed | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 418 | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 419 | lemma empty_inter [simp]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 420 |   "{#} #\<inter> M = {#}"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 421 | by (simp add: multiset_eq_iff) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 422 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 423 | lemma inter_empty [simp]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 424 |   "M #\<inter> {#} = {#}"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 425 | by (simp add: multiset_eq_iff) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 426 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 427 | lemma inter_add_left1: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 428 |   "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 429 | by (simp add: multiset_eq_iff) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 430 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 431 | lemma inter_add_left2: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 432 |   "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 433 | by (simp add: multiset_eq_iff) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 434 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 435 | lemma inter_add_right1: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 436 |   "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 437 | by (simp add: multiset_eq_iff) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 438 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 439 | lemma inter_add_right2: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 440 |   "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 441 | by (simp add: multiset_eq_iff) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 442 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 443 | |
| 51623 | 444 | subsubsection {* Bounded union *}
 | 
| 445 | ||
| 446 | instantiation multiset :: (type) semilattice_sup | |
| 447 | begin | |
| 448 | ||
| 449 | definition sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where | |
| 450 | "sup_multiset A B = A + (B - A)" | |
| 451 | ||
| 452 | instance | |
| 453 | proof - | |
| 454 | have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" by arith | |
| 455 |   show "OFCLASS('a multiset, semilattice_sup_class)"
 | |
| 456 | by default (auto simp add: sup_multiset_def mset_le_def aux) | |
| 457 | qed | |
| 458 | ||
| 459 | end | |
| 460 | ||
| 461 | abbreviation sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<union>" 70) where | |
| 462 | "sup_multiset \<equiv> sup" | |
| 463 | ||
| 464 | lemma sup_multiset_count [simp]: | |
| 465 | "count (A #\<union> B) x = max (count A x) (count B x)" | |
| 466 | by (simp add: sup_multiset_def) | |
| 467 | ||
| 468 | lemma empty_sup [simp]: | |
| 469 |   "{#} #\<union> M = M"
 | |
| 470 | by (simp add: multiset_eq_iff) | |
| 471 | ||
| 472 | lemma sup_empty [simp]: | |
| 473 |   "M #\<union> {#} = M"
 | |
| 474 | by (simp add: multiset_eq_iff) | |
| 475 | ||
| 476 | lemma sup_add_left1: | |
| 477 |   "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
 | |
| 478 | by (simp add: multiset_eq_iff) | |
| 479 | ||
| 480 | lemma sup_add_left2: | |
| 481 |   "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
 | |
| 482 | by (simp add: multiset_eq_iff) | |
| 483 | ||
| 484 | lemma sup_add_right1: | |
| 485 |   "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
 | |
| 486 | by (simp add: multiset_eq_iff) | |
| 487 | ||
| 488 | lemma sup_add_right2: | |
| 489 |   "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
 | |
| 490 | by (simp add: multiset_eq_iff) | |
| 491 | ||
| 492 | ||
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 493 | subsubsection {* Filter (with comprehension syntax) *}
 | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 494 | |
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 495 | text {* Multiset comprehension *}
 | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 496 | |
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 497 | lift_definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
 | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 498 | by (rule filter_preserves_multiset) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 499 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 500 | hide_const (open) filter | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 501 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 502 | lemma count_filter [simp]: | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 503 | "count (Multiset.filter P M) a = (if P a then count M a else 0)" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 504 | by (simp add: filter.rep_eq) | 
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 505 | |
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 506 | lemma filter_empty [simp]: | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 507 |   "Multiset.filter P {#} = {#}"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 508 | by (rule multiset_eqI) simp | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 509 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 510 | lemma filter_single [simp]: | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 511 |   "Multiset.filter P {#x#} = (if P x then {#x#} else {#})"
 | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 512 | by (rule multiset_eqI) simp | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 513 | |
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 514 | lemma filter_union [simp]: | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 515 | "Multiset.filter P (M + N) = Multiset.filter P M + Multiset.filter P N" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 516 | by (rule multiset_eqI) simp | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 517 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 518 | lemma filter_diff [simp]: | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 519 | "Multiset.filter P (M - N) = Multiset.filter P M - Multiset.filter P N" | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 520 | by (rule multiset_eqI) simp | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 521 | |
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 522 | lemma filter_inter [simp]: | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 523 | "Multiset.filter P (M #\<inter> N) = Multiset.filter P M #\<inter> Multiset.filter P N" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 524 | by (rule multiset_eqI) simp | 
| 10249 | 525 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 526 | syntax | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 527 |   "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
 | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 528 | syntax (xsymbol) | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 529 |   "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
 | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 530 | translations | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 531 |   "{#x \<in># M. P#}" == "CONST Multiset.filter (\<lambda>x. P) M"
 | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 532 | |
| 10249 | 533 | |
| 534 | subsubsection {* Set of elements *}
 | |
| 535 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 536 | definition set_of :: "'a multiset => 'a set" where | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 537 |   "set_of M = {x. x :# M}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 538 | |
| 17161 | 539 | lemma set_of_empty [simp]: "set_of {#} = {}"
 | 
| 26178 | 540 | by (simp add: set_of_def) | 
| 10249 | 541 | |
| 17161 | 542 | lemma set_of_single [simp]: "set_of {#b#} = {b}"
 | 
| 26178 | 543 | by (simp add: set_of_def) | 
| 10249 | 544 | |
| 17161 | 545 | lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N" | 
| 26178 | 546 | by (auto simp add: set_of_def) | 
| 10249 | 547 | |
| 17161 | 548 | lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 549 | by (auto simp add: set_of_def multiset_eq_iff) | 
| 10249 | 550 | |
| 17161 | 551 | lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)" | 
| 26178 | 552 | by (auto simp add: set_of_def) | 
| 26016 | 553 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 554 | lemma set_of_filter [simp]: "set_of {# x:#M. P x #} = set_of M \<inter> {x. P x}"
 | 
| 26178 | 555 | by (auto simp add: set_of_def) | 
| 10249 | 556 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 557 | lemma finite_set_of [iff]: "finite (set_of M)" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 558 | using count [of M] by (simp add: multiset_def set_of_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 559 | |
| 46756 
faf62905cd53
adding finiteness of intervals on integer sets; adding another finiteness theorem for multisets
 bulwahn parents: 
46730diff
changeset | 560 | lemma finite_Collect_mem [iff]: "finite {x. x :# M}"
 | 
| 
faf62905cd53
adding finiteness of intervals on integer sets; adding another finiteness theorem for multisets
 bulwahn parents: 
46730diff
changeset | 561 | unfolding set_of_def[symmetric] by simp | 
| 10249 | 562 | |
| 563 | subsubsection {* Size *}
 | |
| 564 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 565 | instantiation multiset :: (type) size | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 566 | begin | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 567 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 568 | definition size_def: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 569 | "size M = setsum (count M) (set_of M)" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 570 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 571 | instance .. | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 572 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 573 | end | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 574 | |
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 575 | lemma size_empty [simp]: "size {#} = 0"
 | 
| 26178 | 576 | by (simp add: size_def) | 
| 10249 | 577 | |
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 578 | lemma size_single [simp]: "size {#b#} = 1"
 | 
| 26178 | 579 | by (simp add: size_def) | 
| 10249 | 580 | |
| 17161 | 581 | lemma setsum_count_Int: | 
| 26178 | 582 | "finite A ==> setsum (count N) (A \<inter> set_of N) = setsum (count N) A" | 
| 583 | apply (induct rule: finite_induct) | |
| 584 | apply simp | |
| 585 | apply (simp add: Int_insert_left set_of_def) | |
| 586 | done | |
| 10249 | 587 | |
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 588 | lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N" | 
| 26178 | 589 | apply (unfold size_def) | 
| 590 | apply (subgoal_tac "count (M + N) = (\<lambda>a. count M a + count N a)") | |
| 591 | prefer 2 | |
| 592 | apply (rule ext, simp) | |
| 593 | apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int) | |
| 594 | apply (subst Int_commute) | |
| 595 | apply (simp (no_asm_simp) add: setsum_count_Int) | |
| 596 | done | |
| 10249 | 597 | |
| 17161 | 598 | lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 599 | by (auto simp add: size_def multiset_eq_iff) | 
| 26016 | 600 | |
| 601 | lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
 | |
| 26178 | 602 | by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty) | 
| 10249 | 603 | |
| 17161 | 604 | lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M" | 
| 26178 | 605 | apply (unfold size_def) | 
| 606 | apply (drule setsum_SucD) | |
| 607 | apply auto | |
| 608 | done | |
| 10249 | 609 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 610 | lemma size_eq_Suc_imp_eq_union: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 611 | assumes "size M = Suc n" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 612 |   shows "\<exists>a N. M = N + {#a#}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 613 | proof - | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 614 | from assms obtain a where "a \<in># M" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 615 | by (erule size_eq_Suc_imp_elem [THEN exE]) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 616 |   then have "M = M - {#a#} + {#a#}" by simp
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 617 | then show ?thesis by blast | 
| 23611 | 618 | qed | 
| 15869 | 619 | |
| 26016 | 620 | |
| 621 | subsection {* Induction and case splits *}
 | |
| 10249 | 622 | |
| 18258 | 623 | theorem multiset_induct [case_names empty add, induct type: multiset]: | 
| 48009 | 624 |   assumes empty: "P {#}"
 | 
| 625 |   assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
 | |
| 626 | shows "P M" | |
| 627 | proof (induct n \<equiv> "size M" arbitrary: M) | |
| 628 | case 0 thus "P M" by (simp add: empty) | |
| 629 | next | |
| 630 | case (Suc k) | |
| 631 |   obtain N x where "M = N + {#x#}"
 | |
| 632 | using `Suc k = size M` [symmetric] | |
| 633 | using size_eq_Suc_imp_eq_union by fast | |
| 634 | with Suc add show "P M" by simp | |
| 10249 | 635 | qed | 
| 636 | ||
| 25610 | 637 | lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
 | 
| 26178 | 638 | by (induct M) auto | 
| 25610 | 639 | |
| 640 | lemma multiset_cases [cases type, case_names empty add]: | |
| 26178 | 641 | assumes em:  "M = {#} \<Longrightarrow> P"
 | 
| 642 | assumes add: "\<And>N x. M = N + {#x#} \<Longrightarrow> P"
 | |
| 643 | shows "P" | |
| 48009 | 644 | using assms by (induct M) simp_all | 
| 25610 | 645 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 646 | lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 647 | by (cases "B = {#}") (auto dest: multi_member_split)
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 648 | |
| 26033 | 649 | lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#M. \<not> P x #}"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 650 | apply (subst multiset_eq_iff) | 
| 26178 | 651 | apply auto | 
| 652 | done | |
| 10249 | 653 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 654 | lemma mset_less_size: "(A::'a multiset) < B \<Longrightarrow> size A < size B" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 655 | proof (induct A arbitrary: B) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 656 | case (empty M) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 657 |   then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 658 |   then obtain M' x where "M = M' + {#x#}" 
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 659 | by (blast dest: multi_nonempty_split) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 660 | then show ?case by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 661 | next | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 662 | case (add S x T) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 663 | have IH: "\<And>B. S < B \<Longrightarrow> size S < size B" by fact | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 664 |   have SxsubT: "S + {#x#} < T" by fact
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 665 | then have "x \<in># T" and "S < T" by (auto dest: mset_less_insertD) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 666 |   then obtain T' where T: "T = T' + {#x#}" 
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 667 | by (blast dest: multi_member_split) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 668 | then have "S < T'" using SxsubT | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 669 | by (blast intro: mset_less_add_bothsides) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 670 | then have "size S < size T'" using IH by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 671 | then show ?case using T by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 672 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 673 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 674 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 675 | subsubsection {* Strong induction and subset induction for multisets *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 676 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 677 | text {* Well-foundedness of proper subset operator: *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 678 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 679 | text {* proper multiset subset *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 680 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 681 | definition | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 682 |   mset_less_rel :: "('a multiset * 'a multiset) set" where
 | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 683 |   "mset_less_rel = {(A,B). A < B}"
 | 
| 10249 | 684 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 685 | lemma multiset_add_sub_el_shuffle: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 686 | assumes "c \<in># B" and "b \<noteq> c" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 687 |   shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 688 | proof - | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 689 |   from `c \<in># B` obtain A where B: "B = A + {#c#}" 
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 690 | by (blast dest: multi_member_split) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 691 |   have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 692 |   then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}" 
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 693 | by (simp add: add_ac) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 694 | then show ?thesis using B by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 695 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 696 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 697 | lemma wf_mset_less_rel: "wf mset_less_rel" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 698 | apply (unfold mset_less_rel_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 699 | apply (rule wf_measure [THEN wf_subset, where f1=size]) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 700 | apply (clarsimp simp: measure_def inv_image_def mset_less_size) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 701 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 702 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 703 | text {* The induction rules: *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 704 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 705 | lemma full_multiset_induct [case_names less]: | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 706 | assumes ih: "\<And>B. \<forall>(A::'a multiset). A < B \<longrightarrow> P A \<Longrightarrow> P B" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 707 | shows "P B" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 708 | apply (rule wf_mset_less_rel [THEN wf_induct]) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 709 | apply (rule ih, auto simp: mset_less_rel_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 710 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 711 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 712 | lemma multi_subset_induct [consumes 2, case_names empty add]: | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 713 | assumes "F \<le> A" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 714 |   and empty: "P {#}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 715 |   and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 716 | shows "P F" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 717 | proof - | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 718 | from `F \<le> A` | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 719 | show ?thesis | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 720 | proof (induct F) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 721 |     show "P {#}" by fact
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 722 | next | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 723 | fix x F | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 724 |     assume P: "F \<le> A \<Longrightarrow> P F" and i: "F + {#x#} \<le> A"
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 725 |     show "P (F + {#x#})"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 726 | proof (rule insert) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 727 | from i show "x \<in># A" by (auto dest: mset_le_insertD) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 728 | from i have "F \<le> A" by (auto dest: mset_le_insertD) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 729 | with P show "P F" . | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 730 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 731 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 732 | qed | 
| 26145 | 733 | |
| 17161 | 734 | |
| 48023 | 735 | subsection {* The fold combinator *}
 | 
| 736 | ||
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 737 | definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
 | 
| 48023 | 738 | where | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 739 | "fold f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_of M)" | 
| 48023 | 740 | |
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 741 | lemma fold_mset_empty [simp]: | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 742 |   "fold f s {#} = s"
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 743 | by (simp add: fold_def) | 
| 48023 | 744 | |
| 745 | context comp_fun_commute | |
| 746 | begin | |
| 747 | ||
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 748 | lemma fold_mset_insert: | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 749 |   "fold f s (M + {#x#}) = f x (fold f s M)"
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 750 | proof - | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 751 | interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y" | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 752 | by (fact comp_fun_commute_funpow) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 753 |   interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 754 | by (fact comp_fun_commute_funpow) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 755 | show ?thesis | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 756 | proof (cases "x \<in> set_of M") | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 757 | case False | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 758 |     then have *: "count (M + {#x#}) x = 1" by simp
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 759 |     from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_of M) =
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 760 | Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_of M)" | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 761 | by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 762 | with False * show ?thesis | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 763 | by (simp add: fold_def del: count_union) | 
| 48023 | 764 | next | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 765 | case True | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 766 |     def N \<equiv> "set_of M - {x}"
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 767 | from N_def True have *: "set_of M = insert x N" "x \<notin> N" "finite N" by auto | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 768 |     then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 769 | Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N" | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 770 | by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 771 | with * show ?thesis by (simp add: fold_def del: count_union) simp | 
| 48023 | 772 | qed | 
| 773 | qed | |
| 774 | ||
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 775 | corollary fold_mset_single [simp]: | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 776 |   "fold f s {#x#} = f x s"
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 777 | proof - | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 778 |   have "fold f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 779 | then show ?thesis by simp | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 780 | qed | 
| 48023 | 781 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 782 | lemma fold_mset_fun_left_comm: | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 783 | "f x (fold f s M) = fold f (f x s) M" | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 784 | by (induct M) (simp_all add: fold_mset_insert fun_left_comm) | 
| 48023 | 785 | |
| 786 | lemma fold_mset_union [simp]: | |
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 787 | "fold f s (M + N) = fold f (fold f s M) N" | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 788 | proof (induct M) | 
| 48023 | 789 | case empty then show ?case by simp | 
| 790 | next | |
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 791 | case (add M x) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 792 |   have "M + {#x#} + N = (M + N) + {#x#}"
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 793 | by (simp add: add_ac) | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 794 | with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm) | 
| 48023 | 795 | qed | 
| 796 | ||
| 797 | lemma fold_mset_fusion: | |
| 798 | assumes "comp_fun_commute g" | |
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 799 | shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold g w A) = fold f (h w) A" (is "PROP ?P") | 
| 48023 | 800 | proof - | 
| 801 | interpret comp_fun_commute g by (fact assms) | |
| 802 | show "PROP ?P" by (induct A) auto | |
| 803 | qed | |
| 804 | ||
| 805 | end | |
| 806 | ||
| 807 | text {*
 | |
| 808 | A note on code generation: When defining some function containing a | |
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 809 |   subterm @{term "fold F"}, code generation is not automatic. When
 | 
| 48023 | 810 |   interpreting locale @{text left_commutative} with @{text F}, the
 | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 811 |   would be code thms for @{const fold} become thms like
 | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 812 |   @{term "fold F z {#} = z"} where @{text F} is not a pattern but
 | 
| 48023 | 813 | contains defined symbols, i.e.\ is not a code thm. Hence a separate | 
| 814 |   constant with its own code thms needs to be introduced for @{text
 | |
| 815 | F}. See the image operator below. | |
| 816 | *} | |
| 817 | ||
| 818 | ||
| 819 | subsection {* Image *}
 | |
| 820 | ||
| 821 | definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
 | |
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 822 |   "image_mset f = fold (plus o single o f) {#}"
 | 
| 48023 | 823 | |
| 49823 | 824 | lemma comp_fun_commute_mset_image: | 
| 825 | "comp_fun_commute (plus o single o f)" | |
| 826 | proof | |
| 827 | qed (simp add: add_ac fun_eq_iff) | |
| 48023 | 828 | |
| 829 | lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
 | |
| 49823 | 830 | by (simp add: image_mset_def) | 
| 48023 | 831 | |
| 832 | lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
 | |
| 49823 | 833 | proof - | 
| 834 | interpret comp_fun_commute "plus o single o f" | |
| 835 | by (fact comp_fun_commute_mset_image) | |
| 836 | show ?thesis by (simp add: image_mset_def) | |
| 837 | qed | |
| 48023 | 838 | |
| 839 | lemma image_mset_union [simp]: | |
| 49823 | 840 | "image_mset f (M + N) = image_mset f M + image_mset f N" | 
| 841 | proof - | |
| 842 | interpret comp_fun_commute "plus o single o f" | |
| 843 | by (fact comp_fun_commute_mset_image) | |
| 844 | show ?thesis by (induct N) (simp_all add: image_mset_def add_ac) | |
| 845 | qed | |
| 846 | ||
| 847 | corollary image_mset_insert: | |
| 848 |   "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
 | |
| 849 | by simp | |
| 48023 | 850 | |
| 49823 | 851 | lemma set_of_image_mset [simp]: | 
| 852 | "set_of (image_mset f M) = image f (set_of M)" | |
| 853 | by (induct M) simp_all | |
| 48040 | 854 | |
| 49823 | 855 | lemma size_image_mset [simp]: | 
| 856 | "size (image_mset f M) = size M" | |
| 857 | by (induct M) simp_all | |
| 48023 | 858 | |
| 49823 | 859 | lemma image_mset_is_empty_iff [simp]: | 
| 860 |   "image_mset f M = {#} \<longleftrightarrow> M = {#}"
 | |
| 861 | by (cases M) auto | |
| 48023 | 862 | |
| 863 | syntax | |
| 864 | "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset" | |
| 865 |       ("({#_/. _ :# _#})")
 | |
| 866 | translations | |
| 867 |   "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
 | |
| 868 | ||
| 869 | syntax | |
| 870 | "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset" | |
| 871 |       ("({#_/ | _ :# _./ _#})")
 | |
| 872 | translations | |
| 873 |   "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
 | |
| 874 | ||
| 875 | text {*
 | |
| 876 |   This allows to write not just filters like @{term "{#x:#M. x<c#}"}
 | |
| 877 |   but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
 | |
| 878 |   "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
 | |
| 879 |   @{term "{#x+x|x:#M. x<c#}"}.
 | |
| 880 | *} | |
| 881 | ||
| 882 | enriched_type image_mset: image_mset | |
| 883 | proof - | |
| 884 | fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)" | |
| 885 | proof | |
| 886 | fix A | |
| 887 | show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A" | |
| 888 | by (induct A) simp_all | |
| 889 | qed | |
| 890 | show "image_mset id = id" | |
| 891 | proof | |
| 892 | fix A | |
| 893 | show "image_mset id A = id A" | |
| 894 | by (induct A) simp_all | |
| 895 | qed | |
| 896 | qed | |
| 897 | ||
| 49717 | 898 | declare image_mset.identity [simp] | 
| 899 | ||
| 48023 | 900 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 901 | subsection {* Further conversions *}
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 902 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 903 | primrec multiset_of :: "'a list \<Rightarrow> 'a multiset" where | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 904 |   "multiset_of [] = {#}" |
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 905 |   "multiset_of (a # x) = multiset_of x + {# a #}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 906 | |
| 37107 | 907 | lemma in_multiset_in_set: | 
| 908 | "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs" | |
| 909 | by (induct xs) simp_all | |
| 910 | ||
| 911 | lemma count_multiset_of: | |
| 912 | "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)" | |
| 913 | by (induct xs) simp_all | |
| 914 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 915 | lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 916 | by (induct x) auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 917 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 918 | lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 919 | by (induct x) auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 920 | |
| 40950 | 921 | lemma set_of_multiset_of[simp]: "set_of (multiset_of x) = set x" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 922 | by (induct x) auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 923 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 924 | lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 925 | by (induct xs) auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 926 | |
| 48012 | 927 | lemma size_multiset_of [simp]: "size (multiset_of xs) = length xs" | 
| 928 | by (induct xs) simp_all | |
| 929 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 930 | lemma multiset_of_append [simp]: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 931 | "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 932 | by (induct xs arbitrary: ys) (auto simp: add_ac) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 933 | |
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 934 | lemma multiset_of_filter: | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 935 |   "multiset_of (filter P xs) = {#x :# multiset_of xs. P x #}"
 | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 936 | by (induct xs) simp_all | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 937 | |
| 40950 | 938 | lemma multiset_of_rev [simp]: | 
| 939 | "multiset_of (rev xs) = multiset_of xs" | |
| 940 | by (induct xs) simp_all | |
| 941 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 942 | lemma surj_multiset_of: "surj multiset_of" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 943 | apply (unfold surj_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 944 | apply (rule allI) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 945 | apply (rule_tac M = y in multiset_induct) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 946 | apply auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 947 | apply (rule_tac x = "x # xa" in exI) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 948 | apply auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 949 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 950 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 951 | lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 952 | by (induct x) auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 953 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 954 | lemma distinct_count_atmost_1: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 955 | "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 956 | apply (induct x, simp, rule iffI, simp_all) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 957 | apply (rule conjI) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 958 | apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 959 | apply (erule_tac x = a in allE, simp, clarify) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 960 | apply (erule_tac x = aa in allE, simp) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 961 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 962 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 963 | lemma multiset_of_eq_setD: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 964 | "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 965 | by (rule) (auto simp add:multiset_eq_iff set_count_greater_0) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 966 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 967 | lemma set_eq_iff_multiset_of_eq_distinct: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 968 | "distinct x \<Longrightarrow> distinct y \<Longrightarrow> | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 969 | (set x = set y) = (multiset_of x = multiset_of y)" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 970 | by (auto simp: multiset_eq_iff distinct_count_atmost_1) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 971 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 972 | lemma set_eq_iff_multiset_of_remdups_eq: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 973 | "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 974 | apply (rule iffI) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 975 | apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1]) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 976 | apply (drule distinct_remdups [THEN distinct_remdups | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 977 | [THEN set_eq_iff_multiset_of_eq_distinct [THEN iffD2]]]) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 978 | apply simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 979 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 980 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 981 | lemma multiset_of_compl_union [simp]: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 982 | "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 983 | by (induct xs) (auto simp: add_ac) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 984 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 985 | lemma count_multiset_of_length_filter: | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 986 | "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 987 | by (induct xs) auto | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 988 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 989 | lemma nth_mem_multiset_of: "i < length ls \<Longrightarrow> (ls ! i) :# multiset_of ls" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 990 | apply (induct ls arbitrary: i) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 991 | apply simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 992 | apply (case_tac i) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 993 | apply auto | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 994 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 995 | |
| 36903 | 996 | lemma multiset_of_remove1[simp]: | 
| 997 |   "multiset_of (remove1 a xs) = multiset_of xs - {#a#}"
 | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 998 | by (induct xs) (auto simp add: multiset_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 999 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1000 | lemma multiset_of_eq_length: | 
| 37107 | 1001 | assumes "multiset_of xs = multiset_of ys" | 
| 1002 | shows "length xs = length ys" | |
| 48012 | 1003 | using assms by (metis size_multiset_of) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1004 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1005 | lemma multiset_of_eq_length_filter: | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1006 | assumes "multiset_of xs = multiset_of ys" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1007 | shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)" | 
| 48012 | 1008 | using assms by (metis count_multiset_of) | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1009 | |
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1010 | lemma fold_multiset_equiv: | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1011 | assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1012 | and equiv: "multiset_of xs = multiset_of ys" | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1013 | shows "List.fold f xs = List.fold f ys" | 
| 46921 | 1014 | using f equiv [symmetric] | 
| 1015 | proof (induct xs arbitrary: ys) | |
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1016 | case Nil then show ?case by simp | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1017 | next | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1018 | case (Cons x xs) | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1019 | then have *: "set ys = set (x # xs)" by (blast dest: multiset_of_eq_setD) | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1020 | have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1021 | by (rule Cons.prems(1)) (simp_all add: *) | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1022 | moreover from * have "x \<in> set ys" by simp | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1023 | ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1024 | moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" by (auto intro: Cons.hyps) | 
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1025 | ultimately show ?case by simp | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1026 | qed | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1027 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1028 | lemma multiset_of_insort [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1029 |   "multiset_of (insort x xs) = multiset_of xs + {#x#}"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1030 | by (induct xs) (simp_all add: ac_simps) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1031 | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1032 | lemma in_multiset_of: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1033 | "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1034 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1035 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1036 | lemma multiset_of_map: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1037 | "multiset_of (map f xs) = image_mset f (multiset_of xs)" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1038 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1039 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1040 | definition multiset_of_set :: "'a set \<Rightarrow> 'a multiset" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1041 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1042 |   "multiset_of_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1043 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1044 | interpretation multiset_of_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1045 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1046 |   "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1047 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1048 |   interpret comp_fun_commute "\<lambda>x M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1049 |   show "folding (\<lambda>x M. {#x#} + M)" by default (fact comp_fun_commute)
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1050 |   from multiset_of_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set" ..
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1051 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1052 | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1053 | lemma count_multiset_of_set [simp]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1054 | "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (multiset_of_set A) x = 1" (is "PROP ?P") | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1055 | "\<not> finite A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?Q") | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1056 | "x \<notin> A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?R") | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1057 | proof - | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1058 |   { fix A
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1059 | assume "x \<notin> A" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1060 | have "count (multiset_of_set A) x = 0" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1061 | proof (cases "finite A") | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1062 | case False then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1063 | next | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1064 | case True from True `x \<notin> A` show ?thesis by (induct A) auto | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1065 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1066 | } note * = this | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1067 | then show "PROP ?P" "PROP ?Q" "PROP ?R" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1068 | by (auto elim!: Set.set_insert) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1069 | qed -- {* TODO: maybe define @{const multiset_of_set} also in terms of @{const Abs_multiset} *}
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1070 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1071 | context linorder | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1072 | begin | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1073 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1074 | definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1075 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1076 | "sorted_list_of_multiset M = fold insort [] M" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1077 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1078 | lemma sorted_list_of_multiset_empty [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1079 |   "sorted_list_of_multiset {#} = []"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1080 | by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1081 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1082 | lemma sorted_list_of_multiset_singleton [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1083 |   "sorted_list_of_multiset {#x#} = [x]"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1084 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1085 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1086 | show ?thesis by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1087 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1088 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1089 | lemma sorted_list_of_multiset_insert [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1090 |   "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1091 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1092 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1093 | show ?thesis by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1094 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1095 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1096 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1097 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1098 | lemma multiset_of_sorted_list_of_multiset [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1099 | "multiset_of (sorted_list_of_multiset M) = M" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1100 | by (induct M) simp_all | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1101 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1102 | lemma sorted_list_of_multiset_multiset_of [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1103 | "sorted_list_of_multiset (multiset_of xs) = sort xs" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1104 | by (induct xs) simp_all | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1105 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1106 | lemma finite_set_of_multiset_of_set: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1107 | assumes "finite A" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1108 | shows "set_of (multiset_of_set A) = A" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1109 | using assms by (induct A) simp_all | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1110 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1111 | lemma infinite_set_of_multiset_of_set: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1112 | assumes "\<not> finite A" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1113 |   shows "set_of (multiset_of_set A) = {}"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1114 | using assms by simp | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1115 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1116 | lemma set_sorted_list_of_multiset [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1117 | "set (sorted_list_of_multiset M) = set_of M" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1118 | by (induct M) (simp_all add: set_insort) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1119 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1120 | lemma sorted_list_of_multiset_of_set [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1121 | "sorted_list_of_multiset (multiset_of_set A) = sorted_list_of_set A" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1122 | by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1123 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1124 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1125 | subsection {* Big operators *}
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1126 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1127 | no_notation times (infixl "*" 70) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1128 | no_notation Groups.one ("1")
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1129 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1130 | locale comm_monoid_mset = comm_monoid | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1131 | begin | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1132 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1133 | definition F :: "'a multiset \<Rightarrow> 'a" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1134 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1135 | eq_fold: "F M = Multiset.fold f 1 M" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1136 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1137 | lemma empty [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1138 |   "F {#} = 1"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1139 | by (simp add: eq_fold) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1140 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1141 | lemma singleton [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1142 |   "F {#x#} = x"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1143 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1144 | interpret comp_fun_commute | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1145 | by default (simp add: fun_eq_iff left_commute) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1146 | show ?thesis by (simp add: eq_fold) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1147 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1148 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1149 | lemma union [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1150 | "F (M + N) = F M * F N" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1151 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1152 | interpret comp_fun_commute f | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1153 | by default (simp add: fun_eq_iff left_commute) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1154 | show ?thesis by (induct N) (simp_all add: left_commute eq_fold) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1155 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1156 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1157 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1158 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1159 | notation times (infixl "*" 70) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1160 | notation Groups.one ("1")
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1161 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1162 | definition (in comm_monoid_add) msetsum :: "'a multiset \<Rightarrow> 'a" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1163 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1164 | "msetsum = comm_monoid_mset.F plus 0" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1165 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1166 | definition (in comm_monoid_mult) msetprod :: "'a multiset \<Rightarrow> 'a" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1167 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1168 | "msetprod = comm_monoid_mset.F times 1" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1169 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1170 | sublocale comm_monoid_add < msetsum!: comm_monoid_mset plus 0 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1171 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1172 | "comm_monoid_mset.F plus 0 = msetsum" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1173 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1174 | show "comm_monoid_mset plus 0" .. | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1175 | from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" .. | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1176 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1177 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1178 | context comm_monoid_add | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1179 | begin | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1180 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1181 | lemma setsum_unfold_msetsum: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1182 | "setsum f A = msetsum (image_mset f (multiset_of_set A))" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1183 | by (cases "finite A") (induct A rule: finite_induct, simp_all) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1184 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1185 | abbreviation msetsum_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1186 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1187 | "msetsum_image f M \<equiv> msetsum (image_mset f M)" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1188 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1189 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1190 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1191 | syntax | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1192 | "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1193 |       ("(3SUM _:#_. _)" [0, 51, 10] 10)
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1194 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1195 | syntax (xsymbols) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1196 | "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1197 |       ("(3\<Sum>_:#_. _)" [0, 51, 10] 10)
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1198 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1199 | syntax (HTML output) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1200 | "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1201 |       ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1202 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1203 | translations | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1204 | "SUM i :# A. b" == "CONST msetsum_image (\<lambda>i. b) A" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1205 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1206 | sublocale comm_monoid_mult < msetprod!: comm_monoid_mset times 1 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1207 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1208 | "comm_monoid_mset.F times 1 = msetprod" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1209 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1210 | show "comm_monoid_mset times 1" .. | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1211 | from msetprod_def show "comm_monoid_mset.F times 1 = msetprod" .. | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1212 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1213 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1214 | context comm_monoid_mult | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1215 | begin | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1216 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1217 | lemma msetprod_empty: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1218 |   "msetprod {#} = 1"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1219 | by (fact msetprod.empty) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1220 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1221 | lemma msetprod_singleton: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1222 |   "msetprod {#x#} = x"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1223 | by (fact msetprod.singleton) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1224 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1225 | lemma msetprod_Un: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1226 | "msetprod (A + B) = msetprod A * msetprod B" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1227 | by (fact msetprod.union) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1228 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1229 | lemma setprod_unfold_msetprod: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1230 | "setprod f A = msetprod (image_mset f (multiset_of_set A))" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1231 | by (cases "finite A") (induct A rule: finite_induct, simp_all) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1232 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1233 | lemma msetprod_multiplicity: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1234 | "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_of M)" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1235 | by (simp add: Multiset.fold_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1236 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1237 | abbreviation msetprod_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1238 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1239 | "msetprod_image f M \<equiv> msetprod (image_mset f M)" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1240 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1241 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1242 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1243 | syntax | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1244 | "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1245 |       ("(3PROD _:#_. _)" [0, 51, 10] 10)
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1246 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1247 | syntax (xsymbols) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1248 | "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1249 |       ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1250 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1251 | syntax (HTML output) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1252 | "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1253 |       ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1254 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1255 | translations | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1256 | "PROD i :# A. b" == "CONST msetprod_image (\<lambda>i. b) A" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1257 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1258 | lemma (in comm_semiring_1) dvd_msetprod: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1259 | assumes "x \<in># A" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1260 | shows "x dvd msetprod A" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1261 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1262 |   from assms have "A = (A - {#x#}) + {#x#}" by simp
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1263 |   then obtain B where "A = B + {#x#}" ..
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1264 | then show ?thesis by simp | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1265 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1266 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1267 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1268 | subsection {* Cardinality *}
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1269 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1270 | definition mcard :: "'a multiset \<Rightarrow> nat" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1271 | where | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1272 | "mcard = msetsum \<circ> image_mset (\<lambda>_. 1)" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1273 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1274 | lemma mcard_empty [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1275 |   "mcard {#} = 0"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1276 | by (simp add: mcard_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1277 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1278 | lemma mcard_singleton [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1279 |   "mcard {#a#} = Suc 0"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1280 | by (simp add: mcard_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1281 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1282 | lemma mcard_plus [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1283 | "mcard (M + N) = mcard M + mcard N" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1284 | by (simp add: mcard_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1285 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1286 | lemma mcard_empty_iff [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1287 |   "mcard M = 0 \<longleftrightarrow> M = {#}"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1288 | by (induct M) simp_all | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1289 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1290 | lemma mcard_unfold_setsum: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1291 | "mcard M = setsum (count M) (set_of M)" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1292 | proof (induct M) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1293 | case empty then show ?case by simp | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1294 | next | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1295 | case (add M x) then show ?case | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1296 | by (cases "x \<in> set_of M") | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1297 | (simp_all del: mem_set_of_iff add: setsum.distrib setsum.delta' insert_absorb, simp) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1298 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1299 | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1300 | lemma size_eq_mcard: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1301 | "size = mcard" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1302 | by (simp add: fun_eq_iff size_def mcard_unfold_setsum) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1303 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1304 | lemma mcard_multiset_of: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1305 | "mcard (multiset_of xs) = length xs" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1306 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1307 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1308 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1309 | subsection {* Alternative representations *}
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1310 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1311 | subsubsection {* Lists *}
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1312 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1313 | context linorder | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1314 | begin | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1315 | |
| 40210 
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
 haftmann parents: 
39533diff
changeset | 1316 | lemma multiset_of_insort [simp]: | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1317 |   "multiset_of (insort_key k x xs) = {#x#} + multiset_of xs"
 | 
| 37107 | 1318 | by (induct xs) (simp_all add: ac_simps) | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1319 | |
| 40210 
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
 haftmann parents: 
39533diff
changeset | 1320 | lemma multiset_of_sort [simp]: | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1321 | "multiset_of (sort_key k xs) = multiset_of xs" | 
| 37107 | 1322 | by (induct xs) (simp_all add: ac_simps) | 
| 1323 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1324 | text {*
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1325 | This lemma shows which properties suffice to show that a function | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1326 |   @{text "f"} with @{text "f xs = ys"} behaves like sort.
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1327 | *} | 
| 37074 | 1328 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1329 | lemma properties_for_sort_key: | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1330 | assumes "multiset_of ys = multiset_of xs" | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1331 | and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs" | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1332 | and "sorted (map f ys)" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1333 | shows "sort_key f xs = ys" | 
| 46921 | 1334 | using assms | 
| 1335 | proof (induct xs arbitrary: ys) | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1336 | case Nil then show ?case by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1337 | next | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1338 | case (Cons x xs) | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1339 | from Cons.prems(2) have | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1340 | "\<forall>k \<in> set ys. filter (\<lambda>x. f k = f x) (remove1 x ys) = filter (\<lambda>x. f k = f x) xs" | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1341 | by (simp add: filter_remove1) | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1342 | with Cons.prems have "sort_key f xs = remove1 x ys" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1343 | by (auto intro!: Cons.hyps simp add: sorted_map_remove1) | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1344 | moreover from Cons.prems have "x \<in> set ys" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1345 | by (auto simp add: mem_set_multiset_eq intro!: ccontr) | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1346 | ultimately show ?case using Cons.prems by (simp add: insort_key_remove1) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1347 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1348 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1349 | lemma properties_for_sort: | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1350 | assumes multiset: "multiset_of ys = multiset_of xs" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1351 | and "sorted ys" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1352 | shows "sort xs = ys" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1353 | proof (rule properties_for_sort_key) | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1354 | from multiset show "multiset_of ys = multiset_of xs" . | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1355 | from `sorted ys` show "sorted (map (\<lambda>x. x) ys)" by simp | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1356 | from multiset have "\<And>k. length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1357 | by (rule multiset_of_eq_length_filter) | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1358 | then have "\<And>k. replicate (length (filter (\<lambda>y. k = y) ys)) k = replicate (length (filter (\<lambda>x. k = x) xs)) k" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1359 | by simp | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1360 | then show "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs" | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1361 | by (simp add: replicate_length_filter) | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1362 | qed | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1363 | |
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1364 | lemma sort_key_by_quicksort: | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1365 | "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))] | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1366 | @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))] | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1367 | @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs") | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1368 | proof (rule properties_for_sort_key) | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1369 | show "multiset_of ?rhs = multiset_of ?lhs" | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1370 | by (rule multiset_eqI) (auto simp add: multiset_of_filter) | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1371 | next | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1372 | show "sorted (map f ?rhs)" | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1373 | by (auto simp add: sorted_append intro: sorted_map_same) | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1374 | next | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1375 | fix l | 
| 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1376 | assume "l \<in> set ?rhs" | 
| 40346 | 1377 | let ?pivot = "f (xs ! (length xs div 2))" | 
| 1378 | have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto | |
| 40306 | 1379 | have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]" | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1380 | unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same) | 
| 40346 | 1381 | with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp | 
| 1382 | have "\<And>x P. P (f x) ?pivot \<and> f l = f x \<longleftrightarrow> P (f l) ?pivot \<and> f l = f x" by auto | |
| 1383 | then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] = | |
| 1384 | [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp | |
| 1385 | note *** = this [of "op <"] this [of "op >"] this [of "op ="] | |
| 40306 | 1386 | show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]" | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1387 | proof (cases "f l" ?pivot rule: linorder_cases) | 
| 46730 | 1388 | case less | 
| 1389 | then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto | |
| 1390 | with less show ?thesis | |
| 40346 | 1391 | by (simp add: filter_sort [symmetric] ** ***) | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1392 | next | 
| 40306 | 1393 | case equal then show ?thesis | 
| 40346 | 1394 | by (simp add: * less_le) | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 1395 | next | 
| 46730 | 1396 | case greater | 
| 1397 | then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto | |
| 1398 | with greater show ?thesis | |
| 40346 | 1399 | by (simp add: filter_sort [symmetric] ** ***) | 
| 40306 | 1400 | qed | 
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1401 | qed | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1402 | |
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1403 | lemma sort_by_quicksort: | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1404 | "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)] | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1405 | @ [x\<leftarrow>xs. x = xs ! (length xs div 2)] | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1406 | @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs") | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1407 | using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1408 | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1409 | text {* A stable parametrized quicksort *}
 | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1410 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1411 | definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
 | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1412 | "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1413 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1414 | lemma part_code [code]: | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1415 | "part f pivot [] = ([], [], [])" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1416 | "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1417 | if x' < pivot then (x # lts, eqs, gts) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1418 | else if x' > pivot then (lts, eqs, x # gts) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1419 | else (lts, x # eqs, gts))" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1420 | by (auto simp add: part_def Let_def split_def) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1421 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1422 | lemma sort_key_by_quicksort_code [code]: | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1423 | "sort_key f xs = (case xs of [] \<Rightarrow> [] | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1424 | | [x] \<Rightarrow> xs | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1425 | | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x]) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1426 | | _ \<Rightarrow> (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1427 | in sort_key f lts @ eqs @ sort_key f gts))" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1428 | proof (cases xs) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1429 | case Nil then show ?thesis by simp | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1430 | next | 
| 46921 | 1431 | case (Cons _ ys) note hyps = Cons show ?thesis | 
| 1432 | proof (cases ys) | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1433 | case Nil with hyps show ?thesis by simp | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1434 | next | 
| 46921 | 1435 | case (Cons _ zs) note hyps = hyps Cons show ?thesis | 
| 1436 | proof (cases zs) | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1437 | case Nil with hyps show ?thesis by auto | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1438 | next | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1439 | case Cons | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1440 | from sort_key_by_quicksort [of f xs] | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1441 | have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1442 | in sort_key f lts @ eqs @ sort_key f gts)" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1443 | by (simp only: split_def Let_def part_def fst_conv snd_conv) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1444 | with hyps Cons show ?thesis by (simp only: list.cases) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1445 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1446 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1447 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1448 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1449 | end | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1450 | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1451 | hide_const (open) part | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 1452 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1453 | lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le> multiset_of xs" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1454 | by (induct xs) (auto intro: order_trans) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1455 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1456 | lemma multiset_of_update: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1457 |   "i < length ls \<Longrightarrow> multiset_of (ls[i := v]) = multiset_of ls - {#ls ! i#} + {#v#}"
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1458 | proof (induct ls arbitrary: i) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1459 | case Nil then show ?case by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1460 | next | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1461 | case (Cons x xs) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1462 | show ?case | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1463 | proof (cases i) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1464 | case 0 then show ?thesis by simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1465 | next | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1466 | case (Suc i') | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1467 | with Cons show ?thesis | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1468 | apply simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1469 | apply (subst add_assoc) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1470 |       apply (subst add_commute [of "{#v#}" "{#x#}"])
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1471 | apply (subst add_assoc [symmetric]) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1472 | apply simp | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1473 | apply (rule mset_le_multiset_union_diff_commute) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1474 | apply (simp add: mset_le_single nth_mem_multiset_of) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1475 | done | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1476 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1477 | qed | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1478 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1479 | lemma multiset_of_swap: | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1480 | "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow> | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1481 | multiset_of (ls[j := ls ! i, i := ls ! j]) = multiset_of ls" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1482 | by (cases "i = j") (simp_all add: multiset_of_update nth_mem_multiset_of) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1483 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1484 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1485 | subsection {* The multiset order *}
 | 
| 10249 | 1486 | |
| 1487 | subsubsection {* Well-foundedness *}
 | |
| 1488 | ||
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 1489 | definition mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
 | 
| 37765 | 1490 |   "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
 | 
| 23751 | 1491 | (\<forall>b. b :# K --> (b, a) \<in> r)}" | 
| 10249 | 1492 | |
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 1493 | definition mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
 | 
| 37765 | 1494 | "mult r = (mult1 r)\<^sup>+" | 
| 10249 | 1495 | |
| 23751 | 1496 | lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
 | 
| 26178 | 1497 | by (simp add: mult1_def) | 
| 10249 | 1498 | |
| 23751 | 1499 | lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
 | 
| 1500 |     (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
 | |
| 1501 | (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)" | |
| 19582 | 1502 | (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2") | 
| 10249 | 1503 | proof (unfold mult1_def) | 
| 23751 | 1504 | let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r" | 
| 11464 | 1505 |   let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
 | 
| 23751 | 1506 |   let ?case1 = "?case1 {(N, M). ?R N M}"
 | 
| 10249 | 1507 | |
| 23751 | 1508 |   assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
 | 
| 18258 | 1509 | then have "\<exists>a' M0' K. | 
| 11464 | 1510 |       M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
 | 
| 18258 | 1511 | then show "?case1 \<or> ?case2" | 
| 10249 | 1512 | proof (elim exE conjE) | 
| 1513 | fix a' M0' K | |
| 1514 | assume N: "N = M0' + K" and r: "?r K a'" | |
| 1515 |     assume "M0 + {#a#} = M0' + {#a'#}"
 | |
| 18258 | 1516 | then have "M0 = M0' \<and> a = a' \<or> | 
| 11464 | 1517 |         (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
 | 
| 10249 | 1518 | by (simp only: add_eq_conv_ex) | 
| 18258 | 1519 | then show ?thesis | 
| 10249 | 1520 | proof (elim disjE conjE exE) | 
| 1521 | assume "M0 = M0'" "a = a'" | |
| 11464 | 1522 | with N r have "?r K a \<and> N = M0 + K" by simp | 
| 18258 | 1523 | then have ?case2 .. then show ?thesis .. | 
| 10249 | 1524 | next | 
| 1525 | fix K' | |
| 1526 |       assume "M0' = K' + {#a#}"
 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1527 |       with N have n: "N = K' + K + {#a#}" by (simp add: add_ac)
 | 
| 10249 | 1528 | |
| 1529 |       assume "M0 = K' + {#a'#}"
 | |
| 1530 | with r have "?R (K' + K) M0" by blast | |
| 18258 | 1531 | with n have ?case1 by simp then show ?thesis .. | 
| 10249 | 1532 | qed | 
| 1533 | qed | |
| 1534 | qed | |
| 1535 | ||
| 23751 | 1536 | lemma all_accessible: "wf r ==> \<forall>M. M \<in> acc (mult1 r)" | 
| 10249 | 1537 | proof | 
| 1538 | let ?R = "mult1 r" | |
| 1539 | let ?W = "acc ?R" | |
| 1540 |   {
 | |
| 1541 | fix M M0 a | |
| 23751 | 1542 | assume M0: "M0 \<in> ?W" | 
| 1543 |       and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
 | |
| 1544 |       and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
 | |
| 1545 |     have "M0 + {#a#} \<in> ?W"
 | |
| 1546 |     proof (rule accI [of "M0 + {#a#}"])
 | |
| 10249 | 1547 | fix N | 
| 23751 | 1548 |       assume "(N, M0 + {#a#}) \<in> ?R"
 | 
| 1549 |       then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
 | |
| 1550 | (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))" | |
| 10249 | 1551 | by (rule less_add) | 
| 23751 | 1552 | then show "N \<in> ?W" | 
| 10249 | 1553 | proof (elim exE disjE conjE) | 
| 23751 | 1554 |         fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
 | 
| 1555 |         from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
 | |
| 1556 |         from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
 | |
| 1557 | then show "N \<in> ?W" by (simp only: N) | |
| 10249 | 1558 | next | 
| 1559 | fix K | |
| 1560 | assume N: "N = M0 + K" | |
| 23751 | 1561 | assume "\<forall>b. b :# K --> (b, a) \<in> r" | 
| 1562 | then have "M0 + K \<in> ?W" | |
| 10249 | 1563 | proof (induct K) | 
| 18730 | 1564 | case empty | 
| 23751 | 1565 |           from M0 show "M0 + {#} \<in> ?W" by simp
 | 
| 18730 | 1566 | next | 
| 1567 | case (add K x) | |
| 23751 | 1568 | from add.prems have "(x, a) \<in> r" by simp | 
| 1569 |           with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
 | |
| 1570 | moreover from add have "M0 + K \<in> ?W" by simp | |
| 1571 |           ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1572 |           then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add_assoc)
 | 
| 10249 | 1573 | qed | 
| 23751 | 1574 | then show "N \<in> ?W" by (simp only: N) | 
| 10249 | 1575 | qed | 
| 1576 | qed | |
| 1577 | } note tedious_reasoning = this | |
| 1578 | ||
| 23751 | 1579 | assume wf: "wf r" | 
| 10249 | 1580 | fix M | 
| 23751 | 1581 | show "M \<in> ?W" | 
| 10249 | 1582 | proof (induct M) | 
| 23751 | 1583 |     show "{#} \<in> ?W"
 | 
| 10249 | 1584 | proof (rule accI) | 
| 23751 | 1585 |       fix b assume "(b, {#}) \<in> ?R"
 | 
| 1586 | with not_less_empty show "b \<in> ?W" by contradiction | |
| 10249 | 1587 | qed | 
| 1588 | ||
| 23751 | 1589 | fix M a assume "M \<in> ?W" | 
| 1590 |     from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
 | |
| 10249 | 1591 | proof induct | 
| 1592 | fix a | |
| 23751 | 1593 |       assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
 | 
| 1594 |       show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
 | |
| 10249 | 1595 | proof | 
| 23751 | 1596 | fix M assume "M \<in> ?W" | 
| 1597 |         then show "M + {#a#} \<in> ?W"
 | |
| 23373 | 1598 | by (rule acc_induct) (rule tedious_reasoning [OF _ r]) | 
| 10249 | 1599 | qed | 
| 1600 | qed | |
| 23751 | 1601 |     from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
 | 
| 10249 | 1602 | qed | 
| 1603 | qed | |
| 1604 | ||
| 23751 | 1605 | theorem wf_mult1: "wf r ==> wf (mult1 r)" | 
| 26178 | 1606 | by (rule acc_wfI) (rule all_accessible) | 
| 10249 | 1607 | |
| 23751 | 1608 | theorem wf_mult: "wf r ==> wf (mult r)" | 
| 26178 | 1609 | unfolding mult_def by (rule wf_trancl) (rule wf_mult1) | 
| 10249 | 1610 | |
| 1611 | ||
| 1612 | subsubsection {* Closure-free presentation *}
 | |
| 1613 | ||
| 1614 | text {* One direction. *}
 | |
| 1615 | ||
| 1616 | lemma mult_implies_one_step: | |
| 23751 | 1617 | "trans r ==> (M, N) \<in> mult r ==> | 
| 11464 | 1618 |     \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
 | 
| 23751 | 1619 | (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)" | 
| 26178 | 1620 | apply (unfold mult_def mult1_def set_of_def) | 
| 1621 | apply (erule converse_trancl_induct, clarify) | |
| 1622 | apply (rule_tac x = M0 in exI, simp, clarify) | |
| 1623 | apply (case_tac "a :# K") | |
| 1624 | apply (rule_tac x = I in exI) | |
| 1625 | apply (simp (no_asm)) | |
| 1626 |  apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1627 | apply (simp (no_asm_simp) add: add_assoc [symmetric]) | 
| 26178 | 1628 |  apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong)
 | 
| 1629 | apply (simp add: diff_union_single_conv) | |
| 1630 | apply (simp (no_asm_use) add: trans_def) | |
| 1631 | apply blast | |
| 1632 | apply (subgoal_tac "a :# I") | |
| 1633 |  apply (rule_tac x = "I - {#a#}" in exI)
 | |
| 1634 |  apply (rule_tac x = "J + {#a#}" in exI)
 | |
| 1635 | apply (rule_tac x = "K + Ka" in exI) | |
| 1636 | apply (rule conjI) | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 1637 | apply (simp add: multiset_eq_iff split: nat_diff_split) | 
| 26178 | 1638 | apply (rule conjI) | 
| 1639 |   apply (drule_tac f = "\<lambda>M. M - {#a#}" in arg_cong, simp)
 | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 1640 | apply (simp add: multiset_eq_iff split: nat_diff_split) | 
| 26178 | 1641 | apply (simp (no_asm_use) add: trans_def) | 
| 1642 | apply blast | |
| 1643 | apply (subgoal_tac "a :# (M0 + {#a#})")
 | |
| 1644 | apply simp | |
| 1645 | apply (simp (no_asm)) | |
| 1646 | done | |
| 10249 | 1647 | |
| 1648 | lemma one_step_implies_mult_aux: | |
| 23751 | 1649 | "trans r ==> | 
| 1650 |     \<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))
 | |
| 1651 | --> (I + K, I + J) \<in> mult r" | |
| 26178 | 1652 | apply (induct_tac n, auto) | 
| 1653 | apply (frule size_eq_Suc_imp_eq_union, clarify) | |
| 1654 | apply (rename_tac "J'", simp) | |
| 1655 | apply (erule notE, auto) | |
| 1656 | apply (case_tac "J' = {#}")
 | |
| 1657 | apply (simp add: mult_def) | |
| 1658 | apply (rule r_into_trancl) | |
| 1659 | apply (simp add: mult1_def set_of_def, blast) | |
| 1660 | txt {* Now we know @{term "J' \<noteq> {#}"}. *}
 | |
| 1661 | apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition) | |
| 1662 | apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp) | |
| 1663 | apply (erule ssubst) | |
| 1664 | apply (simp add: Ball_def, auto) | |
| 1665 | apply (subgoal_tac | |
| 1666 |   "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
 | |
| 1667 |     (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
 | |
| 1668 | prefer 2 | |
| 1669 | apply force | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1670 | apply (simp (no_asm_use) add: add_assoc [symmetric] mult_def) | 
| 26178 | 1671 | apply (erule trancl_trans) | 
| 1672 | apply (rule r_into_trancl) | |
| 1673 | apply (simp add: mult1_def set_of_def) | |
| 1674 | apply (rule_tac x = a in exI) | |
| 1675 | apply (rule_tac x = "I + J'" in exI) | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1676 | apply (simp add: add_ac) | 
| 26178 | 1677 | done | 
| 10249 | 1678 | |
| 17161 | 1679 | lemma one_step_implies_mult: | 
| 23751 | 1680 |   "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
 | 
| 1681 | ==> (I + K, I + J) \<in> mult r" | |
| 26178 | 1682 | using one_step_implies_mult_aux by blast | 
| 10249 | 1683 | |
| 1684 | ||
| 1685 | subsubsection {* Partial-order properties *}
 | |
| 1686 | ||
| 35273 | 1687 | definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where | 
| 1688 |   "M' <# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
 | |
| 10249 | 1689 | |
| 35273 | 1690 | definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where | 
| 1691 | "M' <=# M \<longleftrightarrow> M' <# M \<or> M' = M" | |
| 1692 | ||
| 35308 | 1693 | notation (xsymbols) less_multiset (infix "\<subset>#" 50) | 
| 1694 | notation (xsymbols) le_multiset (infix "\<subseteq>#" 50) | |
| 10249 | 1695 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1696 | interpretation multiset_order: order le_multiset less_multiset | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1697 | proof - | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1698 | have irrefl: "\<And>M :: 'a multiset. \<not> M \<subset># M" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1699 | proof | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1700 | fix M :: "'a multiset" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1701 | assume "M \<subset># M" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1702 |     then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def)
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1703 |     have "trans {(x'::'a, x). x' < x}"
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1704 | by (rule transI) simp | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1705 | moreover note MM | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1706 | ultimately have "\<exists>I J K. M = I + J \<and> M = I + K | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1707 |       \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})"
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1708 | by (rule mult_implies_one_step) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1709 | then obtain I J K where "M = I + J" and "M = I + K" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1710 |       and "J \<noteq> {#}" and "(\<forall>k\<in>set_of K. \<exists>j\<in>set_of J. (k, j) \<in> {(x, y). x < y})" by blast
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1711 |     then have aux1: "K \<noteq> {#}" and aux2: "\<forall>k\<in>set_of K. \<exists>j\<in>set_of K. k < j" by auto
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1712 | have "finite (set_of K)" by simp | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1713 | moreover note aux2 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1714 |     ultimately have "set_of K = {}"
 | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1715 | by (induct rule: finite_induct) (auto intro: order_less_trans) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1716 | with aux1 show False by simp | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1717 | qed | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1718 | have trans: "\<And>K M N :: 'a multiset. K \<subset># M \<Longrightarrow> M \<subset># N \<Longrightarrow> K \<subset># N" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1719 | unfolding less_multiset_def mult_def by (blast intro: trancl_trans) | 
| 46921 | 1720 | show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset" | 
| 1721 | by default (auto simp add: le_multiset_def irrefl dest: trans) | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1722 | qed | 
| 10249 | 1723 | |
| 46730 | 1724 | lemma mult_less_irrefl [elim!]: "M \<subset># (M::'a::order multiset) ==> R" | 
| 1725 | by simp | |
| 26567 
7bcebb8c2d33
instantiation replacing primitive instance plus overloaded defs; more conservative type arities
 haftmann parents: 
26178diff
changeset | 1726 | |
| 10249 | 1727 | |
| 1728 | subsubsection {* Monotonicity of multiset union *}
 | |
| 1729 | ||
| 46730 | 1730 | lemma mult1_union: "(B, D) \<in> mult1 r ==> (C + B, C + D) \<in> mult1 r" | 
| 26178 | 1731 | apply (unfold mult1_def) | 
| 1732 | apply auto | |
| 1733 | apply (rule_tac x = a in exI) | |
| 1734 | apply (rule_tac x = "C + M0" in exI) | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1735 | apply (simp add: add_assoc) | 
| 26178 | 1736 | done | 
| 10249 | 1737 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1738 | lemma union_less_mono2: "B \<subset># D ==> C + B \<subset># C + (D::'a::order multiset)" | 
| 26178 | 1739 | apply (unfold less_multiset_def mult_def) | 
| 1740 | apply (erule trancl_induct) | |
| 40249 
cd404ecb9107
Remove unnecessary premise of mult1_union
 Lars Noschinski <noschinl@in.tum.de> parents: 
39533diff
changeset | 1741 | apply (blast intro: mult1_union) | 
| 
cd404ecb9107
Remove unnecessary premise of mult1_union
 Lars Noschinski <noschinl@in.tum.de> parents: 
39533diff
changeset | 1742 | apply (blast intro: mult1_union trancl_trans) | 
| 26178 | 1743 | done | 
| 10249 | 1744 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1745 | lemma union_less_mono1: "B \<subset># D ==> B + C \<subset># D + (C::'a::order multiset)" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1746 | apply (subst add_commute [of B C]) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1747 | apply (subst add_commute [of D C]) | 
| 26178 | 1748 | apply (erule union_less_mono2) | 
| 1749 | done | |
| 10249 | 1750 | |
| 17161 | 1751 | lemma union_less_mono: | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1752 | "A \<subset># C ==> B \<subset># D ==> A + B \<subset># C + (D::'a::order multiset)" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1753 | by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans) | 
| 10249 | 1754 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1755 | interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1756 | proof | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1757 | qed (auto simp add: le_multiset_def intro: union_less_mono2) | 
| 26145 | 1758 | |
| 15072 | 1759 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1760 | subsection {* Termination proofs with multiset orders *}
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1761 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1762 | 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: 
28823diff
changeset | 1763 |   and multi_member_this: "x \<in># {# x #} + XS"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1764 |   and multi_member_last: "x \<in># {# x #}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1765 | by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1766 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1767 | definition "ms_strict = mult pair_less" | 
| 37765 | 1768 | definition "ms_weak = ms_strict \<union> Id" | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1769 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1770 | 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: 
28823diff
changeset | 1771 | 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: 
28823diff
changeset | 1772 | 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: 
28823diff
changeset | 1773 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1774 | lemma smsI: | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1775 | "(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: 
28823diff
changeset | 1776 | unfolding ms_strict_def | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1777 | 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: 
28823diff
changeset | 1778 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1779 | lemma wmsI: | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1780 |   "(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: 
28823diff
changeset | 1781 | \<Longrightarrow> (Z + A, Z + B) \<in> ms_weak" | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1782 | unfolding ms_weak_def ms_strict_def | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1783 | 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: 
28823diff
changeset | 1784 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1785 | inductive pw_leq | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1786 | where | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1787 |   pw_leq_empty: "pw_leq {#} {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1788 | | 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: 
28823diff
changeset | 1789 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1790 | lemma pw_leq_lstep: | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1791 |   "(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: 
28823diff
changeset | 1792 | 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: 
28823diff
changeset | 1793 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1794 | lemma pw_leq_split: | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1795 | assumes "pw_leq X Y" | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1796 |   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: 
28823diff
changeset | 1797 | using assms | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1798 | proof (induct) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1799 | case pw_leq_empty thus ?case by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1800 | next | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1801 | case (pw_leq_step x y X Y) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1802 | then obtain A B Z where | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1803 | [simp]: "X = A + Z" "Y = B + Z" | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1804 |       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: 
28823diff
changeset | 1805 | by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1806 | 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: 
28823diff
changeset | 1807 | unfolding pair_leq_def by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1808 | thus ?case | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1809 | proof | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1810 | assume [simp]: "x = y" | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1811 | have | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1812 |       "{#x#} + X = A + ({#y#}+Z) 
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1813 |       \<and> {#y#} + Y = B + ({#y#}+Z)
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1814 |       \<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: 
28823diff
changeset | 1815 | by (auto simp: add_ac) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1816 | thus ?case by (intro exI) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1817 | next | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1818 | assume A: "(x, y) \<in> pair_less" | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1819 |     let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1820 |     have "{#x#} + X = ?A' + Z"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1821 |       "{#y#} + Y = ?B' + Z"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1822 | by (auto simp add: add_ac) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1823 | moreover have | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1824 | "(set_of ?A', set_of ?B') \<in> max_strict" | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1825 | using 1 A unfolding max_strict_def | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1826 | by (auto elim!: max_ext.cases) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1827 | ultimately show ?thesis by blast | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1828 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1829 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1830 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1831 | lemma | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1832 | assumes pwleq: "pw_leq Z Z'" | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1833 | 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: 
28823diff
changeset | 1834 | 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: 
28823diff
changeset | 1835 |   and   ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1836 | proof - | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1837 | from pw_leq_split[OF pwleq] | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1838 | obtain A' B' Z'' | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1839 | where [simp]: "Z = A' + Z''" "Z' = B' + Z''" | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1840 |     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: 
28823diff
changeset | 1841 | by blast | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1842 |   {
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1843 | 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: 
28823diff
changeset | 1844 | from mx_or_empty | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1845 | 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: 
28823diff
changeset | 1846 | proof | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1847 | 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: 
28823diff
changeset | 1848 | 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: 
28823diff
changeset | 1849 | 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: 
28823diff
changeset | 1850 | thus ?thesis by (rule smsI) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1851 | next | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1852 |       assume [simp]: "A' = {#} \<and> B' = {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1853 | show ?thesis by (rule smsI) (auto intro: max) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1854 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1855 | 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: 
28823diff
changeset | 1856 | 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: 
28823diff
changeset | 1857 | } | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1858 | from mx_or_empty | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1859 | 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: 
28823diff
changeset | 1860 |   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: 
28823diff
changeset | 1861 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1862 | |
| 39301 | 1863 | lemma empty_neutral: "{#} + x = x" "x + {#} = x"
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1864 | and nonempty_plus: "{# x #} + rs \<noteq> {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1865 | and nonempty_single: "{# x #} \<noteq> {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1866 | by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1867 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1868 | setup {*
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1869 | let | 
| 35402 | 1870 |   fun msetT T = Type (@{type_name multiset}, [T]);
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1871 | |
| 35402 | 1872 |   fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1873 |     | 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: 
28823diff
changeset | 1874 | | mk_mset T (x :: xs) = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1875 |           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: 
28823diff
changeset | 1876 | mk_mset T [x] $ mk_mset T xs | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1877 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1878 | fun mset_member_tac m i = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1879 | (if m <= 0 then | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1880 |            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: 
28823diff
changeset | 1881 | else | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1882 |            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: 
28823diff
changeset | 1883 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1884 | val mset_nonempty_tac = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1885 |       rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1886 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1887 | val regroup_munion_conv = | 
| 35402 | 1888 |       Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
 | 
| 39301 | 1889 |         (map (fn t => t RS eq_reflection) (@{thms add_ac} @ @{thms empty_neutral}))
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1890 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1891 | fun unfold_pwleq_tac i = | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1892 |     (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: 
28823diff
changeset | 1893 |       ORELSE (rtac @{thm pw_leq_lstep} i)
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1894 |       ORELSE (rtac @{thm pw_leq_empty} i)
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1895 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1896 |   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: 
28823diff
changeset | 1897 |                       @{thm Un_insert_left}, @{thm Un_empty_left}]
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1898 | in | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1899 | ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1900 |   {
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1901 | 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: 
28823diff
changeset | 1902 | 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: 
28823diff
changeset | 1903 | mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_of_simps, | 
| 30595 
c87a3350f5a9
proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
 wenzelm parents: 
30428diff
changeset | 1904 |     smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
 | 
| 
c87a3350f5a9
proper spacing before ML antiquotations -- note that @ may be part of symbolic ML identifiers;
 wenzelm parents: 
30428diff
changeset | 1905 |     reduction_pair= @{thm ms_reduction_pair}
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1906 | }) | 
| 10249 | 1907 | end | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1908 | *} | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 1909 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1910 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1911 | subsection {* Legacy theorem bindings *}
 | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1912 | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 1913 | lemmas multi_count_eq = multiset_eq_iff [symmetric] | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1914 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1915 | lemma union_commute: "M + N = N + (M::'a multiset)" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1916 | by (fact add_commute) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1917 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1918 | lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1919 | by (fact add_assoc) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1920 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1921 | lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1922 | by (fact add_left_commute) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1923 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1924 | lemmas union_ac = union_assoc union_commute union_lcomm | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1925 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1926 | lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1927 | by (fact add_right_cancel) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1928 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1929 | lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1930 | by (fact add_left_cancel) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1931 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1932 | lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y" | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1933 | by (fact add_imp_eq) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1934 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1935 | lemma mset_less_trans: "(M::'a multiset) < K \<Longrightarrow> K < N \<Longrightarrow> M < N" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1936 | by (fact order_less_trans) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1937 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1938 | lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1939 | by (fact inf.commute) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1940 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1941 | lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1942 | by (fact inf.assoc [symmetric]) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1943 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1944 | lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1945 | by (fact inf.left_commute) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1946 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1947 | lemmas multiset_inter_ac = | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1948 | multiset_inter_commute | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1949 | multiset_inter_assoc | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1950 | multiset_inter_left_commute | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1951 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1952 | lemma mult_less_not_refl: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1953 | "\<not> M \<subset># (M::'a::order multiset)" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1954 | by (fact multiset_order.less_irrefl) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1955 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1956 | lemma mult_less_trans: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1957 | "K \<subset># M ==> M \<subset># N ==> K \<subset># (N::'a::order multiset)" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1958 | by (fact multiset_order.less_trans) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1959 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1960 | lemma mult_less_not_sym: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1961 | "M \<subset># N ==> \<not> N \<subset># (M::'a::order multiset)" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1962 | by (fact multiset_order.less_not_sym) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1963 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1964 | lemma mult_less_asym: | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1965 | "M \<subset># N ==> (\<not> P ==> N \<subset># (M::'a::order multiset)) ==> P" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1966 | by (fact multiset_order.less_asym) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1967 | |
| 35712 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1968 | ML {*
 | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1969 | fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T])) | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1970 | (Const _ $ t') = | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1971 | let | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1972 | val (maybe_opt, ps) = | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1973 | Nitpick_Model.dest_plain_fun t' ||> op ~~ | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1974 | ||> map (apsnd (snd o HOLogic.dest_number)) | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1975 | fun elems_for t = | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1976 | case AList.lookup (op =) ps t of | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1977 | SOME n => replicate n t | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1978 | | NONE => [Const (maybe_name, elem_T --> elem_T) $ t] | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1979 | in | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1980 | case maps elems_for (all_values elem_T) @ | 
| 37261 | 1981 | (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)] | 
| 1982 | else []) of | |
| 35712 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1983 |         [] => Const (@{const_name zero_class.zero}, T)
 | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1984 | | ts => foldl1 (fn (t1, t2) => | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1985 |                          Const (@{const_name plus_class.plus}, T --> T --> T)
 | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1986 | $ t1 $ t2) | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1987 |                      (map (curry (op $) (Const (@{const_name single},
 | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1988 | elem_T --> T))) ts) | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1989 | end | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1990 | | multiset_postproc _ _ _ _ t = t | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1991 | *} | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1992 | |
| 38287 | 1993 | declaration {*
 | 
| 1994 | Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
 | |
| 38242 | 1995 | multiset_postproc | 
| 35712 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1996 | *} | 
| 
77aa29bf14ee
added a mechanism to Nitpick to support custom rendering of terms, and used it for multisets
 blanchet parents: 
35402diff
changeset | 1997 | |
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1998 | hide_const (open) fold | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1999 | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2000 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2001 | subsection {* Naive implementation using lists *}
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2002 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2003 | code_datatype multiset_of | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2004 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2005 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2006 |   "{#} = multiset_of []"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2007 | by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2008 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2009 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2010 |   "{#x#} = multiset_of [x]"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2011 | by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2012 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2013 | lemma union_code [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2014 | "multiset_of xs + multiset_of ys = multiset_of (xs @ ys)" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2015 | by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2016 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2017 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2018 | "image_mset f (multiset_of xs) = multiset_of (map f xs)" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2019 | by (simp add: multiset_of_map) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2020 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2021 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2022 | "Multiset.filter f (multiset_of xs) = multiset_of (filter f xs)" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2023 | by (simp add: multiset_of_filter) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2024 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2025 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2026 | "multiset_of xs - multiset_of ys = multiset_of (fold remove1 ys xs)" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2027 | by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2028 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2029 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2030 | "multiset_of xs #\<inter> multiset_of ys = | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2031 | multiset_of (snd (fold (\<lambda>x (ys, zs). | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2032 | if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, [])))" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2033 | proof - | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2034 | have "\<And>zs. multiset_of (snd (fold (\<lambda>x (ys, zs). | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2035 | if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) = | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2036 | (multiset_of xs #\<inter> multiset_of ys) + multiset_of zs" | 
| 51623 | 2037 | by (induct xs arbitrary: ys) | 
| 2038 | (auto simp add: mem_set_multiset_eq inter_add_right1 inter_add_right2 ac_simps) | |
| 2039 | then show ?thesis by simp | |
| 2040 | qed | |
| 2041 | ||
| 2042 | lemma [code]: | |
| 2043 | "multiset_of xs #\<union> multiset_of ys = | |
| 2044 | multiset_of (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))" | |
| 2045 | proof - | |
| 2046 | have "\<And>zs. multiset_of (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) = | |
| 2047 | (multiset_of xs #\<union> multiset_of ys) + multiset_of zs" | |
| 2048 | by (induct xs arbitrary: ys) (simp_all add: multiset_eq_iff) | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2049 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2050 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2051 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2052 | lemma [code_unfold]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2053 | "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2054 | by (simp add: in_multiset_of) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2055 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2056 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2057 | "count (multiset_of xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2058 | proof - | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2059 | have "\<And>n. fold (\<lambda>y. if x = y then Suc else id) xs n = count (multiset_of xs) x + n" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2060 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2061 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2062 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2063 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2064 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2065 | "set_of (multiset_of xs) = set xs" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2066 | by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2067 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2068 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2069 | "sorted_list_of_multiset (multiset_of xs) = sort xs" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2070 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2071 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2072 | lemma [code]: -- {* not very efficient, but representation-ignorant! *}
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2073 | "multiset_of_set A = multiset_of (sorted_list_of_set A)" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2074 | apply (cases "finite A") | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2075 | apply simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2076 | apply (induct A rule: finite_induct) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2077 | apply (simp_all add: union_commute) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2078 | done | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2079 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2080 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2081 | "mcard (multiset_of xs) = length xs" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2082 | by (simp add: mcard_multiset_of) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2083 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2084 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2085 | "A \<le> B \<longleftrightarrow> A #\<inter> B = A" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2086 | by (auto simp add: inf.order_iff) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2087 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2088 | instantiation multiset :: (equal) equal | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2089 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2090 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2091 | definition | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2092 | [code]: "HOL.equal A B \<longleftrightarrow> (A::'a multiset) \<le> B \<and> B \<le> A" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2093 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2094 | instance | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2095 | by default (simp add: equal_multiset_def eq_iff) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2096 | |
| 37169 
f69efa106feb
make Nitpick "show_all" option behave less surprisingly
 blanchet parents: 
37107diff
changeset | 2097 | end | 
| 49388 | 2098 | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2099 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2100 | "(A::'a multiset) < B \<longleftrightarrow> A \<le> B \<and> A \<noteq> B" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2101 | by auto | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2102 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2103 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2104 | "msetsum (multiset_of xs) = listsum xs" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2105 | by (induct xs) (simp_all add: add.commute) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2106 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2107 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2108 | "msetprod (multiset_of xs) = fold times xs 1" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2109 | proof - | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2110 | have "\<And>x. fold times xs x = msetprod (multiset_of xs) * x" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2111 | by (induct xs) (simp_all add: mult.assoc) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2112 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2113 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2114 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2115 | lemma [code]: | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2116 | "size = mcard" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2117 | by (fact size_eq_mcard) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2118 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2119 | text {*
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2120 |   Exercise for the casual reader: add implementations for @{const le_multiset}
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2121 |   and @{const less_multiset} (multiset order).
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2122 | *} | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2123 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2124 | text {* Quickcheck generators *}
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2125 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2126 | definition (in term_syntax) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2127 | msetify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2128 | \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2129 |   [code_unfold]: "msetify xs = Code_Evaluation.valtermify multiset_of {\<cdot>} xs"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2130 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2131 | notation fcomp (infixl "\<circ>>" 60) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2132 | notation scomp (infixl "\<circ>\<rightarrow>" 60) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2133 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2134 | instantiation multiset :: (random) random | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2135 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2136 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2137 | definition | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2138 | "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (msetify xs))" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2139 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2140 | instance .. | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2141 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2142 | end | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2143 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2144 | no_notation fcomp (infixl "\<circ>>" 60) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2145 | no_notation scomp (infixl "\<circ>\<rightarrow>" 60) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2146 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2147 | instantiation multiset :: (full_exhaustive) full_exhaustive | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2148 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2149 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2150 | definition full_exhaustive_multiset :: "('a multiset \<times> (unit \<Rightarrow> term) \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option"
 | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2151 | where | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2152 | "full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (\<lambda>xs. f (msetify xs)) i" | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2153 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2154 | instance .. | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2155 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2156 | end | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2157 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2158 | hide_const (open) msetify | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2159 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2160 | end | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2161 |