src/HOL/Library/Multiset.thy
changeset 51548 757fa47af981
parent 51161 6ed12ae3b3e1
child 51599 1559e9266280
     1.1 --- a/src/HOL/Library/Multiset.thy	Tue Mar 26 22:09:39 2013 +0100
     1.2 +++ b/src/HOL/Library/Multiset.thy	Wed Mar 27 10:55:05 2013 +0100
     1.3 @@ -702,7 +702,7 @@
     1.4    then show ?thesis by simp
     1.5  qed
     1.6  
     1.7 -lemma fold_mset_fun_comm:
     1.8 +lemma fold_mset_fun_left_comm:
     1.9    "f x (fold f s M) = fold f (f x s) M"
    1.10    by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
    1.11  
    1.12 @@ -714,7 +714,7 @@
    1.13    case (add M x)
    1.14    have "M + {#x#} + N = (M + N) + {#x#}"
    1.15      by (simp add: add_ac)
    1.16 -  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_comm)
    1.17 +  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
    1.18  qed
    1.19  
    1.20  lemma fold_mset_fusion:
    1.21 @@ -821,9 +821,7 @@
    1.22  declare image_mset.identity [simp]
    1.23  
    1.24  
    1.25 -subsection {* Alternative representations *}
    1.26 -
    1.27 -subsubsection {* Lists *}
    1.28 +subsection {* Further conversions *}
    1.29  
    1.30  primrec multiset_of :: "'a list \<Rightarrow> 'a multiset" where
    1.31    "multiset_of [] = {#}" |
    1.32 @@ -950,6 +948,257 @@
    1.33    ultimately show ?case by simp
    1.34  qed
    1.35  
    1.36 +lemma multiset_of_insort [simp]:
    1.37 +  "multiset_of (insort x xs) = multiset_of xs + {#x#}"
    1.38 +  by (induct xs) (simp_all add: ac_simps)
    1.39 +
    1.40 +definition multiset_of_set :: "'a set \<Rightarrow> 'a multiset"
    1.41 +where
    1.42 +  "multiset_of_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
    1.43 +
    1.44 +interpretation multiset_of_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
    1.45 +where
    1.46 +  "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set"
    1.47 +proof -
    1.48 +  interpret comp_fun_commute "\<lambda>x M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
    1.49 +  show "folding (\<lambda>x M. {#x#} + M)" by default (fact comp_fun_commute)
    1.50 +  from multiset_of_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set" ..
    1.51 +qed
    1.52 +
    1.53 +context linorder
    1.54 +begin
    1.55 +
    1.56 +definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
    1.57 +where
    1.58 +  "sorted_list_of_multiset M = fold insort [] M"
    1.59 +
    1.60 +lemma sorted_list_of_multiset_empty [simp]:
    1.61 +  "sorted_list_of_multiset {#} = []"
    1.62 +  by (simp add: sorted_list_of_multiset_def)
    1.63 +
    1.64 +lemma sorted_list_of_multiset_singleton [simp]:
    1.65 +  "sorted_list_of_multiset {#x#} = [x]"
    1.66 +proof -
    1.67 +  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
    1.68 +  show ?thesis by (simp add: sorted_list_of_multiset_def)
    1.69 +qed
    1.70 +
    1.71 +lemma sorted_list_of_multiset_insert [simp]:
    1.72 +  "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
    1.73 +proof -
    1.74 +  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
    1.75 +  show ?thesis by (simp add: sorted_list_of_multiset_def)
    1.76 +qed
    1.77 +
    1.78 +end
    1.79 +
    1.80 +lemma multiset_of_sorted_list_of_multiset [simp]:
    1.81 +  "multiset_of (sorted_list_of_multiset M) = M"
    1.82 +  by (induct M) simp_all
    1.83 +
    1.84 +lemma sorted_list_of_multiset_multiset_of [simp]:
    1.85 +  "sorted_list_of_multiset (multiset_of xs) = sort xs"
    1.86 +  by (induct xs) simp_all
    1.87 +
    1.88 +lemma finite_set_of_multiset_of_set:
    1.89 +  assumes "finite A"
    1.90 +  shows "set_of (multiset_of_set A) = A"
    1.91 +  using assms by (induct A) simp_all
    1.92 +
    1.93 +lemma infinite_set_of_multiset_of_set:
    1.94 +  assumes "\<not> finite A"
    1.95 +  shows "set_of (multiset_of_set A) = {}"
    1.96 +  using assms by simp
    1.97 +
    1.98 +lemma set_sorted_list_of_multiset [simp]:
    1.99 +  "set (sorted_list_of_multiset M) = set_of M"
   1.100 +  by (induct M) (simp_all add: set_insort)
   1.101 +
   1.102 +lemma sorted_list_of_multiset_of_set [simp]:
   1.103 +  "sorted_list_of_multiset (multiset_of_set A) = sorted_list_of_set A"
   1.104 +  by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
   1.105 +
   1.106 +
   1.107 +subsection {* Big operators *}
   1.108 +
   1.109 +no_notation times (infixl "*" 70)
   1.110 +no_notation Groups.one ("1")
   1.111 +
   1.112 +locale comm_monoid_mset = comm_monoid
   1.113 +begin
   1.114 +
   1.115 +definition F :: "'a multiset \<Rightarrow> 'a"
   1.116 +where
   1.117 +  eq_fold: "F M = Multiset.fold f 1 M"
   1.118 +
   1.119 +lemma empty [simp]:
   1.120 +  "F {#} = 1"
   1.121 +  by (simp add: eq_fold)
   1.122 +
   1.123 +lemma singleton [simp]:
   1.124 +  "F {#x#} = x"
   1.125 +proof -
   1.126 +  interpret comp_fun_commute
   1.127 +    by default (simp add: fun_eq_iff left_commute)
   1.128 +  show ?thesis by (simp add: eq_fold)
   1.129 +qed
   1.130 +
   1.131 +lemma union [simp]:
   1.132 +  "F (M + N) = F M * F N"
   1.133 +proof -
   1.134 +  interpret comp_fun_commute f
   1.135 +    by default (simp add: fun_eq_iff left_commute)
   1.136 +  show ?thesis by (induct N) (simp_all add: left_commute eq_fold)
   1.137 +qed
   1.138 +
   1.139 +end
   1.140 +
   1.141 +notation times (infixl "*" 70)
   1.142 +notation Groups.one ("1")
   1.143 +
   1.144 +definition (in comm_monoid_add) msetsum :: "'a multiset \<Rightarrow> 'a"
   1.145 +where
   1.146 +  "msetsum = comm_monoid_mset.F plus 0"
   1.147 +
   1.148 +definition (in comm_monoid_mult) msetprod :: "'a multiset \<Rightarrow> 'a"
   1.149 +where
   1.150 +  "msetprod = comm_monoid_mset.F times 1"
   1.151 +
   1.152 +sublocale comm_monoid_add < msetsum!: comm_monoid_mset plus 0
   1.153 +where
   1.154 +  "comm_monoid_mset.F plus 0 = msetsum"
   1.155 +proof -
   1.156 +  show "comm_monoid_mset plus 0" ..
   1.157 +  from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
   1.158 +qed
   1.159 +
   1.160 +context comm_monoid_add
   1.161 +begin
   1.162 +
   1.163 +lemma setsum_unfold_msetsum:
   1.164 +  "setsum f A = msetsum (image_mset f (multiset_of_set A))"
   1.165 +  by (cases "finite A") (induct A rule: finite_induct, simp_all)
   1.166 +
   1.167 +abbreviation msetsum_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
   1.168 +where
   1.169 +  "msetsum_image f M \<equiv> msetsum (image_mset f M)"
   1.170 +
   1.171 +end
   1.172 +
   1.173 +syntax
   1.174 +  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
   1.175 +      ("(3SUM _:#_. _)" [0, 51, 10] 10)
   1.176 +
   1.177 +syntax (xsymbols)
   1.178 +  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
   1.179 +      ("(3\<Sum>_:#_. _)" [0, 51, 10] 10)
   1.180 +
   1.181 +syntax (HTML output)
   1.182 +  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
   1.183 +      ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
   1.184 +
   1.185 +translations
   1.186 +  "SUM i :# A. b" == "CONST msetsum_image (\<lambda>i. b) A"
   1.187 +
   1.188 +sublocale comm_monoid_mult < msetprod!: comm_monoid_mset times 1
   1.189 +where
   1.190 +  "comm_monoid_mset.F times 1 = msetprod"
   1.191 +proof -
   1.192 +  show "comm_monoid_mset times 1" ..
   1.193 +  from msetprod_def show "comm_monoid_mset.F times 1 = msetprod" ..
   1.194 +qed
   1.195 +
   1.196 +context comm_monoid_mult
   1.197 +begin
   1.198 +
   1.199 +lemma msetprod_empty:
   1.200 +  "msetprod {#} = 1"
   1.201 +  by (fact msetprod.empty)
   1.202 +
   1.203 +lemma msetprod_singleton:
   1.204 +  "msetprod {#x#} = x"
   1.205 +  by (fact msetprod.singleton)
   1.206 +
   1.207 +lemma msetprod_Un:
   1.208 +  "msetprod (A + B) = msetprod A * msetprod B" 
   1.209 +  by (fact msetprod.union)
   1.210 +
   1.211 +lemma setprod_unfold_msetprod:
   1.212 +  "setprod f A = msetprod (image_mset f (multiset_of_set A))"
   1.213 +  by (cases "finite A") (induct A rule: finite_induct, simp_all)
   1.214 +
   1.215 +lemma msetprod_multiplicity:
   1.216 +  "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_of M)"
   1.217 +  by (simp add: Multiset.fold_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
   1.218 +
   1.219 +abbreviation msetprod_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
   1.220 +where
   1.221 +  "msetprod_image f M \<equiv> msetprod (image_mset f M)"
   1.222 +
   1.223 +end
   1.224 +
   1.225 +syntax
   1.226 +  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
   1.227 +      ("(3PROD _:#_. _)" [0, 51, 10] 10)
   1.228 +
   1.229 +syntax (xsymbols)
   1.230 +  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
   1.231 +      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
   1.232 +
   1.233 +syntax (HTML output)
   1.234 +  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
   1.235 +      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
   1.236 +
   1.237 +translations
   1.238 +  "PROD i :# A. b" == "CONST msetprod_image (\<lambda>i. b) A"
   1.239 +
   1.240 +lemma (in comm_semiring_1) dvd_msetprod:
   1.241 +  assumes "x \<in># A"
   1.242 +  shows "x dvd msetprod A"
   1.243 +proof -
   1.244 +  from assms have "A = (A - {#x#}) + {#x#}" by simp
   1.245 +  then obtain B where "A = B + {#x#}" ..
   1.246 +  then show ?thesis by simp
   1.247 +qed
   1.248 +
   1.249 +
   1.250 +subsection {* Cardinality *}
   1.251 +
   1.252 +definition mcard :: "'a multiset \<Rightarrow> nat"
   1.253 +where
   1.254 +  "mcard = msetsum \<circ> image_mset (\<lambda>_. 1)"
   1.255 +
   1.256 +lemma mcard_empty [simp]:
   1.257 +  "mcard {#} = 0"
   1.258 +  by (simp add: mcard_def)
   1.259 +
   1.260 +lemma mcard_singleton [simp]:
   1.261 +  "mcard {#a#} = Suc 0"
   1.262 +  by (simp add: mcard_def)
   1.263 +
   1.264 +lemma mcard_plus [simp]:
   1.265 +  "mcard (M + N) = mcard M + mcard N"
   1.266 +  by (simp add: mcard_def)
   1.267 +
   1.268 +lemma mcard_empty_iff [simp]:
   1.269 +  "mcard M = 0 \<longleftrightarrow> M = {#}"
   1.270 +  by (induct M) simp_all
   1.271 +
   1.272 +lemma mcard_unfold_setsum:
   1.273 +  "mcard M = setsum (count M) (set_of M)"
   1.274 +proof (induct M)
   1.275 +  case empty then show ?case by simp
   1.276 +next
   1.277 +  case (add M x) then show ?case
   1.278 +    by (cases "x \<in> set_of M")
   1.279 +      (simp_all del: mem_set_of_iff add: setsum.distrib setsum.delta' insert_absorb, simp)
   1.280 +qed
   1.281 +
   1.282 +
   1.283 +subsection {* Alternative representations *}
   1.284 +
   1.285 +subsubsection {* Lists *}
   1.286 +
   1.287  context linorder
   1.288  begin
   1.289