27 include BASIC_PURE_THY |
27 include BASIC_PURE_THY |
28 val get_thm_closure: theory -> xstring -> thm |
28 val get_thm_closure: theory -> xstring -> thm |
29 val get_thms_closure: theory -> xstring -> thm list |
29 val get_thms_closure: theory -> xstring -> thm list |
30 val single_thm: string -> thm list -> thm |
30 val single_thm: string -> thm list -> thm |
31 val cond_extern_thm_sg: Sign.sg -> string -> xstring |
31 val cond_extern_thm_sg: Sign.sg -> string -> xstring |
32 val thms_containing: theory -> string list -> (string * thm) list |
32 val thms_containing: theory -> string list * string list -> (string * thm list) list |
33 val hide_thms: bool -> string list -> theory -> theory |
33 val hide_thms: bool -> string list -> theory -> theory |
34 val store_thm: (bstring * thm) * theory attribute list -> theory -> theory * thm |
34 val store_thm: (bstring * thm) * theory attribute list -> theory -> theory * thm |
35 val smart_store_thms: (bstring * thm list) -> thm list |
35 val smart_store_thms: (bstring * thm list) -> thm list |
36 val smart_store_thms_open: (bstring * thm list) -> thm list |
36 val smart_store_thms_open: (bstring * thm list) -> thm list |
37 val forall_elim_var: int -> thm -> thm |
37 val forall_elim_var: int -> thm -> thm |
80 val name = "Pure/theorems"; |
80 val name = "Pure/theorems"; |
81 |
81 |
82 type T = |
82 type T = |
83 {space: NameSpace.T, |
83 {space: NameSpace.T, |
84 thms_tab: thm list Symtab.table, |
84 thms_tab: thm list Symtab.table, |
85 const_idx: int * (int * thm) list Symtab.table} ref; |
85 index: FactIndex.T} ref; |
86 |
86 |
87 fun mk_empty _ = |
87 fun mk_empty _ = |
88 ref {space = NameSpace.empty, thms_tab = Symtab.empty, const_idx = (0, Symtab.empty)} : T; |
88 ref {space = NameSpace.empty, thms_tab = Symtab.empty, index = FactIndex.empty}: T; |
89 |
89 |
90 val empty = mk_empty (); |
90 val empty = mk_empty (); |
91 fun copy (ref x) = ref x; |
91 fun copy (ref x) = ref x; |
92 val prep_ext = mk_empty; |
92 val prep_ext = mk_empty; |
93 val merge = mk_empty; |
93 val merge = mk_empty; |
94 |
94 |
95 fun pretty sg (ref {space, thms_tab, const_idx = _}) = |
95 fun pretty sg (ref {space, thms_tab, index = _}) = |
96 let |
96 let |
97 val prt_thm = Display.pretty_thm_sg sg; |
97 val prt_thm = Display.pretty_thm_sg sg; |
98 fun prt_thms (name, [th]) = |
98 fun prt_thms (name, [th]) = |
99 Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th] |
99 Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt_thm th] |
100 | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths); |
100 | prt_thms (name, ths) = Pretty.big_list (name ^ ":") (map prt_thm ths); |
167 fun get_thm thy name = single_thm name (get_thms thy name); |
167 fun get_thm thy name = single_thm name (get_thms thy name); |
168 |
168 |
169 |
169 |
170 (* thms_of *) |
170 (* thms_of *) |
171 |
171 |
172 fun attach_name thm = (Thm.name_of_thm thm, thm); |
|
173 |
|
174 fun thms_of thy = |
172 fun thms_of thy = |
175 let val ref {thms_tab, ...} = get_theorems thy in |
173 let val ref {thms_tab, ...} = get_theorems thy in |
176 map attach_name (flat (map snd (Symtab.dest thms_tab))) |
174 map (fn th => (Thm.name_of_thm th, th)) (flat (map snd (Symtab.dest thms_tab))) |
177 end; |
175 end; |
178 |
176 |
179 |
177 |
180 |
178 (* thms_containing *) |
181 (** theorems indexed by constants **) |
179 |
182 |
180 fun thms_containing thy idx = |
183 (* make index *) |
|
184 |
|
185 fun add_const_idx ((next, table), thm) = |
|
186 let |
181 let |
187 val {hyps, prop, ...} = Thm.rep_thm thm; |
182 fun valid (name, ths) = |
188 val consts = |
183 (case try (transform_error (get_thms thy)) name of |
189 foldr add_term_consts (hyps, add_term_consts (prop, [])); |
184 None => false |
190 |
185 | Some ths' => Library.equal_lists Thm.eq_thm (ths, ths')); |
191 fun add (tab, c) = |
186 in |
192 Symtab.update ((c, (next, thm) :: Symtab.lookup_multi (tab, c)), tab); |
187 (thy :: Theory.ancestors_of thy) |
193 in (next + 1, foldl add (table, consts)) end; |
188 |> map (gen_distinct eq_fst o filter valid o FactIndex.find idx o #index o ! o get_theorems) |
194 |
189 |> flat |
195 fun make_const_idx thm_tab = |
190 end; |
196 Symtab.foldl (fn (x, (_, ths)) => foldl add_const_idx (x, ths)) ((0, Symtab.empty), thm_tab); |
|
197 |
|
198 |
|
199 (* lookup index *) |
|
200 |
|
201 (*search locally*) |
|
202 fun containing [] thy = thms_of thy |
|
203 | containing consts thy = |
|
204 let |
|
205 fun int ([], _) = [] |
|
206 | int (_, []) = [] |
|
207 | int (xxs as ((x as (i:int, _)) :: xs), yys as ((y as (j, _)) :: ys)) = |
|
208 if i = j then x :: int (xs, ys) |
|
209 else if i > j then int (xs, yys) |
|
210 else int (xxs, ys); |
|
211 |
|
212 fun ints [xs] = xs |
|
213 | ints xss = if exists null xss then [] else foldl int (hd xss, tl xss); |
|
214 |
|
215 val ref {const_idx = (_, ctab), ...} = get_theorems thy; |
|
216 val ithmss = map (fn c => Symtab.lookup_multi (ctab, c)) consts; |
|
217 in map (attach_name o snd) (ints ithmss) end; |
|
218 |
|
219 (*search globally*) |
|
220 fun thms_containing thy consts = |
|
221 (case filter (is_none o Sign.const_type (Theory.sign_of thy)) consts of |
|
222 [] => flat (map (containing consts) (thy :: Theory.ancestors_of thy)) |
|
223 | cs => raise THEORY ("thms_containing: undeclared consts " ^ commas_quote cs, [thy])) |
|
224 |> map (apsnd (Thm.transfer thy)); |
|
225 |
191 |
226 |
192 |
227 |
193 |
228 (** store theorems **) (*DESTRUCTIVE*) |
194 (** store theorems **) (*DESTRUCTIVE*) |
229 |
195 |
230 (* hiding -- affects current theory node only! *) |
196 (* hiding -- affects current theory node only! *) |
231 |
197 |
232 fun hide_thms b names thy = |
198 fun hide_thms b names thy = |
233 let |
199 let |
234 val r as ref {space, thms_tab, const_idx} = get_theorems thy; |
200 val r as ref {space, thms_tab, index} = get_theorems thy; |
235 val space' = NameSpace.hide b (space, names); |
201 val space' = NameSpace.hide b (space, names); |
236 in r := {space = space', thms_tab = thms_tab, const_idx = const_idx}; thy end; |
202 in r := {space = space', thms_tab = thms_tab, index = index}; thy end; |
237 |
203 |
238 |
204 |
239 (* naming *) |
205 (* naming *) |
240 |
206 |
241 fun gen_names j len name = |
207 fun gen_names j len name = |
265 let |
231 let |
266 val name = Sign.full_name sg bname; |
232 val name = Sign.full_name sg bname; |
267 val (thy', thms') = app_att (thy, pre_name name thms); |
233 val (thy', thms') = app_att (thy, pre_name name thms); |
268 val named_thms = post_name name thms'; |
234 val named_thms = post_name name thms'; |
269 |
235 |
270 val r as ref {space, thms_tab, const_idx} = get_theorems_sg sg; |
236 val r as ref {space, thms_tab, index} = get_theorems_sg sg; |
271 |
|
272 val overwrite = |
|
273 (case Symtab.lookup (thms_tab, name) of |
|
274 None => false |
|
275 | Some thms' => |
|
276 if Library.equal_lists Thm.eq_thm (thms', named_thms) then (warn_same name; false) |
|
277 else (warn_overwrite name; true)); |
|
278 |
|
279 val space' = NameSpace.extend (space, [name]); |
237 val space' = NameSpace.extend (space, [name]); |
280 val thms_tab' = Symtab.update ((name, named_thms), thms_tab); |
238 val thms_tab' = Symtab.update ((name, named_thms), thms_tab); |
281 val const_idx' = |
239 val index' = FactIndex.add (K false) (index, (name, named_thms)); |
282 if overwrite then make_const_idx thms_tab' |
240 in |
283 else foldl add_const_idx (const_idx, named_thms); |
241 (case Symtab.lookup (thms_tab, name) of |
284 in r := {space = space', thms_tab = thms_tab', const_idx = const_idx'}; |
242 None => () |
|
243 | Some thms' => |
|
244 if Library.equal_lists Thm.eq_thm (thms', named_thms) then warn_same name |
|
245 else warn_overwrite name); |
|
246 r := {space = space', thms_tab = thms_tab', index = index'}; |
285 (thy', named_thms) |
247 (thy', named_thms) |
286 end; |
248 end; |
287 |
249 |
288 |
250 |
289 (* add_thms(s) *) |
251 (* add_thms(s) *) |