src/HOL/Library/Multiset.thy
changeset 64075 3d4c3eec5143
parent 64017 6e7bf7678518
child 64076 9f089287687b
equal deleted inserted replaced
64074:7dccbbd8d71d 64075:3d4c3eec5143
  2059 subsection \<open>Big operators\<close>
  2059 subsection \<open>Big operators\<close>
  2060 
  2060 
  2061 locale comm_monoid_mset = comm_monoid
  2061 locale comm_monoid_mset = comm_monoid
  2062 begin
  2062 begin
  2063 
  2063 
       
  2064 interpretation comp_fun_commute f
       
  2065   by standard (simp add: fun_eq_iff left_commute)
       
  2066 
       
  2067 interpretation comp?: comp_fun_commute "f \<circ> g"
       
  2068   by (fact comp_comp_fun_commute)
       
  2069 
       
  2070 context
       
  2071 begin
       
  2072 
  2064 definition F :: "'a multiset \<Rightarrow> 'a"
  2073 definition F :: "'a multiset \<Rightarrow> 'a"
  2065   where eq_fold: "F M = fold_mset f \<^bold>1 M"
  2074   where eq_fold: "F M = fold_mset f \<^bold>1 M"
  2066 
  2075 
  2067 lemma empty [simp]: "F {#} = \<^bold>1"
  2076 lemma empty [simp]: "F {#} = \<^bold>1"
  2068   by (simp add: eq_fold)
  2077   by (simp add: eq_fold)
  2083 qed
  2092 qed
  2084 
  2093 
  2085 lemma add_mset [simp]: "F (add_mset x N) = x \<^bold>* F N"
  2094 lemma add_mset [simp]: "F (add_mset x N) = x \<^bold>* F N"
  2086   unfolding add_mset_add_single[of x N] union by (simp add: ac_simps)
  2095   unfolding add_mset_add_single[of x N] union by (simp add: ac_simps)
  2087 
  2096 
       
  2097 lemma insert [simp]:
       
  2098   shows "F (image_mset g (add_mset x A)) = g x \<^bold>* F (image_mset g A)"
       
  2099   by (simp add: eq_fold)
       
  2100 
       
  2101 lemma remove:
       
  2102   assumes "x \<in># A"
       
  2103   shows "F A = x \<^bold>* F (A - {#x#})"
       
  2104   using multi_member_split[OF assms] by auto
       
  2105 
       
  2106 lemma neutral:
       
  2107   "\<forall>x\<in>#A. x = \<^bold>1 \<Longrightarrow> F A = \<^bold>1"
       
  2108   by (induct A) simp_all
       
  2109 
       
  2110 lemma neutral_const [simp]:
       
  2111   "F (image_mset (\<lambda>_. \<^bold>1) A) = \<^bold>1"
       
  2112   by (simp add: neutral)
       
  2113 
       
  2114 private lemma F_image_mset_product:
       
  2115   "F {#g x j \<^bold>* F {#g i j. i \<in># A#}. j \<in># B#} =
       
  2116     F (image_mset (g x) B) \<^bold>* F {#F {#g i j. i \<in># A#}. j \<in># B#}"
       
  2117   by (induction B) (simp_all add: left_commute semigroup.assoc semigroup_axioms)
       
  2118 
       
  2119 lemma commute:
       
  2120   "F (image_mset (\<lambda>i. F (image_mset (g i) B)) A) =
       
  2121     F (image_mset (\<lambda>j. F (image_mset (\<lambda>i. g i j) A)) B)"
       
  2122   apply (induction A, simp)
       
  2123   apply (induction B, auto simp add: F_image_mset_product ac_simps)
       
  2124   done
       
  2125 
       
  2126 lemma distrib: "F (image_mset (\<lambda>x. g x \<^bold>* h x) A) = F (image_mset g A) \<^bold>* F (image_mset h A)"
       
  2127   by (induction A) (auto simp: ac_simps)
       
  2128 
       
  2129 lemma union_disjoint:
       
  2130   "A \<inter># B = {#} \<Longrightarrow> F (image_mset g (A \<union># B)) = F (image_mset g A) \<^bold>* F (image_mset g B)"
       
  2131   by (induction A) (auto simp: ac_simps)
       
  2132 
       
  2133 end
  2088 end
  2134 end
  2089 
  2135 
  2090 lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)"
  2136 lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)"
  2091   by standard (simp add: add_ac comp_def)
  2137   by standard (simp add: add_ac comp_def)
  2092 
  2138 
  2153 
  2199 
  2154 lemma sum_mset_distrib_left:
  2200 lemma sum_mset_distrib_left:
  2155   fixes f :: "'a \<Rightarrow> 'b::semiring_0"
  2201   fixes f :: "'a \<Rightarrow> 'b::semiring_0"
  2156   shows "c * (\<Sum>x \<in># M. f x) = (\<Sum>x \<in># M. c * f(x))"
  2202   shows "c * (\<Sum>x \<in># M. f x) = (\<Sum>x \<in># M. c * f(x))"
  2157 by (induction M) (simp_all add: distrib_left)
  2203 by (induction M) (simp_all add: distrib_left)
       
  2204 
       
  2205 lemma sum_mset_distrib_right:
       
  2206   fixes f :: "'a \<Rightarrow> 'b::semiring_0"
       
  2207   shows "(\<Sum>b \<in># B. f b) * a = (\<Sum>b \<in># B. f b * a)"
       
  2208   by (induction B) (auto simp: distrib_right)
       
  2209 
       
  2210 lemma sum_mset_constant [simp]:
       
  2211   fixes y :: "'b::semiring_1"
       
  2212   shows \<open>(\<Sum>x\<in>#A. y) = of_nat (size A) * y\<close>
       
  2213   by (induction A) (auto simp: algebra_simps)
       
  2214 
       
  2215 lemma (in ordered_comm_monoid_add) sum_mset_mono:
       
  2216   assumes "\<And>i. i \<in># K \<Longrightarrow> f i \<le> g i"
       
  2217   shows "sum_mset (image_mset f K) \<le> sum_mset (image_mset g K)"
       
  2218   using assms by (induction K) (simp_all add: local.add_mono)
       
  2219 
       
  2220 lemma sum_mset_product:
       
  2221   fixes f :: "'a::{comm_monoid_add,times} \<Rightarrow> 'b::semiring_0"
       
  2222   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)"
       
  2223   by (subst sum_mset.commute) (simp add: sum_mset_distrib_left sum_mset_distrib_right)
  2158 
  2224 
  2159 abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset"  ("\<Union>#_" [900] 900)
  2225 abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset"  ("\<Union>#_" [900] 900)
  2160   where "\<Union># MM \<equiv> sum_mset MM" \<comment> \<open>FIXME ambiguous notation --
  2226   where "\<Union># MM \<equiv> sum_mset MM" \<comment> \<open>FIXME ambiguous notation --
  2161     could likewise refer to \<open>\<Squnion>#\<close>\<close>
  2227     could likewise refer to \<open>\<Squnion>#\<close>\<close>
  2162 
  2228