src/HOL/Tools/atp_wrapper.ML
author immler@in.tum.de
Sun Jun 28 15:01:28 2009 +0200 (2009-06-28)
changeset 31835 b686d4df54c2
parent 31832 db3f00a39edd
child 31838 607a984b70e3
permissions -rw-r--r--
check for current versions on server
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
immler@31409
    62
    val goal_cls = #1 (ResAxioms.neg_conjecture_clauses th subgoalno)
immler@31409
    63
      handle THM ("assume: variables", _, _) =>
immler@31409
    64
        error "Sledgehammer: Goal contains type variables (TVars)"
immler@31409
    65
    val _ = app (fn th => Output.debug (fn _ => Display.string_of_thm th)) goal_cls
immler@31752
    66
    val the_filtered_clauses =
immler@31752
    67
      case filtered_clauses of
immler@31752
    68
          NONE => relevance_filter goal goal_cls
immler@31752
    69
        | SOME fcls => fcls
immler@31409
    70
    val the_axiom_clauses =
immler@31409
    71
      case axiom_clauses of
immler@31752
    72
          NONE => the_filtered_clauses
immler@31409
    73
        | SOME axcls => axcls
immler@31752
    74
    val (thm_names, clauses) = preparer goal_cls chain_ths the_axiom_clauses the_filtered_clauses thy
immler@31750
    75
immler@31750
    76
    (* write out problem file and call prover *)
immler@31750
    77
    val probfile = prob_pathname subgoalno
immler@31750
    78
    val fname = File.platform_path probfile
immler@31752
    79
    val _ = writer fname clauses
wenzelm@28596
    80
    val cmdline =
wenzelm@30899
    81
      if File.exists cmd then "exec " ^ File.shell_path cmd ^ " " ^ args
wenzelm@28596
    82
      else error ("Bad executable: " ^ Path.implode cmd)
immler@30536
    83
    val (proof, rc) = system_out (cmdline ^ " " ^ fname)
wenzelm@28592
    84
immler@31751
    85
    (* if problemfile has not been exported, delete problemfile; otherwise export proof, too *)
immler@31751
    86
    val _ =
immler@31751
    87
      if destdir' = "" then OS.FileSys.remove fname
immler@31751
    88
      else
immler@31751
    89
        let val out = TextIO.openOut (fname ^ "_proof")
immler@31751
    90
        val _ = TextIO.output (out, proof)
immler@31751
    91
        in TextIO.closeOut out end
immler@29590
    92
    
immler@29590
    93
    (* check for success and print out some information on failure *)
immler@29590
    94
    val failure = find_failure proof
immler@29597
    95
    val success = rc = 0 andalso is_none failure
wenzelm@28596
    96
    val message =
wenzelm@30896
    97
      if is_some failure then "External prover failed."
wenzelm@30896
    98
      else if rc <> 0 then "External prover failed: " ^ proof
immler@31037
    99
      else "Try this command: " ^ produce_answer name (proof, thm_names, ctxt, th, subgoalno)
immler@31411
   100
    val _ = Output.debug (fn () => "Sledgehammer response (rc = " ^ string_of_int rc ^ "):\n" ^ proof)
immler@31752
   101
  in (success, message, proof, thm_names, the_filtered_clauses) end;
wenzelm@28596
   102
wenzelm@28592
   103
wenzelm@28596
   104
wenzelm@28596
   105
(** common provers **)
wenzelm@28596
   106
wenzelm@28596
   107
(* generic TPTP-based provers *)
wenzelm@28596
   108
immler@31752
   109
fun tptp_prover_opts_full max_new theory_const full command timeout ax_clauses fcls name n goal =
wenzelm@28596
   110
  external_prover
immler@31409
   111
  (ResAtp.get_relevant max_new theory_const)
immler@31409
   112
  (ResAtp.prepare_clauses false)
nipkow@31791
   113
  (ResHolClause.tptp_write_file (AtpManager.get_full_types()))
immler@31409
   114
  command
immler@31409
   115
  ResReconstruct.find_failure
immler@31409
   116
  (if full then ResReconstruct.structured_proof else ResReconstruct.lemma_list_tstp)
immler@31752
   117
  timeout ax_clauses fcls name n goal;
wenzelm@28596
   118
wenzelm@28596
   119
(*arbitrary ATP with TPTP input/output and problemfile as last argument*)
wenzelm@28596
   120
fun tptp_prover_opts max_new theory_const =
wenzelm@28596
   121
  tptp_prover_opts_full max_new theory_const false;
wenzelm@28596
   122
wenzelm@31368
   123
fun tptp_prover x = tptp_prover_opts 60 true x;
wenzelm@28596
   124
wenzelm@28596
   125
(*for structured proofs: prover must support TSTP*)
wenzelm@28596
   126
fun full_prover_opts max_new theory_const =
wenzelm@28596
   127
  tptp_prover_opts_full max_new theory_const true;
wenzelm@28596
   128
wenzelm@31368
   129
fun full_prover x = full_prover_opts 60 true x;
wenzelm@28596
   130
wenzelm@28592
   131
wenzelm@28596
   132
(* Vampire *)
wenzelm@28596
   133
wenzelm@28596
   134
(*NB: Vampire does not work without explicit timelimit*)
wenzelm@28596
   135
