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