src/HOL/Library/Multiset.thy
changeset 60513 55c7316f76d6
parent 60503 47df24e05b1c
child 60515 484559628038
equal deleted inserted replaced
60512:e0169291b31c 60513:55c7316f76d6
  1091 
  1091 
  1092 lemma multiset_of_map:
  1092 lemma multiset_of_map:
  1093   "multiset_of (map f xs) = image_mset f (multiset_of xs)"
  1093   "multiset_of (map f xs) = image_mset f (multiset_of xs)"
  1094   by (induct xs) simp_all
  1094   by (induct xs) simp_all
  1095 
  1095 
  1096 definition multiset_of_set :: "'a set \<Rightarrow> 'a multiset"
  1096 definition mset_set :: "'a set \<Rightarrow> 'a multiset"
  1097 where
  1097 where
  1098   "multiset_of_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
  1098   "mset_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
  1099 
  1099 
  1100 interpretation multiset_of_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
  1100 interpretation mset_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
  1101 where
  1101 where
  1102   "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set"
  1102   "folding.F (\<lambda>x M. {#x#} + M) {#} = mset_set"
  1103 proof -
  1103 proof -
  1104   interpret comp_fun_commute "\<lambda>x M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
  1104   interpret comp_fun_commute "\<lambda>x M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
  1105   show "folding (\<lambda>x M. {#x#} + M)" by default (fact comp_fun_commute)
  1105   show "folding (\<lambda>x M. {#x#} + M)" by default (fact comp_fun_commute)
  1106   from multiset_of_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set" ..
  1106   from mset_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = mset_set" ..
  1107 qed
  1107 qed
  1108 
  1108 
  1109 lemma count_multiset_of_set [simp]:
  1109 lemma count_mset_set [simp]:
  1110   "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (multiset_of_set A) x = 1" (is "PROP ?P")
  1110   "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (mset_set A) x = 1" (is "PROP ?P")
  1111   "\<not> finite A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?Q")
  1111   "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q")
  1112   "x \<notin> A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?R")
  1112   "x \<notin> A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?R")
  1113 proof -
  1113 proof -
  1114   { fix A
  1114   { fix A
  1115     assume "x \<notin> A"
  1115     assume "x \<notin> A"
  1116     have "count (multiset_of_set A) x = 0"
  1116     have "count (mset_set A) x = 0"
  1117     proof (cases "finite A")
  1117     proof (cases "finite A")
  1118       case False then show ?thesis by simp
  1118       case False then show ?thesis by simp
  1119     next
  1119     next
  1120       case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto
  1120       case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto
  1121     qed
  1121     qed
  1122   } note * = this
  1122   } note * = this
  1123   then show "PROP ?P" "PROP ?Q" "PROP ?R"
  1123   then show "PROP ?P" "PROP ?Q" "PROP ?R"
  1124   by (auto elim!: Set.set_insert)
  1124   by (auto elim!: Set.set_insert)
  1125 qed -- \<open>TODO: maybe define @{const multiset_of_set} also in terms of @{const Abs_multiset}\<close>
  1125 qed -- \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close>
  1126 
  1126 
  1127 lemma elem_multiset_of_set[simp, intro]: "finite A \<Longrightarrow> x \<in># multiset_of_set A \<longleftrightarrow> x \<in> A"
  1127 lemma elem_mset_set[simp, intro]: "finite A \<Longrightarrow> x \<in># mset_set A \<longleftrightarrow> x \<in> A"
  1128   by (induct A rule: finite_induct) simp_all
  1128   by (induct A rule: finite_induct) simp_all
  1129 
  1129 
  1130 context linorder
  1130 context linorder
  1131 begin
  1131 begin
  1132 
  1132 
  1154 
  1154 
  1155 end
  1155 end
  1156 
  1156 
  1157 lemma multiset_of_sorted_list_of_multiset [simp]:
  1157 lemma multiset_of_sorted_list_of_multiset [simp]:
  1158   "multiset_of (sorted_list_of_multiset M) = M"
  1158   "multiset_of (sorted_list_of_multiset M) = M"
  1159   by (induct M) simp_all
  1159 by (induct M) simp_all
  1160 
  1160 
  1161 lemma sorted_list_of_multiset_multiset_of [simp]:
  1161 lemma sorted_list_of_multiset_multiset_of [simp]:
  1162   "sorted_list_of_multiset (multiset_of xs) = sort xs"
  1162   "sorted_list_of_multiset (multiset_of xs) = sort xs"
  1163   by (induct xs) simp_all
  1163 by (induct xs) simp_all
  1164 
  1164 
  1165 lemma finite_set_mset_multiset_of_set:
  1165 lemma finite_set_mset_mset_set[simp]:
  1166   assumes "finite A"
  1166   "finite A \<Longrightarrow> set_mset (mset_set A) = A"
  1167   shows "set_mset (multiset_of_set A) = A"
  1167 by (induct A rule: finite_induct) simp_all
  1168   using assms by (induct A) simp_all
  1168 
  1169 
  1169 lemma infinite_set_mset_mset_set:
  1170 lemma infinite_set_mset_multiset_of_set:
  1170   "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
  1171   assumes "\<not> finite A"
  1171 by simp
  1172   shows "set_mset (multiset_of_set A) = {}"
       
  1173   using assms by simp
       
  1174 
  1172 
  1175 lemma set_sorted_list_of_multiset [simp]:
  1173 lemma set_sorted_list_of_multiset [simp]:
  1176   "set (sorted_list_of_multiset M) = set_mset M"
  1174   "set (sorted_list_of_multiset M) = set_mset M"
  1177   by (induct M) (simp_all add: set_insort)
  1175 by (induct M) (simp_all add: set_insort)
  1178 
  1176 
  1179 lemma sorted_list_of_multiset_of_set [simp]:
  1177 lemma sorted_list_of_mset_set [simp]:
  1180   "sorted_list_of_multiset (multiset_of_set A) = sorted_list_of_set A"
  1178   "sorted_list_of_multiset (mset_set A) = sorted_list_of_set A"
  1181   by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
  1179 by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
  1182 
  1180 
  1183 
  1181 
  1184 subsection \<open>Big operators\<close>
  1182 subsection \<open>Big operators\<close>
  1185 
  1183 
  1186 no_notation times (infixl "*" 70)
  1184 no_notation times (infixl "*" 70)
  1240   show "comm_monoid_mset plus 0" ..
  1238   show "comm_monoid_mset plus 0" ..
  1241   from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
  1239   from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
  1242 qed
  1240 qed
  1243 
  1241 
  1244 lemma setsum_unfold_msetsum:
  1242 lemma setsum_unfold_msetsum:
  1245   "setsum f A = msetsum (image_mset f (multiset_of_set A))"
  1243   "setsum f A = msetsum (image_mset f (mset_set A))"
  1246   by (cases "finite A") (induct A rule: finite_induct, simp_all)
  1244   by (cases "finite A") (induct A rule: finite_induct, simp_all)
  1247 
  1245 
  1248 end
  1246 end
  1249 
  1247 
  1250 lemma msetsum_diff:
  1248 lemma msetsum_diff:
  1314 lemma msetprod_Un:
  1312 lemma msetprod_Un:
  1315   "msetprod (A + B) = msetprod A * msetprod B"
  1313   "msetprod (A + B) = msetprod A * msetprod B"
  1316   by (fact msetprod.union)
  1314   by (fact msetprod.union)
  1317 
  1315 
  1318 lemma setprod_unfold_msetprod:
  1316 lemma setprod_unfold_msetprod:
  1319   "setprod f A = msetprod (image_mset f (multiset_of_set A))"
  1317   "setprod f A = msetprod (image_mset f (mset_set A))"
  1320   by (cases "finite A") (induct A rule: finite_induct, simp_all)
  1318   by (cases "finite A") (induct A rule: finite_induct, simp_all)
  1321 
  1319 
  1322 lemma msetprod_multiplicity:
  1320 lemma msetprod_multiplicity:
  1323   "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_mset M)"
  1321   "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_mset M)"
  1324   by (simp add: fold_mset_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
  1322   by (simp add: fold_mset_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
  2135 declare set_mset_multiset_of [code]
  2133 declare set_mset_multiset_of [code]
  2136 
  2134 
  2137 declare sorted_list_of_multiset_multiset_of [code]
  2135 declare sorted_list_of_multiset_multiset_of [code]
  2138 
  2136 
  2139 lemma [code]: -- \<open>not very efficient, but representation-ignorant!\<close>
  2137 lemma [code]: -- \<open>not very efficient, but representation-ignorant!\<close>
  2140   "multiset_of_set A = multiset_of (sorted_list_of_set A)"
  2138   "mset_set A = multiset_of (sorted_list_of_set A)"
  2141   apply (cases "finite A")
  2139   apply (cases "finite A")
  2142   apply simp_all
  2140   apply simp_all
  2143   apply (induct A rule: finite_induct)
  2141   apply (induct A rule: finite_induct)
  2144   apply (simp_all add: add.commute)
  2142   apply (simp_all add: add.commute)
  2145   done
  2143   done