author | clasohm |
Tue, 06 Dec 1994 12:50:13 +0100 | |
changeset 758 | c2b210bda710 |
parent 746 | 6e815617d79f |
child 759 | e0b172d01c37 |
permissions | -rw-r--r-- |
391 | 1 |
(* Title: Pure/Thy/thy_read.ML |
2 |
ID: $Id$ |
|
559 | 3 |
Author: Carsten Clasohm and Markus Wenzel and Sonia Mahjoub and |
4 |
Tobias Nipkow and L C Paulson |
|
5 |
Copyright 1994 TU Muenchen |
|
391 | 6 |
|
559 | 7 |
(* FIXME !? *) |
391 | 8 |
Reading and writing the theory definition files. |
9 |
||
559 | 10 |
(* FIXME !? *) |
391 | 11 |
For theory XXX, the input file is called XXX.thy |
12 |
the output file is called .XXX.thy.ML |
|
13 |
and it then tries to read XXX.ML |
|
14 |
*) |
|
15 |
||
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
16 |
datatype thy_info = ThyInfo of {path: string, |
391 | 17 |
children: string list, |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
18 |
thy_time: string option, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
19 |
ml_time: string option, |
559 | 20 |
theory: theory option, |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
21 |
thms: thm Symtab.table}; |
391 | 22 |
|
412 | 23 |
signature READTHY = |
391 | 24 |
sig |
25 |
datatype basetype = Thy of string |
|
26 |
| File of string |
|
27 |
||
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
28 |
val loaded_thys : thy_info Symtab.table ref |
391 | 29 |
val loadpath : string list ref |
30 |
val delete_tmpfiles: bool ref |
|
31 |
||
32 |
val use_thy : string -> unit |
|
33 |
val update : unit -> unit |
|
34 |
val time_use_thy : string -> unit |
|
35 |
val unlink_thy : string -> unit |
|
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
559
diff
changeset
|
36 |
val mk_base : basetype list -> string -> bool -> theory |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
37 |
val store_theory : theory * string -> unit |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
38 |
|
559 | 39 |
val theory_of_sign: Sign.sg -> theory |
40 |
val theory_of_thm: thm -> theory |
|
41 |
val store_thm: string * thm -> thm |
|
758 | 42 |
val bind_thm: string * thm -> unit |
559 | 43 |
val qed: string -> unit |
758 | 44 |
val qed_thm: thm ref |
746 | 45 |
val qed_goal: string -> theory -> string -> (thm list -> tactic list) -> unit |
46 |
val qed_goalw: string -> theory->thm list->string->(thm list->tactic list) |
|
47 |
-> unit |
|
559 | 48 |
val get_thm: theory -> string -> thm |
49 |
val thms_of: theory -> (string * thm) list |
|
391 | 50 |
end; |
51 |
||
52 |
||
412 | 53 |
functor ReadthyFUN(structure ThySyn: THY_SYN): READTHY = |
391 | 54 |
struct |
55 |
||
56 |
datatype basetype = Thy of string |
|
57 |
| File of string; |
|
58 |
||
559 | 59 |
val loaded_thys = ref (Symtab.make [("Pure", ThyInfo {path = "", children = [], |
60 |
thy_time = Some "", ml_time = Some "", |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
61 |
theory = Some pure_thy, |
559 | 62 |
thms = Symtab.null})]); |
391 | 63 |
|
64 |
val loadpath = ref ["."]; (*default search path for theory files *) |
|
65 |
||
66 |
val delete_tmpfiles = ref true; (*remove temporary files after use *) |
|
67 |
||
68 |
(*Make name of the output ML file for a theory *) |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
69 |
fun out_name tname = "." ^ tname ^ ".thy.ML"; |
391 | 70 |
|
71 |
(*Read a file specified by thy_file containing theory thy *) |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
72 |
fun read_thy tname thy_file = |
559 | 73 |
let |
391 | 74 |
val instream = open_in thy_file; |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
75 |
val outstream = open_out (out_name tname); |
559 | 76 |
in |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
77 |
output (outstream, ThySyn.parse tname (input (instream, 999999))); |
391 | 78 |
close_out outstream; |
79 |
close_in instream |
|
80 |
end; |
|
81 |
||
82 |
fun file_exists file = |
|
83 |
let val instream = open_in file in close_in instream; true end |
|
84 |
handle Io _ => false; |
|
85 |
||
86 |
(*Get thy_info for a loaded theory *) |
|
559 | 87 |
fun get_thyinfo tname = Symtab.lookup (!loaded_thys, tname); |
391 | 88 |
|
89 |
(*Check if a theory was already loaded *) |
|
90 |
fun already_loaded thy = |
|
91 |
let val t = get_thyinfo thy |
|
92 |
in if is_none t then false |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
93 |
else let val ThyInfo {thy_time, ml_time, ...} = the t |
559 | 94 |
in if is_none thy_time orelse is_none ml_time then false |
95 |
else true |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
96 |
end |
391 | 97 |
end; |
98 |
||
99 |
(*Check if a theory file has changed since its last use. |
|
100 |
Return a pair of boolean values for .thy and for .ML *) |
|
559 | 101 |
fun thy_unchanged thy thy_file ml_file = |
391 | 102 |
let val t = get_thyinfo thy |
103 |
in if is_some t then |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
104 |
let val ThyInfo {thy_time, ml_time, ...} = the t |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
105 |
val tn = is_none thy_time; |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
106 |
val mn = is_none ml_time |
391 | 107 |
in if not tn andalso not mn then |
559 | 108 |
((file_info thy_file = the thy_time), |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
109 |
(file_info ml_file = the ml_time)) |
391 | 110 |
else if not tn andalso mn then (true, false) |
111 |
else (false, false) |
|
112 |
end |
|
113 |
else (false, false) |
|
114 |
end; |
|
115 |
||
116 |
exception FILE_NOT_FOUND; (*raised by find_file *) |
|
117 |
||
118 |
(*Find a file using a list of paths if no absolute or relative path is |
|
119 |
specified.*) |
|
120 |
fun find_file "" name = |
|
121 |
let fun find_it (curr :: paths) = |
|
122 |
if file_exists (tack_on curr name) then |
|
123 |
tack_on curr name |
|
559 | 124 |
else |
391 | 125 |
find_it paths |
126 |
| find_it [] = "" |
|
127 |
in find_it (!loadpath) end |
|
128 |
| find_file path name = |
|
129 |
if file_exists (tack_on path name) then tack_on path name |
|
130 |
else ""; |
|
131 |
||
132 |
(*Get absolute pathnames for a new or already loaded theory *) |
|
133 |
fun get_filenames path name = |
|
134 |
let fun make_absolute file = |
|
559 | 135 |
if file = "" then "" else |
391 | 136 |
if hd (explode file) = "/" then file else tack_on (pwd ()) file; |
137 |
||
138 |
fun new_filename () = |
|
139 |
let val found = find_file path (name ^ ".thy") |
|
140 |
handle FILE_NOT_FOUND => ""; |
|
141 |
val thy_file = make_absolute found; |
|
142 |
val (thy_path, _) = split_filename thy_file; |
|
143 |
val found = find_file path (name ^ ".ML"); |
|
144 |
val ml_file = if thy_file = "" then make_absolute found |
|
145 |
else if file_exists (tack_on thy_path (name ^ ".ML")) |
|
146 |
then tack_on thy_path (name ^ ".ML") |
|
147 |
else ""; |
|
148 |
val searched_dirs = if path = "" then (!loadpath) else [path] |
|
149 |
in if thy_file = "" andalso ml_file = "" then |
|
150 |
error ("Could not find file \"" ^ name ^ ".thy\" or \"" |
|
151 |
^ name ^ ".ML\" for theory \"" ^ name ^ "\"\n" |
|
152 |
^ "in the following directories: \"" ^ |
|
153 |
(space_implode "\", \"" searched_dirs) ^ "\"") |
|
154 |
else (); |
|
559 | 155 |
(thy_file, ml_file) |
391 | 156 |
end; |
157 |
||
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
158 |
val tinfo = get_thyinfo name; |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
159 |
in if is_some tinfo andalso path = "" then |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
160 |
let val ThyInfo {path = abs_path, ...} = the tinfo; |
391 | 161 |
val (thy_file, ml_file) = if abs_path = "" then new_filename () |
162 |
else (find_file abs_path (name ^ ".thy"), |
|
163 |
find_file abs_path (name ^ ".ML")) |
|
164 |
in if thy_file = "" andalso ml_file = "" then |
|
165 |
(writeln ("Warning: File \"" ^ (tack_on path name) |
|
166 |
^ ".thy\"\ncontaining theory \"" ^ name |
|
167 |
^ "\" no longer exists."); |
|
168 |
new_filename () |
|
169 |
) |
|
170 |
else (thy_file, ml_file) |
|
171 |
end |
|
172 |
else new_filename () |
|
173 |
end; |
|
174 |
||
175 |
(*Remove theory from all child lists in loaded_thys *) |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
176 |
fun unlink_thy tname = |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
177 |
let fun remove (ThyInfo {path, children, thy_time, ml_time, theory, thms}) = |
559 | 178 |
ThyInfo {path = path, children = children \ tname, |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
179 |
thy_time = thy_time, ml_time = ml_time, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
180 |
theory = theory, thms = thms} |
559 | 181 |
in loaded_thys := Symtab.map remove (!loaded_thys) end; |
391 | 182 |
|
183 |
(*Remove a theory from loaded_thys *) |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
184 |
fun remove_thy tname = |
559 | 185 |
loaded_thys := Symtab.make (filter_out (fn (id, _) => id = tname) |
186 |
(Symtab.dest (!loaded_thys))); |
|
391 | 187 |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
188 |
(*Change thy_time and ml_time for an existent item *) |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
189 |
fun set_info thy_time ml_time tname = |
559 | 190 |
let val ThyInfo {path, children, theory, thms, ...} = |
191 |
the (Symtab.lookup (!loaded_thys, tname)); |
|
192 |
in loaded_thys := Symtab.update ((tname, |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
193 |
ThyInfo {path = path, children = children, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
194 |
thy_time = Some thy_time, ml_time = Some ml_time, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
195 |
theory = theory, thms = thms}), !loaded_thys) |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
196 |
end; |
391 | 197 |
|
198 |
(*Mark theory as changed since last read if it has been completly read *) |
|
559 | 199 |
fun mark_outdated tname = |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
200 |
if already_loaded tname then set_info "" "" tname else (); |
391 | 201 |
|
559 | 202 |
(*Read .thy and .ML files that haven't been read yet or have changed since |
391 | 203 |
they were last read; |
559 | 204 |
loaded_thys is a thy_info list ref containing all theories that have |
391 | 205 |
completly been read by this and preceeding use_thy calls. |
206 |
If a theory changed since its last use its children are marked as changed *) |
|
207 |
fun use_thy name = |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
208 |
let val (path, tname) = split_filename name; |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
209 |
val (thy_file, ml_file) = get_filenames path tname; |
391 | 210 |
val (abs_path, _) = if thy_file = "" then split_filename ml_file |
211 |
else split_filename thy_file; |
|
559 | 212 |
val (thy_uptodate, ml_uptodate) = thy_unchanged tname |
391 | 213 |
thy_file ml_file; |
214 |
||
215 |
(*Set absolute path for loaded theory *) |
|
216 |
fun set_path () = |
|
559 | 217 |
let val ThyInfo {children, thy_time, ml_time, theory, thms, ...} = |
218 |
the (Symtab.lookup (!loaded_thys, tname)); |
|
219 |
in loaded_thys := Symtab.update ((tname, |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
220 |
ThyInfo {path = abs_path, children = children, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
221 |
thy_time = thy_time, ml_time = ml_time, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
222 |
theory = theory, thms = thms}), |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
223 |
!loaded_thys) |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
224 |
end; |
391 | 225 |
|
226 |
(*Mark all direct descendants of a theory as changed *) |
|
227 |
fun mark_children thy = |
|
228 |
let val ThyInfo {children, ...} = the (get_thyinfo thy) |
|
229 |
val loaded = filter already_loaded children |
|
230 |
in if loaded <> [] then |
|
231 |
(writeln ("The following children of theory " ^ (quote thy) |
|
232 |
^ " are now out-of-date: " |
|
233 |
^ (quote (space_implode "\",\"" loaded))); |
|
234 |
seq mark_outdated loaded |
|
235 |
) |
|
236 |
else () |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
237 |
end; |
391 | 238 |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
239 |
(*Remove all theorems associated with a theory*) |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
240 |
fun delete_thms () = |
559 | 241 |
let val tinfo = case Symtab.lookup (!loaded_thys, tname) of |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
242 |
Some (ThyInfo {path, children, thy_time, ml_time, theory, ...}) => |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
243 |
ThyInfo {path = path, children = children, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
244 |
thy_time = thy_time, ml_time = ml_time, |
559 | 245 |
theory = theory, thms = Symtab.null} |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
246 |
| None => ThyInfo {path = "", children = [], |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
247 |
thy_time = None, ml_time = None, |
559 | 248 |
theory = None, thms = Symtab.null}; |
249 |
in loaded_thys := Symtab.update ((tname, tinfo), !loaded_thys) end; |
|
391 | 250 |
in if thy_uptodate andalso ml_uptodate then () |
251 |
else |
|
252 |
( |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
253 |
delete_thms (); |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
254 |
|
391 | 255 |
if thy_uptodate orelse thy_file = "" then () |
256 |
else (writeln ("Reading \"" ^ name ^ ".thy\""); |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
257 |
read_thy tname thy_file; |
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
258 |
use (out_name tname); |
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
259 |
use_string |
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
260 |
["store_theory (" ^ tname ^ ".thy, " ^ quote tname ^ ");"] |
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
261 |
(*Store theory object in case it is needed for store_thm |
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
262 |
while reading the ML file*) |
391 | 263 |
); |
264 |
||
559 | 265 |
if ml_file = "" then () |
391 | 266 |
else (writeln ("Reading \"" ^ name ^ ".ML\""); |
267 |
use ml_file); |
|
268 |
||
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
269 |
use_string ["store_theory (" ^ tname ^ ".thy, " ^ quote tname ^ ");"]; |
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
270 |
(*Store theory again because it could have been redefined*) |
391 | 271 |
|
272 |
(*Now set the correct info*) |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
273 |
set_info (file_info thy_file) (file_info ml_file) tname; |
391 | 274 |
set_path (); |
275 |
||
276 |
(*Mark theories that have to be reloaded*) |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
277 |
mark_children tname; |
391 | 278 |
|
279 |
(*Remove temporary files*) |
|
559 | 280 |
if not (!delete_tmpfiles) orelse (thy_file = "") orelse thy_uptodate |
391 | 281 |
then () |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
282 |
else delete_file (out_name tname) |
391 | 283 |
) |
284 |
end; |
|
285 |
||
286 |
fun time_use_thy tname = timeit(fn()=> |
|
559 | 287 |
(writeln("\n**** Starting Theory " ^ tname ^ " ****"); |
391 | 288 |
use_thy tname; |
289 |
writeln("\n**** Finished Theory " ^ tname ^ " ****")) |
|
290 |
); |
|
291 |
||
292 |
(*Load all thy or ML files that have been changed and also |
|
293 |
all theories that depend on them *) |
|
294 |
fun update () = |
|
295 |
let (*List theories in the order they have to be loaded *) |
|
296 |
fun load_order [] result = result |
|
297 |
| load_order thys result = |
|
298 |
let fun next_level (t :: ts) = |
|
299 |
let val thy = get_thyinfo t |
|
300 |
in if is_some thy then |
|
301 |
let val ThyInfo {children, ...} = the thy |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
302 |
in children union (next_level ts) end |
391 | 303 |
else next_level ts |
304 |
end |
|
305 |
| next_level [] = []; |
|
559 | 306 |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
307 |
val children = next_level thys; |
391 | 308 |
in load_order children ((result \\ children) @ children) end; |
309 |
||
310 |
fun reload_changed (t :: ts) = |
|
311 |
let val thy = get_thyinfo t; |
|
312 |
||
313 |
fun abspath () = |
|
314 |
if is_some thy then |
|
315 |
let val ThyInfo {path, ...} = the thy in path end |
|
316 |
else ""; |
|
317 |
||
318 |
val (thy_file, ml_file) = get_filenames (abspath ()) t; |
|
319 |
val (thy_uptodate, ml_uptodate) = |
|
320 |
thy_unchanged t thy_file ml_file; |
|
321 |
in if thy_uptodate andalso ml_uptodate then () |
|
322 |
else use_thy t; |
|
323 |
reload_changed ts |
|
324 |
end |
|
325 |
| reload_changed [] = (); |
|
326 |
||
327 |
(*Remove all theories that are no descendants of Pure. |
|
328 |
If there are still children in the deleted theory's list |
|
329 |
schedule them for reloading *) |
|
330 |
fun collect_garbage not_garbage = |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
331 |
let fun collect ((tname, ThyInfo {children, ...}) :: ts) = |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
332 |
if tname mem not_garbage then collect ts |
559 | 333 |
else (writeln ("Theory \"" ^ tname |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
334 |
^ "\" is no longer linked with Pure - removing it."); |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
335 |
remove_thy tname; |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
336 |
seq mark_outdated children) |
391 | 337 |
| collect [] = () |
338 |
||
559 | 339 |
in collect (Symtab.dest (!loaded_thys)) end; |
391 | 340 |
in collect_garbage ("Pure" :: (load_order ["Pure"] [])); |
341 |
reload_changed (load_order ["Pure"] []) |
|
342 |
end; |
|
343 |
||
344 |
(*Merge theories to build a base for a new theory. |
|
345 |
Base members are only loaded if they are missing. *) |
|
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
559
diff
changeset
|
346 |
fun mk_base bases child mk_draft = |
391 | 347 |
let (*List all descendants of a theory list *) |
348 |
fun list_descendants (t :: ts) = |
|
349 |
let val tinfo = get_thyinfo t |
|
350 |
in if is_some tinfo then |
|
351 |
let val ThyInfo {children, ...} = the tinfo |
|
352 |
in children union (list_descendants (ts union children)) |
|
353 |
end |
|
354 |
else [] |
|
355 |
end |
|
356 |
| list_descendants [] = []; |
|
357 |
||
358 |
(*Show the cycle that would be created by add_child *) |
|
359 |
fun show_cycle base = |
|
360 |
let fun find_it result curr = |
|
361 |
let val tinfo = get_thyinfo curr |
|
559 | 362 |
in if base = curr then |
391 | 363 |
error ("Cyclic dependency of theories: " |
364 |
^ child ^ "->" ^ base ^ result) |
|
365 |
else if is_some tinfo then |
|
366 |
let val ThyInfo {children, ...} = the tinfo |
|
367 |
in seq (find_it ("->" ^ curr ^ result)) children |
|
368 |
end |
|
369 |
else () |
|
370 |
end |
|
371 |
in find_it "" child end; |
|
559 | 372 |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
373 |
(*Check if a cycle would be created by add_child *) |
391 | 374 |
fun find_cycle base = |
375 |
if base mem (list_descendants [child]) then show_cycle base |
|
376 |
else (); |
|
559 | 377 |
|
391 | 378 |
(*Add child to child list of base *) |
379 |
fun add_child base = |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
380 |
let val tinfo = |
559 | 381 |
case Symtab.lookup (!loaded_thys, base) of |
382 |
Some (ThyInfo {path, children, thy_time, ml_time, |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
383 |
theory, thms}) => |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
384 |
ThyInfo {path = path, children = child ins children, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
385 |
thy_time = thy_time, ml_time = ml_time, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
386 |
theory = theory, thms = thms} |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
387 |
| None => ThyInfo {path = "", children = [child], |
559 | 388 |
thy_time = None, ml_time = None, |
389 |
theory = None, thms = Symtab.null}; |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
390 |
in loaded_thys := Symtab.update ((base, tinfo), !loaded_thys) end; |
391 | 391 |
|
392 |
(*Load a base theory if not already done |
|
393 |
and no cycle would be created *) |
|
394 |
fun load base = |
|
395 |
let val thy_present = already_loaded base |
|
426
767367131b47
replaced "foldl merge_theories" by "merge_thy_list" in base_on
clasohm
parents:
424
diff
changeset
|
396 |
(*test this before child is added *) |
391 | 397 |
in |
398 |
if child = base then |
|
399 |
error ("Cyclic dependency of theories: " ^ child |
|
400 |
^ "->" ^ child) |
|
559 | 401 |
else |
391 | 402 |
(find_cycle base; |
403 |
add_child base; |
|
404 |
if thy_present then () |
|
405 |
else (writeln ("Autoloading theory " ^ (quote base) |
|
406 |
^ " (used by " ^ (quote child) ^ ")"); |
|
407 |
use_thy base) |
|
408 |
) |
|
559 | 409 |
end; |
391 | 410 |
|
411 |
(*Load all needed files and make a list of all real theories *) |
|
412 |
fun load_base (Thy b :: bs) = |
|
413 |
(load b; |
|
414 |
b :: (load_base bs)) |
|
415 |
| load_base (File b :: bs) = |
|
416 |
(load b; |
|
417 |
load_base bs) (*don't add it to merge_theories' parameter *) |
|
418 |
| load_base [] = []; |
|
419 |
||
420 |
(*Get theory object for a loaded theory *) |
|
421 |
fun get_theory name = |
|
422 |
let val ThyInfo {theory, ...} = the (get_thyinfo name) |
|
423 |
in the theory end; |
|
424 |
||
425 |
val mergelist = (unlink_thy child; |
|
426 |
load_base bases); |
|
427 |
in writeln ("Loading theory " ^ (quote child)); |
|
426
767367131b47
replaced "foldl merge_theories" by "merge_thy_list" in base_on
clasohm
parents:
424
diff
changeset
|
428 |
merge_thy_list mk_draft (map get_theory mergelist) end; |
391 | 429 |
|
559 | 430 |
(*Change theory object for an existent item of loaded_thys |
391 | 431 |
or create a new item *) |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
432 |
fun store_theory (thy, tname) = |
559 | 433 |
let val tinfo = case Symtab.lookup (!loaded_thys, tname) of |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
434 |
Some (ThyInfo {path, children, thy_time, ml_time, thms, ...}) => |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
435 |
ThyInfo {path = path, children = children, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
436 |
thy_time = thy_time, ml_time = ml_time, |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
437 |
theory = Some thy, thms = thms} |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
438 |
| None => ThyInfo {path = "", children = [], |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
439 |
thy_time = Some "", ml_time = Some "", |
559 | 440 |
theory = Some thy, thms = Symtab.null}; |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
441 |
in loaded_thys := Symtab.update ((tname, tinfo), !loaded_thys) end; |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
442 |
|
559 | 443 |
|
444 |
||
445 |
(** store and retrieve theorems **) |
|
446 |
||
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
447 |
(*Guess to which theory a signature belongs and return it's thy_info*) |
559 | 448 |
fun thyinfo_of_sign sg = |
449 |
let |
|
450 |
val ref xname = hd (#stamps (Sign.rep_sg sg)); |
|
451 |
val opt_info = get_thyinfo xname; |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
452 |
|
559 | 453 |
fun eq_sg (ThyInfo {theory = None, ...}) = false |
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
454 |
| eq_sg (ThyInfo {theory = Some thy, ...}) = Sign.eq_sg (sg,sign_of thy); |
559 | 455 |
|
456 |
val show_sg = Pretty.str_of o Sign.pretty_sg; |
|
457 |
in |
|
458 |
if is_some opt_info andalso eq_sg (the opt_info) then |
|
459 |
(xname, the opt_info) |
|
460 |
else |
|
461 |
(case Symtab.find_first (eq_sg o snd) (! loaded_thys) of |
|
462 |
Some name_info => name_info |
|
463 |
| None => error ("Theory " ^ show_sg sg ^ " not stored by loader")) |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
464 |
end; |
391 | 465 |
|
559 | 466 |
|
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
467 |
(*Try to get the theory object corresponding to a given signature*) |
559 | 468 |
fun theory_of_sign sg = |
469 |
(case thyinfo_of_sign sg of |
|
470 |
(_, ThyInfo {theory = Some thy, ...}) => thy |
|
471 |
| _ => sys_error "theory_of_sign"); |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
472 |
|
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
473 |
(*Try to get the theory object corresponding to a given theorem*) |
559 | 474 |
val theory_of_thm = theory_of_sign o #sign o rep_thm; |
475 |
||
476 |
||
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
477 |
(* Store theorems *) |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
478 |
|
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
479 |
(*Store a theorem in the thy_info of its theory*) |
559 | 480 |
fun store_thm (name, thm) = |
481 |
let |
|
482 |
val (thy_name, ThyInfo {path, children, thy_time, ml_time, theory, thms}) = |
|
483 |
thyinfo_of_sign (#sign (rep_thm thm)); |
|
484 |
val thms' = Symtab.update_new ((name, thm), thms) |
|
485 |
handle Symtab.DUPLICATE s => error ("Duplicate theorem name " ^ quote s); |
|
486 |
in |
|
487 |
loaded_thys := Symtab.update |
|
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
488 |
((thy_name, ThyInfo {path = path, children = children, |
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
489 |
thy_time = thy_time, ml_time = ml_time, theory = theory, thms = thms'}), |
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
490 |
! loaded_thys); |
559 | 491 |
thm |
492 |
end; |
|
493 |
||
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
494 |
(*Store result of proof in loaded_thys and as ML value*) |
758 | 495 |
|
496 |
val qed_thm = ref flexpair_def(*dummy*); |
|
497 |
||
498 |
fun bind_thm (name, thm) = |
|
499 |
(qed_thm := thm; |
|
500 |
use_string ["val " ^ name ^ " = standard (store_thm (" ^ quote name ^ |
|
501 |
", !qed_thm));"]); |
|
502 |
||
559 | 503 |
fun qed name = |
504 |
use_string ["val " ^ name ^ " = store_thm (" ^ quote name ^ ", result ());"]; |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
505 |
|
746 | 506 |
fun qed_goal name thy agoal tacsf = |
758 | 507 |
(qed_thm := prove_goal thy agoal tacsf; |
508 |
use_string ["val " ^ name ^ " = store_thm (" ^ quote name ^", !qed_thm);"]); |
|
746 | 509 |
|
510 |
fun qed_goalw name thy rths agoal tacsf = |
|
758 | 511 |
(qed_thm := prove_goalw thy rths agoal tacsf; |
512 |
use_string ["val " ^ name ^ " = store_thm (" ^ quote name ^", !qed_thm);"]); |
|
559 | 513 |
|
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
514 |
(* Retrieve theorems *) |
559 | 515 |
|
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
516 |
(*Get all theorems belonging to a given theory object*) |
559 | 517 |
fun thmtab thy = |
518 |
let val (_, ThyInfo {thms, ...}) = thyinfo_of_sign (sign_of thy) |
|
519 |
in thms end; |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
426
diff
changeset
|
520 |
|
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
521 |
(*Get a stored theorem specified by theory and name*) |
559 | 522 |
fun get_thm thy name = |
523 |
(case Symtab.lookup (thmtab thy, name) of |
|
524 |
Some thm => thm |
|
525 |
| None => raise THEORY ("get_thm: no theorem " ^ quote name, [thy])); |
|
526 |
||
715
f76ad10f5802
added call of store_theory after thy file has been read
clasohm
parents:
586
diff
changeset
|
527 |
(*Get stored theorems of a theory*) |
559 | 528 |
val thms_of = Symtab.dest o thmtab; |
529 |
||
530 |
||
391 | 531 |
end; |