src/HOL/Tools/ATP_Manager/atp_manager.ML
author wenzelm
Thu Oct 15 10:59:10 2009 +0200 (2009-10-15 ago)
changeset 32938 63a364ed3f8d
parent 32937 34f66c9dd8a2
child 32939 1b5a401c78cb
permissions -rw-r--r--
misc tuning and clarification;
wenzelm@32327
     1
(*  Title:      HOL/Tools/ATP_Manager/atp_manager.ML
wenzelm@28477
     2
    Author:     Fabian Immler, TU Muenchen
wenzelm@28477
     3
wenzelm@28571
     4
ATP threads are registered here.
wenzelm@28571
     5
Threads with the same birth-time are seen as one group.
wenzelm@28571
     6
All threads of a group are killed when one thread of it has been successful,
wenzelm@28571
     7
or after a certain time,
wenzelm@28571
     8
or when the maximum number of threads exceeds; then the oldest thread is killed.
wenzelm@28477
     9
*)
wenzelm@28477
    10
wenzelm@28477
    11
signature ATP_MANAGER =
wenzelm@28477
    12
sig
wenzelm@32937
    13
  val atps: string Unsynchronized.ref
wenzelm@32937
    14
  val get_atps: unit -> string list
wenzelm@32937
    15
  val max_atps: int Unsynchronized.ref
wenzelm@32937
    16
  val timeout: int Unsynchronized.ref
wenzelm@32937
    17
  val full_types: bool Unsynchronized.ref
wenzelm@28586
    18
  val kill: unit -> unit
wenzelm@28586
    19
  val info: unit -> unit
wenzelm@29112
    20
  val messages: int option -> unit
wenzelm@32936
    21
  val add_prover: string * ATP_Wrapper.prover -> theory -> theory
wenzelm@28484
    22
  val print_provers: theory -> unit
wenzelm@32936
    23
  val get_prover: string -> theory -> ATP_Wrapper.prover option
wenzelm@28571
    24
  val sledgehammer: string list -> Proof.state -> unit
wenzelm@28477
    25
end;
wenzelm@28477
    26
wenzelm@32936
    27
structure ATP_Manager: ATP_MANAGER =
wenzelm@28477
    28
struct
wenzelm@28477
    29
wenzelm@28586
    30
(** preferences **)
wenzelm@28586
    31
wenzelm@29112
    32
val message_store_limit = 20;
wenzelm@29112
    33
val message_display_limit = 5;
wenzelm@29112
    34
wenzelm@32937
    35
val atps = Unsynchronized.ref "e spass remote_vampire";
wenzelm@32937
    36
fun get_atps () = String.tokens (Symbol.is_ascii_blank o String.str) (! atps);
wenzelm@28586
    37
wenzelm@32938
    38
val max_atps = Unsynchronized.ref 5;
wenzelm@32938
    39
fun excessive_atps active =
wenzelm@32938
    40
  let val max = ! max_atps
wenzelm@32938
    41
  in max >= 0 andalso length active > max end;
wenzelm@32938
    42
wenzelm@32740
    43
val timeout = Unsynchronized.ref 60;
wenzelm@32740
    44
val full_types = Unsynchronized.ref false;
wenzelm@28586
    45
wenzelm@28586
    46
val _ =
wenzelm@30982
    47
  ProofGeneralPgip.add_preference Preferences.category_proof
wenzelm@28586
    48
    (Preferences.string_pref atps
wenzelm@28589
    49
      "ATP: provers" "Default automatic provers (separated by whitespace)");
wenzelm@28586
    50
wenzelm@30982
    51
val _ =
wenzelm@30982
    52
  ProofGeneralPgip.add_preference Preferences.category_proof
wenzelm@28586
    53
    (Preferences.int_pref max_atps
wenzelm@28589
    54
      "ATP: maximum number" "How many provers may run in parallel");
wenzelm@28586
    55
wenzelm@30982
    56
val _ =
wenzelm@30982
    57
  ProofGeneralPgip.add_preference Preferences.category_proof
