1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_fact_filter.ML |
|
2 Author: Jia Meng, Cambridge University Computer Laboratory and NICTA |
|
3 Author: Jasmin Blanchette, TU Muenchen |
|
4 *) |
|
5 |
|
6 signature SLEDGEHAMMER_FACT_FILTER = |
|
7 sig |
|
8 datatype locality = General | Intro | Elim | Simp | Local | Chained |
|
9 |
|
10 type relevance_override = |
|
11 {add: Facts.ref list, |
|
12 del: Facts.ref list, |
|
13 only: bool} |
|
14 |
|
15 val trace : bool Unsynchronized.ref |
|
16 val worse_irrel_freq : real Unsynchronized.ref |
|
17 val higher_order_irrel_weight : real Unsynchronized.ref |
|
18 val abs_rel_weight : real Unsynchronized.ref |
|
19 val abs_irrel_weight : real Unsynchronized.ref |
|
20 val skolem_irrel_weight : real Unsynchronized.ref |
|
21 val intro_bonus : real Unsynchronized.ref |
|
22 val elim_bonus : real Unsynchronized.ref |
|
23 val simp_bonus : real Unsynchronized.ref |
|
24 val local_bonus : real Unsynchronized.ref |
|
25 val chained_bonus : real Unsynchronized.ref |
|
26 val max_imperfect : real Unsynchronized.ref |
|
27 val max_imperfect_exp : real Unsynchronized.ref |
|
28 val threshold_divisor : real Unsynchronized.ref |
|
29 val ridiculous_threshold : real Unsynchronized.ref |
|
30 val name_thm_pairs_from_ref : |
|
31 Proof.context -> unit Symtab.table -> thm list -> Facts.ref |
|
32 -> ((string * locality) * thm) list |
|
33 val relevant_facts : |
|
34 Proof.context -> bool -> real * real -> int -> bool -> relevance_override |
|
35 -> thm list -> term list -> term -> ((string * locality) * thm) list |
|
36 end; |
|
37 |
|
38 structure Sledgehammer_Fact_Filter : SLEDGEHAMMER_FACT_FILTER = |
|
39 struct |
|
40 |
|
41 open Sledgehammer_Util |
|
42 |
|
43 val trace = Unsynchronized.ref false |
|
44 fun trace_msg msg = if !trace then tracing (msg ()) else () |
|
45 |
|
46 (* experimental feature *) |
|
47 val term_patterns = false |
|
48 |
|
49 val respect_no_atp = true |
|
50 |
|
51 datatype locality = General | Intro | Elim | Simp | Local | Chained |
|
52 |
|
53 type relevance_override = |
|
54 {add: Facts.ref list, |
|
55 del: Facts.ref list, |
|
56 only: bool} |
|
57 |
|
58 val sledgehammer_prefix = "Sledgehammer" ^ Long_Name.separator |
|
59 |
|
60 fun repair_name reserved multi j name = |
|
61 (name |> Symtab.defined reserved name ? quote) ^ |
|
62 (if multi then "(" ^ Int.toString j ^ ")" else "") |
|
63 |
|
64 fun name_thm_pairs_from_ref ctxt reserved chained_ths xref = |
|
65 let |
|
66 val ths = ProofContext.get_fact ctxt xref |
|
67 val name = Facts.string_of_ref xref |
|
68 val multi = length ths > 1 |
|
69 in |
|
70 (ths, (1, [])) |
|
71 |-> fold (fn th => fn (j, rest) => |
|
72 (j + 1, ((repair_name reserved multi j name, |
|
73 if member Thm.eq_thm chained_ths th then Chained |
|
74 else General), th) :: rest)) |
|
75 |> snd |
|
76 end |
|
77 |
|
78 (***************************************************************) |
|
79 (* Relevance Filtering *) |
|
80 (***************************************************************) |
|
81 |
|
82 (*** constants with types ***) |
|
83 |
|
84 fun order_of_type (Type (@{type_name fun}, [T1, @{typ bool}])) = |
|
85 order_of_type T1 (* cheat: pretend sets are first-order *) |
|
86 | order_of_type (Type (@{type_name fun}, [T1, T2])) = |
|
87 Int.max (order_of_type T1 + 1, order_of_type T2) |
|
88 | order_of_type (Type (_, Ts)) = fold (Integer.max o order_of_type) Ts 0 |
|
89 | order_of_type _ = 0 |
|
90 |
|
91 (* An abstraction of Isabelle types and first-order terms *) |
|
92 datatype pattern = PVar | PApp of string * pattern list |
|
93 datatype ptype = PType of int * pattern list |
|
94 |
|
95 fun string_for_pattern PVar = "_" |
|
96 | string_for_pattern (PApp (s, ps)) = |
|
97 if null ps then s else s ^ string_for_patterns ps |
|
98 and string_for_patterns ps = "(" ^ commas (map string_for_pattern ps) ^ ")" |
|
99 fun string_for_ptype (PType (_, ps)) = string_for_patterns ps |
|
100 |
|
101 (*Is the second type an instance of the first one?*) |
|
102 fun match_pattern (PVar, _) = true |
|
103 | match_pattern (PApp _, PVar) = false |
|
104 | match_pattern (PApp (s, ps), PApp (t, qs)) = |
|
105 s = t andalso match_patterns (ps, qs) |
|
106 and match_patterns (_, []) = true |
|
107 | match_patterns ([], _) = false |
|
108 | match_patterns (p :: ps, q :: qs) = |
|
109 match_pattern (p, q) andalso match_patterns (ps, qs) |
|
110 fun match_ptype (PType (_, ps), PType (_, qs)) = match_patterns (ps, qs) |
|
111 |
|
112 (* Is there a unifiable constant? *) |
|
113 fun pconst_mem f consts (s, ps) = |
|
114 exists (curry (match_ptype o f) ps) |
|
115 (map snd (filter (curry (op =) s o fst) consts)) |
|
116 fun pconst_hyper_mem f const_tab (s, ps) = |
|
117 exists (curry (match_ptype o f) ps) (these (Symtab.lookup const_tab s)) |
|
118 |
|
119 fun pattern_for_type (Type (s, Ts)) = PApp (s, map pattern_for_type Ts) |
|
120 | pattern_for_type (TFree (s, _)) = PApp (s, []) |
|
121 | pattern_for_type (TVar _) = PVar |
|
122 |
|
123 fun pterm thy t = |
|
124 case strip_comb t of |
|
125 (Const x, ts) => PApp (pconst thy true x ts) |
|
126 | (Free x, ts) => PApp (pconst thy false x ts) |
|
127 | (Var x, []) => PVar |
|
128 | _ => PApp ("?", []) (* equivalence class of higher-order constructs *) |
|
129 (* Pairs a constant with the list of its type instantiations. *) |
|
130 and ptype thy const x ts = |
|
131 (if const then map pattern_for_type (these (try (Sign.const_typargs thy) x)) |
|
132 else []) @ |
|
133 (if term_patterns then map (pterm thy) ts else []) |
|
134 and pconst thy const (s, T) ts = (s, ptype thy const (s, T) ts) |
|
135 and rich_ptype thy const (s, T) ts = |
|
136 PType (order_of_type T, ptype thy const (s, T) ts) |
|
137 and rich_pconst thy const (s, T) ts = (s, rich_ptype thy const (s, T) ts) |
|
138 |
|
139 fun string_for_hyper_pconst (s, ps) = |
|
140 s ^ "{" ^ commas (map string_for_ptype ps) ^ "}" |
|
141 |
|
142 val abs_name = "Sledgehammer.abs" |
|
143 val skolem_prefix = "Sledgehammer.sko" |
|
144 |
|
145 (* These are typically simplified away by "Meson.presimplify". Equality is |
|
146 handled specially via "fequal". *) |
|
147 val boring_consts = |
|
148 [@{const_name False}, @{const_name True}, @{const_name If}, @{const_name Let}, |
|
149 @{const_name HOL.eq}] |
|
150 |
|
151 (* Add a pconstant to the table, but a [] entry means a standard |
|
152 connective, which we ignore.*) |
|
153 fun add_pconst_to_table also_skolem (c, p) = |
|
154 if member (op =) boring_consts c orelse |
|
155 (not also_skolem andalso String.isPrefix skolem_prefix c) then |
|
156 I |
|
157 else |
|
158 Symtab.map_default (c, [p]) (insert (op =) p) |
|
159 |
|
160 fun is_formula_type T = (T = HOLogic.boolT orelse T = propT) |
|
161 |
|
162 fun pconsts_in_terms thy also_skolems pos ts = |
|
163 let |
|
164 val flip = Option.map not |
|
165 (* We include free variables, as well as constants, to handle locales. For |
|
166 each quantifiers that must necessarily be skolemized by the ATP, we |
|
167 introduce a fresh constant to simulate the effect of Skolemization. *) |
|
168 fun do_const const (s, T) ts = |
|
169 add_pconst_to_table also_skolems (rich_pconst thy const (s, T) ts) |
|
170 #> fold do_term ts |
|
171 and do_term t = |
|
172 case strip_comb t of |
|
173 (Const x, ts) => do_const true x ts |
|
174 | (Free x, ts) => do_const false x ts |
|
175 | (Abs (_, T, t'), ts) => |
|
176 (null ts |
|
177 ? add_pconst_to_table true (abs_name, PType (order_of_type T + 1, []))) |
|
178 #> fold do_term (t' :: ts) |
|
179 | (_, ts) => fold do_term ts |
|
180 fun do_quantifier will_surely_be_skolemized abs_T body_t = |
|
181 do_formula pos body_t |
|
182 #> (if also_skolems andalso will_surely_be_skolemized then |
|
183 add_pconst_to_table true |
|
184 (gensym skolem_prefix, PType (order_of_type abs_T, [])) |
|
185 else |
|
186 I) |
|
187 and do_term_or_formula T = |
|
188 if is_formula_type T then do_formula NONE else do_term |
|
189 and do_formula pos t = |
|
190 case t of |
|
191 Const (@{const_name all}, _) $ Abs (_, T, t') => |
|
192 do_quantifier (pos = SOME false) T t' |
|
193 | @{const "==>"} $ t1 $ t2 => |
|
194 do_formula (flip pos) t1 #> do_formula pos t2 |
|
195 | Const (@{const_name "=="}, Type (_, [T, _])) $ t1 $ t2 => |
|
196 fold (do_term_or_formula T) [t1, t2] |
|
197 | @{const Trueprop} $ t1 => do_formula pos t1 |
|
198 | @{const Not} $ t1 => do_formula (flip pos) t1 |
|
199 | Const (@{const_name All}, _) $ Abs (_, T, t') => |
|
200 do_quantifier (pos = SOME false) T t' |
|
201 | Const (@{const_name Ex}, _) $ Abs (_, T, t') => |
|
202 do_quantifier (pos = SOME true) T t' |
|
203 | @{const HOL.conj} $ t1 $ t2 => fold (do_formula pos) [t1, t2] |
|
204 | @{const HOL.disj} $ t1 $ t2 => fold (do_formula pos) [t1, t2] |
|
205 | @{const HOL.implies} $ t1 $ t2 => |
|
206 do_formula (flip pos) t1 #> do_formula pos t2 |
|
207 | Const (@{const_name HOL.eq}, Type (_, [T, _])) $ t1 $ t2 => |
|
208 fold (do_term_or_formula T) [t1, t2] |
|
209 | Const (@{const_name If}, Type (_, [_, Type (_, [T, _])])) |
|
210 $ t1 $ t2 $ t3 => |
|
211 do_formula NONE t1 #> fold (do_term_or_formula T) [t2, t3] |
|
212 | Const (@{const_name Ex1}, _) $ Abs (_, T, t') => |
|
213 do_quantifier (is_some pos) T t' |
|
214 | Const (@{const_name Ball}, _) $ t1 $ Abs (_, T, t') => |
|
215 do_quantifier (pos = SOME false) T |
|
216 (HOLogic.mk_imp (incr_boundvars 1 t1 $ Bound 0, t')) |
|
217 | Const (@{const_name Bex}, _) $ t1 $ Abs (_, T, t') => |
|
218 do_quantifier (pos = SOME true) T |
|
219 (HOLogic.mk_conj (incr_boundvars 1 t1 $ Bound 0, t')) |
|
220 | (t0 as Const (_, @{typ bool})) $ t1 => |
|
221 do_term t0 #> do_formula pos t1 (* theory constant *) |
|
222 | _ => do_term t |
|
223 in Symtab.empty |> fold (do_formula pos) ts end |
|
224 |
|
225 (*Inserts a dummy "constant" referring to the theory name, so that relevance |
|
226 takes the given theory into account.*) |
|
227 fun theory_const_prop_of theory_relevant th = |
|
228 if theory_relevant then |
|
229 let |
|
230 val name = Context.theory_name (theory_of_thm th) |
|
231 val t = Const (name ^ ". 1", @{typ bool}) |
|
232 in t $ prop_of th end |
|
233 else |
|
234 prop_of th |
|
235 |
|
236 (**** Constant / Type Frequencies ****) |
|
237 |
|
238 (* A two-dimensional symbol table counts frequencies of constants. It's keyed |
|
239 first by constant name and second by its list of type instantiations. For the |
|
240 latter, we need a linear ordering on "pattern list". *) |
|
241 |
|
242 fun pattern_ord p = |
|
243 case p of |
|
244 (PVar, PVar) => EQUAL |
|
245 | (PVar, PApp _) => LESS |
|
246 | (PApp _, PVar) => GREATER |
|
247 | (PApp q1, PApp q2) => |
|
248 prod_ord fast_string_ord (dict_ord pattern_ord) (q1, q2) |
|
249 fun ptype_ord (PType p, PType q) = |
|
250 prod_ord (dict_ord pattern_ord) int_ord (swap p, swap q) |
|
251 |
|
252 structure PType_Tab = Table(type key = ptype val ord = ptype_ord) |
|
253 |
|
254 fun count_axiom_consts theory_relevant thy = |
|
255 let |
|
256 fun do_const const (s, T) ts = |
|
257 (* Two-dimensional table update. Constant maps to types maps to count. *) |
|
258 PType_Tab.map_default (rich_ptype thy const (s, T) ts, 0) (Integer.add 1) |
|
259 |> Symtab.map_default (s, PType_Tab.empty) |
|
260 #> fold do_term ts |
|
261 and do_term t = |
|
262 case strip_comb t of |
|
263 (Const x, ts) => do_const true x ts |
|
264 | (Free x, ts) => do_const false x ts |
|
265 | (Abs (_, _, t'), ts) => fold do_term (t' :: ts) |
|
266 | (_, ts) => fold do_term ts |
|
267 in do_term o theory_const_prop_of theory_relevant o snd end |
|
268 |
|
269 |
|
270 (**** Actual Filtering Code ****) |
|
271 |
|
272 fun pow_int x 0 = 1.0 |
|
273 | pow_int x 1 = x |
|
274 | pow_int x n = if n > 0 then x * pow_int x (n - 1) else pow_int x (n + 1) / x |
|
275 |
|
276 (*The frequency of a constant is the sum of those of all instances of its type.*) |
|
277 fun pconst_freq match const_tab (c, ps) = |
|
278 PType_Tab.fold (fn (qs, m) => match (ps, qs) ? Integer.add m) |
|
279 (the (Symtab.lookup const_tab c)) 0 |
|
280 |
|
281 |
|
282 (* A surprising number of theorems contain only a few significant constants. |
|
283 These include all induction rules, and other general theorems. *) |
|
284 |
|
285 (* "log" seems best in practice. A constant function of one ignores the constant |
|
286 frequencies. Rare constants give more points if they are relevant than less |
|
287 rare ones. *) |
|
288 fun rel_weight_for order freq = 1.0 + 2.0 / Math.ln (Real.fromInt freq + 1.0) |
|
289 |
|
290 (* FUDGE *) |
|
291 val worse_irrel_freq = Unsynchronized.ref 100.0 |
|
292 val higher_order_irrel_weight = Unsynchronized.ref 1.05 |
|
293 |
|
294 (* Irrelevant constants are treated differently. We associate lower penalties to |
|
295 very rare constants and very common ones -- the former because they can't |
|
296 lead to the inclusion of too many new facts, and the latter because they are |
|
297 so common as to be of little interest. *) |
|
298 fun irrel_weight_for order freq = |
|
299 let val (k, x) = !worse_irrel_freq |> `Real.ceil in |
|
300 (if freq < k then Math.ln (Real.fromInt (freq + 1)) / Math.ln x |
|
301 else rel_weight_for order freq / rel_weight_for order k) |
|
302 * pow_int (!higher_order_irrel_weight) (order - 1) |
|
303 end |
|
304 |
|
305 (* FUDGE *) |
|
306 val abs_rel_weight = Unsynchronized.ref 0.5 |
|
307 val abs_irrel_weight = Unsynchronized.ref 2.0 |
|
308 val skolem_irrel_weight = Unsynchronized.ref 0.75 |
|
309 |
|
310 (* Computes a constant's weight, as determined by its frequency. *) |
|
311 fun generic_pconst_weight abs_weight skolem_weight weight_for f const_tab |
|
312 (c as (s, PType (m, _))) = |
|
313 if s = abs_name then abs_weight |
|
314 else if String.isPrefix skolem_prefix s then skolem_weight |
|
315 else weight_for m (pconst_freq (match_ptype o f) const_tab c) |
|
316 |
|
317 fun rel_pconst_weight const_tab = |
|
318 generic_pconst_weight (!abs_rel_weight) 0.0 rel_weight_for I const_tab |
|
319 fun irrel_pconst_weight const_tab = |
|
320 generic_pconst_weight (!abs_irrel_weight) (!skolem_irrel_weight) |
|
321 irrel_weight_for swap const_tab |
|
322 |
|
323 (* FUDGE *) |
|
324 val intro_bonus = Unsynchronized.ref 0.15 |
|
325 val elim_bonus = Unsynchronized.ref 0.15 |
|
326 val simp_bonus = Unsynchronized.ref 0.15 |
|
327 val local_bonus = Unsynchronized.ref 0.55 |
|
328 val chained_bonus = Unsynchronized.ref 1.5 |
|
329 |
|
330 fun locality_bonus General = 0.0 |
|
331 | locality_bonus Intro = !intro_bonus |
|
332 | locality_bonus Elim = !elim_bonus |
|
333 | locality_bonus Simp = !simp_bonus |
|
334 | locality_bonus Local = !local_bonus |
|
335 | locality_bonus Chained = !chained_bonus |
|
336 |
|
337 fun axiom_weight loc const_tab relevant_consts axiom_consts = |
|
338 case axiom_consts |> List.partition (pconst_hyper_mem I relevant_consts) |
|
339 ||> filter_out (pconst_hyper_mem swap relevant_consts) of |
|
340 ([], _) => 0.0 |
|
341 | (rel, irrel) => |
|
342 let |
|
343 val irrel = irrel |> filter_out (pconst_mem swap rel) |
|
344 val rel_weight = |
|
345 0.0 |> fold (curry (op +) o rel_pconst_weight const_tab) rel |
|
346 val irrel_weight = |
|
347 ~ (locality_bonus loc) |
|
348 |> fold (curry (op +) o irrel_pconst_weight const_tab) irrel |
|
349 val res = rel_weight / (rel_weight + irrel_weight) |
|
350 in if Real.isFinite res then res else 0.0 end |
|
351 |
|
352 (* FIXME: experiment |
|
353 fun debug_axiom_weight loc const_tab relevant_consts axiom_consts = |
|
354 case axiom_consts |> List.partition (pconst_hyper_mem I relevant_consts) |
|
355 ||> filter_out (pconst_hyper_mem swap relevant_consts) of |
|
356 ([], _) => 0.0 |
|
357 | (rel, irrel) => |
|
358 let |
|
359 val irrel = irrel |> filter_out (pconst_mem swap rel) |
|
360 val rels_weight = |
|
361 0.0 |> fold (curry (op +) o rel_pconst_weight const_tab) rel |
|
362 val irrels_weight = |
|
363 ~ (locality_bonus loc) |
|
364 |> fold (curry (op +) o irrel_pconst_weight const_tab) irrel |
|
365 val _ = tracing (PolyML.makestring ("REL: ", map (`(rel_pconst_weight const_tab)) rel)) |
|
366 val _ = tracing (PolyML.makestring ("IRREL: ", map (`(irrel_pconst_weight const_tab)) irrel)) |
|
367 val res = rels_weight / (rels_weight + irrels_weight) |
|
368 in if Real.isFinite res then res else 0.0 end |
|
369 *) |
|
370 |
|
371 fun pconsts_in_axiom thy t = |
|
372 Symtab.fold (fn (s, pss) => fold (cons o pair s) pss) |
|
373 (pconsts_in_terms thy true (SOME true) [t]) [] |
|
374 fun pair_consts_axiom theory_relevant thy axiom = |
|
375 case axiom |> snd |> theory_const_prop_of theory_relevant |
|
376 |> pconsts_in_axiom thy of |
|
377 [] => NONE |
|
378 | consts => SOME ((axiom, consts), NONE) |
|
379 |
|
380 type annotated_thm = |
|
381 (((unit -> string) * locality) * thm) * (string * ptype) list |
|
382 |
|
383 (* FUDGE *) |
|
384 val max_imperfect = Unsynchronized.ref 11.5 |
|
385 val max_imperfect_exp = Unsynchronized.ref 1.0 |
|
386 |
|
387 fun take_most_relevant max_relevant remaining_max |
|
388 (candidates : (annotated_thm * real) list) = |
|
389 let |
|
390 val max_imperfect = |
|
391 Real.ceil (Math.pow (!max_imperfect, |
|
392 Math.pow (Real.fromInt remaining_max |
|
393 / Real.fromInt max_relevant, !max_imperfect_exp))) |
|
394 val (perfect, imperfect) = |
|
395 candidates |> sort (Real.compare o swap o pairself snd) |
|
396 |> take_prefix (fn (_, w) => w > 0.99999) |
|
397 val ((accepts, more_rejects), rejects) = |
|
398 chop max_imperfect imperfect |>> append perfect |>> chop remaining_max |
|
399 in |
|
400 trace_msg (fn () => |
|
401 "Actually passed (" ^ Int.toString (length accepts) ^ " of " ^ |
|
402 Int.toString (length candidates) ^ "): " ^ |
|
403 (accepts |> map (fn ((((name, _), _), _), weight) => |
|
404 name () ^ " [" ^ Real.toString weight ^ "]") |
|
405 |> commas)); |
|
406 (accepts, more_rejects @ rejects) |
|
407 end |
|
408 |
|
409 fun if_empty_replace_with_locality thy axioms loc tab = |
|
410 if Symtab.is_empty tab then |
|
411 pconsts_in_terms thy false (SOME false) |
|
412 (map_filter (fn ((_, loc'), th) => |
|
413 if loc' = loc then SOME (prop_of th) else NONE) axioms) |
|
414 else |
|
415 tab |
|
416 |
|
417 (* FUDGE *) |
|
418 val threshold_divisor = Unsynchronized.ref 2.0 |
|
419 val ridiculous_threshold = Unsynchronized.ref 0.1 |
|
420 |
|
421 fun relevance_filter ctxt threshold0 decay max_relevant theory_relevant |
|
422 ({add, del, ...} : relevance_override) axioms goal_ts = |
|
423 let |
|
424 val thy = ProofContext.theory_of ctxt |
|
425 val const_tab = |
|
426 fold (count_axiom_consts theory_relevant thy) axioms Symtab.empty |
|
427 val goal_const_tab = |
|
428 pconsts_in_terms thy false (SOME false) goal_ts |
|
429 |> fold (if_empty_replace_with_locality thy axioms) [Chained, Local] |
|
430 val add_thms = maps (ProofContext.get_fact ctxt) add |
|
431 val del_thms = maps (ProofContext.get_fact ctxt) del |
|
432 fun iter j remaining_max threshold rel_const_tab hopeless hopeful = |
|
433 let |
|
434 fun game_over rejects = |
|
435 (* Add "add:" facts. *) |
|
436 if null add_thms then |
|
437 [] |
|
438 else |
|
439 map_filter (fn ((p as (_, th), _), _) => |
|
440 if member Thm.eq_thm add_thms th then SOME p |
|
441 else NONE) rejects |
|
442 fun relevant [] rejects [] = |
|
443 (* Nothing has been added this iteration. *) |
|
444 if j = 0 andalso threshold >= !ridiculous_threshold then |
|
445 (* First iteration? Try again. *) |
|
446 iter 0 max_relevant (threshold / !threshold_divisor) rel_const_tab |
|
447 hopeless hopeful |
|
448 else |
|
449 game_over (rejects @ hopeless) |
|
450 | relevant candidates rejects [] = |
|
451 let |
|
452 val (accepts, more_rejects) = |
|
453 take_most_relevant max_relevant remaining_max candidates |
|
454 val rel_const_tab' = |
|
455 rel_const_tab |
|
456 |> fold (add_pconst_to_table false) (maps (snd o fst) accepts) |
|
457 fun is_dirty (c, _) = |
|
458 Symtab.lookup rel_const_tab' c <> Symtab.lookup rel_const_tab c |
|
459 val (hopeful_rejects, hopeless_rejects) = |
|
460 (rejects @ hopeless, ([], [])) |
|
461 |-> fold (fn (ax as (_, consts), old_weight) => |
|
462 if exists is_dirty consts then |
|
463 apfst (cons (ax, NONE)) |
|
464 else |
|
465 apsnd (cons (ax, old_weight))) |
|
466 |>> append (more_rejects |
|
467 |> map (fn (ax as (_, consts), old_weight) => |
|
468 (ax, if exists is_dirty consts then NONE |
|
469 else SOME old_weight))) |
|
470 val threshold = |
|
471 1.0 - (1.0 - threshold) |
|
472 * Math.pow (decay, Real.fromInt (length accepts)) |
|
473 val remaining_max = remaining_max - length accepts |
|
474 in |
|
475 trace_msg (fn () => "New or updated constants: " ^ |
|
476 commas (rel_const_tab' |> Symtab.dest |
|
477 |> subtract (op =) (rel_const_tab |> Symtab.dest) |
|
478 |> map string_for_hyper_pconst)); |
|
479 map (fst o fst) accepts @ |
|
480 (if remaining_max = 0 then |
|
481 game_over (hopeful_rejects @ map (apsnd SOME) hopeless_rejects) |
|
482 else |
|
483 iter (j + 1) remaining_max threshold rel_const_tab' |
|
484 hopeless_rejects hopeful_rejects) |
|
485 end |
|
486 | relevant candidates rejects |
|
487 (((ax as (((_, loc), th), axiom_consts)), cached_weight) |
|
488 :: hopeful) = |
|
489 let |
|
490 val weight = |
|
491 case cached_weight of |
|
492 SOME w => w |
|
493 | NONE => axiom_weight loc const_tab rel_const_tab axiom_consts |
|
494 (* FIXME: experiment |
|
495 val name = fst (fst (fst ax)) () |
|
496 val _ = if String.isSubstring "positive_minus" name orelse String.isSubstring "not_exp_le_zero" name then |
|
497 tracing ("*** " ^ name ^ PolyML.makestring (debug_axiom_weight loc const_tab rel_const_tab axiom_consts)) |
|
498 else |
|
499 () |
|
500 *) |
|
501 in |
|
502 if weight >= threshold then |
|
503 relevant ((ax, weight) :: candidates) rejects hopeful |
|
504 else |
|
505 relevant candidates ((ax, weight) :: rejects) hopeful |
|
506 end |
|
507 in |
|
508 trace_msg (fn () => |
|
509 "ITERATION " ^ string_of_int j ^ ": current threshold: " ^ |
|
510 Real.toString threshold ^ ", constants: " ^ |
|
511 commas (rel_const_tab |> Symtab.dest |
|
512 |> filter (curry (op <>) [] o snd) |
|
513 |> map string_for_hyper_pconst)); |
|
514 relevant [] [] hopeful |
|
515 end |
|
516 in |
|
517 axioms |> filter_out (member Thm.eq_thm del_thms o snd) |
|
518 |> map_filter (pair_consts_axiom theory_relevant thy) |
|
519 |> iter 0 max_relevant threshold0 goal_const_tab [] |
|
520 |> tap (fn res => trace_msg (fn () => |
|
521 "Total relevant: " ^ Int.toString (length res))) |
|
522 end |
|
523 |
|
524 |
|
525 (***************************************************************) |
|
526 (* Retrieving and filtering lemmas *) |
|
527 (***************************************************************) |
|
528 |
|
529 (*** retrieve lemmas and filter them ***) |
|
530 |
|
531 (*Reject theorems with names like "List.filter.filter_list_def" or |
|
532 "Accessible_Part.acc.defs", as these are definitions arising from packages.*) |
|
533 fun is_package_def a = |
|
534 let val names = Long_Name.explode a |
|
535 in |
|
536 length names > 2 andalso |
|
537 not (hd names = "local") andalso |
|
538 String.isSuffix "_def" a orelse String.isSuffix "_defs" a |
|
539 end; |
|
540 |
|
541 fun mk_fact_table f xs = |
|
542 fold (Termtab.update o `(prop_of o f)) xs Termtab.empty |
|
543 fun uniquify xs = Termtab.fold (cons o snd) (mk_fact_table snd xs) [] |
|
544 |
|
545 (* FIXME: put other record thms here, or declare as "no_atp" *) |
|
546 val multi_base_blacklist = |
|
547 ["defs", "select_defs", "update_defs", "induct", "inducts", "split", "splits", |
|
548 "split_asm", "cases", "ext_cases", "eq.simps", "eq.refl", "nchotomy", |
|
549 "case_cong", "weak_case_cong"] |
|
550 |> map (prefix ".") |
|
551 |
|
552 val max_lambda_nesting = 3 |
|
553 |
|
554 fun term_has_too_many_lambdas max (t1 $ t2) = |
|
555 exists (term_has_too_many_lambdas max) [t1, t2] |
|
556 | term_has_too_many_lambdas max (Abs (_, _, t)) = |
|
557 max = 0 orelse term_has_too_many_lambdas (max - 1) t |
|
558 | term_has_too_many_lambdas _ _ = false |
|
559 |
|
560 (* Don't count nested lambdas at the level of formulas, since they are |
|
561 quantifiers. *) |
|
562 fun formula_has_too_many_lambdas Ts (Abs (_, T, t)) = |
|
563 formula_has_too_many_lambdas (T :: Ts) t |
|
564 | formula_has_too_many_lambdas Ts t = |
|
565 if is_formula_type (fastype_of1 (Ts, t)) then |
|
566 exists (formula_has_too_many_lambdas Ts) (#2 (strip_comb t)) |
|
567 else |
|
568 term_has_too_many_lambdas max_lambda_nesting t |
|
569 |
|
570 (* The max apply depth of any "metis" call in "Metis_Examples" (on 2007-10-31) |
|
571 was 11. *) |
|
572 val max_apply_depth = 15 |
|
573 |
|
574 fun apply_depth (f $ t) = Int.max (apply_depth f, apply_depth t + 1) |
|
575 | apply_depth (Abs (_, _, t)) = apply_depth t |
|
576 | apply_depth _ = 0 |
|
577 |
|
578 fun is_formula_too_complex t = |
|
579 apply_depth t > max_apply_depth orelse formula_has_too_many_lambdas [] t |
|
580 |
|
581 val exists_sledgehammer_const = |
|
582 exists_Const (fn (s, _) => String.isPrefix sledgehammer_prefix s) |
|
583 |
|
584 (* FIXME: make more reliable *) |
|
585 val exists_low_level_class_const = |
|
586 exists_Const (fn (s, _) => |
|
587 String.isSubstring (Long_Name.separator ^ "class" ^ Long_Name.separator) s) |
|
588 |
|
589 fun is_metastrange_theorem th = |
|
590 case head_of (concl_of th) of |
|
591 Const (a, _) => (a <> @{const_name Trueprop} andalso |
|
592 a <> @{const_name "=="}) |
|
593 | _ => false |
|
594 |
|
595 fun is_that_fact th = |
|
596 String.isSuffix (Long_Name.separator ^ Obtain.thatN) (Thm.get_name_hint th) |
|
597 andalso exists_subterm (fn Free (s, _) => s = Name.skolem Auto_Bind.thesisN |
|
598 | _ => false) (prop_of th) |
|
599 |
|
600 val type_has_top_sort = |
|
601 exists_subtype (fn TFree (_, []) => true | TVar (_, []) => true | _ => false) |
|
602 |
|
603 (**** Predicates to detect unwanted facts (prolific or likely to cause |
|
604 unsoundness) ****) |
|
605 |
|
606 (* Too general means, positive equality literal with a variable X as one |
|
607 operand, when X does not occur properly in the other operand. This rules out |
|
608 clearly inconsistent facts such as X = a | X = b, though it by no means |
|
609 guarantees soundness. *) |
|
610 |
|
611 (* Unwanted equalities are those between a (bound or schematic) variable that |
|
612 does not properly occur in the second operand. *) |
|
613 val is_exhaustive_finite = |
|
614 let |
|
615 fun is_bad_equal (Var z) t = |
|
616 not (exists_subterm (fn Var z' => z = z' | _ => false) t) |
|
617 | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j)) |
|
618 | is_bad_equal _ _ = false |
|
619 fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1 |
|
620 fun do_formula pos t = |
|
621 case (pos, t) of |
|
622 (_, @{const Trueprop} $ t1) => do_formula pos t1 |
|
623 | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) => |
|
624 do_formula pos t' |
|
625 | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) => |
|
626 do_formula pos t' |
|
627 | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) => |
|
628 do_formula pos t' |
|
629 | (_, @{const "==>"} $ t1 $ t2) => |
|
630 do_formula (not pos) t1 andalso |
|
631 (t2 = @{prop False} orelse do_formula pos t2) |
|
632 | (_, @{const HOL.implies} $ t1 $ t2) => |
|
633 do_formula (not pos) t1 andalso |
|
634 (t2 = @{const False} orelse do_formula pos t2) |
|
635 | (_, @{const Not} $ t1) => do_formula (not pos) t1 |
|
636 | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2] |
|
637 | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2] |
|
638 | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2 |
|
639 | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2 |
|
640 | _ => false |
|
641 in do_formula true end |
|
642 |
|
643 fun has_bound_or_var_of_type tycons = |
|
644 exists_subterm (fn Var (_, Type (s, _)) => member (op =) tycons s |
|
645 | Abs (_, Type (s, _), _) => member (op =) tycons s |
|
646 | _ => false) |
|
647 |
|
648 (* Facts are forbidden to contain variables of these types. The typical reason |
|
649 is that they lead to unsoundness. Note that "unit" satisfies numerous |
|
650 equations like "?x = ()". The resulting clauses will have no type constraint, |
|
651 yielding false proofs. Even "bool" leads to many unsound proofs, though only |
|
652 for higher-order problems. *) |
|
653 val dangerous_types = [@{type_name unit}, @{type_name bool}, @{type_name prop}]; |
|
654 |
|
655 (* Facts containing variables of type "unit" or "bool" or of the form |
|
656 "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types |
|
657 are omitted. *) |
|
658 fun is_dangerous_term full_types t = |
|
659 not full_types andalso |
|
660 let val t = transform_elim_term t in |
|
661 has_bound_or_var_of_type dangerous_types t orelse |
|
662 is_exhaustive_finite t |
|
663 end |
|
664 |
|
665 fun is_theorem_bad_for_atps full_types thm = |
|
666 let val t = prop_of thm in |
|
667 is_formula_too_complex t orelse exists_type type_has_top_sort t orelse |
|
668 is_dangerous_term full_types t orelse exists_sledgehammer_const t orelse |
|
669 exists_low_level_class_const t orelse is_metastrange_theorem thm orelse |
|
670 is_that_fact thm |
|
671 end |
|
672 |
|
673 fun clasimpset_rules_of ctxt = |
|
674 let |
|
675 val {safeIs, safeEs, hazIs, hazEs, ...} = ctxt |> claset_of |> rep_cs |
|
676 val intros = safeIs @ hazIs |
|
677 val elims = map Classical.classical_rule (safeEs @ hazEs) |
|
678 val simps = ctxt |> simpset_of |> dest_ss |> #simps |> map snd |
|
679 in (mk_fact_table I intros, mk_fact_table I elims, mk_fact_table I simps) end |
|
680 |
|
681 fun all_name_thms_pairs ctxt reserved full_types add_thms chained_ths = |
|
682 let |
|
683 val thy = ProofContext.theory_of ctxt |
|
684 val global_facts = PureThy.facts_of thy |
|
685 val local_facts = ProofContext.facts_of ctxt |
|
686 val named_locals = local_facts |> Facts.dest_static [] |
|
687 val is_chained = member Thm.eq_thm chained_ths |
|
688 val (intros, elims, simps) = |
|
689 if exists (curry (op <) 0.0) [!intro_bonus, !elim_bonus, !simp_bonus] then |
|
690 clasimpset_rules_of ctxt |
|
691 else |
|
692 (Termtab.empty, Termtab.empty, Termtab.empty) |
|
693 (* Unnamed nonchained formulas with schematic variables are omitted, because |
|
694 they are rejected by the backticks (`...`) parser for some reason. *) |
|
695 fun is_good_unnamed_local th = |
|
696 not (Thm.has_name_hint th) andalso |
|
697 (not (exists_subterm is_Var (prop_of th)) orelse (is_chained th)) andalso |
|
698 forall (fn (_, ths) => not (member Thm.eq_thm ths th)) named_locals |
|
699 val unnamed_locals = |
|
700 union Thm.eq_thm (Facts.props local_facts) chained_ths |
|
701 |> filter is_good_unnamed_local |> map (pair "" o single) |
|
702 val full_space = |
|
703 Name_Space.merge (Facts.space_of global_facts, Facts.space_of local_facts) |
|
704 fun add_facts global foldx facts = |
|
705 foldx (fn (name0, ths) => |
|
706 if name0 <> "" andalso |
|
707 forall (not o member Thm.eq_thm add_thms) ths andalso |
|
708 (Facts.is_concealed facts name0 orelse |
|
709 (respect_no_atp andalso is_package_def name0) orelse |
|
710 exists (fn s => String.isSuffix s name0) multi_base_blacklist orelse |
|
711 String.isSuffix "_def_raw" (* FIXME: crude hack *) name0) then |
|
712 I |
|
713 else |
|
714 let |
|
715 val multi = length ths > 1 |
|
716 fun backquotify th = |
|
717 "`" ^ Print_Mode.setmp [Print_Mode.input] |
|
718 (Syntax.string_of_term ctxt) (prop_of th) ^ "`" |
|
719 |> String.translate (fn c => if Char.isPrint c then str c else "") |
|
720 |> simplify_spaces |
|
721 fun check_thms a = |
|
722 case try (ProofContext.get_thms ctxt) a of |
|
723 NONE => false |
|
724 | SOME ths' => Thm.eq_thms (ths, ths') |
|
725 in |
|
726 pair 1 |
|
727 #> fold (fn th => fn (j, rest) => |
|
728 (j + 1, |
|
729 if is_theorem_bad_for_atps full_types th andalso |
|
730 not (member Thm.eq_thm add_thms th) then |
|
731 rest |
|
732 else |
|
733 (((fn () => |
|
734 if name0 = "" then |
|
735 th |> backquotify |
|
736 else |
|
737 let |
|
738 val name1 = Facts.extern facts name0 |
|
739 val name2 = Name_Space.extern full_space name0 |
|
740 in |
|
741 case find_first check_thms [name1, name2, name0] of |
|
742 SOME name => repair_name reserved multi j name |
|
743 | NONE => "" |
|
744 end), |
|
745 let val t = prop_of th in |
|
746 if is_chained th then Chained |
|
747 else if not global then Local |
|
748 else if Termtab.defined intros t then Intro |
|
749 else if Termtab.defined elims t then Elim |
|
750 else if Termtab.defined simps t then Simp |
|
751 else General |
|
752 end), |
|
753 (multi, th)) :: rest)) ths |
|
754 #> snd |
|
755 end) |
|
756 in |
|
757 [] |> add_facts false fold local_facts (unnamed_locals @ named_locals) |
|
758 |> add_facts true Facts.fold_static global_facts global_facts |
|
759 end |
|
760 |
|
761 (* The single-name theorems go after the multiple-name ones, so that single |
|
762 names are preferred when both are available. *) |
|
763 fun name_thm_pairs ctxt respect_no_atp = |
|
764 List.partition (fst o snd) #> op @ #> map (apsnd snd) |
|
765 #> respect_no_atp ? filter_out (No_ATPs.member ctxt o snd) |
|
766 |
|
767 (***************************************************************) |
|
768 (* ATP invocation methods setup *) |
|
769 (***************************************************************) |
|
770 |
|
771 fun relevant_facts ctxt full_types (threshold0, threshold1) max_relevant |
|
772 theory_relevant (relevance_override as {add, del, only}) |
|
773 chained_ths hyp_ts concl_t = |
|
774 let |
|
775 val decay = Math.pow ((1.0 - threshold1) / (1.0 - threshold0), |
|
776 1.0 / Real.fromInt (max_relevant + 1)) |
|
777 val add_thms = maps (ProofContext.get_fact ctxt) add |
|
778 val reserved = reserved_isar_keyword_table () |
|
779 val axioms = |
|
780 (if only then |
|
781 maps (map (fn ((name, loc), th) => ((K name, loc), (true, th))) |
|
782 o name_thm_pairs_from_ref ctxt reserved chained_ths) add |
|
783 else |
|
784 all_name_thms_pairs ctxt reserved full_types add_thms chained_ths) |
|
785 |> name_thm_pairs ctxt (respect_no_atp andalso not only) |
|
786 |> uniquify |
|
787 in |
|
788 trace_msg (fn () => "Considering " ^ Int.toString (length axioms) ^ |
|
789 " theorems"); |
|
790 (if threshold0 > 1.0 orelse threshold0 > threshold1 then |
|
791 [] |
|
792 else if threshold0 < 0.0 then |
|
793 axioms |
|
794 else |
|
795 relevance_filter ctxt threshold0 decay max_relevant theory_relevant |
|
796 relevance_override axioms (concl_t :: hyp_ts)) |
|
797 |> map (apfst (apfst (fn f => f ()))) |
|
798 end |
|
799 |
|
800 end; |
|