1 (* Title: HOL/Tools/ATP/atp_proof.ML
2 Author: Lawrence C. Paulson, Cambridge University Computer Laboratory
3 Author: Claire Quigley, Cambridge University Computer Laboratory
4 Author: Jasmin Blanchette, TU Muenchen
6 Abstract representation of ATP proofs and TSTP/SPASS syntax.
11 type ('a, 'b) ho_term = ('a, 'b) ATP_Problem.ho_term
12 type ('a, 'b, 'c) formula = ('a, 'b, 'c) ATP_Problem.formula
13 type 'a problem = 'a ATP_Problem.problem
15 exception UNRECOGNIZED_ATP_PROOF of unit
22 UnsoundProof of bool * string list |
35 UnknownError of string
37 type step_name = string * string list
40 Definition_Step of step_name * 'a * 'a |
41 Inference_Step of step_name * 'a * string * step_name list
43 type 'a proof = ('a, 'a, ('a, 'a) ho_term) formula step list
45 val short_output : bool -> string -> string
46 val string_for_failure : failure -> string
47 val extract_important_message : string -> string
48 val extract_known_failure :
49 (failure * string) list -> string -> failure option
50 val extract_tstplike_proof_and_outcome :
51 bool -> bool -> (string * string) list -> (failure * string) list -> string
52 -> string * failure option
53 val is_same_atp_step : step_name -> step_name -> bool
54 val scan_general_id : string list -> string * string list
55 val satallax_unsat_coreN : string
58 -> (string, 'a, (string, 'a) ho_term) formula * string list
59 val atp_proof_from_tstplike_proof : string problem -> string -> string proof
60 val clean_up_atp_proof_dependencies : string proof -> string proof
61 val map_term_names_in_atp_proof :
62 (string -> string) -> string proof -> string proof
63 val nasty_atp_proof : string Symtab.table -> string proof -> string proof
66 structure ATP_Proof : ATP_PROOF =
72 exception UNRECOGNIZED_ATP_PROOF of unit
79 UnsoundProof of bool * string list |
92 UnknownError of string
94 fun elide_string threshold s =
95 if size s > threshold then
96 String.extract (s, 0, SOME (threshold div 2 - 5)) ^ " ...... " ^
97 String.extract (s, size s - (threshold + 1) div 2 + 6, NONE)
100 fun short_output verbose output =
102 if output = "" then "No details available" else elide_string 1000 output
106 val missing_message_tail =
107 " appears to be missing. You will need to install it if you want to invoke \
110 fun involving [] = ""
112 "involving " ^ space_implode " " (Try.serial_commas "and" (map quote ss)) ^
115 fun string_for_failure Unprovable = "The generated problem is unprovable."
116 | string_for_failure GaveUp = "The prover gave up."
117 | string_for_failure ProofMissing =
118 "The prover claims the conjecture is a theorem but did not provide a proof."
119 | string_for_failure ProofIncomplete =
120 "The prover claims the conjecture is a theorem but provided an incomplete \
121 \(or unparsable) proof."
122 | string_for_failure (UnsoundProof (false, ss)) =
123 "The prover found a type-unsound proof " ^ involving ss ^
124 "(or, less likely, your axioms are inconsistent). Specify a sound type \
125 \encoding or omit the \"type_enc\" option."
126 | string_for_failure (UnsoundProof (true, ss)) =
127 "The prover found a type-unsound proof " ^ involving ss ^
128 "even though a supposedly type-sound encoding was used (or, less likely, \
129 \your axioms are inconsistent). Please report this to the Isabelle \
131 | string_for_failure CantConnect = "Cannot connect to remote server."
132 | string_for_failure TimedOut = "Timed out."
133 | string_for_failure Inappropriate =
134 "The generated problem lies outside the prover's scope."
135 | string_for_failure OutOfResources = "The prover ran out of resources."
136 | string_for_failure OldSPASS =
137 "The version of SPASS you are using is obsolete. Please upgrade to \
138 \SPASS 3.8ds. To install it, download and extract the package \
139 \\"http://www21.in.tum.de/~blanchet/spass-3.8ds.tar.gz\" and add the \
140 \\"spass-3.8ds\" directory's absolute path to " ^
141 quote (Path.implode (Path.expand (Path.appends
142 (Path.variable "ISABELLE_HOME_USER" ::
143 map Path.basic ["etc", "components"])))) ^
144 " on a line of its own."
145 | string_for_failure NoPerl = "Perl" ^ missing_message_tail
146 | string_for_failure NoLibwwwPerl =
147 "The Perl module \"libwww-perl\"" ^ missing_message_tail
148 | string_for_failure MalformedInput =
149 "The generated problem is malformed. Please report this to the Isabelle \
151 | string_for_failure MalformedOutput = "The prover output is malformed."
152 | string_for_failure Interrupted = "The prover was interrupted."
153 | string_for_failure Crashed = "The prover crashed."
154 | string_for_failure InternalError = "An internal prover error occurred."
155 | string_for_failure (UnknownError string) =
156 "A prover error occurred" ^
157 (if string = "" then ". (Pass the \"verbose\" option for details.)"
160 fun extract_delimited (begin_delim, end_delim) output =
161 output |> first_field begin_delim |> the |> snd
162 |> first_field end_delim |> the |> fst
163 |> first_field "\n" |> the |> snd
164 handle Option.Option => ""
166 val tstp_important_message_delims =
167 ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
169 fun extract_important_message output =
170 case extract_delimited tstp_important_message_delims output of
172 | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
173 |> map (perhaps (try (unprefix "%")))
174 |> map (perhaps (try (unprefix " ")))
175 |> space_implode "\n " |> quote
177 (* Splits by the first possible of a list of delimiters. *)
178 fun extract_tstplike_proof delims output =
179 case pairself (find_first (fn s => String.isSubstring s output))
180 (ListPair.unzip delims) of
181 (SOME begin_delim, SOME end_delim) =>
182 extract_delimited (begin_delim, end_delim) output
185 fun extract_known_failure known_failures output =
187 |> find_first (fn (_, pattern) => String.isSubstring pattern output)
190 fun extract_tstplike_proof_and_outcome verbose complete proof_delims
191 known_failures output =
192 case (extract_tstplike_proof proof_delims output,
193 extract_known_failure known_failures output) of
194 (_, SOME ProofIncomplete) => ("", SOME ProofIncomplete)
195 | ("", SOME ProofMissing) => ("", NONE)
196 | ("", SOME failure) =>
197 ("", SOME (if failure = GaveUp andalso complete then Unprovable
199 | ("", NONE) => ("", SOME (UnknownError (short_output verbose output)))
200 | (tstplike_proof, _) => (tstplike_proof, NONE)
202 type step_name = string * string list
204 fun is_same_atp_step (s1, _) (s2, _) = s1 = s2
206 val vampire_fact_prefix = "f"
208 fun step_name_ord p =
209 let val q = pairself fst p in
210 (* The "unprefix" part is to cope with remote Vampire's output. The proper
211 solution would be to perform a topological sort, e.g. using the nice
213 case pairself (Int.fromString
214 o perhaps (try (unprefix vampire_fact_prefix))) q of
215 (NONE, NONE) => string_ord q
216 | (NONE, SOME _) => LESS
217 | (SOME _, NONE) => GREATER
218 | (SOME i, SOME j) => int_ord (i, j)
222 Definition_Step of step_name * 'a * 'a |
223 Inference_Step of step_name * 'a * string * step_name list
225 type 'a proof = ('a, 'a, ('a, 'a) ho_term) formula step list
227 fun step_name (Definition_Step (name, _, _)) = name
228 | step_name (Inference_Step (name, _, _, _)) = name
230 (**** PARSING OF TSTP FORMAT ****)
232 (* Strings enclosed in single quotes (e.g., file names) *)
233 val scan_general_id =
234 $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
235 || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
236 >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
240 fun skip _ accum [] = (accum, [])
241 | skip 0 accum (ss as "," :: _) = (accum, ss)
242 | skip 0 accum (ss as ")" :: _) = (accum, ss)
243 | skip 0 accum (ss as "]" :: _) = (accum, ss)
244 | skip n accum ((s as "(") :: ss) = skip (n + 1) (s :: accum) ss
245 | skip n accum ((s as "[") :: ss) = skip (n + 1) (s :: accum) ss
246 | skip n accum ((s as "]") :: ss) = skip (n - 1) (s :: accum) ss
247 | skip n accum ((s as ")") :: ss) = skip (n - 1) (s :: accum) ss
248 | skip n accum (s :: ss) = skip n (s :: accum) ss
249 in skip 0 [] #>> (rev #> implode) end
252 File_Source of string * string option |
253 Inference_Source of string * string list
255 val dummy_phi = AAtom (ATerm (("", []), []))
256 val dummy_inference = Inference_Source ("", [])
258 (* "skip_term" is there to cope with Waldmeister nonsense such as
259 "theory(equality)". *)
260 val parse_dependency = scan_general_id --| skip_term
261 val parse_dependencies =
262 parse_dependency ::: Scan.repeat ($$ "," |-- parse_dependency)
265 (Scan.this_string "file" |-- $$ "(" |-- scan_general_id --
266 Scan.option ($$ "," |-- scan_general_id) --| $$ ")"
268 || Scan.this_string "inference" |-- $$ "(" |-- scan_general_id
269 --| skip_term --| $$ "," --| skip_term --| $$ "," --| $$ "["
270 -- parse_dependencies --| $$ "]" --| $$ ")"
272 || skip_term >> K dummy_inference) x
274 fun list_app (f, args) =
275 fold (fn arg => fn f => ATerm ((tptp_app, []), [f, arg])) args f
277 (* We currently ignore TFF and THF types. *)
278 fun parse_type_stuff x =
279 Scan.repeat (($$ tptp_has_type || $$ tptp_fun_type) |-- parse_arg) x
281 ($$ "(" |-- parse_term --| $$ ")" --| parse_type_stuff
282 || scan_general_id --| parse_type_stuff
283 -- Scan.optional ($$ "(" |-- parse_terms --| $$ ")") []
284 >> (ATerm o apfst (rpair []))) x
286 (parse_arg -- Scan.repeat ($$ tptp_app |-- parse_arg) >> list_app) x
288 (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
291 (parse_term -- Scan.option (Scan.option ($$ tptp_not_infix) --| $$ tptp_equal
293 >> (fn (u1, NONE) => AAtom u1
294 | (u1, SOME (neg, u2)) =>
295 AAtom (ATerm (("equal", []), [u1, u2])) |> is_some neg ? mk_anot)) x
297 (* TPTP formulas are fully parenthesized, so we don't need to worry about
298 operator precedence. *)
299 fun parse_literal x =
300 ((Scan.repeat ($$ tptp_not) >> length)
301 -- ($$ "(" |-- parse_formula --| $$ ")"
302 || parse_quantified_formula
304 >> (fn (n, phi) => phi |> n mod 2 = 1 ? mk_anot)) x
305 and parse_formula x =
307 -- Scan.option ((Scan.this_string tptp_implies
308 || Scan.this_string tptp_iff
309 || Scan.this_string tptp_not_iff
310 || Scan.this_string tptp_if
312 || $$ tptp_and) -- parse_formula)
313 >> (fn (phi1, NONE) => phi1
314 | (phi1, SOME (c, phi2)) =>
315 if c = tptp_implies then mk_aconn AImplies phi1 phi2
316 else if c = tptp_iff then mk_aconn AIff phi1 phi2
317 else if c = tptp_not_iff then mk_anot (mk_aconn AIff phi1 phi2)
318 else if c = tptp_if then mk_aconn AImplies phi2 phi1
319 else if c = tptp_or then mk_aconn AOr phi1 phi2
320 else if c = tptp_and then mk_aconn AAnd phi1 phi2
321 else raise Fail ("impossible connective " ^ quote c))) x
322 and parse_quantified_formula x =
323 (($$ tptp_forall >> K AForall || $$ tptp_exists >> K AExists)
324 --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_literal
325 >> (fn ((q, ts), phi) =>
326 (* We ignore TFF and THF types for now. *)
327 AQuant (q, map (fn ATerm ((s, []), _) => (s, NONE)) ts, phi))) x
329 val parse_tstp_extra_arguments =
330 Scan.optional ($$ "," |-- parse_source --| Scan.option ($$ "," |-- skip_term))
333 val waldmeister_conjecture_name = "conjecture_1"
335 val tofof_fact_prefix = "fof_"
337 fun is_same_term subst tm1 tm2 =
339 fun do_term_pair _ NONE = NONE
340 | do_term_pair (ATerm ((s1, _), tm1), ATerm ((s2, _), tm2)) (SOME subst) =
341 case pairself is_tptp_variable (s1, s2) of
343 (case AList.lookup (op =) subst s1 of
344 SOME s2' => if s2' = s2 then SOME subst else NONE
346 if null (AList.find (op =) subst s2) then SOME ((s1, s2) :: subst)
349 if s1 = s2 andalso length tm1 = length tm2 then
350 SOME subst |> fold do_term_pair (tm1 ~~ tm2)
354 in SOME subst |> do_term_pair (tm1, tm2) |> is_some end
356 fun is_same_formula comm subst (AQuant (q1, xs1, phi1)) (AQuant (q2, xs2, phi2)) =
357 q1 = q2 andalso length xs1 = length xs2 andalso
358 is_same_formula comm ((map fst xs1 ~~ map fst xs2) @ subst) phi1 phi2
359 | is_same_formula comm subst (AConn (c1, phis1)) (AConn (c2, phis2)) =
360 c1 = c2 andalso length phis1 = length phis2 andalso
361 forall (uncurry (is_same_formula comm subst)) (phis1 ~~ phis2)
362 | is_same_formula comm subst
363 (AAtom (tm1 as ATerm (("equal", []), [tm11, tm12]))) (AAtom tm2) =
364 is_same_term subst tm1 tm2 orelse
365 (comm andalso is_same_term subst (ATerm (("equal", []), [tm12, tm11])) tm2)
366 | is_same_formula _ subst (AAtom tm1) (AAtom tm2) = is_same_term subst tm1 tm2
367 | is_same_formula _ _ _ _ = false
369 fun matching_formula_line_identifier phi (Formula (ident, _, phi', _, _)) =
370 if is_same_formula true [] phi phi' then SOME (ident, phi') else NONE
371 | matching_formula_line_identifier _ _ = NONE
373 fun find_formula_in_problem problem phi =
374 problem |> maps snd |> map_filter (matching_formula_line_identifier phi)
375 |> try (single o hd) |> the_default []
377 fun commute_eq (AAtom (ATerm ((s, []), tms))) = AAtom (ATerm ((s, []), rev tms))
378 | commute_eq _ = raise Fail "expected equation"
380 (* Syntax: (cnf|fof|tff|thf)\(<num>, <formula_role>,
381 <formula> <extra_arguments>\).
382 The <num> could be an identifier, but we assume integers. *)
383 fun parse_tstp_line problem =
384 ((Scan.this_string tptp_cnf || Scan.this_string tptp_fof
385 || Scan.this_string tptp_tff || Scan.this_string tptp_thf) -- $$ "(")
386 |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
387 -- (parse_formula || skip_term >> K dummy_phi) -- parse_tstp_extra_arguments
388 --| $$ ")" --| $$ "."
389 >> (fn (((num, role), phi), deps) =>
391 val ((name, phi), rule, deps) =
392 (* Waldmeister isn't exactly helping. *)
394 File_Source (_, SOME s) =>
395 (if s = waldmeister_conjecture_name then
396 case find_formula_in_problem problem (mk_anot phi) of
397 (* Waldmeister hack: Get the original orientation of the
398 equation to avoid confusing Isar. *)
401 phi |> not (is_same_formula false [] (mk_anot phi) phi')
403 | _ => ((num, []), phi)
405 ((num, [s |> perhaps (try (unprefix tofof_fact_prefix))]),
409 (((num, phi |> find_formula_in_problem problem |> map fst),
411 | Inference_Source (rule, deps) => (((num, []), phi), rule, deps)
413 Inference_Step (name, phi, rule, map (rpair []) deps)
418 AConn (AIff, [phi1 as AAtom _, phi2]) =>
419 Definition_Step (name, phi1, phi2)
420 | AAtom (ATerm (("equal", []), _)) =>
421 (* Vampire's equality proxy axiom *)
422 Inference_Step (name, phi, rule, map (rpair []) deps)
427 (**** PARSING OF SPASS OUTPUT ****)
429 (* SPASS returns clause references of the form "x.y". We ignore "y", whose role
430 is not clear anyway. *)
431 val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
433 val parse_spass_annotations =
434 Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
435 --| Scan.option ($$ ","))) []
437 (* It is not clear why some literals are followed by sequences of stars and/or
438 pluses. We ignore them. *)
439 fun parse_decorated_atom x =
440 (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
442 fun mk_horn ([], []) = AAtom (ATerm (("c_False", []), []))
443 | mk_horn ([], pos_lits) = foldr1 (uncurry (mk_aconn AOr)) pos_lits
444 | mk_horn (neg_lits, []) = mk_anot (foldr1 (uncurry (mk_aconn AAnd)) neg_lits)
445 | mk_horn (neg_lits, pos_lits) =
446 mk_aconn AImplies (foldr1 (uncurry (mk_aconn AAnd)) neg_lits)
447 (foldr1 (uncurry (mk_aconn AOr)) pos_lits)
449 fun parse_horn_clause x =
450 (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
451 -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
452 -- Scan.repeat parse_decorated_atom
453 >> (mk_horn o apfst (op @))) x
455 val parse_spass_debug =
456 Scan.option ($$ "(" |-- Scan.repeat (scan_general_id --| Scan.option ($$ ","))
459 (* Syntax: <num>[0:<inference><annotations>] <atoms> || <atoms> -> <atoms>.
460 derived from formulae <ident>* *)
461 fun parse_spass_line x =
462 (parse_spass_debug |-- scan_general_id --| $$ "[" --| $$ "0" --| $$ ":"
463 -- Symbol.scan_id -- parse_spass_annotations --| $$ "]"
464 -- parse_horn_clause --| $$ "."
465 -- Scan.option (Scan.this_string "derived from formulae "
466 |-- Scan.repeat (scan_general_id --| Scan.option ($$ " ")))
467 >> (fn ((((num, rule), deps), u), names) =>
468 Inference_Step ((num, these names), u, rule, map (rpair []) deps))) x
470 val satallax_unsat_coreN = "__satallax_unsat_core" (* arbitrary *)
473 fun parse_satallax_line x =
474 (scan_general_id --| Scan.option ($$ " ")
475 >> (fn s => Inference_Step ((s, [s]), dummy_phi, satallax_unsat_coreN, [])))
478 fun parse_line problem =
479 parse_tstp_line problem || parse_spass_line || parse_satallax_line
480 fun parse_proof problem tstp =
481 tstp |> strip_spaces_except_between_idents
483 |> Scan.finite Symbol.stopper
484 (Scan.error (!! (fn _ => raise UNRECOGNIZED_ATP_PROOF ())
485 (Scan.repeat1 (parse_line problem))))
488 fun atp_proof_from_tstplike_proof _ "" = []
489 | atp_proof_from_tstplike_proof problem tstp =
490 tstp ^ "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
491 |> parse_proof problem
492 |> sort (step_name_ord o pairself step_name) (* FIXME: needed? *)
494 fun clean_up_dependencies _ [] = []
495 | clean_up_dependencies seen
496 ((step as Definition_Step (name, _, _)) :: steps) =
497 step :: clean_up_dependencies (name :: seen) steps
498 | clean_up_dependencies seen (Inference_Step (name, u, rule, deps) :: steps) =
499 Inference_Step (name, u, rule,
500 map_filter (fn dep => find_first (is_same_atp_step dep) seen) deps) ::
501 clean_up_dependencies (name :: seen) steps
503 fun clean_up_atp_proof_dependencies proof = clean_up_dependencies [] proof
505 fun map_term_names_in_term f (ATerm ((s, tys), ts)) =
506 ATerm ((f s, tys), map (map_term_names_in_term f) ts)
507 fun map_term_names_in_formula f (AQuant (q, xs, phi)) =
508 AQuant (q, xs, map_term_names_in_formula f phi)
509 | map_term_names_in_formula f (AConn (c, phis)) =
510 AConn (c, map (map_term_names_in_formula f) phis)
511 | map_term_names_in_formula f (AAtom t) = AAtom (map_term_names_in_term f t)
512 fun map_term_names_in_step f (Definition_Step (name, phi1, phi2)) =
513 Definition_Step (name, map_term_names_in_formula f phi1,
514 map_term_names_in_formula f phi2)
515 | map_term_names_in_step f (Inference_Step (name, phi, rule, deps)) =
516 Inference_Step (name, map_term_names_in_formula f phi, rule, deps)
517 fun map_term_names_in_atp_proof f = map (map_term_names_in_step f)
519 fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
520 fun nasty_atp_proof pool =
521 if Symtab.is_empty pool then I
522 else map_term_names_in_atp_proof (nasty_name pool)