| author | wenzelm | 
| Wed, 27 Mar 2024 17:04:37 +0100 | |
| changeset 80038 | b1e2246147eb | 
| parent 75616 | 986506233812 | 
| child 80910 | 406a85a25189 | 
| permissions | -rw-r--r-- | 
| 48285 | 1 | (* Title: HOL/TPTP/mash_eval.ML | 
| 48234 | 2 | Author: Jasmin Blanchette, TU Muenchen | 
| 3 | Copyright 2012 | |
| 4 | ||
| 48285 | 5 | Evaluate proof suggestions from MaSh (Machine-learning for Sledgehammer). | 
| 48234 | 6 | *) | 
| 7 | ||
| 48285 | 8 | signature MASH_EVAL = | 
| 48234 | 9 | sig | 
| 55201 | 10 | type params = Sledgehammer_Prover.params | 
| 48251 
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
 blanchet parents: 
48250diff
changeset | 11 | |
| 57456 | 12 | val evaluate_mash_suggestions : Proof.context -> params -> int * int option -> string option -> | 
| 13 | string list -> string -> unit | |
| 48234 | 14 | end; | 
| 15 | ||
| 48285 | 16 | structure MaSh_Eval : MASH_EVAL = | 
| 48234 | 17 | struct | 
| 48235 | 18 | |
| 50557 | 19 | open Sledgehammer_Util | 
| 48315 
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
 blanchet parents: 
48313diff
changeset | 20 | open Sledgehammer_Fact | 
| 50557 | 21 | open Sledgehammer_MePo | 
| 48381 | 22 | open Sledgehammer_MaSh | 
| 55201 | 23 | open Sledgehammer_Prover | 
| 55212 | 24 | open Sledgehammer_Prover_ATP | 
| 55198 | 25 | open Sledgehammer_Commands | 
| 57431 
02c408aed5ee
killed Python version of MaSh, now that the SML version works adequately
 blanchet parents: 
57406diff
changeset | 26 | open MaSh_Export | 
| 48240 | 27 | |
| 53980 
7e6a82c593f4
made SML/NJ happy (NB: toplevel ML environment is unmanaged);
 wenzelm parents: 
53139diff
changeset | 28 | val prefix = Library.prefix | 
| 
7e6a82c593f4
made SML/NJ happy (NB: toplevel ML environment is unmanaged);
 wenzelm parents: 
53139diff
changeset | 29 | |
| 57456 | 30 | fun evaluate_mash_suggestions ctxt params range prob_dir_name file_names report_file_name = | 
| 48235 | 31 | let | 
| 55205 | 32 | val thy = Proof_Context.theory_of ctxt | 
| 51028 | 33 | val zeros = [0, 0, 0, 0, 0, 0] | 
| 50448 
0a740d127187
have MaSh evaluator keep all raw problem/solution files in a directory
 blanchet parents: 
50442diff
changeset | 34 | val report_path = report_file_name |> Path.explode | 
| 
0a740d127187
have MaSh evaluator keep all raw problem/solution files in a directory
 blanchet parents: 
50442diff
changeset | 35 | val _ = File.write report_path "" | 
| 57432 | 36 | |
| 50589 | 37 | fun print s = File.append report_path (s ^ "\n") | 
| 57432 | 38 | |
| 75044 | 39 |     val {provers, max_facts, slices, type_enc, lam_trans, timeout, induction_rules, ...} =
 | 
| 73942 | 40 | default_params thy [] | 
| 48318 | 41 | val prover = hd provers | 
| 57108 
dc0b4f50e288
more generous max number of suggestions, for more safety
 blanchet parents: 
55212diff
changeset | 42 | val max_suggs = generous_max_suggestions (the max_facts) | 
| 73942 | 43 | val inst_inducts = induction_rules = SOME Instantiate | 
| 57456 | 44 | |
| 45 | val method_of_file_name = | |
| 46 | perhaps (try (unsuffix "_suggestions")) o List.last o space_explode "/" | |
| 47 | ||
| 48 | val methods = "isar" :: map method_of_file_name file_names | |
| 75616 
986506233812
clarified signature: File.read_lines is based on scalable Bytes.T;
 wenzelm parents: 
75612diff
changeset | 49 | val lines_of = Path.explode #> try File.read_lines #> these | 
| 57456 | 50 | val liness0 = map lines_of file_names | 
| 51 | val num_lines = fold (Integer.max o length) liness0 0 | |
| 57432 | 52 | |
| 50964 
2a990baa09af
use precomputed MaSh/MePo data whenever available
 blanchet parents: 
