src/HOL/Library/Multiset.thy
changeset 74865 b5031a8f7718
parent 74864 c256bba593f3
child 74868 2741ef11ccf6
equal deleted inserted replaced
74864:c256bba593f3 74865:b5031a8f7718
     8 *)
     8 *)
     9 
     9 
    10 section \<open>(Finite) Multisets\<close>
    10 section \<open>(Finite) Multisets\<close>
    11 
    11 
    12 theory Multiset
    12 theory Multiset
    13 imports Cancellation
    13   imports Cancellation
    14 begin
    14 begin
    15 
    15 
    16 subsection \<open>The type of multisets\<close>
    16 subsection \<open>The type of multisets\<close>
    17 
    17 
    18 typedef 'a multiset = \<open>{f :: 'a \<Rightarrow> nat. finite {x. f x > 0}}\<close>
    18 typedef 'a multiset = \<open>{f :: 'a \<Rightarrow> nat. finite {x. f x > 0}}\<close>
  3112   moreover from * [of a] have "a \<notin># K"
  3112   moreover from * [of a] have "a \<notin># K"
  3113     using \<open>asymp r\<close> by (meson asymp.cases)
  3113     using \<open>asymp r\<close> by (meson asymp.cases)
  3114   ultimately show thesis by (auto intro: that)
  3114   ultimately show thesis by (auto intro: that)
  3115 qed
  3115 qed
  3116 
  3116 
       
  3117 lemma trans_mult: "trans r \<Longrightarrow> trans (mult r)"
       
  3118   by (simp add: mult_def)
       
  3119 
       
  3120 lemma transp_multp: "transp r \<Longrightarrow> transp (multp r)"
       
  3121   unfolding multp_def transp_trans_eq
       
  3122   by (fact trans_mult[of "{(x, y). r x y}" for r, folded transp_trans])
       
  3123 
       
  3124 lemma irrefl_mult:
       
  3125   assumes "trans r" "irrefl r"
       
  3126   shows "irrefl (mult r)"
       
  3127 proof (intro irreflI notI)
       
  3128   fix M
       
  3129   assume "(M, M) \<in> mult r"
       
  3130   then obtain I J K where "M = I + J" and "M = I + K"
       
  3131     and "J \<noteq> {#}" and "(\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> r)"
       
  3132     using mult_implies_one_step[OF \<open>trans r\<close>] by blast
       
  3133   then have *: "K \<noteq> {#}" and **: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. (k, j) \<in> r" by auto
       
  3134   have "finite (set_mset K)" by simp
       
  3135   hence "set_mset K = {}"
       
  3136     using **
       
  3137   proof (induction rule: finite_induct)
       
  3138     case empty
       
  3139     thus ?case by simp
       
  3140   next
       
  3141     case (insert x F)
       
  3142     have False
       
  3143       using \<open>irrefl r\<close>[unfolded irrefl_def, rule_format]
       
  3144       using \<open>trans r\<close>[THEN transD]
       
  3145       by (metis equals0D insert.IH insert.prems insertE insertI1 insertI2)
       
  3146     thus ?case ..
       
  3147   qed
       
  3148   with * show False by simp
       
  3149 qed
       
  3150 
       
  3151 lemmas irreflp_multp =
       
  3152   irrefl_mult[of "{(x, y). r x y}" for r,
       
  3153     folded transp_trans_eq irreflp_irrefl_eq, simplified, folded multp_def]
       
  3154 
  3117 instantiation multiset :: (preorder) order begin
  3155 instantiation multiset :: (preorder) order begin
  3118 
  3156 
  3119 definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"
  3157 definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"
  3120   where "M < N \<longleftrightarrow> multp (<) M N"
  3158   where "M < N \<longleftrightarrow> multp (<) M N"
  3121 
  3159 
  3122 definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"
  3160 definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"
  3123   where "less_eq_multiset M N \<longleftrightarrow> M < N \<or> M = N"
  3161   where "less_eq_multiset M N \<longleftrightarrow> M < N \<or> M = N"
  3124 
  3162 
  3125 instance
  3163 instance
  3126 proof -
  3164 proof intro_classes
  3127   have irrefl: "\<not> M < M" for M :: "'a multiset"
  3165   fix M N :: "'a multiset"
  3128   proof
  3166   show "(M < N) = (M \<le> N \<and> \<not> N \<le> M)"
  3129     assume "M < M"
  3167     unfolding less_eq_multiset_def less_multiset_def
  3130     then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def multp_def)
  3168     by (metis irreflp_def irreflp_less irreflp_multp transpE transp_less transp_multp)
  3131     have "trans {(x'::'a, x). x' < x}"
  3169 next
  3132       by (metis (mono_tags, lifting) case_prodD case_prodI less_trans mem_Collect_eq transI)
  3170   fix M :: "'a multiset"
  3133     moreover note MM
  3171   show "M \<le> M"
  3134     ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
  3172     unfolding less_eq_multiset_def
  3135       \<and> J \<noteq> {#} \<and> (\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})"
  3173     by simp
  3136       by (rule mult_implies_one_step)
  3174 next
  3137     then obtain I J K where "M = I + J" and "M = I + K"
  3175   fix M1 M2 M3 :: "'a multiset"
  3138       and "J \<noteq> {#}" and "(\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset J. (k, j) \<in> {(x, y). x < y})" by blast
  3176   show "M1 \<le> M2 \<Longrightarrow> M2 \<le> M3 \<Longrightarrow> M1 \<le> M3"
  3139     then have *: "K \<noteq> {#}" and **: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. k < j" by auto
  3177     unfolding less_eq_multiset_def less_multiset_def
  3140     have "finite (set_mset K)" by simp
  3178     using transp_multp[OF transp_less, THEN transpD]
  3141     moreover note **
  3179     by blast
  3142     ultimately have "set_mset K = {}"
  3180 next
  3143       by (induct rule: finite_induct) (auto intro: order_less_trans)
  3181   fix M N :: "'a multiset"
  3144     with * show False by simp
  3182   show "M \<le> N \<Longrightarrow> N \<le> M \<Longrightarrow> M = N"
  3145   qed
  3183     unfolding less_eq_multiset_def less_multiset_def
  3146   have trans: "K < M \<Longrightarrow> M < N \<Longrightarrow> K < N" for K M N :: "'a multiset"
  3184     using transp_multp[OF transp_less, THEN transpD]
  3147     unfolding less_multiset_def multp_def mult_def by (blast intro: trancl_trans)
  3185     using irreflp_multp[OF transp_less irreflp_less, unfolded irreflp_def, rule_format]
  3148   show "OFCLASS('a multiset, order_class)"
  3186     by blast
  3149     by standard (auto simp add: less_eq_multiset_def irrefl dest: trans)
       
  3150 qed
  3187 qed
  3151 
  3188 
  3152 end
  3189 end
  3153 
  3190 
  3154 lemma mset_le_irrefl [elim!]:
  3191 lemma mset_le_irrefl [elim!]: