src/HOL/Tools/ATP_Manager/atp_systems.ML
author blanchet
Mon Jul 26 20:07:31 2010 +0200 (2010-07-26)
changeset 38001 a9b47b85ca24
parent 38000 c0b9efa8bfca
child 38002 31705eccee23
permissions -rw-r--r--
reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet@36376
     1
(*  Title:      HOL/Tools/ATP_Manager/atp_systems.ML
wenzelm@28592
     2
    Author:     Fabian Immler, TU Muenchen
blanchet@36371
     3
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@28592
     4
blanchet@36376
     5
Setup for supported ATPs.
wenzelm@28592
     6
*)
wenzelm@28592
     7
blanchet@36376
     8
signature ATP_SYSTEMS =
wenzelm@28592
     9
sig
blanchet@37623
    10
  val trace : bool Unsynchronized.ref
blanchet@36376
    11
  val dest_dir : string Config.T
blanchet@35867
    12
  val problem_prefix : string Config.T
blanchet@35867
    13
  val measure_runtime : bool Config.T
blanchet@35867
    14
  val refresh_systems_on_tptp : unit -> unit
blanchet@36371
    15
  val default_atps_param_value : unit -> string
blanchet@35867
    16
  val setup : theory -> theory
wenzelm@28592
    17
end;
wenzelm@28592
    18
blanchet@36376
    19
structure ATP_Systems : ATP_SYSTEMS =
wenzelm@28592
    20
struct
wenzelm@28596
    21
blanchet@37578
    22
open Metis_Clauses
blanchet@36187
    23
open Sledgehammer_Util
blanchet@35865
    24
open Sledgehammer_Fact_Filter
blanchet@37578
    25
open Sledgehammer_TPTP_Format
blanchet@35865
    26
open Sledgehammer_Proof_Reconstruct
blanchet@35867
    27
open ATP_Manager
blanchet@35826
    28
blanchet@37623
    29
val trace = Unsynchronized.ref false
blanchet@37623
    30
fun trace_msg msg = if !trace then tracing (msg ()) else ()
blanchet@37623
    31
blanchet@36376
    32
(** generic ATP **)
wenzelm@28596
    33
wenzelm@32944
    34
(* external problem files *)
boehmes@32864
    35
blanchet@36376
    36
val (dest_dir, dest_dir_setup) = Attrib.config_string "atp_dest_dir" (K "");
boehmes@32864
    37
  (*Empty string means create files in Isabelle's temporary files directory.*)
boehmes@32864
    38
boehmes@32864
    39
val (problem_prefix, problem_prefix_setup) =
wenzelm@36001
    40
  Attrib.config_string "atp_problem_prefix" (K "prob");
boehmes@32864
    41
boehmes@33247
    42
val (measure_runtime, measure_runtime_setup) =
wenzelm@36001
    43
  Attrib.config_bool "atp_measure_runtime" (K false);
boehmes@33247
    44
boehmes@32864
    45
blanchet@35867
    46
(* prover configuration *)
boehmes@32864
    47
wenzelm@32941
    48
type prover_config =
blanchet@36917
    49
  {home_var: string,
blanchet@36370
    50
   executable: string,
blanchet@37514
    51
   arguments: bool -> Time.time -> string,
blanchet@36370
    52
   proof_delims: (string * string) list,
blanchet@36370
    53
   known_failures: (failure * string) list,
blanchet@36382
    54
   max_axiom_clauses: int,
blanchet@37994
    55
   prefers_theory_relevant: bool,
blanchet@37994
    56
   explicit_forall: bool}
wenzelm@28596
    57
wenzelm@28596
    58
wenzelm@28596
    59
(* basic template *)
wenzelm@28596
    60
blanchet@36370
    61
val remotify = prefix "remote_"
blanchet@36370
    62
boehmes@32458
    63
fun with_path cleanup after f path =
boehmes@32458
    64
  Exn.capture f path
boehmes@32458
    65
  |> tap (fn _ => cleanup path)
boehmes@32458
    66
  |> Exn.release
blanchet@36369
    67
  |> tap (after path)
blanchet@36369
    68
blanchet@36369
    69
(* Splits by the first possible of a list of delimiters. *)
blanchet@36369
    70
fun extract_proof delims output =
blanchet@36369
    71
  case pairself (find_first (fn s => String.isSubstring s output))
blanchet@36369
    72
                (ListPair.unzip delims) of
blanchet@36369
    73
    (SOME begin_delim, SOME end_delim) =>
blanchet@36549
    74
    (output |> first_field begin_delim |> the |> snd
blanchet@36549
    75
            |> first_field end_delim |> the |> fst
blanchet@36549
    76
            |> first_field "\n" |> the |> snd
blanchet@36549
    77
     handle Option.Option => "")
blanchet@36369
    78
  | _ => ""
boehmes@32458
    79
blanchet@37514
    80
fun extract_proof_and_outcome complete res_code proof_delims known_failures
blanchet@37514
    81
                              output =
blanchet@36370
    82
  case map_filter (fn (failure, pattern) =>
blanchet@36370
    83
                      if String.isSubstring pattern output then SOME failure
blanchet@36370
    84
                      else NONE) known_failures of
blanchet@36369
    85
    [] => (case extract_proof proof_delims output of
blanchet@36370
    86
             "" => ("", SOME UnknownError)
blanchet@36370
    87
           | proof => if res_code = 0 then (proof, NONE)
blanchet@36370
    88
                      else ("", SOME UnknownError))
blanchet@37514
    89
  | (failure :: _) =>
blanchet@37514
    90
    ("", SOME (if failure = IncompleteUnprovable andalso complete then
blanchet@37514
    91
                 Unprovable
blanchet@37514
    92
               else
blanchet@37514
    93
                 failure))
blanchet@36370
    94
blanchet@36370
    95
fun string_for_failure Unprovable = "The ATP problem is unprovable."
blanchet@37413
    96
  | string_for_failure IncompleteUnprovable =
blanchet@37413
    97
    "The ATP cannot prove the problem."
blanchet@37627
    98
  | string_for_failure CantConnect = "Can't connect to remote ATP."
blanchet@36370
    99
  | string_for_failure TimedOut = "Timed out."
blanchet@36370
   100
  | string_for_failure OutOfResources = "The ATP ran out of resources."
blanchet@36370
   101
  | string_for_failure OldSpass =
blanchet@36393
   102
    (* FIXME: Change the error message below to point to the Isabelle download
blanchet@37997
   103
       page once the package is there. *)
blanchet@36370
   104
    "Warning: Sledgehammer requires a more recent version of SPASS with \
blanchet@36370
   105
    \support for the TPTP syntax. To install it, download and untar the \
blanchet@36370
   106
    \package \"http://isabelle.in.tum.de/~blanchet/spass-3.7.tgz\" and add the \
blanchet@36370
   107
    \\"spass-3.7\" directory's full path to \"" ^
blanchet@36370
   108
    Path.implode (Path.expand (Path.appends
blanchet@36370
   109
        (Path.variable "ISABELLE_HOME_USER" ::
blanchet@36370
   110
         map Path.basic ["etc", "components"]))) ^
blanchet@36370
   111
    "\" on a line of its own."
blanchet@36965
   112
  | string_for_failure MalformedInput =
blanchet@36965
   113
    "Internal Sledgehammer error: The ATP problem is malformed. Please report \
blanchet@36965
   114
    \this to the Isabelle developers."
blanchet@36370
   115
  | string_for_failure MalformedOutput = "Error: The ATP output is malformed."
blanchet@36370
   116
  | string_for_failure UnknownError = "Error: An unknown ATP error occurred."
blanchet@35865
   117
blanchet@37623
   118
blanchet@37623
   119
(* Clause preparation *)
blanchet@37623
   120
blanchet@37623
   121
fun make_clause_table xs =
blanchet@37623
   122
  fold (Termtab.update o `(prop_of o snd)) xs Termtab.empty
blanchet@37623
   123
blanchet@37623
   124
(* Remove existing axiom clauses from the conjecture clauses, as this can
blanchet@37623
   125
   dramatically boost an ATP's performance (for some reason). *)
blanchet@37623
   126
fun subtract_cls ax_clauses =
blanchet@37623
   127
  filter_out (Termtab.defined (make_clause_table ax_clauses) o prop_of)
blanchet@37623
   128
blanchet@37995
   129
fun combformula_for_prop thy =
blanchet@37995
   130
  let
blanchet@37995
   131
    val do_term = combterm_from_term thy
blanchet@37995
   132
    fun do_quant bs q s T t' =
blanchet@37995
   133
      do_formula ((s, T) :: bs) t'
blanchet@37995
   134
      #>> (fn phi => AQuant (q, [`make_bound_var s], phi))
blanchet@37995
   135
    and do_conn bs c t1 t2 =
blanchet@37995
   136
      do_formula bs t1 ##>> do_formula bs t2
blanchet@37995
   137
      #>> (fn (phi1, phi2) => AConn (c, [phi1, phi2]))
blanchet@37995
   138
    and do_formula bs t =
blanchet@37995
   139
      case t of
blanchet@38001
   140
        @{const Not} $ t1 =>
blanchet@37995
   141
        do_formula bs t1 #>> (fn phi => AConn (ANot, [phi]))
blanchet@37995
   142
      | Const (@{const_name All}, _) $ Abs (s, T, t') =>
blanchet@37995
   143
        do_quant bs AForall s T t'
blanchet@37995
   144
      | Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
blanchet@37995
   145
        do_quant bs AExists s T t'
blanchet@37995
   146
      | @{const "op &"} $ t1 $ t2 => do_conn bs AAnd t1 t2
blanchet@37995
   147
      | @{const "op |"} $ t1 $ t2 => do_conn bs AOr t1 t2
blanchet@37995
   148
      | @{const "op -->"} $ t1 $ t2 => do_conn bs AImplies t1 t2
blanchet@37995
   149
      | Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
blanchet@37995
   150
        do_conn bs AIff t1 t2
blanchet@37995
   151
      | _ => (fn ts => do_term bs (Envir.eta_contract t)
blanchet@37995
   152
                       |>> APred ||> union (op =) ts)
blanchet@37995
   153
  in do_formula [] end
blanchet@37623
   154
blanchet@38001
   155
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@38001
   156
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@38001
   157
   the conclusion variable to False. (Cf. "transform_elim_term" in
blanchet@38001
   158
   "ATP_Systems".) *)
blanchet@38001
   159
(* FIXME: test! *)
blanchet@38001
   160
fun transform_elim_term t =
blanchet@38001
   161
  case Logic.strip_imp_concl t of
blanchet@38001
   162
    @{const Trueprop} $ Var (z, @{typ bool}) =>
blanchet@38001
   163
    subst_Vars [(z, @{const True})] t
blanchet@38001
   164
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop True})] t
blanchet@38001
   165
  | _ => t
blanchet@38001
   166
blanchet@38001
   167
(* Removes the lambdas from an equation of the form "t = (%x. u)".
blanchet@38001
   168
   (Cf. "extensionalize_theorem" in "Clausifier".) *)
blanchet@38000
   169
fun extensionalize_term t =
blanchet@38000
   170
  let
blanchet@38001
   171
    fun aux j (Const (@{const_name "op ="}, Type (_, [Type (_, [_, T']), _]))
blanchet@38001
   172
               $ t2 $ Abs (s, var_T, t')) =
blanchet@38001
   173
        let val var_t = Var (("x", j), var_T) in
blanchet@38000
   174
          Const (@{const_name "op ="}, T' --> T' --> HOLogic.boolT)
blanchet@38000
   175
            $ betapply (t2, var_t) $ subst_bound (var_t, t')
blanchet@38000
   176
          |> aux (j + 1)
blanchet@38000
   177
        end
blanchet@38000
   178
      | aux _ t = t
blanchet@38000
   179
  in aux (maxidx_of_term t + 1) t end
blanchet@38000
   180
blanchet@38001
   181
(* FIXME: Guarantee freshness *)
blanchet@38001
   182
fun concealed_bound_name j = "Sledgehammer" ^ Int.toString j
blanchet@38001
   183
fun conceal_bounds Ts t =
blanchet@38001
   184
  subst_bounds (map (Free o apfst concealed_bound_name)
blanchet@38001
   185
                    (length Ts - 1 downto 0 ~~ rev Ts), t)
blanchet@38001
   186
fun reveal_bounds Ts =
blanchet@38001
   187
  subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
blanchet@38001
   188
                    (0 upto length Ts - 1 ~~ Ts))
blanchet@38001
   189
blanchet@38001
   190
fun introduce_combinators_in_term thy t =
blanchet@38001
   191
  let
blanchet@38001
   192
    fun aux Ts t =
blanchet@38001
   193
      case t of
blanchet@38001
   194
        @{const Not} $ t1 => @{const Not} $ aux Ts t1
blanchet@38001
   195
      | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
blanchet@38001
   196
        t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38001
   197
      | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
blanchet@38001
   198
        t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38001
   199
      | (t0 as @{const "op &"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38001
   200
      | (t0 as @{const "op |"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38001
   201
      | (t0 as @{const "op -->"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38001
   202
      | (t0 as Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])))
blanchet@38001
   203
          $ t1 $ t2 =>
blanchet@38001
   204
        t0 $ aux Ts t1 $ aux Ts t2
blanchet@38001
   205
      | _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
blanchet@38001
   206
               t
blanchet@38001
   207
             else
blanchet@38001
   208
               t |> conceal_bounds Ts
blanchet@38001
   209
                 |> Envir.eta_contract
blanchet@38001
   210
                 |> cterm_of thy
blanchet@38001
   211
                 |> Clausifier.introduce_combinators_in_cterm
blanchet@38001
   212
                 |> prop_of |> Logic.dest_equals |> snd
blanchet@38001
   213
                 |> reveal_bounds Ts
blanchet@38001
   214
               handle THM (msg, _, _) =>
blanchet@38001
   215
                      (* A type variable of sort "{}" will make abstraction
blanchet@38001
   216
                         fail. *)
blanchet@38001
   217
                      t
blanchet@38001
   218
  in t |> not (Meson.is_fol_term thy t) ? aux [] end
blanchet@38001
   219
blanchet@37623
   220
(* making axiom and conjecture clauses *)
blanchet@37997
   221
fun make_clause thy (formula_id, formula_name, kind, t) =
blanchet@37623
   222
  let
blanchet@37995
   223
    (* ### FIXME: introduce combinators and perform other transformations
blanchet@37995
   224
       previously done by Clausifier.to_nnf *)
blanchet@38001
   225
    val t = t |> transform_elim_term
blanchet@38001
   226
              |> Object_Logic.atomize_term thy
blanchet@38000
   227
              |> extensionalize_term
blanchet@38001
   228
              |> introduce_combinators_in_term thy
blanchet@37995
   229
    val (combformula, ctypes_sorts) = combformula_for_prop thy t []
blanchet@37623
   230
  in
blanchet@37997
   231
    FOLFormula {formula_name = formula_name, formula_id = formula_id,
blanchet@37997
   232
                combformula = combformula, kind = kind,
blanchet@37997
   233
                ctypes_sorts = ctypes_sorts}
blanchet@37623
   234
  end
blanchet@37623
   235
blanchet@37997
   236
fun add_axiom_clause thy ((name, k), th) =
blanchet@37997
   237
  cons (name, make_clause thy (k, name, Axiom, prop_of th))
blanchet@37623
   238
blanchet@37997
   239
fun make_axiom_clauses thy clauses = fold_rev (add_axiom_clause thy) clauses []
blanchet@37623
   240
blanchet@37999
   241
fun make_conjecture_clauses thy ts =
blanchet@37999
   242
  map2 (fn j => fn t => make_clause thy (j, "conjecture", Conjecture, t))
blanchet@37999
   243
       (0 upto length ts - 1) ts
blanchet@37623
   244
blanchet@37623
   245
(** Helper clauses **)
blanchet@37623
   246
blanchet@37994
   247
fun count_combterm (CombConst ((s, _), _, _)) =
blanchet@37994
   248
    Symtab.map_entry s (Integer.add 1)
blanchet@37623
   249
  | count_combterm (CombVar _) = I
blanchet@37994
   250
  | count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
blanchet@37994
   251
fun count_combformula (AQuant (_, _, phi)) = count_combformula phi
blanchet@37994
   252
  | count_combformula (AConn (_, phis)) = fold count_combformula phis
blanchet@37994
   253
  | count_combformula (APred tm) = count_combterm tm
blanchet@37994
   254
fun count_fol_formula (FOLFormula {combformula, ...}) =
blanchet@37994
   255
  count_combformula combformula
blanchet@37623
   256
blanchet@37623
   257
fun cnf_helper_thms thy raw =
blanchet@37623
   258
  map (`Thm.get_name_hint)
blanchet@37926
   259
  #> (if raw then map (apfst (rpair 0)) else Clausifier.cnf_rules_pairs thy true)
blanchet@37623
   260
blanchet@37623
   261
val optional_helpers =
blanchet@37623
   262
  [(["c_COMBI", "c_COMBK"], (false, @{thms COMBI_def COMBK_def})),
blanchet@37623
   263
   (["c_COMBB", "c_COMBC"], (false, @{thms COMBB_def COMBC_def})),
blanchet@37623
   264
   (["c_COMBS"], (false, @{thms COMBS_def}))]
blanchet@37623
   265
val optional_typed_helpers =
blanchet@37623
   266
  [(["c_True", "c_False"], (true, @{thms True_or_False})),
blanchet@37623
   267
   (["c_If"], (true, @{thms if_True if_False True_or_False}))]
blanchet@37623
   268
val mandatory_helpers = @{thms fequal_imp_equal equal_imp_fequal}
blanchet@37623
   269
blanchet@37623
   270
val init_counters =
blanchet@37623
   271
  Symtab.make (maps (maps (map (rpair 0) o fst))
blanchet@37623
   272
                    [optional_helpers, optional_typed_helpers])
blanchet@37623
   273
blanchet@37623
   274
fun get_helper_clauses thy is_FO full_types conjectures axcls =
blanchet@37623
   275
  let
blanchet@37623
   276
    val axclauses = map snd (make_axiom_clauses thy axcls)
blanchet@37994
   277
    val ct = fold (fold count_fol_formula) [conjectures, axclauses]
blanchet@37994
   278
                  init_counters
blanchet@37623
   279
    fun is_needed c = the (Symtab.lookup ct c) > 0
blanchet@37623
   280
    val cnfs =
blanchet@37623
   281
      (optional_helpers
blanchet@37623
   282
       |> full_types ? append optional_typed_helpers
blanchet@37623
   283
       |> maps (fn (ss, (raw, ths)) =>
blanchet@37623
   284
                   if exists is_needed ss then cnf_helper_thms thy raw ths
blanchet@37623
   285
                   else []))
blanchet@37623
   286
      @ (if is_FO then [] else cnf_helper_thms thy false mandatory_helpers)
blanchet@37623
   287
  in map snd (make_axiom_clauses thy cnfs) end
blanchet@37623
   288
blanchet@38001
   289
fun negate_term (@{const Not} $ t) = t
blanchet@38001
   290
  | negate_term t = @{const Not} $ t
blanchet@37995
   291
blanchet@37623
   292
(* prepare for passing to writer,
blanchet@37623
   293
   create additional clauses based on the information from extra_cls *)
blanchet@37995
   294
fun prepare_clauses full_types hyp_ts concl_t axcls extra_cls thy =
blanchet@37623
   295
  let
blanchet@37995
   296
    val goal_t = Logic.list_implies (hyp_ts, concl_t)
blanchet@37995
   297
    val is_FO = Meson.is_fol_term thy goal_t
blanchet@37995
   298
    val _ = trace_msg (fn _ => Syntax.string_of_term_global thy goal_t)
blanchet@37995
   299
    val axtms = map (prop_of o snd) extra_cls
blanchet@37995
   300
    val subs = tfree_classes_of_terms [goal_t]
blanchet@37995
   301
    val supers = tvar_classes_of_terms axtms
blanchet@37995
   302
    val tycons = type_consts_of_terms thy (goal_t :: axtms)
blanchet@37623
   303
    (*TFrees in conjecture clauses; TVars in axiom clauses*)
blanchet@37995
   304
    val conjectures =
blanchet@38001
   305
      make_conjecture_clauses thy (map negate_term hyp_ts @ [concl_t])
blanchet@37623
   306
    val (_, extra_clauses) = ListPair.unzip (make_axiom_clauses thy extra_cls)
blanchet@37623
   307
    val (clnames, axiom_clauses) = ListPair.unzip (make_axiom_clauses thy axcls)
blanchet@37623
   308
    val helper_clauses =
blanchet@37623
   309
      get_helper_clauses thy is_FO full_types conjectures extra_cls
blanchet@37623
   310
    val (supers', arity_clauses) = make_arity_clauses thy tycons supers
blanchet@37925
   311
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@37623
   312
  in
blanchet@37623
   313
    (Vector.fromList clnames,
blanchet@37925
   314
      (conjectures, axiom_clauses, extra_clauses, helper_clauses,
blanchet@37925
   315
       class_rel_clauses, arity_clauses))
blanchet@37623
   316
  end
blanchet@37623
   317
blanchet@37962
   318
fun extract_clause_sequence output =
blanchet@37962
   319
  let
blanchet@37962
   320
    val tokens_of = String.tokens (not o Char.isAlphaNum)
blanchet@37962
   321
    fun extract_num ("clause" :: (ss as _ :: _)) =
blanchet@37962
   322
    Int.fromString (List.last ss)
blanchet@37962
   323
      | extract_num _ = NONE
blanchet@37962
   324
  in output |> split_lines |> map_filter (extract_num o tokens_of) end
blanchet@37962
   325
blanchet@37962
   326
val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
blanchet@37962
   327
blanchet@37962
   328
val parse_clause_formula_pair =
blanchet@37962
   329
  $$ "(" |-- scan_integer --| $$ "," -- Symbol.scan_id --| $$ ")"
blanchet@37962
   330
  --| Scan.option ($$ ",")
blanchet@37962
   331
val parse_clause_formula_relation =
blanchet@37962
   332
  Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
blanchet@37962
   333
  |-- Scan.repeat parse_clause_formula_pair
blanchet@37962
   334
val extract_clause_formula_relation =
blanchet@37962
   335
  Substring.full
blanchet@37962
   336
  #> Substring.position set_ClauseFormulaRelationN
blanchet@37962
   337
  #> snd #> Substring.string #> strip_spaces #> explode
blanchet@37962
   338
  #> parse_clause_formula_relation #> fst
blanchet@37962
   339
blanchet@37989
   340
fun repair_conjecture_shape_and_theorem_names output conjecture_shape
blanchet@37989
   341
                                              thm_names =
blanchet@37962
   342
  if String.isSubstring set_ClauseFormulaRelationN output then
blanchet@37962
   343
    let
blanchet@37996
   344
      val j0 = hd conjecture_shape
blanchet@37962
   345
      val seq = extract_clause_sequence output
blanchet@37962
   346
      val name_map = extract_clause_formula_relation output
blanchet@37989
   347
      fun renumber_conjecture j =
blanchet@37989
   348
        AList.find (op =) name_map (conjecture_prefix ^ Int.toString (j - j0))
blanchet@37989
   349
        |> the_single
blanchet@37989
   350
        |> (fn s => find_index (curry (op =) s) seq + 1)
blanchet@37962
   351
    in
blanchet@37996
   352
      (conjecture_shape |> map renumber_conjecture,
blanchet@37989
   353
       seq |> map (the o AList.lookup (op =) name_map)
blanchet@37989
   354
           |> map (fn s => case try (unprefix axiom_prefix) s of
blanchet@37989
   355
                             SOME s' => undo_ascii_of s'
blanchet@37989
   356
                           | NONE => "")
blanchet@37989
   357
           |> Vector.fromList)
blanchet@37962
   358
    end
blanchet@37962
   359
  else
blanchet@37989
   360
    (conjecture_shape, thm_names)
blanchet@37962
   361
blanchet@37623
   362
blanchet@37499
   363
(* generic TPTP-based provers *)
blanchet@37499
   364
blanchet@37499
   365
fun generic_tptp_prover
blanchet@37499
   366
        (name, {home_var, executable, arguments, proof_delims, known_failures,
blanchet@37994
   367
                max_axiom_clauses, prefers_theory_relevant, explicit_forall})
blanchet@37580
   368
        ({debug, overlord, full_types, explicit_apply, relevance_threshold,
blanchet@37580
   369
          relevance_convergence, theory_relevant, defs_relevant, isar_proof,
blanchet@37580
   370
          isar_shrink_factor, ...} : params)
blanchet@37499
   371
        minimize_command timeout
blanchet@35969
   372
        ({subgoal, goal, relevance_override, axiom_clauses, filtered_clauses}
blanchet@35969
   373
         : problem) =
wenzelm@28596
   374
  let
immler@31750
   375
    (* get clauses and prepare them for writing *)
blanchet@37498
   376
    val (ctxt, (_, th)) = goal;
wenzelm@32942
   377
    val thy = ProofContext.theory_of ctxt;
blanchet@37995
   378
    (* ### FIXME: (1) preprocessing for "if" etc. *)
blanchet@37995
   379
    val (params, hyp_ts, concl_t) = strip_subgoal th subgoal
immler@31752
   380
    val the_filtered_clauses =
blanchet@37621
   381
      case filtered_clauses of
blanchet@37621
   382
        SOME fcls => fcls
blanchet@37621
   383
      | NONE => relevant_facts full_types relevance_threshold
blanchet@37499
   384
                    relevance_convergence defs_relevant max_axiom_clauses
blanchet@37499
   385
                    (the_default prefers_theory_relevant theory_relevant)
blanchet@37995
   386
                    relevance_override goal hyp_ts concl_t
blanchet@37926
   387
                |> Clausifier.cnf_rules_pairs thy true
blanchet@37506
   388
    val the_axiom_clauses = axiom_clauses |> the_default the_filtered_clauses
blanchet@35969
   389
    val (internal_thm_names, clauses) =
blanchet@37995
   390
      prepare_clauses full_types hyp_ts concl_t the_axiom_clauses
blanchet@37995
   391
                      the_filtered_clauses thy
immler@31750
   392
boehmes@32864
   393
    (* path to unique problem file *)
blanchet@36376
   394
    val the_dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@36376
   395
                       else Config.get ctxt dest_dir;
blanchet@36376
   396
    val the_problem_prefix = Config.get ctxt problem_prefix;
boehmes@32864
   397
    fun prob_pathname nr =
blanchet@36143
   398
      let
blanchet@36143
   399
        val probfile =
blanchet@36568
   400
          Path.basic ((if overlord then "prob_" ^ name
blanchet@36568
   401
                       else the_problem_prefix ^ serial_string ())
blanchet@36143
   402
                      ^ "_" ^ string_of_int nr)
wenzelm@32942
   403
      in
blanchet@36376
   404
        if the_dest_dir = "" then File.tmp_path probfile
blanchet@36376
   405
        else if File.exists (Path.explode the_dest_dir)
blanchet@36376
   406
        then Path.append (Path.explode the_dest_dir) probfile
blanchet@36376
   407
        else error ("No such directory: " ^ the_dest_dir ^ ".")
wenzelm@32942
   408
      end;
boehmes@32864
   409
blanchet@36917
   410
    val home = getenv home_var
blanchet@36289
   411
    val command = Path.explode (home ^ "/" ^ executable)
immler@31750
   412
    (* write out problem file and call prover *)
blanchet@37514
   413
    fun command_line complete probfile =
blanchet@37514
   414
      let
blanchet@37514
   415
        val core = File.shell_path command ^ " " ^ arguments complete timeout ^
blanchet@37514
   416
                   " " ^ File.shell_path probfile
blanchet@37514
   417
      in
blanchet@37514
   418
        (if Config.get ctxt measure_runtime then
blanchet@37514
   419
           "TIMEFORMAT='%3U'; { time " ^ core ^ " ; }"
blanchet@37514
   420
         else
blanchet@37962
   421
           "exec " ^ core) ^ " 2>&1"
blanchet@37514
   422
      end
boehmes@32510
   423
    fun split_time s =
boehmes@32510
   424
      let
wenzelm@32942
   425
        val split = String.tokens (fn c => str c = "\n");
blanchet@36369
   426
        val (output, t) = s |> split |> split_last |> apfst cat_lines;
wenzelm@32942
   427
        fun as_num f = f >> (fst o read_int);
wenzelm@32942
   428
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
wenzelm@32942
   429
        val digit = Scan.one Symbol.is_ascii_digit;
wenzelm@32942
   430
        val num3 = as_num (digit ::: digit ::: (digit >> single));
wenzelm@32942
   431
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
wenzelm@32942
   432
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
blanchet@36369
   433
      in (output, as_time t) end;
boehmes@32458
   434
    fun run_on probfile =
blanchet@37962
   435
      if home = "" then
blanchet@37962
   436
        error ("The environment variable " ^ quote home_var ^ " is not set.")
blanchet@37962
   437
      else if File.exists command then
blanchet@37514
   438
        let
blanchet@37514
   439
          fun do_run complete =
blanchet@37514
   440
            let
blanchet@37514
   441
              val command = command_line complete probfile
blanchet@37514
   442
              val ((output, msecs), res_code) =
blanchet@37514
   443
                bash_output command
blanchet@37514
   444
                |>> (if overlord then
blanchet@37514
   445
                       prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@37514
   446
                     else
blanchet@37514
   447
                       I)
blanchet@37514
   448
                |>> (if Config.get ctxt measure_runtime then split_time
blanchet@37514
   449
                     else rpair 0)
blanchet@37514
   450
              val (proof, outcome) =
blanchet@37514
   451
                extract_proof_and_outcome complete res_code proof_delims
blanchet@37514
   452
                                          known_failures output
blanchet@37514
   453
            in (output, msecs, proof, outcome) end
blanchet@37643
   454
          val readable_names = debug andalso overlord
blanchet@37514
   455
          val (pool, conjecture_offset) =
blanchet@37994
   456
            write_tptp_file thy readable_names explicit_forall full_types
blanchet@37994
   457
                            explicit_apply probfile clauses
blanchet@37995
   458
          val conjecture_shape =
blanchet@37995
   459
            conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
blanchet@37514
   460
          val result =
blanchet@37514
   461
            do_run false
blanchet@37550
   462
            |> (fn (_, msecs0, _, SOME _) =>
blanchet@37514
   463
                   do_run true
blanchet@37514
   464
                   |> (fn (output, msecs, proof, outcome) =>
blanchet@37514
   465
                          (output, msecs0 + msecs, proof, outcome))
blanchet@37514
   466
                 | result => result)
blanchet@37514
   467
        in ((pool, conjecture_shape), result) end
blanchet@36382
   468
      else
blanchet@36382
   469
        error ("Bad executable: " ^ Path.implode command ^ ".");
wenzelm@28592
   470
blanchet@36167
   471
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@36167
   472
       the proof file too. *)
blanchet@36376
   473
    fun cleanup probfile =
blanchet@36376
   474
      if the_dest_dir = "" then try File.rm probfile else NONE
blanchet@37514
   475
    fun export probfile (_, (output, _, _, _)) =
blanchet@36376
   476
      if the_dest_dir = "" then
blanchet@36187
   477
        ()
blanchet@36187
   478
      else
blanchet@37514
   479
        File.write (Path.explode (Path.implode probfile ^ "_proof")) output
wenzelm@32257
   480
blanchet@37514
   481
    val ((pool, conjecture_shape), (output, msecs, proof, outcome)) =
blanchet@37514
   482
      with_path cleanup export run_on (prob_pathname subgoal)
blanchet@37989
   483
    val (conjecture_shape, internal_thm_names) =
blanchet@37989
   484
      repair_conjecture_shape_and_theorem_names output conjecture_shape
blanchet@37989
   485
                                                internal_thm_names
blanchet@37514
   486
blanchet@35969
   487
    val (message, relevant_thm_names) =
blanchet@36370
   488
      case outcome of
blanchet@36400
   489
        NONE =>
blanchet@36400
   490
        proof_text isar_proof
blanchet@37480
   491
            (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@37480
   492
            (full_types, minimize_command, proof, internal_thm_names, th,
blanchet@37480
   493
             subgoal)
blanchet@36370
   494
      | SOME failure => (string_for_failure failure ^ "\n", [])
boehmes@32864
   495
  in
blanchet@36393
   496
    {outcome = outcome, message = message, pool = pool,
blanchet@37514
   497
     relevant_thm_names = relevant_thm_names, atp_run_time_in_msecs = msecs,
blanchet@37514
   498
     output = output, proof = proof, internal_thm_names = internal_thm_names,
blanchet@36400
   499
     conjecture_shape = conjecture_shape,
blanchet@35969
   500
     filtered_clauses = the_filtered_clauses}
blanchet@37499
   501
  end
wenzelm@28596
   502
blanchet@35969
   503
fun tptp_prover name p = (name, generic_tptp_prover (name, p));
wenzelm@28596
   504
blanchet@36382
   505
fun to_generous_secs time = (Time.toMilliseconds time + 999) div 1000
blanchet@36142
   506
wenzelm@28596
   507
(* E prover *)
wenzelm@28596
   508
blanchet@36369
   509
val tstp_proof_delims =
blanchet@36369
   510
  ("# SZS output start CNFRefutation.", "# SZS output end CNFRefutation")
blanchet@36369
   511
blanchet@35969
   512
val e_config : prover_config =
blanchet@36917
   513
  {home_var = "E_HOME",
blanchet@36289
   514
   executable = "eproof",
blanchet@37514
   515
   arguments = fn _ => fn timeout =>
blanchet@36382
   516
     "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev --silent --cpu-limit=" ^
blanchet@36382
   517
     string_of_int (to_generous_secs timeout),
blanchet@36369
   518
   proof_delims = [tstp_proof_delims],
blanchet@36265
   519
   known_failures =
blanchet@37995
   520
     [(Unprovable, "SZS status: CounterSatisfiable"),
blanchet@37995
   521
      (Unprovable, "SZS status CounterSatisfiable"),
blanchet@36370
   522
      (TimedOut, "Failure: Resource limit exceeded (time)"),
blanchet@36370
   523
      (TimedOut, "time limit exceeded"),
blanchet@36370
   524
      (OutOfResources,
blanchet@36370
   525
       "# Cannot determine problem status within resource limit"),
blanchet@36370
   526
      (OutOfResources, "SZS status: ResourceOut"),
blanchet@36370
   527
      (OutOfResources, "SZS status ResourceOut")],
blanchet@36382
   528
   max_axiom_clauses = 100,
blanchet@37994
   529
   prefers_theory_relevant = false,
blanchet@37994
   530
   explicit_forall = false}
blanchet@35969
   531
val e = tptp_prover "e" e_config
wenzelm@28596
   532
wenzelm@28596
   533
blanchet@36219
   534
(* The "-VarWeight=3" option helps the higher-order problems, probably by
blanchet@36219
   535
   counteracting the presence of "hAPP". *)
blanchet@37498
   536
val spass_config : prover_config =
blanchet@37962
   537
  {home_var = "ISABELLE_ATP_MANAGER",
blanchet@37962
   538
   executable = "SPASS_TPTP",
blanchet@37550
   539
   (* "div 2" accounts for the fact that SPASS is often run twice. *)
blanchet@37514
   540
   arguments = fn complete => fn timeout =>
blanchet@37962
   541
     ("-Auto -PGiven=0 -PProblem=0 -Splits=0 -FullRed=0 -DocProof \
blanchet@37550
   542
      \-VarWeight=3 -TimeLimit=" ^
blanchet@37550
   543
      string_of_int (to_generous_secs timeout div 2))
blanchet@37514
   544
     |> not complete ? prefix "-SOS=1 ",
blanchet@36369
   545
   proof_delims = [("Here is a proof", "Formulae used in the proof")],
blanchet@36289
   546
   known_failures =
blanchet@37413
   547
     [(IncompleteUnprovable, "SPASS beiseite: Completion found"),
blanchet@36370
   548
      (TimedOut, "SPASS beiseite: Ran out of time"),
blanchet@36965
   549
      (OutOfResources, "SPASS beiseite: Maximal number of loops exceeded"),
blanchet@37413
   550
      (MalformedInput, "Undefined symbol"),
blanchet@37414
   551
      (MalformedInput, "Free Variable"),
blanchet@37962
   552
      (OldSpass, "tptp2dfg")],
blanchet@36382
   553
   max_axiom_clauses = 40,
blanchet@37994
   554
   prefers_theory_relevant = true,
blanchet@37994
   555
   explicit_forall = true}
blanchet@37414
   556
val spass = tptp_prover "spass" spass_config
wenzelm@28596
   557
blanchet@37509
   558
(* Vampire *)
blanchet@37509
   559
blanchet@37509
   560
val vampire_config : prover_config =
blanchet@37509
   561
  {home_var = "VAMPIRE_HOME",
blanchet@37509
   562
   executable = "vampire",
blanchet@37514
   563
   arguments = fn _ => fn timeout =>
blanchet@37509
   564
     "--output_syntax tptp --mode casc -t " ^
blanchet@37509
   565
     string_of_int (to_generous_secs timeout),
blanchet@37509
   566
   proof_delims =
blanchet@37509
   567
     [("=========== Refutation ==========",
blanchet@37509
   568
       "======= End of refutation ======="),
blanchet@37509
   569
      ("% SZS output start Refutation", "% SZS output end Refutation")],
blanchet@37509
   570
   known_failures =
blanchet@37509
   571
     [(Unprovable, "UNPROVABLE"),
blanchet@37509
   572
      (IncompleteUnprovable, "CANNOT PROVE"),
blanchet@37509
   573
      (Unprovable, "Satisfiability detected"),
blanchet@37509
   574
      (OutOfResources, "Refutation not found")],
blanchet@37509
   575
   max_axiom_clauses = 60,
blanchet@37994
   576
   prefers_theory_relevant = false,
blanchet@37994
   577
   explicit_forall = false}
blanchet@37509
   578
val vampire = tptp_prover "vampire" vampire_config
blanchet@37509
   579
blanchet@37509
   580
(* Remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   581
blanchet@36376
   582
val systems = Synchronized.var "atp_systems" ([]: string list);
immler@31835
   583
immler@31835
   584
fun get_systems () =
blanchet@36370
   585
  case bash_output "\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w" of
blanchet@36370
   586
    (answer, 0) => split_lines answer
blanchet@36370
   587
  | (answer, _) =>
blanchet@37627
   588
    error ("Failed to get available systems at SystemOnTPTP:\n" ^
blanchet@37627
   589
           perhaps (try (unsuffix "\n")) answer)
immler@31835
   590
blanchet@35867
   591
fun refresh_systems_on_tptp () =
blanchet@37509
   592
  Synchronized.change systems (fn _ => get_systems ())
immler@31835
   593
immler@31835
   594
fun get_system prefix = Synchronized.change_result systems (fn systems =>
boehmes@32864
   595
  (if null systems then get_systems () else systems)
wenzelm@32942
   596
  |> `(find_first (String.isPrefix prefix)));
boehmes@32864
   597
wenzelm@32948
   598
fun the_system prefix =
boehmes@32864
   599
  (case get_system prefix of
blanchet@37509
   600
    NONE => error ("System " ^ quote prefix ^ " not available at SystemOnTPTP.")
wenzelm@32942
   601
  | SOME sys => sys);
immler@31835
   602
blanchet@36265
   603
val remote_known_failures =
blanchet@37627
   604
  [(CantConnect, "HTTP-Error"),
blanchet@37627
   605
   (TimedOut, "says Timeout"),
blanchet@36377
   606
   (MalformedOutput, "Remote script could not extract proof")]
blanchet@35865
   607
blanchet@37509
   608
fun remote_config atp_prefix args
blanchet@36382
   609
        ({proof_delims, known_failures, max_axiom_clauses,
blanchet@37994
   610
          prefers_theory_relevant, explicit_forall, ...} : prover_config)
blanchet@37994
   611
        : prover_config =
blanchet@36917
   612
  {home_var = "ISABELLE_ATP_MANAGER",
blanchet@36289
   613
   executable = "SystemOnTPTP",
blanchet@37514
   614
   arguments = fn _ => fn timeout =>
blanchet@36382
   615
     args ^ " -t " ^ string_of_int (to_generous_secs timeout) ^ " -s " ^
blanchet@36382
   616
     the_system atp_prefix,
blanchet@36369
   617
   proof_delims = insert (op =) tstp_proof_delims proof_delims,
blanchet@36265
   618
   known_failures = remote_known_failures @ known_failures,
blanchet@36382
   619
   max_axiom_clauses = max_axiom_clauses,
blanchet@37994
   620
   prefers_theory_relevant = prefers_theory_relevant,
blanchet@37994
   621
   explicit_forall = explicit_forall}
wenzelm@28596
   622
blanchet@37509
   623
fun remote_tptp_prover prover atp_prefix args config =
blanchet@37509
   624
  tptp_prover (remotify (fst prover)) (remote_config atp_prefix args config)
boehmes@32864
   625
blanchet@37509
   626
val remote_e = remote_tptp_prover e "EP---" "" e_config
blanchet@37509
   627
val remote_spass = remote_tptp_prover spass "SPASS---" "-x" spass_config
blanchet@37509
   628
val remote_vampire = remote_tptp_prover vampire "Vampire---9" "" vampire_config
wenzelm@28592
   629
blanchet@36917
   630
fun maybe_remote (name, _) ({home_var, ...} : prover_config) =
blanchet@36917
   631
  name |> getenv home_var = "" ? remotify
blanchet@36371
   632
blanchet@36371
   633
fun default_atps_param_value () =
blanchet@36371
   634
  space_implode " " [maybe_remote e e_config, maybe_remote spass spass_config,
blanchet@36371
   635
                     remotify (fst vampire)]
blanchet@36371
   636
blanchet@37509
   637
val provers = [e, spass, vampire, remote_e, remote_spass, remote_vampire]
blanchet@35867
   638
val prover_setup = fold add_prover provers
blanchet@35867
   639
blanchet@35867
   640
val setup =
blanchet@36376
   641
  dest_dir_setup
blanchet@35867
   642
  #> problem_prefix_setup
blanchet@35867
   643
  #> measure_runtime_setup
blanchet@36371
   644
  #> prover_setup
blanchet@35867
   645
wenzelm@28592
   646
end;