src/HOL/Library/Quotient_Set.thy
author huffman
Mon Jun 10 06:08:14 2013 -0700 (2013-06-10)
changeset 52359 0eafa146b399
parent 51994 82cc2aeb7d13
child 53012 cb82606b8215
permissions -rw-r--r--
more transfer rules for sets
     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_eq [relator_eq]: "set_rel (op =) = (op =)"
    26   unfolding set_rel_def fun_eq_iff by auto
    27 
    28 lemma set_rel_mono[relator_mono]:
    29   assumes "A \<le> B"
    30   shows "set_rel A \<le> set_rel B"
    31 using assms unfolding set_rel_def by blast
    32 
    33 lemma set_rel_OO[relator_distr]: "set_rel R OO set_rel S = set_rel (R OO S)"
    34   apply (rule sym)
    35   apply (intro ext, rename_tac X Z)
    36   apply (rule iffI)
    37   apply (rule_tac b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}" in relcomppI)
    38   apply (simp add: set_rel_def, fast)
    39   apply (simp add: set_rel_def, fast)
    40   apply (simp add: set_rel_def, fast)
    41   done
    42 
    43 lemma Domainp_set[relator_domain]:
    44   assumes "Domainp T = R"
    45   shows "Domainp (set_rel T) = (\<lambda>A. Ball A R)"
    46 using assms unfolding set_rel_def Domainp_iff[abs_def]
    47 apply (intro ext)
    48 apply (rule iffI) 
    49 apply blast
    50 apply (rename_tac A, rule_tac x="{y. \<exists>x\<in>A. T x y}" in exI, fast)
    51 done
    52 
    53 lemma reflp_set_rel[reflexivity_rule]: "reflp R \<Longrightarrow> reflp (set_rel R)"
    54   unfolding reflp_def set_rel_def by fast
    55 
    56 lemma left_total_set_rel[reflexivity_rule]: 
    57   "left_total A \<Longrightarrow> left_total (set_rel A)"
    58   unfolding left_total_def set_rel_def
    59   apply safe
    60   apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
    61 done
    62 
    63 lemma left_unique_set_rel[reflexivity_rule]: 
    64   "left_unique A \<Longrightarrow> left_unique (set_rel A)"
    65   unfolding left_unique_def set_rel_def
    66   by fast
    67 
    68 lemma symp_set_rel: "symp R \<Longrightarrow> symp (set_rel R)"
    69   unfolding symp_def set_rel_def by fast
    70 
    71 lemma transp_set_rel: "transp R \<Longrightarrow> transp (set_rel R)"
    72   unfolding transp_def set_rel_def by fast
    73 
    74 lemma equivp_set_rel: "equivp R \<Longrightarrow> equivp (set_rel R)"
    75   by (blast intro: equivpI reflp_set_rel symp_set_rel transp_set_rel
    76     elim: equivpE)
    77 
    78 lemma right_total_set_rel [transfer_rule]:
    79   "right_total A \<Longrightarrow> right_total (set_rel A)"
    80   unfolding right_total_def set_rel_def
    81   by (rule allI, rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
    82 
    83 lemma right_unique_set_rel [transfer_rule]:
    84   "right_unique A \<Longrightarrow> right_unique (set_rel A)"
    85   unfolding right_unique_def set_rel_def by fast
    86 
    87 lemma bi_total_set_rel [transfer_rule]:
    88   "bi_total A \<Longrightarrow> bi_total (set_rel A)"
    89   unfolding bi_total_def set_rel_def
    90   apply safe
    91   apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
    92   apply (rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
    93   done
    94 
    95 lemma bi_unique_set_rel [transfer_rule]:
    96   "bi_unique A \<Longrightarrow> bi_unique (set_rel A)"
    97   unfolding bi_unique_def set_rel_def by fast
    98 
    99 subsection {* Transfer rules for transfer package *}
   100 
   101 subsubsection {* Unconditional transfer rules *}
   102 
   103 lemma empty_transfer [transfer_rule]: "(set_rel A) {} {}"
   104   unfolding set_rel_def by simp
   105 
   106 lemma insert_transfer [transfer_rule]:
   107   "(A ===> set_rel A ===> set_rel A) insert insert"
   108   unfolding fun_rel_def set_rel_def by auto
   109 
   110 lemma union_transfer [transfer_rule]:
   111   "(set_rel A ===> set_rel A ===> set_rel A) union union"
   112   unfolding fun_rel_def set_rel_def by auto
   113 
   114 lemma Union_transfer [transfer_rule]:
   115   "(set_rel (set_rel A) ===> set_rel A) Union Union"
   116   unfolding fun_rel_def set_rel_def by simp fast
   117 
   118 lemma image_transfer [transfer_rule]:
   119   "((A ===> B) ===> set_rel A ===> set_rel B) image image"
   120   unfolding fun_rel_def set_rel_def by simp fast
   121 
   122 lemma UNION_transfer [transfer_rule]:
   123   "(set_rel A ===> (A ===> set_rel B) ===> set_rel B) UNION UNION"
   124   unfolding SUP_def [abs_def] by transfer_prover
   125 
   126 lemma Ball_transfer [transfer_rule]:
   127   "(set_rel A ===> (A ===> op =) ===> op =) Ball Ball"
   128   unfolding set_rel_def fun_rel_def by fast
   129 
   130 lemma Bex_transfer [transfer_rule]:
   131   "(set_rel A ===> (A ===> op =) ===> op =) Bex Bex"
   132   unfolding set_rel_def fun_rel_def by fast
   133 
   134 lemma Pow_transfer [transfer_rule]:
   135   "(set_rel A ===> set_rel (set_rel A)) Pow Pow"
   136   apply (rule fun_relI, rename_tac X Y, rule set_relI)
   137   apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
   138   apply (simp add: set_rel_def, fast)
   139   apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
   140   apply (simp add: set_rel_def, fast)
   141   done
   142 
   143 lemma set_rel_transfer [transfer_rule]:
   144   "((A ===> B ===> op =) ===> set_rel A ===> set_rel B ===> op =)
   145     set_rel set_rel"
   146   unfolding fun_rel_def set_rel_def by fast
   147 
   148 
   149 subsubsection {* Rules requiring bi-unique, bi-total or right-total relations *}
   150 
   151 lemma member_transfer [transfer_rule]:
   152   assumes "bi_unique A"
   153   shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
   154   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
   155 
   156 lemma right_total_Collect_transfer[transfer_rule]:
   157   assumes "right_total A"
   158   shows "((A ===> op =) ===> set_rel A) (\<lambda>P. Collect (\<lambda>x. P x \<and> Domainp A x)) Collect"
   159   using assms unfolding right_total_def set_rel_def fun_rel_def Domainp_iff by fast
   160 
   161 lemma Collect_transfer [transfer_rule]:
   162   assumes "bi_total A"
   163   shows "((A ===> op =) ===> set_rel A) Collect Collect"
   164   using assms unfolding fun_rel_def set_rel_def bi_total_def by fast
   165 
   166 lemma inter_transfer [transfer_rule]:
   167   assumes "bi_unique A"
   168   shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
   169   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
   170 
   171 lemma Diff_transfer [transfer_rule]:
   172   assumes "bi_unique A"
   173   shows "(set_rel A ===> set_rel A ===> set_rel A) (op -) (op -)"
   174   using assms unfolding fun_rel_def set_rel_def bi_unique_def
   175   unfolding Ball_def Bex_def Diff_eq
   176   by (safe, simp, metis, simp, metis)
   177 
   178 lemma subset_transfer [transfer_rule]:
   179   assumes [transfer_rule]: "bi_unique A"
   180   shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
   181   unfolding subset_eq [abs_def] by transfer_prover
   182 
   183 lemma right_total_UNIV_transfer[transfer_rule]: 
   184   assumes "right_total A"
   185   shows "(set_rel A) (Collect (Domainp A)) UNIV"
   186   using assms unfolding right_total_def set_rel_def Domainp_iff by blast
   187 
   188 lemma UNIV_transfer [transfer_rule]:
   189   assumes "bi_total A"
   190   shows "(set_rel A) UNIV UNIV"
   191   using assms unfolding set_rel_def bi_total_def by simp
   192 
   193 lemma right_total_Compl_transfer [transfer_rule]:
   194   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
   195   shows "(set_rel A ===> set_rel A) (\<lambda>S. uminus S \<inter> Collect (Domainp A)) uminus"
   196   unfolding Compl_eq [abs_def]
   197   by (subst Collect_conj_eq[symmetric]) transfer_prover
   198 
   199 lemma Compl_transfer [transfer_rule]:
   200   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   201   shows "(set_rel A ===> set_rel A) uminus uminus"
   202   unfolding Compl_eq [abs_def] by transfer_prover
   203 
   204 lemma right_total_Inter_transfer [transfer_rule]:
   205   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "right_total A"
   206   shows "(set_rel (set_rel A) ===> set_rel A) (\<lambda>S. Inter S \<inter> Collect (Domainp A)) Inter"
   207   unfolding Inter_eq[abs_def]
   208   by (subst Collect_conj_eq[symmetric]) transfer_prover
   209 
   210 lemma Inter_transfer [transfer_rule]:
   211   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
   212   shows "(set_rel (set_rel A) ===> set_rel A) Inter Inter"
   213   unfolding Inter_eq [abs_def] by transfer_prover
   214 
   215 lemma filter_transfer [transfer_rule]:
   216   assumes [transfer_rule]: "bi_unique A"
   217   shows "((A ===> op=) ===> set_rel A ===> set_rel A) Set.filter Set.filter"
   218   unfolding Set.filter_def[abs_def] fun_rel_def set_rel_def by blast
   219 
   220 lemma bi_unique_set_rel_lemma:
   221   assumes "bi_unique R" and "set_rel R X Y"
   222   obtains f where "Y = image f X" and "inj_on f X" and "\<forall>x\<in>X. R x (f x)"
   223 proof
   224   let ?f = "\<lambda>x. THE y. R x y"
   225   from assms show f: "\<forall>x\<in>X. R x (?f x)"
   226     apply (clarsimp simp add: set_rel_def)
   227     apply (drule (1) bspec, clarify)
   228     apply (rule theI2, assumption)
   229     apply (simp add: bi_unique_def)
   230     apply assumption
   231     done
   232   from assms show "Y = image ?f X"
   233     apply safe
   234     apply (clarsimp simp add: set_rel_def)
   235     apply (drule (1) bspec, clarify)
   236     apply (rule image_eqI)
   237     apply (rule the_equality [symmetric], assumption)
   238     apply (simp add: bi_unique_def)
   239     apply assumption
   240     apply (clarsimp simp add: set_rel_def)
   241     apply (frule (1) bspec, clarify)
   242     apply (rule theI2, assumption)
   243     apply (clarsimp simp add: bi_unique_def)
   244     apply (simp add: bi_unique_def, metis)
   245     done
   246   show "inj_on ?f X"
   247     apply (rule inj_onI)
   248     apply (drule f [rule_format])
   249     apply (drule f [rule_format])
   250     apply (simp add: assms(1) [unfolded bi_unique_def])
   251     done
   252 qed
   253 
   254 lemma finite_transfer [transfer_rule]:
   255   "bi_unique A \<Longrightarrow> (set_rel A ===> op =) finite finite"
   256   by (rule fun_relI, erule (1) bi_unique_set_rel_lemma,
   257     auto dest: finite_imageD)
   258 
   259 lemma card_transfer [transfer_rule]:
   260   "bi_unique A \<Longrightarrow> (set_rel A ===> op =) card card"
   261   by (rule fun_relI, erule (1) bi_unique_set_rel_lemma, simp add: card_image)
   262 
   263 subsection {* Setup for lifting package *}
   264 
   265 lemma Quotient_set[quot_map]:
   266   assumes "Quotient R Abs Rep T"
   267   shows "Quotient (set_rel R) (image Abs) (image Rep) (set_rel T)"
   268   using assms unfolding Quotient_alt_def4
   269   apply (simp add: set_rel_OO[symmetric] set_rel_conversep)
   270   apply (simp add: set_rel_def, fast)
   271   done
   272 
   273 lemma set_invariant_commute [invariant_commute]:
   274   "set_rel (Lifting.invariant P) = Lifting.invariant (\<lambda>A. Ball A P)"
   275   unfolding fun_eq_iff set_rel_def Lifting.invariant_def Ball_def by fast
   276 
   277 subsection {* Contravariant set map (vimage) and set relator *}
   278 
   279 definition "vset_rel R xs ys \<equiv> \<forall>x y. R x y \<longrightarrow> x \<in> xs \<longleftrightarrow> y \<in> ys"
   280 
   281 lemma vset_rel_eq [id_simps]:
   282   "vset_rel op = = op ="
   283   by (subst fun_eq_iff, subst fun_eq_iff) (simp add: set_eq_iff vset_rel_def)
   284 
   285 lemma vset_rel_equivp:
   286   assumes e: "equivp R"
   287   shows "vset_rel R xs ys \<longleftrightarrow> xs = ys \<and> (\<forall>x y. x \<in> xs \<longrightarrow> R x y \<longrightarrow> y \<in> xs)"
   288   unfolding vset_rel_def
   289   using equivp_reflp[OF e]
   290   by auto (metis, metis equivp_symp[OF e])
   291 
   292 lemma set_quotient [quot_thm]:
   293   assumes "Quotient3 R Abs Rep"
   294   shows "Quotient3 (vset_rel R) (vimage Rep) (vimage Abs)"
   295 proof (rule Quotient3I)
   296   from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
   297   then show "\<And>xs. Rep -` (Abs -` xs) = xs"
   298     unfolding vimage_def by auto
   299 next
   300   show "\<And>xs. vset_rel R (Abs -` xs) (Abs -` xs)"
   301     unfolding vset_rel_def vimage_def
   302     by auto (metis Quotient3_rel_abs[OF assms])+
   303 next
   304   fix r s
   305   show "vset_rel R r s = (vset_rel R r r \<and> vset_rel R s s \<and> Rep -` r = Rep -` s)"
   306     unfolding vset_rel_def vimage_def set_eq_iff
   307     by auto (metis rep_abs_rsp[OF assms] assms[simplified Quotient3_def])+
   308 qed
   309 
   310 declare [[mapQ3 set = (vset_rel, set_quotient)]]
   311 
   312 lemma empty_set_rsp[quot_respect]:
   313   "vset_rel R {} {}"
   314   unfolding vset_rel_def by simp
   315 
   316 lemma collect_rsp[quot_respect]:
   317   assumes "Quotient3 R Abs Rep"
   318   shows "((R ===> op =) ===> vset_rel R) Collect Collect"
   319   by (intro fun_relI) (simp add: fun_rel_def vset_rel_def)
   320 
   321 lemma collect_prs[quot_preserve]:
   322   assumes "Quotient3 R Abs Rep"
   323   shows "((Abs ---> id) ---> op -` Rep) Collect = Collect"
   324   unfolding fun_eq_iff
   325   by (simp add: Quotient3_abs_rep[OF assms])
   326 
   327 lemma union_rsp[quot_respect]:
   328   assumes "Quotient3 R Abs Rep"
   329   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op \<union> op \<union>"
   330   by (intro fun_relI) (simp add: vset_rel_def)
   331 
   332 lemma union_prs[quot_preserve]:
   333   assumes "Quotient3 R Abs Rep"
   334   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op \<union> = op \<union>"
   335   unfolding fun_eq_iff
   336   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]])
   337 
   338 lemma diff_rsp[quot_respect]:
   339   assumes "Quotient3 R Abs Rep"
   340   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op - op -"
   341   by (intro fun_relI) (simp add: vset_rel_def)
   342 
   343 lemma diff_prs[quot_preserve]:
   344   assumes "Quotient3 R Abs Rep"
   345   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op - = op -"
   346   unfolding fun_eq_iff
   347   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]] vimage_Diff)
   348 
   349 lemma inter_rsp[quot_respect]:
   350   assumes "Quotient3 R Abs Rep"
   351   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op \<inter> op \<inter>"
   352   by (intro fun_relI) (auto simp add: vset_rel_def)
   353 
   354 lemma inter_prs[quot_preserve]:
   355   assumes "Quotient3 R Abs Rep"
   356   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op \<inter> = op \<inter>"
   357   unfolding fun_eq_iff
   358   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]])
   359 
   360 lemma mem_prs[quot_preserve]:
   361   assumes "Quotient3 R Abs Rep"
   362   shows "(Rep ---> op -` Abs ---> id) op \<in> = op \<in>"
   363   by (simp add: fun_eq_iff Quotient3_abs_rep[OF assms])
   364 
   365 lemma mem_rsp[quot_respect]:
   366   shows "(R ===> vset_rel R ===> op =) op \<in> op \<in>"
   367   by (intro fun_relI) (simp add: vset_rel_def)
   368 
   369 end