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