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