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