author  blanchet 
Fri, 22 Oct 2010 14:47:43 +0200  
changeset 40070  bdb890782d4a 
parent 39958  88c9aa5666de 
child 40071  658a37c80b53 
permissions  rwrr 
38988  1 
(* Title: HOL/Tools/Sledgehammer/sledgehammer_filter.ML 
38027  2 
Author: Jia Meng, Cambridge University Computer Laboratory and NICTA 
36393
be73a2b2443b
support readable names even when Isar proof reconstruction is enabled  useful for debugging
blanchet
parents:
36227
diff
changeset

3 
Author: Jasmin Blanchette, TU Muenchen 
39958  4 

5 
Sledgehammer's relevance filter. 

33309  6 
*) 
15452  7 

38988  8 
signature SLEDGEHAMMER_FILTER = 
16802
6eeee59dac4c
use Toplevel.print_state_hook instead of adhoc Proof.atp_hook;
wenzelm
parents:
16767
diff
changeset

9 
sig 
38993
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

10 
datatype locality = General  Intro  Elim  Simp  Local  Assum  Chained 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

11 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

12 
type relevance_fudge = 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

13 
{worse_irrel_freq : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

14 
higher_order_irrel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

15 
abs_rel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

16 
abs_irrel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

17 
skolem_irrel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

18 
theory_const_rel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

19 
theory_const_irrel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

20 
intro_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

21 
elim_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

22 
simp_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

23 
local_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

24 
assum_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

25 
chained_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

26 
max_imperfect : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

27 
max_imperfect_exp : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

28 
threshold_divisor : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

29 
ridiculous_threshold : real} 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

30 

35966
f8c738abaed8
honor the newly introduced Sledgehammer parameters and fixed the parsing;
blanchet
parents:
35963
diff
changeset

31 
type relevance_override = 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

32 
{add : (Facts.ref * Attrib.src list) list, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

33 
del : (Facts.ref * Attrib.src list) list, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

34 
only : bool} 
35966
f8c738abaed8
honor the newly introduced Sledgehammer parameters and fixed the parsing;
blanchet
parents:
35963
diff
changeset

35 

37616
c8d2d84d6011
always perform relevance filtering on original formulas
blanchet
parents:
37580
diff
changeset

36 
val trace : bool Unsynchronized.ref 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

37 
val name_thm_pairs_from_ref : 
38996  38 
Proof.context > unit Symtab.table > thm list 
39 
> Facts.ref * Attrib.src list > ((string * locality) * thm) list 

37347
635425a442e8
show more respect for userspecified facts, even if they could lead to unsound proofs + don't throw away "unsound" theorems in "full_type" mode, since they are then sound
blanchet
parents:
37345
diff
changeset

40 
val relevant_facts : 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

41 
Proof.context > bool > real * real > int > relevance_fudge 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

42 
> relevance_override > thm list > term list > term 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

43 
> ((string * locality) * thm) list 
15347  44 
end; 
45 

38988  46 
structure Sledgehammer_Filter : SLEDGEHAMMER_FILTER = 
15347  47 
struct 
48 

38652
e063be321438
perform etaexpansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38644
diff
changeset

49 
open Sledgehammer_Util 
e063be321438
perform etaexpansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38644
diff
changeset

50 

37616
c8d2d84d6011
always perform relevance filtering on original formulas
blanchet
parents:
37580
diff
changeset

51 
val trace = Unsynchronized.ref false 
c8d2d84d6011
always perform relevance filtering on original formulas
blanchet
parents:
37580
diff
changeset

52 
fun trace_msg msg = if !trace then tracing (msg ()) else () 
35826  53 

38997  54 
(* experimental features *) 
38889  55 
val term_patterns = false 
38997  56 
val respect_no_atp = true 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

57 

38993
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

58 
datatype locality = General  Intro  Elim  Simp  Local  Assum  Chained 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

59 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

60 
type relevance_fudge = 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

61 
{worse_irrel_freq : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

62 
higher_order_irrel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

63 
abs_rel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

64 
abs_irrel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

65 
skolem_irrel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

66 
theory_const_rel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

67 
theory_const_irrel_weight : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

68 
intro_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

69 
elim_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

70 
simp_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

71 
local_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

72 
assum_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

73 
chained_bonus : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

74 
max_imperfect : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

75 
max_imperfect_exp : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

76 
threshold_divisor : real, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

77 
ridiculous_threshold : real} 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

78 

35966
f8c738abaed8
honor the newly introduced Sledgehammer parameters and fixed the parsing;
blanchet
parents:
35963
diff
changeset

79 
type relevance_override = 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

80 
{add : (Facts.ref * Attrib.src list) list, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

81 
del : (Facts.ref * Attrib.src list) list, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

82 
only : bool} 
21070  83 

37616
c8d2d84d6011
always perform relevance filtering on original formulas
blanchet
parents:
37580
diff
changeset

84 
val sledgehammer_prefix = "Sledgehammer" ^ Long_Name.separator 
39896
13b3a2ba9ea7
encode axiom number and cluster number in all zapped quantifiers to help discharging new skolemizer assumptions
blanchet
parents:
39718
diff
changeset

85 
val abs_name = sledgehammer_prefix ^ "abs" 
13b3a2ba9ea7
encode axiom number and cluster number in all zapped quantifiers to help discharging new skolemizer assumptions
blanchet
parents:
39718
diff
changeset

86 
val skolem_prefix = sledgehammer_prefix ^ "sko" 
38992
542474156c66
introduce fudge factors to deal with "theory const"
blanchet
parents:
38988
diff
changeset

87 
val theory_const_suffix = Long_Name.separator ^ " 1" 
37616
c8d2d84d6011
always perform relevance filtering on original formulas
blanchet
parents:
37580
diff
changeset

88 

38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

89 
fun repair_name reserved multi j name = 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

90 
(name > Symtab.defined reserved name ? quote) ^ 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

91 
(if multi then "(" ^ Int.toString j ^ ")" else "") 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

92 

38996  93 
fun name_thm_pairs_from_ref ctxt reserved chained_ths (xthm as (xref, args)) = 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

94 
let 
38996  95 
val ths = Attrib.eval_thms ctxt [xthm] 
96 
val bracket = 

97 
implode (map (fn arg => "[" ^ Pretty.str_of (Args.pretty_src ctxt arg) 

98 
^ "]") args) 

99 
val name = 

100 
case xref of 

39264  101 
Facts.Fact s => "`" ^ s ^ "`" ^ bracket 
38996  102 
 Facts.Named (("", _), _) => "[" ^ bracket ^ "]" 
39264  103 
 _ => Facts.string_of_ref xref ^ bracket 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

104 
val multi = length ths > 1 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

105 
in 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

106 
(ths, (1, [])) 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

107 
> fold (fn th => fn (j, rest) => 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

108 
(j + 1, ((repair_name reserved multi j name, 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

109 
if member Thm.eq_thm chained_ths th then Chained 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

110 
else General), th) :: rest)) 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

111 
> snd 
38699  112 
end 
37616
c8d2d84d6011
always perform relevance filtering on original formulas
blanchet
parents:
37580
diff
changeset

113 

28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
28065
diff
changeset

114 
(***************************************************************) 
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
28065
diff
changeset

115 
(* Relevance Filtering *) 
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
28065
diff
changeset

116 
(***************************************************************) 
19194  117 

24287  118 
(*** constants with types ***) 
119 

38939  120 
fun order_of_type (Type (@{type_name fun}, [T1, @{typ bool}])) = 
121 
order_of_type T1 (* cheat: pretend sets are firstorder *) 

122 
 order_of_type (Type (@{type_name fun}, [T1, T2])) = 

123 
Int.max (order_of_type T1 + 1, order_of_type T2) 

124 
 order_of_type (Type (_, Ts)) = fold (Integer.max o order_of_type) Ts 0 

125 
 order_of_type _ = 0 

126 

38823
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

127 
(* An abstraction of Isabelle types and firstorder terms *) 
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

