1 (* Title: HOL/Tools/ATP/atp_problem.ML
2 Author: Jia Meng, Cambridge University Computer Laboratory and NICTA
3 Author: Jasmin Blanchette, TU Muenchen
5 Abstract representation of ATP problems and TPTP syntax.
8 signature ATP_PROBLEM =
10 datatype ('a, 'b) ho_term =
11 ATerm of 'a * ('a, 'b) ho_term list |
12 AAbs of ('a * 'b) * ('a, 'b) ho_term
13 datatype quantifier = AForall | AExists
14 datatype connective = ANot | AAnd | AOr | AImplies | AIff
15 datatype ('a, 'b, 'c) formula =
16 AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
17 AConn of connective * ('a, 'b, 'c) formula list |
20 datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
22 datatype thf_flavor = Without_Choice | With_Choice
30 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
31 datatype 'a problem_line =
32 Decl of string * 'a * 'a ho_type |
33 Formula of string * formula_kind
34 * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
35 * (string, string ho_type) ho_term option
36 * (string, string ho_type) ho_term option
37 type 'a problem = (string * 'a problem_line list) list
43 val tptp_has_type : string
44 val tptp_type_of_types : string
45 val tptp_bool_type : string
46 val tptp_individual_type : string
47 val tptp_fun_type : string
48 val tptp_product_type : string
49 val tptp_forall : string
50 val tptp_ho_forall : string
51 val tptp_exists : string
52 val tptp_ho_exists : string
53 val tptp_choice : string
57 val tptp_implies : string
60 val tptp_not_iff : string
62 val tptp_not_infix : string
63 val tptp_equal : string
64 val tptp_old_equal : string
65 val tptp_false : string
66 val tptp_true : string
67 val tptp_empty_list : string
68 val is_tptp_equal : string -> bool
69 val is_built_in_tptp_symbol : string -> bool
70 val is_tptp_variable : string -> bool
71 val is_tptp_user_symbol : string -> bool
72 val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
74 connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
75 -> ('a, 'b, 'c) formula
77 bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
80 bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
81 -> connective * 'a list -> ('b, 'c, 'd) formula
83 bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
85 val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
86 val is_format_thf : format -> bool
87 val is_format_typed : format -> bool
88 val tptp_lines_for_atp_problem : format -> string problem -> string list
89 val ensure_cnf_problem :
90 (string * string) problem -> (string * string) problem
91 val filter_cnf_ueq_problem :
92 (string * string) problem -> (string * string) problem
93 val declare_undeclared_syms_in_atp_problem :
94 string -> string -> (string * string) problem -> (string * string) problem
95 val nice_atp_problem :
96 bool -> ('a * (string * string) problem_line list) list
97 -> ('a * string problem_line list) list
98 * (string Symtab.table * string Symtab.table) option
101 structure ATP_Problem : ATP_PROBLEM =
109 datatype ('a, 'b) ho_term =
110 ATerm of 'a * ('a, 'b) ho_term list |
111 AAbs of ('a * 'b) * ('a, 'b) ho_term
112 datatype quantifier = AForall | AExists
113 datatype connective = ANot | AAnd | AOr | AImplies | AIff
114 datatype ('a, 'b, 'c) formula =
115 AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
116 AConn of connective * ('a, 'b, 'c) formula list |
119 datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
121 datatype thf_flavor = Without_Choice | With_Choice
129 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
130 datatype 'a problem_line =
131 Decl of string * 'a * 'a ho_type |
132 Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
133 * (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
134 type 'a problem = (string * 'a problem_line list) list
136 (* official TPTP syntax *)
141 val tptp_has_type = ":"
142 val tptp_type_of_types = "$tType"
143 val tptp_bool_type = "$o"
144 val tptp_individual_type = "$i"
145 val tptp_fun_type = ">"
146 val tptp_product_type = "*"
147 val tptp_forall = "!"
148 val tptp_ho_forall = "!!"
149 val tptp_exists = "?"
150 val tptp_ho_exists = "??"
151 val tptp_choice = "@+"
155 val tptp_implies = "=>"
158 val tptp_not_iff = "<~>"
160 val tptp_not_infix = "!"
162 val tptp_old_equal = "equal"
163 val tptp_false = "$false"
164 val tptp_true = "$true"
165 val tptp_empty_list = "[]"
167 fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
168 fun is_built_in_tptp_symbol s =
169 s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
170 fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
171 val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
173 fun raw_polarities_of_conn ANot = (SOME false, NONE)
174 | raw_polarities_of_conn AAnd = (SOME true, SOME true)
175 | raw_polarities_of_conn AOr = (SOME true, SOME true)
176 | raw_polarities_of_conn AImplies = (SOME false, SOME true)
177 | raw_polarities_of_conn AIff = (NONE, NONE)
178 fun polarities_of_conn NONE = K (NONE, NONE)
179 | polarities_of_conn (SOME pos) =
180 raw_polarities_of_conn #> not pos ? pairself (Option.map not)
182 fun mk_anot (AConn (ANot, [phi])) = phi
183 | mk_anot phi = AConn (ANot, [phi])
184 fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
186 fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
187 | aconn_fold pos f (AImplies, [phi1, phi2]) =
188 f (Option.map not pos) phi1 #> f pos phi2
189 | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
190 | aconn_fold pos f (AOr, phis) = fold (f pos) phis
191 | aconn_fold _ f (_, phis) = fold (f NONE) phis
193 fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
194 | aconn_map pos f (AImplies, [phi1, phi2]) =
195 AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
196 | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
197 | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
198 | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
200 fun formula_fold pos f =
202 fun aux pos (AQuant (_, _, phi)) = aux pos phi
203 | aux pos (AConn conn) = aconn_fold pos aux conn
204 | aux pos (AAtom tm) = f pos tm
207 fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
208 | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
209 | formula_map f (AAtom tm) = AAtom (f tm)
211 fun is_format_thf (THF _) = true
212 | is_format_thf _ = false
213 fun is_format_typed TFF = true
214 | is_format_typed (THF _) = true
215 | is_format_typed _ = false
217 fun string_for_kind Axiom = "axiom"
218 | string_for_kind Definition = "definition"
219 | string_for_kind Lemma = "lemma"
220 | string_for_kind Hypothesis = "hypothesis"
221 | string_for_kind Conjecture = "conjecture"
223 fun strip_tff_type (AFun (AType s, ty)) = strip_tff_type ty |>> cons s
224 | strip_tff_type (AFun (AFun _, _)) =
225 raise Fail "unexpected higher-order type in first-order format"
226 | strip_tff_type (AType s) = ([], s)
228 fun string_for_type (THF _) ty =
230 fun aux _ (AType s) = s
231 | aux rhs (AFun (ty1, ty2)) =
232 aux false ty1 ^ " " ^ tptp_fun_type ^ " " ^ aux true ty2
233 |> not rhs ? enclose "(" ")"
235 | string_for_type TFF ty =
236 (case strip_tff_type ty of
238 | ([s'], s) => s' ^ " " ^ tptp_fun_type ^ " " ^ s
240 "(" ^ space_implode (" " ^ tptp_product_type ^ " ") ss ^ ") " ^
241 tptp_fun_type ^ " " ^ s)
242 | string_for_type _ _ = raise Fail "unexpected type in untyped format"
244 fun string_for_quantifier AForall = tptp_forall
245 | string_for_quantifier AExists = tptp_exists
247 fun string_for_connective ANot = tptp_not
248 | string_for_connective AAnd = tptp_and
249 | string_for_connective AOr = tptp_or
250 | string_for_connective AImplies = tptp_implies
251 | string_for_connective AIff = tptp_iff
253 fun string_for_bound_var format (s, ty) =
254 s ^ (if is_format_typed format then
255 " " ^ tptp_has_type ^ " " ^
256 string_for_type format (ty |> the_default (AType tptp_individual_type))
260 fun string_for_term _ (ATerm (s, [])) = s
261 | string_for_term format (ATerm (s, ts)) =
262 if s = tptp_empty_list then
263 (* used for lists in the optional "source" field of a derivation *)
264 "[" ^ commas (map (string_for_term format) ts) ^ "]"
265 else if is_tptp_equal s then
266 space_implode (" " ^ tptp_equal ^ " ") (map (string_for_term format) ts)
267 |> is_format_thf format ? enclose "(" ")"
269 (case (s = tptp_ho_forall orelse s = tptp_ho_exists,
270 s = tptp_choice andalso format = THF With_Choice, ts) of
271 (true, _, [AAbs ((s', ty), tm)]) =>
272 (* Use syntactic sugar "!" and "?" instead of "!!" and "??" whenever
273 possible, to work around LEO-II 1.2.8 parser limitation. *)
274 string_for_formula format
275 (AQuant (if s = tptp_ho_forall then AForall else AExists,
276 [(s', SOME ty)], AAtom tm))
277 | (_, true, [AAbs ((s', ty), tm)]) =>
278 (*There is code in ATP_Translate to ensure that Eps is always applied
280 tptp_choice ^ "[" ^ s' ^ " : " ^ string_for_type format ty ^ "] : " ^
281 string_for_term format tm ^ ""
285 let val ss = map (string_for_term format) ts in
286 if is_format_thf format then
287 "(" ^ space_implode (" " ^ tptp_app ^ " ") (s :: ss) ^ ")"
289 s ^ "(" ^ commas ss ^ ")"
291 | string_for_term (format as THF _) (AAbs ((s, ty), tm)) =
292 "(^[" ^ s ^ " : " ^ string_for_type format ty ^ "] : " ^
293 string_for_term format tm ^ ")"
294 | string_for_term _ _ = raise Fail "unexpected term in first-order format"
295 and string_for_formula format (AQuant (q, xs, phi)) =
296 string_for_quantifier q ^
297 "[" ^ commas (map (string_for_bound_var format) xs) ^ "] : " ^
298 string_for_formula format phi
300 | string_for_formula format
301 (AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
302 space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
303 (map (string_for_term format) ts)
304 |> is_format_thf format ? enclose "(" ")"
305 | string_for_formula format (AConn (c, [phi])) =
306 string_for_connective c ^ " " ^
307 (string_for_formula format phi |> is_format_thf format ? enclose "(" ")")
309 | string_for_formula format (AConn (c, phis)) =
310 space_implode (" " ^ string_for_connective c ^ " ")
311 (map (string_for_formula format) phis)
313 | string_for_formula format (AAtom tm) = string_for_term format tm
315 fun the_source (SOME source) = source
318 ATerm ("isabelle", []) :: replicate 2 (ATerm ("[]", [])))
320 fun string_for_format CNF = tptp_cnf
321 | string_for_format CNF_UEQ = tptp_cnf
322 | string_for_format FOF = tptp_fof
323 | string_for_format TFF = tptp_tff
324 | string_for_format (THF _) = tptp_thf
326 fun string_for_problem_line format (Decl (ident, sym, ty)) =
327 string_for_format format ^ "(" ^ ident ^ ", type,\n " ^ sym ^ " : " ^
328 string_for_type format ty ^ ").\n"
329 | string_for_problem_line format (Formula (ident, kind, phi, source, info)) =
330 string_for_format format ^ "(" ^ ident ^ ", " ^ string_for_kind kind ^
331 ",\n (" ^ string_for_formula format phi ^ ")" ^
332 (case (source, info) of
334 | (SOME tm, NONE) => ", " ^ string_for_term format tm
336 ", " ^ string_for_term format (the_source source) ^
337 ", " ^ string_for_term format tm) ^ ").\n"
338 fun tptp_lines_for_atp_problem format problem =
339 "% This file was generated by Isabelle (most likely Sledgehammer)\n\
340 \% " ^ timestamp () ^ "\n" ::
341 maps (fn (_, []) => []
342 | (heading, lines) =>
343 "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
344 map (string_for_problem_line format) lines)
348 (** CNF (Metis) and CNF UEQ (Waldmeister) **)
350 fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
351 | is_problem_line_negated _ = false
353 fun is_problem_line_cnf_ueq (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) =
355 | is_problem_line_cnf_ueq _ = false
357 fun open_conjecture_term (ATerm ((s, s'), tms)) =
358 ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
359 else (s, s'), tms |> map open_conjecture_term)
360 | open_conjecture_term _ = raise Fail "unexpected higher-order term"
361 fun open_formula conj =
363 (* We are conveniently assuming that all bound variable names are
364 distinct, which should be the case for the formulas we generate. *)
365 fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
366 | opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
367 | opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
368 | opn pos (AConn (c, [phi1, phi2])) =
369 let val (pos1, pos2) = polarities_of_conn pos c in
370 AConn (c, [opn pos1 phi1, opn pos2 phi2])
372 | opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
374 in opn (SOME (not conj)) end
375 fun open_formula_line (Formula (ident, kind, phi, source, info)) =
376 Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
377 | open_formula_line line = line
379 fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
380 Formula (ident, Hypothesis, mk_anot phi, source, info)
381 | negate_conjecture_line line = line
383 exception CLAUSIFY of unit
385 (* This "clausification" only expands syntactic sugar, such as "phi => psi" to
386 "~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
387 attempt to distribute conjunctions over disjunctions. *)
388 fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
389 | clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
390 | clausify_formula true (AConn (AOr, [phi1, phi2])) =
391 (phi1, phi2) |> pairself (clausify_formula true)
392 |> uncurry (map_product (mk_aconn AOr))
393 | clausify_formula false (AConn (AAnd, [phi1, phi2])) =
394 (phi1, phi2) |> pairself (clausify_formula false)
395 |> uncurry (map_product (mk_aconn AOr))
396 | clausify_formula true (AConn (AImplies, [phi1, phi2])) =
397 clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
398 | clausify_formula true (AConn (AIff, phis)) =
399 clausify_formula true (AConn (AImplies, phis)) @
400 clausify_formula true (AConn (AImplies, rev phis))
401 | clausify_formula _ _ = raise CLAUSIFY ()
403 fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
405 val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
407 map2 (fn phi => fn j =>
408 Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
412 | clausify_formula_line _ = []
414 fun ensure_cnf_problem_line line =
415 line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
417 fun ensure_cnf_problem problem =
418 problem |> map (apsnd (maps ensure_cnf_problem_line))
420 fun filter_cnf_ueq_problem problem =
422 |> map (apsnd (map open_formula_line
423 #> filter is_problem_line_cnf_ueq
424 #> map negate_conjecture_line))
427 val lines = problem |> maps snd
428 val conjs = lines |> filter is_problem_line_negated
429 in if length conjs = 1 andalso conjs <> lines then problem else [] end)
432 (** Symbol declarations **)
434 (* TFF allows implicit declarations of types, function symbols, and predicate
435 symbols (with "$i" as the type of individuals), but some provers (e.g.,
436 SNARK) require explicit declarations. The situation is similar for THF. *)
438 val atype_of_types = AType (`I tptp_type_of_types)
439 val bool_atype = AType (`I tptp_bool_type)
440 val individual_atype = AType (`I tptp_individual_type)
442 fun default_type pred_sym =
444 fun typ 0 = if pred_sym then bool_atype else individual_atype
445 | typ ary = AFun (individual_atype, typ (ary - 1))
448 fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = insert (op =) sym
449 | add_declared_syms_in_problem_line _ = I
450 fun declared_syms_in_problem problem =
451 fold (fold add_declared_syms_in_problem_line o snd) problem []
453 fun undeclared_syms_in_problem declared problem =
456 if member (op =) declared name then I else AList.default (op =) (name, ty)
457 fun do_type (AFun (ty1, ty2)) = fold do_type [ty1, ty2]
458 | do_type (AType name) = do_sym name (K atype_of_types)
459 fun do_term pred_sym (ATerm (name as (s, _), tms)) =
460 is_tptp_user_symbol s
461 ? do_sym name (fn _ => default_type pred_sym (length tms))
462 #> fold (do_term false) tms
463 | do_term _ (AAbs ((_, ty), tm)) = do_type ty #> do_term false tm
464 fun do_formula (AQuant (_, xs, phi)) =
465 fold do_type (map_filter snd xs) #> do_formula phi
466 | do_formula (AConn (_, phis)) = fold do_formula phis
467 | do_formula (AAtom tm) = do_term true tm
468 fun do_problem_line (Decl (_, _, ty)) = do_type ty
469 | do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
471 fold (fold do_problem_line o snd) problem []
472 |> filter_out (is_built_in_tptp_symbol o fst o fst)
475 fun declare_undeclared_syms_in_atp_problem prefix heading problem =
477 fun decl_line (x as (s, _), ty) = Decl (prefix ^ s, x, ty ())
478 val declared = problem |> declared_syms_in_problem
480 problem |> undeclared_syms_in_problem declared
481 |> sort_wrt (fst o fst)
483 in (heading, decls) :: problem end
487 fun empty_name_pool readable_names =
488 if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
490 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
492 pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
497 | skip (#"." :: cs) = skip cs
498 | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
500 | keep (#"." :: cs) = skip cs
501 | keep (c :: cs) = c :: keep cs
502 in String.explode #> rev #> keep #> rev #> String.implode end
504 (* Long names can slow down the ATPs. *)
505 val max_readable_name_size = 20
507 (* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
508 unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
509 ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
510 is still necessary). *)
511 val reserved_nice_names = [tptp_old_equal, "op", "eq"]
513 fun readable_name full_name s =
514 if s = full_name then
518 |> perhaps (try (unprefix "'"))
519 |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
521 if size s > max_readable_name_size then
522 String.substring (s, 0, max_readable_name_size div 2 - 4) ^
523 string_of_int (hash_string full_name) ^
524 String.extract (s, size s - max_readable_name_size div 2 + 4,
528 |> (fn s => if member (op =) reserved_nice_names s then full_name else s)
530 fun nice_name (full_name, _) NONE = (full_name, NONE)
531 | nice_name (full_name, desired_name) (SOME the_pool) =
532 if is_built_in_tptp_symbol full_name then
533 (full_name, SOME the_pool)
534 else case Symtab.lookup (fst the_pool) full_name of
535 SOME nice_name => (nice_name, SOME the_pool)
538 val nice_prefix = readable_name full_name desired_name
542 nice_prefix ^ (if j = 0 then "" else string_of_int j)
544 case Symtab.lookup (snd the_pool) nice_name of
546 if full_name = full_name' then (nice_name, the_pool)
550 (Symtab.update_new (full_name, nice_name) (fst the_pool),
551 Symtab.update_new (nice_name, full_name) (snd the_pool)))
553 in add 0 |> apsnd SOME end
555 fun nice_type (AType name) = nice_name name #>> AType
556 | nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
557 fun nice_term (ATerm (name, ts)) =
558 nice_name name ##>> pool_map nice_term ts #>> ATerm
559 | nice_term (AAbs ((name, ty), tm)) =
560 nice_name name ##>> nice_type ty ##>> nice_term tm #>> AAbs
561 fun nice_formula (AQuant (q, xs, phi)) =
562 pool_map nice_name (map fst xs)
563 ##>> pool_map (fn NONE => pair NONE
564 | SOME ty => nice_type ty #>> SOME) (map snd xs)
565 ##>> nice_formula phi
566 #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
567 | nice_formula (AConn (c, phis)) =
568 pool_map nice_formula phis #>> curry AConn c
569 | nice_formula (AAtom tm) = nice_term tm #>> AAtom
570 fun nice_problem_line (Decl (ident, sym, ty)) =
571 nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
572 | nice_problem_line (Formula (ident, kind, phi, source, info)) =
573 nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
574 fun nice_problem problem =
575 pool_map (fn (heading, lines) =>
576 pool_map nice_problem_line lines #>> pair heading) problem
577 fun nice_atp_problem readable_names problem =
578 nice_problem problem (empty_name_pool readable_names)