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
56 val tptp_implies : string
59 val tptp_not_iff : string
61 val tptp_not_infix : string
62 val tptp_equal : string
63 val tptp_old_equal : string
64 val tptp_false : string
65 val tptp_true : string
66 val tptp_empty_list : string
67 val is_tptp_equal : string -> bool
68 val is_built_in_tptp_symbol : string -> bool
69 val is_tptp_variable : string -> bool
70 val is_tptp_user_symbol : string -> bool
71 val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
73 connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
74 -> ('a, 'b, 'c) formula
76 bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
79 bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
80 -> connective * 'a list -> ('b, 'c, 'd) formula
82 bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
84 val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
85 val is_format_thf : format -> bool
86 val is_format_typed : format -> bool
87 val tptp_lines_for_atp_problem : format -> string problem -> string list
88 val ensure_cnf_problem :
89 (string * string) problem -> (string * string) problem
90 val filter_cnf_ueq_problem :
91 (string * string) problem -> (string * string) problem
92 val declare_undeclared_syms_in_atp_problem :
93 string -> string -> (string * string) problem -> (string * string) problem
94 val nice_atp_problem :
95 bool -> ('a * (string * string) problem_line list) list
96 -> ('a * string problem_line list) list
97 * (string Symtab.table * string Symtab.table) option
100 structure ATP_Problem : ATP_PROBLEM =
108 datatype ('a, 'b) ho_term =
109 ATerm of 'a * ('a, 'b) ho_term list |
110 AAbs of ('a * 'b) * ('a, 'b) ho_term
111 datatype quantifier = AForall | AExists
112 datatype connective = ANot | AAnd | AOr | AImplies | AIff
113 datatype ('a, 'b, 'c) formula =
114 AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
115 AConn of connective * ('a, 'b, 'c) formula list |
118 datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
120 datatype thf_flavor = Without_Choice | With_Choice
128 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
129 datatype 'a problem_line =
130 Decl of string * 'a * 'a ho_type |
131 Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
132 * (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
133 type 'a problem = (string * 'a problem_line list) list
135 (* official TPTP syntax *)
140 val tptp_has_type = ":"
141 val tptp_type_of_types = "$tType"
142 val tptp_bool_type = "$o"
143 val tptp_individual_type = "$i"
144 val tptp_fun_type = ">"
145 val tptp_product_type = "*"
146 val tptp_forall = "!"
147 val tptp_ho_forall = "!!"
148 val tptp_exists = "?"
149 val tptp_ho_exists = "??"
153 val tptp_implies = "=>"
156 val tptp_not_iff = "<~>"
158 val tptp_not_infix = "!"
160 val tptp_old_equal = "equal"
161 val tptp_false = "$false"
162 val tptp_true = "$true"
163 val tptp_empty_list = "[]"
165 fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
166 fun is_built_in_tptp_symbol s =
167 s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
168 fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
169 val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
171 fun raw_polarities_of_conn ANot = (SOME false, NONE)
172 | raw_polarities_of_conn AAnd = (SOME true, SOME true)
173 | raw_polarities_of_conn AOr = (SOME true, SOME true)
174 | raw_polarities_of_conn AImplies = (SOME false, SOME true)
175 | raw_polarities_of_conn AIff = (NONE, NONE)
176 fun polarities_of_conn NONE = K (NONE, NONE)
177 | polarities_of_conn (SOME pos) =
178 raw_polarities_of_conn #> not pos ? pairself (Option.map not)
180 fun mk_anot (AConn (ANot, [phi])) = phi
181 | mk_anot phi = AConn (ANot, [phi])
182 fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
184 fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
185 | aconn_fold pos f (AImplies, [phi1, phi2]) =
186 f (Option.map not pos) phi1 #> f pos phi2
187 | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
188 | aconn_fold pos f (AOr, phis) = fold (f pos) phis
189 | aconn_fold _ f (_, phis) = fold (f NONE) phis
191 fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
192 | aconn_map pos f (AImplies, [phi1, phi2]) =
193 AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
194 | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
195 | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
196 | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
198 fun formula_fold pos f =
200 fun aux pos (AQuant (_, _, phi)) = aux pos phi
201 | aux pos (AConn conn) = aconn_fold pos aux conn
202 | aux pos (AAtom tm) = f pos tm
205 fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
206 | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
207 | formula_map f (AAtom tm) = AAtom (f tm)
209 fun is_format_thf (THF _) = true
210 | is_format_thf _ = false
211 fun is_format_typed TFF = true
212 | is_format_typed (THF _) = true
213 | is_format_typed _ = false
215 fun string_for_kind Axiom = "axiom"
216 | string_for_kind Definition = "definition"
217 | string_for_kind Lemma = "lemma"
218 | string_for_kind Hypothesis = "hypothesis"
219 | string_for_kind Conjecture = "conjecture"
221 fun strip_tff_type (AFun (AType s, ty)) = strip_tff_type ty |>> cons s
222 | strip_tff_type (AFun (AFun _, _)) =
223 raise Fail "unexpected higher-order type in first-order format"
224 | strip_tff_type (AType s) = ([], s)
226 fun string_for_type (THF _) ty =
228 fun aux _ (AType s) = s
229 | aux rhs (AFun (ty1, ty2)) =
230 aux false ty1 ^ " " ^ tptp_fun_type ^ " " ^ aux true ty2
231 |> not rhs ? enclose "(" ")"
233 | string_for_type TFF ty =
234 (case strip_tff_type ty of
236 | ([s'], s) => s' ^ " " ^ tptp_fun_type ^ " " ^ s
238 "(" ^ space_implode (" " ^ tptp_product_type ^ " ") ss ^ ") " ^
239 tptp_fun_type ^ " " ^ s)
240 | string_for_type _ _ = raise Fail "unexpected type in untyped format"
242 fun string_for_quantifier AForall = tptp_forall
243 | string_for_quantifier AExists = tptp_exists
245 fun string_for_connective ANot = tptp_not
246 | string_for_connective AAnd = tptp_and
247 | string_for_connective AOr = tptp_or
248 | string_for_connective AImplies = tptp_implies
249 | string_for_connective AIff = tptp_iff
251 fun string_for_bound_var format (s, ty) =
252 s ^ (if is_format_typed format then
253 " " ^ tptp_has_type ^ " " ^
254 string_for_type format (ty |> the_default (AType tptp_individual_type))
258 fun string_for_term _ (ATerm (s, [])) = s
259 | string_for_term format (ATerm (s, ts)) =
260 if s = tptp_empty_list then
261 (* used for lists in the optional "source" field of a derivation *)
262 "[" ^ commas (map (string_for_term format) ts) ^ "]"
263 else if is_tptp_equal s then
264 space_implode (" " ^ tptp_equal ^ " ") (map (string_for_term format) ts)
265 |> is_format_thf format ? enclose "(" ")"
267 (case (s = tptp_ho_forall orelse s = tptp_ho_exists, ts) of
268 (true, [AAbs ((s', ty), tm)]) =>
269 (* Use syntactic sugar "!" and "?" instead of "!!" and "??" whenever
270 possible, to work around LEO-II 1.2.8 parser limitation. *)
271 string_for_formula format
272 (AQuant (if s = tptp_ho_forall then AForall else AExists,
273 [(s', SOME ty)], AAtom tm))
275 let val ss = map (string_for_term format) ts in
276 if is_format_thf format then
277 "(" ^ space_implode (" " ^ tptp_app ^ " ") (s :: ss) ^ ")"
279 s ^ "(" ^ commas ss ^ ")"
281 | string_for_term (format as THF _) (AAbs ((s, ty), tm)) =
282 "(^[" ^ s ^ " : " ^ string_for_type format ty ^ "] : " ^
283 string_for_term format tm ^ ")"
284 | string_for_term _ _ = raise Fail "unexpected term in first-order format"
285 and string_for_formula format (AQuant (q, xs, phi)) =
286 string_for_quantifier q ^
287 "[" ^ commas (map (string_for_bound_var format) xs) ^ "] : " ^
288 string_for_formula format phi
290 | string_for_formula format
291 (AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
292 space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
293 (map (string_for_term format) ts)
294 |> is_format_thf format ? enclose "(" ")"
295 | string_for_formula format (AConn (c, [phi])) =
296 string_for_connective c ^ " " ^
297 (string_for_formula format phi |> is_format_thf format ? enclose "(" ")")
299 | string_for_formula format (AConn (c, phis)) =
300 space_implode (" " ^ string_for_connective c ^ " ")
301 (map (string_for_formula format) phis)
303 | string_for_formula format (AAtom tm) = string_for_term format tm
305 fun the_source (SOME source) = source
308 ATerm ("isabelle", []) :: replicate 2 (ATerm ("[]", [])))
310 fun string_for_format CNF = tptp_cnf
311 | string_for_format CNF_UEQ = tptp_cnf
312 | string_for_format FOF = tptp_fof
313 | string_for_format TFF = tptp_tff
314 | string_for_format (THF _) = tptp_thf
316 fun string_for_problem_line format (Decl (ident, sym, ty)) =
317 string_for_format format ^ "(" ^ ident ^ ", type,\n " ^ sym ^ " : " ^
318 string_for_type format ty ^ ").\n"
319 | string_for_problem_line format (Formula (ident, kind, phi, source, info)) =
320 string_for_format format ^ "(" ^ ident ^ ", " ^ string_for_kind kind ^
321 ",\n (" ^ string_for_formula format phi ^ ")" ^
322 (case (source, info) of
324 | (SOME tm, NONE) => ", " ^ string_for_term format tm
326 ", " ^ string_for_term format (the_source source) ^
327 ", " ^ string_for_term format tm) ^ ").\n"
328 fun tptp_lines_for_atp_problem format problem =
329 "% This file was generated by Isabelle (most likely Sledgehammer)\n\
330 \% " ^ timestamp () ^ "\n" ::
331 maps (fn (_, []) => []
332 | (heading, lines) =>
333 "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
334 map (string_for_problem_line format) lines)
338 (** CNF (Metis) and CNF UEQ (Waldmeister) **)
340 fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
341 | is_problem_line_negated _ = false
343 fun is_problem_line_cnf_ueq (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) =
345 | is_problem_line_cnf_ueq _ = false
347 fun open_conjecture_term (ATerm ((s, s'), tms)) =
348 ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
349 else (s, s'), tms |> map open_conjecture_term)
350 | open_conjecture_term _ = raise Fail "unexpected higher-order term"
351 fun open_formula conj =
353 (* We are conveniently assuming that all bound variable names are
354 distinct, which should be the case for the formulas we generate. *)
355 fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
356 | opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
357 | opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
358 | opn pos (AConn (c, [phi1, phi2])) =
359 let val (pos1, pos2) = polarities_of_conn pos c in
360 AConn (c, [opn pos1 phi1, opn pos2 phi2])
362 | opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
364 in opn (SOME (not conj)) end
365 fun open_formula_line (Formula (ident, kind, phi, source, info)) =
366 Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
367 | open_formula_line line = line
369 fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
370 Formula (ident, Hypothesis, mk_anot phi, source, info)
371 | negate_conjecture_line line = line
373 exception CLAUSIFY of unit
375 (* This "clausification" only expands syntactic sugar, such as "phi => psi" to
376 "~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
377 attempt to distribute conjunctions over disjunctions. *)
378 fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
379 | clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
380 | clausify_formula true (AConn (AOr, [phi1, phi2])) =
381 (phi1, phi2) |> pairself (clausify_formula true)
382 |> uncurry (map_product (mk_aconn AOr))
383 | clausify_formula false (AConn (AAnd, [phi1, phi2])) =
384 (phi1, phi2) |> pairself (clausify_formula false)
385 |> uncurry (map_product (mk_aconn AOr))
386 | clausify_formula true (AConn (AImplies, [phi1, phi2])) =
387 clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
388 | clausify_formula true (AConn (AIff, phis)) =
389 clausify_formula true (AConn (AImplies, phis)) @
390 clausify_formula true (AConn (AImplies, rev phis))
391 | clausify_formula _ _ = raise CLAUSIFY ()
393 fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
395 val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
397 map2 (fn phi => fn j =>
398 Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
402 | clausify_formula_line _ = []
404 fun ensure_cnf_problem_line line =
405 line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
407 fun ensure_cnf_problem problem =
408 problem |> map (apsnd (maps ensure_cnf_problem_line))
410 fun filter_cnf_ueq_problem problem =
412 |> map (apsnd (map open_formula_line
413 #> filter is_problem_line_cnf_ueq
414 #> map negate_conjecture_line))
417 val lines = problem |> maps snd
418 val conjs = lines |> filter is_problem_line_negated
419 in if length conjs = 1 andalso conjs <> lines then problem else [] end)
422 (** Symbol declarations **)
424 (* TFF allows implicit declarations of types, function symbols, and predicate
425 symbols (with "$i" as the type of individuals), but some provers (e.g.,
426 SNARK) require explicit declarations. The situation is similar for THF. *)
428 val atype_of_types = AType (`I tptp_type_of_types)
429 val bool_atype = AType (`I tptp_bool_type)
430 val individual_atype = AType (`I tptp_individual_type)
432 fun default_type pred_sym =
434 fun typ 0 = if pred_sym then bool_atype else individual_atype
435 | typ ary = AFun (individual_atype, typ (ary - 1))
438 fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = insert (op =) sym
439 | add_declared_syms_in_problem_line _ = I
440 fun declared_syms_in_problem problem =
441 fold (fold add_declared_syms_in_problem_line o snd) problem []
443 fun undeclared_syms_in_problem declared problem =
446 if member (op =) declared name then I else AList.default (op =) (name, ty)
447 fun do_type (AFun (ty1, ty2)) = fold do_type [ty1, ty2]
448 | do_type (AType name) = do_sym name (K atype_of_types)
449 fun do_term pred_sym (ATerm (name as (s, _), tms)) =
450 is_tptp_user_symbol s
451 ? do_sym name (fn _ => default_type pred_sym (length tms))
452 #> fold (do_term false) tms
453 | do_term _ (AAbs ((_, ty), tm)) = do_type ty #> do_term false tm
454 fun do_formula (AQuant (_, xs, phi)) =
455 fold do_type (map_filter snd xs) #> do_formula phi
456 | do_formula (AConn (_, phis)) = fold do_formula phis
457 | do_formula (AAtom tm) = do_term true tm
458 fun do_problem_line (Decl (_, _, ty)) = do_type ty
459 | do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
461 fold (fold do_problem_line o snd) problem []
462 |> filter_out (is_built_in_tptp_symbol o fst o fst)
465 fun declare_undeclared_syms_in_atp_problem prefix heading problem =
467 fun decl_line (x as (s, _), ty) = Decl (prefix ^ s, x, ty ())
468 val declared = problem |> declared_syms_in_problem
470 problem |> undeclared_syms_in_problem declared
471 |> sort_wrt (fst o fst)
473 in (heading, decls) :: problem end
477 fun empty_name_pool readable_names =
478 if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
480 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
482 pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
487 | skip (#"." :: cs) = skip cs
488 | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
490 | keep (#"." :: cs) = skip cs
491 | keep (c :: cs) = c :: keep cs
492 in String.explode #> rev #> keep #> rev #> String.implode end
494 (* Long names can slow down the ATPs. *)
495 val max_readable_name_size = 20
497 (* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
498 unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
499 ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
500 is still necessary). *)
501 val reserved_nice_names = [tptp_old_equal, "op", "eq"]
503 fun readable_name full_name s =
504 if s = full_name then
508 |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
510 if size s > max_readable_name_size then
511 String.substring (s, 0, max_readable_name_size div 2 - 4) ^
512 string_of_int (hash_string full_name) ^
513 String.extract (s, size s - max_readable_name_size div 2 + 4,
517 |> (fn s => if member (op =) reserved_nice_names s then full_name else s)
519 fun nice_name (full_name, _) NONE = (full_name, NONE)
520 | nice_name (full_name, desired_name) (SOME the_pool) =
521 if is_built_in_tptp_symbol full_name then
522 (full_name, SOME the_pool)
523 else case Symtab.lookup (fst the_pool) full_name of
524 SOME nice_name => (nice_name, SOME the_pool)
527 val nice_prefix = readable_name full_name desired_name
531 nice_prefix ^ (if j = 0 then "" else "_" ^ string_of_int j)
533 case Symtab.lookup (snd the_pool) nice_name of
535 if full_name = full_name' then (nice_name, the_pool)
539 (Symtab.update_new (full_name, nice_name) (fst the_pool),
540 Symtab.update_new (nice_name, full_name) (snd the_pool)))
542 in add 0 |> apsnd SOME end
544 fun nice_type (AType name) = nice_name name #>> AType
545 | nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
546 fun nice_term (ATerm (name, ts)) =
547 nice_name name ##>> pool_map nice_term ts #>> ATerm
548 | nice_term (AAbs ((name, ty), tm)) =
549 nice_name name ##>> nice_type ty ##>> nice_term tm #>> AAbs
550 fun nice_formula (AQuant (q, xs, phi)) =
551 pool_map nice_name (map fst xs)
552 ##>> pool_map (fn NONE => pair NONE
553 | SOME ty => nice_type ty #>> SOME) (map snd xs)
554 ##>> nice_formula phi
555 #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
556 | nice_formula (AConn (c, phis)) =
557 pool_map nice_formula phis #>> curry AConn c
558 | nice_formula (AAtom tm) = nice_term tm #>> AAtom
559 fun nice_problem_line (Decl (ident, sym, ty)) =
560 nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
561 | nice_problem_line (Formula (ident, kind, phi, source, info)) =
562 nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
563 fun nice_problem problem =
564 pool_map (fn (heading, lines) =>
565 pool_map nice_problem_line lines #>> pair heading) problem
566 fun nice_atp_problem readable_names problem =
567 nice_problem problem (empty_name_pool readable_names)