src/HOL/TPTP/mash_eval.ML
author blanchet
Thu, 17 Jan 2013 23:29:17 +0100
changeset 51979 2a990baa09af
parent 51968 c4c746bbf836
child 51980 7a7d1418301e
permissions -rw-r--r--
use precomputed MaSh/MePo data whenever available
     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 MePoN : string
    13   val MaSh_IsarN : string
    14   val MaSh_ProverN : string
    15   val MeSh_IsarN : string
    16   val MeSh_ProverN : string
    17   val IsarN : string
    18   val evaluate_mash_suggestions :
    19     Proof.context -> params -> int * int option -> string list -> string option
    20     -> string -> string -> string -> string -> string -> string -> unit
    21 end;
    22 
    23 structure MaSh_Eval : MASH_EVAL =
    24 struct
    25 
    26 open Sledgehammer_Util
    27 open Sledgehammer_Fact
    28 open Sledgehammer_MePo
    29 open Sledgehammer_MaSh
    30 open Sledgehammer_Provers
    31 open Sledgehammer_Isar
    32 
    33 val MePoN = "MePo"
    34 val MaSh_IsarN = "MaSh-Isar"
    35 val MaSh_ProverN = "MaSh-Prover"
    36 val MeSh_IsarN = "MeSh-Isar"
    37 val MeSh_ProverN = "MeSh-Prover"
    38 val IsarN = "Isar"
    39 
    40 fun in_range (from, to) j =
    41   j >= from andalso (to = NONE orelse j <= the to)
    42 
    43 fun evaluate_mash_suggestions ctxt params range methods prob_dir_name
    44         mepo_file_name mash_isar_file_name mash_prover_file_name
    45         mesh_isar_file_name mesh_prover_file_name report_file_name =
    46   let
    47     val report_path = report_file_name |> Path.explode
    48     val _ = File.write report_path ""
    49     fun print s = File.append report_path (s ^ "\n")
    50     val {provers, max_facts, slice, type_enc, lam_trans, timeout, ...} =
    51       default_params ctxt []
    52     val prover = hd provers
    53     val slack_max_facts = generous_max_facts (the max_facts)
    54     val lines_of = Path.explode #> try File.read_lines #> these
    55     val file_names =
    56       [mepo_file_name, mash_isar_file_name, mash_prover_file_name,
    57        mesh_isar_file_name, mesh_prover_file_name]
    58     val lines as [mepo_lines, mash_isar_lines, mash_prover_lines,
    59                   mesh_isar_lines, mesh_prover_lines] =
    60       map lines_of file_names
    61     val num_lines = fold (Integer.max o length) lines 0
    62     fun pad lines = lines @ replicate (num_lines - length lines) ""
    63     val lines =
    64       pad mepo_lines ~~ pad mash_isar_lines ~~ pad mash_prover_lines ~~
    65       pad mesh_isar_lines ~~ pad mesh_prover_lines
    66     val css = clasimpset_rule_table_of ctxt
    67     val facts = all_facts ctxt true false Symtab.empty [] [] css
    68     val name_tabs = build_name_tables nickname_of_thm facts
    69     fun with_index facts s = (find_index (curry (op =) s) facts + 1, s)
    70     fun index_str (j, s) = s ^ "@" ^ string_of_int j
    71     val str_of_method = enclose "  " ": "
    72     fun str_of_result method facts ({outcome, run_time, used_facts, ...}
    73                                      : prover_result) =
    74       let val facts = facts |> map (fn ((name, _), _) => name ()) in
    75         str_of_method method ^
    76         (if is_none outcome then
    77            "Success (" ^ ATP_Util.string_from_time run_time ^ "): " ^
    78            (used_facts |> map (with_index facts o fst)
    79                        |> sort (int_ord o pairself fst)
    80                        |> map index_str
    81                        |> space_implode " ") ^
    82            (if length facts < the max_facts then
    83               " (of " ^ string_of_int (length facts) ^ ")"
    84             else
    85               "")
    86          else
    87            "Failure: " ^
    88            (facts |> take (the max_facts) |> tag_list 1
    89                   |> map index_str
    90                   |> space_implode " "))
    91       end
    92     fun solve_goal (j, ((((mepo_line, mash_isar_line), mash_prover_line),
    93                          mesh_isar_line), mesh_prover_line)) =
    94       if in_range range j then
    95         let
    96           val (name1, mepo_suggs) = extract_suggestions mepo_line
    97           val (name2, mash_isar_suggs) = extract_suggestions mash_isar_line
    98           val (name3, mash_prover_suggs) = extract_suggestions mash_prover_line
    99           val (name4, mesh_isar_suggs) = extract_suggestions mesh_isar_line
   100           val (name5, mesh_prover_suggs) = extract_suggestions mesh_prover_line
   101           val [name] =
   102             [name1, name2, name3, name4, name5]
   103             |> filter (curry (op <>) "") |> distinct (op =)
   104             handle General.Match => error "Input files out of sync."
   105           val th =
   106             case find_first (fn (_, th) => nickname_of_thm th = name) facts of
   107               SOME (_, th) => th
   108             | NONE => error ("No fact called \"" ^ name ^ "\".")
   109           val goal = goal_of_thm (Proof_Context.theory_of ctxt) th
   110           val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
   111           val isar_deps = isar_dependencies_of name_tabs th
   112           val facts = facts |> filter (fn (_, th') => thm_ord (th', th) = LESS)
   113           fun get_facts [] compute = compute facts
   114             | get_facts suggs _ = find_suggested_facts suggs facts
   115           val mepo_facts =
   116             get_facts mepo_suggs (fn _ =>
   117                 mepo_suggested_facts ctxt params prover slack_max_facts NONE
   118                                      hyp_ts concl_t facts
   119                 |> weight_mepo_facts)
   120           fun mash_of suggs =
   121             get_facts suggs (fn _ =>
   122                 find_mash_suggestions slack_max_facts suggs facts [] []
   123                 |> fst |> weight_mash_facts)
   124           val mash_isar_facts = mash_of mash_isar_suggs
   125           val mash_prover_facts = mash_of mash_prover_suggs
   126           fun mess_of mash_facts =
   127             [(mepo_weight, (mepo_facts, [])),
   128              (mash_weight, (mash_facts, []))]
   129           fun mesh_of suggs mash_facts =
   130             get_facts suggs (fn _ =>
   131                 mesh_facts (Thm.eq_thm_prop o pairself snd) slack_max_facts
   132                            (mess_of mash_facts)
   133                 |> map (rpair 1.0))
   134           val mesh_isar_facts = mesh_of mesh_isar_suggs mash_isar_facts
   135           val mesh_prover_facts = mesh_of mesh_prover_suggs mash_prover_facts
   136           val isar_facts =
   137             find_suggested_facts (map (rpair 1.0) isar_deps) facts
   138           (* adapted from "mirabelle_sledgehammer.ML" *)
   139           fun set_file_name method (SOME dir) =
   140               let
   141                 val prob_prefix =
   142                   "goal_" ^ string_of_int j ^ "__" ^ encode_str name ^ "__" ^
   143                   method
   144               in
   145                 Config.put dest_dir dir
   146                 #> Config.put problem_prefix (prob_prefix ^ "__")
   147                 #> Config.put SMT_Config.debug_files (dir ^ "/" ^ prob_prefix)
   148               end
   149             | set_file_name _ NONE = I
   150           fun prove method facts =
   151             if not (member (op =) methods method) orelse
   152                (null facts andalso method <> IsarN) then
   153               (str_of_method method ^ "Skipped", 0)
   154             else
   155               let
   156                 fun nickify ((_, stature), th) =
   157                   ((K (encode_str (nickname_of_thm th)), stature), th)
   158                 val facts =
   159                   facts
   160                   |> map (fst #> nickify)
   161                   |> maybe_instantiate_inducts ctxt hyp_ts concl_t
   162                   |> take (the max_facts)
   163                 val ctxt = ctxt |> set_file_name method prob_dir_name
   164                 val res as {outcome, ...} =
   165                   run_prover_for_mash ctxt params prover facts goal
   166                 val ok = if is_none outcome then 1 else 0
   167               in (str_of_result method facts res, ok) end
   168           val ress =
   169             [fn () => prove MePoN mepo_facts,
   170              fn () => prove MaSh_IsarN mash_isar_facts,
   171              fn () => prove MaSh_ProverN mash_prover_facts,
   172              fn () => prove MeSh_IsarN mesh_isar_facts,
   173              fn () => prove MeSh_ProverN mesh_prover_facts,
   174              fn () => prove IsarN isar_facts]
   175             |> (* Par_List. *) map (fn f => f ())
   176         in
   177           "Goal " ^ string_of_int j ^ ": " ^ name :: map fst ress
   178           |> cat_lines |> print;
   179           map snd ress
   180         end
   181       else
   182         [0, 0, 0, 0, 0, 0]
   183     fun total_of method ok n =
   184       str_of_method method ^ string_of_int ok ^ " (" ^
   185       Real.fmt (StringCvt.FIX (SOME 1))
   186                (100.0 * Real.fromInt ok / Real.fromInt n) ^ "%)"
   187     val inst_inducts = Config.get ctxt instantiate_inducts
   188     val options =
   189       ["prover = " ^ prover,
   190        "max_facts = " ^ string_of_int (the max_facts),
   191        "slice" |> not slice ? prefix "dont_",
   192        "type_enc = " ^ the_default "smart" type_enc,
   193        "lam_trans = " ^ the_default "smart" lam_trans,
   194        "timeout = " ^ ATP_Util.string_from_time (the_default one_year timeout),
   195        "instantiate_inducts" |> not inst_inducts ? prefix "dont_"]
   196     val _ = print " * * *";
   197     val _ = print ("Options: " ^ commas options);
   198     val oks = Par_List.map solve_goal (tag_list 1 lines)
   199     val n = length oks
   200     val [mepo_ok, mash_isar_ok, mash_prover_ok, mesh_isar_ok, mesh_prover_ok,
   201          isar_ok] =
   202       map Integer.sum (map_transpose I oks)
   203   in
   204     ["Successes (of " ^ string_of_int n ^ " goals)",
   205      total_of MePoN mepo_ok n,
   206      total_of MaSh_IsarN mash_isar_ok n,
   207      total_of MaSh_ProverN mash_prover_ok n,
   208      total_of MeSh_IsarN mesh_isar_ok n,
   209      total_of MeSh_ProverN mesh_prover_ok n,
   210      total_of IsarN isar_ok n]
   211     |> cat_lines |> print
   212   end
   213 
   214 end;