src/HOL/Tools/ATP_Manager/atp_manager.ML
author blanchet
Tue Apr 27 18:01:41 2010 +0200 (2010-04-27 ago)
changeset 36482 1281be23bd23
parent 36473 8a5c99a1c965
child 36489 2b2a2c55d787
permissions -rw-r--r--
added total goal count as argument + message when killing ATPs
wenzelm@32327
     1
(*  Title:      HOL/Tools/ATP_Manager/atp_manager.ML
wenzelm@28477
     2
    Author:     Fabian Immler, TU Muenchen
wenzelm@32996
     3
    Author:     Makarius
blanchet@35969
     4
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@28477
     5
wenzelm@32996
     6
Central manager component for ATP threads.
wenzelm@28477
     7
*)
wenzelm@28477
     8
wenzelm@28477
     9
signature ATP_MANAGER =
wenzelm@28477
    10
sig
blanchet@36393
    11
  type name_pool = Sledgehammer_HOL_Clause.name_pool
blanchet@35969
    12
  type relevance_override = Sledgehammer_Fact_Filter.relevance_override
blanchet@36281
    13
  type minimize_command = Sledgehammer_Proof_Reconstruct.minimize_command
blanchet@35969
    14
  type params =
blanchet@35969
    15
    {debug: bool,
blanchet@35969
    16
     verbose: bool,
blanchet@36143
    17
     overlord: bool,
blanchet@35969
    18
     atps: string list,
blanchet@35969
    19
     full_types: bool,
blanchet@36235
    20
     explicit_apply: bool,
blanchet@36058
    21
     respect_no_atp: bool,
blanchet@35969
    22
     relevance_threshold: real,
blanchet@36058
    23
     convergence: real,
blanchet@36220
    24
     theory_relevant: bool option,
blanchet@35969
    25
     follow_defs: bool,
blanchet@35969
    26
     isar_proof: bool,
blanchet@36400
    27
     shrink_factor: int,
blanchet@36064
    28
     sorts: bool,
blanchet@35969
    29
     timeout: Time.time,
blanchet@35969
    30
     minimize_timeout: Time.time}
blanchet@35867
    31
  type problem =
blanchet@35969
    32
    {subgoal: int,
blanchet@35969
    33
     goal: Proof.context * (thm list * thm),
blanchet@35969
    34
     relevance_override: relevance_override,
blanchet@35969
    35
     axiom_clauses: (thm * (string * int)) list option,
blanchet@35969
    36
     filtered_clauses: (thm * (string * int)) list option}
blanchet@36370
    37
  datatype failure =
blanchet@36370
    38
    Unprovable | TimedOut | OutOfResources | OldSpass | MalformedOutput |
blanchet@36370
    39
    UnknownError
blanchet@35867
    40
  type prover_result =
blanchet@36370
    41
    {outcome: failure option,
blanchet@35969
    42
     message: string,
blanchet@36393
    43
     pool: name_pool option,
blanchet@35969
    44
     relevant_thm_names: string list,
blanchet@35969
    45
     atp_run_time_in_msecs: int,
blanchet@36369
    46
     output: string,
blanchet@35969
    47
     proof: string,
blanchet@35969
    48
     internal_thm_names: string Vector.vector,
blanchet@36400
    49
     conjecture_shape: int list list,
blanchet@35969
    50
     filtered_clauses: (thm * (string * int)) list}
blanchet@36281
    51
  type prover =
blanchet@36281
    52
    params -> minimize_command -> Time.time -> problem -> prover_result
blanchet@35867
    53
blanchet@35969
    54
  val kill_atps: unit -> unit
blanchet@35969
    55
  val running_atps: unit -> unit
wenzelm@29112
    56
  val messages: int option -> unit
blanchet@35867
    57
  val add_prover: string * prover -> theory -> theory
blanchet@36379
    58
  val get_prover: theory -> string -> prover
blanchet@35969
    59
  val available_atps: theory -> unit
blanchet@36373
    60
  val start_prover_thread:
blanchet@36482
    61
    params -> Time.time -> Time.time -> int -> int -> relevance_override
blanchet@36373
    62
    -> (string -> minimize_command) -> Proof.state -> string -> unit
wenzelm@28477
    63
end;
wenzelm@28477
    64
blanchet@35865
    65
structure ATP_Manager : ATP_MANAGER =
wenzelm@28477
    66
struct
wenzelm@28477
    67
blanchet@36229
    68
