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