src/HOL/Library/Quotient_Set.thy
author huffman
Mon May 14 17:09:11 2012 +0200 (2012-05-14)
changeset 47922 bba52dffab2b
parent 47777 f29e7dcd7c40
child 47936 756f30eac792
permissions -rw-r--r--
add transfer rule for set_rel
wenzelm@47455
     1
(*  Title:      HOL/Library/Quotient_Set.thy
kaliszyk@44413
     2
    Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@44413
     3
*)
kaliszyk@44413
     4
kaliszyk@44413
     5
header {* Quotient infrastructure for the set type *}
kaliszyk@44413
     6
kaliszyk@44413
     7
theory Quotient_Set
kaliszyk@44413
     8
imports Main Quotient_Syntax
kaliszyk@44413
     9
begin
kaliszyk@44413
    10
huffman@47648
    11
subsection {* Relator for set type *}
huffman@47648
    12
huffman@47648
    13
definition set_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
huffman@47648
    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))"
huffman@47648
    15
huffman@47648
    16
lemma set_relI:
huffman@47648
    17
  assumes "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. R x y"
huffman@47648
    18
  assumes "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. R x y"
huffman@47648
    19
  shows "set_rel R A B"
huffman@47648
    20
  using assms unfolding set_rel_def by simp
huffman@47648
    21
huffman@47648
    22
lemma set_rel_conversep: "set_rel (conversep R) = conversep (set_rel R)"
huffman@47648
    23
  unfolding set_rel_def by auto
huffman@47648
    24
huffman@47648
    25
lemma set_rel_OO: "set_rel (R OO S) = set_rel R OO set_rel S"
huffman@47648
    26
  apply (intro ext, rename_tac X Z)
huffman@47648
    27
  apply (rule iffI)
huffman@47648
    28
  apply (rule_tac b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}" in relcomppI)
huffman@47648
    29
  apply (simp add: set_rel_def, fast)
huffman@47648
    30
  apply (simp add: set_rel_def, fast)
huffman@47648
    31
  apply (simp add: set_rel_def, fast)
huffman@47648
    32
  done
huffman@47648
    33
huffman@47648
    34
lemma set_rel_eq [relator_eq]: "set_rel (op =) = (op =)"
huffman@47648
    35
  unfolding set_rel_def fun_eq_iff by auto
huffman@47648
    36
huffman@47648
    37
lemma reflp_set_rel: "reflp R \<Longrightarrow> reflp (set_rel R)"
huffman@47648
    38
  unfolding reflp_def set_rel_def by fast
huffman@47648
    39
huffman@47648
    40
lemma symp_set_rel: "symp R \<Longrightarrow> symp (set_rel R)"
huffman@47648
    41
  unfolding symp_def set_rel_def by fast
huffman@47648
    42
huffman@47648
    43
lemma transp_set_rel: "transp R \<Longrightarrow> transp (set_rel R)"
huffman@47648
    44
  unfolding transp_def set_rel_def by fast
huffman@47648
    45
huffman@47648
    46
lemma equivp_set_rel: "equivp R \<Longrightarrow> equivp (set_rel R)"
huffman@47648
    47
  by (blast intro: equivpI reflp_set_rel symp_set_rel transp_set_rel
huffman@47648
    48
    elim: equivpE)
huffman@47648
    49
huffman@47648
    50
lemma right_total_set_rel [transfer_rule]:
huffman@47648
    51
  "right_total A \<Longrightarrow> right_total (set_rel A)"
huffman@47648
    52
  unfolding right_total_def set_rel_def
