src/HOL/Lifting_Set.thy
author haftmann
Fri Jun 19 07:53:35 2015 +0200 (2015-06-19)
changeset 60517 f16e4fb20652
parent 60229 4cd6462c1fda
child 60676 92fd47ae2a67
permissions -rw-r--r--
separate class for notions specific for integral (semi)domains, in contrast to fields where these are trivial
kuncar@53012
     1
(*  Title:      HOL/Lifting_Set.thy
kuncar@53012
     2
    Author:     Brian Huffman and Ondrej Kuncar
kuncar@53012
     3
*)
kuncar@53012
     4
wenzelm@58889
     5
section {* Setup for Lifting/Transfer for the set type *}
kuncar@53012
     6
kuncar@53012
     7
theory Lifting_Set
kuncar@53012
     8
imports Lifting
kuncar@53012
     9
begin
kuncar@53012
    10
kuncar@53012
    11
subsection {* Relator and predicator properties *}
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"
blanchet@55938
    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"
blanchet@55938
    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)
kuncar@53012
    30
  apply (intro ext, rename_tac X Z)
kuncar@53012
    31
  apply (rule iffI)
kuncar@53012
    32
  apply (rule_tac b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}" in relcomppI)
blanchet@55938
    33
  apply (simp add: rel_set_def, fast)
blanchet@55938
    34
  apply (simp add: rel_set_def, fast)
blanchet@55938
    35
  apply (simp add: rel_set_def, fast)
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))"
kuncar@56520
    40
unfolding rel_set_def Domainp_iff[abs_def]
kuncar@53012
    41
apply (intro ext)
kuncar@53012
    42
apply (rule iffI) 
kuncar@53012
    43
apply blast
kuncar@53012
    44
apply (rename_tac A, rule_tac x="{y. \<exists>x\<in>A. T x y}" in exI, fast)
kuncar@53012
    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
kuncar@53012
    51
  apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
kuncar@53012
    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)"
blanchet@55938
    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)"
kuncar@56524
    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
hoelzl@57129
    83
  def f \<equiv> "\<lambda>x. THE y. R x y"
hoelzl@57129
    84
  { fix x assume "x \<in> X"
hoelzl@57129
    85
    with `rel_set R X Y` `bi_unique R` have "R x (f x)"
hoelzl@57129
    86
      by (simp add: bi_unique_def rel_set_def f_def) (metis theI)
hoelzl@57129
    87
    with assms `x \<in> X` 
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"
hoelzl@57129
    93
    with `rel_set R X Y` *(3) `y \<in> Y` 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
kuncar@53012
    99
subsection {* Quotient theorem for the Lifting package *}
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])
blanchet@55938
   106
  apply (simp add: rel_set_def, fast)
kuncar@53012
   107
  done
kuncar@53012
   108
kuncar@53012
   109
subsection {* Transfer rules for the Transfer package *}
kuncar@53012
   110
kuncar@53012
   111
subsubsection {* Unconditional transfer rules *}
kuncar@53012
   112
kuncar@53012
   113
context
kuncar@53012
   114
begin
kuncar@53012
   115
interpretation lifting_syntax .
kuncar@53012
   116
blanchet@55938
   117
lemma empty_transfer [transfer_rule]: "(rel_set A) {} {}"
blanchet@55938
   118
  unfolding rel_set_def by simp
kuncar@53012
   119
kuncar@53012
   120
lemma insert_transfer [transfer_rule]:
blanchet@55938
   121
  "(A ===> rel_set A ===> rel_set A) insert insert"
blanchet@55945
   122
  unfolding rel_fun_def rel_set_def by auto
kuncar@53012
   123
kuncar@53012
   124
lemma union_transfer [transfer_rule]:
blanchet@55938
   125
  "(rel_set A ===> rel_set A ===> rel_set A) union union"
blanchet@55945
   126
  unfolding rel_fun_def rel_set_def by auto
kuncar@53012
   127
