src/HOL/Tools/res_atp.ML
author mengj
Fri Oct 28 02:29:01 2005 +0200 (2005-10-28 ago)
changeset 18003 2aecb2d68c00
parent 17888 116a8d1c7a67
child 18270 27227433cb42
permissions -rw-r--r--
Added "writeln_strs" to the signature
     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 (* same function as that in res_axioms.ML *)
    56 fun repeat_RS thm1 thm2 =
    57     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
    58     in
    59         if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
    60     end;
    61 
    62 (* a special version of repeat_RS *)
    63 fun repeat_someI_ex th = repeat_RS th someI_ex;
    64 
    65 fun writeln_strs _   []      = ()
    66   | writeln_strs out (s::ss) = (TextIO.output (out, s); TextIO.output (out, "\n"); writeln_strs out ss);
    67 
    68 (* write out a subgoal as tptp clauses to the file "xxxx_N"*)
    69 fun tptp_inputs_tfrees ths pf n (axclauses,classrel_clauses,arity_clauses) =
    70   let
    71     val clss = ResClause.make_conjecture_clauses (map prop_of ths)
    72     val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
    73     val tfree_clss = map ResClause.tfree_clause (foldl (op union_string) [] tfree_litss)
    74     val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses
    75     val arity_cls = map ResClause.tptp_arity_clause arity_clauses
    76     val out = TextIO.openOut(pf n)
    77   in
    78     writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
    79     writeln_strs out (tfree_clss @ tptp_clss @ classrel_cls @ arity_cls);
    80     TextIO.closeOut out
    81   end;
    82 
    83 (* write out a subgoal in DFG format to the file "xxxx_N"*)
    84 fun dfg_inputs_tfrees ths pf n (axclauses,classrel_clauses,arity_clauses) = 
    85     let val clss = ResClause.make_conjecture_clauses (map prop_of ths)
    86         (*FIXME: classrel_clauses and arity_clauses*)
    87         val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ Int.toString n)
    88                         axclauses [] [] []    
    89 	val out = TextIO.openOut(pf n)
    90     in
    91 	writeln_strs out [probN]; TextIO.closeOut out
    92     end;
    93 
    94 
    95 (* call prover with settings and problem file for the current subgoal *)
    96 fun watcher_call_provers sign sg_terms (childin, childout, pid) =
    97   let
    98     fun make_atp_list [] n = []
    99       | make_atp_list (sg_term::xs) n =
   100           let
   101             val probfile = prob_pathname n
   102             val time = Int.toString (!time_limit)
   103           in
   104             debug ("problem file in watcher_call_provers is " ^ probfile);
   105             (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
   106               versions of Unix.execute treat them differently!*)
   107             (*options are separated by Watcher.setting_sep, currently #"%"*)
   108             if !prover = "spass"
   109             then
   110               let val optionline = 
   111 		      if !AtpCommunication.reconstruct 
   112 		          (*Proof reconstruction works for only a limited set of 
   113 		            inference rules*)
   114                       then space_implode "%" (!custom_spass) ^
   115                            "%-DocProof%-TimeLimit=" ^ time
   116                       else "-DocProof%-SOS%-FullRed=0%-TimeLimit=" ^ time (*Auto mode*)
   117                   val _ = debug ("SPASS option string is " ^ optionline)
   118                   val _ = helper_path "SPASS_HOME" "SPASS"
   119                     (*We've checked that SPASS is there for ATP/spassshell to run.*)
   120               in 
   121                   ([("spass", 
   122                      getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
   123                      optionline, probfile)] @ 
   124                   (make_atp_list xs (n+1)))
   125               end
   126             else if !prover = "vampire"
   127 	    then 
   128               let val vampire = helper_path "VAMPIRE_HOME" "vampire"
   129               in
   130                 ([("vampire", vampire, "-m 100000%-t " ^ time, probfile)] @
   131                  (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
   132               end
   133       	     else if !prover = "E"
   134       	     then
   135 	       let val Eprover = helper_path "E_HOME" "eproof"
   136 	       in
   137 		  ([("E", Eprover, 
   138 		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
   139 		     probfile)] @
   140 		   (make_atp_list xs (n+1)))
   141 	       end
   142 	     else error ("Invalid prover name: " ^ !prover)
   143           end
   144 
   145     val atp_list = make_atp_list sg_terms 1
   146   in
   147     Watcher.callResProvers(childout,atp_list);
   148     debug "Sent commands to watcher!"
   149   end
   150 
   151 (*We write out problem files for each subgoal. Argument pf generates filenames,
   152   and allows the suppression of the suffix "_1" in problem-generation mode.*)
   153 fun write_problem_files pf (ctxt,th)  =
   154   let val prems = Thm.prems_of th
   155       val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
   156               (*FIXME: hack!! need to consider relevance for all prems*)
   157       val _ = debug ("claset and simprules total clauses = " ^ 
   158                      Int.toString (Array.length clause_arr))
   159       val thy = ProofContext.theory_of ctxt
   160       val classrel_clauses = ResClause.classrel_clauses_thy thy
   161       val _ = debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
   162       val arity_clauses = ResClause.arity_clause_thy thy
   163       val _ = debug ("arity clauses = " ^ Int.toString (length arity_clauses))
   164       val write = if !prover = "spass" then dfg_inputs_tfrees else tptp_inputs_tfrees
   165       fun writenext n =
   166 	if n=0 then []
   167 	 else
   168 	   (SELECT_GOAL
   169 	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
   170 	      METAHYPS(fn negs => 
   171 		(write (make_clauses negs) pf n 
   172 		       (axclauses,classrel_clauses,arity_clauses);
   173 		 all_tac))]) n th;
   174 	    pf n :: writenext (n-1))
   175       in (writenext (length prems), clause_arr) end;
   176 
   177 val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
   178                                     Posix.Process.pid * string list) option);
   179 
   180 fun kill_last_watcher () =
   181     (case !last_watcher_pid of 
   182          NONE => ()
   183        | SOME (_, childout, pid, files) => 
   184 	  (debug ("Killing old watcher, pid = " ^ string_of_pid pid);
   185 	   Watcher.killWatcher pid;  
   186 	   ignore (map (try OS.FileSys.remove) files)))
   187      handle OS.SysErr _ => debug "Attempt to kill watcher failed";
   188 
   189 (*writes out the current clasimpset to a tptp file;
   190   turns off xsymbol at start of function, restoring it at end    *)
   191 val isar_atp = setmp print_mode [] 
   192  (fn (ctxt, th) =>
   193   if Thm.no_prems th then ()
   194   else
   195     let
   196       val _ = kill_last_watcher()
   197       val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
   198       val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
   199     in
   200       last_watcher_pid := SOME (childin, childout, pid, files);
   201       debug ("problem files: " ^ space_implode ", " files); 
   202       debug ("pid: " ^ string_of_pid pid);
   203       watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
   204     end);
   205 
   206 val isar_atp_writeonly = setmp print_mode [] 
   207       (fn (ctxt,th) =>
   208        if Thm.no_prems th then ()
   209        else 
   210          let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
   211          	      else prob_pathname
   212          in ignore (write_problem_files pf (ctxt,th)) end);
   213 
   214 
   215 (** the Isar toplevel hook **)
   216 
   217 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   218   let
   219     val proof = Toplevel.proof_of state
   220     val (ctxt, (_, goal)) = Proof.get_goal proof
   221         handle Proof.STATE _ => error "No goal present";
   222     val thy = ProofContext.theory_of ctxt;
   223   in
   224     debug ("subgoals in isar_atp:\n" ^ 
   225            Pretty.string_of (ProofContext.pretty_term ctxt
   226              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   227     debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
   228     debug ("current theory: " ^ Context.theory_name thy);
   229     hook_count := !hook_count +1;
   230     debug ("in hook for time: " ^ Int.toString (!hook_count));
   231     ResClause.init thy;
   232     if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
   233     else isar_atp_writeonly (ctxt, goal)
   234   end);
   235 
   236 val call_atpP =
   237   OuterSyntax.command 
   238     "ProofGeneral.call_atp" 
   239     "call automatic theorem provers" 
   240     OuterKeyword.diag
   241     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   242 
   243 val _ = OuterSyntax.add_parsers [call_atpP];
   244 
   245 end;