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