| author | wenzelm | 
| Sun, 26 Mar 2023 15:02:08 +0200 | |
| changeset 77714 | be0b9396604e | 
| parent 77699 | d5060a919b3f | 
| child 77832 | 8260d8971d87 | 
| 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 | |
| 74803 | 7 | Author: Martin Desharnais, MPI-INF Saarbruecken | 
| 10249 | 8 | *) | 
| 9 | ||
| 65048 | 10 | section \<open>(Finite) Multisets\<close> | 
| 10249 | 11 | |
| 15131 | 12 | theory Multiset | 
| 74865 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 13 | imports Cancellation | 
| 15131 | 14 | begin | 
| 10249 | 15 | |
| 60500 | 16 | subsection \<open>The type of multisets\<close> | 
| 10249 | 17 | |
| 73270 | 18 | typedef 'a multiset = \<open>{f :: 'a \<Rightarrow> nat. finite {x. f x > 0}}\<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 | 19 | morphisms count Abs_multiset | 
| 10249 | 20 | proof | 
| 73270 | 21 |   show \<open>(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}\<close>
 | 
| 22 | by simp | |
| 10249 | 23 | qed | 
| 24 | ||
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 25 | setup_lifting type_definition_multiset | 
| 19086 | 26 | |
| 73270 | 27 | lemma count_Abs_multiset: | 
| 28 |   \<open>count (Abs_multiset f) = f\<close> if \<open>finite {x. f x > 0}\<close>
 | |
| 29 | by (rule Abs_multiset_inverse) (simp add: that) | |
| 30 | ||
| 60606 | 31 | 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 | 32 | by (simp only: count_inject [symmetric] fun_eq_iff) | 
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 33 | |
| 60606 | 34 | 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 | 35 | 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 | 36 | |
| 69593 | 37 | text \<open>Preservation of the representing set \<^term>\<open>multiset\<close>.\<close> | 
| 60606 | 38 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 39 | lemma diff_preserves_multiset: | 
| 73270 | 40 |   \<open>finite {x. 0 < M x - N x}\<close> if \<open>finite {x. 0 < M x}\<close> for M N :: \<open>'a \<Rightarrow> nat\<close>
 | 
| 41 | using that by (rule rev_finite_subset) 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 | 42 | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 43 | lemma filter_preserves_multiset: | 
| 73270 | 44 |   \<open>finite {x. 0 < (if P x then M x else 0)}\<close> if \<open>finite {x. 0 < M x}\<close> for M N :: \<open>'a \<Rightarrow> nat\<close>
 | 
| 45 | using that by (rule rev_finite_subset) auto | |
| 46 | ||
| 47 | lemmas in_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 | 48 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned 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 | |
| 60500 | 50 | subsection \<open>Representing multisets\<close> | 
| 51 | ||
| 52 | 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 | 53 | |
| 48008 | 54 | instantiation multiset :: (type) cancel_comm_monoid_add | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 55 | begin | 
| 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 56 | |
| 73393 | 57 | lift_definition zero_multiset :: \<open>'a multiset\<close> | 
| 58 | is \<open>\<lambda>a. 0\<close> | |
| 73270 | 59 | by simp | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 60 | |
| 73393 | 61 | abbreviation empty_mset :: \<open>'a multiset\<close> (\<open>{#}\<close>)
 | 
| 62 | where \<open>empty_mset \<equiv> 0\<close> | |
| 63 | ||
| 64 | lift_definition plus_multiset :: \<open>'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset\<close> | |
| 65 | is \<open>\<lambda>M N a. M a + N a\<close> | |
| 73270 | 66 | by simp | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 67 | |
| 73393 | 68 | lift_definition minus_multiset :: \<open>'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset\<close> | 
| 69 | is \<open>\<lambda>M N a. M a - N a\<close> | |
| 73270 | 70 | by (rule diff_preserves_multiset) | 
| 59815 
cce82e360c2f
explicit commutative additive inverse operation;
 haftmann parents: 
59813diff
changeset | 71 | |
| 48008 | 72 | instance | 
| 73270 | 73 | by (standard; transfer) (simp_all add: fun_eq_iff) | 
| 25571 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 74 | |
| 
c9e39eafc7a0
instantiation target rather than legacy instance
 haftmann parents: 
25507diff
changeset | 75 | end | 
| 10249 | 76 | |
| 63195 | 77 | context | 
| 78 | begin | |
| 79 | ||
| 80 | qualified definition is_empty :: "'a multiset \<Rightarrow> bool" where | |
| 81 |   [code_abbrev]: "is_empty A \<longleftrightarrow> A = {#}"
 | |
| 82 | ||
| 83 | end | |
| 84 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 85 | lemma add_mset_in_multiset: | 
| 73270 | 86 |   \<open>finite {x. 0 < (if x = a then Suc (M x) else M x)}\<close>
 | 
| 87 |   if \<open>finite {x. 0 < M x}\<close>
 | |
| 88 | using that by (simp add: flip: insert_Collect) | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 89 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 90 | 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 | 91 | "\<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 | 92 | by (rule add_mset_in_multiset) | 
| 15869 | 93 | |
| 26145 | 94 | syntax | 
| 60606 | 95 |   "_multiset" :: "args \<Rightarrow> 'a multiset"    ("{#(_)#}")
 | 
| 25507 | 96 | translations | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 97 |   "{#x, xs#}" == "CONST add_mset x {#xs#}"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 98 |   "{#x#}" == "CONST add_mset x {#}"
 | 
| 25507 | 99 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 100 | lemma count_empty [simp]: "count {#} a = 0"
 | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 101 | by (simp add: zero_multiset.rep_eq) | 
