# Theory Multiset

theory Multiset
imports Main
```(*  Title:      HOL/Library/Multiset.thy
Author:     Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
Author:     Andrei Popescu, TU Muenchen
*)

theory Multiset
imports Main
begin

subsection {* The type of multisets *}

definition "multiset = {f :: 'a => nat. finite {x. f x > 0}}"

typedef 'a multiset = "multiset :: ('a => nat) set"
morphisms count Abs_multiset
unfolding multiset_def
proof
show "(λx. 0::nat) ∈ {f. finite {x. f x > 0}}" by simp
qed

setup_lifting type_definition_multiset

abbreviation Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
"a :# M == 0 < count M a"

notation (xsymbols)
Melem (infix "∈#" 50)

lemma multiset_eq_iff:
"M = N <-> (∀a. count M a = count N a)"
by (simp only: count_inject [symmetric] fun_eq_iff)

lemma multiset_eqI:
"(!!x. count A x = count B x) ==> A = B"
using multiset_eq_iff by auto

text {*
\medskip Preservation of the representing set @{term multiset}.
*}

lemma const0_in_multiset:
"(λa. 0) ∈ multiset"

lemma only1_in_multiset:
"(λb. if b = a then n else 0) ∈ multiset"

lemma union_preserves_multiset:
"M ∈ multiset ==> N ∈ multiset ==> (λa. M a + N a) ∈ multiset"

lemma diff_preserves_multiset:
assumes "M ∈ multiset"
shows "(λa. M a - N a) ∈ multiset"
proof -
have "{x. N x < M x} ⊆ {x. 0 < M x}"
by auto
with assms show ?thesis
by (auto simp add: multiset_def intro: finite_subset)
qed

lemma filter_preserves_multiset:
assumes "M ∈ multiset"
shows "(λx. if P x then M x else 0) ∈ multiset"
proof -
have "{x. (P x --> 0 < M x) ∧ P x} ⊆ {x. 0 < M x}"
by auto
with assms show ?thesis
by (auto simp add: multiset_def intro: finite_subset)
qed

lemmas in_multiset = const0_in_multiset only1_in_multiset
union_preserves_multiset diff_preserves_multiset filter_preserves_multiset

subsection {* Representing multisets *}

text {* Multiset enumeration *}

begin

lift_definition zero_multiset :: "'a multiset" is "λa. 0"
by (rule const0_in_multiset)

abbreviation Mempty :: "'a multiset" ("{#}") where
"Mempty ≡ 0"

lift_definition plus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "λM N. (λa. M a + N a)"
by (rule union_preserves_multiset)

instance
by default (transfer, simp add: fun_eq_iff)+

end

lift_definition single :: "'a => 'a multiset" is "λa b. if b = a then 1 else 0"
by (rule only1_in_multiset)

syntax
"_multiset" :: "args => 'a multiset"    ("{#(_)#}")
translations
"{#x, xs#}" == "{#x#} + {#xs#}"
"{#x#}" == "CONST single x"

lemma count_empty [simp]: "count {#} a = 0"

lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"

subsection {* Basic operations *}

subsubsection {* Union *}

lemma count_union [simp]: "count (M + N) a = count M a + count N a"

subsubsection {* Difference *}

instantiation multiset :: (type) comm_monoid_diff
begin

lift_definition minus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "λ M N. λa. M a - N a"
by (rule diff_preserves_multiset)

instance
by default (transfer, simp add: fun_eq_iff)+

end

lemma count_diff [simp]: "count (M - N) a = count M a - count N a"

lemma diff_empty [simp]: "M - {#} = M ∧ {#} - M = {#}"
by rule (fact Groups.diff_zero, fact Groups.zero_diff)

lemma diff_cancel[simp]: "A - A = {#}"
by (fact Groups.diff_cancel)

lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"

lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"

lemma diff_right_commute:
"(M::'a multiset) - N - Q = M - Q - N"
by (fact diff_right_commute)

"(M::'a multiset) - (N + Q) = M - N - Q"

lemma insert_DiffM:
"x ∈# M ==> {#x#} + (M - {#x#}) = M"
by (clarsimp simp: multiset_eq_iff)

lemma insert_DiffM2 [simp]:
"x ∈# M ==> M - {#x#} + {#x#} = M"
by (clarsimp simp: multiset_eq_iff)

lemma diff_union_swap:
"a ≠ b ==> M - {#a#} + {#b#} = M + {#b#} - {#a#}"

lemma diff_union_single_conv:
"a ∈# J ==> I + J - {#a#} = I + (J - {#a#})"

subsubsection {* Equality of multisets *}

lemma single_not_empty [simp]: "{#a#} ≠ {#} ∧ {#} ≠ {#a#}"

lemma single_eq_single [simp]: "{#a#} = {#b#} <-> a = b"

lemma union_eq_empty [iff]: "M + N = {#} <-> M = {#} ∧ N = {#}"

lemma empty_eq_union [iff]: "{#} = M + N <-> M = {#} ∧ N = {#}"

lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} <-> False"

lemma diff_single_trivial:
"¬ x ∈# M ==> M - {#x#} = M"

lemma diff_single_eq_union:
"x ∈# M ==> M - {#x#} = N <-> M = N + {#x#}"
by auto

lemma union_single_eq_diff:
"M + {#x#} = N ==> M = N - {#x#}"
by (auto dest: sym)

lemma union_single_eq_member:
"M + {#x#} = N ==> x ∈# N"
by auto

lemma union_is_single:
"M + N = {#a#} <-> M = {#a#} ∧ N={#} ∨ M = {#} ∧ N = {#a#}" (is "?lhs = ?rhs")
proof
assume ?rhs then show ?lhs by auto
next
assume ?lhs then show ?rhs
qed

lemma single_is_union:
"{#a#} = M + N <-> {#a#} = M ∧ N = {#} ∨ M = {#} ∧ {#a#} = N"
by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)

"M + {#a#} = N + {#b#} <-> M = N ∧ a = b ∨ M = N - {#a#} + {#b#} ∧ N = M - {#b#} + {#a#}"  (is "?lhs = ?rhs")
(* shorter: by (simp add: multiset_eq_iff) fastforce *)
proof
assume ?rhs then show ?lhs
next
assume ?lhs
show ?rhs
proof (cases "a = b")
case True with `?lhs` show ?thesis by simp
next
case False
from `?lhs` have "a ∈# N + {#b#}" by (rule union_single_eq_member)
with False have "a ∈# N" by auto
moreover from `?lhs` have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
moreover note False
ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
qed
qed

lemma insert_noteq_member:
assumes BC: "B + {#b#} = C + {#c#}"
and bnotc: "b ≠ c"
shows "c ∈# B"
proof -
have "c ∈# C + {#c#}" by simp
have nc: "¬ c ∈# {#b#}" using bnotc by simp
then have "c ∈# B + {#b#}" using BC by simp
then show "c ∈# B" using nc by simp
qed

"(M + {#a#} = N + {#b#}) =
(M = N ∧ a = b ∨ (∃K. M = K + {#b#} ∧ N = K + {#a#}))"

lemma multi_member_split:
"x ∈# M ==> ∃A. M = A + {#x#}"
by (rule_tac x = "M - {#x#}" in exI, simp)

subsubsection {* Pointwise ordering induced by count *}

begin

lift_definition less_eq_multiset :: "'a multiset => 'a multiset => bool" is "λ A B. (∀a. A a ≤ B a)" .

lemmas mset_le_def = less_eq_multiset_def

definition less_multiset :: "'a multiset => 'a multiset => bool" where
mset_less_def: "(A::'a multiset) < B <-> A ≤ B ∧ A ≠ B"

instance
by default (auto simp add: mset_le_def mset_less_def multiset_eq_iff intro: order_trans antisym)

end

lemma mset_less_eqI:
"(!!x. count A x ≤ count B x) ==> A ≤ B"

lemma mset_le_exists_conv:
"(A::'a multiset) ≤ B <-> (∃C. B = A + C)"
apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI)
apply (auto intro: multiset_eq_iff [THEN iffD2])
done

instance multiset :: (type) ordered_cancel_comm_monoid_diff
by default (simp, fact mset_le_exists_conv)

"(A::'a multiset) + C ≤ B + C <-> A ≤ B"

"C + (A::'a multiset) ≤ C + B <-> A ≤ B"

"(A::'a multiset) ≤ B ==> C ≤ D ==> A + C ≤ B + D"

"(A::'a multiset) ≤ A + B"
unfolding mset_le_def by auto

"B ≤ (A::'a multiset) + B"
unfolding mset_le_def by auto

lemma mset_le_single:
"a :# B ==> {#a#} ≤ B"

lemma multiset_diff_union_assoc:
"C ≤ B ==> (A::'a multiset) + B - C = A + (B - C)"

lemma mset_le_multiset_union_diff_commute:
"B ≤ A ==> (A::'a multiset) - B + C = A + C - B"

lemma diff_le_self[simp]: "(M::'a multiset) - N ≤ M"

lemma mset_lessD: "A < B ==> x ∈# A ==> x ∈# B"
apply (clarsimp simp: mset_le_def mset_less_def)
apply (erule_tac x=x in allE)
apply auto
done

lemma mset_leD: "A ≤ B ==> x ∈# A ==> x ∈# B"
apply (clarsimp simp: mset_le_def mset_less_def)
apply (erule_tac x = x in allE)
apply auto
done

lemma mset_less_insertD: "(A + {#x#} < B) ==> (x ∈# B ∧ A < B)"
apply (rule conjI)
apply (clarsimp simp: mset_le_def mset_less_def)
apply safe
apply (erule_tac x = a in allE)
apply (auto split: split_if_asm)
done

lemma mset_le_insertD: "(A + {#x#} ≤ B) ==> (x ∈# B ∧ A ≤ B)"
apply (rule conjI)
apply (force simp: mset_le_def mset_less_def split: split_if_asm)
done

lemma mset_less_of_empty[simp]: "A < {#} <-> False"
by (auto simp add: mset_less_def mset_le_def multiset_eq_iff)

lemma empty_le[simp]: "{#} ≤ A"
unfolding mset_le_exists_conv by auto

lemma le_empty[simp]: "(M ≤ {#}) = (M = {#})"
unfolding mset_le_exists_conv by auto

lemma multi_psub_of_add_self[simp]: "A < A + {#x#}"
by (auto simp: mset_le_def mset_less_def)

lemma multi_psub_self[simp]: "(A::'a multiset) < A = False"
by simp

"T + {#x#} < S + {#x#} ==> T < S"

lemma mset_less_empty_nonempty:
"{#} < S <-> S ≠ {#}"
by (auto simp: mset_le_def mset_less_def)

lemma mset_less_diff_self:
"c ∈# B ==> B - {#c#} < B"
by (auto simp: mset_le_def mset_less_def multiset_eq_iff)

subsubsection {* Intersection *}

instantiation multiset :: (type) semilattice_inf
begin

definition inf_multiset :: "'a multiset => 'a multiset => 'a multiset" where
multiset_inter_def: "inf_multiset A B = A - (A - B)"

instance
proof -
have aux: "!!m n q :: nat. m ≤ n ==> m ≤ q ==> m ≤ n - (n - q)" by arith
show "OFCLASS('a multiset, semilattice_inf_class)"
by default (auto simp add: multiset_inter_def mset_le_def aux)
qed

end

abbreviation multiset_inter :: "'a multiset => 'a multiset => 'a multiset" (infixl "#∩" 70) where
"multiset_inter ≡ inf"

lemma multiset_inter_count [simp]:
"count (A #∩ B) x = min (count A x) (count B x)"

lemma multiset_inter_single: "a ≠ b ==> {#a#} #∩ {#b#} = {#}"
by (rule multiset_eqI) auto

lemma multiset_union_diff_commute:
assumes "B #∩ C = {#}"
shows "A + B - C = A - C + B"
proof (rule multiset_eqI)
fix x
from assms have "min (count B x) (count C x) = 0"
then have "count B x = 0 ∨ count C x = 0"
by auto
then show "count (A + B - C) x = count (A - C + B) x"
by auto
qed

lemma empty_inter [simp]:
"{#} #∩ M = {#}"

lemma inter_empty [simp]:
"M #∩ {#} = {#}"

"¬ x ∈# N ==> (M + {#x#}) #∩ N = M #∩ N"

"x ∈# N ==> (M + {#x#}) #∩ N = (M #∩ (N - {#x#})) + {#x#}"

"¬ x ∈# N ==> N #∩ (M + {#x#}) = N #∩ M"

"x ∈# N ==> N #∩ (M + {#x#}) = ((N - {#x#}) #∩ M) + {#x#}"

subsubsection {* Bounded union *}

instantiation multiset :: (type) semilattice_sup
begin

definition sup_multiset :: "'a multiset => 'a multiset => 'a multiset" where
"sup_multiset A B = A + (B - A)"

instance
proof -
have aux: "!!m n q :: nat. m ≤ n ==> q ≤ n ==> m + (q - m) ≤ n" by arith
show "OFCLASS('a multiset, semilattice_sup_class)"
by default (auto simp add: sup_multiset_def mset_le_def aux)
qed

end

abbreviation sup_multiset :: "'a multiset => 'a multiset => 'a multiset" (infixl "#∪" 70) where
"sup_multiset ≡ sup"

lemma sup_multiset_count [simp]:
"count (A #∪ B) x = max (count A x) (count B x)"

lemma empty_sup [simp]:
"{#} #∪ M = M"

lemma sup_empty [simp]:
"M #∪ {#} = M"

"¬ x ∈# N ==> (M + {#x#}) #∪ N = (M #∪ N) + {#x#}"

"x ∈# N ==> (M + {#x#}) #∪ N = (M #∪ (N - {#x#})) + {#x#}"

"¬ x ∈# N ==> N #∪ (M + {#x#}) = (N #∪ M) + {#x#}"

"x ∈# N ==> N #∪ (M + {#x#}) = ((N - {#x#}) #∪ M) + {#x#}"

subsubsection {* Filter (with comprehension syntax) *}

text {* Multiset comprehension *}

lift_definition filter :: "('a => bool) => 'a multiset => 'a multiset" is "λP M. λx. if P x then M x else 0"
by (rule filter_preserves_multiset)

hide_const (open) filter

lemma count_filter [simp]:
"count (Multiset.filter P M) a = (if P a then count M a else 0)"

lemma filter_empty [simp]:
"Multiset.filter P {#} = {#}"
by (rule multiset_eqI) simp

lemma filter_single [simp]:
"Multiset.filter P {#x#} = (if P x then {#x#} else {#})"
by (rule multiset_eqI) simp

lemma filter_union [simp]:
"Multiset.filter P (M + N) = Multiset.filter P M + Multiset.filter P N"
by (rule multiset_eqI) simp

lemma filter_diff [simp]:
"Multiset.filter P (M - N) = Multiset.filter P M - Multiset.filter P N"
by (rule multiset_eqI) simp

lemma filter_inter [simp]:
"Multiset.filter P (M #∩ N) = Multiset.filter P M #∩ Multiset.filter P N"
by (rule multiset_eqI) simp

syntax
"_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ :# _./ _#})")
syntax (xsymbol)
"_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ ∈# _./ _#})")
translations
"{#x ∈# M. P#}" == "CONST Multiset.filter (λx. P) M"

subsubsection {* Set of elements *}

definition set_of :: "'a multiset => 'a set" where
"set_of M = {x. x :# M}"

lemma set_of_empty [simp]: "set_of {#} = {}"

lemma set_of_single [simp]: "set_of {#b#} = {b}"

lemma set_of_union [simp]: "set_of (M + N) = set_of M ∪ set_of N"

lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
by (auto simp add: set_of_def multiset_eq_iff)

lemma mem_set_of_iff [simp]: "(x ∈ set_of M) = (x :# M)"

lemma set_of_filter [simp]: "set_of {# x:#M. P x #} = set_of M ∩ {x. P x}"

lemma finite_set_of [iff]: "finite (set_of M)"
using count [of M] by (simp add: multiset_def set_of_def)

lemma finite_Collect_mem [iff]: "finite {x. x :# M}"
unfolding set_of_def[symmetric] by simp

lemma set_of_mono: "A ≤ B ==> set_of A ⊆ set_of B"
by (metis mset_leD subsetI mem_set_of_iff)

subsubsection {* Size *}

definition wcount where "wcount f M = (λx. count M x * Suc (f x))"

lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a"

definition size_multiset :: "('a => nat) => 'a multiset => nat" where
"size_multiset f M = setsum (wcount f M) (set_of M)"

lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def]

instantiation multiset :: (type) size begin
definition size_multiset where
size_multiset_overloaded_def: "size_multiset = Multiset.size_multiset (λ_. 0)"
instance ..
end

lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"

lemma size_empty [simp]: "size {#} = 0"

lemma size_multiset_single [simp]: "size_multiset f {#b#} = Suc (f b)"

lemma size_single [simp]: "size {#b#} = 1"

lemma setsum_wcount_Int:
"finite A ==> setsum (wcount f N) (A ∩ set_of N) = setsum (wcount f N) A"
apply (induct rule: finite_induct)
apply simp
apply (simp add: Int_insert_left set_of_def wcount_def)
done

lemma size_multiset_union [simp]:
"size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N"
apply (simp add: size_multiset_def setsum_Un_nat setsum.distrib setsum_wcount_Int wcount_union)
apply (subst Int_commute)
done

lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"

lemma size_multiset_eq_0_iff_empty [iff]: "(size_multiset f M = 0) = (M = {#})"
by (auto simp add: size_multiset_eq multiset_eq_iff)

lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"

lemma nonempty_has_size: "(S ≠ {#}) = (0 < size S)"
by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)

lemma size_eq_Suc_imp_elem: "size M = Suc n ==> ∃a. a :# M"
apply (drule setsum_SucD)
apply auto
done

lemma size_eq_Suc_imp_eq_union:
assumes "size M = Suc n"
shows "∃a N. M = N + {#a#}"
proof -
from assms obtain a where "a ∈# M"
by (erule size_eq_Suc_imp_elem [THEN exE])
then have "M = M - {#a#} + {#a#}" by simp
then show ?thesis by blast
qed

subsection {* Induction and case splits *}

theorem multiset_induct [case_names empty add, induct type: multiset]:
assumes empty: "P {#}"
assumes add: "!!M x. P M ==> P (M + {#x#})"
shows "P M"
proof (induct n ≡ "size M" arbitrary: M)
case 0 thus "P M" by (simp add: empty)
next
case (Suc k)
obtain N x where "M = N + {#x#}"
using `Suc k = size M` [symmetric]
using size_eq_Suc_imp_eq_union by fast
with Suc add show "P M" by simp
qed

lemma multi_nonempty_split: "M ≠ {#} ==> ∃A a. M = A + {#a#}"
by (induct M) auto

lemma multiset_cases [cases type]:
obtains (empty) "M = {#}"
| (add) N x where "M = N + {#x#}"
using assms by (induct M) simp_all

lemma multi_drop_mem_not_eq: "c ∈# B ==> B - {#c#} ≠ B"
by (cases "B = {#}") (auto dest: multi_member_split)

lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#M. ¬ P x #}"
apply (subst multiset_eq_iff)
apply auto
done

lemma mset_less_size: "(A::'a multiset) < B ==> size A < size B"
proof (induct A arbitrary: B)
case (empty M)
then have "M ≠ {#}" by (simp add: mset_less_empty_nonempty)
then obtain M' x where "M = M' + {#x#}"
by (blast dest: multi_nonempty_split)
then show ?case by simp
next
have IH: "!!B. S < B ==> size S < size B" by fact
have SxsubT: "S + {#x#} < T" by fact
then have "x ∈# T" and "S < T" by (auto dest: mset_less_insertD)
then obtain T' where T: "T = T' + {#x#}"
by (blast dest: multi_member_split)
then have "S < T'" using SxsubT
then have "size S < size T'" using IH by simp
then show ?case using T by simp
qed

subsubsection {* Strong induction and subset induction for multisets *}

text {* Well-foundedness of proper subset operator: *}

text {* proper multiset subset *}

definition
mset_less_rel :: "('a multiset * 'a multiset) set" where
"mset_less_rel = {(A,B). A < B}"

assumes "c ∈# B" and "b ≠ c"
shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
proof -
from `c ∈# B` obtain A where B: "B = A + {#c#}"
by (blast dest: multi_member_split)
have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}"
then show ?thesis using B by simp
qed

