author  wenzelm 
Mon, 20 Jun 2005 22:13:59 +0200  
changeset 16486  1a12cdb6ee6b 
parent 16088  f084ba24de29 
child 16895  df67fc190e06 
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 

16088  65 

66 
(* matching theorems *) 

67 

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

69 
let 

70 
val sg = ProofContext.sign_of ctxt; 

71 
val tsig = Sign.tsig_of sg; 

72 

16486  73 
val is_nontrivial = is_Const o head_of o ObjectLogic.drop_judgment sg; 
16088  74 

16486  75 
fun matches pat = 
76 
is_nontrivial pat andalso 

16088  77 
Pattern.matches tsig (if po then (pat,obj) else (obj,pat)) 
78 
handle Pattern.MATCH => false; 

79 

80 
val match_thm = matches o extract_term o Thm.prop_of 

16486  81 
in 
16088  82 
List.exists match_thm (extract_thms thm) 
83 
end; 

84 

85 

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

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

87 

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

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

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

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

91 
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

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

93 

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

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

95 
substring match only (no regexps are performed)*) 
16486  96 
fun filter_name str_pat (thmref, _) = is_substring str_pat (PureThy.name_of_thmref thmref); 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

97 

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

98 

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

100 

16036  101 
local 
102 

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

104 
fun is_elim ctxt thm = 

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

105 
let 
16036  106 
val sg = ProofContext.sign_of ctxt; 
107 
val prop = Thm.prop_of thm; 

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

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

110 
val prem_vars = Drule.vars_of_terms major_prem; 

111 
in 

112 
not (null major_prem) andalso 

113 
Term.is_Var concl andalso 

114 
not (Term.dest_Var concl mem prem_vars) 

115 
end; 

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

116 

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

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

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

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

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

122 
val prems = Logic.prems_of_goal goal 1; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

123 
in 
16486  124 
prems > 
125 
List.exists (fn prem => 

16088  126 
is_matching_thm extract_elim ctxt true prem thm 
127 
andalso (check_thm ctxt) thm) 

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

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

129 

16036  130 
in 
131 

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

133 
let 
16036  134 
val extract_intro = (single, Logic.strip_imp_concl); 
135 
val concl = Logic.concl_of_goal goal 1; 

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

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

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

139 

16036  140 
fun filter_elim ctxt = filter_elim_dest is_elim ctxt; 
141 
fun filter_dest ctxt = filter_elim_dest (not oo is_elim) ctxt; 

142 

143 
end; 

144 

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

145 

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

147 

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

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

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

151 
MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt); 
16074  152 
val extract_simp = (mk, #1 o Logic.dest_equals o Logic.strip_imp_concl); 
16088  153 
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

154 

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

155 

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

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

157 

16088  158 
fun filter_pattern ctxt pat (_, thm) = 
16036  159 
let val tsig = Sign.tsig_of (ProofContext.sign_of ctxt) 
16088  160 
in Pattern.matches_subterm tsig (pat, Thm.prop_of thm) end; 
16033
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

161 

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

162 

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

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

164 

16036  165 
local 
166 

167 
fun err_no_goal c = 

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

169 

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

170 
fun filter_crit _ _ (Name name) = filter_name name 
16036  171 
 filter_crit _ NONE Intro = err_no_goal "intro" 
172 
 filter_crit _ NONE Elim = err_no_goal "elim" 

173 
 filter_crit _ NONE Dest = err_no_goal "dest" 

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

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

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

16088  177 
 filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat 
178 
 filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat; 

16036  179 

180 
in 

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 
fun filter_criterion ctxt opt_goal (b, c) = 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

183 
(if b then I else not) o filter_crit ctxt opt_goal c; 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

184 

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

185 
fun all_filters filters = List.filter (fn x => List.all (fn f => f x) filters); 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

186 

16036  187 
end; 
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 
(* print_theorems *) 
f93ca3d4ffa7
Retrieve theorems from proof context  improved version of
wenzelm
parents:
diff
changeset

191 

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

193 
let 
16036  194 
val criteria = map (apsnd (read_criterion ctxt)) raw_criteria; 
195 
val filters = map (filter_criterion ctxt opt_goal) criteria; 

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

196 

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

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

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

200 

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

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

203 
in 
16036  204 
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

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

206 
else 
16036  207 
[Pretty.str ("found " ^ string_of_int len ^ " theorems" ^ 
208 
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"), 

209 
Pretty.str ""] @ 

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

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

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

212 
end; 
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 
end; 