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