src/HOL/Lifting_Set.thy
author blanchet
Sun May 04 18:14:58 2014 +0200 (2014-05-04)
changeset 56846 9df717fef2bb
parent 56524 f4ba736040fa
child 57129 7edb7550663e
permissions -rw-r--r--
renamed 'xxx_size' to 'size_xxx' for old datatype package
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
kuncar@53012
    88
subsection {* Quotient theorem for the Lifting package *}
kuncar@53012
    89
kuncar@53012
    90
lemma Quotient_set[quot_map]:
kuncar@53012
    91
  assumes "Quotient R Abs Rep T"
blanchet@55938
    92
  shows "Quotient (rel_set R) (image Abs) (image Rep) (rel_set T)"
kuncar@53012
    93
  using assms unfolding Quotient_alt_def4
blanchet@55938
    94
  apply (simp add: rel_set_OO[symmetric])
blanchet@55938
    95
  apply (simp add: rel_set_def, fast)
kuncar@53012
    96
  done
kuncar@53012
    97
kuncar@53012
    98
subsection {* Transfer rules for the Transfer package *}
kuncar@53012
    99
kuncar@53012
   100
subsubsection {* Unconditional transfer rules *}
kuncar@53012
   101
kuncar@53012
   102
context
kuncar@53012
   103
begin
kuncar@53012
   104
interpretation lifting_syntax .
kuncar@53012
   105
blanchet@55938
   106
lemma empty_transfer [transfer_rule]: "(rel_set A) {} {}"
blanchet@55938
   107
  unfolding rel_set_def by simp
kuncar@53012
   108
kuncar@53012
   109
lemma insert_transfer [transfer_rule]:
blanchet@55938
   110
  "(A ===> rel_set A ===> rel_set A) insert insert"
blanchet@55945
   111
  unfolding rel_fun_def rel_set_def by auto
kuncar@53012
   112
kuncar@53012
   113
lemma union_transfer [transfer_rule]:
blanchet@55938
   114
  "(rel_set A ===> rel_set A ===> rel_set A) union union"
blanchet@55945
   115
  unfolding rel_fun_def rel_set_def by auto
kuncar@53012
   116
kuncar@53012
   117
lemma Union_transfer [transfer_rule]:
blanchet@55938
   118
  "(rel_set (rel_set A) ===> rel_set A) Union Union"
blanchet@55945
   119
  unfolding rel_fun_def rel_set_def by simp fast
kuncar@53012
   120
kuncar@53012
   121
lemma image_transfer [transfer_rule]:
blanchet@55938
   122
  "((A ===> B) ===> rel_set A ===> rel_set B) image image"
blanchet@55945
   123
  unfolding rel_fun_def rel_set_def by simp fast
kuncar@53012
   124
kuncar@53012
   125
lemma UNION_transfer [transfer_rule]:
blanchet@55938
   126
  "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) UNION UNION"
haftmann@56166
   127
  unfolding Union_image_eq [symmetric, abs_def] by transfer_prover
kuncar@53012
   128
kuncar@53012
   129
lemma Ball_transfer [transfer_rule]:
blanchet@55938
   130
  "(rel_set A ===> (A ===> op =) ===> op =) Ball Ball"
blanchet@55945
   131
  unfolding rel_set_def rel_fun_def by fast
kuncar@53012
   132
kuncar@53012
   133
lemma Bex_transfer [transfer_rule]:
blanchet@55938
   134
  "(rel_set A ===> (A ===> op =) ===> op =) Bex Bex"
blanchet@55945
   135
  unfolding rel_set_def rel_fun_def by fast
kuncar@53012
   136
kuncar@53012
   137
lemma Pow_transfer [transfer_rule]:
blanchet@55938
   138
  "(rel_set A ===> rel_set (rel_set A)) Pow Pow"
blanchet@55945
   139
  apply (rule rel_funI, rename_tac X Y, rule rel_setI)
kuncar@53012
   140
  apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
blanchet@55938
   141
  apply (simp add: rel_set_def, fast)
kuncar@53012
   142
  apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
blanchet@55938
   143
  apply (simp add: rel_set_def, fast)
kuncar@53012
   144
  done
kuncar@53012
   145
blanchet@55938
   146
lemma rel_set_transfer [transfer_rule]:
haftmann@56482
   147
  "((A ===> B ===> op =) ===> rel_set A ===> rel_set B ===> op =) rel_set rel_set"
