src/HOL/Tools/ATP_Manager/atp_wrapper.ML
author blanchet
Fri Apr 16 20:50:50 2010 +0200 (2010-04-16)
changeset 36187 4deef08608ee
parent 36169 27b1cc58715e
child 36190 500fc43d5537
permissions -rw-r--r--
added timestamp to proof
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@35865
    49
  failure_strs: string list,
boehmes@32864
    50
  max_new_clauses: int,
blanchet@36059
    51
  prefers_theory_const: 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@35865
    63
fun find_failure strs proof =
blanchet@35865
    64
  case filter (fn s => String.isSubstring s proof) strs of
blanchet@35865
    65
    [] => if is_proof_well_formed proof then NONE
blanchet@35865
    66
          else SOME "Ill-formed ATP output"
blanchet@35865
    67
  | (failure :: _) => SOME failure
blanchet@35865
    68
blanchet@36169
    69
fun generic_prover overlord get_facts prepare write_file cmd args failure_strs
blanchet@36169
    70
        produce_answer name ({debug, full_types, ...} : params)
blanchet@35969
    71
        ({subgoal, goal, relevance_override, axiom_clauses, filtered_clauses}
blanchet@35969
    72
         : problem) =
wenzelm@28596
    73
  let
immler@31750
    74
    (* get clauses and prepare them for writing *)
wenzelm@32942
    75
    val (ctxt, (chain_ths, th)) = goal;
wenzelm@32942
    76
    val thy = ProofContext.theory_of ctxt;
blanchet@35865
    77
    val chain_ths = map (Thm.put_name_hint chained_hint) chain_ths;
blanchet@35969
    78
    val goal_cls = #1 (neg_conjecture_clauses ctxt th subgoal);
immler@31752
    79
    val the_filtered_clauses =
wenzelm@32942
    80
      (case filtered_clauses of
blanchet@35969
    81
        NONE => get_facts relevance_override goal goal_cls
wenzelm@32942
    82
      | SOME fcls => fcls);
immler@31409
    83
    val the_axiom_clauses =
wenzelm@32942
    84
      (case axiom_clauses of
wenzelm@32942
    85
        NONE => the_filtered_clauses
wenzelm@32942
    86
      | SOME axcls => axcls);
blanchet@35969
    87
    val (internal_thm_names, clauses) =
wenzelm@32942
    88
      prepare goal_cls chain_ths the_axiom_clauses the_filtered_clauses thy;
immler@31750
    89
boehmes@32864
    90
    (* path to unique problem file *)
blanchet@36143
    91
    val destdir' = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@36143
    92
                   else Config.get ctxt destdir;
wenzelm@32942
    93
    val problem_prefix' = Config.get ctxt problem_prefix;
boehmes@32864
    94
    fun prob_pathname nr =
blanchet@36143
    95
      let
blanchet@36143
    96
        val probfile =
blanchet@36143
    97
          Path.basic (problem_prefix' ^
blanchet@36143
    98
                      (if overlord then "_" ^ name else serial_string ())
blanchet@36143
    99
                      ^ "_" ^ string_of_int nr)
wenzelm@32942
   100
      in
wenzelm@32942
   101
        if destdir' = "" then File.tmp_path probfile
