src/HOL/Lifting_Set.thy
author kuncar
Tue Aug 13 15:59:22 2013 +0200 (2013-08-13)
changeset 53012 cb82606b8215
child 53927 abe2b313f0e5
permissions -rw-r--r--
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
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
kuncar@53012
    13
definition set_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
kuncar@53012
    14
  where "set_rel 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
kuncar@53012
    16
lemma set_relI:
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"
kuncar@53012
    19
  shows "set_rel R A B"
kuncar@53012
    20
  using assms unfolding set_rel_def by simp
kuncar@53012
    21
kuncar@53012
    22
lemma set_rel_conversep: "set_rel (conversep R) = conversep (set_rel R)"
kuncar@53012
    23
  unfolding set_rel_def by auto
kuncar@53012
    24
kuncar@53012
    25
lemma set_rel_eq [relator_eq]: "set_rel (op =) = (op =)"
kuncar@53012
    26
  unfolding set_rel_def fun_eq_iff by auto
kuncar@53012
    27
kuncar@53012
    28
lemma set_rel_mono[relator_mono]:
kuncar@53012
    29
  assumes "A \<le> B"
kuncar@53012
    30
  shows "set_rel A \<le> set_rel B"
kuncar@53012
    31
using assms unfolding set_rel_def by blast
kuncar@53012
    32
kuncar@53012
    33
lemma set_rel_OO[relator_distr]: "set_rel R OO set_rel S = set_rel (R OO S)"
kuncar@53012
    34
  apply (rule sym)
kuncar@53012
    35
  apply (intro ext, rename_tac X Z)
kuncar@53012
    36
  apply (rule iffI)
kuncar@53012
    37
  apply (rule_tac b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}" in relcomppI)
kuncar@53012
    38
  apply (simp add: set_rel_def, fast)
kuncar@53012
    39
  apply (simp add: set_rel_def, fast)
kuncar@53012
    40
  apply (simp add: set_rel_def, fast)
kuncar@53012
    41
  done
kuncar@53012
    42
kuncar@53012
    43
lemma Domainp_set[relator_domain]:
kuncar@53012
    44
  assumes "Domainp T = R"
kuncar@53012
    45
  shows "Domainp (set_rel T) = (\<lambda>A. Ball A R)"
kuncar@53012
    46
using assms unfolding set_rel_def Domainp_iff[abs_def]
kuncar@53012
    47
apply (intro ext)
kuncar@53012
    48
apply (rule iffI) 
kuncar@53012
    49
apply blast
kuncar@53012
    50
apply (rename_tac A, rule_tac x="{y. \<exists>x\<in>A. T x y}" in exI, fast)
kuncar@53012
    51
done
kuncar@53012
    52
kuncar@53012
    53
lemma reflp_set_rel[reflexivity_rule]: "reflp R \<Longrightarrow> reflp (set_rel R)"
kuncar@53012
    54
  unfolding reflp_def set_rel_def by fast
kuncar@53012
    55
kuncar@53012
    56
lemma left_total_set_rel[reflexivity_rule]: 
kuncar@53012
    57
  "left_total A \<Longrightarrow> left_total (set_rel A)"
kuncar@53012
    58
  unfolding left_total_def set_rel_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@53012
    63
lemma left_unique_set_rel[reflexivity_rule]: 
kuncar@53012
    64
  "left_unique A \<Longrightarrow> left_unique (set_rel A)"
kuncar@53012
    65
  unfolding left_unique_def set_rel_def
kuncar@53012
    66
  by fast
kuncar@53012
    67
kuncar@53012
    68
lemma right_total_set_rel [transfer_rule]:
kuncar@53012
    69
  "right_total A \<Longrightarrow> right_total (set_rel A)"
kuncar@53012
    70
  unfolding right_total_def set_rel_def
