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