author | blanchet |
Fri, 23 Apr 2010 17:38:25 +0200 | |
changeset 36375 | 2482446a604c |
parent 36370 | src/HOL/Tools/ATP_Manager/atp_minimal.ML@a4f601daa175 |
child 36378 | f32c567dbcaa |
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 |
35969 | 10 |
type params = ATP_Manager.params |
35867 | 11 |
type prover = ATP_Manager.prover |
12 |
type prover_result = ATP_Manager.prover_result |
|
13 |
||
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
14 |
val minimize_theorems : |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
15 |
params -> prover -> string -> int -> Proof.state -> (string * thm list) list |
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
16 |
-> (string * thm list) list option * string |
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
17 |
end; |
32525 | 18 |
|
36375
2482446a604c
move the minimizer to the Sledgehammer directory
blanchet
parents:
36370
diff
changeset
|
19 |
structure Sledgehammer_Fact_Minimizer : SLEDGEHAMMER_FACT_MINIMIZER = |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
20 |
struct |
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
21 |
|
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 |
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
23 |
open Sledgehammer_Fact_Preprocessor |
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 |
35867 | 25 |
open ATP_Manager |
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
26 |
|
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
27 |
(* Linear minimization algorithm *) |
33492
4168294a9f96
Command atp_minimize uses the naive linear algorithm now
nipkow
parents:
33316
diff
changeset
|
28 |
|
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
29 |
fun linear_minimize test s = |
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
30 |
let |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
31 |
fun aux [] p = p |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
32 |
| aux (x :: xs) (needed, result) = |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
33 |
case test (xs @ needed) of |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
34 |
SOME result => aux xs (needed, result) |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
35 |
| NONE => aux xs (x :: needed, result) |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
36 |
in aux s end |
31236 | 37 |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
38 |
|
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
39 |
(* wrapper for calling external prover *) |
31236 | 40 |
|
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
41 |
fun string_for_failure Unprovable = "Unprovable." |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
42 |
| string_for_failure TimedOut = "Timed out." |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
43 |
| string_for_failure OutOfResources = "Failed." |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
44 |
| string_for_failure OldSpass = "Error." |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
45 |
| string_for_failure MalformedOutput = "Error." |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
46 |
| string_for_failure UnknownError = "Failed." |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
47 |
fun string_for_outcome NONE = "Success." |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
48 |
| string_for_outcome (SOME failure) = string_for_failure failure |
31236 | 49 |
|
35969 | 50 |
fun sledgehammer_test_theorems (params as {full_types, ...} : params) prover |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
51 |
timeout subgoal state filtered_clauses name_thms_pairs = |
31236 | 52 |
let |
36142
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
53 |
val num_theorems = length name_thms_pairs |
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
54 |
val _ = priority ("Testing " ^ string_of_int num_theorems ^ |
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
55 |
" theorem" ^ plural_s num_theorems ^ "...") |
32525 | 56 |
val name_thm_pairs = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs |
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
57 |
val axclauses = cnf_rules_pairs (Proof.theory_of state) name_thm_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
|
58 |
val {context = ctxt, facts, goal} = Proof.goal state |
32941
72d48e333b77
eliminated extraneous wrapping of public records;
wenzelm
parents:
32937
diff
changeset
|
59 |
val problem = |
36063
cdc6855a6387
make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents:
35969
diff
changeset
|
60 |
{subgoal = subgoal, goal = (ctxt, (facts, goal)), |
35969 | 61 |
relevance_override = {add = [], del = [], only = false}, |
36232
4d425766a47f
don't redo an axiom selection in the first round of Sledgehammer "minimize"!;
blanchet
parents:
36231
diff
changeset
|
62 |
axiom_clauses = SOME axclauses, |
4d425766a47f
don't redo an axiom selection in the first round of Sledgehammer "minimize"!;
blanchet
parents:
36231
diff
changeset
|
63 |
filtered_clauses = SOME (the_default axclauses filtered_clauses)} |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
64 |
in |
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
65 |
prover params (K "") timeout problem |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
66 |
|> tap (priority o string_for_outcome o #outcome) |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
67 |
end |
31236 | 68 |
|
69 |
(* minimalization of thms *) |
|
70 |
||
36283
25e69e93954d
failure of reconstructing the Isar proof (e.g., exception) should not prevent Sledgehammer from showing the short one-liner proof -- but in "debug" mode we do want to know what the exception is
blanchet
parents:
36281
diff
changeset
|
71 |
fun minimize_theorems (params as {debug, minimize_timeout, isar_proof, modulus, |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
72 |
sorts, ...}) |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
73 |
prover atp_name i state name_thms_pairs = |
31236 | 74 |
let |
35969 | 75 |
val msecs = Time.toMilliseconds minimize_timeout |
36224 | 76 |
val n = length name_thms_pairs |
31236 | 77 |
val _ = |
36224 | 78 |
priority ("Sledgehammer minimizer: ATP " ^ quote atp_name ^ |
79 |
" with a time limit of " ^ string_of_int msecs ^ " ms.") |
|
35969 | 80 |
val test_thms_fun = |
36063
cdc6855a6387
make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents:
35969
diff
changeset
|
81 |
sledgehammer_test_theorems params prover minimize_timeout i state |
31752
19a5f1c8a844
use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents:
31409
diff
changeset
|
82 |
fun test_thms filtered thms = |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
83 |
case test_thms_fun filtered thms of |
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
84 |
(result as {outcome = NONE, ...}) => SOME result |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
85 |
| _ => NONE |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
86 |
|
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
87 |
val {context = ctxt, facts, goal} = Proof.goal state; |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
88 |
val n = Logic.count_prems (prop_of goal) |
31236 | 89 |
in |
90 |
(* try prove first to check result and get used theorems *) |
|
31409
d8537ba165b5
split preparing clauses and writing problemfile;
immler@in.tum.de
parents:
31236
diff
changeset
|
91 |
(case test_thms_fun NONE name_thms_pairs of |
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
92 |
result as {outcome = NONE, internal_thm_names, filtered_clauses, ...} => |
31236 | 93 |
let |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
94 |
val used = internal_thm_names |> Vector.foldr (op ::) [] |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
95 |
|> sort_distinct string_ord |
31236 | 96 |
val to_use = |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
97 |
if length used < length name_thms_pairs then |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
98 |
filter (fn (name1, _) => List.exists (curry (op =) name1) used) |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
99 |
name_thms_pairs |
33305 | 100 |
else name_thms_pairs |
36231
bede2d49ba3b
get rid of "conjecture_pos", which is no longer necessary now that it's Metis's job, not Sledgehammer's, to report inconsistent contexts
blanchet
parents:
36224
diff
changeset
|
101 |
val (min_thms, {proof, internal_thm_names, ...}) = |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
102 |
linear_minimize (test_thms (SOME filtered_clauses)) to_use |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
103 |
([], result) |
36224 | 104 |
val n = length min_thms |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
105 |
val _ = priority (cat_lines |
36224 | 106 |
["Minimized: " ^ string_of_int n ^ " theorem" ^ plural_s n] ^ ".") |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
107 |
in |
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
108 |
(SOME min_thms, |
36289
f75b6a3e1450
set "atps" reference's default value to "(remote_)e (remote_)spass (remote_)vampire", based on what is installed
blanchet
parents:
36287
diff
changeset
|
109 |
proof_text isar_proof debug modulus sorts ctxt |
36287
96f45c5ffb36
if Isar proof reconstruction is not supported, tell the user so they don't wonder why their "isar_proof" option did nothing
blanchet
parents:
36283
diff
changeset
|
110 |
(K "", proof, internal_thm_names, goal, i) |> fst) |
36223
217ca1273786
make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents:
36143
diff
changeset
|
111 |
end |
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
112 |
| {outcome = SOME TimedOut, ...} => |
36142
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
113 |
(NONE, "Timeout: You can increase the time limit using the \"timeout\" \ |
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
114 |
\option (e.g., \"timeout = " ^ |
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
115 |
string_of_int (10 + msecs div 1000) ^ " s\").") |
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
116 |
| {outcome = SOME UnknownError, ...} => |
36142
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
117 |
(* Failure sometimes mean timeout, unfortunately. *) |
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
118 |
(NONE, "Failure: No proof was found with the current time limit. You \ |
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
119 |
\can increase the time limit using the \"timeout\" \ |
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36063
diff
changeset
|
120 |
\option (e.g., \"timeout = " ^ |
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
121 |
string_of_int (10 + msecs div 1000) ^ " s\").") |
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
122 |
| {message, ...} => (NONE, "ATP error: " ^ message)) |
35865 | 123 |
handle Sledgehammer_HOL_Clause.TRIVIAL => |
36063
cdc6855a6387
make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents:
35969
diff
changeset
|
124 |
(SOME [], metis_line i n []) |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
125 |
| ERROR msg => (NONE, "Error: " ^ msg) |
31236 | 126 |
end |
127 |
||
35866
513074557e06
move the Sledgehammer Isar commands together into one file;
blanchet
parents:
35865
diff
changeset
|
128 |
end; |