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