lemma wf_mset_less_rel: "wf mset_less_rel"
apply (unfold mset_less_rel_def)
apply (rule wf_measure [THEN wf_subset, where f1=size])
apply (clarsimp simp: measure_def inv_image_def mset_less_size)
done

text {* The induction rules: *}

lemma full_multiset_induct [case_names less]:
assumes ih: "!!B. ∀(A::'a multiset). A < B --> P A ==> P B"
shows "P B"
apply (rule wf_mset_less_rel [THEN wf_induct])
apply (rule ih, auto simp: mset_less_rel_def)
done

lemma multi_subset_induct [consumes 2, case_names empty add]:
assumes "F ≤ A"
and empty: "P {#}"
and insert: "!!a F. a ∈# A ==> P F ==> P (F + {#a#})"
shows "P F"
proof -
from `F ≤ A`
show ?thesis
proof (induct F)
show "P {#}" by fact
next
fix x F
assume P: "F ≤ A ==> P F" and i: "F + {#x#} ≤ A"
show "P (F + {#x#})"
proof (rule insert)
from i show "x ∈# A" by (auto dest: mset_le_insertD)
from i have "F ≤ A" by (auto dest: mset_le_insertD)
with P show "P F" .
qed
qed
qed

subsection {* The fold combinator *}

definition fold :: "('a => 'b => 'b) => 'b => 'a multiset => 'b"
where
"fold f s M = Finite_Set.fold (λx. f x ^^ count M x) s (set_of M)"

lemma fold_mset_empty [simp]:
"fold f s {#} = s"

context comp_fun_commute
begin

lemma fold_mset_insert:
"fold f s (M + {#x#}) = f x (fold f s M)"
proof -
interpret mset: comp_fun_commute "λy. f y ^^ count M y"
by (fact comp_fun_commute_funpow)
interpret mset_union: comp_fun_commute "λy. f y ^^ count (M + {#x#}) y"
by (fact comp_fun_commute_funpow)
show ?thesis
proof (cases "x ∈ set_of M")
case False
then have *: "count (M + {#x#}) x = 1" by simp
from False have "Finite_Set.fold (λy. f y ^^ count (M + {#x#}) y) s (set_of M) =
Finite_Set.fold (λy. f y ^^ count M y) s (set_of M)"
by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
with False * show ?thesis
by (simp add: fold_def del: count_union)
next
case True
def N ≡ "set_of M - {x}"
from N_def True have *: "set_of M = insert x N" "x ∉ N" "finite N" by auto
then have "Finite_Set.fold (λy. f y ^^ count (M + {#x#}) y) s N =
Finite_Set.fold (λy. f y ^^ count M y) s N"
by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
with * show ?thesis by (simp add: fold_def del: count_union) simp
qed
qed

corollary fold_mset_single [simp]:
"fold f s {#x#} = f x s"
proof -
have "fold f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
then show ?thesis by simp
qed

lemma fold_mset_fun_left_comm:
"f x (fold f s M) = fold f (f x s) M"
by (induct M) (simp_all add: fold_mset_insert fun_left_comm)

lemma fold_mset_union [simp]:
"fold f s (M + N) = fold f (fold f s M) N"
proof (induct M)
case empty then show ?case by simp
next
have "M + {#x#} + N = (M + N) + {#x#}"
qed

lemma fold_mset_fusion:
assumes "comp_fun_commute g"
shows "(!!x y. h (g x y) = f x (h y)) ==> h (fold g w A) = fold f (h w) A" (is "PROP ?P")
proof -
interpret comp_fun_commute g by (fact assms)
show "PROP ?P" by (induct A) auto
qed

end

text {*
A note on code generation: When defining some function containing a
subterm @{term "fold F"}, code generation is not automatic. When
interpreting locale @{text left_commutative} with @{text F}, the
would be code thms for @{const fold} become thms like
@{term "fold F z {#} = z"} where @{text F} is not a pattern but
contains defined symbols, i.e.\ is not a code thm. Hence a separate
constant with its own code thms needs to be introduced for @{text
F}. See the image operator below.
*}

subsection {* Image *}

definition image_mset :: "('a => 'b) => 'a multiset => 'b multiset" where
"image_mset f = fold (plus o single o f) {#}"

lemma comp_fun_commute_mset_image:
"comp_fun_commute (plus o single o f)"
proof

lemma image_mset_empty [simp]: "image_mset f {#} = {#}"

lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
proof -
interpret comp_fun_commute "plus o single o f"
by (fact comp_fun_commute_mset_image)
show ?thesis by (simp add: image_mset_def)
qed

lemma image_mset_union [simp]:
"image_mset f (M + N) = image_mset f M + image_mset f N"
proof -
interpret comp_fun_commute "plus o single o f"
by (fact comp_fun_commute_mset_image)
show ?thesis by (induct N) (simp_all add: image_mset_def ac_simps)
qed

corollary image_mset_insert:
"image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
by simp

lemma set_of_image_mset [simp]:
"set_of (image_mset f M) = image f (set_of M)"
by (induct M) simp_all

lemma size_image_mset [simp]:
"size (image_mset f M) = size M"
by (induct M) simp_all

lemma image_mset_is_empty_iff [simp]:
"image_mset f M = {#} <-> M = {#}"
by (cases M) auto

syntax
"_comprehension1_mset" :: "'a => 'b => 'b multiset => 'a multiset"
("({#_/. _ :# _#})")
translations
"{#e. x:#M#}" == "CONST image_mset (%x. e) M"

syntax
"_comprehension2_mset" :: "'a => 'b => 'b multiset => bool => 'a multiset"
("({#_/ | _ :# _./ _#})")
translations
"{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"

text {*
This allows to write not just filters like @{term "{#x:#M. x<c#}"}
but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
"{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
@{term "{#x+x|x:#M. x<c#}"}.
*}

functor image_mset: image_mset
proof -
fix f g show "image_mset f o image_mset g = image_mset (f o g)"
proof
fix A
show "(image_mset f o image_mset g) A = image_mset (f o g) A"
by (induct A) simp_all
qed
show "image_mset id = id"
proof
fix A
show "image_mset id A = id A"
by (induct A) simp_all
qed
qed

declare image_mset.identity [simp]

subsection {* Further conversions *}

primrec multiset_of :: "'a list => 'a multiset" where
"multiset_of [] = {#}" |
"multiset_of (a # x) = multiset_of x + {# a #}"

lemma in_multiset_in_set:
"x ∈# multiset_of xs <-> x ∈ set xs"
by (induct xs) simp_all

lemma count_multiset_of:
"count (multiset_of xs) x = length (filter (λy. x = y) xs)"
by (induct xs) simp_all

lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
by (induct x) auto

lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
by (induct x) auto

lemma set_of_multiset_of[simp]: "set_of (multiset_of x) = set x"
by (induct x) auto

lemma mem_set_multiset_eq: "x ∈ set xs = (x :# multiset_of xs)"
by (induct xs) auto

lemma size_multiset_of [simp]: "size (multiset_of xs) = length xs"
by (induct xs) simp_all

lemma multiset_of_append [simp]:
"multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
by (induct xs arbitrary: ys) (auto simp: ac_simps)

lemma multiset_of_filter:
"multiset_of (filter P xs) = {#x :# multiset_of xs. P x #}"
by (induct xs) simp_all

lemma multiset_of_rev [simp]:
"multiset_of (rev xs) = multiset_of xs"
by (induct xs) simp_all

lemma surj_multiset_of: "surj multiset_of"
apply (unfold surj_def)
apply (rule allI)
apply (rule_tac M = y in multiset_induct)
apply auto
apply (rule_tac x = "x # xa" in exI)
apply auto
done

lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
by (induct x) auto

lemma distinct_count_atmost_1:
"distinct x = (! a. count (multiset_of x) a = (if a ∈ set x then 1 else 0))"
apply (induct x, simp, rule iffI, simp_all)
apply (rename_tac a b)
apply (rule conjI)
apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
apply (erule_tac x = a in allE, simp, clarify)
apply (erule_tac x = aa in allE, simp)
done

lemma multiset_of_eq_setD:
"multiset_of xs = multiset_of ys ==> set xs = set ys"
by (rule) (auto simp add:multiset_eq_iff set_count_greater_0)

lemma set_eq_iff_multiset_of_eq_distinct:
"distinct x ==> distinct y ==>
(set x = set y) = (multiset_of x = multiset_of y)"
by (auto simp: multiset_eq_iff distinct_count_atmost_1)

lemma set_eq_iff_multiset_of_remdups_eq:
"(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
apply (rule iffI)
apply (drule distinct_remdups [THEN distinct_remdups
[THEN set_eq_iff_multiset_of_eq_distinct [THEN iffD2]]])
apply simp
done

lemma multiset_of_compl_union [simp]:
"multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. ¬P x] = multiset_of xs"
by (induct xs) (auto simp: ac_simps)

lemma count_multiset_of_length_filter:
"count (multiset_of xs) x = length (filter (λy. x = y) xs)"
by (induct xs) auto

lemma nth_mem_multiset_of: "i < length ls ==> (ls ! i) :# multiset_of ls"
apply (induct ls arbitrary: i)
apply simp
apply (case_tac i)
apply auto
done

