1 (* Title: HOL/Tools/Metis/metis_translate.ML
2 Author: Jia Meng, Cambridge University Computer Laboratory and NICTA
3 Author: Kong W. Susanto, Cambridge University Computer Laboratory
4 Author: Lawrence C. Paulson, Cambridge University Computer Laboratory
5 Author: Jasmin Blanchette, TU Muenchen
7 Translation of HOL to FOL for Metis.
10 signature METIS_TRANSLATE =
12 type type_literal = ATP_Translate.type_literal
13 type type_sys = ATP_Translate.type_sys
15 datatype mode = FO | HO | FT | MX
18 {axioms : (Metis_Thm.thm * thm) list,
19 tfrees : type_literal list,
20 old_skolems : (string * term) list}
22 val metis_equal : string
23 val metis_predicator : string
24 val metis_app_op : string
25 val metis_type_tag : string
26 val metis_generated_var_prefix : string
27 val metis_name_table : ((string * int) * (string * bool)) list
28 val reveal_old_skolem_terms : (string * term) list -> term -> term
29 val string_of_mode : mode -> string
30 val prepare_metis_problem :
31 Proof.context -> mode -> type_sys option -> thm list -> thm list
32 -> mode * int Symtab.table * metis_problem
35 structure Metis_Translate : METIS_TRANSLATE =
42 val metis_predicator = "{}"
43 val metis_app_op = "."
44 val metis_type_tag = ":"
45 val metis_generated_var_prefix = "_"
47 val metis_name_table =
48 [((tptp_equal, 2), (metis_equal, false)),
49 ((tptp_old_equal, 2), (metis_equal, false)),
50 ((const_prefix ^ predicator_name, 1), (metis_predicator, false)),
51 ((const_prefix ^ app_op_name, 2), (metis_app_op, false)),
52 ((const_prefix ^ type_tag_name, 2), (metis_type_tag, true))]
54 fun predicate_of thy ((@{const Not} $ P), pos) = predicate_of thy (P, not pos)
55 | predicate_of thy (t, pos) =
56 (combterm_from_term thy [] (Envir.eta_contract t), pos)
58 fun literals_of_term1 args thy (@{const Trueprop} $ P) =
59 literals_of_term1 args thy P
60 | literals_of_term1 args thy (@{const HOL.disj} $ P $ Q) =
61 literals_of_term1 (literals_of_term1 args thy P) thy Q
62 | literals_of_term1 (lits, ts) thy P =
63 let val ((pred, ts'), pol) = predicate_of thy (P, true) in
64 ((pol, pred) :: lits, union (op =) ts ts')
66 val literals_of_term = literals_of_term1 ([], [])
68 fun old_skolem_const_name i j num_T_args =
69 old_skolem_const_prefix ^ Long_Name.separator ^
70 (space_implode Long_Name.separator (map string_of_int [i, j, num_T_args]))
72 fun conceal_old_skolem_terms i old_skolems t =
73 if exists_Const (curry (op =) @{const_name Meson.skolem} o fst) t then
76 (t as (Const (@{const_name Meson.skolem}, Type (_, [_, T])) $ _)) =
78 val (old_skolems, s) =
80 (old_skolems, @{const_name undefined})
81 else case AList.find (op aconv) old_skolems t of
82 s :: _ => (old_skolems, s)
85 val s = old_skolem_const_name i (length old_skolems)
86 (length (Term.add_tvarsT T []))
87 in ((s, t) :: old_skolems, s) end
88 in (old_skolems, Const (s, T)) end
89 | aux old_skolems (t1 $ t2) =
91 val (old_skolems, t1) = aux old_skolems t1
92 val (old_skolems, t2) = aux old_skolems t2
93 in (old_skolems, t1 $ t2) end
94 | aux old_skolems (Abs (s, T, t')) =
95 let val (old_skolems, t') = aux old_skolems t' in
96 (old_skolems, Abs (s, T, t'))
98 | aux old_skolems t = (old_skolems, t)
99 in aux old_skolems t end
103 fun reveal_old_skolem_terms old_skolems =
104 map_aterms (fn t as Const (s, _) =>
105 if String.isPrefix old_skolem_const_prefix s then
106 AList.lookup (op =) old_skolems s |> the
107 |> map_types Type_Infer.paramify_vars
113 (* ------------------------------------------------------------------------- *)
114 (* HOL to FOL (Isabelle to Metis) *)
115 (* ------------------------------------------------------------------------- *)
117 (* first-order, higher-order, fully-typed, mode X (fleXible) *)
118 datatype mode = FO | HO | FT | MX
120 fun string_of_mode FO = "FO"
121 | string_of_mode HO = "HO"
122 | string_of_mode FT = "FT"
123 | string_of_mode MX = "MX"
125 fun fn_isa_to_met_sublevel "equal" = "c_fequal"
126 | fn_isa_to_met_sublevel "c_False" = "c_fFalse"
127 | fn_isa_to_met_sublevel "c_True" = "c_fTrue"
128 | fn_isa_to_met_sublevel "c_Not" = "c_fNot"
129 | fn_isa_to_met_sublevel "c_conj" = "c_fconj"
130 | fn_isa_to_met_sublevel "c_disj" = "c_fdisj"
131 | fn_isa_to_met_sublevel "c_implies" = "c_fimplies"
132 | fn_isa_to_met_sublevel x = x
134 fun fn_isa_to_met_toplevel "equal" = metis_equal
135 | fn_isa_to_met_toplevel x = x
137 fun metis_lit b c args = (b, (c, args));
139 fun metis_term_from_typ (Type (s, Ts)) =
140 Metis_Term.Fn (make_fixed_type_const s, map metis_term_from_typ Ts)
141 | metis_term_from_typ (TFree (s, _)) =
142 Metis_Term.Fn (make_fixed_type_var s, [])
143 | metis_term_from_typ (TVar (x, _)) =
144 Metis_Term.Var (make_schematic_type_var x)
146 (*These two functions insert type literals before the real literals. That is the
147 opposite order from TPTP linkup, but maybe OK.*)
149 fun hol_term_to_fol_FO tm =
150 case strip_combterm_comb tm of
151 (CombConst ((c, _), _, Ts), tms) =>
152 let val tyargs = map metis_term_from_typ Ts
153 val args = map hol_term_to_fol_FO tms
154 in Metis_Term.Fn (c, tyargs @ args) end
155 | (CombVar ((v, _), _), []) => Metis_Term.Var v
156 | _ => raise Fail "non-first-order combterm"
158 fun hol_term_to_fol_HO (CombConst ((a, _), _, Ts)) =
159 Metis_Term.Fn (fn_isa_to_met_sublevel a, map metis_term_from_typ Ts)
160 | hol_term_to_fol_HO (CombVar ((s, _), _)) = Metis_Term.Var s
161 | hol_term_to_fol_HO (CombApp (tm1, tm2)) =
162 Metis_Term.Fn (metis_app_op, map hol_term_to_fol_HO [tm1, tm2])
164 (*The fully-typed translation, to avoid type errors*)
165 fun tag_with_type tm T =
166 Metis_Term.Fn (metis_type_tag, [tm, metis_term_from_typ T])
168 fun hol_term_to_fol_FT (CombVar ((s, _), ty)) =
169 tag_with_type (Metis_Term.Var s) ty
170 | hol_term_to_fol_FT (CombConst ((a, _), ty, _)) =
171 tag_with_type (Metis_Term.Fn (fn_isa_to_met_sublevel a, [])) ty
172 | hol_term_to_fol_FT (tm as CombApp (tm1,tm2)) =
174 (Metis_Term.Fn (metis_app_op, map hol_term_to_fol_FT [tm1, tm2]))
177 fun hol_literal_to_fol FO (pos, tm) =
179 val (CombConst((p, _), _, Ts), tms) = strip_combterm_comb tm
180 val tylits = if p = "equal" then [] else map metis_term_from_typ Ts
181 val lits = map hol_term_to_fol_FO tms
182 in metis_lit pos (fn_isa_to_met_toplevel p) (tylits @ lits) end
183 | hol_literal_to_fol HO (pos, tm) =
184 (case strip_combterm_comb tm of
185 (CombConst(("equal", _), _, _), tms) =>
186 metis_lit pos metis_equal (map hol_term_to_fol_HO tms)
187 | _ => metis_lit pos metis_predicator [hol_term_to_fol_HO tm])
188 | hol_literal_to_fol FT (pos, tm) =
189 (case strip_combterm_comb tm of
190 (CombConst(("equal", _), _, _), tms) =>
191 metis_lit pos metis_equal (map hol_term_to_fol_FT tms)
192 | _ => metis_lit pos metis_predicator [hol_term_to_fol_FT tm])
194 fun literals_of_hol_term thy mode t =
195 let val (lits, types_sorts) = literals_of_term thy t in
196 (map (hol_literal_to_fol mode) lits, types_sorts)
199 (*Sign should be "true" for conjecture type constraints, "false" for type lits in clauses.*)
200 fun metis_of_type_literals pos (TyLitVar ((s, _), (s', _))) =
201 metis_lit pos s [Metis_Term.Var s']
202 | metis_of_type_literals pos (TyLitFree ((s, _), (s', _))) =
203 metis_lit pos s [Metis_Term.Fn (s',[])]
205 fun has_default_sort _ (TVar _) = false
206 | has_default_sort ctxt (TFree (x, s)) =
207 (s = the_default [] (Variable.def_sort ctxt (x, ~1)));
209 fun metis_of_tfree tf =
210 Metis_Thm.axiom (Metis_LiteralSet.singleton (metis_of_type_literals true tf));
212 fun hol_thm_to_fol is_conjecture ctxt mode j old_skolems th =
214 val thy = Proof_Context.theory_of ctxt
215 val (old_skolems, (mlits, types_sorts)) =
216 th |> prop_of |> Logic.strip_imp_concl
217 |> conceal_old_skolem_terms j old_skolems
218 ||> (HOLogic.dest_Trueprop #> literals_of_hol_term thy mode)
220 if is_conjecture then
221 (Metis_Thm.axiom (Metis_LiteralSet.fromList mlits),
222 raw_type_literals_for_types types_sorts, old_skolems)
225 val tylits = types_sorts |> filter_out (has_default_sort ctxt)
226 |> raw_type_literals_for_types
227 val mtylits = map (metis_of_type_literals false) tylits
229 (Metis_Thm.axiom (Metis_LiteralSet.fromList(mtylits @ mlits)), [],
234 (* ------------------------------------------------------------------------- *)
235 (* Logic maps manage the interface between HOL and first-order logic. *)
236 (* ------------------------------------------------------------------------- *)
239 {axioms : (Metis_Thm.thm * thm) list,
240 tfrees : type_literal list,
241 old_skolems : (string * term) list}
243 fun is_quasi_fol_clause thy =
244 Meson.is_fol_term thy o snd o conceal_old_skolem_terms ~1 [] o prop_of
246 (*Extract TFree constraints from context to include as conjecture clauses*)
247 fun init_tfrees ctxt =
248 let fun add ((a,i),s) Ts = if i = ~1 then TFree(a,s) :: Ts else Ts in
249 Vartab.fold add (#2 (Variable.constraints_of ctxt)) []
250 |> raw_type_literals_for_types
253 fun const_in_metis c (pred, tm_list) =
255 fun in_mterm (Metis_Term.Var _) = false
256 | in_mterm (Metis_Term.Fn (nm, tm_list)) =
257 c = nm orelse exists in_mterm tm_list
258 in c = pred orelse exists in_mterm tm_list end
261 fun m_arity_cls (TConsLit ((c, _), (t, _), args)) =
262 metis_lit true c [Metis_Term.Fn(t, map (Metis_Term.Var o fst) args)]
263 | m_arity_cls (TVarLit ((c, _), (s, _))) =
264 metis_lit false c [Metis_Term.Var s]
265 (* TrueI is returned as the Isabelle counterpart because there isn't any. *)
266 fun arity_cls ({prem_lits, concl_lits, ...} : arity_clause) =
268 Metis_Thm.axiom (Metis_LiteralSet.fromList
269 (map m_arity_cls (concl_lits :: prem_lits))));
271 (* CLASSREL CLAUSE *)
272 fun m_class_rel_cls (subclass, _) (superclass, _) =
273 [metis_lit false subclass [Metis_Term.Var "T"],
274 metis_lit true superclass [Metis_Term.Var "T"]]
275 fun class_rel_cls ({subclass, superclass, ...} : class_rel_clause) =
276 (TrueI, m_class_rel_cls subclass superclass
277 |> Metis_LiteralSet.fromList |> Metis_Thm.axiom)
279 fun type_ext thy tms =
281 val subs = tfree_classes_of_terms tms
282 val supers = tvar_classes_of_terms tms
283 val tycons = type_consts_of_terms thy tms
284 val (supers', arity_clauses) = make_arity_clauses thy tycons supers
285 val class_rel_clauses = make_class_rel_clauses thy subs supers'
286 in map class_rel_cls class_rel_clauses @ map arity_cls arity_clauses end
288 fun metis_name_from_atp s ary =
289 AList.lookup (op =) metis_name_table (s, ary) |> the_default (s, false)
290 fun metis_term_from_atp (ATerm (s, tms)) =
291 if is_tptp_variable s then
294 let val (s, swap) = metis_name_from_atp s (length tms) in
295 Metis_Term.Fn (s, tms |> map metis_term_from_atp |> swap ? rev)
297 fun metis_atom_from_atp (AAtom tm) =
298 (case metis_term_from_atp tm of
300 | _ => raise Fail "non CNF -- expected function")
301 | metis_atom_from_atp _ = raise Fail "not CNF -- expected atom"
302 fun metis_literal_from_atp (AConn (ANot, [phi])) =
303 (false, metis_atom_from_atp phi)
304 | metis_literal_from_atp phi = (true, metis_atom_from_atp phi)
305 fun metis_literals_from_atp (AConn (AOr, [phi1, phi2])) =
306 uncurry (union (op =)) (pairself metis_literals_from_atp (phi1, phi2))
307 | metis_literals_from_atp phi = [metis_literal_from_atp phi]
308 fun metis_axiom_from_atp clauses (Formula (ident, _, phi, _, _)) =
309 (phi |> metis_literals_from_atp |> Metis_LiteralSet.fromList
311 case try (unprefix conjecture_prefix) ident of
312 SOME s => Meson.make_meta_clause (nth clauses (the (Int.fromString s)))
314 | metis_axiom_from_atp _ _ = raise Fail "not CNF -- expected formula"
316 val default_type_sys = Preds (Polymorphic, Nonmonotonic_Types, Light)
318 (* Function to generate metis clauses, including comb and type clauses *)
319 fun prepare_metis_problem ctxt MX type_sys conj_clauses fact_clauses =
321 val type_sys = type_sys |> the_default default_type_sys
322 val explicit_apply = NONE
324 conj_clauses @ fact_clauses
325 |> (if polymorphism_of_type_sys type_sys = Polymorphic then
330 #-> Monomorph.monomorph Monomorph.all_schematic_consts_of
331 #> fst #> maps (map snd))
332 val (atp_problem, _, _, _, _, _, sym_tab) =
333 prepare_atp_problem ctxt CNF Hypothesis Axiom type_sys explicit_apply
334 false false (map prop_of clauses) @{prop False} []
337 |> maps (map_filter (try (metis_axiom_from_atp clauses)) o snd)
340 {axioms = axioms, tfrees = [], old_skolems = [] (* FIXME ### *)})
342 | prepare_metis_problem ctxt mode _ conj_clauses fact_clauses =
344 val thy = Proof_Context.theory_of ctxt
345 (* The modes FO and FT are sticky. HO can be downgraded to FO. *)
348 forall (forall (is_quasi_fol_clause thy))
349 [conj_clauses, fact_clauses] then
353 fun add_thm is_conjecture (isa_ith, metis_ith)
354 {axioms, tfrees, old_skolems} : metis_problem =
356 val (mth, tfree_lits, old_skolems) =
357 hol_thm_to_fol is_conjecture ctxt mode (length axioms) old_skolems
360 {axioms = (mth, isa_ith) :: axioms,
361 tfrees = union (op =) tfree_lits tfrees, old_skolems = old_skolems}
363 fun add_type_thm (ith, mth) {axioms, tfrees, old_skolems} =
364 {axioms = (mth, ith) :: axioms, tfrees = tfrees,
365 old_skolems = old_skolems}
366 fun add_tfrees {axioms, tfrees, old_skolems} =
368 map (rpair TrueI o metis_of_tfree) (distinct (op =) tfrees) @ axioms,
369 tfrees = tfrees, old_skolems = old_skolems}
371 {axioms = [], tfrees = init_tfrees ctxt, old_skolems = []}
372 |> fold (add_thm true o `Meson.make_meta_clause) conj_clauses
374 |> fold (add_thm false o `Meson.make_meta_clause) fact_clauses
375 val clause_lists = map (Metis_Thm.clause o #1) (#axioms problem)
377 exists (Metis_LiteralSet.exists (const_in_metis c o #2)) clause_lists
383 val fdefs = @{thms fFalse_def fTrue_def fNot_def fconj_def fdisj_def
384 fimplies_def fequal_def}
387 #> `(Meson.make_meta_clause
388 #> rewrite_rule (map safe_mk_meta_eq fdefs))
391 |> filter (is_used o prefix const_prefix o fst)
392 |> maps (fn (_, (needs_full_types, thms)) =>
393 if needs_full_types andalso mode <> FT then []
394 else map prepare_helper thms)
395 in problem |> fold (add_thm false) helper_ths end
396 val type_ths = type_ext thy (map prop_of (conj_clauses @ fact_clauses))
397 in (mode, Symtab.empty, fold add_type_thm type_ths problem) end