author | immler@in.tum.de |
Tue, 20 Jan 2009 22:19:46 +0100 | |
changeset 29595 | 93ff1bca5e15 |
parent 29593 | 7b73bd578db2 |
child 29596 | 7b710756609c |
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 |
Author: Fabian Immler, TU Muenchen |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
3 |
|
28571 | 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. |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
9 |
*) |
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 |
signature ATP_MANAGER = |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
12 |
sig |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
13 |
val get_atps: unit -> string |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
14 |
val set_atps: string -> unit |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
15 |
val get_max_atps: unit -> int |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
16 |
val set_max_atps: int -> unit |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
17 |
val get_timeout: unit -> int |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
18 |
val set_timeout: int -> unit |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
19 |
val kill: unit -> unit |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
20 |
val info: unit -> unit |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
21 |
val messages: int option -> unit |
29593 | 22 |
type prover = int -> int -> Proof.state -> bool * string |
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
23 |
val add_prover: string -> prover -> theory -> theory |
28484 | 24 |
val print_provers: theory -> unit |
28571 | 25 |
val sledgehammer: string list -> Proof.state -> unit |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
26 |
end; |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
27 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
28 |
structure AtpManager: ATP_MANAGER = |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
29 |
struct |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
30 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
31 |
(** preferences **) |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
32 |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
33 |
val message_store_limit = 20; |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
34 |
val message_display_limit = 5; |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
35 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
36 |
local |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
37 |
|
29592 | 38 |
val atps = ref "e remote_e remote_vampire remote_spass"; |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
39 |
val max_atps = ref 5; (* ~1 means infinite number of atps *) |
29590 | 40 |
val timeout = ref 100; |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
41 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
42 |
in |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
43 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
44 |
fun get_atps () = CRITICAL (fn () => ! atps); |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
45 |
fun set_atps str = CRITICAL (fn () => atps := str); |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
46 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
47 |
fun get_max_atps () = CRITICAL (fn () => ! max_atps); |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
48 |
fun set_max_atps number = CRITICAL (fn () => max_atps := number); |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
49 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
50 |
fun get_timeout () = CRITICAL (fn () => ! timeout); |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
51 |
fun set_timeout time = CRITICAL (fn () => timeout := time); |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
52 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
53 |
val _ = |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
54 |
ProofGeneralPgip.add_preference "Proof" |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
55 |
(Preferences.string_pref atps |
28589
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
56 |
"ATP: provers" "Default automatic provers (separated by whitespace)"); |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
57 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
58 |
val _ = ProofGeneralPgip.add_preference "Proof" |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
59 |
(Preferences.int_pref max_atps |
28589
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
60 |
"ATP: maximum number" "How many provers may run in parallel"); |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
61 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
62 |
val _ = ProofGeneralPgip.add_preference "Proof" |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
63 |
(Preferences.int_pref timeout |
28589
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
64 |
"ATP: timeout" "ATPs will be interrupted after this time (in seconds)"); |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
65 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
66 |
end; |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
67 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
68 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
69 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
70 |
(** thread management **) |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
71 |
|
28582 | 72 |
(* data structures over threads *) |
73 |
||
74 |
structure ThreadHeap = HeapFun |
|
75 |
( |
|
76 |
type elem = Time.time * Thread.thread; |
|
77 |
fun ord ((a, _), (b, _)) = Time.compare (a, b); |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
78 |
); |
28582 | 79 |
|
80 |
val lookup_thread = AList.lookup Thread.equal; |
|
81 |
val delete_thread = AList.delete Thread.equal; |
|
82 |
val update_thread = AList.update Thread.equal; |
|
83 |
||
84 |
||
85 |
(* state of thread manager *) |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
86 |
|
28582 | 87 |
datatype T = State of |
88 |
{timeout_heap: ThreadHeap.T, |
|
89 |
oldest_heap: ThreadHeap.T, |
|
90 |
active: (Thread.thread * (Time.time * Time.time * string)) list, |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
91 |
cancelling: (Thread.thread * (Time.time * Time.time * string)) list, |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
92 |
messages: string list}; |
28582 | 93 |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
94 |
fun make_state timeout_heap oldest_heap active cancelling messages = |
28582 | 95 |
State {timeout_heap = timeout_heap, oldest_heap = oldest_heap, |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
96 |
active = active, cancelling = cancelling, messages = messages}; |
28582 | 97 |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
98 |
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
|
99 |
|
28582 | 100 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
101 |
(* the managing thread *) |
28582 | 102 |
|
103 |
(*watches over running threads and interrupts them if required*) |
|
104 |
val managing_thread = ref (NONE: Thread.thread option); |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
105 |
|
28582 | 106 |
|
29150
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
107 |
(* unregister thread *) |
28582 | 108 |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
109 |
fun unregister (success, message) thread = Synchronized.change_result state |
29150
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
110 |
(fn state as State {timeout_heap, oldest_heap, active, cancelling, messages} => |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
111 |
(case lookup_thread active thread of |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
112 |
SOME (birthtime, _, description) => |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
113 |
let |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
114 |
val (group, active') = |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
115 |
if success then List.partition (fn (_, (tb, _, _)) => tb = birthtime) active |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
116 |
else List.partition (fn (th, _) => Thread.equal (th, thread)) active |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
117 |
val others = delete_thread thread group |
28582 | 118 |
|
29150
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
119 |
val now = Time.now () |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
120 |
val cancelling' = |
29595 | 121 |
fold (fn (th, (tb, _, desc)) => update_thread (th, (tb, now, desc))) group cancelling |
28582 | 122 |
|
29150
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
123 |
val msg = description ^ "\n" ^ message |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
124 |
val message' = "Sledgehammer: " ^ msg ^ |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
125 |
(if null others then "" |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
126 |
else "\nInterrupted " ^ string_of_int (length others) ^ " other group members") |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
127 |
val messages' = msg :: |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
128 |
(if length messages <= message_store_limit then messages |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
129 |
else #1 (chop message_store_limit messages)) |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
130 |
in (message', make_state timeout_heap oldest_heap active' cancelling' messages') end |
8af5ee47f30c
unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents:
29116
diff
changeset
|
131 |
| NONE => ("", state))); |
28582 | 132 |
|
133 |
||
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
134 |
(* kill excessive atp threads *) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
135 |
|
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
136 |
fun excessive_atps active = |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
137 |
let val max = get_max_atps () |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
138 |
in length active > max andalso max > ~1 end; |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
139 |
|
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
140 |
local |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
141 |
|
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
142 |
fun kill_oldest () = |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
143 |
let exception Unchanged in |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
144 |
Synchronized.change_result state |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
145 |
(fn State {timeout_heap, oldest_heap, active, cancelling, messages} => |
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
146 |
if ThreadHeap.is_empty oldest_heap orelse not (excessive_atps active) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
147 |
then raise Unchanged |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
148 |
else |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
149 |
let val ((_, oldest_thread), oldest_heap') = ThreadHeap.min_elem oldest_heap |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
150 |
in (oldest_thread, make_state timeout_heap oldest_heap' active cancelling messages) end) |
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
151 |
|> (priority o unregister (false, "Interrupted (maximum number of ATPs exceeded)")) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
152 |
handle Unchanged => () |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
153 |
end; |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
154 |
|
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
155 |
in |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
156 |
|
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
157 |
fun kill_excessive () = |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
158 |
let val State {active, ...} = Synchronized.value state |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
159 |
in if excessive_atps active then (kill_oldest (); kill_excessive ()) else () end; |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
160 |
|
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
161 |
end; |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
162 |
|
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
163 |
|
28582 | 164 |
(* start a watching thread which runs forever -- only one may exist *) |
165 |
||
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
166 |
fun check_thread_manager () = CRITICAL (fn () => |
28582 | 167 |
if (case ! managing_thread of SOME thread => Thread.isActive thread | NONE => false) |
168 |
then () else managing_thread := SOME (SimpleThread.fork false (fn () => |
|
28571 | 169 |
let |
28582 | 170 |
val min_wait_time = Time.fromMilliseconds 300 |
171 |
val max_wait_time = Time.fromSeconds 10 |
|
172 |
||
173 |
(* wait for next thread to cancel, or maximum*) |
|
174 |
fun time_limit (State {timeout_heap, ...}) = |
|
175 |
(case try ThreadHeap.min timeout_heap of |
|
176 |
NONE => SOME (Time.+ (Time.now (), max_wait_time)) |
|
177 |
| SOME (time, _) => SOME time) |
|
178 |
||
179 |
(* action: cancel find threads whose timeout is reached, and interrupt cancelling threads *) |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
180 |
fun action (State {timeout_heap, oldest_heap, active, cancelling, messages}) = |
28582 | 181 |
let val (timeout_threads, timeout_heap') = |
182 |
ThreadHeap.upto (Time.now (), Thread.self ()) timeout_heap |
|
183 |
in |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
184 |
if null timeout_threads andalso null cancelling andalso not (excessive_atps active) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
185 |
then NONE |
28582 | 186 |
else |
187 |
let |
|
188 |
val _ = List.app (SimpleThread.interrupt o #1) cancelling |
|
189 |
val cancelling' = filter (Thread.isActive o #1) cancelling |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
190 |
val state' = make_state timeout_heap' oldest_heap active cancelling' messages |
28582 | 191 |
in SOME (map #2 timeout_threads, state') end |
192 |
end |
|
193 |
in |
|
194 |
while true do |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
195 |
(Synchronized.timed_access state time_limit action |
28582 | 196 |
|> these |
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
197 |
|> List.app (priority o unregister (false, "Interrupted (reached timeout)")); |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
198 |
kill_excessive (); |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
199 |
(*give threads time to respond to interrupt*) |
28571 | 200 |
OS.Process.sleep min_wait_time) |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
201 |
end))); |
28582 | 202 |
|
203 |
||
204 |
(* thread is registered here by sledgehammer *) |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
205 |
|
28582 | 206 |
fun register birthtime deadtime (thread, desc) = |
207 |
(check_thread_manager (); |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
208 |
Synchronized.change state |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
209 |
(fn State {timeout_heap, oldest_heap, active, cancelling, messages} => |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
210 |
let |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
211 |
val timeout_heap' = ThreadHeap.insert (deadtime, thread) timeout_heap |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
212 |
val oldest_heap' = ThreadHeap.insert (birthtime, thread) oldest_heap |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
213 |
val active' = update_thread (thread, (birthtime, deadtime, desc)) active |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
214 |
in make_state timeout_heap' oldest_heap' active' cancelling messages end)); |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
215 |
|
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
216 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
217 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
218 |
(** user commands **) |
28582 | 219 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
220 |
(* kill: move all threads to cancelling *) |
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
221 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
222 |
fun kill () = Synchronized.change state |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
223 |
(fn State {timeout_heap, oldest_heap, active, cancelling, messages} => |
28582 | 224 |
let val formerly_active = map (fn (th, (tb, _, desc)) => (th, (tb, Time.now (), desc))) active |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
225 |
in make_state timeout_heap oldest_heap [] (formerly_active @ cancelling) messages end); |
28582 | 226 |
|
227 |
||
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
228 |
(* ATP info *) |
28582 | 229 |
|
230 |
fun info () = |
|
231 |
let |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
232 |
val State {active, cancelling, ...} = Synchronized.value state |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
233 |
|
28571 | 234 |
fun running_info (_, (birth_time, dead_time, desc)) = "Running: " |
28589
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
235 |
^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), birth_time)) |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
236 |
^ " s -- " |
28589
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
237 |
^ (string_of_int o Time.toSeconds) (Time.- (dead_time, Time.now ())) |
28571 | 238 |
^ " s to live:\n" ^ desc |
239 |
fun cancelling_info (_, (_, dead_time, desc)) = "Trying to interrupt thread since " |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
240 |
^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), dead_time)) |
28571 | 241 |
^ " s:\n" ^ desc |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
242 |
|
28589
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
243 |
val running = |
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
244 |
if null active then "No ATPs running." |
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
245 |
else space_implode "\n\n" ("Running ATPs:" :: map running_info active) |
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
246 |
val interrupting = |
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
247 |
if null cancelling then "" |
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
248 |
else space_implode "\n\n" |
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
249 |
("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling) |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
250 |
|
28582 | 251 |
in writeln (running ^ "\n" ^ interrupting) end; |
252 |
||
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
253 |
fun messages opt_limit = |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
254 |
let |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
255 |
val limit = the_default message_display_limit opt_limit; |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
256 |
val State {messages = msgs, ...} = Synchronized.value state |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
257 |
val header = "Recent ATP messages" ^ |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
258 |
(if length msgs <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):"); |
29116 | 259 |
in writeln (space_implode "\n\n" (header :: #1 (chop limit msgs))) end; |
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
260 |
|
28582 | 261 |
|
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
262 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
263 |
(** The Sledgehammer **) |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
264 |
|
28582 | 265 |
(* named provers *) |
28484 | 266 |
|
29593 | 267 |
type prover = int -> int -> Proof.state -> bool * string; |
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
268 |
|
28582 | 269 |
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
|
270 |
|
28582 | 271 |
structure Provers = TheoryDataFun |
272 |
( |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
273 |
type T = (prover * stamp) Symtab.table |
28582 | 274 |
val empty = Symtab.empty |
275 |
val copy = I |
|
276 |
val extend = I |
|
277 |
fun merge _ tabs : T = Symtab.merge (eq_snd op =) tabs |
|
28589
581b2ab9827a
adding preferences is now permissive, no error handling here;
wenzelm
parents:
28586
diff
changeset
|
278 |
handle Symtab.DUP dup => err_dup_prover dup |
28582 | 279 |
); |
28484 | 280 |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
281 |
fun add_prover name prover thy = |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
282 |
Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy |
28582 | 283 |
handle Symtab.DUP dup => err_dup_prover dup; |
284 |
||
285 |
fun print_provers thy = Pretty.writeln |
|
286 |
(Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy)))); |
|
28571 | 287 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
288 |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
289 |
(* start prover thread *) |
28484 | 290 |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
291 |
fun start_prover name birthtime deadtime i proof_state = |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
292 |
(case Symtab.lookup (Provers.get (Proof.theory_of proof_state)) name of |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
293 |
NONE => warning ("Unknown external prover: " ^ quote name) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
294 |
| SOME (prover, _) => |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
295 |
let |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
296 |
val (ctxt, (_, goal)) = Proof.get_goal proof_state |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
297 |
val desc = |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
298 |
"external prover " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^ |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
299 |
Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
300 |
val _ = SimpleThread.fork true (fn () => |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
301 |
let |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
302 |
val _ = register birthtime deadtime (Thread.self (), desc) |
29593 | 303 |
val result = prover (get_timeout ()) i proof_state |
28835 | 304 |
handle ResHolClause.TOO_TRIVIAL |
305 |
=> (true, "Empty clause: Try this command: " ^ Markup.markup Markup.sendback "apply metis") |
|
306 |
| ERROR msg |
|
307 |
=> (false, "Error: " ^ msg) |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
308 |
val _ = priority (unregister result (Thread.self ())) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
309 |
in () end handle Interrupt => ()) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
310 |
in () end); |
28582 | 311 |
|
312 |
||
313 |
(* sledghammer for first subgoal *) |
|
314 |
||
315 |
fun sledgehammer names proof_state = |
|
316 |
let |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
317 |
val provers = |
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
318 |
if null names then String.tokens (Symbol.is_ascii_blank o String.str) (get_atps ()) |
28582 | 319 |
else names |
320 |
val birthtime = Time.now () |
|
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
321 |
val deadtime = Time.+ (birthtime, Time.fromSeconds (get_timeout ())) |
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
322 |
in List.app (fn name => start_prover name birthtime deadtime 1 proof_state) provers end; |
28582 | 323 |
|
324 |
||
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
325 |
|
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
326 |
(** Isar command syntax **) |
28582 | 327 |
|
328 |
local structure K = OuterKeyword and P = OuterParse in |
|
329 |
||
330 |
val _ = |
|
331 |
OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
332 |
(Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill)); |
28582 | 333 |
|
334 |
val _ = |
|
335 |
OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag |
|
336 |
(Scan.succeed (Toplevel.no_timing o Toplevel.imperative info)); |
|
337 |
||
338 |
val _ = |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
339 |
OuterSyntax.improper_command "atp_messages" "print recent messages issued by managed provers" K.diag |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
340 |
(Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >> |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
341 |
(fn limit => Toplevel.no_timing o Toplevel.imperative (fn () => messages limit))); |
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
342 |
|
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
343 |
val _ = |
28582 | 344 |
OuterSyntax.improper_command "print_atps" "print external provers" K.diag |
345 |
(Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o |
|
346 |
Toplevel.keep (print_provers o Toplevel.theory_of))); |
|
347 |
||
348 |
val _ = |
|
349 |
OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag |
|
350 |
(Scan.repeat P.xname >> (fn names => Toplevel.no_timing o Toplevel.unknown_proof o |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
351 |
Toplevel.keep ((sledgehammer names) o Toplevel.proof_of))); |
28582 | 352 |
|
353 |
end; |
|
354 |
||
355 |
end; |