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