| author | wenzelm | 
| Fri, 28 Apr 2017 13:21:03 +0200 | |
| changeset 65604 | 637aa8e93cd7 | 
| parent 65547 | 701bb74c5f97 | 
| child 66276 | acc3b7dd0b21 | 
| permissions | -rw-r--r-- | 
| 10249 | 1 | (* Title: HOL/Library/Multiset.thy | 
| 15072 | 2 | Author: Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3 | Author: Andrei Popescu, TU Muenchen | 
| 59813 | 4 | Author: Jasmin Blanchette, Inria, LORIA, MPII | 
| 5 | Author: Dmitriy Traytel, TU Muenchen | |
| 6 | Author: Mathias Fleury, MPII | |
| 10249 | 7 | *) | 
| 8 | ||
| 65048 | 9 | section \<open>(Finite) Multisets\<close> | 
| 10249 | 10 | |
| 15131 | 11 | theory Multiset | 
| 65029 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 12 | imports Cancellation | 
| 15131 | 13 | begin | 
| 10249 | 14 | |
| 60500 | 15 | subsection \<open>The type of multisets\<close> | 
| 10249 | 16 | |
| 60606 | 17 | definition "multiset = {f :: 'a \<Rightarrow> nat. finite {x. f x > 0}}"
 | 
| 18 | ||
| 19 | typedef 'a multiset = "multiset :: ('a \<Rightarrow> nat) set"
 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 20 | morphisms count Abs_multiset | 
| 45694 
4a8743618257
prefer typedef without extra definition and alternative name;
 wenzelm parents: 
45608diff
changeset | 21 | unfolding multiset_def | 
| 10249 | 22 | proof | 
| 45694 
4a8743618257
prefer typedef without extra definition and alternative name;
 wenzelm parents: 
45608diff
changeset | 23 |   show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp
 | 
| 10249 | 24 | qed | 
| 25 | ||
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 26 | setup_lifting type_definition_multiset | 
| 19086 | 27 | |
| 60606 | 28 | lemma multiset_eq_iff: "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 29 | 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 | 30 | |
| 60606 | 31 | lemma multiset_eqI: "(\<And>x. count A x = count B x) \<Longrightarrow> A = B" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 32 | 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 | 33 | |
| 60606 | 34 | text \<open>Preservation of the representing set @{term multiset}.\<close>
 | 
| 35 | ||
| 36 | lemma const0_in_multiset: "(\<lambda>a. 0) \<in> multiset" | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 37 | 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 | 38 | |
| 60606 | 39 | lemma only1_in_multiset: "(\<lambda>b. if b = a then n else 0) \<in> multiset" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 40 | 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 | 41 | |
| 60606 | 42 | lemma union_preserves_multiset: "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 43 | by (simp add: multiset_def) | 
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 44 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned 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 | 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 | 46 | 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 | 47 | 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 | 48 | 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 | 49 |   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 | 50 | 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 | 51 | 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 | 52 | 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 | 53 | 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 | 54 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 55 | 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 | 56 | 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 | 57 | 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 | 58 | 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 | 59 |   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 | 60 | 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 | 61 | 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 | 62 | 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 | 63 | 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 | 64 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned 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 | 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 | 66 | 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 | 67 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned 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 | |
| 60500 | 69 | subsection \<open>Representing multisets\<close> | 
| 70 | ||
| 71 | text \<open>Multiset enumeration\<close> | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 72 | |
| 48008 | 73 | instantiation multiset :: (type) cancel_comm_monoid_add | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 74 | begin | 
| 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 75 | |
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 76 | lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0" | 
| 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 77 | by (rule const0_in_multiset) | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 78 | |
| 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 | 79 | 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 | 80 | "Mempty \<equiv> 0" | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 81 | |
| 60606 | 82 | lift_definition plus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 83 | by (rule union_preserves_multiset) | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 84 | |
| 60606 | 85 | lift_definition minus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a" | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59813diff
changeset | 86 | by (rule diff_preserves_multiset) | 
| 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59813diff
changeset | 87 | |
| 48008 | 88 | instance | 
| 60678 | 89 | by (standard; transfer; simp add: fun_eq_iff) | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 90 | |
| 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 91 | end | 
| 10249 | 92 | |
| 63195 | 93 | context | 
| 94 | begin | |
| 95 | ||
| 96 | qualified definition is_empty :: "'a multiset \<Rightarrow> bool" where | |
| 97 |   [code_abbrev]: "is_empty A \<longleftrightarrow> A = {#}"
 | |
| 98 | ||
| 99 | end | |
| 100 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 101 | lemma add_mset_in_multiset: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 102 | assumes M: \<open>M \<in> multiset\<close> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 103 | shows \<open>(\<lambda>b. if b = a then Suc (M b) else M b) \<in> multiset\<close> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 104 | using assms by (simp add: multiset_def insert_Collect[symmetric]) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 105 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 106 | lift_definition add_mset :: "'a \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 107 | "\<lambda>a M b. if b = a then Suc (M b) else M b" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 108 | by (rule add_mset_in_multiset) | 
| 15869 | 109 | |
| 26145 | 110 | syntax | 
| 60606 | 111 |   "_multiset" :: "args \<Rightarrow> 'a multiset"    ("{#(_)#}")
 | 
| 25507 | 112 | translations | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 113 |   "{#x, xs#}" == "CONST add_mset x {#xs#}"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 114 |   "{#x#}" == "CONST add_mset x {#}"
 | 
| 25507 | 115 | |
| 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 | 116 | lemma count_empty [simp]: "count {#} a = 0"
 | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 117 | by (simp add: zero_multiset.rep_eq) | 
| 10249 | 118 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 119 | lemma count_add_mset [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 120 | "count (add_mset b A) a = (if b = a then Suc (count A a) else count A a)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 121 | by (simp add: add_mset.rep_eq) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 122 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 123 | lemma count_single: "count {#b#} a = (if b = a then 1 else 0)"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 124 | by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 125 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 126 | lemma | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 127 |   add_mset_not_empty [simp]: \<open>add_mset a A \<noteq> {#}\<close> and
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 128 |   empty_not_add_mset [simp]: "{#} \<noteq> add_mset a A"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 129 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 130 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 131 | lemma add_mset_add_mset_same_iff [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 132 | "add_mset a A = add_mset a B \<longleftrightarrow> A = B" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 133 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 134 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 135 | lemma add_mset_commute: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 136 | "add_mset x (add_mset y M) = add_mset y (add_mset x M)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 137 | by (auto simp: multiset_eq_iff) | 
| 29901 | 138 | |
| 10249 | 139 | |
| 60500 | 140 | subsection \<open>Basic operations\<close> | 
| 141 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 142 | subsubsection \<open>Conversion to set and membership\<close> | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 143 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 144 | definition set_mset :: "'a multiset \<Rightarrow> 'a set" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 145 |   where "set_mset M = {x. count M x > 0}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 146 | |
| 62537 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 147 | abbreviation Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool" | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 148 | where "Melem a M \<equiv> a \<in> set_mset M" | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 149 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 150 | notation | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 151 |   Melem  ("op \<in>#") and
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 152 |   Melem  ("(_/ \<in># _)" [51, 51] 50)
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 153 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 154 | notation (ASCII) | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 155 |   Melem  ("op :#") and
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 156 |   Melem  ("(_/ :# _)" [51, 51] 50)
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 157 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 158 | abbreviation not_Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool" | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 159 | where "not_Melem a M \<equiv> a \<notin> set_mset M" | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 160 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 161 | notation | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 162 |   not_Melem  ("op \<notin>#") and
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 163 |   not_Melem  ("(_/ \<notin># _)" [51, 51] 50)
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 164 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 165 | notation (ASCII) | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 166 |   not_Melem  ("op ~:#") and
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 167 |   not_Melem  ("(_/ ~:# _)" [51, 51] 50)
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 168 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 169 | context | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 170 | begin | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 171 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 172 | qualified abbreviation Ball :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 173 | where "Ball M \<equiv> Set.Ball (set_mset M)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 174 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 175 | qualified abbreviation Bex :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 176 | where "Bex M \<equiv> Set.Bex (set_mset M)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 177 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 178 | end | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 179 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 180 | syntax | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 181 |   "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_\<in>#_./ _)" [0, 0, 10] 10)
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 182 |   "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_\<in>#_./ _)" [0, 0, 10] 10)
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 183 | |
| 62537 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 184 | syntax (ASCII) | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 185 |   "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_:#_./ _)" [0, 0, 10] 10)
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 186 |   "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_:#_./ _)" [0, 0, 10] 10)
 | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 187 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 188 | translations | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 189 | "\<forall>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Ball A (\<lambda>x. P)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 190 | "\<exists>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Bex A (\<lambda>x. P)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 191 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 192 | lemma count_eq_zero_iff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 193 | "count M x = 0 \<longleftrightarrow> x \<notin># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 194 | by (auto simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 195 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 196 | lemma not_in_iff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 197 | "x \<notin># M \<longleftrightarrow> count M x = 0" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 198 | by (auto simp add: count_eq_zero_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 199 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 200 | lemma count_greater_zero_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 201 | "count M x > 0 \<longleftrightarrow> x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 202 | by (auto simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 203 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 204 | lemma count_inI: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 205 | assumes "count M x = 0 \<Longrightarrow> False" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 206 | shows "x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 207 | proof (rule ccontr) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 208 | assume "x \<notin># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 209 | with assms show False by (simp add: not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 210 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 211 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 212 | lemma in_countE: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 213 | assumes "x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 214 | obtains n where "count M x = Suc n" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 215 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 216 | from assms have "count M x > 0" by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 217 | then obtain n where "count M x = Suc n" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 218 | using gr0_conv_Suc by blast | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 219 | with that show thesis . | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 220 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 221 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 222 | lemma count_greater_eq_Suc_zero_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 223 | "count M x \<ge> Suc 0 \<longleftrightarrow> x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 224 | by (simp add: Suc_le_eq) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 225 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 226 | lemma count_greater_eq_one_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 227 | "count M x \<ge> 1 \<longleftrightarrow> x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 228 | by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 229 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 230 | lemma set_mset_empty [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 231 |   "set_mset {#} = {}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 232 | by (simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 233 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 234 | lemma set_mset_single: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 235 |   "set_mset {#b#} = {b}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 236 | by (simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 237 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 238 | lemma set_mset_eq_empty_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 239 |   "set_mset M = {} \<longleftrightarrow> M = {#}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 240 | by (auto simp add: multiset_eq_iff count_eq_zero_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 241 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 242 | lemma finite_set_mset [iff]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 243 | "finite (set_mset M)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 244 | using count [of M] by (simp add: multiset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 245 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 246 | lemma set_mset_add_mset_insert [simp]: \<open>set_mset (add_mset a A) = insert a (set_mset A)\<close> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 247 | by (auto simp del: count_greater_eq_Suc_zero_iff | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 248 | simp: count_greater_eq_Suc_zero_iff[symmetric] split: if_splits) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 249 | |
| 63924 | 250 | lemma multiset_nonemptyE [elim]: | 
| 251 |   assumes "A \<noteq> {#}"
 | |
