author  kleing 
Wed, 20 Jul 2005 07:40:23 +0200  
changeset 16895  df67fc190e06 
parent 16486  1a12cdb6ee6b 
child 16964  6a25e42eaff5 
permissions  rwrr 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

1 
(* Title: Pure/Isar/find_theorems.ML 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

2 
ID: $Id$ 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

3 
Author: Rafal Kolanski, NICTA and Tobias Nipkow, TU Muenchen 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

4 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

5 
Retrieve theorems from proof context. 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

6 
*) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

7 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

8 
val thms_containing_limit = ref 40; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

9 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

10 
signature FIND_THEOREMS = 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

11 
sig 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

12 
val find_thms: Proof.context > FactIndex.spec > (thmref * thm) list 
16036  13 
datatype 'term criterion = 
16074  14 
Name of string  Intro  Elim  Dest  Simp of 'term  Pattern of 'term 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

15 
val print_theorems: Proof.context > term option > int option > 
16036  16 
(bool * string criterion) list > unit 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

17 
end; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

18 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

19 
structure FindTheorems: FIND_THEOREMS = 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

20 
struct 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

21 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

22 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

23 
(* find_thms *) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

24 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

25 
fun find_thms ctxt spec = 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

26 
(PureThy.thms_containing (ProofContext.theory_of ctxt) spec @ 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

27 
ProofContext.lthms_containing ctxt spec) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

28 
> map PureThy.selections > List.concat; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

29 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

30 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

31 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

32 
(** search criteria **) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

33 

16036  34 
datatype 'term criterion = 
16074  35 
Name of string  Intro  Elim  Dest  Simp of 'term  Pattern of 'term; 
16036  36 

37 
fun read_criterion _ (Name name) = Name name 

38 
 read_criterion _ Intro = Intro 

39 
 read_criterion _ Elim = Elim 

40 
 read_criterion _ Dest = Dest 

16486  41 
 read_criterion ctxt (Simp str) = 
16088  42 
Simp (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str])) 
16036  43 
 read_criterion ctxt (Pattern str) = 
44 
Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str])); 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

45 

16036  46 
fun pretty_criterion ctxt (b, c) = 
47 
let 

48 
fun prfx s = if b then s else "" ^ s; 

49 
in 

50 
(case c of 

51 
Name name => Pretty.str (prfx "name: " ^ quote name) 

52 
 Intro => Pretty.str (prfx "intro") 

53 
 Elim => Pretty.str (prfx "elim") 

54 
 Dest => Pretty.str (prfx "dest") 

16088  55 
 Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1, 
56 
Pretty.quote (ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat))] 

16036  57 
 Pattern pat => Pretty.enclose (prfx " \"") "\"" 
58 
[ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat)]) 

59 
end; 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

60 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

61 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

62 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

63 
(** search criterion filters **) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

64 

16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

65 
(*generated filters are to be of the form 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

66 
input: (PureThy.thmref * Thm.thm) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

67 
output: (p::int, s::int) option, where 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

68 
NONE indicates no match 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

69 
p is the primary sorting criterion 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

70 
(eg. number of assumptions in the theorem) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

71 
s is the secondary sorting criterion 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

72 
(eg. size of the substitution for intro, elim and dest) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

73 
when applying a set of filters to a thm, fold results in: 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

74 
(biggest p, sum of all s) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

75 
currently p and s only matter for intro, elim, dest and simp filters, 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

76 
otherwise the default ordering ("by package") is used. 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

77 
*) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

78 

16088  79 

80 
(* matching theorems *) 

81 

82 
fun is_matching_thm (extract_thms, extract_term) ctxt po obj thm = 

83 
let 

84 
val sg = ProofContext.sign_of ctxt; 

85 
val tsig = Sign.tsig_of sg; 

86 

16486  87 
val is_nontrivial = is_Const o head_of o ObjectLogic.drop_judgment sg; 
16088  88 

16486  89 
fun matches pat = 
90 
is_nontrivial pat andalso 

16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

