src/HOL/BNF/More_BNFs.thy
author blanchet
Tue Apr 30 13:34:31 2013 +0200 (2013-04-30)
changeset 51836 4d6dcd51dd52
parent 51782 84e7225f5ab6
child 51893 596baae88a88
permissions -rw-r--r--
renamed "bnf_def" keyword to "bnf" (since it's not a definition, but rather a registration)
     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   BNF_LFP
    16   BNF_GFP
    17   "~~/src/HOL/Quotient_Examples/Lift_FSet"
    18   "~~/src/HOL/Library/Multiset"
    19   Countable_Type
    20 begin
    21 
    22 lemma option_rec_conv_option_case: "option_rec = option_case"
    23 by (simp add: fun_eq_iff split: option.split)
    24 
    25 bnf Option.map [Option.set] "\<lambda>_::'a option. natLeq" ["None"] option_rel
    26 proof -
    27   show "Option.map id = id" by (simp add: fun_eq_iff Option.map_def split: option.split)
    28 next
    29   fix f g
    30   show "Option.map (g \<circ> f) = Option.map g \<circ> Option.map f"
    31     by (auto simp add: fun_eq_iff Option.map_def split: option.split)
    32 next
    33   fix f g x
    34   assume "\<And>z. z \<in> Option.set x \<Longrightarrow> f z = g z"
    35   thus "Option.map f x = Option.map g x"
    36     by (simp cong: Option.map_cong)
    37 next
    38   fix f
    39   show "Option.set \<circ> Option.map f = op ` f \<circ> Option.set"
    40     by fastforce
    41 next
    42   show "card_order natLeq" by (rule natLeq_card_order)
    43 next
    44   show "cinfinite natLeq" by (rule natLeq_cinfinite)
    45 next
    46   fix x
    47   show "|Option.set x| \<le>o natLeq"
    48     by (cases x) (simp_all add: ordLess_imp_ordLeq finite_iff_ordLess_natLeq[symmetric])
    49 next
    50   fix A
    51   have unfold: "{x. Option.set x \<subseteq> A} = Some ` A \<union> {None}"
    52     by (auto simp add: option_rec_conv_option_case Option.set_def split: option.split_asm)
    53   show "|{x. Option.set x \<subseteq> A}| \<le>o ( |A| +c ctwo) ^c natLeq"
    54     apply (rule ordIso_ordLeq_trans)
    55     apply (rule card_of_ordIso_subst[OF unfold])
    56     apply (rule ordLeq_transitive)
    57     apply (rule Un_csum)
    58     apply (rule ordLeq_transitive)
    59     apply (rule csum_mono)
    60     apply (rule card_of_image)
    61     apply (rule ordIso_ordLeq_trans)
    62     apply (rule single_cone)
    63     apply (rule cone_ordLeq_ctwo)
    64     apply (rule ordLeq_cexp1)
    65     apply (simp_all add: natLeq_cinfinite natLeq_Card_order cinfinite_not_czero Card_order_csum)
    66     done
    67 next
    68   fix A B1 B2 f1 f2 p1 p2
    69   assume wpull: "wpull A B1 B2 f1 f2 p1 p2"
    70   show "wpull {x. Option.set x \<subseteq> A} {x. Option.set x \<subseteq> B1} {x. Option.set x \<subseteq> B2}
    71     (Option.map f1) (Option.map f2) (Option.map p1) (Option.map p2)"
    72     (is "wpull ?A ?B1 ?B2 ?f1 ?f2 ?p1 ?p2")
    73     unfolding wpull_def
    74   proof (intro strip, elim conjE)
    75     fix b1 b2
    76     assume "b1 \<in> ?B1" "b2 \<in> ?B2" "?f1 b1 = ?f2 b2"
    77     thus "\<exists>a \<in> ?A. ?p1 a = b1 \<and> ?p2 a = b2" using wpull
    78       unfolding wpull_def by (cases b2) (auto 4 5)
    79   qed
    80 next
    81   fix z
    82   assume "z \<in> Option.set None"
    83   thus False by simp
    84 next
    85   fix R
    86   show "{p. option_rel (\<lambda>x y. (x, y) \<in> R) (fst p) (snd p)} =
    87         (Gr {x. Option.set x \<subseteq> R} (Option.map fst))\<inverse> O Gr {x. Option.set x \<subseteq> R} (Option.map snd)"
    88   unfolding option_rel_unfold Gr_def relcomp_unfold converse_unfold
    89   by (auto simp: trans[OF eq_commute option_map_is_None] trans[OF eq_commute option_map_eq_Some]
    90            split: option.splits) blast
    91 qed
    92 
    93 lemma card_of_list_in:
    94   "|{xs. set xs \<subseteq> A}| \<le>o |Pfunc (UNIV :: nat set) A|" (is "|?LHS| \<le>o |?RHS|")
    95 proof -
    96   let ?f = "%xs. %i. if i < length xs \<and> set xs \<subseteq> A then Some (nth xs i) else None"
    97   have "inj_on ?f ?LHS" unfolding inj_on_def fun_eq_iff
    98   proof safe
    99     fix xs :: "'a list" and ys :: "'a list"
   100     assume su: "set xs \<subseteq> A" "set ys \<subseteq> A" and eq: "\<forall>i. ?f xs i = ?f ys i"
   101     hence *: "length xs = length ys"
   102     by (metis linorder_cases option.simps(2) order_less_irrefl)
   103     thus "xs = ys" by (rule nth_equalityI) (metis * eq su option.inject)
   104   qed
   105   moreover have "?f ` ?LHS \<subseteq> ?RHS" unfolding Pfunc_def by fastforce
   106   ultimately show ?thesis using card_of_ordLeq by blast
   107 qed
   108 
   109 lemma list_in_empty: "A = {} \<Longrightarrow> {x. set x \<subseteq> A} = {[]}"
   110 by simp
   111 
   112 lemma card_of_Func: "|Func A B| =o |B| ^c |A|"
   113 unfolding cexp_def Field_card_of by (rule card_of_refl)
   114 
   115 lemma not_emp_czero_notIn_ordIso_Card_order:
   116 "A \<noteq> {} \<Longrightarrow> ( |A|, czero) \<notin> ordIso \<and> Card_order |A|"
   117   apply (rule conjI)
   118   apply (metis Field_card_of czeroE)
   119   by (rule card_of_Card_order)
   120 
   121 lemma list_in_bd: "|{x. set x \<subseteq> A}| \<le>o ( |A| +c ctwo) ^c natLeq"
   122 proof -
   123   fix A :: "'a set"
   124   show "|{x. set x \<subseteq> A}| \<le>o ( |A| +c ctwo) ^c natLeq"
   125   proof (cases "A = {}")
   126     case False thus ?thesis
   127       apply -
   128       apply (rule ordLeq_transitive)
   129       apply (rule card_of_list_in)
   130       apply (rule ordLeq_transitive)
   131       apply (erule card_of_Pfunc_Pow_Func)
   132       apply (rule ordIso_ordLeq_trans)
   133       apply (rule Times_cprod)
   134       apply (rule cprod_cinfinite_bound)
   135       apply (rule ordIso_ordLeq_trans)
   136       apply (rule Pow_cexp_ctwo)
   137       apply (rule ordIso_ordLeq_trans)
   138       apply (rule cexp_cong2)
   139       apply (rule card_of_nat)
   140       apply (rule Card_order_ctwo)
   141       apply (rule card_of_Card_order)
   142       apply (rule cexp_mono1)
   143       apply (rule ordLeq_csum2)
   144       apply (rule Card_order_ctwo)
   145       apply (rule natLeq_Card_order)
   146       apply (rule ordIso_ordLeq_trans)
   147       apply (rule card_of_Func)
   148       apply (rule ordIso_ordLeq_trans)
   149       apply (rule cexp_cong2)
   150       apply (rule card_of_nat)
   151       apply (rule card_of_Card_order)
   152       apply (rule card_of_Card_order)
   153       apply (rule cexp_mono1)
   154       apply (rule ordLeq_csum1)
   155       apply (rule card_of_Card_order)
   156       apply (rule natLeq_Card_order)
   157       apply (rule card_of_Card_order)
   158       apply (rule card_of_Card_order)
   159       apply (rule Cinfinite_cexp)
   160       apply (rule ordLeq_csum2)
   161       apply (rule Card_order_ctwo)
   162       apply (rule conjI)
   163       apply (rule natLeq_cinfinite)
   164       by (rule natLeq_Card_order)
   165   next
   166     case True thus ?thesis
   167       apply -
   168       apply (rule ordIso_ordLeq_trans)
   169       apply (rule card_of_ordIso_subst)
   170       apply (erule list_in_empty)
   171       apply (rule ordIso_ordLeq_trans)
   172       apply (rule single_cone)
   173       apply (rule cone_ordLeq_cexp)
   174       apply (rule ordLeq_transitive)
   175       apply (rule cone_ordLeq_ctwo)
   176       apply (rule ordLeq_csum2)
   177       by (rule Card_order_ctwo)
   178   qed
   179 qed
   180 
   181 lemma wpull_map:
   182   assumes "wpull A B1 B2 f1 f2 p1 p2"
   183   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)"
   184     (is "wpull ?A ?B1 ?B2 _ _ _ _")
   185 proof (unfold wpull_def)
   186   { fix as bs assume *: "as \<in> ?B1" "bs \<in> ?B2" "map f1 as = map f2 bs"
   187     hence "length as = length bs" by (metis length_map)
   188     hence "\<exists>zs \<in> ?A. map p1 zs = as \<and> map p2 zs = bs" using *
   189     proof (induct as bs rule: list_induct2)
   190       case (Cons a as b bs)
   191       hence "a \<in> B1" "b \<in> B2" "f1 a = f2 b" by auto
   192       with assms obtain z where "z \<in> A" "p1 z = a" "p2 z = b" unfolding wpull_def by blast
   193       moreover
   194       from Cons obtain zs where "zs \<in> ?A" "map p1 zs = as" "map p2 zs = bs" by auto
   195       ultimately have "z # zs \<in> ?A" "map p1 (z # zs) = a # as \<and> map p2 (z # zs) = b # bs" by auto
   196       thus ?case by (rule_tac x = "z # zs" in bexI)
   197     qed simp
   198   }
   199   thus "\<forall>as bs. as \<in> ?B1 \<and> bs \<in> ?B2 \<and> map f1 as = map f2 bs \<longrightarrow>
   200     (\<exists>zs \<in> ?A. map p1 zs = as \<and> map p2 zs = bs)" by blast
   201 qed
   202 
   203 bnf map [set] "\<lambda>_::'a list. natLeq" ["[]"]
   204 proof -
   205   show "map id = id" by (rule List.map.id)
   206 next
   207   fix f g
   208   show "map (g o f) = map g o map f" by (rule List.map.comp[symmetric])
   209 next
   210   fix x f g
   211   assume "\<And>z. z \<in> set x \<Longrightarrow> f z = g z"
   212   thus "map f x = map g x" by simp
   213 next
   214   fix f
   215   show "set o map f = image f o set" by (rule ext, unfold o_apply, rule set_map)
   216 next
   217   show "card_order natLeq" by (rule natLeq_card_order)
   218 next
   219   show "cinfinite natLeq" by (rule natLeq_cinfinite)
   220 next
   221   fix x
   222   show "|set x| \<le>o natLeq"
   223     apply (rule ordLess_imp_ordLeq)
   224     apply (rule finite_ordLess_infinite[OF _ natLeq_Well_order])
   225     unfolding Field_natLeq Field_card_of by (auto simp: card_of_well_order_on)
   226 next
   227   fix A :: "'a set"
   228   show "|{x. set x \<subseteq> A}| \<le>o ( |A| +c ctwo) ^c natLeq" by (rule list_in_bd)
   229 qed (simp add: wpull_map)+
   230 
   231 (* Finite sets *)
   232 
   233 definition fset_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'b fset \<Rightarrow> bool" where
   234 "fset_rel R a b \<longleftrightarrow>
   235  (\<forall>t \<in> fset a. \<exists>u \<in> fset b. R t u) \<and>
   236  (\<forall>t \<in> fset b. \<exists>u \<in> fset a. R u t)"
   237 
   238 
   239 lemma fset_to_fset: "finite A \<Longrightarrow> fset (the_inv fset A) = A"
   240   by (rule f_the_inv_into_f[unfolded inj_on_def])
   241     (transfer, simp,
   242      transfer, metis Collect_finite_eq_lists lists_UNIV mem_Collect_eq)
   243 
   244 
   245 lemma fset_rel_aux:
   246 "(\<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>
   247  (a, b) \<in> (Gr {a. fset a \<subseteq> {(a, b). R a b}} (fmap fst))\<inverse> O
   248           Gr {a. fset a \<subseteq> {(a, b). R a b}} (fmap snd)" (is "?L = ?R")
   249 proof
   250   assume ?L
   251   def R' \<equiv> "the_inv fset (Collect (split R) \<inter> (fset a \<times> fset b))" (is "the_inv fset ?L'")
   252   have "finite ?L'" by (intro finite_Int[OF disjI2] finite_cartesian_product) (transfer, simp)+
   253   hence *: "fset R' = ?L'" unfolding R'_def by (intro fset_to_fset)
   254   show ?R unfolding Gr_def relcomp_unfold converse_unfold
   255   proof (intro CollectI prod_caseI exI conjI)
   256     from * show "(R', a) = (R', fmap fst R')" using conjunct1[OF `?L`]
   257       by (clarify, transfer, auto simp add: image_def Int_def split: prod.splits)
   258     from * show "(R', b) = (R', fmap snd R')" using conjunct2[OF `?L`]
   259       by (clarify, transfer, auto simp add: image_def Int_def split: prod.splits)
   260   qed (auto simp add: *)
   261 next
   262   assume ?R thus ?L unfolding Gr_def relcomp_unfold converse_unfold
   263   apply (simp add: subset_eq Ball_def)
   264   apply (rule conjI)
   265   apply (transfer, clarsimp, metis snd_conv)
   266   by (transfer, clarsimp, metis fst_conv)
   267 qed
   268 
   269 lemma abs_fset_rep_fset[simp]: "abs_fset (rep_fset x) = x"
   270   by (rule Quotient_fset[unfolded Quotient_def, THEN conjunct1, rule_format])
   271 
   272 lemma wpull_image:
   273   assumes "wpull A B1 B2 f1 f2 p1 p2"
   274   shows "wpull (Pow A) (Pow B1) (Pow B2) (image f1) (image f2) (image p1) (image p2)"
   275 unfolding wpull_def Pow_def Bex_def mem_Collect_eq proof clarify
   276   fix Y1 Y2 assume Y1: "Y1 \<subseteq> B1" and Y2: "Y2 \<subseteq> B2" and EQ: "f1 ` Y1 = f2 ` Y2"
   277   def X \<equiv> "{a \<in> A. p1 a \<in> Y1 \<and> p2 a \<in> Y2}"
   278   show "\<exists>X\<subseteq>A. p1 ` X = Y1 \<and> p2 ` X = Y2"
   279   proof (rule exI[of _ X], intro conjI)
   280     show "p1 ` X = Y1"
   281     proof
   282       show "Y1 \<subseteq> p1 ` X"
   283       proof safe
   284         fix y1 assume y1: "y1 \<in> Y1"
   285         then obtain y2 where y2: "y2 \<in> Y2" and eq: "f1 y1 = f2 y2" using EQ by auto
   286         then obtain x where "x \<in> A" and "p1 x = y1" and "p2 x = y2"
   287         using assms y1 Y1 Y2 unfolding wpull_def by blast
   288         thus "y1 \<in> p1 ` X" unfolding X_def using y1 y2 by auto
   289       qed
   290     qed(unfold X_def, auto)
   291     show "p2 ` X = Y2"
   292     proof
   293       show "Y2 \<subseteq> p2 ` X"
   294       proof safe
   295         fix y2 assume y2: "y2 \<in> Y2"
   296         then obtain y1 where y1: "y1 \<in> Y1" and eq: "f1 y1 = f2 y2" using EQ by force
   297         then obtain x where "x \<in> A" and "p1 x = y1" and "p2 x = y2"
   298         using assms y2 Y1 Y2 unfolding wpull_def by blast
   299         thus "y2 \<in> p2 ` X" unfolding X_def using y1 y2 by auto
   300       qed
   301     qed(unfold X_def, auto)
   302   qed(unfold X_def, auto)
   303 qed
   304 
   305 lemma wpull_fmap:
   306   assumes "wpull A B1 B2 f1 f2 p1 p2"
   307   shows "wpull {x. fset x \<subseteq> A} {x. fset x \<subseteq> B1} {x. fset x \<subseteq> B2}
   308               (fmap f1) (fmap f2) (fmap p1) (fmap p2)"
   309 unfolding wpull_def Pow_def Bex_def mem_Collect_eq proof clarify
   310   fix y1 y2
   311   assume Y1: "fset y1 \<subseteq> B1" and Y2: "fset y2 \<subseteq> B2"
   312   assume "fmap f1 y1 = fmap f2 y2"
   313   hence EQ: "f1 ` (fset y1) = f2 ` (fset y2)" by transfer simp
   314   with Y1 Y2 obtain X where X: "X \<subseteq> A" and Y1: "p1 ` X = fset y1" and Y2: "p2 ` X = fset y2"
   315     using wpull_image[OF assms] unfolding wpull_def Pow_def
   316     by (auto elim!: allE[of _ "fset y1"] allE[of _ "fset y2"])
   317   have "\<forall> y1' \<in> fset y1. \<exists> x. x \<in> X \<and> y1' = p1 x" using Y1 by auto
   318   then obtain q1 where q1: "\<forall> y1' \<in> fset y1. q1 y1' \<in> X \<and> y1' = p1 (q1 y1')" by metis
   319   have "\<forall> y2' \<in> fset y2. \<exists> x. x \<in> X \<and> y2' = p2 x" using Y2 by auto
   320   then obtain q2 where q2: "\<forall> y2' \<in> fset y2. q2 y2' \<in> X \<and> y2' = p2 (q2 y2')" by metis
   321   def X' \<equiv> "q1 ` (fset y1) \<union> q2 ` (fset y2)"
   322   have X': "X' \<subseteq> A" and Y1: "p1 ` X' = fset y1" and Y2: "p2 ` X' = fset y2"
   323   using X Y1 Y2 q1 q2 unfolding X'_def by auto
   324   have fX': "finite X'" unfolding X'_def by transfer simp
   325   then obtain x where X'eq: "X' = fset x" by transfer (metis finite_list)
   326   show "\<exists>x. fset x \<subseteq> A \<and> fmap p1 x = y1 \<and> fmap p2 x = y2"
   327      using X' Y1 Y2 by (auto simp: X'eq intro!: exI[of _ "x"]) (transfer, simp)+
   328 qed
   329 
   330 bnf fmap [fset] "\<lambda>_::'a fset. natLeq" ["{||}"] fset_rel
   331 apply -
   332           apply transfer' apply simp
   333          apply transfer' apply simp
   334         apply transfer apply force
   335        apply transfer apply force
   336       apply (rule natLeq_card_order)
   337      apply (rule natLeq_cinfinite)
   338     apply transfer apply (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq finite_set)
   339    apply (rule ordLeq_transitive[OF surj_imp_ordLeq[of _ abs_fset] list_in_bd])
   340    apply (auto simp: fset_def intro!: image_eqI[of _ abs_fset]) []
   341   apply (erule wpull_fmap)
   342  apply (simp add: Gr_def relcomp_unfold converse_unfold fset_rel_def fset_rel_aux) 
   343 apply transfer apply simp
   344 done
   345 
   346 lemmas [simp] = fset.map_comp' fset.map_id' fset.set_map'
   347 
   348 lemma fset_rel_fset: "set_rel \<chi> (fset A1) (fset A2) = fset_rel \<chi> A1 A2"
   349   unfolding fset_rel_def set_rel_def by auto 
   350 
   351 (* Countable sets *)
   352 
   353 lemma card_of_countable_sets_range:
   354 fixes A :: "'a set"
   355 shows "|{X. X \<subseteq> A \<and> countable X \<and> X \<noteq> {}}| \<le>o |{f::nat \<Rightarrow> 'a. range f \<subseteq> A}|"
   356 apply(rule card_of_ordLeqI[of from_nat_into]) using inj_on_from_nat_into
   357 unfolding inj_on_def by auto
   358 
   359 lemma card_of_countable_sets_Func:
   360 "|{X. X \<subseteq> A \<and> countable X \<and> X \<noteq> {}}| \<le>o |A| ^c natLeq"
   361 using card_of_countable_sets_range card_of_Func_UNIV[THEN ordIso_symmetric]
   362 unfolding cexp_def Field_natLeq Field_card_of
   363 by (rule ordLeq_ordIso_trans)
   364 
   365 lemma ordLeq_countable_subsets:
   366 "|A| \<le>o |{X. X \<subseteq> A \<and> countable X}|"
   367 apply (rule card_of_ordLeqI[of "\<lambda> a. {a}"]) unfolding inj_on_def by auto
   368 
   369 lemma finite_countable_subset:
   370 "finite {X. X \<subseteq> A \<and> countable X} \<longleftrightarrow> finite A"
   371 apply default
   372  apply (erule contrapos_pp)
   373  apply (rule card_of_ordLeq_infinite)
   374  apply (rule ordLeq_countable_subsets)
   375  apply assumption
   376 apply (rule finite_Collect_conjI)
   377 apply (rule disjI1)
   378 by (erule finite_Collect_subsets)
   379 
   380 lemma card_of_countable_sets:
   381 "|{X. X \<subseteq> A \<and> countable X}| \<le>o ( |A| +c ctwo) ^c natLeq"
   382 (is "|?L| \<le>o _")
   383 proof(cases "finite A")
   384   let ?R = "Func (UNIV::nat set) (A <+> (UNIV::bool set))"
   385   case True hence "finite ?L" by simp
   386   moreover have "infinite ?R"
   387   apply(rule infinite_Func[of _ "Inr True" "Inr False"]) by auto
   388   ultimately show ?thesis unfolding cexp_def csum_def ctwo_def Field_natLeq Field_card_of
   389   apply(intro ordLess_imp_ordLeq) by (rule finite_ordLess_infinite2)
   390 next
   391   case False
   392   hence "|{X. X \<subseteq> A \<and> countable X}| =o |{X. X \<subseteq> A \<and> countable X} - {{}}|"
   393   by (intro card_of_infinite_diff_finitte finite.emptyI finite.insertI ordIso_symmetric)
   394      (unfold finite_countable_subset)
   395   also have "|{X. X \<subseteq> A \<and> countable X} - {{}}| \<le>o |A| ^c natLeq"
   396   using card_of_countable_sets_Func[of A] unfolding set_diff_eq by auto
   397   also have "|A| ^c natLeq \<le>o ( |A| +c ctwo) ^c natLeq"
   398   apply(rule cexp_mono1)
   399     apply(rule ordLeq_csum1, rule card_of_Card_order)
   400     by (rule natLeq_Card_order)
   401   finally show ?thesis .
   402 qed
   403 
   404 lemma rcset_to_rcset: "countable A \<Longrightarrow> rcset (the_inv rcset A) = A"
   405 apply (rule f_the_inv_into_f)
   406 unfolding inj_on_def rcset_inj using rcset_surj by auto
   407 
   408 lemma Collect_Int_Times:
   409 "{(x, y). R x y} \<inter> A \<times> B = {(x, y). R x y \<and> x \<in> A \<and> y \<in> B}"
   410 by auto
   411 
   412 lemma rcset_map': "rcset (cIm f x) = f ` rcset x"
   413 unfolding cIm_def[abs_def] by simp
   414 
   415 definition cset_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a cset \<Rightarrow> 'b cset \<Rightarrow> bool" where
   416 "cset_rel R a b \<longleftrightarrow>
   417  (\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and>
   418  (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t)"
   419 
   420 lemma cset_rel_aux:
   421 "(\<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>
   422  (a, b) \<in> (Gr {x. rcset x \<subseteq> {(a, b). R a b}} (cIm fst))\<inverse> O
   423           Gr {x. rcset x \<subseteq> {(a, b). R a b}} (cIm snd)" (is "?L = ?R")
   424 proof
   425   assume ?L
   426   def R' \<equiv> "the_inv rcset (Collect (split R) \<inter> (rcset a \<times> rcset b))"
   427   (is "the_inv rcset ?L'")
   428   have "countable ?L'" by auto
   429   hence *: "rcset R' = ?L'" unfolding R'_def using fset_to_fset by (intro rcset_to_rcset)
   430   show ?R unfolding Gr_def relcomp_unfold converse_unfold
   431   proof (intro CollectI prod_caseI exI conjI)
   432     have "rcset a = fst ` ({(x, y). R x y} \<inter> rcset a \<times> rcset b)" (is "_ = ?A")
   433     using conjunct1[OF `?L`] unfolding image_def by (auto simp add: Collect_Int_Times)
   434     hence "a = acset ?A" by (metis acset_rcset)
   435     thus "(R', a) = (R', cIm fst R')" unfolding cIm_def * by auto
   436     have "rcset b = snd ` ({(x, y). R x y} \<inter> rcset a \<times> rcset b)" (is "_ = ?B")
   437     using conjunct2[OF `?L`] unfolding image_def by (auto simp add: Collect_Int_Times)
   438     hence "b = acset ?B" by (metis acset_rcset)
   439     thus "(R', b) = (R', cIm snd R')" unfolding cIm_def * by auto
   440   qed (auto simp add: *)
   441 next
   442   assume ?R thus ?L unfolding Gr_def relcomp_unfold converse_unfold
   443   apply (simp add: subset_eq Ball_def)
   444   apply (rule conjI)
   445   apply (clarsimp, metis (lifting, no_types) rcset_map' image_iff surjective_pairing)
   446   apply (clarsimp)
   447   by (metis Domain.intros Range.simps rcset_map' fst_eq_Domain snd_eq_Range)
   448 qed
   449 
   450 bnf cIm [rcset] "\<lambda>_::'a cset. natLeq" ["cEmp"] cset_rel
   451 proof -
   452   show "cIm id = id" unfolding cIm_def[abs_def] id_def by auto
   453 next
   454   fix f g show "cIm (g \<circ> f) = cIm g \<circ> cIm f"
   455   unfolding cIm_def[abs_def] apply(rule ext) unfolding comp_def by auto
   456 next
   457   fix C f g assume eq: "\<And>a. a \<in> rcset C \<Longrightarrow> f a = g a"
   458   thus "cIm f C = cIm g C"
   459   unfolding cIm_def[abs_def] unfolding image_def by auto
   460 next
   461   fix f show "rcset \<circ> cIm f = op ` f \<circ> rcset" unfolding cIm_def[abs_def] by auto
   462 next
   463   show "card_order natLeq" by (rule natLeq_card_order)
   464 next
   465   show "cinfinite natLeq" by (rule natLeq_cinfinite)
   466 next
   467   fix C show "|rcset C| \<le>o natLeq" using rcset unfolding countable_card_le_natLeq .
   468 next
   469   fix A :: "'a set"
   470   have "|{Z. rcset Z \<subseteq> A}| \<le>o |acset ` {X. X \<subseteq> A \<and> countable X}|"
   471   apply(rule card_of_mono1) unfolding Pow_def image_def
   472   proof (rule Collect_mono, clarsimp)
   473     fix x
   474     assume "rcset x \<subseteq> A"
   475     hence "rcset x \<subseteq> A \<and> countable (rcset x) \<and> x = acset (rcset x)"
   476     using acset_rcset[of x] rcset[of x] by force
   477     thus "\<exists>y \<subseteq> A. countable y \<and> x = acset y" by blast
   478   qed
   479   also have "|acset ` {X. X \<subseteq> A \<and> countable X}| \<le>o |{X. X \<subseteq> A \<and> countable X}|"
   480   using card_of_image .
   481   also have "|{X. X \<subseteq> A \<and> countable X}| \<le>o ( |A| +c ctwo) ^c natLeq"
   482   using card_of_countable_sets .
   483   finally show "|{Z. rcset Z \<subseteq> A}| \<le>o ( |A| +c ctwo) ^c natLeq" .
   484 next
   485   fix A B1 B2 f1 f2 p1 p2
   486   assume wp: "wpull A B1 B2 f1 f2 p1 p2"
   487   show "wpull {x. rcset x \<subseteq> A} {x. rcset x \<subseteq> B1} {x. rcset x \<subseteq> B2}
   488               (cIm f1) (cIm f2) (cIm p1) (cIm p2)"
   489   unfolding wpull_def proof safe
   490     fix y1 y2
   491     assume Y1: "rcset y1 \<subseteq> B1" and Y2: "rcset y2 \<subseteq> B2"
   492     assume "cIm f1 y1 = cIm f2 y2"
   493     hence EQ: "f1 ` (rcset y1) = f2 ` (rcset y2)"
   494     unfolding cIm_def by auto
   495     with Y1 Y2 obtain X where X: "X \<subseteq> A"
   496     and Y1: "p1 ` X = rcset y1" and Y2: "p2 ` X = rcset y2"
   497     using wpull_image[OF wp] unfolding wpull_def Pow_def
   498     unfolding Bex_def mem_Collect_eq apply -
   499     apply(erule allE[of _ "rcset y1"], erule allE[of _ "rcset y2"]) by auto
   500     have "\<forall> y1' \<in> rcset y1. \<exists> x. x \<in> X \<and> y1' = p1 x" using Y1 by auto
   501     then obtain q1 where q1: "\<forall> y1' \<in> rcset y1. q1 y1' \<in> X \<and> y1' = p1 (q1 y1')" by metis
   502     have "\<forall> y2' \<in> rcset y2. \<exists> x. x \<in> X \<and> y2' = p2 x" using Y2 by auto
   503     then obtain q2 where q2: "\<forall> y2' \<in> rcset y2. q2 y2' \<in> X \<and> y2' = p2 (q2 y2')" by metis
   504     def X' \<equiv> "q1 ` (rcset y1) \<union> q2 ` (rcset y2)"
   505     have X': "X' \<subseteq> A" and Y1: "p1 ` X' = rcset y1" and Y2: "p2 ` X' = rcset y2"
   506     using X Y1 Y2 q1 q2 unfolding X'_def by fast+
   507     have fX': "countable X'" unfolding X'_def by simp
   508     then obtain x where X'eq: "X' = rcset x" by (metis rcset_acset)
   509     show "\<exists>x\<in>{x. rcset x \<subseteq> A}. cIm p1 x = y1 \<and> cIm p2 x = y2"
   510     apply(intro bexI[of _ "x"]) using X' Y1 Y2 unfolding X'eq cIm_def by auto
   511   qed
   512 next
   513   fix R
   514   show "{p. cset_rel (\<lambda>x y. (x, y) \<in> R) (fst p) (snd p)} =
   515         (Gr {x. rcset x \<subseteq> R} (cIm fst))\<inverse> O Gr {x. rcset x \<subseteq> R} (cIm snd)"
   516   unfolding cset_rel_def cset_rel_aux by simp
   517 qed (unfold cEmp_def, auto)
   518 
   519 
   520 (* Multisets *)
   521 
   522 lemma setsum_gt_0_iff:
   523 fixes f :: "'a \<Rightarrow> nat" assumes "finite A"
   524 shows "setsum f A > 0 \<longleftrightarrow> (\<exists> a \<in> A. f a > 0)"
   525 (is "?L \<longleftrightarrow> ?R")
   526 proof-
   527   have "?L \<longleftrightarrow> \<not> setsum f A = 0" by fast
   528   also have "... \<longleftrightarrow> (\<exists> a \<in> A. f a \<noteq> 0)" using assms by simp
   529   also have "... \<longleftrightarrow> ?R" by simp
   530   finally show ?thesis .
   531 qed
   532 
   533 (*   *)
   534 definition mmap :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> nat) \<Rightarrow> 'b \<Rightarrow> nat" where
   535 "mmap h f b = setsum f {a. h a = b \<and> f a > 0}"
   536 
   537 lemma mmap_id: "mmap id = id"
   538 proof (rule ext)+
   539   fix f a show "mmap id f a = id f a"
   540   proof(cases "f a = 0")
   541     case False
   542     hence 1: "{aa. aa = a \<and> 0 < f aa} = {a}" by auto
   543     show ?thesis by (simp add: mmap_def id_apply 1)
   544   qed(unfold mmap_def, auto)
   545 qed
   546 
   547 lemma inj_on_setsum_inv:
   548 assumes f: "f \<in> multiset"
   549 and 1: "(0::nat) < setsum f {a. h a = b' \<and> 0 < f a}" (is "0 < setsum f ?A'")
   550 and 2: "{a. h a = b \<and> 0 < f a} = {a. h a = b' \<and> 0 < f a}" (is "?A = ?A'")
   551 shows "b = b'"
   552 proof-
   553   have "finite ?A'" using f unfolding multiset_def by auto
   554   hence "?A' \<noteq> {}" using 1 by (auto simp add: setsum_gt_0_iff)
   555   thus ?thesis using 2 by auto
   556 qed
   557 
   558 lemma mmap_comp:
   559 fixes h1 :: "'a \<Rightarrow> 'b" and h2 :: "'b \<Rightarrow> 'c"
   560 assumes f: "f \<in> multiset"
   561 shows "mmap (h2 o h1) f = (mmap h2 o mmap h1) f"
   562 unfolding mmap_def[abs_def] comp_def proof(rule ext)+
   563   fix c :: 'c
   564   let ?A = "{a. h2 (h1 a) = c \<and> 0 < f a}"
   565   let ?As = "\<lambda> b. {a. h1 a = b \<and> 0 < f a}"
   566   let ?B = "{b. h2 b = c \<and> 0 < setsum f (?As b)}"
   567   have 0: "{?As b | b.  b \<in> ?B} = ?As ` ?B" by auto
   568   have "\<And> b. finite (?As b)" using f unfolding multiset_def by simp
   569   hence "?B = {b. h2 b = c \<and> ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
   570   hence A: "?A = \<Union> {?As b | b.  b \<in> ?B}" by auto
   571   have "setsum f ?A = setsum (setsum f) {?As b | b.  b \<in> ?B}"
   572   unfolding A apply(rule setsum_Union_disjoint)
   573   using f unfolding multiset_def by auto
   574   also have "... = setsum (setsum f) (?As ` ?B)" unfolding 0 ..
   575   also have "... = setsum (setsum f o ?As) ?B" apply(rule setsum_reindex)
   576   unfolding inj_on_def apply auto using inj_on_setsum_inv[OF f, of h1] by blast
   577   also have "... = setsum (\<lambda> b. setsum f (?As b)) ?B" unfolding comp_def ..
   578   finally show "setsum f ?A = setsum (\<lambda> b. setsum f (?As b)) ?B" .
   579 qed
   580 
   581 lemma mmap_comp1:
   582 fixes h1 :: "'a \<Rightarrow> 'b" and h2 :: "'b \<Rightarrow> 'c"
   583 assumes "f \<in> multiset"
   584 shows "mmap (\<lambda> a. h2 (h1 a)) f = mmap h2 (mmap h1 f)"
   585 using mmap_comp[OF assms] unfolding comp_def by auto
   586 
   587 lemma mmap:
   588 assumes "f \<in> multiset"
   589 shows "mmap h f \<in> multiset"
   590 using assms unfolding mmap_def[abs_def] multiset_def proof safe
   591   assume fin: "finite {a. 0 < f a}"  (is "finite ?A")
   592   show "finite {b. 0 < setsum f {a. h a = b \<and> 0 < f a}}"
   593   (is "finite {b. 0 < setsum f (?As b)}")
   594   proof- let ?B = "{b. 0 < setsum f (?As b)}"
   595     have "\<And> b. finite (?As b)" using assms unfolding multiset_def by simp
   596     hence B: "?B = {b. ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
   597     hence "?B \<subseteq> h ` ?A" by auto
   598     thus ?thesis using finite_surj[OF fin] by auto
   599   qed
   600 qed
   601 
   602 lemma mmap_cong:
   603 assumes "\<And>a. a \<in># M \<Longrightarrow> f a = g a"
   604 shows "mmap f (count M) = mmap g (count M)"
   605 using assms unfolding mmap_def[abs_def] by (intro ext, intro setsum_cong) auto
   606 
   607 abbreviation supp where "supp f \<equiv> {a. f a > 0}"
   608 
   609 lemma mmap_image_comp:
   610 assumes f: "f \<in> multiset"
   611 shows "(supp o mmap h) f = (image h o supp) f"
   612 unfolding mmap_def[abs_def] comp_def proof-
   613   have "\<And> b. finite {a. h a = b \<and> 0 < f a}" (is "\<And> b. finite (?As b)")
   614   using f unfolding multiset_def by auto
   615   thus "{b. 0 < setsum f (?As b)} = h ` {a. 0 < f a}"
   616   by (auto simp add:  setsum_gt_0_iff)
   617 qed
   618 
   619 lemma mmap_image:
   620 assumes f: "f \<in> multiset"
   621 shows "supp (mmap h f) = h ` (supp f)"
   622 using mmap_image_comp[OF assms] unfolding comp_def .
   623 
   624 lemma set_of_Abs_multiset:
   625 assumes f: "f \<in> multiset"
   626 shows "set_of (Abs_multiset f) = supp f"
   627 using assms unfolding set_of_def by (auto simp: Abs_multiset_inverse)
   628 
   629 lemma supp_count:
   630 "supp (count M) = set_of M"
   631 using assms unfolding set_of_def by auto
   632 
   633 lemma multiset_of_surj:
   634 "multiset_of ` {as. set as \<subseteq> A} = {M. set_of M \<subseteq> A}"
   635 proof safe
   636   fix M assume M: "set_of M \<subseteq> A"
   637   obtain as where eq: "M = multiset_of as" using surj_multiset_of unfolding surj_def by auto
   638   hence "set as \<subseteq> A" using M by auto
   639   thus "M \<in> multiset_of ` {as. set as \<subseteq> A}" using eq by auto
   640 next
   641   show "\<And>x xa xb. \<lbrakk>set xa \<subseteq> A; xb \<in> set_of (multiset_of xa)\<rbrakk> \<Longrightarrow> xb \<in> A"
   642   by (erule set_mp) (unfold set_of_multiset_of)
   643 qed
   644 
   645 lemma card_of_set_of:
   646 "|{M. set_of M \<subseteq> A}| \<le>o |{as. set as \<subseteq> A}|"
   647 apply(rule card_of_ordLeqI2[of _ multiset_of]) using multiset_of_surj by auto
   648 
   649 lemma nat_sum_induct:
   650 assumes "\<And>n1 n2. (\<And> m1 m2. m1 + m2 < n1 + n2 \<Longrightarrow> phi m1 m2) \<Longrightarrow> phi n1 n2"
   651 shows "phi (n1::nat) (n2::nat)"
   652 proof-
   653   let ?chi = "\<lambda> n1n2 :: nat * nat. phi (fst n1n2) (snd n1n2)"
   654   have "?chi (n1,n2)"
   655   apply(induct rule: measure_induct[of "\<lambda> n1n2. fst n1n2 + snd n1n2" ?chi])
   656   using assms by (metis fstI sndI)
   657   thus ?thesis by simp
   658 qed
   659 
   660 lemma matrix_count:
   661 fixes ct1 ct2 :: "nat \<Rightarrow> nat"
   662 assumes "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
   663 shows
   664 "\<exists> ct. (\<forall> i1 \<le> n1. setsum (\<lambda> i2. ct i1 i2) {..<Suc n2} = ct1 i1) \<and>
   665        (\<forall> i2 \<le> n2. setsum (\<lambda> i1. ct i1 i2) {..<Suc n1} = ct2 i2)"
   666 (is "?phi ct1 ct2 n1 n2")
   667 proof-
   668   have "\<forall> ct1 ct2 :: nat \<Rightarrow> nat.
   669         setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} \<longrightarrow> ?phi ct1 ct2 n1 n2"
   670   proof(induct rule: nat_sum_induct[of
   671 "\<lambda> n1 n2. \<forall> ct1 ct2 :: nat \<Rightarrow> nat.
   672      setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2} \<longrightarrow> ?phi ct1 ct2 n1 n2"],
   673       clarify)
   674   fix n1 n2 :: nat and ct1 ct2 :: "nat \<Rightarrow> nat"
   675   assume IH: "\<And> m1 m2. m1 + m2 < n1 + n2 \<Longrightarrow>
   676                 \<forall> dt1 dt2 :: nat \<Rightarrow> nat.
   677                 setsum dt1 {..<Suc m1} = setsum dt2 {..<Suc m2} \<longrightarrow> ?phi dt1 dt2 m1 m2"
   678   and ss: "setsum ct1 {..<Suc n1} = setsum ct2 {..<Suc n2}"
   679   show "?phi ct1 ct2 n1 n2"
   680   proof(cases n1)
   681     case 0 note n1 = 0
   682     show ?thesis
   683     proof(cases n2)
   684       case 0 note n2 = 0
   685       let ?ct = "\<lambda> i1 i2. ct2 0"
   686       show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by simp
   687     next
   688       case (Suc m2) note n2 = Suc
   689       let ?ct = "\<lambda> i1 i2. ct2 i2"
   690       show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
   691     qed
   692   next
   693     case (Suc m1) note n1 = Suc
   694     show ?thesis
   695     proof(cases n2)
   696       case 0 note n2 = 0
   697       let ?ct = "\<lambda> i1 i2. ct1 i1"
   698       show ?thesis apply(rule exI[of _ ?ct]) using n1 n2 ss by auto
   699     next
   700       case (Suc m2) note n2 = Suc
   701       show ?thesis
   702       proof(cases "ct1 n1 \<le> ct2 n2")
   703         case True
   704         def dt2 \<equiv> "\<lambda> i2. if i2 = n2 then ct2 i2 - ct1 n1 else ct2 i2"
   705         have "setsum ct1 {..<Suc m1} = setsum dt2 {..<Suc n2}"
   706         unfolding dt2_def using ss n1 True by auto
   707         hence "?phi ct1 dt2 m1 n2" using IH[of m1 n2] n1 by simp
   708         then obtain dt where
   709         1: "\<And> i1. i1 \<le> m1 \<Longrightarrow> setsum (\<lambda> i2. dt i1 i2) {..<Suc n2} = ct1 i1" and
   710         2: "\<And> i2. i2 \<le> n2 \<Longrightarrow> setsum (\<lambda> i1. dt i1 i2) {..<Suc m1} = dt2 i2" by auto
   711         let ?ct = "\<lambda> i1 i2. if i1 = n1 then (if i2 = n2 then ct1 n1 else 0)
   712                                        else dt i1 i2"
   713         show ?thesis apply(rule exI[of _ ?ct])
   714         using n1 n2 1 2 True unfolding dt2_def by simp
   715       next
   716         case False
   717         hence False: "ct2 n2 < ct1 n1" by simp
   718         def dt1 \<equiv> "\<lambda> i1. if i1 = n1 then ct1 i1 - ct2 n2 else ct1 i1"
   719         have "setsum dt1 {..<Suc n1} = setsum ct2 {..<Suc m2}"
   720         unfolding dt1_def using ss n2 False by auto
   721         hence "?phi dt1 ct2 n1 m2" using IH[of n1 m2] n2 by simp
   722         then obtain dt where
   723         1: "\<And> i1. i1 \<le> n1 \<Longrightarrow> setsum (\<lambda> i2. dt i1 i2) {..<Suc m2} = dt1 i1" and
   724         2: "\<And> i2. i2 \<le> m2 \<Longrightarrow> setsum (\<lambda> i1. dt i1 i2) {..<Suc n1} = ct2 i2" by force
   725         let ?ct = "\<lambda> i1 i2. if i2 = n2 then (if i1 = n1 then ct2 n2 else 0)
   726                                        else dt i1 i2"
   727         show ?thesis apply(rule exI[of _ ?ct])
   728         using n1 n2 1 2 False unfolding dt1_def by simp
   729       qed
   730     qed
   731   qed
   732   qed
   733   thus ?thesis using assms by auto
   734 qed
   735 
   736 definition
   737 "inj2 u B1 B2 \<equiv>
   738  \<forall> b1 b1' b2 b2'. {b1,b1'} \<subseteq> B1 \<and> {b2,b2'} \<subseteq> B2 \<and> u b1 b2 = u b1' b2'
   739                   \<longrightarrow> b1 = b1' \<and> b2 = b2'"
   740 
   741 lemma matrix_setsum_finite:
   742 assumes B1: "B1 \<noteq> {}" "finite B1" and B2: "B2 \<noteq> {}" "finite B2" and u: "inj2 u B1 B2"
   743 and ss: "setsum N1 B1 = setsum N2 B2"
   744 shows "\<exists> M :: 'a \<Rightarrow> nat.
   745             (\<forall> b1 \<in> B1. setsum (\<lambda> b2. M (u b1 b2)) B2 = N1 b1) \<and>
   746             (\<forall> b2 \<in> B2. setsum (\<lambda> b1. M (u b1 b2)) B1 = N2 b2)"
   747 proof-
   748   obtain n1 where "card B1 = Suc n1" using B1 by (metis card_insert finite.simps)
   749   then obtain e1 where e1: "bij_betw e1 {..<Suc n1} B1"
   750   using ex_bij_betw_finite_nat[OF B1(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
   751   hence e1_inj: "inj_on e1 {..<Suc n1}" and e1_surj: "e1 ` {..<Suc n1} = B1"
   752   unfolding bij_betw_def by auto
   753   def f1 \<equiv> "inv_into {..<Suc n1} e1"
   754   have f1: "bij_betw f1 B1 {..<Suc n1}"
   755   and f1e1[simp]: "\<And> i1. i1 < Suc n1 \<Longrightarrow> f1 (e1 i1) = i1"
   756   and e1f1[simp]: "\<And> b1. b1 \<in> B1 \<Longrightarrow> e1 (f1 b1) = b1" unfolding f1_def
   757   apply (metis bij_betw_inv_into e1, metis bij_betw_inv_into_left e1 lessThan_iff)
   758   by (metis e1_surj f_inv_into_f)
   759   (*  *)
   760   obtain n2 where "card B2 = Suc n2" using B2 by (metis card_insert finite.simps)
   761   then obtain e2 where e2: "bij_betw e2 {..<Suc n2} B2"
   762   using ex_bij_betw_finite_nat[OF B2(2)] by (metis atLeast0LessThan bij_betw_the_inv_into)
   763   hence e2_inj: "inj_on e2 {..<Suc n2}" and e2_surj: "e2 ` {..<Suc n2} = B2"
   764   unfolding bij_betw_def by auto
   765   def f2 \<equiv> "inv_into {..<Suc n2} e2"
   766   have f2: "bij_betw f2 B2 {..<Suc n2}"
   767   and f2e2[simp]: "\<And> i2. i2 < Suc n2 \<Longrightarrow> f2 (e2 i2) = i2"
   768   and e2f2[simp]: "\<And> b2. b2 \<in> B2 \<Longrightarrow> e2 (f2 b2) = b2" unfolding f2_def
   769   apply (metis bij_betw_inv_into e2, metis bij_betw_inv_into_left e2 lessThan_iff)
   770   by (metis e2_surj f_inv_into_f)
   771   (*  *)
   772   let ?ct1 = "N1 o e1"  let ?ct2 = "N2 o e2"
   773   have ss: "setsum ?ct1 {..<Suc n1} = setsum ?ct2 {..<Suc n2}"
   774   unfolding setsum_reindex[OF e1_inj, symmetric] setsum_reindex[OF e2_inj, symmetric]
   775   e1_surj e2_surj using ss .
   776   obtain ct where
   777   ct1: "\<And> i1. i1 \<le> n1 \<Longrightarrow> setsum (\<lambda> i2. ct i1 i2) {..<Suc n2} = ?ct1 i1" and
   778   ct2: "\<And> i2. i2 \<le> n2 \<Longrightarrow> setsum (\<lambda> i1. ct i1 i2) {..<Suc n1} = ?ct2 i2"
   779   using matrix_count[OF ss] by blast
   780   (*  *)
   781   def A \<equiv> "{u b1 b2 | b1 b2. b1 \<in> B1 \<and> b2 \<in> B2}"
   782   have "\<forall> a \<in> A. \<exists> b1b2 \<in> B1 <*> B2. u (fst b1b2) (snd b1b2) = a"
   783   unfolding A_def Ball_def mem_Collect_eq by auto
   784   then obtain h1h2 where h12:
   785   "\<And>a. a \<in> A \<Longrightarrow> u (fst (h1h2 a)) (snd (h1h2 a)) = a \<and> h1h2 a \<in> B1 <*> B2" by metis
   786   def h1 \<equiv> "fst o h1h2"  def h2 \<equiv> "snd o h1h2"
   787   have h12[simp]: "\<And>a. a \<in> A \<Longrightarrow> u (h1 a) (h2 a) = a"
   788                   "\<And> a. a \<in> A \<Longrightarrow> h1 a \<in> B1"  "\<And> a. a \<in> A \<Longrightarrow> h2 a \<in> B2"
   789   using h12 unfolding h1_def h2_def by force+
   790   {fix b1 b2 assume b1: "b1 \<in> B1" and b2: "b2 \<in> B2"
   791    hence inA: "u b1 b2 \<in> A" unfolding A_def by auto
   792    hence "u b1 b2 = u (h1 (u b1 b2)) (h2 (u b1 b2))" by auto
   793    moreover have "h1 (u b1 b2) \<in> B1" "h2 (u b1 b2) \<in> B2" using inA by auto
   794    ultimately have "h1 (u b1 b2) = b1 \<and> h2 (u b1 b2) = b2"
   795    using u b1 b2 unfolding inj2_def by fastforce
   796   }
   797   hence h1[simp]: "\<And> b1 b2. \<lbrakk>b1 \<in> B1; b2 \<in> B2\<rbrakk> \<Longrightarrow> h1 (u b1 b2) = b1" and
   798         h2[simp]: "\<And> b1 b2. \<lbrakk>b1 \<in> B1; b2 \<in> B2\<rbrakk> \<Longrightarrow> h2 (u b1 b2) = b2" by auto
   799   def M \<equiv> "\<lambda> a. ct (f1 (h1 a)) (f2 (h2 a))"
   800   show ?thesis
   801   apply(rule exI[of _ M]) proof safe
   802     fix b1 assume b1: "b1 \<in> B1"
   803     hence f1b1: "f1 b1 \<le> n1" using f1 unfolding bij_betw_def
   804     by (metis bij_betwE f1 lessThan_iff less_Suc_eq_le)
   805     have "(\<Sum>b2\<in>B2. M (u b1 b2)) = (\<Sum>i2<Suc n2. ct (f1 b1) (f2 (e2 i2)))"
   806     unfolding e2_surj[symmetric] setsum_reindex[OF e2_inj]
   807     unfolding M_def comp_def apply(intro setsum_cong) apply force
   808     by (metis e2_surj b1 h1 h2 imageI)
   809     also have "... = N1 b1" using b1 ct1[OF f1b1] by simp
   810     finally show "(\<Sum>b2\<in>B2. M (u b1 b2)) = N1 b1" .
   811   next
   812     fix b2 assume b2: "b2 \<in> B2"
   813     hence f2b2: "f2 b2 \<le> n2" using f2 unfolding bij_betw_def
   814     by (metis bij_betwE f2 lessThan_iff less_Suc_eq_le)
   815     have "(\<Sum>b1\<in>B1. M (u b1 b2)) = (\<Sum>i1<Suc n1. ct (f1 (e1 i1)) (f2 b2))"
   816     unfolding e1_surj[symmetric] setsum_reindex[OF e1_inj]
   817     unfolding M_def comp_def apply(intro setsum_cong) apply force
   818     by (metis e1_surj b2 h1 h2 imageI)
   819     also have "... = N2 b2" using b2 ct2[OF f2b2] by simp
   820     finally show "(\<Sum>b1\<in>B1. M (u b1 b2)) = N2 b2" .
   821   qed
   822 qed
   823 
   824 lemma supp_vimage_mmap:
   825 assumes "M \<in> multiset"
   826 shows "supp M \<subseteq> f -` (supp (mmap f M))"
   827 using assms by (auto simp: mmap_image)
   828 
   829 lemma mmap_ge_0:
   830 assumes "M \<in> multiset"
   831 shows "0 < mmap f M b \<longleftrightarrow> (\<exists>a. 0 < M a \<and> f a = b)"
   832 proof-
   833   have f: "finite {a. f a = b \<and> 0 < M a}" using assms unfolding multiset_def by auto
   834   show ?thesis unfolding mmap_def setsum_gt_0_iff[OF f] by auto
   835 qed
   836 
   837 lemma finite_twosets:
   838 assumes "finite B1" and "finite B2"
   839 shows "finite {u b1 b2 |b1 b2. b1 \<in> B1 \<and> b2 \<in> B2}"  (is "finite ?A")
   840 proof-
   841   have A: "?A = (\<lambda> b1b2. u (fst b1b2) (snd b1b2)) ` (B1 <*> B2)" by force
   842   show ?thesis unfolding A using finite_cartesian_product[OF assms] by auto
   843 qed
   844 
   845 lemma wp_mmap:
   846 fixes A :: "'a set" and B1 :: "'b1 set" and B2 :: "'b2 set"
   847 assumes wp: "wpull A B1 B2 f1 f2 p1 p2"
   848 shows
   849 "wpull {M. M \<in> multiset \<and> supp M \<subseteq> A}
   850        {N1. N1 \<in> multiset \<and> supp N1 \<subseteq> B1} {N2. N2 \<in> multiset \<and> supp N2 \<subseteq> B2}
   851        (mmap f1) (mmap f2) (mmap p1) (mmap p2)"
   852 unfolding wpull_def proof (safe, unfold Bex_def mem_Collect_eq)
   853   fix N1 :: "'b1 \<Rightarrow> nat" and N2 :: "'b2 \<Rightarrow> nat"
   854   assume mmap': "mmap f1 N1 = mmap f2 N2"
   855   and N1[simp]: "N1 \<in> multiset" "supp N1 \<subseteq> B1"
   856   and N2[simp]: "N2 \<in> multiset" "supp N2 \<subseteq> B2"
   857   have mN1[simp]: "mmap f1 N1 \<in> multiset" using N1 by (auto simp: mmap)
   858   have mN2[simp]: "mmap f2 N2 \<in> multiset" using N2 by (auto simp: mmap)
   859   def P \<equiv> "mmap f1 N1"
   860   have P1: "P = mmap f1 N1" and P2: "P = mmap f2 N2" unfolding P_def using mmap' by auto
   861   note P = P1 P2
   862   have P_mult[simp]: "P \<in> multiset" unfolding P_def using N1 by auto
   863   have fin_N1[simp]: "finite (supp N1)" using N1(1) unfolding multiset_def by auto
   864   have fin_N2[simp]: "finite (supp N2)" using N2(1) unfolding multiset_def by auto
   865   have fin_P[simp]: "finite (supp P)" using P_mult unfolding multiset_def by auto
   866   (*  *)
   867   def set1 \<equiv> "\<lambda> c. {b1 \<in> supp N1. f1 b1 = c}"
   868   have set1[simp]: "\<And> c b1. b1 \<in> set1 c \<Longrightarrow> f1 b1 = c" unfolding set1_def by auto
   869   have fin_set1: "\<And> c. c \<in> supp P \<Longrightarrow> finite (set1 c)"
   870   using N1(1) unfolding set1_def multiset_def by auto
   871   have set1_NE: "\<And> c. c \<in> supp P \<Longrightarrow> set1 c \<noteq> {}"
   872   unfolding set1_def P1 mmap_ge_0[OF N1(1)] by auto
   873   have supp_N1_set1: "supp N1 = (\<Union> c \<in> supp P. set1 c)"
   874   using supp_vimage_mmap[OF N1(1), of f1] unfolding set1_def P1 by auto
   875   hence set1_inclN1: "\<And>c. c \<in> supp P \<Longrightarrow> set1 c \<subseteq> supp N1" by auto
   876   hence set1_incl: "\<And> c. c \<in> supp P \<Longrightarrow> set1 c \<subseteq> B1" using N1(2) by blast
   877   have set1_disj: "\<And> c c'. c \<noteq> c' \<Longrightarrow> set1 c \<inter> set1 c' = {}"
   878   unfolding set1_def by auto
   879   have setsum_set1: "\<And> c. setsum N1 (set1 c) = P c"
   880   unfolding P1 set1_def mmap_def apply(rule setsum_cong) by auto
   881   (*  *)
   882   def set2 \<equiv> "\<lambda> c. {b2 \<in> supp N2. f2 b2 = c}"
   883   have set2[simp]: "\<And> c b2. b2 \<in> set2 c \<Longrightarrow> f2 b2 = c" unfolding set2_def by auto
   884   have fin_set2: "\<And> c. c \<in> supp P \<Longrightarrow> finite (set2 c)"
   885   using N2(1) unfolding set2_def multiset_def by auto
   886   have set2_NE: "\<And> c. c \<in> supp P \<Longrightarrow> set2 c \<noteq> {}"
   887   unfolding set2_def P2 mmap_ge_0[OF N2(1)] by auto
   888   have supp_N2_set2: "supp N2 = (\<Union> c \<in> supp P. set2 c)"
   889   using supp_vimage_mmap[OF N2(1), of f2] unfolding set2_def P2 by auto
   890   hence set2_inclN2: "\<And>c. c \<in> supp P \<Longrightarrow> set2 c \<subseteq> supp N2" by auto
   891   hence set2_incl: "\<And> c. c \<in> supp P \<Longrightarrow> set2 c \<subseteq> B2" using N2(2) by blast
   892   have set2_disj: "\<And> c c'. c \<noteq> c' \<Longrightarrow> set2 c \<inter> set2 c' = {}"
   893   unfolding set2_def by auto
   894   have setsum_set2: "\<And> c. setsum N2 (set2 c) = P c"
   895   unfolding P2 set2_def mmap_def apply(rule setsum_cong) by auto
   896   (*  *)
   897   have ss: "\<And> c. c \<in> supp P \<Longrightarrow> setsum N1 (set1 c) = setsum N2 (set2 c)"
   898   unfolding setsum_set1 setsum_set2 ..
   899   have "\<forall> c \<in> supp P. \<forall> b1b2 \<in> (set1 c) \<times> (set2 c).
   900           \<exists> a \<in> A. p1 a = fst b1b2 \<and> p2 a = snd b1b2"
   901   using wp set1_incl set2_incl unfolding wpull_def Ball_def mem_Collect_eq
   902   by simp (metis set1 set2 set_rev_mp)
   903   then obtain uu where uu:
   904   "\<forall> c \<in> supp P. \<forall> b1b2 \<in> (set1 c) \<times> (set2 c).
   905      uu c b1b2 \<in> A \<and> p1 (uu c b1b2) = fst b1b2 \<and> p2 (uu c b1b2) = snd b1b2" by metis
   906   def u \<equiv> "\<lambda> c b1 b2. uu c (b1,b2)"
   907   have u[simp]:
   908   "\<And> c b1 b2. \<lbrakk>c \<in> supp P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> u c b1 b2 \<in> A"
   909   "\<And> c b1 b2. \<lbrakk>c \<in> supp P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> p1 (u c b1 b2) = b1"
   910   "\<And> c b1 b2. \<lbrakk>c \<in> supp P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> p2 (u c b1 b2) = b2"
   911   using uu unfolding u_def by auto
   912   {fix c assume c: "c \<in> supp P"
   913    have "inj2 (u c) (set1 c) (set2 c)" unfolding inj2_def proof clarify
   914      fix b1 b1' b2 b2'
   915      assume "{b1, b1'} \<subseteq> set1 c" "{b2, b2'} \<subseteq> set2 c" and 0: "u c b1 b2 = u c b1' b2'"
   916      hence "p1 (u c b1 b2) = b1 \<and> p2 (u c b1 b2) = b2 \<and>
   917             p1 (u c b1' b2') = b1' \<and> p2 (u c b1' b2') = b2'"
   918      using u(2)[OF c] u(3)[OF c] by simp metis
   919      thus "b1 = b1' \<and> b2 = b2'" using 0 by auto
   920    qed
   921   } note inj = this
   922   def sset \<equiv> "\<lambda> c. {u c b1 b2 | b1 b2. b1 \<in> set1 c \<and> b2 \<in> set2 c}"
   923   have fin_sset[simp]: "\<And> c. c \<in> supp P \<Longrightarrow> finite (sset c)" unfolding sset_def
   924   using fin_set1 fin_set2 finite_twosets by blast
   925   have sset_A: "\<And> c. c \<in> supp P \<Longrightarrow> sset c \<subseteq> A" unfolding sset_def by auto
   926   {fix c a assume c: "c \<in> supp P" and ac: "a \<in> sset c"
   927    then obtain b1 b2 where b1: "b1 \<in> set1 c" and b2: "b2 \<in> set2 c"
   928    and a: "a = u c b1 b2" unfolding sset_def by auto
   929    have "p1 a \<in> set1 c" and p2a: "p2 a \<in> set2 c"
   930    using ac a b1 b2 c u(2) u(3) by simp+
   931    hence "u c (p1 a) (p2 a) = a" unfolding a using b1 b2 inj[OF c]
   932    unfolding inj2_def by (metis c u(2) u(3))
   933   } note u_p12[simp] = this
   934   {fix c a assume c: "c \<in> supp P" and ac: "a \<in> sset c"
   935    hence "p1 a \<in> set1 c" unfolding sset_def by auto
   936   }note p1[simp] = this
   937   {fix c a assume c: "c \<in> supp P" and ac: "a \<in> sset c"
   938    hence "p2 a \<in> set2 c" unfolding sset_def by auto
   939   }note p2[simp] = this
   940   (*  *)
   941   {fix c assume c: "c \<in> supp P"
   942    hence "\<exists> M. (\<forall> b1 \<in> set1 c. setsum (\<lambda> b2. M (u c b1 b2)) (set2 c) = N1 b1) \<and>
   943                (\<forall> b2 \<in> set2 c. setsum (\<lambda> b1. M (u c b1 b2)) (set1 c) = N2 b2)"
   944    unfolding sset_def
   945    using matrix_setsum_finite[OF set1_NE[OF c] fin_set1[OF c]
   946                                  set2_NE[OF c] fin_set2[OF c] inj[OF c] ss[OF c]] by auto
   947   }
   948   then obtain Ms where
   949   ss1: "\<And> c b1. \<lbrakk>c \<in> supp P; b1 \<in> set1 c\<rbrakk> \<Longrightarrow>
   950                    setsum (\<lambda> b2. Ms c (u c b1 b2)) (set2 c) = N1 b1" and
   951   ss2: "\<And> c b2. \<lbrakk>c \<in> supp P; b2 \<in> set2 c\<rbrakk> \<Longrightarrow>
   952                    setsum (\<lambda> b1. Ms c (u c b1 b2)) (set1 c) = N2 b2"
   953   by metis
   954   def SET \<equiv> "\<Union> c \<in> supp P. sset c"
   955   have fin_SET[simp]: "finite SET" unfolding SET_def apply(rule finite_UN_I) by auto
   956   have SET_A: "SET \<subseteq> A" unfolding SET_def using sset_A by auto
   957   have u_SET[simp]: "\<And> c b1 b2. \<lbrakk>c \<in> supp P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk> \<Longrightarrow> u c b1 b2 \<in> SET"
   958   unfolding SET_def sset_def by blast
   959   {fix c a assume c: "c \<in> supp P" and a: "a \<in> SET" and p1a: "p1 a \<in> set1 c"
   960    then obtain c' where c': "c' \<in> supp P" and ac': "a \<in> sset c'"
   961    unfolding SET_def by auto
   962    hence "p1 a \<in> set1 c'" unfolding sset_def by auto
   963    hence eq: "c = c'" using p1a c c' set1_disj by auto
   964    hence "a \<in> sset c" using ac' by simp
   965   } note p1_rev = this
   966   {fix c a assume c: "c \<in> supp P" and a: "a \<in> SET" and p2a: "p2 a \<in> set2 c"
   967    then obtain c' where c': "c' \<in> supp P" and ac': "a \<in> sset c'"
   968    unfolding SET_def by auto
   969    hence "p2 a \<in> set2 c'" unfolding sset_def by auto
   970    hence eq: "c = c'" using p2a c c' set2_disj by auto
   971    hence "a \<in> sset c" using ac' by simp
   972   } note p2_rev = this
   973   (*  *)
   974   have "\<forall> a \<in> SET. \<exists> c \<in> supp P. a \<in> sset c" unfolding SET_def by auto
   975   then obtain h where h: "\<forall> a \<in> SET. h a \<in> supp P \<and> a \<in> sset (h a)" by metis
   976   have h_u[simp]: "\<And> c b1 b2. \<lbrakk>c \<in> supp P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   977                       \<Longrightarrow> h (u c b1 b2) = c"
   978   by (metis h p2 set2 u(3) u_SET)
   979   have h_u1: "\<And> c b1 b2. \<lbrakk>c \<in> supp P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   980                       \<Longrightarrow> h (u c b1 b2) = f1 b1"
   981   using h unfolding sset_def by auto
   982   have h_u2: "\<And> c b1 b2. \<lbrakk>c \<in> supp P; b1 \<in> set1 c; b2 \<in> set2 c\<rbrakk>
   983                       \<Longrightarrow> h (u c b1 b2) = f2 b2"
   984   using h unfolding sset_def by auto
   985   def M \<equiv> "\<lambda> a. if a \<in> SET \<and> p1 a \<in> supp N1 \<and> p2 a \<in> supp N2 then Ms (h a) a else 0"
   986   have sM: "supp M \<subseteq> SET" "supp M \<subseteq> p1 -` (supp N1)" "supp M \<subseteq> p2 -` (supp N2)"
   987   unfolding M_def by auto
   988   show "\<exists>M. (M \<in> multiset \<and> supp M \<subseteq> A) \<and> mmap p1 M = N1 \<and> mmap p2 M = N2"
   989   proof(rule exI[of _ M], safe)
   990     show "M \<in> multiset"
   991     unfolding multiset_def using finite_subset[OF sM(1) fin_SET] by simp
   992   next
   993     fix a assume "0 < M a"
   994     thus "a \<in> A" unfolding M_def using SET_A by (cases "a \<in> SET") auto
   995   next
   996     show "mmap p1 M = N1"
   997     unfolding mmap_def[abs_def] proof(rule ext)
   998       fix b1
   999       let ?K = "{a. p1 a = b1 \<and> 0 < M a}"
  1000       show "setsum M ?K = N1 b1"
  1001       proof(cases "b1 \<in> supp N1")
  1002         case False
  1003         hence "?K = {}" using sM(2) by auto
  1004         thus ?thesis using False by auto
  1005       next
  1006         case True
  1007         def c \<equiv> "f1 b1"
  1008         have c: "c \<in> supp P" and b1: "b1 \<in> set1 c"
  1009         unfolding set1_def c_def P1 using True by (auto simp: mmap_image)
  1010         have "setsum M ?K = setsum M {a. p1 a = b1 \<and> a \<in> SET}"
  1011         apply(rule setsum_mono_zero_cong_left) unfolding M_def by auto
  1012         also have "... = setsum M ((\<lambda> b2. u c b1 b2) ` (set2 c))"
  1013         apply(rule setsum_cong) using c b1 proof safe
  1014           fix a assume p1a: "p1 a \<in> set1 c" and "0 < P c" and "a \<in> SET"
  1015           hence ac: "a \<in> sset c" using p1_rev by auto
  1016           hence "a = u c (p1 a) (p2 a)" using c by auto
  1017           moreover have "p2 a \<in> set2 c" using ac c by auto
  1018           ultimately show "a \<in> u c (p1 a) ` set2 c" by auto
  1019         next
  1020           fix b2 assume b1: "b1 \<in> set1 c" and b2: "b2 \<in> set2 c"
  1021           hence "u c b1 b2 \<in> SET" using c by auto
  1022         qed auto
  1023         also have "... = setsum (\<lambda> b2. M (u c b1 b2)) (set2 c)"
  1024         unfolding comp_def[symmetric] apply(rule setsum_reindex)
  1025         using inj unfolding inj_on_def inj2_def using b1 c u(3) by blast
  1026         also have "... = N1 b1" unfolding ss1[OF c b1, symmetric]
  1027           apply(rule setsum_cong[OF refl]) unfolding M_def
  1028           using True h_u[OF c b1] set2_def u(2,3)[OF c b1] u_SET[OF c b1] by fastforce
  1029         finally show ?thesis .
  1030       qed
  1031     qed
  1032   next
  1033     show "mmap p2 M = N2"
  1034     unfolding mmap_def[abs_def] proof(rule ext)
  1035       fix b2
  1036       let ?K = "{a. p2 a = b2 \<and> 0 < M a}"
  1037       show "setsum M ?K = N2 b2"
  1038       proof(cases "b2 \<in> supp N2")
  1039         case False
  1040         hence "?K = {}" using sM(3) by auto
  1041         thus ?thesis using False by auto
  1042       next
  1043         case True
  1044         def c \<equiv> "f2 b2"
  1045         have c: "c \<in> supp P" and b2: "b2 \<in> set2 c"
  1046         unfolding set2_def c_def P2 using True by (auto simp: mmap_image)
  1047         have "setsum M ?K = setsum M {a. p2 a = b2 \<and> a \<in> SET}"
  1048         apply(rule setsum_mono_zero_cong_left) unfolding M_def by auto
  1049         also have "... = setsum M ((\<lambda> b1. u c b1 b2) ` (set1 c))"
  1050         apply(rule setsum_cong) using c b2 proof safe
  1051           fix a assume p2a: "p2 a \<in> set2 c" and "0 < P c" and "a \<in> SET"
  1052           hence ac: "a \<in> sset c" using p2_rev by auto
  1053           hence "a = u c (p1 a) (p2 a)" using c by auto
  1054           moreover have "p1 a \<in> set1 c" using ac c by auto
  1055           ultimately show "a \<in> (\<lambda>b1. u c b1 (p2 a)) ` set1 c" by auto
  1056         next
  1057           fix b2 assume b1: "b1 \<in> set1 c" and b2: "b2 \<in> set2 c"
  1058           hence "u c b1 b2 \<in> SET" using c by auto
  1059         qed auto
  1060         also have "... = setsum (M o (\<lambda> b1. u c b1 b2)) (set1 c)"
  1061         apply(rule setsum_reindex)
  1062         using inj unfolding inj_on_def inj2_def using b2 c u(2) by blast
  1063         also have "... = setsum (\<lambda> b1. M (u c b1 b2)) (set1 c)"
  1064         unfolding comp_def[symmetric] by simp
  1065         also have "... = N2 b2" unfolding ss2[OF c b2, symmetric]
  1066           apply(rule setsum_cong[OF refl]) unfolding M_def set2_def
  1067           using True h_u1[OF c _ b2] u(2,3)[OF c _ b2] u_SET[OF c _ b2]
  1068           unfolding set1_def by fastforce
  1069         finally show ?thesis .
  1070       qed
  1071     qed
  1072   qed
  1073 qed
  1074 
  1075 definition multiset_map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
  1076 "multiset_map h = Abs_multiset \<circ> mmap h \<circ> count"
  1077 
  1078 bnf multiset_map [set_of] "\<lambda>_::'a multiset. natLeq" ["{#}"]
  1079 unfolding multiset_map_def
  1080 proof -
  1081   show "Abs_multiset \<circ> mmap id \<circ> count = id" unfolding mmap_id by (auto simp: count_inverse)
  1082 next
  1083   fix f g
  1084   show "Abs_multiset \<circ> mmap (g \<circ> f) \<circ> count =
  1085         Abs_multiset \<circ> mmap g \<circ> count \<circ> (Abs_multiset \<circ> mmap f \<circ> count)"
  1086   unfolding comp_def apply(rule ext)
  1087   by (auto simp: Abs_multiset_inverse count mmap_comp1 mmap)
  1088 next
  1089   fix M f g assume eq: "\<And>a. a \<in> set_of M \<Longrightarrow> f a = g a"
  1090   thus "(Abs_multiset \<circ> mmap f \<circ> count) M = (Abs_multiset \<circ> mmap g \<circ> count) M" apply auto
  1091   unfolding cIm_def[abs_def] image_def
  1092   by (auto intro!: mmap_cong simp: Abs_multiset_inject count mmap)
  1093 next
  1094   fix f show "set_of \<circ> (Abs_multiset \<circ> mmap f \<circ> count) = op ` f \<circ> set_of"
  1095   by (auto simp: count mmap mmap_image set_of_Abs_multiset supp_count)
  1096 next
  1097   show "card_order natLeq" by (rule natLeq_card_order)
  1098 next
  1099   show "cinfinite natLeq" by (rule natLeq_cinfinite)
  1100 next
  1101   fix M show "|set_of M| \<le>o natLeq"
  1102   apply(rule ordLess_imp_ordLeq)
  1103   unfolding finite_iff_ordLess_natLeq[symmetric] using finite_set_of .
  1104 next
  1105   fix A :: "'a set"
  1106   have "|{M. set_of M \<subseteq> A}| \<le>o |{as. set as \<subseteq> A}|" using card_of_set_of .
  1107   also have "|{as. set as \<subseteq> A}| \<le>o ( |A| +c ctwo) ^c natLeq"
  1108   by (rule list_in_bd)
  1109   finally show "|{M. set_of M \<subseteq> A}| \<le>o ( |A| +c ctwo) ^c natLeq" .
  1110 next
  1111   fix A B1 B2 f1 f2 p1 p2
  1112   let ?map = "\<lambda> f. Abs_multiset \<circ> mmap f \<circ> count"
  1113   assume wp: "wpull A B1 B2 f1 f2 p1 p2"
  1114   show "wpull {x. set_of x \<subseteq> A} {x. set_of x \<subseteq> B1} {x. set_of x \<subseteq> B2}
  1115               (?map f1) (?map f2) (?map p1) (?map p2)"
  1116   unfolding wpull_def proof safe
  1117     fix y1 y2
  1118     assume y1: "set_of y1 \<subseteq> B1" and y2: "set_of y2 \<subseteq> B2"
  1119     and m: "?map f1 y1 = ?map f2 y2"
  1120     def N1 \<equiv> "count y1"  def N2 \<equiv> "count y2"
  1121     have "N1 \<in> multiset \<and> supp N1 \<subseteq> B1" and "N2 \<in> multiset \<and> supp N2 \<subseteq> B2"
  1122     and "mmap f1 N1 = mmap f2 N2"
  1123     using y1 y2 m unfolding N1_def N2_def
  1124     by (auto simp: Abs_multiset_inject count mmap)
  1125     then obtain M where M: "M \<in> multiset \<and> supp M \<subseteq> A"
  1126     and N1: "mmap p1 M = N1" and N2: "mmap p2 M = N2"
  1127     using wp_mmap[OF wp] unfolding wpull_def by auto
  1128     def x \<equiv> "Abs_multiset M"
  1129     show "\<exists>x\<in>{x. set_of x \<subseteq> A}. ?map p1 x = y1 \<and> ?map p2 x = y2"
  1130     apply(intro bexI[of _ x]) using M N1 N2 unfolding N1_def N2_def x_def
  1131     by (auto simp: count_inverse Abs_multiset_inverse)
  1132   qed
  1133 qed (unfold set_of_empty, auto)
  1134 
  1135 inductive multiset_rel' where
  1136 Zero: "multiset_rel' R {#} {#}"
  1137 |
  1138 Plus: "\<lbrakk>R a b; multiset_rel' R M N\<rbrakk> \<Longrightarrow> multiset_rel' R (M + {#a#}) (N + {#b#})"
  1139 
  1140 lemma multiset_map_Zero_iff[simp]: "multiset_map f M = {#} \<longleftrightarrow> M = {#}"
  1141 by (metis image_is_empty multiset.set_map' set_of_eq_empty_iff)
  1142 
  1143 lemma multiset_map_Zero[simp]: "multiset_map f {#} = {#}" by simp
  1144 
  1145 lemma multiset_rel_Zero: "multiset_rel R {#} {#}"
  1146 unfolding multiset_rel_def Gr_def relcomp_unfold by auto
  1147 
  1148 declare multiset.count[simp]
  1149 declare mmap[simp]
  1150 declare Abs_multiset_inverse[simp]
  1151 declare multiset.count_inverse[simp]
  1152 declare union_preserves_multiset[simp]
  1153 
  1154 lemma mmap_Plus[simp]:
  1155 assumes "K \<in> multiset" and "L \<in> multiset"
  1156 shows "mmap f (\<lambda>a. K a + L a) a = mmap f K a + mmap f L a"
  1157 proof-
  1158   have "{aa. f aa = a \<and> (0 < K aa \<or> 0 < L aa)} \<subseteq>
  1159         {aa. 0 < K aa} \<union> {aa. 0 < L aa}" (is "?C \<subseteq> ?A \<union> ?B") by auto
  1160   moreover have "finite (?A \<union> ?B)" apply(rule finite_UnI)
  1161   using assms unfolding multiset_def by auto
  1162   ultimately have C: "finite ?C" using finite_subset by blast
  1163   have "setsum K {aa. f aa = a \<and> 0 < K aa} = setsum K {aa. f aa = a \<and> 0 < K aa + L aa}"
  1164   apply(rule setsum_mono_zero_cong_left) using C by auto
  1165   moreover
  1166   have "setsum L {aa. f aa = a \<and> 0 < L aa} = setsum L {aa. f aa = a \<and> 0 < K aa + L aa}"
  1167   apply(rule setsum_mono_zero_cong_left) using C by auto
  1168   ultimately show ?thesis
  1169   unfolding mmap_def by (auto simp add: setsum.distrib)
  1170 qed
  1171 
  1172 lemma multiset_map_Plus[simp]:
  1173 "multiset_map f (M1 + M2) = multiset_map f M1 + multiset_map f M2"
  1174 unfolding multiset_map_def
  1175 apply(subst multiset.count_inject[symmetric])
  1176 unfolding plus_multiset.rep_eq comp_def by auto
  1177 
  1178 lemma multiset_map_singl[simp]: "multiset_map f {#a#} = {#f a#}"
  1179 proof-
  1180   have 0: "\<And> b. card {aa. a = aa \<and> (a = aa \<longrightarrow> f aa = b)} =
  1181                 (if b = f a then 1 else 0)" by auto
  1182   thus ?thesis
  1183   unfolding multiset_map_def comp_def mmap_def[abs_def] map_fun_def
  1184   by (simp, simp add: single_def)
  1185 qed
  1186 
  1187 lemma multiset_rel_Plus:
  1188 assumes ab: "R a b" and MN: "multiset_rel R M N"
  1189 shows "multiset_rel R (M + {#a#}) (N + {#b#})"
  1190 proof-
  1191   {fix y assume "R a b" and "set_of y \<subseteq> {(x, y). R x y}"
  1192    hence "\<exists>ya. multiset_map fst y + {#a#} = multiset_map fst ya \<and>
  1193                multiset_map snd y + {#b#} = multiset_map snd ya \<and>
  1194                set_of ya \<subseteq> {(x, y). R x y}"
  1195    apply(intro exI[of _ "y + {#(a,b)#}"]) by auto
  1196   }
  1197   thus ?thesis
  1198   using assms
  1199   unfolding multiset_rel_def Gr_def relcomp_unfold by force
  1200 qed
  1201 
  1202 lemma multiset_rel'_imp_multiset_rel:
  1203 "multiset_rel' R M N \<Longrightarrow> multiset_rel R M N"
  1204 apply(induct rule: multiset_rel'.induct)
  1205 using multiset_rel_Zero multiset_rel_Plus by auto
  1206 
  1207 lemma mcard_multiset_map[simp]: "mcard (multiset_map f M) = mcard M"
  1208 proof -
  1209   def A \<equiv> "\<lambda> b. {a. f a = b \<and> a \<in># M}"
  1210   let ?B = "{b. 0 < setsum (count M) (A b)}"
  1211   have "{b. \<exists>a. f a = b \<and> a \<in># M} \<subseteq> f ` {a. a \<in># M}" by auto
  1212   moreover have "finite (f ` {a. a \<in># M})" apply(rule finite_imageI)
  1213   using finite_Collect_mem .
  1214   ultimately have fin: "finite {b. \<exists>a. f a = b \<and> a \<in># M}" by(rule finite_subset)
  1215   have i: "inj_on A ?B" unfolding inj_on_def A_def apply clarsimp
  1216   by (metis (lifting, mono_tags) mem_Collect_eq rel_simps(54)
  1217                                  setsum_gt_0_iff setsum_infinite)
  1218   have 0: "\<And> b. 0 < setsum (count M) (A b) \<longleftrightarrow> (\<exists> a \<in> A b. count M a > 0)"
  1219   apply safe
  1220     apply (metis less_not_refl setsum_gt_0_iff setsum_infinite)
  1221     by (metis A_def finite_Collect_conjI finite_Collect_mem setsum_gt_0_iff)
  1222   hence AB: "A ` ?B = {A b | b. \<exists> a \<in> A b. count M a > 0}" by auto
  1223 
  1224   have "setsum (\<lambda> x. setsum (count M) (A x)) ?B = setsum (setsum (count M) o A) ?B"
  1225   unfolding comp_def ..
  1226   also have "... = (\<Sum>x\<in> A ` ?B. setsum (count M) x)"
  1227   unfolding setsum.reindex [OF i, symmetric] ..
  1228   also have "... = setsum (count M) (\<Union>x\<in>A ` {b. 0 < setsum (count M) (A b)}. x)"
  1229   (is "_ = setsum (count M) ?J")
  1230   apply(rule setsum.UNION_disjoint[symmetric])
  1231   using 0 fin unfolding A_def by (auto intro!: finite_imageI)
  1232   also have "?J = {a. a \<in># M}" unfolding AB unfolding A_def by auto
  1233   finally have "setsum (\<lambda> x. setsum (count M) (A x)) ?B =
  1234                 setsum (count M) {a. a \<in># M}" .
  1235   then show ?thesis by (simp add: A_def mcard_unfold_setsum multiset_map_def set_of_def mmap_def)
  1236 qed
  1237 
  1238 lemma multiset_rel_mcard:
  1239 assumes "multiset_rel R M N"
  1240 shows "mcard M = mcard N"
  1241 using assms unfolding multiset_rel_def relcomp_unfold Gr_def by auto
  1242 
  1243 lemma multiset_induct2[case_names empty addL addR]:
  1244 assumes empty: "P {#} {#}"
  1245 and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
  1246 and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
  1247 shows "P M N"
  1248 apply(induct N rule: multiset_induct)
  1249   apply(induct M rule: multiset_induct, rule empty, erule addL)
  1250   apply(induct M rule: multiset_induct, erule addR, erule addR)
  1251 done
  1252 
  1253 lemma multiset_induct2_mcard[consumes 1, case_names empty add]:
  1254 assumes c: "mcard M = mcard N"
  1255 and empty: "P {#} {#}"
  1256 and add: "\<And>M N a b. P M N \<Longrightarrow> P (M + {#a#}) (N + {#b#})"
  1257 shows "P M N"
  1258 using c proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
  1259   case (less M)  show ?case
  1260   proof(cases "M = {#}")
  1261     case True hence "N = {#}" using less.prems by auto
  1262     thus ?thesis using True empty by auto
  1263   next
  1264     case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
  1265     have "N \<noteq> {#}" using False less.prems by auto
  1266     then obtain N1 b where N: "N = N1 + {#b#}" by (metis multi_nonempty_split)
  1267     have "mcard M1 = mcard N1" using less.prems unfolding M N by auto
  1268     thus ?thesis using M N less.hyps add by auto
  1269   qed
  1270 qed
  1271 
  1272 lemma msed_map_invL:
  1273 assumes "multiset_map f (M + {#a#}) = N"
  1274 shows "\<exists> N1. N = N1 + {#f a#} \<and> multiset_map f M = N1"
  1275 proof-
  1276   have "f a \<in># N"
  1277   using assms multiset.set_map'[of f "M + {#a#}"] by auto
  1278   then obtain N1 where N: "N = N1 + {#f a#}" using multi_member_split by metis
  1279   have "multiset_map f M = N1" using assms unfolding N by simp
  1280   thus ?thesis using N by blast
  1281 qed
  1282 
  1283 lemma msed_map_invR:
  1284 assumes "multiset_map f M = N + {#b#}"
  1285 shows "\<exists> M1 a. M = M1 + {#a#} \<and> f a = b \<and> multiset_map f M1 = N"
  1286 proof-
  1287   obtain a where a: "a \<in># M" and fa: "f a = b"
  1288   using multiset.set_map'[of f M] unfolding assms
  1289   by (metis image_iff mem_set_of_iff union_single_eq_member)
  1290   then obtain M1 where M: "M = M1 + {#a#}" using multi_member_split by metis
  1291   have "multiset_map f M1 = N" using assms unfolding M fa[symmetric] by simp
  1292   thus ?thesis using M fa by blast
  1293 qed
  1294 
  1295 lemma msed_rel_invL:
  1296 assumes "multiset_rel R (M + {#a#}) N"
  1297 shows "\<exists> N1 b. N = N1 + {#b#} \<and> R a b \<and> multiset_rel R M N1"
  1298 proof-
  1299   obtain K where KM: "multiset_map fst K = M + {#a#}"
  1300   and KN: "multiset_map snd K = N" and sK: "set_of K \<subseteq> {(a, b). R a b}"
  1301   using assms
  1302   unfolding multiset_rel_def Gr_def relcomp_unfold by auto
  1303   obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
  1304   and K1M: "multiset_map fst K1 = M" using msed_map_invR[OF KM] by auto
  1305   obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "multiset_map snd K1 = N1"
  1306   using msed_map_invL[OF KN[unfolded K]] by auto
  1307   have Rab: "R a (snd ab)" using sK a unfolding K by auto
  1308   have "multiset_rel R M N1" using sK K1M K1N1
  1309   unfolding K multiset_rel_def Gr_def relcomp_unfold by auto
  1310   thus ?thesis using N Rab by auto
  1311 qed
  1312 
  1313 lemma msed_rel_invR:
  1314 assumes "multiset_rel R M (N + {#b#})"
  1315 shows "\<exists> M1 a. M = M1 + {#a#} \<and> R a b \<and> multiset_rel R M1 N"
  1316 proof-
  1317   obtain K where KN: "multiset_map snd K = N + {#b#}"
  1318   and KM: "multiset_map fst K = M" and sK: "set_of K \<subseteq> {(a, b). R a b}"
  1319   using assms
  1320   unfolding multiset_rel_def Gr_def relcomp_unfold by auto
  1321   obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
  1322   and K1N: "multiset_map snd K1 = N" using msed_map_invR[OF KN] by auto
  1323   obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "multiset_map fst K1 = M1"
  1324   using msed_map_invL[OF KM[unfolded K]] by auto
  1325   have Rab: "R (fst ab) b" using sK b unfolding K by auto
  1326   have "multiset_rel R M1 N" using sK K1N K1M1
  1327   unfolding K multiset_rel_def Gr_def relcomp_unfold by auto
  1328   thus ?thesis using M Rab by auto
  1329 qed
  1330 
  1331 lemma multiset_rel_imp_multiset_rel':
  1332 assumes "multiset_rel R M N"
  1333 shows "multiset_rel' R M N"
  1334 using assms proof(induct M arbitrary: N rule: measure_induct_rule[of mcard])
  1335   case (less M)
  1336   have c: "mcard M = mcard N" using multiset_rel_mcard[OF less.prems] .
  1337   show ?case
  1338   proof(cases "M = {#}")
  1339     case True hence "N = {#}" using c by simp
  1340     thus ?thesis using True multiset_rel'.Zero by auto
  1341   next
  1342     case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
  1343     obtain N1 b where N: "N = N1 + {#b#}" and R: "R a b" and ms: "multiset_rel R M1 N1"
  1344     using msed_rel_invL[OF less.prems[unfolded M]] by auto
  1345     have "multiset_rel' R M1 N1" using less.hyps[of M1 N1] ms unfolding M by simp
  1346     thus ?thesis using multiset_rel'.Plus[of R a b, OF R] unfolding M N by simp
  1347   qed
  1348 qed
  1349 
  1350 lemma multiset_rel_multiset_rel':
  1351 "multiset_rel R M N = multiset_rel' R M N"
  1352 using  multiset_rel_imp_multiset_rel' multiset_rel'_imp_multiset_rel by auto
  1353 
  1354 (* The main end product for multiset_rel: inductive characterization *)
  1355 theorems multiset_rel_induct[case_names empty add, induct pred: multiset_rel] =
  1356          multiset_rel'.induct[unfolded multiset_rel_multiset_rel'[symmetric]]
  1357 
  1358 
  1359 
  1360 (* Advanced relator customization *)
  1361 
  1362 (* Set vs. sum relators: *)
  1363 (* FIXME: All such facts should be declared as simps: *)
  1364 declare sum_rel_simps[simp]
  1365 
  1366 lemma set_rel_sum_rel[simp]: 
  1367 "set_rel (sum_rel \<chi> \<phi>) A1 A2 \<longleftrightarrow> 
  1368  set_rel \<chi> (Inl -` A1) (Inl -` A2) \<and> set_rel \<phi> (Inr -` A1) (Inr -` A2)"
  1369 (is "?L \<longleftrightarrow> ?Rl \<and> ?Rr")
  1370 proof safe
  1371   assume L: "?L"
  1372   show ?Rl unfolding set_rel_def Bex_def vimage_eq proof safe
  1373     fix l1 assume "Inl l1 \<in> A1"
  1374     then obtain a2 where a2: "a2 \<in> A2" and "sum_rel \<chi> \<phi> (Inl l1) a2"
  1375     using L unfolding set_rel_def by auto
  1376     then obtain l2 where "a2 = Inl l2 \<and> \<chi> l1 l2" by (cases a2, auto)
  1377     thus "\<exists> l2. Inl l2 \<in> A2 \<and> \<chi> l1 l2" using a2 by auto
  1378   next
  1379     fix l2 assume "Inl l2 \<in> A2"
  1380     then obtain a1 where a1: "a1 \<in> A1" and "sum_rel \<chi> \<phi> a1 (Inl l2)"
  1381     using L unfolding set_rel_def by auto
  1382     then obtain l1 where "a1 = Inl l1 \<and> \<chi> l1 l2" by (cases a1, auto)
  1383     thus "\<exists> l1. Inl l1 \<in> A1 \<and> \<chi> l1 l2" using a1 by auto
  1384   qed
  1385   show ?Rr unfolding set_rel_def Bex_def vimage_eq proof safe
  1386     fix r1 assume "Inr r1 \<in> A1"
  1387     then obtain a2 where a2: "a2 \<in> A2" and "sum_rel \<chi> \<phi> (Inr r1) a2"
  1388     using L unfolding set_rel_def by auto
  1389     then obtain r2 where "a2 = Inr r2 \<and> \<phi> r1 r2" by (cases a2, auto)
  1390     thus "\<exists> r2. Inr r2 \<in> A2 \<and> \<phi> r1 r2" using a2 by auto
  1391   next
  1392     fix r2 assume "Inr r2 \<in> A2"
  1393     then obtain a1 where a1: "a1 \<in> A1" and "sum_rel \<chi> \<phi> a1 (Inr r2)"
  1394     using L unfolding set_rel_def by auto
  1395     then obtain r1 where "a1 = Inr r1 \<and> \<phi> r1 r2" by (cases a1, auto)
  1396     thus "\<exists> r1. Inr r1 \<in> A1 \<and> \<phi> r1 r2" using a1 by auto
  1397   qed
  1398 next
  1399   assume Rl: "?Rl" and Rr: "?Rr"
  1400   show ?L unfolding set_rel_def Bex_def vimage_eq proof safe
  1401     fix a1 assume a1: "a1 \<in> A1"
  1402     show "\<exists> a2. a2 \<in> A2 \<and> sum_rel \<chi> \<phi> a1 a2"
  1403     proof(cases a1)
  1404       case (Inl l1) then obtain l2 where "Inl l2 \<in> A2 \<and> \<chi> l1 l2"
  1405       using Rl a1 unfolding set_rel_def by blast
  1406       thus ?thesis unfolding Inl by auto
  1407     next
  1408       case (Inr r1) then obtain r2 where "Inr r2 \<in> A2 \<and> \<phi> r1 r2"
  1409       using Rr a1 unfolding set_rel_def by blast
  1410       thus ?thesis unfolding Inr by auto
  1411     qed
  1412   next
  1413     fix a2 assume a2: "a2 \<in> A2"
  1414     show "\<exists> a1. a1 \<in> A1 \<and> sum_rel \<chi> \<phi> a1 a2"
  1415     proof(cases a2)
  1416       case (Inl l2) then obtain l1 where "Inl l1 \<in> A1 \<and> \<chi> l1 l2"
  1417       using Rl a2 unfolding set_rel_def by blast
  1418       thus ?thesis unfolding Inl by auto
  1419     next
  1420       case (Inr r2) then obtain r1 where "Inr r1 \<in> A1 \<and> \<phi> r1 r2"
  1421       using Rr a2 unfolding set_rel_def by blast
  1422       thus ?thesis unfolding Inr by auto
  1423     qed
  1424   qed
  1425 qed
  1426 
  1427 
  1428 end