src/HOL/Tools/atp_manager.ML
author wenzelm
Fri Oct 03 19:35:17 2008 +0200 (2008-10-03 ago)
changeset 28484 4ed9239b09c1
parent 28478 855ca2dcc03d
child 28487 13e637e0c876
permissions -rw-r--r--
misc simplifcation and tuning;
no export of structure Provers;
added add_provers, print_provers;
operate on Proof.state, not Toplevel.state;
use Time.+ etc. to make SML/XL of NJ happy;
explicit Isar commands 'atp_kill', 'atp_info', 'print_atps';
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@28477
     5
ATP threads have to be registered here.  Threads with the same
wenzelm@28477
     6
birth-time are seen as one group.  All threads of a group are killed
wenzelm@28477
     7
when one thread of it has been successful, or after a certain time, or
wenzelm@28477
     8
when the maximum number of threads exceeds; then the oldest thread is
wenzelm@28477
     9
killed.
wenzelm@28477
    10
*)
wenzelm@28477
    11
wenzelm@28477
    12
signature ATP_MANAGER =
wenzelm@28477
    13
sig
wenzelm@28477
    14
  val kill_all: unit -> unit
wenzelm@28477
    15
  val info: unit -> unit
wenzelm@28477
    16
  val set_atps: string -> unit
wenzelm@28477
    17
  val set_max_atp: int -> unit
wenzelm@28477
    18
  val set_timeout: int -> unit
wenzelm@28477
    19
  val set_groupkilling: bool -> unit
wenzelm@28477
    20
  val start: unit -> unit
wenzelm@28477
    21
  val register: Time.time -> Time.time -> (Thread.thread * string) -> unit
wenzelm@28477
    22
  val unregister: bool -> unit
wenzelm@28484
    23
  val add_prover: string -> (Proof.state -> Thread.thread * string) -> theory -> theory
wenzelm@28484
    24
  val print_provers: theory -> unit
wenzelm@28484
    25
  val sledgehammer: Proof.state -> unit
wenzelm@28477
    26
end;
wenzelm@28477
    27
wenzelm@28477
    28
structure AtpManager : ATP_MANAGER =
wenzelm@28477
    29
struct
wenzelm@28477
    30
wenzelm@28484
    31
  structure ThreadHeap = HeapFun
wenzelm@28484
    32
  (
wenzelm@28484
    33
    type elem = Time.time * Thread.thread;
wenzelm@28484
    34
    fun ord ((a, _), (b, _)) = Time.compare (a, b);
wenzelm@28484
    35
  );
wenzelm@28477
    36
wenzelm@28477
    37
  (* create global state of threadmanager *)
wenzelm@28477
    38
  val timeout_heap = ref ThreadHeap.empty
wenzelm@28477
    39
  val oldest_heap = ref ThreadHeap.empty
wenzelm@28477
    40
  (* managed threads *)
wenzelm@28477
    41
  val active = ref ([] : (Thread.thread * Time.time * Time.time * string) list)
wenzelm@28477
    42
  val cancelling = ref ([] : (Thread.thread * Time.time * Time.time * string) list)
wenzelm@28477
    43
  (* settings *)
wenzelm@28477
    44
  val atps = ref "e,spass"
wenzelm@28477
    45
  val maximum_atps = ref 5   (* ~1 means infinite number of atps*)
wenzelm@28477
    46
  val timeout = ref 60
wenzelm@28477
    47
  val groupkilling = ref true
wenzelm@28477
    48
  (* synchronizing *)
wenzelm@28484
    49
  val lock = Mutex.mutex () (* to be acquired for changing state *)
wenzelm@28477
    50
  val state_change = ConditionVar.conditionVar () (* signal when state changes *)
wenzelm@28477
    51
  (* watches over running threads and interrupts them if required *)
wenzelm@28478
    52
  val managing_thread = ref (NONE: Thread.thread option);
wenzelm@28477
    53
wenzelm@28477
    54
  (* move a thread from active to cancelling
wenzelm@28477
    55
    managing_thread trys to interrupt all threads in cancelling
wenzelm@28477
    56
wenzelm@28477
    57
   call from an environment where a lock has already been aquired *)
