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 formula_role = ATP_Problem.formula_role
13 type ('a, 'b, 'c, 'd) formula = ('a, 'b, 'c, 'd) ATP_Problem.formula
14 type 'a problem = 'a ATP_Problem.problem
16 exception UNRECOGNIZED_ATP_PROOF of unit
23 UnsoundProof of bool * string list |
36 UnknownError of string
38 type step_name = string * string list
39 type 'a step = step_name * formula_role * 'a * string * step_name list
41 type 'a proof = ('a, 'a, ('a, 'a) ho_term, 'a) formula step list
43 val short_output : bool -> string -> string
44 val string_for_failure : failure -> string
45 val extract_important_message : string -> string
46 val extract_known_failure :
47 (failure * string) list -> string -> failure option
48 val extract_tstplike_proof_and_outcome :
49 bool -> (string * string) list -> (failure * string) list -> string
50 -> string * failure option
51 val is_same_atp_step : step_name -> step_name -> bool
52 val scan_general_id : string list -> string * string list
53 val satallax_coreN : string
54 val z3_tptp_coreN : string
57 -> (string, 'a, (string, 'a) ho_term, string) formula * string list
58 val atp_proof_from_tstplike_proof : string problem -> string -> string proof
59 val clean_up_atp_proof_dependencies : string proof -> string proof
60 val map_term_names_in_atp_proof :
61 (string -> string) -> string proof -> string proof
62 val nasty_atp_proof : string Symtab.table -> string proof -> string proof
65 structure ATP_Proof : ATP_PROOF =
71 exception UNRECOGNIZED_ATP_PROOF of unit
78 UnsoundProof of bool * string list |
91 UnknownError of string
93 fun short_output verbose output =
95 if output = "" then "No details available" else elide_string 1000 output
99 val missing_message_tail =
100 " appears to be missing. You will need to install it if you want to invoke \
103 fun involving [] = ""
105 "involving " ^ space_implode " " (Try.serial_commas "and" (map quote ss)) ^
108 fun string_for_failure Unprovable = "The generated problem is unprovable."
109 | string_for_failure GaveUp = "The prover gave up."
110 | string_for_failure ProofMissing =
111 "The prover claims the conjecture is a theorem but did not provide a proof."
112 | string_for_failure ProofIncomplete =
113 "The prover claims the conjecture is a theorem but provided an incomplete \
114 \(or unparsable) proof."
115 | string_for_failure (UnsoundProof (false, ss)) =
116 "The prover found a type-unsound proof " ^ involving ss ^
117 "(or, less likely, your axioms are inconsistent). Specify a sound type \
118 \encoding or omit the \"type_enc\" option."
119 | string_for_failure (UnsoundProof (true, ss)) =
120 "The prover found a type-unsound proof " ^ involving ss ^
121 "even though a supposedly type-sound encoding was used (or, less likely, \
122 \your axioms are inconsistent). Please report this to the Isabelle \
124 | string_for_failure CantConnect = "Cannot connect to remote server."
125 | string_for_failure TimedOut = "Timed out."
126 | string_for_failure Inappropriate =
127 "The generated problem lies outside the prover's scope."
128 | string_for_failure OutOfResources = "The prover ran out of resources."
129 | string_for_failure OldSPASS =
130 "The version of SPASS you are using is obsolete. Please upgrade to \
131 \SPASS 3.8ds. To install it, download and extract the package \
132 \\"http://www21.in.tum.de/~blanchet/spass-3.8ds.tar.gz\" and add the \
133 \\"spass-3.8ds\" directory's absolute path to " ^
134 quote (Path.implode (Path.expand (Path.appends
135 (Path.variable "ISABELLE_HOME_USER" ::
136 map Path.basic ["etc", "components"])))) ^
137 " on a line of its own."
138 | string_for_failure NoPerl = "Perl" ^ missing_message_tail
139 | string_for_failure NoLibwwwPerl =
140 "The Perl module \"libwww-perl\"" ^ missing_message_tail
141 | string_for_failure MalformedInput =
142 "The generated problem is malformed. Please report this to the Isabelle \
144 | string_for_failure MalformedOutput = "The prover output is malformed."
145 | string_for_failure Interrupted = "The prover was interrupted."
146 | string_for_failure Crashed = "The prover crashed."
147 | string_for_failure InternalError = "An internal prover error occurred."
148 | string_for_failure (UnknownError string) =
149 "A prover error occurred" ^
150 (if string = "" then ". (Pass the \"verbose\" option for details.)"
153 fun extract_delimited (begin_delim, end_delim) output =
154 output |> first_field begin_delim |> the |> snd
155 |> first_field end_delim |> the |> fst
156 |> perhaps (try (first_field "\n" #> the #> snd))
157 handle Option.Option => ""
159 val tstp_important_message_delims =
160 ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
162 fun extract_important_message output =
163 case extract_delimited tstp_important_message_delims output of
165 | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
166 |> map (perhaps (try (unprefix "%")))
167 |> map (perhaps (try (unprefix " ")))
168 |> space_implode "\n " |> quote
170 (* Splits by the first possible of a list of delimiters. *)
171 fun extract_tstplike_proof delims output =
172 case pairself (find_first (fn s => String.isSubstring s output))
173 (ListPair.unzip delims) of
174 (SOME begin_delim, SOME end_delim) =>
175 extract_delimited (begin_delim, end_delim) output
178 fun extract_known_failure known_failures output =
180 |> find_first (fn (_, pattern) => String.isSubstring pattern output)
183 fun extract_tstplike_proof_and_outcome verbose proof_delims known_failures
185 case (extract_tstplike_proof proof_delims output,
186 extract_known_failure known_failures output) of
187 (_, SOME ProofIncomplete) => ("", NONE)
188 | ("", SOME ProofMissing) => ("", NONE)
189 | ("", NONE) => ("", SOME (UnknownError (short_output verbose output)))
190 | res as ("", _) => res
191 | (tstplike_proof, _) => (tstplike_proof, NONE)
193 type step_name = string * string list
195 fun is_same_atp_step (s1, _) (s2, _) = s1 = s2
197 val vampire_fact_prefix = "f"
199 fun step_name_ord p =
200 let val q = pairself fst p in
201 (* The "unprefix" part is to cope with remote Vampire's output. *)
202 case pairself (Int.fromString
203 o perhaps (try (unprefix vampire_fact_prefix))) q of
204 (NONE, NONE) => string_ord q
205 | (NONE, SOME _) => LESS
206 | (SOME _, NONE) => GREATER
207 | (SOME i, SOME j) => int_ord (i, j)
210 type 'a step = step_name * formula_role * 'a * string * step_name list
212 type 'a proof = ('a, 'a, ('a, 'a) ho_term, 'a) formula step list
214 (**** PARSING OF TSTP FORMAT ****)
216 (* Strings enclosed in single quotes (e.g., file names) *)
217 val scan_general_id =
218 $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
219 || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
220 >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
222 val scan_nat = Scan.repeat1 (Scan.one Symbol.is_ascii_digit) >> implode
226 fun skip _ accum [] = (accum, [])
227 | skip 0 accum (ss as "," :: _) = (accum, ss)
228 | skip 0 accum (ss as ")" :: _) = (accum, ss)
229 | skip 0 accum (ss as "]" :: _) = (accum, ss)
230 | skip n accum ((s as "(") :: ss) = skip (n + 1) (s :: accum) ss
231 | skip n accum ((s as "[") :: ss) = skip (n + 1) (s :: accum) ss
232 | skip n accum ((s as "]") :: ss) = skip (n - 1) (s :: accum) ss
233 | skip n accum ((s as ")") :: ss) = skip (n - 1) (s :: accum) ss
234 | skip n accum (s :: ss) = skip n (s :: accum) ss
235 in skip 0 [] #>> (rev #> implode) end
238 File_Source of string * string option |
239 Inference_Source of string * string list
241 val dummy_phi = AAtom (ATerm (("", []), []))
242 val dummy_inference = Inference_Source ("", [])
244 (* "skip_term" is there to cope with Waldmeister nonsense such as
245 "theory(equality)". *)
246 fun parse_dependency x =
247 (parse_inference_source >> snd
248 || scan_general_id --| skip_term >> single) x
249 and parse_dependencies x =
250 (parse_dependency ::: Scan.repeat ($$ "," |-- parse_dependency)
252 and parse_file_source x =
253 (Scan.this_string "file" |-- $$ "(" |-- scan_general_id
254 -- Scan.option ($$ "," |-- scan_general_id) --| $$ ")") x
255 and parse_inference_source x =
256 (Scan.this_string "inference" |-- $$ "(" |-- scan_general_id
257 --| skip_term --| $$ "," --| skip_term --| $$ "," --| $$ "["
258 -- parse_dependencies --| $$ "]" --| $$ ")") x
259 and skip_introduced x =
260 (Scan.this_string "introduced" |-- $$ "(" |-- skip_term --| $$ ")") x
262 (parse_file_source >> File_Source
263 || parse_inference_source >> Inference_Source
264 || skip_introduced >> K dummy_inference (* for Vampire *)
265 || scan_nat >> (fn s => Inference_Source ("", [s])) (* for E *)
266 || skip_term >> K dummy_inference) x
268 fun list_app (f, args) =
269 fold (fn arg => fn f => ATerm ((tptp_app, []), [f, arg])) args f
271 (* We currently ignore TFF and THF types. *)
272 fun parse_type_stuff x =
273 Scan.repeat (($$ tptp_has_type || $$ tptp_fun_type) |-- parse_arg) x
275 ($$ "(" |-- parse_term --| $$ ")" --| parse_type_stuff
276 || scan_general_id --| parse_type_stuff
277 -- Scan.optional ($$ "(" |-- parse_terms --| $$ ")") []
278 >> (ATerm o apfst (rpair []))) x
280 (parse_arg -- Scan.repeat ($$ tptp_app |-- parse_arg) >> list_app) x
282 (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
285 (parse_term -- Scan.option (Scan.option ($$ tptp_not_infix) --| $$ tptp_equal
287 >> (fn (u1, NONE) => AAtom u1
288 | (u1, SOME (neg, u2)) =>
289 AAtom (ATerm (("equal", []), [u1, u2])) |> is_some neg ? mk_anot)) x
291 (* TPTP formulas are fully parenthesized, so we don't need to worry about
292 operator precedence. *)
293 fun parse_literal x =
294 ((Scan.repeat ($$ tptp_not) >> length)
295 -- ($$ "(" |-- parse_formula --| $$ ")"
296 || parse_quantified_formula
298 >> (fn (n, phi) => phi |> n mod 2 = 1 ? mk_anot)) x
299 and parse_formula x =
301 -- Scan.option ((Scan.this_string tptp_implies
302 || Scan.this_string tptp_iff
303 || Scan.this_string tptp_not_iff
304 || Scan.this_string tptp_if
306 || $$ tptp_and) -- parse_formula)
307 >> (fn (phi1, NONE) => phi1
308 | (phi1, SOME (c, phi2)) =>
309 if c = tptp_implies then mk_aconn AImplies phi1 phi2
310 else if c = tptp_iff then mk_aconn AIff phi1 phi2
311 else if c = tptp_not_iff then mk_anot (mk_aconn AIff phi1 phi2)
312 else if c = tptp_if then mk_aconn AImplies phi2 phi1
313 else if c = tptp_or then mk_aconn AOr phi1 phi2
314 else if c = tptp_and then mk_aconn AAnd phi1 phi2
315 else raise Fail ("impossible connective " ^ quote c))) x
316 and parse_quantified_formula x =
317 (($$ tptp_forall >> K AForall || $$ tptp_exists >> K AExists)
318 --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_literal
319 >> (fn ((q, ts), phi) =>
320 (* We ignore TFF and THF types for now. *)
321 AQuant (q, map (fn ATerm ((s, []), _) => (s, NONE)) ts, phi))) x
323 val parse_tstp_extra_arguments =
324 Scan.optional ($$ "," |-- parse_source --| Scan.option ($$ "," |-- skip_term))
327 val waldmeister_conjecture_name = "conjecture_1"
329 val tofof_fact_prefix = "fof_"
331 fun is_same_term subst tm1 tm2 =
333 fun do_term_pair _ NONE = NONE
334 | do_term_pair (ATerm ((s1, _), tm1), ATerm ((s2, _), tm2)) (SOME subst) =
335 case pairself is_tptp_variable (s1, s2) of
337 (case AList.lookup (op =) subst s1 of
338 SOME s2' => if s2' = s2 then SOME subst else NONE
340 if null (AList.find (op =) subst s2) then SOME ((s1, s2) :: subst)
343 if s1 = s2 andalso length tm1 = length tm2 then
344 SOME subst |> fold do_term_pair (tm1 ~~ tm2)
348 in SOME subst |> do_term_pair (tm1, tm2) |> is_some end
350 fun is_same_formula comm subst (AQuant (q1, xs1, phi1)) (AQuant (q2, xs2, phi2)) =
351 q1 = q2 andalso length xs1 = length xs2 andalso
352 is_same_formula comm ((map fst xs1 ~~ map fst xs2) @ subst) phi1 phi2
353 | is_same_formula comm subst (AConn (c1, phis1)) (AConn (c2, phis2)) =
354 c1 = c2 andalso length phis1 = length phis2 andalso
355 forall (uncurry (is_same_formula comm subst)) (phis1 ~~ phis2)
356 | is_same_formula comm subst
357 (AAtom (tm1 as ATerm (("equal", []), [tm11, tm12]))) (AAtom tm2) =
358 is_same_term subst tm1 tm2 orelse
359 (comm andalso is_same_term subst (ATerm (("equal", []), [tm12, tm11])) tm2)
360 | is_same_formula _ subst (AAtom tm1) (AAtom tm2) = is_same_term subst tm1 tm2
361 | is_same_formula _ _ _ _ = false
363 fun matching_formula_line_identifier phi (Formula ((ident, _), _, phi', _, _)) =
364 if is_same_formula true [] phi phi' then SOME (ident, phi') else NONE
365 | matching_formula_line_identifier _ _ = NONE
367 fun find_formula_in_problem problem phi =
368 problem |> maps snd |> map_filter (matching_formula_line_identifier phi)
369 |> try (single o hd) |> the_default []
371 fun commute_eq (AAtom (ATerm ((s, []), tms))) = AAtom (ATerm ((s, []), rev tms))
372 | commute_eq _ = raise Fail "expected equation"
374 fun role_of_tptp_string "axiom" = Axiom
375 | role_of_tptp_string "definition" = Definition
376 | role_of_tptp_string "lemma" = Lemma
377 | role_of_tptp_string "hypothesis" = Hypothesis
378 | role_of_tptp_string "conjecture" = Conjecture
379 | role_of_tptp_string "negated_conjecture" = Negated_Conjecture
380 | role_of_tptp_string "plain" = Plain
381 | role_of_tptp_string _ = Unknown
383 (* Syntax: (cnf|fof|tff|thf)\(<num>, <formula_role>,
384 <formula> <extra_arguments>\).
385 The <num> could be an identifier, but we assume integers. *)
386 fun parse_tstp_line problem =
387 ((Scan.this_string tptp_cnf || Scan.this_string tptp_fof
388 || Scan.this_string tptp_tff || Scan.this_string tptp_thf) -- $$ "(")
389 |-- scan_general_id --| $$ "," -- Symbol.scan_ascii_id --| $$ ","
390 -- (parse_formula || skip_term >> K dummy_phi) -- parse_tstp_extra_arguments
391 --| $$ ")" --| $$ "."
392 >> (fn (((num, role), phi), deps) =>
394 val ((name, phi), rule, deps) =
395 (* Waldmeister isn't exactly helping. *)
397 File_Source (_, SOME s) =>
398 (if s = waldmeister_conjecture_name then
399 case find_formula_in_problem problem (mk_anot phi) of
400 (* Waldmeister hack: Get the original orientation of the
401 equation to avoid confusing Isar. *)
404 phi |> not (is_same_formula false [] (mk_anot phi) phi')
406 | _ => ((num, []), phi)
408 ((num, [s |> perhaps (try (unprefix tofof_fact_prefix))]),
412 (((num, phi |> find_formula_in_problem problem |> map fst),
414 | Inference_Source (rule, deps) => (((num, []), phi), rule, deps)
416 (name, role_of_tptp_string role, phi, rule, map (rpair []) deps)
418 case role_of_tptp_string role of
421 AAtom (ATerm (("equal", []), _)) =>
422 (* Vampire's equality proxy axiom *)
423 (name, Definition, phi, rule, map (rpair []) deps)
428 (**** PARSING OF SPASS OUTPUT ****)
430 (* SPASS returns clause references of the form "x.y". We ignore "y", whose role
431 is not clear anyway. *)
432 val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
434 val parse_spass_annotations =
435 Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
436 --| Scan.option ($$ ","))) []
438 (* It is not clear why some literals are followed by sequences of stars and/or
439 pluses. We ignore them. *)
440 fun parse_decorated_atom x =
441 (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
443 fun mk_horn ([], []) = AAtom (ATerm (("c_False", []), []))
444 | mk_horn ([], pos_lits) = foldr1 (uncurry (mk_aconn AOr)) pos_lits
445 | mk_horn (neg_lits, []) = mk_anot (foldr1 (uncurry (mk_aconn AAnd)) neg_lits)
446 | mk_horn (neg_lits, pos_lits) =
447 mk_aconn AImplies (foldr1 (uncurry (mk_aconn AAnd)) neg_lits)
448 (foldr1 (uncurry (mk_aconn AOr)) pos_lits)
450 fun parse_horn_clause x =
451 (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
452 -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
453 -- Scan.repeat parse_decorated_atom
454 >> (mk_horn o apfst (op @))) x
456 val parse_spass_debug =
457 Scan.option ($$ "(" |-- Scan.repeat (scan_general_id --| Scan.option ($$ ","))
460 (* Syntax: <num>[0:<inference><annotations>] <atoms> || <atoms> -> <atoms>.
461 derived from formulae <ident>* *)
462 fun parse_spass_line x =
463 (parse_spass_debug |-- scan_general_id --| $$ "[" --|
464 Scan.many1 Symbol.is_digit --| $$ ":" -- Symbol.scan_ascii_id
465 -- parse_spass_annotations --| $$ "]"
466 -- parse_horn_clause --| $$ "."
467 -- Scan.option (Scan.this_string "derived from formulae "
468 |-- Scan.repeat (scan_general_id --| Scan.option ($$ " ")))
469 >> (fn ((((num, rule), deps), u), names) =>
470 ((num, these names), Unknown, u, rule, map (rpair []) deps))) x
472 val satallax_coreN = "__satallax_core" (* arbitrary *)
473 val z3_tptp_coreN = "__z3_tptp_core" (* arbitrary *)
475 (* Syntax: core(<name>,[<name>,...,<name>]). *)
476 fun parse_z3_tptp_line x =
477 (scan_general_id --| $$ "," --| $$ "[" -- parse_dependencies --| $$ "]"
478 >> (fn (name, names) =>
479 (("", name :: names), Unknown, dummy_phi, z3_tptp_coreN, []))) x
482 fun parse_satallax_line x =
483 (scan_general_id --| Scan.option ($$ " ")
484 >> (fn s => ((s, [s]), Unknown, dummy_phi, satallax_coreN, []))) x
486 fun parse_line problem =
487 parse_tstp_line problem || parse_spass_line || parse_z3_tptp_line
488 || parse_satallax_line
489 fun parse_proof problem =
490 strip_spaces_except_between_idents
492 #> Scan.error (!! (fn _ => raise UNRECOGNIZED_ATP_PROOF ())
493 (Scan.finite Symbol.stopper
494 (Scan.repeat1 (parse_line problem))))
497 fun atp_proof_from_tstplike_proof _ "" = []
498 | atp_proof_from_tstplike_proof problem tstp =
499 tstp ^ "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
500 |> parse_proof problem
501 |> sort (step_name_ord o pairself #1)
503 fun clean_up_dependencies _ [] = []
504 | clean_up_dependencies seen ((name, role, u, rule, deps) :: steps) =
505 (name, role, u, rule,
506 map_filter (fn dep => find_first (is_same_atp_step dep) seen) deps) ::
507 clean_up_dependencies (name :: seen) steps
509 fun clean_up_atp_proof_dependencies proof = clean_up_dependencies [] proof
511 fun map_term_names_in_atp_proof f =
513 fun do_term (ATerm ((s, tys), ts)) = ATerm ((f s, tys), map do_term ts)
514 fun do_formula (AQuant (q, xs, phi)) =
515 AQuant (q, map (apfst f) xs, do_formula phi)
516 | do_formula (AConn (c, phis)) = AConn (c, map do_formula phis)
517 | do_formula (AAtom t) = AAtom (do_term t)
518 fun do_step (name, role, phi, rule, deps) =
519 (name, role, do_formula phi, rule, deps)
522 fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
524 fun nasty_atp_proof pool =
525 not (Symtab.is_empty pool) ? map_term_names_in_atp_proof (nasty_name pool)