blanchet@40358
|
1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
|
blanchet@38506
|
2 |
Author: Fabian Immler, TU Muenchen
|
blanchet@38506
|
3 |
Author: Makarius
|
blanchet@38506
|
4 |
Author: Jasmin Blanchette, TU Muenchen
|
blanchet@38506
|
5 |
|
blanchet@39734
|
6 |
Translation of HOL to FOL for Sledgehammer.
|
blanchet@38506
|
7 |
*)
|
blanchet@38506
|
8 |
|
blanchet@40249
|
9 |
signature SLEDGEHAMMER_ATP_TRANSLATE =
|
blanchet@38506
|
10 |
sig
|
blanchet@43088
|
11 |
type 'a fo_term = 'a ATP_Problem.fo_term
|
blanchet@38506
|
12 |
type 'a problem = 'a ATP_Problem.problem
|
blanchet@40358
|
13 |
type translated_formula
|
blanchet@38506
|
14 |
|
blanchet@41382
|
15 |
datatype type_system =
|
blanchet@41382
|
16 |
Tags of bool |
|
blanchet@43098
|
17 |
Args |
|
blanchet@43088
|
18 |
Mangled |
|
blanchet@41382
|
19 |
No_Types
|
blanchet@41382
|
20 |
|
blanchet@41386
|
21 |
val precise_overloaded_args : bool Unsynchronized.ref
|
blanchet@40445
|
22 |
val fact_prefix : string
|
blanchet@38506
|
23 |
val conjecture_prefix : string
|
blanchet@41386
|
24 |
val types_dangerous_types : type_system -> bool
|
blanchet@41384
|
25 |
val num_atp_type_args : theory -> type_system -> string -> int
|
blanchet@41336
|
26 |
val translate_atp_fact :
|
blanchet@42861
|
27 |
Proof.context -> bool -> (string * 'a) * thm
|
blanchet@41339
|
28 |
-> translated_formula option * ((string * 'a) * thm)
|
blanchet@43088
|
29 |
val unmangled_const : string -> string * string fo_term list
|
blanchet@40240
|
30 |
val prepare_atp_problem :
|
blanchet@41382
|
31 |
Proof.context -> bool -> bool -> type_system -> bool -> term list -> term
|
blanchet@41339
|
32 |
-> (translated_formula option * ((string * 'a) * thm)) list
|
blanchet@39053
|
33 |
-> string problem * string Symtab.table * int * (string * 'a) list vector
|
blanchet@41561
|
34 |
val atp_problem_weights : string problem -> (string * real) list
|
blanchet@38506
|
35 |
end;
|
blanchet@38506
|
36 |
|
blanchet@41388
|
37 |
structure Sledgehammer_ATP_Translate : SLEDGEHAMMER_ATP_TRANSLATE =
|
blanchet@38506
|
38 |
struct
|
blanchet@38506
|
39 |
|
blanchet@38506
|
40 |
open ATP_Problem
|
blanchet@39734
|
41 |
open Metis_Translate
|
blanchet@38506
|
42 |
open Sledgehammer_Util
|
blanchet@38506
|
43 |
|
blanchet@41386
|
44 |
(* FIXME: Remove references once appropriate defaults have been determined
|
blanchet@41386
|
45 |
empirically. *)
|
blanchet@41386
|
46 |
val precise_overloaded_args = Unsynchronized.ref false
|
blanchet@41386
|
47 |
|
blanchet@40445
|
48 |
val fact_prefix = "fact_"
|
blanchet@38506
|
49 |
val conjecture_prefix = "conj_"
|
blanchet@38506
|
50 |
val helper_prefix = "help_"
|
blanchet@38506
|
51 |
val class_rel_clause_prefix = "clrel_";
|
blanchet@38506
|
52 |
val arity_clause_prefix = "arity_"
|
blanchet@40156
|
53 |
val tfree_prefix = "tfree_"
|
blanchet@38506
|
54 |
|
blanchet@38506
|
55 |
(* Freshness almost guaranteed! *)
|
blanchet@38506
|
56 |
val sledgehammer_weak_prefix = "Sledgehammer:"
|
blanchet@38506
|
57 |
|
blanchet@40358
|
58 |
type translated_formula =
|
blanchet@38991
|
59 |
{name: string,
|
blanchet@38991
|
60 |
kind: kind,
|
blanchet@38991
|
61 |
combformula: (name, combterm) formula,
|
blanchet@38991
|
62 |
ctypes_sorts: typ list}
|
blanchet@38506
|
63 |
|
blanchet@41382
|
64 |
datatype type_system =
|
blanchet@41382
|
65 |
Tags of bool |
|
blanchet@43098
|
66 |
Args |
|
blanchet@43088
|
67 |
Mangled |
|
blanchet@41382
|
68 |
No_Types
|
blanchet@41382
|
69 |
|
blanchet@41386
|
70 |
fun types_dangerous_types (Tags _) = true
|
blanchet@41386
|
71 |
| types_dangerous_types _ = false
|
blanchet@41386
|
72 |
|
blanchet@41384
|
73 |
(* This is an approximation. If it returns "true" for a constant that isn't
|
blanchet@41384
|
74 |
overloaded (i.e., that has one uniform definition), needless clutter is
|
blanchet@41384
|
75 |
generated; if it returns "false" for an overloaded constant, the ATP gets a
|
blanchet@41384
|
76 |
license to do unsound reasoning if the type system is "overloaded_args". *)
|
blanchet@41384
|
77 |
fun is_overloaded thy s =
|
blanchet@42619
|
78 |
not (s = @{const_name HOL.eq}) andalso
|
blanchet@42619
|
79 |
not (s = @{const_name Metis.fequal}) andalso
|
blanchet@42619
|
80 |
(not (!precise_overloaded_args) orelse s = @{const_name finite} orelse
|
blanchet@41395
|
81 |
length (Defs.specifications_of (Theory.defs_of thy) s) > 1)
|
blanchet@41384
|
82 |
|
blanchet@41384
|
83 |
fun needs_type_args thy type_sys s =
|
blanchet@41384
|
84 |
case type_sys of
|
blanchet@41386
|
85 |
Tags full_types => not full_types andalso is_overloaded thy s
|
blanchet@43098
|
86 |
| Args => is_overloaded thy s
|
blanchet@43088
|
87 |
| Mangled => true
|
blanchet@41384
|
88 |
| No_Types => false
|
blanchet@41384
|
89 |
|
blanchet@43088
|
90 |
datatype type_arg_policy = No_Type_Args | Explicit_Type_Args | Mangled_Types
|
blanchet@43088
|
91 |
|
blanchet@43088
|
92 |
fun type_arg_policy thy type_sys s =
|
blanchet@43088
|
93 |
if needs_type_args thy type_sys s then
|
blanchet@43088
|
94 |
if type_sys = Mangled then Mangled_Types else Explicit_Type_Args
|
blanchet@43088
|
95 |
else
|
blanchet@43088
|
96 |
No_Type_Args
|
blanchet@43088
|
97 |
|
blanchet@41384
|
98 |
fun num_atp_type_args thy type_sys s =
|
blanchet@43088
|
99 |
if type_arg_policy thy type_sys s = Explicit_Type_Args then
|
blanchet@43088
|
100 |
num_type_args thy s
|
blanchet@43088
|
101 |
else
|
blanchet@43088
|
102 |
0
|
blanchet@41384
|
103 |
|
blanchet@43224
|
104 |
fun atp_type_literals_for_types type_sys kind Ts =
|
blanchet@43224
|
105 |
if type_sys = No_Types then
|
blanchet@43224
|
106 |
[]
|
blanchet@43224
|
107 |
else
|
blanchet@43224
|
108 |
Ts |> type_literals_for_types
|
blanchet@43224
|
109 |
|> filter (fn TyLitVar _ => kind <> Conjecture
|
blanchet@43224
|
110 |
| TyLitFree _ => kind = Conjecture)
|
blanchet@41385
|
111 |
|
blanchet@38506
|
112 |
fun mk_anot phi = AConn (ANot, [phi])
|
blanchet@38506
|
113 |
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
|
blanchet@38506
|
114 |
fun mk_ahorn [] phi = phi
|
blanchet@38506
|
115 |
| mk_ahorn (phi :: phis) psi =
|
blanchet@38506
|
116 |
AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi])
|
blanchet@38506
|
117 |
|
blanchet@41393
|
118 |
fun close_universally phi =
|
blanchet@41393
|
119 |
let
|
blanchet@41393
|
120 |
fun term_vars bounds (ATerm (name as (s, _), tms)) =
|
blanchet@41393
|
121 |
(is_atp_variable s andalso not (member (op =) bounds name))
|
blanchet@41393
|
122 |
? insert (op =) name
|
blanchet@41393
|
123 |
#> fold (term_vars bounds) tms
|
blanchet@41393
|
124 |
fun formula_vars bounds (AQuant (_, xs, phi)) =
|
blanchet@41393
|
125 |
formula_vars (xs @ bounds) phi
|
blanchet@41393
|
126 |
| formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
|
blanchet@41393
|
127 |
| formula_vars bounds (AAtom tm) = term_vars bounds tm
|
blanchet@41393
|
128 |
in
|
blanchet@41393
|
129 |
case formula_vars [] phi [] of [] => phi | xs => AQuant (AForall, xs, phi)
|
blanchet@41393
|
130 |
end
|
blanchet@41393
|
131 |
|
blanchet@41388
|
132 |
fun combformula_for_prop thy eq_as_iff =
|
blanchet@38506
|
133 |
let
|
blanchet@41388
|
134 |
fun do_term bs t ts =
|
blanchet@41388
|
135 |
combterm_from_term thy bs (Envir.eta_contract t)
|
blanchet@41388
|
136 |
|>> AAtom ||> union (op =) ts
|
blanchet@38506
|
137 |
fun do_quant bs q s T t' =
|
blanchet@38743
|
138 |
let val s = Name.variant (map fst bs) s in
|
blanchet@38743
|
139 |
do_formula ((s, T) :: bs) t'
|
blanchet@38743
|
140 |
#>> (fn phi => AQuant (q, [`make_bound_var s], phi))
|
blanchet@38743
|
141 |
end
|
blanchet@38506
|
142 |
and do_conn bs c t1 t2 =
|
blanchet@38506
|
143 |
do_formula bs t1 ##>> do_formula bs t2
|
blanchet@38506
|
144 |
#>> (fn (phi1, phi2) => AConn (c, [phi1, phi2]))
|
blanchet@38506
|
145 |
and do_formula bs t =
|
blanchet@38506
|
146 |
case t of
|
blanchet@38506
|
147 |
@{const Not} $ t1 =>
|
blanchet@38506
|
148 |
do_formula bs t1 #>> (fn phi => AConn (ANot, [phi]))
|
blanchet@38506
|
149 |
| Const (@{const_name All}, _) $ Abs (s, T, t') =>
|
blanchet@38506
|
150 |
do_quant bs AForall s T t'
|
blanchet@38506
|
151 |
| Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
|
blanchet@38506
|
152 |
do_quant bs AExists s T t'
|
haftmann@39028
|
153 |
| @{const HOL.conj} $ t1 $ t2 => do_conn bs AAnd t1 t2
|
haftmann@39028
|
154 |
| @{const HOL.disj} $ t1 $ t2 => do_conn bs AOr t1 t2
|
haftmann@39019
|
155 |
| @{const HOL.implies} $ t1 $ t2 => do_conn bs AImplies t1 t2
|
haftmann@39093
|
156 |
| Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
|
blanchet@41388
|
157 |
if eq_as_iff then do_conn bs AIff t1 t2 else do_term bs t
|
blanchet@41388
|
158 |
| _ => do_term bs t
|
blanchet@38506
|
159 |
in do_formula [] end
|
blanchet@38506
|
160 |
|
blanchet@38841
|
161 |
val presimplify_term = prop_of o Meson.presimplify oo Skip_Proof.make_thm
|
blanchet@38506
|
162 |
|
wenzelm@41739
|
163 |
fun concealed_bound_name j = sledgehammer_weak_prefix ^ string_of_int j
|
blanchet@38506
|
164 |
fun conceal_bounds Ts t =
|
blanchet@38506
|
165 |
subst_bounds (map (Free o apfst concealed_bound_name)
|
blanchet@38506
|
166 |
(0 upto length Ts - 1 ~~ Ts), t)
|
blanchet@38506
|
167 |
fun reveal_bounds Ts =
|
blanchet@38506
|
168 |
subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
|
blanchet@38506
|
169 |
(0 upto length Ts - 1 ~~ Ts))
|
blanchet@38506
|
170 |
|
blanchet@38831
|
171 |
(* Removes the lambdas from an equation of the form "t = (%x. u)".
|
blanchet@40071
|
172 |
(Cf. "extensionalize_theorem" in "Meson_Clausify".) *)
|
blanchet@38831
|
173 |
fun extensionalize_term t =
|
blanchet@38831
|
174 |
let
|
blanchet@38831
|
175 |
fun aux j (@{const Trueprop} $ t') = @{const Trueprop} $ aux j t'
|
blanchet@38831
|
176 |
| aux j (t as Const (s, Type (_, [Type (_, [_, T']),
|
blanchet@38831
|
177 |
Type (_, [_, res_T])]))
|
blanchet@38831
|
178 |
$ t2 $ Abs (var_s, var_T, t')) =
|
haftmann@39093
|
179 |
if s = @{const_name HOL.eq} orelse s = @{const_name "=="} then
|
blanchet@38831
|
180 |
let val var_t = Var ((var_s, j), var_T) in
|
blanchet@38831
|
181 |
Const (s, T' --> T' --> res_T)
|
blanchet@38831
|
182 |
$ betapply (t2, var_t) $ subst_bound (var_t, t')
|
blanchet@38831
|
183 |
|> aux (j + 1)
|
blanchet@38831
|
184 |
end
|
blanchet@38831
|
185 |
else
|
blanchet@38831
|
186 |
t
|
blanchet@38831
|
187 |
| aux _ t = t
|
blanchet@38831
|
188 |
in aux (maxidx_of_term t + 1) t end
|
blanchet@38831
|
189 |
|
blanchet@38506
|
190 |
fun introduce_combinators_in_term ctxt kind t =
|
blanchet@38716
|
191 |
let val thy = ProofContext.theory_of ctxt in
|
blanchet@38716
|
192 |
if Meson.is_fol_term thy t then
|
blanchet@38716
|
193 |
t
|
blanchet@38716
|
194 |
else
|
blanchet@38716
|
195 |
let
|
blanchet@38716
|
196 |
fun aux Ts t =
|
blanchet@38716
|
197 |
case t of
|
blanchet@38716
|
198 |
@{const Not} $ t1 => @{const Not} $ aux Ts t1
|
blanchet@38716
|
199 |
| (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
|
blanchet@38716
|
200 |
t0 $ Abs (s, T, aux (T :: Ts) t')
|
blanchet@38890
|
201 |
| (t0 as Const (@{const_name All}, _)) $ t1 =>
|
blanchet@38890
|
202 |
aux Ts (t0 $ eta_expand Ts t1 1)
|
blanchet@38716
|
203 |
| (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
|
blanchet@38716
|
204 |
t0 $ Abs (s, T, aux (T :: Ts) t')
|
blanchet@38890
|
205 |
| (t0 as Const (@{const_name Ex}, _)) $ t1 =>
|
blanchet@38890
|
206 |
aux Ts (t0 $ eta_expand Ts t1 1)
|
haftmann@39028
|
207 |
| (t0 as @{const HOL.conj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
|
haftmann@39028
|
208 |
| (t0 as @{const HOL.disj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
|
haftmann@39019
|
209 |
| (t0 as @{const HOL.implies}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
|
haftmann@39093
|
210 |
| (t0 as Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])))
|
blanchet@38716
|
211 |
$ t1 $ t2 =>
|
blanchet@38716
|
212 |
t0 $ aux Ts t1 $ aux Ts t2
|
blanchet@38716
|
213 |
| _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
|
blanchet@38716
|
214 |
t
|
blanchet@38716
|
215 |
else
|
blanchet@38716
|
216 |
t |> conceal_bounds Ts
|
blanchet@38716
|
217 |
|> Envir.eta_contract
|
blanchet@38716
|
218 |
|> cterm_of thy
|
blanchet@40071
|
219 |
|> Meson_Clausify.introduce_combinators_in_cterm
|
blanchet@38716
|
220 |
|> prop_of |> Logic.dest_equals |> snd
|
blanchet@38716
|
221 |
|> reveal_bounds Ts
|
blanchet@39616
|
222 |
val (t, ctxt') = Variable.import_terms true [t] ctxt |>> the_single
|
blanchet@38716
|
223 |
in t |> aux [] |> singleton (Variable.export_terms ctxt' ctxt) end
|
blanchet@38716
|
224 |
handle THM _ =>
|
blanchet@38716
|
225 |
(* A type variable of sort "{}" will make abstraction fail. *)
|
blanchet@38836
|
226 |
if kind = Conjecture then HOLogic.false_const
|
blanchet@38836
|
227 |
else HOLogic.true_const
|
blanchet@38716
|
228 |
end
|
blanchet@38506
|
229 |
|
blanchet@38506
|
230 |
(* Metis's use of "resolve_tac" freezes the schematic variables. We simulate the
|
blanchet@43224
|
231 |
same in Sledgehammer to prevent the discovery of unreplayable proofs. *)
|
blanchet@38506
|
232 |
fun freeze_term t =
|
blanchet@38506
|
233 |
let
|
blanchet@38506
|
234 |
fun aux (t $ u) = aux t $ aux u
|
blanchet@38506
|
235 |
| aux (Abs (s, T, t)) = Abs (s, T, aux t)
|
blanchet@38506
|
236 |
| aux (Var ((s, i), T)) =
|
blanchet@38506
|
237 |
Free (sledgehammer_weak_prefix ^ s ^ "_" ^ string_of_int i, T)
|
blanchet@38506
|
238 |
| aux t = t
|
blanchet@38506
|
239 |
in t |> exists_subterm is_Var t ? aux end
|
blanchet@38506
|
240 |
|
blanchet@40445
|
241 |
(* making fact and conjecture formulas *)
|
blanchet@41388
|
242 |
fun make_formula ctxt eq_as_iff presimp name kind t =
|
blanchet@38506
|
243 |
let
|
blanchet@38506
|
244 |
val thy = ProofContext.theory_of ctxt
|
blanchet@38831
|
245 |
val t = t |> Envir.beta_eta_contract
|
blanchet@38890
|
246 |
|> transform_elim_term
|
blanchet@41459
|
247 |
|> Object_Logic.atomize_term thy
|
blanchet@38890
|
248 |
val need_trueprop = (fastype_of t = HOLogic.boolT)
|
blanchet@38890
|
249 |
val t = t |> need_trueprop ? HOLogic.mk_Trueprop
|
blanchet@38506
|
250 |
|> extensionalize_term
|
blanchet@38506
|
251 |
|> presimp ? presimplify_term thy
|
blanchet@38506
|
252 |
|> perhaps (try (HOLogic.dest_Trueprop))
|
blanchet@38506
|
253 |
|> introduce_combinators_in_term ctxt kind
|
blanchet@38836
|
254 |
|> kind <> Axiom ? freeze_term
|
blanchet@41388
|
255 |
val (combformula, ctypes_sorts) = combformula_for_prop thy eq_as_iff t []
|
blanchet@38506
|
256 |
in
|
blanchet@38991
|
257 |
{name = name, combformula = combformula, kind = kind,
|
blanchet@38991
|
258 |
ctypes_sorts = ctypes_sorts}
|
blanchet@38506
|
259 |
end
|
blanchet@38506
|
260 |
|
blanchet@42861
|
261 |
fun make_fact ctxt keep_trivial eq_as_iff presimp ((name, _), th) =
|
blanchet@42861
|
262 |
case (keep_trivial,
|
blanchet@42861
|
263 |
make_formula ctxt eq_as_iff presimp name Axiom (prop_of th)) of
|
blanchet@42861
|
264 |
(false, {combformula = AAtom (CombConst (("c_True", _), _, _)), ...}) =>
|
blanchet@42861
|
265 |
NONE
|
blanchet@42861
|
266 |
| (_, formula) => SOME formula
|
blanchet@38836
|
267 |
fun make_conjecture ctxt ts =
|
blanchet@38836
|
268 |
let val last = length ts - 1 in
|
wenzelm@41739
|
269 |
map2 (fn j => make_formula ctxt true true (string_of_int j)
|
blanchet@38836
|
270 |
(if j = last then Conjecture else Hypothesis))
|
blanchet@38836
|
271 |
(0 upto last) ts
|
blanchet@38836
|
272 |
end
|
blanchet@38506
|
273 |
|
blanchet@38506
|
274 |
(** Helper facts **)
|
blanchet@38506
|
275 |
|
blanchet@41561
|
276 |
fun fold_formula f (AQuant (_, _, phi)) = fold_formula f phi
|
blanchet@41561
|
277 |
| fold_formula f (AConn (_, phis)) = fold (fold_formula f) phis
|
blanchet@41561
|
278 |
| fold_formula f (AAtom tm) = f tm
|
blanchet@41561
|
279 |
|
blanchet@41388
|
280 |
fun count_term (ATerm ((s, _), tms)) =
|
blanchet@41388
|
281 |
(if is_atp_variable s then I
|
blanchet@41388
|
282 |
else Symtab.map_entry s (Integer.add 1))
|
blanchet@41388
|
283 |
#> fold count_term tms
|
wenzelm@41654
|
284 |
fun count_formula x = fold_formula count_term x
|
blanchet@38506
|
285 |
|
blanchet@38506
|
286 |
val init_counters =
|
blanchet@41388
|
287 |
metis_helpers |> map fst |> sort_distinct string_ord |> map (rpair 0)
|
blanchet@41388
|
288 |
|> Symtab.make
|
blanchet@38506
|
289 |
|
blanchet@41393
|
290 |
fun get_helper_facts ctxt explicit_forall type_sys formulas =
|
blanchet@38506
|
291 |
let
|
blanchet@41388
|
292 |
val no_dangerous_types = types_dangerous_types type_sys
|
blanchet@41388
|
293 |
val ct = init_counters |> fold count_formula formulas
|
blanchet@41388
|
294 |
fun is_used s = the (Symtab.lookup ct s) > 0
|
blanchet@41388
|
295 |
fun dub c needs_full_types (th, j) =
|
blanchet@41388
|
296 |
((c ^ "_" ^ string_of_int j ^ (if needs_full_types then "ft" else ""),
|
blanchet@41388
|
297 |
false), th)
|
blanchet@42861
|
298 |
fun make_facts eq_as_iff = map_filter (make_fact ctxt false eq_as_iff false)
|
blanchet@38506
|
299 |
in
|
blanchet@41393
|
300 |
(metis_helpers
|
blanchet@41393
|
301 |
|> filter (is_used o fst)
|
blanchet@41393
|
302 |
|> maps (fn (c, (needs_full_types, ths)) =>
|
blanchet@41393
|
303 |
if needs_full_types andalso not no_dangerous_types then
|
blanchet@41393
|
304 |
[]
|
blanchet@41393
|
305 |
else
|
blanchet@41393
|
306 |
ths ~~ (1 upto length ths)
|
blanchet@41393
|
307 |
|> map (dub c needs_full_types)
|
blanchet@41393
|
308 |
|> make_facts (not needs_full_types)),
|
blanchet@41393
|
309 |
if type_sys = Tags false then
|
blanchet@41393
|
310 |
let
|
blanchet@41393
|
311 |
fun var s = ATerm (`I s, [])
|
blanchet@41393
|
312 |
fun tag tm = ATerm (`I type_tag_name, [var "X", tm])
|
blanchet@41393
|
313 |
in
|
blanchet@41393
|
314 |
[Fof (helper_prefix ^ ascii_of "ti_ti", Axiom,
|
blanchet@41393
|
315 |
AAtom (ATerm (`I "equal", [tag (tag (var "Y")), tag (var "Y")]))
|
blanchet@42640
|
316 |
|> explicit_forall ? close_universally, NONE)]
|
blanchet@41393
|
317 |
end
|
blanchet@41393
|
318 |
else
|
blanchet@41393
|
319 |
[])
|
blanchet@38506
|
320 |
end
|
blanchet@38506
|
321 |
|
blanchet@42861
|
322 |
fun translate_atp_fact ctxt keep_trivial =
|
blanchet@42861
|
323 |
`(make_fact ctxt keep_trivial true true)
|
blanchet@39248
|
324 |
|
blanchet@41382
|
325 |
fun translate_formulas ctxt type_sys hyp_ts concl_t rich_facts =
|
blanchet@38506
|
326 |
let
|
blanchet@38506
|
327 |
val thy = ProofContext.theory_of ctxt
|
blanchet@41339
|
328 |
val fact_ts = map (prop_of o snd o snd) rich_facts
|
blanchet@41339
|
329 |
val (facts, fact_names) =
|
blanchet@41339
|
330 |
rich_facts
|
blanchet@41339
|
331 |
|> map_filter (fn (NONE, _) => NONE
|
blanchet@41339
|
332 |
| (SOME fact, (name, _)) => SOME (fact, name))
|
blanchet@41339
|
333 |
|> ListPair.unzip
|
blanchet@40445
|
334 |
(* Remove existing facts from the conjecture, as this can dramatically
|
blanchet@39249
|
335 |
boost an ATP's performance (for some reason). *)
|
blanchet@40445
|
336 |
val hyp_ts = hyp_ts |> filter_out (member (op aconv) fact_ts)
|
blanchet@38506
|
337 |
val goal_t = Logic.list_implies (hyp_ts, concl_t)
|
blanchet@43224
|
338 |
val all_ts = goal_t :: fact_ts
|
blanchet@43224
|
339 |
val subs = tfree_classes_of_terms all_ts
|
blanchet@43224
|
340 |
val supers = tvar_classes_of_terms all_ts
|
blanchet@43224
|
341 |
val tycons = type_consts_of_terms thy all_ts
|
blanchet@41561
|
342 |
val conjs = make_conjecture ctxt (hyp_ts @ [concl_t])
|
blanchet@41385
|
343 |
val (supers', arity_clauses) =
|
blanchet@41385
|
344 |
if type_sys = No_Types then ([], [])
|
blanchet@41385
|
345 |
else make_arity_clauses thy tycons supers
|
blanchet@38506
|
346 |
val class_rel_clauses = make_class_rel_clauses thy subs supers'
|
blanchet@38506
|
347 |
in
|
blanchet@41561
|
348 |
(fact_names |> map single, (conjs, facts, class_rel_clauses, arity_clauses))
|
blanchet@38506
|
349 |
end
|
blanchet@38506
|
350 |
|
blanchet@41386
|
351 |
fun tag_with_type ty t = ATerm (`I type_tag_name, [ty, t])
|
blanchet@38506
|
352 |
|
blanchet@38506
|
353 |
fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
|
blanchet@38506
|
354 |
| fo_term_for_combtyp (CombTFree name) = ATerm (name, [])
|
blanchet@38506
|
355 |
| fo_term_for_combtyp (CombType (name, tys)) =
|
blanchet@38506
|
356 |
ATerm (name, map fo_term_for_combtyp tys)
|
blanchet@38506
|
357 |
|
blanchet@38506
|
358 |
fun fo_literal_for_type_literal (TyLitVar (class, name)) =
|
blanchet@38506
|
359 |
(true, ATerm (class, [ATerm (name, [])]))
|
blanchet@38506
|
360 |
| fo_literal_for_type_literal (TyLitFree (class, name)) =
|
blanchet@38506
|
361 |
(true, ATerm (class, [ATerm (name, [])]))
|
blanchet@38506
|
362 |
|
blanchet@38506
|
363 |
fun formula_for_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
|
blanchet@38506
|
364 |
|
blanchet@41386
|
365 |
(* Finite types such as "unit", "bool", "bool * bool", and "bool => bool" are
|
blanchet@41386
|
366 |
considered dangerous because their "exhaust" properties can easily lead to
|
blanchet@41386
|
367 |
unsound ATP proofs. The checks below are an (unsound) approximation of
|
blanchet@41386
|
368 |
finiteness. *)
|
blanchet@41386
|
369 |
|
blanchet@41386
|
370 |
fun is_dtyp_dangerous _ (Datatype_Aux.DtTFree _) = true
|
blanchet@41386
|
371 |
| is_dtyp_dangerous ctxt (Datatype_Aux.DtType (s, Us)) =
|
blanchet@41386
|
372 |
is_type_constr_dangerous ctxt s andalso forall (is_dtyp_dangerous ctxt) Us
|
blanchet@41386
|
373 |
| is_dtyp_dangerous _ (Datatype_Aux.DtRec _) = false
|
blanchet@41386
|
374 |
and is_type_dangerous ctxt (Type (s, Ts)) =
|
blanchet@41386
|
375 |
is_type_constr_dangerous ctxt s andalso forall (is_type_dangerous ctxt) Ts
|
blanchet@41388
|
376 |
| is_type_dangerous _ _ = false
|
blanchet@41386
|
377 |
and is_type_constr_dangerous ctxt s =
|
blanchet@41386
|
378 |
let val thy = ProofContext.theory_of ctxt in
|
blanchet@41386
|
379 |
case Datatype_Data.get_info thy s of
|
blanchet@41386
|
380 |
SOME {descr, ...} =>
|
blanchet@41386
|
381 |
forall (fn (_, (_, _, constrs)) =>
|
blanchet@41386
|
382 |
forall (forall (is_dtyp_dangerous ctxt) o snd) constrs) descr
|
blanchet@41386
|
383 |
| NONE =>
|
blanchet@41386
|
384 |
case Typedef.get_info ctxt s of
|
blanchet@41386
|
385 |
({rep_type, ...}, _) :: _ => is_type_dangerous ctxt rep_type
|
blanchet@41386
|
386 |
| [] => true
|
blanchet@41386
|
387 |
end
|
blanchet@41386
|
388 |
|
blanchet@41386
|
389 |
fun is_combtyp_dangerous ctxt (CombType ((s, _), tys)) =
|
blanchet@41386
|
390 |
(case strip_prefix_and_unascii type_const_prefix s of
|
blanchet@41386
|
391 |
SOME s' => forall (is_combtyp_dangerous ctxt) tys andalso
|
blanchet@41386
|
392 |
is_type_constr_dangerous ctxt (invert_const s')
|
blanchet@41386
|
393 |
| NONE => false)
|
blanchet@41386
|
394 |
| is_combtyp_dangerous _ _ = false
|
blanchet@41386
|
395 |
|
blanchet@41386
|
396 |
fun should_tag_with_type ctxt (Tags full_types) ty =
|
blanchet@41386
|
397 |
full_types orelse is_combtyp_dangerous ctxt ty
|
blanchet@41386
|
398 |
| should_tag_with_type _ _ _ = false
|
blanchet@41386
|
399 |
|
blanchet@41388
|
400 |
val fname_table =
|
blanchet@41388
|
401 |
[("c_False", (0, ("c_fFalse", @{const_name Metis.fFalse}))),
|
blanchet@41388
|
402 |
("c_True", (0, ("c_fTrue", @{const_name Metis.fTrue}))),
|
blanchet@41388
|
403 |
("c_Not", (1, ("c_fNot", @{const_name Metis.fNot}))),
|
blanchet@41388
|
404 |
("c_conj", (2, ("c_fconj", @{const_name Metis.fconj}))),
|
blanchet@41388
|
405 |
("c_disj", (2, ("c_fdisj", @{const_name Metis.fdisj}))),
|
blanchet@41388
|
406 |
("c_implies", (2, ("c_fimplies", @{const_name Metis.fimplies}))),
|
blanchet@41388
|
407 |
("equal", (2, ("c_fequal", @{const_name Metis.fequal})))]
|
blanchet@41388
|
408 |
|
blanchet@43088
|
409 |
(* We are crossing our fingers that it doesn't clash with anything else. *)
|
blanchet@43088
|
410 |
val mangled_type_sep = "\000"
|
blanchet@43088
|
411 |
|
blanchet@43088
|
412 |
fun mangled_combtyp f (CombTFree name) = f name
|
blanchet@43097
|
413 |
| mangled_combtyp f (CombTVar name) =
|
blanchet@43097
|
414 |
f name (* FIXME: shouldn't happen *)
|
blanchet@43097
|
415 |
(* raise Fail "impossible schematic type variable" *)
|
blanchet@43088
|
416 |
| mangled_combtyp f (CombType (name, tys)) =
|
blanchet@43088
|
417 |
"(" ^ commas (map (mangled_combtyp f) tys) ^ ")" ^ f name
|
blanchet@43088
|
418 |
|
blanchet@43088
|
419 |
fun mangled_type_suffix f g tys =
|
blanchet@43088
|
420 |
fold_rev (curry (op ^) o g o prefix mangled_type_sep o mangled_combtyp f)
|
blanchet@43088
|
421 |
tys ""
|
blanchet@43088
|
422 |
|
blanchet@43088
|
423 |
val parse_mangled_ident =
|
blanchet@43088
|
424 |
Scan.many1 (not o member (op =) ["(", ")", ","]) >> implode
|
blanchet@43088
|
425 |
|
blanchet@43088
|
426 |
fun parse_mangled_type x =
|
blanchet@43088
|
427 |
($$ "(" |-- Scan.optional parse_mangled_types [] --| $$ ")"
|
blanchet@43088
|
428 |
-- parse_mangled_ident >> (ATerm o swap)
|
blanchet@43088
|
429 |
|| parse_mangled_ident >> (ATerm o rpair [])) x
|
blanchet@43088
|
430 |
and parse_mangled_types x =
|
blanchet@43088
|
431 |
(parse_mangled_type ::: Scan.repeat ($$ "," |-- parse_mangled_type)) x
|
blanchet@43088
|
432 |
|
blanchet@43088
|
433 |
fun unmangled_type s =
|
blanchet@43088
|
434 |
s |> suffix ")" |> raw_explode
|
blanchet@43088
|
435 |
|> Scan.finite Symbol.stopper
|
blanchet@43088
|
436 |
(Scan.error (!! (fn _ => raise Fail ("unrecognized mangled type " ^
|
blanchet@43088
|
437 |
quote s)) parse_mangled_type))
|
blanchet@43088
|
438 |
|> fst
|
blanchet@43088
|
439 |
|
blanchet@43088
|
440 |
fun unmangled_const s =
|
blanchet@43088
|
441 |
let val ss = space_explode mangled_type_sep s in
|
blanchet@43088
|
442 |
(hd ss, map unmangled_type (tl ss))
|
blanchet@43088
|
443 |
end
|
blanchet@43088
|
444 |
|
blanchet@41386
|
445 |
fun fo_term_for_combterm ctxt type_sys =
|
blanchet@38506
|
446 |
let
|
blanchet@41386
|
447 |
val thy = ProofContext.theory_of ctxt
|
blanchet@38506
|
448 |
fun aux top_level u =
|
blanchet@38506
|
449 |
let
|
blanchet@38506
|
450 |
val (head, args) = strip_combterm_comb u
|
blanchet@38506
|
451 |
val (x, ty_args) =
|
blanchet@38506
|
452 |
case head of
|
blanchet@38506
|
453 |
CombConst (name as (s, s'), _, ty_args) =>
|
blanchet@41388
|
454 |
(case AList.lookup (op =) fname_table s of
|
blanchet@41388
|
455 |
SOME (n, fname) =>
|
blanchet@41398
|
456 |
(if top_level andalso length args = n then
|
blanchet@41398
|
457 |
case s of
|
blanchet@41398
|
458 |
"c_False" => ("$false", s')
|
blanchet@41398
|
459 |
| "c_True" => ("$true", s')
|
blanchet@41398
|
460 |
| _ => name
|
blanchet@41398
|
461 |
else
|
blanchet@41398
|
462 |
fname, [])
|
blanchet@41388
|
463 |
| NONE =>
|
blanchet@41388
|
464 |
case strip_prefix_and_unascii const_prefix s of
|
blanchet@41388
|
465 |
NONE => (name, ty_args)
|
blanchet@41388
|
466 |
| SOME s'' =>
|
blanchet@43088
|
467 |
let val s'' = invert_const s'' in
|
blanchet@43088
|
468 |
case type_arg_policy thy type_sys s'' of
|
blanchet@43088
|
469 |
No_Type_Args => (name, [])
|
blanchet@43088
|
470 |
| Explicit_Type_Args => (name, ty_args)
|
blanchet@43088
|
471 |
| Mangled_Types =>
|
blanchet@43088
|
472 |
((s ^ mangled_type_suffix fst ascii_of ty_args,
|
blanchet@43088
|
473 |
s' ^ mangled_type_suffix snd I ty_args), [])
|
blanchet@43088
|
474 |
end)
|
blanchet@38506
|
475 |
| CombVar (name, _) => (name, [])
|
blanchet@38506
|
476 |
| CombApp _ => raise Fail "impossible \"CombApp\""
|
blanchet@41386
|
477 |
val t =
|
blanchet@41386
|
478 |
ATerm (x, map fo_term_for_combtyp ty_args @ map (aux false) args)
|
blanchet@41386
|
479 |
val ty = combtyp_of u
|
blanchet@38506
|
480 |
in
|
blanchet@41386
|
481 |
t |> (if should_tag_with_type ctxt type_sys ty then
|
blanchet@41386
|
482 |
tag_with_type (fo_term_for_combtyp ty)
|
blanchet@41382
|
483 |
else
|
blanchet@41382
|
484 |
I)
|
blanchet@38506
|
485 |
end
|
blanchet@38506
|
486 |
in aux true end
|
blanchet@38506
|
487 |
|
blanchet@41386
|
488 |
fun formula_for_combformula ctxt type_sys =
|
blanchet@38506
|
489 |
let
|
blanchet@38506
|
490 |
fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
|
blanchet@38506
|
491 |
| aux (AConn (c, phis)) = AConn (c, map aux phis)
|
blanchet@41386
|
492 |
| aux (AAtom tm) = AAtom (fo_term_for_combterm ctxt type_sys tm)
|
blanchet@38506
|
493 |
in aux end
|
blanchet@38506
|
494 |
|
blanchet@41386
|
495 |
fun formula_for_fact ctxt type_sys
|
blanchet@40445
|
496 |
({combformula, ctypes_sorts, ...} : translated_formula) =
|
blanchet@38506
|
497 |
mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
|
blanchet@43224
|
498 |
(atp_type_literals_for_types type_sys Axiom ctypes_sorts))
|
blanchet@41386
|
499 |
(formula_for_combformula ctxt type_sys combformula)
|
blanchet@38506
|
500 |
|
blanchet@43051
|
501 |
(* Each fact is given a unique fact number to avoid name clashes (e.g., because
|
blanchet@43051
|
502 |
of monomorphization). The TPTP explicitly forbids name clashes, and some of
|
blanchet@43051
|
503 |
the remote provers might care. *)
|
blanchet@43051
|
504 |
fun problem_line_for_fact ctxt prefix type_sys
|
blanchet@43051
|
505 |
(j, formula as {name, kind, ...}) =
|
blanchet@43051
|
506 |
Fof (prefix ^ string_of_int j ^ "_" ^ ascii_of name, kind,
|
blanchet@43051
|
507 |
formula_for_fact ctxt type_sys formula, NONE)
|
blanchet@38506
|
508 |
|
blanchet@38506
|
509 |
fun problem_line_for_class_rel_clause (ClassRelClause {name, subclass,
|
blanchet@38506
|
510 |
superclass, ...}) =
|
blanchet@38506
|
511 |
let val ty_arg = ATerm (("T", "T"), []) in
|
blanchet@38506
|
512 |
Fof (class_rel_clause_prefix ^ ascii_of name, Axiom,
|
blanchet@38506
|
513 |
AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
|
blanchet@42640
|
514 |
AAtom (ATerm (superclass, [ty_arg]))]), NONE)
|
blanchet@38506
|
515 |
end
|
blanchet@38506
|
516 |
|
blanchet@38506
|
517 |
fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
|
blanchet@38506
|
518 |
(true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
|
blanchet@38506
|
519 |
| fo_literal_for_arity_literal (TVarLit (c, sort)) =
|
blanchet@38506
|
520 |
(false, ATerm (c, [ATerm (sort, [])]))
|
blanchet@38506
|
521 |
|
blanchet@38506
|
522 |
fun problem_line_for_arity_clause (ArityClause {name, conclLit, premLits,
|
blanchet@38506
|
523 |
...}) =
|
blanchet@38506
|
524 |
Fof (arity_clause_prefix ^ ascii_of name, Axiom,
|
blanchet@38506
|
525 |
mk_ahorn (map (formula_for_fo_literal o apfst not
|
blanchet@38506
|
526 |
o fo_literal_for_arity_literal) premLits)
|
blanchet@38506
|
527 |
(formula_for_fo_literal
|
blanchet@42640
|
528 |
(fo_literal_for_arity_literal conclLit)), NONE)
|
blanchet@38506
|
529 |
|
blanchet@41386
|
530 |
fun problem_line_for_conjecture ctxt type_sys
|
blanchet@40358
|
531 |
({name, kind, combformula, ...} : translated_formula) =
|
blanchet@38506
|
532 |
Fof (conjecture_prefix ^ name, kind,
|
blanchet@42640
|
533 |
formula_for_combformula ctxt type_sys combformula, NONE)
|
blanchet@38506
|
534 |
|
blanchet@43224
|
535 |
fun free_type_literals type_sys ({ctypes_sorts, ...} : translated_formula) =
|
blanchet@43224
|
536 |
ctypes_sorts |> atp_type_literals_for_types type_sys Conjecture
|
blanchet@41385
|
537 |
|> map fo_literal_for_type_literal
|
blanchet@38506
|
538 |
|
blanchet@40156
|
539 |
fun problem_line_for_free_type j lit =
|
blanchet@42640
|
540 |
Fof (tfree_prefix ^ string_of_int j, Hypothesis, formula_for_fo_literal lit,
|
blanchet@42640
|
541 |
NONE)
|
blanchet@43224
|
542 |
fun problem_lines_for_free_types type_sys facts =
|
blanchet@38506
|
543 |
let
|
blanchet@43224
|
544 |
val litss = map (free_type_literals type_sys) facts
|
blanchet@38506
|
545 |
val lits = fold (union (op =)) litss []
|
blanchet@40156
|
546 |
in map2 problem_line_for_free_type (0 upto length lits - 1) lits end
|
blanchet@38506
|
547 |
|
blanchet@38506
|
548 |
(** "hBOOL" and "hAPP" **)
|
blanchet@38506
|
549 |
|
blanchet@38506
|
550 |
type const_info = {min_arity: int, max_arity: int, sub_level: bool}
|
blanchet@38506
|
551 |
|
blanchet@38506
|
552 |
fun consider_term top_level (ATerm ((s, _), ts)) =
|
blanchet@39692
|
553 |
(if is_atp_variable s then
|
blanchet@38506
|
554 |
I
|
blanchet@38506
|
555 |
else
|
blanchet@38506
|
556 |
let val n = length ts in
|
blanchet@38506
|
557 |
Symtab.map_default
|
blanchet@38506
|
558 |
(s, {min_arity = n, max_arity = 0, sub_level = false})
|
blanchet@38506
|
559 |
(fn {min_arity, max_arity, sub_level} =>
|
blanchet@38506
|
560 |
{min_arity = Int.min (n, min_arity),
|
blanchet@38506
|
561 |
max_arity = Int.max (n, max_arity),
|
blanchet@38506
|
562 |
sub_level = sub_level orelse not top_level})
|
blanchet@38506
|
563 |
end)
|
blanchet@41386
|
564 |
#> fold (consider_term (top_level andalso s = type_tag_name)) ts
|
blanchet@38506
|
565 |
fun consider_formula (AQuant (_, _, phi)) = consider_formula phi
|
blanchet@38506
|
566 |
| consider_formula (AConn (_, phis)) = fold consider_formula phis
|
blanchet@38506
|
567 |
| consider_formula (AAtom tm) = consider_term true tm
|
blanchet@38506
|
568 |
|
blanchet@42640
|
569 |
fun consider_problem_line (Fof (_, _, phi, _)) = consider_formula phi
|
blanchet@38506
|
570 |
fun consider_problem problem = fold (fold consider_problem_line o snd) problem
|
blanchet@38506
|
571 |
|
blanchet@41388
|
572 |
(* needed for helper facts if the problem otherwise does not involve equality *)
|
blanchet@41388
|
573 |
val equal_entry = ("equal", {min_arity = 2, max_arity = 2, sub_level = false})
|
blanchet@41388
|
574 |
|
blanchet@38506
|
575 |
fun const_table_for_problem explicit_apply problem =
|
blanchet@41388
|
576 |
if explicit_apply then
|
blanchet@41388
|
577 |
NONE
|
blanchet@41388
|
578 |
else
|
blanchet@41395
|
579 |
SOME (Symtab.empty |> Symtab.default equal_entry |> consider_problem problem)
|
blanchet@38506
|
580 |
|
blanchet@41382
|
581 |
fun min_arity_of thy type_sys NONE s =
|
blanchet@41386
|
582 |
(if s = "equal" orelse s = type_tag_name orelse
|
blanchet@38506
|
583 |
String.isPrefix type_const_prefix s orelse
|
blanchet@38506
|
584 |
String.isPrefix class_prefix s then
|
blanchet@38506
|
585 |
16383 (* large number *)
|
blanchet@38987
|
586 |
else case strip_prefix_and_unascii const_prefix s of
|
blanchet@43088
|
587 |
SOME s' =>
|
blanchet@43088
|
588 |
s' |> unmangled_const |> fst |> invert_const
|
blanchet@43088
|
589 |
|> num_atp_type_args thy type_sys
|
blanchet@38506
|
590 |
| NONE => 0)
|
blanchet@38506
|
591 |
| min_arity_of _ _ (SOME the_const_tab) s =
|
blanchet@38506
|
592 |
case Symtab.lookup the_const_tab s of
|
blanchet@38506
|
593 |
SOME ({min_arity, ...} : const_info) => min_arity
|
blanchet@38506
|
594 |
| NONE => 0
|
blanchet@38506
|
595 |
|
blanchet@38506
|
596 |
fun full_type_of (ATerm ((s, _), [ty, _])) =
|
blanchet@41386
|
597 |
if s = type_tag_name then SOME ty else NONE
|
blanchet@41386
|
598 |
| full_type_of _ = NONE
|
blanchet@38506
|
599 |
|
blanchet@38506
|
600 |
fun list_hAPP_rev _ t1 [] = t1
|
blanchet@38506
|
601 |
| list_hAPP_rev NONE t1 (t2 :: ts2) =
|
blanchet@38506
|
602 |
ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2])
|
blanchet@38506
|
603 |
| list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
|
blanchet@41386
|
604 |
case full_type_of t2 of
|
blanchet@41386
|
605 |
SOME ty2 =>
|
blanchet@41386
|
606 |
let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
|
blanchet@41386
|
607 |
[ty2, ty]) in
|
blanchet@41386
|
608 |
ATerm (`I "hAPP",
|
blanchet@41386
|
609 |
[tag_with_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
|
blanchet@41386
|
610 |
end
|
blanchet@41386
|
611 |
| NONE => list_hAPP_rev NONE t1 (t2 :: ts2)
|
blanchet@38506
|
612 |
|
blanchet@41382
|
613 |
fun repair_applications_in_term thy type_sys const_tab =
|
blanchet@38506
|
614 |
let
|
blanchet@38506
|
615 |
fun aux opt_ty (ATerm (name as (s, _), ts)) =
|
blanchet@41386
|
616 |
if s = type_tag_name then
|
blanchet@38506
|
617 |
case ts of
|
blanchet@38506
|
618 |
[t1, t2] => ATerm (name, [aux NONE t1, aux (SOME t1) t2])
|
blanchet@41386
|
619 |
| _ => raise Fail "malformed type tag"
|
blanchet@38506
|
620 |
else
|
blanchet@38506
|
621 |
let
|
blanchet@38506
|
622 |
val ts = map (aux NONE) ts
|
blanchet@41382
|
623 |
val (ts1, ts2) = chop (min_arity_of thy type_sys const_tab s) ts
|
blanchet@38506
|
624 |
in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
|
blanchet@38506
|
625 |
in aux NONE end
|
blanchet@38506
|
626 |
|
blanchet@38506
|
627 |
fun boolify t = ATerm (`I "hBOOL", [t])
|
blanchet@38506
|
628 |
|
blanchet@38506
|
629 |
(* True if the constant ever appears outside of the top-level position in
|
blanchet@38506
|
630 |
literals, or if it appears with different arities (e.g., because of different
|
blanchet@38506
|
631 |
type instantiations). If false, the constant always receives all of its
|
blanchet@38506
|
632 |
arguments and is used as a predicate. *)
|
blanchet@38506
|
633 |
fun is_predicate NONE s =
|
blanchet@38812
|
634 |
s = "equal" orelse s = "$false" orelse s = "$true" orelse
|
blanchet@38812
|
635 |
String.isPrefix type_const_prefix s orelse String.isPrefix class_prefix s
|
blanchet@38506
|
636 |
| is_predicate (SOME the_const_tab) s =
|
blanchet@38506
|
637 |
case Symtab.lookup the_const_tab s of
|
blanchet@38506
|
638 |
SOME {min_arity, max_arity, sub_level} =>
|
blanchet@38506
|
639 |
not sub_level andalso min_arity = max_arity
|
blanchet@38506
|
640 |
| NONE => false
|
blanchet@38506
|
641 |
|
blanchet@41388
|
642 |
fun repair_predicates_in_term pred_const_tab (t as ATerm ((s, _), ts)) =
|
blanchet@41386
|
643 |
if s = type_tag_name then
|
blanchet@38506
|
644 |
case ts of
|
blanchet@38506
|
645 |
[_, t' as ATerm ((s', _), _)] =>
|
blanchet@41388
|
646 |
if is_predicate pred_const_tab s' then t' else boolify t
|
blanchet@41386
|
647 |
| _ => raise Fail "malformed type tag"
|
blanchet@38506
|
648 |
else
|
blanchet@41388
|
649 |
t |> not (is_predicate pred_const_tab s) ? boolify
|
blanchet@38506
|
650 |
|
blanchet@41382
|
651 |
fun repair_formula thy explicit_forall type_sys const_tab =
|
blanchet@38506
|
652 |
let
|
blanchet@41388
|
653 |
val pred_const_tab = case type_sys of Tags _ => NONE | _ => const_tab
|
blanchet@38506
|
654 |
fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
|
blanchet@38506
|
655 |
| aux (AConn (c, phis)) = AConn (c, map aux phis)
|
blanchet@38506
|
656 |
| aux (AAtom tm) =
|
blanchet@41382
|
657 |
AAtom (tm |> repair_applications_in_term thy type_sys const_tab
|
blanchet@41388
|
658 |
|> repair_predicates_in_term pred_const_tab)
|
blanchet@38506
|
659 |
in aux #> explicit_forall ? close_universally end
|
blanchet@38506
|
660 |
|
blanchet@41382
|
661 |
fun repair_problem_line thy explicit_forall type_sys const_tab
|
blanchet@42640
|
662 |
(Fof (ident, kind, phi, source)) =
|
blanchet@42640
|
663 |
Fof (ident, kind, repair_formula thy explicit_forall type_sys const_tab phi,
|
blanchet@42640
|
664 |
source)
|
blanchet@41388
|
665 |
fun repair_problem thy = map o apsnd o map ooo repair_problem_line thy
|
blanchet@38506
|
666 |
|
blanchet@41388
|
667 |
fun dest_Fof (Fof z) = z
|
blanchet@38506
|
668 |
|
blanchet@41405
|
669 |
val factsN = "Relevant facts"
|
blanchet@41405
|
670 |
val class_relsN = "Class relationships"
|
blanchet@41405
|
671 |
val aritiesN = "Arity declarations"
|
blanchet@41405
|
672 |
val helpersN = "Helper facts"
|
blanchet@41405
|
673 |
val conjsN = "Conjectures"
|
blanchet@41561
|
674 |
val free_typesN = "Type variables"
|
blanchet@41405
|
675 |
|
blanchet@41405
|
676 |
fun offset_of_heading_in_problem _ [] j = j
|
blanchet@41405
|
677 |
| offset_of_heading_in_problem needle ((heading, lines) :: problem) j =
|
blanchet@41405
|
678 |
if heading = needle then j
|
blanchet@41405
|
679 |
else offset_of_heading_in_problem needle problem (j + length lines)
|
blanchet@41405
|
680 |
|
blanchet@41382
|
681 |
fun prepare_atp_problem ctxt readable_names explicit_forall type_sys
|
blanchet@40445
|
682 |
explicit_apply hyp_ts concl_t facts =
|
blanchet@38506
|
683 |
let
|
blanchet@38506
|
684 |
val thy = ProofContext.theory_of ctxt
|
blanchet@41561
|
685 |
val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
|
blanchet@41382
|
686 |
translate_formulas ctxt type_sys hyp_ts concl_t facts
|
blanchet@38506
|
687 |
(* Reordering these might or might not confuse the proof reconstruction
|
blanchet@38506
|
688 |
code or the SPASS Flotter hack. *)
|
blanchet@38506
|
689 |
val problem =
|
blanchet@43051
|
690 |
[(factsN, map (problem_line_for_fact ctxt fact_prefix type_sys)
|
blanchet@43051
|
691 |
(0 upto length facts - 1 ~~ facts)),
|
blanchet@41405
|
692 |
(class_relsN, map problem_line_for_class_rel_clause class_rel_clauses),
|
blanchet@41405
|
693 |
(aritiesN, map problem_line_for_arity_clause arity_clauses),
|
blanchet@41405
|
694 |
(helpersN, []),
|
blanchet@41561
|
695 |
(conjsN, map (problem_line_for_conjecture ctxt type_sys) conjs),
|
blanchet@43224
|
696 |
(free_typesN, problem_lines_for_free_types type_sys (facts @ conjs))]
|
blanchet@41388
|
697 |
val const_tab = const_table_for_problem explicit_apply problem
|
blanchet@41388
|
698 |
val problem =
|
blanchet@41388
|
699 |
problem |> repair_problem thy explicit_forall type_sys const_tab
|
blanchet@41405
|
700 |
val helper_lines =
|
blanchet@41393
|
701 |
get_helper_facts ctxt explicit_forall type_sys
|
blanchet@41393
|
702 |
(maps (map (#3 o dest_Fof) o snd) problem)
|
blanchet@43051
|
703 |
|>> map (pair 0
|
blanchet@43051
|
704 |
#> problem_line_for_fact ctxt helper_prefix type_sys
|
blanchet@41405
|
705 |
#> repair_problem_line thy explicit_forall type_sys const_tab)
|
blanchet@41405
|
706 |
|> op @
|
blanchet@41388
|
707 |
val (problem, pool) =
|
blanchet@41405
|
708 |
problem |> AList.update (op =) (helpersN, helper_lines)
|
blanchet@41388
|
709 |
|> nice_atp_problem readable_names
|
blanchet@38506
|
710 |
in
|
blanchet@38506
|
711 |
(problem,
|
blanchet@38506
|
712 |
case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
|
blanchet@41405
|
713 |
offset_of_heading_in_problem conjsN problem 0,
|
blanchet@41405
|
714 |
fact_names |> Vector.fromList)
|
blanchet@38506
|
715 |
end
|
blanchet@38506
|
716 |
|
blanchet@41561
|
717 |
(* FUDGE *)
|
blanchet@41561
|
718 |
val conj_weight = 0.0
|
blanchet@42641
|
719 |
val hyp_weight = 0.1
|
blanchet@42641
|
720 |
val fact_min_weight = 0.2
|
blanchet@41561
|
721 |
val fact_max_weight = 1.0
|
blanchet@41561
|
722 |
|
blanchet@41561
|
723 |
fun add_term_weights weight (ATerm (s, tms)) =
|
blanchet@41561
|
724 |
(not (is_atp_variable s) andalso s <> "equal") ? Symtab.default (s, weight)
|
blanchet@41561
|
725 |
#> fold (add_term_weights weight) tms
|
blanchet@42640
|
726 |
fun add_problem_line_weights weight (Fof (_, _, phi, _)) =
|
blanchet@41623
|
727 |
fold_formula (add_term_weights weight) phi
|
blanchet@41561
|
728 |
|
blanchet@41561
|
729 |
fun add_conjectures_weights [] = I
|
blanchet@41561
|
730 |
| add_conjectures_weights conjs =
|
blanchet@41561
|
731 |
let val (hyps, conj) = split_last conjs in
|
blanchet@41561
|
732 |
add_problem_line_weights conj_weight conj
|
blanchet@41561
|
733 |
#> fold (add_problem_line_weights hyp_weight) hyps
|
blanchet@41561
|
734 |
end
|
blanchet@41561
|
735 |
|
blanchet@41561
|
736 |
fun add_facts_weights facts =
|
blanchet@41561
|
737 |
let
|
blanchet@41561
|
738 |
val num_facts = length facts
|
blanchet@41561
|
739 |
fun weight_of j =
|
blanchet@41561
|
740 |
fact_min_weight + (fact_max_weight - fact_min_weight) * Real.fromInt j
|
blanchet@41561
|
741 |
/ Real.fromInt num_facts
|
blanchet@41561
|
742 |
in
|
blanchet@41561
|
743 |
map weight_of (0 upto num_facts - 1) ~~ facts
|
blanchet@41561
|
744 |
|> fold (uncurry add_problem_line_weights)
|
blanchet@41561
|
745 |
end
|
blanchet@41561
|
746 |
|
blanchet@41561
|
747 |
(* Weights are from 0.0 (most important) to 1.0 (least important). *)
|
blanchet@41561
|
748 |
fun atp_problem_weights problem =
|
blanchet@41561
|
749 |
Symtab.empty
|
blanchet@41561
|
750 |
|> add_conjectures_weights (these (AList.lookup (op =) problem conjsN))
|
blanchet@41561
|
751 |
|> add_facts_weights (these (AList.lookup (op =) problem factsN))
|
blanchet@41561
|
752 |
|> Symtab.dest
|
blanchet@42590
|
753 |
|> sort (prod_ord Real.compare string_ord o pairself swap)
|
blanchet@41561
|
754 |
|
blanchet@38506
|
755 |
end;
|