# Theory Multiset

Up to index of Isabelle/HOL/HOL-Complex/ex

theory Multiset
imports Accessible_Part
begin

    (*  Title:      HOL/Library/Multiset.thy
        ID:         $Id: Multiset.thy,v 1.30 2005/08/31 13:46:37 wenzelm Exp $
        Author:     Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
    *)

    header {* Multisets *}

    theory Multiset
    imports Accessible_Part
    begin

    subsection {* The type of multisets *}

    typedef 'a multiset = "{f::'a => nat. finite {x . 0 < f x}}"
    proof
      show "(λx. 0::nat) ∈ ?multiset" by simp
    qed

    lemmas multiset_typedef [simp] =
        Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
      and [simp] = Rep_multiset_inject [symmetric]

    constdefs
      Mempty :: "'a multiset"    ("{#}")
      "{#} == Abs_multiset (λa. 0)"

      single :: "'a => 'a multiset"    ("{#_#}")
      "{#a#} == Abs_multiset (λb. if b = a then 1 else 0)"

      count :: "'a multiset => 'a => nat"
      "count == Rep_multiset"

      MCollect :: "'a multiset => ('a => bool) => 'a multiset"
      "MCollect M P == Abs_multiset (λx. if P x then Rep_multiset M x else 0)"

    syntax
      "_Melem" :: "'a => 'a multiset => bool"    ("(_/ :# _)" [50, 51] 50)
      "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ : _./ _#})")
    translations
      "a :# M" == "0 < count M a"
      "{#x:M. P#}" == "MCollect M (λx. P)"

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

    instance multiset :: (type) "{plus, minus, zero}" ..

    defs (overloaded)
      union_def: "M + N == Abs_multiset (λa. Rep_multiset M a + Rep_multiset N a)"
      diff_def: "M - N == Abs_multiset (λa. Rep_multiset M a - Rep_multiset N a)"
      Zero_multiset_def [simp]: "0 == {#}"
      size_def: "size M == setsum (count M) (set_of M)"

    constdefs
     multiset_inter :: "'a multiset => 'a multiset => 'a multiset" (infixl "#∩" 70)
     "multiset_inter A B ≡ A - (A - B)"

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

    lemma const0_in_multiset [simp]: "(λa. 0) ∈ multiset"
      by (simp add: multiset_def)

    lemma only1_in_multiset [simp]: "(λb. if b = a then 1 else 0) ∈ multiset"
      by (simp add: multiset_def)

    lemma union_preserves_multiset [simp]:
        "M ∈ multiset ==> N ∈ multiset ==> (λa. M a + N a) ∈ multiset"
      apply (simp add: multiset_def)
      apply (drule (1) finite_UnI)
      apply (simp del: finite_Un add: Un_def)
      done

    lemma diff_preserves_multiset [simp]:
        "M ∈ multiset ==> (λa. M a - N a) ∈ multiset"
      apply (simp add: multiset_def)
      apply (rule finite_subset)
       apply auto
      done

    subsection {* Algebraic properties of multisets *}

    subsubsection {* Union *}

    lemma union_empty [simp]: "M + {#} = M ∧ {#} + M = M"
      by (simp add: union_def Mempty_def)

    lemma union_commute: "M + N = N + (M::'a multiset)"
      by (simp add: union_def add_ac)

    lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
      by (simp add: union_def add_ac)

    lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
    proof -
      have "M + (N + K) = (N + K) + M"
        by (rule union_commute)
      also have "… = N + (K + M)"
        by (rule union_assoc)
      also have "K + M = M + K"
        by (rule union_commute)
      finally show ?thesis .
    qed

    lemmas union_ac = union_assoc union_commute union_lcomm

    instance multiset :: (type) comm_monoid_add
    proof
      fix a b c :: "'a multiset"
      show "(a + b) + c = a + (b + c)" by (rule union_assoc)
      show "a + b = b + a" by (rule union_commute)
      show "0 + a = a" by simp
    qed

    subsubsection {* Difference *}

    lemma diff_empty [simp]: "M - {#} = M ∧ {#} - M = {#}"
      by (simp add: Mempty_def diff_def)

    lemma diff_union_inverse2 [simp]: "M + {#a#} - {#a#} = M"
      by (simp add: union_def diff_def)

    subsubsection {* Count of elements *}

    lemma count_empty [simp]: "count {#} a = 0"
      by (simp add: count_def Mempty_def)

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

    lemma count_union [simp]: "count (M + N) a = count M a + count N a"
      by (simp add: count_def union_def)

    lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
      by (simp add: count_def diff_def)

    subsubsection {* Set of elements *}

    lemma set_of_empty [simp]: "set_of {#} = {}"
      by (simp add: set_of_def)

    lemma set_of_single [simp]: "set_of {#b#} = {b}"
      by (simp add: set_of_def)

    lemma set_of_union [simp]: "set_of (M + N) = set_of M ∪ set_of N"
      by (auto simp add: set_of_def)

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

    lemma mem_set_of_iff [simp]: "(x ∈ set_of M) = (x :# M)"
      by (auto simp add: set_of_def)

    subsubsection {* Size *}

    lemma size_empty [simp]: "size {#} = 0"
      by (simp add: size_def)

    lemma size_single [simp]: "size {#b#} = 1"
      by (simp add: size_def)

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

    lemma setsum_count_Int:
        "finite A ==> setsum (count N) (A ∩ set_of N) = setsum (count N) A"
      apply (erule finite_induct)
       apply simp
      apply (simp add: Int_insert_left set_of_def)
      done

    lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
      apply (unfold size_def)
      apply (subgoal_tac "count (M + N) = (λa. count M a + count N a)")
       prefer 2
       apply (rule ext, simp)
      apply (simp (no_asm_simp) add: setsum_Un_nat setsum_addf setsum_count_Int)
      apply (subst Int_commute)
      apply (simp (no_asm_simp) add: setsum_count_Int)
      done

    lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
      apply (unfold size_def Mempty_def count_def, auto)
      apply (simp add: set_of_def count_def expand_fun_eq)
      done

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

    subsubsection {* Equality of multisets *}

    lemma multiset_eq_conv_count_eq: "(M = N) = (∀a. count M a = count N a)"
      by (simp add: count_def expand_fun_eq)

    lemma single_not_empty [simp]: "{#a#} ≠ {#} ∧ {#} ≠ {#a#}"
      by (simp add: single_def Mempty_def expand_fun_eq)

    lemma single_eq_single [simp]: "({#a#} = {#b#}) = (a = b)"
      by (auto simp add: single_def expand_fun_eq)

    lemma union_eq_empty [iff]: "(M + N = {#}) = (M = {#} ∧ N = {#})"
      by (auto simp add: union_def Mempty_def expand_fun_eq)

    lemma empty_eq_union [iff]: "({#} = M + N) = (M = {#} ∧ N = {#})"
      by (auto simp add: union_def Mempty_def expand_fun_eq)

    lemma union_right_cancel [simp]: "(M + K = N + K) = (M = (N::'a multiset))"
      by (simp add: union_def expand_fun_eq)

    lemma union_left_cancel [simp]: "(K + M = K + N) = (M = (N::'a multiset))"
      by (simp add: union_def expand_fun_eq)

    lemma union_is_single:
        "(M + N = {#a#}) = (M = {#a#} ∧ N={#} ∨ M = {#} ∧ N = {#a#})"
      apply (simp add: Mempty_def single_def union_def add_is_1 expand_fun_eq)
      apply blast
      done

    lemma single_is_union:
         "({#a#} = M + N) = ({#a#} = M ∧ N = {#} ∨ M = {#} ∧ {#a#} = N)"
      apply (unfold Mempty_def single_def union_def)
      apply (simp add: add_is_1 one_is_add expand_fun_eq)
      apply (blast dest: sym)
      done

    lemma add_eq_conv_diff:
      "(M + {#a#} = N + {#b#}) =
       (M = N ∧ a = b ∨ M = N - {#a#} + {#b#} ∧ N = M - {#b#} + {#a#})"
      apply (unfold single_def union_def diff_def)
      apply (simp (no_asm) add: expand_fun_eq)
      apply (rule conjI, force, safe, simp_all)
      apply (simp add: eq_sym_conv)
      done

    declare Rep_multiset_inject [symmetric, simp del]

    subsubsection {* Intersection *}

    lemma multiset_inter_count:
        "count (A #∩ B) x = min (count A x) (count B x)"
      by (simp add: multiset_inter_def min_def)

    lemma multiset_inter_commute: "A #∩ B = B #∩ A"
      by (simp add: multiset_eq_conv_count_eq multiset_inter_count
        min_max.below_inf.inf_commute)

    lemma multiset_inter_assoc: "A #∩ (B #∩ C) = A #∩ B #∩ C"
      by (simp add: multiset_eq_conv_count_eq multiset_inter_count
        min_max.below_inf.inf_assoc)

    lemma multiset_inter_left_commute: "A #∩ (B #∩ C) = B #∩ (A #∩ C)"
      by (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def)

    lemmas multiset_inter_ac =
      multiset_inter_commute
      multiset_inter_assoc
      multiset_inter_left_commute

    lemma multiset_union_diff_commute: "B #∩ C = {#} ==> A + B - C = A - C + B"
      apply (simp add: multiset_eq_conv_count_eq multiset_inter_count min_def
        split: split_if_asm)
      apply clarsimp
      apply (erule_tac x = a in allE)
      apply auto
      done

    subsection {* Induction over multisets *}

    lemma setsum_decr:
      "finite F ==> (0::nat) < f a ==>
        setsum (f (a := f a - 1)) F = (if a∈F then setsum f F - 1 else setsum f F)"
      apply (erule finite_induct, auto)
      apply (drule_tac a = a in mk_disjoint_insert, auto)
      done

    lemma rep_multiset_induct_aux:
      assumes "P (λa. (0::nat))"
        and "!!f b. f ∈ multiset ==> P f ==> P (f (b := f b + 1))"
      shows "∀f. f ∈ multiset --> setsum f {x. 0 < f x} = n --> P f"
    proof -
      note premises = prems [unfolded multiset_def]
      show ?thesis
        apply (unfold multiset_def)
        apply (induct_tac n, simp, clarify)
         apply (subgoal_tac "f = (λa.0)")
          apply simp
          apply (rule premises)
         apply (rule ext, force, clarify)
        apply (frule setsum_SucD, clarify)
        apply (rename_tac a)
        apply (subgoal_tac "finite {x. 0 < (f (a := f a - 1)) x}")
         prefer 2
         apply (rule finite_subset)
          prefer 2
          apply assumption
         apply simp
         apply blast
        apply (subgoal_tac "f = (f (a := f a - 1))(a := (f (a := f a - 1)) a + 1)")
         prefer 2
         apply (rule ext)
         apply (simp (no_asm_simp))
         apply (erule ssubst, rule premises, blast)
        apply (erule allE, erule impE, erule_tac [2] mp, blast)
        apply (simp (no_asm_simp) add: setsum_decr del: fun_upd_apply One_nat_def)
        apply (subgoal_tac "{x. x ≠ a --> 0 < f x} = {x. 0 < f x}")
         prefer 2
         apply blast
        apply (subgoal_tac "{x. x ≠ a ∧ 0 < f x} = {x. 0 < f x} - {a}")
         prefer 2
         apply blast
        apply (simp add: le_imp_diff_is_add setsum_diff1_nat cong: conj_cong)
        done
    qed

    theorem rep_multiset_induct:
      "f ∈ multiset ==> P (λa. 0) ==>
        (!!f b. f ∈ multiset ==> P f ==> P (f (b := f b + 1))) ==> P f"
      using rep_multiset_induct_aux by blast

    theorem multiset_induct [induct type: multiset]:
      assumes prem1: "P {#}"
        and prem2: "!!M x. P M ==> P (M + {#x#})"
      shows "P M"
    proof -
      note defns = union_def single_def Mempty_def
      show ?thesis
        apply (rule Rep_multiset_inverse [THEN subst])
        apply (rule Rep_multiset [THEN rep_multiset_induct])
         apply (rule prem1 [unfolded defns])
        apply (subgoal_tac "f(b := f b + 1) = (λa. f a + (if a=b then 1 else 0))")
         prefer 2
         apply (simp add: expand_fun_eq)
        apply (erule ssubst)
        apply (erule Abs_multiset_inverse [THEN subst])
        apply (erule prem2 [unfolded defns, simplified])
        done
    qed

    lemma MCollect_preserves_multiset:
        "M ∈ multiset ==> (λx. if P x then M x else 0) ∈ multiset"
      apply (simp add: multiset_def)
      apply (rule finite_subset, auto)
      done

    lemma count_MCollect [simp]:
        "count {# x:M. P x #} a = (if P a then count M a else 0)"
      by (simp add: count_def MCollect_def MCollect_preserves_multiset)

    lemma set_of_MCollect [simp]: "set_of {# x:M. P x #} = set_of M ∩ {x. P x}"
      by (auto simp add: set_of_def)

    lemma multiset_partition: "M = {# x:M. P x #} + {# x:M. ¬ P x #}"
      by (subst multiset_eq_conv_count_eq, auto)

    lemma add_eq_conv_ex:
      "(M + {#a#} = N + {#b#}) =
        (M = N ∧ a = b ∨ (∃K. M = K + {#b#} ∧ N = K + {#a#}))"
      by (auto simp add: add_eq_conv_diff)

    declare multiset_typedef [simp del]

    subsection {* Multiset orderings *}

    subsubsection {* Well-foundedness *}

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

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

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

    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)"
      (concl 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}"
      hence "∃a' M0' K.
          M0 + {#a#} = M0' + {#a'#} ∧ N = M0' + K ∧ ?r K a'" by simp
      thus "?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'#}"
        hence "M0 = M0' ∧ a = a' ∨
            (∃K'. M0 = K' + {#a'#} ∧ M0' = K' + {#a#})"
          by (simp only: add_eq_conv_ex)
        thus ?thesis
        proof (elim disjE conjE exE)
          assume "M0 = M0'" "a = a'"
          with N r have "?r K a ∧ N = M0 + K" by simp
          hence ?case2 .. thus ?thesis ..
        next
          fix K'
          assume "M0' = K' + {#a#}"
          with N have n: "N = K' + K + {#a#}" by (simp add: union_ac)

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

    lemma all_accessible: "wf r ==> ∀M. M ∈ acc (mult1 r)"
    proof
      let ?R = "mult1 r"
      let ?W = "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"
          hence "((∃M. (M, M0) ∈ ?R ∧ N = M + {#a#}) ∨
              (∃K. (∀b. b :# K --> (b, a) ∈ r) ∧ N = M0 + K))"
            by (rule less_add)
          thus "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" ..
            hence "M + {#a#} ∈ ?W" ..
            thus "N ∈ ?W" by (simp only: N)
          next
            fix K
            assume N: "N = M0 + K"
            assume "∀b. b :# K --> (b, a) ∈ r"
            have "?this --> M0 + K ∈ ?W" (is "?P K")
            proof (induct K)
              from M0 have "M0 + {#} ∈ ?W" by simp
              thus "?P {#}" ..

              fix K x assume hyp: "?P K"
              show "?P (K + {#x#})"
              proof
                assume a: "∀b. b :# (K + {#x#}) --> (b, a) ∈ r"
                hence "(x, a) ∈ r" by simp
                with wf_hyp have b: "∀M ∈ ?W. M + {#x#} ∈ ?W" by blast

                from a hyp have "M0 + K ∈ ?W" by simp
                with b have "(M0 + K) + {#x#} ∈ ?W" ..
                thus "M0 + (K + {#x#}) ∈ ?W" by (simp only: union_assoc)
              qed
            qed
            hence "M0 + K ∈ ?W" ..
            thus "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 "!!b. (b, a) ∈ r ==> (∀M ∈ ?W. M + {#b#} ∈ ?W)"
          show "∀M ∈ ?W. M + {#a#} ∈ ?W"
          proof
            fix M assume "M ∈ ?W"
            thus "M + {#a#} ∈ ?W"
              by (rule acc_induct) (rule tedious_reasoning)
          qed
        qed
        thus "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)"
      by (unfold mult_def, rule wf_trancl, rule wf_mult1)

    subsubsection {* Closure-free presentation *}

    (*Badly needed: a linear arithmetic procedure for multisets*)

    lemma diff_union_single_conv: "a :# J ==> I + J - {#a#} = I + (J - {#a#})"
    by (simp add: multiset_eq_conv_count_eq)

    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 (simp (no_asm_simp) add: union_assoc [symmetric])
       apply (drule_tac f = "λM. M - {#a#}" in arg_cong)
       apply (simp add: diff_union_single_conv)
       apply (simp (no_asm_use) add: trans_def)
       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_conv_count_eq split: nat_diff_split)
       apply (rule conjI)
        apply (drule_tac f = "λM. M - {#a#}" in arg_cong, simp)
        apply (simp add: multiset_eq_conv_count_eq split: nat_diff_split)
       apply (simp (no_asm_use) add: trans_def)
       apply blast
      apply (subgoal_tac "a :# (M0 + {#a#})")
       apply simp
      apply (simp (no_asm))
      done

    lemma elem_imp_eq_diff_union: "a :# M ==> M = M - {#a#} + {#a#}"
    by (simp add: multiset_eq_conv_count_eq)

    lemma size_eq_Suc_imp_eq_union: "size M = Suc n ==> ∃a N. M = N + {#a#}"
      apply (erule size_eq_Suc_imp_elem [THEN exE])
      apply (drule elem_imp_eq_diff_union, auto)
      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 (simp add: mult_def)
       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 (simp add: Ball_def, auto)
      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 (simp (no_asm_use) add: union_assoc [symmetric] mult_def)
      apply (erule trancl_trans)
      apply (rule r_into_trancl)
      apply (simp add: mult1_def set_of_def)
      apply (rule_tac x = a in exI)
      apply (rule_tac x = "I + J'" in exI)
      apply (simp add: union_ac)
      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"
      apply (insert one_step_implies_mult_aux, blast)
      done

    subsubsection {* Partial-order properties *}

    instance multiset :: (type) ord ..

    defs (overloaded)
      less_multiset_def: "M' < M == (M', M) ∈ mult {(x', x). x' < x}"
      le_multiset_def: "M' <= M == M' = M ∨ M' < (M::'a multiset)"

    lemma trans_base_order: "trans {(x', x). x' < (x::'a::order)}"
      apply (unfold trans_def)
      apply (blast intro: order_less_trans)
      done

    text {*
     \medskip Irreflexivity.
    *}

    lemma mult_irrefl_aux:
        "finite A ==> (∀x ∈ A. ∃y ∈ A. x < (y::'a::order)) --> A = {}"
      apply (erule finite_induct)
       apply (auto intro: order_less_trans)
      done

    lemma mult_less_not_refl: "¬ M < (M::'a::order multiset)"
      apply (unfold less_multiset_def, auto)
      apply (drule trans_base_order [THEN mult_implies_one_step], auto)
      apply (drule finite_set_of [THEN mult_irrefl_aux [rule_format (no_asm)]])
      apply (simp add: set_of_eq_empty_iff)
      done

    lemma mult_less_irrefl [elim!]: "M < (M::'a::order multiset) ==> R"
    by (insert mult_less_not_refl, fast)

    text {* Transitivity. *}

    theorem mult_less_trans: "K < M ==> M < N ==> K < (N::'a::order multiset)"
      apply (unfold less_multiset_def mult_def)
      apply (blast intro: trancl_trans)
      done

    text {* Asymmetry. *}

    theorem mult_less_not_sym: "M < N ==> ¬ N < (M::'a::order multiset)"
      apply auto
      apply (rule mult_less_not_refl [THEN notE])
      apply (erule mult_less_trans, assumption)
      done

    theorem mult_less_asym:
        "M < N ==> (¬ P ==> N < (M::'a::order multiset)) ==> P"
      by (insert mult_less_not_sym, blast)

    theorem mult_le_refl [iff]: "M <= (M::'a::order multiset)"
    by (unfold le_multiset_def, auto)

    text {* Anti-symmetry. *}

    theorem mult_le_antisym:
        "M <= N ==> N <= M ==> M = (N::'a::order multiset)"
      apply (unfold le_multiset_def)
      apply (blast dest: mult_less_not_sym)
      done

    text {* Transitivity. *}

    theorem mult_le_trans:
        "K <= M ==> M <= N ==> K <= (N::'a::order multiset)"
      apply (unfold le_multiset_def)
      apply (blast intro: mult_less_trans)
      done

    theorem mult_less_le: "(M < N) = (M <= N ∧ M ≠ (N::'a::order multiset))"
    by (unfold le_multiset_def, auto)

    text {* Partial order. *}

    instance multiset :: (order) order
      apply intro_classes
         apply (rule mult_le_refl)
        apply (erule mult_le_trans, assumption)
       apply (erule mult_le_antisym, assumption)
      apply (rule mult_less_le)
      done

    subsubsection {* Monotonicity of multiset union *}

    lemma mult1_union:
        "(B, D) ∈ mult1 r ==> trans r ==> (C + B, C + D) ∈ mult1 r"
      apply (unfold mult1_def, auto)
      apply (rule_tac x = a in exI)
      apply (rule_tac x = "C + M0" in exI)
      apply (simp add: union_assoc)
      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 transI order_less_trans r_into_trancl)
      apply (blast intro: mult1_union transI order_less_trans r_into_trancl trancl_trans)
      done

    lemma union_less_mono1: "B < D ==> B + C < D + (C::'a::order multiset)"
      apply (subst union_commute [of B C])
      apply (subst union_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)"
      apply (blast intro!: union_less_mono1 union_less_mono2 mult_less_trans)
      done

    lemma union_le_mono:
        "A <= C ==> B <= D ==> A + B <= C + (D::'a::order multiset)"
      apply (unfold le_multiset_def)
      apply (blast intro: union_less_mono union_less_mono1 union_less_mono2)
      done

    lemma empty_leI [iff]: "{#} <= (M::'a::order multiset)"
      apply (unfold le_multiset_def less_multiset_def)
      apply (case_tac "M = {#}")
       prefer 2
       apply (subgoal_tac "({#} + {#}, {#} + M) ∈ mult (Collect (split op <))")
        prefer 2
        apply (rule one_step_implies_mult)
          apply (simp only: trans_def, auto)
      done

    lemma union_upper1: "A <= A + (B::'a::order multiset)"
    proof -
      have "A + {#} <= A + B" by (blast intro: union_le_mono)
      thus ?thesis by simp
    qed

    lemma union_upper2: "B <= A + (B::'a::order multiset)"
    by (subst union_commute, rule union_upper1)

    subsection {* Link with lists *}

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

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

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

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

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

    lemma multiset_of_append[simp]:
      "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
      by (rule_tac x=ys in spec, induct_tac xs, auto simp: union_ac)

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

    lemma set_count_greater_0: "set x = {a. 0 < count (multiset_of x) a}"
      by (induct_tac 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_tac x, simp, rule iffI, simp_all)
       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_conv_count_eq 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_conv_count_eq 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 (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
      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∈xs. P x] + multiset_of [x∈xs. ¬P x] = multiset_of xs"
      by (induct xs) (auto simp: union_ac)

    lemma count_filter:
      "count (multiset_of xs) x = length [y ∈ xs. y = x]"
      by (induct xs, auto)

    subsection {* Pointwise ordering induced by count *}

    consts
      mset_le :: "['a multiset, 'a multiset] => bool"

    syntax
      "_mset_le" :: "'a multiset => 'a multiset => bool"   ("_ ≤# _"  [50,51] 50)
    translations
      "x ≤# y" == "mset_le x y"

    defs
      mset_le_def: "xs ≤# ys == (∀a. count xs a ≤ count ys a)"

    lemma mset_le_refl[simp]: "xs ≤# xs"
      by (unfold mset_le_def) auto

    lemma mset_le_trans: "[| xs ≤# ys; ys ≤# zs |] ==> xs ≤# zs"
      by (unfold mset_le_def) (fast intro: order_trans)

    lemma mset_le_antisym: "[| xs≤# ys; ys ≤# xs|] ==> xs = ys"
      apply (unfold mset_le_def)
      apply (rule multiset_eq_conv_count_eq[THEN iffD2])
      apply (blast intro: order_antisym)
      done

    lemma mset_le_exists_conv:
      "(xs ≤# ys) = (∃zs. ys = xs + zs)"
      apply (unfold mset_le_def, rule iffI, rule_tac x = "ys - xs" in exI)
      apply (auto intro: multiset_eq_conv_count_eq [THEN iffD2])
      done

    lemma mset_le_mono_add_right_cancel[simp]: "(xs + zs ≤# ys + zs) = (xs ≤# ys)"
      by (unfold mset_le_def) auto

    lemma mset_le_mono_add_left_cancel[simp]: "(zs + xs ≤# zs + ys) = (xs ≤# ys)"
      by (unfold mset_le_def) auto

    lemma mset_le_mono_add: "[| xs ≤# ys; vs ≤# ws |] ==> xs + vs ≤# ys + ws"
      apply (unfold mset_le_def)
      apply auto
      apply (erule_tac x=a in allE)+
      apply auto
      done

    lemma mset_le_add_left[simp]: "xs ≤# xs + ys"
      by (unfold mset_le_def) auto

    lemma mset_le_add_right[simp]: "ys ≤# xs + ys"
      by (unfold mset_le_def) auto

    lemma multiset_of_remdups_le: "multiset_of (remdups x) ≤# multiset_of x"
      apply (induct x)
       apply auto
      apply (rule mset_le_trans)
       apply auto
      done

    end

### The type of multisets

lemmas multiset_typedef:

      y ∈ multiset ==> Rep_multiset (Abs_multiset y) = y

      Abs_multiset (Rep_multiset x) = x

      Rep_multiset x ∈ multiset

and

      (x1 = y1) = (Rep_multiset x1 = Rep_multiset y1)

lemmas multiset_typedef:

      y ∈ multiset ==> Rep_multiset (Abs_multiset y) = y

      Abs_multiset (Rep_multiset x) = x

      Rep_multiset x ∈ multiset

and

      (x1 = y1) = (Rep_multiset x1 = Rep_multiset y1)

lemma const0_in_multiset:

      (%a. 0) ∈ multiset

lemma only1_in_multiset:

      (%b. if b = a then 1 else 0) ∈ multiset

lemma union_preserves_multiset:

      [| M ∈ multiset; N ∈ multiset |] ==> (%a. M a + N a) ∈ multiset

lemma diff_preserves_multiset:

      M ∈ multiset ==> (%a. M a - N a) ∈ multiset

### Algebraic properties of multisets

#### Union

lemma union_empty:

      M + {#} = M ∧ {#} + M = M

lemma union_commute:

      M + N = N + M

lemma union_assoc:

      M + N + K = M + (N + K)

lemma union_lcomm:

      M + (N + K) = N + (M + K)

lemmas union_ac:

      M + N + K = M + (N + K)

      M + N = N + M

      M + (N + K) = N + (M + K)

lemmas union_ac:

      M + N + K = M + (N + K)

      M + N = N + M

      M + (N + K) = N + (M + K)

#### Difference

lemma diff_empty:

      M - {#} = M ∧ {#} - M = {#}

lemma diff_union_inverse2:

      M + {#a#} - {#a#} = M

#### Count of elements

lemma count_empty:

      count {#} a = 0

lemma count_single:

      count {#b#} a = (if b = a then 1 else 0)

lemma count_union:

      count (M + N) a = count M a + count N a

lemma count_diff:

      count (M - N) a = count M a - count N a

#### Set of elements

lemma set_of_empty:

      set_of {#} = {}

lemma set_of_single:

      set_of {#b#} = {b}

lemma set_of_union:

      set_of (M + N) = set_of M ∪ set_of N

lemma set_of_eq_empty_iff:

      (set_of M = {}) = (M = {#})

lemma mem_set_of_iff:

      (x ∈ set_of M) = (x :# M)

#### Size

lemma size_empty:

      size {#} = 0

lemma size_single:

      size {#b#} = 1

lemma finite_set_of:

      finite (set_of M)

lemma setsum_count_Int:

      finite A ==> setsum (count N) (A ∩ set_of N) = setsum (count N) A

lemma size_union:

      size (M + N) = size M + size N

lemma size_eq_0_iff_empty:

      (size M = 0) = (M = {#})

lemma size_eq_Suc_imp_elem:

      size M = Suc n ==> ∃a. a :# M

#### Equality of multisets

lemma multiset_eq_conv_count_eq:

      (M = N) = (∀a. count M a = count N a)

lemma single_not_empty:

      {#a#} ≠ {#} ∧ {#} ≠ {#a#}

lemma single_eq_single:

      ({#a#} = {#b#}) = (a = b)

lemma union_eq_empty:

      (M + N = {#}) = (M = {#} ∧ N = {#})

lemma empty_eq_union:

      ({#} = M + N) = (M = {#} ∧ N = {#})

lemma union_right_cancel:

      (M + K = N + K) = (M = N)

lemma union_left_cancel:

      (K + M = K + N) = (M = N)

lemma union_is_single:

      (M + N = {#a#}) = (M = {#a#} ∧ N = {#} ∨ M = {#} ∧ N = {#a#})

lemma single_is_union:

      ({#a#} = M + N) = ({#a#} = M ∧ N = {#} ∨ M = {#} ∧ {#a#} = N)

lemma add_eq_conv_diff:

      (M + {#a#} = N + {#b#}) =
      (M = N ∧ a = b ∨ M = N - {#a#} + {#b#} ∧ N = M - {#b#} + {#a#})

#### Intersection

lemma multiset_inter_count:

      count (A #∩ B) x = min (count A x) (count B x)

lemma multiset_inter_commute:

      A #∩ B = B #∩ A

lemma multiset_inter_assoc:

      A #∩ (B #∩ C) = A #∩ B #∩ C

lemma multiset_inter_left_commute:

      A #∩ (B #∩ C) = B #∩ (A #∩ C)

lemmas multiset_inter_ac:

      A #∩ B = B #∩ A

      A #∩ (B #∩ C) = A #∩ B #∩ C

      A #∩ (B #∩ C) = B #∩ (A #∩ C)

lemmas multiset_inter_ac:

      A #∩ B = B #∩ A

      A #∩ (B #∩ C) = A #∩ B #∩ C

      A #∩ (B #∩ C) = B #∩ (A #∩ C)

lemma multiset_union_diff_commute:

      B #∩ C = {#} ==> A + B - C = A - C + B

### Induction over multisets

lemma setsum_decr:

      [| finite F; 0 < f a |]
      ==> setsum (f(a := f a - 1)) F = (if a ∈ F then setsum f F - 1 else setsum f F)

lemma rep_multiset_induct_aux:

      [| P (%a. 0); !!f b. [| f ∈ multiset; P f |] ==> P (f(b := f b + 1)) |]
      ==> ∀f. f ∈ multiset --> setsum f {x. 0 < f x} = n --> P f

theorem rep_multiset_induct:

      [| f ∈ multiset; P (%a. 0);
         !!f b. [| f ∈ multiset; P f |] ==> P (f(b := f b + 1)) |]
      ==> P f

theorem multiset_induct:

      [| P {#}; !!M x. P M ==> P (M + {#x#}) |] ==> P M

lemma MCollect_preserves_multiset:

      M ∈ multiset ==> (%x. if P x then M x else 0) ∈ multiset

lemma count_MCollect:

      count (MCollect M P) a = (if P a then count M a else 0)

lemma set_of_MCollect:

      set_of (MCollect M P) = set_of M ∩ {x. P x}

lemma multiset_partition:

      M = MCollect M P + {# x : M. ¬ P x#}

lemma add_eq_conv_ex:

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

### Multiset orderings

#### Well-foundedness

lemma not_less_empty:

      (M, {#}) ∉ mult1 r

lemma less_add:

      (N, M0.0 + {#a#}) ∈ mult1 r
      ==> (∃M. (M, M0.0) ∈ mult1 r ∧ N = M + {#a#}) ∨
          (∃K. (∀b. b :# K --> (b, a) ∈ r) ∧ N = M0.0 + K)

lemma all_accessible:

      wf r ==> ∀M. M ∈ acc (mult1 r)

theorem wf_mult1:

      wf r ==> wf (mult1 r)

theorem wf_mult:

      wf r ==> wf (mult r)

#### Closure-free presentation

lemma diff_union_single_conv:

      a :# J ==> I + J - {#a#} = I + (J - {#a#})

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)

lemma elem_imp_eq_diff_union:

      a :# M ==> M = M - {#a#} + {#a#}

lemma size_eq_Suc_imp_eq_union:

      size M = Suc n ==> ∃a N. M = N + {#a#}

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

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

#### Partial-order properties

lemma trans_base_order:

      trans {(x', x). x' < x}

lemma mult_irrefl_aux:

      finite A ==> (∀x∈A. ∃y∈A. x < y) --> A = {}

lemma mult_less_not_refl:

      ¬ M < M

lemma mult_less_irrefl:

      M < M ==> R

theorem mult_less_trans:

      [| K < M; M < N |] ==> K < N

theorem mult_less_not_sym:

      M < N ==> ¬ N < M

theorem mult_less_asym:

      [| M < N; ¬ P ==> N < M |] ==> P

theorem mult_le_refl:

      M ≤ M

theorem mult_le_antisym:

      [| M ≤ N; N ≤ M |] ==> M = N

theorem mult_le_trans:

      [| K ≤ M; M ≤ N |] ==> K ≤ N

theorem mult_less_le:

      (M < N) = (M ≤ N ∧ M ≠ N)

#### Monotonicity of multiset union

lemma mult1_union:

      [| (B, D) ∈ mult1 r; trans r |] ==> (C + B, C + D) ∈ mult1 r

lemma union_less_mono2:

      B < D ==> C + B < C + D

lemma union_less_mono1:

      B < D ==> B + C < D + C

lemma union_less_mono:

      [| A < C; B < D |] ==> A + B < C + D

lemma union_le_mono:

      [| A ≤ C; B ≤ D |] ==> A + B ≤ C + D

lemma empty_leI:

      {#} ≤ M

lemma union_upper1:

      A ≤ A + B

lemma union_upper2:

      B ≤ A + B

### Link with lists

lemma multiset_of_zero_iff:

      (multiset_of x = {#}) = (x = [])

lemma multiset_of_zero_iff_right:

      ({#} = multiset_of x) = (x = [])

lemma set_of_multiset_of:

      set_of (multiset_of x) = set x

lemma mem_set_multiset_eq:

      (x ∈ set xs) = (x :# multiset_of xs)

lemma multiset_of_append:

      multiset_of (xs @ ys) = multiset_of xs + multiset_of ys

lemma surj_multiset_of:

      surj multiset_of

lemma set_count_greater_0:

      set x = {a. a :# multiset_of x}

lemma distinct_count_atmost_1:

      distinct x = (∀a. count (multiset_of x) a = (if a ∈ set x then 1 else 0))

lemma multiset_of_eq_setD:

      multiset_of xs = multiset_of ys ==> set xs = set ys

lemma set_eq_iff_multiset_of_eq_distinct:

      [| distinct x; distinct y |]
      ==> (set x = set y) = (multiset_of x = multiset_of y)

lemma set_eq_iff_multiset_of_remdups_eq:

      (set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))

lemma multiset_of_compl_union:

      multiset_of (filter P xs) + multiset_of [x∈xs . ¬ P x] = multiset_of xs

lemma count_filter:

      count (multiset_of xs) x = length [y∈xs . y = x]

### Pointwise ordering induced by count

lemma mset_le_refl:

      xs ≤# xs

lemma mset_le_trans:

      [| xs ≤# ys; ys ≤# zs |] ==> xs ≤# zs

lemma mset_le_antisym:

      [| xs ≤# ys; ys ≤# xs |] ==> xs = ys

lemma mset_le_exists_conv:

      (xs ≤# ys) = (∃zs. ys = xs + zs)

lemma mset_le_mono_add_right_cancel:

      (xs + zs ≤# ys + zs) = (xs ≤# ys)

lemma mset_le_mono_add_left_cancel:

      (zs + xs ≤# zs + ys) = (xs ≤# ys)

lemma mset_le_mono_add:

      [| xs ≤# ys; vs ≤# ws |] ==> xs + vs ≤# ys + ws

lemma mset_le_add_left:

      xs ≤# xs + ys

lemma mset_le_add_right:

      ys ≤# xs + ys

lemma multiset_of_remdups_le:

      multiset_of (remdups x) ≤# multiset_of x