blanchet@49263
|
1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_filter_mash.ML
|
blanchet@49263
|
2 |
Author: Jasmin Blanchette, TU Muenchen
|
blanchet@49263
|
3 |
|
blanchet@49263
|
4 |
Sledgehammer's machine-learning-based relevance filter (MaSh).
|
blanchet@49263
|
5 |
*)
|
blanchet@49263
|
6 |
|
blanchet@49263
|
7 |
signature SLEDGEHAMMER_FILTER_MASH =
|
blanchet@49263
|
8 |
sig
|
blanchet@49266
|
9 |
type status = ATP_Problem_Generate.status
|
blanchet@49266
|
10 |
type stature = ATP_Problem_Generate.stature
|
blanchet@49311
|
11 |
type fact = Sledgehammer_Fact.fact
|
blanchet@49311
|
12 |
type fact_override = Sledgehammer_Fact.fact_override
|
blanchet@49266
|
13 |
type params = Sledgehammer_Provers.params
|
blanchet@49303
|
14 |
type relevance_fudge = Sledgehammer_Provers.relevance_fudge
|
blanchet@49266
|
15 |
type prover_result = Sledgehammer_Provers.prover_result
|
blanchet@49266
|
16 |
|
blanchet@49323
|
17 |
val trace : bool Config.T
|
blanchet@49329
|
18 |
val meshN : string
|
blanchet@49329
|
19 |
val iterN : string
|
blanchet@49329
|
20 |
val mashN : string
|
blanchet@49329
|
21 |
val fact_filters : string list
|
blanchet@49318
|
22 |
val escape_meta : string -> string
|
blanchet@49318
|
23 |
val escape_metas : string list -> string
|
blanchet@49323
|
24 |
val unescape_meta : string -> string
|
blanchet@49323
|
25 |
val unescape_metas : string -> string list
|
blanchet@49326
|
26 |
val extract_query : string -> string * string list
|
blanchet@49327
|
27 |
val suggested_facts : string list -> fact list -> fact list
|
blanchet@49328
|
28 |
val mesh_facts : int -> (fact list * int option) list -> fact list
|
blanchet@49330
|
29 |
val is_likely_tautology : thm -> bool
|
blanchet@49330
|
30 |
val is_too_meta : thm -> bool
|
blanchet@49266
|
31 |
val theory_ord : theory * theory -> order
|
blanchet@49266
|
32 |
val thm_ord : thm * thm -> order
|
blanchet@49317
|
33 |
val features_of : theory -> status -> term list -> string list
|
blanchet@49331
|
34 |
val isabelle_dependencies_of : unit Symtab.table -> thm -> string list
|
blanchet@49266
|
35 |
val goal_of_thm : theory -> thm -> thm
|
blanchet@49311
|
36 |
val run_prover : Proof.context -> params -> fact list -> thm -> prover_result
|
blanchet@49323
|
37 |
val mash_RESET : Proof.context -> unit
|
blanchet@49331
|
38 |
val mash_INIT :
|
blanchet@49331
|
39 |
Proof.context -> bool
|
blanchet@49331
|
40 |
-> (string * string list * string list * string list) list -> unit
|
blanchet@49323
|
41 |
val mash_ADD :
|
blanchet@49331
|
42 |
Proof.context -> bool
|
blanchet@49331
|
43 |
-> (string * string list * string list * string list) list -> unit
|
blanchet@49331
|
44 |
val mash_QUERY :
|
blanchet@49331
|
45 |
Proof.context -> bool -> string list * string list -> string list
|
blanchet@49323
|
46 |
val mash_reset : Proof.context -> unit
|
blanchet@49323
|
47 |
val mash_can_suggest_facts : Proof.context -> bool
|
blanchet@49313
|
48 |
val mash_suggest_facts :
|
blanchet@49326
|
49 |
Proof.context -> params -> string -> term list -> term -> fact list
|
blanchet@49326
|
50 |
-> fact list
|
blanchet@49331
|
51 |
val mash_learn_thy : Proof.context -> params -> theory -> real -> unit
|
blanchet@49331
|
52 |
val mash_learn_proof :
|
blanchet@49331
|
53 |
Proof.context -> params -> term -> thm list -> fact list -> unit
|
blanchet@49303
|
54 |
val relevant_facts :
|
blanchet@49307
|
55 |
Proof.context -> params -> string -> int -> fact_override -> term list
|
blanchet@49311
|
56 |
-> term -> fact list -> fact list
|
blanchet@49263
|
57 |
end;
|
blanchet@49263
|
58 |
|
blanchet@49263
|
59 |
structure Sledgehammer_Filter_MaSh : SLEDGEHAMMER_FILTER_MASH =
|
blanchet@49263
|
60 |
struct
|
blanchet@49264
|
61 |
|
blanchet@49266
|
62 |
open ATP_Util
|
blanchet@49266
|
63 |
open ATP_Problem_Generate
|
blanchet@49266
|
64 |
open Sledgehammer_Util
|
blanchet@49266
|
65 |
open Sledgehammer_Fact
|
blanchet@49266
|
66 |
open Sledgehammer_Filter_Iter
|
blanchet@49266
|
67 |
open Sledgehammer_Provers
|
blanchet@49266
|
68 |
|
blanchet@49323
|
69 |
val trace =
|
blanchet@49323
|
70 |
Attrib.setup_config_bool @{binding sledgehammer_filter_mash_trace} (K false)
|
blanchet@49323
|
71 |
fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else ()
|
blanchet@49323
|
72 |
|
blanchet@49329
|
73 |
val meshN = "mesh"
|
blanchet@49329
|
74 |
val iterN = "iter"
|
blanchet@49329
|
75 |
val mashN = "mash"
|
blanchet@49329
|
76 |
|
blanchet@49329
|
77 |
val fact_filters = [meshN, iterN, mashN]
|
blanchet@49329
|
78 |
|
blanchet@49329
|
79 |
fun mash_home () = getenv "MASH_HOME"
|
blanchet@49329
|
80 |
fun mash_state_dir () =
|
blanchet@49324
|
81 |
getenv "ISABELLE_HOME_USER" ^ "/mash"
|
blanchet@49331
|
82 |
|> tap (Isabelle_System.mkdir o Path.explode)
|
blanchet@49329
|
83 |
fun mash_state_path () = mash_state_dir () ^ "/state" |> Path.explode
|
blanchet@49266
|
84 |
|
blanchet@49266
|
85 |
(*** Isabelle helpers ***)
|
blanchet@49266
|
86 |
|
blanchet@49323
|
87 |
fun meta_char c =
|
blanchet@49266
|
88 |
if Char.isAlphaNum c orelse c = #"_" orelse c = #"." orelse c = #"(" orelse
|
blanchet@49266
|
89 |
c = #")" orelse c = #"," then
|
blanchet@49266
|
90 |
String.str c
|
blanchet@49266
|
91 |
else
|
blanchet@49266
|
92 |
(* fixed width, in case more digits follow *)
|
blanchet@49266
|
93 |
"\\" ^ stringN_of_int 3 (Char.ord c)
|
blanchet@49266
|
94 |
|
blanchet@49323
|
95 |
fun unmeta_chars accum [] = String.implode (rev accum)
|
blanchet@49323
|
96 |
| unmeta_chars accum (#"\\" :: d1 :: d2 :: d3 :: cs) =
|
blanchet@49323
|
97 |
(case Int.fromString (String.implode [d1, d2, d3]) of
|
blanchet@49323
|
98 |
SOME n => unmeta_chars (Char.chr n :: accum) cs
|
blanchet@49323
|
99 |
| NONE => "" (* error *))
|
blanchet@49323
|
100 |
| unmeta_chars _ (#"\\" :: _) = "" (* error *)
|
blanchet@49323
|
101 |
| unmeta_chars accum (c :: cs) = unmeta_chars (c :: accum) cs
|
blanchet@49323
|
102 |
|
blanchet@49323
|
103 |
val escape_meta = String.translate meta_char
|
blanchet@49318
|
104 |
val escape_metas = map escape_meta #> space_implode " "
|
blanchet@49330
|
105 |
val unescape_meta = String.explode #> unmeta_chars []
|
blanchet@49330
|
106 |
val unescape_metas =
|
blanchet@49330
|
107 |
space_explode " " #> filter_out (curry (op =) "") #> map unescape_meta
|
blanchet@49266
|
108 |
|
blanchet@49326
|
109 |
fun extract_query line =
|
blanchet@49326
|
110 |
case space_explode ":" line of
|
blanchet@49330
|
111 |
[goal_name, suggs] => (unescape_meta goal_name, unescape_metas suggs)
|
blanchet@49327
|
112 |
| _ => ("", [])
|
blanchet@49326
|
113 |
|
blanchet@49326
|
114 |
fun find_suggested facts sugg =
|
blanchet@49326
|
115 |
find_first (fn (_, th) => Thm.get_name_hint th = sugg) facts
|
blanchet@49326
|
116 |
fun suggested_facts suggs facts = map_filter (find_suggested facts) suggs
|
blanchet@49326
|
117 |
|
blanchet@49328
|
118 |
fun sum_avg n xs =
|
blanchet@49328
|
119 |
fold (Integer.add o Integer.mult n) xs 0 div (length xs)
|
blanchet@49328
|
120 |
|
blanchet@49329
|
121 |
fun mesh_facts max_facts [(facts, _)] = facts |> take max_facts
|
blanchet@49329
|
122 |
| mesh_facts max_facts mess =
|
blanchet@49329
|
123 |
let
|
blanchet@49329
|
124 |
val n = length mess
|
blanchet@49329
|
125 |
val fact_eq = Thm.eq_thm o pairself snd
|
blanchet@49329
|
126 |
fun score_in fact (facts, def) =
|
blanchet@49329
|
127 |
case find_index (curry fact_eq fact) facts of
|
blanchet@49329
|
128 |
~1 => def
|
blanchet@49329
|
129 |
| j => SOME j
|
blanchet@49329
|
130 |
fun score_of fact = mess |> map_filter (score_in fact) |> sum_avg n
|
blanchet@49329
|
131 |
val facts = fold (union fact_eq o take max_facts o fst) mess []
|
blanchet@49329
|
132 |
in
|
blanchet@49329
|
133 |
facts |> map (`score_of) |> sort (int_ord o pairself fst) |> map snd
|
blanchet@49329
|
134 |
|> take max_facts
|
blanchet@49329
|
135 |
end
|
blanchet@49327
|
136 |
|
blanchet@49318
|
137 |
val thy_feature_prefix = "y_"
|
blanchet@49266
|
138 |
|
blanchet@49318
|
139 |
val thy_feature_name_of = prefix thy_feature_prefix
|
blanchet@49318
|
140 |
val const_name_of = prefix const_prefix
|
blanchet@49318
|
141 |
val type_name_of = prefix type_const_prefix
|
blanchet@49318
|
142 |
val class_name_of = prefix class_prefix
|
blanchet@49266
|
143 |
|
blanchet@49266
|
144 |
local
|
blanchet@49266
|
145 |
|
blanchet@49266
|
146 |
fun has_bool @{typ bool} = true
|
blanchet@49266
|
147 |
| has_bool (Type (_, Ts)) = exists has_bool Ts
|
blanchet@49266
|
148 |
| has_bool _ = false
|
blanchet@49266
|
149 |
|
blanchet@49266
|
150 |
fun has_fun (Type (@{type_name fun}, _)) = true
|
blanchet@49266
|
151 |
| has_fun (Type (_, Ts)) = exists has_fun Ts
|
blanchet@49266
|
152 |
| has_fun _ = false
|
blanchet@49266
|
153 |
|
blanchet@49266
|
154 |
val is_conn = member (op =)
|
blanchet@49266
|
155 |
[@{const_name Trueprop}, @{const_name HOL.conj}, @{const_name HOL.disj},
|
blanchet@49266
|
156 |
@{const_name HOL.implies}, @{const_name Not},
|
blanchet@49266
|
157 |
@{const_name All}, @{const_name Ex}, @{const_name Ball}, @{const_name Bex},
|
blanchet@49266
|
158 |
@{const_name HOL.eq}]
|
blanchet@49266
|
159 |
|
blanchet@49266
|
160 |
val has_bool_arg_const =
|
blanchet@49266
|
161 |
exists_Const (fn (c, T) =>
|
blanchet@49266
|
162 |
not (is_conn c) andalso exists has_bool (binder_types T))
|
blanchet@49266
|
163 |
|
blanchet@49331
|
164 |
fun higher_inst_const thy (s, T) =
|
blanchet@49266
|
165 |
case binder_types T of
|
blanchet@49266
|
166 |
[] => false
|
blanchet@49331
|
167 |
| Ts => length (binder_types (Sign.the_const_type thy s)) <> length Ts
|
blanchet@49331
|
168 |
handle TYPE _ => false
|
blanchet@49266
|
169 |
|
blanchet@49266
|
170 |
val binders = [@{const_name All}, @{const_name Ex}]
|
blanchet@49266
|
171 |
|
blanchet@49266
|
172 |
in
|
blanchet@49266
|
173 |
|
blanchet@49266
|
174 |
fun is_fo_term thy t =
|
blanchet@49266
|
175 |
let
|
blanchet@49266
|
176 |
val t =
|
blanchet@49266
|
177 |
t |> Envir.beta_eta_contract
|
blanchet@49266
|
178 |
|> transform_elim_prop
|
blanchet@49266
|
179 |
|> Object_Logic.atomize_term thy
|
blanchet@49266
|
180 |
in
|
blanchet@49266
|
181 |
Term.is_first_order binders t andalso
|
blanchet@49266
|
182 |
not (exists_subterm (fn Var (_, T) => has_bool T orelse has_fun T
|
blanchet@49266
|
183 |
| _ => false) t orelse
|
blanchet@49266
|
184 |
has_bool_arg_const t orelse exists_Const (higher_inst_const thy) t)
|
blanchet@49266
|
185 |
end
|
blanchet@49266
|
186 |
|
blanchet@49266
|
187 |
end
|
blanchet@49266
|
188 |
|
blanchet@49317
|
189 |
fun interesting_terms_types_and_classes term_max_depth type_max_depth ts =
|
blanchet@49266
|
190 |
let
|
blanchet@49266
|
191 |
val bad_types = [@{type_name prop}, @{type_name bool}, @{type_name fun}]
|
blanchet@49266
|
192 |
val bad_consts = atp_widely_irrelevant_consts
|
blanchet@49319
|
193 |
fun add_classes @{sort type} = I
|
blanchet@49319
|
194 |
| add_classes S = union (op =) (map class_name_of S)
|
blanchet@49266
|
195 |
fun do_add_type (Type (s, Ts)) =
|
blanchet@49266
|
196 |
(not (member (op =) bad_types s) ? insert (op =) (type_name_of s))
|
blanchet@49266
|
197 |
#> fold do_add_type Ts
|
blanchet@49319
|
198 |
| do_add_type (TFree (_, S)) = add_classes S
|
blanchet@49319
|
199 |
| do_add_type (TVar (_, S)) = add_classes S
|
blanchet@49266
|
200 |
fun add_type T = type_max_depth >= 0 ? do_add_type T
|
blanchet@49266
|
201 |
fun mk_app s args =
|
blanchet@49266
|
202 |
if member (op <>) args "" then s ^ "(" ^ space_implode "," args ^ ")"
|
blanchet@49266
|
203 |
else s
|
blanchet@49266
|
204 |
fun patternify ~1 _ = ""
|
blanchet@49266
|
205 |
| patternify depth t =
|
blanchet@49266
|
206 |
case strip_comb t of
|
blanchet@49266
|
207 |
(Const (s, _), args) =>
|
blanchet@49266
|
208 |
mk_app (const_name_of s) (map (patternify (depth - 1)) args)
|
blanchet@49266
|
209 |
| _ => ""
|
blanchet@49266
|
210 |
fun add_term_patterns ~1 _ = I
|
blanchet@49266
|
211 |
| add_term_patterns depth t =
|
blanchet@49266
|
212 |
insert (op =) (patternify depth t)
|
blanchet@49266
|
213 |
#> add_term_patterns (depth - 1) t
|
blanchet@49266
|
214 |
val add_term = add_term_patterns term_max_depth
|
blanchet@49266
|
215 |
fun add_patterns t =
|
blanchet@49266
|
216 |
let val (head, args) = strip_comb t in
|
blanchet@49266
|
217 |
(case head of
|
blanchet@49266
|
218 |
Const (s, T) =>
|
blanchet@49266
|
219 |
not (member (op =) bad_consts s) ? (add_term t #> add_type T)
|
blanchet@49266
|
220 |
| Free (_, T) => add_type T
|
blanchet@49266
|
221 |
| Var (_, T) => add_type T
|
blanchet@49266
|
222 |
| Abs (_, T, body) => add_type T #> add_patterns body
|
blanchet@49266
|
223 |
| _ => I)
|
blanchet@49266
|
224 |
#> fold add_patterns args
|
blanchet@49266
|
225 |
end
|
blanchet@49317
|
226 |
in [] |> fold add_patterns ts |> sort string_ord end
|
blanchet@49266
|
227 |
|
blanchet@49266
|
228 |
fun is_likely_tautology th =
|
blanchet@49317
|
229 |
null (interesting_terms_types_and_classes 0 ~1 [prop_of th]) andalso
|
blanchet@49266
|
230 |
not (Thm.eq_thm_prop (@{thm ext}, th))
|
blanchet@49266
|
231 |
|
blanchet@49330
|
232 |
(* ### FIXME: optimize *)
|
blanchet@49330
|
233 |
fun is_too_meta th =
|
blanchet@49330
|
234 |
fastype_of (Object_Logic.atomize_term (theory_of_thm th) (prop_of th))
|
blanchet@49330
|
235 |
<> @{typ bool}
|
blanchet@49266
|
236 |
|
blanchet@49266
|
237 |
fun theory_ord p =
|
blanchet@49330
|
238 |
if Theory.eq_thy p then
|
blanchet@49330
|
239 |
EQUAL
|
blanchet@49330
|
240 |
else if Theory.subthy p then
|
blanchet@49330
|
241 |
LESS
|
blanchet@49330
|
242 |
else if Theory.subthy (swap p) then
|
blanchet@49330
|
243 |
GREATER
|
blanchet@49330
|
244 |
else case int_ord (pairself (length o Theory.ancestors_of) p) of
|
blanchet@49330
|
245 |
EQUAL => string_ord (pairself Context.theory_name p)
|
blanchet@49330
|
246 |
| order => order
|
blanchet@49266
|
247 |
|
blanchet@49266
|
248 |
val thm_ord = theory_ord o pairself theory_of_thm
|
blanchet@49266
|
249 |
|
blanchet@49266
|
250 |
fun is_exists (s, _) = (s = @{const_name Ex} orelse s = @{const_name Ex1})
|
blanchet@49266
|
251 |
|
blanchet@49312
|
252 |
val term_max_depth = 1
|
blanchet@49312
|
253 |
val type_max_depth = 1
|
blanchet@49266
|
254 |
|
blanchet@49266
|
255 |
(* TODO: Generate type classes for types? *)
|
blanchet@49317
|
256 |
fun features_of thy status ts =
|
blanchet@49318
|
257 |
thy_feature_name_of (Context.theory_name thy) ::
|
blanchet@49317
|
258 |
interesting_terms_types_and_classes term_max_depth type_max_depth ts
|
blanchet@49317
|
259 |
|> exists (not o is_lambda_free) ts ? cons "lambdas"
|
blanchet@49317
|
260 |
|> exists (exists_Const is_exists) ts ? cons "skolems"
|
blanchet@49317
|
261 |
|> exists (not o is_fo_term thy) ts ? cons "ho"
|
blanchet@49317
|
262 |
|> (case status of
|
blanchet@49317
|
263 |
General => I
|
blanchet@49317
|
264 |
| Induction => cons "induction"
|
blanchet@49317
|
265 |
| Intro => cons "intro"
|
blanchet@49317
|
266 |
| Inductive => cons "inductive"
|
blanchet@49317
|
267 |
| Elim => cons "elim"
|
blanchet@49317
|
268 |
| Simp => cons "simp"
|
blanchet@49317
|
269 |
| Def => cons "def")
|
blanchet@49266
|
270 |
|
blanchet@49266
|
271 |
fun isabelle_dependencies_of all_facts =
|
blanchet@49318
|
272 |
thms_in_proof (SOME all_facts) #> sort string_ord
|
blanchet@49266
|
273 |
|
blanchet@49266
|
274 |
val freezeT = Type.legacy_freeze_type
|
blanchet@49266
|
275 |
|
blanchet@49266
|
276 |
fun freeze (t $ u) = freeze t $ freeze u
|
blanchet@49266
|
277 |
| freeze (Abs (s, T, t)) = Abs (s, freezeT T, freeze t)
|
blanchet@49266
|
278 |
| freeze (Var ((s, _), T)) = Free (s, freezeT T)
|
blanchet@49266
|
279 |
| freeze (Const (s, T)) = Const (s, freezeT T)
|
blanchet@49266
|
280 |
| freeze (Free (s, T)) = Free (s, freezeT T)
|
blanchet@49266
|
281 |
| freeze t = t
|
blanchet@49266
|
282 |
|
blanchet@49266
|
283 |
fun goal_of_thm thy = prop_of #> freeze #> cterm_of thy #> Goal.init
|
blanchet@49266
|
284 |
|
blanchet@49266
|
285 |
fun run_prover ctxt (params as {provers, ...}) facts goal =
|
blanchet@49266
|
286 |
let
|
blanchet@49266
|
287 |
val problem =
|
blanchet@49266
|
288 |
{state = Proof.init ctxt, goal = goal, subgoal = 1, subgoal_count = 1,
|
blanchet@49304
|
289 |
facts = facts |> map (apfst (apfst (fn name => name ())))
|
blanchet@49304
|
290 |
|> map Sledgehammer_Provers.Untranslated_Fact}
|
blanchet@49266
|
291 |
val prover =
|
blanchet@49266
|
292 |
Sledgehammer_Minimize.get_minimizing_prover ctxt
|
blanchet@49266
|
293 |
Sledgehammer_Provers.Normal (hd provers)
|
blanchet@49266
|
294 |
in prover params (K (K (K ""))) problem end
|
blanchet@49266
|
295 |
|
blanchet@49266
|
296 |
|
blanchet@49317
|
297 |
(*** Low-level communication with MaSh ***)
|
blanchet@49317
|
298 |
|
blanchet@49331
|
299 |
val max_preds = 500
|
blanchet@49331
|
300 |
|
blanchet@49331
|
301 |
fun mash_info overlord =
|
blanchet@49331
|
302 |
if overlord then (getenv "ISABELLE_HOME_USER", "")
|
blanchet@49331
|
303 |
else (getenv "ISABELLE_TMP", serial_string ())
|
blanchet@49331
|
304 |
|
blanchet@49331
|
305 |
fun run_mash ctxt (temp_dir, serial) core =
|
blanchet@49326
|
306 |
let
|
blanchet@49331
|
307 |
val log_file = temp_dir ^ "/mash_log" ^ serial
|
blanchet@49331
|
308 |
val err_file = temp_dir ^ "/mash_err" ^ serial
|
blanchet@49326
|
309 |
val command =
|
blanchet@49331
|
310 |
mash_home () ^ "/mash.py --quiet --outputDir " ^ mash_state_dir () ^
|
blanchet@49331
|
311 |
" --log " ^ log_file ^ " " ^ core ^ " 2>&1 > " ^ err_file
|
blanchet@49331
|
312 |
in
|
blanchet@49331
|
313 |
trace_msg ctxt (fn () => "Running " ^ command);
|
blanchet@49331
|
314 |
Isabelle_System.bash command; ()
|
blanchet@49331
|
315 |
end
|
blanchet@49326
|
316 |
|
blanchet@49331
|
317 |
fun write_file write file =
|
blanchet@49331
|
318 |
let val path = Path.explode file in
|
blanchet@49331
|
319 |
File.write path ""; write (File.append path)
|
blanchet@49331
|
320 |
end
|
blanchet@49331
|
321 |
|
blanchet@49331
|
322 |
fun run_mash_init ctxt overlord write_access write_feats write_deps =
|
blanchet@49331
|
323 |
let
|
blanchet@49331
|
324 |
val info as (temp_dir, serial) = mash_info overlord
|
blanchet@49331
|
325 |
val in_dir = temp_dir ^ "/mash_init" ^ serial
|
blanchet@49331
|
326 |
|> tap (Isabelle_System.mkdir o Path.explode)
|
blanchet@49331
|
327 |
in
|
blanchet@49331
|
328 |
write_file write_access (in_dir ^ "/mash_accessibility");
|
blanchet@49331
|
329 |
write_file write_feats (in_dir ^ "/mash_features");
|
blanchet@49331
|
330 |
write_file write_deps (in_dir ^ "/mash_dependencies");
|
blanchet@49331
|
331 |
run_mash ctxt info ("--init --inputDir " ^ in_dir)
|
blanchet@49331
|
332 |
end
|
blanchet@49331
|
333 |
|
blanchet@49331
|
334 |
fun run_mash_commands ctxt overlord save write_cmds read_preds =
|
blanchet@49331
|
335 |
let
|
blanchet@49331
|
336 |
val info as (temp_dir, serial) = mash_info overlord
|
blanchet@49331
|
337 |
val cmd_file = temp_dir ^ "/mash_commands" ^ serial
|
blanchet@49331
|
338 |
val pred_file = temp_dir ^ "/mash_preds" ^ serial
|
blanchet@49331
|
339 |
in
|
blanchet@49331
|
340 |
write_file write_cmds cmd_file;
|
blanchet@49331
|
341 |
run_mash ctxt info
|
blanchet@49331
|
342 |
("--inputFile " ^ cmd_file ^ " --predictions " ^ pred_file ^
|
blanchet@49331
|
343 |
" --numberOfPredictions " ^ string_of_int max_preds ^
|
blanchet@49331
|
344 |
(if save then " --saveModel" else ""));
|
blanchet@49331
|
345 |
read_preds (fn () => File.read_lines (Path.explode pred_file))
|
blanchet@49331
|
346 |
end
|
blanchet@49331
|
347 |
|
blanchet@49331
|
348 |
fun str_of_update (name, parents, feats, deps) =
|
blanchet@49331
|
349 |
"! " ^ escape_meta name ^ ": " ^ escape_metas parents ^ "; " ^
|
blanchet@49326
|
350 |
escape_metas feats ^ "; " ^ escape_metas deps ^ "\n"
|
blanchet@49326
|
351 |
|
blanchet@49331
|
352 |
fun str_of_query (parents, feats) =
|
blanchet@49331
|
353 |
"? " ^ escape_metas parents ^ "; " ^ escape_metas feats
|
blanchet@49331
|
354 |
|
blanchet@49331
|
355 |
fun init_str_of_update get (upd as (name, _, _, _)) =
|
blanchet@49331
|
356 |
escape_meta name ^ ": " ^ escape_metas (get upd) ^ "\n"
|
blanchet@49326
|
357 |
|
blanchet@49323
|
358 |
fun mash_RESET ctxt =
|
blanchet@49329
|
359 |
let val path = mash_state_dir () |> Path.explode in
|
blanchet@49324
|
360 |
trace_msg ctxt (K "MaSh RESET");
|
blanchet@49324
|
361 |
File.fold_dir (fn file => fn () =>
|
blanchet@49324
|
362 |
File.rm (Path.append path (Path.basic file)))
|
blanchet@49324
|
363 |
path ()
|
blanchet@49324
|
364 |
end
|
blanchet@49317
|
365 |
|
blanchet@49331
|
366 |
fun mash_INIT ctxt _ [] = mash_RESET ctxt
|
blanchet@49331
|
367 |
| mash_INIT ctxt overlord upds =
|
blanchet@49331
|
368 |
(trace_msg ctxt (fn () => "MaSh INIT " ^
|
blanchet@49331
|
369 |
elide_string 1000 (space_implode " " (map #1 upds)));
|
blanchet@49331
|
370 |
run_mash_init ctxt overlord
|
blanchet@49331
|
371 |
(fn append => List.app (append o init_str_of_update #2) upds)
|
blanchet@49331
|
372 |
(fn append => List.app (append o init_str_of_update #3) upds)
|
blanchet@49331
|
373 |
(fn append => List.app (append o init_str_of_update #4) upds))
|
blanchet@49317
|
374 |
|
blanchet@49331
|
375 |
fun mash_ADD _ _ [] = ()
|
blanchet@49331
|
376 |
| mash_ADD ctxt overlord upds =
|
blanchet@49331
|
377 |
(trace_msg ctxt (fn () => "MaSh ADD " ^
|
blanchet@49331
|
378 |
elide_string 1000 (space_implode " " (map #1 upds)));
|
blanchet@49331
|
379 |
run_mash_commands ctxt overlord true
|
blanchet@49331
|
380 |
(fn append => List.app (append o str_of_update) upds) (K ()))
|
blanchet@49317
|
381 |
|
blanchet@49331
|
382 |
fun mash_QUERY ctxt overlord (query as (_, feats)) =
|
blanchet@49329
|
383 |
(trace_msg ctxt (fn () => "MaSh QUERY " ^ space_implode " " feats);
|
blanchet@49331
|
384 |
run_mash_commands ctxt overlord false
|
blanchet@49331
|
385 |
(fn append => append (str_of_query query))
|
blanchet@49331
|
386 |
(fn preds => snd (extract_query (List.last (preds ()))))
|
blanchet@49326
|
387 |
handle List.Empty => [])
|
blanchet@49317
|
388 |
|
blanchet@49317
|
389 |
|
blanchet@49266
|
390 |
(*** High-level communication with MaSh ***)
|
blanchet@49266
|
391 |
|
blanchet@49316
|
392 |
type mash_state =
|
blanchet@49331
|
393 |
{thys : bool Symtab.table,
|
blanchet@49331
|
394 |
fact_graph : unit Graph.T}
|
blanchet@49264
|
395 |
|
blanchet@49331
|
396 |
val empty_state = {thys = Symtab.empty, fact_graph = Graph.empty}
|
blanchet@49316
|
397 |
|
blanchet@49316
|
398 |
local
|
blanchet@49316
|
399 |
|
blanchet@49316
|
400 |
fun mash_load (state as (true, _)) = state
|
blanchet@49316
|
401 |
| mash_load _ =
|
blanchet@49324
|
402 |
let val path = mash_state_path () in
|
blanchet@49317
|
403 |
(true,
|
blanchet@49317
|
404 |
case try File.read_lines path of
|
blanchet@49331
|
405 |
SOME (comp_thys :: incomp_thys :: fact_lines) =>
|
blanchet@49317
|
406 |
let
|
blanchet@49331
|
407 |
fun add_thy comp thy = Symtab.update (thy, comp)
|
blanchet@49331
|
408 |
fun add_fact_line line =
|
blanchet@49331
|
409 |
case extract_query line of
|
blanchet@49331
|
410 |
("", _) => I (* shouldn't happen *)
|
blanchet@49331
|
411 |
| (name, parents) =>
|
blanchet@49331
|
412 |
Graph.default_node (name, ())
|
blanchet@49331
|
413 |
#> fold (fn par => Graph.add_edge (par, name)) parents
|
blanchet@49331
|
414 |
val thys =
|
blanchet@49331
|
415 |
Symtab.empty |> fold (add_thy true) (unescape_metas comp_thys)
|
blanchet@49331
|
416 |
|> fold (add_thy false) (unescape_metas incomp_thys)
|
blanchet@49331
|
417 |
val fact_graph = Graph.empty |> fold add_fact_line fact_lines
|
blanchet@49331
|
418 |
in {thys = thys, fact_graph = fact_graph} end
|
blanchet@49319
|
419 |
| _ => empty_state)
|
blanchet@49317
|
420 |
end
|
blanchet@49316
|
421 |
|
blanchet@49331
|
422 |
fun mash_save ({thys, fact_graph, ...} : mash_state) =
|
blanchet@49316
|
423 |
let
|
blanchet@49324
|
424 |
val path = mash_state_path ()
|
blanchet@49331
|
425 |
val thys = Symtab.dest thys
|
blanchet@49331
|
426 |
fun line_for_thys comp = AList.find (op =) thys comp |> escape_metas
|
blanchet@49331
|
427 |
fun fact_line_for name parents = name :: parents |> escape_metas
|
blanchet@49331
|
428 |
val append_fact = File.append path o suffix "\n" oo fact_line_for
|
blanchet@49316
|
429 |
in
|
blanchet@49331
|
430 |
File.write path (line_for_thys true ^ "\n" ^ line_for_thys false ^ "\n");
|
blanchet@49331
|
431 |
Graph.fold (fn (name, ((), (parents, _))) => fn () =>
|
blanchet@49331
|
432 |
append_fact name (Graph.Keys.dest parents))
|
blanchet@49331
|
433 |
fact_graph ()
|
blanchet@49316
|
434 |
end
|
blanchet@49316
|
435 |
|
blanchet@49317
|
436 |
val global_state =
|
blanchet@49319
|
437 |
Synchronized.var "Sledgehammer_Filter_MaSh.global_state" (false, empty_state)
|
blanchet@49316
|
438 |
|
blanchet@49316
|
439 |
in
|
blanchet@49316
|
440 |
|
blanchet@49321
|
441 |
fun mash_map f =
|
blanchet@49317
|
442 |
Synchronized.change global_state (mash_load ##> (f #> tap mash_save))
|
blanchet@49316
|
443 |
|
blanchet@49319
|
444 |
fun mash_get () = Synchronized.change_result global_state (mash_load #> `snd)
|
blanchet@49317
|
445 |
|
blanchet@49323
|
446 |
fun mash_reset ctxt =
|
blanchet@49317
|
447 |
Synchronized.change global_state (fn _ =>
|
blanchet@49324
|
448 |
(mash_RESET ctxt; File.write (mash_state_path ()) "";
|
blanchet@49323
|
449 |
(true, empty_state)))
|
blanchet@49316
|
450 |
|
blanchet@49316
|
451 |
end
|
blanchet@49316
|
452 |
|
blanchet@49323
|
453 |
fun mash_can_suggest_facts (_ : Proof.context) =
|
blanchet@49331
|
454 |
mash_home () <> "" andalso not (Graph.is_empty (#fact_graph (mash_get ())))
|
blanchet@49264
|
455 |
|
blanchet@49331
|
456 |
fun parents_wrt facts fact_graph =
|
blanchet@49331
|
457 |
let
|
blanchet@49331
|
458 |
val facts =
|
blanchet@49331
|
459 |
Symtab.empty
|
blanchet@49331
|
460 |
|> fold (fn (_, th) => Symtab.update (Thm.get_name_hint th, ())) facts
|
blanchet@49331
|
461 |
in fact_graph |> Graph.restrict (Symtab.defined facts) |> Graph.maximals end
|
blanchet@49331
|
462 |
|
blanchet@49331
|
463 |
fun mash_suggest_facts ctxt ({overlord, ...} : params) prover hyp_ts concl_t
|
blanchet@49331
|
464 |
facts =
|
blanchet@49316
|
465 |
let
|
blanchet@49317
|
466 |
val thy = Proof_Context.theory_of ctxt
|
blanchet@49331
|
467 |
val fact_graph = #fact_graph (mash_get ())
|
blanchet@49331
|
468 |
val parents = parents_wrt facts fact_graph
|
blanchet@49317
|
469 |
val feats = features_of thy General (concl_t :: hyp_ts)
|
blanchet@49331
|
470 |
val suggs = mash_QUERY ctxt overlord (parents, feats)
|
blanchet@49326
|
471 |
in suggested_facts suggs facts end
|
blanchet@49264
|
472 |
|
blanchet@49331
|
473 |
fun add_thys_for thy =
|
blanchet@49331
|
474 |
let fun add comp thy = Symtab.update (Context.theory_name thy, comp) in
|
blanchet@49331
|
475 |
add false thy #> fold (add true) (Theory.ancestors_of thy)
|
blanchet@49331
|
476 |
end
|
blanchet@49319
|
477 |
|
blanchet@49331
|
478 |
fun update_fact_graph ctxt (name, parents, feats, deps) (upds, graph) =
|
blanchet@49331
|
479 |
let
|
blanchet@49331
|
480 |
fun maybe_add_from from (accum as (parents, graph)) =
|
blanchet@49331
|
481 |
(from :: parents, Graph.add_edge_acyclic (from, name) graph)
|
blanchet@49331
|
482 |
handle Graph.CYCLES _ =>
|
blanchet@49331
|
483 |
(trace_msg ctxt (fn () =>
|
blanchet@49331
|
484 |
"Cycle between " ^ quote from ^ " and " ^ quote name); accum)
|
blanchet@49331
|
485 |
| Graph.UNDEF _ =>
|
blanchet@49331
|
486 |
(trace_msg ctxt (fn () => "Unknown node " ^ quote from); accum)
|
blanchet@49331
|
487 |
val graph = graph |> Graph.new_node (name, ())
|
blanchet@49331
|
488 |
val (parents, graph) = ([], graph) |> fold maybe_add_from parents
|
blanchet@49331
|
489 |
val (deps, graph) = ([], graph) |> fold maybe_add_from deps
|
blanchet@49331
|
490 |
in ((name, parents, feats, deps) :: upds, graph) end
|
blanchet@49321
|
491 |
|
blanchet@49331
|
492 |
fun mash_learn_thy ctxt ({overlord, ...} : params) thy timeout =
|
blanchet@49319
|
493 |
let
|
blanchet@49319
|
494 |
val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
|
blanchet@49330
|
495 |
val facts = all_facts_of thy css_table
|
blanchet@49331
|
496 |
val {fact_graph, ...} = mash_get ()
|
blanchet@49331
|
497 |
fun is_old (_, th) = can (Graph.get_node fact_graph) (Thm.get_name_hint th)
|
blanchet@49331
|
498 |
val new_facts = facts |> filter_out is_old |> sort (thm_ord o pairself snd)
|
blanchet@49323
|
499 |
in
|
blanchet@49323
|
500 |
if null new_facts then
|
blanchet@49323
|
501 |
()
|
blanchet@49323
|
502 |
else
|
blanchet@49319
|
503 |
let
|
blanchet@49323
|
504 |
val ths = facts |> map snd
|
blanchet@49330
|
505 |
val all_names =
|
blanchet@49330
|
506 |
ths |> filter_out (is_likely_tautology orf is_too_meta)
|
blanchet@49331
|
507 |
|> map (rpair () o Thm.get_name_hint)
|
blanchet@49331
|
508 |
|> Symtab.make
|
blanchet@49331
|
509 |
fun do_fact ((_, (_, status)), th) (parents, upds) =
|
blanchet@49323
|
510 |
let
|
blanchet@49323
|
511 |
val name = Thm.get_name_hint th
|
blanchet@49323
|
512 |
val feats = features_of thy status [prop_of th]
|
blanchet@49323
|
513 |
val deps = isabelle_dependencies_of all_names th
|
blanchet@49331
|
514 |
val upd = (name, parents, feats, deps)
|
blanchet@49326
|
515 |
in ([name], upd :: upds) end
|
blanchet@49331
|
516 |
val parents = parents_wrt facts fact_graph
|
blanchet@49331
|
517 |
val (_, upds) = (parents, []) |> fold do_fact new_facts ||> rev
|
blanchet@49331
|
518 |
fun trans {thys, fact_graph} =
|
blanchet@49331
|
519 |
let
|
blanchet@49331
|
520 |
val mash_INIT_or_ADD =
|
blanchet@49331
|
521 |
if Graph.is_empty fact_graph then mash_INIT else mash_ADD
|
blanchet@49331
|
522 |
val (upds, fact_graph) =
|
blanchet@49331
|
523 |
([], fact_graph) |> fold (update_fact_graph ctxt) upds
|
blanchet@49331
|
524 |
in
|
blanchet@49331
|
525 |
(mash_INIT_or_ADD ctxt overlord (rev upds);
|
blanchet@49331
|
526 |
{thys = thys |> add_thys_for thy,
|
blanchet@49331
|
527 |
fact_graph = fact_graph})
|
blanchet@49331
|
528 |
end
|
blanchet@49323
|
529 |
in mash_map trans end
|
blanchet@49323
|
530 |
end
|
blanchet@49319
|
531 |
|
blanchet@49331
|
532 |
fun mash_learn_proof ctxt ({overlord, ...} : params) t used_ths facts =
|
blanchet@49331
|
533 |
let
|
blanchet@49331
|
534 |
val thy = Proof_Context.theory_of ctxt
|
blanchet@49331
|
535 |
val name = ATP_Util.timestamp () (* should be fairly fresh *)
|
blanchet@49331
|
536 |
val feats = features_of thy General [t]
|
blanchet@49331
|
537 |
val deps = used_ths |> map Thm.get_name_hint
|
blanchet@49331
|
538 |
in
|
blanchet@49331
|
539 |
mash_map (fn {thys, fact_graph} =>
|
blanchet@49324
|
540 |
let
|
blanchet@49331
|
541 |
val parents = parents_wrt facts fact_graph
|
blanchet@49331
|
542 |
val upds = [(name, parents, feats, deps)]
|
blanchet@49331
|
543 |
val (upds, fact_graph) =
|
blanchet@49331
|
544 |
([], fact_graph) |> fold (update_fact_graph ctxt) upds
|
blanchet@49324
|
545 |
in
|
blanchet@49331
|
546 |
mash_ADD ctxt overlord upds;
|
blanchet@49331
|
547 |
{thys = thys, fact_graph = fact_graph}
|
blanchet@49331
|
548 |
end)
|
blanchet@49331
|
549 |
end
|
blanchet@49264
|
550 |
|
blanchet@49329
|
551 |
fun relevant_facts ctxt (params as {fact_filter, ...}) prover max_facts
|
blanchet@49313
|
552 |
({add, only, ...} : fact_override) hyp_ts concl_t facts =
|
blanchet@49329
|
553 |
if not (subset (op =) (the_list fact_filter, fact_filters)) then
|
blanchet@49329
|
554 |
error ("Unknown fact filter: " ^ quote (the fact_filter) ^ ".")
|
blanchet@49329
|
555 |
else if only then
|
blanchet@49304
|
556 |
facts
|
blanchet@49308
|
557 |
else if max_facts <= 0 then
|
blanchet@49303
|
558 |
[]
|
blanchet@49303
|
559 |
else
|
blanchet@49303
|
560 |
let
|
blanchet@49329
|
561 |
val fact_filter =
|
blanchet@49329
|
562 |
case fact_filter of
|
blanchet@49329
|
563 |
SOME ff => ff
|
blanchet@49330
|
564 |
| NONE => if mash_can_suggest_facts ctxt then meshN else iterN
|
blanchet@49303
|
565 |
val add_ths = Attrib.eval_thms ctxt add
|
blanchet@49307
|
566 |
fun prepend_facts ths accepts =
|
blanchet@49303
|
567 |
((facts |> filter (member Thm.eq_thm_prop ths o snd)) @
|
blanchet@49307
|
568 |
(accepts |> filter_out (member Thm.eq_thm_prop ths o snd)))
|
blanchet@49308
|
569 |
|> take max_facts
|
blanchet@49329
|
570 |
fun iter () =
|
blanchet@49313
|
571 |
iterative_relevant_facts ctxt params prover max_facts NONE hyp_ts
|
blanchet@49313
|
572 |
concl_t facts
|
blanchet@49329
|
573 |
|> (fn facts => (facts, SOME (length facts)))
|
blanchet@49329
|
574 |
fun mash () =
|
blanchet@49329
|
575 |
(facts |> mash_suggest_facts ctxt params prover hyp_ts concl_t, NONE)
|
blanchet@49329
|
576 |
val mess =
|
blanchet@49329
|
577 |
[] |> (if fact_filter <> mashN then cons (iter ()) else I)
|
blanchet@49329
|
578 |
|> (if fact_filter <> iterN then cons (mash ()) else I)
|
blanchet@49303
|
579 |
in
|
blanchet@49328
|
580 |
mesh_facts max_facts mess
|
blanchet@49303
|
581 |
|> not (null add_ths) ? prepend_facts add_ths
|
blanchet@49303
|
582 |
end
|
blanchet@49303
|
583 |
|
blanchet@49263
|
584 |
end;
|