src/HOL/Tools/atp_manager.ML
author wenzelm
Tue Oct 14 15:45:44 2008 +0200 (2008-10-14 ago)
changeset 28589 581b2ab9827a
parent 28586 d238b83ba3fc
child 28595 67e3945b53f1
permissions -rw-r--r--
adding preferences is now permissive, no error handling here;
tuned messages;
wenzelm@28477
     1
(*  Title:      HOL/Tools/atp_manager.ML
wenzelm@28477
     2
    ID:         $Id$
wenzelm@28477
     3
    Author:     Fabian Immler, TU Muenchen
wenzelm@28477
     4
wenzelm@28571
     5
ATP threads are registered here.
wenzelm@28571
     6
Threads with the same birth-time are seen as one group.
wenzelm@28571
     7
All threads of a group are killed when one thread of it has been successful,
wenzelm@28571
     8
or after a certain time,
wenzelm@28571
     9
or when the maximum number of threads exceeds; then the oldest thread is killed.
wenzelm@28477
    10
*)
wenzelm@28477
    11
wenzelm@28477
    12
signature ATP_MANAGER =
wenzelm@28477
    13
sig
wenzelm@28586
    14
  val get_atps: unit -> string
wenzelm@28477
    15
  val set_atps: string -> unit
wenzelm@28586
    16
  val get_max_atps: unit -> int
wenzelm@28586
    17
  val set_max_atps: int -> unit
wenzelm@28586
    18
  val get_timeout: unit -> int
wenzelm@28477
    19
  val set_timeout: int -> unit
wenzelm@28586
    20
  val kill: unit -> unit
wenzelm@28586
    21
  val info: unit -> unit
wenzelm@28571
    22
  val atp_thread: (unit -> 'a option) -> ('a -> string) -> Thread.thread
wenzelm@28571
    23
  val add_prover: string -> (int -> Proof.state -> Thread.thread) -> theory -> theory
wenzelm@28484
    24
  val print_provers: theory -> unit
wenzelm@28571
    25
  val sledgehammer: string list -> Proof.state -> unit
wenzelm@28477
    26
end;
wenzelm@28477
    27
wenzelm@28586
    28
structure AtpManager: ATP_MANAGER =
wenzelm@28477
    29
struct
wenzelm@28477
    30
wenzelm@28586
    31
(** preferences **)
wenzelm@28586
    32
wenzelm@28586
    33
local
wenzelm@28586
    34
wenzelm@28586
    35
val atps = ref "e";
wenzelm@28586
    36
val max_atps = ref 5;   (* ~1 means infinite number of atps *)
wenzelm@28586
    37
val timeout = ref 60;
wenzelm@28586
    38
wenzelm@28586
    39
in
wenzelm@28586
    40
wenzelm@28586
    41
fun get_atps () = CRITICAL (fn () => ! atps);
wenzelm@28586
    42
fun set_atps str = CRITICAL (fn () => atps := str);
wenzelm@28586
    43
wenzelm@28586
    44
fun get_max_atps () = CRITICAL (fn () => ! max_atps);
wenzelm@28586
    45
fun set_max_atps number = CRITICAL (fn () => max_atps := number);
wenzelm@28586
    46
wenzelm@28586
    47
fun get_timeout () = CRITICAL (fn () => ! timeout);
wenzelm@28586
    48
fun set_timeout time = CRITICAL (fn () => timeout := time);
wenzelm@28586
    49
wenzelm@28586
    50
val _ =
wenzelm@28586
    51
  ProofGeneralPgip.add_preference "Proof"
wenzelm@28586
    52
    (Preferences.string_pref atps
wenzelm@28589
    53
      "ATP: provers" "Default automatic provers (separated by whitespace)");
wenzelm@28586
    54
wenzelm@28586
    55
val _ = ProofGeneralPgip.add_preference "Proof"
wenzelm@28586
    56
    (Preferences.int_pref max_atps
wenzelm@28589
    57
      "ATP: maximum number" "How many provers may run in parallel");
wenzelm@28586
    58
wenzelm@28586
    59
val _ = ProofGeneralPgip.add_preference "Proof"
wenzelm@28586
    60
    (Preferences.int_pref timeout
wenzelm@28589
    61
      "ATP: timeout" "ATPs will be interrupted after this time (in seconds)");
wenzelm@28586
    62
wenzelm@28586
    63
end;
wenzelm@28586
    64
wenzelm@28586
    65