lemma multiset_of_remove1[simp]:
"multiset_of (remove1 a xs) = multiset_of xs - {#a#}"
by (induct xs) (auto simp add: multiset_eq_iff)

lemma multiset_of_eq_length:
assumes "multiset_of xs = multiset_of ys"
shows "length xs = length ys"
using assms by (metis size_multiset_of)

lemma multiset_of_eq_length_filter:
assumes "multiset_of xs = multiset_of ys"
shows "length (filter (λx. z = x) xs) = length (filter (λy. z = y) ys)"
using assms by (metis count_multiset_of)

lemma fold_multiset_equiv:
assumes f: "!!x y. x ∈ set xs ==> y ∈ set xs ==> f x o f y = f y o f x"
and equiv: "multiset_of xs = multiset_of ys"
shows "List.fold f xs = List.fold f ys"
using f equiv [symmetric]
proof (induct xs arbitrary: ys)
case Nil then show ?case by simp
next
case (Cons x xs)
then have *: "set ys = set (x # xs)" by (blast dest: multiset_of_eq_setD)
have "!!x y. x ∈ set ys ==> y ∈ set ys ==> f x o f y = f y o f x"
by (rule Cons.prems(1)) (simp_all add: *)
moreover from * have "x ∈ set ys" by simp
ultimately have "List.fold f ys = List.fold f (remove1 x ys) o f x" by (fact fold_remove1_split)
moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" by (auto intro: Cons.hyps)
ultimately show ?case by simp
qed

lemma multiset_of_insort [simp]:
"multiset_of (insort x xs) = multiset_of xs + {#x#}"
by (induct xs) (simp_all add: ac_simps)

lemma in_multiset_of:
"x ∈# multiset_of xs <-> x ∈ set xs"
by (induct xs) simp_all

lemma multiset_of_map:
"multiset_of (map f xs) = image_mset f (multiset_of xs)"
by (induct xs) simp_all

definition multiset_of_set :: "'a set => 'a multiset"
where
"multiset_of_set = folding.F (λx M. {#x#} + M) {#}"

interpretation multiset_of_set!: folding "λx M. {#x#} + M" "{#}"
where
"folding.F (λx M. {#x#} + M) {#} = multiset_of_set"
proof -
interpret comp_fun_commute "λx M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
show "folding (λx M. {#x#} + M)" by default (fact comp_fun_commute)
from multiset_of_set_def show "folding.F (λx M. {#x#} + M) {#} = multiset_of_set" ..
qed

lemma count_multiset_of_set [simp]:
"finite A ==> x ∈ A ==> count (multiset_of_set A) x = 1" (is "PROP ?P")
"¬ finite A ==> count (multiset_of_set A) x = 0" (is "PROP ?Q")
"x ∉ A ==> count (multiset_of_set A) x = 0" (is "PROP ?R")
proof -
{ fix A
assume "x ∉ A"
have "count (multiset_of_set A) x = 0"
proof (cases "finite A")
case False then show ?thesis by simp
next
case True from True `x ∉ A` show ?thesis by (induct A) auto
qed
} note * = this
then show "PROP ?P" "PROP ?Q" "PROP ?R"
by (auto elim!: Set.set_insert)
qed -- {* TODO: maybe define @{const multiset_of_set} also in terms of @{const Abs_multiset} *}

context linorder
begin

definition sorted_list_of_multiset :: "'a multiset => 'a list"
where
"sorted_list_of_multiset M = fold insort [] M"

lemma sorted_list_of_multiset_empty [simp]:
"sorted_list_of_multiset {#} = []"

lemma sorted_list_of_multiset_singleton [simp]:
"sorted_list_of_multiset {#x#} = [x]"
proof -
interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
show ?thesis by (simp add: sorted_list_of_multiset_def)
qed

lemma sorted_list_of_multiset_insert [simp]:
"sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
proof -
interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
show ?thesis by (simp add: sorted_list_of_multiset_def)
qed

end

lemma multiset_of_sorted_list_of_multiset [simp]:
"multiset_of (sorted_list_of_multiset M) = M"
by (induct M) simp_all

lemma sorted_list_of_multiset_multiset_of [simp]:
"sorted_list_of_multiset (multiset_of xs) = sort xs"
by (induct xs) simp_all

lemma finite_set_of_multiset_of_set:
assumes "finite A"
shows "set_of (multiset_of_set A) = A"
using assms by (induct A) simp_all

lemma infinite_set_of_multiset_of_set:
assumes "¬ finite A"
shows "set_of (multiset_of_set A) = {}"
using assms by simp

lemma set_sorted_list_of_multiset [simp]:
"set (sorted_list_of_multiset M) = set_of M"
by (induct M) (simp_all add: set_insort)

lemma sorted_list_of_multiset_of_set [simp]:
"sorted_list_of_multiset (multiset_of_set A) = sorted_list_of_set A"
by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)

subsection {* Big operators *}

no_notation times (infixl "*" 70)
no_notation Groups.one ("1")

locale comm_monoid_mset = comm_monoid
begin

definition F :: "'a multiset => 'a"
where
eq_fold: "F M = Multiset.fold f 1 M"

lemma empty [simp]:
"F {#} = 1"

lemma singleton [simp]:
"F {#x#} = x"
proof -
interpret comp_fun_commute
by default (simp add: fun_eq_iff left_commute)
show ?thesis by (simp add: eq_fold)
qed

lemma union [simp]:
"F (M + N) = F M * F N"
proof -
interpret comp_fun_commute f
by default (simp add: fun_eq_iff left_commute)
show ?thesis by (induct N) (simp_all add: left_commute eq_fold)
qed

end

notation times (infixl "*" 70)
notation Groups.one ("1")

begin

definition msetsum :: "'a multiset => 'a"
where
"msetsum = comm_monoid_mset.F plus 0"

sublocale msetsum!: comm_monoid_mset plus 0
where
"comm_monoid_mset.F plus 0 = msetsum"
proof -
show "comm_monoid_mset plus 0" ..
from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
qed

lemma setsum_unfold_msetsum:
"setsum f A = msetsum (image_mset f (multiset_of_set A))"
by (cases "finite A") (induct A rule: finite_induct, simp_all)

end

syntax
"_msetsum_image" :: "pttrn => 'b set => 'a => 'a::comm_monoid_add"
("(3SUM _:#_. _)" [0, 51, 10] 10)

syntax (xsymbols)
"_msetsum_image" :: "pttrn => 'b set => 'a => 'a::comm_monoid_add"
("(3∑_∈#_. _)" [0, 51, 10] 10)

syntax (HTML output)
"_msetsum_image" :: "pttrn => 'b set => 'a => 'a::comm_monoid_add"
("(3∑_∈#_. _)" [0, 51, 10] 10)

translations
"SUM i :# A. b" == "CONST msetsum (CONST image_mset (λi. b) A)"

context comm_monoid_mult
begin

definition msetprod :: "'a multiset => 'a"
where
"msetprod = comm_monoid_mset.F times 1"

sublocale msetprod!: comm_monoid_mset times 1
where
"comm_monoid_mset.F times 1 = msetprod"
proof -
show "comm_monoid_mset times 1" ..
from msetprod_def show "comm_monoid_mset.F times 1 = msetprod" ..
qed

lemma msetprod_empty:
"msetprod {#} = 1"
by (fact msetprod.empty)

lemma msetprod_singleton:
"msetprod {#x#} = x"
by (fact msetprod.singleton)

lemma msetprod_Un:
"msetprod (A + B) = msetprod A * msetprod B"
by (fact msetprod.union)

lemma setprod_unfold_msetprod:
"setprod f A = msetprod (image_mset f (multiset_of_set A))"
by (cases "finite A") (induct A rule: finite_induct, simp_all)

lemma msetprod_multiplicity:
"msetprod M = setprod (λx. x ^ count M x) (set_of M)"
by (simp add: Multiset.fold_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)

end

syntax
"_msetprod_image" :: "pttrn => 'b set => 'a => 'a::comm_monoid_mult"
("(3PROD _:#_. _)" [0, 51, 10] 10)

syntax (xsymbols)
"_msetprod_image" :: "pttrn => 'b set => 'a => 'a::comm_monoid_mult"
("(3∏_∈#_. _)" [0, 51, 10] 10)

syntax (HTML output)
"_msetprod_image" :: "pttrn => 'b set => 'a => 'a::comm_monoid_mult"
("(3∏_∈#_. _)" [0, 51, 10] 10)

translations
"PROD i :# A. b" == "CONST msetprod (CONST image_mset (λi. b) A)"

lemma (in comm_semiring_1) dvd_msetprod:
assumes "x ∈# A"
shows "x dvd msetprod A"
proof -
from assms have "A = (A - {#x#}) + {#x#}" by simp
then obtain B where "A = B + {#x#}" ..
then show ?thesis by simp
qed

subsection {* Cardinality *}

definition mcard :: "'a multiset => nat"
where
"mcard = msetsum o image_mset (λ_. 1)"

lemma mcard_empty [simp]:
"mcard {#} = 0"

lemma mcard_singleton [simp]:
"mcard {#a#} = Suc 0"

lemma mcard_plus [simp]:
"mcard (M + N) = mcard M + mcard N"

lemma mcard_empty_iff [simp]:
"mcard M = 0 <-> M = {#}"
by (induct M) simp_all

lemma mcard_unfold_setsum:
"mcard M = setsum (count M) (set_of M)"
proof (induct M)
case empty then show ?case by simp
next
case (add M x) then show ?case
by (cases "x ∈ set_of M")
(simp_all del: mem_set_of_iff add: setsum.distrib setsum.delta' insert_absorb, simp)
qed

lemma size_eq_mcard:
"size = mcard"

lemma mcard_multiset_of:
"mcard (multiset_of xs) = length xs"
by (induct xs) simp_all

subsection {* Alternative representations *}

subsubsection {* Lists *}

context linorder
begin

lemma multiset_of_insort [simp]:
"multiset_of (insort_key k x xs) = {#x#} + multiset_of xs"
by (induct xs) (simp_all add: ac_simps)

lemma multiset_of_sort [simp]:
"multiset_of (sort_key k xs) = multiset_of xs"
by (induct xs) (simp_all add: ac_simps)

text {*
This lemma shows which properties suffice to show that a function
@{text "f"} with @{text "f xs = ys"} behaves like sort.
*}

lemma properties_for_sort_key:
assumes "multiset_of ys = multiset_of xs"
and "!!k. k ∈ set ys ==> filter (λx. f k = f x) ys = filter (λx. f k = f x) xs"
and "sorted (map f ys)"
shows "sort_key f xs = ys"
using assms
proof (induct xs arbitrary: ys)
case Nil then show ?case by simp
next
case (Cons x xs)
from Cons.prems(2) have
"∀k ∈ set ys. filter (λx. f k = f x) (remove1 x ys) = filter (λx. f k = f x) xs"
with Cons.prems have "sort_key f xs = remove1 x ys"
by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
moreover from Cons.prems have "x ∈ set ys"
by (auto simp add: mem_set_multiset_eq intro!: ccontr)
ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
qed

lemma properties_for_sort:
assumes multiset: "multiset_of ys = multiset_of xs"
and "sorted ys"
shows "sort xs = ys"
proof (rule properties_for_sort_key)
from multiset show "multiset_of ys = multiset_of xs" .
from `sorted ys` show "sorted (map (λx. x) ys)" by simp
from multiset have "!!k. length (filter (λy. k = y) ys) = length (filter (λx. k = x) xs)"
by (rule multiset_of_eq_length_filter)
then have "!!k. replicate (length (filter (λy. k = y) ys)) k = replicate (length (filter (λx. k = x) xs)) k"
by simp
then show "!!k. k ∈ set ys ==> filter (λy. k = y) ys = filter (λx. k = x) xs"
qed

lemma sort_key_by_quicksort:
"sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
@ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
@ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
proof (rule properties_for_sort_key)
show "multiset_of ?rhs = multiset_of ?lhs"
by (rule multiset_eqI) (auto simp add: multiset_of_filter)
next
show "sorted (map f ?rhs)"
by (auto simp add: sorted_append intro: sorted_map_same)
next
fix l
assume "l ∈ set ?rhs"
let ?pivot = "f (xs ! (length xs div 2))"
have *: "!!x. f l = f x <-> f x = f l" by auto
have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
have "!!x P. P (f x) ?pivot ∧ f l = f x <-> P (f l) ?pivot ∧ f l = f x" by auto
then have "!!P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot ∧ f l = f x] =
[x \<leftarrow> sort_key f xs. P (f l) ?pivot ∧ f l = f x]" by simp
note *** = this [of "op <"] this [of "op >"] this [of "op ="]
show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
proof (cases "f l" ?pivot rule: linorder_cases)
case less
then have "f l ≠ ?pivot" and "¬ f l > ?pivot" by auto
with less show ?thesis
by (simp add: filter_sort [symmetric] ** ***)
next
case equal then show ?thesis
next
case greater
then have "f l ≠ ?pivot" and "¬ f l < ?pivot" by auto
with greater show ?thesis
by (simp add: filter_sort [symmetric] ** ***)
qed
qed

lemma sort_by_quicksort:
"sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
@ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
@ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
using sort_key_by_quicksort [of "λx. x", symmetric] by simp

text {* A stable parametrized quicksort *}

definition part :: "('b => 'a) => 'a => 'b list => 'b list × 'b list × 'b list" where
"part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"

lemma part_code [code]:
"part f pivot [] = ([], [], [])"
"part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
if x' < pivot then (x # lts, eqs, gts)
else if x' > pivot then (lts, eqs, x # gts)
else (lts, x # eqs, gts))"
by (auto simp add: part_def Let_def split_def)

lemma sort_key_by_quicksort_code [code]:
"sort_key f xs = (case xs of [] => []
| [x] => xs
| [x, y] => (if f x ≤ f y then xs else [y, x])
| _ => (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
in sort_key f lts @ eqs @ sort_key f gts))"
proof (cases xs)
case Nil then show ?thesis by simp
next
case (Cons _ ys) note hyps = Cons show ?thesis
proof (cases ys)
case Nil with hyps show ?thesis by simp
next
case (Cons _ zs) note hyps = hyps Cons show ?thesis
proof (cases zs)
case Nil with hyps show ?thesis by auto
next
case Cons
from sort_key_by_quicksort [of f xs]
have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
in sort_key f lts @ eqs @ sort_key f gts)"
by (simp only: split_def Let_def part_def fst_conv snd_conv)
with hyps Cons show ?thesis by (simp only: list.cases)
qed
qed
qed

end

hide_const (open) part

lemma multiset_of_remdups_le: "multiset_of (remdups xs) ≤ multiset_of xs"
by (induct xs) (auto intro: order_trans)

