1 (* Title: HOL/Tools/ATP_Manager/atp_manager.ML
2 Author: Fabian Immler, TU Muenchen
4 Author: Jasmin Blanchette, TU Muenchen
6 Central manager component for ATP threads.
9 signature ATP_MANAGER =
11 type name_pool = Sledgehammer_HOL_Clause.name_pool
12 type relevance_override = Sledgehammer_Fact_Filter.relevance_override
13 type minimize_command = Sledgehammer_Proof_Reconstruct.minimize_command
22 relevance_threshold: real,
23 relevance_convergence: real,
24 theory_relevant: bool option,
27 isar_shrink_factor: int,
29 minimize_timeout: Time.time}
32 goal: Proof.context * (thm list * thm),
33 relevance_override: relevance_override,
34 axiom_clauses: (thm * (string * int)) list option,
35 filtered_clauses: (thm * (string * int)) list option}
37 Unprovable | TimedOut | OutOfResources | OldSpass | MalformedInput |
38 MalformedOutput | UnknownError
40 {outcome: failure option,
42 pool: name_pool option,
43 relevant_thm_names: string list,
44 atp_run_time_in_msecs: int,
47 internal_thm_names: string Vector.vector,
48 conjecture_shape: int list list,
49 filtered_clauses: (thm * (string * int)) list}
51 params -> minimize_command -> Time.time -> problem -> prover_result
53 val kill_atps: unit -> unit
54 val running_atps: unit -> unit
55 val messages: int option -> unit
56 val add_prover: string * prover -> theory -> theory
57 val get_prover: theory -> string -> prover
58 val available_atps: theory -> unit
59 val start_prover_thread:
60 params -> Time.time -> Time.time -> int -> int -> relevance_override
61 -> (string -> minimize_command) -> Proof.state -> string -> unit
64 structure ATP_Manager : ATP_MANAGER =
67 open Sledgehammer_Util
68 open Sledgehammer_Fact_Filter
69 open Sledgehammer_Proof_Reconstruct
71 (** problems, results, provers, etc. **)
81 relevance_threshold: real,
82 relevance_convergence: real,
83 theory_relevant: bool option,
86 isar_shrink_factor: int,
88 minimize_timeout: Time.time}
92 goal: Proof.context * (thm list * thm),
93 relevance_override: relevance_override,
94 axiom_clauses: (thm * (string * int)) list option,
95 filtered_clauses: (thm * (string * int)) list option};
98 Unprovable | TimedOut | OutOfResources | OldSpass | MalformedInput |
99 MalformedOutput | UnknownError
102 {outcome: failure option,
104 pool: name_pool option,
105 relevant_thm_names: string list,
106 atp_run_time_in_msecs: int,
109 internal_thm_names: string Vector.vector,
110 conjecture_shape: int list list,
111 filtered_clauses: (thm * (string * int)) list};
114 params -> minimize_command -> Time.time -> problem -> prover_result
119 val message_store_limit = 20;
120 val message_display_limit = 5;
123 (** thread management **)
125 (* data structures over threads *)
127 structure Thread_Heap = Heap
129 type elem = Time.time * Thread.thread;
130 fun ord ((a, _), (b, _)) = Time.compare (a, b);
133 fun lookup_thread xs = AList.lookup Thread.equal xs;
134 fun delete_thread xs = AList.delete Thread.equal xs;
135 fun update_thread xs = AList.update Thread.equal xs;
138 (* state of thread manager *)
141 {manager: Thread.thread option,
142 timeout_heap: Thread_Heap.T,
143 active: (Thread.thread * (Time.time * Time.time * string)) list,
144 cancelling: (Thread.thread * (Time.time * string)) list,
145 messages: string list,
148 fun make_state manager timeout_heap active cancelling messages store : state =
149 {manager = manager, timeout_heap = timeout_heap, active = active,
150 cancelling = cancelling, messages = messages, store = store};
152 val global_state = Synchronized.var "atp_manager"
153 (make_state NONE Thread_Heap.empty [] [] [] []);
156 (* unregister ATP thread *)
158 fun unregister ({verbose, ...} : params) message thread =
159 Synchronized.change global_state
160 (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
161 (case lookup_thread active thread of
162 SOME (birth_time, _, desc) =>
164 val active' = delete_thread thread active;
165 val now = Time.now ()
166 val cancelling' = (thread, (now, desc)) :: cancelling;
168 desc ^ "\n" ^ message ^
170 "Total time: " ^ Int.toString (Time.toMilliseconds
171 (Time.- (now, birth_time))) ^ " ms.\n"
174 val messages' = message' :: messages;
175 val store' = message' ::
176 (if length store <= message_store_limit then store
177 else #1 (chop message_store_limit store));
178 in make_state manager timeout_heap active' cancelling' messages' store' end
182 (* main manager thread -- only one may exist *)
184 val min_wait_time = Time.fromMilliseconds 300;
185 val max_wait_time = Time.fromSeconds 10;
187 (* This is a workaround for Proof General's off-by-a-few sendback display bug,
188 whereby "pr" in "proof" is not highlighted. *)
189 val break_into_chunks =
190 map (replace_all "\n\n" "\000") #> maps (space_explode "\000")
192 fun print_new_messages () =
193 case Synchronized.change_result global_state
194 (fn {manager, timeout_heap, active, cancelling, messages, store} =>
195 (messages, make_state manager timeout_heap active cancelling []
199 msgs |> break_into_chunks
200 |> (fn msg :: msgs => "Sledgehammer: " ^ msg :: msgs)
203 fun check_thread_manager params = Synchronized.change global_state
204 (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
205 if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
206 else let val manager = SOME (Toplevel.thread false (fn () =>
208 fun time_limit timeout_heap =
209 (case try Thread_Heap.min timeout_heap of
210 NONE => Time.+ (Time.now (), max_wait_time)
211 | SOME (time, _) => time);
213 (*action: find threads whose timeout is reached, and interrupt cancelling threads*)
214 fun action {manager, timeout_heap, active, cancelling, messages, store} =
215 let val (timeout_threads, timeout_heap') =
216 Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
218 if null timeout_threads andalso null cancelling
222 val _ = List.app (Simple_Thread.interrupt o #1) cancelling;
223 val cancelling' = filter (Thread.isActive o #1) cancelling;
224 val state' = make_state manager timeout_heap' active cancelling' messages store;
225 in SOME (map #2 timeout_threads, state') end
228 while Synchronized.change_result global_state
229 (fn state as {timeout_heap, active, cancelling, messages, store, ...} =>
230 if null active andalso null cancelling andalso null messages
231 then (false, make_state NONE timeout_heap active cancelling messages store)
234 (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
236 |> List.app (unregister params "Timed out.\n");
237 print_new_messages ();
238 (*give threads some time to respond to interrupt*)
239 OS.Process.sleep min_wait_time)
241 in make_state manager timeout_heap active cancelling messages store end);
244 (* register ATP thread *)
246 fun register params birth_time death_time (thread, desc) =
247 (Synchronized.change global_state
248 (fn {manager, timeout_heap, active, cancelling, messages, store} =>
250 val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
251 val active' = update_thread (thread, (birth_time, death_time, desc)) active;
252 val state' = make_state manager timeout_heap' active' cancelling messages store;
254 check_thread_manager params);
258 (** user commands **)
262 fun kill_atps () = Synchronized.change global_state
263 (fn {manager, timeout_heap, active, cancelling, messages, store} =>
265 val killing = map (fn (th, (_, _, desc)) => (th, (Time.now (), desc))) active;
266 val state' = make_state manager timeout_heap [] (killing @ cancelling) messages store;
267 val _ = if null active then ()
268 else priority "Killed active Sledgehammer threads."
274 fun seconds time = string_of_int (Time.toSeconds time) ^ "s";
276 fun running_atps () =
278 val {active, cancelling, ...} = Synchronized.value global_state;
280 val now = Time.now ();
281 fun running_info (_, (birth_time, death_time, desc)) =
282 "Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
283 seconds (Time.- (death_time, now)) ^ " to live:\n" ^ desc;
284 fun cancelling_info (_, (deadth_time, desc)) =
285 "Trying to interrupt thread since " ^ seconds (Time.- (now, deadth_time)) ^ ":\n" ^ desc;
288 if null active then "No ATPs running."
289 else space_implode "\n\n" ("Running ATPs:" :: map running_info active);
291 if null cancelling then ""
294 ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling);
296 in priority (running ^ "\n" ^ interrupting) end;
298 fun messages opt_limit =
300 val limit = the_default message_display_limit opt_limit;
301 val {store, ...} = Synchronized.value global_state;
303 "Recent ATP messages" ^
304 (if length store <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
305 in List.app priority (header :: break_into_chunks (#1 (chop limit store))) end
308 (** The Sledgehammer **)
312 fun err_dup_prover name = error ("Duplicate prover: " ^ quote name ^ ".");
314 structure Data = Theory_Data
316 type T = (prover * stamp) Symtab.table;
317 val empty = Symtab.empty;
319 fun merge data : T = Symtab.merge (eq_snd op =) data
320 handle Symtab.DUP name => err_dup_prover name;
323 fun add_prover (name, prover) thy =
324 Data.map (Symtab.update_new (name, (prover, stamp ()))) thy
325 handle Symtab.DUP name => err_dup_prover name;
327 fun get_prover thy name =
328 case Symtab.lookup (Data.get thy) name of
329 SOME (prover, _) => prover
330 | NONE => error ("Unknown ATP: " ^ name)
332 fun available_atps thy =
333 priority ("Available ATPs: " ^
334 commas (sort_strings (Symtab.keys (Data.get thy))) ^ ".")
337 (* start prover thread *)
339 fun start_prover_thread (params as {timeout, ...}) birth_time death_time i n
340 relevance_override minimize_command proof_state name =
342 val prover = get_prover (Proof.theory_of proof_state) name
343 val {context = ctxt, facts, goal} = Proof.goal proof_state;
345 "ATP " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
346 Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i));
347 val _ = Toplevel.thread true (fn () =>
349 val _ = register params birth_time death_time (Thread.self (), desc)
351 {subgoal = i, goal = (ctxt, (facts, goal)),
352 relevance_override = relevance_override, axiom_clauses = NONE,
353 filtered_clauses = NONE}
355 #message (prover params (minimize_command name) timeout problem)
356 handle Sledgehammer_HOL_Clause.TRIVIAL => metis_line i n []
357 | ERROR message => "Error: " ^ message ^ "\n"
358 val _ = unregister params message (Thread.self ());