blanchet@38293
|
1 |
(* Title: HOL/Tools/ATP/atp_problem.ML
|
blanchet@38261
|
2 |
Author: Jia Meng, Cambridge University Computer Laboratory and NICTA
|
blanchet@37509
|
3 |
Author: Jasmin Blanchette, TU Muenchen
|
blanchet@37509
|
4 |
|
blanchet@39692
|
5 |
Abstract representation of ATP problems and TPTP syntax.
|
blanchet@37509
|
6 |
*)
|
blanchet@37509
|
7 |
|
blanchet@38253
|
8 |
signature ATP_PROBLEM =
|
blanchet@37509
|
9 |
sig
|
nik@44535
|
10 |
datatype ('a, 'b) ho_term =
|
nik@44535
|
11 |
ATerm of 'a * ('a, 'b) ho_term list |
|
nik@44535
|
12 |
AAbs of ('a * 'b) * ('a, 'b) ho_term
|
blanchet@38226
|
13 |
datatype quantifier = AForall | AExists
|
blanchet@44004
|
14 |
datatype connective = ANot | AAnd | AOr | AImplies | AIff
|
blanchet@43402
|
15 |
datatype ('a, 'b, 'c) formula =
|
blanchet@43402
|
16 |
AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
|
blanchet@43402
|
17 |
AConn of connective * ('a, 'b, 'c) formula list |
|
blanchet@43402
|
18 |
AAtom of 'c
|
blanchet@38228
|
19 |
|
blanchet@43804
|
20 |
datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
|
blanchet@43804
|
21 |
|
blanchet@43933
|
22 |
datatype format = CNF | CNF_UEQ | FOF | TFF | THF
|
blanchet@43396
|
23 |
datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
|
blanchet@43398
|
24 |
datatype 'a problem_line =
|
blanchet@43804
|
25 |
Decl of string * 'a * 'a ho_type |
|
nik@44535
|
26 |
Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
|
nik@44535
|
27 |
* (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
|
blanchet@38251
|
28 |
type 'a problem = (string * 'a problem_line list) list
|
blanchet@38226
|
29 |
|
blanchet@43809
|
30 |
val tptp_cnf : string
|
blanchet@43809
|
31 |
val tptp_fof : string
|
blanchet@43809
|
32 |
val tptp_tff : string
|
blanchet@43809
|
33 |
val tptp_thf : string
|
blanchet@43808
|
34 |
val tptp_has_type : string
|
blanchet@43803
|
35 |
val tptp_type_of_types : string
|
blanchet@43803
|
36 |
val tptp_bool_type : string
|
blanchet@43803
|
37 |
val tptp_individual_type : string
|
blanchet@43804
|
38 |
val tptp_fun_type : string
|
blanchet@43839
|
39 |
val tptp_product_type : string
|
blanchet@43809
|
40 |
val tptp_forall : string
|
nik@44537
|
41 |
val tptp_ho_forall : string
|
blanchet@43809
|
42 |
val tptp_exists : string
|
nik@44537
|
43 |
val tptp_ho_exists : string
|
blanchet@43809
|
44 |
val tptp_not : string
|
blanchet@43809
|
45 |
val tptp_and : string
|
blanchet@43809
|
46 |
val tptp_or : string
|
blanchet@43809
|
47 |
val tptp_implies : string
|
blanchet@43809
|
48 |
val tptp_if : string
|
blanchet@43809
|
49 |
val tptp_iff : string
|
blanchet@43809
|
50 |
val tptp_not_iff : string
|
blanchet@43809
|
51 |
val tptp_app : string
|
blanchet@43809
|
52 |
val tptp_not_infix : string
|
blanchet@43809
|
53 |
val tptp_equal : string
|
blanchet@43841
|
54 |
val tptp_old_equal : string
|
blanchet@43807
|
55 |
val tptp_false : string
|
blanchet@43807
|
56 |
val tptp_true : string
|
blanchet@43841
|
57 |
val tptp_empty_list : string
|
blanchet@43841
|
58 |
val is_tptp_equal : string -> bool
|
blanchet@43839
|
59 |
val is_built_in_tptp_symbol : string -> bool
|
blanchet@43839
|
60 |
val is_tptp_variable : string -> bool
|
blanchet@43839
|
61 |
val is_tptp_user_symbol : string -> bool
|
blanchet@43783
|
62 |
val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
|
blanchet@43783
|
63 |
val mk_aconn :
|
blanchet@43783
|
64 |
connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
|
blanchet@43783
|
65 |
-> ('a, 'b, 'c) formula
|
blanchet@43839
|
66 |
val aconn_fold :
|
blanchet@43839
|
67 |
bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
|
blanchet@43839
|
68 |
-> 'b -> 'b
|
blanchet@43839
|
69 |
val aconn_map :
|
blanchet@43839
|
70 |
bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
|
blanchet@43839
|
71 |
-> connective * 'a list -> ('b, 'c, 'd) formula
|
blanchet@43839
|
72 |
val formula_fold :
|
blanchet@43839
|
73 |
bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
|
blanchet@43839
|
74 |
-> 'd -> 'd
|
blanchet@43785
|
75 |
val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
|
blanchet@43839
|
76 |
val is_format_typed : format -> bool
|
blanchet@44065
|
77 |
val tptp_lines_for_atp_problem : format -> string problem -> string list
|
blanchet@43933
|
78 |
val ensure_cnf_problem :
|
blanchet@43933
|
79 |
(string * string) problem -> (string * string) problem
|
blanchet@43780
|
80 |
val filter_cnf_ueq_problem :
|
blanchet@43780
|
81 |
(string * string) problem -> (string * string) problem
|
blanchet@43839
|
82 |
val declare_undeclared_syms_in_atp_problem :
|
blanchet@43839
|
83 |
string -> string -> (string * string) problem -> (string * string) problem
|
blanchet@39692
|
84 |
val nice_atp_problem :
|
blanchet@38251
|
85 |
bool -> ('a * (string * string) problem_line list) list
|
blanchet@38251
|
86 |
-> ('a * string problem_line list) list
|
blanchet@38251
|
87 |
* (string Symtab.table * string Symtab.table) option
|
blanchet@37509
|
88 |
end;
|
blanchet@37509
|
89 |
|
blanchet@38253
|
90 |
structure ATP_Problem : ATP_PROBLEM =
|
blanchet@37509
|
91 |
struct
|
blanchet@37509
|
92 |
|
blanchet@43926
|
93 |
open ATP_Util
|
blanchet@43926
|
94 |
|
blanchet@43926
|
95 |
|
blanchet@37643
|
96 |
(** ATP problem **)
|
blanchet@37643
|
97 |
|
nik@44535
|
98 |
datatype ('a, 'b) ho_term =
|
nik@44535
|
99 |
ATerm of 'a * ('a, 'b) ho_term list |
|
nik@44535
|
100 |
AAbs of ('a * 'b) * ('a, 'b) ho_term
|
blanchet@38198
|
101 |
datatype quantifier = AForall | AExists
|
blanchet@44004
|
102 |
datatype connective = ANot | AAnd | AOr | AImplies | AIff
|
blanchet@43402
|
103 |
datatype ('a, 'b, 'c) formula =
|
blanchet@43402
|
104 |
AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
|
blanchet@43402
|
105 |
AConn of connective * ('a, 'b, 'c) formula list |
|
blanchet@43402
|
106 |
AAtom of 'c
|
blanchet@38198
|
107 |
|
blanchet@43804
|
108 |
datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
|
blanchet@43804
|
109 |
|
blanchet@43933
|
110 |
datatype format = CNF | CNF_UEQ | FOF | TFF | THF
|
blanchet@43396
|
111 |
datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
|
blanchet@43398
|
112 |
datatype 'a problem_line =
|
blanchet@43804
|
113 |
Decl of string * 'a * 'a ho_type |
|
nik@44535
|
114 |
Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
|
nik@44535
|
115 |
* (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
|
blanchet@37643
|
116 |
type 'a problem = (string * 'a problem_line list) list
|
blanchet@37643
|
117 |
|
blanchet@43587
|
118 |
(* official TPTP syntax *)
|
blanchet@43809
|
119 |
val tptp_cnf = "cnf"
|
blanchet@43809
|
120 |
val tptp_fof = "fof"
|
blanchet@43809
|
121 |
val tptp_tff = "tff"
|
blanchet@43809
|
122 |
val tptp_thf = "thf"
|
blanchet@43808
|
123 |
val tptp_has_type = ":"
|
blanchet@43803
|
124 |
val tptp_type_of_types = "$tType"
|
blanchet@43803
|
125 |
val tptp_bool_type = "$o"
|
blanchet@43803
|
126 |
val tptp_individual_type = "$i"
|
blanchet@43804
|
127 |
val tptp_fun_type = ">"
|
blanchet@43839
|
128 |
val tptp_product_type = "*"
|
blanchet@43809
|
129 |
val tptp_forall = "!"
|
nik@44537
|
130 |
val tptp_ho_forall = "!!"
|
blanchet@43809
|
131 |
val tptp_exists = "?"
|
nik@44537
|
132 |
val tptp_ho_exists = "??"
|
blanchet@43809
|
133 |
val tptp_not = "~"
|
blanchet@43809
|
134 |
val tptp_and = "&"
|
blanchet@43809
|
135 |
val tptp_or = "|"
|
blanchet@43809
|
136 |
val tptp_implies = "=>"
|
blanchet@43809
|
137 |
val tptp_if = "<="
|
blanchet@43809
|
138 |
val tptp_iff = "<=>"
|
blanchet@43809
|
139 |
val tptp_not_iff = "<~>"
|
blanchet@43809
|
140 |
val tptp_app = "@"
|
blanchet@43809
|
141 |
val tptp_not_infix = "!"
|
blanchet@43809
|
142 |
val tptp_equal = "="
|
blanchet@43841
|
143 |
val tptp_old_equal = "equal"
|
blanchet@43807
|
144 |
val tptp_false = "$false"
|
blanchet@43807
|
145 |
val tptp_true = "$true"
|
blanchet@43841
|
146 |
val tptp_empty_list = "[]"
|
blanchet@43587
|
147 |
|
blanchet@43841
|
148 |
fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
|
blanchet@43841
|
149 |
fun is_built_in_tptp_symbol s =
|
blanchet@43841
|
150 |
s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
|
blanchet@43839
|
151 |
fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
|
blanchet@43839
|
152 |
val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
|
blanchet@43780
|
153 |
|
blanchet@43939
|
154 |
fun raw_polarities_of_conn ANot = (SOME false, NONE)
|
blanchet@43939
|
155 |
| raw_polarities_of_conn AAnd = (SOME true, SOME true)
|
blanchet@43939
|
156 |
| raw_polarities_of_conn AOr = (SOME true, SOME true)
|
blanchet@43939
|
157 |
| raw_polarities_of_conn AImplies = (SOME false, SOME true)
|
blanchet@43939
|
158 |
| raw_polarities_of_conn AIff = (NONE, NONE)
|
blanchet@43939
|
159 |
fun polarities_of_conn NONE = K (NONE, NONE)
|
blanchet@43939
|
160 |
| polarities_of_conn (SOME pos) =
|
blanchet@43939
|
161 |
raw_polarities_of_conn #> not pos ? pairself (Option.map not)
|
blanchet@43939
|
162 |
|
blanchet@43783
|
163 |
fun mk_anot (AConn (ANot, [phi])) = phi
|
blanchet@43783
|
164 |
| mk_anot phi = AConn (ANot, [phi])
|
blanchet@43783
|
165 |
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
|
blanchet@43783
|
166 |
|
blanchet@43839
|
167 |
fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
|
blanchet@43839
|
168 |
| aconn_fold pos f (AImplies, [phi1, phi2]) =
|
blanchet@43839
|
169 |
f (Option.map not pos) phi1 #> f pos phi2
|
blanchet@43839
|
170 |
| aconn_fold pos f (AAnd, phis) = fold (f pos) phis
|
blanchet@43839
|
171 |
| aconn_fold pos f (AOr, phis) = fold (f pos) phis
|
blanchet@43839
|
172 |
| aconn_fold _ f (_, phis) = fold (f NONE) phis
|
blanchet@43839
|
173 |
|
blanchet@43839
|
174 |
fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
|
blanchet@43839
|
175 |
| aconn_map pos f (AImplies, [phi1, phi2]) =
|
blanchet@43839
|
176 |
AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
|
blanchet@43839
|
177 |
| aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
|
blanchet@43839
|
178 |
| aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
|
blanchet@43839
|
179 |
| aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
|
blanchet@43839
|
180 |
|
blanchet@43839
|
181 |
fun formula_fold pos f =
|
blanchet@43839
|
182 |
let
|
blanchet@43839
|
183 |
fun aux pos (AQuant (_, _, phi)) = aux pos phi
|
blanchet@43839
|
184 |
| aux pos (AConn conn) = aconn_fold pos aux conn
|
blanchet@43839
|
185 |
| aux pos (AAtom tm) = f pos tm
|
blanchet@43839
|
186 |
in aux pos end
|
blanchet@43839
|
187 |
|
blanchet@43785
|
188 |
fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
|
blanchet@43785
|
189 |
| formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
|
blanchet@43785
|
190 |
| formula_map f (AAtom tm) = AAtom (f tm)
|
blanchet@43785
|
191 |
|
blanchet@43839
|
192 |
val is_format_typed = member (op =) [TFF, THF]
|
blanchet@43839
|
193 |
|
blanchet@38854
|
194 |
fun string_for_kind Axiom = "axiom"
|
blanchet@42640
|
195 |
| string_for_kind Definition = "definition"
|
blanchet@42640
|
196 |
| string_for_kind Lemma = "lemma"
|
blanchet@38854
|
197 |
| string_for_kind Hypothesis = "hypothesis"
|
blanchet@38854
|
198 |
| string_for_kind Conjecture = "conjecture"
|
blanchet@38854
|
199 |
|
blanchet@43804
|
200 |
fun strip_tff_type (AFun (AType s, ty)) = strip_tff_type ty |>> cons s
|
blanchet@43804
|
201 |
| strip_tff_type (AFun (AFun _, _)) =
|
blanchet@43804
|
202 |
raise Fail "unexpected higher-order type in first-order format"
|
blanchet@43804
|
203 |
| strip_tff_type (AType s) = ([], s)
|
blanchet@43804
|
204 |
|
blanchet@43804
|
205 |
fun string_for_type THF ty =
|
blanchet@43804
|
206 |
let
|
blanchet@43804
|
207 |
fun aux _ (AType s) = s
|
blanchet@43804
|
208 |
| aux rhs (AFun (ty1, ty2)) =
|
blanchet@43804
|
209 |
aux false ty1 ^ " " ^ tptp_fun_type ^ " " ^ aux true ty2
|
blanchet@43804
|
210 |
|> not rhs ? enclose "(" ")"
|
blanchet@43804
|
211 |
in aux true ty end
|
blanchet@43804
|
212 |
| string_for_type TFF ty =
|
blanchet@43804
|
213 |
(case strip_tff_type ty of
|
blanchet@43804
|
214 |
([], s) => s
|
blanchet@43804
|
215 |
| ([s'], s) => s' ^ " " ^ tptp_fun_type ^ " " ^ s
|
blanchet@43809
|
216 |
| (ss, s) =>
|
blanchet@43839
|
217 |
"(" ^ space_implode (" " ^ tptp_product_type ^ " ") ss ^ ") " ^
|
blanchet@43835
|
218 |
tptp_fun_type ^ " " ^ s)
|
blanchet@43804
|
219 |
| string_for_type _ _ = raise Fail "unexpected type in untyped format"
|
blanchet@43804
|
220 |
|
blanchet@43803
|
221 |
fun string_for_term _ (ATerm (s, [])) = s
|
blanchet@43803
|
222 |
| string_for_term format (ATerm (s, ts)) =
|
blanchet@43841
|
223 |
if s = tptp_empty_list then
|
blanchet@43841
|
224 |
(* used for lists in the optional "source" field of a derivation *)
|
blanchet@43841
|
225 |
"[" ^ commas (map (string_for_term format) ts) ^ "]"
|
blanchet@43841
|
226 |
else if is_tptp_equal s then
|
blanchet@43841
|
227 |
space_implode (" " ^ tptp_equal ^ " ") (map (string_for_term format) ts)
|
blanchet@43841
|
228 |
|> format = THF ? enclose "(" ")"
|
blanchet@43841
|
229 |
else
|
blanchet@43841
|
230 |
let val ss = map (string_for_term format) ts in
|
blanchet@43841
|
231 |
if format = THF then
|
blanchet@43841
|
232 |
"(" ^ space_implode (" " ^ tptp_app ^ " ") (s :: ss) ^ ")"
|
blanchet@43841
|
233 |
else
|
blanchet@43841
|
234 |
s ^ "(" ^ commas ss ^ ")"
|
blanchet@43841
|
235 |
end
|
nik@44535
|
236 |
| string_for_term THF (AAbs ((s, ty), tm)) =
|
nik@44536
|
237 |
"(^[" ^ s ^ ":" ^ string_for_type THF ty ^ "] : " ^ string_for_term THF tm ^ ")"
|
nik@44535
|
238 |
| string_for_term _ _ = raise Fail "unexpected term in first-order format"
|
blanchet@43804
|
239 |
|
blanchet@43809
|
240 |
fun string_for_quantifier AForall = tptp_forall
|
blanchet@43809
|
241 |
| string_for_quantifier AExists = tptp_exists
|
blanchet@43804
|
242 |
|
blanchet@43808
|
243 |
fun string_for_connective ANot = tptp_not
|
blanchet@43809
|
244 |
| string_for_connective AAnd = tptp_and
|
blanchet@43809
|
245 |
| string_for_connective AOr = tptp_or
|
blanchet@43809
|
246 |
| string_for_connective AImplies = tptp_implies
|
blanchet@43809
|
247 |
| string_for_connective AIff = tptp_iff
|
blanchet@43804
|
248 |
|
blanchet@43803
|
249 |
fun string_for_bound_var format (s, ty) =
|
blanchet@43804
|
250 |
s ^ (if format = TFF orelse format = THF then
|
blanchet@43809
|
251 |
" " ^ tptp_has_type ^ " " ^
|
blanchet@43804
|
252 |
string_for_type format (ty |> the_default (AType tptp_individual_type))
|
blanchet@43804
|
253 |
else
|
blanchet@43804
|
254 |
"")
|
blanchet@43804
|
255 |
|
blanchet@43587
|
256 |
fun string_for_formula format (AQuant (q, xs, phi)) =
|
blanchet@43815
|
257 |
string_for_quantifier q ^
|
blanchet@43587
|
258 |
"[" ^ commas (map (string_for_bound_var format) xs) ^ "] : " ^
|
blanchet@43815
|
259 |
string_for_formula format phi
|
blanchet@43815
|
260 |
|> enclose "(" ")"
|
blanchet@43841
|
261 |
| string_for_formula format
|
blanchet@43841
|
262 |
(AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
|
blanchet@43809
|
263 |
space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
|
blanchet@43809
|
264 |
(map (string_for_term format) ts)
|
blanchet@43804
|
265 |
|> format = THF ? enclose "(" ")"
|
blanchet@43587
|
266 |
| string_for_formula format (AConn (c, [phi])) =
|
blanchet@43815
|
267 |
string_for_connective c ^ " " ^
|
blanchet@43815
|
268 |
(string_for_formula format phi |> format = THF ? enclose "(" ")")
|
blanchet@43815
|
269 |
|> enclose "(" ")"
|
blanchet@43587
|
270 |
| string_for_formula format (AConn (c, phis)) =
|
blanchet@43815
|
271 |
space_implode (" " ^ string_for_connective c ^ " ")
|
blanchet@43815
|
272 |
(map (string_for_formula format) phis)
|
blanchet@43815
|
273 |
|> enclose "(" ")"
|
blanchet@43803
|
274 |
| string_for_formula format (AAtom tm) = string_for_term format tm
|
blanchet@38198
|
275 |
|
blanchet@44558
|
276 |
fun the_source (SOME source) = source
|
blanchet@44558
|
277 |
| the_source NONE =
|
blanchet@44558
|
278 |
ATerm ("inference",
|
blanchet@44558
|
279 |
ATerm ("isabelle", []) :: replicate 2 (ATerm ("[]", [])))
|
blanchet@43510
|
280 |
|
blanchet@43933
|
281 |
fun string_for_format CNF = tptp_cnf
|
blanchet@43933
|
282 |
| string_for_format CNF_UEQ = tptp_cnf
|
blanchet@43809
|
283 |
| string_for_format FOF = tptp_fof
|
blanchet@43809
|
284 |
| string_for_format TFF = tptp_tff
|
blanchet@43809
|
285 |
| string_for_format THF = tptp_thf
|
blanchet@43803
|
286 |
|
blanchet@43804
|
287 |
fun string_for_problem_line format (Decl (ident, sym, ty)) =
|
blanchet@43803
|
288 |
string_for_format format ^ "(" ^ ident ^ ", type,\n " ^ sym ^ " : " ^
|
blanchet@43804
|
289 |
string_for_type format ty ^ ").\n"
|
blanchet@43780
|
290 |
| string_for_problem_line format (Formula (ident, kind, phi, source, info)) =
|
blanchet@43803
|
291 |
string_for_format format ^ "(" ^ ident ^ ", " ^ string_for_kind kind ^
|
blanchet@43803
|
292 |
",\n (" ^ string_for_formula format phi ^ ")" ^
|
blanchet@43780
|
293 |
(case (source, info) of
|
blanchet@43580
|
294 |
(NONE, NONE) => ""
|
blanchet@43803
|
295 |
| (SOME tm, NONE) => ", " ^ string_for_term format tm
|
blanchet@43580
|
296 |
| (_, SOME tm) =>
|
blanchet@44558
|
297 |
", " ^ string_for_term format (the_source source) ^
|
blanchet@43803
|
298 |
", " ^ string_for_term format tm) ^ ").\n"
|
blanchet@44065
|
299 |
fun tptp_lines_for_atp_problem format problem =
|
blanchet@37643
|
300 |
"% This file was generated by Isabelle (most likely Sledgehammer)\n\
|
blanchet@37643
|
301 |
\% " ^ timestamp () ^ "\n" ::
|
blanchet@37643
|
302 |
maps (fn (_, []) => []
|
blanchet@37643
|
303 |
| (heading, lines) =>
|
wenzelm@41739
|
304 |
"\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
|
blanchet@43580
|
305 |
map (string_for_problem_line format) lines)
|
blanchet@38854
|
306 |
problem
|
blanchet@37643
|
307 |
|
blanchet@43780
|
308 |
|
blanchet@43939
|
309 |
(** CNF (Metis) and CNF UEQ (Waldmeister) **)
|
blanchet@43780
|
310 |
|
blanchet@43780
|
311 |
fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
|
blanchet@43780
|
312 |
| is_problem_line_negated _ = false
|
blanchet@43780
|
313 |
|
blanchet@44034
|
314 |
fun is_problem_line_cnf_ueq (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) =
|
blanchet@44034
|
315 |
is_tptp_equal s
|
blanchet@43780
|
316 |
| is_problem_line_cnf_ueq _ = false
|
blanchet@43780
|
317 |
|
blanchet@43785
|
318 |
fun open_conjecture_term (ATerm ((s, s'), tms)) =
|
nik@44535
|
319 |
ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
|
nik@44535
|
320 |
else (s, s'), tms |> map open_conjecture_term)
|
nik@44535
|
321 |
| open_conjecture_term _ = raise Fail "unexpected higher-order term"
|
blanchet@43939
|
322 |
fun open_formula conj =
|
blanchet@43939
|
323 |
let
|
blanchet@44034
|
324 |
(* We are conveniently assuming that all bound variable names are
|
blanchet@44034
|
325 |
distinct, which should be the case for the formulas we generate. *)
|
blanchet@44034
|
326 |
fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
|
blanchet@44034
|
327 |
| opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
|
blanchet@43939
|
328 |
| opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
|
blanchet@43939
|
329 |
| opn pos (AConn (c, [phi1, phi2])) =
|
blanchet@43939
|
330 |
let val (pos1, pos2) = polarities_of_conn pos c in
|
blanchet@43939
|
331 |
AConn (c, [opn pos1 phi1, opn pos2 phi2])
|
blanchet@43939
|
332 |
end
|
blanchet@43939
|
333 |
| opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
|
blanchet@44283
|
334 |
| opn _ phi = phi
|
blanchet@43939
|
335 |
in opn (SOME (not conj)) end
|
blanchet@43785
|
336 |
fun open_formula_line (Formula (ident, kind, phi, source, info)) =
|
blanchet@43785
|
337 |
Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
|
blanchet@43785
|
338 |
| open_formula_line line = line
|
blanchet@43780
|
339 |
|
blanchet@43780
|
340 |
fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
|
blanchet@43783
|
341 |
Formula (ident, Hypothesis, mk_anot phi, source, info)
|
blanchet@43780
|
342 |
| negate_conjecture_line line = line
|
blanchet@43780
|
343 |
|
blanchet@43939
|
344 |
exception CLAUSIFY of unit
|
blanchet@43939
|
345 |
|
blanchet@43967
|
346 |
(* This "clausification" only expands syntactic sugar, such as "phi => psi" to
|
blanchet@43967
|
347 |
"~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
|
blanchet@43967
|
348 |
attempt to distribute conjunctions over disjunctions. *)
|
blanchet@44034
|
349 |
fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
|
blanchet@44034
|
350 |
| clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
|
blanchet@44034
|
351 |
| clausify_formula true (AConn (AOr, [phi1, phi2])) =
|
blanchet@44034
|
352 |
(phi1, phi2) |> pairself (clausify_formula true)
|
blanchet@44034
|
353 |
|> uncurry (map_product (mk_aconn AOr))
|
blanchet@44034
|
354 |
| clausify_formula false (AConn (AAnd, [phi1, phi2])) =
|
blanchet@44034
|
355 |
(phi1, phi2) |> pairself (clausify_formula false)
|
blanchet@44034
|
356 |
|> uncurry (map_product (mk_aconn AOr))
|
blanchet@44034
|
357 |
| clausify_formula true (AConn (AImplies, [phi1, phi2])) =
|
blanchet@44034
|
358 |
clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
|
blanchet@44034
|
359 |
| clausify_formula true (AConn (AIff, phis)) =
|
blanchet@44034
|
360 |
clausify_formula true (AConn (AImplies, phis)) @
|
blanchet@44034
|
361 |
clausify_formula true (AConn (AImplies, rev phis))
|
blanchet@44034
|
362 |
| clausify_formula _ _ = raise CLAUSIFY ()
|
blanchet@43939
|
363 |
|
blanchet@43939
|
364 |
fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
|
blanchet@43967
|
365 |
let
|
blanchet@43967
|
366 |
val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
|
blanchet@43967
|
367 |
in
|
blanchet@43967
|
368 |
map2 (fn phi => fn j =>
|
blanchet@44153
|
369 |
Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
|
blanchet@44153
|
370 |
info))
|
blanchet@43967
|
371 |
phis (1 upto n)
|
blanchet@43967
|
372 |
end
|
blanchet@43967
|
373 |
| clausify_formula_line _ = []
|
blanchet@43939
|
374 |
|
blanchet@43939
|
375 |
fun ensure_cnf_problem_line line =
|
blanchet@43939
|
376 |
line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
|
blanchet@43939
|
377 |
|
blanchet@43939
|
378 |
fun ensure_cnf_problem problem =
|
blanchet@43967
|
379 |
problem |> map (apsnd (maps ensure_cnf_problem_line))
|
blanchet@43933
|
380 |
|
blanchet@43803
|
381 |
fun filter_cnf_ueq_problem problem =
|
blanchet@43803
|
382 |
problem
|
blanchet@43803
|
383 |
|> map (apsnd (map open_formula_line
|
blanchet@43803
|
384 |
#> filter is_problem_line_cnf_ueq
|
blanchet@43803
|
385 |
#> map negate_conjecture_line))
|
blanchet@43803
|
386 |
|> (fn problem =>
|
blanchet@43780
|
387 |
let
|
blanchet@44688
|
388 |
val lines = problem |> maps snd
|
blanchet@44688
|
389 |
val conjs = lines |> filter is_problem_line_negated
|
blanchet@44688
|
390 |
in if length conjs = 1 andalso conjs <> lines then problem else [] end)
|
blanchet@38251
|
391 |
|
blanchet@37643
|
392 |
|
blanchet@43839
|
393 |
(** Symbol declarations **)
|
blanchet@43839
|
394 |
|
blanchet@43839
|
395 |
(* TFF allows implicit declarations of types, function symbols, and predicate
|
blanchet@43839
|
396 |
symbols (with "$i" as the type of individuals), but some provers (e.g.,
|
blanchet@43839
|
397 |
SNARK) require explicit declarations. The situation is similar for THF. *)
|
blanchet@43839
|
398 |
|
blanchet@43839
|
399 |
val atype_of_types = AType (`I tptp_type_of_types)
|
blanchet@43839
|
400 |
val bool_atype = AType (`I tptp_bool_type)
|
blanchet@43839
|
401 |
val individual_atype = AType (`I tptp_individual_type)
|
blanchet@43839
|
402 |
|
blanchet@43839
|
403 |
fun default_type pred_sym =
|
blanchet@43839
|
404 |
let
|
blanchet@43839
|
405 |
fun typ 0 = if pred_sym then bool_atype else individual_atype
|
blanchet@43839
|
406 |
| typ ary = AFun (individual_atype, typ (ary - 1))
|
blanchet@43839
|
407 |
in typ end
|
blanchet@43839
|
408 |
|
blanchet@43839
|
409 |
fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = insert (op =) sym
|
blanchet@43839
|
410 |
| add_declared_syms_in_problem_line _ = I
|
blanchet@43839
|
411 |
fun declared_syms_in_problem problem =
|
blanchet@43839
|
412 |
fold (fold add_declared_syms_in_problem_line o snd) problem []
|
blanchet@43839
|
413 |
|
blanchet@43839
|
414 |
fun undeclared_syms_in_problem declared problem =
|
blanchet@43839
|
415 |
let
|
blanchet@43839
|
416 |
fun do_sym name ty =
|
blanchet@43839
|
417 |
if member (op =) declared name then I else AList.default (op =) (name, ty)
|
blanchet@43839
|
418 |
fun do_type (AFun (ty1, ty2)) = fold do_type [ty1, ty2]
|
blanchet@43839
|
419 |
| do_type (AType name) = do_sym name (K atype_of_types)
|
blanchet@43839
|
420 |
fun do_term pred_sym (ATerm (name as (s, _), tms)) =
|
nik@44535
|
421 |
is_tptp_user_symbol s
|
nik@44535
|
422 |
? do_sym name (fn _ => default_type pred_sym (length tms))
|
nik@44535
|
423 |
#> fold (do_term false) tms
|
nik@44535
|
424 |
| do_term _ (AAbs ((_, ty), tm)) = do_type ty #> do_term false tm
|
blanchet@43839
|
425 |
fun do_formula (AQuant (_, xs, phi)) =
|
blanchet@43839
|
426 |
fold do_type (map_filter snd xs) #> do_formula phi
|
blanchet@43839
|
427 |
| do_formula (AConn (_, phis)) = fold do_formula phis
|
blanchet@43839
|
428 |
| do_formula (AAtom tm) = do_term true tm
|
blanchet@43839
|
429 |
fun do_problem_line (Decl (_, _, ty)) = do_type ty
|
blanchet@43839
|
430 |
| do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
|
blanchet@43839
|
431 |
in
|
blanchet@43839
|
432 |
fold (fold do_problem_line o snd) problem []
|
blanchet@43839
|
433 |
|> filter_out (is_built_in_tptp_symbol o fst o fst)
|
blanchet@43839
|
434 |
end
|
blanchet@43839
|
435 |
|
blanchet@43839
|
436 |
fun declare_undeclared_syms_in_atp_problem prefix heading problem =
|
blanchet@43839
|
437 |
let
|
blanchet@43839
|
438 |
fun decl_line (x as (s, _), ty) = Decl (prefix ^ s, x, ty ())
|
blanchet@43839
|
439 |
val declared = problem |> declared_syms_in_problem
|
blanchet@43839
|
440 |
val decls =
|
blanchet@43839
|
441 |
problem |> undeclared_syms_in_problem declared
|
blanchet@43839
|
442 |
|> sort_wrt (fst o fst)
|
blanchet@43839
|
443 |
|> map decl_line
|
blanchet@43839
|
444 |
in (heading, decls) :: problem end
|
blanchet@43839
|
445 |
|
blanchet@37643
|
446 |
(** Nice names **)
|
blanchet@37643
|
447 |
|
blanchet@37624
|
448 |
fun empty_name_pool readable_names =
|
blanchet@37643
|
449 |
if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
|
blanchet@37624
|
450 |
|
blanchet@37624
|
451 |
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
|
blanchet@37624
|
452 |
fun pool_map f xs =
|
blanchet@37624
|
453 |
pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
|
blanchet@37624
|
454 |
|
blanchet@43088
|
455 |
val no_qualifiers =
|
blanchet@43088
|
456 |
let
|
blanchet@43088
|
457 |
fun skip [] = []
|
blanchet@43088
|
458 |
| skip (#"." :: cs) = skip cs
|
blanchet@43088
|
459 |
| skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
|
blanchet@43088
|
460 |
and keep [] = []
|
blanchet@43088
|
461 |
| keep (#"." :: cs) = skip cs
|
blanchet@43088
|
462 |
| keep (c :: cs) = c :: keep cs
|
blanchet@43088
|
463 |
in String.explode #> rev #> keep #> rev #> String.implode end
|
blanchet@43088
|
464 |
|
blanchet@43626
|
465 |
(* Long names can slow down the ATPs. *)
|
blanchet@43589
|
466 |
val max_readable_name_size = 20
|
blanchet@43438
|
467 |
|
blanchet@43841
|
468 |
(* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
|
blanchet@43841
|
469 |
unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
|
blanchet@43841
|
470 |
ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
|
blanchet@43841
|
471 |
is still necessary). *)
|
blanchet@43841
|
472 |
val reserved_nice_names = [tptp_old_equal, "op", "eq"]
|
blanchet@43780
|
473 |
|
blanchet@37624
|
474 |
fun readable_name full_name s =
|
blanchet@37643
|
475 |
if s = full_name then
|
blanchet@37643
|
476 |
s
|
blanchet@37643
|
477 |
else
|
blanchet@43438
|
478 |
s |> no_qualifiers
|
blanchet@43438
|
479 |
|> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
|
blanchet@43454
|
480 |
|> (fn s =>
|
blanchet@43530
|
481 |
if size s > max_readable_name_size then
|
blanchet@43530
|
482 |
String.substring (s, 0, max_readable_name_size div 2 - 4) ^
|
blanchet@43454
|
483 |
Word.toString (hashw_string (full_name, 0w0)) ^
|
blanchet@43530
|
484 |
String.extract (s, size s - max_readable_name_size div 2 + 4,
|
blanchet@43530
|
485 |
NONE)
|
blanchet@43454
|
486 |
else
|
blanchet@43454
|
487 |
s)
|
blanchet@43438
|
488 |
|> (fn s => if member (op =) reserved_nice_names s then full_name else s)
|
blanchet@37624
|
489 |
|
blanchet@37624
|
490 |
fun nice_name (full_name, _) NONE = (full_name, NONE)
|
blanchet@37624
|
491 |
| nice_name (full_name, desired_name) (SOME the_pool) =
|
blanchet@43839
|
492 |
if is_built_in_tptp_symbol full_name then
|
blanchet@39630
|
493 |
(full_name, SOME the_pool)
|
blanchet@39630
|
494 |
else case Symtab.lookup (fst the_pool) full_name of
|
blanchet@37624
|
495 |
SOME nice_name => (nice_name, SOME the_pool)
|
blanchet@37624
|
496 |
| NONE =>
|
blanchet@37624
|
497 |
let
|
blanchet@37624
|
498 |
val nice_prefix = readable_name full_name desired_name
|
blanchet@37624
|
499 |
fun add j =
|
blanchet@37624
|
500 |
let
|
blanchet@43515
|
501 |
val nice_name =
|
blanchet@43783
|
502 |
nice_prefix ^ (if j = 0 then "" else "_" ^ string_of_int j)
|
blanchet@37624
|
503 |
in
|
blanchet@37624
|
504 |
case Symtab.lookup (snd the_pool) nice_name of
|
blanchet@37624
|
505 |
SOME full_name' =>
|
blanchet@37624
|
506 |
if full_name = full_name' then (nice_name, the_pool)
|
blanchet@37624
|
507 |
else add (j + 1)
|
blanchet@37624
|
508 |
| NONE =>
|
blanchet@37624
|
509 |
(nice_name,
|
blanchet@37624
|
510 |
(Symtab.update_new (full_name, nice_name) (fst the_pool),
|
blanchet@37624
|
511 |
Symtab.update_new (nice_name, full_name) (snd the_pool)))
|
blanchet@37624
|
512 |
end
|
blanchet@37624
|
513 |
in add 0 |> apsnd SOME end
|
blanchet@37624
|
514 |
|
blanchet@43804
|
515 |
fun nice_type (AType name) = nice_name name #>> AType
|
blanchet@43804
|
516 |
| nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
|
nik@44535
|
517 |
fun nice_term (ATerm (name, ts)) =
|
nik@44535
|
518 |
nice_name name ##>> pool_map nice_term ts #>> ATerm
|
nik@44535
|
519 |
| nice_term (AAbs ((name, ty), tm)) =
|
nik@44535
|
520 |
nice_name name ##>> nice_type ty ##>> nice_term tm #>> AAbs
|
blanchet@38198
|
521 |
fun nice_formula (AQuant (q, xs, phi)) =
|
blanchet@43397
|
522 |
pool_map nice_name (map fst xs)
|
blanchet@43397
|
523 |
##>> pool_map (fn NONE => pair NONE
|
blanchet@43804
|
524 |
| SOME ty => nice_type ty #>> SOME) (map snd xs)
|
blanchet@43397
|
525 |
##>> nice_formula phi
|
blanchet@43397
|
526 |
#>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
|
blanchet@38198
|
527 |
| nice_formula (AConn (c, phis)) =
|
blanchet@38198
|
528 |
pool_map nice_formula phis #>> curry AConn c
|
blanchet@38280
|
529 |
| nice_formula (AAtom tm) = nice_term tm #>> AAtom
|
blanchet@43804
|
530 |
fun nice_problem_line (Decl (ident, sym, ty)) =
|
blanchet@43839
|
531 |
nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
|
blanchet@43780
|
532 |
| nice_problem_line (Formula (ident, kind, phi, source, info)) =
|
blanchet@43780
|
533 |
nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
|
blanchet@38171
|
534 |
fun nice_problem problem =
|
blanchet@37643
|
535 |
pool_map (fn (heading, lines) =>
|
blanchet@38171
|
536 |
pool_map nice_problem_line lines #>> pair heading) problem
|
blanchet@39692
|
537 |
fun nice_atp_problem readable_names problem =
|
blanchet@38251
|
538 |
nice_problem problem (empty_name_pool readable_names)
|
blanchet@37509
|
539 |
|
blanchet@37509
|
540 |
end;
|