open Sledgehammer_Util
blanchet@36063
    69
open Sledgehammer_Fact_Filter
blanchet@36063
    70
open Sledgehammer_Proof_Reconstruct
blanchet@35969
    71
blanchet@36281
    72
(** problems, results, provers, etc. **)
blanchet@35969
    73
blanchet@35969
    74
type params =
blanchet@35969
    75
  {debug: bool,
blanchet@35969
    76
   verbose: bool,
blanchet@36143
    77
   overlord: bool,
blanchet@35969
    78
   atps: string list,
blanchet@35969
    79
   full_types: bool,
blanchet@36235
    80
   explicit_apply: bool,
blanchet@36058
    81
   respect_no_atp: bool,
blanchet@35969
    82
   relevance_threshold: real,
blanchet@36058
    83
   convergence: real,
blanchet@36220
    84
   theory_relevant: bool option,
blanchet@35969
    85
   follow_defs: bool,
blanchet@35969
    86
   isar_proof: bool,
blanchet@36400
    87
   shrink_factor: int,
blanchet@36064
    88
   sorts: bool,
blanchet@35969
    89
   timeout: Time.time,
blanchet@35969
    90
   minimize_timeout: Time.time}
blanchet@35867
    91
blanchet@35867
    92
type problem =
blanchet@35969
    93
  {subgoal: int,
blanchet@35969
    94
   goal: Proof.context * (thm list * thm),
blanchet@35969
    95
   relevance_override: relevance_override,
blanchet@35969
    96
   axiom_clauses: (thm * (string * int)) list option,
blanchet@35969
    97
   filtered_clauses: (thm * (string * int)) list option};
blanchet@35867
    98
blanchet@36370
    99
datatype failure =
blanchet@36370
   100
  Unprovable | TimedOut | OutOfResources | OldSpass | MalformedOutput |
blanchet@36370
   101
  UnknownError
blanchet@36370
   102
blanchet@35867
   103
type prover_result =
blanchet@36370
   104
  {outcome: failure option,
blanchet@35969
   105
   message: string,
blanchet@36393
   106
   pool: name_pool option,
blanchet@35969
   107
   relevant_thm_names: string list,
blanchet@35969
   108
   atp_run_time_in_msecs: int,
blanchet@36369
   109
   output: string,
blanchet@35969
   110
   proof: string,
blanchet@35969
   111
   internal_thm_names: string Vector.vector,
blanchet@36400
   112
   conjecture_shape: int list list,
blanchet@35969
   113
   filtered_clauses: (thm * (string * int)) list};
blanchet@35867
   114
blanchet@36281
   115
type prover =
blanchet@36281
   116
  params -> minimize_command -> Time.time -> problem -> prover_result
blanchet@35867
   117
blanchet@35867
   118
wenzelm@28586
   119
(** preferences **)
wenzelm@28586
   120
wenzelm@29112
   121
val message_store_limit = 20;
wenzelm@29112
   122
val message_display_limit = 5;
wenzelm@29112
   123
wenzelm@28586
   124
wenzelm@28586
   125
(** thread management **)
wenzelm@28586
   126
wenzelm@28582
   127
(* data structures over threads *)
wenzelm@28582
   128
wenzelm@32939
   129
structure Thread_Heap = Heap
wenzelm@28582
   130
(
wenzelm@28582
   131
  type elem = Time.time * Thread.thread;
wenzelm@28582
   132
  fun ord ((a, _), (b, _)) = Time.compare (a, b);
wenzelm@28595
   133
);
wenzelm@28582
   134
wenzelm@31368
   135
fun lookup_thread xs = AList.lookup Thread.equal xs;
wenzelm@32996
   136
fun delete_thread xs = AList.delete Thread.equal xs;
wenzelm@31368
   137
fun update_thread xs = AList.update Thread.equal xs;
wenzelm@28582
   138
wenzelm@28582
   139
wenzelm@28582
   140
(* state of thread manager *)
wenzelm@28477
   141
wenzelm@32938
   142
type state =
wenzelm@32938
   143
 {manager: Thread.thread option,
wenzelm@32938
   144
  timeout_heap: Thread_Heap.T,
wenzelm@28582
   145
  active: (Thread.thread * (Time.time * Time.time * string)) list,
wenzelm@32996
   146
  cancelling: (Thread.thread * (Time.time * string)) list,
immler@29620
   147
  messages: string list,
immler@29620
   148
  store: string list};
