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@38506
|
11 |
type 'a problem = 'a ATP_Problem.problem
|
blanchet@40358
|
12 |
type translated_formula
|
blanchet@38506
|
13 |
|
blanchet@38506
|
14 |
val axiom_prefix : string
|
blanchet@38506
|
15 |
val conjecture_prefix : string
|
blanchet@40358
|
16 |
val translate_axiom :
|
blanchet@39249
|
17 |
Proof.context -> (string * 'a) * thm
|
blanchet@40358
|
18 |
-> term * ((string * 'a) * translated_formula) option
|
blanchet@40240
|
19 |
val prepare_atp_problem :
|
blanchet@38506
|
20 |
Proof.context -> bool -> bool -> bool -> bool -> term list -> term
|
blanchet@40358
|
21 |
-> (term * ((string * 'a) * translated_formula) option) list
|
blanchet@39053
|
22 |
-> string problem * string Symtab.table * int * (string * 'a) list vector
|
blanchet@38506
|
23 |
end;
|
blanchet@38506
|
24 |
|
blanchet@40249
|
25 |
structure Sledgehammer_ATP_Translate : SLEDGEHAMMER_ATP_TRANSLATE =
|
blanchet@38506
|
26 |
struct
|
blanchet@38506
|
27 |
|
blanchet@38506
|
28 |
open ATP_Problem
|
blanchet@39734
|
29 |
open Metis_Translate
|
blanchet@38506
|
30 |
open Sledgehammer_Util
|
blanchet@38506
|
31 |
|
blanchet@38506
|
32 |
val axiom_prefix = "ax_"
|
blanchet@38506
|
33 |
val conjecture_prefix = "conj_"
|
blanchet@38506
|
34 |
val helper_prefix = "help_"
|
blanchet@38506
|
35 |
val class_rel_clause_prefix = "clrel_";
|
blanchet@38506
|
36 |
val arity_clause_prefix = "arity_"
|
blanchet@40156
|
37 |
val tfree_prefix = "tfree_"
|
blanchet@38506
|
38 |
|
blanchet@38506
|
39 |
(* Freshness almost guaranteed! *)
|
blanchet@38506
|
40 |
val sledgehammer_weak_prefix = "Sledgehammer:"
|
blanchet@38506
|
41 |
|
blanchet@40358
|
42 |
type translated_formula =
|
blanchet@38991
|
43 |
{name: string,
|
blanchet@38991
|
44 |
kind: kind,
|
blanchet@38991
|
45 |
combformula: (name, combterm) formula,
|
blanchet@38991
|
46 |
ctypes_sorts: typ list}
|
blanchet@38506
|
47 |
|
blanchet@38506
|
48 |
fun mk_anot phi = AConn (ANot, [phi])
|
blanchet@38506
|
49 |
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
|
blanchet@38506
|
50 |
fun mk_ahorn [] phi = phi
|
blanchet@38506
|
51 |
| mk_ahorn (phi :: phis) psi =
|
blanchet@38506
|
52 |
AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi])
|
blanchet@38506
|
53 |
|
blanchet@38506
|
54 |
fun combformula_for_prop thy =
|
blanchet@38506
|
55 |
let
|
blanchet@40067
|
56 |
val do_term = combterm_from_term thy ~1
|
blanchet@38506
|
57 |
fun do_quant bs q s T t' =
|
blanchet@38743
|
58 |
let val s = Name.variant (map fst bs) s in
|
blanchet@38743
|
59 |
do_formula ((s, T) :: bs) t'
|
blanchet@38743
|
60 |
#>> (fn phi => AQuant (q, [`make_bound_var s], phi))
|
blanchet@38743
|
61 |
end
|
blanchet@38506
|
62 |
and do_conn bs c t1 t2 =
|
blanchet@38506
|
63 |
do_formula bs t1 ##>> do_formula bs t2
|
blanchet@38506
|
64 |
#>> (fn (phi1, phi2) => AConn (c, [phi1, phi2]))
|
blanchet@38506
|
65 |
and do_formula bs t =
|
blanchet@38506
|
66 |
case t of
|
blanchet@38506
|
67 |
@{const Not} $ t1 =>
|
blanchet@38506
|
68 |
do_formula bs t1 #>> (fn phi => AConn (ANot, [phi]))
|
blanchet@38506
|
69 |
| Const (@{const_name All}, _) $ Abs (s, T, t') =>
|
blanchet@38506
|
70 |
do_quant bs AForall s T t'
|
blanchet@38506
|
71 |
| Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
|
blanchet@38506
|
72 |
do_quant bs AExists s T t'
|
haftmann@39028
|
73 |
| @{const HOL.conj} $ t1 $ t2 => do_conn bs AAnd t1 t2
|
haftmann@39028
|
74 |
| @{const HOL.disj} $ t1 $ t2 => do_conn bs AOr t1 t2
|
haftmann@39019
|
75 |
| @{const HOL.implies} $ t1 $ t2 => do_conn bs AImplies t1 t2
|
haftmann@39093
|
76 |
| Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
|
blanchet@38506
|
77 |
do_conn bs AIff t1 t2
|
blanchet@38506
|
78 |
| _ => (fn ts => do_term bs (Envir.eta_contract t)
|
blanchet@38506
|
79 |
|>> AAtom ||> union (op =) ts)
|
blanchet@38506
|
80 |
in do_formula [] end
|
blanchet@38506
|
81 |
|
blanchet@38841
|
82 |
val presimplify_term = prop_of o Meson.presimplify oo Skip_Proof.make_thm
|
blanchet@38506
|
83 |
|
blanchet@38506
|
84 |
fun concealed_bound_name j = sledgehammer_weak_prefix ^ Int.toString j
|
blanchet@38506
|
85 |
fun conceal_bounds Ts t =
|
blanchet@38506
|
86 |
subst_bounds (map (Free o apfst concealed_bound_name)
|
blanchet@38506
|
87 |
(0 upto length Ts - 1 ~~ Ts), t)
|
blanchet@38506
|
88 |
fun reveal_bounds Ts =
|
blanchet@38506
|
89 |
subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
|
blanchet@38506
|
90 |
(0 upto length Ts - 1 ~~ Ts))
|
blanchet@38506
|
91 |
|
blanchet@38831
|
92 |
(* Removes the lambdas from an equation of the form "t = (%x. u)".
|
blanchet@40071
|
93 |
(Cf. "extensionalize_theorem" in "Meson_Clausify".) *)
|
blanchet@38831
|
94 |
fun extensionalize_term t =
|
blanchet@38831
|
95 |
let
|
blanchet@38831
|
96 |
fun aux j (@{const Trueprop} $ t') = @{const Trueprop} $ aux j t'
|
blanchet@38831
|
97 |
| aux j (t as Const (s, Type (_, [Type (_, [_, T']),
|
blanchet@38831
|
98 |
Type (_, [_, res_T])]))
|
blanchet@38831
|
99 |
$ t2 $ Abs (var_s, var_T, t')) =
|
haftmann@39093
|
100 |
if s = @{const_name HOL.eq} orelse s = @{const_name "=="} then
|
blanchet@38831
|
101 |
let val var_t = Var ((var_s, j), var_T) in
|
blanchet@38831
|
102 |
Const (s, T' --> T' --> res_T)
|
blanchet@38831
|
103 |
$ betapply (t2, var_t) $ subst_bound (var_t, t')
|
blanchet@38831
|
104 |
|> aux (j + 1)
|
blanchet@38831
|
105 |
end
|
blanchet@38831
|
106 |
else
|
blanchet@38831
|
107 |
t
|
blanchet@38831
|
108 |
| aux _ t = t
|
blanchet@38831
|
109 |
in aux (maxidx_of_term t + 1) t end
|
blanchet@38831
|
110 |
|
blanchet@38506
|
111 |
fun introduce_combinators_in_term ctxt kind t =
|
blanchet@38716
|
112 |
let val thy = ProofContext.theory_of ctxt in
|
blanchet@38716
|
113 |
if Meson.is_fol_term thy t then
|
blanchet@38716
|
114 |
t
|
blanchet@38716
|
115 |
else
|
blanchet@38716
|
116 |
let
|
blanchet@38716
|
117 |
fun aux Ts t =
|
blanchet@38716
|
118 |
case t of
|
blanchet@38716
|
119 |
@{const Not} $ t1 => @{const Not} $ aux Ts t1
|
blanchet@38716
|
120 |
| (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
|
blanchet@38716
|
121 |
t0 $ Abs (s, T, aux (T :: Ts) t')
|
blanchet@38890
|
122 |
| (t0 as Const (@{const_name All}, _)) $ t1 =>
|
blanchet@38890
|
123 |
aux Ts (t0 $ eta_expand Ts t1 1)
|
blanchet@38716
|
124 |
| (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
|
blanchet@38716
|
125 |
t0 $ Abs (s, T, aux (T :: Ts) t')
|
blanchet@38890
|
126 |
| (t0 as Const (@{const_name Ex}, _)) $ t1 =>
|
blanchet@38890
|
127 |
aux Ts (t0 $ eta_expand Ts t1 1)
|
haftmann@39028
|
128 |
| (t0 as @{const HOL.conj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
|
haftmann@39028
|
129 |
| (t0 as @{const HOL.disj}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
|
haftmann@39019
|
130 |
| (t0 as @{const HOL.implies}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
|
haftmann@39093
|
131 |
| (t0 as Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])))
|
blanchet@38716
|
132 |
$ t1 $ t2 =>
|
blanchet@38716
|
133 |
t0 $ aux Ts t1 $ aux Ts t2
|
blanchet@38716
|
134 |
| _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
|
blanchet@38716
|
135 |
t
|
blanchet@38716
|
136 |
else
|
blanchet@38716
|
137 |
t |> conceal_bounds Ts
|
blanchet@38716
|
138 |
|> Envir.eta_contract
|
blanchet@38716
|
139 |
|> cterm_of thy
|
blanchet@40071
|
140 |
|> Meson_Clausify.introduce_combinators_in_cterm
|
blanchet@38716
|
141 |
|> prop_of |> Logic.dest_equals |> snd
|
blanchet@38716
|
142 |
|> reveal_bounds Ts
|
blanchet@39616
|
143 |
val (t, ctxt') = Variable.import_terms true [t] ctxt |>> the_single
|
blanchet@38716
|
144 |
in t |> aux [] |> singleton (Variable.export_terms ctxt' ctxt) end
|
blanchet@38716
|
145 |
handle THM _ =>
|
blanchet@38716
|
146 |
(* A type variable of sort "{}" will make abstraction fail. *)
|
blanchet@38836
|
147 |
if kind = Conjecture then HOLogic.false_const
|
blanchet@38836
|
148 |
else HOLogic.true_const
|
blanchet@38716
|
149 |
end
|
blanchet@38506
|
150 |
|
blanchet@38506
|
151 |
(* Metis's use of "resolve_tac" freezes the schematic variables. We simulate the
|
blanchet@38506
|
152 |
same in Sledgehammer to prevent the discovery of unreplable proofs. *)
|
blanchet@38506
|
153 |
fun freeze_term t =
|
blanchet@38506
|
154 |
let
|
blanchet@38506
|
155 |
fun aux (t $ u) = aux t $ aux u
|
blanchet@38506
|
156 |
| aux (Abs (s, T, t)) = Abs (s, T, aux t)
|
blanchet@38506
|
157 |
| aux (Var ((s, i), T)) =
|
blanchet@38506
|
158 |
Free (sledgehammer_weak_prefix ^ s ^ "_" ^ string_of_int i, T)
|
blanchet@38506
|
159 |
| aux t = t
|
blanchet@38506
|
160 |
in t |> exists_subterm is_Var t ? aux end
|
blanchet@38506
|
161 |
|
blanchet@38827
|
162 |
(* "Object_Logic.atomize_term" isn't as powerful as it could be; for example,
|
blanchet@38827
|
163 |
it leaves metaequalities over "prop"s alone. *)
|
blanchet@38828
|
164 |
val atomize_term =
|
blanchet@38828
|
165 |
let
|
blanchet@38828
|
166 |
fun aux (@{const Trueprop} $ t1) = t1
|
blanchet@38828
|
167 |
| aux (Const (@{const_name all}, _) $ Abs (s, T, t')) =
|
blanchet@38828
|
168 |
HOLogic.all_const T $ Abs (s, T, aux t')
|
blanchet@38828
|
169 |
| aux (@{const "==>"} $ t1 $ t2) = HOLogic.mk_imp (pairself aux (t1, t2))
|
blanchet@38828
|
170 |
| aux (Const (@{const_name "=="}, Type (_, [@{typ prop}, _])) $ t1 $ t2) =
|
blanchet@38828
|
171 |
HOLogic.eq_const HOLogic.boolT $ aux t1 $ aux t2
|
blanchet@38828
|
172 |
| aux (Const (@{const_name "=="}, Type (_, [T, _])) $ t1 $ t2) =
|
blanchet@38828
|
173 |
HOLogic.eq_const T $ t1 $ t2
|
blanchet@38828
|
174 |
| aux _ = raise Fail "aux"
|
blanchet@38828
|
175 |
in perhaps (try aux) end
|
blanchet@38827
|
176 |
|
blanchet@38506
|
177 |
(* making axiom and conjecture formulas *)
|
blanchet@38836
|
178 |
fun make_formula ctxt presimp name kind t =
|
blanchet@38506
|
179 |
let
|
blanchet@38506
|
180 |
val thy = ProofContext.theory_of ctxt
|
blanchet@38831
|
181 |
val t = t |> Envir.beta_eta_contract
|
blanchet@38890
|
182 |
|> transform_elim_term
|
blanchet@38827
|
183 |
|> atomize_term
|
blanchet@38890
|
184 |
val need_trueprop = (fastype_of t = HOLogic.boolT)
|
blanchet@38890
|
185 |
val t = t |> need_trueprop ? HOLogic.mk_Trueprop
|
blanchet@38506
|
186 |
|> extensionalize_term
|
blanchet@38506
|
187 |
|> presimp ? presimplify_term thy
|
blanchet@38506
|
188 |
|> perhaps (try (HOLogic.dest_Trueprop))
|
blanchet@38506
|
189 |
|> introduce_combinators_in_term ctxt kind
|
blanchet@38836
|
190 |
|> kind <> Axiom ? freeze_term
|
blanchet@38506
|
191 |
val (combformula, ctypes_sorts) = combformula_for_prop thy t []
|
blanchet@38506
|
192 |
in
|
blanchet@38991
|
193 |
{name = name, combformula = combformula, kind = kind,
|
blanchet@38991
|
194 |
ctypes_sorts = ctypes_sorts}
|
blanchet@38506
|
195 |
end
|
blanchet@38506
|
196 |
|
blanchet@38991
|
197 |
fun make_axiom ctxt presimp ((name, loc), th) =
|
blanchet@38841
|
198 |
case make_formula ctxt presimp name Axiom (prop_of th) of
|
blanchet@38991
|
199 |
{combformula = AAtom (CombConst (("c_True", _), _, _)), ...} => NONE
|
blanchet@38991
|
200 |
| formula => SOME ((name, loc), formula)
|
blanchet@38836
|
201 |
fun make_conjecture ctxt ts =
|
blanchet@38836
|
202 |
let val last = length ts - 1 in
|
blanchet@38836
|
203 |
map2 (fn j => make_formula ctxt true (Int.toString j)
|
blanchet@38836
|
204 |
(if j = last then Conjecture else Hypothesis))
|
blanchet@38836
|
205 |
(0 upto last) ts
|
blanchet@38836
|
206 |
end
|
blanchet@38506
|
207 |
|
blanchet@38506
|
208 |
(** Helper facts **)
|
blanchet@38506
|
209 |
|
blanchet@38506
|
210 |
fun count_combterm (CombConst ((s, _), _, _)) =
|
blanchet@38506
|
211 |
Symtab.map_entry s (Integer.add 1)
|
blanchet@38506
|
212 |
| count_combterm (CombVar _) = I
|
blanchet@38506
|
213 |
| count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
|
blanchet@38506
|
214 |
fun count_combformula (AQuant (_, _, phi)) = count_combformula phi
|
blanchet@38506
|
215 |
| count_combformula (AConn (_, phis)) = fold count_combformula phis
|
blanchet@38506
|
216 |
| count_combformula (AAtom tm) = count_combterm tm
|
blanchet@40358
|
217 |
fun count_translated_formula ({combformula, ...} : translated_formula) =
|
blanchet@38506
|
218 |
count_combformula combformula
|
blanchet@38506
|
219 |
|
blanchet@38506
|
220 |
val optional_helpers =
|
blanchet@40134
|
221 |
[(["c_COMBI"], @{thms Meson.COMBI_def}),
|
blanchet@40134
|
222 |
(["c_COMBK"], @{thms Meson.COMBK_def}),
|
blanchet@40134
|
223 |
(["c_COMBB"], @{thms Meson.COMBB_def}),
|
blanchet@40134
|
224 |
(["c_COMBC"], @{thms Meson.COMBC_def}),
|
blanchet@40134
|
225 |
(["c_COMBS"], @{thms Meson.COMBS_def})]
|
blanchet@38506
|
226 |
val optional_typed_helpers =
|
blanchet@38917
|
227 |
[(["c_True", "c_False", "c_If"], @{thms True_or_False}),
|
blanchet@38917
|
228 |
(["c_If"], @{thms if_True if_False})]
|
blanchet@40135
|
229 |
val mandatory_helpers = @{thms Metis.fequal_def}
|
blanchet@38506
|
230 |
|
blanchet@38506
|
231 |
val init_counters =
|
blanchet@38926
|
232 |
[optional_helpers, optional_typed_helpers] |> maps (maps fst)
|
blanchet@38917
|
233 |
|> sort_distinct string_ord |> map (rpair 0) |> Symtab.make
|
blanchet@38506
|
234 |
|
blanchet@38506
|
235 |
fun get_helper_facts ctxt is_FO full_types conjectures axioms =
|
blanchet@38506
|
236 |
let
|
blanchet@40250
|
237 |
val ct =
|
blanchet@40358
|
238 |
fold (fold count_translated_formula) [conjectures, axioms] init_counters
|
blanchet@38506
|
239 |
fun is_needed c = the (Symtab.lookup ct c) > 0
|
blanchet@38937
|
240 |
fun baptize th = ((Thm.get_name_hint th, false), th)
|
blanchet@38506
|
241 |
in
|
blanchet@38506
|
242 |
(optional_helpers
|
blanchet@38506
|
243 |
|> full_types ? append optional_typed_helpers
|
blanchet@38506
|
244 |
|> maps (fn (ss, ths) =>
|
blanchet@38937
|
245 |
if exists is_needed ss then map baptize ths else [])) @
|
blanchet@38937
|
246 |
(if is_FO then [] else map baptize mandatory_helpers)
|
blanchet@38841
|
247 |
|> map_filter (Option.map snd o make_axiom ctxt false)
|
blanchet@38506
|
248 |
end
|
blanchet@38506
|
249 |
|
blanchet@40358
|
250 |
fun translate_axiom ctxt (ax as (_, th)) = (prop_of th, make_axiom ctxt true ax)
|
blanchet@39248
|
251 |
|
blanchet@40358
|
252 |
fun translate_formulas ctxt full_types hyp_ts concl_t axioms =
|
blanchet@38506
|
253 |
let
|
blanchet@38506
|
254 |
val thy = ProofContext.theory_of ctxt
|
blanchet@40358
|
255 |
val (axiom_ts, translated_axioms) = ListPair.unzip axioms
|
blanchet@39249
|
256 |
(* Remove existing axioms from the conjecture, as this can dramatically
|
blanchet@39249
|
257 |
boost an ATP's performance (for some reason). *)
|
blanchet@39249
|
258 |
val hyp_ts = hyp_ts |> filter_out (member (op aconv) axiom_ts)
|
blanchet@38506
|
259 |
val goal_t = Logic.list_implies (hyp_ts, concl_t)
|
blanchet@38506
|
260 |
val is_FO = Meson.is_fol_term thy goal_t
|
blanchet@38506
|
261 |
val subs = tfree_classes_of_terms [goal_t]
|
blanchet@38506
|
262 |
val supers = tvar_classes_of_terms axiom_ts
|
blanchet@38506
|
263 |
val tycons = type_consts_of_terms thy (goal_t :: axiom_ts)
|
blanchet@38506
|
264 |
(* TFrees in the conjecture; TVars in the axioms *)
|
blanchet@38836
|
265 |
val conjectures = make_conjecture ctxt (hyp_ts @ [concl_t])
|
blanchet@40358
|
266 |
val (axiom_names, axioms) = ListPair.unzip (map_filter I translated_axioms)
|
blanchet@38506
|
267 |
val helper_facts = get_helper_facts ctxt is_FO full_types conjectures axioms
|
blanchet@38506
|
268 |
val (supers', arity_clauses) = make_arity_clauses thy tycons supers
|
blanchet@38506
|
269 |
val class_rel_clauses = make_class_rel_clauses thy subs supers'
|
blanchet@38506
|
270 |
in
|
blanchet@39053
|
271 |
(axiom_names |> map single |> Vector.fromList,
|
blanchet@38506
|
272 |
(conjectures, axioms, helper_facts, class_rel_clauses, arity_clauses))
|
blanchet@38506
|
273 |
end
|
blanchet@38506
|
274 |
|
blanchet@38506
|
275 |
fun wrap_type ty t = ATerm ((type_wrapper_name, type_wrapper_name), [ty, t])
|
blanchet@38506
|
276 |
|
blanchet@38506
|
277 |
fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
|
blanchet@38506
|
278 |
| fo_term_for_combtyp (CombTFree name) = ATerm (name, [])
|
blanchet@38506
|
279 |
| fo_term_for_combtyp (CombType (name, tys)) =
|
blanchet@38506
|
280 |
ATerm (name, map fo_term_for_combtyp tys)
|
blanchet@38506
|
281 |
|
blanchet@38506
|
282 |
fun fo_literal_for_type_literal (TyLitVar (class, name)) =
|
blanchet@38506
|
283 |
(true, ATerm (class, [ATerm (name, [])]))
|
blanchet@38506
|
284 |
| fo_literal_for_type_literal (TyLitFree (class, name)) =
|
blanchet@38506
|
285 |
(true, ATerm (class, [ATerm (name, [])]))
|
blanchet@38506
|
286 |
|
blanchet@38506
|
287 |
fun formula_for_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
|
blanchet@38506
|
288 |
|
blanchet@38506
|
289 |
fun fo_term_for_combterm full_types =
|
blanchet@38506
|
290 |
let
|
blanchet@38506
|
291 |
fun aux top_level u =
|
blanchet@38506
|
292 |
let
|
blanchet@38506
|
293 |
val (head, args) = strip_combterm_comb u
|
blanchet@38506
|
294 |
val (x, ty_args) =
|
blanchet@38506
|
295 |
case head of
|
blanchet@38506
|
296 |
CombConst (name as (s, s'), _, ty_args) =>
|
blanchet@38721
|
297 |
let val ty_args = if full_types then [] else ty_args in
|
blanchet@38721
|
298 |
if s = "equal" then
|
blanchet@38721
|
299 |
if top_level andalso length args = 2 then (name, [])
|
blanchet@40135
|
300 |
else (("c_fequal", @{const_name Metis.fequal}), ty_args)
|
blanchet@38721
|
301 |
else if top_level then
|
blanchet@38721
|
302 |
case s of
|
blanchet@38721
|
303 |
"c_False" => (("$false", s'), [])
|
blanchet@38721
|
304 |
| "c_True" => (("$true", s'), [])
|
blanchet@38721
|
305 |
| _ => (name, ty_args)
|
blanchet@38721
|
306 |
else
|
blanchet@38721
|
307 |
(name, ty_args)
|
blanchet@38721
|
308 |
end
|
blanchet@38506
|
309 |
| CombVar (name, _) => (name, [])
|
blanchet@38506
|
310 |
| CombApp _ => raise Fail "impossible \"CombApp\""
|
blanchet@38506
|
311 |
val t = ATerm (x, map fo_term_for_combtyp ty_args @
|
blanchet@38506
|
312 |
map (aux false) args)
|
blanchet@38506
|
313 |
in
|
blanchet@38506
|
314 |
if full_types then wrap_type (fo_term_for_combtyp (combtyp_of u)) t else t
|
blanchet@38506
|
315 |
end
|
blanchet@38506
|
316 |
in aux true end
|
blanchet@38506
|
317 |
|
blanchet@38506
|
318 |
fun formula_for_combformula full_types =
|
blanchet@38506
|
319 |
let
|
blanchet@38506
|
320 |
fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
|
blanchet@38506
|
321 |
| aux (AConn (c, phis)) = AConn (c, map aux phis)
|
blanchet@38506
|
322 |
| aux (AAtom tm) = AAtom (fo_term_for_combterm full_types tm)
|
blanchet@38506
|
323 |
in aux end
|
blanchet@38506
|
324 |
|
blanchet@38991
|
325 |
fun formula_for_axiom full_types
|
blanchet@40358
|
326 |
({combformula, ctypes_sorts, ...} : translated_formula) =
|
blanchet@38506
|
327 |
mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
|
blanchet@38506
|
328 |
(type_literals_for_types ctypes_sorts))
|
blanchet@38506
|
329 |
(formula_for_combformula full_types combformula)
|
blanchet@38506
|
330 |
|
blanchet@38991
|
331 |
fun problem_line_for_fact prefix full_types (formula as {name, kind, ...}) =
|
blanchet@38506
|
332 |
Fof (prefix ^ ascii_of name, kind, formula_for_axiom full_types formula)
|
blanchet@38506
|
333 |
|
blanchet@38506
|
334 |
fun problem_line_for_class_rel_clause (ClassRelClause {name, subclass,
|
blanchet@38506
|
335 |
superclass, ...}) =
|
blanchet@38506
|
336 |
let val ty_arg = ATerm (("T", "T"), []) in
|
blanchet@38506
|
337 |
Fof (class_rel_clause_prefix ^ ascii_of name, Axiom,
|
blanchet@38506
|
338 |
AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
|
blanchet@38506
|
339 |
AAtom (ATerm (superclass, [ty_arg]))]))
|
blanchet@38506
|
340 |
end
|
blanchet@38506
|
341 |
|
blanchet@38506
|
342 |
fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
|
blanchet@38506
|
343 |
(true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
|
blanchet@38506
|
344 |
| fo_literal_for_arity_literal (TVarLit (c, sort)) =
|
blanchet@38506
|
345 |
(false, ATerm (c, [ATerm (sort, [])]))
|
blanchet@38506
|
346 |
|
blanchet@38506
|
347 |
fun problem_line_for_arity_clause (ArityClause {name, conclLit, premLits,
|
blanchet@38506
|
348 |
...}) =
|
blanchet@38506
|
349 |
Fof (arity_clause_prefix ^ ascii_of name, Axiom,
|
blanchet@38506
|
350 |
mk_ahorn (map (formula_for_fo_literal o apfst not
|
blanchet@38506
|
351 |
o fo_literal_for_arity_literal) premLits)
|
blanchet@38506
|
352 |
(formula_for_fo_literal
|
blanchet@38506
|
353 |
(fo_literal_for_arity_literal conclLit)))
|
blanchet@38506
|
354 |
|
blanchet@38506
|
355 |
fun problem_line_for_conjecture full_types
|
blanchet@40358
|
356 |
({name, kind, combformula, ...} : translated_formula) =
|
blanchet@38506
|
357 |
Fof (conjecture_prefix ^ name, kind,
|
blanchet@38506
|
358 |
formula_for_combformula full_types combformula)
|
blanchet@38506
|
359 |
|
blanchet@40358
|
360 |
fun free_type_literals_for_conjecture
|
blanchet@40358
|
361 |
({ctypes_sorts, ...} : translated_formula) =
|
blanchet@38506
|
362 |
map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts)
|
blanchet@38506
|
363 |
|
blanchet@40156
|
364 |
fun problem_line_for_free_type j lit =
|
blanchet@40156
|
365 |
Fof (tfree_prefix ^ string_of_int j, Hypothesis, formula_for_fo_literal lit)
|
blanchet@38506
|
366 |
fun problem_lines_for_free_types conjectures =
|
blanchet@38506
|
367 |
let
|
blanchet@38506
|
368 |
val litss = map free_type_literals_for_conjecture conjectures
|
blanchet@38506
|
369 |
val lits = fold (union (op =)) litss []
|
blanchet@40156
|
370 |
in map2 problem_line_for_free_type (0 upto length lits - 1) lits end
|
blanchet@38506
|
371 |
|
blanchet@38506
|
372 |
(** "hBOOL" and "hAPP" **)
|
blanchet@38506
|
373 |
|
blanchet@38506
|
374 |
type const_info = {min_arity: int, max_arity: int, sub_level: bool}
|
blanchet@38506
|
375 |
|
blanchet@38506
|
376 |
fun consider_term top_level (ATerm ((s, _), ts)) =
|
blanchet@39692
|
377 |
(if is_atp_variable s then
|
blanchet@38506
|
378 |
I
|
blanchet@38506
|
379 |
else
|
blanchet@38506
|
380 |
let val n = length ts in
|
blanchet@38506
|
381 |
Symtab.map_default
|
blanchet@38506
|
382 |
(s, {min_arity = n, max_arity = 0, sub_level = false})
|
blanchet@38506
|
383 |
(fn {min_arity, max_arity, sub_level} =>
|
blanchet@38506
|
384 |
{min_arity = Int.min (n, min_arity),
|
blanchet@38506
|
385 |
max_arity = Int.max (n, max_arity),
|
blanchet@38506
|
386 |
sub_level = sub_level orelse not top_level})
|
blanchet@38506
|
387 |
end)
|
blanchet@38506
|
388 |
#> fold (consider_term (top_level andalso s = type_wrapper_name)) ts
|
blanchet@38506
|
389 |
fun consider_formula (AQuant (_, _, phi)) = consider_formula phi
|
blanchet@38506
|
390 |
| consider_formula (AConn (_, phis)) = fold consider_formula phis
|
blanchet@38506
|
391 |
| consider_formula (AAtom tm) = consider_term true tm
|
blanchet@38506
|
392 |
|
blanchet@38506
|
393 |
fun consider_problem_line (Fof (_, _, phi)) = consider_formula phi
|
blanchet@38506
|
394 |
fun consider_problem problem = fold (fold consider_problem_line o snd) problem
|
blanchet@38506
|
395 |
|
blanchet@38506
|
396 |
fun const_table_for_problem explicit_apply problem =
|
blanchet@38506
|
397 |
if explicit_apply then NONE
|
blanchet@38506
|
398 |
else SOME (Symtab.empty |> consider_problem problem)
|
blanchet@38506
|
399 |
|
blanchet@38506
|
400 |
fun min_arity_of thy full_types NONE s =
|
blanchet@38506
|
401 |
(if s = "equal" orelse s = type_wrapper_name orelse
|
blanchet@38506
|
402 |
String.isPrefix type_const_prefix s orelse
|
blanchet@38506
|
403 |
String.isPrefix class_prefix s then
|
blanchet@38506
|
404 |
16383 (* large number *)
|
blanchet@38506
|
405 |
else if full_types then
|
blanchet@38506
|
406 |
0
|
blanchet@38987
|
407 |
else case strip_prefix_and_unascii const_prefix s of
|
blanchet@38506
|
408 |
SOME s' => num_type_args thy (invert_const s')
|
blanchet@38506
|
409 |
| NONE => 0)
|
blanchet@38506
|
410 |
| min_arity_of _ _ (SOME the_const_tab) s =
|
blanchet@38506
|
411 |
case Symtab.lookup the_const_tab s of
|
blanchet@38506
|
412 |
SOME ({min_arity, ...} : const_info) => min_arity
|
blanchet@38506
|
413 |
| NONE => 0
|
blanchet@38506
|
414 |
|
blanchet@38506
|
415 |
fun full_type_of (ATerm ((s, _), [ty, _])) =
|
blanchet@38506
|
416 |
if s = type_wrapper_name then ty else raise Fail "expected type wrapper"
|
blanchet@38506
|
417 |
| full_type_of _ = raise Fail "expected type wrapper"
|
blanchet@38506
|
418 |
|
blanchet@38506
|
419 |
fun list_hAPP_rev _ t1 [] = t1
|
blanchet@38506
|
420 |
| list_hAPP_rev NONE t1 (t2 :: ts2) =
|
blanchet@38506
|
421 |
ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2])
|
blanchet@38506
|
422 |
| list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
|
blanchet@38506
|
423 |
let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
|
blanchet@38506
|
424 |
[full_type_of t2, ty]) in
|
blanchet@38506
|
425 |
ATerm (`I "hAPP", [wrap_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
|
blanchet@38506
|
426 |
end
|
blanchet@38506
|
427 |
|
blanchet@38506
|
428 |
fun repair_applications_in_term thy full_types const_tab =
|
blanchet@38506
|
429 |
let
|
blanchet@38506
|
430 |
fun aux opt_ty (ATerm (name as (s, _), ts)) =
|
blanchet@38506
|
431 |
if s = type_wrapper_name then
|
blanchet@38506
|
432 |
case ts of
|
blanchet@38506
|
433 |
[t1, t2] => ATerm (name, [aux NONE t1, aux (SOME t1) t2])
|
blanchet@38506
|
434 |
| _ => raise Fail "malformed type wrapper"
|
blanchet@38506
|
435 |
else
|
blanchet@38506
|
436 |
let
|
blanchet@38506
|
437 |
val ts = map (aux NONE) ts
|
blanchet@38506
|
438 |
val (ts1, ts2) = chop (min_arity_of thy full_types const_tab s) ts
|
blanchet@38506
|
439 |
in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
|
blanchet@38506
|
440 |
in aux NONE end
|
blanchet@38506
|
441 |
|
blanchet@38506
|
442 |
fun boolify t = ATerm (`I "hBOOL", [t])
|
blanchet@38506
|
443 |
|
blanchet@38506
|
444 |
(* True if the constant ever appears outside of the top-level position in
|
blanchet@38506
|
445 |
literals, or if it appears with different arities (e.g., because of different
|
blanchet@38506
|
446 |
type instantiations). If false, the constant always receives all of its
|
blanchet@38506
|
447 |
arguments and is used as a predicate. *)
|
blanchet@38506
|
448 |
fun is_predicate NONE s =
|
blanchet@38812
|
449 |
s = "equal" orelse s = "$false" orelse s = "$true" orelse
|
blanchet@38812
|
450 |
String.isPrefix type_const_prefix s orelse String.isPrefix class_prefix s
|
blanchet@38506
|
451 |
| is_predicate (SOME the_const_tab) s =
|
blanchet@38506
|
452 |
case Symtab.lookup the_const_tab s of
|
blanchet@38506
|
453 |
SOME {min_arity, max_arity, sub_level} =>
|
blanchet@38506
|
454 |
not sub_level andalso min_arity = max_arity
|
blanchet@38506
|
455 |
| NONE => false
|
blanchet@38506
|
456 |
|
blanchet@38506
|
457 |
fun repair_predicates_in_term const_tab (t as ATerm ((s, _), ts)) =
|
blanchet@38506
|
458 |
if s = type_wrapper_name then
|
blanchet@38506
|
459 |
case ts of
|
blanchet@38506
|
460 |
[_, t' as ATerm ((s', _), _)] =>
|
blanchet@38506
|
461 |
if is_predicate const_tab s' then t' else boolify t
|
blanchet@38506
|
462 |
| _ => raise Fail "malformed type wrapper"
|
blanchet@38506
|
463 |
else
|
blanchet@38506
|
464 |
t |> not (is_predicate const_tab s) ? boolify
|
blanchet@38506
|
465 |
|
blanchet@38506
|
466 |
fun close_universally phi =
|
blanchet@38506
|
467 |
let
|
blanchet@38506
|
468 |
fun term_vars bounds (ATerm (name as (s, _), tms)) =
|
blanchet@39692
|
469 |
(is_atp_variable s andalso not (member (op =) bounds name))
|
blanchet@38506
|
470 |
? insert (op =) name
|
blanchet@38506
|
471 |
#> fold (term_vars bounds) tms
|
blanchet@38917
|
472 |
fun formula_vars bounds (AQuant (_, xs, phi)) =
|
blanchet@38506
|
473 |
formula_vars (xs @ bounds) phi
|
blanchet@38506
|
474 |
| formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
|
blanchet@38506
|
475 |
| formula_vars bounds (AAtom tm) = term_vars bounds tm
|
blanchet@38506
|
476 |
in
|
blanchet@38506
|
477 |
case formula_vars [] phi [] of [] => phi | xs => AQuant (AForall, xs, phi)
|
blanchet@38506
|
478 |
end
|
blanchet@38506
|
479 |
|
blanchet@38506
|
480 |
fun repair_formula thy explicit_forall full_types const_tab =
|
blanchet@38506
|
481 |
let
|
blanchet@38506
|
482 |
fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
|
blanchet@38506
|
483 |
| aux (AConn (c, phis)) = AConn (c, map aux phis)
|
blanchet@38506
|
484 |
| aux (AAtom tm) =
|
blanchet@38506
|
485 |
AAtom (tm |> repair_applications_in_term thy full_types const_tab
|
blanchet@38506
|
486 |
|> repair_predicates_in_term const_tab)
|
blanchet@38506
|
487 |
in aux #> explicit_forall ? close_universally end
|
blanchet@38506
|
488 |
|
blanchet@38506
|
489 |
fun repair_problem_line thy explicit_forall full_types const_tab
|
blanchet@38506
|
490 |
(Fof (ident, kind, phi)) =
|
blanchet@38506
|
491 |
Fof (ident, kind, repair_formula thy explicit_forall full_types const_tab phi)
|
blanchet@38506
|
492 |
fun repair_problem_with_const_table thy =
|
blanchet@38506
|
493 |
map o apsnd o map ooo repair_problem_line thy
|
blanchet@38506
|
494 |
|
blanchet@38506
|
495 |
fun repair_problem thy explicit_forall full_types explicit_apply problem =
|
blanchet@38506
|
496 |
repair_problem_with_const_table thy explicit_forall full_types
|
blanchet@38506
|
497 |
(const_table_for_problem explicit_apply problem) problem
|
blanchet@38506
|
498 |
|
blanchet@40240
|
499 |
fun prepare_atp_problem ctxt readable_names explicit_forall full_types
|
blanchet@40240
|
500 |
explicit_apply hyp_ts concl_t axioms =
|
blanchet@38506
|
501 |
let
|
blanchet@38506
|
502 |
val thy = ProofContext.theory_of ctxt
|
blanchet@38506
|
503 |
val (axiom_names, (conjectures, axioms, helper_facts, class_rel_clauses,
|
blanchet@38506
|
504 |
arity_clauses)) =
|
blanchet@40358
|
505 |
translate_formulas ctxt full_types hyp_ts concl_t axioms
|
blanchet@38506
|
506 |
val axiom_lines = map (problem_line_for_fact axiom_prefix full_types) axioms
|
blanchet@38506
|
507 |
val helper_lines =
|
blanchet@38506
|
508 |
map (problem_line_for_fact helper_prefix full_types) helper_facts
|
blanchet@38506
|
509 |
val conjecture_lines =
|
blanchet@38506
|
510 |
map (problem_line_for_conjecture full_types) conjectures
|
blanchet@38506
|
511 |
val tfree_lines = problem_lines_for_free_types conjectures
|
blanchet@38506
|
512 |
val class_rel_lines =
|
blanchet@38506
|
513 |
map problem_line_for_class_rel_clause class_rel_clauses
|
blanchet@38506
|
514 |
val arity_lines = map problem_line_for_arity_clause arity_clauses
|
blanchet@38506
|
515 |
(* Reordering these might or might not confuse the proof reconstruction
|
blanchet@38506
|
516 |
code or the SPASS Flotter hack. *)
|
blanchet@38506
|
517 |
val problem =
|
blanchet@38506
|
518 |
[("Relevant facts", axiom_lines),
|
blanchet@38506
|
519 |
("Class relationships", class_rel_lines),
|
blanchet@38506
|
520 |
("Arity declarations", arity_lines),
|
blanchet@38506
|
521 |
("Helper facts", helper_lines),
|
blanchet@38506
|
522 |
("Conjectures", conjecture_lines),
|
blanchet@38506
|
523 |
("Type variables", tfree_lines)]
|
blanchet@38506
|
524 |
|> repair_problem thy explicit_forall full_types explicit_apply
|
blanchet@39692
|
525 |
val (problem, pool) = nice_atp_problem readable_names problem
|
blanchet@38506
|
526 |
val conjecture_offset =
|
blanchet@38506
|
527 |
length axiom_lines + length class_rel_lines + length arity_lines
|
blanchet@38506
|
528 |
+ length helper_lines
|
blanchet@38506
|
529 |
in
|
blanchet@38506
|
530 |
(problem,
|
blanchet@38506
|
531 |
case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
|
blanchet@38506
|
532 |
conjecture_offset, axiom_names)
|
blanchet@38506
|
533 |
end
|
blanchet@38506
|
534 |
|
blanchet@38506
|
535 |
end;
|