src/HOL/Library/DAList_Multiset.thy
changeset 73270 e2d03448d5b5
parent 69593 3dda49e08b9d
child 73393 716d256259d5
equal deleted inserted replaced
73269:f5a77ee9106c 73270:e2d03448d5b5
    98 text \<open>Implementing multisets by means of association lists\<close>
    98 text \<open>Implementing multisets by means of association lists\<close>
    99 
    99 
   100 definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat"
   100 definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat"
   101   where "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)"
   101   where "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)"
   102 
   102 
   103 lemma count_of_multiset: "count_of xs \<in> multiset"
   103 lemma count_of_multiset: "finite {x. 0 < count_of xs x}"
   104 proof -
   104 proof -
   105   let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0::nat | Some n \<Rightarrow> n)}"
   105   let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0::nat | Some n \<Rightarrow> n)}"
   106   have "?A \<subseteq> dom (map_of xs)"
   106   have "?A \<subseteq> dom (map_of xs)"
   107   proof
   107   proof
   108     fix x
   108     fix x
   115       by auto
   115       by auto
   116   qed
   116   qed
   117   with finite_dom_map_of [of xs] have "finite ?A"
   117   with finite_dom_map_of [of xs] have "finite ?A"
   118     by (auto intro: finite_subset)
   118     by (auto intro: finite_subset)
   119   then show ?thesis
   119   then show ?thesis
   120     by (simp add: count_of_def fun_eq_iff multiset_def)
   120     by (simp add: count_of_def fun_eq_iff)
   121 qed
   121 qed
   122 
   122 
   123 lemma count_simps [simp]:
   123 lemma count_simps [simp]:
   124   "count_of [] = (\<lambda>_. 0)"
   124   "count_of [] = (\<lambda>_. 0)"
   125   "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)"
   125   "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)"
   289 proof (induct al)
   289 proof (induct al)
   290   fix ys
   290   fix ys
   291   let ?inv = "{xs :: ('a \<times> nat) list. (distinct \<circ> map fst) xs}"
   291   let ?inv = "{xs :: ('a \<times> nat) list. (distinct \<circ> map fst) xs}"
   292   note cs[simp del] = count_simps
   292   note cs[simp del] = count_simps
   293   have count[simp]: "\<And>x. count (Abs_multiset (count_of x)) = count_of x"
   293   have count[simp]: "\<And>x. count (Abs_multiset (count_of x)) = count_of x"
   294     by (rule Abs_multiset_inverse[OF count_of_multiset])
   294     by (rule Abs_multiset_inverse) (simp add: count_of_multiset)
   295   assume ys: "ys \<in> ?inv"
   295   assume ys: "ys \<in> ?inv"
   296   then show "fold_mset f e (Bag (Alist ys)) = fold_impl fn e (DAList.impl_of (Alist ys))"
   296   then show "fold_mset f e (Bag (Alist ys)) = fold_impl fn e (DAList.impl_of (Alist ys))"
   297     unfolding Bag_def unfolding Alist_inverse[OF ys]
   297     unfolding Bag_def unfolding Alist_inverse[OF ys]
   298   proof (induct ys arbitrary: e rule: list.induct)
   298   proof (induct ys arbitrary: e rule: list.induct)
   299     case Nil
   299     case Nil