391
|
1 |
(* Title: Pure/Thy/thy_read.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Sonia Mahjoub / Tobias Nipkow / L C Paulson
|
|
4 |
Copyright 1993 TU Muenchen
|
|
5 |
|
|
6 |
Reading and writing the theory definition files.
|
|
7 |
|
|
8 |
For theory XXX, the input file is called XXX.thy
|
|
9 |
the output file is called .XXX.thy.ML
|
|
10 |
and it then tries to read XXX.ML
|
|
11 |
*)
|
|
12 |
|
|
13 |
datatype thy_info = ThyInfo of {name: string, path: string,
|
|
14 |
children: string list,
|
|
15 |
thy_info: string option, ml_info: string option,
|
|
16 |
theory: Thm.theory option};
|
|
17 |
|
|
18 |
signature THY_READ =
|
|
19 |
sig
|
|
20 |
datatype basetype = Thy of string
|
|
21 |
| File of string
|
|
22 |
|
|
23 |
val loaded_thys : thy_info list ref
|
|
24 |
val loadpath : string list ref
|
|
25 |
val delete_tmpfiles: bool ref
|
|
26 |
|
|
27 |
val use_thy : string -> unit
|
|
28 |
val update : unit -> unit
|
|
29 |
val time_use_thy : string -> unit
|
|
30 |
val unlink_thy : string -> unit
|
|
31 |
val base_on : basetype list -> string -> Thm.theory
|
|
32 |
val store_theory : string -> Thm.theory -> unit
|
|
33 |
end;
|
|
34 |
|
|
35 |
|
|
36 |
functor ThyReadFun(structure ThyParse: THY_PARSE): THY_READ =
|
|
37 |
struct
|
|
38 |
|
|
39 |
datatype basetype = Thy of string
|
|
40 |
| File of string;
|
|
41 |
|
|
42 |
val loaded_thys = ref [ThyInfo {name = "Pure", path = "", children = [],
|
|
43 |
thy_info = Some "", ml_info = Some "",
|
|
44 |
theory = Some Thm.pure_thy}];
|
|
45 |
|
|
46 |
val loadpath = ref ["."]; (*default search path for theory files *)
|
|
47 |
|
|
48 |
val delete_tmpfiles = ref true; (*remove temporary files after use *)
|
|
49 |
|
|
50 |
(*Make name of the output ML file for a theory *)
|
|
51 |
fun out_name thy = "." ^ thy ^ ".thy.ML";
|
|
52 |
|
|
53 |
(*Read a file specified by thy_file containing theory thy *)
|
|
54 |
fun read_thy thy thy_file =
|
|
55 |
let
|
|
56 |
open ThyParse;
|
|
57 |
val instream = open_in thy_file;
|
|
58 |
val outstream = open_out (out_name thy);
|
|
59 |
in
|
|
60 |
output (outstream, parse_thy pure_syntax (input (instream, 999999)));
|
|
61 |
close_out outstream;
|
|
62 |
close_in instream
|
|
63 |
end;
|
|
64 |
|
|
65 |
fun file_exists file =
|
|
66 |
let val instream = open_in file in close_in instream; true end
|
|
67 |
handle Io _ => false;
|
|
68 |
|
|
69 |
(*Get thy_info for a loaded theory *)
|
|
70 |
fun get_thyinfo thy =
|
|
71 |
let fun do_search (t :: loaded : thy_info list) =
|
|
72 |
let val ThyInfo {name, ...} = t
|
|
73 |
in if name = thy then Some t else do_search loaded end
|
|
74 |
| do_search [] = None
|
|
75 |
in do_search (!loaded_thys) end;
|
|
76 |
|
|
77 |
(*Replace an item by the result of make_change *)
|
|
78 |
fun change_thyinfo make_change =
|
|
79 |
let fun search (t :: loaded) =
|
|
80 |
let val ThyInfo {name, path, children, thy_info, ml_info,
|
|
81 |
theory} = t
|
|
82 |
val (new_t, continue) = make_change name path children thy_info
|
|
83 |
ml_info theory
|
|
84 |
in if continue then
|
|
85 |
new_t :: (search loaded)
|
|
86 |
else
|
|
87 |
new_t :: loaded
|
|
88 |
end
|
|
89 |
| search [] = []
|
|
90 |
in loaded_thys := search (!loaded_thys) end;
|
|
91 |
|
|
92 |
(*Check if a theory was already loaded *)
|
|
93 |
fun already_loaded thy =
|
|
94 |
let val t = get_thyinfo thy
|
|
95 |
in if is_none t then false
|
|
96 |
else let val ThyInfo {thy_info, ml_info, ...} = the t
|
|
97 |
in if is_none thy_info orelse is_none ml_info then false
|
|
98 |
else true end
|
|
99 |
end;
|
|
100 |
|
|
101 |
(*Check if a theory file has changed since its last use.
|
|
102 |
Return a pair of boolean values for .thy and for .ML *)
|
|
103 |
fun thy_unchanged thy thy_file ml_file =
|
|
104 |
let val t = get_thyinfo thy
|
|
105 |
in if is_some t then
|
|
106 |
let val ThyInfo {thy_info, ml_info, ...} = the t
|
|
107 |
val tn = is_none thy_info;
|
|
108 |
val mn = is_none ml_info
|
|
109 |
in if not tn andalso not mn then
|
|
110 |
((file_info thy_file = the thy_info),
|
|
111 |
(file_info ml_file = the ml_info))
|
|
112 |
else if not tn andalso mn then (true, false)
|
|
113 |
else (false, false)
|
|
114 |
end
|
|
115 |
else (false, false)
|
|
116 |
end;
|
|
117 |
|
|
118 |
exception FILE_NOT_FOUND; (*raised by find_file *)
|
|
119 |
|
|
120 |
(*Find a file using a list of paths if no absolute or relative path is
|
|
121 |
specified.*)
|
|
122 |
fun find_file "" name =
|
|
123 |
let fun find_it (curr :: paths) =
|
|
124 |
if file_exists (tack_on curr name) then
|
|
125 |
tack_on curr name
|
|
126 |
else
|
|
127 |
find_it paths
|
|
128 |
| find_it [] = ""
|
|
129 |
in find_it (!loadpath) end
|
|
130 |
| find_file path name =
|
|
131 |
if file_exists (tack_on path name) then tack_on path name
|
|
132 |
else "";
|
|
133 |
|
|
134 |
(*Get absolute pathnames for a new or already loaded theory *)
|
|
135 |
fun get_filenames path name =
|
|
136 |
let fun make_absolute file =
|
|
137 |
if file = "" then "" else
|
|
138 |
if hd (explode file) = "/" then file else tack_on (pwd ()) file;
|
|
139 |
|
|
140 |
fun new_filename () =
|
|
141 |
let val found = find_file path (name ^ ".thy")
|
|
142 |
handle FILE_NOT_FOUND => "";
|
|
143 |
val thy_file = make_absolute found;
|
|
144 |
val (thy_path, _) = split_filename thy_file;
|
|
145 |
val found = find_file path (name ^ ".ML");
|
|
146 |
val ml_file = if thy_file = "" then make_absolute found
|
|
147 |
else if file_exists (tack_on thy_path (name ^ ".ML"))
|
|
148 |
then tack_on thy_path (name ^ ".ML")
|
|
149 |
else "";
|
|
150 |
val searched_dirs = if path = "" then (!loadpath) else [path]
|
|
151 |
in if thy_file = "" andalso ml_file = "" then
|
|
152 |
error ("Could not find file \"" ^ name ^ ".thy\" or \""
|
|
153 |
^ name ^ ".ML\" for theory \"" ^ name ^ "\"\n"
|
|
154 |
^ "in the following directories: \"" ^
|
|
155 |
(space_implode "\", \"" searched_dirs) ^ "\"")
|
|
156 |
else ();
|
|
157 |
(thy_file, ml_file)
|
|
158 |
end;
|
|
159 |
|
|
160 |
val thy = get_thyinfo name
|
|
161 |
in if is_some thy andalso path = "" then
|
|
162 |
let val ThyInfo {path = abs_path, ...} = the thy;
|
|
163 |
val (thy_file, ml_file) = if abs_path = "" then new_filename ()
|
|
164 |
else (find_file abs_path (name ^ ".thy"),
|
|
165 |
find_file abs_path (name ^ ".ML"))
|
|
166 |
in if thy_file = "" andalso ml_file = "" then
|
|
167 |
(writeln ("Warning: File \"" ^ (tack_on path name)
|
|
168 |
^ ".thy\"\ncontaining theory \"" ^ name
|
|
169 |
^ "\" no longer exists.");
|
|
170 |
new_filename ()
|
|
171 |
)
|
|
172 |
else (thy_file, ml_file)
|
|
173 |
end
|
|
174 |
else new_filename ()
|
|
175 |
end;
|
|
176 |
|
|
177 |
(*Remove theory from all child lists in loaded_thys *)
|
|
178 |
fun unlink_thy thy =
|
|
179 |
let fun remove name path children thy_info ml_info theory =
|
|
180 |
(ThyInfo {name = name, path = path, children = children \ thy,
|
|
181 |
thy_info = thy_info, ml_info = ml_info,
|
|
182 |
theory = theory}, true)
|
|
183 |
in change_thyinfo remove end;
|
|
184 |
|
|
185 |
(*Remove a theory from loaded_thys *)
|
|
186 |
fun remove_thy thy =
|
|
187 |
let fun remove (t :: ts) =
|
|
188 |
let val ThyInfo {name, ...} = t
|
|
189 |
in if name = thy then ts
|
|
190 |
else t :: (remove ts)
|
|
191 |
end
|
|
192 |
| remove [] = []
|
|
193 |
in loaded_thys := remove (!loaded_thys) end;
|
|
194 |
|
|
195 |
(*Change thy_info and ml_info for an existent item *)
|
|
196 |
fun set_info thy_new ml_new thy =
|
|
197 |
let fun change name path children thy_info ml_info theory =
|
|
198 |
if name = thy then
|
|
199 |
(ThyInfo {name = name, path = path, children = children,
|
|
200 |
thy_info = Some thy_new, ml_info = Some ml_new,
|
|
201 |
theory = theory}, false)
|
|
202 |
else
|
|
203 |
(ThyInfo {name = name, path = path, children = children,
|
|
204 |
thy_info = thy_info, ml_info = ml_info,
|
|
205 |
theory = theory}, true)
|
|
206 |
in change_thyinfo change end;
|
|
207 |
|
|
208 |
(*Mark theory as changed since last read if it has been completly read *)
|
|
209 |
fun mark_outdated thy =
|
|
210 |
if already_loaded thy then set_info "" "" thy
|
|
211 |
else ();
|
|
212 |
|
|
213 |
(*Read .thy and .ML files that haven't been read yet or have changed since
|
|
214 |
they were last read;
|
|
215 |
loaded_thys is a thy_info list ref containing all theories that have
|
|
216 |
completly been read by this and preceeding use_thy calls.
|
|
217 |
If a theory changed since its last use its children are marked as changed *)
|
|
218 |
fun use_thy name =
|
|
219 |
let val (path, thy_name) = split_filename name;
|
|
220 |
val (thy_file, ml_file) = get_filenames path thy_name;
|
|
221 |
val (abs_path, _) = if thy_file = "" then split_filename ml_file
|
|
222 |
else split_filename thy_file;
|
|
223 |
val (thy_uptodate, ml_uptodate) = thy_unchanged thy_name
|
|
224 |
thy_file ml_file;
|
|
225 |
|
|
226 |
(*Set absolute path for loaded theory *)
|
|
227 |
fun set_path () =
|
|
228 |
let fun change name path children thy_info ml_info theory =
|
|
229 |
if name = thy_name then
|
|
230 |
(ThyInfo {name = name, path = abs_path, children = children,
|
|
231 |
thy_info = thy_info, ml_info = ml_info,
|
|
232 |
theory = theory}, false)
|
|
233 |
else
|
|
234 |
(ThyInfo {name = name, path = path, children = children,
|
|
235 |
thy_info = thy_info, ml_info = ml_info,
|
|
236 |
theory = theory}, true)
|
|
237 |
in change_thyinfo change end;
|
|
238 |
|
|
239 |
(*Mark all direct descendants of a theory as changed *)
|
|
240 |
fun mark_children thy =
|
|
241 |
let val ThyInfo {children, ...} = the (get_thyinfo thy)
|
|
242 |
val loaded = filter already_loaded children
|
|
243 |
in if loaded <> [] then
|
|
244 |
(writeln ("The following children of theory " ^ (quote thy)
|
|
245 |
^ " are now out-of-date: "
|
|
246 |
^ (quote (space_implode "\",\"" loaded)));
|
|
247 |
seq mark_outdated loaded
|
|
248 |
)
|
|
249 |
else ()
|
|
250 |
end
|
|
251 |
|
|
252 |
in if thy_uptodate andalso ml_uptodate then ()
|
|
253 |
else
|
|
254 |
(
|
|
255 |
if thy_uptodate orelse thy_file = "" then ()
|
|
256 |
else (writeln ("Reading \"" ^ name ^ ".thy\"");
|
|
257 |
read_thy thy_name thy_file;
|
|
258 |
use (out_name thy_name)
|
|
259 |
);
|
|
260 |
|
|
261 |
if ml_file = "" then ()
|
|
262 |
else (writeln ("Reading \"" ^ name ^ ".ML\"");
|
|
263 |
use ml_file);
|
|
264 |
|
|
265 |
use_string ("store_theory " ^ quote thy_name ^ " "
|
|
266 |
^ thy_name ^ ".thy");
|
|
267 |
|
|
268 |
(*Now set the correct info*)
|
|
269 |
set_info (file_info thy_file) (file_info ml_file) thy_name;
|
|
270 |
set_path ();
|
|
271 |
|
|
272 |
(*Mark theories that have to be reloaded*)
|
|
273 |
mark_children thy_name;
|
|
274 |
|
|
275 |
(*Remove temporary files*)
|
|
276 |
if not (!delete_tmpfiles) orelse (thy_file = "") orelse thy_uptodate
|
|
277 |
then ()
|
|
278 |
else delete_file (out_name thy_name)
|
|
279 |
)
|
|
280 |
end;
|
|
281 |
|
|
282 |
fun time_use_thy tname = timeit(fn()=>
|
|
283 |
(writeln("\n**** Starting Theory " ^ tname ^ " ****");
|
|
284 |
use_thy tname;
|
|
285 |
writeln("\n**** Finished Theory " ^ tname ^ " ****"))
|
|
286 |
);
|
|
287 |
|
|
288 |
(*Load all thy or ML files that have been changed and also
|
|
289 |
all theories that depend on them *)
|
|
290 |
fun update () =
|
|
291 |
let (*List theories in the order they have to be loaded *)
|
|
292 |
fun load_order [] result = result
|
|
293 |
| load_order thys result =
|
|
294 |
let fun next_level (t :: ts) =
|
|
295 |
let val thy = get_thyinfo t
|
|
296 |
in if is_some thy then
|
|
297 |
let val ThyInfo {children, ...} = the thy
|
|
298 |
in children union (next_level ts)
|
|
299 |
end
|
|
300 |
else next_level ts
|
|
301 |
end
|
|
302 |
| next_level [] = [];
|
|
303 |
|
|
304 |
val children = next_level thys
|
|
305 |
in load_order children ((result \\ children) @ children) end;
|
|
306 |
|
|
307 |
fun reload_changed (t :: ts) =
|
|
308 |
let val thy = get_thyinfo t;
|
|
309 |
|
|
310 |
fun abspath () =
|
|
311 |
if is_some thy then
|
|
312 |
let val ThyInfo {path, ...} = the thy in path end
|
|
313 |
else "";
|
|
314 |
|
|
315 |
val (thy_file, ml_file) = get_filenames (abspath ()) t;
|
|
316 |
val (thy_uptodate, ml_uptodate) =
|
|
317 |
thy_unchanged t thy_file ml_file;
|
|
318 |
in if thy_uptodate andalso ml_uptodate then ()
|
|
319 |
else use_thy t;
|
|
320 |
reload_changed ts
|
|
321 |
end
|
|
322 |
| reload_changed [] = ();
|
|
323 |
|
|
324 |
(*Remove all theories that are no descendants of Pure.
|
|
325 |
If there are still children in the deleted theory's list
|
|
326 |
schedule them for reloading *)
|
|
327 |
fun collect_garbage not_garbage =
|
|
328 |
let fun collect (t :: ts) =
|
|
329 |
let val ThyInfo {name, children, ...} = t
|
|
330 |
in if name mem not_garbage then collect ts
|
|
331 |
else (writeln("Theory \"" ^ name
|
|
332 |
^ "\" is no longer linked with Pure - removing it.");
|
|
333 |
remove_thy name;
|
|
334 |
seq mark_outdated children
|
|
335 |
)
|
|
336 |
end
|
|
337 |
| collect [] = ()
|
|
338 |
|
|
339 |
in collect (!loaded_thys) end
|
|
340 |
|
|
341 |
in collect_garbage ("Pure" :: (load_order ["Pure"] []));
|
|
342 |
reload_changed (load_order ["Pure"] [])
|
|
343 |
end;
|
|
344 |
|
|
345 |
(*Merge theories to build a base for a new theory.
|
|
346 |
Base members are only loaded if they are missing. *)
|
|
347 |
fun base_on bases child =
|
|
348 |
let (*List all descendants of a theory list *)
|
|
349 |
fun list_descendants (t :: ts) =
|
|
350 |
let val tinfo = get_thyinfo t
|
|
351 |
in if is_some tinfo then
|
|
352 |
let val ThyInfo {children, ...} = the tinfo
|
|
353 |
in children union (list_descendants (ts union children))
|
|
354 |
end
|
|
355 |
else []
|
|
356 |
end
|
|
357 |
| list_descendants [] = [];
|
|
358 |
|
|
359 |
(*Show the cycle that would be created by add_child *)
|
|
360 |
fun show_cycle base =
|
|
361 |
let fun find_it result curr =
|
|
362 |
let val tinfo = get_thyinfo curr
|
|
363 |
in if base = curr then
|
|
364 |
error ("Cyclic dependency of theories: "
|
|
365 |
^ child ^ "->" ^ base ^ result)
|
|
366 |
else if is_some tinfo then
|
|
367 |
let val ThyInfo {children, ...} = the tinfo
|
|
368 |
in seq (find_it ("->" ^ curr ^ result)) children
|
|
369 |
end
|
|
370 |
else ()
|
|
371 |
end
|
|
372 |
in find_it "" child end;
|
|
373 |
|
|
374 |
(*Check if a cycle will be created by add_child *)
|
|
375 |
fun find_cycle base =
|
|
376 |
if base mem (list_descendants [child]) then show_cycle base
|
|
377 |
else ();
|
|
378 |
|
|
379 |
(*Add child to child list of base *)
|
|
380 |
fun add_child base =
|
|
381 |
let fun add (t :: loaded) =
|
|
382 |
let val ThyInfo {name, path, children,
|
|
383 |
thy_info, ml_info, theory} = t
|
|
384 |
in if name = base then
|
|
385 |
ThyInfo {name = name, path = path,
|
|
386 |
children = child ins children,
|
|
387 |
thy_info = thy_info, ml_info = ml_info,
|
|
388 |
theory = theory} :: loaded
|
|
389 |
else
|
|
390 |
t :: (add loaded)
|
|
391 |
end
|
|
392 |
| add [] =
|
|
393 |
[ThyInfo {name = base, path = "", children = [child],
|
|
394 |
thy_info = None, ml_info = None, theory = None}]
|
|
395 |
in loaded_thys := add (!loaded_thys) end;
|
|
396 |
|
|
397 |
(*Load a base theory if not already done
|
|
398 |
and no cycle would be created *)
|
|
399 |
fun load base =
|
|
400 |
let val thy_present = already_loaded base
|
|
401 |
(*test this before child is added *)
|
|
402 |
in
|
|
403 |
if child = base then
|
|
404 |
error ("Cyclic dependency of theories: " ^ child
|
|
405 |
^ "->" ^ child)
|
|
406 |
else
|
|
407 |
(find_cycle base;
|
|
408 |
add_child base;
|
|
409 |
if thy_present then ()
|
|
410 |
else (writeln ("Autoloading theory " ^ (quote base)
|
|
411 |
^ " (used by " ^ (quote child) ^ ")");
|
|
412 |
use_thy base)
|
|
413 |
)
|
|
414 |
end;
|
|
415 |
|
|
416 |
(*Load all needed files and make a list of all real theories *)
|
|
417 |
fun load_base (Thy b :: bs) =
|
|
418 |
(load b;
|
|
419 |
b :: (load_base bs))
|
|
420 |
| load_base (File b :: bs) =
|
|
421 |
(load b;
|
|
422 |
load_base bs) (*don't add it to merge_theories' parameter *)
|
|
423 |
| load_base [] = [];
|
|
424 |
|
|
425 |
(*Get theory object for a loaded theory *)
|
|
426 |
fun get_theory name =
|
|
427 |
let val ThyInfo {theory, ...} = the (get_thyinfo name)
|
|
428 |
in the theory end;
|
|
429 |
|
|
430 |
val mergelist = (unlink_thy child;
|
|
431 |
load_base bases);
|
|
432 |
val (t :: ts) = if mergelist = [] then ["Pure"] else mergelist
|
|
433 |
(*we have to return something *)
|
|
434 |
in writeln ("Loading theory " ^ (quote child));
|
|
435 |
foldl Thm.merge_theories (get_theory t, map get_theory ts) end;
|
|
436 |
|
|
437 |
(*Change theory object for an existent item of loaded_thys
|
|
438 |
or create a new item *)
|
|
439 |
fun store_theory thy_name thy =
|
|
440 |
let fun make_change (t :: loaded) =
|
|
441 |
let val ThyInfo {name, path, children, thy_info, ml_info, ...} = t
|
|
442 |
in if name = thy_name then
|
|
443 |
ThyInfo {name = name, path = path, children = children,
|
|
444 |
thy_info = thy_info, ml_info = ml_info,
|
|
445 |
theory = Some thy} :: loaded
|
|
446 |
else
|
|
447 |
t :: (make_change loaded)
|
|
448 |
end
|
|
449 |
| make_change [] =
|
|
450 |
[ThyInfo {name = thy_name, path = "", children = [],
|
|
451 |
thy_info = Some "", ml_info = Some "",
|
|
452 |
theory = Some thy}]
|
|
453 |
in loaded_thys := make_change (!loaded_thys) end;
|
|
454 |
|
|
455 |
end;
|
|
456 |
|