src/HOL/Library/FSet.thy
author haftmann
Wed Jul 18 20:51:21 2018 +0200 (11 months ago)
changeset 68658 16cc1161ad7f
parent 68463 410818a69ee3
child 69164 74f1b0f10b2b
permissions -rw-r--r--
tuned equation
kuncar@53953
     1
(*  Title:      HOL/Library/FSet.thy
kuncar@53953
     2
    Author:     Ondrej Kuncar, TU Muenchen
kuncar@53953
     3
    Author:     Cezary Kaliszyk and Christian Urban
blanchet@55129
     4
    Author:     Andrei Popescu, TU Muenchen
kuncar@53953
     5
*)
kuncar@53953
     6
wenzelm@60500
     7
section \<open>Type of finite sets defined as a subtype of sets\<close>
kuncar@53953
     8
kuncar@53953
     9
theory FSet
lars@66262
    10
imports Main Countable
kuncar@53953
    11
begin
kuncar@53953
    12
wenzelm@60500
    13
subsection \<open>Definition of the type\<close>
kuncar@53953
    14
kuncar@53953
    15
typedef 'a fset = "{A :: 'a set. finite A}"  morphisms fset Abs_fset
kuncar@53953
    16
by auto
kuncar@53953
    17
kuncar@53953
    18
setup_lifting type_definition_fset
kuncar@53953
    19
blanchet@55129
    20
wenzelm@60500
    21
subsection \<open>Basic operations and type class instantiations\<close>
kuncar@53953
    22
kuncar@53953
    23
(* FIXME transfer and right_total vs. bi_total *)
kuncar@53953
    24
instantiation fset :: (finite) finite
kuncar@53953
    25
begin
wenzelm@60679
    26
instance by (standard; transfer; simp)
kuncar@53953
    27
end
kuncar@53953
    28
kuncar@53953
    29
instantiation fset :: (type) "{bounded_lattice_bot, distrib_lattice, minus}"
kuncar@53953
    30
begin
kuncar@53953
    31
hoelzl@63331
    32
lift_definition bot_fset :: "'a fset" is "{}" parametric empty_transfer by simp
kuncar@53953
    33
hoelzl@63331
    34
lift_definition less_eq_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" is subset_eq parametric subset_transfer
kuncar@55565
    35
  .
kuncar@53953
    36
kuncar@53953
    37
