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