src/HOL/Tools/res_atp.ML
author paulson
Tue Jan 31 10:39:13 2006 +0100 (2006-01-31)
changeset 18863 a113b6839df1
parent 18798 ca02a2077955
child 18986 5060ca625e02
permissions -rw-r--r--
reorganization of code to support DFG otuput
     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 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 30;
    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 (*Return the path to a "helper" like SPASS or tptp2X, first checking that
    33   it exists.  FIXME: modify to use Path primitives and move to some central place.*)  
    34 fun helper_path evar base =
    35   case getenv evar of
    36       "" => error  ("Isabelle environment variable " ^ evar ^ " not defined")
    37     | home => 
    38         let val path = home ^ "/" ^ base
    39         in  if File.exists (File.unpack_platform_path path) then path 
    40 	    else error ("Could not find the file " ^ path)
    41 	end;  
    42 
    43 fun probfile_nosuffix _ = 
    44   if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
    45   else if File.exists (File.unpack_platform_path (!destdir))
    46   then !destdir ^ "/" ^ !problem_name
    47   else error ("No such directory: " ^ !destdir);
    48 
    49 fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n;
    50 
    51 (* call prover with settings and problem file for the current subgoal *)
    52 fun watcher_call_provers sign sg_terms (childin, childout, pid) =
    53   let
    54     fun make_atp_list [] n = []
    55       | make_atp_list (sg_term::xs) n =
    56           let
    57             val probfile = prob_pathname n
    58             val time = Int.toString (!time_limit)
    59           in
    60             Output.debug ("problem file in watcher_call_provers is " ^ probfile);
    61             (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
    62               versions of Unix.execute treat them differently!*)
    63             (*options are separated by Watcher.setting_sep, currently #"%"*)
    64             if !prover = "spass"
    65             then
    66               let val baseopts = "%-PGiven=0%-PProblem=0%-Splits=0%-FullRed=0%-DocProof%-TimeLimit=" ^ time
    67               val infopts = 
    68 		      if !AtpCommunication.reconstruct 
    69 		          (*Proof reconstruction needs a limited set of inf rules*)
    70                       then space_implode "%" (!custom_spass)                           
    71                       else "-Auto%-SOS=1"
    72                   val spass = helper_path "SPASS_HOME" "SPASS"
    73             in 
    74                 ([("spass", spass, infopts ^ baseopts, probfile)] @ 
    75                   make_atp_list xs (n+1))
    76               end
    77             else if !prover = "vampire"
    78 	    then 
    79               let val vampire = helper_path "VAMPIRE_HOME" "vampire"
    80               in
    81                 ([("vampire", vampire, "-m 100000%-t " ^ time, probfile)] @
    82                  make_atp_list xs (n+1))       (*BEWARE! spaces in options!*)
    83               end
    84       	     else if !prover = "E"
    85       	     then
    86 	       let val Eprover = helper_path "E_HOME" "eproof"
    87 	       in
    88 		  ([("E", Eprover, 
    89 		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
    90 		     probfile)] @
    91 		   make_atp_list xs (n+1))
    92 	       end
    93 	     else error ("Invalid prover name: " ^ !prover)
    94           end
    95 
    96     val atp_list = make_atp_list sg_terms 1
    97   in
    98     Watcher.callResProvers(childout,atp_list);
    99     Output.debug "Sent commands to watcher!"
   100   end
   101 
   102 (*We write out problem files for each subgoal. Argument pf generates filenames,
   103   and allows the suppression of the suffix "_1" in problem-generation mode.*)
   104 fun write_problem_files pf (ctxt,th)  =
   105   let val goals = Thm.prems_of th
   106       val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt goals 
   107       val _ = Output.debug ("claset and simprules total clauses = " ^ 
   108                      Int.toString (Array.length clause_arr))
   109       val thy = ProofContext.theory_of ctxt
   110       val classrel_clauses = if !ResClause.keep_types then ResClause.classrel_clauses_thy thy else []
   111       val _ = Output.debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
   112       val arity_clauses = if !ResClause.keep_types then ResClause.arity_clause_thy thy else []
   113       val _ = Output.debug ("arity clauses = " ^ Int.toString (length arity_clauses))
   114       val write = if !prover = "spass" then ResClause.dfg_write_file 
   115                                        else ResClause.tptp_write_file
   116       fun writenext n =
   117 	if n=0 then []
   118 	 else
   119 	   (SELECT_GOAL
   120 	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
   121 	      METAHYPS(fn negs => 
   122 		(write (make_clauses negs) (pf n) 
   123 		       (axclauses,classrel_clauses,arity_clauses);
   124 		 all_tac))]) n th;
   125 	    pf n :: writenext (n-1))
   126       in (writenext (length goals), clause_arr) end;
   127 
   128 val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
   129                                     Posix.Process.pid * string list) option);
   130 
   131 fun kill_last_watcher () =
   132     (case !last_watcher_pid of 
   133          NONE => ()
   134        | SOME (_, childout, pid, files) => 
   135 	  (Output.debug ("Killing old watcher, pid = " ^ string_of_pid pid);
   136 	   Watcher.killWatcher pid;  
   137 	   ignore (map (try OS.FileSys.remove) files)))
   138      handle OS.SysErr _ => Output.debug "Attempt to kill watcher failed";
   139 
   140 (*writes out the current clasimpset to a tptp file;
   141   turns off xsymbol at start of function, restoring it at end    *)
   142 val isar_atp = setmp print_mode [] 
   143  (fn (ctxt, th) =>
   144   if Thm.no_prems th then ()
   145   else
   146     let
   147       val _ = kill_last_watcher()
   148       val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
   149       val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
   150     in
   151       last_watcher_pid := SOME (childin, childout, pid, files);
   152       Output.debug ("problem files: " ^ space_implode ", " files); 
   153       Output.debug ("pid: " ^ string_of_pid pid);
   154       watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
   155     end);
   156 
   157 val isar_atp_writeonly = setmp print_mode [] 
   158       (fn (ctxt,th) =>
   159        if Thm.no_prems th then ()
   160        else 
   161          let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
   162          	      else prob_pathname
   163          in ignore (write_problem_files pf (ctxt,th)) end);
   164 
   165 
   166 (** the Isar toplevel hook **)
   167 
   168 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   169   let
   170     val proof = Toplevel.proof_of state
   171     val (ctxt, (_, goal)) = Proof.get_goal proof;
   172     val thy = ProofContext.theory_of ctxt;
   173   in
   174     Output.debug ("subgoals in isar_atp:\n" ^ 
   175            Pretty.string_of (ProofContext.pretty_term ctxt
   176              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   177     Output.debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
   178     Output.debug ("current theory: " ^ Context.theory_name thy);
   179     hook_count := !hook_count +1;
   180     Output.debug ("in hook for time: " ^ Int.toString (!hook_count));
   181     ResClause.init thy;
   182     if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
   183     else isar_atp_writeonly (ctxt, goal)
   184   end);
   185 
   186 val call_atpP =
   187   OuterSyntax.command 
   188     "ProofGeneral.call_atp" 
   189     "call automatic theorem provers" 
   190     OuterKeyword.diag
   191     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   192 
   193 val _ = OuterSyntax.add_parsers [call_atpP];
   194 
   195 end;