src/HOL/Tools/ATP_Manager/atp_manager.ML
author wenzelm
Thu Oct 15 00:55:29 2009 +0200 (2009-10-15)
changeset 32937 34f66c9dd8a2
parent 32936 9491bec20595
child 32938 63a364ed3f8d
permissions -rw-r--r--
structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
uniform interpretation of ATP_Manager.atps via ATP_Manager.get_atps;
     1 (*  Title:      HOL/Tools/ATP_Manager/atp_manager.ML
     2     Author:     Fabian Immler, TU Muenchen
     3 
     4 ATP threads are registered here.
     5 Threads with the same birth-time are seen as one group.
     6 All threads of a group are killed when one thread of it has been successful,
     7 or after a certain time,
     8 or when the maximum number of threads exceeds; then the oldest thread is killed.
     9 *)
    10 
    11 signature ATP_MANAGER =
    12 sig
    13   val atps: string Unsynchronized.ref
    14   val get_atps: unit -> string list
    15   val max_atps: int Unsynchronized.ref
    16   val timeout: int Unsynchronized.ref
    17   val full_types: bool Unsynchronized.ref
    18   val kill: unit -> unit
    19   val info: unit -> unit
    20   val messages: int option -> unit
    21   val add_prover: string * ATP_Wrapper.prover -> theory -> theory
    22   val print_provers: theory -> unit
    23   val get_prover: string -> theory -> ATP_Wrapper.prover option
    24   val sledgehammer: string list -> Proof.state -> unit
    25 end;
    26 
    27 structure ATP_Manager: ATP_MANAGER =
    28 struct
    29 
    30 (** preferences **)
    31 
    32 val message_store_limit = 20;
    33 val message_display_limit = 5;
    34 
    35 val atps = Unsynchronized.ref "e spass remote_vampire";
    36 fun get_atps () = String.tokens (Symbol.is_ascii_blank o String.str) (! atps);
    37 
    38 val max_atps = Unsynchronized.ref 5;   (* ~1 means infinite number of atps *)
    39 val timeout = Unsynchronized.ref 60;
    40 val full_types = Unsynchronized.ref false;
    41 
    42 val _ =
    43   ProofGeneralPgip.add_preference Preferences.category_proof
    44     (Preferences.string_pref atps
    45       "ATP: provers" "Default automatic provers (separated by whitespace)");
    46 
    47 val _ =
    48   ProofGeneralPgip.add_preference Preferences.category_proof
    49     (Preferences.int_pref max_atps
    50       "ATP: maximum number" "How many provers may run in parallel");
    51 
    52 val _ =
    53   ProofGeneralPgip.add_preference Preferences.category_proof
    54     (Preferences.int_pref timeout
    55       "ATP: timeout" "ATPs will be interrupted after this time (in seconds)");
    56 
    57 val _ =
    58   ProofGeneralPgip.add_preference Preferences.category_proof
    59     (Preferences.bool_pref full_types
    60       "ATP: full types" "ATPs will use full type information");
    61 
    62 
    63 
    64 (** thread management **)
    65 
    66 (* data structures over threads *)
    67 
    68 structure ThreadHeap = HeapFun
    69 (
    70   type elem = Time.time * Thread.thread;
    71   fun ord ((a, _), (b, _)) = Time.compare (a, b);
    72 );
    73 
    74 fun lookup_thread xs = AList.lookup Thread.equal xs;
    75 fun update_thread xs = AList.update Thread.equal xs;
    76 
    77 
    78 (* state of thread manager *)
    79 
    80 datatype T = State of
    81  {managing_thread: Thread.thread option,
    82   timeout_heap: ThreadHeap.T,
    83   oldest_heap: ThreadHeap.T,
    84   active: (Thread.thread * (Time.time * Time.time * string)) list,
    85   cancelling: (Thread.thread * (Time.time * Time.time * string)) list,
    86   messages: string list,
    87   store: string list};
    88 
    89 fun make_state managing_thread timeout_heap oldest_heap active cancelling messages store =
    90   State {managing_thread = managing_thread, timeout_heap = timeout_heap, oldest_heap = oldest_heap,
    91     active = active, cancelling = cancelling, messages = messages, store = store};
    92 
    93 val state = Synchronized.var "atp_manager"
    94   (make_state NONE ThreadHeap.empty ThreadHeap.empty [] [] [] []);
    95 
    96 
    97 (* unregister thread *)
    98 
    99 fun unregister (success, message) thread = Synchronized.change state
   100   (fn state as
   101       State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
   102     (case lookup_thread active thread of
   103       SOME (birthtime, _, description) =>
   104         let
   105           val (group, active') =
   106             if success then List.partition (fn (_, (tb, _, _)) => tb = birthtime) active
   107             else List.partition (fn (th, _) => Thread.equal (th, thread)) active
   108 
   109           val now = Time.now ()
   110           val cancelling' =
   111             fold (fn (th, (tb, _, desc)) => update_thread (th, (tb, now, desc))) group cancelling
   112 
   113           val message' = description ^ "\n" ^ message ^
   114             (if length group <= 1 then ""
   115              else "\nInterrupted " ^ string_of_int (length group - 1) ^ " other group members")
   116           val store' = message' ::
   117             (if length store <= message_store_limit then store
   118              else #1 (chop message_store_limit store))
   119         in make_state
   120           managing_thread timeout_heap oldest_heap active' cancelling' (message' :: messages) store'
   121         end
   122     | NONE => state));
   123 
   124 
   125 (* kill excessive atp threads *)
   126 
   127 fun excessive_atps active =
   128   let val max = ! max_atps
   129   in length active > max andalso max > ~1 end;
   130 
   131 local
   132 
   133 fun kill_oldest () =
   134   let exception Unchanged in
   135     Synchronized.change_result state
   136       (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
   137         if ThreadHeap.is_empty oldest_heap orelse not (excessive_atps active)
   138         then raise Unchanged
   139         else
   140           let val ((_, oldest_thread), oldest_heap') = ThreadHeap.min_elem oldest_heap
   141           in (oldest_thread,
   142           make_state managing_thread timeout_heap oldest_heap' active cancelling messages store) end)
   143       |> unregister (false, "Interrupted (maximum number of ATPs exceeded)")
   144     handle Unchanged => ()
   145   end;
   146 
   147 in
   148 
   149 fun kill_excessive () =
   150   let val State {active, ...} = Synchronized.value state
   151   in if excessive_atps active then (kill_oldest (); kill_excessive ()) else () end;
   152 
   153 end;
   154 
   155 fun print_new_messages () =
   156   let val to_print = Synchronized.change_result state
   157     (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
   158       (messages, make_state managing_thread timeout_heap oldest_heap active cancelling [] store))
   159   in
   160     if null to_print then ()
   161     else priority ("Sledgehammer: " ^ space_implode "\n\n" to_print)
   162   end;
   163 
   164 
   165 (* start a watching thread -- only one may exist *)
   166 
   167 fun check_thread_manager () = Synchronized.change state
   168   (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
   169     if (case managing_thread of SOME thread => Thread.isActive thread | NONE => false)
   170     then make_state managing_thread timeout_heap oldest_heap active cancelling messages store
   171     else let val managing_thread = SOME (SimpleThread.fork false (fn () =>
   172       let
   173         val min_wait_time = Time.fromMilliseconds 300
   174         val max_wait_time = Time.fromSeconds 10
   175 
   176         (* wait for next thread to cancel, or maximum*)
   177         fun time_limit (State {timeout_heap, ...}) =
   178           (case try ThreadHeap.min timeout_heap of
   179             NONE => SOME (Time.+ (Time.now (), max_wait_time))
   180           | SOME (time, _) => SOME time)
   181 
   182         (* action: find threads whose timeout is reached, and interrupt cancelling threads *)
   183         fun action (State {managing_thread, timeout_heap, oldest_heap, active, cancelling,
   184                            messages, store}) =
   185           let val (timeout_threads, timeout_heap') =
   186             ThreadHeap.upto (Time.now (), Thread.self ()) timeout_heap
   187           in
   188             if null timeout_threads andalso null cancelling andalso not (excessive_atps active)
   189             then NONE
   190             else
   191               let
   192                 val _ = List.app (SimpleThread.interrupt o #1) cancelling
   193                 val cancelling' = filter (Thread.isActive o #1) cancelling
   194                 val state' = make_state
   195                   managing_thread timeout_heap' oldest_heap active cancelling' messages store
   196               in SOME (map #2 timeout_threads, state') end
   197           end
   198       in
   199         while Synchronized.change_result state
   200           (fn st as
   201             State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
   202             if (null active) andalso (null cancelling) andalso (null messages)
   203             then (false, make_state NONE timeout_heap oldest_heap active cancelling messages store)
   204             else (true, st))
   205         do
   206           (Synchronized.timed_access state time_limit action
   207             |> these
   208             |> List.app (unregister (false, "Interrupted (reached timeout)"));
   209             kill_excessive ();
   210             print_new_messages ();
   211             (*give threads time to respond to interrupt*)
   212             OS.Process.sleep min_wait_time)
   213       end))
   214     in make_state managing_thread timeout_heap oldest_heap active cancelling messages store end);
   215 
   216 
   217 (* thread is registered here by sledgehammer *)
   218 
   219 fun register birthtime deadtime (thread, desc) =
   220  (Synchronized.change state
   221     (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
   222       let
   223         val timeout_heap' = ThreadHeap.insert (deadtime, thread) timeout_heap
   224         val oldest_heap' = ThreadHeap.insert (birthtime, thread) oldest_heap
   225         val active' = update_thread (thread, (birthtime, deadtime, desc)) active
   226       in make_state managing_thread timeout_heap' oldest_heap' active' cancelling messages store end);
   227   check_thread_manager ());
   228 
   229 
   230 
   231 (** user commands **)
   232 
   233 (* kill: move all threads to cancelling *)
   234 
   235 fun kill () = Synchronized.change state
   236   (fn State {managing_thread, timeout_heap, oldest_heap, active, cancelling, messages, store} =>
   237     let val formerly_active = map (fn (th, (tb, _, desc)) => (th, (tb, Time.now (), desc))) active
   238     in make_state
   239       managing_thread timeout_heap oldest_heap [] (formerly_active @ cancelling) messages store
   240     end);
   241 
   242 
   243 (* ATP info *)
   244 
   245 fun info () =
   246   let
   247     val State {active, cancelling, ...} = Synchronized.value state
   248 
   249     fun running_info (_, (birth_time, dead_time, desc)) = "Running: "
   250         ^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), birth_time))
   251         ^ " s  --  "
   252         ^ (string_of_int o Time.toSeconds) (Time.- (dead_time, Time.now ()))
   253         ^ " s to live:\n" ^ desc
   254     fun cancelling_info (_, (_, dead_time, desc)) = "Trying to interrupt thread since "
   255         ^ (string_of_int o Time.toSeconds) (Time.- (Time.now (), dead_time))
   256         ^ " s:\n" ^ desc
   257 
   258     val running =
   259       if null active then "No ATPs running."
   260       else space_implode "\n\n" ("Running ATPs:" :: map running_info active)
   261     val interrupting =
   262       if null cancelling then ""
   263       else space_implode "\n\n"
   264         ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling)
   265 
   266   in writeln (running ^ "\n" ^ interrupting) end;
   267 
   268 fun messages opt_limit =
   269   let
   270     val limit = the_default message_display_limit opt_limit;
   271     val State {store = msgs, ...} = Synchronized.value state
   272     val header = "Recent ATP messages" ^
   273       (if length msgs <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
   274   in writeln (space_implode "\n\n" (header :: #1 (chop limit msgs))) end;
   275 
   276 
   277 
   278 (** The Sledgehammer **)
   279 
   280 (* named provers *)
   281 
   282 fun err_dup_prover name = error ("Duplicate prover: " ^ quote name);
   283 
   284 structure Provers = TheoryDataFun
   285 (
   286   type T = (ATP_Wrapper.prover * stamp) Symtab.table
   287   val empty = Symtab.empty
   288   val copy = I
   289   val extend = I
   290   fun merge _ tabs : T = Symtab.merge (eq_snd op =) tabs
   291     handle Symtab.DUP dup => err_dup_prover dup
   292 );
   293 
   294 fun add_prover (name, prover) thy =
   295   Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy
   296     handle Symtab.DUP dup => err_dup_prover dup;
   297 
   298 fun print_provers thy = Pretty.writeln
   299   (Pretty.strs ("external provers:" :: sort_strings (Symtab.keys (Provers.get thy))));
   300 
   301 fun get_prover name thy =
   302   (case Symtab.lookup (Provers.get thy) name of
   303     NONE => NONE
   304   | SOME (prover, _) => SOME prover);
   305 
   306 
   307 (* start prover thread *)
   308 
   309 fun start_prover name birthtime deadtime i proof_state =
   310   (case get_prover name (Proof.theory_of proof_state) of
   311     NONE => warning ("Unknown external prover: " ^ quote name)
   312   | SOME prover =>
   313       let
   314         val (ctxt, (_, goal)) = Proof.get_goal proof_state
   315         val desc =
   316           "external prover " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
   317             Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i))
   318         val _ = SimpleThread.fork true (fn () =>
   319           let
   320             val _ = register birthtime deadtime (Thread.self (), desc)
   321             val problem =
   322               ATP_Wrapper.atp_problem_of_goal (! full_types) i (Proof.get_goal proof_state)
   323             val result =
   324               let val ATP_Wrapper.Prover_Result {success, message, ...} =
   325                 prover problem (! timeout)
   326               in (success, message) end
   327               handle ResHolClause.TOO_TRIVIAL =>   (* FIXME !? *)
   328                   (true, "Empty clause: Try this command: " ^
   329                     Markup.markup Markup.sendback "apply metis")
   330                 | ERROR msg => (false, "Error: " ^ msg)
   331             val _ = unregister result (Thread.self ())
   332           in () end handle Interrupt => ())
   333       in () end);
   334 
   335 
   336 (* sledghammer for first subgoal *)
   337 
   338 fun sledgehammer names proof_state =
   339   let
   340     val provers = if null names then get_atps () else names
   341     val birthtime = Time.now ()
   342     val deadtime = Time.+ (birthtime, Time.fromSeconds (! timeout))
   343   in List.app (fn name => start_prover name birthtime deadtime 1 proof_state) provers end;
   344 
   345 
   346 
   347 (** Isar command syntax **)
   348 
   349 local structure K = OuterKeyword and P = OuterParse in
   350 
   351 val _ =
   352   OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
   353     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill));
   354 
   355 val _ =
   356   OuterSyntax.improper_command "atp_info" "print information about managed provers" K.diag
   357     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative info));
   358 
   359 val _ =
   360   OuterSyntax.improper_command "atp_messages" "print recent messages issued by managed provers" K.diag
   361     (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
   362       (fn limit => Toplevel.no_timing o Toplevel.imperative (fn () => messages limit)));
   363 
   364 val _ =
   365   OuterSyntax.improper_command "print_atps" "print external provers" K.diag
   366     (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
   367       Toplevel.keep (print_provers o Toplevel.theory_of)));
   368 
   369 val _ =
   370   OuterSyntax.command "sledgehammer" "call all automatic theorem provers" K.diag
   371     (Scan.repeat P.xname >> (fn names => Toplevel.no_timing o Toplevel.unknown_proof o
   372       Toplevel.keep (sledgehammer names o Toplevel.proof_of)));
   373 
   374 end;
   375 
   376 end;
   377