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