src/HOL/Tools/ATP_Manager/atp_systems.ML
author blanchet
Tue Jul 27 17:58:30 2010 +0200 (2010-07-27)
changeset 38022 d9c4d87838f3
parent 38021 e024504943d1
child 38023 962b0a7f544b
permissions -rw-r--r--
kill needless tracing
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@36376
    10
  val dest_dir : string Config.T
blanchet@35867
    11
  val problem_prefix : string Config.T
blanchet@35867
    12
  val measure_runtime : bool Config.T
blanchet@35867
    13
  val refresh_systems_on_tptp : unit -> unit
blanchet@36371
    14
  val default_atps_param_value : unit -> string
blanchet@35867
    15
  val setup : theory -> theory
wenzelm@28592
    16
end;
wenzelm@28592
    17
blanchet@36376
    18
structure ATP_Systems : ATP_SYSTEMS =
wenzelm@28592
    19
struct
wenzelm@28596
    20
blanchet@37578
    21
open Metis_Clauses
blanchet@36187
    22
open Sledgehammer_Util
blanchet@35865
    23
open Sledgehammer_Fact_Filter
blanchet@38019
    24
open ATP_Problem
blanchet@35865
    25
open Sledgehammer_Proof_Reconstruct
blanchet@38021
    26
open Sledgehammer
blanchet@35826
    27
blanchet@36376
    28
(** generic ATP **)
wenzelm@28596
    29
wenzelm@32944
    30
(* external problem files *)
boehmes@32864
    31
blanchet@36376
    32
val (dest_dir, dest_dir_setup) = Attrib.config_string "atp_dest_dir" (K "");
boehmes@32864
    33
  (*Empty string means create files in Isabelle's temporary files directory.*)
boehmes@32864
    34
boehmes@32864
    35
val (problem_prefix, problem_prefix_setup) =
wenzelm@36001
    36
  Attrib.config_string "atp_problem_prefix" (K "prob");
boehmes@32864
    37
boehmes@33247
    38
val (measure_runtime, measure_runtime_setup) =
wenzelm@36001
    39
  Attrib.config_bool "atp_measure_runtime" (K false);
boehmes@33247
    40
boehmes@32864
    41
blanchet@35867
    42
(* prover configuration *)
boehmes@32864
    43
wenzelm@32941
    44
type prover_config =
blanchet@36917
    45
  {home_var: string,
blanchet@36370
    46
   executable: string,
blanchet@37514
    47
   arguments: bool -> Time.time -> string,
blanchet@36370
    48
   proof_delims: (string * string) list,
blanchet@36370
    49
   known_failures: (failure * string) list,
blanchet@38009
    50
   max_new_relevant_facts_per_iter: int,
blanchet@37994
    51
   prefers_theory_relevant: bool,
blanchet@37994
    52
   explicit_forall: bool}
wenzelm@28596
    53
wenzelm@28596
    54
wenzelm@28596
    55
(* basic template *)
wenzelm@28596
    56
blanchet@36370
    57
val remotify = prefix "remote_"
blanchet@36370
    58
boehmes@32458
    59
fun with_path cleanup after f path =
boehmes@32458
    60
  Exn.capture f path
boehmes@32458
    61
  |> tap (fn _ => cleanup path)
boehmes@32458
    62
  |> Exn.release
blanchet@36369
    63
  |> tap (after path)
blanchet@36369
    64
blanchet@36369
    65
(* Splits by the first possible of a list of delimiters. *)
blanchet@36369
    66
fun extract_proof delims output =
blanchet@36369
    67
  case pairself (find_first (fn s => String.isSubstring s output))
blanchet@36369
    68
                (ListPair.unzip delims) of
blanchet@36369
    69
    (SOME begin_delim, SOME end_delim) =>
blanchet@36549
    70
    (output |> first_field begin_delim |> the |> snd
blanchet@36549
    71
            |> first_field end_delim |> the |> fst
blanchet@36549
    72
            |> first_field "\n" |> the |> snd
blanchet@36549
    73
     handle Option.Option => "")
blanchet@36369
    74
  | _ => ""
boehmes@32458
    75
blanchet@37514
    76
fun extract_proof_and_outcome complete res_code proof_delims known_failures
blanchet@37514
    77
                              output =
blanchet@36370
    78
  case map_filter (fn (failure, pattern) =>
blanchet@36370
    79
                      if String.isSubstring pattern output then SOME failure
blanchet@36370
    80
                      else NONE) known_failures of
blanchet@36369
    81
    [] => (case extract_proof proof_delims output of
blanchet@36370
    82
             "" => ("", SOME UnknownError)
blanchet@36370
    83
           | proof => if res_code = 0 then (proof, NONE)
blanchet@36370
    84
                      else ("", SOME UnknownError))
blanchet@37514
    85
  | (failure :: _) =>
blanchet@37514
    86
    ("", SOME (if failure = IncompleteUnprovable andalso complete then
blanchet@37514
    87
                 Unprovable
blanchet@37514
    88
               else
blanchet@37514
    89
                 failure))
blanchet@36370
    90
blanchet@36370
    91
fun string_for_failure Unprovable = "The ATP problem is unprovable."
blanchet@37413
    92
  | string_for_failure IncompleteUnprovable =
blanchet@37413
    93
    "The ATP cannot prove the problem."
blanchet@37627
    94
  | string_for_failure CantConnect = "Can't connect to remote ATP."
blanchet@36370
    95
  | string_for_failure TimedOut = "Timed out."
blanchet@36370
    96
  | string_for_failure OutOfResources = "The ATP ran out of resources."
blanchet@36370
    97
  | string_for_failure OldSpass =
blanchet@36393
    98
    (* FIXME: Change the error message below to point to the Isabelle download
blanchet@37997
    99
       page once the package is there. *)
blanchet@36370
   100
    "Warning: Sledgehammer requires a more recent version of SPASS with \
blanchet@36370
   101
    \support for the TPTP syntax. To install it, download and untar the \
blanchet@36370
   102
    \package \"http://isabelle.in.tum.de/~blanchet/spass-3.7.tgz\" and add the \
blanchet@36370
   103
    \\"spass-3.7\" directory's full path to \"" ^
blanchet@36370
   104
    Path.implode (Path.expand (Path.appends
blanchet@36370
   105
        (Path.variable "ISABELLE_HOME_USER" ::
blanchet@36370
   106
         map Path.basic ["etc", "components"]))) ^
blanchet@36370
   107
    "\" on a line of its own."
blanchet@36965
   108
  | string_for_failure MalformedInput =
blanchet@36965
   109
    "Internal Sledgehammer error: The ATP problem is malformed. Please report \
blanchet@36965
   110
    \this to the Isabelle developers."
blanchet@36370
   111
  | string_for_failure MalformedOutput = "Error: The ATP output is malformed."
blanchet@36370
   112
  | string_for_failure UnknownError = "Error: An unknown ATP error occurred."
blanchet@35865
   113
blanchet@37623
   114
blanchet@37623
   115
(* Clause preparation *)
blanchet@37623
   116
blanchet@38017
   117
datatype fol_formula =
blanchet@38017
   118
  FOLFormula of {formula_name: string,
blanchet@38017
   119
                 kind: kind,
blanchet@38017
   120
                 combformula: (name, combterm) formula,
blanchet@38017
   121
                 ctypes_sorts: typ list}
blanchet@38017
   122
blanchet@38017
   123
fun mk_anot phi = AConn (ANot, [phi])
blanchet@38017
   124
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
blanchet@38017
   125
fun mk_ahorn [] phi = phi
blanchet@38017
   126
  | mk_ahorn (phi :: phis) psi =
blanchet@38017
   127
    AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi])
