src/HOL/Tools/res_atp.ML
author mengj
Thu Apr 20 04:20:06 2006 +0200 (2006-04-20 ago)
changeset 19451 c7a25c05986c
parent 19450 651d949776f8
child 19490 bf7f8347174a
permissions -rw-r--r--
Changed the logic detection method.
     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 custom_spass: string list ref
    12   val destdir: string ref
    13   val helper_path: string -> string -> string
    14   val problem_name: string ref
    15   val time_limit: int ref
    16    
    17   datatype mode = Auto | Fol | Hol
    18   val linkup_logic_mode : mode ref
    19   val write_subgoal_file: mode -> Proof.context -> thm list -> thm list -> int -> string
    20   val vampire_time: int ref
    21   val eprover_time: int ref
    22   val run_vampire: int -> unit
    23   val run_eprover: int -> unit
    24   val vampireLimit: unit -> int
    25   val eproverLimit: unit -> int
    26   val atp_method: (ProofContext.context -> thm list -> int -> Tactical.tactic) ->
    27 		  Method.src -> ProofContext.context -> Method.method
    28   val cond_rm_tmp: string -> unit
    29   val keep_atp_input: bool ref
    30   val fol_keep_types: bool ref
    31   val hol_full_types: unit -> unit
    32   val hol_partial_types: unit -> unit
    33   val hol_const_types_only: unit -> unit
    34   val hol_no_types: unit -> unit
    35   val hol_typ_level: unit -> ResHolClause.type_level
    36   val run_relevance_filter: bool ref
    37   val invoke_atp_ml : ProofContext.context * thm -> unit
    38   val add_claset : unit -> unit
    39   val add_simpset : unit -> unit
    40   val add_clasimp : unit -> unit
    41   val add_atpset : unit -> unit
    42   val rm_claset : unit -> unit
    43   val rm_simpset : unit -> unit
    44   val rm_atpset : unit -> unit
    45   val rm_clasimp : unit -> unit
    46 end;
    47 
    48 structure ResAtp : RES_ATP =
    49 struct
    50 
    51 (********************************************************************)
    52 (* some settings for both background automatic ATP calling procedure*)
    53 (* and also explicit ATP invocation methods                         *)
    54 (********************************************************************)
    55 
    56 (*** background linkup ***)
    57 val call_atp = ref false; 
    58 val hook_count = ref 0;
    59 val time_limit = ref 30;
    60 val prover = ref "E";   (* use E as the default prover *)
    61 val custom_spass =   (*specialized options for SPASS*)
    62       ref ["-Auto=0","-FullRed=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub"];
    63 val destdir = ref "";   (*Empty means write files to /tmp*)
    64 val problem_name = ref "prob";
    65 
    66 (*Return the path to a "helper" like SPASS or tptp2X, first checking that
    67   it exists.  FIXME: modify to use Path primitives and move to some central place.*)  
    68 fun helper_path evar base =
    69   case getenv evar of
    70       "" => error  ("Isabelle environment variable " ^ evar ^ " not defined")
    71     | home => 
    72         let val path = home ^ "/" ^ base
    73         in  if File.exists (File.unpack_platform_path path) then path 
    74 	    else error ("Could not find the file " ^ path)
    75 	end;  
    76 
    77 fun probfile_nosuffix _ = 
    78   if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
    79   else if File.exists (File.unpack_platform_path (!destdir))
    80   then !destdir ^ "/" ^ !problem_name
    81   else error ("No such directory: " ^ !destdir);
    82 
    83 fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n;
    84 
    85 
    86 (*** ATP methods ***)
    87 val vampire_time = ref 60;
    88 val eprover_time = ref 60;
    89 fun run_vampire time =  
    90     if (time >0) then vampire_time:= time
    91     else vampire_time:=60;
    92 
    93 fun run_eprover time = 
    94     if (time > 0) then eprover_time:= time
    95     else eprover_time:=60;
    96 
    97 fun vampireLimit () = !vampire_time;
    98 fun eproverLimit () = !eprover_time;
    99 
   100 val keep_atp_input = ref false;
   101 val fol_keep_types = ResClause.keep_types;
   102 val hol_full_types = ResHolClause.full_types;
   103 val hol_partial_types = ResHolClause.partial_types;
   104 val hol_const_types_only = ResHolClause.const_types_only;
   105 val hol_no_types = ResHolClause.no_types;
   106 fun hol_typ_level () = ResHolClause.find_typ_level ();
   107 fun is_typed_hol () = 
   108     let val tp_level = hol_typ_level()
   109     in
   110 	not (tp_level = ResHolClause.T_NONE)
   111     end;
   112 val include_combS = ResHolClause.include_combS;
   113 val include_min_comb = ResHolClause.include_min_comb;
   114 
   115 fun atp_input_file () =
   116     let val file = !problem_name 
   117     in
   118 	if !destdir = "" then File.platform_path (File.tmp_path (Path.basic file))
   119 	else if File.exists (File.unpack_platform_path (!destdir))
   120 	then !destdir ^ "/" ^ file
   121 	else error ("No such directory: " ^ !destdir)
   122     end;
   123 
   124 val include_simpset = ref false;
   125 val include_claset = ref false; 
   126 val include_atpset = ref true;
   127 val add_simpset = (fn () => include_simpset:=true);
   128 val add_claset = (fn () => include_claset:=true);
   129 val add_clasimp = (fn () => (include_simpset:=true;include_claset:=true));
   130 val add_atpset = (fn () => include_atpset:=true);
   131 val rm_simpset = (fn () => include_simpset:=false);
   132 val rm_claset = (fn () => include_claset:=false);
   133 val rm_clasimp = (fn () => (include_simpset:=false;include_claset:=false));
   134 val rm_atpset = (fn () => include_atpset:=false);
   135 
   136 (*** paths for HOL helper files ***)
   137 fun full_typed_comb_inclS () =
   138     helper_path "E_HOME" "additionals/full_comb_inclS"
   139     handle _ => helper_path "VAMPIRE_HOME" "additionals/full_comb_inclS";
   140 
   141 fun full_typed_comb_noS () =
   142     helper_path "E_HOME" "additionals/full_comb_noS"
   143     handle _ => helper_path "VAMPIRE_HOME" "additionals/full_comb_noS";
   144 									      
   145 fun partial_typed_comb_inclS () =
   146     helper_path "E_HOME" "additionals/par_comb_inclS"
   147     handle _ => helper_path "VAMPIRE_HOME" "additionals/par_comb_inclS";
   148 
   149 fun partial_typed_comb_noS () =
   150     helper_path "E_HOME" "additionals/par_comb_noS"
   151     handle _ => helper_path "VAMPIRE_HOME" "additionals/par_comb_noS";
   152 
   153 fun const_typed_comb_inclS () =
   154     helper_path "E_HOME" "additionals/const_comb_inclS"
   155     handle _ => helper_path "VAMPIRE_HOME" "additionals/const_comb_inclS";
   156 
   157 fun const_typed_comb_noS () =
   158     helper_path "E_HOME" "additionals/const_comb_noS"
   159     handle _ => helper_path "VAMPIRE_HOME" "additionals/const_comb_noS";
   160 
   161 fun untyped_comb_inclS () =
   162     helper_path "E_HOME" "additionals/u_comb_inclS"
   163     handle _ => helper_path "VAMPIRE_HOME" "additionals/u_comb_inclS";
   164 
   165 fun untyped_comb_noS () =
   166     helper_path "E_HOME" "additionals/u_comb_noS"
   167     handle _ => helper_path "VAMPIRE_HOME" "additionals/u_comb_noS";
   168 
   169 fun full_typed_HOL_helper1 () =
   170     helper_path "E_HOME" "additionals/full_helper1"
   171     handle _ => helper_path "VAMPIRE_HOME" "additionals/full_helper1";
   172 
   173 fun partial_typed_HOL_helper1 () = 
   174     helper_path "E_HOME" "additionals/par_helper1"
   175     handle _ => helper_path "VAMPIRE_HOME" "additionals/par_helper1";
   176 
   177 fun const_typed_HOL_helper1 () = 
   178     helper_path "E_HOME" "additionals/const_helper1"
   179     handle _ => helper_path "VAMPIRE_HOME" "additionals/const_helper1";
   180 
   181 fun untyped_HOL_helper1 () = 
   182     helper_path "E_HOME" "additionals/u_helper1"
   183     handle _ => helper_path "VAMPIRE_HOME" "additionals/u_helper1";
   184 
   185 fun get_full_typed_helpers () =
   186     (full_typed_HOL_helper1 (), full_typed_comb_noS (), full_typed_comb_inclS ());
   187 
   188 fun get_partial_typed_helpers () =
   189     (partial_typed_HOL_helper1 (), partial_typed_comb_noS (), partial_typed_comb_inclS ());
   190 
   191 fun get_const_typed_helpers () =
   192     (const_typed_HOL_helper1 (), const_typed_comb_noS (), const_typed_comb_inclS ());
   193 
   194 fun get_untyped_helpers () =
   195     (untyped_HOL_helper1 (), untyped_comb_noS (), untyped_comb_inclS ());
   196 
   197 fun get_all_helpers () =
   198     (get_full_typed_helpers (), get_partial_typed_helpers (), get_const_typed_helpers (), get_untyped_helpers ());
   199 
   200 
   201 (**** relevance filter ****)
   202 val run_relevance_filter = ref true;
   203 
   204 (******************************************************************)
   205 (* detect whether a given problem (clauses) is FOL/HOL/HOLC/HOLCS *)
   206 (******************************************************************)
   207 
   208 datatype logic = FOL | HOL | HOLC | HOLCS;
   209 
   210 fun string_of_logic FOL = "FOL"
   211   | string_of_logic HOL = "HOL"
   212   | string_of_logic HOLC = "HOLC"
   213   | string_of_logic HOLCS = "HOLCS";
   214 
   215 
   216 fun is_fol_logic FOL = true
   217   | is_fol_logic  _ = false
   218 
   219 
   220 (*HOLCS will not occur here*)
   221 fun upgrade_lg HOLC _ = HOLC
   222   | upgrade_lg HOL HOLC = HOLC
   223   | upgrade_lg HOL _ = HOL
   224   | upgrade_lg FOL lg = lg; 
   225 
   226 (* check types *)
   227 fun has_bool_hfn (Type("bool",_)) = true
   228   | has_bool_hfn (Type("fun",_)) = true
   229   | has_bool_hfn (Type(_, Ts)) = exists has_bool_hfn Ts
   230   | has_bool_hfn _ = false;
   231 
   232 fun is_hol_fn tp =
   233     let val (targs,tr) = strip_type tp
   234     in
   235 	exists (has_bool_hfn) (tr::targs)
   236     end;
   237 
   238 fun is_hol_pred tp =
   239     let val (targs,tr) = strip_type tp
   240     in
   241 	exists (has_bool_hfn) targs
   242     end;
   243 
   244 exception FN_LG of term;
   245 
   246 fun fn_lg (t as Const(f,tp)) (lg,seen) = 
   247     if is_hol_fn tp then (upgrade_lg HOL lg, t ins seen) else (lg, t ins seen) 
   248   | fn_lg (t as Free(f,tp)) (lg,seen) = 
   249     if is_hol_fn tp then (upgrade_lg HOL lg, t ins seen) else (lg, t ins seen) 
   250   | fn_lg (t as Var(f,tp)) (lg,seen) =
   251     if is_hol_fn tp then (upgrade_lg HOL lg,t ins seen) else (lg,t ins seen)
   252   | fn_lg (t as Abs(_,_,_)) (lg,seen) = (upgrade_lg HOLC lg,t ins seen)
   253   | fn_lg f _ = raise FN_LG(f); 
   254 
   255 
   256 fun term_lg [] (lg,seen) = (lg,seen)
   257   | term_lg (tm::tms) (FOL,seen) =
   258     let val (f,args) = strip_comb tm
   259 	val (lg',seen') = if f mem seen then (FOL,seen) 
   260 			  else fn_lg f (FOL,seen)
   261 	val _ = if is_fol_logic lg' then () else warning ("Found a HOL term: " ^ (Term.str_of_term f))
   262 	 in
   263 	     term_lg (args@tms) (lg',seen')
   264     end
   265   | term_lg _ (lg,seen) = (lg,seen)
   266 
   267 exception PRED_LG of term;
   268 
   269 fun pred_lg (t as Const(P,tp)) (lg,seen)= 
   270     if is_hol_pred tp then (upgrade_lg HOL lg, t ins seen) else (lg,t ins seen) 
   271   | pred_lg (t as Free(P,tp)) (lg,seen) =
   272     if is_hol_pred tp then (upgrade_lg HOL lg, t ins seen) else (lg,t ins seen)
   273   | pred_lg (t as Var(_,_)) (lg,seen) = (upgrade_lg HOL lg, t ins seen)
   274   | pred_lg P _ = raise PRED_LG(P);
   275 
   276 
   277 fun lit_lg (Const("Not",_) $ P) (lg,seen) = lit_lg P (lg,seen)
   278   | lit_lg P (lg,seen) =
   279     let val (pred,args) = strip_comb P
   280 	val (lg',seen') = if pred mem seen then (lg,seen) 
   281 			  else pred_lg pred (lg,seen)
   282 	val _ = if is_fol_logic lg' then () else warning ("Found a HOL predicate: " ^ (Term.str_of_term pred))
   283     in
   284 	term_lg args (lg',seen')
   285     end;
   286 
   287 fun lits_lg [] (lg,seen) = (lg,seen)
   288   | lits_lg (lit::lits) (FOL,seen) =
   289     let val (lg,seen') = lit_lg lit (FOL,seen)
   290 	val _ =  if is_fol_logic lg then () else warning ("Found a HOL literal: " ^ (Term.str_of_term lit))
   291     in
   292 	lits_lg lits (lg,seen')
   293     end
   294   | lits_lg lits (lg,seen) = (lg,seen);
   295 
   296 
   297 fun dest_disj_aux (Const ("op |", _) $ t $ t') disjs = 
   298     dest_disj_aux t (dest_disj_aux t' disjs)
   299   | dest_disj_aux t disjs = t::disjs;
   300 
   301 fun dest_disj t = dest_disj_aux t [];
   302 
   303 fun logic_of_clause tm (lg,seen) =
   304     let val tm' = HOLogic.dest_Trueprop tm
   305 	val disjs = dest_disj tm'
   306     in
   307 	lits_lg disjs (lg,seen)
   308     end;
   309 
   310 fun logic_of_clauses [] (lg,seen) = (lg,seen)
   311   | logic_of_clauses (cls::clss) (FOL,seen) =
   312     let val (lg,seen') = logic_of_clause cls (FOL,seen)
   313 	val _ = if is_fol_logic lg then () else warning ("Found a HOL clause: " ^ (Term.str_of_term cls))
   314     in
   315 	logic_of_clauses clss (lg,seen')
   316     end
   317   | logic_of_clauses (cls::clss) (lg,seen) = (lg,seen);
   318 
   319 fun problem_logic_goals_aux [] (lg,seen) = lg
   320   | problem_logic_goals_aux (subgoal::subgoals) (lg,seen) = 
   321     problem_logic_goals_aux subgoals (logic_of_clauses subgoal (lg,seen));
   322     
   323 fun problem_logic_goals subgoals = problem_logic_goals_aux subgoals (FOL,[]);
   324 
   325 
   326 (***************************************************************)
   327 (* ATP invocation methods setup                                *)
   328 (***************************************************************)
   329 
   330 
   331 (**** prover-specific format: TPTP ****)
   332 
   333 
   334 fun cnf_hyps_thms ctxt = 
   335     let val ths = ProofContext.prems_of ctxt
   336     in
   337 	ResClause.union_all (map ResAxioms.skolem_thm ths)
   338     end;
   339 
   340 
   341 (**** write to files ****)
   342 
   343 datatype mode = Auto | Fol | Hol;
   344 
   345 val linkup_logic_mode = ref Auto;
   346 
   347 fun tptp_writer logic goals filename (axioms,classrels,arities) =
   348     if is_fol_logic logic 
   349     then ResClause.tptp_write_file goals filename (axioms, classrels, arities)
   350     else ResHolClause.tptp_write_file goals filename (axioms, classrels, arities) 
   351 	   (get_all_helpers());
   352 
   353 (*2006-04-07: not working: goals has type thm list (not term list as above) and
   354   axioms has type ResClause.clause list (not (thm * (string * int)) list as above)*)
   355 fun dfg_writer logic goals filename (axioms,classrels,arities) =
   356     if is_fol_logic logic 
   357     then ResClause.dfg_write_file goals filename (axioms, classrels, arities)
   358     else error "DFG output for higher-order translations is not implemented"
   359 
   360 
   361 fun write_subgoal_file mode ctxt conjectures user_thms n =
   362     let val conj_cls = make_clauses conjectures 
   363 	val hyp_cls = cnf_hyps_thms ctxt
   364 	val goal_cls = conj_cls@hyp_cls
   365 	val user_rules = map ResAxioms.pairname user_thms
   366 	val (names_arr,axclauses_as_thms) = ResClasimp.get_clasimp_atp_lemmas ctxt (map prop_of goal_cls) user_rules (!include_claset,!include_simpset,!include_atpset) (!run_relevance_filter)  
   367 	val thy = ProofContext.theory_of ctxt
   368 	val prob_logic = case mode of Auto => problem_logic_goals [map prop_of goal_cls]
   369 				    | Fol => FOL
   370 				    | Hol => HOL
   371 	val keep_types = if is_fol_logic prob_logic then !fol_keep_types else is_typed_hol ()
   372 	val classrel_clauses = if keep_types then ResClause.classrel_clauses_thy thy else []
   373 	val arity_clauses = if keep_types then ResClause.arity_clause_thy thy else []
   374         val writer = (*if !prover = "spass" then dfg_writer else*) tptp_writer 
   375 	val file = atp_input_file()
   376     in
   377 	(writer prob_logic goal_cls file (axclauses_as_thms,classrel_clauses,arity_clauses);
   378 	 warning ("Writing to " ^ file);
   379 	 file)
   380     end;
   381 
   382 
   383 (**** remove tmp files ****)
   384 fun cond_rm_tmp file = 
   385     if !keep_atp_input then warning "ATP input kept..." 
   386     else if !destdir <> "" then warning ("ATP input kept in directory " ^ (!destdir))
   387     else (warning "deleting ATP inputs..."; OS.FileSys.remove file);
   388 
   389 
   390 (****** setup ATPs as Isabelle methods ******)
   391 fun atp_meth' tac ths ctxt = 
   392     Method.SIMPLE_METHOD' HEADGOAL
   393     (tac ctxt ths);
   394 
   395 fun atp_meth tac ths ctxt = 
   396     let val thy = ProofContext.theory_of ctxt
   397 	val _ = ResClause.init thy
   398 	val _ = ResHolClause.init thy
   399     in
   400 	atp_meth' tac ths ctxt
   401     end;
   402 
   403 fun atp_method tac = Method.thms_ctxt_args (atp_meth tac);
   404 
   405 (***************************************************************)
   406 (* automatic ATP invocation                                    *)
   407 (***************************************************************)
   408 
   409 (* call prover with settings and problem file for the current subgoal *)
   410 fun watcher_call_provers sign sg_terms (childin, childout, pid) =
   411   let
   412     fun make_atp_list [] n = []
   413       | make_atp_list (sg_term::xs) n =
   414           let
   415             val probfile = prob_pathname n
   416             val time = Int.toString (!time_limit)
   417           in
   418             Output.debug ("problem file in watcher_call_provers is " ^ probfile);
   419             (*options are separated by Watcher.setting_sep, currently #"%"*)
   420             if !prover = "spass"
   421             then
   422               let val spass = helper_path "SPASS_HOME" "SPASS"
   423                   val sopts =
   424    "-Auto%-SOS=1%-PGiven=0%-PProblem=0%-Splits=0%-FullRed=0%-DocProof%-TimeLimit=" ^ time
   425               in 
   426                   ("spass", spass, sopts, probfile) :: make_atp_list xs (n+1)
   427               end
   428             else if !prover = "vampire"
   429 	    then 
   430               let val vampire = helper_path "VAMPIRE_HOME" "vampire"
   431                   val casc = if !time_limit > 70 then "--mode casc%" else ""
   432                   val vopts = casc ^ "-m 100000%-t " ^ time
   433               in
   434                   ("vampire", vampire, vopts, probfile) :: make_atp_list xs (n+1)
   435               end
   436       	     else if !prover = "E"
   437       	     then
   438 	       let val Eprover = helper_path "E_HOME" "eproof"
   439 	       in
   440 		  ("E", Eprover, 
   441 		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time, probfile) ::
   442 		   make_atp_list xs (n+1)
   443 	       end
   444 	     else error ("Invalid prover name: " ^ !prover)
   445           end
   446 
   447     val atp_list = make_atp_list sg_terms 1
   448   in
   449     Watcher.callResProvers(childout,atp_list);
   450     Output.debug "Sent commands to watcher!"
   451   end
   452 
   453 (*We write out problem files for each subgoal. Argument pf generates filenames,
   454   and allows the suppression of the suffix "_1" in problem-generation mode.
   455   FIXME: does not cope with &&, and it isn't easy because one could have multiple
   456   subgoals, each involving &&.*)
   457 fun write_problem_files pf (ctxt,th)  =
   458   let val goals = Thm.prems_of th
   459       val _ = Output.debug ("number of subgoals = " ^ Int.toString (length goals))
   460       val (names_arr, axclauses) = ResClasimp.get_clasimp_atp_lemmas ctxt goals [] (true,true,true) (!run_relevance_filter) (* no user supplied rules here, because no user invocation *)
   461       val _ = Output.debug ("claset, simprules and atprules total clauses = " ^ 
   462                      Int.toString (Array.length names_arr))
   463       val thy = ProofContext.theory_of ctxt
   464       fun get_neg_subgoals n =
   465 	  if n=0 then []
   466 	  else
   467 	      let val st = Seq.hd (EVERY'
   468 				       [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac] n th)
   469 		  val negs = Option.valOf (metahyps_thms n st)
   470 		  val negs_clauses = make_clauses negs
   471 	      in
   472 		  negs_clauses::(get_neg_subgoals (n - 1))
   473 	      end
   474       val neg_subgoals = get_neg_subgoals (length goals) 
   475       val goals_logic = case !linkup_logic_mode of Auto => problem_logic_goals (map (map prop_of) neg_subgoals)
   476 						 | Fol => FOL
   477 						 | Hol => HOL
   478       val keep_types = if is_fol_logic goals_logic then !ResClause.keep_types else is_typed_hol ()
   479       val classrel_clauses = if keep_types then ResClause.classrel_clauses_thy thy else []
   480       val _ = Output.debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
   481       val arity_clauses = if keep_types then ResClause.arity_clause_thy thy else []
   482       val _ = Output.debug ("arity clauses = " ^ Int.toString (length arity_clauses))
   483       val writer = (*if !prover = "spass" then dfg_writer else*) tptp_writer 
   484       fun write_all [] _ = []
   485 	| write_all (subgoal::subgoals) k =
   486 	  (writer goals_logic subgoal (pf k) (axclauses,classrel_clauses,arity_clauses); pf k):: (write_all subgoals (k - 1))
   487   in
   488       (write_all neg_subgoals (length goals), names_arr)
   489   end;
   490 
   491 val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
   492                                     Posix.Process.pid * string list) option);
   493 
   494 fun kill_last_watcher () =
   495     (case !last_watcher_pid of 
   496          NONE => ()
   497        | SOME (_, _, pid, files) => 
   498 	  (Output.debug ("Killing old watcher, pid = " ^ string_of_pid pid);
   499 	   Watcher.killWatcher pid;  
   500 	   ignore (map (try OS.FileSys.remove) files)))
   501      handle OS.SysErr _ => Output.debug "Attempt to kill watcher failed";
   502 
   503 (*writes out the current clasimpset to a tptp file;
   504   turns off xsymbol at start of function, restoring it at end    *)
   505 val isar_atp = setmp print_mode [] 
   506  (fn (ctxt, th) =>
   507   if Thm.no_prems th then ()
   508   else
   509     let
   510       val _ = kill_last_watcher()
   511       val (files,names_arr) = write_problem_files prob_pathname (ctxt,th)
   512       val (childin, childout, pid) = Watcher.createWatcher (th, names_arr)
   513     in
   514       last_watcher_pid := SOME (childin, childout, pid, files);
   515       Output.debug ("problem files: " ^ space_implode ", " files); 
   516       Output.debug ("pid: " ^ string_of_pid pid);
   517       watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
   518     end);
   519 
   520 val isar_atp_writeonly = setmp print_mode [] 
   521       (fn (ctxt,th) =>
   522        if Thm.no_prems th then ()
   523        else 
   524          let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
   525          	      else prob_pathname
   526          in ignore (write_problem_files pf (ctxt,th)) end);
   527 
   528 
   529 (** the Isar toplevel hook **)
   530 
   531 fun invoke_atp_ml (ctxt, goal) =
   532   let val thy = ProofContext.theory_of ctxt;
   533   in
   534     Output.debug ("subgoals in isar_atp:\n" ^ 
   535 		  Pretty.string_of (ProofContext.pretty_term ctxt
   536 		    (Logic.mk_conjunction_list (Thm.prems_of goal))));
   537     Output.debug ("current theory: " ^ Context.theory_name thy);
   538     hook_count := !hook_count +1;
   539     Output.debug ("in hook for time: " ^ Int.toString (!hook_count));
   540     ResClause.init thy;
   541     ResHolClause.init thy;
   542     if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
   543     else isar_atp_writeonly (ctxt, goal)
   544   end;
   545 
   546 val invoke_atp = Toplevel.no_timing o Toplevel.unknown_proof o Toplevel.keep
   547  (fn state =>
   548   let val (ctxt, (_, goal)) = Proof.get_goal (Toplevel.proof_of state)
   549   in  invoke_atp_ml (ctxt, goal)  end);
   550 
   551 val call_atpP =
   552   OuterSyntax.command 
   553     "ProofGeneral.call_atp" 
   554     "call automatic theorem provers" 
   555     OuterKeyword.diag
   556     (Scan.succeed invoke_atp);
   557 
   558 val _ = OuterSyntax.add_parsers [call_atpP];
   559 
   560 end;