39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* METIS FIRST ORDER PROVER *)
|
|
3 |
(* *)
|
|
4 |
(* Copyright (c) 2001 Joe Hurd *)
|
|
5 |
(* *)
|
|
6 |
(* Metis is free software; you can redistribute it and/or modify *)
|
|
7 |
(* it under the terms of the GNU General Public License as published by *)
|
|
8 |
(* the Free Software Foundation; either version 2 of the License, or *)
|
|
9 |
(* (at your option) any later version. *)
|
|
10 |
(* *)
|
|
11 |
(* Metis is distributed in the hope that it will be useful, *)
|
|
12 |
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
13 |
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
|
|
14 |
(* GNU General Public License for more details. *)
|
|
15 |
(* *)
|
|
16 |
(* You should have received a copy of the GNU General Public License *)
|
|
17 |
(* along with Metis; if not, write to the Free Software *)
|
|
18 |
(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *)
|
|
19 |
(* ========================================================================= *)
|
|
20 |
|
|
21 |
open Useful;
|
|
22 |
|
|
23 |
(* ------------------------------------------------------------------------- *)
|
|
24 |
(* The program name and version. *)
|
|
25 |
(* ------------------------------------------------------------------------- *)
|
|
26 |
|
|
27 |
val PROGRAM = "metis";
|
|
28 |
|
|
29 |
val VERSION = "2.2";
|
|
30 |
|
|
31 |
val versionString = PROGRAM^" "^VERSION^" (release 20100825)"^"\n";
|
|
32 |
|
|
33 |
(* ------------------------------------------------------------------------- *)
|
|
34 |
(* Program options. *)
|
|
35 |
(* ------------------------------------------------------------------------- *)
|
|
36 |
|
|
37 |
val QUIET = ref false;
|
|
38 |
|
|
39 |
val TEST = ref false;
|
|
40 |
|
|
41 |
val TPTP : string option ref = ref NONE;
|
|
42 |
|
|
43 |
val ITEMS = ["name","goal","clauses","size","category","proof","saturation"];
|
|
44 |
|
|
45 |
val extended_items = "all" :: ITEMS;
|
|
46 |
|
|
47 |
val show_items = map (fn s => (s, ref false)) ITEMS;
|
|
48 |
|
|
49 |
fun show_ref s =
|
|
50 |
case List.find (equal s o fst) show_items of
|
|
51 |
NONE => raise Bug ("item " ^ s ^ " not found")
|
|
52 |
| SOME (_,r) => r;
|
|
53 |
|
|
54 |
fun show_set b = app (fn (_,r) => r := b) show_items;
|
|
55 |
|
|
56 |
fun showing s = not (!QUIET) andalso (s = "status" orelse !(show_ref s));
|
|
57 |
|
|
58 |
fun notshowing s = not (showing s);
|
|
59 |
|
|
60 |
fun showing_any () = List.exists showing ITEMS;
|
|
61 |
|
|
62 |
fun notshowing_any () = not (showing_any ());
|
|
63 |
|
|
64 |
fun show "all" = show_set true
|
|
65 |
| show s = case show_ref s of r => r := true;
|
|
66 |
|
|
67 |
fun hide "all" = show_set false
|
|
68 |
| hide s = case show_ref s of r => r := false;
|
|
69 |
|
|
70 |
(* ------------------------------------------------------------------------- *)
|
|
71 |
(* Process command line arguments and environment variables. *)
|
|
72 |
(* ------------------------------------------------------------------------- *)
|
|
73 |
|
|
74 |
local
|
|
75 |
open Useful Options;
|
|
76 |
in
|
|
77 |
val specialOptions =
|
|
78 |
[{switches = ["--show"], arguments = ["ITEM"],
|
|
79 |
description = "show ITEM (see below for list)",
|
|
80 |
processor =
|
|
81 |
beginOpt (enumOpt extended_items endOpt) (fn _ => fn s => show s)},
|
|
82 |
{switches = ["--hide"], arguments = ["ITEM"],
|
|
83 |
description = "hide ITEM (see below for list)",
|
|
84 |
processor =
|
|
85 |
beginOpt (enumOpt extended_items endOpt) (fn _ => fn s => hide s)},
|
|
86 |
{switches = ["--tptp"], arguments = ["DIR"],
|
|
87 |
description = "specify the TPTP installation directory",
|
|
88 |
processor =
|
|
89 |
beginOpt (stringOpt endOpt) (fn _ => fn s => TPTP := SOME s)},
|
|
90 |
{switches = ["-q","--quiet"], arguments = [],
|
|
91 |
description = "Run quietly; indicate provability with return value",
|
|
92 |
processor = beginOpt endOpt (fn _ => QUIET := true)},
|
|
93 |
{switches = ["--test"], arguments = [],
|
|
94 |
description = "Skip the proof search for the input problems",
|
|
95 |
processor = beginOpt endOpt (fn _ => TEST := true)}];
|
|
96 |
end;
|
|
97 |
|
|
98 |
val programOptions =
|
|
99 |
{name = PROGRAM,
|
|
100 |
version = versionString,
|
|
101 |
header = "usage: "^PROGRAM^" [option ...] problem.tptp ...\n" ^
|
|
102 |
"Proves the input TPTP problem files.\n",
|
|
103 |
footer = "Possible ITEMs are {" ^ join "," extended_items ^ "}.\n" ^
|
|
104 |
"Problems can be read from standard input using the " ^
|
|
105 |
"special - filename.\n",
|
|
106 |
options = specialOptions @ Options.basicOptions};
|
|
107 |
|
|
108 |
fun exit x : unit = Options.exit programOptions x;
|
|
109 |
fun succeed () = Options.succeed programOptions;
|
|
110 |
fun fail mesg = Options.fail programOptions mesg;
|
|
111 |
fun usage mesg = Options.usage programOptions mesg;
|
|
112 |
|
|
113 |
fun processOptions () =
|
|
114 |
let
|
|
115 |
val args = CommandLine.arguments ()
|
|
116 |
|
|
117 |
val (_,work) = Options.processOptions programOptions args
|
|
118 |
|
|
119 |
val () =
|
|
120 |
case !TPTP of
|
|
121 |
SOME _ => ()
|
|
122 |
| NONE => TPTP := OS.Process.getEnv "TPTP"
|
|
123 |
in
|
|
124 |
work
|
|
125 |
end;
|
|
126 |
|
|
127 |
(* ------------------------------------------------------------------------- *)
|
|
128 |
(* The core application. *)
|
|
129 |
(* ------------------------------------------------------------------------- *)
|
|
130 |
|
|
131 |
(*MetisDebug
|
|
132 |
val next_cnf =
|
|
133 |
let
|
|
134 |
val cnf_counter = ref 0
|
|
135 |
in
|
|
136 |
fn () =>
|
|
137 |
let
|
|
138 |
val ref cnf_count = cnf_counter
|
|
139 |
val () = cnf_counter := cnf_count + 1
|
|
140 |
in
|
|
141 |
cnf_count
|
|
142 |
end
|
|
143 |
end;
|
|
144 |
*)
|
|
145 |
|
|
146 |
local
|
|
147 |
fun display_sep () =
|
|
148 |
if notshowing_any () then ()
|
|
149 |
else print (nChars #"-" (!Print.lineLength) ^ "\n");
|
|
150 |
|
|
151 |
fun display_name filename =
|
|
152 |
if notshowing "name" then ()
|
|
153 |
else print ("Problem: " ^ filename ^ "\n\n");
|
|
154 |
|
|
155 |
fun display_goal tptp =
|
|
156 |
if notshowing "goal" then ()
|
|
157 |
else
|
|
158 |
let
|
|
159 |
val goal = Tptp.goal tptp
|
|
160 |
in
|
|
161 |
print ("Goal:\n" ^ Formula.toString goal ^ "\n\n")
|
|
162 |
end;
|
|
163 |
|
|
164 |
fun display_clauses cls =
|
|
165 |
if notshowing "clauses" then ()
|
|
166 |
else print ("Clauses:\n" ^ Problem.toString cls ^ "\n\n");
|
|
167 |
|
|
168 |
fun display_size cls =
|
|
169 |
if notshowing "size" then ()
|
|
170 |
else
|
|
171 |
let
|
|
172 |
fun plural 1 s = "1 " ^ s
|
|
173 |
| plural n s = Int.toString n ^ " " ^ s ^ "s"
|
|
174 |
|
|
175 |
val {clauses,literals,symbols,typedSymbols} = Problem.size cls
|
|
176 |
in
|
|
177 |
print
|
|
178 |
("Size: " ^
|
|
179 |
plural clauses "clause" ^ ", " ^
|
|
180 |
plural literals "literal" ^ ", " ^
|
|
181 |
plural symbols "symbol" ^ ", " ^
|
|
182 |
plural typedSymbols "typed symbol" ^ ".\n\n")
|
|
183 |
end;
|
|
184 |
|
|
185 |
fun display_category cls =
|
|
186 |
if notshowing "category" then ()
|
|
187 |
else
|
|
188 |
let
|
|
189 |
val cat = Problem.categorize cls
|
|
190 |
in
|
|
191 |
print ("Category: " ^ Problem.categoryToString cat ^ ".\n\n")
|
|
192 |
end;
|
|
193 |
|
|
194 |
local
|
|
195 |
fun display_proof_start filename =
|
|
196 |
print ("\nSZS output start CNFRefutation for " ^ filename ^ "\n");
|
|
197 |
|
|
198 |
fun display_proof_body problem proofs =
|
|
199 |
let
|
|
200 |
val comments = []
|
|
201 |
|
|
202 |
val includes = []
|
|
203 |
|
|
204 |
val formulas =
|
|
205 |
Tptp.fromProof
|
|
206 |
{problem = problem,
|
|
207 |
proofs = proofs}
|
|
208 |
|
|
209 |
val proof =
|
|
210 |
Tptp.Problem
|
|
211 |
{comments = comments,
|
|
212 |
includes = includes,
|
|
213 |
formulas = formulas}
|
|
214 |
|
|
215 |
val mapping = Tptp.defaultMapping
|
|
216 |
val mapping = Tptp.addVarSetMapping mapping (Tptp.freeVars proof)
|
|
217 |
|
|
218 |
val filename = "-"
|
|
219 |
in
|
|
220 |
Tptp.write
|
|
221 |
{problem = proof,
|
|
222 |
mapping = mapping,
|
|
223 |
filename = filename}
|
|
224 |
end;
|
|
225 |
|
|
226 |
fun display_proof_end filename =
|
|
227 |
print ("SZS output end CNFRefutation for " ^ filename ^ "\n\n");
|
|
228 |
in
|
|
229 |
fun display_proof filename problem proofs =
|
|
230 |
if notshowing "proof" then ()
|
|
231 |
else
|
|
232 |
let
|
|
233 |
val () = display_proof_start filename
|
|
234 |
val () = display_proof_body problem proofs
|
|
235 |
val () = display_proof_end filename
|
|
236 |
in
|
|
237 |
()
|
|
238 |
end;
|
|
239 |
end;
|
|
240 |
|
|
241 |
fun display_saturation filename ths =
|
|
242 |
if notshowing "saturation" then ()
|
|
243 |
else
|
|
244 |
let
|
|
245 |
(*MetisDebug
|
|
246 |
val () =
|
|
247 |
let
|
|
248 |
val problem =
|
|
249 |
Tptp.mkProblem
|
|
250 |
{comments = ["Saturation clause set for " ^ filename],
|
|
251 |
includes = [],
|
|
252 |
names = Tptp.noClauseNames,
|
|
253 |
roles = Tptp.noClauseRoles,
|
|
254 |
problem = {axioms = [],
|
|
255 |
conjecture = map Thm.clause ths}}
|
|
256 |
|
|
257 |
val mapping =
|
|
258 |
Tptp.addVarSetMapping Tptp.defaultMapping
|
|
259 |
(Tptp.freeVars problem)
|
|
260 |
in
|
|
261 |
Tptp.write
|
|
262 |
{problem = problem,
|
|
263 |
mapping = mapping,
|
|
264 |
filename = "saturation.tptp"}
|
|
265 |
end
|
|
266 |
*)
|
|
267 |
val () = print ("\nSZS output start Saturation for " ^ filename ^ "\n")
|
|
268 |
val () = app (fn th => print (Thm.toString th ^ "\n")) ths
|
|
269 |
val () = print ("SZS output end Saturation for " ^ filename ^ "\n\n")
|
|
270 |
in
|
|
271 |
()
|
|
272 |
end;
|
|
273 |
|
|
274 |
fun display_status filename status =
|
|
275 |
if notshowing "status" then ()
|
|
276 |
else print ("SZS status " ^ Tptp.toStringStatus status ^
|
|
277 |
" for " ^ filename ^ "\n");
|
|
278 |
|
|
279 |
fun display_problem filename cls =
|
|
280 |
let
|
|
281 |
(*MetisDebug
|
|
282 |
val () =
|
|
283 |
let
|
|
284 |
val problem =
|
|
285 |
Tptp.mkProblem
|
|
286 |
{comments = ["CNF clauses for " ^ filename],
|
|
287 |
includes = [],
|
|
288 |
names = Tptp.noClauseNames,
|
|
289 |
roles = Tptp.noClauseRoles,
|
|
290 |
problem = cls}
|
|
291 |
|
|
292 |
val mapping =
|
|
293 |
Tptp.addVarSetMapping Tptp.defaultMapping
|
|
294 |
(Tptp.freeVars problem)
|
|
295 |
|
|
296 |
val filename = "cnf_" ^ Int.toString (next_cnf ()) ^ ".tptp"
|
|
297 |
in
|
|
298 |
Tptp.write
|
|
299 |
{problem = problem,
|
|
300 |
mapping = mapping,
|
|
301 |
filename = filename}
|
|
302 |
end
|
|
303 |
*)
|
|
304 |
val () = display_clauses cls
|
|
305 |
val () = display_size cls
|
|
306 |
val () = display_category cls
|
|
307 |
in
|
|
308 |
()
|
|
309 |
end;
|
|
310 |
|
|
311 |
fun mkTptpFilename filename =
|
|
312 |
case !TPTP of
|
|
313 |
NONE => filename
|
|
314 |
| SOME tptp =>
|
|
315 |
let
|
|
316 |
val tptp = stripSuffix (equal #"/") tptp
|
|
317 |
in
|
|
318 |
tptp ^ "/" ^ filename
|
|
319 |
end;
|
|
320 |
|
|
321 |
fun readIncludes mapping seen formulas includes =
|
|
322 |
case includes of
|
|
323 |
[] => formulas
|
|
324 |
| inc :: includes =>
|
|
325 |
if StringSet.member inc seen then
|
|
326 |
readIncludes mapping seen formulas includes
|
|
327 |
else
|
|
328 |
let
|
|
329 |
val seen = StringSet.add seen inc
|
|
330 |
|
|
331 |
val filename = mkTptpFilename inc
|
|
332 |
|
|
333 |
val Tptp.Problem {includes = i, formulas = f, ...} =
|
|
334 |
Tptp.read {filename = filename, mapping = mapping}
|
|
335 |
|
|
336 |
val formulas = f @ formulas
|
|
337 |
|
|
338 |
val includes = List.revAppend (i,includes)
|
|
339 |
in
|
|
340 |
readIncludes mapping seen formulas includes
|
|
341 |
end;
|
|
342 |
|
|
343 |
fun read mapping filename =
|
|
344 |
let
|
|
345 |
val problem = Tptp.read {filename = filename, mapping = mapping}
|
|
346 |
|
|
347 |
val Tptp.Problem {comments,includes,formulas} = problem
|
|
348 |
in
|
|
349 |
if null includes then problem
|
|
350 |
else
|
|
351 |
let
|
|
352 |
val seen = StringSet.empty
|
|
353 |
|
|
354 |
val includes = rev includes
|
|
355 |
|
|
356 |
val formulas = readIncludes mapping seen formulas includes
|
|
357 |
in
|
|
358 |
Tptp.Problem
|
|
359 |
{comments = comments,
|
|
360 |
includes = [],
|
|
361 |
formulas = formulas}
|
|
362 |
end
|
|
363 |
end;
|
|
364 |
|
|
365 |
val resolutionParameters =
|
|
366 |
let
|
|
367 |
val {active,
|
|
368 |
waiting} = Resolution.default
|
|
369 |
|
|
370 |
val waiting =
|
|
371 |
let
|
|
372 |
val {symbolsWeight,
|
|
373 |
variablesWeight,
|
|
374 |
literalsWeight,
|
|
375 |
models} = waiting
|
|
376 |
|
|
377 |
val models =
|
|
378 |
case models of
|
|
379 |
[{model = _,
|
|
380 |
initialPerturbations,
|
|
381 |
maxChecks,
|
|
382 |
perturbations,
|
|
383 |
weight}] =>
|
|
384 |
let
|
|
385 |
val model = Tptp.defaultModel
|
|
386 |
in
|
|
387 |
[{model = model,
|
|
388 |
initialPerturbations = initialPerturbations,
|
|
389 |
maxChecks = maxChecks,
|
|
390 |
perturbations = perturbations,
|
|
391 |
weight = weight}]
|
|
392 |
end
|
|
393 |
| _ => raise Bug "resolutionParameters.waiting.models"
|
|
394 |
in
|
|
395 |
{symbolsWeight = symbolsWeight,
|
|
396 |
variablesWeight = variablesWeight,
|
|
397 |
literalsWeight = literalsWeight,
|
|
398 |
models = models}
|
|
399 |
end
|
|
400 |
in
|
|
401 |
{active = active,
|
|
402 |
waiting = waiting}
|
|
403 |
end;
|
|
404 |
|
|
405 |
fun refute {axioms,conjecture} =
|
|
406 |
let
|
|
407 |
val axioms = map Thm.axiom axioms
|
|
408 |
and conjecture = map Thm.axiom conjecture
|
|
409 |
val problem = {axioms = axioms, conjecture = conjecture}
|
|
410 |
val resolution = Resolution.new resolutionParameters problem
|
|
411 |
in
|
|
412 |
Resolution.loop resolution
|
|
413 |
end;
|
|
414 |
|
|
415 |
fun refuteAll filename tptp probs acc =
|
|
416 |
case probs of
|
|
417 |
[] =>
|
|
418 |
let
|
|
419 |
val status =
|
|
420 |
if !TEST then Tptp.UnknownStatus
|
|
421 |
else if Tptp.hasFofConjecture tptp then Tptp.TheoremStatus
|
|
422 |
else Tptp.UnsatisfiableStatus
|
|
423 |
|
|
424 |
val () = display_status filename status
|
|
425 |
|
|
426 |
val () =
|
|
427 |
if !TEST then ()
|
|
428 |
else display_proof filename tptp (rev acc)
|
|
429 |
in
|
|
430 |
true
|
|
431 |
end
|
|
432 |
| prob :: probs =>
|
|
433 |
let
|
|
434 |
val {subgoal,problem,sources} = prob
|
|
435 |
|
|
436 |
val () = display_problem filename problem
|
|
437 |
in
|
|
438 |
if !TEST then refuteAll filename tptp probs acc
|
|
439 |
else
|
|
440 |
case refute problem of
|
|
441 |
Resolution.Contradiction th =>
|
|
442 |
let
|
|
443 |
val subgoalProof =
|
|
444 |
{subgoal = subgoal,
|
|
445 |
sources = sources,
|
|
446 |
refutation = th}
|
|
447 |
|
|
448 |
val acc = subgoalProof :: acc
|
|
449 |
in
|
|
450 |
refuteAll filename tptp probs acc
|
|
451 |
end
|
|
452 |
| Resolution.Satisfiable ths =>
|
|
453 |
let
|
|
454 |
val status =
|
|
455 |
if Tptp.hasFofConjecture tptp then
|
|
456 |
Tptp.CounterSatisfiableStatus
|
|
457 |
else
|
|
458 |
Tptp.SatisfiableStatus
|
|
459 |
|
|
460 |
val () = display_status filename status
|
|
461 |
val () = display_saturation filename ths
|
|
462 |
in
|
|
463 |
false
|
|
464 |
end
|
|
465 |
end;
|
|
466 |
in
|
|
467 |
fun prove mapping filename =
|
|
468 |
let
|
|
469 |
val () = display_sep ()
|
|
470 |
val () = display_name filename
|
|
471 |
val tptp = read mapping filename
|
|
472 |
val () = display_goal tptp
|
|
473 |
val problems = Tptp.normalize tptp
|
|
474 |
in
|
|
475 |
refuteAll filename tptp problems []
|
|
476 |
end;
|
|
477 |
|
|
478 |
fun proveAll mapping filenames =
|
|
479 |
List.all
|
|
480 |
(if !QUIET then prove mapping
|
|
481 |
else fn filename => prove mapping filename orelse true)
|
|
482 |
filenames;
|
|
483 |
end;
|
|
484 |
|
|
485 |
(* ------------------------------------------------------------------------- *)
|
|
486 |
(* Top level. *)
|
|
487 |
(* ------------------------------------------------------------------------- *)
|
|
488 |
|
|
489 |
val () =
|
|
490 |
let
|
|
491 |
val work = processOptions ()
|
|
492 |
|
|
493 |
val () = if null work then usage "no input problem files" else ()
|
|
494 |
|
|
495 |
val mapping = Tptp.defaultMapping
|
|
496 |
|
|
497 |
val success = proveAll mapping work
|
|
498 |
in
|
|
499 |
exit {message = NONE, usage = false, success = success}
|
|
500 |
end
|
|
501 |
handle Error s => die (PROGRAM^" failed:\n" ^ s)
|
|
502 |
| Bug s => die ("BUG found in "^PROGRAM^" program:\n" ^ s);
|