definition less_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" where "xs < ys \<equiv> xs \<le> ys \<and> xs \<noteq> (ys::'a fset)"
kuncar@53953
    38
kuncar@53953
    39
lemma less_fset_transfer[transfer_rule]:
wenzelm@63343
    40
  includes lifting_syntax
hoelzl@63331
    41
  assumes [transfer_rule]: "bi_unique A"
nipkow@67399
    42
  shows "((pcr_fset A) ===> (pcr_fset A) ===> (=)) (\<subset>) (<)"
kuncar@53953
    43
  unfolding less_fset_def[abs_def] psubset_eq[abs_def] by transfer_prover
hoelzl@63331
    44
kuncar@53953
    45
kuncar@53953
    46
lift_definition sup_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is union parametric union_transfer
kuncar@53953
    47
  by simp
kuncar@53953
    48
kuncar@53953
    49
lift_definition inf_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is inter parametric inter_transfer
kuncar@53953
    50
  by simp
kuncar@53953
    51
kuncar@53953
    52
lift_definition minus_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is minus parametric Diff_transfer
kuncar@53953
    53
  by simp
kuncar@53953
    54
kuncar@53953
    55
instance
wenzelm@60679
    56
  by (standard; transfer; auto)+
kuncar@53953
    57
kuncar@53953
    58
end
kuncar@53953
    59
kuncar@53953
    60
abbreviation fempty :: "'a fset" ("{||}") where "{||} \<equiv> bot"
kuncar@53953
    61
abbreviation fsubset_eq :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subseteq>|" 50) where "xs |\<subseteq>| ys \<equiv> xs \<le> ys"
kuncar@53953
    62
abbreviation fsubset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subset>|" 50) where "xs |\<subset>| ys \<equiv> xs < ys"
kuncar@53953
    63
abbreviation funion :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" (infixl "|\<union>|" 65) where "xs |\<union>| ys \<equiv> sup xs ys"
kuncar@53953
    64
abbreviation finter :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" (infixl "|\<inter>|" 65) where "xs |\<inter>| ys \<equiv> inf xs ys"
kuncar@53953
    65
abbreviation fminus :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" (infixl "|-|" 65) where "xs |-| ys \<equiv> minus xs ys"
kuncar@53953
    66
traytel@54014
    67
instantiation fset :: (equal) equal
traytel@54014
    68
begin
traytel@54014
    69
definition "HOL.equal A B \<longleftrightarrow> A |\<subseteq>| B \<and> B |\<subseteq>| A"
traytel@54014
    70
instance by intro_classes (auto simp add: equal_fset_def)
hoelzl@63331
    71
end
traytel@54014
    72
kuncar@53953
    73
instantiation fset :: (type) conditionally_complete_lattice
kuncar@53953
    74
begin
kuncar@53953
    75
wenzelm@63343
    76
context includes lifting_syntax
wenzelm@63343
    77
begin
kuncar@53953
    78
kuncar@53953
    79
lemma right_total_Inf_fset_transfer:
kuncar@53953
    80
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
hoelzl@63331
    81
  shows "(rel_set (rel_set A) ===> rel_set A)
hoelzl@63331
    82
    (\<lambda>S. if finite (\<Inter>S \<inter> Collect (Domainp A)) then \<Inter>S \<inter> Collect (Domainp A) else {})
kuncar@53953
    83
      (\<lambda>S. if finite (Inf S) then Inf S else {})"
kuncar@53953
    84
    by transfer_prover
kuncar@53953
    85
kuncar@53953
    86
lemma Inf_fset_transfer:
kuncar@53953
    87
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
hoelzl@63331
    88
  shows "(rel_set (rel_set A) ===> rel_set A) (\<lambda>A. if finite (Inf A) then Inf A else {})
kuncar@53953
    89
    (\<lambda>A. if finite (Inf A) then Inf A else {})"
kuncar@53953
    90
  by transfer_prover
kuncar@53953
    91
hoelzl@63331
    92
lift_definition Inf_fset :: "'a fset set \<Rightarrow> 'a fset" is "\<lambda>A. if finite (Inf A) then Inf A else {}"
kuncar@53953
    93
parametric right_total_Inf_fset_transfer Inf_fset_transfer by simp
kuncar@53953
    94
kuncar@53953
    95
lemma Sup_fset_transfer:
kuncar@53953
    96
  assumes [transfer_rule]: "bi_unique A"
blanchet@55938
    97
  shows "(rel_set (rel_set A) ===> rel_set A) (\<lambda>A. if finite (Sup A) then Sup A else {})
kuncar@53953
    98
  (\<lambda>A. if finite (Sup A) then Sup A else {})" by transfer_prover
kuncar@53953
    99
kuncar@53953
   100
lift_definition Sup_fset :: "'a fset set \<Rightarrow> 'a fset" is "\<lambda>A. if finite (Sup A) then Sup A else {}"
kuncar@53953
   101
parametric Sup_fset_transfer by simp
kuncar@53953
   102
kuncar@53953
   103
lemma finite_Sup: "\<exists>z. finite z \<and> (\<forall>a. a \<in> X \<longrightarrow> a \<le> z) \<Longrightarrow> finite (Sup X)"
kuncar@53953
   104
by (auto intro: finite_subset)
kuncar@53953
   105
nipkow@67399
   106
lemma transfer_bdd_below[transfer_rule]: "(rel_set (pcr_fset (=)) ===> (=)) bdd_below bdd_below"
hoelzl@54258
   107
  by auto
hoelzl@54258
   108
wenzelm@63343
   109
end
wenzelm@63343
   110
kuncar@53953
   111
instance
hoelzl@63331
   112
proof
kuncar@53953
   113
  fix x z :: "'a fset"
kuncar@53953
   114
  fix X :: "'a fset set"
kuncar@53953
   115
  {
hoelzl@63331
   116
    assume "x \<in> X" "bdd_below X"
blanchet@56646
   117
    then show "Inf X |\<subseteq>| x" by transfer auto
kuncar@53953
   118
  next
kuncar@53953
   119
    assume "X \<noteq> {}" "(\<And>x. x \<in> X \<Longrightarrow> z |\<subseteq>| x)"
kuncar@53953
   120
    then show "z |\<subseteq>| Inf X" by transfer (clarsimp, blast)
kuncar@53953
   121
  next
hoelzl@54258
   122
    assume "x \<in> X" "bdd_above X"
hoelzl@54258
   123
    then obtain z where "x \<in> X" "(\<And>x. x \<in> X \<Longrightarrow> x |\<subseteq>| z)"
hoelzl@54258
   124
      by (auto simp: bdd_above_def)
hoelzl@54258
   125
    then show "x |\<subseteq>| Sup X"
hoelzl@54258
   126
      by transfer (auto intro!: finite_Sup)
kuncar@53953
   127
  next
kuncar@53953
   128
    assume "X \<noteq> {}" "(\<And>x. x \<in> X \<Longrightarrow> x |\<subseteq>| z)"
kuncar@53953
   129
    then show "Sup X |\<subseteq>| z" by transfer (clarsimp, blast)
kuncar@53953
   130
  }
kuncar@53953
   131
qed
kuncar@53953
   132
end
kuncar@53953
   133
hoelzl@63331
   134
instantiation fset :: (finite) complete_lattice
kuncar@53953
   135
begin
kuncar@53953
   136
wenzelm@60679
   137
lift_definition top_fset :: "'a fset" is UNIV parametric right_total_UNIV_transfer UNIV_transfer
wenzelm@60679
   138
  by simp
kuncar@53953
   139
wenzelm@60679
   140
instance
wenzelm@60679
   141
  by (standard; transfer; auto)
wenzelm@60679
   142
kuncar@53953
   143
end
kuncar@53953
   144
kuncar@53953
   145
instantiation fset :: (finite) complete_boolean_algebra
kuncar@53953
   146
begin
kuncar@53953
   147
hoelzl@63331
   148
lift_definition uminus_fset :: "'a fset \<Rightarrow> 'a fset" is uminus
kuncar@53953
   149
  parametric right_total_Compl_transfer Compl_transfer by simp
kuncar@53953
   150
wenzelm@60679
   151
instance
eberlm@67829
   152
  by (standard; transfer) (simp_all add: Inf_Sup Diff_eq)
kuncar@53953
   153
end
kuncar@53953
   154
kuncar@53953
   155
abbreviation fUNIV :: "'a::finite fset" where "fUNIV \<equiv> top"
kuncar@53953
   156
abbreviation fuminus :: "'a::finite fset \<Rightarrow> 'a fset" ("|-| _" [81] 80) where "|-| x \<equiv> uminus x"
kuncar@53953
   157
blanchet@56646
   158
declare top_fset.rep_eq[simp]
blanchet@56646
   159
blanchet@55129
   160
wenzelm@60500
   161
subsection \<open>Other operations\<close>
kuncar@53953
   162
kuncar@53953
   163
lift_definition finsert :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is insert parametric Lifting_Set.insert_transfer
kuncar@53953
   164
  by simp
kuncar@53953
   165
kuncar@53953
   166
syntax
kuncar@53953
   167
  "_insert_fset"     :: "args => 'a fset"  ("{|(_)|}")
kuncar@53953
   168
kuncar@53953
   169
translations
kuncar@53953
   170
  "{|x, xs|}" == "CONST finsert x {|xs|}"
kuncar@53953
   171
  "{|x|}"     == "CONST finsert x {||}"
kuncar@53953
   172
hoelzl@63331
   173
lift_definition fmember :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<in>|" 50) is Set.member
kuncar@55565
   174
  parametric member_transfer .
kuncar@53953
   175
kuncar@53953
   176
abbreviation notin_fset :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<notin>|" 50) where "x |\<notin>| S \<equiv> \<not> (x |\<in>| S)"
kuncar@53953
   177
wenzelm@63343
   178
context includes lifting_syntax
kuncar@53953
   179
begin
blanchet@55129
   180
hoelzl@63331
   181
lift_definition ffilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is Set.filter
kuncar@53953
   182
  parametric Lifting_Set.filter_transfer unfolding Set.filter_def by simp
kuncar@53953
   183
hoelzl@63331
   184
lift_definition fPow :: "'a fset \<Rightarrow> 'a fset fset" is Pow parametric Pow_transfer
kuncar@55732
   185
by (simp add: finite_subset)
kuncar@53953
   186
kuncar@55565
   187
lift_definition fcard :: "'a fset \<Rightarrow> nat" is card parametric card_transfer .
kuncar@53953
   188
hoelzl@63331
   189
lift_definition fimage :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset" (infixr "|`|" 90) is image
kuncar@53953
   190
  parametric image_transfer by simp
kuncar@53953
   191
kuncar@55565
   192
lift_definition fthe_elem :: "'a fset \<Rightarrow> 'a" is the_elem .
kuncar@53953
   193
hoelzl@63331
   194
lift_definition fbind :: "'a fset \<Rightarrow> ('a \<Rightarrow> 'b fset) \<Rightarrow> 'b fset" is Set.bind parametric bind_transfer
kuncar@55738
   195
by (simp add: Set.bind_def)
kuncar@53953
   196
kuncar@55732
   197
lift_definition ffUnion :: "'a fset fset \<Rightarrow> 'a fset" is Union parametric Union_transfer by simp
kuncar@53953
   198
kuncar@55565
   199
lift_definition fBall :: "'a fset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" is Ball parametric Ball_transfer .
kuncar@55565
   200
lift_definition fBex :: "'a fset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" is Bex parametric Bex_transfer .
kuncar@53953
   201
kuncar@55565
   202
lift_definition ffold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a fset \<Rightarrow> 'b" is Finite_Set.fold .
kuncar@53963
   203
lars@63622
   204
lift_definition fset_of_list :: "'a list \<Rightarrow> 'a fset" is set by (rule finite_set)
lars@63622
   205
lars@68463
   206
lift_definition sorted_list_of_fset :: "'a::linorder fset \<Rightarrow> 'a list" is sorted_list_of_set .
lars@68463
   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]
lars@66264
   229
lemmas fBall_cong[fundef_cong] = ball_cong[Transfer.transferred]
lars@66264
   230
lemmas fBex_cong[fundef_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]
lars@68463
   392
lemmas ffunion_empty[simp] = Union_empty[Transfer.transferred]
lars@68463
   393
lemmas ffunion_mono = Union_mono[Transfer.transferred]
lars@68463
   394
lemmas ffunion_insert[simp] = Union_insert[Transfer.transferred]
kuncar@53953
   395
lemmas fminus_finter2 = Diff_Int2[Transfer.transferred]
kuncar@53953
   396
lemmas funion_finter_assoc_eq = Un_Int_assoc_eq[Transfer.transferred]
kuncar@53953
   397
lemmas fBall_funion = ball_Un[Transfer.transferred]
kuncar@53953
   398
lemmas fBex_funion = bex_Un[Transfer.transferred]
kuncar@53953
   399
lemmas fminus_eq_fempty_iff[simp,no_atp] = Diff_eq_empty_iff[Transfer.transferred]
kuncar@53953
   400
lemmas fminus_cancel[simp] = Diff_cancel[Transfer.transferred]
kuncar@53953
   401
lemmas fminus_idemp[simp] = Diff_idemp[Transfer.transferred]
kuncar@53953
   402
lemmas fminus_triv = Diff_triv[Transfer.transferred]
kuncar@53953
   403
lemmas fempty_fminus[simp] = empty_Diff[Transfer.transferred]
kuncar@53953
   404
lemmas fminus_fempty[simp] = Diff_empty[Transfer.transferred]
kuncar@53953
   405
lemmas fminus_finsertffempty[simp,no_atp] = Diff_insert0[Transfer.transferred]
kuncar@53953
   406
lemmas fminus_finsert = Diff_insert[Transfer.transferred]
kuncar@53953
   407
lemmas fminus_finsert2 = Diff_insert2[Transfer.transferred]
kuncar@53953
   408
lemmas finsert_fminus_if = insert_Diff_if[Transfer.transferred]
kuncar@53953
   409
lemmas finsert_fminus1[simp] = insert_Diff1[Transfer.transferred]
kuncar@53953
   410
lemmas finsert_fminus_single[simp] = insert_Diff_single[Transfer.transferred]
kuncar@53953
   411
lemmas finsert_fminus = insert_Diff[Transfer.transferred]
kuncar@53953
   412
lemmas fminus_finsert_absorb = Diff_insert_absorb[Transfer.transferred]
kuncar@53953
   413
lemmas fminus_disjoint[simp] = Diff_disjoint[Transfer.transferred]
kuncar@53953
   414
lemmas fminus_partition = Diff_partition[Transfer.transferred]
kuncar@53953
   415
lemmas double_fminus = double_diff[Transfer.transferred]
kuncar@53953
   416
lemmas funion_fminus_cancel[simp] = Un_Diff_cancel[Transfer.transferred]
kuncar@53953
   417
lemmas funion_fminus_cancel2[simp] = Un_Diff_cancel2[Transfer.transferred]
kuncar@53953
   418
lemmas fminus_funion = Diff_Un[Transfer.transferred]
kuncar@53953
   419
lemmas fminus_finter = Diff_Int[Transfer.transferred]
kuncar@53953
   420
lemmas funion_fminus = Un_Diff[Transfer.transferred]
kuncar@53953
   421
lemmas finter_fminus = Int_Diff[Transfer.transferred]
kuncar@53953
   422
lemmas fminus_finter_distrib = Diff_Int_distrib[Transfer.transferred]
kuncar@53953
   423
lemmas fminus_finter_distrib2 = Diff_Int_distrib2[Transfer.transferred]
kuncar@53953
   424
lemmas fUNIV_bool[no_atp] = UNIV_bool[Transfer.transferred]
kuncar@53953
   425
lemmas fPow_fempty[simp] = Pow_empty[Transfer.transferred]
kuncar@53953
   426
lemmas fPow_finsert = Pow_insert[Transfer.transferred]
kuncar@53964
   427
lemmas funion_fPow_fsubset = Un_Pow_subset[Transfer.transferred]
kuncar@53953
   428
lemmas fPow_finter_eq[simp] = Pow_Int_eq[Transfer.transferred]
kuncar@53964
   429
lemmas fset_eq_fsubset = set_eq_subset[Transfer.transferred]
kuncar@53964
   430
lemmas fsubset_iff[no_atp] = subset_iff[Transfer.transferred]
kuncar@53964
   431
lemmas fsubset_iff_pfsubset_eq = subset_iff_psubset_eq[Transfer.transferred]
kuncar@53953
   432
lemmas all_not_fin_conv[simp] = all_not_in_conv[Transfer.transferred]
kuncar@53953
   433
lemmas ex_fin_conv = ex_in_conv[Transfer.transferred]
kuncar@53953
   434
lemmas fimage_mono = image_mono[Transfer.transferred]
kuncar@53953
   435
lemmas fPow_mono = Pow_mono[Transfer.transferred]
kuncar@53953
   436
lemmas finsert_mono = insert_mono[Transfer.transferred]
kuncar@53953
   437
lemmas funion_mono = Un_mono[Transfer.transferred]
kuncar@53953
   438
lemmas finter_mono = Int_mono[Transfer.transferred]
kuncar@53953
   439
lemmas fminus_mono = Diff_mono[Transfer.transferred]
kuncar@53953
   440
lemmas fin_mono = in_mono[Transfer.transferred]
kuncar@53953
   441
lemmas fthe_felem_eq[simp] = the_elem_eq[Transfer.transferred]
kuncar@53953
   442
lemmas fLeast_mono = Least_mono[Transfer.transferred]
kuncar@53953
   443
lemmas fbind_fbind = bind_bind[Transfer.transferred]
kuncar@53953
   444
lemmas fempty_fbind[simp] = empty_bind[Transfer.transferred]
kuncar@53953
   445
lemmas nonfempty_fbind_const = nonempty_bind_const[Transfer.transferred]
kuncar@53953
   446
lemmas fbind_const = bind_const[Transfer.transferred]
kuncar@53953
   447
lemmas ffmember_filter[simp] = member_filter[Transfer.transferred]
kuncar@53953
   448
lemmas fequalityI = equalityI[Transfer.transferred]
lars@63622
   449
lemmas fset_of_list_simps[simp] = set_simps[Transfer.transferred]
lars@63622
   450
lemmas fset_of_list_append[simp] = set_append[Transfer.transferred]
lars@63622
   451
lemmas fset_of_list_rev[simp] = set_rev[Transfer.transferred]
lars@63622
   452
lemmas fset_of_list_map[simp] = set_map[Transfer.transferred]
kuncar@53953
   453
blanchet@55129
   454
wenzelm@60500
   455
subsection \<open>Additional lemmas\<close>
kuncar@53953
   456
lars@66264
   457
subsubsection \<open>\<open>ffUnion\<close>\<close>
lars@66264
   458
lars@66264
   459
lemmas ffUnion_funion_distrib[simp] = Union_Un_distrib[Transfer.transferred]
lars@66264
   460
lars@66264
   461
lars@66264
   462
subsubsection \<open>\<open>fbind\<close>\<close>
lars@66264
   463
lars@66264
   464
lemma fbind_cong[fundef_cong]: "A = B \<Longrightarrow> (\<And>x. x |\<in>| B \<Longrightarrow> f x = g x) \<Longrightarrow> fbind A f = fbind B g"
lars@66264
   465
by transfer force
lars@66264
   466
lars@66264
   467
wenzelm@61585
   468
subsubsection \<open>\<open>fsingleton\<close>\<close>
kuncar@53953
   469
kuncar@53953
   470
lemmas fsingletonE = fsingletonD [elim_format]
kuncar@53953
   471
blanchet@55129
   472
wenzelm@61585
   473
subsubsection \<open>\<open>femepty\<close>\<close>
kuncar@53953
   474
kuncar@53953
   475
lemma fempty_ffilter[simp]: "ffilter (\<lambda>_. False) A = {||}"
kuncar@53953
   476
by transfer auto
kuncar@53953
   477
kuncar@53953
   478
(* FIXME, transferred doesn't work here *)
kuncar@53953
   479
lemma femptyE [elim!]: "a |\<in>| {||} \<Longrightarrow> P"
kuncar@53953
   480
  by simp
kuncar@53953
   481
blanchet@55129
   482
wenzelm@61585
   483
subsubsection \<open>\<open>fset\<close>\<close>
kuncar@53953
   484
kuncar@53963
   485
lemmas fset_simps[simp] = bot_fset.rep_eq finsert.rep_eq
kuncar@53953
   486
hoelzl@63331
   487
lemma finite_fset [simp]:
kuncar@53953
   488
  shows "finite (fset S)"
kuncar@53953
   489
  by transfer simp
kuncar@53953
   490
kuncar@53963
   491
lemmas fset_cong = fset_inject
kuncar@53953
   492
kuncar@53953
   493
lemma filter_fset [simp]:
kuncar@53953
   494
  shows "fset (ffilter P xs) = Collect P \<inter> fset xs"
kuncar@53953
   495
  by transfer auto
kuncar@53953
   496
kuncar@53963
   497
lemma notin_fset: "x |\<notin>| S \<longleftrightarrow> x \<notin> fset S" by (simp add: fmember.rep_eq)
kuncar@53963
   498
kuncar@53963
   499
lemmas inter_fset[simp] = inf_fset.rep_eq
kuncar@53953
   500
kuncar@53963
   501
lemmas union_fset[simp] = sup_fset.rep_eq
kuncar@53953
   502
kuncar@53963
   503
lemmas minus_fset[simp] = minus_fset.rep_eq
kuncar@53953
   504
blanchet@55129
   505
lars@63622
   506
subsubsection \<open>\<open>ffilter\<close>\<close>
kuncar@53953
   507
hoelzl@63331
   508
lemma subset_ffilter:
kuncar@53953
   509
  "ffilter P A |\<subseteq>| ffilter Q A = (\<forall> x. x |\<in>| A \<longrightarrow> P x \<longrightarrow> Q x)"
kuncar@53953
   510
  by transfer auto
kuncar@53953
   511
hoelzl@63331
   512
lemma eq_ffilter:
kuncar@53953
   513
  "(ffilter P A = ffilter Q A) = (\<forall>x. x |\<in>| A \<longrightarrow> P x = Q x)"
kuncar@53953
   514
  by transfer auto
kuncar@53953
   515
kuncar@53964
   516
lemma pfsubset_ffilter:
wenzelm@67091
   517
  "(\<And>x. x |\<in>| A \<Longrightarrow> P x \<Longrightarrow> Q x) \<Longrightarrow> (x |\<in>| A \<and> \<not> P x \<and> Q x) \<Longrightarrow>
kuncar@53953
   518
    ffilter P A |\<subset>| ffilter Q A"
kuncar@53953
   519
  unfolding less_fset_def by (auto simp add: subset_ffilter eq_ffilter)
kuncar@53953
   520
blanchet@55129
   521
lars@63622
   522
subsubsection \<open>\<open>fset_of_list\<close>\<close>
lars@63622
   523
lars@63622
   524
lemma fset_of_list_filter[simp]:
lars@63622
   525
  "fset_of_list (filter P xs) = ffilter P (fset_of_list xs)"
lars@63622
   526
  by transfer (auto simp: Set.filter_def)
lars@63622
   527
lars@63622
   528
lemma fset_of_list_subset[intro]:
lars@63622
   529
  "set xs \<subseteq> set ys \<Longrightarrow> fset_of_list xs |\<subseteq>| fset_of_list ys"
lars@63622
   530
  by transfer simp
lars@63622
   531
lars@63622
   532
lemma fset_of_list_elem: "(x |\<in>| fset_of_list xs) \<longleftrightarrow> (x \<in> set xs)"
lars@63622
   533
  by transfer simp
lars@63622
   534
lars@63622
   535
wenzelm@61585
   536
subsubsection \<open>\<open>finsert\<close>\<close>
kuncar@53953
   537
kuncar@53953
   538
(* FIXME, transferred doesn't work here *)
kuncar@53953
   539
lemma set_finsert:
kuncar@53953
   540
  assumes "x |\<in>| A"
kuncar@53953
   541
  obtains B where "A = finsert x B" and "x |\<notin>| B"
kuncar@53953
   542
using assms by transfer (metis Set.set_insert finite_insert)
kuncar@53953
   543
kuncar@53953
   544
lemma mk_disjoint_finsert: "a |\<in>| A \<Longrightarrow> \<exists>B. A = finsert a B \<and> a |\<notin>| B"
wenzelm@63649
   545
  by (rule exI [where x = "A |-| {|a|}"]) blast
kuncar@53953
   546
lars@66264
   547
lemma finsert_eq_iff:
lars@66264
   548
  assumes "a |\<notin>| A" and "b |\<notin>| B"
lars@66264
   549
  shows "(finsert a A = finsert b B) =
lars@66264
   550
    (if a = b then A = B else \<exists>C. A = finsert b C \<and> b |\<notin>| C \<and> B = finsert a C \<and> a |\<notin>| C)"
lars@66264
   551
  using assms by transfer (force simp: insert_eq_iff)
lars@66264
   552
blanchet@55129
   553
wenzelm@61585
   554
subsubsection \<open>\<open>fimage\<close>\<close>
kuncar@53953
   555
kuncar@53953
   556
lemma subset_fimage_iff: "(B |\<subseteq>| f|`|A) = (\<exists> AA. AA |\<subseteq>| A \<and> B = f|`|AA)"
kuncar@53953
   557
by transfer (metis mem_Collect_eq rev_finite_subset subset_image_iff)
kuncar@53953
   558
blanchet@55129
   559
wenzelm@60500
   560
subsubsection \<open>bounded quantification\<close>
kuncar@53953
   561
kuncar@53953
   562
lemma bex_simps [simp, no_atp]:
hoelzl@63331
   563
  "\<And>A P Q. fBex A (\<lambda>x. P x \<and> Q) = (fBex A P \<and> Q)"
kuncar@53953
   564
  "\<And>A P Q. fBex A (\<lambda>x. P \<and> Q x) = (P \<and> fBex A Q)"
hoelzl@63331
   565
  "\<And>P. fBex {||} P = False"
kuncar@53953
   566
  "\<And>a B P. fBex (finsert a B) P = (P a \<or> fBex B P)"
kuncar@53953
   567
  "\<And>A P f. fBex (f |`| A) P = fBex A (\<lambda>x. P (f x))"
