src/HOL/Tools/ATP_Manager/atp_manager.ML
author blanchet
Fri Apr 23 15:48:34 2010 +0200 (2010-04-23 ago)
changeset 36370 a4f601daa175
parent 36369 d2cd0d04b8e6
child 36371 8c83ea1a7740
permissions -rw-r--r--
centralized ATP-specific error handling in "atp_wrapper.ML"
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@35969
    11
  type relevance_override = Sledgehammer_Fact_Filter.relevance_override
blanchet@36281
    12
  type minimize_command = Sledgehammer_Proof_Reconstruct.minimize_command
blanchet@35969
    13
  type params =
blanchet@35969
    14
    {debug: bool,
blanchet@35969
    15
     verbose: bool,
blanchet@36143
    16
     overlord: bool,
blanchet@35969
    17
     atps: string list,
blanchet@35969
    18
     full_types: bool,
blanchet@36235
    19
     explicit_apply: bool,
blanchet@36058
    20
     respect_no_atp: bool,
blanchet@35969
    21
     relevance_threshold: real,
blanchet@36058
    22
     convergence: real,
blanchet@36220
    23
     theory_relevant: bool option,
blanchet@35969
    24
     higher_order: bool option,
blanchet@35969
    25
     follow_defs: bool,
blanchet@35969
    26
     isar_proof: bool,
blanchet@36064
    27
     modulus: 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@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@35969
    48
     filtered_clauses: (thm * (string * int)) list}
blanchet@36281
    49
  type prover =
blanchet@36281
    50
    params -> minimize_command -> Time.time -> problem -> prover_result
blanchet@35867
    51
wenzelm@32937
    52
  val atps: string Unsynchronized.ref
wenzelm@32937
    53
  val timeout: int Unsynchronized.ref
wenzelm@32937
    54
  val full_types: bool Unsynchronized.ref
blanchet@35969
    55
  val kill_atps: unit -> unit
blanchet@35969
    56
  val running_atps: unit -> unit
wenzelm@29112
    57
  val messages: int option -> unit
blanchet@35867
    58
  val add_prover: string * prover -> theory -> theory
blanchet@35867
    59
  val get_prover: theory -> string -> prover option
blanchet@35969
    60
  val available_atps: theory -> unit
blanchet@36281
    61
  val sledgehammer:
blanchet@36281
    62
    params -> int -> relevance_override -> (string -> minimize_command)
blanchet@36281
    63
    -> Proof.state -> unit
wenzelm@28477
    64
end;
wenzelm@28477
    65
blanchet@35865
    66
structure ATP_Manager : ATP_MANAGER =
wenzelm@28477
    67
struct
wenzelm@28477
    68
blanchet@36229
    69
open Sledgehammer_Util
blanchet@36063
    70
open Sledgehammer_Fact_Filter
blanchet@36063
    71
open Sledgehammer_Proof_Reconstruct
blanchet@35969
    72
blanchet@36281
    73
(** problems, results, provers, etc. **)
blanchet@35969
    74
blanchet@35969
    75
type params =
blanchet@35969
    76
  {debug: bool,
blanchet@35969
    77
   verbose: bool,
blanchet@36143
    78
   overlord: bool,
blanchet@35969
    79
   atps: string list,
blanchet@35969
    80
   full_types: bool,
blanchet@36235
    81
   explicit_apply: bool,
blanchet@36058
    82
   respect_no_atp: bool,
blanchet@35969
    83
   relevance_threshold: real,
blanchet@36058
    84
   convergence: real,
blanchet@36220
    85
   theory_relevant: bool option,
blanchet@35969
    86
   higher_order: bool option,
blanchet@35969
    87
   follow_defs: bool,
blanchet@35969
    88
   isar_proof: bool,
blanchet@36064
    89
   modulus: int,
blanchet@36064
    90
   sorts: bool,
blanchet@35969
    91
   timeout: Time.time,
blanchet@35969
    92
   minimize_timeout: Time.time}
blanchet@35867
    93
blanchet@35867
    94
type problem =
blanchet@35969
    95
  {subgoal: int,
blanchet@35969
    96
   goal: Proof.context * (thm list * thm),
blanchet@35969
    97
   relevance_override: relevance_override,
blanchet@35969
    98
   axiom_clauses: (thm * (string * int)) list option,
blanchet@35969
    99
   filtered_clauses: (thm * (string * int)) list option};
blanchet@35867
   100
