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, 'd) formula = ('a, 'b, 'c, 'd) 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, 'a) 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_coreN : string
56 val z3_tptp_coreN : string
59 -> (string, 'a, (string, 'a) ho_term, string) formula * string list
60 val atp_proof_from_tstplike_proof : string problem -> string -> string proof
61 val clean_up_atp_proof_dependencies : string proof -> string proof
62 val map_term_names_in_atp_proof :
63 (string -> string) -> string proof -> string proof
64 val nasty_atp_proof : string Symtab.table -> string proof -> string proof
67 structure ATP_Proof : ATP_PROOF =
73 exception UNRECOGNIZED_ATP_PROOF of unit
80 UnsoundProof of bool * string list |
93 UnknownError of string
95 fun short_output verbose output =
97 if output = "" then "No details available" else elide_string 1000 output
101 val missing_message_tail =
102 " appears to be missing. You will need to install it if you want to invoke \
105 fun involving [] = ""
107 "involving " ^ space_implode " " (Try.serial_commas "and" (map quote ss)) ^
110 fun string_for_failure Unprovable = "The generated problem is unprovable."
111 | string_for_failure GaveUp = "The prover gave up."
112 | string_for_failure ProofMissing =
113 "The prover claims the conjecture is a theorem but did not provide a proof."
114 | string_for_failure ProofIncomplete =
115 "The prover claims the conjecture is a theorem but provided an incomplete \
116 \(or unparsable) proof."
117 | string_for_failure (UnsoundProof (false, ss)) =
118 "The prover found a type-unsound proof " ^ involving ss ^
119 "(or, less likely, your axioms are inconsistent). Specify a sound type \
120 \encoding or omit the \"type_enc\" option."
121 | string_for_failure (UnsoundProof (true, ss)) =
122 "The prover found a type-unsound proof " ^ involving ss ^
123 "even though a supposedly type-sound encoding was used (or, less likely, \
124 \your axioms are inconsistent). Please report this to the Isabelle \
126 | string_for_failure CantConnect = "Cannot connect to remote server."
127 | string_for_failure TimedOut = "Timed out."
128 | string_for_failure Inappropriate =
129 "The generated problem lies outside the prover's scope."
130 | string_for_failure OutOfResources = "The prover ran out of resources."
131 | string_for_failure OldSPASS =
132 "The version of SPASS you are using is obsolete. Please upgrade to \
133 \SPASS 3.8ds. To install it, download and extract the package \
134 \\"http://www21.in.tum.de/~blanchet/spass-3.8ds.tar.gz\" and add the \
135 \\"spass-3.8ds\" directory's absolute path to " ^
136 quote (Path.implode (Path.expand (Path.appends
137 (Path.variable "ISABELLE_HOME_USER" ::
138 map Path.basic ["etc", "components"])))) ^
139 " on a line of its own."
140 | string_for_failure NoPerl = "Perl" ^ missing_message_tail
141 | string_for_failure NoLibwwwPerl =
142 "The Perl module \"libwww-perl\"" ^ missing_message_tail
143 | string_for_failure MalformedInput =
144 "The generated problem is malformed. Please report this to the Isabelle \
146 | string_for_failure MalformedOutput = "The prover output is malformed."
147 | string_for_failure Interrupted = "The prover was interrupted."
148 | string_for_failure Crashed = "The prover crashed."
149 | string_for_failure InternalError = "An internal prover error occurred."
150 | string_for_failure (UnknownError string) =
151 "A prover error occurred" ^
152 (if string = "" then ". (Pass the \"verbose\" option for details.)"
155 fun extract_delimited (begin_delim, end_delim) output =
156 output |> first_field begin_delim |> the |> snd
157 |> first_field end_delim |> the |> fst
158 |> perhaps (try (first_field "\n" #> the #> snd))
159 handle Option.Option => ""
161 val tstp_important_message_delims =
162 ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
164 fun extract_important_message output =
165 case extract_delimited tstp_important_message_delims output of
167 | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
168 |> map (perhaps (try (unprefix "%")))
169 |> map (perhaps (try (unprefix " ")))
170 |> space_implode "\n " |> quote
172 (* Splits by the first possible of a list of delimiters. *)
173 fun extract_tstplike_proof delims output =
174 case pairself (find_first (fn s => String.isSubstring s output))
175 (ListPair.unzip delims) of
176 (SOME begin_delim, SOME end_delim) =>
177 extract_delimited (begin_delim, end_delim) output
180 fun extract_known_failure known_failures output =
182 |> find_first (fn (_, pattern) => String.isSubstring pattern output)
185 fun extract_tstplike_proof_and_outcome verbose complete proof_delims
186 known_failures output =
187 case (extract_tstplike_proof proof_delims output,
188 extract_known_failure known_failures output) of
189 (_, SOME ProofIncomplete) => ("", SOME ProofIncomplete)
190 | ("", SOME ProofMissing) => ("", NONE)
191 | ("", SOME failure) =>
192 ("", SOME (if failure = GaveUp andalso complete then Unprovable
194 | ("", NONE) => ("", SOME (UnknownError (short_output verbose output)))
195 | (tstplike_proof, _) => (tstplike_proof, NONE)
197 type step_name = string * string list
199 fun is_same_atp_step (s1, _) (s2, _) = s1 = s2
201 val vampire_fact_prefix = "f"
203 fun step_name_ord p =
204 let val q = pairself fst p in
205 (* The "unprefix" part is to cope with remote Vampire's output. The proper
206 solution would be to perform a topological sort, e.g. using the nice
208 case pairself (Int.fromString
209 o perhaps (try (unprefix vampire_fact_prefix))) q of
210 (NONE, NONE) => string_ord q
211 | (NONE, SOME _) => LESS
212 | (SOME _, NONE) => GREATER
213 | (SOME i, SOME j) => int_ord (i, j)
217 Definition_Step of step_name * 'a * 'a |
218 Inference_Step of step_name * 'a * string * step_name list
220 type 'a proof = ('a, 'a, ('a, 'a) ho_term, 'a) formula step list
222 fun step_name (Definition_Step (name, _, _)) = name
223 | step_name (Inference_Step (name, _, _, _)) = name
225 (**** PARSING OF TSTP FORMAT ****)
227 (* Strings enclosed in single quotes (e.g., file names) *)
228 val scan_general_id =
229 $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
230 || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
231 >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
235 fun skip _ accum [] = (accum, [])
236 | skip 0 accum (ss as "," :: _) = (accum, ss)
237 | skip 0 accum (ss as ")" :: _) = (accum, ss)
238 | skip 0 accum (ss as "]" :: _) = (accum, ss)
239 | skip n accum ((s as "(") :: ss) = skip (n + 1) (s :: accum) ss
240 | skip n accum ((s as "[") :: ss) = skip (n + 1) (s :: accum) ss
241 | skip n accum ((s as "]") :: ss) = skip (n - 1) (s :: accum) ss
242 | skip n accum ((s as ")") :: ss) = skip (n - 1) (s :: accum) ss
243 | skip n accum (s :: ss) = skip n (s :: accum) ss
244 in skip 0 [] #>> (rev #> implode) end
247 File_Source of string * string option |
248 Inference_Source of string * string list
250 val dummy_phi = AAtom (ATerm (("", []), []))
251 val dummy_inference = Inference_Source ("", [])
253 (* "skip_term" is there to cope with Waldmeister nonsense such as
254 "theory(equality)". *)
255 val parse_dependency = scan_general_id --| skip_term
256 val parse_dependencies =
257 parse_dependency ::: Scan.repeat ($$ "," |-- parse_dependency)
260 (Scan.this_string "file" |-- $$ "(" |-- scan_general_id --
261 Scan.option ($$ "," |-- scan_general_id) --| $$ ")"
263 || Scan.this_string "inference" |-- $$ "(" |-- scan_general_id
264 --| skip_term --| $$ "," --| skip_term --| $$ "," --| $$ "["
265 -- parse_dependencies --| $$ "]" --| $$ ")"
267 || skip_term >> K dummy_inference) x
269 fun list_app (f, args) =
270 fold (fn arg => fn f => ATerm ((tptp_app, []), [f, arg])) args f
272 (* We currently ignore TFF and THF types. *)
273 fun parse_type_stuff x =
274 Scan.repeat (($$ tptp_has_type || $$ tptp_fun_type) |-- parse_arg) x
276 ($$ "(" |-- parse_term --| $$ ")" --| parse_type_stuff
277 || scan_general_id --| parse_type_stuff
278 -- Scan.optional ($$ "(" |-- parse_terms --| $$ ")") []
279 >> (ATerm o apfst (rpair []))) x
281 (parse_arg -- Scan.repeat ($$ tptp_app |-- parse_arg) >> list_app) x
283 (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
286 (parse_term -- Scan.option (Scan.option ($$ tptp_not_infix) --| $$ tptp_equal
288 >> (fn (u1, NONE) => AAtom u1
289 | (u1, SOME (neg, u2)) =>
290 AAtom (ATerm (("equal", []), [u1, u2])) |> is_some neg ? mk_anot)) x
292 (* TPTP formulas are fully parenthesized, so we don't need to worry about
293 operator precedence. *)
294 fun parse_literal x =
295 ((Scan.repeat ($$ tptp_not) >> length)
296 -- ($$ "(" |-- parse_formula --| $$ ")"
297 || parse_quantified_formula
299 >> (fn (n, phi) => phi |> n mod 2 = 1 ? mk_anot)) x
300 and parse_formula x =
302 -- Scan.option ((Scan.this_string tptp_implies
303 || Scan.this_string tptp_iff
304 || Scan.this_string tptp_not_iff
305 || Scan.this_string tptp_if
307 || $$ tptp_and) -- parse_formula)
308 >> (fn (phi1, NONE) => phi1
309 | (phi1, SOME (c, phi2)) =>
310 if c = tptp_implies then mk_aconn AImplies phi1 phi2
311 else if c = tptp_iff then mk_aconn AIff phi1 phi2
312 else if c = tptp_not_iff then mk_anot (mk_aconn AIff phi1 phi2)
313 else if c = tptp_if then mk_aconn AImplies phi2 phi1
314 else if c = tptp_or then mk_aconn AOr phi1 phi2
315 else if c = tptp_and then mk_aconn AAnd phi1 phi2
316 else raise Fail ("impossible connective " ^ quote c))) x
317 and parse_quantified_formula x =
318 (($$ tptp_forall >> K AForall || $$ tptp_exists >> K AExists)
319 --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_literal
320 >> (fn ((q, ts), phi) =>
321 (* We ignore TFF and THF types for now. *)
322 AQuant (q, map (fn ATerm ((s, []), _) => (s, NONE)) ts, phi))) x
324 val parse_tstp_extra_arguments =
325 Scan.optional ($$ "," |-- parse_source --| Scan.option ($$ "," |-- skip_term))
328 val waldmeister_conjecture_name = "conjecture_1"
330 val tofof_fact_prefix = "fof_"
332 fun is_same_term subst tm1 tm2 =
334 fun do_term_pair _ NONE = NONE
335 | do_term_pair (ATerm ((s1, _), tm1), ATerm ((s2, _), tm2)) (SOME subst) =
336 case pairself is_tptp_variable (s1, s2) of
338 (case AList.lookup (op =) subst s1 of
339 SOME s2' => if s2' = s2 then SOME subst else NONE
341 if null (AList.find (op =) subst s2) then SOME ((s1, s2) :: subst)
344 if s1 = s2 andalso length tm1 = length tm2 then
345 SOME subst |> fold do_term_pair (tm1 ~~ tm2)
349 in SOME subst |> do_term_pair (tm1, tm2) |> is_some end
351 fun is_same_formula comm subst (AQuant (q1, xs1, phi1)) (AQuant (q2, xs2, phi2)) =
352 q1 = q2 andalso length xs1 = length xs2 andalso
353 is_same_formula comm ((map fst xs1 ~~ map fst xs2) @ subst) phi1 phi2
354 | is_same_formula comm subst (AConn (c1, phis1)) (AConn (c2, phis2)) =
355 c1 = c2 andalso length phis1 = length phis2 andalso
356 forall (uncurry (is_same_formula comm subst)) (phis1 ~~ phis2)
357 | is_same_formula comm subst
358 (AAtom (tm1 as ATerm (("equal", []), [tm11, tm12]))) (AAtom tm2) =
359 is_same_term subst tm1 tm2 orelse
360 (comm andalso is_same_term subst (ATerm (("equal", []), [tm12, tm11])) tm2)
361 | is_same_formula _ subst (AAtom tm1) (AAtom tm2) = is_same_term subst tm1 tm2
362 | is_same_formula _ _ _ _ = false
364 fun matching_formula_line_identifier phi (Formula (ident, _, phi', _, _)) =
365 if is_same_formula true [] phi phi' then SOME (ident, phi') else NONE
366 | matching_formula_line_identifier _ _ = NONE
368 fun find_formula_in_problem problem phi =
369 problem |> maps snd |> map_filter (matching_formula_line_identifier phi)
370 |> try (single o hd) |> the_default []
372 fun commute_eq (AAtom (ATerm ((s, []), tms))) = AAtom (ATerm ((s, []), rev tms))
373 | commute_eq _ = raise Fail "expected equation"
375 (* Syntax: (cnf|fof|tff|thf)\(<num>, <formula_role>,
376 <formula> <extra_arguments>\).
377 The <num> could be an identifier, but we assume integers. *)
378 fun parse_tstp_line problem =
379 ((Scan.this_string tptp_cnf || Scan.this_string tptp_fof
380 || Scan.this_string tptp_tff || Scan.this_string tptp_thf) -- $$ "(")
381 |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
382 -- (parse_formula || skip_term >> K dummy_phi) -- parse_tstp_extra_arguments
383 --| $$ ")" --| $$ "."
384 >> (fn (((num, role), phi), deps) =>
386 val ((name, phi), rule, deps) =
387 (* Waldmeister isn't exactly helping. *)
389 File_Source (_, SOME s) =>
390 (if s = waldmeister_conjecture_name then
391 case find_formula_in_problem problem (mk_anot phi) of
392 (* Waldmeister hack: Get the original orientation of the
393 equation to avoid confusing Isar. *)
396 phi |> not (is_same_formula false [] (mk_anot phi) phi')
398 | _ => ((num, []), phi)
400 ((num, [s |> perhaps (try (unprefix tofof_fact_prefix))]),
404 (((num, phi |> find_formula_in_problem problem |> map fst),
406 | Inference_Source (rule, deps) => (((num, []), phi), rule, deps)
408 Inference_Step (name, phi, rule, map (rpair []) deps)
413 AConn (AIff, [phi1 as AAtom _, phi2]) =>
414 Definition_Step (name, phi1, phi2)
415 | AAtom (ATerm (("equal", []), _)) =>
416 (* Vampire's equality proxy axiom *)
417 Inference_Step (name, phi, rule, map (rpair []) deps)
422 (**** PARSING OF SPASS OUTPUT ****)
424 (* SPASS returns clause references of the form "x.y". We ignore "y", whose role
425 is not clear anyway. *)
426 val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
428 val parse_spass_annotations =
429 Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
430 --| Scan.option ($$ ","))) []
432 (* It is not clear why some literals are followed by sequences of stars and/or
433 pluses. We ignore them. *)
434 fun parse_decorated_atom x =
435 (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
437 fun mk_horn ([], []) = AAtom (ATerm (("c_False", []), []))
438 | mk_horn ([], pos_lits) = foldr1 (uncurry (mk_aconn AOr)) pos_lits
439 | mk_horn (neg_lits, []) = mk_anot (foldr1 (uncurry (mk_aconn AAnd)) neg_lits)
440 | mk_horn (neg_lits, pos_lits) =
441 mk_aconn AImplies (foldr1 (uncurry (mk_aconn AAnd)) neg_lits)
442 (foldr1 (uncurry (mk_aconn AOr)) pos_lits)
444 fun parse_horn_clause x =
445 (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
446 -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
447 -- Scan.repeat parse_decorated_atom
448 >> (mk_horn o apfst (op @))) x
450 val parse_spass_debug =
451 Scan.option ($$ "(" |-- Scan.repeat (scan_general_id --| Scan.option ($$ ","))
454 (* Syntax: <num>[0:<inference><annotations>] <atoms> || <atoms> -> <atoms>.
455 derived from formulae <ident>* *)
456 fun parse_spass_line x =
457 (parse_spass_debug |-- scan_general_id --| $$ "[" --| $$ "0" --| $$ ":"
458 -- Symbol.scan_id -- parse_spass_annotations --| $$ "]"
459 -- parse_horn_clause --| $$ "."
460 -- Scan.option (Scan.this_string "derived from formulae "
461 |-- Scan.repeat (scan_general_id --| Scan.option ($$ " ")))
462 >> (fn ((((num, rule), deps), u), names) =>
463 Inference_Step ((num, these names), u, rule, map (rpair []) deps))) x
465 val satallax_coreN = "__satallax_core" (* arbitrary *)
466 val z3_tptp_coreN = "__z3_tptp_core" (* arbitrary *)
468 (* Syntax: core(<name>,[<name>,...,<name>]). *)
469 fun parse_z3_tptp_line x =
470 (scan_general_id --| $$ "," --| $$ "[" -- parse_dependencies --| $$ "]"
471 >> (fn (name, names) =>
472 Inference_Step (("", name :: names), dummy_phi, z3_tptp_coreN, []))) x
475 fun parse_satallax_line x =
476 (scan_general_id --| Scan.option ($$ " ")
477 >> (fn s => Inference_Step ((s, [s]), dummy_phi, satallax_coreN, []))) x
479 fun parse_line problem =
480 parse_tstp_line problem || parse_spass_line || parse_z3_tptp_line
481 || parse_satallax_line
482 fun parse_proof problem tstp =
483 tstp |> strip_spaces_except_between_idents
485 |> Scan.finite Symbol.stopper
486 (Scan.error (!! (fn _ => raise UNRECOGNIZED_ATP_PROOF ())
487 (Scan.repeat1 (parse_line problem))))
490 fun atp_proof_from_tstplike_proof _ "" = []
491 | atp_proof_from_tstplike_proof problem tstp =
492 tstp ^ "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
493 |> parse_proof problem
494 |> sort (step_name_ord o pairself step_name) (* FIXME: needed? *)
496 fun clean_up_dependencies _ [] = []
497 | clean_up_dependencies seen
498 ((step as Definition_Step (name, _, _)) :: steps) =
499 step :: clean_up_dependencies (name :: seen) steps
500 | clean_up_dependencies seen (Inference_Step (name, u, rule, deps) :: steps) =
501 Inference_Step (name, u, rule,
502 map_filter (fn dep => find_first (is_same_atp_step dep) seen) deps) ::
503 clean_up_dependencies (name :: seen) steps
505 fun clean_up_atp_proof_dependencies proof = clean_up_dependencies [] proof
507 fun map_term_names_in_term f (ATerm ((s, tys), ts)) =
508 ATerm ((f s, tys), map (map_term_names_in_term f) ts)
509 fun map_term_names_in_formula f (AQuant (q, xs, phi)) =
510 AQuant (q, xs, map_term_names_in_formula f phi)
511 | map_term_names_in_formula f (AConn (c, phis)) =
512 AConn (c, map (map_term_names_in_formula f) phis)
513 | map_term_names_in_formula f (AAtom t) = AAtom (map_term_names_in_term f t)
514 fun map_term_names_in_step f (Definition_Step (name, phi1, phi2)) =
515 Definition_Step (name, map_term_names_in_formula f phi1,
516 map_term_names_in_formula f phi2)
517 | map_term_names_in_step f (Inference_Step (name, phi, rule, deps)) =
518 Inference_Step (name, map_term_names_in_formula f phi, rule, deps)
519 fun map_term_names_in_atp_proof f = map (map_term_names_in_step f)
521 fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
522 fun nasty_atp_proof pool =
523 if Symtab.is_empty pool then I
524 else map_term_names_in_atp_proof (nasty_name pool)