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