wenzelm@28586
    66
wenzelm@28586
    67
(** thread management **)
wenzelm@28586
    68
wenzelm@28582
    69
(* data structures over threads *)
wenzelm@28582
    70
wenzelm@28582
    71
structure ThreadHeap = HeapFun
wenzelm@28582
    72
(
wenzelm@28582
    73
  type elem = Time.time * Thread.thread;
wenzelm@28582
    74
  fun ord ((a, _), (b, _)) = Time.compare (a, b);
wenzelm@28582
    75
)
wenzelm@28582
    76
wenzelm@28582
    77
val lookup_thread = AList.lookup Thread.equal;
wenzelm@28582
    78
val delete_thread = AList.delete Thread.equal;
wenzelm@28582
    79
val update_thread = AList.update Thread.equal;
wenzelm@28582
    80
wenzelm@28582
    81
wenzelm@28582
    82
(* state of thread manager *)
wenzelm@28477
    83
wenzelm@28582
    84
datatype T = State of
wenzelm@28582
    85
 {timeout_heap: ThreadHeap.T,
wenzelm@28582
    86
  oldest_heap: ThreadHeap.T,
wenzelm@28582
    87
  active: (Thread.thread * (Time.time * Time.time * string)) list,
wenzelm@28582
    88
  cancelling: (Thread.thread * (Time.time * Time.time * string)) list};
wenzelm@28582
    89
wenzelm@28582
    90
fun make_state timeout_heap oldest_heap active cancelling =
wenzelm@28582
    91
  State {timeout_heap = timeout_heap, oldest_heap = oldest_heap,
wenzelm@28582
    92
    active = active, cancelling = cancelling};
wenzelm@28582
    93
wenzelm@28582
    94
val state = Synchronized.var "atp_manager" (make_state ThreadHeap.empty ThreadHeap.empty [] []);
wenzelm@28477
    95
wenzelm@28582
    96
wenzelm@28586
    97
(* the managing thread *)
wenzelm@28582
    98
wenzelm@28582
    99
(*watches over running threads and interrupts them if required*)
wenzelm@28582
   100
val managing_thread = ref (NONE: Thread.thread option);
wenzelm@28477
   101
wenzelm@28582
   102
wenzelm@28582
   103
(* unregister thread from thread manager -- move to cancelling *)
wenzelm@28582
   104
wenzelm@28582
   105
