src/HOL/Library/Multiset.thy
changeset 80914 d97fdabd9e2b
parent 80786 70076ba563d2
child 81019 dd59daa3c37a
equal deleted inserted replaced
80913:46f59511b7bb 80914:d97fdabd9e2b
    91   "\<lambda>a M b. if b = a then Suc (M b) else M b"
    91   "\<lambda>a M b. if b = a then Suc (M b) else M b"
    92 by (rule add_mset_in_multiset)
    92 by (rule add_mset_in_multiset)
    93 
    93 
    94 nonterminal multiset_args
    94 nonterminal multiset_args
    95 syntax
    95 syntax
    96   "" :: "'a \<Rightarrow> multiset_args"  ("_")
    96   "" :: "'a \<Rightarrow> multiset_args"  (\<open>_\<close>)
    97   "_multiset_args" :: "'a \<Rightarrow> multiset_args \<Rightarrow> multiset_args"  ("_,/ _")
    97   "_multiset_args" :: "'a \<Rightarrow> multiset_args \<Rightarrow> multiset_args"  (\<open>_,/ _\<close>)
    98   "_multiset" :: "multiset_args \<Rightarrow> 'a multiset"    ("{#(_)#}")
    98   "_multiset" :: "multiset_args \<Rightarrow> 'a multiset"    (\<open>{#(_)#}\<close>)
    99 syntax_consts
    99 syntax_consts
   100   "_multiset_args" "_multiset" == add_mset
   100   "_multiset_args" "_multiset" == add_mset
   101 translations
   101 translations
   102   "{#x, xs#}" == "CONST add_mset x {#xs#}"
   102   "{#x, xs#}" == "CONST add_mset x {#xs#}"
   103   "{#x#}" == "CONST add_mset x {#}"
   103   "{#x#}" == "CONST add_mset x {#}"
   165   where "Bex M \<equiv> Set.Bex (set_mset M)"
   165   where "Bex M \<equiv> Set.Bex (set_mset M)"
   166 
   166 
   167 end
   167 end
   168 
   168 
   169 syntax
   169 syntax
   170   "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_\<in>#_./ _)" [0, 0, 10] 10)
   170   "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      (\<open>(3\<forall>_\<in>#_./ _)\<close> [0, 0, 10] 10)
   171   "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_\<in>#_./ _)" [0, 0, 10] 10)
   171   "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      (\<open>(3\<exists>_\<in>#_./ _)\<close> [0, 0, 10] 10)
   172 
   172 
   173 syntax  (ASCII)
   173 syntax  (ASCII)
   174   "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_:#_./ _)" [0, 0, 10] 10)
   174   "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      (\<open>(3\<forall>_:#_./ _)\<close> [0, 0, 10] 10)
   175   "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_:#_./ _)" [0, 0, 10] 10)
   175   "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      (\<open>(3\<exists>_:#_./ _)\<close> [0, 0, 10] 10)
   176 
   176 
   177 syntax_consts
   177 syntax_consts
   178   "_MBall" \<rightleftharpoons> Multiset.Ball and
   178   "_MBall" \<rightleftharpoons> Multiset.Ball and
   179   "_MBex" \<rightleftharpoons> Multiset.Bex
   179   "_MBex" \<rightleftharpoons> Multiset.Bex
   180 
   180 
   524   by auto
   524   by auto
   525 
   525 
   526 
   526 
   527 subsubsection \<open>Pointwise ordering induced by count\<close>
   527 subsubsection \<open>Pointwise ordering induced by count\<close>
   528 
   528 
   529 definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<subseteq>#" 50)
   529 definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix \<open>\<subseteq>#\<close> 50)
   530   where "A \<subseteq># B \<longleftrightarrow> (\<forall>a. count A a \<le> count B a)"
   530   where "A \<subseteq># B \<longleftrightarrow> (\<forall>a. count A a \<le> count B a)"
   531 
   531 
   532 definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subset>#" 50)
   532 definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix \<open>\<subset>#\<close> 50)
   533   where "A \<subset># B \<longleftrightarrow> A \<subseteq># B \<and> A \<noteq> B"
   533   where "A \<subset># B \<longleftrightarrow> A \<subseteq># B \<and> A \<noteq> B"
   534 
   534 
   535 abbreviation (input) supseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supseteq>#" 50)
   535 abbreviation (input) supseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix \<open>\<supseteq>#\<close> 50)
   536   where "supseteq_mset A B \<equiv> B \<subseteq># A"
   536   where "supseteq_mset A B \<equiv> B \<subseteq># A"
   537 
   537 
   538 abbreviation (input) supset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supset>#" 50)
   538 abbreviation (input) supset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix \<open>\<supset>#\<close> 50)
   539   where "supset_mset A B \<equiv> B \<subset># A"
   539   where "supset_mset A B \<equiv> B \<subset># A"
   540 
   540 
   541 notation (input)
   541 notation (input)
   542   subseteq_mset  (infix "\<le>#" 50) and
   542   subseteq_mset  (infix \<open>\<le>#\<close> 50) and
   543   supseteq_mset  (infix "\<ge>#" 50)
   543   supseteq_mset  (infix \<open>\<ge>#\<close> 50)
   544 
   544 
   545 notation (ASCII)
   545 notation (ASCII)
   546   subseteq_mset  (infix "<=#" 50) and
   546   subseteq_mset  (infix \<open><=#\<close> 50) and
   547   subset_mset  (infix "<#" 50) and
   547   subset_mset  (infix \<open><#\<close> 50) and
   548   supseteq_mset  (infix ">=#" 50) and
   548   supseteq_mset  (infix \<open>>=#\<close> 50) and
   549   supset_mset  (infix ">#" 50)
   549   supset_mset  (infix \<open>>#\<close> 50)
   550 
   550 
   551 global_interpretation subset_mset: ordering \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close>
   551 global_interpretation subset_mset: ordering \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close>
   552   by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order.trans order.antisym)
   552   by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order.trans order.antisym)
   553 
   553 
   554 interpretation subset_mset: ordered_ab_semigroup_add_imp_le \<open>(+)\<close> \<open>(-)\<close> \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close>
   554 interpretation subset_mset: ordered_ab_semigroup_add_imp_le \<open>(+)\<close> \<open>(-)\<close> \<open>(\<subseteq>#)\<close> \<open>(\<subset>#)\<close>
  1252 lift_definition filter_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"
  1252 lift_definition filter_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"
  1253 is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
  1253 is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
  1254 by (rule filter_preserves_multiset)
  1254 by (rule filter_preserves_multiset)
  1255 
  1255 
  1256 syntax (ASCII)
  1256 syntax (ASCII)
  1257   "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{#_ :# _./ _#})")
  1257   "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    (\<open>(1{#_ :# _./ _#})\<close>)
  1258 syntax
  1258 syntax
  1259   "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{#_ \<in># _./ _#})")
  1259   "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    (\<open>(1{#_ \<in># _./ _#})\<close>)
  1260 syntax_consts
  1260 syntax_consts
  1261   "_MCollect" == filter_mset
  1261   "_MCollect" == filter_mset
  1262 translations
  1262 translations
  1263   "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
  1263   "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
  1264 
  1264 
  1827 lemma image_mset_subset_mono: "M \<subset># N \<Longrightarrow> image_mset f M \<subset># image_mset f N"
  1827 lemma image_mset_subset_mono: "M \<subset># N \<Longrightarrow> image_mset f M \<subset># image_mset f N"
  1828   by (metis (no_types) Diff_eq_empty_iff_mset image_mset_Diff image_mset_is_empty_iff
  1828   by (metis (no_types) Diff_eq_empty_iff_mset image_mset_Diff image_mset_is_empty_iff
  1829     image_mset_subseteq_mono subset_mset.less_le_not_le)
  1829     image_mset_subseteq_mono subset_mset.less_le_not_le)
  1830 
  1830 
  1831 syntax (ASCII)
  1831 syntax (ASCII)
  1832   "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ :# _#})")
  1832   "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  (\<open>({#_/. _ :# _#})\<close>)
  1833 syntax
  1833 syntax
  1834   "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ \<in># _#})")
  1834   "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  (\<open>({#_/. _ \<in># _#})\<close>)
  1835 syntax_consts
  1835 syntax_consts
  1836   "_comprehension_mset" \<rightleftharpoons> image_mset
  1836   "_comprehension_mset" \<rightleftharpoons> image_mset
  1837 translations
  1837 translations
  1838   "{#e. x \<in># M#}" \<rightleftharpoons> "CONST image_mset (\<lambda>x. e) M"
  1838   "{#e. x \<in># M#}" \<rightleftharpoons> "CONST image_mset (\<lambda>x. e) M"
  1839 
  1839 
  1840 syntax (ASCII)
  1840 syntax (ASCII)
  1841   "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ :# _./ _#})")
  1841   "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  (\<open>({#_/ | _ :# _./ _#})\<close>)
  1842 syntax
  1842 syntax
  1843   "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ \<in># _./ _#})")
  1843   "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  (\<open>({#_/ | _ \<in># _./ _#})\<close>)
  1844 syntax_consts
  1844 syntax_consts
  1845   "_comprehension_mset'" \<rightleftharpoons> image_mset
  1845   "_comprehension_mset'" \<rightleftharpoons> image_mset
  1846 translations
  1846 translations
  1847   "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
  1847   "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
  1848 
  1848 
  2682   "sum f A = sum_mset (image_mset f (mset_set A))"
  2682   "sum f A = sum_mset (image_mset f (mset_set A))"
  2683   by (cases "finite A") (induct A rule: finite_induct, simp_all)
  2683   by (cases "finite A") (induct A rule: finite_induct, simp_all)
  2684 
  2684 
  2685 end
  2685 end
  2686 
  2686 
  2687 notation sum_mset ("\<Sum>\<^sub>#")
  2687 notation sum_mset (\<open>\<Sum>\<^sub>#\<close>)
  2688 
  2688 
  2689 syntax (ASCII)
  2689 syntax (ASCII)
  2690   "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3SUM _:#_. _)" [0, 51, 10] 10)
  2690   "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  (\<open>(3SUM _:#_. _)\<close> [0, 51, 10] 10)
  2691 syntax
  2691 syntax
  2692   "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
  2692   "_sum_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  (\<open>(3\<Sum>_\<in>#_. _)\<close> [0, 51, 10] 10)
  2693 syntax_consts
  2693 syntax_consts
  2694   "_sum_mset_image" \<rightleftharpoons> sum_mset
  2694   "_sum_mset_image" \<rightleftharpoons> sum_mset
  2695 translations
  2695 translations
  2696   "\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST sum_mset (CONST image_mset (\<lambda>i. b) A)"
  2696   "\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST sum_mset (CONST image_mset (\<lambda>i. b) A)"
  2697 
  2697 
  2863   shows "x dvd prod_mset A"
  2863   shows "x dvd prod_mset A"
  2864   using assms prod_mset_subset_imp_dvd [of "{#x#}" A] by simp
  2864   using assms prod_mset_subset_imp_dvd [of "{#x#}" A] by simp
  2865 
  2865 
  2866 end
  2866 end
  2867 
  2867 
  2868 notation prod_mset ("\<Prod>\<^sub>#")
  2868 notation prod_mset (\<open>\<Prod>\<^sub>#\<close>)
  2869 
  2869 
  2870 syntax (ASCII)
  2870 syntax (ASCII)
  2871   "_prod_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3PROD _:#_. _)" [0, 51, 10] 10)
  2871   "_prod_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  (\<open>(3PROD _:#_. _)\<close> [0, 51, 10] 10)
  2872 syntax
  2872 syntax
  2873   "_prod_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
  2873   "_prod_mset_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  (\<open>(3\<Prod>_\<in>#_. _)\<close> [0, 51, 10] 10)
  2874 syntax_consts
  2874 syntax_consts
  2875   "_prod_mset_image" \<rightleftharpoons> prod_mset
  2875   "_prod_mset_image" \<rightleftharpoons> prod_mset
  2876 translations
  2876 translations
  2877   "\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST prod_mset (CONST image_mset (\<lambda>i. b) A)"
  2877   "\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST prod_mset (CONST image_mset (\<lambda>i. b) A)"
  2878 
  2878