src/HOL/Library/FSet.thy
author wenzelm
Wed Mar 08 10:50:59 2017 +0100 (2017-03-08)
changeset 65151 a7394aa4d21c
parent 64267 b9a1486e79be
child 66261 fb6efe575c6d
permissions -rw-r--r--
tuned proofs;
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
hoelzl@63331
    10
imports Main
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"
kuncar@53953
    42
  shows "((pcr_fset A) ===> (pcr_fset A) ===> op =) op \<subset> op <"
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
blanchet@55938
   106
lemma transfer_bdd_below[transfer_rule]: "(rel_set (pcr_fset op =) ===> op =) 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
blanchet@55129
   207
wenzelm@60500
   208
subsection \<open>Transferred lemmas from Set.thy\<close>
kuncar@53953
   209
kuncar@53953
   210
lemmas fset_eqI = set_eqI[Transfer.transferred]
kuncar@53953
   211
lemmas fset_eq_iff[no_atp] = set_eq_iff[Transfer.transferred]
kuncar@53953
   212
lemmas fBallI[intro!] = ballI[Transfer.transferred]
kuncar@53953
   213
lemmas fbspec[dest?] = bspec[Transfer.transferred]
kuncar@53953
   214
lemmas fBallE[elim] = ballE[Transfer.transferred]
kuncar@53953
   215
lemmas fBexI[intro] = bexI[Transfer.transferred]
kuncar@53953
   216
lemmas rev_fBexI[intro?] = rev_bexI[Transfer.transferred]
kuncar@53953
   217
lemmas fBexCI = bexCI[Transfer.transferred]
kuncar@53953
   218
lemmas fBexE[elim!] = bexE[Transfer.transferred]
kuncar@53953
   219
lemmas fBall_triv[simp] = ball_triv[Transfer.transferred]
kuncar@53953
   220
lemmas fBex_triv[simp] = bex_triv[Transfer.transferred]
kuncar@53953
   221
lemmas fBex_triv_one_point1[simp] = bex_triv_one_point1[Transfer.transferred]
kuncar@53953
   222
lemmas fBex_triv_one_point2[simp] = bex_triv_one_point2[Transfer.transferred]
kuncar@53953
   223
lemmas fBex_one_point1[simp] = bex_one_point1[Transfer.transferred]
kuncar@53953
   224
lemmas fBex_one_point2[simp] = bex_one_point2[Transfer.transferred]
kuncar@53953
   225
lemmas fBall_one_point1[simp] = ball_one_point1[Transfer.transferred]
kuncar@53953
   226
lemmas fBall_one_point2[simp] = ball_one_point2[Transfer.transferred]
kuncar@53953
   227
lemmas fBall_conj_distrib = ball_conj_distrib[Transfer.transferred]
kuncar@53953
   228
lemmas fBex_disj_distrib = bex_disj_distrib[Transfer.transferred]
kuncar@53953
   229
lemmas fBall_cong = ball_cong[Transfer.transferred]
kuncar@53953
   230
lemmas fBex_cong = bex_cong[Transfer.transferred]
kuncar@53964
   231
lemmas fsubsetI[intro!] = subsetI[Transfer.transferred]
kuncar@53964
   232
lemmas fsubsetD[elim, intro?] = subsetD[Transfer.transferred]
kuncar@53964
   233
lemmas rev_fsubsetD[no_atp,intro?] = rev_subsetD[Transfer.transferred]
kuncar@53964
   234
lemmas fsubsetCE[no_atp,elim] = subsetCE[Transfer.transferred]
kuncar@53964
   235
lemmas fsubset_eq[no_atp] = subset_eq[Transfer.transferred]
kuncar@53964
   236
lemmas contra_fsubsetD[no_atp] = contra_subsetD[Transfer.transferred]
kuncar@53964
   237
lemmas fsubset_refl = subset_refl[Transfer.transferred]
kuncar@53964
   238
lemmas fsubset_trans = subset_trans[Transfer.transferred]
kuncar@53953
   239
lemmas fset_rev_mp = set_rev_mp[Transfer.transferred]
kuncar@53953
   240
lemmas fset_mp = set_mp[Transfer.transferred]
kuncar@53964
   241
lemmas fsubset_not_fsubset_eq[code] = subset_not_subset_eq[Transfer.transferred]
kuncar@53953
   242
lemmas eq_fmem_trans = eq_mem_trans[Transfer.transferred]
kuncar@53964
   243
lemmas fsubset_antisym[intro!] = subset_antisym[Transfer.transferred]
kuncar@53953
   244
lemmas fequalityD1 = equalityD1[Transfer.transferred]
kuncar@53953
   245
lemmas fequalityD2 = equalityD2[Transfer.transferred]
kuncar@53953
   246
lemmas fequalityE = equalityE[Transfer.transferred]
kuncar@53953
   247
lemmas fequalityCE[elim] = equalityCE[Transfer.transferred]
kuncar@53953
   248
lemmas eqfset_imp_iff = eqset_imp_iff[Transfer.transferred]
kuncar@53953
   249
lemmas eqfelem_imp_iff = eqelem_imp_iff[Transfer.transferred]
kuncar@53953
   250
lemmas fempty_iff[simp] = empty_iff[Transfer.transferred]
kuncar@53964
   251
lemmas fempty_fsubsetI[iff] = empty_subsetI[Transfer.transferred]
kuncar@53953
   252
lemmas equalsffemptyI = equals0I[Transfer.transferred]
kuncar@53953
   253
lemmas equalsffemptyD = equals0D[Transfer.transferred]
kuncar@53953
   254
lemmas fBall_fempty[simp] = ball_empty[Transfer.transferred]
kuncar@53953
   255
lemmas fBex_fempty[simp] = bex_empty[Transfer.transferred]
kuncar@53953
   256
lemmas fPow_iff[iff] = Pow_iff[Transfer.transferred]
kuncar@53953
   257
lemmas fPowI = PowI[Transfer.transferred]
kuncar@53953
   258
lemmas fPowD = PowD[Transfer.transferred]
kuncar@53953
   259
lemmas fPow_bottom = Pow_bottom[Transfer.transferred]
kuncar@53953
   260
lemmas fPow_top = Pow_top[Transfer.transferred]
kuncar@53953
   261
lemmas fPow_not_fempty = Pow_not_empty[Transfer.transferred]
kuncar@53953
   262
lemmas finter_iff[simp] = Int_iff[Transfer.transferred]
kuncar@53953
   263
lemmas finterI[intro!] = IntI[Transfer.transferred]
kuncar@53953
   264
lemmas finterD1 = IntD1[Transfer.transferred]
kuncar@53953
   265
lemmas finterD2 = IntD2[Transfer.transferred]
kuncar@53953
   266
lemmas finterE[elim!] = IntE[Transfer.transferred]
kuncar@53953
   267
lemmas funion_iff[simp] = Un_iff[Transfer.transferred]
kuncar@53953
   268
lemmas funionI1[elim?] = UnI1[Transfer.transferred]
kuncar@53953
   269
lemmas funionI2[elim?] = UnI2[Transfer.transferred]
kuncar@53953
   270
lemmas funionCI[intro!] = UnCI[Transfer.transferred]
kuncar@53953
   271
lemmas funionE[elim!] = UnE[Transfer.transferred]
kuncar@53953
   272
lemmas fminus_iff[simp] = Diff_iff[Transfer.transferred]
kuncar@53953
   273
lemmas fminusI[intro!] = DiffI[Transfer.transferred]
kuncar@53953
   274
lemmas fminusD1 = DiffD1[Transfer.transferred]
kuncar@53953
   275
lemmas fminusD2 = DiffD2[Transfer.transferred]
kuncar@53953
   276
lemmas fminusE[elim!] = DiffE[Transfer.transferred]
kuncar@53953
   277
lemmas finsert_iff[simp] = insert_iff[Transfer.transferred]
kuncar@53953
   278
lemmas finsertI1 = insertI1[Transfer.transferred]
kuncar@53953
   279
lemmas finsertI2 = insertI2[Transfer.transferred]
kuncar@53953
   280
lemmas finsertE[elim!] = insertE[Transfer.transferred]
kuncar@53953
   281
lemmas finsertCI[intro!] = insertCI[Transfer.transferred]
kuncar@53964
   282
lemmas fsubset_finsert_iff = subset_insert_iff[Transfer.transferred]
kuncar@53953
   283
lemmas finsert_ident = insert_ident[Transfer.transferred]
kuncar@53953
   284
lemmas fsingletonI[intro!,no_atp] = singletonI[Transfer.transferred]
kuncar@53953
   285
lemmas fsingletonD[dest!,no_atp] = singletonD[Transfer.transferred]
kuncar@53953
   286
lemmas fsingleton_iff = singleton_iff[Transfer.transferred]
kuncar@53953
   287
lemmas fsingleton_inject[dest!] = singleton_inject[Transfer.transferred]
kuncar@53953
   288
lemmas fsingleton_finsert_inj_eq[iff,no_atp] = singleton_insert_inj_eq[Transfer.transferred]
kuncar@53953
   289
lemmas fsingleton_finsert_inj_eq'[iff,no_atp] = singleton_insert_inj_eq'[Transfer.transferred]
kuncar@53964
   290
lemmas fsubset_fsingletonD = subset_singletonD[Transfer.transferred]
paulson@62087
   291
lemmas fminus_single_finsert = Diff_single_insert[Transfer.transferred]
kuncar@53953
   292
lemmas fdoubleton_eq_iff = doubleton_eq_iff[Transfer.transferred]
kuncar@53953
   293
lemmas funion_fsingleton_iff = Un_singleton_iff[Transfer.transferred]
kuncar@53953
   294
