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