src/HOL/Tools/ATP_Manager/atp_manager.ML
author wenzelm
Fri, 02 Oct 2009 23:15:36 +0200
changeset 32863 5e8cef567042
parent 32824 712ad8109fff
child 32865 f8d1e16ec758
permissions -rw-r--r--
eliminated dead code; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32327
0971cc0b6a57 src/HOL/Tools/ATP_Manager as separate component, with (almost) everything in one place;
wenzelm
parents: 31793
diff changeset
     1
(*  Title:      HOL/Tools/ATP_Manager/atp_manager.ML
28477
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
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
     4
ATP threads are registered here.
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
     5
Threads with the same birth-time are seen as one group.
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
     6
All threads of a group are killed when one thread of it has been successful,
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
     7
or after a certain time,
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
     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
31791
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    19
  val get_full_types: unit -> bool
31793
7c10b13d49fe proper ML interface set_full_types;
wenzelm
parents: 31791
diff changeset
    20
  val set_full_types: bool -> unit
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    21
  val kill: unit -> unit
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    22
  val info: unit -> unit
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
    23
  val messages: int option -> unit
31409
d8537ba165b5 split preparing clauses and writing problemfile;
immler@in.tum.de
parents: 31368
diff changeset
    24
  type prover = int -> (thm * (string * int)) list option ->
31752
19a5f1c8a844 use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents: 31409
diff changeset
    25
    (thm * (string * int)) list option -> string -> int ->
31409
d8537ba165b5 split preparing clauses and writing problemfile;
immler@in.tum.de
parents: 31368
diff changeset
    26
    Proof.context * (thm list * thm) ->
32510
1b56f8b1e5cc added runtime information to sledgehammer
boehmes
parents: 32451
diff changeset
    27
    bool * (string * string list) * int * string * string vector * (thm * (string * int)) list
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
    28
  val add_prover: string -> prover -> theory -> theory
28484
4ed9239b09c1 misc simplifcation and tuning;
wenzelm
parents: 28478
diff changeset
    29
  val print_provers: theory -> unit
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents: 30982
diff changeset
    30
  val get_prover: string -> theory -> prover option
28571
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
    31
  val sledgehammer: string list -> Proof.state -> unit
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    32
end;
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    33
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    34
structure AtpManager: ATP_MANAGER =
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    35
struct
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    36
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    37
(** preferences **)
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    38
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
    39
val message_store_limit = 20;
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
    40
val message_display_limit = 5;
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
    41
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    42
local
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    43
32824
712ad8109fff made spass additional default prover
nipkow
parents: 32740
diff changeset
    44
val atps = Unsynchronized.ref "e spass remote_vampire";
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32510
diff changeset
    45
val max_atps = Unsynchronized.ref 5;   (* ~1 means infinite number of atps *)
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32510
diff changeset
    46
val timeout = Unsynchronized.ref 60;
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32510
diff changeset
    47
val full_types = Unsynchronized.ref false;
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    48
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    49
in
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    50
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    51
fun get_atps () = CRITICAL (fn () => ! atps);
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    52
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
    53
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    54
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
    55
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
    56
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    57
fun get_timeout () = CRITICAL (fn () => ! timeout);
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    58
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
    59
31791
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    60
fun get_full_types () = CRITICAL (fn () => ! full_types);
31793
7c10b13d49fe proper ML interface set_full_types;
wenzelm
parents: 31791
diff changeset
    61
fun set_full_types bool = CRITICAL (fn () => full_types := bool);
31791
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    62
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    63
val _ =
30982
7882a1268a48 use predefined preferences categories;
wenzelm
parents: 30830
diff changeset
    64
  ProofGeneralPgip.add_preference Preferences.category_proof
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    65
    (Preferences.string_pref atps
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
    66
      "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
    67
30982
7882a1268a48 use predefined preferences categories;
wenzelm
parents: 30830
diff changeset
    68
val _ =
7882a1268a48 use predefined preferences categories;
wenzelm
parents: 30830
diff changeset
    69
  ProofGeneralPgip.add_preference Preferences.category_proof
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    70
    (Preferences.int_pref max_atps
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
    71
      "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
    72
30982
7882a1268a48 use predefined preferences categories;
wenzelm
parents: 30830
diff changeset
    73
val _ =
7882a1268a48 use predefined preferences categories;
wenzelm
parents: 30830
diff changeset
    74
  ProofGeneralPgip.add_preference Preferences.category_proof
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    75
    (Preferences.int_pref timeout
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
    76
      "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
    77
31791
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    78
val _ =
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    79
  ProofGeneralPgip.add_preference Preferences.category_proof
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    80
    (Preferences.bool_pref full_types
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    81
      "ATP: full types" "ATPs will use full type information");
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    82
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    83
end;
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    84
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    85
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    86
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    87
(** thread management **)
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    88
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    89
(* data structures over threads *)
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    90
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    91
structure ThreadHeap = HeapFun
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    92
(
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    93
  type elem = Time.time * Thread.thread;
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    94
  fun ord ((a, _), (b, _)) = Time.compare (a, b);
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
    95
);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    96
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31037
diff changeset
    97
fun lookup_thread xs = AList.lookup Thread.equal xs;
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31037
diff changeset
    98
fun update_thread xs = AList.update Thread.equal xs;
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    99
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   100
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   101
(* state of thread manager *)
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   102
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   103
datatype T = State of
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   104
 {managing_thread: Thread.thread option,
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   105
  timeout_heap: ThreadHeap.T,
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   106
  oldest_heap: ThreadHeap.T,
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   107
  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
   108
  cancelling: (Thread.thread * (Time.time * Time.time * string)) list,
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   109
  messages: string list,
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   110
  store: string list};
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   111
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   112
fun make_state managing_thread timeout_heap oldest_heap active cancelling messages store =
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   113
  State {managing_thread = managing_thread, timeout_heap = timeout_heap, oldest_heap = oldest_heap,
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   114
    active = active, cancelling = cancelling, messages = messages, store = store};
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   115
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   116
val state = Synchronized.var "atp_manager"
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   117
  (make_state NONE ThreadHeap.empty ThreadHeap.empty [] [] [] []);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   118
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31037
diff changeset
   119
29150
8af5ee47f30c unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents: 29116
diff changeset
   120
(* unregister thread *)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   121
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   122
fun unregister (success, message) thread = Synchronized.change state
32863
5e8cef567042 eliminated dead code;
wenzelm
parents: 32824
diff changeset
   123
  (fn state as
5e8cef567042 eliminated dead code;
wenzelm
parents: 32824
diff changeset
   124
      State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
29150
8af5ee47f30c unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents: 29116
diff changeset
   125
    (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
   126
      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
   127
        let
8af5ee47f30c unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents: 29116
diff changeset
   128
          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
   129
            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
   130
            else List.partition (fn (th, _) => Thread.equal (th, thread)) active
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   131
29150
8af5ee47f30c unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents: 29116
diff changeset
   132
          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
   133
          val cancelling' =
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   134
            fold (fn (th, (tb, _, desc)) => update_thread (th, (tb, now, desc))) group cancelling
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   135
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   136
          val message' = description ^ "\n" ^ message ^
29596
7b710756609c do not interrupt successful thread
immler@in.tum.de
parents: 29595
diff changeset
   137
            (if length group <= 1 then ""
7b710756609c do not interrupt successful thread
immler@in.tum.de
parents: 29595
diff changeset
   138
             else "\nInterrupted " ^ string_of_int (length group - 1) ^ " other group members")
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   139
          val store' = message' ::
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   140
            (if length store <= message_store_limit then store
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   141
             else #1 (chop message_store_limit store))
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   142
        in make_state
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   143
          managing_thread timeout_heap oldest_heap active' cancelling' (message' :: messages) store'
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   144
        end
30800
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   145
    | NONE => state));
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   146
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   147
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   148
(* kill excessive atp threads *)
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   149
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   150
fun excessive_atps active =
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   151
  let val max = get_max_atps ()
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   152
  in length active > max andalso max > ~1 end;
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   153
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   154
local
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   155
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   156
fun kill_oldest () =
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   157
  let exception Unchanged in
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   158
    Synchronized.change_result state
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   159
      (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   160
        if ThreadHeap.is_empty oldest_heap orelse not (excessive_atps active)
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   161
        then raise Unchanged
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   162
        else
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   163
          let val ((_, oldest_thread), oldest_heap') = ThreadHeap.min_elem oldest_heap
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   164
          in (oldest_thread,
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   165
          make_state managing_thread timeout_heap oldest_heap' active cancelling messages store) end)
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   166
      |> unregister (false, "Interrupted (maximum number of ATPs exceeded)")
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   167
    handle Unchanged => ()
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   168
  end;
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   169
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   170
in
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   171
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   172
fun kill_excessive () =
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   173
  let val State {active, ...} = Synchronized.value state
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   174
  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
   175
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   176
end;
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   177
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   178
fun print_new_messages () =
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   179
  let val to_print = Synchronized.change_result state
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   180
    (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   181
      (messages, make_state managing_thread timeout_heap oldest_heap active cancelling [] store))
30800
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   182
  in
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   183
    if null to_print then ()
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   184
    else priority ("Sledgehammer: " ^ space_implode "\n\n" to_print)
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   185
  end;
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   186
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   187
30800
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   188
(* start a watching thread -- only one may exist *)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   189
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   190
fun check_thread_manager () = Synchronized.change state
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   191
  (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   192
    if (case managing_thread of SOME thread => Thread.isActive thread | NONE => false)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   193
    then make_state managing_thread timeout_heap oldest_heap active cancelling messages store
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   194
    else let val managing_thread = SOME (SimpleThread.fork false (fn () =>
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   195
      let
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   196
        val min_wait_time = Time.fromMilliseconds 300
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   197
        val max_wait_time = Time.fromSeconds 10
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   198
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   199
        (* wait for next thread to cancel, or maximum*)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   200
        fun time_limit (State {timeout_heap, ...}) =
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   201
          (case try ThreadHeap.min timeout_heap of
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   202
            NONE => SOME (Time.+ (Time.now (), max_wait_time))
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   203
          | SOME (time, _) => SOME time)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   204
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   205
        (* action: find threads whose timeout is reached, and interrupt cancelling threads *)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   206
        fun action (State {managing_thread, timeout_heap, oldest_heap, active, cancelling,
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   207
                           messages, store}) =
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   208
          let val (timeout_threads, timeout_heap') =
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   209
            ThreadHeap.upto (Time.now (), Thread.self ()) timeout_heap
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   210
          in
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   211
            if null timeout_threads andalso null cancelling andalso not (excessive_atps active)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   212
            then NONE
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   213
            else
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   214
              let
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   215
                val _ = List.app (SimpleThread.interrupt o #1) cancelling
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   216
                val cancelling' = filter (Thread.isActive o #1) cancelling
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   217
                val state' = make_state
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   218
                  managing_thread timeout_heap' oldest_heap active cancelling' messages store
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   219
              in SOME (map #2 timeout_threads, state') end
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   220
          end
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   221
      in
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   222
        while Synchronized.change_result state
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   223
          (fn st as
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   224
            State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   225
            if (null active) andalso (null cancelling) andalso (null messages)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   226
            then (false, make_state NONE timeout_heap oldest_heap active cancelling messages store)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   227
            else (true, st))
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   228
        do
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   229
          (Synchronized.timed_access state time_limit action
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   230
            |> these
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   231
            |> List.app (unregister (false, "Interrupted (reached timeout)"));
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   232
            kill_excessive ();
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   233
            print_new_messages ();
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   234
            (*give threads time to respond to interrupt*)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   235
            OS.Process.sleep min_wait_time)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   236
      end))
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   237
    in make_state managing_thread timeout_heap oldest_heap active cancelling messages store end);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   238
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   239
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   240
(* thread is registered here by sledgehammer *)
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   241
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   242
fun register birthtime deadtime (thread, desc) =
30798
36b41d297d65 terminate watching thread
immler@in.tum.de
parents: 30537
diff changeset
   243
 (Synchronized.change state
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   244
    (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   245
      let
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   246
        val timeout_heap' = ThreadHeap.insert (deadtime, thread) timeout_heap
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   247
        val oldest_heap' = ThreadHeap.insert (birthtime, thread) oldest_heap
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   248
        val active' = update_thread (thread, (birthtime, deadtime, desc)) active
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   249
      in make_state managing_thread timeout_heap' oldest_heap' active' cancelling messages store end);
30798
36b41d297d65 terminate watching thread
immler@in.tum.de
parents: 30537
diff changeset
   250
  check_thread_manager ());
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   251
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   252
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   253
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   254
(** user commands **)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   255
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   256
(* kill: move all threads to cancelling *)
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   257
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   258
fun kill () = Synchronized.change state
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   259
  (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   260
    let val formerly_active = map (fn (th, (tb, _, desc)) => (th, (tb, Time.now (), desc))) active
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   261
    in make_state
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   262
      managing_thread timeout_heap oldest_heap [] (formerly_active @ cancelling) messages store
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   263
    end);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   264
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   265
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   266
(* ATP info *)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   267
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   268
fun info () =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   269
  let
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   270
    val State {active, cancelling, ...} = Synchronized.value state
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   271
28571
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
   272
    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
   273
        ^ (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
   274
        ^ " s  --  "
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   275
        ^ (string_of_int o Time.toSeconds) (Time.- (dead_time, Time.now ()))
28571
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
   276
        ^ " s to live:\n" ^ desc
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
   277
    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
   278
        ^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), dead_time))
28571
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
   279
        ^ " s:\n" ^ desc
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   280
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   281
    val running =
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   282
      if null active then "No ATPs running."
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   283
      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
   284
    val interrupting =
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   285
      if null cancelling then ""
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   286
      else space_implode "\n\n"
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   287
        ("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
   288
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   289
  in writeln (running ^ "\n" ^ interrupting) end;
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   290
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   291
fun messages opt_limit =
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   292
  let
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   293
    val limit = the_default message_display_limit opt_limit;
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   294
    val State {store = msgs, ...} = Synchronized.value state
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   295
    val header = "Recent ATP messages" ^
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   296
      (if length msgs <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
29116
d9e423b9e707 tuned messages;
wenzelm
parents: 29112
diff changeset
   297
  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
   298
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   299
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   300
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   301
(** The Sledgehammer **)
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   302
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   303
(* named provers *)
28484
4ed9239b09c1 misc simplifcation and tuning;
wenzelm
parents: 28478
diff changeset
   304
31409
d8537ba165b5 split preparing clauses and writing problemfile;
immler@in.tum.de
parents: 31368
diff changeset
   305
type prover = int -> (thm * (string * int)) list option ->
31752
19a5f1c8a844 use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents: 31409
diff changeset
   306
  (thm * (string * int)) list option -> string -> int ->
31409
d8537ba165b5 split preparing clauses and writing problemfile;
immler@in.tum.de
parents: 31368
diff changeset
   307
  Proof.context * (thm list * thm) ->
32510
1b56f8b1e5cc added runtime information to sledgehammer
boehmes
parents: 32451
diff changeset
   308
  bool * (string * string list) * int * string * string vector * (thm * (string * int)) list
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   309
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   310
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
   311
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   312
structure Provers = TheoryDataFun
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   313
(
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   314
  type T = (prover * stamp) Symtab.table
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   315
  val empty = Symtab.empty
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   316
  val copy = I
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   317
  val extend = I
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   318
  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
   319
    handle Symtab.DUP dup => err_dup_prover dup
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   320
);
28484
4ed9239b09c1 misc simplifcation and tuning;
wenzelm
parents: 28478
diff changeset
   321
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   322
fun add_prover name prover thy =
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   323
  Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   324
    handle Symtab.DUP dup => err_dup_prover dup;
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   325
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   326
fun print_provers thy = Pretty.writeln
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   327
  (Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy))));
28571
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
   328
32863
5e8cef567042 eliminated dead code;
wenzelm
parents: 32824
diff changeset
   329
fun get_prover name thy =
5e8cef567042 eliminated dead code;
wenzelm
parents: 32824
diff changeset
   330
  (case Symtab.lookup (Provers.get thy) name of
5e8cef567042 eliminated dead code;
wenzelm
parents: 32824
diff changeset
   331
    NONE => NONE
5e8cef567042 eliminated dead code;
wenzelm
parents: 32824
diff changeset
   332
  | SOME (prover, _) => SOME prover);
5e8cef567042 eliminated dead code;
wenzelm
parents: 32824
diff changeset
   333
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   334
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   335
(* start prover thread *)
28484
4ed9239b09c1 misc simplifcation and tuning;
wenzelm
parents: 28478
diff changeset
   336
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   337
fun start_prover name birthtime deadtime i proof_state =
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents: 30982
diff changeset
   338
  (case get_prover name (Proof.theory_of proof_state) of
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   339
    NONE => warning ("Unknown external prover: " ^ quote name)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents: 30982
diff changeset
   340
  | SOME prover =>
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   341
      let
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   342
        val (ctxt, (_, goal)) = Proof.get_goal proof_state
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   343
        val desc =
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   344
          "external prover " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   345
            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
   346
        val _ = SimpleThread.fork true (fn () =>
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   347
          let
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   348
            val _ = register birthtime deadtime (Thread.self (), desc)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents: 30982
diff changeset
   349
            val result =
32510
1b56f8b1e5cc added runtime information to sledgehammer
boehmes
parents: 32451
diff changeset
   350
              let val (success, (message, _), _, _, _, _) =
31409
d8537ba165b5 split preparing clauses and writing problemfile;
immler@in.tum.de
parents: 31368
diff changeset
   351
                prover (get_timeout ()) NONE NONE name i (Proof.get_goal proof_state)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents: 30982
diff changeset
   352
              in (success, message) end
28835
d4d8eba5f781 changes by Fabian Immler:
wenzelm
parents: 28595
diff changeset
   353
              handle ResHolClause.TOO_TRIVIAL
d4d8eba5f781 changes by Fabian Immler:
wenzelm
parents: 28595
diff changeset
   354
                => (true, "Empty clause: Try this command: " ^ Markup.markup Markup.sendback "apply metis")
d4d8eba5f781 changes by Fabian Immler:
wenzelm
parents: 28595
diff changeset
   355
              | ERROR msg
d4d8eba5f781 changes by Fabian Immler:
wenzelm
parents: 28595
diff changeset
   356
                => (false, "Error: " ^ msg)
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   357
            val _ = unregister result (Thread.self ())
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   358
          in () end handle Interrupt => ())
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   359
      in () end);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   360
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   361
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   362
(* sledghammer for first subgoal *)
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   363
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   364
fun sledgehammer names proof_state =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   365
  let
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   366
    val provers =
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   367
      if null names then String.tokens (Symbol.is_ascii_blank o String.str) (get_atps ())
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   368
      else names
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   369
    val birthtime = Time.now ()
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   370
    val deadtime = Time.+ (birthtime, Time.fromSeconds (get_timeout ()))
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   371
  in List.app (fn name => start_prover name birthtime deadtime 1 proof_state) provers end;
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   372
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   373
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   374
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   375
(** Isar command syntax **)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   376
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   377
local structure K = OuterKeyword and P = OuterParse in
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   378
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   379
val _ =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   380
  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
   381
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill));
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   382
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   383
val _ =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   384
  OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   385
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative info));
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   386
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   387
val _ =
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   388
  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
   389
    (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   390
      (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
   391
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   392
val _ =
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   393
  OuterSyntax.improper_command "print_atps" "print external provers" K.diag
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   394
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   395
      Toplevel.keep (print_provers o Toplevel.theory_of)));
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   396
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   397
val _ =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   398
  OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   399
    (Scan.repeat P.xname >> (fn names => Toplevel.no_timing o Toplevel.unknown_proof o
30800
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   400
      Toplevel.keep (sledgehammer names o Toplevel.proof_of)));
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   401
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   402
end;
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   403
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   404
end;
30537
0dd8dfe424cf use goal instead of Proof State
immler@in.tum.de
parents: 29784
diff changeset
   405