fun unregister success message thread = Synchronized.change_result state
wenzelm@28582
   106
  (fn State {timeout_heap, oldest_heap, active, cancelling} =>
wenzelm@28477
   107
    let
wenzelm@28582
   108
      val info = lookup_thread active thread
wenzelm@28582
   109
wenzelm@28582
   110
      (* get birthtime of unregistering thread if successful - for group-killing*)
wenzelm@28582
   111
      val birthtime = case info of NONE => Time.zeroTime
wenzelm@28582
   112
        | SOME (tb, _, _) => if success then tb else Time.zeroTime
wenzelm@28582
   113
wenzelm@28582
   114
      (* move unregistering thread to cancelling *)
wenzelm@28582
   115
      val active' = delete_thread thread active
wenzelm@28582
   116
      val cancelling' = case info of NONE => cancelling
wenzelm@28582
   117
        | SOME (tb, _, desc) => update_thread (thread, (tb, Time.now (), desc)) cancelling
wenzelm@28582
   118
wenzelm@28582
   119
      (* move all threads of the same group to cancelling *)
wenzelm@28582
   120
      val group_threads = active |> map_filter (fn (th, (tb, _, desc)) =>
wenzelm@28582
   121
          if tb = birthtime then SOME (th, (tb, Time.now (), desc)) else NONE)
wenzelm@28582
   122
      val active'' = filter_out (fn (_, (tb, _, _)) => tb = birthtime) active'
wenzelm@28582
   123
      val cancelling'' = append group_threads cancelling'
wenzelm@28582
   124
wenzelm@28582
   125
      (* message for user *)
wenzelm@28582
   126
      val message' = case info of NONE => ""
wenzelm@28586
   127
        | SOME (_, _, desc) => "Sledgehammer: " ^ desc ^ "\n" ^ message ^
wenzelm@28582
   128
          (if null group_threads then ""
wenzelm@28586
   129
           else "\nInterrupted " ^ string_of_int (length group_threads - 1) ^ " other group members")
wenzelm@28582
   130
    in (message', make_state timeout_heap oldest_heap active'' cancelling'') end);
wenzelm@28582
   131
wenzelm@28582
   132
wenzelm@28582
   133
(* start a watching thread which runs forever -- only one may exist *)
wenzelm@28582
   134
wenzelm@28586
   135
fun check_thread_manager () = CRITICAL (fn () =>
wenzelm@28582
   136
  if (case ! managing_thread of SOME thread => Thread.isActive thread | NONE => false)
wenzelm@28582
   137
  then () else managing_thread := SOME (SimpleThread.fork false (fn () =>
wenzelm@28571
   138
    let
wenzelm@28582
   139
      val min_wait_time = Time.fromMilliseconds 300
wenzelm@28582
   140
      val max_wait_time = Time.fromSeconds 10
wenzelm@28582
   141
wenzelm@28582
   142
      (* wait for next thread to cancel, or maximum*)
wenzelm@28582
   143
      fun time_limit (State {timeout_heap, ...}) =
wenzelm@28582
   144
        (case try ThreadHeap.min timeout_heap of
wenzelm@28582
   145
          NONE => SOME (Time.+ (Time.now (), max_wait_time))
wenzelm@28582
   146
        | SOME (time, _) => SOME time)
wenzelm@28582
   147
wenzelm@28582
   148
      (* action: cancel find threads whose timeout is reached, and interrupt cancelling threads *)
wenzelm@28582
   149
      fun action (State {timeout_heap, oldest_heap, active, cancelling}) =
wenzelm@28582
   150
        let val (timeout_threads, timeout_heap') =
wenzelm@28582
   151
          ThreadHeap.upto (Time.now (), Thread.self ()) timeout_heap
wenzelm@28582
   152
        in
wenzelm@28582
   153
          if null timeout_threads andalso null cancelling then NONE
wenzelm@28582
   154
          else
wenzelm@28582
   155
            let
wenzelm@28582
   156
              val _ = List.app (SimpleThread.interrupt o #1) cancelling
wenzelm@28582
   157
              val cancelling' = filter (Thread.isActive o #1) cancelling
wenzelm@28582
   158
              val state' = make_state timeout_heap' oldest_heap active cancelling'
wenzelm@28582
   159
            in SOME (map #2 timeout_threads, state') end
wenzelm@28582
   160
        end
wenzelm@28582
   161
    in
wenzelm@28582
   162
      while true do
wenzelm@28582
   163
       ((* cancel threads found by action *)
wenzelm@28582
   164
        Synchronized.timed_access state time_limit action
wenzelm@28582
   165
        |> these
wenzelm@28582
   166
        |> List.app (priority o unregister false "Interrupted (reached timeout)");
wenzelm@28571
   167
        (* give threads time to respond to interrupt *)
wenzelm@28571
   168
        OS.Process.sleep min_wait_time)
wenzelm@28586
   169
    end)));
wenzelm@28582
   170
wenzelm@28582
   171
wenzelm@28582
   172
(* thread is registered here by sledgehammer *)
wenzelm@28477
   173
wenzelm@28582
   174
fun register birthtime deadtime (thread, desc) =
wenzelm@28582
   175
 (check_thread_manager ();
wenzelm@28582
   176
  Synchronized.change state (fn State {timeout_heap, oldest_heap, active, cancelling} =>
wenzelm@28582
   177
    let
wenzelm@28582
   178
      val timeout_heap' = ThreadHeap.insert (deadtime, thread) timeout_heap
wenzelm@28582
   179
      val oldest_heap' = ThreadHeap.insert (birthtime, thread) oldest_heap
wenzelm@28582
   180
      val active' = update_thread (thread, (birthtime, deadtime, desc)) active
wenzelm@28582
   181
    in make_state timeout_heap' oldest_heap' active' cancelling end));
wenzelm@28477
   182
wenzelm@28477
   183
wenzelm@28586
   184
wenzelm@28586
   185
(** user commands **)
wenzelm@28582
   186
wenzelm@28586
   187
(* kill: move all threads to cancelling *)
wenzelm@28586
   188
wenzelm@28586
   189
fun kill () = Synchronized.change state
wenzelm@28582
   190
  (fn State {timeout_heap, oldest_heap, active, cancelling} =>
wenzelm@28582
   191
    let val formerly_active = map (fn (th, (tb, _, desc)) => (th, (tb, Time.now (), desc))) active
wenzelm@28582
   192
    in make_state timeout_heap oldest_heap [] (formerly_active @ cancelling) end);
wenzelm@28582
   193
wenzelm@28582
   194
wenzelm@28586
   195
(* info: information on running threads *)
wenzelm@28582
   196
wenzelm@28582
   197
fun info () =
wenzelm@28582
   198
  let
wenzelm@28582
   199
    val State {timeout_heap, oldest_heap, active, cancelling} = Synchronized.value state
wenzelm@28571
   200
    fun running_info (_, (birth_time, dead_time, desc)) = "Running: "
wenzelm@28589
   201
        ^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), birth_time))
wenzelm@28477
   202
        ^ " s  --  "
wenzelm@28589
   203
        ^ (string_of_int o Time.toSeconds) (Time.- (dead_time, Time.now ()))
wenzelm@28571
   204
        ^ " s to live:\n" ^ desc
wenzelm@28571
   205
    fun cancelling_info (_, (_, dead_time, desc)) = "Trying to interrupt thread since "
wenzelm@28586
   206
        ^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), dead_time))
wenzelm@28571
   207
        ^ " s:\n" ^ desc
wenzelm@28589
   208
    val running =
wenzelm@28589
   209
      if null active then "No ATPs running."
wenzelm@28589
   210
      else space_implode "\n\n" ("Running ATPs:" :: map running_info active)
wenzelm@28589
   211
    val interrupting =
wenzelm@28589
   212
      if null cancelling then ""
wenzelm@28589
   213
      else space_implode "\n\n"
wenzelm@28589
   214
        ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling)
