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/Vampire/SPASS syntax.
11 type 'a fo_term = 'a ATP_Problem.fo_term
12 type 'a uniform_formula = 'a ATP_Problem.uniform_formula
15 Unprovable | IncompleteUnprovable | ProofMissing | CantConnect | TimedOut |
16 OutOfResources | SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl |
17 NoRealZ3 | MalformedInput | MalformedOutput | Interrupted | Crashed |
18 InternalError | UnknownError of string
20 type step_name = string * string option
23 Definition of step_name * 'a * 'a |
24 Inference of step_name * 'a * step_name list
26 type 'a proof = 'a uniform_formula step list
28 val strip_spaces : (char -> bool) -> string -> string
29 val short_output : bool -> string -> string
30 val string_for_failure : failure -> string
31 val extract_important_message : string -> string
32 val extract_known_failure :
33 (failure * string) list -> string -> failure option
34 val extract_tstplike_proof_and_outcome :
35 bool -> bool -> int -> (string * string) list -> (failure * string) list
36 -> string -> string * failure option
37 val is_same_step : step_name * step_name -> bool
38 val atp_proof_from_tstplike_string : bool -> string -> string proof
39 val map_term_names_in_atp_proof :
40 (string -> string) -> string proof -> string proof
41 val nasty_atp_proof : string Symtab.table -> string proof -> string proof
44 structure ATP_Proof : ATP_PROOF =
50 Unprovable | IncompleteUnprovable | ProofMissing | CantConnect | TimedOut |
51 OutOfResources | SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl |
52 NoRealZ3 | MalformedInput | MalformedOutput | Interrupted | Crashed |
53 InternalError | UnknownError of string
55 fun strip_spaces_in_list _ [] = []
56 | strip_spaces_in_list _ [c1] = if Char.isSpace c1 then [] else [str c1]
57 | strip_spaces_in_list is_evil [c1, c2] =
58 strip_spaces_in_list is_evil [c1] @ strip_spaces_in_list is_evil [c2]
59 | strip_spaces_in_list is_evil (c1 :: c2 :: c3 :: cs) =
60 if Char.isSpace c1 then
61 strip_spaces_in_list is_evil (c2 :: c3 :: cs)
62 else if Char.isSpace c2 then
63 if Char.isSpace c3 then
64 strip_spaces_in_list is_evil (c1 :: c3 :: cs)
66 str c1 :: (if forall is_evil [c1, c3] then [" "] else []) @
67 strip_spaces_in_list is_evil (c3 :: cs)
69 str c1 :: strip_spaces_in_list is_evil (c2 :: c3 :: cs)
70 fun strip_spaces is_evil =
71 implode o strip_spaces_in_list is_evil o String.explode
73 fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
74 val strip_spaces_except_between_ident_chars = strip_spaces is_ident_char
76 fun elide_string threshold s =
77 if size s > threshold then
78 String.extract (s, 0, SOME (threshold div 2 - 5)) ^ " ...... " ^
79 String.extract (s, size s - (threshold + 1) div 2 + 6, NONE)
82 fun short_output verbose output =
83 if verbose then elide_string 1000 output else ""
85 val missing_message_tail =
86 " appears to be missing. You will need to install it if you want to invoke \
89 fun string_for_failure Unprovable =
90 "The problem is unprovable."
91 | string_for_failure IncompleteUnprovable =
93 | string_for_failure ProofMissing =
94 "The prover claims the conjecture is a theorem but did not provide a proof."
95 | string_for_failure CantConnect = "Cannot connect to remote server."
96 | string_for_failure TimedOut = "Timed out."
97 | string_for_failure OutOfResources = "The prover ran out of resources."
98 | string_for_failure SpassTooOld =
99 "Isabelle requires a more recent version of SPASS with support for the \
100 \TPTP syntax. To install it, download and extract the package \
101 \\"http://isabelle.in.tum.de/dist/contrib/spass-3.7.tar.gz\" and add the \
102 \\"spass-3.7\" directory's absolute path to " ^
103 quote (Path.implode (Path.expand (Path.appends
104 (Path.variable "ISABELLE_HOME_USER" ::
105 map Path.basic ["etc", "components"])))) ^
106 " on a line of its own."
107 | string_for_failure VampireTooOld =
108 "Isabelle requires a more recent version of Vampire. To install it, follow \
109 \the instructions from the Sledgehammer manual (\"isabelle doc\
111 | string_for_failure NoPerl = "Perl" ^ missing_message_tail
112 | string_for_failure NoLibwwwPerl =
113 "The Perl module \"libwww-perl\"" ^ missing_message_tail
114 | string_for_failure NoRealZ3 =
115 "The environment variable \"Z3_REAL_SOLVER\" must be set to Z3's full path."
116 | string_for_failure MalformedInput =
117 "The generated problem is malformed. Please report this to the Isabelle \
119 | string_for_failure MalformedOutput = "The prover output is malformed."
120 | string_for_failure Crashed = "The prover crashed."
121 | string_for_failure InternalError = "An internal prover error occurred."
122 | string_for_failure (UnknownError string) =
123 "A prover error occurred" ^
124 (if string = "" then ". (Pass the \"verbose\" option for details.)"
127 fun extract_delimited (begin_delim, end_delim) output =
128 output |> first_field begin_delim |> the |> snd
129 |> first_field end_delim |> the |> fst
130 |> first_field "\n" |> the |> snd
131 handle Option.Option => ""
133 val tstp_important_message_delims =
134 ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
136 fun extract_important_message output =
137 case extract_delimited tstp_important_message_delims output of
139 | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
140 |> map (perhaps (try (unprefix "%")))
141 |> map (perhaps (try (unprefix " ")))
142 |> space_implode "\n " |> quote
144 (* Splits by the first possible of a list of delimiters. *)
145 fun extract_tstplike_proof delims output =
146 case pairself (find_first (fn s => String.isSubstring s output))
147 (ListPair.unzip delims) of
148 (SOME begin_delim, SOME end_delim) =>
149 extract_delimited (begin_delim, end_delim) output
152 fun extract_known_failure known_failures output =
154 |> find_first (fn (_, pattern) => String.isSubstring pattern output)
157 fun extract_tstplike_proof_and_outcome verbose complete res_code proof_delims
158 known_failures output =
159 case extract_known_failure known_failures output of
160 NONE => (case extract_tstplike_proof proof_delims output of
161 "" => ("", SOME (if res_code = 0 andalso output = "" then
164 UnknownError (short_output verbose output)))
166 if res_code = 0 then (tstplike_proof, NONE)
167 else ("", SOME (UnknownError (short_output verbose output))))
169 ("", SOME (if failure = IncompleteUnprovable andalso complete then
174 fun mk_anot (AConn (ANot, [phi])) = phi
175 | mk_anot phi = AConn (ANot, [phi])
176 fun mk_aconn c (phi1, phi2) = AConn (c, [phi1, phi2])
178 type step_name = string * string option
180 fun is_same_step p = p |> pairself fst |> op =
182 fun step_name_ord p =
183 let val q = pairself fst p in
184 (* The "unprefix" part is to cope with remote Vampire's output. The proper
185 solution would be to perform a topological sort, e.g. using the nice
187 case pairself (Int.fromString o perhaps (try (unprefix "f"))) q of
188 (NONE, NONE) => string_ord q
189 | (NONE, SOME _) => LESS
190 | (SOME _, NONE) => GREATER
191 | (SOME i, SOME j) => int_ord (i, j)
195 Definition of step_name * 'a * 'a |
196 Inference of step_name * 'a * step_name list
198 type 'a proof = 'a uniform_formula step list
200 fun step_name (Definition (name, _, _)) = name
201 | step_name (Inference (name, _, _)) = name
203 (**** PARSING OF TSTP FORMAT ****)
205 (*Strings enclosed in single quotes, e.g. filenames*)
206 val scan_general_id =
207 $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
208 || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
209 >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
211 (* Generalized first-order terms, which include file names, numbers, etc. *)
212 fun parse_annotation strict x =
213 ((scan_general_id ::: Scan.repeat ($$ " " |-- scan_general_id)
214 >> (strict ? filter (is_some o Int.fromString)))
215 -- Scan.optional (parse_annotation strict) [] >> op @
216 || $$ "(" |-- parse_annotations strict --| $$ ")"
217 || $$ "[" |-- parse_annotations strict --| $$ "]") x
218 and parse_annotations strict x =
219 (Scan.optional (parse_annotation strict
220 ::: Scan.repeat ($$ "," |-- parse_annotation strict)) []
223 (* Vampire proof lines sometimes contain needless information such as "(0:3)",
224 which can be hard to disambiguate from function application in an LL(1)
225 parser. As a workaround, we extend the TPTP term syntax with such detritus
227 fun parse_vampire_detritus x =
228 (scan_general_id |-- $$ ":" --| scan_general_id >> K []) x
232 -- Scan.optional ($$ "(" |-- (parse_vampire_detritus || parse_terms)
234 --| Scan.optional ($$ "(" |-- parse_vampire_detritus --| $$ ")") []
236 and parse_terms x = (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
239 (parse_term -- Scan.option (Scan.option ($$ "!") --| $$ "=" -- parse_term)
240 >> (fn (u1, NONE) => AAtom u1
241 | (u1, SOME (NONE, u2)) => AAtom (ATerm ("c_equal", [u1, u2]))
242 | (u1, SOME (SOME _, u2)) =>
243 mk_anot (AAtom (ATerm ("c_equal", [u1, u2]))))) x
245 fun fo_term_head (ATerm (s, _)) = s
247 (* TPTP formulas are fully parenthesized, so we don't need to worry about
248 operator precedence. *)
249 fun parse_formula x =
250 (($$ "(" |-- parse_formula --| $$ ")"
251 || ($$ "!" >> K AForall || $$ "?" >> K AExists)
252 --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_formula
253 >> (fn ((q, ts), phi) => AQuant (q, map fo_term_head ts, phi))
254 || $$ "~" |-- parse_formula >> mk_anot
256 -- Scan.option ((Scan.this_string "=>" >> K AImplies
257 || Scan.this_string "<=>" >> K AIff
258 || Scan.this_string "<~>" >> K ANotIff
259 || Scan.this_string "<=" >> K AIf
260 || $$ "|" >> K AOr || $$ "&" >> K AAnd)
262 >> (fn (phi1, NONE) => phi1
263 | (phi1, SOME (c, phi2)) => mk_aconn c (phi1, phi2))) x
265 val parse_tstp_extra_arguments =
266 Scan.optional ($$ "," |-- parse_annotation false
267 --| Scan.option ($$ "," |-- parse_annotations false)) []
269 val vampire_unknown_fact = "unknown"
271 (* Syntax: (fof|cnf)\(<num>, <formula_role>, <formula> <extra_arguments>\).
272 The <num> could be an identifier, but we assume integers. *)
273 val parse_tstp_line =
274 ((Scan.this_string "fof" || Scan.this_string "cnf") -- $$ "(")
275 |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
276 -- parse_formula -- parse_tstp_extra_arguments --| $$ ")" --| $$ "."
277 >> (fn (((num, role), phi), deps) =>
282 ((num, if s = vampire_unknown_fact then NONE else SOME s), [])
283 | _ => ((num, NONE), deps)
288 AConn (AIff, [phi1 as AAtom _, phi2]) =>
289 Definition (name, phi1, phi2)
290 | AAtom (ATerm ("c_equal", _)) =>
291 (* Vampire's equality proxy axiom *)
292 Inference (name, phi, map (rpair NONE) deps)
293 | _ => raise Fail "malformed definition")
294 | _ => Inference (name, phi, map (rpair NONE) deps)
297 (**** PARSING OF VAMPIRE OUTPUT ****)
299 val parse_vampire_braced_stuff =
300 $$ "{" -- Scan.repeat (scan_general_id --| Scan.option ($$ ",")) -- $$ "}"
301 val parse_vampire_parenthesized_detritus =
302 $$ "(" |-- parse_vampire_detritus --| $$ ")"
304 (* Syntax: <num>. <formula> <annotation> *)
305 val parse_vampire_line =
306 scan_general_id --| $$ "." -- parse_formula
307 --| Scan.option parse_vampire_braced_stuff
308 --| Scan.option parse_vampire_parenthesized_detritus
309 -- parse_annotation true
310 >> (fn ((num, phi), deps) =>
311 Inference ((num, NONE), phi, map (rpair NONE) deps))
313 (**** PARSING OF SPASS OUTPUT ****)
315 (* SPASS returns clause references of the form "x.y". We ignore "y", whose role
316 is not clear anyway. *)
317 val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
319 val parse_spass_annotations =
320 Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
321 --| Scan.option ($$ ","))) []
323 (* It is not clear why some literals are followed by sequences of stars and/or
324 pluses. We ignore them. *)
325 fun parse_decorated_atom x =
326 (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
328 fun mk_horn ([], []) = AAtom (ATerm ("c_False", []))
329 | mk_horn ([], pos_lits) = foldr1 (mk_aconn AOr) pos_lits
330 | mk_horn (neg_lits, []) = mk_anot (foldr1 (mk_aconn AAnd) neg_lits)
331 | mk_horn (neg_lits, pos_lits) =
332 mk_aconn AImplies (foldr1 (mk_aconn AAnd) neg_lits,
333 foldr1 (mk_aconn AOr) pos_lits)
335 fun parse_horn_clause x =
336 (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
337 -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
338 -- Scan.repeat parse_decorated_atom
339 >> (mk_horn o apfst (op @))) x
341 (* Syntax: <num>[0:<inference><annotations>]
342 <atoms> || <atoms> -> <atoms>. *)
343 fun parse_spass_line x =
344 (scan_general_id --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
345 -- parse_spass_annotations --| $$ "]" -- parse_horn_clause --| $$ "."
346 >> (fn ((num, deps), u) =>
347 Inference ((num, NONE), u, map (rpair NONE) deps))) x
349 fun parse_line x = (parse_tstp_line || parse_vampire_line || parse_spass_line) x
351 fst o Scan.finite Symbol.stopper
352 (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
353 (Scan.repeat1 parse_line)))
354 o raw_explode o strip_spaces_except_between_ident_chars
356 fun clean_up_dependency seen dep = find_first (curry is_same_step dep) seen
357 fun clean_up_dependencies _ [] = []
358 | clean_up_dependencies seen ((step as Definition (name, _, _)) :: steps) =
359 step :: clean_up_dependencies (name :: seen) steps
360 | clean_up_dependencies seen (Inference (name, u, deps) :: steps) =
361 Inference (name, u, map_filter (clean_up_dependency seen) deps) ::
362 clean_up_dependencies (name :: seen) steps
364 fun atp_proof_from_tstplike_string clean =
365 suffix "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
367 #> clean ? (sort (step_name_ord o pairself step_name)
368 #> clean_up_dependencies [])
370 fun map_term_names_in_term f (ATerm (s, ts)) =
371 ATerm (f s, map (map_term_names_in_term f) ts)
372 fun map_term_names_in_formula f (AQuant (q, xs, phi)) =
373 AQuant (q, xs, map_term_names_in_formula f phi)
374 | map_term_names_in_formula f (AConn (c, phis)) =
375 AConn (c, map (map_term_names_in_formula f) phis)
376 | map_term_names_in_formula f (AAtom t) = AAtom (map_term_names_in_term f t)
377 fun map_term_names_in_step f (Definition (name, phi1, phi2)) =
378 Definition (name, map_term_names_in_formula f phi1,
379 map_term_names_in_formula f phi2)
380 | map_term_names_in_step f (Inference (name, phi, deps)) =
381 Inference (name, map_term_names_in_formula f phi, deps)
382 fun map_term_names_in_atp_proof f = map (map_term_names_in_step f)
384 fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
385 fun nasty_atp_proof pool =
386 if Symtab.is_empty pool then I
387 else map_term_names_in_atp_proof (nasty_name pool)