blanchet@38017
   128
blanchet@38017
   129
(* ### FIXME: reintroduce
blanchet@37623
   130
fun make_clause_table xs =
blanchet@37623
   131
  fold (Termtab.update o `(prop_of o snd)) xs Termtab.empty
blanchet@37623
   132
(* Remove existing axiom clauses from the conjecture clauses, as this can
blanchet@37623
   133
   dramatically boost an ATP's performance (for some reason). *)
blanchet@37623
   134
fun subtract_cls ax_clauses =
blanchet@37623
   135
  filter_out (Termtab.defined (make_clause_table ax_clauses) o prop_of)
blanchet@38017
   136
*)
blanchet@37623
   137
blanchet@37995
   138
fun combformula_for_prop thy =
blanchet@37995
   139
  let
blanchet@37995
   140
    val do_term = combterm_from_term thy
blanchet@37995
   141
    fun do_quant bs q s T t' =
blanchet@37995
   142
      do_formula ((s, T) :: bs) t'
blanchet@37995
   143
      #>> (fn phi => AQuant (q, [`make_bound_var s], phi))
blanchet@37995
   144
    and do_conn bs c t1 t2 =
blanchet@37995
   145
      do_formula bs t1 ##>> do_formula bs t2
blanchet@37995
   146
      #>> (fn (phi1, phi2) => AConn (c, [phi1, phi2]))
blanchet@37995
   147
    and do_formula bs t =
blanchet@37995
   148
      case t of
blanchet@38001
   149
        @{const Not} $ t1 =>
blanchet@37995
   150
        do_formula bs t1 #>> (fn phi => AConn (ANot, [phi]))
blanchet@37995
   151
      | Const (@{const_name All}, _) $ Abs (s, T, t') =>
blanchet@37995
   152
        do_quant bs AForall s T t'
blanchet@37995
   153
      | Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
blanchet@37995
   154
        do_quant bs AExists s T t'
blanchet@37995
   155
      | @{const "op &"} $ t1 $ t2 => do_conn bs AAnd t1 t2
blanchet@37995
   156
      | @{const "op |"} $ t1 $ t2 => do_conn bs AOr t1 t2
blanchet@37995
   157
      | @{const "op -->"} $ t1 $ t2 => do_conn bs AImplies t1 t2
blanchet@37995
   158
      | Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
blanchet@37995
   159
        do_conn bs AIff t1 t2
blanchet@37995
   160
      | _ => (fn ts => do_term bs (Envir.eta_contract t)
blanchet@37995
   161
                       |>> APred ||> union (op =) ts)
blanchet@37995
   162
  in do_formula [] end
blanchet@37623
   163
blanchet@38001
   164
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@38001
   165
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@38001
   166
   the conclusion variable to False. (Cf. "transform_elim_term" in
blanchet@38001
   167
   "ATP_Systems".) *)
blanchet@38001
   168
(* FIXME: test! *)
blanchet@38001
   169
fun transform_elim_term t =
blanchet@38001
   170
  case Logic.strip_imp_concl t of
blanchet@38001
   171
    @{const Trueprop} $ Var (z, @{typ bool}) =>
blanchet@38001
   172
    subst_Vars [(z, @{const True})] t
blanchet@38001
   173
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop True})] t
blanchet@38001
   174
  | _ => t
blanchet@38001
   175
blanchet@38001
   176
(* Removes the lambdas from an equation of the form "t = (%x. u)".
blanchet@38001
   177
   (Cf. "extensionalize_theorem" in "Clausifier".) *)
blanchet@38000
   178
fun extensionalize_term t =
blanchet@38000
   179
  let
