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