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