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