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