| 10249 | 102 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 103 | lemma count_add_mset [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 104 | "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 | 105 | by (simp add: add_mset.rep_eq) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 106 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 107 | 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 | 108 | by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 109 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 110 | lemma | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 111 |   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 | 112 |   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 | 113 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 114 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 115 | lemma add_mset_add_mset_same_iff [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 116 | "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 | 117 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 118 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 119 | lemma add_mset_commute: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 120 | "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 | 121 | by (auto simp: multiset_eq_iff) | 
| 29901 | 122 | |
| 10249 | 123 | |
| 60500 | 124 | subsection \<open>Basic operations\<close> | 
| 125 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 126 | subsubsection \<open>Conversion to set and membership\<close> | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 127 | |
| 73393 | 128 | definition set_mset :: \<open>'a multiset \<Rightarrow> 'a set\<close> | 
| 129 |   where \<open>set_mset M = {x. count M x > 0}\<close>
 | |
| 130 | ||
| 131 | abbreviation member_mset :: \<open>'a \<Rightarrow> 'a multiset \<Rightarrow> bool\<close> | |
| 132 | where \<open>member_mset a M \<equiv> a \<in> set_mset M\<close> | |
| 62537 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 133 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 134 | notation | 
| 73393 | 135 | member_mset (\<open>'(\<in>#')\<close>) and | 
| 73394 | 136 | member_mset (\<open>(_/ \<in># _)\<close> [50, 51] 50) | 
| 62537 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 137 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 138 | notation (ASCII) | 
| 73393 | 139 | member_mset (\<open>'(:#')\<close>) and | 
| 73394 | 140 | member_mset (\<open>(_/ :# _)\<close> [50, 51] 50) | 
| 73393 | 141 | |
| 142 | abbreviation not_member_mset :: \<open>'a \<Rightarrow> 'a multiset \<Rightarrow> bool\<close> | |
| 143 | where \<open>not_member_mset a M \<equiv> a \<notin> set_mset M\<close> | |
| 62537 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 144 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 145 | notation | 
| 73393 | 146 | not_member_mset (\<open>'(\<notin>#')\<close>) and | 
| 73394 | 147 | not_member_mset (\<open>(_/ \<notin># _)\<close> [50, 51] 50) | 
| 62537 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 148 | |
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 149 | notation (ASCII) | 
| 73393 | 150 | not_member_mset (\<open>'(~:#')\<close>) and | 
| 73394 | 151 | not_member_mset (\<open>(_/ ~:# _)\<close> [50, 51] 50) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 152 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 153 | context | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 154 | begin | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 155 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 156 | 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 | 157 | where "Ball M \<equiv> Set.Ball (set_mset M)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 158 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 159 | 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 | 160 | where "Bex M \<equiv> Set.Bex (set_mset M)" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 161 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 162 | end | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 163 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 164 | syntax | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 165 |   "_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 | 166 |   "_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 | 167 | |
| 62537 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 168 | syntax (ASCII) | 
| 
7a9aa69f9b38
syntax for multiset membership modelled after syntax for set membership
 haftmann parents: 
62430diff
changeset | 169 |   "_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 | 170 |   "_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 | 171 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 172 | translations | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 173 | "\<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 | 174 | "\<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 | 175 | |
| 71917 
4c5778d8a53d
should have been copied across from Set.thy as well for better printing
 nipkow parents: 
71398diff
changeset | 176 | print_translation \<open> | 
| 
4c5778d8a53d
should have been copied across from Set.thy as well for better printing
 nipkow parents: 
71398diff
changeset | 177 | [Syntax_Trans.preserve_binder_abs2_tr' \<^const_syntax>\<open>Multiset.Ball\<close> \<^syntax_const>\<open>_MBall\<close>, | 
| 
4c5778d8a53d
should have been copied across from Set.thy as well for better printing
 nipkow parents: 
71398diff
changeset | 178 | Syntax_Trans.preserve_binder_abs2_tr' \<^const_syntax>\<open>Multiset.Bex\<close> \<^syntax_const>\<open>_MBex\<close>] | 
| 
4c5778d8a53d
should have been copied across from Set.thy as well for better printing
 nipkow parents: 
71398diff
changeset | 179 | \<close> \<comment> \<open>to avoid eta-contraction of body\<close> | 
| 
4c5778d8a53d
should have been copied across from Set.thy as well for better printing
 nipkow parents: 
71398diff
changeset | 180 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 181 | lemma count_eq_zero_iff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 182 | "count M x = 0 \<longleftrightarrow> x \<notin># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 183 | by (auto simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 184 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 185 | lemma not_in_iff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 186 | "x \<notin># M \<longleftrightarrow> count M x = 0" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 187 | by (auto simp add: count_eq_zero_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 188 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 189 | lemma count_greater_zero_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 190 | "count M x > 0 \<longleftrightarrow> x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 191 | by (auto simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 192 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 193 | lemma count_inI: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 194 | assumes "count M x = 0 \<Longrightarrow> False" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 195 | shows "x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 196 | proof (rule ccontr) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 197 | assume "x \<notin># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 198 | 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 | 199 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 200 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 201 | lemma in_countE: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 202 | assumes "x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 203 | obtains n where "count M x = Suc n" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 204 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 205 | from assms have "count M x > 0" by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 206 | then obtain n where "count M x = Suc n" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 207 | using gr0_conv_Suc by blast | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 208 | with that show thesis . | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 209 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 210 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 211 | lemma count_greater_eq_Suc_zero_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 212 | "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 | 213 | by (simp add: Suc_le_eq) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 214 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 215 | lemma count_greater_eq_one_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 216 | "count M x \<ge> 1 \<longleftrightarrow> x \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 217 | by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 218 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 219 | lemma set_mset_empty [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 220 |   "set_mset {#} = {}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 221 | by (simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 222 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 223 | lemma set_mset_single: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 224 |   "set_mset {#b#} = {b}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 225 | by (simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 226 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 227 | lemma set_mset_eq_empty_iff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 228 |   "set_mset M = {} \<longleftrightarrow> M = {#}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 229 | 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 | 230 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 231 | lemma finite_set_mset [iff]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 232 | "finite (set_mset M)" | 
| 73270 | 233 | using count [of M] by simp | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 234 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 235 | lemma set_mset_add_mset_insert [simp]: \<open>set_mset (add_mset a A) = insert a (set_mset A)\<close> | 
| 68406 | 236 | by (auto simp flip: count_greater_eq_Suc_zero_iff split: if_splits) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 237 | |
| 63924 | 238 | lemma multiset_nonemptyE [elim]: | 
| 239 |   assumes "A \<noteq> {#}"
 | |
| 240 | obtains x where "x \<in># A" | |
| 241 | proof - | |
| 242 | have "\<exists>x. x \<in># A" by (rule ccontr) (insert assms, auto) | |
| 243 | with that show ?thesis by blast | |
| 244 | qed | |
| 245 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 246 | |
| 60500 | 247 | subsubsection \<open>Union\<close> | 
| 10249 | 248 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 249 | lemma count_union [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 250 | "count (M + N) a = count M a + count N a" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 251 | by (simp add: plus_multiset.rep_eq) | 
| 10249 | 252 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 253 | lemma set_mset_union [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 254 | "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 | 255 | 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 | 256 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 257 | lemma union_mset_add_mset_left [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 258 | "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 | 259 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 260 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 261 | lemma union_mset_add_mset_right [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 262 | "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 | 263 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 264 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 265 | 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 | 266 | 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 | 267 | |
| 10249 | 268 | |
| 60500 | 269 | subsubsection \<open>Difference\<close> | 
| 10249 | 270 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 271 | instance multiset :: (type) comm_monoid_diff | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 272 | by standard (transfer; simp add: fun_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 273 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 274 | lemma count_diff [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 275 | "count (M - N) a = count M a - count N a" | 
| 47429 
ec64d94cbf9c
multiset operations are defined with lift_definitions;
 bulwahn parents: 
47308diff
changeset | 276 | 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 | 277 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 278 | lemma add_mset_diff_bothsides: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 279 | \<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 | 280 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 281 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 282 | lemma in_diff_count: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 283 | "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 | 284 | by (simp add: set_mset_def) | 
| 
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_in_diffI: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 287 | 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 | 288 | shows "x \<in># M - N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 289 | proof (rule ccontr) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 290 | assume "x \<notin># M - N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 291 | 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 | 292 | by (simp add: in_diff_count not_less) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 293 | with assms show False by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 294 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 295 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 296 | lemma in_diff_countE: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 297 | assumes "x \<in># M - N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 298 | 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 | 299 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 300 | 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 | 301 | 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 | 302 | 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 | 303 | using less_iff_Suc_add by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 304 | with that show thesis . | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 305 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 306 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 307 | lemma in_diffD: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 308 | assumes "a \<in># M - N" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 309 | shows "a \<in># M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 310 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 311 | have "0 \<le> count N a" by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 312 | 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 | 313 | by (simp add: in_diff_count) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 314 | finally show ?thesis by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 315 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 316 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 317 | lemma set_mset_diff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 318 |   "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 | 319 | by (simp add: set_mset_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 320 | |
| 17161 | 321 | lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
 | 
| 52289 | 322 | by rule (fact Groups.diff_zero, fact Groups.zero_diff) | 
| 36903 | 323 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 324 | lemma diff_cancel: "A - A = {#}"
 | 
| 52289 | 325 | by (fact Groups.diff_cancel) | 
| 10249 | 326 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 327 | lemma diff_union_cancelR: "M + N - N = (M::'a multiset)" | 
| 52289 | 328 | by (fact add_diff_cancel_right') | 
| 10249 | 329 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 330 | lemma diff_union_cancelL: "N + M - N = (M::'a multiset)" | 
| 52289 | 331 | 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 | 332 | |
| 52289 | 333 | lemma diff_right_commute: | 
| 60606 | 334 | fixes M N Q :: "'a multiset" | 
| 335 | shows "M - N - Q = M - Q - N" | |
| 52289 | 336 | by (fact diff_right_commute) | 
| 337 | ||
| 338 | lemma diff_add: | |
| 60606 | 339 | fixes M N Q :: "'a multiset" | 
| 340 | shows "M - (N + Q) = M - N - Q" | |
| 52289 | 341 | by (rule sym) (fact diff_diff_add) | 
| 58425 | 342 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 343 | 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 | 344 | 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 | 345 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 346 | 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 | 347 | by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 348 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 349 | 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 | 350 | 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 | 351 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 352 | 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 | 353 | 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 | 354 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 355 | 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 | 356 | 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 | 357 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 358 | 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 | 359 | by (rule diff_diff_add) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 360 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 361 | lemma diff_union_single_conv: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 362 |   "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 | 363 | by (simp add: multiset_eq_iff Suc_le_eq) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 364 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 365 | lemma mset_add [elim?]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 366 | assumes "a \<in># A" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 367 | obtains B where "A = add_mset a B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 368 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 369 |   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 | 370 | by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 371 | with that show thesis . | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 372 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 373 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 374 | lemma union_iff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 375 | "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 | 376 | by auto | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26033diff
changeset | 377 | |
| 10249 | 378 | |
| 66425 | 379 | subsubsection \<open>Min and Max\<close> | 
| 380 | ||
| 381 | abbreviation Min_mset :: "'a::linorder multiset \<Rightarrow> 'a" where | |
| 382 | "Min_mset m \<equiv> Min (set_mset m)" | |
| 383 | ||
| 384 | abbreviation Max_mset :: "'a::linorder multiset \<Rightarrow> 'a" where | |
| 385 | "Max_mset m \<equiv> Max (set_mset m)" | |
| 386 | ||
| 387 | ||
| 60500 | 388 | 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 | 389 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 390 | 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 | 391 | 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 | 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 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 | 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 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 | 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 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 399 | 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 | 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 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 | 403 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 404 | |
| 60606 | 405 | 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 | 406 | 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 | 407 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 408 | 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 | 409 | by auto | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 410 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 411 | 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 | 412 | 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 | 413 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 414 | 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 | 415 | 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 | 416 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 417 | lemma add_mset_remove_trivial_If: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 418 |   "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 | 419 | by (simp add: diff_single_trivial) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 420 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 421 | 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 | 422 | 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 | 423 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 424 | lemma union_is_single: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 425 |   "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N = {#} \<or> M = {#} \<and> N = {#a#}"
 | 
| 60606 | 426 | (is "?lhs = ?rhs") | 
| 46730 | 427 | proof | 
| 60606 | 428 | show ?lhs if ?rhs using that by auto | 
| 429 | show ?rhs if ?lhs | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 430 | 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 | 431 | 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 | 432 | |
| 60606 | 433 | 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 | 434 |   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 | 435 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 436 | lemma add_eq_conv_diff: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 437 |   "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 | 438 | (is "?lhs \<longleftrightarrow> ?rhs") | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44339diff
changeset | 439 | (* 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 | 440 | proof | 
| 60606 | 441 | show ?lhs if ?rhs | 
| 442 | using that | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 443 | by (auto simp add: add_mset_commute[of a b]) | 
| 60606 | 444 | 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 | 445 | proof (cases "a = b") | 
| 60500 | 446 | 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 | 447 | 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 | 448 | case False | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 449 | 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 | 450 | 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 | 451 |     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 | 452 | moreover note False | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 453 |     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 | 454 | 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 | 455 | 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 | 456 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 457 | 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 | 458 | by (auto simp: add_eq_conv_diff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 459 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 460 | 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 | 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 | |
| 58425 | 463 | lemma insert_noteq_member: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 464 | 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 | 465 | 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 | 466 | 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 | 467 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 468 | 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 | 469 |   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 | 470 | 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 | 471 | 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 | 472 | 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 | 473 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned 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 | lemma add_eq_conv_ex: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 475 | "(add_mset a M = add_mset b N) = | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 476 | (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 | 477 | 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 | 478 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 479 | lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = add_mset x A" | 
| 60678 | 480 |   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 | 481 | |
| 58425 | 482 | lemma multiset_add_sub_el_shuffle: | 
| 60606 | 483 | assumes "c \<in># B" | 
| 484 | and "b \<noteq> c" | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 485 |   shows "add_mset b (B - {#c#}) = add_mset b B - {#c#}"
 | 
| 58098 | 486 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 487 | from \<open>c \<in># B\<close> obtain A where B: "B = add_mset c A" | 
| 58098 | 488 | by (blast dest: multi_member_split) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 489 |   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 | 490 |   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 | 491 | by (simp add: \<open>b \<noteq> c\<close>) | 
| 58098 | 492 | then show ?thesis using B by simp | 
| 493 | qed | |
| 494 | ||
| 64418 | 495 | lemma add_mset_eq_singleton_iff[iff]: | 
| 496 |   "add_mset x M = {#y#} \<longleftrightarrow> M = {#} \<and> x = y"
 | |
| 497 | by auto | |
| 498 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 499 | |
| 60500 | 500 | 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 | 501 | |
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 502 | definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subseteq>#" 50) | 
| 65466 | 503 | 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 | 504 | |
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 505 | definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subset>#" 50) | 
| 65466 | 506 | 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 | 507 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 508 | 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 | 509 | where "supseteq_mset A B \<equiv> B \<subseteq># A" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 510 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 511 | 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 | 512 | where "supset_mset A B \<equiv> B \<subset># A" | 
| 62208 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 513 | |
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 514 | notation (input) | 
| 62208 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 515 | subseteq_mset (infix "\<le>#" 50) and | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 516 | 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 | 517 | |
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 518 | notation (ASCII) | 
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 519 | subseteq_mset (infix "<=#" 50) and | 
| 62208 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 520 | subset_mset (infix "<#" 50) and | 
| 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 521 | supseteq_mset (infix ">=#" 50) and | 
| 
ad43b3ab06e4
added 'supset' variants for new '<#' etc. symbols on multisets
 blanchet parents: 
62082diff
changeset | 522 | supset_mset (infix ">#" 50) | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 523 | |
| 73411 | 524 | global_interpretation subset_mset: ordering \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close> | 
| 525 | by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order.trans order.antisym) | |
| 526 | ||
| 73451 | 527 | interpretation subset_mset: ordered_ab_semigroup_add_imp_le \<open>(+)\<close> \<open>(-)\<close> \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close> | 
| 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 | |
| 67398 | 531 | interpretation subset_mset: ordered_ab_semigroup_monoid_add_imp_le "(+)" 0 "(-)" "(\<subseteq>#)" "(\<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 | |
| 67398 | 550 | interpretation subset_mset: ordered_cancel_comm_monoid_diff "(+)" 0 "(\<subseteq>#)" "(\<subset>#)" "(-)" | 
| 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 | |
| 73393 | 681 | definition inter_mset :: \<open>'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset\<close> (infixl \<open>\<inter>#\<close> 70) | 
| 682 | where \<open>A \<inter># B = A - (A - B)\<close> | |
| 683 | ||
| 73451 | 684 | lemma count_inter_mset [simp]: | 
| 685 | \<open>count (A \<inter># B) x = min (count A x) (count B x)\<close> | |
| 686 | by (simp add: inter_mset_def) | |
| 687 | ||
| 688 | (*global_interpretation subset_mset: semilattice_order \<open>(\<inter>#)\<close> \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close> | |
| 689 | by standard (simp_all add: multiset_eq_iff subseteq_mset_def subset_mset_def min_def)*) | |
| 690 | ||
| 73393 | 691 | interpretation subset_mset: semilattice_inf \<open>(\<inter>#)\<close> \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close> | 
| 73451 | 692 | by standard (simp_all add: multiset_eq_iff subseteq_mset_def) | 
| 693 | \<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 | 694 | |
| 73393 | 695 | definition union_mset :: \<open>'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset\<close> (infixl \<open>\<union>#\<close> 70) | 
| 696 | where \<open>A \<union># B = A + (B - A)\<close> | |
| 697 | ||
| 73451 | 698 | lemma count_union_mset [simp]: | 
| 699 | \<open>count (A \<union># B) x = max (count A x) (count B x)\<close> | |
| 700 | by (simp add: union_mset_def) | |
| 701 | ||
| 702 | global_interpretation subset_mset: semilattice_neutr_order \<open>(\<union>#)\<close> \<open>{#}\<close> \<open>(\<supseteq>#)\<close> \<open>(\<supset>#)\<close>
 | |
| 703 | apply standard | |
| 704 | apply (simp_all add: multiset_eq_iff subseteq_mset_def subset_mset_def max_def) | |
| 705 | apply (auto simp add: le_antisym dest: sym) | |
| 706 | apply (metis nat_le_linear)+ | |
| 707 | done | |
| 708 | ||
| 73393 | 709 | interpretation subset_mset: semilattice_sup \<open>(\<union>#)\<close> \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close> | 
| 64076 | 710 | proof - | 
| 711 | have [simp]: "m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" for m n q :: nat | |
| 712 | by arith | |
| 67398 | 713 | show "class.semilattice_sup (\<union>#) (\<subseteq>#) (\<subset>#)" | 
| 73393 | 714 | by standard (auto simp add: union_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 | 715 | qed \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 64076 | 716 | |
| 67398 | 717 | interpretation subset_mset: bounded_lattice_bot "(\<inter>#)" "(\<subseteq>#)" "(\<subset>#)" | 
| 718 |   "(\<union>#)" "{#}"
 | |
| 64076 | 719 | by standard auto | 
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 720 | \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 64076 | 721 | |
| 722 | ||
| 723 | subsubsection \<open>Additional intersection facts\<close> | |
| 724 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 725 | lemma set_mset_inter [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 726 | "set_mset (A \<inter># B) = set_mset A \<inter> set_mset B" | 
| 73393 | 727 | by (simp only: set_mset_def) auto | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 728 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 729 | lemma diff_intersect_left_idem [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 730 | "M - M \<inter># N = M - N" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 731 | by (simp add: multiset_eq_iff min_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 732 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 733 | lemma diff_intersect_right_idem [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 734 | "M - N \<inter># M = M - N" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 735 | by (simp add: multiset_eq_iff min_def) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 736 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 737 | lemma multiset_inter_single[simp]: "a \<noteq> b \<Longrightarrow> {#a#} \<inter># {#b#} = {#}"
 | 
| 46730 | 738 | 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 | 739 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 740 | lemma multiset_union_diff_commute: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 741 |   assumes "B \<inter># C = {#}"
 | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 742 | 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 | 743 | proof (rule multiset_eqI) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 744 | fix x | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 745 | from assms have "min (count B x) (count C x) = 0" | 
| 46730 | 746 | by (auto simp add: multiset_eq_iff) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 747 | 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 | 748 | unfolding min_def by (auto split: if_splits) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 749 | then show "count (A + B - C) x = count (A - C + B) x" | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 750 | by auto | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 751 | qed | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 752 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 753 | lemma disjunct_not_in: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 754 |   "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 | 755 | proof | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 756 | assume ?P | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 757 | show ?Q | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 758 | proof | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 759 | fix a | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 760 | 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 | 761 | by (simp add: multiset_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 762 | 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 | 763 | 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 | 764 | then show "a \<notin># A \<or> a \<notin># B" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 765 | by (simp add: not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 766 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 767 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 768 | assume ?Q | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 769 | show ?P | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 770 | proof (rule multiset_eqI) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 771 | fix a | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 772 | 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 | 773 | 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 | 774 |     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 | 775 | by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 776 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 777 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 778 | |
| 64077 | 779 | lemma inter_mset_empty_distrib_right: "A \<inter># (B + C) = {#} \<longleftrightarrow> A \<inter># B = {#} \<and> A \<inter># C = {#}"
 | 
| 780 | by (meson disjunct_not_in union_iff) | |
| 781 | ||
| 782 | lemma inter_mset_empty_distrib_left: "(A + B) \<inter># C = {#} \<longleftrightarrow> A \<inter># C = {#} \<and> B \<inter># C = {#}"
 | |
| 783 | by (meson disjunct_not_in union_iff) | |
| 784 | ||
| 73393 | 785 | 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 | 786 | "add_mset a A \<inter># add_mset a B = add_mset a (A \<inter># B)" | 
| 73393 | 787 | by (rule multiset_eqI) simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 788 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 789 | lemma add_mset_disjoint [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 790 |   "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 | 791 |   "{#} = 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 | 792 | by (auto simp: disjunct_not_in) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 793 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 794 | lemma disjoint_add_mset [simp]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 795 |   "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 | 796 |   "{#} = 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 | 797 | by (auto simp: disjunct_not_in) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 798 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 799 | 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 | 800 | 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 | 801 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 802 | 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 | 803 | 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 | 804 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 805 | 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 | 806 | 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 | 807 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 808 | 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 | 809 | 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 | 810 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 811 | lemma disjunct_set_mset_diff: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 812 |   assumes "M \<inter># N = {#}"
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 813 | shows "set_mset (M - N) = set_mset M" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 814 | proof (rule set_eqI) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 815 | fix a | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 816 | 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 | 817 | by (simp add: disjunct_not_in) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 818 | 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 | 819 | 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 | 820 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 821 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 822 | lemma at_most_one_mset_mset_diff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 823 |   assumes "a \<notin># M - {#a#}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 824 |   shows "set_mset (M - {#a#}) = set_mset M - {a}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 825 | 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 | 826 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 827 | lemma more_than_one_mset_mset_diff: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 828 |   assumes "a \<in># M - {#a#}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 829 |   shows "set_mset (M - {#a#}) = set_mset M"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 830 | proof (rule set_eqI) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 831 | fix b | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 832 | 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 | 833 |   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 | 834 | using assms by (auto simp add: in_diff_count) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 835 | qed | 
| 
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_iff: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 838 | "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 | 839 | by simp | 
| 
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_union_distrib_left: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 842 | "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 | 843 | 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 | 844 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 845 | lemma inter_union_distrib_right: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 846 | "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 | 847 | 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 | 848 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 849 | lemma inter_subset_eq_union: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 850 | "A \<inter># B \<subseteq># A + B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 851 | 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 | 852 | |
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 853 | |
| 64076 | 854 | subsubsection \<open>Additional bounded union facts\<close> | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 855 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 856 | lemma set_mset_sup [simp]: | 
| 73393 | 857 | \<open>set_mset (A \<union># B) = set_mset A \<union> set_mset B\<close> | 
| 858 | by (simp only: set_mset_def) (auto simp add: less_max_iff_disj) | |
| 62430 
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_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 | 861 | by (simp add: multiset_eq_iff not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 862 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 863 | lemma sup_union_left2: "x \<in># N \<Longrightarrow> (add_mset x M) \<union># N = add_mset x (M \<union># (N - {#x#}))"
 | 
| 51623 | 864 | by (simp add: multiset_eq_iff) | 
| 865 | ||
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 866 | 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 | 867 | by (simp add: multiset_eq_iff not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 868 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 869 | lemma sup_union_right2: "x \<in># N \<Longrightarrow> N \<union># (add_mset x M) = add_mset x ((N - {#x#}) \<union># M)"
 | 
| 51623 | 870 | by (simp add: multiset_eq_iff) | 
| 871 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 872 | lemma sup_union_distrib_left: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 873 | "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 | 874 | 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 | 875 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 876 | lemma union_sup_distrib_right: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 877 | "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 | 878 | 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 | 879 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 880 | lemma union_diff_inter_eq_sup: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 881 | "A + B - A \<inter># B = A \<union># B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 882 | by (auto simp add: multiset_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 883 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 884 | lemma union_diff_sup_eq_inter: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 885 | "A + B - A \<union># B = A \<inter># B" | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 886 | by (auto simp add: multiset_eq_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 887 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 888 | lemma add_mset_union: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 889 | \<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 | 890 | by (auto simp: multiset_eq_iff max_def) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 891 | |
| 51623 | 892 | |
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 893 | subsection \<open>Replicate and repeat operations\<close> | 
| 
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 | 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 | 896 |   "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 | 897 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 898 | 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 | 899 | unfolding replicate_mset_def by simp | 
| 
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 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 | 902 | 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 | 903 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 904 | 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 | 905 | 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 | 906 | |
| 73393 | 907 | lift_definition repeat_mset :: \<open>nat \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset\<close> | 
| 908 | is \<open>\<lambda>n M a. n * M a\<close> by simp | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 909 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 910 | lemma count_repeat_mset [simp]: "count (repeat_mset i A) a = i * count A a" | 
| 73393 | 911 | by transfer rule | 
| 912 | ||
| 913 | lemma repeat_mset_0 [simp]: | |
| 914 |   \<open>repeat_mset 0 M = {#}\<close>
 | |
| 915 | by transfer simp | |
| 916 | ||
| 917 | lemma repeat_mset_Suc [simp]: | |
| 918 | \<open>repeat_mset (Suc n) M = M + repeat_mset n M\<close> | |
| 919 | by transfer simp | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 920 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 921 | 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 | 922 | 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 | 923 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 924 | 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 | 925 | 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 | 926 | |
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 927 | lemma left_add_mult_distrib_mset: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 928 | "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 | 929 | 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 | 930 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 931 | lemma repeat_mset_distrib: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 932 | "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 | 933 | 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 | 934 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 935 | lemma repeat_mset_distrib2[simp]: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 936 | "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 | 937 | 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 | 938 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 939 | lemma repeat_mset_replicate_mset[simp]: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 940 |   "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 | 941 | by (auto simp: multiset_eq_iff) | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 942 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 943 | lemma repeat_mset_distrib_add_mset[simp]: | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 944 | "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 | 945 | by (auto simp: multiset_eq_iff) | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 946 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 947 | lemma repeat_mset_empty[simp]: "repeat_mset n {#} = {#}"
 | 
| 73393 | 948 | by transfer simp | 
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 949 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 950 | |
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 951 | subsubsection \<open>Simprocs\<close> | 
| 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 952 | |
| 65031 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 953 | 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 | 954 | 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 | 955 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 956 | lemma mset_subseteq_add_iff1: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 957 | "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 | 958 | 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 | 959 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 960 | lemma mset_subseteq_add_iff2: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 961 | "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 | 962 | 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 | 963 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 964 | lemma mset_subset_add_iff1: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 965 | "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 | 966 | unfolding subset_mset_def repeat_mset_iterate_add | 
| 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 967 | 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 | 968 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 969 | lemma mset_subset_add_iff2: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 970 | "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 | 971 | unfolding subset_mset_def repeat_mset_iterate_add | 
| 
52e2c99f3711
use the cancellation simprocs directly
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65029diff
changeset | 972 | 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 | 973 | |
| 69605 | 974 | ML_file \<open>multiset_simprocs.ML\<close> | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 975 | |
| 65029 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 976 | 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 | 977 | by simp | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 978 | |
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 979 | 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 | 980 | |
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 981 | declare iterate_add_distrib[cancelation_simproc_pre] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 982 | 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 | 983 | |
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 984 | 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 | 985 | 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 | 986 | 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 | 987 | 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 | 988 | 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 | 989 | 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 | 990 | le_zero_eq[cancelation_simproc_eq_elim] | 
| 
00731700e54f
cancellation simprocs generalising the multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
65027diff
changeset | 991 | |
| 65027 
2b8583507891
renaming multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
64911diff
changeset | 992 | simproc_setup mseteq_cancel | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 993 |   ("(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 | 994 | "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 | 995 | "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 | 996 | "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 | 997 | \<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 | 998 | |
| 65027 
2b8583507891
renaming multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
64911diff
changeset | 999 | simproc_setup msetsubset_cancel | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1000 |   ("(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 | 1001 | "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 | 1002 | "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 | 1003 | "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 | 1004 | \<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 | 1005 | |
| 65027 
2b8583507891
renaming multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
64911diff
changeset | 1006 | simproc_setup msetsubset_eq_cancel | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1007 |   ("(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 | 1008 | "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 | 1009 | "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 | 1010 | "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 | 1011 | \<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 | 1012 | |
| 65027 
2b8583507891
renaming multiset simprocs
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
64911diff
changeset | 1013 | simproc_setup msetdiff_cancel | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1014 |   ("((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 | 1015 | "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 | 1016 | "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 | 1017 | "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 | 1018 | \<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 | 1019 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1020 | |
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1021 | subsubsection \<open>Conditionally complete lattice\<close> | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1022 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1023 | instantiation multiset :: (type) Inf | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1024 | begin | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1025 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1026 | 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 | 1027 |   "\<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 | 1028 | proof - | 
| 73270 | 1029 |   fix A :: "('a \<Rightarrow> nat) set"
 | 
| 1030 |   assume *: "\<And>f. f \<in> A \<Longrightarrow> finite {x. 0 < f x}"
 | |
| 1031 |   show \<open>finite {i. 0 < (if A = {} then 0 else INF f\<in>A. f i)}\<close>
 | |
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1032 |   proof (cases "A = {}")
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1033 | case False | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1034 | then obtain f where "f \<in> A" by blast | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1035 |     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 | 1036 | by (auto intro: less_le_trans[OF _ cInf_lower]) | 
| 73270 | 1037 | moreover from \<open>f \<in> A\<close> * have "finite \<dots>" by simp | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1038 |     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 | 1039 | with False show ?thesis by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1040 | qed simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1041 | qed | 
| 
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 | instance .. | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1044 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1045 | end | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1046 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1047 | lemma Inf_multiset_empty: "Inf {} = {#}"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1048 | by transfer simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1049 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1050 | 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 | 1051 | by transfer simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1052 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1053 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1054 | instantiation multiset :: (type) Sup | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1055 | begin | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1056 | |
| 63360 | 1057 | definition Sup_multiset :: "'a multiset set \<Rightarrow> 'a multiset" where | 
| 1058 |   "Sup_multiset A = (if A \<noteq> {} \<and> subset_mset.bdd_above A then
 | |
| 1059 |            Abs_multiset (\<lambda>i. Sup ((\<lambda>X. count X i) ` A)) else {#})"
 | |
| 1060 | ||
| 1061 | lemma Sup_multiset_empty: "Sup {} = {#}"
 | |
| 1062 | by (simp add: Sup_multiset_def) | |
| 1063 | ||
| 73451 | 1064 | lemma Sup_multiset_unbounded: "\<not> subset_mset.bdd_above A \<Longrightarrow> Sup A = {#}"
 | 
| 63360 | 1065 | by (simp add: Sup_multiset_def) | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1066 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1067 | instance .. | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1068 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1069 | end | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1070 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1071 | lemma bdd_above_multiset_imp_bdd_above_count: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1072 | assumes "subset_mset.bdd_above (A :: 'a multiset set)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1073 | shows "bdd_above ((\<lambda>X. count X x) ` A)" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1074 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1075 | from assms obtain Y where Y: "\<forall>X\<in>A. X \<subseteq># Y" | 
| 73451 | 1076 | by (meson subset_mset.bdd_above.E) | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1077 | 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 | 1078 | using that by (auto intro: mset_subset_eq_count) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1079 | 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 | 1080 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1081 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1082 | lemma bdd_above_multiset_imp_finite_support: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1083 |   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 | 1084 |   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 | 1085 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1086 | from assms obtain Y where Y: "\<forall>X\<in>A. X \<subseteq># Y" | 
| 73451 | 1087 | by (meson subset_mset.bdd_above.E) | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1088 | 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 | 1089 | using that by (auto intro: mset_subset_eq_count) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1090 |   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 | 1091 | by safe (erule less_le_trans) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1092 | moreover have "finite \<dots>" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1093 | ultimately show ?thesis by (rule finite_subset) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1094 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1095 | |
| 63360 | 1096 | lemma Sup_multiset_in_multiset: | 
| 73270 | 1097 |   \<open>finite {i. 0 < (SUP M\<in>A. count M i)}\<close>
 | 
| 1098 |   if \<open>A \<noteq> {}\<close> \<open>subset_mset.bdd_above A\<close>
 | |
| 1099 | proof - | |
| 63360 | 1100 |   have "{i. Sup ((\<lambda>X. count X i) ` A) > 0} \<subseteq> (\<Union>X\<in>A. {i. 0 < count X i})"
 | 
| 1101 | proof safe | |
| 69260 
0a9688695a1b
removed relics of ASCII syntax for indexed big operators
 haftmann parents: 
69107diff
changeset | 1102 | fix i assume pos: "(SUP X\<in>A. count X i) > 0" | 
| 63360 | 1103 |     show "i \<in> (\<Union>X\<in>A. {i. 0 < count X i})"
 | 
| 1104 | proof (rule ccontr) | |
| 1105 |       assume "i \<notin> (\<Union>X\<in>A. {i. 0 < count X i})"
 | |
| 1106 | hence "\<forall>X\<in>A. count X i \<le> 0" by (auto simp: count_eq_zero_iff) | |
| 73270 | 1107 | with that have "(SUP X\<in>A. count X i) \<le> 0" | 
| 63360 | 1108 | by (intro cSup_least bdd_above_multiset_imp_bdd_above_count) auto | 
| 1109 | with pos show False by simp | |
| 1110 | qed | |
| 1111 | qed | |
| 73270 | 1112 | moreover from that have "finite \<dots>" | 
| 1113 | by (rule bdd_above_multiset_imp_finite_support) | |
| 1114 |   ultimately show "finite {i. Sup ((\<lambda>X. count X i) ` A) > 0}"
 | |
| 1115 | by (rule finite_subset) | |
| 63360 | 1116 | qed | 
| 1117 | ||
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1118 | lemma count_Sup_multiset_nonempty: | 
| 73270 | 1119 | \<open>count (Sup A) x = (SUP X\<in>A. count X x)\<close> | 
| 1120 |   if \<open>A \<noteq> {}\<close> \<open>subset_mset.bdd_above A\<close>
 | |
| 1121 | using that by (simp add: Sup_multiset_def Sup_multiset_in_multiset count_Abs_multiset) | |
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1122 | |
| 67398 | 1123 | interpretation subset_mset: conditionally_complete_lattice Inf Sup "(\<inter>#)" "(\<subseteq>#)" "(\<subset>#)" "(\<union>#)" | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1124 | proof | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1125 | fix X :: "'a multiset" and A | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1126 | assume "X \<in> A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1127 | show "Inf A \<subseteq># X" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1128 | proof (rule mset_subset_eqI) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1129 | fix x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1130 |     from \<open>X \<in> A\<close> have "A \<noteq> {}" by auto
 | 
| 69260 
0a9688695a1b
removed relics of ASCII syntax for indexed big operators
 haftmann parents: 
69107diff
changeset | 1131 | hence "count (Inf A) x = (INF X\<in>A. count X x)" | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1132 | by (simp add: count_Inf_multiset_nonempty) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1133 | 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 | 1134 | by (intro cInf_lower) simp_all | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1135 | 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 | 1136 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1137 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1138 | fix X :: "'a multiset" and A | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1139 |   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 | 1140 | show "X \<subseteq># Inf A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1141 | proof (rule mset_subset_eqI) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1142 | fix x | 
| 69260 
0a9688695a1b
removed relics of ASCII syntax for indexed big operators
 haftmann parents: 
69107diff
changeset | 1143 | from nonempty have "count X x \<le> (INF X\<in>A. count X x)" | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1144 | 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 | 1145 | 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 | 1146 | 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 | 1147 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1148 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1149 | fix X :: "'a multiset" and A | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1150 | 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 | 1151 | show "X \<subseteq># Sup A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1152 | proof (rule mset_subset_eqI) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1153 | fix x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1154 |     from X have "A \<noteq> {}" by auto
 | 
| 69260 
0a9688695a1b
removed relics of ASCII syntax for indexed big operators
 haftmann parents: 
69107diff
changeset | 1155 | have "count X x \<le> (SUP X\<in>A. count X x)" | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1156 | 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 | 1157 |     also from count_Sup_multiset_nonempty[OF \<open>A \<noteq> {}\<close> bdd]
 | 
| 69260 
0a9688695a1b
removed relics of ASCII syntax for indexed big operators
 haftmann parents: 
69107diff
changeset | 1158 | have "(SUP X\<in>A. count X x) = count (Sup A) x" by simp | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1159 | 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 | 1160 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1161 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1162 | fix X :: "'a multiset" and A | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1163 |   assume nonempty: "A \<noteq> {}" and ge: "\<And>Y. Y \<in> A \<Longrightarrow> Y \<subseteq># X"
 | 
| 73451 | 1164 | from ge have bdd: "subset_mset.bdd_above A" | 
| 1165 | by blast | |
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1166 | show "Sup A \<subseteq># X" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1167 | proof (rule mset_subset_eqI) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1168 | fix x | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1169 |     from count_Sup_multiset_nonempty[OF \<open>A \<noteq> {}\<close> bdd]
 | 
| 69260 
0a9688695a1b
removed relics of ASCII syntax for indexed big operators
 haftmann parents: 
69107diff
changeset | 1170 | have "count (Sup A) x = (SUP X\<in>A. count X x)" . | 
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1171 | also from nonempty have "\<dots> \<le> count X x" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1172 | 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 | 1173 | 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 | 1174 | qed | 
| 64585 
2155c0c1ecb6
renewed and spread FIXME tags on watering bin interpretation, which got partially lost in 9f089287687b
 haftmann parents: 
64531diff
changeset | 1175 | 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 | 1176 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1177 | lemma set_mset_Inf: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1178 |   assumes "A \<noteq> {}"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1179 | 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 | 1180 | proof safe | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1181 | 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 | 1182 |   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 | 1183 |   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 | 1184 | 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 | 1185 | finally show "x \<in># X" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1186 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1187 | 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 | 1188 |   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 | 1189 |   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 | 1190 | thus "x \<in># Inf A" by simp | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1191 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1192 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1193 | lemma in_Inf_multiset_iff: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1194 |   assumes "A \<noteq> {}"
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1195 | 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 | 1196 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1197 | 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 | 1198 | 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 | 1199 | finally show ?thesis . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1200 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1201 | |
| 63360 | 1202 | lemma in_Inf_multisetD: "x \<in># Inf A \<Longrightarrow> X \<in> A \<Longrightarrow> x \<in># X" | 
| 1203 | by (subst (asm) in_Inf_multiset_iff) auto | |
| 1204 | ||
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1205 | lemma set_mset_Sup: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1206 | assumes "subset_mset.bdd_above A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1207 | 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 | 1208 | proof safe | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1209 | fix x assume "x \<in># Sup A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1210 |   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 | 1211 | 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 | 1212 | proof (rule ccontr) | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1213 | 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 | 1214 | 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 | 1215 | 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 | 1216 |     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 | 1217 | 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 | 1218 |     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 | 1219 | with \<open>x \<in># Sup A\<close> show False | 
| 68406 | 1220 | by (auto simp: subseteq_mset_def simp flip: count_greater_zero_iff | 
| 1221 | dest!: spec[of _ x]) | |
| 63358 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1222 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1223 | next | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1224 | 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 | 1225 |   hence "{#x#} \<subseteq># X" by auto
 | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1226 | 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 | 1227 | 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 | 1228 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1229 | |
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1230 | lemma in_Sup_multiset_iff: | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1231 | assumes "subset_mset.bdd_above A" | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1232 | 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 | 1233 | proof - | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1234 | 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 | 1235 | 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 | 1236 | finally show ?thesis . | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1237 | qed | 
| 
a500677d4cec
Conditionally complete lattice of multisets
 Manuel Eberl <eberlm@in.tum.de> parents: 
63310diff
changeset | 1238 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1239 | lemma in_Sup_multisetD: | 
| 63360 | 1240 | assumes "x \<in># Sup A" | 
| 1241 | shows "\<exists>X\<in>A. x \<in># X" | |
| 1242 | proof - | |
| 1243 | have "subset_mset.bdd_above A" | |
| 1244 | by (rule ccontr) (insert assms, simp_all add: Sup_multiset_unbounded) | |
| 1245 | 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 | 1246 | qed | 
| 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 1247 | |
| 67398 | 1248 | interpretation subset_mset: distrib_lattice "(\<inter>#)" "(\<subseteq>#)" "(\<subset>#)" "(\<union>#)" | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 1249 | proof | 
| 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 1250 | fix A B C :: "'a multiset" | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 1251 | 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 | 1252 | 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 | 1253 | qed \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close> | 
| 63360 | 1254 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1255 | |
| 60500 | 1256 | subsubsection \<open>Filter (with comprehension syntax)\<close> | 
| 1257 | ||
| 1258 | text \<open>Multiset comprehension\<close> | |
| 41069 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 1259 | |
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1260 | 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 | 1261 | 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 | 1262 | by (rule filter_preserves_multiset) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1263 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1264 | syntax (ASCII) | 
| 63689 | 1265 |   "_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 | 1266 | syntax | 
| 63689 | 1267 |   "_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 | 1268 | translations | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1269 |   "{#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 | 1270 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1271 | lemma count_filter_mset [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1272 | "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 | 1273 | by (simp add: filter_mset.rep_eq) | 
| 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1274 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1275 | lemma set_mset_filter [simp]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1276 |   "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 | 1277 | 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 | 1278 | |
| 60606 | 1279 | lemma filter_empty_mset [simp]: "filter_mset P {#} = {#}"
 | 
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1280 | by (rule multiset_eqI) simp | 
| 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1281 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1282 | 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 | 1283 | by (rule multiset_eqI) simp | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1284 | |
| 60606 | 1285 | 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 | 1286 | by (rule multiset_eqI) simp | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 1287 | |
| 60606 | 1288 | 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 | 1289 | by (rule multiset_eqI) simp | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 1290 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 1291 | 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 | 1292 | by (rule multiset_eqI) simp | 
| 
6fabc0414055
name filter operation just filter (c.f. List.filter and list comprehension syntax)
 haftmann parents: 
40968diff
changeset | 1293 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 1294 | 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 | 1295 | by (rule multiset_eqI) simp | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1296 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1297 | lemma filter_mset_add_mset [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1298 | "filter_mset P (add_mset x A) = | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1299 | (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 | 1300 | by (auto simp: multiset_eq_iff) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1301 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1302 | 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 | 1303 | by (simp add: mset_subset_eqI) | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 1304 | |
| 60606 | 1305 | lemma multiset_filter_mono: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1306 | assumes "A \<subseteq># B" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1307 | shows "filter_mset f A \<subseteq># filter_mset f B" | 
| 58035 | 1308 | proof - | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1309 | from assms[unfolded mset_subset_eq_exists_conv] | 
| 58035 | 1310 | obtain C where B: "B = A + C" by auto | 
| 1311 | show ?thesis unfolding B by auto | |
| 1312 | qed | |
| 1313 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1314 | lemma filter_mset_eq_conv: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1315 | "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 | 1316 | proof | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1317 | 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 | 1318 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1319 | assume ?Q | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1320 | then obtain Q where M: "M = N + Q" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1321 | 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 | 1322 | then have MN: "M - N = Q" by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1323 | show ?P | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1324 | proof (rule multiset_eqI) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1325 | fix a | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1326 | 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 | 1327 | by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1328 | 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 | 1329 | proof (cases "a \<in># M") | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1330 | case True | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1331 | with * show ?thesis | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1332 | by (simp add: not_in_iff M) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1333 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1334 | case False then have "count M a = 0" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1335 | by (simp add: not_in_iff) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1336 | with M show ?thesis by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1337 | qed | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1338 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1339 | qed | 
| 59813 | 1340 | |
| 64077 | 1341 | lemma filter_filter_mset: "filter_mset P (filter_mset Q M) = {#x \<in># M. Q x \<and> P x#}"
 | 
| 1342 | by (auto simp: multiset_eq_iff) | |
| 1343 | ||
| 64418 | 1344 | lemma | 
| 1345 |   filter_mset_True[simp]: "{#y \<in># M. True#} = M" and
 | |
| 1346 |   filter_mset_False[simp]: "{#y \<in># M. False#} = {#}"
 | |
| 1347 | by (auto simp: multiset_eq_iff) | |
| 1348 | ||
| 75457 | 1349 | lemma filter_mset_cong0: | 
| 1350 | assumes "\<And>x. x \<in># M \<Longrightarrow> f x \<longleftrightarrow> g x" | |
| 1351 | shows "filter_mset f M = filter_mset g M" | |
| 1352 | proof (rule subset_mset.antisym; unfold subseteq_mset_def; rule allI) | |
| 1353 | fix x | |
| 1354 | show "count (filter_mset f M) x \<le> count (filter_mset g M) x" | |
| 1355 | using assms by (cases "x \<in># M") (simp_all add: not_in_iff) | |
| 1356 | next | |
| 1357 | fix x | |
| 1358 | show "count (filter_mset g M) x \<le> count (filter_mset f M) x" | |
| 1359 | using assms by (cases "x \<in># M") (simp_all add: not_in_iff) | |
| 1360 | qed | |
| 1361 | ||
| 1362 | lemma filter_mset_cong: | |
| 1363 | assumes "M = M'" and "\<And>x. x \<in># M' \<Longrightarrow> f x \<longleftrightarrow> g x" | |
| 1364 | shows "filter_mset f M = filter_mset g M'" | |
| 1365 | unfolding \<open>M = M'\<close> | |
| 1366 | using assms by (auto intro: filter_mset_cong0) | |
| 1367 | ||
| 59813 | 1368 | |
| 60500 | 1369 | subsubsection \<open>Size\<close> | 
| 10249 | 1370 | |
| 56656 | 1371 | definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))" | 
| 1372 | ||
| 1373 | lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a" | |
| 1374 | by (auto simp: wcount_def add_mult_distrib) | |
| 1375 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1376 | lemma wcount_add_mset: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1377 | "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 | 1378 | 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 | 1379 | |
| 56656 | 1380 | definition size_multiset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a multiset \<Rightarrow> nat" where
 | 
| 64267 | 1381 | "size_multiset f M = sum (wcount f M) (set_mset M)" | 
| 56656 | 1382 | |
| 1383 | lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def] | |
| 1384 | ||
| 60606 | 1385 | instantiation multiset :: (type) size | 
| 1386 | begin | |
| 1387 | ||
| 56656 | 1388 | definition size_multiset where | 
| 1389 | 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 | 1390 | instance .. | 
| 60606 | 1391 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1392 | 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 | 1393 | |
| 56656 | 1394 | lemmas size_multiset_overloaded_eq = | 
| 1395 | size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified] | |
| 1396 | ||
| 1397 | lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
 | |
| 1398 | by (simp add: size_multiset_def) | |
| 1399 | ||
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 1400 | lemma size_empty [simp]: "size {#} = 0"
 | 
| 56656 | 1401 | by (simp add: size_multiset_overloaded_def) | 
| 1402 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1403 | lemma size_multiset_single : "size_multiset f {#b#} = Suc (f b)"
 | 
| 56656 | 1404 | by (simp add: size_multiset_eq) | 
| 10249 | 1405 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1406 | lemma size_single: "size {#b#} = 1"
 | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1407 | by (simp add: size_multiset_overloaded_def size_multiset_single) | 
| 56656 | 1408 | |
| 64267 | 1409 | lemma sum_wcount_Int: | 
| 1410 | "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 | 1411 | by (induct rule: finite_induct) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1412 | (simp_all add: Int_insert_left wcount_def count_eq_zero_iff) | 
| 56656 | 1413 | |
| 1414 | lemma size_multiset_union [simp]: | |
| 1415 | "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N" | |
| 64267 | 1416 | apply (simp add: size_multiset_def sum_Un_nat sum.distrib sum_wcount_Int wcount_union) | 
| 56656 | 1417 | apply (subst Int_commute) | 
| 64267 | 1418 | apply (simp add: sum_wcount_Int) | 
| 26178 | 1419 | done | 
| 10249 | 1420 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1421 | lemma size_multiset_add_mset [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1422 | "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 | 1423 | 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 | 1424 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1425 | 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 | 1426 | 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 | 1427 | |
| 28708 
a1a436f09ec6
explicit check for pattern discipline before code translation
 haftmann parents: 
28562diff
changeset | 1428 | lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N" | 
| 56656 | 1429 | by (auto simp add: size_multiset_overloaded_def) | 
| 1430 | ||
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1431 | lemma size_multiset_eq_0_iff_empty [iff]: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1432 |   "size_multiset f M = 0 \<longleftrightarrow> M = {#}"
 | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1433 | by (auto simp add: size_multiset_eq count_eq_zero_iff) | 
| 10249 | 1434 | |
| 17161 | 1435 | lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
 | 
| 56656 | 1436 | by (auto simp add: size_multiset_overloaded_def) | 
| 26016 | 1437 | |
| 1438 | lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
 | |
| 26178 | 1439 | by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty) | 
| 10249 | 1440 | |
| 60607 | 1441 | lemma size_eq_Suc_imp_elem: "size M = Suc n \<Longrightarrow> \<exists>a. a \<in># M" | 
| 56656 | 1442 | apply (unfold size_multiset_overloaded_eq) | 
| 64267 | 1443 | apply (drule sum_SucD) | 
| 26178 | 1444 | apply auto | 
| 1445 | done | |
| 10249 | 1446 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1447 | 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 | 1448 | assumes "size M = Suc n" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1449 | 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 | 1450 | 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 | 1451 | 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 | 1452 | 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 | 1453 |   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 | 1454 | then show ?thesis by blast | 
| 23611 | 1455 | qed | 
| 15869 | 1456 | |
| 60606 | 1457 | lemma size_mset_mono: | 
| 1458 | fixes A B :: "'a multiset" | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1459 | assumes "A \<subseteq># B" | 
| 60606 | 1460 | shows "size A \<le> size B" | 
| 59949 | 1461 | proof - | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1462 | from assms[unfolded mset_subset_eq_exists_conv] | 
| 59949 | 1463 | obtain C where B: "B = A + C" by auto | 
| 60606 | 1464 | show ?thesis unfolding B by (induct C) auto | 
| 59949 | 1465 | qed | 
| 1466 | ||
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1467 | lemma size_filter_mset_lesseq[simp]: "size (filter_mset f M) \<le> size M" | 
| 59949 | 1468 | by (rule size_mset_mono[OF multiset_filter_subset]) | 
| 1469 | ||
| 1470 | lemma size_Diff_submset: | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1471 | "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 | 1472 | by (metis add_diff_cancel_left' size_union mset_subset_eq_exists_conv) | 
| 26016 | 1473 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1474 | |
| 60500 | 1475 | subsection \<open>Induction and case splits\<close> | 
| 10249 | 1476 | |
| 18258 | 1477 | theorem multiset_induct [case_names empty add, induct type: multiset]: | 
| 48009 | 1478 |   assumes empty: "P {#}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1479 | assumes add: "\<And>x M. P M \<Longrightarrow> P (add_mset x M)" | 
| 48009 | 1480 | shows "P M" | 
| 65545 | 1481 | proof (induct "size M" arbitrary: M) | 
| 48009 | 1482 | case 0 thus "P M" by (simp add: empty) | 
| 1483 | next | |
| 1484 | case (Suc k) | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1485 | obtain N x where "M = add_mset x N" | 
| 60500 | 1486 | using \<open>Suc k = size M\<close> [symmetric] | 
| 48009 | 1487 | using size_eq_Suc_imp_eq_union by fast | 
| 1488 | with Suc add show "P M" by simp | |
| 10249 | 1489 | qed | 
| 1490 | ||
| 65545 | 1491 | lemma multiset_induct_min[case_names empty add]: | 
| 1492 | fixes M :: "'a::linorder multiset" | |
| 1493 | assumes | |
| 1494 |     empty: "P {#}" and
 | |
| 1495 | add: "\<And>x M. P M \<Longrightarrow> (\<forall>y \<in># M. y \<ge> x) \<Longrightarrow> P (add_mset x M)" | |
| 1496 | shows "P M" | |
| 1497 | proof (induct "size M" arbitrary: M) | |
| 1498 | case (Suc k) | |
| 1499 | note ih = this(1) and Sk_eq_sz_M = this(2) | |
| 1500 | ||
| 66425 | 1501 | let ?y = "Min_mset M" | 
| 65545 | 1502 |   let ?N = "M - {#?y#}"
 | 
| 1503 | ||
| 1504 | have M: "M = add_mset ?y ?N" | |
| 1505 | by (metis Min_in Sk_eq_sz_M finite_set_mset insert_DiffM lessI not_less_zero | |
| 1506 | set_mset_eq_empty_iff size_empty) | |
| 1507 | show ?case | |
| 1508 | by (subst M, rule add, rule ih, metis M Sk_eq_sz_M nat.inject size_add_mset, | |
| 1509 | meson Min_le finite_set_mset in_diffD) | |
| 1510 | qed (simp add: empty) | |
| 1511 | ||
| 1512 | lemma multiset_induct_max[case_names empty add]: | |
| 1513 | fixes M :: "'a::linorder multiset" | |
| 1514 | assumes | |
| 1515 |     empty: "P {#}" and
 | |
| 1516 | add: "\<And>x M. P M \<Longrightarrow> (\<forall>y \<in># M. y \<le> x) \<Longrightarrow> P (add_mset x M)" | |
| 1517 | shows "P M" | |
| 1518 | proof (induct "size M" arbitrary: M) | |
| 1519 | case (Suc k) | |
| 1520 | note ih = this(1) and Sk_eq_sz_M = this(2) | |
| 1521 | ||
| 66425 | 1522 | let ?y = "Max_mset M" | 
| 65545 | 1523 |   let ?N = "M - {#?y#}"
 | 
| 1524 | ||
| 1525 | have M: "M = add_mset ?y ?N" | |
| 1526 | by (metis Max_in Sk_eq_sz_M finite_set_mset insert_DiffM lessI not_less_zero | |
| 1527 | set_mset_eq_empty_iff size_empty) | |
| 1528 | show ?case | |
| 1529 | by (subst M, rule add, rule ih, metis M Sk_eq_sz_M nat.inject size_add_mset, | |
| 1530 | meson Max_ge finite_set_mset in_diffD) | |
| 1531 | qed (simp add: empty) | |
| 1532 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1533 | lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = add_mset a A"
 | 
| 26178 | 1534 | by (induct M) auto | 
| 25610 | 1535 | |
| 55913 | 1536 | lemma multiset_cases [cases type]: | 
| 1537 |   obtains (empty) "M = {#}"
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1538 | | (add) x N where "M = add_mset x N" | 
| 63092 | 1539 | by (induct M) simp_all | 
| 25610 | 1540 | |
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1541 | 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 | 1542 | 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 | 1543 | |
| 68992 | 1544 | lemma union_filter_mset_complement[simp]: | 
| 1545 | "\<forall>x. P x = (\<not> Q x) \<Longrightarrow> filter_mset P M + filter_mset Q M = M" | |
| 1546 | by (subst multiset_eq_iff) auto | |
| 1547 | ||
| 66494 | 1548 | lemma multiset_partition: "M = {#x \<in># M. P x#} + {#x \<in># M. \<not> P x#}"
 | 
| 68992 | 1549 | by simp | 
| 66494 | 1550 | |
| 1551 | lemma mset_subset_size: "A \<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 | 1552 | proof (induct A arbitrary: B) | 
| 66494 | 1553 | case empty | 
| 1554 | then show ?case | |
| 1555 | using nonempty_has_size 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 | 1556 | next | 
| 66494 | 1557 | case (add x A) | 
| 1558 | have "add_mset x A \<subseteq># B" | |
| 1559 | by (meson add.prems subset_mset_def) | |
| 1560 | then show ?case | |
| 1561 | by (metis (no_types) add.prems add.right_neutral add_diff_cancel_left' leD nat_neq_iff | |
| 1562 | size_Diff_submset size_eq_0_iff_empty size_mset_mono subset_mset.le_iff_add subset_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 | 1563 | 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 | 1564 | |
| 59949 | 1565 | lemma size_1_singleton_mset: "size M = 1 \<Longrightarrow> \<exists>a. M = {#a#}"
 | 
| 66494 | 1566 | by (cases M) auto | 
| 59949 | 1567 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1568 | |
| 60500 | 1569 | subsubsection \<open>Strong induction and subset induction for multisets\<close> | 
| 1570 | ||
| 1571 | text \<open>Well-foundedness of strict subset relation\<close> | |
| 58098 | 1572 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1573 | 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 | 1574 | 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 | 1575 | 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 | 1576 | 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 | 1577 | |
| 76300 | 1578 | lemma wfP_subset_mset[simp]: "wfP (\<subset>#)" | 
| 1579 | by (rule wf_subset_mset_rel[to_pred]) | |
| 1580 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned 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 | lemma full_multiset_induct [case_names less]: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1582 | 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 | 1583 | shows "P B" | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1584 | apply (rule wf_subset_mset_rel [THEN wf_induct]) | 
| 58098 | 1585 | 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 | 1586 | 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 | 1587 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1588 | 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 | 1589 | assumes "F \<subseteq># A" | 
| 60606 | 1590 |     and empty: "P {#}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1591 | and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (add_mset a F)" | 
| 60606 | 1592 | 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 | 1593 | proof - | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1594 | 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 | 1595 | 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 | 1596 | 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 | 1597 |     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 | 1598 | 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 | 1599 | fix x F | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1600 | 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 | 1601 | 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 | 1602 | proof (rule insert) | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 1603 | 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 | 1604 | 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 | 1605 | 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 | 1606 | 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 | 1607 | 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 | 1608 | qed | 
| 26145 | 1609 | |
| 17161 | 1610 | |
| 75467 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1611 | subsection \<open>Least and greatest elements\<close> | 
| 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1612 | |
| 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1613 | context begin | 
| 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1614 | |
| 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1615 | qualified lemma | 
| 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1616 | assumes | 
| 77699 
d5060a919b3f
reordered assumption and tuned proof of Multiset.bex_least_element and Multiset.bex_greatest_element
 desharna parents: 
77688diff
changeset | 1617 |     "M \<noteq> {#}" and
 | 
| 76754 
b5f4ae037fe2
used transp_on in assumptions of lemmas Multiset.bex_(least|greatest)_element
 desharna parents: 
76749diff
changeset | 1618 | "transp_on (set_mset M) R" and | 
| 77699 
d5060a919b3f
reordered assumption and tuned proof of Multiset.bex_least_element and Multiset.bex_greatest_element
 desharna parents: 
77688diff
changeset | 1619 | "totalp_on (set_mset M) R" | 
| 75467 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1620 | shows | 
| 77699 
d5060a919b3f
reordered assumption and tuned proof of Multiset.bex_least_element and Multiset.bex_greatest_element
 desharna parents: 
77688diff
changeset | 1621 | bex_least_element: "(\<exists>l \<in># M. \<forall>x \<in># M. x \<noteq> l \<longrightarrow> R l x)" and | 
| 
d5060a919b3f
reordered assumption and tuned proof of Multiset.bex_least_element and Multiset.bex_greatest_element
 desharna parents: 
77688diff
changeset | 1622 | bex_greatest_element: "(\<exists>g \<in># M. \<forall>x \<in># M. x \<noteq> g \<longrightarrow> R x g)" | 
| 75467 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1623 | using assms | 
| 77699 
d5060a919b3f
reordered assumption and tuned proof of Multiset.bex_least_element and Multiset.bex_greatest_element
 desharna parents: 
77688diff
changeset | 1624 | by (auto intro: Finite_Set.bex_least_element Finite_Set.bex_greatest_element) | 
| 75467 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1625 | |
| 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1626 | end | 
| 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1627 | |
| 
9e34819a7ca1
added lemmas Multiset.bex_{least,greatest}_element
 desharna parents: 
75459diff
changeset | 1628 | |
| 60500 | 1629 | subsection \<open>The fold combinator\<close> | 
| 48023 | 1630 | |
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 1631 | definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
 | 
| 48023 | 1632 | where | 
| 60495 | 1633 | "fold_mset f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_mset M)" | 
| 48023 | 1634 | |
| 60606 | 1635 | 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 | 1636 | by (simp add: fold_mset_def) | 
| 48023 | 1637 | |
| 1638 | context comp_fun_commute | |
| 1639 | begin | |
| 1640 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1641 | 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 | 1642 | proof - | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1643 | interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y" | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1644 | by (fact comp_fun_commute_funpow) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1645 | 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 | 1646 | by (fact comp_fun_commute_funpow) | 
| 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1647 | show ?thesis | 
| 60495 | 1648 | proof (cases "x \<in> set_mset M") | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1649 | case False | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1650 | 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 | 1651 | by (simp add: not_in_iff) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1652 | from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (add_mset x M) y) s (set_mset M) = | 
| 60495 | 1653 | Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_mset M)" | 
| 73832 | 1654 | by (auto intro!: Finite_Set.fold_cong comp_fun_commute_on_funpow) | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1655 | with False * show ?thesis | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1656 | by (simp add: fold_mset_def del: count_add_mset) | 
| 48023 | 1657 | next | 
| 49822 
0cfc1651be25
simplified construction of fold combinator on multisets;
 haftmann parents: 
49717diff
changeset | 1658 | case True | 
| 63040 | 1659 |     define N where "N = set_mset M - {x}"
 | 
| 60495 | 1660 | 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 | 1661 | 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 | 1662 | Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N" | 
| 73832 | 1663 | by (auto intro!: Finite_Set.fold_cong comp_fun_commute_on_funpow) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1664 | with * show ?thesis by (simp add: fold_mset_def del: count_add_mset) simp | 
| 48023 | 1665 | qed | 
| 1666 | qed | |
| 1667 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1668 | 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 | 1669 | by simp | 
| 48023 | 1670 | |
| 60606 | 1671 | 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 | 1672 | by (induct M) (simp_all add: fun_left_comm) | 
| 48023 | 1673 | |
| 60606 | 1674 | 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 | 1675 | by (induct M) (simp_all add: fold_mset_fun_left_comm) | 
| 48023 | 1676 | |
| 1677 | lemma fold_mset_fusion: | |
| 1678 | assumes "comp_fun_commute g" | |
| 60606 | 1679 | and *: "\<And>x y. h (g x y) = f x (h y)" | 
| 1680 | shows "h (fold_mset g w A) = fold_mset f (h w) A" | |
| 48023 | 1681 | proof - | 
| 1682 | interpret comp_fun_commute g by (fact assms) | |
| 60606 | 1683 | from * show ?thesis by (induct A) auto | 
| 48023 | 1684 | qed | 
| 1685 | ||
| 1686 | end | |
| 1687 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1688 | 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 | 1689 | proof - | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1690 | interpret comp_fun_commute add_mset | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1691 | by standard auto | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1692 | show ?thesis | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1693 | by (induction B) auto | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1694 | qed | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1695 | |
| 60500 | 1696 | text \<open> | 
| 48023 | 1697 | A note on code generation: When defining some function containing a | 
| 69593 | 1698 | subterm \<^term>\<open>fold_mset F\<close>, code generation is not automatic. When | 
| 61585 | 1699 | interpreting locale \<open>left_commutative\<close> with \<open>F\<close>, the | 
| 69593 | 1700 | would be code thms for \<^const>\<open>fold_mset\<close> become thms like | 
| 1701 |   \<^term>\<open>fold_mset F z {#} = z\<close> where \<open>F\<close> is not a pattern but
 | |
| 48023 | 1702 | contains defined symbols, i.e.\ is not a code thm. Hence a separate | 
| 61585 | 1703 | constant with its own code thms needs to be introduced for \<open>F\<close>. See the image operator below. | 
| 60500 | 1704 | \<close> | 
| 1705 | ||
| 1706 | ||
| 1707 | subsection \<open>Image\<close> | |
| 48023 | 1708 | |
| 1709 | 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 | 1710 |   "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 | 1711 | |
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1712 | lemma comp_fun_commute_mset_image: "comp_fun_commute (add_mset \<circ> f)" | 
| 66494 | 1713 | by unfold_locales (simp add: fun_eq_iff) | 
| 48023 | 1714 | |
| 1715 | lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
 | |
| 49823 | 1716 | by (simp add: image_mset_def) | 
| 48023 | 1717 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1718 | lemma image_mset_single: "image_mset f {#x#} = {#f x#}"
 | 
| 66494 | 1719 | by (simp add: comp_fun_commute.fold_mset_add_mset comp_fun_commute_mset_image image_mset_def) | 
| 48023 | 1720 | |
| 60606 | 1721 | lemma image_mset_union [simp]: "image_mset f (M + N) = image_mset f M + image_mset f N" | 
| 49823 | 1722 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1723 | interpret comp_fun_commute "add_mset \<circ> f" | 
| 49823 | 1724 | by (fact comp_fun_commute_mset_image) | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 1725 | show ?thesis by (induct N) (simp_all add: image_mset_def) | 
| 49823 | 1726 | qed | 
| 1727 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1728 | corollary image_mset_add_mset [simp]: | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1729 | "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 | 1730 | unfolding image_mset_union add_mset_add_single[of a M] by (simp add: image_mset_single) | 
| 48023 | 1731 | |
| 60606 | 1732 | lemma set_image_mset [simp]: "set_mset (image_mset f M) = image f (set_mset M)" | 
| 49823 | 1733 | by (induct M) simp_all | 
| 48040 | 1734 | |
| 60606 | 1735 | lemma size_image_mset [simp]: "size (image_mset f M) = size M" | 
| 49823 | 1736 | by (induct M) simp_all | 
| 48023 | 1737 | |
| 60606 | 1738 | lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
 | 
| 49823 | 1739 | by (cases M) auto | 
| 48023 | 1740 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1741 | lemma image_mset_If: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1742 | "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 | 1743 | 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 | 1744 | by (induction A) auto | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1745 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1746 | lemma image_mset_Diff: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1747 | assumes "B \<subseteq># A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1748 | 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 | 1749 | proof - | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1750 | 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 | 1751 | by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1752 | 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 | 1753 | by (simp add: subset_mset.diff_add) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1754 | finally show ?thesis by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1755 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1756 | |
| 73594 | 1757 | lemma count_image_mset: | 
| 1758 |   \<open>count (image_mset f A) x = (\<Sum>y\<in>f -` {x} \<inter> set_mset A. count A y)\<close>
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1759 | proof (induction A) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1760 | case empty | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1761 | then show ?case by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1762 | next | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1763 | case (add x A) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1764 | 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 | 1765 | by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1766 | ultimately show ?case | 
| 66494 | 1767 | by (auto simp: sum.distrib intro!: sum.mono_neutral_left) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1768 | qed | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 1769 | |
| 73594 | 1770 | lemma count_image_mset': | 
| 1771 | \<open>count (image_mset f X) y = (\<Sum>x | x \<in># X \<and> y = f x. count X x)\<close> | |
| 1772 | by (auto simp add: count_image_mset simp flip: singleton_conv2 simp add: Collect_conj_eq ac_simps) | |
| 1773 | ||
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 1774 | 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 | 1775 | 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 | 1776 | |
| 65048 | 1777 | lemma image_mset_subset_mono: "M \<subset># N \<Longrightarrow> image_mset f M \<subset># image_mset f N" | 
| 1778 | by (metis (no_types) Diff_eq_empty_iff_mset image_mset_Diff image_mset_is_empty_iff | |
| 1779 | image_mset_subseteq_mono subset_mset.less_le_not_le) | |
| 1780 | ||
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1781 | syntax (ASCII) | 
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1782 |   "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ :# _#})")
 | 
| 48023 | 1783 | syntax | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1784 |   "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ \<in># _#})")
 | 
| 59813 | 1785 | translations | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1786 |   "{#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 | 1787 | |
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1788 | syntax (ASCII) | 
| 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1789 |   "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ :# _./ _#})")
 | 
| 48023 | 1790 | syntax | 
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 1791 |   "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ \<in># _./ _#})")
 | 
| 59813 | 1792 | translations | 
| 60606 | 1793 |   "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
 | 
| 59813 | 1794 | |
| 60500 | 1795 | text \<open> | 
| 69593 | 1796 |   This allows to write not just filters like \<^term>\<open>{#x\<in>#M. x<c#}\<close>
 | 
| 1797 |   but also images like \<^term>\<open>{#x+x. x\<in>#M #}\<close> and @{term [source]
 | |
| 60607 | 1798 |   "{#x+x|x\<in>#M. x<c#}"}, where the latter is currently displayed as
 | 
| 69593 | 1799 |   \<^term>\<open>{#x+x|x\<in>#M. x<c#}\<close>.
 | 
| 60500 | 1800 | \<close> | 
| 48023 | 1801 | |
| 60495 | 1802 | lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M"
 | 
| 66494 | 1803 | by simp | 
| 59813 | 1804 | |
| 55467 
a5c9002bc54d
renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
 blanchet parents: 
55417diff
changeset | 1805 | functor image_mset: image_mset | 
| 48023 | 1806 | proof - | 
| 1807 | fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)" | |
| 1808 | proof | |
| 1809 | fix A | |
| 1810 | show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A" | |
| 1811 | by (induct A) simp_all | |
| 1812 | qed | |
| 1813 | show "image_mset id = id" | |
| 1814 | proof | |
| 1815 | fix A | |
| 1816 | show "image_mset id A = id A" | |
| 1817 | by (induct A) simp_all | |
| 1818 | qed | |
| 1819 | qed | |
| 1820 | ||
| 59813 | 1821 | declare | 
| 1822 | image_mset.id [simp] | |
| 1823 | image_mset.identity [simp] | |
| 1824 | ||
| 1825 | lemma image_mset_id[simp]: "image_mset id x = x" | |
| 1826 | unfolding id_def by auto | |
| 1827 | ||
| 1828 | 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#}"
 | |
| 1829 | by (induct M) auto | |
| 1830 | ||
| 1831 | lemma image_mset_cong_pair: | |
| 1832 |   "(\<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#}"
 | |
| 1833 | by (metis image_mset_cong split_cong) | |
| 49717 | 1834 | |
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 1835 | lemma image_mset_const_eq: | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 1836 |   "{#c. a \<in># M#} = replicate_mset (size M) c"
 | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 1837 | by (induct M) simp_all | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 1838 | |
| 75459 | 1839 | lemma image_mset_filter_mset_swap: | 
| 1840 | "image_mset f (filter_mset (\<lambda>x. P (f x)) M) = filter_mset P (image_mset f M)" | |
| 1841 | by (induction M rule: multiset_induct) simp_all | |
| 1842 | ||
| 48023 | 1843 | |
| 75560 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1844 | lemma image_mset_eq_plusD: | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1845 | "image_mset f A = B + C \<Longrightarrow> \<exists>B' C'. A = B' + C' \<and> B = image_mset f B' \<and> C = image_mset f C'" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1846 | proof (induction A arbitrary: B C) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1847 | case empty | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1848 | thus ?case by simp | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1849 | next | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1850 | case (add x A) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1851 | show ?case | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1852 | proof (cases "f x \<in># B") | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1853 | case True | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1854 |     with add.prems have "image_mset f A = (B - {#f x#}) + C"
 | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1855 | by (metis add_mset_remove_trivial image_mset_add_mset mset_subset_eq_single | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1856 | subset_mset.add_diff_assoc2) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1857 | thus ?thesis | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1858 | using add.IH add.prems by force | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1859 | next | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1860 | case False | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1861 |     with add.prems have "image_mset f A = B + (C - {#f x#})"
 | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1862 | by (metis diff_single_eq_union diff_union_single_conv image_mset_add_mset union_iff | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1863 | union_single_eq_member) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1864 | then show ?thesis | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1865 | using add.IH add.prems by force | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1866 | qed | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1867 | qed | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1868 | |
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1869 | lemma image_mset_eq_image_mset_plusD: | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1870 | assumes "image_mset f A = image_mset f B + C" and inj_f: "inj_on f (set_mset A \<union> set_mset B)" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1871 | shows "\<exists>C'. A = B + C' \<and> C = image_mset f C'" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1872 | using assms | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1873 | proof (induction A arbitrary: B C) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1874 | case empty | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1875 | thus ?case by simp | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1876 | next | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1877 | case (add x A) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1878 | show ?case | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1879 | proof (cases "x \<in># B") | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1880 | case True | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1881 |     with add.prems have "image_mset f A = image_mset f (B - {#x#}) + C"
 | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1882 | by (smt (verit, del_insts) add.left_commute add_cancel_right_left diff_union_cancelL | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1883 | diff_union_single_conv image_mset_union union_mset_add_mset_left | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1884 | union_mset_add_mset_right) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1885 |     with add.IH have "\<exists>M3'. A = B - {#x#} + M3' \<and> image_mset f M3' = C"
 | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1886 | by (smt (verit, del_insts) True Un_insert_left Un_insert_right add.prems(2) inj_on_insert | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1887 | insert_DiffM set_mset_add_mset_insert) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1888 | with True show ?thesis | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1889 | by auto | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1890 | next | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1891 | case False | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1892 | with add.prems(2) have "f x \<notin># image_mset f B" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1893 | by auto | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1894 |     with add.prems(1) have "image_mset f A = image_mset f B + (C - {#f x#})"
 | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1895 | by (metis (no_types, lifting) diff_union_single_conv image_eqI image_mset_Diff | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1896 | image_mset_single mset_subset_eq_single set_image_mset union_iff union_single_eq_diff | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1897 | union_single_eq_member) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1898 |     with add.prems(2) add.IH have "\<exists>M3'. A = B + M3' \<and> C - {#f x#} = image_mset f M3'"
 | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1899 | by auto | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1900 | then show ?thesis | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1901 | by (metis add.prems(1) add_diff_cancel_left' image_mset_Diff mset_subset_eq_add_left | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1902 | union_mset_add_mset_right) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1903 | qed | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1904 | qed | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1905 | |
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1906 | lemma image_mset_eq_plus_image_msetD: | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1907 | "image_mset f A = B + image_mset f C \<Longrightarrow> inj_on f (set_mset A \<union> set_mset C) \<Longrightarrow> | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1908 | \<exists>B'. A = B' + C \<and> B = image_mset f B'" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1909 | unfolding add.commute[of B] add.commute[of _ C] | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1910 | by (rule image_mset_eq_image_mset_plusD; assumption) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1911 | |
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 1912 | |
| 60500 | 1913 | 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 | 1914 | |
| 60515 | 1915 | primrec mset :: "'a list \<Rightarrow> 'a multiset" where | 
| 1916 |   "mset [] = {#}" |
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 1917 | "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 | 1918 | |
| 37107 | 1919 | lemma in_multiset_in_set: | 
| 60515 | 1920 | "x \<in># mset xs \<longleftrightarrow> x \<in> set xs" | 
| 37107 | 1921 | by (induct xs) simp_all | 
| 1922 | ||
| 60515 | 1923 | lemma count_mset: | 
| 1924 | "count (mset xs) x = length (filter (\<lambda>y. x = y) xs)" | |
| 37107 | 1925 | by (induct xs) simp_all | 
| 1926 | ||
| 60515 | 1927 | lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])"
 | 
| 59813 | 1928 | 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 | 1929 | |
| 60515 | 1930 | 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 | 1931 | 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 | 1932 | |
| 66276 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 1933 | lemma count_mset_gt_0: "x \<in> set xs \<Longrightarrow> count (mset xs) x > 0" | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 1934 | by (induction xs) auto | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 1935 | |
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 1936 | lemma count_mset_0_iff [simp]: "count (mset xs) x = 0 \<longleftrightarrow> x \<notin> set xs" | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 1937 | by (induction xs) auto | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 1938 | |
| 64077 | 1939 | lemma mset_single_iff[iff]: "mset xs = {#x#} \<longleftrightarrow> xs = [x]"
 | 
| 1940 | by (cases xs) auto | |
| 1941 | ||
| 1942 | lemma mset_single_iff_right[iff]: "{#x#} = mset xs \<longleftrightarrow> xs = [x]"
 | |
| 1943 | by (cases xs) auto | |
| 1944 | ||
| 64076 | 1945 | lemma set_mset_mset[simp]: "set_mset (mset xs) = set xs" | 
| 1946 | 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 | 1947 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1948 | 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 | 1949 | 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 | 1950 | |
| 60515 | 1951 | lemma size_mset [simp]: "size (mset xs) = length xs" | 
| 48012 | 1952 | by (induct xs) simp_all | 
| 1953 | ||
| 60606 | 1954 | 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 | 1955 | 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 | 1956 | |
| 68988 | 1957 | lemma mset_filter[simp]: "mset (filter P xs) = {#x \<in># mset xs. P x #}"
 | 
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1958 | by (induct xs) simp_all | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 1959 | |
| 60515 | 1960 | lemma mset_rev [simp]: | 
| 1961 | "mset (rev xs) = mset xs" | |
| 40950 | 1962 | by (induct xs) simp_all | 
| 1963 | ||
| 60515 | 1964 | lemma surj_mset: "surj mset" | 
| 76359 | 1965 | unfolding surj_def | 
| 1966 | proof (rule allI) | |
| 1967 | fix M | |
| 1968 | show "\<exists>xs. M = mset xs" | |
| 1969 | by (induction M) (auto intro: exI[of _ "_ # _"]) | |
| 1970 | 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 | 1971 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 1972 | lemma distinct_count_atmost_1: | 
| 60606 | 1973 | "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 | 1974 | proof (induct x) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1975 | case Nil then show ?case by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1976 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1977 | 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 | 1978 | proof | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1979 | assume ?lhs then show ?rhs using Cons by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1980 | next | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1981 | assume ?rhs then have "x \<notin> set xs" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1982 | by (simp split: if_splits) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1983 | 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 | 1984 | (if a \<in> set xs then 1 else 0))" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1985 | 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 | 1986 | ultimately show ?lhs using Cons by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1987 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1988 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1989 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1990 | lemma mset_eq_setD: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1991 | assumes "mset xs = mset ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1992 | shows "set xs = set ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1993 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1994 | 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 | 1995 | by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1996 | then show ?thesis by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 1997 | 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 | 1998 | |
| 60515 | 1999 | lemma set_eq_iff_mset_eq_distinct: | 
| 73301 | 2000 | \<open>distinct x \<Longrightarrow> distinct y \<Longrightarrow> set x = set y \<longleftrightarrow> mset x = mset y\<close> | 
| 2001 | 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 | 2002 | |
| 60515 | 2003 | lemma set_eq_iff_mset_remdups_eq: | 
| 73301 | 2004 | \<open>set x = set y \<longleftrightarrow> mset (remdups x) = mset (remdups y)\<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 | 2005 | apply (rule iffI) | 
| 60515 | 2006 | 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 | 2007 | apply (drule distinct_remdups [THEN distinct_remdups | 
| 60515 | 2008 | [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 | 2009 | 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 | 2010 | 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 | 2011 | |
| 73301 | 2012 | lemma mset_eq_imp_distinct_iff: | 
| 2013 | \<open>distinct xs \<longleftrightarrow> distinct ys\<close> if \<open>mset xs = mset ys\<close> | |
| 2014 | using that by (auto simp add: distinct_count_atmost_1 dest: mset_eq_setD) | |
| 2015 | ||
| 60607 | 2016 | lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) \<in># mset ls" | 
| 60678 | 2017 | proof (induct ls arbitrary: i) | 
| 2018 | case Nil | |
| 2019 | then show ?case by simp | |
| 2020 | next | |
| 2021 | case Cons | |
| 2022 | then show ?case by (cases i) auto | |
| 2023 | 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 | 2024 | |
| 60606 | 2025 | lemma mset_remove1[simp]: "mset (remove1 a xs) = mset xs - {#a#}"
 | 
| 60678 | 2026 | 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 | 2027 | |
| 60515 | 2028 | lemma mset_eq_length: | 
| 2029 | assumes "mset xs = mset ys" | |
| 37107 | 2030 | shows "length xs = length ys" | 
| 60515 | 2031 | using assms by (metis size_mset) | 
| 2032 | ||
| 2033 | lemma mset_eq_length_filter: | |
| 2034 | 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 | 2035 | shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)" | 
| 60515 | 2036 | 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 | 2037 | |
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 2038 | lemma fold_multiset_equiv: | 
| 73706 | 2039 | \<open>List.fold f xs = List.fold f ys\<close> | 
| 2040 | if f: \<open>\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x\<close> | |
| 2041 | and \<open>mset xs = mset ys\<close> | |
| 2042 | using f \<open>mset xs = mset ys\<close> [symmetric] proof (induction xs arbitrary: ys) | |
| 60678 | 2043 | case Nil | 
| 2044 | then show ?case by simp | |
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 2045 | next | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 2046 | case (Cons x xs) | 
| 73706 | 2047 | then have *: \<open>set ys = set (x # xs)\<close> | 
| 60678 | 2048 | by (blast dest: mset_eq_setD) | 
| 73706 | 2049 | have \<open>\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x\<close> | 
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 2050 | by (rule Cons.prems(1)) (simp_all add: *) | 
| 73706 | 2051 | moreover from * have \<open>x \<in> set ys\<close> | 
| 2052 | by simp | |
| 2053 | ultimately have \<open>List.fold f ys = List.fold f (remove1 x ys) \<circ> f x\<close> | |
| 2054 | by (fact fold_remove1_split) | |
| 2055 | moreover from Cons.prems have \<open>List.fold f xs = List.fold f (remove1 x ys)\<close> | |
| 2056 | by (auto intro: Cons.IH) | |
| 2057 | ultimately show ?case | |
| 60678 | 2058 | by simp | 
| 73706 | 2059 | qed | 
| 2060 | ||
| 2061 | lemma fold_permuted_eq: | |
| 2062 | \<open>List.fold (\<odot>) xs z = List.fold (\<odot>) ys z\<close> | |
| 2063 | if \<open>mset xs = mset ys\<close> | |
| 2064 | and \<open>P z\<close> and P: \<open>\<And>x z. x \<in> set xs \<Longrightarrow> P z \<Longrightarrow> P (x \<odot> z)\<close> | |
| 2065 | and f: \<open>\<And>x y z. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> P z \<Longrightarrow> x \<odot> (y \<odot> z) = y \<odot> (x \<odot> z)\<close> | |
| 2066 | for f (infixl \<open>\<odot>\<close> 70) | |
| 2067 | using \<open>P z\<close> P f \<open>mset xs = mset ys\<close> [symmetric] proof (induction xs arbitrary: ys z) | |
| 2068 | case Nil | |
| 2069 | then show ?case by simp | |
| 2070 | next | |
| 2071 | case (Cons x xs) | |
| 2072 | then have *: \<open>set ys = set (x # xs)\<close> | |
| 2073 | by (blast dest: mset_eq_setD) | |
| 2074 | have \<open>P z\<close> | |
| 2075 | by (fact Cons.prems(1)) | |
| 2076 | moreover have \<open>\<And>x z. x \<in> set ys \<Longrightarrow> P z \<Longrightarrow> P (x \<odot> z)\<close> | |
| 2077 | by (rule Cons.prems(2)) (simp_all add: *) | |
| 2078 | moreover have \<open>\<And>x y z. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> P z \<Longrightarrow> x \<odot> (y \<odot> z) = y \<odot> (x \<odot> z)\<close> | |
| 2079 | by (rule Cons.prems(3)) (simp_all add: *) | |
| 2080 | moreover from * have \<open>x \<in> set ys\<close> | |
| 2081 | by simp | |
| 2082 | ultimately have \<open>fold (\<odot>) ys z = fold (\<odot>) (remove1 x ys) (x \<odot> z)\<close> | |
| 2083 | by (induction ys arbitrary: z) auto | |
| 2084 | moreover from Cons.prems have \<open>fold (\<odot>) xs (x \<odot> z) = fold (\<odot>) (remove1 x ys) (x \<odot> z)\<close> | |
| 2085 | by (auto intro: Cons.IH) | |
| 2086 | ultimately show ?case | |
| 2087 | by simp | |
| 45989 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 2088 | qed | 
| 
b39256df5f8a
moved theorem requiring multisets from More_List to Multiset
 haftmann parents: 
45866diff
changeset | 2089 | |
| 69107 | 2090 | lemma mset_shuffles: "zs \<in> shuffles xs ys \<Longrightarrow> mset zs = mset xs + mset ys" | 
| 2091 | by (induction xs ys arbitrary: zs rule: shuffles.induct) auto | |
| 65350 
b149abe619f7
added shuffle product to HOL/List
 eberlm <eberlm@in.tum.de> parents: 
65048diff
changeset | 2092 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2093 | 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 | 2094 | by (induct xs) simp_all | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2095 | |
| 63524 
4ec755485732
adding mset_map to the simp rules
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63410diff
changeset | 2096 | 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 | 2097 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2098 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2099 | global_interpretation mset_set: folding add_mset "{#}"
 | 
| 73832 | 2100 |   defines mset_set = "folding_on.F add_mset {#}"
 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2101 | by standard (simp add: fun_eq_iff) | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2102 | |
| 66276 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2103 | lemma sum_multiset_singleton [simp]: "sum (\<lambda>n. {#n#}) A = mset_set A"
 | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2104 | by (induction A rule: infinite_finite_induct) auto | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2105 | |
| 60513 | 2106 | lemma count_mset_set [simp]: | 
| 2107 | "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (mset_set A) x = 1" (is "PROP ?P") | |
| 2108 | "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q") | |
| 2109 | "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 | 2110 | proof - | 
| 60606 | 2111 | have *: "count (mset_set A) x = 0" if "x \<notin> A" for A | 
| 2112 | proof (cases "finite A") | |
| 2113 | case False then show ?thesis by simp | |
| 2114 | next | |
| 2115 | case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto | |
| 2116 | qed | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 2117 | 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 | 2118 | by (auto elim!: Set.set_insert) | 
| 69593 | 2119 | qed \<comment> \<open>TODO: maybe define \<^const>\<open>mset_set\<close> also in terms of \<^const>\<open>Abs_multiset\<close>\<close> | 
| 60513 | 2120 | |
| 2121 | lemma elem_mset_set[simp, intro]: "finite A \<Longrightarrow> x \<in># mset_set A \<longleftrightarrow> x \<in> A" | |
| 59813 | 2122 | by (induct A rule: finite_induct) simp_all | 
| 2123 | ||
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2124 | lemma mset_set_Union: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2125 |   "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 | 2126 | by (induction A rule: finite_induct) auto | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2127 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2128 | lemma filter_mset_mset_set [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2129 |   "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 | 2130 | proof (induction A rule: finite_induct) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2131 | case (insert x A) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2132 | 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 | 2133 |       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 | 2134 | by simp | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2135 |   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 | 2136 | by (rule insert.IH) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2137 | also from insert.hyps | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2138 |     have "\<dots> + mset_set (if P x then {x} else {}) =
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2139 |             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 | 2140 | by (intro mset_set_Union [symmetric]) simp_all | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2141 |   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 | 2142 | finally show ?case . | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2143 | qed simp_all | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2144 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2145 | lemma mset_set_Diff: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2146 | assumes "finite A" "B \<subseteq> A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2147 | 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 | 2148 | proof - | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2149 | 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 | 2150 | by (intro mset_set_Union) (auto dest: finite_subset) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2151 | 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 | 2152 | finally show ?thesis by simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2153 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2154 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2155 | 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 | 2156 | by (induction xs) simp_all | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2157 | |
| 66276 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2158 | lemma count_mset_set': "count (mset_set A) x = (if finite A \<and> x \<in> A then 1 else 0)" | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2159 | by auto | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2160 | |
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2161 | lemma subset_imp_msubset_mset_set: | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2162 | assumes "A \<subseteq> B" "finite B" | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2163 | shows "mset_set A \<subseteq># mset_set B" | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2164 | proof (rule mset_subset_eqI) | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2165 | fix x :: 'a | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2166 | from assms have "finite A" by (rule finite_subset) | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2167 | with assms show "count (mset_set A) x \<le> count (mset_set B) x" | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2168 | by (cases "x \<in> A"; cases "x \<in> B") auto | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2169 | qed | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2170 | |
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2171 | lemma mset_set_set_mset_msubset: "mset_set (set_mset A) \<subseteq># A" | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2172 | proof (rule mset_subset_eqI) | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2173 | fix x show "count (mset_set (set_mset A)) x \<le> count A x" | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2174 | by (cases "x \<in># A") simp_all | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2175 | qed | 
| 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2176 | |
| 73466 | 2177 | lemma mset_set_upto_eq_mset_upto: | 
| 2178 |   \<open>mset_set {..<n} = mset [0..<n]\<close>
 | |
| 2179 | by (induction n) (auto simp: ac_simps lessThan_Suc) | |
| 2180 | ||
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2181 | context linorder | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2182 | begin | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2183 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2184 | definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2185 | where | 
| 59998 
c54d36be22ef
renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
 nipkow parents: 
59986diff
changeset | 2186 | "sorted_list_of_multiset M = fold_mset insort [] M" | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2187 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2188 | lemma sorted_list_of_multiset_empty [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2189 |   "sorted_list_of_multiset {#} = []"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2190 | by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2191 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2192 | lemma sorted_list_of_multiset_singleton [simp]: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2193 |   "sorted_list_of_multiset {#x#} = [x]"
 | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2194 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2195 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2196 | show ?thesis by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2197 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2198 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2199 | lemma sorted_list_of_multiset_insert [simp]: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2200 | "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 | 2201 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2202 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2203 | show ?thesis by (simp add: sorted_list_of_multiset_def) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2204 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2205 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2206 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2207 | |
| 66494 | 2208 | lemma mset_sorted_list_of_multiset[simp]: "mset (sorted_list_of_multiset M) = M" | 
| 2209 | by (induct M) simp_all | |
| 2210 | ||
| 2211 | lemma sorted_list_of_multiset_mset[simp]: "sorted_list_of_multiset (mset xs) = sort xs" | |
| 2212 | by (induct xs) simp_all | |
| 2213 | ||
| 2214 | lemma finite_set_mset_mset_set[simp]: "finite A \<Longrightarrow> set_mset (mset_set A) = A" | |
| 2215 | by auto | |
| 60513 | 2216 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2217 | 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 | 2218 | using finite_set_mset_mset_set by fastforce | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2219 | |
| 66494 | 2220 | lemma infinite_set_mset_mset_set: "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
 | 
| 2221 | by simp | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2222 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2223 | lemma set_sorted_list_of_multiset [simp]: | 
| 60495 | 2224 | "set (sorted_list_of_multiset M) = set_mset M" | 
| 66434 
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
 nipkow parents: 
66425diff
changeset | 2225 | by (induct M) (simp_all add: set_insort_key) | 
| 60513 | 2226 | |
| 2227 | lemma sorted_list_of_mset_set [simp]: | |
| 2228 | "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 | 2229 | by (cases "finite A") (induct A rule: finite_induct, simp_all) | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2230 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2231 | 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 | 2232 | by (induction n) (simp_all add: atLeastLessThanSuc) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2233 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2234 | lemma image_mset_map_of: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2235 |   "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 | 2236 | proof (induction xs) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2237 | case (Cons x xs) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2238 |   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 | 2239 |           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 | 2240 | 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 | 2241 |   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 | 2242 | 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 | 2243 | 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 | 2244 | finally show ?case by simp | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2245 | qed simp_all | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
63092diff
changeset | 2246 | |
| 66494 | 2247 | lemma msubset_mset_set_iff[simp]: | 
| 66276 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2248 | assumes "finite A" "finite B" | 
| 66494 | 2249 | shows "mset_set A \<subseteq># mset_set B \<longleftrightarrow> A \<subseteq> B" | 
| 2250 | using assms set_mset_mono subset_imp_msubset_mset_set by fastforce | |
| 2251 | ||
| 2252 | lemma mset_set_eq_iff[simp]: | |
| 2253 | assumes "finite A" "finite B" | |
| 2254 | shows "mset_set A = mset_set B \<longleftrightarrow> A = B" | |
| 2255 | using assms by (fastforce dest: finite_set_mset_mset_set) | |
| 66276 
acc3b7dd0b21
More material on powers for HOL-Computational_Algebra/HOL-Number_Theory
 eberlm <eberlm@in.tum.de> parents: 
65547diff
changeset | 2256 | |
| 69895 
6b03a8cf092d
more formal contributors (with the help of the history);
 wenzelm parents: 
69605diff
changeset | 2257 | lemma image_mset_mset_set: \<^marker>\<open>contributor \<open>Lukas Bulwahn\<close>\<close> | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2258 | assumes "inj_on f A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2259 | 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 | 2260 | proof cases | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2261 | assume "finite A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2262 | 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 | 2263 | by (induct A) auto | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2264 | next | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2265 | assume "infinite A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2266 | 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 | 2267 | using finite_imageD by blast | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2268 | 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 | 2269 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63908diff
changeset | 2270 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2271 | |
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 2272 | subsection \<open>More properties of the replicate and repeat operations\<close> | 
| 60804 | 2273 | |
| 2274 | 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 | 2275 | unfolding replicate_mset_def by (induct n) auto | 
| 60804 | 2276 | |
| 2277 | lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})"
 | |
| 2278 | by (auto split: if_splits) | |
| 2279 | ||
| 2280 | lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n" | |
| 2281 | by (induct n, simp_all) | |
| 2282 | ||
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 2283 | 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 | 2284 | by (auto simp add: mset_subset_eqI) (metis count_replicate_mset subseteq_mset_def) | 
| 60804 | 2285 | |
| 2286 | lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x"
 | |
| 2287 | by (induct D) simp_all | |
| 2288 | ||
| 66494 | 2289 | lemma replicate_count_mset_eq_filter_eq: "replicate (count (mset xs) k) k = filter (HOL.eq k) xs" | 
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2290 | by (induct xs) auto | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2291 | |
| 66494 | 2292 | lemma replicate_mset_eq_empty_iff [simp]: "replicate_mset n a = {#} \<longleftrightarrow> n = 0"
 | 
| 62366 | 2293 | by (induct n) simp_all | 
| 2294 | ||
| 2295 | lemma replicate_mset_eq_iff: | |
| 66494 | 2296 | "replicate_mset m a = replicate_mset n b \<longleftrightarrow> m = 0 \<and> n = 0 \<or> m = n \<and> a = b" | 
| 62366 | 2297 | by (auto simp add: multiset_eq_iff) | 
| 2298 | ||
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 2299 | 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 | 2300 | by (auto simp: multiset_eq_iff) | 
| 
0dd6731060d7
delete looping simp rule
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63831diff
changeset | 2301 | |
| 63908 
ca41b6670904
support replicate_mset in multiset simproc
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63882diff
changeset | 2302 | 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 | 2303 | by (auto simp: multiset_eq_iff) | 
| 
0dd6731060d7
delete looping simp rule
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63831diff
changeset | 2304 | |
| 64077 | 2305 | lemma repeat_mset_eq_empty_iff: "repeat_mset n A = {#} \<longleftrightarrow> n = 0 \<or> A = {#}"
 | 
| 2306 | by (cases n) auto | |
| 2307 | ||
| 63924 | 2308 | lemma image_replicate_mset [simp]: | 
| 2309 | "image_mset f (replicate_mset n a) = replicate_mset n (f a)" | |
| 2310 | by (induct n) simp_all | |
| 2311 | ||
| 67051 | 2312 | lemma replicate_mset_msubseteq_iff: | 
| 2313 | "replicate_mset m a \<subseteq># replicate_mset n b \<longleftrightarrow> m = 0 \<or> a = b \<and> m \<le> n" | |
| 2314 | by (cases m) | |
| 68406 | 2315 | (auto simp: insert_subset_eq_iff simp flip: count_le_replicate_mset_subset_eq) | 
| 67051 | 2316 | |
| 2317 | lemma msubseteq_replicate_msetE: | |
| 2318 | assumes "A \<subseteq># replicate_mset n a" | |
| 2319 | obtains m where "m \<le> n" and "A = replicate_mset m a" | |
| 2320 | proof (cases "n = 0") | |
| 2321 | case True | |
| 2322 | with assms that show thesis | |
| 2323 | by simp | |
| 2324 | next | |
| 2325 | case False | |
| 2326 | from assms have "set_mset A \<subseteq> set_mset (replicate_mset n a)" | |
| 2327 | by (rule set_mset_mono) | |
| 2328 |   with False have "set_mset A \<subseteq> {a}"
 | |
| 2329 | by simp | |
| 2330 | then have "\<exists>m. A = replicate_mset m a" | |
| 2331 | proof (induction A) | |
| 2332 | case empty | |
| 2333 | then show ?case | |
| 2334 | by simp | |
| 2335 | next | |
| 2336 | case (add b A) | |
| 2337 | then obtain m where "A = replicate_mset m a" | |
| 2338 | by auto | |
| 2339 | with add.prems show ?case | |
| 2340 | by (auto intro: exI [of _ "Suc m"]) | |
| 2341 | qed | |
| 2342 | then obtain m where A: "A = replicate_mset m a" .. | |
| 2343 | with assms have "m \<le> n" | |
| 2344 | by (auto simp add: replicate_mset_msubseteq_iff) | |
| 2345 | then show thesis using A .. | |
| 2346 | qed | |
| 2347 | ||
| 60804 | 2348 | |
| 60500 | 2349 | subsection \<open>Big operators\<close> | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2350 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2351 | locale comm_monoid_mset = comm_monoid | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2352 | begin | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2353 | |
| 64075 | 2354 | interpretation comp_fun_commute f | 
| 2355 | by standard (simp add: fun_eq_iff left_commute) | |
| 2356 | ||
| 2357 | interpretation comp?: comp_fun_commute "f \<circ> g" | |
| 2358 | by (fact comp_comp_fun_commute) | |
| 2359 | ||
| 2360 | context | |
| 2361 | begin | |
| 2362 | ||
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2363 | 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 | 2364 | 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 | 2365 | |
| 
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
 haftmann parents: 
63195diff
changeset | 2366 | lemma empty [simp]: "F {#} = \<^bold>1"
 | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2367 | by (simp add: eq_fold) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2368 | |
| 60678 | 2369 | lemma singleton [simp]: "F {#x#} = x"
 | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2370 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2371 | interpret comp_fun_commute | 
| 60678 | 2372 | by standard (simp add: fun_eq_iff left_commute) | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2373 | show ?thesis by (simp add: eq_fold) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2374 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2375 | |
| 63290 
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
 haftmann parents: 
63195diff
changeset | 2376 | lemma union [simp]: "F (M + N) = F M \<^bold>* F N" | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2377 | proof - | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2378 | interpret comp_fun_commute f | 
| 60678 | 2379 | by standard (simp add: fun_eq_iff left_commute) | 
| 2380 | show ?thesis | |
| 2381 | by (induct N) (simp_all add: left_commute eq_fold) | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2382 | qed | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2383 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2384 | 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 | 2385 | 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 | 2386 | |
| 64075 | 2387 | lemma insert [simp]: | 
| 2388 | shows "F (image_mset g (add_mset x A)) = g x \<^bold>* F (image_mset g A)" | |
| 2389 | by (simp add: eq_fold) | |
| 2390 | ||
| 2391 | lemma remove: | |
| 2392 | assumes "x \<in># A" | |
| 2393 |   shows "F A = x \<^bold>* F (A - {#x#})"
 | |
| 2394 | using multi_member_split[OF assms] by auto | |
| 2395 | ||
| 2396 | lemma neutral: | |
| 2397 | "\<forall>x\<in>#A. x = \<^bold>1 \<Longrightarrow> F A = \<^bold>1" | |
| 2398 | by (induct A) simp_all | |
| 2399 | ||
| 2400 | lemma neutral_const [simp]: | |
| 2401 | "F (image_mset (\<lambda>_. \<^bold>1) A) = \<^bold>1" | |
| 2402 | by (simp add: neutral) | |
| 2403 | ||
| 2404 | private lemma F_image_mset_product: | |
| 2405 |   "F {#g x j \<^bold>* F {#g i j. i \<in># A#}. j \<in># B#} =
 | |
| 2406 |     F (image_mset (g x) B) \<^bold>* F {#F {#g i j. i \<in># A#}. j \<in># B#}"
 | |
| 2407 | by (induction B) (simp_all add: left_commute semigroup.assoc semigroup_axioms) | |
| 2408 | ||
| 68938 | 2409 | lemma swap: | 
| 64075 | 2410 | "F (image_mset (\<lambda>i. F (image_mset (g i) B)) A) = | 
| 2411 | F (image_mset (\<lambda>j. F (image_mset (\<lambda>i. g i j) A)) B)" | |
| 2412 | apply (induction A, simp) | |
| 2413 | apply (induction B, auto simp add: F_image_mset_product ac_simps) | |
| 2414 | done | |
| 2415 | ||
| 2416 | lemma distrib: "F (image_mset (\<lambda>x. g x \<^bold>* h x) A) = F (image_mset g A) \<^bold>* F (image_mset h A)" | |
| 2417 | by (induction A) (auto simp: ac_simps) | |
| 2418 | ||
| 2419 | lemma union_disjoint: | |
| 2420 |   "A \<inter># B = {#} \<Longrightarrow> F (image_mset g (A \<union># B)) = F (image_mset g A) \<^bold>* F (image_mset g B)"
 | |
| 2421 | by (induction A) (auto simp: ac_simps) | |
| 2422 | ||
| 2423 | end | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2424 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2425 | |
| 67398 | 2426 | lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute ((+) :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)" | 
| 60678 | 2427 | by standard (simp add: add_ac comp_def) | 
| 59813 | 2428 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2429 | declare comp_fun_commute.fold_mset_add_mset[OF comp_fun_commute_plus_mset, simp] | 
| 59813 | 2430 | |
| 67398 | 2431 | lemma in_mset_fold_plus_iff[iff]: "x \<in># fold_mset (+) M NN \<longleftrightarrow> x \<in># M \<or> (\<exists>N. N \<in># NN \<and> x \<in># N)" | 
| 59813 | 2432 | by (induct NN) auto | 
| 2433 | ||
| 54868 | 2434 | context comm_monoid_add | 
| 2435 | begin | |
| 2436 | ||
| 63830 | 2437 | sublocale sum_mset: comm_monoid_mset plus 0 | 
| 2438 | defines sum_mset = sum_mset.F .. | |
| 2439 | ||
| 64267 | 2440 | lemma sum_unfold_sum_mset: | 
| 2441 | "sum f A = sum_mset (image_mset f (mset_set A))" | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2442 | by (cases "finite A") (induct A rule: finite_induct, simp_all) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2443 | |
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2444 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2445 | |
| 73047 
ab9e27da0e85
HOL-Library: Changed notation for sum_mset
 Manuel Eberl <eberlm@in.tum.de> parents: 
72607diff
changeset | 2446 | notation sum_mset ("\<Sum>\<^sub>#")
 | 
| 
ab9e27da0e85
HOL-Library: Changed notation for sum_mset
 Manuel Eberl <eberlm@in.tum.de> parents: 
72607diff
changeset | 2447 | |
| 62366 | 2448 | syntax (ASCII) | 
| 63830 | 2449 |   "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3SUM _:#_. _)" [0, 51, 10] 10)
 | 
| 62366 | 2450 | syntax | 
| 63830 | 2451 |   "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
 | 
| 62366 | 2452 | translations | 
| 63830 | 2453 | "\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST sum_mset (CONST image_mset (\<lambda>i. b) A)" | 
| 59949 | 2454 | |
| 66938 | 2455 | context comm_monoid_add | 
| 2456 | begin | |
| 2457 | ||
| 2458 | lemma sum_mset_sum_list: | |
| 2459 | "sum_mset (mset xs) = sum_list xs" | |
| 2460 | by (induction xs) auto | |
| 2461 | ||
| 2462 | end | |
| 2463 | ||
| 2464 | context canonically_ordered_monoid_add | |
| 2465 | begin | |
| 2466 | ||
| 2467 | lemma sum_mset_0_iff [simp]: | |
| 2468 | "sum_mset M = 0 \<longleftrightarrow> (\<forall>x \<in> set_mset M. x = 0)" | |
| 2469 | by (induction M) auto | |
| 2470 | ||
| 2471 | end | |
| 2472 | ||
| 2473 | context ordered_comm_monoid_add | |
| 2474 | begin | |
| 2475 | ||
| 2476 | lemma sum_mset_mono: | |
| 2477 | "sum_mset (image_mset f K) \<le> sum_mset (image_mset g K)" | |
| 2478 | if "\<And>i. i \<in># K \<Longrightarrow> f i \<le> g i" | |
| 2479 | using that by (induction K) (simp_all add: add_mono) | |
| 2480 | ||
| 2481 | end | |
| 2482 | ||
| 73470 | 2483 | context cancel_comm_monoid_add | 
| 66938 | 2484 | begin | 
| 2485 | ||
| 2486 | lemma sum_mset_diff: | |
| 2487 | "sum_mset (M - N) = sum_mset M - sum_mset N" if "N \<subseteq># M" for M N :: "'a multiset" | |
| 2488 | using that by (auto simp add: subset_mset.le_iff_add) | |
| 2489 | ||
| 2490 | end | |
| 2491 | ||
| 2492 | context semiring_0 | |
| 2493 | begin | |
| 2494 | ||
| 63860 | 2495 | lemma sum_mset_distrib_left: | 
| 66938 | 2496 | "c * (\<Sum>x \<in># M. f x) = (\<Sum>x \<in># M. c * f(x))" | 
| 2497 | by (induction M) (simp_all add: algebra_simps) | |
| 63860 | 2498 | |
| 64075 | 2499 | lemma sum_mset_distrib_right: | 
| 66938 | 2500 | "(\<Sum>x \<in># M. f x) * c = (\<Sum>x \<in># M. f x * c)" | 
| 2501 | by (induction M) (simp_all add: algebra_simps) | |
| 2502 | ||
| 2503 | end | |
| 2504 | ||
| 2505 | lemma sum_mset_product: | |
| 2506 |   fixes f :: "'a::{comm_monoid_add,times} \<Rightarrow> 'b::semiring_0"
 | |
| 2507 | 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)" | |
| 68938 | 2508 | by (subst sum_mset.swap) (simp add: sum_mset_distrib_left sum_mset_distrib_right) | 
| 66938 | 2509 | |
| 2510 | context semiring_1 | |
| 2511 | begin | |
| 2512 | ||
| 2513 | lemma sum_mset_replicate_mset [simp]: | |
| 2514 | "sum_mset (replicate_mset n a) = of_nat n * a" | |
| 2515 | by (induction n) (simp_all add: algebra_simps) | |
| 2516 | ||
| 2517 | lemma sum_mset_delta: | |
| 2518 | "sum_mset (image_mset (\<lambda>x. if x = y then c else 0) A) = c * of_nat (count A y)" | |
| 2519 | by (induction A) (simp_all add: algebra_simps) | |
| 2520 | ||
| 2521 | lemma sum_mset_delta': | |
| 2522 | "sum_mset (image_mset (\<lambda>x. if y = x then c else 0) A) = c * of_nat (count A y)" | |
| 2523 | by (induction A) (simp_all add: algebra_simps) | |
| 2524 | ||
| 2525 | end | |
| 2526 | ||
| 2527 | lemma of_nat_sum_mset [simp]: | |
| 2528 | "of_nat (sum_mset A) = sum_mset (image_mset of_nat A)" | |
| 2529 | by (induction A) auto | |
| 2530 | ||
| 2531 | lemma size_eq_sum_mset: | |
| 2532 | "size M = (\<Sum>a\<in>#M. 1)" | |
| 2533 | using image_mset_const_eq [of "1::nat" M] by simp | |
| 2534 | ||
| 2535 | lemma size_mset_set [simp]: | |
| 2536 | "size (mset_set A) = card A" | |
| 2537 | by (simp only: size_eq_sum_mset card_eq_sum sum_unfold_sum_mset) | |
| 64075 | 2538 | |
| 2539 | lemma sum_mset_constant [simp]: | |
| 2540 | fixes y :: "'b::semiring_1" | |
| 2541 | shows \<open>(\<Sum>x\<in>#A. y) = of_nat (size A) * y\<close> | |
| 2542 | by (induction A) (auto simp: algebra_simps) | |
| 2543 | ||
| 73047 
ab9e27da0e85
HOL-Library: Changed notation for sum_mset
 Manuel Eberl <eberlm@in.tum.de> parents: 
72607diff
changeset | 2544 | lemma set_mset_Union_mset[simp]: "set_mset (\<Sum>\<^sub># MM) = (\<Union>M \<in> set_mset MM. set_mset M)" | 
| 59813 | 2545 | by (induct MM) auto | 
| 2546 | ||
| 73047 
ab9e27da0e85
HOL-Library: Changed notation for sum_mset
 Manuel Eberl <eberlm@in.tum.de> parents: 
72607diff
changeset | 2547 | lemma in_Union_mset_iff[iff]: "x \<in># \<Sum>\<^sub># MM \<longleftrightarrow> (\<exists>M. M \<in># MM \<and> x \<in># M)" | 
| 59813 | 2548 | by (induct MM) auto | 
| 2549 | ||
| 64267 | 2550 | lemma count_sum: | 
| 2551 | "count (sum f A) x = sum (\<lambda>a. count (f a) x) A" | |
| 62366 | 2552 | by (induct A rule: infinite_finite_induct) simp_all | 
| 2553 | ||
| 64267 | 2554 | lemma sum_eq_empty_iff: | 
| 62366 | 2555 | assumes "finite A" | 
| 64267 | 2556 |   shows "sum f A = {#} \<longleftrightarrow> (\<forall>a\<in>A. f a = {#})"
 | 
| 62366 | 2557 | using assms by induct simp_all | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2558 | |
| 73047 
ab9e27da0e85
HOL-Library: Changed notation for sum_mset
 Manuel Eberl <eberlm@in.tum.de> parents: 
72607diff
changeset | 2559 | lemma Union_mset_empty_conv[simp]: "\<Sum>\<^sub># M = {#} \<longleftrightarrow> (\<forall>i\<in>#M. i = {#})"
 | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2560 | by (induction M) auto | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 2561 | |
| 73047 
ab9e27da0e85
HOL-Library: Changed notation for sum_mset
 Manuel Eberl <eberlm@in.tum.de> parents: 
72607diff
changeset | 2562 | lemma Union_image_single_mset[simp]: "\<Sum>\<^sub># (image_mset (\<lambda>x. {#x#}) m) = m"
 | 
| 67656 | 2563 | by(induction m) auto | 
| 2564 | ||
| 66938 | 2565 | |
| 54868 | 2566 | context comm_monoid_mult | 
| 2567 | begin | |
| 2568 | ||
| 63830 | 2569 | sublocale prod_mset: comm_monoid_mset times 1 | 
| 2570 | defines prod_mset = prod_mset.F .. | |
| 2571 | ||
| 2572 | lemma prod_mset_empty: | |
| 2573 |   "prod_mset {#} = 1"
 | |
| 2574 | by (fact prod_mset.empty) | |
| 2575 | ||
| 2576 | lemma prod_mset_singleton: | |
| 2577 |   "prod_mset {#x#} = x"
 | |
| 2578 | by (fact prod_mset.singleton) | |
| 2579 | ||
| 2580 | lemma prod_mset_Un: | |
| 2581 | "prod_mset (A + B) = prod_mset A * prod_mset B" | |
| 2582 | by (fact prod_mset.union) | |
| 2583 | ||
| 66938 | 2584 | lemma prod_mset_prod_list: | 
| 2585 | "prod_mset (mset xs) = prod_list xs" | |
| 2586 | by (induct xs) auto | |
| 2587 | ||
| 63830 | 2588 | lemma prod_mset_replicate_mset [simp]: | 
| 2589 | "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 | 2590 | by (induct n) simp_all | 
| 60804 | 2591 | |
| 64272 | 2592 | lemma prod_unfold_prod_mset: | 
| 2593 | "prod f A = prod_mset (image_mset f (mset_set A))" | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2594 | by (cases "finite A") (induct A rule: finite_induct, simp_all) | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2595 | |
| 63830 | 2596 | lemma prod_mset_multiplicity: | 
| 64272 | 2597 | "prod_mset M = prod (\<lambda>x. x ^ count M x) (set_mset M)" | 
| 2598 | by (simp add: fold_mset_def prod.eq_fold prod_mset.eq_fold funpow_times_power comp_def) | |
| 63830 | 2599 | |
| 2600 | 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 | 2601 | by (induction A) simp_all | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 2602 | |
| 63830 | 2603 | 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 | 2604 | by (induction A) simp_all | 
| 63534 
523b488b15c9
Overhaul of prime/multiplicity/prime_factors
 eberlm <eberlm@in.tum.de> parents: 
63524diff
changeset | 2605 | |
| 66938 | 2606 | lemma prod_mset_subset_imp_dvd: | 
| 2607 | assumes "A \<subseteq># B" | |
| 2608 | shows "prod_mset A dvd prod_mset B" | |
| 2609 | proof - | |
| 2610 | from assms have "B = (B - A) + A" by (simp add: subset_mset.diff_add) | |
| 2611 | also have "prod_mset \<dots> = prod_mset (B - A) * prod_mset A" by simp | |
| 2612 | also have "prod_mset A dvd \<dots>" by simp | |
| 2613 | finally show ?thesis . | |
| 2614 | qed | |
| 2615 | ||
| 2616 | lemma dvd_prod_mset: | |
| 2617 | assumes "x \<in># A" | |
| 2618 | shows "x dvd prod_mset A" | |
| 2619 |   using assms prod_mset_subset_imp_dvd [of "{#x#}" A] by simp
 | |
| 2620 | ||
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2621 | end | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2622 | |
| 73052 
c03a148110cc
HOL-Library.Multiset: new notation for prod_mset, consistent with sum_mset
 Manuel Eberl <eberlm@in.tum.de> parents: 
73047diff
changeset | 2623 | notation prod_mset ("\<Prod>\<^sub>#")
 | 
| 
c03a148110cc
HOL-Library.Multiset: new notation for prod_mset, consistent with sum_mset
 Manuel Eberl <eberlm@in.tum.de> parents: 
73047diff
changeset | 2624 | |
| 61955 
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
 wenzelm parents: 
61890diff
changeset | 2625 | syntax (ASCII) | 
| 63830 | 2626 |   "_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 | 2627 | syntax | 
| 63830 | 2628 |   "_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 | 2629 | translations | 
| 63830 | 2630 | "\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST prod_mset (CONST image_mset (\<lambda>i. b) A)" | 
| 2631 | ||
| 64591 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 2632 | lemma prod_mset_constant [simp]: "(\<Prod>_\<in>#A. c) = c ^ size A" | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 2633 | by (simp add: image_mset_const_eq) | 
| 
240a39af9ec4
restructured matter on polynomials and normalized fractions
 haftmann parents: 
64587diff
changeset | 2634 | |
| 63830 | 2635 | lemma (in semidom) prod_mset_zero_iff [iff]: | 
| 2636 | "prod_mset A = 0 \<longleftrightarrow> 0 \<in># A" | |
| 62366 | 2637 | by (induct A) auto | 
| 2638 | ||
| 63830 | 2639 | lemma (in semidom_divide) prod_mset_diff: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2640 | assumes "B \<subseteq># A" and "0 \<notin># B" | 
| 63830 | 2641 | 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 | 2642 | proof - | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2643 | from assms obtain C where "A = B + C" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2644 | by (metis subset_mset.add_diff_inverse) | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2645 | with assms show ?thesis by simp | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2646 | qed | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2647 | |
| 63830 | 2648 | lemma (in semidom_divide) prod_mset_minus: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2649 | assumes "a \<in># A" and "a \<noteq> 0" | 
| 63830 | 2650 |   shows "prod_mset (A - {#a#}) = prod_mset A div a"
 | 
| 2651 |   using assms prod_mset_diff [of "{#a#}" A] by auto
 | |
| 2652 | ||
| 71398 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2653 | lemma (in normalization_semidom) normalize_prod_mset_normalize: | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2654 | "normalize (prod_mset (image_mset normalize A)) = normalize (prod_mset A)" | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2655 | proof (induction A) | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2656 | case (add x A) | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2657 | have "normalize (prod_mset (image_mset normalize (add_mset x A))) = | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2658 | normalize (x * normalize (prod_mset (image_mset normalize A)))" | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2659 | by simp | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2660 | also note add.IH | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2661 | finally show ?case by simp | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2662 | qed auto | 
| 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2663 | |
| 63924 | 2664 | lemma (in algebraic_semidom) is_unit_prod_mset_iff: | 
| 2665 | "is_unit (prod_mset A) \<longleftrightarrow> (\<forall>x \<in># A. is_unit x)" | |
| 2666 | by (induct A) (auto simp: is_unit_mult_iff) | |
| 2667 | ||
| 71398 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2668 | lemma (in normalization_semidom_multiplicative) normalize_prod_mset: | 
| 63924 | 2669 | "normalize (prod_mset A) = prod_mset (image_mset normalize A)" | 
| 2670 | by (induct A) (simp_all add: normalize_mult) | |
| 2671 | ||
| 71398 
e0237f2eb49d
Removed multiplicativity assumption from normalization_semidom
 Manuel Eberl <eberlm@in.tum.de> parents: 
69895diff
changeset | 2672 | lemma (in normalization_semidom_multiplicative) normalized_prod_msetI: | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2673 | assumes "\<And>a. a \<in># A \<Longrightarrow> normalize a = a" | 
| 63830 | 2674 | shows "normalize (prod_mset A) = prod_mset A" | 
| 63924 | 2675 | proof - | 
| 2676 | from assms have "image_mset normalize A = A" | |
| 2677 | by (induct A) simp_all | |
| 2678 | then show ?thesis by (simp add: normalize_prod_mset) | |
| 2679 | qed | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2680 | |
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2681 | |
| 73301 | 2682 | subsection \<open>Multiset as order-ignorant lists\<close> | 
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51161diff
changeset | 2683 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2684 | context linorder | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2685 | begin | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2686 | |
| 60515 | 2687 | lemma mset_insort [simp]: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2688 | "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 | 2689 | by (induct xs) simp_all | 
| 58425 | 2690 | |
| 60515 | 2691 | lemma mset_sort [simp]: | 
| 2692 | "mset (sort_key k xs) = mset xs" | |
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2693 | by (induct xs) simp_all | 
| 37107 | 2694 | |
| 60500 | 2695 | 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 | 2696 | This lemma shows which properties suffice to show that a function | 
| 61585 | 2697 | \<open>f\<close> with \<open>f xs = ys\<close> behaves like sort. | 
| 60500 | 2698 | \<close> | 
| 37074 | 2699 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2700 | lemma properties_for_sort_key: | 
| 60515 | 2701 | assumes "mset ys = mset xs" | 
| 60606 | 2702 | and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs" | 
| 2703 | 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 | 2704 | shows "sort_key f xs = ys" | 
| 60606 | 2705 | using assms | 
| 46921 | 2706 | 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 | 2707 | 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 | 2708 | 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 | 2709 | case (Cons x xs) | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2710 | from Cons.prems(2) have | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2711 | "\<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 | 2712 | by (simp add: filter_remove1) | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2713 | 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 | 2714 | 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 | 2715 | moreover from Cons.prems have "x \<in># mset ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2716 | by auto | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2717 | then have "x \<in> set ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2718 | by simp | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2719 | 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 | 2720 | 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 | 2721 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2722 | lemma properties_for_sort: | 
| 60515 | 2723 | assumes multiset: "mset ys = mset xs" | 
| 60606 | 2724 | and "sorted ys" | 
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2725 | shows "sort xs = ys" | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2726 | proof (rule properties_for_sort_key) | 
| 60515 | 2727 | from multiset show "mset ys = mset xs" . | 
| 60500 | 2728 | from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp | 
| 60678 | 2729 | from multiset have "length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" for k | 
| 60515 | 2730 | by (rule mset_eq_length_filter) | 
| 60678 | 2731 | then have "replicate (length (filter (\<lambda>y. k = y) ys)) k = | 
| 2732 | 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 | 2733 | by simp | 
| 60678 | 2734 | 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 | 2735 | 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 | 2736 | qed | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2737 | |
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2738 | lemma sort_key_inj_key_eq: | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2739 | assumes mset_equal: "mset xs = mset ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2740 | and "inj_on f (set xs)" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2741 | and "sorted (map f ys)" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2742 | shows "sort_key f xs = ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2743 | proof (rule properties_for_sort_key) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2744 | from mset_equal | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2745 | show "mset ys = mset xs" by simp | 
| 61188 | 2746 | from \<open>sorted (map f ys)\<close> | 
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2747 | show "sorted (map f ys)" . | 
| 68386 | 2748 | show "[x\<leftarrow>ys . f k = f x] = [x\<leftarrow>xs . f k = f x]" if "k \<in> set ys" for k | 
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2749 | proof - | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2750 | from mset_equal | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2751 | 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 | 2752 | with that have "insert k (set ys) = set ys" by auto | 
| 61188 | 2753 | 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 | 2754 | by (simp add: set_equal) | 
| 68386 | 2755 | from inj have "[x\<leftarrow>ys . f k = f x] = filter (HOL.eq k) ys" | 
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2756 | by (auto intro!: inj_on_filter_key_eq) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2757 | also have "\<dots> = replicate (count (mset ys) k) k" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2758 | by (simp add: replicate_count_mset_eq_filter_eq) | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2759 | also have "\<dots> = replicate (count (mset xs) k) k" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2760 | using mset_equal by simp | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2761 | also have "\<dots> = filter (HOL.eq k) xs" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2762 | by (simp add: replicate_count_mset_eq_filter_eq) | 
| 68386 | 2763 | also have "\<dots> = [x\<leftarrow>xs . f k = f x]" | 
| 61031 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2764 | 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 | 2765 | finally show ?thesis . | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2766 | qed | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2767 | qed | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2768 | |
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2769 | lemma sort_key_eq_sort_key: | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2770 | assumes "mset xs = mset ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2771 | and "inj_on f (set xs)" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2772 | shows "sort_key f xs = sort_key f ys" | 
| 
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
 haftmann parents: 
60804diff
changeset | 2773 | 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 | 2774 | |
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2775 | lemma sort_key_by_quicksort: | 
| 68386 | 2776 | "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))] | 
| 2777 | @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))] | |
| 2778 | @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs") | |
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2779 | proof (rule properties_for_sort_key) | 
| 60515 | 2780 | show "mset ?rhs = mset ?lhs" | 
| 69442 | 2781 | by (rule multiset_eqI) auto | 
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2782 | show "sorted (map f ?rhs)" | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2783 | by (auto simp add: sorted_append intro: sorted_map_same) | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2784 | next | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2785 | fix l | 
| 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2786 | assume "l \<in> set ?rhs" | 
| 40346 | 2787 | let ?pivot = "f (xs ! (length xs div 2))" | 
| 2788 | have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto | |
| 68386 | 2789 | 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 | 2790 | unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same) | 
| 68386 | 2791 | with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp | 
| 40346 | 2792 | 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 | 
| 68386 | 2793 | then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] = | 
| 2794 | [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp | |
| 67398 | 2795 | note *** = this [of "(<)"] this [of "(>)"] this [of "(=)"] | 
| 68386 | 2796 | 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 | 2797 | proof (cases "f l" ?pivot rule: linorder_cases) | 
| 46730 | 2798 | case less | 
| 2799 | then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto | |
| 2800 | with less show ?thesis | |
| 40346 | 2801 | by (simp add: filter_sort [symmetric] ** ***) | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2802 | next | 
| 40306 | 2803 | case equal then show ?thesis | 
| 40346 | 2804 | by (simp add: * less_le) | 
| 40305 
41833242cc42
tuned lemma proposition of properties_for_sort_key
 haftmann parents: 
40303diff
changeset | 2805 | next | 
| 46730 | 2806 | case greater | 
| 2807 | then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto | |
| 2808 | with greater show ?thesis | |
| 40346 | 2809 | by (simp add: filter_sort [symmetric] ** ***) | 
| 40306 | 2810 | qed | 
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2811 | qed | 
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2812 | |
| 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2813 | lemma sort_by_quicksort: | 
| 68386 | 2814 | "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)] | 
| 2815 | @ [x\<leftarrow>xs. x = xs ! (length xs div 2)] | |
| 2816 | @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs") | |
| 40303 
2d507370e879
lemmas multiset_of_filter, sort_key_by_quicksort
 haftmann parents: 
40250diff
changeset | 2817 | 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 | 2818 | |
| 68983 | 2819 | text \<open>A stable parameterized quicksort\<close> | 
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2820 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2821 | definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
 | 
| 68386 | 2822 | "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])" | 
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2823 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2824 | lemma part_code [code]: | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2825 | "part f pivot [] = ([], [], [])" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2826 | "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 | 2827 | if x' < pivot then (x # lts, eqs, gts) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2828 | else if x' > pivot then (lts, eqs, x # gts) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2829 | else (lts, x # eqs, gts))" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2830 | by (auto simp add: part_def Let_def split_def) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2831 | |
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2832 | lemma sort_key_by_quicksort_code [code]: | 
| 60606 | 2833 | "sort_key f xs = | 
| 2834 | (case xs of | |
| 2835 | [] \<Rightarrow> [] | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2836 | | [x] \<Rightarrow> xs | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2837 | | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x]) | 
| 60606 | 2838 | | _ \<Rightarrow> | 
| 2839 | let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs | |
| 2840 | in sort_key f lts @ eqs @ sort_key f gts)" | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2841 | proof (cases xs) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2842 | case Nil then show ?thesis by simp | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2843 | next | 
| 46921 | 2844 | case (Cons _ ys) note hyps = Cons show ?thesis | 
| 2845 | proof (cases ys) | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2846 | case Nil with hyps show ?thesis by simp | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2847 | next | 
| 46921 | 2848 | case (Cons _ zs) note hyps = hyps Cons show ?thesis | 
| 2849 | proof (cases zs) | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2850 | case Nil with hyps show ?thesis by auto | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2851 | next | 
| 58425 | 2852 | case Cons | 
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2853 | from sort_key_by_quicksort [of f xs] | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2854 | 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 | 2855 | in sort_key f lts @ eqs @ sort_key f gts)" | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2856 | 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 | 2857 | with hyps Cons show ?thesis by (simp only: list.cases) | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2858 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2859 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2860 | qed | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2861 | |
| 39533 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2862 | end | 
| 
91a0ff0ff237
generalized lemmas multiset_of_insort, multiset_of_sort, properties_for_sort for *_key variants
 haftmann parents: 
39314diff
changeset | 2863 | |
| 40347 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2864 | hide_const (open) part | 
| 
429bf4388b2f
added code lemmas for stable parametrized quicksort
 haftmann parents: 
40346diff
changeset | 2865 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 2866 | 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 | 2867 | 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 | 2868 | |
| 60515 | 2869 | lemma mset_update: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2870 |   "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 | 2871 | 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 | 2872 | 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 | 2873 | 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 | 2874 | 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 | 2875 | 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 | 2876 | 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 | 2877 | 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 | 2878 | 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 | 2879 | 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 | 2880 | with Cons show ?thesis | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2881 | 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 | 2882 | 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 | 2883 | 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 | 2884 | |
| 60515 | 2885 | 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 | 2886 | "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow> | 
| 60515 | 2887 | mset (ls[j := ls ! i, i := ls ! j]) = mset ls" | 
| 2888 | 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 | 2889 | |
| 73327 
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
 haftmann parents: 
73301diff
changeset | 2890 | lemma mset_eq_finite: | 
| 73301 | 2891 |   \<open>finite {ys. mset ys = mset xs}\<close>
 | 
| 2892 | proof - | |
| 2893 |   have \<open>{ys. mset ys = mset xs} \<subseteq> {ys. set ys \<subseteq> set xs \<and> length ys \<le> length xs}\<close>
 | |
| 2894 | by (auto simp add: dest: mset_eq_setD mset_eq_length) | |
| 2895 |   moreover have \<open>finite {ys. set ys \<subseteq> set xs \<and> length ys \<le> length xs}\<close>
 | |
| 2896 | using finite_lists_length_le by blast | |
| 2897 | ultimately show ?thesis | |
| 2898 | by (rule finite_subset) | |
| 2899 | qed | |
| 2900 | ||
| 34943 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 2901 | |
| 60500 | 2902 | subsection \<open>The multiset order\<close> | 
| 2903 | ||
| 60606 | 2904 | 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 | 2905 |   "mult1 r = {(N, M). \<exists>a M0 K. M = add_mset a M0 \<and> N = M0 + K \<and>
 | 
| 60607 | 2906 | (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r)}" | 
| 60606 | 2907 | |
| 2908 | definition mult :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
 | |
| 37765 | 2909 | "mult r = (mult1 r)\<^sup>+" | 
| 10249 | 2910 | |
| 74858 
c5059f9fbfba
added Multiset.multp as predicate equivalent of Multiset.mult
 desharna parents: 
74806diff
changeset | 2911 | definition multp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
 | 
| 
c5059f9fbfba
added Multiset.multp as predicate equivalent of Multiset.mult
 desharna parents: 
74806diff
changeset | 2912 |   "multp r M N \<longleftrightarrow> (M, N) \<in> mult {(x, y). r x y}"
 | 
| 
c5059f9fbfba
added Multiset.multp as predicate equivalent of Multiset.mult
 desharna parents: 
74806diff
changeset | 2913 | |
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 2914 | declare multp_def[pred_set_conv] | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 2915 | |
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2916 | lemma mult1I: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2917 | 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 | 2918 | shows "(N, M) \<in> mult1 r" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2919 | using assms unfolding mult1_def by blast | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2920 | |
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2921 | lemma mult1E: | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2922 | assumes "(N, M) \<in> mult1 r" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2923 | 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 | 2924 | using assms unfolding mult1_def by blast | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 2925 | |
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2926 | lemma mono_mult1: | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2927 | assumes "r \<subseteq> r'" shows "mult1 r \<subseteq> mult1 r'" | 
| 74858 
c5059f9fbfba
added Multiset.multp as predicate equivalent of Multiset.mult
 desharna parents: 
74806diff
changeset | 2928 | unfolding mult1_def using assms by blast | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2929 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2930 | lemma mono_mult: | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2931 | assumes "r \<subseteq> r'" shows "mult r \<subseteq> mult r'" | 
| 74858 
c5059f9fbfba
added Multiset.multp as predicate equivalent of Multiset.mult
 desharna parents: 
74806diff
changeset | 2932 | unfolding mult_def using mono_mult1[OF assms] trancl_mono by blast | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 2933 | |
| 74859 | 2934 | lemma mono_multp[mono]: "r \<le> r' \<Longrightarrow> multp r \<le> multp r'" | 
| 2935 | unfolding le_fun_def le_bool_def | |
| 2936 | proof (intro allI impI) | |
| 2937 | fix M N :: "'a multiset" | |
| 2938 | assume "\<forall>x xa. r x xa \<longrightarrow> r' x xa" | |
| 2939 |   hence "{(x, y). r x y} \<subseteq> {(x, y). r' x y}"
 | |
| 2940 | by blast | |
| 2941 | thus "multp r M N \<Longrightarrow> multp r' M N" | |
| 2942 | unfolding multp_def | |
| 2943 | by (fact mono_mult[THEN subsetD, rotated]) | |
| 2944 | qed | |
| 2945 | ||
| 23751 | 2946 | lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
 | 
| 74858 
c5059f9fbfba
added Multiset.multp as predicate equivalent of Multiset.mult
 desharna parents: 
74806diff
changeset | 2947 | by (simp add: mult1_def) | 
| 10249 | 2948 | |
| 74860 | 2949 | |
| 2950 | subsubsection \<open>Well-foundedness\<close> | |
| 2951 | ||
| 60608 | 2952 | lemma less_add: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2953 | assumes mult1: "(N, add_mset a M0) \<in> mult1 r" | 
| 60608 | 2954 | shows | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2955 | "(\<exists>M. (M, M0) \<in> mult1 r \<and> N = add_mset a M) \<or> | 
| 60608 | 2956 | (\<exists>K. (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r) \<and> N = M0 + K)" | 
| 2957 | proof - | |
| 60607 | 2958 | 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 | 2959 | 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 | 2960 | obtain a' M0' K where M0: "add_mset a M0 = add_mset a' M0'" | 
| 60608 | 2961 | and N: "N = M0' + K" | 
| 2962 | and r: "?r K a'" | |
| 2963 | using mult1 unfolding mult1_def by auto | |
| 2964 | show ?thesis (is "?case1 \<or> ?case2") | |
| 60606 | 2965 | proof - | 
| 2966 | from M0 consider "M0 = M0'" "a = a'" | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2967 | | K' where "M0 = add_mset a' K'" "M0' = add_mset a K'" | 
| 60606 | 2968 | by atomize_elim (simp only: add_eq_conv_ex) | 
| 18258 | 2969 | then show ?thesis | 
| 60606 | 2970 | proof cases | 
| 2971 | case 1 | |
| 11464 | 2972 | with N r have "?r K a \<and> N = M0 + K" by simp | 
| 60606 | 2973 | then have ?case2 .. | 
| 2974 | then show ?thesis .. | |
| 10249 | 2975 | next | 
| 60606 | 2976 | case 2 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 2977 | from N 2(2) have n: "N = add_mset a (K' + K)" by simp | 
| 60606 | 2978 | with r 2(1) have "?R (K' + K) M0" by blast | 
| 60608 | 2979 | with n have ?case1 by (simp add: mult1_def) | 
| 60606 | 2980 | then show ?thesis .. | 
| 10249 | 2981 | qed | 
| 2982 | qed | |
| 2983 | qed | |
| 2984 | ||
| 60608 | 2985 | lemma all_accessible: | 
| 2986 | assumes "wf r" | |
| 2987 | shows "\<forall>M. M \<in> Wellfounded.acc (mult1 r)" | |
| 10249 | 2988 | proof | 
| 2989 | let ?R = "mult1 r" | |
| 54295 | 2990 | let ?W = "Wellfounded.acc ?R" | 
| 10249 | 2991 |   {
 | 
| 2992 | fix M M0 a | |
| 23751 | 2993 | assume M0: "M0 \<in> ?W" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2994 | 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 | 2995 | 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 | 2996 | have "add_mset a M0 \<in> ?W" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2997 | proof (rule accI [of "add_mset a M0"]) | 
| 10249 | 2998 | fix N | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 2999 | assume "(N, add_mset a M0) \<in> ?R" | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3000 | then consider M where "(M, M0) \<in> ?R" "N = add_mset a M" | 
| 60608 | 3001 | | K where "\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" "N = M0 + K" | 
| 3002 | by atomize_elim (rule less_add) | |
| 23751 | 3003 | then show "N \<in> ?W" | 
| 60608 | 3004 | proof cases | 
| 3005 | case 1 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3006 | 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 | 3007 | 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 | 3008 | then show "N \<in> ?W" by (simp only: \<open>N = add_mset a M\<close>) | 
| 10249 | 3009 | next | 
| 60608 | 3010 | case 2 | 
| 3011 | from this(1) have "M0 + K \<in> ?W" | |
| 10249 | 3012 | proof (induct K) | 
| 18730 | 3013 | case empty | 
| 23751 | 3014 |           from M0 show "M0 + {#} \<in> ?W" by simp
 | 
| 18730 | 3015 | next | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3016 | case (add x K) | 
| 23751 | 3017 | 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 | 3018 | with wf_hyp have "\<forall>M \<in> ?W. add_mset x M \<in> ?W" by blast | 
| 23751 | 3019 | 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 | 3020 | 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 | 3021 | then show "M0 + (add_mset x K) \<in> ?W" by simp | 
| 10249 | 3022 | qed | 
| 60608 | 3023 | then show "N \<in> ?W" by (simp only: 2(2)) | 
| 10249 | 3024 | qed | 
| 3025 | qed | |
| 3026 | } note tedious_reasoning = this | |
| 3027 | ||
| 60608 | 3028 | show "M \<in> ?W" for M | 
| 10249 | 3029 | proof (induct M) | 
| 23751 | 3030 |     show "{#} \<in> ?W"
 | 
| 10249 | 3031 | proof (rule accI) | 
| 23751 | 3032 |       fix b assume "(b, {#}) \<in> ?R"
 | 
| 3033 | with not_less_empty show "b \<in> ?W" by contradiction | |
| 10249 | 3034 | qed | 
| 3035 | ||
| 23751 | 3036 | fix M a assume "M \<in> ?W" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3037 | from \<open>wf r\<close> have "\<forall>M \<in> ?W. add_mset a M \<in> ?W" | 
| 10249 | 3038 | proof induct | 
| 3039 | fix a | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3040 | 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 | 3041 | show "\<forall>M \<in> ?W. add_mset a M \<in> ?W" | 
| 10249 | 3042 | proof | 
| 23751 | 3043 | fix M assume "M \<in> ?W" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3044 | then show "add_mset a M \<in> ?W" | 
| 23373 | 3045 | by (rule acc_induct) (rule tedious_reasoning [OF _ r]) | 
| 10249 | 3046 | qed | 
| 3047 | qed | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3048 | from this and \<open>M \<in> ?W\<close> show "add_mset a M \<in> ?W" .. | 
| 10249 | 3049 | qed | 
| 3050 | qed | |
| 3051 | ||
| 74860 | 3052 | lemma wf_mult1: "wf r \<Longrightarrow> wf (mult1 r)" | 
| 3053 | by (rule acc_wfI) (rule all_accessible) | |
| 3054 | ||
| 3055 | lemma wf_mult: "wf r \<Longrightarrow> wf (mult r)" | |
| 3056 | unfolding mult_def by (rule wf_trancl) (rule wf_mult1) | |
| 3057 | ||
| 3058 | lemma wfP_multp: "wfP r \<Longrightarrow> wfP (multp r)" | |
| 3059 | unfolding multp_def wfP_def | |
| 3060 | by (simp add: wf_mult) | |
| 10249 | 3061 | |
| 3062 | ||
| 60500 | 3063 | subsubsection \<open>Closure-free presentation\<close> | 
| 3064 | ||
| 3065 | text \<open>One direction.\<close> | |
| 10249 | 3066 | lemma mult_implies_one_step: | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3067 | assumes | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3068 | trans: "trans r" and | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3069 | MN: "(M, N) \<in> mult r" | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3070 |   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 | 3071 | using MN unfolding mult_def mult1_def | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3072 | proof (induction rule: converse_trancl_induct) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3073 | case (base y) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3074 | then show ?case by force | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3075 | next | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3076 | 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 | 3077 | obtain I J K where | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3078 |     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 | 3079 | using N_decomp by blast | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3080 | obtain a M0 K' where | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3081 | 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 | 3082 | using yz by blast | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3083 | show ?case | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3084 | proof (cases "a \<in># K") | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3085 | case True | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3086 | 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 | 3087 | using K N trans True by (meson that transE) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3088 | ultimately show ?thesis | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3089 |       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 | 3090 | (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 | 3091 | next | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3092 | case False | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3093 | 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 | 3094 |     moreover have "M0 = I + K - {#a#}"
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3095 | using N(2) z by force | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3096 | ultimately show ?thesis | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3097 |       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 | 3098 | rule_tac x = "K + K'" in exI) | 
| 64017 
6e7bf7678518
more multiset simp rules
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63924diff
changeset | 3099 | (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 | 3100 | qed | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3101 | qed | 
| 10249 | 3102 | |
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3103 | lemma multp_implies_one_step: | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3104 |   "transp R \<Longrightarrow> multp R M N \<Longrightarrow> \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and> (\<forall>k\<in>#K. \<exists>x\<in>#J. R k x)"
 | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3105 | by (rule mult_implies_one_step[to_pred]) | 
| 74861 
74ac414618e2
added lemmas multp_implies_one_step, one_step_implies_multp, and subset_implies_multp
 desharna parents: 
74860diff
changeset | 3106 | |
| 17161 | 3107 | lemma one_step_implies_mult: | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3108 | assumes | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3109 |     "J \<noteq> {#}" and
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3110 | "\<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 | 3111 | shows "(I + K, I + J) \<in> mult r" | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3112 | using assms | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3113 | proof (induction "size J" arbitrary: I J K) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3114 | case 0 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3115 | then show ?case by auto | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3116 | next | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3117 | 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 | 3118 | 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 | 3119 | 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 | 3120 | show ?case | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3121 |   proof (cases "J' = {#}")
 | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3122 | case True | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3123 | then show ?thesis | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3124 | 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 | 3125 | next | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3126 | case [simp]: False | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3127 |     have K: "K = {#x \<in># K. (x, a) \<in> r#} + {#x \<in># K. (x, a) \<notin> r#}"
 | 
| 68992 | 3128 | by simp | 
| 63795 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3129 |     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 | 3130 |       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 | 3131 | 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 | 3132 |     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 | 3133 | by (fastforce simp: J mult1_def mult_def) | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3134 | ultimately show ?thesis | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3135 | unfolding mult_def by simp | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3136 | qed | 
| 
7f6128adfe67
tuning multisets; more interpretations
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63794diff
changeset | 3137 | qed | 
| 10249 | 3138 | |
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3139 | lemma one_step_implies_multp: | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3140 |   "J \<noteq> {#} \<Longrightarrow> \<forall>k\<in>#K. \<exists>j\<in>#J. R k j \<Longrightarrow> multp R (I + K) (I + J)"
 | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3141 |   by (rule one_step_implies_mult[of _ _ "{(x, y). r x y}" for r, folded multp_def, simplified])
 | 
| 74861 
74ac414618e2
added lemmas multp_implies_one_step, one_step_implies_multp, and subset_implies_multp
 desharna parents: 
74860diff
changeset | 3142 | |
| 65047 | 3143 | lemma subset_implies_mult: | 
| 3144 | assumes sub: "A \<subset># B" | |
| 3145 | shows "(A, B) \<in> mult r" | |
| 3146 | proof - | |
| 3147 | have ApBmA: "A + (B - A) = B" | |
| 3148 | using sub by simp | |
| 3149 |   have BmA: "B - A \<noteq> {#}"
 | |
| 3150 | using sub by (simp add: Diff_eq_empty_iff_mset subset_mset.less_le_not_le) | |
| 3151 | thus ?thesis | |
| 3152 |     by (rule one_step_implies_mult[of "B - A" "{#}" _ A, unfolded ApBmA, simplified])
 | |
| 3153 | qed | |
| 3154 | ||
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3155 | lemma subset_implies_multp: "A \<subset># B \<Longrightarrow> multp r A B" | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3156 |   by (rule subset_implies_mult[of _ _ "{(x, y). r x y}" for r, folded multp_def])
 | 
| 74861 
74ac414618e2
added lemmas multp_implies_one_step, one_step_implies_multp, and subset_implies_multp
 desharna parents: 
74860diff
changeset | 3157 | |
| 77688 | 3158 | lemma multp_repeat_mset_repeat_msetI: | 
| 3159 | assumes "transp R" and "multp R A B" and "n \<noteq> 0" | |
| 3160 | shows "multp R (repeat_mset n A) (repeat_mset n B)" | |
| 3161 | proof - | |
| 3162 | from \<open>transp R\<close> \<open>multp R A B\<close> obtain I J K where | |
| 3163 |     "B = I + J" and "A = I + K" and "J \<noteq> {#}" and "\<forall>k \<in># K. \<exists>x \<in># J. R k x"
 | |
| 3164 | by (auto dest: multp_implies_one_step) | |
| 3165 | ||
| 3166 | have repeat_n_A_eq: "repeat_mset n A = repeat_mset n I + repeat_mset n K" | |
| 3167 | using \<open>A = I + K\<close> by simp | |
| 3168 | ||
| 3169 | have repeat_n_B_eq: "repeat_mset n B = repeat_mset n I + repeat_mset n J" | |
| 3170 | using \<open>B = I + J\<close> by simp | |
| 3171 | ||
| 3172 | show ?thesis | |
| 3173 | unfolding repeat_n_A_eq repeat_n_B_eq | |
| 3174 | proof (rule one_step_implies_multp) | |
| 3175 |     from \<open>n \<noteq> 0\<close> show "repeat_mset n J \<noteq> {#}"
 | |
| 3176 |       using \<open>J \<noteq> {#}\<close>
 | |
| 3177 | by (simp add: repeat_mset_eq_empty_iff) | |
| 3178 | next | |
| 3179 | show "\<forall>k \<in># repeat_mset n K. \<exists>j \<in># repeat_mset n J. R k j" | |
| 3180 | using \<open>\<forall>k \<in># K. \<exists>x \<in># J. R k x\<close> | |
| 3181 | by (metis count_greater_zero_iff nat_0_less_mult_iff repeat_mset.rep_eq) | |
| 3182 | qed | |
| 3183 | qed | |
| 3184 | ||
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3185 | |
| 75560 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3186 | subsubsection \<open>Monotonicity\<close> | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3187 | |
| 76401 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3188 | lemma multp_mono_strong: | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3189 | assumes "multp R M1 M2" and "transp R" and | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3190 | S_if_R: "\<And>x y. x \<in> set_mset M1 \<Longrightarrow> y \<in> set_mset M2 \<Longrightarrow> R x y \<Longrightarrow> S x y" | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3191 | shows "multp S M1 M2" | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3192 | proof - | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3193 |   obtain I J K where "M2 = I + J" and "M1 = I + K" and "J \<noteq> {#}" and "\<forall>k\<in>#K. \<exists>x\<in>#J. R k x"
 | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3194 | using multp_implies_one_step[OF \<open>transp R\<close> \<open>multp R M1 M2\<close>] by auto | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3195 | show ?thesis | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3196 | unfolding \<open>M2 = I + J\<close> \<open>M1 = I + K\<close> | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3197 |   proof (rule one_step_implies_multp[OF \<open>J \<noteq> {#}\<close>])
 | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3198 | show "\<forall>k\<in>#K. \<exists>j\<in>#J. S k j" | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3199 | using S_if_R | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3200 | by (metis \<open>M1 = I + K\<close> \<open>M2 = I + J\<close> \<open>\<forall>k\<in>#K. \<exists>x\<in>#J. R k x\<close> union_iff) | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3201 | qed | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3202 | qed | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3203 | |
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3204 | lemma mult_mono_strong: | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3205 | assumes "(M1, M2) \<in> mult r" and "trans r" and | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3206 | S_if_R: "\<And>x y. x \<in> set_mset M1 \<Longrightarrow> y \<in> set_mset M2 \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> (x, y) \<in> s" | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3207 | shows "(M1, M2) \<in> mult s" | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3208 | using assms multp_mono_strong[of "\<lambda>x y. (x, y) \<in> r" M1 M2 "\<lambda>x y. (x, y) \<in> s", | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3209 | unfolded multp_def transp_trans_eq, simplified] | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3210 | by blast | 
| 
e7e8fbc89870
added lemmas multp_mono_strong and mult_mono_strong
 desharna parents: 
76359diff
changeset | 3211 | |
| 75584 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3212 | lemma monotone_on_multp_multp_image_mset: | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3213 | assumes "monotone_on A orda ordb f" and "transp orda" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3214 |   shows "monotone_on {M. set_mset M \<subseteq> A} (multp orda) (multp ordb) (image_mset f)"
 | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3215 | proof (rule monotone_onI) | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3216 | fix M1 M2 | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3217 | assume | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3218 |     M1_in: "M1 \<in> {M. set_mset M \<subseteq> A}" and
 | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3219 |     M2_in: "M2 \<in> {M. set_mset M \<subseteq> A}" and
 | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3220 | M1_lt_M2: "multp orda M1 M2" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3221 | |
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3222 | from multp_implies_one_step[OF \<open>transp orda\<close> M1_lt_M2] obtain I J K where | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3223 | M2_eq: "M2 = I + J" and | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3224 | M1_eq: "M1 = I + K" and | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3225 |     J_neq_mempty: "J \<noteq> {#}" and
 | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3226 | ball_K_less: "\<forall>k\<in>#K. \<exists>x\<in>#J. orda k x" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3227 | by metis | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3228 | |
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3229 | have "multp ordb (image_mset f I + image_mset f K) (image_mset f I + image_mset f J)" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3230 | proof (intro one_step_implies_multp ballI) | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3231 |     show "image_mset f J \<noteq> {#}"
 | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3232 | using J_neq_mempty by simp | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3233 | next | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3234 | fix k' assume "k'\<in>#image_mset f K" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3235 | then obtain k where "k' = f k" and k_in: "k \<in># K" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3236 | by auto | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3237 | then obtain j where j_in: "j\<in>#J" and "orda k j" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3238 | using ball_K_less by auto | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3239 | |
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3240 | have "ordb (f k) (f j)" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3241 | proof (rule \<open>monotone_on A orda ordb f\<close>[THEN monotone_onD, OF _ _ \<open>orda k j\<close>]) | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3242 | show "k \<in> A" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3243 | using M1_eq M1_in k_in by auto | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3244 | next | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3245 | show "j \<in> A" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3246 | using M2_eq M2_in j_in by auto | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3247 | qed | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3248 | thus "\<exists>j\<in>#image_mset f J. ordb k' j" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3249 | using \<open>j \<in># J\<close> \<open>k' = f k\<close> by auto | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3250 | qed | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3251 | thus "multp ordb (image_mset f M1) (image_mset f M2)" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3252 | by (simp add: M1_eq M2_eq) | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3253 | qed | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3254 | |
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3255 | lemma monotone_multp_multp_image_mset: | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3256 | assumes "monotone orda ordb f" and "transp orda" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3257 | shows "monotone (multp orda) (multp ordb) (image_mset f)" | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3258 | by (rule monotone_on_multp_multp_image_mset[OF assms, simplified]) | 
| 
c32658b9e4df
added lemmas monotone{,_on}_multp_multp_image_mset
 desharna parents: 
75560diff
changeset | 3259 | |
| 75560 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3260 | lemma multp_image_mset_image_msetD: | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3261 | assumes | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3262 | "multp R (image_mset f A) (image_mset f B)" and | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3263 | "transp R" and | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3264 | inj_on_f: "inj_on f (set_mset A \<union> set_mset B)" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3265 | shows "multp (\<lambda>x y. R (f x) (f y)) A B" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3266 | proof - | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3267 | from assms(1,2) obtain I J K where | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3268 | f_B_eq: "image_mset f B = I + J" and | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3269 | f_A_eq: "image_mset f A = I + K" and | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3270 |     J_neq_mempty: "J \<noteq> {#}" and
 | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3271 | ball_K_less: "\<forall>k\<in>#K. \<exists>x\<in>#J. R k x" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3272 | by (auto dest: multp_implies_one_step) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3273 | |
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3274 | from f_B_eq obtain I' J' where | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3275 | B_def: "B = I' + J'" and I_def: "I = image_mset f I'" and J_def: "J = image_mset f J'" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3276 | using image_mset_eq_plusD by blast | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3277 | |
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3278 | from inj_on_f have inj_on_f': "inj_on f (set_mset A \<union> set_mset I')" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3279 | by (rule inj_on_subset) (auto simp add: B_def) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3280 | |
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3281 | from f_A_eq obtain K' where | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3282 | A_def: "A = I' + K'" and K_def: "K = image_mset f K'" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3283 | by (auto simp: I_def dest: image_mset_eq_image_mset_plusD[OF _ inj_on_f']) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3284 | |
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3285 | show ?thesis | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3286 | unfolding A_def B_def | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3287 | proof (intro one_step_implies_multp ballI) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3288 |     from J_neq_mempty show "J' \<noteq> {#}"
 | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3289 | by (simp add: J_def) | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3290 | next | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3291 | fix k assume "k \<in># K'" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3292 | with ball_K_less obtain j' where "j' \<in># J" and "R (f k) j'" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3293 | using K_def by auto | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3294 | moreover then obtain j where "j \<in># J'" and "f j = j'" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3295 | using J_def by auto | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3296 | ultimately show "\<exists>j\<in>#J'. R (f k) (f j)" | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3297 | by blast | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3298 | qed | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3299 | qed | 
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3300 | |
| 
aeb797356de0
added lemmas image_mset_eq_{image_mset_plus,plus,plus_image_mset}D, and multp_image_mset_image_msetD
 desharna parents: 
75467diff
changeset | 3301 | |
| 74862 
aa51e974b688
added lemmas multp_cancel, multp_cancel_add_mset, and multp_cancel_max
 desharna parents: 
74861diff
changeset | 3302 | subsubsection \<open>The multiset extension is cancellative for multiset union\<close> | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3303 | |
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3304 | lemma mult_cancel: | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3305 | assumes "trans s" and "irrefl_on (set_mset Z) s" | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3306 | 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 | 3307 | proof | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3308 | assume ?L thus ?R | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3309 | using \<open>irrefl_on (set_mset Z) s\<close> | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3310 | proof (induct Z) | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3311 | case (add z Z) | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3312 |     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 | 3313 | "\<forall>x \<in> set_mset X'. \<exists>y \<in> set_mset Y'. (x, y) \<in> s" | 
| 64911 | 3314 | 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 | 3315 | 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 | 3316 | 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 | 3317 | thus ?case | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3318 | proof (cases) | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3319 | case 1 thus ?thesis | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3320 | using * one_step_implies_mult[of Y' X' s Z2] add(3) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3321 |         by (auto simp: add.commute[of _ "{#_#}"] add.assoc intro: add(1) elim: irrefl_on_subset)
 | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3322 | next | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3323 | case 2 then obtain y where "y \<in> set_mset Y2" "(z, y) \<in> s" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3324 | using *(4) \<open>irrefl_on (set_mset (add_mset z Z)) s\<close> | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3325 | by (auto simp: irrefl_on_def) | 
| 64911 | 3326 | 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 | 3327 | 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 | 3328 | using 2 *(4)[rule_format, of x'] by auto | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3329 | ultimately show ?thesis | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3330 | using * one_step_implies_mult[of Y2 X2 s Z'] 2 add(3) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3331 |         by (force simp: add.commute[of "{#_#}"] add.assoc[symmetric] intro: add(1)
 | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3332 | elim: irrefl_on_subset) | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3333 | qed | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3334 | qed auto | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3335 | next | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3336 | 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 | 3337 |     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 | 3338 | 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 | 3339 | 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 | 3340 | qed | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3341 | |
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3342 | lemma multp_cancel: | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3343 | "transp R \<Longrightarrow> irreflp_on (set_mset Z) R \<Longrightarrow> multp R (X + Z) (Y + Z) \<longleftrightarrow> multp R X Y" | 
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3344 | by (rule mult_cancel[to_pred]) | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3345 | |
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3346 | lemma mult_cancel_add_mset: | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3347 |   "trans r \<Longrightarrow> irrefl_on {z} r \<Longrightarrow>
 | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3348 | ((add_mset z X, add_mset z Y) \<in> mult r) = ((X, Y) \<in> mult r)" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3349 |   by (rule mult_cancel[of _ "{#_#}", simplified])
 | 
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3350 | |
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3351 | lemma multp_cancel_add_mset: | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3352 |   "transp R \<Longrightarrow> irreflp_on {z} R \<Longrightarrow> multp R (add_mset z X) (add_mset z Y) = multp R X Y"
 | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3353 | by (rule mult_cancel_add_mset[to_pred, folded bot_set_def]) | 
| 74862 
aa51e974b688
added lemmas multp_cancel, multp_cancel_add_mset, and multp_cancel_max
 desharna parents: 
74861diff
changeset | 3354 | |
| 74804 
5749fefd3fa0
simplified mult_cancel_max and introduced orginal lemma as mult_cancel_max0
 desharna parents: 
74803diff
changeset | 3355 | lemma mult_cancel_max0: | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3356 | assumes "trans s" and "irrefl_on (set_mset X \<inter> set_mset Y) s" | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3357 | 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 | 3358 | proof - | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3359 | have "(X - X \<inter># Y + X \<inter># Y, Y - X \<inter># Y + X \<inter># Y) \<in> mult s \<longleftrightarrow> (X - X \<inter># Y, Y - X \<inter># Y) \<in> mult s" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3360 | proof (rule mult_cancel) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3361 | from assms show "trans s" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3362 | by simp | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3363 | next | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3364 | from assms show "irrefl_on (set_mset (X \<inter># Y)) s" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3365 | by simp | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3366 | qed | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3367 | moreover have "X - X \<inter># Y + X \<inter># Y = X" "Y - X \<inter># Y + X \<inter># Y = Y" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3368 | by (auto simp flip: count_inject) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3369 | ultimately show ?thesis | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3370 | by simp | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3371 | qed | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3372 | |
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3373 | lemma mult_cancel_max: | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3374 | "trans r \<Longrightarrow> irrefl_on (set_mset X \<inter> set_mset Y) r \<Longrightarrow> | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3375 | (X, Y) \<in> mult r \<longleftrightarrow> (X - Y, Y - X) \<in> mult r" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3376 | by (rule mult_cancel_max0[simplified]) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3377 | |
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3378 | lemma multp_cancel_max: | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3379 | "transp R \<Longrightarrow> irreflp_on (set_mset X \<inter> set_mset Y) R \<Longrightarrow> multp R X Y \<longleftrightarrow> multp R (X - Y) (Y - X)" | 
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3380 | by (rule mult_cancel_max[to_pred]) | 
| 74862 
aa51e974b688
added lemmas multp_cancel, multp_cancel_add_mset, and multp_cancel_max
 desharna parents: 
74861diff
changeset | 3381 | |
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3382 | |
| 77049 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3383 | subsubsection \<open>Strict partial-order properties\<close> | 
| 74864 | 3384 | |
| 3385 | lemma mult1_lessE: | |
| 3386 |   assumes "(N, M) \<in> mult1 {(a, b). r a b}" and "asymp r"
 | |
| 3387 | obtains a M0 K where "M = add_mset a M0" "N = M0 + K" | |
| 3388 | "a \<notin># K" "\<And>b. b \<in># K \<Longrightarrow> r b a" | |
| 3389 | proof - | |
| 3390 | from assms obtain a M0 K where "M = add_mset a M0" "N = M0 + K" and | |
| 3391 | *: "b \<in># K \<Longrightarrow> r b a" for b by (blast elim: mult1E) | |
| 3392 | moreover from * [of a] have "a \<notin># K" | |
| 76682 
e260dabc88e6
added predicates asym_on and asymp_on and redefined asym and asymp to be abbreviations
 desharna parents: 
76611diff
changeset | 3393 | using \<open>asymp r\<close> by (meson asympD) | 
| 74864 | 3394 | ultimately show thesis by (auto intro: that) | 
| 3395 | qed | |
| 3396 | ||
| 74865 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3397 | lemma trans_mult: "trans r \<Longrightarrow> trans (mult r)" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3398 | by (simp add: mult_def) | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3399 | |
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3400 | lemma transp_multp: "transp r \<Longrightarrow> transp (multp r)" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3401 | unfolding multp_def transp_trans_eq | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3402 |   by (fact trans_mult[of "{(x, y). r x y}" for r, folded transp_trans])
 | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3403 | |
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3404 | lemma irrefl_mult: | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3405 | assumes "trans r" "irrefl r" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3406 | shows "irrefl (mult r)" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3407 | proof (intro irreflI notI) | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3408 | fix M | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3409 | assume "(M, M) \<in> mult r" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3410 | then obtain I J K where "M = I + J" and "M = I + K" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3411 |     and "J \<noteq> {#}" and "(\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> r)"
 | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3412 | using mult_implies_one_step[OF \<open>trans r\<close>] by blast | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3413 |   then have *: "K \<noteq> {#}" and **: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. (k, j) \<in> r" by auto
 | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3414 | have "finite (set_mset K)" by simp | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3415 |   hence "set_mset K = {}"
 | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3416 | using ** | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3417 | proof (induction rule: finite_induct) | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3418 | case empty | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3419 | thus ?case by simp | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3420 | next | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3421 | case (insert x F) | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3422 | have False | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3423 | using \<open>irrefl r\<close>[unfolded irrefl_def, rule_format] | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3424 | using \<open>trans r\<close>[THEN transD] | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3425 | by (metis equals0D insert.IH insert.prems insertE insertI1 insertI2) | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3426 | thus ?case .. | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3427 | qed | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3428 | with * show False by simp | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3429 | qed | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3430 | |
| 76589 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3431 | lemma irreflp_multp: "transp R \<Longrightarrow> irreflp R \<Longrightarrow> irreflp (multp R)" | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3432 |   by (rule irrefl_mult[of "{(x, y). r x y}" for r,
 | 
| 
1c083e32aed6
stated goals of some lemmas explicitely to prevent silent changes
 desharna parents: 
76570diff
changeset | 3433 | folded transp_trans_eq irreflp_irrefl_eq, simplified, folded multp_def]) | 
| 74865 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3434 | |
| 74864 | 3435 | instantiation multiset :: (preorder) order begin | 
| 3436 | ||
| 3437 | definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" | |
| 3438 | where "M < N \<longleftrightarrow> multp (<) M N" | |
| 3439 | ||
| 3440 | definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" | |
| 3441 | where "less_eq_multiset M N \<longleftrightarrow> M < N \<or> M = N" | |
| 3442 | ||
| 3443 | instance | |
| 74865 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3444 | proof intro_classes | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3445 | fix M N :: "'a multiset" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3446 | show "(M < N) = (M \<le> N \<and> \<not> N \<le> M)" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3447 | unfolding less_eq_multiset_def less_multiset_def | 
| 76749 
11a24dab1880
strengthened and renamed lemmas preorder.transp_(ge|gr|le|less)
 desharna parents: 
76682diff
changeset | 3448 | by (metis irreflp_def irreflp_on_less irreflp_multp transpE transp_on_less transp_multp) | 
| 74865 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3449 | next | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3450 | fix M :: "'a multiset" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3451 | show "M \<le> M" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3452 | unfolding less_eq_multiset_def | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3453 | by simp | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3454 | next | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3455 | fix M1 M2 M3 :: "'a multiset" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3456 | show "M1 \<le> M2 \<Longrightarrow> M2 \<le> M3 \<Longrightarrow> M1 \<le> M3" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3457 | unfolding less_eq_multiset_def less_multiset_def | 
| 76749 
11a24dab1880
strengthened and renamed lemmas preorder.transp_(ge|gr|le|less)
 desharna parents: 
76682diff
changeset | 3458 | using transp_multp[OF transp_on_less, THEN transpD] | 
| 74865 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3459 | by blast | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3460 | next | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3461 | fix M N :: "'a multiset" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3462 | show "M \<le> N \<Longrightarrow> N \<le> M \<Longrightarrow> M = N" | 
| 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3463 | unfolding less_eq_multiset_def less_multiset_def | 
| 76749 
11a24dab1880
strengthened and renamed lemmas preorder.transp_(ge|gr|le|less)
 desharna parents: 
76682diff
changeset | 3464 | using transp_multp[OF transp_on_less, THEN transpD] | 
| 
11a24dab1880
strengthened and renamed lemmas preorder.transp_(ge|gr|le|less)
 desharna parents: 
76682diff
changeset | 3465 | using irreflp_multp[OF transp_on_less irreflp_on_less, unfolded irreflp_def, rule_format] | 
| 74865 
b5031a8f7718
added lemmas irreflp_{less,greater} to preorder and {trans,irrefl}_mult{,p} to Multiset
 desharna parents: 
74864diff
changeset | 3466 | by blast | 
| 74864 | 3467 | qed | 
| 3468 | ||
| 3469 | end | |
| 3470 | ||
| 3471 | lemma mset_le_irrefl [elim!]: | |
| 3472 | fixes M :: "'a::preorder multiset" | |
| 3473 | shows "M < M \<Longrightarrow> R" | |
| 3474 | by simp | |
| 3475 | ||
| 74868 
2741ef11ccf6
added wfP_less to wellorder and wfP_less_multiset
 desharna parents: 
74865diff
changeset | 3476 | lemma wfP_less_multiset[simp]: | 
| 
2741ef11ccf6
added wfP_less to wellorder and wfP_less_multiset
 desharna parents: 
74865diff
changeset | 3477 |   assumes wfP_less: "wfP ((<) :: ('a :: preorder) \<Rightarrow> 'a \<Rightarrow> bool)"
 | 
| 
2741ef11ccf6
added wfP_less to wellorder and wfP_less_multiset
 desharna parents: 
74865diff
changeset | 3478 | shows "wfP ((<) :: 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool)" | 
| 
2741ef11ccf6
added wfP_less to wellorder and wfP_less_multiset
 desharna parents: 
74865diff
changeset | 3479 | using wfP_multp[OF wfP_less] less_multiset_def | 
| 
2741ef11ccf6
added wfP_less to wellorder and wfP_less_multiset
 desharna parents: 
74865diff
changeset | 3480 | by (metis wfPUNIVI wfP_induct) | 
| 
2741ef11ccf6
added wfP_less to wellorder and wfP_less_multiset
 desharna parents: 
74865diff
changeset | 3481 | |
| 74864 | 3482 | |
| 77049 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3483 | subsubsection \<open>Strict total-order properties\<close> | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3484 | |
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3485 | lemma total_on_mult: | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3486 | assumes "total_on A r" and "trans r" and "\<And>M. M \<in> B \<Longrightarrow> set_mset M \<subseteq> A" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3487 | shows "total_on B (mult r)" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3488 | proof (rule total_onI) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3489 | fix M1 M2 assume "M1 \<in> B" and "M2 \<in> B" and "M1 \<noteq> M2" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3490 | let ?I = "M1 \<inter># M2" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3491 | show "(M1, M2) \<in> mult r \<or> (M2, M1) \<in> mult r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3492 |   proof (cases "M1 - ?I = {#} \<or> M2 - ?I = {#}")
 | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3493 | case True | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3494 | with \<open>M1 \<noteq> M2\<close> show ?thesis | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3495 | by (metis Diff_eq_empty_iff_mset diff_intersect_left_idem diff_intersect_right_idem | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3496 | subset_implies_mult subset_mset.less_le) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3497 | next | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3498 | case False | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3499 | from assms(1) have "total_on (set_mset (M1 - ?I)) r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3500 | by (meson \<open>M1 \<in> B\<close> assms(3) diff_subset_eq_self set_mset_mono total_on_subset) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3501 | with False obtain greatest1 where | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3502 | greatest1_in: "greatest1 \<in># M1 - ?I" and | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3503 | greatest1_greatest: "\<forall>x \<in># M1 - ?I. greatest1 \<noteq> x \<longrightarrow> (x, greatest1) \<in> r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3504 | using Multiset.bex_greatest_element[to_set, of "M1 - ?I" r] | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3505 | by (metis assms(2) subset_UNIV trans_on_subset) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3506 | |
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3507 | from assms(1) have "total_on (set_mset (M2 - ?I)) r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3508 | by (meson \<open>M2 \<in> B\<close> assms(3) diff_subset_eq_self set_mset_mono total_on_subset) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3509 | with False obtain greatest2 where | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3510 | greatest2_in: "greatest2 \<in># M2 - ?I" and | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3511 | greatest2_greatest: "\<forall>x \<in># M2 - ?I. greatest2 \<noteq> x \<longrightarrow> (x, greatest2) \<in> r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3512 | using Multiset.bex_greatest_element[to_set, of "M2 - ?I" r] | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3513 | by (metis assms(2) subset_UNIV trans_on_subset) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3514 | |
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3515 | have "greatest1 \<noteq> greatest2" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3516 | using greatest1_in \<open>greatest2 \<in># M2 - ?I\<close> | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3517 | by (metis diff_intersect_left_idem diff_intersect_right_idem dual_order.eq_iff in_diff_count | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3518 | in_diff_countE le_add_same_cancel2 less_irrefl zero_le) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3519 | hence "(greatest1, greatest2) \<in> r \<or> (greatest2, greatest1) \<in> r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3520 | using \<open>total_on A r\<close>[unfolded total_on_def, rule_format, of greatest1 greatest2] | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3521 | \<open>M1 \<in> B\<close> \<open>M2 \<in> B\<close> greatest1_in greatest2_in assms(3) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3522 | by (meson in_diffD in_mono) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3523 | thus ?thesis | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3524 | proof (elim disjE) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3525 | assume "(greatest1, greatest2) \<in> r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3526 | have "(?I + (M1 - ?I), ?I + (M2 - ?I)) \<in> mult r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3527 | proof (rule one_step_implies_mult[of "M2 - ?I" "M1 - ?I" r ?I]) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3528 |         show "M2 - ?I \<noteq> {#}"
 | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3529 | using False by force | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3530 | next | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3531 | show "\<forall>k\<in>#M1 - ?I. \<exists>j\<in>#M2 - ?I. (k, j) \<in> r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3532 | using \<open>(greatest1, greatest2) \<in> r\<close> greatest2_in greatest1_greatest | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3533 | by (metis assms(2) transD) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3534 | qed | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3535 | hence "(M1, M2) \<in> mult r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3536 | by (metis subset_mset.add_diff_inverse subset_mset.inf.cobounded1 | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3537 | subset_mset.inf.cobounded2) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3538 | thus "(M1, M2) \<in> mult r \<or> (M2, M1) \<in> mult r" .. | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3539 | next | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3540 | assume "(greatest2, greatest1) \<in> r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3541 | have "(?I + (M2 - ?I), ?I + (M1 - ?I)) \<in> mult r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3542 | proof (rule one_step_implies_mult[of "M1 - ?I" "M2 - ?I" r ?I]) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3543 |         show "M1 - M1 \<inter># M2 \<noteq> {#}"
 | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3544 | using False by force | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3545 | next | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3546 | show "\<forall>k\<in>#M2 - ?I. \<exists>j\<in>#M1 - ?I. (k, j) \<in> r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3547 | using \<open>(greatest2, greatest1) \<in> r\<close> greatest1_in greatest2_greatest | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3548 | by (metis assms(2) transD) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3549 | qed | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3550 | hence "(M2, M1) \<in> mult r" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3551 | by (metis subset_mset.add_diff_inverse subset_mset.inf.cobounded1 | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3552 | subset_mset.inf.cobounded2) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3553 | thus "(M1, M2) \<in> mult r \<or> (M2, M1) \<in> mult r" .. | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3554 | qed | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3555 | qed | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3556 | qed | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3557 | |
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3558 | lemma total_mult: "total r \<Longrightarrow> trans r \<Longrightarrow> total (mult r)" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3559 | by (rule total_on_mult[of UNIV r UNIV, simplified]) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3560 | |
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3561 | lemma totalp_on_multp: | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3562 | "totalp_on A R \<Longrightarrow> transp R \<Longrightarrow> (\<And>M. M \<in> B \<Longrightarrow> set_mset M \<subseteq> A) \<Longrightarrow> totalp_on B (multp R)" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3563 |   using total_on_mult[of A "{(x,y). R x y}" B, to_pred]
 | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3564 | by (simp add: multp_def total_on_def totalp_on_def) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3565 | |
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3566 | lemma totalp_multp: "totalp R \<Longrightarrow> transp R \<Longrightarrow> totalp (multp R)" | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3567 | by (rule totalp_on_multp[of UNIV R UNIV, simplified]) | 
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3568 | |
| 
e293216df994
added lemmas total_on_mult, total_mult, totalp_on_multp, and totalp_multp
 desharna parents: 
76755diff
changeset | 3569 | |
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3570 | 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 | 3571 | |
| 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 3572 | text \<open> | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3573 | 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 | 3574 | executable whenever the given predicate \<open>P\<close> is. Together with the standard | 
| 67398 | 3575 | code equations for \<open>(\<inter>#\<close>) and \<open>(-\<close>) this should yield quadratic | 
| 74803 | 3576 | (with respect to calls to \<open>P\<close>) implementations of \<open>multp_code\<close> and \<open>multeqp_code\<close>. | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 3577 | \<close> | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3578 | |
| 74803 | 3579 | definition multp_code :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
 | 
| 3580 | "multp_code P N M = | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3581 | (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 | 3582 |     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 | 3583 | |
| 74803 | 3584 | definition multeqp_code :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
 | 
| 3585 | "multeqp_code P N M = | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3586 | (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 | 3587 | (\<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 | 3588 | |
| 74805 | 3589 | lemma multp_code_iff_mult: | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3590 | assumes "irrefl_on (set_mset N \<inter> set_mset M) R" and "trans R" and | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3591 | [simp]: "\<And>x y. P x y \<longleftrightarrow> (x, y) \<in> R" | 
| 74803 | 3592 | shows "multp_code P N M \<longleftrightarrow> (N, M) \<in> mult R" (is "?L \<longleftrightarrow> ?R") | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3593 | proof - | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3594 | have *: "M \<inter># N + (N - M \<inter># N) = N" "M \<inter># N + (M - M \<inter># N) = M" | 
| 68406 | 3595 |     "(M - M \<inter># N) \<inter># (N - M \<inter># N) = {#}" by (auto simp flip: count_inject)
 | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3596 | show ?thesis | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 3597 | proof | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3598 | assume ?L thus ?R | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3599 | using one_step_implies_mult[of "M - M \<inter># N" "N - M \<inter># N" R "M \<inter># N"] * | 
| 74803 | 3600 | by (auto simp: multp_code_def Let_def) | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 3601 | next | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3602 |     { 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 | 3603 |       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 | 3604 | } note [dest!] = this | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3605 | assume ?R thus ?L | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3606 | using mult_cancel_max | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3607 | using mult_implies_one_step[OF assms(2), of "N - M \<inter># N" "M - M \<inter># N"] | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3608 | mult_cancel_max[OF assms(2,1)] * by (auto simp: multp_code_def) | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 3609 | qed | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3610 | qed | 
| 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3611 | |
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3612 | lemma multp_code_iff_multp: | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3613 | "irreflp_on (set_mset M \<inter> set_mset N) R \<Longrightarrow> transp R \<Longrightarrow> multp_code R M N \<longleftrightarrow> multp R M N" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3614 | using multp_code_iff_mult[simplified, to_pred, of M N R R] by simp | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3615 | |
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3616 | lemma multp_code_eq_multp: | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3617 | assumes "irreflp R" and "transp R" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3618 | shows "multp_code R = multp R" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3619 | proof (intro ext) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3620 | fix M N | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3621 | show "multp_code R M N = multp R M N" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3622 | proof (rule multp_code_iff_multp) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3623 | from assms show "irreflp_on (set_mset M \<inter> set_mset N) R" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3624 | by (auto intro: irreflp_on_subset) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3625 | next | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3626 | from assms show "transp R" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3627 | by simp | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3628 | qed | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3629 | qed | 
| 74863 
691131ce4641
added lemmas multp_code_eq_multp and multeqp_code_eq_reflclp_multp
 desharna parents: 
74862diff
changeset | 3630 | |
| 74805 | 3631 | lemma multeqp_code_iff_reflcl_mult: | 
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3632 | assumes "irrefl_on (set_mset N \<inter> set_mset M) R" and "trans R" and "\<And>x y. P x y \<longleftrightarrow> (x, y) \<in> R" | 
| 74803 | 3633 | shows "multeqp_code P N M \<longleftrightarrow> (N, M) \<in> (mult R)\<^sup>=" | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3634 | proof - | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3635 |   { assume "N \<noteq> M" "M - M \<inter># N = {#}"
 | 
| 68406 | 3636 | then obtain y where "count N y \<noteq> count M y" by (auto simp flip: count_inject) | 
| 64911 | 3637 |     then have "\<exists>y. count M y < count N y" using \<open>M - M \<inter># N = {#}\<close>
 | 
| 68406 | 3638 | by (auto simp flip: count_inject dest!: le_neq_implies_less fun_cong[of _ _ y]) | 
| 63660 
76302202a92d
add monotonicity propertyies of `mult1` and `mult`
 Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at> parents: 
63560diff
changeset | 3639 | } | 
| 74803 | 3640 | then have "multeqp_code P N M \<longleftrightarrow> multp_code P N M \<or> N = M" | 
| 3641 | by (auto simp: multeqp_code_def multp_code_def Let_def in_diff_count) | |
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3642 | thus ?thesis | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3643 | using multp_code_iff_mult[OF assms] by simp | 
| 63088 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 3644 | qed | 
| 
f2177f5d2aed
a quasi-recursive characterization of the multiset order (by Christian Sternagel)
 haftmann parents: 
63060diff
changeset | 3645 | |
| 76611 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3646 | lemma multeqp_code_iff_reflclp_multp: | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3647 | "irreflp_on (set_mset M \<inter> set_mset N) R \<Longrightarrow> transp R \<Longrightarrow> multeqp_code R M N \<longleftrightarrow> (multp R)\<^sup>=\<^sup>= M N" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3648 | using multeqp_code_iff_reflcl_mult[simplified, to_pred, of M N R R] by simp | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3649 | |
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3650 | lemma multeqp_code_eq_reflclp_multp: | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3651 | assumes "irreflp R" and "transp R" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3652 | shows "multeqp_code R = (multp R)\<^sup>=\<^sup>=" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3653 | proof (intro ext) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3654 | fix M N | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3655 | show "multeqp_code R M N \<longleftrightarrow> (multp R)\<^sup>=\<^sup>= M N" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3656 | proof (rule multeqp_code_iff_reflclp_multp) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3657 | from assms show "irreflp_on (set_mset M \<inter> set_mset N) R" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3658 | by (auto intro: irreflp_on_subset) | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3659 | next | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3660 | from assms show "transp R" | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3661 | by simp | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3662 | qed | 
| 
a7d2a7a737b8
Strengthened multiset lemmas w.r.t. irrefl and irreflp
 desharna parents: 
76589diff
changeset | 3663 | qed | 
| 74863 
691131ce4641
added lemmas multp_code_eq_multp and multeqp_code_eq_reflclp_multp
 desharna parents: 
74862diff
changeset | 3664 | |
| 
691131ce4641
added lemmas multp_code_eq_multp and multeqp_code_eq_reflclp_multp
 desharna parents: 
74862diff
changeset | 3665 | |
| 60500 | 3666 | subsubsection \<open>Monotonicity of multiset union\<close> | 
| 10249 | 3667 | |
| 60606 | 3668 | lemma mult1_union: "(B, D) \<in> mult1 r \<Longrightarrow> (C + B, C + D) \<in> mult1 r" | 
| 64076 | 3669 | by (force simp: mult1_def) | 
| 10249 | 3670 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3671 | lemma union_le_mono2: "B < D \<Longrightarrow> C + B < C + (D::'a::preorder multiset)" | 
| 74864 | 3672 | apply (unfold less_multiset_def multp_def mult_def) | 
| 26178 | 3673 | apply (erule trancl_induct) | 
| 40249 
cd404ecb9107
Remove unnecessary premise of mult1_union
 Lars Noschinski <noschinl@in.tum.de> parents: 
39533diff
changeset | 3674 | apply (blast intro: mult1_union) | 
| 
cd404ecb9107
Remove unnecessary premise of mult1_union
 Lars Noschinski <noschinl@in.tum.de> parents: 
39533diff
changeset | 3675 | apply (blast intro: mult1_union trancl_trans) | 
| 26178 | 3676 | done | 
| 10249 | 3677 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3678 | 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 | 3679 | apply (subst add.commute [of B C]) | 
| 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 haftmann parents: 
57492diff
changeset | 3680 | apply (subst add.commute [of D C]) | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3681 | apply (erule union_le_mono2) | 
| 26178 | 3682 | done | 
| 10249 | 3683 | |
| 17161 | 3684 | lemma union_less_mono: | 
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3685 | 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 | 3686 | 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 | 3687 | 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 | 3688 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3689 | instantiation multiset :: (preorder) ordered_ab_semigroup_add | 
| 63388 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3690 | begin | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3691 | instance | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3692 | 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 | 3693 | end | 
| 15072 | 3694 | |
| 63409 
3f3223b90239
moved lemmas and locales around (with minor incompatibilities)
 blanchet parents: 
63388diff
changeset | 3695 | |
| 60500 | 3696 | 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 | 3697 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3698 | 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 | 3699 |   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 | 3700 |   and multi_member_last: "x \<in># {# x #}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3701 | by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3702 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3703 | definition "ms_strict = mult pair_less" | 
| 37765 | 3704 | 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 | 3705 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3706 | 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 | 3707 | 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 | 3708 | 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 | 3709 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3710 | lemma smsI: | 
| 60495 | 3711 | "(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 | 3712 | unfolding ms_strict_def | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3713 | 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 | 3714 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3715 | lemma wmsI: | 
| 60495 | 3716 |   "(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 | 3717 | \<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 | 3718 | unfolding ms_weak_def ms_strict_def | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3719 | 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 | 3720 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3721 | inductive pw_leq | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3722 | where | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3723 |   pw_leq_empty: "pw_leq {#} {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3724 | | 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 | 3725 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3726 | lemma pw_leq_lstep: | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3727 |   "(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 | 3728 | 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 | 3729 | |
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3730 | lemma pw_leq_split: | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3731 | assumes "pw_leq X Y" | 
| 60495 | 3732 |   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 | 3733 | using assms | 
| 60606 | 3734 | proof induct | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3735 | 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 | 3736 | next | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3737 | 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 | 3738 | then obtain A B Z where | 
| 58425 | 3739 | [simp]: "X = A + Z" "Y = B + Z" | 
| 60495 | 3740 |       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 | 3741 | by auto | 
| 60606 | 3742 | 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 | 3743 | unfolding pair_leq_def by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3744 | thus ?case | 
| 60606 | 3745 | proof cases | 
| 3746 | case [simp]: 1 | |
| 3747 |     have "{#x#} + X = A + ({#y#}+Z) \<and> {#y#} + Y = B + ({#y#}+Z) \<and>
 | |
| 3748 |       ((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 | 3749 | by auto | 
| 60606 | 3750 | thus ?thesis by blast | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3751 | next | 
| 60606 | 3752 | case 2 | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3753 |     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 | 3754 |     have "{#x#} + X = ?A' + Z"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3755 |       "{#y#} + Y = ?B' + Z"
 | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3756 | by auto | 
| 58425 | 3757 | moreover have | 
| 60495 | 3758 | "(set_mset ?A', set_mset ?B') \<in> max_strict" | 
| 60606 | 3759 | 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 | 3760 | by (auto elim!: max_ext.cases) | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3761 | ultimately show ?thesis by blast | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3762 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3763 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3764 | |
| 58425 | 3765 | lemma | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3766 | assumes pwleq: "pw_leq Z Z'" | 
| 60495 | 3767 | shows ms_strictI: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict" | 
| 60606 | 3768 | and ms_weakI1: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak" | 
| 3769 |     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 | 3770 | proof - | 
| 58425 | 3771 | from pw_leq_split[OF pwleq] | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3772 | obtain A' B' Z'' | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3773 | where [simp]: "Z = A' + Z''" "Z' = B' + Z''" | 
| 60495 | 3774 |     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 | 3775 | by blast | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3776 |   {
 | 
| 60495 | 3777 | 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 | 3778 | from mx_or_empty | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3779 | 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 | 3780 | proof | 
| 60495 | 3781 | assume max': "(set_mset A', set_mset B') \<in> max_strict" | 
| 3782 | 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 | 3783 | by (auto simp: max_strict_def intro: max_ext_additive) | 
| 58425 | 3784 | thus ?thesis by (rule smsI) | 
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3785 | next | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3786 |       assume [simp]: "A' = {#} \<and> B' = {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3787 | 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 | 3788 | qed | 
| 60606 | 3789 | 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 | 3790 | 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 | 3791 | } | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3792 | from mx_or_empty | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3793 | 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 | 3794 |   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 | 3795 | qed | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3796 | |
| 39301 | 3797 | 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 | 3798 | and nonempty_plus: "{# x #} + rs \<noteq> {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3799 | and nonempty_single: "{# x #} \<noteq> {#}"
 | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3800 | by auto | 
| 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3801 | |
| 60500 | 3802 | setup \<open> | 
| 60606 | 3803 | let | 
| 74634 | 3804 | fun msetT T = \<^Type>\<open>multiset T\<close>; | 
| 3805 | ||
| 3806 |     fun mk_mset T [] = \<^instantiate>\<open>'a = T in term \<open>{#}\<close>\<close>
 | |
| 3807 |       | mk_mset T [x] = \<^instantiate>\<open>'a = T and x in term \<open>{#x#}\<close>\<close>
 | |
| 3808 | | mk_mset T (x :: xs) = \<^Const>\<open>plus \<open>msetT T\<close> for \<open>mk_mset T [x]\<close> \<open>mk_mset T xs\<close>\<close> | |
| 60606 | 3809 | |
| 60752 | 3810 | fun mset_member_tac ctxt m i = | 
| 60606 | 3811 | if m <= 0 then | 
| 60752 | 3812 |         resolve_tac ctxt @{thms multi_member_this} i ORELSE
 | 
| 3813 |         resolve_tac ctxt @{thms multi_member_last} i
 | |
| 60606 | 3814 | else | 
| 60752 | 3815 |         resolve_tac ctxt @{thms multi_member_skip} i THEN mset_member_tac ctxt (m - 1) i
 | 
| 3816 | ||
| 3817 | fun mset_nonempty_tac ctxt = | |
| 3818 |       resolve_tac ctxt @{thms nonempty_plus} ORELSE'
 | |
| 3819 |       resolve_tac ctxt @{thms nonempty_single}
 | |
| 29125 
d41182a8135c
method "sizechange" proves termination of functions; added more infrastructure for termination proofs
 krauss parents: 
28823diff
changeset | 3820 | |
| 60606 | 3821 | fun regroup_munion_conv ctxt = | 
| 73393 | 3822 | Function_Lib.regroup_conv ctxt \<^const_abbrev>\<open>empty_mset\<close> \<^const_name>\<open>plus\<close> | 
| 60606 | 3823 |         (map (fn t => t RS eq_reflection) (@{thms ac_simps} @ @{thms empty_neutral}))
 | 
| 3824 | ||
| 60752 | 3825 | fun unfold_pwleq_tac ctxt i = | 
| 3826 |       (resolve_tac ctxt @{thms pw_leq_step} i THEN (fn st => unfold_pwleq_tac ctxt (i + 1) st))
 | |
| 3827 |         ORELSE (resolve_tac ctxt @{thms pw_leq_lstep} i)
 | |
| 3828 |         ORELSE (resolve_tac ctxt @{thms pw_leq_empty} i)
 | |
| 60606 | 3829 | |
| 3830 |     val set_mset_simps = [@{thm set_mset_empty}, @{thm set_mset_single}, @{thm set_mset_union},
 | |
| 3831 |                         @{thm Un_insert_left}, @{thm Un_empty_left}]
 | |
| 3832 | in | |
| 3833 | ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset | |
| 3834 |     {
 | |
| 3835 | msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv, | |
| 3836 | mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac, | |
| 3837 | mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_mset_simps, | |
| 3838 |       smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
 | |
| 60752 | 3839 |       reduction_pair = @{thm ms_reduction_pair}
 | 
| 60606 | 3840 | }) | 
| 3841 | end | |
| 60500 | 3842 | \<close> | 
| 3843 | ||
| 3844 | ||
| 3845 | 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 | 3846 | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39301diff
changeset | 3847 | 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 | 3848 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3849 | 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 | 3850 | 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 | 3851 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3852 | 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 | 3853 | 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 | 3854 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3855 | 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 | 3856 | 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 | 3857 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3858 | 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 | 3859 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3860 | 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 | 3861 | 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 | 3862 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3863 | 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 | 3864 | 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 | 3865 | |
| 
e97b22500a5c
cleanup of Multiset.thy: less duplication, tuned and simplified a couple of proofs, less historical organization of sections, conversion from associations lists to multisets, rudimentary code generation
 haftmann parents: 
33102diff
changeset | 3866 | lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y" | 
| 59557 | 3867 | 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 | 3868 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3869 | 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 | 3870 | by (fact subset_mset.less_trans) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3871 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3872 | 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 | 3873 | by (fact subset_mset.inf.commute) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3874 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3875 | 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 | 3876 | by (fact subset_mset.inf.assoc [symmetric]) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3877 | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3878 | 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 | 3879 | by (fact subset_mset.inf.left_commute) | 
| 35268 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3880 | |
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3881 | lemmas multiset_inter_ac = | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3882 | multiset_inter_commute | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3883 | multiset_inter_assoc | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3884 | multiset_inter_left_commute | 
| 
04673275441a
switched notations for pointwise and multiset order
 haftmann parents: 
35028diff
changeset | 3885 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3886 | 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 | 3887 | by (fact less_irrefl) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3888 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3889 | 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 | 3890 | by (fact less_trans) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3891 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3892 | 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 | 3893 | by (fact less_not_sym) | 
| 
a095acd4cfbf
instantiate multiset with multiset ordering
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63360diff
changeset | 3894 | |
| 63410 
9789ccc2a477
more instantiations for multiset
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63409diff
changeset | 3895 | 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 | 3896 | 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 | 3897 | |
| 60500 | 3898 | declaration \<open> | 
| 60606 | 3899 | let | 
| 3900 | fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T])) (Const _ $ t') = | |
| 3901 | let | |
| 3902 | val (maybe_opt, ps) = | |
| 3903 | Nitpick_Model.dest_plain_fun t' | |
| 67398 | 3904 | ||> (~~) | 
| 60606 | 3905 | ||> map (apsnd (snd o HOLogic.dest_number)) | 
| 3906 | fun elems_for t = | |
| 67398 | 3907 | (case AList.lookup (=) ps t of | 
| 60606 | 3908 | SOME n => replicate n t | 
| 3909 | | NONE => [Const (maybe_name, elem_T --> elem_T) $ t]) | |
| 3910 | in | |
| 3911 | (case maps elems_for (all_values elem_T) @ | |
| 61333 | 3912 | (if maybe_opt then [Const (Nitpick_Model.unrep_mixfix (), elem_T)] else []) of | 
| 74634 | 3913 | [] => \<^Const>\<open>Groups.zero T\<close> | 
| 3914 | | ts => foldl1 (fn (s, t) => \<^Const>\<open>add_mset elem_T for s t\<close>) ts) | |
| 60606 | 3915 | end | 
| 3916 | | multiset_postproc _ _ _ _ t = t | |
| 69593 | 3917 | in Nitpick_Model.register_term_postprocessor \<^typ>\<open>'a multiset\<close> multiset_postproc end | 
| 60500 | 3918 | \<close> | 
| 3919 | ||
| 3920 | ||
| 3921 | 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 | 3922 | |
| 60515 | 3923 | code_datatype mset | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3924 | |
| 60606 | 3925 | lemma [code]: "{#} = mset []"
 | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3926 | by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3927 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3928 | 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 | 3929 | by simp | 
| 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 3930 | |
| 63195 | 3931 | lemma [code]: "Multiset.is_empty (mset xs) \<longleftrightarrow> List.null xs" | 
| 3932 | by (simp add: Multiset.is_empty_def List.null_def) | |
| 3933 | ||
| 60606 | 3934 | 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 | 3935 | by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3936 | |
| 60606 | 3937 | 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 | 3938 | by simp | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3939 | |
| 60606 | 3940 | lemma [code]: "filter_mset f (mset xs) = mset (filter f xs)" | 
| 69442 | 3941 | by simp | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3942 | |
| 60606 | 3943 | 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 | 3944 | 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 | 3945 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3946 | lemma [code]: | 
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3947 | "mset xs \<inter># mset ys = | 
| 60515 | 3948 | 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 | 3949 | 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 | 3950 | proof - | 
| 60515 | 3951 | 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 | 3952 | 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 | 3953 | (mset xs \<inter># mset ys) + mset zs" | 
| 51623 | 3954 | by (induct xs arbitrary: ys) | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 3955 | (auto simp add: inter_add_right1 inter_add_right2 ac_simps) | 
| 51623 | 3956 | then show ?thesis by simp | 
| 3957 | qed | |
| 3958 | ||
| 3959 | lemma [code]: | |
| 63919 
9aed2da07200
# after multiset intersection and union symbol
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63908diff
changeset | 3960 | "mset xs \<union># mset ys = | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61378diff
changeset | 3961 | mset (case_prod append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))" | 
| 51623 | 3962 | proof - | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61378diff
changeset | 3963 | 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 | 3964 | (mset xs \<union># mset ys) + mset zs" | 
| 51623 | 3965 | 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 | 3966 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3967 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3968 | |
| 59813 | 3969 | 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 | 3970 | |
| 60606 | 3971 | 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 | 3972 | proof - | 
| 60515 | 3973 | 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 | 3974 | by (induct xs) simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3975 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3976 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3977 | |
| 60515 | 3978 | declare set_mset_mset [code] | 
| 3979 | ||
| 3980 | 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 | 3981 | |
| 61585 | 3982 | lemma [code]: \<comment> \<open>not very efficient, but representation-ignorant!\<close> | 
| 60515 | 3983 | "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 | 3984 | apply (cases "finite A") | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3985 | apply simp_all | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3986 | apply (induct A rule: finite_induct) | 
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 3987 | apply simp_all | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3988 | done | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3989 | |
| 60515 | 3990 | declare size_mset [code] | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 3991 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3992 | 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 | 3993 | "subset_eq_mset_impl [] ys = Some (ys \<noteq> [])" | 
| 67398 | 3994 | | "subset_eq_mset_impl (Cons x xs) ys = (case List.extract ((=) x) ys of | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 3995 | None \<Rightarrow> None | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3996 | | 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 | 3997 | |
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 3998 | 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 | 3999 | (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 | 4000 | (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 | 4001 | proof (induct xs arbitrary: ys) | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4002 | case (Nil ys) | 
| 64076 | 4003 | 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 | 4004 | next | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4005 | case (Cons x xs ys) | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4006 | show ?case | 
| 67398 | 4007 | proof (cases "List.extract ((=) x) ys") | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4008 | case None | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4009 | 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 | 4010 |     {
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 4011 | assume "mset (x # xs) \<subseteq># mset ys" | 
| 60495 | 4012 | 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 | 4013 | } note nle = this | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4014 | moreover | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4015 |     {
 | 
| 62430 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 4016 | assume "mset (x # xs) \<subset># mset ys" | 
| 
9527ff088c15
more succint formulation of membership for multisets, similar to lists;
 haftmann parents: 
62390diff
changeset | 4017 | 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 | 4018 | 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 | 4019 | } | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4020 | 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 | 4021 | next | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4022 | case (Some res) | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4023 | 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 | 4024 | note Some = Some[unfolded res] | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4025 | 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 | 4026 | 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 | 4027 | by auto | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 4028 | 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 | 4029 | unfolding Some option.simps split | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4030 | unfolding id | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4031 | using Cons[of "ys1 @ ys2"] | 
| 60397 
f8a513fedb31
Renaming multiset operators < ~> <#,...
 Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
59999diff
changeset | 4032 | 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 | 4033 | qed | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4034 | qed | 
| 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4035 | |
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 4036 | 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 | 4037 | 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 | 4038 | |
| 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 4039 | 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 | 4040 | 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 | 4041 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4042 | instantiation multiset :: (equal) equal | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4043 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4044 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4045 | definition | 
| 55808 
488c3e8282c8
added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
 nipkow parents: 
55565diff
changeset | 4046 | [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 | 4047 | 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 | 4048 | unfolding equal_multiset_def | 
| 63310 
caaacf37943f
normalising multiset theorem names
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63290diff
changeset | 4049 | 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 | 4050 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4051 | instance | 
| 60678 | 4052 | by standard (simp add: equal_multiset_def) | 
| 4053 | ||
| 37169 
f69efa106feb
make Nitpick "show_all" option behave less surprisingly
 blanchet parents: 
37107diff
changeset | 4054 | end | 
| 49388 | 4055 | |
| 66313 | 4056 | declare sum_mset_sum_list [code] | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4057 | |
| 63830 | 4058 | 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 | 4059 | proof - | 
| 63830 | 4060 | 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 | 4061 | 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 | 4062 | then show ?thesis by simp | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4063 | qed | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4064 | |
| 60500 | 4065 | text \<open> | 
| 69593 | 4066 | Exercise for the casual reader: add implementations for \<^term>\<open>(\<le>)\<close> | 
| 4067 | and \<^term>\<open>(<)\<close> (multiset order). | |
| 60500 | 4068 | \<close> | 
| 4069 | ||
| 4070 | text \<open>Quickcheck generators\<close> | |
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4071 | |
| 72607 | 4072 | context | 
| 4073 | includes term_syntax | |
| 4074 | begin | |
| 4075 | ||
| 4076 | definition | |
| 61076 | 4077 | 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 | 4078 | \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where | 
| 60515 | 4079 |   [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 | 4080 | |
| 72607 | 4081 | end | 
| 4082 | ||
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4083 | instantiation multiset :: (random) random | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4084 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4085 | |
| 72581 | 4086 | context | 
| 4087 | includes state_combinator_syntax | |
| 4088 | begin | |
| 4089 | ||
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4090 | definition | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4091 | "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 | 4092 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4093 | instance .. | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4094 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4095 | end | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4096 | |
| 72581 | 4097 | end | 
| 51600 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4098 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4099 | instantiation multiset :: (full_exhaustive) full_exhaustive | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4100 | begin | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4101 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4102 | 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 | 4103 | where | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4104 | "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 | 4105 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4106 | instance .. | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4107 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4108 | end | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4109 | |
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4110 | hide_const (open) msetify | 
| 
197e25f13f0c
default implementation of multisets by list with reasonable coverage of operations on multisets
 haftmann parents: 
51599diff
changeset | 4111 | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4112 | |
| 60500 | 4113 | subsection \<open>BNF setup\<close> | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4114 | |
| 57966 | 4115 | definition rel_mset where | 
| 60515 | 4116 | "rel_mset R X Y \<longleftrightarrow> (\<exists>xs ys. mset xs = X \<and> mset ys = Y \<and> list_all2 R xs ys)" | 
| 4117 | ||
| 4118 | lemma mset_zip_take_Cons_drop_twice: | |
| 57966 | 4119 | assumes "length xs = length ys" "j \<le> length xs" | 
| 60515 | 4120 | 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 | 4121 | add_mset (x,y) (mset (zip xs ys))" | 
| 60606 | 4122 | using assms | 
| 57966 | 4123 | proof (induct xs ys arbitrary: x y j rule: list_induct2) | 
| 4124 | case Nil | |
| 4125 | thus ?case | |
| 4126 | by simp | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4127 | next | 
| 57966 | 4128 | case (Cons x xs y ys) | 
| 4129 | thus ?case | |
| 4130 | proof (cases "j = 0") | |
| 4131 | case True | |
| 4132 | thus ?thesis | |
| 4133 | by simp | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4134 | next | 
| 57966 | 4135 | case False | 
| 4136 | then obtain k where k: "j = Suc k" | |
| 60678 | 4137 | by (cases j) simp | 
| 57966 | 4138 | hence "k \<le> length xs" | 
| 4139 | using Cons.prems by auto | |
| 60515 | 4140 | 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 | 4141 | add_mset (x,y) (mset (zip xs ys))" | 
| 57966 | 4142 | by (rule Cons.hyps(2)) | 
| 4143 | thus ?thesis | |
| 63794 
bcec0534aeea
clean argument of simp add
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63793diff
changeset | 4144 | unfolding k by auto | 
| 58425 | 4145 | qed | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4146 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4147 | |
| 60515 | 4148 | lemma ex_mset_zip_left: | 
| 4149 | assumes "length xs = length ys" "mset xs' = mset xs" | |
| 4150 | shows "\<exists>ys'. length ys' = length xs' \<and> mset (zip xs' ys') = mset (zip xs ys)" | |
| 58425 | 4151 | using assms | 
| 57966 | 4152 | proof (induct xs ys arbitrary: xs' rule: list_induct2) | 
| 4153 | case Nil | |
| 4154 | thus ?case | |
| 4155 | by auto | |
| 4156 | next | |
| 4157 | case (Cons x xs y ys xs') | |
| 4158 | obtain j where j_len: "j < length xs'" and nth_j: "xs' ! j = x" | |
| 60515 | 4159 | by (metis Cons.prems in_set_conv_nth list.set_intros(1) mset_eq_setD) | 
| 58425 | 4160 | |
| 63040 | 4161 | define xsa where "xsa = take j xs' @ drop (Suc j) xs'" | 
| 60515 | 4162 |   have "mset xs' = {#x#} + mset xsa"
 | 
| 57966 | 4163 | 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 | 4164 | 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 | 4165 | append_take_drop_id mset.simps(2) mset_append) | 
| 60515 | 4166 | hence ms_x: "mset xsa = mset xs" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4167 | by (simp add: Cons.prems) | 
| 57966 | 4168 | then obtain ysa where | 
| 60515 | 4169 | len_a: "length ysa = length xsa" and ms_a: "mset (zip xsa ysa) = mset (zip xs ys)" | 
| 57966 | 4170 | using Cons.hyps(2) by blast | 
| 4171 | ||
| 63040 | 4172 | define ys' where "ys' = take j ysa @ y # drop j ysa" | 
| 57966 | 4173 | have xs': "xs' = take j xsa @ x # drop j xsa" | 
| 4174 | 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 | 4175 | by (metis append_eq_append_conv append_take_drop_id diff_Suc_Suc Cons_nth_drop_Suc length_Cons | 
| 60515 | 4176 | length_drop size_mset) | 
| 57966 | 4177 | have j_len': "j \<le> length xsa" | 
| 4178 | using j_len xs' xsa_def | |
| 4179 | by (metis add_Suc_right append_take_drop_id length_Cons length_append less_eq_Suc_le not_less) | |
| 4180 | have "length ys' = length xs'" | |
| 4181 | unfolding ys'_def using Cons.prems len_a ms_x | |
| 60515 | 4182 | by (metis add_Suc_right append_take_drop_id length_Cons length_append mset_eq_length) | 
| 4183 | moreover have "mset (zip xs' ys') = mset (zip (x # xs) (y # ys))" | |
| 57966 | 4184 | unfolding xs' ys'_def | 
| 60515 | 4185 | 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 | 4186 | (auto simp: len_a ms_a j_len') | 
| 57966 | 4187 | ultimately show ?case | 
| 4188 | by blast | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4189 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4190 | |
| 57966 | 4191 | lemma list_all2_reorder_left_invariance: | 
| 60515 | 4192 | assumes rel: "list_all2 R xs ys" and ms_x: "mset xs' = mset xs" | 
| 4193 | shows "\<exists>ys'. list_all2 R xs' ys' \<and> mset ys' = mset ys" | |
| 57966 | 4194 | proof - | 
| 4195 | have len: "length xs = length ys" | |
| 4196 | using rel list_all2_conv_all_nth by auto | |
| 4197 | obtain ys' where | |
| 60515 | 4198 | len': "length xs' = length ys'" and ms_xy: "mset (zip xs' ys') = mset (zip xs ys)" | 
| 4199 | using len ms_x by (metis ex_mset_zip_left) | |
| 57966 | 4200 | have "list_all2 R xs' ys'" | 
| 60515 | 4201 | using assms(1) len' ms_xy unfolding list_all2_iff by (blast dest: mset_eq_setD) | 
| 4202 | moreover have "mset ys' = mset ys" | |
| 4203 | using len len' ms_xy map_snd_zip mset_map by metis | |
| 57966 | 4204 | ultimately show ?thesis | 
| 4205 | by blast | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4206 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4207 | |
| 60515 | 4208 | lemma ex_mset: "\<exists>xs. mset xs = X" | 
| 4209 | 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 | 4210 | |
| 73301 | 4211 | inductive pred_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> bool" 
 | 
| 62324 | 4212 | where | 
| 4213 |   "pred_mset P {#}"
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4214 | | "\<lbrakk>P a; pred_mset P M\<rbrakk> \<Longrightarrow> pred_mset P (add_mset a M)" | 
| 62324 | 4215 | |
| 73301 | 4216 | lemma pred_mset_iff: \<comment> \<open>TODO: alias for \<^const>\<open>Multiset.Ball\<close>\<close> | 
| 4217 | \<open>pred_mset P M \<longleftrightarrow> Multiset.Ball M P\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) | |
| 4218 | proof | |
| 4219 | assume ?P | |
| 4220 | then show ?Q by induction simp_all | |
| 4221 | next | |
| 4222 | assume ?Q | |
| 4223 | then show ?P | |
| 4224 | by (induction M) (auto intro: pred_mset.intros) | |
| 4225 | qed | |
| 4226 | ||
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4227 | bnf "'a multiset" | 
| 57966 | 4228 | map: image_mset | 
| 60495 | 4229 | sets: set_mset | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4230 | bd: natLeq | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4231 |   wits: "{#}"
 | 
| 57966 | 4232 | rel: rel_mset | 
| 62324 | 4233 | pred: pred_mset | 
| 57966 | 4234 | proof - | 
| 4235 | show "image_mset id = id" | |
| 4236 | by (rule image_mset.id) | |
| 60606 | 4237 | show "image_mset (g \<circ> f) = image_mset g \<circ> image_mset f" for f g | 
| 59813 | 4238 | unfolding comp_def by (rule ext) (simp add: comp_def image_mset.compositionality) | 
| 60606 | 4239 | 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 | 4240 | by (induct X) simp_all | 
| 67398 | 4241 | show "set_mset \<circ> image_mset f = (`) f \<circ> set_mset" for f | 
| 57966 | 4242 | by auto | 
| 4243 | show "card_order natLeq" | |
| 4244 | by (rule natLeq_card_order) | |
| 4245 | show "BNF_Cardinal_Arithmetic.cinfinite natLeq" | |
| 4246 | by (rule natLeq_cinfinite) | |
| 75624 | 4247 | show "regularCard natLeq" | 
| 4248 | by (rule regularCard_natLeq) | |
| 4249 | show "ordLess2 (card_of (set_mset X)) natLeq" for X | |
| 57966 | 4250 | by transfer | 
| 75624 | 4251 | (auto simp: finite_iff_ordLess_natLeq[symmetric]) | 
| 60606 | 4252 | show "rel_mset R OO rel_mset S \<le> rel_mset (R OO S)" for R S | 
| 57966 | 4253 | unfolding rel_mset_def[abs_def] OO_def | 
| 4254 | apply clarify | |
| 60678 | 4255 | subgoal for X Z Y xs ys' ys zs | 
| 4256 | apply (drule list_all2_reorder_left_invariance [where xs = ys' and ys = zs and xs' = ys]) | |
| 4257 | apply (auto intro: list_all2_trans) | |
| 4258 | done | |
| 60606 | 4259 | done | 
| 4260 | show "rel_mset R = | |
| 62324 | 4261 |     (\<lambda>x y. \<exists>z. set_mset z \<subseteq> {(x, y). R x y} \<and>
 | 
| 4262 | image_mset fst z = x \<and> image_mset snd z = y)" for R | |
| 4263 | unfolding rel_mset_def[abs_def] | |
| 57966 | 4264 | apply (rule ext)+ | 
| 62324 | 4265 | apply safe | 
| 4266 | apply (rule_tac x = "mset (zip xs ys)" in exI; | |
| 68406 | 4267 | auto simp: in_set_zip list_all2_iff simp flip: mset_map) | 
| 57966 | 4268 | apply (rename_tac XY) | 
| 60515 | 4269 | apply (cut_tac X = XY in ex_mset) | 
| 57966 | 4270 | apply (erule exE) | 
| 4271 | apply (rename_tac xys) | |
| 4272 | apply (rule_tac x = "map fst xys" in exI) | |
| 60515 | 4273 | apply (auto simp: mset_map) | 
| 57966 | 4274 | apply (rule_tac x = "map snd xys" in exI) | 
| 60515 | 4275 | apply (auto simp: mset_map list_all2I subset_eq zip_map_fst_snd) | 
| 59997 | 4276 | done | 
| 60606 | 4277 |   show "z \<in> set_mset {#} \<Longrightarrow> False" for z
 | 
| 57966 | 4278 | by auto | 
| 62324 | 4279 | show "pred_mset P = (\<lambda>x. Ball (set_mset x) P)" for P | 
| 73301 | 4280 | by (simp add: fun_eq_iff pred_mset_iff) | 
| 57966 | 4281 | qed | 
| 4282 | ||
| 73301 | 4283 | inductive rel_mset' :: \<open>('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset \<Rightarrow> bool\<close>
 | 
| 60606 | 4284 | where | 
| 57966 | 4285 |   Zero[intro]: "rel_mset' R {#} {#}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4286 | | 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 | 4287 | |
| 4288 | lemma rel_mset_Zero: "rel_mset R {#} {#}"
 | |
| 4289 | 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 | 4290 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4291 | declare multiset.count[simp] | 
| 73270 | 4292 | declare count_Abs_multiset[simp] | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4293 | declare multiset.count_inverse[simp] | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4294 | |
| 57966 | 4295 | lemma rel_mset_Plus: | 
| 60606 | 4296 | assumes ab: "R a b" | 
| 4297 | and MN: "rel_mset R M N" | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4298 | shows "rel_mset R (add_mset a M) (add_mset b N)" | 
| 60606 | 4299 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4300 | 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 | 4301 | add_mset b (image_mset snd y) = image_mset snd ya \<and> | 
| 60606 | 4302 |     set_mset ya \<subseteq> {(x, y). R x y}"
 | 
| 4303 |     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 | 4304 | 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 | 4305 | thus ?thesis | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4306 | using assms | 
| 57966 | 4307 | 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 | 4308 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4309 | |
| 60606 | 4310 | lemma rel_mset'_imp_rel_mset: "rel_mset' R M N \<Longrightarrow> rel_mset R M N" | 
| 60678 | 4311 | by (induct rule: rel_mset'.induct) (auto simp: rel_mset_Zero rel_mset_Plus) | 
| 57966 | 4312 | |
| 60606 | 4313 | lemma rel_mset_size: "rel_mset R M N \<Longrightarrow> size M = size N" | 
| 60678 | 4314 | 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 | 4315 | |
| 73594 | 4316 | lemma rel_mset_Zero_iff [simp]: | 
| 4317 |   shows "rel_mset rel {#} Y \<longleftrightarrow> Y = {#}" and "rel_mset rel X {#} \<longleftrightarrow> X = {#}"
 | |
| 4318 | by (auto simp add: rel_mset_Zero dest: rel_mset_size) | |
| 4319 | ||
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4320 | lemma multiset_induct2[case_names empty addL addR]: | 
| 60678 | 4321 |   assumes empty: "P {#} {#}"
 | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4322 | 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 | 4323 | and addR: "\<And>a M N. P M N \<Longrightarrow> P M (add_mset a N)" | 
| 60678 | 4324 | shows "P M N" | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4325 | apply(induct N rule: multiset_induct) | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4326 | 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 | 4327 | 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 | 4328 | done | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4329 | |
| 59949 | 4330 | lemma multiset_induct2_size[consumes 1, case_names empty add]: | 
| 60606 | 4331 | assumes c: "size M = size N" | 
| 4332 |     and empty: "P {#} {#}"
 | |
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4333 | and add: "\<And>a b M N a b. P M N \<Longrightarrow> P (add_mset a M) (add_mset b N)" | 
| 60606 | 4334 | shows "P M N" | 
| 60678 | 4335 | using c | 
| 4336 | proof (induct M arbitrary: N rule: measure_induct_rule[of size]) | |
| 60606 | 4337 | case (less M) | 
| 4338 | show ?case | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4339 |   proof(cases "M = {#}")
 | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4340 |     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 | 4341 | thus ?thesis using True empty by auto | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4342 | next | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4343 | 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 | 4344 |     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 | 4345 | then obtain N1 b where N: "N = add_mset b N1" by (metis multi_nonempty_split) | 
| 59949 | 4346 | 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 | 4347 | 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 | 4348 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4349 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4350 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4351 | lemma msed_map_invL: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4352 | 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 | 4353 | shows "\<exists>N1. N = add_mset (f a) N1 \<and> image_mset f M = N1" | 
| 60606 | 4354 | proof - | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4355 | have "f a \<in># N" | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4356 | 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 | 4357 | then obtain N1 where N: "N = add_mset (f a) N1" using multi_member_split by metis | 
| 57966 | 4358 | 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 | 4359 | thus ?thesis using N by blast | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4360 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4361 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4362 | lemma msed_map_invR: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4363 | 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 | 4364 | shows "\<exists>M1 a. M = add_mset a M1 \<and> f a = b \<and> image_mset f M1 = N" | 
| 60606 | 4365 | proof - | 
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4366 | obtain a where a: "a \<in># M" and fa: "f a = b" | 
| 60606 | 4367 | 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 | 4368 | 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 | 4369 | then obtain M1 where M: "M = add_mset a M1" using multi_member_split by metis | 
| 57966 | 4370 | 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 | 4371 | thus ?thesis using M fa by blast | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4372 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4373 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4374 | lemma msed_rel_invL: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4375 | 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 | 4376 | shows "\<exists>N1 b. N = add_mset b N1 \<and> R a b \<and> rel_mset R M N1" | 
| 60606 | 4377 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4378 | obtain K where KM: "image_mset fst K = add_mset a M" | 
| 60606 | 4379 |     and KN: "image_mset snd K = N" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
 | 
| 4380 | using assms | |
| 4381 | 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 | 4382 | obtain K1 ab where K: "K = add_mset ab K1" and a: "fst ab = a" | 
| 60606 | 4383 | 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 | 4384 | obtain N1 where N: "N = add_mset (snd ab) N1" and K1N1: "image_mset snd K1 = N1" | 
| 60606 | 4385 | 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 | 4386 | have Rab: "R a (snd ab)" using sK a unfolding K by auto | 
| 57966 | 4387 | have "rel_mset R M N1" using sK K1M K1N1 | 
| 60606 | 4388 | 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 | 4389 | thus ?thesis using N Rab by auto | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4390 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4391 | |
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4392 | lemma msed_rel_invR: | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4393 | 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 | 4394 | shows "\<exists>M1 a. M = add_mset a M1 \<and> R a b \<and> rel_mset R M1 N" | 
| 60606 | 4395 | proof - | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4396 | obtain K where KN: "image_mset snd K = add_mset b N" | 
| 60606 | 4397 |     and KM: "image_mset fst K = M" and sK: "set_mset K \<subseteq> {(a, b). R a b}"
 | 
| 4398 | using assms | |
| 4399 | 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 | 4400 | obtain K1 ab where K: "K = add_mset ab K1" and b: "snd ab = b" | 
| 60606 | 4401 | 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 | 4402 | obtain M1 where M: "M = add_mset (fst ab) M1" and K1M1: "image_mset fst K1 = M1" | 
| 60606 | 4403 | 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 | 4404 | have Rab: "R (fst ab) b" using sK b unfolding K by auto | 
| 57966 | 4405 | have "rel_mset R M1 N" using sK K1N K1M1 | 
| 60606 | 4406 | 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 | 4407 | thus ?thesis using M Rab by auto | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4408 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4409 | |
| 57966 | 4410 | lemma rel_mset_imp_rel_mset': | 
| 60606 | 4411 | assumes "rel_mset R M N" | 
| 4412 | shows "rel_mset' R M N" | |
| 59949 | 4413 | 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 | 4414 | case (less M) | 
| 59949 | 4415 | 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 | 4416 | show ?case | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4417 |   proof(cases "M = {#}")
 | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4418 |     case True hence "N = {#}" using c by simp
 | 
| 57966 | 4419 | 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 | 4420 | next | 
| 63793 
e68a0b651eb5
add_mset constructor in multisets
 fleury <Mathias.Fleury@mpi-inf.mpg.de> parents: 
63689diff
changeset | 4421 | 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 | 4422 | obtain N1 b where N: "N = add_mset b N1" and R: "R a b" and ms: "rel_mset R M1 N1" | 
| 60606 | 4423 | using msed_rel_invL[OF less.prems[unfolded M]] by auto | 
| 57966 | 4424 | have "rel_mset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp | 
| 4425 | 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 | 4426 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4427 | qed | 
| 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4428 | |
| 60606 | 4429 | lemma rel_mset_rel_mset': "rel_mset R M N = rel_mset' R M N" | 
| 60678 | 4430 | using rel_mset_imp_rel_mset' rel_mset'_imp_rel_mset by auto | 
| 57966 | 4431 | |
| 69593 | 4432 | text \<open>The main end product for \<^const>\<open>rel_mset\<close>: inductive characterization:\<close> | 
| 61337 | 4433 | lemmas rel_mset_induct[case_names empty add, induct pred: rel_mset] = | 
| 60606 | 4434 | 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 | 4435 | |
| 56656 | 4436 | |
| 60500 | 4437 | subsection \<open>Size setup\<close> | 
| 56656 | 4438 | |
| 67332 | 4439 | lemma size_multiset_o_map: "size_multiset g \<circ> image_mset f = size_multiset (g \<circ> f)" | 
| 65547 | 4440 | apply (rule ext) | 
| 4441 | subgoal for x by (induct x) auto | |
| 4442 | done | |
| 56656 | 4443 | |
| 60500 | 4444 | setup \<open> | 
| 69593 | 4445 | BNF_LFP_Size.register_size_global \<^type_name>\<open>multiset\<close> \<^const_name>\<open>size_multiset\<close> | 
| 62082 | 4446 |     @{thm size_multiset_overloaded_def}
 | 
| 60606 | 4447 |     @{thms size_multiset_empty size_multiset_single size_multiset_union size_empty size_single
 | 
| 4448 | size_union} | |
| 67332 | 4449 |     @{thms size_multiset_o_map}
 | 
| 60500 | 4450 | \<close> | 
| 56656 | 4451 | |
| 65547 | 4452 | subsection \<open>Lemmas about Size\<close> | 
| 4453 | ||
| 4454 | lemma size_mset_SucE: "size A = Suc n \<Longrightarrow> (\<And>a B. A = {#a#} + B \<Longrightarrow> size B = n \<Longrightarrow> P) \<Longrightarrow> P"
 | |
| 4455 | by (cases A) (auto simp add: ac_simps) | |
| 4456 | ||
| 4457 | lemma size_Suc_Diff1: "x \<in># M \<Longrightarrow> Suc (size (M - {#x#})) = size M"
 | |
| 4458 | using arg_cong[OF insert_DiffM, of _ _ size] by simp | |
| 4459 | ||
| 4460 | lemma size_Diff_singleton: "x \<in># M \<Longrightarrow> size (M - {#x#}) = size M - 1"
 | |
| 68406 | 4461 | by (simp flip: size_Suc_Diff1) | 
| 65547 | 4462 | |
| 4463 | lemma size_Diff_singleton_if: "size (A - {#x#}) = (if x \<in># A then size A - 1 else size A)"
 | |
| 4464 | by (simp add: diff_single_trivial size_Diff_singleton) | |
| 4465 | ||
| 4466 | lemma size_Un_Int: "size A + size B = size (A \<union># B) + size (A \<inter># B)" | |
| 4467 | by (metis inter_subset_eq_union size_union subset_mset.diff_add union_diff_inter_eq_sup) | |
| 4468 | ||
| 4469 | lemma size_Un_disjoint: "A \<inter># B = {#} \<Longrightarrow> size (A \<union># B) = size A + size B"
 | |
| 4470 | using size_Un_Int[of A B] by simp | |
| 4471 | ||
| 4472 | lemma size_Diff_subset_Int: "size (M - M') = size M - size (M \<inter># M')" | |
| 4473 | by (metis diff_intersect_left_idem size_Diff_submset subset_mset.inf_le1) | |
| 4474 | ||
| 4475 | lemma diff_size_le_size_Diff: "size (M :: _ multiset) - size M' \<le> size (M - M')" | |
| 4476 | by (simp add: diff_le_mono2 size_Diff_subset_Int size_mset_mono) | |
| 4477 | ||
| 4478 | lemma size_Diff1_less: "x\<in># M \<Longrightarrow> size (M - {#x#}) < size M"
 | |
| 4479 | by (rule Suc_less_SucD) (simp add: size_Suc_Diff1) | |
| 4480 | ||
| 4481 | lemma size_Diff2_less: "x\<in># M \<Longrightarrow> y\<in># M \<Longrightarrow> size (M - {#x#} - {#y#}) < size M"
 | |
| 4482 | by (metis less_imp_diff_less size_Diff1_less size_Diff_subset_Int) | |
| 4483 | ||
| 4484 | lemma size_Diff1_le: "size (M - {#x#}) \<le> size M"
 | |
| 4485 | by (cases "x \<in># M") (simp_all add: size_Diff1_less less_imp_le diff_single_trivial) | |
| 4486 | ||
| 4487 | lemma size_psubset: "M \<subseteq># M' \<Longrightarrow> size M < size M' \<Longrightarrow> M \<subset># M'" | |
| 4488 | using less_irrefl subset_mset_def by blast | |
| 4489 | ||
| 76700 
c48fe2be847f
added lifting_forget as suggested by Peter Lammich
 blanchet parents: 
76682diff
changeset | 4490 | lifting_update multiset.lifting | 
| 
c48fe2be847f
added lifting_forget as suggested by Peter Lammich
 blanchet parents: 
76682diff
changeset | 4491 | lifting_forget multiset.lifting | 
| 
c48fe2be847f
added lifting_forget as suggested by Peter Lammich
 blanchet parents: 
76682diff
changeset | 4492 | |
| 56656 | 4493 | hide_const (open) wcount | 
| 4494 | ||
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54868diff
changeset | 4495 | end |