src/HOL/Library/More_BNFs.thy
changeset 55129 26bd1cba3ab5
parent 55128 6e16d2dd4f14
child 55131 9808f186795c
     1.1 --- a/src/HOL/Library/More_BNFs.thy	Thu Jan 23 19:02:22 2014 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,1050 +0,0 @@
     1.4 -(*  Title:      HOL/Library/More_BNFs.thy
     1.5 -    Author:     Dmitriy Traytel, TU Muenchen
     1.6 -    Author:     Andrei Popescu, TU Muenchen
     1.7 -    Author:     Andreas Lochbihler, Karlsruhe Institute of Technology
     1.8 -    Author:     Jasmin Blanchette, TU Muenchen
     1.9 -    Copyright   2012
    1.10 -
    1.11 -Registration of various types as bounded natural functors.
    1.12 -*)
    1.13 -
    1.14 -header {* Registration of Various Types as Bounded Natural Functors *}
    1.15 -
    1.16 -theory More_BNFs
    1.17 -imports FSet Multiset Cardinal_Notations
    1.18 -begin
    1.19 -
    1.20 -abbreviation "Grp \<equiv> BNF_Util.Grp"
    1.21 -abbreviation "fstOp \<equiv> BNF_Def.fstOp"
    1.22 -abbreviation "sndOp \<equiv> BNF_Def.sndOp"
    1.23 -
    1.24 -lemma option_rec_conv_option_case: "option_rec = option_case"
    1.25 -by (simp add: fun_eq_iff split: option.split)
    1.26 -
    1.27 -bnf "'a option"
    1.28 -  map: Option.map
    1.29 -  sets: Option.set
    1.30 -  bd: natLeq 
    1.31 -  wits: None
    1.32 -  rel: option_rel
    1.33 -proof -
    1.34 -  show "Option.map id = id" by (simp add: fun_eq_iff Option.map_def split: option.split)
    1.35 -next
    1.36 -  fix f g
    1.37 -  show "Option.map (g \<circ> f) = Option.map g \<circ> Option.map f"
    1.38 -    by (auto simp add: fun_eq_iff Option.map_def split: option.split)
    1.39 -next
    1.40 -  fix f g x
    1.41 -  assume "\<And>z. z \<in> Option.set x \<Longrightarrow> f z = g z"
    1.42 -  thus "Option.map f x = Option.map g x"
    1.43 -    by (simp cong: Option.map_cong)
    1.44 -next
    1.45 -  fix f
    1.46 -  show "Option.set \<circ> Option.map f = op ` f \<circ> Option.set"
    1.47 -    by fastforce
    1.48 -next
    1.49 -  show "card_order natLeq" by (rule natLeq_card_order)
    1.50 -next
    1.51 -  show "cinfinite natLeq" by (rule natLeq_cinfinite)
    1.52 -next
    1.53 -  fix x
    1.54 -  show "|Option.set x| \<le>o natLeq"
    1.55 -    by (cases x) (simp_all add: ordLess_imp_ordLeq finite_iff_ordLess_natLeq[symmetric])
    1.56 -next
    1.57 -  fix R S
    1.58 -  show "option_rel R OO option_rel S \<le> option_rel (R OO S)"
    1.59 -    by (auto simp: option_rel_def split: option.splits)
    1.60 -next
    1.61 -  fix z
    1.62 -  assume "z \<in> Option.set None"
    1.63 -  thus False by simp
    1.64 -next
    1.65 -  fix R
    1.66 -  show "option_rel R =
    1.67 -        (Grp {x. Option.set x \<subseteq> Collect (split R)} (Option.map fst))\<inverse>\<inverse> OO
    1.68 -         Grp {x. Option.set x \<subseteq> Collect (split R)} (Option.map snd)"
    1.69 -  unfolding option_rel_def Grp_def relcompp.simps conversep.simps fun_eq_iff prod.cases
    1.70 -  by (auto simp: trans[OF eq_commute option_map_is_None] trans[OF eq_commute option_map_eq_Some]
    1.71 -           split: option.splits)
    1.72 -qed
    1.73 -
    1.74 -bnf "'a list"
    1.75 -  map: map
    1.76 -  sets: set
    1.77 -  bd: natLeq
    1.78 -  wits: Nil
    1.79 -  rel: list_all2
    1.80 -proof -
    1.81 -  show "map id = id" by (rule List.map.id)
    1.82 -next
    1.83 -  fix f g
    1.84 -  show "map (g o f) = map g o map f" by (rule List.map.comp[symmetric])
    1.85 -next
    1.86 -  fix x f g
    1.87 -  assume "\<And>z. z \<in> set x \<Longrightarrow> f z = g z"
    1.88 -  thus "map f x = map g x" by simp
    1.89 -next
    1.90 -  fix f
    1.91 -  show "set o map f = image f o set" by (rule ext, unfold comp_apply, rule set_map)
    1.92 -next
    1.93 -  show "card_order natLeq" by (rule natLeq_card_order)
    1.94 -next
    1.95 -  show "cinfinite natLeq" by (rule natLeq_cinfinite)
    1.96 -next
    1.97 -  fix x
    1.98 -  show "|set x| \<le>o natLeq"
    1.99 -    by (metis List.finite_set finite_iff_ordLess_natLeq ordLess_imp_ordLeq)
   1.100 -next
   1.101 -  fix R S
   1.102 -  show "list_all2 R OO list_all2 S \<le> list_all2 (R OO S)"
   1.103 -    by (metis list_all2_OO order_refl)
   1.104 -next
   1.105 -  fix R
   1.106 -  show "list_all2 R =
   1.107 -         (Grp {x. set x \<subseteq> {(x, y). R x y}} (map fst))\<inverse>\<inverse> OO
   1.108 -         Grp {x. set x \<subseteq> {(x, y). R x y}} (map snd)"
   1.109 -    unfolding list_all2_def[abs_def] Grp_def fun_eq_iff relcompp.simps conversep.simps
   1.110 -    by (force simp: zip_map_fst_snd)
   1.111 -qed simp_all
   1.112 -
   1.113 -
   1.114 -(* Finite sets *)
   1.115 -
   1.116 -context
   1.117 -includes fset.lifting
   1.118 -begin
   1.119 -
   1.120 -lemma fset_rel_alt: "fset_rel R a b \<longleftrightarrow> (\<forall>t \<in> fset a. \<exists>u \<in> fset b. R t u) \<and>
   1.121 -                                        (\<forall>t \<in> fset b. \<exists>u \<in> fset a. R u t)"
   1.122 -  by transfer (simp add: set_rel_def)
   1.123 -
   1.124 -lemma fset_to_fset: "finite A \<Longrightarrow> fset (the_inv fset A) = A"
   1.125 -  apply (rule f_the_inv_into_f[unfolded inj_on_def])
   1.126 -  apply (simp add: fset_inject) apply (rule range_eqI Abs_fset_inverse[symmetric] CollectI)+
   1.127 -  .
   1.128 -
   1.129 -lemma fset_rel_aux:
   1.130 -"(\<forall>t \<in> fset a. \<exists>u \<in> fset b. R t u) \<and> (\<forall>u \<in> fset b. \<exists>t \<in> fset a. R t u) \<longleftrightarrow>
   1.131 - ((Grp {a. fset a \<subseteq> {(a, b). R a b}} (fimage fst))\<inverse>\<inverse> OO
   1.132 -  Grp {a. fset a \<subseteq> {(a, b). R a b}} (fimage snd)) a b" (is "?L = ?R")
   1.133 -proof
   1.134 -  assume ?L
   1.135 -  def R' \<equiv> "the_inv fset (Collect (split R) \<inter> (fset a \<times> fset b))" (is "the_inv fset ?L'")
   1.136 -  have "finite ?L'" by (intro finite_Int[OF disjI2] finite_cartesian_product) (transfer, simp)+
   1.137 -  hence *: "fset R' = ?L'" unfolding R'_def by (intro fset_to_fset)
   1.138 -  show ?R unfolding Grp_def relcompp.simps conversep.simps
   1.139 -  proof (intro CollectI prod_caseI exI[of _ a] exI[of _ b] exI[of _ R'] conjI refl)
   1.140 -    from * show "a = fimage fst R'" using conjunct1[OF `?L`]
   1.141 -      by (transfer, auto simp add: image_def Int_def split: prod.splits)
   1.142 -    from * show "b = fimage snd R'" using conjunct2[OF `?L`]
   1.143 -      by (transfer, auto simp add: image_def Int_def split: prod.splits)
   1.144 -  qed (auto simp add: *)
   1.145 -next
   1.146 -  assume ?R thus ?L unfolding Grp_def relcompp.simps conversep.simps
   1.147 -  apply (simp add: subset_eq Ball_def)
   1.148 -  apply (rule conjI)
   1.149 -  apply (transfer, clarsimp, metis snd_conv)
   1.150 -  by (transfer, clarsimp, metis fst_conv)
   1.151 -qed
   1.152 -
   1.153 -bnf "'a fset"
   1.154 -  map: fimage
   1.155 -  sets: fset 
   1.156 -  bd: natLeq
   1.157 -  wits: "{||}"
   1.158 -  rel: fset_rel
   1.159 -apply -
   1.160 -          apply transfer' apply simp
   1.161 -         apply transfer' apply force
   1.162 -        apply transfer apply force
   1.163 -       apply transfer' apply force
   1.164 -      apply (rule natLeq_card_order)
   1.165 -     apply (rule natLeq_cinfinite)
   1.166 -    apply transfer apply (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq)
   1.167 -   apply (fastforce simp: fset_rel_alt)
   1.168 - apply (simp add: Grp_def relcompp.simps conversep.simps fun_eq_iff fset_rel_alt fset_rel_aux) 
   1.169 -apply transfer apply simp
   1.170 -done
   1.171 -
   1.172 -lemma fset_rel_fset: "set_rel \<chi> (fset A1) (fset A2) = fset_rel \<chi> A1 A2"
   1.173 -  by transfer (rule refl)
   1.174 -
   1.175 -end
   1.176 -
   1.177 -lemmas [simp] = fset.map_comp fset.map_id fset.set_map
   1.178 -
   1.179 -
   1.180 -(* Multisets *)
   1.181 -
   1.182 -lemma setsum_gt_0_iff:
   1.183 -fixes f :: "'a \<Rightarrow> nat" assumes "finite A"
   1.184 -shows "setsum f A > 0 \<longleftrightarrow> (\<exists> a \<in> A. f a > 0)"
   1.185 -(is "?L \<longleftrightarrow> ?R")
   1.186 -proof-
   1.187 -  have "?L \<longleftrightarrow> \<not> setsum f A = 0" by fast
   1.188 -  also have "... \<longleftrightarrow> (\<exists> a \<in> A. f a \<noteq> 0)" using assms by simp
   1.189 -  also have "... \<longleftrightarrow> ?R" by simp
   1.190 -  finally show ?thesis .
   1.191 -qed
   1.192 -
   1.193 -lift_definition mmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" is
   1.194 -  "\<lambda>h f b. setsum f {a. h a = b \<and> f a > 0} :: nat"
   1.195 -unfolding multiset_def proof safe
   1.196 -  fix h :: "'a \<Rightarrow> 'b" and f :: "'a \<Rightarrow> nat"
   1.197 -  assume fin: "finite {a. 0 < f a}"  (is "finite ?A")
   1.198 -  show "finite {b. 0 < setsum f {a. h a = b \<and> 0 < f a}}"
   1.199 -  (is "finite {b. 0 < setsum f (?As b)}")
   1.200 -  proof- let ?B = "{b. 0 < setsum f (?As b)}"
   1.201 -    have "\<And> b. finite (?As b)" using fin by simp
   1.202 -    hence B: "?B = {b. ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
   1.203 -    hence "?B \<subseteq> h ` ?A" by auto
   1.204 -    thus ?thesis using finite_surj[OF fin] by auto
   1.205 -  qed
   1.206 -qed
   1.207 -
   1.208 -lemma mmap_id0: "mmap id = id"
   1.209 -proof (intro ext multiset_eqI)
   1.210 -  fix f a show "count (mmap id f) a = count (id f) a"
   1.211 -  proof (cases "count f a = 0")
   1.212 -    case False
   1.213 -    hence 1: "{aa. aa = a \<and> aa \<in># f} = {a}" by auto
   1.214 -    thus ?thesis by transfer auto
   1.215 -  qed (transfer, simp)
   1.216 -qed
   1.217 -
   1.218 -lemma inj_on_setsum_inv:
   1.219 -assumes 1: "(0::nat) < setsum (count f) {a. h a = b' \<and> a \<in># f}" (is "0 < setsum (count f) ?A'")
   1.220 -and     2: "{a. h a = b \<and> a \<in># f} = {a. h a = b' \<and> a \<in># f}" (is "?A = ?A'")
   1.221 -shows "b = b'"
   1.222 -using assms by (auto simp add: setsum_gt_0_iff)
   1.223 -
   1.224 -lemma mmap_comp:
   1.225 -fixes h1 :: "'a \<Rightarrow> 'b" and h2 :: "'b \<Rightarrow> 'c"
   1.226 -shows "mmap (h2 o h1) = mmap h2 o mmap h1"
   1.227 -proof (intro ext multiset_eqI)
   1.228 -  fix f :: "'a multiset" fix c :: 'c
   1.229 -  let ?A = "{a. h2 (h1 a) = c \<and> a \<in># f}"
   1.230 -  let ?As = "\<lambda> b. {a. h1 a = b \<and> a \<in># f}"
   1.231 -  let ?B = "{b. h2 b = c \<and> 0 < setsum (count f) (?As b)}"
   1.232 -  have 0: "{?As b | b.  b \<in> ?B} = ?As ` ?B" by auto
   1.233 -  have "\<And> b. finite (?As b)" by transfer (simp add: multiset_def)
   1.234 -  hence "?B = {b. h2 b = c \<and> ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
   1.235 -  hence A: "?A = \<Union> {?As b | b.  b \<in> ?B}" by auto
   1.236 -  have "setsum (count f) ?A = setsum (setsum (count f)) {?As b | b.  b \<in> ?B}"
   1.237 -    unfolding A by transfer (intro setsum_Union_disjoint, auto simp: multiset_def)
   1.238 -  also have "... = setsum (setsum (count f)) (?As ` ?B)" unfolding 0 ..
   1.239 -  also have "... = setsum (setsum (count f) o ?As) ?B"
   1.240 -    by(intro setsum_reindex) (auto simp add: setsum_gt_0_iff inj_on_def)
   1.241 -  also have "... = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" unfolding comp_def ..
   1.242 -  finally have "setsum (count f) ?A = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" .
   1.243 -  thus "count (mmap (h2 \<circ> h1) f) c = count ((mmap h2 \<circ> mmap h1) f) c"
   1.244 -    by transfer (unfold comp_apply, blast)
   1.245 -qed
   1.246 -
   1.247 -lemma mmap_cong:
   1.248 -assumes "\<And>a. a \<in># M \<Longrightarrow> f a = g a"
   1.249 -shows "mmap f M = mmap g M"
   1.250 -using assms by transfer (auto intro!: setsum_cong)
   1.251 -
   1.252 -context
   1.253 -begin
   1.254 -interpretation lifting_syntax .
   1.255 -
   1.256 -lemma set_of_transfer[transfer_rule]: "(pcr_multiset op = ===> op =) (\<lambda>f. {a. 0 < f a}) set_of"
   1.257 -  unfolding set_of_def pcr_multiset_def cr_multiset_def fun_rel_def by auto
   1.258 -
   1.259 -end
   1.260 -
   1.261 -lemma set_of_mmap: "set_of o mmap h = image h o set_of"
   1.262 -proof (rule ext, unfold comp_apply)
   1.263 -  fix M show "set_of (mmap h M) = h ` set_of M"
   1.264 -    by transfer (auto simp add: multiset_def setsum_gt_0_iff)
   1.265 -qed
   1.266 -
   1.267 -lemma multiset_of_surj:
   1.268 -  "multiset_of ` {as. set as \<subseteq> A} = {M. set_of M \<subseteq> A}"
   1.269 -proof safe
   1.270 -  fix M assume M: "set_of M \<subseteq> A"
   1.271 -  obtain as where eq: "M = multiset_of as" using surj_multiset_of unfolding surj_def by auto
   1.272 -  hence "set as \<subseteq> A" using M by auto
   1.273 -  thus "M \<in> multiset_of ` {as. set as \<subseteq> A}" using eq by auto
   1.274 -next
   1.275 -  show "\<And>x xa xb. \<lbrakk>set xa \<subseteq> A; xb \<in> set_of (multiset_of xa)\<rbrakk> \<Longrightarrow> xb \<in> A"
   1.276 -  by (erule set_mp) (unfold set_of_multiset_of)
   1.277 -qed
   1.278 -
   1.279 -lemma card_of_set_of:
   1.280 -"|{M. set_of M \<subseteq> A}| \<le>o |{as. set as \<subseteq> A}|"
   1.281 -apply(rule card_of_ordLeqI2[of _ multiset_of]) using multiset_of_surj by auto
   1.282 -
   1.283 -lemma nat_sum_induct:
   1.284 -assumes "\<And>n1 n2. (\<And> m1 m2. m1 + m2 < n1 + n2 \<Longrightarrow> phi m1 m2) \<Longrightarrow> phi n1 n2"
   1.285 -shows "phi (n1::nat) (n2::nat)"
   1.286 -proof-
   1.287 -  let ?chi = "\<lambda> n1n2 :: nat * nat. phi (fst n1n2) (snd n1n2)"
   1.288 -  have "?chi (n1,n2)"
   1.289 -  apply(induct rule: measure_induct[of "\<lambda> n1n2. fst n1n2 + snd n1n2" ?chi])
   1.290 -  using assms by (metis fstI sndI)
   1.291 -  thus ?thesis by simp
   1.292 -qed
   1.293 -
   1.294 -lemma matrix_count:
   1.295 -fixes ct1 ct2 :: "nat \<Rightarrow> nat"
   1.296 -assumes "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
   1.297 -shows
   1.298 -"\<exists> ct. (\<forall> i1 \<le> n1. setsum (\<lambda> i2. ct i1 i2) {..<Suc n2} = ct1 i1) \<and>
   1.299 -       (\<forall> i2 \<le> n2. setsum (\<lambda> i1. ct i1 i2) {..<Suc n1} = ct2 i2)"
   1.300 -(is "?phi ct1 ct2 n1 n2")
   1.301 -proof-
   1.302 -  have "\<forall> ct1 ct2 :: nat \<Rightarrow> nat.
   1.303 -        setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} \<longrightarrow> ?phi ct1 ct2 n1 n2"
   1.304 -  proof(induct rule: nat_sum_induct[of
   1.305 -"\<lambda> n1 n2. \<forall> ct1 ct2 :: nat \<Rightarrow> nat.
   1.306 -     setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} \<longrightarrow> ?phi ct1 ct2 n1 n2"],
   1.307 -      clarify)
   1.308 -  fix n1 n2 :: nat and ct1 ct2 :: "nat \<Rightarrow> nat"
   1.309 -  assume IH: "\<And> m1 m2. m1 + m2 < n1 + n2 \<Longrightarrow>
   1.310 -                \<forall> dt1 dt2 :: nat \<Rightarrow> nat.
   1.311 -                setsum dt1 {..<Suc m1} = setsum dt2 {..<Suc m2} \<longrightarrow> ?phi dt1 dt2 m1 m2"
   1.312 -  and ss: "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
   1.313 -  show "?phi ct1 ct2 n1 n2"
   1.314 -  proof(cases n1)
   1.315 -    case 0 note n1 = 0
   1.316 -    show ?thesis
   1.317 -    proof(cases n2)
   1.318 -      case 0 note n2 = 0
   1.319 -      let ?ct = "\<lambda> i1 i2. ct2 0"
   1.320 -      show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by simp
   1.321 -    next
   1.322 -      case (Suc m2) note n2 = Suc
   1.323 -      let ?ct = "\<lambda> i1 i2. ct2 i2"
   1.324 -      show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
   1.325 -    qed
   1.326 -  next
   1.327 -    case (Suc m1) note n1 = Suc
   1.328 -    show ?thesis
   1.329 -    proof(cases n2)
   1.330 -      case 0 note n2 = 0
   1.331 -      let ?ct = "\<lambda> i1 i2. ct1 i1"
   1.332 -      show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
   1.333 -    next
   1.334 -      case (Suc m2) note n2 = Suc
   1.335 -      show ?thesis
   1.336 -      proof(cases "ct1 n1 \<le> ct2 n2")
   1.337 -        case True
   1.338 -        def dt2 \<equiv> "\<lambda> i2. if i2 = n2 then ct2 i2 - ct1 n1 else ct2 i2"
   1.339 -        have "setsum ct1 {..<Suc m1} = setsum dt2 {..<Suc n2}"
   1.340 -        unfolding dt2_def using ss n1 True by auto
   1.341 -        hence "?phi ct1 dt2 m1 n2" using IH[of m1 n2] n1 by simp
   1.342 -        then obtain dt where
   1.343 -        1: "\<And> i1. i1 \<le> m1 \<Longrightarrow> setsum (\<lambda> i2. dt i1 i2) {..<Suc n2} = ct1 i1" and
   1.344 -        2: "\<And> i2. i2 \<le> n2 \<Longrightarrow> setsum (\<lambda> i1. dt i1 i2) {..<Suc m1} = dt2 i2" by auto
   1.345 -        let ?ct = "\<lambda> i1 i2. if i1 = n1 then (if i2 = n2 then ct1 n1 else 0)
   1.346 -                                       else dt i1 i2"
   1.347 -        show ?thesis apply(rule exI[of _ ?ct])
   1.348 -        using n1 n2 1 2 True unfolding dt2_def by simp
   1.349 -      next
   1.350 -        case False
   1.351 -        hence False: "ct2 n2 < ct1 n1" by simp
   1.352 -        def dt1 \<equiv> "\<lambda> i1. if i1 = n1 then ct1 i1 - ct2 n2 else ct1 i1"
   1.353 -        have "setsum dt1 {..<Suc n1} = setsum ct2 {..<Suc m2}"
   1.354 -        unfolding dt1_def using ss n2 False by auto
   1.355 -        hence "?phi dt1 ct2 n1 m2" using IH[of n1 m2] n2 by simp
   1.356 -        then obtain dt where
   1.357 -        1: "\<And> i1. i1 \<le> n1 \<Longrightarrow> setsum (\<lambda> i2. dt i1 i2) {..<Suc m2} = dt1 i1" and
   1.358 -        2: "\<And> i2. i2 \<le> m2 \<Longrightarrow> setsum (\<lambda> i1. dt i1 i2) {..<Suc n1} = ct2 i2" by force
   1.359 -        let ?ct = "\<lambda> i1 i2. if i2 = n2 then (if i1 = n1 then ct2 n2 else 0)
   1.360 -                                       else dt i1 i2"
   1.361 -        show ?thesis apply(rule exI[of _ ?ct])
   1.362 -        using n1 n2 1 2 False unfolding dt1_def by simp
   1.363 -      qed
   1.364 -    qed
   1.365 -  qed
   1.366 -  qed
   1.367 -  thus ?thesis using assms by auto
   1.368 -qed
   1.369 -
   1.370 -definition
   1.371 -"inj2 u B1 B2 \<equiv>
   1.372 - \<forall> b1 b1' b2 b2'. {b1,b1'} \<subseteq> B1 \<and> {b2,b2'} \<subseteq> B2 \<and> u b1 b2 = u b1' b2'
   1.373 -                  \<longrightarrow> b1 = b1' \<and> b2 = b2'"
   1.374 -
   1.375 -lemma matrix_setsum_finite:
   1.376 -assumes B1: "B1 \<noteq> {}" "finite B1" and B2: "B2 \<noteq> {}" "finite B2" and u: "inj2 u B1 B2"
   1.377 -and ss: "setsum N1 B1 = setsum N2 B2"
   1.378 -shows "\<exists> M :: 'a \<Rightarrow> nat.
   1.379 -            (\<forall> b1 \<in> B1. setsum (\<lambda> b2. M (u b1 b2)) B2 = N1 b1) \<and>
   1.380 -            (\<forall> b2 \<in> B2. setsum (\<lambda> b1. M (u b1 b2)) B1 = N2 b2)"
   1.381 -proof-
   1.382 -  obtain n1 where "card B1 = Suc n1" using B1 by (metis card_insert finite.simps)
   1.383 -  then obtain e1 where e1: "bij_betw e1 {..<Suc n1} B1"
   1.384 -  using ex_bij_betw_finite_nat[OF B1(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
   1.385 -  hence e1_inj: "inj_on e1 {..<Suc n1}" and e1_surj: "e1 ` {..<Suc n1} = B1"
   1.386 -  unfolding bij_betw_def by auto
   1.387 -  def f1 \<equiv> "inv_into {..<Suc n1} e1"
   1.388 -  have f1: "bij_betw f1 B1 {..<Suc n1}"
   1.389 -  and f1e1[simp]: "\<And> i1. i1 < Suc n1 \<Longrightarrow> f1 (e1 i1) = i1"
   1.390 -  and e1f1[simp]: "\<And> b1. b1 \<in> B1 \<Longrightarrow> e1 (f1 b1) = b1" unfolding f1_def
   1.391 -  apply (metis bij_betw_inv_into e1, metis bij_betw_inv_into_left e1 lessThan_iff)
   1.392 -  by (metis e1_surj f_inv_into_f)
   1.393 -  (*  *)
   1.394 -  obtain n2 where "card B2 = Suc n2" using B2 by (metis card_insert finite.simps)
   1.395 -  then obtain e2 where e2: "bij_betw e2 {..<Suc n2} B2"
   1.396 -  using ex_bij_betw_finite_nat[OF B2(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
   1.397 -  hence e2_inj: "inj_on e2 {..<Suc n2}" and e2_surj: "e2 ` {..<Suc n2} = B2"
   1.398 -  unfolding bij_betw_def by auto
   1.399 -  def f2 \<equiv> "inv_into {..<Suc n2} e2"
   1.400 -  have f2: "bij_betw f2 B2 {..<Suc n2}"
   1.401 -  and f2e2[simp]: "\<And> i2. i2 < Suc n2 \<Longrightarrow> f2 (e2 i2) = i2"
   1.402 -  and e2f2[simp]: "\<And> b2. b2 \<in> B2 \<Longrightarrow> e2 (f2 b2) = b2" unfolding f2_def
   1.403 -  apply (metis bij_betw_inv_into e2, metis bij_betw_inv_into_left e2 lessThan_iff)
   1.404 -  by (metis e2_surj f_inv_into_f)
   1.405 -  (*  *)
   1.406 -  let ?ct1 = "N1 o e1"  let ?ct2 = "N2 o e2"
   1.407 -  have ss: "setsum ?ct1 {..<Suc n1} = setsum ?ct2 {..<Suc n2}"
   1.408 -  unfolding setsum_reindex[OF e1_inj, symmetric] setsum_reindex[OF e2_inj, symmetric]
   1.409 -  e1_surj e2_surj using ss .
   1.410 -  obtain ct where
   1.411 -  ct1: "\<And> i1. i1 \<le> n1 \<Longrightarrow> setsum (\<lambda> i2. ct i1 i2) {..<Suc n2} = ?ct1 i1" and
   1.412 -  ct2: "\<And> i2. i2 \<le> n2 \<Longrightarrow> setsum (\<lambda> i1. ct i1 i2) {..<Suc n1} = ?ct2 i2"
   1.413 -  using matrix_count[OF ss] by blast
   1.414 -  (*  *)
   1.415 -  def A \<equiv> "{u b1 b2 | b1 b2. b1 \<in> B1 \<and> b2 \<in> B2}"
   1.416 -  have "\<forall> a \<in> A. \<exists> b1b2 \<in> B1 <*> B2. u (fst b1b2) (snd b1b2) = a"
   1.417 -  unfolding A_def Ball_def mem_Collect_eq by auto
   1.418 -  then obtain h1h2 where h12:
   1.419 -  "\<And>a. a \<in> A \<Longrightarrow> u (fst (h1h2 a)) (snd (h1h2 a)) = a \<and> h1h2 a \<in> B1 <*> B2" by metis
   1.420 -  def h1 \<equiv> "fst o h1h2"  def h2 \<equiv> "snd o h1h2"
   1.421 -  have h12[simp]: "\<And>a. a \<in> A \<Longrightarrow> u (h1 a) (h2 a) = a"
   1.422 -                  "\<And> a. a \<in> A \<Longrightarrow> h1 a \<in> B1"  "\<And> a. a \<in> A \<Longrightarrow> h2 a \<in> B2"
   1.423 -  using h12 unfolding h1_def h2_def by force+
   1.424 -  {fix b1 b2 assume b1: "b1 \<in> B1" and b2: "b2 \<in> B2"
   1.425 -   hence inA: "u b1 b2 \<in> A" unfolding A_def by auto
   1.426 -   hence "u b1 b2 = u (h1 (u b1 b2)) (h2 (u b1 b2))" by auto
   1.427 -   moreover have "h1 (u b1 b2) \<in> B1" "h2 (u b1 b2) \<in> B2" using inA by auto
   1.428 -   ultimately have "h1 (u b1 b2) = b1 \<and> h2 (u b1 b2) = b2"
   1.429 -   using u b1 b2 unfolding inj2_def by fastforce
   1.430 -  }
   1.431 -  hence h1[simp]: "\<And> b1 b2. \<lbrakk>b1 \<in> B1; b2 \<in> B2\<rbrakk> \<Longrightarrow> h1 (u b1 b2) = b1" and
   1.432 -        h2[simp]: "\<And> b1 b2. \<lbrakk>b1 \<in> B1; b2 \<in> B2\<rbrakk> \<Longrightarrow> h2 (u b1 b2) = b2" by auto
   1.433 -  def M \<equiv> "\<lambda> a. ct (f1 (h1 a)) (f2 (h2 a))"
   1.434 -  show ?thesis
   1.435 -  apply(rule exI[of _ M]) proof safe
   1.436 -    fix b1 assume b1: "b1 \<in> B1"
   1.437 -    hence f1b1: "f1 b1 \<le> n1" using f1 unfolding bij_betw_def
   1.438 -    by (metis image_eqI lessThan_iff less_Suc_eq_le)
   1.439 -    have "(\<Sum>b2\<in>B2. M (u b1 b2)) = (\<Sum>i2<Suc n2. ct (f1 b1) (f2 (e2 i2)))"
   1.440 -    unfolding e2_surj[symmetric] setsum_reindex[OF e2_inj]
   1.441 -    unfolding M_def comp_def apply(intro setsum_cong) apply force
   1.442 -    by (metis e2_surj b1 h1 h2 imageI)
   1.443 -    also have "... = N1 b1" using b1 ct1[OF f1b1] by simp
   1.444 -    finally show "(\<Sum>b2\<in>B2. M (u b1 b2)) = N1 b1" .
   1.445 -  next
   1.446 -    fix b2 assume b2: "b2 \<in> B2"
   1.447 -    hence f2b2: "f2 b2 \<le> n2" using f2 unfolding bij_betw_def
   1.448 -    by (metis image_eqI lessThan_iff less_Suc_eq_le)
   1.449 -    have "(\<Sum>b1\<in>B1. M (u b1 b2)) = (\<Sum>i1<Suc n1. ct (f1 (e1 i1)) (f2 b2))"
   1.450 -    unfolding e1_surj[symmetric] setsum_reindex[OF e1_inj]
   1.451 -    unfolding M_def comp_def apply(intro setsum_cong) apply force
   1.452 -    by (metis e1_surj b2 h1 h2 imageI)
   1.453 -    also have "... = N2 b2" using b2 ct2[OF f2b2] by simp
   1.454 -    finally show "(\<Sum>b1\<in>B1. M (u b1 b2)) = N2 b2" .
   1.455 -  qed
   1.456 -qed
   1.457 -
   1.458 -lemma supp_vimage_mmap: "set_of M \<subseteq> f -` (set_of (mmap f M))"
   1.459 -  by transfer (auto simp: multiset_def setsum_gt_0_iff)
   1.460 -
   1.461 -lemma mmap_ge_0: "b \<in># mmap f M \<longleftrightarrow> (\<exists>a. a \<in># M \<and> f a = b)"
   1.462 -  by transfer (auto simp: multiset_def setsum_gt_0_iff)
   1.463 -
   1.464 -lemma finite_twosets:
   1.465 -assumes "finite B1" and "finite B2"
   1.466 -shows "finite {u b1 b2 |b1 b2. b1 \<in> B1 \<and> b2 \<in> B2}"  (is "finite ?A")
   1.467 -proof-
   1.468 -  have A: "?A = (\<lambda> b1b2. u (fst b1b2) (snd b1b2)) ` (B1 <*> B2)" by force
   1.469 -  show ?thesis unfolding A using finite_cartesian_product[OF assms] by auto
   1.470 -qed
   1.471 -
   1.472 -(* Weak pullbacks: *)
   1.473 -definition wpull where
   1.474 -"wpull A B1 B2 f1 f2 p1 p2 \<longleftrightarrow>
   1.475 - (\<forall> b1 b2. b1 \<in> B1 \<and> b2 \<in> B2 \<and> f1 b1 = f2 b2 \<longrightarrow> (\<exists> a \<in> A. p1 a = b1 \<and> p2 a = b2))"
   1.476 -
   1.477 -(* Weak pseudo-pullbacks *)
   1.478 -definition wppull where
   1.479 -"wppull A B1 B2 f1 f2 e1 e2 p1 p2 \<longleftrightarrow>
   1.480 - (\<forall> b1 b2. b1 \<in> B1 \<and> b2 \<in> B2 \<and> f1 b1 = f2 b2 \<longrightarrow>
   1.481 -           (\<exists> a \<in> A. e1 (p1 a) = e1 b1 \<and> e2 (p2 a) = e2 b2))"
   1.482 -
   1.483 -
   1.484 -(* The pullback of sets *)
   1.485 -definition thePull where
   1.486 -"thePull B1 B2 f1 f2 = {(b1,b2). b1 \<in> B1 \<and> b2 \<in> B2 \<and> f1 b1 = f2 b2}"
   1.487 -
   1.488 -lemma wpull_thePull:
   1.489 -"wpull (thePull B1 B2 f1 f2) B1 B2 f1 f2 fst snd"
   1.490 -unfolding wpull_def thePull_def by auto
   1.491 -
   1.492 -lemma wppull_thePull:
   1.493 -assumes "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
   1.494 -shows
   1.495 -"\<exists> j. \<forall> a' \<in> thePull B1 B2 f1 f2.
   1.496 -   j a' \<in> A \<and>
   1.497 -   e1 (p1 (j a')) = e1 (fst a') \<and> e2 (p2 (j a')) = e2 (snd a')"
   1.498 -(is "\<exists> j. \<forall> a' \<in> ?A'. ?phi a' (j a')")
   1.499 -proof(rule bchoice[of ?A' ?phi], default)
   1.500 -  fix a' assume a': "a' \<in> ?A'"
   1.501 -  hence "fst a' \<in> B1" unfolding thePull_def by auto
   1.502 -  moreover
   1.503 -  from a' have "snd a' \<in> B2" unfolding thePull_def by auto
   1.504 -  moreover have "f1 (fst a') = f2 (snd a')"
   1.505 -  using a' unfolding csquare_def thePull_def by auto
   1.506 -  ultimately show "\<exists> ja'. ?phi a' ja'"
   1.507 -  using assms unfolding wppull_def by blast
   1.508 -qed
   1.509 -
   1.510 -lemma wpull_wppull:
   1.511 -assumes wp: "wpull A' B1 B2 f1 f2 p1' p2'" and
   1.512 -1: "\<forall> a' \<in> A'. j a' \<in> A \<and> e1 (p1 (j a')) = e1 (p1' a') \<and> e2 (p2 (j a')) = e2 (p2' a')"
   1.513 -shows "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
   1.514 -unfolding wppull_def proof safe
   1.515 -  fix b1 b2
   1.516 -  assume b1: "b1 \<in> B1" and b2: "b2 \<in> B2" and f: "f1 b1 = f2 b2"
   1.517 -  then obtain a' where a': "a' \<in> A'" and b1: "b1 = p1' a'" and b2: "b2 = p2' a'"
   1.518 -  using wp unfolding wpull_def by blast
   1.519 -  show "\<exists>a\<in>A. e1 (p1 a) = e1 b1 \<and> e2 (p2 a) = e2 b2"
   1.520 -  apply (rule bexI[of _ "j a'"]) unfolding b1 b2 using a' 1 by auto
   1.521 -qed
   1.522 -
   1.523 -lemma wppull_fstOp_sndOp:
   1.524 -shows "wppull (Collect (split (P OO Q))) (Collect (split P)) (Collect (split Q))
   1.525 -  snd fst fst snd (fstOp P Q) (sndOp P Q)"
   1.526 -using pick_middlep unfolding wppull_def fstOp_def sndOp_def relcompp.simps by auto
   1.527 -
   1.528 -lemma wpull_mmap:
   1.529 -fixes A :: "'a set" and B1 :: "'b1 set" and B2 :: "'b2 set"
   1.530 -assumes wp: "wpull A B1 B2 f1 f2 p1 p2"
   1.531 -shows
   1.532 -"wpull {M. set_of M \<subseteq> A}
   1.533 -       {N1. set_of N1 \<subseteq> B1} {N2. set_of N2 \<subseteq> B2}
   1.534 -       (mmap f1) (mmap f2) (mmap p1) (mmap p2)"
   1.535 -unfolding wpull_def proof (safe, unfold Bex_def mem_Collect_eq)
   1.536 -  fix N1 :: "'b1 multiset" and N2 :: "'b2 multiset"
   1.537 -  assume mmap': "mmap f1 N1 = mmap f2 N2"
   1.538 -  and N1[simp]: "set_of N1 \<subseteq> B1"
   1.539 -  and N2[simp]: "set_of N2 \<subseteq> B2"
   1.540 -  def P \<equiv> "mmap f1 N1"
   1.541 -  have P1: "P = mmap f1 N1" and P2: "P = mmap f2 N2" unfolding P_def using mmap' by auto
   1.542 -  note P = P1 P2
   1.543 -  have fin_N1[simp]: "finite (set_of N1)"
   1.544 -   and fin_N2[simp]: "finite (set_of N2)"
   1.545 -   and fin_P[simp]: "finite (set_of P)" by auto
   1.546 -  (*  *)
   1.547 -  def set1 \<equiv> "\<lambda> c. {b1 \<in> set_of N1. f1 b1 = c}"
   1.548 -  have set1[simp]: "\<And> c b1. b1 \<in> set1 c \<Longrightarrow> f1 b1 = c" unfolding set1_def by auto
   1.549 -  have fin_set1: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (set1 c)"
   1.550 -    using N1(1) unfolding set1_def multiset_def by auto
   1.551 -  have set1_NE: "\<And> c. c \<in> set_of P \<Longrightarrow> set1 c \<noteq> {}"
   1.552 -   unfolding set1_def set_of_def P mmap_ge_0 by auto
   1.553 -  have supp_N1_set1: "set_of N1 = (\<Union> c \<in> set_of P. set1 c)"
   1.554 -    using supp_vimage_mmap[of N1 f1] unfolding set1_def P1 by auto
   1.555 -  hence set1_inclN1: "\<And>c. c \<in> set_of P \<Longrightarrow> set1 c \<subseteq> set_of N1" by auto
   1.556 -  hence set1_incl: "\<And> c. c \<in> set_of P \<Longrightarrow> set1 c \<subseteq> B1" using N1 by blast
   1.557 -  have set1_disj: "\<And> c c'. c \<noteq> c' \<Longrightarrow> set1 c \<inter> set1 c' = {}"
   1.558 -    unfolding set1_def by auto
   1.559 -  have setsum_set1: "\<And> c. setsum (count N1) (set1 c) = count P c"
   1.560 -    unfolding P1 set1_def by transfer (auto intro: setsum_cong)
   1.561 -  (*  *)
   1.562 -  def set2 \<equiv> "\<lambda> c. {b2 \<in> set_of N2. f2 b2 = c}"
   1.563 -  have set2[simp]: "\<And> c b2. b2 \<in> set2 c \<Longrightarrow> f2 b2 = c" unfolding set2_def by auto
   1.564 -  have fin_set2: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (set2 c)"
   1.565 -  using N2(1) unfolding set2_def multiset_def by auto
   1.566 -  have set2_NE: "\<And> c. c \<in> set_of P \<Longrightarrow> set2 c \<noteq> {}"
   1.567 -    unfolding set2_def P2 mmap_ge_0 set_of_def by auto
   1.568 -  have supp_N2_set2: "set_of N2 = (\<Union> c \<in> set_of P. set2 c)"
   1.569 -    using supp_vimage_mmap[of N2 f2] unfolding set2_def P2 by auto
   1.570 -  hence set2_inclN2: "\<And>c. c \<in> set_of P \<Longrightarrow> set2 c \<subseteq> set_of N2" by auto
   1.571 -  hence set2_incl: "\<And> c. c \<in> set_of P \<Longrightarrow> set2 c \<subseteq> B2" using N2 by blast
   1.572 -  have set2_disj: "\<And> c c'. c \<noteq> c' \<Longrightarrow> set2 c \<inter> set2 c' = {}"
   1.573 -    unfolding set2_def by auto
   1.574 -  have setsum_set2: "\<And> c. setsum (count N2) (set2 c) = count P c"
   1.575 -    unfolding P2 set2_def by transfer (auto intro: setsum_cong)
   1.576 -  (*  *)
   1.577 -  have ss: "\<And> c. c \<in> set_of P \<Longrightarrow> setsum (count N1) (set1 c) = setsum (count N2) (set2 c)"
   1.578 -    unfolding setsum_set1 setsum_set2 ..
   1.579 -  have "\<forall> c \<in> set_of P. \<forall> b1b2 \<in> (set1 c) \<times> (set2 c).
   1.580 -          \<exists> a \<in> A. p1 a = fst b1b2 \<and> p2 a = snd b1b2"
   1.581 -    using wp set1_incl set2_incl unfolding wpull_def Ball_def mem_Collect_eq
   1.582 -    by simp (metis set1 set2 set_rev_mp)
   1.583 -  then obtain uu where uu:
   1.584 -  "\<forall> c \<in> set_of P. \<forall> b1b2 \<in> (set1 c) \<times> (set2 c).
   1.585 -     uu c b1b2 \<in> A \<and> p1 (uu c b1b2) = fst b1b2 \<and> p2 (uu c b1b2) = snd b1b2" by metis
   1.586 -  def u \<equiv> "\<lambda> c b1 b2. uu c (b1,b2)"
   1.587 -  have u[simp]:
   1.588 -  "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> u c b1 b2 \<in> A"
   1.589 -  "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> p1 (u c b1 b2) = b1"
   1.590 -  "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> p2 (u c b1 b2) = b2"
   1.591 -    using uu unfolding u_def by auto
   1.592 -  {fix c assume c: "c \<in> set_of P"
   1.593 -   have "inj2 (u c) (set1 c) (set2 c)" unfolding inj2_def proof clarify
   1.594 -     fix b1 b1' b2 b2'
   1.595 -     assume "{b1, b1'} \<subseteq> set1 c" "{b2, b2'} \<subseteq> set2 c" and 0: "u c b1 b2 = u c b1' b2'"
   1.596 -     hence "p1 (u c b1 b2) = b1 \<and> p2 (u c b1 b2) = b2 \<and>
   1.597 -            p1 (u c b1' b2') = b1' \<and> p2 (u c b1' b2') = b2'"
   1.598 -     using u(2)[OF c] u(3)[OF c] by simp metis
   1.599 -     thus "b1 = b1' \<and> b2 = b2'" using 0 by auto
   1.600 -   qed
   1.601 -  } note inj = this
   1.602 -  def sset \<equiv> "\<lambda> c. {u c b1 b2 | b1 b2. b1 \<in> set1 c \<and> b2 \<in> set2 c}"
   1.603 -  have fin_sset[simp]: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (sset c)" unfolding sset_def
   1.604 -    using fin_set1 fin_set2 finite_twosets by blast
   1.605 -  have sset_A: "\<And> c. c \<in> set_of P \<Longrightarrow> sset c \<subseteq> A" unfolding sset_def by auto
   1.606 -  {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
   1.607 -   then obtain b1 b2 where b1: "b1 \<in> set1 c" and b2: "b2 \<in> set2 c"
   1.608 -   and a: "a = u c b1 b2" unfolding sset_def by auto
   1.609 -   have "p1 a \<in> set1 c" and p2a: "p2 a \<in> set2 c"
   1.610 -   using ac a b1 b2 c u(2) u(3) by simp+
   1.611 -   hence "u c (p1 a) (p2 a) = a" unfolding a using b1 b2 inj[OF c]
   1.612 -   unfolding inj2_def by (metis c u(2) u(3))
   1.613 -  } note u_p12[simp] = this
   1.614 -  {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
   1.615 -   hence "p1 a \<in> set1 c" unfolding sset_def by auto
   1.616 -  }note p1[simp] = this
   1.617 -  {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
   1.618 -   hence "p2 a \<in> set2 c" unfolding sset_def by auto
   1.619 -  }note p2[simp] = this
   1.620 -  (*  *)
   1.621 -  {fix c assume c: "c \<in> set_of P"
   1.622 -   hence "\<exists> M. (\<forall> b1 \<in> set1 c. setsum (\<lambda> b2. M (u c b1 b2)) (set2 c) = count N1 b1) \<and>
   1.623 -               (\<forall> b2 \<in> set2 c. setsum (\<lambda> b1. M (u c b1 b2)) (set1 c) = count N2 b2)"
   1.624 -   unfolding sset_def
   1.625 -   using matrix_setsum_finite[OF set1_NE[OF c] fin_set1[OF c]
   1.626 -                                 set2_NE[OF c] fin_set2[OF c] inj[OF c] ss[OF c]] by auto
   1.627 -  }
   1.628 -  then obtain Ms where
   1.629 -  ss1: "\<And> c b1. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c\<rbrakk> \<Longrightarrow>
   1.630 -                   setsum (\<lambda> b2. Ms c (u c b1 b2)) (set2 c) = count N1 b1" and
   1.631 -  ss2: "\<And> c b2. \<lbrakk>c \<in> set_of P; b2 \<in> set2 c\<rbrakk> \<Longrightarrow>
   1.632 -                   setsum (\<lambda> b1. Ms c (u c b1 b2)) (set1 c) = count N2 b2"
   1.633 -  by metis
   1.634 -  def SET \<equiv> "\<Union> c \<in> set_of P. sset c"
   1.635 -  have fin_SET[simp]: "finite SET" unfolding SET_def apply(rule finite_UN_I) by auto
   1.636 -  have SET_A: "SET \<subseteq> A" unfolding SET_def using sset_A by blast
   1.637 -  have u_SET[simp]: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> u c b1 b2 \<in> SET"
   1.638 -    unfolding SET_def sset_def by blast
   1.639 -  {fix c a assume c: "c \<in> set_of P" and a: "a \<in> SET" and p1a: "p1 a \<in> set1 c"
   1.640 -   then obtain c' where c': "c' \<in> set_of P" and ac': "a \<in> sset c'"
   1.641 -    unfolding SET_def by auto
   1.642 -   hence "p1 a \<in> set1 c'" unfolding sset_def by auto
   1.643 -   hence eq: "c = c'" using p1a c c' set1_disj by auto
   1.644 -   hence "a \<in> sset c" using ac' by simp
   1.645 -  } note p1_rev = this
   1.646 -  {fix c a assume c: "c \<in> set_of P" and a: "a \<in> SET" and p2a: "p2 a \<in> set2 c"
   1.647 -   then obtain c' where c': "c' \<in> set_of P" and ac': "a \<in> sset c'"
   1.648 -   unfolding SET_def by auto
   1.649 -   hence "p2 a \<in> set2 c'" unfolding sset_def by auto
   1.650 -   hence eq: "c = c'" using p2a c c' set2_disj by auto
   1.651 -   hence "a \<in> sset c" using ac' by simp
   1.652 -  } note p2_rev = this
   1.653 -  (*  *)
   1.654 -  have "\<forall> a \<in> SET. \<exists> c \<in> set_of P. a \<in> sset c" unfolding SET_def by auto
   1.655 -  then obtain h where h: "\<forall> a \<in> SET. h a \<in> set_of P \<and> a \<in> sset (h a)" by metis
   1.656 -  have h_u[simp]: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   1.657 -                      \<Longrightarrow> h (u c b1 b2) = c"
   1.658 -  by (metis h p2 set2 u(3) u_SET)
   1.659 -  have h_u1: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   1.660 -                      \<Longrightarrow> h (u c b1 b2) = f1 b1"
   1.661 -  using h unfolding sset_def by auto
   1.662 -  have h_u2: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   1.663 -                      \<Longrightarrow> h (u c b1 b2) = f2 b2"
   1.664 -  using h unfolding sset_def by auto
   1.665 -  def M \<equiv>
   1.666 -    "Abs_multiset (\<lambda> a. if a \<in> SET \<and> p1 a \<in> set_of N1 \<and> p2 a \<in> set_of N2 then Ms (h a) a else 0)"
   1.667 -  have "(\<lambda> a. if a \<in> SET \<and> p1 a \<in> set_of N1 \<and> p2 a \<in> set_of N2 then Ms (h a) a else 0) \<in> multiset"
   1.668 -    unfolding multiset_def by auto
   1.669 -  hence [transfer_rule]: "pcr_multiset op = (\<lambda> a. if a \<in> SET \<and> p1 a \<in> set_of N1 \<and> p2 a \<in> set_of N2 then Ms (h a) a else 0) M"
   1.670 -    unfolding M_def pcr_multiset_def cr_multiset_def by (auto simp: Abs_multiset_inverse)
   1.671 -  have sM: "set_of M \<subseteq> SET" "set_of M \<subseteq> p1 -` (set_of N1)" "set_of M \<subseteq> p2 -` set_of N2"
   1.672 -    by (transfer, auto split: split_if_asm)+
   1.673 -  show "\<exists>M. set_of M \<subseteq> A \<and> mmap p1 M = N1 \<and> mmap p2 M = N2"
   1.674 -  proof(rule exI[of _ M], safe)
   1.675 -    fix a assume *: "a \<in> set_of M"
   1.676 -    from SET_A show "a \<in> A"
   1.677 -    proof (cases "a \<in> SET")
   1.678 -      case False thus ?thesis using * by transfer' auto
   1.679 -    qed blast
   1.680 -  next
   1.681 -    show "mmap p1 M = N1"
   1.682 -    proof(intro multiset_eqI)
   1.683 -      fix b1
   1.684 -      let ?K = "{a. p1 a = b1 \<and> a \<in># M}"
   1.685 -      have "setsum (count M) ?K = count N1 b1"
   1.686 -      proof(cases "b1 \<in> set_of N1")
   1.687 -        case False
   1.688 -        hence "?K = {}" using sM(2) by auto
   1.689 -        thus ?thesis using False by auto
   1.690 -      next
   1.691 -        case True
   1.692 -        def c \<equiv> "f1 b1"
   1.693 -        have c: "c \<in> set_of P" and b1: "b1 \<in> set1 c"
   1.694 -          unfolding set1_def c_def P1 using True by (auto simp: comp_eq_dest[OF set_of_mmap])
   1.695 -        with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p1 a = b1 \<and> a \<in> SET}"
   1.696 -          by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
   1.697 -        also have "... = setsum (count M) ((\<lambda> b2. u c b1 b2) ` (set2 c))"
   1.698 -          apply(rule setsum_cong) using c b1 proof safe
   1.699 -          fix a assume p1a: "p1 a \<in> set1 c" and "c \<in> set_of P" and "a \<in> SET"
   1.700 -          hence ac: "a \<in> sset c" using p1_rev by auto
   1.701 -          hence "a = u c (p1 a) (p2 a)" using c by auto
   1.702 -          moreover have "p2 a \<in> set2 c" using ac c by auto
   1.703 -          ultimately show "a \<in> u c (p1 a) ` set2 c" by auto
   1.704 -        qed auto
   1.705 -        also have "... = setsum (\<lambda> b2. count M (u c b1 b2)) (set2 c)"
   1.706 -          unfolding comp_def[symmetric] apply(rule setsum_reindex)
   1.707 -          using inj unfolding inj_on_def inj2_def using b1 c u(3) by blast
   1.708 -        also have "... = count N1 b1" unfolding ss1[OF c b1, symmetric]
   1.709 -          apply(rule setsum_cong[OF refl]) apply (transfer fixing: Ms u c b1 set2)
   1.710 -          using True h_u[OF c b1] set2_def u(2,3)[OF c b1] u_SET[OF c b1] by fastforce
   1.711 -        finally show ?thesis .
   1.712 -      qed
   1.713 -      thus "count (mmap p1 M) b1 = count N1 b1" by transfer
   1.714 -    qed
   1.715 -  next
   1.716 -next
   1.717 -    show "mmap p2 M = N2"
   1.718 -    proof(intro multiset_eqI)
   1.719 -      fix b2
   1.720 -      let ?K = "{a. p2 a = b2 \<and> a \<in># M}"
   1.721 -      have "setsum (count M) ?K = count N2 b2"
   1.722 -      proof(cases "b2 \<in> set_of N2")
   1.723 -        case False
   1.724 -        hence "?K = {}" using sM(3) by auto
   1.725 -        thus ?thesis using False by auto
   1.726 -      next
   1.727 -        case True
   1.728 -        def c \<equiv> "f2 b2"
   1.729 -        have c: "c \<in> set_of P" and b2: "b2 \<in> set2 c"
   1.730 -          unfolding set2_def c_def P2 using True by (auto simp: comp_eq_dest[OF set_of_mmap])
   1.731 -        with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p2 a = b2 \<and> a \<in> SET}"
   1.732 -          by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
   1.733 -        also have "... = setsum (count M) ((\<lambda> b1. u c b1 b2) ` (set1 c))"
   1.734 -          apply(rule setsum_cong) using c b2 proof safe
   1.735 -          fix a assume p2a: "p2 a \<in> set2 c" and "c \<in> set_of P" and "a \<in> SET"
   1.736 -          hence ac: "a \<in> sset c" using p2_rev by auto
   1.737 -          hence "a = u c (p1 a) (p2 a)" using c by auto
   1.738 -          moreover have "p1 a \<in> set1 c" using ac c by auto
   1.739 -          ultimately show "a \<in> (\<lambda>x. u c x (p2 a)) ` set1 c" by auto
   1.740 -        qed auto
   1.741 -        also have "... = setsum (count M o (\<lambda> b1. u c b1 b2)) (set1 c)"
   1.742 -          apply(rule setsum_reindex)
   1.743 -          using inj unfolding inj_on_def inj2_def using b2 c u(2) by blast
   1.744 -        also have "... = setsum (\<lambda> b1. count M (u c b1 b2)) (set1 c)" by simp
   1.745 -        also have "... = count N2 b2" unfolding ss2[OF c b2, symmetric] comp_def
   1.746 -          apply(rule setsum_cong[OF refl]) apply (transfer fixing: Ms u c b2 set1)
   1.747 -          using True h_u1[OF c _ b2] u(2,3)[OF c _ b2] u_SET[OF c _ b2] set1_def by fastforce
   1.748 -        finally show ?thesis .
   1.749 -      qed
   1.750 -      thus "count (mmap p2 M) b2 = count N2 b2" by transfer
   1.751 -    qed
   1.752 -  qed
   1.753 -qed
   1.754 -
   1.755 -lemma set_of_bd: "|set_of x| \<le>o natLeq"
   1.756 -  by transfer
   1.757 -    (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def)
   1.758 -
   1.759 -lemma wppull_mmap:
   1.760 -  assumes "wppull A B1 B2 f1 f2 e1 e2 p1 p2"
   1.761 -  shows "wppull {M. set_of M \<subseteq> A} {N1. set_of N1 \<subseteq> B1} {N2. set_of N2 \<subseteq> B2}
   1.762 -    (mmap f1) (mmap f2) (mmap e1) (mmap e2) (mmap p1) (mmap p2)"
   1.763 -proof -
   1.764 -  from assms obtain j where j: "\<forall>a'\<in>thePull B1 B2 f1 f2.
   1.765 -    j a' \<in> A \<and> e1 (p1 (j a')) = e1 (fst a') \<and> e2 (p2 (j a')) = e2 (snd a')" 
   1.766 -    by (blast dest: wppull_thePull)
   1.767 -  then show ?thesis
   1.768 -    by (intro wpull_wppull[OF wpull_mmap[OF wpull_thePull], of _ _ _ _ "mmap j"])
   1.769 -      (auto simp: comp_eq_dest_lhs[OF mmap_comp[symmetric]] comp_eq_dest[OF set_of_mmap]
   1.770 -        intro!: mmap_cong simp del: mem_set_of_iff simp: mem_set_of_iff[symmetric])
   1.771 -qed
   1.772 -
   1.773 -bnf "'a multiset"
   1.774 -  map: mmap
   1.775 -  sets: set_of 
   1.776 -  bd: natLeq
   1.777 -  wits: "{#}"
   1.778 -by (auto simp add: mmap_id0 mmap_comp set_of_mmap natLeq_card_order natLeq_cinfinite set_of_bd
   1.779 -  Grp_def relcompp.simps intro: mmap_cong)
   1.780 -  (metis wppull_mmap[OF wppull_fstOp_sndOp, unfolded wppull_def
   1.781 -    o_eq_dest_lhs[OF mmap_comp[symmetric]] fstOp_def sndOp_def comp_def, simplified])
   1.782 -
   1.783 -inductive rel_multiset' where
   1.784 -  Zero[intro]: "rel_multiset' R {#} {#}"
   1.785 -| Plus[intro]: "\<lbrakk>R a b; rel_multiset' R M N\<rbrakk> \<Longrightarrow> rel_multiset' R (M + {#a#}) (N + {#b#})"
   1.786 -
   1.787 -lemma map_multiset_Zero_iff[simp]: "mmap f M = {#} \<longleftrightarrow> M = {#}"
   1.788 -by (metis image_is_empty multiset.set_map set_of_eq_empty_iff)
   1.789 -
   1.790 -lemma map_multiset_Zero[simp]: "mmap f {#} = {#}" by simp
   1.791 -
   1.792 -lemma rel_multiset_Zero: "rel_multiset R {#} {#}"
   1.793 -unfolding rel_multiset_def Grp_def by auto
   1.794 -
   1.795 -declare multiset.count[simp]
   1.796 -declare Abs_multiset_inverse[simp]
   1.797 -declare multiset.count_inverse[simp]
   1.798 -declare union_preserves_multiset[simp]
   1.799 -
   1.800 -
   1.801 -lemma map_multiset_Plus[simp]: "mmap f (M1 + M2) = mmap f M1 + mmap f M2"
   1.802 -proof (intro multiset_eqI, transfer fixing: f)
   1.803 -  fix x :: 'a and M1 M2 :: "'b \<Rightarrow> nat"
   1.804 -  assume "M1 \<in> multiset" "M2 \<in> multiset"
   1.805 -  hence "setsum M1 {a. f a = x \<and> 0 < M1 a} = setsum M1 {a. f a = x \<and> 0 < M1 a + M2 a}"
   1.806 -        "setsum M2 {a. f a = x \<and> 0 < M2 a} = setsum M2 {a. f a = x \<and> 0 < M1 a + M2 a}"
   1.807 -    by (auto simp: multiset_def intro!: setsum_mono_zero_cong_left)
   1.808 -  then show "(\<Sum>a | f a = x \<and> 0 < M1 a + M2 a. M1 a + M2 a) =
   1.809 -       setsum M1 {a. f a = x \<and> 0 < M1 a} +
   1.810 -       setsum M2 {a. f a = x \<and> 0 < M2 a}"
   1.811 -    by (auto simp: setsum.distrib[symmetric])
   1.812 -qed
   1.813 -
   1.814 -lemma map_multiset_singl[simp]: "mmap f {#a#} = {#f a#}"
   1.815 -  by transfer auto
   1.816 -
   1.817 -lemma rel_multiset_Plus:
   1.818 -assumes ab: "R a b" and MN: "rel_multiset R M N"
   1.819 -shows "rel_multiset R (M + {#a#}) (N + {#b#})"
   1.820 -proof-
   1.821 -  {fix y assume "R a b" and "set_of y \<subseteq> {(x, y). R x y}"
   1.822 -   hence "\<exists>ya. mmap fst y + {#a#} = mmap fst ya \<and>
   1.823 -               mmap snd y + {#b#} = mmap snd ya \<and>
   1.824 -               set_of ya \<subseteq> {(x, y). R x y}"
   1.825 -   apply(intro exI[of _ "y + {#(a,b)#}"]) by auto
   1.826 -  }
   1.827 -  thus ?thesis
   1.828 -  using assms
   1.829 -  unfolding rel_multiset_def Grp_def by force
   1.830 -qed
   1.831 -
   1.832 -lemma rel_multiset'_imp_rel_multiset:
   1.833 -"rel_multiset' R M N \<Longrightarrow> rel_multiset R M N"
   1.834 -apply(induct rule: rel_multiset'.induct)
   1.835 -using rel_multiset_Zero rel_multiset_Plus by auto
   1.836 -
   1.837 -lemma mcard_mmap[simp]: "mcard (mmap f M) = mcard M"
   1.838 -proof -
   1.839 -  def A \<equiv> "\<lambda> b. {a. f a = b \<and> a \<in># M}"
   1.840 -  let ?B = "{b. 0 < setsum (count M) (A b)}"
   1.841 -  have "{b. \<exists>a. f a = b \<and> a \<in># M} \<subseteq> f ` {a. a \<in># M}" by auto
   1.842 -  moreover have "finite (f ` {a. a \<in># M})" apply(rule finite_imageI)
   1.843 -  using finite_Collect_mem .
   1.844 -  ultimately have fin: "finite {b. \<exists>a. f a = b \<and> a \<in># M}" by(rule finite_subset)
   1.845 -  have i: "inj_on A ?B" unfolding inj_on_def A_def apply clarsimp
   1.846 -    by (metis (lifting, full_types) mem_Collect_eq neq0_conv setsum.neutral)
   1.847 -  have 0: "\<And> b. 0 < setsum (count M) (A b) \<longleftrightarrow> (\<exists> a \<in> A b. count M a > 0)"
   1.848 -  apply safe
   1.849 -    apply (metis less_not_refl setsum_gt_0_iff setsum_infinite)
   1.850 -    by (metis A_def finite_Collect_conjI finite_Collect_mem setsum_gt_0_iff)
   1.851 -  hence AB: "A ` ?B = {A b | b. \<exists> a \<in> A b. count M a > 0}" by auto
   1.852 -
   1.853 -  have "setsum (\<lambda> x. setsum (count M) (A x)) ?B = setsum (setsum (count M) o A) ?B"
   1.854 -  unfolding comp_def ..
   1.855 -  also have "... = (\<Sum>x\<in> A ` ?B. setsum (count M) x)"
   1.856 -  unfolding setsum.reindex [OF i, symmetric] ..
   1.857 -  also have "... = setsum (count M) (\<Union>x\<in>A ` {b. 0 < setsum (count M) (A b)}. x)"
   1.858 -  (is "_ = setsum (count M) ?J")
   1.859 -  apply(rule setsum.UNION_disjoint[symmetric])
   1.860 -  using 0 fin unfolding A_def by auto
   1.861 -  also have "?J = {a. a \<in># M}" unfolding AB unfolding A_def by auto
   1.862 -  finally have "setsum (\<lambda> x. setsum (count M) (A x)) ?B =
   1.863 -                setsum (count M) {a. a \<in># M}" .
   1.864 -  then show ?thesis unfolding mcard_unfold_setsum A_def by transfer
   1.865 -qed
   1.866 -
   1.867 -lemma rel_multiset_mcard:
   1.868 -assumes "rel_multiset R M N"
   1.869 -shows "mcard M = mcard N"
   1.870 -using assms unfolding rel_multiset_def Grp_def by auto
   1.871 -
   1.872 -lemma multiset_induct2[case_names empty addL addR]:
   1.873 -assumes empty: "P {#} {#}"
   1.874 -and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
   1.875 -and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
   1.876 -shows "P M N"
   1.877 -apply(induct N rule: multiset_induct)
   1.878 -  apply(induct M rule: multiset_induct, rule empty, erule addL)
   1.879 -  apply(induct M rule: multiset_induct, erule addR, erule addR)
   1.880 -done
   1.881 -
   1.882 -lemma multiset_induct2_mcard[consumes 1, case_names empty add]:
   1.883 -assumes c: "mcard M = mcard N"
   1.884 -and empty: "P {#} {#}"
   1.885 -and add: "\<And>M N a b. P M N \<Longrightarrow> P (M + {#a#}) (N + {#b#})"
   1.886 -shows "P M N"
   1.887 -using c proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
   1.888 -  case (less M)  show ?case
   1.889 -  proof(cases "M = {#}")
   1.890 -    case True hence "N = {#}" using less.prems by auto
   1.891 -    thus ?thesis using True empty by auto
   1.892 -  next
   1.893 -    case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
   1.894 -    have "N \<noteq> {#}" using False less.prems by auto
   1.895 -    then obtain N1 b where N: "N = N1 + {#b#}" by (metis multi_nonempty_split)
   1.896 -    have "mcard M1 = mcard N1" using less.prems unfolding M N by auto
   1.897 -    thus ?thesis using M N less.hyps add by auto
   1.898 -  qed
   1.899 -qed
   1.900 -
   1.901 -lemma msed_map_invL:
   1.902 -assumes "mmap f (M + {#a#}) = N"
   1.903 -shows "\<exists> N1. N = N1 + {#f a#} \<and> mmap f M = N1"
   1.904 -proof-
   1.905 -  have "f a \<in># N"
   1.906 -  using assms multiset.set_map[of f "M + {#a#}"] by auto
   1.907 -  then obtain N1 where N: "N = N1 + {#f a#}" using multi_member_split by metis
   1.908 -  have "mmap f M = N1" using assms unfolding N by simp
   1.909 -  thus ?thesis using N by blast
   1.910 -qed
   1.911 -
   1.912 -lemma msed_map_invR:
   1.913 -assumes "mmap f M = N + {#b#}"
   1.914 -shows "\<exists> M1 a. M = M1 + {#a#} \<and> f a = b \<and> mmap f M1 = N"
   1.915 -proof-
   1.916 -  obtain a where a: "a \<in># M" and fa: "f a = b"
   1.917 -  using multiset.set_map[of f M] unfolding assms
   1.918 -  by (metis image_iff mem_set_of_iff union_single_eq_member)
   1.919 -  then obtain M1 where M: "M = M1 + {#a#}" using multi_member_split by metis
   1.920 -  have "mmap f M1 = N" using assms unfolding M fa[symmetric] by simp
   1.921 -  thus ?thesis using M fa by blast
   1.922 -qed
   1.923 -
   1.924 -lemma msed_rel_invL:
   1.925 -assumes "rel_multiset R (M + {#a#}) N"
   1.926 -shows "\<exists> N1 b. N = N1 + {#b#} \<and> R a b \<and> rel_multiset R M N1"
   1.927 -proof-
   1.928 -  obtain K where KM: "mmap fst K = M + {#a#}"
   1.929 -  and KN: "mmap snd K = N" and sK: "set_of K \<subseteq> {(a, b). R a b}"
   1.930 -  using assms
   1.931 -  unfolding rel_multiset_def Grp_def by auto
   1.932 -  obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
   1.933 -  and K1M: "mmap fst K1 = M" using msed_map_invR[OF KM] by auto
   1.934 -  obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "mmap snd K1 = N1"
   1.935 -  using msed_map_invL[OF KN[unfolded K]] by auto
   1.936 -  have Rab: "R a (snd ab)" using sK a unfolding K by auto
   1.937 -  have "rel_multiset R M N1" using sK K1M K1N1
   1.938 -  unfolding K rel_multiset_def Grp_def by auto
   1.939 -  thus ?thesis using N Rab by auto
   1.940 -qed
   1.941 -
   1.942 -lemma msed_rel_invR:
   1.943 -assumes "rel_multiset R M (N + {#b#})"
   1.944 -shows "\<exists> M1 a. M = M1 + {#a#} \<and> R a b \<and> rel_multiset R M1 N"
   1.945 -proof-
   1.946 -  obtain K where KN: "mmap snd K = N + {#b#}"
   1.947 -  and KM: "mmap fst K = M" and sK: "set_of K \<subseteq> {(a, b). R a b}"
   1.948 -  using assms
   1.949 -  unfolding rel_multiset_def Grp_def by auto
   1.950 -  obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
   1.951 -  and K1N: "mmap snd K1 = N" using msed_map_invR[OF KN] by auto
   1.952 -  obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "mmap fst K1 = M1"
   1.953 -  using msed_map_invL[OF KM[unfolded K]] by auto
   1.954 -  have Rab: "R (fst ab) b" using sK b unfolding K by auto
   1.955 -  have "rel_multiset R M1 N" using sK K1N K1M1
   1.956 -  unfolding K rel_multiset_def Grp_def by auto
   1.957 -  thus ?thesis using M Rab by auto
   1.958 -qed
   1.959 -
   1.960 -lemma rel_multiset_imp_rel_multiset':
   1.961 -assumes "rel_multiset R M N"
   1.962 -shows "rel_multiset' R M N"
   1.963 -using assms proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
   1.964 -  case (less M)
   1.965 -  have c: "mcard M = mcard N" using rel_multiset_mcard[OF less.prems] .
   1.966 -  show ?case
   1.967 -  proof(cases "M = {#}")
   1.968 -    case True hence "N = {#}" using c by simp
   1.969 -    thus ?thesis using True rel_multiset'.Zero by auto
   1.970 -  next
   1.971 -    case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
   1.972 -    obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "rel_multiset R M1 N1"
   1.973 -    using msed_rel_invL[OF less.prems[unfolded M]] by auto
   1.974 -    have "rel_multiset' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp
   1.975 -    thus ?thesis using rel_multiset'.Plus[of R a b, OF R] unfolding M N by simp
   1.976 -  qed
   1.977 -qed
   1.978 -
   1.979 -lemma rel_multiset_rel_multiset':
   1.980 -"rel_multiset R M N = rel_multiset' R M N"
   1.981 -using  rel_multiset_imp_rel_multiset' rel_multiset'_imp_rel_multiset by auto
   1.982 -
   1.983 -(* The main end product for rel_multiset: inductive characterization *)
   1.984 -theorems rel_multiset_induct[case_names empty add, induct pred: rel_multiset] =
   1.985 -         rel_multiset'.induct[unfolded rel_multiset_rel_multiset'[symmetric]]
   1.986 -
   1.987 -
   1.988 -(* Advanced relator customization *)
   1.989 -
   1.990 -(* Set vs. sum relators: *)
   1.991 -
   1.992 -lemma set_rel_sum_rel[simp]: 
   1.993 -"set_rel (sum_rel \<chi> \<phi>) A1 A2 \<longleftrightarrow> 
   1.994 - set_rel \<chi> (Inl -` A1) (Inl -` A2) \<and> set_rel \<phi> (Inr -` A1) (Inr -` A2)"
   1.995 -(is "?L \<longleftrightarrow> ?Rl \<and> ?Rr")
   1.996 -proof safe
   1.997 -  assume L: "?L"
   1.998 -  show ?Rl unfolding set_rel_def Bex_def vimage_eq proof safe
   1.999 -    fix l1 assume "Inl l1 \<in> A1"
  1.1000 -    then obtain a2 where a2: "a2 \<in> A2" and "sum_rel \<chi> \<phi> (Inl l1) a2"
  1.1001 -    using L unfolding set_rel_def by auto
  1.1002 -    then obtain l2 where "a2 = Inl l2 \<and> \<chi> l1 l2" by (cases a2, auto)
  1.1003 -    thus "\<exists> l2. Inl l2 \<in> A2 \<and> \<chi> l1 l2" using a2 by auto
  1.1004 -  next
  1.1005 -    fix l2 assume "Inl l2 \<in> A2"
  1.1006 -    then obtain a1 where a1: "a1 \<in> A1" and "sum_rel \<chi> \<phi> a1 (Inl l2)"
  1.1007 -    using L unfolding set_rel_def by auto
  1.1008 -    then obtain l1 where "a1 = Inl l1 \<and> \<chi> l1 l2" by (cases a1, auto)
  1.1009 -    thus "\<exists> l1. Inl l1 \<in> A1 \<and> \<chi> l1 l2" using a1 by auto
  1.1010 -  qed
  1.1011 -  show ?Rr unfolding set_rel_def Bex_def vimage_eq proof safe
  1.1012 -    fix r1 assume "Inr r1 \<in> A1"
  1.1013 -    then obtain a2 where a2: "a2 \<in> A2" and "sum_rel \<chi> \<phi> (Inr r1) a2"
  1.1014 -    using L unfolding set_rel_def by auto
  1.1015 -    then obtain r2 where "a2 = Inr r2 \<and> \<phi> r1 r2" by (cases a2, auto)
  1.1016 -    thus "\<exists> r2. Inr r2 \<in> A2 \<and> \<phi> r1 r2" using a2 by auto
  1.1017 -  next
  1.1018 -    fix r2 assume "Inr r2 \<in> A2"
  1.1019 -    then obtain a1 where a1: "a1 \<in> A1" and "sum_rel \<chi> \<phi> a1 (Inr r2)"
  1.1020 -    using L unfolding set_rel_def by auto
  1.1021 -    then obtain r1 where "a1 = Inr r1 \<and> \<phi> r1 r2" by (cases a1, auto)
  1.1022 -    thus "\<exists> r1. Inr r1 \<in> A1 \<and> \<phi> r1 r2" using a1 by auto
  1.1023 -  qed
  1.1024 -next
  1.1025 -  assume Rl: "?Rl" and Rr: "?Rr"
  1.1026 -  show ?L unfolding set_rel_def Bex_def vimage_eq proof safe
  1.1027 -    fix a1 assume a1: "a1 \<in> A1"
  1.1028 -    show "\<exists> a2. a2 \<in> A2 \<and> sum_rel \<chi> \<phi> a1 a2"
  1.1029 -    proof(cases a1)
  1.1030 -      case (Inl l1) then obtain l2 where "Inl l2 \<in> A2 \<and> \<chi> l1 l2"
  1.1031 -      using Rl a1 unfolding set_rel_def by blast
  1.1032 -      thus ?thesis unfolding Inl by auto
  1.1033 -    next
  1.1034 -      case (Inr r1) then obtain r2 where "Inr r2 \<in> A2 \<and> \<phi> r1 r2"
  1.1035 -      using Rr a1 unfolding set_rel_def by blast
  1.1036 -      thus ?thesis unfolding Inr by auto
  1.1037 -    qed
  1.1038 -  next
  1.1039 -    fix a2 assume a2: "a2 \<in> A2"
  1.1040 -    show "\<exists> a1. a1 \<in> A1 \<and> sum_rel \<chi> \<phi> a1 a2"
  1.1041 -    proof(cases a2)
  1.1042 -      case (Inl l2) then obtain l1 where "Inl l1 \<in> A1 \<and> \<chi> l1 l2"
  1.1043 -      using Rl a2 unfolding set_rel_def by blast
  1.1044 -      thus ?thesis unfolding Inl by auto
  1.1045 -    next
  1.1046 -      case (Inr r2) then obtain r1 where "Inr r1 \<in> A1 \<and> \<phi> r1 r2"
  1.1047 -      using Rr a2 unfolding set_rel_def by blast
  1.1048 -      thus ?thesis unfolding Inr by auto
  1.1049 -    qed
  1.1050 -  qed
  1.1051 -qed
  1.1052 -
  1.1053 -end