author | wenzelm |
Thu, 15 Oct 2009 17:06:19 +0200 | |
changeset 32947 | 3c19b98a35cd |
parent 32942 | b6711ec9de26 |
child 32948 | e95a4be101a8 |
permissions | -rw-r--r-- |
32327
0971cc0b6a57
src/HOL/Tools/ATP_Manager as separate component, with (almost) everything in one place;
wenzelm
parents:
32091
diff
changeset
|
1 |
(* Title: HOL/Tools/ATP_Manager/atp_minimal.ML |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
2 |
Author: Philipp Meyer, TU Muenchen |
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
3 |
|
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
4 |
Minimalization of theorem list for metis by using an external automated theorem prover |
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
5 |
*) |
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
6 |
|
32525 | 7 |
signature ATP_MINIMAL = |
8 |
sig |
|
32936 | 9 |
val minimalize: ATP_Wrapper.prover -> string -> int -> Proof.state -> |
32571
d4bb776874b8
count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents:
32538
diff
changeset
|
10 |
(string * thm list) list -> ((string * thm list) list * int) option * string |
32525 | 11 |
end |
12 |
||
32936 | 13 |
structure ATP_Minimal: ATP_MINIMAL = |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
14 |
struct |
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
15 |
|
31236 | 16 |
(* minimalization algorithm *) |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
17 |
|
31236 | 18 |
local |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
19 |
fun isplit (l, r) [] = (l, r) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
20 |
| isplit (l, r) [h] = (h :: l, r) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
21 |
| isplit (l, r) (h1 :: h2 :: t) = isplit (h1 :: l, h2 :: r) t |
31236 | 22 |
in |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
23 |
fun split lst = isplit ([], []) lst |
31236 | 24 |
end |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
25 |
|
31236 | 26 |
local |
27 |
fun min p sup [] = raise Empty |
|
28 |
| min p sup [s0] = [s0] |
|
29 |
| min p sup s = |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
30 |
let |
31236 | 31 |
val (l0, r0) = split s |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
32 |
in |
31236 | 33 |
if p (sup @ l0) |
34 |
then min p sup l0 |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
35 |
else |
31236 | 36 |
if p (sup @ r0) |
37 |
then min p sup r0 |
|
38 |
else |
|
39 |
let |
|
40 |
val l = min p (sup @ r0) l0 |
|
41 |
val r = min p (sup @ l) r0 |
|
42 |
in |
|
43 |
l @ r |
|
44 |
end |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
45 |
end |
31236 | 46 |
in |
47 |
(* return a minimal subset v of s that satisfies p |
|
48 |
@pre p(s) & ~p([]) & monotone(p) |
|
49 |
@post v subset s & p(v) & |
|
50 |
forall e in v. ~p(v \ e) |
|
51 |
*) |
|
32538
86035c5f61b5
Fixed "minimal" to cover the case that "p []" holds (excluded in the article by Bradley & Manna)
nipkow
parents:
32525
diff
changeset
|
52 |
fun minimal p s = |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
53 |
let |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
54 |
val count = Unsynchronized.ref 0 |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
55 |
fun p_count xs = (Unsynchronized.inc count; p xs) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
56 |
val v = |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
57 |
(case min p_count [] s of |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
58 |
[x] => if p_count [] then [] else [x] |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
59 |
| m => m); |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
60 |
in (v, ! count) end |
31236 | 61 |
end |
62 |
||
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
63 |
|
31236 | 64 |
(* failure check and producing answer *) |
65 |
||
66 |
datatype 'a prove_result = Success of 'a | Failure | Timeout | Error |
|
67 |
||
68 |
val string_of_result = |
|
69 |
fn Success _ => "Success" |
|
70 |
| Failure => "Failure" |
|
71 |
| Timeout => "Timeout" |
|
72 |
| Error => "Error" |
|
73 |
||
74 |
val failure_strings = |
|
75 |
[("SPASS beiseite: Ran out of time.", Timeout), |
|
76 |
("Timeout", Timeout), |
|
77 |
("time limit exceeded", Timeout), |
|
78 |
("# Cannot determine problem status within resource limit", Timeout), |
|
79 |
("Error", Error)] |
|
80 |
||
32941
72d48e333b77
eliminated extraneous wrapping of public records;
wenzelm
parents:
32937
diff
changeset
|
81 |
fun produce_answer (result: ATP_Wrapper.prover_result) = |
32864
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
82 |
let |
32942
b6711ec9de26
misc tuning and recovery of Isabelle coding style;
wenzelm
parents:
32941
diff
changeset
|
83 |
val {success, proof = result_string, internal_thm_names = thm_name_vec, |
b6711ec9de26
misc tuning and recovery of Isabelle coding style;
wenzelm
parents:
32941
diff
changeset
|
84 |
filtered_clauses = filtered, ...} = result |
32864
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
85 |
in |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
86 |
if success then |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
87 |
(Success (Vector.foldr (op ::) [] thm_name_vec, filtered), result_string) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
88 |
else |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
89 |
let |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
90 |
val failure = failure_strings |> get_first (fn (s, t) => |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
91 |
if String.isSubstring s result_string then SOME (t, result_string) else NONE) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
92 |
in |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
93 |
(case failure of |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
94 |
SOME res => res |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
95 |
| NONE => (Failure, result_string)) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
96 |
end |
32864
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
97 |
end |
31236 | 98 |
|
32936 | 99 |
|
31236 | 100 |
(* wrapper for calling external prover *) |
101 |
||
32864
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
102 |
fun sh_test_thms prover time_limit subgoalno state filtered name_thms_pairs = |
31236 | 103 |
let |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
104 |
val _ = priority ("Testing " ^ string_of_int (length name_thms_pairs) ^ " theorems... ") |
32525 | 105 |
val name_thm_pairs = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs |
31236 | 106 |
val axclauses = ResAxioms.cnf_rules_pairs (Proof.theory_of state) name_thm_pairs |
32941
72d48e333b77
eliminated extraneous wrapping of public records;
wenzelm
parents:
32937
diff
changeset
|
107 |
val problem = |
72d48e333b77
eliminated extraneous wrapping of public records;
wenzelm
parents:
32937
diff
changeset
|
108 |
{with_full_types = ! ATP_Manager.full_types, |
32864
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
109 |
subgoal = subgoalno, |
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
110 |
goal = Proof.get_goal state, |
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
111 |
axiom_clauses = SOME axclauses, |
32941
72d48e333b77
eliminated extraneous wrapping of public records;
wenzelm
parents:
32937
diff
changeset
|
112 |
filtered_clauses = filtered} |
32864
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
113 |
val (result, proof) = produce_answer (prover problem time_limit) |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
114 |
val _ = priority (string_of_result result) |
31236 | 115 |
in |
116 |
(result, proof) |
|
117 |
end |
|
118 |
||
119 |
||
120 |
(* minimalization of thms *) |
|
121 |
||
122 |
fun minimalize prover prover_name time_limit state name_thms_pairs = |
|
123 |
let |
|
124 |
val _ = |
|
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
125 |
priority ("Minimize called with " ^ string_of_int (length name_thms_pairs) ^ |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
126 |
" theorems, prover: " ^ prover_name ^ |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
127 |
", time limit: " ^ string_of_int time_limit ^ " seconds") |
32864
a226f29d4bdc
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents:
32740
diff
changeset
|
128 |
val test_thms_fun = sh_test_thms prover time_limit 1 state |
31752
19a5f1c8a844
use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents:
31409
diff
changeset
|
129 |
fun test_thms filtered thms = |
19a5f1c8a844
use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents:
31409
diff
changeset
|
130 |
case test_thms_fun filtered thms of (Success _, _) => true | _ => false |
31236 | 131 |
in |
132 |
(* 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
|
133 |
(case test_thms_fun NONE name_thms_pairs of |
31752
19a5f1c8a844
use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents:
31409
diff
changeset
|
134 |
(Success (used, filtered), _) => |
31236 | 135 |
let |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
136 |
val ordered_used = sort_distinct string_ord used |
31236 | 137 |
val to_use = |
138 |
if length ordered_used < length name_thms_pairs then |
|
139 |
filter (fn (name1, _) => List.exists (equal name1) ordered_used) name_thms_pairs |
|
140 |
else |
|
141 |
name_thms_pairs |
|
32571
d4bb776874b8
count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents:
32538
diff
changeset
|
142 |
val (min_thms, n) = if null to_use then ([], 0) |
32525 | 143 |
else minimal (test_thms (SOME filtered)) to_use |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
144 |
val min_names = sort_distinct string_ord (map fst min_thms) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
145 |
val _ = priority (cat_lines |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
146 |
["Interations: " ^ string_of_int n, |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
147 |
"Minimal " ^ string_of_int (length min_thms) ^ " theorems"]) |
31236 | 148 |
in |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
149 |
(SOME (min_thms, n), "Try this command: " ^ |
31236 | 150 |
Markup.markup Markup.sendback ("apply (metis " ^ space_implode " " min_names ^ ")")) |
151 |
end |
|
152 |
| (Timeout, _) => |
|
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
153 |
(NONE, "Timeout: You may need to increase the time limit of " ^ |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
154 |
string_of_int time_limit ^ " seconds. Call atp_minimize [time=...] ") |
31236 | 155 |
| (Error, msg) => |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
156 |
(NONE, "Error in prover: " ^ msg) |
31236 | 157 |
| (Failure, _) => |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
158 |
(NONE, "Failure: No proof with the theorems supplied")) |
31236 | 159 |
handle ResHolClause.TOO_TRIVIAL => |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
160 |
(SOME ([], 0), "Trivial: Try this command: " ^ Markup.markup Markup.sendback "apply metis") |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
161 |
| ERROR msg => (NONE, "Error: " ^ msg) |
31236 | 162 |
end |
163 |
||
164 |
||
165 |
(* Isar command and parsing input *) |
|
166 |
||
167 |
local structure K = OuterKeyword and P = OuterParse and T = OuterLex in |
|
168 |
||
169 |
fun get_thms context = |
|
170 |
map (fn (name, interval) => |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
171 |
let |
31236 | 172 |
val thmref = Facts.Named ((name, Position.none), interval) |
173 |
val ths = ProofContext.get_fact context thmref |
|
174 |
val name' = Facts.string_of_ref thmref |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
175 |
in |
31236 | 176 |
(name', ths) |
177 |
end) |
|
178 |
||
179 |
val default_prover = "remote_vampire" |
|
180 |
val default_time_limit = 5 |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
181 |
|
31236 | 182 |
fun get_time_limit_arg time_string = |
183 |
(case Int.fromString time_string of |
|
184 |
SOME t => t |
|
185 |
| NONE => error ("Invalid time limit: " ^ quote time_string)) |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
186 |
|
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
187 |
fun get_opt (name, a) (p, t) = |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
188 |
(case name of |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
189 |
"time" => (p, get_time_limit_arg a) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
190 |
| "atp" => (a, t) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
191 |
| n => error ("Invalid argument: " ^ n)) |
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
192 |
|
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
193 |
fun get_options args = fold get_opt args (default_prover, default_time_limit) |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
194 |
|
31236 | 195 |
fun sh_min_command args thm_names state = |
196 |
let |
|
197 |
val (prover_name, time_limit) = get_options args |
|
198 |
val prover = |
|
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
199 |
(case ATP_Manager.get_prover (Proof.theory_of state) prover_name of |
31236 | 200 |
SOME prover => prover |
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
201 |
| NONE => error ("Unknown prover: " ^ quote prover_name)) |
31236 | 202 |
val name_thms_pairs = get_thms (Proof.context_of state) thm_names |
203 |
in |
|
32947
3c19b98a35cd
ATP_Manager.get_prover: canonical argument order;
wenzelm
parents:
32942
diff
changeset
|
204 |
writeln (#2 (minimalize prover prover_name time_limit state name_thms_pairs)) |
31236 | 205 |
end |
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
206 |
|
31236 | 207 |
val parse_args = Scan.optional (Args.bracks (P.list (P.xname --| P.$$$ "=" -- P.xname))) [] |
208 |
val parse_thm_names = Scan.repeat (P.xname -- Scan.option Attrib.thm_sel) |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
209 |
|
31236 | 210 |
val _ = |
211 |
OuterSyntax.command "atp_minimize" "minimize theorem list with external prover" K.diag |
|
212 |
(parse_args -- parse_thm_names >> (fn (args, thm_names) => |
|
213 |
Toplevel.no_timing o Toplevel.unknown_proof o |
|
214 |
Toplevel.keep (sh_min_command args thm_names o Toplevel.proof_of))) |
|
31037
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
215 |
|
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
216 |
end |
ac8669134e7a
added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff
changeset
|
217 |
|
31236 | 218 |
end |
219 |