128 
datatype pattern = PVar  PApp of string * pattern list 
38939  129 
datatype ptype = PType of int * pattern list 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

130 

38823
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

131 
fun string_for_pattern PVar = "_" 
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

132 
 string_for_pattern (PApp (s, ps)) = 
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

133 
if null ps then s else s ^ string_for_patterns ps 
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

134 
and string_for_patterns ps = "(" ^ commas (map string_for_pattern ps) ^ ")" 
38939  135 
fun string_for_ptype (PType (_, ps)) = string_for_patterns ps 
24287  136 

137 
(*Is the second type an instance of the first one?*) 

38824  138 
fun match_pattern (PVar, _) = true 
139 
 match_pattern (PApp _, PVar) = false 

140 
 match_pattern (PApp (s, ps), PApp (t, qs)) = 

141 
s = t andalso match_patterns (ps, qs) 

142 
and match_patterns (_, []) = true 

143 
 match_patterns ([], _) = false 

144 
 match_patterns (p :: ps, q :: qs) = 

145 
match_pattern (p, q) andalso match_patterns (ps, qs) 

38939  146 
fun match_ptype (PType (_, ps), PType (_, qs)) = match_patterns (ps, qs) 
24287  147 

38823
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

148 
(* Is there a unifiable constant? *) 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

149 
fun pconst_mem f consts (s, ps) = 
38939  150 
exists (curry (match_ptype o f) ps) 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

151 
(map snd (filter (curry (op =) s o fst) consts)) 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

152 
fun pconst_hyper_mem f const_tab (s, ps) = 
38939  153 
exists (curry (match_ptype o f) ps) (these (Symtab.lookup const_tab s)) 
37505
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

154 

38939  155 
fun pattern_for_type (Type (s, Ts)) = PApp (s, map pattern_for_type Ts) 
156 
 pattern_for_type (TFree (s, _)) = PApp (s, []) 

157 
 pattern_for_type (TVar _) = PVar 

38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

158 

cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

159 
fun pterm thy t = 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

160 
case strip_comb t of 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

161 
(Const x, ts) => PApp (pconst thy true x ts) 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

162 
 (Free x, ts) => PApp (pconst thy false x ts) 
39367  163 
 (Var _, []) => PVar 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

164 
 _ => PApp ("?", []) (* equivalence class of higherorder constructs *) 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

165 
(* Pairs a constant with the list of its type instantiations. *) 
38939  166 
and ptype thy const x ts = 
167 
(if const then map pattern_for_type (these (try (Sign.const_typargs thy) x)) 

38828
91ad85f962c4
turn off experimental feature per default + avoid exception on "theory constant"
blanchet
parents:
38827
diff
changeset

168 
else []) @ 
38889  169 
(if term_patterns then map (pterm thy) ts else []) 
38939  170 
and pconst thy const (s, T) ts = (s, ptype thy const (s, T) ts) 
171 
and rich_ptype thy const (s, T) ts = 

172 
PType (order_of_type T, ptype thy const (s, T) ts) 

173 
and rich_pconst thy const (s, T) ts = (s, rich_ptype thy const (s, T) ts) 

24287  174 

38939  175 
fun string_for_hyper_pconst (s, ps) = 
176 
s ^ "{" ^ commas (map string_for_ptype ps) ^ "}" 

24287  177 

38822
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

178 
(* These are typically simplified away by "Meson.presimplify". Equality is 
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

179 
handled specially via "fequal". *) 
38682  180 
val boring_consts = 
38822
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

181 
[@{const_name False}, @{const_name True}, @{const_name If}, @{const_name Let}, 
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
38829
diff
changeset

182 
@{const_name HOL.eq}] 
37537  183 

38823
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

184 
(* Add a pconstant to the table, but a [] entry means a standard 
38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

185 
connective, which we ignore.*) 
38939  186 
fun add_pconst_to_table also_skolem (c, p) = 
38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

187 
if member (op =) boring_consts c orelse 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

188 
(not also_skolem andalso String.isPrefix skolem_prefix c) then 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

189 
I 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

190 
else 
38939  191 
Symtab.map_default (c, [p]) (insert (op =) p) 
38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

192 

71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

193 
fun is_formula_type T = (T = HOLogic.boolT orelse T = propT) 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

194 

38906  195 
fun pconsts_in_terms thy also_skolems pos ts = 
37505
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

196 
let 
38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

197 
val flip = Option.map not 
38587
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

198 
(* We include free variables, as well as constants, to handle locales. For 
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

199 
each quantifiers that must necessarily be skolemized by the ATP, we 
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

200 
introduce a fresh constant to simulate the effect of Skolemization. *) 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

201 
fun do_const const (s, T) ts = 
38939  202 
add_pconst_to_table also_skolems (rich_pconst thy const (s, T) ts) 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

203 
#> fold do_term ts 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

204 
and do_term t = 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

205 
case strip_comb t of 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

206 
(Const x, ts) => do_const true x ts 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

207 
 (Free x, ts) => do_const false x ts 
