src/HOL/Tools/res_atp.ML
author paulson
Wed Aug 15 12:52:56 2007 +0200 (2007-08-15)
changeset 24286 7619080e49f0
parent 24215 5458fbf18276
child 24287 c857dac06da6
permissions -rw-r--r--
ATP blacklisting is now in theory data, attribute noatp
mengj@19194
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory, NICTA
paulson@15608
     2
    ID: $Id$
paulson@15608
     3
    Copyright 2004 University of Cambridge
paulson@15347
     4
paulson@15347
     5
ATPs with TPTP format input.
paulson@15347
     6
*)
paulson@15452
     7
wenzelm@16802
     8
signature RES_ATP =
wenzelm@16802
     9
sig
paulson@17306
    10
  val prover: string ref
paulson@17484
    11
  val destdir: string ref
paulson@17849
    12
  val helper_path: string -> string -> string
paulson@17484
    13
  val problem_name: string ref
paulson@17690
    14
  val time_limit: int ref
paulson@21224
    15
  val set_prover: string -> unit
wenzelm@21588
    16
mengj@19194
    17
  datatype mode = Auto | Fol | Hol
paulson@19450
    18
  val linkup_logic_mode : mode ref
mengj@19722
    19
  val write_subgoal_file: bool -> mode -> Proof.context -> thm list -> thm list -> int -> string
wenzelm@20289
    20
  val atp_method: (Proof.context -> thm list -> int -> tactic) ->
wenzelm@20289
    21
    Method.src -> Proof.context -> Proof.method
mengj@19194
    22
  val cond_rm_tmp: string -> unit
paulson@20246
    23
  val include_all: bool ref
mengj@19194
    24
  val run_relevance_filter: bool ref
mengj@19768
    25
  val run_blacklist_filter: bool ref
paulson@19894
    26
  val add_all : unit -> unit
mengj@19227
    27
  val add_claset : unit -> unit
mengj@19227
    28
  val add_simpset : unit -> unit
mengj@19227
    29
  val add_clasimp : unit -> unit
mengj@19227
    30
  val add_atpset : unit -> unit
paulson@19894
    31
  val rm_all : unit -> unit
mengj@19227
    32
  val rm_claset : unit -> unit
mengj@19227
    33
  val rm_simpset : unit -> unit
mengj@19227
    34
  val rm_atpset : unit -> unit
mengj@19227
    35
  val rm_clasimp : unit -> unit
mengj@21311
    36
  val is_fol_thms : thm list -> bool
paulson@22989
    37
  val tvar_classes_of_terms : term list -> string list
paulson@22989
    38
  val tfree_classes_of_terms : term list -> string list
paulson@22989
    39
  val type_consts_of_terms : theory -> term list -> string list
paulson@15347
    40
end;
paulson@15347
    41
wenzelm@22865
    42
structure ResAtp: RES_ATP =
paulson@15347
    43
struct
paulson@15347
    44
wenzelm@22130
    45
fun timestamp s = Output.debug (fn () => ("At " ^ Time.toString (Time.now()) ^ ": " ^ s));
paulson@21070
    46
mengj@19194
    47
(********************************************************************)
mengj@19194
    48
(* some settings for both background automatic ATP calling procedure*)
mengj@19194
    49
(* and also explicit ATP invocation methods                         *)
mengj@19194
    50
(********************************************************************)
mengj@19194
    51
mengj@19194
    52
(*** background linkup ***)
paulson@21224
    53
val time_limit = ref 60;
wenzelm@21588
    54
val prover = ref "";
paulson@21224
    55
wenzelm@21588
    56
fun set_prover atp =
paulson@21224
    57
  case String.map Char.toLower atp of
wenzelm@21588
    58
      "e" =>
paulson@21290
    59
          (ReduceAxiomsN.max_new := 100;
paulson@21224
    60
           ReduceAxiomsN.theory_const := false;
paulson@21224
    61
           prover := "E")
wenzelm@21588
    62
    | "spass" =>
paulson@21224
    63
          (ReduceAxiomsN.max_new := 40;
paulson@21224
    64
           ReduceAxiomsN.theory_const := true;
paulson@21224
    65
           prover := "spass")
wenzelm@21588
    66
    | "vampire" =>
paulson@21224
    67
          (ReduceAxiomsN.max_new := 60;
paulson@21224
    68
           ReduceAxiomsN.theory_const := false;
paulson@21224
    69
           prover := "vampire")
paulson@21224
    70
    | _ => error ("No such prover: " ^ atp);
paulson@21224
    71
paulson@21224
    72
val _ = set_prover "E"; (* use E as the default prover *)
paulson@21224
    73
paulson@17484
    74
val destdir = ref "";   (*Empty means write files to /tmp*)
paulson@17484
    75
val problem_name = ref "prob";
paulson@17484
    76
paulson@17819
    77
(*Return the path to a "helper" like SPASS or tptp2X, first checking that
wenzelm@21588
    78
  it exists.  FIXME: modify to use Path primitives and move to some central place.*)
paulson@17819
    79
fun helper_path evar base =
paulson@17819
    80
  case getenv evar of
paulson@17819
    81
      "" => error  ("Isabelle environment variable " ^ evar ^ " not defined")
wenzelm@21588
    82
    | home =>
paulson@17819
    83
        let val path = home ^ "/" ^ base
wenzelm@21858
    84
        in  if File.exists (File.explode_platform_path path) then path
wenzelm@21588
    85
            else error ("Could not find the file " ^ path)
wenzelm@21588
    86
        end;
paulson@17819
    87
wenzelm@21588
    88
fun probfile_nosuffix _ =
paulson@17484
    89
  if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
wenzelm@21858
    90
  else if File.exists (File.explode_platform_path (!destdir))
paulson@17484
    91
  then !destdir ^ "/" ^ !problem_name
paulson@17484
    92
  else error ("No such directory: " ^ !destdir);
quigley@15644
    93
paulson@17717
    94
fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n;
paulson@17717
    95
mengj@19194
    96
fun atp_input_file () =
wenzelm@21588
    97
    let val file = !problem_name
mengj@19194
    98
    in
wenzelm@21588
    99
        if !destdir = "" then File.platform_path (File.tmp_path (Path.basic file))
wenzelm@21858
   100
        else if File.exists (File.explode_platform_path (!destdir))
wenzelm@21588
   101
        then !destdir ^ "/" ^ file
wenzelm@21588
   102
        else error ("No such directory: " ^ !destdir)