wenzelm@28586
    58
    (Preferences.int_pref timeout
wenzelm@28589
    59
      "ATP: timeout" "ATPs will be interrupted after this time (in seconds)");
wenzelm@28586
    60
nipkow@31791
    61
val _ =
nipkow@31791
    62
  ProofGeneralPgip.add_preference Preferences.category_proof
nipkow@31791
    63
    (Preferences.bool_pref full_types
nipkow@31791
    64
      "ATP: full types" "ATPs will use full type information");
nipkow@31791
    65
wenzelm@28586
    66
wenzelm@28586
    67
wenzelm@28586
    68
(** thread management **)
wenzelm@28586
    69
wenzelm@28582
    70
(* data structures over threads *)
wenzelm@28582
    71
wenzelm@32938
    72
structure Thread_Heap = HeapFun
wenzelm@28582
    73
(
wenzelm@28582
    74
  type elem = Time.time * Thread.thread;
wenzelm@28582
    75
  fun ord ((a, _), (b, _)) = Time.compare (a, b);
wenzelm@28595
    76
);
wenzelm@28582
    77
wenzelm@31368
    78
fun lookup_thread xs = AList.lookup Thread.equal xs;
wenzelm@31368
    79
fun update_thread xs = AList.update Thread.equal xs;
wenzelm@28582
    80
wenzelm@28582
    81
wenzelm@28582
    82
(* state of thread manager *)
wenzelm@28477
    83
wenzelm@32938
    84
type state =
wenzelm@32938
    85
 {manager: Thread.thread option,
wenzelm@32938
    86
  timeout_heap: Thread_Heap.T,
wenzelm@32938
    87
  oldest_heap: Thread_Heap.T,
wenzelm@28582
    88
  active: (Thread.thread * (Time.time * Time.time * string)) list,
wenzelm@29112
    89
  cancelling: (Thread.thread * (Time.time * Time.time * string)) list,
immler@29620
    90
  messages: string list,
immler@29620
    91
  store: string list};
wenzelm@28582
    92
wenzelm@32938
    93
fun make_state manager timeout_heap oldest_heap active cancelling messages store =
wenzelm@32938
    94
  {manager = manager, timeout_heap = timeout_heap, oldest_heap = oldest_heap,
immler@29620
    95
    active = active, cancelling = cancelling, messages = messages, store = store};
wenzelm@28582
    96
wenzelm@32938
    97
val global_state = Synchronized.var "atp_manager"
wenzelm@32938
    98
  (make_state NONE Thread_Heap.empty Thread_Heap.empty [] [] [] []);
wenzelm@28582
    99
wenzelm@31368
   100
wenzelm@29150
   101
(* unregister thread *)
wenzelm@28582
   102
wenzelm@32938
   103
