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