kuncar@53012
   128
lemma Union_transfer [transfer_rule]:
blanchet@55938
   129
  "(rel_set (rel_set A) ===> rel_set A) Union Union"
blanchet@55945
   130
  unfolding rel_fun_def rel_set_def by simp fast
kuncar@53012
   131
kuncar@53012
   132
lemma image_transfer [transfer_rule]:
blanchet@55938
   133
  "((A ===> B) ===> rel_set A ===> rel_set B) image image"
blanchet@55945
   134
  unfolding rel_fun_def rel_set_def by simp fast
kuncar@53012
   135
kuncar@53012
   136
lemma UNION_transfer [transfer_rule]:
blanchet@55938
   137
  "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) UNION UNION"
haftmann@56166
   138
  unfolding Union_image_eq [symmetric, abs_def] by transfer_prover
kuncar@53012
   139
kuncar@53012
   140
lemma Ball_transfer [transfer_rule]:
blanchet@55938
   141
  "(rel_set A ===> (A ===> op =) ===> op =) Ball Ball"
blanchet@55945
   142
  unfolding rel_set_def rel_fun_def by fast
kuncar@53012
   143
kuncar@53012
   144
lemma Bex_transfer [transfer_rule]:
blanchet@55938
   145
  "(rel_set A ===> (A ===> op =) ===> op =) Bex Bex"
blanchet@55945
   146
  unfolding rel_set_def rel_fun_def by fast
kuncar@53012
   147
kuncar@53012
   148
lemma Pow_transfer [transfer_rule]:
blanchet@55938
   149
  "(rel_set A ===> rel_set (rel_set A)) Pow Pow"
blanchet@55945
   150
  apply (rule rel_funI, rename_tac X Y, rule rel_setI)
kuncar@53012
   151
  apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
blanchet@55938
   152
  apply (simp add: rel_set_def, fast)
kuncar@53012
   153
  apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
blanchet@55938
   154
  apply (simp add: rel_set_def, fast)
kuncar@53012
   155
  done
kuncar@53012
   156
blanchet@55938
   157
lemma rel_set_transfer [transfer_rule]:
haftmann@56482
   158
  "((A ===> B ===> op =) ===> rel_set A ===> rel_set B ===> op =) rel_set rel_set"
blanchet@55945
   159
  unfolding rel_fun_def rel_set_def by fast
kuncar@53012
   160
kuncar@53952
   161
lemma bind_transfer [transfer_rule]:
blanchet@55938
   162
  "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) Set.bind Set.bind"
haftmann@56482
   163
  unfolding bind_UNION [abs_def] by transfer_prover
haftmann@56482
   164
haftmann@56482
   165
lemma INF_parametric [transfer_rule]:
haftmann@56482
   166
  "(rel_set A ===> (A ===> HOL.eq) ===> HOL.eq) INFIMUM INFIMUM"
haftmann@56482
   167
  unfolding INF_def [abs_def] by transfer_prover
haftmann@56482
   168
haftmann@56482
   169
lemma SUP_parametric [transfer_rule]:
haftmann@56482
   170
  "(rel_set R ===> (R ===> HOL.eq) ===> HOL.eq) SUPREMUM SUPREMUM"
haftmann@56482
   171
  unfolding SUP_def [abs_def] by transfer_prover
haftmann@56482
   172
kuncar@53952
   173
kuncar@53012
   174
subsubsection {* Rules requiring bi-unique, bi-total or right-total relations *}
kuncar@53012
   175
kuncar@53012
   176
lemma member_transfer [transfer_rule]:
kuncar@53012
   177
  assumes "bi_unique A"
blanchet@55938
   178
  shows "(A ===> rel_set A ===> op =) (op \<in>) (op \<in>)"
blanchet@55945
   179
  using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
kuncar@53012
   180
kuncar@53012
   181
lemma right_total_Collect_transfer[transfer_rule]:
kuncar@53012
   182
  assumes "right_total A"