mengj@19194
   103
    end;
mengj@19194
   104
paulson@20868
   105
val include_all = ref true;
mengj@19194
   106
val include_simpset = ref false;
wenzelm@21588
   107
val include_claset = ref false;
mengj@19194
   108
val include_atpset = ref true;
paulson@20246
   109
paulson@20246
   110
(*Tests show that follow_defs gives VERY poor results with "include_all"*)
paulson@20246
   111
fun add_all() = (include_all:=true; ReduceAxiomsN.follow_defs := false);
paulson@20246
   112
fun rm_all() = include_all:=false;
paulson@20246
   113
paulson@20246
   114
fun add_simpset() = include_simpset:=true;
paulson@20246
   115
fun rm_simpset() = include_simpset:=false;
paulson@20246
   116
paulson@20246
   117
fun add_claset() = include_claset:=true;
paulson@20246
   118
fun rm_claset() = include_claset:=false;
paulson@20246
   119
paulson@20246
   120
fun add_clasimp() = (include_simpset:=true;include_claset:=true);
paulson@20246
   121
fun rm_clasimp() = (include_simpset:=false;include_claset:=false);
paulson@20246
   122
paulson@20246
   123
fun add_atpset() = include_atpset:=true;
paulson@20246
   124
fun rm_atpset() = include_atpset:=false;
mengj@19194
   125
mengj@19194
   126
mengj@19194
   127
(**** relevance filter ****)
paulson@20457
   128
val run_relevance_filter = ReduceAxiomsN.run_relevance_filter;
mengj@19768
   129
val run_blacklist_filter = ref true;
mengj@19194
   130
mengj@19194
   131
(******************************************************************)
mengj@19194
   132
(* detect whether a given problem (clauses) is FOL/HOL/HOLC/HOLCS *)
mengj@19194
   133
(******************************************************************)
mengj@19194
   134
mengj@19194
   135
datatype logic = FOL | HOL | HOLC | HOLCS;
mengj@19194
   136
mengj@19194
   137
fun string_of_logic FOL = "FOL"
mengj@19194
   138
  | string_of_logic HOL = "HOL"
mengj@19194
   139
  | string_of_logic HOLC = "HOLC"
mengj@19194
   140
  | string_of_logic HOLCS = "HOLCS";
mengj@19194
   141
mengj@19194
   142
fun is_fol_logic FOL = true
mengj@19194
   143
  | is_fol_logic  _ = false
mengj@19194
   144
mengj@19194
   145
(*HOLCS will not occur here*)
mengj@19194
   146
fun upgrade_lg HOLC _ = HOLC
mengj@19194
   147
  | upgrade_lg HOL HOLC = HOLC
mengj@19194
   148
  | upgrade_lg HOL _ = HOL
wenzelm@21588
   149
  | upgrade_lg FOL lg = lg;
mengj@19194
   150
mengj@19194
   151
(* check types *)
mengj@19451
   152
fun has_bool_hfn (Type("bool",_)) = true
mengj@19451
   153
  | has_bool_hfn (Type("fun",_)) = true
mengj@19451
   154
  | has_bool_hfn (Type(_, Ts)) = exists has_bool_hfn Ts
mengj@19451
   155
  | has_bool_hfn _ = false;
mengj@19194
   156
mengj@19451
   157
fun is_hol_fn tp =
mengj@19194
   158
    let val (targs,tr) = strip_type tp
mengj@19194
   159
    in
wenzelm@21588
   160
        exists (has_bool_hfn) (tr::targs)
mengj@19194
   161
    end;
mengj@19194
   162
mengj@19451
   163
fun is_hol_pred tp =
mengj@19451
   164
    let val (targs,tr) = strip_type tp
mengj@19451
   165
    in
wenzelm@21588
   166
        exists (has_bool_hfn) targs
mengj@19451
   167
    end;
mengj@19194
   168
mengj@19194
   169
exception FN_LG of term;
mengj@19194
   170
wenzelm@21588
   171
fun fn_lg (t as Const(f,tp)) (lg,seen) =
wenzelm@21588
   172
    if is_hol_fn tp then (upgrade_lg HOL lg, insert (op =) t seen) else (lg, insert (op =) t seen)
wenzelm@21588
   173
  | fn_lg (t as Free(f,tp)) (lg,seen) =
wenzelm@21588
   174
    if is_hol_fn tp then (upgrade_lg HOL lg, insert (op =) t seen) else (lg, insert (op =) t seen)
mengj@19194
   175
  | fn_lg (t as Var(f,tp)) (lg,seen) =
haftmann@20854
   176
    if is_hol_fn tp then (upgrade_lg HOL lg,insert (op =) t seen) else (lg,insert (op =) t seen)
haftmann@20854
   177
  | fn_lg (t as Abs(_,_,_)) (lg,seen) = (upgrade_lg HOLC lg,insert (op =) t seen)
wenzelm@21588
   178
  | fn_lg f _ = raise FN_LG(f);
mengj@19194
   179
mengj@19194
   180
mengj@19194
   181
fun term_lg [] (lg,seen) = (lg,seen)
mengj@19194
   182
  | term_lg (tm::tms) (FOL,seen) =
paulson@20457
   183
      let val (f,args) = strip_comb tm
wenzelm@21588
   184
          val (lg',seen') = if f mem seen then (FOL,seen)
wenzelm@21588
   185
                            else fn_lg f (FOL,seen)
paulson@20457
   186
      in
wenzelm@21588
   187
        if is_fol_logic lg' then ()
wenzelm@22130
   188
        else Output.debug (fn () => ("Found a HOL term: " ^ Display.raw_string_of_term f));
paulson@20457
   189
        term_lg (args@tms) (lg',seen')
paulson@20457
   190
      end
mengj@19194
   191
  | term_lg _ (lg,seen) = (lg,seen)
mengj@19194
   192
mengj@19194
   193
exception PRED_LG of term;
mengj@19194
   194
wenzelm@21588
   195
fun pred_lg (t as Const(P,tp)) (lg,seen)=
wenzelm@21588
   196
      if is_hol_pred tp then (upgrade_lg HOL lg, insert (op =) t seen)
wenzelm@21588
   197
      else (lg,insert (op =) t seen)
mengj@19194
   198
  | pred_lg (t as Free(P,tp)) (lg,seen) =
wenzelm@21588
   199
      if is_hol_pred tp then (upgrade_lg HOL lg, insert (op =) t seen)
paulson@21470
   200
      else (lg,insert (op =) t seen)
