<commit_before>(*  Title:      HOL/Quotient_Examples/Lift_FSet.thy
    Author:     <NAME>, <NAME>
*)

header {* Lifting and transfer with a finite set type *}

theory Lift_FSet
imports "~~/src/HOL/Library/Quotient_List"
begin

subsection {* Equivalence relation and quotient type definition *}

definition list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
  where [simp]: "list_eq xs ys \<longleftrightarrow> set xs = set ys"

lemma reflp_list_eq: "reflp list_eq"
  unfolding reflp_def by simp

lemma symp_list_eq: "symp list_eq"
  unfolding symp_def by simp

lemma transp_list_eq: "transp list_eq"
  unfolding transp_def by simp

lemma equivp_list_eq: "equivp list_eq"
  by (intro equivpI reflp_list_eq symp_list_eq transp_list_eq)

quotient_type 'a fset = "'a list" / "list_eq"
  by (rule equivp_list_eq)

subsection {* Lifted constant definitions *}

lift_definition fnil :: "'a fset" is "[]"
  by simp

lift_definition fcons :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is Cons
  by simp

lift_definition fappend :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is append
  by simp

lift_definition fmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset" is map
  by simp

lift_definition ffilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is filter
  by simp

lift_definition fset :: "'a fset \<Rightarrow> 'a set" is set
  by simp

text {* Constants with nested types (like concat) yield a more
  complicated proof obligation. *}

lemma list_all2_cr_fset:
  "list_all2 cr_fset xs ys \<longleftrightarrow> map abs_fset xs = ys"
  unfolding cr_fset_def
  apply safe
  apply (erule list_all2_induct, simp, simp)
  apply (simp add: list_all2_map2 List.list_all2_refl)
  done

lemma abs_fset_eq_iff: "abs_fset xs = abs_fset ys \<longleftrightarrow> list_eq xs ys"
  using Quotient_rel [OF Quotient_fset] by simp

lift_definition fconcat :: "'a fset fset \<Rightarrow> 'a fset" is concat
proof -
  fix xss yss :: "'a list list"
  assume "(list_all2 cr_fset OO list_eq OO (list_all2 cr_fset)\<inverse>\<inverse>) xss yss"
  then obtain uss vss where
    "list_all2 cr_fset xss uss" and "list_eq uss vss" and
    "list_all2 cr_fset yss vss" by clarsimp
  hence "list_eq (map abs_fset xss) (map abs_fset yss)"
    unfolding list_all2_cr_fset by simp
  thus "list_eq (concat xss) (concat yss)"
    apply (simp add: set_eq_iff image_def)
    apply safe
    apply (rename_tac xs, drule_tac x="abs_fset xs" in spec)
    apply (drule iffD1, fast, clarsimp simp add: abs_fset_eq_iff, fast)
    apply (rename_tac xs, drule_tac x="abs_fset xs" in spec)
    apply (drule iffD2, fast, clarsimp simp add: abs_fset_eq_iff, fast)
    done
qed

text {* Note that the generated transfer rule contains a composition
  of relations. The transfer rule is not yet very useful in this form. *}

lemma "(list_all2 cr_fset OO cr_fset ===> cr_fset) concat fconcat"
  by (fact fconcat.transfer)


subsection {* Using transfer with type @{text "fset"} *}

text {* The correspondence relation @{text "cr_fset"} can only relate
  @{text "list"} and @{text "fset"} types with the same element type.
  To relate nested types like @{text "'a list list"} and
  @{text "'a fset fset"}, we define a parameterized version of the
  correspondence relation, @{text "cr_fset'"}. *}

definition cr_fset' :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b fset \<Rightarrow> bool"
  where "cr_fset' R = list_all2 R OO cr_fset"

lemma right_unique_cr_fset' [transfer_rule]:
  "right_unique A \<Longrightarrow> right_unique (cr_fset' A)"
  unfolding cr_fset'_def
  by (intro right_unique_OO right_unique_list_all2 fset.right_unique)

lemma right_total_cr_fset' [transfer_rule]:
  "right_total A \<Longrightarrow> right_total (cr_fset' A)"
  unfolding cr_fset'_def
  by (intro right_total_OO right_total_list_all2 fset.right_total)

lemma bi_total_cr_fset' [transfer_rule]:
  "bi_total A \<Longrightarrow> bi_total (cr_fset' A)"
  unfolding cr_fset'_def
  by (intro bi_total_OO bi_total_list_all2 fset.bi_total)

text {* Transfer rules for @{text "cr_fset'"} can be derived from the
  existing transfer rules for @{text "cr_fset"} together with the
  transfer rules for the polymorphic raw constants. *}

text {* Note that the proofs below all have a similar structure and
  could potentially be automated. *}

