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