author  kleing 
Tue, 20 Feb 2007 00:14:33 +0100  
changeset 22340  275802767bf3 
parent 19502  369cde91963d 
child 22343  8e0f61d05f48 
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 
16036  12 
datatype 'term criterion = 
16074  13 
Name of string  Intro  Elim  Dest  Simp of 'term  Pattern of 'term 
22340
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

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

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

17 

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

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

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

20 

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

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

22 

16036  23 
datatype 'term criterion = 
16074  24 
Name of string  Intro  Elim  Dest  Simp of 'term  Pattern of 'term; 
16036  25 

26 
fun read_criterion _ (Name name) = Name name 

27 
 read_criterion _ Intro = Intro 

28 
 read_criterion _ Elim = Elim 

29 
 read_criterion _ Dest = Dest 

16486  30 
 read_criterion ctxt (Simp str) = 
16088  31 
Simp (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str])) 
16036  32 
 read_criterion ctxt (Pattern str) = 
33 
Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str])); 

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

34 

16036  35 
fun pretty_criterion ctxt (b, c) = 
36 
let 

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

38 
in 

39 
(case c of 

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

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

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

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

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

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

48 
end; 

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

49 

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

50 

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

51 

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

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

53 

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

54 
(*generated filters are to be of the form 
17205  55 
input: (thmref * thm) 
17106  56 
output: (p:int, s:int) option, where 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

57 
NONE indicates no match 
17106  58 
p is the primary sorting criterion 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

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

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

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

62 
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

63 
(biggest p, sum of all s) 
17106  64 
currently p and s only matter for intro, elim, dest and simp filters, 
65 
otherwise the default ordering is used. 

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

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

67 

16088  68 

69 
(* matching theorems *) 

17106  70 

17205  71 
fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy; 
16088  72 

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

73 
(*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

74 
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

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

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

77 
fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src = 
16088  78 
let 
17106  79 
val thy = ProofContext.theory_of ctxt; 
16088  80 

16486  81 
fun matches pat = 
17106  82 
is_nontrivial thy pat andalso 
17205  83 
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

84 

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

85 
fun substsize pat = 
18184  86 
let val (_, subst) = 
87 
Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty) 

17205  88 
in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end; 
16088  89 

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

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

92 

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

93 
val match_thm = matches o refine_term; 
16486  94 
in 
17106  95 
map (substsize o refine_term) 
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19476
diff
changeset

96 
(filter match_thm (extract_terms term_src)) > bestmatch 
16088  97 
end; 
98 

99 

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

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

101 

17755
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

102 
fun match_string pat str = 
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

103 
let 
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

104 
fun match [] _ = true 
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

105 
 match (p :: ps) s = 
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

106 
size p <= size s andalso 
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

107 
(case try (unprefix p) s of 
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

108 
SOME s' => match ps s' 
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

109 
 NONE => match (p :: ps) (String.substring (s, 1, size s  1))); 
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

110 
in match (space_explode "*" pat) str end; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

111 

17106  112 
fun filter_name str_pat (thmref, _) = 
17755
b0cd55afead1
find_theorems: support * wildcard in name: criterion;
wenzelm
parents:
17205
diff
changeset

113 
if match_string str_pat (PureThy.name_of_thmref thmref) 
17205  114 
then SOME (0, 0) else NONE; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

115 

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

116 

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

118 

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

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

121 
val extract_dest = 
17205  122 
(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

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

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

125 

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

126 
fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm; 
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19476
diff
changeset

127 
val successful = prems > map_filter try_subst; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

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

129 
(*if possible, keep best substitution (one with smallest size)*) 
17106  130 
(*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

131 
assumption is as good as an intro rule with none*) 
17205  132 
if not (null successful) 
133 
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

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

135 

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

137 
let 
17205  138 
val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl); 
16036  139 
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

140 
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

141 
in 
18939  142 
if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

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

144 

17205  145 
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

146 
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

147 
let 
17205  148 
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

149 
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

150 
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

151 
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

152 
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

153 
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

154 
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

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

157 
is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree; 
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19476
diff
changeset

158 
val successful = prems > map_filter try_subst; 
16964
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
kleing
parents:
16895
diff
changeset

159 
in 
17106  160 
(*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

161 
assumption is as good as an intro rule with none*) 
17106  162 
if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm) 
17205  163 
andalso not (null successful) 
164 
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

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

166 
else NONE 
16036  167 

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

168 

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

170 

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

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

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

174 
MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt); 
17106  175 
val extract_simp = 
17205  176 
(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

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

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

181 

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

182 

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

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

184 

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

188 

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 
(* interpret criteria as filters *) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

191 

16036  192 
local 
193 

194 
fun err_no_goal c = 

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

196 

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

197 
fun filter_crit _ _ (Name name) = filter_name name 
16036  198 
 filter_crit _ NONE Intro = err_no_goal "intro" 
199 
 filter_crit _ NONE Elim = err_no_goal "elim" 

200 
 filter_crit _ NONE Dest = err_no_goal "dest" 

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

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

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

16088  204 
 filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat 
205 
 filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat; 

16036  206 

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

208 

17756  209 
fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int) 
17205  210 
 opt_add _ _ = NONE; 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

