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