lemma multiset_of_update:
"i < length ls ==> multiset_of (ls[i := v]) = multiset_of ls - {#ls ! i#} + {#v#}"
proof (induct ls arbitrary: i)
case Nil then show ?case by simp
next
case (Cons x xs)
show ?case
proof (cases i)
case 0 then show ?thesis by simp
next
case (Suc i')
with Cons show ?thesis
apply simp
apply (subst add.commute [of "{#v#}" "{#x#}"])
apply simp
apply (rule mset_le_multiset_union_diff_commute)
done
qed
qed

lemma multiset_of_swap:
"i < length ls ==> j < length ls ==>
multiset_of (ls[j := ls ! i, i := ls ! j]) = multiset_of ls"
by (cases "i = j") (simp_all add: multiset_of_update nth_mem_multiset_of)

subsection {* The multiset order *}

subsubsection {* Well-foundedness *}

definition mult1 :: "('a × 'a) set => ('a multiset × 'a multiset) set" where
"mult1 r = {(N, M). ∃a M0 K. M = M0 + {#a#} ∧ N = M0 + K ∧
(∀b. b :# K --> (b, a) ∈ r)}"

definition mult :: "('a × 'a) set => ('a multiset × 'a multiset) set" where
"mult r = (mult1 r)⇧+"

lemma not_less_empty [iff]: "(M, {#}) ∉ mult1 r"

lemma less_add: "(N, M0 + {#a#}) ∈ mult1 r ==>
(∃M. (M, M0) ∈ mult1 r ∧ N = M + {#a#}) ∨
(∃K. (∀b. b :# K --> (b, a) ∈ r) ∧ N = M0 + K)"
(is "_ ==> ?case1 (mult1 r) ∨ ?case2")
proof (unfold mult1_def)
let ?r = "λK a. ∀b. b :# K --> (b, a) ∈ r"
let ?R = "λN M. ∃a M0 K. M = M0 + {#a#} ∧ N = M0 + K ∧ ?r K a"
let ?case1 = "?case1 {(N, M). ?R N M}"

assume "(N, M0 + {#a#}) ∈ {(N, M). ?R N M}"
then have "∃a' M0' K.
M0 + {#a#} = M0' + {#a'#} ∧ N = M0' + K ∧ ?r K a'" by simp
then show "?case1 ∨ ?case2"
proof (elim exE conjE)
fix a' M0' K
assume N: "N = M0' + K" and r: "?r K a'"
assume "M0 + {#a#} = M0' + {#a'#}"
then have "M0 = M0' ∧ a = a' ∨
(∃K'. M0 = K' + {#a'#} ∧ M0' = K' + {#a#})"
then show ?thesis
proof (elim disjE conjE exE)
assume "M0 = M0'" "a = a'"
with N r have "?r K a ∧ N = M0 + K" by simp
then have ?case2 .. then show ?thesis ..
next
fix K'
assume "M0' = K' + {#a#}"
with N have n: "N = K' + K + {#a#}" by (simp add: ac_simps)

assume "M0 = K' + {#a'#}"
with r have "?R (K' + K) M0" by blast
with n have ?case1 by simp then show ?thesis ..
qed
qed
qed

lemma all_accessible: "wf r ==> ∀M. M ∈ Wellfounded.acc (mult1 r)"
proof
let ?R = "mult1 r"
let ?W = "Wellfounded.acc ?R"
{
fix M M0 a
assume M0: "M0 ∈ ?W"
and wf_hyp: "!!b. (b, a) ∈ r ==> (∀M ∈ ?W. M + {#b#} ∈ ?W)"
and acc_hyp: "∀M. (M, M0) ∈ ?R --> M + {#a#} ∈ ?W"
have "M0 + {#a#} ∈ ?W"
proof (rule accI [of "M0 + {#a#}"])
fix N
assume "(N, M0 + {#a#}) ∈ ?R"
then have "((∃M. (M, M0) ∈ ?R ∧ N = M + {#a#}) ∨
(∃K. (∀b. b :# K --> (b, a) ∈ r) ∧ N = M0 + K))"
then show "N ∈ ?W"
proof (elim exE disjE conjE)
fix M assume "(M, M0) ∈ ?R" and N: "N = M + {#a#}"
from acc_hyp have "(M, M0) ∈ ?R --> M + {#a#} ∈ ?W" ..
from this and `(M, M0) ∈ ?R` have "M + {#a#} ∈ ?W" ..
then show "N ∈ ?W" by (simp only: N)
next
fix K
assume N: "N = M0 + K"
assume "∀b. b :# K --> (b, a) ∈ r"
then have "M0 + K ∈ ?W"
proof (induct K)
case empty
from M0 show "M0 + {#} ∈ ?W" by simp
next
from add.prems have "(x, a) ∈ r" by simp
with wf_hyp have "∀M ∈ ?W. M + {#x#} ∈ ?W" by blast
moreover from add have "M0 + K ∈ ?W" by simp
ultimately have "(M0 + K) + {#x#} ∈ ?W" ..
then show "M0 + (K + {#x#}) ∈ ?W" by (simp only: add.assoc)
qed
then show "N ∈ ?W" by (simp only: N)
qed
qed
} note tedious_reasoning = this

assume wf: "wf r"
fix M
show "M ∈ ?W"
proof (induct M)
show "{#} ∈ ?W"
proof (rule accI)
fix b assume "(b, {#}) ∈ ?R"
with not_less_empty show "b ∈ ?W" by contradiction
qed

fix M a assume "M ∈ ?W"
from wf have "∀M ∈ ?W. M + {#a#} ∈ ?W"
proof induct
fix a
assume r: "!!b. (b, a) ∈ r ==> (∀M ∈ ?W. M + {#b#} ∈ ?W)"
show "∀M ∈ ?W. M + {#a#} ∈ ?W"
proof
fix M assume "M ∈ ?W"
then show "M + {#a#} ∈ ?W"
by (rule acc_induct) (rule tedious_reasoning [OF _ r])
qed
qed
from this and `M ∈ ?W` show "M + {#a#} ∈ ?W" ..
qed
qed

theorem wf_mult1: "wf r ==> wf (mult1 r)"
by (rule acc_wfI) (rule all_accessible)

theorem wf_mult: "wf r ==> wf (mult r)"
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)

subsubsection {* Closure-free presentation *}

text {* One direction. *}

lemma mult_implies_one_step:
"trans r ==> (M, N) ∈ mult r ==>
∃I J K. N = I + J ∧ M = I + K ∧ J ≠ {#} ∧
(∀k ∈ set_of K. ∃j ∈ set_of J. (k, j) ∈ r)"
apply (unfold mult_def mult1_def set_of_def)
apply (erule converse_trancl_induct, clarify)
apply (rule_tac x = M0 in exI, simp, clarify)
apply (case_tac "a :# K")
apply (rule_tac x = I in exI)
apply (simp (no_asm))
apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
apply (drule_tac f = "λM. M - {#a#}" and x="?S + ?T" in arg_cong)
apply blast
apply (subgoal_tac "a :# I")
apply (rule_tac x = "I - {#a#}" in exI)
apply (rule_tac x = "J + {#a#}" in exI)
apply (rule_tac x = "K + Ka" in exI)
apply (rule conjI)
apply (simp add: multiset_eq_iff split: nat_diff_split)
apply (rule conjI)
apply (drule_tac f = "λM. M - {#a#}" and x="?S + ?T" in arg_cong, simp)
apply (simp add: multiset_eq_iff split: nat_diff_split)
apply blast
apply (subgoal_tac "a :# (M0 + {#a#})")
apply simp
apply (simp (no_asm))
done

lemma one_step_implies_mult_aux:
"trans r ==>
∀I J K. (size J = n ∧ J ≠ {#} ∧ (∀k ∈ set_of K. ∃j ∈ set_of J. (k, j) ∈ r))
--> (I + K, I + J) ∈ mult r"
apply (induct_tac n, auto)
apply (frule size_eq_Suc_imp_eq_union, clarify)
apply (rename_tac "J'", simp)
apply (erule notE, auto)
apply (case_tac "J' = {#}")
apply (rule r_into_trancl)
apply (simp add: mult1_def set_of_def, blast)
txt {* Now we know @{term "J' ≠ {#}"}. *}
apply (cut_tac M = K and P = "λx. (x, a) ∈ r" in multiset_partition)
apply (erule_tac P = "∀k ∈ set_of K. ?P k" in rev_mp)
apply (erule ssubst)
apply (subgoal_tac
"((I + {# x :# K. (x, a) ∈ r #}) + {# x :# K. (x, a) ∉ r #},
(I + {# x :# K. (x, a) ∈ r #}) + J') ∈ mult r")
prefer 2
apply force
apply (erule trancl_trans)
apply (rule r_into_trancl)
apply (rule_tac x = a in exI)
apply (rule_tac x = "I + J'" in exI)
done

lemma one_step_implies_mult:
"trans r ==> J ≠ {#} ==> ∀k ∈ set_of K. ∃j ∈ set_of J. (k, j) ∈ r
==> (I + K, I + J) ∈ mult r"
using one_step_implies_mult_aux by blast

subsubsection {* Partial-order properties *}

definition less_multiset :: "'a::order multiset => 'a multiset => bool" (infix "<#" 50) where
"M' <# M <-> (M', M) ∈ mult {(x', x). x' < x}"

definition le_multiset :: "'a::order multiset => 'a multiset => bool" (infix "<=#" 50) where
"M' <=# M <-> M' <# M ∨ M' = M"

notation (xsymbols) less_multiset (infix "⊂#" 50)
notation (xsymbols) le_multiset (infix "⊆#" 50)

interpretation multiset_order: order le_multiset less_multiset
proof -
have irrefl: "!!M :: 'a multiset. ¬ M ⊂# M"
proof
fix M :: "'a multiset"
assume "M ⊂# M"
then have MM: "(M, M) ∈ mult {(x, y). x < y}" by (simp add: less_multiset_def)
have "trans {(x'::'a, x). x' < x}"
by (rule transI) simp
moreover note MM
ultimately have "∃I J K. M = I + J ∧ M = I + K
∧ J ≠ {#} ∧ (∀k∈set_of K. ∃j∈set_of J. (k, j) ∈ {(x, y). x < y})"
by (rule mult_implies_one_step)
then obtain I J K where "M = I + J" and "M = I + K"
and "J ≠ {#}" and "(∀k∈set_of K. ∃j∈set_of J. (k, j) ∈ {(x, y). x < y})" by blast
then have aux1: "K ≠ {#}" and aux2: "∀k∈set_of K. ∃j∈set_of K. k < j" by auto
have "finite (set_of K)" by simp
moreover note aux2
ultimately have "set_of K = {}"
by (induct rule: finite_induct) (auto intro: order_less_trans)
with aux1 show False by simp
qed
have trans: "!!K M N :: 'a multiset. K ⊂# M ==> M ⊂# N ==> K ⊂# N"
unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
show "class.order (le_multiset :: 'a multiset => _) less_multiset"
by default (auto simp add: le_multiset_def irrefl dest: trans)
qed

lemma mult_less_irrefl [elim!]: "M ⊂# (M::'a::order multiset) ==> R"
by simp

subsubsection {* Monotonicity of multiset union *}

lemma mult1_union: "(B, D) ∈ mult1 r ==> (C + B, C + D) ∈ mult1 r"
apply (unfold mult1_def)
apply auto
apply (rule_tac x = a in exI)
apply (rule_tac x = "C + M0" in exI)
done

lemma union_less_mono2: "B ⊂# D ==> C + B ⊂# C + (D::'a::order multiset)"
apply (unfold less_multiset_def mult_def)
apply (erule trancl_induct)
apply (blast intro: mult1_union)
apply (blast intro: mult1_union trancl_trans)
done

lemma union_less_mono1: "B ⊂# D ==> B + C ⊂# D + (C::'a::order multiset)"
apply (subst add.commute [of B C])
apply (subst add.commute [of D C])
apply (erule union_less_mono2)
done

lemma union_less_mono:
"A ⊂# C ==> B ⊂# D ==> A + B ⊂# C + (D::'a::order multiset)"
by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)

interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
proof
qed (auto simp add: le_multiset_def intro: union_less_mono2)

subsection {* Termination proofs with multiset orders *}

lemma multi_member_skip: "x ∈# XS ==> x ∈# {# y #} + XS"
and multi_member_this: "x ∈# {# x #} + XS"
and multi_member_last: "x ∈# {# x #}"
by auto

definition "ms_strict = mult pair_less"
definition "ms_weak = ms_strict ∪ Id"

lemma ms_reduction_pair: "reduction_pair (ms_strict, ms_weak)"
unfolding reduction_pair_def ms_strict_def ms_weak_def pair_less_def
by (auto intro: wf_mult1 wf_trancl simp: mult_def)

lemma smsI:
"(set_of A, set_of B) ∈ max_strict ==> (Z + A, Z + B) ∈ ms_strict"
unfolding ms_strict_def
by (rule one_step_implies_mult) (auto simp add: max_strict_def pair_less_def elim!:max_ext.cases)

lemma wmsI:
"(set_of A, set_of B) ∈ max_strict ∨ A = {#} ∧ B = {#}
==> (Z + A, Z + B) ∈ ms_weak"
unfolding ms_weak_def ms_strict_def
by (auto simp add: pair_less_def max_strict_def elim!:max_ext.cases intro: one_step_implies_mult)

inductive pw_leq
where
pw_leq_empty: "pw_leq {#} {#}"
| pw_leq_step:  "[|(x,y) ∈ pair_leq; pw_leq X Y |] ==> pw_leq ({#x#} + X) ({#y#} + Y)"

lemma pw_leq_lstep:
"(x, y) ∈ pair_leq ==> pw_leq {#x#} {#y#}"
by (drule pw_leq_step) (rule pw_leq_empty, simp)

lemma pw_leq_split:
assumes "pw_leq X Y"
shows "∃A B Z. X = A + Z ∧ Y = B + Z ∧ ((set_of A, set_of B) ∈ max_strict ∨ (B = {#} ∧ A = {#}))"
using assms
proof (induct)
case pw_leq_empty thus ?case by auto
next
case (pw_leq_step x y X Y)
then obtain A B Z where
[simp]: "X = A + Z" "Y = B + Z"
and 1[simp]: "(set_of A, set_of B) ∈ max_strict ∨ (B = {#} ∧ A = {#})"
by auto
from pw_leq_step have "x = y ∨ (x, y) ∈ pair_less"
unfolding pair_leq_def by auto
thus ?case
proof
assume [simp]: "x = y"
have
"{#x#} + X = A + ({#y#}+Z)
∧ {#y#} + Y = B + ({#y#}+Z)
∧ ((set_of A, set_of B) ∈ max_strict ∨ (B = {#} ∧ A = {#}))"
by (auto simp: ac_simps)
thus ?case by (intro exI)
next
assume A: "(x, y) ∈ pair_less"
let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
have "{#x#} + X = ?A' + Z"
"{#y#} + Y = ?B' + Z"
moreover have
"(set_of ?A', set_of ?B') ∈ max_strict"
using 1 A unfolding max_strict_def
by (auto elim!: max_ext.cases)
ultimately show ?thesis by blast
qed
qed

lemma
assumes pwleq: "pw_leq Z Z'"
shows ms_strictI: "(set_of A, set_of B) ∈ max_strict ==> (Z + A, Z' + B) ∈ ms_strict"
and   ms_weakI1:  "(set_of A, set_of B) ∈ max_strict ==> (Z + A, Z' + B) ∈ ms_weak"
and   ms_weakI2:  "(Z + {#}, Z' + {#}) ∈ ms_weak"
proof -
from pw_leq_split[OF pwleq]
obtain A' B' Z''
where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
and mx_or_empty: "(set_of A', set_of B') ∈ max_strict ∨ (A' = {#} ∧ B' = {#})"
by blast
{
assume max: "(set_of A, set_of B) ∈ max_strict"
from mx_or_empty
have "(Z'' + (A + A'), Z'' + (B + B')) ∈ ms_strict"
proof
assume max': "(set_of A', set_of B') ∈ max_strict"
with max have "(set_of (A + A'), set_of (B + B')) ∈ max_strict"
by (auto simp: max_strict_def intro: max_ext_additive)
thus ?thesis by (rule smsI)
next
assume [simp]: "A' = {#} ∧ B' = {#}"
show ?thesis by (rule smsI) (auto intro: max)
qed
thus "(Z + A, Z' + B) ∈ ms_strict" by (simp add:ac_simps)
thus "(Z + A, Z' + B) ∈ ms_weak" by (simp add: ms_weak_def)
}
from mx_or_empty
have "(Z'' + A', Z'' + B') ∈ ms_weak" by (rule wmsI)
thus "(Z + {#}, Z' + {#}) ∈ ms_weak" by (simp add:ac_simps)
qed

lemma empty_neutral: "{#} + x = x" "x + {#} = x"
and nonempty_plus: "{# x #} + rs ≠ {#}"
and nonempty_single: "{# x #} ≠ {#}"
by auto

setup {*
let
fun msetT T = Type (@{type_name multiset}, [T]);

fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
| mk_mset T [x] = Const (@{const_name single}, T --> msetT T) \$ x
| mk_mset T (x :: xs) =
Const (@{const_name plus}, msetT T --> msetT T --> msetT T) \$
mk_mset T [x] \$ mk_mset T xs

fun mset_member_tac m i =
(if m <= 0 then
rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
else
rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i)

val mset_nonempty_tac =
rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}

val regroup_munion_conv =
Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
(map (fn t => t RS eq_reflection) (@{thms ac_simps} @ @{thms empty_neutral}))

fun unfold_pwleq_tac i =
(rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
ORELSE (rtac @{thm pw_leq_lstep} i)
ORELSE (rtac @{thm pw_leq_empty} i)

val set_of_simps = [@{thm set_of_empty}, @{thm set_of_single}, @{thm set_of_union},
@{thm Un_insert_left}, @{thm Un_empty_left}]
in
ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset
{
msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_of_simps,
smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
reduction_pair= @{thm ms_reduction_pair}
})
end
*}

subsection {* Legacy theorem bindings *}

lemmas multi_count_eq = multiset_eq_iff [symmetric]

lemma union_commute: "M + N = N + (M::'a multiset)"

lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"

lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"

lemmas union_ac = union_assoc union_commute union_lcomm

lemma union_right_cancel: "M + K = N + K <-> M = (N::'a multiset)"

lemma union_left_cancel: "K + M = K + N <-> M = (N::'a multiset)"

lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y ==> X = Y"

lemma mset_less_trans: "(M::'a multiset) < K ==> K < N ==> M < N"
by (fact order_less_trans)

lemma multiset_inter_commute: "A #∩ B = B #∩ A"
by (fact inf.commute)

lemma multiset_inter_assoc: "A #∩ (B #∩ C) = A #∩ B #∩ C"
by (fact inf.assoc [symmetric])

lemma multiset_inter_left_commute: "A #∩ (B #∩ C) = B #∩ (A #∩ C)"
by (fact inf.left_commute)

lemmas multiset_inter_ac =
multiset_inter_commute
multiset_inter_assoc
multiset_inter_left_commute

lemma mult_less_not_refl:
"¬ M ⊂# (M::'a::order multiset)"
by (fact multiset_order.less_irrefl)

lemma mult_less_trans:
"K ⊂# M ==> M ⊂# N ==> K ⊂# (N::'a::order multiset)"
by (fact multiset_order.less_trans)

lemma mult_less_not_sym:
"M ⊂# N ==> ¬ N ⊂# (M::'a::order multiset)"
by (fact multiset_order.less_not_sym)

lemma mult_less_asym:
"M ⊂# N ==> (¬ P ==> N ⊂# (M::'a::order multiset)) ==> P"
by (fact multiset_order.less_asym)

ML {*
fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T]))
(Const _ \$ t') =
let
val (maybe_opt, ps) =
Nitpick_Model.dest_plain_fun t' ||> op ~~
||> map (apsnd (snd o HOLogic.dest_number))
fun elems_for t =
case AList.lookup (op =) ps t of
SOME n => replicate n t
| NONE => [Const (maybe_name, elem_T --> elem_T) \$ t]
in
case maps elems_for (all_values elem_T) @
(if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
else []) of
[] => Const (@{const_name zero_class.zero}, T)
| ts => foldl1 (fn (t1, t2) =>
Const (@{const_name plus_class.plus}, T --> T --> T)
\$ t1 \$ t2)
(map (curry (op \$) (Const (@{const_name single},
elem_T --> T))) ts)
end
| multiset_postproc _ _ _ _ t = t
*}

declaration {*
Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
multiset_postproc
*}

hide_const (open) fold

subsection {* Naive implementation using lists *}

code_datatype multiset_of

lemma [code]:
"{#} = multiset_of []"
by simp

lemma [code]:
"{#x#} = multiset_of [x]"
by simp

lemma union_code [code]:
"multiset_of xs + multiset_of ys = multiset_of (xs @ ys)"
by simp

lemma [code]:
"image_mset f (multiset_of xs) = multiset_of (map f xs)"

lemma [code]:
"Multiset.filter f (multiset_of xs) = multiset_of (filter f xs)"

lemma [code]:
"multiset_of xs - multiset_of ys = multiset_of (fold remove1 ys xs)"

lemma [code]:
"multiset_of xs #∩ multiset_of ys =
multiset_of (snd (fold (λx (ys, zs).
if x ∈ set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, [])))"
proof -
have "!!zs. multiset_of (snd (fold (λx (ys, zs).
if x ∈ set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) =
(multiset_of xs #∩ multiset_of ys) + multiset_of zs"
by (induct xs arbitrary: ys)
then show ?thesis by simp
qed

lemma [code]:
"multiset_of xs #∪ multiset_of ys =
multiset_of (split append (fold (λx (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))"
proof -
have "!!zs. multiset_of (split append (fold (λx (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) =
(multiset_of xs #∪ multiset_of ys) + multiset_of zs"
by (induct xs arbitrary: ys) (simp_all add: multiset_eq_iff)
then show ?thesis by simp
qed

lemma [code_unfold]:
"x ∈# multiset_of xs <-> x ∈ set xs"

lemma [code]:
"count (multiset_of xs) x = fold (λy. if x = y then Suc else id) xs 0"
proof -
have "!!n. fold (λy. if x = y then Suc else id) xs n = count (multiset_of xs) x + n"
by (induct xs) simp_all
then show ?thesis by simp
qed

lemma [code]:
"set_of (multiset_of xs) = set xs"
by simp

lemma [code]:
"sorted_list_of_multiset (multiset_of xs) = sort xs"
by (induct xs) simp_all

lemma [code]: -- {* not very efficient, but representation-ignorant! *}
"multiset_of_set A = multiset_of (sorted_list_of_set A)"
apply (cases "finite A")
apply simp_all
apply (induct A rule: finite_induct)
done

lemma [code]:
"mcard (multiset_of xs) = length xs"

fun ms_lesseq_impl :: "'a list => 'a list => bool option" where
"ms_lesseq_impl [] ys = Some (ys ≠ [])"
| "ms_lesseq_impl (Cons x xs) ys = (case List.extract (op = x) ys of
None => None
| Some (ys1,_,ys2) => ms_lesseq_impl xs (ys1 @ ys2))"

lemma ms_lesseq_impl: "(ms_lesseq_impl xs ys = None <-> ¬ multiset_of xs ≤ multiset_of ys) ∧
(ms_lesseq_impl xs ys = Some True <-> multiset_of xs < multiset_of ys) ∧
(ms_lesseq_impl xs ys = Some False --> multiset_of xs = multiset_of ys)"
proof (induct xs arbitrary: ys)
case (Nil ys)
show ?case by (auto simp: mset_less_empty_nonempty)
next
case (Cons x xs ys)
show ?case
proof (cases "List.extract (op = x) ys")
case None
hence x: "x ∉ set ys" by (simp add: extract_None_iff)
{
assume "multiset_of (x # xs) ≤ multiset_of ys"
from set_of_mono[OF this] x have False by simp
} note nle = this
moreover
{
assume "multiset_of (x # xs) < multiset_of ys"
hence "multiset_of (x # xs) ≤ multiset_of ys" by auto
from nle[OF this] have False .
}
ultimately show ?thesis using None by auto
next
case (Some res)
obtain ys1 y ys2 where res: "res = (ys1,y,ys2)" by (cases res, auto)
note Some = Some[unfolded res]
from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp
hence id: "multiset_of ys = multiset_of (ys1 @ ys2) + {#x#}"
by (auto simp: ac_simps)
show ?thesis unfolding ms_lesseq_impl.simps
unfolding Some option.simps split
unfolding id
using Cons[of "ys1 @ ys2"]
unfolding mset_le_def mset_less_def by auto
qed
qed

lemma [code]: "multiset_of xs ≤ multiset_of ys <-> ms_lesseq_impl xs ys ≠ None"
using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)

lemma [code]: "multiset_of xs < multiset_of ys <-> ms_lesseq_impl xs ys = Some True"
using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)

instantiation multiset :: (equal) equal
begin

definition
[code del]: "HOL.equal A (B :: 'a multiset) <-> A = B"
lemma [code]: "HOL.equal (multiset_of xs) (multiset_of ys) <-> ms_lesseq_impl xs ys = Some False"
unfolding equal_multiset_def
using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)

instance
end

lemma [code]:
"msetsum (multiset_of xs) = listsum xs"

lemma [code]:
"msetprod (multiset_of xs) = fold times xs 1"
proof -
have "!!x. fold times xs x = msetprod (multiset_of xs) * x"
by (induct xs) (simp_all add: mult.assoc)
then show ?thesis by simp
qed

lemma [code]:
"size = mcard"
by (fact size_eq_mcard)

text {*
and @{const less_multiset} (multiset order).
*}

text {* Quickcheck generators *}

definition (in term_syntax)
msetify :: "'a::typerep list × (unit => Code_Evaluation.term)
=> 'a multiset × (unit => Code_Evaluation.term)" where
[code_unfold]: "msetify xs = Code_Evaluation.valtermify multiset_of {·} xs"

notation fcomp (infixl "o>" 60)
notation scomp (infixl "o->" 60)

instantiation multiset :: (random) random
begin

definition
"Quickcheck_Random.random i = Quickcheck_Random.random i o-> (λxs. Pair (msetify xs))"

instance ..

end

no_notation fcomp (infixl "o>" 60)
no_notation scomp (infixl "o->" 60)

instantiation multiset :: (full_exhaustive) full_exhaustive
begin

definition full_exhaustive_multiset :: "('a multiset × (unit => term) => (bool × term list) option) => natural => (bool × term list) option"
where
"full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (λxs. f (msetify xs)) i"

instance ..

end

hide_const (open) msetify

subsection {* BNF setup *}

lemma setsum_gt_0_iff:
fixes f :: "'a => nat" assumes "finite A"
shows "setsum f A > 0 <-> (∃ a ∈ A. f a > 0)"
(is "?L <-> ?R")
proof-
have "?L <-> ¬ setsum f A = 0" by fast
also have "... <-> (∃ a ∈ A. f a ≠ 0)" using assms by simp
also have "... <-> ?R" by simp
finally show ?thesis .
qed

lift_definition mmap :: "('a => 'b) => 'a multiset => 'b multiset" is
"λh f b. setsum f {a. h a = b ∧ f a > 0} :: nat"
unfolding multiset_def proof safe
fix h :: "'a => 'b" and f :: "'a => nat"
assume fin: "finite {a. 0 < f a}"  (is "finite ?A")
show "finite {b. 0 < setsum f {a. h a = b ∧ 0 < f a}}"
(is "finite {b. 0 < setsum f (?As b)}")
proof- let ?B = "{b. 0 < setsum f (?As b)}"
have "!! b. finite (?As b)" using fin by simp
hence B: "?B = {b. ?As b ≠ {}}" by (auto simp add: setsum_gt_0_iff)
hence "?B ⊆ h ` ?A" by auto
thus ?thesis using finite_surj[OF fin] by auto
qed
qed

lemma mmap_id0: "mmap id = id"
proof (intro ext multiset_eqI)
fix f a show "count (mmap id f) a = count (id f) a"
proof (cases "count f a = 0")
case False
hence 1: "{aa. aa = a ∧ aa ∈# f} = {a}" by auto
thus ?thesis by transfer auto
qed (transfer, simp)
qed

lemma inj_on_setsum_inv:
assumes 1: "(0::nat) < setsum (count f) {a. h a = b' ∧ a ∈# f}" (is "0 < setsum (count f) ?A'")
and     2: "{a. h a = b ∧ a ∈# f} = {a. h a = b' ∧ a ∈# f}" (is "?A = ?A'")
shows "b = b'"
using assms by (auto simp add: setsum_gt_0_iff)

lemma mmap_comp:
fixes h1 :: "'a => 'b" and h2 :: "'b => 'c"
shows "mmap (h2 o h1) = mmap h2 o mmap h1"
proof (intro ext multiset_eqI)
fix f :: "'a multiset" fix c :: 'c
let ?A = "{a. h2 (h1 a) = c ∧ a ∈# f}"
let ?As = "λ b. {a. h1 a = b ∧ a ∈# f}"
let ?B = "{b. h2 b = c ∧ 0 < setsum (count f) (?As b)}"
have 0: "{?As b | b.  b ∈ ?B} = ?As ` ?B" by auto
have "!! b. finite (?As b)" by transfer (simp add: multiset_def)
hence "?B = {b. h2 b = c ∧ ?As b ≠ {}}" by (auto simp add: setsum_gt_0_iff)
hence A: "?A = \<Union> {?As b | b.  b ∈ ?B}" by auto
have "setsum (count f) ?A = setsum (setsum (count f)) {?As b | b.  b ∈ ?B}"
unfolding A by transfer (intro setsum.Union_disjoint [simplified], auto simp: multiset_def setsum.Union_disjoint)
also have "... = setsum (setsum (count f)) (?As ` ?B)" unfolding 0 ..
also have "... = setsum (setsum (count f) o ?As) ?B"
by (intro setsum.reindex) (auto simp add: setsum_gt_0_iff inj_on_def)
also have "... = setsum (λ b. setsum (count f) (?As b)) ?B" unfolding comp_def ..
finally have "setsum (count f) ?A = setsum (λ b. setsum (count f) (?As b)) ?B" .
thus "count (mmap (h2 o h1) f) c = count ((mmap h2 o mmap h1) f) c"
by transfer (unfold comp_apply, blast)
qed

lemma mmap_cong:
assumes "!!a. a ∈# M ==> f a = g a"
shows "mmap f M = mmap g M"
using assms by transfer (auto intro!: setsum.cong)

context
begin
interpretation lifting_syntax .

lemma set_of_transfer[transfer_rule]: "(pcr_multiset op = ===> op =) (λf. {a. 0 < f a}) set_of"
unfolding set_of_def pcr_multiset_def cr_multiset_def rel_fun_def by auto

end

lemma set_of_mmap: "set_of o mmap h = image h o set_of"
proof (rule ext, unfold comp_apply)
fix M show "set_of (mmap h M) = h ` set_of M"
by transfer (auto simp add: multiset_def setsum_gt_0_iff)
qed

lemma multiset_of_surj:
"multiset_of ` {as. set as ⊆ A} = {M. set_of M ⊆ A}"
proof safe
fix M assume M: "set_of M ⊆ A"
obtain as where eq: "M = multiset_of as" using surj_multiset_of unfolding surj_def by auto
hence "set as ⊆ A" using M by auto
thus "M ∈ multiset_of ` {as. set as ⊆ A}" using eq by auto
next
show "!!x xa xb. [|set xa ⊆ A; xb ∈ set_of (multiset_of xa)|] ==> xb ∈ A"
by (erule set_mp) (unfold set_of_multiset_of)
qed

lemma card_of_set_of:
"(card_of {M. set_of M ⊆ A}, card_of {as. set as ⊆ A}) ∈ ordLeq"
apply(rule surj_imp_ordLeq[of _ multiset_of]) using multiset_of_surj by auto

lemma nat_sum_induct:
assumes "!!n1 n2. (!! m1 m2. m1 + m2 < n1 + n2 ==> phi m1 m2) ==> phi n1 n2"
shows "phi (n1::nat) (n2::nat)"
proof-
let ?chi = "λ n1n2 :: nat * nat. phi (fst n1n2) (snd n1n2)"
have "?chi (n1,n2)"
apply(induct rule: measure_induct[of "λ n1n2. fst n1n2 + snd n1n2" ?chi])
using assms by (metis fstI sndI)
thus ?thesis by simp
qed

lemma matrix_count:
fixes ct1 ct2 :: "nat => nat"
assumes "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
shows
"∃ ct. (∀ i1 ≤ n1. setsum (λ i2. ct i1 i2) {..<Suc n2} = ct1 i1) ∧
(∀ i2 ≤ n2. setsum (λ i1. ct i1 i2) {..<Suc n1} = ct2 i2)"
(is "?phi ct1 ct2 n1 n2")
proof-
have "∀ ct1 ct2 :: nat => nat.
setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} --> ?phi ct1 ct2 n1 n2"
proof(induct rule: nat_sum_induct[of
"λ n1 n2. ∀ ct1 ct2 :: nat => nat.
setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} --> ?phi ct1 ct2 n1 n2"],
clarify)
fix n1 n2 :: nat and ct1 ct2 :: "nat => nat"
assume IH: "!! m1 m2. m1 + m2 < n1 + n2 ==>
∀ dt1 dt2 :: nat => nat.
setsum dt1 {..<Suc m1} = setsum dt2 {..<Suc m2} --> ?phi dt1 dt2 m1 m2"
and ss: "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
show "?phi ct1 ct2 n1 n2"
proof(cases n1)
case 0 note n1 = 0
show ?thesis
proof(cases n2)
case 0 note n2 = 0
let ?ct = "λ i1 i2. ct2 0"
show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by simp
next
case (Suc m2) note n2 = Suc
let ?ct = "λ i1 i2. ct2 i2"
show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
qed
next
case (Suc m1) note n1 = Suc
show ?thesis
proof(cases n2)
case 0 note n2 = 0
let ?ct = "λ i1 i2. ct1 i1"
show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
next
case (Suc m2) note n2 = Suc
show ?thesis
proof(cases "ct1 n1 ≤ ct2 n2")
case True
def dt2 ≡ "λ i2. if i2 = n2 then ct2 i2 - ct1 n1 else ct2 i2"
have "setsum ct1 {..<Suc m1} = setsum dt2 {..<Suc n2}"
unfolding dt2_def using ss n1 True by auto
hence "?phi ct1 dt2 m1 n2" using IH[of m1 n2] n1 by simp
then obtain dt where
1: "!! i1. i1 ≤ m1 ==> setsum (λ i2. dt i1 i2) {..<Suc n2} = ct1 i1" and
2: "!! i2. i2 ≤ n2 ==> setsum (λ i1. dt i1 i2) {..<Suc m1} = dt2 i2" by auto
let ?ct = "λ i1 i2. if i1 = n1 then (if i2 = n2 then ct1 n1 else 0)
else dt i1 i2"
show ?thesis apply(rule exI[of _ ?ct])
using n1 n2 1 2 True unfolding dt2_def by simp
next
case False
hence False: "ct2 n2 < ct1 n1" by simp
def dt1 ≡ "λ i1. if i1 = n1 then ct1 i1 - ct2 n2 else ct1 i1"
have "setsum dt1 {..<Suc n1} = setsum ct2 {..<Suc m2}"
unfolding dt1_def using ss n2 False by auto
hence "?phi dt1 ct2 n1 m2" using IH[of n1 m2] n2 by simp
then obtain dt where
1: "!! i1. i1 ≤ n1 ==> setsum (λ i2. dt i1 i2) {..<Suc m2} = dt1 i1" and
2: "!! i2. i2 ≤ m2 ==> setsum (λ i1. dt i1 i2) {..<Suc n1} = ct2 i2" by force
let ?ct = "λ i1 i2. if i2 = n2 then (if i1 = n1 then ct2 n2 else 0)
else dt i1 i2"
show ?thesis apply(rule exI[of _ ?ct])
using n1 n2 1 2 False unfolding dt1_def by simp
qed
qed
qed
qed
thus ?thesis using assms by auto
qed

definition
"inj2 u B1 B2 ≡
∀ b1 b1' b2 b2'. {b1,b1'} ⊆ B1 ∧ {b2,b2'} ⊆ B2 ∧ u b1 b2 = u b1' b2'
--> b1 = b1' ∧ b2 = b2'"

lemma matrix_setsum_finite:
assumes B1: "B1 ≠ {}" "finite B1" and B2: "B2 ≠ {}" "finite B2" and u: "inj2 u B1 B2"
and ss: "setsum N1 B1 = setsum N2 B2"
shows "∃ M :: 'a => nat.
(∀ b1 ∈ B1. setsum (λ b2. M (u b1 b2)) B2 = N1 b1) ∧
(∀ b2 ∈ B2. setsum (λ b1. M (u b1 b2)) B1 = N2 b2)"
proof-
obtain n1 where "card B1 = Suc n1" using B1 by (metis card_insert finite.simps)
then obtain e1 where e1: "bij_betw e1 {..<Suc n1} B1"
using ex_bij_betw_finite_nat[OF B1(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
hence e1_inj: "inj_on e1 {..<Suc n1}" and e1_surj: "e1 ` {..<Suc n1} = B1"
unfolding bij_betw_def by auto
def f1 ≡ "inv_into {..<Suc n1} e1"
have f1: "bij_betw f1 B1 {..<Suc n1}"
and f1e1[simp]: "!! i1. i1 < Suc n1 ==> f1 (e1 i1) = i1"
and e1f1[simp]: "!! b1. b1 ∈ B1 ==> e1 (f1 b1) = b1" unfolding f1_def
apply (metis bij_betw_inv_into e1, metis bij_betw_inv_into_left e1 lessThan_iff)
by (metis e1_surj f_inv_into_f)
(*  *)
obtain n2 where "card B2 = Suc n2" using B2 by (metis card_insert finite.simps)
then obtain e2 where e2: "bij_betw e2 {..<Suc n2} B2"
using ex_bij_betw_finite_nat[OF B2(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
hence e2_inj: "inj_on e2 {..<Suc n2}" and e2_surj: "e2 ` {..<Suc n2} = B2"
unfolding bij_betw_def by auto
def f2 ≡ "inv_into {..<Suc n2} e2"
have f2: "bij_betw f2 B2 {..<Suc n2}"
and f2e2[simp]: "!! i2. i2 < Suc n2 ==> f2 (e2 i2) = i2"
and e2f2[simp]: "!! b2. b2 ∈ B2 ==> e2 (f2 b2) = b2" unfolding f2_def
apply (metis bij_betw_inv_into e2, metis bij_betw_inv_into_left e2 lessThan_iff)
by (metis e2_surj f_inv_into_f)
(*  *)
let ?ct1 = "N1 o e1"  let ?ct2 = "N2 o e2"
have ss: "setsum ?ct1 {..<Suc n1} = setsum ?ct2 {..<Suc n2}"
unfolding setsum.reindex[OF e1_inj, symmetric] setsum.reindex[OF e2_inj, symmetric]
e1_surj e2_surj using ss .
obtain ct where
ct1: "!! i1. i1 ≤ n1 ==> setsum (λ i2. ct i1 i2) {..<Suc n2} = ?ct1 i1" and
ct2: "!! i2. i2 ≤ n2 ==> setsum (λ i1. ct i1 i2) {..<Suc n1} = ?ct2 i2"
using matrix_count[OF ss] by blast
(*  *)
def A ≡ "{u b1 b2 | b1 b2. b1 ∈ B1 ∧ b2 ∈ B2}"
have "∀ a ∈ A. ∃ b1b2 ∈ B1 <*> B2. u (fst b1b2) (snd b1b2) = a"
unfolding A_def Ball_def mem_Collect_eq by auto
then obtain h1h2 where h12:
"!!a. a ∈ A ==> u (fst (h1h2 a)) (snd (h1h2 a)) = a ∧ h1h2 a ∈ B1 <*> B2" by metis
def h1 ≡ "fst o h1h2"  def h2 ≡ "snd o h1h2"
have h12[simp]: "!!a. a ∈ A ==> u (h1 a) (h2 a) = a"
"!! a. a ∈ A ==> h1 a ∈ B1"  "!! a. a ∈ A ==> h2 a ∈ B2"
using h12 unfolding h1_def h2_def by force+
{fix b1 b2 assume b1: "b1 ∈ B1" and b2: "b2 ∈ B2"
hence inA: "u b1 b2 ∈ A" unfolding A_def by auto
hence "u b1 b2 = u (h1 (u b1 b2)) (h2 (u b1 b2))" by auto
moreover have "h1 (u b1 b2) ∈ B1" "h2 (u b1 b2) ∈ B2" using inA by auto
ultimately have "h1 (u b1 b2) = b1 ∧ h2 (u b1 b2) = b2"
using u b1 b2 unfolding inj2_def by fastforce
}
hence h1[simp]: "!! b1 b2. [|b1 ∈ B1; b2 ∈ B2|] ==> h1 (u b1 b2) = b1" and
h2[simp]: "!! b1 b2. [|b1 ∈ B1; b2 ∈ B2|] ==> h2 (u b1 b2) = b2" by auto
def M ≡ "λ a. ct (f1 (h1 a)) (f2 (h2 a))"
show ?thesis
apply(rule exI[of _ M]) proof safe
fix b1 assume b1: "b1 ∈ B1"
hence f1b1: "f1 b1 ≤ n1" using f1 unfolding bij_betw_def
by (metis image_eqI lessThan_iff less_Suc_eq_le)
have "(∑b2∈B2. M (u b1 b2)) = (∑i2<Suc n2. ct (f1 b1) (f2 (e2 i2)))"
unfolding e2_surj[symmetric] setsum.reindex[OF e2_inj]
unfolding M_def comp_def apply(intro setsum.cong) apply force
by (metis e2_surj b1 h1 h2 imageI)
also have "... = N1 b1" using b1 ct1[OF f1b1] by simp
finally show "(∑b2∈B2. M (u b1 b2)) = N1 b1" .
next
fix b2 assume b2: "b2 ∈ B2"
hence f2b2: "f2 b2 ≤ n2" using f2 unfolding bij_betw_def
by (metis image_eqI lessThan_iff less_Suc_eq_le)
have "(∑b1∈B1. M (u b1 b2)) = (∑i1<Suc n1. ct (f1 (e1 i1)) (f2 b2))"
unfolding e1_surj[symmetric] setsum.reindex[OF e1_inj]
unfolding M_def comp_def apply(intro setsum.cong) apply force
by (metis e1_surj b2 h1 h2 imageI)
also have "... = N2 b2" using b2 ct2[OF f2b2] by simp
finally show "(∑b1∈B1. M (u b1 b2)) = N2 b2" .
qed
qed

lemma supp_vimage_mmap: "set_of M ⊆ f -` (set_of (mmap f M))"
by transfer (auto simp: multiset_def setsum_gt_0_iff)

lemma mmap_ge_0: "b ∈# mmap f M <-> (∃a. a ∈# M ∧ f a = b)"
by transfer (auto simp: multiset_def setsum_gt_0_iff)

lemma finite_twosets:
assumes "finite B1" and "finite B2"
shows "finite {u b1 b2 |b1 b2. b1 ∈ B1 ∧ b2 ∈ B2}"  (is "finite ?A")
proof-
have A: "?A = (λ b1b2. u (fst b1b2) (snd b1b2)) ` (B1 <*> B2)" by force
show ?thesis unfolding A using finite_cartesian_product[OF assms] by auto
qed

(* Weak pullbacks: *)
definition wpull where
"wpull A B1 B2 f1 f2 p1 p2 <->
(∀ b1 b2. b1 ∈ B1 ∧ b2 ∈ B2 ∧ f1 b1 = f2 b2 --> (∃ a ∈ A. p1 a = b1 ∧ p2 a = b2))"

(* Weak pseudo-pullbacks *)
definition wppull where
"wppull A B1 B2 f1 f2 e1 e2 p1 p2 <->
(∀ b1 b2. b1 ∈ B1 ∧ b2 ∈ B2 ∧ f1 b1 = f2 b2 -->
(∃ a ∈ A. e1 (p1 a) = e1 b1 ∧ e2 (p2 a) = e2 b2))"

(* The pullback of sets *)
definition thePull where
"thePull B1 B2 f1 f2 = {(b1,b2). b1 ∈ B1 ∧ b2 ∈ B2 ∧ f1 b1 = f2 b2}"

lemma wpull_thePull:
"wpull (thePull B1 B2 f1 f2) B1 B2 f1 f2 fst snd"
unfolding wpull_def thePull_def by auto

lemma wppull_thePull:
assumes "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
shows
"∃ j. ∀ a' ∈ thePull B1 B2 f1 f2.
j a' ∈ A ∧
e1 (p1 (j a')) = e1 (fst a') ∧ e2 (p2 (j a')) = e2 (snd a')"
(is "∃ j. ∀ a' ∈ ?A'. ?phi a' (j a')")
proof(rule bchoice[of ?A' ?phi], default)
fix a' assume a': "a' ∈ ?A'"
hence "fst a' ∈ B1" unfolding thePull_def by auto
moreover
from a' have "snd a' ∈ B2" unfolding thePull_def by auto
moreover have "f1 (fst a') = f2 (snd a')"
using a' unfolding csquare_def thePull_def by auto
ultimately show "∃ ja'. ?phi a' ja'"
using assms unfolding wppull_def by blast
qed

lemma wpull_wppull:
assumes wp: "wpull A' B1 B2 f1 f2 p1' p2'" and
1: "∀ a' ∈ A'. j a' ∈ A ∧ e1 (p1 (j a')) = e1 (p1' a') ∧ e2 (p2 (j a')) = e2 (p2' a')"
shows "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
unfolding wppull_def proof safe
fix b1 b2
assume b1: "b1 ∈ B1" and b2: "b2 ∈ B2" and f: "f1 b1 = f2 b2"
then obtain a' where a': "a' ∈ A'" and b1: "b1 = p1' a'" and b2: "b2 = p2' a'"
using wp unfolding wpull_def by blast
show "∃a∈A. e1 (p1 a) = e1 b1 ∧ e2 (p2 a) = e2 b2"
apply (rule bexI[of _ "j a'"]) unfolding b1 b2 using a' 1 by auto
qed

lemma wppull_fstOp_sndOp:
shows "wppull (Collect (split (P OO Q))) (Collect (split P)) (Collect (split Q))
snd fst fst snd (BNF_Def.fstOp P Q) (BNF_Def.sndOp P Q)"
using pick_middlep unfolding wppull_def fstOp_def sndOp_def relcompp.simps by auto

lemma wpull_mmap:
fixes A :: "'a set" and B1 :: "'b1 set" and B2 :: "'b2 set"
assumes wp: "wpull A B1 B2 f1 f2 p1 p2"
shows
"wpull {M. set_of M ⊆ A}
{N1. set_of N1 ⊆ B1} {N2. set_of N2 ⊆ B2}
(mmap f1) (mmap f2) (mmap p1) (mmap p2)"
unfolding wpull_def proof (safe, unfold Bex_def mem_Collect_eq)
fix N1 :: "'b1 multiset" and N2 :: "'b2 multiset"
assume mmap': "mmap f1 N1 = mmap f2 N2"
and N1[simp]: "set_of N1 ⊆ B1"
and N2[simp]: "set_of N2 ⊆ B2"
def P ≡ "mmap f1 N1"
have P1: "P = mmap f1 N1" and P2: "P = mmap f2 N2" unfolding P_def using mmap' by auto
note P = P1 P2
have fin_N1[simp]: "finite (set_of N1)"
and fin_N2[simp]: "finite (set_of N2)"
and fin_P[simp]: "finite (set_of P)" by auto

def set1 ≡ "λ c. {b1 ∈ set_of N1. f1 b1 = c}"
have set1[simp]: "!! c b1. b1 ∈ set1 c ==> f1 b1 = c" unfolding set1_def by auto
have fin_set1: "!! c. c ∈ set_of P ==> finite (set1 c)"
using N1(1) unfolding set1_def multiset_def by auto
have set1_NE: "!! c. c ∈ set_of P ==> set1 c ≠ {}"
unfolding set1_def set_of_def P mmap_ge_0 by auto
have supp_N1_set1: "set_of N1 = (\<Union> c ∈ set_of P. set1 c)"
using supp_vimage_mmap[of N1 f1] unfolding set1_def P1 by auto
hence set1_inclN1: "!!c. c ∈ set_of P ==> set1 c ⊆ set_of N1" by auto
hence set1_incl: "!! c. c ∈ set_of P ==> set1 c ⊆ B1" using N1 by blast
have set1_disj: "!! c c'. c ≠ c' ==> set1 c ∩ set1 c' = {}"
unfolding set1_def by auto
have setsum_set1: "!! c. setsum (count N1) (set1 c) = count P c"
unfolding P1 set1_def by transfer (auto intro: setsum.cong)

def set2 ≡ "λ c. {b2 ∈ set_of N2. f2 b2 = c}"
have set2[simp]: "!! c b2. b2 ∈ set2 c ==> f2 b2 = c" unfolding set2_def by auto
have fin_set2: "!! c. c ∈ set_of P ==> finite (set2 c)"
using N2(1) unfolding set2_def multiset_def by auto
have set2_NE: "!! c. c ∈ set_of P ==> set2 c ≠ {}"
unfolding set2_def P2 mmap_ge_0 set_of_def by auto
have supp_N2_set2: "set_of N2 = (\<Union> c ∈ set_of P. set2 c)"
using supp_vimage_mmap[of N2 f2] unfolding set2_def P2 by auto
hence set2_inclN2: "!!c. c ∈ set_of P ==> set2 c ⊆ set_of N2" by auto
hence set2_incl: "!! c. c ∈ set_of P ==> set2 c ⊆ B2" using N2 by blast
have set2_disj: "!! c c'. c ≠ c' ==> set2 c ∩ set2 c' = {}"
unfolding set2_def by auto
have setsum_set2: "!! c. setsum (count N2) (set2 c) = count P c"
unfolding P2 set2_def by transfer (auto intro: setsum.cong)

have ss: "!! c. c ∈ set_of P ==> setsum (count N1) (set1 c) = setsum (count N2) (set2 c)"
unfolding setsum_set1 setsum_set2 ..
have "∀ c ∈ set_of P. ∀ b1b2 ∈ (set1 c) × (set2 c).
∃ a ∈ A. p1 a = fst b1b2 ∧ p2 a = snd b1b2"
using wp set1_incl set2_incl unfolding wpull_def Ball_def mem_Collect_eq
by simp (metis set1 set2 set_rev_mp)
then obtain uu where uu:
"∀ c ∈ set_of P. ∀ b1b2 ∈ (set1 c) × (set2 c).
uu c b1b2 ∈ A ∧ p1 (uu c b1b2) = fst b1b2 ∧ p2 (uu c b1b2) = snd b1b2" by metis
def u ≡ "λ c b1 b2. uu c (b1,b2)"
have u[simp]:
"!! c b1 b2. [|c ∈ set_of P; b1 ∈ set1 c; b2 ∈ set2 c|] ==> u c b1 b2 ∈ A"
"!! c b1 b2. [|c ∈ set_of P; b1 ∈ set1 c; b2 ∈ set2 c|] ==> p1 (u c b1 b2) = b1"
"!! c b1 b2. [|c ∈ set_of P; b1 ∈ set1 c; b2 ∈ set2 c|] ==> p2 (u c b1 b2) = b2"
using uu unfolding u_def by auto
{fix c assume c: "c ∈ set_of P"
have "inj2 (u c) (set1 c) (set2 c)" unfolding inj2_def proof clarify
fix b1 b1' b2 b2'
assume "{b1, b1'} ⊆ set1 c" "{b2, b2'} ⊆ set2 c" and 0: "u c b1 b2 = u c b1' b2'"
hence "p1 (u c b1 b2) = b1 ∧ p2 (u c b1 b2) = b2 ∧
p1 (u c b1' b2') = b1' ∧ p2 (u c b1' b2') = b2'"
using u(2)[OF c] u(3)[OF c] by simp metis
thus "b1 = b1' ∧ b2 = b2'" using 0 by auto
qed
} note inj = this
def sset ≡ "λ c. {u c b1 b2 | b1 b2. b1 ∈ set1 c ∧ b2 ∈ set2 c}"
have fin_sset[simp]: "!! c. c ∈ set_of P ==> finite (sset c)" unfolding sset_def
using fin_set1 fin_set2 finite_twosets by blast
have sset_A: "!! c. c ∈ set_of P ==> sset c ⊆ A" unfolding sset_def by auto
{fix c a assume c: "c ∈ set_of P" and ac: "a ∈ sset c"
then obtain b1 b2 where b1: "b1 ∈ set1 c" and b2: "b2 ∈ set2 c"
and a: "a = u c b1 b2" unfolding sset_def by auto
have "p1 a ∈ set1 c" and p2a: "p2 a ∈ set2 c"
using ac a b1 b2 c u(2) u(3) by simp+
hence "u c (p1 a) (p2 a) = a" unfolding a using b1 b2 inj[OF c]
unfolding inj2_def by (metis c u(2) u(3))
} note u_p12[simp] = this
{fix c a assume c: "c ∈ set_of P" and ac: "a ∈ sset c"
hence "p1 a ∈ set1 c" unfolding sset_def by auto
}note p1[simp] = this
{fix c a assume c: "c ∈ set_of P" and ac: "a ∈ sset c"
hence "p2 a ∈ set2 c" unfolding sset_def by auto
}note p2[simp] = this

{fix c assume c: "c ∈ set_of P"
hence "∃ M. (∀ b1 ∈ set1 c. setsum (λ b2. M (u c b1 b2)) (set2 c) = count N1 b1) ∧
(∀ b2 ∈ set2 c. setsum (λ b1. M (u c b1 b2)) (set1 c) = count N2 b2)"
unfolding sset_def
using matrix_setsum_finite[OF set1_NE[OF c] fin_set1[OF c]
set2_NE[OF c] fin_set2[OF c] inj[OF c] ss[OF c]] by auto
}
then obtain Ms where
ss1: "!! c b1. [|c ∈ set_of P; b1 ∈ set1 c|] ==>
setsum (λ b2. Ms c (u c b1 b2)) (set2 c) = count N1 b1" and
ss2: "!! c b2. [|c ∈ set_of P; b2 ∈ set2 c|] ==>
setsum (λ b1. Ms c (u c b1 b2)) (set1 c) = count N2 b2"
by metis
def SET ≡ "\<Union> c ∈ set_of P. sset c"
have fin_SET[simp]: "finite SET" unfolding SET_def apply(rule finite_UN_I) by auto
have SET_A: "SET ⊆ A" unfolding SET_def using sset_A by blast
have u_SET[simp]: "!! c b1 b2. [|c ∈ set_of P; b1 ∈ set1 c; b2 ∈ set2 c|] ==> u c b1 b2 ∈ SET"
unfolding SET_def sset_def by blast
{fix c a assume c: "c ∈ set_of P" and a: "a ∈ SET" and p1a: "p1 a ∈ set1 c"
then obtain c' where c': "c' ∈ set_of P" and ac': "a ∈ sset c'"
unfolding SET_def by auto
hence "p1 a ∈ set1 c'" unfolding sset_def by auto
hence eq: "c = c'" using p1a c c' set1_disj by auto
hence "a ∈ sset c" using ac' by simp
} note p1_rev = this
{fix c a assume c: "c ∈ set_of P" and a: "a ∈ SET" and p2a: "p2 a ∈ set2 c"
then obtain c' where c': "c' ∈ set_of P" and ac': "a ∈ sset c'"
unfolding SET_def by auto
hence "p2 a ∈ set2 c'" unfolding sset_def by auto
hence eq: "c = c'" using p2a c c' set2_disj by auto
hence "a ∈ sset c" using ac' by simp
} note p2_rev = this

have "∀ a ∈ SET. ∃ c ∈ set_of P. a ∈ sset c" unfolding SET_def by auto
then obtain h where h: "∀ a ∈ SET. h a ∈ set_of P ∧ a ∈ sset (h a)" by metis
have h_u[simp]: "!! c b1 b2. [|c ∈ set_of P; b1 ∈ set1 c; b2 ∈ set2 c|]
==> h (u c b1 b2) = c"
by (metis h p2 set2 u(3) u_SET)
have h_u1: "!! c b1 b2. [|c ∈ set_of P; b1 ∈ set1 c; b2 ∈ set2 c|]
==> h (u c b1 b2) = f1 b1"
using h unfolding sset_def by auto
have h_u2: "!! c b1 b2. [|c ∈ set_of P; b1 ∈ set1 c; b2 ∈ set2 c|]
==> h (u c b1 b2) = f2 b2"
using h unfolding sset_def by auto
def M ≡
"Abs_multiset (λ a. if a ∈ SET ∧ p1 a ∈ set_of N1 ∧ p2 a ∈ set_of N2 then Ms (h a) a else 0)"
have "(λ a. if a ∈ SET ∧ p1 a ∈ set_of N1 ∧ p2 a ∈ set_of N2 then Ms (h a) a else 0) ∈ multiset"
unfolding multiset_def by auto
hence [transfer_rule]: "pcr_multiset op = (λ a. if a ∈ SET ∧ p1 a ∈ set_of N1 ∧ p2 a ∈ set_of N2 then Ms (h a) a else 0) M"
unfolding M_def pcr_multiset_def cr_multiset_def by (auto simp: Abs_multiset_inverse)
have sM: "set_of M ⊆ SET" "set_of M ⊆ p1 -` (set_of N1)" "set_of M ⊆ p2 -` set_of N2"
by (transfer, auto split: split_if_asm)+
show "∃M. set_of M ⊆ A ∧ mmap p1 M = N1 ∧ mmap p2 M = N2"
proof(rule exI[of _ M], safe)
fix a assume *: "a ∈ set_of M"
from SET_A show "a ∈ A"
proof (cases "a ∈ SET")
case False thus ?thesis using * by transfer' auto
qed blast
next
show "mmap p1 M = N1"
proof(intro multiset_eqI)
fix b1
let ?K = "{a. p1 a = b1 ∧ a ∈# M}"
have "setsum (count M) ?K = count N1 b1"
proof(cases "b1 ∈ set_of N1")
case False
hence "?K = {}" using sM(2) by auto
thus ?thesis using False by auto
next
case True
def c ≡ "f1 b1"
have c: "c ∈ set_of P" and b1: "b1 ∈ set1 c"
unfolding set1_def c_def P1 using True by (auto simp: comp_eq_dest[OF set_of_mmap])
with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p1 a = b1 ∧ a ∈ SET}"
by transfer (force intro: setsum.mono_neutral_cong_left split: split_if_asm)
also have "... = setsum (count M) ((λ b2. u c b1 b2) ` (set2 c))"
apply(rule setsum.cong) using c b1 proof safe
fix a assume p1a: "p1 a ∈ set1 c" and "c ∈ set_of P" and "a ∈ SET"
hence ac: "a ∈ sset c" using p1_rev by auto
hence "a = u c (p1 a) (p2 a)" using c by auto
moreover have "p2 a ∈ set2 c" using ac c by auto
ultimately show "a ∈ u c (p1 a) ` set2 c" by auto
qed auto
also have "... = setsum (λ b2. count M (u c b1 b2)) (set2 c)"
unfolding comp_def[symmetric] apply(rule setsum.reindex)
using inj unfolding inj_on_def inj2_def using b1 c u(3) by blast
also have "... = count N1 b1" unfolding ss1[OF c b1, symmetric]
apply(rule setsum.cong[OF refl]) apply (transfer fixing: Ms u c b1 set2)
using True h_u[OF c b1] set2_def u(2,3)[OF c b1] u_SET[OF c b1]
[[hypsubst_thin = true]]
by fastforce
finally show ?thesis .
qed
thus "count (mmap p1 M) b1 = count N1 b1" by transfer
qed
next
show "mmap p2 M = N2"
proof(intro multiset_eqI)
fix b2
let ?K = "{a. p2 a = b2 ∧ a ∈# M}"
have "setsum (count M) ?K = count N2 b2"
proof(cases "b2 ∈ set_of N2")
case False
hence "?K = {}" using sM(3) by auto
thus ?thesis using False by auto
next
case True
def c ≡ "f2 b2"
have c: "c ∈ set_of P" and b2: "b2 ∈ set2 c"
unfolding set2_def c_def P2 using True by (auto simp: comp_eq_dest[OF set_of_mmap])
with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p2 a = b2 ∧ a ∈ SET}"
by transfer (force intro: setsum.mono_neutral_cong_left split: split_if_asm)
also have "... = setsum (count M) ((λ b1. u c b1 b2) ` (set1 c))"
apply(rule setsum.cong) using c b2 proof safe
fix a assume p2a: "p2 a ∈ set2 c" and "c ∈ set_of P" and "a ∈ SET"
hence ac: "a ∈ sset c" using p2_rev by auto
hence "a = u c (p1 a) (p2 a)" using c by auto
moreover have "p1 a ∈ set1 c" using ac c by auto
ultimately show "a ∈ (λx. u c x (p2 a)) ` set1 c" by auto
qed auto
also have "... = setsum (count M o (λ b1. u c b1 b2)) (set1 c)"
apply(rule setsum.reindex)
using inj unfolding inj_on_def inj2_def using b2 c u(2) by blast
also have "... = setsum (λ b1. count M (u c b1 b2)) (set1 c)" by simp
also have "... = count N2 b2" unfolding ss2[OF c b2, symmetric] comp_def
apply(rule setsum.cong[OF refl]) apply (transfer fixing: Ms u c b2 set1)
using True h_u1[OF c _ b2] u(2,3)[OF c _ b2] u_SET[OF c _ b2] set1_def
[[hypsubst_thin = true]]
by fastforce
finally show ?thesis .
qed
thus "count (mmap p2 M) b2 = count N2 b2" by transfer
qed
qed
qed

lemma set_of_bd: "(card_of (set_of x), natLeq) ∈ ordLeq"
by transfer
(auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def)

lemma wppull_mmap:
assumes "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
shows "wppull {M. set_of M ⊆ A} {N1. set_of N1 ⊆ B1} {N2. set_of N2 ⊆ B2}
(mmap f1) (mmap f2) (mmap e1) (mmap e2) (mmap p1) (mmap p2)"
proof -
from assms obtain j where j: "∀a'∈thePull B1 B2 f1 f2.
j a' ∈ A ∧ e1 (p1 (j a')) = e1 (fst a') ∧ e2 (p2 (j a')) = e2 (snd a')"
by (blast dest: wppull_thePull)
then show ?thesis
by (intro wpull_wppull[OF wpull_mmap[OF wpull_thePull], of _ _ _ _ "mmap j"])
(auto simp: comp_eq_dest_lhs[OF mmap_comp[symmetric]] comp_eq_dest[OF set_of_mmap]
intro!: mmap_cong simp del: mem_set_of_iff simp: mem_set_of_iff[symmetric])
qed

bnf "'a multiset"
map: mmap
sets: set_of
bd: natLeq
wits: "{#}"
by (auto simp add: mmap_id0 mmap_comp set_of_mmap natLeq_card_order natLeq_cinfinite set_of_bd
Grp_def relcompp.simps intro: mmap_cong)
(metis wppull_mmap[OF wppull_fstOp_sndOp, unfolded wppull_def
o_eq_dest_lhs[OF mmap_comp[symmetric]] fstOp_def sndOp_def comp_def, simplified])

inductive rel_multiset' where
Zero[intro]: "rel_multiset' R {#} {#}"
| Plus[intro]: "[|R a b; rel_multiset' R M N|] ==> rel_multiset' R (M + {#a#}) (N + {#b#})"

lemma map_multiset_Zero_iff[simp]: "mmap f M = {#} <-> M = {#}"
by (metis image_is_empty multiset.set_map set_of_eq_empty_iff)

lemma map_multiset_Zero[simp]: "mmap f {#} = {#}" by simp

lemma rel_multiset_Zero: "rel_multiset R {#} {#}"
unfolding rel_multiset_def Grp_def by auto

declare multiset.count[simp]
declare Abs_multiset_inverse[simp]
declare multiset.count_inverse[simp]
declare union_preserves_multiset[simp]

lemma map_multiset_Plus[simp]: "mmap f (M1 + M2) = mmap f M1 + mmap f M2"
proof (intro multiset_eqI, transfer fixing: f)
fix x :: 'a and M1 M2 :: "'b => nat"
assume "M1 ∈ multiset" "M2 ∈ multiset"
hence "setsum M1 {a. f a = x ∧ 0 < M1 a} = setsum M1 {a. f a = x ∧ 0 < M1 a + M2 a}"
"setsum M2 {a. f a = x ∧ 0 < M2 a} = setsum M2 {a. f a = x ∧ 0 < M1 a + M2 a}"
by (auto simp: multiset_def intro!: setsum.mono_neutral_cong_left)
then show "(∑a | f a = x ∧ 0 < M1 a + M2 a. M1 a + M2 a) =
setsum M1 {a. f a = x ∧ 0 < M1 a} +
setsum M2 {a. f a = x ∧ 0 < M2 a}"
by (auto simp: setsum.distrib[symmetric])
qed

lemma map_multiset_single[simp]: "mmap f {#a#} = {#f a#}"
by transfer auto

lemma rel_multiset_Plus:
assumes ab: "R a b" and MN: "rel_multiset R M N"
shows "rel_multiset R (M + {#a#}) (N + {#b#})"
proof-
{fix y assume "R a b" and "set_of y ⊆ {(x, y). R x y}"
hence "∃ya. mmap fst y + {#a#} = mmap fst ya ∧
mmap snd y + {#b#} = mmap snd ya ∧
set_of ya ⊆ {(x, y). R x y}"
apply(intro exI[of _ "y + {#(a,b)#}"]) by auto
}
thus ?thesis
using assms
unfolding rel_multiset_def Grp_def by force
qed

lemma rel_multiset'_imp_rel_multiset:
"rel_multiset' R M N ==> rel_multiset R M N"
apply(induct rule: rel_multiset'.induct)
using rel_multiset_Zero rel_multiset_Plus by auto

lemma mcard_mmap[simp]: "mcard (mmap f M) = mcard M"
proof -
def A ≡ "λ b. {a. f a = b ∧ a ∈# M}"
let ?B = "{b. 0 < setsum (count M) (A b)}"
have "{b. ∃a. f a = b ∧ a ∈# M} ⊆ f ` {a. a ∈# M}" by auto
moreover have "finite (f ` {a. a ∈# M})" apply(rule finite_imageI)
using finite_Collect_mem .
ultimately have fin: "finite {b. ∃a. f a = b ∧ a ∈# M}" by(rule finite_subset)
have i: "inj_on A ?B" unfolding inj_on_def A_def apply clarsimp
by (metis (lifting, full_types) mem_Collect_eq neq0_conv setsum.neutral)
have 0: "!! b. 0 < setsum (count M) (A b) <-> (∃ a ∈ A b. count M a > 0)"
apply safe
apply (metis less_not_refl setsum_gt_0_iff setsum.infinite)
by (metis A_def finite_Collect_conjI finite_Collect_mem setsum_gt_0_iff)
hence AB: "A ` ?B = {A b | b. ∃ a ∈ A b. count M a > 0}" by auto

have "setsum (λ x. setsum (count M) (A x)) ?B = setsum (setsum (count M) o A) ?B"
unfolding comp_def ..
also have "... = (∑x∈ A ` ?B. setsum (count M) x)"
unfolding setsum.reindex [OF i, symmetric] ..
also have "... = setsum (count M) (\<Union>x∈A ` {b. 0 < setsum (count M) (A b)}. x)"
(is "_ = setsum (count M) ?J")
apply(rule setsum.UNION_disjoint[symmetric])
using 0 fin unfolding A_def by auto
also have "?J = {a. a ∈# M}" unfolding AB unfolding A_def by auto
finally have "setsum (λ x. setsum (count M) (A x)) ?B =
setsum (count M) {a. a ∈# M}" .
then show ?thesis unfolding mcard_unfold_setsum A_def by transfer
qed

lemma rel_multiset_mcard:
assumes "rel_multiset R M N"
shows "mcard M = mcard N"
using assms unfolding rel_multiset_def Grp_def by auto

assumes empty: "P {#} {#}"
and addL: "!!M N a. P M N ==> P (M + {#a#}) N"
and addR: "!!M N a. P M N ==> P M (N + {#a#})"
shows "P M N"
apply(induct N rule: multiset_induct)
apply(induct M rule: multiset_induct, rule empty, erule addL)
done

lemma multiset_induct2_mcard[consumes 1, case_names empty add]:
assumes c: "mcard M = mcard N"
and empty: "P {#} {#}"
and add: "!!M N a b. P M N ==> P (M + {#a#}) (N + {#b#})"
shows "P M N"
using c proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
case (less M)  show ?case
proof(cases "M = {#}")
case True hence "N = {#}" using less.prems by auto
thus ?thesis using True empty by auto
next
case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
have "N ≠ {#}" using False less.prems by auto
then obtain N1 b where N: "N = N1 + {#b#}" by (metis multi_nonempty_split)
have "mcard M1 = mcard N1" using less.prems unfolding M N by auto
thus ?thesis using M N less.hyps add by auto
qed
qed

lemma msed_map_invL:
assumes "mmap f (M + {#a#}) = N"
shows "∃ N1. N = N1 + {#f a#} ∧ mmap f M = N1"
proof-
have "f a ∈# N"
using assms multiset.set_map[of f "M + {#a#}"] by auto
then obtain N1 where N: "N = N1 + {#f a#}" using multi_member_split by metis
have "mmap f M = N1" using assms unfolding N by simp
thus ?thesis using N by blast
qed

lemma msed_map_invR:
assumes "mmap f M = N + {#b#}"
shows "∃ M1 a. M = M1 + {#a#} ∧ f a = b ∧ mmap f M1 = N"
proof-
obtain a where a: "a ∈# M" and fa: "f a = b"
using multiset.set_map[of f M] unfolding assms
by (metis image_iff mem_set_of_iff union_single_eq_member)
then obtain M1 where M: "M = M1 + {#a#}" using multi_member_split by metis
have "mmap f M1 = N" using assms unfolding M fa[symmetric] by simp
thus ?thesis using M fa by blast
qed

lemma msed_rel_invL:
assumes "rel_multiset R (M + {#a#}) N"
shows "∃ N1 b. N = N1 + {#b#} ∧ R a b ∧ rel_multiset R M N1"
proof-
obtain K where KM: "mmap fst K = M + {#a#}"
and KN: "mmap snd K = N" and sK: "set_of K ⊆ {(a, b). R a b}"
using assms
unfolding rel_multiset_def Grp_def by auto
obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
and K1M: "mmap fst K1 = M" using msed_map_invR[OF KM] by auto
obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "mmap snd K1 = N1"
using msed_map_invL[OF KN[unfolded K]] by auto
have Rab: "R a (snd ab)" using sK a unfolding K by auto
have "rel_multiset R M N1" using sK K1M K1N1
unfolding K rel_multiset_def Grp_def by auto
thus ?thesis using N Rab by auto
qed

lemma msed_rel_invR:
assumes "rel_multiset R M (N + {#b#})"
shows "∃ M1 a. M = M1 + {#a#} ∧ R a b ∧ rel_multiset R M1 N"
proof-
obtain K where KN: "mmap snd K = N + {#b#}"
and KM: "mmap fst K = M" and sK: "set_of K ⊆ {(a, b). R a b}"
using assms
unfolding rel_multiset_def Grp_def by auto
obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
and K1N: "mmap snd K1 = N" using msed_map_invR[OF KN] by auto
obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "mmap fst K1 = M1"
using msed_map_invL[OF KM[unfolded K]] by auto
have Rab: "R (fst ab) b" using sK b unfolding K by auto
have "rel_multiset R M1 N" using sK K1N K1M1
unfolding K rel_multiset_def Grp_def by auto
thus ?thesis using M Rab by auto
qed

lemma rel_multiset_imp_rel_multiset':
assumes "rel_multiset R M N"
shows "rel_multiset' R M N"
using assms proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
case (less M)
have c: "mcard M = mcard N" using rel_multiset_mcard[OF less.prems] .
show ?case
proof(cases "M = {#}")
case True hence "N = {#}" using c by simp
thus ?thesis using True rel_multiset'.Zero by auto
next
case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "rel_multiset R M1 N1"
using msed_rel_invL[OF less.prems[unfolded M]] by auto
have "rel_multiset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp
thus ?thesis using rel_multiset'.Plus[of R a b, OF R] unfolding M N by simp
qed
qed

lemma rel_multiset_rel_multiset':
"rel_multiset R M N = rel_multiset' R M N"
using  rel_multiset_imp_rel_multiset' rel_multiset'_imp_rel_multiset by auto

(* The main end product for rel_multiset: inductive characterization *)
theorems rel_multiset_induct[case_names empty add, induct pred: rel_multiset] =
rel_multiset'.induct[unfolded rel_multiset_rel_multiset'[symmetric]]

subsection {* Size setup *}

lemma multiset_size_o_map: "size_multiset g o mmap f = size_multiset (g o f)"
apply (rule ext)
apply (unfold o_apply)
apply (induct_tac x)
apply auto
done

setup {*
BNF_LFP_Size.register_size_global @{type_name multiset} @{const_name size_multiset}
@{thms size_multiset_empty size_multiset_single size_multiset_union size_empty size_single
size_union}
@{thms multiset_size_o_map}
*}

hide_const (open) wcount

end
```