src/HOL/TPTP/mash_eval.ML
author blanchet
Fri, 20 Jul 2012 22:19:45 +0200
changeset 49393 9e96486d53ad
parent 49348 2250197977dc
child 49394 2b5ad61e2ccc
permissions -rw-r--r--
handle local facts smoothly in 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 val all_names =
    30   filter_out is_likely_tautology_or_too_meta
    31   #> map (rpair () o nickname_of) #> Symtab.make
    32 
    33 fun evaluate_mash_suggestions ctxt params thy file_name =
    34   let
    35     val {provers, max_facts, slice, type_enc, lam_trans, timeout, ...} =
    36       Sledgehammer_Isar.default_params ctxt []
    37     val prover = hd provers
    38     val slack_max_facts = max_facts_slack * the max_facts
    39     val path = file_name |> Path.explode
    40     val lines = path |> File.read_lines
    41     val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
    42     val facts = all_facts_of thy css_table
    43     val all_names = all_names (facts |> map snd)
    44     val iter_ok = Unsynchronized.ref 0
    45     val mash_ok = Unsynchronized.ref 0
    46     val mesh_ok = Unsynchronized.ref 0
    47     val isar_ok = Unsynchronized.ref 0
    48     fun with_index facts s = (find_index (curry (op =) s) facts + 1, s)
    49     fun index_string (j, s) = s ^ "@" ^ string_of_int j
    50     fun str_of_res label facts {outcome, run_time, used_facts, ...} =
    51       let val facts = facts |> map (fn ((name, _), _) => name ()) in
    52         "  " ^ label ^ ": " ^
    53         (if is_none outcome then
    54            "Success (" ^ ATP_Util.string_from_time run_time ^ "): " ^
    55            (used_facts |> map (with_index facts o fst)
    56                        |> sort (int_ord o pairself fst)
    57                        |> map index_string
    58                        |> space_implode " ") ^
    59            (if length facts < the max_facts then
    60               " (of " ^ string_of_int (length facts) ^ ")"
    61             else
    62               "")
    63          else
    64            "Failure: " ^
    65            (facts |> take (the max_facts) |> tag_list 1
    66                   |> map index_string
    67                   |> space_implode " "))
    68       end
    69     fun solve_goal (j, line) =
    70       let
    71         val (name, suggs) = extract_query line
    72         val th =
    73           case find_first (fn (_, th) => nickname_of th = name) facts of
    74             SOME (_, th) => th
    75           | NONE => error ("No fact called \"" ^ name ^ "\"")
    76         val goal = goal_of_thm thy th
    77         val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
    78         val isar_deps = isabelle_dependencies_of all_names th
    79         val facts = facts |> filter (fn (_, th') => thm_ord (th', th) = LESS)
    80         val isar_facts = suggested_facts isar_deps facts
    81         val iter_facts =
    82           Sledgehammer_Filter_Iter.iterative_relevant_facts ctxt params
    83               prover slack_max_facts NONE hyp_ts concl_t facts
    84         val mash_facts = facts |> suggested_facts suggs
    85         val mess = [(iter_facts, []), (mash_facts, [])]
    86         val mesh_facts = mesh_facts slack_max_facts mess
    87         fun prove ok heading facts =
    88           let
    89             val facts =
    90               facts
    91               |> Sledgehammer_Fact.maybe_instantiate_inducts ctxt hyp_ts concl_t
    92               |> take (the max_facts)
    93             val res as {outcome, ...} =
    94               run_prover_for_mash ctxt params prover facts goal
    95             val _ = if is_none outcome then ok := !ok + 1 else ()
    96           in str_of_res heading facts res end
    97         val iter_s = prove iter_ok iterN iter_facts
    98         val mash_s = prove mash_ok mashN mash_facts
    99         val mesh_s = prove mesh_ok meshN mesh_facts
   100         val isar_s = prove isar_ok isarN isar_facts
   101       in
   102         ["Goal " ^ string_of_int j ^ ": " ^ name, iter_s, mash_s, mesh_s,
   103          isar_s]
   104         |> cat_lines |> tracing
   105       end
   106     fun total_of heading ok n =
   107       " " ^ heading ^ ": " ^ string_of_int (!ok) ^ " (" ^
   108       Real.fmt (StringCvt.FIX (SOME 1))
   109                (100.0 * Real.fromInt (!ok) / Real.fromInt n) ^ "%)"
   110     val inst_inducts = Config.get ctxt Sledgehammer_Fact.instantiate_inducts
   111     val options =
   112       [prover, string_of_int (the max_facts) ^ " facts",
   113        "slice" |> not slice ? prefix "dont_", the_default "smart" type_enc,
   114        the_default "smart" lam_trans, ATP_Util.string_from_time timeout,
   115        "instantiate_inducts" |> not inst_inducts ? prefix "dont_"]
   116     val n = length lines
   117   in
   118     tracing " * * *";
   119     tracing ("Options: " ^ commas options);
   120     List.app solve_goal (tag_list 1 lines);
   121     ["Successes (of " ^ string_of_int n ^ " goals)",
   122      total_of iterN iter_ok n,
   123      total_of mashN mash_ok n,
   124      total_of meshN mesh_ok n,
   125      total_of isarN isar_ok n]
   126     |> cat_lines |> tracing
   127   end
   128 
   129 end;