
wenzelm@47455: (*  Title:      HOL/Library/Quotient_Set.thy
kaliszyk@44413:     Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@44413: *)
kaliszyk@44413: 
kaliszyk@44413: header {* Quotient infrastructure for the set type *}
kaliszyk@44413: 
kaliszyk@44413: theory Quotient_Set
kaliszyk@44413: imports Main Quotient_Syntax
kaliszyk@44413: begin
kaliszyk@44413: 
huffman@47648: subsection {* Relator for set type *}
huffman@47648: 
huffman@47648: definition set_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
huffman@47648:   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))"
huffman@47648: 
huffman@47648: lemma set_relI:
huffman@47648:   assumes "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. R x y"
huffman@47648:   assumes "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. R x y"
huffman@47648:   shows "set_rel R A B"
huffman@47648:   using assms unfolding set_rel_def by simp
huffman@47648: 
huffman@47648: lemma set_rel_conversep: "set_rel (conversep R) = conversep (set_rel R)"
huffman@47648:   unfolding set_rel_def by auto
huffman@47648: 
huffman@47648: lemma set_rel_OO: "set_rel (R OO S) = set_rel R OO set_rel S"
huffman@47648:   apply (intro ext, rename_tac X Z)
huffman@47648:   apply (rule iffI)
huffman@47648:   apply (rule_tac b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}" in relcomppI)
huffman@47648:   apply (simp add: set_rel_def, fast)
huffman@47648:   apply (simp add: set_rel_def, fast)
huffman@47648:   apply (simp add: set_rel_def, fast)
huffman@47648:   done
huffman@47648: 
huffman@47648: lemma set_rel_eq [relator_eq]: "set_rel (op =) = (op =)"
huffman@47648:   unfolding set_rel_def fun_eq_iff by auto
huffman@47648: 
huffman@47648: lemma reflp_set_rel: "reflp R \<Longrightarrow> reflp (set_rel R)"
huffman@47648:   unfolding reflp_def set_rel_def by fast
huffman@47648: 
huffman@47648: lemma symp_set_rel: "symp R \<Longrightarrow> symp (set_rel R)"
huffman@47648:   unfolding symp_def set_rel_def by fast
huffman@47648: 
huffman@47648: lemma transp_set_rel: "transp R \<Longrightarrow> transp (set_rel R)"
huffman@47648:   unfolding transp_def set_rel_def by fast
huffman@47648: 
huffman@47648: lemma equivp_set_rel: "equivp R \<Longrightarrow> equivp (set_rel R)"
huffman@47648:   by (blast intro: equivpI reflp_set_rel symp_set_rel transp_set_rel
huffman@47648:     elim: equivpE)
huffman@47648: 
huffman@47648: lemma right_total_set_rel [transfer_rule]:
huffman@47648:   "right_total A \<Longrightarrow> right_total (set_rel A)"
huffman@47648:   unfolding right_total_def set_rel_def
huffman@47648:   by (rule allI, rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
huffman@47648: 
huffman@47648: lemma right_unique_set_rel [transfer_rule]:
huffman@47648:   "right_unique A \<Longrightarrow> right_unique (set_rel A)"
huffman@47648:   unfolding right_unique_def set_rel_def by fast
huffman@47648: 
huffman@47648: lemma bi_total_set_rel [transfer_rule]:
huffman@47648:   "bi_total A \<Longrightarrow> bi_total (set_rel A)"
huffman@47648:   unfolding bi_total_def set_rel_def
huffman@47648:   apply safe
huffman@47648:   apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
huffman@47648:   apply (rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
huffman@47648:   done
huffman@47648: 
huffman@47648: lemma bi_unique_set_rel [transfer_rule]:
huffman@47648:   "bi_unique A \<Longrightarrow> bi_unique (set_rel A)"
huffman@47648:   unfolding bi_unique_def set_rel_def by fast
huffman@47648: 
huffman@47648: subsection {* Transfer rules for transfer package *}
huffman@47648: 
huffman@47648: subsubsection {* Unconditional transfer rules *}
huffman@47648: 
huffman@47648: lemma empty_transfer [transfer_rule]: "(set_rel A) {} {}"
huffman@47648:   unfolding set_rel_def by simp
huffman@47648: 
huffman@47648: lemma insert_transfer [transfer_rule]:
huffman@47648:   "(A ===> set_rel A ===> set_rel A) insert insert"
huffman@47648:   unfolding fun_rel_def set_rel_def by auto
huffman@47648: 
huffman@47648: lemma union_transfer [transfer_rule]:
huffman@47648:   "(set_rel A ===> set_rel A ===> set_rel A) union union"
huffman@47648:   unfolding fun_rel_def set_rel_def by auto
huffman@47648: 
huffman@47648: lemma Union_transfer [transfer_rule]:
huffman@47648:   "(set_rel (set_rel A) ===> set_rel A) Union Union"
huffman@47648:   unfolding fun_rel_def set_rel_def by simp fast
huffman@47648: 
huffman@47648: lemma image_transfer [transfer_rule]:
huffman@47648:   "((A ===> B) ===> set_rel A ===> set_rel B) image image"
huffman@47648:   unfolding fun_rel_def set_rel_def by simp fast
huffman@47648: 
huffman@47648: lemma Ball_transfer [transfer_rule]:
huffman@47648:   "(set_rel A ===> (A ===> op =) ===> op =) Ball Ball"
huffman@47648:   unfolding set_rel_def fun_rel_def by fast
huffman@47648: 
huffman@47648: lemma Bex_transfer [transfer_rule]:
huffman@47648:   "(set_rel A ===> (A ===> op =) ===> op =) Bex Bex"
huffman@47648:   unfolding set_rel_def fun_rel_def by fast
huffman@47648: 
huffman@47648: lemma Pow_transfer [transfer_rule]:
huffman@47648:   "(set_rel A ===> set_rel (set_rel A)) Pow Pow"
huffman@47648:   apply (rule fun_relI, rename_tac X Y, rule set_relI)
huffman@47648:   apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
huffman@47648:   apply (simp add: set_rel_def, fast)
huffman@47648:   apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
huffman@47648:   apply (simp add: set_rel_def, fast)
huffman@47648:   done
huffman@47648: 
huffman@47648: subsubsection {* Rules requiring bi-unique or bi-total relations *}
huffman@47648: 
huffman@47648: lemma member_transfer [transfer_rule]:
huffman@47648:   assumes "bi_unique A"
huffman@47648:   shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
huffman@47648:   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
huffman@47648: 
huffman@47648: lemma Collect_transfer [transfer_rule]:
huffman@47648:   assumes "bi_total A"
huffman@47648:   shows "((A ===> op =) ===> set_rel A) Collect Collect"
huffman@47648:   using assms unfolding fun_rel_def set_rel_def bi_total_def by fast
huffman@47648: 
huffman@47648: lemma inter_transfer [transfer_rule]:
huffman@47648:   assumes "bi_unique A"
huffman@47648:   shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
huffman@47648:   using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast
huffman@47648: 
huffman@47648: lemma subset_transfer [transfer_rule]:
huffman@47648:   assumes [transfer_rule]: "bi_unique A"
huffman@47648:   shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
huffman@47648:   unfolding subset_eq [abs_def] by transfer_prover
huffman@47648: 
huffman@47648: lemma UNIV_transfer [transfer_rule]:
huffman@47648:   assumes "bi_total A"
huffman@47648:   shows "(set_rel A) UNIV UNIV"
huffman@47648:   using assms unfolding set_rel_def bi_total_def by simp
huffman@47648: 
huffman@47648: lemma Compl_transfer [transfer_rule]:
huffman@47648:   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
huffman@47648:   shows "(set_rel A ===> set_rel A) uminus uminus"
huffman@47648:   unfolding Compl_eq [abs_def] by transfer_prover
huffman@47648: 
huffman@47648: lemma Inter_transfer [transfer_rule]:
huffman@47648:   assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
huffman@47648:   shows "(set_rel (set_rel A) ===> set_rel A) Inter Inter"
huffman@47648:   unfolding Inter_eq [abs_def] by transfer_prover
huffman@47648: 
huffman@47648: lemma finite_transfer [transfer_rule]:
huffman@47648:   assumes "bi_unique A"
huffman@47648:   shows "(set_rel A ===> op =) finite finite"
huffman@47648:   apply (rule fun_relI, rename_tac X Y)
huffman@47648:   apply (rule iffI)
huffman@47648:   apply (subgoal_tac "Y \<subseteq> (\<lambda>x. THE y. A x y) ` X")
huffman@47648:   apply (erule finite_subset, erule finite_imageI)
huffman@47648:   apply (rule subsetI, rename_tac y)
huffman@47648:   apply (clarsimp simp add: set_rel_def)
huffman@47648:   apply (drule (1) bspec, clarify)
huffman@47648:   apply (rule image_eqI)
huffman@47648:   apply (rule the_equality [symmetric])
huffman@47648:   apply assumption
huffman@47648:   apply (simp add: assms [unfolded bi_unique_def])
huffman@47648:   apply assumption
huffman@47648:   apply (subgoal_tac "X \<subseteq> (\<lambda>y. THE x. A x y) ` Y")
huffman@47648:   apply (erule finite_subset, erule finite_imageI)
huffman@47648:   apply (rule subsetI, rename_tac x)
huffman@47648:   apply (clarsimp simp add: set_rel_def)
huffman@47648:   apply (drule (1) bspec, clarify)
huffman@47648:   apply (rule image_eqI)
huffman@47648:   apply (rule the_equality [symmetric])
huffman@47648:   apply assumption
huffman@47648:   apply (simp add: assms [unfolded bi_unique_def])
huffman@47648:   apply assumption
huffman@47648:   done
huffman@47648: 
huffman@47648: subsection {* Setup for lifting package *}
huffman@47648: 
huffman@47648: lemma Quotient_alt_def3:
huffman@47648:   "Quotient R Abs Rep T \<longleftrightarrow>
huffman@47648:     (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and>
huffman@47648:     (\<forall>x y. R x y \<longleftrightarrow> (\<exists>z. T x z \<and> T y z))"
huffman@47648:   unfolding Quotient_alt_def2 by (safe, metis+)
huffman@47648: 
huffman@47648: lemma Quotient_alt_def4:
huffman@47648:   "Quotient R Abs Rep T \<longleftrightarrow>
huffman@47648:     (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and> R = T OO conversep T"
huffman@47648:   unfolding Quotient_alt_def3 fun_eq_iff by auto
huffman@47648: 
huffman@47648: lemma Quotient_set:
huffman@47648:   assumes "Quotient R Abs Rep T"
huffman@47648:   shows "Quotient (set_rel R) (image Abs) (image Rep) (set_rel T)"
huffman@47648:   using assms unfolding Quotient_alt_def4
huffman@47648:   apply (simp add: set_rel_OO set_rel_conversep)
huffman@47648:   apply (simp add: set_rel_def, fast)
huffman@47648:   done
huffman@47648: 
huffman@47648: declare [[map set = (set_rel, Quotient_set)]]
huffman@47648: 
huffman@47648: lemma set_invariant_commute [invariant_commute]:
huffman@47648:   "set_rel (Lifting.invariant P) = Lifting.invariant (\<lambda>A. Ball A P)"
huffman@47648:   unfolding fun_eq_iff set_rel_def Lifting.invariant_def Ball_def by fast
huffman@47648: 
huffman@47648: subsection {* Contravariant set map (vimage) and set relator *}
huffman@47626: 
huffman@47647: definition "vset_rel R xs ys \<equiv> \<forall>x y. R x y \<longrightarrow> x \<in> xs \<longleftrightarrow> y \<in> ys"
huffman@47626: 
huffman@47647: lemma vset_rel_eq [id_simps]:
huffman@47647:   "vset_rel op = = op ="
huffman@47647:   by (subst fun_eq_iff, subst fun_eq_iff) (simp add: set_eq_iff vset_rel_def)
huffman@47626: 
huffman@47647: lemma vset_rel_equivp:
huffman@47626:   assumes e: "equivp R"
huffman@47647:   shows "vset_rel R xs ys \<longleftrightarrow> xs = ys \<and> (\<forall>x y. x \<in> xs \<longrightarrow> R x y \<longrightarrow> y \<in> xs)"
huffman@47647:   unfolding vset_rel_def
huffman@47626:   using equivp_reflp[OF e]
huffman@47626:   by auto (metis, metis equivp_symp[OF e])
huffman@47626: 
kaliszyk@44413: lemma set_quotient [quot_thm]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
huffman@47647:   shows "Quotient3 (vset_rel R) (vimage Rep) (vimage Abs)"
kuncar@47308: proof (rule Quotient3I)
kuncar@47308:   from assms have "\<And>x. Abs (Rep x) = x" by (rule Quotient3_abs_rep)
kaliszyk@44413:   then show "\<And>xs. Rep -` (Abs -` xs) = xs"
kaliszyk@44413:     unfolding vimage_def by auto
kaliszyk@44413: next
huffman@47647:   show "\<And>xs. vset_rel R (Abs -` xs) (Abs -` xs)"
huffman@47647:     unfolding vset_rel_def vimage_def
kuncar@47308:     by auto (metis Quotient3_rel_abs[OF assms])+
kaliszyk@44413: next
kaliszyk@44413:   fix r s
huffman@47647:   show "vset_rel R r s = (vset_rel R r r \<and> vset_rel R s s \<and> Rep -` r = Rep -` s)"
huffman@47647:     unfolding vset_rel_def vimage_def set_eq_iff
kuncar@47308:     by auto (metis rep_abs_rsp[OF assms] assms[simplified Quotient3_def])+
kaliszyk@44413: qed
kaliszyk@44413: 
huffman@47647: declare [[mapQ3 set = (vset_rel, set_quotient)]]
kuncar@47094: 
kaliszyk@44413: lemma empty_set_rsp[quot_respect]:
huffman@47647:   "vset_rel R {} {}"
huffman@47647:   unfolding vset_rel_def by simp
kaliszyk@44413: 
kaliszyk@44413: lemma collect_rsp[quot_respect]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
huffman@47647:   shows "((R ===> op =) ===> vset_rel R) Collect Collect"
huffman@47647:   by (intro fun_relI) (simp add: fun_rel_def vset_rel_def)
kaliszyk@44413: 
kaliszyk@44413: lemma collect_prs[quot_preserve]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
kaliszyk@44413:   shows "((Abs ---> id) ---> op -` Rep) Collect = Collect"
kaliszyk@44413:   unfolding fun_eq_iff
kuncar@47308:   by (simp add: Quotient3_abs_rep[OF assms])
kaliszyk@44413: 
kaliszyk@44413: lemma union_rsp[quot_respect]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
huffman@47647:   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op \<union> op \<union>"
huffman@47647:   by (intro fun_relI) (simp add: vset_rel_def)
kaliszyk@44413: 
kaliszyk@44413: lemma union_prs[quot_preserve]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
kaliszyk@44413:   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op \<union> = op \<union>"
kaliszyk@44413:   unfolding fun_eq_iff
kuncar@47308:   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]])
kaliszyk@44413: 
kaliszyk@44413: lemma diff_rsp[quot_respect]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
huffman@47647:   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op - op -"
huffman@47647:   by (intro fun_relI) (simp add: vset_rel_def)
kaliszyk@44413: 
kaliszyk@44413: lemma diff_prs[quot_preserve]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
kaliszyk@44413:   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op - = op -"
kaliszyk@44413:   unfolding fun_eq_iff
kuncar@47308:   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]] vimage_Diff)
kaliszyk@44413: 
kaliszyk@44413: lemma inter_rsp[quot_respect]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
huffman@47647:   shows "(vset_rel R ===> vset_rel R ===> vset_rel R) op \<inter> op \<inter>"
huffman@47647:   by (intro fun_relI) (auto simp add: vset_rel_def)
kaliszyk@44413: 
kaliszyk@44413: lemma inter_prs[quot_preserve]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
kaliszyk@44413:   shows "(op -` Abs ---> op -` Abs ---> op -` Rep) op \<inter> = op \<inter>"
kaliszyk@44413:   unfolding fun_eq_iff
kuncar@47308:   by (simp add: Quotient3_abs_rep[OF set_quotient[OF assms]])
kaliszyk@44413: 
kaliszyk@44459: lemma mem_prs[quot_preserve]:
kuncar@47308:   assumes "Quotient3 R Abs Rep"
kaliszyk@44459:   shows "(Rep ---> op -` Abs ---> id) op \<in> = op \<in>"
kuncar@47308:   by (simp add: fun_eq_iff Quotient3_abs_rep[OF assms])
kaliszyk@44459: 
haftmann@45970: lemma mem_rsp[quot_respect]:
huffman@47647:   shows "(R ===> vset_rel R ===> op =) op \<in> op \<in>"
huffman@47647:   by (intro fun_relI) (simp add: vset_rel_def)
kaliszyk@44459: 
kaliszyk@44413: end




