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 fo_term = ATerm of 'a * 'a fo_term list
11 datatype quantifier = AForall | AExists
12 datatype connective = ANot | AAnd | AOr | AImplies | AIff
13 datatype ('a, 'b, 'c) formula =
14 AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
15 AConn of connective * ('a, 'b, 'c) formula list |
18 datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
20 datatype format = CNF | CNF_UEQ | FOF | TFF | THF
21 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
22 datatype 'a problem_line =
23 Decl of string * 'a * 'a ho_type |
24 Formula of string * formula_kind * ('a, 'a ho_type, 'a fo_term) formula
25 * string fo_term option * string fo_term option
26 type 'a problem = (string * 'a problem_line list) list
32 val tptp_has_type : string
33 val tptp_type_of_types : string
34 val tptp_bool_type : string
35 val tptp_individual_type : string
36 val tptp_fun_type : string
37 val tptp_product_type : string
38 val tptp_forall : string
39 val tptp_exists : string
43 val tptp_implies : string
46 val tptp_not_iff : string
48 val tptp_not_infix : string
49 val tptp_equal : string
50 val tptp_old_equal : string
51 val tptp_false : string
52 val tptp_true : string
53 val tptp_empty_list : string
54 val is_tptp_equal : string -> bool
55 val is_built_in_tptp_symbol : string -> bool
56 val is_tptp_variable : string -> bool
57 val is_tptp_user_symbol : string -> bool
58 val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
60 connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
61 -> ('a, 'b, 'c) formula
63 bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
66 bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
67 -> connective * 'a list -> ('b, 'c, 'd) formula
69 bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
71 val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
72 val is_format_typed : format -> bool
73 val tptp_strings_for_atp_problem : format -> string problem -> string list
74 val ensure_cnf_problem :
75 (string * string) problem -> (string * string) problem
76 val filter_cnf_ueq_problem :
77 (string * string) problem -> (string * string) problem
78 val declare_undeclared_syms_in_atp_problem :
79 string -> string -> (string * string) problem -> (string * string) problem
80 val nice_atp_problem :
81 bool -> ('a * (string * string) problem_line list) list
82 -> ('a * string problem_line list) list
83 * (string Symtab.table * string Symtab.table) option
86 structure ATP_Problem : ATP_PROBLEM =
94 datatype 'a fo_term = ATerm of 'a * 'a fo_term list
95 datatype quantifier = AForall | AExists
96 datatype connective = ANot | AAnd | AOr | AImplies | AIff
97 datatype ('a, 'b, 'c) formula =
98 AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
99 AConn of connective * ('a, 'b, 'c) formula list |
102 datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
104 datatype format = CNF | CNF_UEQ | FOF | TFF | THF
105 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
106 datatype 'a problem_line =
107 Decl of string * 'a * 'a ho_type |
108 Formula of string * formula_kind * ('a, 'a ho_type, 'a fo_term) formula
109 * string fo_term option * string fo_term option
110 type 'a problem = (string * 'a problem_line list) list
112 (* official TPTP syntax *)
117 val tptp_has_type = ":"
118 val tptp_type_of_types = "$tType"
119 val tptp_bool_type = "$o"
120 val tptp_individual_type = "$i"
121 val tptp_fun_type = ">"
122 val tptp_product_type = "*"
123 val tptp_forall = "!"
124 val tptp_exists = "?"
128 val tptp_implies = "=>"
131 val tptp_not_iff = "<~>"
133 val tptp_not_infix = "!"
135 val tptp_old_equal = "equal"
136 val tptp_false = "$false"
137 val tptp_true = "$true"
138 val tptp_empty_list = "[]"
140 fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
141 fun is_built_in_tptp_symbol s =
142 s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
143 fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
144 val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
146 fun raw_polarities_of_conn ANot = (SOME false, NONE)
147 | raw_polarities_of_conn AAnd = (SOME true, SOME true)
148 | raw_polarities_of_conn AOr = (SOME true, SOME true)
149 | raw_polarities_of_conn AImplies = (SOME false, SOME true)
150 | raw_polarities_of_conn AIff = (NONE, NONE)
151 fun polarities_of_conn NONE = K (NONE, NONE)
152 | polarities_of_conn (SOME pos) =
153 raw_polarities_of_conn #> not pos ? pairself (Option.map not)
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 fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
160 | aconn_fold pos f (AImplies, [phi1, phi2]) =
161 f (Option.map not pos) phi1 #> f pos phi2
162 | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
163 | aconn_fold pos f (AOr, phis) = fold (f pos) phis
164 | aconn_fold _ f (_, phis) = fold (f NONE) phis
166 fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
167 | aconn_map pos f (AImplies, [phi1, phi2]) =
168 AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
169 | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
170 | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
171 | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
173 fun formula_fold pos f =
175 fun aux pos (AQuant (_, _, phi)) = aux pos phi
176 | aux pos (AConn conn) = aconn_fold pos aux conn
177 | aux pos (AAtom tm) = f pos tm
180 fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
181 | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
182 | formula_map f (AAtom tm) = AAtom (f tm)
184 val is_format_typed = member (op =) [TFF, THF]
186 fun string_for_kind Axiom = "axiom"
187 | string_for_kind Definition = "definition"
188 | string_for_kind Lemma = "lemma"
189 | string_for_kind Hypothesis = "hypothesis"
190 | string_for_kind Conjecture = "conjecture"
192 fun strip_tff_type (AFun (AType s, ty)) = strip_tff_type ty |>> cons s
193 | strip_tff_type (AFun (AFun _, _)) =
194 raise Fail "unexpected higher-order type in first-order format"
195 | strip_tff_type (AType s) = ([], s)
197 fun string_for_type THF ty =
199 fun aux _ (AType s) = s
200 | aux rhs (AFun (ty1, ty2)) =
201 aux false ty1 ^ " " ^ tptp_fun_type ^ " " ^ aux true ty2
202 |> not rhs ? enclose "(" ")"
204 | string_for_type TFF ty =
205 (case strip_tff_type ty of
207 | ([s'], s) => s' ^ " " ^ tptp_fun_type ^ " " ^ s
209 "(" ^ space_implode (" " ^ tptp_product_type ^ " ") ss ^ ") " ^
210 tptp_fun_type ^ " " ^ s)
211 | string_for_type _ _ = raise Fail "unexpected type in untyped format"
213 fun string_for_term _ (ATerm (s, [])) = s
214 | string_for_term format (ATerm (s, ts)) =
215 if s = tptp_empty_list then
216 (* used for lists in the optional "source" field of a derivation *)
217 "[" ^ commas (map (string_for_term format) ts) ^ "]"
218 else if is_tptp_equal s then
219 space_implode (" " ^ tptp_equal ^ " ") (map (string_for_term format) ts)
220 |> format = THF ? enclose "(" ")"
222 let val ss = map (string_for_term format) ts in
224 "(" ^ space_implode (" " ^ tptp_app ^ " ") (s :: ss) ^ ")"
226 s ^ "(" ^ commas ss ^ ")"
229 fun string_for_quantifier AForall = tptp_forall
230 | string_for_quantifier AExists = tptp_exists
232 fun string_for_connective ANot = tptp_not
233 | string_for_connective AAnd = tptp_and
234 | string_for_connective AOr = tptp_or
235 | string_for_connective AImplies = tptp_implies
236 | string_for_connective AIff = tptp_iff
238 fun string_for_bound_var format (s, ty) =
239 s ^ (if format = TFF orelse format = THF then
240 " " ^ tptp_has_type ^ " " ^
241 string_for_type format (ty |> the_default (AType tptp_individual_type))
245 fun string_for_formula format (AQuant (q, xs, phi)) =
246 string_for_quantifier q ^
247 "[" ^ commas (map (string_for_bound_var format) xs) ^ "] : " ^
248 string_for_formula format phi
250 | string_for_formula format
251 (AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
252 space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
253 (map (string_for_term format) ts)
254 |> format = THF ? enclose "(" ")"
255 | string_for_formula format (AConn (c, [phi])) =
256 string_for_connective c ^ " " ^
257 (string_for_formula format phi |> format = THF ? enclose "(" ")")
259 | string_for_formula format (AConn (c, phis)) =
260 space_implode (" " ^ string_for_connective c ^ " ")
261 (map (string_for_formula format) phis)
263 | string_for_formula format (AAtom tm) = string_for_term format tm
266 ATerm ("inference", ATerm ("isabelle", []) :: replicate 2 (ATerm ("[]", [])))
268 fun string_for_format CNF = tptp_cnf
269 | string_for_format CNF_UEQ = tptp_cnf
270 | string_for_format FOF = tptp_fof
271 | string_for_format TFF = tptp_tff
272 | string_for_format THF = tptp_thf
274 fun string_for_problem_line format (Decl (ident, sym, ty)) =
275 string_for_format format ^ "(" ^ ident ^ ", type,\n " ^ sym ^ " : " ^
276 string_for_type format ty ^ ").\n"
277 | string_for_problem_line format (Formula (ident, kind, phi, source, info)) =
278 string_for_format format ^ "(" ^ ident ^ ", " ^ string_for_kind kind ^
279 ",\n (" ^ string_for_formula format phi ^ ")" ^
280 (case (source, info) of
282 | (SOME tm, NONE) => ", " ^ string_for_term format tm
284 ", " ^ string_for_term format (source |> the_default default_source) ^
285 ", " ^ string_for_term format tm) ^ ").\n"
286 fun tptp_strings_for_atp_problem format problem =
287 "% This file was generated by Isabelle (most likely Sledgehammer)\n\
288 \% " ^ timestamp () ^ "\n" ::
289 maps (fn (_, []) => []
290 | (heading, lines) =>
291 "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
292 map (string_for_problem_line format) lines)
296 (** CNF (Metis) and CNF UEQ (Waldmeister) **)
298 fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
299 | is_problem_line_negated _ = false
301 fun is_problem_line_cnf_ueq
302 (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) = is_tptp_equal s
303 | is_problem_line_cnf_ueq _ = false
305 fun open_conjecture_term (ATerm ((s, s'), tms)) =
306 ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
307 else (s, s'), tms |> map open_conjecture_term)
308 fun open_formula conj =
310 fun opn (pos as SOME true) (AQuant (AForall, xs, phi)) = opn pos phi
311 | opn (pos as SOME false) (AQuant (AExists, xs, phi)) = opn pos phi
312 | opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
313 | opn pos (AConn (c, [phi1, phi2])) =
314 let val (pos1, pos2) = polarities_of_conn pos c in
315 AConn (c, [opn pos1 phi1, opn pos2 phi2])
317 | opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
318 in opn (SOME (not conj)) end
319 fun open_formula_line (Formula (ident, kind, phi, source, info)) =
320 Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
321 | open_formula_line line = line
323 fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
324 Formula (ident, Hypothesis, mk_anot phi, source, info)
325 | negate_conjecture_line line = line
327 exception CLAUSIFY of unit
329 (* This "clausification" only expands syntactic sugar, such as "phi => psi" to
330 "~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
331 attempt to distribute conjunctions over disjunctions. *)
332 fun clausify_formula1 pos (phi as AAtom _) = phi |> not pos ? mk_anot
333 | clausify_formula1 pos (AConn (ANot, [phi])) = clausify_formula1 (not pos) phi
334 | clausify_formula1 false (AConn (AAnd, phis)) =
335 AConn (AOr, map (clausify_formula1 false) phis)
336 | clausify_formula1 true (AConn (AOr, phis)) =
337 AConn (AOr, map (clausify_formula1 true) phis)
338 | clausify_formula1 true (AConn (AImplies, [phi1, phi2])) =
339 AConn (AOr, [clausify_formula1 false phi1, clausify_formula1 true phi2])
340 | clausify_formula1 _ _ = raise CLAUSIFY ()
341 fun clausify_formula true (AConn (AIff, phis)) =
342 [clausify_formula1 true (AConn (AImplies, rev phis)),
343 clausify_formula1 true (AConn (AImplies, phis))]
344 | clausify_formula pos phi = [clausify_formula1 pos phi]
346 fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
348 val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
350 map2 (fn phi => fn j =>
352 (if n > 1 then "_cls" ^ string_of_int j else ""),
353 kind, phi, source, info))
356 | clausify_formula_line _ = []
358 fun ensure_cnf_problem_line line =
359 line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
361 fun ensure_cnf_problem problem =
362 problem |> map (apsnd (maps ensure_cnf_problem_line))
364 fun filter_cnf_ueq_problem problem =
366 |> map (apsnd (map open_formula_line
367 #> filter is_problem_line_cnf_ueq
368 #> map negate_conjecture_line))
371 val conjs = problem |> maps snd |> filter is_problem_line_negated
372 in if length conjs = 1 then problem else [] end)
375 (** Symbol declarations **)
377 (* TFF allows implicit declarations of types, function symbols, and predicate
378 symbols (with "$i" as the type of individuals), but some provers (e.g.,
379 SNARK) require explicit declarations. The situation is similar for THF. *)
381 val atype_of_types = AType (`I tptp_type_of_types)
382 val bool_atype = AType (`I tptp_bool_type)
383 val individual_atype = AType (`I tptp_individual_type)
385 fun default_type pred_sym =
387 fun typ 0 = if pred_sym then bool_atype else individual_atype
388 | typ ary = AFun (individual_atype, typ (ary - 1))
391 fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = insert (op =) sym
392 | add_declared_syms_in_problem_line _ = I
393 fun declared_syms_in_problem problem =
394 fold (fold add_declared_syms_in_problem_line o snd) problem []
396 fun undeclared_syms_in_problem declared problem =
399 if member (op =) declared name then I else AList.default (op =) (name, ty)
400 fun do_type (AFun (ty1, ty2)) = fold do_type [ty1, ty2]
401 | do_type (AType name) = do_sym name (K atype_of_types)
402 fun do_term pred_sym (ATerm (name as (s, _), tms)) =
403 is_tptp_user_symbol s
404 ? do_sym name (fn _ => default_type pred_sym (length tms))
405 #> fold (do_term false) tms
406 fun do_formula (AQuant (_, xs, phi)) =
407 fold do_type (map_filter snd xs) #> do_formula phi
408 | do_formula (AConn (_, phis)) = fold do_formula phis
409 | do_formula (AAtom tm) = do_term true tm
410 fun do_problem_line (Decl (_, _, ty)) = do_type ty
411 | do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
413 fold (fold do_problem_line o snd) problem []
414 |> filter_out (is_built_in_tptp_symbol o fst o fst)
417 fun declare_undeclared_syms_in_atp_problem prefix heading problem =
419 fun decl_line (x as (s, _), ty) = Decl (prefix ^ s, x, ty ())
420 val declared = problem |> declared_syms_in_problem
422 problem |> undeclared_syms_in_problem declared
423 |> sort_wrt (fst o fst)
425 in (heading, decls) :: problem end
429 fun empty_name_pool readable_names =
430 if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
432 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
434 pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
439 | skip (#"." :: cs) = skip cs
440 | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
442 | keep (#"." :: cs) = skip cs
443 | keep (c :: cs) = c :: keep cs
444 in String.explode #> rev #> keep #> rev #> String.implode end
446 (* Long names can slow down the ATPs. *)
447 val max_readable_name_size = 20
449 (* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
450 unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
451 ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
452 is still necessary). *)
453 val reserved_nice_names = [tptp_old_equal, "op", "eq"]
455 fun readable_name full_name s =
456 if s = full_name then
460 |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
462 if size s > max_readable_name_size then
463 String.substring (s, 0, max_readable_name_size div 2 - 4) ^
464 Word.toString (hashw_string (full_name, 0w0)) ^
465 String.extract (s, size s - max_readable_name_size div 2 + 4,
469 |> (fn s => if member (op =) reserved_nice_names s then full_name else s)
471 fun nice_name (full_name, _) NONE = (full_name, NONE)
472 | nice_name (full_name, desired_name) (SOME the_pool) =
473 if is_built_in_tptp_symbol full_name then
474 (full_name, SOME the_pool)
475 else case Symtab.lookup (fst the_pool) full_name of
476 SOME nice_name => (nice_name, SOME the_pool)
479 val nice_prefix = readable_name full_name desired_name
483 nice_prefix ^ (if j = 0 then "" else "_" ^ string_of_int j)
485 case Symtab.lookup (snd the_pool) nice_name of
487 if full_name = full_name' then (nice_name, the_pool)
491 (Symtab.update_new (full_name, nice_name) (fst the_pool),
492 Symtab.update_new (nice_name, full_name) (snd the_pool)))
494 in add 0 |> apsnd SOME end
496 fun nice_term (ATerm (name, ts)) =
497 nice_name name ##>> pool_map nice_term ts #>> ATerm
498 fun nice_type (AType name) = nice_name name #>> AType
499 | nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
500 fun nice_formula (AQuant (q, xs, phi)) =
501 pool_map nice_name (map fst xs)
502 ##>> pool_map (fn NONE => pair NONE
503 | SOME ty => nice_type ty #>> SOME) (map snd xs)
504 ##>> nice_formula phi
505 #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
506 | nice_formula (AConn (c, phis)) =
507 pool_map nice_formula phis #>> curry AConn c
508 | nice_formula (AAtom tm) = nice_term tm #>> AAtom
509 fun nice_problem_line (Decl (ident, sym, ty)) =
510 nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
511 | nice_problem_line (Formula (ident, kind, phi, source, info)) =
512 nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
513 fun nice_problem problem =
514 pool_map (fn (heading, lines) =>
515 pool_map nice_problem_line lines #>> pair heading) problem
516 fun nice_atp_problem readable_names problem =
517 nice_problem problem (empty_name_pool readable_names)