lemma fnil_transfer [transfer_rule]:
  "(cr_fset' A) [] fnil"
  unfolding cr_fset'_def
  apply (rule relcomppI)
  apply (rule Nil_transfer)
  apply (rule fnil.transfer)
  done

lemma fcons_transfer [transfer_rule]:
  "(A ===> cr_fset' A ===> cr_fset' A) Cons fcons"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (rule relcomppI)
  apply (erule (1) Cons_transfer [THEN fun_relD, THEN fun_relD])
  apply (erule fcons.transfer [THEN fun_relD, THEN fun_relD, OF refl])
  done

lemma fappend_transfer [transfer_rule]:
  "(cr_fset' A ===> cr_fset' A ===> cr_fset' A) append fappend"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (rule relcomppI)
  apply (erule (1) append_transfer [THEN fun_relD, THEN fun_relD])
  apply (erule (1) fappend.transfer [THEN fun_relD, THEN fun_relD])
  done

lemma fmap_transfer [transfer_rule]:
  "((A ===> B) ===> cr_fset' A ===> cr_fset' B) map fmap"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (rule relcomppI)
  apply (erule (1) map_transfer [THEN fun_relD, THEN fun_relD])
  apply (erule fmap.transfer [THEN fun_relD, THEN fun_relD, OF refl])
  done

lemma ffilter_transfer [transfer_rule]:
  "((A ===> op =) ===> cr_fset' A ===> cr_fset' A) filter ffilter"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (rule relcomppI)
  apply (erule (1) filter_transfer [THEN fun_relD, THEN fun_relD])
  apply (erule ffilter.transfer [THEN fun_relD, THEN fun_relD, OF refl])
  done

lemma fset_transfer [transfer_rule]:
  "(cr_fset' A ===> set_rel A) set fset"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (drule fset.transfer [THEN fun_relD])
  apply (erule subst)
  apply (erule set_transfer [THEN fun_relD])
  done

lemma fconcat_transfer [transfer_rule]:
  "(cr_fset' (cr_fset' A) ===> cr_fset' A) concat fconcat"
  unfolding cr_fset'_def
  unfolding list_all2_OO
  apply (intro fun_relI)<commit_msg>adapt to changes in generated transfer rules (cf. 4483c004499a)<commit_after>(*  Title:      HOL/Quotient_Examples/Lift_FSet.thy
    Author:     <NAME>, <NAME>
*)

header {* Lifting and transfer with a finite set type *}

theory Lift_FSet
imports "~~/src/HOL/Library/Quotient_List"
begin

subsection {* Equivalence relation and quotient type definition *}

definition list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
  where [simp]: "list_eq xs ys \<longleftrightarrow> set xs = set ys"

lemma reflp_list_eq: "reflp list_eq"
  unfolding reflp_def by simp

lemma symp_list_eq: "symp list_eq"
  unfolding symp_def by simp

lemma transp_list_eq: "transp list_eq"
  unfolding transp_def by simp

lemma equivp_list_eq: "equivp list_eq"
  by (intro equivpI reflp_list_eq symp_list_eq transp_list_eq)

quotient_type 'a fset = "'a list" / "list_eq"
  by (rule equivp_list_eq)

subsection {* Lifted constant definitions *}

lift_definition fnil :: "'a fset" is "[]"
  by simp

lift_definition fcons :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is Cons
  by simp

lift_definition fappend :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is append
  by simp

lift_definition fmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset" is map
  by simp

lift_definition ffilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is filter
  by simp

lift_definition fset :: "'a fset \<Rightarrow> 'a set" is set
  by simp

text {* Constants with nested types (like concat) yield a more
  complicated proof obligation. *}

lemma list_all2_cr_fset:
  "list_all2 cr_fset xs ys \<longleftrightarrow> map abs_fset xs = ys"
  unfolding cr_fset_def
  apply safe
  apply (erule list_all2_induct, simp, simp)
  apply (simp add: list_all2_map2 List.list_all2_refl)
  done

lemma abs_fset_eq_iff: "abs_fset xs = abs_fset ys \<longleftrightarrow> list_eq xs ys"
  using Quotient_rel [OF Quotient_fset] by simp

lift_definition fconcat :: "'a fset fset \<Rightarrow> 'a fset" is concat
proof -
  fix xss yss :: "'a list list"
  assume "(list_all2 cr_fset OO list_eq OO (list_all2 cr_fset)\<inverse>\<inverse>) xss yss"
  then obtain uss vss where
    "list_all2 cr_fset xss uss" and "list_eq uss vss" and
    "list_all2 cr_fset yss vss" by clarsimp
  hence "list_eq (map abs_fset xss) (map abs_fset yss)"
    unfolding list_all2_cr_fset by simp
  thus "list_eq (concat xss) (concat yss)"
    apply (simp add: set_eq_iff image_def)
    apply safe
    apply (rename_tac xs, drule_tac x="abs_fset xs" in spec)
    apply (drule iffD1, fast, clarsimp simp add: abs_fset_eq_iff, fast)
    apply (rename_tac xs, drule_tac x="abs_fset xs" in spec)
    apply (drule iffD2, fast, clarsimp simp add: abs_fset_eq_iff, fast)
    done