lemmas fsingleton_funion_iff = singleton_Un_iff[Transfer.transferred]
kuncar@53953
   295
lemmas fimage_eqI[simp, intro] = image_eqI[Transfer.transferred]
kuncar@53953
   296
lemmas fimageI = imageI[Transfer.transferred]
kuncar@53953
   297
lemmas rev_fimage_eqI = rev_image_eqI[Transfer.transferred]
kuncar@53953
   298
lemmas fimageE[elim!] = imageE[Transfer.transferred]
kuncar@53953
   299
lemmas Compr_fimage_eq = Compr_image_eq[Transfer.transferred]
kuncar@53953
   300
lemmas fimage_funion = image_Un[Transfer.transferred]
kuncar@53953
   301
lemmas fimage_iff = image_iff[Transfer.transferred]
kuncar@53964
   302
lemmas fimage_fsubset_iff[no_atp] = image_subset_iff[Transfer.transferred]
kuncar@53964
   303
lemmas fimage_fsubsetI = image_subsetI[Transfer.transferred]
kuncar@53953
   304
lemmas fimage_ident[simp] = image_ident[Transfer.transferred]
nipkow@62390
   305
lemmas if_split_fmem1 = if_split_mem1[Transfer.transferred]
nipkow@62390
   306
lemmas if_split_fmem2 = if_split_mem2[Transfer.transferred]
kuncar@53964
   307
lemmas pfsubsetI[intro!,no_atp] = psubsetI[Transfer.transferred]
kuncar@53964
   308
lemmas pfsubsetE[elim!,no_atp] = psubsetE[Transfer.transferred]
kuncar@53964
   309
lemmas pfsubset_finsert_iff = psubset_insert_iff[Transfer.transferred]
kuncar@53964
   310
lemmas pfsubset_eq = psubset_eq[Transfer.transferred]
kuncar@53964
   311
lemmas pfsubset_imp_fsubset = psubset_imp_subset[Transfer.transferred]
kuncar@53964
   312
lemmas pfsubset_trans = psubset_trans[Transfer.transferred]
kuncar@53964
   313
lemmas pfsubsetD = psubsetD[Transfer.transferred]
kuncar@53964
   314
lemmas pfsubset_fsubset_trans = psubset_subset_trans[Transfer.transferred]
kuncar@53964
   315
lemmas fsubset_pfsubset_trans = subset_psubset_trans[Transfer.transferred]
kuncar@53964
   316
lemmas pfsubset_imp_ex_fmem = psubset_imp_ex_mem[Transfer.transferred]
kuncar@53953
   317
lemmas fimage_fPow_mono = image_Pow_mono[Transfer.transferred]
kuncar@53953
   318
lemmas fimage_fPow_surj = image_Pow_surj[Transfer.transferred]
kuncar@53964
   319
lemmas fsubset_finsertI = subset_insertI[Transfer.transferred]
kuncar@53964
   320
lemmas fsubset_finsertI2 = subset_insertI2[Transfer.transferred]
kuncar@53964
   321
lemmas fsubset_finsert = subset_insert[Transfer.transferred]
kuncar@53953
   322
lemmas funion_upper1 = Un_upper1[Transfer.transferred]
kuncar@53953
   323
lemmas funion_upper2 = Un_upper2[Transfer.transferred]
kuncar@53953
   324
lemmas funion_least = Un_least[Transfer.transferred]
kuncar@53953
   325
lemmas finter_lower1 = Int_lower1[Transfer.transferred]
kuncar@53953
   326
lemmas finter_lower2 = Int_lower2[Transfer.transferred]
kuncar@53953
   327
lemmas finter_greatest = Int_greatest[Transfer.transferred]
kuncar@53964
   328
lemmas fminus_fsubset = Diff_subset[Transfer.transferred]
kuncar@53964
   329
lemmas fminus_fsubset_conv = Diff_subset_conv[Transfer.transferred]
kuncar@53964
   330
lemmas fsubset_fempty[simp] = subset_empty[Transfer.transferred]
kuncar@53964
   331
lemmas not_pfsubset_fempty[iff] = not_psubset_empty[Transfer.transferred]
kuncar@53953
   332
lemmas finsert_is_funion = insert_is_Un[Transfer.transferred]
kuncar@53953
   333
lemmas finsert_not_fempty[simp] = insert_not_empty[Transfer.transferred]
kuncar@53953
   334
lemmas fempty_not_finsert = empty_not_insert[Transfer.transferred]
kuncar@53953
   335
lemmas finsert_absorb = insert_absorb[Transfer.transferred]
kuncar@53953
   336
lemmas finsert_absorb2[simp] = insert_absorb2[Transfer.transferred]
kuncar@53953
   337
lemmas finsert_commute = insert_commute[Transfer.transferred]
kuncar@53964
   338
lemmas finsert_fsubset[simp] = insert_subset[Transfer.transferred]
kuncar@53953
   339
lemmas finsert_inter_finsert[simp] = insert_inter_insert[Transfer.transferred]
kuncar@53953
   340
lemmas finsert_disjoint[simp,no_atp] = insert_disjoint[Transfer.transferred]
kuncar@53953
   341
lemmas disjoint_finsert[simp,no_atp] = disjoint_insert[Transfer.transferred]
kuncar@53953
   342
lemmas fimage_fempty[simp] = image_empty[Transfer.transferred]
kuncar@53953
   343
lemmas fimage_finsert[simp] = image_insert[Transfer.transferred]
kuncar@53953
   344
lemmas fimage_constant = image_constant[Transfer.transferred]
kuncar@53953
   345
lemmas fimage_constant_conv = image_constant_conv[Transfer.transferred]
kuncar@53953
   346
lemmas fimage_fimage = image_image[Transfer.transferred]
kuncar@53953
   347
lemmas finsert_fimage[simp] = insert_image[Transfer.transferred]
kuncar@53953
   348
lemmas fimage_is_fempty[iff] = image_is_empty[Transfer.transferred]
kuncar@53953
   349
lemmas fempty_is_fimage[iff] = empty_is_image[Transfer.transferred]
kuncar@53953
   350
lemmas fimage_cong = image_cong[Transfer.transferred]
kuncar@53964
   351
lemmas fimage_finter_fsubset = image_Int_subset[Transfer.transferred]
kuncar@53964
   352
lemmas fimage_fminus_fsubset = image_diff_subset[Transfer.transferred]
kuncar@53953
   353
lemmas finter_absorb = Int_absorb[Transfer.transferred]
kuncar@53953
   354
lemmas finter_left_absorb = Int_left_absorb[Transfer.transferred]
kuncar@53953
   355
lemmas finter_commute = Int_commute[Transfer.transferred]
kuncar@53953
   356
lemmas finter_left_commute = Int_left_commute[Transfer.transferred]
kuncar@53953
   357
lemmas finter_assoc = Int_assoc[Transfer.transferred]
kuncar@53953
   358
lemmas finter_ac = Int_ac[Transfer.transferred]
kuncar@53953
   359
lemmas finter_absorb1 = Int_absorb1[Transfer.transferred]
kuncar@53953
   360
lemmas finter_absorb2 = Int_absorb2[Transfer.transferred]
kuncar@53953
   361
lemmas finter_fempty_left = Int_empty_left[Transfer.transferred]
kuncar@53953
   362
lemmas finter_fempty_right = Int_empty_right[Transfer.transferred]
kuncar@53953
   363
lemmas disjoint_iff_fnot_equal = disjoint_iff_not_equal[Transfer.transferred]
kuncar@53953
   364
lemmas finter_funion_distrib = Int_Un_distrib[Transfer.transferred]
kuncar@53953
   365
lemmas finter_funion_distrib2 = Int_Un_distrib2[Transfer.transferred]
kuncar@53964
   366
lemmas finter_fsubset_iff[no_atp, simp] = Int_subset_iff[Transfer.transferred]
kuncar@53953
   367
lemmas funion_absorb = Un_absorb[Transfer.transferred]
kuncar@53953
   368
lemmas funion_left_absorb = Un_left_absorb[Transfer.transferred]
kuncar@53953
   369
lemmas funion_commute = Un_commute[Transfer.transferred]
kuncar@53953
   370
lemmas funion_left_commute = Un_left_commute[Transfer.transferred]
kuncar@53953
   371
lemmas funion_assoc = Un_assoc[Transfer.transferred]
kuncar@53953
   372
lemmas funion_ac = Un_ac[Transfer.transferred]
kuncar@53953
   373
lemmas funion_absorb1 = Un_absorb1[Transfer.transferred]
kuncar@53953
   374
lemmas funion_absorb2 = Un_absorb2[Transfer.transferred]
kuncar@53953
   375
lemmas funion_fempty_left = Un_empty_left[Transfer.transferred]
kuncar@53953
   376
lemmas funion_fempty_right = Un_empty_right[Transfer.transferred]
kuncar@53953
   377
lemmas funion_finsert_left[simp] = Un_insert_left[Transfer.transferred]
kuncar@53953
   378
lemmas funion_finsert_right[simp] = Un_insert_right[Transfer.transferred]
kuncar@53953
   379
lemmas finter_finsert_left = Int_insert_left[Transfer.transferred]
kuncar@53953
   380
lemmas finter_finsert_left_ifffempty[simp] = Int_insert_left_if0[Transfer.transferred]
kuncar@53953
   381
lemmas finter_finsert_left_if1[simp] = Int_insert_left_if1[Transfer.transferred]
kuncar@53953
   382
lemmas finter_finsert_right = Int_insert_right[Transfer.transferred]
kuncar@53953
   383
lemmas finter_finsert_right_ifffempty[simp] = Int_insert_right_if0[Transfer.transferred]
kuncar@53953
   384
