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 format = CNF | CNF_UEQ | FOF | TFF | THF
23 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
24 datatype 'a problem_line =
25 Decl of string * 'a * 'a ho_type |
26 Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
27 * (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
28 type 'a problem = (string * 'a problem_line list) list
34 val tptp_has_type : string
35 val tptp_type_of_types : string
36 val tptp_bool_type : string
37 val tptp_individual_type : string
38 val tptp_fun_type : string
39 val tptp_product_type : string
40 val tptp_forall : string
41 val tptp_exists : string
45 val tptp_implies : string
48 val tptp_not_iff : string
50 val tptp_not_infix : string
51 val tptp_equal : string
52 val tptp_old_equal : string
53 val tptp_false : string
54 val tptp_true : string
55 val tptp_empty_list : string
56 val is_tptp_equal : string -> bool
57 val is_built_in_tptp_symbol : string -> bool
58 val is_tptp_variable : string -> bool
59 val is_tptp_user_symbol : string -> bool
60 val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
62 connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
63 -> ('a, 'b, 'c) formula
65 bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
68 bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
69 -> connective * 'a list -> ('b, 'c, 'd) formula
71 bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
73 val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
74 val is_format_typed : format -> bool
75 val tptp_lines_for_atp_problem : format -> string problem -> string list
76 val ensure_cnf_problem :
77 (string * string) problem -> (string * string) problem
78 val filter_cnf_ueq_problem :
79 (string * string) problem -> (string * string) problem
80 val declare_undeclared_syms_in_atp_problem :
81 string -> string -> (string * string) problem -> (string * string) problem
82 val nice_atp_problem :
83 bool -> ('a * (string * string) problem_line list) list
84 -> ('a * string problem_line list) list
85 * (string Symtab.table * string Symtab.table) option
88 structure ATP_Problem : ATP_PROBLEM =
96 datatype ('a, 'b) ho_term =
97 ATerm of 'a * ('a, 'b) ho_term list |
98 AAbs of ('a * 'b) * ('a, 'b) ho_term
99 datatype quantifier = AForall | AExists
100 datatype connective = ANot | AAnd | AOr | AImplies | AIff
101 datatype ('a, 'b, 'c) formula =
102 AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
103 AConn of connective * ('a, 'b, 'c) formula list |
106 datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
108 datatype format = CNF | CNF_UEQ | FOF | TFF | THF
109 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
110 datatype 'a problem_line =
111 Decl of string * 'a * 'a ho_type |
112 Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
113 * (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
114 type 'a problem = (string * 'a problem_line list) list
116 (* official TPTP syntax *)
121 val tptp_has_type = ":"
122 val tptp_type_of_types = "$tType"
123 val tptp_bool_type = "$o"
124 val tptp_individual_type = "$i"
125 val tptp_fun_type = ">"
126 val tptp_product_type = "*"
127 val tptp_forall = "!"
128 val tptp_exists = "?"
132 val tptp_implies = "=>"
135 val tptp_not_iff = "<~>"
137 val tptp_not_infix = "!"
139 val tptp_old_equal = "equal"
140 val tptp_false = "$false"
141 val tptp_true = "$true"
142 val tptp_empty_list = "[]"
144 fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
145 fun is_built_in_tptp_symbol s =
146 s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
147 fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
148 val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
150 fun raw_polarities_of_conn ANot = (SOME false, NONE)
151 | raw_polarities_of_conn AAnd = (SOME true, SOME true)
152 | raw_polarities_of_conn AOr = (SOME true, SOME true)
153 | raw_polarities_of_conn AImplies = (SOME false, SOME true)
154 | raw_polarities_of_conn AIff = (NONE, NONE)
155 fun polarities_of_conn NONE = K (NONE, NONE)
156 | polarities_of_conn (SOME pos) =
157 raw_polarities_of_conn #> not pos ? pairself (Option.map not)
159 fun mk_anot (AConn (ANot, [phi])) = phi
160 | mk_anot phi = AConn (ANot, [phi])
161 fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
163 fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
164 | aconn_fold pos f (AImplies, [phi1, phi2]) =
165 f (Option.map not pos) phi1 #> f pos phi2
166 | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
167 | aconn_fold pos f (AOr, phis) = fold (f pos) phis
168 | aconn_fold _ f (_, phis) = fold (f NONE) phis
170 fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
171 | aconn_map pos f (AImplies, [phi1, phi2]) =
172 AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
173 | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
174 | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
175 | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
177 fun formula_fold pos f =
179 fun aux pos (AQuant (_, _, phi)) = aux pos phi
180 | aux pos (AConn conn) = aconn_fold pos aux conn
181 | aux pos (AAtom tm) = f pos tm
184 fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
185 | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
186 | formula_map f (AAtom tm) = AAtom (f tm)
188 val is_format_typed = member (op =) [TFF, THF]
190 fun string_for_kind Axiom = "axiom"
191 | string_for_kind Definition = "definition"
192 | string_for_kind Lemma = "lemma"
193 | string_for_kind Hypothesis = "hypothesis"
194 | string_for_kind Conjecture = "conjecture"
196 fun strip_tff_type (AFun (AType s, ty)) = strip_tff_type ty |>> cons s
197 | strip_tff_type (AFun (AFun _, _)) =
198 raise Fail "unexpected higher-order type in first-order format"
199 | strip_tff_type (AType s) = ([], s)
201 fun string_for_type THF ty =
203 fun aux _ (AType s) = s
204 | aux rhs (AFun (ty1, ty2)) =
205 aux false ty1 ^ " " ^ tptp_fun_type ^ " " ^ aux true ty2
206 |> not rhs ? enclose "(" ")"
208 | string_for_type TFF ty =
209 (case strip_tff_type ty of
211 | ([s'], s) => s' ^ " " ^ tptp_fun_type ^ " " ^ s
213 "(" ^ space_implode (" " ^ tptp_product_type ^ " ") ss ^ ") " ^
214 tptp_fun_type ^ " " ^ s)
215 | string_for_type _ _ = raise Fail "unexpected type in untyped format"
217 fun string_for_term _ (ATerm (s, [])) = s
218 | string_for_term format (ATerm (s, ts)) =
219 if s = tptp_empty_list then
220 (* used for lists in the optional "source" field of a derivation *)
221 "[" ^ commas (map (string_for_term format) ts) ^ "]"
222 else if is_tptp_equal s then
223 space_implode (" " ^ tptp_equal ^ " ") (map (string_for_term format) ts)
224 |> format = THF ? enclose "(" ")"
226 let val ss = map (string_for_term format) ts in
228 "(" ^ space_implode (" " ^ tptp_app ^ " ") (s :: ss) ^ ")"
230 s ^ "(" ^ commas ss ^ ")"
232 | string_for_term THF (AAbs ((s, ty), tm)) =
233 "(^[" ^ s ^ ":" ^ string_for_type THF ty ^ "] : " ^ string_for_term THF tm ^ ")"
234 | string_for_term _ _ = raise Fail "unexpected term in first-order format"
236 fun string_for_quantifier AForall = tptp_forall
237 | string_for_quantifier AExists = tptp_exists
239 fun string_for_connective ANot = tptp_not
240 | string_for_connective AAnd = tptp_and
241 | string_for_connective AOr = tptp_or
242 | string_for_connective AImplies = tptp_implies
243 | string_for_connective AIff = tptp_iff
245 fun string_for_bound_var format (s, ty) =
246 s ^ (if format = TFF orelse format = THF then
247 " " ^ tptp_has_type ^ " " ^
248 string_for_type format (ty |> the_default (AType tptp_individual_type))
252 fun string_for_formula format (AQuant (q, xs, phi)) =
253 string_for_quantifier q ^
254 "[" ^ commas (map (string_for_bound_var format) xs) ^ "] : " ^
255 string_for_formula format phi
257 | string_for_formula format
258 (AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
259 space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
260 (map (string_for_term format) ts)
261 |> format = THF ? enclose "(" ")"
262 | string_for_formula format (AConn (c, [phi])) =
263 string_for_connective c ^ " " ^
264 (string_for_formula format phi |> format = THF ? enclose "(" ")")
266 | string_for_formula format (AConn (c, phis)) =
267 space_implode (" " ^ string_for_connective c ^ " ")
268 (map (string_for_formula format) phis)
270 | string_for_formula format (AAtom tm) = string_for_term format tm
273 ATerm ("inference", ATerm ("isabelle", []) :: replicate 2 (ATerm ("[]", [])))
275 fun string_for_format CNF = tptp_cnf
276 | string_for_format CNF_UEQ = tptp_cnf
277 | string_for_format FOF = tptp_fof
278 | string_for_format TFF = tptp_tff
279 | string_for_format THF = tptp_thf
281 fun string_for_problem_line format (Decl (ident, sym, ty)) =
282 string_for_format format ^ "(" ^ ident ^ ", type,\n " ^ sym ^ " : " ^
283 string_for_type format ty ^ ").\n"
284 | string_for_problem_line format (Formula (ident, kind, phi, source, info)) =
285 string_for_format format ^ "(" ^ ident ^ ", " ^ string_for_kind kind ^
286 ",\n (" ^ string_for_formula format phi ^ ")" ^
287 (case (source, info) of
289 | (SOME tm, NONE) => ", " ^ string_for_term format tm
291 ", " ^ string_for_term format (source |> the_default default_source) ^
292 ", " ^ string_for_term format tm) ^ ").\n"
293 fun tptp_lines_for_atp_problem format problem =
294 "% This file was generated by Isabelle (most likely Sledgehammer)\n\
295 \% " ^ timestamp () ^ "\n" ::
296 maps (fn (_, []) => []
297 | (heading, lines) =>
298 "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
299 map (string_for_problem_line format) lines)
303 (** CNF (Metis) and CNF UEQ (Waldmeister) **)
305 fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
306 | is_problem_line_negated _ = false
308 fun is_problem_line_cnf_ueq (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) =
310 | is_problem_line_cnf_ueq _ = false
312 fun open_conjecture_term (ATerm ((s, s'), tms)) =
313 ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
314 else (s, s'), tms |> map open_conjecture_term)
315 | open_conjecture_term _ = raise Fail "unexpected higher-order term"
316 fun open_formula conj =
318 (* We are conveniently assuming that all bound variable names are
319 distinct, which should be the case for the formulas we generate. *)
320 fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
321 | opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
322 | opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
323 | opn pos (AConn (c, [phi1, phi2])) =
324 let val (pos1, pos2) = polarities_of_conn pos c in
325 AConn (c, [opn pos1 phi1, opn pos2 phi2])
327 | opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
329 in opn (SOME (not conj)) end
330 fun open_formula_line (Formula (ident, kind, phi, source, info)) =
331 Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
332 | open_formula_line line = line
334 fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
335 Formula (ident, Hypothesis, mk_anot phi, source, info)
336 | negate_conjecture_line line = line
338 exception CLAUSIFY of unit
340 (* This "clausification" only expands syntactic sugar, such as "phi => psi" to
341 "~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
342 attempt to distribute conjunctions over disjunctions. *)
343 fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
344 | clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
345 | clausify_formula true (AConn (AOr, [phi1, phi2])) =
346 (phi1, phi2) |> pairself (clausify_formula true)
347 |> uncurry (map_product (mk_aconn AOr))
348 | clausify_formula false (AConn (AAnd, [phi1, phi2])) =
349 (phi1, phi2) |> pairself (clausify_formula false)
350 |> uncurry (map_product (mk_aconn AOr))
351 | clausify_formula true (AConn (AImplies, [phi1, phi2])) =
352 clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
353 | clausify_formula true (AConn (AIff, phis)) =
354 clausify_formula true (AConn (AImplies, phis)) @
355 clausify_formula true (AConn (AImplies, rev phis))
356 | clausify_formula _ _ = raise CLAUSIFY ()
358 fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
360 val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
362 map2 (fn phi => fn j =>
363 Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
367 | clausify_formula_line _ = []
369 fun ensure_cnf_problem_line line =
370 line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
372 fun ensure_cnf_problem problem =
373 problem |> map (apsnd (maps ensure_cnf_problem_line))
375 fun filter_cnf_ueq_problem problem =
377 |> map (apsnd (map open_formula_line
378 #> filter is_problem_line_cnf_ueq
379 #> map negate_conjecture_line))
382 val conjs = problem |> maps snd |> filter is_problem_line_negated
383 in if length conjs = 1 then problem else [] end)
386 (** Symbol declarations **)
388 (* TFF allows implicit declarations of types, function symbols, and predicate
389 symbols (with "$i" as the type of individuals), but some provers (e.g.,
390 SNARK) require explicit declarations. The situation is similar for THF. *)
392 val atype_of_types = AType (`I tptp_type_of_types)
393 val bool_atype = AType (`I tptp_bool_type)
394 val individual_atype = AType (`I tptp_individual_type)
396 fun default_type pred_sym =
398 fun typ 0 = if pred_sym then bool_atype else individual_atype
399 | typ ary = AFun (individual_atype, typ (ary - 1))
402 fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = insert (op =) sym
403 | add_declared_syms_in_problem_line _ = I
404 fun declared_syms_in_problem problem =
405 fold (fold add_declared_syms_in_problem_line o snd) problem []
407 fun undeclared_syms_in_problem declared problem =
410 if member (op =) declared name then I else AList.default (op =) (name, ty)
411 fun do_type (AFun (ty1, ty2)) = fold do_type [ty1, ty2]
412 | do_type (AType name) = do_sym name (K atype_of_types)
413 fun do_term pred_sym (ATerm (name as (s, _), tms)) =
414 is_tptp_user_symbol s
415 ? do_sym name (fn _ => default_type pred_sym (length tms))
416 #> fold (do_term false) tms
417 | do_term _ (AAbs ((_, ty), tm)) = do_type ty #> do_term false tm
418 fun do_formula (AQuant (_, xs, phi)) =
419 fold do_type (map_filter snd xs) #> do_formula phi
420 | do_formula (AConn (_, phis)) = fold do_formula phis
421 | do_formula (AAtom tm) = do_term true tm
422 fun do_problem_line (Decl (_, _, ty)) = do_type ty
423 | do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
425 fold (fold do_problem_line o snd) problem []
426 |> filter_out (is_built_in_tptp_symbol o fst o fst)
429 fun declare_undeclared_syms_in_atp_problem prefix heading problem =
431 fun decl_line (x as (s, _), ty) = Decl (prefix ^ s, x, ty ())
432 val declared = problem |> declared_syms_in_problem
434 problem |> undeclared_syms_in_problem declared
435 |> sort_wrt (fst o fst)
437 in (heading, decls) :: problem end
441 fun empty_name_pool readable_names =
442 if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
444 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
446 pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
451 | skip (#"." :: cs) = skip cs
452 | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
454 | keep (#"." :: cs) = skip cs
455 | keep (c :: cs) = c :: keep cs
456 in String.explode #> rev #> keep #> rev #> String.implode end
458 (* Long names can slow down the ATPs. *)
459 val max_readable_name_size = 20
461 (* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
462 unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
463 ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
464 is still necessary). *)
465 val reserved_nice_names = [tptp_old_equal, "op", "eq"]
467 fun readable_name full_name s =
468 if s = full_name then
472 |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
474 if size s > max_readable_name_size then
475 String.substring (s, 0, max_readable_name_size div 2 - 4) ^
476 Word.toString (hashw_string (full_name, 0w0)) ^
477 String.extract (s, size s - max_readable_name_size div 2 + 4,
481 |> (fn s => if member (op =) reserved_nice_names s then full_name else s)
483 fun nice_name (full_name, _) NONE = (full_name, NONE)
484 | nice_name (full_name, desired_name) (SOME the_pool) =
485 if is_built_in_tptp_symbol full_name then
486 (full_name, SOME the_pool)
487 else case Symtab.lookup (fst the_pool) full_name of
488 SOME nice_name => (nice_name, SOME the_pool)
491 val nice_prefix = readable_name full_name desired_name
495 nice_prefix ^ (if j = 0 then "" else "_" ^ string_of_int j)
497 case Symtab.lookup (snd the_pool) nice_name of
499 if full_name = full_name' then (nice_name, the_pool)
503 (Symtab.update_new (full_name, nice_name) (fst the_pool),
504 Symtab.update_new (nice_name, full_name) (snd the_pool)))
506 in add 0 |> apsnd SOME end
508 fun nice_type (AType name) = nice_name name #>> AType
509 | nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
510 fun nice_term (ATerm (name, ts)) =
511 nice_name name ##>> pool_map nice_term ts #>> ATerm
512 | nice_term (AAbs ((name, ty), tm)) =
513 nice_name name ##>> nice_type ty ##>> nice_term tm #>> AAbs
514 fun nice_formula (AQuant (q, xs, phi)) =
515 pool_map nice_name (map fst xs)
516 ##>> pool_map (fn NONE => pair NONE
517 | SOME ty => nice_type ty #>> SOME) (map snd xs)
518 ##>> nice_formula phi
519 #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
520 | nice_formula (AConn (c, phis)) =
521 pool_map nice_formula phis #>> curry AConn c
522 | nice_formula (AAtom tm) = nice_term tm #>> AAtom
523 fun nice_problem_line (Decl (ident, sym, ty)) =
524 nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
525 | nice_problem_line (Formula (ident, kind, phi, source, info)) =
526 nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
527 fun nice_problem problem =
528 pool_map (fn (heading, lines) =>
529 pool_map nice_problem_line lines #>> pair heading) problem
530 fun nice_atp_problem readable_names problem =
531 nice_problem problem (empty_name_pool readable_names)