src/HOL/Tools/res_atp.ML
author paulson
Thu Oct 06 10:14:22 2005 +0200 (2005-10-06)
changeset 17772 818cec5f82a4
parent 17764 fde495b9e24b
child 17773 a7258e1020b7
permissions -rw-r--r--
major simplification: removal of the goalstring argument
     1 (*  Author: Jia Meng, Cambridge University Computer Laboratory
     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 hook_count: int ref
    14   val problem_name: string ref
    15   val time_limit: int ref
    16 end;
    17 
    18 structure ResAtp: RES_ATP =
    19 struct
    20 
    21 val call_atp = ref false;
    22 val hook_count = ref 0;
    23 val time_limit = ref 60;
    24 
    25 val prover = ref "E";   (* use E as the default prover *)
    26 val custom_spass =   (*specialized options for SPASS*)
    27       ref ["-Auto=0","-FullRed=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub"];
    28 
    29 val destdir = ref "";   (*Empty means write files to /tmp*)
    30 val problem_name = ref "prob";
    31 
    32 fun probfile_nosuffix _ = 
    33   if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
    34   else if File.exists (File.unpack_platform_path (!destdir))
    35   then !destdir ^ "/" ^ !problem_name
    36   else error ("No such directory: " ^ !destdir);
    37 
    38 fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n;
    39 
    40 
    41 (**** For running in Isar ****)
    42 
    43 (* same function as that in res_axioms.ML *)
    44 fun repeat_RS thm1 thm2 =
    45     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
    46     in
    47         if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
    48     end;
    49 
    50 (* a special version of repeat_RS *)
    51 fun repeat_someI_ex th = repeat_RS th someI_ex;
    52 
    53 fun writeln_strs _   []      = ()
    54   | writeln_strs out (s::ss) = (TextIO.output (out, s); TextIO.output (out, "\n"); writeln_strs out ss);
    55 
    56 (* write out a subgoal as tptp clauses to the file "xxxx_N"*)
    57 fun tptp_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) =
    58     let
    59       val clss = map (ResClause.make_conjecture_clause_thm) thms
    60       val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
    61       val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
    62       val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses
    63       val arity_cls = map ResClause.tptp_arity_clause arity_clauses
    64       val out = TextIO.openOut(pf n)
    65     in
    66       writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
    67       writeln_strs out (tfree_clss @ tptp_clss @ classrel_cls @ arity_cls);
    68       TextIO.closeOut out
    69     end;
    70 
    71 (* write out a subgoal in DFG format to the file "xxxx_N"*)
    72 fun dfg_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) = 
    73     let val clss = map (ResClause.make_conjecture_clause_thm) thms
    74         (*FIXME: classrel_clauses and arity_clauses*)
    75         val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ Int.toString n)
    76                         axclauses [] [] []    
    77 	val out = TextIO.openOut(pf n)
    78     in
    79 	writeln_strs out [probN]; TextIO.closeOut out
    80     end;
    81 
    82 
    83 (*********************************************************************)
    84 (* call prover with settings and problem file for the current subgoal *)
    85 (*********************************************************************)
    86 (* now passing in list of skolemized thms and list of sgterms to go with them *)
    87 fun watcher_call_provers sign sg_terms (childin, childout, pid) =
    88   let
    89     fun make_atp_list [] n = []
    90       | make_atp_list (sg_term::xs) n =
    91           let
    92             val probfile = prob_pathname n
    93             val time = Int.toString (!time_limit)
    94           in
    95             debug ("problem file in watcher_call_provers is " ^ probfile);
    96             (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
    97               versions of Unix.execute treat them differently!*)
    98             (*options are separated by Watcher.setting_sep, currently #"%"*)
    99             if !prover = "spass"
   100             then
   101               let val optionline = 
   102 		      if !AtpCommunication.reconstruct 
   103 		          (*Proof reconstruction works for only a limited set of 
   104 		            inference rules*)
   105                       then space_implode "%" (!custom_spass) ^
   106                            "%-DocProof%-TimeLimit=" ^ time
   107                       else "-DocProof%-SOS%-FullRed=0%-TimeLimit=" ^ time (*Auto mode*)
   108                   val _ = debug ("SPASS option string is " ^ optionline)
   109                   val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
   110                     (*We've checked that SPASS is there for ATP/spassshell to run.*)
   111               in 
   112                   ([("spass", 
   113                      getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
   114                      optionline, probfile)] @ 
   115                   (make_atp_list xs (n+1)))
   116               end
   117             else if !prover = "vampire"
   118 	    then 
   119               let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vampire"
   120               in
   121                 ([("vampire", vampire, "-m 100000%-t " ^ time, probfile)] @
   122                  (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
   123               end
   124       	     else if !prover = "E"
   125       	     then
   126 	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
   127 	       in
   128 		  ([("E", Eprover, 
   129 		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
   130 		     probfile)] @
   131 		   (make_atp_list xs (n+1)))
   132 	       end
   133 	     else error ("Invalid prover name: " ^ !prover)
   134           end
   135 
   136     val atp_list = make_atp_list sg_terms 1
   137   in
   138     Watcher.callResProvers(childout,atp_list);
   139     debug "Sent commands to watcher!"
   140   end
   141 
   142 (*We write out problem files for each subgoal. Argument pf generates filenames,
   143   and allows the suppression of the suffix "_1" in problem-generation mode.*)
   144 fun write_problem_files pf (ctxt,th)  =
   145   let val prems = Thm.prems_of th
   146       val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
   147               (*FIXME: hack!! need to consider relevance for all prems*)
   148       val _ = debug ("claset and simprules total clauses = " ^ 
   149                      Int.toString (Array.length clause_arr))
   150       val thy = ProofContext.theory_of ctxt
   151       val classrel_clauses = ResTypesSorts.classrel_clauses_thy thy
   152       val _ = debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
   153       val arity_clauses = ResTypesSorts.arity_clause_thy thy
   154       val _ = debug ("arity clauses = " ^ Int.toString (length arity_clauses))
   155       val write = if !prover = "spass" then dfg_inputs_tfrees else tptp_inputs_tfrees
   156       fun writenext n =
   157 	if n=0 then []
   158 	 else
   159 	   (SELECT_GOAL
   160 	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
   161 	      METAHYPS(fn negs => 
   162 		(write (make_clauses negs) pf n 
   163 		       (axclauses,classrel_clauses,arity_clauses);
   164 		 all_tac))]) n th;
   165 	    pf n :: writenext (n-1))
   166       in (writenext (length prems), clause_arr) end;
   167 
   168 val last_watcher_pid =
   169   ref (NONE : (TextIO.instream * TextIO.outstream * 
   170                Posix.Process.pid * string list) option);
   171 
   172 (*writes out the current clasimpset to a tptp file;
   173   turns off xsymbol at start of function, restoring it at end    *)
   174 val isar_atp = setmp print_mode [] 
   175  (fn (ctxt, th) =>
   176   if Thm.no_prems th then ()
   177   else
   178     let
   179       fun reap s = (*Signal handler to tidy away dead processes*)
   180 	   (case Posix.Process.waitpid_nh(Posix.Process.W_ANY_CHILD, []) of
   181 		SOME _ => reap s | NONE => ()) 
   182            handle OS.SysErr _ => ()
   183       val _ = 
   184 	      IsaSignal.signal (IsaSignal.chld, IsaSignal.SIG_HANDLE reap)
   185       val _ = (case !last_watcher_pid of NONE => ()
   186                | SOME (_, childout, pid, files) => 
   187                   (debug ("Killing old watcher, pid = " ^ 
   188                           Int.toString (ResLib.intOfPid pid));
   189                    Watcher.killWatcher pid;
   190                    ignore (map (try OS.FileSys.remove) files)))
   191               handle OS.SysErr _ => debug "Attempt to kill watcher failed";
   192       val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
   193       val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
   194     in
   195       last_watcher_pid := SOME (childin, childout, pid, files);
   196       debug ("problem files: " ^ space_implode ", " files); 
   197       debug ("pid: " ^ Int.toString (ResLib.intOfPid pid));
   198       watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
   199     end);
   200 
   201 val isar_atp_writeonly = setmp print_mode [] 
   202       (fn (ctxt,th) =>
   203        if Thm.no_prems th then ()
   204        else 
   205          let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
   206          	      else prob_pathname
   207          in ignore (write_problem_files pf (ctxt,th)) end);
   208 
   209 
   210 (** the Isar toplevel hook **)
   211 
   212 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   213   let
   214     val proof = Toplevel.proof_of state
   215     val (ctxt, (_, goal)) = Proof.get_goal proof
   216         handle Proof.STATE _ => error "No goal present";
   217     val thy = ProofContext.theory_of ctxt;
   218   in
   219     debug ("subgoals in isar_atp:\n" ^ 
   220            Pretty.string_of (ProofContext.pretty_term ctxt
   221              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   222     debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
   223     debug ("current theory: " ^ Context.theory_name thy);
   224     hook_count := !hook_count +1;
   225     debug ("in hook for time: " ^ Int.toString (!hook_count));
   226     ResClause.init thy;
   227     if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
   228     else isar_atp_writeonly (ctxt, goal)
   229   end);
   230 
   231 val call_atpP =
   232   OuterSyntax.command 
   233     "ProofGeneral.call_atp" 
   234     "call automatic theorem provers" 
   235     OuterKeyword.diag
   236     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   237 
   238 val _ = OuterSyntax.add_parsers [call_atpP];
   239 
   240 end;