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 tff_flavor = Implicit | Explicit
23 datatype thf_flavor = Without_Choice | With_Choice
31 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
32 datatype 'a problem_line =
33 Decl of string * 'a * 'a ho_type |
34 Formula of string * formula_kind
35 * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
36 * (string, string ho_type) ho_term option
37 * (string, string ho_type) ho_term option
38 type 'a problem = (string * 'a problem_line list) list
44 val tptp_has_type : string
45 val tptp_type_of_types : string
46 val tptp_bool_type : string
47 val tptp_individual_type : string
48 val tptp_fun_type : string
49 val tptp_product_type : string
50 val tptp_forall : string
51 val tptp_ho_forall : string
52 val tptp_exists : string
53 val tptp_ho_exists : string
54 val tptp_choice : string
58 val tptp_implies : string
61 val tptp_not_iff : string
63 val tptp_not_infix : string
64 val tptp_equal : string
65 val tptp_old_equal : string
66 val tptp_false : string
67 val tptp_true : string
68 val tptp_empty_list : string
69 val is_tptp_equal : string -> bool
70 val is_built_in_tptp_symbol : string -> bool
71 val is_tptp_variable : string -> bool
72 val is_tptp_user_symbol : string -> bool
73 val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
75 connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
76 -> ('a, 'b, 'c) formula
78 bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
81 bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
82 -> connective * 'a list -> ('b, 'c, 'd) formula
84 bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
86 val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
87 val is_format_thf : format -> bool
88 val is_format_typed : format -> bool
89 val tptp_lines_for_atp_problem : format -> string problem -> string list
90 val ensure_cnf_problem :
91 (string * string) problem -> (string * string) problem
92 val filter_cnf_ueq_problem :
93 (string * string) problem -> (string * string) problem
94 val declare_undeclared_syms_in_atp_problem :
95 string -> string -> (string * string) problem -> (string * string) problem
96 val nice_atp_problem :
97 bool -> ('a * (string * string) problem_line list) list
98 -> ('a * string problem_line list) list
99 * (string Symtab.table * string Symtab.table) option
102 structure ATP_Problem : ATP_PROBLEM =
110 datatype ('a, 'b) ho_term =
111 ATerm of 'a * ('a, 'b) ho_term list |
112 AAbs of ('a * 'b) * ('a, 'b) ho_term
113 datatype quantifier = AForall | AExists
114 datatype connective = ANot | AAnd | AOr | AImplies | AIff
115 datatype ('a, 'b, 'c) formula =
116 AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
117 AConn of connective * ('a, 'b, 'c) formula list |
120 datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
122 datatype tff_flavor = Implicit | Explicit
123 datatype thf_flavor = Without_Choice | With_Choice
132 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
133 datatype 'a problem_line =
134 Decl of string * 'a * 'a ho_type |
135 Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
136 * (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
137 type 'a problem = (string * 'a problem_line list) list
139 (* official TPTP syntax *)
144 val tptp_has_type = ":"
145 val tptp_type_of_types = "$tType"
146 val tptp_bool_type = "$o"
147 val tptp_individual_type = "$i"
148 val tptp_fun_type = ">"
149 val tptp_product_type = "*"
150 val tptp_forall = "!"
151 val tptp_ho_forall = "!!"
152 val tptp_exists = "?"
153 val tptp_ho_exists = "??"
154 val tptp_choice = "@+"
158 val tptp_implies = "=>"
161 val tptp_not_iff = "<~>"
163 val tptp_not_infix = "!"
165 val tptp_old_equal = "equal"
166 val tptp_false = "$false"
167 val tptp_true = "$true"
168 val tptp_empty_list = "[]"
170 fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
171 fun is_built_in_tptp_symbol s =
172 s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
173 fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
174 val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
176 fun raw_polarities_of_conn ANot = (SOME false, NONE)
177 | raw_polarities_of_conn AAnd = (SOME true, SOME true)
178 | raw_polarities_of_conn AOr = (SOME true, SOME true)
179 | raw_polarities_of_conn AImplies = (SOME false, SOME true)
180 | raw_polarities_of_conn AIff = (NONE, NONE)
181 fun polarities_of_conn NONE = K (NONE, NONE)
182 | polarities_of_conn (SOME pos) =
183 raw_polarities_of_conn #> not pos ? pairself (Option.map not)
185 fun mk_anot (AConn (ANot, [phi])) = phi
186 | mk_anot phi = AConn (ANot, [phi])
187 fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
189 fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
190 | aconn_fold pos f (AImplies, [phi1, phi2]) =
191 f (Option.map not pos) phi1 #> f pos phi2
192 | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
193 | aconn_fold pos f (AOr, phis) = fold (f pos) phis
194 | aconn_fold _ f (_, phis) = fold (f NONE) phis
196 fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
197 | aconn_map pos f (AImplies, [phi1, phi2]) =
198 AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
199 | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
200 | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
201 | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
203 fun formula_fold pos f =
205 fun fld pos (AQuant (_, _, phi)) = fld pos phi
206 | fld pos (AConn conn) = aconn_fold pos fld conn
207 | fld pos (AAtom tm) = f pos tm
210 fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
211 | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
212 | formula_map f (AAtom tm) = AAtom (f tm)
214 fun is_format_thf (THF _) = true
215 | is_format_thf _ = false
216 fun is_format_typed (TFF _) = true
217 | is_format_typed (THF _) = true
218 | is_format_typed _ = false
220 fun string_for_kind Axiom = "axiom"
221 | string_for_kind Definition = "definition"
222 | string_for_kind Lemma = "lemma"
223 | string_for_kind Hypothesis = "hypothesis"
224 | string_for_kind Conjecture = "conjecture"
226 fun strip_tff_type (AFun (AType s, ty)) = strip_tff_type ty |>> cons s
227 | strip_tff_type (AFun (AFun _, _)) =
228 raise Fail "unexpected higher-order type in first-order format"
229 | strip_tff_type (AType s) = ([], s)
231 fun string_for_type (THF _) ty =
233 fun aux _ (AType s) = s
234 | aux rhs (AFun (ty1, ty2)) =
235 aux false ty1 ^ " " ^ tptp_fun_type ^ " " ^ aux true ty2
236 |> not rhs ? enclose "(" ")"
238 | string_for_type (TFF _) ty =
239 (case strip_tff_type ty of
241 | ([s'], s) => s' ^ " " ^ tptp_fun_type ^ " " ^ s
243 "(" ^ space_implode (" " ^ tptp_product_type ^ " ") ss ^ ") " ^
244 tptp_fun_type ^ " " ^ s)
245 | string_for_type _ _ = raise Fail "unexpected type in untyped format"
247 fun string_for_quantifier AForall = tptp_forall
248 | string_for_quantifier AExists = tptp_exists
250 fun string_for_connective ANot = tptp_not
251 | string_for_connective AAnd = tptp_and
252 | string_for_connective AOr = tptp_or
253 | string_for_connective AImplies = tptp_implies
254 | string_for_connective AIff = tptp_iff
256 fun string_for_bound_var format (s, ty) =
257 s ^ (if is_format_typed format then
258 " " ^ tptp_has_type ^ " " ^
259 string_for_type format (ty |> the_default (AType tptp_individual_type))
263 fun string_for_term _ (ATerm (s, [])) = s
264 | string_for_term format (ATerm (s, ts)) =
265 if s = tptp_empty_list then
266 (* used for lists in the optional "source" field of a derivation *)
267 "[" ^ commas (map (string_for_term format) ts) ^ "]"
268 else if is_tptp_equal s then
269 space_implode (" " ^ tptp_equal ^ " ") (map (string_for_term format) ts)
270 |> is_format_thf format ? enclose "(" ")"
272 (case (s = tptp_ho_forall orelse s = tptp_ho_exists,
273 s = tptp_choice andalso format = THF With_Choice, ts) of
274 (true, _, [AAbs ((s', ty), tm)]) =>
275 (* Use syntactic sugar "!" and "?" instead of "!!" and "??" whenever
276 possible, to work around LEO-II 1.2.8 parser limitation. *)
277 string_for_formula format
278 (AQuant (if s = tptp_ho_forall then AForall else AExists,
279 [(s', SOME ty)], AAtom tm))
280 | (_, true, [AAbs ((s', ty), tm)]) =>
281 (*There is code in ATP_Translate to ensure that Eps is always applied
283 tptp_choice ^ "[" ^ s' ^ " : " ^ string_for_type format ty ^ "] : " ^
284 string_for_term format tm ^ ""
288 let val ss = map (string_for_term format) ts in
289 if is_format_thf format then
290 "(" ^ space_implode (" " ^ tptp_app ^ " ") (s :: ss) ^ ")"
292 s ^ "(" ^ commas ss ^ ")"
294 | string_for_term (format as THF _) (AAbs ((s, ty), tm)) =
295 "(^[" ^ s ^ " : " ^ string_for_type format ty ^ "] : " ^
296 string_for_term format tm ^ ")"
297 | string_for_term _ _ = raise Fail "unexpected term in first-order format"
298 and string_for_formula format (AQuant (q, xs, phi)) =
299 string_for_quantifier q ^
300 "[" ^ commas (map (string_for_bound_var format) xs) ^ "] : " ^
301 string_for_formula format phi
303 | string_for_formula format
304 (AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
305 space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
306 (map (string_for_term format) ts)
307 |> is_format_thf format ? enclose "(" ")"
308 | string_for_formula format (AConn (c, [phi])) =
309 string_for_connective c ^ " " ^
310 (string_for_formula format phi |> is_format_thf format ? enclose "(" ")")
312 | string_for_formula format (AConn (c, phis)) =
313 space_implode (" " ^ string_for_connective c ^ " ")
314 (map (string_for_formula format) phis)
316 | string_for_formula format (AAtom tm) = string_for_term format tm
318 fun the_source (SOME source) = source
321 ATerm ("isabelle", []) :: replicate 2 (ATerm ("[]", [])))
323 fun string_for_format CNF = tptp_cnf
324 | string_for_format CNF_UEQ = tptp_cnf
325 | string_for_format FOF = tptp_fof
326 | string_for_format (TFF _) = tptp_tff
327 | string_for_format (THF _) = tptp_thf
329 fun string_for_problem_line format (Decl (ident, sym, ty)) =
330 string_for_format format ^ "(" ^ ident ^ ", type,\n " ^ sym ^ " : " ^
331 string_for_type format ty ^ ").\n"
332 | string_for_problem_line format (Formula (ident, kind, phi, source, info)) =
333 string_for_format format ^ "(" ^ ident ^ ", " ^ string_for_kind kind ^
334 ",\n (" ^ string_for_formula format phi ^ ")" ^
335 (case (source, info) of
337 | (SOME tm, NONE) => ", " ^ string_for_term format tm
339 ", " ^ string_for_term format (the_source source) ^
340 ", " ^ string_for_term format tm) ^ ").\n"
341 fun tptp_lines_for_atp_problem format problem =
342 "% This file was generated by Isabelle (most likely Sledgehammer)\n\
343 \% " ^ timestamp () ^ "\n" ::
344 maps (fn (_, []) => []
345 | (heading, lines) =>
346 "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
347 map (string_for_problem_line format) lines)
351 (** CNF (Metis) and CNF UEQ (Waldmeister) **)
353 fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
354 | is_problem_line_negated _ = false
356 fun is_problem_line_cnf_ueq (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) =
358 | is_problem_line_cnf_ueq _ = false
360 fun open_conjecture_term (ATerm ((s, s'), tms)) =
361 ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
362 else (s, s'), tms |> map open_conjecture_term)
363 | open_conjecture_term _ = raise Fail "unexpected higher-order term"
364 fun open_formula conj =
366 (* We are conveniently assuming that all bound variable names are
367 distinct, which should be the case for the formulas we generate. *)
368 fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
369 | opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
370 | opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
371 | opn pos (AConn (c, [phi1, phi2])) =
372 let val (pos1, pos2) = polarities_of_conn pos c in
373 AConn (c, [opn pos1 phi1, opn pos2 phi2])
375 | opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
377 in opn (SOME (not conj)) end
378 fun open_formula_line (Formula (ident, kind, phi, source, info)) =
379 Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
380 | open_formula_line line = line
382 fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
383 Formula (ident, Hypothesis, mk_anot phi, source, info)
384 | negate_conjecture_line line = line
386 exception CLAUSIFY of unit
388 (* This "clausification" only expands syntactic sugar, such as "phi => psi" to
389 "~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
390 attempt to distribute conjunctions over disjunctions. *)
391 fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
392 | clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
393 | clausify_formula true (AConn (AOr, [phi1, phi2])) =
394 (phi1, phi2) |> pairself (clausify_formula true)
395 |> uncurry (map_product (mk_aconn AOr))
396 | clausify_formula false (AConn (AAnd, [phi1, phi2])) =
397 (phi1, phi2) |> pairself (clausify_formula false)
398 |> uncurry (map_product (mk_aconn AOr))
399 | clausify_formula true (AConn (AImplies, [phi1, phi2])) =
400 clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
401 | clausify_formula true (AConn (AIff, phis)) =
402 clausify_formula true (AConn (AImplies, phis)) @
403 clausify_formula true (AConn (AImplies, rev phis))
404 | clausify_formula _ _ = raise CLAUSIFY ()
406 fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
408 val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
410 map2 (fn phi => fn j =>
411 Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
415 | clausify_formula_line _ = []
417 fun ensure_cnf_problem_line line =
418 line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
420 fun ensure_cnf_problem problem =
421 problem |> map (apsnd (maps ensure_cnf_problem_line))
423 fun filter_cnf_ueq_problem problem =
425 |> map (apsnd (map open_formula_line
426 #> filter is_problem_line_cnf_ueq
427 #> map negate_conjecture_line))
430 val lines = problem |> maps snd
431 val conjs = lines |> filter is_problem_line_negated
432 in if length conjs = 1 andalso conjs <> lines then problem else [] end)
435 (** Symbol declarations **)
437 (* TFF allows implicit declarations of types, function symbols, and predicate
438 symbols (with "$i" as the type of individuals), but some provers (e.g.,
439 SNARK) require explicit declarations. The situation is similar for THF. *)
441 val atype_of_types = AType (`I tptp_type_of_types)
442 val bool_atype = AType (`I tptp_bool_type)
443 val individual_atype = AType (`I tptp_individual_type)
445 fun default_type pred_sym =
447 fun typ 0 = if pred_sym then bool_atype else individual_atype
448 | typ ary = AFun (individual_atype, typ (ary - 1))
451 fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = insert (op =) sym
452 | add_declared_syms_in_problem_line _ = I
453 fun declared_syms_in_problem problem =
454 fold (fold add_declared_syms_in_problem_line o snd) problem []
456 fun undeclared_syms_in_problem declared problem =
459 if member (op =) declared name then I else AList.default (op =) (name, ty)
460 fun do_type (AFun (ty1, ty2)) = fold do_type [ty1, ty2]
461 | do_type (AType name) = do_sym name (K atype_of_types)
462 fun do_term pred_sym (ATerm (name as (s, _), tms)) =
463 is_tptp_user_symbol s
464 ? do_sym name (fn _ => default_type pred_sym (length tms))
465 #> fold (do_term false) tms
466 | do_term _ (AAbs ((_, ty), tm)) = do_type ty #> do_term false tm
467 fun do_formula (AQuant (_, xs, phi)) =
468 fold do_type (map_filter snd xs) #> do_formula phi
469 | do_formula (AConn (_, phis)) = fold do_formula phis
470 | do_formula (AAtom tm) = do_term true tm
471 fun do_problem_line (Decl (_, _, ty)) = do_type ty
472 | do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
474 fold (fold do_problem_line o snd) problem []
475 |> filter_out (is_built_in_tptp_symbol o fst o fst)
478 fun declare_undeclared_syms_in_atp_problem prefix heading problem =
480 fun decl_line (x as (s, _), ty) = Decl (prefix ^ s, x, ty ())
481 val declared = problem |> declared_syms_in_problem
483 problem |> undeclared_syms_in_problem declared
484 |> sort_wrt (fst o fst)
486 in (heading, decls) :: problem end
490 fun empty_name_pool readable_names =
491 if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
493 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
495 pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
500 | skip (#"." :: cs) = skip cs
501 | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
503 | keep (#"." :: cs) = skip cs
504 | keep (c :: cs) = c :: keep cs
505 in String.explode #> rev #> keep #> rev #> String.implode end
507 (* Long names can slow down the ATPs. *)
508 val max_readable_name_size = 20
510 (* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
511 unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
512 ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
513 is still necessary). *)
514 val reserved_nice_names = [tptp_old_equal, "op", "eq"]
516 fun readable_name full_name s =
517 if s = full_name then
521 |> perhaps (try (unprefix "'"))
522 |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
524 if size s > max_readable_name_size then
525 String.substring (s, 0, max_readable_name_size div 2 - 4) ^
526 string_of_int (hash_string full_name) ^
527 String.extract (s, size s - max_readable_name_size div 2 + 4,
531 |> (fn s => if member (op =) reserved_nice_names s then full_name else s)
533 fun nice_name (full_name, _) NONE = (full_name, NONE)
534 | nice_name (full_name, desired_name) (SOME the_pool) =
535 if is_built_in_tptp_symbol full_name then
536 (full_name, SOME the_pool)
537 else case Symtab.lookup (fst the_pool) full_name of
538 SOME nice_name => (nice_name, SOME the_pool)
541 val nice_prefix = readable_name full_name desired_name
545 nice_prefix ^ (if j = 0 then "" else string_of_int j)
547 case Symtab.lookup (snd the_pool) nice_name of
549 if full_name = full_name' then (nice_name, the_pool)
553 (Symtab.update_new (full_name, nice_name) (fst the_pool),
554 Symtab.update_new (nice_name, full_name) (snd the_pool)))
556 in add 0 |> apsnd SOME end
558 fun nice_type (AType name) = nice_name name #>> AType
559 | nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
560 fun nice_term (ATerm (name, ts)) =
561 nice_name name ##>> pool_map nice_term ts #>> ATerm
562 | nice_term (AAbs ((name, ty), tm)) =
563 nice_name name ##>> nice_type ty ##>> nice_term tm #>> AAbs
564 fun nice_formula (AQuant (q, xs, phi)) =
565 pool_map nice_name (map fst xs)
566 ##>> pool_map (fn NONE => pair NONE
567 | SOME ty => nice_type ty #>> SOME) (map snd xs)
568 ##>> nice_formula phi
569 #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
570 | nice_formula (AConn (c, phis)) =
571 pool_map nice_formula phis #>> curry AConn c
572 | nice_formula (AAtom tm) = nice_term tm #>> AAtom
573 fun nice_problem_line (Decl (ident, sym, ty)) =
574 nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
575 | nice_problem_line (Formula (ident, kind, phi, source, info)) =
576 nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
577 fun nice_problem problem =
578 pool_map (fn (heading, lines) =>
579 pool_map nice_problem_line lines #>> pair heading) problem
580 fun nice_atp_problem readable_names problem =
581 nice_problem problem (empty_name_pool readable_names)