src/HOL/Tools/ATP_Manager/atp_manager.ML
author blanchet
Wed Apr 14 21:22:13 2010 +0200 (2010-04-14 ago)
changeset 36143 6490319b1703
parent 36064 48aec67c284f
child 36167 c1a35be8e476
permissions -rw-r--r--
added "overlord" option (to get easy access to output files for debugging) + systematically use "raw_goal" rather than an inconsistent mixture
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@36059
    21
     theory_const: 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@36059
    74
   theory_const: 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
wenzelm@33312
   163
fun unregister message thread = Synchronized.change global_state
wenzelm@32996
   164
  (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@29150
   165
    (case lookup_thread active thread of
wenzelm@35569
   166
      SOME (_, _, description) =>
wenzelm@29150
   167
        let
wenzelm@32996
   168
          val active' = delete_thread thread active;
wenzelm@32996
   169
          val cancelling' = (thread, (Time.now (), description)) :: cancelling;
wenzelm@32996
   170
          val message' = description ^ "\n" ^ message;
wenzelm@32996
   171
          val messages' = message' :: messages;
immler@29620
   172
          val store' = message' ::
immler@29620
   173
            (if length store <= message_store_limit then store
wenzelm@32938
   174
             else #1 (chop message_store_limit store));
wenzelm@32996
   175
        in make_state manager timeout_heap active' cancelling' messages' store' end
wenzelm@30800
   176
    | NONE => state));
wenzelm@28582
   177
wenzelm@28582
   178
wenzelm@32996
   179
(* main manager thread -- only one may exist *)
wenzelm@28595
   180
wenzelm@32996
   181
val min_wait_time = Time.fromMilliseconds 300;
wenzelm@32996
   182
val max_wait_time = Time.fromSeconds 10;
wenzelm@28595
   183
immler@29620
   184
fun print_new_messages () =
wenzelm@32938
   185
  let val msgs = Synchronized.change_result global_state
wenzelm@32996
   186
    (fn {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@32996
   187
      (messages, make_state manager timeout_heap active cancelling [] store))
wenzelm@30800
   188
  in
wenzelm@32938
   189
    if null msgs then ()
wenzelm@32938
   190
    else priority ("Sledgehammer: " ^ space_implode "\n\n" msgs)
wenzelm@30800
   191
  end;
immler@29620
   192
wenzelm@32938
   193
fun check_thread_manager () = Synchronized.change global_state
wenzelm@32996
   194
  (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@32996
   195
    if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
wenzelm@33604
   196
    else let val manager = SOME (Toplevel.thread false (fn () =>
wenzelm@32938
   197
      let
wenzelm@32938
   198
        fun time_limit timeout_heap =
wenzelm@32938
   199
          (case try Thread_Heap.min timeout_heap of
wenzelm@32938
   200
            NONE => Time.+ (Time.now (), max_wait_time)
wenzelm@32938
   201
          | SOME (time, _) => time);
wenzelm@28582
   202
wenzelm@32938
   203
        (*action: find threads whose timeout is reached, and interrupt cancelling threads*)
wenzelm@32996
   204
        fun action {manager, timeout_heap, active, cancelling, messages, store} =
immler@30830
   205
          let val (timeout_threads, timeout_heap') =
wenzelm@32938
   206
            Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
immler@30830
   207
          in
wenzelm@32996
   208
            if null timeout_threads andalso null cancelling
immler@30830
   209
            then NONE
immler@30830
   210
            else
immler@30830
   211
              let
wenzelm@32938
   212
                val _ = List.app (SimpleThread.interrupt o #1) cancelling;
wenzelm@32938
   213
                val cancelling' = filter (Thread.isActive o #1) cancelling;
wenzelm@32996
   214
                val state' = make_state manager timeout_heap' active cancelling' messages store;
immler@30830
   215
              in SOME (map #2 timeout_threads, state') end
wenzelm@32938
   216
          end;
immler@30830
   217
      in
wenzelm@32938
   218
        while Synchronized.change_result global_state
wenzelm@32996
   219
          (fn state as {timeout_heap, active, cancelling, messages, store, ...} =>
wenzelm@32938
   220
            if null active andalso null cancelling andalso null messages
wenzelm@32996
   221
            then (false, make_state NONE timeout_heap active cancelling messages store)
wenzelm@32938
   222
            else (true, state))
immler@30830
   223
        do
wenzelm@32938
   224
          (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
immler@30830
   225
            |> these
wenzelm@33312
   226
            |> List.app (unregister "Interrupted (reached timeout)");
immler@30830
   227
            print_new_messages ();
wenzelm@32938
   228
            (*give threads some time to respond to interrupt*)
immler@30830
   229
            OS.Process.sleep min_wait_time)
immler@30830
   230
      end))
wenzelm@32996
   231
    in make_state manager timeout_heap active cancelling messages store end);
wenzelm@28582
   232
wenzelm@28582
   233
wenzelm@32996
   234
(* register ATP thread *)
wenzelm@28477
   235
wenzelm@32996
   236
fun register birth_time death_time (thread, desc) =
wenzelm@32938
   237
 (Synchronized.change global_state
wenzelm@32996
   238
    (fn {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@29112
   239
      let
wenzelm@32996
   240
        val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
wenzelm@32996
   241
        val active' = update_thread (thread, (birth_time, death_time, desc)) active;
wenzelm@32996
   242
        val state' = make_state manager timeout_heap' active' cancelling messages store;
wenzelm@32938
   243
      in state' end);
immler@30798
   244
  check_thread_manager ());
wenzelm@28477
   245
wenzelm@28477
   246
wenzelm@28586
   247
wenzelm@28586
   248
(** user commands **)
wenzelm@28582
   249
blanchet@35969
   250
(* kill ATPs *)
wenzelm@28586
   251
blanchet@35969
   252
fun kill_atps () = Synchronized.change global_state
wenzelm@32996
   253
  (fn {manager, timeout_heap, active, cancelling, messages, store} =>
wenzelm@32938
   254
    let
wenzelm@32996
   255
      val killing = map (fn (th, (_, _, desc)) => (th, (Time.now (), desc))) active;
wenzelm@32996
   256
      val state' = make_state manager timeout_heap [] (killing @ cancelling) messages store;
wenzelm@32938
   257
    in state' end);
wenzelm@28582
   258
wenzelm@28582
   259
blanchet@35969
   260
(* running_atps *)
wenzelm@28582
   261
wenzelm@32938
   262
fun seconds time = string_of_int (Time.toSeconds time) ^ "s";
wenzelm@32938
   263
blanchet@35969
   264
fun running_atps () =
wenzelm@28582
   265
  let
wenzelm@32938
   266
    val {active, cancelling, ...} = Synchronized.value global_state;
wenzelm@29112
   267
wenzelm@32938
   268
    val now = Time.now ();
wenzelm@32996
   269
    fun running_info (_, (birth_time, death_time, desc)) =
wenzelm@32938
   270
      "Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
wenzelm@32996
   271
        seconds (Time.- (death_time, now)) ^ " to live:\n" ^ desc;
wenzelm@32996
   272
    fun cancelling_info (_, (deadth_time, desc)) =
wenzelm@32996
   273
      "Trying to interrupt thread since " ^ seconds (Time.- (now, deadth_time)) ^ ":\n" ^ desc;
wenzelm@29112
   274
wenzelm@28589
   275
    val running =
wenzelm@28589
   276
      if null active then "No ATPs running."
wenzelm@32938
   277
      else space_implode "\n\n" ("Running ATPs:" :: map running_info active);
wenzelm@28589
   278
    val interrupting =
wenzelm@28589
   279
      if null cancelling then ""
wenzelm@32938
   280
      else
wenzelm@32938
   281
        space_implode "\n\n"
wenzelm@32938
   282
          ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling);
wenzelm@29112
   283
wenzelm@28582
   284
  in writeln (running ^ "\n" ^ interrupting) end;
wenzelm@28582
   285
wenzelm@29112
   286
fun messages opt_limit =
wenzelm@29112
   287
  let
wenzelm@29112
   288
    val limit = the_default message_display_limit opt_limit;
wenzelm@32938
   289
    val {store, ...} = Synchronized.value global_state;
wenzelm@32938
   290
    val header =
wenzelm@32938
   291
      "Recent ATP messages" ^
wenzelm@32938
   292
        (if length store <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
wenzelm@32938
   293
  in writeln (space_implode "\n\n" (header :: #1 (chop limit store))) end;
wenzelm@29112
   294
wenzelm@28582
   295
wenzelm@28477
   296
wenzelm@28586
   297
(** The Sledgehammer **)
wenzelm@28477
   298
wenzelm@28582
   299
(* named provers *)
wenzelm@28484
   300
wenzelm@28582
   301
fun err_dup_prover name = error ("Duplicate prover: " ^ quote name);
wenzelm@28477
   302
wenzelm@33522
   303
structure Provers = Theory_Data
wenzelm@28582
   304
(
blanchet@35867
   305
  type T = (prover * stamp) Symtab.table;
wenzelm@32938
   306
  val empty = Symtab.empty;
wenzelm@32938
   307
  val extend = I;
wenzelm@33522
   308
  fun merge data : T = Symtab.merge (eq_snd op =) data
wenzelm@32938
   309
    handle Symtab.DUP dup => err_dup_prover dup;
wenzelm@28582
   310
);
wenzelm@28484
   311
boehmes@32864
   312
fun add_prover (name, prover) thy =
wenzelm@28595
   313
  Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy
wenzelm@28582
   314
    handle Symtab.DUP dup => err_dup_prover dup;
wenzelm@28582
   315
wenzelm@32995
   316
fun get_prover thy name =
wenzelm@32995
   317
  Option.map #1 (Symtab.lookup (Provers.get thy) name);
wenzelm@32995
   318
blanchet@35969
   319
fun available_atps thy = Pretty.writeln
blanchet@35969
   320
  (Pretty.strs ("ATPs:" :: sort_strings (Symtab.keys (Provers.get thy))));
wenzelm@28571
   321
wenzelm@28586
   322
wenzelm@28595
   323
(* start prover thread *)
wenzelm@28484
   324
blanchet@35969
   325
fun start_prover (params as {timeout, ...}) birth_time death_time i
blanchet@35969
   326
                 relevance_override proof_state name =
wenzelm@32947
   327
  (case get_prover (Proof.theory_of proof_state) name of
blanchet@35969
   328
    NONE => warning ("Unknown ATP: " ^ quote name)
immler@31037
   329
  | SOME prover =>
wenzelm@28595
   330
      let
wenzelm@35592
   331
        val {context = ctxt, facts, goal} = Proof.goal proof_state;
blanchet@36063
   332
        val n = Logic.count_prems (prop_of goal)
wenzelm@28595
   333
        val desc =
blanchet@35969
   334
          "ATP " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
wenzelm@32938
   335
            Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i));
wenzelm@32996
   336
wenzelm@33604
   337
        val _ = Toplevel.thread true (fn () =>
wenzelm@28595
   338
          let
wenzelm@32996
   339
            val _ = register birth_time death_time (Thread.self (), desc);
blanchet@35969
   340
            val problem =
blanchet@35969
   341
              {subgoal = i, goal = (ctxt, (facts, goal)),
blanchet@35969
   342
               relevance_override = relevance_override, axiom_clauses = NONE,
blanchet@35969
   343
               filtered_clauses = NONE}
blanchet@35969
   344
            val message = #message (prover params timeout problem)
blanchet@36063
   345
              handle Sledgehammer_HOL_Clause.TRIVIAL =>
blanchet@36063
   346
                  metis_line i n []
wenzelm@33312
   347
                | ERROR msg => ("Error: " ^ msg);
wenzelm@33312
   348
            val _ = unregister message (Thread.self ());
wenzelm@35569
   349
          in () end);
wenzelm@28595
   350
      in () end);
wenzelm@28582
   351
wenzelm@28582
   352
blanchet@35969
   353
(* Sledgehammer the given subgoal *)
wenzelm@28582
   354
blanchet@35969
   355
fun sledgehammer (params as {atps, timeout, ...}) i relevance_override
blanchet@35969
   356
                 proof_state =
wenzelm@28582
   357
  let
blanchet@35969
   358
    val birth_time = Time.now ()
blanchet@35969
   359
    val death_time = Time.+ (birth_time, timeout)
blanchet@35969
   360
    val _ = kill_atps () (* RACE w.r.t. other invocations of Sledgehammer *)
blanchet@35969
   361
    val _ = priority "Sledgehammering..."
blanchet@35969
   362
    val _ = List.app (start_prover params birth_time death_time i
blanchet@35969
   363
                                   relevance_override proof_state) atps
blanchet@35969
   364
  in () end
wenzelm@28582
   365
wenzelm@28582
   366
end;