section‹Multisets partially implemented by association lists›
theory DAList_Multiset imports Multiset DAList begin
text‹Raw operations on lists›
definition join_raw :: "('key ==> 'val × 'val ==> 'val) ==> ('key × 'val) list ==> ('key × 'val) list ==> ('key × 'val) list" where"join_raw f xs ys = foldr (λ(k, v). map_default k v (λv'. f k (v', v))) ys xs"
lemma join_raw_Nil [simp]: "join_raw f xs [] = xs" by (simp add: join_raw_def)
lemma join_raw_Cons [simp]: "join_raw f xs ((k, v) # ys) = map_default k v (λv'. f k (v', v)) (join_raw f xs ys)" by (simp add: join_raw_def)
lemma map_of_join_raw: assumes"distinct (map fst ys)" shows"map_of (join_raw f xs ys) x = (case map_of xs x of None ==> map_of ys x | Some v ==> (case map_of ys x of None ==> Some v | Some v' ==> Some (f x (v, v'))))" using assms apply (induct ys) apply (auto simp add: map_of_map_default split: option.split) apply (metis map_of_eq_None_iff option.simps(2) weak_map_of_SomeI) apply (metis Some_eq_map_of_iff map_of_eq_None_iff option.simps(2)) done
lemma distinct_join_raw: assumes"distinct (map fst xs)" shows"distinct (map fst (join_raw f xs ys))" using assms proof (induct ys) case Nil thenshow ?caseby simp next case (Cons y ys) thenshow ?caseby (cases y) (simp add: distinct_map_default) qed
text‹Implementing multisets by means of association lists›
definition count_of :: "('a × nat) list ==> 'a ==> nat" where"count_of xs x = (case map_of xs x of None ==> 0 | Some n ==> n)"
lemma count_of_multiset: "finite {x. 0 < count_of xs x}" proof - let ?A = "{x::'a. 0 < (case map_of xs x of None ==> 0::nat | Some n ==> n)}" have"?A ⊆ dom (map_of xs)" proof fix x assume"x ∈ ?A" thenhave"0 < (case map_of xs x of None ==> 0::nat | Some n ==> n)" by simp thenhave"map_of xs x ≠ None" by (cases "map_of xs x") auto thenshow"x ∈ dom (map_of xs)" by auto qed with finite_dom_map_of [of xs] have"finite ?A" by (auto intro: finite_subset) thenshow ?thesis by (simp add: count_of_def fun_eq_iff) qed
lemma count_simps [simp]: "count_of [] = (λ_. 0)" "count_of ((x, n) # xs) = (λy. if x = y then n else count_of xs y)" by (simp_all add: count_of_def fun_eq_iff)
lemma count_of_empty: "x ∉ fst ` set xs ==> count_of xs x = 0" by (induct xs) (simp_all add: count_of_def)
lemma count_of_filter: "count_of (List.filter (P ∘ fst) xs) x = (if P x then count_of xs x else 0)" by (induct xs) auto
lemma count_of_map_default [simp]: "count_of (map_default x b (λx. x + b) xs) y = (if x = y then count_of xs x + b else count_of xs y)" unfolding count_of_def by (simp add: map_of_map_default split: option.split)
lemma count_of_join_raw: "distinct (map fst ys) ==> count_of xs x + count_of ys x = count_of (join_raw (λx (x, y). x + y) xs ys) x" unfolding count_of_def by (simp add: map_of_join_raw split: option.split)
lemma count_of_subtract_entries_raw: "distinct (map fst ys) ==> count_of xs x - count_of ys x = count_of (subtract_entries_raw xs ys) x" unfolding count_of_def by (simp add: map_of_subtract_entries_raw split: option.split)
text‹Code equations for multiset operations›
definition Bag :: "('a, nat) alist ==> 'a multiset" where"Bag xs = Abs_multiset (count_of (DAList.impl_of xs))"
lemma Bag_eq: ‹Bag ms = (∑(a, n)←alist.impl_of ms. replicate_mset n a)› for ms :: ‹('a, nat) alist› proof - have *: ‹count_of xs a = count (∑(a, n)←xs. replicate_mset n a) a› if‹distinct (map fst xs)› for xs and a :: 'a using that proof (induction xs) case Nil thenshow ?case by simp next case (Cons xn xs) thenshow ?caseby (cases xn)
(auto simp add: count_eq_zero_iff if_split_mem2 image_iff) qed show ?thesis by (rule multiset_eqI) (simp add: *) qed
lemma Mempty_Bag [code]: "{#} = Bag (DAList.empty)" by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def)
text‹By default the code for ‹<\<close> is prop‹xs < ys ⟷ xs ≤ ys ∧¬ xs = ys›.
equality implemented by ‹≤›, this leads to three calls of ‹≤›.
is a more efficient version:›
mset_less[code]: "xs ⊂# (ys :: 'a multiset) ⟷ xs ⊆# ys ∧¬ ys ⊆# xs"
by (rule subset_mset.less_le_not_le)
mset_less_eq_Bag0:
"Bag xs ⊆# A ⟷ (∀(x, n) ∈ set (DAList.impl_of xs). count_of (DAList.impl_of xs) x ≤ count A x)"
(is "?lhs ⟷ ?rhs")
assume ?lhs
then show ?rhs by (auto simp add: subseteq_mset_def)
assume ?rhs
show ?lhs
proof (rule mset_subset_eqI)
fix x
from ‹?rhs› have "count_of (DAList.impl_of xs) x ≤ count A x"
by (cases "x ∈ fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty)
then show "count (Bag xs) x ≤ count A x" by (simp add: subset_mset_def)
qed
mset_less_eq_Bag [code]:
"Bag xs ⊆# (A :: 'a multiset) ⟷ (∀(x, n) ∈ set (DAList.impl_of xs). n ≤ count A x)"
-
{
fix x n
assume "(x,n) ∈ set (DAList.impl_of xs)"
then have "count_of (DAList.impl_of xs) x = n"
proof transfer
fix x n
fix xs :: "('a × nat) list"
show "(distinct ∘ map fst) xs ==> (x, n) ∈ set xs ==> count_of xs x = n"
proof (induct xs)
case Nil
then show ?case by simp
next
case (Cons ym ys)
obtain y m where ym: "ym = (y,m)" by force
note Cons = Cons[unfolded ym]
show ?case
proof (cases "x = y")
case False
with Cons show ?thesis
unfolding ym by auto
next
case True
with Cons(2-3) have "m = n" by force
with True show ?thesis
unfolding ym by auto
qed
qed
qed
}
then show ?thesis
unfolding mset_less_eq_Bag0 by auto
"fold_impl fn e ((a,n) # ms) = (fold_impl fn ((fn a n) e) ms)"
"fold_impl fn e [] = e"
definition fold :: "('a ==> nat ==> 'b ==> 'b) ==> 'b ==> ('a, nat) alist ==> 'b"
where "fold f e al = fold_impl f e (DAList.impl_of al)"
comp_fun_commute
DAList_Multiset_fold:
assumes fn: "∧a n x. fn a n x = (f a ^^ n) x"
shows "fold_mset f e (Bag al) = DAList_Multiset.fold fn e al"
unfolding DAList_Multiset.fold_def
(induct al)
fix ys
let ?inv = "{xs :: ('a × nat) list. (distinct ∘ map fst) xs}"
note cs[simp del] = count_simps
have count[simp]: "∧x. count (Abs_multiset (count_of x)) = count_of x"
by (rule Abs_multiset_inverse) (simp add: count_of_multiset)
assume ys: "ys ∈ ?inv"
then show "fold_mset f e (Bag (Alist ys)) = fold_impl fn e (DAList.impl_of (Alist ys))"
unfolding Bag_def unfolding Alist_inverse[OF ys]
proof (induct ys arbitrary: e rule: list.induct)
case Nil
show ?case
by (rule trans[OF arg_cong[of _ "{#}" "fold_mset f e", OF multiset_eqI]])
(auto, simp add: cs)
next
case (Cons pair ys e)
obtain a n where pair: "pair = (a,n)"
by force
from fn[of a n] have [simp]: "fn a n = (f a ^^ n)"
by auto
have inv: "ys ∈ ?inv"
using Cons(2) by auto
note IH = Cons(1)[OF inv]
define Ys where "Ys = Abs_multiset (count_of ys)"
have id: "Abs_multiset (count_of ((a, n) # ys)) = (((+) {# a #}) ^^ n) Ys"
unfolding Ys_def
proof (rule multiset_eqI, unfold count)
fix c
show "count_of ((a, n) # ys) c =
count (((+) {#a#} ^^ n) (Abs_multiset (count_of ys))) c" (is "?l = ?r")
proof (cases "c = a")
case False
then show ?thesis
unfolding cs by (induct n) auto
next
case True
then have "?l = n" by (simp add: cs)
also have "n = ?r" unfolding True
proof (induct n)
case 0
from Cons(2)[unfolded pair] have "a ∉ fst ` set ys" by auto
then show ?case by (induct ys) (simp, auto simp: cs)
next
case Suc
then show ?case by simp
qed
finally show ?thesis .
qed
qed
show ?case
unfolding pair
apply (simp add: IH[symmetric])
unfolding id Ys_def[symmetric]
apply (induct n)
apply (auto simp: fold_mset_fun_left_comm[symmetric])
done
qed
lift_definition single_alist_entry :: "'a ==> 'b ==> ('a, 'b) alist" is "λa b. [(a, b)]"
by auto
image_mset_Bag [code]:
"image_mset f (Bag ms) =
DAList_Multiset.fold (λa n m. Bag (single_alist_entry (f a) n) + m) {#} ms"
unfolding image_mset_def
(rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, (auto simp: ac_simps)[1])
fix a n m
show "Bag (single_alist_entry (f a) n) + m = ((add_mset ∘ f) a ^^ n) m" (is "?l = ?r")
proof (rule multiset_eqI)
fix x
have "count ?r x = (if x = f a then n + count m x else count m x)"
by (induct n) auto
also have "… = count ?l x"
by (simp add: single_alist_entry.rep_eq)
finally show "count ?l x = count ?r x" ..
qed
\<comment> \<open>we cannot use \<open>\<lambda>a n. (+) (a * n)\<close> for folding, since \<open>(*)›isnot defined in‹comm_monoid_add›› lemmasum_mset_Bag[code]:"sum_mset(Bagms)=DAList_Multiset.fold(\<lambda>an.(((+)a)^^n))0ms" unfoldingsum_mset.eq_fold apply(rulecomp_fun_commute.DAList_Multiset_fold) applyunfold_locales apply(autosimp:ac_simps) done
\<comment> \<open>we cannot use \<open>\<lambda>a n. (*) (a ^ n)›for folding, since ‹(^)› is not defined in ‹comm_monoid_mult››
lemma prod_mset_Bag[code]: "prod_mset (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (((*) a) ^^ n)) 1 ms" unfolding prod_mset.eq_fold apply (rule comp_fun_commute.DAList_Multiset_fold) apply unfold_locales apply (auto simp: ac_simps) done
lemma size_fold: "size A = fold_mset (λ_. Suc) 0 A" (is "_ = fold_mset ?f _ _") proof - interpret comp_fun_commute ?f by standard auto show ?thesis by (induct A) auto qed
lemma size_Bag[code]: "size (Bag ms) = DAList_Multiset.fold (λa n. (+) n) 0 ms" unfolding size_fold proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, simp) fix a n x show "n + x = (Suc ^^ n) x" by (induct n) auto qed
lemma set_mset_fold: "set_mset A = fold_mset insert {} A" (is "_ = fold_mset ?f _ _") proof - interpret comp_fun_commute ?f by standard auto show ?thesis by (induct A) auto qed
lemma set_mset_Bag[code]: "set_mset (Bag ms) = DAList_Multiset.fold (λa n. (if n = 0then (λm. m) else insert a)) {} ms" unfolding set_mset_fold proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, (auto simp: ac_simps)[1]) fix a n x show "(if n = 0then λm. m else insert a) x = (insert a ^^ n) x" (is "?l n = ?r n") proof (cases n) case 0 then show ?thesis by simp next case (Suc m) then have "?l n = insert a x" by simp moreover have "?r n = insert a x" unfolding Suc by (induct m) auto ultimately show ?thesis by auto qed qed
lemma sorted_list_of_multiset_Bag [code]: ‹sorted_list_of_multiset (Bag ms) = concat (map (λ(a, n). replicate n a) (sort_key fst (DAList.impl_of ms)))› (is ‹?lhs = ?rhs›) proof - have *: ‹sorted (concat (map (λ(a, n). replicate n a) ans))› if ‹sorted (map fst ans)› for ans :: ‹('a × nat) list› using that by (induction ans) (auto simp add: sorted_append) have ‹mset ?rhs = mset ?lhs› by (simp add: Bag_eq mset_concat comp_def split_def flip: sum_mset_sum_list) moreover have ‹sorted ?rhs› by (rule *) simp ultimately have ‹sort ?lhs = ?rhs› by (rule properties_for_sort) then show ?thesis by simp qed
instantiation multiset :: (exhaustive) exhaustive begin
definition exhaustive_multiset :: "('a multiset ==> (bool ×term list) option) ==> natural ==> (bool ×term list) option" where "exhaustive_multiset f i = Quickcheck_Exhaustive.exhaustive (λxs. f (Bag xs)) i"
instance ..
end
end
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.8 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.