38939  208 
 (Abs (_, T, t'), ts) => 
209 
(null ts 

210 
? add_pconst_to_table true (abs_name, PType (order_of_type T + 1, []))) 

38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

211 
#> fold do_term (t' :: ts) 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

212 
 (_, ts) => fold do_term ts 
38939  213 
fun do_quantifier will_surely_be_skolemized abs_T body_t = 
37537  214 
do_formula pos body_t 
38747  215 
#> (if also_skolems andalso will_surely_be_skolemized then 
38939  216 
add_pconst_to_table true 
217 
(gensym skolem_prefix, PType (order_of_type abs_T, [])) 

38587
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

218 
else 
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

219 
I) 
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

220 
and do_term_or_formula T = 
38692  221 
if is_formula_type T then do_formula NONE else do_term 
37537  222 
and do_formula pos t = 
223 
case t of 

38939  224 
Const (@{const_name all}, _) $ Abs (_, T, t') => 
225 
do_quantifier (pos = SOME false) T t' 

37537  226 
 @{const "==>"} $ t1 $ t2 => 
227 
do_formula (flip pos) t1 #> do_formula pos t2 

228 
 Const (@{const_name "=="}, Type (_, [T, _])) $ t1 $ t2 => 

38587
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

229 
fold (do_term_or_formula T) [t1, t2] 
37537  230 
 @{const Trueprop} $ t1 => do_formula pos t1 
231 
 @{const Not} $ t1 => do_formula (flip pos) t1 

38939  232 
 Const (@{const_name All}, _) $ Abs (_, T, t') => 
233 
do_quantifier (pos = SOME false) T t' 

234 
 Const (@{const_name Ex}, _) $ Abs (_, T, t') => 

235 
do_quantifier (pos = SOME true) T t' 

38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

236 
 @{const HOL.conj} $ t1 $ t2 => fold (do_formula pos) [t1, t2] 
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

237 
 @{const HOL.disj} $ t1 $ t2 => fold (do_formula pos) [t1, t2] 
38786
e46e7a9cb622
formerly unnamed infix impliciation now named HOL.implies
haftmann
parents:
38752
diff
changeset

238 
 @{const HOL.implies} $ t1 $ t2 => 
37537  239 
do_formula (flip pos) t1 #> do_formula pos t2 
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
38829
diff
changeset

240 
 Const (@{const_name HOL.eq}, Type (_, [T, _])) $ t1 $ t2 => 
38587
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

241 
fold (do_term_or_formula T) [t1, t2] 
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

242 
 Const (@{const_name If}, Type (_, [_, Type (_, [T, _])])) 
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

243 
$ t1 $ t2 $ t3 => 
1317657d6aa9
fix the relevance filter so that it ignores If, Ex1, Ball, Bex
blanchet
parents:
38395
diff
changeset

244 
do_formula NONE t1 #> fold (do_term_or_formula T) [t2, t3] 
38939  245 
 Const (@{const_name Ex1}, _) $ Abs (_, T, t') => 
246 
do_quantifier (is_some pos) T t' 

247 
 Const (@{const_name Ball}, _) $ t1 $ Abs (_, T, t') => 

248 
do_quantifier (pos = SOME false) T 

249 
(HOLogic.mk_imp (incr_boundvars 1 t1 $ Bound 0, t')) 

250 
 Const (@{const_name Bex}, _) $ t1 $ Abs (_, T, t') => 

251 
do_quantifier (pos = SOME true) T 

252 
(HOLogic.mk_conj (incr_boundvars 1 t1 $ Bound 0, t')) 

37537  253 
 (t0 as Const (_, @{typ bool})) $ t1 => 
254 
do_term t0 #> do_formula pos t1 (* theory constant *) 

255 
 _ => do_term t 

38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

256 
in Symtab.empty > fold (do_formula pos) ts end 
24287  257 

258 
(*Inserts a dummy "constant" referring to the theory name, so that relevance 

259 
takes the given theory into account.*) 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

260 
fun theory_const_prop_of ({theory_const_rel_weight, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

261 
theory_const_irrel_weight, ...} : relevance_fudge) 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

262 
th = 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

263 
if exists (curry (op <) 0.0) [theory_const_rel_weight, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

264 
theory_const_irrel_weight] then 
37505
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

265 
let 
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

266 
val name = Context.theory_name (theory_of_thm th) 
38992
542474156c66
introduce fudge factors to deal with "theory const"
blanchet
parents:
38988
diff
changeset

267 
val t = Const (name ^ theory_const_suffix, @{typ bool}) 
37505
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

268 
in t $ prop_of th end 
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

269 
else 
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

270 
prop_of th 
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

271 

24287  272 
(**** Constant / Type Frequencies ****) 
273 

38743  274 
(* A twodimensional symbol table counts frequencies of constants. It's keyed 
275 
first by constant name and second by its list of type instantiations. For the 

38823
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

276 
latter, we need a linear ordering on "pattern list". *) 
24287  277 

38823
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

278 
fun pattern_ord p = 
38743  279 
case p of 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

280 
(PVar, PVar) => EQUAL 
38823
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

281 
 (PVar, PApp _) => LESS 
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

282 
 (PApp _, PVar) => GREATER 
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

283 
 (PApp q1, PApp q2) => 
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

284 
prod_ord fast_string_ord (dict_ord pattern_ord) (q1, q2) 
38939  285 
fun ptype_ord (PType p, PType q) = 
286 
prod_ord (dict_ord pattern_ord) int_ord (swap p, swap q) 

24287  287 

38939  288 
structure PType_Tab = Table(type key = ptype val ord = ptype_ord) 
24287  289 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

290 
fun count_axiom_consts thy fudge = 
37503
c2dfa26b9da6
cosmetics + prevent consideration of inlined Skolem terms in relevance filter
blanchet
parents:
37502
diff
changeset

291 
let 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

292 
fun do_const const (s, T) ts = 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

293 
(* Twodimensional table update. Constant maps to types maps to count. *) 
38939  294 
PType_Tab.map_default (rich_ptype thy const (s, T) ts, 0) (Integer.add 1) 
295 
> Symtab.map_default (s, PType_Tab.empty) 

38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

296 
#> fold do_term ts 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

297 
and do_term t = 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

298 
case strip_comb t of 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

299 
(Const x, ts) => do_const true x ts 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

300 
 (Free x, ts) => do_const false x ts 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

301 
 (Abs (_, _, t'), ts) => fold do_term (t' :: ts) 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

302 
 (_, ts) => fold do_term ts 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

303 
in do_term o theory_const_prop_of fudge o snd end 
24287  304 

305 

306 
(**** Actual Filtering Code ****) 

307 

39367  308 
fun pow_int _ 0 = 1.0 
38939  309 
 pow_int x 1 = x 
310 
 pow_int x n = if n > 0 then x * pow_int x (n  1) else pow_int x (n + 1) / x 

311 

24287  312 
(*The frequency of a constant is the sum of those of all instances of its type.*) 
38824  313 
fun pconst_freq match const_tab (c, ps) = 
38939  314 
PType_Tab.fold (fn (qs, m) => match (ps, qs) ? Integer.add m) 
315 
(the (Symtab.lookup const_tab c)) 0 

38686  316 

24287  317 

38085
cc44e887246c
avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents:
38027
diff
changeset

318 
(* A surprising number of theorems contain only a few significant constants. 
cc44e887246c
avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents:
38027
diff
changeset

319 
These include all induction rules, and other general theorems. *) 
37503
c2dfa26b9da6
cosmetics + prevent consideration of inlined Skolem terms in relevance filter
blanchet
parents:
37502
diff
changeset

320 

c2dfa26b9da6
cosmetics + prevent consideration of inlined Skolem terms in relevance filter
blanchet
parents:
37502
diff
changeset

321 
(* "log" seems best in practice. A constant function of one ignores the constant 
38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

322 
frequencies. Rare constants give more points if they are relevant than less 
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

323 
rare ones. *) 
39367  324 
fun rel_weight_for _ freq = 1.0 + 2.0 / Math.ln (Real.fromInt freq + 1.0) 
38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

325 

2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

326 
(* Irrelevant constants are treated differently. We associate lower penalties to 
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

327 
very rare constants and very common ones  the former because they can't 
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

328 
lead to the inclusion of too many new facts, and the latter because they are 
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

329 
so common as to be of little interest. *) 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

330 
fun irrel_weight_for ({worse_irrel_freq, higher_order_irrel_weight, ...} 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

331 
: relevance_fudge) order freq = 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

332 
let val (k, x) = worse_irrel_freq > `Real.ceil in 
38939  333 
(if freq < k then Math.ln (Real.fromInt (freq + 1)) / Math.ln x 
334 
else rel_weight_for order freq / rel_weight_for order k) 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

335 
* pow_int higher_order_irrel_weight (order  1) 
38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

336 
end 
37503
c2dfa26b9da6
cosmetics + prevent consideration of inlined Skolem terms in relevance filter
blanchet
parents:
37502
diff
changeset

337 

c2dfa26b9da6
cosmetics + prevent consideration of inlined Skolem terms in relevance filter
blanchet
parents:
37502
diff
changeset

338 
(* Computes a constant's weight, as determined by its frequency. *) 
38992
542474156c66
introduce fudge factors to deal with "theory const"
blanchet
parents:
38988
diff
changeset

339 
fun generic_pconst_weight abs_weight skolem_weight theory_const_weight 
542474156c66
introduce fudge factors to deal with "theory const"
blanchet
parents:
38988
diff
changeset

340 
weight_for f const_tab (c as (s, PType (m, _))) = 
38816  341 
if s = abs_name then abs_weight 
342 
else if String.isPrefix skolem_prefix s then skolem_weight 

38992
542474156c66
introduce fudge factors to deal with "theory const"
blanchet
parents:
38988
diff
changeset

343 
else if String.isSuffix theory_const_suffix s then theory_const_weight 
38939  344 
else weight_for m (pconst_freq (match_ptype o f) const_tab c) 
38821  345 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

346 
fun rel_pconst_weight ({abs_rel_weight, theory_const_rel_weight, ...} 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

347 
: relevance_fudge) const_tab = 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

348 
generic_pconst_weight abs_rel_weight 0.0 theory_const_rel_weight 
38992
542474156c66
introduce fudge factors to deal with "theory const"
blanchet
parents:
38988
diff
changeset

349 
rel_weight_for I const_tab 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

350 
fun irrel_pconst_weight (fudge as {abs_irrel_weight, skolem_irrel_weight, 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

351 
theory_const_irrel_weight, ...}) const_tab = 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

352 
generic_pconst_weight abs_irrel_weight skolem_irrel_weight 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

353 
theory_const_irrel_weight (irrel_weight_for fudge) swap 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

354 
const_tab 
24287  355 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

356 
fun locality_bonus (_ : relevance_fudge) General = 0.0 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

357 
 locality_bonus {intro_bonus, ...} Intro = intro_bonus 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

358 
 locality_bonus {elim_bonus, ...} Elim = elim_bonus 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

359 
 locality_bonus {simp_bonus, ...} Simp = simp_bonus 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

360 
 locality_bonus {local_bonus, ...} Local = local_bonus 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

361 
 locality_bonus {assum_bonus, ...} Assum = assum_bonus 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

362 
 locality_bonus {chained_bonus, ...} Chained = chained_bonus 
38751
01c4d14b2a61
add a bonus for chained facts, since they are likely to be relevant;
blanchet
parents:
38749
diff
changeset

363 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

364 
fun axiom_weight fudge loc const_tab relevant_consts axiom_consts = 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

365 
case axiom_consts > List.partition (pconst_hyper_mem I relevant_consts) 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

366 
> filter_out (pconst_hyper_mem swap relevant_consts) of 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

367 
([], _) => 0.0 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

368 
 (rel, irrel) => 
38889  369 
let 
370 
val irrel = irrel > filter_out (pconst_mem swap rel) 

38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

371 
val rel_weight = 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

372 
0.0 > fold (curry (op +) o rel_pconst_weight fudge const_tab) rel 
38889  373 
val irrel_weight = 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

374 
~ (locality_bonus fudge loc) 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

375 
> fold (curry (op +) o irrel_pconst_weight fudge const_tab) irrel 
38889  376 
val res = rel_weight / (rel_weight + irrel_weight) 
377 
in if Real.isFinite res then res else 0.0 end 

38747  378 

38904  379 
(* FIXME: experiment 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

380 
fun debug_axiom_weight fudge loc const_tab relevant_consts axiom_consts = 
38904  381 
case axiom_consts > List.partition (pconst_hyper_mem I relevant_consts) 
382 
> filter_out (pconst_hyper_mem swap relevant_consts) of 

383 
([], _) => 0.0 

384 
 (rel, irrel) => 

385 
let 

386 
val irrel = irrel > filter_out (pconst_mem swap rel) 

38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

387 
val rels_weight = 
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

388 
0.0 > fold (curry (op +) o rel_pconst_weight const_tab) rel 
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

389 
val irrels_weight = 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

390 
~ (locality_bonus fudge loc) 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

391 
> fold (curry (op +) o irrel_pconst_weight fudge const_tab) irrel 
38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

392 
val _ = tracing (PolyML.makestring ("REL: ", map (`(rel_pconst_weight const_tab)) rel)) 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

393 
val _ = tracing (PolyML.makestring ("IRREL: ", map (`(irrel_pconst_weight fudge const_tab)) irrel)) 
38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

394 
val res = rels_weight / (rels_weight + irrels_weight) 
38904  395 
in if Real.isFinite res then res else 0.0 end 
396 
*) 

397 

38823
828e68441a2f
renaming + treat "TFree" better in "pattern_for_type"
blanchet
parents:
38822
diff
changeset

398 
fun pconsts_in_axiom thy t = 
38825  399 
Symtab.fold (fn (s, pss) => fold (cons o pair s) pss) 
38906  400 
(pconsts_in_terms thy true (SOME true) [t]) [] 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

401 
fun pair_consts_axiom thy fudge axiom = 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

402 
case axiom > snd > theory_const_prop_of fudge > pconsts_in_axiom thy of 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

403 
[] => NONE 
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

404 
 consts => SOME ((axiom, consts), NONE) 
24287  405 

38699  406 
type annotated_thm = 
38939  407 
(((unit > string) * locality) * thm) * (string * ptype) list 
37505
d9af5c01dc4a
added code to optionally perform fact filtering on the original (nonCNF) formulas
blanchet
parents:
37504
diff
changeset

408 

38904  409 
fun take_most_relevant max_relevant remaining_max 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

410 
({max_imperfect, max_imperfect_exp, ...} : relevance_fudge) 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

411 
(candidates : (annotated_thm * real) list) = 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

412 
let 
38747  413 
val max_imperfect = 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

414 
Real.ceil (Math.pow (max_imperfect, 
38904  415 
Math.pow (Real.fromInt remaining_max 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

416 
/ Real.fromInt max_relevant, max_imperfect_exp))) 
38747  417 
val (perfect, imperfect) = 
38889  418 
candidates > sort (Real.compare o swap o pairself snd) 
419 
> take_prefix (fn (_, w) => w > 0.99999) 

38747  420 
val ((accepts, more_rejects), rejects) = 
421 
chop max_imperfect imperfect >> append perfect >> chop remaining_max 

38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

422 
in 
38889  423 
trace_msg (fn () => 
424 
"Actually passed (" ^ Int.toString (length accepts) ^ " of " ^ 

425 
Int.toString (length candidates) ^ "): " ^ 

426 
(accepts > map (fn ((((name, _), _), _), weight) => 

38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

427 
name () ^ " [" ^ Real.toString weight ^ "]") 
38745
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

428 
> commas)); 
38747  429 
(accepts, more_rejects @ rejects) 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

430 
end 
24287  431 

38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

432 
fun if_empty_replace_with_locality thy axioms loc tab = 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

433 
if Symtab.is_empty tab then 
38906  434 
pconsts_in_terms thy false (SOME false) 
38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

435 
(map_filter (fn ((_, loc'), th) => 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

436 
if loc' = loc then SOME (prop_of th) else NONE) axioms) 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

437 
else 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

438 
tab 
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

439 

38997  440 
fun relevance_filter ctxt threshold0 decay max_relevant 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

441 
(fudge as {threshold_divisor, ridiculous_threshold, ...}) 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

442 
({add, del, ...} : relevance_override) axioms goal_ts = 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

443 
let 
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

444 
val thy = ProofContext.theory_of ctxt 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

445 
val const_tab = fold (count_axiom_consts thy fudge) axioms Symtab.empty 
38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

446 
val goal_const_tab = 
38906  447 
pconsts_in_terms thy false (SOME false) goal_ts 
38993
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

448 
> fold (if_empty_replace_with_locality thy axioms) 
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

449 
[Chained, Assum, Local] 
39012  450 
val add_ths = Attrib.eval_thms ctxt add 
451 
val del_ths = Attrib.eval_thms ctxt del 

38747  452 
fun iter j remaining_max threshold rel_const_tab hopeless hopeful = 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

453 
let 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

454 
fun game_over rejects = 
38747  455 
(* Add "add:" facts. *) 
39012  456 
if null add_ths then 
38747  457 
[] 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

458 
else 
38747  459 
map_filter (fn ((p as (_, th), _), _) => 
39012  460 
if member Thm.eq_thm add_ths th then SOME p 
38747  461 
else NONE) rejects 
38889  462 
fun relevant [] rejects [] = 
38747  463 
(* Nothing has been added this iteration. *) 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

464 
if j = 0 andalso threshold >= ridiculous_threshold then 
38747  465 
(* First iteration? Try again. *) 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

466 
iter 0 max_relevant (threshold / threshold_divisor) rel_const_tab 
38747  467 
hopeless hopeful 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

468 
else 
38747  469 
game_over (rejects @ hopeless) 
38889  470 
 relevant candidates rejects [] = 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

471 
let 
38747  472 
val (accepts, more_rejects) = 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

473 
take_most_relevant max_relevant remaining_max fudge candidates 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

474 
val rel_const_tab' = 
38745
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

475 
rel_const_tab 
38901
ee36b983ca22
fiddle with fact filter based on Mirabelle experiments
blanchet
parents:
38899
diff
changeset

476 
> fold (add_pconst_to_table false) (maps (snd o fst) accepts) 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

477 
fun is_dirty (c, _) = 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

478 
Symtab.lookup rel_const_tab' c <> Symtab.lookup rel_const_tab c 
38745
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

479 
val (hopeful_rejects, hopeless_rejects) = 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

480 
(rejects @ hopeless, ([], [])) 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

481 
> fold (fn (ax as (_, consts), old_weight) => 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

482 
if exists is_dirty consts then 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

483 
apfst (cons (ax, NONE)) 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

484 
else 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

485 
apsnd (cons (ax, old_weight))) 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

486 
>> append (more_rejects 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

487 
> map (fn (ax as (_, consts), old_weight) => 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

488 
(ax, if exists is_dirty consts then NONE 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

489 
else SOME old_weight))) 
38747  490 
val threshold = 
38822
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

491 
1.0  (1.0  threshold) 
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

492 
* Math.pow (decay, Real.fromInt (length accepts)) 
38747  493 
val remaining_max = remaining_max  length accepts 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

494 
in 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

495 
trace_msg (fn () => "New or updated constants: " ^ 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

496 
commas (rel_const_tab' > Symtab.dest 
38822
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

497 
> subtract (op =) (rel_const_tab > Symtab.dest) 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

498 
> map string_for_hyper_pconst)); 
38745
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

499 
map (fst o fst) accepts @ 
38747  500 
(if remaining_max = 0 then 
38745
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

501 
game_over (hopeful_rejects @ map (apsnd SOME) hopeless_rejects) 
ad577fd62ee4
reorganize options regarding to the relevance threshold and decay
blanchet
parents:
38744
diff
changeset

502 
else 
38747  503 
iter (j + 1) remaining_max threshold rel_const_tab' 
504 
hopeless_rejects hopeful_rejects) 

38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

505 
end 
38889  506 
 relevant candidates rejects 
39367  507 
(((ax as (((_, loc), _), axiom_consts)), cached_weight) 
38747  508 
:: hopeful) = 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

509 
let 
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

510 
val weight = 
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

511 
case cached_weight of 
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

512 
SOME w => w 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

513 
 NONE => axiom_weight fudge loc const_tab rel_const_tab 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

514 
axiom_consts 
38904  515 
(* FIXME: experiment 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

516 
val name = fst (fst (fst ax)) () 
38938
2b93dbc07778
improve weighting of irrelevant constants, based on Mirabelle experiments
blanchet
parents:
38937
diff
changeset

517 
val _ = if String.isSubstring "positive_minus" name orelse String.isSubstring "not_exp_le_zero" name then 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

518 
tracing ("*** " ^ name ^ PolyML.makestring (debug_axiom_weight fudge loc const_tab rel_const_tab axiom_consts)) 
38747  519 
else 
520 
() 

521 
*) 

38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

522 
in 
38741  523 
if weight >= threshold then 
38889  524 
relevant ((ax, weight) :: candidates) rejects hopeful 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

525 
else 
38889  526 
relevant candidates ((ax, weight) :: rejects) hopeful 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

527 
end 
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

528 
in 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

529 
trace_msg (fn () => 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

530 
"ITERATION " ^ string_of_int j ^ ": current threshold: " ^ 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

531 
Real.toString threshold ^ ", constants: " ^ 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

532 
commas (rel_const_tab > Symtab.dest 
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

533 
> filter (curry (op <>) [] o snd) 
38827
cf01645cbbce
extended relevance filter with firstorder term matching
blanchet
parents:
38825
diff
changeset

534 
> map string_for_hyper_pconst)); 
38889  535 
relevant [] [] hopeful 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

536 
end 
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

537 
in 
39012  538 
axioms > filter_out (member Thm.eq_thm del_ths o snd) 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

539 
> map_filter (pair_consts_axiom thy fudge) 
38819
71c9f61516cd
if the goal contains no constants or frees, fall back on chained facts, then on local facts, etc., instead of generating a trivial ATP problem
blanchet
parents:
38818
diff
changeset

540 
> iter 0 max_relevant threshold0 goal_const_tab [] 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

541 
> tap (fn res => trace_msg (fn () => 
38686  542 
"Total relevant: " ^ Int.toString (length res))) 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

543 
end 
24287  544 

38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

545 

24287  546 
(***************************************************************) 
19768
9afd9b9c47d0
ATP/res_clasimpset.ML has been merged into res_atp.ML.
mengj
parents:
19746
diff
changeset

547 
(* Retrieving and filtering lemmas *) 
9afd9b9c47d0
ATP/res_clasimpset.ML has been merged into res_atp.ML.
mengj
parents:
19746
diff
changeset

548 
(***************************************************************) 
9afd9b9c47d0
ATP/res_clasimpset.ML has been merged into res_atp.ML.
mengj
parents:
19746
diff
changeset

549 

33022
c95102496490
Removal of the unused atpset concept, the atp attribute and some related code.
paulson
parents:
32994
diff
changeset

550 
(*** retrieve lemmas and filter them ***) 
19768
9afd9b9c47d0
ATP/res_clasimpset.ML has been merged into res_atp.ML.
mengj
parents:
19746
diff
changeset

551 

20757
fe84fe0dfd30
Definitions produced by packages are now blacklisted.
paulson
parents:
20661
diff
changeset

552 
(*Reject theorems with names like "List.filter.filter_list_def" or 
21690
552d20ff9a95
Removal of theorem tagging, which the ATP linkup no longer requires.
paulson
parents:
21588
diff
changeset

553 
"Accessible_Part.acc.defs", as these are definitions arising from packages.*) 
20757
fe84fe0dfd30
Definitions produced by packages are now blacklisted.
paulson
parents:
20661
diff
changeset

554 
fun is_package_def a = 
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30291
diff
changeset

555 
let val names = Long_Name.explode a 
21690
552d20ff9a95
Removal of theorem tagging, which the ATP linkup no longer requires.
paulson
parents:
21588
diff
changeset

556 
in 
552d20ff9a95
Removal of theorem tagging, which the ATP linkup no longer requires.
paulson
parents:
21588
diff
changeset

557 
length names > 2 andalso 
552d20ff9a95
Removal of theorem tagging, which the ATP linkup no longer requires.
paulson
parents:
21588
diff
changeset

558 
not (hd names = "local") andalso 
552d20ff9a95
Removal of theorem tagging, which the ATP linkup no longer requires.
paulson
parents:
21588
diff
changeset

559 
String.isSuffix "_def" a orelse String.isSuffix "_defs" a 
552d20ff9a95
Removal of theorem tagging, which the ATP linkup no longer requires.
paulson
parents:
21588
diff
changeset

560 
end; 
20757
fe84fe0dfd30
Definitions produced by packages are now blacklisted.
paulson
parents:
20661
diff
changeset

561 

38937
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

562 
fun mk_fact_table f xs = 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

563 
fold (Termtab.update o `(prop_of o f)) xs Termtab.empty 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

564 
fun uniquify xs = Termtab.fold (cons o snd) (mk_fact_table snd xs) [] 
19768
9afd9b9c47d0
ATP/res_clasimpset.ML has been merged into res_atp.ML.
mengj
parents:
19746
diff
changeset

565 

37626
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

566 
(* FIXME: put other record thms here, or declare as "no_atp" *) 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

567 
val multi_base_blacklist = 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

568 
["defs", "select_defs", "update_defs", "induct", "inducts", "split", "splits", 
38682  569 
"split_asm", "cases", "ext_cases", "eq.simps", "eq.refl", "nchotomy", 
570 
"case_cong", "weak_case_cong"] 

571 
> map (prefix ".") 

37626
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

572 

1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

573 
val max_lambda_nesting = 3 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

574 

1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

575 
fun term_has_too_many_lambdas max (t1 $ t2) = 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

576 
exists (term_has_too_many_lambdas max) [t1, t2] 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

577 
 term_has_too_many_lambdas max (Abs (_, _, t)) = 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

578 
max = 0 orelse term_has_too_many_lambdas (max  1) t 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

579 
 term_has_too_many_lambdas _ _ = false 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

580 

1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

581 
(* Don't count nested lambdas at the level of formulas, since they are 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

582 
quantifiers. *) 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

583 
fun formula_has_too_many_lambdas Ts (Abs (_, T, t)) = 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

584 
formula_has_too_many_lambdas (T :: Ts) t 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

585 
 formula_has_too_many_lambdas Ts t = 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

586 
if is_formula_type (fastype_of1 (Ts, t)) then 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

587 
exists (formula_has_too_many_lambdas Ts) (#2 (strip_comb t)) 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

588 
else 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

589 
term_has_too_many_lambdas max_lambda_nesting t 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

590 

38692  591 
(* The max apply depth of any "metis" call in "Metis_Examples" (on 20071031) 
37626
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

592 
was 11. *) 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

593 
val max_apply_depth = 15 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

594 

1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

595 
fun apply_depth (f $ t) = Int.max (apply_depth f, apply_depth t + 1) 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

596 
 apply_depth (Abs (_, _, t)) = apply_depth t 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

597 
 apply_depth _ = 0 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

598 

1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

599 
fun is_formula_too_complex t = 
38085
cc44e887246c
avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents:
38027
diff
changeset

600 
apply_depth t > max_apply_depth orelse formula_has_too_many_lambdas [] t 
37626
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

601 

39946  602 
(* FIXME: Extend to "Meson" and "Metis" *) 
37543  603 
val exists_sledgehammer_const = 
37626
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

604 
exists_Const (fn (s, _) => String.isPrefix sledgehammer_prefix s) 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

605 

38904  606 
(* FIXME: make more reliable *) 
607 
val exists_low_level_class_const = 

608 
exists_Const (fn (s, _) => 

609 
String.isSubstring (Long_Name.separator ^ "class" ^ Long_Name.separator) s) 

610 

38821  611 
fun is_metastrange_theorem th = 
37626
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

612 
case head_of (concl_of th) of 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

613 
Const (a, _) => (a <> @{const_name Trueprop} andalso 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

614 
a <> @{const_name "=="}) 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

615 
 _ => false 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

616 

38821  617 
fun is_that_fact th = 
618 
String.isSuffix (Long_Name.separator ^ Obtain.thatN) (Thm.get_name_hint th) 

619 
andalso exists_subterm (fn Free (s, _) => s = Name.skolem Auto_Bind.thesisN 

620 
 _ => false) (prop_of th) 

621 

37626
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

622 
val type_has_top_sort = 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

623 
exists_subtype (fn TFree (_, []) => true  TVar (_, []) => true  _ => false) 
1146291fe718
move blacklisting completely out of the clausifier;
blanchet
parents:
37616
diff
changeset

624 

38085
cc44e887246c
avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents:
38027
diff
changeset

625 
(**** Predicates to detect unwanted facts (prolific or likely to cause 
37347
635425a442e8
show more respect for userspecified facts, even if they could lead to unsound proofs + don't throw away "unsound" theorems in "full_type" mode, since they are then sound
blanchet
parents:
37345
diff
changeset

626 
unsoundness) ****) 
21470
7c1b59ddcd56
Consolidation of code to "blacklist" unhelpful theorems, including record
paulson
parents:
21431
diff
changeset

627 

38289
74dd8dd33512
adapt "too_general_equality" blacklisting to the new FOF context, where quantifiers are sometimes present
blanchet
parents:
38279
diff
changeset

628 
(* Too general means, positive equality literal with a variable X as one 
74dd8dd33512
adapt "too_general_equality" blacklisting to the new FOF context, where quantifiers are sometimes present
blanchet
parents:
38279
diff
changeset

629 
operand, when X does not occur properly in the other operand. This rules out 
74dd8dd33512
adapt "too_general_equality" blacklisting to the new FOF context, where quantifiers are sometimes present
blanchet
parents:
38279
diff
changeset

630 
clearly inconsistent facts such as X = a  X = b, though it by no means 
74dd8dd33512
adapt "too_general_equality" blacklisting to the new FOF context, where quantifiers are sometimes present
blanchet
parents:
38279
diff
changeset

631 
guarantees soundness. *) 
21470
7c1b59ddcd56
Consolidation of code to "blacklist" unhelpful theorems, including record
paulson
parents:
21431
diff
changeset

632 

38289
74dd8dd33512
adapt "too_general_equality" blacklisting to the new FOF context, where quantifiers are sometimes present
blanchet
parents:
38279
diff
changeset

633 
(* Unwanted equalities are those between a (bound or schematic) variable that 
74dd8dd33512
adapt "too_general_equality" blacklisting to the new FOF context, where quantifiers are sometimes present
blanchet
parents:
38279
diff
changeset

634 
does not properly occur in the second operand. *) 
38607
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

635 
val is_exhaustive_finite = 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

636 
let 
38629  637 
fun is_bad_equal (Var z) t = 
638 
not (exists_subterm (fn Var z' => z = z'  _ => false) t) 

639 
 is_bad_equal (Bound j) t = not (loose_bvar1 (t, j)) 

640 
 is_bad_equal _ _ = false 

641 
fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1 

38607
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

642 
fun do_formula pos t = 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

643 
case (pos, t) of 
38615
4e1d828ee514
improve "x = A  x = B  x = C"style axiom detection
blanchet
parents:
38611
diff
changeset

644 
(_, @{const Trueprop} $ t1) => do_formula pos t1 
38607
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

645 
 (true, Const (@{const_name all}, _) $ Abs (_, _, t')) => 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

646 
do_formula pos t' 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

647 
 (true, Const (@{const_name All}, _) $ Abs (_, _, t')) => 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

648 
do_formula pos t' 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

649 
 (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) => 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

650 
do_formula pos t' 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

651 
 (_, @{const "==>"} $ t1 $ t2) => 
38629  652 
do_formula (not pos) t1 andalso 
653 
(t2 = @{prop False} orelse do_formula pos t2) 

38786
e46e7a9cb622
formerly unnamed infix impliciation now named HOL.implies
haftmann
parents:
38752
diff
changeset

654 
 (_, @{const HOL.implies} $ t1 $ t2) => 
38629  655 
do_formula (not pos) t1 andalso 
656 
(t2 = @{const False} orelse do_formula pos t2) 

38607
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

657 
 (_, @{const Not} $ t1) => do_formula (not pos) t1 
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

658 
 (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2] 
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
38786
diff
changeset

659 
 (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2] 
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
38829
diff
changeset

660 
 (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2 
38607
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

661 
 (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

662 
 _ => false 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

663 
in do_formula true end 
a2abe8c2a1c2
generalize the "too general equality" code to handle facts like "x ~= A ==> x = B"
blanchet
parents:
38606
diff
changeset

664 

38592
ae6bb801e583
bound variables can be just as evil as schematic variables and lead to unsound proofs (e.g. "all_bool_eq")
blanchet
parents:
38587
diff
changeset

665 
fun has_bound_or_var_of_type tycons = 
ae6bb801e583
bound variables can be just as evil as schematic variables and lead to unsound proofs (e.g. "all_bool_eq")
blanchet
parents:
38587
diff
changeset

666 
exists_subterm (fn Var (_, Type (s, _)) => member (op =) tycons s 
ae6bb801e583
bound variables can be just as evil as schematic variables and lead to unsound proofs (e.g. "all_bool_eq")
blanchet
parents:
38587
diff
changeset

667 
 Abs (_, Type (s, _), _) => member (op =) tycons s 
ae6bb801e583
bound variables can be just as evil as schematic variables and lead to unsound proofs (e.g. "all_bool_eq")
blanchet
parents:
38587
diff
changeset

668 
 _ => false) 
21431
ef9080e7dbbc
Outputs a minimal number of arity clauses. Tidying of blacklist, fixing the blacklisting of thm lists
paulson
parents:
21397
diff
changeset

669 

38085
cc44e887246c
avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents:
38027
diff
changeset

670 
(* Facts are forbidden to contain variables of these types. The typical reason 
37347
635425a442e8
show more respect for userspecified facts, even if they could lead to unsound proofs + don't throw away "unsound" theorems in "full_type" mode, since they are then sound
blanchet
parents:
37345
diff
changeset

671 
is that they lead to unsoundness. Note that "unit" satisfies numerous 
38085
cc44e887246c
avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents:
38027
diff
changeset

672 
equations like "?x = ()". The resulting clauses will have no type constraint, 
37347
635425a442e8
show more respect for userspecified facts, even if they could lead to unsound proofs + don't throw away "unsound" theorems in "full_type" mode, since they are then sound
blanchet
parents:
37345
diff
changeset

673 
yielding false proofs. Even "bool" leads to many unsound proofs, though only 
635425a442e8
show more respect for userspecified facts, even if they could lead to unsound proofs + don't throw away "unsound" theorems in "full_type" mode, since they are then sound
blanchet
parents:
37345
diff
changeset

674 
for higherorder problems. *) 
38592
ae6bb801e583
bound variables can be just as evil as schematic variables and lead to unsound proofs (e.g. "all_bool_eq")
blanchet
parents:
38587
diff
changeset

675 
val dangerous_types = [@{type_name unit}, @{type_name bool}, @{type_name prop}]; 
22217
a5d983f7113f
Tidying; more debugging information. New reference unwanted_types.
paulson
parents:
22193
diff
changeset

676 

38085
cc44e887246c
avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents:
38027
diff
changeset

677 
(* Facts containing variables of type "unit" or "bool" or of the form 
38290
581a402a80f0
prevent ATP thread for staying around for 1 minute if an exception occurred earlier;
blanchet
parents:
38289
diff
changeset

678 
"ALL x. x = A  x = B  x = C" are likely to lead to unsound proofs if types 
581a402a80f0
prevent ATP thread for staying around for 1 minute if an exception occurred earlier;
blanchet
parents:
38289
diff
changeset

679 
are omitted. *) 
38593  680 
fun is_dangerous_term full_types t = 
38609  681 
not full_types andalso 
38679
2cfd0777580f
destroy elim rules before checking for finite exhaustive facts
blanchet
parents:
38652
diff
changeset

682 
let val t = transform_elim_term t in 
2cfd0777580f
destroy elim rules before checking for finite exhaustive facts
blanchet
parents:
38652
diff
changeset

683 
has_bound_or_var_of_type dangerous_types t orelse 
2cfd0777580f
destroy elim rules before checking for finite exhaustive facts
blanchet
parents:
38652
diff
changeset

684 
is_exhaustive_finite t 
2cfd0777580f
destroy elim rules before checking for finite exhaustive facts
blanchet
parents:
38652
diff
changeset

685 
end 
21470
7c1b59ddcd56
Consolidation of code to "blacklist" unhelpful theorems, including record
paulson
parents:
21431
diff
changeset

686 

38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

687 
fun is_theorem_bad_for_atps full_types thm = 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

688 
let val t = prop_of thm in 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

689 
is_formula_too_complex t orelse exists_type type_has_top_sort t orelse 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

690 
is_dangerous_term full_types t orelse exists_sledgehammer_const t orelse 
38904  691 
exists_low_level_class_const t orelse is_metastrange_theorem thm orelse 
692 
is_that_fact thm 

38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

693 
end 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

694 

38937
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

695 
fun clasimpset_rules_of ctxt = 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

696 
let 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

697 
val {safeIs, safeEs, hazIs, hazEs, ...} = ctxt > claset_of > rep_cs 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

698 
val intros = safeIs @ hazIs 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

699 
val elims = map Classical.classical_rule (safeEs @ hazEs) 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

700 
val simps = ctxt > simpset_of > dest_ss > #simps > map snd 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

701 
in (mk_fact_table I intros, mk_fact_table I elims, mk_fact_table I simps) end 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

702 

39265
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

703 
fun all_prefixes_of s = 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

704 
map (fn i => String.extract (s, 0, SOME i)) (1 upto size s  1) 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

705 

c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

706 
(* This is a terrible hack. Free variables are sometimes code as "M__" when they 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

707 
are displayed as "M" and we want to avoid clashes with these. But sometimes 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

708 
it's even worse: "Ma__" encodes "M". So we simply reserve all prefixes of all 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

709 
free variables. In the worse case scenario, where the fact won't be resolved 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

710 
correctly, the user can fix it manually, e.g., by naming the fact in 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

711 
question. Ideally we would need nothing of it, but backticks just don't work 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

712 
with schematic variables. *) 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

713 
fun close_form t = 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

714 
(t, [] > Term.add_free_names t > maps all_prefixes_of) 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

715 
> fold (fn ((s, i), T) => fn (t', taken) => 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

716 
let val s' = Name.variant taken s in 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

717 
(Term.all T $ Abs (s', T, abstract_over (Var ((s, i), T), t')), 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

718 
s' :: taken) 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

719 
end) 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

720 
(Term.add_vars t [] > sort_wrt (fst o fst)) 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

721 
> fst 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

722 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

723 
fun all_name_thms_pairs ctxt reserved full_types 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

724 
({intro_bonus, elim_bonus, simp_bonus, ...} : relevance_fudge) add_ths 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

725 
chained_ths = 
38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

726 
let 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

727 
val thy = ProofContext.theory_of ctxt 
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is globalonly;
wenzelm
parents:
39367
diff
changeset

728 
val global_facts = Global_Theory.facts_of thy 
38644
25bbbaf7ce65
don't penalize abstractions in relevance filter + support nameless `foo`style facts
blanchet
parents:
38629
diff
changeset

729 
val local_facts = ProofContext.facts_of ctxt 
25bbbaf7ce65
don't penalize abstractions in relevance filter + support nameless `foo`style facts
blanchet
parents:
38629
diff
changeset

730 
val named_locals = local_facts > Facts.dest_static [] 
38993
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

731 
val assms = Assumption.all_assms_of ctxt 
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

732 
fun is_assum th = exists (fn ct => prop_of th aconv term_of ct) assms 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

733 
val is_chained = member Thm.eq_thm chained_ths 
38937
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

734 
val (intros, elims, simps) = 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

735 
if exists (curry (op <) 0.0) [intro_bonus, elim_bonus, simp_bonus] then 
38937
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

736 
clasimpset_rules_of ctxt 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

737 
else 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

738 
(Termtab.empty, Termtab.empty, Termtab.empty) 
38738
0ce517c1970f
make sure that "undo_ascii_of" is the inverse of "ascii_of", also for nonprintable characters  and avoid those in ``style facts
blanchet
parents:
38699
diff
changeset

739 
fun is_good_unnamed_local th = 
38820
d0f98bd81a85
add nameless chained facts to the pool of things known to Sledgehammer
blanchet
parents:
38819
diff
changeset

740 
not (Thm.has_name_hint th) andalso 
38738
0ce517c1970f
make sure that "undo_ascii_of" is the inverse of "ascii_of", also for nonprintable characters  and avoid those in ``style facts
blanchet
parents:
38699
diff
changeset

741 
forall (fn (_, ths) => not (member Thm.eq_thm ths th)) named_locals 
38644
25bbbaf7ce65
don't penalize abstractions in relevance filter + support nameless `foo`style facts
blanchet
parents:
38629
diff
changeset

742 
val unnamed_locals = 
38820
d0f98bd81a85
add nameless chained facts to the pool of things known to Sledgehammer
blanchet
parents:
38819
diff
changeset

743 
union Thm.eq_thm (Facts.props local_facts) chained_ths 
d0f98bd81a85
add nameless chained facts to the pool of things known to Sledgehammer
blanchet
parents:
38819
diff
changeset

744 
> filter is_good_unnamed_local > map (pair "" o single) 
38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

745 
val full_space = 
38738
0ce517c1970f
make sure that "undo_ascii_of" is the inverse of "ascii_of", also for nonprintable characters  and avoid those in ``style facts
blanchet
parents:
38699
diff
changeset

746 
Name_Space.merge (Facts.space_of global_facts, Facts.space_of local_facts) 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

747 
fun add_facts global foldx facts = 
38699  748 
foldx (fn (name0, ths) => 
749 
if name0 <> "" andalso 

39012  750 
forall (not o member Thm.eq_thm add_ths) ths andalso 
38699  751 
(Facts.is_concealed facts name0 orelse 
752 
(respect_no_atp andalso is_package_def name0) orelse 

753 
exists (fn s => String.isSuffix s name0) multi_base_blacklist orelse 

754 
String.isSuffix "_def_raw" (* FIXME: crude hack *) name0) then 

38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

755 
I 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

756 
else 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

757 
let 
38699  758 
val multi = length ths > 1 
38696
4c6b65d6a135
quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents:
38692
diff
changeset

759 
fun backquotify th = 
39718  760 
"`" ^ Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN) 
39265
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

761 
(print_mode_value ())) 
c09c150f6af7
allow Sledgehammer proofs containing nameless local facts with schematic variables in them
blanchet
parents:
39264
diff
changeset

762 
(Syntax.string_of_term ctxt) (close_form (prop_of th)) ^ "`" 
38738
0ce517c1970f
make sure that "undo_ascii_of" is the inverse of "ascii_of", also for nonprintable characters  and avoid those in ``style facts
blanchet
parents:
38699
diff
changeset

763 
> String.translate (fn c => if Char.isPrint c then str c else "") 
0ce517c1970f
make sure that "undo_ascii_of" is the inverse of "ascii_of", also for nonprintable characters  and avoid those in ``style facts
blanchet
parents:
38699
diff
changeset

764 
> simplify_spaces 
38699  765 
fun check_thms a = 
766 
case try (ProofContext.get_thms ctxt) a of 

767 
NONE => false 

768 
 SOME ths' => Thm.eq_thms (ths, ths') 

38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

769 
in 
38699  770 
pair 1 
771 
#> fold (fn th => fn (j, rest) => 

772 
(j + 1, 

773 
if is_theorem_bad_for_atps full_types th andalso 

39012  774 
not (member Thm.eq_thm add_ths th) then 
38699  775 
rest 
776 
else 

38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

777 
(((fn () => 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

778 
if name0 = "" then 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

779 
th > backquotify 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

780 
else 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

781 
let 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

782 
val name1 = Facts.extern facts name0 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

783 
val name2 = Name_Space.extern full_space name0 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

784 
in 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

785 
case find_first check_thms [name1, name2, name0] of 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

786 
SOME name => repair_name reserved multi j name 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

787 
 NONE => "" 
38937
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

788 
end), 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

789 
let val t = prop_of th in 
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

790 
if is_chained th then Chained 
38993
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

791 
else if global then 
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

792 
if Termtab.defined intros t then Intro 
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

793 
else if Termtab.defined elims t then Elim 
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

794 
else if Termtab.defined simps t then Simp 
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

795 
else General 
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

796 
else 
504b9e1efd33
give priority to assumptions in structured proofs
blanchet
parents:
38992
diff
changeset

797 
if is_assum th then Assum else Local 
38937
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

798 
end), 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

799 
(multi, th)) :: rest)) ths 
38699  800 
#> snd 
38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

801 
end) 
38644
25bbbaf7ce65
don't penalize abstractions in relevance filter + support nameless `foo`style facts
blanchet
parents:
38629
diff
changeset

802 
in 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

803 
[] > add_facts false fold local_facts (unnamed_locals @ named_locals) 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

804 
> add_facts true Facts.fold_static global_facts global_facts 
38644
25bbbaf7ce65
don't penalize abstractions in relevance filter + support nameless `foo`style facts
blanchet
parents:
38629
diff
changeset

805 
end 
38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

806 

760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

807 
(* The singlename theorems go after the multiplename ones, so that single 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

808 
names are preferred when both are available. *) 
38699  809 
fun name_thm_pairs ctxt respect_no_atp = 
38744
2b6333f78a9e
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
blanchet
parents:
38743
diff
changeset

810 
List.partition (fst o snd) #> op @ #> map (apsnd snd) 
38699  811 
#> respect_no_atp ? filter_out (No_ATPs.member ctxt o snd) 
38627
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

812 

760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

813 
(***************************************************************) 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

814 
(* ATP invocation methods setup *) 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

815 
(***************************************************************) 
760a2d5cc671
make sure minimizer facts go through "transform_elim_theorems"
blanchet
parents:
38617
diff
changeset

816 

40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

817 
fun relevant_facts ctxt full_types (threshold0, threshold1) max_relevant fudge 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

818 
(override as {add, only, ...}) chained_ths hyp_ts concl_t = 
37538
97ab019d5ac8
make sure that theorems passed using "add:" to Sledgehammer are not eliminated on heuristic grounds
blanchet
parents:
37537
diff
changeset

819 
let 
38822
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

820 
val decay = Math.pow ((1.0  threshold1) / (1.0  threshold0), 
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

821 
1.0 / Real.fromInt (max_relevant + 1)) 
39012  822 
val add_ths = Attrib.eval_thms ctxt add 
38696
4c6b65d6a135
quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents:
38692
diff
changeset

823 
val reserved = reserved_isar_keyword_table () 
37538
97ab019d5ac8
make sure that theorems passed using "add:" to Sledgehammer are not eliminated on heuristic grounds
blanchet
parents:
37537
diff
changeset

824 
val axioms = 
38699  825 
(if only then 
38752
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

826 
maps (map (fn ((name, loc), th) => ((K name, loc), (true, th))) 
6628adcae4a7
consider "locality" when assigning weights to facts
blanchet
parents:
38751
diff
changeset

827 
o name_thm_pairs_from_ref ctxt reserved chained_ths) add 
38699  828 
else 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

829 
all_name_thms_pairs ctxt reserved full_types fudge add_ths chained_ths) 
38688  830 
> name_thm_pairs ctxt (respect_no_atp andalso not only) 
38937
1b1a2f5ccd7d
take into consideration whether a fact is an "intro"/"elim"/"simp" rule as an additional factor influencing the relevance filter
blanchet
parents:
38907
diff
changeset

831 
> uniquify 
37538
97ab019d5ac8
make sure that theorems passed using "add:" to Sledgehammer are not eliminated on heuristic grounds
blanchet
parents:
37537
diff
changeset

832 
in 
38688  833 
trace_msg (fn () => "Considering " ^ Int.toString (length axioms) ^ 
834 
" theorems"); 

39366
f58fbb959826
handle relevance filter corner cases more gracefully;
blanchet
parents:
39265
diff
changeset

835 
(if only orelse threshold1 < 0.0 then 
f58fbb959826
handle relevance filter corner cases more gracefully;
blanchet
parents:
39265
diff
changeset

836 
axioms 
f58fbb959826
handle relevance filter corner cases more gracefully;
blanchet
parents:
39265
diff
changeset

837 
else if threshold0 > 1.0 orelse threshold0 > threshold1 orelse 
f58fbb959826
handle relevance filter corner cases more gracefully;
blanchet
parents:
39265
diff
changeset

838 
max_relevant = 0 then 
38739
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

839 
[] 
8b8ed80b5699
renamed "relevance_convergence" to "relevance_decay"
blanchet
parents:
38738
diff
changeset

840 
else 
40070
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

841 
relevance_filter ctxt threshold0 decay max_relevant fudge override axioms 
bdb890782d4a
replaced references with proper record that's threaded through
blanchet
parents:
39958
diff
changeset

842 
(concl_t :: hyp_ts)) 
38822
aa0101e618e2
fix threshold computation + remove "op =" from relevant constants
blanchet
parents:
38821
diff
changeset

843 
> map (apfst (apfst (fn f => f ()))) 
37538
97ab019d5ac8
make sure that theorems passed using "add:" to Sledgehammer are not eliminated on heuristic grounds
blanchet
parents:
37537
diff
changeset

844 
end 
30536
07b4f050e4df
split relevancefilter and writing of problemfiles;
immler@in.tum.de
parents:
30364
diff
changeset

845 

15347  846 
end; 