wenzelm@28582
   215
  in writeln (running ^ "\n" ^ interrupting) end;
wenzelm@28582
   216
wenzelm@28582
   217
wenzelm@28477
   218
wenzelm@28586
   219
(** The Sledgehammer **)
wenzelm@28477
   220
wenzelm@28582
   221
(* named provers *)
wenzelm@28484
   222
wenzelm@28582
   223
fun err_dup_prover name = error ("Duplicate prover: " ^ quote name);
wenzelm@28477
   224
wenzelm@28582
   225
structure Provers = TheoryDataFun
wenzelm@28582
   226
(
wenzelm@28582
   227
  type T = ((int -> Proof.state -> Thread.thread) * stamp) Symtab.table
wenzelm@28582
   228
  val empty = Symtab.empty
wenzelm@28582
   229
  val copy = I
wenzelm@28582
   230
  val extend = I
wenzelm@28582
   231
  fun merge _ tabs : T = Symtab.merge (eq_snd op =) tabs
wenzelm@28589
   232
    handle Symtab.DUP dup => err_dup_prover dup
wenzelm@28582
   233
);
wenzelm@28484
   234
wenzelm@28586
   235
fun add_prover name prover_fn thy =
wenzelm@28586
   236
  Provers.map (Symtab.update_new (name, (prover_fn, stamp ()))) thy
wenzelm@28582
   237
    handle Symtab.DUP dup => err_dup_prover dup;
wenzelm@28582
   238
wenzelm@28582
   239
fun print_provers thy = Pretty.writeln
wenzelm@28582
   240
  (Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy))));
wenzelm@28571
   241
wenzelm@28582
   242
fun prover_desc state subgoal name =
wenzelm@28586
   243
  let val (ctxt, (_, goal)) = Proof.get_goal state in
wenzelm@28586
   244
    "external prover " ^ quote name ^ " for subgoal " ^ string_of_int subgoal ^ ":\n" ^
wenzelm@28586
   245
      Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal subgoal))
wenzelm@28586
   246
  end;
wenzelm@28586
   247
wenzelm@28586
   248
wenzelm@28586
   249
(* thread wrapping an atp-call *)
wenzelm@28586
   250
wenzelm@28586
   251
fun atp_thread call_prover produce_answer =
wenzelm@28586
   252
  SimpleThread.fork true (fn () =>
wenzelm@28586
   253
    let
wenzelm@28586
   254
      val result = call_prover ()
wenzelm@28586
   255
      val message = case result of NONE => "Failed."
wenzelm@28586
   256
          | SOME result => "Try this command: " ^ produce_answer result
wenzelm@28586
   257
    in priority (unregister (is_some result) message (Thread.self ()))
wenzelm@28586
   258
    end handle Interrupt => ());
wenzelm@28582
   259
wenzelm@28582
   260