blanchet@55938
   183
  shows "((A ===> op =) ===> rel_set A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
blanchet@55945
   184
  using assms unfolding right_total_def rel_set_def rel_fun_def Domainp_iff by fast
kuncar@53012
   185
kuncar@53012
   186
lemma Collect_transfer [transfer_rule]:
kuncar@53012
   187
  assumes "bi_total A"
blanchet@55938
   188
  shows "((A ===> op =) ===> rel_set A) Collect Collect"
blanchet@55945
   189
  using assms unfolding rel_fun_def rel_set_def bi_total_def by fast
kuncar@53012
   190
kuncar@53012
   191
lemma inter_transfer [transfer_rule]:
kuncar@53012
   192
  assumes "bi_unique A"
blanchet@55938
   193
  shows "(rel_set A ===> rel_set A ===> rel_set A) inter inter"
blanchet@55945
   194
  using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
kuncar@53012
   195
kuncar@53012
   196
lemma Diff_transfer [transfer_rule]:
kuncar@53012
   197
  assumes "bi_unique A"
blanchet@55938
   198
  shows "(rel_set A ===> rel_set A ===> rel_set A) (op -) (op -)"
blanchet@55945
   199
  using assms unfolding rel_fun_def rel_set_def bi_unique_def
kuncar@53012
   200
  unfolding Ball_def Bex_def Diff_eq
kuncar@53012
   201
  by (safe, simp, metis, simp, metis)
kuncar@53012
   202
kuncar@53012
   203
lemma subset_transfer [transfer_rule]:
kuncar@53012
   204
  assumes [transfer_rule]: "bi_unique A"
blanchet@55938
   205
  shows "(rel_set A ===> rel_set A ===> op =) (op \<subseteq>) (op \<subseteq>)"
kuncar@53012
   206
  unfolding subset_eq [abs_def] by transfer_prover
kuncar@53012
   207
kuncar@60229
   208
declare right_total_UNIV_transfer[transfer_rule]
kuncar@53012
   209
kuncar@53012
   210
lemma UNIV_transfer [transfer_rule]:
kuncar@53012
   211
  assumes "bi_total A"
blanchet@55938
   212
  shows "(rel_set A) UNIV UNIV"
blanchet@55938
   213
  using assms unfolding rel_set_def bi_total_def by simp
kuncar@53012
   214
kuncar@53012
   215
lemma right_total_Compl_transfer [transfer_rule]:
kuncar@53012
   216
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
blanchet@55938
   217
  shows "(rel_set A ===> rel_set A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
kuncar@53012
   218
  unfolding Compl_eq [abs_def]
kuncar@53012
   219
  by (subst Collect_conj_eq[symmetric]) transfer_prover
kuncar@53012
   220
kuncar@53012
   221
lemma Compl_transfer [transfer_rule]:
kuncar@53012
   222
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
blanchet@55938
   223
  shows "(rel_set A ===> rel_set A) uminus uminus"
kuncar@53012
   224
  unfolding Compl_eq [abs_def] by transfer_prover
kuncar@53012
   225
kuncar@53012
   226
lemma right_total_Inter_transfer [transfer_rule]:
kuncar@53012
   227
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
blanchet@55938
   228
  shows "(rel_set (rel_set A) ===> rel_set A) (\<lambda>S. Inter S \<inter> Collect (Domainp A)) Inter"
kuncar@53012
   229
  unfolding Inter_eq[abs_def]
kuncar@53012
   230
  by (subst Collect_conj_eq[symmetric]) transfer_prover
kuncar@53012
   231
kuncar@53012
   232
lemma Inter_transfer [transfer_rule]:
kuncar@53012
   233
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
blanchet@55938
   234
  shows "(rel_set (rel_set A) ===> rel_set A) Inter Inter"
kuncar@53012
   235
  unfolding Inter_eq [abs_def] by transfer_prover
kuncar@53012
   236
kuncar@53012
   237
