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