src/HOL/Tools/Sledgehammer/sledgehammer_instantiations.ML
author wenzelm
Sat, 30 Nov 2024 16:01:58 +0100
changeset 81513 d11ed1bf0ad2
parent 81254 d3c0734059ee
child 81757 4d15005da582
permissions -rw-r--r--
tuned signature: more operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
81254
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Metis/metis_instantiations.ML
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     2
    Author:     Lukas Bartl, LMU Muenchen
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     3
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     4
Inference of Variable Instantiations with Metis.
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     5
*)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     6
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     7
signature SLEDGEHAMMER_INSTANTIATIONS =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     8
sig
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
     9
  type stature = ATP_Problem_Generate.stature
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    10
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    11
  val instantiate_facts : Proof.state -> bool -> Time.time -> thm -> int ->
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    12
    (string * stature) list -> (Pretty.T * stature) list option
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    13
end;
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    14
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    15
structure Sledgehammer_Instantiations : SLEDGEHAMMER_INSTANTIATIONS =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    16
struct
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    17
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    18
open ATP_Util
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    19
open ATP_Problem_Generate
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    20
open ATP_Proof_Reconstruct
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    21
open Sledgehammer_Proof_Methods
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    22
open Sledgehammer_Util
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    23
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    24
(* metis with different options, last two with extensionality *)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    25
fun metis_methods ctxt =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    26
  let
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    27
    val ext_facts = [short_thm_name ctxt ext]
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    28
  in
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    29
    [Metis_Method (NONE, NONE, []),
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    30
     Metis_Method (SOME really_full_type_enc, SOME liftingN, []),
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    31
     Metis_Method (SOME no_typesN, SOME liftingN, ext_facts),
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    32
     Metis_Method (SOME full_typesN, SOME liftingN, ext_facts)]
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    33
  end
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    34
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    35
(* Infer variable instantiations using metis with the given options
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    36
 * (cf. tac_of_metis in Sledgehammer_Proof_Methods.tac_of_proof_method) *)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    37
fun infer_thm_insts (Metis_Method (type_enc_opt, lam_trans_opt, additional_fact_names)) thms ctxt =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    38
  let
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    39
    val additional_facts = maps (thms_of_name ctxt) additional_fact_names
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    40
    val ctxt = ctxt
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    41
      |> Config.put Metis_Tactic.verbose false
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    42
      |> Config.put Metis_Tactic.trace false
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    43
  in
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    44
    Metis_Tactic.metis_infer_thm_insts ((Option.map single type_enc_opt, lam_trans_opt),
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    45
      additional_facts @ thms) ctxt
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    46
  end
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    47
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    48
(* Infer variable instantiations of theorems with timeout *)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    49
fun timed_infer_thm_insts ctxt verbose timeout chained goal subgoal fact_thms metis_method =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    50
  let
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    51
    val thms = maps snd fact_thms
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    52
    val timing = Timing.start ()
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    53
    val opt_thm_insts =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    54
      try (Timeout.apply timeout (infer_thm_insts metis_method thms ctxt chained subgoal)) goal
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    55
      |> Option.join
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    56
    val {cpu = time, ...} = Timing.result timing
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    57
    val _ =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    58
      if verbose then
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    59
       (let
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    60
          val meth_str = string_of_proof_method (map (fst o fst) fact_thms) metis_method
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    61
          val time_str = string_of_time time
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    62
        in
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    63
          if is_some opt_thm_insts then
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    64
            writeln ("Inferred variable instantiations with " ^ meth_str ^
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    65
              " (" ^ time_str ^ ")")
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    66
          else
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    67
            writeln ("Could not infer variable instantiations with " ^ meth_str ^
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    68
              " (tried " ^ time_str ^ ")")
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    69
        end)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    70
      else
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    71
        ()
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    72
  in
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    73
    opt_thm_insts
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    74
  end
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    75
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    76
(* Try to infer variable instantiations of facts and instantiate them
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    77
 * using of-instantiations (e.g. "assms(1)[of x]") *)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    78
fun instantiate_facts state verbose timeout goal subgoal facts =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    79
  let
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    80
    val ctxt = Proof.context_of state
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    81
    val {facts = chained, ...} = Proof.goal state
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    82
    val goal = Thm.solve_constraints goal (* avoid exceptions *)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    83
    val fact_thms = AList.make (thms_of_name ctxt o fst) facts
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    84
    val timed_infer_thm_insts =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    85
      timed_infer_thm_insts ctxt verbose timeout chained goal subgoal fact_thms
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    86
    fun infer_thm_insts [] = NONE
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    87
      | infer_thm_insts (metis_method :: metis_methods) =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    88
        (case timed_infer_thm_insts metis_method of
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    89
          NONE => infer_thm_insts metis_methods
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    90
        | thm_insts => thm_insts)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    91
    fun fact_of_thm_inst (th, inst) =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    92
      List.find (fn (_, ths) => member Thm.eq_thm ths th) fact_thms
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    93
      |> Option.map (apfst (rpair inst) o fst)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    94
      |> Option.map (apfst (Metis_Tactic.pretty_name_inst ctxt))
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    95
  in
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    96
    infer_thm_insts (metis_methods ctxt)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    97
    |> Option.map (map_filter fact_of_thm_inst)
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    98
    |> verbose ? tap (Option.app (fn inst_facts =>
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
    99
        Pretty.str "Instantiated facts:" :: map fst inst_facts
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
   100
        |> Pretty.block o Pretty.breaks
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
   101
        |> Pretty.writeln))
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
   102
  end
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
   103
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents:
diff changeset
   104
end;