kuncar@53953
   568
  "\<And>A P. (\<not> fBex A P) = fBall A (\<lambda>x. \<not> P x)"
kuncar@53953
   569
by auto
kuncar@53953
   570
kuncar@53953
   571
lemma ball_simps [simp, no_atp]:
kuncar@53953
   572
  "\<And>A P Q. fBall A (\<lambda>x. P x \<or> Q) = (fBall A P \<or> Q)"
kuncar@53953
   573
  "\<And>A P Q. fBall A (\<lambda>x. P \<or> Q x) = (P \<or> fBall A Q)"
kuncar@53953
   574
  "\<And>A P Q. fBall A (\<lambda>x. P \<longrightarrow> Q x) = (P \<longrightarrow> fBall A Q)"
kuncar@53953
   575
  "\<And>A P Q. fBall A (\<lambda>x. P x \<longrightarrow> Q) = (fBex A P \<longrightarrow> Q)"
kuncar@53953
   576
  "\<And>P. fBall {||} P = True"
kuncar@53953
   577
  "\<And>a B P. fBall (finsert a B) P = (P a \<and> fBall B P)"
kuncar@53953
   578
  "\<And>A P f. fBall (f |`| A) P = fBall A (\<lambda>x. P (f x))"
kuncar@53953
   579
  "\<And>A P. (\<not> fBall A P) = fBex A (\<lambda>x. \<not> P x)"
kuncar@53953
   580
by auto
kuncar@53953
   581
kuncar@53953
   582
lemma atomize_fBall:
kuncar@53953
   583
    "(\<And>x. x |\<in>| A ==> P x) == Trueprop (fBall A (\<lambda>x. P x))"
kuncar@53953
   584
apply (simp only: atomize_all atomize_imp)
kuncar@53953
   585
apply (rule equal_intr_rule)
lars@63622
   586
  by (transfer, simp)+
lars@63622
   587
lars@63622
   588
lemma fBall_mono[mono]: "P \<le> Q \<Longrightarrow> fBall S P \<le> fBall S Q"
lars@63622
   589
by auto
lars@63622
   590
lars@68463
   591
lemma fBex_mono[mono]: "P \<le> Q \<Longrightarrow> fBex S P \<le> fBex S Q"
lars@68463
   592
by auto
kuncar@53953
   593
kuncar@53963
   594
end
kuncar@53963
   595
blanchet@55129
   596
wenzelm@61585
   597
subsubsection \<open>\<open>fcard\<close>\<close>
kuncar@53963
   598
kuncar@53964
   599
(* FIXME: improve transferred to handle bounded meta quantification *)
kuncar@53964
   600
kuncar@53963
   601
lemma fcard_fempty:
kuncar@53963
   602
  "fcard {||} = 0"
