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