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