src/HOL/Library/Quotient_Set.thy
author huffman
Sun Apr 22 20:16:30 2012 +0200 (2012-04-22)
changeset 47680 49aa3686e566
parent 47660 7a5c681c0265
child 47777 f29e7dcd7c40
permissions -rw-r--r--
add transfer rule for set difference
     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 subsubsection {* Rules requiring bi-unique or bi-total relations *}
   116 
   117 lemma member_transfer [transfer_rule]:
   118   assumes "bi_unique A"
   119   shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
   120   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
   121 
   122 lemma Collect_transfer [transfer_rule]:
   123   assumes "bi_total A"
   124   shows "((A ===> op =) ===> set_rel A) Collect Collect"
   125   using assms unfolding fun_rel_def set_rel_def bi_total_def by fast
   126 
   127 lemma inter_transfer [transfer_rule]:
   128   assumes "bi_unique A"
   129   shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
   130   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
   131 
   132 lemma Diff_transfer [transfer_rule]:
   133   assumes "bi_unique A"
   134   shows "(set_rel A ===> set_rel A ===> set_rel A) (op -) (op -)"
   135   using assms unfolding fun_rel_def set_rel_def bi_unique_def
   136   unfolding Ball_def Bex_def Diff_eq
   137   by (safe, simp, metis, simp, metis)
   138 
   139 lemma subset_transfer [transfer_rule]:
   140   assumes [transfer_rule]: "bi_unique A"
   141   shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
   142   unfolding subset_eq [abs_def] by transfer_prover
   143 
   144 lemma UNIV_transfer [transfer_rule]:
   145   assumes "bi_total A"
   146   shows "(set_rel A) UNIV UNIV"
   147   using assms unfolding set_rel_def bi_total_def by simp
   148 
   149 lemma Compl_transfer [transfer_rule]:
   150   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   151   shows "(set_rel A ===> set_rel A) uminus uminus"
   152   unfolding Compl_eq [abs_def] by transfer_prover
   153 
   154 lemma Inter_transfer [transfer_rule]:
   155   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   156   shows "(set_rel (set_rel A) ===> set_rel A) Inter Inter"
   157   unfolding Inter_eq [abs_def] by transfer_prover
   158 
   159 lemma finite_transfer [transfer_rule]:
   160   assumes "bi_unique A"
   161   shows "(set_rel A ===> op =) finite finite"
   162   apply (rule fun_relI, rename_tac X Y)
   163   apply (rule iffI)
   164   apply (subgoal_tac "Y \<subseteq> (\<lambda>x. THE y. A x y) ` X")
   165   apply (erule finite_subset, erule finite_imageI)
   166   apply (rule subsetI, rename_tac y)
   167   apply (clarsimp simp add: set_rel_def)
   168   apply (drule (1) bspec, clarify)
   169   apply (rule image_eqI)
   170   apply (rule the_equality [symmetric])
   171   apply assumption
   172   apply (simp add: assms [unfolded bi_unique_def])
   173   apply assumption
   174   apply (subgoal_tac "X \<subseteq> (\<lambda>y. THE x. A x y) ` Y")
   175   apply (erule finite_subset, erule finite_imageI)
   176   apply (rule subsetI, rename_tac x)
   177   apply (clarsimp simp add: set_rel_def)
   178   apply (drule (1) bspec, clarify)
   179   apply (rule image_eqI)
   180   apply (rule the_equality [symmetric])
   181   apply assumption
   182   apply (simp add: assms [unfolded bi_unique_def])
   183   apply assumption
   184   done
   185 
   186 subsection {* Setup for lifting package *}
   187 
   188 lemma Quotient_set:
   189   assumes "Quotient R Abs Rep T"
   190   shows "Quotient (set_rel R) (image Abs) (image Rep) (set_rel T)"
   191   using assms unfolding Quotient_alt_def4
   192   apply (simp add: set_rel_OO set_rel_conversep)
   193   apply (simp add: set_rel_def, fast)
   194   done
   195 
   196 declare [[map set = (set_rel, Quotient_set)]]
   197 
   198 lemma set_invariant_commute [invariant_commute]:
   199   "set_rel (Lifting.invariant P) = Lifting.invariant (\<lambda>A. Ball A P)"
   200   unfolding fun_eq_iff set_rel_def Lifting.invariant_def Ball_def by fast
   201 
   202 subsection {* Contravariant set map (vimage) and set relator *}
   203 
   204 definition "vset_rel R xs ys \<equiv> \<forall>x y. R x y \<longrightarrow> x \<in> xs \<longleftrightarrow> y \<in> ys"
   205 
   206 lemma vset_rel_eq [id_simps]:
   207   "vset_rel op = = op ="
   208   by (subst fun_eq_iff, subst fun_eq_iff) (simp add: set_eq_iff vset_rel_def)
   209 
   210 lemma vset_rel_equivp:
   211   assumes e: "equivp R"
   212   shows "vset_rel R xs ys \<longleftrightarrow> xs = ys \<and> (\<forall>x y. x \<in> xs \<longrightarrow> R x y \<longrightarrow> y \<in> xs)"
   213   unfolding vset_rel_def
   214   using equivp_reflp[OF e]
   215   by auto (metis, metis equivp_symp[OF e])
   216 
   217 lemma set_quotient [quot_thm]:
   218   assumes "Quotient3 R Abs Rep"
   219   shows "Quotient3 (vset_rel R) (vimage Rep) (vimage Abs)"
   220 proof (rule Quotient3I)
   221   from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
   222   then show "\<And>xs. Rep -` (Abs -` xs) = xs"
   223     unfolding vimage_def by auto
   224 next
   225   show "\<And>xs. vset_rel R (Abs -` xs) (Abs -` xs)"
   226     unfolding vset_rel_def vimage_def
   227     by auto (metis Quotient3_rel_abs[OF assms])+
   228 next
   229   fix r s
   230   show "vset_rel R r s = (vset_rel R r r \<and> vset_rel R s s \<and> Rep -` r = Rep -` s)"
   231     unfolding vset_rel_def vimage_def set_eq_iff
   232     by auto (metis rep_abs_rsp[OF assms] assms[simplified Quotient3_def])+
   233 qed
   234 
   235 declare [[mapQ3 set = (vset_rel, set_quotient)]]
   236 
   237 lemma empty_set_rsp[quot_respect]:
   238   "vset_rel R {} {}"
   239   unfolding vset_rel_def by simp
   240 
   241 lemma collect_rsp[quot_respect]:
   242   assumes "Quotient3 R Abs Rep"
   243   shows "((R ===> op =) ===> vset_rel R) Collect Collect"
   244   by (intro fun_relI) (simp add: fun_rel_def vset_rel_def)
   245 
   246 lemma collect_prs[quot_preserve]:
   247   assumes "Quotient3 R Abs Rep"
   248   shows "((Abs ---> id) ---> op -` Rep) Collect = Collect"
   249   unfolding fun_eq_iff
   250   by (simp add: Quotient3_abs_rep[OF assms])
   251 
   252 lemma union_rsp[quot_respect]:
   253   assumes "Quotient3 R Abs Rep"
   254   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op \<union> op \<union>"
   255   by (intro fun_relI) (simp add: vset_rel_def)
   256 
   257 lemma union_prs[quot_preserve]:
   258   assumes "Quotient3 R Abs Rep"
   259   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op \<union> = op \<union>"
   260   unfolding fun_eq_iff
   261   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]])
   262 
   263 lemma diff_rsp[quot_respect]:
   264   assumes "Quotient3 R Abs Rep"
   265   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op - op -"
   266   by (intro fun_relI) (simp add: vset_rel_def)
   267 
   268 lemma diff_prs[quot_preserve]:
   269   assumes "Quotient3 R Abs Rep"
   270   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op - = op -"
   271   unfolding fun_eq_iff
   272   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]] vimage_Diff)
   273 
   274 lemma inter_rsp[quot_respect]:
   275   assumes "Quotient3 R Abs Rep"
   276   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op \<inter> op \<inter>"
   277   by (intro fun_relI) (auto simp add: vset_rel_def)
   278 
   279 lemma inter_prs[quot_preserve]:
   280   assumes "Quotient3 R Abs Rep"
   281   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op \<inter> = op \<inter>"
   282   unfolding fun_eq_iff
   283   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]])
   284 
   285 lemma mem_prs[quot_preserve]:
   286   assumes "Quotient3 R Abs Rep"
   287   shows "(Rep ---> op -` Abs ---> id) op \<in> = op \<in>"
   288   by (simp add: fun_eq_iff Quotient3_abs_rep[OF assms])
   289 
   290 lemma mem_rsp[quot_respect]:
   291   shows "(R ===> vset_rel R ===> op =) op \<in> op \<in>"
   292   by (intro fun_relI) (simp add: vset_rel_def)
   293 
   294 end