author  paulson 
Mon, 10 Oct 2005 15:35:29 +0200  
changeset 17819  1241e5d31d5b 
parent 17775  2679ba74411f 
child 17845  1438291d57f0 
permissions  rwrr 
15608  1 
(* Author: Jia Meng, Cambridge University Computer Laboratory 
2 
ID: $Id$ 

3 
Copyright 2004 University of Cambridge 

15347  4 

5 
ATPs with TPTP format input. 

6 
*) 

15452  7 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

8 
signature RES_ATP = 
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

9 
sig 
17306  10 
val prover: string ref 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

11 
val custom_spass: string list ref 
17484
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

12 
val destdir: string ref 
17150
ce2a1aeb42aa
DFG output now works for untyped rules (ML "ResClause.untyped();")
quigley
parents:
17091
diff
changeset

13 
val hook_count: int ref 
17484
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

14 
val problem_name: string ref 
17690
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

15 
val time_limit: int ref 
15347  16 
end; 
17 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

18 
structure ResAtp: RES_ATP = 
15347  19 
struct 
20 

17150
ce2a1aeb42aa
DFG output now works for untyped rules (ML "ResClause.untyped();")
quigley
parents:
17091
diff
changeset

21 
val call_atp = ref false; 
ce2a1aeb42aa
DFG output now works for untyped rules (ML "ResClause.untyped();")
quigley
parents:
17091
diff
changeset

22 
val hook_count = ref 0; 
17690
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

23 
val time_limit = ref 60; 
17150
ce2a1aeb42aa
DFG output now works for untyped rules (ML "ResClause.untyped();")
quigley
parents:
17091
diff
changeset

24 

17404
d16c3a62c396
the experimental tagging system, and the usual tidying
paulson
parents:
17317
diff
changeset

25 
val prover = ref "E"; (* use E as the default prover *) 
17305
6cef3aedd661
axioms now included in tptp files, no /bin/cat and various tidying
paulson
parents:
17235
diff
changeset

26 
val custom_spass = (*specialized options for SPASS*) 
17690
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

27 
ref ["Auto=0","FullRed=0","IORe","IOFc","RTaut","RFSub","RBSub"]; 
15347  28 

17484
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

29 
val destdir = ref ""; (*Empty means write files to /tmp*) 
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

30 
val problem_name = ref "prob"; 
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

31 

17819  32 
(*Return the path to a "helper" like SPASS or tptp2X, first checking that 
33 
it exists. FIXME: modify to use Path primitives and move to some central place.*) 

34 
fun helper_path evar base = 

35 
case getenv evar of 

36 
"" => error ("Isabelle environment variable " ^ evar ^ " not defined") 

37 
 home => 

38 
let val path = home ^ "/" ^ base 

39 
in if File.exists (File.unpack_platform_path path) then path 

40 
else error ("Could not find the file " ^ path) 

41 
end; 

42 

17717  43 
fun probfile_nosuffix _ = 
17484
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

44 
if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name))) 
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

45 
else if File.exists (File.unpack_platform_path (!destdir)) 
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

46 
then !destdir ^ "/" ^ !problem_name 
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

47 
else error ("No such directory: " ^ !destdir); 
15644  48 

17717  49 
fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n; 
50 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

51 

15347  52 
(**** For running in Isar ****) 
53 

15608  54 
(* same function as that in res_axioms.ML *) 
55 
fun repeat_RS thm1 thm2 = 

56 
let val thm1' = thm1 RS thm2 handle THM _ => thm1 

57 
in 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

58 
if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2) 
15608  59 
end; 
60 

61 
(* a special version of repeat_RS *) 

17717  62 
fun repeat_someI_ex th = repeat_RS th someI_ex; 
15608  63 

17764  64 
fun writeln_strs _ [] = () 
65 
 writeln_strs out (s::ss) = (TextIO.output (out, s); TextIO.output (out, "\n"); writeln_strs out ss); 

16925
0fd7b1438d28
simpler variable names, and no types for monomorphic constants
paulson
parents:
16904
diff
changeset

66 

17502  67 
(* write out a subgoal as tptp clauses to the file "xxxx_N"*) 
17717  68 
fun tptp_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) = 
17775  69 
let 
70 
val clss = map (ResClause.make_conjecture_clause_thm) thms 

71 
val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss) 

72 
val tfree_clss = map ResClause.tfree_clause (foldl (op union_string) [] tfree_litss) 

73 
val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses 

74 
val arity_cls = map ResClause.tptp_arity_clause arity_clauses 

75 
val out = TextIO.openOut(pf n) 

76 
in 

77 
writeln_strs out (List.concat (map ResClause.tptp_clause axclauses)); 

