# HG changeset patch # User blanchet # Date 1408307278 -7200 # Node ID 6fab7e95587d0911ae07ae542f71d6af6a399f7f # Parent a18a351132b7669e7efa9bec086297d6766a961f use 'image_mset' as BNF map function diff -r a18a351132b7 -r 6fab7e95587d src/HOL/Library/Multiset.thy --- a/src/HOL/Library/Multiset.thy Sun Aug 17 16:24:04 2014 +0200 +++ b/src/HOL/Library/Multiset.thy Sun Aug 17 22:27:58 2014 +0200 @@ -2224,697 +2224,214 @@ subsection {* BNF setup *} -lemma setsum_gt_0_iff: -fixes f :: "'a \ nat" assumes "finite A" -shows "setsum f A > 0 \ (\ a \ A. f a > 0)" -(is "?L \ ?R") -proof- - have "?L \ \ setsum f A = 0" by fast - also have "... \ (\ a \ A. f a \ 0)" using assms by simp - also have "... \ ?R" by simp - finally show ?thesis . -qed - -lift_definition mmap :: "('a \ 'b) \ 'a multiset \ 'b multiset" is - "\h f b. setsum f {a. h a = b \ f a > 0} :: nat" -unfolding multiset_def proof safe - fix h :: "'a \ 'b" and f :: "'a \ nat" - assume fin: "finite {a. 0 < f a}" (is "finite ?A") - show "finite {b. 0 < setsum f {a. h a = b \ 0 < f a}}" - (is "finite {b. 0 < setsum f (?As b)}") - proof- let ?B = "{b. 0 < setsum f (?As b)}" - have "\ b. finite (?As b)" using fin by simp - hence B: "?B = {b. ?As b \ {}}" by (auto simp add: setsum_gt_0_iff) - hence "?B \ h ` ?A" by auto - thus ?thesis using finite_surj[OF fin] by auto - qed -qed - -lemma mmap_id0: "mmap id = id" -proof (intro ext multiset_eqI) - fix f a show "count (mmap id f) a = count (id f) a" - proof (cases "count f a = 0") - case False - hence 1: "{aa. aa = a \ aa \# f} = {a}" by auto - thus ?thesis by transfer auto - qed (transfer, simp) -qed - -lemma inj_on_setsum_inv: -assumes 1: "(0::nat) < setsum (count f) {a. h a = b' \ a \# f}" (is "0 < setsum (count f) ?A'") -and 2: "{a. h a = b \ a \# f} = {a. h a = b' \ a \# f}" (is "?A = ?A'") -shows "b = b'" -using assms by (auto simp add: setsum_gt_0_iff) - -lemma mmap_comp: -fixes h1 :: "'a \ 'b" and h2 :: "'b \ 'c" -shows "mmap (h2 o h1) = mmap h2 o mmap h1" -proof (intro ext multiset_eqI) - fix f :: "'a multiset" fix c :: 'c - let ?A = "{a. h2 (h1 a) = c \ a \# f}" - let ?As = "\ b. {a. h1 a = b \ a \# f}" - let ?B = "{b. h2 b = c \ 0 < setsum (count f) (?As b)}" - have 0: "{?As b | b. b \ ?B} = ?As ` ?B" by auto - have "\ b. finite (?As b)" by transfer (simp add: multiset_def) - hence "?B = {b. h2 b = c \ ?As b \ {}}" by (auto simp add: setsum_gt_0_iff) - hence A: "?A = \ {?As b | b. b \ ?B}" by auto - have "setsum (count f) ?A = setsum (setsum (count f)) {?As b | b. b \ ?B}" - unfolding A by transfer (intro setsum.Union_disjoint [simplified], auto simp: multiset_def setsum.Union_disjoint) - also have "... = setsum (setsum (count f)) (?As ` ?B)" unfolding 0 .. - also have "... = setsum (setsum (count f) o ?As) ?B" - by (intro setsum.reindex) (auto simp add: setsum_gt_0_iff inj_on_def) - also have "... = setsum (\ b. setsum (count f) (?As b)) ?B" unfolding comp_def .. - finally have "setsum (count f) ?A = setsum (\ b. setsum (count f) (?As b)) ?B" . - thus "count (mmap (h2 \ h1) f) c = count ((mmap h2 \ mmap h1) f) c" - by transfer (unfold comp_apply, blast) -qed - -lemma mmap_cong: -assumes "\a. a \# M \ f a = g a" -shows "mmap f M = mmap g M" -using assms by transfer (auto intro!: setsum.cong) - -context -begin -interpretation lifting_syntax . - -lemma set_of_transfer[transfer_rule]: "(pcr_multiset op = ===> op =) (\f. {a. 0 < f a}) set_of" - unfolding set_of_def pcr_multiset_def cr_multiset_def rel_fun_def by auto - -end - -lemma set_of_mmap: "set_of o mmap h = image h o set_of" -proof (rule ext, unfold comp_apply) - fix M show "set_of (mmap h M) = h ` set_of M" - by transfer (auto simp add: multiset_def setsum_gt_0_iff) -qed - -lemma multiset_of_surj: - "multiset_of ` {as. set as \ A} = {M. set_of M \ A}" -proof safe - fix M assume M: "set_of M \ A" - obtain as where eq: "M = multiset_of as" using surj_multiset_of unfolding surj_def by auto - hence "set as \ A" using M by auto - thus "M \ multiset_of ` {as. set as \ A}" using eq by auto +definition rel_mset where + "rel_mset R X Y \ (\xs ys. multiset_of xs = X \ multiset_of ys = Y \ list_all2 R xs ys)" + +lemma multiset_of_zip_take_Cons_drop_twice: + assumes "length xs = length ys" "j \ length xs" + shows "multiset_of (zip (take j xs @ x # drop j xs) (take j ys @ y # drop j ys)) = + multiset_of (zip xs ys) + {#(x, y)#}" +using assms +proof (induct xs ys arbitrary: x y j rule: list_induct2) + case Nil + thus ?case + by simp next - show "\x xa xb. \set xa \ A; xb \ set_of (multiset_of xa)\ \ xb \ A" - by (erule set_mp) (unfold set_of_multiset_of) -qed - -lemma card_of_set_of: -"(card_of {M. set_of M \ A}, card_of {as. set as \ A}) \ ordLeq" -apply(rule surj_imp_ordLeq[of _ multiset_of]) using multiset_of_surj by auto - -lemma nat_sum_induct: -assumes "\n1 n2. (\ m1 m2. m1 + m2 < n1 + n2 \ phi m1 m2) \ phi n1 n2" -shows "phi (n1::nat) (n2::nat)" -proof- - let ?chi = "\ n1n2 :: nat * nat. phi (fst n1n2) (snd n1n2)" - have "?chi (n1,n2)" - apply(induct rule: measure_induct[of "\ n1n2. fst n1n2 + snd n1n2" ?chi]) - using assms by (metis fstI sndI) - thus ?thesis by simp -qed - -lemma matrix_count: -fixes ct1 ct2 :: "nat \ nat" -assumes "setsum ct1 {.. ct. (\ i1 \ n1. setsum (\ i2. ct i1 i2) {.. - (\ i2 \ n2. setsum (\ i1. ct i1 i2) {.. ct1 ct2 :: nat \ nat. - setsum ct1 {.. ?phi ct1 ct2 n1 n2" - proof(induct rule: nat_sum_induct[of -"\ n1 n2. \ ct1 ct2 :: nat \ nat. - setsum ct1 {.. ?phi ct1 ct2 n1 n2"], - clarify) - fix n1 n2 :: nat and ct1 ct2 :: "nat \ nat" - assume IH: "\ m1 m2. m1 + m2 < n1 + n2 \ - \ dt1 dt2 :: nat \ nat. - setsum dt1 {.. ?phi dt1 dt2 m1 m2" - and ss: "setsum ct1 {.. ct2 n2") - case True - def dt2 \ "\ i2. if i2 = n2 then ct2 i2 - ct1 n1 else ct2 i2" - have "setsum ct1 {.. i1. i1 \ m1 \ setsum (\ i2. dt i1 i2) {.. i2. i2 \ n2 \ setsum (\ i1. dt i1 i2) {.. "\ i1. if i1 = n1 then ct1 i1 - ct2 n2 else ct1 i1" - have "setsum dt1 {.. i1. i1 \ n1 \ setsum (\ i2. dt i1 i2) {.. i2. i2 \ m2 \ setsum (\ i1. dt i1 i2) {.. - \ b1 b1' b2 b2'. {b1,b1'} \ B1 \ {b2,b2'} \ B2 \ u b1 b2 = u b1' b2' - \ b1 = b1' \ b2 = b2'" - -lemma matrix_setsum_finite: -assumes B1: "B1 \ {}" "finite B1" and B2: "B2 \ {}" "finite B2" and u: "inj2 u B1 B2" -and ss: "setsum N1 B1 = setsum N2 B2" -shows "\ M :: 'a \ nat. - (\ b1 \ B1. setsum (\ b2. M (u b1 b2)) B2 = N1 b1) \ - (\ b2 \ B2. setsum (\ b1. M (u b1 b2)) B1 = N2 b2)" -proof- - obtain n1 where "card B1 = Suc n1" using B1 by (metis card_insert finite.simps) - then obtain e1 where e1: "bij_betw e1 {.. "inv_into {.. i1. i1 < Suc n1 \ f1 (e1 i1) = i1" - and e1f1[simp]: "\ b1. b1 \ B1 \ e1 (f1 b1) = b1" unfolding f1_def - apply (metis bij_betw_inv_into e1, metis bij_betw_inv_into_left e1 lessThan_iff) - by (metis e1_surj f_inv_into_f) - (* *) - obtain n2 where "card B2 = Suc n2" using B2 by (metis card_insert finite.simps) - then obtain e2 where e2: "bij_betw e2 {.. "inv_into {.. i2. i2 < Suc n2 \ f2 (e2 i2) = i2" - and e2f2[simp]: "\ b2. b2 \ B2 \ e2 (f2 b2) = b2" unfolding f2_def - apply (metis bij_betw_inv_into e2, metis bij_betw_inv_into_left e2 lessThan_iff) - by (metis e2_surj f_inv_into_f) - (* *) - let ?ct1 = "N1 o e1" let ?ct2 = "N2 o e2" - have ss: "setsum ?ct1 {.. i1. i1 \ n1 \ setsum (\ i2. ct i1 i2) {.. i2. i2 \ n2 \ setsum (\ i1. ct i1 i2) {.. "{u b1 b2 | b1 b2. b1 \ B1 \ b2 \ B2}" - have "\ a \ A. \ b1b2 \ B1 <*> B2. u (fst b1b2) (snd b1b2) = a" - unfolding A_def Ball_def mem_Collect_eq by auto - then obtain h1h2 where h12: - "\a. a \ A \ u (fst (h1h2 a)) (snd (h1h2 a)) = a \ h1h2 a \ B1 <*> B2" by metis - def h1 \ "fst o h1h2" def h2 \ "snd o h1h2" - have h12[simp]: "\a. a \ A \ u (h1 a) (h2 a) = a" - "\ a. a \ A \ h1 a \ B1" "\ a. a \ A \ h2 a \ B2" - using h12 unfolding h1_def h2_def by force+ - {fix b1 b2 assume b1: "b1 \ B1" and b2: "b2 \ B2" - hence inA: "u b1 b2 \ A" unfolding A_def by auto - hence "u b1 b2 = u (h1 (u b1 b2)) (h2 (u b1 b2))" by auto - moreover have "h1 (u b1 b2) \ B1" "h2 (u b1 b2) \ B2" using inA by auto - ultimately have "h1 (u b1 b2) = b1 \ h2 (u b1 b2) = b2" - using u b1 b2 unfolding inj2_def by fastforce - } - hence h1[simp]: "\ b1 b2. \b1 \ B1; b2 \ B2\ \ h1 (u b1 b2) = b1" and - h2[simp]: "\ b1 b2. \b1 \ B1; b2 \ B2\ \ h2 (u b1 b2) = b2" by auto - def M \ "\ a. ct (f1 (h1 a)) (f2 (h2 a))" - show ?thesis - apply(rule exI[of _ M]) proof safe - fix b1 assume b1: "b1 \ B1" - hence f1b1: "f1 b1 \ n1" using f1 unfolding bij_betw_def - by (metis image_eqI lessThan_iff less_Suc_eq_le) - have "(\b2\B2. M (u b1 b2)) = (\i2b2\B2. M (u b1 b2)) = N1 b1" . - next - fix b2 assume b2: "b2 \ B2" - hence f2b2: "f2 b2 \ n2" using f2 unfolding bij_betw_def - by (metis image_eqI lessThan_iff less_Suc_eq_le) - have "(\b1\B1. M (u b1 b2)) = (\i1b1\B1. M (u b1 b2)) = N2 b2" . - qed -qed - -lemma supp_vimage_mmap: "set_of M \ f -` (set_of (mmap f M))" - by transfer (auto simp: multiset_def setsum_gt_0_iff) - -lemma mmap_ge_0: "b \# mmap f M \ (\a. a \# M \ f a = b)" - by transfer (auto simp: multiset_def setsum_gt_0_iff) - -lemma finite_twosets: -assumes "finite B1" and "finite B2" -shows "finite {u b1 b2 |b1 b2. b1 \ B1 \ b2 \ B2}" (is "finite ?A") -proof- - have A: "?A = (\ b1b2. u (fst b1b2) (snd b1b2)) ` (B1 <*> B2)" by force - show ?thesis unfolding A using finite_cartesian_product[OF assms] by auto + case False + then obtain k where k: "j = Suc k" + by (case_tac j) simp + hence "k \ length xs" + using Cons.prems by auto + hence "multiset_of (zip (take k xs @ x # drop k xs) (take k ys @ y # drop k ys)) = + multiset_of (zip xs ys) + {#(x, y)#}" + by (rule Cons.hyps(2)) + thus ?thesis + unfolding k by (auto simp: add.commute union_lcomm) + qed qed -(* Weak pullbacks: *) -definition wpull where -"wpull A B1 B2 f1 f2 p1 p2 \ - (\ b1 b2. b1 \ B1 \ b2 \ B2 \ f1 b1 = f2 b2 \ (\ a \ A. p1 a = b1 \ p2 a = b2))" - -(* Weak pseudo-pullbacks *) -definition wppull where -"wppull A B1 B2 f1 f2 e1 e2 p1 p2 \ - (\ b1 b2. b1 \ B1 \ b2 \ B2 \ f1 b1 = f2 b2 \ - (\ a \ A. e1 (p1 a) = e1 b1 \ e2 (p2 a) = e2 b2))" - - -(* The pullback of sets *) -definition thePull where -"thePull B1 B2 f1 f2 = {(b1,b2). b1 \ B1 \ b2 \ B2 \ f1 b1 = f2 b2}" - -lemma wpull_thePull: -"wpull (thePull B1 B2 f1 f2) B1 B2 f1 f2 fst snd" -unfolding wpull_def thePull_def by auto - -lemma wppull_thePull: -assumes "wppull A B1 B2 f1 f2 e1 e2 p1 p2" -shows -"\ j. \ a' \ thePull B1 B2 f1 f2. - j a' \ A \ - e1 (p1 (j a')) = e1 (fst a') \ e2 (p2 (j a')) = e2 (snd a')" -(is "\ j. \ a' \ ?A'. ?phi a' (j a')") -proof(rule bchoice[of ?A' ?phi], default) - fix a' assume a': "a' \ ?A'" - hence "fst a' \ B1" unfolding thePull_def by auto - moreover - from a' have "snd a' \ B2" unfolding thePull_def by auto - moreover have "f1 (fst a') = f2 (snd a')" - using a' unfolding csquare_def thePull_def by auto - ultimately show "\ ja'. ?phi a' ja'" - using assms unfolding wppull_def by blast -qed - -lemma wpull_wppull: -assumes wp: "wpull A' B1 B2 f1 f2 p1' p2'" and -1: "\ a' \ A'. j a' \ A \ e1 (p1 (j a')) = e1 (p1' a') \ e2 (p2 (j a')) = e2 (p2' a')" -shows "wppull A B1 B2 f1 f2 e1 e2 p1 p2" -unfolding wppull_def proof safe - fix b1 b2 - assume b1: "b1 \ B1" and b2: "b2 \ B2" and f: "f1 b1 = f2 b2" - then obtain a' where a': "a' \ A'" and b1: "b1 = p1' a'" and b2: "b2 = p2' a'" - using wp unfolding wpull_def by blast - show "\a\A. e1 (p1 a) = e1 b1 \ e2 (p2 a) = e2 b2" - apply (rule bexI[of _ "j a'"]) unfolding b1 b2 using a' 1 by auto +lemma ex_multiset_of_zip_left: + assumes "length xs = length ys" "multiset_of xs' = multiset_of xs" + shows "\ys'. length ys' = length xs' \ multiset_of (zip xs' ys') = multiset_of (zip xs ys)" +using assms +proof (induct xs ys arbitrary: xs' rule: list_induct2) + case Nil + thus ?case + by auto +next + case (Cons x xs y ys xs') + obtain j where j_len: "j < length xs'" and nth_j: "xs' ! j = x" + proof - + assume "\j. \j < length xs'; xs' ! j = x\ \ ?thesis" + moreover have "\k m n. (m\nat) + n < m + k \ \ n < k" by linarith + moreover have "\n a as. n - n < length (a # as) \ n < n" + by (metis Nat.add_diff_inverse diff_add_inverse2 impossible_Cons le_add1 + less_diff_conv not_add_less2) + moreover have "\ length xs' < length xs'" by blast + ultimately show ?thesis + by (metis (no_types) Cons.prems Nat.add_diff_inverse diff_add_inverse2 length_append + less_diff_conv list.set_intros(1) multiset_of_eq_setD nth_append_length split_list) + qed + + def xsa \ "take j xs' @ drop (Suc j) xs'" + have "multiset_of xs' = {#x#} + multiset_of xsa" + unfolding xsa_def using j_len nth_j + by (metis (no_types) ab_semigroup_add_class.add_ac(1) append_take_drop_id drop_Suc_conv_tl + multiset_of.simps(2) union_code union_commute) + hence ms_x: "multiset_of xsa = multiset_of xs" + by (metis Cons.prems add.commute add_right_imp_eq multiset_of.simps(2)) + then obtain ysa where + len_a: "length ysa = length xsa" and ms_a: "multiset_of (zip xsa ysa) = multiset_of (zip xs ys)" + using Cons.hyps(2) by blast + + def ys' \ "take j ysa @ y # drop j ysa" + have xs': "xs' = take j xsa @ x # drop j xsa" + using ms_x j_len nth_j Cons.prems xsa_def + by (metis append_eq_append_conv append_take_drop_id diff_Suc_Suc drop_Suc_conv_tl length_Cons + length_drop mcard_multiset_of) + have j_len': "j \ length xsa" + using j_len xs' xsa_def + by (metis add_Suc_right append_take_drop_id length_Cons length_append less_eq_Suc_le not_less) + have "length ys' = length xs'" + unfolding ys'_def using Cons.prems len_a ms_x + by (metis add_Suc_right append_take_drop_id length_Cons length_append multiset_of_eq_length) + moreover have "multiset_of (zip xs' ys') = multiset_of (zip (x # xs) (y # ys))" + unfolding xs' ys'_def + by (rule trans[OF multiset_of_zip_take_Cons_drop_twice]) + (auto simp: len_a ms_a j_len' add.commute) + ultimately show ?case + by blast qed -lemma wppull_fstOp_sndOp: -shows "wppull (Collect (split (P OO Q))) (Collect (split P)) (Collect (split Q)) - snd fst fst snd (BNF_Def.fstOp P Q) (BNF_Def.sndOp P Q)" -using pick_middlep unfolding wppull_def fstOp_def sndOp_def relcompp.simps by auto - -lemma wpull_mmap: -fixes A :: "'a set" and B1 :: "'b1 set" and B2 :: "'b2 set" -assumes wp: "wpull A B1 B2 f1 f2 p1 p2" -shows -"wpull {M. set_of M \ A} - {N1. set_of N1 \ B1} {N2. set_of N2 \ B2} - (mmap f1) (mmap f2) (mmap p1) (mmap p2)" -unfolding wpull_def proof (safe, unfold Bex_def mem_Collect_eq) - fix N1 :: "'b1 multiset" and N2 :: "'b2 multiset" - assume mmap': "mmap f1 N1 = mmap f2 N2" - and N1[simp]: "set_of N1 \ B1" - and N2[simp]: "set_of N2 \ B2" - def P \ "mmap f1 N1" - have P1: "P = mmap f1 N1" and P2: "P = mmap f2 N2" unfolding P_def using mmap' by auto - note P = P1 P2 - have fin_N1[simp]: "finite (set_of N1)" - and fin_N2[simp]: "finite (set_of N2)" - and fin_P[simp]: "finite (set_of P)" by auto - - def set1 \ "\ c. {b1 \ set_of N1. f1 b1 = c}" - have set1[simp]: "\ c b1. b1 \ set1 c \ f1 b1 = c" unfolding set1_def by auto - have fin_set1: "\ c. c \ set_of P \ finite (set1 c)" - using N1(1) unfolding set1_def multiset_def by auto - have set1_NE: "\ c. c \ set_of P \ set1 c \ {}" - unfolding set1_def set_of_def P mmap_ge_0 by auto - have supp_N1_set1: "set_of N1 = (\ c \ set_of P. set1 c)" - using supp_vimage_mmap[of N1 f1] unfolding set1_def P1 by auto - hence set1_inclN1: "\c. c \ set_of P \ set1 c \ set_of N1" by auto - hence set1_incl: "\ c. c \ set_of P \ set1 c \ B1" using N1 by blast - have set1_disj: "\ c c'. c \ c' \ set1 c \ set1 c' = {}" - unfolding set1_def by auto - have setsum_set1: "\ c. setsum (count N1) (set1 c) = count P c" - unfolding P1 set1_def by transfer (auto intro: setsum.cong) - - def set2 \ "\ c. {b2 \ set_of N2. f2 b2 = c}" - have set2[simp]: "\ c b2. b2 \ set2 c \ f2 b2 = c" unfolding set2_def by auto - have fin_set2: "\ c. c \ set_of P \ finite (set2 c)" - using N2(1) unfolding set2_def multiset_def by auto - have set2_NE: "\ c. c \ set_of P \ set2 c \ {}" - unfolding set2_def P2 mmap_ge_0 set_of_def by auto - have supp_N2_set2: "set_of N2 = (\ c \ set_of P. set2 c)" - using supp_vimage_mmap[of N2 f2] unfolding set2_def P2 by auto - hence set2_inclN2: "\c. c \ set_of P \ set2 c \ set_of N2" by auto - hence set2_incl: "\ c. c \ set_of P \ set2 c \ B2" using N2 by blast - have set2_disj: "\ c c'. c \ c' \ set2 c \ set2 c' = {}" - unfolding set2_def by auto - have setsum_set2: "\ c. setsum (count N2) (set2 c) = count P c" - unfolding P2 set2_def by transfer (auto intro: setsum.cong) - - have ss: "\ c. c \ set_of P \ setsum (count N1) (set1 c) = setsum (count N2) (set2 c)" - unfolding setsum_set1 setsum_set2 .. - have "\ c \ set_of P. \ b1b2 \ (set1 c) \ (set2 c). - \ a \ A. p1 a = fst b1b2 \ p2 a = snd b1b2" - using wp set1_incl set2_incl unfolding wpull_def Ball_def mem_Collect_eq - by simp (metis set1 set2 set_rev_mp) - then obtain uu where uu: - "\ c \ set_of P. \ b1b2 \ (set1 c) \ (set2 c). - uu c b1b2 \ A \ p1 (uu c b1b2) = fst b1b2 \ p2 (uu c b1b2) = snd b1b2" by metis - def u \ "\ c b1 b2. uu c (b1,b2)" - have u[simp]: - "\ c b1 b2. \c \ set_of P; b1 \ set1 c; b2 \ set2 c\ \ u c b1 b2 \ A" - "\ c b1 b2. \c \ set_of P; b1 \ set1 c; b2 \ set2 c\ \ p1 (u c b1 b2) = b1" - "\ c b1 b2. \c \ set_of P; b1 \ set1 c; b2 \ set2 c\ \ p2 (u c b1 b2) = b2" - using uu unfolding u_def by auto - {fix c assume c: "c \ set_of P" - have "inj2 (u c) (set1 c) (set2 c)" unfolding inj2_def proof clarify - fix b1 b1' b2 b2' - assume "{b1, b1'} \ set1 c" "{b2, b2'} \ set2 c" and 0: "u c b1 b2 = u c b1' b2'" - hence "p1 (u c b1 b2) = b1 \ p2 (u c b1 b2) = b2 \ - p1 (u c b1' b2') = b1' \ p2 (u c b1' b2') = b2'" - using u(2)[OF c] u(3)[OF c] by simp metis - thus "b1 = b1' \ b2 = b2'" using 0 by auto - qed - } note inj = this - def sset \ "\ c. {u c b1 b2 | b1 b2. b1 \ set1 c \ b2 \ set2 c}" - have fin_sset[simp]: "\ c. c \ set_of P \ finite (sset c)" unfolding sset_def - using fin_set1 fin_set2 finite_twosets by blast - have sset_A: "\ c. c \ set_of P \ sset c \ A" unfolding sset_def by auto - {fix c a assume c: "c \ set_of P" and ac: "a \ sset c" - then obtain b1 b2 where b1: "b1 \ set1 c" and b2: "b2 \ set2 c" - and a: "a = u c b1 b2" unfolding sset_def by auto - have "p1 a \ set1 c" and p2a: "p2 a \ set2 c" - using ac a b1 b2 c u(2) u(3) by simp+ - hence "u c (p1 a) (p2 a) = a" unfolding a using b1 b2 inj[OF c] - unfolding inj2_def by (metis c u(2) u(3)) - } note u_p12[simp] = this - {fix c a assume c: "c \ set_of P" and ac: "a \ sset c" - hence "p1 a \ set1 c" unfolding sset_def by auto - }note p1[simp] = this - {fix c a assume c: "c \ set_of P" and ac: "a \ sset c" - hence "p2 a \ set2 c" unfolding sset_def by auto - }note p2[simp] = this - - {fix c assume c: "c \ set_of P" - hence "\ M. (\ b1 \ set1 c. setsum (\ b2. M (u c b1 b2)) (set2 c) = count N1 b1) \ - (\ b2 \ set2 c. setsum (\ b1. M (u c b1 b2)) (set1 c) = count N2 b2)" - unfolding sset_def - using matrix_setsum_finite[OF set1_NE[OF c] fin_set1[OF c] - set2_NE[OF c] fin_set2[OF c] inj[OF c] ss[OF c]] by auto - } - then obtain Ms where - ss1: "\ c b1. \c \ set_of P; b1 \ set1 c\ \ - setsum (\ b2. Ms c (u c b1 b2)) (set2 c) = count N1 b1" and - ss2: "\ c b2. \c \ set_of P; b2 \ set2 c\ \ - setsum (\ b1. Ms c (u c b1 b2)) (set1 c) = count N2 b2" - by metis - def SET \ "\ c \ set_of P. sset c" - have fin_SET[simp]: "finite SET" unfolding SET_def apply(rule finite_UN_I) by auto - have SET_A: "SET \ A" unfolding SET_def using sset_A by blast - have u_SET[simp]: "\ c b1 b2. \c \ set_of P; b1 \ set1 c; b2 \ set2 c\ \ u c b1 b2 \ SET" - unfolding SET_def sset_def by blast - {fix c a assume c: "c \ set_of P" and a: "a \ SET" and p1a: "p1 a \ set1 c" - then obtain c' where c': "c' \ set_of P" and ac': "a \ sset c'" - unfolding SET_def by auto - hence "p1 a \ set1 c'" unfolding sset_def by auto - hence eq: "c = c'" using p1a c c' set1_disj by auto - hence "a \ sset c" using ac' by simp - } note p1_rev = this - {fix c a assume c: "c \ set_of P" and a: "a \ SET" and p2a: "p2 a \ set2 c" - then obtain c' where c': "c' \ set_of P" and ac': "a \ sset c'" - unfolding SET_def by auto - hence "p2 a \ set2 c'" unfolding sset_def by auto - hence eq: "c = c'" using p2a c c' set2_disj by auto - hence "a \ sset c" using ac' by simp - } note p2_rev = this - - have "\ a \ SET. \ c \ set_of P. a \ sset c" unfolding SET_def by auto - then obtain h where h: "\ a \ SET. h a \ set_of P \ a \ sset (h a)" by metis - have h_u[simp]: "\ c b1 b2. \c \ set_of P; b1 \ set1 c; b2 \ set2 c\ - \ h (u c b1 b2) = c" - by (metis h p2 set2 u(3) u_SET) - have h_u1: "\ c b1 b2. \c \ set_of P; b1 \ set1 c; b2 \ set2 c\ - \ h (u c b1 b2) = f1 b1" - using h unfolding sset_def by auto - have h_u2: "\ c b1 b2. \c \ set_of P; b1 \ set1 c; b2 \ set2 c\ - \ h (u c b1 b2) = f2 b2" - using h unfolding sset_def by auto - def M \ - "Abs_multiset (\ a. if a \ SET \ p1 a \ set_of N1 \ p2 a \ set_of N2 then Ms (h a) a else 0)" - have "(\ a. if a \ SET \ p1 a \ set_of N1 \ p2 a \ set_of N2 then Ms (h a) a else 0) \ multiset" - unfolding multiset_def by auto - hence [transfer_rule]: "pcr_multiset op = (\ a. if a \ SET \ p1 a \ set_of N1 \ p2 a \ set_of N2 then Ms (h a) a else 0) M" - unfolding M_def pcr_multiset_def cr_multiset_def by (auto simp: Abs_multiset_inverse) - have sM: "set_of M \ SET" "set_of M \ p1 -` (set_of N1)" "set_of M \ p2 -` set_of N2" - by (transfer, auto split: split_if_asm)+ - show "\M. set_of M \ A \ mmap p1 M = N1 \ mmap p2 M = N2" - proof(rule exI[of _ M], safe) - fix a assume *: "a \ set_of M" - from SET_A show "a \ A" - proof (cases "a \ SET") - case False thus ?thesis using * by transfer' auto - qed blast - next - show "mmap p1 M = N1" - proof(intro multiset_eqI) - fix b1 - let ?K = "{a. p1 a = b1 \ a \# M}" - have "setsum (count M) ?K = count N1 b1" - proof(cases "b1 \ set_of N1") - case False - hence "?K = {}" using sM(2) by auto - thus ?thesis using False by auto - next - case True - def c \ "f1 b1" - have c: "c \ set_of P" and b1: "b1 \ set1 c" - unfolding set1_def c_def P1 using True by (auto simp: comp_eq_dest[OF set_of_mmap]) - with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p1 a = b1 \ a \ SET}" - by transfer (force intro: setsum.mono_neutral_cong_left split: split_if_asm) - also have "... = setsum (count M) ((\ b2. u c b1 b2) ` (set2 c))" - apply(rule setsum.cong) using c b1 proof safe - fix a assume p1a: "p1 a \ set1 c" and "c \ set_of P" and "a \ SET" - hence ac: "a \ sset c" using p1_rev by auto - hence "a = u c (p1 a) (p2 a)" using c by auto - moreover have "p2 a \ set2 c" using ac c by auto - ultimately show "a \ u c (p1 a) ` set2 c" by auto - qed auto - also have "... = setsum (\ b2. count M (u c b1 b2)) (set2 c)" - unfolding comp_def[symmetric] apply(rule setsum.reindex) - using inj unfolding inj_on_def inj2_def using b1 c u(3) by blast - also have "... = count N1 b1" unfolding ss1[OF c b1, symmetric] - apply(rule setsum.cong[OF refl]) apply (transfer fixing: Ms u c b1 set2) - using True h_u[OF c b1] set2_def u(2,3)[OF c b1] u_SET[OF c b1] - [[hypsubst_thin = true]] - by fastforce - finally show ?thesis . - qed - thus "count (mmap p1 M) b1 = count N1 b1" by transfer - qed - next - show "mmap p2 M = N2" - proof(intro multiset_eqI) - fix b2 - let ?K = "{a. p2 a = b2 \ a \# M}" - have "setsum (count M) ?K = count N2 b2" - proof(cases "b2 \ set_of N2") - case False - hence "?K = {}" using sM(3) by auto - thus ?thesis using False by auto - next - case True - def c \ "f2 b2" - have c: "c \ set_of P" and b2: "b2 \ set2 c" - unfolding set2_def c_def P2 using True by (auto simp: comp_eq_dest[OF set_of_mmap]) - with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p2 a = b2 \ a \ SET}" - by transfer (force intro: setsum.mono_neutral_cong_left split: split_if_asm) - also have "... = setsum (count M) ((\ b1. u c b1 b2) ` (set1 c))" - apply(rule setsum.cong) using c b2 proof safe - fix a assume p2a: "p2 a \ set2 c" and "c \ set_of P" and "a \ SET" - hence ac: "a \ sset c" using p2_rev by auto - hence "a = u c (p1 a) (p2 a)" using c by auto - moreover have "p1 a \ set1 c" using ac c by auto - ultimately show "a \ (\x. u c x (p2 a)) ` set1 c" by auto - qed auto - also have "... = setsum (count M o (\ b1. u c b1 b2)) (set1 c)" - apply(rule setsum.reindex) - using inj unfolding inj_on_def inj2_def using b2 c u(2) by blast - also have "... = setsum (\ b1. count M (u c b1 b2)) (set1 c)" by simp - also have "... = count N2 b2" unfolding ss2[OF c b2, symmetric] comp_def - apply(rule setsum.cong[OF refl]) apply (transfer fixing: Ms u c b2 set1) - using True h_u1[OF c _ b2] u(2,3)[OF c _ b2] u_SET[OF c _ b2] set1_def - [[hypsubst_thin = true]] - by fastforce - finally show ?thesis . - qed - thus "count (mmap p2 M) b2 = count N2 b2" by transfer - qed - qed +lemma list_all2_reorder_left_invariance: + assumes rel: "list_all2 R xs ys" and ms_x: "multiset_of xs' = multiset_of xs" + shows "\ys'. list_all2 R xs' ys' \ multiset_of ys' = multiset_of ys" +proof - + have len: "length xs = length ys" + using rel list_all2_conv_all_nth by auto + obtain ys' where + len': "length xs' = length ys'" and ms_xy: "multiset_of (zip xs' ys') = multiset_of (zip xs ys)" + using len ms_x by (metis ex_multiset_of_zip_left) + have "list_all2 R xs' ys'" + using assms(1) len' ms_xy unfolding list_all2_iff by (blast dest: multiset_of_eq_setD) + moreover have "multiset_of ys' = multiset_of ys" + using len len' ms_xy map_snd_zip multiset_of_map by metis + ultimately show ?thesis + by blast qed -lemma set_of_bd: "(card_of (set_of x), natLeq) \ ordLeq" - by transfer - (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def) - -lemma wppull_mmap: - assumes "wppull A B1 B2 f1 f2 e1 e2 p1 p2" - shows "wppull {M. set_of M \ A} {N1. set_of N1 \ B1} {N2. set_of N2 \ B2} - (mmap f1) (mmap f2) (mmap e1) (mmap e2) (mmap p1) (mmap p2)" -proof - - from assms obtain j where j: "\a'\thePull B1 B2 f1 f2. - j a' \ A \ e1 (p1 (j a')) = e1 (fst a') \ e2 (p2 (j a')) = e2 (snd a')" - by (blast dest: wppull_thePull) - then show ?thesis - by (intro wpull_wppull[OF wpull_mmap[OF wpull_thePull], of _ _ _ _ "mmap j"]) - (auto simp: comp_eq_dest_lhs[OF mmap_comp[symmetric]] comp_eq_dest[OF set_of_mmap] - intro!: mmap_cong simp del: mem_set_of_iff simp: mem_set_of_iff[symmetric]) -qed +lemma ex_multiset_of: "\xs. multiset_of xs = X" + by (induct X) (simp, metis multiset_of.simps(2)) bnf "'a multiset" - map: mmap + map: image_mset sets: set_of bd: natLeq wits: "{#}" -by (auto simp add: mmap_id0 mmap_comp set_of_mmap natLeq_card_order natLeq_cinfinite set_of_bd - Grp_def relcompp.simps intro: mmap_cong) - (metis wppull_mmap[OF wppull_fstOp_sndOp, unfolded wppull_def - o_eq_dest_lhs[OF mmap_comp[symmetric]] fstOp_def sndOp_def comp_def, simplified]) - -inductive rel_multiset' where - Zero[intro]: "rel_multiset' R {#} {#}" -| Plus[intro]: "\R a b; rel_multiset' R M N\ \ rel_multiset' R (M + {#a#}) (N + {#b#})" - -lemma map_multiset_Zero_iff[simp]: "mmap f M = {#} \ M = {#}" -by (metis image_is_empty multiset.set_map set_of_eq_empty_iff) - -lemma map_multiset_Zero[simp]: "mmap f {#} = {#}" by simp - -lemma rel_multiset_Zero: "rel_multiset R {#} {#}" -unfolding rel_multiset_def Grp_def by auto + rel: rel_mset +proof - + show "image_mset id = id" + by (rule image_mset.id) +next + show "\f g. image_mset (g \ f) = image_mset g \ image_mset f" + unfolding comp_def by (rule ext) (simp add: image_mset.compositionality comp_def) +next + fix X :: "'a multiset" + show "\f g. (\z. z \ set_of X \ f z = g z) \ image_mset f X = image_mset g X" + by (induct X, (simp (no_asm))+, + metis One_nat_def Un_iff count_single mem_set_of_iff set_of_union zero_less_Suc) +next + show "\f. set_of \ image_mset f = op ` f \ set_of" + by auto +next + show "card_order natLeq" + by (rule natLeq_card_order) +next + show "BNF_Cardinal_Arithmetic.cinfinite natLeq" + by (rule natLeq_cinfinite) +next + show "\X. ordLeq3 (card_of (set_of X)) natLeq" + by transfer + (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def) +next + show "\R S. rel_mset R OO rel_mset S \ rel_mset (R OO S)" + unfolding rel_mset_def[abs_def] OO_def + apply clarify + apply (rename_tac X Z Y xs ys' ys zs) + apply (drule_tac xs = ys' and ys = zs and xs' = ys in list_all2_reorder_left_invariance) + by (auto intro: list_all2_trans) +next + show "\R. rel_mset R = + (BNF_Def.Grp {x. set_of x \ {(x, y). R x y}} (image_mset fst))\\ OO + BNF_Def.Grp {x. set_of x \ {(x, y). R x y}} (image_mset snd)" + unfolding rel_mset_def[abs_def] BNF_Def.Grp_def OO_def + apply (rule ext)+ + apply auto + apply (rule_tac x = "multiset_of (zip xs ys)" in exI) + apply auto[1] + apply (metis list_all2_lengthD map_fst_zip multiset_of_map) + apply (auto simp: list_all2_iff)[1] + apply (metis list_all2_lengthD map_snd_zip multiset_of_map) + apply (auto simp: list_all2_iff)[1] + apply (rename_tac XY) + apply (cut_tac X = XY in ex_multiset_of) + apply (erule exE) + apply (rename_tac xys) + apply (rule_tac x = "map fst xys" in exI) + apply (auto simp: multiset_of_map) + apply (rule_tac x = "map snd xys" in exI) + by (auto simp: multiset_of_map list_all2I subset_eq zip_map_fst_snd) +next + show "\z. z \ set_of {#} \ False" + by auto +qed + +inductive rel_mset' where + Zero[intro]: "rel_mset' R {#} {#}" +| Plus[intro]: "\R a b; rel_mset' R M N\ \ rel_mset' R (M + {#a#}) (N + {#b#})" + +lemma rel_mset_Zero: "rel_mset R {#} {#}" +unfolding rel_mset_def Grp_def by auto declare multiset.count[simp] declare Abs_multiset_inverse[simp] declare multiset.count_inverse[simp] declare union_preserves_multiset[simp] -lemma map_multiset_Plus[simp]: "mmap f (M1 + M2) = mmap f M1 + mmap f M2" -proof (intro multiset_eqI, transfer fixing: f) - fix x :: 'a and M1 M2 :: "'b \ nat" - assume "M1 \ multiset" "M2 \ multiset" - hence "setsum M1 {a. f a = x \ 0 < M1 a} = setsum M1 {a. f a = x \ 0 < M1 a + M2 a}" - "setsum M2 {a. f a = x \ 0 < M2 a} = setsum M2 {a. f a = x \ 0 < M1 a + M2 a}" - by (auto simp: multiset_def intro!: setsum.mono_neutral_cong_left) - then show "(\a | f a = x \ 0 < M1 a + M2 a. M1 a + M2 a) = - setsum M1 {a. f a = x \ 0 < M1 a} + - setsum M2 {a. f a = x \ 0 < M2 a}" - by (auto simp: setsum.distrib[symmetric]) -qed - -lemma map_multiset_single[simp]: "mmap f {#a#} = {#f a#}" - by transfer auto - -lemma rel_multiset_Plus: -assumes ab: "R a b" and MN: "rel_multiset R M N" -shows "rel_multiset R (M + {#a#}) (N + {#b#})" +lemma rel_mset_Plus: +assumes ab: "R a b" and MN: "rel_mset R M N" +shows "rel_mset R (M + {#a#}) (N + {#b#})" proof- {fix y assume "R a b" and "set_of y \ {(x, y). R x y}" - hence "\ya. mmap fst y + {#a#} = mmap fst ya \ - mmap snd y + {#b#} = mmap snd ya \ + hence "\ya. image_mset fst y + {#a#} = image_mset fst ya \ + image_mset snd y + {#b#} = image_mset snd ya \ set_of ya \ {(x, y). R x y}" apply(intro exI[of _ "y + {#(a,b)#}"]) by auto } thus ?thesis using assms - unfolding rel_multiset_def Grp_def by force + unfolding multiset.rel_compp_Grp Grp_def by blast qed -lemma rel_multiset'_imp_rel_multiset: -"rel_multiset' R M N \ rel_multiset R M N" -apply(induct rule: rel_multiset'.induct) -using rel_multiset_Zero rel_multiset_Plus by auto - -lemma mcard_mmap[simp]: "mcard (mmap f M) = mcard M" -proof - - def A \ "\ b. {a. f a = b \ a \# M}" - let ?B = "{b. 0 < setsum (count M) (A b)}" - have "{b. \a. f a = b \ a \# M} \ f ` {a. a \# M}" by auto - moreover have "finite (f ` {a. a \# M})" apply(rule finite_imageI) - using finite_Collect_mem . - ultimately have fin: "finite {b. \a. f a = b \ a \# M}" by(rule finite_subset) - have i: "inj_on A ?B" unfolding inj_on_def A_def apply clarsimp - by (metis (lifting, full_types) mem_Collect_eq neq0_conv setsum.neutral) - have 0: "\ b. 0 < setsum (count M) (A b) \ (\ a \ A b. count M a > 0)" - apply safe - apply (metis less_not_refl setsum_gt_0_iff setsum.infinite) - by (metis A_def finite_Collect_conjI finite_Collect_mem setsum_gt_0_iff) - hence AB: "A ` ?B = {A b | b. \ a \ A b. count M a > 0}" by auto - - have "setsum (\ x. setsum (count M) (A x)) ?B = setsum (setsum (count M) o A) ?B" - unfolding comp_def .. - also have "... = (\x\ A ` ?B. setsum (count M) x)" - unfolding setsum.reindex [OF i, symmetric] .. - also have "... = setsum (count M) (\x\A ` {b. 0 < setsum (count M) (A b)}. x)" - (is "_ = setsum (count M) ?J") - apply(rule setsum.UNION_disjoint[symmetric]) - using 0 fin unfolding A_def by auto - also have "?J = {a. a \# M}" unfolding AB unfolding A_def by auto - finally have "setsum (\ x. setsum (count M) (A x)) ?B = - setsum (count M) {a. a \# M}" . - then show ?thesis unfolding mcard_unfold_setsum A_def by transfer -qed - -lemma rel_multiset_mcard: -assumes "rel_multiset R M N" -shows "mcard M = mcard N" -using assms unfolding rel_multiset_def Grp_def by auto +lemma rel_mset'_imp_rel_mset: +"rel_mset' R M N \ rel_mset R M N" +apply(induct rule: rel_mset'.induct) +using rel_mset_Zero rel_mset_Plus by auto + +lemma mcard_image_mset[simp]: "mcard (image_mset f M) = mcard M" + unfolding size_eq_mcard[symmetric] by (rule size_image_mset) + +lemma rel_mset_mcard: + assumes "rel_mset R M N" + shows "mcard M = mcard N" +using assms unfolding multiset.rel_compp_Grp Grp_def by auto lemma multiset_induct2[case_names empty addL addR]: assumes empty: "P {#} {#}" @@ -2946,100 +2463,96 @@ qed lemma msed_map_invL: -assumes "mmap f (M + {#a#}) = N" -shows "\ N1. N = N1 + {#f a#} \ mmap f M = N1" +assumes "image_mset f (M + {#a#}) = N" +shows "\N1. N = N1 + {#f a#} \ image_mset f M = N1" proof- have "f a \# N" using assms multiset.set_map[of f "M + {#a#}"] by auto then obtain N1 where N: "N = N1 + {#f a#}" using multi_member_split by metis - have "mmap f M = N1" using assms unfolding N by simp + have "image_mset f M = N1" using assms unfolding N by simp thus ?thesis using N by blast qed lemma msed_map_invR: -assumes "mmap f M = N + {#b#}" -shows "\ M1 a. M = M1 + {#a#} \ f a = b \ mmap f M1 = N" +assumes "image_mset f M = N + {#b#}" +shows "\M1 a. M = M1 + {#a#} \ f a = b \ image_mset f M1 = N" proof- obtain a where a: "a \# M" and fa: "f a = b" using multiset.set_map[of f M] unfolding assms by (metis image_iff mem_set_of_iff union_single_eq_member) then obtain M1 where M: "M = M1 + {#a#}" using multi_member_split by metis - have "mmap f M1 = N" using assms unfolding M fa[symmetric] by simp + have "image_mset f M1 = N" using assms unfolding M fa[symmetric] by simp thus ?thesis using M fa by blast qed lemma msed_rel_invL: -assumes "rel_multiset R (M + {#a#}) N" -shows "\ N1 b. N = N1 + {#b#} \ R a b \ rel_multiset R M N1" +assumes "rel_mset R (M + {#a#}) N" +shows "\N1 b. N = N1 + {#b#} \ R a b \ rel_mset R M N1" proof- - obtain K where KM: "mmap fst K = M + {#a#}" - and KN: "mmap snd K = N" and sK: "set_of K \ {(a, b). R a b}" + obtain K where KM: "image_mset fst K = M + {#a#}" + and KN: "image_mset snd K = N" and sK: "set_of K \ {(a, b). R a b}" using assms - unfolding rel_multiset_def Grp_def by auto + unfolding multiset.rel_compp_Grp Grp_def by auto obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a" - and K1M: "mmap fst K1 = M" using msed_map_invR[OF KM] by auto - obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "mmap snd K1 = N1" + and K1M: "image_mset fst K1 = M" using msed_map_invR[OF KM] by auto + obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "image_mset snd K1 = N1" using msed_map_invL[OF KN[unfolded K]] by auto have Rab: "R a (snd ab)" using sK a unfolding K by auto - have "rel_multiset R M N1" using sK K1M K1N1 - unfolding K rel_multiset_def Grp_def by auto + have "rel_mset R M N1" using sK K1M K1N1 + unfolding K multiset.rel_compp_Grp Grp_def by auto thus ?thesis using N Rab by auto qed lemma msed_rel_invR: -assumes "rel_multiset R M (N + {#b#})" -shows "\ M1 a. M = M1 + {#a#} \ R a b \ rel_multiset R M1 N" +assumes "rel_mset R M (N + {#b#})" +shows "\M1 a. M = M1 + {#a#} \ R a b \ rel_mset R M1 N" proof- - obtain K where KN: "mmap snd K = N + {#b#}" - and KM: "mmap fst K = M" and sK: "set_of K \ {(a, b). R a b}" + obtain K where KN: "image_mset snd K = N + {#b#}" + and KM: "image_mset fst K = M" and sK: "set_of K \ {(a, b). R a b}" using assms - unfolding rel_multiset_def Grp_def by auto + unfolding multiset.rel_compp_Grp Grp_def by auto obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b" - and K1N: "mmap snd K1 = N" using msed_map_invR[OF KN] by auto - obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "mmap fst K1 = M1" + and K1N: "image_mset snd K1 = N" using msed_map_invR[OF KN] by auto + obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "image_mset fst K1 = M1" using msed_map_invL[OF KM[unfolded K]] by auto have Rab: "R (fst ab) b" using sK b unfolding K by auto - have "rel_multiset R M1 N" using sK K1N K1M1 - unfolding K rel_multiset_def Grp_def by auto + have "rel_mset R M1 N" using sK K1N K1M1 + unfolding K multiset.rel_compp_Grp Grp_def by auto thus ?thesis using M Rab by auto qed -lemma rel_multiset_imp_rel_multiset': -assumes "rel_multiset R M N" -shows "rel_multiset' R M N" +lemma rel_mset_imp_rel_mset': +assumes "rel_mset R M N" +shows "rel_mset' R M N" using assms proof(induct M arbitrary: N rule: measure_induct_rule[of mcard]) case (less M) - have c: "mcard M = mcard N" using rel_multiset_mcard[OF less.prems] . + have c: "mcard M = mcard N" using rel_mset_mcard[OF less.prems] . show ?case proof(cases "M = {#}") case True hence "N = {#}" using c by simp - thus ?thesis using True rel_multiset'.Zero by auto + thus ?thesis using True rel_mset'.Zero by auto next case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split) - obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "rel_multiset R M1 N1" + obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "rel_mset R M1 N1" using msed_rel_invL[OF less.prems[unfolded M]] by auto - have "rel_multiset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp - thus ?thesis using rel_multiset'.Plus[of R a b, OF R] unfolding M N by simp + have "rel_mset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp + thus ?thesis using rel_mset'.Plus[of R a b, OF R] unfolding M N by simp qed qed -lemma rel_multiset_rel_multiset': -"rel_multiset R M N = rel_multiset' R M N" -using rel_multiset_imp_rel_multiset' rel_multiset'_imp_rel_multiset by auto - -(* The main end product for rel_multiset: inductive characterization *) -theorems rel_multiset_induct[case_names empty add, induct pred: rel_multiset] = - rel_multiset'.induct[unfolded rel_multiset_rel_multiset'[symmetric]] +lemma rel_mset_rel_mset': +"rel_mset R M N = rel_mset' R M N" +using rel_mset_imp_rel_mset' rel_mset'_imp_rel_mset by auto + +(* The main end product for rel_mset: inductive characterization *) +theorems rel_mset_induct[case_names empty add, induct pred: rel_mset] = + rel_mset'.induct[unfolded rel_mset_rel_mset'[symmetric]] subsection {* Size setup *} -lemma multiset_size_o_map: "size_multiset g \ mmap f = size_multiset (g \ f)" -apply (rule ext) -apply (unfold o_apply) -apply (induct_tac x) -apply auto -done +lemma multiset_size_o_map: "size_multiset g \ image_mset f = size_multiset (g \ f)" + unfolding o_apply by (rule ext) (induct_tac, auto) setup {* BNF_LFP_Size.register_size_global @{type_name multiset} @{const_name size_multiset}