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