src/HOL/TPTP/mash_eval.ML
author blanchet
Wed, 18 Jul 2012 08:44:04 +0200
changeset 48307 7c78f14d20cf
parent 48303 f1d135d0ea69
child 48311 3c4e10606567
permissions -rw-r--r--
cleaner handling of metacharacters + freshness of one-off facts
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48285
902ab51dd12a renaming
blanchet
parents: 48284
diff changeset
     1
(*  Title:      HOL/TPTP/mash_eval.ML
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     3
    Copyright   2012
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     4
48285
902ab51dd12a renaming
blanchet
parents: 48284
diff changeset
     5
Evaluate proof suggestions from MaSh (Machine-learning for Sledgehammer).
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     6
*)
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     7
48285
902ab51dd12a renaming
blanchet
parents: 48284
diff changeset
     8
signature MASH_EVAL =
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     9
sig
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    10
  type params = Sledgehammer_Provers.params
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    11
48285
902ab51dd12a renaming
blanchet
parents: 48284
diff changeset
    12
  val evaluate_mash_suggestions :
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    13
    Proof.context -> params -> theory -> string -> unit
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    14
end;
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    15
48285
902ab51dd12a renaming
blanchet
parents: 48284
diff changeset
    16
structure MaSh_Eval : MASH_EVAL =
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    17
struct
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    18
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    19
open Sledgehammer_Filter_MaSh
48240
6a8d18798161 rationalized output
blanchet
parents: 48239
diff changeset
    20
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
    21
val isarN = "Isa"
48285
902ab51dd12a renaming
blanchet
parents: 48284
diff changeset
    22
val iterN = "Iter"
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
    23
val mashN = "MaSh"
48298
f5b160f9859e more work on MaSh
blanchet
parents: 48293
diff changeset
    24
val meshN = "Mesh"
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
    25
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
    26
val max_facts_slack = 2
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    27
48285
902ab51dd12a renaming
blanchet
parents: 48284
diff changeset
    28
fun evaluate_mash_suggestions ctxt params thy file_name =
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    29
  let
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
    30
    val {provers, max_facts, slice, type_enc, lam_trans, timeout, ...} =
48239
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48236
diff changeset
    31
      Sledgehammer_Isar.default_params ctxt []
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48236
diff changeset
    32
    val prover_name = hd provers
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    33
    val path = file_name |> Path.explode
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    34
    val lines = path |> File.read_lines
48299
5e5c6616f0fe centrally construct expensive data structures
blanchet
parents: 48298
diff changeset
    35
    val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
5e5c6616f0fe centrally construct expensive data structures
blanchet
parents: 48298
diff changeset
    36
    val facts = all_non_tautological_facts_of thy css_table
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    37
    val all_names = facts |> map (Thm.get_name_hint o snd)
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    38
    val iter_ok = Unsynchronized.ref 0
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
    39
    val mash_ok = Unsynchronized.ref 0
48298
f5b160f9859e more work on MaSh
blanchet
parents: 48293
diff changeset
    40
    val mesh_ok = Unsynchronized.ref 0
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
    41
    val isar_ok = Unsynchronized.ref 0
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    42
    fun find_sugg facts sugg =
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    43
      find_first (fn (_, th) => Thm.get_name_hint th = sugg) facts
48236
e174ecc4f5a4 instantiate induction rules
blanchet
parents: 48235
diff changeset
    44
    fun sugg_facts hyp_ts concl_t facts =
48303
f1d135d0ea69 improved MaSh string escaping and make more operations string-based
blanchet
parents: 48300
diff changeset
    45
      map_filter (find_sugg facts)
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
    46
      #> take (max_facts_slack * the max_facts)
48250
1065c307fafe further ML structure split to permit finer-grained loading/reordering (problem to solve: MaSh needs most of Sledgehammer)
blanchet
parents: 48245
diff changeset
    47
      #> Sledgehammer_Fact.maybe_instantiate_inducts ctxt hyp_ts concl_t
48298
f5b160f9859e more work on MaSh
blanchet
parents: 48293
diff changeset
    48
    fun mesh_facts fsp =
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    49
      let
48289
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    50
        val (fs1, fs2) =
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    51
          fsp |> pairself (map (apfst (apfst (fn name => name ()))))
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    52
        val fact_eq = (op =) o pairself fst
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    53
        fun score_in f fs =
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    54
          case find_index (curry fact_eq f) fs of
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    55
            ~1 => length fs
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    56
          | j => j
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    57
        fun score_of f = score_in f fs1 + score_in f fs2
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    58
      in
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    59
        union fact_eq fs1 fs2
