wenzelm@16033
|
1 |
(* Title: Pure/Isar/find_theorems.ML
|
wenzelm@16033
|
2 |
ID: $Id$
|
wenzelm@16033
|
3 |
Author: Rafal Kolanski, NICTA and Tobias Nipkow, TU Muenchen
|
wenzelm@16033
|
4 |
|
wenzelm@16033
|
5 |
Retrieve theorems from proof context.
|
wenzelm@16033
|
6 |
*)
|
wenzelm@16033
|
7 |
|
wenzelm@16033
|
8 |
val thms_containing_limit = ref 40;
|
wenzelm@16033
|
9 |
|
wenzelm@16033
|
10 |
signature FIND_THEOREMS =
|
wenzelm@16033
|
11 |
sig
|
wenzelm@16033
|
12 |
val find_thms: Proof.context -> FactIndex.spec -> (thmref * thm) list
|
wenzelm@16036
|
13 |
datatype 'term criterion =
|
kleing@16074
|
14 |
Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term
|
wenzelm@16033
|
15 |
val print_theorems: Proof.context -> term option -> int option ->
|
wenzelm@16036
|
16 |
(bool * string criterion) list -> unit
|
wenzelm@16033
|
17 |
end;
|
wenzelm@16033
|
18 |
|
wenzelm@16033
|
19 |
structure FindTheorems: FIND_THEOREMS =
|
wenzelm@16033
|
20 |
struct
|
wenzelm@16033
|
21 |
|
wenzelm@16033
|
22 |
|
wenzelm@16033
|
23 |
(* find_thms *)
|
wenzelm@16033
|
24 |
|
wenzelm@16033
|
25 |
fun find_thms ctxt spec =
|
wenzelm@16033
|
26 |
(PureThy.thms_containing (ProofContext.theory_of ctxt) spec @
|
wenzelm@16033
|
27 |
ProofContext.lthms_containing ctxt spec)
|
wenzelm@16033
|
28 |
|> map PureThy.selections |> List.concat;
|
wenzelm@16033
|
29 |
|
wenzelm@16033
|
30 |
|
wenzelm@16033
|
31 |
|
wenzelm@16033
|
32 |
(** search criteria **)
|
wenzelm@16033
|
33 |
|
wenzelm@16036
|
34 |
datatype 'term criterion =
|
kleing@16074
|
35 |
Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term;
|
wenzelm@16033
|
36 |
|
wenzelm@16036
|
37 |
fun read_criterion _ (Name name) = Name name
|
wenzelm@16036
|
38 |
| read_criterion _ Intro = Intro
|
wenzelm@16036
|
39 |
| read_criterion _ Elim = Elim
|
wenzelm@16036
|
40 |
| read_criterion _ Dest = Dest
|
wenzelm@16486
|
41 |
| read_criterion ctxt (Simp str) =
|
kleing@16088
|
42 |
Simp (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]))
|
wenzelm@16036
|
43 |
| read_criterion ctxt (Pattern str) =
|
wenzelm@16036
|
44 |
Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]));
|
wenzelm@16033
|
45 |
|
wenzelm@16036
|
46 |
fun pretty_criterion ctxt (b, c) =
|
wenzelm@16036
|
47 |
let
|
wenzelm@16036
|
48 |
fun prfx s = if b then s else "-" ^ s;
|
wenzelm@16036
|
49 |
in
|
wenzelm@16036
|
50 |
(case c of
|
wenzelm@16036
|
51 |
Name name => Pretty.str (prfx "name: " ^ quote name)
|
wenzelm@16036
|
52 |
| Intro => Pretty.str (prfx "intro")
|
wenzelm@16036
|
53 |
| Elim => Pretty.str (prfx "elim")
|
wenzelm@16036
|
54 |
| Dest => Pretty.str (prfx "dest")
|
kleing@16088
|
55 |
| Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
|
kleing@16088
|
56 |
Pretty.quote (ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat))]
|
wenzelm@16036
|
57 |
| Pattern pat => Pretty.enclose (prfx " \"") "\""
|
wenzelm@16036
|
58 |
[ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat)])
|
wenzelm@16036
|
59 |
end;
|
wenzelm@16033
|
60 |
|
wenzelm@16033
|
61 |
|
wenzelm@16033
|
62 |
|
wenzelm@16033
|
63 |
(** search criterion filters **)
|
wenzelm@16033
|
64 |
|
kleing@16895
|
65 |
(*generated filters are to be of the form
|
wenzelm@17205
|
66 |
input: (thmref * thm)
|
wenzelm@17106
|
67 |
output: (p:int, s:int) option, where
|
kleing@16895
|
68 |
NONE indicates no match
|
wenzelm@17106
|
69 |
p is the primary sorting criterion
|
kleing@16895
|
70 |
(eg. number of assumptions in the theorem)
|
kleing@16895
|
71 |
s is the secondary sorting criterion
|
kleing@16895
|
72 |
(eg. size of the substitution for intro, elim and dest)
|
kleing@16895
|
73 |
when applying a set of filters to a thm, fold results in:
|
kleing@16895
|
74 |
(biggest p, sum of all s)
|
wenzelm@17106
|
75 |
currently p and s only matter for intro, elim, dest and simp filters,
|
wenzelm@17106
|
76 |
otherwise the default ordering is used.
|
kleing@16895
|
77 |
*)
|
kleing@16895
|
78 |
|
kleing@16088
|
79 |
|
kleing@16088
|
80 |
(* matching theorems *)
|
wenzelm@17106
|
81 |
|
wenzelm@17205
|
82 |
fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy;
|
kleing@16088
|
83 |
|
kleing@16964
|
84 |
(*extract terms from term_src, refine them to the parts that concern us,
|
kleing@16964
|
85 |
if po try match them against obj else vice versa.
|
kleing@16964
|
86 |
trivial matches are ignored.
|
kleing@16964
|
87 |
returns: smallest substitution size*)
|
kleing@16964
|
88 |
fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
|
kleing@16088
|
89 |
let
|
wenzelm@17106
|
90 |
val thy = ProofContext.theory_of ctxt;
|
kleing@16088
|
91 |
|
wenzelm@16486
|
92 |
fun matches pat =
|
wenzelm@17106
|
93 |
is_nontrivial thy pat andalso
|
wenzelm@17205
|
94 |
Pattern.matches thy (if po then (pat, obj) else (obj, pat));
|
kleing@16088
|
95 |
|
kleing@16895
|
96 |
fun substsize pat =
|
wenzelm@17205
|
97 |
let val (_, subst) = Pattern.match thy (if po then (pat, obj) else (obj, pat))
|
wenzelm@17205
|
98 |
in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
|
kleing@16895
|
99 |
|
kleing@16895
|
100 |
fun bestmatch [] = NONE
|
wenzelm@17205
|
101 |
| bestmatch xs = SOME (foldr1 Int.min xs);
|
kleing@16895
|
102 |
|
kleing@16964
|
103 |
val match_thm = matches o refine_term;
|
wenzelm@16486
|
104 |
in
|
wenzelm@17106
|
105 |
map (substsize o refine_term)
|
kleing@16964
|
106 |
(List.filter match_thm (extract_terms term_src)) |> bestmatch
|
kleing@16088
|
107 |
end;
|
kleing@16088
|
108 |
|
kleing@16088
|
109 |
|
wenzelm@16033
|
110 |
(* filter_name *)
|
wenzelm@16033
|
111 |
|
wenzelm@17755
|
112 |
fun match_string pat str =
|
wenzelm@17755
|
113 |
let
|
wenzelm@17755
|
114 |
fun match [] _ = true
|
wenzelm@17755
|
115 |
| match (p :: ps) s =
|
wenzelm@17755
|
116 |
size p <= size s andalso
|
wenzelm@17755
|
117 |
(case try (unprefix p) s of
|
wenzelm@17755
|
118 |
SOME s' => match ps s'
|
wenzelm@17755
|
119 |
| NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
|
wenzelm@17755
|
120 |
in match (space_explode "*" pat) str end;
|
wenzelm@16033
|
121 |
|
wenzelm@16033
|
122 |
(*filter that just looks for a string in the name,
|
wenzelm@16033
|
123 |
substring match only (no regexps are performed)*)
|
wenzelm@17106
|
124 |
fun filter_name str_pat (thmref, _) =
|
wenzelm@17755
|
125 |
if match_string str_pat (PureThy.name_of_thmref thmref)
|
wenzelm@17205
|
126 |
then SOME (0, 0) else NONE;
|
wenzelm@16033
|
127 |
|
wenzelm@16033
|
128 |
|
wenzelm@16036
|
129 |
(* filter intro/elim/dest rules *)
|
wenzelm@16033
|
130 |
|
wenzelm@17205
|
131 |
fun filter_dest ctxt goal (_, thm) =
|
wenzelm@16033
|
132 |
let
|
kleing@16964
|
133 |
val extract_dest =
|
wenzelm@17205
|
134 |
(fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm],
|
wenzelm@16033
|
135 |
hd o Logic.strip_imp_prems);
|
wenzelm@16033
|
136 |
val prems = Logic.prems_of_goal goal 1;
|
kleing@16895
|
137 |
|
kleing@16964
|
138 |
fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm;
|
wenzelm@17205
|
139 |
val successful = prems |> List.mapPartial try_subst;
|
wenzelm@16033
|
140 |
in
|
kleing@16895
|
141 |
(*if possible, keep best substitution (one with smallest size)*)
|
wenzelm@17106
|
142 |
(*dest rules always have assumptions, so a dest with one
|
kleing@16895
|
143 |
assumption is as good as an intro rule with none*)
|
wenzelm@17205
|
144 |
if not (null successful)
|
wenzelm@17205
|
145 |
then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
|
wenzelm@16033
|
146 |
end;
|
wenzelm@16033
|
147 |
|
wenzelm@17205
|
148 |
fun filter_intro ctxt goal (_, thm) =
|
wenzelm@16033
|
149 |
let
|
wenzelm@17205
|
150 |
val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl);
|
wenzelm@16036
|
151 |
val concl = Logic.concl_of_goal goal 1;
|
kleing@16964
|
152 |
val ss = is_matching_thm extract_intro ctxt true concl thm;
|
wenzelm@16033
|
153 |
in
|
wenzelm@17205
|
154 |
if is_some ss then SOME (Thm.nprems_of thm, valOf ss) else NONE
|
wenzelm@16033
|
155 |
end;
|
wenzelm@16033
|
156 |
|
wenzelm@17205
|
157 |
fun filter_elim ctxt goal (_, thm) =
|
kleing@16964
|
158 |
if not (Thm.no_prems thm) then
|
kleing@16964
|
159 |
let
|
wenzelm@17205
|
160 |
val rule = Thm.full_prop_of thm;
|
kleing@16964
|
161 |
val prems = Logic.prems_of_goal goal 1;
|
kleing@16964
|
162 |
val goal_concl = Logic.concl_of_goal goal 1;
|
kleing@16964
|
163 |
val rule_mp = (hd o Logic.strip_imp_prems) rule;
|
kleing@16964
|
164 |
val rule_concl = Logic.strip_imp_concl rule;
|
kleing@16964
|
165 |
fun combine t1 t2 = Const ("combine", dummyT --> dummyT) $ (t1 $ t2);
|
kleing@16964
|
166 |
val rule_tree = combine rule_mp rule_concl;
|
kleing@16964
|
167 |
fun goal_tree prem = (combine prem goal_concl);
|
wenzelm@17106
|
168 |
fun try_subst prem =
|
kleing@16964
|
169 |
is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
|
wenzelm@17205
|
170 |
val successful = prems |> List.mapPartial try_subst;
|
kleing@16964
|
171 |
in
|
wenzelm@17106
|
172 |
(*elim rules always have assumptions, so an elim with one
|
kleing@16964
|
173 |
assumption is as good as an intro rule with none*)
|
wenzelm@17106
|
174 |
if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm)
|
wenzelm@17205
|
175 |
andalso not (null successful)
|
wenzelm@17205
|
176 |
then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
|
kleing@16964
|
177 |
end
|
kleing@16964
|
178 |
else NONE
|
wenzelm@16036
|
179 |
|
wenzelm@16033
|
180 |
|
kleing@16074
|
181 |
(* filter_simp *)
|
wenzelm@16033
|
182 |
|
wenzelm@17205
|
183 |
fun filter_simp ctxt t (_, thm) =
|
wenzelm@16033
|
184 |
let
|
wenzelm@16033
|
185 |
val (_, {mk_rews = {mk, ...}, ...}) =
|
wenzelm@16033
|
186 |
MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
|
wenzelm@17106
|
187 |
val extract_simp =
|
wenzelm@17205
|
188 |
(map Thm.full_prop_of o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
|
kleing@16964
|
189 |
val ss = is_matching_thm extract_simp ctxt false t thm
|
wenzelm@17106
|
190 |
in
|
wenzelm@17205
|
191 |
if is_some ss then SOME (Thm.nprems_of thm, valOf ss) else NONE
|
kleing@16964
|
192 |
end;
|
wenzelm@16033
|
193 |
|
wenzelm@16033
|
194 |
|
wenzelm@16033
|
195 |
(* filter_pattern *)
|
wenzelm@16033
|
196 |
|
kleing@16088
|
197 |
fun filter_pattern ctxt pat (_, thm) =
|
wenzelm@17205
|
198 |
if Pattern.matches_subterm (ProofContext.theory_of ctxt) (pat, Thm.full_prop_of thm)
|
wenzelm@17205
|
199 |
then SOME (0, 0) else NONE;
|
wenzelm@17205
|
200 |
|
wenzelm@16033
|
201 |
|
wenzelm@16033
|
202 |
(* interpret criteria as filters *)
|
wenzelm@16033
|
203 |
|
wenzelm@16036
|
204 |
local
|
wenzelm@16036
|
205 |
|
wenzelm@16036
|
206 |
fun err_no_goal c =
|
wenzelm@16036
|
207 |
error ("Current goal required for " ^ c ^ " search criterion");
|
wenzelm@16036
|
208 |
|
wenzelm@16033
|
209 |
fun filter_crit _ _ (Name name) = filter_name name
|
wenzelm@16036
|
210 |
| filter_crit _ NONE Intro = err_no_goal "intro"
|
wenzelm@16036
|
211 |
| filter_crit _ NONE Elim = err_no_goal "elim"
|
wenzelm@16036
|
212 |
| filter_crit _ NONE Dest = err_no_goal "dest"
|
wenzelm@16036
|
213 |
| filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
|
wenzelm@16036
|
214 |
| filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
|
wenzelm@16036
|
215 |
| filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
|
kleing@16088
|
216 |
| filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat
|
kleing@16088
|
217 |
| filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
|
wenzelm@16036
|
218 |
|
wenzelm@17205
|
219 |
fun opt_not x = if isSome x then NONE else SOME (0, 0);
|
kleing@16895
|
220 |
|
wenzelm@17756
|
221 |
fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
|
wenzelm@17205
|
222 |
| opt_add _ _ = NONE;
|
kleing@16895
|
223 |
|
wenzelm@16036
|
224 |
in
|
wenzelm@16033
|
225 |
|
wenzelm@16033
|
226 |
fun filter_criterion ctxt opt_goal (b, c) =
|
kleing@16895
|
227 |
(if b then I else opt_not) o filter_crit ctxt opt_goal c;
|
wenzelm@16033
|
228 |
|
kleing@16895
|
229 |
fun all_filters filters thms =
|
kleing@16895
|
230 |
let
|
kleing@16895
|
231 |
fun eval_filters filters thm =
|
wenzelm@17205
|
232 |
fold opt_add (map (fn f => f thm) filters) (SOME (0, 0));
|
kleing@16895
|
233 |
|
kleing@16895
|
234 |
(*filters return: (number of assumptions, substitution size) option, so
|
kleing@16964
|
235 |
sort (desc. in both cases) according to number of assumptions first,
|
kleing@16895
|
236 |
then by the substitution size*)
|
wenzelm@17205
|
237 |
fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
|
wenzelm@17205
|
238 |
prod_ord int_ord int_ord ((p1, s1), (p0, s0));
|
kleing@16895
|
239 |
in
|
wenzelm@17205
|
240 |
map (`(eval_filters filters)) thms
|
wenzelm@17205
|
241 |
|> List.mapPartial (fn (SOME x, y) => SOME (x, y) | (NONE, _) => NONE)
|
wenzelm@17205
|
242 |
|> sort thm_ord |> map #2
|
kleing@16895
|
243 |
end;
|
wenzelm@16033
|
244 |
|
wenzelm@16036
|
245 |
end;
|
wenzelm@16036
|
246 |
|
wenzelm@16033
|
247 |
|
wenzelm@16033
|
248 |
(* print_theorems *)
|
wenzelm@16033
|
249 |
|
wenzelm@16036
|
250 |
fun print_theorems ctxt opt_goal opt_limit raw_criteria =
|
wenzelm@16033
|
251 |
let
|
wenzelm@16036
|
252 |
val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
|
wenzelm@16036
|
253 |
val filters = map (filter_criterion ctxt opt_goal) criteria;
|
wenzelm@16036
|
254 |
|
wenzelm@16036
|
255 |
val matches = all_filters filters (find_thms ctxt ([], []));
|
wenzelm@16036
|
256 |
val len = length matches;
|
wenzelm@16036
|
257 |
val limit = if_none opt_limit (! thms_containing_limit);
|
wenzelm@16036
|
258 |
|
wenzelm@16033
|
259 |
fun prt_fact (thmref, thm) =
|
wenzelm@16033
|
260 |
ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
|
wenzelm@16033
|
261 |
in
|
wenzelm@16036
|
262 |
Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" ::
|
wenzelm@16033
|
263 |
(if null matches then [Pretty.str "nothing found"]
|
wenzelm@16033
|
264 |
else
|
wenzelm@16036
|
265 |
[Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
|
wenzelm@16036
|
266 |
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
|
wenzelm@16036
|
267 |
Pretty.str ""] @
|
wenzelm@16036
|
268 |
map prt_fact (Library.drop (len - limit, matches)))
|
wenzelm@16033
|
269 |
|> Pretty.chunks |> Pretty.writeln
|
wenzelm@16033
|
270 |
end;
|
wenzelm@16033
|
271 |
|
wenzelm@16033
|
272 |
end;
|