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