<commit_before>(*  Title:      HOL/Library/Quotient_Set.thy
    Author:     <NAME> and <NAME>
*)

header {* Quotient infrastructure for the set type *}

theory Quotient_Set
imports Main Quotient_Syntax
begin

subsection {* Relator for set type *}

definition set_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
  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))"

lemma set_relI:
  assumes "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. R x y"
  assumes "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. R x y"
  shows "set_rel R A B"
  using assms unfolding set_rel_def by simp

lemma set_rel_conversep: "set_rel (conversep R) = conversep (set_rel R)"
  unfolding set_rel_def by auto

lemma set_rel_OO: "set_rel (R OO S) = set_rel R OO set_rel S"
  apply (intro ext, rename_tac X Z)
  apply (rule iffI)
  apply (rule_tac b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}" in relcomppI)
  apply (simp add: set_rel_def, fast)
  apply (simp add: set_rel_def, fast)
  apply (simp add: set_rel_def, fast)
  done

lemma set_rel_eq [relator_eq]: "set_rel (op =) = (op =)"
  unfolding set_rel_def fun_eq_iff by auto

lemma reflp_set_rel: "reflp R \<Longrightarrow> reflp (set_rel R)"
  unfolding reflp_def set_rel_def by fast

lemma symp_set_rel: "symp R \<Longrightarrow> symp (set_rel R)"
  unfolding symp_def set_rel_def by fast

lemma transp_set_rel: "transp R \<Longrightarrow> transp (set_rel R)"
  unfolding transp_def set_rel_def by fast

lemma equivp_set_rel: "equivp R \<Longrightarrow> equivp (set_rel R)"
  by (blast intro: equivpI reflp_set_rel symp_set_rel transp_set_rel
    elim: equivpE)

lemma right_total_set_rel [transfer_rule]:
  "right_total A \<Longrightarrow> right_total (set_rel A)"
  unfolding right_total_def set_rel_def
  by (rule allI, rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)

lemma right_unique_set_rel [transfer_rule]:
  "right_unique A \<Longrightarrow> right_unique (set_rel A)"
  unfolding right_unique_def set_rel_def by fast

lemma bi_total_set_rel [transfer_rule]:
  "bi_total A \<Longrightarrow> bi_total (set_rel A)"
  unfolding bi_total_def set_rel_def
  apply safe
  apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
  apply (rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
  done

lemma bi_unique_set_rel [transfer_rule]:
  "bi_unique A \<Longrightarrow> bi_unique (set_rel A)"
  unfolding bi_unique_def set_rel_def by fast

subsection {* Transfer rules for transfer package *}

subsubsection {* Unconditional transfer rules *}

lemma empty_transfer [transfer_rule]: "(set_rel A) {} {}"
  unfolding set_rel_def by simp

lemma insert_transfer [transfer_rule]:
  "(A ===> set_rel A ===> set_rel A) insert insert"
  unfolding fun_rel_def set_rel_def by auto

lemma union_transfer [transfer_rule]:
  "(set_rel A ===> set_rel A ===> set_rel A) union union"
  unfolding fun_rel_def set_rel_def by auto

lemma Union_transfer [transfer_rule]:
  "(set_rel (set_rel A) ===> set_rel A) Union Union"
  unfolding fun_rel_def set_rel_def by simp fast

lemma image_transfer [transfer_rule]:
  "((A ===> B) ===> set_rel A ===> set_rel B) image image"
  unfolding fun_rel_def set_rel_def by simp fast

lemma UNION_transfer [transfer_rule]:
  "(set_rel A ===> (A ===> set_rel B) ===> set_rel B) UNION UNION"
  unfolding SUP_def [abs_def] by transfer_prover

lemma Ball_transfer [transfer_rule]:
  "(set_rel A ===> (A ===> op =) ===> op =) Ball Ball"
  unfolding set_rel_def fun_rel_def by fast

lemma Bex_transfer [transfer_rule]:
  "(set_rel A ===> (A ===> op =) ===> op =) Bex Bex"
  unfolding set_rel_def fun_rel_def by fast

lemma Pow_transfer [transfer_rule]:
  "(set_rel A ===> set_rel (set_rel A)) Pow Pow"
  apply (rule fun_relI, rename_tac X Y, rule set_relI)
  apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
  apply (simp add: set_rel_def, fast)
  apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
  apply (simp add: set_rel_def, fast)
  done

subsubsection {* Rules requiring bi-unique or bi-total relations *}

lemma member_transfer [transfer_rule]:
  assumes "bi_unique A"
  shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
  using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast

lemma Collect_transfer [transfer_rule]:
  assumes "bi_total A"
  shows "((A ===> op =) ===> set_rel A) Collect Collect"
  using assms unfolding fun_rel_def set_rel_def bi_total_def by fast

lemma inter_transfer [transfer_rule]:
  assumes "bi_unique A"
  shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
  using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast

lemma subset_transfer [transfer_rule]:
  assumes [transfer_rule]: "bi_unique A"
  shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
  unfolding subset_eq [abs_def] by transfer_prover

lemma UNIV_transfer [transfer_rule]:
  assumes "bi_total A"
  shows "(set_rel A) UNIV UNIV"
  using assms unfolding set_rel_def bi_total_def by simp

lemma Compl_transfer [transfer_rule]:
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
  shows "(set_rel A ===> set_rel A) uminus uminus"
  unfolding Compl_eq [abs_def] by transfer_prover<commit_msg>add transfer rule for set difference<commit_after>(*  Title:      HOL/Library/Quotient_Set.thy
    Author:     <NAME> and <NAME>
*)

header {* Quotient infrastructure for the set type *}

theory Quotient_Set
imports Main Quotient_Syntax
begin

subsection {* Relator for set type *}

definition set_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool"
  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))"

