src/HOL/Tools/res_atp.ML
author paulson
Tue Oct 04 09:59:01 2005 +0200 (2005-10-04)
changeset 17746 af59c748371d
parent 17717 7c6a96cbc966
child 17764 fde495b9e24b
permissions -rw-r--r--
fixed the ascii-armouring of goalstring
     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 
    54 (* write out a subgoal as tptp clauses to the file "xxxx_N"*)
    55 fun tptp_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) =
    56     let
    57       val clss = map (ResClause.make_conjecture_clause_thm) thms
    58       val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
    59       val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
    60       val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses
    61       val arity_cls = map ResClause.tptp_arity_clause arity_clauses
    62       val out = TextIO.openOut(pf n)
    63     in
    64       ResLib.writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
    65       ResLib.writeln_strs out (tfree_clss @ tptp_clss @ classrel_cls @ arity_cls);
    66       TextIO.closeOut out
    67     end;
    68 
    69 (* write out a subgoal in DFG format to the file "xxxx_N"*)
    70 fun dfg_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) = 
    71     let val clss = map (ResClause.make_conjecture_clause_thm) thms
    72         (*FIXME: classrel_clauses and arity_clauses*)
    73         val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ Int.toString n)
    74                         axclauses [] [] []    
    75 	val out = TextIO.openOut(pf n)
    76     in
    77 	ResLib.writeln_strs out [probN]; TextIO.closeOut out
    78     end;
    79 
    80 
    81 (*********************************************************************)
    82 (* call prover with settings and problem file for the current subgoal *)
    83 (*********************************************************************)
    84 (* now passing in list of skolemized thms and list of sgterms to go with them *)
    85 fun watcher_call_provers sign sg_terms (childin, childout,pid) =
    86   let
    87     fun make_atp_list [] n = []
    88       | make_atp_list (sg_term::xs) n =
    89           let
    90             val goalstring = Sign.string_of_term sign sg_term
    91             val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
    92             val probfile = prob_pathname n
    93             val time = Int.toString (!time_limit)
    94             val _ = debug ("problem file in watcher_call_provers is " ^ probfile)
    95           in
    96             (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
    97               versions of Unix.execute treat them differently!*)
    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 _ = ResLib.helper_path "SPASS_HOME" "SPASS"
   109                     (*We've checked that SPASS is there for ATP/spassshell to run.*)
   110               in 
   111                   ([("spass", goalstring,
   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 = ResLib.helper_path "VAMPIRE_HOME" "vampire"
   119               in
   120                 ([("vampire", goalstring, 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 = ResLib.helper_path "E_HOME" "eproof"
   126 	       in
   127 		  ([("E", goalstring, 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 = ResTypesSorts.classrel_clauses_thy thy
   151       val _ = debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
   152       val arity_clauses = ResTypesSorts.arity_clause_thy thy
   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 		 writenext (n-1); 
   164 		 all_tac))]) n th;
   165 	    ())
   166       in writenext (length prems); clause_arr end;
   167 
   168 val last_watcher_pid = ref (NONE : Posix.Process.pid option);
   169 
   170 
   171 (*writes out the current clasimpset to a tptp file;
   172   turns off xsymbol at start of function, restoring it at end    *)
   173 val isar_atp = setmp print_mode [] 
   174  (fn (ctxt, th) =>
   175   if Thm.no_prems th then ()
   176   else
   177     let
   178       val _ = (case !last_watcher_pid of NONE => ()
   179                | SOME pid => (*FIXME: should kill ATP processes too; at least they time out*)
   180                   (debug ("Killing old watcher, pid = " ^ 
   181                           Int.toString (ResLib.intOfPid pid));
   182                    Watcher.killWatcher pid))
   183               handle OS.SysErr _ => debug "Attempt to kill watcher failed";
   184       val clause_arr = write_problem_files prob_pathname (ctxt,th)
   185       val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
   186     in
   187       last_watcher_pid := SOME pid;
   188       debug ("proof state: " ^ string_of_thm th);
   189       debug ("pid: " ^ Int.toString (ResLib.intOfPid pid));
   190       watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
   191     end);
   192 
   193 val isar_atp_writeonly = setmp print_mode [] 
   194       (fn (ctxt,th) =>
   195        if Thm.no_prems th then ()
   196        else 
   197          let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
   198          	      else prob_pathname
   199          in ignore (write_problem_files pf (ctxt,th)) end);
   200 
   201 
   202 (** the Isar toplevel hook **)
   203 
   204 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   205   let
   206     val proof = Toplevel.proof_of state
   207     val (ctxt, (_, goal)) = Proof.get_goal proof
   208         handle Proof.STATE _ => error "No goal present";
   209     val thy = ProofContext.theory_of ctxt;
   210   in
   211     debug ("initial thm in isar_atp: " ^ 
   212            Pretty.string_of (ProofContext.pretty_thm ctxt goal));
   213     debug ("subgoals in isar_atp: " ^ 
   214            Pretty.string_of (ProofContext.pretty_term ctxt
   215              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   216     debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
   217     debug ("current theory: " ^ Context.theory_name thy);
   218     hook_count := !hook_count +1;
   219     debug ("in hook for time: " ^ Int.toString (!hook_count));
   220     ResClause.init thy;
   221     if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
   222     else isar_atp_writeonly (ctxt, goal)
   223   end);
   224 
   225 val call_atpP =
   226   OuterSyntax.command 
   227     "ProofGeneral.call_atp" 
   228     "call automatic theorem provers" 
   229     OuterKeyword.diag
   230     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   231 
   232 val _ = OuterSyntax.add_parsers [call_atpP];
   233 
   234 end;