berghofe@3604
|
1 |
(* Title: Pure/Thy/thy_info.ML
|
berghofe@3604
|
2 |
ID: $Id$
|
wenzelm@6211
|
3 |
Author: Markus Wenzel, TU Muenchen
|
berghofe@3604
|
4 |
|
wenzelm@15801
|
5 |
Main part of theory loader database, including handling of theory and
|
wenzelm@15801
|
6 |
file dependencies.
|
wenzelm@3976
|
7 |
*)
|
berghofe@3604
|
8 |
|
wenzelm@5209
|
9 |
signature BASIC_THY_INFO =
|
wenzelm@5209
|
10 |
sig
|
wenzelm@5209
|
11 |
val theory: string -> theory
|
wenzelm@6211
|
12 |
val touch_thy: string -> unit
|
wenzelm@6666
|
13 |
val remove_thy: string -> unit
|
wenzelm@6211
|
14 |
end;
|
wenzelm@5209
|
15 |
|
berghofe@3604
|
16 |
signature THY_INFO =
|
berghofe@3604
|
17 |
sig
|
wenzelm@5209
|
18 |
include BASIC_THY_INFO
|
wenzelm@7099
|
19 |
datatype action = Update | Outdate | Remove
|
wenzelm@7099
|
20 |
val str_of_action: action -> string
|
wenzelm@7099
|
21 |
val add_hook: (action -> string -> unit) -> unit
|
wenzelm@6211
|
22 |
val names: unit -> string list
|
wenzelm@7910
|
23 |
val known_thy: string -> bool
|
wenzelm@7935
|
24 |
val check_known_thy: string -> bool
|
wenzelm@7935
|
25 |
val if_known_thy: (string -> unit) -> string -> unit
|
wenzelm@7288
|
26 |
val lookup_theory: string -> theory option
|
wenzelm@6211
|
27 |
val get_theory: string -> theory
|
wenzelm@19547
|
28 |
val the_theory: string -> theory -> theory
|
wenzelm@24080
|
29 |
val loaded_files: string -> Path.T list
|
wenzelm@23871
|
30 |
val get_parents: string -> string list
|
wenzelm@24080
|
31 |
val pretty_theory: theory -> Pretty.T
|
wenzelm@7910
|
32 |
val touch_child_thys: string -> unit
|
wenzelm@7099
|
33 |
val touch_all_thys: unit -> unit
|
wenzelm@7941
|
34 |
val load_file: bool -> Path.T -> unit
|
wenzelm@6241
|
35 |
val use: string -> unit
|
wenzelm@24057
|
36 |
val time_use: string -> unit
|
wenzelm@24057
|
37 |
val use_thys: string list -> unit
|
wenzelm@24057
|
38 |
val use_thy: string -> unit
|
wenzelm@24057
|
39 |
val time_use_thy: string -> unit
|
wenzelm@24057
|
40 |
val update_thy: string -> unit
|
wenzelm@23900
|
41 |
val begin_theory: string -> string list -> (Path.T * bool) list -> bool -> theory
|
wenzelm@6211
|
42 |
val end_theory: theory -> theory
|
wenzelm@24080
|
43 |
val register_thy: string -> unit
|
wenzelm@24080
|
44 |
val register_theory: theory -> unit
|
wenzelm@6666
|
45 |
val finish: unit -> unit
|
berghofe@3604
|
46 |
end;
|
berghofe@3604
|
47 |
|
wenzelm@6362
|
48 |
structure ThyInfo: THY_INFO =
|
berghofe@3604
|
49 |
struct
|
berghofe@3604
|
50 |
|
wenzelm@7099
|
51 |
(** theory loader actions and hooks **)
|
wenzelm@7099
|
52 |
|
wenzelm@7099
|
53 |
datatype action = Update | Outdate | Remove;
|
wenzelm@7099
|
54 |
val str_of_action = fn Update => "Update" | Outdate => "Outdate" | Remove => "Remove";
|
wenzelm@7099
|
55 |
|
wenzelm@7099
|
56 |
local
|
wenzelm@7099
|
57 |
val hooks = ref ([]: (action -> string -> unit) list);
|
wenzelm@7099
|
58 |
in
|
wenzelm@23939
|
59 |
fun add_hook f = CRITICAL (fn () => change hooks (cons f));
|
skalberg@15570
|
60 |
fun perform action name = List.app (fn f => (try (fn () => f action name) (); ())) (! hooks);
|
wenzelm@7099
|
61 |
end;
|
wenzelm@7099
|
62 |
|
wenzelm@7099
|
63 |
|
wenzelm@7099
|
64 |
|
wenzelm@6211
|
65 |
(** thy database **)
|
wenzelm@3976
|
66 |
|
wenzelm@6211
|
67 |
(* messages *)
|
berghofe@3604
|
68 |
|
wenzelm@23871
|
69 |
fun loader_msg txt [] = "Theory loader: " ^ txt
|
wenzelm@23871
|
70 |
| loader_msg txt names = "Theory loader: " ^ txt ^ " " ^ commas_quote names;
|
berghofe@3604
|
71 |
|
wenzelm@6211
|
72 |
val show_path = space_implode " via " o map quote;
|
wenzelm@9332
|
73 |
fun cycle_msg names = loader_msg ("cyclic dependency of " ^ show_path names) [];
|
berghofe@3604
|
74 |
|
berghofe@3604
|
75 |
|
wenzelm@6666
|
76 |
(* derived graph operations *)
|
berghofe@3604
|
77 |
|
wenzelm@9327
|
78 |
fun add_deps name parents G = Graph.add_deps_acyclic (name, parents) G
|
wenzelm@9332
|
79 |
handle Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
|
berghofe@3604
|
80 |
|
wenzelm@7952
|
81 |
fun upd_deps name entry G =
|
wenzelm@19473
|
82 |
fold (fn parent => Graph.del_edge (parent, name)) (Graph.imm_preds G name) G
|
wenzelm@7952
|
83 |
|> Graph.map_node name (K entry);
|
berghofe@3604
|
84 |
|
wenzelm@23967
|
85 |
fun new_deps name parents entry G =
|
wenzelm@7952
|
86 |
(if can (Graph.get_node G) name then upd_deps name entry G else Graph.new_node (name, entry) G)
|
wenzelm@7952
|
87 |
|> add_deps name parents;
|
berghofe@3604
|
88 |
|
berghofe@3604
|
89 |
|
wenzelm@6211
|
90 |
(* thy database *)
|
berghofe@3604
|
91 |
|
wenzelm@23871
|
92 |
type master = (Path.T * File.ident) * (Path.T * File.ident) option;
|
wenzelm@23871
|
93 |
|
wenzelm@6211
|
94 |
type deps =
|
wenzelm@24151
|
95 |
{update_time: int, (*symbolic time of last update; < 0 means outdated*)
|
wenzelm@23886
|
96 |
master: master option, (*master dependencies for thy + attached ML file*)
|
wenzelm@24068
|
97 |
text: string list, (*source text for thy*)
|
wenzelm@23893
|
98 |
parents: string list, (*source specification of parents (partially qualified)*)
|
wenzelm@23886
|
99 |
files: (*auxiliary files: source path, physical path + identifier*)
|
wenzelm@23886
|
100 |
(Path.T * (Path.T * File.ident) option) list};
|
wenzelm@23886
|
101 |
|
wenzelm@24151
|
102 |
fun make_deps update_time master text parents files : deps =
|
wenzelm@24151
|
103 |
{update_time = update_time, master = master, text = text, parents = parents, files = files};
|
wenzelm@23886
|
104 |
|
wenzelm@24068
|
105 |
fun init_deps master text parents files =
|
wenzelm@24151
|
106 |
SOME (make_deps ~1 master text parents (map (rpair NONE) files));
|
wenzelm@23871
|
107 |
|
wenzelm@23871
|
108 |
fun master_idents (NONE: master option) = []
|
wenzelm@23871
|
109 |
| master_idents (SOME ((_, thy_id), NONE)) = [thy_id]
|
wenzelm@23871
|
110 |
| master_idents (SOME ((_, thy_id), SOME (_, ml_id))) = [thy_id, ml_id];
|
wenzelm@23871
|
111 |
|
wenzelm@23886
|
112 |
fun master_dir (NONE: master option) = Path.current
|
wenzelm@23886
|
113 |
| master_dir (SOME ((path, _), _)) = Path.dir path;
|
berghofe@3604
|
114 |
|
wenzelm@23886
|
115 |
fun master_dir' (d: deps option) = the_default Path.current (Option.map (master_dir o #master) d);
|
wenzelm@23886
|
116 |
fun master_dir'' d = the_default Path.current (Option.map master_dir' d);
|
berghofe@3604
|
117 |
|
wenzelm@23967
|
118 |
fun base_name s = Path.implode (Path.base (Path.explode s));
|
wenzelm@23967
|
119 |
|
wenzelm@7211
|
120 |
|
wenzelm@6362
|
121 |
type thy = deps option * theory option;
|
berghofe@3604
|
122 |
|
wenzelm@6211
|
123 |
local
|
wenzelm@6362
|
124 |
val database = ref (Graph.empty: thy Graph.T);
|
wenzelm@6211
|
125 |
in
|
wenzelm@6362
|
126 |
fun get_thys () = ! database;
|
wenzelm@23939
|
127 |
fun change_thys f = CRITICAL (fn () => Library.change database f);
|
berghofe@3604
|
128 |
end;
|
wenzelm@5209
|
129 |
|
wenzelm@5209
|
130 |
|
wenzelm@6211
|
131 |
(* access thy graph *)
|
wenzelm@6211
|
132 |
|
wenzelm@6211
|
133 |
fun thy_graph f x = f (get_thys ()) x;
|
wenzelm@9417
|
134 |
|
wenzelm@23967
|
135 |
fun get_names () = Graph.topological_order (get_thys ());
|
wenzelm@6211
|
136 |
|
wenzelm@6211
|
137 |
|
wenzelm@6211
|
138 |
(* access thy *)
|
wenzelm@6211
|
139 |
|
wenzelm@7935
|
140 |
fun lookup_thy name =
|
skalberg@15531
|
141 |
SOME (thy_graph Graph.get_node name) handle Graph.UNDEF _ => NONE;
|
wenzelm@7935
|
142 |
|
wenzelm@16047
|
143 |
val known_thy = is_some o lookup_thy;
|
wenzelm@7935
|
144 |
fun check_known_thy name = known_thy name orelse (warning ("Unknown theory " ^ quote name); false);
|
wenzelm@7935
|
145 |
fun if_known_thy f name = if check_known_thy name then f name else ();
|
wenzelm@6211
|
146 |
|
wenzelm@6211
|
147 |
fun get_thy name =
|
wenzelm@6211
|
148 |
(case lookup_thy name of
|
skalberg@15531
|
149 |
SOME thy => thy
|
skalberg@15531
|
150 |
| NONE => error (loader_msg "nothing known about theory" [name]));
|
wenzelm@6211
|
151 |
|
wenzelm@23939
|
152 |
fun change_thy name f = CRITICAL (fn () =>
|
wenzelm@23939
|
153 |
(get_thy name; change_thys (Graph.map_node name f)));
|
wenzelm@6211
|
154 |
|
wenzelm@6211
|
155 |
|
wenzelm@6211
|
156 |
(* access deps *)
|
wenzelm@6211
|
157 |
|
skalberg@15570
|
158 |
val lookup_deps = Option.map #1 o lookup_thy;
|
wenzelm@6211
|
159 |
val get_deps = #1 o get_thy;
|
wenzelm@6211
|
160 |
fun change_deps name f = change_thy name (fn (deps, x) => (f deps, x));
|
wenzelm@6211
|
161 |
|
wenzelm@6666
|
162 |
fun is_finished name = is_none (get_deps name);
|
wenzelm@7191
|
163 |
|
wenzelm@7191
|
164 |
fun loaded_files name =
|
wenzelm@7191
|
165 |
(case get_deps name of
|
skalberg@15531
|
166 |
NONE => []
|
skalberg@15531
|
167 |
| SOME {master, files, ...} =>
|
wenzelm@23871
|
168 |
(case master of SOME ((thy_path, _), _) => [thy_path] | NONE => []) @
|
wenzelm@19482
|
169 |
(map_filter (Option.map #1 o #2) files));
|
wenzelm@6211
|
170 |
|
wenzelm@23871
|
171 |
fun get_parents name =
|
wenzelm@23967
|
172 |
thy_graph Graph.imm_preds name handle Graph.UNDEF _ =>
|
berghofe@6654
|
173 |
error (loader_msg "nothing known about theory" [name]);
|
berghofe@6654
|
174 |
|
wenzelm@6211
|
175 |
|
wenzelm@16047
|
176 |
(* pretty printing a theory *)
|
paulson@15633
|
177 |
|
wenzelm@16047
|
178 |
local
|
paulson@15633
|
179 |
|
wenzelm@16047
|
180 |
fun relevant_names names =
|
wenzelm@16047
|
181 |
let
|
wenzelm@16047
|
182 |
val (finished, unfinished) =
|
wenzelm@19305
|
183 |
List.filter (fn name => name = Context.draftN orelse known_thy name) names
|
wenzelm@19305
|
184 |
|> List.partition (fn name => name <> Context.draftN andalso is_finished name);
|
wenzelm@16047
|
185 |
in (if not (null finished) then [List.last finished] else []) @ unfinished end;
|
wenzelm@16047
|
186 |
|
wenzelm@16047
|
187 |
in
|
paulson@15633
|
188 |
|
wenzelm@16454
|
189 |
fun pretty_theory thy =
|
wenzelm@16454
|
190 |
Pretty.str_list "{" "}" (relevant_names (Context.names_of thy));
|
paulson@15633
|
191 |
|
wenzelm@16047
|
192 |
end;
|
wenzelm@16047
|
193 |
|
paulson@15633
|
194 |
|
wenzelm@6211
|
195 |
(* access theory *)
|
wenzelm@6211
|
196 |
|
wenzelm@7687
|
197 |
fun lookup_theory name =
|
wenzelm@7687
|
198 |
(case lookup_thy name of
|
skalberg@15531
|
199 |
SOME (_, SOME thy) => SOME thy
|
skalberg@15531
|
200 |
| _ => NONE);
|
wenzelm@7288
|
201 |
|
wenzelm@6211
|
202 |
fun get_theory name =
|
wenzelm@7288
|
203 |
(case lookup_theory name of
|
wenzelm@23871
|
204 |
SOME theory => theory
|
wenzelm@6211
|
205 |
| _ => error (loader_msg "undefined theory entry for" [name]));
|
wenzelm@6211
|
206 |
|
wenzelm@19547
|
207 |
fun the_theory name thy =
|
wenzelm@19547
|
208 |
if Context.theory_name thy = name then thy
|
wenzelm@19547
|
209 |
else get_theory name;
|
wenzelm@19547
|
210 |
|
wenzelm@22137
|
211 |
val _ = ML_Context.value_antiq "theory"
|
wenzelm@22137
|
212 |
(Scan.lift Args.name
|
wenzelm@22137
|
213 |
>> (fn name => (name ^ "_thy", "ThyInfo.theory " ^ ML_Syntax.print_string name))
|
wenzelm@22148
|
214 |
|| Scan.succeed ("thy", "ML_Context.the_context ()"));
|
wenzelm@22137
|
215 |
|
wenzelm@6211
|
216 |
|
wenzelm@6211
|
217 |
|
wenzelm@6211
|
218 |
(** thy operations **)
|
wenzelm@6211
|
219 |
|
wenzelm@24080
|
220 |
(* check state *)
|
wenzelm@24080
|
221 |
|
wenzelm@24080
|
222 |
fun check_unfinished fail name =
|
wenzelm@24080
|
223 |
if known_thy name andalso is_finished name then
|
wenzelm@24080
|
224 |
fail (loader_msg "cannot update finished theory" [name])
|
wenzelm@24080
|
225 |
else ();
|
wenzelm@24080
|
226 |
|
wenzelm@24080
|
227 |
fun check_files name =
|
wenzelm@24080
|
228 |
let
|
wenzelm@24080
|
229 |
val files = (case get_deps name of SOME {files, ...} => files | NONE => []);
|
wenzelm@24080
|
230 |
val missing_files = map_filter (fn (path, NONE) => SOME (Path.implode path) | _ => NONE) files;
|
wenzelm@24080
|
231 |
val _ =
|
wenzelm@24080
|
232 |
if null missing_files then ()
|
wenzelm@24080
|
233 |
else warning (loader_msg "unresolved dependencies of theory" [name] ^
|
wenzelm@24080
|
234 |
" on file(s): " ^ commas_quote missing_files);
|
wenzelm@24080
|
235 |
in files end;
|
wenzelm@24080
|
236 |
|
wenzelm@24080
|
237 |
|
wenzelm@24151
|
238 |
(* maintain update_time *)
|
wenzelm@6211
|
239 |
|
wenzelm@7099
|
240 |
local
|
wenzelm@7099
|
241 |
|
wenzelm@6241
|
242 |
fun is_outdated name =
|
wenzelm@6241
|
243 |
(case lookup_deps name of
|
wenzelm@24151
|
244 |
SOME (SOME {update_time, ...}) => update_time < 0
|
wenzelm@6241
|
245 |
| _ => false);
|
wenzelm@6211
|
246 |
|
wenzelm@6241
|
247 |
fun outdate_thy name =
|
wenzelm@7099
|
248 |
if is_finished name orelse is_outdated name then ()
|
wenzelm@23939
|
249 |
else CRITICAL (fn () =>
|
wenzelm@24151
|
250 |
(change_deps name (Option.map (fn {master, text, parents, files, ...} =>
|
wenzelm@24151
|
251 |
make_deps ~1 master text parents files)); perform Outdate name));
|
wenzelm@7099
|
252 |
|
wenzelm@7910
|
253 |
fun check_unfinished name =
|
skalberg@15531
|
254 |
if is_finished name then (warning (loader_msg "tried to touch finished theory" [name]); NONE)
|
skalberg@15531
|
255 |
else SOME name;
|
wenzelm@7910
|
256 |
|
wenzelm@7099
|
257 |
in
|
wenzelm@6211
|
258 |
|
wenzelm@7910
|
259 |
fun touch_thys names =
|
wenzelm@19482
|
260 |
List.app outdate_thy (thy_graph Graph.all_succs (map_filter check_unfinished names));
|
wenzelm@7910
|
261 |
|
wenzelm@7910
|
262 |
fun touch_thy name = touch_thys [name];
|
wenzelm@7910
|
263 |
fun touch_child_thys name = touch_thys (thy_graph Graph.imm_succs name);
|
wenzelm@6241
|
264 |
|
skalberg@15570
|
265 |
fun touch_all_thys () = List.app outdate_thy (get_names ());
|
wenzelm@6211
|
266 |
|
wenzelm@7099
|
267 |
end;
|
wenzelm@6211
|
268 |
|
wenzelm@6211
|
269 |
|
wenzelm@7941
|
270 |
(* load_file *)
|
wenzelm@6211
|
271 |
|
wenzelm@7191
|
272 |
local
|
wenzelm@7191
|
273 |
|
wenzelm@24151
|
274 |
fun provide path name info (deps as SOME {update_time, master, text, parents, files}) =
|
wenzelm@23871
|
275 |
(if AList.defined (op =) files path then ()
|
wenzelm@7941
|
276 |
else warning (loader_msg "undeclared dependency of theory" [name] ^
|
wenzelm@21858
|
277 |
" on file: " ^ quote (Path.implode path));
|
wenzelm@24151
|
278 |
SOME (make_deps update_time master text parents
|
wenzelm@24151
|
279 |
(AList.update (op =) (path, SOME info) files)))
|
skalberg@15531
|
280 |
| provide _ _ _ NONE = NONE;
|
wenzelm@7941
|
281 |
|
wenzelm@7941
|
282 |
fun run_file path =
|
wenzelm@22095
|
283 |
(case Option.map (Context.theory_name o Context.the_theory) (ML_Context.get_context ()) of
|
wenzelm@23886
|
284 |
NONE => (ThyLoad.load_ml Path.current path; ())
|
wenzelm@23871
|
285 |
| SOME name =>
|
wenzelm@23871
|
286 |
(case lookup_deps name of
|
wenzelm@23871
|
287 |
SOME deps =>
|
wenzelm@23886
|
288 |
change_deps name (provide path name (ThyLoad.load_ml (master_dir' deps) path))
|
wenzelm@23886
|
289 |
| NONE => (ThyLoad.load_ml Path.current path; ())));
|
wenzelm@6211
|
290 |
|
wenzelm@7191
|
291 |
in
|
wenzelm@7191
|
292 |
|
wenzelm@24057
|
293 |
fun load_file time path =
|
wenzelm@7941
|
294 |
if time then
|
wenzelm@21858
|
295 |
let val name = Path.implode path in
|
wenzelm@7244
|
296 |
timeit (fn () =>
|
wenzelm@9778
|
297 |
(priority ("\n**** Starting file " ^ quote name ^ " ****");
|
wenzelm@9036
|
298 |
run_file path;
|
wenzelm@9778
|
299 |
priority ("**** Finished file " ^ quote name ^ " ****\n")))
|
wenzelm@7244
|
300 |
end
|
wenzelm@24057
|
301 |
else run_file path;
|
wenzelm@7941
|
302 |
|
wenzelm@21858
|
303 |
val use = load_file false o Path.explode;
|
wenzelm@21858
|
304 |
val time_use = load_file true o Path.explode;
|
wenzelm@7191
|
305 |
|
wenzelm@7191
|
306 |
end;
|
wenzelm@6233
|
307 |
|
wenzelm@6233
|
308 |
|
wenzelm@7099
|
309 |
(* load_thy *)
|
wenzelm@7099
|
310 |
|
wenzelm@9490
|
311 |
fun required_by _ [] = ""
|
wenzelm@9490
|
312 |
| required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
|
wenzelm@7099
|
313 |
|
wenzelm@24151
|
314 |
fun load_thy ml time upd_time initiators dir name =
|
wenzelm@7099
|
315 |
let
|
wenzelm@24080
|
316 |
val _ = priority ("Loading theory " ^ quote name ^ required_by " " initiators);
|
wenzelm@24080
|
317 |
val (pos, text, files) =
|
wenzelm@24068
|
318 |
(case get_deps name of
|
wenzelm@24080
|
319 |
SOME {master = SOME ((master_path, _), _), text, files, ...} =>
|
wenzelm@24080
|
320 |
(Position.path master_path, text, files)
|
wenzelm@24068
|
321 |
| _ => error (loader_msg "corrupted dependency information" [name]));
|
wenzelm@7099
|
322 |
val _ = touch_thy name;
|
wenzelm@24151
|
323 |
val _ = CRITICAL (fn () =>
|
wenzelm@24151
|
324 |
change_deps name (Option.map (fn {master, text, parents, files, ...} =>
|
wenzelm@24151
|
325 |
make_deps upd_time master text parents files)));
|
wenzelm@24080
|
326 |
val _ = ThyLoad.load_thy dir name pos text ml (time orelse ! Output.timing);
|
wenzelm@24080
|
327 |
val _ = check_files name;
|
wenzelm@7099
|
328 |
in
|
wenzelm@23939
|
329 |
CRITICAL (fn () =>
|
wenzelm@23939
|
330 |
(change_deps name
|
wenzelm@24151
|
331 |
(Option.map (fn {update_time, master, parents, files, ...} =>
|
wenzelm@24151
|
332 |
make_deps update_time master [] parents files));
|
wenzelm@23939
|
333 |
perform Update name))
|
wenzelm@7099
|
334 |
end;
|
wenzelm@7099
|
335 |
|
wenzelm@7099
|
336 |
|
wenzelm@23967
|
337 |
(* require_thy -- checking database entries wrt. the file-system *)
|
berghofe@15065
|
338 |
|
wenzelm@7211
|
339 |
local
|
wenzelm@6211
|
340 |
|
wenzelm@23886
|
341 |
fun check_ml master (src_path, info) =
|
wenzelm@23871
|
342 |
let val info' =
|
wenzelm@23871
|
343 |
(case info of NONE => NONE
|
wenzelm@23871
|
344 |
| SOME (_, id) =>
|
wenzelm@23886
|
345 |
(case ThyLoad.check_ml (master_dir master) src_path of NONE => NONE
|
wenzelm@23871
|
346 |
| SOME (path', id') => if id <> id' then NONE else SOME (path', id')))
|
wenzelm@23886
|
347 |
in (src_path, info') end;
|
wenzelm@6211
|
348 |
|
wenzelm@23886
|
349 |
fun check_deps ml strict dir name =
|
wenzelm@23871
|
350 |
(case lookup_deps name of
|
wenzelm@23893
|
351 |
SOME NONE => (true, NONE, get_parents name)
|
wenzelm@23893
|
352 |
| NONE =>
|
wenzelm@24068
|
353 |
let val {master, text, imports = parents, uses = files} = ThyLoad.deps_thy dir name ml
|
wenzelm@24068
|
354 |
in (false, init_deps (SOME master) text parents files, parents) end
|
wenzelm@24151
|
355 |
| SOME (deps as SOME {update_time, master, text, parents, files}) =>
|
wenzelm@23910
|
356 |
if not strict andalso can get_theory name then (true, deps, parents)
|
wenzelm@23871
|
357 |
else
|
wenzelm@23893
|
358 |
let val master' = SOME (ThyLoad.check_thy dir name ml) in
|
wenzelm@23871
|
359 |
if master_idents master <> master_idents master'
|
wenzelm@23893
|
360 |
then
|
wenzelm@24068
|
361 |
let val {text = text', imports = parents', uses = files', ...} =
|
wenzelm@24068
|
362 |
ThyLoad.deps_thy dir name ml;
|
wenzelm@24068
|
363 |
in (false, init_deps master' text' parents' files', parents') end
|
wenzelm@23871
|
364 |
else
|
wenzelm@23871
|
365 |
let
|
wenzelm@23871
|
366 |
val checked_files = map (check_ml master') files;
|
wenzelm@24151
|
367 |
val current = update_time >= 0 andalso forall (is_some o snd) checked_files;
|
wenzelm@24151
|
368 |
val update_time' = if current then update_time else ~1;
|
wenzelm@24151
|
369 |
val deps' = SOME (make_deps update_time' master' text parents checked_files);
|
wenzelm@23893
|
370 |
in (current, deps', parents) end
|
wenzelm@23871
|
371 |
end);
|
wenzelm@6211
|
372 |
|
wenzelm@23967
|
373 |
in
|
wenzelm@23967
|
374 |
|
wenzelm@24080
|
375 |
fun require_thys ml time strict keep_strict initiators dir strs tasks =
|
wenzelm@24080
|
376 |
fold_map (require_thy ml time strict keep_strict initiators dir) strs tasks
|
wenzelm@23967
|
377 |
|>> forall I
|
wenzelm@24080
|
378 |
and require_thy ml time strict keep_strict initiators dir str tasks =
|
wenzelm@6211
|
379 |
let
|
wenzelm@21858
|
380 |
val path = Path.expand (Path.explode str);
|
wenzelm@21858
|
381 |
val name = Path.implode (Path.base path);
|
wenzelm@23893
|
382 |
val dir' = Path.append dir (Path.dir path);
|
wenzelm@23871
|
383 |
val _ = member (op =) initiators name andalso error (cycle_msg initiators);
|
wenzelm@7066
|
384 |
in
|
wenzelm@23974
|
385 |
(case try (Graph.get_node (fst tasks)) name of
|
wenzelm@23982
|
386 |
SOME task => (Task.is_finished task, tasks)
|
wenzelm@23967
|
387 |
| NONE =>
|
wenzelm@23967
|
388 |
let
|
wenzelm@23967
|
389 |
val (current, deps, parents) = check_deps ml strict dir' name
|
wenzelm@23967
|
390 |
handle ERROR msg => cat_error msg
|
wenzelm@23967
|
391 |
(loader_msg "the error(s) above occurred while examining theory" [name] ^
|
wenzelm@23967
|
392 |
required_by "\n" initiators);
|
wenzelm@23967
|
393 |
val parent_names = map base_name parents;
|
wenzelm@6211
|
394 |
|
wenzelm@23974
|
395 |
val (parents_current, (tasks_graph', tasks_len')) =
|
wenzelm@24080
|
396 |
require_thys true time (strict andalso keep_strict) keep_strict
|
wenzelm@23974
|
397 |
(name :: initiators) (Path.append dir (master_dir' deps)) parents tasks;
|
wenzelm@23871
|
398 |
|
wenzelm@23967
|
399 |
val all_current = current andalso parents_current;
|
wenzelm@24151
|
400 |
val theory = if all_current then SOME (get_theory name) else NONE;
|
wenzelm@24151
|
401 |
val _ = change_thys (new_deps name parent_names (deps, theory));
|
wenzelm@23967
|
402 |
|
wenzelm@24151
|
403 |
val upd_time = serial ();
|
wenzelm@23974
|
404 |
val tasks_graph'' = tasks_graph' |> new_deps name parent_names
|
wenzelm@23982
|
405 |
(if all_current then Task.Finished
|
wenzelm@24151
|
406 |
else Task.Task (fn () => load_thy ml time upd_time initiators dir' name));
|
wenzelm@23974
|
407 |
val tasks_len'' = if all_current then tasks_len' else tasks_len' + 1;
|
wenzelm@23974
|
408 |
in (all_current, (tasks_graph'', tasks_len'')) end)
|
wenzelm@7066
|
409 |
end;
|
wenzelm@6484
|
410 |
|
wenzelm@23967
|
411 |
end;
|
wenzelm@23967
|
412 |
|
wenzelm@23967
|
413 |
|
wenzelm@23982
|
414 |
(* variations on use_thy -- scheduling required theories *)
|
wenzelm@23967
|
415 |
|
wenzelm@23967
|
416 |
local
|
wenzelm@23967
|
417 |
|
wenzelm@23967
|
418 |
fun schedule_seq tasks =
|
wenzelm@23974
|
419 |
Graph.topological_order tasks
|
wenzelm@23982
|
420 |
|> List.app (fn name => (case Graph.get_node tasks name of Task.Task f => f () | _ => ()));
|
wenzelm@23967
|
421 |
|
wenzelm@24133
|
422 |
fun max_task (task as (_, (Task.Task _, _: int))) NONE = SOME task
|
wenzelm@24068
|
423 |
| max_task (task as (_, (Task.Task _, m))) (task' as SOME (_, (_, m'))) =
|
wenzelm@24068
|
424 |
if m > m' then SOME task else task'
|
wenzelm@24068
|
425 |
| max_task _ task' = task';
|
wenzelm@24068
|
426 |
|
wenzelm@23982
|
427 |
fun next_task G =
|
wenzelm@23982
|
428 |
let
|
wenzelm@24068
|
429 |
val tasks = Graph.minimals G |> map (fn name =>
|
wenzelm@24151
|
430 |
(name, (Graph.get_node G name, length (Graph.imm_succs G name))));
|
wenzelm@24068
|
431 |
val finished = filter (Task.is_finished o fst o snd) tasks;
|
wenzelm@23982
|
432 |
in
|
wenzelm@23982
|
433 |
if not (null finished) then next_task (Graph.del_nodes (map fst finished) G)
|
wenzelm@23982
|
434 |
else if null tasks then ((Task.Finished, I), G)
|
wenzelm@23982
|
435 |
else
|
wenzelm@24068
|
436 |
(case fold max_task tasks NONE of
|
wenzelm@23982
|
437 |
NONE => ((Task.Running, I), G)
|
wenzelm@24068
|
438 |
| SOME (name, (task, _)) =>
|
wenzelm@23982
|
439 |
((task, Graph.del_nodes [name]), Graph.map_node name (K Task.Running) G))
|
wenzelm@23982
|
440 |
end;
|
wenzelm@23974
|
441 |
|
wenzelm@23974
|
442 |
fun schedule_tasks (tasks, n) =
|
wenzelm@23974
|
443 |
let val m = ! Multithreading.max_threads in
|
wenzelm@23974
|
444 |
if m <= 1 orelse n <= 1 then schedule_seq tasks
|
wenzelm@23967
|
445 |
else if Multithreading.self_critical () then
|
wenzelm@23967
|
446 |
(warning (loader_msg "no multithreading within critical section" []);
|
wenzelm@23967
|
447 |
schedule_seq tasks)
|
wenzelm@23974
|
448 |
else
|
wenzelm@23982
|
449 |
(case Multithreading.schedule (Int.min (m, n)) next_task tasks of
|
wenzelm@23974
|
450 |
[] => ()
|
wenzelm@23974
|
451 |
| exns => raise Exn.EXCEPTIONS (exns, ""))
|
wenzelm@23967
|
452 |
end;
|
wenzelm@23967
|
453 |
|
wenzelm@24057
|
454 |
fun gen_use_thy' req dir arg =
|
wenzelm@23974
|
455 |
let val (_, tasks) = req [] dir arg (Graph.empty, 0)
|
wenzelm@24057
|
456 |
in schedule_tasks tasks end;
|
wenzelm@6211
|
457 |
|
wenzelm@23893
|
458 |
fun gen_use_thy req str =
|
wenzelm@23893
|
459 |
let val name = base_name str in
|
wenzelm@23893
|
460 |
check_unfinished warning name;
|
wenzelm@23893
|
461 |
gen_use_thy' req Path.current str;
|
wenzelm@24071
|
462 |
CRITICAL (fn () => ML_Context.set_context (SOME (Context.Theory (get_theory name))))
|
wenzelm@23893
|
463 |
end;
|
wenzelm@7244
|
464 |
|
wenzelm@7211
|
465 |
in
|
wenzelm@7211
|
466 |
|
wenzelm@24080
|
467 |
val quiet_update_thys = gen_use_thy' (require_thys true false true true);
|
wenzelm@24080
|
468 |
val use_thys = gen_use_thy' (require_thys true false true false) Path.current;
|
wenzelm@7244
|
469 |
|
wenzelm@24080
|
470 |
val use_thy = gen_use_thy (require_thy true false true false);
|
wenzelm@24080
|
471 |
val time_use_thy = gen_use_thy (require_thy true true true false);
|
wenzelm@24080
|
472 |
val update_thy = gen_use_thy (require_thy true false true true);
|
wenzelm@6211
|
473 |
|
wenzelm@7211
|
474 |
end;
|
wenzelm@7211
|
475 |
|
wenzelm@6241
|
476 |
|
wenzelm@6666
|
477 |
(* remove_thy *)
|
wenzelm@6666
|
478 |
|
wenzelm@6666
|
479 |
fun remove_thy name =
|
wenzelm@7910
|
480 |
if is_finished name then error (loader_msg "cannot remove finished theory" [name])
|
wenzelm@6666
|
481 |
else
|
wenzelm@6666
|
482 |
let val succs = thy_graph Graph.all_succs [name] in
|
wenzelm@9778
|
483 |
priority (loader_msg "removing" succs);
|
wenzelm@23939
|
484 |
CRITICAL (fn () => (List.app (perform Remove) succs; change_thys (Graph.del_nodes succs)))
|
wenzelm@6666
|
485 |
end;
|
wenzelm@6666
|
486 |
|
wenzelm@6666
|
487 |
|
wenzelm@7066
|
488 |
(* begin / end theory *)
|
wenzelm@6211
|
489 |
|
wenzelm@17365
|
490 |
fun check_uses name uses =
|
wenzelm@23871
|
491 |
let val illegal = map (fn ext => Path.ext ext (Path.basic name)) ("" :: ThyLoad.ml_exts) in
|
wenzelm@17365
|
492 |
(case find_first (member (op =) illegal o Path.base o Path.expand o #1) uses of
|
wenzelm@17365
|
493 |
NONE => ()
|
wenzelm@21858
|
494 |
| SOME (path, _) => error ("Illegal use of theory ML file: " ^ quote (Path.implode path)))
|
wenzelm@17365
|
495 |
end;
|
wenzelm@17365
|
496 |
|
wenzelm@23900
|
497 |
fun begin_theory name parents uses int =
|
wenzelm@6211
|
498 |
let
|
wenzelm@23893
|
499 |
val parent_names = map base_name parents;
|
wenzelm@23886
|
500 |
val dir = master_dir'' (lookup_deps name);
|
wenzelm@7244
|
501 |
val _ = check_unfinished error name;
|
wenzelm@23893
|
502 |
val _ = if int then quiet_update_thys dir parents else ();
|
wenzelm@23893
|
503 |
(* FIXME tmp *)
|
wenzelm@24071
|
504 |
val _ = CRITICAL (fn () =>
|
wenzelm@24071
|
505 |
ML_Context.set_context (SOME (Context.Theory (get_theory (List.last parent_names)))));
|
wenzelm@17365
|
506 |
val _ = check_uses name uses;
|
wenzelm@17365
|
507 |
|
wenzelm@24151
|
508 |
val theory = Theory.begin_theory name (map get_theory parent_names);
|
wenzelm@23900
|
509 |
|
wenzelm@7952
|
510 |
val deps =
|
wenzelm@7952
|
511 |
if known_thy name then get_deps name
|
wenzelm@24068
|
512 |
else init_deps NONE [] parents (map #1 uses);
|
wenzelm@23967
|
513 |
val _ = change_thys (new_deps name parent_names (deps, NONE));
|
wenzelm@23900
|
514 |
|
wenzelm@24151
|
515 |
val update_time = (case deps of NONE => 0 | SOME {update_time, ...} => update_time);
|
wenzelm@24151
|
516 |
val theory' = Present.begin_theory update_time dir uses theory;
|
wenzelm@24151
|
517 |
|
wenzelm@19482
|
518 |
val uses_now = map_filter (fn (x, true) => SOME x | _ => NONE) uses;
|
wenzelm@24151
|
519 |
val ((), theory'') =
|
wenzelm@24151
|
520 |
ML_Context.pass_context (Context.Theory theory') (List.app (load_file false)) uses_now
|
wenzelm@22086
|
521 |
||> Context.the_theory;
|
wenzelm@24151
|
522 |
in theory'' end;
|
wenzelm@7244
|
523 |
|
wenzelm@6211
|
524 |
fun end_theory theory =
|
wenzelm@7099
|
525 |
let
|
wenzelm@16454
|
526 |
val name = Context.theory_name theory;
|
wenzelm@16504
|
527 |
val theory' = Theory.end_theory theory;
|
wenzelm@23900
|
528 |
val _ = change_thy name (fn (deps, _) => (deps, SOME theory'));
|
wenzelm@23900
|
529 |
in theory' end;
|
wenzelm@6211
|
530 |
|
wenzelm@6211
|
531 |
|
wenzelm@24080
|
532 |
(* register existing theories *)
|
wenzelm@6211
|
533 |
|
wenzelm@24080
|
534 |
fun register_thy name =
|
wenzelm@24080
|
535 |
let
|
wenzelm@24080
|
536 |
val _ = priority ("Registering theory " ^ quote name);
|
wenzelm@24080
|
537 |
val _ = get_theory name;
|
wenzelm@24096
|
538 |
val _ = map get_theory (get_parents name);
|
wenzelm@24096
|
539 |
val _ = check_unfinished error name;
|
wenzelm@24080
|
540 |
val _ = touch_thy name;
|
wenzelm@24080
|
541 |
val files = check_files name;
|
wenzelm@24080
|
542 |
val master = #master (ThyLoad.deps_thy Path.current name false);
|
wenzelm@24151
|
543 |
val upd_time = serial ();
|
wenzelm@24080
|
544 |
in
|
wenzelm@24080
|
545 |
CRITICAL (fn () =>
|
wenzelm@24080
|
546 |
(change_deps name
|
wenzelm@24151
|
547 |
(Option.map (fn {parents, ...} => make_deps upd_time (SOME master) [] parents files));
|
wenzelm@24080
|
548 |
perform Update name))
|
wenzelm@24080
|
549 |
end;
|
wenzelm@6211
|
550 |
|
wenzelm@6211
|
551 |
fun register_theory theory =
|
wenzelm@6211
|
552 |
let
|
wenzelm@16454
|
553 |
val name = Context.theory_name theory;
|
wenzelm@6211
|
554 |
val parents = Theory.parents_of theory;
|
wenzelm@16454
|
555 |
val parent_names = map Context.theory_name parents;
|
wenzelm@6211
|
556 |
|
wenzelm@6211
|
557 |
fun err txt bads =
|
wenzelm@23871
|
558 |
error (loader_msg txt bads ^ "\ncannot register theory " ^ quote name);
|
wenzelm@6211
|
559 |
|
wenzelm@6666
|
560 |
val nonfinished = filter_out is_finished parent_names;
|
wenzelm@6211
|
561 |
fun get_variant (x, y_name) =
|
skalberg@15531
|
562 |
if Theory.eq_thy (x, get_theory y_name) then NONE
|
skalberg@15531
|
563 |
else SOME y_name;
|
wenzelm@19482
|
564 |
val variants = map_filter get_variant (parents ~~ parent_names);
|
wenzelm@6211
|
565 |
|
wenzelm@6211
|
566 |
fun register G =
|
skalberg@15531
|
567 |
(Graph.new_node (name, (NONE, SOME theory)) G
|
wenzelm@9327
|
568 |
handle Graph.DUP _ => err "duplicate theory entry" [])
|
wenzelm@6211
|
569 |
|> add_deps name parent_names;
|
wenzelm@6211
|
570 |
in
|
wenzelm@6666
|
571 |
if not (null nonfinished) then err "non-finished parent theories" nonfinished
|
wenzelm@6211
|
572 |
else if not (null variants) then err "different versions of parent theories" variants
|
wenzelm@23939
|
573 |
else CRITICAL (fn () => (change_thys register; perform Update name))
|
wenzelm@6211
|
574 |
end;
|
wenzelm@6211
|
575 |
|
wenzelm@15801
|
576 |
val _ = register_theory ProtoPure.thy;
|
wenzelm@15801
|
577 |
|
wenzelm@24080
|
578 |
|
wenzelm@24080
|
579 |
(* finish all theories *)
|
wenzelm@24080
|
580 |
|
wenzelm@24080
|
581 |
fun finish () = change_thys (Graph.map_nodes (fn (_, entry) => (NONE, entry)));
|
wenzelm@24080
|
582 |
|
wenzelm@24080
|
583 |
|
wenzelm@6211
|
584 |
(*final declarations of this structure*)
|
wenzelm@6211
|
585 |
val theory = get_theory;
|
wenzelm@6211
|
586 |
val names = get_names;
|
wenzelm@6211
|
587 |
|
wenzelm@6211
|
588 |
end;
|
wenzelm@6211
|
589 |
|
wenzelm@5209
|
590 |
structure BasicThyInfo: BASIC_THY_INFO = ThyInfo;
|
wenzelm@5209
|
591 |
open BasicThyInfo;
|