| 252 | obtains x where "x \<in># A" | |
| 253 | proof - | |
| 254 | have "\<exists>x. x \<in># A" by (rule ccontr) (insert assms, auto) | |
| 255 | with that show ?thesis by blast | |
| 256 | qed | |
| 257 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 258 | |
| 60500 | 259 | subsubsection \<open>Union\<close> | 
| 10249 | 260 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 261 | lemma count_union [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 262 | "count (M + N) a = count M a + count N a" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 263 | by (simp add: plus_multiset.rep_eq) | 
| 10249 | 264 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 265 | lemma set_mset_union [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 266 | "set_mset (M + N) = set_mset M \<union> set_mset N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 267 | by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_union) simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 268 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 269 | lemma union_mset_add_mset_left [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 270 | "add_mset a A + B = add_mset a (A + B)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 271 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 272 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 273 | lemma union_mset_add_mset_right [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 274 | "A + add_mset a B = add_mset a (A + B)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 275 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 276 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 277 | lemma add_mset_add_single: \<open>add_mset a A = A + {#a#}\<close>
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 278 | by (subst union_mset_add_mset_right, subst add.comm_neutral) standard | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 279 | |
| 10249 | 280 | |
| 60500 | 281 | subsubsection \<open>Difference\<close> | 
| 10249 | 282 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 283 | instance multiset :: (type) comm_monoid_diff | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 284 | by standard (transfer; simp add: fun_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 285 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 286 | lemma count_diff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 287 | "count (M - N) a = count M a - count N a" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 288 | 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 | 289 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 290 | lemma add_mset_diff_bothsides: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 291 | \<open>add_mset a M - add_mset a A = M - A\<close> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 292 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 293 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 294 | lemma in_diff_count: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 295 | "a \<in># M - N \<longleftrightarrow> count N a < count M a" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 296 | by (simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 297 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 298 | lemma count_in_diffI: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 299 | assumes "\<And>n. count N x = n + count M x \<Longrightarrow> False" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 300 | shows "x \<in># M - N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 301 | proof (rule ccontr) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 302 | assume "x \<notin># M - N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 303 | then have "count N x = (count N x - count M x) + count M x" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 304 | by (simp add: in_diff_count not_less) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 305 | with assms show False by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 306 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 307 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 308 | lemma in_diff_countE: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 309 | assumes "x \<in># M - N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 310 | obtains n where "count M x = Suc n + count N x" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 311 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 312 | from assms have "count M x - count N x > 0" by (simp add: in_diff_count) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 313 | then have "count M x > count N x" by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 314 | then obtain n where "count M x = Suc n + count N x" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 315 | using less_iff_Suc_add by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 316 | with that show thesis . | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 317 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 318 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 319 | lemma in_diffD: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 320 | assumes "a \<in># M - N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 321 | shows "a \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 322 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 323 | have "0 \<le> count N a" by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 324 | also from assms have "count N a < count M a" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 325 | by (simp add: in_diff_count) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 326 | finally show ?thesis by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 327 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 328 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 329 | lemma set_mset_diff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 330 |   "set_mset (M - N) = {a. count N a < count M a}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 331 | by (simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 332 | |
| 17161 | 333 | lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
 | 
| 52289 | 334 | by rule (fact Groups.diff_zero, fact Groups.zero_diff) | 
| 36903 | 335 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 336 | lemma diff_cancel: "A - A = {#}"
 | 
| 52289 | 337 | by (fact Groups.diff_cancel) | 
| 10249 | 338 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 339 | lemma diff_union_cancelR: "M + N - N = (M::'a multiset)" | 
| 52289 | 340 | by (fact add_diff_cancel_right') | 
| 10249 | 341 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 342 | lemma diff_union_cancelL: "N + M - N = (M::'a multiset)" | 
| 52289 | 343 | 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 | 344 | |
| 52289 | 345 | lemma diff_right_commute: | 
| 60606 | 346 | fixes M N Q :: "'a multiset" | 
| 347 | shows "M - N - Q = M - Q - N" | |
| 52289 | 348 | by (fact diff_right_commute) | 
| 349 | ||
| 350 | lemma diff_add: | |
| 60606 | 351 | fixes M N Q :: "'a multiset" | 
| 352 | shows "M - (N + Q) = M - N - Q" | |
| 52289 | 353 | by (rule sym) (fact diff_diff_add) | 
| 58425 | 354 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 355 | lemma insert_DiffM [simp]: "x \<in># M \<Longrightarrow> add_mset 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 | 356 | 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 | 357 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 358 | lemma insert_DiffM2: "x \<in># M \<Longrightarrow> (M - {#x#}) + {#x#} = M"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 359 | by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 360 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 361 | lemma diff_union_swap: "a \<noteq> b \<Longrightarrow> add_mset b (M - {#a#}) = add_mset b M - {#a#}"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 362 | 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 | 363 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 364 | lemma diff_add_mset_swap [simp]: "b \<notin># A \<Longrightarrow> add_mset b M - A = add_mset b (M - A)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 365 | by (auto simp add: multiset_eq_iff simp: not_in_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 366 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 367 | lemma diff_union_swap2 [simp]: "y \<in># M \<Longrightarrow> add_mset x M - {#y#} = add_mset x (M - {#y#})"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 368 | by (metis add_mset_diff_bothsides diff_union_swap diff_zero insert_DiffM) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 369 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 370 | lemma diff_diff_add_mset [simp]: "(M::'a multiset) - N - P = M - (N + P)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 371 | by (rule diff_diff_add) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 372 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 373 | lemma diff_union_single_conv: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 374 |   "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 375 | by (simp add: multiset_eq_iff Suc_le_eq) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 376 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 377 | lemma mset_add [elim?]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 378 | assumes "a \<in># A" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 379 | obtains B where "A = add_mset a B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 380 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 381 |   from assms have "A = add_mset a (A - {#a#})"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 382 | by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 383 | with that show thesis . | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 384 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 385 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 386 | lemma union_iff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 387 | "a \<in># A + B \<longleftrightarrow> a \<in># A \<or> a \<in># B" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 388 | by auto | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26033diff
changeset | 389 | |
| 10249 | 390 | |
| 60500 | 391 | subsubsection \<open>Equality of multisets\<close> | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 392 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 393 | 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 | 394 | 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 | 395 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 396 | 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 | 397 | 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 | 398 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 399 | 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 | 400 | 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 | 401 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 402 | lemma multi_self_add_other_not_self [simp]: "M = add_mset x M \<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 | 403 | 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 | 404 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 405 | lemma add_mset_remove_trivial [simp]: \<open>add_mset x M - {#x#} = M\<close>
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 406 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 407 | |
| 60606 | 408 | lemma diff_single_trivial: "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 409 | by (auto simp add: multiset_eq_iff not_in_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 | 410 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 411 | lemma diff_single_eq_union: "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = add_mset x N"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 412 | by auto | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 413 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 414 | lemma union_single_eq_diff: "add_mset x M = N \<Longrightarrow> M = N - {#x#}"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 415 | unfolding add_mset_add_single[of _ M] by (fact add_implies_diff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 416 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 417 | lemma union_single_eq_member: "add_mset x M = N \<Longrightarrow> x \<in># N" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 418 | 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 | 419 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 420 | lemma add_mset_remove_trivial_If: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 421 |   "add_mset a (N - {#a#}) = (if a \<in># N then N else add_mset a N)"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 422 | by (simp add: diff_single_trivial) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 423 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 424 | lemma add_mset_remove_trivial_eq: \<open>N = add_mset a (N - {#a#}) \<longleftrightarrow> a \<in># N\<close>
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 425 | by (auto simp: add_mset_remove_trivial_If) | 
| 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 | 426 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 427 | lemma union_is_single: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 428 |   "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N = {#} \<or> M = {#} \<and> N = {#a#}"
 | 
| 60606 | 429 | (is "?lhs = ?rhs") | 
| 46730 | 430 | proof | 
| 60606 | 431 | show ?lhs if ?rhs using that by auto | 
| 432 | show ?rhs if ?lhs | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 433 | by (metis Multiset.diff_cancel add.commute add_diff_cancel_left' diff_add_zero diff_single_trivial insert_DiffM that) | 
| 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 | 434 | 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 | 435 | |
| 60606 | 436 | lemma single_is_union: "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 437 |   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 | 438 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 439 | lemma add_eq_conv_diff: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 440 |   "add_mset a M = add_mset b N \<longleftrightarrow> M = N \<and> a = b \<or> M = add_mset b (N - {#a#}) \<and> N = add_mset a (M - {#b#})"
 | 
| 60606 | 441 | (is "?lhs \<longleftrightarrow> ?rhs") | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44339diff
changeset | 442 | (* 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 | 443 | proof | 
| 60606 | 444 | show ?lhs if ?rhs | 
| 445 | using that | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 446 | by (auto simp add: add_mset_commute[of a b]) | 
| 60606 | 447 | show ?rhs if ?lhs | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 448 | proof (cases "a = b") | 
| 60500 | 449 | case True with \<open>?lhs\<close> show ?thesis by simp | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 450 | 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 | 451 | case False | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 452 | from \<open>?lhs\<close> have "a \<in># add_mset b N" by (rule union_single_eq_member) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 453 | with False have "a \<in># N" by auto | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 454 |     moreover from \<open>?lhs\<close> have "M = add_mset b N - {#a#}" by (rule union_single_eq_diff)
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 455 | moreover note False | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 456 |     ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#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 | 457 | 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 | 458 | 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 | 459 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 460 | lemma add_mset_eq_single [iff]: "add_mset b M = {#a#} \<longleftrightarrow> b = a \<and> M = {#}"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 461 | by (auto simp: add_eq_conv_diff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 462 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 463 | lemma single_eq_add_mset [iff]: "{#a#} = add_mset b M \<longleftrightarrow> b = a \<and> M = {#}"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 464 | by (auto simp: add_eq_conv_diff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 465 | |
| 58425 | 466 | lemma insert_noteq_member: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 467 | assumes BC: "add_mset b B = add_mset c 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 | 468 | 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 | 469 | 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 | 470 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 471 | have "c \<in># add_mset c C" 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 | 472 |   have nc: "\<not> c \<in># {#b#}" using bnotc by simp
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 473 | then have "c \<in># add_mset b B" using BC 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 | 474 | 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 | 475 | 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 | 476 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 477 | lemma add_eq_conv_ex: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 478 | "(add_mset a M = add_mset b N) = | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 479 | (M = N \<and> a = b \<or> (\<exists>K. M = add_mset b K \<and> N = add_mset a K))" | 
| 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 | 480 | 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 | 481 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 482 | lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = add_mset x A" | 
| 60678 | 483 |   by (rule exI [where x = "M - {#x#}"]) simp
 | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 484 | |
| 58425 | 485 | lemma multiset_add_sub_el_shuffle: | 
| 60606 | 486 | assumes "c \<in># B" | 
| 487 | and "b \<noteq> c" | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 488 |   shows "add_mset b (B - {#c#}) = add_mset b B - {#c#}"
 | 
| 58098 | 489 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 490 | from \<open>c \<in># B\<close> obtain A where B: "B = add_mset c A" | 
| 58098 | 491 | by (blast dest: multi_member_split) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 492 |   have "add_mset b A = add_mset c (add_mset b A) - {#c#}" by simp
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 493 |   then have "add_mset b A = add_mset b (add_mset c A) - {#c#}"
 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 494 | by (simp add: \<open>b \<noteq> c\<close>) | 
| 58098 | 495 | then show ?thesis using B by simp | 
| 496 | qed | |
| 497 | ||
| 64418 | 498 | lemma add_mset_eq_singleton_iff[iff]: | 
| 499 |   "add_mset x M = {#y#} \<longleftrightarrow> M = {#} \<and> x = y"
 | |
| 500 | by auto | |
| 501 | ||
| 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 | 502 | |
| 60500 | 503 | subsubsection \<open>Pointwise ordering induced by count\<close> | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 504 | |
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 505 | definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subseteq>#" 50) | 
| 65466 | 506 | where "A \<subseteq># B \<longleftrightarrow> (\<forall>a. count A a \<le> count B a)" | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 507 | |
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 508 | definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subset>#" 50) | 
| 65466 | 509 | where "A \<subset># B \<longleftrightarrow> A \<subseteq># B \<and> A \<noteq> B" | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 510 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 511 | abbreviation (input) supseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<supseteq>#" 50) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 512 | where "supseteq_mset A B \<equiv> B \<subseteq># A" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 513 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 514 | abbreviation (input) supset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<supset>#" 50) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 515 | where "supset_mset A B \<equiv> B \<subset># A" | 
| 62208 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 516 | |
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 517 | notation (input) | 
| 62208 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 518 | subseteq_mset (infix "\<le>#" 50) and | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 519 | supseteq_mset (infix "\<ge>#" 50) | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 520 | |
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 521 | notation (ASCII) | 
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 522 | subseteq_mset (infix "<=#" 50) and | 
| 62208 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 523 | subset_mset (infix "<#" 50) and | 
| 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 524 | supseteq_mset (infix ">=#" 50) and | 
| 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 525 | supset_mset (infix ">#" 50) | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 526 | |
| 60606 | 527 | interpretation subset_mset: ordered_ab_semigroup_add_imp_le "op +" "op -" "op \<subseteq>#" "op \<subset>#" | 
| 60678 | 528 | by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order_trans antisym) | 
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 529 | \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 530 | |
| 64587 | 531 | interpretation subset_mset: ordered_ab_semigroup_monoid_add_imp_le "op +" 0 "op -" "op \<subseteq>#" "op \<subset>#" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 532 | by standard | 
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 533 | \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 534 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 535 | lemma mset_subset_eqI: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 536 | "(\<And>a. count A a \<le> count B a) \<Longrightarrow> A \<subseteq># B" | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 537 | by (simp add: subseteq_mset_def) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 538 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 539 | lemma mset_subset_eq_count: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 540 | "A \<subseteq># B \<Longrightarrow> count A a \<le> count B a" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 541 | by (simp add: subseteq_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 542 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 543 | lemma mset_subset_eq_exists_conv: "(A::'a multiset) \<subseteq># B \<longleftrightarrow> (\<exists>C. B = A + C)" | 
| 60678 | 544 | unfolding subseteq_mset_def | 
| 545 | apply (rule iffI) | |
| 546 | apply (rule exI [where x = "B - A"]) | |
| 547 | apply (auto intro: multiset_eq_iff [THEN iffD2]) | |
| 548 | done | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 549 | |
| 64587 | 550 | interpretation subset_mset: ordered_cancel_comm_monoid_diff "op +" 0 "op \<subseteq>#" "op \<subset>#" "op -" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 551 | by standard (simp, fact mset_subset_eq_exists_conv) | 
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 552 | \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 553 | |
| 64017 
6e7bf7678518
more multiset simp rules
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63924diff
changeset | 554 | declare subset_mset.add_diff_assoc[simp] subset_mset.add_diff_assoc2[simp] | 
| 
6e7bf7678518
more multiset simp rules
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63924diff
changeset | 555 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 556 | lemma mset_subset_eq_mono_add_right_cancel: "(A::'a multiset) + C \<subseteq># B + C \<longleftrightarrow> A \<subseteq># B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 557 | by (fact subset_mset.add_le_cancel_right) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 558 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 559 | lemma mset_subset_eq_mono_add_left_cancel: "C + (A::'a multiset) \<subseteq># C + B \<longleftrightarrow> A \<subseteq># B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 560 | by (fact subset_mset.add_le_cancel_left) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 561 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 562 | lemma mset_subset_eq_mono_add: "(A::'a multiset) \<subseteq># B \<Longrightarrow> C \<subseteq># D \<Longrightarrow> A + C \<subseteq># B + D" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 563 | by (fact subset_mset.add_mono) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 564 | |
| 63560 
3e3097ac37d1
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63547diff
changeset | 565 | lemma mset_subset_eq_add_left: "(A::'a multiset) \<subseteq># A + B" | 
| 
3e3097ac37d1
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63547diff
changeset | 566 | by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 567 | |
| 63560 
3e3097ac37d1
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63547diff
changeset | 568 | lemma mset_subset_eq_add_right: "B \<subseteq># (A::'a multiset) + B" | 
| 
3e3097ac37d1
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63547diff
changeset | 569 | by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 570 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 571 | lemma single_subset_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 572 |   "{#a#} \<subseteq># M \<longleftrightarrow> a \<in># M"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 573 | by (auto simp add: subseteq_mset_def Suc_le_eq) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 574 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 575 | lemma mset_subset_eq_single: "a \<in># B \<Longrightarrow> {#a#} \<subseteq># B"
 | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 576 | by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 577 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 578 | lemma mset_subset_eq_add_mset_cancel: \<open>add_mset a A \<subseteq># add_mset a B \<longleftrightarrow> A \<subseteq># B\<close> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 579 | unfolding add_mset_add_single[of _ A] add_mset_add_single[of _ B] | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 580 | by (rule mset_subset_eq_mono_add_right_cancel) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 581 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 582 | lemma multiset_diff_union_assoc: | 
| 60606 | 583 | fixes A B C D :: "'a multiset" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 584 | shows "C \<subseteq># B \<Longrightarrow> A + B - C = A + (B - C)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 585 | by (fact subset_mset.diff_add_assoc) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 586 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 587 | lemma mset_subset_eq_multiset_union_diff_commute: | 
| 60606 | 588 | fixes A B C D :: "'a multiset" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 589 | shows "B \<subseteq># A \<Longrightarrow> A - B + C = A + C - B" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 590 | by (fact subset_mset.add_diff_assoc2) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 591 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 592 | lemma diff_subset_eq_self[simp]: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 593 | "(M::'a multiset) - N \<subseteq># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 594 | by (simp add: subseteq_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 595 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 596 | lemma mset_subset_eqD: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 597 | assumes "A \<subseteq># B" and "x \<in># A" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 598 | shows "x \<in># B" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 599 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 600 | from \<open>x \<in># A\<close> have "count A x > 0" by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 601 | also from \<open>A \<subseteq># B\<close> have "count A x \<le> count B x" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 602 | by (simp add: subseteq_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 603 | finally show ?thesis by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 604 | qed | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 605 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 606 | lemma mset_subsetD: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 607 | "A \<subset># B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 608 | by (auto intro: mset_subset_eqD [of A]) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 609 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 610 | lemma set_mset_mono: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 611 | "A \<subseteq># B \<Longrightarrow> set_mset A \<subseteq> set_mset B" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 612 | by (metis mset_subset_eqD subsetI) | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 613 | |
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 614 | lemma mset_subset_eq_insertD: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 615 | "add_mset x A \<subseteq># B \<Longrightarrow> x \<in># B \<and> A \<subset># 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 | 616 | apply (rule conjI) | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 617 | apply (simp add: mset_subset_eqD) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 618 | apply (clarsimp simp: subset_mset_def subseteq_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 619 | apply safe | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 620 | apply (erule_tac x = a in allE) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 621 | apply (auto split: if_split_asm) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 622 | 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 | 623 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 624 | lemma mset_subset_insertD: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 625 | "add_mset x A \<subset># B \<Longrightarrow> x \<in># B \<and> A \<subset># B" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 626 | by (rule mset_subset_eq_insertD) simp | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 627 | |
| 63831 | 628 | lemma mset_subset_of_empty[simp]: "A \<subset># {#} \<longleftrightarrow> False"
 | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 629 | by (simp only: subset_mset.not_less_zero) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 630 | |
| 64587 | 631 | lemma empty_subset_add_mset[simp]: "{#} \<subset># add_mset x M"
 | 
| 632 | by (auto intro: subset_mset.gr_zeroI) | |
| 63831 | 633 | |
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 634 | lemma empty_le: "{#} \<subseteq># A"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 635 | by (fact subset_mset.zero_le) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 636 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 637 | lemma insert_subset_eq_iff: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 638 |   "add_mset a A \<subseteq># B \<longleftrightarrow> a \<in># B \<and> A \<subseteq># B - {#a#}"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 639 | using le_diff_conv2 [of "Suc 0" "count B a" "count A a"] | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 640 | apply (auto simp add: subseteq_mset_def not_in_iff Suc_le_eq) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 641 | apply (rule ccontr) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 642 | apply (auto simp add: not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 643 | done | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 644 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 645 | lemma insert_union_subset_iff: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 646 |   "add_mset a A \<subset># B \<longleftrightarrow> a \<in># B \<and> A \<subset># B - {#a#}"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 647 | by (auto simp add: insert_subset_eq_iff subset_mset_def) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 648 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 649 | lemma subset_eq_diff_conv: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 650 | "A - C \<subseteq># B \<longleftrightarrow> A \<subseteq># B + C" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 651 | by (simp add: subseteq_mset_def le_diff_conv) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 652 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 653 | lemma multi_psub_of_add_self [simp]: "A \<subset># add_mset x A" | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 654 | by (auto simp: subset_mset_def subseteq_mset_def) | 
| 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 655 | |
| 64076 | 656 | lemma multi_psub_self: "A \<subset># A = False" | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 657 | 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 | 658 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 659 | lemma mset_subset_add_mset [simp]: "add_mset x N \<subset># add_mset x M \<longleftrightarrow> N \<subset># M" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 660 | unfolding add_mset_add_single[of _ N] add_mset_add_single[of _ M] | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 661 | by (fact subset_mset.add_less_cancel_right) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 662 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 663 | lemma mset_subset_diff_self: "c \<in># B \<Longrightarrow> B - {#c#} \<subset># B"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 664 | by (auto simp: subset_mset_def elim: mset_add) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 665 | |
| 64077 | 666 | lemma Diff_eq_empty_iff_mset: "A - B = {#} \<longleftrightarrow> A \<subseteq># B"
 | 
| 667 | by (auto simp: multiset_eq_iff subseteq_mset_def) | |
| 668 | ||
| 64418 | 669 | lemma add_mset_subseteq_single_iff[iff]: "add_mset a M \<subseteq># {#b#} \<longleftrightarrow> M = {#} \<and> a = b"
 | 
| 670 | proof | |
| 671 |   assume A: "add_mset a M \<subseteq># {#b#}"
 | |
| 672 | then have \<open>a = b\<close> | |
| 673 | by (auto dest: mset_subset_eq_insertD) | |
| 674 |   then show "M={#} \<and> a=b"
 | |
| 675 | using A by (simp add: mset_subset_eq_add_mset_cancel) | |
| 676 | qed simp | |
| 677 | ||
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 678 | |
| 64076 | 679 | subsubsection \<open>Intersection and bounded union\<close> | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 680 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 681 | definition inf_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "\<inter>#" 70) where | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 682 | multiset_inter_def: "inf_subset_mset A B = A - (A - B)" | 
| 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 683 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 684 | interpretation subset_mset: semilattice_inf inf_subset_mset "op \<subseteq>#" "op \<subset>#" | 
| 46921 | 685 | proof - | 
| 60678 | 686 | have [simp]: "m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" for m n q :: nat | 
| 687 | by arith | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 688 | show "class.semilattice_inf op \<inter># op \<subseteq># op \<subset>#" | 
| 60678 | 689 | by standard (auto simp add: multiset_inter_def subseteq_mset_def) | 
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 690 | qed \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 691 | |
| 64076 | 692 | definition sup_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"(infixl "\<union>#" 70) | 
| 693 | where "sup_subset_mset A B = A + (B - A)" \<comment> \<open>FIXME irregular fact name\<close> | |
| 694 | ||
| 695 | interpretation subset_mset: semilattice_sup sup_subset_mset "op \<subseteq>#" "op \<subset>#" | |
| 696 | proof - | |
| 697 | have [simp]: "m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" for m n q :: nat | |
| 698 | by arith | |
| 699 | show "class.semilattice_sup op \<union># op \<subseteq># op \<subset>#" | |
| 700 | by standard (auto simp add: sup_subset_mset_def subseteq_mset_def) | |
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 701 | qed \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 64076 | 702 | |
| 703 | interpretation subset_mset: bounded_lattice_bot "op \<inter>#" "op \<subseteq>#" "op \<subset>#" | |
| 704 |   "op \<union>#" "{#}"
 | |
| 705 | by standard auto | |
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 706 | \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 64076 | 707 | |
| 708 | ||
| 709 | subsubsection \<open>Additional intersection facts\<close> | |
| 710 | ||
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 711 | lemma multiset_inter_count [simp]: | 
| 60606 | 712 | fixes A B :: "'a multiset" | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 713 | shows "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 | 714 | by (simp add: multiset_inter_def) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 715 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 716 | lemma set_mset_inter [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 717 | "set_mset (A \<inter># B) = set_mset A \<inter> set_mset B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 718 | by (simp only: set_eq_iff count_greater_zero_iff [symmetric] multiset_inter_count) simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 719 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 720 | lemma diff_intersect_left_idem [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 721 | "M - M \<inter># N = M - N" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 722 | by (simp add: multiset_eq_iff min_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 723 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 724 | lemma diff_intersect_right_idem [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 725 | "M - N \<inter># M = M - N" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 726 | by (simp add: multiset_eq_iff min_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 727 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 728 | lemma multiset_inter_single[simp]: "a \<noteq> b \<Longrightarrow> {#a#} \<inter># {#b#} = {#}"
 | 
| 46730 | 729 | 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 | 730 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 731 | lemma multiset_union_diff_commute: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 732 |   assumes "B \<inter># C = {#}"
 | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 733 | 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 | 734 | proof (rule multiset_eqI) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 735 | fix x | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 736 | from assms have "min (count B x) (count C x) = 0" | 
| 46730 | 737 | by (auto simp add: multiset_eq_iff) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 738 | then have "count B x = 0 \<or> count C x = 0" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 739 | unfolding min_def by (auto split: if_splits) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 740 | then show "count (A + B - C) x = count (A - C + B) x" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 741 | by auto | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 742 | qed | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 743 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 744 | lemma disjunct_not_in: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 745 |   "A \<inter># B = {#} \<longleftrightarrow> (\<forall>a. a \<notin># A \<or> a \<notin># B)" (is "?P \<longleftrightarrow> ?Q")
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 746 | proof | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 747 | assume ?P | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 748 | show ?Q | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 749 | proof | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 750 | fix a | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 751 | from \<open>?P\<close> have "min (count A a) (count B a) = 0" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 752 | by (simp add: multiset_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 753 | then have "count A a = 0 \<or> count B a = 0" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 754 | by (cases "count A a \<le> count B a") (simp_all add: min_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 755 | then show "a \<notin># A \<or> a \<notin># B" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 756 | by (simp add: not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 757 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 758 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 759 | assume ?Q | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 760 | show ?P | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 761 | proof (rule multiset_eqI) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 762 | fix a | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 763 | from \<open>?Q\<close> have "count A a = 0 \<or> count B a = 0" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 764 | by (auto simp add: not_in_iff) | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 765 |     then show "count (A \<inter># B) a = count {#} a"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 766 | by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 767 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 768 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 769 | |
| 64077 | 770 | lemma inter_mset_empty_distrib_right: "A \<inter># (B + C) = {#} \<longleftrightarrow> A \<inter># B = {#} \<and> A \<inter># C = {#}"
 | 
| 771 | by (meson disjunct_not_in union_iff) | |
| 772 | ||
| 773 | lemma inter_mset_empty_distrib_left: "(A + B) \<inter># C = {#} \<longleftrightarrow> A \<inter># C = {#} \<and> B \<inter># C = {#}"
 | |
| 774 | by (meson disjunct_not_in union_iff) | |
| 775 | ||
| 63831 | 776 | lemma add_mset_inter_add_mset[simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 777 | "add_mset a A \<inter># add_mset a B = add_mset a (A \<inter># B)" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 778 | by (metis add_mset_add_single add_mset_diff_bothsides diff_subset_eq_self multiset_inter_def | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 779 | subset_mset.diff_add_assoc2) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 780 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 781 | lemma add_mset_disjoint [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 782 |   "add_mset a A \<inter># B = {#} \<longleftrightarrow> a \<notin># B \<and> A \<inter># B = {#}"
 | 
| 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 783 |   "{#} = add_mset a A \<inter># B \<longleftrightarrow> a \<notin># B \<and> {#} = A \<inter># B"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 784 | by (auto simp: disjunct_not_in) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 785 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 786 | lemma disjoint_add_mset [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 787 |   "B \<inter># add_mset a A = {#} \<longleftrightarrow> a \<notin># B \<and> B \<inter># A = {#}"
 | 
| 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 788 |   "{#} = A \<inter># add_mset b B \<longleftrightarrow> b \<notin># A \<and> {#} = A \<inter># B"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 789 | by (auto simp: disjunct_not_in) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 790 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 791 | lemma inter_add_left1: "\<not> x \<in># N \<Longrightarrow> (add_mset x M) \<inter># N = M \<inter># N" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 792 | by (simp add: multiset_eq_iff not_in_iff) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 793 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 794 | lemma inter_add_left2: "x \<in># N \<Longrightarrow> (add_mset x M) \<inter># N = add_mset x (M \<inter># (N - {#x#}))"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 795 | by (auto simp add: multiset_eq_iff elim: mset_add) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 796 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 797 | lemma inter_add_right1: "\<not> x \<in># N \<Longrightarrow> N \<inter># (add_mset x M) = N \<inter># M" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 798 | by (simp add: multiset_eq_iff not_in_iff) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 799 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 800 | lemma inter_add_right2: "x \<in># N \<Longrightarrow> N \<inter># (add_mset x M) = add_mset x ((N - {#x#}) \<inter># M)"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 801 | by (auto simp add: multiset_eq_iff elim: mset_add) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 802 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 803 | lemma disjunct_set_mset_diff: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 804 |   assumes "M \<inter># N = {#}"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 805 | shows "set_mset (M - N) = set_mset M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 806 | proof (rule set_eqI) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 807 | fix a | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 808 | from assms have "a \<notin># M \<or> a \<notin># N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 809 | by (simp add: disjunct_not_in) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 810 | then show "a \<in># M - N \<longleftrightarrow> a \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 811 | by (auto dest: in_diffD) (simp add: in_diff_count not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 812 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 813 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 814 | lemma at_most_one_mset_mset_diff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 815 |   assumes "a \<notin># M - {#a#}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 816 |   shows "set_mset (M - {#a#}) = set_mset M - {a}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 817 | using assms by (auto simp add: not_in_iff in_diff_count set_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 818 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 819 | lemma more_than_one_mset_mset_diff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 820 |   assumes "a \<in># M - {#a#}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 821 |   shows "set_mset (M - {#a#}) = set_mset M"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 822 | proof (rule set_eqI) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 823 | fix b | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 824 | have "Suc 0 < count M b \<Longrightarrow> count M b > 0" by arith | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 825 |   then show "b \<in># M - {#a#} \<longleftrightarrow> b \<in># M"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 826 | using assms by (auto simp add: in_diff_count) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 827 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 828 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 829 | lemma inter_iff: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 830 | "a \<in># A \<inter># B \<longleftrightarrow> a \<in># A \<and> a \<in># B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 831 | by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 832 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 833 | lemma inter_union_distrib_left: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 834 | "A \<inter># B + C = (A + C) \<inter># (B + C)" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 835 | by (simp add: multiset_eq_iff min_add_distrib_left) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 836 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 837 | lemma inter_union_distrib_right: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 838 | "C + A \<inter># B = (C + A) \<inter># (C + B)" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 839 | using inter_union_distrib_left [of A B C] by (simp add: ac_simps) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 840 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 841 | lemma inter_subset_eq_union: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 842 | "A \<inter># B \<subseteq># A + B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 843 | by (auto simp add: subseteq_mset_def) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 844 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 845 | |
| 64076 | 846 | subsubsection \<open>Additional bounded union facts\<close> | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 847 | |
| 62837 | 848 | lemma sup_subset_mset_count [simp]: \<comment> \<open>FIXME irregular fact name\<close> | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 849 | "count (A \<union># B) x = max (count A x) (count B x)" | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 850 | by (simp add: sup_subset_mset_def) | 
| 51623 | 851 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 852 | lemma set_mset_sup [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 853 | "set_mset (A \<union># B) = set_mset A \<union> set_mset B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 854 | by (simp only: set_eq_iff count_greater_zero_iff [symmetric] sup_subset_mset_count) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 855 | (auto simp add: not_in_iff elim: mset_add) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 856 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 857 | lemma sup_union_left1 [simp]: "\<not> x \<in># N \<Longrightarrow> (add_mset x M) \<union># N = add_mset x (M \<union># N)" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 858 | by (simp add: multiset_eq_iff not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 859 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 860 | lemma sup_union_left2: "x \<in># N \<Longrightarrow> (add_mset x M) \<union># N = add_mset x (M \<union># (N - {#x#}))"
 | 
| 51623 | 861 | by (simp add: multiset_eq_iff) | 
| 862 | ||
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 863 | lemma sup_union_right1 [simp]: "\<not> x \<in># N \<Longrightarrow> N \<union># (add_mset x M) = add_mset x (N \<union># M)" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 864 | by (simp add: multiset_eq_iff not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 865 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 866 | lemma sup_union_right2: "x \<in># N \<Longrightarrow> N \<union># (add_mset x M) = add_mset x ((N - {#x#}) \<union># M)"
 | 
| 51623 | 867 | by (simp add: multiset_eq_iff) | 
| 868 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 869 | lemma sup_union_distrib_left: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 870 | "A \<union># B + C = (A + C) \<union># (B + C)" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 871 | by (simp add: multiset_eq_iff max_add_distrib_left) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 872 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 873 | lemma union_sup_distrib_right: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 874 | "C + A \<union># B = (C + A) \<union># (C + B)" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 875 | using sup_union_distrib_left [of A B C] by (simp add: ac_simps) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 876 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 877 | lemma union_diff_inter_eq_sup: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 878 | "A + B - A \<inter># B = A \<union># B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 879 | by (auto simp add: multiset_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 880 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 881 | lemma union_diff_sup_eq_inter: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 882 | "A + B - A \<union># B = A \<inter># B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 883 | by (auto simp add: multiset_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 884 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 885 | lemma add_mset_union: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 886 | \<open>add_mset a A \<union># add_mset a B = add_mset a (A \<union># B)\<close> | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 887 | by (auto simp: multiset_eq_iff max_def) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 888 | |
| 51623 | 889 | |
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 890 | subsection \<open>Replicate and repeat operations\<close> | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 891 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 892 | definition replicate_mset :: "nat \<Rightarrow> 'a \<Rightarrow> 'a multiset" where | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 893 |   "replicate_mset n x = (add_mset x ^^ n) {#}"
 | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 894 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 895 | lemma replicate_mset_0[simp]: "replicate_mset 0 x = {#}"
 | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 896 | unfolding replicate_mset_def by simp | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 897 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 898 | lemma replicate_mset_Suc [simp]: "replicate_mset (Suc n) x = add_mset x (replicate_mset n x)" | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 899 | unfolding replicate_mset_def by (induct n) (auto intro: add.commute) | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 900 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 901 | lemma count_replicate_mset[simp]: "count (replicate_mset n x) y = (if y = x then n else 0)" | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 902 | unfolding replicate_mset_def by (induct n) auto | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 903 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 904 | fun repeat_mset :: "nat \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 905 |   "repeat_mset 0 _ = {#}" |
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 906 | "repeat_mset (Suc n) A = A + repeat_mset n A" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 907 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 908 | lemma count_repeat_mset [simp]: "count (repeat_mset i A) a = i * count A a" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 909 | by (induction i) auto | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 910 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 911 | lemma repeat_mset_right [simp]: "repeat_mset a (repeat_mset b A) = repeat_mset (a * b) A" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 912 | by (auto simp: multiset_eq_iff left_diff_distrib') | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 913 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 914 | lemma left_diff_repeat_mset_distrib': \<open>repeat_mset (i - j) u = repeat_mset i u - repeat_mset j u\<close> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 915 | by (auto simp: multiset_eq_iff left_diff_distrib') | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 916 | |
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 917 | lemma left_add_mult_distrib_mset: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 918 | "repeat_mset i u + (repeat_mset j u + k) = repeat_mset (i+j) u + k" | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 919 | by (auto simp: multiset_eq_iff add_mult_distrib) | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 920 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 921 | lemma repeat_mset_distrib: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 922 | "repeat_mset (m + n) A = repeat_mset m A + repeat_mset n A" | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 923 | by (auto simp: multiset_eq_iff Nat.add_mult_distrib) | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 924 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 925 | lemma repeat_mset_distrib2[simp]: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 926 | "repeat_mset n (A + B) = repeat_mset n A + repeat_mset n B" | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 927 | by (auto simp: multiset_eq_iff add_mult_distrib2) | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 928 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 929 | lemma repeat_mset_replicate_mset[simp]: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 930 |   "repeat_mset n {#a#} = replicate_mset n a"
 | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 931 | by (auto simp: multiset_eq_iff) | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 932 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 933 | lemma repeat_mset_distrib_add_mset[simp]: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 934 | "repeat_mset n (add_mset a A) = replicate_mset n a + repeat_mset n A" | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 935 | by (auto simp: multiset_eq_iff) | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 936 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 937 | lemma repeat_mset_empty[simp]: "repeat_mset n {#} = {#}"
 | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 938 | by (induction n) simp_all | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 939 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 940 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 941 | subsubsection \<open>Simprocs\<close> | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 942 | |
| 65031 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 943 | lemma repeat_mset_iterate_add: \<open>repeat_mset n M = iterate_add n M\<close> | 
| 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 944 | unfolding iterate_add_def by (induction n) auto | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 945 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 946 | lemma mset_subseteq_add_iff1: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 947 | "j \<le> (i::nat) \<Longrightarrow> (repeat_mset i u + m \<subseteq># repeat_mset j u + n) = (repeat_mset (i-j) u + m \<subseteq># n)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 948 | by (auto simp add: subseteq_mset_def nat_le_add_iff1) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 949 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 950 | lemma mset_subseteq_add_iff2: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 951 | "i \<le> (j::nat) \<Longrightarrow> (repeat_mset i u + m \<subseteq># repeat_mset j u + n) = (m \<subseteq># repeat_mset (j-i) u + n)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 952 | by (auto simp add: subseteq_mset_def nat_le_add_iff2) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 953 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 954 | lemma mset_subset_add_iff1: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 955 | "j \<le> (i::nat) \<Longrightarrow> (repeat_mset i u + m \<subset># repeat_mset j u + n) = (repeat_mset (i-j) u + m \<subset># n)" | 
| 65031 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 956 | unfolding subset_mset_def repeat_mset_iterate_add | 
| 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 957 | by (simp add: iterate_add_eq_add_iff1 mset_subseteq_add_iff1[unfolded repeat_mset_iterate_add]) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 958 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 959 | lemma mset_subset_add_iff2: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 960 | "i \<le> (j::nat) \<Longrightarrow> (repeat_mset i u + m \<subset># repeat_mset j u + n) = (m \<subset># repeat_mset (j-i) u + n)" | 
| 65031 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 961 | unfolding subset_mset_def repeat_mset_iterate_add | 
| 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 962 | by (simp add: iterate_add_eq_add_iff2 mset_subseteq_add_iff2[unfolded repeat_mset_iterate_add]) | 
| 65029 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 963 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 964 | ML_file "multiset_simprocs.ML" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 965 | |
| 65029 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 966 | lemma add_mset_replicate_mset_safe[cancelation_simproc_pre]: \<open>NO_MATCH {#} M \<Longrightarrow> add_mset a M = {#a#} + M\<close>
 | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 967 | by simp | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 968 | |
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 969 | declare repeat_mset_iterate_add[cancelation_simproc_pre] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 970 | |
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 971 | declare iterate_add_distrib[cancelation_simproc_pre] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 972 | declare repeat_mset_iterate_add[symmetric, cancelation_simproc_post] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 973 | |
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 974 | declare add_mset_not_empty[cancelation_simproc_eq_elim] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 975 | empty_not_add_mset[cancelation_simproc_eq_elim] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 976 | subset_mset.le_zero_eq[cancelation_simproc_eq_elim] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 977 | empty_not_add_mset[cancelation_simproc_eq_elim] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 978 | add_mset_not_empty[cancelation_simproc_eq_elim] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 979 | subset_mset.le_zero_eq[cancelation_simproc_eq_elim] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 980 | le_zero_eq[cancelation_simproc_eq_elim] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 981 | |
| 65027 
2b8583507891
renaming multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
64911diff
changeset | 982 | simproc_setup mseteq_cancel | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 983 |   ("(l::'a multiset) + m = n" | "(l::'a multiset) = m + n" |
 | 
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 984 | "add_mset a m = n" | "m = add_mset a n" | | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 985 | "replicate_mset p a = n" | "m = replicate_mset p a" | | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 986 | "repeat_mset p m = n" | "m = repeat_mset p m") = | 
| 65029 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 987 | \<open>fn phi => Cancel_Simprocs.eq_cancel\<close> | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 988 | |
| 65027 
2b8583507891
renaming multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
64911diff
changeset | 989 | simproc_setup msetsubset_cancel | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 990 |   ("(l::'a multiset) + m \<subset># n" | "(l::'a multiset) \<subset># m + n" |
 | 
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 991 | "add_mset a m \<subset># n" | "m \<subset># add_mset a n" | | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 992 | "replicate_mset p r \<subset># n" | "m \<subset># replicate_mset p r" | | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 993 | "repeat_mset p m \<subset># n" | "m \<subset># repeat_mset p m") = | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 994 | \<open>fn phi => Multiset_Simprocs.subset_cancel_msets\<close> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 995 | |
| 65027 
2b8583507891
renaming multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
64911diff
changeset | 996 | simproc_setup msetsubset_eq_cancel | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 997 |   ("(l::'a multiset) + m \<subseteq># n" | "(l::'a multiset) \<subseteq># m + n" |
 | 
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 998 | "add_mset a m \<subseteq># n" | "m \<subseteq># add_mset a n" | | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 999 | "replicate_mset p r \<subseteq># n" | "m \<subseteq># replicate_mset p r" | | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 1000 | "repeat_mset p m \<subseteq># n" | "m \<subseteq># repeat_mset p m") = | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1001 | \<open>fn phi => Multiset_Simprocs.subseteq_cancel_msets\<close> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1002 | |
| 65027 
2b8583507891
renaming multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
64911diff
changeset | 1003 | simproc_setup msetdiff_cancel | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1004 |   ("((l::'a multiset) + m) - n" | "(l::'a multiset) - (m + n)" |
 | 
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 1005 | "add_mset a m - n" | "m - add_mset a n" | | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 1006 | "replicate_mset p r - n" | "m - replicate_mset p r" | | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 1007 | "repeat_mset p m - n" | "m - repeat_mset p m") = | 
| 65029 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 1008 | \<open>fn phi => Cancel_Simprocs.diff_cancel\<close> | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1009 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1010 | |
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1011 | subsubsection \<open>Conditionally complete lattice\<close> | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1012 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1013 | instantiation multiset :: (type) Inf | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1014 | begin | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1015 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1016 | lift_definition Inf_multiset :: "'a multiset set \<Rightarrow> 'a multiset" is | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1017 |   "\<lambda>A i. if A = {} then 0 else Inf ((\<lambda>f. f i) ` A)"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1018 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1019 |   fix A :: "('a \<Rightarrow> nat) set" assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<in> multiset"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1020 |   have "finite {i. (if A = {} then 0 else Inf ((\<lambda>f. f i) ` A)) > 0}" unfolding multiset_def
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1021 |   proof (cases "A = {}")
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1022 | case False | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1023 | then obtain f where "f \<in> A" by blast | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1024 |     hence "{i. Inf ((\<lambda>f. f i) ` A) > 0} \<subseteq> {i. f i > 0}"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1025 | by (auto intro: less_le_trans[OF _ cInf_lower]) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1026 | moreover from \<open>f \<in> A\<close> * have "finite \<dots>" by (simp add: multiset_def) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1027 |     ultimately have "finite {i. Inf ((\<lambda>f. f i) ` A) > 0}" by (rule finite_subset)
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1028 | with False show ?thesis by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1029 | qed simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1030 |   thus "(\<lambda>i. if A = {} then 0 else INF f:A. f i) \<in> multiset" by (simp add: multiset_def)
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1031 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1032 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1033 | instance .. | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1034 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1035 | end | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1036 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1037 | lemma Inf_multiset_empty: "Inf {} = {#}"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1038 | by transfer simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1039 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1040 | lemma count_Inf_multiset_nonempty: "A \<noteq> {} \<Longrightarrow> count (Inf A) x = Inf ((\<lambda>X. count X x) ` A)"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1041 | by transfer simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1042 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1043 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1044 | instantiation multiset :: (type) Sup | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1045 | begin | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1046 | |
| 63360 | 1047 | definition Sup_multiset :: "'a multiset set \<Rightarrow> 'a multiset" where | 
| 1048 |   "Sup_multiset A = (if A \<noteq> {} \<and> subset_mset.bdd_above A then
 | |
| 1049 |            Abs_multiset (\<lambda>i. Sup ((\<lambda>X. count X i) ` A)) else {#})"
 | |
| 1050 | ||
| 1051 | lemma Sup_multiset_empty: "Sup {} = {#}"
 | |
| 1052 | by (simp add: Sup_multiset_def) | |
| 1053 | ||
| 1054 | lemma Sup_multiset_unbounded: "\<not>subset_mset.bdd_above A \<Longrightarrow> Sup A = {#}"
 | |
| 1055 | by (simp add: Sup_multiset_def) | |
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1056 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1057 | instance .. | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1058 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1059 | end | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1060 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1061 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1062 | lemma bdd_above_multiset_imp_bdd_above_count: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1063 | assumes "subset_mset.bdd_above (A :: 'a multiset set)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1064 | shows "bdd_above ((\<lambda>X. count X x) ` A)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1065 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1066 | from assms obtain Y where Y: "\<forall>X\<in>A. X \<subseteq># Y" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1067 | by (auto simp: subset_mset.bdd_above_def) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1068 | hence "count X x \<le> count Y x" if "X \<in> A" for X | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1069 | using that by (auto intro: mset_subset_eq_count) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1070 | thus ?thesis by (intro bdd_aboveI[of _ "count Y x"]) auto | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1071 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1072 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1073 | lemma bdd_above_multiset_imp_finite_support: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1074 |   assumes "A \<noteq> {}" "subset_mset.bdd_above (A :: 'a multiset set)"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1075 |   shows   "finite (\<Union>X\<in>A. {x. count X x > 0})"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1076 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1077 | from assms obtain Y where Y: "\<forall>X\<in>A. X \<subseteq># Y" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1078 | by (auto simp: subset_mset.bdd_above_def) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1079 | hence "count X x \<le> count Y x" if "X \<in> A" for X x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1080 | using that by (auto intro: mset_subset_eq_count) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1081 |   hence "(\<Union>X\<in>A. {x. count X x > 0}) \<subseteq> {x. count Y x > 0}"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1082 | by safe (erule less_le_trans) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1083 | moreover have "finite \<dots>" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1084 | ultimately show ?thesis by (rule finite_subset) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1085 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1086 | |
| 63360 | 1087 | lemma Sup_multiset_in_multiset: | 
| 1088 |   assumes "A \<noteq> {}" "subset_mset.bdd_above A"
 | |
| 1089 | shows "(\<lambda>i. SUP X:A. count X i) \<in> multiset" | |
| 1090 | unfolding multiset_def | |
| 1091 | proof | |
| 1092 |   have "{i. Sup ((\<lambda>X. count X i) ` A) > 0} \<subseteq> (\<Union>X\<in>A. {i. 0 < count X i})"
 | |
| 1093 | proof safe | |
| 1094 | fix i assume pos: "(SUP X:A. count X i) > 0" | |
| 1095 |     show "i \<in> (\<Union>X\<in>A. {i. 0 < count X i})"
 | |
| 1096 | proof (rule ccontr) | |
| 1097 |       assume "i \<notin> (\<Union>X\<in>A. {i. 0 < count X i})"
 | |
| 1098 | hence "\<forall>X\<in>A. count X i \<le> 0" by (auto simp: count_eq_zero_iff) | |
| 1099 | with assms have "(SUP X:A. count X i) \<le> 0" | |
| 1100 | by (intro cSup_least bdd_above_multiset_imp_bdd_above_count) auto | |
| 1101 | with pos show False by simp | |
| 1102 | qed | |
| 1103 | qed | |
| 1104 | moreover from assms have "finite \<dots>" by (rule bdd_above_multiset_imp_finite_support) | |
| 1105 |   ultimately show "finite {i. Sup ((\<lambda>X. count X i) ` A) > 0}" by (rule finite_subset)
 | |
| 1106 | qed | |
| 1107 | ||
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1108 | lemma count_Sup_multiset_nonempty: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1109 |   assumes "A \<noteq> {}" "subset_mset.bdd_above A"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1110 | shows "count (Sup A) x = (SUP X:A. count X x)" | 
| 63360 | 1111 | using assms by (simp add: Sup_multiset_def Abs_multiset_inverse Sup_multiset_in_multiset) | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1112 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1113 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 1114 | interpretation subset_mset: conditionally_complete_lattice Inf Sup "op \<inter>#" "op \<subseteq>#" "op \<subset>#" "op \<union>#" | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1115 | proof | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1116 | fix X :: "'a multiset" and A | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1117 | assume "X \<in> A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1118 | show "Inf A \<subseteq># X" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1119 | proof (rule mset_subset_eqI) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1120 | fix x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1121 |     from \<open>X \<in> A\<close> have "A \<noteq> {}" by auto
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1122 | hence "count (Inf A) x = (INF X:A. count X x)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1123 | by (simp add: count_Inf_multiset_nonempty) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1124 | also from \<open>X \<in> A\<close> have "\<dots> \<le> count X x" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1125 | by (intro cInf_lower) simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1126 | finally show "count (Inf A) x \<le> count X x" . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1127 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1128 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1129 | fix X :: "'a multiset" and A | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1130 |   assume nonempty: "A \<noteq> {}" and le: "\<And>Y. Y \<in> A \<Longrightarrow> X \<subseteq># Y"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1131 | show "X \<subseteq># Inf A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1132 | proof (rule mset_subset_eqI) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1133 | fix x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1134 | from nonempty have "count X x \<le> (INF X:A. count X x)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1135 | by (intro cInf_greatest) (auto intro: mset_subset_eq_count le) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1136 | also from nonempty have "\<dots> = count (Inf A) x" by (simp add: count_Inf_multiset_nonempty) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1137 | finally show "count X x \<le> count (Inf A) x" . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1138 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1139 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1140 | fix X :: "'a multiset" and A | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1141 | assume X: "X \<in> A" and bdd: "subset_mset.bdd_above A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1142 | show "X \<subseteq># Sup A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1143 | proof (rule mset_subset_eqI) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1144 | fix x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1145 |     from X have "A \<noteq> {}" by auto
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1146 | have "count X x \<le> (SUP X:A. count X x)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1147 | by (intro cSUP_upper X bdd_above_multiset_imp_bdd_above_count bdd) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1148 |     also from count_Sup_multiset_nonempty[OF \<open>A \<noteq> {}\<close> bdd]
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1149 | have "(SUP X:A. count X x) = count (Sup A) x" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1150 | finally show "count X x \<le> count (Sup A) x" . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1151 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1152 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1153 | fix X :: "'a multiset" and A | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1154 |   assume nonempty: "A \<noteq> {}" and ge: "\<And>Y. Y \<in> A \<Longrightarrow> Y \<subseteq># X"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1155 | from ge have bdd: "subset_mset.bdd_above A" by (rule subset_mset.bdd_aboveI[of _ X]) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1156 | show "Sup A \<subseteq># X" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1157 | proof (rule mset_subset_eqI) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1158 | fix x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1159 |     from count_Sup_multiset_nonempty[OF \<open>A \<noteq> {}\<close> bdd]
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1160 | have "count (Sup A) x = (SUP X:A. count X x)" . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1161 | also from nonempty have "\<dots> \<le> count X x" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1162 | by (intro cSup_least) (auto intro: mset_subset_eq_count ge) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1163 | finally show "count (Sup A) x \<le> count X x" . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1164 | qed | 
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 1165 | qed \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1166 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1167 | lemma set_mset_Inf: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1168 |   assumes "A \<noteq> {}"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1169 | shows "set_mset (Inf A) = (\<Inter>X\<in>A. set_mset X)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1170 | proof safe | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1171 | fix x X assume "x \<in># Inf A" "X \<in> A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1172 |   hence nonempty: "A \<noteq> {}" by (auto simp: Inf_multiset_empty)
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1173 |   from \<open>x \<in># Inf A\<close> have "{#x#} \<subseteq># Inf A" by auto
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1174 | also from \<open>X \<in> A\<close> have "\<dots> \<subseteq># X" by (rule subset_mset.cInf_lower) simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1175 | finally show "x \<in># X" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1176 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1177 | fix x assume x: "x \<in> (\<Inter>X\<in>A. set_mset X)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1178 |   hence "{#x#} \<subseteq># X" if "X \<in> A" for X using that by auto
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1179 |   from assms and this have "{#x#} \<subseteq># Inf A" by (rule subset_mset.cInf_greatest)
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1180 | thus "x \<in># Inf A" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1181 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1182 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1183 | lemma in_Inf_multiset_iff: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1184 |   assumes "A \<noteq> {}"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1185 | shows "x \<in># Inf A \<longleftrightarrow> (\<forall>X\<in>A. x \<in># X)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1186 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1187 | from assms have "set_mset (Inf A) = (\<Inter>X\<in>A. set_mset X)" by (rule set_mset_Inf) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1188 | also have "x \<in> \<dots> \<longleftrightarrow> (\<forall>X\<in>A. x \<in># X)" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1189 | finally show ?thesis . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1190 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1191 | |
| 63360 | 1192 | lemma in_Inf_multisetD: "x \<in># Inf A \<Longrightarrow> X \<in> A \<Longrightarrow> x \<in># X" | 
| 1193 | by (subst (asm) in_Inf_multiset_iff) auto | |
| 1194 | ||
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1195 | lemma set_mset_Sup: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1196 | assumes "subset_mset.bdd_above A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1197 | shows "set_mset (Sup A) = (\<Union>X\<in>A. set_mset X)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1198 | proof safe | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1199 | fix x assume "x \<in># Sup A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1200 |   hence nonempty: "A \<noteq> {}" by (auto simp: Sup_multiset_empty)
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1201 | show "x \<in> (\<Union>X\<in>A. set_mset X)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1202 | proof (rule ccontr) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1203 | assume x: "x \<notin> (\<Union>X\<in>A. set_mset X)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1204 | have "count X x \<le> count (Sup A) x" if "X \<in> A" for X x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1205 | using that by (intro mset_subset_eq_count subset_mset.cSup_upper assms) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1206 |     with x have "X \<subseteq># Sup A - {#x#}" if "X \<in> A" for X
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1207 | using that by (auto simp: subseteq_mset_def algebra_simps not_in_iff) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1208 |     hence "Sup A \<subseteq># Sup A - {#x#}" by (intro subset_mset.cSup_least nonempty)
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1209 | with \<open>x \<in># Sup A\<close> show False | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1210 | by (auto simp: subseteq_mset_def count_greater_zero_iff [symmetric] | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1211 | simp del: count_greater_zero_iff dest!: spec[of _ x]) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1212 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1213 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1214 | fix x X assume "x \<in> set_mset X" "X \<in> A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1215 |   hence "{#x#} \<subseteq># X" by auto
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1216 | also have "X \<subseteq># Sup A" by (intro subset_mset.cSup_upper \<open>X \<in> A\<close> assms) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1217 | finally show "x \<in> set_mset (Sup A)" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1218 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1219 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1220 | lemma in_Sup_multiset_iff: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1221 | assumes "subset_mset.bdd_above A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1222 | shows "x \<in># Sup A \<longleftrightarrow> (\<exists>X\<in>A. x \<in># X)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1223 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1224 | from assms have "set_mset (Sup A) = (\<Union>X\<in>A. set_mset X)" by (rule set_mset_Sup) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1225 | also have "x \<in> \<dots> \<longleftrightarrow> (\<exists>X\<in>A. x \<in># X)" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1226 | finally show ?thesis . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1227 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1228 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1229 | lemma in_Sup_multisetD: | 
| 63360 | 1230 | assumes "x \<in># Sup A" | 
| 1231 | shows "\<exists>X\<in>A. x \<in># X" | |
| 1232 | proof - | |
| 1233 | have "subset_mset.bdd_above A" | |
| 1234 | by (rule ccontr) (insert assms, simp_all add: Sup_multiset_unbounded) | |
| 1235 | with assms show ?thesis by (simp add: in_Sup_multiset_iff) | |
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 1236 | qed | 
| 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 1237 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 1238 | interpretation subset_mset: distrib_lattice "op \<inter>#" "op \<subseteq>#" "op \<subset>#" "op \<union>#" | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 1239 | proof | 
| 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 1240 | fix A B C :: "'a multiset" | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 1241 | show "A \<union># (B \<inter># C) = A \<union># B \<inter># (A \<union># C)" | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 1242 | by (intro multiset_eqI) simp_all | 
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 1243 | qed \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 63360 | 1244 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1245 | |
| 60500 | 1246 | subsubsection \<open>Filter (with comprehension syntax)\<close> | 
| 1247 | ||
| 1248 | text \<open>Multiset comprehension\<close> | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 1249 | |
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1250 | lift_definition filter_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"
 | 
| 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1251 | is "\<lambda>P M. \<lambda>x. if P x then M x else 0" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 1252 | by (rule filter_preserves_multiset) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1253 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1254 | syntax (ASCII) | 
| 63689 | 1255 |   "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{#_ :# _./ _#})")
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1256 | syntax | 
| 63689 | 1257 |   "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{#_ \<in># _./ _#})")
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1258 | translations | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1259 |   "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1260 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1261 | lemma count_filter_mset [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1262 | "count (filter_mset P M) a = (if P a then count M a else 0)" | 
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1263 | by (simp add: filter_mset.rep_eq) | 
| 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1264 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1265 | lemma set_mset_filter [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1266 |   "set_mset (filter_mset P M) = {a \<in> set_mset M. P a}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1267 | by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_filter_mset) simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1268 | |
| 60606 | 1269 | lemma filter_empty_mset [simp]: "filter_mset P {#} = {#}"
 | 
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1270 | by (rule multiset_eqI) simp | 
| 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1271 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1272 | lemma filter_single_mset: "filter_mset P {#x#} = (if P x then {#x#} else {#})"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 1273 | by (rule multiset_eqI) simp | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1274 | |
| 60606 | 1275 | lemma filter_union_mset [simp]: "filter_mset P (M + N) = filter_mset P M + filter_mset P N" | 
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 1276 | by (rule multiset_eqI) simp | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 1277 | |
| 60606 | 1278 | lemma filter_diff_mset [simp]: "filter_mset P (M - N) = filter_mset P M - filter_mset P N" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 1279 | by (rule multiset_eqI) simp | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1280 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 1281 | lemma filter_inter_mset [simp]: "filter_mset P (M \<inter># N) = filter_mset P M \<inter># filter_mset P N" | 
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 1282 | by (rule multiset_eqI) simp | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 1283 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 1284 | lemma filter_sup_mset[simp]: "filter_mset P (A \<union># B) = filter_mset P A \<union># filter_mset P B" | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1285 | by (rule multiset_eqI) simp | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1286 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1287 | lemma filter_mset_add_mset [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1288 | "filter_mset P (add_mset x A) = | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1289 | (if P x then add_mset x (filter_mset P A) else filter_mset P A)" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1290 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1291 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1292 | lemma multiset_filter_subset[simp]: "filter_mset f M \<subseteq># M" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1293 | by (simp add: mset_subset_eqI) | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 1294 | |
| 60606 | 1295 | lemma multiset_filter_mono: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1296 | assumes "A \<subseteq># B" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1297 | shows "filter_mset f A \<subseteq># filter_mset f B" | 
| 58035 | 1298 | proof - | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1299 | from assms[unfolded mset_subset_eq_exists_conv] | 
| 58035 | 1300 | obtain C where B: "B = A + C" by auto | 
| 1301 | show ?thesis unfolding B by auto | |
| 1302 | qed | |
| 1303 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1304 | lemma filter_mset_eq_conv: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1305 | "filter_mset P M = N \<longleftrightarrow> N \<subseteq># M \<and> (\<forall>b\<in>#N. P b) \<and> (\<forall>a\<in>#M - N. \<not> P a)" (is "?P \<longleftrightarrow> ?Q") | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1306 | proof | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1307 | assume ?P then show ?Q by auto (simp add: multiset_eq_iff in_diff_count) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1308 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1309 | assume ?Q | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1310 | then obtain Q where M: "M = N + Q" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1311 | by (auto simp add: mset_subset_eq_exists_conv) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1312 | then have MN: "M - N = Q" by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1313 | show ?P | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1314 | proof (rule multiset_eqI) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1315 | fix a | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1316 | from \<open>?Q\<close> MN have *: "\<not> P a \<Longrightarrow> a \<notin># N" "P a \<Longrightarrow> a \<notin># Q" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1317 | by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1318 | show "count (filter_mset P M) a = count N a" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1319 | proof (cases "a \<in># M") | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1320 | case True | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1321 | with * show ?thesis | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1322 | by (simp add: not_in_iff M) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1323 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1324 | case False then have "count M a = 0" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1325 | by (simp add: not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1326 | with M show ?thesis by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1327 | qed | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1328 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1329 | qed | 
| 59813 | 1330 | |
| 64077 | 1331 | lemma filter_filter_mset: "filter_mset P (filter_mset Q M) = {#x \<in># M. Q x \<and> P x#}"
 | 
| 1332 | by (auto simp: multiset_eq_iff) | |
| 1333 | ||
| 64418 | 1334 | lemma | 
| 1335 |   filter_mset_True[simp]: "{#y \<in># M. True#} = M" and
 | |
| 1336 |   filter_mset_False[simp]: "{#y \<in># M. False#} = {#}"
 | |
| 1337 | by (auto simp: multiset_eq_iff) | |
| 1338 | ||
| 59813 | 1339 | |
| 60500 | 1340 | subsubsection \<open>Size\<close> | 
| 10249 | 1341 | |
| 56656 | 1342 | definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))" | 
| 1343 | ||
| 1344 | lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a" | |
| 1345 | by (auto simp: wcount_def add_mult_distrib) | |
| 1346 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1347 | lemma wcount_add_mset: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1348 | "wcount f (add_mset x M) a = (if x = a then Suc (f a) else 0) + wcount f M a" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1349 | unfolding add_mset_add_single[of _ M] wcount_union by (auto simp: wcount_def) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1350 | |
| 56656 | 1351 | definition size_multiset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a multiset \<Rightarrow> nat" where
 | 
| 64267 | 1352 | "size_multiset f M = sum (wcount f M) (set_mset M)" | 
| 56656 | 1353 | |
| 1354 | lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def] | |
| 1355 | ||
| 60606 | 1356 | instantiation multiset :: (type) size | 
| 1357 | begin | |
| 1358 | ||
| 56656 | 1359 | definition size_multiset where | 
| 1360 | size_multiset_overloaded_def: "size_multiset = Multiset.size_multiset (\<lambda>_. 0)" | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1361 | instance .. | 
| 60606 | 1362 | |
| 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 | 1363 | 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 | 1364 | |
| 56656 | 1365 | lemmas size_multiset_overloaded_eq = | 
| 1366 | size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified] | |
| 1367 | ||
| 1368 | lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
 | |
| 1369 | by (simp add: size_multiset_def) | |
| 1370 | ||
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 1371 | lemma size_empty [simp]: "size {#} = 0"
 | 
| 56656 | 1372 | by (simp add: size_multiset_overloaded_def) | 
| 1373 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1374 | lemma size_multiset_single : "size_multiset f {#b#} = Suc (f b)"
 | 
| 56656 | 1375 | by (simp add: size_multiset_eq) | 
| 10249 | 1376 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1377 | lemma size_single: "size {#b#} = 1"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1378 | by (simp add: size_multiset_overloaded_def size_multiset_single) | 
| 56656 | 1379 | |
| 64267 | 1380 | lemma sum_wcount_Int: | 
| 1381 | "finite A \<Longrightarrow> sum (wcount f N) (A \<inter> set_mset N) = sum (wcount f N) A" | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1382 | by (induct rule: finite_induct) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1383 | (simp_all add: Int_insert_left wcount_def count_eq_zero_iff) | 
| 56656 | 1384 | |
| 1385 | lemma size_multiset_union [simp]: | |
| 1386 | "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N" | |
| 64267 | 1387 | apply (simp add: size_multiset_def sum_Un_nat sum.distrib sum_wcount_Int wcount_union) | 
| 56656 | 1388 | apply (subst Int_commute) | 
| 64267 | 1389 | apply (simp add: sum_wcount_Int) | 
| 26178 | 1390 | done | 
| 10249 | 1391 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1392 | lemma size_multiset_add_mset [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1393 | "size_multiset f (add_mset a M) = Suc (f a) + size_multiset f M" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1394 | unfolding add_mset_add_single[of _ M] size_multiset_union by (auto simp: size_multiset_single) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1395 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1396 | lemma size_add_mset [simp]: "size (add_mset a A) = Suc (size A)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1397 | by (simp add: size_multiset_overloaded_def wcount_add_mset) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1398 | |
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 1399 | lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N" | 
| 56656 | 1400 | by (auto simp add: size_multiset_overloaded_def) | 
| 1401 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1402 | lemma size_multiset_eq_0_iff_empty [iff]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1403 |   "size_multiset f M = 0 \<longleftrightarrow> M = {#}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1404 | by (auto simp add: size_multiset_eq count_eq_zero_iff) | 
| 10249 | 1405 | |
| 17161 | 1406 | lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
 | 
| 56656 | 1407 | by (auto simp add: size_multiset_overloaded_def) | 
| 26016 | 1408 | |
| 1409 | lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
 | |
| 26178 | 1410 | by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty) | 
| 10249 | 1411 | |
| 60607 | 1412 | lemma size_eq_Suc_imp_elem: "size M = Suc n \<Longrightarrow> \<exists>a. a \<in># M" | 
| 56656 | 1413 | apply (unfold size_multiset_overloaded_eq) | 
| 64267 | 1414 | apply (drule sum_SucD) | 
| 26178 | 1415 | apply auto | 
| 1416 | done | |
| 10249 | 1417 | |
| 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 | 1418 | 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 | 1419 | assumes "size M = Suc n" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1420 | shows "\<exists>a N. M = add_mset a N" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1421 | 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 | 1422 | 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 | 1423 | by (erule size_eq_Suc_imp_elem [THEN exE]) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1424 |   then have "M = add_mset a (M - {#a#})" 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 | 1425 | then show ?thesis by blast | 
| 23611 | 1426 | qed | 
| 15869 | 1427 | |
| 60606 | 1428 | lemma size_mset_mono: | 
| 1429 | fixes A B :: "'a multiset" | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1430 | assumes "A \<subseteq># B" | 
| 60606 | 1431 | shows "size A \<le> size B" | 
| 59949 | 1432 | proof - | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1433 | from assms[unfolded mset_subset_eq_exists_conv] | 
| 59949 | 1434 | obtain C where B: "B = A + C" by auto | 
| 60606 | 1435 | show ?thesis unfolding B by (induct C) auto | 
| 59949 | 1436 | qed | 
| 1437 | ||
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1438 | lemma size_filter_mset_lesseq[simp]: "size (filter_mset f M) \<le> size M" | 
| 59949 | 1439 | by (rule size_mset_mono[OF multiset_filter_subset]) | 
| 1440 | ||
| 1441 | lemma size_Diff_submset: | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1442 | "M \<subseteq># M' \<Longrightarrow> size (M' - M) = size M' - size(M::'a multiset)" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1443 | by (metis add_diff_cancel_left' size_union mset_subset_eq_exists_conv) | 
| 26016 | 1444 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1445 | |
| 60500 | 1446 | subsection \<open>Induction and case splits\<close> | 
| 10249 | 1447 | |
| 18258 | 1448 | theorem multiset_induct [case_names empty add, induct type: multiset]: | 
| 48009 | 1449 |   assumes empty: "P {#}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1450 | assumes add: "\<And>x M. P M \<Longrightarrow> P (add_mset x M)" | 
| 48009 | 1451 | shows "P M" | 
| 65545 | 1452 | proof (induct "size M" arbitrary: M) | 
| 48009 | 1453 | case 0 thus "P M" by (simp add: empty) | 
| 1454 | next | |
| 1455 | case (Suc k) | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1456 | obtain N x where "M = add_mset x N" | 
| 60500 | 1457 | using \<open>Suc k = size M\<close> [symmetric] | 
| 48009 | 1458 | using size_eq_Suc_imp_eq_union by fast | 
| 1459 | with Suc add show "P M" by simp | |
| 10249 | 1460 | qed | 
| 1461 | ||
| 65545 | 1462 | lemma multiset_induct_min[case_names empty add]: | 
| 1463 | fixes M :: "'a::linorder multiset" | |
| 1464 | assumes | |
| 1465 |     empty: "P {#}" and
 | |
| 1466 | add: "\<And>x M. P M \<Longrightarrow> (\<forall>y \<in># M. y \<ge> x) \<Longrightarrow> P (add_mset x M)" | |
| 1467 | shows "P M" | |
| 1468 | proof (induct "size M" arbitrary: M) | |
| 1469 | case (Suc k) | |
| 1470 | note ih = this(1) and Sk_eq_sz_M = this(2) | |
| 1471 | ||
| 1472 | let ?y = "Min (set_mset M)" | |
| 1473 |   let ?N = "M - {#?y#}"
 | |
| 1474 | ||
| 1475 | have M: "M = add_mset ?y ?N" | |
| 1476 | by (metis Min_in Sk_eq_sz_M finite_set_mset insert_DiffM lessI not_less_zero | |
| 1477 | set_mset_eq_empty_iff size_empty) | |
| 1478 | show ?case | |
| 1479 | by (subst M, rule add, rule ih, metis M Sk_eq_sz_M nat.inject size_add_mset, | |
| 1480 | meson Min_le finite_set_mset in_diffD) | |
| 1481 | qed (simp add: empty) | |
| 1482 | ||
| 1483 | lemma multiset_induct_max[case_names empty add]: | |
| 1484 | fixes M :: "'a::linorder multiset" | |
| 1485 | assumes | |
| 1486 |     empty: "P {#}" and
 | |
| 1487 | add: "\<And>x M. P M \<Longrightarrow> (\<forall>y \<in># M. y \<le> x) \<Longrightarrow> P (add_mset x M)" | |
| 1488 | shows "P M" | |
| 1489 | proof (induct "size M" arbitrary: M) | |
| 1490 | case (Suc k) | |
| 1491 | note ih = this(1) and Sk_eq_sz_M = this(2) | |
| 1492 | ||
| 1493 | let ?y = "Max (set_mset M)" | |
| 1494 |   let ?N = "M - {#?y#}"
 | |
| 1495 | ||
| 1496 | have M: "M = add_mset ?y ?N" | |
| 1497 | by (metis Max_in Sk_eq_sz_M finite_set_mset insert_DiffM lessI not_less_zero | |
| 1498 | set_mset_eq_empty_iff size_empty) | |
| 1499 | show ?case | |
| 1500 | by (subst M, rule add, rule ih, metis M Sk_eq_sz_M nat.inject size_add_mset, | |
| 1501 | meson Max_ge finite_set_mset in_diffD) | |
| 1502 | qed (simp add: empty) | |
| 1503 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1504 | lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = add_mset a A"
 | 
| 26178 | 1505 | by (induct M) auto | 
| 25610 | 1506 | |
| 55913 | 1507 | lemma multiset_cases [cases type]: | 
| 1508 |   obtains (empty) "M = {#}"
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1509 | | (add) x N where "M = add_mset x N" | 
| 63092 | 1510 | by (induct M) simp_all | 
| 25610 | 1511 | |
| 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 | 1512 | 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 | 1513 | 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 | 1514 | |
| 60607 | 1515 | lemma multiset_partition: "M = {# x\<in>#M. P x #} + {# x\<in>#M. \<not> P x #}"
 | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 1516 | apply (subst multiset_eq_iff) | 
| 26178 | 1517 | apply auto | 
| 1518 | done | |
| 10249 | 1519 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1520 | lemma mset_subset_size: "(A::'a multiset) \<subset># 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 | 1521 | 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 | 1522 | case (empty M) | 
| 64076 | 1523 |   then have "M \<noteq> {#}" by (simp add: subset_mset.zero_less_iff_neq_zero)
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1524 | then obtain M' x where "M = add_mset x M'" | 
| 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 | 1525 | 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 | 1526 | 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 | 1527 | next | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1528 | case (add x S T) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1529 | have IH: "\<And>B. S \<subset># B \<Longrightarrow> size S < size B" by fact | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1530 | have SxsubT: "add_mset x S \<subset># T" by fact | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1531 | then have "x \<in># T" and "S \<subset># T" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1532 | by (auto dest: mset_subset_insertD) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1533 | then obtain T' where T: "T = add_mset x T'" | 
| 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 | 1534 | by (blast dest: multi_member_split) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1535 | then have "S \<subset># T'" using SxsubT | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1536 | 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 | 1537 | 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 | 1538 | 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 | 1539 | 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 | 1540 | |
| 59949 | 1541 | lemma size_1_singleton_mset: "size M = 1 \<Longrightarrow> \<exists>a. M = {#a#}"
 | 
| 1542 | by (cases M) auto | |
| 1543 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1544 | |
| 60500 | 1545 | subsubsection \<open>Strong induction and subset induction for multisets\<close> | 
| 1546 | ||
| 1547 | text \<open>Well-foundedness of strict subset relation\<close> | |
| 58098 | 1548 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1549 | lemma wf_subset_mset_rel: "wf {(M, N :: 'a multiset). M \<subset># N}"
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1550 | apply (rule wf_measure [THEN wf_subset, where f1=size]) | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1551 | apply (clarsimp simp: measure_def inv_image_def mset_subset_size) | 
| 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 | 1552 | 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 | 1553 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1554 | lemma full_multiset_induct [case_names less]: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1555 | assumes ih: "\<And>B. \<forall>(A::'a multiset). A \<subset># 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 | 1556 | shows "P B" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1557 | apply (rule wf_subset_mset_rel [THEN wf_induct]) | 
| 58098 | 1558 | apply (rule ih, auto) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1559 | 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 | 1560 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1561 | lemma multi_subset_induct [consumes 2, case_names empty add]: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1562 | assumes "F \<subseteq># A" | 
| 60606 | 1563 |     and empty: "P {#}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1564 | and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (add_mset a F)" | 
| 60606 | 1565 | shows "P F" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1566 | proof - | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1567 | from \<open>F \<subseteq># A\<close> | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1568 | 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 | 1569 | 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 | 1570 |     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 | 1571 | 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 | 1572 | fix x F | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1573 | assume P: "F \<subseteq># A \<Longrightarrow> P F" and i: "add_mset x F \<subseteq># A" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1574 | show "P (add_mset x F)" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1575 | proof (rule insert) | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1576 | from i show "x \<in># A" by (auto dest: mset_subset_eq_insertD) | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1577 | from i have "F \<subseteq># A" by (auto dest: mset_subset_eq_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 | 1578 | 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 | 1579 | 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 | 1580 | 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 | 1581 | qed | 
| 26145 | 1582 | |
| 17161 | 1583 | |
| 60500 | 1584 | subsection \<open>The fold combinator\<close> | 
| 48023 | 1585 | |
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1586 | definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
 | 
| 48023 | 1587 | where | 
| 60495 | 1588 | "fold_mset f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_mset M)" | 
| 48023 | 1589 | |
| 60606 | 1590 | lemma fold_mset_empty [simp]: "fold_mset f s {#} = s"
 | 
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1591 | by (simp add: fold_mset_def) | 
| 48023 | 1592 | |
| 1593 | context comp_fun_commute | |
| 1594 | begin | |
| 1595 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1596 | lemma fold_mset_add_mset [simp]: "fold_mset f s (add_mset x M) = f x (fold_mset f s M)" | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1597 | proof - | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1598 | interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y" | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1599 | by (fact comp_fun_commute_funpow) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1600 | interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (add_mset x M) y" | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1601 | by (fact comp_fun_commute_funpow) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1602 | show ?thesis | 
| 60495 | 1603 | proof (cases "x \<in> set_mset M") | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1604 | case False | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1605 | then have *: "count (add_mset x M) x = 1" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1606 | by (simp add: not_in_iff) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1607 | from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (add_mset x M) y) s (set_mset M) = | 
| 60495 | 1608 | Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_mset M)" | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1609 | by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1610 | with False * show ?thesis | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1611 | by (simp add: fold_mset_def del: count_add_mset) | 
| 48023 | 1612 | next | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1613 | case True | 
| 63040 | 1614 |     define N where "N = set_mset M - {x}"
 | 
| 60495 | 1615 | from N_def True have *: "set_mset M = insert x N" "x \<notin> N" "finite N" by auto | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1616 | then have "Finite_Set.fold (\<lambda>y. f y ^^ count (add_mset x M) y) s N = | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1617 | Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N" | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1618 | by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1619 | with * show ?thesis by (simp add: fold_mset_def del: count_add_mset) simp | 
| 48023 | 1620 | qed | 
| 1621 | qed | |
| 1622 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1623 | corollary fold_mset_single: "fold_mset f s {#x#} = f x s"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1624 | by simp | 
| 48023 | 1625 | |
| 60606 | 1626 | lemma fold_mset_fun_left_comm: "f x (fold_mset f s M) = fold_mset f (f x s) M" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1627 | by (induct M) (simp_all add: fun_left_comm) | 
| 48023 | 1628 | |
| 60606 | 1629 | lemma fold_mset_union [simp]: "fold_mset f s (M + N) = fold_mset f (fold_mset f s M) N" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1630 | by (induct M) (simp_all add: fold_mset_fun_left_comm) | 
| 48023 | 1631 | |
| 1632 | lemma fold_mset_fusion: | |
| 1633 | assumes "comp_fun_commute g" | |
| 60606 | 1634 | and *: "\<And>x y. h (g x y) = f x (h y)" | 
| 1635 | shows "h (fold_mset g w A) = fold_mset f (h w) A" | |
| 48023 | 1636 | proof - | 
| 1637 | interpret comp_fun_commute g by (fact assms) | |
| 60606 | 1638 | from * show ?thesis by (induct A) auto | 
| 48023 | 1639 | qed | 
| 1640 | ||
| 1641 | end | |
| 1642 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1643 | lemma union_fold_mset_add_mset: "A + B = fold_mset add_mset A B" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1644 | proof - | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1645 | interpret comp_fun_commute add_mset | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1646 | by standard auto | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1647 | show ?thesis | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1648 | by (induction B) auto | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1649 | qed | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1650 | |
| 60500 | 1651 | text \<open> | 
| 48023 | 1652 | A note on code generation: When defining some function containing a | 
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1653 |   subterm @{term "fold_mset F"}, code generation is not automatic. When
 | 
| 61585 | 1654 | interpreting locale \<open>left_commutative\<close> with \<open>F\<close>, the | 
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1655 |   would be code thms for @{const fold_mset} become thms like
 | 
| 61585 | 1656 |   @{term "fold_mset F z {#} = z"} where \<open>F\<close> is not a pattern but
 | 
| 48023 | 1657 | contains defined symbols, i.e.\ is not a code thm. Hence a separate | 
| 61585 | 1658 | constant with its own code thms needs to be introduced for \<open>F\<close>. See the image operator below. | 
| 60500 | 1659 | \<close> | 
| 1660 | ||
| 1661 | ||
| 1662 | subsection \<open>Image\<close> | |
| 48023 | 1663 | |
| 1664 | definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1665 |   "image_mset f = fold_mset (add_mset \<circ> f) {#}"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1666 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1667 | lemma comp_fun_commute_mset_image: "comp_fun_commute (add_mset \<circ> f)" | 
| 49823 | 1668 | proof | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1669 | qed (simp add: fun_eq_iff) | 
| 48023 | 1670 | |
| 1671 | lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
 | |
| 49823 | 1672 | by (simp add: image_mset_def) | 
| 48023 | 1673 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1674 | lemma image_mset_single: "image_mset f {#x#} = {#f x#}"
 | 
| 49823 | 1675 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1676 | interpret comp_fun_commute "add_mset \<circ> f" | 
| 49823 | 1677 | by (fact comp_fun_commute_mset_image) | 
| 1678 | show ?thesis by (simp add: image_mset_def) | |
| 1679 | qed | |
| 48023 | 1680 | |
| 60606 | 1681 | lemma image_mset_union [simp]: "image_mset f (M + N) = image_mset f M + image_mset f N" | 
| 49823 | 1682 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1683 | interpret comp_fun_commute "add_mset \<circ> f" | 
| 49823 | 1684 | by (fact comp_fun_commute_mset_image) | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1685 | show ?thesis by (induct N) (simp_all add: image_mset_def) | 
| 49823 | 1686 | qed | 
| 1687 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1688 | corollary image_mset_add_mset [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1689 | "image_mset f (add_mset a M) = add_mset (f a) (image_mset f M)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1690 | unfolding image_mset_union add_mset_add_single[of a M] by (simp add: image_mset_single) | 
| 48023 | 1691 | |
| 60606 | 1692 | lemma set_image_mset [simp]: "set_mset (image_mset f M) = image f (set_mset M)" | 
| 49823 | 1693 | by (induct M) simp_all | 
| 48040 | 1694 | |
| 60606 | 1695 | lemma size_image_mset [simp]: "size (image_mset f M) = size M" | 
| 49823 | 1696 | by (induct M) simp_all | 
| 48023 | 1697 | |
| 60606 | 1698 | lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
 | 
| 49823 | 1699 | by (cases M) auto | 
| 48023 | 1700 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1701 | lemma image_mset_If: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1702 | "image_mset (\<lambda>x. if P x then f x else g x) A = | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1703 | image_mset f (filter_mset P A) + image_mset g (filter_mset (\<lambda>x. \<not>P x) A)" | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1704 | by (induction A) auto | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1705 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1706 | lemma image_mset_Diff: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1707 | assumes "B \<subseteq># A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1708 | shows "image_mset f (A - B) = image_mset f A - image_mset f B" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1709 | proof - | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1710 | have "image_mset f (A - B + B) = image_mset f (A - B) + image_mset f B" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1711 | by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1712 | also from assms have "A - B + B = A" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1713 | by (simp add: subset_mset.diff_add) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1714 | finally show ?thesis by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1715 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1716 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1717 | lemma count_image_mset: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1718 |   "count (image_mset f A) x = (\<Sum>y\<in>f -` {x} \<inter> set_mset A. count A y)"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1719 | proof (induction A) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1720 | case empty | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1721 | then show ?case by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1722 | next | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1723 | case (add x A) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1724 | moreover have *: "(if x = y then Suc n else n) = n + (if x = y then 1 else 0)" for n y | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1725 | by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1726 | ultimately show ?case | 
| 64267 | 1727 | by (auto simp: sum.distrib sum.delta' intro!: sum.mono_neutral_left) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1728 | qed | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1729 | |
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1730 | lemma image_mset_subseteq_mono: "A \<subseteq># B \<Longrightarrow> image_mset f A \<subseteq># image_mset f B" | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1731 | by (metis image_mset_union subset_mset.le_iff_add) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1732 | |
| 65048 | 1733 | lemma image_mset_subset_mono: "M \<subset># N \<Longrightarrow> image_mset f M \<subset># image_mset f N" | 
| 1734 | by (metis (no_types) Diff_eq_empty_iff_mset image_mset_Diff image_mset_is_empty_iff | |
| 1735 | image_mset_subseteq_mono subset_mset.less_le_not_le) | |
| 1736 | ||
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1737 | syntax (ASCII) | 
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1738 |   "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ :# _#})")
 | 
| 48023 | 1739 | syntax | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1740 |   "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ \<in># _#})")
 | 
| 59813 | 1741 | translations | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1742 |   "{#e. x \<in># M#}" \<rightleftharpoons> "CONST image_mset (\<lambda>x. e) M"
 | 
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1743 | |
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1744 | syntax (ASCII) | 
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1745 |   "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ :# _./ _#})")
 | 
| 48023 | 1746 | syntax | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1747 |   "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ \<in># _./ _#})")
 | 
| 59813 | 1748 | translations | 
| 60606 | 1749 |   "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
 | 
| 59813 | 1750 | |
| 60500 | 1751 | text \<open> | 
| 60607 | 1752 |   This allows to write not just filters like @{term "{#x\<in>#M. x<c#}"}
 | 
| 1753 |   but also images like @{term "{#x+x. x\<in>#M #}"} and @{term [source]
 | |
| 1754 |   "{#x+x|x\<in>#M. x<c#}"}, where the latter is currently displayed as
 | |
| 1755 |   @{term "{#x+x|x\<in>#M. x<c#}"}.
 | |
| 60500 | 1756 | \<close> | 
| 48023 | 1757 | |
| 60495 | 1758 | lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1759 | by (metis set_image_mset) | 
| 59813 | 1760 | |
| 55467 
a5c9002bc54d
renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
 blanchet parents: 
55417diff
changeset | 1761 | functor image_mset: image_mset | 
| 48023 | 1762 | proof - | 
| 1763 | fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)" | |
| 1764 | proof | |
| 1765 | fix A | |
| 1766 | show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A" | |
| 1767 | by (induct A) simp_all | |
| 1768 | qed | |
| 1769 | show "image_mset id = id" | |
| 1770 | proof | |
| 1771 | fix A | |
| 1772 | show "image_mset id A = id A" | |
| 1773 | by (induct A) simp_all | |
| 1774 | qed | |
| 1775 | qed | |
| 1776 | ||
| 59813 | 1777 | declare | 
| 1778 | image_mset.id [simp] | |
| 1779 | image_mset.identity [simp] | |
| 1780 | ||
| 1781 | lemma image_mset_id[simp]: "image_mset id x = x" | |
| 1782 | unfolding id_def by auto | |
| 1783 | ||
| 1784 | lemma image_mset_cong: "(\<And>x. x \<in># M \<Longrightarrow> f x = g x) \<Longrightarrow> {#f x. x \<in># M#} = {#g x. x \<in># M#}"
 | |
| 1785 | by (induct M) auto | |
| 1786 | ||
| 1787 | lemma image_mset_cong_pair: | |
| 1788 |   "(\<forall>x y. (x, y) \<in># M \<longrightarrow> f x y = g x y) \<Longrightarrow> {#f x y. (x, y) \<in># M#} = {#g x y. (x, y) \<in># M#}"
 | |
| 1789 | by (metis image_mset_cong split_cong) | |
| 49717 | 1790 | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 1791 | lemma image_mset_const_eq: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 1792 |   "{#c. a \<in># M#} = replicate_mset (size M) c"
 | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 1793 | by (induct M) simp_all | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 1794 | |
| 48023 | 1795 | |
| 60500 | 1796 | subsection \<open>Further conversions\<close> | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1797 | |
| 60515 | 1798 | primrec mset :: "'a list \<Rightarrow> 'a multiset" where | 
| 1799 |   "mset [] = {#}" |
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1800 | "mset (a # x) = add_mset a (mset 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 | 1801 | |
| 37107 | 1802 | lemma in_multiset_in_set: | 
| 60515 | 1803 | "x \<in># mset xs \<longleftrightarrow> x \<in> set xs" | 
| 37107 | 1804 | by (induct xs) simp_all | 
| 1805 | ||
| 60515 | 1806 | lemma count_mset: | 
| 1807 | "count (mset xs) x = length (filter (\<lambda>y. x = y) xs)" | |
| 37107 | 1808 | by (induct xs) simp_all | 
| 1809 | ||
| 60515 | 1810 | lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])"
 | 
| 59813 | 1811 | by (induct x) auto | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1812 | |
| 60515 | 1813 | lemma mset_zero_iff_right[simp]: "({#} = mset x) = (x = [])"
 | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1814 | 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 | 1815 | |
| 64077 | 1816 | lemma mset_single_iff[iff]: "mset xs = {#x#} \<longleftrightarrow> xs = [x]"
 | 
| 1817 | by (cases xs) auto | |
| 1818 | ||
| 1819 | lemma mset_single_iff_right[iff]: "{#x#} = mset xs \<longleftrightarrow> xs = [x]"
 | |
| 1820 | by (cases xs) auto | |
| 1821 | ||
| 64076 | 1822 | lemma set_mset_mset[simp]: "set_mset (mset xs) = set xs" | 
| 1823 | 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 | 1824 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1825 | lemma set_mset_comp_mset [simp]: "set_mset \<circ> mset = set" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1826 | by (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 | 1827 | |
| 60515 | 1828 | lemma size_mset [simp]: "size (mset xs) = length xs" | 
| 48012 | 1829 | by (induct xs) simp_all | 
| 1830 | ||
| 60606 | 1831 | lemma mset_append [simp]: "mset (xs @ ys) = mset xs + mset ys" | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1832 | by (induct xs arbitrary: ys) 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 | 1833 | |
| 60607 | 1834 | lemma mset_filter: "mset (filter P xs) = {#x \<in># mset xs. P x #}"
 | 
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1835 | by (induct xs) simp_all | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1836 | |
| 60515 | 1837 | lemma mset_rev [simp]: | 
| 1838 | "mset (rev xs) = mset xs" | |
| 40950 | 1839 | by (induct xs) simp_all | 
| 1840 | ||
| 60515 | 1841 | lemma surj_mset: "surj mset" | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1842 | 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 | 1843 | 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 | 1844 | 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 | 1845 | 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 | 1846 | 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 | 1847 | 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 | 1848 | 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 | 1849 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1850 | lemma distinct_count_atmost_1: | 
| 60606 | 1851 | "distinct x = (\<forall>a. count (mset x) a = (if a \<in> set x then 1 else 0))" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1852 | proof (induct x) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1853 | case Nil then show ?case by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1854 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1855 | case (Cons x xs) show ?case (is "?lhs \<longleftrightarrow> ?rhs") | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1856 | proof | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1857 | assume ?lhs then show ?rhs using Cons by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1858 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1859 | assume ?rhs then have "x \<notin> set xs" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1860 | by (simp split: if_splits) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1861 | moreover from \<open>?rhs\<close> have "(\<forall>a. count (mset xs) a = | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1862 | (if a \<in> set xs then 1 else 0))" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1863 | by (auto split: if_splits simp add: count_eq_zero_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1864 | ultimately show ?lhs using Cons by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1865 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1866 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1867 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1868 | lemma mset_eq_setD: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1869 | assumes "mset xs = mset ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1870 | shows "set xs = set ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1871 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1872 | from assms have "set_mset (mset xs) = set_mset (mset ys)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1873 | by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1874 | then show ?thesis by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1875 | qed | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1876 | |
| 60515 | 1877 | lemma set_eq_iff_mset_eq_distinct: | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1878 | "distinct x \<Longrightarrow> distinct y \<Longrightarrow> | 
| 60515 | 1879 | (set x = set y) = (mset x = mset y)" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 1880 | 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 | 1881 | |
| 60515 | 1882 | lemma set_eq_iff_mset_remdups_eq: | 
| 1883 | "(set x = set y) = (mset (remdups x) = mset (remdups y))" | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1884 | apply (rule iffI) | 
| 60515 | 1885 | apply (simp add: set_eq_iff_mset_eq_distinct[THEN iffD1]) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1886 | apply (drule distinct_remdups [THEN distinct_remdups | 
| 60515 | 1887 | [THEN set_eq_iff_mset_eq_distinct [THEN iffD2]]]) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1888 | 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 | 1889 | 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 | 1890 | |
| 60606 | 1891 | lemma mset_compl_union [simp]: "mset [x\<leftarrow>xs. P x] + mset [x\<leftarrow>xs. \<not>P x] = mset xs" | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1892 | 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 | 1893 | |
| 60607 | 1894 | lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) \<in># mset ls" | 
| 60678 | 1895 | proof (induct ls arbitrary: i) | 
| 1896 | case Nil | |
| 1897 | then show ?case by simp | |
| 1898 | next | |
| 1899 | case Cons | |
| 1900 | then show ?case by (cases i) auto | |
| 1901 | qed | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1902 | |
| 60606 | 1903 | lemma mset_remove1[simp]: "mset (remove1 a xs) = mset xs - {#a#}"
 | 
| 60678 | 1904 | 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 | 1905 | |
| 60515 | 1906 | lemma mset_eq_length: | 
| 1907 | assumes "mset xs = mset ys" | |
| 37107 | 1908 | shows "length xs = length ys" | 
| 60515 | 1909 | using assms by (metis size_mset) | 
| 1910 | ||
| 1911 | lemma mset_eq_length_filter: | |
| 1912 | assumes "mset xs = mset ys" | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1913 | shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)" | 
| 60515 | 1914 | using assms by (metis count_mset) | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 1915 | |
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1916 | lemma fold_multiset_equiv: | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1917 | assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" | 
| 60515 | 1918 | and equiv: "mset xs = mset ys" | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1919 | shows "List.fold f xs = List.fold f ys" | 
| 60606 | 1920 | using f equiv [symmetric] | 
| 46921 | 1921 | proof (induct xs arbitrary: ys) | 
| 60678 | 1922 | case Nil | 
| 1923 | then show ?case by simp | |
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1924 | next | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1925 | case (Cons x xs) | 
| 60678 | 1926 | then have *: "set ys = set (x # xs)" | 
| 1927 | by (blast dest: mset_eq_setD) | |
| 58425 | 1928 | have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" | 
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1929 | by (rule Cons.prems(1)) (simp_all add: *) | 
| 60678 | 1930 | moreover from * have "x \<in> set ys" | 
| 1931 | by simp | |
| 1932 | ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" | |
| 1933 | by (fact fold_remove1_split) | |
| 1934 | moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" | |
| 1935 | by (auto intro: Cons.hyps) | |
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1936 | ultimately show ?case by simp | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1937 | qed | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 1938 | |
| 65354 | 1939 | lemma mset_shuffle: "zs \<in> shuffle xs ys \<Longrightarrow> mset zs = mset xs + mset ys" | 
| 65350 
b149abe619f7
added shuffle product to HOL/List
 eberlm <eberlm@in.tum.de> parents: 
65048diff
changeset | 1940 | by (induction xs ys arbitrary: zs rule: shuffle.induct) auto | 
| 
b149abe619f7
added shuffle product to HOL/List
 eberlm <eberlm@in.tum.de> parents: 
65048diff
changeset | 1941 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1942 | lemma mset_insort [simp]: "mset (insort x xs) = add_mset x (mset xs)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1943 | by (induct xs) simp_all | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1944 | |
| 63524 
4ec755485732
adding mset_map to the simp rules
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63410diff
changeset | 1945 | lemma mset_map[simp]: "mset (map f xs) = image_mset f (mset xs)" | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1946 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1947 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1948 | global_interpretation mset_set: folding add_mset "{#}"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1949 |   defines mset_set = "folding.F add_mset {#}"
 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1950 | by standard (simp add: fun_eq_iff) | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 1951 | |
| 60513 | 1952 | lemma count_mset_set [simp]: | 
| 1953 | "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (mset_set A) x = 1" (is "PROP ?P") | |
| 1954 | "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q") | |
| 1955 | "x \<notin> A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?R") | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1956 | proof - | 
| 60606 | 1957 | have *: "count (mset_set A) x = 0" if "x \<notin> A" for A | 
| 1958 | proof (cases "finite A") | |
| 1959 | case False then show ?thesis by simp | |
| 1960 | next | |
| 1961 | case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto | |
| 1962 | qed | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 1963 | 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 | 1964 | by (auto elim!: Set.set_insert) | 
| 61585 | 1965 | qed \<comment> \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close>
 | 
| 60513 | 1966 | |
| 1967 | lemma elem_mset_set[simp, intro]: "finite A \<Longrightarrow> x \<in># mset_set A \<longleftrightarrow> x \<in> A" | |
| 59813 | 1968 | by (induct A rule: finite_induct) simp_all | 
| 1969 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1970 | lemma mset_set_Union: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1971 |   "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> mset_set (A \<union> B) = mset_set A + mset_set B"
 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1972 | by (induction A rule: finite_induct) auto | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1973 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1974 | lemma filter_mset_mset_set [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1975 |   "finite A \<Longrightarrow> filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1976 | proof (induction A rule: finite_induct) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1977 | case (insert x A) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1978 | from insert.hyps have "filter_mset P (mset_set (insert x A)) = | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1979 |       filter_mset P (mset_set A) + mset_set (if P x then {x} else {})"
 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1980 | by simp | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1981 |   also have "filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1982 | by (rule insert.IH) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1983 | also from insert.hyps | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1984 |     have "\<dots> + mset_set (if P x then {x} else {}) =
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1985 |             mset_set ({x \<in> A. P x} \<union> (if P x then {x} else {}))" (is "_ = mset_set ?A")
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1986 | by (intro mset_set_Union [symmetric]) simp_all | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1987 |   also from insert.hyps have "?A = {y\<in>insert x A. P y}" by auto
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1988 | finally show ?case . | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1989 | qed simp_all | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1990 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1991 | lemma mset_set_Diff: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1992 | assumes "finite A" "B \<subseteq> A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1993 | shows "mset_set (A - B) = mset_set A - mset_set B" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1994 | proof - | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1995 | from assms have "mset_set ((A - B) \<union> B) = mset_set (A - B) + mset_set B" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1996 | by (intro mset_set_Union) (auto dest: finite_subset) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1997 | also from assms have "A - B \<union> B = A" by blast | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1998 | finally show ?thesis by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1999 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2000 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2001 | lemma mset_set_set: "distinct xs \<Longrightarrow> mset_set (set xs) = mset xs" | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2002 | by (induction xs) simp_all | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2003 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2004 | context linorder | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2005 | begin | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2006 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2007 | definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2008 | where | 
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 2009 | "sorted_list_of_multiset M = fold_mset insort [] M" | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2010 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2011 | lemma sorted_list_of_multiset_empty [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2012 |   "sorted_list_of_multiset {#} = []"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2013 | by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2014 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2015 | lemma sorted_list_of_multiset_singleton [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2016 |   "sorted_list_of_multiset {#x#} = [x]"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2017 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2018 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2019 | show ?thesis by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2020 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2021 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2022 | lemma sorted_list_of_multiset_insert [simp]: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2023 | "sorted_list_of_multiset (add_mset x M) = List.insort x (sorted_list_of_multiset M)" | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2024 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2025 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2026 | show ?thesis by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2027 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2028 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2029 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2030 | |
| 60515 | 2031 | lemma mset_sorted_list_of_multiset [simp]: | 
| 2032 | "mset (sorted_list_of_multiset M) = M" | |
| 60513 | 2033 | by (induct M) simp_all | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2034 | |
| 60515 | 2035 | lemma sorted_list_of_multiset_mset [simp]: | 
| 2036 | "sorted_list_of_multiset (mset xs) = sort xs" | |
| 60513 | 2037 | by (induct xs) simp_all | 
| 2038 | ||
| 2039 | lemma finite_set_mset_mset_set[simp]: | |
| 2040 | "finite A \<Longrightarrow> set_mset (mset_set A) = A" | |
| 2041 | by (induct A rule: finite_induct) simp_all | |
| 2042 | ||
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2043 | lemma mset_set_empty_iff: "mset_set A = {#} \<longleftrightarrow> A = {} \<or> infinite A"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2044 | using finite_set_mset_mset_set by fastforce | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2045 | |
| 60513 | 2046 | lemma infinite_set_mset_mset_set: | 
| 2047 |   "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
 | |
| 2048 | by simp | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2049 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2050 | lemma set_sorted_list_of_multiset [simp]: | 
| 60495 | 2051 | "set (sorted_list_of_multiset M) = set_mset M" | 
| 60513 | 2052 | by (induct M) (simp_all add: set_insort) | 
| 2053 | ||
| 2054 | lemma sorted_list_of_mset_set [simp]: | |
| 2055 | "sorted_list_of_multiset (mset_set A) = sorted_list_of_set A" | |
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2056 | by (cases "finite A") (induct A rule: finite_induct, simp_all) | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2057 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2058 | lemma mset_upt [simp]: "mset [m..<n] = mset_set {m..<n}"
 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2059 | by (induction n) (simp_all add: atLeastLessThanSuc) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2060 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2061 | lemma image_mset_map_of: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2062 |   "distinct (map fst xs) \<Longrightarrow> {#the (map_of xs i). i \<in># mset (map fst xs)#} = mset (map snd xs)"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2063 | proof (induction xs) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2064 | case (Cons x xs) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2065 |   have "{#the (map_of (x # xs) i). i \<in># mset (map fst (x # xs))#} =
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2066 |           add_mset (snd x) {#the (if i = fst x then Some (snd x) else map_of xs i).
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2067 | i \<in># mset (map fst xs)#}" (is "_ = add_mset _ ?A") by simp | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2068 |   also from Cons.prems have "?A = {#the (map_of xs i). i :# mset (map fst xs)#}"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2069 | by (cases x, intro image_mset_cong) (auto simp: in_multiset_in_set) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2070 | also from Cons.prems have "\<dots> = mset (map snd xs)" by (intro Cons.IH) simp_all | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2071 | finally show ?case by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2072 | qed simp_all | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2073 | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2074 | (* Contributed by Lukas Bulwahn *) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2075 | lemma image_mset_mset_set: | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2076 | assumes "inj_on f A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2077 | shows "image_mset f (mset_set A) = mset_set (f ` A)" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2078 | proof cases | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2079 | assume "finite A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2080 | from this \<open>inj_on f A\<close> show ?thesis | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2081 | by (induct A) auto | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2082 | next | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2083 | assume "infinite A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2084 | from this \<open>inj_on f A\<close> have "infinite (f ` A)" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2085 | using finite_imageD by blast | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2086 | from \<open>infinite A\<close> \<open>infinite (f ` A)\<close> show ?thesis by simp | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2087 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2088 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2089 | |
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 2090 | subsection \<open>More properties of the replicate and repeat operations\<close> | 
| 60804 | 2091 | |
| 2092 | lemma in_replicate_mset[simp]: "x \<in># replicate_mset n y \<longleftrightarrow> n > 0 \<and> x = y" | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2093 | unfolding replicate_mset_def by (induct n) auto | 
| 60804 | 2094 | |
| 2095 | lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})"
 | |
| 2096 | by (auto split: if_splits) | |
| 2097 | ||
| 2098 | lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n" | |
| 2099 | by (induct n, simp_all) | |
| 2100 | ||
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 2101 | lemma count_le_replicate_mset_subset_eq: "n \<le> count M x \<longleftrightarrow> replicate_mset n x \<subseteq># M" | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 2102 | by (auto simp add: mset_subset_eqI) (metis count_replicate_mset subseteq_mset_def) | 
| 60804 | 2103 | |
| 2104 | lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x"
 | |
| 2105 | by (induct D) simp_all | |
| 2106 | ||
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2107 | lemma replicate_count_mset_eq_filter_eq: | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2108 | "replicate (count (mset xs) k) k = filter (HOL.eq k) xs" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2109 | by (induct xs) auto | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2110 | |
| 62366 | 2111 | lemma replicate_mset_eq_empty_iff [simp]: | 
| 2112 |   "replicate_mset n a = {#} \<longleftrightarrow> n = 0"
 | |
| 2113 | by (induct n) simp_all | |
| 2114 | ||
| 2115 | lemma replicate_mset_eq_iff: | |
| 2116 | "replicate_mset m a = replicate_mset n b \<longleftrightarrow> | |
| 2117 | m = 0 \<and> n = 0 \<or> m = n \<and> a = b" | |
| 2118 | by (auto simp add: multiset_eq_iff) | |
| 2119 | ||
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 2120 | lemma repeat_mset_cancel1: "repeat_mset a A = repeat_mset a B \<longleftrightarrow> A = B \<or> a = 0" | 
| 63849 
0dd6731060d7
delete looping simp rule
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63831diff
changeset | 2121 | by (auto simp: multiset_eq_iff) | 
| 
0dd6731060d7
delete looping simp rule
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63831diff
changeset | 2122 | |
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 2123 | lemma repeat_mset_cancel2: "repeat_mset a A = repeat_mset b A \<longleftrightarrow> a = b \<or> A = {#}"
 | 
| 63849 
0dd6731060d7
delete looping simp rule
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63831diff
changeset | 2124 | by (auto simp: multiset_eq_iff) | 
| 
0dd6731060d7
delete looping simp rule
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63831diff
changeset | 2125 | |
| 64077 | 2126 | lemma repeat_mset_eq_empty_iff: "repeat_mset n A = {#} \<longleftrightarrow> n = 0 \<or> A = {#}"
 | 
| 2127 | by (cases n) auto | |
| 2128 | ||
| 63924 | 2129 | lemma image_replicate_mset [simp]: | 
| 2130 | "image_mset f (replicate_mset n a) = replicate_mset n (f a)" | |
| 2131 | by (induct n) simp_all | |
| 2132 | ||
| 60804 | 2133 | |
| 60500 | 2134 | subsection \<open>Big operators\<close> | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2135 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2136 | locale comm_monoid_mset = comm_monoid | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2137 | begin | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2138 | |
| 64075 | 2139 | interpretation comp_fun_commute f | 
| 2140 | by standard (simp add: fun_eq_iff left_commute) | |
| 2141 | ||
| 2142 | interpretation comp?: comp_fun_commute "f \<circ> g" | |
| 2143 | by (fact comp_comp_fun_commute) | |
| 2144 | ||
| 2145 | context | |
| 2146 | begin | |
| 2147 | ||
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2148 | definition F :: "'a multiset \<Rightarrow> 'a" | 
| 63290 
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
 haftmann parents: 
63195diff
changeset | 2149 | where eq_fold: "F M = fold_mset f \<^bold>1 M" | 
| 
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
 haftmann parents: 
63195diff
changeset | 2150 | |
| 
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
 haftmann parents: 
63195diff
changeset | 2151 | lemma empty [simp]: "F {#} = \<^bold>1"
 | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2152 | by (simp add: eq_fold) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2153 | |
| 60678 | 2154 | lemma singleton [simp]: "F {#x#} = x"
 | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2155 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2156 | interpret comp_fun_commute | 
| 60678 | 2157 | by standard (simp add: fun_eq_iff left_commute) | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2158 | show ?thesis by (simp add: eq_fold) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2159 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2160 | |
| 63290 
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
 haftmann parents: 
63195diff
changeset | 2161 | lemma union [simp]: "F (M + N) = F M \<^bold>* F N" | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2162 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2163 | interpret comp_fun_commute f | 
| 60678 | 2164 | by standard (simp add: fun_eq_iff left_commute) | 
| 2165 | show ?thesis | |
| 2166 | by (induct N) (simp_all add: left_commute eq_fold) | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2167 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2168 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2169 | lemma add_mset [simp]: "F (add_mset x N) = x \<^bold>* F N" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2170 | unfolding add_mset_add_single[of x N] union by (simp add: ac_simps) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2171 | |
| 64075 | 2172 | lemma insert [simp]: | 
| 2173 | shows "F (image_mset g (add_mset x A)) = g x \<^bold>* F (image_mset g A)" | |
| 2174 | by (simp add: eq_fold) | |
| 2175 | ||
| 2176 | lemma remove: | |
| 2177 | assumes "x \<in># A" | |
| 2178 |   shows "F A = x \<^bold>* F (A - {#x#})"
 | |
| 2179 | using multi_member_split[OF assms] by auto | |
| 2180 | ||
| 2181 | lemma neutral: | |
| 2182 | "\<forall>x\<in>#A. x = \<^bold>1 \<Longrightarrow> F A = \<^bold>1" | |
| 2183 | by (induct A) simp_all | |
| 2184 | ||
| 2185 | lemma neutral_const [simp]: | |
| 2186 | "F (image_mset (\<lambda>_. \<^bold>1) A) = \<^bold>1" | |
| 2187 | by (simp add: neutral) | |
| 2188 | ||
| 2189 | private lemma F_image_mset_product: | |
| 2190 |   "F {#g x j \<^bold>* F {#g i j. i \<in># A#}. j \<in># B#} =
 | |
| 2191 |     F (image_mset (g x) B) \<^bold>* F {#F {#g i j. i \<in># A#}. j \<in># B#}"
 | |
| 2192 | by (induction B) (simp_all add: left_commute semigroup.assoc semigroup_axioms) | |
| 2193 | ||
| 2194 | lemma commute: | |
| 2195 | "F (image_mset (\<lambda>i. F (image_mset (g i) B)) A) = | |
| 2196 | F (image_mset (\<lambda>j. F (image_mset (\<lambda>i. g i j) A)) B)" | |
| 2197 | apply (induction A, simp) | |
| 2198 | apply (induction B, auto simp add: F_image_mset_product ac_simps) | |
| 2199 | done | |
| 2200 | ||
| 2201 | lemma distrib: "F (image_mset (\<lambda>x. g x \<^bold>* h x) A) = F (image_mset g A) \<^bold>* F (image_mset h A)" | |
| 2202 | by (induction A) (auto simp: ac_simps) | |
| 2203 | ||
| 2204 | lemma union_disjoint: | |
| 2205 |   "A \<inter># B = {#} \<Longrightarrow> F (image_mset g (A \<union># B)) = F (image_mset g A) \<^bold>* F (image_mset g B)"
 | |
| 2206 | by (induction A) (auto simp: ac_simps) | |
| 2207 | ||
| 2208 | end | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2209 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2210 | |
| 61076 | 2211 | lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)" | 
| 60678 | 2212 | by standard (simp add: add_ac comp_def) | 
| 59813 | 2213 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2214 | declare comp_fun_commute.fold_mset_add_mset[OF comp_fun_commute_plus_mset, simp] | 
| 59813 | 2215 | |
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 2216 | lemma in_mset_fold_plus_iff[iff]: "x \<in># fold_mset (op +) M NN \<longleftrightarrow> x \<in># M \<or> (\<exists>N. N \<in># NN \<and> x \<in># N)" | 
| 59813 | 2217 | by (induct NN) auto | 
| 2218 | ||
| 54868 | 2219 | context comm_monoid_add | 
| 2220 | begin | |
| 2221 | ||
| 63830 | 2222 | sublocale sum_mset: comm_monoid_mset plus 0 | 
| 2223 | defines sum_mset = sum_mset.F .. | |
| 2224 | ||
| 2225 | lemma (in semiring_1) sum_mset_replicate_mset [simp]: | |
| 2226 | "sum_mset (replicate_mset n a) = of_nat n * a" | |
| 60804 | 2227 | by (induct n) (simp_all add: algebra_simps) | 
| 2228 | ||
| 64267 | 2229 | lemma sum_unfold_sum_mset: | 
| 2230 | "sum f A = sum_mset (image_mset f (mset_set A))" | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2231 | by (cases "finite A") (induct A rule: finite_induct, simp_all) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2232 | |
| 63830 | 2233 | lemma sum_mset_delta: "sum_mset (image_mset (\<lambda>x. if x = y then c else 0) A) = c * count A y" | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 2234 | by (induction A) simp_all | 
| 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 2235 | |
| 63830 | 2236 | lemma sum_mset_delta': "sum_mset (image_mset (\<lambda>x. if y = x then c else 0) A) = c * count A y" | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 2237 | by (induction A) simp_all | 
| 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 2238 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2239 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2240 | |
| 63860 | 2241 | lemma of_nat_sum_mset [simp]: | 
| 2242 | "of_nat (sum_mset M) = sum_mset (image_mset of_nat M)" | |
| 2243 | by (induction M) auto | |
| 2244 | ||
| 2245 | lemma sum_mset_0_iff [simp]: | |
| 2246 | "sum_mset M = (0::'a::canonically_ordered_monoid_add) | |
| 2247 | \<longleftrightarrow> (\<forall>x \<in> set_mset M. x = 0)" | |
| 64418 | 2248 | by(induction M) auto | 
| 63860 | 2249 | |
| 63830 | 2250 | lemma sum_mset_diff: | 
| 61076 | 2251 |   fixes M N :: "('a :: ordered_cancel_comm_monoid_diff) multiset"
 | 
| 63830 | 2252 | shows "N \<subseteq># M \<Longrightarrow> sum_mset (M - N) = sum_mset M - sum_mset N" | 
| 2253 | by (metis add_diff_cancel_right' sum_mset.union subset_mset.diff_add) | |
| 2254 | ||
| 2255 | lemma size_eq_sum_mset: "size M = sum_mset (image_mset (\<lambda>_. 1) M)" | |
| 59949 | 2256 | proof (induct M) | 
| 2257 | case empty then show ?case by simp | |
| 2258 | next | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2259 | case (add x M) then show ?case | 
| 60495 | 2260 | by (cases "x \<in> set_mset M") | 
| 64267 | 2261 | (simp_all add: size_multiset_overloaded_eq not_in_iff sum.If_cases Diff_eq[symmetric] | 
| 2262 | sum.remove) | |
| 59949 | 2263 | qed | 
| 2264 | ||
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2265 | lemma size_mset_set [simp]: "size (mset_set A) = card A" | 
| 64267 | 2266 | by (simp only: size_eq_sum_mset card_eq_sum sum_unfold_sum_mset) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2267 | |
| 64418 | 2268 | lemma sum_mset_sum_list: "sum_mset (mset xs) = sum_list xs" | 
| 2269 | by (induction xs) auto | |
| 2270 | ||
| 62366 | 2271 | syntax (ASCII) | 
| 63830 | 2272 |   "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3SUM _:#_. _)" [0, 51, 10] 10)
 | 
| 62366 | 2273 | syntax | 
| 63830 | 2274 |   "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
 | 
| 62366 | 2275 | translations | 
| 63830 | 2276 | "\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST sum_mset (CONST image_mset (\<lambda>i. b) A)" | 
| 59949 | 2277 | |
| 63860 | 2278 | lemma sum_mset_distrib_left: | 
| 2279 | fixes f :: "'a \<Rightarrow> 'b::semiring_0" | |
| 2280 | shows "c * (\<Sum>x \<in># M. f x) = (\<Sum>x \<in># M. c * f(x))" | |
| 2281 | by (induction M) (simp_all add: distrib_left) | |
| 2282 | ||
| 64075 | 2283 | lemma sum_mset_distrib_right: | 
| 2284 | fixes f :: "'a \<Rightarrow> 'b::semiring_0" | |
| 2285 | shows "(\<Sum>b \<in># B. f b) * a = (\<Sum>b \<in># B. f b * a)" | |
| 2286 | by (induction B) (auto simp: distrib_right) | |
| 2287 | ||
| 2288 | lemma sum_mset_constant [simp]: | |
| 2289 | fixes y :: "'b::semiring_1" | |
| 2290 | shows \<open>(\<Sum>x\<in>#A. y) = of_nat (size A) * y\<close> | |
| 2291 | by (induction A) (auto simp: algebra_simps) | |
| 2292 | ||
| 2293 | lemma (in ordered_comm_monoid_add) sum_mset_mono: | |
| 2294 | assumes "\<And>i. i \<in># K \<Longrightarrow> f i \<le> g i" | |
| 2295 | shows "sum_mset (image_mset f K) \<le> sum_mset (image_mset g K)" | |
| 2296 | using assms by (induction K) (simp_all add: local.add_mono) | |
| 2297 | ||
| 2298 | lemma sum_mset_product: | |
| 2299 |   fixes f :: "'a::{comm_monoid_add,times} \<Rightarrow> 'b::semiring_0"
 | |
| 2300 | shows "(\<Sum>i \<in># A. f i) * (\<Sum>i \<in># B. g i) = (\<Sum>i\<in>#A. \<Sum>j\<in>#B. f i * g j)" | |
| 2301 | by (subst sum_mset.commute) (simp add: sum_mset_distrib_left sum_mset_distrib_right) | |
| 2302 | ||
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 2303 | abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset"  ("\<Union>#_" [900] 900)
 | 
| 63830 | 2304 | where "\<Union># MM \<equiv> sum_mset MM" \<comment> \<open>FIXME ambiguous notation -- | 
| 62837 | 2305 | could likewise refer to \<open>\<Squnion>#\<close>\<close> | 
| 59813 | 2306 | |
| 60495 | 2307 | lemma set_mset_Union_mset[simp]: "set_mset (\<Union># MM) = (\<Union>M \<in> set_mset MM. set_mset M)" | 
| 59813 | 2308 | by (induct MM) auto | 
| 2309 | ||
| 2310 | lemma in_Union_mset_iff[iff]: "x \<in># \<Union># MM \<longleftrightarrow> (\<exists>M. M \<in># MM \<and> x \<in># M)" | |
| 2311 | by (induct MM) auto | |
| 2312 | ||
| 64267 | 2313 | lemma count_sum: | 
| 2314 | "count (sum f A) x = sum (\<lambda>a. count (f a) x) A" | |
| 62366 | 2315 | by (induct A rule: infinite_finite_induct) simp_all | 
| 2316 | ||
| 64267 | 2317 | lemma sum_eq_empty_iff: | 
| 62366 | 2318 | assumes "finite A" | 
| 64267 | 2319 |   shows "sum f A = {#} \<longleftrightarrow> (\<forall>a\<in>A. f a = {#})"
 | 
| 62366 | 2320 | using assms by induct simp_all | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2321 | |
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2322 | lemma Union_mset_empty_conv[simp]: "\<Union># M = {#} \<longleftrightarrow> (\<forall>i\<in>#M. i = {#})"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2323 | by (induction M) auto | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2324 | |
| 54868 | 2325 | context comm_monoid_mult | 
| 2326 | begin | |
| 2327 | ||
| 63830 | 2328 | sublocale prod_mset: comm_monoid_mset times 1 | 
| 2329 | defines prod_mset = prod_mset.F .. | |
| 2330 | ||
| 2331 | lemma prod_mset_empty: | |
| 2332 |   "prod_mset {#} = 1"
 | |
| 2333 | by (fact prod_mset.empty) | |
| 2334 | ||
| 2335 | lemma prod_mset_singleton: | |
| 2336 |   "prod_mset {#x#} = x"
 | |
| 2337 | by (fact prod_mset.singleton) | |
| 2338 | ||
| 2339 | lemma prod_mset_Un: | |
| 2340 | "prod_mset (A + B) = prod_mset A * prod_mset B" | |
| 2341 | by (fact prod_mset.union) | |
| 2342 | ||
| 2343 | lemma prod_mset_replicate_mset [simp]: | |
| 2344 | "prod_mset (replicate_mset n a) = a ^ n" | |
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2345 | by (induct n) simp_all | 
| 60804 | 2346 | |
| 64272 | 2347 | lemma prod_unfold_prod_mset: | 
| 2348 | "prod f A = prod_mset (image_mset f (mset_set A))" | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2349 | by (cases "finite A") (induct A rule: finite_induct, simp_all) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2350 | |
| 63830 | 2351 | lemma prod_mset_multiplicity: | 
| 64272 | 2352 | "prod_mset M = prod (\<lambda>x. x ^ count M x) (set_mset M)" | 
| 2353 | by (simp add: fold_mset_def prod.eq_fold prod_mset.eq_fold funpow_times_power comp_def) | |
| 63830 | 2354 | |
| 2355 | lemma prod_mset_delta: "prod_mset (image_mset (\<lambda>x. if x = y then c else 1) A) = c ^ count A y" | |
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2356 | by (induction A) simp_all | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 2357 | |
| 63830 | 2358 | lemma prod_mset_delta': "prod_mset (image_mset (\<lambda>x. if y = x then c else 1) A) = c ^ count A y" | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2359 | by (induction A) simp_all | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 2360 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2361 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2362 | |
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 2363 | syntax (ASCII) | 
| 63830 | 2364 |   "_prod_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3PROD _:#_. _)" [0, 51, 10] 10)
 | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2365 | syntax | 
| 63830 | 2366 |   "_prod_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
 | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2367 | translations | 
| 63830 | 2368 | "\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST prod_mset (CONST image_mset (\<lambda>i. b) A)" | 
| 2369 | ||
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 2370 | lemma prod_mset_constant [simp]: "(\<Prod>_\<in>#A. c) = c ^ size A" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 2371 | by (simp add: image_mset_const_eq) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 2372 | |
| 63924 | 2373 | lemma (in comm_monoid_mult) prod_mset_subset_imp_dvd: | 
| 2374 | assumes "A \<subseteq># B" | |
| 2375 | shows "prod_mset A dvd prod_mset B" | |
| 2376 | proof - | |
| 2377 | from assms have "B = (B - A) + A" by (simp add: subset_mset.diff_add) | |
| 2378 | also have "prod_mset \<dots> = prod_mset (B - A) * prod_mset A" by simp | |
| 2379 | also have "prod_mset A dvd \<dots>" by simp | |
| 2380 | finally show ?thesis . | |
| 2381 | qed | |
| 2382 | ||
| 2383 | lemma (in comm_monoid_mult) dvd_prod_mset: | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2384 | assumes "x \<in># A" | 
| 63830 | 2385 | shows "x dvd prod_mset A" | 
| 63924 | 2386 |   using assms prod_mset_subset_imp_dvd [of "{#x#}" A] by simp
 | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2387 | |
| 63830 | 2388 | lemma (in semidom) prod_mset_zero_iff [iff]: | 
| 2389 | "prod_mset A = 0 \<longleftrightarrow> 0 \<in># A" | |
| 62366 | 2390 | by (induct A) auto | 
| 2391 | ||
| 63830 | 2392 | lemma (in semidom_divide) prod_mset_diff: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2393 | assumes "B \<subseteq># A" and "0 \<notin># B" | 
| 63830 | 2394 | shows "prod_mset (A - B) = prod_mset A div prod_mset B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2395 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2396 | from assms obtain C where "A = B + C" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2397 | by (metis subset_mset.add_diff_inverse) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2398 | with assms show ?thesis by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2399 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2400 | |
| 63830 | 2401 | lemma (in semidom_divide) prod_mset_minus: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2402 | assumes "a \<in># A" and "a \<noteq> 0" | 
| 63830 | 2403 |   shows "prod_mset (A - {#a#}) = prod_mset A div a"
 | 
| 2404 |   using assms prod_mset_diff [of "{#a#}" A] by auto
 | |
| 2405 | ||
| 63924 | 2406 | lemma (in algebraic_semidom) is_unit_prod_mset_iff: | 
| 2407 | "is_unit (prod_mset A) \<longleftrightarrow> (\<forall>x \<in># A. is_unit x)" | |
| 2408 | by (induct A) (auto simp: is_unit_mult_iff) | |
| 2409 | ||
| 2410 | lemma (in normalization_semidom) normalize_prod_mset: | |
| 2411 | "normalize (prod_mset A) = prod_mset (image_mset normalize A)" | |
| 2412 | by (induct A) (simp_all add: normalize_mult) | |
| 2413 | ||
| 63830 | 2414 | lemma (in normalization_semidom) normalized_prod_msetI: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2415 | assumes "\<And>a. a \<in># A \<Longrightarrow> normalize a = a" | 
| 63830 | 2416 | shows "normalize (prod_mset A) = prod_mset A" | 
| 63924 | 2417 | proof - | 
| 2418 | from assms have "image_mset normalize A = A" | |
| 2419 | by (induct A) simp_all | |
| 2420 | then show ?thesis by (simp add: normalize_prod_mset) | |
| 2421 | qed | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2422 | |
| 64418 | 2423 | lemma prod_mset_prod_list: "prod_mset (mset xs) = prod_list xs" | 
| 2424 | by (induct xs) auto | |
| 2425 | ||
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2426 | |
| 60500 | 2427 | subsection \<open>Alternative representations\<close> | 
| 2428 | ||
| 2429 | subsubsection \<open>Lists\<close> | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2430 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2431 | context linorder | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2432 | begin | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2433 | |
| 60515 | 2434 | lemma mset_insort [simp]: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2435 | "mset (insort_key k x xs) = add_mset x (mset xs)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2436 | by (induct xs) simp_all | 
| 58425 | 2437 | |
| 60515 | 2438 | lemma mset_sort [simp]: | 
| 2439 | "mset (sort_key k xs) = mset xs" | |
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2440 | by (induct xs) simp_all | 
| 37107 | 2441 | |
| 60500 | 2442 | text \<open> | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 2443 | This lemma shows which properties suffice to show that a function | 
| 61585 | 2444 | \<open>f\<close> with \<open>f xs = ys\<close> behaves like sort. | 
| 60500 | 2445 | \<close> | 
| 37074 | 2446 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2447 | lemma properties_for_sort_key: | 
| 60515 | 2448 | assumes "mset ys = mset xs" | 
| 60606 | 2449 | and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs" | 
| 2450 | and "sorted (map f ys)" | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2451 | shows "sort_key f xs = ys" | 
| 60606 | 2452 | using assms | 
| 46921 | 2453 | 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 | 2454 | 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 | 2455 | 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 | 2456 | case (Cons x xs) | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2457 | from Cons.prems(2) have | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2458 | "\<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 | 2459 | by (simp add: filter_remove1) | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2460 | 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 | 2461 | by (auto intro!: Cons.hyps simp add: sorted_map_remove1) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2462 | moreover from Cons.prems have "x \<in># mset ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2463 | by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2464 | then have "x \<in> set ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2465 | by simp | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2466 | 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 | 2467 | 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 | 2468 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2469 | lemma properties_for_sort: | 
| 60515 | 2470 | assumes multiset: "mset ys = mset xs" | 
| 60606 | 2471 | and "sorted ys" | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2472 | shows "sort xs = ys" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2473 | proof (rule properties_for_sort_key) | 
| 60515 | 2474 | from multiset show "mset ys = mset xs" . | 
| 60500 | 2475 | from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp | 
| 60678 | 2476 | from multiset have "length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" for k | 
| 60515 | 2477 | by (rule mset_eq_length_filter) | 
| 60678 | 2478 | then have "replicate (length (filter (\<lambda>y. k = y) ys)) k = | 
| 2479 | replicate (length (filter (\<lambda>x. k = x) xs)) k" for k | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2480 | by simp | 
| 60678 | 2481 | then show "k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs" for k | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2482 | 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 | 2483 | qed | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2484 | |
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2485 | lemma sort_key_inj_key_eq: | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2486 | assumes mset_equal: "mset xs = mset ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2487 | and "inj_on f (set xs)" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2488 | and "sorted (map f ys)" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2489 | shows "sort_key f xs = ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2490 | proof (rule properties_for_sort_key) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2491 | from mset_equal | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2492 | show "mset ys = mset xs" by simp | 
| 61188 | 2493 | from \<open>sorted (map f ys)\<close> | 
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2494 | show "sorted (map f ys)" . | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2495 | show "[x\<leftarrow>ys . f k = f x] = [x\<leftarrow>xs . f k = f x]" if "k \<in> set ys" for k | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2496 | proof - | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2497 | from mset_equal | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2498 | have set_equal: "set xs = set ys" by (rule mset_eq_setD) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2499 | with that have "insert k (set ys) = set ys" by auto | 
| 61188 | 2500 | with \<open>inj_on f (set xs)\<close> have inj: "inj_on f (insert k (set ys))" | 
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2501 | by (simp add: set_equal) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2502 | from inj have "[x\<leftarrow>ys . f k = f x] = filter (HOL.eq k) ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2503 | by (auto intro!: inj_on_filter_key_eq) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2504 | also have "\<dots> = replicate (count (mset ys) k) k" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2505 | by (simp add: replicate_count_mset_eq_filter_eq) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2506 | also have "\<dots> = replicate (count (mset xs) k) k" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2507 | using mset_equal by simp | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2508 | also have "\<dots> = filter (HOL.eq k) xs" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2509 | by (simp add: replicate_count_mset_eq_filter_eq) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2510 | also have "\<dots> = [x\<leftarrow>xs . f k = f x]" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2511 | using inj by (auto intro!: inj_on_filter_key_eq [symmetric] simp add: set_equal) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2512 | finally show ?thesis . | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2513 | qed | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2514 | qed | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2515 | |
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2516 | lemma sort_key_eq_sort_key: | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2517 | assumes "mset xs = mset ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2518 | and "inj_on f (set xs)" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2519 | shows "sort_key f xs = sort_key f ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2520 | by (rule sort_key_inj_key_eq) (simp_all add: assms) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2521 | |
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2522 | lemma sort_key_by_quicksort: | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2523 | "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 | 2524 | @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))] | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2525 | @ 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 | 2526 | proof (rule properties_for_sort_key) | 
| 60515 | 2527 | show "mset ?rhs = mset ?lhs" | 
| 2528 | by (rule multiset_eqI) (auto simp add: mset_filter) | |
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2529 | show "sorted (map f ?rhs)" | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2530 | by (auto simp add: sorted_append intro: sorted_map_same) | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2531 | next | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2532 | fix l | 
| 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2533 | assume "l \<in> set ?rhs" | 
| 40346 | 2534 | let ?pivot = "f (xs ! (length xs div 2))" | 
| 2535 | have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto | |
| 40306 | 2536 | 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 | 2537 | unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same) | 
| 40346 | 2538 | with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp | 
| 2539 | 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 | |
| 2540 | then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] = | |
| 2541 | [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp | |
| 2542 | note *** = this [of "op <"] this [of "op >"] this [of "op ="] | |
| 40306 | 2543 | 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 | 2544 | proof (cases "f l" ?pivot rule: linorder_cases) | 
| 46730 | 2545 | case less | 
| 2546 | then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto | |
| 2547 | with less show ?thesis | |
| 40346 | 2548 | by (simp add: filter_sort [symmetric] ** ***) | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2549 | next | 
| 40306 | 2550 | case equal then show ?thesis | 
| 40346 | 2551 | by (simp add: * less_le) | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2552 | next | 
| 46730 | 2553 | case greater | 
| 2554 | then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto | |
| 2555 | with greater show ?thesis | |
| 40346 | 2556 | by (simp add: filter_sort [symmetric] ** ***) | 
| 40306 | 2557 | qed | 
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2558 | qed | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2559 | |
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2560 | lemma sort_by_quicksort: | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2561 | "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 | 2562 | @ [x\<leftarrow>xs. x = xs ! (length xs div 2)] | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2563 | @ 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 | 2564 | 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 | 2565 | |
| 60500 | 2566 | text \<open>A stable parametrized quicksort\<close> | 
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2567 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2568 | 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 | 2569 | "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 | 2570 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2571 | lemma part_code [code]: | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2572 | "part f pivot [] = ([], [], [])" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2573 | "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 | 2574 | if x' < pivot then (x # lts, eqs, gts) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2575 | else if x' > pivot then (lts, eqs, x # gts) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2576 | else (lts, x # eqs, gts))" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2577 | by (auto simp add: part_def Let_def split_def) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2578 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2579 | lemma sort_key_by_quicksort_code [code]: | 
| 60606 | 2580 | "sort_key f xs = | 
| 2581 | (case xs of | |
| 2582 | [] \<Rightarrow> [] | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2583 | | [x] \<Rightarrow> xs | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2584 | | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x]) | 
| 60606 | 2585 | | _ \<Rightarrow> | 
| 2586 | let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs | |
| 2587 | in sort_key f lts @ eqs @ sort_key f gts)" | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2588 | proof (cases xs) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2589 | case Nil then show ?thesis by simp | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2590 | next | 
| 46921 | 2591 | case (Cons _ ys) note hyps = Cons show ?thesis | 
| 2592 | proof (cases ys) | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2593 | case Nil with hyps show ?thesis by simp | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2594 | next | 
| 46921 | 2595 | case (Cons _ zs) note hyps = hyps Cons show ?thesis | 
| 2596 | proof (cases zs) | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2597 | case Nil with hyps show ?thesis by auto | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2598 | next | 
| 58425 | 2599 | case Cons | 
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2600 | from sort_key_by_quicksort [of f xs] | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2601 | 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 | 2602 | in sort_key f lts @ eqs @ sort_key f gts)" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2603 | 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 | 2604 | with hyps Cons show ?thesis by (simp only: list.cases) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2605 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2606 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2607 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2608 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2609 | end | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2610 | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2611 | hide_const (open) part | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2612 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 2613 | lemma mset_remdups_subset_eq: "mset (remdups xs) \<subseteq># mset xs" | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 2614 | by (induct xs) (auto intro: subset_mset.order_trans) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 2615 | |
| 60515 | 2616 | lemma mset_update: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2617 |   "i < length ls \<Longrightarrow> mset (ls[i := v]) = add_mset v (mset ls - {#ls ! i#})"
 | 
| 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 | 2618 | 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 | 2619 | 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 | 2620 | 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 | 2621 | 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 | 2622 | 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 | 2623 | 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 | 2624 | 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 | 2625 | 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 | 2626 | 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 | 2627 | with Cons show ?thesis | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2628 | by (cases \<open>x = xs ! i'\<close>) 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 | 2629 | 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 | 2630 | 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 | 2631 | |
| 60515 | 2632 | lemma mset_swap: | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 2633 | "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow> | 
| 60515 | 2634 | mset (ls[j := ls ! i, i := ls ! j]) = mset ls" | 
| 2635 | by (cases "i = j") (simp_all add: mset_update nth_mem_mset) | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 2636 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 2637 | |
| 60500 | 2638 | subsection \<open>The multiset order\<close> | 
| 2639 | ||
| 2640 | subsubsection \<open>Well-foundedness\<close> | |
| 10249 | 2641 | |
| 60606 | 2642 | definition mult1 :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2643 |   "mult1 r = {(N, M). \<exists>a M0 K. M = add_mset a M0 \<and> N = M0 + K \<and>
 | 
| 60607 | 2644 | (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r)}" | 
| 60606 | 2645 | |
| 2646 | definition mult :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
 | |
| 37765 | 2647 | "mult r = (mult1 r)\<^sup>+" | 
| 10249 | 2648 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2649 | lemma mult1I: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2650 | assumes "M = add_mset a M0" and "N = M0 + K" and "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2651 | shows "(N, M) \<in> mult1 r" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2652 | using assms unfolding mult1_def by blast | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2653 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2654 | lemma mult1E: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2655 | assumes "(N, M) \<in> mult1 r" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2656 | obtains a M0 K where "M = add_mset a M0" "N = M0 + K" "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2657 | using assms unfolding mult1_def by blast | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2658 | |
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2659 | lemma mono_mult1: | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2660 | assumes "r \<subseteq> r'" shows "mult1 r \<subseteq> mult1 r'" | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2661 | unfolding mult1_def using assms by blast | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2662 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2663 | lemma mono_mult: | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2664 | assumes "r \<subseteq> r'" shows "mult r \<subseteq> mult r'" | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2665 | unfolding mult_def using mono_mult1[OF assms] trancl_mono by blast | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2666 | |
| 23751 | 2667 | lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
 | 
| 26178 | 2668 | by (simp add: mult1_def) | 
| 10249 | 2669 | |
| 60608 | 2670 | lemma less_add: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2671 | assumes mult1: "(N, add_mset a M0) \<in> mult1 r" | 
| 60608 | 2672 | shows | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2673 | "(\<exists>M. (M, M0) \<in> mult1 r \<and> N = add_mset a M) \<or> | 
| 60608 | 2674 | (\<exists>K. (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r) \<and> N = M0 + K)" | 
| 2675 | proof - | |
| 60607 | 2676 | let ?r = "\<lambda>K a. \<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2677 | let ?R = "\<lambda>N M. \<exists>a M0 K. M = add_mset a M0 \<and> N = M0 + K \<and> ?r K a" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2678 | obtain a' M0' K where M0: "add_mset a M0 = add_mset a' M0'" | 
| 60608 | 2679 | and N: "N = M0' + K" | 
| 2680 | and r: "?r K a'" | |
| 2681 | using mult1 unfolding mult1_def by auto | |
| 2682 | show ?thesis (is "?case1 \<or> ?case2") | |
| 60606 | 2683 | proof - | 
| 2684 | from M0 consider "M0 = M0'" "a = a'" | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2685 | | K' where "M0 = add_mset a' K'" "M0' = add_mset a K'" | 
| 60606 | 2686 | by atomize_elim (simp only: add_eq_conv_ex) | 
| 18258 | 2687 | then show ?thesis | 
| 60606 | 2688 | proof cases | 
| 2689 | case 1 | |
| 11464 | 2690 | with N r have "?r K a \<and> N = M0 + K" by simp | 
| 60606 | 2691 | then have ?case2 .. | 
| 2692 | then show ?thesis .. | |
| 10249 | 2693 | next | 
| 60606 | 2694 | case 2 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2695 | from N 2(2) have n: "N = add_mset a (K' + K)" by simp | 
| 60606 | 2696 | with r 2(1) have "?R (K' + K) M0" by blast | 
| 60608 | 2697 | with n have ?case1 by (simp add: mult1_def) | 
| 60606 | 2698 | then show ?thesis .. | 
| 10249 | 2699 | qed | 
| 2700 | qed | |
| 2701 | qed | |
| 2702 | ||
| 60608 | 2703 | lemma all_accessible: | 
| 2704 | assumes "wf r" | |
| 2705 | shows "\<forall>M. M \<in> Wellfounded.acc (mult1 r)" | |
| 10249 | 2706 | proof | 
| 2707 | let ?R = "mult1 r" | |
| 54295 | 2708 | let ?W = "Wellfounded.acc ?R" | 
| 10249 | 2709 |   {
 | 
| 2710 | fix M M0 a | |
| 23751 | 2711 | assume M0: "M0 \<in> ?W" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2712 | and wf_hyp: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. add_mset b M \<in> ?W)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2713 | and acc_hyp: "\<forall>M. (M, M0) \<in> ?R \<longrightarrow> add_mset a M \<in> ?W" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2714 | have "add_mset a M0 \<in> ?W" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2715 | proof (rule accI [of "add_mset a M0"]) | 
| 10249 | 2716 | fix N | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2717 | assume "(N, add_mset a M0) \<in> ?R" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2718 | then consider M where "(M, M0) \<in> ?R" "N = add_mset a M" | 
| 60608 | 2719 | | K where "\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" "N = M0 + K" | 
| 2720 | by atomize_elim (rule less_add) | |
| 23751 | 2721 | then show "N \<in> ?W" | 
| 60608 | 2722 | proof cases | 
| 2723 | case 1 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2724 | from acc_hyp have "(M, M0) \<in> ?R \<longrightarrow> add_mset a M \<in> ?W" .. | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2725 | from this and \<open>(M, M0) \<in> ?R\<close> have "add_mset a M \<in> ?W" .. | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2726 | then show "N \<in> ?W" by (simp only: \<open>N = add_mset a M\<close>) | 
| 10249 | 2727 | next | 
| 60608 | 2728 | case 2 | 
| 2729 | from this(1) have "M0 + K \<in> ?W" | |
| 10249 | 2730 | proof (induct K) | 
| 18730 | 2731 | case empty | 
| 23751 | 2732 |           from M0 show "M0 + {#} \<in> ?W" by simp
 | 
| 18730 | 2733 | next | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2734 | case (add x K) | 
| 23751 | 2735 | from add.prems have "(x, a) \<in> r" by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2736 | with wf_hyp have "\<forall>M \<in> ?W. add_mset x M \<in> ?W" by blast | 
| 23751 | 2737 | moreover from add have "M0 + K \<in> ?W" by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2738 | ultimately have "add_mset x (M0 + K) \<in> ?W" .. | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2739 | then show "M0 + (add_mset x K) \<in> ?W" by simp | 
| 10249 | 2740 | qed | 
| 60608 | 2741 | then show "N \<in> ?W" by (simp only: 2(2)) | 
| 10249 | 2742 | qed | 
| 2743 | qed | |
| 2744 | } note tedious_reasoning = this | |
| 2745 | ||
| 60608 | 2746 | show "M \<in> ?W" for M | 
| 10249 | 2747 | proof (induct M) | 
| 23751 | 2748 |     show "{#} \<in> ?W"
 | 
| 10249 | 2749 | proof (rule accI) | 
| 23751 | 2750 |       fix b assume "(b, {#}) \<in> ?R"
 | 
| 2751 | with not_less_empty show "b \<in> ?W" by contradiction | |
| 10249 | 2752 | qed | 
| 2753 | ||
| 23751 | 2754 | fix M a assume "M \<in> ?W" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2755 | from \<open>wf r\<close> have "\<forall>M \<in> ?W. add_mset a M \<in> ?W" | 
| 10249 | 2756 | proof induct | 
| 2757 | fix a | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2758 | assume r: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. add_mset b M \<in> ?W)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2759 | show "\<forall>M \<in> ?W. add_mset a M \<in> ?W" | 
| 10249 | 2760 | proof | 
| 23751 | 2761 | fix M assume "M \<in> ?W" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2762 | then show "add_mset a M \<in> ?W" | 
| 23373 | 2763 | by (rule acc_induct) (rule tedious_reasoning [OF _ r]) | 
| 10249 | 2764 | qed | 
| 2765 | qed | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2766 | from this and \<open>M \<in> ?W\<close> show "add_mset a M \<in> ?W" .. | 
| 10249 | 2767 | qed | 
| 2768 | qed | |
| 2769 | ||
| 60606 | 2770 | theorem wf_mult1: "wf r \<Longrightarrow> wf (mult1 r)" | 
| 26178 | 2771 | by (rule acc_wfI) (rule all_accessible) | 
| 10249 | 2772 | |
| 60606 | 2773 | theorem wf_mult: "wf r \<Longrightarrow> wf (mult r)" | 
| 26178 | 2774 | unfolding mult_def by (rule wf_trancl) (rule wf_mult1) | 
| 10249 | 2775 | |
| 2776 | ||
| 60500 | 2777 | subsubsection \<open>Closure-free presentation\<close> | 
| 2778 | ||
| 2779 | text \<open>One direction.\<close> | |
| 10249 | 2780 | lemma mult_implies_one_step: | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2781 | assumes | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2782 | trans: "trans r" and | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2783 | MN: "(M, N) \<in> mult r" | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2784 |   shows "\<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and> (\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r)"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2785 | using MN unfolding mult_def mult1_def | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2786 | proof (induction rule: converse_trancl_induct) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2787 | case (base y) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2788 | then show ?case by force | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2789 | next | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2790 | case (step y z) note yz = this(1) and zN = this(2) and N_decomp = this(3) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2791 | obtain I J K where | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2792 |     N: "N = I + J" "z = I + K" "J \<noteq> {#}" "\<forall>k\<in>#K. \<exists>j\<in>#J. (k, j) \<in> r"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2793 | using N_decomp by blast | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2794 | obtain a M0 K' where | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2795 | z: "z = add_mset a M0" and y: "y = M0 + K'" and K: "\<forall>b. b \<in># K' \<longrightarrow> (b, a) \<in> r" | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2796 | using yz by blast | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2797 | show ?case | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2798 | proof (cases "a \<in># K") | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2799 | case True | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2800 | moreover have "\<exists>j\<in>#J. (k, j) \<in> r" if "k \<in># K'" for k | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2801 | using K N trans True by (meson that transE) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2802 | ultimately show ?thesis | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2803 |       by (rule_tac x = I in exI, rule_tac x = J in exI, rule_tac x = "(K - {#a#}) + K'" in exI)
 | 
| 64017 
6e7bf7678518
more multiset simp rules
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63924diff
changeset | 2804 | (use z y N in \<open>auto simp del: subset_mset.add_diff_assoc2 dest: in_diffD\<close>) | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2805 | next | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2806 | case False | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2807 | then have "a \<in># I" by (metis N(2) union_iff union_single_eq_member z) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2808 |     moreover have "M0 = I + K - {#a#}"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2809 | using N(2) z by force | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2810 | ultimately show ?thesis | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2811 |       by (rule_tac x = "I - {#a#}" in exI, rule_tac x = "add_mset a J" in exI,
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2812 | rule_tac x = "K + K'" in exI) | 
| 64017 
6e7bf7678518
more multiset simp rules
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63924diff
changeset | 2813 | (use z y N False K in \<open>auto simp: add.assoc\<close>) | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2814 | qed | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2815 | qed | 
| 10249 | 2816 | |
| 17161 | 2817 | lemma one_step_implies_mult: | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2818 | assumes | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2819 |     "J \<noteq> {#}" and
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2820 | "\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r" | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2821 | shows "(I + K, I + J) \<in> mult r" | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2822 | using assms | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2823 | proof (induction "size J" arbitrary: I J K) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2824 | case 0 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2825 | then show ?case by auto | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2826 | next | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2827 | case (Suc n) note IH = this(1) and size_J = this(2)[THEN sym] | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2828 | obtain J' a where J: "J = add_mset a J'" | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2829 | using size_J by (blast dest: size_eq_Suc_imp_eq_union) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2830 | show ?case | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2831 |   proof (cases "J' = {#}")
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2832 | case True | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2833 | then show ?thesis | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2834 | using J Suc by (fastforce simp add: mult_def mult1_def) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2835 | next | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2836 | case [simp]: False | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2837 |     have K: "K = {#x \<in># K. (x, a) \<in> r#} + {#x \<in># K. (x, a) \<notin> r#}"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2838 | by (rule multiset_partition) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2839 |     have "(I + K, (I + {# x \<in># K. (x, a) \<in> r #}) + J') \<in> mult r"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2840 |       using IH[of J' "{# x \<in># K. (x, a) \<notin> r#}" "I + {# x \<in># K. (x, a) \<in> r#}"]
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2841 | J Suc.prems K size_J by (auto simp: ac_simps) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2842 |     moreover have "(I + {#x \<in># K. (x, a) \<in> r#} + J', I + J) \<in> mult r"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2843 | by (fastforce simp: J mult1_def mult_def) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2844 | ultimately show ?thesis | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2845 | unfolding mult_def by simp | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2846 | qed | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2847 | qed | 
| 10249 | 2848 | |
| 65047 | 2849 | lemma subset_implies_mult: | 
| 2850 | assumes sub: "A \<subset># B" | |
| 2851 | shows "(A, B) \<in> mult r" | |
| 2852 | proof - | |
| 2853 | have ApBmA: "A + (B - A) = B" | |
| 2854 | using sub by simp | |
| 2855 |   have BmA: "B - A \<noteq> {#}"
 | |
| 2856 | using sub by (simp add: Diff_eq_empty_iff_mset subset_mset.less_le_not_le) | |
| 2857 | thus ?thesis | |
| 2858 |     by (rule one_step_implies_mult[of "B - A" "{#}" _ A, unfolded ApBmA, simplified])
 | |
| 2859 | qed | |
| 2860 | ||
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2861 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2862 | subsection \<open>The multiset extension is cancellative for multiset union\<close> | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2863 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2864 | lemma mult_cancel: | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2865 | assumes "trans s" and "irrefl s" | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2866 | shows "(X + Z, Y + Z) \<in> mult s \<longleftrightarrow> (X, Y) \<in> mult s" (is "?L \<longleftrightarrow> ?R") | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2867 | proof | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2868 | assume ?L thus ?R | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2869 | proof (induct Z) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2870 | case (add z Z) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2871 |     obtain X' Y' Z' where *: "add_mset z X + Z = Z' + X'" "add_mset z Y + Z = Z' + Y'" "Y' \<noteq> {#}"
 | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2872 | "\<forall>x \<in> set_mset X'. \<exists>y \<in> set_mset Y'. (x, y) \<in> s" | 
| 64911 | 2873 | using mult_implies_one_step[OF \<open>trans s\<close> add(2)] by auto | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2874 | consider Z2 where "Z' = add_mset z Z2" | X2 Y2 where "X' = add_mset z X2" "Y' = add_mset z Y2" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2875 | using *(1,2) by (metis add_mset_remove_trivial_If insert_iff set_mset_add_mset_insert union_iff) | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2876 | thus ?case | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2877 | proof (cases) | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2878 | case 1 thus ?thesis using * one_step_implies_mult[of Y' X' s Z2] | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2879 |         by (auto simp: add.commute[of _ "{#_#}"] add.assoc intro: add(1))
 | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2880 | next | 
| 64911 | 2881 | case 2 then obtain y where "y \<in> set_mset Y2" "(z, y) \<in> s" using *(4) \<open>irrefl s\<close> | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2882 | by (auto simp: irrefl_def) | 
| 64911 | 2883 | moreover from this transD[OF \<open>trans s\<close> _ this(2)] | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2884 | have "x' \<in> set_mset X2 \<Longrightarrow> \<exists>y \<in> set_mset Y2. (x', y) \<in> s" for x' | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2885 | using 2 *(4)[rule_format, of x'] by auto | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2886 | ultimately show ?thesis using * one_step_implies_mult[of Y2 X2 s Z'] 2 | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2887 |         by (force simp: add.commute[of "{#_#}"] add.assoc[symmetric] intro: add(1))
 | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2888 | qed | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2889 | qed auto | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2890 | next | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2891 | assume ?R then obtain I J K | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2892 |     where "Y = I + J" "X = I + K" "J \<noteq> {#}" "\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> s"
 | 
| 64911 | 2893 | using mult_implies_one_step[OF \<open>trans s\<close>] by blast | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2894 | thus ?L using one_step_implies_mult[of J K s "I + Z"] by (auto simp: ac_simps) | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2895 | qed | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2896 | |
| 64531 
166a2563e0ca
added lemma about 'mult', as suggested by Bertram Felgenhauer
 blanchet parents: 
64418diff
changeset | 2897 | lemmas mult_cancel_add_mset = | 
| 
166a2563e0ca
added lemma about 'mult', as suggested by Bertram Felgenhauer
 blanchet parents: 
64418diff
changeset | 2898 |   mult_cancel[of _ _ "{#_#}", unfolded union_mset_add_mset_right add.comm_neutral]
 | 
| 
166a2563e0ca
added lemma about 'mult', as suggested by Bertram Felgenhauer
 blanchet parents: 
64418diff
changeset | 2899 | |
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2900 | lemma mult_cancel_max: | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2901 | assumes "trans s" and "irrefl s" | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2902 | shows "(X, Y) \<in> mult s \<longleftrightarrow> (X - X \<inter># Y, Y - X \<inter># Y) \<in> mult s" (is "?L \<longleftrightarrow> ?R") | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2903 | proof - | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2904 | have "X - X \<inter># Y + X \<inter># Y = X" "Y - X \<inter># Y + X \<inter># Y = Y" by (auto simp: count_inject[symmetric]) | 
| 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2905 | thus ?thesis using mult_cancel[OF assms, of "X - X \<inter># Y" "X \<inter># Y" "Y - X \<inter># Y"] by auto | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2906 | qed | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2907 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2908 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2909 | subsection \<open>Quasi-executable version of the multiset extension\<close> | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2910 | |
| 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2911 | text \<open> | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2912 | Predicate variants of \<open>mult\<close> and the reflexive closure of \<open>mult\<close>, which are | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2913 | executable whenever the given predicate \<open>P\<close> is. Together with the standard | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2914 | code equations for \<open>op \<inter>#\<close> and \<open>op -\<close> this should yield quadratic | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2915 | (with respect to calls to \<open>P\<close>) implementations of \<open>multp\<close> and \<open>multeqp\<close>. | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2916 | \<close> | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2917 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2918 | definition multp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
 | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2919 | "multp P N M = | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2920 | (let Z = M \<inter># N; X = M - Z in | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2921 |     X \<noteq> {#} \<and> (let Y = N - Z in (\<forall>y \<in> set_mset Y. \<exists>x \<in> set_mset X. P y x)))"
 | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2922 | |
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2923 | definition multeqp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
 | 
| 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2924 | "multeqp P N M = | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2925 | (let Z = M \<inter># N; X = M - Z; Y = N - Z in | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2926 | (\<forall>y \<in> set_mset Y. \<exists>x \<in> set_mset X. P y x))" | 
| 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2927 | |
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2928 | lemma multp_iff: | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2929 | assumes "irrefl R" and "trans R" and [simp]: "\<And>x y. P x y \<longleftrightarrow> (x, y) \<in> R" | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2930 | shows "multp P N M \<longleftrightarrow> (N, M) \<in> mult R" (is "?L \<longleftrightarrow> ?R") | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2931 | proof - | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2932 | have *: "M \<inter># N + (N - M \<inter># N) = N" "M \<inter># N + (M - M \<inter># N) = M" | 
| 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2933 |     "(M - M \<inter># N) \<inter># (N - M \<inter># N) = {#}" by (auto simp: count_inject[symmetric])
 | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2934 | show ?thesis | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2935 | proof | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2936 | assume ?L thus ?R | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2937 | using one_step_implies_mult[of "M - M \<inter># N" "N - M \<inter># N" R "M \<inter># N"] * | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2938 | by (auto simp: multp_def Let_def) | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2939 | next | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2940 |     { fix I J K :: "'a multiset" assume "(I + J) \<inter># (I + K) = {#}"
 | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2941 |       then have "I = {#}" by (metis inter_union_distrib_right union_eq_empty)
 | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2942 | } note [dest!] = this | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2943 | assume ?R thus ?L | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2944 | using mult_implies_one_step[OF assms(2), of "N - M \<inter># N" "M - M \<inter># N"] | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2945 | mult_cancel_max[OF assms(2,1), of "N" "M"] * by (auto simp: multp_def) | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2946 | qed | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2947 | qed | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2948 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2949 | lemma multeqp_iff: | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2950 | assumes "irrefl R" and "trans R" and "\<And>x y. P x y \<longleftrightarrow> (x, y) \<in> R" | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2951 | shows "multeqp P N M \<longleftrightarrow> (N, M) \<in> (mult R)\<^sup>=" | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2952 | proof - | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 2953 |   { assume "N \<noteq> M" "M - M \<inter># N = {#}"
 | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2954 | then obtain y where "count N y \<noteq> count M y" by (auto simp: count_inject[symmetric]) | 
| 64911 | 2955 |     then have "\<exists>y. count M y < count N y" using \<open>M - M \<inter># N = {#}\<close>
 | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2956 | by (auto simp: count_inject[symmetric] dest!: le_neq_implies_less fun_cong[of _ _ y]) | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2957 | } | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2958 | then have "multeqp P N M \<longleftrightarrow> multp P N M \<or> N = M" | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2959 | by (auto simp: multeqp_def multp_def Let_def in_diff_count) | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2960 | thus ?thesis using multp_iff[OF assms] by simp | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2961 | qed | 
| 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 2962 | |
| 10249 | 2963 | |
| 60500 | 2964 | subsubsection \<open>Partial-order properties\<close> | 
| 10249 | 2965 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 2966 | lemma (in preorder) mult1_lessE: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2967 |   assumes "(N, M) \<in> mult1 {(a, b). a < b}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2968 | obtains a M0 K where "M = add_mset a M0" "N = M0 + K" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2969 | "a \<notin># K" "\<And>b. b \<in># K \<Longrightarrow> b < a" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2970 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2971 | from assms obtain a M0 K where "M = add_mset a M0" "N = M0 + K" and | 
| 63539 | 2972 | *: "b \<in># K \<Longrightarrow> b < a" for b by (blast elim: mult1E) | 
| 2973 | moreover from * [of a] have "a \<notin># K" by auto | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2974 | ultimately show thesis by (auto intro: that) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2975 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2976 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 2977 | instantiation multiset :: (preorder) order | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2978 | begin | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2979 | |
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2980 | definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2981 |   where "M' < M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
 | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2982 | |
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2983 | definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2984 | where "less_eq_multiset M' M \<longleftrightarrow> M' < M \<or> M' = M" | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2985 | |
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2986 | instance | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 2987 | proof - | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2988 | have irrefl: "\<not> M < M" for M :: "'a multiset" | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 2989 | proof | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 2990 | assume "M < M" | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 2991 |     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 | 2992 |     have "trans {(x'::'a, x). x' < x}"
 | 
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 2993 | by (metis (mono_tags, lifting) case_prodD case_prodI less_trans mem_Collect_eq transI) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 2994 | moreover note MM | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 2995 | ultimately have "\<exists>I J K. M = I + J \<and> M = I + K | 
| 60495 | 2996 |       \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})"
 | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 2997 | by (rule mult_implies_one_step) | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 2998 | then obtain I J K where "M = I + J" and "M = I + K" | 
| 60495 | 2999 |       and "J \<noteq> {#}" and "(\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})" by blast
 | 
| 60678 | 3000 |     then have *: "K \<noteq> {#}" and **: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. k < j" by auto
 | 
| 60495 | 3001 | have "finite (set_mset K)" by simp | 
| 60678 | 3002 | moreover note ** | 
| 60495 | 3003 |     ultimately have "set_mset K = {}"
 | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3004 | by (induct rule: finite_induct) (auto intro: order_less_trans) | 
| 60678 | 3005 | with * show False by simp | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3006 | qed | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3007 | have trans: "K < M \<Longrightarrow> M < N \<Longrightarrow> K < N" for K M N :: "'a multiset" | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3008 | unfolding less_multiset_def mult_def by (blast intro: trancl_trans) | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3009 |   show "OFCLASS('a multiset, order_class)"
 | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3010 | by standard (auto simp add: less_eq_multiset_def irrefl dest: trans) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3011 | qed | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3012 | end \<comment> \<open>FIXME avoid junk stemming from type class interpretation\<close> | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3013 | |
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3014 | lemma mset_le_irrefl [elim!]: | 
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3015 | fixes M :: "'a::preorder multiset" | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3016 | shows "M < M \<Longrightarrow> R" | 
| 46730 | 3017 | by simp | 
| 26567 
7bcebb8c2d33
instantiation replacing primitive instance plus overloaded defs; more conservative type arities
 haftmann parents: 
26178diff
changeset | 3018 | |
| 10249 | 3019 | |
| 60500 | 3020 | subsubsection \<open>Monotonicity of multiset union\<close> | 
| 10249 | 3021 | |
| 60606 | 3022 | lemma mult1_union: "(B, D) \<in> mult1 r \<Longrightarrow> (C + B, C + D) \<in> mult1 r" | 
| 64076 | 3023 | by (force simp: mult1_def) | 
| 10249 | 3024 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3025 | lemma union_le_mono2: "B < D \<Longrightarrow> C + B < C + (D::'a::preorder multiset)" | 
| 26178 | 3026 | apply (unfold less_multiset_def mult_def) | 
| 3027 | apply (erule trancl_induct) | |
| 40249 
cd404ecb9107
Remove unnecessary premise of mult1_union
 Lars Noschinski <noschinl@in.tum.de> parents: 
39533diff
changeset | 3028 | apply (blast intro: mult1_union) | 
| 
cd404ecb9107
Remove unnecessary premise of mult1_union
 Lars Noschinski <noschinl@in.tum.de> parents: 
39533diff
changeset | 3029 | apply (blast intro: mult1_union trancl_trans) | 
| 26178 | 3030 | done | 
| 10249 | 3031 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3032 | lemma union_le_mono1: "B < D \<Longrightarrow> B + C < D + (C::'a::preorder multiset)" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57492diff
changeset | 3033 | apply (subst add.commute [of B C]) | 
| 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57492diff
changeset | 3034 | apply (subst add.commute [of D C]) | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3035 | apply (erule union_le_mono2) | 
| 26178 | 3036 | done | 
| 10249 | 3037 | |
| 17161 | 3038 | lemma union_less_mono: | 
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3039 | fixes A B C D :: "'a::preorder multiset" | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3040 | shows "A < C \<Longrightarrow> B < D \<Longrightarrow> A + B < C + D" | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3041 | by (blast intro!: union_le_mono1 union_le_mono2 less_trans) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3042 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3043 | instantiation multiset :: (preorder) ordered_ab_semigroup_add | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3044 | begin | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3045 | instance | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3046 | by standard (auto simp add: less_eq_multiset_def intro: union_le_mono2) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3047 | end | 
| 15072 | 3048 | |
| 63409 
3f3223b90239
moved lemmas and locales around (with minor incompatibilities)
 blanchet parents: 
63388diff
changeset | 3049 | |
| 60500 | 3050 | subsubsection \<open>Termination proofs with multiset orders\<close> | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3051 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3052 | 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 | 3053 |   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 | 3054 |   and multi_member_last: "x \<in># {# x #}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3055 | by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3056 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3057 | definition "ms_strict = mult pair_less" | 
| 37765 | 3058 | 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 | 3059 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3060 | 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 | 3061 | 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 | 3062 | 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 | 3063 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3064 | lemma smsI: | 
| 60495 | 3065 | "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict" | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3066 | unfolding ms_strict_def | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3067 | 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 | 3068 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3069 | lemma wmsI: | 
| 60495 | 3070 |   "(set_mset A, set_mset B) \<in> max_strict \<or> A = {#} \<and> B = {#}
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3071 | \<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 | 3072 | unfolding ms_weak_def ms_strict_def | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3073 | 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 | 3074 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3075 | inductive pw_leq | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3076 | where | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3077 |   pw_leq_empty: "pw_leq {#} {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3078 | | 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 | 3079 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3080 | lemma pw_leq_lstep: | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3081 |   "(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 | 3082 | 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 | 3083 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3084 | lemma pw_leq_split: | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3085 | assumes "pw_leq X Y" | 
| 60495 | 3086 |   shows "\<exists>A B Z. X = A + Z \<and> Y = B + Z \<and> ((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3087 | using assms | 
| 60606 | 3088 | proof induct | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3089 | 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 | 3090 | next | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3091 | 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 | 3092 | then obtain A B Z where | 
| 58425 | 3093 | [simp]: "X = A + Z" "Y = B + Z" | 
| 60495 | 3094 |       and 1[simp]: "(set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#})"
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3095 | by auto | 
| 60606 | 3096 | from pw_leq_step consider "x = y" | "(x, y) \<in> pair_less" | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3097 | unfolding pair_leq_def by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3098 | thus ?case | 
| 60606 | 3099 | proof cases | 
| 3100 | case [simp]: 1 | |
| 3101 |     have "{#x#} + X = A + ({#y#}+Z) \<and> {#y#} + Y = B + ({#y#}+Z) \<and>
 | |
| 3102 |       ((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
 | |
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3103 | by auto | 
| 60606 | 3104 | thus ?thesis by blast | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3105 | next | 
| 60606 | 3106 | case 2 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3107 |     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 | 3108 |     have "{#x#} + X = ?A' + Z"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3109 |       "{#y#} + Y = ?B' + Z"
 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3110 | by auto | 
| 58425 | 3111 | moreover have | 
| 60495 | 3112 | "(set_mset ?A', set_mset ?B') \<in> max_strict" | 
| 60606 | 3113 | using 1 2 unfolding max_strict_def | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3114 | by (auto elim!: max_ext.cases) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3115 | ultimately show ?thesis by blast | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3116 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3117 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3118 | |
| 58425 | 3119 | lemma | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3120 | assumes pwleq: "pw_leq Z Z'" | 
| 60495 | 3121 | shows ms_strictI: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict" | 
| 60606 | 3122 | and ms_weakI1: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak" | 
| 3123 |     and ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3124 | proof - | 
| 58425 | 3125 | from pw_leq_split[OF pwleq] | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3126 | obtain A' B' Z'' | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3127 | where [simp]: "Z = A' + Z''" "Z' = B' + Z''" | 
| 60495 | 3128 |     and mx_or_empty: "(set_mset A', set_mset B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3129 | by blast | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3130 |   {
 | 
| 60495 | 3131 | assume max: "(set_mset A, set_mset B) \<in> max_strict" | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3132 | from mx_or_empty | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3133 | 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 | 3134 | proof | 
| 60495 | 3135 | assume max': "(set_mset A', set_mset B') \<in> max_strict" | 
| 3136 | with max have "(set_mset (A + A'), set_mset (B + B')) \<in> max_strict" | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3137 | by (auto simp: max_strict_def intro: max_ext_additive) | 
| 58425 | 3138 | thus ?thesis by (rule smsI) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3139 | next | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3140 |       assume [simp]: "A' = {#} \<and> B' = {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3141 | 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 | 3142 | qed | 
| 60606 | 3143 | thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add: ac_simps) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3144 | 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 | 3145 | } | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3146 | from mx_or_empty | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3147 | have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI) | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3148 |   thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add: ac_simps)
 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3149 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3150 | |
| 39301 | 3151 | 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 | 3152 | and nonempty_plus: "{# x #} + rs \<noteq> {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3153 | and nonempty_single: "{# x #} \<noteq> {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3154 | by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3155 | |
| 60500 | 3156 | setup \<open> | 
| 60606 | 3157 | let | 
| 3158 |     fun msetT T = Type (@{type_name multiset}, [T]);
 | |
| 3159 | ||
| 3160 |     fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3161 | | mk_mset T [x] = | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3162 |         Const (@{const_name add_mset}, T --> msetT T --> msetT T) $ x $
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3163 |           Const (@{const_abbrev Mempty}, msetT T)
 | 
| 60606 | 3164 | | mk_mset T (x :: xs) = | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3165 |         Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3166 | mk_mset T [x] $ mk_mset T xs | 
| 60606 | 3167 | |
| 60752 | 3168 | fun mset_member_tac ctxt m i = | 
| 60606 | 3169 | if m <= 0 then | 
| 60752 | 3170 |         resolve_tac ctxt @{thms multi_member_this} i ORELSE
 | 
| 3171 |         resolve_tac ctxt @{thms multi_member_last} i
 | |
| 60606 | 3172 | else | 
| 60752 | 3173 |         resolve_tac ctxt @{thms multi_member_skip} i THEN mset_member_tac ctxt (m - 1) i
 | 
| 3174 | ||
| 3175 | fun mset_nonempty_tac ctxt = | |
| 3176 |       resolve_tac ctxt @{thms nonempty_plus} ORELSE'
 | |
| 3177 |       resolve_tac ctxt @{thms nonempty_single}
 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3178 | |
| 60606 | 3179 | fun regroup_munion_conv ctxt = | 
| 3180 |       Function_Lib.regroup_conv ctxt @{const_abbrev Mempty} @{const_name plus}
 | |
| 3181 |         (map (fn t => t RS eq_reflection) (@{thms ac_simps} @ @{thms empty_neutral}))
 | |
| 3182 | ||
| 60752 | 3183 | fun unfold_pwleq_tac ctxt i = | 
| 3184 |       (resolve_tac ctxt @{thms pw_leq_step} i THEN (fn st => unfold_pwleq_tac ctxt (i + 1) st))
 | |
| 3185 |         ORELSE (resolve_tac ctxt @{thms pw_leq_lstep} i)
 | |
| 3186 |         ORELSE (resolve_tac ctxt @{thms pw_leq_empty} i)
 | |
| 60606 | 3187 | |
| 3188 |     val set_mset_simps = [@{thm set_mset_empty}, @{thm set_mset_single}, @{thm set_mset_union},
 | |
| 3189 |                         @{thm Un_insert_left}, @{thm Un_empty_left}]
 | |
| 3190 | in | |
| 3191 | ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset | |
| 3192 |     {
 | |
| 3193 | msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv, | |
| 3194 | mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac, | |
| 3195 | mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_mset_simps, | |
| 3196 |       smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
 | |
| 60752 | 3197 |       reduction_pair = @{thm ms_reduction_pair}
 | 
| 60606 | 3198 | }) | 
| 3199 | end | |
| 60500 | 3200 | \<close> | 
| 3201 | ||
| 3202 | ||
| 3203 | subsection \<open>Legacy theorem bindings\<close> | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3204 | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 3205 | 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 | 3206 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3207 | lemma union_commute: "M + N = N + (M::'a multiset)" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57492diff
changeset | 3208 | by (fact add.commute) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3209 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3210 | lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57492diff
changeset | 3211 | by (fact add.assoc) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3212 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3213 | lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))" | 
| 57512 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57492diff
changeset | 3214 | by (fact add.left_commute) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3215 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3216 | lemmas union_ac = union_assoc union_commute union_lcomm add_mset_commute | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3217 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3218 | 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 | 3219 | 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 | 3220 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3221 | 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 | 3222 | 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 | 3223 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3224 | lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y" | 
| 59557 | 3225 | by (fact add_left_imp_eq) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3226 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3227 | lemma mset_subset_trans: "(M::'a multiset) \<subset># K \<Longrightarrow> K \<subset># N \<Longrightarrow> M \<subset># N" | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 3228 | by (fact subset_mset.less_trans) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3229 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3230 | lemma multiset_inter_commute: "A \<inter># B = B \<inter># A" | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 3231 | by (fact subset_mset.inf.commute) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3232 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3233 | lemma multiset_inter_assoc: "A \<inter># (B \<inter># C) = A \<inter># B \<inter># C" | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 3234 | by (fact subset_mset.inf.assoc [symmetric]) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3235 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3236 | lemma multiset_inter_left_commute: "A \<inter># (B \<inter># C) = B \<inter># (A \<inter># C)" | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 3237 | by (fact subset_mset.inf.left_commute) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3238 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3239 | lemmas multiset_inter_ac = | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3240 | multiset_inter_commute | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3241 | multiset_inter_assoc | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3242 | multiset_inter_left_commute | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3243 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3244 | lemma mset_le_not_refl: "\<not> M < (M::'a::preorder multiset)" | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3245 | by (fact less_irrefl) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3246 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3247 | lemma mset_le_trans: "K < M \<Longrightarrow> M < N \<Longrightarrow> K < (N::'a::preorder multiset)" | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3248 | by (fact less_trans) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3249 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3250 | lemma mset_le_not_sym: "M < N \<Longrightarrow> \<not> N < (M::'a::preorder multiset)" | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3251 | by (fact less_not_sym) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3252 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3253 | lemma mset_le_asym: "M < N \<Longrightarrow> (\<not> P \<Longrightarrow> N < (M::'a::preorder multiset)) \<Longrightarrow> P" | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3254 | by (fact 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 | 3255 | |
| 60500 | 3256 | declaration \<open> | 
| 60606 | 3257 | let | 
| 3258 | fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T])) (Const _ $ t') = | |
| 3259 | let | |
| 3260 | val (maybe_opt, ps) = | |
| 3261 | Nitpick_Model.dest_plain_fun t' | |
| 3262 | ||> op ~~ | |
| 3263 | ||> map (apsnd (snd o HOLogic.dest_number)) | |
| 3264 | fun elems_for t = | |
| 3265 | (case AList.lookup (op =) ps t of | |
| 3266 | SOME n => replicate n t | |
| 3267 | | NONE => [Const (maybe_name, elem_T --> elem_T) $ t]) | |
| 3268 | in | |
| 3269 | (case maps elems_for (all_values elem_T) @ | |
| 61333 | 3270 | (if maybe_opt then [Const (Nitpick_Model.unrep_mixfix (), elem_T)] else []) of | 
| 60606 | 3271 |               [] => Const (@{const_name zero_class.zero}, T)
 | 
| 3272 | | ts => | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3273 |                 foldl1 (fn (s, t) => Const (@{const_name add_mset}, elem_T --> T --> T) $ s $ t)
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3274 | ts) | 
| 60606 | 3275 | end | 
| 3276 | | multiset_postproc _ _ _ _ t = t | |
| 3277 |   in Nitpick_Model.register_term_postprocessor @{typ "'a multiset"} multiset_postproc end
 | |
| 60500 | 3278 | \<close> | 
| 3279 | ||
| 3280 | ||
| 3281 | subsection \<open>Naive implementation using lists\<close> | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3282 | |
| 60515 | 3283 | code_datatype mset | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3284 | |
| 60606 | 3285 | lemma [code]: "{#} = mset []"
 | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3286 | by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3287 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3288 | lemma [code]: "add_mset x (mset xs) = mset (x # xs)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3289 | by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3290 | |
| 63195 | 3291 | lemma [code]: "Multiset.is_empty (mset xs) \<longleftrightarrow> List.null xs" | 
| 3292 | by (simp add: Multiset.is_empty_def List.null_def) | |
| 3293 | ||
| 60606 | 3294 | lemma union_code [code]: "mset xs + mset ys = mset (xs @ ys)" | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3295 | by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3296 | |
| 60606 | 3297 | lemma [code]: "image_mset f (mset xs) = mset (map f xs)" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3298 | by simp | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3299 | |
| 60606 | 3300 | lemma [code]: "filter_mset f (mset xs) = mset (filter f xs)" | 
| 60515 | 3301 | by (simp add: mset_filter) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3302 | |
| 60606 | 3303 | lemma [code]: "mset xs - mset ys = mset (fold remove1 ys xs)" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3304 | by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute diff_diff_add) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3305 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3306 | lemma [code]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3307 | "mset xs \<inter># mset ys = | 
| 60515 | 3308 | mset (snd (fold (\<lambda>x (ys, zs). | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3309 | 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 | 3310 | proof - | 
| 60515 | 3311 | have "\<And>zs. mset (snd (fold (\<lambda>x (ys, zs). | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3312 | if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) = | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3313 | (mset xs \<inter># mset ys) + mset zs" | 
| 51623 | 3314 | by (induct xs arbitrary: ys) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 3315 | (auto simp add: inter_add_right1 inter_add_right2 ac_simps) | 
| 51623 | 3316 | then show ?thesis by simp | 
| 3317 | qed | |
| 3318 | ||
| 3319 | lemma [code]: | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3320 | "mset xs \<union># mset ys = | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61378diff
changeset | 3321 | mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))" | 
| 51623 | 3322 | proof - | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61378diff
changeset | 3323 | have "\<And>zs. mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) = | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3324 | (mset xs \<union># mset ys) + mset zs" | 
| 51623 | 3325 | 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 | 3326 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3327 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3328 | |
| 59813 | 3329 | declare in_multiset_in_set [code_unfold] | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3330 | |
| 60606 | 3331 | lemma [code]: "count (mset xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0" | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3332 | proof - | 
| 60515 | 3333 | have "\<And>n. fold (\<lambda>y. if x = y then Suc else id) xs n = count (mset xs) x + n" | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3334 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3335 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3336 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3337 | |
| 60515 | 3338 | declare set_mset_mset [code] | 
| 3339 | ||
| 3340 | declare sorted_list_of_multiset_mset [code] | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3341 | |
| 61585 | 3342 | lemma [code]: \<comment> \<open>not very efficient, but representation-ignorant!\<close> | 
| 60515 | 3343 | "mset_set A = mset (sorted_list_of_set A)" | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3344 | apply (cases "finite A") | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3345 | apply simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3346 | apply (induct A rule: finite_induct) | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3347 | apply simp_all | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3348 | done | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3349 | |
| 60515 | 3350 | declare size_mset [code] | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3351 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3352 | fun subset_eq_mset_impl :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool option" where | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3353 | "subset_eq_mset_impl [] ys = Some (ys \<noteq> [])" | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3354 | | "subset_eq_mset_impl (Cons x xs) ys = (case List.extract (op = x) ys of | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3355 | None \<Rightarrow> None | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3356 | | Some (ys1,_,ys2) \<Rightarrow> subset_eq_mset_impl xs (ys1 @ ys2))" | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3357 | |
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3358 | lemma subset_eq_mset_impl: "(subset_eq_mset_impl xs ys = None \<longleftrightarrow> \<not> mset xs \<subseteq># mset ys) \<and> | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3359 | (subset_eq_mset_impl xs ys = Some True \<longleftrightarrow> mset xs \<subset># mset ys) \<and> | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3360 | (subset_eq_mset_impl xs ys = Some False \<longrightarrow> mset xs = mset ys)" | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3361 | proof (induct xs arbitrary: ys) | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3362 | case (Nil ys) | 
| 64076 | 3363 | show ?case by (auto simp: subset_mset.zero_less_iff_neq_zero) | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3364 | next | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3365 | case (Cons x xs ys) | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3366 | show ?case | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3367 | proof (cases "List.extract (op = x) ys") | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3368 | case None | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3369 | hence x: "x \<notin> set ys" by (simp add: extract_None_iff) | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3370 |     {
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 3371 | assume "mset (x # xs) \<subseteq># mset ys" | 
| 60495 | 3372 | from set_mset_mono[OF this] x have False by simp | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3373 | } note nle = this | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3374 | moreover | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3375 |     {
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 3376 | assume "mset (x # xs) \<subset># mset ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 3377 | hence "mset (x # xs) \<subseteq># mset ys" by auto | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3378 | from nle[OF this] have False . | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3379 | } | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3380 | ultimately show ?thesis using None by auto | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3381 | next | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3382 | case (Some res) | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3383 | obtain ys1 y ys2 where res: "res = (ys1,y,ys2)" by (cases res, auto) | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3384 | note Some = Some[unfolded res] | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3385 | from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3386 | hence id: "mset ys = add_mset x (mset (ys1 @ ys2))" | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3387 | by auto | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3388 | show ?thesis unfolding subset_eq_mset_impl.simps | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3389 | unfolding Some option.simps split | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3390 | unfolding id | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3391 | using Cons[of "ys1 @ ys2"] | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 3392 | unfolding subset_mset_def subseteq_mset_def by auto | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3393 | qed | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3394 | qed | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3395 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3396 | lemma [code]: "mset xs \<subseteq># mset ys \<longleftrightarrow> subset_eq_mset_impl xs ys \<noteq> None" | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3397 | using subset_eq_mset_impl[of xs ys] by (cases "subset_eq_mset_impl xs ys", auto) | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3398 | |
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3399 | lemma [code]: "mset xs \<subset># mset ys \<longleftrightarrow> subset_eq_mset_impl xs ys = Some True" | 
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3400 | using subset_eq_mset_impl[of xs ys] by (cases "subset_eq_mset_impl xs ys", auto) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3401 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3402 | instantiation multiset :: (equal) equal | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3403 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3404 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3405 | definition | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3406 | [code del]: "HOL.equal A (B :: 'a multiset) \<longleftrightarrow> A = B" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3407 | lemma [code]: "HOL.equal (mset xs) (mset ys) \<longleftrightarrow> subset_eq_mset_impl xs ys = Some False" | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3408 | unfolding equal_multiset_def | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3409 | using subset_eq_mset_impl[of xs ys] by (cases "subset_eq_mset_impl xs ys", auto) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3410 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3411 | instance | 
| 60678 | 3412 | by standard (simp add: equal_multiset_def) | 
| 3413 | ||
| 37169 
f69efa106feb
make Nitpick "show_all" option behave less surprisingly
 blanchet parents: 
37107diff
changeset | 3414 | end | 
| 49388 | 3415 | |
| 63882 
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
 nipkow parents: 
63860diff
changeset | 3416 | lemma [code]: "sum_mset (mset xs) = sum_list xs" | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3417 | by (induct xs) simp_all | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3418 | |
| 63830 | 3419 | lemma [code]: "prod_mset (mset xs) = fold times xs 1" | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3420 | proof - | 
| 63830 | 3421 | have "\<And>x. fold times xs x = prod_mset (mset xs) * x" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3422 | by (induct xs) (simp_all add: ac_simps) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3423 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3424 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3425 | |
| 60500 | 3426 | text \<open> | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3427 |   Exercise for the casual reader: add implementations for @{term "op \<le>"}
 | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3428 |   and @{term "op <"} (multiset order).
 | 
| 60500 | 3429 | \<close> | 
| 3430 | ||
| 3431 | text \<open>Quickcheck generators\<close> | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3432 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3433 | definition (in term_syntax) | 
| 61076 | 3434 | msetify :: "'a::typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term) | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3435 | \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where | 
| 60515 | 3436 |   [code_unfold]: "msetify xs = Code_Evaluation.valtermify mset {\<cdot>} xs"
 | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3437 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3438 | notation fcomp (infixl "\<circ>>" 60) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3439 | notation scomp (infixl "\<circ>\<rightarrow>" 60) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3440 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3441 | instantiation multiset :: (random) random | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3442 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3443 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3444 | definition | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3445 | "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 | 3446 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3447 | instance .. | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3448 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3449 | end | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3450 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3451 | no_notation fcomp (infixl "\<circ>>" 60) | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3452 | 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 | 3453 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3454 | instantiation multiset :: (full_exhaustive) full_exhaustive | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3455 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3456 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3457 | 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 | 3458 | where | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3459 | "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 | 3460 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3461 | instance .. | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3462 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3463 | end | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3464 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3465 | hide_const (open) msetify | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3466 | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3467 | |
| 60500 | 3468 | subsection \<open>BNF setup\<close> | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3469 | |
| 57966 | 3470 | definition rel_mset where | 
| 60515 | 3471 | "rel_mset R X Y \<longleftrightarrow> (\<exists>xs ys. mset xs = X \<and> mset ys = Y \<and> list_all2 R xs ys)" | 
| 3472 | ||
| 3473 | lemma mset_zip_take_Cons_drop_twice: | |
| 57966 | 3474 | assumes "length xs = length ys" "j \<le> length xs" | 
| 60515 | 3475 | shows "mset (zip (take j xs @ x # drop j xs) (take j ys @ y # drop j ys)) = | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3476 | add_mset (x,y) (mset (zip xs ys))" | 
| 60606 | 3477 | using assms | 
| 57966 | 3478 | proof (induct xs ys arbitrary: x y j rule: list_induct2) | 
| 3479 | case Nil | |
| 3480 | thus ?case | |
| 3481 | by simp | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3482 | next | 
| 57966 | 3483 | case (Cons x xs y ys) | 
| 3484 | thus ?case | |
| 3485 | proof (cases "j = 0") | |
| 3486 | case True | |
| 3487 | thus ?thesis | |
| 3488 | by simp | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3489 | next | 
| 57966 | 3490 | case False | 
| 3491 | then obtain k where k: "j = Suc k" | |
| 60678 | 3492 | by (cases j) simp | 
| 57966 | 3493 | hence "k \<le> length xs" | 
| 3494 | using Cons.prems by auto | |
| 60515 | 3495 | hence "mset (zip (take k xs @ x # drop k xs) (take k ys @ y # drop k ys)) = | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3496 | add_mset (x,y) (mset (zip xs ys))" | 
| 57966 | 3497 | by (rule Cons.hyps(2)) | 
| 3498 | thus ?thesis | |
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3499 | unfolding k by auto | 
| 58425 | 3500 | qed | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3501 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3502 | |
| 60515 | 3503 | lemma ex_mset_zip_left: | 
| 3504 | assumes "length xs = length ys" "mset xs' = mset xs" | |
| 3505 | shows "\<exists>ys'. length ys' = length xs' \<and> mset (zip xs' ys') = mset (zip xs ys)" | |
| 58425 | 3506 | using assms | 
| 57966 | 3507 | proof (induct xs ys arbitrary: xs' rule: list_induct2) | 
| 3508 | case Nil | |
| 3509 | thus ?case | |
| 3510 | by auto | |
| 3511 | next | |
| 3512 | case (Cons x xs y ys xs') | |
| 3513 | obtain j where j_len: "j < length xs'" and nth_j: "xs' ! j = x" | |
| 60515 | 3514 | by (metis Cons.prems in_set_conv_nth list.set_intros(1) mset_eq_setD) | 
| 58425 | 3515 | |
| 63040 | 3516 | define xsa where "xsa = take j xs' @ drop (Suc j) xs'" | 
| 60515 | 3517 |   have "mset xs' = {#x#} + mset xsa"
 | 
| 57966 | 3518 | unfolding xsa_def using j_len nth_j | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3519 | by (metis Cons_nth_drop_Suc union_mset_add_mset_right add_mset_remove_trivial add_diff_cancel_left' | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3520 | append_take_drop_id mset.simps(2) mset_append) | 
| 60515 | 3521 | hence ms_x: "mset xsa = mset xs" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3522 | by (simp add: Cons.prems) | 
| 57966 | 3523 | then obtain ysa where | 
| 60515 | 3524 | len_a: "length ysa = length xsa" and ms_a: "mset (zip xsa ysa) = mset (zip xs ys)" | 
| 57966 | 3525 | using Cons.hyps(2) by blast | 
| 3526 | ||
| 63040 | 3527 | define ys' where "ys' = take j ysa @ y # drop j ysa" | 
| 57966 | 3528 | have xs': "xs' = take j xsa @ x # drop j xsa" | 
| 3529 | using ms_x j_len nth_j Cons.prems xsa_def | |
| 58247 
98d0f85d247f
enamed drop_Suc_conv_tl and nth_drop' to Cons_nth_drop_Suc
 nipkow parents: 
58098diff
changeset | 3530 | by (metis append_eq_append_conv append_take_drop_id diff_Suc_Suc Cons_nth_drop_Suc length_Cons | 
| 60515 | 3531 | length_drop size_mset) | 
| 57966 | 3532 | have j_len': "j \<le> length xsa" | 
| 3533 | using j_len xs' xsa_def | |
| 3534 | by (metis add_Suc_right append_take_drop_id length_Cons length_append less_eq_Suc_le not_less) | |
| 3535 | have "length ys' = length xs'" | |
| 3536 | unfolding ys'_def using Cons.prems len_a ms_x | |
| 60515 | 3537 | by (metis add_Suc_right append_take_drop_id length_Cons length_append mset_eq_length) | 
| 3538 | moreover have "mset (zip xs' ys') = mset (zip (x # xs) (y # ys))" | |
| 57966 | 3539 | unfolding xs' ys'_def | 
| 60515 | 3540 | by (rule trans[OF mset_zip_take_Cons_drop_twice]) | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3541 | (auto simp: len_a ms_a j_len') | 
| 57966 | 3542 | ultimately show ?case | 
| 3543 | by blast | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3544 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3545 | |
| 57966 | 3546 | lemma list_all2_reorder_left_invariance: | 
| 60515 | 3547 | assumes rel: "list_all2 R xs ys" and ms_x: "mset xs' = mset xs" | 
| 3548 | shows "\<exists>ys'. list_all2 R xs' ys' \<and> mset ys' = mset ys" | |
| 57966 | 3549 | proof - | 
| 3550 | have len: "length xs = length ys" | |
| 3551 | using rel list_all2_conv_all_nth by auto | |
| 3552 | obtain ys' where | |
| 60515 | 3553 | len': "length xs' = length ys'" and ms_xy: "mset (zip xs' ys') = mset (zip xs ys)" | 
| 3554 | using len ms_x by (metis ex_mset_zip_left) | |
| 57966 | 3555 | have "list_all2 R xs' ys'" | 
| 60515 | 3556 | using assms(1) len' ms_xy unfolding list_all2_iff by (blast dest: mset_eq_setD) | 
| 3557 | moreover have "mset ys' = mset ys" | |
| 3558 | using len len' ms_xy map_snd_zip mset_map by metis | |
| 57966 | 3559 | ultimately show ?thesis | 
| 3560 | by blast | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3561 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3562 | |
| 60515 | 3563 | lemma ex_mset: "\<exists>xs. mset xs = X" | 
| 3564 | by (induct X) (simp, metis mset.simps(2)) | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3565 | |
| 62324 | 3566 | inductive pred_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> bool"
 | 
| 3567 | where | |
| 3568 |   "pred_mset P {#}"
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3569 | | "\<lbrakk>P a; pred_mset P M\<rbrakk> \<Longrightarrow> pred_mset P (add_mset a M)" | 
| 62324 | 3570 | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3571 | bnf "'a multiset" | 
| 57966 | 3572 | map: image_mset | 
| 60495 | 3573 | sets: set_mset | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3574 | bd: natLeq | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3575 |   wits: "{#}"
 | 
| 57966 | 3576 | rel: rel_mset | 
| 62324 | 3577 | pred: pred_mset | 
| 57966 | 3578 | proof - | 
| 3579 | show "image_mset id = id" | |
| 3580 | by (rule image_mset.id) | |
| 60606 | 3581 | show "image_mset (g \<circ> f) = image_mset g \<circ> image_mset f" for f g | 
| 59813 | 3582 | unfolding comp_def by (rule ext) (simp add: comp_def image_mset.compositionality) | 
| 60606 | 3583 | show "(\<And>z. z \<in> set_mset X \<Longrightarrow> f z = g z) \<Longrightarrow> image_mset f X = image_mset g X" for f g X | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 3584 | by (induct X) simp_all | 
| 60606 | 3585 | show "set_mset \<circ> image_mset f = op ` f \<circ> set_mset" for f | 
| 57966 | 3586 | by auto | 
| 3587 | show "card_order natLeq" | |
| 3588 | by (rule natLeq_card_order) | |
| 3589 | show "BNF_Cardinal_Arithmetic.cinfinite natLeq" | |
| 3590 | by (rule natLeq_cinfinite) | |
| 60606 | 3591 | show "ordLeq3 (card_of (set_mset X)) natLeq" for X | 
| 57966 | 3592 | by transfer | 
| 3593 | (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def) | |
| 60606 | 3594 | show "rel_mset R OO rel_mset S \<le> rel_mset (R OO S)" for R S | 
| 57966 | 3595 | unfolding rel_mset_def[abs_def] OO_def | 
| 3596 | apply clarify | |
| 60678 | 3597 | subgoal for X Z Y xs ys' ys zs | 
| 3598 | apply (drule list_all2_reorder_left_invariance [where xs = ys' and ys = zs and xs' = ys]) | |
| 3599 | apply (auto intro: list_all2_trans) | |
| 3600 | done | |
| 60606 | 3601 | done | 
| 3602 | show "rel_mset R = | |
| 62324 | 3603 |     (\<lambda>x y. \<exists>z. set_mset z \<subseteq> {(x, y). R x y} \<and>
 | 
| 3604 | image_mset fst z = x \<and> image_mset snd z = y)" for R | |
| 3605 | unfolding rel_mset_def[abs_def] | |
| 57966 | 3606 | apply (rule ext)+ | 
| 62324 | 3607 | apply safe | 
| 3608 | apply (rule_tac x = "mset (zip xs ys)" in exI; | |
| 3609 | auto simp: in_set_zip list_all2_iff mset_map[symmetric]) | |
| 57966 | 3610 | apply (rename_tac XY) | 
| 60515 | 3611 | apply (cut_tac X = XY in ex_mset) | 
| 57966 | 3612 | apply (erule exE) | 
| 3613 | apply (rename_tac xys) | |
| 3614 | apply (rule_tac x = "map fst xys" in exI) | |
| 60515 | 3615 | apply (auto simp: mset_map) | 
| 57966 | 3616 | apply (rule_tac x = "map snd xys" in exI) | 
| 60515 | 3617 | apply (auto simp: mset_map list_all2I subset_eq zip_map_fst_snd) | 
| 59997 | 3618 | done | 
| 60606 | 3619 |   show "z \<in> set_mset {#} \<Longrightarrow> False" for z
 | 
| 57966 | 3620 | by auto | 
| 62324 | 3621 | show "pred_mset P = (\<lambda>x. Ball (set_mset x) P)" for P | 
| 3622 | proof (intro ext iffI) | |
| 3623 | fix x | |
| 3624 | assume "pred_mset P x" | |
| 3625 | then show "Ball (set_mset x) P" by (induct pred: pred_mset; simp) | |
| 3626 | next | |
| 3627 | fix x | |
| 3628 | assume "Ball (set_mset x) P" | |
| 3629 | then show "pred_mset P x" by (induct x; auto intro: pred_mset.intros) | |
| 3630 | qed | |
| 57966 | 3631 | qed | 
| 3632 | ||
| 60606 | 3633 | inductive rel_mset' | 
| 3634 | where | |
| 57966 | 3635 |   Zero[intro]: "rel_mset' R {#} {#}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3636 | | Plus[intro]: "\<lbrakk>R a b; rel_mset' R M N\<rbrakk> \<Longrightarrow> rel_mset' R (add_mset a M) (add_mset b N)" | 
| 57966 | 3637 | |
| 3638 | lemma rel_mset_Zero: "rel_mset R {#} {#}"
 | |
| 3639 | unfolding rel_mset_def Grp_def by auto | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3640 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3641 | declare multiset.count[simp] | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3642 | declare Abs_multiset_inverse[simp] | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3643 | declare multiset.count_inverse[simp] | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3644 | declare union_preserves_multiset[simp] | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3645 | |
| 57966 | 3646 | lemma rel_mset_Plus: | 
| 60606 | 3647 | assumes ab: "R a b" | 
| 3648 | and MN: "rel_mset R M N" | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3649 | shows "rel_mset R (add_mset a M) (add_mset b N)" | 
| 60606 | 3650 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3651 | have "\<exists>ya. add_mset a (image_mset fst y) = image_mset fst ya \<and> | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3652 | add_mset b (image_mset snd y) = image_mset snd ya \<and> | 
| 60606 | 3653 |     set_mset ya \<subseteq> {(x, y). R x y}"
 | 
| 3654 |     if "R a b" and "set_mset y \<subseteq> {(x, y). R x y}" for y
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3655 | using that by (intro exI[of _ "add_mset (a,b) y"]) auto | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3656 | thus ?thesis | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3657 | using assms | 
| 57966 | 3658 | unfolding multiset.rel_compp_Grp Grp_def by blast | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3659 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3660 | |
| 60606 | 3661 | lemma rel_mset'_imp_rel_mset: "rel_mset' R M N \<Longrightarrow> rel_mset R M N" | 
| 60678 | 3662 | by (induct rule: rel_mset'.induct) (auto simp: rel_mset_Zero rel_mset_Plus) | 
| 57966 | 3663 | |
| 60606 | 3664 | lemma rel_mset_size: "rel_mset R M N \<Longrightarrow> size M = size N" | 
| 60678 | 3665 | unfolding multiset.rel_compp_Grp Grp_def by auto | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3666 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3667 | lemma multiset_induct2[case_names empty addL addR]: | 
| 60678 | 3668 |   assumes empty: "P {#} {#}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3669 | and addL: "\<And>a M N. P M N \<Longrightarrow> P (add_mset a M) N" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3670 | and addR: "\<And>a M N. P M N \<Longrightarrow> P M (add_mset a N)" | 
| 60678 | 3671 | shows "P M N" | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3672 | apply(induct N rule: multiset_induct) | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3673 | apply(induct M rule: multiset_induct, rule empty, erule addL) | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3674 | apply(induct M rule: multiset_induct, erule addR, erule addR) | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3675 | done | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3676 | |
| 59949 | 3677 | lemma multiset_induct2_size[consumes 1, case_names empty add]: | 
| 60606 | 3678 | assumes c: "size M = size N" | 
| 3679 |     and empty: "P {#} {#}"
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3680 | and add: "\<And>a b M N a b. P M N \<Longrightarrow> P (add_mset a M) (add_mset b N)" | 
| 60606 | 3681 | shows "P M N" | 
| 60678 | 3682 | using c | 
| 3683 | proof (induct M arbitrary: N rule: measure_induct_rule[of size]) | |
| 60606 | 3684 | case (less M) | 
| 3685 | show ?case | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3686 |   proof(cases "M = {#}")
 | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3687 |     case True hence "N = {#}" using less.prems by auto
 | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3688 | thus ?thesis using True empty by auto | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3689 | next | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3690 | case False then obtain M1 a where M: "M = add_mset a M1" by (metis multi_nonempty_split) | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3691 |     have "N \<noteq> {#}" using False less.prems by auto
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3692 | then obtain N1 b where N: "N = add_mset b N1" by (metis multi_nonempty_split) | 
| 59949 | 3693 | have "size M1 = size N1" using less.prems unfolding M N by auto | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3694 | thus ?thesis using M N less.hyps add by auto | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3695 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3696 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3697 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3698 | lemma msed_map_invL: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3699 | assumes "image_mset f (add_mset a M) = N" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3700 | shows "\<exists>N1. N = add_mset (f a) N1 \<and> image_mset f M = N1" | 
| 60606 | 3701 | proof - | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3702 | have "f a \<in># N" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3703 | using assms multiset.set_map[of f "add_mset a M"] by auto | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3704 | then obtain N1 where N: "N = add_mset (f a) N1" using multi_member_split by metis | 
| 57966 | 3705 | have "image_mset f M = N1" using assms unfolding N by simp | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3706 | thus ?thesis using N by blast | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3707 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3708 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3709 | lemma msed_map_invR: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3710 | assumes "image_mset f M = add_mset b N" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3711 | shows "\<exists>M1 a. M = add_mset a M1 \<and> f a = b \<and> image_mset f M1 = N" | 
| 60606 | 3712 | proof - | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3713 | obtain a where a: "a \<in># M" and fa: "f a = b" | 
| 60606 | 3714 | using multiset.set_map[of f M] unfolding assms | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 3715 | by (metis image_iff union_single_eq_member) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3716 | then obtain M1 where M: "M = add_mset a M1" using multi_member_split by metis | 
| 57966 | 3717 | have "image_mset f M1 = N" using assms unfolding M fa[symmetric] by simp | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3718 | thus ?thesis using M fa by blast | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3719 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3720 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3721 | lemma msed_rel_invL: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3722 | assumes "rel_mset R (add_mset a M) N" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3723 | shows "\<exists>N1 b. N = add_mset b N1 \<and> R a b \<and> rel_mset R M N1" | 
| 60606 | 3724 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3725 | obtain K where KM: "image_mset fst K = add_mset a M" | 
| 60606 | 3726 |     and KN: "image_mset snd K = N" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
 | 
| 3727 | using assms | |
| 3728 | unfolding multiset.rel_compp_Grp Grp_def by auto | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3729 | obtain K1 ab where K: "K = add_mset ab K1" and a: "fst ab = a" | 
| 60606 | 3730 | and K1M: "image_mset fst K1 = M" using msed_map_invR[OF KM] by auto | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3731 | obtain N1 where N: "N = add_mset (snd ab) N1" and K1N1: "image_mset snd K1 = N1" | 
| 60606 | 3732 | using msed_map_invL[OF KN[unfolded K]] by auto | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3733 | have Rab: "R a (snd ab)" using sK a unfolding K by auto | 
| 57966 | 3734 | have "rel_mset R M N1" using sK K1M K1N1 | 
| 60606 | 3735 | unfolding K multiset.rel_compp_Grp Grp_def by auto | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3736 | thus ?thesis using N Rab by auto | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3737 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3738 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3739 | lemma msed_rel_invR: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3740 | assumes "rel_mset R M (add_mset b N)" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3741 | shows "\<exists>M1 a. M = add_mset a M1 \<and> R a b \<and> rel_mset R M1 N" | 
| 60606 | 3742 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3743 | obtain K where KN: "image_mset snd K = add_mset b N" | 
| 60606 | 3744 |     and KM: "image_mset fst K = M" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
 | 
| 3745 | using assms | |
| 3746 | unfolding multiset.rel_compp_Grp Grp_def by auto | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3747 | obtain K1 ab where K: "K = add_mset ab K1" and b: "snd ab = b" | 
| 60606 | 3748 | and K1N: "image_mset snd K1 = N" using msed_map_invR[OF KN] by auto | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3749 | obtain M1 where M: "M = add_mset (fst ab) M1" and K1M1: "image_mset fst K1 = M1" | 
| 60606 | 3750 | using msed_map_invL[OF KM[unfolded K]] by auto | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3751 | have Rab: "R (fst ab) b" using sK b unfolding K by auto | 
| 57966 | 3752 | have "rel_mset R M1 N" using sK K1N K1M1 | 
| 60606 | 3753 | unfolding K multiset.rel_compp_Grp Grp_def by auto | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3754 | thus ?thesis using M Rab by auto | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3755 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3756 | |
| 57966 | 3757 | lemma rel_mset_imp_rel_mset': | 
| 60606 | 3758 | assumes "rel_mset R M N" | 
| 3759 | shows "rel_mset' R M N" | |
| 59949 | 3760 | using assms proof(induct M arbitrary: N rule: measure_induct_rule[of size]) | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3761 | case (less M) | 
| 59949 | 3762 | have c: "size M = size N" using rel_mset_size[OF less.prems] . | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3763 | show ?case | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3764 |   proof(cases "M = {#}")
 | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3765 |     case True hence "N = {#}" using c by simp
 | 
| 57966 | 3766 | thus ?thesis using True rel_mset'.Zero by auto | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3767 | next | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3768 | case False then obtain M1 a where M: "M = add_mset a M1" by (metis multi_nonempty_split) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3769 | obtain N1 b where N: "N = add_mset b N1" and R: "R a b" and ms: "rel_mset R M1 N1" | 
| 60606 | 3770 | using msed_rel_invL[OF less.prems[unfolded M]] by auto | 
| 57966 | 3771 | have "rel_mset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp | 
| 3772 | thus ?thesis using rel_mset'.Plus[of R a b, OF R] unfolding M N by simp | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3773 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3774 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3775 | |
| 60606 | 3776 | lemma rel_mset_rel_mset': "rel_mset R M N = rel_mset' R M N" | 
| 60678 | 3777 | using rel_mset_imp_rel_mset' rel_mset'_imp_rel_mset by auto | 
| 57966 | 3778 | |
| 60613 | 3779 | text \<open>The main end product for @{const rel_mset}: inductive characterization:\<close>
 | 
| 61337 | 3780 | lemmas rel_mset_induct[case_names empty add, induct pred: rel_mset] = | 
| 60606 | 3781 | rel_mset'.induct[unfolded rel_mset_rel_mset'[symmetric]] | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3782 | |
| 56656 | 3783 | |
| 60500 | 3784 | subsection \<open>Size setup\<close> | 
| 56656 | 3785 | |
| 65547 | 3786 | lemma multiset_size_o_map: "size_multiset g \<circ> image_mset f = size_multiset (g \<circ> f)" | 
| 3787 | apply (rule ext) | |
| 3788 | subgoal for x by (induct x) auto | |
| 3789 | done | |
| 56656 | 3790 | |
| 60500 | 3791 | setup \<open> | 
| 60606 | 3792 |   BNF_LFP_Size.register_size_global @{type_name multiset} @{const_name size_multiset}
 | 
| 62082 | 3793 |     @{thm size_multiset_overloaded_def}
 | 
| 60606 | 3794 |     @{thms size_multiset_empty size_multiset_single size_multiset_union size_empty size_single
 | 
| 3795 | size_union} | |
| 3796 |     @{thms multiset_size_o_map}
 | |
| 60500 | 3797 | \<close> | 
| 56656 | 3798 | |
| 65547 | 3799 | subsection \<open>Lemmas about Size\<close> | 
| 3800 | ||
| 3801 | lemma size_mset_SucE: "size A = Suc n \<Longrightarrow> (\<And>a B. A = {#a#} + B \<Longrightarrow> size B = n \<Longrightarrow> P) \<Longrightarrow> P"
 | |
| 3802 | by (cases A) (auto simp add: ac_simps) | |
| 3803 | ||
| 3804 | lemma size_Suc_Diff1: "x \<in># M \<Longrightarrow> Suc (size (M - {#x#})) = size M"
 | |
| 3805 | using arg_cong[OF insert_DiffM, of _ _ size] by simp | |
| 3806 | ||
| 3807 | lemma size_Diff_singleton: "x \<in># M \<Longrightarrow> size (M - {#x#}) = size M - 1"
 | |
| 3808 | by (simp add: size_Suc_Diff1 [symmetric]) | |
| 3809 | ||
| 3810 | lemma size_Diff_singleton_if: "size (A - {#x#}) = (if x \<in># A then size A - 1 else size A)"
 | |
| 3811 | by (simp add: diff_single_trivial size_Diff_singleton) | |
| 3812 | ||
| 3813 | lemma size_Un_Int: "size A + size B = size (A \<union># B) + size (A \<inter># B)" | |
| 3814 | by (metis inter_subset_eq_union size_union subset_mset.diff_add union_diff_inter_eq_sup) | |
| 3815 | ||
| 3816 | lemma size_Un_disjoint: "A \<inter># B = {#} \<Longrightarrow> size (A \<union># B) = size A + size B"
 | |
| 3817 | using size_Un_Int[of A B] by simp | |
| 3818 | ||
| 3819 | lemma size_Diff_subset_Int: "size (M - M') = size M - size (M \<inter># M')" | |
| 3820 | by (metis diff_intersect_left_idem size_Diff_submset subset_mset.inf_le1) | |
| 3821 | ||
| 3822 | lemma diff_size_le_size_Diff: "size (M :: _ multiset) - size M' \<le> size (M - M')" | |
| 3823 | by (simp add: diff_le_mono2 size_Diff_subset_Int size_mset_mono) | |
| 3824 | ||
| 3825 | lemma size_Diff1_less: "x\<in># M \<Longrightarrow> size (M - {#x#}) < size M"
 | |
| 3826 | by (rule Suc_less_SucD) (simp add: size_Suc_Diff1) | |
| 3827 | ||
| 3828 | lemma size_Diff2_less: "x\<in># M \<Longrightarrow> y\<in># M \<Longrightarrow> size (M - {#x#} - {#y#}) < size M"
 | |
| 3829 | by (metis less_imp_diff_less size_Diff1_less size_Diff_subset_Int) | |
| 3830 | ||
| 3831 | lemma size_Diff1_le: "size (M - {#x#}) \<le> size M"
 | |
| 3832 | by (cases "x \<in># M") (simp_all add: size_Diff1_less less_imp_le diff_single_trivial) | |
| 3833 | ||
| 3834 | lemma size_psubset: "M \<subseteq># M' \<Longrightarrow> size M < size M' \<Longrightarrow> M \<subset># M'" | |
| 3835 | using less_irrefl subset_mset_def by blast | |
| 3836 | ||
| 56656 | 3837 | hide_const (open) wcount | 
| 3838 | ||
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 3839 | end |