src/HOL/Library/Multiset.thy
changeset 76589 1c083e32aed6
parent 76570 608489919ecf
child 76611 a7d2a7a737b8
equal deleted inserted replaced
76588:82a36e3d1b55 76589:1c083e32aed6
  2949   "mult r = (mult1 r)\<^sup>+"
  2949   "mult r = (mult1 r)\<^sup>+"
  2950 
  2950 
  2951 definition multp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
  2951 definition multp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
  2952   "multp r M N \<longleftrightarrow> (M, N) \<in> mult {(x, y). r x y}"
  2952   "multp r M N \<longleftrightarrow> (M, N) \<in> mult {(x, y). r x y}"
  2953 
  2953 
       
  2954 declare multp_def[pred_set_conv]
       
  2955 
  2954 lemma mult1I:
  2956 lemma mult1I:
  2955   assumes "M = add_mset a M0" and "N = M0 + K" and "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
  2957   assumes "M = add_mset a M0" and "N = M0 + K" and "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
  2956   shows "(N, M) \<in> mult1 r"
  2958   shows "(N, M) \<in> mult1 r"
  2957   using assms unfolding mult1_def by blast
  2959   using assms unfolding mult1_def by blast
  2958 
  2960 
  3136           rule_tac x = "K + K'" in exI)
  3138           rule_tac x = "K + K'" in exI)
  3137         (use z y N False K in \<open>auto simp: add.assoc\<close>)
  3139         (use z y N False K in \<open>auto simp: add.assoc\<close>)
  3138   qed
  3140   qed
  3139 qed
  3141 qed
  3140 
  3142 
  3141 lemmas multp_implies_one_step =
  3143 lemma multp_implies_one_step:
  3142   mult_implies_one_step[of "{(x, y). r x y}" for r, folded multp_def transp_trans, simplified]
  3144   "transp R \<Longrightarrow> multp R M N \<Longrightarrow> \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and> (\<forall>k\<in>#K. \<exists>x\<in>#J. R k x)"
       
  3145   by (rule mult_implies_one_step[to_pred])
  3143 
  3146 
  3144 lemma one_step_implies_mult:
  3147 lemma one_step_implies_mult:
  3145   assumes
  3148   assumes
  3146     "J \<noteq> {#}" and
  3149     "J \<noteq> {#}" and
  3147     "\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r"
  3150     "\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r"
  3171     ultimately show ?thesis
  3174     ultimately show ?thesis
  3172       unfolding mult_def by simp
  3175       unfolding mult_def by simp
  3173   qed
  3176   qed
  3174 qed
  3177 qed
  3175 
  3178 
  3176 lemmas one_step_implies_multp =
  3179 lemma one_step_implies_multp:
  3177   one_step_implies_mult[of _ _ "{(x, y). r x y}" for r, folded multp_def, simplified]
  3180   "J \<noteq> {#} \<Longrightarrow> \<forall>k\<in>#K. \<exists>j\<in>#J. R k j \<Longrightarrow> multp R (I + K) (I + J)"
       
  3181   by (rule one_step_implies_mult[of _ _ "{(x, y). r x y}" for r, folded multp_def, simplified])
  3178 
  3182 
  3179 lemma subset_implies_mult:
  3183 lemma subset_implies_mult:
  3180   assumes sub: "A \<subset># B"
  3184   assumes sub: "A \<subset># B"
  3181   shows "(A, B) \<in> mult r"
  3185   shows "(A, B) \<in> mult r"
  3182 proof -
  3186 proof -
  3186     using sub by (simp add: Diff_eq_empty_iff_mset subset_mset.less_le_not_le)
  3190     using sub by (simp add: Diff_eq_empty_iff_mset subset_mset.less_le_not_le)
  3187   thus ?thesis
  3191   thus ?thesis
  3188     by (rule one_step_implies_mult[of "B - A" "{#}" _ A, unfolded ApBmA, simplified])
  3192     by (rule one_step_implies_mult[of "B - A" "{#}" _ A, unfolded ApBmA, simplified])
  3189 qed
  3193 qed
  3190 
  3194 
  3191 lemmas subset_implies_multp = subset_implies_mult[of _ _ "{(x, y). r x y}" for r, folded multp_def]
  3195 lemma subset_implies_multp: "A \<subset># B \<Longrightarrow> multp r A B"
       
  3196   by (rule subset_implies_mult[of _ _ "{(x, y). r x y}" for r, folded multp_def])
  3192 
  3197 
  3193 
  3198 
  3194 subsubsection \<open>Monotonicity\<close>
  3199 subsubsection \<open>Monotonicity\<close>
  3195 
  3200 
  3196 lemma multp_mono_strong:
  3201 lemma multp_mono_strong:
  3340     where "Y = I + J" "X = I + K" "J \<noteq> {#}" "\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> s"
  3345     where "Y = I + J" "X = I + K" "J \<noteq> {#}" "\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> s"
  3341     using mult_implies_one_step[OF \<open>trans s\<close>] by blast
  3346     using mult_implies_one_step[OF \<open>trans s\<close>] by blast
  3342   thus ?L using one_step_implies_mult[of J K s "I + Z"] by (auto simp: ac_simps)
  3347   thus ?L using one_step_implies_mult[of J K s "I + Z"] by (auto simp: ac_simps)
  3343 qed
  3348 qed
  3344 
  3349 
  3345 lemmas multp_cancel =
  3350 lemma multp_cancel:
  3346   mult_cancel[of "{(x, y). r x y}" for r,
  3351   "transp R \<Longrightarrow> irreflp R \<Longrightarrow> multp R (X + Z) (Y + Z) \<longleftrightarrow> multp R X Y"
  3347     folded multp_def transp_trans irreflp_irrefl_eq, simplified]
  3352   by (rule mult_cancel[to_pred])
  3348 
  3353 
  3349 lemmas mult_cancel_add_mset =
  3354 lemma mult_cancel_add_mset:
  3350   mult_cancel[of _ _ "{#_#}", unfolded union_mset_add_mset_right add.comm_neutral]
  3355   "trans r \<Longrightarrow> irrefl r \<Longrightarrow> ((add_mset z X, add_mset z Y) \<in> mult r) = ((X, Y) \<in> mult r)"
  3351 
  3356   by (rule mult_cancel[of _ _ "{#_#}", unfolded union_mset_add_mset_right add.comm_neutral])
  3352 lemmas multp_cancel_add_mset =
  3357 
  3353   mult_cancel_add_mset[of "{(x, y). r x y}" for r,
  3358 lemma multp_cancel_add_mset:
  3354     folded multp_def transp_trans irreflp_irrefl_eq, simplified]
  3359   "transp R \<Longrightarrow> irreflp R \<Longrightarrow> multp R (add_mset z X) (add_mset z Y) = multp R X Y"
       
  3360   by (rule mult_cancel_add_mset[to_pred])
  3355 
  3361 
  3356 lemma mult_cancel_max0:
  3362 lemma mult_cancel_max0:
  3357   assumes "trans s" and "irrefl s"
  3363   assumes "trans s" and "irrefl s"
  3358   shows "(X, Y) \<in> mult s \<longleftrightarrow> (X - X \<inter># Y, Y - X \<inter># Y) \<in> mult s" (is "?L \<longleftrightarrow> ?R")
  3364   shows "(X, Y) \<in> mult s \<longleftrightarrow> (X - X \<inter># Y, Y - X \<inter># Y) \<in> mult s" (is "?L \<longleftrightarrow> ?R")
  3359 proof -
  3365 proof -
  3361   thus ?thesis using mult_cancel[OF assms, of "X - X \<inter># Y"  "X \<inter># Y" "Y - X \<inter># Y"] by auto
  3367   thus ?thesis using mult_cancel[OF assms, of "X - X \<inter># Y"  "X \<inter># Y" "Y - X \<inter># Y"] by auto
  3362 qed
  3368 qed
  3363 
  3369 
  3364 lemmas mult_cancel_max = mult_cancel_max0[simplified]
  3370 lemmas mult_cancel_max = mult_cancel_max0[simplified]
  3365 
  3371 
  3366 lemmas multp_cancel_max =
  3372 lemma multp_cancel_max: "transp R \<Longrightarrow> irreflp R \<Longrightarrow> multp R X Y = multp R (X - Y) (Y - X)"
  3367   mult_cancel_max[of "{(x, y). r x y}" for r,
  3373   by (rule mult_cancel_max[to_pred])
  3368     folded multp_def transp_trans irreflp_irrefl_eq, simplified]
       
  3369 
  3374 
  3370 
  3375 
  3371 subsubsection \<open>Partial-order properties\<close>
  3376 subsubsection \<open>Partial-order properties\<close>
  3372 
  3377 
  3373 lemma mult1_lessE:
  3378 lemma mult1_lessE:
  3414     thus ?case ..
  3419     thus ?case ..
  3415   qed
  3420   qed
  3416   with * show False by simp
  3421   with * show False by simp
  3417 qed
  3422 qed
  3418 
  3423 
  3419 lemmas irreflp_multp =
  3424 lemma irreflp_multp: "transp R \<Longrightarrow> irreflp R \<Longrightarrow> irreflp (multp R)"
  3420   irrefl_mult[of "{(x, y). r x y}" for r,
  3425   by (rule irrefl_mult[of "{(x, y). r x y}" for r,
  3421     folded transp_trans_eq irreflp_irrefl_eq, simplified, folded multp_def]
  3426     folded transp_trans_eq irreflp_irrefl_eq, simplified, folded multp_def])
  3422 
  3427 
  3423 instantiation multiset :: (preorder) order begin
  3428 instantiation multiset :: (preorder) order begin
  3424 
  3429 
  3425 definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"
  3430 definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"
  3426   where "M < N \<longleftrightarrow> multp (<) M N"
  3431   where "M < N \<longleftrightarrow> multp (<) M N"