1 (* Title: HOL/TPTP/atp_theory_export.ML
2 Author: Jasmin Blanchette, TU Muenchen
5 Export Isabelle theories as first-order TPTP inferences, exploiting
6 Sledgehammer's translation.
9 signature ATP_THEORY_EXPORT =
11 type atp_format = ATP_Problem.atp_format
13 val theorems_mentioned_in_proof_term :
14 string list option -> thm -> string list
15 val generate_tptp_graph_file_for_theory :
16 Proof.context -> theory -> string -> unit
17 val generate_tptp_inference_file_for_theory :
18 Proof.context -> theory -> atp_format -> string -> string -> unit
21 structure ATP_Theory_Export : ATP_THEORY_EXPORT =
26 open ATP_Problem_Generate
29 val fact_name_of = prefix fact_prefix o ascii_of
32 let val ctxt = Proof_Context.init_global thy in
33 Sledgehammer_Filter.all_facts ctxt false
34 Symtab.empty true [] []
35 (Sledgehammer_Filter.clasimpset_rule_table_of ctxt)
36 |> filter (curry (op =) @{typ bool} o fastype_of
37 o Object_Logic.atomize_term thy o prop_of o snd)
40 (* FIXME: Similar yet different code in "mirabelle.ML". The code here has a few
41 fixes that seem to be missing over there; or maybe the two code portions are
42 not doing the same? *)
43 fun fold_body_thms thm_name all_names f =
45 fun app n (PBody {thms, ...}) =
46 thms |> fold (fn (_, (name, prop, body)) => fn x =>
48 val body' = Future.join body
50 n + (if name = "" orelse
51 (is_some all_names andalso
52 not (member (op =) (the all_names) name)) orelse
53 (* uncommon case where the proved theorem occurs twice
54 (e.g., "Transitive_Closure.trancl_into_trancl") *)
55 n = 1 andalso name = thm_name then
59 val x' = x |> n' <= 1 ? app n' body'
60 in (x' |> n = 1 ? f (name, prop, body')) end)
63 fun theorems_mentioned_in_proof_term all_names thm =
65 fun collect (s, _, _) = if s <> "" then insert (op =) s else I
67 [] |> fold_body_thms (Thm.get_name_hint thm) all_names collect
68 [Thm.proof_body_of thm]
72 fun interesting_const_names ctxt =
73 let val thy = Proof_Context.theory_of ctxt in
74 Sledgehammer_Filter.const_names_in_fact thy
75 (Sledgehammer_Provers.is_built_in_const_for_prover ctxt eN)
78 fun generate_tptp_graph_file_for_theory ctxt thy file_name =
80 val path = file_name |> Path.explode
81 val _ = File.write path ""
82 val axioms = Theory.all_axioms_of thy |> map fst
85 val name = Thm.get_name_hint thm
87 "[" ^ Thm.legacy_get_kind thm ^ "] " ^
88 (if member (op =) axioms name then "A" else "T") ^ " " ^
89 prefix fact_prefix (ascii_of name) ^ ": " ^
90 commas (theorems_mentioned_in_proof_term NONE thm) ^ "; " ^
91 commas (map (prefix const_prefix o ascii_of)
92 (interesting_const_names ctxt (Thm.prop_of thm))) ^ " \n"
93 in File.append path s end
94 val thms = facts_of thy |> map snd
95 val _ = map do_thm thms
98 fun inference_term [] = NONE
101 [ATerm ("isabelle", []),
102 ATerm (tptp_empty_list, []),
103 ATerm (tptp_empty_list, map (fn s => ATerm (s, [])) ss)])
105 fun inference infers ident =
106 these (AList.lookup (op =) infers ident) |> inference_term
107 fun add_inferences_to_problem_line infers
108 (Formula (ident, Axiom, phi, NONE, tms)) =
109 Formula (ident, Lemma, phi, inference infers ident, tms)
110 | add_inferences_to_problem_line _ line = line
111 fun add_inferences_to_problem infers =
112 map (apsnd (map (add_inferences_to_problem_line infers)))
114 fun ident_of_problem_line (Decl (ident, _, _)) = ident
115 | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
117 fun run_some_atp ctxt format problem =
119 val thy = Proof_Context.theory_of ctxt
120 val prob_file = File.tmp_path (Path.explode "prob.tptp")
121 val {exec, arguments, proof_delims, known_failures, ...} =
122 get_atp thy (case format of DFG _ => spassN | _ => eN)
123 val _ = problem |> lines_for_atp_problem format (K [])
124 |> File.write_list prob_file
126 File.shell_path (Path.explode (getenv (fst exec) ^ "/" ^ snd exec)) ^
127 " " ^ arguments ctxt false "" (seconds 1.0) (K []) ^ " " ^
128 File.shell_path prob_file
130 TimeLimit.timeLimit (seconds 0.3) Isabelle_System.bash_output command
132 |> extract_tstplike_proof_and_outcome false true proof_delims
136 handle TimeLimit.TimeOut => SOME TimedOut
138 val likely_tautology_prefixes =
139 [@{theory HOL}, @{theory Meson}, @{theory ATP}, @{theory Metis}]
140 |> map (fact_name_of o Context.theory_name)
142 fun is_problem_line_tautology ctxt format (Formula (ident, _, phi, _, _)) =
143 exists (fn prefix => String.isPrefix prefix ident)
144 likely_tautology_prefixes andalso
145 is_none (run_some_atp ctxt format
146 [(factsN, [Formula (ident, Conjecture, phi, NONE, [])])])
147 | is_problem_line_tautology _ _ _ = false
149 fun order_facts ord = sort (ord o pairself ident_of_problem_line)
150 fun order_problem_facts _ [] = []
151 | order_problem_facts ord ((heading, lines) :: problem) =
152 if heading = factsN then (heading, order_facts ord lines) :: problem
153 else (heading, lines) :: order_problem_facts ord problem
155 (* A fairly random selection of types used for monomorphizing. *)
157 [@{typ nat}, HOLogic.intT, HOLogic.realT, @{typ "nat => bool"}, @{typ bool},
160 fun ground_type_for_tvar _ [] tvar =
161 raise TYPE ("ground_type_for_sorts", [TVar tvar], [])
162 | ground_type_for_tvar thy (T :: Ts) tvar =
163 if can (Sign.typ_match thy (TVar tvar, T)) Vartab.empty then T
164 else ground_type_for_tvar thy Ts tvar
166 fun monomorphize_term ctxt t =
167 let val thy = Proof_Context.theory_of ctxt in
168 t |> map_types (map_type_tvar (ground_type_for_tvar thy ground_types))
169 handle TYPE _ => @{prop True}
172 fun generate_tptp_inference_file_for_theory ctxt thy format type_enc file_name =
174 val type_enc = type_enc |> type_enc_from_string Strict
175 |> adjust_type_enc format
176 val mono = polymorphism_of_type_enc type_enc <> Polymorphic
177 val path = file_name |> Path.explode
178 val _ = File.write path ""
179 val facts = facts_of thy
182 |> map (fn ((_, loc), th) =>
183 ((Thm.get_name_hint th, loc),
184 th |> prop_of |> mono ? monomorphize_term ctxt))
185 |> prepare_atp_problem ctxt format Axiom Axiom type_enc true combsN false
186 false true [] @{prop False}
190 |> map (apsnd (filter_out (is_problem_line_tautology ctxt format)))
191 val all_names = facts |> map (Thm.get_name_hint o snd)
193 facts |> map (fn (_, th) =>
194 (fact_name_of (Thm.get_name_hint th),
195 theorems_mentioned_in_proof_term (SOME all_names) th))
196 val all_atp_problem_names =
197 atp_problem |> maps (map ident_of_problem_line o snd)
199 infers |> filter (member (op =) all_atp_problem_names o fst)
200 |> map (apsnd (filter (member (op =) all_atp_problem_names)))
203 |> fold (String_Graph.new_node o rpair ()) all_atp_problem_names
204 |> fold (fn (to, froms) =>
205 fold (fn from => String_Graph.add_edge (from, to)) froms)
207 |> String_Graph.topological_order
210 |> fold (Symtab.insert (op =))
211 (ordered_names ~~ (1 upto length ordered_names))
212 val name_ord = int_ord o pairself (the o Symtab.lookup order_tab)
215 |> (case format of DFG _ => I | _ => add_inferences_to_problem infers)
216 |> order_problem_facts name_ord
217 val ss = lines_for_atp_problem format (K []) atp_problem
218 val _ = app (File.append path) ss