immler@29593
   136
fun vampire_opts max_new theory_const timeout = tptp_prover_opts
wenzelm@28596
   137
  max_new theory_const
immler@29593
   138
  (Path.explode "$VAMPIRE_HOME/vampire",
immler@29593
   139
               ("--output_syntax tptp --mode casc -t " ^ string_of_int timeout))
immler@29593
   140
  timeout;
wenzelm@28596
   141
wenzelm@28596
   142
val vampire = vampire_opts 60 false;
wenzelm@28596
   143
immler@29593
   144
fun vampire_opts_full max_new theory_const timeout = full_prover_opts
wenzelm@28596
   145
  max_new theory_const
immler@29593
   146
  (Path.explode "$VAMPIRE_HOME/vampire",
immler@29593
   147
               ("--output_syntax tptp --mode casc -t " ^ string_of_int timeout))
immler@29593
   148
  timeout;
wenzelm@28596
   149
immler@31832
   150
val vampire_full = vampire_opts_full 60 false;
wenzelm@28596
   151
wenzelm@28592
   152
wenzelm@28596
   153
(* E prover *)
wenzelm@28596
   154
immler@30536
   155
fun eprover_opts max_new theory_const timeout = tptp_prover_opts
wenzelm@28596
   156
  max_new theory_const
immler@30536
   157
  (Path.explode "$E_HOME/eproof",
immler@30536
   158
    "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev --silent --cpu-limit=" ^ string_of_int timeout)
immler@30536
   159
  timeout;
wenzelm@28596
   160
wenzelm@28596
   161
val eprover = eprover_opts 100 false;
wenzelm@28596
   162
immler@30536
   163
fun eprover_opts_full max_new theory_const timeout = full_prover_opts
wenzelm@28596
   164
  max_new theory_const
immler@30536
   165
  (Path.explode "$E_HOME/eproof",
immler@30536
   166
    "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev --silent --cpu-limit=" ^ string_of_int timeout)
immler@30536
   167
  timeout;
wenzelm@28596
   168
wenzelm@28596
   169
val eprover_full = eprover_opts_full 100 false;
wenzelm@28596
   170
wenzelm@28596
   171
wenzelm@28596
   172
(* SPASS *)
wenzelm@28592
   173
immler@31752
   174
fun spass_opts max_new theory_const timeout ax_clauses fcls name n goal = external_prover
immler@31409
   175
  (ResAtp.get_relevant max_new theory_const)
immler@31409
   176
  (ResAtp.prepare_clauses true)
nipkow@31791
   177
  (ResHolClause.dfg_write_file (AtpManager.get_full_types()))
immler@30536
   178
  (Path.explode "$SPASS_HOME/SPASS",
immler@30536
   179
    "-Auto -SOS=1 -PGiven=0 -PProblem=0 -Splits=0 -FullRed=0 -DocProof -TimeLimit=" ^ string_of_int timeout)
immler@30874
   180
  ResReconstruct.find_failure
immler@30536
   181
  ResReconstruct.lemma_list_dfg
immler@31752
   182
  timeout ax_clauses fcls name n goal;
wenzelm@28596
   183
wenzelm@28596
   184
val spass = spass_opts 40 true;
wenzelm@28592
   185
wenzelm@28596
   186
wenzelm@28596
   187
(* remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   188
immler@31835
   189
val systems =
immler@31835
   190
  Synchronized.var "atp_wrapper_systems" ([]: string list);
immler@31835
   191
immler@31835
   192
fun get_systems () =
immler@31835
   193
  let
immler@31835
   194
    val (answer, rc) = system_out (("$ISABELLE_HOME/lib/scripts/SystemOnTPTP" |>
immler@31835
   195
      Path.explode |> File.shell_path) ^ " -w")
immler@31835
   196
  in
immler@31835
   197
    if rc <> 0 then error ("Get available systems from SystemOnTPTP:\n" ^ answer)
immler@31835
   198
    else split_lines answer
immler@31835
   199
  end;
immler@31835
   200
immler@31835
   201
fun refresh_systems () = Synchronized.change systems (fn _ =>
immler@31835
   202
  get_systems());
immler@31835
   203
immler@31835
   204
fun get_system prefix = Synchronized.change_result systems (fn systems =>
immler@31835
   205
  let val systems = if null systems then get_systems() else systems
immler@31835
   206
  in (find_first (String.isPrefix prefix) systems, systems) end);
immler@31835
   207
immler@31835
   208
fun remote_prover_opts max_new theory_const args prover_prefix timeout =
immler@31835
   209
  let val sys = case get_system prover_prefix of
immler@31835
   210
      NONE => error ("No system like " ^ quote prover_prefix ^ " at SystemOnTPTP")
immler@31835
   211
    | SOME sys => sys
immler@31835
   212
  in tptp_prover_opts max_new theory_const
immler@31835
   213
    (Path.explode "$ISABELLE_HOME/lib/scripts/SystemOnTPTP",
immler@31835
   214
      args ^ " -t " ^ string_of_int timeout ^ " -s " ^ sys) timeout end;
wenzelm@28596
   215
wenzelm@28596
   216
val remote_prover = remote_prover_opts 60 false;
wenzelm@28592
   217
wenzelm@28592
   218
end;
immler@30536
   219
immler@30536
   220