blanchet@55945
   148
  unfolding rel_fun_def rel_set_def by fast
kuncar@53012
   149
kuncar@53952
   150
lemma bind_transfer [transfer_rule]:
blanchet@55938
   151
  "(rel_set A ===> (A ===> rel_set B) ===> rel_set B) Set.bind Set.bind"
haftmann@56482
   152
  unfolding bind_UNION [abs_def] by transfer_prover
haftmann@56482
   153
haftmann@56482
   154
lemma INF_parametric [transfer_rule]:
haftmann@56482
   155
  "(rel_set A ===> (A ===> HOL.eq) ===> HOL.eq) INFIMUM INFIMUM"
haftmann@56482
   156
  unfolding INF_def [abs_def] by transfer_prover
haftmann@56482
   157
haftmann@56482
   158
lemma SUP_parametric [transfer_rule]:
haftmann@56482
   159
  "(rel_set R ===> (R ===> HOL.eq) ===> HOL.eq) SUPREMUM SUPREMUM"
haftmann@56482
   160
  unfolding SUP_def [abs_def] by transfer_prover
haftmann@56482
   161
kuncar@53952
   162
kuncar@53012
   163
subsubsection {* Rules requiring bi-unique, bi-total or right-total relations *}
kuncar@53012
   164
kuncar@53012
   165
lemma member_transfer [transfer_rule]:
kuncar@53012
   166
  assumes "bi_unique A"
blanchet@55938
   167
  shows "(A ===> rel_set A ===> op =) (op \<in>) (op \<in>)"
blanchet@55945
   168
  using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
kuncar@53012
   169
kuncar@53012
   170
lemma right_total_Collect_transfer[transfer_rule]:
kuncar@53012
   171
  assumes "right_total A"
blanchet@55938
   172
  shows "((A ===> op =) ===> rel_set A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
blanchet@55945
   173
  using assms unfolding right_total_def rel_set_def rel_fun_def Domainp_iff by fast
kuncar@53012
   174
kuncar@53012
   175
lemma Collect_transfer [transfer_rule]:
kuncar@53012
   176
  assumes "bi_total A"
blanchet@55938
   177
  shows "((A ===> op =) ===> rel_set A) Collect Collect"
blanchet@55945
   178
  using assms unfolding rel_fun_def rel_set_def bi_total_def by fast
kuncar@53012
   179
kuncar@53012
   180
lemma inter_transfer [transfer_rule]:
kuncar@53012
   181
  assumes "bi_unique A"
blanchet@55938
   182
  shows "(rel_set A ===> rel_set A ===> rel_set A) inter inter"
blanchet@55945
   183
  using assms unfolding rel_fun_def rel_set_def bi_unique_def by fast
kuncar@53012
   184
kuncar@53012
   185
lemma Diff_transfer [transfer_rule]:
kuncar@53012
   186
  assumes "bi_unique A"
blanchet@55938
   187
  shows "(rel_set A ===> rel_set A ===> rel_set A) (op -) (op -)"
blanchet@55945
   188
  using assms unfolding rel_fun_def rel_set_def bi_unique_def
kuncar@53012
   189
  unfolding Ball_def Bex_def Diff_eq
kuncar@53012
   190
  by (safe, simp, metis, simp, metis)
kuncar@53012
   191
kuncar@53012
   192
lemma subset_transfer [transfer_rule]:
kuncar@53012
   193
  assumes [transfer_rule]: "bi_unique A"
blanchet@55938
   194
  shows "(rel_set A ===> rel_set A ===> op =) (op \<subseteq>) (op \<subseteq>)"
kuncar@53012
   195
  unfolding subset_eq [abs_def] by transfer_prover
kuncar@53012
   196
kuncar@53012
   197
lemma right_total_UNIV_transfer[transfer_rule]: 
kuncar@53012
   198
  assumes "right_total A"
blanchet@55938
   199
  shows "(rel_set A) (Collect (Domainp A)) UNIV"
blanchet@55938
   200
  using assms unfolding right_total_def rel_set_def Domainp_iff by blast
kuncar@53012
   201
kuncar@53012
   202
lemma UNIV_transfer [transfer_rule]:
kuncar@53012
   203
  assumes "bi_total A"
blanchet@55938
   204
  shows "(rel_set A) UNIV UNIV"