kuncar@53963
   603
  by transfer (rule card_empty)
kuncar@53963
   604
kuncar@53963
   605
lemma fcard_finsert_disjoint:
kuncar@53963
   606
  "x |\<notin>| A \<Longrightarrow> fcard (finsert x A) = Suc (fcard A)"
kuncar@53963
   607
  by transfer (rule card_insert_disjoint)
kuncar@53963
   608
kuncar@53963
   609
lemma fcard_finsert_if:
kuncar@53963
   610
  "fcard (finsert x A) = (if x |\<in>| A then fcard A else Suc (fcard A))"
kuncar@53963
   611
  by transfer (rule card_insert_if)
kuncar@53963
   612
lars@66265
   613
lemma fcard_0_eq [simp, no_atp]:
kuncar@53963
   614
  "fcard A = 0 \<longleftrightarrow> A = {||}"
kuncar@53963
   615
  by transfer (rule card_0_eq)
kuncar@53963
   616
kuncar@53963
   617
lemma fcard_Suc_fminus1:
kuncar@53963
   618
  "x |\<in>| A \<Longrightarrow> Suc (fcard (A |-| {|x|})) = fcard A"
kuncar@53963
   619
  by transfer (rule card_Suc_Diff1)
kuncar@53963
   620
kuncar@53963
   621
lemma fcard_fminus_fsingleton:
kuncar@53963
   622
  "x |\<in>| A \<Longrightarrow> fcard (A |-| {|x|}) = fcard A - 1"
kuncar@53963
   623
  by transfer (rule card_Diff_singleton)
kuncar@53963
   624
kuncar@53963
   625
lemma fcard_fminus_fsingleton_if:
kuncar@53963
   626
  "fcard (A |-| {|x|}) = (if x |\<in>| A then fcard A - 1 else fcard A)"
kuncar@53963
   627
  by transfer (rule card_Diff_singleton_if)
kuncar@53963
   628
kuncar@53963
   629
lemma fcard_fminus_finsert[simp]:
kuncar@53963
   630
  assumes "a |\<in>| A" and "a |\<notin>| B"
kuncar@53963
   631
  shows "fcard (A |-| finsert a B) = fcard (A |-| B) - 1"
kuncar@53963
   632
using assms by transfer (rule card_Diff_insert)
kuncar@53963
   633
kuncar@53963
   634
lemma fcard_finsert: "fcard (finsert x A) = Suc (fcard (A |-| {|x|}))"
kuncar@53963
   635
by transfer (rule card_insert)
kuncar@53963
   636
kuncar@53963
   637
lemma fcard_finsert_le: "fcard A \<le> fcard (finsert x A)"
kuncar@53963
   638
by transfer (rule card_insert_le)
kuncar@53963
   639
kuncar@53963
   640
lemma fcard_mono:
kuncar@53963
   641
  "A |\<subseteq>| B \<Longrightarrow> fcard A \<le> fcard B"
kuncar@53963
   642
by transfer (rule card_mono)
kuncar@53963
   643
kuncar@53963
   644
lemma fcard_seteq: "A |\<subseteq>| B \<Longrightarrow> fcard B \<le> fcard A \<Longrightarrow> A = B"
kuncar@53963
   645
by transfer (rule card_seteq)
kuncar@53963
   646
kuncar@53963
   647
lemma pfsubset_fcard_mono: "A |\<subset>| B \<Longrightarrow> fcard A < fcard B"
kuncar@53963
   648
by transfer (rule psubset_card_mono)
kuncar@53963
   649
hoelzl@63331
   650
lemma fcard_funion_finter:
kuncar@53963
   651
  "fcard A + fcard B = fcard (A |\<union>| B) + fcard (A |\<inter>| B)"
kuncar@53963
   652
by transfer (rule card_Un_Int)
kuncar@53963
   653
kuncar@53963
   654
lemma fcard_funion_disjoint:
kuncar@53963
   655
  "A |\<inter>| B = {||} \<Longrightarrow> fcard (A |\<union>| B) = fcard A + fcard B"
kuncar@53963
   656
by transfer (rule card_Un_disjoint)
kuncar@53963
   657
kuncar@53963
   658
lemma fcard_funion_fsubset:
kuncar@53963
   659
  "B |\<subseteq>| A \<Longrightarrow> fcard (A |-| B) = fcard A - fcard B"
kuncar@53963
   660
by transfer (rule card_Diff_subset)
kuncar@53963
   661
kuncar@53963
   662
lemma diff_fcard_le_fcard_fminus:
kuncar@53963
   663
  "fcard A - fcard B \<le> fcard(A |-| B)"
kuncar@53963
   664
by transfer (rule diff_card_le_card_Diff)
kuncar@53963
   665
kuncar@53963
   666
lemma fcard_fminus1_less: "x |\<in>| A \<Longrightarrow> fcard (A |-| {|x|}) < fcard A"
kuncar@53963
   667
by transfer (rule card_Diff1_less)
kuncar@53963
   668
kuncar@53963
   669
lemma fcard_fminus2_less:
kuncar@53963
   670
  "x |\<in>| A \<Longrightarrow> y |\<in>| A \<Longrightarrow> fcard (A |-| {|x|} |-| {|y|}) < fcard A"
kuncar@53963
   671
by transfer (rule card_Diff2_less)
kuncar@53963
   672
kuncar@53963
   673
lemma fcard_fminus1_le: "fcard (A |-| {|x|}) \<le> fcard A"
kuncar@53963
   674
by transfer (rule card_Diff1_le)
kuncar@53963
   675
kuncar@53963
   676
lemma fcard_pfsubset: "A |\<subseteq>| B \<Longrightarrow> fcard A < fcard B \<Longrightarrow> A < B"
kuncar@53963
   677
by transfer (rule card_psubset)
kuncar@53963
   678
blanchet@55129
   679
lars@68463
   680
subsubsection \<open>\<open>sorted_list_of_fset\<close>\<close>
lars@68463
   681
lars@68463
   682
lemma sorted_list_of_fset_simps[simp]:
lars@68463
   683
  "set (sorted_list_of_fset S) = fset S"
lars@68463
   684
  "fset_of_list (sorted_list_of_fset S) = S"
lars@68463
   685
by (transfer, simp)+
lars@68463
   686
lars@68463
   687
wenzelm@61585
   688
subsubsection \<open>\<open>ffold\<close>\<close>
kuncar@53963
   689
kuncar@53963
   690
(* FIXME: improve transferred to handle bounded meta quantification *)
kuncar@53963
   691
kuncar@53963
   692
context comp_fun_commute
kuncar@53963
   693
begin
kuncar@53963
   694
  lemmas ffold_empty[simp] = fold_empty[Transfer.transferred]
kuncar@53963
   695
kuncar@53963
   696
  lemma ffold_finsert [simp]:
kuncar@53963
   697
    assumes "x |\<notin>| A"
kuncar@53963
   698
    shows "ffold f z (finsert x A) = f x (ffold f z A)"
kuncar@53963
   699
    using assms by (transfer fixing: f) (rule fold_insert)
kuncar@53963
   700
kuncar@53963
   701
  lemma ffold_fun_left_comm:
kuncar@53963
   702
    "f x (ffold f z A) = ffold f (f x z) A"
kuncar@53963
   703
    by (transfer fixing: f) (rule fold_fun_left_comm)
kuncar@53963
   704
kuncar@53963
   705
  lemma ffold_finsert2:
blanchet@56646
   706
    "x |\<notin>| A \<Longrightarrow> ffold f z (finsert x A) = ffold f (f x z) A"
kuncar@53963
   707
    by (transfer fixing: f) (rule fold_insert2)
kuncar@53963
   708
kuncar@53963
   709
  lemma ffold_rec:
kuncar@53963
   710
    assumes "x |\<in>| A"
kuncar@53963
   711
    shows "ffold f z A = f x (ffold f z (A |-| {|x|}))"
kuncar@53963
   712
    using assms by (transfer fixing: f) (rule fold_rec)
hoelzl@63331
   713
kuncar@53963
   714
  lemma ffold_finsert_fremove:
kuncar@53963
   715
    "ffold f z (finsert x A) = f x (ffold f z (A |-| {|x|}))"
kuncar@53963
   716
     by (transfer fixing: f) (rule fold_insert_remove)
kuncar@53963
   717
end
kuncar@53963
   718
kuncar@53963
   719
lemma ffold_fimage:
kuncar@53963
   720
  assumes "inj_on g (fset A)"
kuncar@53963
   721
  shows "ffold f z (g |`| A) = ffold (f \<circ> g) z A"
kuncar@53963
   722
using assms by transfer' (rule fold_image)
kuncar@53963
   723
kuncar@53963
   724
lemma ffold_cong:
kuncar@53963
   725
  assumes "comp_fun_commute f" "comp_fun_commute g"
kuncar@53963
   726
  "\<And>x. x |\<in>| A \<Longrightarrow> f x = g x"
kuncar@53963
   727
    and "s = t" and "A = B"
kuncar@53963
   728
  shows "ffold f s A = ffold g t B"
kuncar@53963
   729
using assms by transfer (metis Finite_Set.fold_cong)
kuncar@53963
   730
kuncar@53963
   731
context comp_fun_idem
kuncar@53963
   732
begin
kuncar@53963
   733
kuncar@53963
   734
  lemma ffold_finsert_idem:
blanchet@56646
   735
    "ffold f z (finsert x A) = f x (ffold f z A)"
kuncar@53963
   736
    by (transfer fixing: f) (rule fold_insert_idem)
hoelzl@63331
   737
kuncar@53963
   738
  declare ffold_finsert [simp del] ffold_finsert_idem [simp]
hoelzl@63331
   739
kuncar@53963
   740
  lemma ffold_finsert_idem2:
kuncar@53963
   741
    "ffold f z (finsert x A) = ffold f (f x z) A"
kuncar@53963
   742
    by (transfer fixing: f) (rule fold_insert_idem2)
kuncar@53963
   743
kuncar@53963
   744
end
kuncar@53963
   745
lars@66292
   746
lars@66292
   747
subsubsection \<open>Group operations\<close>
lars@66292
   748
lars@66292
   749
locale comm_monoid_fset = comm_monoid
lars@66292
   750
begin
lars@66292
   751
lars@66292
   752
sublocale set: comm_monoid_set ..
lars@66292
   753
lars@66292
   754
lift_definition F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b fset \<Rightarrow> 'a" is set.F .
lars@66292
   755
lars@66292
   756
lemmas cong[fundef_cong] = set.cong[Transfer.transferred]
lars@66261
   757
