src/HOL/Tools/ATP_Manager/atp_wrapper.ML
author wenzelm
Thu Oct 15 12:23:24 2009 +0200 (2009-10-15)
changeset 32942 b6711ec9de26
parent 32941 72d48e333b77
child 32944 ecc0705174c2
permissions -rw-r--r--
misc tuning and recovery of Isabelle coding style;
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
wenzelm@32942
   110
fun external_prover relevance_filter prepare write 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 *)
wenzelm@32942
   114
    val (ctxt, (chain_ths, th)) = goal;
wenzelm@32942
   115
    val thy = ProofContext.theory_of ctxt;
wenzelm@32942
   116
    val chain_ths = map (Thm.put_name_hint ResReconstruct.chained_hint) chain_ths;
wenzelm@32942
   117
    val goal_cls = #1 (ResAxioms.neg_conjecture_clauses ctxt th subgoalno);
immler@31752
   118
    val the_filtered_clauses =
wenzelm@32942
   119
      (case filtered_clauses of
wenzelm@32942
   120
        NONE => relevance_filter goal goal_cls
wenzelm@32942
   121
      | SOME fcls => fcls);
immler@31409
   122
    val the_axiom_clauses =
wenzelm@32942
   123
      (case axiom_clauses of
wenzelm@32942
   124
        NONE => the_filtered_clauses
wenzelm@32942
   125
      | SOME axcls => axcls);
wenzelm@32257
   126
    val (thm_names, clauses) =
wenzelm@32942
   127
      prepare goal_cls chain_ths the_axiom_clauses the_filtered_clauses thy;
immler@31750
   128
boehmes@32864
   129
    (* path to unique problem file *)
wenzelm@32942
   130
    val destdir' = Config.get ctxt destdir;
wenzelm@32942
   131
    val problem_prefix' = Config.get ctxt problem_prefix;
boehmes@32864
   132
    fun prob_pathname nr =
wenzelm@32942
   133
      let val probfile =
wenzelm@32942
   134
        Path.basic (problem_prefix' ^ serial_string () ^ "_" ^ string_of_int nr)
wenzelm@32942
   135
      in
wenzelm@32942
   136
        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')
wenzelm@32942
   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
wenzelm@32942
   147
        val split = String.tokens (fn c => str c = "\n");
wenzelm@32942
   148
        val (proof, t) = s |> split |> split_last |> apfst cat_lines;
wenzelm@32942
   149
        fun as_num f = f >> (fst o read_int);
wenzelm@32942
   150
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
wenzelm@32942
   151
        val digit = Scan.one Symbol.is_ascii_digit;
wenzelm@32942
   152
        val num3 = as_num (digit ::: digit ::: (digit >> single));
wenzelm@32942
   153
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
wenzelm@32942
   154
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
wenzelm@32942
   155
      in (proof, as_time t) end;
boehmes@32458
   156
    fun run_on probfile =
wenzelm@32942
   157
      if File.exists cmd then
wenzelm@32942
   158
        write probfile clauses
boehmes@32510
   159
        |> pair (apfst split_time (system_out (cmd_line probfile)))
wenzelm@32942
   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 *)
wenzelm@32942
   163
    fun cleanup probfile = if destdir' = "" then try File.rm probfile else NONE;
wenzelm@32942
   164
    fun export probfile (((proof, _), _), _) =
wenzelm@32942
   165
      if destdir' = "" then ()
wenzelm@32942
   166
      else File.write (Path.explode (Path.implode probfile ^ "_proof")) proof;
wenzelm@32257
   167
wenzelm@32942
   168
    val (((proof, time), rc), conj_pos) =
wenzelm@32942
   169
      with_path cleanup export run_on (prob_pathname subgoalno);
boehmes@32458
   170
immler@29590
   171
    (* check for success and print out some information on failure *)
wenzelm@32942
   172
    val failure = find_failure proof;
wenzelm@32942
   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)
wenzelm@32942
   178
        (produce_answer name (proof, thm_names, conj_pos, ctxt, th, subgoalno));
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}
wenzelm@32942
   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@32942
   191
      prover_config;
wenzelm@32942
   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
wenzelm@32942
   208
  end;
wenzelm@28596
   209
wenzelm@32942
   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
wenzelm@32942
   220
val vampire_max_new_clauses = 60;
wenzelm@32942
   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@32942
   229
  emit_structured_proof = full};
wenzelm@28596
   230
wenzelm@32942
   231
val vampire = tptp_prover ("vampire", vampire_prover_config false);
wenzelm@32942
   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
wenzelm@32942
   237
val eprover_max_new_clauses = 100;
wenzelm@32942
   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@32942
   246
  emit_structured_proof = full};
wenzelm@28596
   247
wenzelm@32942
   248
val eprover = tptp_prover ("e", eprover_config false);
wenzelm@32942
   249
