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