src/HOL/Tools/atp_wrapper.ML
author wenzelm
Tue Jul 28 18:17:35 2009 +0200 (2009-07-28)
changeset 32257 bad5a99c16d8
parent 32091 30e2ffbba718
permissions -rw-r--r--
neg_conjecture_clauses, neg_clausify_tac: proper context, eliminated METAHYPS;
external_prover: neg_conjecture_clauses should handle TVars within goals;
misc tuning;
wenzelm@28592
     1
(*  Title:      HOL/Tools/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
wenzelm@28592
     9
  val destdir: string ref
wenzelm@28592
    10
  val problem_name: string ref
wenzelm@28596
    11
  val tptp_prover_opts_full: int -> bool -> bool -> Path.T * string -> AtpManager.prover
wenzelm@28596
    12
  val tptp_prover_opts: int -> bool -> Path.T * string -> AtpManager.prover
wenzelm@28596
    13
  val tptp_prover: Path.T * string -> AtpManager.prover
wenzelm@28596
    14
  val full_prover_opts: int -> bool -> Path.T * string -> AtpManager.prover
wenzelm@28596
    15
  val full_prover: Path.T * string  -> AtpManager.prover
wenzelm@28596
    16
  val vampire_opts: int -> bool -> AtpManager.prover
wenzelm@28596
    17
  val vampire: AtpManager.prover
wenzelm@28596
    18
  val vampire_opts_full: int -> bool -> AtpManager.prover
wenzelm@28596
    19
  val vampire_full: AtpManager.prover
wenzelm@28596
    20
  val eprover_opts: int -> bool  -> AtpManager.prover
wenzelm@28596
    21
  val eprover: AtpManager.prover
wenzelm@28596
    22
  val eprover_opts_full: int -> bool -> AtpManager.prover
wenzelm@28596
    23
  val eprover_full: AtpManager.prover
wenzelm@28596
    24
  val spass_opts: int -> bool  -> AtpManager.prover
wenzelm@28596
    25
  val spass: AtpManager.prover
immler@31835
    26
  val remote_prover_opts: int -> bool -> string -> string -> AtpManager.prover
immler@31835
    27
  val remote_prover: string -> string -> AtpManager.prover
immler@31835
    28
  val refresh_systems: unit -> unit
wenzelm@28592
    29
end;
wenzelm@28592
    30
wenzelm@28592
    31
structure AtpWrapper: ATP_WRAPPER =
wenzelm@28592
    32
struct
wenzelm@28596
    33
wenzelm@28596
    34
(** generic ATP wrapper **)
wenzelm@28596
    35
wenzelm@28596
    36
(* global hooks for writing problemfiles *)
wenzelm@28596
    37
wenzelm@28596
    38
val destdir = ref "";   (*Empty means write files to /tmp*)
wenzelm@28596
    39
val problem_name = ref "prob";
wenzelm@28596
    40
wenzelm@28596
    41
wenzelm@28596
    42
(* basic template *)
wenzelm@28596
    43
immler@31409
    44
fun external_prover relevance_filter preparer writer (cmd, args) find_failure produce_answer
immler@31752
    45
  timeout axiom_clauses filtered_clauses name subgoalno goal =
wenzelm@28596
    46
  let
wenzelm@28596
    47
    (* path to unique problem file *)
wenzelm@28592
    48
    val destdir' = ! destdir
wenzelm@28592
    49
    val problem_name' = ! problem_name
wenzelm@28592
    50
    fun prob_pathname nr =
wenzelm@28596
    51
      let val probfile = Path.basic (problem_name' ^ serial_string () ^ "_" ^ string_of_int nr)
wenzelm@28592
    52
      in if destdir' = "" then File.tmp_path probfile
wenzelm@28592
    53
        else if File.exists (Path.explode (destdir'))
wenzelm@28592
    54
        then Path.append  (Path.explode (destdir')) probfile
wenzelm@28592
    55
        else error ("No such directory: " ^ destdir')
wenzelm@28592
    56
      end
wenzelm@28596
    57
immler@31750
    58
    (* get clauses and prepare them for writing *)
immler@30537
    59
    val (ctxt, (chain_ths, th)) = goal
immler@30536
    60
    val thy = ProofContext.theory_of ctxt
wenzelm@28596
    61
    val chain_ths = map (Thm.put_name_hint ResReconstruct.chained_hint) chain_ths
wenzelm@32257
    62
    val goal_cls = #1 (ResAxioms.neg_conjecture_clauses ctxt th subgoalno)
wenzelm@32091
    63
    val _ = app (fn th => Output.debug (fn _ => Display.string_of_thm ctxt th)) goal_cls
immler@31752
    64
    val the_filtered_clauses =
immler@31752
    65
      case filtered_clauses of
immler@31752
    66
          NONE => relevance_filter goal goal_cls
immler@31752
    67
        | SOME fcls => fcls
immler@31409
    68
    val the_axiom_clauses =
immler@31409
    69
      case axiom_clauses of
immler@31752
    70
          NONE => the_filtered_clauses
immler@31409
    71
        | SOME axcls => axcls
wenzelm@32257
    72
    val (thm_names, clauses) =
wenzelm@32257
    73
      preparer goal_cls chain_ths the_axiom_clauses the_filtered_clauses thy
immler@31750
    74
immler@31750
    75
    (* write out problem file and call prover *)
immler@31750
    76
    val probfile = prob_pathname subgoalno
immler@31840
    77
    val conj_pos = writer probfile clauses
immler@31838
    78
    val (proof, rc) = system_out (
immler@31838
    79
      if File.exists cmd then
immler@31838
    80
        space_implode " " ["exec", File.shell_path cmd, args, File.platform_path probfile]
immler@31838
    81
      else error ("Bad executable: " ^ Path.implode cmd))
wenzelm@28592
    82
immler@31751
    83
    (* if problemfile has not been exported, delete problemfile; otherwise export proof, too *)
immler@31751
    84
    val _ =
immler@31838
    85
      if destdir' = "" then File.rm probfile
immler@31838
    86
      else File.write (Path.explode (Path.implode probfile ^ "_proof")) proof
wenzelm@32257
    87
immler@29590
    88
    (* check for success and print out some information on failure *)
immler@29590
    89
    val failure = find_failure proof
immler@29597
    90
    val success = rc = 0 andalso is_none failure
wenzelm@28596
    91
    val message =
wenzelm@30896
    92
      if is_some failure then "External prover failed."
wenzelm@30896
    93
      else if rc <> 0 then "External prover failed: " ^ proof
immler@31840
    94
      else "Try this command: " ^
immler@31840
    95
        produce_answer name (proof, thm_names, conj_pos, ctxt, th, subgoalno)
immler@31411
    96
    val _ = Output.debug (fn () => "Sledgehammer response (rc = " ^ string_of_int rc ^ "):\n" ^ proof)
immler@31752
    97
  in (success, message, proof, thm_names, the_filtered_clauses) end;
wenzelm@28596
    98
wenzelm@28592
    99
wenzelm@28596
   100
wenzelm@28596
   101
(** common provers **)
wenzelm@28596
   102
wenzelm@28596
   103
(* generic TPTP-based provers *)
wenzelm@28596
   104
immler@31752
   105
fun tptp_prover_opts_full max_new theory_const full command timeout ax_clauses fcls name n goal =
wenzelm@28596
   106
  external_prover
immler@31409
   107
  (ResAtp.get_relevant max_new theory_const)
immler@31409
   108
  (ResAtp.prepare_clauses false)
nipkow@31791
   109
  (ResHolClause.tptp_write_file (AtpManager.get_full_types()))
immler@31409
   110
  command
immler@31409
   111
  ResReconstruct.find_failure
immler@31840
   112
  (if full then ResReconstruct.structured_proof else ResReconstruct.lemma_list false)
immler@31752
   113
  timeout ax_clauses fcls name n goal;
wenzelm@28596
   114
wenzelm@28596
   115
(*arbitrary ATP with TPTP input/output and problemfile as last argument*)
wenzelm@28596
   116
fun tptp_prover_opts max_new theory_const =
wenzelm@28596
   117
  tptp_prover_opts_full max_new theory_const false;
wenzelm@28596
   118
wenzelm@31368
   119
fun tptp_prover x = tptp_prover_opts 60 true x;
wenzelm@28596
   120
wenzelm@28596
   121
(*for structured proofs: prover must support TSTP*)
wenzelm@28596
   122
fun full_prover_opts max_new theory_const =
wenzelm@28596
   123
  tptp_prover_opts_full max_new theory_const true;
wenzelm@28596
   124
wenzelm@31368
   125
fun full_prover x = full_prover_opts 60 true x;
wenzelm@28596
   126
wenzelm@28592
   127
wenzelm@28596
   128
(* Vampire *)
wenzelm@28596
   129
wenzelm@28596
   130
(*NB: Vampire does not work without explicit timelimit*)
wenzelm@28596
   131
immler@29593
   132
fun vampire_opts max_new theory_const timeout = tptp_prover_opts
wenzelm@28596
   133
  max_new theory_const
immler@29593
   134
  (Path.explode "$VAMPIRE_HOME/vampire",
wenzelm@32257
   135
    ("--output_syntax tptp --mode casc -t " ^ string_of_int timeout))
immler@29593
   136
  timeout;
wenzelm@28596
   137
wenzelm@28596
   138
val vampire = vampire_opts 60 false;
wenzelm@28596
   139
immler@29593
   140
fun vampire_opts_full max_new theory_const timeout = full_prover_opts
wenzelm@28596
   141
  max_new theory_const
immler@29593
   142
  (Path.explode "$VAMPIRE_HOME/vampire",
wenzelm@32257
   143
    ("--output_syntax tptp --mode casc -t " ^ string_of_int timeout))
immler@29593
   144
  timeout;
wenzelm@28596
   145
immler@31832
   146
val vampire_full = vampire_opts_full 60 false;
wenzelm@28596
   147
wenzelm@28592
   148
wenzelm@28596
   149
(* E prover *)
wenzelm@28596
   150
immler@30536
   151
fun eprover_opts max_new theory_const timeout = tptp_prover_opts
wenzelm@28596
   152
  max_new theory_const
immler@30536
   153
  (Path.explode "$E_HOME/eproof",
immler@30536
   154
    "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev --silent --cpu-limit=" ^ string_of_int timeout)
immler@30536
   155
  timeout;
wenzelm@28596
   156
wenzelm@28596
   157
val eprover = eprover_opts 100 false;
wenzelm@28596
   158
immler@30536
   159
fun eprover_opts_full max_new theory_const timeout = full_prover_opts
wenzelm@28596
   160
  max_new theory_const
immler@30536
   161
  (Path.explode "$E_HOME/eproof",
immler@30536
   162
    "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev --silent --cpu-limit=" ^ string_of_int timeout)
immler@30536
   163
  timeout;
wenzelm@28596
   164
wenzelm@28596
   165
val eprover_full = eprover_opts_full 100 false;
wenzelm@28596
   166
wenzelm@28596
   167
wenzelm@28596
   168
(* SPASS *)
wenzelm@28592
   169
immler@31752
   170
fun spass_opts max_new theory_const timeout ax_clauses fcls name n goal = external_prover
immler@31409
   171
  (ResAtp.get_relevant max_new theory_const)
immler@31409
   172
  (ResAtp.prepare_clauses true)
nipkow@31791
   173
  (ResHolClause.dfg_write_file (AtpManager.get_full_types()))
immler@30536
   174
  (Path.explode "$SPASS_HOME/SPASS",
wenzelm@32257
   175
    "-Auto -SOS=1 -PGiven=0 -PProblem=0 -Splits=0 -FullRed=0 -DocProof -TimeLimit=" ^
wenzelm@32257
   176
      string_of_int timeout)
immler@30874
   177
  ResReconstruct.find_failure
immler@31840
   178
  (ResReconstruct.lemma_list true)
immler@31752
   179
  timeout ax_clauses fcls name n goal;
wenzelm@28596
   180
wenzelm@28596
   181
val spass = spass_opts 40 true;
wenzelm@28592
   182
wenzelm@28596
   183
wenzelm@28596
   184
(* remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   185
immler@31835
   186
val systems =
immler@31835
   187
  Synchronized.var "atp_wrapper_systems" ([]: string list);
immler@31835
   188
immler@31835
   189
fun get_systems () =
immler@31835
   190
  let
immler@31835
   191
    val (answer, rc) = system_out (("$ISABELLE_HOME/lib/scripts/SystemOnTPTP" |>
immler@31835
   192
      Path.explode |> File.shell_path) ^ " -w")
immler@31835
   193
  in
immler@31835
   194
    if rc <> 0 then error ("Get available systems from SystemOnTPTP:\n" ^ answer)
immler@31835
   195
    else split_lines answer
immler@31835
   196
  end;
immler@31835
   197
immler@31835
   198
fun refresh_systems () = Synchronized.change systems (fn _ =>
wenzelm@32257
   199
  get_systems ());
immler@31835
   200
immler@31835
   201
fun get_system prefix = Synchronized.change_result systems (fn systems =>
immler@31835
   202
  let val systems = if null systems then get_systems() else systems
immler@31835
   203
  in (find_first (String.isPrefix prefix) systems, systems) end);
immler@31835
   204
immler@31835
   205
fun remote_prover_opts max_new theory_const args prover_prefix timeout =
wenzelm@32257
   206
  let val sys =
wenzelm@32257
   207
    case get_system prover_prefix of
immler@31835
   208
      NONE => error ("No system like " ^ quote prover_prefix ^ " at SystemOnTPTP")
immler@31835
   209
    | SOME sys => sys
immler@31835
   210
  in tptp_prover_opts max_new theory_const
immler@31835
   211
    (Path.explode "$ISABELLE_HOME/lib/scripts/SystemOnTPTP",
wenzelm@32257
   212
      args ^ " -t " ^ string_of_int timeout ^ " -s " ^ sys) timeout
wenzelm@32257
   213
  end;
wenzelm@28596
   214
wenzelm@28596
   215
val remote_prover = remote_prover_opts 60 false;
wenzelm@28592
   216
wenzelm@28592
   217
end;
immler@30536
   218