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