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