lemma filter_transfer [transfer_rule]:
kuncar@53012
   238
  assumes [transfer_rule]: "bi_unique A"
blanchet@55938
   239
  shows "((A ===> op=) ===> rel_set A ===> rel_set A) Set.filter Set.filter"
blanchet@55945
   240
  unfolding Set.filter_def[abs_def] rel_fun_def rel_set_def by blast
kuncar@53012
   241
kuncar@53012
   242
lemma finite_transfer [transfer_rule]:
blanchet@55938
   243
  "bi_unique A \<Longrightarrow> (rel_set A ===> op =) finite finite"
hoelzl@57129
   244
  by (rule rel_funI, erule (1) bi_unique_rel_set_lemma)
hoelzl@57129
   245
     (auto dest: finite_imageD)
kuncar@53012
   246
kuncar@53012
   247
lemma card_transfer [transfer_rule]:
blanchet@55938
   248
  "bi_unique A \<Longrightarrow> (rel_set A ===> op =) card card"
hoelzl@57129
   249
  by (rule rel_funI, erule (1) bi_unique_rel_set_lemma)
hoelzl@57129
   250
     (simp add: card_image)
kuncar@53012
   251
Andreas@53927
   252
lemma vimage_parametric [transfer_rule]:
Andreas@53927
   253
  assumes [transfer_rule]: "bi_total A" "bi_unique B"
blanchet@55938
   254
  shows "((A ===> B) ===> rel_set B ===> rel_set A) vimage vimage"
hoelzl@57129
   255
  unfolding vimage_def[abs_def] by transfer_prover
Andreas@53927
   256
Andreas@57599
   257
lemma Image_parametric [transfer_rule]:
Andreas@57599
   258
  assumes "bi_unique A"
Andreas@57599
   259
  shows "(rel_set (rel_prod A B) ===> rel_set A ===> rel_set B) op `` op ``"
Andreas@57599
   260
by(intro rel_funI rel_setI)
Andreas@57599
   261
  (force dest: rel_setD1 bi_uniqueDr[OF assms], force dest: rel_setD2 bi_uniqueDl[OF assms])
Andreas@57599
   262
kuncar@53012
   263
end
kuncar@53012
   264
hoelzl@57129
   265
lemma (in comm_monoid_set) F_parametric [transfer_rule]:
hoelzl@57129
   266
  fixes A :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
hoelzl@57129
   267
  assumes "bi_unique A"
hoelzl@57129
   268
  shows "rel_fun (rel_fun A (op =)) (rel_fun (rel_set A) (op =)) F F"
hoelzl@57129
   269
proof(rule rel_funI)+
hoelzl@57129
   270
  fix f :: "'b \<Rightarrow> 'a" and g S T
hoelzl@57129
   271
  assume "rel_fun A (op =) f g" "rel_set A S T"
hoelzl@57129
   272
  with `bi_unique A` obtain i where "bij_betw i S T" "\<And>x. x \<in> S \<Longrightarrow> f x = g (i x)"
hoelzl@57129
   273
    by (auto elim: bi_unique_rel_set_lemma simp: rel_fun_def bij_betw_def)
hoelzl@57129
   274
  then show "F f S = F g T"
hoelzl@57129
   275
    by (simp add: reindex_bij_betw)
hoelzl@57129
   276
qed
hoelzl@57129
   277
hoelzl@57129
   278
lemmas setsum_parametric = setsum.F_parametric
hoelzl@57129
   279
lemmas setprod_parametric = setprod.F_parametric
hoelzl@57129
   280
Andreas@60057
   281
lemma rel_set_UNION:
Andreas@60057
   282
  assumes [transfer_rule]: "rel_set Q A B" "rel_fun Q (rel_set R) f g"
Andreas@60057
   283
  shows "rel_set R (UNION A f) (UNION B g)"
Andreas@60057
   284
by transfer_prover
Andreas@60057
   285
kuncar@53012
   286
end