src/HOL/Tools/ATP_Manager/atp_wrapper.ML
author boehmes
Sat Oct 03 12:05:40 2009 +0200 (2009-10-03)
changeset 32864 a226f29d4bdc
parent 32740 9dd0a2f83429
child 32869 159309603edc
permissions -rw-r--r--
re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
eliminated unused provers,
turned references into configuration values
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@32864
    12
  val setup: theory -> theory
boehmes@32864
    13
boehmes@32864
    14
  (*prover configuration, problem format, and prover result*)
boehmes@32864
    15
  datatype prover_config = Prover_Config of {
boehmes@32864
    16
    command: Path.T,
boehmes@32864
    17
    arguments: int -> string,
boehmes@32864
    18
    max_new_clauses: int,
boehmes@32864
    19
    insert_theory_const: bool,
boehmes@32864
    20
    emit_structured_proof: bool }
boehmes@32864
    21
  datatype atp_problem = ATP_Problem of {
boehmes@32864
    22
    with_full_types: bool,
boehmes@32864
    23
    subgoal: int,
boehmes@32864
    24
    goal: Proof.context * (thm list * thm),
boehmes@32864
    25
    axiom_clauses: (thm * (string * int)) list option,
boehmes@32864
    26
    filtered_clauses: (thm * (string * int)) list option }
boehmes@32864
    27
  val atp_problem_of_goal: bool -> int -> Proof.context * (thm list * thm) ->
boehmes@32864
    28
    atp_problem
boehmes@32864
    29
  datatype prover_result = Prover_Result of {
boehmes@32864
    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,
boehmes@32864
    36
    filtered_clauses: (thm * (string * int)) list }
boehmes@32864
    37
  type prover = atp_problem -> int -> 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@28592
    52
structure AtpWrapper: ATP_WRAPPER =
wenzelm@28592
    53
struct
wenzelm@28596
    54
wenzelm@28596
    55
(** generic ATP wrapper **)
wenzelm@28596
    56
boehmes@32864
    57