wenzelm@28477
    58
  fun unregister_locked thread =
wenzelm@28477
    59
    let val entrys = (filter (fn (t,_,_,_) => Thread.equal (t, thread))) (! active)
wenzelm@28477
    60
    val entrys_update = map (fn (th, tb, _, desc) => (th, tb, Time.now(), desc)) entrys
wenzelm@28477
    61
    val _ = change cancelling (append entrys_update)
wenzelm@28477
    62
    val _ = change active (filter_out (fn (t,_,_,_) => Thread.equal (t, thread)))
wenzelm@28477
    63
    in () end;
wenzelm@28477
    64
wenzelm@28477
    65
  (* start a watching thread which runs forever *)
wenzelm@28477
    66
  (* must *not* be called more than once!! => problem with locks *)
wenzelm@28477
    67
  fun start () =
wenzelm@28477
    68
    let
wenzelm@28478
    69
    val new_thread = SimpleThread.fork false (fn () =>
wenzelm@28477
    70
      let
wenzelm@28477
    71
      (* never give up lock except for waiting *)
wenzelm@28477
    72
      val _ = Mutex.lock lock
wenzelm@28477
    73
      fun wait_for_next_event time =
wenzelm@28477
    74
        let
wenzelm@28477
    75
        (* wait for signal or next timeout, give up lock meanwhile *)
wenzelm@28477
    76
        val _ = ConditionVar.waitUntil (state_change, lock, time)
wenzelm@28477
    77
        (* move threads with priority less than Time.now() to cancelling *)
wenzelm@28477
    78
        fun cancelolder heap =
wenzelm@28477
    79
          if ThreadHeap.is_empty heap then heap else
wenzelm@28477
    80
          let val (mintime, minthread) = ThreadHeap.min heap
wenzelm@28477
    81
          in
wenzelm@28484
    82
            if Time.> (mintime, Time.now()) then heap
wenzelm@28477
    83
            else (unregister_locked minthread;
wenzelm@28477
    84
            cancelolder (ThreadHeap.delete_min heap))
wenzelm@28477
    85
          end
wenzelm@28477
    86
        val _ = change timeout_heap cancelolder
wenzelm@28477
    87
        (* try to interrupt threads that are to cancel*)
wenzelm@28477
    88
        fun interrupt t = Thread.interrupt t handle Thread _ => ()
wenzelm@28477
    89
        val _ = change cancelling (filter (fn (t,_,_,_) => Thread.isActive t))
wenzelm@28477
    90
        val _ = map (fn (t, _, _, _) => interrupt t) (! cancelling)
wenzelm@28477
    91
        (* if there are threads to cancel, send periodic interrupts *)
wenzelm@28477
    92
        (* TODO: find out what realtime-values are appropriate *)
wenzelm@28477
    93
        val next_time =
wenzelm@28477
    94
          if length (! cancelling) > 0 then
wenzelm@28484
    95
            Time.+ (Time.now(), Time.fromMilliseconds 300)
wenzelm@28477
    96
          else if ThreadHeap.is_empty (! timeout_heap) then
wenzelm@28484
    97
            Time.+ (Time.now(), Time.fromSeconds 10)
wenzelm@28477
    98
          else
wenzelm@28477
    99
            #1 (ThreadHeap.min (! timeout_heap))
wenzelm@28477
   100
          in
wenzelm@28477
   101
            wait_for_next_event next_time
wenzelm@28477
   102
          end
wenzelm@28478
   103
        in wait_for_next_event Time.zeroTime end)
wenzelm@28478
   104
      in managing_thread := SOME new_thread end
wenzelm@28477
   105
wenzelm@28477
   106
  (* calling thread registers itself to be managed here with a relative timeout *)
wenzelm@28477
   107
  fun register birthtime deadtime (thread, name) =
wenzelm@28477
   108
    let
wenzelm@28477
   109
    val _ = Mutex.lock lock
wenzelm@28477
   110
    (* create the atp-managing-thread if this is the first call to register *)
wenzelm@28478
   111
    val _ =