lemma set_relI:
  assumes "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. R x y"
  assumes "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. R x y"
  shows "set_rel R A B"
  using assms unfolding set_rel_def by simp

lemma set_rel_conversep: "set_rel (conversep R) = conversep (set_rel R)"
  unfolding set_rel_def by auto

lemma set_rel_OO: "set_rel (R OO S) = set_rel R OO set_rel S"
  apply (intro ext, rename_tac X Z)
  apply (rule iffI)
  apply (rule_tac b="{y. (\<exists>x\<in>X. R x y) \<and> (\<exists>z\<in>Z. S y z)}" in relcomppI)
  apply (simp add: set_rel_def, fast)
  apply (simp add: set_rel_def, fast)
  apply (simp add: set_rel_def, fast)
  done

lemma set_rel_eq [relator_eq]: "set_rel (op =) = (op =)"
  unfolding set_rel_def fun_eq_iff by auto

lemma reflp_set_rel: "reflp R \<Longrightarrow> reflp (set_rel R)"
  unfolding reflp_def set_rel_def by fast

lemma symp_set_rel: "symp R \<Longrightarrow> symp (set_rel R)"
  unfolding symp_def set_rel_def by fast

lemma transp_set_rel: "transp R \<Longrightarrow> transp (set_rel R)"
  unfolding transp_def set_rel_def by fast

lemma equivp_set_rel: "equivp R \<Longrightarrow> equivp (set_rel R)"
  by (blast intro: equivpI reflp_set_rel symp_set_rel transp_set_rel
    elim: equivpE)

lemma right_total_set_rel [transfer_rule]:
  "right_total A \<Longrightarrow> right_total (set_rel A)"
  unfolding right_total_def set_rel_def
  by (rule allI, rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)

lemma right_unique_set_rel [transfer_rule]:
  "right_unique A \<Longrightarrow> right_unique (set_rel A)"
  unfolding right_unique_def set_rel_def by fast

lemma bi_total_set_rel [transfer_rule]:
  "bi_total A \<Longrightarrow> bi_total (set_rel A)"
  unfolding bi_total_def set_rel_def
  apply safe
  apply (rename_tac X, rule_tac x="{y. \<exists>x\<in>X. A x y}" in exI, fast)
  apply (rename_tac Y, rule_tac x="{x. \<exists>y\<in>Y. A x y}" in exI, fast)
  done

lemma bi_unique_set_rel [transfer_rule]:
  "bi_unique A \<Longrightarrow> bi_unique (set_rel A)"
  unfolding bi_unique_def set_rel_def by fast

subsection {* Transfer rules for transfer package *}

subsubsection {* Unconditional transfer rules *}

