src/HOL/Tools/ATP_Manager/atp_wrapper.ML
author blanchet
Wed Apr 21 16:38:03 2010 +0200 (2010-04-21)
changeset 36282 9a7c5b86a105
parent 36281 dbbf4d5d584d
child 36283 25e69e93954d
permissions -rw-r--r--
generate command-line in addition to timestamp in ATP output file, for debugging purposes
wenzelm@32327
     1
(*  Title:      HOL/Tools/ATP_Manager/atp_wrapper.ML
wenzelm@28592
     2
    Author:     Fabian Immler, TU Muenchen
wenzelm@28592
     3
wenzelm@28592
     4
Wrapper functions for external ATPs.
wenzelm@28592
     5
*)
wenzelm@28592
     6
wenzelm@28592
     7
signature ATP_WRAPPER =
wenzelm@28592
     8
sig
blanchet@35867
     9
  type prover = ATP_Manager.prover
boehmes@32864
    10
blanchet@35867
    11
  (* hooks for problem files *)
blanchet@35867
    12
  val destdir : string Config.T
blanchet@35867
    13
  val problem_prefix : string Config.T
blanchet@35867
    14
  val measure_runtime : bool Config.T
boehmes@32864
    15
blanchet@35867
    16
  val refresh_systems_on_tptp : unit -> unit
blanchet@35867
    17
  val setup : theory -> theory
wenzelm@28592
    18
end;
wenzelm@28592
    19
blanchet@35865
    20
structure ATP_Wrapper : ATP_WRAPPER =
wenzelm@28592
    21
struct
wenzelm@28596
    22
blanchet@36187
    23
open Sledgehammer_Util
blanchet@35969
    24
open Sledgehammer_Fact_Preprocessor
blanchet@35865
    25
open Sledgehammer_HOL_Clause
blanchet@35865
    26
open Sledgehammer_Fact_Filter
blanchet@35865
    27
open Sledgehammer_Proof_Reconstruct
blanchet@35867
    28
open ATP_Manager
blanchet@35826
    29
wenzelm@28596
    30
(** generic ATP wrapper **)
wenzelm@28596
    31
wenzelm@32944
    32
(* external problem files *)
boehmes@32864
    33
wenzelm@36001
    34
