src/HOL/Tools/res_atp.ML
author paulson
Wed Sep 28 11:16:27 2005 +0200 (2005-09-28)
changeset 17690 8ba7c3cd24a8
parent 17525 ae5bb6001afb
child 17717 7c6a96cbc966
permissions -rw-r--r--
time limit option; fixed bug concerning first line of ATP output
     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 prob_pathname() = 
    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 
    39 (**** for Isabelle/ML interface  ****)
    40 
    41 (*Remove unwanted characters such as ? and newline from the textural 
    42   representation of a theorem (surely they don't need to be produced in 
    43   the first place?) *)
    44 
    45 fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
    46 
    47 val proofstring =
    48     String.translate (fn c => if is_proof_char c then str c else "");
    49 
    50 
    51 (**** For running in Isar ****)
    52 
    53 (* same function as that in res_axioms.ML *)
    54 fun repeat_RS thm1 thm2 =
    55     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
    56     in
    57         if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
    58     end;
    59 
    60 (* a special version of repeat_RS *)
    61 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
    62 
    63 
    64 (* write out a subgoal as tptp clauses to the file "xxxx_N"*)
    65 fun tptp_inputs_tfrees thms n (axclauses,classrel_clauses,arity_clauses) =
    66     let
    67       val clss = map (ResClause.make_conjecture_clause_thm) thms
    68       val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
    69       val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
    70       val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses
    71       val arity_cls = map ResClause.tptp_arity_clause arity_clauses
    72       val probfile = prob_pathname() ^ "_" ^ Int.toString n
    73       val out = TextIO.openOut(probfile)
    74     in
    75       ResLib.writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
    76       ResLib.writeln_strs out (tfree_clss @ tptp_clss @ classrel_cls @ arity_cls);
    77       TextIO.closeOut out;
    78       debug probfile
    79     end;
    80 
    81 (* write out a subgoal in DFG format to the file "xxxx_N"*)
    82 fun dfg_inputs_tfrees thms n (axclauses,classrel_clauses,arity_clauses) = 
    83     let val clss = map (ResClause.make_conjecture_clause_thm) thms
    84         val probfile = prob_pathname() ^ "_" ^ (Int.toString n)
    85         (*FIXME: classrel_clauses and arity_clauses*)
    86         val _ = debug ("about to write out dfg prob file " ^ probfile)
    87         val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ Int.toString n)
    88                         axclauses [] [] []    
    89 	val out = TextIO.openOut(probfile)
    90     in
    91 	(ResLib.writeln_strs out [probN]; TextIO.closeOut out; debug probfile )
    92     end;
    93 
    94 
    95 (*********************************************************************)
    96 (* call prover with settings and problem file for the current subgoal *)
    97 (*********************************************************************)
    98 (* now passing in list of skolemized thms and list of sgterms to go with them *)
    99 fun watcher_call_provers sign sg_terms (childin, childout,pid) =
   100   let
   101     fun make_atp_list [] n = []
   102       | make_atp_list ((sg_term)::xs) n =
   103           let
   104             val goalstring = proofstring (Sign.string_of_term sign sg_term)
   105             val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
   106 
   107             val probfile = prob_pathname() ^ "_" ^ Int.toString n
   108             val time = Int.toString (!time_limit)
   109             val _ = debug ("problem file in watcher_call_provers is " ^ probfile)
   110           in
   111             (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
   112               versions of Unix.execute treat them differently!*)
   113             if !prover = "spass"
   114             then
   115               let val optionline = 
   116 		      if !AtpCommunication.reconstruct 
   117 		          (*Proof reconstruction works for only a limited set of 
   118 		            inference rules*)
   119                       then space_implode "%" (!custom_spass) ^
   120                            "%-DocProof%-TimeLimit=" ^ time
   121                       else "-DocProof%-SOS%-FullRed=0%-TimeLimit=" ^ time (*Auto mode*)
   122                   val _ = debug ("SPASS option string is " ^ optionline)
   123                   val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
   124                     (*We've checked that SPASS is there for ATP/spassshell to run.*)
   125               in 
   126                   ([("spass", goalstring,
   127                      getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
   128                      optionline, probfile)] @ 
   129                   (make_atp_list xs (n+1)))
   130               end
   131             else if !prover = "vampire"
   132 	    then 
   133               let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vampire"
   134               in
   135                 ([("vampire", goalstring, vampire, "-m 100000%-t " ^ time, probfile)] @
   136                  (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
   137               end
   138       	     else if !prover = "E"
   139       	     then
   140 	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
   141 	       in
   142 		  ([("E", goalstring, Eprover, 
   143 		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
   144 		     probfile)] @
   145 		   (make_atp_list xs (n+1)))
   146 	       end
   147 	     else error ("Invalid prover name: " ^ !prover)
   148           end
   149 
   150     val atp_list = make_atp_list sg_terms 1
   151   in
   152     Watcher.callResProvers(childout,atp_list);
   153     debug "Sent commands to watcher!"
   154   end
   155 
   156 (*We write out problem files for each subgoal*)
   157 fun write_problem_files clause thm n =
   158     if n=0 then ()
   159      else
   160        (SELECT_GOAL
   161         (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
   162           METAHYPS(fn negs => 
   163             (if !prover = "spass" 
   164              then dfg_inputs_tfrees (make_clauses negs) n clause
   165              else tptp_inputs_tfrees (make_clauses negs) n clause;
   166              write_problem_files clause thm (n-1); 
   167              all_tac))]) n thm;
   168         ());
   169 
   170 val last_watcher_pid = ref (NONE : Posix.Process.pid option);
   171 
   172 
   173 (*writes out the current clasimpset to a tptp file;
   174   turns off xsymbol at start of function, restoring it at end    *)
   175 val isar_atp = setmp print_mode [] 
   176  (fn (ctxt, thm) =>
   177   if Thm.no_prems thm then ()
   178   else
   179     let
   180       val _= debug "in isar_atp"
   181       val thy = ProofContext.theory_of ctxt
   182       val prems = Thm.prems_of thm
   183       val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) prems)
   184 
   185       val _ = (case !last_watcher_pid of NONE => ()
   186                | SOME pid => (*FIXME: should kill ATP processes too; at least they time out*)
   187                   (debug ("Killing old watcher, pid = " ^ 
   188                           Int.toString (ResLib.intOfPid pid));
   189                    Watcher.killWatcher pid))
   190               handle OS.SysErr _ => debug "Attempt to kill watcher failed";
   191       (*set up variables for writing out the clasimps to a tptp file*)
   192       val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
   193               (*FIXME: hack!! need to consider relevance for all prems*)
   194       val _ = debug ("claset and simprules total clauses = " ^ 
   195                      Int.toString (Array.length clause_arr))
   196       val classrel_clauses = ResTypesSorts.classrel_clauses_thy thy
   197       val _ = debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
   198       val arity_clauses = ResTypesSorts.arity_clause_thy thy
   199       val _ = debug ("arity clauses = " ^ Int.toString (length arity_clauses))
   200       val (childin, childout, pid) = Watcher.createWatcher (thm, clause_arr)
   201     in
   202       last_watcher_pid := SOME pid;
   203       debug ("subgoals: " ^ prems_string);
   204       debug ("pid: " ^ Int.toString (ResLib.intOfPid pid));
   205       write_problem_files (axclauses,classrel_clauses,arity_clauses) thm (length prems);
   206       watcher_call_provers (sign_of_thm thm) (Thm.prems_of thm) (childin, childout, pid)
   207     end);
   208 
   209 val isar_atp_writeonly = setmp print_mode [] 
   210  (fn (ctxt, thm) =>
   211   if Thm.no_prems thm then ()
   212   else
   213     let 
   214       val prems = Thm.prems_of thm
   215       val (_, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
   216       val thy = ProofContext.theory_of ctxt
   217       val classrel_clauses = ResTypesSorts.classrel_clauses_thy thy
   218       val arity_clauses = ResTypesSorts.arity_clause_thy thy
   219     in
   220       write_problem_files (axclauses,classrel_clauses,arity_clauses) thm (length prems)
   221     end);
   222 
   223 
   224 (** the Isar toplevel hook **)
   225 
   226 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   227   let
   228     val proof = Toplevel.proof_of state
   229     val (ctxt, (_, goal)) = Proof.get_goal proof
   230         handle Proof.STATE _ => error "No goal present";
   231     val thy = ProofContext.theory_of ctxt;
   232   in
   233     debug ("initial thm in isar_atp: " ^ 
   234            Pretty.string_of (ProofContext.pretty_thm ctxt goal));
   235     debug ("subgoals in isar_atp: " ^ 
   236            Pretty.string_of (ProofContext.pretty_term ctxt
   237              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   238     debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
   239     debug ("current theory: " ^ Context.theory_name thy);
   240     hook_count := !hook_count +1;
   241     debug ("in hook for time: " ^(Int.toString (!hook_count)) );
   242     ResClause.init thy;
   243     if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
   244     else isar_atp_writeonly (ctxt, goal)
   245   end);
   246 
   247 val call_atpP =
   248   OuterSyntax.improper_command 
   249     "ProofGeneral.call_atp" 
   250     "call automatic theorem provers" 
   251     OuterKeyword.diag
   252     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   253 
   254 val _ = OuterSyntax.add_parsers [call_atpP];
   255 
   256 end;