src/HOL/Tools/ATP_Manager/atp_wrapper.ML
author wenzelm
Sat Feb 06 14:50:55 2010 +0100 (2010-02-06)
changeset 35010 d6e492cea6e4
parent 33316 6a72af4e84b8
child 35570 0e30eef52d85
permissions -rw-r--r--
renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
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@32942
   114
fun external_prover relevance_filter prepare write cmd args find_failure produce_answer
wenzelm@32944
   115
    axiom_clauses filtered_clauses name subgoalno goal =
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@33316
   121
    val goal_cls = #1 (Res_Axioms.neg_conjecture_clauses ctxt th subgoalno);
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
boehmes@32864
   141
        else if File.exists (Path.explode (destdir'))
boehmes@32864
   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@32942
   170
        write 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@32942
   181
      with_path cleanup export run_on (prob_pathname subgoalno);
boehmes@32458
   182
immler@29590
   183
    (* check for success and print out some information on failure *)
wenzelm@32942
   184
    val failure = 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@32942
   190
        (produce_answer name (proof, thm_names, conj_pos, ctxt, th, subgoalno));
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;
wenzelm@32942
   204
    val {with_full_types, subgoal, goal, axiom_clauses, filtered_clauses} = problem;
boehmes@32864
   205
  in
boehmes@32864
   206
    external_prover
wenzelm@33316
   207
      (Res_ATP.get_relevant max_new_clauses insert_theory_const)
wenzelm@33316
   208
      (Res_ATP.prepare_clauses false)
wenzelm@33316
   209
      (Res_HOL_Clause.tptp_write_file with_full_types)
boehmes@32864
   210
      command
boehmes@32864
   211
      (arguments timeout)
wenzelm@33316
   212
      Res_Reconstruct.find_failure
wenzelm@33316
   213
      (if emit_structured_proof then Res_Reconstruct.structured_proof
wenzelm@33316
   214
       else Res_Reconstruct.lemma_list false)
boehmes@32864
   215
      axiom_clauses
boehmes@32864
   216
      filtered_clauses
boehmes@32864
   217
      name
boehmes@32864
   218
      subgoal
boehmes@32864
   219
      goal
wenzelm@32942
   220
  end;
wenzelm@28596
   221
wenzelm@32942
   222
fun tptp_prover (name, config) = (name, gen_tptp_prover (name, config));
wenzelm@28596
   223
wenzelm@28596
   224
wenzelm@32941
   225
boehmes@32864
   226
(** common provers **)
wenzelm@28592
   227
wenzelm@28596
   228
(* Vampire *)
wenzelm@28596
   229
wenzelm@28596
   230
(*NB: Vampire does not work without explicit timelimit*)
wenzelm@28596
   231
wenzelm@32942
   232
val vampire_max_new_clauses = 60;
wenzelm@32942
   233
val vampire_insert_theory_const = false;
wenzelm@28596
   234
wenzelm@32941
   235
fun vampire_prover_config full : prover_config =
wenzelm@32941
   236
 {command = Path.explode "$VAMPIRE_HOME/vampire",
boehmes@32864
   237
  arguments = (fn timeout => "--output_syntax tptp --mode casc" ^
boehmes@32864
   238
    " -t " ^ string_of_int timeout),
boehmes@32864
   239
  max_new_clauses = vampire_max_new_clauses,
boehmes@32864
   240
  insert_theory_const = vampire_insert_theory_const,
wenzelm@32942
   241
  emit_structured_proof = full};
wenzelm@28596
   242
wenzelm@32942
   243
val vampire = tptp_prover ("vampire", vampire_prover_config false);
wenzelm@32942
   244
val vampire_full = tptp_prover ("vampire_full", vampire_prover_config true);
wenzelm@28596
   245
wenzelm@28592
   246
wenzelm@28596
   247
(* E prover *)
wenzelm@28596
   248
wenzelm@32942
   249
val eprover_max_new_clauses = 100;
wenzelm@32942
   250
val eprover_insert_theory_const = false;
wenzelm@28596
   251
wenzelm@32941
   252
fun eprover_config full : prover_config =
wenzelm@32941
   253
 {command = Path.explode "$E_HOME/eproof",
boehmes@32864
   254
  arguments = (fn timeout => "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev" ^
boehmes@32864
   255
    " --silent --cpu-limit=" ^ string_of_int timeout),
boehmes@32864
   256
  max_new_clauses = eprover_max_new_clauses,
boehmes@32864
   257
  insert_theory_const = eprover_insert_theory_const,
wenzelm@32942
   258
  emit_structured_proof = full};
wenzelm@28596
   259
wenzelm@32942
   260
val eprover = tptp_prover ("e", eprover_config false);
wenzelm@32942
   261
val eprover_full = tptp_prover ("e_full", eprover_config true);
wenzelm@28596
   262
wenzelm@28596
   263
wenzelm@28596
   264
(* SPASS *)
wenzelm@28592
   265
wenzelm@32942
   266
val spass_max_new_clauses = 40;
wenzelm@32942
   267
val spass_insert_theory_const = true;
wenzelm@28596
   268
wenzelm@32941
   269
fun spass_config insert_theory_const: prover_config =
wenzelm@32941
   270
 {command = Path.explode "$SPASS_HOME/SPASS",
boehmes@32864
   271
  arguments = (fn timeout => "-Auto -SOS=1 -PGiven=0 -PProblem=0 -Splits=0" ^
boehmes@32864
   272
    " -FullRed=0 -DocProof -TimeLimit=" ^ string_of_int timeout),
boehmes@32864
   273
  max_new_clauses = spass_max_new_clauses,
boehmes@32864
   274
  insert_theory_const = insert_theory_const,
wenzelm@32942
   275
  emit_structured_proof = false};
boehmes@32864
   276
wenzelm@32948
   277
fun gen_dfg_prover (name, prover_config: prover_config) timeout problem =
boehmes@32869
   278
  let
wenzelm@32942
   279
    val {max_new_clauses, insert_theory_const, command, arguments, ...} = prover_config
wenzelm@32941
   280
    val {with_full_types, subgoal, goal, axiom_clauses, filtered_clauses} = problem
boehmes@32869
   281
  in
boehmes@32869
   282
    external_prover
wenzelm@33316
   283
      (Res_ATP.get_relevant max_new_clauses insert_theory_const)
wenzelm@33316
   284
      (Res_ATP.prepare_clauses true)
wenzelm@33316
   285
      (Res_HOL_Clause.dfg_write_file with_full_types)
boehmes@32869
   286
      command
boehmes@32869
   287
      (arguments timeout)
wenzelm@33316
   288
      Res_Reconstruct.find_failure
wenzelm@33316
   289
      (Res_Reconstruct.lemma_list true)
boehmes@32869
   290
      axiom_clauses
boehmes@32869
   291
      filtered_clauses
boehmes@32869
   292
      name
boehmes@32869
   293
      subgoal
boehmes@32869
   294
      goal
wenzelm@32942
   295
  end;
boehmes@32869
   296
wenzelm@32942
   297
fun dfg_prover (name, config) = (name, gen_dfg_prover (name, config));
boehmes@32869
   298
wenzelm@32942
   299
val spass = dfg_prover ("spass", spass_config spass_insert_theory_const);
wenzelm@32942
   300
val spass_no_tc = dfg_prover ("spass_no_tc", spass_config false);
wenzelm@28592
   301
wenzelm@28596
   302
wenzelm@28596
   303
(* remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   304
wenzelm@32942
   305
val systems = Synchronized.var "atp_wrapper_systems" ([]: string list);
immler@31835
   306
immler@31835
   307
fun get_systems () =
immler@31835
   308
  let
wenzelm@35010
   309
    val (answer, rc) = bash_output ("\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w")
immler@31835
   310
  in
wenzelm@32327
   311
    if rc <> 0 then error ("Failed to get available systems from SystemOnTPTP:\n" ^ answer)
immler@31835
   312
    else split_lines answer
immler@31835
   313
  end;
immler@31835
   314
wenzelm@32942
   315
fun refresh_systems () = Synchronized.change systems (fn _ => get_systems ());
immler@31835
   316
immler@31835
   317
fun get_system prefix = Synchronized.change_result systems (fn systems =>
boehmes@32864
   318
  (if null systems then get_systems () else systems)
wenzelm@32942
   319
  |> `(find_first (String.isPrefix prefix)));
boehmes@32864
   320
wenzelm@32948
   321
fun the_system prefix =
boehmes@32864
   322
  (case get_system prefix of
boehmes@32864
   323
    NONE => error ("No system like " ^ quote prefix ^ " at SystemOnTPTP")
wenzelm@32942
   324
  | SOME sys => sys);
immler@31835
   325
wenzelm@32941
   326
fun remote_prover_config prover_prefix args max_new insert_tc: prover_config =
wenzelm@32941
   327
 {command = Path.explode "$ISABELLE_ATP_MANAGER/SystemOnTPTP",
wenzelm@32942
   328
  arguments =
wenzelm@32948
   329
    (fn timeout => args ^ " -t " ^ string_of_int timeout ^ " -s " ^ the_system prover_prefix),
boehmes@32864
   330
  max_new_clauses = max_new,
boehmes@32864
   331
  insert_theory_const = insert_tc,
wenzelm@32942
   332
  emit_structured_proof = false};
wenzelm@28596
   333
boehmes@32864
   334
val remote_vampire = tptp_prover ("remote_vampire", remote_prover_config
wenzelm@32942
   335
  "Vampire---9" "" vampire_max_new_clauses vampire_insert_theory_const);
boehmes@32864
   336
boehmes@32864
   337
val remote_eprover = tptp_prover ("remote_e", remote_prover_config
wenzelm@32942
   338
  "EP---" "" eprover_max_new_clauses eprover_insert_theory_const);
boehmes@32864
   339
boehmes@32864
   340
val remote_spass = tptp_prover ("remote_spass", remote_prover_config
wenzelm@32942
   341
  "SPASS---" "-x" spass_max_new_clauses spass_insert_theory_const);
wenzelm@28592
   342
wenzelm@28592
   343
end;