val (destdir, destdir_setup) = Attrib.config_string "atp_destdir" (K "");
boehmes@32864
    35
  (*Empty string means create files in Isabelle's temporary files directory.*)
boehmes@32864
    36
boehmes@32864
    37
val (problem_prefix, problem_prefix_setup) =
wenzelm@36001
    38
  Attrib.config_string "atp_problem_prefix" (K "prob");
boehmes@32864
    39
boehmes@33247
    40
val (measure_runtime, measure_runtime_setup) =
wenzelm@36001
    41
  Attrib.config_bool "atp_measure_runtime" (K false);
boehmes@33247
    42
boehmes@32864
    43
blanchet@35867
    44
(* prover configuration *)
boehmes@32864
    45
wenzelm@32941
    46
type prover_config =
wenzelm@32941
    47
 {command: Path.T,
blanchet@35969
    48
  arguments: Time.time -> string,
blanchet@36265
    49
  known_failures: (string list * string) list,
boehmes@32864
    50
  max_new_clauses: int,
blanchet@36220
    51
  prefers_theory_relevant: bool,
blanchet@35969
    52
  supports_isar_proofs: bool};
wenzelm@28596
    53
wenzelm@28596
    54
wenzelm@28596
    55
(* basic template *)
wenzelm@28596
    56
boehmes@32458
    57
fun with_path cleanup after f path =
boehmes@32458
    58
  Exn.capture f path
boehmes@32458
    59
  |> tap (fn _ => cleanup path)
boehmes@32458
    60
  |> Exn.release
wenzelm@32941
    61
  |> tap (after path);
boehmes@32458
    62
blanchet@36265
    63
fun find_known_failure known_failures proof =
blanchet@36265
    64
  case map_filter (fn (patterns, message) =>
blanchet@36265
    65
                      if exists (fn pattern => String.isSubstring pattern proof)
blanchet@36265
    66
                                patterns then
blanchet@36265
    67
                        SOME message
blanchet@36265
    68
                      else
blanchet@36265
    69
                        NONE) known_failures of
blanchet@36265
    70
    [] => if is_proof_well_formed proof then ""
blanchet@36265
    71
          else "Error: The ATP output is ill-formed."
blanchet@36265
    72
  | (message :: _) => message
blanchet@35865
    73
blanchet@36265
    74
fun generic_prover overlord get_facts prepare write_file cmd args known_failures
blanchet@36235
    75
        proof_text name ({debug, full_types, explicit_apply, ...} : params)
blanchet@36281
    76
        minimize_command
blanchet@35969
    77
        ({subgoal, goal, relevance_override, axiom_clauses, filtered_clauses}
blanchet@35969
    78
         : problem) =
wenzelm@28596
    79
  let
immler@31750
    80
    (* get clauses and prepare them for writing *)
wenzelm@32942
    81
    val (ctxt, (chain_ths, th)) = goal;
wenzelm@32942
    82
    val thy = ProofContext.theory_of ctxt;
blanchet@35865
    83
    val chain_ths = map (Thm.put_name_hint chained_hint) chain_ths;
blanchet@35969
    84
    val goal_cls = #1 (neg_conjecture_clauses ctxt th subgoal);
immler@31752
    85
    val the_filtered_clauses =
wenzelm@32942
    86
      (case filtered_clauses of
blanchet@35969
    87
        NONE => get_facts relevance_override goal goal_cls
wenzelm@32942
    88
      | SOME fcls => fcls);
immler@31409
    89
    val the_axiom_clauses =
wenzelm@32942
    90
      (case axiom_clauses of
wenzelm@32942
    91
        NONE => the_filtered_clauses
wenzelm@32942
    92
      | SOME axcls => axcls);
blanchet@35969
    93
    val (internal_thm_names, clauses) =
wenzelm@32942
    94
      prepare goal_cls chain_ths the_axiom_clauses the_filtered_clauses thy;
immler@31750
    95
boehmes@32864
    96
    (* path to unique problem file *)
blanchet@36143
    97
    val destdir' = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@36143
    98
                   else Config.get ctxt destdir;
wenzelm@32942
    99
    val problem_prefix' = Config.get ctxt problem_prefix;
boehmes@32864
   100
    fun prob_pathname nr =
blanchet@36143
   101
      let
blanchet@36143
   102
        val probfile =
blanchet@36143
   103
          Path.basic (problem_prefix' ^
blanchet@36143
   104
                      (if overlord then "_" ^ name else serial_string ())
blanchet@36143
   105
                      ^ "_" ^ string_of_int nr)
wenzelm@32942
   106
      in
wenzelm@32942
   107
        if destdir' = "" then File.tmp_path probfile
wenzelm@35570
   108
        else if File.exists (Path.explode destdir')
blanchet@36281
   109
        then Path.append (Path.explode destdir') probfile
blanchet@36169
   110
        else error ("No such directory: " ^ destdir' ^ ".")
wenzelm@32942
   111
      end;
boehmes@32864
   112
immler@31750
   113
    (* write out problem file and call prover *)
boehmes@33247
   114
    fun cmd_line probfile =
blanchet@36265
   115
      if Config.get ctxt measure_runtime then
boehmes@33247
   116
        "TIMEFORMAT='%3U'; { time " ^ space_implode " " [File.shell_path cmd,
blanchet@36265
   117
        args, File.shell_path probfile] ^ " ; } 2>&1"
boehmes@33247
   118
      else
boehmes@33247
   119
        space_implode " " ["exec", File.shell_path cmd, args,
blanchet@36265
   120
        File.shell_path probfile, "2>&1"];
boehmes@32510
   121
    fun split_time s =
boehmes@32510
   122
      let
wenzelm@32942
   123
        val split = String.tokens (fn c => str c = "\n");
wenzelm@32942
   124
        val (proof, t) = s |> split |> split_last |> apfst cat_lines;
wenzelm@32942
   125
        fun as_num f = f >> (fst o read_int);
wenzelm@32942
   126
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
wenzelm@32942
   127
        val digit = Scan.one Symbol.is_ascii_digit;
wenzelm@32942
   128
        val num3 = as_num (digit ::: digit ::: (digit >> single));
wenzelm@32942
   129
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
wenzelm@32942
   130
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
wenzelm@32942
   131
      in (proof, as_time t) end;
boehmes@33247
   132
    fun split_time' s =
boehmes@33247
   133
      if Config.get ctxt measure_runtime then split_time s else (s, 0)
boehmes@32458
   134
    fun run_on probfile =
wenzelm@32942
   135
      if File.exists cmd then
blanchet@36235
   136
        write_file full_types explicit_apply probfile clauses
wenzelm@35010
   137
        |> pair (apfst split_time' (bash_output (cmd_line probfile)))
blanchet@36167
   138
      else error ("Bad executable: " ^ Path.implode cmd ^ ".");
wenzelm@28592
   139
blanchet@36167
   140
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@36167
   141
       the proof file too. *)
wenzelm@32942
   142
    fun cleanup probfile = if destdir' = "" then try File.rm probfile else NONE;
wenzelm@32942
   143
    fun export probfile (((proof, _), _), _) =
blanchet@36187
   144
      if destdir' = "" then
blanchet@36187
   145
        ()
blanchet@36187
   146
      else
blanchet@36187
   147
        File.write (Path.explode (Path.implode probfile ^ "_proof"))
blanchet@36282
   148
                   ((if overlord then
blanchet@36282
   149
                       "% " ^ cmd_line probfile ^ "\n% " ^ timestamp () ^ "\n"
blanchet@36282
   150
                     else
blanchet@36282
   151
                        "") ^ proof)
wenzelm@32257
   152
blanchet@36231
   153
    val (((proof, atp_run_time_in_msecs), rc), _) =
wenzelm@35570
   154
      with_path cleanup export run_on (prob_pathname subgoal);
boehmes@32458
   155
blanchet@36167
   156
    (* Check for success and print out some information on failure. *)
blanchet@36265
   157
    val failure = find_known_failure known_failures proof;
blanchet@36265
   158
    val success = rc = 0 andalso failure = "";
blanchet@35969
   159
    val (message, relevant_thm_names) =
blanchet@36265
   160
      if success then
blanchet@36281
   161
        proof_text ctxt minimize_command proof internal_thm_names th subgoal
blanchet@36265
   162
      else if failure <> "" then
blanchet@36265
   163
        (failure, [])
blanchet@36265
   164
      else
blanchet@36265
   165
        ("Unknown ATP error: " ^ proof ^ ".\n", [])
boehmes@32864
   166
  in
blanchet@36231
   167
    {success = success, message = message,
blanchet@35969
   168
     relevant_thm_names = relevant_thm_names,
blanchet@35969
   169
     atp_run_time_in_msecs = atp_run_time_in_msecs, proof = proof,
blanchet@35969
   170
     internal_thm_names = internal_thm_names,
blanchet@35969
   171
     filtered_clauses = the_filtered_clauses}
wenzelm@32942
   172
  end;
wenzelm@28592
   173
wenzelm@28596
   174
wenzelm@28596
   175
(* generic TPTP-based provers *)
wenzelm@28596
   176
blanchet@35865
   177
fun generic_tptp_prover
blanchet@36265
   178
        (name, {command, arguments, known_failures, max_new_clauses,
blanchet@36220
   179
                prefers_theory_relevant, supports_isar_proofs})
blanchet@36264
   180
        (params as {debug, overlord, respect_no_atp, relevance_threshold,
blanchet@36264
   181
                    convergence, theory_relevant, higher_order, follow_defs,
blanchet@36264
   182
                    isar_proof, modulus, sorts, ...})
blanchet@36281
   183
        minimize_command timeout =
blanchet@36143
   184
  generic_prover overlord
blanchet@36058
   185
      (get_relevant_facts respect_no_atp relevance_threshold convergence
blanchet@36058
   186
                          higher_order follow_defs max_new_clauses
blanchet@36220
   187
                          (the_default prefers_theory_relevant theory_relevant))
blanchet@36222
   188
      (prepare_clauses higher_order false)
blanchet@36264
   189
      (write_tptp_file (debug andalso overlord andalso not isar_proof)) command
blanchet@36265
   190
      (arguments timeout) known_failures
blanchet@36281
   191
      (proof_text (supports_isar_proofs andalso isar_proof) modulus sorts)
blanchet@36281
   192
      name params minimize_command
wenzelm@28596
   193
blanchet@35969
   194
fun tptp_prover name p = (name, generic_tptp_prover (name, p));
wenzelm@28596
   195
wenzelm@32941
   196
boehmes@32864
   197
(** common provers **)
wenzelm@28592
   198
blanchet@36142
   199
fun generous_to_secs time = (Time.toMilliseconds time + 999) div 1000
blanchet@36142
   200
wenzelm@28596
   201
(* Vampire *)
wenzelm@28596
   202
blanchet@35969
   203
(* NB: Vampire does not work without explicit time limit. *)
wenzelm@28596
   204
blanchet@35969
   205
val vampire_config : prover_config =
blanchet@35969
   206
  {command = Path.explode "$VAMPIRE_HOME/vampire",
blanchet@35969
   207
   arguments = (fn timeout => "--output_syntax tptp --mode casc -t " ^
blanchet@36142
   208
                              string_of_int (generous_to_secs timeout)),
blanchet@36265
   209
   known_failures =
blanchet@36265
   210
     [(["Satisfiability detected", "CANNOT PROVE"],
blanchet@36265
   211
       "The ATP problem is unprovable."),
blanchet@36265
   212
      (["Refutation not found"],
blanchet@36265
   213
       "The ATP failed to determine the problem's status.")],
blanchet@35969
   214
   max_new_clauses = 60,
blanchet@36220
   215
   prefers_theory_relevant = false,
blanchet@35969
   216
   supports_isar_proofs = true}
blanchet@35969
   217
val vampire = tptp_prover "vampire" vampire_config
wenzelm@28596
   218
wenzelm@28592
   219
wenzelm@28596
   220
(* E prover *)
wenzelm@28596
   221
blanchet@35969
   222
val e_config : prover_config =
blanchet@35969
   223
  {command = Path.explode "$E_HOME/eproof",
blanchet@35969
   224
   arguments = (fn timeout => "--tstp-in --tstp-out -l5 -xAutoDev \
blanchet@35969
   225
                              \-tAutoDev --silent --cpu-limit=" ^
blanchet@36142
   226
                              string_of_int (generous_to_secs timeout)),
blanchet@36265
   227
   known_failures =
blanchet@36265
   228
       [(["SZS status: Satisfiable", "SZS status Satisfiable"],
blanchet@36265
   229
         "The ATP problem is unprovable."),
blanchet@36265
   230
        (["SZS status: ResourceOut", "SZS status ResourceOut"],
blanchet@36265
   231
         "The ATP ran out of resources."),
blanchet@36265
   232
        (["# Cannot determine problem status"],
blanchet@36265
   233
         "The ATP failed to determine the problem's status.")],
blanchet@35969
   234
   max_new_clauses = 100,
blanchet@36220
   235
   prefers_theory_relevant = false,
blanchet@35969
   236
   supports_isar_proofs = true}
blanchet@35969
   237
val e = tptp_prover "e" e_config
wenzelm@28596
   238
wenzelm@28596
   239
wenzelm@28596
   240
(* SPASS *)
wenzelm@28592
   241
blanchet@35865
   242
fun generic_dfg_prover
blanchet@36265
   243
        (name, ({command, arguments, known_failures, max_new_clauses,
blanchet@36220
   244
                 prefers_theory_relevant, ...} : prover_config))
blanchet@36143
   245
        (params as {overlord, respect_no_atp, relevance_threshold, convergence,
blanchet@36220
   246
                    theory_relevant, higher_order, follow_defs, ...})
blanchet@36281
   247
        minimize_command timeout =
blanchet@36143
   248
  generic_prover overlord
blanchet@36058
   249
      (get_relevant_facts respect_no_atp relevance_threshold convergence
blanchet@36058
   250
                          higher_order follow_defs max_new_clauses
blanchet@36220
   251
                          (the_default prefers_theory_relevant theory_relevant))
blanchet@35969
   252
      (prepare_clauses higher_order true) write_dfg_file command
blanchet@36281
   253
      (arguments timeout) known_failures (K metis_proof_text)
blanchet@36281
   254
      name params minimize_command
boehmes@32869
   255
blanchet@36264
   256
fun dfg_prover name p = (name, generic_dfg_prover (name, p))
boehmes@32869
   257
blanchet@36219
   258
(* The "-VarWeight=3" option helps the higher-order problems, probably by
blanchet@36219
   259
   counteracting the presence of "hAPP". *)
blanchet@36059
   260
val spass_config : prover_config =
blanchet@35969
   261
 {command = Path.explode "$SPASS_HOME/SPASS",
blanchet@35969
   262
  arguments = (fn timeout => "-Auto -SOS=1 -PGiven=0 -PProblem=0 -Splits=0" ^
blanchet@36219
   263
    " -FullRed=0 -DocProof -VarWeight=3 -TimeLimit=" ^
blanchet@36142
   264
    string_of_int (generous_to_secs timeout)),
blanchet@36265
   265
  known_failures =
blanchet@36265
   266
    [(["SPASS beiseite: Completion found."], "The ATP problem is unprovable."),
blanchet@36265
   267
     (["SPASS beiseite: Ran out of time."], "The ATP timed out."),
blanchet@36265
   268
     (["SPASS beiseite: Maximal number of loops exceeded."],
blanchet@36265
   269
      "The ATP hit its loop limit.")],
blanchet@35969
   270
  max_new_clauses = 40,
blanchet@36220
   271
  prefers_theory_relevant = true,
blanchet@36059
   272
  supports_isar_proofs = false}
blanchet@36264
   273
val spass = dfg_prover "spass" spass_config
blanchet@36264
   274
blanchet@36264
   275
(* SPASS 3.7 supports both the DFG and the TPTP syntax, whereas SPASS 3.0
blanchet@36264
   276
   supports only the DFG syntax. As soon as all Isabelle repository/snapshot
blanchet@36264
   277
   users have upgraded to 3.7, we can kill "spass" (and all DFG support in
blanchet@36264
   278
   Sledgehammer) and rename "spass_tptp" "spass". *)
blanchet@35969
   279
blanchet@36265
   280
(* FIXME: Change the error message below to point to the Isabelle download
blanchet@36265
   281
   page once the package is there (around the Isabelle2010 release). *)
blanchet@36265
   282
blanchet@36264
   283
val spass_tptp_config =
blanchet@36264
   284
  {command = #command spass_config,
blanchet@36264
   285
   arguments = prefix "-TPTP " o #arguments spass_config,
blanchet@36265
   286
   known_failures =
blanchet@36265
   287
     #known_failures spass_config @
blanchet@36265
   288
     [(["unrecognized option `-TPTP'", "Unrecognized option TPTP"],
blanchet@36265
   289
       "Warning: Sledgehammer requires a more recent version of SPASS with \
blanchet@36265
   290
       \support for the TPTP syntax. To install it, download and untar the \
blanchet@36265
   291
       \package \"http://isabelle.in.tum.de/~blanchet/spass-3.7.tgz\" and add \
blanchet@36265
   292
       \the \"spass-3.7\" directory's full path to \"" ^
blanchet@36265
   293
       Path.implode (Path.expand (Path.appends
blanchet@36265
   294
           (Path.variable "ISABELLE_HOME_USER" ::
blanchet@36265
   295
            map Path.basic ["etc", "components"]))) ^
blanchet@36265
   296
       "\" on a line of its own.")],
blanchet@36264
   297
   max_new_clauses = #max_new_clauses spass_config,
blanchet@36264
   298
   prefers_theory_relevant = #prefers_theory_relevant spass_config,
blanchet@36264
   299
   supports_isar_proofs = #supports_isar_proofs spass_config}
blanchet@36264
   300
val spass_tptp = tptp_prover "spass_tptp" spass_tptp_config
wenzelm@28596
   301
wenzelm@28596
   302
(* remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   303
wenzelm@32942
   304
val systems = Synchronized.var "atp_wrapper_systems" ([]: string list);
immler@31835
   305
immler@31835
   306
fun get_systems () =
immler@31835
   307
  let
blanchet@35969
   308
    val (answer, rc) = bash_output "\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w"
immler@31835
   309
  in
blanchet@35969
   310
    if rc <> 0 then
blanchet@35969
   311
      error ("Failed to get available systems at SystemOnTPTP:\n" ^ answer)
blanchet@35969
   312
    else
blanchet@35969
   313
      split_lines answer
immler@31835
   314
  end;
immler@31835
   315
blanchet@35867
   316
fun refresh_systems_on_tptp () =
blanchet@35867
   317
  Synchronized.change systems (fn _ => get_systems ());
immler@31835
   318
immler@31835
   319
fun get_system prefix = Synchronized.change_result systems (fn systems =>
boehmes@32864
   320
  (if null systems then get_systems () else systems)
wenzelm@32942
   321
  |> `(find_first (String.isPrefix prefix)));
boehmes@32864
   322
wenzelm@32948
   323
fun the_system prefix =
boehmes@32864
   324
  (case get_system prefix of
blanchet@35826
   325
    NONE => error ("System " ^ quote prefix ^ " not available at SystemOnTPTP")
wenzelm@32942
   326
  | SOME sys => sys);
immler@31835
   327
blanchet@36265
   328
val remote_known_failures =
blanchet@36265
   329
  [(["Remote-script could not extract proof"],
blanchet@36265
   330
    "Error: The remote ATP proof is ill-formed.")]
blanchet@35865
   331
blanchet@36059
   332
fun remote_prover_config prover_prefix args
blanchet@36265
   333
        ({known_failures, max_new_clauses, prefers_theory_relevant, ...}
blanchet@36059
   334
         : prover_config) : prover_config =
blanchet@35969
   335
  {command = Path.explode "$ISABELLE_ATP_MANAGER/SystemOnTPTP",
blanchet@35969
   336
   arguments = (fn timeout =>
blanchet@36142
   337
     args ^ " -t " ^ string_of_int (generous_to_secs timeout) ^ " -s " ^
blanchet@35969
   338
     the_system prover_prefix),
blanchet@36265
   339
   known_failures = remote_known_failures @ known_failures,
blanchet@35969
   340
   max_new_clauses = max_new_clauses,
blanchet@36220
   341
   prefers_theory_relevant = prefers_theory_relevant,
blanchet@35969
   342
   supports_isar_proofs = false}
wenzelm@28596
   343
blanchet@35969
   344
val remote_vampire =
blanchet@35969
   345
  tptp_prover "remote_vampire"
blanchet@36059
   346
              (remote_prover_config "Vampire---9" "" vampire_config)
boehmes@32864
   347
blanchet@35969
   348
val remote_e =
blanchet@36059
   349
  tptp_prover "remote_e" (remote_prover_config "EP---" "" e_config)
boehmes@32864
   350
blanchet@35969
   351
val remote_spass =
blanchet@36059
   352
  tptp_prover "remote_spass" (remote_prover_config "SPASS---" "-x" spass_config)
wenzelm@28592
   353
blanchet@36264
   354
val provers = [spass, spass_tptp, vampire, e, remote_vampire, remote_spass,
blanchet@36264
   355
               remote_e]
blanchet@35867
   356
val prover_setup = fold add_prover provers
blanchet@35867
   357
blanchet@35867
   358
val setup =
blanchet@35867
   359
  destdir_setup
blanchet@35867
   360
  #> problem_prefix_setup
blanchet@35867
   361
  #> measure_runtime_setup
blanchet@35867
   362
  #> prover_setup;
blanchet@35867
   363
wenzelm@28592
   364
end;