src/HOL/Library/Multiset.thy
changeset 21404 eb85850d3eb7
parent 21214 a91bab12b2bd
child 21417 13c33ad15303
equal deleted inserted replaced
21403:dd58f13a8eb4 21404:eb85850d3eb7
    19 lemmas multiset_typedef [simp] =
    19 lemmas multiset_typedef [simp] =
    20     Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
    20     Abs_multiset_inverse Rep_multiset_inverse Rep_multiset
    21   and [simp] = Rep_multiset_inject [symmetric]
    21   and [simp] = Rep_multiset_inject [symmetric]
    22 
    22 
    23 definition
    23 definition
    24   Mempty :: "'a multiset"    ("{#}")
    24   Mempty :: "'a multiset"  ("{#}") where
    25   "{#} = Abs_multiset (\<lambda>a. 0)"
    25   "{#} = Abs_multiset (\<lambda>a. 0)"
    26 
    26 
    27   single :: "'a => 'a multiset"    ("{#_#}")
    27 definition
       
    28   single :: "'a => 'a multiset"  ("{#_#}") where
    28   "{#a#} = Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
    29   "{#a#} = Abs_multiset (\<lambda>b. if b = a then 1 else 0)"
    29 
    30 
    30   count :: "'a multiset => 'a => nat"
    31 definition
       
    32   count :: "'a multiset => 'a => nat" where
    31   "count = Rep_multiset"
    33   "count = Rep_multiset"
    32 
    34 
    33   MCollect :: "'a multiset => ('a => bool) => 'a multiset"
    35 definition
       
    36   MCollect :: "'a multiset => ('a => bool) => 'a multiset" where
    34   "MCollect M P = Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)"
    37   "MCollect M P = Abs_multiset (\<lambda>x. if P x then Rep_multiset M x else 0)"
    35 
    38 
    36 abbreviation
    39 abbreviation
    37   Melem :: "'a => 'a multiset => bool"    ("(_/ :# _)" [50, 51] 50)
    40   Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
    38   "a :# M == 0 < count M a"
    41   "a :# M == 0 < count M a"
    39 
    42 
    40 syntax
    43 syntax
    41   "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ : _./ _#})")
    44   "_MCollect" :: "pttrn => 'a multiset => bool => 'a multiset"    ("(1{# _ : _./ _#})")
    42 translations
    45 translations
    43   "{#x:M. P#}" == "CONST MCollect M (\<lambda>x. P)"
    46   "{#x:M. P#}" == "CONST MCollect M (\<lambda>x. P)"
    44 
    47 
    45 definition
    48 definition
    46   set_of :: "'a multiset => 'a set"
    49   set_of :: "'a multiset => 'a set" where
    47   "set_of M = {x. x :# M}"
    50   "set_of M = {x. x :# M}"
    48 
    51 
    49 instance multiset :: (type) "{plus, minus, zero}" ..
    52 instance multiset :: (type) "{plus, minus, zero}" ..
    50 
    53 
    51 defs (overloaded)
    54 defs (overloaded)
    53   diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)"
    56   diff_def: "M - N == Abs_multiset (\<lambda>a. Rep_multiset M a - Rep_multiset N a)"
    54   Zero_multiset_def [simp]: "0 == {#}"
    57   Zero_multiset_def [simp]: "0 == {#}"
    55   size_def: "size M == setsum (count M) (set_of M)"
    58   size_def: "size M == setsum (count M) (set_of M)"
    56 
    59 
    57 definition
    60 definition
    58   multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70)
    61   multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"  (infixl "#\<inter>" 70) where
    59   "multiset_inter A B = A - (A - B)"
    62   "multiset_inter A B = A - (A - B)"
    60 
    63 
    61 
    64 
    62 text {*
    65 text {*
    63  \medskip Preservation of the representing set @{term multiset}.
    66  \medskip Preservation of the representing set @{term multiset}.
   378 subsection {* Multiset orderings *}
   381 subsection {* Multiset orderings *}
   379 
   382 
   380 subsubsection {* Well-foundedness *}
   383 subsubsection {* Well-foundedness *}
   381 
   384 
   382 definition
   385 definition
   383   mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
   386   mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
   384   "mult1 r =
   387   "mult1 r =
   385     {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
   388     {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
   386       (\<forall>b. b :# K --> (b, a) \<in> r)}"
   389       (\<forall>b. b :# K --> (b, a) \<in> r)}"
   387 
   390 
   388   mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set"
   391 definition
       
   392   mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
   389   "mult r = (mult1 r)\<^sup>+"
   393   "mult r = (mult1 r)\<^sup>+"
   390 
   394 
   391 lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
   395 lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
   392   by (simp add: mult1_def)
   396   by (simp add: mult1_def)
   393 
   397 
   794 
   798 
   795 
   799 
   796 subsection {* Pointwise ordering induced by count *}
   800 subsection {* Pointwise ordering induced by count *}
   797 
   801 
   798 definition
   802 definition
   799   mset_le :: "['a multiset, 'a multiset] \<Rightarrow> bool"   ("_ \<le># _"  [50,51] 50)
   803   mset_le :: "['a multiset, 'a multiset] \<Rightarrow> bool"  ("_ \<le># _"  [50,51] 50) where
   800   "(xs \<le># ys) = (\<forall>a. count xs a \<le> count ys a)"
   804   "(xs \<le># ys) = (\<forall>a. count xs a \<le> count ys a)"
   801 
   805 
   802 lemma mset_le_refl[simp]: "xs \<le># xs"
   806 lemma mset_le_refl[simp]: "xs \<le># xs"
   803   unfolding mset_le_def by auto
   807   unfolding mset_le_def by auto
   804 
   808