val eprover_full = tptp_prover ("e_full", eprover_config true);
wenzelm@28596
   250
wenzelm@28596
   251
wenzelm@28596
   252
(* SPASS *)
wenzelm@28592
   253
wenzelm@32942
   254
val spass_max_new_clauses = 40;
wenzelm@32942
   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@32942
   263
  emit_structured_proof = false};
boehmes@32864
   264
wenzelm@32942
   265
fun gen_dfg_prover (name, prover_config: prover_config) problem timeout =
boehmes@32869
   266
  let
wenzelm@32942
   267
    val {max_new_clauses, insert_theory_const, command, arguments, ...} = prover_config
wenzelm@32941
   268
    val {with_full_types, subgoal, goal, axiom_clauses, filtered_clauses} = problem
boehmes@32869
   269
  in
boehmes@32869
   270
    external_prover
boehmes@32869
   271
      (ResAtp.get_relevant max_new_clauses insert_theory_const)
boehmes@32869
   272
      (ResAtp.prepare_clauses true)
boehmes@32869
   273
      (ResHolClause.dfg_write_file with_full_types)
boehmes@32869
   274
      command
boehmes@32869
   275
      (arguments timeout)
boehmes@32869
   276
      ResReconstruct.find_failure
boehmes@32869
   277
      (ResReconstruct.lemma_list true)
boehmes@32869
   278
      axiom_clauses
boehmes@32869
   279
      filtered_clauses
boehmes@32869
   280
      name
boehmes@32869
   281
      subgoal
boehmes@32869
   282
      goal
wenzelm@32942
   283
  end;
boehmes@32869
   284
wenzelm@32942
   285
fun dfg_prover (name, config) = (name, gen_dfg_prover (name, config));
boehmes@32869
   286
wenzelm@32942
   287
val spass = dfg_prover ("spass", spass_config spass_insert_theory_const);
wenzelm@32942
   288
val spass_no_tc = dfg_prover ("spass_no_tc", spass_config false);
wenzelm@28592
   289
wenzelm@28596
   290
wenzelm@28596
   291
(* remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   292
wenzelm@32942
   293
val systems = Synchronized.var "atp_wrapper_systems" ([]: string list);
immler@31835
   294
immler@31835
   295
fun get_systems () =
immler@31835
   296
  let
wenzelm@32327
   297
    val (answer, rc) = system_out ("\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w")
immler@31835
   298
  in
wenzelm@32327
   299
    if rc <> 0 then error ("Failed to get available systems from SystemOnTPTP:\n" ^ answer)
immler@31835
   300
    else split_lines answer
immler@31835
   301
  end;
immler@31835
   302
wenzelm@32942
   303
fun refresh_systems () = Synchronized.change systems (fn _ => get_systems ());
immler@31835
   304
immler@31835
   305
fun get_system prefix = Synchronized.change_result systems (fn systems =>
boehmes@32864
   306
  (if null systems then get_systems () else systems)
wenzelm@32942
   307
  |> `(find_first (String.isPrefix prefix)));
boehmes@32864
   308
boehmes@32864
   309
fun get_the_system prefix =
boehmes@32864
   310
  (case get_system prefix of
boehmes@32864
   311
    NONE => error ("No system like " ^ quote prefix ^ " at SystemOnTPTP")
wenzelm@32942
   312
  | SOME sys => sys);
immler@31835
   313
wenzelm@32941
   314
fun remote_prover_config prover_prefix args max_new insert_tc: prover_config =
wenzelm@32941
   315
 {command = Path.explode "$ISABELLE_ATP_MANAGER/SystemOnTPTP",
wenzelm@32942
   316
  arguments =
wenzelm@32942
   317
    (fn timeout => args ^ " -t " ^ string_of_int timeout ^ " -s " ^ get_the_system prover_prefix),
boehmes@32864
   318
  max_new_clauses = max_new,
boehmes@32864
   319
  insert_theory_const = insert_tc,
wenzelm@32942
   320
  emit_structured_proof = false};
wenzelm@28596
   321
boehmes@32864
   322
val remote_vampire = tptp_prover ("remote_vampire", remote_prover_config
wenzelm@32942
   323
  "Vampire---9" "" vampire_max_new_clauses vampire_insert_theory_const);
boehmes@32864
   324
boehmes@32864
   325
val remote_eprover = tptp_prover ("remote_e", remote_prover_config
wenzelm@32942
   326
  "EP---" "" eprover_max_new_clauses eprover_insert_theory_const);
boehmes@32864
   327
boehmes@32864
   328
val remote_spass = tptp_prover ("remote_spass", remote_prover_config
wenzelm@32942
   329
  "SPASS---" "-x" spass_max_new_clauses spass_insert_theory_const);
wenzelm@28592
   330
wenzelm@28592
   331
end;