author | wenzelm |
Tue, 14 Oct 2008 13:01:58 +0200 | |
changeset 28582 | c269a3045fdf |
parent 28571 | 47d88239658d |
child 28586 | d238b83ba3fc |
permissions | -rw-r--r-- |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
1 |
(* Title: HOL/Tools/atp_manager.ML |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
3 |
Author: Fabian Immler, TU Muenchen |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
4 |
|
28571 | 5 |
ATP threads are registered here. |
6 |
Threads with the same birth-time are seen as one group. |
|
7 |
All threads of a group are killed when one thread of it has been successful, |
|
8 |
or after a certain time, |
|
9 |
or when the maximum number of threads exceeds; then the oldest thread is killed. |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
10 |
*) |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
11 |
|
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
12 |
signature ATP_MANAGER = |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
13 |
sig |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
14 |
val kill_all: unit -> unit |
28582 | 15 |
val info: unit -> unit |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
16 |
val set_atps: string -> unit |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
17 |
val set_max_atp: int -> unit |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
18 |
val set_timeout: int -> unit |
28571 | 19 |
val atp_thread: (unit -> 'a option) -> ('a -> string) -> Thread.thread |
20 |
val add_prover: string -> (int -> Proof.state -> Thread.thread) -> theory -> theory |
|
28484 | 21 |
val print_provers: theory -> unit |
28571 | 22 |
val sledgehammer: string list -> Proof.state -> unit |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
23 |
end; |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
24 |
|
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
25 |
structure AtpManager : ATP_MANAGER = |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
26 |
struct |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
27 |
|
28582 | 28 |
(* data structures over threads *) |
29 |
||
30 |
structure ThreadHeap = HeapFun |
|
31 |
( |
|
32 |
type elem = Time.time * Thread.thread; |
|
33 |
fun ord ((a, _), (b, _)) = Time.compare (a, b); |
|
34 |
) |
|
35 |
||
36 |
val lookup_thread = AList.lookup Thread.equal; |
|
37 |
val delete_thread = AList.delete Thread.equal; |
|
38 |
val update_thread = AList.update Thread.equal; |
|
39 |
||
40 |
||
41 |
(* state of thread manager *) |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
42 |
|
28582 | 43 |
datatype T = State of |
44 |
{timeout_heap: ThreadHeap.T, |
|
45 |
oldest_heap: ThreadHeap.T, |
|
46 |
active: (Thread.thread * (Time.time * Time.time * string)) list, |
|
47 |
cancelling: (Thread.thread * (Time.time * Time.time * string)) list}; |
|
48 |
||
49 |
fun make_state timeout_heap oldest_heap active cancelling = |
|
50 |
State {timeout_heap = timeout_heap, oldest_heap = oldest_heap, |
|
51 |
active = active, cancelling = cancelling}; |
|
52 |
||
53 |
val state = Synchronized.var "atp_manager" (make_state ThreadHeap.empty ThreadHeap.empty [] []); |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
54 |
|
28582 | 55 |
|
56 |
(* the thread manager thread *) |
|
57 |
||
58 |
(*watches over running threads and interrupts them if required*) |
|
59 |
val managing_thread = ref (NONE: Thread.thread option); |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
60 |
|
28582 | 61 |
|
62 |
(* unregister thread from thread manager -- move to cancelling *) |
|
63 |
||
64 |
fun unregister success message thread = Synchronized.change_result state |
|
65 |
(fn State {timeout_heap, oldest_heap, active, cancelling} => |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
66 |
let |
28582 | 67 |
val info = lookup_thread active thread |
68 |
||
69 |
(* get birthtime of unregistering thread if successful - for group-killing*) |
|
70 |
val birthtime = case info of NONE => Time.zeroTime |
|
71 |
| SOME (tb, _, _) => if success then tb else Time.zeroTime |
|
72 |
||
73 |
(* move unregistering thread to cancelling *) |
|
74 |
val active' = delete_thread thread active |
|
75 |
val cancelling' = case info of NONE => cancelling |
|
76 |
| SOME (tb, _, desc) => update_thread (thread, (tb, Time.now (), desc)) cancelling |
|
77 |
||
78 |
(* move all threads of the same group to cancelling *) |
|
79 |
val group_threads = active |> map_filter (fn (th, (tb, _, desc)) => |
|
80 |
if tb = birthtime then SOME (th, (tb, Time.now (), desc)) else NONE) |
|
81 |
val active'' = filter_out (fn (_, (tb, _, _)) => tb = birthtime) active' |
|
82 |
val cancelling'' = append group_threads cancelling' |
|
83 |
||
84 |
(* message for user *) |
|
85 |
val message' = case info of NONE => "" |
|
86 |
| SOME (_, _, desc) => "Sledgehammer: " ^ desc ^ "\n " ^ message ^ |
|
87 |
(if null group_threads then "" |
|
88 |
else "\nInterrupted " ^ Int.toString (length group_threads - 1) ^ " other group members") |
|
89 |
in (message', make_state timeout_heap oldest_heap active'' cancelling'') end); |
|
90 |
||
91 |
||
92 |
(* start a watching thread which runs forever -- only one may exist *) |
|
93 |
||
94 |
fun check_thread_manager () = |
|
95 |
if (case ! managing_thread of SOME thread => Thread.isActive thread | NONE => false) |
|
96 |
then () else managing_thread := SOME (SimpleThread.fork false (fn () => |
|
28571 | 97 |
let |
28582 | 98 |
val min_wait_time = Time.fromMilliseconds 300 |
99 |
val max_wait_time = Time.fromSeconds 10 |
|
100 |
||
101 |
(* wait for next thread to cancel, or maximum*) |
|
102 |
fun time_limit (State {timeout_heap, ...}) = |
|
103 |
(case try ThreadHeap.min timeout_heap of |
|
104 |
NONE => SOME (Time.+ (Time.now (), max_wait_time)) |
|
105 |
| SOME (time, _) => SOME time) |
|
106 |
||
107 |
(* action: cancel find threads whose timeout is reached, and interrupt cancelling threads *) |
|
108 |
fun action (State {timeout_heap, oldest_heap, active, cancelling}) = |
|
109 |
let val (timeout_threads, timeout_heap') = |
|
110 |
ThreadHeap.upto (Time.now (), Thread.self ()) timeout_heap |
|
111 |
in |
|
112 |
if null timeout_threads andalso null cancelling then NONE |
|
113 |
else |
|
114 |
let |
|
115 |
val _ = List.app (SimpleThread.interrupt o #1) cancelling |
|
116 |
val cancelling' = filter (Thread.isActive o #1) cancelling |
|
117 |
val state' = make_state timeout_heap' oldest_heap active cancelling' |
|
118 |
in SOME (map #2 timeout_threads, state') end |
|
119 |
end |
|
120 |
in |
|
121 |
while true do |
|
122 |
((* cancel threads found by action *) |
|
123 |
Synchronized.timed_access state time_limit action |
|
124 |
|> these |
|
125 |
|> List.app (priority o unregister false "Interrupted (reached timeout)"); |
|
28571 | 126 |
(* give threads time to respond to interrupt *) |
127 |
OS.Process.sleep min_wait_time) |
|
28582 | 128 |
end)); |
129 |
||
130 |
||
131 |
(* thread is registered here by sledgehammer *) |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
132 |
|
28582 | 133 |
fun register birthtime deadtime (thread, desc) = |
134 |
(check_thread_manager (); |
|
135 |
Synchronized.change state (fn State {timeout_heap, oldest_heap, active, cancelling} => |
|
136 |
let |
|
137 |
val timeout_heap' = ThreadHeap.insert (deadtime, thread) timeout_heap |
|
138 |
val oldest_heap' = ThreadHeap.insert (birthtime, thread) oldest_heap |
|
139 |
val active' = update_thread (thread, (birthtime, deadtime, desc)) active |
|
140 |
in make_state timeout_heap' oldest_heap' active' cancelling end)); |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
141 |
|
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
142 |
|
28582 | 143 |
(* move all threads to cancelling *) |
144 |
||
145 |
fun kill_all () = Synchronized.change state |
|
146 |
(fn State {timeout_heap, oldest_heap, active, cancelling} => |
|
147 |
let val formerly_active = map (fn (th, (tb, _, desc)) => (th, (tb, Time.now (), desc))) active |
|
148 |
in make_state timeout_heap oldest_heap [] (formerly_active @ cancelling) end); |
|
149 |
||
150 |
||
151 |
(* information on running threads *) |
|
152 |
||
153 |
fun info () = |
|
154 |
let |
|
155 |
val State {timeout_heap, oldest_heap, active, cancelling} = Synchronized.value state |
|
28571 | 156 |
fun running_info (_, (birth_time, dead_time, desc)) = "Running: " |
28582 | 157 |
^ ((Int.toString o Time.toSeconds) (Time.- (Time.now (), birth_time))) |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
158 |
^ " s -- " |
28582 | 159 |
^ ((Int.toString o Time.toSeconds) (Time.- (dead_time, Time.now ()))) |
28571 | 160 |
^ " s to live:\n" ^ desc |
161 |
fun cancelling_info (_, (_, dead_time, desc)) = "Trying to interrupt thread since " |
|
28582 | 162 |
^ (Int.toString o Time.toSeconds) (Time.- (Time.now (), dead_time)) |
28571 | 163 |
^ " s:\n" ^ desc |
164 |
val running = if null active then "No ATPs running." |
|
165 |
else String.concatWith "\n\n" ("--- RUNNING ATPs ---" :: |
|
166 |
(map (fn entry => running_info entry) active)) |
|
167 |
val interrupting = if null cancelling then "" |
|
168 |
else String.concatWith "\n\n" ("--- TRYING TO INTERRUPT FOLLOWING ATPs ---" :: |
|
169 |
(map (fn entry => cancelling_info entry) cancelling)) |
|
28582 | 170 |
in writeln (running ^ "\n" ^ interrupting) end; |
171 |
||
172 |
||
173 |
(* thread wrapping an atp-call *) |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
174 |
|
28582 | 175 |
fun atp_thread call_prover produce_answer = |
176 |
SimpleThread.fork true (fn () => |
|
177 |
let |
|
178 |
val result = call_prover () |
|
179 |
val message = case result of NONE => "Failed." |
|
180 |
| SOME result => "Try this command: " ^ produce_answer result |
|
181 |
in priority (unregister (is_some result) message (Thread.self ())) |
|
182 |
end handle Interrupt => ()); |
|
183 |
||
28484 | 184 |
|
28582 | 185 |
(* preferences *) |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
186 |
|
28582 | 187 |
val atps = ref "e spass"; |
188 |
val maximum_atps = ref 5; (* ~1 means infinite number of atps*) |
|
189 |
val timeout = ref 60; |
|
190 |
||
191 |
fun set_atps str = CRITICAL (fn () => atps := str); |
|
192 |
fun set_max_atp number = CRITICAL (fn () => maximum_atps := number); |
|
193 |
fun set_timeout time = CRITICAL (fn () => timeout := time); |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
194 |
|
28582 | 195 |
val _ = ProofGeneralPgip.add_preference "Proof" |
196 |
{name = "ATP - Provers (see print_atps)", |
|
197 |
descr = "Default automatic provers (seperated by whitespace)", |
|
198 |
default = !atps, |
|
199 |
pgiptype = PgipTypes.Pgipstring, |
|
200 |
get = fn () => !atps, |
|
201 |
set = set_atps} |
|
202 |
handle ERROR _ => warning "Preference already exists"; |
|
28484 | 203 |
|
28582 | 204 |
val _ = ProofGeneralPgip.add_preference "Proof" |
205 |
{name = "ATP - Maximum number", |
|
206 |
descr = "How many provers may run in parallel", |
|
207 |
default = Int.toString (! maximum_atps), |
|
208 |
pgiptype = PgipTypes.Pgipstring, |
|
209 |
get = fn () => Int.toString (! maximum_atps), |
|
210 |
set = fn str => set_max_atp (the_default 1 (Int.fromString str))} |
|
211 |
handle ERROR _ => warning "Preference already exists"; |
|
28484 | 212 |
|
28582 | 213 |
val _ = ProofGeneralPgip.add_preference "Proof" |
214 |
{name = "ATP - Timeout", |
|
215 |
descr = "ATPs will be interrupted after this time (in seconds)", |
|
216 |
default = Int.toString (! timeout), |
|
217 |
pgiptype = PgipTypes.Pgipstring, |
|
218 |
get = fn () => Int.toString (! timeout), |
|
219 |
set = fn str => set_timeout (the_default 60 (Int.fromString str))} |
|
220 |
handle ERROR _ => warning "Preference already exists"; |
|
28484 | 221 |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
222 |
|
28582 | 223 |
(* named provers *) |
28484 | 224 |
|
28582 | 225 |
fun err_dup_prover name = error ("Duplicate prover: " ^ quote name); |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
226 |
|
28582 | 227 |
structure Provers = TheoryDataFun |
228 |
( |
|
229 |
type T = ((int -> Proof.state -> Thread.thread) * stamp) Symtab.table |
|
230 |
val empty = Symtab.empty |
|
231 |
val copy = I |
|
232 |
val extend = I |
|
233 |
fun merge _ tabs : T = Symtab.merge (eq_snd op =) tabs |
|
234 |
handle Symtab.DUP dup => err_dup_prover dup; |
|
235 |
); |
|
28484 | 236 |
|
28582 | 237 |
fun add_prover name prover_fn = |
238 |
Provers.map (Symtab.update_new (name, (prover_fn, stamp ()))) |
|
239 |
handle Symtab.DUP dup => err_dup_prover dup; |
|
240 |
||
241 |
fun print_provers thy = Pretty.writeln |
|
242 |
(Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy)))); |
|
28571 | 243 |
|
28582 | 244 |
fun prover_desc state subgoal name = |
245 |
let val (ctxt, (chain_ths, goal)) = Proof.get_goal state |
|
246 |
in "External prover " ^ quote name ^ " for Subgoal " ^ Int.toString subgoal^ ":\n" ^ Syntax.string_of_term ctxt (List.nth(prems_of goal, subgoal-1)) end |
|
247 |
||
248 |
fun run_prover state subgoal name = |
|
249 |
(case Symtab.lookup (Provers.get (Proof.theory_of state)) name of |
|
250 |
NONE => (warning ("Unknown external prover: " ^ quote name); NONE) |
|
251 |
| SOME (prover_fn, _) => SOME (prover_fn subgoal state, prover_desc state subgoal name)); |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
252 |
|
28484 | 253 |
|
28582 | 254 |
(* kill excessive atp threads *) |
28484 | 255 |
|
28582 | 256 |
local |
28484 | 257 |
|
28582 | 258 |
fun excessive_atps active = |
259 |
let val max = ! maximum_atps |
|
260 |
in length active > max andalso max > ~1 end; |
|
28484 | 261 |
|
28582 | 262 |
fun kill_oldest () = |
263 |
let exception Unchanged in |
|
264 |
Synchronized.change_result state (fn State {timeout_heap, oldest_heap, active, cancelling} => |
|
265 |
if ThreadHeap.is_empty oldest_heap orelse not (excessive_atps active) |
|
266 |
then raise Unchanged |
|
267 |
else |
|
268 |
let val ((_, oldest_thread), oldest_heap') = ThreadHeap.min_elem oldest_heap |
|
269 |
in (oldest_thread, make_state timeout_heap oldest_heap' active cancelling) end) |
|
270 |
|> (priority o unregister false "Interrupted (Maximum number of ATPs exceeded).") |
|
271 |
handle Unchanged => () |
|
28484 | 272 |
end; |
273 |
||
28582 | 274 |
in |
275 |
||
276 |
fun kill_excessive () = |
|
277 |
let val State {active, ...} = Synchronized.value state |
|
278 |
in if excessive_atps active then (kill_oldest (); kill_excessive ()) else () end; |
|
279 |
||
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
280 |
end; |
28582 | 281 |
|
282 |
||
283 |
(* sledghammer for first subgoal *) |
|
284 |
||
285 |
fun sledgehammer names proof_state = |
|
286 |
let |
|
287 |
val proverids = |
|
288 |
if null names then String.tokens (Symbol.is_ascii_blank o String.str) (! atps) |
|
289 |
else names |
|
290 |
val threads_names = map_filter (run_prover proof_state 1) proverids |
|
291 |
val birthtime = Time.now () |
|
292 |
val deadtime = Time.+ (Time.now (), Time.fromSeconds (! timeout)) |
|
293 |
val _ = List.app (register birthtime deadtime) threads_names |
|
294 |
val _ = kill_excessive () |
|
295 |
in () end |
|
296 |
||
297 |
||
298 |
(* concrete syntax *) |
|
299 |
||
300 |
local structure K = OuterKeyword and P = OuterParse in |
|
301 |
||
302 |
val _ = |
|
303 |
OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag |
|
304 |
(Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill_all)); |
|
305 |
||
306 |
val _ = |
|
307 |
OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag |
|
308 |
(Scan.succeed (Toplevel.no_timing o Toplevel.imperative info)); |
|
309 |
||
310 |
val _ = |
|
311 |
OuterSyntax.improper_command "print_atps" "print external provers" K.diag |
|
312 |
(Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o |
|
313 |
Toplevel.keep (print_provers o Toplevel.theory_of))); |
|
314 |
||
315 |
val _ = |
|
316 |
OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag |
|
317 |
(Scan.repeat P.xname >> (fn names => Toplevel.no_timing o Toplevel.unknown_proof o |
|
318 |
Toplevel.keep ((sledgehammer names) o Toplevel.proof_of))); |
|
319 |
||
320 |
end; |
|
321 |
||
322 |
end; |