lemmas finter_finsert_right_if1[simp] = Int_insert_right_if1[Transfer.transferred]
kuncar@53953
   385
lemmas funion_finter_distrib = Un_Int_distrib[Transfer.transferred]
kuncar@53953
   386
lemmas funion_finter_distrib2 = Un_Int_distrib2[Transfer.transferred]
kuncar@53953
   387
lemmas funion_finter_crazy = Un_Int_crazy[Transfer.transferred]
kuncar@53964
   388
lemmas fsubset_funion_eq = subset_Un_eq[Transfer.transferred]
kuncar@53953
   389
lemmas funion_fempty[iff] = Un_empty[Transfer.transferred]
kuncar@53964
   390
lemmas funion_fsubset_iff[no_atp, simp] = Un_subset_iff[Transfer.transferred]
kuncar@53953
   391
lemmas funion_fminus_finter = Un_Diff_Int[Transfer.transferred]
kuncar@53953
   392
lemmas fminus_finter2 = Diff_Int2[Transfer.transferred]
kuncar@53953
   393
lemmas funion_finter_assoc_eq = Un_Int_assoc_eq[Transfer.transferred]
kuncar@53953
   394
lemmas fBall_funion = ball_Un[Transfer.transferred]
kuncar@53953
   395
lemmas fBex_funion = bex_Un[Transfer.transferred]
kuncar@53953
   396
lemmas fminus_eq_fempty_iff[simp,no_atp] = Diff_eq_empty_iff[Transfer.transferred]
kuncar@53953
   397
lemmas fminus_cancel[simp] = Diff_cancel[Transfer.transferred]
kuncar@53953
   398
lemmas fminus_idemp[simp] = Diff_idemp[Transfer.transferred]
kuncar@53953
   399
lemmas fminus_triv = Diff_triv[Transfer.transferred]
kuncar@53953
   400
lemmas fempty_fminus[simp] = empty_Diff[Transfer.transferred]
kuncar@53953
   401
lemmas fminus_fempty[simp] = Diff_empty[Transfer.transferred]
kuncar@53953
   402
lemmas fminus_finsertffempty[simp,no_atp] = Diff_insert0[Transfer.transferred]
kuncar@53953
   403
lemmas fminus_finsert = Diff_insert[Transfer.transferred]
kuncar@53953
   404
lemmas fminus_finsert2 = Diff_insert2[Transfer.transferred]
kuncar@53953
   405
lemmas finsert_fminus_if = insert_Diff_if[Transfer.transferred]
kuncar@53953
   406
lemmas finsert_fminus1[simp] = insert_Diff1[Transfer.transferred]
kuncar@53953
   407
lemmas finsert_fminus_single[simp] = insert_Diff_single[Transfer.transferred]
kuncar@53953
   408
lemmas finsert_fminus = insert_Diff[Transfer.transferred]
kuncar@53953
   409
lemmas fminus_finsert_absorb = Diff_insert_absorb[Transfer.transferred]
kuncar@53953
   410
lemmas fminus_disjoint[simp] = Diff_disjoint[Transfer.transferred]
kuncar@53953
   411
lemmas fminus_partition = Diff_partition[Transfer.transferred]
kuncar@53953
   412
lemmas double_fminus = double_diff[Transfer.transferred]
kuncar@53953
   413
lemmas funion_fminus_cancel[simp] = Un_Diff_cancel[Transfer.transferred]
kuncar@53953
   414
lemmas funion_fminus_cancel2[simp] = Un_Diff_cancel2[Transfer.transferred]
kuncar@53953
   415
lemmas fminus_funion = Diff_Un[Transfer.transferred]
kuncar@53953
   416
lemmas fminus_finter = Diff_Int[Transfer.transferred]
kuncar@53953
   417
lemmas funion_fminus = Un_Diff[Transfer.transferred]
kuncar@53953
   418
lemmas finter_fminus = Int_Diff[Transfer.transferred]
kuncar@53953
   419
lemmas fminus_finter_distrib = Diff_Int_distrib[Transfer.transferred]
kuncar@53953
   420
lemmas fminus_finter_distrib2 = Diff_Int_distrib2[Transfer.transferred]
kuncar@53953
   421
lemmas fUNIV_bool[no_atp] = UNIV_bool[Transfer.transferred]
kuncar@53953
   422
lemmas fPow_fempty[simp] = Pow_empty[Transfer.transferred]
kuncar@53953
   423
lemmas fPow_finsert = Pow_insert[Transfer.transferred]
kuncar@53964
   424
lemmas funion_fPow_fsubset = Un_Pow_subset[Transfer.transferred]
kuncar@53953
   425
lemmas fPow_finter_eq[simp] = Pow_Int_eq[Transfer.transferred]
kuncar@53964
   426
lemmas fset_eq_fsubset = set_eq_subset[Transfer.transferred]
kuncar@53964
   427
lemmas fsubset_iff[no_atp] = subset_iff[Transfer.transferred]
kuncar@53964
   428
lemmas fsubset_iff_pfsubset_eq = subset_iff_psubset_eq[Transfer.transferred]
kuncar@53953
   429
lemmas all_not_fin_conv[simp] = all_not_in_conv[Transfer.transferred]
kuncar@53953
   430
lemmas ex_fin_conv = ex_in_conv[Transfer.transferred]
kuncar@53953
   431
lemmas fimage_mono = image_mono[Transfer.transferred]
kuncar@53953
   432
lemmas fPow_mono = Pow_mono[Transfer.transferred]
kuncar@53953
   433
lemmas finsert_mono = insert_mono[Transfer.transferred]
kuncar@53953
   434
lemmas funion_mono = Un_mono[Transfer.transferred]
kuncar@53953
   435
lemmas finter_mono = Int_mono[Transfer.transferred]
kuncar@53953
   436
lemmas fminus_mono = Diff_mono[Transfer.transferred]
kuncar@53953
   437
lemmas fin_mono = in_mono[Transfer.transferred]
kuncar@53953
   438
lemmas fthe_felem_eq[simp] = the_elem_eq[Transfer.transferred]
kuncar@53953
   439
lemmas fLeast_mono = Least_mono[Transfer.transferred]
kuncar@53953
   440
lemmas fbind_fbind = bind_bind[Transfer.transferred]
kuncar@53953
   441
lemmas fempty_fbind[simp] = empty_bind[Transfer.transferred]
kuncar@53953
   442
lemmas nonfempty_fbind_const = nonempty_bind_const[Transfer.transferred]
kuncar@53953
   443
lemmas fbind_const = bind_const[Transfer.transferred]
kuncar@53953
   444
lemmas ffmember_filter[simp] = member_filter[Transfer.transferred]
kuncar@53953
   445
lemmas fequalityI = equalityI[Transfer.transferred]
lars@63622
   446
lemmas fset_of_list_simps[simp] = set_simps[Transfer.transferred]
lars@63622
   447
lemmas fset_of_list_append[simp] = set_append[Transfer.transferred]
lars@63622
   448
lemmas fset_of_list_rev[simp] = set_rev[Transfer.transferred]
lars@63622
   449
lemmas fset_of_list_map[simp] = set_map[Transfer.transferred]
kuncar@53953
   450
blanchet@55129
   451
wenzelm@60500
   452
subsection \<open>Additional lemmas\<close>
kuncar@53953
   453
wenzelm@61585
   454
subsubsection \<open>\<open>fsingleton\<close>\<close>
kuncar@53953
   455
kuncar@53953
   456
lemmas fsingletonE = fsingletonD [elim_format]
kuncar@53953
   457
blanchet@55129
   458
wenzelm@61585
   459
subsubsection \<open>\<open>femepty\<close>\<close>
kuncar@53953
   460
kuncar@53953
   461
lemma fempty_ffilter[simp]: "ffilter (\<lambda>_. False) A = {||}"
kuncar@53953
   462
by transfer auto
kuncar@53953
   463
kuncar@53953
   464