wenzelm@28478
   112
      if (case ! managing_thread of SOME thread => Thread.isActive thread | NONE => false)
wenzelm@28478
   113
      then () else start ()
wenzelm@28477
   114
    (* insertion *)
wenzelm@28477
   115
    val _ = change timeout_heap (ThreadHeap.insert (deadtime, thread))
wenzelm@28477
   116
    val _ = change oldest_heap (ThreadHeap.insert (birthtime, thread))
wenzelm@28477
   117
    val _ = change active (cons (thread, birthtime, deadtime, name))
wenzelm@28477
   118
    (*maximum number of atps must not exceed*)
wenzelm@28477
   119
    val _ = let
wenzelm@28477
   120
      fun kill_oldest () =
wenzelm@28477
   121
        let val (_, oldest_thread) = ThreadHeap.min (!oldest_heap)
wenzelm@28484
   122
        val _ = change oldest_heap ThreadHeap.delete_min
wenzelm@28477
   123
        in unregister_locked oldest_thread end
wenzelm@28477
   124
      in
wenzelm@28477
   125
        while ! maximum_atps > ~1 andalso length (! active) > ! maximum_atps
wenzelm@28477
   126
        do kill_oldest ()
wenzelm@28477
   127
      end
wenzelm@28477
   128
    (* state of threadmanager changed => signal *)
wenzelm@28477
   129
    val _ = ConditionVar.signal state_change
wenzelm@28477
   130
    val _ = Mutex.unlock lock
wenzelm@28477
   131
    in () end
wenzelm@28477
   132
wenzelm@28477
   133
  (* calling Thread unregisters itself from Threadmanager; thread is responsible
wenzelm@28477
   134
    to terminate after calling this method *)
wenzelm@28477
   135
  fun unregister success =
wenzelm@28477
   136
    let val _ = Mutex.lock lock
wenzelm@28477
   137
    val thread = Thread.self ()
wenzelm@28477
   138
    (* get birthtime of unregistering thread - for group-killing*)
wenzelm@28477
   139
    fun get_birthtime [] = Time.zeroTime
wenzelm@28477
   140
      | get_birthtime ((t,tb,td,desc)::actives) = if Thread.equal (thread, t)
wenzelm@28477
   141
      then tb
wenzelm@28477
   142
      else get_birthtime actives
wenzelm@28477
   143
    val birthtime = get_birthtime (! active)
wenzelm@28477
   144
    (* remove unregistering thread *)
wenzelm@28477
   145
    val _ = change active (filter_out (fn (t,_,_,_) => Thread.equal (t, thread)))
wenzelm@28477
   146
    val _ = if (! groupkilling) andalso success
wenzelm@28477
   147
      then (* remove all threads of the same group *)
wenzelm@28477
   148
      let
wenzelm@28477
   149
      val group_threads = filter (fn (_, tb, _, _) => tb = birthtime) (! active)
wenzelm@28477
   150
      val _ = change cancelling (append group_threads)
wenzelm@28477
   151
      val _ = change active (filter_out (fn (_, tb, _, _) => tb = birthtime))
wenzelm@28477
   152
      in () end
wenzelm@28477
   153
      else ()
wenzelm@28477
   154
    val _ = ConditionVar.signal state_change
wenzelm@28477
   155
    val _ = Mutex.unlock lock
wenzelm@28477
   156
    in () end;
wenzelm@28477
   157
wenzelm@28477
   158
  (* Move all threads to cancelling *)
wenzelm@28477
   159
  fun kill_all () =
wenzelm@28477
   160
    let
wenzelm@28477
   161
    val _ = Mutex.lock lock
wenzelm@28477
   162
    val _ = change active (map (fn (th, tb, _, desc) => (th, tb, Time.now(), desc)))
wenzelm@28477
   163
    val _ = change cancelling (append (! active))
wenzelm@28484
   164
    val _ = active := []
wenzelm@28477
   165
    val _ = ConditionVar.signal state_change
wenzelm@28477
   166
    val _ = Mutex.unlock lock
wenzelm@28477
   167
    in () end;
wenzelm@28477
   168
