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