src/HOL/Tools/ATP_Manager/atp_manager.ML
author blanchet
Fri, 19 Mar 2010 13:02:18 +0100
changeset 35865 2f8fb5242799
parent 35826 1590abc3d42a
child 35866 513074557e06
permissions -rw-r--r--
more Sledgehammer refactoring
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
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
     3
    Author:     Makarius
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     4
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
     5
Central manager component for ATP threads.
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     6
*)
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     7
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     8
signature ATP_MANAGER =
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     9
sig
32937
34f66c9dd8a2 structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32936
diff changeset
    10
  val atps: string Unsynchronized.ref
34f66c9dd8a2 structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32936
diff changeset
    11
  val get_atps: unit -> string list
34f66c9dd8a2 structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32936
diff changeset
    12
  val timeout: int Unsynchronized.ref
34f66c9dd8a2 structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32936
diff changeset
    13
  val full_types: bool Unsynchronized.ref
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    14
  val kill: unit -> unit
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    15
  val info: unit -> unit
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
    16
  val messages: int option -> unit
32936
9491bec20595 modernized structure names;
wenzelm
parents: 32865
diff changeset
    17
  val add_prover: string * ATP_Wrapper.prover -> theory -> theory
32995
wenzelm
parents: 32954
diff changeset
    18
  val get_prover: theory -> string -> ATP_Wrapper.prover option
28484
4ed9239b09c1 misc simplifcation and tuning;
wenzelm
parents: 28478
diff changeset
    19
  val print_provers: theory -> unit
28571
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
    20
  val sledgehammer: string list -> Proof.state -> unit
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    21
end;
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    22
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
    23
structure ATP_Manager : ATP_MANAGER =
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    24
struct
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    25
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    26
(** preferences **)
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    27
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
    28
val message_store_limit = 20;
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
    29
val message_display_limit = 5;
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
    30
32937
34f66c9dd8a2 structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32936
diff changeset
    31
val atps = Unsynchronized.ref "e spass remote_vampire";
34f66c9dd8a2 structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32936
diff changeset
    32
fun get_atps () = String.tokens (Symbol.is_ascii_blank o String.str) (! atps);
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    33
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32510
diff changeset
    34
val timeout = Unsynchronized.ref 60;
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32510
diff changeset
    35
