src/HOL/BNF/More_BNFs.thy
author blanchet
Tue Oct 01 14:05:25 2013 +0200 (2013-10-01)
changeset 54006 9fe1bd54d437
parent 53374 a14d2a854c02
child 54014 21dac9a60f0c
permissions -rw-r--r--
renamed theory file
     1 (*  Title:      HOL/BNF/More_BNFs.thy
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Author:     Andrei Popescu, TU Muenchen
     4     Author:     Andreas Lochbihler, Karlsruhe Institute of Technology
     5     Author:     Jasmin Blanchette, TU Muenchen
     6     Copyright   2012
     7 
     8 Registration of various types as bounded natural functors.
     9 *)
    10 
    11 header {* Registration of Various Types as Bounded Natural Functors *}
    12 
    13 theory More_BNFs
    14 imports
    15   Basic_BNFs
    16   "~~/src/HOL/Quotient_Examples/Lift_FSet"
    17   "~~/src/HOL/Library/Multiset"
    18   Countable_Type
    19 begin
    20 
    21 lemma option_rec_conv_option_case: "option_rec = option_case"
    22 by (simp add: fun_eq_iff split: option.split)
    23 
    24 bnf Option.map [Option.set] "\<lambda>_::'a option. natLeq" ["None"] option_rel
    25 proof -
    26   show "Option.map id = id" by (simp add: fun_eq_iff Option.map_def split: option.split)
    27 next
    28   fix f g
    29   show "Option.map (g \<circ> f) = Option.map g \<circ> Option.map f"
    30     by (auto simp add: fun_eq_iff Option.map_def split: option.split)
    31 next
    32   fix f g x
    33   assume "\<And>z. z \<in> Option.set x \<Longrightarrow> f z = g z"
    34   thus "Option.map f x = Option.map g x"
    35     by (simp cong: Option.map_cong)
    36 next
    37   fix f
    38   show "Option.set \<circ> Option.map f = op ` f \<circ> Option.set"
    39     by fastforce
    40 next
    41   show "card_order natLeq" by (rule natLeq_card_order)
    42 next
    43   show "cinfinite natLeq" by (rule natLeq_cinfinite)
    44 next
    45   fix x
    46   show "|Option.set x| \<le>o natLeq"
    47     by (cases x) (simp_all add: ordLess_imp_ordLeq finite_iff_ordLess_natLeq[symmetric])
    48 next
    49   fix A B1 B2 f1 f2 p1 p2
    50   assume wpull: "wpull A B1 B2 f1 f2 p1 p2"
    51   show "wpull {x. Option.set x \<subseteq> A} {x. Option.set x \<subseteq> B1} {x. Option.set x \<subseteq> B2}
    52     (Option.map f1) (Option.map f2) (Option.map p1) (Option.map p2)"
    53     (is "wpull ?A ?B1 ?B2 ?f1 ?f2 ?p1 ?p2")
    54     unfolding wpull_def
    55   proof (intro strip, elim conjE)
    56     fix b1 b2
    57     assume "b1 \<in> ?B1" "b2 \<in> ?B2" "?f1 b1 = ?f2 b2"
    58     thus "\<exists>a \<in> ?A. ?p1 a = b1 \<and> ?p2 a = b2" using wpull
    59       unfolding wpull_def by (cases b2) (auto 4 5)
    60   qed
    61 next
    62   fix z
    63   assume "z \<in> Option.set None"
    64   thus False by simp
    65 next
    66   fix R
    67   show "option_rel R =
    68         (Grp {x. Option.set x \<subseteq> Collect (split R)} (Option.map fst))\<inverse>\<inverse> OO
    69          Grp {x. Option.set x \<subseteq> Collect (split R)} (Option.map snd)"
    70   unfolding option_rel_def Grp_def relcompp.simps conversep.simps fun_eq_iff prod.cases
    71   by (auto simp: trans[OF eq_commute option_map_is_None] trans[OF eq_commute option_map_eq_Some]
    72            split: option.splits)
    73 qed
    74 
    75 lemma wpull_map:
    76   assumes "wpull A B1 B2 f1 f2 p1 p2"
    77   shows "wpull {x. set x \<subseteq> A} {x. set x \<subseteq> B1} {x. set x \<subseteq> B2} (map f1) (map f2) (map p1) (map p2)"
    78     (is "wpull ?A ?B1 ?B2 _ _ _ _")
    79 proof (unfold wpull_def)
    80   { fix as bs assume *: "as \<in> ?B1" "bs \<in> ?B2" "map f1 as = map f2 bs"
    81     hence "length as = length bs" by (metis length_map)
    82     hence "\<exists>zs \<in> ?A. map p1 zs = as \<and> map p2 zs = bs" using *
    83     proof (induct as bs rule: list_induct2)
    84       case (Cons a as b bs)
    85       hence "a \<in> B1" "b \<in> B2" "f1 a = f2 b" by auto
    86       with assms obtain z where "z \<in> A" "p1 z = a" "p2 z = b" unfolding wpull_def by blast
    87       moreover
    88       from Cons obtain zs where "zs \<in> ?A" "map p1 zs = as" "map p2 zs = bs" by auto
    89       ultimately have "z # zs \<in> ?A" "map p1 (z # zs) = a # as \<and> map p2 (z # zs) = b # bs" by auto
    90       thus ?case by (rule_tac x = "z # zs" in bexI)
    91     qed simp
    92   }
    93   thus "\<forall>as bs. as \<in> ?B1 \<and> bs \<in> ?B2 \<and> map f1 as = map f2 bs \<longrightarrow>
    94     (\<exists>zs \<in> ?A. map p1 zs = as \<and> map p2 zs = bs)" by blast
    95 qed
    96 
    97 bnf map [set] "\<lambda>_::'a list. natLeq" ["[]"]
    98 proof -
    99   show "map id = id" by (rule List.map.id)
   100 next
   101   fix f g
   102   show "map (g o f) = map g o map f" by (rule List.map.comp[symmetric])
   103 next
   104   fix x f g
   105   assume "\<And>z. z \<in> set x \<Longrightarrow> f z = g z"
   106   thus "map f x = map g x" by simp
   107 next
   108   fix f
   109   show "set o map f = image f o set" by (rule ext, unfold o_apply, rule set_map)
   110 next
   111   show "card_order natLeq" by (rule natLeq_card_order)
   112 next
   113   show "cinfinite natLeq" by (rule natLeq_cinfinite)
   114 next
   115   fix x
   116   show "|set x| \<le>o natLeq"
   117     by (metis List.finite_set finite_iff_ordLess_natLeq ordLess_imp_ordLeq)
   118 qed (simp add: wpull_map)+
   119 
   120 (* Finite sets *)
   121 
   122 definition fset_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'b fset \<Rightarrow> bool" where
   123 "fset_rel R a b \<longleftrightarrow>
   124  (\<forall>t \<in> fset a. \<exists>u \<in> fset b. R t u) \<and>
   125  (\<forall>t \<in> fset b. \<exists>u \<in> fset a. R u t)"
   126 
   127 
   128 lemma fset_to_fset: "finite A \<Longrightarrow> fset (the_inv fset A) = A"
   129   by (rule f_the_inv_into_f[unfolded inj_on_def])
   130     (transfer, simp,
   131      transfer, metis Collect_finite_eq_lists lists_UNIV mem_Collect_eq)
   132 
   133 
   134 lemma fset_rel_aux:
   135 "(\<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>
   136  ((Grp {a. fset a \<subseteq> {(a, b). R a b}} (fmap fst))\<inverse>\<inverse> OO
   137   Grp {a. fset a \<subseteq> {(a, b). R a b}} (fmap snd)) a b" (is "?L = ?R")
   138 proof
   139   assume ?L
   140   def R' \<equiv> "the_inv fset (Collect (split R) \<inter> (fset a \<times> fset b))" (is "the_inv fset ?L'")
   141   have "finite ?L'" by (intro finite_Int[OF disjI2] finite_cartesian_product) (transfer, simp)+
   142   hence *: "fset R' = ?L'" unfolding R'_def by (intro fset_to_fset)
   143   show ?R unfolding Grp_def relcompp.simps conversep.simps
   144   proof (intro CollectI prod_caseI exI[of _ a] exI[of _ b] exI[of _ R'] conjI refl)
   145     from * show "a = fmap fst R'" using conjunct1[OF `?L`]
   146       by (transfer, auto simp add: image_def Int_def split: prod.splits)
   147     from * show "b = fmap snd R'" using conjunct2[OF `?L`]
   148       by (transfer, auto simp add: image_def Int_def split: prod.splits)
   149   qed (auto simp add: *)
   150 next
   151   assume ?R thus ?L unfolding Grp_def relcompp.simps conversep.simps
   152   apply (simp add: subset_eq Ball_def)
   153   apply (rule conjI)
   154   apply (transfer, clarsimp, metis snd_conv)
   155   by (transfer, clarsimp, metis fst_conv)
   156 qed
   157 
   158 lemma wpull_image:
   159   assumes "wpull A B1 B2 f1 f2 p1 p2"
   160   shows "wpull (Pow A) (Pow B1) (Pow B2) (image f1) (image f2) (image p1) (image p2)"
   161 unfolding wpull_def Pow_def Bex_def mem_Collect_eq proof clarify
   162   fix Y1 Y2 assume Y1: "Y1 \<subseteq> B1" and Y2: "Y2 \<subseteq> B2" and EQ: "f1 ` Y1 = f2 ` Y2"
   163   def X \<equiv> "{a \<in> A. p1 a \<in> Y1 \<and> p2 a \<in> Y2}"
   164   show "\<exists>X\<subseteq>A. p1 ` X = Y1 \<and> p2 ` X = Y2"
   165   proof (rule exI[of _ X], intro conjI)
   166     show "p1 ` X = Y1"
   167     proof
   168       show "Y1 \<subseteq> p1 ` X"
   169       proof safe
   170         fix y1 assume y1: "y1 \<in> Y1"
   171         then obtain y2 where y2: "y2 \<in> Y2" and eq: "f1 y1 = f2 y2" using EQ by auto
   172         then obtain x where "x \<in> A" and "p1 x = y1" and "p2 x = y2"
   173         using assms y1 Y1 Y2 unfolding wpull_def by blast
   174         thus "y1 \<in> p1 ` X" unfolding X_def using y1 y2 by auto
   175       qed
   176     qed(unfold X_def, auto)
   177     show "p2 ` X = Y2"
   178     proof
   179       show "Y2 \<subseteq> p2 ` X"
   180       proof safe
   181         fix y2 assume y2: "y2 \<in> Y2"
   182         then obtain y1 where y1: "y1 \<in> Y1" and eq: "f1 y1 = f2 y2" using EQ by force
   183         then obtain x where "x \<in> A" and "p1 x = y1" and "p2 x = y2"
   184         using assms y2 Y1 Y2 unfolding wpull_def by blast
   185         thus "y2 \<in> p2 ` X" unfolding X_def using y1 y2 by auto
   186       qed
   187     qed(unfold X_def, auto)
   188   qed(unfold X_def, auto)
   189 qed
   190 
   191 lemma wpull_fmap:
   192   assumes "wpull A B1 B2 f1 f2 p1 p2"
   193   shows "wpull {x. fset x \<subseteq> A} {x. fset x \<subseteq> B1} {x. fset x \<subseteq> B2}
   194               (fmap f1) (fmap f2) (fmap p1) (fmap p2)"
   195 unfolding wpull_def Pow_def Bex_def mem_Collect_eq proof clarify
   196   fix y1 y2
   197   assume Y1: "fset y1 \<subseteq> B1" and Y2: "fset y2 \<subseteq> B2"
   198   assume "fmap f1 y1 = fmap f2 y2"
   199   hence EQ: "f1 ` (fset y1) = f2 ` (fset y2)" by transfer simp
   200   with Y1 Y2 obtain X where X: "X \<subseteq> A" and Y1: "p1 ` X = fset y1" and Y2: "p2 ` X = fset y2"
   201     using wpull_image[OF assms] unfolding wpull_def Pow_def
   202     by (auto elim!: allE[of _ "fset y1"] allE[of _ "fset y2"])
   203   have "\<forall> y1' \<in> fset y1. \<exists> x. x \<in> X \<and> y1' = p1 x" using Y1 by auto
   204   then obtain q1 where q1: "\<forall> y1' \<in> fset y1. q1 y1' \<in> X \<and> y1' = p1 (q1 y1')" by metis
   205   have "\<forall> y2' \<in> fset y2. \<exists> x. x \<in> X \<and> y2' = p2 x" using Y2 by auto
   206   then obtain q2 where q2: "\<forall> y2' \<in> fset y2. q2 y2' \<in> X \<and> y2' = p2 (q2 y2')" by metis
   207   def X' \<equiv> "q1 ` (fset y1) \<union> q2 ` (fset y2)"
   208   have X': "X' \<subseteq> A" and Y1: "p1 ` X' = fset y1" and Y2: "p2 ` X' = fset y2"
   209   using X Y1 Y2 q1 q2 unfolding X'_def by auto
   210   have fX': "finite X'" unfolding X'_def by transfer simp
   211   then obtain x where X'eq: "X' = fset x" by transfer (metis finite_list)
   212   show "\<exists>x. fset x \<subseteq> A \<and> fmap p1 x = y1 \<and> fmap p2 x = y2"
   213      using X' Y1 Y2 by (auto simp: X'eq intro!: exI[of _ "x"]) (transfer, simp)+
   214 qed
   215 
   216 bnf fmap [fset] "\<lambda>_::'a fset. natLeq" ["{||}"] fset_rel
   217 apply -
   218           apply transfer' apply simp
   219          apply transfer' apply simp
   220         apply transfer apply force
   221        apply transfer apply force
   222       apply (rule natLeq_card_order)
   223      apply (rule natLeq_cinfinite)
   224     apply transfer apply (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq finite_set)
   225   apply (erule wpull_fmap)
   226  apply (simp add: Grp_def relcompp.simps conversep.simps fun_eq_iff fset_rel_def fset_rel_aux) 
   227 apply transfer apply simp
   228 done
   229 
   230 lemmas [simp] = fset.map_comp fset.map_id fset.set_map
   231 
   232 lemma fset_rel_fset: "set_rel \<chi> (fset A1) (fset A2) = fset_rel \<chi> A1 A2"
   233   unfolding fset_rel_def set_rel_def by auto
   234 
   235 (* Countable sets *)
   236 
   237 lemma card_of_countable_sets_range:
   238 fixes A :: "'a set"
   239 shows "|{X. X \<subseteq> A \<and> countable X \<and> X \<noteq> {}}| \<le>o |{f::nat \<Rightarrow> 'a. range f \<subseteq> A}|"
   240 apply(rule card_of_ordLeqI[of from_nat_into]) using inj_on_from_nat_into
   241 unfolding inj_on_def by auto
   242 
   243 lemma card_of_countable_sets_Func:
   244 "|{X. X \<subseteq> A \<and> countable X \<and> X \<noteq> {}}| \<le>o |A| ^c natLeq"
   245 using card_of_countable_sets_range card_of_Func_UNIV[THEN ordIso_symmetric]
   246 unfolding cexp_def Field_natLeq Field_card_of
   247 by (rule ordLeq_ordIso_trans)
   248 
   249 lemma ordLeq_countable_subsets:
   250 "|A| \<le>o |{X. X \<subseteq> A \<and> countable X}|"
   251 apply (rule card_of_ordLeqI[of "\<lambda> a. {a}"]) unfolding inj_on_def by auto
   252 
   253 lemma finite_countable_subset:
   254 "finite {X. X \<subseteq> A \<and> countable X} \<longleftrightarrow> finite A"
   255 apply default
   256  apply (erule contrapos_pp)
   257  apply (rule card_of_ordLeq_infinite)
   258  apply (rule ordLeq_countable_subsets)
   259  apply assumption
   260 apply (rule finite_Collect_conjI)
   261 apply (rule disjI1)
   262 by (erule finite_Collect_subsets)
   263 
   264 lemma rcset_to_rcset: "countable A \<Longrightarrow> rcset (the_inv rcset A) = A"
   265   apply (rule f_the_inv_into_f[unfolded inj_on_def image_iff])
   266    apply transfer' apply simp
   267   apply transfer' apply simp
   268   done
   269 
   270 lemma Collect_Int_Times:
   271 "{(x, y). R x y} \<inter> A \<times> B = {(x, y). R x y \<and> x \<in> A \<and> y \<in> B}"
   272 by auto
   273 
   274 definition cset_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a cset \<Rightarrow> 'b cset \<Rightarrow> bool" where
   275 "cset_rel R a b \<longleftrightarrow>
   276  (\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and>
   277  (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t)"
   278 
   279 lemma cset_rel_aux:
   280 "(\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and> (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t) \<longleftrightarrow>
   281  ((Grp {x. rcset x \<subseteq> {(a, b). R a b}} (cimage fst))\<inverse>\<inverse> OO
   282           Grp {x. rcset x \<subseteq> {(a, b). R a b}} (cimage snd)) a b" (is "?L = ?R")
   283 proof
   284   assume ?L
   285   def R' \<equiv> "the_inv rcset (Collect (split R) \<inter> (rcset a \<times> rcset b))"
   286   (is "the_inv rcset ?L'")
   287   have L: "countable ?L'" by auto
   288   hence *: "rcset R' = ?L'" unfolding R'_def using fset_to_fset by (intro rcset_to_rcset)
   289   thus ?R unfolding Grp_def relcompp.simps conversep.simps
   290   proof (intro CollectI prod_caseI exI[of _ a] exI[of _ b] exI[of _ R'] conjI refl)
   291     from * `?L` show "a = cimage fst R'" by transfer (auto simp: image_def Collect_Int_Times)
   292   next
   293     from * `?L` show "b = cimage snd R'" by transfer (auto simp: image_def Collect_Int_Times)
   294   qed simp_all
   295 next
   296   assume ?R thus ?L unfolding Grp_def relcompp.simps conversep.simps
   297     by transfer force
   298 qed
   299 
   300 bnf cimage [rcset] "\<lambda>_::'a cset. natLeq" ["cempty"] cset_rel
   301 proof -
   302   show "cimage id = id" by transfer' simp
   303 next
   304   fix f g show "cimage (g \<circ> f) = cimage g \<circ> cimage f" by transfer' fastforce
   305 next
   306   fix C f g assume eq: "\<And>a. a \<in> rcset C \<Longrightarrow> f a = g a"
   307   thus "cimage f C = cimage g C" by transfer force
   308 next
   309   fix f show "rcset \<circ> cimage f = op ` f \<circ> rcset" by transfer' fastforce
   310 next
   311   show "card_order natLeq" by (rule natLeq_card_order)
   312 next
   313   show "cinfinite natLeq" by (rule natLeq_cinfinite)
   314 next
   315   fix C show "|rcset C| \<le>o natLeq" by transfer (unfold countable_card_le_natLeq)
   316 next
   317   fix A B1 B2 f1 f2 p1 p2
   318   assume wp: "wpull A B1 B2 f1 f2 p1 p2"
   319   show "wpull {x. rcset x \<subseteq> A} {x. rcset x \<subseteq> B1} {x. rcset x \<subseteq> B2}
   320               (cimage f1) (cimage f2) (cimage p1) (cimage p2)"
   321   unfolding wpull_def proof safe
   322     fix y1 y2
   323     assume Y1: "rcset y1 \<subseteq> B1" and Y2: "rcset y2 \<subseteq> B2"
   324     assume "cimage f1 y1 = cimage f2 y2"
   325     hence EQ: "f1 ` (rcset y1) = f2 ` (rcset y2)" by transfer
   326     with Y1 Y2 obtain X where X: "X \<subseteq> A"
   327     and Y1: "p1 ` X = rcset y1" and Y2: "p2 ` X = rcset y2"
   328     using wpull_image[OF wp] unfolding wpull_def Pow_def Bex_def mem_Collect_eq
   329       by (auto elim!: allE[of _ "rcset y1"] allE[of _ "rcset y2"])
   330     have "\<forall> y1' \<in> rcset y1. \<exists> x. x \<in> X \<and> y1' = p1 x" using Y1 by auto
   331     then obtain q1 where q1: "\<forall> y1' \<in> rcset y1. q1 y1' \<in> X \<and> y1' = p1 (q1 y1')" by metis
   332     have "\<forall> y2' \<in> rcset y2. \<exists> x. x \<in> X \<and> y2' = p2 x" using Y2 by auto
   333     then obtain q2 where q2: "\<forall> y2' \<in> rcset y2. q2 y2' \<in> X \<and> y2' = p2 (q2 y2')" by metis
   334     def X' \<equiv> "q1 ` (rcset y1) \<union> q2 ` (rcset y2)"
   335     have X': "X' \<subseteq> A" and Y1: "p1 ` X' = rcset y1" and Y2: "p2 ` X' = rcset y2"
   336     using X Y1 Y2 q1 q2 unfolding X'_def by fast+
   337     have fX': "countable X'" unfolding X'_def by simp
   338     then obtain x where X'eq: "X' = rcset x" by transfer blast
   339     show "\<exists>x\<in>{x. rcset x \<subseteq> A}. cimage p1 x = y1 \<and> cimage p2 x = y2"
   340       using X' Y1 Y2 unfolding X'eq by (intro bexI[of _ "x"]) (transfer, auto)
   341   qed
   342 next
   343   fix R
   344   show "cset_rel R =
   345         (Grp {x. rcset x \<subseteq> Collect (split R)} (cimage fst))\<inverse>\<inverse> OO
   346          Grp {x. rcset x \<subseteq> Collect (split R)} (cimage snd)"
   347   unfolding cset_rel_def[abs_def] cset_rel_aux by simp
   348 qed (transfer, simp)
   349 
   350 
   351 (* Multisets *)
   352 
   353 lemma setsum_gt_0_iff:
   354 fixes f :: "'a \<Rightarrow> nat" assumes "finite A"
   355 shows "setsum f A > 0 \<longleftrightarrow> (\<exists> a \<in> A. f a > 0)"
   356 (is "?L \<longleftrightarrow> ?R")
   357 proof-
   358   have "?L \<longleftrightarrow> \<not> setsum f A = 0" by fast
   359   also have "... \<longleftrightarrow> (\<exists> a \<in> A. f a \<noteq> 0)" using assms by simp
   360   also have "... \<longleftrightarrow> ?R" by simp
   361   finally show ?thesis .
   362 qed
   363 
   364 lift_definition mmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" is
   365   "\<lambda>h f b. setsum f {a. h a = b \<and> f a > 0} :: nat"
   366 unfolding multiset_def proof safe
   367   fix h :: "'a \<Rightarrow> 'b" and f :: "'a \<Rightarrow> nat"
   368   assume fin: "finite {a. 0 < f a}"  (is "finite ?A")
   369   show "finite {b. 0 < setsum f {a. h a = b \<and> 0 < f a}}"
   370   (is "finite {b. 0 < setsum f (?As b)}")
   371   proof- let ?B = "{b. 0 < setsum f (?As b)}"
   372     have "\<And> b. finite (?As b)" using fin by simp
   373     hence B: "?B = {b. ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
   374     hence "?B \<subseteq> h ` ?A" by auto
   375     thus ?thesis using finite_surj[OF fin] by auto
   376   qed
   377 qed
   378 
   379 lemma mmap_id0: "mmap id = id"
   380 proof (intro ext multiset_eqI)
   381   fix f a show "count (mmap id f) a = count (id f) a"
   382   proof (cases "count f a = 0")
   383     case False
   384     hence 1: "{aa. aa = a \<and> aa \<in># f} = {a}" by auto
   385     thus ?thesis by transfer auto
   386   qed (transfer, simp)
   387 qed
   388 
   389 lemma inj_on_setsum_inv:
   390 assumes 1: "(0::nat) < setsum (count f) {a. h a = b' \<and> a \<in># f}" (is "0 < setsum (count f) ?A'")
   391 and     2: "{a. h a = b \<and> a \<in># f} = {a. h a = b' \<and> a \<in># f}" (is "?A = ?A'")
   392 shows "b = b'"
   393 using assms by (auto simp add: setsum_gt_0_iff)
   394 
   395 lemma mmap_comp:
   396 fixes h1 :: "'a \<Rightarrow> 'b" and h2 :: "'b \<Rightarrow> 'c"
   397 shows "mmap (h2 o h1) = mmap h2 o mmap h1"
   398 proof (intro ext multiset_eqI)
   399   fix f :: "'a multiset" fix c :: 'c
   400   let ?A = "{a. h2 (h1 a) = c \<and> a \<in># f}"
   401   let ?As = "\<lambda> b. {a. h1 a = b \<and> a \<in># f}"
   402   let ?B = "{b. h2 b = c \<and> 0 < setsum (count f) (?As b)}"
   403   have 0: "{?As b | b.  b \<in> ?B} = ?As ` ?B" by auto
   404   have "\<And> b. finite (?As b)" by transfer (simp add: multiset_def)
   405   hence "?B = {b. h2 b = c \<and> ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
   406   hence A: "?A = \<Union> {?As b | b.  b \<in> ?B}" by auto
   407   have "setsum (count f) ?A = setsum (setsum (count f)) {?As b | b.  b \<in> ?B}"
   408     unfolding A by transfer (intro setsum_Union_disjoint, auto simp: multiset_def)
   409   also have "... = setsum (setsum (count f)) (?As ` ?B)" unfolding 0 ..
   410   also have "... = setsum (setsum (count f) o ?As) ?B"
   411     by(intro setsum_reindex) (auto simp add: setsum_gt_0_iff inj_on_def)
   412   also have "... = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" unfolding comp_def ..
   413   finally have "setsum (count f) ?A = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" .
   414   thus "count (mmap (h2 \<circ> h1) f) c = count ((mmap h2 \<circ> mmap h1) f) c"
   415     by transfer (unfold o_apply, blast)
   416 qed
   417 
   418 lemma mmap_cong:
   419 assumes "\<And>a. a \<in># M \<Longrightarrow> f a = g a"
   420 shows "mmap f M = mmap g M"
   421 using assms by transfer (auto intro!: setsum_cong)
   422 
   423 context
   424 begin
   425 interpretation lifting_syntax .
   426 
   427 lemma set_of_transfer[transfer_rule]: "(pcr_multiset op = ===> op =) (\<lambda>f. {a. 0 < f a}) set_of"
   428   unfolding set_of_def pcr_multiset_def cr_multiset_def fun_rel_def by auto
   429 
   430 end
   431 
   432 lemma set_of_mmap: "set_of o mmap h = image h o set_of"
   433 proof (rule ext, unfold o_apply)
   434   fix M show "set_of (mmap h M) = h ` set_of M"
   435     by transfer (auto simp add: multiset_def setsum_gt_0_iff)
   436 qed
   437 
   438 lemma multiset_of_surj:
   439   "multiset_of ` {as. set as \<subseteq> A} = {M. set_of M \<subseteq> A}"
   440 proof safe
   441   fix M assume M: "set_of M \<subseteq> A"
   442   obtain as where eq: "M = multiset_of as" using surj_multiset_of unfolding surj_def by auto
   443   hence "set as \<subseteq> A" using M by auto
   444   thus "M \<in> multiset_of ` {as. set as \<subseteq> A}" using eq by auto
   445 next
   446   show "\<And>x xa xb. \<lbrakk>set xa \<subseteq> A; xb \<in> set_of (multiset_of xa)\<rbrakk> \<Longrightarrow> xb \<in> A"
   447   by (erule set_mp) (unfold set_of_multiset_of)
   448 qed
   449 
   450 lemma card_of_set_of:
   451 "|{M. set_of M \<subseteq> A}| \<le>o |{as. set as \<subseteq> A}|"
   452 apply(rule card_of_ordLeqI2[of _ multiset_of]) using multiset_of_surj by auto
   453 
   454 lemma nat_sum_induct:
   455 assumes "\<And>n1 n2. (\<And> m1 m2. m1 + m2 < n1 + n2 \<Longrightarrow> phi m1 m2) \<Longrightarrow> phi n1 n2"
   456 shows "phi (n1::nat) (n2::nat)"
   457 proof-
   458   let ?chi = "\<lambda> n1n2 :: nat * nat. phi (fst n1n2) (snd n1n2)"
   459   have "?chi (n1,n2)"
   460   apply(induct rule: measure_induct[of "\<lambda> n1n2. fst n1n2 + snd n1n2" ?chi])
   461   using assms by (metis fstI sndI)
   462   thus ?thesis by simp
   463 qed
   464 
   465 lemma matrix_count:
   466 fixes ct1 ct2 :: "nat \<Rightarrow> nat"
   467 assumes "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
   468 shows
   469 "\<exists> ct. (\<forall> i1 \<le> n1. setsum (\<lambda> i2. ct i1 i2) {..<Suc n2} = ct1 i1) \<and>
   470        (\<forall> i2 \<le> n2. setsum (\<lambda> i1. ct i1 i2) {..<Suc n1} = ct2 i2)"
   471 (is "?phi ct1 ct2 n1 n2")
   472 proof-
   473   have "\<forall> ct1 ct2 :: nat \<Rightarrow> nat.
   474         setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} \<longrightarrow> ?phi ct1 ct2 n1 n2"
   475   proof(induct rule: nat_sum_induct[of
   476 "\<lambda> n1 n2. \<forall> ct1 ct2 :: nat \<Rightarrow> nat.
   477      setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} \<longrightarrow> ?phi ct1 ct2 n1 n2"],
   478       clarify)
   479   fix n1 n2 :: nat and ct1 ct2 :: "nat \<Rightarrow> nat"
   480   assume IH: "\<And> m1 m2. m1 + m2 < n1 + n2 \<Longrightarrow>
   481                 \<forall> dt1 dt2 :: nat \<Rightarrow> nat.
   482                 setsum dt1 {..<Suc m1} = setsum dt2 {..<Suc m2} \<longrightarrow> ?phi dt1 dt2 m1 m2"
   483   and ss: "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
   484   show "?phi ct1 ct2 n1 n2"
   485   proof(cases n1)
   486     case 0 note n1 = 0
   487     show ?thesis
   488     proof(cases n2)
   489       case 0 note n2 = 0
   490       let ?ct = "\<lambda> i1 i2. ct2 0"
   491       show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by simp
   492     next
   493       case (Suc m2) note n2 = Suc
   494       let ?ct = "\<lambda> i1 i2. ct2 i2"
   495       show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
   496     qed
   497   next
   498     case (Suc m1) note n1 = Suc
   499     show ?thesis
   500     proof(cases n2)
   501       case 0 note n2 = 0
   502       let ?ct = "\<lambda> i1 i2. ct1 i1"
   503       show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
   504     next
   505       case (Suc m2) note n2 = Suc
   506       show ?thesis
   507       proof(cases "ct1 n1 \<le> ct2 n2")
   508         case True
   509         def dt2 \<equiv> "\<lambda> i2. if i2 = n2 then ct2 i2 - ct1 n1 else ct2 i2"
   510         have "setsum ct1 {..<Suc m1} = setsum dt2 {..<Suc n2}"
   511         unfolding dt2_def using ss n1 True by auto
   512         hence "?phi ct1 dt2 m1 n2" using IH[of m1 n2] n1 by simp
   513         then obtain dt where
   514         1: "\<And> i1. i1 \<le> m1 \<Longrightarrow> setsum (\<lambda> i2. dt i1 i2) {..<Suc n2} = ct1 i1" and
   515         2: "\<And> i2. i2 \<le> n2 \<Longrightarrow> setsum (\<lambda> i1. dt i1 i2) {..<Suc m1} = dt2 i2" by auto
   516         let ?ct = "\<lambda> i1 i2. if i1 = n1 then (if i2 = n2 then ct1 n1 else 0)
   517                                        else dt i1 i2"
   518         show ?thesis apply(rule exI[of _ ?ct])
   519         using n1 n2 1 2 True unfolding dt2_def by simp
   520       next
   521         case False
   522         hence False: "ct2 n2 < ct1 n1" by simp
   523         def dt1 \<equiv> "\<lambda> i1. if i1 = n1 then ct1 i1 - ct2 n2 else ct1 i1"
   524         have "setsum dt1 {..<Suc n1} = setsum ct2 {..<Suc m2}"
   525         unfolding dt1_def using ss n2 False by auto
   526         hence "?phi dt1 ct2 n1 m2" using IH[of n1 m2] n2 by simp
   527         then obtain dt where
   528         1: "\<And> i1. i1 \<le> n1 \<Longrightarrow> setsum (\<lambda> i2. dt i1 i2) {..<Suc m2} = dt1 i1" and
   529         2: "\<And> i2. i2 \<le> m2 \<Longrightarrow> setsum (\<lambda> i1. dt i1 i2) {..<Suc n1} = ct2 i2" by force
   530         let ?ct = "\<lambda> i1 i2. if i2 = n2 then (if i1 = n1 then ct2 n2 else 0)
   531                                        else dt i1 i2"
   532         show ?thesis apply(rule exI[of _ ?ct])
   533         using n1 n2 1 2 False unfolding dt1_def by simp
   534       qed
   535     qed
   536   qed
   537   qed
   538   thus ?thesis using assms by auto
   539 qed
   540 
   541 definition
   542 "inj2 u B1 B2 \<equiv>
   543  \<forall> b1 b1' b2 b2'. {b1,b1'} \<subseteq> B1 \<and> {b2,b2'} \<subseteq> B2 \<and> u b1 b2 = u b1' b2'
   544                   \<longrightarrow> b1 = b1' \<and> b2 = b2'"
   545 
   546 lemma matrix_setsum_finite:
   547 assumes B1: "B1 \<noteq> {}" "finite B1" and B2: "B2 \<noteq> {}" "finite B2" and u: "inj2 u B1 B2"
   548 and ss: "setsum N1 B1 = setsum N2 B2"
   549 shows "\<exists> M :: 'a \<Rightarrow> nat.
   550             (\<forall> b1 \<in> B1. setsum (\<lambda> b2. M (u b1 b2)) B2 = N1 b1) \<and>
   551             (\<forall> b2 \<in> B2. setsum (\<lambda> b1. M (u b1 b2)) B1 = N2 b2)"
   552 proof-
   553   obtain n1 where "card B1 = Suc n1" using B1 by (metis card_insert finite.simps)
   554   then obtain e1 where e1: "bij_betw e1 {..<Suc n1} B1"
   555   using ex_bij_betw_finite_nat[OF B1(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
   556   hence e1_inj: "inj_on e1 {..<Suc n1}" and e1_surj: "e1 ` {..<Suc n1} = B1"
   557   unfolding bij_betw_def by auto
   558   def f1 \<equiv> "inv_into {..<Suc n1} e1"
   559   have f1: "bij_betw f1 B1 {..<Suc n1}"
   560   and f1e1[simp]: "\<And> i1. i1 < Suc n1 \<Longrightarrow> f1 (e1 i1) = i1"
   561   and e1f1[simp]: "\<And> b1. b1 \<in> B1 \<Longrightarrow> e1 (f1 b1) = b1" unfolding f1_def
   562   apply (metis bij_betw_inv_into e1, metis bij_betw_inv_into_left e1 lessThan_iff)
   563   by (metis e1_surj f_inv_into_f)
   564   (*  *)
   565   obtain n2 where "card B2 = Suc n2" using B2 by (metis card_insert finite.simps)
   566   then obtain e2 where e2: "bij_betw e2 {..<Suc n2} B2"
   567   using ex_bij_betw_finite_nat[OF B2(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
   568   hence e2_inj: "inj_on e2 {..<Suc n2}" and e2_surj: "e2 ` {..<Suc n2} = B2"
   569   unfolding bij_betw_def by auto
   570   def f2 \<equiv> "inv_into {..<Suc n2} e2"
   571   have f2: "bij_betw f2 B2 {..<Suc n2}"
   572   and f2e2[simp]: "\<And> i2. i2 < Suc n2 \<Longrightarrow> f2 (e2 i2) = i2"
   573   and e2f2[simp]: "\<And> b2. b2 \<in> B2 \<Longrightarrow> e2 (f2 b2) = b2" unfolding f2_def
   574   apply (metis bij_betw_inv_into e2, metis bij_betw_inv_into_left e2 lessThan_iff)
   575   by (metis e2_surj f_inv_into_f)
   576   (*  *)
   577   let ?ct1 = "N1 o e1"  let ?ct2 = "N2 o e2"
   578   have ss: "setsum ?ct1 {..<Suc n1} = setsum ?ct2 {..<Suc n2}"
   579   unfolding setsum_reindex[OF e1_inj, symmetric] setsum_reindex[OF e2_inj, symmetric]
   580   e1_surj e2_surj using ss .
   581   obtain ct where
   582   ct1: "\<And> i1. i1 \<le> n1 \<Longrightarrow> setsum (\<lambda> i2. ct i1 i2) {..<Suc n2} = ?ct1 i1" and
   583   ct2: "\<And> i2. i2 \<le> n2 \<Longrightarrow> setsum (\<lambda> i1. ct i1 i2) {..<Suc n1} = ?ct2 i2"
   584   using matrix_count[OF ss] by blast
   585   (*  *)
   586   def A \<equiv> "{u b1 b2 | b1 b2. b1 \<in> B1 \<and> b2 \<in> B2}"
   587   have "\<forall> a \<in> A. \<exists> b1b2 \<in> B1 <*> B2. u (fst b1b2) (snd b1b2) = a"
   588   unfolding A_def Ball_def mem_Collect_eq by auto
   589   then obtain h1h2 where h12:
   590   "\<And>a. a \<in> A \<Longrightarrow> u (fst (h1h2 a)) (snd (h1h2 a)) = a \<and> h1h2 a \<in> B1 <*> B2" by metis
   591   def h1 \<equiv> "fst o h1h2"  def h2 \<equiv> "snd o h1h2"
   592   have h12[simp]: "\<And>a. a \<in> A \<Longrightarrow> u (h1 a) (h2 a) = a"
   593                   "\<And> a. a \<in> A \<Longrightarrow> h1 a \<in> B1"  "\<And> a. a \<in> A \<Longrightarrow> h2 a \<in> B2"
   594   using h12 unfolding h1_def h2_def by force+
   595   {fix b1 b2 assume b1: "b1 \<in> B1" and b2: "b2 \<in> B2"
   596    hence inA: "u b1 b2 \<in> A" unfolding A_def by auto
   597    hence "u b1 b2 = u (h1 (u b1 b2)) (h2 (u b1 b2))" by auto
   598    moreover have "h1 (u b1 b2) \<in> B1" "h2 (u b1 b2) \<in> B2" using inA by auto
   599    ultimately have "h1 (u b1 b2) = b1 \<and> h2 (u b1 b2) = b2"
   600    using u b1 b2 unfolding inj2_def by fastforce
   601   }
   602   hence h1[simp]: "\<And> b1 b2. \<lbrakk>b1 \<in> B1; b2 \<in> B2\<rbrakk> \<Longrightarrow> h1 (u b1 b2) = b1" and
   603         h2[simp]: "\<And> b1 b2. \<lbrakk>b1 \<in> B1; b2 \<in> B2\<rbrakk> \<Longrightarrow> h2 (u b1 b2) = b2" by auto
   604   def M \<equiv> "\<lambda> a. ct (f1 (h1 a)) (f2 (h2 a))"
   605   show ?thesis
   606   apply(rule exI[of _ M]) proof safe
   607     fix b1 assume b1: "b1 \<in> B1"
   608     hence f1b1: "f1 b1 \<le> n1" using f1 unfolding bij_betw_def
   609     by (metis image_eqI lessThan_iff less_Suc_eq_le)
   610     have "(\<Sum>b2\<in>B2. M (u b1 b2)) = (\<Sum>i2<Suc n2. ct (f1 b1) (f2 (e2 i2)))"
   611     unfolding e2_surj[symmetric] setsum_reindex[OF e2_inj]
   612     unfolding M_def comp_def apply(intro setsum_cong) apply force
   613     by (metis e2_surj b1 h1 h2 imageI)
   614     also have "... = N1 b1" using b1 ct1[OF f1b1] by simp
   615     finally show "(\<Sum>b2\<in>B2. M (u b1 b2)) = N1 b1" .
   616   next
   617     fix b2 assume b2: "b2 \<in> B2"
   618     hence f2b2: "f2 b2 \<le> n2" using f2 unfolding bij_betw_def
   619     by (metis image_eqI lessThan_iff less_Suc_eq_le)
   620     have "(\<Sum>b1\<in>B1. M (u b1 b2)) = (\<Sum>i1<Suc n1. ct (f1 (e1 i1)) (f2 b2))"
   621     unfolding e1_surj[symmetric] setsum_reindex[OF e1_inj]
   622     unfolding M_def comp_def apply(intro setsum_cong) apply force
   623     by (metis e1_surj b2 h1 h2 imageI)
   624     also have "... = N2 b2" using b2 ct2[OF f2b2] by simp
   625     finally show "(\<Sum>b1\<in>B1. M (u b1 b2)) = N2 b2" .
   626   qed
   627 qed
   628 
   629 lemma supp_vimage_mmap: "set_of M \<subseteq> f -` (set_of (mmap f M))"
   630   by transfer (auto simp: multiset_def setsum_gt_0_iff)
   631 
   632 lemma mmap_ge_0: "b \<in># mmap f M \<longleftrightarrow> (\<exists>a. a \<in># M \<and> f a = b)"
   633   by transfer (auto simp: multiset_def setsum_gt_0_iff)
   634 
   635 lemma finite_twosets:
   636 assumes "finite B1" and "finite B2"
   637 shows "finite {u b1 b2 |b1 b2. b1 \<in> B1 \<and> b2 \<in> B2}"  (is "finite ?A")
   638 proof-
   639   have A: "?A = (\<lambda> b1b2. u (fst b1b2) (snd b1b2)) ` (B1 <*> B2)" by force
   640   show ?thesis unfolding A using finite_cartesian_product[OF assms] by auto
   641 qed
   642 
   643 lemma wpull_mmap:
   644 fixes A :: "'a set" and B1 :: "'b1 set" and B2 :: "'b2 set"
   645 assumes wp: "wpull A B1 B2 f1 f2 p1 p2"
   646 shows
   647 "wpull {M. set_of M \<subseteq> A}
   648        {N1. set_of N1 \<subseteq> B1} {N2. set_of N2 \<subseteq> B2}
   649        (mmap f1) (mmap f2) (mmap p1) (mmap p2)"
   650 unfolding wpull_def proof (safe, unfold Bex_def mem_Collect_eq)
   651   fix N1 :: "'b1 multiset" and N2 :: "'b2 multiset"
   652   assume mmap': "mmap f1 N1 = mmap f2 N2"
   653   and N1[simp]: "set_of N1 \<subseteq> B1"
   654   and N2[simp]: "set_of N2 \<subseteq> B2"
   655   def P \<equiv> "mmap f1 N1"
   656   have P1: "P = mmap f1 N1" and P2: "P = mmap f2 N2" unfolding P_def using mmap' by auto
   657   note P = P1 P2
   658   have fin_N1[simp]: "finite (set_of N1)"
   659    and fin_N2[simp]: "finite (set_of N2)"
   660    and fin_P[simp]: "finite (set_of P)" by auto
   661   (*  *)
   662   def set1 \<equiv> "\<lambda> c. {b1 \<in> set_of N1. f1 b1 = c}"
   663   have set1[simp]: "\<And> c b1. b1 \<in> set1 c \<Longrightarrow> f1 b1 = c" unfolding set1_def by auto
   664   have fin_set1: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (set1 c)"
   665     using N1(1) unfolding set1_def multiset_def by auto
   666   have set1_NE: "\<And> c. c \<in> set_of P \<Longrightarrow> set1 c \<noteq> {}"
   667    unfolding set1_def set_of_def P mmap_ge_0 by auto
   668   have supp_N1_set1: "set_of N1 = (\<Union> c \<in> set_of P. set1 c)"
   669     using supp_vimage_mmap[of N1 f1] unfolding set1_def P1 by auto
   670   hence set1_inclN1: "\<And>c. c \<in> set_of P \<Longrightarrow> set1 c \<subseteq> set_of N1" by auto
   671   hence set1_incl: "\<And> c. c \<in> set_of P \<Longrightarrow> set1 c \<subseteq> B1" using N1 by blast
   672   have set1_disj: "\<And> c c'. c \<noteq> c' \<Longrightarrow> set1 c \<inter> set1 c' = {}"
   673     unfolding set1_def by auto
   674   have setsum_set1: "\<And> c. setsum (count N1) (set1 c) = count P c"
   675     unfolding P1 set1_def by transfer (auto intro: setsum_cong)
   676   (*  *)
   677   def set2 \<equiv> "\<lambda> c. {b2 \<in> set_of N2. f2 b2 = c}"
   678   have set2[simp]: "\<And> c b2. b2 \<in> set2 c \<Longrightarrow> f2 b2 = c" unfolding set2_def by auto
   679   have fin_set2: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (set2 c)"
   680   using N2(1) unfolding set2_def multiset_def by auto
   681   have set2_NE: "\<And> c. c \<in> set_of P \<Longrightarrow> set2 c \<noteq> {}"
   682     unfolding set2_def P2 mmap_ge_0 set_of_def by auto
   683   have supp_N2_set2: "set_of N2 = (\<Union> c \<in> set_of P. set2 c)"
   684     using supp_vimage_mmap[of N2 f2] unfolding set2_def P2 by auto
   685   hence set2_inclN2: "\<And>c. c \<in> set_of P \<Longrightarrow> set2 c \<subseteq> set_of N2" by auto
   686   hence set2_incl: "\<And> c. c \<in> set_of P \<Longrightarrow> set2 c \<subseteq> B2" using N2 by blast
   687   have set2_disj: "\<And> c c'. c \<noteq> c' \<Longrightarrow> set2 c \<inter> set2 c' = {}"
   688     unfolding set2_def by auto
   689   have setsum_set2: "\<And> c. setsum (count N2) (set2 c) = count P c"
   690     unfolding P2 set2_def by transfer (auto intro: setsum_cong)
   691   (*  *)
   692   have ss: "\<And> c. c \<in> set_of P \<Longrightarrow> setsum (count N1) (set1 c) = setsum (count N2) (set2 c)"
   693     unfolding setsum_set1 setsum_set2 ..
   694   have "\<forall> c \<in> set_of P. \<forall> b1b2 \<in> (set1 c) \<times> (set2 c).
   695           \<exists> a \<in> A. p1 a = fst b1b2 \<and> p2 a = snd b1b2"
   696     using wp set1_incl set2_incl unfolding wpull_def Ball_def mem_Collect_eq
   697     by simp (metis set1 set2 set_rev_mp)
   698   then obtain uu where uu:
   699   "\<forall> c \<in> set_of P. \<forall> b1b2 \<in> (set1 c) \<times> (set2 c).
   700      uu c b1b2 \<in> A \<and> p1 (uu c b1b2) = fst b1b2 \<and> p2 (uu c b1b2) = snd b1b2" by metis
   701   def u \<equiv> "\<lambda> c b1 b2. uu c (b1,b2)"
   702   have u[simp]:
   703   "\<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"
   704   "\<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"
   705   "\<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"
   706     using uu unfolding u_def by auto
   707   {fix c assume c: "c \<in> set_of P"
   708    have "inj2 (u c) (set1 c) (set2 c)" unfolding inj2_def proof clarify
   709      fix b1 b1' b2 b2'
   710      assume "{b1, b1'} \<subseteq> set1 c" "{b2, b2'} \<subseteq> set2 c" and 0: "u c b1 b2 = u c b1' b2'"
   711      hence "p1 (u c b1 b2) = b1 \<and> p2 (u c b1 b2) = b2 \<and>
   712             p1 (u c b1' b2') = b1' \<and> p2 (u c b1' b2') = b2'"
   713      using u(2)[OF c] u(3)[OF c] by simp metis
   714      thus "b1 = b1' \<and> b2 = b2'" using 0 by auto
   715    qed
   716   } note inj = this
   717   def sset \<equiv> "\<lambda> c. {u c b1 b2 | b1 b2. b1 \<in> set1 c \<and> b2 \<in> set2 c}"
   718   have fin_sset[simp]: "\<And> c. c \<in> set_of P \<Longrightarrow> finite (sset c)" unfolding sset_def
   719     using fin_set1 fin_set2 finite_twosets by blast
   720   have sset_A: "\<And> c. c \<in> set_of P \<Longrightarrow> sset c \<subseteq> A" unfolding sset_def by auto
   721   {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
   722    then obtain b1 b2 where b1: "b1 \<in> set1 c" and b2: "b2 \<in> set2 c"
   723    and a: "a = u c b1 b2" unfolding sset_def by auto
   724    have "p1 a \<in> set1 c" and p2a: "p2 a \<in> set2 c"
   725    using ac a b1 b2 c u(2) u(3) by simp+
   726    hence "u c (p1 a) (p2 a) = a" unfolding a using b1 b2 inj[OF c]
   727    unfolding inj2_def by (metis c u(2) u(3))
   728   } note u_p12[simp] = this
   729   {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
   730    hence "p1 a \<in> set1 c" unfolding sset_def by auto
   731   }note p1[simp] = this
   732   {fix c a assume c: "c \<in> set_of P" and ac: "a \<in> sset c"
   733    hence "p2 a \<in> set2 c" unfolding sset_def by auto
   734   }note p2[simp] = this
   735   (*  *)
   736   {fix c assume c: "c \<in> set_of P"
   737    hence "\<exists> M. (\<forall> b1 \<in> set1 c. setsum (\<lambda> b2. M (u c b1 b2)) (set2 c) = count N1 b1) \<and>
   738                (\<forall> b2 \<in> set2 c. setsum (\<lambda> b1. M (u c b1 b2)) (set1 c) = count N2 b2)"
   739    unfolding sset_def
   740    using matrix_setsum_finite[OF set1_NE[OF c] fin_set1[OF c]
   741                                  set2_NE[OF c] fin_set2[OF c] inj[OF c] ss[OF c]] by auto
   742   }
   743   then obtain Ms where
   744   ss1: "\<And> c b1. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c\<rbrakk> \<Longrightarrow>
   745                    setsum (\<lambda> b2. Ms c (u c b1 b2)) (set2 c) = count N1 b1" and
   746   ss2: "\<And> c b2. \<lbrakk>c \<in> set_of P; b2 \<in> set2 c\<rbrakk> \<Longrightarrow>
   747                    setsum (\<lambda> b1. Ms c (u c b1 b2)) (set1 c) = count N2 b2"
   748   by metis
   749   def SET \<equiv> "\<Union> c \<in> set_of P. sset c"
   750   have fin_SET[simp]: "finite SET" unfolding SET_def apply(rule finite_UN_I) by auto
   751   have SET_A: "SET \<subseteq> A" unfolding SET_def using sset_A by blast
   752   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"
   753     unfolding SET_def sset_def by blast
   754   {fix c a assume c: "c \<in> set_of P" and a: "a \<in> SET" and p1a: "p1 a \<in> set1 c"
   755    then obtain c' where c': "c' \<in> set_of P" and ac': "a \<in> sset c'"
   756     unfolding SET_def by auto
   757    hence "p1 a \<in> set1 c'" unfolding sset_def by auto
   758    hence eq: "c = c'" using p1a c c' set1_disj by auto
   759    hence "a \<in> sset c" using ac' by simp
   760   } note p1_rev = this
   761   {fix c a assume c: "c \<in> set_of P" and a: "a \<in> SET" and p2a: "p2 a \<in> set2 c"
   762    then obtain c' where c': "c' \<in> set_of P" and ac': "a \<in> sset c'"
   763    unfolding SET_def by auto
   764    hence "p2 a \<in> set2 c'" unfolding sset_def by auto
   765    hence eq: "c = c'" using p2a c c' set2_disj by auto
   766    hence "a \<in> sset c" using ac' by simp
   767   } note p2_rev = this
   768   (*  *)
   769   have "\<forall> a \<in> SET. \<exists> c \<in> set_of P. a \<in> sset c" unfolding SET_def by auto
   770   then obtain h where h: "\<forall> a \<in> SET. h a \<in> set_of P \<and> a \<in> sset (h a)" by metis
   771   have h_u[simp]: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   772                       \<Longrightarrow> h (u c b1 b2) = c"
   773   by (metis h p2 set2 u(3) u_SET)
   774   have h_u1: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   775                       \<Longrightarrow> h (u c b1 b2) = f1 b1"
   776   using h unfolding sset_def by auto
   777   have h_u2: "\<And> c b1 b2. \<lbrakk>c \<in> set_of P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   778                       \<Longrightarrow> h (u c b1 b2) = f2 b2"
   779   using h unfolding sset_def by auto
   780   def M \<equiv>
   781     "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)"
   782   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"
   783     unfolding multiset_def by auto
   784   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"
   785     unfolding M_def pcr_multiset_def cr_multiset_def by (auto simp: Abs_multiset_inverse)
   786   have sM: "set_of M \<subseteq> SET" "set_of M \<subseteq> p1 -` (set_of N1)" "set_of M \<subseteq> p2 -` set_of N2"
   787     by (transfer, auto split: split_if_asm)+
   788   show "\<exists>M. set_of M \<subseteq> A \<and> mmap p1 M = N1 \<and> mmap p2 M = N2"
   789   proof(rule exI[of _ M], safe)
   790     fix a assume *: "a \<in> set_of M"
   791     from SET_A show "a \<in> A"
   792     proof (cases "a \<in> SET")
   793       case False thus ?thesis using * by transfer' auto
   794     qed blast
   795   next
   796     show "mmap p1 M = N1"
   797     proof(intro multiset_eqI)
   798       fix b1
   799       let ?K = "{a. p1 a = b1 \<and> a \<in># M}"
   800       have "setsum (count M) ?K = count N1 b1"
   801       proof(cases "b1 \<in> set_of N1")
   802         case False
   803         hence "?K = {}" using sM(2) by auto
   804         thus ?thesis using False by auto
   805       next
   806         case True
   807         def c \<equiv> "f1 b1"
   808         have c: "c \<in> set_of P" and b1: "b1 \<in> set1 c"
   809           unfolding set1_def c_def P1 using True by (auto simp: o_eq_dest[OF set_of_mmap])
   810         with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p1 a = b1 \<and> a \<in> SET}"
   811           by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
   812         also have "... = setsum (count M) ((\<lambda> b2. u c b1 b2) ` (set2 c))"
   813           apply(rule setsum_cong) using c b1 proof safe
   814           fix a assume p1a: "p1 a \<in> set1 c" and "c \<in> set_of P" and "a \<in> SET"
   815           hence ac: "a \<in> sset c" using p1_rev by auto
   816           hence "a = u c (p1 a) (p2 a)" using c by auto
   817           moreover have "p2 a \<in> set2 c" using ac c by auto
   818           ultimately show "a \<in> u c (p1 a) ` set2 c" by auto
   819         qed auto
   820         also have "... = setsum (\<lambda> b2. count M (u c b1 b2)) (set2 c)"
   821           unfolding comp_def[symmetric] apply(rule setsum_reindex)
   822           using inj unfolding inj_on_def inj2_def using b1 c u(3) by blast
   823         also have "... = count N1 b1" unfolding ss1[OF c b1, symmetric]
   824           apply(rule setsum_cong[OF refl]) apply (transfer fixing: Ms u c b1 set2)
   825           using True h_u[OF c b1] set2_def u(2,3)[OF c b1] u_SET[OF c b1] by fastforce
   826         finally show ?thesis .
   827       qed
   828       thus "count (mmap p1 M) b1 = count N1 b1" by transfer
   829     qed
   830   next
   831 next
   832     show "mmap p2 M = N2"
   833     proof(intro multiset_eqI)
   834       fix b2
   835       let ?K = "{a. p2 a = b2 \<and> a \<in># M}"
   836       have "setsum (count M) ?K = count N2 b2"
   837       proof(cases "b2 \<in> set_of N2")
   838         case False
   839         hence "?K = {}" using sM(3) by auto
   840         thus ?thesis using False by auto
   841       next
   842         case True
   843         def c \<equiv> "f2 b2"
   844         have c: "c \<in> set_of P" and b2: "b2 \<in> set2 c"
   845           unfolding set2_def c_def P2 using True by (auto simp: o_eq_dest[OF set_of_mmap])
   846         with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p2 a = b2 \<and> a \<in> SET}"
   847           by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
   848         also have "... = setsum (count M) ((\<lambda> b1. u c b1 b2) ` (set1 c))"
   849           apply(rule setsum_cong) using c b2 proof safe
   850           fix a assume p2a: "p2 a \<in> set2 c" and "c \<in> set_of P" and "a \<in> SET"
   851           hence ac: "a \<in> sset c" using p2_rev by auto
   852           hence "a = u c (p1 a) (p2 a)" using c by auto
   853           moreover have "p1 a \<in> set1 c" using ac c by auto
   854           ultimately show "a \<in> (\<lambda>x. u c x (p2 a)) ` set1 c" by auto
   855         qed auto
   856         also have "... = setsum (count M o (\<lambda> b1. u c b1 b2)) (set1 c)"
   857           apply(rule setsum_reindex)
   858           using inj unfolding inj_on_def inj2_def using b2 c u(2) by blast
   859         also have "... = setsum (\<lambda> b1. count M (u c b1 b2)) (set1 c)" by simp
   860         also have "... = count N2 b2" unfolding ss2[OF c b2, symmetric] o_def
   861           apply(rule setsum_cong[OF refl]) apply (transfer fixing: Ms u c b2 set1)
   862           using True h_u1[OF c _ b2] u(2,3)[OF c _ b2] u_SET[OF c _ b2] set1_def by fastforce
   863         finally show ?thesis .
   864       qed
   865       thus "count (mmap p2 M) b2 = count N2 b2" by transfer
   866     qed
   867   qed
   868 qed
   869 
   870 lemma set_of_bd: "|set_of x| \<le>o natLeq"
   871   by transfer
   872     (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def)
   873 
   874 bnf mmap [set_of] "\<lambda>_::'a multiset. natLeq" ["{#}"]
   875 by (auto simp add: mmap_id0 mmap_comp set_of_mmap natLeq_card_order natLeq_cinfinite set_of_bd
   876   intro: mmap_cong wpull_mmap)
   877 
   878 inductive multiset_rel' where
   879 Zero: "multiset_rel' R {#} {#}"
   880 |
   881 Plus: "\<lbrakk>R a b; multiset_rel' R M N\<rbrakk> \<Longrightarrow> multiset_rel' R (M + {#a#}) (N + {#b#})"
   882 
   883 lemma multiset_map_Zero_iff[simp]: "mmap f M = {#} \<longleftrightarrow> M = {#}"
   884 by (metis image_is_empty multiset.set_map set_of_eq_empty_iff)
   885 
   886 lemma multiset_map_Zero[simp]: "mmap f {#} = {#}" by simp
   887 
   888 lemma multiset_rel_Zero: "multiset_rel R {#} {#}"
   889 unfolding multiset_rel_def Grp_def by auto
   890 
   891 declare multiset.count[simp]
   892 declare Abs_multiset_inverse[simp]
   893 declare multiset.count_inverse[simp]
   894 declare union_preserves_multiset[simp]
   895 
   896 
   897 lemma multiset_map_Plus[simp]: "mmap f (M1 + M2) = mmap f M1 + mmap f M2"
   898 proof (intro multiset_eqI, transfer fixing: f)
   899   fix x :: 'a and M1 M2 :: "'b \<Rightarrow> nat"
   900   assume "M1 \<in> multiset" "M2 \<in> multiset"
   901   hence "setsum M1 {a. f a = x \<and> 0 < M1 a} = setsum M1 {a. f a = x \<and> 0 < M1 a + M2 a}"
   902         "setsum M2 {a. f a = x \<and> 0 < M2 a} = setsum M2 {a. f a = x \<and> 0 < M1 a + M2 a}"
   903     by (auto simp: multiset_def intro!: setsum_mono_zero_cong_left)
   904   then show "(\<Sum>a | f a = x \<and> 0 < M1 a + M2 a. M1 a + M2 a) =
   905        setsum M1 {a. f a = x \<and> 0 < M1 a} +
   906        setsum M2 {a. f a = x \<and> 0 < M2 a}"
   907     by (auto simp: setsum.distrib[symmetric])
   908 qed
   909 
   910 lemma multiset_map_singl[simp]: "mmap f {#a#} = {#f a#}"
   911   by transfer auto
   912 
   913 lemma multiset_rel_Plus:
   914 assumes ab: "R a b" and MN: "multiset_rel R M N"
   915 shows "multiset_rel R (M + {#a#}) (N + {#b#})"
   916 proof-
   917   {fix y assume "R a b" and "set_of y \<subseteq> {(x, y). R x y}"
   918    hence "\<exists>ya. mmap fst y + {#a#} = mmap fst ya \<and>
   919                mmap snd y + {#b#} = mmap snd ya \<and>
   920                set_of ya \<subseteq> {(x, y). R x y}"
   921    apply(intro exI[of _ "y + {#(a,b)#}"]) by auto
   922   }
   923   thus ?thesis
   924   using assms
   925   unfolding multiset_rel_def Grp_def by force
   926 qed
   927 
   928 lemma multiset_rel'_imp_multiset_rel:
   929 "multiset_rel' R M N \<Longrightarrow> multiset_rel R M N"
   930 apply(induct rule: multiset_rel'.induct)
   931 using multiset_rel_Zero multiset_rel_Plus by auto
   932 
   933 lemma mcard_mmap[simp]: "mcard (mmap f M) = mcard M"
   934 proof -
   935   def A \<equiv> "\<lambda> b. {a. f a = b \<and> a \<in># M}"
   936   let ?B = "{b. 0 < setsum (count M) (A b)}"
   937   have "{b. \<exists>a. f a = b \<and> a \<in># M} \<subseteq> f ` {a. a \<in># M}" by auto
   938   moreover have "finite (f ` {a. a \<in># M})" apply(rule finite_imageI)
   939   using finite_Collect_mem .
   940   ultimately have fin: "finite {b. \<exists>a. f a = b \<and> a \<in># M}" by(rule finite_subset)
   941   have i: "inj_on A ?B" unfolding inj_on_def A_def apply clarsimp
   942   by (metis (lifting, mono_tags) mem_Collect_eq rel_simps(54)
   943                                  setsum_gt_0_iff setsum_infinite)
   944   have 0: "\<And> b. 0 < setsum (count M) (A b) \<longleftrightarrow> (\<exists> a \<in> A b. count M a > 0)"
   945   apply safe
   946     apply (metis less_not_refl setsum_gt_0_iff setsum_infinite)
   947     by (metis A_def finite_Collect_conjI finite_Collect_mem setsum_gt_0_iff)
   948   hence AB: "A ` ?B = {A b | b. \<exists> a \<in> A b. count M a > 0}" by auto
   949 
   950   have "setsum (\<lambda> x. setsum (count M) (A x)) ?B = setsum (setsum (count M) o A) ?B"
   951   unfolding comp_def ..
   952   also have "... = (\<Sum>x\<in> A ` ?B. setsum (count M) x)"
   953   unfolding setsum.reindex [OF i, symmetric] ..
   954   also have "... = setsum (count M) (\<Union>x\<in>A ` {b. 0 < setsum (count M) (A b)}. x)"
   955   (is "_ = setsum (count M) ?J")
   956   apply(rule setsum.UNION_disjoint[symmetric])
   957   using 0 fin unfolding A_def by auto
   958   also have "?J = {a. a \<in># M}" unfolding AB unfolding A_def by auto
   959   finally have "setsum (\<lambda> x. setsum (count M) (A x)) ?B =
   960                 setsum (count M) {a. a \<in># M}" .
   961   then show ?thesis unfolding mcard_unfold_setsum A_def by transfer
   962 qed
   963 
   964 lemma multiset_rel_mcard:
   965 assumes "multiset_rel R M N"
   966 shows "mcard M = mcard N"
   967 using assms unfolding multiset_rel_def Grp_def by auto
   968 
   969 lemma multiset_induct2[case_names empty addL addR]:
   970 assumes empty: "P {#} {#}"
   971 and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
   972 and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
   973 shows "P M N"
   974 apply(induct N rule: multiset_induct)
   975   apply(induct M rule: multiset_induct, rule empty, erule addL)
   976   apply(induct M rule: multiset_induct, erule addR, erule addR)
   977 done
   978 
   979 lemma multiset_induct2_mcard[consumes 1, case_names empty add]:
   980 assumes c: "mcard M = mcard N"
   981 and empty: "P {#} {#}"
   982 and add: "\<And>M N a b. P M N \<Longrightarrow> P (M + {#a#}) (N + {#b#})"
   983 shows "P M N"
   984 using c proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
   985   case (less M)  show ?case
   986   proof(cases "M = {#}")
   987     case True hence "N = {#}" using less.prems by auto
   988     thus ?thesis using True empty by auto
   989   next
   990     case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
   991     have "N \<noteq> {#}" using False less.prems by auto
   992     then obtain N1 b where N: "N = N1 + {#b#}" by (metis multi_nonempty_split)
   993     have "mcard M1 = mcard N1" using less.prems unfolding M N by auto
   994     thus ?thesis using M N less.hyps add by auto
   995   qed
   996 qed
   997 
   998 lemma msed_map_invL:
   999 assumes "mmap f (M + {#a#}) = N"
  1000 shows "\<exists> N1. N = N1 + {#f a#} \<and> mmap f M = N1"
  1001 proof-
  1002   have "f a \<in># N"
  1003   using assms multiset.set_map[of f "M + {#a#}"] by auto
  1004   then obtain N1 where N: "N = N1 + {#f a#}" using multi_member_split by metis
  1005   have "mmap f M = N1" using assms unfolding N by simp
  1006   thus ?thesis using N by blast
  1007 qed
  1008 
  1009 lemma msed_map_invR:
  1010 assumes "mmap f M = N + {#b#}"
  1011 shows "\<exists> M1 a. M = M1 + {#a#} \<and> f a = b \<and> mmap f M1 = N"
  1012 proof-
  1013   obtain a where a: "a \<in># M" and fa: "f a = b"
  1014   using multiset.set_map[of f M] unfolding assms
  1015   by (metis image_iff mem_set_of_iff union_single_eq_member)
  1016   then obtain M1 where M: "M = M1 + {#a#}" using multi_member_split by metis
  1017   have "mmap f M1 = N" using assms unfolding M fa[symmetric] by simp
  1018   thus ?thesis using M fa by blast
  1019 qed
  1020 
  1021 lemma msed_rel_invL:
  1022 assumes "multiset_rel R (M + {#a#}) N"
  1023 shows "\<exists> N1 b. N = N1 + {#b#} \<and> R a b \<and> multiset_rel R M N1"
  1024 proof-
  1025   obtain K where KM: "mmap fst K = M + {#a#}"
  1026   and KN: "mmap snd K = N" and sK: "set_of K \<subseteq> {(a, b). R a b}"
  1027   using assms
  1028   unfolding multiset_rel_def Grp_def by auto
  1029   obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
  1030   and K1M: "mmap fst K1 = M" using msed_map_invR[OF KM] by auto
  1031   obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "mmap snd K1 = N1"
  1032   using msed_map_invL[OF KN[unfolded K]] by auto
  1033   have Rab: "R a (snd ab)" using sK a unfolding K by auto
  1034   have "multiset_rel R M N1" using sK K1M K1N1
  1035   unfolding K multiset_rel_def Grp_def by auto
  1036   thus ?thesis using N Rab by auto
  1037 qed
  1038 
  1039 lemma msed_rel_invR:
  1040 assumes "multiset_rel R M (N + {#b#})"
  1041 shows "\<exists> M1 a. M = M1 + {#a#} \<and> R a b \<and> multiset_rel R M1 N"
  1042 proof-
  1043   obtain K where KN: "mmap snd K = N + {#b#}"
  1044   and KM: "mmap fst K = M" and sK: "set_of K \<subseteq> {(a, b). R a b}"
  1045   using assms
  1046   unfolding multiset_rel_def Grp_def by auto
  1047   obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
  1048   and K1N: "mmap snd K1 = N" using msed_map_invR[OF KN] by auto
  1049   obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "mmap fst K1 = M1"
  1050   using msed_map_invL[OF KM[unfolded K]] by auto
  1051   have Rab: "R (fst ab) b" using sK b unfolding K by auto
  1052   have "multiset_rel R M1 N" using sK K1N K1M1
  1053   unfolding K multiset_rel_def Grp_def by auto
  1054   thus ?thesis using M Rab by auto
  1055 qed
  1056 
  1057 lemma multiset_rel_imp_multiset_rel':
  1058 assumes "multiset_rel R M N"
  1059 shows "multiset_rel' R M N"
  1060 using assms proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
  1061   case (less M)
  1062   have c: "mcard M = mcard N" using multiset_rel_mcard[OF less.prems] .
  1063   show ?case
  1064   proof(cases "M = {#}")
  1065     case True hence "N = {#}" using c by simp
  1066     thus ?thesis using True multiset_rel'.Zero by auto
  1067   next
  1068     case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
  1069     obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "multiset_rel R M1 N1"
  1070     using msed_rel_invL[OF less.prems[unfolded M]] by auto
  1071     have "multiset_rel' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp
  1072     thus ?thesis using multiset_rel'.Plus[of R a b, OF R] unfolding M N by simp
  1073   qed
  1074 qed
  1075 
  1076 lemma multiset_rel_multiset_rel':
  1077 "multiset_rel R M N = multiset_rel' R M N"
  1078 using  multiset_rel_imp_multiset_rel' multiset_rel'_imp_multiset_rel by auto
  1079 
  1080 (* The main end product for multiset_rel: inductive characterization *)
  1081 theorems multiset_rel_induct[case_names empty add, induct pred: multiset_rel] =
  1082          multiset_rel'.induct[unfolded multiset_rel_multiset_rel'[symmetric]]
  1083 
  1084 
  1085 
  1086 (* Advanced relator customization *)
  1087 
  1088 (* Set vs. sum relators: *)
  1089 (* FIXME: All such facts should be declared as simps: *)
  1090 declare sum_rel_simps[simp]
  1091 
  1092 lemma set_rel_sum_rel[simp]: 
  1093 "set_rel (sum_rel \<chi> \<phi>) A1 A2 \<longleftrightarrow> 
  1094  set_rel \<chi> (Inl -` A1) (Inl -` A2) \<and> set_rel \<phi> (Inr -` A1) (Inr -` A2)"
  1095 (is "?L \<longleftrightarrow> ?Rl \<and> ?Rr")
  1096 proof safe
  1097   assume L: "?L"
  1098   show ?Rl unfolding set_rel_def Bex_def vimage_eq proof safe
  1099     fix l1 assume "Inl l1 \<in> A1"
  1100     then obtain a2 where a2: "a2 \<in> A2" and "sum_rel \<chi> \<phi> (Inl l1) a2"
  1101     using L unfolding set_rel_def by auto
  1102     then obtain l2 where "a2 = Inl l2 \<and> \<chi> l1 l2" by (cases a2, auto)
  1103     thus "\<exists> l2. Inl l2 \<in> A2 \<and> \<chi> l1 l2" using a2 by auto
  1104   next
  1105     fix l2 assume "Inl l2 \<in> A2"
  1106     then obtain a1 where a1: "a1 \<in> A1" and "sum_rel \<chi> \<phi> a1 (Inl l2)"
  1107     using L unfolding set_rel_def by auto
  1108     then obtain l1 where "a1 = Inl l1 \<and> \<chi> l1 l2" by (cases a1, auto)
  1109     thus "\<exists> l1. Inl l1 \<in> A1 \<and> \<chi> l1 l2" using a1 by auto
  1110   qed
  1111   show ?Rr unfolding set_rel_def Bex_def vimage_eq proof safe
  1112     fix r1 assume "Inr r1 \<in> A1"
  1113     then obtain a2 where a2: "a2 \<in> A2" and "sum_rel \<chi> \<phi> (Inr r1) a2"
  1114     using L unfolding set_rel_def by auto
  1115     then obtain r2 where "a2 = Inr r2 \<and> \<phi> r1 r2" by (cases a2, auto)
  1116     thus "\<exists> r2. Inr r2 \<in> A2 \<and> \<phi> r1 r2" using a2 by auto
  1117   next
  1118     fix r2 assume "Inr r2 \<in> A2"
  1119     then obtain a1 where a1: "a1 \<in> A1" and "sum_rel \<chi> \<phi> a1 (Inr r2)"
  1120     using L unfolding set_rel_def by auto
  1121     then obtain r1 where "a1 = Inr r1 \<and> \<phi> r1 r2" by (cases a1, auto)
  1122     thus "\<exists> r1. Inr r1 \<in> A1 \<and> \<phi> r1 r2" using a1 by auto
  1123   qed
  1124 next
  1125   assume Rl: "?Rl" and Rr: "?Rr"
  1126   show ?L unfolding set_rel_def Bex_def vimage_eq proof safe
  1127     fix a1 assume a1: "a1 \<in> A1"
  1128     show "\<exists> a2. a2 \<in> A2 \<and> sum_rel \<chi> \<phi> a1 a2"
  1129     proof(cases a1)
  1130       case (Inl l1) then obtain l2 where "Inl l2 \<in> A2 \<and> \<chi> l1 l2"
  1131       using Rl a1 unfolding set_rel_def by blast
  1132       thus ?thesis unfolding Inl by auto
  1133     next
  1134       case (Inr r1) then obtain r2 where "Inr r2 \<in> A2 \<and> \<phi> r1 r2"
  1135       using Rr a1 unfolding set_rel_def by blast
  1136       thus ?thesis unfolding Inr by auto
  1137     qed
  1138   next
  1139     fix a2 assume a2: "a2 \<in> A2"
  1140     show "\<exists> a1. a1 \<in> A1 \<and> sum_rel \<chi> \<phi> a1 a2"
  1141     proof(cases a2)
  1142       case (Inl l2) then obtain l1 where "Inl l1 \<in> A1 \<and> \<chi> l1 l2"
  1143       using Rl a2 unfolding set_rel_def by blast
  1144       thus ?thesis unfolding Inl by auto
  1145     next
  1146       case (Inr r2) then obtain r1 where "Inr r1 \<in> A1 \<and> \<phi> r1 r2"
  1147       using Rr a2 unfolding set_rel_def by blast
  1148       thus ?thesis unfolding Inr by auto
  1149     qed
  1150   qed
  1151 qed
  1152 
  1153 
  1154 end