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