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