qed

text {* Note that the generated transfer rule contains a composition
  of relations. The transfer rule is not yet very useful in this form. *}

lemma "(list_all2 cr_fset OO cr_fset ===> cr_fset) concat fconcat"
  by (fact fconcat.transfer)


subsection {* Using transfer with type @{text "fset"} *}

text {* The correspondence relation @{text "cr_fset"} can only relate
  @{text "list"} and @{text "fset"} types with the same element type.
  To relate nested types like @{text "'a list list"} and
  @{text "'a fset fset"}, we define a parameterized version of the
  correspondence relation, @{text "cr_fset'"}. *}

definition cr_fset' :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b fset \<Rightarrow> bool"
  where "cr_fset' R = list_all2 R OO cr_fset"

lemma right_unique_cr_fset' [transfer_rule]:
  "right_unique A \<Longrightarrow> right_unique (cr_fset' A)"
  unfolding cr_fset'_def
  by (intro right_unique_OO right_unique_list_all2 fset.right_unique)

lemma right_total_cr_fset' [transfer_rule]:
  "right_total A \<Longrightarrow> right_total (cr_fset' A)"
  unfolding cr_fset'_def
  by (intro right_total_OO right_total_list_all2 fset.right_total)

lemma bi_total_cr_fset' [transfer_rule]:
  "bi_total A \<Longrightarrow> bi_total (cr_fset' A)"
  unfolding cr_fset'_def
  by (intro bi_total_OO bi_total_list_all2 fset.bi_total)

text {* Transfer rules for @{text "cr_fset'"} can be derived from the
  existing transfer rules for @{text "cr_fset"} together with the
  transfer rules for the polymorphic raw constants. *}

text {* Note that the proofs below all have a similar structure and
  could potentially be automated. *}

lemma fnil_transfer [transfer_rule]:
  "(cr_fset' A) [] fnil"
  unfolding cr_fset'_def
  apply (rule relcomppI)
  apply (rule Nil_transfer)
  apply (rule fnil.transfer)
  done

lemma fcons_transfer [transfer_rule]:
  "(A ===> cr_fset' A ===> cr_fset' A) Cons fcons"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (rule relcomppI)
  apply (erule (1) Cons_transfer [THEN fun_relD, THEN fun_relD])
  apply (erule fcons.transfer [THEN fun_relD, THEN fun_relD, OF refl])
  done

lemma fappend_transfer [transfer_rule]:
  "(cr_fset' A ===> cr_fset' A ===> cr_fset' A) append fappend"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (rule relcomppI)
  apply (erule (1) append_transfer [THEN fun_relD, THEN fun_relD])
  apply (erule (1) fappend.transfer [THEN fun_relD, THEN fun_relD])
  done

lemma fmap_transfer [transfer_rule]:
  "((A ===> B) ===> cr_fset' A ===> cr_fset' B) map fmap"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (rule relcomppI)
  apply (erule (1) map_transfer [THEN fun_relD, THEN fun_relD])
  apply (erule fmap.transfer [THEN fun_relD, THEN fun_relD, unfolded relator_eq, OF refl])
  done

lemma ffilter_transfer [transfer_rule]:
  "((A ===> op =) ===> cr_fset' A ===> cr_fset' A) filter ffilter"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (rule relcomppI)
  apply (erule (1) filter_transfer [THEN fun_relD, THEN fun_relD])
  apply (erule ffilter.transfer [THEN fun_relD, THEN fun_relD, unfolded relator_eq, OF refl])
  done

lemma fset_transfer [transfer_rule]:
  "(cr_fset' A ===> set_rel A) set fset"
  unfolding cr_fset'_def
  apply (intro fun_relI)
  apply (elim relcomppE)
  apply (drule fset.transfer [THEN fun_relD, unfolded relator_eq])
  apply (erule subst)
  apply (erule set_transfer [THEN fun_relD])
  done

lemma fconcat_transfer [transfer_rule]:
  "(cr_fset' (cr_fset' A) ===> cr_fset' A) concat fconcat"
  unfolding cr_fset'_def
  unfolding list_all2_OO
  apply (intro fun_relI)
{'fraction_non_alphanumeric': 0.10356338793571927, 'fraction_numerical': 0.004502755997205186, 'mean_word_length': 4.615518744551003, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 87, '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': '1354127', 'n_tokens_mistral': 5039, 'n_tokens_neox': 4846, 'n_words': 1521}