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