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