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