(* hooks for writing 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@32864
    65
val setup = destdir_setup #> problem_prefix_setup;
boehmes@32864
    66
boehmes@32864
    67
boehmes@32864
    68
(* prover configuration, problem format, and prover result *)
boehmes@32864
    69
boehmes@32864
    70
datatype prover_config = Prover_Config of {
boehmes@32864
    71
  command: Path.T,
boehmes@32864
    72
  arguments: int -> string,
boehmes@32864
    73
  max_new_clauses: int,
boehmes@32864
    74
  insert_theory_const: bool,
boehmes@32864
    75
  emit_structured_proof: bool }
wenzelm@28596
    76
boehmes@32864
    77
datatype atp_problem = ATP_Problem of {
boehmes@32864
    78
  with_full_types: bool,
boehmes@32864
    79
  subgoal: int,
boehmes@32864
    80
  goal: Proof.context * (thm list * thm),
boehmes@32864
    81
  axiom_clauses: (thm * (string * int)) list option,
boehmes@32864
    82
  filtered_clauses: (thm * (string * int)) list option }
boehmes@32864
    83
boehmes@32864
    84
fun atp_problem_of_goal with_full_types subgoal goal = ATP_Problem {
boehmes@32864
    85
  with_full_types = with_full_types,
boehmes@32864
    86
  subgoal = subgoal,
boehmes@32864
    87
  goal = goal,
boehmes@32864
    88
  axiom_clauses = NONE,
boehmes@32864
    89
  filtered_clauses = NONE }
boehmes@32864
    90
boehmes@32864
    91
datatype prover_result = Prover_Result of {
boehmes@32864
    92
  success: bool,
boehmes@32864
    93
  message: string,
boehmes@32864
    94
  theorem_names: string list,  (* relevant theorems *)
boehmes@32864
    95
  runtime: int,  (* user time of the ATP, in milliseconds *)
boehmes@32864
    96
  proof: string,
boehmes@32864
    97
  internal_thm_names: string Vector.vector,
boehmes@32864
    98
  filtered_clauses: (thm * (string * int)) list }
boehmes@32864
    99
boehmes@32864
   100
type prover = atp_problem -> int -> prover_result
wenzelm@28596
   101
wenzelm@28596
   102
wenzelm@28596
   103
(* basic template *)
wenzelm@28596
   104
boehmes@32458
   105
fun with_path cleanup after f path =
boehmes@32458
   106
  Exn.capture f path
boehmes@32458
   107
  |> tap (fn _ => cleanup path)
boehmes@32458
   108
  |> Exn.release
boehmes@32458
   109
  |> tap (after path)
boehmes@32458
   110
boehmes@32864
   111
fun external_prover relevance_filter preparer writer cmd args find_failure produce_answer
boehmes@32864
   112
  axiom_clauses filtered_clauses name subgoalno goal =
wenzelm@28596
   113
  let
immler@31750
   114
    (* get clauses and prepare them for writing *)
immler@30537
   115
    val (ctxt, (chain_ths, th)) = goal
immler@30536
   116
    val thy = ProofContext.theory_of ctxt
wenzelm@28596
   117
    val chain_ths = map (Thm.put_name_hint ResReconstruct.chained_hint) chain_ths
wenzelm@32257
   118
    val goal_cls = #1 (ResAxioms.neg_conjecture_clauses ctxt th subgoalno)
wenzelm@32091
   119
    val _ = app (fn th => Output.debug (fn _ => Display.string_of_thm ctxt th)) goal_cls
immler@31752
   120
    val the_filtered_clauses =
immler@31752
   121
      case filtered_clauses of
immler@31752
   122
          NONE => relevance_filter goal goal_cls
immler@31752
   123
        | SOME fcls => fcls
immler@31409
   124
    val the_axiom_clauses =
immler@31409
   125
      case axiom_clauses of
immler@31752
   126
          NONE => the_filtered_clauses
immler@31409
   127
        | SOME axcls => axcls
wenzelm@32257
   128
    val (thm_names, clauses) =
wenzelm@32257
   129
      preparer goal_cls chain_ths the_axiom_clauses the_filtered_clauses thy
immler@31750
   130
boehmes@32864
   131
    (* path to unique problem file *)
boehmes@32864
   132
    val destdir' = Config.get ctxt destdir
boehmes@32864
   133
    val problem_prefix' = Config.get ctxt problem_prefix
boehmes@32864
   134
    fun prob_pathname nr =
boehmes@32864
   135
      let val probfile = Path.basic (problem_prefix' ^ serial_string () ^ "_" ^ string_of_int nr)
boehmes@32864
   136
      in if destdir' = "" then File.tmp_path probfile
boehmes@32864
   137
        else if File.exists (Path.explode (destdir'))
boehmes@32864
   138
        then Path.append  (Path.explode (destdir')) probfile
boehmes@32864
   139
        else error ("No such directory: " ^ destdir')
boehmes@32864
   140
      end
boehmes@32864
   141
immler@31750
   142
    (* write out problem file and call prover *)
boehmes@32593
   143
    fun cmd_line probfile = "TIMEFORMAT='%3U'; { time " ^ space_implode " "
boehmes@32593
   144
      [File.shell_path cmd, args, File.platform_path probfile] ^ " ; } 2>&1"
boehmes@32510
   145
    fun split_time s =
boehmes@32510
   146
      let
boehmes@32510
   147
        val split = String.tokens (fn c => str c = "\n")
boehmes@32510
   148
        val (proof, t) = s |> split |> split_last |> apfst cat_lines
boehmes@32593
   149
        fun as_num f = f >> (fst o read_int)
boehmes@32593
   150
        val num = as_num (Scan.many1 Symbol.is_ascii_digit)
boehmes@32593
   151
        val digit = Scan.one Symbol.is_ascii_digit
boehmes@32593
   152
        val num3 = as_num (digit ::: digit ::: (digit >> single))
boehmes@32593
   153
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
boehmes@32510
   154
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode
boehmes@32510
   155
      in (proof, as_time t) end
boehmes@32458
   156
    fun run_on probfile =
boehmes@32458
   157
      if File.exists cmd
boehmes@32510
   158
      then
boehmes@32510
   159
        writer probfile clauses
boehmes@32510
   160
        |> pair (apfst split_time (system_out (cmd_line probfile)))
boehmes@32458
   161
      else error ("Bad executable: " ^ Path.implode cmd)
wenzelm@28592
   162
immler@31751
   163
    (* if problemfile has not been exported, delete problemfile; otherwise export proof, too *)
boehmes@32593
   164
    fun cleanup probfile = if destdir' = "" then try File.rm probfile else NONE
boehmes@32510
   165
    fun export probfile (((proof, _), _), _) = if destdir' = "" then ()
immler@31838
   166
      else File.write (Path.explode (Path.implode probfile ^ "_proof")) proof
wenzelm@32257
   167
boehmes@32510
   168
    val (((proof, time), rc), conj_pos) = with_path cleanup export run_on
boehmes@32458
   169
      (prob_pathname subgoalno)
boehmes@32458
   170
immler@29590
   171
    (* check for success and print out some information on failure *)
immler@29590
   172
    val failure = find_failure proof
immler@29597
   173
    val success = rc = 0 andalso is_none failure
boehmes@32864
   174
    val (message, real_thm_names) =
boehmes@32451
   175
      if is_some failure then ("External prover failed.", [])
boehmes@32451
   176
      else if rc <> 0 then ("External prover failed: " ^ proof, [])
boehmes@32451
   177
      else apfst (fn s => "Try this command: " ^ s)
boehmes@32451
   178
        (produce_answer name (proof, thm_names, conj_pos, ctxt, th, subgoalno))
immler@31411
   179
    val _ = Output.debug (fn () => "Sledgehammer response (rc = " ^ string_of_int rc ^ "):\n" ^ proof)
boehmes@32864
   180
  in
boehmes@32864
   181
    Prover_Result {success=success, message=message,
boehmes@32864
   182
      theorem_names=real_thm_names, runtime=time, proof=proof,
boehmes@32864
   183
      internal_thm_names = thm_names, filtered_clauses=the_filtered_clauses}
boehmes@32864
   184
  end
wenzelm@28592
   185
wenzelm@28596
   186
wenzelm@28596
   187
(* generic TPTP-based provers *)
wenzelm@28596
   188
boehmes@32864
   189
fun gen_tptp_prover (name, prover_config) problem timeout =
boehmes@32864
   190
  let
boehmes@32864
   191
    val Prover_Config {max_new_clauses, insert_theory_const,
boehmes@32864
   192
      emit_structured_proof, command, arguments} = prover_config
boehmes@32864
   193
    val ATP_Problem {with_full_types, subgoal, goal, axiom_clauses,
boehmes@32864
   194
      filtered_clauses} = problem
boehmes@32864
   195
  in
boehmes@32864
   196
    external_prover
boehmes@32864
   197
      (ResAtp.get_relevant max_new_clauses insert_theory_const)
boehmes@32864
   198
      (ResAtp.prepare_clauses false)
boehmes@32864
   199
      (ResHolClause.tptp_write_file with_full_types)
boehmes@32864
   200
      command
boehmes@32864
   201
      (arguments timeout)
boehmes@32864
   202
      ResReconstruct.find_failure
boehmes@32864
   203
      (if emit_structured_proof then ResReconstruct.structured_proof
boehmes@32864
   204
        else ResReconstruct.lemma_list false)
boehmes@32864
   205
      axiom_clauses
boehmes@32864
   206
      filtered_clauses
boehmes@32864
   207
      name
boehmes@32864
   208
      subgoal
boehmes@32864
   209
      goal
boehmes@32864
   210
  end
wenzelm@28596
   211
boehmes@32864
   212
fun tptp_prover (name, config) = (name, gen_tptp_prover (name, config))
wenzelm@28596
   213
wenzelm@28596
   214
boehmes@32864
   215
(** common provers **)
wenzelm@28592
   216
wenzelm@28596
   217
(* Vampire *)
wenzelm@28596
   218
wenzelm@28596
   219
(*NB: Vampire does not work without explicit timelimit*)
wenzelm@28596
   220
boehmes@32864
   221
val vampire_max_new_clauses = 60
boehmes@32864
   222
val vampire_insert_theory_const = false
wenzelm@28596
   223
boehmes@32864
   224
fun vampire_prover_config full = Prover_Config {
boehmes@32864
   225
  command = Path.explode "$VAMPIRE_HOME/vampire",
boehmes@32864
   226
  arguments = (fn timeout => "--output_syntax tptp --mode casc" ^
boehmes@32864
   227
    " -t " ^ string_of_int timeout),
boehmes@32864
   228
  max_new_clauses = vampire_max_new_clauses,
boehmes@32864
   229
  insert_theory_const = vampire_insert_theory_const,
boehmes@32864
   230
  emit_structured_proof = full }
wenzelm@28596
   231
boehmes@32864
   232
val vampire = tptp_prover ("vampire", vampire_prover_config false)
boehmes@32864
   233
val vampire_full = tptp_prover ("vampire_full", vampire_prover_config true)
wenzelm@28596
   234
wenzelm@28592
   235
wenzelm@28596
   236
(* E prover *)
wenzelm@28596
   237
boehmes@32864
   238
val eprover_max_new_clauses = 100
boehmes@32864
   239
val eprover_insert_theory_const = false
wenzelm@28596
   240
boehmes@32864
   241
fun eprover_config full = Prover_Config {
boehmes@32864
   242
  command = Path.explode "$E_HOME/eproof",
boehmes@32864
   243
  arguments = (fn timeout => "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev" ^
boehmes@32864
   244
    " --silent --cpu-limit=" ^ string_of_int timeout),
boehmes@32864
   245
  max_new_clauses = eprover_max_new_clauses,
boehmes@32864
   246
  insert_theory_const = eprover_insert_theory_const,
boehmes@32864
   247
  emit_structured_proof = full }
wenzelm@28596
   248
boehmes@32864
   249
val eprover = tptp_prover ("e", eprover_config false)
boehmes@32864
   250
val eprover_full = tptp_prover ("e_full", eprover_config true)
wenzelm@28596
   251
wenzelm@28596
   252
wenzelm@28596
   253
(* SPASS *)
wenzelm@28592
   254
boehmes@32864
   255
val spass_max_new_clauses = 40
boehmes@32864
   256
val spass_insert_theory_const = true
wenzelm@28596
   257
boehmes@32864
   258
fun spass_config insert_theory_const = Prover_Config {
boehmes@32864
   259
  command = Path.explode "$SPASS_HOME/SPASS",
boehmes@32864
   260
  arguments = (fn timeout => "-Auto -SOS=1 -PGiven=0 -PProblem=0 -Splits=0" ^
boehmes@32864
   261
    " -FullRed=0 -DocProof -TimeLimit=" ^ string_of_int timeout),
boehmes@32864
   262
  max_new_clauses = spass_max_new_clauses,
boehmes@32864
   263
  insert_theory_const = insert_theory_const,
boehmes@32864
   264
  emit_structured_proof = false }
boehmes@32864
   265
boehmes@32864
   266
val spass = tptp_prover ("spass", spass_config spass_insert_theory_const)
boehmes@32864
   267
val spass_no_tc = tptp_prover ("spass_no_tc", spass_config false)
wenzelm@28592
   268
wenzelm@28596
   269
wenzelm@28596
   270
(* remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   271
immler@31835
   272
val systems =
immler@31835
   273
  Synchronized.var "atp_wrapper_systems" ([]: string list);
immler@31835
   274
immler@31835
   275
fun get_systems () =
immler@31835
   276
  let
wenzelm@32327
   277
    val (answer, rc) = system_out ("\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w")
immler@31835
   278
  in
wenzelm@32327
   279
    if rc <> 0 then error ("Failed to get available systems from SystemOnTPTP:\n" ^ answer)
immler@31835
   280
    else split_lines answer
immler@31835
   281
  end;
immler@31835
   282
immler@31835
   283
fun refresh_systems () = Synchronized.change systems (fn _ =>
wenzelm@32257
   284
  get_systems ());
immler@31835
   285
immler@31835
   286
fun get_system prefix = Synchronized.change_result systems (fn systems =>
boehmes@32864
   287
  (if null systems then get_systems () else systems)
boehmes@32864
   288
  |> ` (find_first (String.isPrefix prefix)));
boehmes@32864
   289
boehmes@32864
   290
fun get_the_system prefix =
boehmes@32864
   291
  (case get_system prefix of
boehmes@32864
   292
    NONE => error ("No system like " ^ quote prefix ^ " at SystemOnTPTP")
boehmes@32864
   293
  | SOME sys => sys)
immler@31835
   294
boehmes@32864
   295
fun remote_prover_config prover_prefix args max_new insert_tc = Prover_Config {
boehmes@32864
   296
  command = Path.explode "$ISABELLE_ATP_MANAGER/SystemOnTPTP",
boehmes@32864
   297
  arguments = (fn timeout => args ^ " -t " ^ string_of_int timeout ^ " -s " ^
boehmes@32864
   298
    get_the_system prover_prefix),
boehmes@32864
   299
  max_new_clauses = max_new,
boehmes@32864
   300
  insert_theory_const = insert_tc,
boehmes@32864
   301
  emit_structured_proof = false }
wenzelm@28596
   302
boehmes@32864
   303
val remote_vampire = tptp_prover ("remote_vampire", remote_prover_config
boehmes@32864
   304
  "Vampire---9" "" vampire_max_new_clauses vampire_insert_theory_const)
boehmes@32864
   305
boehmes@32864
   306
val remote_eprover = tptp_prover ("remote_e", remote_prover_config
boehmes@32864
   307
  "EP---" "" eprover_max_new_clauses eprover_insert_theory_const)
boehmes@32864
   308
boehmes@32864
   309
val remote_spass = tptp_prover ("remote_spass", remote_prover_config
boehmes@32864
   310
  "SPASS---" "-x" spass_max_new_clauses spass_insert_theory_const)
wenzelm@28592
   311
wenzelm@28592
   312
end;