src/HOL/Library/Quotient_Set.thy
author huffman
Sat Apr 21 11:02:01 2012 +0200 (2012-04-21)
changeset 47648 6b9d20a095ae
parent 47647 ec29cc09599d
child 47652 1b722b100301
permissions -rw-r--r--
added covariant relator set_rel, with transfer rules for set operations
     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 Ball_transfer [transfer_rule]:
    95   "(set_rel A ===> (A ===> op =) ===> op =) Ball Ball"
    96   unfolding set_rel_def fun_rel_def by fast
    97 
    98 lemma Bex_transfer [transfer_rule]:
    99   "(set_rel A ===> (A ===> op =) ===> op =) Bex Bex"
   100   unfolding set_rel_def fun_rel_def by fast
   101 
   102 lemma Pow_transfer [transfer_rule]:
   103   "(set_rel A ===> set_rel (set_rel A)) Pow Pow"
   104   apply (rule fun_relI, rename_tac X Y, rule set_relI)
   105   apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
   106   apply (simp add: set_rel_def, fast)
   107   apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
   108   apply (simp add: set_rel_def, fast)
   109   done
   110 
   111 subsubsection {* Rules requiring bi-unique or bi-total relations *}
   112 
   113 lemma member_transfer [transfer_rule]:
   114   assumes "bi_unique A"
   115   shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
   116   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
   117 
   118 lemma Collect_transfer [transfer_rule]:
   119   assumes "bi_total A"
   120   shows "((A ===> op =) ===> set_rel A) Collect Collect"
   121   using assms unfolding fun_rel_def set_rel_def bi_total_def by fast
   122 
   123 lemma inter_transfer [transfer_rule]:
   124   assumes "bi_unique A"
   125   shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
   126   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
   127 
   128 lemma subset_transfer [transfer_rule]:
   129   assumes [transfer_rule]: "bi_unique A"
   130   shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
   131   unfolding subset_eq [abs_def] by transfer_prover
   132 
   133 lemma UNIV_transfer [transfer_rule]:
   134   assumes "bi_total A"
   135   shows "(set_rel A) UNIV UNIV"
   136   using assms unfolding set_rel_def bi_total_def by simp
   137 
   138 lemma Compl_transfer [transfer_rule]:
   139   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   140   shows "(set_rel A ===> set_rel A) uminus uminus"
   141   unfolding Compl_eq [abs_def] by transfer_prover
   142 
   143 lemma Inter_transfer [transfer_rule]:
   144   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   145   shows "(set_rel (set_rel A) ===> set_rel A) Inter Inter"
   146   unfolding Inter_eq [abs_def] by transfer_prover
   147 
   148 lemma finite_transfer [transfer_rule]:
   149   assumes "bi_unique A"
   150   shows "(set_rel A ===> op =) finite finite"
   151   apply (rule fun_relI, rename_tac X Y)
   152   apply (rule iffI)
   153   apply (subgoal_tac "Y \<subseteq> (\<lambda>x. THE y. A x y) ` X")
   154   apply (erule finite_subset, erule finite_imageI)
   155   apply (rule subsetI, rename_tac y)
   156   apply (clarsimp simp add: set_rel_def)
   157   apply (drule (1) bspec, clarify)
   158   apply (rule image_eqI)
   159   apply (rule the_equality [symmetric])
   160   apply assumption
   161   apply (simp add: assms [unfolded bi_unique_def])
   162   apply assumption
   163   apply (subgoal_tac "X \<subseteq> (\<lambda>y. THE x. A x y) ` Y")
   164   apply (erule finite_subset, erule finite_imageI)
   165   apply (rule subsetI, rename_tac x)
   166   apply (clarsimp simp add: set_rel_def)
   167   apply (drule (1) bspec, clarify)
   168   apply (rule image_eqI)
   169   apply (rule the_equality [symmetric])
   170   apply assumption
   171   apply (simp add: assms [unfolded bi_unique_def])
   172   apply assumption
   173   done
   174 
   175 subsection {* Setup for lifting package *}
   176 
   177 lemma Quotient_alt_def3:
   178   "Quotient R Abs Rep T \<longleftrightarrow>
   179     (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and>
   180     (\<forall>x y. R x y \<longleftrightarrow> (\<exists>z. T x z \<and> T y z))"
   181   unfolding Quotient_alt_def2 by (safe, metis+)
   182 
   183 lemma Quotient_alt_def4:
   184   "Quotient R Abs Rep T \<longleftrightarrow>
   185     (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and> R = T OO conversep T"
   186   unfolding Quotient_alt_def3 fun_eq_iff by auto
   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