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