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