1 (* Author: Jia Meng, Cambridge University Computer Laboratory
3 Copyright 2004 University of Cambridge
5 ATPs with TPTP format input.
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*)
19 val custom_spass: string list ref
22 structure ResAtp: RES_ATP =
25 val call_atp = ref false;
27 fun debug_tac tac = (debug "testing"; tac);
29 val full_spass = ref false;
31 (* use spass as default prover *)
32 (*val spass = ref true;*)
34 val custom_spass = ref ["Auto=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub","-DocProof","-TimeLimit=60"];
35 val vampire = ref false;
37 val skolem_tac = skolemize_tac;
39 val num_of_clauses = ref 0;
40 val clause_arr = Array.array (3500, ("empty", 0));
46 let val ts = Logic.strip_assums_hyp prop
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)]
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);
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;
64 (**** for Isabelle/ML interface ****)
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
70 fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
73 String.translate (fn c => if is_proof_char c then str c else "");
77 fun call_atp_tac thms n = (tptp_inputs thms ; dummy_tac);
81 (**** For running in Isar ****)
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
87 if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
90 (* a special version of repeat_RS *)
91 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
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 *)
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)
111 ResLib.writeln_strs out (tfree_clss @ tptp_clss);
112 TextIO.closeOut out; debug hypsfile;
117 (*********************************************************************)
118 (* write out a subgoal as tptp clauses to the file "probN" *)
119 (* where N is the number of this subgoal *)
120 (*********************************************************************)
122 fun tptp_inputs_tfrees thms n tfrees =
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)
134 ResLib.writeln_strs out (tfree_clss @ tptp_clss);
140 (*********************************************************************)
141 (* write out a subgoal as DFG clauses to the file "probN" *)
142 (* where N is the number of this subgoal *)
143 (*********************************************************************)
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)
156 (ResLib.writeln_strs out (tfree_clss @ dfg_clss); TextIO.closeOut out; debug probfile
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 =
166 val axfile = (File.platform_path axiom_file)
168 val hypsfile = (File.platform_path hyps_file)
169 val clasimpfile = (File.platform_path clasimp_file)
171 fun make_atp_list [] sign n = []
172 | make_atp_list ((sko_thm, sg_term)::xs) sign n =
174 val thmstr = proofstring (Meson.concat_with_and (map string_of_thm sko_thm))
175 val _ = debug ("thmstring in make_atp_lists is " ^ thmstr)
177 val goalstring = proofstring (Sign.string_of_term sign sg_term)
178 val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
180 val probfile = File.platform_path prob_file ^ "_" ^ (string_of_int n)
181 val _ = debug ("prob file in call_resolve_tac is " ^ probfile)
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.*)
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)))
199 let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vkernel"
201 ([("vampire", thmstr, goalstring, vampire, "-t 60%-m 100000",
202 clasimpfile, axfile, hypsfile, probfile)] @
203 (make_atp_list xs sign (n+1)))
207 val atp_list = make_atp_list (ListPair.zip (thms, sg_terms)) sign 1
209 Watcher.callResProvers(childout,atp_list);
210 debug "Sent commands to watcher!";
214 (**********************************************************)
215 (* write out the current subgoal as a tptp file, probN, *)
216 (* then call dummy_tac - should be call_res_tac *)
217 (**********************************************************)
219 fun get_sko_thms tfrees sign sg_terms (childin, childout, pid) thm n sko_thms =
221 (call_resolve_tac (rev sko_thms)
222 sign sg_terms (childin, childout, pid) (List.length sg_terms);
226 (EVERY1 [rtac ccontr, atomize_tac, skolemize_tac,
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;
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 (**********************************************)
239 fun isar_atp_goal' thm n tfree_lits (childin, childout, pid) =
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
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 []
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 *)
261 (* write to file "hyps" *)
262 (**************************************************)
264 fun isar_atp_aux thms thm n_subgoals (childin, childout, pid) =
265 let val tfree_lits = isar_atp_h thms
267 debug ("in isar_atp_aux");
268 isar_atp_goal' thm n_subgoals tfree_lits (childin, childout, pid)
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 ()
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)
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)
296 string_of_int (Word.toInt (Word.fromLargeWord (Posix.Process.pidToWord pid)))
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);
307 fun get_thms_cs claset =
308 let val {safeEs, safeIs, hazEs, hazIs, ...} = rep_cs claset
309 in safeEs @ safeIs @ hazEs @ hazIs end;
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);
315 fun append_names (name :: names) (thms :: thmss) =
316 append_name name thms 0 :: append_names names thmss;
318 fun get_thms_ss [] = []
321 val names = map Thm.name_of_thm thms
322 val thms' = map (mksimps mksimps_pairs) thms
323 val thms'' = append_names names thms'
325 ResLib.flat_noDup thms''
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*)
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
343 ResLib.writeln_strs out clauses_strs;
344 debug ("axiom file is "^ax_file));
350 fun subtract_simpset thy ctxt =
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;
356 fun subtract_claset thy ctxt =
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;
365 (** the Isar toplevel hook **)
367 val _ = Toplevel.print_state_hook (fn _ => fn state =>
369 val _ = if ! call_atp then () else raise Toplevel.UNDEF;
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;
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));
379 (* FIXME presently unused *)
380 val ss_thms = subtract_simpset thy ctxt;
381 val cs_thms = subtract_claset thy ctxt;
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)