lars@66292
   758
lemma strong_cong[cong]:
lars@66261
   759
  assumes "A = B" "\<And>x. x |\<in>| B =simp=> g x = h x"
lars@66292
   760
  shows "F g A = F h B"
lars@66292
   761
using assms unfolding simp_implies_def by (auto cong: cong)
lars@66292
   762
lars@66292
   763
end
lars@66292
   764
lars@66292
   765
context comm_monoid_add begin
lars@66292
   766
lars@66292
   767
sublocale fsum: comm_monoid_fset plus 0
ballarin@67764
   768
  rewrites "comm_monoid_set.F plus 0 = sum"
lars@66292
   769
  defines fsum = fsum.F
lars@66292
   770
proof -
nipkow@67399
   771
  show "comm_monoid_fset (+) 0" by standard
lars@66292
   772
nipkow@67399
   773
  show "comm_monoid_set.F (+) 0 = sum" unfolding sum_def ..
lars@66292
   774
qed
lars@66292
   775
lars@66292
   776
end
lars@66261
   777
blanchet@55129
   778
lars@66264
   779
subsubsection \<open>Semilattice operations\<close>
lars@66264
   780
lars@66292
   781
locale semilattice_fset = semilattice
lars@66292
   782
begin
lars@66292
   783
lars@66292
   784
sublocale set: semilattice_set ..
lars@66292
   785
lars@66292
   786
lift_definition F :: "'a fset \<Rightarrow> 'a" is set.F .
lars@66292
   787
lars@66292
   788
lemma eq_fold: "F (finsert x A) = ffold f x A"
lars@66292
   789
  by transfer (rule set.eq_fold)
lars@66292
   790
lars@66292
   791
lemma singleton [simp]: "F {|x|} = x"
lars@66292
   792
  by transfer (rule set.singleton)
lars@66292
   793
lars@66292
   794
lemma insert_not_elem: "x |\<notin>| A \<Longrightarrow> A \<noteq> {||} \<Longrightarrow> F (finsert x A) = x \<^bold>* F A"
lars@66292
   795
  by transfer (rule set.insert_not_elem)
lars@66292
   796
lars@66292
   797
lemma in_idem: "x |\<in>| A \<Longrightarrow> x \<^bold>* F A = F A"
lars@66292
   798
  by transfer (rule set.in_idem)
lars@66292
   799
lars@66292
   800
lemma insert [simp]: "A \<noteq> {||} \<Longrightarrow> F (finsert x A) = x \<^bold>* F A"
lars@66292
   801
  by transfer (rule set.insert)
lars@66292
   802
lars@66292
   803
end
lars@66292
   804
lars@66292
   805
locale semilattice_order_fset = binary?: semilattice_order + semilattice_fset
lars@66292
   806
begin
lars@66264
   807
lars@66292
   808
end
lars@66292
   809
lars@66292
   810
lars@66292
   811
context linorder begin
lars@66292
   812
lars@66292
   813
sublocale fMin: semilattice_order_fset min less_eq less
ballarin@67764
   814
  rewrites "semilattice_set.F min = Min"
lars@66292
   815
  defines fMin = fMin.F
lars@66292
   816
proof -
nipkow@67399
   817
  show "semilattice_order_fset min (\<le>) (<)" by standard
lars@66292
   818
lars@66292
   819
  show "semilattice_set.F min = Min" unfolding Min_def ..
lars@66292
   820
qed
lars@66292
   821
lars@66292
   822
sublocale fMax: semilattice_order_fset max greater_eq greater
ballarin@67764
   823
  rewrites "semilattice_set.F max = Max"
lars@66292
   824
  defines fMax = fMax.F
lars@66292
   825
proof -
nipkow@67399
   826
  show "semilattice_order_fset max (\<ge>) (>)"
lars@66292
   827
    by standard
lars@66292
   828
lars@66292
   829
  show "semilattice_set.F max = Max"
lars@66292
   830
    unfolding Max_def ..
lars@66292
   831
qed
lars@66292
   832
lars@66292
   833
end
lars@66264
   834
lars@66264
   835
lemma mono_fMax_commute: "mono f \<Longrightarrow> A \<noteq> {||} \<Longrightarrow> f (fMax A) = fMax (f |`| A)"
lars@66292
   836
  by transfer (rule mono_Max_commute)
lars@66264
   837
lars@66264
   838
lemma mono_fMin_commute: "mono f \<Longrightarrow> A \<noteq> {||} \<Longrightarrow> f (fMin A) = fMin (f |`| A)"
lars@66292
   839
  by transfer (rule mono_Min_commute)
lars@66264
   840
lars@66264
   841
lemma fMax_in[simp]: "A \<noteq> {||} \<Longrightarrow> fMax A |\<in>| A"
lars@66264
   842
  by transfer (rule Max_in)
lars@66264
   843
lars@66264
   844
lemma fMin_in[simp]: "A \<noteq> {||} \<Longrightarrow> fMin A |\<in>| A"
lars@66264
   845
  by transfer (rule Min_in)
lars@66264
   846
lars@66264
   847
lemma fMax_ge[simp]: "x |\<in>| A \<Longrightarrow> x \<le> fMax A"
lars@66264
   848
  by transfer (rule Max_ge)
lars@66264
   849
lars@66264
   850
lemma fMin_le[simp]: "x |\<in>| A \<Longrightarrow> fMin A \<le> x"
lars@66264
   851
  by transfer (rule Min_le)
lars@66264
   852
lars@66264
   853
lemma fMax_eqI: "(\<And>y. y |\<in>| A \<Longrightarrow> y \<le> x) \<Longrightarrow> x |\<in>| A \<Longrightarrow> fMax A = x"
lars@66264
   854
  by transfer (rule Max_eqI)
lars@66264
   855
lars@66264
   856
lemma fMin_eqI: "(\<And>y. y |\<in>| A \<Longrightarrow> x \<le> y) \<Longrightarrow> x |\<in>| A \<Longrightarrow> fMin A = x"
lars@66264
   857
  by transfer (rule Min_eqI)
lars@66264
   858
lars@66264
   859
lemma fMax_finsert[simp]: "fMax (finsert x A) = (if A = {||} then x else max x (fMax A))"
lars@66264
   860
  by transfer simp
lars@66264
   861
lars@66264
   862
lemma fMin_finsert[simp]: "fMin (finsert x A) = (if A = {||} then x else min x (fMin A))"
lars@66264
   863
  by transfer simp
lars@66264
   864
lars@66264
   865
context linorder begin
lars@66264
   866
lars@66264
   867
lemma fset_linorder_max_induct[case_names fempty finsert]:
lars@66264
   868
  assumes "P {||}"
lars@66264
   869
  and     "\<And>x S. \<lbrakk>\<forall>y. y |\<in>| S \<longrightarrow> y < x; P S\<rbrakk> \<Longrightarrow> P (finsert x S)"
lars@66264
   870
  shows "P S"
lars@66264
   871
proof -
lars@66264
   872
  (* FIXME transfer and right_total vs. bi_total *)
lars@66264
   873
  note Domainp_forall_transfer[transfer_rule]
lars@66264
   874
  show ?thesis
lars@66264
   875
  using assms by (transfer fixing: less) (auto intro: finite_linorder_max_induct)
lars@66264
   876
qed
lars@66264
   877
lars@66264
   878
lemma fset_linorder_min_induct[case_names fempty finsert]:
lars@66264
   879
  assumes "P {||}"
lars@66264
   880
  and     "\<And>x S. \<lbrakk>\<forall>y. y |\<in>| S \<longrightarrow> y > x; P S\<rbrakk> \<Longrightarrow> P (finsert x S)"
lars@66264
   881
  shows "P S"
lars@66264
   882
proof -
lars@66264
   883
  (* FIXME transfer and right_total vs. bi_total *)
lars@66264
   884
  note Domainp_forall_transfer[transfer_rule]
lars@66264
   885
  show ?thesis
lars@66264
   886
  using assms by (transfer fixing: less) (auto intro: finite_linorder_min_induct)
lars@66264
   887
qed
lars@66264
   888
lars@66264
   889
end
lars@66264
   890
lars@66264
   891
wenzelm@60500
   892
subsection \<open>Choice in fsets\<close>
kuncar@53953
   893
hoelzl@63331
   894
lemma fset_choice:
kuncar@53953
   895
  assumes "\<forall>x. x |\<in>| A \<longrightarrow> (\<exists>y. P x y)"
kuncar@53953
   896
  shows "\<exists>f. \<forall>x. x |\<in>| A \<longrightarrow> P x (f x)"
kuncar@53953
   897
  using assms by transfer metis
kuncar@53953
   898
blanchet@55129
   899
wenzelm@60500
   900
subsection \<open>Induction and Cases rules for fsets\<close>
kuncar@53953
   901
kuncar@53953
   902
lemma fset_exhaust [case_names empty insert, cases type: fset]:
hoelzl@63331
   903
  assumes fempty_case: "S = {||} \<Longrightarrow> P"
kuncar@53953
   904
  and     finsert_case: "\<And>x S'. S = finsert x S' \<Longrightarrow> P"
kuncar@53953
   905
  shows "P"
kuncar@53953
   906
  using assms by transfer blast
kuncar@53953
   907
kuncar@53953
   908
lemma fset_induct [case_names empty insert]:
kuncar@53953
   909
  assumes fempty_case: "P {||}"
kuncar@53953
   910
  and     finsert_case: "\<And>x S. P S \<Longrightarrow> P (finsert x S)"
kuncar@53953
   911
  shows "P S"
kuncar@53953
   912
proof -
kuncar@53953
   913
  (* FIXME transfer and right_total vs. bi_total *)
kuncar@53953
   914
  note Domainp_forall_transfer[transfer_rule]
kuncar@53953
   915
  show ?thesis
kuncar@53953
   916
  using assms by transfer (auto intro: finite_induct)
kuncar@53953
   917
qed
kuncar@53953
   918
kuncar@53953
   919
lemma fset_induct_stronger [case_names empty insert, induct type: fset]:
kuncar@53953
   920
  assumes empty_fset_case: "P {||}"
kuncar@53953
   921
  and     insert_fset_case: "\<And>x S. \<lbrakk>x |\<notin>| S; P S\<rbrakk> \<Longrightarrow> P (finsert x S)"
kuncar@53953
   922
  shows "P S"
kuncar@53953
   923
proof -
kuncar@53953
   924
  (* FIXME transfer and right_total vs. bi_total *)
kuncar@53953
   925
  note Domainp_forall_transfer[transfer_rule]