91 
Pattern.matches tsig (if po then (pat,obj) else (obj,pat)); 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

92 

df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

93 
fun substsize pat = 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

94 
let 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

95 
val (_,subst) = Pattern.match tsig (if po then (pat,obj) else (obj,pat)) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

96 
in Vartab.foldl (op + o apsnd (size_of_term o snd o snd)) (0, subst) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

97 
end; 
16088  98 

16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

99 
fun bestmatch [] = NONE 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

100 
 bestmatch (x :: xs) = SOME (nprems_of thm, foldl Int.min x xs); 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

101 

df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

102 
val match_thm = matches o extract_term o Thm.prop_of; 
16486  103 
in 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

104 
map (substsize o extract_term o Thm.prop_of) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

105 
(List.filter match_thm (extract_thms thm)) > bestmatch 
16088  106 
end; 
107 

108 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

109 
(* filter_name *) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

110 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

111 
fun is_substring pat str = 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

112 
if String.size pat = 0 then true 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

113 
else if String.size pat > String.size str then false 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

114 
else if String.substring (str, 0, String.size pat) = pat then true 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

115 
else is_substring pat (String.extract (str, 1, NONE)); 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

116 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

117 
(*filter that just looks for a string in the name, 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

118 
substring match only (no regexps are performed)*) 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

119 
fun filter_name str_pat (thmref, _) = 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

120 
if is_substring str_pat (PureThy.name_of_thmref thmref) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

121 
then SOME (0,0) else NONE; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

122 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

123 

16036  124 
(* filter intro/elim/dest rules *) 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

125 

16036  126 
local 
127 

128 
(*elimination rule: conclusion is a Var which does not appear in the major premise*) 

129 
fun is_elim ctxt thm = 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

130 
let 
16036  131 
val sg = ProofContext.sign_of ctxt; 
132 
val prop = Thm.prop_of thm; 

133 
val concl = ObjectLogic.drop_judgment sg (Logic.strip_imp_concl prop); 

134 
val major_prem = Library.take (1, Logic.strip_imp_prems prop); 

135 
val prem_vars = Drule.vars_of_terms major_prem; 

136 
in 

137 
not (null major_prem) andalso 

138 
Term.is_Var concl andalso 

139 
not (Term.dest_Var concl mem prem_vars) 

140 
end; 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

141 

16088  142 
fun filter_elim_dest check_thm ctxt goal (_,thm) = 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

143 
let 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

144 
val extract_elim = 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

145 
(fn thm => if Thm.no_prems thm then [] else [thm], 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

146 
hd o Logic.strip_imp_prems); 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

147 
val prems = Logic.prems_of_goal goal 1; 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

148 

df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

149 
fun try_subst prem = is_matching_thm extract_elim ctxt true prem thm; 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

150 

df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

151 
(*keep successful substitutions*) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

152 
val ss = prems > List.map try_subst 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

153 
> List.filter isSome 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

