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