50953diff
changeset | 53 | fun pad lines = lines @ replicate (num_lines - length lines) "" | 
| 57432 | 54 | |
| 58205 | 55 | val liness' = Ctr_Sugar_Util.transpose (map pad liness0) | 
| 57456 | 56 | |
| 50557 | 57 | val css = clasimpset_rule_table_of ctxt | 
| 73942 | 58 | val facts = all_facts ctxt true Keyword.empty_keywords [] [] css | 
| 61321 | 59 | val name_tabs = build_name_tables nickname_of_thm facts | 
| 57432 | 60 | |
| 48289 | 61 | fun with_index facts s = (find_index (curry (op =) s) facts + 1, s) | 
| 50952 | 62 | fun index_str (j, s) = s ^ "@" ^ string_of_int j | 
| 50953 | 63 | val str_of_method = enclose " " ": " | 
| 57432 | 64 | |
| 65 |     fun str_of_result method facts ({outcome, run_time, used_facts, ...} : prover_result) =
 | |
| 51004 
5f2788c38127
distinguish raw and non-raw facts, using raw for 10 000s of facts and non-raw after selection of some hundreds
 blanchet parents: 
50967diff
changeset | 66 | let val facts = facts |> map (fst o fst) in | 
| 50953 | 67 | str_of_method method ^ | 
| 48289 | 68 | (if is_none outcome then | 
| 52031 
9a9238342963
tuning -- renamed '_from_' to '_of_' in Sledgehammer
 blanchet parents: 
51199diff
changeset | 69 |            "Success (" ^ ATP_Util.string_of_time run_time ^ "): " ^
 | 
| 48289 | 70 | (used_facts |> map (with_index facts o fst) | 
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58922diff
changeset | 71 | |> sort (int_ord o apply2 fst) | 
| 50952 | 72 | |> map index_str | 
| 48289 | 73 | |> space_implode " ") ^ | 
| 48293 | 74 | (if length facts < the max_facts then | 
| 48289 | 75 | " (of " ^ string_of_int (length facts) ^ ")" | 
| 76 | else | |
| 77 | "") | |
| 78 | else | |
| 79 | "Failure: " ^ | |
| 48293 | 80 | (facts |> take (the max_facts) |> tag_list 1 | 
| 50952 | 81 | |> map index_str | 
| 48289 | 82 | |> space_implode " ")) | 
| 83 | end | |
| 57432 | 84 | |
| 57456 | 85 | fun solve_goal (j, lines) = | 
| 58205 | 86 | if in_range range j andalso exists (curry (op <>) "") lines then | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 87 | let | 
| 57125 
2f620ef839ee
added another way of invoking Python code, for experiments
 blanchet parents: 
57108diff
changeset | 88 | val get_suggs = extract_suggestions ##> (take max_suggs #> map fst) | 
| 57456 | 89 | val (names, suggss0) = split_list (map get_suggs lines) | 
| 58205 | 90 | val name = | 
| 91 | (case names |> filter (curry (op <>) "") |> distinct (op =) of | |
| 92 | [name] => name | |
| 63692 | 93 |             | names => error ("Input files out of sync: facts " ^ commas (map quote names)))
 | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 94 | val th = | 
| 61321 | 95 | case find_first (fn (_, th) => nickname_of_thm th = name) facts of | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 96 | SOME (_, th) => th | 
| 63692 | 97 |             | NONE => error ("No fact called \"" ^ name)
 | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 98 | val goal = goal_of_thm (Proof_Context.theory_of ctxt) th | 
| 52196 
2281f33e8da6
redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
 blanchet parents: 
52125diff
changeset | 99 | val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal goal 1 ctxt | 
| 61321 | 100 | val isar_deps = these (isar_dependencies_of name_tabs th) | 
| 57456 | 101 | val suggss = isar_deps :: suggss0 | 
| 57406 | 102 | val facts = facts |> filter (fn (_, th') => thm_less (th', th)) | 
| 57432 | 103 | |
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 104 | (* adapted from "mirabelle_sledgehammer.ML" *) | 
| 50953 | 105 | fun set_file_name method (SOME dir) = | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 106 | let | 
| 57432 | 107 | val prob_prefix = "goal_" ^ string_of_int j ^ "__" ^ encode_str name ^ "__" ^ method | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 108 | in | 
| 74981 
10df7a627ab6
split option "sledgehammer_atp_dest_dir" into "sledgehammer_atp_prob_dest_dir" and "sledgehammer_atp_proof_dest_dir"
 desharna parents: 