(* FIXME, transferred doesn't work here *)
kuncar@53953
   465
lemma femptyE [elim!]: "a |\<in>| {||} \<Longrightarrow> P"
kuncar@53953
   466
  by simp
kuncar@53953
   467
blanchet@55129
   468
wenzelm@61585
   469
subsubsection \<open>\<open>fset\<close>\<close>
kuncar@53953
   470
kuncar@53963
   471
lemmas fset_simps[simp] = bot_fset.rep_eq finsert.rep_eq
kuncar@53953
   472
hoelzl@63331
   473
lemma finite_fset [simp]:
kuncar@53953
   474
  shows "finite (fset S)"
kuncar@53953
   475
  by transfer simp
kuncar@53953
   476
kuncar@53963
   477
lemmas fset_cong = fset_inject
kuncar@53953
   478
kuncar@53953
   479
lemma filter_fset [simp]:
kuncar@53953
   480
  shows "fset (ffilter P xs) = Collect P \<inter> fset xs"
kuncar@53953
   481
  by transfer auto
kuncar@53953
   482
kuncar@53963
   483
lemma notin_fset: "x |\<notin>| S \<longleftrightarrow> x \<notin> fset S" by (simp add: fmember.rep_eq)
kuncar@53963
   484
kuncar@53963
   485
lemmas inter_fset[simp] = inf_fset.rep_eq
kuncar@53953
   486
kuncar@53963
   487
lemmas union_fset[simp] = sup_fset.rep_eq
kuncar@53953
   488
kuncar@53963
   489
lemmas minus_fset[simp] = minus_fset.rep_eq
kuncar@53953
   490
blanchet@55129
   491
lars@63622
   492
subsubsection \<open>\<open>ffilter\<close>\<close>
kuncar@53953
   493
hoelzl@63331
   494
lemma subset_ffilter:
kuncar@53953
   495
  "ffilter P A |\<subseteq>| ffilter Q A = (\<forall> x. x |\<in>| A \<longrightarrow> P x \<longrightarrow> Q x)"
kuncar@53953
   496
  by transfer auto
kuncar@53953
   497
hoelzl@63331
   498
lemma eq_ffilter:
kuncar@53953
   499
  "(ffilter P A = ffilter Q A) = (\<forall>x. x |\<in>| A \<longrightarrow> P x = Q x)"
kuncar@53953
   500
  by transfer auto
kuncar@53953
   501
kuncar@53964
   502
lemma pfsubset_ffilter:
hoelzl@63331
   503
  "(\<And>x. x |\<in>| A \<Longrightarrow> P x \<Longrightarrow> Q x) \<Longrightarrow> (x |\<in>| A & \<not> P x & Q x) \<Longrightarrow>
kuncar@53953
   504
    ffilter P A |\<subset>| ffilter Q A"
kuncar@53953
   505
  unfolding less_fset_def by (auto simp add: subset_ffilter eq_ffilter)
kuncar@53953
   506
blanchet@55129
   507
lars@63622
   508
subsubsection \<open>\<open>fset_of_list\<close>\<close>
lars@63622
   509
lars@63622
   510
lemma fset_of_list_filter[simp]:
lars@63622
   511
  "fset_of_list (filter P xs) = ffilter P (fset_of_list xs)"
lars@63622
   512
  by transfer (auto simp: Set.filter_def)
lars@63622
   513
lars@63622
   514
lemma fset_of_list_subset[intro]:
lars@63622
   515
  "set xs \<subseteq> set ys \<Longrightarrow> fset_of_list xs |\<subseteq>| fset_of_list ys"
lars@63622
   516
  by transfer simp
lars@63622
   517
lars@63622
   518
lemma fset_of_list_elem: "(x |\<in>| fset_of_list xs) \<longleftrightarrow> (x \<in> set xs)"
lars@63622
   519
  by transfer simp
lars@63622
   520
lars@63622
   521
wenzelm@61585
   522
subsubsection \<open>\<open>finsert\<close>\<close>
kuncar@53953
   523
kuncar@53953
   524
(* FIXME, transferred doesn't work here *)
kuncar@53953
   525
lemma set_finsert:
kuncar@53953
   526
  assumes "x |\<in>| A"
kuncar@53953
   527
  obtains B where "A = finsert x B" and "x |\<notin>| B"
kuncar@53953
   528
using assms by transfer (metis Set.set_insert finite_insert)
kuncar@53953
   529
kuncar@53953
   530
lemma mk_disjoint_finsert: "a |\<in>| A \<Longrightarrow> \<exists>B. A = finsert a B \<and> a |\<notin>| B"
wenzelm@63649
   531
  by (rule exI [where x = "A |-| {|a|}"]) blast
kuncar@53953
   532
blanchet@55129
   533
wenzelm@61585
   534
subsubsection \<open>\<open>fimage\<close>\<close>
kuncar@53953
   535
kuncar@53953
   536
lemma subset_fimage_iff: "(B |\<subseteq>| f|`|A) = (\<exists> AA. AA |\<subseteq>| A \<and> B = f|`|AA)"
kuncar@53953
   537
by transfer (metis mem_Collect_eq rev_finite_subset subset_image_iff)
kuncar@53953
   538
blanchet@55129
   539
wenzelm@60500
   540
subsubsection \<open>bounded quantification\<close>
kuncar@53953
   541
kuncar@53953
   542
lemma bex_simps [simp, no_atp]:
hoelzl@63331
   543
  "\<And>A P Q. fBex A (\<lambda>x. P x \<and> Q) = (fBex A P \<and> Q)"
kuncar@53953
   544
  "\<And>A P Q. fBex A (\<lambda>x. P \<and> Q x) = (P \<and> fBex A Q)"
hoelzl@63331
   545
  "\<And>P. fBex {||} P = False"
kuncar@53953
   546
  "\<And>a B P. fBex (finsert a B) P = (P a \<or> fBex B P)"
kuncar@53953
   547
  "\<And>A P f. fBex (f |`| A) P = fBex A (\<lambda>x. P (f x))"
kuncar@53953
   548
  "\<And>A P. (\<not> fBex A P) = fBall A (\<lambda>x. \<not> P x)"
kuncar@53953
   549
by auto
kuncar@53953
   550
kuncar@53953
   551
lemma ball_simps [simp, no_atp]:
kuncar@53953
   552
  "\<And>A P Q. fBall A (\<lambda>x. P x \<or> Q) = (fBall A P \<or> Q)"
kuncar@53953
   553
  "\<And>A P Q. fBall A (\<lambda>x. P \<or> Q x) = (P \<or> fBall A Q)"
kuncar@53953
   554
  "\<And>A P Q. fBall A (\<lambda>x. P \<longrightarrow> Q x) = (P \<longrightarrow> fBall A Q)"
kuncar@53953
   555
  "\<And>A P Q. fBall A (\<lambda>x. P x \<longrightarrow> Q) = (fBex A P \<longrightarrow> Q)"
kuncar@53953
   556
  "\<And>P. fBall {||} P = True"
kuncar@53953
   557
  "\<And>a B P. fBall (finsert a B) P = (P a \<and> fBall B P)"
kuncar@53953
   558
  "\<And>A P f. fBall (f |`| A) P = fBall A (\<lambda>x. P (f x))"
kuncar@53953
   559
  "\<And>A P. (\<not> fBall A P) = fBex A (\<lambda>x. \<not> P x)"
kuncar@53953
   560
by auto
kuncar@53953
   561
kuncar@53953
   562
lemma atomize_fBall:
kuncar@53953
   563
    "(\<And>x. x |\<in>| A ==> P x) == Trueprop (fBall A (\<lambda>x. P x))"
kuncar@53953
   564
apply (simp only: atomize_all atomize_imp)
kuncar@53953
   565
apply (rule equal_intr_rule)
lars@63622
   566
  by (transfer, simp)+
lars@63622
   567
lars@63622
   568
lemma fBall_mono[mono]: "P \<le> Q \<Longrightarrow> fBall S P \<le> fBall S Q"
lars@63622
   569
by auto
lars@63622
   570
kuncar@53953
   571
kuncar@53963
   572
end
kuncar@53963
   573
blanchet@55129
   574
wenzelm@61585
   575
subsubsection \<open>\<open>fcard\<close>\<close>
kuncar@53963
   576
kuncar@53964
   577
(* FIXME: improve transferred to handle bounded meta quantification *)
kuncar@53964
   578
kuncar@53963
   579
lemma fcard_fempty:
kuncar@53963
   580
  "fcard {||} = 0"
kuncar@53963
   581
  by transfer (rule card_empty)
kuncar@53963
   582
kuncar@53963
   583
lemma fcard_finsert_disjoint:
kuncar@53963
   584
  "x |\<notin>| A \<Longrightarrow> fcard (finsert x A) = Suc (fcard A)"
kuncar@53963
   585
  by transfer (rule card_insert_disjoint)
kuncar@53963
   586
kuncar@53963
   587
lemma fcard_finsert_if:
kuncar@53963
   588
  "fcard (finsert x A) = (if x |\<in>| A then fcard A else Suc (fcard A))"
kuncar@53963
   589
  by transfer (rule card_insert_if)
kuncar@53963
   590
kuncar@53963
   591
lemma card_0_eq [simp, no_atp]:
kuncar@53963
   592
  "fcard A = 0 \<longleftrightarrow> A = {||}"
kuncar@53963
   593
  by transfer (rule card_0_eq)
kuncar@53963
   594
kuncar@53963
   595
lemma fcard_Suc_fminus1:
kuncar@53963
   596
  "x |\<in>| A \<Longrightarrow> Suc (fcard (A |-| {|x|})) = fcard A"
kuncar@53963
   597
  by transfer (rule card_Suc_Diff1)
kuncar@53963
   598
kuncar@53963
   599
lemma fcard_fminus_fsingleton:
kuncar@53963
   600
  "x |\<in>| A \<Longrightarrow> fcard (A |-| {|x|}) = fcard A - 1"
kuncar@53963
   601
  by transfer (rule card_Diff_singleton)
kuncar@53963
   602
kuncar@53963
   603
lemma fcard_fminus_fsingleton_if:
kuncar@53963
   604
  "fcard (A |-| {|x|}) = (if x |\<in>| A then fcard A - 1 else fcard A)"
kuncar@53963
   605
  by transfer (rule card_Diff_singleton_if)
kuncar@53963
   606
kuncar@53963
   607
lemma fcard_fminus_finsert[simp]:
kuncar@53963
   608
  assumes "a |\<in>| A" and "a |\<notin>| B"
kuncar@53963
   609
  shows "fcard (A |-| finsert a B) = fcard (A |-| B) - 1"
kuncar@53963
   610
using assms by transfer (rule card_Diff_insert)
kuncar@53963
   611
kuncar@53963
   612
lemma fcard_finsert: "fcard (finsert x A) = Suc (fcard (A |-| {|x|}))"
kuncar@53963
   613
by transfer (rule card_insert)
kuncar@53963
   614
kuncar@53963
   615
lemma fcard_finsert_le: "fcard A \<le> fcard (finsert x A)"
kuncar@53963
   616
by transfer (rule card_insert_le)
kuncar@53963
   617
kuncar@53963
   618
lemma fcard_mono:
kuncar@53963
   619
  "A |\<subseteq>| B \<Longrightarrow> fcard A \<le> fcard B"
kuncar@53963
   620
by transfer (rule card_mono)
kuncar@53963
   621
kuncar@53963
   622
lemma fcard_seteq: "A |\<subseteq>| B \<Longrightarrow> fcard B \<le> fcard A \<Longrightarrow> A = B"
kuncar@53963
   623
by transfer (rule card_seteq)
kuncar@53963
   624
kuncar@53963
   625
lemma pfsubset_fcard_mono: "A |\<subset>| B \<Longrightarrow> fcard A < fcard B"
kuncar@53963
   626
by transfer (rule psubset_card_mono)
kuncar@53963
   627
hoelzl@63331
   628
lemma fcard_funion_finter:
kuncar@53963
   629
  "fcard A + fcard B = fcard (A |\<union>| B) + fcard (A |\<inter>| B)"
kuncar@53963
   630
by transfer (rule card_Un_Int)
kuncar@53963
   631
kuncar@53963
   632
lemma fcard_funion_disjoint:
kuncar@53963
   633
  "A |\<inter>| B = {||} \<Longrightarrow> fcard (A |\<union>| B) = fcard A + fcard B"
kuncar@53963
   634
by transfer (rule card_Un_disjoint)
kuncar@53963
   635
kuncar@53963
   636
lemma fcard_funion_fsubset:
kuncar@53963
   637
  "B |\<subseteq>| A \<Longrightarrow> fcard (A |-| B) = fcard A - fcard B"
kuncar@53963
   638
by transfer (rule card_Diff_subset)
kuncar@53963
   639
kuncar@53963
   640
lemma diff_fcard_le_fcard_fminus:
kuncar@53963
   641
  "fcard A - fcard B \<le> fcard(A |-| B)"
kuncar@53963
   642
by transfer (rule diff_card_le_card_Diff)
kuncar@53963
   643
kuncar@53963
   644
lemma fcard_fminus1_less: "x |\<in>| A \<Longrightarrow> fcard (A |-| {|x|}) < fcard A"
kuncar@53963
   645
by transfer (rule card_Diff1_less)
kuncar@53963
   646
kuncar@53963
   647
lemma fcard_fminus2_less:
kuncar@53963
   648
  "x |\<in>| A \<Longrightarrow> y |\<in>| A \<Longrightarrow> fcard (A |-| {|x|} |-| {|y|}) < fcard A"
kuncar@53963
   649
by transfer (rule card_Diff2_less)
kuncar@53963
   650
kuncar@53963
   651
lemma fcard_fminus1_le: "fcard (A |-| {|x|}) \<le> fcard A"
kuncar@53963
   652
by transfer (rule card_Diff1_le)
kuncar@53963
   653
kuncar@53963
   654
lemma fcard_pfsubset: "A |\<subseteq>| B \<Longrightarrow> fcard A < fcard B \<Longrightarrow> A < B"
kuncar@53963
   655
by transfer (rule card_psubset)
kuncar@53963
   656
blanchet@55129
   657
wenzelm@61585
   658
subsubsection \<open>\<open>ffold\<close>\<close>
kuncar@53963
   659
kuncar@53963
   660
(* FIXME: improve transferred to handle bounded meta quantification *)
kuncar@53963
   661
kuncar@53963
   662
context comp_fun_commute
kuncar@53963
   663
begin
kuncar@53963
   664
  lemmas ffold_empty[simp] = fold_empty[Transfer.transferred]
kuncar@53963
   665
kuncar@53963
   666
  lemma ffold_finsert [simp]:
kuncar@53963
   667
    assumes "x |\<notin>| A"
kuncar@53963
   668
    shows "ffold f z (finsert x A) = f x (ffold f z A)"
kuncar@53963
   669
    using assms by (transfer fixing: f) (rule fold_insert)
kuncar@53963
   670
kuncar@53963
   671
  lemma ffold_fun_left_comm:
kuncar@53963
   672
    "f x (ffold f z A) = ffold f (f x z) A"
kuncar@53963
   673
    by (transfer fixing: f) (rule fold_fun_left_comm)
kuncar@53963
   674
kuncar@53963
   675
  lemma ffold_finsert2:
blanchet@56646
   676
    "x |\<notin>| A \<Longrightarrow> ffold f z (finsert x A) = ffold f (f x z) A"
kuncar@53963
   677
    by (transfer fixing: f) (rule fold_insert2)
kuncar@53963
   678
kuncar@53963
   679
  lemma ffold_rec:
kuncar@53963
   680
    assumes "x |\<in>| A"
kuncar@53963
   681
    shows "ffold f z A = f x (ffold f z (A |-| {|x|}))"
kuncar@53963
   682
    using assms by (transfer fixing: f) (rule fold_rec)
hoelzl@63331
   683
kuncar@53963
   684
  lemma ffold_finsert_fremove:
kuncar@53963
   685
    "ffold f z (finsert x A) = f x (ffold f z (A |-| {|x|}))"
kuncar@53963
   686
     by (transfer fixing: f) (rule fold_insert_remove)
kuncar@53963
   687
end
kuncar@53963
   688
kuncar@53963
   689
lemma ffold_fimage:
kuncar@53963
   690
  assumes "inj_on g (fset A)"
kuncar@53963
   691
  shows "ffold f z (g |`| A) = ffold (f \<circ> g) z A"
kuncar@53963
   692
using assms by transfer' (rule fold_image)
kuncar@53963
   693
kuncar@53963
   694
lemma ffold_cong:
kuncar@53963
   695
  assumes "comp_fun_commute f" "comp_fun_commute g"
kuncar@53963
   696
  "\<And>x. x |\<in>| A \<Longrightarrow> f x = g x"
kuncar@53963
   697
    and "s = t" and "A = B"
kuncar@53963
   698
  shows "ffold f s A = ffold g t B"
kuncar@53963
   699
using assms by transfer (metis Finite_Set.fold_cong)
kuncar@53963
   700
kuncar@53963
   701
context comp_fun_idem
kuncar@53963
   702
begin
kuncar@53963
   703
kuncar@53963
   704
  lemma ffold_finsert_idem:
blanchet@56646
   705
    "ffold f z (finsert x A) = f x (ffold f z A)"
kuncar@53963
   706
    by (transfer fixing: f) (rule fold_insert_idem)
hoelzl@63331
   707
kuncar@53963
   708
  declare ffold_finsert [simp del] ffold_finsert_idem [simp]
hoelzl@63331
   709
kuncar@53963
   710
  lemma ffold_finsert_idem2:
kuncar@53963
   711
    "ffold f z (finsert x A) = ffold f (f x z) A"
kuncar@53963
   712
    by (transfer fixing: f) (rule fold_insert_idem2)
kuncar@53963
   713
kuncar@53963
   714
end
kuncar@53963
   715
blanchet@55129
   716
wenzelm@60500
   717
subsection \<open>Choice in fsets\<close>
kuncar@53953
   718
hoelzl@63331
   719
lemma fset_choice:
kuncar@53953
   720
  assumes "\<forall>x. x |\<in>| A \<longrightarrow> (\<exists>y. P x y)"
kuncar@53953
   721
  shows "\<exists>f. \<forall>x. x |\<in>| A \<longrightarrow> P x (f x)"
kuncar@53953
   722
  using assms by transfer metis
kuncar@53953
   723
blanchet@55129
   724
wenzelm@60500
   725
subsection \<open>Induction and Cases rules for fsets\<close>
kuncar@53953
   726
kuncar@53953
   727
lemma fset_exhaust [case_names empty insert, cases type: fset]:
hoelzl@63331
   728
  assumes fempty_case: "S = {||} \<Longrightarrow> P"
kuncar@53953
   729
  and     finsert_case: "\<And>x S'. S = finsert x S' \<Longrightarrow> P"
kuncar@53953
   730
  shows "P"
kuncar@53953
   731
  using assms by transfer blast
kuncar@53953
   732
kuncar@53953
   733
lemma fset_induct [case_names empty insert]:
kuncar@53953
   734
  assumes fempty_case: "P {||}"
kuncar@53953
   735
  and     finsert_case: "\<And>x S. P S \<Longrightarrow> P (finsert x S)"
kuncar@53953
   736
  shows "P S"
kuncar@53953
   737
proof -
kuncar@53953
   738
  (* FIXME transfer and right_total vs. bi_total *)
kuncar@53953
   739
  note Domainp_forall_transfer[transfer_rule]
kuncar@53953
   740
  show ?thesis
kuncar@53953
   741
  using assms by transfer (auto intro: finite_induct)
kuncar@53953
   742
qed
kuncar@53953
   743
kuncar@53953
   744
lemma fset_induct_stronger [case_names empty insert, induct type: fset]:
kuncar@53953
   745
  assumes empty_fset_case: "P {||}"
kuncar@53953
   746
  and     insert_fset_case: "\<And>x S. \<lbrakk>x |\<notin>| S; P S\<rbrakk> \<Longrightarrow> P (finsert x S)"
kuncar@53953
   747
  shows "P S"
kuncar@53953
   748
proof -
kuncar@53953
   749
  (* FIXME transfer and right_total vs. bi_total *)
kuncar@53953
   750
  note Domainp_forall_transfer[transfer_rule]
kuncar@53953
   751
  show ?thesis
kuncar@53953
   752
  using assms by transfer (auto intro: finite_induct)
kuncar@53953
   753
qed
kuncar@53953
   754
kuncar@53953
   755
lemma fset_card_induct:
kuncar@53953
   756
  assumes empty_fset_case: "P {||}"
kuncar@53953
   757
  and     card_fset_Suc_case: "\<And>S T. Suc (fcard S) = (fcard T) \<Longrightarrow> P S \<Longrightarrow> P T"
kuncar@53953
   758
  shows "P S"
kuncar@53953
   759
proof (induct S)
kuncar@53953
   760
  case empty
kuncar@53953
   761
  show "P {||}" by (rule empty_fset_case)
kuncar@53953
   762
next
kuncar@53953
   763
  case (insert x S)
kuncar@53953
   764
  have h: "P S" by fact
kuncar@53953
   765
  have "x |\<notin>| S" by fact
hoelzl@63331
   766
  then have "Suc (fcard S) = fcard (finsert x S)"
kuncar@53953
   767
    by transfer auto
hoelzl@63331
   768
  then show "P (finsert x S)"
kuncar@53953
   769
    using h card_fset_Suc_case by simp
kuncar@53953
   770
qed
kuncar@53953
   771
kuncar@53953
   772
lemma fset_strong_cases:
kuncar@53953
   773
  obtains "xs = {||}"
kuncar@53953
   774
    | ys x where "x |\<notin>| ys" and "xs = finsert x ys"
kuncar@53953
   775
by transfer blast
kuncar@53953
   776
kuncar@53953
   777
lemma fset_induct2:
kuncar@53953
   778
  "P {||} {||} \<Longrightarrow>
kuncar@53953
   779
  (\<And>x xs. x |\<notin>| xs \<Longrightarrow> P (finsert x xs) {||}) \<Longrightarrow>
kuncar@53953
   780
  (\<And>y ys. y |\<notin>| ys \<Longrightarrow> P {||} (finsert y ys)) \<Longrightarrow>
kuncar@53953
   781
  (\<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
   782
  P xsa ysa"
kuncar@53953
   783
  apply (induct xsa arbitrary: ysa)
kuncar@53953
   784
  apply (induct_tac x rule: fset_induct_stronger)
kuncar@53953
   785
  apply simp_all
kuncar@53953
   786
  apply (induct_tac xa rule: fset_induct_stronger)
kuncar@53953
   787
  apply simp_all
kuncar@53953
   788
  done
kuncar@53953
   789
blanchet@55129
   790
wenzelm@60500
   791
subsection \<open>Setup for Lifting/Transfer\<close>
kuncar@53953
   792
wenzelm@60500
   793
subsubsection \<open>Relator and predicator properties\<close>
kuncar@53953
   794
blanchet@55938
   795
lift_definition rel_fset :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'b fset \<Rightarrow> bool" is rel_set
blanchet@55938
   796
parametric rel_set_transfer .
kuncar@53953
   797
hoelzl@63331
   798
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
   799
  \<and> (\<forall>y. \<exists>x. y|\<in>|B \<longrightarrow> x|\<in>|A \<and> R x y))"
kuncar@53953
   800
apply (rule ext)+
kuncar@53953
   801
apply transfer'
hoelzl@63331
   802
apply (subst rel_set_def[unfolded fun_eq_iff])
kuncar@53953
   803
by blast
kuncar@53953
   804
blanchet@55938
   805
lemma finite_rel_set:
kuncar@53953
   806
  assumes fin: "finite X" "finite Z"
blanchet@55938
   807
  assumes R_S: "rel_set (R OO S) X Z"
blanchet@55938
   808
  shows "\<exists>Y. finite Y \<and> rel_set R X Y \<and> rel_set S Y Z"
kuncar@53953
   809
proof -
kuncar@53953
   810
  obtain f where f: "\<forall>x\<in>X. R x (f x) \<and> (\<exists>z\<in>Z. S (f x) z)"
kuncar@53953
   811
  apply atomize_elim
kuncar@53953
   812
  apply (subst bchoice_iff[symmetric])
blanchet@55938
   813
  using R_S[unfolded rel_set_def OO_def] by blast
hoelzl@63331
   814
blanchet@56646
   815
  obtain g where g: "\<forall>z\<in>Z. S (g z) z \<and> (\<exists>x\<in>X. R x (g z))"
kuncar@53953
   816
  apply atomize_elim
kuncar@53953
   817
  apply (subst bchoice_iff[symmetric])
blanchet@55938
   818
  using R_S[unfolded rel_set_def OO_def] by blast
hoelzl@63331
   819
kuncar@53953
   820
  let ?Y = "f ` X \<union> g ` Z"
kuncar@53953
   821
  have "finite ?Y" by (simp add: fin)
blanchet@55938
   822
  moreover have "rel_set R X ?Y"
blanchet@55938
   823
    unfolding rel_set_def
kuncar@53953
   824
    using f g by clarsimp blast
blanchet@55938
   825
  moreover have "rel_set S ?Y Z"
blanchet@55938
   826
    unfolding rel_set_def
kuncar@53953
   827
    using f g by clarsimp blast
kuncar@53953
   828
  ultimately show ?thesis by metis
kuncar@53953
   829
qed
kuncar@53953
   830
wenzelm@60500
   831
subsubsection \<open>Transfer rules for the Transfer package\<close>
kuncar@53953
   832
wenzelm@60500
   833
text \<open>Unconditional transfer rules\<close>
kuncar@53953
   834
wenzelm@63343
   835
context includes lifting_syntax
kuncar@53963
   836
begin
kuncar@53963
   837
kuncar@53953
   838
lemmas fempty_transfer [transfer_rule] = empty_transfer[Transfer.transferred]
kuncar@53953
   839
kuncar@53953
   840
lemma finsert_transfer [transfer_rule]:
blanchet@55933
   841
  "(A ===> rel_fset A ===> rel_fset A) finsert finsert"
blanchet@55945
   842
  unfolding rel_fun_def rel_fset_alt_def by blast
kuncar@53953
   843
kuncar@53953
   844
lemma funion_transfer [transfer_rule]:
blanchet@55933
   845
  "(rel_fset A ===> rel_fset A ===> rel_fset A) funion funion"
blanchet@55945
   846
  unfolding rel_fun_def rel_fset_alt_def by blast
kuncar@53953
   847
kuncar@53953
   848
lemma ffUnion_transfer [transfer_rule]:
blanchet@55933
   849
  "(rel_fset (rel_fset A) ===> rel_fset A) ffUnion ffUnion"
blanchet@55945
   850
  unfolding rel_fun_def rel_fset_alt_def by transfer (simp, fast)
kuncar@53953
   851
kuncar@53953
   852
lemma fimage_transfer [transfer_rule]:
blanchet@55933
   853
  "((A ===> B) ===> rel_fset A ===> rel_fset B) fimage fimage"
blanchet@55945
   854
  unfolding rel_fun_def rel_fset_alt_def by simp blast
kuncar@53953
   855
kuncar@53953
   856
lemma fBall_transfer [transfer_rule]:
blanchet@55933
   857
  "(rel_fset A ===> (A ===> op =) ===> op =) fBall fBall"
blanchet@55945
   858
  unfolding rel_fset_alt_def rel_fun_def by blast
kuncar@53953
   859
kuncar@53953
   860
lemma fBex_transfer [transfer_rule]:
blanchet@55933
   861
  "(rel_fset A ===> (A ===> op =) ===> op =) fBex fBex"
blanchet@55945
   862
  unfolding rel_fset_alt_def rel_fun_def by blast
kuncar@53953
   863
kuncar@53953
   864
(* FIXME transfer doesn't work here *)
kuncar@53953
   865
lemma fPow_transfer [transfer_rule]:
blanchet@55933
   866
  "(rel_fset A ===> rel_fset (rel_fset A)) fPow fPow"
blanchet@55945
   867
  unfolding rel_fun_def
blanchet@55945
   868
  using Pow_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred]
kuncar@53953
   869
  by blast
kuncar@53953
   870
blanchet@55933
   871
lemma rel_fset_transfer [transfer_rule]:
blanchet@55933
   872
  "((A ===> B ===> op =) ===> rel_fset A ===> rel_fset B ===> op =)
blanchet@55933
   873
    rel_fset rel_fset"
blanchet@55945
   874
  unfolding rel_fun_def
blanchet@55945
   875
  using rel_set_transfer[unfolded rel_fun_def,rule_format, Transfer.transferred, where A = A and B = B]
kuncar@53953
   876
  by simp
kuncar@53953
   877
kuncar@53953
   878
lemma bind_transfer [transfer_rule]:
blanchet@55933
   879
  "(rel_fset A ===> (A ===> rel_fset B) ===> rel_fset B) fbind fbind"
wenzelm@63092
   880
  unfolding rel_fun_def
blanchet@55945
   881
  using bind_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
   882
wenzelm@60500
   883
text \<open>Rules requiring bi-unique, bi-total or right-total relations\<close>
kuncar@53953
   884
kuncar@53953
   885
lemma fmember_transfer [transfer_rule]:
kuncar@53953
   886
  assumes "bi_unique A"
blanchet@55933
   887
  shows "(A ===> rel_fset A ===> op =) (op |\<in>|) (op |\<in>|)"
blanchet@55945
   888
  using assms unfolding rel_fun_def rel_fset_alt_def bi_unique_def by metis
kuncar@53953
   889
kuncar@53953
   890
lemma finter_transfer [transfer_rule]:
kuncar@53953
   891
  assumes "bi_unique A"
blanchet@55933
   892
  shows "(rel_fset A ===> rel_fset A ===> rel_fset A) finter finter"
blanchet@55945
   893
  using assms unfolding rel_fun_def
blanchet@55945
   894
  using inter_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
   895
kuncar@53963
   896
lemma fminus_transfer [transfer_rule]:
kuncar@53953
   897
  assumes "bi_unique A"
blanchet@55933
   898
  shows "(rel_fset A ===> rel_fset A ===> rel_fset A) (op |-|) (op |-|)"
blanchet@55945
   899
  using assms unfolding rel_fun_def
blanchet@55945
   900
  using Diff_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
   901
kuncar@53953
   902
lemma fsubset_transfer [transfer_rule]:
kuncar@53953
   903
  assumes "bi_unique A"
blanchet@55933
   904
  shows "(rel_fset A ===> rel_fset A ===> op =) (op |\<subseteq>|) (op |\<subseteq>|)"
blanchet@55945
   905
  using assms unfolding rel_fun_def
blanchet@55945
   906
  using subset_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
   907
kuncar@53953
   908
lemma fSup_transfer [transfer_rule]:
blanchet@55938
   909
  "bi_unique A \<Longrightarrow> (rel_set (rel_fset A) ===> rel_fset A) Sup Sup"
wenzelm@63092
   910
  unfolding rel_fun_def
kuncar@53953
   911
  apply clarify
kuncar@53953
   912
  apply transfer'
blanchet@55945
   913
  using Sup_fset_transfer[unfolded rel_fun_def] by blast
kuncar@53953
   914
kuncar@53953
   915
(* FIXME: add right_total_fInf_transfer *)
kuncar@53953
   916
kuncar@53953
   917
lemma fInf_transfer [transfer_rule]:
kuncar@53953
   918
  assumes "bi_unique A" and "bi_total A"
blanchet@55938
   919
  shows "(rel_set (rel_fset A) ===> rel_fset A) Inf Inf"
blanchet@55945
   920
  using assms unfolding rel_fun_def
kuncar@53953
   921
  apply clarify
kuncar@53953
   922
  apply transfer'
blanchet@55945
   923
  using Inf_fset_transfer[unfolded rel_fun_def] by blast
kuncar@53953
   924
kuncar@53953
   925
lemma ffilter_transfer [transfer_rule]:
kuncar@53953
   926
  assumes "bi_unique A"
blanchet@55933
   927
  shows "((A ===> op=) ===> rel_fset A ===> rel_fset A) ffilter ffilter"
blanchet@55945
   928
  using assms unfolding rel_fun_def
blanchet@55945
   929
  using Lifting_Set.filter_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
   930
kuncar@53953
   931
lemma card_transfer [transfer_rule]:
blanchet@55933
   932
  "bi_unique A \<Longrightarrow> (rel_fset A ===> op =) fcard fcard"
wenzelm@63092
   933
  unfolding rel_fun_def
blanchet@55945
   934
  using card_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
   935
kuncar@53953
   936
end
kuncar@53953
   937
kuncar@53953
   938
lifting_update fset.lifting
kuncar@53953
   939
lifting_forget fset.lifting
kuncar@53953
   940
blanchet@55129
   941
wenzelm@60500
   942
subsection \<open>BNF setup\<close>
blanchet@55129
   943
blanchet@55129
   944
context
blanchet@55129
   945
includes fset.lifting
blanchet@55129
   946
begin
blanchet@55129
   947
blanchet@55933
   948
lemma rel_fset_alt:
blanchet@55933
   949
  "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
   950
by transfer (simp add: rel_set_def)
blanchet@55129
   951
blanchet@55129
   952
lemma fset_to_fset: "finite A \<Longrightarrow> fset (the_inv fset A) = A"
blanchet@55129
   953
apply (rule f_the_inv_into_f[unfolded inj_on_def])
blanchet@55129
   954
apply (simp add: fset_inject)
blanchet@55129
   955
apply (rule range_eqI Abs_fset_inverse[symmetric] CollectI)+
blanchet@55129
   956
.
blanchet@55129
   957
blanchet@55933
   958
lemma rel_fset_aux:
blanchet@55129
   959
"(\<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
   960
 ((BNF_Def.Grp {a. fset a \<subseteq> {(a, b). R a b}} (fimage fst))\<inverse>\<inverse> OO
blanchet@57398
   961
  BNF_Def.Grp {a. fset a \<subseteq> {(a, b). R a b}} (fimage snd)) a b" (is "?L = ?R")
blanchet@55129
   962
proof
blanchet@55129
   963
  assume ?L
wenzelm@63040
   964
  define R' where "R' =
wenzelm@63040
   965
    the_inv fset (Collect (case_prod R) \<inter> (fset a \<times> fset b))" (is "_ = the_inv fset ?L'")
blanchet@55129
   966
  have "finite ?L'" by (intro finite_Int[OF disjI2] finite_cartesian_product) (transfer, simp)+
blanchet@55129
   967
  hence *: "fset R' = ?L'" unfolding R'_def by (intro fset_to_fset)
blanchet@55129
   968
  show ?R unfolding Grp_def relcompp.simps conversep.simps
blanchet@55414
   969
  proof (intro CollectI case_prodI exI[of _ a] exI[of _ b] exI[of _ R'] conjI refl)
wenzelm@60500
   970
    from * show "a = fimage fst R'" using conjunct1[OF \<open>?L\<close>]
blanchet@55129
   971
      by (transfer, auto simp add: image_def Int_def split: prod.splits)
wenzelm@60500
   972
    from * show "b = fimage snd R'" using conjunct2[OF \<open>?L\<close>]
blanchet@55129
   973
      by (transfer, auto simp add: image_def Int_def split: prod.splits)
blanchet@55129
   974
  qed (auto simp add: *)
blanchet@55129
   975
next
blanchet@55129
   976
  assume ?R thus ?L unfolding Grp_def relcompp.simps conversep.simps
blanchet@55129
   977
  apply (simp add: subset_eq Ball_def)
blanchet@55129
   978
  apply (rule conjI)
blanchet@55129
   979
  apply (transfer, clarsimp, metis snd_conv)
blanchet@55129
   980
  by (transfer, clarsimp, metis fst_conv)
blanchet@55129
   981
qed
blanchet@55129
   982
blanchet@55129
   983
bnf "'a fset"
blanchet@55129
   984
  map: fimage
hoelzl@63331
   985
  sets: fset
blanchet@55129
   986
  bd: natLeq
blanchet@55129
   987
  wits: "{||}"
blanchet@55933
   988
  rel: rel_fset
blanchet@55129
   989
apply -
blanchet@55129
   990
          apply transfer' apply simp
blanchet@55129
   991
         apply transfer' apply force
blanchet@55129
   992
        apply transfer apply force
blanchet@55129
   993
       apply transfer' apply force
blanchet@55129
   994
      apply (rule natLeq_card_order)
blanchet@55129
   995
     apply (rule natLeq_cinfinite)
blanchet@55129
   996
    apply transfer apply (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq)
blanchet@55933
   997
   apply (fastforce simp: rel_fset_alt)
traytel@62324
   998
 apply (simp add: Grp_def relcompp.simps conversep.simps fun_eq_iff rel_fset_alt
hoelzl@63331
   999
   rel_fset_aux[unfolded OO_Grp_alt])
blanchet@55129
  1000
apply transfer apply simp
blanchet@55129
  1001
done
blanchet@55129
  1002
blanchet@55938
  1003
lemma rel_fset_fset: "rel_set \<chi> (fset A1) (fset A2) = rel_fset \<chi> A1 A2"
blanchet@55129
  1004
  by transfer (rule refl)
blanchet@55129
  1005
kuncar@53953
  1006
end
blanchet@55129
  1007
blanchet@55129
  1008
lemmas [simp] = fset.map_comp fset.map_id fset.set_map
blanchet@55129
  1009
blanchet@55129
  1010
wenzelm@60500
  1011
subsection \<open>Size setup\<close>
blanchet@56646
  1012
blanchet@56646
  1013
context includes fset.lifting begin
nipkow@64267
  1014
lift_definition size_fset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a fset \<Rightarrow> nat" is "\<lambda>f. sum (Suc \<circ> f)" .
blanchet@56646
  1015
end
blanchet@56646
  1016
blanchet@56646
  1017
instantiation fset :: (type) size begin
blanchet@56646
  1018
definition size_fset where
blanchet@56646
  1019
  size_fset_overloaded_def: "size_fset = FSet.size_fset (\<lambda>_. 0)"
blanchet@56646
  1020
instance ..
blanchet@56646
  1021
end
blanchet@56646
  1022
blanchet@56646
  1023
lemmas size_fset_simps[simp] =
blanchet@56646
  1024
  size_fset_def[THEN meta_eq_to_obj_eq, THEN fun_cong, THEN fun_cong,
blanchet@56646
  1025
    unfolded map_fun_def comp_def id_apply]
blanchet@56646
  1026
blanchet@56646
  1027
lemmas size_fset_overloaded_simps[simp] =
blanchet@56646
  1028
  size_fset_simps[of "\<lambda>_. 0", unfolded add_0_left add_0_right,
blanchet@56646
  1029
    folded size_fset_overloaded_def]
blanchet@56646
  1030
blanchet@56646
  1031
lemma fset_size_o_map: "inj f \<Longrightarrow> size_fset g \<circ> fimage f = size_fset (g \<circ> f)"
kuncar@60228
  1032
  apply (subst fun_eq_iff)
nipkow@64267
  1033
  including fset.lifting by transfer (auto intro: sum.reindex_cong subset_inj_on)
hoelzl@63331
  1034
wenzelm@60500
  1035
setup \<open>
blanchet@56651
  1036
BNF_LFP_Size.register_size_global @{type_name fset} @{const_name size_fset}
blanchet@62082
  1037
  @{thm size_fset_overloaded_def} @{thms size_fset_simps size_fset_overloaded_simps}
blanchet@62082
  1038
  @{thms fset_size_o_map}
wenzelm@60500
  1039
\<close>
blanchet@56646
  1040
kuncar@60228
  1041
lifting_update fset.lifting
kuncar@60228
  1042
lifting_forget fset.lifting
blanchet@56646
  1043
wenzelm@60500
  1044
subsection \<open>Advanced relator customization\<close>
blanchet@55129
  1045
blanchet@55129
  1046
(* Set vs. sum relators: *)
blanchet@55129
  1047
hoelzl@63331
  1048
lemma rel_set_rel_sum[simp]:
hoelzl@63331
  1049
"rel_set (rel_sum \<chi> \<phi>) A1 A2 \<longleftrightarrow>
blanchet@55938
  1050
 rel_set \<chi> (Inl -` A1) (Inl -` A2) \<and> rel_set \<phi> (Inr -` A1) (Inr -` A2)"
blanchet@55129
  1051
(is "?L \<longleftrightarrow> ?Rl \<and> ?Rr")
blanchet@55129
  1052
proof safe
blanchet@55129
  1053
  assume L: "?L"
blanchet@55938
  1054
  show ?Rl unfolding rel_set_def Bex_def vimage_eq proof safe
blanchet@55129
  1055
    fix l1 assume "Inl l1 \<in> A1"
blanchet@55943
  1056
    then obtain a2 where a2: "a2 \<in> A2" and "rel_sum \<chi> \<phi> (Inl l1) a2"
blanchet@55938
  1057
    using L unfolding rel_set_def by auto
blanchet@55129
  1058
    then obtain l2 where "a2 = Inl l2 \<and> \<chi> l1 l2" by (cases a2, auto)
blanchet@55129
  1059
    thus "\<exists> l2. Inl l2 \<in> A2 \<and> \<chi> l1 l2" using a2 by auto
blanchet@55129
  1060
  next
blanchet@55129
  1061
    fix l2 assume "Inl l2 \<in> A2"
blanchet@55943
  1062
    then obtain a1 where a1: "a1 \<in> A1" and "rel_sum \<chi> \<phi> a1 (Inl l2)"
blanchet@55938
  1063
    using L unfolding rel_set_def by auto
blanchet@55129
  1064
    then obtain l1 where "a1 = Inl l1 \<and> \<chi> l1 l2" by (cases a1, auto)
blanchet@55129
  1065
    thus "\<exists> l1. Inl l1 \<in> A1 \<and> \<chi> l1 l2" using a1 by auto
blanchet@55129
  1066
  qed
blanchet@55938
  1067
  show ?Rr unfolding rel_set_def Bex_def vimage_eq proof safe
blanchet@55129
  1068
    fix r1 assume "Inr r1 \<in> A1"
blanchet@55943
  1069
    then obtain a2 where a2: "a2 \<in> A2" and "rel_sum \<chi> \<phi> (Inr r1) a2"
blanchet@55938
  1070
    using L unfolding rel_set_def by auto
blanchet@55129
  1071
    then obtain r2 where "a2 = Inr r2 \<and> \<phi> r1 r2" by (cases a2, auto)
blanchet@55129
  1072
    thus "\<exists> r2. Inr r2 \<in> A2 \<and> \<phi> r1 r2" using a2 by auto
blanchet@55129
  1073
  next
blanchet@55129
  1074
    fix r2 assume "Inr r2 \<in> A2"
blanchet@55943
  1075
    then obtain a1 where a1: "a1 \<in> A1" and "rel_sum \<chi> \<phi> a1 (Inr r2)"
blanchet@55938
  1076
    using L unfolding rel_set_def by auto
blanchet@55129
  1077
    then obtain r1 where "a1 = Inr r1 \<and> \<phi> r1 r2" by (cases a1, auto)
blanchet@55129
  1078
    thus "\<exists> r1. Inr r1 \<in> A1 \<and> \<phi> r1 r2" using a1 by auto
blanchet@55129
  1079
  qed
blanchet@55129
  1080
next
blanchet@55129
  1081
  assume Rl: "?Rl" and Rr: "?Rr"
blanchet@55938
  1082
  show ?L unfolding rel_set_def Bex_def vimage_eq proof safe
blanchet@55129
  1083
    fix a1 assume a1: "a1 \<in> A1"
blanchet@55943
  1084
    show "\<exists> a2. a2 \<in> A2 \<and> rel_sum \<chi> \<phi> a1 a2"
blanchet@55129
  1085
    proof(cases a1)
blanchet@55129
  1086
      case (Inl l1) then obtain l2 where "Inl l2 \<in> A2 \<and> \<chi> l1 l2"
blanchet@55938
  1087
      using Rl a1 unfolding rel_set_def by blast
blanchet@55129
  1088
      thus ?thesis unfolding Inl by auto
blanchet@55129
  1089
    next
blanchet@55129
  1090
      case (Inr r1) then obtain r2 where "Inr r2 \<in> A2 \<and> \<phi> r1 r2"
blanchet@55938
  1091
      using Rr a1 unfolding rel_set_def by blast
blanchet@55129
  1092
      thus ?thesis unfolding Inr by auto
blanchet@55129
  1093
    qed
blanchet@55129
  1094
  next
blanchet@55129
  1095
    fix a2 assume a2: "a2 \<in> A2"
blanchet@55943
  1096
    show "\<exists> a1. a1 \<in> A1 \<and> rel_sum \<chi> \<phi> a1 a2"
blanchet@55129
  1097
    proof(cases a2)
blanchet@55129
  1098
      case (Inl l2) then obtain l1 where "Inl l1 \<in> A1 \<and> \<chi> l1 l2"
blanchet@55938
  1099
      using Rl a2 unfolding rel_set_def by blast
blanchet@55129
  1100
      thus ?thesis unfolding Inl by auto
blanchet@55129
  1101
    next
blanchet@55129
  1102
      case (Inr r2) then obtain r1 where "Inr r1 \<in> A1 \<and> \<phi> r1 r2"
blanchet@55938
  1103
      using Rr a2 unfolding rel_set_def by blast
blanchet@55129
  1104
      thus ?thesis unfolding Inr by auto
blanchet@55129
  1105
    qed
blanchet@55129
  1106
  qed
blanchet@55129
  1107
qed
blanchet@55129
  1108
lars@60712
  1109
lars@60712
  1110
subsection \<open>Quickcheck setup\<close>
lars@60712
  1111
lars@60712
  1112
text \<open>Setup adapted from sets.\<close>
lars@60712
  1113
lars@60712
  1114
notation Quickcheck_Exhaustive.orelse (infixr "orelse" 55)
lars@60712
  1115
lars@60712
  1116
definition (in term_syntax) [code_unfold]:
lars@60712
  1117
"valterm_femptyset = Code_Evaluation.valtermify ({||} :: ('a :: typerep) fset)"
lars@60712
  1118
lars@60712
  1119
definition (in term_syntax) [code_unfold]:
lars@60712
  1120
"valtermify_finsert x s = Code_Evaluation.valtermify finsert {\<cdot>} (x :: ('a :: typerep * _)) {\<cdot>} s"
lars@60712
  1121
lars@60712
  1122
instantiation fset :: (exhaustive) exhaustive
lars@60712
  1123
begin
lars@60712
  1124
lars@60712
  1125
fun exhaustive_fset where
lars@60712
  1126
"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
  1127
lars@60712
  1128
instance ..
lars@60712
  1129
blanchet@55129
  1130
end
lars@60712
  1131
lars@60712
  1132
instantiation fset :: (full_exhaustive) full_exhaustive
lars@60712
  1133
begin
lars@60712
  1134
lars@60712
  1135
fun full_exhaustive_fset where
lars@60712
  1136
"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
  1137
lars@60712
  1138
instance ..
lars@60712
  1139
lars@60712
  1140
end
lars@60712
  1141
lars@60712
  1142
no_notation Quickcheck_Exhaustive.orelse (infixr "orelse" 55)
lars@60712
  1143
lars@60712
  1144
notation scomp (infixl "\<circ>\<rightarrow>" 60)
lars@60712
  1145
lars@60712
  1146
instantiation fset :: (random) random
lars@60712
  1147
begin
lars@60712
  1148
lars@60712
  1149
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
  1150
"random_aux_fset 0 j = Quickcheck_Random.collapse (Random.select_weight [(1, Pair valterm_femptyset)])" |
lars@60712
  1151
"random_aux_fset (Code_Numeral.Suc i) j =
lars@60712
  1152
  Quickcheck_Random.collapse (Random.select_weight
lars@60712
  1153
    [(1, Pair valterm_femptyset),
lars@60712
  1154
     (Code_Numeral.Suc i,
lars@60712
  1155
      Quickcheck_Random.random j \<circ>\<rightarrow> (\<lambda>x. random_aux_fset i j \<circ>\<rightarrow> (\<lambda>s. Pair (valtermify_finsert x s))))])"
lars@60712
  1156
lars@60712
  1157
lemma [code]:
lars@60712
  1158
  "random_aux_fset i j =
lars@60712
  1159
    Quickcheck_Random.collapse (Random.select_weight [(1, Pair valterm_femptyset),
lars@60712
  1160
      (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
  1161
proof (induct i rule: natural.induct)
lars@60712
  1162
  case zero
lars@60712
  1163
  show ?case by (subst select_weight_drop_zero[symmetric]) (simp add: less_natural_def)
lars@60712
  1164
next
lars@60712
  1165
  case (Suc i)
lars@60712
  1166
  show ?case by (simp only: random_aux_fset.simps Suc_natural_minus_one)
lars@60712
  1167
qed
lars@60712
  1168
lars@60712
  1169
definition "random_fset i = random_aux_fset i i"
lars@60712
  1170
lars@60712
  1171
instance ..
lars@60712
  1172
lars@60712
  1173
end
lars@60712
  1174
lars@60712
  1175
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
lars@60712
  1176
lars@60712
  1177
end