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