73979diff
changeset | 109 | Config.put atp_problem_dest_dir dir | 
| 55212 | 110 | #> Config.put atp_problem_prefix (prob_prefix ^ "__") | 
| 58061 | 111 | #> Config.put SMT_Config.debug_files (dir ^ "/" ^ prob_prefix) | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 112 | end | 
| 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 113 | | set_file_name _ NONE = I | 
| 57432 | 114 | |
| 53139 | 115 | fun prove method suggs = | 
| 57456 | 116 | if null facts then | 
| 50953 | 117 | (str_of_method method ^ "Skipped", 0) | 
| 50952 | 118 | else | 
| 119 | let | |
| 120 | fun nickify ((_, stature), th) = | |
| 61321 | 121 | ((K (encode_str (nickname_of_thm th)), stature), th) | 
| 57432 | 122 | |
| 50952 | 123 | val facts = | 
| 53139 | 124 | suggs | 
| 125 | |> find_suggested_facts ctxt facts | |
| 73979 | 126 | |> map (fact_of_lazy_fact #> nickify) | 
| 73942 | 127 | |> inst_inducts ? instantiate_inducts ctxt hyp_ts concl_t | 
| 50952 | 128 | |> take (the max_facts) | 
| 73979 | 129 | |> map fact_of_lazy_fact | 
| 50953 | 130 | val ctxt = ctxt |> set_file_name method prob_dir_name | 
| 57434 | 131 |                 val res as {outcome, ...} = run_prover_for_mash ctxt params prover name facts goal
 | 
| 50952 | 132 | val ok = if is_none outcome then 1 else 0 | 
| 57432 | 133 | in | 
| 134 | (str_of_result method facts res, ok) | |
| 135 | end | |
| 136 | ||
| 57456 | 137 | val ress = map2 prove methods suggss | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 138 | in | 
| 50591 | 139 | "Goal " ^ string_of_int j ^ ": " ^ name :: map fst ress | 
| 50587 
bd6582be1562
synchronize access to shared reference and print proper total
 blanchet parents: 
50563diff
changeset | 140 | |> cat_lines |> print; | 
| 50591 | 141 | map snd ress | 
| 50559 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 142 | end | 
| 
89c0d2f13cca
MaSh exporter can now export subsets of the facts, as consecutive ranges
 blanchet parents: 
50557diff
changeset | 143 | else | 
| 51028 | 144 | zeros | 
| 57432 | 145 | |
| 48245 | 146 | val options = | 
| 50767 | 147 | ["prover = " ^ prover, | 
| 148 | "max_facts = " ^ string_of_int (the max_facts), | |
| 75044 | 149 | "slices = " ^ string_of_int slices, | 
| 50767 | 150 | "type_enc = " ^ the_default "smart" type_enc, | 
| 151 | "lam_trans = " ^ the_default "smart" lam_trans, | |
| 54816 
10d48c2a3e32
made timeouts in Sledgehammer not be 'option's -- simplified lots of code
 blanchet parents: 
54141diff
changeset | 152 | "timeout = " ^ ATP_Util.string_of_time timeout, | 
| 48241 | 153 | "instantiate_inducts" |> not inst_inducts ? prefix "dont_"] | 
| 50587 
bd6582be1562
synchronize access to shared reference and print proper total
 blanchet parents: 
50563diff
changeset | 154 | val _ = print " * * *"; | 
| 
bd6582be1562
synchronize access to shared reference and print proper total
 blanchet parents: 
50563diff
changeset | 155 |     val _ = print ("Options: " ^ commas options);
 | 
| 58205 | 156 | val oks = Par_List.map solve_goal (tag_list 1 liness') | 
| 50620 | 157 | val n = length oks | 
| 57456 | 158 | |
| 159 | fun total_of method ok = | |
| 160 |       str_of_method method ^ string_of_int ok ^ " (" ^ Real.fmt (StringCvt.FIX (SOME 1))
 | |
| 161 | (100.0 * Real.fromInt ok / Real.fromInt (Int.max (1, n))) ^ "%)" | |
| 162 | ||
| 163 | val oks' = if n = 0 then zeros else map Integer.sum (map_transpose I oks) | |
| 48241 | 164 | in | 
| 57456 | 165 | "Successes (of " ^ string_of_int n ^ " goals)" :: | 
| 166 | map2 total_of methods oks' | |
| 50437 | 167 | |> cat_lines |> print | 
| 48241 | 168 | end | 
| 48235 | 169 | |
| 48234 | 170 | end; |