src/HOL/Tools/ATP_Manager/atp_manager.ML
author blanchet
Thu Apr 15 13:49:46 2010 +0200 (2010-04-15)
changeset 36167 c1a35be8e476
parent 36143 6490319b1703
child 36181 2156a7392885
permissions -rw-r--r--
make Sledgehammer's output more debugging friendly
     1 (*  Title:      HOL/Tools/ATP_Manager/atp_manager.ML
     2     Author:     Fabian Immler, TU Muenchen
     3     Author:     Makarius
     4     Author:     Jasmin Blanchette, TU Muenchen
     5 
     6 Central manager component for ATP threads.
     7 *)
     8 
     9 signature ATP_MANAGER =
    10 sig
    11   type relevance_override = Sledgehammer_Fact_Filter.relevance_override
    12   type params =
    13     {debug: bool,
    14      verbose: bool,
    15      overlord: bool,
    16      atps: string list,
    17      full_types: bool,
    18      respect_no_atp: bool,
    19      relevance_threshold: real,
    20      convergence: real,
    21      theory_const: bool option,
    22      higher_order: bool option,
    23      follow_defs: bool,
    24      isar_proof: bool,
    25      modulus: int,
    26      sorts: bool,
    27      timeout: Time.time,
    28      minimize_timeout: Time.time}
    29   type problem =
    30     {subgoal: int,
    31      goal: Proof.context * (thm list * thm),
    32      relevance_override: relevance_override,
    33      axiom_clauses: (thm * (string * int)) list option,
    34      filtered_clauses: (thm * (string * int)) list option}
    35   type prover_result =
    36     {success: bool,
    37      message: string,
    38      relevant_thm_names: string list,
    39      atp_run_time_in_msecs: int,
    40      proof: string,
    41      internal_thm_names: string Vector.vector,
    42      filtered_clauses: (thm * (string * int)) list}
    43   type prover = params -> Time.time -> problem -> prover_result
    44 
    45   val atps: string Unsynchronized.ref
    46   val timeout: int Unsynchronized.ref
    47   val full_types: bool Unsynchronized.ref
    48   val kill_atps: unit -> unit
    49   val running_atps: unit -> unit
    50   val messages: int option -> unit
    51   val add_prover: string * prover -> theory -> theory
    52   val get_prover: theory -> string -> prover option
    53   val available_atps: theory -> unit
    54   val sledgehammer: params -> int -> relevance_override -> Proof.state -> unit
    55 end;
    56 
    57 structure ATP_Manager : ATP_MANAGER =
    58 struct
    59 
    60 open Sledgehammer_Fact_Filter
    61 open Sledgehammer_Proof_Reconstruct
    62 
    63 (** parameters, problems, results, and provers **)
    64 
    65 type params =
    66   {debug: bool,
    67    verbose: bool,
    68    overlord: bool,
    69    atps: string list,
    70    full_types: bool,
    71    respect_no_atp: bool,
    72    relevance_threshold: real,
    73    convergence: real,
    74    theory_const: bool option,
    75    higher_order: bool option,
    76    follow_defs: bool,
    77    isar_proof: bool,
    78    modulus: int,
    79    sorts: bool,
    80    timeout: Time.time,
    81    minimize_timeout: Time.time}
    82 
    83 type problem =
    84   {subgoal: int,
    85    goal: Proof.context * (thm list * thm),
    86    relevance_override: relevance_override,
    87    axiom_clauses: (thm * (string * int)) list option,
    88    filtered_clauses: (thm * (string * int)) list option};
    89 
    90 type prover_result =
    91   {success: bool,
    92    message: string,
    93    relevant_thm_names: string list,
    94    atp_run_time_in_msecs: int,
    95    proof: string,
    96    internal_thm_names: string Vector.vector,
    97    filtered_clauses: (thm * (string * int)) list};
    98 
    99 type prover = params -> Time.time -> problem -> prover_result;
   100 
   101 
   102 (** preferences **)
   103 
   104 val message_store_limit = 20;
   105 val message_display_limit = 5;
   106 
   107 val atps = Unsynchronized.ref "e spass remote_vampire";
   108 val timeout = Unsynchronized.ref 60;
   109 val full_types = Unsynchronized.ref false;
   110 
   111 val _ =
   112   ProofGeneralPgip.add_preference Preferences.category_proof
   113     (Preferences.string_pref atps
   114       "ATP: provers" "Default automatic provers (separated by whitespace)");
   115 
   116 val _ =
   117   ProofGeneralPgip.add_preference Preferences.category_proof
   118     (Preferences.int_pref timeout
   119       "ATP: timeout" "ATPs will be interrupted after this time (in seconds)");
   120 
   121 val _ =
   122   ProofGeneralPgip.add_preference Preferences.category_proof
   123     (Preferences.bool_pref full_types
   124       "ATP: full types" "ATPs will use full type information");
   125 
   126 
   127 
   128 (** thread management **)
   129 
   130 (* data structures over threads *)
   131 
   132 structure Thread_Heap = Heap
   133 (
   134   type elem = Time.time * Thread.thread;
   135   fun ord ((a, _), (b, _)) = Time.compare (a, b);
   136 );
   137 
   138 fun lookup_thread xs = AList.lookup Thread.equal xs;
   139 fun delete_thread xs = AList.delete Thread.equal xs;
   140 fun update_thread xs = AList.update Thread.equal xs;
   141 
   142 
   143 (* state of thread manager *)
   144 
   145 type state =
   146  {manager: Thread.thread option,
   147   timeout_heap: Thread_Heap.T,
   148   active: (Thread.thread * (Time.time * Time.time * string)) list,
   149   cancelling: (Thread.thread * (Time.time * string)) list,
   150   messages: string list,
   151   store: string list};
   152 
   153 fun make_state manager timeout_heap active cancelling messages store : state =
   154   {manager = manager, timeout_heap = timeout_heap, active = active,
   155     cancelling = cancelling, messages = messages, store = store};
   156 
   157 val global_state = Synchronized.var "atp_manager"
   158   (make_state NONE Thread_Heap.empty [] [] [] []);
   159 
   160 
   161 (* unregister ATP thread *)
   162 
   163 fun unregister message thread = Synchronized.change global_state
   164   (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
   165     (case lookup_thread active thread of
   166       SOME (_, _, description) =>
   167         let
   168           val active' = delete_thread thread active;
   169           val cancelling' = (thread, (Time.now (), description)) :: cancelling;
   170           val message' = description ^ "\n" ^ message;
   171           val messages' = message' :: messages;
   172           val store' = message' ::
   173             (if length store <= message_store_limit then store
   174              else #1 (chop message_store_limit store));
   175         in make_state manager timeout_heap active' cancelling' messages' store' end
   176     | NONE => state));
   177 
   178 
   179 (* main manager thread -- only one may exist *)
   180 
   181 val min_wait_time = Time.fromMilliseconds 300;
   182 val max_wait_time = Time.fromSeconds 10;
   183 
   184 fun print_new_messages () =
   185   let val msgs = Synchronized.change_result global_state
   186     (fn {manager, timeout_heap, active, cancelling, messages, store} =>
   187       (messages, make_state manager timeout_heap active cancelling [] store))
   188   in
   189     if null msgs then ()
   190     else priority ("Sledgehammer: " ^ space_implode "\n\n" msgs)
   191   end;
   192 
   193 fun check_thread_manager () = Synchronized.change global_state
   194   (fn state as {manager, timeout_heap, active, cancelling, messages, store} =>
   195     if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
   196     else let val manager = SOME (Toplevel.thread false (fn () =>
   197       let
   198         fun time_limit timeout_heap =
   199           (case try Thread_Heap.min timeout_heap of
   200             NONE => Time.+ (Time.now (), max_wait_time)
   201           | SOME (time, _) => time);
   202 
   203         (*action: find threads whose timeout is reached, and interrupt cancelling threads*)
   204         fun action {manager, timeout_heap, active, cancelling, messages, store} =
   205           let val (timeout_threads, timeout_heap') =
   206             Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
   207           in
   208             if null timeout_threads andalso null cancelling
   209             then NONE
   210             else
   211               let
   212                 val _ = List.app (SimpleThread.interrupt o #1) cancelling;
   213                 val cancelling' = filter (Thread.isActive o #1) cancelling;
   214                 val state' = make_state manager timeout_heap' active cancelling' messages store;
   215               in SOME (map #2 timeout_threads, state') end
   216           end;
   217       in
   218         while Synchronized.change_result global_state
   219           (fn state as {timeout_heap, active, cancelling, messages, store, ...} =>
   220             if null active andalso null cancelling andalso null messages
   221             then (false, make_state NONE timeout_heap active cancelling messages store)
   222             else (true, state))
   223         do
   224           (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
   225             |> these
   226             |> List.app (unregister "Timed out.");
   227             print_new_messages ();
   228             (*give threads some time to respond to interrupt*)
   229             OS.Process.sleep min_wait_time)
   230       end))
   231     in make_state manager timeout_heap active cancelling messages store end);
   232 
   233 
   234 (* register ATP thread *)
   235 
   236 fun register birth_time death_time (thread, desc) =
   237  (Synchronized.change global_state
   238     (fn {manager, timeout_heap, active, cancelling, messages, store} =>
   239       let
   240         val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
   241         val active' = update_thread (thread, (birth_time, death_time, desc)) active;
   242         val state' = make_state manager timeout_heap' active' cancelling messages store;
   243       in state' end);
   244   check_thread_manager ());
   245 
   246 
   247 
   248 (** user commands **)
   249 
   250 (* kill ATPs *)
   251 
   252 fun kill_atps () = Synchronized.change global_state
   253   (fn {manager, timeout_heap, active, cancelling, messages, store} =>
   254     let
   255       val killing = map (fn (th, (_, _, desc)) => (th, (Time.now (), desc))) active;
   256       val state' = make_state manager timeout_heap [] (killing @ cancelling) messages store;
   257     in state' end);
   258 
   259 
   260 (* running_atps *)
   261 
   262 fun seconds time = string_of_int (Time.toSeconds time) ^ "s";
   263 
   264 fun running_atps () =
   265   let
   266     val {active, cancelling, ...} = Synchronized.value global_state;
   267 
   268     val now = Time.now ();
   269     fun running_info (_, (birth_time, death_time, desc)) =
   270       "Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
   271         seconds (Time.- (death_time, now)) ^ " to live:\n" ^ desc;
   272     fun cancelling_info (_, (deadth_time, desc)) =
   273       "Trying to interrupt thread since " ^ seconds (Time.- (now, deadth_time)) ^ ":\n" ^ desc;
   274 
   275     val running =
   276       if null active then "No ATPs running."
   277       else space_implode "\n\n" ("Running ATPs:" :: map running_info active);
   278     val interrupting =
   279       if null cancelling then ""
   280       else
   281         space_implode "\n\n"
   282           ("Trying to interrupt the following ATPs:" :: map cancelling_info cancelling);
   283 
   284   in writeln (running ^ "\n" ^ interrupting) end;
   285 
   286 fun messages opt_limit =
   287   let
   288     val limit = the_default message_display_limit opt_limit;
   289     val {store, ...} = Synchronized.value global_state;
   290     val header =
   291       "Recent ATP messages" ^
   292         (if length store <= limit then ":" else " (" ^ string_of_int limit ^ " displayed):");
   293   in writeln (space_implode "\n\n" (header :: #1 (chop limit store))) end;
   294 
   295 
   296 
   297 (** The Sledgehammer **)
   298 
   299 (* named provers *)
   300 
   301 fun err_dup_prover name = error ("Duplicate prover: " ^ quote name);
   302 
   303 structure Provers = Theory_Data
   304 (
   305   type T = (prover * stamp) Symtab.table;
   306   val empty = Symtab.empty;
   307   val extend = I;
   308   fun merge data : T = Symtab.merge (eq_snd op =) data
   309     handle Symtab.DUP dup => err_dup_prover dup;
   310 );
   311 
   312 fun add_prover (name, prover) thy =
   313   Provers.map (Symtab.update_new (name, (prover, stamp ()))) thy
   314     handle Symtab.DUP dup => err_dup_prover dup;
   315 
   316 fun get_prover thy name =
   317   Option.map #1 (Symtab.lookup (Provers.get thy) name);
   318 
   319 fun available_atps thy = Pretty.writeln
   320   (Pretty.strs ("ATPs:" :: sort_strings (Symtab.keys (Provers.get thy))));
   321 
   322 
   323 (* start prover thread *)
   324 
   325 fun start_prover (params as {timeout, ...}) birth_time death_time i
   326                  relevance_override proof_state name =
   327   (case get_prover (Proof.theory_of proof_state) name of
   328     NONE => warning ("Unknown ATP: " ^ quote name ^ ".")
   329   | SOME prover =>
   330       let
   331         val {context = ctxt, facts, goal} = Proof.goal proof_state;
   332         val n = Logic.count_prems (prop_of goal)
   333         val desc =
   334           "ATP " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^
   335             Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i));
   336 
   337         val _ = Toplevel.thread true (fn () =>
   338           let
   339             val _ = register birth_time death_time (Thread.self (), desc);
   340             val problem =
   341               {subgoal = i, goal = (ctxt, (facts, goal)),
   342                relevance_override = relevance_override, axiom_clauses = NONE,
   343                filtered_clauses = NONE}
   344             val message = #message (prover params timeout problem)
   345               handle Sledgehammer_HOL_Clause.TRIVIAL =>
   346                   metis_line i n []
   347                 | ERROR msg => ("Error: " ^ msg);
   348             val _ = unregister message (Thread.self ());
   349           in () end);
   350       in () end);
   351 
   352 
   353 (* Sledgehammer the given subgoal *)
   354 
   355 fun sledgehammer (params as {atps, timeout, ...}) i relevance_override
   356                  proof_state =
   357   let
   358     val birth_time = Time.now ()
   359     val death_time = Time.+ (birth_time, timeout)
   360     val _ =
   361       (* RACE w.r.t. other invocations of Sledgehammer *)
   362       if null (#active (Synchronized.value global_state)) then ()
   363       else (kill_atps (); priority "Killed active Sledgehammer threads.")
   364     val _ = priority "Sledgehammering..."
   365     val _ = List.app (start_prover params birth_time death_time i
   366                                    relevance_override proof_state) atps
   367   in () end
   368 
   369 end;