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