kuncar@53953
   926
  show ?thesis
kuncar@53953
   927
  using assms by transfer (auto intro: finite_induct)
kuncar@53953
   928
qed
kuncar@53953
   929
kuncar@53953
   930
lemma fset_card_induct:
kuncar@53953
   931
  assumes empty_fset_case: "P {||}"
kuncar@53953
   932
  and     card_fset_Suc_case: "\<And>S T. Suc (fcard S) = (fcard T) \<Longrightarrow> P S \<Longrightarrow> P T"
kuncar@53953
   933
  shows "P S"
kuncar@53953
   934
proof (induct S)
kuncar@53953
   935
  case empty
kuncar@53953
   936
  show "P {||}" by (rule empty_fset_case)
kuncar@53953
   937
next
kuncar@53953
   938
  case (insert x S)
kuncar@53953
   939
  have h: "P S" by fact
kuncar@53953
   940
  have "x |\<notin>| S" by fact
hoelzl@63331
   941
  then have "Suc (fcard S) = fcard (finsert x S)"
kuncar@53953
   942
    by transfer auto
hoelzl@63331
   943
  then show "P (finsert x S)"
kuncar@53953
   944
    using h card_fset_Suc_case by simp
kuncar@53953
   945
qed
kuncar@53953
   946
kuncar@53953
   947
lemma fset_strong_cases:
kuncar@53953
   948
  obtains "xs = {||}"
kuncar@53953
   949
    | ys x where "x |\<notin>| ys" and "xs = finsert x ys"
kuncar@53953
   950
by transfer blast
kuncar@53953
   951
kuncar@53953
   952
lemma fset_induct2:
kuncar@53953
   953
  "P {||} {||} \<Longrightarrow>
kuncar@53953
   954
  (\<And>x xs. x |\<notin>| xs \<Longrightarrow> P (finsert x xs) {||}) \<Longrightarrow>
kuncar@53953
   955
  (\<And>y ys. y |\<notin>| ys \<Longrightarrow> P {||} (finsert y ys)) \<Longrightarrow>
kuncar@53953
   956
  (\<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
   957
  P xsa ysa"
kuncar@53953
   958
  apply (induct xsa arbitrary: ysa)
kuncar@53953
   959
  apply (induct_tac x rule: fset_induct_stronger)
kuncar@53953
   960
  apply simp_all
kuncar@53953
   961
  apply (induct_tac xa rule: fset_induct_stronger)
kuncar@53953
   962
  apply simp_all
kuncar@53953
   963
  done
kuncar@53953
   964
blanchet@55129
   965
wenzelm@60500
   966
subsection \<open>Setup for Lifting/Transfer\<close>
kuncar@53953
   967
wenzelm@60500
   968
subsubsection \<open>Relator and predicator properties\<close>
kuncar@53953
   969
blanchet@55938
   970
lift_definition rel_fset :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'b fset \<Rightarrow> bool" is rel_set
blanchet@55938
   971
parametric rel_set_transfer .
kuncar@53953
   972
hoelzl@63331
   973
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
   974
  \<and> (\<forall>y. \<exists>x. y|\<in>|B \<longrightarrow> x|\<in>|A \<and> R x y))"
kuncar@53953
   975
apply (rule ext)+
kuncar@53953
   976
apply transfer'
hoelzl@63331
   977
apply (subst rel_set_def[unfolded fun_eq_iff])
kuncar@53953
   978
by blast
kuncar@53953
   979
blanchet@55938
   980
lemma finite_rel_set:
kuncar@53953
   981
  assumes fin: "finite X" "finite Z"
blanchet@55938
   982
  assumes R_S: "rel_set (R OO S) X Z"
blanchet@55938
   983
  shows "\<exists>Y. finite Y \<and> rel_set R X Y \<and> rel_set S Y Z"
kuncar@53953
   984
proof -
kuncar@53953
   985
  obtain f where f: "\<forall>x\<in>X. R x (f x) \<and> (\<exists>z\<in>Z. S (f x) z)"
kuncar@53953
   986
  apply atomize_elim
kuncar@53953
   987
  apply (subst bchoice_iff[symmetric])
blanchet@55938
   988
  using R_S[unfolded rel_set_def OO_def] by blast
hoelzl@63331
   989
blanchet@56646
   990
  obtain g where g: "\<forall>z\<in>Z. S (g z) z \<and> (\<exists>x\<in>X. R x (g z))"
kuncar@53953
   991
  apply atomize_elim
kuncar@53953
   992
  apply (subst bchoice_iff[symmetric])
blanchet@55938
   993
  using R_S[unfolded rel_set_def OO_def] by blast
hoelzl@63331
   994
kuncar@53953
   995
  let ?Y = "f ` X \<union> g ` Z"
kuncar@53953
   996
  have "finite ?Y" by (simp add: fin)
blanchet@55938
   997
  moreover have "rel_set R X ?Y"
blanchet@55938
   998
    unfolding rel_set_def
kuncar@53953
   999
    using f g by clarsimp blast
blanchet@55938
  1000
  moreover have "rel_set S ?Y Z"
blanchet@55938
  1001
    unfolding rel_set_def
kuncar@53953
  1002
    using f g by clarsimp blast
kuncar@53953
  1003
  ultimately show ?thesis by metis
kuncar@53953
  1004
qed
kuncar@53953
  1005
wenzelm@60500
  1006
subsubsection \<open>Transfer rules for the Transfer package\<close>
kuncar@53953
  1007
wenzelm@60500
  1008
text \<open>Unconditional transfer rules\<close>
kuncar@53953
  1009
wenzelm@63343
  1010
context includes lifting_syntax
kuncar@53963
  1011
begin
kuncar@53963
  1012
kuncar@53953
  1013
lemmas fempty_transfer [transfer_rule] = empty_transfer[Transfer.transferred]
kuncar@53953
  1014
kuncar@53953
  1015
lemma finsert_transfer [transfer_rule]:
blanchet@55933
  1016
  "(A ===> rel_fset A ===> rel_fset A) finsert finsert"
blanchet@55945
  1017
  unfolding rel_fun_def rel_fset_alt_def by blast
kuncar@53953
  1018
kuncar@53953
  1019
lemma funion_transfer [transfer_rule]:
blanchet@55933
  1020
  "(rel_fset A ===> rel_fset A ===> rel_fset A) funion funion"
blanchet@55945
  1021
  unfolding rel_fun_def rel_fset_alt_def by blast
kuncar@53953
  1022
kuncar@53953
  1023
lemma ffUnion_transfer [transfer_rule]:
blanchet@55933
  1024
  "(rel_fset (rel_fset A) ===> rel_fset A) ffUnion ffUnion"
blanchet@55945
  1025
  unfolding rel_fun_def rel_fset_alt_def by transfer (simp, fast)
kuncar@53953
  1026
kuncar@53953
  1027
lemma fimage_transfer [transfer_rule]:
blanchet@55933
  1028
  "((A ===> B) ===> rel_fset A ===> rel_fset B) fimage fimage"
blanchet@55945
  1029
  unfolding rel_fun_def rel_fset_alt_def by simp blast
kuncar@53953
  1030
kuncar@53953
  1031
lemma fBall_transfer [transfer_rule]:
nipkow@67399
  1032
  "(rel_fset A ===> (A ===> (=)) ===> (=)) fBall fBall"
blanchet@55945
  1033
  unfolding rel_fset_alt_def rel_fun_def by blast
kuncar@53953
  1034
kuncar@53953
  1035
lemma fBex_transfer [transfer_rule]:
nipkow@67399
  1036
  "(rel_fset A ===> (A ===> (=)) ===> (=)) fBex fBex"
blanchet@55945
  1037
  unfolding rel_fset_alt_def rel_fun_def by blast
kuncar@53953
  1038
kuncar@53953
  1039
(* FIXME transfer doesn't work here *)
kuncar@53953
  1040
lemma fPow_transfer [transfer_rule]:
blanchet@55933
  1041
  "(rel_fset A ===> rel_fset (rel_fset A)) fPow fPow"
blanchet@55945
  1042
  unfolding rel_fun_def
blanchet@55945
  1043
  using Pow_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred]
kuncar@53953
  1044
  by blast
kuncar@53953
  1045
blanchet@55933
  1046
lemma rel_fset_transfer [transfer_rule]:
nipkow@67399
  1047
  "((A ===> B ===> (=)) ===> rel_fset A ===> rel_fset B ===> (=))
blanchet@55933
  1048
    rel_fset rel_fset"
blanchet@55945
  1049
  unfolding rel_fun_def
blanchet@55945
  1050
  using rel_set_transfer[unfolded rel_fun_def,rule_format, Transfer.transferred, where A = A and B = B]
kuncar@53953
  1051
  by simp
kuncar@53953
  1052
kuncar@53953
  1053
lemma bind_transfer [transfer_rule]:
blanchet@55933
  1054
  "(rel_fset A ===> (A ===> rel_fset B) ===> rel_fset B) fbind fbind"
wenzelm@63092
  1055
  unfolding rel_fun_def
blanchet@55945
  1056
  using bind_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
  1057
wenzelm@60500
  1058
text \<open>Rules requiring bi-unique, bi-total or right-total relations\<close>
kuncar@53953
  1059
kuncar@53953
  1060
lemma fmember_transfer [transfer_rule]:
kuncar@53953
  1061
  assumes "bi_unique A"
nipkow@67399
  1062
  shows "(A ===> rel_fset A ===> (=)) (|\<in>|) (|\<in>|)"
blanchet@55945
  1063
  using assms unfolding rel_fun_def rel_fset_alt_def bi_unique_def by metis
kuncar@53953
  1064
kuncar@53953
  1065
lemma finter_transfer [transfer_rule]:
kuncar@53953
  1066
  assumes "bi_unique A"
blanchet@55933
  1067
  shows "(rel_fset A ===> rel_fset A ===> rel_fset A) finter finter"
blanchet@55945
  1068
  using assms unfolding rel_fun_def
blanchet@55945
  1069
  using inter_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
  1070
kuncar@53963
  1071
lemma fminus_transfer [transfer_rule]:
kuncar@53953
  1072
  assumes "bi_unique A"
nipkow@67399
  1073
  shows "(rel_fset A ===> rel_fset A ===> rel_fset A) (|-|) (|-|)"
blanchet@55945
  1074
  using assms unfolding rel_fun_def
blanchet@55945
  1075
  using Diff_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
  1076
kuncar@53953
  1077
lemma fsubset_transfer [transfer_rule]:
kuncar@53953
  1078
  assumes "bi_unique A"
