src/HOL/Tools/res_atp.ML
author paulson
Thu Feb 09 12:20:31 2006 +0100 (2006-02-09)
changeset 18986 5060ca625e02
parent 18863 a113b6839df1
child 19194 7681c04d8bff
permissions -rw-r--r--
tidying
     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   FIXME: does not cope with &&, and it isn't easy because one could have multiple
   105   subgoals, each involving &&.*)
   106 fun write_problem_files pf (ctxt,th)  =
   107   let val goals = Thm.prems_of th
   108       val _ = Output.debug ("number of subgoals = " ^ Int.toString (length goals));
   109       val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt goals 
   110       val _ = Output.debug ("claset and simprules total clauses = " ^ 
   111                      Int.toString (Array.length clause_arr))
   112       val thy = ProofContext.theory_of ctxt
   113       val classrel_clauses = 
   114           if !ResClause.keep_types then ResClause.classrel_clauses_thy thy else []
   115       val _ = Output.debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
   116       val arity_clauses = 
   117           if !ResClause.keep_types then ResClause.arity_clause_thy thy else []
   118       val _ = Output.debug ("arity clauses = " ^ Int.toString (length arity_clauses))
   119       val write = if !prover = "spass" then ResClause.dfg_write_file 
   120                                        else ResClause.tptp_write_file
   121       fun writenext n =
   122 	if n=0 then []
   123 	 else
   124 	   (SELECT_GOAL
   125 	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
   126 	      METAHYPS(fn negs => 
   127 		(write (make_clauses negs) (pf n) 
   128 		       (axclauses,classrel_clauses,arity_clauses);
   129 		 all_tac))]) n th;
   130 	    pf n :: writenext (n-1))
   131       in (writenext (length goals), clause_arr) end;
   132 
   133 val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
   134                                     Posix.Process.pid * string list) option);
   135 
   136 fun kill_last_watcher () =
   137     (case !last_watcher_pid of 
   138          NONE => ()
   139        | SOME (_, childout, pid, files) => 
   140 	  (Output.debug ("Killing old watcher, pid = " ^ string_of_pid pid);
   141 	   Watcher.killWatcher pid;  
   142 	   ignore (map (try OS.FileSys.remove) files)))
   143      handle OS.SysErr _ => Output.debug "Attempt to kill watcher failed";
   144 
   145 (*writes out the current clasimpset to a tptp file;
   146   turns off xsymbol at start of function, restoring it at end    *)
   147 val isar_atp = setmp print_mode [] 
   148  (fn (ctxt, th) =>
   149   if Thm.no_prems th then ()
   150   else
   151     let
   152       val _ = kill_last_watcher()
   153       val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
   154       val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
   155     in
   156       last_watcher_pid := SOME (childin, childout, pid, files);
   157       Output.debug ("problem files: " ^ space_implode ", " files); 
   158       Output.debug ("pid: " ^ string_of_pid pid);
   159       watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
   160     end);
   161 
   162 val isar_atp_writeonly = setmp print_mode [] 
   163       (fn (ctxt,th) =>
   164        if Thm.no_prems th then ()
   165        else 
   166          let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
   167          	      else prob_pathname
   168          in ignore (write_problem_files pf (ctxt,th)) end);
   169 
   170 
   171 (** the Isar toplevel hook **)
   172 
   173 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   174   let
   175     val proof = Toplevel.proof_of state
   176     val (ctxt, (_, goal)) = Proof.get_goal proof;
   177     val thy = ProofContext.theory_of ctxt;
   178   in
   179     Output.debug ("subgoals in isar_atp:\n" ^ 
   180            Pretty.string_of (ProofContext.pretty_term ctxt
   181              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   182     Output.debug ("current theory: " ^ Context.theory_name thy);
   183     hook_count := !hook_count +1;
   184     Output.debug ("in hook for time: " ^ Int.toString (!hook_count));
   185     ResClause.init thy;
   186     if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
   187     else isar_atp_writeonly (ctxt, goal)
   188   end);
   189 
   190 val call_atpP =
   191   OuterSyntax.command 
   192     "ProofGeneral.call_atp" 
   193     "call automatic theorem provers" 
   194     OuterKeyword.diag
   195     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   196 
   197 val _ = OuterSyntax.add_parsers [call_atpP];
   198 
   199 end;