78 
writeln_strs out (tfree_clss @ tptp_clss @ classrel_cls @ arity_cls); 

79 
TextIO.closeOut out 

80 
end; 

15452  81 

17502  82 
(* write out a subgoal in DFG format to the file "xxxx_N"*) 
17717  83 
fun dfg_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) = 
17150
ce2a1aeb42aa
DFG output now works for untyped rules (ML "ResClause.untyped();")
quigley
parents:
17091
diff
changeset

84 
let val clss = map (ResClause.make_conjecture_clause_thm) thms 
17525
ae5bb6001afb
tidying, and support for axclass/classrel clauses
paulson
parents:
17502
diff
changeset

85 
(*FIXME: classrel_clauses and arity_clauses*) 
ae5bb6001afb
tidying, and support for axclass/classrel clauses
paulson
parents:
17502
diff
changeset

86 
val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ Int.toString n) 
17422  87 
axclauses [] [] [] 
17717  88 
val out = TextIO.openOut(pf n) 
16767  89 
in 
17764  90 
writeln_strs out [probN]; TextIO.closeOut out 
17150
ce2a1aeb42aa
DFG output now works for untyped rules (ML "ResClause.untyped();")
quigley
parents:
17091
diff
changeset

91 
end; 
ce2a1aeb42aa
DFG output now works for untyped rules (ML "ResClause.untyped();")
quigley
parents:
17091
diff
changeset

92 

16357  93 

15644  94 
(*********************************************************************) 
17306  95 
(* call prover with settings and problem file for the current subgoal *) 
15644  96 
(*********************************************************************) 
16357  97 
(* now passing in list of skolemized thms and list of sgterms to go with them *) 
17764  98 
fun watcher_call_provers sign sg_terms (childin, childout, pid) = 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

99 
let 
17422  100 
fun make_atp_list [] n = [] 
17717  101 
 make_atp_list (sg_term::xs) n = 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

102 
let 
17717  103 
val probfile = prob_pathname n 
17690
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

104 
val time = Int.toString (!time_limit) 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

105 
in 
17764  106 
debug ("problem file in watcher_call_provers is " ^ probfile); 
17317
3f12de2e2e6e
IsabelleATP link: sortable axiom names; no spaces in switches; general tidying
paulson
parents:
17306
diff
changeset

107 
(*Avoid command arguments containing spaces: Poly/ML and SML/NJ 
3f12de2e2e6e
IsabelleATP link: sortable axiom names; no spaces in switches; general tidying
paulson
parents:
17306
diff
changeset

108 
versions of Unix.execute treat them differently!*) 
17764  109 
(*options are separated by Watcher.setting_sep, currently #"%"*) 
17306  110 
if !prover = "spass" 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

111 
then 
17306  112 
let val optionline = 
17484
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

113 
if !AtpCommunication.reconstruct 
17306  114 
(*Proof reconstruction works for only a limited set of 
115 
inference rules*) 

17690
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

116 
then space_implode "%" (!custom_spass) ^ 
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

117 
"%DocProof%TimeLimit=" ^ time 
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

118 
else "DocProof%SOS%FullRed=0%TimeLimit=" ^ time (*Auto mode*) 
16904  119 
val _ = debug ("SPASS option string is " ^ optionline) 
17819  120 
val _ = helper_path "SPASS_HOME" "SPASS" 
16897  121 
(*We've checked that SPASS is there for ATP/spassshell to run.*) 
122 
in 

17772
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

123 
([("spass", 
16897  124 
getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell", 
17422  125 
optionline, probfile)] @ 
126 
(make_atp_list xs (n+1))) 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

127 
end 
17306  128 
else if !prover = "vampire" 
17235
8e55ad29b690
Added ECommunication.ML and modified res_atp.ML, Reconstruction.thy, and
quigley
parents:
17234
diff
changeset

129 
then 
17819  130 
let val vampire = helper_path "VAMPIRE_HOME" "vampire" 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

131 
in 
17772
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

132 
([("vampire", vampire, "m 100000%t " ^ time, probfile)] @ 
17435  133 
(make_atp_list xs (n+1))) (*BEWARE! spaces in options!*) 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

134 
end 
17306  135 
else if !prover = "E" 
136 
then 

17819  137 
let val Eprover = helper_path "E_HOME" "eproof" 
17306  138 
in 
17772
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

139 
([("E", Eprover, 
17690
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

140 
"tptpin%l5%xAuto%tAuto%cpulimit=" ^ time, 
17422  141 
probfile)] @ 
142 
(make_atp_list xs (n+1))) 

17306  143 
end 
144 
else error ("Invalid prover name: " ^ !prover) 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

145 
end 
15452  146 

17422  147 
val atp_list = make_atp_list sg_terms 1 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

148 
in 
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

149 
Watcher.callResProvers(childout,atp_list); 
17422  150 
debug "Sent commands to watcher!" 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

151 
end 
16357  152 

17717  153 
(*We write out problem files for each subgoal. Argument pf generates filenames, 
154 
and allows the suppression of the suffix "_1" in problemgeneration 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 = 

17772
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

168 
if n=0 then [] 
17717  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 
all_tac))]) n th; 

