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