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