src/HOL/Tools/ATP_Manager as separate component, with (almost) everything in one place;
1 (* Title: HOL/Tools/ATP_Manager/atp_manager.ML
2 Author: Fabian Immler, TU Muenchen
4 ATP threads are registered here.
5 Threads with the same birth-time are seen as one group.
6 All threads of a group are killed when one thread of it has been successful,
7 or after a certain time,
8 or when the maximum number of threads exceeds; then the oldest thread is killed.
11 signature ATP_MANAGER =
13 val get_atps: unit -> string
14 val set_atps: string -> unit
15 val get_max_atps: unit -> int
16 val set_max_atps: int -> unit
17 val get_timeout: unit -> int
18 val set_timeout: int -> unit
19 val get_full_types: unit -> bool
20 val set_full_types: bool -> unit
21 val kill: unit -> unit
22 val info: unit -> unit
23 val messages: int option -> unit
24 type prover = int -> (thm * (string * int)) list option ->
25 (thm * (string * int)) list option -> string -> int ->
26 Proof.context * (thm list * thm) ->
27 bool * string * string * string vector * (thm * (string * int)) list
28 val add_prover: string -> prover -> theory -> theory
29 val print_provers: theory -> unit
30 val get_prover: string -> theory -> prover option
31 val sledgehammer: string list -> Proof.state -> unit
34 structure AtpManager: ATP_MANAGER =
39 val message_store_limit = 20;
40 val message_display_limit = 5;
44 val atps = ref "e remote_vampire";
45 val max_atps = ref 5; (* ~1 means infinite number of atps *)
47 val full_types = ref false;
51 fun get_atps () = CRITICAL (fn () => ! atps);
52 fun set_atps str = CRITICAL (fn () => atps := str);
54 fun get_max_atps () = CRITICAL (fn () => ! max_atps);
55 fun set_max_atps number = CRITICAL (fn () => max_atps := number);
57 fun get_timeout () = CRITICAL (fn () => ! timeout);
58 fun set_timeout time = CRITICAL (fn () => timeout := time);
60 fun get_full_types () = CRITICAL (fn () => ! full_types);
61 fun set_full_types bool = CRITICAL (fn () => full_types := bool);
64 ProofGeneralPgip.add_preference Preferences.category_proof
65 (Preferences.string_pref atps
66 "ATP: provers" "Default automatic provers (separated by whitespace)");
69 ProofGeneralPgip.add_preference Preferences.category_proof
70 (Preferences.int_pref max_atps
71 "ATP: maximum number" "How many provers may run in parallel");
74 ProofGeneralPgip.add_preference Preferences.category_proof
75 (Preferences.int_pref timeout
76 "ATP: timeout" "ATPs will be interrupted after this time (in seconds)");
79 ProofGeneralPgip.add_preference Preferences.category_proof
80 (Preferences.bool_pref full_types
81 "ATP: full types" "ATPs will use full type information");
87 (** thread management **)
89 (* data structures over threads *)
91 structure ThreadHeap = HeapFun
93 type elem = Time.time * Thread.thread;
94 fun ord ((a, _), (b, _)) = Time.compare (a, b);
97 fun lookup_thread xs = AList.lookup Thread.equal xs;
98 fun delete_thread xs = AList.delete Thread.equal xs;
99 fun update_thread xs = AList.update Thread.equal xs;
102 (* state of thread manager *)
104 datatype T = State of
105 {managing_thread: Thread.thread option,
106 timeout_heap: ThreadHeap.T,
107 oldest_heap: ThreadHeap.T,
108 active: (Thread.thread * (Time.time * Time.time * string)) list,
109 cancelling: (Thread.thread * (Time.time * Time.time * string)) list,
110 messages: string list,
113 fun make_state managing_thread timeout_heap oldest_heap active cancelling messages store =
114 State {managing_thread = managing_thread, timeout_heap = timeout_heap, oldest_heap = oldest_heap,
115 active = active, cancelling = cancelling, messages = messages, store = store};
117 val state = Synchronized.var "atp_manager"
118 (make_state NONE ThreadHeap.empty ThreadHeap.empty [] [] [] []);
121 (* unregister thread *)
123 fun unregister (success, message) thread = Synchronized.change state
124 (fn state as State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
125 (case lookup_thread active thread of
126 SOME (birthtime, _, description) =>
128 val (group, active') =
129 if success then List.partition (fn (_, (tb, _, _)) => tb = birthtime) active
130 else List.partition (fn (th, _) => Thread.equal (th, thread)) active
132 val now = Time.now ()
134 fold (fn (th, (tb, _, desc)) => update_thread (th, (tb, now, desc))) group cancelling
136 val message' = description ^ "\n" ^ message ^
137 (if length group <= 1 then ""
138 else "\nInterrupted " ^ string_of_int (length group - 1) ^ " other group members")
139 val store' = message' ::
140 (if length store <= message_store_limit then store
141 else #1 (chop message_store_limit store))
143 managing_thread timeout_heap oldest_heap active' cancelling' (message' :: messages) store'
148 (* kill excessive atp threads *)
150 fun excessive_atps active =
151 let val max = get_max_atps ()
152 in length active > max andalso max > ~1 end;
157 let exception Unchanged in
158 Synchronized.change_result state
159 (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
160 if ThreadHeap.is_empty oldest_heap orelse not (excessive_atps active)
163 let val ((_, oldest_thread), oldest_heap') = ThreadHeap.min_elem oldest_heap
165 make_state managing_thread timeout_heap oldest_heap' active cancelling messages store) end)
166 |> unregister (false, "Interrupted (maximum number of ATPs exceeded)")
167 handle Unchanged => ()
172 fun kill_excessive () =
173 let val State {active, ...} = Synchronized.value state
174 in if excessive_atps active then (kill_oldest (); kill_excessive ()) else () end;
178 fun print_new_messages () =
179 let val to_print = Synchronized.change_result state
180 (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
181 (messages, make_state managing_thread timeout_heap oldest_heap active cancelling [] store))
183 if null to_print then ()
184 else priority ("Sledgehammer: " ^ space_implode "\n\n" to_print)
188 (* start a watching thread -- only one may exist *)
190 fun check_thread_manager () = Synchronized.change state
191 (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
192 if (case managing_thread of SOME thread => Thread.isActive thread | NONE => false)
193 then make_state managing_thread timeout_heap oldest_heap active cancelling messages store
194 else let val managing_thread = SOME (SimpleThread.fork false (fn () =>
196 val min_wait_time = Time.fromMilliseconds 300
197 val max_wait_time = Time.fromSeconds 10
199 (* wait for next thread to cancel, or maximum*)
200 fun time_limit (State {timeout_heap, ...}) =
201 (case try ThreadHeap.min timeout_heap of
202 NONE => SOME (Time.+ (Time.now (), max_wait_time))
203 | SOME (time, _) => SOME time)
205 (* action: find threads whose timeout is reached, and interrupt cancelling threads *)
206 fun action (State {managing_thread, timeout_heap, oldest_heap, active, cancelling,
208 let val (timeout_threads, timeout_heap') =
209 ThreadHeap.upto (Time.now (), Thread.self ()) timeout_heap
211 if null timeout_threads andalso null cancelling andalso not (excessive_atps active)
215 val _ = List.app (SimpleThread.interrupt o #1) cancelling
216 val cancelling' = filter (Thread.isActive o #1) cancelling
217 val state' = make_state
218 managing_thread timeout_heap' oldest_heap active cancelling' messages store
219 in SOME (map #2 timeout_threads, state') end
222 while Synchronized.change_result state
224 State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
225 if (null active) andalso (null cancelling) andalso (null messages)
226 then (false, make_state NONE timeout_heap oldest_heap active cancelling messages store)
229 (Synchronized.timed_access state time_limit action
231 |> List.app (unregister (false, "Interrupted (reached timeout)"));
233 print_new_messages ();
234 (*give threads time to respond to interrupt*)
235 OS.Process.sleep min_wait_time)
237 in make_state managing_thread timeout_heap oldest_heap active cancelling messages store end);
240 (* thread is registered here by sledgehammer *)
242 fun register birthtime deadtime (thread, desc) =
243 (Synchronized.change state
244 (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
246 val timeout_heap' = ThreadHeap.insert (deadtime, thread) timeout_heap
247 val oldest_heap' = ThreadHeap.insert (birthtime, thread) oldest_heap
248 val active' = update_thread (thread, (birthtime, deadtime, desc)) active
249 in make_state managing_thread timeout_heap' oldest_heap' active' cancelling messages store end);
250 check_thread_manager ());
254 (** user commands **)
256 (* kill: move all threads to cancelling *)
258 fun kill () = Synchronized.change state
259 (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
260 let val formerly_active = map (fn (th, (tb, _, desc)) => (th, (tb, Time.now (), desc))) active
262 managing_thread timeout_heap oldest_heap [] (formerly_active @ cancelling) messages store
270 val State {active, cancelling, ...} = Synchronized.value state
272 fun running_info (_, (birth_time, dead_time, desc)) = "Running: "
273 ^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), birth_time))
275 ^ (string_of_int o Time.toSeconds) (Time.- (dead_time, Time.now ()))
276 ^ " s to live:\n" ^ desc
277 fun cancelling_info (_, (_, dead_time, desc)) = "Trying to interrupt thread since "
278 ^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), dead_time))
282 if null active then "No ATPs running."
283 else space_implode "\n\n" ("Running ATPs:" :: map running_info active)
285 if null cancelling then ""
286 else space_implode "\n\n"
287 ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling)
289 in writeln (running ^ "\n" ^ interrupting) end;
291 fun messages opt_limit =
293 val limit = the_default message_display_limit opt_limit;
294 val State {store = msgs, ...} = Synchronized.value state
295 val header = "Recent ATP messages" ^
296 (if length msgs <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
297 in writeln (space_implode "\n\n" (header :: #1 (chop limit msgs))) end;
301 (** The Sledgehammer **)
305 type prover = int -> (thm * (string * int)) list option ->
306 (thm * (string * int)) list option -> string -> int ->
307 Proof.context * (thm list * thm) ->
308 bool * string * string * string vector * (thm * (string * int)) list
310 fun err_dup_prover name = error ("Duplicate prover: " ^ quote name);
312 structure Provers = TheoryDataFun
314 type T = (prover * stamp) Symtab.table
315 val empty = Symtab.empty
318 fun merge _ tabs : T = Symtab.merge (eq_snd op =) tabs
319 handle Symtab.DUP dup => err_dup_prover dup
322 fun add_prover name prover thy =
323 Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy
324 handle Symtab.DUP dup => err_dup_prover dup;
326 fun print_provers thy = Pretty.writeln
327 (Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy))));
329 fun get_prover name thy = case Symtab.lookup (Provers.get thy) name of
331 | SOME (prover, _) => SOME prover;
333 (* start prover thread *)
335 fun start_prover name birthtime deadtime i proof_state =
336 (case get_prover name (Proof.theory_of proof_state) of
337 NONE => warning ("Unknown external prover: " ^ quote name)
340 val (ctxt, (_, goal)) = Proof.get_goal proof_state
342 "external prover " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
343 Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i))
344 val _ = SimpleThread.fork true (fn () =>
346 val _ = register birthtime deadtime (Thread.self (), desc)
348 let val (success, message, _, _, _) =
349 prover (get_timeout ()) NONE NONE name i (Proof.get_goal proof_state)
350 in (success, message) end
351 handle ResHolClause.TOO_TRIVIAL
352 => (true, "Empty clause: Try this command: " ^ Markup.markup Markup.sendback "apply metis")
354 => (false, "Error: " ^ msg)
355 val _ = unregister result (Thread.self ())
356 in () end handle Interrupt => ())
360 (* sledghammer for first subgoal *)
362 fun sledgehammer names proof_state =
365 if null names then String.tokens (Symbol.is_ascii_blank o String.str) (get_atps ())
367 val birthtime = Time.now ()
368 val deadtime = Time.+ (birthtime, Time.fromSeconds (get_timeout ()))
369 in List.app (fn name => start_prover name birthtime deadtime 1 proof_state) provers end;
373 (** Isar command syntax **)
375 local structure K = OuterKeyword and P = OuterParse in
378 OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
379 (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill));
382 OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag
383 (Scan.succeed (Toplevel.no_timing o Toplevel.imperative info));
386 OuterSyntax.improper_command "atp_messages" "print recent messages issued by managed provers" K.diag
387 (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
388 (fn limit => Toplevel.no_timing o Toplevel.imperative (fn () => messages limit)));
391 OuterSyntax.improper_command "print_atps" "print external provers" K.diag
392 (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
393 Toplevel.keep (print_provers o Toplevel.theory_of)));
396 OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag
397 (Scan.repeat P.xname >> (fn names => Toplevel.no_timing o Toplevel.unknown_proof o
398 Toplevel.keep (sledgehammer names o Toplevel.proof_of)));