17772
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

176 
pf n :: writenext (n1)) 
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

177 
in (writenext (length prems), clause_arr) end; 
15644  178 

17775  179 
val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
180 
Posix.Process.pid * string list) option); 

181 

182 
fun kill_last_watcher () = 

183 
(case !last_watcher_pid of 

184 
NONE => () 

185 
 SOME (_, childout, pid, files) => 

17819  186 
(debug ("Killing old watcher, pid = " ^ string_of_pid pid); 
17775  187 
Watcher.killWatcher pid; 
188 
ignore (map (try OS.FileSys.remove) files))) 

189 
handle OS.SysErr _ => debug "Attempt to kill watcher failed"; 

17525
ae5bb6001afb
tidying, and support for axclass/classrel clauses
paulson
parents:
17502
diff
changeset

190 

ae5bb6001afb
tidying, and support for axclass/classrel clauses
paulson
parents:
17502
diff
changeset

191 
(*writes out the current clasimpset to a tptp file; 
ae5bb6001afb
tidying, and support for axclass/classrel clauses
paulson
parents:
17502
diff
changeset

192 
turns off xsymbol at start of function, restoring it at end *) 
17484
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

193 
val isar_atp = setmp print_mode [] 
17717  194 
(fn (ctxt, th) => 
195 
if Thm.no_prems th then () 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

196 
else 
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

197 
let 
17775  198 
val _ = kill_last_watcher() 
17772
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

199 
val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th) 
17717  200 
val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr) 
15608  201 
in 
17772
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

202 
last_watcher_pid := SOME (childin, childout, pid, files); 
818cec5f82a4
major simplification: removal of the goalstring argument
paulson
parents:
17764
diff
changeset

203 
debug ("problem files: " ^ space_implode ", " files); 
17819  204 
debug ("pid: " ^ string_of_pid pid); 
17717  205 
watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid) 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

206 
end); 
15608  207 

17422  208 
val isar_atp_writeonly = setmp print_mode [] 
17717  209 
(fn (ctxt,th) => 
210 
if Thm.no_prems th then () 

211 
else 

212 
let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 

213 
else prob_pathname 

214 
in ignore (write_problem_files pf (ctxt,th)) end); 

15452  215 

16357  216 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

217 
(** the Isar toplevel hook **) 
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

218 

17091  219 
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state => 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

220 
let 
17091  221 
val proof = Toplevel.proof_of state 
222 
val (ctxt, (_, goal)) = Proof.get_goal proof 

223 
handle Proof.STATE _ => error "No goal present"; 

16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

224 
val thy = ProofContext.theory_of ctxt; 
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

225 
in 
17764  226 
debug ("subgoals in isar_atp:\n" ^ 
17091  227 
Pretty.string_of (ProofContext.pretty_term ctxt 
228 
(Logic.mk_conjunction_list (Thm.prems_of goal)))); 

17525
ae5bb6001afb
tidying, and support for axclass/classrel clauses
paulson
parents:
17502
diff
changeset

229 
debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal)); 
17484
f6a225f97f0a
simplification of the IsabelleATP code; hooks for batch generation of problems
paulson
parents:
17435
diff
changeset

230 
debug ("current theory: " ^ Context.theory_name thy); 
17150
ce2a1aeb42aa
DFG output now works for untyped rules (ML "ResClause.untyped();")
quigley
parents:
17091
diff
changeset

231 
hook_count := !hook_count +1; 
17717  232 
debug ("in hook for time: " ^ Int.toString (!hook_count)); 
16925
0fd7b1438d28
simpler variable names, and no types for monomorphic constants
paulson
parents:
16904
diff
changeset

233 
ResClause.init thy; 
17690
8ba7c3cd24a8
time limit option; fixed bug concerning first line of ATP output
paulson
parents:
17525
diff
changeset

234 
if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal) 
17502  235 
else isar_atp_writeonly (ctxt, goal) 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

236 
end); 
16357  237 

17091  238 
val call_atpP = 
17746  239 
OuterSyntax.command 
17091  240 
"ProofGeneral.call_atp" 
241 
"call automatic theorem provers" 

242 
OuterKeyword.diag 

243 
(Scan.succeed (Toplevel.no_timing o invoke_atp)); 

244 

245 
val _ = OuterSyntax.add_parsers [call_atpP]; 

246 

15347  247 
end; 