src/HOL/Lifting_Set.thy
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (19 months ago)
changeset 67022 49309fe530fd
parent 64272 f76b6dda2e56
child 67399 eab6ce8368fa
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
kuncar@53012
     1
(*  Title:      HOL/Lifting_Set.thy
kuncar@53012
     2
    Author:     Brian Huffman and Ondrej Kuncar
kuncar@53012
     3
*)
kuncar@53012
     4
wenzelm@60758
     5
section \<open>Setup for Lifting/Transfer for the set type\<close>
kuncar@53012
     6
kuncar@53012
     7
theory Lifting_Set
kuncar@53012
     8
imports Lifting
kuncar@53012
     9
begin
kuncar@53012
    10
wenzelm@60758
    11
subsection \<open>Relator and predicator properties\<close>
kuncar@53012
    12
blanchet@55938
    13
lemma rel_setD1: "\<lbrakk> rel_set R A B; x \<in> A \<rbrakk> \<Longrightarrow> \<exists>y \<in> B. R x y"
blanchet@55938
    14
  and rel_setD2: "\<lbrakk> rel_set R A B; y \<in> B \<rbrakk> \<Longrightarrow> \<exists>x \<in> A. R x y"
wenzelm@60676
    15
  by (simp_all add: rel_set_def)
Andreas@53927
    16
blanchet@55938
    17
lemma rel_set_conversep [simp]: "rel_set A\<inverse>\<inverse> = (rel_set A)\<inverse>\<inverse>"
blanchet@55938
    18
  unfolding rel_set_def by auto
kuncar@53012
    19
blanchet@55938
    20
lemma rel_set_eq [relator_eq]: "rel_set (op =) = (op =)"
blanchet@55938
    21
  unfolding rel_set_def fun_eq_iff by auto
kuncar@53012
    22
blanchet@55938
    23
lemma rel_set_mono[relator_mono]:
kuncar@53012
    24
  assumes "A \<le> B"
blanchet@55938
    25
  shows "rel_set A \<le> rel_set B"
wenzelm@60676
    26
  using assms unfolding rel_set_def by blast
kuncar@53012
    27
blanchet@55938
    28
lemma rel_set_OO[relator_distr]: "rel_set R OO rel_set S = rel_set (R OO S)"
kuncar@53012
    29
  apply (rule sym)
wenzelm@60676
    30
  apply (intro ext)
wenzelm@60676
    31
  subgoal for X Z
wenzelm@60676
    32
    apply (rule iffI)