blanchet@55938
   205
  using assms unfolding rel_set_def bi_total_def by simp
kuncar@53012
   206
kuncar@53012
   207
lemma right_total_Compl_transfer [transfer_rule]:
kuncar@53012
   208
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
blanchet@55938
   209
  shows "(rel_set A ===> rel_set A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
kuncar@53012
   210
  unfolding Compl_eq [abs_def]
kuncar@53012
   211
  by (subst Collect_conj_eq[symmetric]) transfer_prover
kuncar@53012
   212
kuncar@53012
   213
lemma Compl_transfer [transfer_rule]:
kuncar@53012
   214
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
blanchet@55938
   215
  shows "(rel_set A ===> rel_set A) uminus uminus"
kuncar@53012
   216
  unfolding Compl_eq [abs_def] by transfer_prover
kuncar@53012
   217
kuncar@53012
   218
lemma right_total_Inter_transfer [transfer_rule]:
kuncar@53012
   219
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
blanchet@55938
   220
  shows "(rel_set (rel_set A) ===> rel_set A) (\<lambda>S. Inter S \<inter> Collect (Domainp A)) Inter"
kuncar@53012
   221
  unfolding Inter_eq[abs_def]
kuncar@53012
   222
  by (subst Collect_conj_eq[symmetric]) transfer_prover
kuncar@53012
   223
kuncar@53012
   224
lemma Inter_transfer [transfer_rule]:
kuncar@53012
   225
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
blanchet@55938
   226
  shows "(rel_set (rel_set A) ===> rel_set A) Inter Inter"
kuncar@53012
   227
  unfolding Inter_eq [abs_def] by transfer_prover
kuncar@53012
   228
kuncar@53012
   229
lemma filter_transfer [transfer_rule]:
kuncar@53012
   230
  assumes [transfer_rule]: "bi_unique A"
blanchet@55938
   231
  shows "((A ===> op=) ===> rel_set A ===> rel_set A) Set.filter Set.filter"
blanchet@55945
   232
  unfolding Set.filter_def[abs_def] rel_fun_def rel_set_def by blast
kuncar@53012
   233
blanchet@55938
   234
lemma bi_unique_rel_set_lemma:
blanchet@55938
   235
  assumes "bi_unique R" and "rel_set R X Y"
kuncar@53012
   236
  obtains f where "Y = image f X" and "inj_on f X" and "\<forall>x\<in>X. R x (f x)"
kuncar@53012
   237
proof
kuncar@53012
   238
  let ?f = "\<lambda>x. THE y. R x y"
kuncar@53012
   239
  from assms show f: "\<forall>x\<in>X. R x (?f x)"
blanchet@55938
   240
    apply (clarsimp simp add: rel_set_def)
kuncar@53012
   241
    apply (drule (1) bspec, clarify)
kuncar@53012
   242
    apply (rule theI2, assumption)
kuncar@53012
   243
    apply (simp add: bi_unique_def)
kuncar@53012
   244
    apply assumption
kuncar@53012
   245
    done
kuncar@53012
   246
  from assms show "Y = image ?f X"
kuncar@53012
   247
    apply safe
blanchet@55938
   248
    apply (clarsimp simp add: rel_set_def)
kuncar@53012
   249
    apply (drule (1) bspec, clarify)
kuncar@53012
   250
    apply (rule image_eqI)
kuncar@53012
   251
    apply (rule the_equality [symmetric], assumption)
kuncar@53012
   252
    apply (simp add: bi_unique_def)
kuncar@53012
   253
    apply assumption
blanchet@55938
   254
    apply (clarsimp simp add: rel_set_def)
kuncar@53012
   255
    apply (frule (1) bspec, clarify)
kuncar@53012
   256
    apply (rule theI2, assumption)
kuncar@53012
   257
    apply (clarsimp simp add: bi_unique_def)
kuncar@53012
   258
    apply (simp add: bi_unique_def, metis)
kuncar@53012
   259
    done
kuncar@53012
   260
  show "inj_on ?f X"
kuncar@53012
   261
    apply (rule inj_onI)
kuncar@53012
   262
    apply (drule f [rule_format])
kuncar@53012
   263
    apply (drule f [rule_format])
kuncar@53012
   264
    apply (simp add: assms(1) [unfolded bi_unique_def])
