author  wenzelm 
Wed, 31 Aug 2005 15:46:44 +0200  
changeset 17205  8994750ae33c 
parent 17106  2bd6c20cdda1 
child 17755  b0cd55afead1 
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 
17205  66 
input: (thmref * thm) 
17106  67 
output: (p:int, s:int) option, where 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

68 
NONE indicates no match 
17106  69 
p is the primary sorting criterion 
16895
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) 
17106  75 
currently p and s only matter for intro, elim, dest and simp filters, 
76 
otherwise the default ordering is used. 

16895
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 *) 

17106  81 

17205  82 
fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy; 
16088  83 

16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

84 
(*extract terms from term_src, refine them to the parts that concern us, 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

85 
if po try match them against obj else vice versa. 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

86 
trivial matches are ignored. 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

87 
returns: smallest substitution size*) 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

88 
fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src = 
16088  89 
let 
17106  90 
val thy = ProofContext.theory_of ctxt; 
16088  91 

16486  92 
fun matches pat = 
17106  93 
is_nontrivial thy pat andalso 
17205  94 
Pattern.matches thy (if po then (pat, obj) else (obj, pat)); 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

95 

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

96 
fun substsize pat = 
17205  97 
let val (_, subst) = Pattern.match thy (if po then (pat, obj) else (obj, pat)) 
98 
in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end; 

16088  99 

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

100 
fun bestmatch [] = NONE 
17205  101 
 bestmatch xs = SOME (foldr1 Int.min xs); 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

102 

16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

103 
val match_thm = matches o refine_term; 
16486  104 
in 
17106  105 
map (substsize o refine_term) 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

106 
(List.filter match_thm (extract_terms term_src)) > bestmatch 
16088  107 
end; 
108 

109 

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

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

111 

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

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

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

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

115 
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

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

117 

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

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

119 
substring match only (no regexps are performed)*) 
17106  120 
fun filter_name str_pat (thmref, _) = 
121 
if is_substring str_pat (PureThy.name_of_thmref thmref) 

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

123 

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

124 

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

126 

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

128 
let 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

129 
val extract_dest = 
17205  130 
(fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm], 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

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

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

133 

16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

134 
fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm; 
17205  135 
val successful = prems > List.mapPartial try_subst; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

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

137 
(*if possible, keep best substitution (one with smallest size)*) 
17106  138 
(*dest rules always have assumptions, so a dest with one 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

139 
assumption is as good as an intro rule with none*) 
17205  140 
if not (null successful) 
141 
then SOME (Thm.nprems_of thm  1, foldr1 Int.min successful) else NONE 

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

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

143 

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

145 
let 
17205  146 
val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl); 
16036  147 
val concl = Logic.concl_of_goal goal 1; 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

148 
val ss = is_matching_thm extract_intro ctxt true concl thm; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

149 
in 
17205  150 
if is_some ss then SOME (Thm.nprems_of thm, valOf ss) else NONE 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

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

152 

17205  153 
fun filter_elim ctxt goal (_, thm) = 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

154 
if not (Thm.no_prems thm) then 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

155 
let 
17205  156 
val rule = Thm.full_prop_of thm; 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

157 
val prems = Logic.prems_of_goal goal 1; 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

158 
val goal_concl = Logic.concl_of_goal goal 1; 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

159 
val rule_mp = (hd o Logic.strip_imp_prems) rule; 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

160 
val rule_concl = Logic.strip_imp_concl rule; 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

161 
fun combine t1 t2 = Const ("combine", dummyT > dummyT) $ (t1 $ t2); 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

162 
val rule_tree = combine rule_mp rule_concl; 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

163 
fun goal_tree prem = (combine prem goal_concl); 
17106  164 
fun try_subst prem = 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

165 
is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree; 
17205  166 
val successful = prems > List.mapPartial try_subst; 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

167 
in 
17106  168 
(*elim rules always have assumptions, so an elim with one 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

169 
assumption is as good as an intro rule with none*) 
17106  170 
if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm) 
17205  171 
andalso not (null successful) 
172 
then SOME (Thm.nprems_of thm  1, foldr1 Int.min successful) else NONE 

16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

173 
end 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

174 
else NONE 
16036  175 

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

176 

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

178 

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

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

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

182 
MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt); 
17106  183 
val extract_simp = 
17205  184 
(map Thm.full_prop_of o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl); 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

185 
val ss = is_matching_thm extract_simp ctxt false t thm 
17106  186 
in 
17205  187 
if is_some ss then SOME (Thm.nprems_of thm, valOf ss) else NONE 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

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

189 

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

190 

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

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

192 

16088  193 
fun filter_pattern ctxt pat (_, thm) = 
17205  194 
if Pattern.matches_subterm (ProofContext.theory_of ctxt) (pat, Thm.full_prop_of thm) 
195 
then SOME (0, 0) else NONE; 

196 

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

197 

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

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

199 

16036  200 
local 
201 

202 
fun err_no_goal c = 

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

204 

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

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

208 
 filter_crit _ NONE Dest = err_no_goal "dest" 

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

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

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

16088  212 
 filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat 
213 
 filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat; 

16036  214 

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

216 

17205  217 
fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y) 
218 
 opt_add _ _ = NONE; 

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

219 

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

221 

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

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

223 
(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

224 

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

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

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

227 
fun eval_filters filters thm = 
17205  228 
fold opt_add (map (fn f => f thm) filters) (SOME (0, 0)); 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

229 

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

230 
(*filters return: (number of assumptions, substitution size) option, so 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

231 
sort (desc. in both cases) according to number of assumptions first, 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

232 
then by the substitution size*) 
17205  233 
fun thm_ord (((p0, s0), _), ((p1, s1), _)) = 
234 
prod_ord int_ord int_ord ((p1, s1), (p0, s0)); 

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

235 
in 
17205  236 
map (`(eval_filters filters)) thms 
237 
> List.mapPartial (fn (SOME x, y) => SOME (x, y)  (NONE, _) => NONE) 

238 
> sort thm_ord > map #2 

16895
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; 