wenzelm@28477
   169
  fun info () =
wenzelm@28477
   170
    let
wenzelm@28477
   171
    val _ = Mutex.lock lock
wenzelm@28477
   172
    fun running_info (_, birth_time, dead_time, desc) =
wenzelm@28477
   173
      priority ("Running: "
wenzelm@28484
   174
        ^ ((Int.toString o Time.toSeconds) (Time.- (Time.now(), birth_time)))
wenzelm@28477
   175
        ^ " s  --  "
wenzelm@28484
   176
        ^ ((Int.toString o Time.toSeconds) (Time.- (dead_time, Time.now())))
wenzelm@28477
   177
        ^ " s to live:\n" ^ desc)
wenzelm@28477
   178
    fun cancelling_info (_, _, dead_time, desc) =
wenzelm@28477
   179
      priority ("Trying to interrupt thread since "
wenzelm@28484
   180
        ^ (Int.toString o Time.toSeconds) (Time.- (Time.now(), dead_time))
wenzelm@28477
   181
        ^ " s:\n" ^ desc )
wenzelm@28477
   182
    val _ = if length (! active) = 0 then [priority "No ATPs running."]
wenzelm@28477
   183
      else (priority "--- RUNNING ATPs ---";
wenzelm@28477
   184
      map (fn entry => running_info entry) (! active))
wenzelm@28477
   185
    val _ = if length (! cancelling) = 0 then []
wenzelm@28477
   186
      else (priority "--- TRYING TO INTERRUPT FOLLOWING ATPs ---";
wenzelm@28477
   187
      map (fn entry => cancelling_info entry) (! cancelling))
wenzelm@28477
   188
    val _ = Mutex.unlock lock
wenzelm@28477
   189
    in () end;
wenzelm@28477
   190
wenzelm@28484
   191
wenzelm@28484
   192
    (* preferences *)
wenzelm@28477
   193
wenzelm@28477
   194
    fun set_max_atp number = CRITICAL (fn () => maximum_atps := number);
wenzelm@28477
   195
    fun set_atps str = CRITICAL (fn () => atps := str);
wenzelm@28477
   196
    fun set_timeout time = CRITICAL (fn () => timeout := time);
wenzelm@28477
   197
    fun set_groupkilling boolean = CRITICAL (fn () => groupkilling := boolean);
wenzelm@28477
   198
wenzelm@28477
   199
    val _ = ProofGeneralPgip.add_preference "Proof"
wenzelm@28484
   200
        {name = "ATP - Provers (see print_atps)",
wenzelm@28477
   201
         descr = "Which external automatic provers (seperated by commas)",
wenzelm@28477
   202
         default = !atps,
wenzelm@28477
   203
         pgiptype = PgipTypes.Pgipstring,
wenzelm@28477
   204
         get = fn () => !atps,
wenzelm@28484
   205
         set = set_atps}
wenzelm@28484
   206
        handle Error => warning "Preference already exists";
wenzelm@28484
   207
wenzelm@28477
   208
    val _ = ProofGeneralPgip.add_preference "Proof"
wenzelm@28477
   209
        {name = "ATP - Maximum number",
wenzelm@28484
   210
         descr = "How many provers may run in parallel",
wenzelm@28477
   211
         default = Int.toString (! maximum_atps),
wenzelm@28477
   212
         pgiptype = PgipTypes.Pgipstring,
wenzelm@28477
   213
         get = fn () => Int.toString (! maximum_atps),
wenzelm@28484
   214
         set = fn str => set_max_atp (the_default 1 (Int.fromString str))}
wenzelm@28484
   215
        handle Error => warning "Preference already exists";
wenzelm@28484
   216
wenzelm@28477
   217
    val _ = ProofGeneralPgip.add_preference "Proof"
wenzelm@28477
   218
        {name = "ATP - Timeout",
wenzelm@28477
   219
         descr = "ATPs will be interrupted after this time (in seconds)",
wenzelm@28477
   220
         default = Int.toString (! timeout),
wenzelm@28477
   221
         pgiptype = PgipTypes.Pgipstring,
wenzelm@28477
   222
         get = fn () => Int.toString (! timeout),
wenzelm@28484
   223
         set = fn str => set_timeout (the_default 60 (Int.fromString str))}