wenzelm@28582
   149
wenzelm@32996
   150
fun make_state manager timeout_heap active cancelling messages store : state =
wenzelm@32996
   151
  {manager = manager, timeout_heap = timeout_heap, active = active,
wenzelm@32996
   152
    cancelling = cancelling, messages = messages, store = store};
wenzelm@28582
   153
wenzelm@32938
   154
val global_state = Synchronized.var "atp_manager"
wenzelm@32996
   155
  (make_state NONE Thread_Heap.empty [] [] [] []);
wenzelm@28582
   156
wenzelm@31368
   157
wenzelm@32996
   158
(* unregister ATP thread *)
wenzelm@28582
   159
blanchet@36184
   160
fun unregister ({verbose, ...} : params) message thread =
blanchet@36184
   161
  Synchronized.change global_state
wenzelm@32996
   162
  (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@29150
   163
    (case lookup_thread active thread of
blanchet@36379
   164
      SOME (birth_time, _, desc) =>
wenzelm@29150
   165
        let
wenzelm@32996
   166
          val active' = delete_thread thread active;
blanchet@36184
   167
          val now = Time.now ()
blanchet@36379
   168
          val cancelling' = (thread, (now, desc)) :: cancelling;
blanchet@36184
   169
          val message' =
blanchet@36379
   170
            desc ^ "\n" ^ message ^
blanchet@36184
   171
            (if verbose then
blanchet@36184
   172
               "Total time: " ^ Int.toString (Time.toMilliseconds
blanchet@36184
   173
                                          (Time.- (now, birth_time))) ^ " ms.\n"
blanchet@36184
   174
             else
blanchet@36184
   175
               "")
wenzelm@32996
   176
          val messages' = message' :: messages;
immler@29620
   177
          val store' = message' ::
immler@29620
   178
            (if length store <= message_store_limit then store
wenzelm@32938
   179
             else #1 (chop message_store_limit store));
wenzelm@32996
   180
        in make_state manager timeout_heap active' cancelling' messages' store' end
wenzelm@30800
   181
    | NONE => state));
wenzelm@28582
   182
wenzelm@28582
   183
wenzelm@32996
   184
(* main manager thread -- only one may exist *)
wenzelm@28595
   185
wenzelm@32996
   186
val min_wait_time = Time.fromMilliseconds 300;
wenzelm@32996
   187
val max_wait_time = Time.fromSeconds 10;
wenzelm@28595
   188
blanchet@36229
   189
(* This is a workaround for Proof General's off-by-a-few sendback display bug,
blanchet@36229
   190
   whereby "pr" in "proof" is not highlighted. *)
blanchet@36229
   191
val break_into_chunks =
blanchet@36229
   192
  map (replace_all "\n\n" "\000") #> maps (space_explode "\000")
blanchet@36229
   193
immler@29620
   194
fun print_new_messages () =
blanchet@36229
   195
  case Synchronized.change_result global_state
blanchet@36229
   196
         (fn {manager, timeout_heap, active, cancelling, messages, store} =>
blanchet@36229
   197
             (messages, make_state manager timeout_heap active cancelling []
blanchet@36229
   198
                                   store)) of
blanchet@36229
   199
    [] => ()
blanchet@36229
   200
  | msgs =>
blanchet@36229
   201
    msgs |> break_into_chunks
blanchet@36229
   202
         |> (fn msg :: msgs => "Sledgehammer: " ^ msg :: msgs)
blanchet@36229
   203
         |> List.app priority
immler@29620
   204
blanchet@36184
   205
fun check_thread_manager params = Synchronized.change global_state
wenzelm@32996
   206
  (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@32996
   207
    if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
wenzelm@33604
   208
    else let val manager = SOME (Toplevel.thread false (fn () =>
wenzelm@32938
   209
      let
wenzelm@32938
   210
        fun time_limit timeout_heap =
wenzelm@32938
   211
          (case try Thread_Heap.min timeout_heap of
wenzelm@32938
   212
            NONE => Time.+ (Time.now (), max_wait_time)
wenzelm@32938
   213
          | SOME (time, _) => time);
wenzelm@28582
   214
wenzelm@32938
   215
        (*action: find threads whose timeout is reached, and interrupt cancelling threads*)
wenzelm@32996
   216
        fun action {manager, timeout_heap, active, cancelling, messages, store} =
immler@30830
   217
          let val (timeout_threads, timeout_heap') =
wenzelm@32938
   218
            Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
immler@30830
   219
          in
wenzelm@32996
   220
            if null timeout_threads andalso null cancelling
immler@30830
   221
            then NONE
immler@30830
   222
            else
immler@30830
   223
              let
wenzelm@32938
   224
                val _ = List.app (SimpleThread.interrupt o #1) cancelling;
wenzelm@32938
   225
                val cancelling' = filter (Thread.isActive o #1) cancelling;
wenzelm@32996
   226
                val state' = make_state manager timeout_heap' active cancelling' messages store;
immler@30830
   227
              in SOME (map #2 timeout_threads, state') end
wenzelm@32938
   228
          end;
immler@30830
   229
      in
wenzelm@32938
   230
        while Synchronized.change_result global_state
wenzelm@32996
   231
          (fn state as {timeout_heap, active, cancelling, messages, store, ...} =>
wenzelm@32938
   232
            if null active andalso null cancelling andalso null messages
wenzelm@32996
   233
            then (false, make_state NONE timeout_heap active cancelling messages store)
wenzelm@32938
   234
            else (true, state))
immler@30830
   235
        do
wenzelm@32938
   236
          (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
immler@30830
   237
            |> these
blanchet@36370
   238
            |> List.app (unregister params "Timed out.\n");
immler@30830
   239
            print_new_messages ();
wenzelm@32938
   240
            (*give threads some time to respond to interrupt*)
immler@30830
   241
            OS.Process.sleep min_wait_time)
immler@30830
   242
      end))
wenzelm@32996
   243
    in make_state manager timeout_heap active cancelling messages store end);
wenzelm@28582
   244
wenzelm@28582
   245
wenzelm@32996
   246
(* register ATP thread *)
wenzelm@28477
   247
blanchet@36184
   248
fun register params birth_time death_time (thread, desc) =
wenzelm@32938
   249
 (Synchronized.change global_state
wenzelm@32996
   250
    (fn {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@29112
   251
      let
wenzelm@32996
   252
        val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
wenzelm@32996
   253
        val active' = update_thread (thread, (birth_time, death_time, desc)) active;
wenzelm@32996
   254
        val state' = make_state manager timeout_heap' active' cancelling messages store;
wenzelm@32938
   255
      in state' end);
blanchet@36184
   256
  check_thread_manager params);
wenzelm@28477
   257
wenzelm@28477
   258
wenzelm@28586
   259
wenzelm@28586
   260
(** user commands **)
wenzelm@28582
   261
blanchet@35969
   262
(* kill ATPs *)
wenzelm@28586
   263
blanchet@35969
   264
fun kill_atps () = Synchronized.change global_state
wenzelm@32996
   265
  (fn {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@32938
   266
    let
wenzelm@32996
   267
      val killing = map (fn (th, (_, _, desc)) => (th, (Time.now (), desc))) active;
wenzelm@32996
   268
      val state' = make_state manager timeout_heap [] (killing @ cancelling) messages store;
blanchet@36482
   269
      val _ = if null active then ()
blanchet@36482
   270
              else priority "Killed active Sledgehammer threads."
wenzelm@32938
   271
    in state' end);
wenzelm@28582
   272
wenzelm@28582
   273
blanchet@35969
   274
(* running_atps *)
wenzelm@28582
   275
wenzelm@32938
   276
fun seconds time = string_of_int (Time.toSeconds time) ^ "s";
wenzelm@32938
   277
blanchet@35969
   278
fun running_atps () =
wenzelm@28582
   279
  let
wenzelm@32938
   280
    val {active, cancelling, ...} = Synchronized.value global_state;
wenzelm@29112
   281
wenzelm@32938
   282
    val now = Time.now ();
wenzelm@32996
   283
    fun running_info (_, (birth_time, death_time, desc)) =
wenzelm@32938
   284
      "Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
wenzelm@32996
   285
        seconds (Time.- (death_time, now)) ^ " to live:\n" ^ desc;
wenzelm@32996
   286
    fun cancelling_info (_, (deadth_time, desc)) =
wenzelm@32996
   287
      "Trying to interrupt thread since " ^ seconds (Time.- (now, deadth_time)) ^ ":\n" ^ desc;
wenzelm@29112
   288
wenzelm@28589
   289
    val running =
wenzelm@28589
   290
      if null active then "No ATPs running."
wenzelm@32938
   291
      else space_implode "\n\n" ("Running ATPs:" :: map running_info active);
wenzelm@28589
   292
    val interrupting =
wenzelm@28589
   293
      if null cancelling then ""
wenzelm@32938
   294
      else
wenzelm@32938
   295
        space_implode "\n\n"
wenzelm@32938
   296
          ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling);
wenzelm@29112
   297
blanchet@36229
   298
  in priority (running ^ "\n" ^ interrupting) end;
wenzelm@28582
   299
wenzelm@29112
   300
fun messages opt_limit =
wenzelm@29112
   301
  let
wenzelm@29112
   302
    val limit = the_default message_display_limit opt_limit;
wenzelm@32938
   303
    val {store, ...} = Synchronized.value global_state;
wenzelm@32938
   304
    val header =
wenzelm@32938
   305
      "Recent ATP messages" ^
wenzelm@32938
   306
        (if length store <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
blanchet@36229
   307
  in List.app priority (header :: break_into_chunks (#1 (chop limit store))) end
wenzelm@28582
   308
wenzelm@28477
   309
wenzelm@28586
   310
(** The Sledgehammer **)
wenzelm@28477
   311
wenzelm@28582
   312
(* named provers *)
wenzelm@28484
   313
blanchet@36229
   314
fun err_dup_prover name = error ("Duplicate prover: " ^ quote name ^ ".");
wenzelm@28477
   315
blanchet@36379
   316
structure Data = Theory_Data
wenzelm@28582
   317
(
blanchet@35867
   318
  type T = (prover * stamp) Symtab.table;
wenzelm@32938
   319
  val empty = Symtab.empty;
wenzelm@32938
   320
  val extend = I;
wenzelm@33522
   321
  fun merge data : T = Symtab.merge (eq_snd op =) data
blanchet@36281
   322
    handle Symtab.DUP name => err_dup_prover name;
wenzelm@28582
   323
);
wenzelm@28484
   324
boehmes@32864
   325
fun add_prover (name, prover) thy =
blanchet@36379
   326
  Data.map (Symtab.update_new (name, (prover, stamp ()))) thy
blanchet@36379
   327
  handle Symtab.DUP name => err_dup_prover name;
wenzelm@28582
   328
wenzelm@32995
   329
fun get_prover thy name =
blanchet@36379
   330
  case Symtab.lookup (Data.get thy) name of
blanchet@36379
   331
    SOME (prover, _) => prover
blanchet@36379
   332
  | NONE => error ("Unknown ATP: " ^ name)
wenzelm@32995
   333
blanchet@36229
   334
fun available_atps thy =
blanchet@36229
   335
  priority ("Available ATPs: " ^
blanchet@36379
   336
            commas (sort_strings (Symtab.keys (Data.get thy))) ^ ".")
wenzelm@28571
   337
wenzelm@28586
   338
wenzelm@28595
   339
(* start prover thread *)
wenzelm@28484
   340
blanchet@36482
   341
fun start_prover_thread (params as {timeout, ...}) birth_time death_time i n
blanchet@36373
   342
                        relevance_override minimize_command proof_state name =
blanchet@36379
   343
  let
blanchet@36379
   344
    val prover = get_prover (Proof.theory_of proof_state) name
blanchet@36379
   345
    val {context = ctxt, facts, goal} = Proof.goal proof_state;
blanchet@36379
   346
    val desc =
blanchet@36379
   347
      "ATP " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
blanchet@36392
   348
      Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i));
blanchet@36379
   349
    val _ = Toplevel.thread true (fn () =>
wenzelm@28595
   350
      let
blanchet@36379
   351
        val _ = register params birth_time death_time (Thread.self (), desc)
blanchet@36379
   352
        val problem =
blanchet@36379
   353
          {subgoal = i, goal = (ctxt, (facts, goal)),
blanchet@36379
   354
           relevance_override = relevance_override, axiom_clauses = NONE,
blanchet@36379
   355
           filtered_clauses = NONE}
blanchet@36379
   356
        val message =
blanchet@36379
   357
          #message (prover params (minimize_command name) timeout problem)
blanchet@36379
   358
          handle Sledgehammer_HOL_Clause.TRIVIAL => metis_line i n []
blanchet@36383
   359
               | ERROR message => "Error: " ^ message ^ "\n"
blanchet@36379
   360
        val _ = unregister params message (Thread.self ());
blanchet@36379
   361
      in () end)
blanchet@36379
   362
  in () end
wenzelm@28582
   363
wenzelm@28582
   364
end;