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 | CantConnect | TimedOut |
16 OutOfResources | SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl |
17 MalformedInput | MalformedOutput | Interrupted | Crashed | InternalError |
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 string_for_failure : failure -> string
30 val extract_important_message : string -> string
31 val extract_known_failure :
32 (failure * string) list -> string -> failure option
33 val extract_tstplike_proof_and_outcome :
34 bool -> int -> (string * string) list -> (failure * string) list -> string
35 -> string * failure option
36 val is_same_step : step_name * step_name -> bool
37 val atp_proof_from_tstplike_string : string -> string proof
38 val map_term_names_in_atp_proof :
39 (string -> string) -> string proof -> string proof
40 val nasty_atp_proof : string Symtab.table -> string proof -> string proof
43 structure ATP_Proof : ATP_PROOF =
49 Unprovable | IncompleteUnprovable | CantConnect | TimedOut | OutOfResources |
50 SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl | MalformedInput |
51 MalformedOutput | Interrupted | Crashed | InternalError | UnknownError
53 fun strip_spaces_in_list _ [] = []
54 | strip_spaces_in_list _ [c1] = if Char.isSpace c1 then [] else [str c1]
55 | strip_spaces_in_list is_evil [c1, c2] =
56 strip_spaces_in_list is_evil [c1] @ strip_spaces_in_list is_evil [c2]
57 | strip_spaces_in_list is_evil (c1 :: c2 :: c3 :: cs) =
58 if Char.isSpace c1 then
59 strip_spaces_in_list is_evil (c2 :: c3 :: cs)
60 else if Char.isSpace c2 then
61 if Char.isSpace c3 then
62 strip_spaces_in_list is_evil (c1 :: c3 :: cs)
64 str c1 :: (if forall is_evil [c1, c3] then [" "] else []) @
65 strip_spaces_in_list is_evil (c3 :: cs)
67 str c1 :: strip_spaces_in_list is_evil (c2 :: c3 :: cs)
68 fun strip_spaces is_evil =
69 implode o strip_spaces_in_list is_evil o String.explode
71 fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
72 val strip_spaces_except_between_ident_chars = strip_spaces is_ident_char
74 val missing_message_tail =
75 " appears to be missing. You will need to install it if you want to run \
78 fun string_for_failure Unprovable = "The ATP problem is unprovable."
79 | string_for_failure IncompleteUnprovable =
80 "The ATP cannot prove the problem."
81 | string_for_failure CantConnect = "Can't connect to remote server."
82 | string_for_failure TimedOut = "Timed out."
83 | string_for_failure OutOfResources = "The ATP ran out of resources."
84 | string_for_failure SpassTooOld =
85 "Isabelle requires a more recent version of SPASS with support for the \
86 \TPTP syntax. To install it, download and extract the package \
87 \\"http://isabelle.in.tum.de/dist/contrib/spass-3.7.tar.gz\" and add the \
88 \\"spass-3.7\" directory's absolute path to " ^
89 quote (Path.implode (Path.expand (Path.appends
90 (Path.variable "ISABELLE_HOME_USER" ::
91 map Path.basic ["etc", "components"])))) ^
92 " on a line of its own."
93 | string_for_failure VampireTooOld =
94 "Isabelle requires a more recent version of Vampire. To install it, follow \
95 \the instructions from the Sledgehammer manual (\"isabelle doc\
97 | string_for_failure NoPerl = "Perl" ^ missing_message_tail
98 | string_for_failure NoLibwwwPerl =
99 "The Perl module \"libwww-perl\"" ^ missing_message_tail
100 | string_for_failure MalformedInput =
101 "The ATP problem is malformed. Please report this to the Isabelle \
103 | string_for_failure MalformedOutput = "The ATP output is malformed."
104 | string_for_failure Interrupted = "The ATP was interrupted."
105 | string_for_failure Crashed = "The ATP crashed."
106 | string_for_failure InternalError = "An internal ATP error occurred."
107 | string_for_failure UnknownError = "An unknown ATP error occurred."
109 fun extract_delimited (begin_delim, end_delim) output =
110 output |> first_field begin_delim |> the |> snd
111 |> first_field end_delim |> the |> fst
112 |> first_field "\n" |> the |> snd
113 handle Option.Option => ""
115 val tstp_important_message_delims =
116 ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
118 fun extract_important_message output =
119 case extract_delimited tstp_important_message_delims output of
121 | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
122 |> map (perhaps (try (unprefix "%")))
123 |> map (perhaps (try (unprefix " ")))
124 |> space_implode "\n " |> quote
126 (* Splits by the first possible of a list of delimiters. *)
127 fun extract_tstplike_proof delims output =
128 case pairself (find_first (fn s => String.isSubstring s output))
129 (ListPair.unzip delims) of
130 (SOME begin_delim, SOME end_delim) =>
131 extract_delimited (begin_delim, end_delim) output
134 fun extract_known_failure known_failures output =
136 |> find_first (fn (_, pattern) => String.isSubstring pattern output)
139 fun extract_tstplike_proof_and_outcome complete res_code proof_delims
140 known_failures output =
141 case extract_known_failure known_failures output of
142 NONE => (case extract_tstplike_proof proof_delims output of
143 "" => ("", SOME (if res_code = 0 andalso output = "" then
147 | tstplike_proof => if res_code = 0 then (tstplike_proof, NONE)
148 else ("", SOME UnknownError))
150 ("", SOME (if failure = IncompleteUnprovable andalso complete then
155 fun mk_anot (AConn (ANot, [phi])) = phi
156 | mk_anot phi = AConn (ANot, [phi])
157 fun mk_aconn c (phi1, phi2) = AConn (c, [phi1, phi2])
159 type step_name = string * string option
161 fun is_same_step p = p |> pairself fst |> op =
163 fun step_name_ord p =
164 let val q = pairself fst p in
165 (* The "unprefix" part is to cope with remote Vampire's output. The proper
166 solution would be to perform a topological sort, e.g. using the nice
168 case pairself (Int.fromString o perhaps (try (unprefix "f"))) q of
169 (NONE, NONE) => string_ord q
170 | (NONE, SOME _) => LESS
171 | (SOME _, NONE) => GREATER
172 | (SOME i, SOME j) => int_ord (i, j)
176 Definition of step_name * 'a * 'a |
177 Inference of step_name * 'a * step_name list
179 type 'a proof = 'a uniform_formula step list
181 fun step_name (Definition (name, _, _)) = name
182 | step_name (Inference (name, _, _)) = name
184 (**** PARSING OF TSTP FORMAT ****)
186 (*Strings enclosed in single quotes, e.g. filenames*)
187 val scan_general_id =
188 $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
189 || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
190 >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
192 (* Generalized first-order terms, which include file names, numbers, etc. *)
193 fun parse_annotation strict x =
194 ((scan_general_id ::: Scan.repeat ($$ " " |-- scan_general_id)
195 >> (strict ? filter (is_some o Int.fromString)))
196 -- Scan.optional (parse_annotation strict) [] >> op @
197 || $$ "(" |-- parse_annotations strict --| $$ ")"
198 || $$ "[" |-- parse_annotations strict --| $$ "]") x
199 and parse_annotations strict x =
200 (Scan.optional (parse_annotation strict
201 ::: Scan.repeat ($$ "," |-- parse_annotation strict)) []
204 (* Vampire proof lines sometimes contain needless information such as "(0:3)",
205 which can be hard to disambiguate from function application in an LL(1)
206 parser. As a workaround, we extend the TPTP term syntax with such detritus
208 fun parse_vampire_detritus x =
209 (scan_general_id |-- $$ ":" --| scan_general_id >> K []) x
213 -- Scan.optional ($$ "(" |-- (parse_vampire_detritus || parse_terms)
215 --| Scan.optional ($$ "(" |-- parse_vampire_detritus --| $$ ")") []
217 and parse_terms x = (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
220 (parse_term -- Scan.option (Scan.option ($$ "!") --| $$ "=" -- parse_term)
221 >> (fn (u1, NONE) => AAtom u1
222 | (u1, SOME (NONE, u2)) => AAtom (ATerm ("c_equal", [u1, u2]))
223 | (u1, SOME (SOME _, u2)) =>
224 mk_anot (AAtom (ATerm ("c_equal", [u1, u2]))))) x
226 fun fo_term_head (ATerm (s, _)) = s
228 (* TPTP formulas are fully parenthesized, so we don't need to worry about
229 operator precedence. *)
230 fun parse_formula x =
231 (($$ "(" |-- parse_formula --| $$ ")"
232 || ($$ "!" >> K AForall || $$ "?" >> K AExists)
233 --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_formula
234 >> (fn ((q, ts), phi) => AQuant (q, map fo_term_head ts, phi))
235 || $$ "~" |-- parse_formula >> mk_anot
237 -- Scan.option ((Scan.this_string "=>" >> K AImplies
238 || Scan.this_string "<=>" >> K AIff
239 || Scan.this_string "<~>" >> K ANotIff
240 || Scan.this_string "<=" >> K AIf
241 || $$ "|" >> K AOr || $$ "&" >> K AAnd)
243 >> (fn (phi1, NONE) => phi1
244 | (phi1, SOME (c, phi2)) => mk_aconn c (phi1, phi2))) x
246 val parse_tstp_extra_arguments =
247 Scan.optional ($$ "," |-- parse_annotation false
248 --| Scan.option ($$ "," |-- parse_annotations false)) []
250 (* Syntax: (fof|cnf)\(<num>, <formula_role>, <formula> <extra_arguments>\).
251 The <num> could be an identifier, but we assume integers. *)
252 val parse_tstp_line =
253 ((Scan.this_string "fof" || Scan.this_string "cnf") -- $$ "(")
254 |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
255 -- parse_formula -- parse_tstp_extra_arguments --| $$ ")" --| $$ "."
256 >> (fn (((num, role), phi), deps) =>
260 ["file", _, s] => ((num, SOME s), [])
261 | _ => ((num, NONE), deps)
266 AConn (AIff, [phi1 as AAtom _, phi2]) =>
267 Definition (name, phi1, phi2)
268 | AAtom (ATerm ("c_equal", _)) =>
269 (* Vampire's equality proxy axiom *)
270 Inference (name, phi, map (rpair NONE) deps)
271 | _ => raise Fail "malformed definition")
272 | _ => Inference (name, phi, map (rpair NONE) deps)
275 (**** PARSING OF VAMPIRE OUTPUT ****)
277 (* Syntax: <num>. <formula> <annotation> *)
278 val parse_vampire_line =
279 scan_general_id --| $$ "." -- parse_formula -- parse_annotation true
280 >> (fn ((num, phi), deps) =>
281 Inference ((num, NONE), phi, map (rpair NONE) deps))
283 (**** PARSING OF SPASS OUTPUT ****)
285 (* SPASS returns clause references of the form "x.y". We ignore "y", whose role
286 is not clear anyway. *)
287 val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
289 val parse_spass_annotations =
290 Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
291 --| Scan.option ($$ ","))) []
293 (* It is not clear why some literals are followed by sequences of stars and/or
294 pluses. We ignore them. *)
295 fun parse_decorated_atom x =
296 (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
298 fun mk_horn ([], []) = AAtom (ATerm ("c_False", []))
299 | mk_horn ([], pos_lits) = foldr1 (mk_aconn AOr) pos_lits
300 | mk_horn (neg_lits, []) = mk_anot (foldr1 (mk_aconn AAnd) neg_lits)
301 | mk_horn (neg_lits, pos_lits) =
302 mk_aconn AImplies (foldr1 (mk_aconn AAnd) neg_lits,
303 foldr1 (mk_aconn AOr) pos_lits)
305 fun parse_horn_clause x =
306 (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
307 -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
308 -- Scan.repeat parse_decorated_atom
309 >> (mk_horn o apfst (op @))) x
311 (* Syntax: <num>[0:<inference><annotations>]
312 <atoms> || <atoms> -> <atoms>. *)
313 fun parse_spass_line x =
314 (scan_general_id --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
315 -- parse_spass_annotations --| $$ "]" -- parse_horn_clause --| $$ "."
316 >> (fn ((num, deps), u) =>
317 Inference ((num, NONE), u, map (rpair NONE) deps))) x
319 fun parse_line x = (parse_tstp_line || parse_vampire_line || parse_spass_line) x
321 fst o Scan.finite Symbol.stopper
322 (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
323 (Scan.repeat1 parse_line)))
324 o explode o strip_spaces_except_between_ident_chars
326 fun clean_up_dependency seen dep = find_first (curry is_same_step dep) seen
327 fun clean_up_dependencies _ [] = []
328 | clean_up_dependencies seen ((step as Definition (name, _, _)) :: steps) =
329 step :: clean_up_dependencies (name :: seen) steps
330 | clean_up_dependencies seen (Inference (name, u, deps) :: steps) =
331 Inference (name, u, map_filter (clean_up_dependency seen) deps) ::
332 clean_up_dependencies (name :: seen) steps
334 val atp_proof_from_tstplike_string =
335 suffix "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
337 #> sort (step_name_ord o pairself step_name)
338 #> clean_up_dependencies []
340 fun map_term_names_in_term f (ATerm (s, ts)) =
341 ATerm (f s, map (map_term_names_in_term f) ts)
342 fun map_term_names_in_formula f (AQuant (q, xs, phi)) =
343 AQuant (q, xs, map_term_names_in_formula f phi)
344 | map_term_names_in_formula f (AConn (c, phis)) =
345 AConn (c, map (map_term_names_in_formula f) phis)
346 | map_term_names_in_formula f (AAtom t) = AAtom (map_term_names_in_term f t)
347 fun map_term_names_in_step f (Definition (name, phi1, phi2)) =
348 Definition (name, map_term_names_in_formula f phi1,
349 map_term_names_in_formula f phi2)
350 | map_term_names_in_step f (Inference (name, phi, deps)) =
351 Inference (name, map_term_names_in_formula f phi, deps)
352 fun map_term_names_in_atp_proof f = map (map_term_names_in_step f)
354 fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
355 fun nasty_atp_proof pool =
356 if Symtab.is_empty pool then I
357 else map_term_names_in_atp_proof (nasty_name pool)