<commit_before>(*  Title:      HOLCF/Tools/pcpodef.ML
    Author:     <NAME>

Primitive domain definitions for HOLCF, similar to Gordon/HOL-style
typedef (see also ~~/src/HOL/Tools/typedef.ML).
*)

signature PCPODEF =
sig
  val pcpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    * (binding * binding) option -> theory -> Proof.state
  val pcpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    * (binding * binding) option -> theory -> Proof.state
  val cpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    * (binding * binding) option -> theory -> Proof.state
  val cpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    * (binding * binding) option -> theory -> Proof.state
end;

structure Pcpodef :> PCPODEF =
struct

(** type definitions **)

(* prepare_cpodef *)

fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));

fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT);
fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);

fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy =
  let
    val _ = Theory.requires thy "Pcpodef" "pcpodefs";
    val ctxt = ProofContext.init thy;

    val full = Sign.full_name thy;
    val full_name = full name;
    val bname = Binding.name_of name;

    (*rhs*)
    val set = prep_term (ctxt |> fold declare_type_name vs) raw_set;
    val setT = Term.fastype_of set;
    val rhs_tfrees = Term.add_tfrees set [];
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
      error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT));

    (*goal*)
    val goal_UU_mem = HOLogic.mk_Trueprop (HOLogic.mk_mem (Const (@{const_name UU}, oldT), set));
    val goal_nonempty =
      HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
    val goal_admissible =
      HOLogic.mk_Trueprop (mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));

    (*lhs*)
    val defS = Sign.defaultS thy;
    val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
    val lhs_sorts = map snd lhs_tfrees;

    val tname = Binding.map_name (Syntax.type_name mx) t;
    val full_tname = full tname;
    val newT = Type (full_tname, map TFree lhs_tfrees);

    val (Rep_name, Abs_name) =
      (case opt_morphs of
        NONE => (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name)
      | SOME morphs => morphs);
    val RepC = Const (full Rep_name, newT --> oldT);
    fun belowC T = Const (@{const_name below}, T --> T --> HOLogic.boolT);
    val below_def = Logic.mk_equals (belowC newT,
      Abs ("x", newT, Abs ("y", newT, belowC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0))));

    fun make_po tac thy1 =
      let
        val ((_, {type_definition, set_def, ...}), thy2) = thy1
          |> Typedef.add_typedef def (SOME name) (t, vs, mx) set opt_morphs tac;
        val lthy3 = thy2
          |> Theory_Target.instantiation ([full_tname], lhs_tfrees, @{sort po});
        val below_def' = Syntax.check_term lthy3 below_def;
        val ((_, (_, below_definition')), lthy4) = lthy3
          |> Specification.definition (NONE,
              ((Binding.prefix_name "below_" (Binding.suffix_name "_def" name), []), below_def'));
        val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy4);
        val below_definition = singleton (ProofContext.export lthy4 ctxt_thy) below_definition';
        val thy5 = lthy4
          |> Class.prove_instantiation_instance
              (K (Tactic.rtac (@{thm typedef_po} OF [type_definition, below_definition]) 1))
          |> LocalTheory.exit_global;
      in ((type_definition, below_definition, set_def), thy5) end;

    fun make_cpo admissible (type_def, below_def, set_def) theory =
      let
        (* FIXME fold_rule might fold user input inintentionally *)
        val admissible' = fold_rule (the_list set_def) admissible;
        val cpo_thms = map (Thm.transfer theory) [type_def, below_def, admissible'];
        val theory' = theory
          |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort cpo})
            (Tactic.rtac (@{thm typedef_cpo} OF cpo_thms) 1);
        val cpo_thms' = map (Thm.transfer theory') cpo_thms;
      in
        theory'
        |> Sign.add_path (Binding.name_of name)
        |> PureThy.add_thms
          ([((Binding.prefix_name "adm_" name, admissible'), []),
            ((Binding.prefix_name "cont_" Rep_name, @{thm typedef_cont_Rep} OF cpo_thms'), []),
            ((Binding.prefix_name "cont_" Abs_name, @{thm typedef_cont_Abs} OF cpo_thms'), []),
            ((Binding.prefix_name "lub_" name, @{thm typedef_lub} OF cpo_thms'), []),
            ((Binding.prefix_name "thelub_" name, @{thm typedef_thelub} OF cpo_thms'), []),
            ((Binding.prefix_name "compact_" name, @{thm typedef_compact} OF cpo_thms'), [])])
        |> snd
        |> Sign.parent_path
      end;

    fun make_pcpo UU_mem (type_def, below_def, set_def) theory =
      let
        (* FIXME fold_rule might fold user input inintentionally *)
        val UU_mem' = fold_rule (the_list set_def) UU_mem;
        val pcpo_thms = map (Thm.transfer theory) [type_def, below_def, UU_mem'];
        val theory' = theory
          |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort pcpo})
            (Tactic.rtac (@{thm typedef_pcpo} OF pcpo_thms) 1);
        val pcpo_thms' = map (Thm.transfer theory') pcpo_thms;
      in
        theory'
        |> Sign.add_path (Binding.name_of name)
        |> PureThy.add_thms
          ([((Binding.suffix_name "_strict" Rep_name, @{thm typedef_Rep_strict} OF pcpo_thms'), []),
            ((Binding.suffix_name "_strict" Abs_name, @{thm typedef_Abs_strict} OF pcpo_thms'), []),
            ((Binding.suffix_name "_strict_iff" Rep_name, @{thm typedef_Rep_strict_iff} OF pcpo_thms'), []),
            ((Binding.suffix_name "_strict_iff" Abs_name, @{thm typedef_Abs_strict_iff} OF pcpo_thms'), []),
            ((Binding.suffix_name "_defined" Rep_name, @{thm typedef_Rep_defined} OF pcpo_thms'), []),
            ((Binding.suffix_name "_defined" Abs_name, @{thm typedef_Abs_defined} OF pcpo_thms'), [])])
        |> snd
        |> Sign.parent_path
      end;

    fun pcpodef_result UU_mem admissible =
      make_po (Tactic.rtac exI 1 THEN Tactic.rtac UU_mem 1)
      #-> (fn defs => make_cpo admissible defs #> make_pcpo UU_mem defs);

    fun cpodef_result nonempty admissible =
      make_po (Tactic.rtac nonempty 1)
      #-> make_cpo admissible;
  in
    if pcpo
    then (goal_UU_mem, goal_admissible, pcpodef_result)
    else (goal_nonempty, goal_admissible, cpodef_result)
  end
  handle ERROR msg =>
    cat_error msg ("The error(s) above occurred in cpodef " ^ quote (Binding.str_of name));


(* proof interface *)

local

fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) thy =
  let
    val (goal1, goal2, make_result) =<commit_msg>use Drule.standard (following typedef package), add pcpodef tactic interface<commit_after>(*  Title:      HOLCF/Tools/pcpodef.ML
    Author:     <NAME>

Primitive domain definitions for HOLCF, similar to Gordon/HOL-style
typedef (see also ~~/src/HOL/Tools/typedef.ML).
*)

signature PCPODEF =
sig
  val add_pcpodef: bool -> binding option -> binding * string list * mixfix ->
    term -> (binding * binding) option -> tactic -> theory -> theory
  val add_cpodef: bool -> binding option -> binding * string list * mixfix ->
    term -> (binding * binding) option -> tactic -> theory -> theory
  val pcpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    * (binding * binding) option -> theory -> Proof.state
  val pcpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    * (binding * binding) option -> theory -> Proof.state
  val cpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    * (binding * binding) option -> theory -> Proof.state
  val cpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    * (binding * binding) option -> theory -> Proof.state
end;

structure Pcpodef :> PCPODEF =
struct

(** type definitions **)

(* prepare_cpodef *)

fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));

fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT);
fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);

fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy =
  let
    val _ = Theory.requires thy "Pcpodef" "pcpodefs";
    val ctxt = ProofContext.init thy;

    val full = Sign.full_name thy;
    val full_name = full name;
    val bname = Binding.name_of name;

    (*rhs*)
    val set = prep_term (ctxt |> fold declare_type_name vs) raw_set;
    val setT = Term.fastype_of set;
    val rhs_tfrees = Term.add_tfrees set [];
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
      error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT));

    (*goal*)
    val goal_UU_mem = HOLogic.mk_Trueprop (HOLogic.mk_mem (Const (@{const_name UU}, oldT), set));
    val goal_nonempty =
      HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
    val goal_admissible =
      HOLogic.mk_Trueprop (mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));

    (*lhs*)
    val defS = Sign.defaultS thy;
    val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
    val lhs_sorts = map snd lhs_tfrees;

    val tname = Binding.map_name (Syntax.type_name mx) t;
    val full_tname = full tname;
    val newT = Type (full_tname, map TFree lhs_tfrees);

    val (Rep_name, Abs_name) =
      (case opt_morphs of
        NONE => (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name)
      | SOME morphs => morphs);
    val RepC = Const (full Rep_name, newT --> oldT);
    fun belowC T = Const (@{const_name below}, T --> T --> HOLogic.boolT);
    val below_def = Logic.mk_equals (belowC newT,
      Abs ("x", newT, Abs ("y", newT, belowC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0))));

    fun make_po tac thy1 =
      let
        val ((_, {type_definition, set_def, ...}), thy2) = thy1
          |> Typedef.add_typedef def (SOME name) (t, vs, mx) set opt_morphs tac;
        val lthy3 = thy2
          |> Theory_Target.instantiation ([full_tname], lhs_tfrees, @{sort po});
        val below_def' = Syntax.check_term lthy3 below_def;
        val ((_, (_, below_definition')), lthy4) = lthy3
          |> Specification.definition (NONE,
              ((Binding.prefix_name "below_" (Binding.suffix_name "_def" name), []), below_def'));
        val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy4);
        val below_definition = singleton (ProofContext.export lthy4 ctxt_thy) below_definition';
        val thy5 = lthy4
          |> Class.prove_instantiation_instance
              (K (Tactic.rtac (@{thm typedef_po} OF [type_definition, below_definition]) 1))
          |> LocalTheory.exit_global;
      in ((type_definition, below_definition, set_def), thy5) end;

    fun make_cpo admissible (type_def, below_def, set_def) theory =
      let
        (* FIXME fold_rule might fold user input inintentionally *)
        val admissible' = fold_rule (the_list set_def) admissible;
        val cpo_thms = map (Thm.transfer theory) [type_def, below_def, admissible'];
        val theory' = theory
          |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort cpo})
            (Tactic.rtac (@{thm typedef_cpo} OF cpo_thms) 1);
        val cpo_thms' = map (Thm.transfer theory') cpo_thms;
        fun make thm = Drule.standard (thm OF cpo_thms');
      in
        theory'
        |> Sign.add_path (Binding.name_of name)
        |> PureThy.add_thms
          ([((Binding.prefix_name "adm_" name, admissible'), []),
            ((Binding.prefix_name "cont_" Rep_name, make @{thm typedef_cont_Rep}), []),
            ((Binding.prefix_name "cont_" Abs_name, make @{thm typedef_cont_Abs}), []),
            ((Binding.prefix_name "lub_" name, make @{thm typedef_lub}), []),
            ((Binding.prefix_name "thelub_" name, make @{thm typedef_thelub}), []),
            ((Binding.prefix_name "compact_" name, make @{thm typedef_compact}), [])])
        |> snd
        |> Sign.parent_path
      end;

    fun make_pcpo UU_mem (type_def, below_def, set_def) theory =
      let
        (* FIXME fold_rule might fold user input inintentionally *)
        val UU_mem' = fold_rule (the_list set_def) UU_mem;
        val pcpo_thms = map (Thm.transfer theory) [type_def, below_def, UU_mem'];
        val theory' = theory
          |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort pcpo})
            (Tactic.rtac (@{thm typedef_pcpo} OF pcpo_thms) 1);
        val pcpo_thms' = map (Thm.transfer theory') pcpo_thms;
        fun make thm = Drule.standard (thm OF pcpo_thms');
      in
        theory'
        |> Sign.add_path (Binding.name_of name)
        |> PureThy.add_thms
          ([((Binding.suffix_name "_strict" Rep_name, make @{thm typedef_Rep_strict}), []),
            ((Binding.suffix_name "_strict" Abs_name, make @{thm typedef_Abs_strict}), []),
            ((Binding.suffix_name "_strict_iff" Rep_name, make @{thm typedef_Rep_strict_iff}), []),
            ((Binding.suffix_name "_strict_iff" Abs_name, make @{thm typedef_Abs_strict_iff}), []),
            ((Binding.suffix_name "_defined" Rep_name, make @{thm typedef_Rep_defined}), []),
            ((Binding.suffix_name "_defined" Abs_name, make @{thm typedef_Abs_defined}), [])])
        |> snd
        |> Sign.parent_path
      end;

    fun pcpodef_result UU_mem admissible =
      make_po (Tactic.rtac exI 1 THEN Tactic.rtac UU_mem 1)
      #-> (fn defs => make_cpo admissible defs #> make_pcpo UU_mem defs);

    fun cpodef_result nonempty admissible =
      make_po (Tactic.rtac nonempty 1)
      #-> make_cpo admissible;
  in
    if pcpo
    then (goal_UU_mem, goal_admissible, pcpodef_result)
    else (goal_nonempty, goal_admissible, cpodef_result)
  end
  handle ERROR msg =>
    cat_error msg ("The error(s) above occurred in cpodef " ^ quote (Binding.str_of name));


(* tactic interface *)

local

fun gen_add_pcpodef pcpo def opt_name typ set opt_morphs tac thy =
  let
    val name = the_default (#1 typ) opt_name;
    val (goal1, goal2, pcpodef_result) =
      prepare_pcpodef Syntax.check_term pcpo def name typ set opt_morphs thy;
    val thm1 = Goal.prove_global thy [] [] goal1 (K tac)
      handle ERROR msg => cat_error msg
        ("Failed to prove non-emptiness of " ^ quote (Syntax.string_of_term_global thy set));
    val thm2 = Goal.prove_global thy [] [] goal2 (K tac)
      handle ERROR msg => cat_error msg
        ("Failed to prove admissibility of " ^ quote (Syntax.string_of_term_global thy set));
  in pcpodef_result thm1 thm2 thy end;

in

val add_pcpodef = gen_add_pcpodef true;
val add_cpodef = gen_add_pcpodef false;

end;

(* proof interface *)

local

fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) thy =
  let
    val (goal1, goal2, make_result) =
{'fraction_non_alphanumeric': 0.12448160094792969, 'fraction_numerical': 0.003620564808110065, 'mean_word_length': 3.07620069761202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, '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': '634074', 'n_tokens_mistral': 5442, 'n_tokens_neox': 5203, 'n_words': 1430}