lemma empty_transfer [transfer_rule]: "(set_rel A) {} {}"
  unfolding set_rel_def by simp

lemma insert_transfer [transfer_rule]:
  "(A ===> set_rel A ===> set_rel A) insert insert"
  unfolding fun_rel_def set_rel_def by auto

lemma union_transfer [transfer_rule]:
  "(set_rel A ===> set_rel A ===> set_rel A) union union"
  unfolding fun_rel_def set_rel_def by auto

lemma Union_transfer [transfer_rule]:
  "(set_rel (set_rel A) ===> set_rel A) Union Union"
  unfolding fun_rel_def set_rel_def by simp fast

lemma image_transfer [transfer_rule]:
  "((A ===> B) ===> set_rel A ===> set_rel B) image image"
  unfolding fun_rel_def set_rel_def by simp fast

lemma UNION_transfer [transfer_rule]:
  "(set_rel A ===> (A ===> set_rel B) ===> set_rel B) UNION UNION"
  unfolding SUP_def [abs_def] by transfer_prover

lemma Ball_transfer [transfer_rule]:
  "(set_rel A ===> (A ===> op =) ===> op =) Ball Ball"
  unfolding set_rel_def fun_rel_def by fast

lemma Bex_transfer [transfer_rule]:
  "(set_rel A ===> (A ===> op =) ===> op =) Bex Bex"
  unfolding set_rel_def fun_rel_def by fast

lemma Pow_transfer [transfer_rule]:
  "(set_rel A ===> set_rel (set_rel A)) Pow Pow"
  apply (rule fun_relI, rename_tac X Y, rule set_relI)
  apply (rename_tac X', rule_tac x="{y\<in>Y. \<exists>x\<in>X'. A x y}" in rev_bexI, clarsimp)
  apply (simp add: set_rel_def, fast)
  apply (rename_tac Y', rule_tac x="{x\<in>X. \<exists>y\<in>Y'. A x y}" in rev_bexI, clarsimp)
  apply (simp add: set_rel_def, fast)
  done

subsubsection {* Rules requiring bi-unique or bi-total relations *}

lemma member_transfer [transfer_rule]:
  assumes "bi_unique A"
  shows "(A ===> set_rel A ===> op =) (op \<in>) (op \<in>)"
  using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast

lemma Collect_transfer [transfer_rule]:
  assumes "bi_total A"
  shows "((A ===> op =) ===> set_rel A) Collect Collect"
  using assms unfolding fun_rel_def set_rel_def bi_total_def by fast

lemma inter_transfer [transfer_rule]:
  assumes "bi_unique A"
  shows "(set_rel A ===> set_rel A ===> set_rel A) inter inter"
  using assms unfolding fun_rel_def set_rel_def bi_unique_def by fast

lemma Diff_transfer [transfer_rule]:
  assumes "bi_unique A"
  shows "(set_rel A ===> set_rel A ===> set_rel A) (op -) (op -)"
  using assms unfolding fun_rel_def set_rel_def bi_unique_def
  unfolding Ball_def Bex_def Diff_eq
  by (safe, simp, metis, simp, metis)

lemma subset_transfer [transfer_rule]:
  assumes [transfer_rule]: "bi_unique A"
  shows "(set_rel A ===> set_rel A ===> op =) (op \<subseteq>) (op \<subseteq>)"
  unfolding subset_eq [abs_def] by transfer_prover

lemma UNIV_transfer [transfer_rule]:
  assumes "bi_total A"
  shows "(set_rel A) UNIV UNIV"
  using assms unfolding set_rel_def bi_total_def by simp

lemma Compl_transfer [transfer_rule]:
  assumes [transfer_rule]: "bi_unique A" and [transfer_rule]: "bi_total A"
  shows "(set_rel A ===> set_rel A) uminus uminus"
  unfolding Compl_eq [abs_def] by transfer_prover
{'fraction_non_alphanumeric': 0.12595965006248885, 'fraction_numerical': 0.0, 'mean_word_length': 4.681034482758621, 'pattern_counts': {'":': 0, '<': 115, '<?xml version=': 0, '>': 171, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '104927', 'n_tokens_mistral': 4373, 'n_tokens_neox': 4290, 'n_words': 1297}