haftmann@20854
   201
  | pred_lg (t as Var(_,_)) (lg,seen) = (upgrade_lg HOL lg, insert (op =) t seen)
mengj@19194
   202
  | pred_lg P _ = raise PRED_LG(P);
mengj@19194
   203
mengj@19194
   204
mengj@19194
   205
fun lit_lg (Const("Not",_) $ P) (lg,seen) = lit_lg P (lg,seen)
mengj@19194
   206
  | lit_lg P (lg,seen) =
paulson@20457
   207
      let val (pred,args) = strip_comb P
wenzelm@21588
   208
          val (lg',seen') = if pred mem seen then (lg,seen)
wenzelm@21588
   209
                            else pred_lg pred (lg,seen)
paulson@20457
   210
      in
wenzelm@21588
   211
        if is_fol_logic lg' then ()
wenzelm@22130
   212
        else Output.debug (fn () => ("Found a HOL predicate: " ^ Display.raw_string_of_term pred));
wenzelm@21588
   213
        term_lg args (lg',seen')
paulson@20457
   214
      end;
mengj@19194
   215
mengj@19194
   216
fun lits_lg [] (lg,seen) = (lg,seen)
mengj@19194
   217
  | lits_lg (lit::lits) (FOL,seen) =
paulson@20457
   218
      let val (lg,seen') = lit_lg lit (FOL,seen)
paulson@20457
   219
      in
wenzelm@21588
   220
        if is_fol_logic lg then ()
wenzelm@22130
   221
        else Output.debug (fn () => ("Found a HOL literal: " ^ Display.raw_string_of_term lit));
wenzelm@21588
   222
        lits_lg lits (lg,seen')
paulson@20457
   223
      end
mengj@19194
   224
  | lits_lg lits (lg,seen) = (lg,seen);
mengj@19194
   225
paulson@21470
   226
fun dest_disj_aux (Const("Trueprop",_) $ t) disjs = dest_disj_aux t disjs
paulson@21470
   227
  | dest_disj_aux (Const ("op |", _) $ t $ t') disjs = dest_disj_aux t (dest_disj_aux t' disjs)
mengj@19227
   228
  | dest_disj_aux t disjs = t::disjs;
mengj@19227
   229
mengj@19227
   230
fun dest_disj t = dest_disj_aux t [];
mengj@19227
   231
paulson@21470
   232
fun logic_of_clause tm = lits_lg (dest_disj tm);
mengj@19194
   233
mengj@19194
   234
fun logic_of_clauses [] (lg,seen) = (lg,seen)
mengj@19194
   235
  | logic_of_clauses (cls::clss) (FOL,seen) =
mengj@19227
   236
    let val (lg,seen') = logic_of_clause cls (FOL,seen)
wenzelm@21588
   237
        val _ =
wenzelm@19641
   238
          if is_fol_logic lg then ()
wenzelm@22130
   239
          else Output.debug (fn () => ("Found a HOL clause: " ^ Display.raw_string_of_term cls))
mengj@19227
   240
    in
wenzelm@21588
   241
        logic_of_clauses clss (lg,seen')
mengj@19227
   242
    end
mengj@19194
   243
  | logic_of_clauses (cls::clss) (lg,seen) = (lg,seen);
mengj@19194
   244
mengj@19194
   245
fun problem_logic_goals_aux [] (lg,seen) = lg
wenzelm@21588
   246
  | problem_logic_goals_aux (subgoal::subgoals) (lg,seen) =
mengj@19194
   247
    problem_logic_goals_aux subgoals (logic_of_clauses subgoal (lg,seen));
wenzelm@21588
   248
mengj@19194
   249
fun problem_logic_goals subgoals = problem_logic_goals_aux subgoals (FOL,[]);
mengj@19194
   250
mengj@21311
   251
fun is_fol_thms ths = ((fst(logic_of_clauses (map prop_of ths) (FOL,[]))) = FOL);
mengj@21311
   252
mengj@19768
   253
(***************************************************************)
mengj@19768
   254
(* Retrieving and filtering lemmas                             *)
mengj@19768
   255
(***************************************************************)
mengj@19768
   256
mengj@19768
   257
(*** white list and black list of lemmas ***)
mengj@19768
   258
paulson@24215
   259
(*The rule subsetI is frequently omitted by the relevance filter. This could be theory data
paulson@24215
   260
  or identified with ATPset (which however is too big currently)*)
paulson@24215
   261
val whitelist = [subsetI];
wenzelm@21588
   262
mengj@19768
   263
(*** retrieve lemmas from clasimpset and atpset, may filter them ***)
mengj@19768
   264
mengj@19768
   265
(*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute*)
mengj@19768
   266
paulson@22382
   267
fun setinsert (x,s) = Symtab.update (x,()) s;
mengj@19768
   268
paulson@20757
   269
(*Reject theorems with names like "List.filter.filter_list_def" or
paulson@21690
   270
  "Accessible_Part.acc.defs", as these are definitions arising from packages.*)
paulson@20757
   271
fun is_package_def a =
wenzelm@21858
   272
  let val names = NameSpace.explode a
paulson@21690
   273
  in
paulson@21690
   274
     length names > 2 andalso
paulson@21690
   275
     not (hd names = "local") andalso
paulson@21690
   276
     String.isSuffix "_def" a  orelse  String.isSuffix "_defs" a
paulson@21690
   277
  end;
paulson@20757
   278
mengj@19768
   279
(** a hash function from Term.term to int, and also a hash table **)
mengj@19768
   280
val xor_words = List.foldl Word.xorb 0w0;
mengj@19768
   281
mengj@19768
   282
fun hashw_term ((Const(c,_)), w) = Polyhash.hashw_string (c,w)
paulson@20661
   283
  | hashw_term ((Free(a,_)), w) = Polyhash.hashw_string (a,w)
mengj@19768
   284
  | hashw_term ((Var(_,_)), w) = w
paulson@20661
   285
  | hashw_term ((Bound i), w) = Polyhash.hashw_int(i,w)
mengj@19768
   286
  | hashw_term ((Abs(_,_,t)), w) = hashw_term (t, w)
mengj@19768
   287
  | hashw_term ((P$Q), w) = hashw_term (Q, (hashw_term (P, w)));
mengj@19768
   288
paulson@21070
   289
fun hash_literal (Const("Not",_)$P) = Word.notb(hashw_term(P,0w0))
paulson@21070
   290
  | hash_literal P = hashw_term(P,0w0);
mengj@19768
   291
paulson@21470
   292
fun hash_term t = Word.toIntX (xor_words (map hash_literal (dest_disj t)));
mengj@19768
   293
mengj@19768
   294
fun equal_thm (thm1,thm2) = Term.aconv(prop_of thm1, prop_of thm2);
paulson@20457
   295
paulson@22382
   296
exception HASH_CLAUSE;
paulson@22382
   297
mengj@19768
   298
(*Create a hash table for clauses, of the given size*)
mengj@19768
   299
fun mk_clause_table n =
paulson@20457
   300
      Polyhash.mkTable (hash_term o prop_of, equal_thm)
mengj@19768
   301
                       (n, HASH_CLAUSE);
mengj@19768
   302
paulson@20457
   303
(*Use a hash table to eliminate duplicates from xs. Argument is a list of
paulson@20868
   304
  (thm * (string * int)) tuples. The theorems are hashed into the table. *)
wenzelm@21588
   305
fun make_unique xs =
paulson@20868
   306
  let val ht = mk_clause_table 7000
paulson@20457
   307
  in
wenzelm@22130
   308
      Output.debug (fn () => ("make_unique gets " ^ Int.toString (length xs) ^ " clauses"));
wenzelm@21588
   309
      app (ignore o Polyhash.peekInsert ht) xs;
paulson@20868
   310
      Polyhash.listItems ht
paulson@20457
   311
  end;
mengj@19768
   312
paulson@20868
   313
(*Remove existing axiom clauses from the conjecture clauses, as this can dramatically
paulson@20868
   314
  boost an ATP's performance (for some reason)*)
wenzelm@21588
   315
fun subtract_cls c_clauses ax_clauses =
paulson@20868
   316
  let val ht = mk_clause_table 2200
paulson@20868
   317
      fun known x = isSome (Polyhash.peek ht x)
paulson@20868
   318
  in
wenzelm@21588
   319
      app (ignore o Polyhash.peekInsert ht) ax_clauses;
wenzelm@21588
   320
      filter (not o known) c_clauses
paulson@20868
   321
  end;
mengj@19768
   322
wenzelm@21588
   323
(*Filter axiom clauses, but keep supplied clauses and clauses in whitelist.
paulson@20868
   324
  Duplicates are removed later.*)
paulson@20457
   325
fun get_relevant_clauses thy cls_thms white_cls goals =
paulson@20868
   326
  white_cls @ (ReduceAxiomsN.relevance_filter thy cls_thms goals);
paulson@20457
   327
paulson@21224
   328
fun all_valid_thms ctxt =
paulson@22382
   329
  let
paulson@24286
   330
    fun blacklisted s = !run_blacklist_filter andalso is_package_def s
paulson@22382
   331
paulson@22382
   332
    fun extern_valid space (name, ths) =
paulson@22382
   333
      let
paulson@22382
   334
        val is_valid = ProofContext.valid_thms ctxt;
paulson@22382
   335
        val xname = NameSpace.extern space name;
paulson@22382
   336
      in
paulson@22382
   337
        if blacklisted name then I
paulson@22382
   338
        else if is_valid (xname, ths) then cons (xname, ths)
paulson@22382
   339
        else if is_valid (name, ths) then cons (name, ths)
paulson@22382
   340
        else I
paulson@22382
   341
      end;
paulson@22382
   342
    val thy = ProofContext.theory_of ctxt;
paulson@22382
   343
    val all_thys = thy :: Theory.ancestors_of thy;
paulson@22382
   344
paulson@22382
   345
    fun dest_valid (space, tab) = Symtab.fold (extern_valid space) tab [];
paulson@22382
   346
  in
paulson@22382
   347
    maps (dest_valid o PureThy.theorems_of) all_thys @
paulson@22382
   348
    fold (extern_valid (#1 (ProofContext.theorems_of ctxt)))
paulson@22382
   349
      (FactIndex.find (ProofContext.fact_index_of ctxt) ([], [])) []
paulson@22382
   350
  end;
paulson@21224
   351
wenzelm@21588
   352
fun multi_name a (th, (n,pairs)) =
paulson@21224
   353
  (n+1, (a ^ "(" ^ Int.toString n ^ ")", th) :: pairs)
paulson@21224
   354
paulson@22382
   355
fun add_single_names ((a, []), pairs) = pairs
paulson@22382
   356
  | add_single_names ((a, [th]), pairs) = (a,th)::pairs
paulson@22382
   357
  | add_single_names ((a, ths), pairs) = #2 (foldl (multi_name a) (1,pairs) ths);
paulson@21431
   358
paulson@21470
   359
val multi_base_blacklist =
paulson@21470
   360
  ["defs","select_defs","update_defs","induct","inducts","split","splits","split_asm"];
paulson@21470
   361
paulson@22382
   362
(*Ignore blacklisted basenames*)
wenzelm@21588
   363
fun add_multi_names ((a, ths), pairs) =
paulson@22382
   364
  if (Sign.base_name a) mem_string multi_base_blacklist  then pairs
paulson@22382
   365
  else add_single_names ((a, ths), pairs);
paulson@21224
   366
paulson@21290
   367
fun is_multi (a, ths) = length ths > 1 orelse String.isSuffix ".axioms" a;
paulson@21290
   368
paulson@24286
   369
(*The single theorems go BEFORE the multiple ones. Blacklist is applied to all.*)
wenzelm@21588
   370
fun name_thm_pairs ctxt =
paulson@21290
   371
  let val (mults,singles) = List.partition is_multi (all_valid_thms ctxt)
paulson@24286
   372
      val ht = mk_clause_table 900   (*ht for blacklisted theorems*)
paulson@24286
   373
      fun blacklisted x = !run_blacklist_filter andalso isSome (Polyhash.peek ht x)
paulson@24286
   374
  in
paulson@24286
   375
      app (fn th => ignore (Polyhash.peekInsert ht (th,()))) (ResBlacklist.get ctxt);
paulson@24286
   376
      filter (not o blacklisted o #2)
paulson@24286
   377
        (foldl add_single_names (foldl add_multi_names [] mults) singles)
paulson@24286
   378
  end;
paulson@21224
   379
paulson@21224
   380
fun check_named ("",th) = (warning ("No name for theorem " ^ string_of_thm th); false)
paulson@21224
   381
  | check_named (_,th) = true;
paulson@19894
   382
paulson@22193
   383
fun display_thm (name,th) = Output.debug (fn () => name ^ ": " ^ string_of_thm th);
paulson@22193
   384
mengj@19768
   385
(* get lemmas from claset, simpset, atpset and extra supplied rules *)
wenzelm@21588
   386
fun get_clasimp_atp_lemmas ctxt user_thms =
paulson@19894
   387
  let val included_thms =
wenzelm@21588
   388
        if !include_all
wenzelm@21588
   389
        then (tap (fn ths => Output.debug
wenzelm@22130
   390
                     (fn () => ("Including all " ^ Int.toString (length ths) ^ " theorems")))
wenzelm@21588
   391
                  (name_thm_pairs ctxt))
wenzelm@21588
   392
        else
wenzelm@21588
   393
        let val claset_thms =
wenzelm@21588
   394
                if !include_claset then ResAxioms.claset_rules_of ctxt
wenzelm@21588
   395
                else []
wenzelm@21588
   396
            val simpset_thms =
wenzelm@21588
   397
                if !include_simpset then ResAxioms.simpset_rules_of ctxt
wenzelm@21588
   398
                else []
wenzelm@21588
   399
            val atpset_thms =
wenzelm@21588
   400
                if !include_atpset then ResAxioms.atpset_rules_of ctxt
wenzelm@21588
   401
                else []
paulson@22193
   402
            val _ = (Output.debug (fn () => "ATP theorems: ");  app display_thm atpset_thms)
wenzelm@21588
   403
        in  claset_thms @ simpset_thms @ atpset_thms  end
wenzelm@21588
   404
      val user_rules = filter check_named
paulson@22382
   405
                         (map ResAxioms.pairname
paulson@24215
   406
                           (if null user_thms then whitelist else user_thms))
paulson@19894
   407
  in
paulson@21224
   408
      (filter check_named included_thms, user_rules)
paulson@19894
   409
  end;
mengj@19768
   410
paulson@21290
   411
(***************************************************************)
paulson@21290
   412
(* Type Classes Present in the Axiom or Conjecture Clauses     *)
paulson@21290
   413
(***************************************************************)
paulson@21290
   414
paulson@21290
   415
fun add_classes (sorts, cset) = foldl setinsert cset (List.concat sorts);
paulson@21290
   416
paulson@21290
   417
(*Remove this trivial type class*)
paulson@21290
   418
fun delete_type cset = Symtab.delete_safe "HOL.type" cset;
paulson@21290
   419
paulson@21290
   420
fun tvar_classes_of_terms ts =
paulson@21290
   421
  let val sorts_list = map (map #2 o term_tvars) ts
paulson@21290
   422
  in  Symtab.keys (delete_type (foldl add_classes Symtab.empty sorts_list))  end;
paulson@21290
   423
paulson@21290
   424
fun tfree_classes_of_terms ts =
paulson@21290
   425
  let val sorts_list = map (map #2 o term_tfrees) ts
paulson@21290
   426
  in  Symtab.keys (delete_type (foldl add_classes Symtab.empty sorts_list))  end;
paulson@20526
   427
paulson@21373
   428
(*fold type constructors*)
paulson@21373
   429
fun fold_type_consts f (Type (a, Ts)) x = fold (fold_type_consts f) Ts (f (a,x))
paulson@21373
   430
  | fold_type_consts f T x = x;
paulson@21373
   431
paulson@21373
   432
val add_type_consts_in_type = fold_type_consts setinsert;
paulson@21373
   433
paulson@21397
   434
(*Type constructors used to instantiate overloaded constants are the only ones needed.*)
paulson@21397
   435
fun add_type_consts_in_term thy =
paulson@21397
   436
  let val const_typargs = Sign.const_typargs thy
paulson@21397
   437
      fun add_tcs (Const cT) x = fold add_type_consts_in_type (const_typargs cT) x
paulson@21397
   438
        | add_tcs (Abs (_, T, u)) x = add_tcs u x
paulson@21397
   439
        | add_tcs (t $ u) x = add_tcs t (add_tcs u x)
paulson@21397
   440
        | add_tcs _ x = x
paulson@21397
   441
  in  add_tcs  end
paulson@21373
   442
paulson@21397
   443
fun type_consts_of_terms thy ts =
paulson@21397
   444
  Symtab.keys (fold (add_type_consts_in_term thy) ts Symtab.empty);
paulson@21373
   445
paulson@21373
   446
mengj@19194
   447
(***************************************************************)
mengj@19194
   448
(* ATP invocation methods setup                                *)
mengj@19194
   449
(***************************************************************)
mengj@19194
   450
wenzelm@21588
   451
fun cnf_hyps_thms ctxt =
wenzelm@20224
   452
    let val ths = Assumption.prems_of ctxt
wenzelm@19617
   453
    in fold (fold (insert Thm.eq_thm) o ResAxioms.skolem_thm) ths [] end;
mengj@19194
   454
paulson@20526
   455
(*Translation mode can be auto-detected, or forced to be first-order or higher-order*)
mengj@19194
   456
datatype mode = Auto | Fol | Hol;
mengj@19194
   457
paulson@19450
   458
val linkup_logic_mode = ref Auto;
paulson@19205
   459
paulson@20526
   460
(*Ensures that no higher-order theorems "leak out"*)
paulson@22382
   461
fun restrict_to_logic thy logic cls =
paulson@22382
   462
  if is_fol_logic logic then filter (Meson.is_fol_term thy o prop_of o fst) cls
wenzelm@21588
   463
                        else cls;
paulson@20526
   464
paulson@21470
   465
(**** Predicates to detect unwanted clauses (prolific or likely to cause unsoundness) ****)
paulson@21470
   466
paulson@21470
   467
(** Too general means, positive equality literal with a variable X as one operand,
paulson@21470
   468
  when X does not occur properly in the other operand. This rules out clearly
paulson@21470
   469
  inconsistent clauses such as V=a|V=b, though it by no means guarantees soundness. **)
wenzelm@21588
   470
paulson@21470
   471
fun occurs ix =
paulson@21470
   472
    let fun occ(Var (jx,_)) = (ix=jx)
paulson@21470
   473
          | occ(t1$t2)      = occ t1 orelse occ t2
paulson@21470
   474
          | occ(Abs(_,_,t)) = occ t
paulson@21470
   475
          | occ _           = false
paulson@21470
   476
    in occ end;
paulson@21470
   477
paulson@21470
   478
fun is_recordtype T = not (null (RecordPackage.dest_recTs T));
paulson@21470
   479
paulson@21470
   480
(*Unwanted equalities include
paulson@21470
   481
  (1) those between a variable that does not properly occur in the second operand,
paulson@21470
   482
  (2) those between a variable and a record, since these seem to be prolific "cases" thms
wenzelm@21588
   483
*)
paulson@21470
   484
fun too_general_eqterms (Var (ix,T), t) = not (occurs ix t) orelse is_recordtype T
paulson@21470
   485
  | too_general_eqterms _ = false;
paulson@21470
   486
paulson@21470
   487
fun too_general_equality (Const ("op =", _) $ x $ y) =
paulson@21470
   488
      too_general_eqterms (x,y) orelse too_general_eqterms(y,x)
paulson@21470
   489
  | too_general_equality _ = false;
paulson@21470
   490
paulson@21470
   491
(* tautologous? *)
paulson@21470
   492
fun is_taut (Const ("Trueprop", _) $ Const ("True", _)) = true
paulson@21470
   493
  | is_taut _ = false;
paulson@21470
   494
paulson@21431
   495
(*True if the term contains a variable whose (atomic) type is in the given list.*)
wenzelm@21588
   496
fun has_typed_var tycons =
paulson@21431
   497
  let fun var_tycon (Var (_, Type(a,_))) = a mem_string tycons
paulson@21431
   498
        | var_tycon _ = false
paulson@21431
   499
  in  exists var_tycon o term_vars  end;
paulson@21431
   500
paulson@22217
   501
(*Clauses are forbidden to contain variables of these types. The typical reason is that
paulson@22217
   502
  they lead to unsoundness. Note that "unit" satisfies numerous equations like ?X=().
paulson@22217
   503
  The resulting clause will have no type constraint, yielding false proofs. Even "bool"
paulson@22217
   504
  leads to many unsound proofs, though (obviously) only for higher-order problems.*)
paulson@24215
   505
val unwanted_types = ["Product_Type.unit","bool"];
paulson@22217
   506
paulson@21470
   507
fun unwanted t =
paulson@24215
   508
    is_taut t orelse has_typed_var unwanted_types t orelse
paulson@21470
   509
    forall too_general_equality (dest_disj t);
paulson@21470
   510
paulson@21431
   511
(*Clauses containing variables of type "unit" or "bool" are unlikely to be useful and
paulson@21431
   512
  likely to lead to unsound proofs.*)
paulson@22217
   513
fun remove_unwanted_clauses cls = filter (not o unwanted o prop_of o fst) cls;
paulson@21431
   514
paulson@23387
   515
fun tptp_writer logic = ResHolClause.tptp_write_file (logic=FOL);
paulson@19445
   516
paulson@23387
   517
fun dfg_writer logic = ResHolClause.dfg_write_file (logic=FOL);
mengj@19194
   518
paulson@20022
   519
(*Called by the oracle-based methods declared in res_atp_methods.ML*)
mengj@19722
   520
fun write_subgoal_file dfg mode ctxt conjectures user_thms n =
wenzelm@21588
   521
    let val conj_cls = make_clauses conjectures
paulson@22731
   522
                         |> ResAxioms.assume_abstract_list "subgoal" |> Meson.finish_cnf
wenzelm@21588
   523
        val hyp_cls = cnf_hyps_thms ctxt
wenzelm@21588
   524
        val goal_cls = conj_cls@hyp_cls
wenzelm@21588
   525
        val goal_tms = map prop_of goal_cls
paulson@22382
   526
        val thy = ProofContext.theory_of ctxt
wenzelm@21588
   527
        val logic = case mode of
paulson@21431
   528
                            Auto => problem_logic_goals [goal_tms]
wenzelm@21588
   529
                          | Fol => FOL
wenzelm@21588
   530
                          | Hol => HOL
wenzelm@21588
   531
        val (included_thms,user_rules) = get_clasimp_atp_lemmas ctxt user_thms
wenzelm@22865
   532
        val cla_simp_atp_clauses = included_thms
wenzelm@21588
   533
                                     |> ResAxioms.cnf_rules_pairs |> make_unique
paulson@22382
   534
                                     |> restrict_to_logic thy logic
paulson@21470
   535
                                     |> remove_unwanted_clauses
wenzelm@21588
   536
        val user_cls = ResAxioms.cnf_rules_pairs user_rules
wenzelm@21588
   537
        val axclauses = make_unique (get_relevant_clauses thy cla_simp_atp_clauses user_cls goal_tms)
paulson@21431
   538
        val subs = tfree_classes_of_terms goal_tms
paulson@21373
   539
        and axtms = map (prop_of o #1) axclauses
paulson@21373
   540
        val supers = tvar_classes_of_terms axtms
paulson@21431
   541
        and tycons = type_consts_of_terms thy (goal_tms@axtms)
paulson@21290
   542
        (*TFrees in conjecture clauses; TVars in axiom clauses*)
paulson@22645
   543
        val (supers',arity_clauses) = ResClause.make_arity_clauses thy tycons supers
paulson@22645
   544
        val classrel_clauses = ResClause.make_classrel_clauses thy subs supers'
wenzelm@21588
   545
        val writer = if dfg then dfg_writer else tptp_writer
wenzelm@21588
   546
        and file = atp_input_file()
wenzelm@21588
   547
        and user_lemmas_names = map #1 user_rules
mengj@19194
   548
    in
wenzelm@21588
   549
        writer logic goal_cls file (axclauses,classrel_clauses,arity_clauses) user_lemmas_names;
wenzelm@22130
   550
        Output.debug (fn () => "Writing to " ^ file);
wenzelm@21588
   551
        file
mengj@19194
   552
    end;
mengj@19194
   553
mengj@19194
   554
mengj@19194
   555
(**** remove tmp files ****)
wenzelm@21588
   556
fun cond_rm_tmp file =
wenzelm@22130
   557
    if !Output.debugging orelse !destdir <> ""
wenzelm@22130
   558
    then Output.debug (fn () => "ATP input kept...")
paulson@20870
   559
    else OS.FileSys.remove file;
mengj@19194
   560
mengj@19194
   561
mengj@19194
   562
(****** setup ATPs as Isabelle methods ******)
mengj@19194
   563
wenzelm@21588
   564
fun atp_meth tac ths ctxt =
mengj@19194
   565
    let val thy = ProofContext.theory_of ctxt
wenzelm@21588
   566
        val _ = ResClause.init thy
wenzelm@21588
   567
        val _ = ResHolClause.init thy
wenzelm@21588
   568
    in Method.SIMPLE_METHOD' (tac ctxt ths) end;
mengj@19194
   569
mengj@19194
   570
fun atp_method tac = Method.thms_ctxt_args (atp_meth tac);
mengj@19194
   571
mengj@19194
   572
(***************************************************************)
mengj@19194
   573
(* automatic ATP invocation                                    *)
mengj@19194
   574
(***************************************************************)
mengj@19194
   575
paulson@17306
   576
(* call prover with settings and problem file for the current subgoal *)
paulson@17764
   577
fun watcher_call_provers sign sg_terms (childin, childout, pid) =
wenzelm@16802
   578
  let
paulson@17422
   579
    fun make_atp_list [] n = []
paulson@17717
   580
      | make_atp_list (sg_term::xs) n =
wenzelm@16802
   581
          let
paulson@17717
   582
            val probfile = prob_pathname n
paulson@17690
   583
            val time = Int.toString (!time_limit)
wenzelm@16802
   584
          in
wenzelm@22130
   585
            Output.debug (fn () => "problem file in watcher_call_provers is " ^ probfile);
paulson@17764
   586
            (*options are separated by Watcher.setting_sep, currently #"%"*)
paulson@17306
   587
            if !prover = "spass"
wenzelm@16802
   588
            then
paulson@19445
   589
              let val spass = helper_path "SPASS_HOME" "SPASS"
paulson@19445
   590
                  val sopts =
paulson@19445
   591
   "-Auto%-SOS=1%-PGiven=0%-PProblem=0%-Splits=0%-FullRed=0%-DocProof%-TimeLimit=" ^ time
wenzelm@21588
   592
              in
paulson@19445
   593
                  ("spass", spass, sopts, probfile) :: make_atp_list xs (n+1)
wenzelm@16802
   594
              end
paulson@17306
   595
            else if !prover = "vampire"
wenzelm@21588
   596
            then
paulson@17819
   597
              let val vampire = helper_path "VAMPIRE_HOME" "vampire"
paulson@21132
   598
                  val vopts = "--mode casc%-t " ^ time  (*what about -m 100000?*)
wenzelm@16802
   599
              in
paulson@19445
   600
                  ("vampire", vampire, vopts, probfile) :: make_atp_list xs (n+1)
wenzelm@16802
   601
              end
wenzelm@21588
   602
             else if !prover = "E"
wenzelm@21588
   603
             then
wenzelm@21588
   604
               let val Eprover = helper_path "E_HOME" "eproof"
wenzelm@21588
   605
               in
wenzelm@21588
   606
                  ("E", Eprover,
paulson@21900
   607
                     "--tstp-in%--tstp-out%-l5%-xAutoDev%-tAutoDev%--silent%--cpu-limit=" ^ time, probfile) ::
wenzelm@21588
   608
                   make_atp_list xs (n+1)
wenzelm@21588
   609
               end
wenzelm@21588
   610
             else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   611
          end
paulson@15452
   612
paulson@17422
   613
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   614
  in
wenzelm@16802
   615
    Watcher.callResProvers(childout,atp_list);
wenzelm@22130
   616
    Output.debug (fn () => "Sent commands to watcher!")
wenzelm@16802
   617
  end
wenzelm@21588
   618
paulson@22193
   619
(*For debugging the generated set of theorem names*)
paulson@21888
   620
fun trace_vector fname =
paulson@22193
   621
  let val path = File.explode_platform_path (fname ^ "_thm_names")
paulson@21888
   622
  in  Vector.app (File.append path o (fn s => s ^ "\n"))  end;
quigley@16357
   623
paulson@20022
   624
(*We write out problem files for each subgoal. Argument probfile generates filenames,
paulson@18986
   625
  and allows the suppression of the suffix "_1" in problem-generation mode.
paulson@18986
   626
  FIXME: does not cope with &&, and it isn't easy because one could have multiple
paulson@18986
   627
  subgoals, each involving &&.*)
paulson@20022
   628
fun write_problem_files probfile (ctxt,th)  =
paulson@18753
   629
  let val goals = Thm.prems_of th
wenzelm@22130
   630
      val _ = Output.debug (fn () => "number of subgoals = " ^ Int.toString (length goals))
paulson@17717
   631
      val thy = ProofContext.theory_of ctxt
paulson@20457
   632
      fun get_neg_subgoals [] _ = []
wenzelm@22865
   633
        | get_neg_subgoals (gl::gls) n = #1 (ResAxioms.neg_conjecture_clauses th n) ::
paulson@21999
   634
                                         get_neg_subgoals gls (n+1)
paulson@20526
   635
      val goal_cls = get_neg_subgoals goals 1
paulson@20457
   636
      val logic = case !linkup_logic_mode of
paulson@23387
   637
                Auto => problem_logic_goals (map (map prop_of) goal_cls)
wenzelm@21588
   638
              | Fol => FOL
wenzelm@21588
   639
              | Hol => HOL
paulson@20526
   640
      val (included_thms,white_thms) = get_clasimp_atp_lemmas ctxt []
paulson@22382
   641
      val included_cls = included_thms |> ResAxioms.cnf_rules_pairs |> make_unique
paulson@22382
   642
                                       |> restrict_to_logic thy logic
paulson@21470
   643
                                       |> remove_unwanted_clauses
wenzelm@22130
   644
      val _ = Output.debug (fn () => "included clauses = " ^ Int.toString(length included_cls))
paulson@20526
   645
      val white_cls = ResAxioms.cnf_rules_pairs white_thms
paulson@20526
   646
      (*clauses relevant to goal gl*)
mengj@21563
   647
      val axcls_list = map (fn ngcls => get_relevant_clauses thy included_cls white_cls (map prop_of ngcls)) goal_cls
wenzelm@22130
   648
      val _ = app (fn axcls => Output.debug (fn () => "filtered clauses = " ^ Int.toString(length axcls)))
paulson@21070
   649
                  axcls_list
wenzelm@21588
   650
      val writer = if !prover = "spass" then dfg_writer else tptp_writer
paulson@20526
   651
      fun write_all [] [] _ = []
wenzelm@21588
   652
        | write_all (ccls::ccls_list) (axcls::axcls_list) k =
paulson@20868
   653
            let val fname = probfile k
paulson@22217
   654
                val _ = Output.debug (fn () => "About to write file " ^ fname)
paulson@20868
   655
                val axcls = make_unique axcls
paulson@22217
   656
                val _ = Output.debug (fn () => "Conjecture Clauses (before duplicate removal)")
paulson@22217
   657
                val _ = app (fn th => Output.debug (fn _ => string_of_thm th)) ccls
paulson@20868
   658
                val ccls = subtract_cls ccls axcls
paulson@22217
   659
                val _ = Output.debug (fn () => "Conjecture Clauses (AFTER duplicate removal)")
paulson@22217
   660
                val _ = app (fn th => Output.debug (fn _ => string_of_thm th)) ccls
paulson@21431
   661
                val ccltms = map prop_of ccls
paulson@21373
   662
                and axtms = map (prop_of o #1) axcls
paulson@21431
   663
                val subs = tfree_classes_of_terms ccltms
paulson@21431
   664
                and supers = tvar_classes_of_terms axtms
paulson@21431
   665
                and tycons = type_consts_of_terms thy (ccltms@axtms)
paulson@21290
   666
                (*TFrees in conjecture clauses; TVars in axiom clauses*)
paulson@22645
   667
                val (supers',arity_clauses) = ResClause.make_arity_clauses thy tycons supers
paulson@22645
   668
                val _ = Output.debug (fn () => "arity clauses = " ^ Int.toString (length arity_clauses))
paulson@22645
   669
                val classrel_clauses = ResClause.make_classrel_clauses thy subs supers'
wenzelm@22130
   670
                val _ = Output.debug (fn () => "classrel clauses = " ^ Int.toString (length classrel_clauses))
paulson@20868
   671
                val clnames = writer logic ccls fname (axcls,classrel_clauses,arity_clauses) []
paulson@21888
   672
                val thm_names = Vector.fromList clnames
paulson@22193
   673
                val _ = if !Output.debugging then trace_vector fname thm_names else ()
paulson@20870
   674
            in  (thm_names,fname) :: write_all ccls_list axcls_list (k+1)  end
paulson@20870
   675
      val (thm_names_list, filenames) = ListPair.unzip (write_all goal_cls axcls_list 1)
mengj@19194
   676
  in
paulson@20870
   677
      (filenames, thm_names_list)
mengj@19194
   678
  end;
quigley@15644
   679
wenzelm@21588
   680
val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream *
paulson@17775
   681
                                    Posix.Process.pid * string list) option);
paulson@17775
   682
paulson@17775
   683
fun kill_last_watcher () =
wenzelm@21588
   684
    (case !last_watcher_pid of
paulson@17775
   685
         NONE => ()
wenzelm@21588
   686
       | SOME (_, _, pid, files) =>
wenzelm@22130
   687
          (Output.debug (fn () => "Killing old watcher, pid = " ^ string_of_pid pid);
wenzelm@21588
   688
           Watcher.killWatcher pid;
wenzelm@21588
   689
           ignore (map (try cond_rm_tmp) files)))
wenzelm@22130
   690
     handle OS.SysErr _ => Output.debug (fn () => "Attempt to kill watcher failed");
paulson@17525
   691
paulson@21980
   692
(*writes out the current problems and calls ATPs*)
paulson@21980
   693
fun isar_atp (ctxt, th) =
paulson@17717
   694
  if Thm.no_prems th then ()
wenzelm@16802
   695
  else
wenzelm@16802
   696
    let
paulson@17775
   697
      val _ = kill_last_watcher()
paulson@20870
   698
      val (files,thm_names_list) = write_problem_files prob_pathname (ctxt,th)
paulson@22012
   699
      val (childin, childout, pid) = Watcher.createWatcher (ctxt, th, thm_names_list)
paulson@15608
   700
    in
paulson@17772
   701
      last_watcher_pid := SOME (childin, childout, pid, files);
wenzelm@22130
   702
      Output.debug (fn () => "problem files: " ^ space_implode ", " files);
wenzelm@22130
   703
      Output.debug (fn () => "pid: " ^ string_of_pid pid);
wenzelm@22578
   704
      watcher_call_provers (Thm.theory_of_thm th) (Thm.prems_of th) (childin, childout, pid)
paulson@20954
   705
    end;
paulson@20954
   706
paulson@20954
   707
(*For ML scripts, and primarily, for debugging*)
wenzelm@21588
   708
fun callatp () =
paulson@20954
   709
  let val th = topthm()
paulson@20954
   710
      val ctxt = ProofContext.init (theory_of_thm th)
paulson@21980
   711
  in  isar_atp (ctxt, th)  end;
paulson@15608
   712
wenzelm@21588
   713
val isar_atp_writeonly = setmp print_mode []
paulson@17717
   714
      (fn (ctxt,th) =>
paulson@17717
   715
       if Thm.no_prems th then ()
wenzelm@21588
   716
       else
wenzelm@21588
   717
         let val probfile = if Thm.nprems_of th = 1 then probfile_nosuffix
wenzelm@21588
   718
                            else prob_pathname
paulson@20022
   719
         in ignore (write_problem_files probfile (ctxt,th)) end);
paulson@15452
   720
quigley@16357
   721
wenzelm@22865
   722
(** the Isar toplevel command **)
wenzelm@16802
   723
wenzelm@22865
   724
fun sledgehammer state =
wenzelm@22865
   725
  let
wenzelm@22865
   726
    val (ctxt, (_, goal)) = Proof.get_goal (Toplevel.proof_of state);
wenzelm@22865
   727
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   728
  in
wenzelm@22130
   729
    Output.debug (fn () => "subgoals in isar_atp:\n" ^
wenzelm@21588
   730
                  Pretty.string_of (ProofContext.pretty_term ctxt
wenzelm@21588
   731
                    (Logic.mk_conjunction_list (Thm.prems_of goal))));
wenzelm@22130
   732
    Output.debug (fn () => "current theory: " ^ Context.theory_name thy);
paulson@16925
   733
    ResClause.init thy;
mengj@19194
   734
    ResHolClause.init thy;
paulson@21132
   735
    if !time_limit > 0 then isar_atp (ctxt, goal)
wenzelm@22865
   736
    else (warning ("Writing problem file only: " ^ !problem_name);
paulson@22193
   737
          isar_atp_writeonly (ctxt, goal))
paulson@19205
   738
  end;
paulson@19205
   739
wenzelm@22865
   740
val _ = OuterSyntax.add_parsers
wenzelm@22865
   741
  [OuterSyntax.command "sledgehammer" "call automatic theorem provers" OuterKeyword.diag
wenzelm@22865
   742
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_proof o Toplevel.keep sledgehammer))];
paulson@17091
   743
paulson@15347
   744
end;