huffman@47648
    53
  by (rule allI, rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
huffman@47648
    54
huffman@47648
    55
lemma right_unique_set_rel [transfer_rule]:
huffman@47648
    56
  "right_unique A \<Longrightarrow> right_unique (set_rel A)"
huffman@47648
    57
  unfolding right_unique_def set_rel_def by fast
huffman@47648
    58
huffman@47648
    59
lemma bi_total_set_rel [transfer_rule]:
huffman@47648
    60
  "bi_total A \<Longrightarrow> bi_total (set_rel A)"
huffman@47648
    61
  unfolding bi_total_def set_rel_def
huffman@47648
    62
  apply safe
huffman@47648
    63
  apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
huffman@47648
    64
  apply (rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
huffman@47648
    65
  done
huffman@47648
    66
huffman@47648
    67
lemma bi_unique_set_rel [transfer_rule]:
huffman@47648
    68
  "bi_unique A \<Longrightarrow> bi_unique (set_rel A)"
huffman@47648
    69
  unfolding bi_unique_def set_rel_def by fast
huffman@47648
    70
huffman@47648
    71
subsection {* Transfer rules for transfer package *}
huffman@47648
    72
huffman@47648
    73
subsubsection {* Unconditional transfer rules *}
huffman@47648
    74
huffman@47648
    75
lemma empty_transfer [transfer_rule]: "(set_rel A) {} {}"
huffman@47648
    76
  unfolding set_rel_def by simp
huffman@47648
    77
huffman@47648
    78
lemma insert_transfer [transfer_rule]:
huffman@47648
    79
  "(A ===> set_rel A ===> set_rel A) insert insert"
huffman@47648
    80
  unfolding fun_rel_def set_rel_def by auto
huffman@47648
    81
huffman@47648
    82
lemma union_transfer [transfer_rule]:
huffman@47648
    83
  "(set_rel A ===> set_rel A ===> set_rel A) union union"
huffman@47648
    84
  unfolding fun_rel_def set_rel_def by auto
huffman@47648
    85
huffman@47648
    86
lemma Union_transfer [transfer_rule]:
huffman@47648
    87
  "(set_rel (set_rel A) ===> set_rel A) Union Union"
huffman@47648
    88
  unfolding fun_rel_def set_rel_def by simp fast
huffman@47648
    89
huffman@47648
    90
lemma image_transfer [transfer_rule]:
huffman@47648
    91
  "((A ===> B) ===> set_rel A ===> set_rel B) image image"
huffman@47648
    92
  unfolding fun_rel_def set_rel_def by simp fast
huffman@47648
    93
huffman@47660
    94
lemma UNION_transfer [transfer_rule]:
huffman@47660
    95
  "(set_rel A ===> (A ===> set_rel B) ===> set_rel B) UNION UNION"
huffman@47660
    96
  unfolding SUP_def [abs_def] by transfer_prover
huffman@47660
    97
huffman@47648
    98
lemma Ball_transfer [transfer_rule]:
huffman@47648
    99
  "(set_rel A ===> (A ===> op =) ===> op =) Ball Ball"
huffman@47648
   100
  unfolding set_rel_def fun_rel_def by fast
huffman@47648
   101
huffman@47648
   102
lemma Bex_transfer [transfer_rule]:
huffman@47648
   103
  "(set_rel A ===> (A ===> op =) ===> op =) Bex Bex"
huffman@47648
   104
  unfolding set_rel_def fun_rel_def by fast
huffman@47648
   105
huffman@47648
   106
lemma Pow_transfer [transfer_rule]:
huffman@47648
   107
  "(set_rel A ===> set_rel (set_rel A)) Pow Pow"
huffman@47648
   108
  apply (rule fun_relI, rename_tac X Y, rule set_relI)
huffman@47648
   109
  apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
huffman@47648
   110
  apply (simp add: set_rel_def, fast)
huffman@47648
   111
  apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
huffman@47648
   112
  apply (simp add: set_rel_def, fast)
huffman@47648
   113
  done
huffman@47648
   114
huffman@47922
   115
lemma set_rel_transfer [transfer_rule]:
huffman@47922
   116
  "((A ===> B ===> op =) ===> set_rel A ===> set_rel B ===> op =)
huffman@47922
   117
    set_rel set_rel"
huffman@47922
   118
  unfolding fun_rel_def set_rel_def by fast
huffman@47922
   119
huffman@47648
   120
subsubsection {* Rules requiring bi-unique or bi-total relations *}
huffman@47648
   121
huffman@47648
   122
lemma member_transfer [transfer_rule]:
huffman@47648
   123
  assumes "bi_unique A"
huffman@47648
   124
  shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
huffman@47648
   125
  using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
huffman@47648
   126
huffman@47648
   127
lemma Collect_transfer [transfer_rule]:
huffman@47648
   128
  assumes "bi_total A"
huffman@47648
   129
  shows "((A ===> op =) ===> set_rel A) Collect Collect"
huffman@47648
   130
  using assms unfolding fun_rel_def set_rel_def bi_total_def by fast
huffman@47648
   131
huffman@47648
   132
lemma inter_transfer [transfer_rule]:
huffman@47648
   133
  assumes "bi_unique A"
huffman@47648
   134
  shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
huffman@47648
   135
  using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
huffman@47648
   136
huffman@47680
   137
lemma Diff_transfer [transfer_rule]:
huffman@47680
   138
  assumes "bi_unique A"
huffman@47680
   139
  shows "(set_rel A ===> set_rel A ===> set_rel A) (op -) (op -)"
huffman@47680
   140
  using assms unfolding fun_rel_def set_rel_def bi_unique_def
huffman@47680
   141
  unfolding Ball_def Bex_def Diff_eq
huffman@47680
   142
  by (safe, simp, metis, simp, metis)
huffman@47680
   143
huffman@47648
   144
lemma subset_transfer [transfer_rule]:
huffman@47648
   145
  assumes [transfer_rule]: "bi_unique A"
huffman@47648
   146
  shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
huffman@47648
   147
  unfolding subset_eq [abs_def] by transfer_prover
huffman@47648
   148
huffman@47648
   149
lemma UNIV_transfer [transfer_rule]:
huffman@47648
   150
  assumes "bi_total A"
huffman@47648
   151
  shows "(set_rel A) UNIV UNIV"
huffman@47648
   152
  using assms unfolding set_rel_def bi_total_def by simp
huffman@47648
   153
huffman@47648
   154
lemma Compl_transfer [transfer_rule]:
huffman@47648
   155
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
huffman@47648
   156
  shows "(set_rel A ===> set_rel A) uminus uminus"
huffman@47648
   157
  unfolding Compl_eq [abs_def] by transfer_prover
huffman@47648
   158
huffman@47648
   159
lemma Inter_transfer [transfer_rule]:
huffman@47648
   160
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
huffman@47648
   161
  shows "(set_rel (set_rel A) ===> set_rel A) Inter Inter"
huffman@47648
   162
  unfolding Inter_eq [abs_def] by transfer_prover
huffman@47648
   163
huffman@47648
   164
lemma finite_transfer [transfer_rule]:
huffman@47648
   165
  assumes "bi_unique A"
huffman@47648
   166
  shows "(set_rel A ===> op =) finite finite"
huffman@47648
   167
  apply (rule fun_relI, rename_tac X Y)
huffman@47648
   168
  apply (rule iffI)
huffman@47648
   169
  apply (subgoal_tac "Y \<subseteq> (\<lambda>x. THE y. A x y) ` X")
huffman@47648
   170
  apply (erule finite_subset, erule finite_imageI)
huffman@47648
   171
  apply (rule subsetI, rename_tac y)
huffman@47648
   172
  apply (clarsimp simp add: set_rel_def)
huffman@47648
   173
  apply (drule (1) bspec, clarify)
huffman@47648
   174
  apply (rule image_eqI)
huffman@47648
   175
  apply (rule the_equality [symmetric])
huffman@47648
   176
  apply assumption
huffman@47648
   177
  apply (simp add: assms [unfolded bi_unique_def])
huffman@47648
   178
  apply assumption
huffman@47648
   179
  apply (subgoal_tac "X \<subseteq> (\<lambda>y. THE x. A x y) ` Y")
huffman@47648
   180
  apply (erule finite_subset, erule finite_imageI)
huffman@47648
   181
  apply (rule subsetI, rename_tac x)
huffman@47648
   182
  apply (clarsimp simp add: set_rel_def)
huffman@47648
   183
  apply (drule (1) bspec, clarify)
huffman@47648
   184
  apply (rule image_eqI)
huffman@47648
   185
  apply (rule the_equality [symmetric])
huffman@47648
   186
  apply assumption
huffman@47648
   187
  apply (simp add: assms [unfolded bi_unique_def])
huffman@47648
   188
  apply assumption
huffman@47648
   189
  done
huffman@47648
   190
huffman@47648
   191
subsection {* Setup for lifting package *}
huffman@47648
   192
kuncar@47777
   193
lemma Quotient_set[quot_map]:
huffman@47648
   194
  assumes "Quotient R Abs Rep T"
huffman@47648
   195
  shows "Quotient (set_rel R) (image Abs) (image Rep) (set_rel T)"
huffman@47648
   196
  using assms unfolding Quotient_alt_def4
huffman@47648
   197
  apply (simp add: set_rel_OO set_rel_conversep)
huffman@47648
   198
  apply (simp add: set_rel_def, fast)
huffman@47648
   199
  done
huffman@47648
   200
huffman@47648
   201
lemma set_invariant_commute [invariant_commute]:
huffman@47648
   202
  "set_rel (Lifting.invariant P) = Lifting.invariant (\<lambda>A. Ball A P)"
huffman@47648
   203
  unfolding fun_eq_iff set_rel_def Lifting.invariant_def Ball_def by fast
huffman@47648
   204
huffman@47648
   205
subsection {* Contravariant set map (vimage) and set relator *}
huffman@47626
   206
huffman@47647
   207
definition "vset_rel R xs ys \<equiv> \<forall>x y. R x y \<longrightarrow> x \<in> xs \<longleftrightarrow> y \<in> ys"
huffman@47626
   208
huffman@47647
   209
lemma vset_rel_eq [id_simps]:
huffman@47647
   210
  "vset_rel op = = op ="
huffman@47647
   211
  by (subst fun_eq_iff, subst fun_eq_iff) (simp add: set_eq_iff vset_rel_def)
huffman@47626
   212
huffman@47647
   213
lemma vset_rel_equivp:
huffman@47626
   214
  assumes e: "equivp R"
huffman@47647
   215
  shows "vset_rel R xs ys \<longleftrightarrow> xs = ys \<and> (\<forall>x y. x \<in> xs \<longrightarrow> R x y \<longrightarrow> y \<in> xs)"
huffman@47647
   216
  unfolding vset_rel_def
huffman@47626
   217
  using equivp_reflp[OF e]
huffman@47626
   218
  by auto (metis, metis equivp_symp[OF e])
huffman@47626
   219
kaliszyk@44413
   220
lemma set_quotient [quot_thm]:
kuncar@47308
   221
  assumes "Quotient3 R Abs Rep"
huffman@47647
   222
  shows "Quotient3 (vset_rel R) (vimage Rep) (vimage Abs)"
kuncar@47308
   223
proof (rule Quotient3I)
kuncar@47308
   224
  from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
kaliszyk@44413
   225
  then show "\<And>xs. Rep -` (Abs -` xs) = xs"
kaliszyk@44413
   226
    unfolding vimage_def by auto
kaliszyk@44413
   227
next
huffman@47647
   228
  show "\<And>xs. vset_rel R (Abs -` xs) (Abs -` xs)"
huffman@47647
   229
    unfolding vset_rel_def vimage_def
kuncar@47308
   230
    by auto (metis Quotient3_rel_abs[OF assms])+
kaliszyk@44413
   231
next
kaliszyk@44413
   232
  fix r s
huffman@47647
   233
  show "vset_rel R r s = (vset_rel R r r \<and> vset_rel R s s \<and> Rep -` r = Rep -` s)"
huffman@47647
   234
    unfolding vset_rel_def vimage_def set_eq_iff
kuncar@47308
   235
    by auto (metis rep_abs_rsp[OF assms] assms[simplified Quotient3_def])+
kaliszyk@44413
   236
qed
kaliszyk@44413
   237
huffman@47647
   238
declare [[mapQ3 set = (vset_rel, set_quotient)]]
kuncar@47094
   239
kaliszyk@44413
   240
lemma empty_set_rsp[quot_respect]:
huffman@47647
   241
  "vset_rel R {} {}"
huffman@47647
   242
  unfolding vset_rel_def by simp
kaliszyk@44413
   243
kaliszyk@44413
   244
lemma collect_rsp[quot_respect]:
kuncar@47308
   245
  assumes "Quotient3 R Abs Rep"
huffman@47647
   246
  shows "((R ===> op =) ===> vset_rel R) Collect Collect"
huffman@47647
   247
  by (intro fun_relI) (simp add: fun_rel_def vset_rel_def)
kaliszyk@44413
   248
kaliszyk@44413
   249
lemma collect_prs[quot_preserve]:
kuncar@47308
   250
  assumes "Quotient3 R Abs Rep"
kaliszyk@44413
   251
  shows "((Abs ---> id) ---> op -` Rep) Collect = Collect"
kaliszyk@44413
   252
  unfolding fun_eq_iff
kuncar@47308
   253
  by (simp add: Quotient3_abs_rep[OF assms])
kaliszyk@44413
   254
kaliszyk@44413
   255
lemma union_rsp[quot_respect]:
kuncar@47308
   256
  assumes "Quotient3 R Abs Rep"
huffman@47647
   257
  shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op \<union> op \<union>"
huffman@47647
   258
  by (intro fun_relI) (simp add: vset_rel_def)
kaliszyk@44413
   259
kaliszyk@44413
   260
lemma union_prs[quot_preserve]:
kuncar@47308
   261
  assumes "Quotient3 R Abs Rep"
kaliszyk@44413
   262
  shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op \<union> = op \<union>"
kaliszyk@44413
   263
  unfolding fun_eq_iff
kuncar@47308
   264
  by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]])