blanchet@38001
   180
    fun aux j (Const (@{const_name "op ="}, Type (_, [Type (_, [_, T']), _]))
blanchet@38001
   181
               $ t2 $ Abs (s, var_T, t')) =
blanchet@38001
   182
        let val var_t = Var (("x", j), var_T) in
blanchet@38000
   183
          Const (@{const_name "op ="}, T' --> T' --> HOLogic.boolT)
blanchet@38000
   184
            $ betapply (t2, var_t) $ subst_bound (var_t, t')
blanchet@38000
   185
          |> aux (j + 1)
blanchet@38000
   186
        end
blanchet@38000
   187
      | aux _ t = t
blanchet@38000
   188
  in aux (maxidx_of_term t + 1) t end
blanchet@38000
   189
blanchet@38001
   190
(* FIXME: Guarantee freshness *)
blanchet@38001
   191
fun concealed_bound_name j = "Sledgehammer" ^ Int.toString j
blanchet@38001
   192
fun conceal_bounds Ts t =
blanchet@38001
   193
  subst_bounds (map (Free o apfst concealed_bound_name)
blanchet@38001
   194
                    (length Ts - 1 downto 0 ~~ rev Ts), t)
blanchet@38001
   195
fun reveal_bounds Ts =
blanchet@38001
   196
  subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
blanchet@38001
   197
                    (0 upto length Ts - 1 ~~ Ts))
blanchet@38001
   198
blanchet@38005
   199
fun introduce_combinators_in_term ctxt kind t =
blanchet@38001
   200
  let
blanchet@38005
   201
    val thy = ProofContext.theory_of ctxt
blanchet@38001
   202
    fun aux Ts t =
blanchet@38001
   203
      case t of
blanchet@38001
   204
        @{const Not} $ t1 => @{const Not} $ aux Ts t1
blanchet@38001
   205
      | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
blanchet@38001
   206
        t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38001
   207
      | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
blanchet@38001
   208
        t0 $ Abs (s, T, aux (T :: Ts) t')
blanchet@38001
   209
      | (t0 as @{const "op &"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38001
   210
      | (t0 as @{const "op |"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38001
   211
      | (t0 as @{const "op -->"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
blanchet@38001
   212
      | (t0 as Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])))
blanchet@38001
   213
          $ t1 $ t2 =>
blanchet@38001
   214
        t0 $ aux Ts t1 $ aux Ts t2
blanchet@38001
   215
      | _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
blanchet@38001
   216
               t
blanchet@38001
   217
             else
blanchet@38005
   218
               let
blanchet@38005
   219
                 val t = t |> conceal_bounds Ts
blanchet@38005
   220
                           |> Envir.eta_contract
blanchet@38005
   221
                 val ([t], ctxt') = Variable.import_terms true [t] ctxt
blanchet@38005
   222
               in
blanchet@38005
   223
                 t |> cterm_of thy
blanchet@38005
   224
                   |> Clausifier.introduce_combinators_in_cterm
blanchet@38005
   225
                   |> singleton (Variable.export ctxt' ctxt)
blanchet@38005
   226
                   |> prop_of |> Logic.dest_equals |> snd
blanchet@38005
   227
                   |> reveal_bounds Ts
blanchet@38005
   228
               end
blanchet@38001
   229
  in t |> not (Meson.is_fol_term thy t) ? aux [] end
blanchet@38005
   230
  handle THM _ =>
blanchet@38005
   231
         (* A type variable of sort "{}" will make abstraction fail. *)
blanchet@38005
   232
         case kind of
blanchet@38005
   233
           Axiom => HOLogic.true_const
blanchet@38005
   234
         | Conjecture => HOLogic.false_const
blanchet@38001
   235
blanchet@37623
   236
(* making axiom and conjecture clauses *)
blanchet@38005
   237
fun make_clause ctxt (formula_name, kind, t) =
blanchet@37623
   238
  let
blanchet@38005
   239
    val thy = ProofContext.theory_of ctxt
blanchet@38002
   240
    (* ### FIXME: perform other transformations previously done by
blanchet@38002
   241
       "Clausifier.to_nnf", e.g. "HOL.If" *)
blanchet@38001
   242
    val t = t |> transform_elim_term
blanchet@38001
   243
              |> Object_Logic.atomize_term thy
blanchet@38000
   244
              |> extensionalize_term
blanchet@38005
   245
              |> introduce_combinators_in_term ctxt kind
blanchet@37995
   246
    val (combformula, ctypes_sorts) = combformula_for_prop thy t []
blanchet@37623
   247
  in
blanchet@38004
   248
    FOLFormula {formula_name = formula_name, combformula = combformula,
blanchet@38004
   249
                kind = kind, ctypes_sorts = ctypes_sorts}
blanchet@37623
   250
  end
blanchet@37623
   251
blanchet@38005
   252
fun make_axiom_clause ctxt (name, th) =
blanchet@38005
   253
  (name, make_clause ctxt (name, Axiom, prop_of th))
blanchet@38005
   254
fun make_conjecture_clauses ctxt ts =
blanchet@38005
   255
  map2 (fn j => fn t => make_clause ctxt (Int.toString j, Conjecture, t))
blanchet@37999
   256
       (0 upto length ts - 1) ts
blanchet@37623
   257
blanchet@37623
   258
(** Helper clauses **)
blanchet@37623
   259
blanchet@37994
   260
fun count_combterm (CombConst ((s, _), _, _)) =
blanchet@37994
   261
    Symtab.map_entry s (Integer.add 1)
blanchet@37623
   262
  | count_combterm (CombVar _) = I
blanchet@37994
   263
  | count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
blanchet@37994
   264
fun count_combformula (AQuant (_, _, phi)) = count_combformula phi
blanchet@37994
   265
  | count_combformula (AConn (_, phis)) = fold count_combformula phis
blanchet@37994
   266
  | count_combformula (APred tm) = count_combterm tm
blanchet@37994
   267
fun count_fol_formula (FOLFormula {combformula, ...}) =
blanchet@37994
   268
  count_combformula combformula
blanchet@37623
   269
blanchet@37623
   270
val optional_helpers =
blanchet@38002
   271
  [(["c_COMBI", "c_COMBK"], @{thms COMBI_def COMBK_def}),
blanchet@38002
   272
   (["c_COMBB", "c_COMBC"], @{thms COMBB_def COMBC_def}),
blanchet@38002
   273
   (["c_COMBS"], @{thms COMBS_def})]
blanchet@37623
   274
val optional_typed_helpers =
blanchet@38002
   275
  [(["c_True", "c_False"], @{thms True_or_False}),
blanchet@38002
   276
   (["c_If"], @{thms if_True if_False True_or_False})]
blanchet@37623
   277
val mandatory_helpers = @{thms fequal_imp_equal equal_imp_fequal}
blanchet@37623
   278
blanchet@37623
   279
val init_counters =
blanchet@37623
   280
  Symtab.make (maps (maps (map (rpair 0) o fst))
blanchet@37623
   281
                    [optional_helpers, optional_typed_helpers])
blanchet@37623
   282
blanchet@38005
   283
fun get_helper_clauses ctxt is_FO full_types conjectures axclauses =
blanchet@37623
   284
  let
blanchet@37994
   285
    val ct = fold (fold count_fol_formula) [conjectures, axclauses]
blanchet@37994
   286
                  init_counters
blanchet@37623
   287
    fun is_needed c = the (Symtab.lookup ct c) > 0
blanchet@37623
   288
    val cnfs =
blanchet@37623
   289
      (optional_helpers
blanchet@37623
   290
       |> full_types ? append optional_typed_helpers
blanchet@38002
   291
       |> maps (fn (ss, ths) =>
blanchet@38002
   292
                   if exists is_needed ss then map (`Thm.get_name_hint) ths
blanchet@38002
   293
                   else [])) @
blanchet@38002
   294
      (if is_FO then [] else map (`Thm.get_name_hint) mandatory_helpers)
blanchet@38005
   295
  in map (snd o make_axiom_clause ctxt) cnfs end
blanchet@37623
   296
blanchet@38002
   297
fun s_not (@{const Not} $ t) = t
blanchet@38002
   298
  | s_not t = @{const Not} $ t
blanchet@37995
   299
blanchet@37623
   300
(* prepare for passing to writer,
blanchet@37623
   301
   create additional clauses based on the information from extra_cls *)
blanchet@38005
   302
fun prepare_clauses ctxt full_types hyp_ts concl_t axcls extra_cls =
blanchet@37623
   303
  let
blanchet@38005
   304
    val thy = ProofContext.theory_of ctxt
blanchet@37995
   305
    val goal_t = Logic.list_implies (hyp_ts, concl_t)
blanchet@37995
   306
    val is_FO = Meson.is_fol_term thy goal_t
blanchet@37995
   307
    val axtms = map (prop_of o snd) extra_cls
blanchet@37995
   308
    val subs = tfree_classes_of_terms [goal_t]
blanchet@37995
   309
    val supers = tvar_classes_of_terms axtms
blanchet@37995
   310
    val tycons = type_consts_of_terms thy (goal_t :: axtms)
blanchet@38002
   311
    (* TFrees in conjecture clauses; TVars in axiom clauses *)
blanchet@38005
   312
    val conjectures =
blanchet@38005
   313
      map (s_not o HOLogic.dest_Trueprop) hyp_ts @
blanchet@38005
   314
        [HOLogic.dest_Trueprop concl_t]
blanchet@38005
   315
      |> make_conjecture_clauses ctxt
blanchet@38005
   316
    val extra_clauses = map (snd o make_axiom_clause ctxt) extra_cls
blanchet@38003
   317
    val (clnames, axiom_clauses) =
blanchet@38005
   318
      ListPair.unzip (map (make_axiom_clause ctxt) axcls)
blanchet@38003
   319
    (* FIXME: Should it read "extra_clauses" or "axiom_clauses" in the
blanchet@38003
   320
       "get_helper_clauses" call? *)
blanchet@37623
   321
    val helper_clauses =
blanchet@38005
   322
      get_helper_clauses ctxt is_FO full_types conjectures extra_clauses
blanchet@37623
   323
    val (supers', arity_clauses) = make_arity_clauses thy tycons supers
blanchet@37925
   324
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@37623
   325
  in
blanchet@37623
   326
    (Vector.fromList clnames,
blanchet@37925
   327
      (conjectures, axiom_clauses, extra_clauses, helper_clauses,
blanchet@37925
   328
       class_rel_clauses, arity_clauses))
blanchet@37623
   329
  end
blanchet@37623
   330
blanchet@38017
   331
val axiom_prefix = "ax_"
blanchet@38017
   332
val conjecture_prefix = "conj_"
blanchet@38017
   333
val arity_clause_prefix = "clsarity_"
blanchet@38017
   334
val tfrees_name = "tfrees"
blanchet@38017
   335
blanchet@38017
   336
fun wrap_type ty t = ATerm ((type_wrapper_name, type_wrapper_name), [ty, t])
blanchet@38017
   337
blanchet@38017
   338
fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
blanchet@38017
   339
  | fo_term_for_combtyp (CombTFree name) = ATerm (name, [])
blanchet@38017
   340
  | fo_term_for_combtyp (CombType (name, tys)) =
blanchet@38017
   341
    ATerm (name, map fo_term_for_combtyp tys)
blanchet@38017
   342
blanchet@38017
   343
fun fo_literal_for_type_literal (TyLitVar (class, name)) =
blanchet@38017
   344
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@38017
   345
  | fo_literal_for_type_literal (TyLitFree (class, name)) =
blanchet@38017
   346
    (true, ATerm (class, [ATerm (name, [])]))
blanchet@38017
   347
blanchet@38017
   348
fun formula_for_fo_literal (pos, t) = APred t |> not pos ? mk_anot
blanchet@38017
   349
blanchet@38017
   350
fun fo_term_for_combterm full_types =
blanchet@38017
   351
  let
blanchet@38017
   352
    fun aux top_level u =
blanchet@38017
   353
      let
blanchet@38017
   354
        val (head, args) = strip_combterm_comb u
blanchet@38017
   355
        val (x, ty_args) =
blanchet@38017
   356
          case head of
blanchet@38017
   357
            CombConst (name, _, ty_args) =>
blanchet@38017
   358
            if fst name = "equal" then
blanchet@38017
   359
              (if top_level andalso length args = 2 then name
blanchet@38017
   360
               else ("c_fequal", @{const_name fequal}), [])
blanchet@38017
   361
            else
blanchet@38017
   362
              (name, if full_types then [] else ty_args)
blanchet@38017
   363
          | CombVar (name, _) => (name, [])
blanchet@38017
   364
          | CombApp _ => raise Fail "impossible \"CombApp\""
blanchet@38017
   365
        val t = ATerm (x, map fo_term_for_combtyp ty_args @
blanchet@38017
   366
                          map (aux false) args)
blanchet@38017
   367
    in
blanchet@38017
   368
      if full_types then wrap_type (fo_term_for_combtyp (combtyp_of u)) t else t
blanchet@38017
   369
    end
blanchet@38017
   370
  in aux true end
blanchet@38017
   371
blanchet@38017
   372
fun formula_for_combformula full_types =
blanchet@38017
   373
  let
blanchet@38017
   374
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
blanchet@38017
   375
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
blanchet@38017
   376
      | aux (APred tm) = APred (fo_term_for_combterm full_types tm)
blanchet@38017
   377
  in aux end
blanchet@38017
   378
blanchet@38017
   379
fun formula_for_axiom full_types (FOLFormula {combformula, ctypes_sorts, ...}) =
blanchet@38017
   380
  mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
blanchet@38017
   381
                (type_literals_for_types ctypes_sorts))
blanchet@38017
   382
           (formula_for_combformula full_types combformula)
blanchet@38017
   383
blanchet@38017
   384
fun problem_line_for_axiom full_types
blanchet@38017
   385
        (formula as FOLFormula {formula_name, kind, ...}) =
blanchet@38017
   386
  Fof (axiom_prefix ^ ascii_of formula_name, kind,
blanchet@38017
   387
       formula_for_axiom full_types formula)
blanchet@38017
   388
blanchet@38017
   389
fun problem_line_for_class_rel_clause
blanchet@38017
   390
        (ClassRelClause {axiom_name, subclass, superclass, ...}) =
blanchet@38017
   391
  let val ty_arg = ATerm (("T", "T"), []) in
blanchet@38017
   392
    Fof (ascii_of axiom_name, Axiom,
blanchet@38017
   393
         AConn (AImplies, [APred (ATerm (subclass, [ty_arg])),
blanchet@38017
   394
                           APred (ATerm (superclass, [ty_arg]))]))
blanchet@38017
   395
  end
blanchet@38017
   396
blanchet@38017
   397
fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
blanchet@38017
   398
    (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
blanchet@38017
   399
  | fo_literal_for_arity_literal (TVarLit (c, sort)) =
blanchet@38017
   400
    (false, ATerm (c, [ATerm (sort, [])]))
blanchet@38017
   401
blanchet@38017
   402
fun problem_line_for_arity_clause
blanchet@38017
   403
        (ArityClause {axiom_name, conclLit, premLits, ...}) =
blanchet@38017
   404
  Fof (arity_clause_prefix ^ ascii_of axiom_name, Axiom,
blanchet@38017
   405
       mk_ahorn (map (formula_for_fo_literal o apfst not
blanchet@38017
   406
                      o fo_literal_for_arity_literal) premLits)
blanchet@38017
   407
                (formula_for_fo_literal
blanchet@38017
   408
                     (fo_literal_for_arity_literal conclLit)))
blanchet@38017
   409
blanchet@38017
   410
fun problem_line_for_conjecture full_types
blanchet@38017
   411
        (FOLFormula {formula_name, kind, combformula, ...}) =
blanchet@38017
   412
  Fof (conjecture_prefix ^ formula_name, kind,
blanchet@38017
   413
       formula_for_combformula full_types combformula)
blanchet@38017
   414
blanchet@38017
   415
fun free_type_literals_for_conjecture (FOLFormula {ctypes_sorts, ...}) =
blanchet@38017
   416
  map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts)
blanchet@38017
   417
blanchet@38017
   418
fun problem_line_for_free_type lit =
blanchet@38017
   419
  Fof (tfrees_name, Conjecture, mk_anot (formula_for_fo_literal lit))
blanchet@38017
   420
fun problem_lines_for_free_types conjectures =
blanchet@38017
   421
  let
blanchet@38017
   422
    val litss = map free_type_literals_for_conjecture conjectures
blanchet@38017
   423
    val lits = fold (union (op =)) litss []
blanchet@38017
   424
  in map problem_line_for_free_type lits end
blanchet@38017
   425
blanchet@38017
   426
(** "hBOOL" and "hAPP" **)
blanchet@38017
   427
blanchet@38017
   428
type const_info = {min_arity: int, max_arity: int, sub_level: bool}
blanchet@38017
   429
blanchet@38017
   430
fun consider_term top_level (ATerm ((s, _), ts)) =
blanchet@38017
   431
  (if is_tptp_variable s then
blanchet@38017
   432
     I
blanchet@38017
   433
   else
blanchet@38017
   434
     let val n = length ts in
blanchet@38017
   435
       Symtab.map_default
blanchet@38017
   436
           (s, {min_arity = n, max_arity = 0, sub_level = false})
blanchet@38017
   437
           (fn {min_arity, max_arity, sub_level} =>
blanchet@38017
   438
               {min_arity = Int.min (n, min_arity),
blanchet@38017
   439
                max_arity = Int.max (n, max_arity),
blanchet@38017
   440
                sub_level = sub_level orelse not top_level})
blanchet@38017
   441
     end)
blanchet@38017
   442
  #> fold (consider_term (top_level andalso s = type_wrapper_name)) ts
blanchet@38017
   443
fun consider_formula (AQuant (_, _, phi)) = consider_formula phi
blanchet@38017
   444
  | consider_formula (AConn (_, phis)) = fold consider_formula phis
blanchet@38017
   445
  | consider_formula (APred tm) = consider_term true tm
blanchet@38017
   446
blanchet@38017
   447
fun consider_problem_line (Fof (_, _, phi)) = consider_formula phi
blanchet@38017
   448
fun consider_problem problem = fold (fold consider_problem_line o snd) problem
blanchet@38017
   449
blanchet@38017
   450
fun const_table_for_problem explicit_apply problem =
blanchet@38017
   451
  if explicit_apply then NONE
blanchet@38017
   452
  else SOME (Symtab.empty |> consider_problem problem)
blanchet@38017
   453
blanchet@38017
   454
val tc_fun = make_fixed_type_const @{type_name fun}
blanchet@38017
   455
blanchet@38017
   456
fun min_arity_of thy full_types NONE s =
blanchet@38017
   457
    (if s = "equal" orelse s = type_wrapper_name orelse
blanchet@38017
   458
        String.isPrefix type_const_prefix s orelse
blanchet@38017
   459
        String.isPrefix class_prefix s then
blanchet@38017
   460
       16383 (* large number *)
blanchet@38017
   461
     else if full_types then
blanchet@38017
   462
       0
blanchet@38017
   463
     else case strip_prefix_and_undo_ascii const_prefix s of
blanchet@38017
   464
       SOME s' => num_type_args thy (invert_const s')
blanchet@38017
   465
     | NONE => 0)
blanchet@38017
   466
  | min_arity_of _ _ (SOME the_const_tab) s =
blanchet@38017
   467
    case Symtab.lookup the_const_tab s of
blanchet@38017
   468
      SOME ({min_arity, ...} : const_info) => min_arity
blanchet@38017
   469
    | NONE => 0
blanchet@38017
   470
blanchet@38017
   471
fun full_type_of (ATerm ((s, _), [ty, _])) =
blanchet@38017
   472
    if s = type_wrapper_name then ty else raise Fail "expected type wrapper"
blanchet@38017
   473
  | full_type_of _ = raise Fail "expected type wrapper"
blanchet@38017
   474
blanchet@38017
   475
fun list_hAPP_rev _ t1 [] = t1
blanchet@38017
   476
  | list_hAPP_rev NONE t1 (t2 :: ts2) =
blanchet@38017
   477
    ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2])
blanchet@38017
   478
  | list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
blanchet@38017
   479
    let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
blanchet@38017
   480
                         [full_type_of t2, ty]) in
blanchet@38017
   481
      ATerm (`I "hAPP", [wrap_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
blanchet@38017
   482
    end
blanchet@38017
   483
blanchet@38017
   484
fun repair_applications_in_term thy full_types const_tab =
blanchet@38017
   485
  let
blanchet@38017
   486
    fun aux opt_ty (ATerm (name as (s, _), ts)) =
blanchet@38017
   487
      if s = type_wrapper_name then
blanchet@38017
   488
        case ts of
blanchet@38017
   489
          [t1, t2] => ATerm (name, [aux NONE t1, aux (SOME t1) t2])
blanchet@38017
   490
        | _ => raise Fail "malformed type wrapper"
blanchet@38017
   491
      else
blanchet@38017
   492
        let
blanchet@38017
   493
          val ts = map (aux NONE) ts
blanchet@38017
   494
          val (ts1, ts2) = chop (min_arity_of thy full_types const_tab s) ts
blanchet@38017
   495
        in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
blanchet@38017
   496
  in aux NONE end
blanchet@38017
   497
blanchet@38017
   498
fun boolify t = ATerm (`I "hBOOL", [t])
blanchet@38017
   499
blanchet@38017
   500
(* True if the constant ever appears outside of the top-level position in
blanchet@38017
   501
   literals, or if it appears with different arities (e.g., because of different
blanchet@38017
   502
   type instantiations). If false, the constant always receives all of its
blanchet@38017
   503
   arguments and is used as a predicate. *)
blanchet@38017
   504
fun is_predicate NONE s =
blanchet@38017
   505
    s = "equal" orelse String.isPrefix type_const_prefix s orelse
blanchet@38017
   506
    String.isPrefix class_prefix s
blanchet@38017
   507
  | is_predicate (SOME the_const_tab) s =
blanchet@38017
   508
    case Symtab.lookup the_const_tab s of
blanchet@38017
   509
      SOME {min_arity, max_arity, sub_level} =>
blanchet@38017
   510
      not sub_level andalso min_arity = max_arity
blanchet@38017
   511
    | NONE => false
blanchet@38017
   512
blanchet@38017
   513
fun repair_predicates_in_term const_tab (t as ATerm ((s, _), ts)) =
blanchet@38017
   514
  if s = type_wrapper_name then
blanchet@38017
   515
    case ts of
blanchet@38017
   516
      [_, t' as ATerm ((s', _), _)] =>
blanchet@38017
   517
      if is_predicate const_tab s' then t' else boolify t
blanchet@38017
   518
    | _ => raise Fail "malformed type wrapper"
blanchet@38017
   519
  else
blanchet@38017
   520
    t |> not (is_predicate const_tab s) ? boolify
blanchet@38017
   521
blanchet@38017
   522
fun close_universally phi =
blanchet@38017
   523
  let
blanchet@38017
   524
    fun term_vars bounds (ATerm (name as (s, _), tms)) =
blanchet@38017
   525
        (is_tptp_variable s andalso not (member (op =) bounds name))
blanchet@38017
   526
          ? insert (op =) name
blanchet@38017
   527
        #> fold (term_vars bounds) tms
blanchet@38017
   528
    fun formula_vars bounds (AQuant (q, xs, phi)) =
blanchet@38017
   529
        formula_vars (xs @ bounds) phi
blanchet@38017
   530
      | formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
blanchet@38017
   531
      | formula_vars bounds (APred tm) = term_vars bounds tm
blanchet@38017
   532
  in
blanchet@38017
   533
    case formula_vars [] phi [] of [] => phi | xs => AQuant (AForall, xs, phi)
blanchet@38017
   534
  end
blanchet@38017
   535
blanchet@38017
   536
fun repair_formula thy explicit_forall full_types const_tab =
blanchet@38017
   537
  let
blanchet@38017
   538
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
blanchet@38017
   539
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
blanchet@38017
   540
      | aux (APred tm) =
blanchet@38017
   541
        APred (tm |> repair_applications_in_term thy full_types const_tab
blanchet@38017
   542
                  |> repair_predicates_in_term const_tab)
blanchet@38017
   543
  in aux #> explicit_forall ? close_universally end
blanchet@38017
   544
blanchet@38017
   545
fun repair_problem_line thy explicit_forall full_types const_tab
blanchet@38017
   546
                        (Fof (ident, kind, phi)) =
blanchet@38017
   547
  Fof (ident, kind, repair_formula thy explicit_forall full_types const_tab phi)
blanchet@38017
   548
fun repair_problem_with_const_table thy =
blanchet@38017
   549
  map o apsnd o map ooo repair_problem_line thy
blanchet@38017
   550
blanchet@38017
   551
fun repair_problem thy explicit_forall full_types explicit_apply problem =
blanchet@38017
   552
  repair_problem_with_const_table thy explicit_forall full_types
blanchet@38017
   553
      (const_table_for_problem explicit_apply problem) problem
blanchet@38017
   554
blanchet@38017
   555
fun write_tptp_file thy readable_names explicit_forall full_types explicit_apply
blanchet@38017
   556
                    file (conjectures, axiom_clauses, extra_clauses,
blanchet@38017
   557
                          helper_clauses, class_rel_clauses, arity_clauses) =
blanchet@38017
   558
  let
blanchet@38017
   559
    val axiom_lines = map (problem_line_for_axiom full_types) axiom_clauses
blanchet@38017
   560
    val class_rel_lines =
blanchet@38017
   561
      map problem_line_for_class_rel_clause class_rel_clauses
blanchet@38017
   562
    val arity_lines = map problem_line_for_arity_clause arity_clauses
blanchet@38017
   563
    val helper_lines = map (problem_line_for_axiom full_types) helper_clauses
blanchet@38017
   564
    val conjecture_lines =
blanchet@38017
   565
      map (problem_line_for_conjecture full_types) conjectures
blanchet@38017
   566
    val tfree_lines = problem_lines_for_free_types conjectures
blanchet@38017
   567
    (* Reordering these might or might not confuse the proof reconstruction
blanchet@38017
   568
       code or the SPASS Flotter hack. *)
blanchet@38017
   569
    val problem =
blanchet@38017
   570
      [("Relevant facts", axiom_lines),
blanchet@38017
   571
       ("Class relationships", class_rel_lines),
blanchet@38017
   572
       ("Arity declarations", arity_lines),
blanchet@38017
   573
       ("Helper facts", helper_lines),
blanchet@38017
   574
       ("Conjectures", conjecture_lines),
blanchet@38017
   575
       ("Type variables", tfree_lines)]
blanchet@38017
   576
      |> repair_problem thy explicit_forall full_types explicit_apply
blanchet@38017
   577
    val (problem, pool) = nice_tptp_problem readable_names problem
blanchet@38017
   578
    val conjecture_offset =
blanchet@38017
   579
      length axiom_lines + length class_rel_lines + length arity_lines
blanchet@38017
   580
      + length helper_lines
blanchet@38017
   581
    val _ = File.write_list file (strings_for_tptp_problem problem)
blanchet@38017
   582
  in
blanchet@38017
   583
    (case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
blanchet@38017
   584
     conjecture_offset)
blanchet@38017
   585
  end
blanchet@38017
   586
blanchet@37962
   587
fun extract_clause_sequence output =
blanchet@37962
   588
  let
blanchet@37962
   589
    val tokens_of = String.tokens (not o Char.isAlphaNum)
blanchet@37962
   590
    fun extract_num ("clause" :: (ss as _ :: _)) =
blanchet@37962
   591
    Int.fromString (List.last ss)
blanchet@37962
   592
      | extract_num _ = NONE
blanchet@37962
   593
  in output |> split_lines |> map_filter (extract_num o tokens_of) end
blanchet@37962
   594
blanchet@37962
   595
val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
blanchet@37962
   596
blanchet@37962
   597
val parse_clause_formula_pair =
blanchet@37962
   598
  $$ "(" |-- scan_integer --| $$ "," -- Symbol.scan_id --| $$ ")"
blanchet@37962
   599
  --| Scan.option ($$ ",")
blanchet@37962
   600
val parse_clause_formula_relation =
blanchet@37962
   601
  Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
blanchet@37962
   602
  |-- Scan.repeat parse_clause_formula_pair
blanchet@37962
   603
val extract_clause_formula_relation =
blanchet@37962
   604
  Substring.full
blanchet@37962
   605
  #> Substring.position set_ClauseFormulaRelationN
blanchet@37962
   606
  #> snd #> Substring.string #> strip_spaces #> explode
blanchet@37962
   607
  #> parse_clause_formula_relation #> fst
blanchet@37962
   608
blanchet@37989
   609
fun repair_conjecture_shape_and_theorem_names output conjecture_shape
blanchet@37989
   610
                                              thm_names =
blanchet@37962
   611
  if String.isSubstring set_ClauseFormulaRelationN output then
blanchet@38009
   612
    (* This is a hack required for keeping track of axioms after they have been
blanchet@38009
   613
       clausified by SPASS's Flotter tool. The "SPASS_TPTP" script is also part
blanchet@38009
   614
       of this hack. *)
blanchet@37962
   615
    let
blanchet@37996
   616
      val j0 = hd conjecture_shape
blanchet@37962
   617
      val seq = extract_clause_sequence output
blanchet@37962
   618
      val name_map = extract_clause_formula_relation output
blanchet@37989
   619
      fun renumber_conjecture j =
blanchet@37989
   620
        AList.find (op =) name_map (conjecture_prefix ^ Int.toString (j - j0))
blanchet@37989
   621
        |> the_single
blanchet@37989
   622
        |> (fn s => find_index (curry (op =) s) seq + 1)
blanchet@37962
   623
    in
blanchet@37996
   624
      (conjecture_shape |> map renumber_conjecture,
blanchet@37989
   625
       seq |> map (the o AList.lookup (op =) name_map)
blanchet@37989
   626
           |> map (fn s => case try (unprefix axiom_prefix) s of
blanchet@37989
   627
                             SOME s' => undo_ascii_of s'
blanchet@37989
   628
                           | NONE => "")
blanchet@37989
   629
           |> Vector.fromList)
blanchet@37962
   630
    end
blanchet@37962
   631
  else
blanchet@37989
   632
    (conjecture_shape, thm_names)
blanchet@37962
   633
blanchet@37623
   634
blanchet@37499
   635
(* generic TPTP-based provers *)
blanchet@37499
   636
blanchet@37499
   637
fun generic_tptp_prover
blanchet@37499
   638
        (name, {home_var, executable, arguments, proof_delims, known_failures,
blanchet@38009
   639
                max_new_relevant_facts_per_iter, prefers_theory_relevant,
blanchet@38009
   640
                explicit_forall})
blanchet@37580
   641
        ({debug, overlord, full_types, explicit_apply, relevance_threshold,
blanchet@37580
   642
          relevance_convergence, theory_relevant, defs_relevant, isar_proof,
blanchet@37580
   643
          isar_shrink_factor, ...} : params)
blanchet@37499
   644
        minimize_command timeout
blanchet@35969
   645
        ({subgoal, goal, relevance_override, axiom_clauses, filtered_clauses}
blanchet@35969
   646
         : problem) =
wenzelm@28596
   647
  let
immler@31750
   648
    (* get clauses and prepare them for writing *)
blanchet@37498
   649
    val (ctxt, (_, th)) = goal;
blanchet@38005
   650
    val thy = ProofContext.theory_of ctxt
blanchet@37995
   651
    (* ### FIXME: (1) preprocessing for "if" etc. *)
blanchet@37995
   652
    val (params, hyp_ts, concl_t) = strip_subgoal th subgoal
immler@31752
   653
    val the_filtered_clauses =
blanchet@37621
   654
      case filtered_clauses of
blanchet@37621
   655
        SOME fcls => fcls
blanchet@37621
   656
      | NONE => relevant_facts full_types relevance_threshold
blanchet@38009
   657
                    relevance_convergence defs_relevant
blanchet@38009
   658
                    max_new_relevant_facts_per_iter
blanchet@37499
   659
                    (the_default prefers_theory_relevant theory_relevant)
blanchet@37995
   660
                    relevance_override goal hyp_ts concl_t
blanchet@37506
   661
    val the_axiom_clauses = axiom_clauses |> the_default the_filtered_clauses
blanchet@35969
   662
    val (internal_thm_names, clauses) =
blanchet@38005
   663
      prepare_clauses ctxt full_types hyp_ts concl_t the_axiom_clauses
blanchet@38005
   664
                      the_filtered_clauses
immler@31750
   665
boehmes@32864
   666
    (* path to unique problem file *)
blanchet@36376
   667
    val the_dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@36376
   668
                       else Config.get ctxt dest_dir;
blanchet@36376
   669
    val the_problem_prefix = Config.get ctxt problem_prefix;
boehmes@32864
   670
    fun prob_pathname nr =
blanchet@36143
   671
      let
blanchet@36143
   672
        val probfile =
blanchet@36568
   673
          Path.basic ((if overlord then "prob_" ^ name
blanchet@36568
   674
                       else the_problem_prefix ^ serial_string ())
blanchet@36143
   675
                      ^ "_" ^ string_of_int nr)
wenzelm@32942
   676
      in
blanchet@36376
   677
        if the_dest_dir = "" then File.tmp_path probfile
blanchet@36376
   678
        else if File.exists (Path.explode the_dest_dir)
blanchet@36376
   679
        then Path.append (Path.explode the_dest_dir) probfile
blanchet@36376
   680
        else error ("No such directory: " ^ the_dest_dir ^ ".")
wenzelm@32942
   681
      end;
boehmes@32864
   682
blanchet@36917
   683
    val home = getenv home_var
blanchet@36289
   684
    val command = Path.explode (home ^ "/" ^ executable)
immler@31750
   685
    (* write out problem file and call prover *)
blanchet@37514
   686
    fun command_line complete probfile =
blanchet@37514
   687
      let
blanchet@37514
   688
        val core = File.shell_path command ^ " " ^ arguments complete timeout ^
blanchet@37514
   689
                   " " ^ File.shell_path probfile
blanchet@37514
   690
      in
blanchet@37514
   691
        (if Config.get ctxt measure_runtime then
blanchet@37514
   692
           "TIMEFORMAT='%3U'; { time " ^ core ^ " ; }"
blanchet@37514
   693
         else
blanchet@37962
   694
           "exec " ^ core) ^ " 2>&1"
blanchet@37514
   695
      end
boehmes@32510
   696
    fun split_time s =
boehmes@32510
   697
      let
wenzelm@32942
   698
        val split = String.tokens (fn c => str c = "\n");
blanchet@36369
   699
        val (output, t) = s |> split |> split_last |> apfst cat_lines;
wenzelm@32942
   700
        fun as_num f = f >> (fst o read_int);
wenzelm@32942
   701
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
wenzelm@32942
   702
        val digit = Scan.one Symbol.is_ascii_digit;
wenzelm@32942
   703
        val num3 = as_num (digit ::: digit ::: (digit >> single));
wenzelm@32942
   704
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
wenzelm@32942
   705
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
blanchet@36369
   706
      in (output, as_time t) end;
boehmes@32458
   707
    fun run_on probfile =
blanchet@37962
   708
      if home = "" then
blanchet@37962
   709
        error ("The environment variable " ^ quote home_var ^ " is not set.")
blanchet@37962
   710
      else if File.exists command then
blanchet@37514
   711
        let
blanchet@37514
   712
          fun do_run complete =
blanchet@37514
   713
            let
blanchet@37514
   714
              val command = command_line complete probfile
blanchet@37514
   715
              val ((output, msecs), res_code) =
blanchet@37514
   716
                bash_output command
blanchet@37514
   717
                |>> (if overlord then
blanchet@37514
   718
                       prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@37514
   719
                     else
blanchet@37514
   720
                       I)
blanchet@37514
   721
                |>> (if Config.get ctxt measure_runtime then split_time
blanchet@37514
   722
                     else rpair 0)
blanchet@37514
   723
              val (proof, outcome) =
blanchet@37514
   724
                extract_proof_and_outcome complete res_code proof_delims
blanchet@37514
   725
                                          known_failures output
blanchet@37514
   726
            in (output, msecs, proof, outcome) end
blanchet@37643
   727
          val readable_names = debug andalso overlord
blanchet@37514
   728
          val (pool, conjecture_offset) =
blanchet@37994
   729
            write_tptp_file thy readable_names explicit_forall full_types
blanchet@37994
   730
                            explicit_apply probfile clauses
blanchet@37995
   731
          val conjecture_shape =
blanchet@37995
   732
            conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
blanchet@37514
   733
          val result =
blanchet@37514
   734
            do_run false
blanchet@37550
   735
            |> (fn (_, msecs0, _, SOME _) =>
blanchet@37514
   736
                   do_run true
blanchet@37514
   737
                   |> (fn (output, msecs, proof, outcome) =>
blanchet@37514
   738
                          (output, msecs0 + msecs, proof, outcome))
blanchet@37514
   739
                 | result => result)
blanchet@37514
   740
        in ((pool, conjecture_shape), result) end
blanchet@36382
   741
      else
blanchet@36382
   742
        error ("Bad executable: " ^ Path.implode command ^ ".");
wenzelm@28592
   743
blanchet@36167
   744
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@36167
   745
       the proof file too. *)
blanchet@36376
   746
    fun cleanup probfile =
blanchet@36376
   747
      if the_dest_dir = "" then try File.rm probfile else NONE
blanchet@37514
   748
    fun export probfile (_, (output, _, _, _)) =
blanchet@36376
   749
      if the_dest_dir = "" then
blanchet@36187
   750
        ()
blanchet@36187
   751
      else
blanchet@37514
   752
        File.write (Path.explode (Path.implode probfile ^ "_proof")) output
wenzelm@32257
   753
blanchet@37514
   754
    val ((pool, conjecture_shape), (output, msecs, proof, outcome)) =
blanchet@37514
   755
      with_path cleanup export run_on (prob_pathname subgoal)
blanchet@37989
   756
    val (conjecture_shape, internal_thm_names) =
blanchet@37989
   757
      repair_conjecture_shape_and_theorem_names output conjecture_shape
blanchet@37989
   758
                                                internal_thm_names
blanchet@37514
   759
blanchet@38015
   760
    val (message, used_thm_names) =
blanchet@36370
   761
      case outcome of
blanchet@36400
   762
        NONE =>
blanchet@36400
   763
        proof_text isar_proof
blanchet@37480
   764
            (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@37480
   765
            (full_types, minimize_command, proof, internal_thm_names, th,
blanchet@37480
   766
             subgoal)
blanchet@36370
   767
      | SOME failure => (string_for_failure failure ^ "\n", [])
boehmes@32864
   768
  in
blanchet@36393
   769
    {outcome = outcome, message = message, pool = pool,
blanchet@38015
   770
     used_thm_names = used_thm_names, atp_run_time_in_msecs = msecs,
blanchet@37514
   771
     output = output, proof = proof, internal_thm_names = internal_thm_names,
blanchet@36400
   772
     conjecture_shape = conjecture_shape,
blanchet@35969
   773
     filtered_clauses = the_filtered_clauses}
blanchet@37499
   774
  end
wenzelm@28596
   775
blanchet@35969
   776
fun tptp_prover name p = (name, generic_tptp_prover (name, p));
wenzelm@28596
   777
blanchet@36382
   778
fun to_generous_secs time = (Time.toMilliseconds time + 999) div 1000
blanchet@36142
   779
wenzelm@28596
   780
(* E prover *)
wenzelm@28596
   781
blanchet@36369
   782
val tstp_proof_delims =
blanchet@36369
   783
  ("# SZS output start CNFRefutation.", "# SZS output end CNFRefutation")
blanchet@36369
   784
blanchet@35969
   785
val e_config : prover_config =
blanchet@36917
   786
  {home_var = "E_HOME",
blanchet@36289
   787
   executable = "eproof",
blanchet@37514
   788
   arguments = fn _ => fn timeout =>
blanchet@36382
   789
     "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev --silent --cpu-limit=" ^
blanchet@36382
   790
     string_of_int (to_generous_secs timeout),
blanchet@36369
   791
   proof_delims = [tstp_proof_delims],
blanchet@36265
   792
   known_failures =
blanchet@37995
   793
     [(Unprovable, "SZS status: CounterSatisfiable"),
blanchet@37995
   794
      (Unprovable, "SZS status CounterSatisfiable"),
blanchet@36370
   795
      (TimedOut, "Failure: Resource limit exceeded (time)"),
blanchet@36370
   796
      (TimedOut, "time limit exceeded"),
blanchet@36370
   797
      (OutOfResources,
blanchet@36370
   798
       "# Cannot determine problem status within resource limit"),
blanchet@36370
   799
      (OutOfResources, "SZS status: ResourceOut"),
blanchet@36370
   800
      (OutOfResources, "SZS status ResourceOut")],
blanchet@38009
   801
   max_new_relevant_facts_per_iter = 80 (* FIXME *),
blanchet@37994
   802
   prefers_theory_relevant = false,
blanchet@37994
   803
   explicit_forall = false}
blanchet@35969
   804
val e = tptp_prover "e" e_config
wenzelm@28596
   805
wenzelm@28596
   806
blanchet@36219
   807
(* The "-VarWeight=3" option helps the higher-order problems, probably by
blanchet@36219
   808
   counteracting the presence of "hAPP". *)
blanchet@37498
   809
val spass_config : prover_config =
blanchet@37962
   810
  {home_var = "ISABELLE_ATP_MANAGER",
blanchet@37962
   811
   executable = "SPASS_TPTP",
blanchet@37550
   812
   (* "div 2" accounts for the fact that SPASS is often run twice. *)
blanchet@37514
   813
   arguments = fn complete => fn timeout =>
blanchet@37962
   814
     ("-Auto -PGiven=0 -PProblem=0 -Splits=0 -FullRed=0 -DocProof \
blanchet@37550
   815
      \-VarWeight=3 -TimeLimit=" ^
blanchet@37550
   816
      string_of_int (to_generous_secs timeout div 2))
blanchet@37514
   817
     |> not complete ? prefix "-SOS=1 ",
blanchet@36369
   818
   proof_delims = [("Here is a proof", "Formulae used in the proof")],
blanchet@36289
   819
   known_failures =
blanchet@37413
   820
     [(IncompleteUnprovable, "SPASS beiseite: Completion found"),
blanchet@36370
   821
      (TimedOut, "SPASS beiseite: Ran out of time"),
blanchet@36965
   822
      (OutOfResources, "SPASS beiseite: Maximal number of loops exceeded"),
blanchet@37413
   823
      (MalformedInput, "Undefined symbol"),
blanchet@37414
   824
      (MalformedInput, "Free Variable"),
blanchet@37962
   825
      (OldSpass, "tptp2dfg")],
blanchet@38009
   826
   max_new_relevant_facts_per_iter = 26 (* FIXME *),
blanchet@37994
   827
   prefers_theory_relevant = true,
blanchet@37994
   828
   explicit_forall = true}
blanchet@37414
   829
val spass = tptp_prover "spass" spass_config
wenzelm@28596
   830
blanchet@37509
   831
(* Vampire *)
blanchet@37509
   832
blanchet@37509
   833
val vampire_config : prover_config =
blanchet@37509
   834
  {home_var = "VAMPIRE_HOME",
blanchet@37509
   835
   executable = "vampire",
blanchet@37514
   836
   arguments = fn _ => fn timeout =>
blanchet@37509
   837
     "--output_syntax tptp --mode casc -t " ^
blanchet@37509
   838
     string_of_int (to_generous_secs timeout),
blanchet@37509
   839
   proof_delims =
blanchet@37509
   840
     [("=========== Refutation ==========",
blanchet@37509
   841
       "======= End of refutation ======="),
blanchet@37509
   842
      ("% SZS output start Refutation", "% SZS output end Refutation")],
blanchet@37509
   843
   known_failures =
blanchet@37509
   844
     [(Unprovable, "UNPROVABLE"),
blanchet@37509
   845
      (IncompleteUnprovable, "CANNOT PROVE"),
blanchet@37509
   846
      (Unprovable, "Satisfiability detected"),
blanchet@37509
   847
      (OutOfResources, "Refutation not found")],
blanchet@38009
   848
   max_new_relevant_facts_per_iter = 40 (* FIXME *),
blanchet@37994
   849
   prefers_theory_relevant = false,
blanchet@37994
   850
   explicit_forall = false}
blanchet@37509
   851
val vampire = tptp_prover "vampire" vampire_config
blanchet@37509
   852
blanchet@37509
   853
(* Remote prover invocation via SystemOnTPTP *)
wenzelm@28596
   854
blanchet@36376
   855
val systems = Synchronized.var "atp_systems" ([]: string list);
immler@31835
   856
immler@31835
   857
fun get_systems () =
blanchet@36370
   858
  case bash_output "\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w" of
blanchet@36370
   859
    (answer, 0) => split_lines answer
blanchet@36370
   860
  | (answer, _) =>
blanchet@37627
   861
    error ("Failed to get available systems at SystemOnTPTP:\n" ^
blanchet@37627
   862
           perhaps (try (unsuffix "\n")) answer)
immler@31835
   863
blanchet@35867
   864
fun refresh_systems_on_tptp () =
blanchet@37509
   865
  Synchronized.change systems (fn _ => get_systems ())
immler@31835
   866
immler@31835
   867
fun get_system prefix = Synchronized.change_result systems (fn systems =>
boehmes@32864
   868
  (if null systems then get_systems () else systems)
wenzelm@32942
   869
  |> `(find_first (String.isPrefix prefix)));
boehmes@32864
   870
wenzelm@32948
   871
fun the_system prefix =
boehmes@32864
   872
  (case get_system prefix of
blanchet@37509
   873
    NONE => error ("System " ^ quote prefix ^ " not available at SystemOnTPTP.")
wenzelm@32942
   874
  | SOME sys => sys);
immler@31835
   875
blanchet@36265
   876
val remote_known_failures =
blanchet@37627
   877
  [(CantConnect, "HTTP-Error"),
blanchet@37627
   878
   (TimedOut, "says Timeout"),
blanchet@36377
   879
   (MalformedOutput, "Remote script could not extract proof")]
blanchet@35865
   880
blanchet@37509
   881
fun remote_config atp_prefix args
blanchet@38009
   882
        ({proof_delims, known_failures, max_new_relevant_facts_per_iter,
blanchet@37994
   883
          prefers_theory_relevant, explicit_forall, ...} : prover_config)
blanchet@37994
   884
        : prover_config =
blanchet@36917
   885
  {home_var = "ISABELLE_ATP_MANAGER",
blanchet@36289
   886
   executable = "SystemOnTPTP",
blanchet@37514
   887
   arguments = fn _ => fn timeout =>
blanchet@36382
   888
     args ^ " -t " ^ string_of_int (to_generous_secs timeout) ^ " -s " ^
blanchet@36382
   889
     the_system atp_prefix,
blanchet@36369
   890
   proof_delims = insert (op =) tstp_proof_delims proof_delims,
blanchet@36265
   891
   known_failures = remote_known_failures @ known_failures,
blanchet@38009
   892
   max_new_relevant_facts_per_iter = max_new_relevant_facts_per_iter,
blanchet@37994
   893
   prefers_theory_relevant = prefers_theory_relevant,
blanchet@37994
   894
   explicit_forall = explicit_forall}
wenzelm@28596
   895
blanchet@37509
   896
fun remote_tptp_prover prover atp_prefix args config =
blanchet@37509
   897
  tptp_prover (remotify (fst prover)) (remote_config atp_prefix args config)
boehmes@32864
   898
blanchet@37509
   899
val remote_e = remote_tptp_prover e "EP---" "" e_config
blanchet@37509
   900
val remote_spass = remote_tptp_prover spass "SPASS---" "-x" spass_config
blanchet@37509
   901
val remote_vampire = remote_tptp_prover vampire "Vampire---9" "" vampire_config
wenzelm@28592
   902
blanchet@36917
   903
fun maybe_remote (name, _) ({home_var, ...} : prover_config) =
blanchet@36917
   904
  name |> getenv home_var = "" ? remotify
blanchet@36371
   905
blanchet@36371
   906
fun default_atps_param_value () =
blanchet@36371
   907
  space_implode " " [maybe_remote e e_config, maybe_remote spass spass_config,
blanchet@36371
   908
                     remotify (fst vampire)]
blanchet@36371
   909
blanchet@37509
   910
val provers = [e, spass, vampire, remote_e, remote_spass, remote_vampire]
blanchet@35867
   911
val prover_setup = fold add_prover provers
blanchet@35867
   912
blanchet@35867
   913
val setup =
blanchet@36376
   914
  dest_dir_setup
blanchet@35867
   915
  #> problem_prefix_setup
blanchet@35867
   916
  #> measure_runtime_setup
blanchet@36371
   917
  #> prover_setup
blanchet@35867
   918
wenzelm@28592
   919
end;