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