nipkow@67399
  1079
  shows "(rel_fset A ===> rel_fset A ===> (=)) (|\<subseteq>|) (|\<subseteq>|)"
blanchet@55945
  1080
  using assms unfolding rel_fun_def
blanchet@55945
  1081
  using subset_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
  1082
kuncar@53953
  1083
lemma fSup_transfer [transfer_rule]:
blanchet@55938
  1084
  "bi_unique A \<Longrightarrow> (rel_set (rel_fset A) ===> rel_fset A) Sup Sup"
wenzelm@63092
  1085
  unfolding rel_fun_def
kuncar@53953
  1086
  apply clarify
kuncar@53953
  1087
  apply transfer'
blanchet@55945
  1088
  using Sup_fset_transfer[unfolded rel_fun_def] by blast
kuncar@53953
  1089
kuncar@53953
  1090
(* FIXME: add right_total_fInf_transfer *)
kuncar@53953
  1091
kuncar@53953
  1092
lemma fInf_transfer [transfer_rule]:
kuncar@53953
  1093
  assumes "bi_unique A" and "bi_total A"
blanchet@55938
  1094
  shows "(rel_set (rel_fset A) ===> rel_fset A) Inf Inf"
blanchet@55945
  1095
  using assms unfolding rel_fun_def
kuncar@53953
  1096
  apply clarify
kuncar@53953
  1097
  apply transfer'
blanchet@55945
  1098
  using Inf_fset_transfer[unfolded rel_fun_def] by blast
kuncar@53953
  1099
kuncar@53953
  1100
lemma ffilter_transfer [transfer_rule]:
kuncar@53953
  1101
  assumes "bi_unique A"
nipkow@67399
  1102
  shows "((A ===> (=)) ===> rel_fset A ===> rel_fset A) ffilter ffilter"
blanchet@55945
  1103
  using assms unfolding rel_fun_def
blanchet@55945
  1104
  using Lifting_Set.filter_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
  1105
kuncar@53953
  1106
lemma card_transfer [transfer_rule]:
nipkow@67399
  1107
  "bi_unique A \<Longrightarrow> (rel_fset A ===> (=)) fcard fcard"
wenzelm@63092
  1108
  unfolding rel_fun_def
blanchet@55945
  1109
  using card_transfer[unfolded rel_fun_def, rule_format, Transfer.transferred] by blast
kuncar@53953
  1110
kuncar@53953
  1111
end
kuncar@53953
  1112
kuncar@53953
  1113
lifting_update fset.lifting
kuncar@53953
  1114
lifting_forget fset.lifting
kuncar@53953
  1115
blanchet@55129
  1116
wenzelm@60500
  1117
subsection \<open>BNF setup\<close>
blanchet@55129
  1118
blanchet@55129
  1119
context
blanchet@55129
  1120
includes fset.lifting
blanchet@55129
  1121
begin
blanchet@55129
  1122
blanchet@55933
  1123
lemma rel_fset_alt:
blanchet@55933
  1124
  "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
  1125
by transfer (simp add: rel_set_def)
blanchet@55129
  1126
blanchet@55129
  1127
lemma fset_to_fset: "finite A \<Longrightarrow> fset (the_inv fset A) = A"
blanchet@55129
  1128
apply (rule f_the_inv_into_f[unfolded inj_on_def])
blanchet@55129
  1129
apply (simp add: fset_inject)
blanchet@55129
  1130
apply (rule range_eqI Abs_fset_inverse[symmetric] CollectI)+
blanchet@55129
  1131
.
blanchet@55129
  1132
blanchet@55933
  1133
lemma rel_fset_aux:
blanchet@55129
  1134
"(\<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
  1135
 ((BNF_Def.Grp {a. fset a \<subseteq> {(a, b). R a b}} (fimage fst))\<inverse>\<inverse> OO
blanchet@57398
  1136
  BNF_Def.Grp {a. fset a \<subseteq> {(a, b). R a b}} (fimage snd)) a b" (is "?L = ?R")
blanchet@55129
  1137
proof
blanchet@55129
  1138
  assume ?L
wenzelm@63040
  1139
  define R' where "R' =
wenzelm@63040
  1140
    the_inv fset (Collect (case_prod R) \<inter> (fset a \<times> fset b))" (is "_ = the_inv fset ?L'")
blanchet@55129
  1141
  have "finite ?L'" by (intro finite_Int[OF disjI2] finite_cartesian_product) (transfer, simp)+
blanchet@55129
  1142
  hence *: "fset R' = ?L'" unfolding R'_def by (intro fset_to_fset)
blanchet@55129
  1143
  show ?R unfolding Grp_def relcompp.simps conversep.simps