211 

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

213 

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

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

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

216 

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

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

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

219 
fun eval_filters filters thm = 
17205  220 
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

221 

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

222 
(*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

223 
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

224 
then by the substitution size*) 
17205  225 
fun thm_ord (((p0, s0), _), ((p1, s1), _)) = 
226 
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

227 
in 
17205  228 
map (`(eval_filters filters)) thms 
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19476
diff
changeset

229 
> map_filter (fn (SOME x, y) => SOME (x, y)  (NONE, _) => NONE) 
22340
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

230 
> sort thm_ord > map #2 
16895
df67fc190e06
Sort search results in order of relevance, where relevance =
kleing
parents:
16486
diff
changeset

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

232 

16036  233 
end; 
234 

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

235 

22340
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

236 
(* removing duplicates, preferring nicer names *) 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

237 

275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

238 
fun rem_thm_dups xs = 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

239 
let 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

240 
fun nicer (Fact x) (Fact y) = size x <= size y 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

241 
 nicer (Fact _) _ = true 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

242 
 nicer (PureThy.Name x) (PureThy.Name y) = size x <= size y 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

243 
 nicer (PureThy.Name _) (Fact _) = false 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

244 
 nicer (PureThy.Name _) _ = true 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

245 
 nicer (NameSelection (x,_)) (NameSelection (y,_)) = size x <= size y 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

246 
 nicer (NameSelection _) _ = false; 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

247 

275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

248 
fun is_in [] _ = NONE 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

249 
 is_in ((n,t) :: xs) t' = if eq_thm (t, t') 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

250 
then SOME (n,t) 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

251 
else is_in xs t'; 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

252 

275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

253 
fun eq ((_,t1),(_,t2)) = eq_thm (t1,t2) 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

254 

275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

255 
fun rem_d (rev_seen, []) = rev rev_seen 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

256 
 rem_d (rev_seen, (x as (n',t')) :: xs) = 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

257 
case is_in rev_seen t' of 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

258 
NONE => rem_d (x::rev_seen, xs) 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

259 
 SOME (n,t) => if nicer n n' 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

260 
then rem_d (rev_seen, xs) 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

261 
else rem_d (x::remove eq (n,t) rev_seen,xs) 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

262 

275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

263 
in rem_d ([], xs) end; 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

264 

275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

265 

275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

266 

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

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

268 

17972  269 
fun find_thms ctxt spec = 
18325
2d504ea54e5b
ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents:
18184
diff
changeset

270 
(PureThy.thms_containing (ProofContext.theory_of ctxt) spec 
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19476
diff
changeset

271 
> maps PureThy.selections) @ 
18325
2d504ea54e5b
ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents:
18184
diff
changeset

272 
(ProofContext.lthms_containing ctxt spec 
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19476
diff
changeset

273 
> maps PureThy.selections 
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18939
diff
changeset

274 
> distinct (fn ((r1, th1), (r2, th2)) => 
18325
2d504ea54e5b
ProofContext.lthms_containing: suppress obvious duplicates;
wenzelm
parents:
18184
diff
changeset

275 
r1 = r2 andalso Drule.eq_thm_prop (th1, th2))); 
17972  276 

22340
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

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

278 
let 
16036  279 
val criteria = map (apsnd (read_criterion ctxt)) raw_criteria; 
280 
val filters = map (filter_criterion ctxt opt_goal) criteria; 

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

281 

22340
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

282 
val raw_matches = all_filters filters (find_thms ctxt ([], [])); 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

283 
val matches = if rem_dups then rem_thm_dups raw_matches else raw_matches; 
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

284 

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

285 
val len = length matches; 
19476  286 
val limit = the_default (! thms_containing_limit) opt_limit; 
22340
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

287 
val thms = Library.drop (len  limit, matches); 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

288 

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

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

291 
in 
16036  292 
Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" :: 
22340
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

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

294 
else 
16036  295 
[Pretty.str ("found " ^ string_of_int len ^ " theorems" ^ 
296 
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"), 

297 
Pretty.str ""] @ 

22340
275802767bf3
Remove duplicates from printed theorems in find_theorems
kleing
parents:
19502
diff
changeset

298 
map prt_fact thms) 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

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

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

301 

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

302 
end; 