blanchet@36370
   101
datatype failure =
blanchet@36370
   102
  Unprovable | TimedOut | OutOfResources | OldSpass | MalformedOutput |
blanchet@36370
   103
  UnknownError
blanchet@36370
   104
blanchet@35867
   105
type prover_result =
blanchet@36370
   106
  {outcome: failure option,
blanchet@35969
   107
   message: string,
blanchet@35969
   108
   relevant_thm_names: string list,
blanchet@35969
   109
   atp_run_time_in_msecs: int,
blanchet@36369
   110
   output: string,
blanchet@35969
   111
   proof: string,
blanchet@35969
   112
   internal_thm_names: string Vector.vector,
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
blanchet@36289
   124
val atps = Unsynchronized.ref ""; (* set in "ATP_Wrapper" *)
wenzelm@32740
   125
val timeout = Unsynchronized.ref 60;
wenzelm@32740
   126
val full_types = Unsynchronized.ref false;
wenzelm@28586
   127
wenzelm@28586
   128
val _ =
wenzelm@30982
   129
  ProofGeneralPgip.add_preference Preferences.category_proof
wenzelm@28586
   130
    (Preferences.string_pref atps
wenzelm@28589
   131
      "ATP: provers" "Default automatic provers (separated by whitespace)");
wenzelm@28586
   132
wenzelm@30982
   133
val _ =
wenzelm@30982
   134
  ProofGeneralPgip.add_preference Preferences.category_proof
wenzelm@28586
   135
    (Preferences.int_pref timeout
wenzelm@28589
   136
      "ATP: timeout" "ATPs will be interrupted after this time (in seconds)");
wenzelm@28586
   137
nipkow@31791
   138
val _ =
nipkow@31791
   139
  ProofGeneralPgip.add_preference Preferences.category_proof
nipkow@31791
   140
    (Preferences.bool_pref full_types
nipkow@31791
   141
      "ATP: full types" "ATPs will use full type information");
nipkow@31791
   142
wenzelm@28586
   143
wenzelm@28586
   144
wenzelm@28586
   145
(** thread management **)
wenzelm@28586
   146
wenzelm@28582
   147
(* data structures over threads *)
wenzelm@28582
   148
wenzelm@32939
   149
structure Thread_Heap = Heap
wenzelm@28582
   150
(
wenzelm@28582
   151
  type elem = Time.time * Thread.thread;
wenzelm@28582
   152
  fun ord ((a, _), (b, _)) = Time.compare (a, b);
wenzelm@28595
   153
);
wenzelm@28582
   154
wenzelm@31368
   155
fun lookup_thread xs = AList.lookup Thread.equal xs;
wenzelm@32996
   156
fun delete_thread xs = AList.delete Thread.equal xs;
wenzelm@31368
   157
fun update_thread xs = AList.update Thread.equal xs;
wenzelm@28582
   158
wenzelm@28582
   159
wenzelm@28582
   160
(* state of thread manager *)
wenzelm@28477
   161
wenzelm@32938
   162
type state =
wenzelm@32938
   163
 {manager: Thread.thread option,
wenzelm@32938
   164
  timeout_heap: Thread_Heap.T,
wenzelm@28582
   165
  active: (Thread.thread * (Time.time * Time.time * string)) list,
wenzelm@32996
   166
  cancelling: (Thread.thread * (Time.time * string)) list,
immler@29620
   167
  messages: string list,
immler@29620
   168
  store: string list};
wenzelm@28582
   169
wenzelm@32996
   170
fun make_state manager timeout_heap active cancelling messages store : state =
wenzelm@32996
   171
  {manager = manager, timeout_heap = timeout_heap, active = active,
wenzelm@32996
   172
    cancelling = cancelling, messages = messages, store = store};
wenzelm@28582
   173
wenzelm@32938
   174
val global_state = Synchronized.var "atp_manager"
wenzelm@32996
   175
  (make_state NONE Thread_Heap.empty [] [] [] []);
wenzelm@28582
   176
wenzelm@31368
   177
wenzelm@32996
   178
(* unregister ATP thread *)
wenzelm@28582
   179
blanchet@36184
   180
fun unregister ({verbose, ...} : params) message thread =
blanchet@36184
   181
  Synchronized.change global_state
wenzelm@32996
   182
  (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@29150
   183
    (case lookup_thread active thread of
blanchet@36184
   184
      SOME (birth_time, _, description) =>
wenzelm@29150
   185
        let
wenzelm@32996
   186
          val active' = delete_thread thread active;
blanchet@36184
   187
          val now = Time.now ()
blanchet@36184
   188
          val cancelling' = (thread, (now, description)) :: cancelling;
blanchet@36184
   189
          val message' =
blanchet@36184
   190
            description ^ "\n" ^ message ^
blanchet@36184
   191
            (if verbose then
blanchet@36184
   192
               "Total time: " ^ Int.toString (Time.toMilliseconds
blanchet@36184
   193
                                          (Time.- (now, birth_time))) ^ " ms.\n"
blanchet@36184
   194
             else
blanchet@36184
   195
               "")
wenzelm@32996
   196
          val messages' = message' :: messages;
immler@29620
   197
          val store' = message' ::
immler@29620
   198
            (if length store <= message_store_limit then store
wenzelm@32938
   199
             else #1 (chop message_store_limit store));
wenzelm@32996
   200
        in make_state manager timeout_heap active' cancelling' messages' store' end
wenzelm@30800
   201
    | NONE => state));
wenzelm@28582
   202
wenzelm@28582
   203
wenzelm@32996
   204
(* main manager thread -- only one may exist *)
wenzelm@28595
   205
wenzelm@32996
   206
val min_wait_time = Time.fromMilliseconds 300;
wenzelm@32996
   207
val max_wait_time = Time.fromSeconds 10;
wenzelm@28595
   208
blanchet@36229
   209
(* This is a workaround for Proof General's off-by-a-few sendback display bug,
blanchet@36229
   210
   whereby "pr" in "proof" is not highlighted. *)
blanchet@36229
   211
val break_into_chunks =
blanchet@36229
   212
  map (replace_all "\n\n" "\000") #> maps (space_explode "\000")
blanchet@36229
   213
immler@29620
   214
fun print_new_messages () =
blanchet@36229
   215
  case Synchronized.change_result global_state
blanchet@36229
   216
         (fn {manager, timeout_heap, active, cancelling, messages, store} =>
blanchet@36229
   217
             (messages, make_state manager timeout_heap active cancelling []
blanchet@36229
   218
                                   store)) of
blanchet@36229
   219
    [] => ()
blanchet@36229
   220
  | msgs =>
blanchet@36229
   221
    msgs |> break_into_chunks
blanchet@36229
   222
         |> (fn msg :: msgs => "Sledgehammer: " ^ msg :: msgs)
blanchet@36229
   223
         |> List.app priority
immler@29620
   224
blanchet@36184
   225
fun check_thread_manager params = Synchronized.change global_state
wenzelm@32996
   226
  (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@32996
   227
    if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
wenzelm@33604
   228
    else let val manager = SOME (Toplevel.thread false (fn () =>
wenzelm@32938
   229
      let
wenzelm@32938
   230
        fun time_limit timeout_heap =
wenzelm@32938
   231
          (case try Thread_Heap.min timeout_heap of
wenzelm@32938
   232
            NONE => Time.+ (Time.now (), max_wait_time)
wenzelm@32938
   233
          | SOME (time, _) => time);
wenzelm@28582
   234
wenzelm@32938
   235
        (*action: find threads whose timeout is reached, and interrupt cancelling threads*)
wenzelm@32996
   236
        fun action {manager, timeout_heap, active, cancelling, messages, store} =
immler@30830
   237
          let val (timeout_threads, timeout_heap') =
wenzelm@32938
   238
            Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
immler@30830
   239
          in
wenzelm@32996
   240
            if null timeout_threads andalso null cancelling
immler@30830
   241
            then NONE
immler@30830
   242
            else
immler@30830
   243
              let
wenzelm@32938
   244
                val _ = List.app (SimpleThread.interrupt o #1) cancelling;
wenzelm@32938
   245
                val cancelling' = filter (Thread.isActive o #1) cancelling;
wenzelm@32996
   246
                val state' = make_state manager timeout_heap' active cancelling' messages store;
immler@30830
   247
              in SOME (map #2 timeout_threads, state') end
wenzelm@32938
   248
          end;
immler@30830
   249
      in
wenzelm@32938
   250
        while Synchronized.change_result global_state
wenzelm@32996
   251
          (fn state as {timeout_heap, active, cancelling, messages, store, ...} =>
wenzelm@32938
   252
            if null active andalso null cancelling andalso null messages
wenzelm@32996
   253
            then (false, make_state NONE timeout_heap active cancelling messages store)
wenzelm@32938
   254
            else (true, state))
immler@30830
   255
        do
wenzelm@32938
   256
          (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
immler@30830
   257
            |> these
blanchet@36370
   258
            |> List.app (unregister params "Timed out.\n");
immler@30830
   259
            print_new_messages ();
wenzelm@32938
   260
            (*give threads some time to respond to interrupt*)
immler@30830
   261
            OS.Process.sleep min_wait_time)
immler@30830
   262
      end))
wenzelm@32996
   263
    in make_state manager timeout_heap active cancelling messages store end);
wenzelm@28582
   264
wenzelm@28582
   265
wenzelm@32996
   266
(* register ATP thread *)
wenzelm@28477
   267
blanchet@36184
   268
fun register params birth_time death_time (thread, desc) =
wenzelm@32938
   269
 (Synchronized.change global_state
wenzelm@32996
   270
    (fn {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@29112
   271
      let
wenzelm@32996
   272
        val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
wenzelm@32996
   273
        val active' = update_thread (thread, (birth_time, death_time, desc)) active;
wenzelm@32996
   274
        val state' = make_state manager timeout_heap' active' cancelling messages store;
wenzelm@32938
   275
      in state' end);
blanchet@36184
   276
  check_thread_manager params);
wenzelm@28477
   277
wenzelm@28477
   278
wenzelm@28586
   279
wenzelm@28586
   280
(** user commands **)
wenzelm@28582
   281
blanchet@35969
   282
(* kill ATPs *)
wenzelm@28586
   283
blanchet@35969
   284
fun kill_atps () = Synchronized.change global_state
wenzelm@32996
   285
  (fn {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@32938
   286
    let
wenzelm@32996
   287
      val killing = map (fn (th, (_, _, desc)) => (th, (Time.now (), desc))) active;
wenzelm@32996
   288
      val state' = make_state manager timeout_heap [] (killing @ cancelling) messages store;
wenzelm@32938
   289
    in state' end);
wenzelm@28582
   290
wenzelm@28582
   291
blanchet@35969
   292
(* running_atps *)
wenzelm@28582
   293
wenzelm@32938
   294
fun seconds time = string_of_int (Time.toSeconds time) ^ "s";
wenzelm@32938
   295
blanchet@35969
   296
fun running_atps () =
wenzelm@28582
   297
  let
wenzelm@32938
   298
    val {active, cancelling, ...} = Synchronized.value global_state;
wenzelm@29112
   299
wenzelm@32938
   300
    val now = Time.now ();
wenzelm@32996
   301
    fun running_info (_, (birth_time, death_time, desc)) =
wenzelm@32938
   302
      "Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
wenzelm@32996
   303
        seconds (Time.- (death_time, now)) ^ " to live:\n" ^ desc;
wenzelm@32996
   304
    fun cancelling_info (_, (deadth_time, desc)) =
wenzelm@32996
   305
      "Trying to interrupt thread since " ^ seconds (Time.- (now, deadth_time)) ^ ":\n" ^ desc;
wenzelm@29112
   306
wenzelm@28589
   307
    val running =
wenzelm@28589
   308
      if null active then "No ATPs running."
wenzelm@32938
   309
      else space_implode "\n\n" ("Running ATPs:" :: map running_info active);
wenzelm@28589
   310
    val interrupting =
wenzelm@28589
   311
      if null cancelling then ""
wenzelm@32938
   312
      else
wenzelm@32938
   313
        space_implode "\n\n"
wenzelm@32938
   314
          ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling);
wenzelm@29112
   315
blanchet@36229
   316
  in priority (running ^ "\n" ^ interrupting) end;
wenzelm@28582
   317
wenzelm@29112
   318
fun messages opt_limit =
wenzelm@29112
   319
  let
wenzelm@29112
   320
    val limit = the_default message_display_limit opt_limit;
wenzelm@32938
   321
    val {store, ...} = Synchronized.value global_state;
wenzelm@32938
   322
    val header =
wenzelm@32938
   323
      "Recent ATP messages" ^
wenzelm@32938
   324
        (if length store <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
blanchet@36229
   325
  in List.app priority (header :: break_into_chunks (#1 (chop limit store))) end
wenzelm@28582
   326
wenzelm@28477
   327
wenzelm@28586
   328
(** The Sledgehammer **)
wenzelm@28477
   329
wenzelm@28582
   330
(* named provers *)
wenzelm@28484
   331
blanchet@36229
   332
fun err_dup_prover name = error ("Duplicate prover: " ^ quote name ^ ".");
wenzelm@28477
   333
wenzelm@33522
   334
structure Provers = Theory_Data
wenzelm@28582
   335
(
blanchet@35867
   336
  type T = (prover * stamp) Symtab.table;
wenzelm@32938
   337
  val empty = Symtab.empty;
wenzelm@32938
   338
  val extend = I;
wenzelm@33522
   339
  fun merge data : T = Symtab.merge (eq_snd op =) data
blanchet@36281
   340
    handle Symtab.DUP name => err_dup_prover name;
wenzelm@28582
   341
);
wenzelm@28484
   342
boehmes@32864
   343
fun add_prover (name, prover) thy =
wenzelm@28595
   344
  Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy
blanchet@36281
   345
    handle Symtab.DUP name => err_dup_prover name;
wenzelm@28582
   346
wenzelm@32995
   347
fun get_prover thy name =
wenzelm@32995
   348
  Option.map #1 (Symtab.lookup (Provers.get thy) name);
wenzelm@32995
   349
blanchet@36229
   350
fun available_atps thy =
blanchet@36229
   351
  priority ("Available ATPs: " ^
blanchet@36229
   352
            commas (sort_strings (Symtab.keys (Provers.get thy))) ^ ".")
wenzelm@28571
   353
wenzelm@28586
   354
wenzelm@28595
   355
(* start prover thread *)
wenzelm@28484
   356
blanchet@35969
   357
fun start_prover (params as {timeout, ...}) birth_time death_time i
blanchet@36281
   358
                 relevance_override minimize_command proof_state name =
wenzelm@32947
   359
  (case get_prover (Proof.theory_of proof_state) name of
blanchet@36226
   360
    NONE => warning ("Unknown ATP: " ^ quote name ^ ".")
immler@31037
   361
  | SOME prover =>
wenzelm@28595
   362
      let
wenzelm@35592
   363
        val {context = ctxt, facts, goal} = Proof.goal proof_state;
blanchet@36063
   364
        val n = Logic.count_prems (prop_of goal)
wenzelm@28595
   365
        val desc =
blanchet@35969
   366
          "ATP " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
wenzelm@32938
   367
            Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i));
wenzelm@32996
   368
wenzelm@33604
   369
        val _ = Toplevel.thread true (fn () =>
wenzelm@28595
   370
          let
blanchet@36184
   371
            val _ = register params birth_time death_time (Thread.self (), desc)
blanchet@35969
   372
            val problem =
blanchet@35969
   373
              {subgoal = i, goal = (ctxt, (facts, goal)),
blanchet@35969
   374
               relevance_override = relevance_override, axiom_clauses = NONE,
blanchet@35969
   375
               filtered_clauses = NONE}
blanchet@36281
   376
            val message =
blanchet@36281
   377
              #message (prover params (minimize_command name) timeout problem)
blanchet@36063
   378
              handle Sledgehammer_HOL_Clause.TRIVIAL =>
blanchet@36063
   379
                  metis_line i n []
blanchet@36226
   380
                | ERROR msg => "Error: " ^ msg ^ ".\n";
blanchet@36184
   381
            val _ = unregister params message (Thread.self ());
wenzelm@35569
   382
          in () end);
wenzelm@28595
   383
      in () end);
wenzelm@28582
   384
wenzelm@28582
   385
blanchet@35969
   386
(* Sledgehammer the given subgoal *)
wenzelm@28582
   387
blanchet@35969
   388
fun sledgehammer (params as {atps, timeout, ...}) i relevance_override
blanchet@36281
   389
                 minimize_command proof_state =
wenzelm@28582
   390
  let
blanchet@35969
   391
    val birth_time = Time.now ()
blanchet@35969
   392
    val death_time = Time.+ (birth_time, timeout)
blanchet@36181
   393
    val _ = kill_atps () (* RACE w.r.t. other invocations of Sledgehammer *)
blanchet@35969
   394
    val _ = priority "Sledgehammering..."
blanchet@35969
   395
    val _ = List.app (start_prover params birth_time death_time i
blanchet@36281
   396
                                   relevance_override minimize_command
blanchet@36281
   397
                                   proof_state) atps
blanchet@35969
   398
  in () end
wenzelm@28582
   399
wenzelm@28582
   400
end;