kuncar@53012
   265
    done
kuncar@53012
   266
qed
kuncar@53012
   267
kuncar@53012
   268
lemma finite_transfer [transfer_rule]:
blanchet@55938
   269
  "bi_unique A \<Longrightarrow> (rel_set A ===> op =) finite finite"
blanchet@55945
   270
  by (rule rel_funI, erule (1) bi_unique_rel_set_lemma,
kuncar@53012
   271
    auto dest: finite_imageD)
kuncar@53012
   272
kuncar@53012
   273
lemma card_transfer [transfer_rule]:
blanchet@55938
   274
  "bi_unique A \<Longrightarrow> (rel_set A ===> op =) card card"
blanchet@55945
   275
  by (rule rel_funI, erule (1) bi_unique_rel_set_lemma, simp add: card_image)
kuncar@53012
   276
Andreas@53927
   277
lemma vimage_parametric [transfer_rule]:
Andreas@53927
   278
  assumes [transfer_rule]: "bi_total A" "bi_unique B"
blanchet@55938
   279
  shows "((A ===> B) ===> rel_set B ===> rel_set A) vimage vimage"
Andreas@53927
   280
unfolding vimage_def[abs_def] by transfer_prover
Andreas@53927
   281
Andreas@53927
   282
lemma setsum_parametric [transfer_rule]:
Andreas@53927
   283
  assumes "bi_unique A"
blanchet@55938
   284
  shows "((A ===> op =) ===> rel_set A ===> op =) setsum setsum"
blanchet@55945
   285
proof(rule rel_funI)+
Andreas@53927
   286
  fix f :: "'a \<Rightarrow> 'c" and g S T
Andreas@53927
   287
  assume fg: "(A ===> op =) f g"
blanchet@55938
   288
    and ST: "rel_set A S T"
Andreas@53927
   289
  show "setsum f S = setsum g T"
Andreas@53927
   290
  proof(rule setsum_reindex_cong)
Andreas@53927
   291
    let ?f = "\<lambda>t. THE s. A s t"
Andreas@53927
   292
    show "S = ?f ` T"
blanchet@55938
   293
      by(blast dest: rel_setD1[OF ST] rel_setD2[OF ST] bi_uniqueDl[OF assms] 
Andreas@53927
   294
           intro: rev_image_eqI the_equality[symmetric] subst[rotated, where P="\<lambda>x. x \<in> S"])
Andreas@53927
   295
Andreas@53927
   296
    show "inj_on ?f T"
Andreas@53927
   297
    proof(rule inj_onI)
Andreas@53927
   298
      fix t1 t2
Andreas@53927
   299
      assume "t1 \<in> T" "t2 \<in> T" "?f t1 = ?f t2"
blanchet@55938
   300
      from ST `t1 \<in> T` obtain s1 where "A s1 t1" "s1 \<in> S" by(auto dest: rel_setD2)
Andreas@53927
   301
      hence "?f t1 = s1" by(auto dest: bi_uniqueDl[OF assms])
Andreas@53927
   302
      moreover
blanchet@55938
   303
      from ST `t2 \<in> T` obtain s2 where "A s2 t2" "s2 \<in> S" by(auto dest: rel_setD2)
Andreas@53927
   304
      hence "?f t2 = s2" by(auto dest: bi_uniqueDl[OF assms])
Andreas@53927
   305
      ultimately have "s1 = s2" using `?f t1 = ?f t2` by simp
Andreas@53927
   306
      with `A s1 t1` `A s2 t2` show "t1 = t2" by(auto dest: bi_uniqueDr[OF assms])
Andreas@53927
   307
    qed
Andreas@53927
   308
Andreas@53927
   309
    fix t
Andreas@53927
   310
    assume "t \<in> T"
blanchet@55938
   311
    with ST obtain s where "A s t" "s \<in> S" by(auto dest: rel_setD2)
Andreas@53927
   312
    hence "?f t = s" by(auto dest: bi_uniqueDl[OF assms])
blanchet@55945
   313
    moreover from fg `A s t` have "f s = g t" by(rule rel_funD)
Andreas@53927
   314
    ultimately show "g t = f (?f t)" by simp
Andreas@53927
   315
  qed
Andreas@53927
   316
qed
Andreas@53927
   317
kuncar@53012
   318
end
kuncar@53012
   319
kuncar@53012
   320
end