author | blanchet |
Thu, 29 Jul 2010 18:45:41 +0200 | |
changeset 38092 | 81a003f7de0d |
parent 38084 | e2aac207d13b |
child 38093 | ff1d4078fe9a |
permissions | -rw-r--r-- |
36375
2482446a604c
move the minimizer to the Sledgehammer directory
blanchet
parents:
36370
diff
changeset
|
1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_fact_minimizer.ML |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
2 |
Author: Philipp Meyer, TU Muenchen |
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
3 |
Author: Jasmin Blanchette, TU Muenchen |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
4 |
|
35867 | 5 |
Minimization of theorem list for Metis using automatic theorem provers. |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
6 |
*) |
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
7 |
|
36375
2482446a604c
move the minimizer to the Sledgehammer directory
blanchet
parents:
36370
diff
changeset
|
8 |
signature SLEDGEHAMMER_FACT_MINIMIZER = |
32525 | 9 |
sig |
38021
e024504943d1
rename "ATP_Manager" ML module to "Sledgehammer";
blanchet
parents:
38015
diff
changeset
|
10 |
type params = Sledgehammer.params |
35867 | 11 |
|
38050 | 12 |
val minimize_theorems : |
13 |
params -> int -> int -> Proof.state -> (string * thm list) list |
|
14 |
-> (string * thm list) list option * string |
|
38045 | 15 |
val run_minimizer : params -> int -> Facts.ref list -> Proof.state -> unit |
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
16 |
end; |
32525 | 17 |
|
36375
2482446a604c
move the minimizer to the Sledgehammer directory
blanchet
parents:
36370
diff
changeset
|
18 |
structure Sledgehammer_Fact_Minimizer : SLEDGEHAMMER_FACT_MINIMIZER = |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
19 |
struct |
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
20 |
|
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
21 |
open ATP_Systems |
36142
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
22 |
open Sledgehammer_Util |
38045 | 23 |
open Sledgehammer_Fact_Filter |
36063
cdc6855a6387
make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents:
35969
diff
changeset
|
24 |
open Sledgehammer_Proof_Reconstruct |
38021
e024504943d1
rename "ATP_Manager" ML module to "Sledgehammer";
blanchet
parents:
38015
diff
changeset
|
25 |
open Sledgehammer |
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
26 |
|
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
27 |
(* wrapper for calling external prover *) |
31236 | 28 |
|
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
29 |
fun string_for_failure Unprovable = "Unprovable." |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
30 |
| string_for_failure TimedOut = "Timed out." |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
31 |
| string_for_failure _ = "Unknown error." |
31236 | 32 |
|
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
33 |
fun n_theorems name_thms_pairs = |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
34 |
let val n = length name_thms_pairs in |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
35 |
string_of_int n ^ " theorem" ^ plural_s n ^ |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
36 |
(if n > 0 then |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
37 |
": " ^ space_implode " " (sort_distinct string_ord name_thms_pairs) |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
38 |
else |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
39 |
"") |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
40 |
end |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
41 |
|
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
42 |
fun sledgehammer_test_theorems (params : params) (prover : prover) timeout |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
43 |
subgoal state name_thms_pairs = |
31236 | 44 |
let |
38015 | 45 |
val _ = |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
46 |
priority ("Testing " ^ n_theorems (map fst name_thms_pairs) ^ "...") |
38084
e2aac207d13b
"axiom_clauses" -> "axioms" (these are no longer clauses)
blanchet
parents:
38083
diff
changeset
|
47 |
val axioms = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs |
36263
56bf63d70120
use "Proof.goal" in Sledgehammer's minimizer (just like everywhere else in Sledgehammer), not "Proof.raw_goal"
blanchet
parents:
36232
diff
changeset
|
48 |
val {context = ctxt, facts, goal} = Proof.goal state |
32941
72d48e333b77
eliminated extraneous wrapping of public records;
wenzelm
parents:
32937
diff
changeset
|
49 |
val problem = |
36063
cdc6855a6387
make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents:
35969
diff
changeset
|
50 |
{subgoal = subgoal, goal = (ctxt, (facts, goal)), |
35969 | 51 |
relevance_override = {add = [], del = [], only = false}, |
38084
e2aac207d13b
"axiom_clauses" -> "axioms" (these are no longer clauses)
blanchet
parents:
38083
diff
changeset
|
52 |
axioms = SOME axioms} |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
53 |
val result as {outcome, used_thm_names, ...} = |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
54 |
prover params (K "") timeout problem |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
55 |
in |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
56 |
priority (case outcome of |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
57 |
NONE => |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
58 |
if length used_thm_names = length name_thms_pairs then |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
59 |
"Found proof." |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
60 |
else |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
61 |
"Found proof with " ^ n_theorems used_thm_names ^ "." |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
62 |
| SOME failure => string_for_failure failure); |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
63 |
result |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
64 |
end |
31236 | 65 |
|
66 |
(* minimalization of thms *) |
|
67 |
||
38015 | 68 |
fun filter_used_facts used = filter (member (op =) used o fst) |
69 |
||
70 |
fun sublinear_minimize _ [] p = p |
|
71 |
| sublinear_minimize test (x :: xs) (seen, result) = |
|
72 |
case test (xs @ seen) of |
|
73 |
result as {outcome = NONE, proof, used_thm_names, ...} |
|
74 |
: prover_result => |
|
75 |
sublinear_minimize test (filter_used_facts used_thm_names xs) |
|
76 |
(filter_used_facts used_thm_names seen, result) |
|
77 |
| _ => sublinear_minimize test xs (x :: seen, result) |
|
78 |
||
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
79 |
(* Give the ATP some slack. The ATP gets further slack because the Sledgehammer |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
80 |
preprocessing time is included in the estimate below but isn't part of the |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
81 |
timeout. *) |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
82 |
val fudge_msecs = 250 |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
83 |
|
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
84 |
fun minimize_theorems {atps = [], ...} _ _ _ _ = error "No ATP is set." |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
85 |
| minimize_theorems |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
86 |
(params as {debug, verbose, overlord, atps as atp :: _, full_types, |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
87 |
explicit_apply, relevance_threshold, relevance_convergence, |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
88 |
theory_relevant, defs_relevant, isar_proof, |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
89 |
isar_shrink_factor, minimize_timeout, ...}) |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
90 |
i n state name_thms_pairs = |
31236 | 91 |
let |
36378 | 92 |
val thy = Proof.theory_of state |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
93 |
val prover = get_prover_fun thy atp |
35969 | 94 |
val msecs = Time.toMilliseconds minimize_timeout |
31236 | 95 |
val _ = |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
96 |
priority ("Sledgehammer minimizer: ATP " ^ quote atp ^ |
36224 | 97 |
" with a time limit of " ^ string_of_int msecs ^ " ms.") |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
98 |
fun test_facts timeout = |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
99 |
sledgehammer_test_theorems params prover timeout i state |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
100 |
val {context = ctxt, goal, ...} = Proof.goal state |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
101 |
val timer = Timer.startRealTimer () |
31236 | 102 |
in |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
103 |
(case test_facts minimize_timeout name_thms_pairs of |
38083
c4b57f68ddb3
remove the "extra_clauses" business introduced in 19a5f1c8a844;
blanchet
parents:
38050
diff
changeset
|
104 |
result as {outcome = NONE, pool, used_thm_names, |
c4b57f68ddb3
remove the "extra_clauses" business introduced in 19a5f1c8a844;
blanchet
parents:
38050
diff
changeset
|
105 |
conjecture_shape, ...} => |
38015 | 106 |
let |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
107 |
val time = Timer.checkRealTimer timer |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
108 |
val new_timeout = |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
109 |
Int.min (msecs, Time.toMilliseconds time + fudge_msecs) |
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
110 |
|> Time.fromMilliseconds |
38015 | 111 |
val (min_thms, {proof, internal_thm_names, ...}) = |
38092
81a003f7de0d
speed up the minimizer by using the time taken for the first iteration as a timeout for the following iterations, and fix a subtle bug in "string_for_failure"
blanchet
parents:
38084
diff
changeset
|
112 |
sublinear_minimize (test_facts new_timeout) |
38083
c4b57f68ddb3
remove the "extra_clauses" business introduced in 19a5f1c8a844;
blanchet
parents:
38050
diff
changeset
|
113 |
(filter_used_facts used_thm_names name_thms_pairs) ([], result) |
38015 | 114 |
val m = length min_thms |
115 |
val _ = priority (cat_lines |
|
116 |
["Minimized: " ^ string_of_int m ^ " theorem" ^ plural_s m] ^ ".") |
|
117 |
in |
|
118 |
(SOME min_thms, |
|
119 |
proof_text isar_proof |
|
120 |
(pool, debug, isar_shrink_factor, ctxt, conjecture_shape) |
|
121 |
(full_types, K "", proof, internal_thm_names, goal, i) |> fst) |
|
122 |
end |
|
123 |
| {outcome = SOME TimedOut, ...} => |
|
124 |
(NONE, "Timeout: You can increase the time limit using the \"timeout\" \ |
|
125 |
\option (e.g., \"timeout = " ^ |
|
126 |
string_of_int (10 + msecs div 1000) ^ " s\").") |
|
127 |
| {outcome = SOME UnknownError, ...} => |
|
128 |
(* Failure sometimes mean timeout, unfortunately. *) |
|
129 |
(NONE, "Failure: No proof was found with the current time limit. You \ |
|
130 |
\can increase the time limit using the \"timeout\" \ |
|
131 |
\option (e.g., \"timeout = " ^ |
|
132 |
string_of_int (10 + msecs div 1000) ^ " s\").") |
|
133 |
| {message, ...} => (NONE, "ATP error: " ^ message)) |
|
37994
b04307085a09
make TPTP generator accept full first-order formulas
blanchet
parents:
37926
diff
changeset
|
134 |
handle ERROR msg => (NONE, "Error: " ^ msg) |
31236 | 135 |
end |
136 |
||
38045 | 137 |
fun run_minimizer params i refs state = |
138 |
let |
|
139 |
val ctxt = Proof.context_of state |
|
140 |
val chained_ths = #facts (Proof.goal state) |
|
141 |
val name_thms_pairs = map (name_thms_pair_from_ref ctxt chained_ths) refs |
|
142 |
in |
|
143 |
case subgoal_count state of |
|
144 |
0 => priority "No subgoal!" |
|
145 |
| n => |
|
146 |
(kill_atps (); |
|
147 |
priority (#2 (minimize_theorems params i n state name_thms_pairs))) |
|
148 |
end |
|
149 |
||
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
150 |
end; |