src/HOL/Tools/ATP_Manager/atp_wrapper.ML
author blanchet
Fri Mar 19 15:33:18 2010 +0100 (2010-03-19)
changeset 35867 16279c4c7a33
parent 35865 2f8fb5242799
child 35868 491a97039ce1
permissions -rw-r--r--
move all ATP setup code into ATP_Wrapper
     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_HOL_Clause
    24 open Sledgehammer_Fact_Filter
    25 open Sledgehammer_Proof_Reconstruct
    26 open ATP_Manager
    27 
    28 (** generic ATP wrapper **)
    29 
    30 (* external problem files *)
    31 
    32 val (destdir, destdir_setup) = Attrib.config_string "atp_destdir" "";
    33   (*Empty string means create files in Isabelle's temporary files directory.*)
    34 
    35 val (problem_prefix, problem_prefix_setup) =
    36   Attrib.config_string "atp_problem_prefix" "prob";
    37 
    38 val (measure_runtime, measure_runtime_setup) =
    39   Attrib.config_bool "atp_measure_runtime" false;
    40 
    41 
    42 (* prover configuration *)
    43 
    44 type prover_config =
    45  {command: Path.T,
    46   arguments: int -> string,
    47   failure_strs: string list,
    48   max_new_clauses: int,
    49   insert_theory_const: bool,
    50   emit_structured_proof: bool};
    51 
    52 
    53 (* basic template *)
    54 
    55 fun with_path cleanup after f path =
    56   Exn.capture f path
    57   |> tap (fn _ => cleanup path)
    58   |> Exn.release
    59   |> tap (after path);
    60 
    61 fun find_failure strs proof =
    62   case filter (fn s => String.isSubstring s proof) strs of
    63     [] => if is_proof_well_formed proof then NONE
    64           else SOME "Ill-formed ATP output"
    65   | (failure :: _) => SOME failure
    66 
    67 fun external_prover relevance_filter prepare write cmd args failure_strs
    68         produce_answer name ({with_full_types, subgoal, goal, axiom_clauses,
    69                               filtered_clauses}: problem) =
    70   let
    71     (* get clauses and prepare them for writing *)
    72     val (ctxt, (chain_ths, th)) = goal;
    73     val thy = ProofContext.theory_of ctxt;
    74     val chain_ths = map (Thm.put_name_hint chained_hint) chain_ths;
    75     val goal_cls = #1 (Sledgehammer_Fact_Preprocessor.neg_conjecture_clauses ctxt th subgoal);
    76     val the_filtered_clauses =
    77       (case filtered_clauses of
    78         NONE => relevance_filter goal goal_cls
    79       | SOME fcls => fcls);
    80     val the_axiom_clauses =
    81       (case axiom_clauses of
    82         NONE => the_filtered_clauses
    83       | SOME axcls => axcls);
    84     val (thm_names, clauses) =
    85       prepare goal_cls chain_ths the_axiom_clauses the_filtered_clauses thy;
    86 
    87     (* path to unique problem file *)
    88     val destdir' = Config.get ctxt destdir;
    89     val problem_prefix' = Config.get ctxt problem_prefix;
    90     fun prob_pathname nr =
    91       let val probfile =
    92         Path.basic (problem_prefix' ^ serial_string () ^ "_" ^ string_of_int nr)
    93       in
    94         if destdir' = "" then File.tmp_path probfile
    95         else if File.exists (Path.explode destdir')
    96         then Path.append  (Path.explode destdir') probfile
    97         else error ("No such directory: " ^ destdir')
    98       end;
    99 
   100     (* write out problem file and call prover *)
   101     fun cmd_line probfile =
   102       if Config.get ctxt measure_runtime
   103       then (* Warning: suppresses error messages of ATPs *)
   104         "TIMEFORMAT='%3U'; { time " ^ space_implode " " [File.shell_path cmd,
   105         args, File.shell_path probfile] ^ " 2> /dev/null" ^ " ; } 2>&1"
   106       else
   107         space_implode " " ["exec", File.shell_path cmd, args,
   108         File.shell_path probfile];
   109     fun split_time s =
   110       let
   111         val split = String.tokens (fn c => str c = "\n");
   112         val (proof, t) = s |> split |> split_last |> apfst cat_lines;
   113         fun as_num f = f >> (fst o read_int);
   114         val num = as_num (Scan.many1 Symbol.is_ascii_digit);
   115         val digit = Scan.one Symbol.is_ascii_digit;
   116         val num3 = as_num (digit ::: digit ::: (digit >> single));
   117         val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
   118         val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
   119       in (proof, as_time t) end;
   120     fun split_time' s =
   121       if Config.get ctxt measure_runtime then split_time s else (s, 0)
   122     fun run_on probfile =
   123       if File.exists cmd then
   124         write with_full_types probfile clauses
   125         |> pair (apfst split_time' (bash_output (cmd_line probfile)))
   126       else error ("Bad executable: " ^ Path.implode cmd);
   127 
   128     (* if problemfile has not been exported, delete problemfile; otherwise export proof, too *)
   129     fun cleanup probfile = if destdir' = "" then try File.rm probfile else NONE;
   130     fun export probfile (((proof, _), _), _) =
   131       if destdir' = "" then ()
   132       else File.write (Path.explode (Path.implode probfile ^ "_proof")) proof;
   133 
   134     val (((proof, time), rc), conj_pos) =
   135       with_path cleanup export run_on (prob_pathname subgoal);
   136 
   137     (* check for success and print out some information on failure *)
   138     val failure = find_failure failure_strs proof;
   139     val success = rc = 0 andalso is_none failure;
   140     val (message, real_thm_names) =
   141       if is_some failure then ("External prover failed.", [])
   142       else if rc <> 0 then ("External prover failed: " ^ proof, [])
   143       else apfst (fn s => "Try this command: " ^ s)
   144         (produce_answer name (proof, thm_names, conj_pos, ctxt, th, subgoal));
   145   in
   146     {success = success, message = message,
   147       theorem_names = real_thm_names, runtime = time, proof = proof,
   148       internal_thm_names = thm_names, filtered_clauses = the_filtered_clauses}
   149   end;
   150 
   151 
   152 (* generic TPTP-based provers *)
   153 
   154 fun generic_tptp_prover
   155         (name, {command, arguments, failure_strs, max_new_clauses,
   156                 insert_theory_const, emit_structured_proof}) timeout =
   157   external_prover (get_relevant_facts max_new_clauses insert_theory_const)
   158       (prepare_clauses false) write_tptp_file command (arguments timeout)
   159       failure_strs
   160       (if emit_structured_proof then structured_isar_proof
   161        else metis_lemma_list false) name;
   162 
   163 fun tptp_prover (name, p) = (name, generic_tptp_prover (name, p));
   164 
   165 
   166 (** common provers **)
   167 
   168 (* Vampire *)
   169 
   170 (*NB: Vampire does not work without explicit timelimit*)
   171 
   172 val vampire_failure_strs =
   173   ["Satisfiability detected", "Refutation not found", "CANNOT PROVE"];
   174 val vampire_max_new_clauses = 60;
   175 val vampire_insert_theory_const = false;
   176 
   177 fun vampire_prover_config full : prover_config =
   178  {command = Path.explode "$VAMPIRE_HOME/vampire",
   179   arguments = (fn timeout => "--output_syntax tptp --mode casc" ^
   180     " -t " ^ string_of_int timeout),
   181   failure_strs = vampire_failure_strs,
   182   max_new_clauses = vampire_max_new_clauses,
   183   insert_theory_const = vampire_insert_theory_const,
   184   emit_structured_proof = full};
   185 
   186 val vampire = tptp_prover ("vampire", vampire_prover_config false);
   187 val vampire_full = tptp_prover ("vampire_full", vampire_prover_config true);
   188 
   189 
   190 (* E prover *)
   191 
   192 val eprover_failure_strs =
   193   ["SZS status: Satisfiable", "SZS status Satisfiable",
   194    "SZS status: ResourceOut", "SZS status ResourceOut",
   195    "# Cannot determine problem status"];
   196 val eprover_max_new_clauses = 100;
   197 val eprover_insert_theory_const = false;
   198 
   199 fun eprover_config full : prover_config =
   200  {command = Path.explode "$E_HOME/eproof",
   201   arguments = (fn timeout => "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev" ^
   202     " --silent --cpu-limit=" ^ string_of_int timeout),
   203   failure_strs = eprover_failure_strs,
   204   max_new_clauses = eprover_max_new_clauses,
   205   insert_theory_const = eprover_insert_theory_const,
   206   emit_structured_proof = full};
   207 
   208 val eprover = tptp_prover ("e", eprover_config false);
   209 val eprover_full = tptp_prover ("e_full", eprover_config true);
   210 
   211 
   212 (* SPASS *)
   213 
   214 val spass_failure_strs =
   215   ["SPASS beiseite: Completion found.", "SPASS beiseite: Ran out of time.",
   216    "SPASS beiseite: Maximal number of loops exceeded."];
   217 val spass_max_new_clauses = 40;
   218 val spass_insert_theory_const = true;
   219 
   220 fun spass_config insert_theory_const: prover_config =
   221  {command = Path.explode "$SPASS_HOME/SPASS",
   222   arguments = (fn timeout => "-Auto -SOS=1 -PGiven=0 -PProblem=0 -Splits=0" ^
   223     " -FullRed=0 -DocProof -TimeLimit=" ^ string_of_int timeout),
   224   failure_strs = spass_failure_strs,
   225   max_new_clauses = spass_max_new_clauses,
   226   insert_theory_const = insert_theory_const,
   227   emit_structured_proof = false};
   228 
   229 fun generic_dfg_prover
   230         (name, ({command, arguments, failure_strs, max_new_clauses,
   231                  insert_theory_const, ...} : prover_config)) timeout =
   232   external_prover
   233     (get_relevant_facts max_new_clauses insert_theory_const)
   234     (prepare_clauses true)
   235     write_dfg_file
   236     command
   237     (arguments timeout)
   238     failure_strs
   239     (metis_lemma_list true)
   240     name;
   241 
   242 fun dfg_prover (name, p) = (name, generic_dfg_prover (name, p));
   243 
   244 val spass = dfg_prover ("spass", spass_config spass_insert_theory_const);
   245 val spass_no_tc = dfg_prover ("spass_no_tc", spass_config false);
   246 
   247 
   248 (* remote prover invocation via SystemOnTPTP *)
   249 
   250 val systems = Synchronized.var "atp_wrapper_systems" ([]: string list);
   251 
   252 fun get_systems () =
   253   let
   254     val (answer, rc) = bash_output ("\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w")
   255   in
   256     if rc <> 0 then error ("Failed to get available systems at SystemOnTPTP:\n" ^ answer)
   257     else split_lines answer
   258   end;
   259 
   260 fun refresh_systems_on_tptp () =
   261   Synchronized.change systems (fn _ => get_systems ());
   262 
   263 fun get_system prefix = Synchronized.change_result systems (fn systems =>
   264   (if null systems then get_systems () else systems)
   265   |> `(find_first (String.isPrefix prefix)));
   266 
   267 fun the_system prefix =
   268   (case get_system prefix of
   269     NONE => error ("System " ^ quote prefix ^ " not available at SystemOnTPTP")
   270   | SOME sys => sys);
   271 
   272 val remote_failure_strs = ["Remote-script could not extract proof"];
   273 
   274 fun remote_prover_config prover_prefix args max_new insert_tc: prover_config =
   275  {command = Path.explode "$ISABELLE_ATP_MANAGER/SystemOnTPTP",
   276   arguments = (fn timeout =>
   277     args ^ " -t " ^ string_of_int timeout ^ " -s " ^ the_system prover_prefix),
   278   failure_strs = remote_failure_strs,
   279   max_new_clauses = max_new,
   280   insert_theory_const = insert_tc,
   281   emit_structured_proof = false};
   282 
   283 val remote_vampire = tptp_prover ("remote_vampire", remote_prover_config
   284   "Vampire---9" "" vampire_max_new_clauses vampire_insert_theory_const);
   285 
   286 val remote_eprover = tptp_prover ("remote_e", remote_prover_config
   287   "EP---" "" eprover_max_new_clauses eprover_insert_theory_const);
   288 
   289 val remote_spass = tptp_prover ("remote_spass", remote_prover_config
   290   "SPASS---" "-x" spass_max_new_clauses spass_insert_theory_const);
   291 
   292 val provers =
   293   [spass, vampire, eprover, vampire_full, eprover_full, spass_no_tc,
   294    remote_vampire, remote_spass, remote_eprover]
   295 val prover_setup = fold add_prover provers
   296 
   297 val setup =
   298   destdir_setup
   299   #> problem_prefix_setup
   300   #> measure_runtime_setup
   301   #> prover_setup;
   302 
   303 end;