src/HOL/Library/FSet.thy
author haftmann
Fri Nov 01 18:51:14 2013 +0100 (2013-11-01)
changeset 54230 b1d955791529
parent 54014 21dac9a60f0c
child 54258 adfc759263ab
permissions -rw-r--r--
more simplification rules on unary and binary minus
     1 (*  Title:      HOL/Library/FSet.thy
     2     Author:     Ondrej Kuncar, TU Muenchen
     3     Author:     Cezary Kaliszyk and Christian Urban
     4 *)
     5 
     6 header {* Type of finite sets defined as a subtype of sets *}
     7 
     8 theory FSet
     9 imports Main Conditionally_Complete_Lattices
    10 begin
    11 
    12 subsection {* Definition of the type *}
    13 
    14 typedef 'a fset = "{A :: 'a set. finite A}"  morphisms fset Abs_fset
    15 by auto
    16 
    17 setup_lifting type_definition_fset
    18 
    19 subsection {* Basic operations and type class instantiations *}
    20 
    21 (* FIXME transfer and right_total vs. bi_total *)
    22 instantiation fset :: (finite) finite
    23 begin
    24 instance by default (transfer, simp)
    25 end
    26 
    27 instantiation fset :: (type) "{bounded_lattice_bot, distrib_lattice, minus}"
    28 begin
    29 
    30 interpretation lifting_syntax .
    31 
    32 lift_definition bot_fset :: "'a fset" is "{}" parametric empty_transfer by simp 
    33 
    34 lift_definition less_eq_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" is subset_eq parametric subset_transfer 
    35   by simp
    36 
    37 definition less_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" where "xs < ys \<equiv> xs \<le> ys \<and> xs \<noteq> (ys::'a fset)"
    38 
    39 lemma less_fset_transfer[transfer_rule]:
    40   assumes [transfer_rule]: "bi_unique A" 
    41   shows "((pcr_fset A) ===> (pcr_fset A) ===> op =) op \<subset> op <"
    42   unfolding less_fset_def[abs_def] psubset_eq[abs_def] by transfer_prover
    43   
    44 
    45 lift_definition sup_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is union parametric union_transfer
    46   by simp
    47 
    48 lift_definition inf_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is inter parametric inter_transfer
    49   by simp
    50 
    51 lift_definition minus_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is minus parametric Diff_transfer
    52   by simp
    53 
    54 instance
    55 by default (transfer, auto)+
    56 
    57 end
    58 
    59 abbreviation fempty :: "'a fset" ("{||}") where "{||} \<equiv> bot"
    60 abbreviation fsubset_eq :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subseteq>|" 50) where "xs |\<subseteq>| ys \<equiv> xs \<le> ys"
    61 abbreviation fsubset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subset>|" 50) where "xs |\<subset>| ys \<equiv> xs < ys"
    62 abbreviation funion :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" (infixl "|\<union>|" 65) where "xs |\<union>| ys \<equiv> sup xs ys"
    63 abbreviation finter :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" (infixl "|\<inter>|" 65) where "xs |\<inter>| ys \<equiv> inf xs ys"
    64 abbreviation fminus :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" (infixl "|-|" 65) where "xs |-| ys \<equiv> minus xs ys"
    65 
    66 instantiation fset :: (equal) equal
    67 begin
    68 definition "HOL.equal A B \<longleftrightarrow> A |\<subseteq>| B \<and> B |\<subseteq>| A"
    69 instance by intro_classes (auto simp add: equal_fset_def)
    70 end 
    71 
    72 instantiation fset :: (type) conditionally_complete_lattice
    73 begin
    74 
    75 interpretation lifting_syntax .
    76 
    77 lemma right_total_Inf_fset_transfer:
    78   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
    79   shows "(set_rel (set_rel A) ===> set_rel A) 
    80     (\<lambda>S. if finite (Inter S \<inter> Collect (Domainp A)) then Inter S \<inter> Collect (Domainp A) else {}) 
    81       (\<lambda>S. if finite (Inf S) then Inf S else {})"
    82     by transfer_prover
    83 
    84 lemma Inf_fset_transfer:
    85   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
    86   shows "(set_rel (set_rel A) ===> set_rel A) (\<lambda>A. if finite (Inf A) then Inf A else {}) 
    87     (\<lambda>A. if finite (Inf A) then Inf A else {})"
    88   by transfer_prover
    89 
    90 lift_definition Inf_fset :: "'a fset set \<Rightarrow> 'a fset" is "\<lambda>A. if finite (Inf A) then Inf A else {}" 
    91 parametric right_total_Inf_fset_transfer Inf_fset_transfer by simp
    92 
    93 lemma Sup_fset_transfer:
    94   assumes [transfer_rule]: "bi_unique A"
    95   shows "(set_rel (set_rel A) ===> set_rel A) (\<lambda>A. if finite (Sup A) then Sup A else {})
    96   (\<lambda>A. if finite (Sup A) then Sup A else {})" by transfer_prover
    97 
    98 lift_definition Sup_fset :: "'a fset set \<Rightarrow> 'a fset" is "\<lambda>A. if finite (Sup A) then Sup A else {}"
    99 parametric Sup_fset_transfer by simp
   100 
   101 lemma finite_Sup: "\<exists>z. finite z \<and> (\<forall>a. a \<in> X \<longrightarrow> a \<le> z) \<Longrightarrow> finite (Sup X)"
   102 by (auto intro: finite_subset)
   103 
   104 instance
   105 proof 
   106   fix x z :: "'a fset"
   107   fix X :: "'a fset set"
   108   {
   109     assume "x \<in> X" "(\<And>a. a \<in> X \<Longrightarrow> z |\<subseteq>| a)" 
   110     then show "Inf X |\<subseteq>| x"  by transfer auto
   111   next
   112     assume "X \<noteq> {}" "(\<And>x. x \<in> X \<Longrightarrow> z |\<subseteq>| x)"
   113     then show "z |\<subseteq>| Inf X" by transfer (clarsimp, blast)
   114   next
   115     assume "x \<in> X" "(\<And>a. a \<in> X \<Longrightarrow> a |\<subseteq>| z)"
   116     then show "x |\<subseteq>| Sup X" by transfer (auto intro!: finite_Sup)
   117   next
   118     assume "X \<noteq> {}" "(\<And>x. x \<in> X \<Longrightarrow> x |\<subseteq>| z)"
   119     then show "Sup X |\<subseteq>| z" by transfer (clarsimp, blast)
   120   }
   121 qed
   122 end
   123 
   124 instantiation fset :: (finite) complete_lattice 
   125 begin
   126 
   127 lift_definition top_fset :: "'a fset" is UNIV parametric right_total_UNIV_transfer UNIV_transfer by simp
   128 
   129 instance by default (transfer, auto)+
   130 end
   131 
   132 instantiation fset :: (finite) complete_boolean_algebra
   133 begin
   134 
   135 lift_definition uminus_fset :: "'a fset \<Rightarrow> 'a fset" is uminus 
   136   parametric right_total_Compl_transfer Compl_transfer by simp
   137 
   138 instance by (default, simp_all only: INF_def SUP_def) (transfer, auto)+
   139 
   140 end
   141 
   142 abbreviation fUNIV :: "'a::finite fset" where "fUNIV \<equiv> top"
   143 abbreviation fuminus :: "'a::finite fset \<Rightarrow> 'a fset" ("|-| _" [81] 80) where "|-| x \<equiv> uminus x"
   144 
   145 subsection {* Other operations *}
   146 
   147 lift_definition finsert :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is insert parametric Lifting_Set.insert_transfer
   148   by simp
   149 
   150 syntax
   151   "_insert_fset"     :: "args => 'a fset"  ("{|(_)|}")
   152 
   153 translations
   154   "{|x, xs|}" == "CONST finsert x {|xs|}"
   155   "{|x|}"     == "CONST finsert x {||}"
   156 
   157 lift_definition fmember :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<in>|" 50) is Set.member 
   158   parametric member_transfer by simp
   159 
   160 abbreviation notin_fset :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<notin>|" 50) where "x |\<notin>| S \<equiv> \<not> (x |\<in>| S)"
   161 
   162 context
   163 begin
   164 interpretation lifting_syntax .
   165 
   166 lift_definition ffilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is Set.filter 
   167   parametric Lifting_Set.filter_transfer unfolding Set.filter_def by simp
   168 
   169 lemma compose_rel_to_Domainp:
   170   assumes "left_unique R"
   171   assumes "(R ===> op=) P P'"
   172   shows "(R OO Lifting.invariant P' OO R\<inverse>\<inverse>) x y \<longleftrightarrow> Domainp R x \<and> P x \<and> x = y"
   173 using assms unfolding OO_def conversep_iff Domainp_iff left_unique_def fun_rel_def invariant_def
   174 by blast
   175 
   176 lift_definition fPow :: "'a fset \<Rightarrow> 'a fset fset" is Pow parametric Pow_transfer 
   177 by (subst compose_rel_to_Domainp [OF _ finite_transfer]) (auto intro: transfer_raw finite_subset 
   178   simp add: fset.pcr_cr_eq[symmetric] Domainp_set fset.domain_eq)
   179 
   180 lift_definition fcard :: "'a fset \<Rightarrow> nat" is card parametric card_transfer by simp
   181 
   182 lift_definition fimage :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset" (infixr "|`|" 90) is image 
   183   parametric image_transfer by simp
   184 
   185 lift_definition fthe_elem :: "'a fset \<Rightarrow> 'a" is the_elem ..
   186 
   187 (* FIXME why is not invariant here unfolded ? *)
   188 lift_definition fbind :: "'a fset \<Rightarrow> ('a \<Rightarrow> 'b fset) \<Rightarrow> 'b fset" is Set.bind parametric bind_transfer
   189 unfolding invariant_def Set.bind_def by clarsimp metis
   190 
   191 lift_definition ffUnion :: "'a fset fset \<Rightarrow> 'a fset" is Union parametric Union_transfer
   192 by (subst(asm) compose_rel_to_Domainp [OF _ finite_transfer])
   193   (auto intro: transfer_raw simp add: fset.pcr_cr_eq[symmetric] Domainp_set fset.domain_eq invariant_def)
   194 
   195 lift_definition fBall :: "'a fset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" is Ball parametric Ball_transfer ..
   196 lift_definition fBex :: "'a fset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" is Bex parametric Bex_transfer ..
   197 
   198 lift_definition ffold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a fset \<Rightarrow> 'b" is Finite_Set.fold ..
   199 
   200 subsection {* Transferred lemmas from Set.thy *}
   201 
   202 lemmas fset_eqI = set_eqI[Transfer.transferred]
   203 lemmas fset_eq_iff[no_atp] = set_eq_iff[Transfer.transferred]
   204 lemmas fBallI[intro!] = ballI[Transfer.transferred]
   205 lemmas fbspec[dest?] = bspec[Transfer.transferred]
   206 lemmas fBallE[elim] = ballE[Transfer.transferred]
   207 lemmas fBexI[intro] = bexI[Transfer.transferred]
   208 lemmas rev_fBexI[intro?] = rev_bexI[Transfer.transferred]
   209 lemmas fBexCI = bexCI[Transfer.transferred]
   210 lemmas fBexE[elim!] = bexE[Transfer.transferred]
   211 lemmas fBall_triv[simp] = ball_triv[Transfer.transferred]
   212 lemmas fBex_triv[simp] = bex_triv[Transfer.transferred]
   213 lemmas fBex_triv_one_point1[simp] = bex_triv_one_point1[Transfer.transferred]
   214 lemmas fBex_triv_one_point2[simp] = bex_triv_one_point2[Transfer.transferred]
   215 lemmas fBex_one_point1[simp] = bex_one_point1[Transfer.transferred]
   216 lemmas fBex_one_point2[simp] = bex_one_point2[Transfer.transferred]
   217 lemmas fBall_one_point1[simp] = ball_one_point1[Transfer.transferred]
   218 lemmas fBall_one_point2[simp] = ball_one_point2[Transfer.transferred]
   219 lemmas fBall_conj_distrib = ball_conj_distrib[Transfer.transferred]
   220 lemmas fBex_disj_distrib = bex_disj_distrib[Transfer.transferred]
   221 lemmas fBall_cong = ball_cong[Transfer.transferred]
   222 lemmas fBex_cong = bex_cong[Transfer.transferred]
   223 lemmas fsubsetI[intro!] = subsetI[Transfer.transferred]
   224 lemmas fsubsetD[elim, intro?] = subsetD[Transfer.transferred]
   225 lemmas rev_fsubsetD[no_atp,intro?] = rev_subsetD[Transfer.transferred]
   226 lemmas fsubsetCE[no_atp,elim] = subsetCE[Transfer.transferred]
   227 lemmas fsubset_eq[no_atp] = subset_eq[Transfer.transferred]
   228 lemmas contra_fsubsetD[no_atp] = contra_subsetD[Transfer.transferred]
   229 lemmas fsubset_refl = subset_refl[Transfer.transferred]
   230 lemmas fsubset_trans = subset_trans[Transfer.transferred]
   231 lemmas fset_rev_mp = set_rev_mp[Transfer.transferred]
   232 lemmas fset_mp = set_mp[Transfer.transferred]
   233 lemmas fsubset_not_fsubset_eq[code] = subset_not_subset_eq[Transfer.transferred]
   234 lemmas eq_fmem_trans = eq_mem_trans[Transfer.transferred]
   235 lemmas fsubset_antisym[intro!] = subset_antisym[Transfer.transferred]
   236 lemmas fequalityD1 = equalityD1[Transfer.transferred]
   237 lemmas fequalityD2 = equalityD2[Transfer.transferred]
   238 lemmas fequalityE = equalityE[Transfer.transferred]
   239 lemmas fequalityCE[elim] = equalityCE[Transfer.transferred]
   240 lemmas eqfset_imp_iff = eqset_imp_iff[Transfer.transferred]
   241 lemmas eqfelem_imp_iff = eqelem_imp_iff[Transfer.transferred]
   242 lemmas fempty_iff[simp] = empty_iff[Transfer.transferred]
   243 lemmas fempty_fsubsetI[iff] = empty_subsetI[Transfer.transferred]
   244 lemmas equalsffemptyI = equals0I[Transfer.transferred]
   245 lemmas equalsffemptyD = equals0D[Transfer.transferred]
   246 lemmas fBall_fempty[simp] = ball_empty[Transfer.transferred]
   247 lemmas fBex_fempty[simp] = bex_empty[Transfer.transferred]
   248 lemmas fPow_iff[iff] = Pow_iff[Transfer.transferred]
   249 lemmas fPowI = PowI[Transfer.transferred]
   250 lemmas fPowD = PowD[Transfer.transferred]
   251 lemmas fPow_bottom = Pow_bottom[Transfer.transferred]
   252 lemmas fPow_top = Pow_top[Transfer.transferred]
   253 lemmas fPow_not_fempty = Pow_not_empty[Transfer.transferred]
   254 lemmas finter_iff[simp] = Int_iff[Transfer.transferred]
   255 lemmas finterI[intro!] = IntI[Transfer.transferred]
   256 lemmas finterD1 = IntD1[Transfer.transferred]
   257 lemmas finterD2 = IntD2[Transfer.transferred]
   258 lemmas finterE[elim!] = IntE[Transfer.transferred]
   259 lemmas funion_iff[simp] = Un_iff[Transfer.transferred]
   260 lemmas funionI1[elim?] = UnI1[Transfer.transferred]
   261 lemmas funionI2[elim?] = UnI2[Transfer.transferred]
   262 lemmas funionCI[intro!] = UnCI[Transfer.transferred]
   263 lemmas funionE[elim!] = UnE[Transfer.transferred]
   264 lemmas fminus_iff[simp] = Diff_iff[Transfer.transferred]
   265 lemmas fminusI[intro!] = DiffI[Transfer.transferred]
   266 lemmas fminusD1 = DiffD1[Transfer.transferred]
   267 lemmas fminusD2 = DiffD2[Transfer.transferred]
   268 lemmas fminusE[elim!] = DiffE[Transfer.transferred]
   269 lemmas finsert_iff[simp] = insert_iff[Transfer.transferred]
   270 lemmas finsertI1 = insertI1[Transfer.transferred]
   271 lemmas finsertI2 = insertI2[Transfer.transferred]
   272 lemmas finsertE[elim!] = insertE[Transfer.transferred]
   273 lemmas finsertCI[intro!] = insertCI[Transfer.transferred]
   274 lemmas fsubset_finsert_iff = subset_insert_iff[Transfer.transferred]
   275 lemmas finsert_ident = insert_ident[Transfer.transferred]
   276 lemmas fsingletonI[intro!,no_atp] = singletonI[Transfer.transferred]
   277 lemmas fsingletonD[dest!,no_atp] = singletonD[Transfer.transferred]
   278 lemmas fsingleton_iff = singleton_iff[Transfer.transferred]
   279 lemmas fsingleton_inject[dest!] = singleton_inject[Transfer.transferred]
   280 lemmas fsingleton_finsert_inj_eq[iff,no_atp] = singleton_insert_inj_eq[Transfer.transferred]
   281 lemmas fsingleton_finsert_inj_eq'[iff,no_atp] = singleton_insert_inj_eq'[Transfer.transferred]
   282 lemmas fsubset_fsingletonD = subset_singletonD[Transfer.transferred]
   283 lemmas fminus_single_finsert = diff_single_insert[Transfer.transferred]
   284 lemmas fdoubleton_eq_iff = doubleton_eq_iff[Transfer.transferred]
   285 lemmas funion_fsingleton_iff = Un_singleton_iff[Transfer.transferred]
   286 lemmas fsingleton_funion_iff = singleton_Un_iff[Transfer.transferred]
   287 lemmas fimage_eqI[simp, intro] = image_eqI[Transfer.transferred]
   288 lemmas fimageI = imageI[Transfer.transferred]
   289 lemmas rev_fimage_eqI = rev_image_eqI[Transfer.transferred]
   290 lemmas fimageE[elim!] = imageE[Transfer.transferred]
   291 lemmas Compr_fimage_eq = Compr_image_eq[Transfer.transferred]
   292 lemmas fimage_funion = image_Un[Transfer.transferred]
   293 lemmas fimage_iff = image_iff[Transfer.transferred]
   294 lemmas fimage_fsubset_iff[no_atp] = image_subset_iff[Transfer.transferred]
   295 lemmas fimage_fsubsetI = image_subsetI[Transfer.transferred]
   296 lemmas fimage_ident[simp] = image_ident[Transfer.transferred]
   297 lemmas split_if_fmem1 = split_if_mem1[Transfer.transferred]
   298 lemmas split_if_fmem2 = split_if_mem2[Transfer.transferred]
   299 lemmas pfsubsetI[intro!,no_atp] = psubsetI[Transfer.transferred]
   300 lemmas pfsubsetE[elim!,no_atp] = psubsetE[Transfer.transferred]
   301 lemmas pfsubset_finsert_iff = psubset_insert_iff[Transfer.transferred]
   302 lemmas pfsubset_eq = psubset_eq[Transfer.transferred]
   303 lemmas pfsubset_imp_fsubset = psubset_imp_subset[Transfer.transferred]
   304 lemmas pfsubset_trans = psubset_trans[Transfer.transferred]
   305 lemmas pfsubsetD = psubsetD[Transfer.transferred]
   306 lemmas pfsubset_fsubset_trans = psubset_subset_trans[Transfer.transferred]
   307 lemmas fsubset_pfsubset_trans = subset_psubset_trans[Transfer.transferred]
   308 lemmas pfsubset_imp_ex_fmem = psubset_imp_ex_mem[Transfer.transferred]
   309 lemmas fimage_fPow_mono = image_Pow_mono[Transfer.transferred]
   310 lemmas fimage_fPow_surj = image_Pow_surj[Transfer.transferred]
   311 lemmas fsubset_finsertI = subset_insertI[Transfer.transferred]
   312 lemmas fsubset_finsertI2 = subset_insertI2[Transfer.transferred]
   313 lemmas fsubset_finsert = subset_insert[Transfer.transferred]
   314 lemmas funion_upper1 = Un_upper1[Transfer.transferred]
   315 lemmas funion_upper2 = Un_upper2[Transfer.transferred]
   316 lemmas funion_least = Un_least[Transfer.transferred]
   317 lemmas finter_lower1 = Int_lower1[Transfer.transferred]
   318 lemmas finter_lower2 = Int_lower2[Transfer.transferred]
   319 lemmas finter_greatest = Int_greatest[Transfer.transferred]
   320 lemmas fminus_fsubset = Diff_subset[Transfer.transferred]
   321 lemmas fminus_fsubset_conv = Diff_subset_conv[Transfer.transferred]
   322 lemmas fsubset_fempty[simp] = subset_empty[Transfer.transferred]
   323 lemmas not_pfsubset_fempty[iff] = not_psubset_empty[Transfer.transferred]
   324 lemmas finsert_is_funion = insert_is_Un[Transfer.transferred]
   325 lemmas finsert_not_fempty[simp] = insert_not_empty[Transfer.transferred]
   326 lemmas fempty_not_finsert = empty_not_insert[Transfer.transferred]
   327 lemmas finsert_absorb = insert_absorb[Transfer.transferred]
   328 lemmas finsert_absorb2[simp] = insert_absorb2[Transfer.transferred]
   329 lemmas finsert_commute = insert_commute[Transfer.transferred]
   330 lemmas finsert_fsubset[simp] = insert_subset[Transfer.transferred]
   331 lemmas finsert_inter_finsert[simp] = insert_inter_insert[Transfer.transferred]
   332 lemmas finsert_disjoint[simp,no_atp] = insert_disjoint[Transfer.transferred]
   333 lemmas disjoint_finsert[simp,no_atp] = disjoint_insert[Transfer.transferred]
   334 lemmas fimage_fempty[simp] = image_empty[Transfer.transferred]
   335 lemmas fimage_finsert[simp] = image_insert[Transfer.transferred]
   336 lemmas fimage_constant = image_constant[Transfer.transferred]
   337 lemmas fimage_constant_conv = image_constant_conv[Transfer.transferred]
   338 lemmas fimage_fimage = image_image[Transfer.transferred]
   339 lemmas finsert_fimage[simp] = insert_image[Transfer.transferred]
   340 lemmas fimage_is_fempty[iff] = image_is_empty[Transfer.transferred]
   341 lemmas fempty_is_fimage[iff] = empty_is_image[Transfer.transferred]
   342 lemmas fimage_cong = image_cong[Transfer.transferred]
   343 lemmas fimage_finter_fsubset = image_Int_subset[Transfer.transferred]
   344 lemmas fimage_fminus_fsubset = image_diff_subset[Transfer.transferred]
   345 lemmas finter_absorb = Int_absorb[Transfer.transferred]
   346 lemmas finter_left_absorb = Int_left_absorb[Transfer.transferred]
   347 lemmas finter_commute = Int_commute[Transfer.transferred]
   348 lemmas finter_left_commute = Int_left_commute[Transfer.transferred]
   349 lemmas finter_assoc = Int_assoc[Transfer.transferred]
   350 lemmas finter_ac = Int_ac[Transfer.transferred]
   351 lemmas finter_absorb1 = Int_absorb1[Transfer.transferred]
   352 lemmas finter_absorb2 = Int_absorb2[Transfer.transferred]
   353 lemmas finter_fempty_left = Int_empty_left[Transfer.transferred]
   354 lemmas finter_fempty_right = Int_empty_right[Transfer.transferred]
   355 lemmas disjoint_iff_fnot_equal = disjoint_iff_not_equal[Transfer.transferred]
   356 lemmas finter_funion_distrib = Int_Un_distrib[Transfer.transferred]
   357 lemmas finter_funion_distrib2 = Int_Un_distrib2[Transfer.transferred]
   358 lemmas finter_fsubset_iff[no_atp, simp] = Int_subset_iff[Transfer.transferred]
   359 lemmas funion_absorb = Un_absorb[Transfer.transferred]
   360 lemmas funion_left_absorb = Un_left_absorb[Transfer.transferred]
   361 lemmas funion_commute = Un_commute[Transfer.transferred]
   362 lemmas funion_left_commute = Un_left_commute[Transfer.transferred]
   363 lemmas funion_assoc = Un_assoc[Transfer.transferred]
   364 lemmas funion_ac = Un_ac[Transfer.transferred]
   365 lemmas funion_absorb1 = Un_absorb1[Transfer.transferred]
   366 lemmas funion_absorb2 = Un_absorb2[Transfer.transferred]
   367 lemmas funion_fempty_left = Un_empty_left[Transfer.transferred]
   368 lemmas funion_fempty_right = Un_empty_right[Transfer.transferred]
   369 lemmas funion_finsert_left[simp] = Un_insert_left[Transfer.transferred]
   370 lemmas funion_finsert_right[simp] = Un_insert_right[Transfer.transferred]
   371 lemmas finter_finsert_left = Int_insert_left[Transfer.transferred]
   372 lemmas finter_finsert_left_ifffempty[simp] = Int_insert_left_if0[Transfer.transferred]
   373 lemmas finter_finsert_left_if1[simp] = Int_insert_left_if1[Transfer.transferred]
   374 lemmas finter_finsert_right = Int_insert_right[Transfer.transferred]
   375 lemmas finter_finsert_right_ifffempty[simp] = Int_insert_right_if0[Transfer.transferred]
   376 lemmas finter_finsert_right_if1[simp] = Int_insert_right_if1[Transfer.transferred]
   377 lemmas funion_finter_distrib = Un_Int_distrib[Transfer.transferred]
   378 lemmas funion_finter_distrib2 = Un_Int_distrib2[Transfer.transferred]
   379 lemmas funion_finter_crazy = Un_Int_crazy[Transfer.transferred]
   380 lemmas fsubset_funion_eq = subset_Un_eq[Transfer.transferred]
   381 lemmas funion_fempty[iff] = Un_empty[Transfer.transferred]
   382 lemmas funion_fsubset_iff[no_atp, simp] = Un_subset_iff[Transfer.transferred]
   383 lemmas funion_fminus_finter = Un_Diff_Int[Transfer.transferred]
   384 lemmas fminus_finter2 = Diff_Int2[Transfer.transferred]
   385 lemmas funion_finter_assoc_eq = Un_Int_assoc_eq[Transfer.transferred]
   386 lemmas fBall_funion = ball_Un[Transfer.transferred]
   387 lemmas fBex_funion = bex_Un[Transfer.transferred]
   388 lemmas fminus_eq_fempty_iff[simp,no_atp] = Diff_eq_empty_iff[Transfer.transferred]
   389 lemmas fminus_cancel[simp] = Diff_cancel[Transfer.transferred]
   390 lemmas fminus_idemp[simp] = Diff_idemp[Transfer.transferred]
   391 lemmas fminus_triv = Diff_triv[Transfer.transferred]
   392 lemmas fempty_fminus[simp] = empty_Diff[Transfer.transferred]
   393 lemmas fminus_fempty[simp] = Diff_empty[Transfer.transferred]
   394 lemmas fminus_finsertffempty[simp,no_atp] = Diff_insert0[Transfer.transferred]
   395 lemmas fminus_finsert = Diff_insert[Transfer.transferred]
   396 lemmas fminus_finsert2 = Diff_insert2[Transfer.transferred]
   397 lemmas finsert_fminus_if = insert_Diff_if[Transfer.transferred]
   398 lemmas finsert_fminus1[simp] = insert_Diff1[Transfer.transferred]
   399 lemmas finsert_fminus_single[simp] = insert_Diff_single[Transfer.transferred]
   400 lemmas finsert_fminus = insert_Diff[Transfer.transferred]
   401 lemmas fminus_finsert_absorb = Diff_insert_absorb[Transfer.transferred]
   402 lemmas fminus_disjoint[simp] = Diff_disjoint[Transfer.transferred]
   403 lemmas fminus_partition = Diff_partition[Transfer.transferred]
   404 lemmas double_fminus = double_diff[Transfer.transferred]
   405 lemmas funion_fminus_cancel[simp] = Un_Diff_cancel[Transfer.transferred]
   406 lemmas funion_fminus_cancel2[simp] = Un_Diff_cancel2[Transfer.transferred]
   407 lemmas fminus_funion = Diff_Un[Transfer.transferred]
   408 lemmas fminus_finter = Diff_Int[Transfer.transferred]
   409 lemmas funion_fminus = Un_Diff[Transfer.transferred]
   410 lemmas finter_fminus = Int_Diff[Transfer.transferred]
   411 lemmas fminus_finter_distrib = Diff_Int_distrib[Transfer.transferred]
   412 lemmas fminus_finter_distrib2 = Diff_Int_distrib2[Transfer.transferred]
   413 lemmas fUNIV_bool[no_atp] = UNIV_bool[Transfer.transferred]
   414 lemmas fPow_fempty[simp] = Pow_empty[Transfer.transferred]
   415 lemmas fPow_finsert = Pow_insert[Transfer.transferred]
   416 lemmas funion_fPow_fsubset = Un_Pow_subset[Transfer.transferred]
   417 lemmas fPow_finter_eq[simp] = Pow_Int_eq[Transfer.transferred]
   418 lemmas fset_eq_fsubset = set_eq_subset[Transfer.transferred]
   419 lemmas fsubset_iff[no_atp] = subset_iff[Transfer.transferred]
   420 lemmas fsubset_iff_pfsubset_eq = subset_iff_psubset_eq[Transfer.transferred]
   421 lemmas all_not_fin_conv[simp] = all_not_in_conv[Transfer.transferred]
   422 lemmas ex_fin_conv = ex_in_conv[Transfer.transferred]
   423 lemmas fimage_mono = image_mono[Transfer.transferred]
   424 lemmas fPow_mono = Pow_mono[Transfer.transferred]
   425 lemmas finsert_mono = insert_mono[Transfer.transferred]
   426 lemmas funion_mono = Un_mono[Transfer.transferred]
   427 lemmas finter_mono = Int_mono[Transfer.transferred]
   428 lemmas fminus_mono = Diff_mono[Transfer.transferred]
   429 lemmas fin_mono = in_mono[Transfer.transferred]
   430 lemmas fthe_felem_eq[simp] = the_elem_eq[Transfer.transferred]
   431 lemmas fLeast_mono = Least_mono[Transfer.transferred]
   432 lemmas fbind_fbind = bind_bind[Transfer.transferred]
   433 lemmas fempty_fbind[simp] = empty_bind[Transfer.transferred]
   434 lemmas nonfempty_fbind_const = nonempty_bind_const[Transfer.transferred]
   435 lemmas fbind_const = bind_const[Transfer.transferred]
   436 lemmas ffmember_filter[simp] = member_filter[Transfer.transferred]
   437 lemmas fequalityI = equalityI[Transfer.transferred]
   438 
   439 subsection {* Additional lemmas*}
   440 
   441 subsubsection {* @{text fsingleton} *}
   442 
   443 lemmas fsingletonE = fsingletonD [elim_format]
   444 
   445 subsubsection {* @{text femepty} *}
   446 
   447 lemma fempty_ffilter[simp]: "ffilter (\<lambda>_. False) A = {||}"
   448 by transfer auto
   449 
   450 (* FIXME, transferred doesn't work here *)
   451 lemma femptyE [elim!]: "a |\<in>| {||} \<Longrightarrow> P"
   452   by simp
   453 
   454 subsubsection {* @{text fset} *}
   455 
   456 lemmas fset_simps[simp] = bot_fset.rep_eq finsert.rep_eq
   457 
   458 lemma finite_fset [simp]: 
   459   shows "finite (fset S)"
   460   by transfer simp
   461 
   462 lemmas fset_cong = fset_inject
   463 
   464 lemma filter_fset [simp]:
   465   shows "fset (ffilter P xs) = Collect P \<inter> fset xs"
   466   by transfer auto
   467 
   468 lemma notin_fset: "x |\<notin>| S \<longleftrightarrow> x \<notin> fset S" by (simp add: fmember.rep_eq)
   469 
   470 lemmas inter_fset[simp] = inf_fset.rep_eq
   471 
   472 lemmas union_fset[simp] = sup_fset.rep_eq
   473 
   474 lemmas minus_fset[simp] = minus_fset.rep_eq
   475 
   476 subsubsection {* @{text filter_fset} *}
   477 
   478 lemma subset_ffilter: 
   479   "ffilter P A |\<subseteq>| ffilter Q A = (\<forall> x. x |\<in>| A \<longrightarrow> P x \<longrightarrow> Q x)"
   480   by transfer auto
   481 
   482 lemma eq_ffilter: 
   483   "(ffilter P A = ffilter Q A) = (\<forall>x. x |\<in>| A \<longrightarrow> P x = Q x)"
   484   by transfer auto
   485 
   486 lemma pfsubset_ffilter:
   487   "(\<And>x. x |\<in>| A \<Longrightarrow> P x \<Longrightarrow> Q x) \<Longrightarrow> (x |\<in>| A & \<not> P x & Q x) \<Longrightarrow> 
   488     ffilter P A |\<subset>| ffilter Q A"
   489   unfolding less_fset_def by (auto simp add: subset_ffilter eq_ffilter)
   490 
   491 subsubsection {* @{text finsert} *}
   492 
   493 (* FIXME, transferred doesn't work here *)
   494 lemma set_finsert:
   495   assumes "x |\<in>| A"
   496   obtains B where "A = finsert x B" and "x |\<notin>| B"
   497 using assms by transfer (metis Set.set_insert finite_insert)
   498 
   499 lemma mk_disjoint_finsert: "a |\<in>| A \<Longrightarrow> \<exists>B. A = finsert a B \<and> a |\<notin>| B"
   500   by (rule_tac x = "A |-| {|a|}" in exI, blast)
   501 
   502 subsubsection {* @{text fimage} *}
   503 
   504 lemma subset_fimage_iff: "(B |\<subseteq>| f|`|A) = (\<exists> AA. AA |\<subseteq>| A \<and> B = f|`|AA)"
   505 by transfer (metis mem_Collect_eq rev_finite_subset subset_image_iff)
   506 
   507 subsubsection {* bounded quantification *}
   508 
   509 lemma bex_simps [simp, no_atp]:
   510   "\<And>A P Q. fBex A (\<lambda>x. P x \<and> Q) = (fBex A P \<and> Q)" 
   511   "\<And>A P Q. fBex A (\<lambda>x. P \<and> Q x) = (P \<and> fBex A Q)"
   512   "\<And>P. fBex {||} P = False" 
   513   "\<And>a B P. fBex (finsert a B) P = (P a \<or> fBex B P)"
   514   "\<And>A P f. fBex (f |`| A) P = fBex A (\<lambda>x. P (f x))"
   515   "\<And>A P. (\<not> fBex A P) = fBall A (\<lambda>x. \<not> P x)"
   516 by auto
   517 
   518 lemma ball_simps [simp, no_atp]:
   519   "\<And>A P Q. fBall A (\<lambda>x. P x \<or> Q) = (fBall A P \<or> Q)"
   520   "\<And>A P Q. fBall A (\<lambda>x. P \<or> Q x) = (P \<or> fBall A Q)"
   521   "\<And>A P Q. fBall A (\<lambda>x. P \<longrightarrow> Q x) = (P \<longrightarrow> fBall A Q)"
   522   "\<And>A P Q. fBall A (\<lambda>x. P x \<longrightarrow> Q) = (fBex A P \<longrightarrow> Q)"
   523   "\<And>P. fBall {||} P = True"
   524   "\<And>a B P. fBall (finsert a B) P = (P a \<and> fBall B P)"
   525   "\<And>A P f. fBall (f |`| A) P = fBall A (\<lambda>x. P (f x))"
   526   "\<And>A P. (\<not> fBall A P) = fBex A (\<lambda>x. \<not> P x)"
   527 by auto
   528 
   529 lemma atomize_fBall:
   530     "(\<And>x. x |\<in>| A ==> P x) == Trueprop (fBall A (\<lambda>x. P x))"
   531 apply (simp only: atomize_all atomize_imp)
   532 apply (rule equal_intr_rule)
   533 by (transfer, simp)+
   534 
   535 end
   536 
   537 subsubsection {* @{text fcard} *}
   538 
   539 (* FIXME: improve transferred to handle bounded meta quantification *)
   540 
   541 lemma fcard_fempty:
   542   "fcard {||} = 0"
   543   by transfer (rule card_empty)
   544 
   545 lemma fcard_finsert_disjoint:
   546   "x |\<notin>| A \<Longrightarrow> fcard (finsert x A) = Suc (fcard A)"
   547   by transfer (rule card_insert_disjoint)
   548 
   549 lemma fcard_finsert_if:
   550   "fcard (finsert x A) = (if x |\<in>| A then fcard A else Suc (fcard A))"
   551   by transfer (rule card_insert_if)
   552 
   553 lemma card_0_eq [simp, no_atp]:
   554   "fcard A = 0 \<longleftrightarrow> A = {||}"
   555   by transfer (rule card_0_eq)
   556 
   557 lemma fcard_Suc_fminus1:
   558   "x |\<in>| A \<Longrightarrow> Suc (fcard (A |-| {|x|})) = fcard A"
   559   by transfer (rule card_Suc_Diff1)
   560 
   561 lemma fcard_fminus_fsingleton:
   562   "x |\<in>| A \<Longrightarrow> fcard (A |-| {|x|}) = fcard A - 1"
   563   by transfer (rule card_Diff_singleton)
   564 
   565 lemma fcard_fminus_fsingleton_if:
   566   "fcard (A |-| {|x|}) = (if x |\<in>| A then fcard A - 1 else fcard A)"
   567   by transfer (rule card_Diff_singleton_if)
   568 
   569 lemma fcard_fminus_finsert[simp]:
   570   assumes "a |\<in>| A" and "a |\<notin>| B"
   571   shows "fcard (A |-| finsert a B) = fcard (A |-| B) - 1"
   572 using assms by transfer (rule card_Diff_insert)
   573 
   574 lemma fcard_finsert: "fcard (finsert x A) = Suc (fcard (A |-| {|x|}))"
   575 by transfer (rule card_insert)
   576 
   577 lemma fcard_finsert_le: "fcard A \<le> fcard (finsert x A)"
   578 by transfer (rule card_insert_le)
   579 
   580 lemma fcard_mono:
   581   "A |\<subseteq>| B \<Longrightarrow> fcard A \<le> fcard B"
   582 by transfer (rule card_mono)
   583 
   584 lemma fcard_seteq: "A |\<subseteq>| B \<Longrightarrow> fcard B \<le> fcard A \<Longrightarrow> A = B"
   585 by transfer (rule card_seteq)
   586 
   587 lemma pfsubset_fcard_mono: "A |\<subset>| B \<Longrightarrow> fcard A < fcard B"
   588 by transfer (rule psubset_card_mono)
   589 
   590 lemma fcard_funion_finter: 
   591   "fcard A + fcard B = fcard (A |\<union>| B) + fcard (A |\<inter>| B)"
   592 by transfer (rule card_Un_Int)
   593 
   594 lemma fcard_funion_disjoint:
   595   "A |\<inter>| B = {||} \<Longrightarrow> fcard (A |\<union>| B) = fcard A + fcard B"
   596 by transfer (rule card_Un_disjoint)
   597 
   598 lemma fcard_funion_fsubset:
   599   "B |\<subseteq>| A \<Longrightarrow> fcard (A |-| B) = fcard A - fcard B"
   600 by transfer (rule card_Diff_subset)
   601 
   602 lemma diff_fcard_le_fcard_fminus:
   603   "fcard A - fcard B \<le> fcard(A |-| B)"
   604 by transfer (rule diff_card_le_card_Diff)
   605 
   606 lemma fcard_fminus1_less: "x |\<in>| A \<Longrightarrow> fcard (A |-| {|x|}) < fcard A"
   607 by transfer (rule card_Diff1_less)
   608 
   609 lemma fcard_fminus2_less:
   610   "x |\<in>| A \<Longrightarrow> y |\<in>| A \<Longrightarrow> fcard (A |-| {|x|} |-| {|y|}) < fcard A"
   611 by transfer (rule card_Diff2_less)
   612 
   613 lemma fcard_fminus1_le: "fcard (A |-| {|x|}) \<le> fcard A"
   614 by transfer (rule card_Diff1_le)
   615 
   616 lemma fcard_pfsubset: "A |\<subseteq>| B \<Longrightarrow> fcard A < fcard B \<Longrightarrow> A < B"
   617 by transfer (rule card_psubset)
   618 
   619 subsubsection {* @{text ffold} *}
   620 
   621 (* FIXME: improve transferred to handle bounded meta quantification *)
   622 
   623 context comp_fun_commute
   624 begin
   625   lemmas ffold_empty[simp] = fold_empty[Transfer.transferred]
   626 
   627   lemma ffold_finsert [simp]:
   628     assumes "x |\<notin>| A"
   629     shows "ffold f z (finsert x A) = f x (ffold f z A)"
   630     using assms by (transfer fixing: f) (rule fold_insert)
   631 
   632   lemma ffold_fun_left_comm:
   633     "f x (ffold f z A) = ffold f (f x z) A"
   634     by (transfer fixing: f) (rule fold_fun_left_comm)
   635 
   636   lemma ffold_finsert2:
   637     "x |\<notin>| A \<Longrightarrow> ffold f z (finsert x A)  = ffold f (f x z) A"
   638     by (transfer fixing: f) (rule fold_insert2)
   639 
   640   lemma ffold_rec:
   641     assumes "x |\<in>| A"
   642     shows "ffold f z A = f x (ffold f z (A |-| {|x|}))"
   643     using assms by (transfer fixing: f) (rule fold_rec)
   644   
   645   lemma ffold_finsert_fremove:
   646     "ffold f z (finsert x A) = f x (ffold f z (A |-| {|x|}))"
   647      by (transfer fixing: f) (rule fold_insert_remove)
   648 end
   649 
   650 lemma ffold_fimage:
   651   assumes "inj_on g (fset A)"
   652   shows "ffold f z (g |`| A) = ffold (f \<circ> g) z A"
   653 using assms by transfer' (rule fold_image)
   654 
   655 lemma ffold_cong:
   656   assumes "comp_fun_commute f" "comp_fun_commute g"
   657   "\<And>x. x |\<in>| A \<Longrightarrow> f x = g x"
   658     and "s = t" and "A = B"
   659   shows "ffold f s A = ffold g t B"
   660 using assms by transfer (metis Finite_Set.fold_cong)
   661 
   662 context comp_fun_idem
   663 begin
   664 
   665   lemma ffold_finsert_idem:
   666     "ffold f z (finsert x A)  = f x (ffold f z A)"
   667     by (transfer fixing: f) (rule fold_insert_idem)
   668   
   669   declare ffold_finsert [simp del] ffold_finsert_idem [simp]
   670   
   671   lemma ffold_finsert_idem2:
   672     "ffold f z (finsert x A) = ffold f (f x z) A"
   673     by (transfer fixing: f) (rule fold_insert_idem2)
   674 
   675 end
   676 
   677 subsection {* Choice in fsets *}
   678 
   679 lemma fset_choice: 
   680   assumes "\<forall>x. x |\<in>| A \<longrightarrow> (\<exists>y. P x y)"
   681   shows "\<exists>f. \<forall>x. x |\<in>| A \<longrightarrow> P x (f x)"
   682   using assms by transfer metis
   683 
   684 subsection {* Induction and Cases rules for fsets *}
   685 
   686 lemma fset_exhaust [case_names empty insert, cases type: fset]:
   687   assumes fempty_case: "S = {||} \<Longrightarrow> P" 
   688   and     finsert_case: "\<And>x S'. S = finsert x S' \<Longrightarrow> P"
   689   shows "P"
   690   using assms by transfer blast
   691 
   692 lemma fset_induct [case_names empty insert]:
   693   assumes fempty_case: "P {||}"
   694   and     finsert_case: "\<And>x S. P S \<Longrightarrow> P (finsert x S)"
   695   shows "P S"
   696 proof -
   697   (* FIXME transfer and right_total vs. bi_total *)
   698   note Domainp_forall_transfer[transfer_rule]
   699   show ?thesis
   700   using assms by transfer (auto intro: finite_induct)
   701 qed
   702 
   703 lemma fset_induct_stronger [case_names empty insert, induct type: fset]:
   704   assumes empty_fset_case: "P {||}"
   705   and     insert_fset_case: "\<And>x S. \<lbrakk>x |\<notin>| S; P S\<rbrakk> \<Longrightarrow> P (finsert x S)"
   706   shows "P S"
   707 proof -
   708   (* FIXME transfer and right_total vs. bi_total *)
   709   note Domainp_forall_transfer[transfer_rule]
   710   show ?thesis
   711   using assms by transfer (auto intro: finite_induct)
   712 qed
   713 
   714 lemma fset_card_induct:
   715   assumes empty_fset_case: "P {||}"
   716   and     card_fset_Suc_case: "\<And>S T. Suc (fcard S) = (fcard T) \<Longrightarrow> P S \<Longrightarrow> P T"
   717   shows "P S"
   718 proof (induct S)
   719   case empty
   720   show "P {||}" by (rule empty_fset_case)
   721 next
   722   case (insert x S)
   723   have h: "P S" by fact
   724   have "x |\<notin>| S" by fact
   725   then have "Suc (fcard S) = fcard (finsert x S)" 
   726     by transfer auto
   727   then show "P (finsert x S)" 
   728     using h card_fset_Suc_case by simp
   729 qed
   730 
   731 lemma fset_strong_cases:
   732   obtains "xs = {||}"
   733     | ys x where "x |\<notin>| ys" and "xs = finsert x ys"
   734 by transfer blast
   735 
   736 lemma fset_induct2:
   737   "P {||} {||} \<Longrightarrow>
   738   (\<And>x xs. x |\<notin>| xs \<Longrightarrow> P (finsert x xs) {||}) \<Longrightarrow>
   739   (\<And>y ys. y |\<notin>| ys \<Longrightarrow> P {||} (finsert y ys)) \<Longrightarrow>
   740   (\<And>x xs y ys. \<lbrakk>P xs ys; x |\<notin>| xs; y |\<notin>| ys\<rbrakk> \<Longrightarrow> P (finsert x xs) (finsert y ys)) \<Longrightarrow>
   741   P xsa ysa"
   742   apply (induct xsa arbitrary: ysa)
   743   apply (induct_tac x rule: fset_induct_stronger)
   744   apply simp_all
   745   apply (induct_tac xa rule: fset_induct_stronger)
   746   apply simp_all
   747   done
   748 
   749 subsection {* Setup for Lifting/Transfer *}
   750 
   751 subsubsection {* Relator and predicator properties *}
   752 
   753 lift_definition fset_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'b fset \<Rightarrow> bool" is set_rel
   754 parametric set_rel_transfer ..
   755 
   756 lemma fset_rel_alt_def: "fset_rel R = (\<lambda>A B. (\<forall>x.\<exists>y. x|\<in>|A \<longrightarrow> y|\<in>|B \<and> R x y) 
   757   \<and> (\<forall>y. \<exists>x. y|\<in>|B \<longrightarrow> x|\<in>|A \<and> R x y))"
   758 apply (rule ext)+
   759 apply transfer'
   760 apply (subst set_rel_def[unfolded fun_eq_iff]) 
   761 by blast
   762 
   763 lemma fset_rel_conversep: "fset_rel (conversep R) = conversep (fset_rel R)"
   764   unfolding fset_rel_alt_def by auto
   765 
   766 lemmas fset_rel_eq [relator_eq] = set_rel_eq[Transfer.transferred]
   767 
   768 lemma fset_rel_mono[relator_mono]: "A \<le> B \<Longrightarrow> fset_rel A \<le> fset_rel B"
   769 unfolding fset_rel_alt_def by blast
   770 
   771 lemma finite_set_rel:
   772   assumes fin: "finite X" "finite Z"
   773   assumes R_S: "set_rel (R OO S) X Z"
   774   shows "\<exists>Y. finite Y \<and> set_rel R X Y \<and> set_rel S Y Z"
   775 proof -
   776   obtain f where f: "\<forall>x\<in>X. R x (f x) \<and> (\<exists>z\<in>Z. S (f x) z)"
   777   apply atomize_elim
   778   apply (subst bchoice_iff[symmetric])
   779   using R_S[unfolded set_rel_def OO_def] by blast
   780   
   781   obtain g where g: "\<forall>z\<in>Z. S (g z) z \<and> (\<exists>x\<in>X. R  x (g z))"
   782   apply atomize_elim
   783   apply (subst bchoice_iff[symmetric])
   784   using R_S[unfolded set_rel_def OO_def] by blast
   785   
   786   let ?Y = "f ` X \<union> g ` Z"
   787   have "finite ?Y" by (simp add: fin)
   788   moreover have "set_rel R X ?Y"
   789     unfolding set_rel_def
   790     using f g by clarsimp blast
   791   moreover have "set_rel S ?Y Z"
   792     unfolding set_rel_def
   793     using f g by clarsimp blast
   794   ultimately show ?thesis by metis
   795 qed
   796 
   797 lemma fset_rel_OO[relator_distr]: "fset_rel R OO fset_rel S = fset_rel (R OO S)"
   798 apply (rule ext)+
   799 by transfer (auto intro: finite_set_rel set_rel_OO[unfolded fun_eq_iff, rule_format, THEN iffD1])
   800 
   801 lemma Domainp_fset[relator_domain]:
   802   assumes "Domainp T = P"
   803   shows "Domainp (fset_rel T) = (\<lambda>A. fBall A P)"
   804 proof -
   805   from assms obtain f where f: "\<forall>x\<in>Collect P. T x (f x)"
   806     unfolding Domainp_iff[abs_def]
   807     apply atomize_elim
   808     by (subst bchoice_iff[symmetric]) auto
   809   from assms f show ?thesis
   810     unfolding fun_eq_iff fset_rel_alt_def Domainp_iff
   811     apply clarify
   812     apply (rule iffI)
   813       apply blast
   814     by (rename_tac A, rule_tac x="f |`| A" in exI, blast)
   815 qed
   816 
   817 lemmas reflp_fset_rel[reflexivity_rule] = reflp_set_rel[Transfer.transferred]
   818 
   819 lemma right_total_fset_rel[transfer_rule]: "right_total A \<Longrightarrow> right_total (fset_rel A)"
   820 unfolding right_total_def 
   821 apply transfer
   822 apply (subst(asm) choice_iff)
   823 apply clarsimp
   824 apply (rename_tac A f y, rule_tac x = "f ` y" in exI)
   825 by (auto simp add: set_rel_def)
   826 
   827 lemma left_total_fset_rel[reflexivity_rule]: "left_total A \<Longrightarrow> left_total (fset_rel A)"
   828 unfolding left_total_def 
   829 apply transfer
   830 apply (subst(asm) choice_iff)
   831 apply clarsimp
   832 apply (rename_tac A f y, rule_tac x = "f ` y" in exI)
   833 by (auto simp add: set_rel_def)
   834 
   835 lemmas right_unique_fset_rel[transfer_rule] = right_unique_set_rel[Transfer.transferred]
   836 lemmas left_unique_fset_rel[reflexivity_rule] = left_unique_set_rel[Transfer.transferred]
   837 
   838 thm right_unique_fset_rel left_unique_fset_rel
   839 
   840 lemma bi_unique_fset_rel[transfer_rule]: "bi_unique A \<Longrightarrow> bi_unique (fset_rel A)"
   841 by (auto intro: right_unique_fset_rel left_unique_fset_rel iff: bi_unique_iff)
   842 
   843 lemma bi_total_fset_rel[transfer_rule]: "bi_total A \<Longrightarrow> bi_total (fset_rel A)"
   844 by (auto intro: right_total_fset_rel left_total_fset_rel iff: bi_total_iff)
   845 
   846 lemmas fset_invariant_commute [invariant_commute] = set_invariant_commute[Transfer.transferred]
   847 
   848 subsubsection {* Quotient theorem for the Lifting package *}
   849 
   850 lemma Quotient_fset_map[quot_map]:
   851   assumes "Quotient R Abs Rep T"
   852   shows "Quotient (fset_rel R) (fimage Abs) (fimage Rep) (fset_rel T)"
   853   using assms unfolding Quotient_alt_def4
   854   by (simp add: fset_rel_OO[symmetric] fset_rel_conversep) (simp add: fset_rel_alt_def, blast)
   855 
   856 subsubsection {* Transfer rules for the Transfer package *}
   857 
   858 text {* Unconditional transfer rules *}
   859 
   860 context
   861 begin
   862 
   863 interpretation lifting_syntax .
   864 
   865 lemmas fempty_transfer [transfer_rule] = empty_transfer[Transfer.transferred]
   866 
   867 lemma finsert_transfer [transfer_rule]:
   868   "(A ===> fset_rel A ===> fset_rel A) finsert finsert"
   869   unfolding fun_rel_def fset_rel_alt_def by blast
   870 
   871 lemma funion_transfer [transfer_rule]:
   872   "(fset_rel A ===> fset_rel A ===> fset_rel A) funion funion"
   873   unfolding fun_rel_def fset_rel_alt_def by blast
   874 
   875 lemma ffUnion_transfer [transfer_rule]:
   876   "(fset_rel (fset_rel A) ===> fset_rel A) ffUnion ffUnion"
   877   unfolding fun_rel_def fset_rel_alt_def by transfer (simp, fast)
   878 
   879 lemma fimage_transfer [transfer_rule]:
   880   "((A ===> B) ===> fset_rel A ===> fset_rel B) fimage fimage"
   881   unfolding fun_rel_def fset_rel_alt_def by simp blast
   882 
   883 lemma fBall_transfer [transfer_rule]:
   884   "(fset_rel A ===> (A ===> op =) ===> op =) fBall fBall"
   885   unfolding fset_rel_alt_def fun_rel_def by blast
   886 
   887 lemma fBex_transfer [transfer_rule]:
   888   "(fset_rel A ===> (A ===> op =) ===> op =) fBex fBex"
   889   unfolding fset_rel_alt_def fun_rel_def by blast
   890 
   891 (* FIXME transfer doesn't work here *)
   892 lemma fPow_transfer [transfer_rule]:
   893   "(fset_rel A ===> fset_rel (fset_rel A)) fPow fPow"
   894   unfolding fun_rel_def
   895   using Pow_transfer[unfolded fun_rel_def, rule_format, Transfer.transferred]
   896   by blast
   897 
   898 lemma fset_rel_transfer [transfer_rule]:
   899   "((A ===> B ===> op =) ===> fset_rel A ===> fset_rel B ===> op =)
   900     fset_rel fset_rel"
   901   unfolding fun_rel_def
   902   using set_rel_transfer[unfolded fun_rel_def,rule_format, Transfer.transferred, where A = A and B = B]
   903   by simp
   904 
   905 lemma bind_transfer [transfer_rule]:
   906   "(fset_rel A ===> (A ===> fset_rel B) ===> fset_rel B) fbind fbind"
   907   using assms unfolding fun_rel_def
   908   using bind_transfer[unfolded fun_rel_def, rule_format, Transfer.transferred] by blast
   909 
   910 text {* Rules requiring bi-unique, bi-total or right-total relations *}
   911 
   912 lemma fmember_transfer [transfer_rule]:
   913   assumes "bi_unique A"
   914   shows "(A ===> fset_rel A ===> op =) (op |\<in>|) (op |\<in>|)"
   915   using assms unfolding fun_rel_def fset_rel_alt_def bi_unique_def by metis
   916 
   917 lemma finter_transfer [transfer_rule]:
   918   assumes "bi_unique A"
   919   shows "(fset_rel A ===> fset_rel A ===> fset_rel A) finter finter"
   920   using assms unfolding fun_rel_def
   921   using inter_transfer[unfolded fun_rel_def, rule_format, Transfer.transferred] by blast
   922 
   923 lemma fminus_transfer [transfer_rule]:
   924   assumes "bi_unique A"
   925   shows "(fset_rel A ===> fset_rel A ===> fset_rel A) (op |-|) (op |-|)"
   926   using assms unfolding fun_rel_def
   927   using Diff_transfer[unfolded fun_rel_def, rule_format, Transfer.transferred] by blast
   928 
   929 lemma fsubset_transfer [transfer_rule]:
   930   assumes "bi_unique A"
   931   shows "(fset_rel A ===> fset_rel A ===> op =) (op |\<subseteq>|) (op |\<subseteq>|)"
   932   using assms unfolding fun_rel_def
   933   using subset_transfer[unfolded fun_rel_def, rule_format, Transfer.transferred] by blast
   934 
   935 lemma fSup_transfer [transfer_rule]:
   936   "bi_unique A \<Longrightarrow> (set_rel (fset_rel A) ===> fset_rel A) Sup Sup"
   937   using assms unfolding fun_rel_def
   938   apply clarify
   939   apply transfer'
   940   using Sup_fset_transfer[unfolded fun_rel_def] by blast
   941 
   942 (* FIXME: add right_total_fInf_transfer *)
   943 
   944 lemma fInf_transfer [transfer_rule]:
   945   assumes "bi_unique A" and "bi_total A"
   946   shows "(set_rel (fset_rel A) ===> fset_rel A) Inf Inf"
   947   using assms unfolding fun_rel_def
   948   apply clarify
   949   apply transfer'
   950   using Inf_fset_transfer[unfolded fun_rel_def] by blast
   951 
   952 lemma ffilter_transfer [transfer_rule]:
   953   assumes "bi_unique A"
   954   shows "((A ===> op=) ===> fset_rel A ===> fset_rel A) ffilter ffilter"
   955   using assms unfolding fun_rel_def
   956   using Lifting_Set.filter_transfer[unfolded fun_rel_def, rule_format, Transfer.transferred] by blast
   957 
   958 lemma card_transfer [transfer_rule]:
   959   "bi_unique A \<Longrightarrow> (fset_rel A ===> op =) fcard fcard"
   960   using assms unfolding fun_rel_def
   961   using card_transfer[unfolded fun_rel_def, rule_format, Transfer.transferred] by blast
   962 
   963 end
   964 
   965 lifting_update fset.lifting
   966 lifting_forget fset.lifting
   967 
   968 end