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