blanchet@55414
  1144
  proof (intro CollectI case_prodI exI[of _ a] exI[of _ b] exI[of _ R'] conjI refl)
wenzelm@60500
  1145
    from * show "a = fimage fst R'" using conjunct1[OF \<open>?L\<close>]
blanchet@55129
  1146
      by (transfer, auto simp add: image_def Int_def split: prod.splits)
wenzelm@60500
  1147
    from * show "b = fimage snd R'" using conjunct2[OF \<open>?L\<close>]
blanchet@55129
  1148
      by (transfer, auto simp add: image_def Int_def split: prod.splits)
blanchet@55129
  1149
  qed (auto simp add: *)
blanchet@55129
  1150
next
blanchet@55129
  1151
  assume ?R thus ?L unfolding Grp_def relcompp.simps conversep.simps
blanchet@55129
  1152
  apply (simp add: subset_eq Ball_def)
blanchet@55129
  1153
  apply (rule conjI)
blanchet@55129
  1154
  apply (transfer, clarsimp, metis snd_conv)
blanchet@55129
  1155
  by (transfer, clarsimp, metis fst_conv)
blanchet@55129
  1156
qed
blanchet@55129
  1157
blanchet@55129
  1158
bnf "'a fset"
blanchet@55129
  1159
  map: fimage
hoelzl@63331
  1160
  sets: fset
blanchet@55129
  1161
  bd: natLeq
blanchet@55129
  1162
  wits: "{||}"
blanchet@55933
  1163
  rel: rel_fset
blanchet@55129
  1164
apply -
blanchet@55129
  1165
          apply transfer' apply simp
blanchet@55129
  1166
         apply transfer' apply force
blanchet@55129
  1167
        apply transfer apply force
blanchet@55129
  1168
       apply transfer' apply force
blanchet@55129
  1169
      apply (rule natLeq_card_order)
blanchet@55129
  1170
     apply (rule natLeq_cinfinite)
blanchet@55129
  1171
    apply transfer apply (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq)
blanchet@55933
  1172
   apply (fastforce simp: rel_fset_alt)
traytel@62324
  1173
 apply (simp add: Grp_def relcompp.simps conversep.simps fun_eq_iff rel_fset_alt
hoelzl@63331
  1174
   rel_fset_aux[unfolded OO_Grp_alt])
blanchet@55129
  1175
apply transfer apply simp
blanchet@55129
  1176
done
blanchet@55129
  1177
blanchet@55938
  1178
lemma rel_fset_fset: "rel_set \<chi> (fset A1) (fset A2) = rel_fset \<chi> A1 A2"
blanchet@55129
  1179
  by transfer (rule refl)
blanchet@55129
  1180
kuncar@53953
  1181
end
blanchet@55129
  1182
blanchet@55129
  1183
lemmas [simp] = fset.map_comp fset.map_id fset.set_map
blanchet@55129
  1184
blanchet@55129
  1185
wenzelm@60500
  1186
subsection \<open>Size setup\<close>
blanchet@56646
  1187
blanchet@56646
  1188
context includes fset.lifting begin
nipkow@64267
  1189
lift_definition size_fset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a fset \<Rightarrow> nat" is "\<lambda>f. sum (Suc \<circ> f)" .
blanchet@56646
  1190
end
blanchet@56646
  1191
blanchet@56646
  1192
instantiation fset :: (type) size begin
blanchet@56646
  1193
definition size_fset where
blanchet@56646
  1194
  size_fset_overloaded_def: "size_fset = FSet.size_fset (\<lambda>_. 0)"
blanchet@56646
  1195
instance ..
blanchet@56646
  1196
end
blanchet@56646
  1197
blanchet@56646
  1198
lemmas size_fset_simps[simp] =
blanchet@56646
  1199
  size_fset_def[THEN meta_eq_to_obj_eq, THEN fun_cong, THEN fun_cong,
blanchet@56646
  1200
    unfolded map_fun_def comp_def id_apply]
blanchet@56646
  1201
blanchet@56646
  1202
lemmas size_fset_overloaded_simps[simp] =
blanchet@56646
  1203
  size_fset_simps[of "\<lambda>_. 0", unfolded add_0_left add_0_right,
blanchet@56646
  1204
    folded size_fset_overloaded_def]
blanchet@56646
  1205
blanchet@56646
  1206
lemma fset_size_o_map: "inj f \<Longrightarrow> size_fset g \<circ> fimage f = size_fset (g \<circ> f)"
kuncar@60228
  1207
  apply (subst fun_eq_iff)
nipkow@64267
  1208
  including fset.lifting by transfer (auto intro: sum.reindex_cong subset_inj_on)
hoelzl@63331
  1209
wenzelm@60500
  1210
setup \<open>
blanchet@56651
  1211
BNF_LFP_Size.register_size_global @{type_name fset} @{const_name size_fset}
blanchet@62082
  1212
  @{thm size_fset_overloaded_def} @{thms size_fset_simps size_fset_overloaded_simps}
blanchet@62082
  1213
  @{thms fset_size_o_map}
wenzelm@60500
  1214
\<close>
blanchet@56646
  1215
kuncar@60228
  1216
lifting_update fset.lifting
kuncar@60228
  1217
lifting_forget fset.lifting
blanchet@56646
  1218
wenzelm@60500
  1219
subsection \<open>Advanced relator customization\<close>
blanchet@55129
  1220
wenzelm@67408
  1221
text \<open>Set vs. sum relators:\<close>
blanchet@55129
  1222
hoelzl@63331
  1223
lemma rel_set_rel_sum[simp]:
hoelzl@63331
  1224
"rel_set (rel_sum \<chi> \<phi>) A1 A2 \<longleftrightarrow>
blanchet@55938
  1225
 rel_set \<chi> (Inl -` A1) (Inl -` A2) \<and> rel_set \<phi> (Inr -` A1) (Inr -` A2)"
blanchet@55129
  1226
(is "?L \<longleftrightarrow> ?Rl \<and> ?Rr")
blanchet@55129
  1227
proof safe
blanchet@55129
  1228
  assume L: "?L"
blanchet@55938
  1229
  show ?Rl unfolding rel_set_def Bex_def vimage_eq proof safe
blanchet@55129
  1230
    fix l1 assume "Inl l1 \<in> A1"
blanchet@55943
  1231
    then obtain a2 where a2: "a2 \<in> A2" and "rel_sum \<chi> \<phi> (Inl l1) a2"
blanchet@55938
  1232
    using L unfolding rel_set_def by auto
blanchet@55129
  1233
    then obtain l2 where "a2 = Inl l2 \<and> \<chi> l1 l2" by (cases a2, auto)
blanchet@55129
  1234
    thus "\<exists> l2. Inl l2 \<in> A2 \<and> \<chi> l1 l2" using a2 by auto
blanchet@55129
  1235
  next
blanchet@55129
  1236
    fix l2 assume "Inl l2 \<in> A2"
blanchet@55943
  1237
    then obtain a1 where a1: "a1 \<in> A1" and "rel_sum \<chi> \<phi> a1 (Inl l2)"
blanchet@55938
  1238
    using L unfolding rel_set_def by auto
blanchet@55129
  1239
    then obtain l1 where "a1 = Inl l1 \<and> \<chi> l1 l2" by (cases a1, auto)
blanchet@55129
  1240
    thus "\<exists> l1. Inl l1 \<in> A1 \<and> \<chi> l1 l2" using a1 by auto
blanchet@55129
  1241
  qed
blanchet@55938
  1242
  show ?Rr unfolding rel_set_def Bex_def vimage_eq proof safe
blanchet@55129
  1243
    fix r1 assume "Inr r1 \<in> A1"
blanchet@55943
  1244
    then obtain a2 where a2: "a2 \<in> A2" and "rel_sum \<chi> \<phi> (Inr r1) a2"
blanchet@55938
  1245
    using L unfolding rel_set_def by auto
blanchet@55129
  1246
    then obtain r2 where "a2 = Inr r2 \<and> \<phi> r1 r2" by (cases a2, auto)
blanchet@55129
  1247
    thus "\<exists> r2. Inr r2 \<in> A2 \<and> \<phi> r1 r2" using a2 by auto
blanchet@55129
  1248
  next
blanchet@55129
  1249
    fix r2 assume "Inr r2 \<in> A2"
blanchet@55943
  1250
    then obtain a1 where a1: "a1 \<in> A1" and "rel_sum \<chi> \<phi> a1 (Inr r2)"
blanchet@55938
  1251
    using L unfolding rel_set_def by auto
blanchet@55129
  1252
    then obtain r1 where "a1 = Inr r1 \<and> \<phi> r1 r2" by (cases a1, auto)
blanchet@55129
  1253
    thus "\<exists> r1. Inr r1 \<in> A1 \<and> \<phi> r1 r2" using a1 by auto
blanchet@55129
  1254
  qed
blanchet@55129
  1255
next
blanchet@55129
  1256
  assume Rl: "?Rl" and Rr: "?Rr"
blanchet@55938
  1257
  show ?L unfolding rel_set_def Bex_def vimage_eq proof safe
blanchet@55129
  1258
    fix a1 assume a1: "a1 \<in> A1"
blanchet@55943
  1259
    show "\<exists> a2. a2 \<in> A2 \<and> rel_sum \<chi> \<phi> a1 a2"
blanchet@55129
  1260
    proof(cases a1)
blanchet@55129
  1261
      case (Inl l1) then obtain l2 where "Inl l2 \<in> A2 \<and> \<chi> l1 l2"
blanchet@55938
  1262
      using Rl a1 unfolding rel_set_def by blast
blanchet@55129
  1263
      thus ?thesis unfolding Inl by auto
blanchet@55129
  1264
    next
blanchet@55129
  1265
      case (Inr r1) then obtain r2 where "Inr r2 \<in> A2 \<and> \<phi> r1 r2"
blanchet@55938
  1266
      using Rr a1 unfolding rel_set_def by blast
blanchet@55129
  1267
      thus ?thesis unfolding Inr by auto
blanchet@55129
  1268
    qed
blanchet@55129
  1269
  next
blanchet@55129
  1270
    fix a2 assume a2: "a2 \<in> A2"
blanchet@55943
  1271
    show "\<exists> a1. a1 \<in> A1 \<and> rel_sum \<chi> \<phi> a1 a2"
blanchet@55129
  1272
    proof(cases a2)
blanchet@55129
  1273
      case (Inl l2) then obtain l1 where "Inl l1 \<in> A1 \<and> \<chi> l1 l2"
blanchet@55938
  1274
      using Rl a2 unfolding rel_set_def by blast
blanchet@55129
  1275
      thus ?thesis unfolding Inl by auto
blanchet@55129
  1276
    next
blanchet@55129
  1277
      case (Inr r2) then obtain r1 where "Inr r1 \<in> A1 \<and> \<phi> r1 r2"
blanchet@55938
  1278
      using Rr a2 unfolding rel_set_def by blast
blanchet@55129
  1279
      thus ?thesis unfolding Inr by auto
blanchet@55129
  1280
    qed
blanchet@55129
  1281
  qed
blanchet@55129
  1282
qed
blanchet@55129
  1283
lars@60712
  1284
lars@66262
  1285
subsubsection \<open>Countability\<close>
lars@66262
  1286
lars@66262
  1287
lemma exists_fset_of_list: "\<exists>xs. fset_of_list xs = S"
lars@66262
  1288
including fset.lifting
lars@66262
  1289
by transfer (rule finite_list)
lars@66262
  1290
lars@66262
  1291
lemma fset_of_list_surj[simp, intro]: "surj fset_of_list"
lars@66262
  1292
proof -
lars@66262
  1293
  have "x \<in> range fset_of_list" for x :: "'a fset"
lars@66262
  1294
    unfolding image_iff
lars@66262
  1295
    using exists_fset_of_list by fastforce
lars@66262
  1296
  thus ?thesis by auto
lars@66262
  1297
qed
lars@66262
  1298
lars@66262
  1299
instance fset :: (countable) countable
lars@66262
  1300
proof
lars@66262
  1301
  obtain to_nat :: "'a list \<Rightarrow> nat" where "inj to_nat"
lars@66262
  1302
    by (metis ex_inj)
lars@66262
  1303
  moreover have "inj (inv fset_of_list)"
lars@66262
  1304
    using fset_of_list_surj by (rule surj_imp_inj_inv)
lars@66262
  1305
  ultimately have "inj (to_nat \<circ> inv fset_of_list)"
lars@66262
  1306
    by (rule inj_comp)
lars@66262
  1307
  thus "\<exists>to_nat::'a fset \<Rightarrow> nat. inj to_nat"
lars@66262
  1308
    by auto
lars@66262
  1309
qed
lars@66262
  1310
lars@66262
  1311
lars@60712
  1312
subsection \<open>Quickcheck setup\<close>
lars@60712
  1313
lars@60712
  1314
text \<open>Setup adapted from sets.\<close>
lars@60712
  1315
lars@60712
  1316
notation Quickcheck_Exhaustive.orelse (infixr "orelse" 55)
lars@60712
  1317
lars@60712
  1318
definition (in term_syntax) [code_unfold]:
lars@60712
  1319
"valterm_femptyset = Code_Evaluation.valtermify ({||} :: ('a :: typerep) fset)"
lars@60712
  1320
lars@60712
  1321
definition (in term_syntax) [code_unfold]:
lars@60712
  1322
"valtermify_finsert x s = Code_Evaluation.valtermify finsert {\<cdot>} (x :: ('a :: typerep * _)) {\<cdot>} s"
lars@60712
  1323
lars@60712
  1324
instantiation fset :: (exhaustive) exhaustive
lars@60712
  1325
begin
lars@60712
  1326
lars@60712
  1327
fun exhaustive_fset where
lars@60712
  1328
"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
  1329
lars@60712
  1330
instance ..
lars@60712
  1331
blanchet@55129
  1332
end
lars@60712
  1333
lars@60712
  1334
instantiation fset :: (full_exhaustive) full_exhaustive
lars@60712
  1335
begin
lars@60712
  1336
lars@60712
  1337
fun full_exhaustive_fset where
lars@60712
  1338
"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
  1339
lars@60712
  1340
instance ..
lars@60712
  1341
lars@60712
  1342
end
lars@60712
  1343
lars@60712
  1344
no_notation Quickcheck_Exhaustive.orelse (infixr "orelse" 55)
lars@60712
  1345
lars@60712
  1346
notation scomp (infixl "\<circ>\<rightarrow>" 60)
lars@60712
  1347
lars@60712
  1348
instantiation fset :: (random) random
lars@60712
  1349
begin
lars@60712
  1350
lars@60712
  1351
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
  1352
"random_aux_fset 0 j = Quickcheck_Random.collapse (Random.select_weight [(1, Pair valterm_femptyset)])" |
lars@60712
  1353
"random_aux_fset (Code_Numeral.Suc i) j =
lars@60712
  1354
  Quickcheck_Random.collapse (Random.select_weight
lars@60712
  1355
    [(1, Pair valterm_femptyset),
lars@60712
  1356
     (Code_Numeral.Suc i,
lars@60712
  1357
      Quickcheck_Random.random j \<circ>\<rightarrow> (\<lambda>x. random_aux_fset i j \<circ>\<rightarrow> (\<lambda>s. Pair (valtermify_finsert x s))))])"
lars@60712
  1358
lars@60712
  1359
lemma [code]:
lars@60712
  1360
  "random_aux_fset i j =
lars@60712
  1361
    Quickcheck_Random.collapse (Random.select_weight [(1, Pair valterm_femptyset),
lars@60712
  1362
      (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
  1363
proof (induct i rule: natural.induct)
lars@60712
  1364
  case zero
lars@60712
  1365
  show ?case by (subst select_weight_drop_zero[symmetric]) (simp add: less_natural_def)
lars@60712
  1366
next
lars@60712
  1367
  case (Suc i)
lars@60712
  1368
  show ?case by (simp only: random_aux_fset.simps Suc_natural_minus_one)
lars@60712
  1369
qed
lars@60712
  1370
lars@60712
  1371
definition "random_fset i = random_aux_fset i i"
lars@60712
  1372
lars@60712
  1373
instance ..
lars@60712
  1374
lars@60712
  1375
end
lars@60712
  1376
lars@60712
  1377
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
lars@60712
  1378
nipkow@67399
  1379
end