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