val full_types = Unsynchronized.ref false;
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    36
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    37
val _ =
30982
7882a1268a48 use predefined preferences categories;
wenzelm
parents: 30830
diff changeset
    38
  ProofGeneralPgip.add_preference Preferences.category_proof
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    39
    (Preferences.string_pref atps
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
    40
      "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
    41
30982
7882a1268a48 use predefined preferences categories;
wenzelm
parents: 30830
diff changeset
    42
val _ =
7882a1268a48 use predefined preferences categories;
wenzelm
parents: 30830
diff changeset
    43
  ProofGeneralPgip.add_preference Preferences.category_proof
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    44
    (Preferences.int_pref timeout
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
    45
      "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
    46
31791
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    47
val _ =
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    48
  ProofGeneralPgip.add_preference Preferences.category_proof
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    49
    (Preferences.bool_pref full_types
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    50
      "ATP: full types" "ATPs will use full type information");
c9a1caf218c8 New ATP option: full types
nipkow
parents: 31752
diff changeset
    51
28586
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
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    54
(** thread management **)
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
    55
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    56
(* data structures over threads *)
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    57
32939
1b5a401c78cb renamed functor HeapFun to Heap;
wenzelm
parents: 32938
diff changeset
    58
structure Thread_Heap = Heap
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    59
(
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    60
  type elem = Time.time * Thread.thread;
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    61
  fun ord ((a, _), (b, _)) = Time.compare (a, b);
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
    62
);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    63
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31037
diff changeset
    64
fun lookup_thread xs = AList.lookup Thread.equal xs;
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    65
fun delete_thread xs = AList.delete Thread.equal xs;
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31037
diff changeset
    66
fun update_thread xs = AList.update Thread.equal xs;
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    67
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    68
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    69
(* state of thread manager *)
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    70
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
    71
type state =
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
    72
 {manager: Thread.thread option,
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
    73
  timeout_heap: Thread_Heap.T,
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    74
  active: (Thread.thread * (Time.time * Time.time * string)) list,
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    75
  cancelling: (Thread.thread * (Time.time * string)) list,
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
    76
  messages: string list,
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
    77
  store: string list};
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    78
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    79
fun make_state manager timeout_heap active cancelling messages store : state =
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    80
  {manager = manager, timeout_heap = timeout_heap, active = active,
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    81
    cancelling = cancelling, messages = messages, store = store};
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    82
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
    83
val global_state = Synchronized.var "atp_manager"
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    84
  (make_state NONE Thread_Heap.empty [] [] [] []);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    85
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31037
diff changeset
    86
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    87
(* unregister ATP thread *)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
    88
33312
6ca8a7984fd9 unregister: eliminated unused status;
wenzelm
parents: 33292
diff changeset
    89
fun unregister message thread = Synchronized.change global_state
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    90
  (fn state as {manager, timeout_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
    91
    (case lookup_thread active thread of
35569
wenzelm
parents: 33604
diff changeset
    92
      SOME (_, _, description) =>
29150
8af5ee47f30c unregister: refrain from cancelling self, which sometimes caused sledghammer to fail just before printing the result;
wenzelm
parents: 29116
diff changeset
    93
        let
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    94
          val active' = delete_thread thread active;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    95
          val cancelling' = (thread, (Time.now (), description)) :: cancelling;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    96
          val message' = description ^ "\n" ^ message;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
    97
          val messages' = message' :: messages;
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
    98
          val store' = message' ::
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
    99
            (if length store <= message_store_limit then store
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   100
             else #1 (chop message_store_limit store));
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   101
        in make_state manager timeout_heap active' cancelling' messages' store' end
30800
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   102
    | NONE => state));
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   103
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   104
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   105
(* main manager thread -- only one may exist *)
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   106
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   107
val min_wait_time = Time.fromMilliseconds 300;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   108
val max_wait_time = Time.fromSeconds 10;
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   109
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   110
fun print_new_messages () =
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   111
  let val msgs = Synchronized.change_result global_state
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   112
    (fn {manager, timeout_heap, active, cancelling, messages, store} =>
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   113
      (messages, make_state manager timeout_heap active cancelling [] store))
30800
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   114
  in
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   115
    if null msgs then ()
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   116
    else priority ("Sledgehammer: " ^ space_implode "\n\n" msgs)
30800
95cbadcd47fc tuned spacing and formatting;
wenzelm
parents: 30798
diff changeset
   117
  end;
29620
dc1257eaa4f2 moved all output to watcher-thread
immler@in.tum.de
parents: 29598
diff changeset
   118
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   119
fun check_thread_manager () = Synchronized.change global_state
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   120
  (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   121
    if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
33604
d4220df6fde2 Toplevel.thread provides Isar-style exception output;
wenzelm
parents: 33522
diff changeset
   122
    else let val manager = SOME (Toplevel.thread false (fn () =>
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   123
      let
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   124
        fun time_limit timeout_heap =
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   125
          (case try Thread_Heap.min timeout_heap of
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   126
            NONE => Time.+ (Time.now (), max_wait_time)
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   127
          | SOME (time, _) => time);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   128
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   129
        (*action: find threads whose timeout is reached, and interrupt cancelling threads*)
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   130
        fun action {manager, timeout_heap, active, cancelling, messages, store} =
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   131
          let val (timeout_threads, timeout_heap') =
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   132
            Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   133
          in
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   134
            if null timeout_threads andalso null cancelling
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   135
            then NONE
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   136
            else
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   137
              let
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   138
                val _ = List.app (SimpleThread.interrupt o #1) cancelling;
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   139
                val cancelling' = filter (Thread.isActive o #1) cancelling;
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   140
                val state' = make_state manager timeout_heap' active cancelling' messages store;
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   141
              in SOME (map #2 timeout_threads, state') end
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   142
          end;
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   143
      in
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   144
        while Synchronized.change_result global_state
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   145
          (fn state as {timeout_heap, active, cancelling, messages, store, ...} =>
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   146
            if null active andalso null cancelling andalso null messages
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   147
            then (false, make_state NONE timeout_heap active cancelling messages store)
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   148
            else (true, state))
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   149
        do
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   150
          (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   151
            |> these
33312
6ca8a7984fd9 unregister: eliminated unused status;
wenzelm
parents: 33292
diff changeset
   152
            |> List.app (unregister "Interrupted (reached timeout)");
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   153
            print_new_messages ();
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   154
            (*give threads some time to respond to interrupt*)
30830
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   155
            OS.Process.sleep min_wait_time)
263064c4d0c3 included managing_thread in state of AtpManager:
immler@in.tum.de
parents: 30800
diff changeset
   156
      end))
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   157
    in make_state manager timeout_heap active cancelling messages store end);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   158
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   159
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   160
(* register ATP thread *)
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   161
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   162
fun register birth_time death_time (thread, desc) =
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   163
 (Synchronized.change global_state
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   164
    (fn {manager, timeout_heap, active, cancelling, messages, store} =>
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   165
      let
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   166
        val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   167
        val active' = update_thread (thread, (birth_time, death_time, desc)) active;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   168
        val state' = make_state manager timeout_heap' active' cancelling messages store;
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   169
      in state' end);
30798
36b41d297d65 terminate watching thread
immler@in.tum.de
parents: 30537
diff changeset
   170
  check_thread_manager ());
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   171
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   172
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   173
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   174
(** user commands **)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   175
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   176
(* kill *)
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   177
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   178
fun kill () = Synchronized.change global_state
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   179
  (fn {manager, timeout_heap, active, cancelling, messages, store} =>
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   180
    let
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   181
      val killing = map (fn (th, (_, _, desc)) => (th, (Time.now (), desc))) active;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   182
      val state' = make_state manager timeout_heap [] (killing @ cancelling) messages store;
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   183
    in state' end);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   184
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   185
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   186
(* info *)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   187
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   188
fun seconds time = string_of_int (Time.toSeconds time) ^ "s";
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   189
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   190
fun info () =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   191
  let
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   192
    val {active, cancelling, ...} = Synchronized.value global_state;
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   193
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   194
    val now = Time.now ();
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   195
    fun running_info (_, (birth_time, death_time, desc)) =
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   196
      "Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   197
        seconds (Time.- (death_time, now)) ^ " to live:\n" ^ desc;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   198
    fun cancelling_info (_, (deadth_time, desc)) =
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   199
      "Trying to interrupt thread since " ^ seconds (Time.- (now, deadth_time)) ^ ":\n" ^ desc;
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   200
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   201
    val running =
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   202
      if null active then "No ATPs running."
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   203
      else space_implode "\n\n" ("Running ATPs:" :: map running_info active);
28589
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   204
    val interrupting =
581b2ab9827a adding preferences is now permissive, no error handling here;
wenzelm
parents: 28586
diff changeset
   205
      if null cancelling then ""
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   206
      else
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   207
        space_implode "\n\n"
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   208
          ("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
   209
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   210
  in writeln (running ^ "\n" ^ interrupting) end;
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   211
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   212
fun messages opt_limit =
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   213
  let
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   214
    val limit = the_default message_display_limit opt_limit;
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   215
    val {store, ...} = Synchronized.value global_state;
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   216
    val header =
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   217
      "Recent ATP messages" ^
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   218
        (if length store <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   219
  in writeln (space_implode "\n\n" (header :: #1 (chop limit store))) end;
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   220
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   221
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   222
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   223
(** The Sledgehammer **)
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
   224
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   225
(* named provers *)
28484
4ed9239b09c1 misc simplifcation and tuning;
wenzelm
parents: 28478
diff changeset
   226
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   227
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
   228
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33316
diff changeset
   229
structure Provers = Theory_Data
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   230
(
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   231
  type T = (ATP_Wrapper.prover * stamp) Symtab.table;
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   232
  val empty = Symtab.empty;
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   233
  val extend = I;
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33316
diff changeset
   234
  fun merge data : T = Symtab.merge (eq_snd op =) data
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   235
    handle Symtab.DUP dup => err_dup_prover dup;
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   236
);
28484
4ed9239b09c1 misc simplifcation and tuning;
wenzelm
parents: 28478
diff changeset
   237
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32824
diff changeset
   238
fun add_prover (name, prover) thy =
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   239
  Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   240
    handle Symtab.DUP dup => err_dup_prover dup;
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   241
32995
wenzelm
parents: 32954
diff changeset
   242
fun get_prover thy name =
wenzelm
parents: 32954
diff changeset
   243
  Option.map #1 (Symtab.lookup (Provers.get thy) name);
wenzelm
parents: 32954
diff changeset
   244
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   245
fun print_provers thy = Pretty.writeln
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   246
  (Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy))));
28571
47d88239658d ** Update from Fabian **
wenzelm
parents: 28543
diff changeset
   247
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   248
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   249
(* start prover thread *)
28484
4ed9239b09c1 misc simplifcation and tuning;
wenzelm
parents: 28478
diff changeset
   250
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   251
fun start_prover name birth_time death_time i proof_state =
32947
3c19b98a35cd ATP_Manager.get_prover: canonical argument order;
wenzelm
parents: 32941
diff changeset
   252
  (case get_prover (Proof.theory_of proof_state) name of
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   253
    NONE => warning ("Unknown external prover: " ^ quote name)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents: 30982
diff changeset
   254
  | SOME prover =>
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   255
      let
35592
768d17f54125 use regular Proof.goal (which is what methods usually see) and prevent sledgehammer from crashing;
wenzelm
parents: 35569
diff changeset
   256
        val {context = ctxt, facts, goal} = Proof.goal proof_state;
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   257
        val desc =
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   258
          "external prover " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   259
            Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i));
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   260
33604
d4220df6fde2 Toplevel.thread provides Isar-style exception output;
wenzelm
parents: 33522
diff changeset
   261
        val _ = Toplevel.thread true (fn () =>
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   262
          let
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   263
            val _ = register birth_time death_time (Thread.self (), desc);
33292
affe60b3d864 renamed raw Proof.get_goal to Proof.raw_goal;
wenzelm
parents: 33220
diff changeset
   264
            val problem = ATP_Wrapper.problem_of_goal (! full_types) i (ctxt, (facts, goal));
33312
6ca8a7984fd9 unregister: eliminated unused status;
wenzelm
parents: 33292
diff changeset
   265
            val message = #message (prover (! timeout) problem)
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   266
              handle Sledgehammer_HOL_Clause.TRIVIAL =>   (* FIXME !? *)
33312
6ca8a7984fd9 unregister: eliminated unused status;
wenzelm
parents: 33292
diff changeset
   267
                  "Empty clause: Try this command: " ^ Markup.markup Markup.sendback "apply metis"
6ca8a7984fd9 unregister: eliminated unused status;
wenzelm
parents: 33292
diff changeset
   268
                | ERROR msg => ("Error: " ^ msg);
6ca8a7984fd9 unregister: eliminated unused status;
wenzelm
parents: 33292
diff changeset
   269
            val _ = unregister message (Thread.self ());
35569
wenzelm
parents: 33604
diff changeset
   270
          in () end);
28595
67e3945b53f1 add_prover: plain prover function, without thread;
wenzelm
parents: 28589
diff changeset
   271
      in () end);
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   272
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   273
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   274
(* sledghammer for first subgoal *)
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   275
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   276
fun sledgehammer names proof_state =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   277
  let
32938
63a364ed3f8d misc tuning and clarification;
wenzelm
parents: 32937
diff changeset
   278
    val provers = if null names then get_atps () else names;
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   279
    val birth_time = Time.now ();
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   280
    val death_time = Time.+ (birth_time, Time.fromSeconds (! timeout));
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   281
    val _ = kill ();   (*RACE wrt. other invocations of sledgehammer*)
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   282
    val _ = List.app (fn name => start_prover name birth_time death_time 1 proof_state) provers;
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
   283
  in () end;
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   284
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   285
28586
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   286
d238b83ba3fc renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents: 28582
diff changeset
   287
(** Isar command syntax **)
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   288
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   289
local structure K = OuterKeyword and P = OuterParse in
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   290
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   291
val _ =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   292
  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
   293
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill));
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   294
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   295
val _ =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   296
  OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   297
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative info));
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   298
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   299
val _ =
29112
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   300
  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
   301
    (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   302
      (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
   303
f2b45eea6dac added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents: 28835
diff changeset
   304
val _ =
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   305
  OuterSyntax.improper_command "print_atps" "print external provers" K.diag
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   306
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   307
      Toplevel.keep (print_provers o Toplevel.theory_of)));
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   308
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   309
val _ =
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   310
  OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   311
    (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
   312
      Toplevel.keep (sledgehammer names o Toplevel.proof_of)));
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   313
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   314
end;
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   315
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   316
end;
30537
0dd8dfe424cf use goal instead of Proof State
immler@in.tum.de
parents: 29784
diff changeset
   317