wenzelm@28484
   224
        handle Error => warning "Preference already exists";
wenzelm@28484
   225
wenzelm@28477
   226
wenzelm@28484
   227
  (* named provers *)
wenzelm@28484
   228
wenzelm@28484
   229
  fun err_dup_prover name = error ("Duplicate prover: " ^ quote name);
wenzelm@28484
   230
wenzelm@28477
   231
  structure Provers = TheoryDataFun
wenzelm@28477
   232
  (
wenzelm@28484
   233
    type T = ((Proof.state -> Thread.thread * string) * stamp) Symtab.table
wenzelm@28477
   234
    val empty = Symtab.empty
wenzelm@28477
   235
    val copy = I
wenzelm@28477
   236
    val extend = I
wenzelm@28484
   237
    fun merge _ tabs : T = Symtab.merge (eq_snd op =) tabs
wenzelm@28484
   238
      handle Symtab.DUP dup => err_dup_prover dup;
wenzelm@28477
   239
  );
wenzelm@28477
   240
wenzelm@28484
   241
  fun add_prover name prover_fn =
wenzelm@28484
   242
    Provers.map (Symtab.update_new (name, (prover_fn, stamp ())))
wenzelm@28484
   243
      handle Symtab.DUP dup => err_dup_prover dup;
wenzelm@28484
   244
wenzelm@28484
   245
  fun print_provers thy = Pretty.writeln
wenzelm@28484
   246
    (Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy))));
wenzelm@28484
   247
wenzelm@28484
   248
  fun run_prover state name =
wenzelm@28484
   249
    (case Symtab.lookup (Provers.get (Proof.theory_of state)) name of
wenzelm@28484
   250
      NONE => (warning ("Unknown external prover: " ^ quote name); NONE)
wenzelm@28484
   251
    | SOME (prover_fn, _) => SOME (prover_fn state));
wenzelm@28484
   252
wenzelm@28484
   253
wenzelm@28484
   254
  (* sledghammer *)
wenzelm@28484
   255
wenzelm@28477
   256
  fun sledgehammer state =
wenzelm@28477
   257
    let
wenzelm@28484
   258
      val proverids = String.tokens (fn c => c = #",") (! atps)
wenzelm@28484
   259
      val threads_names = map_filter (run_prover state) proverids
wenzelm@28484
   260
      val birthtime = Time.now()
wenzelm@28484
   261
      val deadtime = Time.+ (Time.now(), Time.fromSeconds (! timeout))
wenzelm@28484
   262
      val _ = List.app (register birthtime deadtime) threads_names
wenzelm@28477
   263
    in () end
wenzelm@28477
   264
wenzelm@28484
   265
wenzelm@28484
   266
  (* concrete syntax *)
wenzelm@28484
   267
wenzelm@28484
   268
  local structure K = OuterKeyword and P = OuterParse in
wenzelm@28484
   269
wenzelm@28477
   270
  val _ =
wenzelm@28484
   271
    OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
wenzelm@28484
   272
      (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill_all));
wenzelm@28484
   273
wenzelm@28484
   274
  val _ =
wenzelm@28484
   275
    OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag
wenzelm@28484
   276
      (Scan.succeed (Toplevel.no_timing o Toplevel.imperative info));
wenzelm@28484
   277
wenzelm@28484
   278
  val _ =
wenzelm@28484
   279
    OuterSyntax.improper_command "print_atps" "print external provers" K.diag
wenzelm@28484
   280
      (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
wenzelm@28484
   281
        Toplevel.keep (print_provers o Toplevel.theory_of)));
wenzelm@28484
   282
wenzelm@28484
   283
  val _ =
wenzelm@28484
   284
    OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag
wenzelm@28484
   285
      (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_proof o
wenzelm@28484
   286
        Toplevel.keep (sledgehammer o Toplevel.proof_of)));
wenzelm@28484
   287
wenzelm@28484
   288
  end;
wenzelm@28484
   289
wenzelm@28477
   290
end;