154 
> List.map (#2 o valOf); 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

155 
in 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

156 
(*if possible, keep best substitution (one with smallest size)*) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

157 
(*elim and dest rules always have assumptions, so an elim with one 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

158 
assumption is as good as an intro rule with none*) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

159 
if check_thm ctxt thm andalso not (null ss) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

160 
then SOME (nprems_of thm  1, foldl Int.min (hd ss) (tl ss)) else NONE 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

161 
end; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

162 

16036  163 
in 
164 

16088  165 
fun filter_intro ctxt goal (_,thm) = 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

166 
let 
16036  167 
val extract_intro = (single, Logic.strip_imp_concl); 
168 
val concl = Logic.concl_of_goal goal 1; 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

169 
in 
16088  170 
is_matching_thm extract_intro ctxt true concl thm 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

171 
end; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

172 

16036  173 
fun filter_elim ctxt = filter_elim_dest is_elim ctxt; 
174 
fun filter_dest ctxt = filter_elim_dest (not oo is_elim) ctxt; 

175 

176 
end; 

177 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

178 

16074  179 
(* filter_simp *) 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

180 

16088  181 
fun filter_simp ctxt t (_,thm) = 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

182 
let 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

183 
val (_, {mk_rews = {mk, ...}, ...}) = 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

184 
MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt); 
16074  185 
val extract_simp = (mk, #1 o Logic.dest_equals o Logic.strip_imp_concl); 
16088  186 
in is_matching_thm extract_simp ctxt false t thm end; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

187 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

188 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

189 
(* filter_pattern *) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

190 

16088  191 
fun filter_pattern ctxt pat (_, thm) = 
16036  192 
let val tsig = Sign.tsig_of (ProofContext.sign_of ctxt) 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

193 
in if Pattern.matches_subterm tsig (pat, Thm.prop_of thm) then SOME (0,0) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

194 
else NONE end; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

195 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

196 
(* interpret criteria as filters *) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

197 

16036  198 
local 
199 

200 
fun err_no_goal c = 

201 
error ("Current goal required for " ^ c ^ " search criterion"); 

202 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

203 
fun filter_crit _ _ (Name name) = filter_name name 
16036  204 
 filter_crit _ NONE Intro = err_no_goal "intro" 
205 
 filter_crit _ NONE Elim = err_no_goal "elim" 

206 
 filter_crit _ NONE Dest = err_no_goal "dest" 

207 
 filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal 

208 
 filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal 

209 
 filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal 

16088  210 
 filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat 
211 
 filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat; 

16036  212 

16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

213 
fun opt_not x = if isSome x then NONE else SOME (0,0); 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

214 

df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

215 
fun opt_add (SOME (a,x), SOME (b,y)) = SOME ((Int.max (a,b)), (x + y)) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

216 
 opt_add _ = NONE; 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

217 

16036  218 
in 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

219 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

220 
fun filter_criterion ctxt opt_goal (b, c) = 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

221 
(if b then I else opt_not) o filter_crit ctxt opt_goal c; 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

222 

df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

223 
fun all_filters filters thms = 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

224 
let 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

225 
fun eval_filters filters thm = 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

226 
map (fn f => f thm) filters > List.foldl opt_add (SOME (0,0)); 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

227 

16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

228 
(*filters return: (number of assumptions, substitution size) option, so 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

229 
sort (desc. in both cases) according to whether a theorem has assumptions, 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

230 
then by the substitution size*) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

231 
fun thm_ord (((p0,s0),_),((p1,s1),_)) = 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

232 
prod_ord (int_ord o pairself (fn 0 => 0  x => 1)) 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

233 
int_ord ((p1,s1),(p0,s0)); 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

234 

df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

235 
val processed = List.map (fn t => (eval_filters filters t, t)) thms; 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

236 
val filtered = List.filter (isSome o #1) processed; 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

237 
in 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

238 
filtered > List.map (apfst valOf) > sort thm_ord > map #2 
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

239 
end; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

240 

16036  241 
end; 
242 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

243 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

244 
(* print_theorems *) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

245 

16036  246 
fun print_theorems ctxt opt_goal opt_limit raw_criteria = 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

247 
let 
16036  248 
val criteria = map (apsnd (read_criterion ctxt)) raw_criteria; 
249 
val filters = map (filter_criterion ctxt opt_goal) criteria; 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

250 

16036  251 
val matches = all_filters filters (find_thms ctxt ([], [])); 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

252 
val len = length matches; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

253 
val limit = if_none opt_limit (! thms_containing_limit); 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

254 

16036  255 
fun prt_fact (thmref, thm) = 
256 
ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]); 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

257 
in 
16036  258 
Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" :: 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

259 
(if null matches then [Pretty.str "nothing found"] 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

260 
else 
16036  261 
[Pretty.str ("found " ^ string_of_int len ^ " theorems" ^ 
262 
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"), 

263 
Pretty.str ""] @ 

264 
map prt_fact (Library.drop (len  limit, matches))) 

16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

265 
> Pretty.chunks > Pretty.writeln 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

266 
end; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

267 

f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

268 
end; 