src/HOL/TPTP/mash_eval.ML
author blanchet
Wed, 18 Jul 2012 08:44:04 +0200
changeset 49330 82d6e46c673f
parent 49328 0faafdffa662
child 49331 252f45c04042
permissions -rw-r--r--
fixed order of accessibles + other tweaks to MaSh
     1 (*  Title:      HOL/TPTP/mash_eval.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2012
     4 
     5 Evaluate proof suggestions from MaSh (Machine-learning for Sledgehammer).
     6 *)
     7 
     8 signature MASH_EVAL =
     9 sig
    10   type params = Sledgehammer_Provers.params
    11 
    12   val evaluate_mash_suggestions :
    13     Proof.context -> params -> theory -> string -> unit
    14 end;
    15 
    16 structure MaSh_Eval : MASH_EVAL =
    17 struct
    18 
    19 open Sledgehammer_Fact
    20 open Sledgehammer_Filter_MaSh
    21 
    22 val isarN = "Isar"
    23 val iterN = "Iter"
    24 val mashN = "MaSh"
    25 val meshN = "Mesh"
    26 
    27 val max_facts_slack = 2
    28 
    29 fun evaluate_mash_suggestions ctxt params thy file_name =
    30   let
    31     val {provers, max_facts, slice, type_enc, lam_trans, timeout, ...} =
    32       Sledgehammer_Isar.default_params ctxt []
    33     val prover_name = hd provers
    34     val slack_max_facts = max_facts_slack * the max_facts
    35     val path = file_name |> Path.explode
    36     val lines = path |> File.read_lines
    37     val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
    38     val facts = all_facts_of thy css_table
    39     val all_names =
    40       facts |> map snd
    41             |> filter_out (is_likely_tautology orf is_too_meta)
    42             |> map Thm.get_name_hint
    43     val iter_ok = Unsynchronized.ref 0
    44     val mash_ok = Unsynchronized.ref 0
    45     val mesh_ok = Unsynchronized.ref 0
    46     val isar_ok = Unsynchronized.ref 0
    47     fun with_index facts s = (find_index (curry (op =) s) facts + 1, s)
    48     fun index_string (j, s) = s ^ "@" ^ string_of_int j
    49     fun str_of_res label facts {outcome, run_time, used_facts, ...} =
    50       let val facts = facts |> map (fn ((name, _), _) => name ()) in
    51         "  " ^ label ^ ": " ^
    52         (if is_none outcome then
    53            "Success (" ^ ATP_Util.string_from_time run_time ^ "): " ^
    54            (used_facts |> map (with_index facts o fst)
    55                        |> sort (int_ord o pairself fst)
    56                        |> map index_string
    57                        |> space_implode " ") ^
    58            (if length facts < the max_facts then
    59               " (of " ^ string_of_int (length facts) ^ ")"
    60             else
    61               "")
    62          else
    63            "Failure: " ^
    64            (facts |> take (the max_facts) |> tag_list 1
    65                   |> map index_string
    66                   |> space_implode " "))
    67       end
    68     fun solve_goal (j, line) =
    69       let
    70         val (name, suggs) = extract_query line
    71         val th =
    72           case find_first (fn (_, th) => Thm.get_name_hint th = name) facts of
    73             SOME (_, th) => th
    74           | NONE => error ("No fact called \"" ^ name ^ "\"")
    75         val goal = goal_of_thm thy th
    76         val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
    77         val isar_deps = isabelle_dependencies_of all_names th
    78         val facts = facts |> filter (fn (_, th') => thm_ord (th', th) = LESS)
    79         val isar_facts = suggested_facts isar_deps facts
    80         val iter_facts =
    81           Sledgehammer_Filter_Iter.iterative_relevant_facts ctxt params
    82               prover_name slack_max_facts NONE hyp_ts concl_t facts
    83         val mash_facts = suggested_facts suggs facts
    84         val mess = [(iter_facts, SOME (length iter_facts)), (mash_facts, NONE)]
    85         val mesh_facts = mesh_facts slack_max_facts mess
    86         fun prove ok heading facts =
    87           let
    88             val facts =
    89               facts |> Sledgehammer_Fact.maybe_instantiate_inducts ctxt hyp_ts concl_t
    90                     |> take (the max_facts)
    91             val res as {outcome, ...} = run_prover ctxt params facts goal
    92             val _ = if is_none outcome then ok := !ok + 1 else ()
    93           in str_of_res heading facts res end
    94         val iter_s = prove iter_ok iterN iter_facts
    95         val mash_s = prove mash_ok mashN mash_facts
    96         val mesh_s = prove mesh_ok meshN mesh_facts
    97         val isar_s = prove isar_ok isarN isar_facts
    98       in
    99         ["Goal " ^ string_of_int j ^ ": " ^ name, iter_s, mash_s, mesh_s,
   100          isar_s]
   101         |> cat_lines |> tracing
   102       end
   103     fun total_of heading ok n =
   104       " " ^ heading ^ ": " ^ string_of_int (!ok) ^ " (" ^
   105       Real.fmt (StringCvt.FIX (SOME 1))
   106                (100.0 * Real.fromInt (!ok) / Real.fromInt n) ^ "%)"
   107     val inst_inducts = Config.get ctxt Sledgehammer_Fact.instantiate_inducts
   108     val options =
   109       [prover_name, string_of_int (the max_facts) ^ " facts",
   110        "slice" |> not slice ? prefix "dont_", the_default "smart" type_enc,
   111        the_default "smart" lam_trans, ATP_Util.string_from_time timeout,
   112        "instantiate_inducts" |> not inst_inducts ? prefix "dont_"]
   113     val n = length lines
   114   in
   115     tracing " * * *";
   116     tracing ("Options: " ^ commas options);
   117     List.app solve_goal (tag_list 1 lines);
   118     ["Successes (of " ^ string_of_int n ^ " goals)",
   119      total_of iterN iter_ok n,
   120      total_of mashN mash_ok n,
   121      total_of meshN mesh_ok n,
   122      total_of isarN isar_ok n]
   123     |> cat_lines |> tracing
   124   end
   125 
   126 end;