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 |
|
blanchet@48926
|
12 |
AAbs of (('a * 'b) * ('a, 'b) ho_term) * ('a, 'b) ho_term list
|
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@45458
|
22 |
AFun of 'a ho_type * 'a ho_type |
|
blanchet@45458
|
23 |
ATyAbs of 'a list * 'a ho_type
|
blanchet@43804
|
24 |
|
blanchet@47909
|
25 |
type term_order =
|
blanchet@47909
|
26 |
{is_lpo : bool,
|
blanchet@47909
|
27 |
gen_weights : bool,
|
blanchet@47909
|
28 |
gen_prec : bool,
|
blanchet@47909
|
29 |
gen_simp : bool}
|
blanchet@47909
|
30 |
|
blanchet@45618
|
31 |
datatype tptp_polymorphism = TPTP_Monomorphic | TPTP_Polymorphic
|
blanchet@45618
|
32 |
datatype tptp_explicitness = TPTP_Implicit | TPTP_Explicit
|
blanchet@45453
|
33 |
datatype thf_flavor = THF_Without_Choice | THF_With_Choice
|
blanchet@46174
|
34 |
datatype dfg_flavor = DFG_Unsorted | DFG_Sorted
|
blanchet@46172
|
35 |
|
blanchet@46172
|
36 |
datatype atp_format =
|
blanchet@45106
|
37 |
CNF |
|
blanchet@45106
|
38 |
CNF_UEQ |
|
blanchet@45106
|
39 |
FOF |
|
blanchet@45618
|
40 |
TFF of tptp_polymorphism * tptp_explicitness |
|
blanchet@46172
|
41 |
THF of tptp_polymorphism * tptp_explicitness * thf_flavor |
|
blanchet@46174
|
42 |
DFG of dfg_flavor
|
blanchet@45106
|
43 |
|
blanchet@43396
|
44 |
datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
|
blanchet@43398
|
45 |
datatype 'a problem_line =
|
blanchet@43804
|
46 |
Decl of string * 'a * 'a ho_type |
|
blanchet@45261
|
47 |
Formula of string * formula_kind
|
blanchet@45261
|
48 |
* ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
|
blanchet@45261
|
49 |
* (string, string ho_type) ho_term option
|
blanchet@47234
|
50 |
* (string, string ho_type) ho_term list
|
blanchet@38251
|
51 |
type 'a problem = (string * 'a problem_line list) list
|
blanchet@38226
|
52 |
|
blanchet@43809
|
53 |
val tptp_cnf : string
|
blanchet@43809
|
54 |
val tptp_fof : string
|
blanchet@43809
|
55 |
val tptp_tff : string
|
blanchet@43809
|
56 |
val tptp_thf : string
|
blanchet@43808
|
57 |
val tptp_has_type : string
|
blanchet@43803
|
58 |
val tptp_type_of_types : string
|
blanchet@43803
|
59 |
val tptp_bool_type : string
|
blanchet@43803
|
60 |
val tptp_individual_type : string
|
blanchet@43804
|
61 |
val tptp_fun_type : string
|
blanchet@43839
|
62 |
val tptp_product_type : string
|
blanchet@43809
|
63 |
val tptp_forall : string
|
nik@44537
|
64 |
val tptp_ho_forall : string
|
blanchet@45514
|
65 |
val tptp_pi_binder : string
|
blanchet@43809
|
66 |
val tptp_exists : string
|
nik@44537
|
67 |
val tptp_ho_exists : string
|
nik@45350
|
68 |
val tptp_choice : string
|
blanchet@43809
|
69 |
val tptp_not : string
|
blanchet@43809
|
70 |
val tptp_and : string
|
blanchet@43809
|
71 |
val tptp_or : string
|
blanchet@43809
|
72 |
val tptp_implies : string
|
blanchet@43809
|
73 |
val tptp_if : string
|
blanchet@43809
|
74 |
val tptp_iff : string
|
blanchet@43809
|
75 |
val tptp_not_iff : string
|
blanchet@43809
|
76 |
val tptp_app : string
|
blanchet@43809
|
77 |
val tptp_not_infix : string
|
blanchet@43809
|
78 |
val tptp_equal : string
|
blanchet@43841
|
79 |
val tptp_old_equal : string
|
blanchet@43807
|
80 |
val tptp_false : string
|
blanchet@43807
|
81 |
val tptp_true : string
|
blanchet@43841
|
82 |
val tptp_empty_list : string
|
blanchet@47234
|
83 |
val isabelle_info_prefix : string
|
blanchet@47909
|
84 |
val isabelle_info : string -> int -> (string, 'a) ho_term list
|
blanchet@47270
|
85 |
val extract_isabelle_status : (string, 'a) ho_term list -> string option
|
blanchet@47270
|
86 |
val extract_isabelle_rank : (string, 'a) ho_term list -> int
|
blanchet@47234
|
87 |
val introN : string
|
blanchet@48019
|
88 |
val inductiveN : string
|
blanchet@47234
|
89 |
val elimN : string
|
blanchet@47234
|
90 |
val simpN : string
|
blanchet@48019
|
91 |
val defN : string
|
blanchet@47234
|
92 |
val rankN : string
|
blanchet@47234
|
93 |
val minimum_rank : int
|
blanchet@47234
|
94 |
val default_rank : int
|
blanchet@47901
|
95 |
val default_term_order_weight : int
|
blanchet@43841
|
96 |
val is_tptp_equal : string -> bool
|
blanchet@43839
|
97 |
val is_built_in_tptp_symbol : string -> bool
|
blanchet@43839
|
98 |
val is_tptp_variable : string -> bool
|
blanchet@43839
|
99 |
val is_tptp_user_symbol : string -> bool
|
blanchet@45459
|
100 |
val atype_of_types : (string * string) ho_type
|
blanchet@45459
|
101 |
val bool_atype : (string * string) ho_type
|
blanchet@45459
|
102 |
val individual_atype : (string * string) ho_type
|
blanchet@43783
|
103 |
val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
|
blanchet@43783
|
104 |
val mk_aconn :
|
blanchet@43783
|
105 |
connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
|
blanchet@43783
|
106 |
-> ('a, 'b, 'c) formula
|
blanchet@43839
|
107 |
val aconn_fold :
|
blanchet@43839
|
108 |
bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
|
blanchet@43839
|
109 |
-> 'b -> 'b
|
blanchet@43839
|
110 |
val aconn_map :
|
blanchet@43839
|
111 |
bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
|
blanchet@43839
|
112 |
-> connective * 'a list -> ('b, 'c, 'd) formula
|
blanchet@43839
|
113 |
val formula_fold :
|
blanchet@43839
|
114 |
bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
|
blanchet@43839
|
115 |
-> 'd -> 'd
|
blanchet@43785
|
116 |
val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
|
blanchet@47270
|
117 |
val is_function_type : string ho_type -> bool
|
blanchet@47270
|
118 |
val is_predicate_type : string ho_type -> bool
|
blanchet@46174
|
119 |
val is_format_higher_order : atp_format -> bool
|
blanchet@47270
|
120 |
val lines_for_atp_problem :
|
blanchet@47909
|
121 |
atp_format -> term_order -> (unit -> (string * int) list) -> string problem
|
blanchet@47909
|
122 |
-> string list
|
blanchet@43933
|
123 |
val ensure_cnf_problem :
|
blanchet@43933
|
124 |
(string * string) problem -> (string * string) problem
|
blanchet@43780
|
125 |
val filter_cnf_ueq_problem :
|
blanchet@43780
|
126 |
(string * string) problem -> (string * string) problem
|
blanchet@48021
|
127 |
val declared_syms_in_problem : 'a problem -> 'a list
|
blanchet@39692
|
128 |
val nice_atp_problem :
|
blanchet@46810
|
129 |
bool -> atp_format -> ('a * (string * string) problem_line list) list
|
blanchet@38251
|
130 |
-> ('a * string problem_line list) list
|
blanchet@38251
|
131 |
* (string Symtab.table * string Symtab.table) option
|
blanchet@37509
|
132 |
end;
|
blanchet@37509
|
133 |
|
blanchet@38253
|
134 |
structure ATP_Problem : ATP_PROBLEM =
|
blanchet@37509
|
135 |
struct
|
blanchet@37509
|
136 |
|
blanchet@43926
|
137 |
open ATP_Util
|
blanchet@43926
|
138 |
|
blanchet@43926
|
139 |
|
blanchet@37643
|
140 |
(** ATP problem **)
|
blanchet@37643
|
141 |
|
nik@44535
|
142 |
datatype ('a, 'b) ho_term =
|
nik@44535
|
143 |
ATerm of 'a * ('a, 'b) ho_term list |
|
blanchet@48926
|
144 |
AAbs of (('a * 'b) * ('a, 'b) ho_term) * ('a, 'b) ho_term list
|
blanchet@38198
|
145 |
datatype quantifier = AForall | AExists
|
blanchet@44004
|
146 |
datatype connective = ANot | AAnd | AOr | AImplies | AIff
|
blanchet@43402
|
147 |
datatype ('a, 'b, 'c) formula =
|
blanchet@43402
|
148 |
AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
|
blanchet@43402
|
149 |
AConn of connective * ('a, 'b, 'c) formula list |
|
blanchet@43402
|
150 |
AAtom of 'c
|
blanchet@38198
|
151 |
|
blanchet@45457
|
152 |
datatype 'a ho_type =
|
blanchet@45457
|
153 |
AType of 'a * 'a ho_type list |
|
blanchet@45458
|
154 |
AFun of 'a ho_type * 'a ho_type |
|
blanchet@45458
|
155 |
ATyAbs of 'a list * 'a ho_type
|
blanchet@43804
|
156 |
|
blanchet@47909
|
157 |
type term_order =
|
blanchet@47909
|
158 |
{is_lpo : bool,
|
blanchet@47909
|
159 |
gen_weights : bool,
|
blanchet@47909
|
160 |
gen_prec : bool,
|
blanchet@47909
|
161 |
gen_simp : bool}
|
blanchet@47909
|
162 |
|
blanchet@45618
|
163 |
datatype tptp_polymorphism = TPTP_Monomorphic | TPTP_Polymorphic
|
blanchet@45618
|
164 |
datatype tptp_explicitness = TPTP_Implicit | TPTP_Explicit
|
blanchet@45453
|
165 |
datatype thf_flavor = THF_Without_Choice | THF_With_Choice
|
blanchet@46174
|
166 |
datatype dfg_flavor = DFG_Unsorted | DFG_Sorted
|
blanchet@45354
|
167 |
|
blanchet@46172
|
168 |
datatype atp_format =
|
blanchet@45106
|
169 |
CNF |
|
blanchet@45106
|
170 |
CNF_UEQ |
|
blanchet@45106
|
171 |
FOF |
|
blanchet@45618
|
172 |
TFF of tptp_polymorphism * tptp_explicitness |
|
blanchet@46172
|
173 |
THF of tptp_polymorphism * tptp_explicitness * thf_flavor |
|
blanchet@46174
|
174 |
DFG of dfg_flavor
|
blanchet@45106
|
175 |
|
blanchet@43396
|
176 |
datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
|
blanchet@43398
|
177 |
datatype 'a problem_line =
|
blanchet@43804
|
178 |
Decl of string * 'a * 'a ho_type |
|
blanchet@47234
|
179 |
Formula of string * formula_kind
|
blanchet@47234
|
180 |
* ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
|
blanchet@47234
|
181 |
* (string, string ho_type) ho_term option
|
blanchet@47234
|
182 |
* (string, string ho_type) ho_term list
|
blanchet@37643
|
183 |
type 'a problem = (string * 'a problem_line list) list
|
blanchet@37643
|
184 |
|
blanchet@43587
|
185 |
(* official TPTP syntax *)
|
blanchet@43809
|
186 |
val tptp_cnf = "cnf"
|
blanchet@43809
|
187 |
val tptp_fof = "fof"
|
blanchet@43809
|
188 |
val tptp_tff = "tff"
|
blanchet@43809
|
189 |
val tptp_thf = "thf"
|
blanchet@43808
|
190 |
val tptp_has_type = ":"
|
blanchet@43803
|
191 |
val tptp_type_of_types = "$tType"
|
blanchet@43803
|
192 |
val tptp_bool_type = "$o"
|
blanchet@43803
|
193 |
val tptp_individual_type = "$i"
|
blanchet@43804
|
194 |
val tptp_fun_type = ">"
|
blanchet@43839
|
195 |
val tptp_product_type = "*"
|
blanchet@43809
|
196 |
val tptp_forall = "!"
|
nik@44537
|
197 |
val tptp_ho_forall = "!!"
|
blanchet@45514
|
198 |
val tptp_pi_binder = "!>"
|
blanchet@43809
|
199 |
val tptp_exists = "?"
|
nik@44537
|
200 |
val tptp_ho_exists = "??"
|
nik@45350
|
201 |
val tptp_choice = "@+"
|
blanchet@43809
|
202 |
val tptp_not = "~"
|
blanchet@43809
|
203 |
val tptp_and = "&"
|
blanchet@43809
|
204 |
val tptp_or = "|"
|
blanchet@43809
|
205 |
val tptp_implies = "=>"
|
blanchet@43809
|
206 |
val tptp_if = "<="
|
blanchet@43809
|
207 |
val tptp_iff = "<=>"
|
blanchet@43809
|
208 |
val tptp_not_iff = "<~>"
|
blanchet@43809
|
209 |
val tptp_app = "@"
|
blanchet@43809
|
210 |
val tptp_not_infix = "!"
|
blanchet@43809
|
211 |
val tptp_equal = "="
|
blanchet@43841
|
212 |
val tptp_old_equal = "equal"
|
blanchet@43807
|
213 |
val tptp_false = "$false"
|
blanchet@43807
|
214 |
val tptp_true = "$true"
|
blanchet@43841
|
215 |
val tptp_empty_list = "[]"
|
blanchet@43587
|
216 |
|
blanchet@47234
|
217 |
val isabelle_info_prefix = "isabelle_"
|
blanchet@47234
|
218 |
|
blanchet@47234
|
219 |
val introN = "intro"
|
blanchet@48019
|
220 |
val inductiveN = "inductive"
|
blanchet@47234
|
221 |
val elimN = "elim"
|
blanchet@47234
|
222 |
val simpN = "simp"
|
blanchet@48019
|
223 |
val defN = "def"
|
blanchet@47234
|
224 |
val rankN = "rank"
|
blanchet@47234
|
225 |
|
blanchet@47234
|
226 |
val minimum_rank = 0
|
blanchet@47234
|
227 |
val default_rank = 1000
|
blanchet@47901
|
228 |
val default_term_order_weight = 1
|
blanchet@47234
|
229 |
|
blanchet@47234
|
230 |
(* Currently, only newer versions of SPASS, with sorted DFG format support, can
|
blanchet@47234
|
231 |
process Isabelle metainformation. *)
|
blanchet@47909
|
232 |
fun isabelle_info status rank =
|
blanchet@47909
|
233 |
[] |> rank <> default_rank
|
blanchet@47909
|
234 |
? cons (ATerm (isabelle_info_prefix ^ rankN,
|
blanchet@47909
|
235 |
[ATerm (string_of_int rank, [])]))
|
blanchet@47909
|
236 |
|> status <> "" ? cons (ATerm (isabelle_info_prefix ^ status, []))
|
blanchet@47234
|
237 |
|
blanchet@47234
|
238 |
fun extract_isabelle_status (ATerm (s, []) :: _) =
|
blanchet@47234
|
239 |
try (unprefix isabelle_info_prefix) s
|
blanchet@47234
|
240 |
| extract_isabelle_status _ = NONE
|
blanchet@47234
|
241 |
|
blanchet@47234
|
242 |
fun extract_isabelle_rank (tms as _ :: _) =
|
blanchet@47234
|
243 |
(case List.last tms of
|
blanchet@47234
|
244 |
ATerm (_, [ATerm (rank, [])]) => the (Int.fromString rank)
|
blanchet@47234
|
245 |
| _ => default_rank)
|
blanchet@47234
|
246 |
| extract_isabelle_rank _ = default_rank
|
blanchet@47234
|
247 |
|
blanchet@43841
|
248 |
fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
|
blanchet@43841
|
249 |
fun is_built_in_tptp_symbol s =
|
blanchet@43841
|
250 |
s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
|
blanchet@43839
|
251 |
fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
|
blanchet@43839
|
252 |
val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
|
blanchet@43780
|
253 |
|
blanchet@46172
|
254 |
val atype_of_types = AType (`I tptp_type_of_types, [])
|
blanchet@46172
|
255 |
val bool_atype = AType (`I tptp_bool_type, [])
|
blanchet@46172
|
256 |
val individual_atype = AType (`I tptp_individual_type, [])
|
blanchet@46172
|
257 |
|
blanchet@43939
|
258 |
fun raw_polarities_of_conn ANot = (SOME false, NONE)
|
blanchet@43939
|
259 |
| raw_polarities_of_conn AAnd = (SOME true, SOME true)
|
blanchet@43939
|
260 |
| raw_polarities_of_conn AOr = (SOME true, SOME true)
|
blanchet@43939
|
261 |
| raw_polarities_of_conn AImplies = (SOME false, SOME true)
|
blanchet@43939
|
262 |
| raw_polarities_of_conn AIff = (NONE, NONE)
|
blanchet@43939
|
263 |
fun polarities_of_conn NONE = K (NONE, NONE)
|
blanchet@43939
|
264 |
| polarities_of_conn (SOME pos) =
|
blanchet@43939
|
265 |
raw_polarities_of_conn #> not pos ? pairself (Option.map not)
|
blanchet@43939
|
266 |
|
blanchet@43783
|
267 |
fun mk_anot (AConn (ANot, [phi])) = phi
|
blanchet@43783
|
268 |
| mk_anot phi = AConn (ANot, [phi])
|
blanchet@43783
|
269 |
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
|
blanchet@43783
|
270 |
|
blanchet@43839
|
271 |
fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
|
blanchet@43839
|
272 |
| aconn_fold pos f (AImplies, [phi1, phi2]) =
|
blanchet@43839
|
273 |
f (Option.map not pos) phi1 #> f pos phi2
|
blanchet@43839
|
274 |
| aconn_fold pos f (AAnd, phis) = fold (f pos) phis
|
blanchet@43839
|
275 |
| aconn_fold pos f (AOr, phis) = fold (f pos) phis
|
blanchet@43839
|
276 |
| aconn_fold _ f (_, phis) = fold (f NONE) phis
|
blanchet@43839
|
277 |
|
blanchet@43839
|
278 |
fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
|
blanchet@43839
|
279 |
| aconn_map pos f (AImplies, [phi1, phi2]) =
|
blanchet@43839
|
280 |
AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
|
blanchet@43839
|
281 |
| aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
|
blanchet@43839
|
282 |
| aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
|
blanchet@43839
|
283 |
| aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
|
blanchet@43839
|
284 |
|
blanchet@43839
|
285 |
fun formula_fold pos f =
|
blanchet@43839
|
286 |
let
|
blanchet@45356
|
287 |
fun fld pos (AQuant (_, _, phi)) = fld pos phi
|
blanchet@45356
|
288 |
| fld pos (AConn conn) = aconn_fold pos fld conn
|
blanchet@45356
|
289 |
| fld pos (AAtom tm) = f pos tm
|
blanchet@45356
|
290 |
in fld pos end
|
blanchet@43839
|
291 |
|
blanchet@43785
|
292 |
fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
|
blanchet@43785
|
293 |
| formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
|
blanchet@43785
|
294 |
| formula_map f (AAtom tm) = AAtom (f tm)
|
blanchet@43785
|
295 |
|
blanchet@47270
|
296 |
fun is_function_type (AFun (_, ty)) = is_function_type ty
|
blanchet@47270
|
297 |
| is_function_type (AType (s, _)) =
|
blanchet@47270
|
298 |
s <> tptp_type_of_types andalso s <> tptp_bool_type
|
blanchet@47270
|
299 |
| is_function_type _ = false
|
blanchet@47270
|
300 |
fun is_predicate_type (AFun (_, ty)) = is_predicate_type ty
|
blanchet@47270
|
301 |
| is_predicate_type (AType (s, _)) = (s = tptp_bool_type)
|
blanchet@47270
|
302 |
| is_predicate_type _ = false
|
blanchet@47270
|
303 |
fun is_nontrivial_predicate_type (AFun (_, ty)) = is_predicate_type ty
|
blanchet@47270
|
304 |
| is_nontrivial_predicate_type _ = false
|
blanchet@47270
|
305 |
|
blanchet@46174
|
306 |
fun is_format_higher_order (THF _) = true
|
blanchet@46174
|
307 |
| is_format_higher_order _ = false
|
blanchet@45354
|
308 |
fun is_format_typed (TFF _) = true
|
blanchet@45618
|
309 |
| is_format_typed (THF _) = true
|
blanchet@46174
|
310 |
| is_format_typed (DFG DFG_Sorted) = true
|
blanchet@45106
|
311 |
| is_format_typed _ = false
|
blanchet@43839
|
312 |
|
blanchet@46172
|
313 |
fun tptp_string_for_kind Axiom = "axiom"
|
blanchet@46172
|
314 |
| tptp_string_for_kind Definition = "definition"
|
blanchet@46172
|
315 |
| tptp_string_for_kind Lemma = "lemma"
|
blanchet@46172
|
316 |
| tptp_string_for_kind Hypothesis = "hypothesis"
|
blanchet@46172
|
317 |
| tptp_string_for_kind Conjecture = "conjecture"
|
blanchet@38854
|
318 |
|
blanchet@46172
|
319 |
fun tptp_string_for_app format func args =
|
blanchet@46174
|
320 |
if is_format_higher_order format then
|
blanchet@45655
|
321 |
"(" ^ space_implode (" " ^ tptp_app ^ " ") (func :: args) ^ ")"
|
blanchet@45655
|
322 |
else
|
blanchet@45655
|
323 |
func ^ "(" ^ commas args ^ ")"
|
blanchet@45655
|
324 |
|
blanchet@45458
|
325 |
fun flatten_type (ATyAbs (tys, ty)) = ATyAbs (tys, flatten_type ty)
|
blanchet@45458
|
326 |
| flatten_type (ty as AFun (ty1 as AType _, ty2)) =
|
blanchet@45458
|
327 |
(case flatten_type ty2 of
|
blanchet@45458
|
328 |
AFun (ty' as AType (s, tys), ty) =>
|
blanchet@45458
|
329 |
AFun (AType (tptp_product_type,
|
blanchet@45458
|
330 |
ty1 :: (if s = tptp_product_type then tys else [ty'])), ty)
|
blanchet@45458
|
331 |
| _ => ty)
|
blanchet@45458
|
332 |
| flatten_type (ty as AType _) = ty
|
blanchet@45458
|
333 |
| flatten_type _ =
|
blanchet@43804
|
334 |
raise Fail "unexpected higher-order type in first-order format"
|
blanchet@43804
|
335 |
|
blanchet@47273
|
336 |
val dfg_individual_type = "iii" (* cannot clash *)
|
blanchet@47166
|
337 |
|
blanchet@45655
|
338 |
fun str_for_type format ty =
|
blanchet@45457
|
339 |
let
|
blanchet@46174
|
340 |
val dfg = (format = DFG DFG_Sorted)
|
blanchet@46172
|
341 |
fun str _ (AType (s, [])) =
|
blanchet@47166
|
342 |
if dfg andalso s = tptp_individual_type then dfg_individual_type else s
|
blanchet@45458
|
343 |
| str _ (AType (s, tys)) =
|
blanchet@45655
|
344 |
let val ss = tys |> map (str false) in
|
blanchet@45655
|
345 |
if s = tptp_product_type then
|
blanchet@46172
|
346 |
ss |> space_implode
|
blanchet@46172
|
347 |
(if dfg then ", " else " " ^ tptp_product_type ^ " ")
|
blanchet@46172
|
348 |
|> (not dfg andalso length ss > 1) ? enclose "(" ")"
|
blanchet@45655
|
349 |
else
|
blanchet@46172
|
350 |
tptp_string_for_app format s ss
|
blanchet@45655
|
351 |
end
|
blanchet@45457
|
352 |
| str rhs (AFun (ty1, ty2)) =
|
blanchet@46172
|
353 |
(str false ty1 |> dfg ? enclose "(" ")") ^ " " ^
|
blanchet@46172
|
354 |
(if dfg then "" else tptp_fun_type ^ " ") ^ str true ty2
|
blanchet@45457
|
355 |
|> not rhs ? enclose "(" ")"
|
blanchet@45458
|
356 |
| str _ (ATyAbs (ss, ty)) =
|
blanchet@45514
|
357 |
tptp_pi_binder ^ "[" ^
|
blanchet@45458
|
358 |
commas (map (suffix (" " ^ tptp_has_type ^ " " ^ tptp_type_of_types))
|
blanchet@45603
|
359 |
ss) ^ "]: " ^ str false ty
|
blanchet@45457
|
360 |
in str true ty end
|
blanchet@45457
|
361 |
|
blanchet@45655
|
362 |
fun string_for_type (format as THF _) ty = str_for_type format ty
|
blanchet@46172
|
363 |
| string_for_type format ty = str_for_type format (flatten_type ty)
|
blanchet@43804
|
364 |
|
blanchet@46172
|
365 |
fun tptp_string_for_quantifier AForall = tptp_forall
|
blanchet@46172
|
366 |
| tptp_string_for_quantifier AExists = tptp_exists
|
blanchet@43804
|
367 |
|
blanchet@46172
|
368 |
fun tptp_string_for_connective ANot = tptp_not
|
blanchet@46172
|
369 |
| tptp_string_for_connective AAnd = tptp_and
|
blanchet@46172
|
370 |
| tptp_string_for_connective AOr = tptp_or
|
blanchet@46172
|
371 |
| tptp_string_for_connective AImplies = tptp_implies
|
blanchet@46172
|
372 |
| tptp_string_for_connective AIff = tptp_iff
|
blanchet@43804
|
373 |
|
blanchet@43803
|
374 |
fun string_for_bound_var format (s, ty) =
|
blanchet@45457
|
375 |
s ^
|
blanchet@45457
|
376 |
(if is_format_typed format then
|
blanchet@45457
|
377 |
" " ^ tptp_has_type ^ " " ^
|
blanchet@45457
|
378 |
(ty |> the_default (AType (tptp_individual_type, []))
|
blanchet@45457
|
379 |
|> string_for_type format)
|
blanchet@45457
|
380 |
else
|
blanchet@45457
|
381 |
"")
|
blanchet@43804
|
382 |
|
blanchet@45618
|
383 |
fun is_format_with_choice (THF (_, _, THF_With_Choice)) = true
|
blanchet@45618
|
384 |
| is_format_with_choice _ = false
|
blanchet@45618
|
385 |
|
blanchet@46172
|
386 |
fun tptp_string_for_term _ (ATerm (s, [])) = s
|
blanchet@46172
|
387 |
| tptp_string_for_term format (ATerm (s, ts)) =
|
blanchet@45655
|
388 |
(if s = tptp_empty_list then
|
blanchet@45655
|
389 |
(* used for lists in the optional "source" field of a derivation *)
|
blanchet@46172
|
390 |
"[" ^ commas (map (tptp_string_for_term format) ts) ^ "]"
|
blanchet@45655
|
391 |
else if is_tptp_equal s then
|
blanchet@46172
|
392 |
space_implode (" " ^ tptp_equal ^ " ")
|
blanchet@46172
|
393 |
(map (tptp_string_for_term format) ts)
|
blanchet@46174
|
394 |
|> is_format_higher_order format ? enclose "(" ")"
|
blanchet@45655
|
395 |
else case (s = tptp_ho_forall orelse s = tptp_ho_exists,
|
blanchet@45655
|
396 |
s = tptp_choice andalso is_format_with_choice format, ts) of
|
blanchet@48926
|
397 |
(true, _, [AAbs (((s', ty), tm), [])]) =>
|
blanchet@45655
|
398 |
(* Use syntactic sugar "!" and "?" instead of "!!" and "??" whenever
|
blanchet@45655
|
399 |
possible, to work around LEO-II 1.2.8 parser limitation. *)
|
blanchet@46172
|
400 |
tptp_string_for_formula format
|
blanchet@45655
|
401 |
(AQuant (if s = tptp_ho_forall then AForall else AExists,
|
blanchet@45655
|
402 |
[(s', SOME ty)], AAtom tm))
|
blanchet@48926
|
403 |
| (_, true, [AAbs (((s', ty), tm), args)]) =>
|
blanchet@47148
|
404 |
(* There is code in "ATP_Problem_Generate" to ensure that "Eps" is always
|
blanchet@45655
|
405 |
applied to an abstraction. *)
|
blanchet@48926
|
406 |
tptp_string_for_app format
|
blanchet@48926
|
407 |
(tptp_choice ^ "[" ^ s' ^ " : " ^ string_for_type format ty ^ "]: " ^
|
blanchet@48926
|
408 |
tptp_string_for_term format tm ^ ""
|
blanchet@48926
|
409 |
|> enclose "(" ")")
|
blanchet@48926
|
410 |
(map (tptp_string_for_term format) args)
|
blanchet@46172
|
411 |
| _ => tptp_string_for_app format s (map (tptp_string_for_term format) ts))
|
blanchet@48926
|
412 |
| tptp_string_for_term (format as THF _) (AAbs (((s, ty), tm), args)) =
|
blanchet@48926
|
413 |
tptp_string_for_app format
|
blanchet@48926
|
414 |
("(^[" ^ s ^ " : " ^ string_for_type format ty ^ "]: " ^
|
blanchet@48926
|
415 |
tptp_string_for_term format tm ^ ")")
|
blanchet@48926
|
416 |
(map (tptp_string_for_term format) args)
|
blanchet@46172
|
417 |
| tptp_string_for_term _ _ =
|
blanchet@46172
|
418 |
raise Fail "unexpected term in first-order format"
|
blanchet@46172
|
419 |
and tptp_string_for_formula format (AQuant (q, xs, phi)) =
|
blanchet@46172
|
420 |
tptp_string_for_quantifier q ^
|
blanchet@45603
|
421 |
"[" ^ commas (map (string_for_bound_var format) xs) ^ "]: " ^
|
blanchet@46172
|
422 |
tptp_string_for_formula format phi
|
blanchet@43815
|
423 |
|> enclose "(" ")"
|
blanchet@46172
|
424 |
| tptp_string_for_formula format
|
blanchet@43841
|
425 |
(AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
|
blanchet@43809
|
426 |
space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
|
blanchet@46172
|
427 |
(map (tptp_string_for_term format) ts)
|
blanchet@46174
|
428 |
|> is_format_higher_order format ? enclose "(" ")"
|
blanchet@46172
|
429 |
| tptp_string_for_formula format (AConn (c, [phi])) =
|
blanchet@46172
|
430 |
tptp_string_for_connective c ^ " " ^
|
blanchet@46172
|
431 |
(tptp_string_for_formula format phi
|
blanchet@46174
|
432 |
|> is_format_higher_order format ? enclose "(" ")")
|
blanchet@43815
|
433 |
|> enclose "(" ")"
|
blanchet@46172
|
434 |
| tptp_string_for_formula format (AConn (c, phis)) =
|
blanchet@46172
|
435 |
space_implode (" " ^ tptp_string_for_connective c ^ " ")
|
blanchet@46172
|
436 |
(map (tptp_string_for_formula format) phis)
|
blanchet@43815
|
437 |
|> enclose "(" ")"
|
blanchet@46172
|
438 |
| tptp_string_for_formula format (AAtom tm) = tptp_string_for_term format tm
|
blanchet@38198
|
439 |
|
blanchet@46172
|
440 |
fun tptp_string_for_format CNF = tptp_cnf
|
blanchet@46172
|
441 |
| tptp_string_for_format CNF_UEQ = tptp_cnf
|
blanchet@46172
|
442 |
| tptp_string_for_format FOF = tptp_fof
|
blanchet@46172
|
443 |
| tptp_string_for_format (TFF _) = tptp_tff
|
blanchet@46172
|
444 |
| tptp_string_for_format (THF _) = tptp_thf
|
blanchet@46174
|
445 |
| tptp_string_for_format (DFG _) = raise Fail "non-TPTP format"
|
blanchet@43803
|
446 |
|
blanchet@46172
|
447 |
fun tptp_string_for_problem_line format (Decl (ident, sym, ty)) =
|
blanchet@46172
|
448 |
tptp_string_for_format format ^ "(" ^ ident ^ ", type,\n " ^ sym ^
|
blanchet@46172
|
449 |
" : " ^ string_for_type format ty ^ ").\n"
|
blanchet@46172
|
450 |
| tptp_string_for_problem_line format
|
blanchet@47909
|
451 |
(Formula (ident, kind, phi, source, _)) =
|
blanchet@46172
|
452 |
tptp_string_for_format format ^ "(" ^ ident ^ ", " ^
|
blanchet@46172
|
453 |
tptp_string_for_kind kind ^ ",\n (" ^
|
blanchet@46172
|
454 |
tptp_string_for_formula format phi ^ ")" ^
|
blanchet@47909
|
455 |
(case source of
|
blanchet@47909
|
456 |
SOME tm => ", " ^ tptp_string_for_term format tm
|
blanchet@47909
|
457 |
| NONE => "") ^ ").\n"
|
blanchet@46172
|
458 |
|
blanchet@46172
|
459 |
fun tptp_lines format =
|
blanchet@37643
|
460 |
maps (fn (_, []) => []
|
blanchet@37643
|
461 |
| (heading, lines) =>
|
wenzelm@41739
|
462 |
"\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
|
blanchet@46172
|
463 |
map (tptp_string_for_problem_line format) lines)
|
blanchet@46172
|
464 |
|
blanchet@46172
|
465 |
fun arity_of_type (AFun (_, ty)) = 1 + arity_of_type ty
|
blanchet@46172
|
466 |
| arity_of_type _ = 0
|
blanchet@46172
|
467 |
|
blanchet@46172
|
468 |
fun binder_atypes (AFun (ty1, ty2)) = ty1 :: binder_atypes ty2
|
blanchet@46172
|
469 |
| binder_atypes _ = []
|
blanchet@46172
|
470 |
|
blanchet@47909
|
471 |
fun dfg_string_for_formula gen_simp flavor info =
|
blanchet@46172
|
472 |
let
|
blanchet@47207
|
473 |
fun suffix_tag top_level s =
|
blanchet@47912
|
474 |
if flavor = DFG_Sorted andalso top_level then
|
blanchet@47234
|
475 |
case extract_isabelle_status info of
|
blanchet@48019
|
476 |
SOME s' => if s' = defN then s ^ ":lt"
|
blanchet@47909
|
477 |
else if s' = simpN andalso gen_simp then s ^ ":lr"
|
blanchet@47207
|
478 |
else s
|
blanchet@47207
|
479 |
| NONE => s
|
blanchet@47207
|
480 |
else
|
blanchet@47207
|
481 |
s
|
blanchet@47207
|
482 |
fun str_for_term top_level (ATerm (s, tms)) =
|
blanchet@47207
|
483 |
(if is_tptp_equal s then "equal" |> suffix_tag top_level
|
blanchet@46172
|
484 |
else if s = tptp_true then "true"
|
blanchet@46172
|
485 |
else if s = tptp_false then "false"
|
blanchet@46172
|
486 |
else s) ^
|
blanchet@46172
|
487 |
(if null tms then ""
|
blanchet@46172
|
488 |
else "(" ^ commas (map (str_for_term false) tms) ^ ")")
|
blanchet@46172
|
489 |
| str_for_term _ _ = raise Fail "unexpected term in first-order format"
|
blanchet@46172
|
490 |
fun str_for_quant AForall = "forall"
|
blanchet@46172
|
491 |
| str_for_quant AExists = "exists"
|
blanchet@46172
|
492 |
fun str_for_conn _ ANot = "not"
|
blanchet@46172
|
493 |
| str_for_conn _ AAnd = "and"
|
blanchet@46172
|
494 |
| str_for_conn _ AOr = "or"
|
blanchet@46172
|
495 |
| str_for_conn _ AImplies = "implies"
|
blanchet@47207
|
496 |
| str_for_conn top_level AIff = "equiv" |> suffix_tag top_level
|
blanchet@47207
|
497 |
fun str_for_formula top_level (AQuant (q, xs, phi)) =
|
blanchet@46172
|
498 |
str_for_quant q ^ "(" ^ "[" ^
|
blanchet@46174
|
499 |
commas (map (string_for_bound_var (DFG flavor)) xs) ^ "], " ^
|
blanchet@47207
|
500 |
str_for_formula top_level phi ^ ")"
|
blanchet@47207
|
501 |
| str_for_formula top_level (AConn (c, phis)) =
|
blanchet@47207
|
502 |
str_for_conn top_level c ^ "(" ^
|
blanchet@46172
|
503 |
commas (map (str_for_formula false) phis) ^ ")"
|
blanchet@47207
|
504 |
| str_for_formula top_level (AAtom tm) = str_for_term top_level tm
|
blanchet@47207
|
505 |
in str_for_formula true end
|
blanchet@46172
|
506 |
|
blanchet@47909
|
507 |
fun maybe_enclose bef aft "" = "% " ^ bef ^ aft
|
blanchet@47909
|
508 |
| maybe_enclose bef aft s = bef ^ s ^ aft
|
blanchet@47909
|
509 |
|
blanchet@47909
|
510 |
fun dfg_lines flavor {is_lpo, gen_weights, gen_prec, gen_simp} ord_info
|
blanchet@47909
|
511 |
problem =
|
blanchet@46172
|
512 |
let
|
blanchet@46175
|
513 |
val sorted = (flavor = DFG_Sorted)
|
blanchet@46174
|
514 |
val format = DFG flavor
|
blanchet@47270
|
515 |
fun spair (sym, k) = "(" ^ sym ^ ", " ^ string_of_int k ^ ")"
|
blanchet@47270
|
516 |
fun ary sym = curry spair sym o arity_of_type
|
blanchet@46172
|
517 |
fun fun_typ sym ty =
|
blanchet@46174
|
518 |
"function(" ^ sym ^ ", " ^ string_for_type format ty ^ ")."
|
blanchet@46172
|
519 |
fun pred_typ sym ty =
|
blanchet@46172
|
520 |
"predicate(" ^
|
blanchet@46174
|
521 |
commas (sym :: map (string_for_type format) (binder_atypes ty)) ^ ")."
|
blanchet@46172
|
522 |
fun formula pred (Formula (ident, kind, phi, _, info)) =
|
blanchet@46172
|
523 |
if pred kind then
|
blanchet@47912
|
524 |
let
|
blanchet@47912
|
525 |
val rank =
|
blanchet@47912
|
526 |
if flavor = DFG_Sorted then extract_isabelle_rank info
|
blanchet@47912
|
527 |
else default_rank
|
blanchet@47912
|
528 |
in
|
blanchet@47909
|
529 |
"formula(" ^ dfg_string_for_formula gen_simp flavor info phi ^
|
blanchet@47909
|
530 |
", " ^ ident ^
|
blanchet@47234
|
531 |
(if rank = default_rank then "" else ", " ^ string_of_int rank) ^
|
blanchet@47234
|
532 |
")." |> SOME
|
blanchet@47234
|
533 |
end
|
blanchet@46172
|
534 |
else
|
blanchet@46172
|
535 |
NONE
|
blanchet@46172
|
536 |
| formula _ _ = NONE
|
blanchet@47241
|
537 |
fun filt f = problem |> map (map_filter f o snd) |> filter_out null
|
blanchet@46172
|
538 |
val func_aries =
|
blanchet@46172
|
539 |
filt (fn Decl (_, sym, ty) =>
|
blanchet@46172
|
540 |
if is_function_type ty then SOME (ary sym ty) else NONE
|
blanchet@46172
|
541 |
| _ => NONE)
|
blanchet@47909
|
542 |
|> flat |> commas |> maybe_enclose "functions [" "]."
|
blanchet@46172
|
543 |
val pred_aries =
|
blanchet@46172
|
544 |
filt (fn Decl (_, sym, ty) =>
|
blanchet@47219
|
545 |
if is_predicate_type ty then SOME (ary sym ty) else NONE
|
blanchet@46172
|
546 |
| _ => NONE)
|
blanchet@47909
|
547 |
|> flat |> commas |> maybe_enclose "predicates [" "]."
|
blanchet@46175
|
548 |
fun sorts () =
|
blanchet@46172
|
549 |
filt (fn Decl (_, sym, AType (s, [])) =>
|
blanchet@46172
|
550 |
if s = tptp_type_of_types then SOME sym else NONE
|
blanchet@47241
|
551 |
| _ => NONE) @ [[dfg_individual_type]]
|
blanchet@47909
|
552 |
|> flat |> commas |> maybe_enclose "sorts [" "]."
|
blanchet@47909
|
553 |
val ord_info =
|
blanchet@47909
|
554 |
if sorted andalso (gen_weights orelse gen_prec) then ord_info () else []
|
blanchet@47901
|
555 |
fun do_term_order_weights () =
|
blanchet@47909
|
556 |
(if gen_weights then ord_info else [])
|
blanchet@47909
|
557 |
|> map spair |> commas |> maybe_enclose "weights [" "]."
|
blanchet@47270
|
558 |
val syms =
|
blanchet@47933
|
559 |
[func_aries, pred_aries] @
|
blanchet@47933
|
560 |
(if sorted then [do_term_order_weights (), sorts ()] else [])
|
blanchet@46175
|
561 |
fun func_sigs () =
|
blanchet@46172
|
562 |
filt (fn Decl (_, sym, ty) =>
|
blanchet@46172
|
563 |
if is_function_type ty then SOME (fun_typ sym ty) else NONE
|
blanchet@46172
|
564 |
| _ => NONE)
|
blanchet@47241
|
565 |
|> flat
|
blanchet@46175
|
566 |
fun pred_sigs () =
|
blanchet@46172
|
567 |
filt (fn Decl (_, sym, ty) =>
|
blanchet@47219
|
568 |
if is_nontrivial_predicate_type ty then SOME (pred_typ sym ty)
|
blanchet@47219
|
569 |
else NONE
|
blanchet@46172
|
570 |
| _ => NONE)
|
blanchet@47241
|
571 |
|> flat
|
blanchet@46175
|
572 |
val decls = if sorted then func_sigs () @ pred_sigs () else []
|
blanchet@47241
|
573 |
val axioms =
|
blanchet@47241
|
574 |
filt (formula (curry (op <>) Conjecture)) |> separate [""] |> flat
|
blanchet@47241
|
575 |
val conjs =
|
blanchet@47241
|
576 |
filt (formula (curry (op =) Conjecture)) |> separate [""] |> flat
|
blanchet@47909
|
577 |
val settings =
|
blanchet@47924
|
578 |
(if is_lpo then ["set_flag(Ordering, 1)."] else []) @
|
blanchet@47909
|
579 |
(if gen_prec then
|
blanchet@47909
|
580 |
[ord_info |> map fst |> rev |> commas
|
blanchet@47909
|
581 |
|> maybe_enclose "set_precedence(" ")."]
|
blanchet@47909
|
582 |
else
|
blanchet@47909
|
583 |
[])
|
blanchet@47923
|
584 |
fun list_of _ [] = []
|
blanchet@47923
|
585 |
| list_of heading ss =
|
blanchet@47923
|
586 |
"list_of_" ^ heading ^ ".\n" :: map (suffix "\n") ss @
|
blanchet@47923
|
587 |
["end_of_list.\n\n"]
|
blanchet@46172
|
588 |
in
|
blanchet@46172
|
589 |
"\nbegin_problem(isabelle).\n\n" ::
|
blanchet@47923
|
590 |
list_of "descriptions"
|
blanchet@46172
|
591 |
["name({**}).", "author({**}).", "status(unknown).",
|
blanchet@46172
|
592 |
"description({**})."] @
|
blanchet@47923
|
593 |
list_of "symbols" syms @
|
blanchet@47923
|
594 |
list_of "declarations" decls @
|
blanchet@47923
|
595 |
list_of "formulae(axioms)" axioms @
|
blanchet@47923
|
596 |
list_of "formulae(conjectures)" conjs @
|
blanchet@47923
|
597 |
list_of "settings(SPASS)" settings @
|
blanchet@46172
|
598 |
["end_problem.\n"]
|
blanchet@46172
|
599 |
end
|
blanchet@46172
|
600 |
|
blanchet@47909
|
601 |
fun lines_for_atp_problem format ord ord_info problem =
|
blanchet@46172
|
602 |
"% This file was generated by Isabelle (most likely Sledgehammer)\n\
|
blanchet@46172
|
603 |
\% " ^ timestamp () ^ "\n" ::
|
blanchet@46174
|
604 |
(case format of
|
blanchet@47909
|
605 |
DFG flavor => dfg_lines flavor ord ord_info
|
blanchet@46174
|
606 |
| _ => tptp_lines format) problem
|
blanchet@37643
|
607 |
|
blanchet@43780
|
608 |
|
blanchet@43939
|
609 |
(** CNF (Metis) and CNF UEQ (Waldmeister) **)
|
blanchet@43780
|
610 |
|
blanchet@43780
|
611 |
fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
|
blanchet@43780
|
612 |
| is_problem_line_negated _ = false
|
blanchet@43780
|
613 |
|
blanchet@44034
|
614 |
fun is_problem_line_cnf_ueq (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) =
|
blanchet@44034
|
615 |
is_tptp_equal s
|
blanchet@43780
|
616 |
| is_problem_line_cnf_ueq _ = false
|
blanchet@43780
|
617 |
|
blanchet@43785
|
618 |
fun open_conjecture_term (ATerm ((s, s'), tms)) =
|
nik@44535
|
619 |
ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
|
nik@44535
|
620 |
else (s, s'), tms |> map open_conjecture_term)
|
nik@44535
|
621 |
| open_conjecture_term _ = raise Fail "unexpected higher-order term"
|
blanchet@43939
|
622 |
fun open_formula conj =
|
blanchet@43939
|
623 |
let
|
blanchet@44034
|
624 |
(* We are conveniently assuming that all bound variable names are
|
blanchet@44034
|
625 |
distinct, which should be the case for the formulas we generate. *)
|
blanchet@44034
|
626 |
fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
|
blanchet@44034
|
627 |
| opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
|
blanchet@43939
|
628 |
| opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
|
blanchet@43939
|
629 |
| opn pos (AConn (c, [phi1, phi2])) =
|
blanchet@43939
|
630 |
let val (pos1, pos2) = polarities_of_conn pos c in
|
blanchet@43939
|
631 |
AConn (c, [opn pos1 phi1, opn pos2 phi2])
|
blanchet@43939
|
632 |
end
|
blanchet@43939
|
633 |
| opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
|
blanchet@44283
|
634 |
| opn _ phi = phi
|
blanchet@43939
|
635 |
in opn (SOME (not conj)) end
|
blanchet@43785
|
636 |
fun open_formula_line (Formula (ident, kind, phi, source, info)) =
|
blanchet@43785
|
637 |
Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
|
blanchet@43785
|
638 |
| open_formula_line line = line
|
blanchet@43780
|
639 |
|
blanchet@43780
|
640 |
fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
|
blanchet@43783
|
641 |
Formula (ident, Hypothesis, mk_anot phi, source, info)
|
blanchet@43780
|
642 |
| negate_conjecture_line line = line
|
blanchet@43780
|
643 |
|
blanchet@43939
|
644 |
exception CLAUSIFY of unit
|
blanchet@43939
|
645 |
|
blanchet@43967
|
646 |
(* This "clausification" only expands syntactic sugar, such as "phi => psi" to
|
blanchet@43967
|
647 |
"~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
|
blanchet@43967
|
648 |
attempt to distribute conjunctions over disjunctions. *)
|
blanchet@44034
|
649 |
fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
|
blanchet@44034
|
650 |
| clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
|
blanchet@44034
|
651 |
| clausify_formula true (AConn (AOr, [phi1, phi2])) =
|
blanchet@44034
|
652 |
(phi1, phi2) |> pairself (clausify_formula true)
|
blanchet@44034
|
653 |
|> uncurry (map_product (mk_aconn AOr))
|
blanchet@44034
|
654 |
| clausify_formula false (AConn (AAnd, [phi1, phi2])) =
|
blanchet@44034
|
655 |
(phi1, phi2) |> pairself (clausify_formula false)
|
blanchet@44034
|
656 |
|> uncurry (map_product (mk_aconn AOr))
|
blanchet@44034
|
657 |
| clausify_formula true (AConn (AImplies, [phi1, phi2])) =
|
blanchet@44034
|
658 |
clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
|
blanchet@44034
|
659 |
| clausify_formula true (AConn (AIff, phis)) =
|
blanchet@44034
|
660 |
clausify_formula true (AConn (AImplies, phis)) @
|
blanchet@44034
|
661 |
clausify_formula true (AConn (AImplies, rev phis))
|
blanchet@44034
|
662 |
| clausify_formula _ _ = raise CLAUSIFY ()
|
blanchet@43939
|
663 |
|
blanchet@43939
|
664 |
fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
|
blanchet@43967
|
665 |
let
|
blanchet@43967
|
666 |
val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
|
blanchet@43967
|
667 |
in
|
blanchet@43967
|
668 |
map2 (fn phi => fn j =>
|
blanchet@44153
|
669 |
Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
|
blanchet@44153
|
670 |
info))
|
blanchet@43967
|
671 |
phis (1 upto n)
|
blanchet@43967
|
672 |
end
|
blanchet@43967
|
673 |
| clausify_formula_line _ = []
|
blanchet@43939
|
674 |
|
blanchet@43939
|
675 |
fun ensure_cnf_problem_line line =
|
blanchet@43939
|
676 |
line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
|
blanchet@43939
|
677 |
|
blanchet@43939
|
678 |
fun ensure_cnf_problem problem =
|
blanchet@43967
|
679 |
problem |> map (apsnd (maps ensure_cnf_problem_line))
|
blanchet@43933
|
680 |
|
blanchet@43803
|
681 |
fun filter_cnf_ueq_problem problem =
|
blanchet@43803
|
682 |
problem
|
blanchet@43803
|
683 |
|> map (apsnd (map open_formula_line
|
blanchet@43803
|
684 |
#> filter is_problem_line_cnf_ueq
|
blanchet@43803
|
685 |
#> map negate_conjecture_line))
|
blanchet@43803
|
686 |
|> (fn problem =>
|
blanchet@43780
|
687 |
let
|
blanchet@44688
|
688 |
val lines = problem |> maps snd
|
blanchet@44688
|
689 |
val conjs = lines |> filter is_problem_line_negated
|
blanchet@44688
|
690 |
in if length conjs = 1 andalso conjs <> lines then problem else [] end)
|
blanchet@38251
|
691 |
|
blanchet@37643
|
692 |
|
blanchet@43839
|
693 |
(** Symbol declarations **)
|
blanchet@43839
|
694 |
|
blanchet@48021
|
695 |
fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = cons sym
|
blanchet@43839
|
696 |
| add_declared_syms_in_problem_line _ = I
|
blanchet@43839
|
697 |
fun declared_syms_in_problem problem =
|
blanchet@43839
|
698 |
fold (fold add_declared_syms_in_problem_line o snd) problem []
|
blanchet@43839
|
699 |
|
blanchet@37643
|
700 |
(** Nice names **)
|
blanchet@37643
|
701 |
|
blanchet@37624
|
702 |
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
|
blanchet@37624
|
703 |
fun pool_map f xs =
|
blanchet@37624
|
704 |
pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
|
blanchet@37624
|
705 |
|
blanchet@43088
|
706 |
val no_qualifiers =
|
blanchet@43088
|
707 |
let
|
blanchet@43088
|
708 |
fun skip [] = []
|
blanchet@43088
|
709 |
| skip (#"." :: cs) = skip cs
|
blanchet@43088
|
710 |
| skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
|
blanchet@43088
|
711 |
and keep [] = []
|
blanchet@43088
|
712 |
| keep (#"." :: cs) = skip cs
|
blanchet@43088
|
713 |
| keep (c :: cs) = c :: keep cs
|
blanchet@43088
|
714 |
in String.explode #> rev #> keep #> rev #> String.implode end
|
blanchet@43088
|
715 |
|
blanchet@43626
|
716 |
(* Long names can slow down the ATPs. *)
|
blanchet@43589
|
717 |
val max_readable_name_size = 20
|
blanchet@43438
|
718 |
|
blanchet@43841
|
719 |
(* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
|
blanchet@43841
|
720 |
unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
|
blanchet@43841
|
721 |
ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
|
blanchet@43841
|
722 |
is still necessary). *)
|
blanchet@46810
|
723 |
val reserved_nice_names = [tptp_old_equal, "op", "eq"]
|
blanchet@43780
|
724 |
|
blanchet@47242
|
725 |
(* hack to get the same hashing across Mirabelle runs (see "mirabelle.pl") *)
|
blanchet@47242
|
726 |
fun cleanup_mirabelle_name s =
|
blanchet@47242
|
727 |
let
|
blanchet@47242
|
728 |
val mirabelle_infix = "_Mirabelle_"
|
blanchet@47242
|
729 |
val random_suffix_len = 10
|
blanchet@47242
|
730 |
val (s1, s2) = Substring.position mirabelle_infix (Substring.full s)
|
blanchet@47242
|
731 |
in
|
blanchet@47242
|
732 |
if Substring.isEmpty s2 then
|
blanchet@47242
|
733 |
s
|
blanchet@47242
|
734 |
else
|
blanchet@47242
|
735 |
Substring.string s1 ^
|
blanchet@47242
|
736 |
Substring.string (Substring.triml (size mirabelle_infix + random_suffix_len) s2)
|
blanchet@47242
|
737 |
end
|
blanchet@47242
|
738 |
|
blanchet@47206
|
739 |
fun readable_name protect full_name s =
|
blanchet@46810
|
740 |
(if s = full_name then
|
blanchet@46810
|
741 |
s
|
blanchet@46810
|
742 |
else
|
blanchet@46810
|
743 |
s |> no_qualifiers
|
blanchet@46810
|
744 |
|> perhaps (try (unprefix "'"))
|
blanchet@46810
|
745 |
|> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
|
blanchet@46810
|
746 |
|> (fn s =>
|
blanchet@46810
|
747 |
if size s > max_readable_name_size then
|
blanchet@46810
|
748 |
String.substring (s, 0, max_readable_name_size div 2 - 4) ^
|
blanchet@47242
|
749 |
string_of_int (hash_string (cleanup_mirabelle_name full_name)) ^
|
blanchet@46810
|
750 |
String.extract (s, size s - max_readable_name_size div 2 + 4,
|
blanchet@46810
|
751 |
NONE)
|
blanchet@46810
|
752 |
else
|
blanchet@46810
|
753 |
s)
|
blanchet@46810
|
754 |
|> (fn s =>
|
blanchet@46810
|
755 |
if member (op =) reserved_nice_names s then full_name else s))
|
blanchet@47206
|
756 |
|> protect
|
blanchet@37624
|
757 |
|
blanchet@46810
|
758 |
fun nice_name _ (full_name, _) NONE = (full_name, NONE)
|
blanchet@47206
|
759 |
| nice_name protect (full_name, desired_name) (SOME the_pool) =
|
blanchet@43839
|
760 |
if is_built_in_tptp_symbol full_name then
|
blanchet@39630
|
761 |
(full_name, SOME the_pool)
|
blanchet@39630
|
762 |
else case Symtab.lookup (fst the_pool) full_name of
|
blanchet@37624
|
763 |
SOME nice_name => (nice_name, SOME the_pool)
|
blanchet@37624
|
764 |
| NONE =>
|
blanchet@37624
|
765 |
let
|
blanchet@47206
|
766 |
val nice_prefix = readable_name protect full_name desired_name
|
blanchet@37624
|
767 |
fun add j =
|
blanchet@37624
|
768 |
let
|
blanchet@43515
|
769 |
val nice_name =
|
blanchet@47909
|
770 |
nice_prefix ^ (if j = 1 then "" else string_of_int j)
|
blanchet@37624
|
771 |
in
|
blanchet@37624
|
772 |
case Symtab.lookup (snd the_pool) nice_name of
|
blanchet@37624
|
773 |
SOME full_name' =>
|
blanchet@37624
|
774 |
if full_name = full_name' then (nice_name, the_pool)
|
blanchet@37624
|
775 |
else add (j + 1)
|
blanchet@37624
|
776 |
| NONE =>
|
blanchet@37624
|
777 |
(nice_name,
|
blanchet@37624
|
778 |
(Symtab.update_new (full_name, nice_name) (fst the_pool),
|
blanchet@37624
|
779 |
Symtab.update_new (nice_name, full_name) (snd the_pool)))
|
blanchet@37624
|
780 |
end
|
blanchet@47909
|
781 |
in add 1 |> apsnd SOME end
|
blanchet@37624
|
782 |
|
blanchet@47514
|
783 |
fun avoid_clash_with_alt_ergo_type_vars s =
|
blanchet@47514
|
784 |
if is_tptp_variable s then s else s ^ "_"
|
blanchet@47514
|
785 |
|
blanchet@47206
|
786 |
fun avoid_clash_with_dfg_keywords s =
|
blanchet@47206
|
787 |
let val n = String.size s in
|
blanchet@47271
|
788 |
if n < 2 orelse (n = 2 andalso String.sub (s, 0) = String.sub (s, 1)) orelse
|
blanchet@47271
|
789 |
String.isSubstring "_" s then
|
blanchet@47206
|
790 |
s
|
blanchet@48021
|
791 |
else if is_tptp_variable s then
|
blanchet@48017
|
792 |
(* "DL" appears to be a SPASS 3.7 keyword *)
|
blanchet@48021
|
793 |
if s = "DL" then s ^ "_" else s
|
blanchet@47206
|
794 |
else
|
blanchet@47206
|
795 |
String.substring (s, 0, n - 1) ^
|
blanchet@47206
|
796 |
String.str (Char.toUpper (String.sub (s, n - 1)))
|
blanchet@47206
|
797 |
end
|
blanchet@47206
|
798 |
|
blanchet@46810
|
799 |
fun nice_atp_problem readable_names format problem =
|
blanchet@46810
|
800 |
let
|
blanchet@46810
|
801 |
val empty_pool =
|
blanchet@46810
|
802 |
if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
|
blanchet@47514
|
803 |
val avoid_clash =
|
blanchet@47514
|
804 |
case format of
|
blanchet@47514
|
805 |
TFF (TPTP_Polymorphic, _) => avoid_clash_with_alt_ergo_type_vars
|
blanchet@47514
|
806 |
| DFG _ => avoid_clash_with_dfg_keywords
|
blanchet@47514
|
807 |
| _ => I
|
blanchet@47514
|
808 |
val nice_name = nice_name avoid_clash
|
blanchet@46810
|
809 |
fun nice_type (AType (name, tys)) =
|
blanchet@46810
|
810 |
nice_name name ##>> pool_map nice_type tys #>> AType
|
blanchet@46810
|
811 |
| nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
|
blanchet@46810
|
812 |
| nice_type (ATyAbs (names, ty)) =
|
blanchet@46810
|
813 |
pool_map nice_name names ##>> nice_type ty #>> ATyAbs
|
blanchet@46810
|
814 |
fun nice_term (ATerm (name, ts)) =
|
blanchet@46810
|
815 |
nice_name name ##>> pool_map nice_term ts #>> ATerm
|
blanchet@48926
|
816 |
| nice_term (AAbs (((name, ty), tm), args)) =
|
blanchet@48926
|
817 |
nice_name name ##>> nice_type ty ##>> nice_term tm
|
blanchet@48926
|
818 |
##>> pool_map nice_term args #>> AAbs
|
blanchet@46810
|
819 |
fun nice_formula (AQuant (q, xs, phi)) =
|
blanchet@46810
|
820 |
pool_map nice_name (map fst xs)
|
blanchet@46810
|
821 |
##>> pool_map (fn NONE => pair NONE
|
blanchet@46810
|
822 |
| SOME ty => nice_type ty #>> SOME) (map snd xs)
|
blanchet@46810
|
823 |
##>> nice_formula phi
|
blanchet@46810
|
824 |
#>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
|
blanchet@46810
|
825 |
| nice_formula (AConn (c, phis)) =
|
blanchet@46810
|
826 |
pool_map nice_formula phis #>> curry AConn c
|
blanchet@46810
|
827 |
| nice_formula (AAtom tm) = nice_term tm #>> AAtom
|
blanchet@46810
|
828 |
fun nice_problem_line (Decl (ident, sym, ty)) =
|
blanchet@46810
|
829 |
nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
|
blanchet@46810
|
830 |
| nice_problem_line (Formula (ident, kind, phi, source, info)) =
|
blanchet@46810
|
831 |
nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
|
blanchet@46810
|
832 |
fun nice_problem problem =
|
blanchet@46810
|
833 |
pool_map (fn (heading, lines) =>
|
blanchet@46810
|
834 |
pool_map nice_problem_line lines #>> pair heading) problem
|
blanchet@46810
|
835 |
in nice_problem problem empty_pool end
|
blanchet@37509
|
836 |
|
blanchet@37509
|
837 |
end;
|