src/HOL/Library/Countable_Set_Type.thy
author wenzelm
Thu Nov 05 10:39:49 2015 +0100 (2015-11-05)
changeset 61585 a9599d3d7610
parent 61424 c3658c18b7bc
child 61952 546958347e05
permissions -rw-r--r--
isabelle update_cartouches -c -t;
     1 (*  Title:      HOL/Library/Countable_Set_Type.thy
     2     Author:     Andrei Popescu, TU Muenchen
     3     Author:     Andreas Lochbihler, ETH Zurich
     4     Copyright   2012
     5 
     6 Type of (at most) countable sets.
     7 *)
     8 
     9 section \<open>Type of (at Most) Countable Sets\<close>
    10 
    11 theory Countable_Set_Type
    12 imports Countable_Set Cardinal_Notations Conditionally_Complete_Lattices
    13 begin
    14 
    15 
    16 subsection\<open>Cardinal stuff\<close>
    17 
    18 lemma countable_card_of_nat: "countable A \<longleftrightarrow> |A| \<le>o |UNIV::nat set|"
    19   unfolding countable_def card_of_ordLeq[symmetric] by auto
    20 
    21 lemma countable_card_le_natLeq: "countable A \<longleftrightarrow> |A| \<le>o natLeq"
    22   unfolding countable_card_of_nat using card_of_nat ordLeq_ordIso_trans ordIso_symmetric by blast
    23 
    24 lemma countable_or_card_of:
    25 assumes "countable A"
    26 shows "(finite A \<and> |A| <o |UNIV::nat set| ) \<or>
    27        (infinite A  \<and> |A| =o |UNIV::nat set| )"
    28 by (metis assms countable_card_of_nat infinite_iff_card_of_nat ordIso_iff_ordLeq
    29       ordLeq_iff_ordLess_or_ordIso)
    30 
    31 lemma countable_cases_card_of[elim]:
    32   assumes "countable A"
    33   obtains (Fin) "finite A" "|A| <o |UNIV::nat set|"
    34         | (Inf) "infinite A" "|A| =o |UNIV::nat set|"
    35   using assms countable_or_card_of by blast
    36 
    37 lemma countable_or:
    38   "countable A \<Longrightarrow> (\<exists> f::'a\<Rightarrow>nat. finite A \<and> inj_on f A) \<or> (\<exists> f::'a\<Rightarrow>nat. infinite A \<and> bij_betw f A UNIV)"
    39   by (elim countable_enum_cases) fastforce+
    40 
    41 lemma countable_cases[elim]:
    42   assumes "countable A"
    43   obtains (Fin) f :: "'a\<Rightarrow>nat" where "finite A" "inj_on f A"
    44         | (Inf) f :: "'a\<Rightarrow>nat" where "infinite A" "bij_betw f A UNIV"
    45   using assms countable_or by metis
    46 
    47 lemma countable_ordLeq:
    48 assumes "|A| \<le>o |B|" and "countable B"
    49 shows "countable A"
    50 using assms unfolding countable_card_of_nat by(rule ordLeq_transitive)
    51 
    52 lemma countable_ordLess:
    53 assumes AB: "|A| <o |B|" and B: "countable B"
    54 shows "countable A"
    55 using countable_ordLeq[OF ordLess_imp_ordLeq[OF AB] B] .
    56 
    57 subsection \<open>The type of countable sets\<close>
    58 
    59 typedef 'a cset = "{A :: 'a set. countable A}" morphisms rcset acset
    60   by (rule exI[of _ "{}"]) simp
    61 
    62 setup_lifting type_definition_cset
    63 
    64 declare
    65   rcset_inverse[simp]
    66   acset_inverse[Transfer.transferred, unfolded mem_Collect_eq, simp]
    67   acset_inject[Transfer.transferred, unfolded mem_Collect_eq, simp]
    68   rcset[Transfer.transferred, unfolded mem_Collect_eq, simp]
    69 
    70 instantiation cset :: (type) "{bounded_lattice_bot, distrib_lattice, minus}"
    71 begin
    72 
    73 interpretation lifting_syntax .
    74 
    75 lift_definition bot_cset :: "'a cset" is "{}" parametric empty_transfer by simp 
    76 
    77 lift_definition less_eq_cset :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> bool"
    78   is subset_eq parametric subset_transfer .
    79 
    80 definition less_cset :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> bool"
    81 where "xs < ys \<equiv> xs \<le> ys \<and> xs \<noteq> (ys::'a cset)"
    82 
    83 lemma less_cset_transfer[transfer_rule]:
    84   assumes [transfer_rule]: "bi_unique A" 
    85   shows "((pcr_cset A) ===> (pcr_cset A) ===> op =) op \<subset> op <"
    86 unfolding less_cset_def[abs_def] psubset_eq[abs_def] by transfer_prover
    87 
    88 lift_definition sup_cset :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> 'a cset"
    89 is union parametric union_transfer by simp
    90 
    91 lift_definition inf_cset :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> 'a cset"
    92 is inter parametric inter_transfer by simp
    93 
    94 lift_definition minus_cset :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> 'a cset"
    95 is minus parametric Diff_transfer by simp
    96 
    97 instance by standard (transfer; auto)+
    98 
    99 end
   100 
   101 abbreviation cempty :: "'a cset" where "cempty \<equiv> bot"
   102 abbreviation csubset_eq :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> bool" where "csubset_eq xs ys \<equiv> xs \<le> ys"
   103 abbreviation csubset :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> bool" where "csubset xs ys \<equiv> xs < ys"
   104 abbreviation cUn :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> 'a cset" where "cUn xs ys \<equiv> sup xs ys"
   105 abbreviation cInt :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> 'a cset" where "cInt xs ys \<equiv> inf xs ys"
   106 abbreviation cDiff :: "'a cset \<Rightarrow> 'a cset \<Rightarrow> 'a cset" where "cDiff xs ys \<equiv> minus xs ys"
   107 
   108 lift_definition cin :: "'a \<Rightarrow> 'a cset \<Rightarrow> bool" is "op \<in>" parametric member_transfer
   109   .
   110 lift_definition cinsert :: "'a \<Rightarrow> 'a cset \<Rightarrow> 'a cset" is insert parametric Lifting_Set.insert_transfer
   111   by (rule countable_insert)
   112 abbreviation csingle :: "'a \<Rightarrow> 'a cset" where "csingle x \<equiv> cinsert x cempty"
   113 lift_definition cimage :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a cset \<Rightarrow> 'b cset" is "op `" parametric image_transfer
   114   by (rule countable_image)
   115 lift_definition cBall :: "'a cset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" is Ball parametric Ball_transfer .
   116 lift_definition cBex :: "'a cset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" is Bex parametric Bex_transfer .
   117 lift_definition cUNION :: "'a cset \<Rightarrow> ('a \<Rightarrow> 'b cset) \<Rightarrow> 'b cset"
   118   is "UNION" parametric UNION_transfer by simp
   119 definition cUnion :: "'a cset cset \<Rightarrow> 'a cset" where "cUnion A = cUNION A id"
   120 
   121 lemma Union_conv_UNION: "Union A = UNION A id"
   122 by auto
   123 
   124 lemma cUnion_transfer [transfer_rule]:
   125   "rel_fun (pcr_cset (pcr_cset A)) (pcr_cset A) Union cUnion"
   126 unfolding cUnion_def[abs_def] Union_conv_UNION[abs_def] by transfer_prover
   127 
   128 lemmas cset_eqI = set_eqI[Transfer.transferred]
   129 lemmas cset_eq_iff[no_atp] = set_eq_iff[Transfer.transferred]
   130 lemmas cBallI[intro!] = ballI[Transfer.transferred]
   131 lemmas cbspec[dest?] = bspec[Transfer.transferred]
   132 lemmas cBallE[elim] = ballE[Transfer.transferred]
   133 lemmas cBexI[intro] = bexI[Transfer.transferred]
   134 lemmas rev_cBexI[intro?] = rev_bexI[Transfer.transferred]
   135 lemmas cBexCI = bexCI[Transfer.transferred]
   136 lemmas cBexE[elim!] = bexE[Transfer.transferred]
   137 lemmas cBall_triv[simp] = ball_triv[Transfer.transferred]
   138 lemmas cBex_triv[simp] = bex_triv[Transfer.transferred]
   139 lemmas cBex_triv_one_point1[simp] = bex_triv_one_point1[Transfer.transferred]
   140 lemmas cBex_triv_one_point2[simp] = bex_triv_one_point2[Transfer.transferred]
   141 lemmas cBex_one_point1[simp] = bex_one_point1[Transfer.transferred]
   142 lemmas cBex_one_point2[simp] = bex_one_point2[Transfer.transferred]
   143 lemmas cBall_one_point1[simp] = ball_one_point1[Transfer.transferred]
   144 lemmas cBall_one_point2[simp] = ball_one_point2[Transfer.transferred]
   145 lemmas cBall_conj_distrib = ball_conj_distrib[Transfer.transferred]
   146 lemmas cBex_disj_distrib = bex_disj_distrib[Transfer.transferred]
   147 lemmas cBall_cong = ball_cong[Transfer.transferred]
   148 lemmas cBex_cong = bex_cong[Transfer.transferred]
   149 lemmas csubsetI[intro!] = subsetI[Transfer.transferred]
   150 lemmas csubsetD[elim, intro?] = subsetD[Transfer.transferred]
   151 lemmas rev_csubsetD[no_atp,intro?] = rev_subsetD[Transfer.transferred]
   152 lemmas csubsetCE[no_atp,elim] = subsetCE[Transfer.transferred]
   153 lemmas csubset_eq[no_atp] = subset_eq[Transfer.transferred]
   154 lemmas contra_csubsetD[no_atp] = contra_subsetD[Transfer.transferred]
   155 lemmas csubset_refl = subset_refl[Transfer.transferred]
   156 lemmas csubset_trans = subset_trans[Transfer.transferred]
   157 lemmas cset_rev_mp = set_rev_mp[Transfer.transferred]
   158 lemmas cset_mp = set_mp[Transfer.transferred]
   159 lemmas csubset_not_fsubset_eq[code] = subset_not_subset_eq[Transfer.transferred]
   160 lemmas eq_cmem_trans = eq_mem_trans[Transfer.transferred]
   161 lemmas csubset_antisym[intro!] = subset_antisym[Transfer.transferred]
   162 lemmas cequalityD1 = equalityD1[Transfer.transferred]
   163 lemmas cequalityD2 = equalityD2[Transfer.transferred]
   164 lemmas cequalityE = equalityE[Transfer.transferred]
   165 lemmas cequalityCE[elim] = equalityCE[Transfer.transferred]
   166 lemmas eqcset_imp_iff = eqset_imp_iff[Transfer.transferred]
   167 lemmas eqcelem_imp_iff = eqelem_imp_iff[Transfer.transferred]
   168 lemmas cempty_iff[simp] = empty_iff[Transfer.transferred]
   169 lemmas cempty_fsubsetI[iff] = empty_subsetI[Transfer.transferred]
   170 lemmas equals_cemptyI = equals0I[Transfer.transferred]
   171 lemmas equals_cemptyD = equals0D[Transfer.transferred]
   172 lemmas cBall_cempty[simp] = ball_empty[Transfer.transferred]
   173 lemmas cBex_cempty[simp] = bex_empty[Transfer.transferred]
   174 lemmas cInt_iff[simp] = Int_iff[Transfer.transferred]
   175 lemmas cIntI[intro!] = IntI[Transfer.transferred]
   176 lemmas cIntD1 = IntD1[Transfer.transferred]
   177 lemmas cIntD2 = IntD2[Transfer.transferred]
   178 lemmas cIntE[elim!] = IntE[Transfer.transferred]
   179 lemmas cUn_iff[simp] = Un_iff[Transfer.transferred]
   180 lemmas cUnI1[elim?] = UnI1[Transfer.transferred]
   181 lemmas cUnI2[elim?] = UnI2[Transfer.transferred]
   182 lemmas cUnCI[intro!] = UnCI[Transfer.transferred]
   183 lemmas cuUnE[elim!] = UnE[Transfer.transferred]
   184 lemmas cDiff_iff[simp] = Diff_iff[Transfer.transferred]
   185 lemmas cDiffI[intro!] = DiffI[Transfer.transferred]
   186 lemmas cDiffD1 = DiffD1[Transfer.transferred]
   187 lemmas cDiffD2 = DiffD2[Transfer.transferred]
   188 lemmas cDiffE[elim!] = DiffE[Transfer.transferred]
   189 lemmas cinsert_iff[simp] = insert_iff[Transfer.transferred]
   190 lemmas cinsertI1 = insertI1[Transfer.transferred]
   191 lemmas cinsertI2 = insertI2[Transfer.transferred]
   192 lemmas cinsertE[elim!] = insertE[Transfer.transferred]
   193 lemmas cinsertCI[intro!] = insertCI[Transfer.transferred]
   194 lemmas csubset_cinsert_iff = subset_insert_iff[Transfer.transferred]
   195 lemmas cinsert_ident = insert_ident[Transfer.transferred]
   196 lemmas csingletonI[intro!,no_atp] = singletonI[Transfer.transferred]
   197 lemmas csingletonD[dest!,no_atp] = singletonD[Transfer.transferred]
   198 lemmas fsingletonE = csingletonD [elim_format]
   199 lemmas csingleton_iff = singleton_iff[Transfer.transferred]
   200 lemmas csingleton_inject[dest!] = singleton_inject[Transfer.transferred]
   201 lemmas csingleton_finsert_inj_eq[iff,no_atp] = singleton_insert_inj_eq[Transfer.transferred]
   202 lemmas csingleton_finsert_inj_eq'[iff,no_atp] = singleton_insert_inj_eq'[Transfer.transferred]
   203 lemmas csubset_csingletonD = subset_singletonD[Transfer.transferred]
   204 lemmas cDiff_single_cinsert = diff_single_insert[Transfer.transferred]
   205 lemmas cdoubleton_eq_iff = doubleton_eq_iff[Transfer.transferred]
   206 lemmas cUn_csingleton_iff = Un_singleton_iff[Transfer.transferred]
   207 lemmas csingleton_cUn_iff = singleton_Un_iff[Transfer.transferred]
   208 lemmas cimage_eqI[simp, intro] = image_eqI[Transfer.transferred]
   209 lemmas cimageI = imageI[Transfer.transferred]
   210 lemmas rev_cimage_eqI = rev_image_eqI[Transfer.transferred]
   211 lemmas cimageE[elim!] = imageE[Transfer.transferred]
   212 lemmas Compr_cimage_eq = Compr_image_eq[Transfer.transferred]
   213 lemmas cimage_cUn = image_Un[Transfer.transferred]
   214 lemmas cimage_iff = image_iff[Transfer.transferred]
   215 lemmas cimage_csubset_iff[no_atp] = image_subset_iff[Transfer.transferred]
   216 lemmas cimage_csubsetI = image_subsetI[Transfer.transferred]
   217 lemmas cimage_ident[simp] = image_ident[Transfer.transferred]
   218 lemmas split_if_cin1 = split_if_mem1[Transfer.transferred]
   219 lemmas split_if_cin2 = split_if_mem2[Transfer.transferred]
   220 lemmas cpsubsetI[intro!,no_atp] = psubsetI[Transfer.transferred]
   221 lemmas cpsubsetE[elim!,no_atp] = psubsetE[Transfer.transferred]
   222 lemmas cpsubset_finsert_iff = psubset_insert_iff[Transfer.transferred]
   223 lemmas cpsubset_eq = psubset_eq[Transfer.transferred]
   224 lemmas cpsubset_imp_fsubset = psubset_imp_subset[Transfer.transferred]
   225 lemmas cpsubset_trans = psubset_trans[Transfer.transferred]
   226 lemmas cpsubsetD = psubsetD[Transfer.transferred]
   227 lemmas cpsubset_csubset_trans = psubset_subset_trans[Transfer.transferred]
   228 lemmas csubset_cpsubset_trans = subset_psubset_trans[Transfer.transferred]
   229 lemmas cpsubset_imp_ex_fmem = psubset_imp_ex_mem[Transfer.transferred]
   230 lemmas csubset_cinsertI = subset_insertI[Transfer.transferred]
   231 lemmas csubset_cinsertI2 = subset_insertI2[Transfer.transferred]
   232 lemmas csubset_cinsert = subset_insert[Transfer.transferred]
   233 lemmas cUn_upper1 = Un_upper1[Transfer.transferred]
   234 lemmas cUn_upper2 = Un_upper2[Transfer.transferred]
   235 lemmas cUn_least = Un_least[Transfer.transferred]
   236 lemmas cInt_lower1 = Int_lower1[Transfer.transferred]
   237 lemmas cInt_lower2 = Int_lower2[Transfer.transferred]
   238 lemmas cInt_greatest = Int_greatest[Transfer.transferred]
   239 lemmas cDiff_csubset = Diff_subset[Transfer.transferred]
   240 lemmas cDiff_csubset_conv = Diff_subset_conv[Transfer.transferred]
   241 lemmas csubset_cempty[simp] = subset_empty[Transfer.transferred]
   242 lemmas not_cpsubset_cempty[iff] = not_psubset_empty[Transfer.transferred]
   243 lemmas cinsert_is_cUn = insert_is_Un[Transfer.transferred]
   244 lemmas cinsert_not_cempty[simp] = insert_not_empty[Transfer.transferred]
   245 lemmas cempty_not_cinsert = empty_not_insert[Transfer.transferred]
   246 lemmas cinsert_absorb = insert_absorb[Transfer.transferred]
   247 lemmas cinsert_absorb2[simp] = insert_absorb2[Transfer.transferred]
   248 lemmas cinsert_commute = insert_commute[Transfer.transferred]
   249 lemmas cinsert_csubset[simp] = insert_subset[Transfer.transferred]
   250 lemmas cinsert_cinter_cinsert[simp] = insert_inter_insert[Transfer.transferred]
   251 lemmas cinsert_disjoint[simp,no_atp] = insert_disjoint[Transfer.transferred]
   252 lemmas disjoint_cinsert[simp,no_atp] = disjoint_insert[Transfer.transferred]
   253 lemmas cimage_cempty[simp] = image_empty[Transfer.transferred]
   254 lemmas cimage_cinsert[simp] = image_insert[Transfer.transferred]
   255 lemmas cimage_constant = image_constant[Transfer.transferred]
   256 lemmas cimage_constant_conv = image_constant_conv[Transfer.transferred]
   257 lemmas cimage_cimage = image_image[Transfer.transferred]
   258 lemmas cinsert_cimage[simp] = insert_image[Transfer.transferred]
   259 lemmas cimage_is_cempty[iff] = image_is_empty[Transfer.transferred]
   260 lemmas cempty_is_cimage[iff] = empty_is_image[Transfer.transferred]
   261 lemmas cimage_cong = image_cong[Transfer.transferred]
   262 lemmas cimage_cInt_csubset = image_Int_subset[Transfer.transferred]
   263 lemmas cimage_cDiff_csubset = image_diff_subset[Transfer.transferred]
   264 lemmas cInt_absorb = Int_absorb[Transfer.transferred]
   265 lemmas cInt_left_absorb = Int_left_absorb[Transfer.transferred]
   266 lemmas cInt_commute = Int_commute[Transfer.transferred]
   267 lemmas cInt_left_commute = Int_left_commute[Transfer.transferred]
   268 lemmas cInt_assoc = Int_assoc[Transfer.transferred]
   269 lemmas cInt_ac = Int_ac[Transfer.transferred]
   270 lemmas cInt_absorb1 = Int_absorb1[Transfer.transferred]
   271 lemmas cInt_absorb2 = Int_absorb2[Transfer.transferred]
   272 lemmas cInt_cempty_left = Int_empty_left[Transfer.transferred]
   273 lemmas cInt_cempty_right = Int_empty_right[Transfer.transferred]
   274 lemmas disjoint_iff_cnot_equal = disjoint_iff_not_equal[Transfer.transferred]
   275 lemmas cInt_cUn_distrib = Int_Un_distrib[Transfer.transferred]
   276 lemmas cInt_cUn_distrib2 = Int_Un_distrib2[Transfer.transferred]
   277 lemmas cInt_csubset_iff[no_atp, simp] = Int_subset_iff[Transfer.transferred]
   278 lemmas cUn_absorb = Un_absorb[Transfer.transferred]
   279 lemmas cUn_left_absorb = Un_left_absorb[Transfer.transferred]
   280 lemmas cUn_commute = Un_commute[Transfer.transferred]
   281 lemmas cUn_left_commute = Un_left_commute[Transfer.transferred]
   282 lemmas cUn_assoc = Un_assoc[Transfer.transferred]
   283 lemmas cUn_ac = Un_ac[Transfer.transferred]
   284 lemmas cUn_absorb1 = Un_absorb1[Transfer.transferred]
   285 lemmas cUn_absorb2 = Un_absorb2[Transfer.transferred]
   286 lemmas cUn_cempty_left = Un_empty_left[Transfer.transferred]
   287 lemmas cUn_cempty_right = Un_empty_right[Transfer.transferred]
   288 lemmas cUn_cinsert_left[simp] = Un_insert_left[Transfer.transferred]
   289 lemmas cUn_cinsert_right[simp] = Un_insert_right[Transfer.transferred]
   290 lemmas cInt_cinsert_left = Int_insert_left[Transfer.transferred]
   291 lemmas cInt_cinsert_left_if0[simp] = Int_insert_left_if0[Transfer.transferred]
   292 lemmas cInt_cinsert_left_if1[simp] = Int_insert_left_if1[Transfer.transferred]
   293 lemmas cInt_cinsert_right = Int_insert_right[Transfer.transferred]
   294 lemmas cInt_cinsert_right_if0[simp] = Int_insert_right_if0[Transfer.transferred]
   295 lemmas cInt_cinsert_right_if1[simp] = Int_insert_right_if1[Transfer.transferred]
   296 lemmas cUn_cInt_distrib = Un_Int_distrib[Transfer.transferred]
   297 lemmas cUn_cInt_distrib2 = Un_Int_distrib2[Transfer.transferred]
   298 lemmas cUn_cInt_crazy = Un_Int_crazy[Transfer.transferred]
   299 lemmas csubset_cUn_eq = subset_Un_eq[Transfer.transferred]
   300 lemmas cUn_cempty[iff] = Un_empty[Transfer.transferred]
   301 lemmas cUn_csubset_iff[no_atp, simp] = Un_subset_iff[Transfer.transferred]
   302 lemmas cUn_cDiff_cInt = Un_Diff_Int[Transfer.transferred]
   303 lemmas cDiff_cInt2 = Diff_Int2[Transfer.transferred]
   304 lemmas cUn_cInt_assoc_eq = Un_Int_assoc_eq[Transfer.transferred]
   305 lemmas cBall_cUn = ball_Un[Transfer.transferred]
   306 lemmas cBex_cUn = bex_Un[Transfer.transferred]
   307 lemmas cDiff_eq_cempty_iff[simp,no_atp] = Diff_eq_empty_iff[Transfer.transferred]
   308 lemmas cDiff_cancel[simp] = Diff_cancel[Transfer.transferred]
   309 lemmas cDiff_idemp[simp] = Diff_idemp[Transfer.transferred]
   310 lemmas cDiff_triv = Diff_triv[Transfer.transferred]
   311 lemmas cempty_cDiff[simp] = empty_Diff[Transfer.transferred]
   312 lemmas cDiff_cempty[simp] = Diff_empty[Transfer.transferred]
   313 lemmas cDiff_cinsert0[simp,no_atp] = Diff_insert0[Transfer.transferred]
   314 lemmas cDiff_cinsert = Diff_insert[Transfer.transferred]
   315 lemmas cDiff_cinsert2 = Diff_insert2[Transfer.transferred]
   316 lemmas cinsert_cDiff_if = insert_Diff_if[Transfer.transferred]
   317 lemmas cinsert_cDiff1[simp] = insert_Diff1[Transfer.transferred]
   318 lemmas cinsert_cDiff_single[simp] = insert_Diff_single[Transfer.transferred]
   319 lemmas cinsert_cDiff = insert_Diff[Transfer.transferred]
   320 lemmas cDiff_cinsert_absorb = Diff_insert_absorb[Transfer.transferred]
   321 lemmas cDiff_disjoint[simp] = Diff_disjoint[Transfer.transferred]
   322 lemmas cDiff_partition = Diff_partition[Transfer.transferred]
   323 lemmas double_cDiff = double_diff[Transfer.transferred]
   324 lemmas cUn_cDiff_cancel[simp] = Un_Diff_cancel[Transfer.transferred]
   325 lemmas cUn_cDiff_cancel2[simp] = Un_Diff_cancel2[Transfer.transferred]
   326 lemmas cDiff_cUn = Diff_Un[Transfer.transferred]
   327 lemmas cDiff_cInt = Diff_Int[Transfer.transferred]
   328 lemmas cUn_cDiff = Un_Diff[Transfer.transferred]
   329 lemmas cInt_cDiff = Int_Diff[Transfer.transferred]
   330 lemmas cDiff_cInt_distrib = Diff_Int_distrib[Transfer.transferred]
   331 lemmas cDiff_cInt_distrib2 = Diff_Int_distrib2[Transfer.transferred]
   332 lemmas cset_eq_csubset = set_eq_subset[Transfer.transferred]
   333 lemmas csubset_iff[no_atp] = subset_iff[Transfer.transferred]
   334 lemmas csubset_iff_pfsubset_eq = subset_iff_psubset_eq[Transfer.transferred]
   335 lemmas all_not_cin_conv[simp] = all_not_in_conv[Transfer.transferred]
   336 lemmas ex_cin_conv = ex_in_conv[Transfer.transferred]
   337 lemmas cimage_mono = image_mono[Transfer.transferred]
   338 lemmas cinsert_mono = insert_mono[Transfer.transferred]
   339 lemmas cunion_mono = Un_mono[Transfer.transferred]
   340 lemmas cinter_mono = Int_mono[Transfer.transferred]
   341 lemmas cminus_mono = Diff_mono[Transfer.transferred]
   342 lemmas cin_mono = in_mono[Transfer.transferred]
   343 lemmas cLeast_mono = Least_mono[Transfer.transferred]
   344 lemmas cequalityI = equalityI[Transfer.transferred]
   345 lemmas cUnion_cimage_eq [simp] = Union_image_eq[Transfer.transferred]
   346 lemmas cUN_iff [simp] = UN_iff[Transfer.transferred]
   347 lemmas cUN_I [intro] = UN_I[Transfer.transferred]
   348 lemmas cUN_E [elim!] = UN_E[Transfer.transferred]
   349 lemmas cimage_eq_cUN = image_eq_UN[Transfer.transferred]
   350 lemmas cUN_upper = UN_upper[Transfer.transferred]
   351 lemmas cUN_least = UN_least[Transfer.transferred]
   352 lemmas cUN_cinsert_distrib = UN_insert_distrib[Transfer.transferred]
   353 lemmas cUN_empty [simp] = UN_empty[Transfer.transferred]
   354 lemmas cUN_empty2 [simp] = UN_empty2[Transfer.transferred]
   355 lemmas cUN_absorb = UN_absorb[Transfer.transferred]
   356 lemmas cUN_cinsert [simp] = UN_insert[Transfer.transferred]
   357 lemmas cUN_cUn [simp] = UN_Un[Transfer.transferred]
   358 lemmas cUN_cUN_flatten = UN_UN_flatten[Transfer.transferred]
   359 lemmas cUN_csubset_iff = UN_subset_iff[Transfer.transferred]
   360 lemmas cUN_constant [simp] = UN_constant[Transfer.transferred]
   361 lemmas cimage_cUnion = image_Union[Transfer.transferred]
   362 lemmas cUNION_cempty_conv [simp] = UNION_empty_conv[Transfer.transferred]
   363 lemmas cBall_cUN = ball_UN[Transfer.transferred]
   364 lemmas cBex_cUN = bex_UN[Transfer.transferred]
   365 lemmas cUn_eq_cUN = Un_eq_UN[Transfer.transferred]
   366 lemmas cUN_mono = UN_mono[Transfer.transferred]
   367 lemmas cimage_cUN = image_UN[Transfer.transferred]
   368 lemmas cUN_csingleton [simp] = UN_singleton[Transfer.transferred]
   369 
   370 subsection \<open>Additional lemmas\<close>
   371 
   372 subsubsection \<open>\<open>cempty\<close>\<close>
   373 
   374 lemma cemptyE [elim!]: "cin a cempty \<Longrightarrow> P" by simp
   375 
   376 subsubsection \<open>\<open>cinsert\<close>\<close>
   377 
   378 lemma countable_insert_iff: "countable (insert x A) \<longleftrightarrow> countable A"
   379 by (metis Diff_eq_empty_iff countable_empty countable_insert subset_insertI uncountable_minus_countable)
   380 
   381 lemma set_cinsert:
   382   assumes "cin x A"
   383   obtains B where "A = cinsert x B" and "\<not> cin x B"
   384 using assms by transfer(erule Set.set_insert, simp add: countable_insert_iff)
   385 
   386 lemma mk_disjoint_cinsert: "cin a A \<Longrightarrow> \<exists>B. A = cinsert a B \<and> \<not> cin a B"
   387   by (rule exI[where x = "cDiff A (csingle a)"]) blast
   388 
   389 subsubsection \<open>\<open>cimage\<close>\<close>
   390 
   391 lemma subset_cimage_iff: "csubset_eq B (cimage f A) \<longleftrightarrow> (\<exists>AA. csubset_eq AA A \<and> B = cimage f AA)"
   392 by transfer (metis countable_subset image_mono mem_Collect_eq subset_imageE) 
   393 
   394 subsubsection \<open>bounded quantification\<close>
   395 
   396 lemma cBex_simps [simp, no_atp]:
   397   "\<And>A P Q. cBex A (\<lambda>x. P x \<and> Q) = (cBex A P \<and> Q)" 
   398   "\<And>A P Q. cBex A (\<lambda>x. P \<and> Q x) = (P \<and> cBex A Q)"
   399   "\<And>P. cBex cempty P = False" 
   400   "\<And>a B P. cBex (cinsert a B) P = (P a \<or> cBex B P)"
   401   "\<And>A P f. cBex (cimage f A) P = cBex A (\<lambda>x. P (f x))"
   402   "\<And>A P. (\<not> cBex A P) = cBall A (\<lambda>x. \<not> P x)"
   403 by auto
   404 
   405 lemma cBall_simps [simp, no_atp]:
   406   "\<And>A P Q. cBall A (\<lambda>x. P x \<or> Q) = (cBall A P \<or> Q)"
   407   "\<And>A P Q. cBall A (\<lambda>x. P \<or> Q x) = (P \<or> cBall A Q)"
   408   "\<And>A P Q. cBall A (\<lambda>x. P \<longrightarrow> Q x) = (P \<longrightarrow> cBall A Q)"
   409   "\<And>A P Q. cBall A (\<lambda>x. P x \<longrightarrow> Q) = (cBex A P \<longrightarrow> Q)"
   410   "\<And>P. cBall cempty P = True"
   411   "\<And>a B P. cBall (cinsert a B) P = (P a \<and> cBall B P)"
   412   "\<And>A P f. cBall (cimage f A) P = cBall A (\<lambda>x. P (f x))"
   413   "\<And>A P. (\<not> cBall A P) = cBex A (\<lambda>x. \<not> P x)"
   414 by auto
   415 
   416 lemma atomize_cBall:
   417     "(\<And>x. cin x A ==> P x) == Trueprop (cBall A (\<lambda>x. P x))"
   418 apply (simp only: atomize_all atomize_imp)
   419 apply (rule equal_intr_rule)
   420 by (transfer, simp)+
   421 
   422 subsubsection \<open>@{const cUnion}\<close>
   423 
   424 lemma cUNION_cimage: "cUNION (cimage f A) g = cUNION A (g \<circ> f)"
   425 including cset.lifting by transfer auto
   426 
   427 
   428 subsection \<open>Setup for Lifting/Transfer\<close>
   429 
   430 subsubsection \<open>Relator and predicator properties\<close>
   431 
   432 lift_definition rel_cset :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a cset \<Rightarrow> 'b cset \<Rightarrow> bool"
   433   is rel_set parametric rel_set_transfer .
   434 
   435 lemma rel_cset_alt_def:
   436   "rel_cset R a b \<longleftrightarrow>
   437    (\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and>
   438    (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t)"
   439 by(simp add: rel_cset_def rel_set_def)
   440 
   441 lemma rel_cset_iff:
   442   "rel_cset R a b \<longleftrightarrow>
   443    (\<forall>t. cin t a \<longrightarrow> (\<exists>u. cin u b \<and> R t u)) \<and>
   444    (\<forall>t. cin t b \<longrightarrow> (\<exists>u. cin u a \<and> R u t))"
   445 by transfer(auto simp add: rel_set_def)
   446 
   447 lemma rel_cset_cUNION:
   448   "\<lbrakk> rel_cset Q A B; rel_fun Q (rel_cset R) f g \<rbrakk>
   449   \<Longrightarrow> rel_cset R (cUNION A f) (cUNION B g)"
   450 unfolding rel_fun_def by transfer(erule rel_set_UNION, simp add: rel_fun_def)
   451 
   452 lemma rel_cset_csingle_iff [simp]: "rel_cset R (csingle x) (csingle y) \<longleftrightarrow> R x y"
   453 by transfer(auto simp add: rel_set_def)
   454 
   455 subsubsection \<open>Transfer rules for the Transfer package\<close>
   456 
   457 text \<open>Unconditional transfer rules\<close>
   458 
   459 context begin interpretation lifting_syntax .
   460 
   461 lemmas cempty_parametric [transfer_rule] = empty_transfer[Transfer.transferred]
   462 
   463 lemma cinsert_parametric [transfer_rule]:
   464   "(A ===> rel_cset A ===> rel_cset A) cinsert cinsert"
   465   unfolding rel_fun_def rel_cset_iff by blast
   466 
   467 lemma cUn_parametric [transfer_rule]:
   468   "(rel_cset A ===> rel_cset A ===> rel_cset A) cUn cUn"
   469   unfolding rel_fun_def rel_cset_iff by blast
   470 
   471 lemma cUnion_parametric [transfer_rule]:
   472   "(rel_cset (rel_cset A) ===> rel_cset A) cUnion cUnion"
   473   unfolding rel_fun_def by transfer(simp, fast dest: rel_setD1 rel_setD2 intro!: rel_setI)
   474 
   475 lemma cimage_parametric [transfer_rule]:
   476   "((A ===> B) ===> rel_cset A ===> rel_cset B) cimage cimage"
   477   unfolding rel_fun_def rel_cset_iff by blast
   478 
   479 lemma cBall_parametric [transfer_rule]:
   480   "(rel_cset A ===> (A ===> op =) ===> op =) cBall cBall"
   481   unfolding rel_cset_iff rel_fun_def by blast
   482 
   483 lemma cBex_parametric [transfer_rule]:
   484   "(rel_cset A ===> (A ===> op =) ===> op =) cBex cBex"
   485   unfolding rel_cset_iff rel_fun_def by blast
   486 
   487 lemma rel_cset_parametric [transfer_rule]:
   488   "((A ===> B ===> op =) ===> rel_cset A ===> rel_cset B ===> op =) rel_cset rel_cset"
   489   unfolding rel_fun_def
   490   using rel_set_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred, where A = A and B = B]
   491   by simp
   492 
   493 text \<open>Rules requiring bi-unique, bi-total or right-total relations\<close>
   494 
   495 lemma cin_parametric [transfer_rule]:
   496   "bi_unique A \<Longrightarrow> (A ===> rel_cset A ===> op =) cin cin"
   497 unfolding rel_fun_def rel_cset_iff bi_unique_def by metis
   498 
   499 lemma cInt_parametric [transfer_rule]:
   500   "bi_unique A \<Longrightarrow> (rel_cset A ===> rel_cset A ===> rel_cset A) cInt cInt"
   501 unfolding rel_fun_def 
   502 using inter_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred]
   503 by blast
   504 
   505 lemma cDiff_parametric [transfer_rule]:
   506   "bi_unique A \<Longrightarrow> (rel_cset A ===> rel_cset A ===> rel_cset A) cDiff cDiff"
   507 unfolding rel_fun_def
   508 using Diff_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
   509 
   510 lemma csubset_parametric [transfer_rule]:
   511   "bi_unique A \<Longrightarrow> (rel_cset A ===> rel_cset A ===> op =) csubset_eq csubset_eq"
   512 unfolding rel_fun_def
   513 using subset_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
   514 
   515 end
   516 
   517 lifting_update cset.lifting
   518 lifting_forget cset.lifting
   519 
   520 subsection \<open>Registration as BNF\<close>
   521 
   522 lemma card_of_countable_sets_range:
   523 fixes A :: "'a set"
   524 shows "|{X. X \<subseteq> A \<and> countable X \<and> X \<noteq> {}}| \<le>o |{f::nat \<Rightarrow> 'a. range f \<subseteq> A}|"
   525 apply(rule card_of_ordLeqI[of from_nat_into]) using inj_on_from_nat_into
   526 unfolding inj_on_def by auto
   527 
   528 lemma card_of_countable_sets_Func:
   529 "|{X. X \<subseteq> A \<and> countable X \<and> X \<noteq> {}}| \<le>o |A| ^c natLeq"
   530 using card_of_countable_sets_range card_of_Func_UNIV[THEN ordIso_symmetric]
   531 unfolding cexp_def Field_natLeq Field_card_of
   532 by (rule ordLeq_ordIso_trans)
   533 
   534 lemma ordLeq_countable_subsets:
   535 "|A| \<le>o |{X. X \<subseteq> A \<and> countable X}|"
   536 apply (rule card_of_ordLeqI[of "\<lambda> a. {a}"]) unfolding inj_on_def by auto
   537 
   538 lemma finite_countable_subset:
   539 "finite {X. X \<subseteq> A \<and> countable X} \<longleftrightarrow> finite A"
   540 apply (rule iffI)
   541  apply (erule contrapos_pp)
   542  apply (rule card_of_ordLeq_infinite)
   543  apply (rule ordLeq_countable_subsets)
   544  apply assumption
   545 apply (rule finite_Collect_conjI)
   546 apply (rule disjI1)
   547 apply (erule finite_Collect_subsets)
   548 done
   549 
   550 lemma rcset_to_rcset: "countable A \<Longrightarrow> rcset (the_inv rcset A) = A"
   551   including cset.lifting
   552   apply (rule f_the_inv_into_f[unfolded inj_on_def image_iff])
   553    apply transfer' apply simp
   554   apply transfer' apply simp
   555   done
   556 
   557 lemma Collect_Int_Times: "{(x, y). R x y} \<inter> A \<times> B = {(x, y). R x y \<and> x \<in> A \<and> y \<in> B}"
   558 by auto
   559 
   560 
   561 lemma rel_cset_aux:
   562 "(\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and> (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t) \<longleftrightarrow>
   563  ((BNF_Def.Grp {x. rcset x \<subseteq> {(a, b). R a b}} (cimage fst))\<inverse>\<inverse> OO
   564    BNF_Def.Grp {x. rcset x \<subseteq> {(a, b). R a b}} (cimage snd)) a b" (is "?L = ?R")
   565 proof
   566   assume ?L
   567   def R' \<equiv> "the_inv rcset (Collect (case_prod R) \<inter> (rcset a \<times> rcset b))"
   568   (is "the_inv rcset ?L'")
   569   have L: "countable ?L'" by auto
   570   hence *: "rcset R' = ?L'" unfolding R'_def by (intro rcset_to_rcset)
   571   thus ?R unfolding Grp_def relcompp.simps conversep.simps including cset.lifting
   572   proof (intro CollectI case_prodI exI[of _ a] exI[of _ b] exI[of _ R'] conjI refl)
   573     from * \<open>?L\<close> show "a = cimage fst R'" by transfer (auto simp: image_def Collect_Int_Times)
   574     from * \<open>?L\<close> show "b = cimage snd R'" by transfer (auto simp: image_def Collect_Int_Times)
   575   qed simp_all
   576 next
   577   assume ?R thus ?L unfolding Grp_def relcompp.simps conversep.simps
   578     by (simp add: subset_eq Ball_def)(transfer, auto simp add: cimage.rep_eq, metis snd_conv, metis fst_conv)
   579 qed
   580 
   581 bnf "'a cset"
   582   map: cimage
   583   sets: rcset
   584   bd: natLeq
   585   wits: "cempty"
   586   rel: rel_cset
   587 proof -
   588   show "cimage id = id" by auto
   589 next
   590   fix f g show "cimage (g \<circ> f) = cimage g \<circ> cimage f" by fastforce
   591 next
   592   fix C f g assume eq: "\<And>a. a \<in> rcset C \<Longrightarrow> f a = g a"
   593   thus "cimage f C = cimage g C" including cset.lifting by transfer force
   594 next
   595   fix f show "rcset \<circ> cimage f = op ` f \<circ> rcset" including cset.lifting by transfer' fastforce
   596 next
   597   show "card_order natLeq" by (rule natLeq_card_order)
   598 next
   599   show "cinfinite natLeq" by (rule natLeq_cinfinite)
   600 next
   601   fix C show "|rcset C| \<le>o natLeq"
   602     including cset.lifting by transfer (unfold countable_card_le_natLeq)
   603 next
   604   fix R S
   605   show "rel_cset R OO rel_cset S \<le> rel_cset (R OO S)"
   606     unfolding rel_cset_alt_def[abs_def] by fast
   607 next
   608   fix R
   609   show "rel_cset R =
   610         (BNF_Def.Grp {x. rcset x \<subseteq> Collect (case_prod R)} (cimage fst))\<inverse>\<inverse> OO
   611          BNF_Def.Grp {x. rcset x \<subseteq> Collect (case_prod R)} (cimage snd)"
   612   unfolding rel_cset_alt_def[abs_def] rel_cset_aux by simp
   613 qed(simp add: bot_cset.rep_eq)
   614 
   615 end