kaliszyk@44413
   265
kaliszyk@44413
   266
lemma diff_rsp[quot_respect]:
kuncar@47308
   267
  assumes "Quotient3 R Abs Rep"
huffman@47647
   268
  shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op - op -"
huffman@47647
   269
  by (intro fun_relI) (simp add: vset_rel_def)
kaliszyk@44413
   270
kaliszyk@44413
   271
lemma diff_prs[quot_preserve]:
kuncar@47308
   272
  assumes "Quotient3 R Abs Rep"
kaliszyk@44413
   273
  shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op - = op -"
kaliszyk@44413
   274
  unfolding fun_eq_iff
kuncar@47308
   275
  by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]] vimage_Diff)
kaliszyk@44413
   276
kaliszyk@44413
   277
lemma inter_rsp[quot_respect]:
kuncar@47308
   278
  assumes "Quotient3 R Abs Rep"
huffman@47647
   279
  shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op \<inter> op \<inter>"
huffman@47647
   280
  by (intro fun_relI) (auto simp add: vset_rel_def)
kaliszyk@44413
   281
kaliszyk@44413
   282
lemma inter_prs[quot_preserve]:
kuncar@47308
   283
  assumes "Quotient3 R Abs Rep"
kaliszyk@44413
   284
  shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op \<inter> = op \<inter>"
kaliszyk@44413
   285
  unfolding fun_eq_iff
kuncar@47308
   286
  by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]])
kaliszyk@44413
   287
kaliszyk@44459
   288
lemma mem_prs[quot_preserve]:
kuncar@47308
   289
  assumes "Quotient3 R Abs Rep"
kaliszyk@44459
   290
  shows "(Rep ---> op -` Abs ---> id) op \<in> = op \<in>"
kuncar@47308
   291
  by (simp add: fun_eq_iff Quotient3_abs_rep[OF assms])
kaliszyk@44459
   292
haftmann@45970
   293
lemma mem_rsp[quot_respect]:
huffman@47647
   294
  shows "(R ===> vset_rel R ===> op =) op \<in> op \<in>"
huffman@47647
   295
  by (intro fun_relI) (simp add: vset_rel_def)
kaliszyk@44459
   296
kaliszyk@44413
   297
end