48236
e174ecc4f5a4 instantiate induction rules
blanchet
parents: 48235
diff changeset
    60
        |> map (`score_of) |> sort (int_ord o pairself fst) |> map snd
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
    61
        |> take (the max_facts)
48289
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    62
        |> map (apfst (apfst K))
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    63
      end
48289
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    64
    fun with_index facts s = (find_index (curry (op =) s) facts + 1, s)
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    65
    fun index_string (j, s) = s ^ "@" ^ string_of_int j
48240
6a8d18798161 rationalized output
blanchet
parents: 48239
diff changeset
    66
    fun str_of_res label facts {outcome, run_time, used_facts, ...} =
48289
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    67
      let val facts = facts |> map (fn ((name, _), _) => name ()) in
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    68
        "  " ^ label ^ ": " ^
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    69
        (if is_none outcome then
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    70
           "Success (" ^ ATP_Util.string_from_time run_time ^ "): " ^
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    71
           (used_facts |> map (with_index facts o fst)
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    72
                       |> sort (int_ord o pairself fst)
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    73
                       |> map index_string
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    74
                       |> space_implode " ") ^
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
    75
           (if length facts < the max_facts then
48289
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    76
              " (of " ^ string_of_int (length facts) ^ ")"
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    77
            else
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    78
              "")
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    79
         else
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    80
           "Failure: " ^
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
    81
           (facts |> take (the max_facts) |> tag_list 1
48289
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    82
                  |> map index_string
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    83
                  |> space_implode " "))
6b65f1ad0e4b systematize lazy names in relevance filter
blanchet
parents: 48285
diff changeset
    84
      end
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    85
    fun prove ok heading facts goal =
48239
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48236
diff changeset
    86
      let
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
    87
        val facts = facts |> take (the max_facts)
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    88
        val res as {outcome, ...} = run_prover ctxt params facts goal
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
    89
        val _ = if is_none outcome then ok := !ok + 1 else ()
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
    90
      in str_of_res heading facts res end
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
    91
    fun solve_goal j name suggs =
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    92
      let
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    93
        val th =
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    94
          case find_first (fn (_, th) => Thm.get_name_hint th = name) facts of
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    95
            SOME (_, th) => th
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    96
          | NONE => error ("No fact called \"" ^ name ^ "\"")
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
    97
        val isar_deps = isabelle_dependencies_of all_names th
48250
1065c307fafe further ML structure split to permit finer-grained loading/reordering (problem to solve: MaSh needs most of Sledgehammer)
blanchet
parents: 48245
diff changeset
    98
        val goal = goal_of_thm thy th
48245
854a47677335 generate ATP dependencies
blanchet
parents: 48241
diff changeset
    99
        val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
   100
        val facts = facts |> filter (fn (_, th') => thm_ord (th', th) = LESS)
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
   101
        val isar_facts = sugg_facts hyp_ts concl_t facts isar_deps
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
   102
        val iter_facts =
48292
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
   103
          Sledgehammer_Filter_Iter.iterative_relevant_facts ctxt params
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
   104
              prover_name (max_facts_slack * the max_facts) NONE hyp_ts concl_t
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
   105
              facts
48236
e174ecc4f5a4 instantiate induction rules
blanchet
parents: 48235
diff changeset
   106
        val mash_facts = sugg_facts hyp_ts concl_t facts suggs
48298
f5b160f9859e more work on MaSh
blanchet
parents: 48293
diff changeset
   107
        val mesh_facts = mesh_facts (iter_facts, mash_facts)
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
   108
        val iter_s = prove iter_ok iterN iter_facts goal
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
   109
        val mash_s = prove mash_ok mashN mash_facts goal
48298
f5b160f9859e more work on MaSh
blanchet
parents: 48293
diff changeset
   110
        val mesh_s = prove mesh_ok meshN mesh_facts goal
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
   111
        val isar_s = prove isar_ok isarN isar_facts goal
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
   112
      in
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
   113
        ["Goal " ^ string_of_int j ^ ": " ^ name, iter_s, mash_s, mesh_s,
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
   114
         isar_s]
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   115
        |> cat_lines |> tracing
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
   116
      end
48303
f1d135d0ea69 improved MaSh string escaping and make more operations string-based
blanchet
parents: 48300
diff changeset
   117
    val explode_suggs =
f1d135d0ea69 improved MaSh string escaping and make more operations string-based
blanchet
parents: 48300
diff changeset
   118
      space_explode " " #> filter_out (curry (op =) "") #> map unescape_meta
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   119
    fun do_line (j, line) =
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
   120
      case space_explode ":" line of
48303
f1d135d0ea69 improved MaSh string escaping and make more operations string-based
blanchet
parents: 48300
diff changeset
   121
        [goal_name, suggs] =>
f1d135d0ea69 improved MaSh string escaping and make more operations string-based
blanchet
parents: 48300
diff changeset
   122
        solve_goal j (unescape_meta goal_name) (explode_suggs suggs)
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
   123
      | _ => ()
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   124
    fun total_of heading ok n =
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   125
      " " ^ heading ^ ": " ^ string_of_int (!ok) ^ " (" ^
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   126
      Real.fmt (StringCvt.FIX (SOME 1))
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   127
               (100.0 * Real.fromInt (!ok) / Real.fromInt n) ^ "%)"
48250
1065c307fafe further ML structure split to permit finer-grained loading/reordering (problem to solve: MaSh needs most of Sledgehammer)
blanchet
parents: 48245
diff changeset
   128
    val inst_inducts = Config.get ctxt Sledgehammer_Fact.instantiate_inducts
48245
854a47677335 generate ATP dependencies
blanchet
parents: 48241
diff changeset
   129
    val options =
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
   130
      [prover_name, string_of_int (the max_facts) ^ " facts",
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   131
       "slice" |> not slice ? prefix "dont_", the_default "smart" type_enc,
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   132
       the_default "smart" lam_trans, ATP_Util.string_from_time timeout,
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   133
       "instantiate_inducts" |> not inst_inducts ? prefix "dont_"]
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   134
    val n = length lines
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   135
  in
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   136
    tracing " * * *";
48245
854a47677335 generate ATP dependencies
blanchet
parents: 48241
diff changeset
   137
    tracing ("Options: " ^ commas options);
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   138
    List.app do_line (tag_list 1 lines);
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   139
    ["Successes (of " ^ string_of_int n ^ " goals)",
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
   140
     total_of iterN iter_ok n,
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   141
     total_of mashN mash_ok n,
48298
f5b160f9859e more work on MaSh
blanchet
parents: 48293
diff changeset
   142
     total_of meshN mesh_ok n,
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
   143
     total_of isarN isar_ok n]
48241
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   144
    |> cat_lines |> tracing
688f1172d523 nicer output
blanchet
parents: 48240
diff changeset
   145
  end
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
   146
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
   147
end;