src/HOL/TPTP/mash_eval.ML
author blanchet
Wed Dec 05 13:25:06 2012 +0100 (2012-12-05 ago)
changeset 50383 4274b25ff4e7
parent 50358 b7d3319409b7
child 50412 e83ab94e3e6e
permissions -rw-r--r--
take proximity into account for MaSh + fix a debilitating bug in feature generation
     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 : Proof.context -> params -> string -> unit
    13 end;
    14 
    15 structure MaSh_Eval : MASH_EVAL =
    16 struct
    17 
    18 open Sledgehammer_Fact
    19 open Sledgehammer_MaSh
    20 
    21 val MePoN = "MePo"
    22 val MaShN = "MaSh"
    23 val MeshN = "Mesh"
    24 val IsarN = "Isar"
    25 
    26 val max_facts_slack = 2
    27 
    28 val all_names = map (rpair () o nickname_of) #> Symtab.make
    29 
    30 fun evaluate_mash_suggestions ctxt params file_name =
    31   let
    32     val {provers, max_facts, slice, type_enc, lam_trans, timeout, ...} =
    33       Sledgehammer_Isar.default_params ctxt []
    34     val prover = hd provers
    35     val slack_max_facts = max_facts_slack * the max_facts
    36     val path = file_name |> Path.explode
    37     val lines = path |> File.read_lines
    38     val css = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
    39     val facts = all_facts ctxt false Symtab.empty [] [] css
    40     val all_names = all_names (facts |> map snd)
    41     val mepo_ok = Unsynchronized.ref 0
    42     val mash_ok = Unsynchronized.ref 0
    43     val mesh_ok = Unsynchronized.ref 0
    44     val isar_ok = Unsynchronized.ref 0
    45     fun with_index facts s = (find_index (curry (op =) s) facts + 1, s)
    46     fun index_string (j, s) = s ^ "@" ^ string_of_int j
    47     fun str_of_res label facts ({outcome, run_time, used_facts, ...}: Sledgehammer_Provers.prover_result) =
    48       let val facts = facts |> map (fn ((name, _), _) => name ()) in
    49         "  " ^ label ^ ": " ^
    50         (if is_none outcome then
    51            "Success (" ^ ATP_Util.string_from_time run_time ^ "): " ^
    52            (used_facts |> map (with_index facts o fst)
    53                        |> sort (int_ord o pairself fst)
    54                        |> map index_string
    55                        |> space_implode " ") ^
    56            (if length facts < the max_facts then
    57               " (of " ^ string_of_int (length facts) ^ ")"
    58             else
    59               "")
    60          else
    61            "Failure: " ^
    62            (facts |> take (the max_facts) |> tag_list 1
    63                   |> map index_string
    64                   |> space_implode " "))
    65       end
    66     fun solve_goal (j, line) =
    67       let
    68         val (name, suggs) = extract_query line
    69         val th =
    70           case find_first (fn (_, th) => nickname_of th = name) facts of
    71             SOME (_, th) => th
    72           | NONE => error ("No fact called \"" ^ name ^ "\".")
    73         val goal = goal_of_thm (Proof_Context.theory_of ctxt) th
    74         val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
    75         val isar_deps = isar_dependencies_of all_names th |> these
    76         val facts = facts |> filter (fn (_, th') => thm_ord (th', th) = LESS)
    77         val mepo_facts =
    78           Sledgehammer_MePo.mepo_suggested_facts ctxt params prover
    79               slack_max_facts NONE hyp_ts concl_t facts
    80           |> Sledgehammer_MePo.weight_mepo_facts
    81         val mash_facts = suggested_facts suggs facts
    82         val mess = [(0.5, (mepo_facts, [])), (0.5, (mash_facts, []))]
    83         val mesh_facts = mesh_facts slack_max_facts mess
    84         val isar_facts = suggested_facts (map (rpair 1.0) isar_deps) facts
    85         fun prove ok heading get facts =
    86           let
    87             val facts =
    88               facts |> map get
    89                     |> Sledgehammer_Fact.maybe_instantiate_inducts ctxt hyp_ts
    90                                                                    concl_t
    91                     |> take (the max_facts)
    92             val res as {outcome, ...} =
    93               run_prover_for_mash ctxt params prover facts goal
    94             val _ = if is_none outcome then ok := !ok + 1 else ()
    95           in str_of_res heading facts res end
    96         val mepo_s = prove mepo_ok MePoN fst mepo_facts
    97         val mash_s = prove mash_ok MaShN fst mash_facts
    98         val mesh_s = prove mesh_ok MeshN I mesh_facts
    99         val isar_s = prove isar_ok IsarN fst isar_facts
   100       in
   101         ["Goal " ^ string_of_int j ^ ": " ^ name, mepo_s, mash_s, mesh_s,
   102          isar_s]
   103         |> cat_lines |> tracing
   104       end
   105     fun total_of heading ok n =
   106       "  " ^ heading ^ ": " ^ string_of_int (!ok) ^ " (" ^
   107       Real.fmt (StringCvt.FIX (SOME 1))
   108                (100.0 * Real.fromInt (!ok) / Real.fromInt n) ^ "%)"
   109     val inst_inducts = Config.get ctxt Sledgehammer_Fact.instantiate_inducts
   110     val options =
   111       [prover, string_of_int (the max_facts) ^ " facts",
   112        "slice" |> not slice ? prefix "dont_", the_default "smart" type_enc,
   113        the_default "smart" lam_trans, ATP_Util.string_from_time timeout,
   114        "instantiate_inducts" |> not inst_inducts ? prefix "dont_"]
   115     val n = length lines
   116   in
   117     tracing " * * *";
   118     tracing ("Options: " ^ commas options);
   119     List.app solve_goal (tag_list 1 lines);
   120     ["Successes (of " ^ string_of_int n ^ " goals)",
   121      total_of MePoN mepo_ok n,
   122      total_of MaShN mash_ok n,
   123      total_of MeshN mesh_ok n,
   124      total_of IsarN isar_ok n]
   125     |> cat_lines |> tracing
   126   end
   127 
   128 end;