wenzelm@60676
    33
    apply (rule relcomppI [where b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}"])
wenzelm@60676
    34
    apply (simp add: rel_set_def, fast)+
wenzelm@60676
    35
    done
kuncar@53012
    36
  done
kuncar@53012
    37
kuncar@53012
    38
lemma Domainp_set[relator_domain]:
kuncar@56520
    39
  "Domainp (rel_set T) = (\<lambda>A. Ball A (Domainp T))"
wenzelm@60676
    40
  unfolding rel_set_def Domainp_iff[abs_def]
wenzelm@60676
    41
  apply (intro ext)
wenzelm@60676
    42
  apply (rule iffI) 
wenzelm@60676
    43
  apply blast
wenzelm@60676
    44
  subgoal for A by (rule exI [where x="{y. \<exists>x\<in>A. T x y}"]) fast
wenzelm@60676
    45
  done
kuncar@53012
    46
kuncar@56518
    47
lemma left_total_rel_set[transfer_rule]: 
blanchet@55938
    48
  "left_total A \<Longrightarrow> left_total (rel_set A)"
blanchet@55938
    49
  unfolding left_total_def rel_set_def
kuncar@53012
    50
  apply safe
wenzelm@60676
    51
  subgoal for X by (rule exI [where x="{y. \<exists>x\<in>X. A x y}"]) fast
wenzelm@60676
    52
  done
kuncar@53012
    53
kuncar@56518
    54
lemma left_unique_rel_set[transfer_rule]: 
blanchet@55938
    55
  "left_unique A \<Longrightarrow> left_unique (rel_set A)"
blanchet@55938
    56
  unfolding left_unique_def rel_set_def
kuncar@53012
    57
  by fast
kuncar@53012
    58
blanchet@55938
    59
lemma right_total_rel_set [transfer_rule]:
blanchet@55938
    60
  "right_total A \<Longrightarrow> right_total (rel_set A)"
wenzelm@60676
    61
  using left_total_rel_set[of "A\<inverse>\<inverse>"] by simp
kuncar@53012
    62
blanchet@55938
    63
lemma right_unique_rel_set [transfer_rule]:
blanchet@55938
    64
  "right_unique A \<Longrightarrow> right_unique (rel_set A)"
blanchet@55938
    65
  unfolding right_unique_def rel_set_def by fast
kuncar@53012
    66
blanchet@55938
    67
lemma bi_total_rel_set [transfer_rule]:
blanchet@55938
    68
  "bi_total A \<Longrightarrow> bi_total (rel_set A)"
wenzelm@60676
    69
  by(simp add: bi_total_alt_def left_total_rel_set right_total_rel_set)
kuncar@53012
    70
blanchet@55938
    71
lemma bi_unique_rel_set [transfer_rule]:
blanchet@55938
    72
  "bi_unique A \<Longrightarrow> bi_unique (rel_set A)"
blanchet@55938
    73
  unfolding bi_unique_def rel_set_def by fast
kuncar@53012
    74
kuncar@56519
    75
lemma set_relator_eq_onp [relator_eq_onp]:
kuncar@56519
    76
  "rel_set (eq_onp P) = eq_onp (\<lambda>A. Ball A P)"
kuncar@56519
    77
  unfolding fun_eq_iff rel_set_def eq_onp_def Ball_def by fast
kuncar@53012
    78
hoelzl@57129
    79
lemma bi_unique_rel_set_lemma:
hoelzl@57129
    80
  assumes "bi_unique R" and "rel_set R X Y"
hoelzl@57129
    81
  obtains f where "Y = image f X" and "inj_on f X" and "\<forall>x\<in>X. R x (f x)"
hoelzl@57129
    82
proof
wenzelm@63040
    83
  define f where "f x = (THE y. R x y)" for x
hoelzl@57129
    84
  { fix x assume "x \<in> X"
wenzelm@60758
    85
    with \<open>rel_set R X Y\<close> \<open>bi_unique R\<close> have "R x (f x)"
hoelzl@57129
    86
      by (simp add: bi_unique_def rel_set_def f_def) (metis theI)
wenzelm@60758
    87
    with assms \<open>x \<in> X\<close> 
hoelzl@57129
    88
    have  "R x (f x)" "\<forall>x'\<in>X. R x' (f x) \<longrightarrow> x = x'" "\<forall>y\<in>Y. R x y \<longrightarrow> y = f x" "f x \<in> Y"
hoelzl@57129
    89
      by (fastforce simp add: bi_unique_def rel_set_def)+ }
hoelzl@57129
    90
  note * = this
hoelzl@57129
    91
  moreover
hoelzl@57129
    92
  { fix y assume "y \<in> Y"
wenzelm@60758
    93
    with \<open>rel_set R X Y\<close> *(3) \<open>y \<in> Y\<close> have "\<exists>x\<in>X. y = f x"
hoelzl@57129
    94
      by (fastforce simp: rel_set_def) }
hoelzl@57129
    95
  ultimately show "\<forall>x\<in>X. R x (f x)" "Y = image f X" "inj_on f X"
hoelzl@57129
    96
    by (auto simp: inj_on_def image_iff)
hoelzl@57129
    97
qed
hoelzl@57129
    98
wenzelm@60758
    99
subsection \<open>Quotient theorem for the Lifting package\<close>
kuncar@53012
   100
kuncar@53012
   101
lemma Quotient_set[quot_map]:
kuncar@53012
   102
  assumes "Quotient R Abs Rep T"
blanchet@55938
   103
  shows "Quotient (rel_set R) (image Abs) (image Rep) (rel_set T)"
kuncar@53012
   104
  using assms unfolding Quotient_alt_def4
blanchet@55938
   105
  apply (simp add: rel_set_OO[symmetric])
wenzelm@60676
   106
  apply (simp add: rel_set_def)
wenzelm@60676
   107
  apply fast
kuncar@53012
   108
  done
kuncar@53012
   109
wenzelm@60676
   110
wenzelm@60758
   111
subsection \<open>Transfer rules for the Transfer package\<close>
kuncar@53012
   112
wenzelm@60758
   113
subsubsection \<open>Unconditional transfer rules\<close>
kuncar@53012
   114
wenzelm@63343
   115
context includes lifting_syntax
kuncar@53012
   116
begin
wenzelm@60676
   117
blanchet@55938
   118
lemma empty_transfer [transfer_rule]: "(rel_set A) {} {}"
blanchet@55938
   119
  unfolding rel_set_def by simp
kuncar@53012
   120
kuncar@53012
   121
lemma insert_transfer [transfer_rule]:
blanchet@55938
   122
  "(A ===> rel_set A ===> rel_set A) insert insert"
blanchet@55945
   123
  unfolding rel_fun_def rel_set_def by auto
kuncar@53012
   124
kuncar@53012
   125
lemma union_transfer [transfer_rule]:
blanchet@55938
   126
  "(rel_set A ===> rel_set A ===> rel_set A) union union"
blanchet@55945
   127
  unfolding rel_fun_def rel_set_def by auto
kuncar@53012
   128
kuncar@53012
   129
lemma Union_transfer [transfer_rule]:
blanchet@55938
   130
  "(rel_set (rel_set A) ===> rel_set A) Union Union"
blanchet@55945
   131
  unfolding rel_fun_def rel_set_def by simp fast
kuncar@53012
   132
kuncar@53012
   133
lemma image_transfer [transfer_rule]:
blanchet@55938
   134
  "((A ===> B) ===> rel_set A ===> rel_set B) image image"
blanchet@55945
   135
  unfolding rel_fun_def rel_set_def by simp fast
kuncar@53012
   136
kuncar@53012
   137
lemma UNION_transfer [transfer_rule]:
blanchet@55938
   138
  "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) UNION UNION"
haftmann@62343
   139
  by transfer_prover
kuncar@53012
   140
kuncar@53012
   141
lemma Ball_transfer [transfer_rule]:
blanchet@55938
   142
  "(rel_set A ===> (A ===> op =) ===> op =) Ball Ball"
blanchet@55945
   143
  unfolding rel_set_def rel_fun_def by fast
kuncar@53012
   144
kuncar@53012
   145
lemma Bex_transfer [transfer_rule]:
blanchet@55938
   146
  "(rel_set A ===> (A ===> op =) ===> op =) Bex Bex"
blanchet@55945
   147
  unfolding rel_set_def rel_fun_def by fast
kuncar@53012
   148
kuncar@53012
   149
lemma Pow_transfer [transfer_rule]:
blanchet@55938
   150
  "(rel_set A ===> rel_set (rel_set A)) Pow Pow"
wenzelm@60676
   151
  apply (rule rel_funI)
wenzelm@60676
   152
  apply (rule rel_setI)
wenzelm@60676
   153
  subgoal for X Y X'
wenzelm@60676
   154
    apply (rule rev_bexI [where x="{y\<in>Y. \<exists>x\<in>X'. A x y}"])
wenzelm@60676
   155
    apply clarsimp
wenzelm@60676
   156
    apply (simp add: rel_set_def)
wenzelm@60676
   157
    apply fast
wenzelm@60676
   158
    done
wenzelm@60676
   159
  subgoal for X Y Y'
wenzelm@60676
   160
    apply (rule rev_bexI [where x="{x\<in>X. \<exists>y\<in>Y'. A x y}"])
wenzelm@60676
   161
    apply clarsimp
wenzelm@60676
   162
    apply (simp add: rel_set_def)
wenzelm@60676
   163
    apply fast
wenzelm@60676
   164
    done
kuncar@53012
   165
  done
kuncar@53012
   166
blanchet@55938
   167
lemma rel_set_transfer [transfer_rule]:
haftmann@56482
   168
  "((A ===> B ===> op =) ===> rel_set A ===> rel_set B ===> op =) rel_set rel_set"
blanchet@55945
   169
  unfolding rel_fun_def rel_set_def by fast
kuncar@53012
   170
kuncar@53952
   171
lemma bind_transfer [transfer_rule]:
blanchet@55938
   172
  "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) Set.bind Set.bind"
haftmann@56482
   173
  unfolding bind_UNION [abs_def] by transfer_prover
haftmann@56482
   174
haftmann@56482
   175
lemma INF_parametric [transfer_rule]:
haftmann@56482
   176
  "(rel_set A ===> (A ===> HOL.eq) ===> HOL.eq) INFIMUM INFIMUM"
haftmann@62343
   177
  by transfer_prover
haftmann@56482
   178
haftmann@56482
   179
lemma SUP_parametric [transfer_rule]:
haftmann@56482
   180
  "(rel_set R ===> (R ===> HOL.eq) ===> HOL.eq) SUPREMUM SUPREMUM"
haftmann@62343
   181
  by transfer_prover
haftmann@56482
   182
kuncar@53952
   183
wenzelm@60758
   184
subsubsection \<open>Rules requiring bi-unique, bi-total or right-total relations\<close>
kuncar@53012
   185
kuncar@53012
   186
lemma member_transfer [transfer_rule]:
kuncar@53012
   187
  assumes "bi_unique A"
blanchet@55938
   188
  shows "(A ===> rel_set A ===> op =) (op \<in>) (op \<in>)"
blanchet@55945
   189
  using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
kuncar@53012
   190
kuncar@53012
   191
lemma right_total_Collect_transfer[transfer_rule]:
kuncar@53012
   192
  assumes "right_total A"
blanchet@55938
   193
  shows "((A ===> op =) ===> rel_set A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
blanchet@55945
   194
  using assms unfolding right_total_def rel_set_def rel_fun_def Domainp_iff by fast
kuncar@53012
   195
kuncar@53012
   196
lemma Collect_transfer [transfer_rule]:
kuncar@53012
   197
  assumes "bi_total A"
blanchet@55938
   198
  shows "((A ===> op =) ===> rel_set A) Collect Collect"
blanchet@55945
   199
  using assms unfolding rel_fun_def rel_set_def bi_total_def by fast
kuncar@53012
   200
kuncar@53012
   201
lemma inter_transfer [transfer_rule]:
kuncar@53012
   202
  assumes "bi_unique A"
blanchet@55938
   203
  shows "(rel_set A ===> rel_set A ===> rel_set A) inter inter"
blanchet@55945
   204
  using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
kuncar@53012
   205
kuncar@53012
   206
lemma Diff_transfer [transfer_rule]:
kuncar@53012
   207
  assumes "bi_unique A"
blanchet@55938
   208
  shows "(rel_set A ===> rel_set A ===> rel_set A) (op -) (op -)"
blanchet@55945
   209
  using assms unfolding rel_fun_def rel_set_def bi_unique_def
kuncar@53012
   210
  unfolding Ball_def Bex_def Diff_eq
kuncar@53012
   211
  by (safe, simp, metis, simp, metis)
kuncar@53012
   212
kuncar@53012
   213
lemma subset_transfer [transfer_rule]:
kuncar@53012
   214
  assumes [transfer_rule]: "bi_unique A"
blanchet@55938
   215
  shows "(rel_set A ===> rel_set A ===> op =) (op \<subseteq>) (op \<subseteq>)"
kuncar@53012
   216
  unfolding subset_eq [abs_def] by transfer_prover
kuncar@53012
   217
kuncar@60229
   218
declare right_total_UNIV_transfer[transfer_rule]
kuncar@53012
   219
kuncar@53012
   220
lemma UNIV_transfer [transfer_rule]:
kuncar@53012
   221
  assumes "bi_total A"
blanchet@55938
   222
  shows "(rel_set A) UNIV UNIV"
blanchet@55938
   223
  using assms unfolding rel_set_def bi_total_def by simp
kuncar@53012
   224
kuncar@53012
   225
lemma right_total_Compl_transfer [transfer_rule]:
kuncar@53012
   226
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
blanchet@55938
   227
  shows "(rel_set A ===> rel_set A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
kuncar@53012
   228
  unfolding Compl_eq [abs_def]
kuncar@53012
   229
  by (subst Collect_conj_eq[symmetric]) transfer_prover
kuncar@53012
   230
kuncar@53012
   231
lemma Compl_transfer [transfer_rule]:
kuncar@53012
   232
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
blanchet@55938
   233
  shows "(rel_set A ===> rel_set A) uminus uminus"
kuncar@53012
   234
  unfolding Compl_eq [abs_def] by transfer_prover
kuncar@53012
   235
kuncar@53012
   236
lemma right_total_Inter_transfer [transfer_rule]:
kuncar@53012
   237
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
wenzelm@61952
   238
  shows "(rel_set (rel_set A) ===> rel_set A) (\<lambda>S. \<Inter>S \<inter> Collect (Domainp A)) Inter"
kuncar@53012
   239
  unfolding Inter_eq[abs_def]
kuncar@53012
   240
  by (subst Collect_conj_eq[symmetric]) transfer_prover
kuncar@53012
   241
kuncar@53012
   242
lemma Inter_transfer [transfer_rule]:
kuncar@53012
   243
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
blanchet@55938
   244
  shows "(rel_set (rel_set A) ===> rel_set A) Inter Inter"
kuncar@53012
   245
  unfolding Inter_eq [abs_def] by transfer_prover
kuncar@53012
   246
kuncar@53012
   247
lemma filter_transfer [transfer_rule]:
kuncar@53012
   248
  assumes [transfer_rule]: "bi_unique A"
blanchet@55938
   249
  shows "((A ===> op=) ===> rel_set A ===> rel_set A) Set.filter Set.filter"
blanchet@55945
   250
  unfolding Set.filter_def[abs_def] rel_fun_def rel_set_def by blast
kuncar@53012
   251
kuncar@53012
   252
lemma finite_transfer [transfer_rule]:
blanchet@55938
   253
  "bi_unique A \<Longrightarrow> (rel_set A ===> op =) finite finite"
hoelzl@57129
   254
  by (rule rel_funI, erule (1) bi_unique_rel_set_lemma)
hoelzl@57129
   255
     (auto dest: finite_imageD)
kuncar@53012
   256
kuncar@53012
   257
lemma card_transfer [transfer_rule]:
blanchet@55938
   258
  "bi_unique A \<Longrightarrow> (rel_set A ===> op =) card card"
hoelzl@57129
   259
  by (rule rel_funI, erule (1) bi_unique_rel_set_lemma)
hoelzl@57129
   260
     (simp add: card_image)
kuncar@53012
   261
Andreas@53927
   262
lemma vimage_parametric [transfer_rule]:
Andreas@53927
   263
  assumes [transfer_rule]: "bi_total A" "bi_unique B"
blanchet@55938
   264
  shows "((A ===> B) ===> rel_set B ===> rel_set A) vimage vimage"
hoelzl@57129
   265
  unfolding vimage_def[abs_def] by transfer_prover
Andreas@53927
   266
Andreas@57599
   267
lemma Image_parametric [transfer_rule]:
Andreas@57599
   268
  assumes "bi_unique A"
Andreas@57599
   269
  shows "(rel_set (rel_prod A B) ===> rel_set A ===> rel_set B) op `` op ``"
wenzelm@60676
   270
  by (intro rel_funI rel_setI)
wenzelm@60676
   271
    (force dest: rel_setD1 bi_uniqueDr[OF assms], force dest: rel_setD2 bi_uniqueDl[OF assms])
Andreas@57599
   272
kuncar@53012
   273
end
kuncar@53012
   274
hoelzl@57129
   275
lemma (in comm_monoid_set) F_parametric [transfer_rule]:
hoelzl@57129
   276
  fixes A :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
hoelzl@57129
   277
  assumes "bi_unique A"
hoelzl@57129
   278
  shows "rel_fun (rel_fun A (op =)) (rel_fun (rel_set A) (op =)) F F"
wenzelm@60676
   279
proof (rule rel_funI)+
hoelzl@57129
   280
  fix f :: "'b \<Rightarrow> 'a" and g S T
hoelzl@57129
   281
  assume "rel_fun A (op =) f g" "rel_set A S T"
wenzelm@60758
   282
  with \<open>bi_unique A\<close> obtain i where "bij_betw i S T" "\<And>x. x \<in> S \<Longrightarrow> f x = g (i x)"
hoelzl@57129
   283
    by (auto elim: bi_unique_rel_set_lemma simp: rel_fun_def bij_betw_def)
hoelzl@57129
   284
  then show "F f S = F g T"
hoelzl@57129
   285
    by (simp add: reindex_bij_betw)
hoelzl@57129
   286
qed
hoelzl@57129
   287
nipkow@64267
   288
lemmas sum_parametric = sum.F_parametric
nipkow@64272
   289
lemmas prod_parametric = prod.F_parametric
hoelzl@57129
   290
Andreas@60057
   291
lemma rel_set_UNION:
Andreas@60057
   292
  assumes [transfer_rule]: "rel_set Q A B" "rel_fun Q (rel_set R) f g"
Andreas@60057
   293
  shows "rel_set R (UNION A f) (UNION B g)"
wenzelm@60676
   294
  by transfer_prover
Andreas@60057
   295
kuncar@53012
   296
end