src/HOL/Tools/res_atp.ML
author paulson
Fri Sep 16 11:39:09 2005 +0200 (2005-09-16)
changeset 17435 0eed5a1c00c1
parent 17422 3b237822985d
child 17484 f6a225f97f0a
permissions -rw-r--r--
PARTIAL conversion to Vampire8
     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 hook_count: int ref
    13 end;
    14 
    15 structure ResAtp: RES_ATP =
    16 struct
    17 
    18 
    19 val call_atp = ref false;
    20 val hook_count = ref 0;
    21 
    22 fun debug_tac tac = (debug "testing"; tac);
    23 
    24 val prover = ref "E";   (* use E as the default prover *)
    25 val custom_spass =   (*specialized options for SPASS*)
    26       ref ["Auto=0","-FullRed=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub",
    27            "-DocProof","-TimeLimit=60"];
    28 
    29 val prob_file = File.tmp_path (Path.basic "prob");
    30 
    31 
    32 (**** for Isabelle/ML interface  ****)
    33 
    34 (*Remove unwanted characters such as ? and newline from the textural 
    35   representation of a theorem (surely they don't need to be produced in 
    36   the first place?) *)
    37 
    38 fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
    39 
    40 val proofstring =
    41     String.translate (fn c => if is_proof_char c then str c else "");
    42 
    43 
    44 (**** For running in Isar ****)
    45 
    46 (* same function as that in res_axioms.ML *)
    47 fun repeat_RS thm1 thm2 =
    48     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
    49     in
    50         if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
    51     end;
    52 
    53 (* a special version of repeat_RS *)
    54 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
    55 
    56 
    57 (*********************************************************************)
    58 (* write out a subgoal as tptp clauses to the file "probN"           *)
    59 (* where N is the number of this subgoal                             *)
    60 (*********************************************************************)
    61 
    62 fun tptp_inputs_tfrees thms n axclauses =
    63     let
    64       val _ = debug ("in tptp_inputs_tfrees 0")
    65       val clss = map (ResClause.make_conjecture_clause_thm) thms
    66       val _ = debug ("in tptp_inputs_tfrees 1")
    67       val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
    68       val _ = debug ("in tptp_inputs_tfrees 2")
    69       val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
    70       val _ = debug ("in tptp_inputs_tfrees 3")
    71       val probfile = File.platform_path prob_file ^ "_" ^ string_of_int n
    72       val out = TextIO.openOut(probfile)
    73     in
    74       ResLib.writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
    75       ResLib.writeln_strs out (tfree_clss @ tptp_clss);
    76       TextIO.closeOut out;
    77       debug probfile
    78     end;
    79 
    80 
    81 (*********************************************************************)
    82 (* write out a subgoal as DFG clauses to the file "probN"           *)
    83 (* where N is the number of this subgoal                             *)
    84 (*********************************************************************)
    85 
    86 fun dfg_inputs_tfrees thms n axclauses = 
    87     let val clss = map (ResClause.make_conjecture_clause_thm) thms
    88         val probfile = (File.platform_path prob_file) ^ "_" ^ (string_of_int n)
    89         val _ = debug ("about to write out dfg prob file " ^ probfile)
    90         val probN = ResClause.clauses2dfg clss ("prob" ^ (string_of_int n)) 
    91                         axclauses [] [] []    
    92 	val out = TextIO.openOut(probfile)
    93     in
    94 	(ResLib.writeln_strs out [probN]; TextIO.closeOut out; debug probfile )
    95 (* (ResLib.writeln_strs out (tfree_clss @ dfg_clss); *)
    96     end;
    97 
    98 
    99 (*********************************************************************)
   100 (* call prover with settings and problem file for the current subgoal *)
   101 (*********************************************************************)
   102 (* now passing in list of skolemized thms and list of sgterms to go with them *)
   103 fun watcher_call_provers sign sg_terms (childin, childout,pid) =
   104   let
   105     fun make_atp_list [] n = []
   106       | make_atp_list ((sg_term)::xs) n =
   107           let
   108             val goalstring = proofstring (Sign.string_of_term sign sg_term)
   109             val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
   110 
   111             val probfile = File.platform_path prob_file ^ "_" ^ (string_of_int n)
   112             val _ = debug ("prob file in watcher_call_provers is " ^ probfile)
   113           in
   114             (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
   115               versions of Unix.execute treat them differently!*)
   116             if !prover = "spass"
   117             then
   118               let val optionline = 
   119 		      if !SpassComm.reconstruct 
   120 		          (*Proof reconstruction works for only a limited set of 
   121 		            inference rules*)
   122                       then "-" ^ space_implode "%-" (!custom_spass)
   123                       else "-DocProof%-TimeLimit=60%-SOS%-FullRed=0" (*Auto mode*)
   124                   val _ = debug ("SPASS option string is " ^ optionline)
   125                   val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
   126                     (*We've checked that SPASS is there for ATP/spassshell to run.*)
   127               in 
   128                   ([("spass", goalstring,
   129                      getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
   130                      optionline, probfile)] @ 
   131                   (make_atp_list xs (n+1)))
   132               end
   133             else if !prover = "vampire"
   134 	    then 
   135               let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vampire"
   136               in
   137                 ([("vampire", goalstring, vampire, "-t 60%-m 100000", probfile)] @
   138                  (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
   139               end
   140       	     else if !prover = "E"
   141       	     then
   142 	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
   143 	       in
   144 		  ([("E", goalstring, Eprover, 
   145 		     "--tptp-in%-l5%-xAuto%-tAuto%--soft-cpu-limit=60",
   146 		     probfile)] @
   147 		   (make_atp_list xs (n+1)))
   148 	       end
   149 	     else error ("Invalid prover name: " ^ !prover)
   150           end
   151 
   152     val atp_list = make_atp_list sg_terms 1
   153   in
   154     Watcher.callResProvers(childout,atp_list);
   155     debug "Sent commands to watcher!"
   156   end
   157 
   158 (*We write out problem files for each subgoal, but work is repeated (skolemize)*)
   159 fun write_problem_files axclauses thm n =
   160     if n=0 then ()
   161      else
   162        (SELECT_GOAL
   163         (EVERY1 [rtac ccontr, ResLib.atomize_tac, skolemize_tac, 
   164           METAHYPS(fn negs => 
   165             (if !prover = "spass" 
   166              then dfg_inputs_tfrees (make_clauses negs) n axclauses
   167              else tptp_inputs_tfrees (make_clauses negs) n axclauses;
   168              write_problem_files axclauses thm (n-1); 
   169              all_tac))]) n thm;
   170         ());
   171 
   172 
   173 (******************************************************************)
   174 (* called in Isar automatically                                   *)
   175 (* writes out the current clasimpset to a tptp file               *)
   176 (* turns off xsymbol at start of function, restoring it at end    *)
   177 (******************************************************************)
   178 (*FIX changed to clasimp_file *)
   179 val isar_atp' = setmp print_mode [] 
   180  (fn (ctxt, thms, thm) =>
   181   if Thm.no_prems thm then ()
   182   else
   183     let
   184       val _= debug ("in isar_atp'")
   185       val thy = ProofContext.theory_of ctxt
   186       val prems = Thm.prems_of thm
   187       val thms_string = Meson.concat_with_and (map string_of_thm thms)
   188       val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) prems)
   189 
   190       (*set up variables for writing out the clasimps to a tptp file*)
   191       val (clause_arr, num_of_clauses, axclauses) =
   192         ResClasimp.get_clasimp_lemmas thy (hd prems) (*FIXME: hack!! need to do all prems*)
   193       val _ = debug ("claset and simprules total " ^ (string_of_int num_of_clauses)^
   194                   " clauses")
   195       val (childin, childout, pid) = 
   196           Watcher.createWatcher (thm, clause_arr, num_of_clauses)
   197       val pid_string =
   198         string_of_int (Word.toInt (Word.fromLargeWord (Posix.Process.pidToWord pid)))
   199     in
   200       debug ("initial thms: " ^ thms_string);
   201       debug ("subgoals: " ^ prems_string);
   202       debug ("pid: "^ pid_string);
   203       write_problem_files axclauses thm (length prems);
   204       watcher_call_provers (sign_of_thm thm) (Thm.prems_of thm) (childin, childout, pid)
   205     end);
   206 
   207 val isar_atp_writeonly = setmp print_mode [] 
   208  (fn (ctxt, thms, thm) =>
   209   if Thm.no_prems thm then ()
   210   else
   211     let
   212       val thy = ProofContext.theory_of ctxt
   213       val prems = Thm.prems_of thm
   214 
   215       (*set up variables for writing out the clasimps to a tptp file*)
   216       val (clause_arr, num_of_clauses, axclauses) =
   217         ResClasimp.get_clasimp_lemmas thy (hd prems) (*FIXME: hack!! need to do all prems*)
   218     in
   219       write_problem_files axclauses thm (length prems)
   220     end);
   221 
   222 fun get_thms_cs claset =
   223   let val {safeEs, safeIs, hazEs, hazIs, ...} = rep_cs claset
   224   in safeEs @ safeIs @ hazEs @ hazIs end;
   225 
   226 fun append_name name [] _ = []
   227   | append_name name (thm :: thms) k =
   228       Thm.name_thm ((name ^ "_" ^ string_of_int k), thm) :: append_name name thms (k + 1);
   229 
   230 fun append_names (name :: names) (thms :: thmss) =
   231   append_name name thms 0 :: append_names names thmss;
   232 
   233 fun get_thms_ss [] = []
   234   | get_thms_ss thms =
   235       let
   236         val names = map Thm.name_of_thm thms
   237         val thms' = map (mksimps mksimps_pairs) thms
   238         val thms'' = append_names names thms'
   239       in
   240         ResLib.flat_noDup thms''
   241       end;
   242 
   243 
   244 (* convert locally declared rules to axiom clauses *)
   245 
   246 fun subtract_simpset thy ctxt =
   247   let
   248     val rules1 = #rules (#1 (rep_ss (simpset_of thy)));
   249     val rules2 = #rules (#1 (rep_ss (local_simpset_of ctxt)));
   250   in map #thm (Net.subtract MetaSimplifier.eq_rrule rules1 rules2) end;
   251 
   252 fun subtract_claset thy ctxt =
   253   let
   254     val (netI1, netE1) = #xtra_netpair (rep_cs (claset_of thy));
   255     val (netI2, netE2) = #xtra_netpair (rep_cs (local_claset_of ctxt));
   256     val subtract = map (#2 o #2) oo Net.subtract Tactic.eq_kbrl;
   257   in subtract netI1 netI2 @ subtract netE1 netE2 end;
   258 
   259 
   260 
   261 (** the Isar toplevel hook **)
   262 
   263 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   264   let
   265     val proof = Toplevel.proof_of state
   266     val (ctxt, (_, goal)) = Proof.get_goal proof
   267         handle Proof.STATE _ => error "No goal present";
   268 
   269     val thy = ProofContext.theory_of ctxt;
   270 
   271     (* FIXME presently unused *)
   272     val ss_thms = subtract_simpset thy ctxt;
   273     val cs_thms = subtract_claset thy ctxt;
   274   in
   275     debug ("initial thm in isar_atp: " ^ 
   276            Pretty.string_of (ProofContext.pretty_thm ctxt goal));
   277     debug ("subgoals in isar_atp: " ^ 
   278            Pretty.string_of (ProofContext.pretty_term ctxt
   279              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   280     debug ("number of subgoals in isar_atp: " ^ string_of_int (Thm.nprems_of goal));
   281     hook_count := !hook_count +1;
   282     debug ("in hook for time: " ^(string_of_int (!hook_count)) );
   283     ResClause.init thy;
   284     isar_atp' (ctxt, ProofContext.prems_of ctxt, goal)
   285   end);
   286 
   287 val call_atpP =
   288   OuterSyntax.improper_command 
   289     "ProofGeneral.call_atp" 
   290     "call automatic theorem provers" 
   291     OuterKeyword.diag
   292     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   293 
   294 val _ = OuterSyntax.add_parsers [call_atpP];
   295 
   296 end;