wenzelm@35570
   102
        else if File.exists (Path.explode destdir')
wenzelm@35570
   103
        then Path.append  (Path.explode destdir') probfile
blanchet@36169
   104
        else error ("No such directory: " ^ destdir' ^ ".")
wenzelm@32942
   105
      end;
boehmes@32864
   106
immler@31750
   107
    (* write out problem file and call prover *)
boehmes@33247
   108
    fun cmd_line probfile =
boehmes@33247
   109
      if Config.get ctxt measure_runtime
boehmes@33247
   110
      then (* Warning: suppresses error messages of ATPs *)
boehmes@33247
   111
        "TIMEFORMAT='%3U'; { time " ^ space_implode " " [File.shell_path cmd,
boehmes@33247
   112
        args, File.shell_path probfile] ^ " 2> /dev/null" ^ " ; } 2>&1"
boehmes@33247
   113
      else
boehmes@33247
   114
        space_implode " " ["exec", File.shell_path cmd, args,
boehmes@33247
   115
        File.shell_path probfile];
boehmes@32510
   116
    fun split_time s =
boehmes@32510
   117
      let
wenzelm@32942
   118
        val split = String.tokens (fn c => str c = "\n");
wenzelm@32942
   119
        val (proof, t) = s |> split |> split_last |> apfst cat_lines;
wenzelm@32942
   120
        fun as_num f = f >> (fst o read_int);
wenzelm@32942
   121
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
wenzelm@32942
   122
        val digit = Scan.one Symbol.is_ascii_digit;
wenzelm@32942
   123
        val num3 = as_num (digit ::: digit ::: (digit >> single));
wenzelm@32942
   124
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
wenzelm@32942
   125
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
wenzelm@32942
   126
      in (proof, as_time t) end;
boehmes@33247
   127
    fun split_time' s =
boehmes@33247
   128
      if Config.get ctxt measure_runtime then split_time s else (s, 0)
boehmes@32458
   129
    fun run_on probfile =
wenzelm@32942
   130
      if File.exists cmd then
blanchet@36169
   131
        write_file debug full_types probfile clauses
wenzelm@35010
   132
        |> pair (apfst split_time' (bash_output (cmd_line probfile)))
blanchet@36167
   133
      else error ("Bad executable: " ^ Path.implode cmd ^ ".");
wenzelm@28592
   134
blanchet@36167
   135
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@36167
   136
       the proof file too. *)
wenzelm@32942
   137
    fun cleanup probfile = if destdir' = "" then try File.rm probfile else NONE;
wenzelm@32942
   138
    fun export probfile (((proof, _), _), _) =
blanchet@36187
   139
      if destdir' = "" then
blanchet@36187
   140
        ()
blanchet@36187
   141
      else
blanchet@36187
   142
        File.write (Path.explode (Path.implode probfile ^ "_proof"))
blanchet@36187
   143
                   ("#" ^ timestamp () ^ "\n" ^ proof)
wenzelm@32257
   144
blanchet@35969
   145
    val (((proof, atp_run_time_in_msecs), rc), conj_pos) =
wenzelm@35570
   146
      with_path cleanup export run_on (prob_pathname subgoal);
boehmes@32458
   147
blanchet@36167
   148
    (* Check for success and print out some information on failure. *)
blanchet@35865
   149
    val failure = find_failure failure_strs proof;
wenzelm@32942
   150
    val success = rc = 0 andalso is_none failure;
blanchet@35969
   151
    val (message, relevant_thm_names) =
blanchet@36167
   152
      if is_some failure then ("ATP failed to find a proof.", [])
blanchet@36167
   153
      else if rc <> 0 then ("ATP error: " ^ proof ^ ".", [])
blanchet@35969
   154
      else
blanchet@35969
   155
        (produce_answer name (proof, internal_thm_names, conj_pos, ctxt, th,
blanchet@35969
   156
                              subgoal));
boehmes@32864
   157
  in
wenzelm@32941
   158
    {success = success, message = message,
blanchet@35969
   159
     relevant_thm_names = relevant_thm_names,
blanchet@35969
   160
     atp_run_time_in_msecs = atp_run_time_in_msecs, proof = proof,
blanchet@35969
   161
     internal_thm_names = internal_thm_names,
blanchet@35969
   162
     filtered_clauses = the_filtered_clauses}
wenzelm@32942
   163
  end;
wenzelm@28592
   164
wenzelm@28596
   165
wenzelm@28596
   166
(* generic TPTP-based provers *)
wenzelm@28596
   167
blanchet@35865
   168
fun generic_tptp_prover
blanchet@35865
   169
        (name, {command, arguments, failure_strs, max_new_clauses,
blanchet@36059
   170
                prefers_theory_const, supports_isar_proofs})
blanchet@36143
   171
        (params as {overlord, respect_no_atp, relevance_threshold, convergence,
blanchet@36064
   172
                    theory_const, higher_order, follow_defs, isar_proof,
blanchet@36064
   173
                    modulus, sorts, ...})
blanchet@36059
   174
        timeout =
blanchet@36143
   175
  generic_prover overlord
blanchet@36058
   176
      (get_relevant_facts respect_no_atp relevance_threshold convergence
blanchet@36058
   177
                          higher_order follow_defs max_new_clauses
blanchet@36059
   178
                          (the_default prefers_theory_const theory_const))
blanchet@35969
   179
      (prepare_clauses higher_order false) write_tptp_file command
blanchet@35969
   180
      (arguments timeout) failure_strs
blanchet@36064
   181
      (if supports_isar_proofs andalso isar_proof then
blanchet@36064
   182
         structured_isar_proof modulus sorts
blanchet@36064
   183
       else
blanchet@36064
   184
         metis_lemma_list false) name params;
wenzelm@28596
   185
blanchet@35969
   186
fun tptp_prover name p = (name, generic_tptp_prover (name, p));
wenzelm@28596
   187
wenzelm@32941
   188
boehmes@32864
   189
(** common provers **)
wenzelm@28592
   190
blanchet@36142
   191
fun generous_to_secs time = (Time.toMilliseconds time + 999) div 1000
blanchet@36142
   192
wenzelm@28596
   193
(* Vampire *)
wenzelm@28596
   194
blanchet@35969
   195
(* NB: Vampire does not work without explicit time limit. *)
wenzelm@28596
   196
blanchet@35969
   197
val vampire_config : prover_config =
blanchet@35969
   198
  {command = Path.explode "$VAMPIRE_HOME/vampire",
blanchet@35969
   199
   arguments = (fn timeout => "--output_syntax tptp --mode casc -t " ^
blanchet@36142
   200
                              string_of_int (generous_to_secs timeout)),
blanchet@35969
   201
   failure_strs =
blanchet@35969
   202
     ["Satisfiability detected", "Refutation not found", "CANNOT PROVE"],
blanchet@35969
   203
   max_new_clauses = 60,
blanchet@36059
   204
   prefers_theory_const = false,
blanchet@35969
   205
   supports_isar_proofs = true}
blanchet@35969
   206
val vampire = tptp_prover "vampire" vampire_config
wenzelm@28596
   207
wenzelm@28592
   208
wenzelm@28596
   209
(* E prover *)
wenzelm@28596
   210
blanchet@35969
   211
val e_config : prover_config =
blanchet@35969
   212
  {command = Path.explode "$E_HOME/eproof",
blanchet@35969
   213
   arguments = (fn timeout => "--tstp-in --tstp-out -l5 -xAutoDev \
blanchet@35969
   214
                              \-tAutoDev --silent --cpu-limit=" ^
blanchet@36142
   215
                              string_of_int (generous_to_secs timeout)),
blanchet@35969
   216
   failure_strs =
blanchet@35969
   217
       ["SZS status: Satisfiable", "SZS status Satisfiable",
blanchet@35969
   218
        "SZS status: ResourceOut", "SZS status ResourceOut",
blanchet@35969
   219
        "# Cannot determine problem status"],
blanchet@35969
   220
   max_new_clauses = 100,
blanchet@36059
   221
   prefers_theory_const = false,
blanchet@35969
   222
   supports_isar_proofs = true}
blanchet@35969
   223
val e = tptp_prover "e" e_config
wenzelm@28596
   224
wenzelm@28596
   225
wenzelm@28596
   226
(* SPASS *)
wenzelm@28592
   227
blanchet@35865
   228
fun generic_dfg_prover
blanchet@35865
   229
        (name, ({command, arguments, failure_strs, max_new_clauses,
blanchet@36059
   230
                 prefers_theory_const, ...} : prover_config))
blanchet@36143
   231
        (params as {overlord, respect_no_atp, relevance_threshold, convergence,
blanchet@36064
   232
                    theory_const, higher_order, follow_defs, ...})
blanchet@36059
   233
        timeout =
blanchet@36143
   234
  generic_prover overlord
blanchet@36058
   235
      (get_relevant_facts respect_no_atp relevance_threshold convergence
blanchet@36058
   236
                          higher_order follow_defs max_new_clauses
blanchet@36059
   237
                          (the_default prefers_theory_const theory_const))
blanchet@35969
   238
      (prepare_clauses higher_order true) write_dfg_file command
blanchet@35969
   239
      (arguments timeout) failure_strs (metis_lemma_list true) name params;
boehmes@32869
   240
blanchet@35865
   241
fun dfg_prover (name, p) = (name, generic_dfg_prover (name, p));
boehmes@32869
   242
blanchet@36059
   243
val spass_config : prover_config =
blanchet@35969
   244
 {command = Path.explode "$SPASS_HOME/SPASS",
blanchet@35969
   245
  arguments = (fn timeout => "-Auto -SOS=1 -PGiven=0 -PProblem=0 -Splits=0" ^
blanchet@35969
   246
    " -FullRed=0 -DocProof -TimeLimit=" ^
blanchet@36142
   247
    string_of_int (generous_to_secs timeout)),
blanchet@35969
   248
  failure_strs =
blanchet@35969
   249
    ["SPASS beiseite: Completion found.", "SPASS beiseite: Ran out of time.",
blanchet@35969
   250
     "SPASS beiseite: Maximal number of loops exceeded."],
blanchet@35969
   251
  max_new_clauses = 40,
blanchet@36059
   252
  prefers_theory_const = true,
blanchet@36059
   253
  supports_isar_proofs = false}
blanchet@35969
   254
val spass = dfg_prover ("spass", spass_config)
blanchet@35969
   255
wenzelm@28596
   256
wenzelm@28596
   257
(* remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   258
wenzelm@32942
   259
val systems = Synchronized.var "atp_wrapper_systems" ([]: string list);
immler@31835
   260
immler@31835
   261
fun get_systems () =
immler@31835
   262
  let
blanchet@35969
   263
    val (answer, rc) = bash_output "\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w"
immler@31835
   264
  in
blanchet@35969
   265
    if rc <> 0 then
blanchet@35969
   266
      error ("Failed to get available systems at SystemOnTPTP:\n" ^ answer)
blanchet@35969
   267
    else
blanchet@35969
   268
      split_lines answer
immler@31835
   269
  end;
immler@31835
   270
blanchet@35867
   271
fun refresh_systems_on_tptp () =
blanchet@35867
   272
  Synchronized.change systems (fn _ => get_systems ());
immler@31835
   273
immler@31835
   274
fun get_system prefix = Synchronized.change_result systems (fn systems =>
boehmes@32864
   275
  (if null systems then get_systems () else systems)
wenzelm@32942
   276
  |> `(find_first (String.isPrefix prefix)));
boehmes@32864
   277
wenzelm@32948
   278
fun the_system prefix =
boehmes@32864
   279
  (case get_system prefix of
blanchet@35826
   280
    NONE => error ("System " ^ quote prefix ^ " not available at SystemOnTPTP")
wenzelm@32942
   281
  | SOME sys => sys);
immler@31835
   282
blanchet@35865
   283
val remote_failure_strs = ["Remote-script could not extract proof"];
blanchet@35865
   284
blanchet@36059
   285
fun remote_prover_config prover_prefix args
blanchet@36059
   286
        ({failure_strs, max_new_clauses, prefers_theory_const, ...}
blanchet@36059
   287
         : prover_config) : prover_config =
blanchet@35969
   288
  {command = Path.explode "$ISABELLE_ATP_MANAGER/SystemOnTPTP",
blanchet@35969
   289
   arguments = (fn timeout =>
blanchet@36142
   290
     args ^ " -t " ^ string_of_int (generous_to_secs timeout) ^ " -s " ^
blanchet@35969
   291
     the_system prover_prefix),
blanchet@36059
   292
   failure_strs = remote_failure_strs @ failure_strs,
blanchet@35969
   293
   max_new_clauses = max_new_clauses,
blanchet@36059
   294
   prefers_theory_const = prefers_theory_const,
blanchet@35969
   295
   supports_isar_proofs = false}
wenzelm@28596
   296
blanchet@35969
   297
val remote_vampire =
blanchet@35969
   298
  tptp_prover "remote_vampire"
blanchet@36059
   299
              (remote_prover_config "Vampire---9" "" vampire_config)
boehmes@32864
   300
blanchet@35969
   301
val remote_e =
blanchet@36059
   302
  tptp_prover "remote_e" (remote_prover_config "EP---" "" e_config)
boehmes@32864
   303
blanchet@35969
   304
val remote_spass =
blanchet@36059
   305
  tptp_prover "remote_spass" (remote_prover_config "SPASS---" "-x" spass_config)
wenzelm@28592
   306
blanchet@36059
   307
val provers = [spass, vampire, e, remote_vampire, remote_spass, remote_e]
blanchet@35867
   308
val prover_setup = fold add_prover provers
blanchet@35867
   309
blanchet@35867
   310
val setup =
blanchet@35867
   311
  destdir_setup
blanchet@35867
   312
  #> problem_prefix_setup
blanchet@35867
   313
  #> measure_runtime_setup
blanchet@35867
   314
  #> prover_setup;
blanchet@35867
   315
wenzelm@28592
   316
end;