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