kuncar@53012
    71
  by (rule allI, rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
kuncar@53012
    72
kuncar@53012
    73
lemma right_unique_set_rel [transfer_rule]:
kuncar@53012
    74
  "right_unique A \<Longrightarrow> right_unique (set_rel A)"
kuncar@53012
    75
  unfolding right_unique_def set_rel_def by fast
kuncar@53012
    76
kuncar@53012
    77
lemma bi_total_set_rel [transfer_rule]:
kuncar@53012
    78
  "bi_total A \<Longrightarrow> bi_total (set_rel A)"
kuncar@53012
    79
  unfolding bi_total_def set_rel_def
kuncar@53012
    80
  apply safe
kuncar@53012
    81
  apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
kuncar@53012
    82
  apply (rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
kuncar@53012
    83
  done
kuncar@53012
    84
kuncar@53012
    85
lemma bi_unique_set_rel [transfer_rule]:
kuncar@53012
    86
  "bi_unique A \<Longrightarrow> bi_unique (set_rel A)"
kuncar@53012
    87
  unfolding bi_unique_def set_rel_def by fast
kuncar@53012
    88
kuncar@53012
    89
lemma set_invariant_commute [invariant_commute]:
kuncar@53012
    90
  "set_rel (Lifting.invariant P) = Lifting.invariant (\<lambda>A. Ball A P)"
kuncar@53012
    91
  unfolding fun_eq_iff set_rel_def Lifting.invariant_def Ball_def by fast
kuncar@53012
    92
kuncar@53012
    93
subsection {* Quotient theorem for the Lifting package *}
kuncar@53012
    94
kuncar@53012
    95
lemma Quotient_set[quot_map]:
kuncar@53012
    96
  assumes "Quotient R Abs Rep T"
kuncar@53012
    97
  shows "Quotient (set_rel R) (image Abs) (image Rep) (set_rel T)"
kuncar@53012
    98
  using assms unfolding Quotient_alt_def4
kuncar@53012
    99
  apply (simp add: set_rel_OO[symmetric] set_rel_conversep)
kuncar@53012
   100
  apply (simp add: set_rel_def, fast)
kuncar@53012
   101
  done
kuncar@53012
   102
kuncar@53012
   103
subsection {* Transfer rules for the Transfer package *}
kuncar@53012
   104
kuncar@53012
   105
subsubsection {* Unconditional transfer rules *}
kuncar@53012
   106
kuncar@53012
   107
context
kuncar@53012
   108
begin
kuncar@53012
   109
interpretation lifting_syntax .
kuncar@53012
   110
kuncar@53012
   111
lemma empty_transfer [transfer_rule]: "(set_rel A) {} {}"
kuncar@53012
   112
  unfolding set_rel_def by simp
kuncar@53012
   113
kuncar@53012
   114
lemma insert_transfer [transfer_rule]:
kuncar@53012
   115
  "(A ===> set_rel A ===> set_rel A) insert insert"
kuncar@53012
   116
  unfolding fun_rel_def set_rel_def by auto
kuncar@53012
   117
kuncar@53012
   118
lemma union_transfer [transfer_rule]:
kuncar@53012
   119
  "(set_rel A ===> set_rel A ===> set_rel A) union union"
kuncar@53012
   120
  unfolding fun_rel_def set_rel_def by auto
kuncar@53012
   121
kuncar@53012
   122
lemma Union_transfer [transfer_rule]:
kuncar@53012
   123
  "(set_rel (set_rel A) ===> set_rel A) Union Union"
kuncar@53012
   124
  unfolding fun_rel_def set_rel_def by simp fast
kuncar@53012
   125
kuncar@53012
   126
lemma image_transfer [transfer_rule]:
kuncar@53012
   127
  "((A ===> B) ===> set_rel A ===> set_rel B) image image"
kuncar@53012
   128
  unfolding fun_rel_def set_rel_def by simp fast
kuncar@53012
   129
kuncar@53012
   130
lemma UNION_transfer [transfer_rule]:
kuncar@53012
   131
  "(set_rel A ===> (A ===> set_rel B) ===> set_rel B) UNION UNION"
kuncar@53012
   132
  unfolding SUP_def [abs_def] by transfer_prover
kuncar@53012
   133
kuncar@53012
   134
lemma Ball_transfer [transfer_rule]:
kuncar@53012
   135
  "(set_rel A ===> (A ===> op =) ===> op =) Ball Ball"
kuncar@53012
   136
  unfolding set_rel_def fun_rel_def by fast
kuncar@53012
   137
kuncar@53012
   138
lemma Bex_transfer [transfer_rule]:
kuncar@53012
   139
  "(set_rel A ===> (A ===> op =) ===> op =) Bex Bex"
kuncar@53012
   140
  unfolding set_rel_def fun_rel_def by fast
kuncar@53012
   141
kuncar@53012
   142
lemma Pow_transfer [transfer_rule]:
kuncar@53012
   143
  "(set_rel A ===> set_rel (set_rel A)) Pow Pow"
kuncar@53012
   144
  apply (rule fun_relI, rename_tac X Y, rule set_relI)
kuncar@53012
   145
  apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
kuncar@53012
   146
  apply (simp add: set_rel_def, fast)
kuncar@53012
   147
  apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
kuncar@53012
   148
  apply (simp add: set_rel_def, fast)
kuncar@53012
   149
  done
kuncar@53012
   150
kuncar@53012
   151
lemma set_rel_transfer [transfer_rule]:
kuncar@53012
   152
  "((A ===> B ===> op =) ===> set_rel A ===> set_rel B ===> op =)
kuncar@53012
   153
    set_rel set_rel"
kuncar@53012
   154
  unfolding fun_rel_def set_rel_def by fast
kuncar@53012
   155
kuncar@53012
   156
kuncar@53012
   157
subsubsection {* Rules requiring bi-unique, bi-total or right-total relations *}
kuncar@53012
   158
kuncar@53012
   159
lemma member_transfer [transfer_rule]:
kuncar@53012
   160
  assumes "bi_unique A"
kuncar@53012
   161
  shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
kuncar@53012
   162
  using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
kuncar@53012
   163
kuncar@53012
   164
lemma right_total_Collect_transfer[transfer_rule]:
kuncar@53012
   165
  assumes "right_total A"
kuncar@53012
   166
  shows "((A ===> op =) ===> set_rel A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
kuncar@53012
   167
  using assms unfolding right_total_def set_rel_def fun_rel_def Domainp_iff by fast
kuncar@53012
   168
kuncar@53012
   169
lemma Collect_transfer [transfer_rule]:
kuncar@53012
   170
  assumes "bi_total A"
kuncar@53012
   171
  shows "((A ===> op =) ===> set_rel A) Collect Collect"
kuncar@53012
   172
  using assms unfolding fun_rel_def set_rel_def bi_total_def by fast
kuncar@53012
   173
kuncar@53012
   174
lemma inter_transfer [transfer_rule]:
kuncar@53012
   175
  assumes "bi_unique A"
kuncar@53012
   176
  shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
kuncar@53012
   177
  using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
kuncar@53012
   178
kuncar@53012
   179
lemma Diff_transfer [transfer_rule]:
kuncar@53012
   180
  assumes "bi_unique A"
kuncar@53012
   181
  shows "(set_rel A ===> set_rel A ===> set_rel A) (op -) (op -)"
kuncar@53012
   182
  using assms unfolding fun_rel_def set_rel_def bi_unique_def
kuncar@53012
   183
  unfolding Ball_def Bex_def Diff_eq
kuncar@53012
   184
  by (safe, simp, metis, simp, metis)
kuncar@53012
   185
kuncar@53012
   186
lemma subset_transfer [transfer_rule]:
kuncar@53012
   187
  assumes [transfer_rule]: "bi_unique A"
kuncar@53012
   188
  shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
kuncar@53012
   189
  unfolding subset_eq [abs_def] by transfer_prover
kuncar@53012
   190
kuncar@53012
   191
lemma right_total_UNIV_transfer[transfer_rule]: 
kuncar@53012
   192
  assumes "right_total A"
kuncar@53012
   193
  shows "(set_rel A) (Collect (Domainp A)) UNIV"
kuncar@53012
   194
  using assms unfolding right_total_def set_rel_def Domainp_iff by blast
kuncar@53012
   195
kuncar@53012
   196
lemma UNIV_transfer [transfer_rule]:
kuncar@53012
   197
  assumes "bi_total A"
kuncar@53012
   198
  shows "(set_rel A) UNIV UNIV"
kuncar@53012
   199
  using assms unfolding set_rel_def bi_total_def by simp
kuncar@53012
   200
kuncar@53012
   201
lemma right_total_Compl_transfer [transfer_rule]:
kuncar@53012
   202
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
kuncar@53012
   203
  shows "(set_rel A ===> set_rel A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
kuncar@53012
   204
  unfolding Compl_eq [abs_def]
kuncar@53012
   205
  by (subst Collect_conj_eq[symmetric]) transfer_prover
kuncar@53012
   206
kuncar@53012
   207
lemma Compl_transfer [transfer_rule]:
kuncar@53012
   208
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
kuncar@53012
   209
  shows "(set_rel A ===> set_rel A) uminus uminus"
kuncar@53012
   210
  unfolding Compl_eq [abs_def] by transfer_prover
kuncar@53012
   211
kuncar@53012
   212
lemma right_total_Inter_transfer [transfer_rule]:
kuncar@53012
   213
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
kuncar@53012
   214
  shows "(set_rel (set_rel A) ===> set_rel A) (\<lambda>S. Inter S \<inter> Collect (Domainp A)) Inter"
kuncar@53012
   215
  unfolding Inter_eq[abs_def]
kuncar@53012
   216
  by (subst Collect_conj_eq[symmetric]) transfer_prover
kuncar@53012
   217
kuncar@53012
   218
lemma Inter_transfer [transfer_rule]:
kuncar@53012
   219
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
kuncar@53012
   220
  shows "(set_rel (set_rel A) ===> set_rel A) Inter Inter"
kuncar@53012
   221
  unfolding Inter_eq [abs_def] by transfer_prover
kuncar@53012
   222
kuncar@53012
   223
lemma filter_transfer [transfer_rule]:
kuncar@53012
   224
  assumes [transfer_rule]: "bi_unique A"
kuncar@53012
   225
  shows "((A ===> op=) ===> set_rel A ===> set_rel A) Set.filter Set.filter"
kuncar@53012
   226
  unfolding Set.filter_def[abs_def] fun_rel_def set_rel_def by blast
kuncar@53012
   227
kuncar@53012
   228
lemma bi_unique_set_rel_lemma:
kuncar@53012
   229
  assumes "bi_unique R" and "set_rel R X Y"
kuncar@53012
   230
  obtains f where "Y = image f X" and "inj_on f X" and "\<forall>x\<in>X. R x (f x)"
kuncar@53012
   231
proof
kuncar@53012
   232
  let ?f = "\<lambda>x. THE y. R x y"
kuncar@53012
   233
  from assms show f: "\<forall>x\<in>X. R x (?f x)"
kuncar@53012
   234
    apply (clarsimp simp add: set_rel_def)
kuncar@53012
   235
    apply (drule (1) bspec, clarify)
kuncar@53012
   236
    apply (rule theI2, assumption)
kuncar@53012
   237
    apply (simp add: bi_unique_def)
kuncar@53012
   238
    apply assumption
kuncar@53012
   239
    done
kuncar@53012
   240
  from assms show "Y = image ?f X"
kuncar@53012
   241
    apply safe
kuncar@53012
   242
    apply (clarsimp simp add: set_rel_def)
kuncar@53012
   243
    apply (drule (1) bspec, clarify)
kuncar@53012
   244
    apply (rule image_eqI)
kuncar@53012
   245
    apply (rule the_equality [symmetric], assumption)
kuncar@53012
   246
    apply (simp add: bi_unique_def)
kuncar@53012
   247
    apply assumption
kuncar@53012
   248
    apply (clarsimp simp add: set_rel_def)
kuncar@53012
   249
    apply (frule (1) bspec, clarify)
kuncar@53012
   250
    apply (rule theI2, assumption)
kuncar@53012
   251
    apply (clarsimp simp add: bi_unique_def)
kuncar@53012
   252
    apply (simp add: bi_unique_def, metis)
kuncar@53012
   253
    done
kuncar@53012
   254
  show "inj_on ?f X"
kuncar@53012
   255
    apply (rule inj_onI)
kuncar@53012
   256
    apply (drule f [rule_format])
kuncar@53012
   257
    apply (drule f [rule_format])
kuncar@53012
   258
    apply (simp add: assms(1) [unfolded bi_unique_def])
kuncar@53012
   259
    done
kuncar@53012
   260
qed
kuncar@53012
   261
kuncar@53012
   262
lemma finite_transfer [transfer_rule]:
kuncar@53012
   263
  "bi_unique A \<Longrightarrow> (set_rel A ===> op =) finite finite"
kuncar@53012
   264
  by (rule fun_relI, erule (1) bi_unique_set_rel_lemma,
kuncar@53012
   265
    auto dest: finite_imageD)
kuncar@53012
   266
kuncar@53012
   267
lemma card_transfer [transfer_rule]:
kuncar@53012
   268
  "bi_unique A \<Longrightarrow> (set_rel A ===> op =) card card"
kuncar@53012
   269
  by (rule fun_relI, erule (1) bi_unique_set_rel_lemma, simp add: card_image)
kuncar@53012
   270
kuncar@53012
   271
end
kuncar@53012
   272
kuncar@53012
   273
end