src/HOL/Tools/res_atp.ML
author paulson
Wed Aug 17 13:52:53 2005 +0200 (2005-08-17)
changeset 17091 13593aa6a546
parent 16955 93270c5f56f6
child 17150 ce2a1aeb42aa
permissions -rw-r--r--
new command to invoke ATPs
     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 axiom_file : Path.T
    11   val hyps_file : Path.T
    12   val prob_file : Path.T;
    13 (*val atp_ax_tac : thm list -> int -> Tactical.tactic*)
    14 (*val atp_tac : int -> Tactical.tactic*)
    15   val full_spass: bool ref
    16 (*val spass: bool ref*)
    17   val vampire: bool ref
    18   val custom_spass: string list ref
    19 end;
    20 
    21 structure ResAtp: RES_ATP =
    22 struct
    23 
    24 fun debug_tac tac = (debug "testing"; tac);
    25 
    26 val full_spass = ref false;
    27 
    28 (* use spass as default prover *)
    29 (*val spass = ref true;*)
    30 
    31 val custom_spass = ref ["Auto=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub","-DocProof","-TimeLimit=60"];
    32 val vampire = ref false;
    33 
    34 val skolem_tac = skolemize_tac;
    35 
    36 val num_of_clauses = ref 0;
    37 val clause_arr = Array.array (3500, ("empty", 0));
    38 
    39 
    40 val atomize_tac =
    41     SUBGOAL
    42      (fn (prop,_) =>
    43          let val ts = Logic.strip_assums_hyp prop
    44          in EVERY1
    45                 [METAHYPS
    46                      (fn hyps => (cut_facts_tac (map (ObjectLogic.atomize_thm o forall_intr_vars) hyps) 1)),
    47           REPEAT_DETERM_N (length ts) o (etac thin_rl)]
    48      end);
    49 
    50 (* temporarily use these files, which will be loaded by Vampire *)
    51 val file_id_num = ref 0;
    52 fun new_prob_file () = "prob" ^ string_of_int (inc file_id_num);
    53 
    54 val axiom_file = File.tmp_path (Path.basic "axioms");
    55 val clasimp_file = File.tmp_path (Path.basic "clasimp");
    56 val hyps_file = File.tmp_path (Path.basic "hyps");
    57 val prob_file = File.tmp_path (Path.basic "prob");
    58 val dummy_tac = all_tac;
    59 
    60 
    61 (**** for Isabelle/ML interface  ****)
    62 
    63 (*Remove unwanted characters such as ? and newline from the textural 
    64   representation of a theorem (surely they don't need to be produced in 
    65   the first place?) *)
    66 
    67 fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
    68 
    69 val proofstring =
    70     String.translate (fn c => if is_proof_char c then str c else "");
    71 
    72 
    73 (**** For running in Isar ****)
    74 
    75 (* same function as that in res_axioms.ML *)
    76 fun repeat_RS thm1 thm2 =
    77     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
    78     in
    79         if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
    80     end;
    81 
    82 (* a special version of repeat_RS *)
    83 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
    84 
    85 
    86 (*FIXME: is function isar_atp_h used? If not, delete!*)
    87 (*********************************************************************)
    88 (* convert clauses from "assume" to conjecture. write to file "hyps" *)
    89 (* hypotheses of the goal currently being proved                     *)
    90 (*********************************************************************)
    91 (*perhaps have 2 different versions of this, depending on whether or not SpassComm.spass is set *)
    92 fun isar_atp_h thms =
    93     let val prems = map (skolemize o make_nnf o ObjectLogic.atomize_thm) thms
    94         val prems' = map repeat_someI_ex prems
    95         val prems'' = make_clauses prems'
    96         val prems''' = ResAxioms.rm_Eps [] prems''
    97         val clss = map ResClause.make_conjecture_clause prems'''
    98 	val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss) 
    99 	val tfree_lits = ResLib.flat_noDup tfree_litss
   100         (* tfree clause is different in tptp and dfg versions *)
   101 	val tfree_clss = map ResClause.tfree_clause tfree_lits 
   102         val hypsfile = File.platform_path hyps_file
   103         val out = TextIO.openOut(hypsfile)
   104     in
   105         ResLib.writeln_strs out (tfree_clss @ tptp_clss);
   106         TextIO.closeOut out; debug hypsfile;
   107         tfree_lits
   108     end;
   109 
   110 
   111 (*********************************************************************)
   112 (* write out a subgoal as tptp clauses to the file "probN"           *)
   113 (* where N is the number of this subgoal                             *)
   114 (*********************************************************************)
   115 
   116 fun tptp_inputs_tfrees thms n tfrees =
   117     let
   118       val _ = debug ("in tptp_inputs_tfrees 0")
   119       val clss = map (ResClause.make_conjecture_clause_thm) thms
   120       val _ = debug ("in tptp_inputs_tfrees 1")
   121       val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
   122       val _ = debug ("in tptp_inputs_tfrees 2")
   123       val tfree_clss = map ResClause.tfree_clause ((ResLib.flat_noDup tfree_litss) \\ tfrees)
   124       val _ = debug ("in tptp_inputs_tfrees 3")
   125       val probfile = File.platform_path prob_file ^ "_" ^ string_of_int n
   126       val out = TextIO.openOut(probfile)
   127     in
   128       ResLib.writeln_strs out (tfree_clss @ tptp_clss);
   129       TextIO.closeOut out;
   130       debug probfile
   131     end;
   132 
   133 
   134 (*********************************************************************)
   135 (* write out a subgoal as DFG clauses to the file "probN"           *)
   136 (* where N is the number of this subgoal                             *)
   137 (*********************************************************************)
   138 (*
   139 fun dfg_inputs_tfrees thms n tfrees = 
   140     let val _ = (debug ("in dfg_inputs_tfrees 0"))
   141         val clss = map (ResClause.make_conjecture_clause_thm) thms
   142          val _ = (debug ("in dfg_inputs_tfrees 1"))
   143 	val (dfg_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2dfg clss)
   144         val _ = (debug ("in dfg_inputs_tfrees 2"))
   145 	val tfree_clss = map ResClause.tfree_clause ((ResLib.flat_noDup tfree_litss) \\ tfrees) 
   146          val _ = (debug ("in dfg_inputs_tfrees 3"))
   147         val probfile = (File.platform_path prob_file) ^ "_" ^ (string_of_int n)
   148 	val out = TextIO.openOut(probfile)
   149     in
   150 	(ResLib.writeln_strs out (tfree_clss @ dfg_clss); TextIO.closeOut out; debug probfile
   151     end;*)
   152 
   153 (*********************************************************************)
   154 (* call SPASS with settings and problem file for the current subgoal *)
   155 (* should be modified to allow other provers to be called            *)
   156 (*********************************************************************)
   157 (* now passing in list of skolemized thms and list of sgterms to go with them *)
   158 fun call_resolve_tac  (thms: thm list list)  sign (sg_terms:  term list) (childin, childout,pid) n  =
   159   let
   160     val axfile = (File.platform_path axiom_file)
   161 
   162     val hypsfile = (File.platform_path hyps_file)
   163     val clasimpfile = (File.platform_path clasimp_file)
   164 
   165     fun make_atp_list [] sign n = []
   166       | make_atp_list ((sko_thm, sg_term)::xs) sign n =
   167           let
   168             val thmstr = proofstring (Meson.concat_with_and (map string_of_thm sko_thm))
   169             val _ = debug ("thmstring in make_atp_lists is " ^ thmstr)
   170 
   171             val goalstring = proofstring (Sign.string_of_term sign sg_term)
   172             val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
   173 
   174             val probfile = File.platform_path prob_file ^ "_" ^ (string_of_int n)
   175             val _ = debug ("prob file in call_resolve_tac is " ^ probfile)
   176           in
   177             if !SpassComm.spass
   178             then
   179               let val optionline = (*Custom SPASS options, or default?*)
   180 		      if !full_spass (*Auto mode: all SPASS inference rules*)
   181                       then "-DocProof%-TimeLimit=60%-SOS"
   182                       else "-" ^ space_implode "%-" (!custom_spass)
   183                   val _ = debug ("SPASS option string is " ^ optionline)
   184                   val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
   185                     (*We've checked that SPASS is there for ATP/spassshell to run.*)
   186               in 
   187                   ([("spass", thmstr, goalstring,
   188                      getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
   189                      optionline, clasimpfile, axfile, hypsfile, probfile)] @ 
   190                   (make_atp_list xs sign (n+1)))
   191               end
   192             else
   193               let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vkernel"
   194               in
   195                 ([("vampire", thmstr, goalstring, vampire, "-t 60%-m 100000",
   196                    clasimpfile, axfile, hypsfile, probfile)] @
   197                  (make_atp_list xs sign (n+1)))
   198               end
   199           end
   200 
   201     val atp_list = make_atp_list (ListPair.zip (thms, sg_terms)) sign 1
   202   in
   203     Watcher.callResProvers(childout,atp_list);
   204     debug "Sent commands to watcher!";
   205     dummy_tac
   206   end
   207 
   208 (**********************************************************)
   209 (* write out the current subgoal as a tptp file, probN,   *)
   210 (* then call dummy_tac - should be call_res_tac           *)
   211 (**********************************************************)
   212 
   213 fun get_sko_thms tfrees sign sg_terms (childin, childout, pid) thm n sko_thms =
   214   if n = 0 then
   215     (call_resolve_tac (rev sko_thms)
   216       sign sg_terms (childin, childout, pid) (List.length sg_terms);
   217      dummy_tac thm)
   218   else
   219     SELECT_GOAL
   220       (EVERY1 [rtac ccontr, atomize_tac, skolemize_tac,
   221         METAHYPS (fn negs =>
   222           (tptp_inputs_tfrees (make_clauses negs) n tfrees;
   223            get_sko_thms tfrees sign sg_terms (childin, childout, pid) thm (n - 1)
   224              (negs::sko_thms); dummy_tac))]) n thm;
   225 
   226 
   227 (**********************************************)
   228 (* recursively call atp_tac_g on all subgoals *)
   229 (* sg_term is the nth subgoal as a term - used*)
   230 (* in proof reconstruction                    *)
   231 (**********************************************)
   232 
   233 fun isar_atp_goal' thm n tfree_lits (childin, childout, pid) =
   234   let
   235     val prems = Thm.prems_of thm
   236     (*val sg_term = get_nth k prems*)
   237     val sign = sign_of_thm thm
   238     val thmstring = string_of_thm thm
   239   in
   240     debug("in isar_atp_goal'");
   241     debug("thmstring in isar_atp_goal': " ^ thmstring);
   242     (* go and call callResProvers with this subgoal *)
   243     (* isar_atp_g tfree_lits  sg_term (childin, childout, pid) k thm; *)
   244     (* recursive call to pick up the remaining subgoals *)
   245     (* isar_atp_goal' thm (k+1) n tfree_lits  (childin, childout, pid) *)
   246     get_sko_thms tfree_lits sign prems (childin, childout, pid) thm n []
   247   end;
   248 
   249 
   250 (**************************************************)
   251 (* convert clauses from "assume" to conjecture.   *)
   252 (* i.e. apply make_clauses and then get tptp for  *)
   253 (* any hypotheses in the goal produced by assume  *)
   254 (* statements;                                    *)
   255 (* write to file "hyps"                           *)
   256 (**************************************************)
   257 
   258 fun isar_atp_aux thms thm n_subgoals  (childin, childout, pid) =
   259   let val tfree_lits = isar_atp_h thms
   260   in
   261     debug ("in isar_atp_aux");
   262     isar_atp_goal' thm n_subgoals tfree_lits (childin, childout, pid)
   263   end;
   264 
   265 (******************************************************************)
   266 (* called in Isar automatically                                   *)
   267 (* writes out the current clasimpset to a tptp file               *)
   268 (* passes all subgoals on to isar_atp_aux for further processing  *)
   269 (* turns off xsymbol at start of function, restoring it at end    *)
   270 (******************************************************************)
   271 (*FIX changed to clasimp_file *)
   272 val isar_atp' = setmp print_mode [] (fn (ctxt, thms, thm) =>
   273   if Thm.no_prems thm then ()
   274   else
   275     let
   276       val _= debug ("in isar_atp'")
   277       val thy = ProofContext.theory_of ctxt
   278       val prems = Thm.prems_of thm
   279       val thms_string = Meson.concat_with_and (map string_of_thm thms)
   280       val thm_string = string_of_thm thm
   281       val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) prems)
   282 
   283       (*set up variables for writing out the clasimps to a tptp file*)
   284       val (clause_arr, num_of_clauses) =
   285         ResClasimp.write_out_clasimp (File.platform_path clasimp_file) thy
   286           (hd prems) (*FIXME: hack!! need to do all prems*)
   287       val _ = debug ("clasimp_file is " ^ File.platform_path clasimp_file)
   288       val (childin, childout, pid) = Watcher.createWatcher (thm, clause_arr, num_of_clauses)
   289       val pid_string =
   290         string_of_int (Word.toInt (Word.fromLargeWord (Posix.Process.pidToWord pid)))
   291     in
   292       debug ("initial thms: " ^ thms_string);
   293       debug ("initial thm: " ^ thm_string);
   294       debug ("subgoals: " ^ prems_string);
   295       debug ("pid: "^ pid_string);
   296       isar_atp_aux thms thm (length prems) (childin, childout, pid);
   297       ()
   298     end);
   299 
   300 
   301 fun get_thms_cs claset =
   302   let val {safeEs, safeIs, hazEs, hazIs, ...} = rep_cs claset
   303   in safeEs @ safeIs @ hazEs @ hazIs end;
   304 
   305 fun append_name name [] _ = []
   306   | append_name name (thm :: thms) k =
   307       Thm.name_thm ((name ^ "_" ^ string_of_int k), thm) :: append_name name thms (k + 1);
   308 
   309 fun append_names (name :: names) (thms :: thmss) =
   310   append_name name thms 0 :: append_names names thmss;
   311 
   312 fun get_thms_ss [] = []
   313   | get_thms_ss thms =
   314       let
   315         val names = map Thm.name_of_thm thms
   316         val thms' = map (mksimps mksimps_pairs) thms
   317         val thms'' = append_names names thms'
   318       in
   319         ResLib.flat_noDup thms''
   320       end;
   321 
   322 
   323 (* convert locally declared rules to axiom clauses *)
   324 
   325 fun subtract_simpset thy ctxt =
   326   let
   327     val rules1 = #rules (#1 (rep_ss (simpset_of thy)));
   328     val rules2 = #rules (#1 (rep_ss (local_simpset_of ctxt)));
   329   in map #thm (Net.subtract MetaSimplifier.eq_rrule rules1 rules2) end;
   330 
   331 fun subtract_claset thy ctxt =
   332   let
   333     val (netI1, netE1) = #xtra_netpair (rep_cs (claset_of thy));
   334     val (netI2, netE2) = #xtra_netpair (rep_cs (local_claset_of ctxt));
   335     val subtract = map (#2 o #2) oo Net.subtract Tactic.eq_kbrl;
   336   in subtract netI1 netI2 @ subtract netE1 netE2 end;
   337 
   338 
   339 
   340 (** the Isar toplevel hook **)
   341 
   342 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   343   let
   344     val proof = Toplevel.proof_of state
   345     val (ctxt, (_, goal)) = Proof.get_goal proof
   346         handle Proof.STATE _ => error "No goal present";
   347     val thy = ProofContext.theory_of ctxt;
   348 
   349     (* FIXME presently unused *)
   350     val ss_thms = subtract_simpset thy ctxt;
   351     val cs_thms = subtract_claset thy ctxt;
   352   in
   353     debug ("initial thm in isar_atp: " ^ 
   354            Pretty.string_of (ProofContext.pretty_thm ctxt goal));
   355     debug ("subgoals in isar_atp: " ^ 
   356            Pretty.string_of (ProofContext.pretty_term ctxt
   357              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   358     debug ("number of subgoals in isar_atp: " ^ string_of_int (Thm.nprems_of goal));
   359     ResClause.init thy;
   360     isar_atp' (ctxt, ProofContext.prems_of ctxt, goal)
   361   end);
   362 
   363 val call_atpP =
   364   OuterSyntax.improper_command 
   365     "ProofGeneral.call_atp" 
   366     "call automatic theorem provers" 
   367     OuterKeyword.diag
   368     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   369 
   370 val _ = OuterSyntax.add_parsers [call_atpP];
   371 
   372 end;