fun run_prover state subgoal name =
wenzelm@28582
   261
  (case Symtab.lookup (Provers.get (Proof.theory_of state)) name of
wenzelm@28582
   262
    NONE => (warning ("Unknown external prover: " ^ quote name); NONE)
wenzelm@28582
   263
  | SOME (prover_fn, _) => SOME (prover_fn subgoal state, prover_desc state subgoal name));
wenzelm@28477
   264
wenzelm@28484
   265
wenzelm@28582
   266
(* kill excessive atp threads *)
wenzelm@28484
   267
wenzelm@28582
   268
local
wenzelm@28484
   269
wenzelm@28582
   270
fun excessive_atps active =
wenzelm@28586
   271
  let val max = get_max_atps ()
wenzelm@28582
   272
  in length active > max andalso max > ~1 end;
wenzelm@28484
   273
wenzelm@28582
   274
fun kill_oldest () =
wenzelm@28582
   275
  let exception Unchanged in
wenzelm@28582
   276
    Synchronized.change_result state (fn State {timeout_heap, oldest_heap, active, cancelling} =>
wenzelm@28582
   277
        if ThreadHeap.is_empty oldest_heap orelse not (excessive_atps active)
wenzelm@28582
   278
        then raise Unchanged
wenzelm@28582
   279
        else
wenzelm@28582
   280
          let val ((_, oldest_thread), oldest_heap') = ThreadHeap.min_elem oldest_heap
wenzelm@28582
   281
          in (oldest_thread, make_state timeout_heap oldest_heap' active cancelling) end)
wenzelm@28586
   282
      |> (priority o unregister false "Interrupted (maximum number of ATPs exceeded)")
wenzelm@28582
   283
    handle Unchanged => ()
wenzelm@28484
   284
  end;
wenzelm@28484
   285
wenzelm@28582
   286
in
wenzelm@28582
   287
wenzelm@28582
   288
fun kill_excessive () =
wenzelm@28582
   289
  let val State {active, ...} = Synchronized.value state
wenzelm@28582
   290
  in if excessive_atps active then (kill_oldest (); kill_excessive ()) else () end;
wenzelm@28582
   291
wenzelm@28477
   292
end;
wenzelm@28582
   293
wenzelm@28582
   294
wenzelm@28582
   295
(* sledghammer for first subgoal *)
wenzelm@28582
   296
wenzelm@28582
   297
fun sledgehammer names proof_state =
wenzelm@28582
   298
  let
wenzelm@28582
   299
    val proverids =
wenzelm@28586
   300
      if null names then String.tokens (Symbol.is_ascii_blank o String.str) (get_atps ())
wenzelm@28582
   301
      else names
wenzelm@28582
   302
    val threads_names = map_filter (run_prover proof_state 1) proverids
wenzelm@28582
   303
    val birthtime = Time.now ()
wenzelm@28586
   304
    val deadtime = Time.+ (Time.now (), Time.fromSeconds (get_timeout ()))
wenzelm@28582
   305
    val _ = List.app (register birthtime deadtime) threads_names
wenzelm@28582
   306
    val _ = kill_excessive ()
wenzelm@28586
   307
  in () end;
wenzelm@28582
   308
wenzelm@28582
   309
wenzelm@28586
   310
wenzelm@28586
   311
(** Isar command syntax **)
wenzelm@28582
   312
wenzelm@28582
   313
local structure K = OuterKeyword and P = OuterParse in
wenzelm@28582
   314
wenzelm@28582
   315
val _ =
wenzelm@28582
   316
  OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
wenzelm@28586
   317
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill));
wenzelm@28582
   318
wenzelm@28582
   319
val _ =
wenzelm@28582
   320
  OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag
wenzelm@28582
   321
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative info));
wenzelm@28582
   322
wenzelm@28582
   323
val _ =
wenzelm@28582
   324
  OuterSyntax.improper_command "print_atps" "print external provers" K.diag
wenzelm@28582
   325
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
wenzelm@28582
   326
      Toplevel.keep (print_provers o Toplevel.theory_of)));
wenzelm@28582
   327
wenzelm@28582
   328
val _ =
wenzelm@28582
   329
  OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag
wenzelm@28582
   330
    (Scan.repeat P.xname >> (fn names => Toplevel.no_timing o Toplevel.unknown_proof o
wenzelm@28582
   331
    Toplevel.keep ((sledgehammer names) o Toplevel.proof_of)));
wenzelm@28582
   332
wenzelm@28582
   333
end;
wenzelm@28582
   334
wenzelm@28582
   335
end;