fun unregister (success, message) thread = Synchronized.change global_state
wenzelm@32938
   104
  (fn state as {manager, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
wenzelm@29150
   105
    (case lookup_thread active thread of
wenzelm@29150
   106
      SOME (birthtime, _, description) =>
wenzelm@29150
   107
        let
wenzelm@29150
   108
          val (group, active') =
wenzelm@29150
   109
            if success then List.partition (fn (_, (tb, _, _)) => tb = birthtime) active
wenzelm@32938
   110
            else List.partition (fn (th, _) => Thread.equal (th, thread)) active;
wenzelm@28582
   111
wenzelm@32938
   112
          val now = Time.now ();
wenzelm@29150
   113
          val cancelling' =
wenzelm@32938
   114
            fold (fn (th, (tb, _, desc)) => update_thread (th, (tb, now, desc))) group cancelling;
wenzelm@28582
   115
immler@29620
   116
          val message' = description ^ "\n" ^ message ^
immler@29596
   117
            (if length group <= 1 then ""
wenzelm@32938
   118
             else "\nInterrupted " ^ string_of_int (length group - 1) ^ " other group members");
immler@29620
   119
          val store' = message' ::
immler@29620
   120
            (if length store <= message_store_limit then store
wenzelm@32938
   121
             else #1 (chop message_store_limit store));
wenzelm@32938
   122
        in
wenzelm@32938
   123
          make_state manager timeout_heap oldest_heap
wenzelm@32938
   124
            active' cancelling' (message' :: messages) store'
immler@30830
   125
        end
wenzelm@30800
   126
    | NONE => state));
wenzelm@28582
   127
wenzelm@28582
   128
wenzelm@28595
   129
(* kill excessive atp threads *)
wenzelm@28595
   130
wenzelm@32938
   131
local
wenzelm@28595
   132
wenzelm@32938
   133
exception UNCHANGED of unit;
wenzelm@28595
   134
wenzelm@28595
   135
fun kill_oldest () =
wenzelm@32938
   136
  Synchronized.change_result global_state
wenzelm@32938
   137
    (fn {manager, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
wenzelm@32938
   138
      if Thread_Heap.is_empty oldest_heap orelse not (excessive_atps active)
wenzelm@32938
   139
      then raise UNCHANGED ()
wenzelm@32938
   140
      else
wenzelm@32938
   141
        let
wenzelm@32938
   142
          val ((_, oldest_thread), oldest_heap') = Thread_Heap.min_elem oldest_heap;
wenzelm@32938
   143
          val state' =
wenzelm@32938
   144
            make_state manager timeout_heap oldest_heap' active cancelling messages store;
wenzelm@32938
   145
        in (oldest_thread, state') end)
wenzelm@32938
   146
    |> unregister (false, "Interrupted (maximum number of ATPs exceeded)")
wenzelm@32938
   147
  handle UNCHANGED () => ();
wenzelm@28595
   148
wenzelm@28595
   149
in
wenzelm@28595
   150
wenzelm@28595
   151
fun kill_excessive () =
wenzelm@32938
   152
  let val {active, ...} = Synchronized.value global_state
wenzelm@28595
   153
  in if excessive_atps active then (kill_oldest (); kill_excessive ()) else () end;
wenzelm@28595
   154
wenzelm@28595
   155
end;
wenzelm@28595
   156
immler@29620
   157
fun print_new_messages () =
wenzelm@32938
   158
  let val msgs = Synchronized.change_result global_state
wenzelm@32938
   159
    (fn {manager, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
wenzelm@32938
   160
      (messages, make_state manager timeout_heap oldest_heap active cancelling [] store))
wenzelm@30800
   161
  in
wenzelm@32938
   162
    if null msgs then ()
wenzelm@32938
   163
    else priority ("Sledgehammer: " ^ space_implode "\n\n" msgs)
wenzelm@30800
   164
  end;
immler@29620
   165
wenzelm@28595
   166
wenzelm@32938
   167
(* start manager thread -- only one may exist *)
wenzelm@28582
   168
wenzelm@32938
   169
val min_wait_time = Time.fromMilliseconds 300;
wenzelm@32938
   170
val max_wait_time = Time.fromSeconds 10;
wenzelm@28582
   171
wenzelm@32938
   172
fun check_thread_manager () = Synchronized.change global_state
wenzelm@32938
   173
  (fn {manager, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
wenzelm@32938
   174
    if (case manager of SOME thread => Thread.isActive thread | NONE => false)
wenzelm@32938
   175
    then make_state manager timeout_heap oldest_heap active cancelling messages store
wenzelm@32938
   176
    else let val manager = SOME (SimpleThread.fork false (fn () =>
wenzelm@32938
   177
      let
wenzelm@32938
   178
        fun time_limit timeout_heap =
wenzelm@32938
   179
          (case try Thread_Heap.min timeout_heap of
wenzelm@32938
   180
            NONE => Time.+ (Time.now (), max_wait_time)
wenzelm@32938
   181
          | SOME (time, _) => time);
wenzelm@28582
   182
wenzelm@32938
   183
        (*action: find threads whose timeout is reached, and interrupt cancelling threads*)
wenzelm@32938
   184
        fun action {manager, timeout_heap, oldest_heap, active, cancelling, messages, store} =
immler@30830
   185
          let val (timeout_threads, timeout_heap') =
wenzelm@32938
   186
            Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
immler@30830
   187
          in
immler@30830
   188
            if null timeout_threads andalso null cancelling andalso not (excessive_atps active)
immler@30830
   189
            then NONE
immler@30830
   190
            else
immler@30830
   191
              let
wenzelm@32938
   192
                val _ = List.app (SimpleThread.interrupt o #1) cancelling;
wenzelm@32938
   193
                val cancelling' = filter (Thread.isActive o #1) cancelling;
wenzelm@32938
   194
                val state' =
wenzelm@32938
   195
                  make_state manager timeout_heap' oldest_heap active cancelling' messages store;
immler@30830
   196
              in SOME (map #2 timeout_threads, state') end
wenzelm@32938
   197
          end;
immler@30830
   198
      in
wenzelm@32938
   199
        while Synchronized.change_result global_state
wenzelm@32938
   200
          (fn state as {timeout_heap, oldest_heap, active, cancelling, messages, store, ...} =>
wenzelm@32938
   201
            if null active andalso null cancelling andalso null messages
immler@30830
   202
            then (false, make_state NONE timeout_heap oldest_heap active cancelling messages store)
wenzelm@32938
   203
            else (true, state))
immler@30830
   204
        do
wenzelm@32938
   205
          (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
immler@30830
   206
            |> these
immler@30830
   207
            |> List.app (unregister (false, "Interrupted (reached timeout)"));
immler@30830
   208
            kill_excessive ();
immler@30830
   209
            print_new_messages ();
wenzelm@32938
   210
            (*give threads some time to respond to interrupt*)
immler@30830
   211
            OS.Process.sleep min_wait_time)
immler@30830
   212
      end))
wenzelm@32938
   213
    in make_state manager timeout_heap oldest_heap active cancelling messages store end);
wenzelm@28582
   214
wenzelm@28582
   215
wenzelm@28582
   216
(* thread is registered here by sledgehammer *)
wenzelm@28477
   217
wenzelm@28582
   218
fun register birthtime deadtime (thread, desc) =
wenzelm@32938
   219
 (Synchronized.change global_state
wenzelm@32938
   220
    (fn {manager, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
wenzelm@29112
   221
      let
wenzelm@32938
   222
        val timeout_heap' = Thread_Heap.insert (deadtime, thread) timeout_heap;
wenzelm@32938
   223
        val oldest_heap' = Thread_Heap.insert (birthtime, thread) oldest_heap;
wenzelm@32938
   224
        val active' = update_thread (thread, (birthtime, deadtime, desc)) active;
wenzelm@32938
   225
        val state' =
wenzelm@32938
   226
          make_state manager timeout_heap' oldest_heap' active' cancelling messages store;
wenzelm@32938
   227
      in state' end);
immler@30798
   228
  check_thread_manager ());
wenzelm@28477
   229
wenzelm@28477
   230
wenzelm@28586
   231
wenzelm@28586
   232
(** user commands **)
wenzelm@28582
   233
wenzelm@28586
   234
(* kill: move all threads to cancelling *)
wenzelm@28586
   235
wenzelm@32938
   236
fun kill () = Synchronized.change global_state
wenzelm@32938
   237
  (fn {manager, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
wenzelm@32938
   238
    let
wenzelm@32938
   239
      val formerly_active = map (fn (th, (tb, _, desc)) => (th, (tb, Time.now (), desc))) active;
wenzelm@32938
   240
      val state' =
wenzelm@32938
   241
        make_state manager timeout_heap oldest_heap [] (formerly_active @ cancelling) messages store;
wenzelm@32938
   242
    in state' end);
wenzelm@28582
   243
wenzelm@28582
   244
wenzelm@29112
   245
(* ATP info *)
wenzelm@28582
   246
wenzelm@32938
   247
fun seconds time = string_of_int (Time.toSeconds time) ^ "s";
wenzelm@32938
   248
wenzelm@28582
   249
fun info () =
wenzelm@28582
   250
  let
wenzelm@32938
   251
    val {active, cancelling, ...} = Synchronized.value global_state;
wenzelm@29112
   252
wenzelm@32938
   253
    val now = Time.now ();
wenzelm@32938
   254
    fun running_info (_, (birth_time, dead_time, desc)) =
wenzelm@32938
   255
      "Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
wenzelm@32938
   256
        seconds (Time.- (dead_time, now)) ^ " to live:\n" ^ desc;
wenzelm@32938
   257
    fun cancelling_info (_, (_, dead_time, desc)) =
wenzelm@32938
   258
      "Trying to interrupt thread since " ^ seconds (Time.- (now, dead_time)) ^ ":\n" ^ desc;
wenzelm@29112
   259
wenzelm@28589
   260
    val running =
wenzelm@28589
   261
      if null active then "No ATPs running."
wenzelm@32938
   262
      else space_implode "\n\n" ("Running ATPs:" :: map running_info active);
wenzelm@28589
   263
    val interrupting =
wenzelm@28589
   264
      if null cancelling then ""
wenzelm@32938
   265
      else
wenzelm@32938
   266
        space_implode "\n\n"
wenzelm@32938
   267
          ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling);
wenzelm@29112
   268
wenzelm@28582
   269
  in writeln (running ^ "\n" ^ interrupting) end;
wenzelm@28582
   270
wenzelm@29112
   271
fun messages opt_limit =
wenzelm@29112
   272
  let
wenzelm@29112
   273
    val limit = the_default message_display_limit opt_limit;
wenzelm@32938
   274
    val {store, ...} = Synchronized.value global_state;
wenzelm@32938
   275
    val header =
wenzelm@32938
   276
      "Recent ATP messages" ^
wenzelm@32938
   277
        (if length store <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
wenzelm@32938
   278
  in writeln (space_implode "\n\n" (header :: #1 (chop limit store))) end;
wenzelm@29112
   279
wenzelm@28582
   280
wenzelm@28477
   281
wenzelm@28586
   282
(** The Sledgehammer **)
wenzelm@28477
   283
wenzelm@28582
   284
(* named provers *)
wenzelm@28484
   285
wenzelm@28582
   286
fun err_dup_prover name = error ("Duplicate prover: " ^ quote name);
wenzelm@28477
   287
wenzelm@28582
   288
structure Provers = TheoryDataFun
wenzelm@28582
   289
(
wenzelm@32938
   290
  type T = (ATP_Wrapper.prover * stamp) Symtab.table;
wenzelm@32938
   291
  val empty = Symtab.empty;
wenzelm@32938
   292
  val copy = I;
wenzelm@32938
   293
  val extend = I;
wenzelm@28582
   294
  fun merge _ tabs : T = Symtab.merge (eq_snd op =) tabs
wenzelm@32938
   295
    handle Symtab.DUP dup => err_dup_prover dup;
wenzelm@28582
   296
);
wenzelm@28484
   297
boehmes@32864
   298
fun add_prover (name, prover) thy =
wenzelm@28595
   299
  Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy
wenzelm@28582
   300
    handle Symtab.DUP dup => err_dup_prover dup;
wenzelm@28582
   301
wenzelm@28582
   302
fun print_provers thy = Pretty.writeln
wenzelm@28582
   303
  (Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy))));
wenzelm@28571
   304
wenzelm@32863
   305
fun get_prover name thy =
wenzelm@32863
   306
  (case Symtab.lookup (Provers.get thy) name of
wenzelm@32863
   307
    NONE => NONE
wenzelm@32863
   308
  | SOME (prover, _) => SOME prover);
wenzelm@32863
   309
wenzelm@28586
   310
wenzelm@28595
   311
(* start prover thread *)
wenzelm@28484
   312
wenzelm@28595
   313
fun start_prover name birthtime deadtime i proof_state =
immler@31037
   314
  (case get_prover name (Proof.theory_of proof_state) of
wenzelm@28595
   315
    NONE => warning ("Unknown external prover: " ^ quote name)
immler@31037
   316
  | SOME prover =>
wenzelm@28595
   317
      let
wenzelm@32938
   318
        val (ctxt, (_, goal)) = Proof.get_goal proof_state;
wenzelm@28595
   319
        val desc =
wenzelm@28595
   320
          "external prover " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
wenzelm@32938
   321
            Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i));
wenzelm@28595
   322
        val _ = SimpleThread.fork true (fn () =>
wenzelm@28595
   323
          let
wenzelm@32938
   324
            val _ = register birthtime deadtime (Thread.self (), desc);
wenzelm@32937
   325
            val problem =
wenzelm@32938
   326
              ATP_Wrapper.atp_problem_of_goal (! full_types) i (Proof.get_goal proof_state);
immler@31037
   327
            val result =
wenzelm@32938
   328
              let val ATP_Wrapper.Prover_Result {success, message, ...} = prover problem (! timeout);
immler@31037
   329
              in (success, message) end
wenzelm@32937
   330
              handle ResHolClause.TOO_TRIVIAL =>   (* FIXME !? *)
wenzelm@32937
   331
                  (true, "Empty clause: Try this command: " ^
wenzelm@32937
   332
                    Markup.markup Markup.sendback "apply metis")
wenzelm@32938
   333
                | ERROR msg => (false, "Error: " ^ msg);
wenzelm@32938
   334
            val _ = unregister result (Thread.self ());
wenzelm@32938
   335
          in () end handle Exn.Interrupt => ())
wenzelm@28595
   336
      in () end);
wenzelm@28582
   337
wenzelm@28582
   338
wenzelm@28582
   339
(* sledghammer for first subgoal *)
wenzelm@28582
   340
wenzelm@28582
   341
fun sledgehammer names proof_state =
wenzelm@28582
   342
  let
wenzelm@32938
   343
    val provers = if null names then get_atps () else names;
wenzelm@32938
   344
    val birthtime = Time.now ();
wenzelm@32938
   345
    val deadtime = Time.+ (birthtime, Time.fromSeconds (! timeout));
wenzelm@28595
   346
  in List.app (fn name => start_prover name birthtime deadtime 1 proof_state) provers end;
wenzelm@28582
   347
wenzelm@28582
   348
wenzelm@28586
   349
wenzelm@28586
   350
(** Isar command syntax **)
wenzelm@28582
   351
wenzelm@28582
   352
local structure K = OuterKeyword and P = OuterParse in
wenzelm@28582
   353
wenzelm@28582
   354
val _ =
wenzelm@28582
   355
  OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
wenzelm@28586
   356
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill));
wenzelm@28582
   357
wenzelm@28582
   358
val _ =
wenzelm@28582
   359
  OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag
wenzelm@28582
   360
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative info));
wenzelm@28582
   361
wenzelm@28582
   362
val _ =
wenzelm@29112
   363
  OuterSyntax.improper_command "atp_messages" "print recent messages issued by managed provers" K.diag
wenzelm@29112
   364
    (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
wenzelm@29112
   365
      (fn limit => Toplevel.no_timing o Toplevel.imperative (fn () => messages limit)));
wenzelm@29112
   366
wenzelm@29112
   367
val _ =
wenzelm@28582
   368
  OuterSyntax.improper_command "print_atps" "print external provers" K.diag
wenzelm@28582
   369
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
wenzelm@28582
   370
      Toplevel.keep (print_provers o Toplevel.theory_of)));
wenzelm@28582
   371
wenzelm@28582
   372
val _ =
wenzelm@28582
   373
  OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag
wenzelm@28582
   374
    (Scan.repeat P.xname >> (fn names => Toplevel.no_timing o Toplevel.unknown_proof o
wenzelm@30800
   375
      Toplevel.keep (sledgehammer names o Toplevel.proof_of)));
wenzelm@28582
   376
wenzelm@28582
   377
end;
wenzelm@28582
   378
wenzelm@28582
   379
end;
immler@30537
   380