berghofe@3604
|
1 |
(* Title: Pure/Thy/thy_info.ML
|
wenzelm@6211
|
2 |
Author: Markus Wenzel, TU Muenchen
|
berghofe@3604
|
3 |
|
wenzelm@38267
|
4 |
Global theory info database, with auto-loading according to theory and
|
wenzelm@15801
|
5 |
file dependencies.
|
wenzelm@3976
|
6 |
*)
|
berghofe@3604
|
7 |
|
berghofe@3604
|
8 |
signature THY_INFO =
|
berghofe@3604
|
9 |
sig
|
wenzelm@38270
|
10 |
datatype action = Update | Remove
|
wenzelm@7099
|
11 |
val add_hook: (action -> string -> unit) -> unit
|
wenzelm@26614
|
12 |
val get_names: unit -> string list
|
wenzelm@44548
|
13 |
val status: unit -> unit
|
wenzelm@7288
|
14 |
val lookup_theory: string -> theory option
|
wenzelm@6211
|
15 |
val get_theory: string -> theory
|
wenzelm@24563
|
16 |
val is_finished: string -> bool
|
wenzelm@26983
|
17 |
val master_directory: string -> Path.T
|
wenzelm@24080
|
18 |
val loaded_files: string -> Path.T list
|
wenzelm@29422
|
19 |
val remove_thy: string -> unit
|
wenzelm@29422
|
20 |
val kill_thy: string -> unit
|
wenzelm@38449
|
21 |
val use_thys_wrt: Path.T -> string list -> unit
|
wenzelm@24057
|
22 |
val use_thys: string list -> unit
|
wenzelm@24057
|
23 |
val use_thy: string -> unit
|
wenzelm@45059
|
24 |
val toplevel_begin_theory: Path.T -> string -> string list -> (Path.T * bool) list -> theory
|
wenzelm@38217
|
25 |
val register_thy: theory -> unit
|
wenzelm@6666
|
26 |
val finish: unit -> unit
|
berghofe@3604
|
27 |
end;
|
berghofe@3604
|
28 |
|
wenzelm@37216
|
29 |
structure Thy_Info: THY_INFO =
|
berghofe@3604
|
30 |
struct
|
berghofe@3604
|
31 |
|
wenzelm@7099
|
32 |
(** theory loader actions and hooks **)
|
wenzelm@7099
|
33 |
|
wenzelm@38270
|
34 |
datatype action = Update | Remove;
|
wenzelm@7099
|
35 |
|
wenzelm@7099
|
36 |
local
|
wenzelm@44517
|
37 |
val hooks = Synchronized.var "Thy_Info.hooks" ([]: (action -> string -> unit) list);
|
wenzelm@7099
|
38 |
in
|
wenzelm@44517
|
39 |
fun add_hook f = Synchronized.change hooks (cons f);
|
wenzelm@44517
|
40 |
fun perform action name =
|
wenzelm@44517
|
41 |
List.app (fn f => (try (fn () => f action name) (); ())) (Synchronized.value hooks);
|
wenzelm@7099
|
42 |
end;
|
wenzelm@7099
|
43 |
|
wenzelm@7099
|
44 |
|
wenzelm@7099
|
45 |
|
wenzelm@6211
|
46 |
(** thy database **)
|
wenzelm@3976
|
47 |
|
wenzelm@6211
|
48 |
(* messages *)
|
berghofe@3604
|
49 |
|
wenzelm@23871
|
50 |
fun loader_msg txt [] = "Theory loader: " ^ txt
|
wenzelm@23871
|
51 |
| loader_msg txt names = "Theory loader: " ^ txt ^ " " ^ commas_quote names;
|
berghofe@3604
|
52 |
|
wenzelm@6211
|
53 |
val show_path = space_implode " via " o map quote;
|
wenzelm@9332
|
54 |
fun cycle_msg names = loader_msg ("cyclic dependency of " ^ show_path names) [];
|
berghofe@3604
|
55 |
|
berghofe@3604
|
56 |
|
wenzelm@6666
|
57 |
(* derived graph operations *)
|
berghofe@3604
|
58 |
|
wenzelm@9327
|
59 |
fun add_deps name parents G = Graph.add_deps_acyclic (name, parents) G
|
wenzelm@9332
|
60 |
handle Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
|
berghofe@3604
|
61 |
|
wenzelm@38273
|
62 |
fun new_entry name parents entry =
|
wenzelm@38266
|
63 |
Graph.new_node (name, entry) #> add_deps name parents;
|
wenzelm@38266
|
64 |
|
berghofe@3604
|
65 |
|
wenzelm@6211
|
66 |
(* thy database *)
|
berghofe@3604
|
67 |
|
wenzelm@6211
|
68 |
type deps =
|
wenzelm@42826
|
69 |
{master: (Path.T * SHA1.digest), (*master dependencies for thy file*)
|
wenzelm@40994
|
70 |
imports: string list}; (*source specification of imports (partially qualified)*)
|
wenzelm@23886
|
71 |
|
wenzelm@38451
|
72 |
fun make_deps master imports : deps = {master = master, imports = imports};
|
wenzelm@23871
|
73 |
|
wenzelm@38269
|
74 |
fun master_dir (d: deps option) = the_default Path.current (Option.map (Path.dir o #1 o #master) d);
|
wenzelm@45099
|
75 |
fun base_name s = Path.implode (Path.base (Path.explode s));
|
wenzelm@23967
|
76 |
|
wenzelm@6211
|
77 |
local
|
wenzelm@38176
|
78 |
val database = Unsynchronized.ref (Graph.empty: (deps option * theory option) Graph.T);
|
wenzelm@6211
|
79 |
in
|
wenzelm@6362
|
80 |
fun get_thys () = ! database;
|
wenzelm@38448
|
81 |
fun change_thys f = NAMED_CRITICAL "Thy_Info" (fn () => Unsynchronized.change database f);
|
berghofe@3604
|
82 |
end;
|
wenzelm@5209
|
83 |
|
wenzelm@5209
|
84 |
|
wenzelm@6211
|
85 |
(* access thy graph *)
|
wenzelm@6211
|
86 |
|
wenzelm@6211
|
87 |
fun thy_graph f x = f (get_thys ()) x;
|
wenzelm@9417
|
88 |
|
wenzelm@23967
|
89 |
fun get_names () = Graph.topological_order (get_thys ());
|
wenzelm@6211
|
90 |
|
wenzelm@44548
|
91 |
fun status () =
|
wenzelm@44548
|
92 |
List.app (Output.status o Markup.markup_only o Markup.loaded_theory) (get_names ());
|
wenzelm@44548
|
93 |
|
wenzelm@6211
|
94 |
|
wenzelm@6211
|
95 |
(* access thy *)
|
wenzelm@6211
|
96 |
|
wenzelm@7935
|
97 |
fun lookup_thy name =
|
skalberg@15531
|
98 |
SOME (thy_graph Graph.get_node name) handle Graph.UNDEF _ => NONE;
|
wenzelm@7935
|
99 |
|
wenzelm@16047
|
100 |
val known_thy = is_some o lookup_thy;
|
wenzelm@6211
|
101 |
|
wenzelm@6211
|
102 |
fun get_thy name =
|
wenzelm@6211
|
103 |
(case lookup_thy name of
|
skalberg@15531
|
104 |
SOME thy => thy
|
skalberg@15531
|
105 |
| NONE => error (loader_msg "nothing known about theory" [name]));
|
wenzelm@6211
|
106 |
|
wenzelm@6211
|
107 |
|
wenzelm@6211
|
108 |
(* access deps *)
|
wenzelm@6211
|
109 |
|
skalberg@15570
|
110 |
val lookup_deps = Option.map #1 o lookup_thy;
|
wenzelm@6211
|
111 |
val get_deps = #1 o get_thy;
|
wenzelm@6211
|
112 |
|
wenzelm@26983
|
113 |
val is_finished = is_none o get_deps;
|
wenzelm@38269
|
114 |
val master_directory = master_dir o get_deps;
|
wenzelm@7191
|
115 |
|
wenzelm@23871
|
116 |
fun get_parents name =
|
wenzelm@23967
|
117 |
thy_graph Graph.imm_preds name handle Graph.UNDEF _ =>
|
berghofe@6654
|
118 |
error (loader_msg "nothing known about theory" [name]);
|
berghofe@6654
|
119 |
|
wenzelm@6211
|
120 |
|
wenzelm@6211
|
121 |
(* access theory *)
|
wenzelm@6211
|
122 |
|
wenzelm@7687
|
123 |
fun lookup_theory name =
|
wenzelm@7687
|
124 |
(case lookup_thy name of
|
wenzelm@38445
|
125 |
SOME (_, SOME theory) => SOME theory
|
skalberg@15531
|
126 |
| _ => NONE);
|
wenzelm@7288
|
127 |
|
wenzelm@6211
|
128 |
fun get_theory name =
|
wenzelm@7288
|
129 |
(case lookup_theory name of
|
wenzelm@23871
|
130 |
SOME theory => theory
|
wenzelm@6211
|
131 |
| _ => error (loader_msg "undefined theory entry for" [name]));
|
wenzelm@6211
|
132 |
|
wenzelm@38448
|
133 |
fun loaded_files name = NAMED_CRITICAL "Thy_Info" (fn () =>
|
wenzelm@38212
|
134 |
(case get_deps name of
|
wenzelm@38212
|
135 |
NONE => []
|
wenzelm@38448
|
136 |
| SOME {master = (thy_path, _), ...} => thy_path :: Thy_Load.loaded_files (get_theory name)));
|
wenzelm@38212
|
137 |
|
wenzelm@6211
|
138 |
|
wenzelm@6211
|
139 |
|
wenzelm@6211
|
140 |
(** thy operations **)
|
wenzelm@6211
|
141 |
|
wenzelm@44517
|
142 |
(* main loader actions *)
|
wenzelm@29422
|
143 |
|
wenzelm@38448
|
144 |
fun remove_thy name = NAMED_CRITICAL "Thy_Info" (fn () =>
|
wenzelm@38268
|
145 |
if is_finished name then error (loader_msg "attempt to change finished theory" [name])
|
wenzelm@29422
|
146 |
else
|
wenzelm@29434
|
147 |
let
|
wenzelm@29434
|
148 |
val succs = thy_graph Graph.all_succs [name];
|
wenzelm@40392
|
149 |
val _ = Output.urgent_message (loader_msg "removing" succs);
|
wenzelm@38445
|
150 |
val _ = List.app (perform Remove) succs;
|
wenzelm@38445
|
151 |
val _ = change_thys (Graph.del_nodes succs);
|
wenzelm@38445
|
152 |
in () end);
|
wenzelm@29422
|
153 |
|
wenzelm@38448
|
154 |
fun kill_thy name = NAMED_CRITICAL "Thy_Info" (fn () =>
|
wenzelm@38266
|
155 |
if known_thy name then remove_thy name
|
wenzelm@38445
|
156 |
else ());
|
wenzelm@7099
|
157 |
|
wenzelm@44517
|
158 |
fun update_thy deps theory = NAMED_CRITICAL "Thy_Info" (fn () =>
|
wenzelm@44517
|
159 |
let
|
wenzelm@44517
|
160 |
val name = Context.theory_name theory;
|
wenzelm@44517
|
161 |
val parents = map Context.theory_name (Theory.parents_of theory);
|
wenzelm@44517
|
162 |
val _ = kill_thy name;
|
wenzelm@44517
|
163 |
val _ = map get_theory parents;
|
wenzelm@44517
|
164 |
val _ = change_thys (new_entry name parents (SOME deps, SOME theory));
|
wenzelm@44517
|
165 |
val _ = perform Update name;
|
wenzelm@44517
|
166 |
in () end);
|
wenzelm@44517
|
167 |
|
wenzelm@7099
|
168 |
|
wenzelm@24209
|
169 |
(* scheduling loader tasks *)
|
wenzelm@24209
|
170 |
|
wenzelm@44518
|
171 |
type result = theory * unit future * (unit -> unit);
|
wenzelm@44518
|
172 |
|
wenzelm@38266
|
173 |
datatype task =
|
wenzelm@44518
|
174 |
Task of string list * (theory list -> result) |
|
wenzelm@38266
|
175 |
Finished of theory;
|
wenzelm@38266
|
176 |
|
wenzelm@38266
|
177 |
fun task_finished (Task _) = false
|
wenzelm@38266
|
178 |
| task_finished (Finished _) = true;
|
wenzelm@24209
|
179 |
|
wenzelm@45041
|
180 |
fun task_parents deps (parents: string list) = map (the o AList.lookup (op =) deps) parents;
|
wenzelm@45041
|
181 |
|
wenzelm@29119
|
182 |
local
|
wenzelm@29119
|
183 |
|
wenzelm@44518
|
184 |
fun finish_thy ((thy, present, commit): result) =
|
wenzelm@44518
|
185 |
(Global_Theory.join_proofs thy; Future.join present; commit (); thy);
|
wenzelm@44518
|
186 |
|
wenzelm@45041
|
187 |
val schedule_seq =
|
wenzelm@45041
|
188 |
Graph.schedule (fn deps => fn (_, task) =>
|
wenzelm@45041
|
189 |
(case task of
|
wenzelm@45041
|
190 |
Task (parents, body) => finish_thy (body (task_parents deps parents))
|
wenzelm@45041
|
191 |
| Finished thy => thy)) #> ignore;
|
wenzelm@45041
|
192 |
|
wenzelm@45041
|
193 |
val schedule_futures = uninterruptible (fn _ =>
|
wenzelm@45041
|
194 |
Graph.schedule (fn deps => fn (name, task) =>
|
wenzelm@45041
|
195 |
(case task of
|
wenzelm@38266
|
196 |
Task (parents, body) =>
|
wenzelm@45177
|
197 |
(singleton o Future.forks)
|
wenzelm@45177
|
198 |
{name = "theory:" ^ name, group = NONE,
|
wenzelm@45177
|
199 |
deps = map (Future.task_of o #2) deps, pri = 0, interrupts = true}
|
wenzelm@45041
|
200 |
(fn () =>
|
wenzelm@45041
|
201 |
(case filter (not o can Future.join o #2) deps of
|
wenzelm@45041
|
202 |
[] => body (map (#1 o Future.join) (task_parents deps parents))
|
wenzelm@45041
|
203 |
| bad =>
|
wenzelm@45041
|
204 |
error (loader_msg ("failed to load " ^ quote name ^
|
wenzelm@45041
|
205 |
" (unresolved " ^ commas_quote (map #1 bad) ^ ")") [])))
|
wenzelm@45041
|
206 |
| Finished thy => Future.value (thy, Future.value (), I)))
|
wenzelm@45041
|
207 |
#> maps (fn result => (finish_thy (Future.join result); []) handle exn => [Exn.Exn exn])
|
wenzelm@45125
|
208 |
#> rev #> Par_Exn.release_all) #> ignore;
|
wenzelm@29429
|
209 |
|
wenzelm@24209
|
210 |
in
|
wenzelm@24209
|
211 |
|
wenzelm@32801
|
212 |
fun schedule_tasks tasks =
|
wenzelm@29119
|
213 |
if not (Multithreading.enabled ()) then schedule_seq tasks
|
wenzelm@29119
|
214 |
else if Multithreading.self_critical () then
|
wenzelm@24209
|
215 |
(warning (loader_msg "no multithreading within critical section" []);
|
wenzelm@24209
|
216 |
schedule_seq tasks)
|
wenzelm@29119
|
217 |
else schedule_futures tasks;
|
wenzelm@24209
|
218 |
|
wenzelm@24209
|
219 |
end;
|
wenzelm@24209
|
220 |
|
wenzelm@24209
|
221 |
|
wenzelm@23967
|
222 |
(* require_thy -- checking database entries wrt. the file-system *)
|
berghofe@15065
|
223 |
|
wenzelm@7211
|
224 |
local
|
wenzelm@6211
|
225 |
|
wenzelm@38266
|
226 |
fun required_by _ [] = ""
|
wenzelm@38266
|
227 |
| required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
|
wenzelm@38266
|
228 |
|
wenzelm@44583
|
229 |
fun load_thy initiators update_time deps text name parents =
|
wenzelm@38266
|
230 |
let
|
wenzelm@38266
|
231 |
val _ = kill_thy name;
|
wenzelm@40392
|
232 |
val _ = Output.urgent_message ("Loading theory " ^ quote name ^ required_by " " initiators);
|
wenzelm@38266
|
233 |
|
wenzelm@41796
|
234 |
val {master = (thy_path, _), imports} = deps;
|
wenzelm@38380
|
235 |
val dir = Path.dir thy_path;
|
wenzelm@38266
|
236 |
val pos = Path.position thy_path;
|
wenzelm@42874
|
237 |
val (_, _, uses) = Thy_Header.read pos text;
|
wenzelm@38266
|
238 |
|
wenzelm@44583
|
239 |
val (theory, present) = Thy_Load.load_thy update_time dir name imports uses pos text parents;
|
wenzelm@44517
|
240 |
fun commit () = update_thy deps theory;
|
wenzelm@43000
|
241 |
in (theory, present, commit) end;
|
wenzelm@38266
|
242 |
|
wenzelm@24175
|
243 |
fun check_deps dir name =
|
wenzelm@23871
|
244 |
(case lookup_deps name of
|
wenzelm@42874
|
245 |
SOME NONE => (true, NONE, get_parents name)
|
wenzelm@23893
|
246 |
| NONE =>
|
wenzelm@42874
|
247 |
let val {master, text, imports, ...} = Thy_Load.check_thy dir name
|
wenzelm@42874
|
248 |
in (false, SOME (make_deps master imports, text), imports) end
|
wenzelm@43000
|
249 |
| SOME (SOME {master, ...}) =>
|
wenzelm@42874
|
250 |
let
|
wenzelm@42874
|
251 |
val {master = master', text = text', imports = imports', ...} = Thy_Load.check_thy dir name;
|
wenzelm@42874
|
252 |
val deps' = SOME (make_deps master' imports', text');
|
wenzelm@42874
|
253 |
val current =
|
wenzelm@42874
|
254 |
#2 master = #2 master' andalso
|
wenzelm@42874
|
255 |
(case lookup_theory name of
|
wenzelm@42874
|
256 |
NONE => false
|
wenzelm@42874
|
257 |
| SOME theory => Thy_Load.all_current theory);
|
wenzelm@42874
|
258 |
in (current, deps', imports') end);
|
wenzelm@6211
|
259 |
|
wenzelm@23967
|
260 |
in
|
wenzelm@23967
|
261 |
|
wenzelm@38141
|
262 |
fun require_thys initiators dir strs tasks =
|
wenzelm@38141
|
263 |
fold_map (require_thy initiators dir) strs tasks |>> forall I
|
wenzelm@38141
|
264 |
and require_thy initiators dir str tasks =
|
wenzelm@6211
|
265 |
let
|
wenzelm@21858
|
266 |
val path = Path.expand (Path.explode str);
|
wenzelm@21858
|
267 |
val name = Path.implode (Path.base path);
|
wenzelm@7066
|
268 |
in
|
wenzelm@32801
|
269 |
(case try (Graph.get_node tasks) name of
|
wenzelm@24209
|
270 |
SOME task => (task_finished task, tasks)
|
wenzelm@23967
|
271 |
| NONE =>
|
wenzelm@23967
|
272 |
let
|
wenzelm@44528
|
273 |
val dir' = Path.append dir (Path.dir path);
|
wenzelm@44528
|
274 |
val _ = member (op =) initiators name andalso error (cycle_msg initiators);
|
wenzelm@44528
|
275 |
|
wenzelm@42874
|
276 |
val (current, deps, imports) = check_deps dir' name
|
wenzelm@23967
|
277 |
handle ERROR msg => cat_error msg
|
wenzelm@23967
|
278 |
(loader_msg "the error(s) above occurred while examining theory" [name] ^
|
wenzelm@23967
|
279 |
required_by "\n" initiators);
|
wenzelm@38270
|
280 |
|
wenzelm@38451
|
281 |
val parents = map base_name imports;
|
wenzelm@38266
|
282 |
val (parents_current, tasks') =
|
wenzelm@42874
|
283 |
require_thys (name :: initiators)
|
wenzelm@42874
|
284 |
(Path.append dir (master_dir (Option.map #1 deps))) imports tasks;
|
wenzelm@23871
|
285 |
|
wenzelm@23967
|
286 |
val all_current = current andalso parents_current;
|
wenzelm@38266
|
287 |
val task =
|
wenzelm@38266
|
288 |
if all_current then Finished (get_theory name)
|
wenzelm@38266
|
289 |
else
|
wenzelm@38269
|
290 |
(case deps of
|
wenzelm@38269
|
291 |
NONE => raise Fail "Malformed deps"
|
wenzelm@42874
|
292 |
| SOME (dep, text) =>
|
wenzelm@42874
|
293 |
let val update_time = serial ()
|
wenzelm@38451
|
294 |
in Task (parents, load_thy initiators update_time dep text name) end);
|
wenzelm@38451
|
295 |
in (all_current, new_entry name parents task tasks') end)
|
wenzelm@7066
|
296 |
end;
|
wenzelm@6484
|
297 |
|
wenzelm@23967
|
298 |
end;
|
wenzelm@23967
|
299 |
|
wenzelm@23967
|
300 |
|
wenzelm@38268
|
301 |
(* use_thy *)
|
wenzelm@23967
|
302 |
|
wenzelm@38449
|
303 |
fun use_thys_wrt dir arg =
|
wenzelm@38141
|
304 |
schedule_tasks (snd (require_thys [] dir arg Graph.empty));
|
wenzelm@23967
|
305 |
|
wenzelm@38449
|
306 |
val use_thys = use_thys_wrt Path.current;
|
wenzelm@38212
|
307 |
val use_thy = use_thys o single;
|
wenzelm@7211
|
308 |
|
wenzelm@6241
|
309 |
|
wenzelm@38268
|
310 |
(* toplevel begin theory -- without maintaining database *)
|
wenzelm@6211
|
311 |
|
wenzelm@45059
|
312 |
fun toplevel_begin_theory dir name imports uses =
|
wenzelm@38266
|
313 |
let
|
wenzelm@38266
|
314 |
val _ = kill_thy name;
|
wenzelm@38449
|
315 |
val _ = use_thys_wrt dir imports;
|
wenzelm@44583
|
316 |
val parents = map (get_theory o base_name) imports;
|
wenzelm@44583
|
317 |
in Thy_Load.begin_theory dir name imports uses parents end;
|
wenzelm@38212
|
318 |
|
wenzelm@17365
|
319 |
|
wenzelm@38266
|
320 |
(* register theory *)
|
wenzelm@6211
|
321 |
|
wenzelm@38217
|
322 |
fun register_thy theory =
|
wenzelm@24080
|
323 |
let
|
wenzelm@38217
|
324 |
val name = Context.theory_name theory;
|
wenzelm@42874
|
325 |
val {master, ...} = Thy_Load.check_thy (Thy_Load.master_directory theory) name;
|
wenzelm@41796
|
326 |
val imports = Thy_Load.imports_of theory;
|
wenzelm@24080
|
327 |
in
|
wenzelm@38448
|
328 |
NAMED_CRITICAL "Thy_Info" (fn () =>
|
wenzelm@38445
|
329 |
(kill_thy name;
|
wenzelm@40392
|
330 |
Output.urgent_message ("Registering theory " ^ quote name);
|
wenzelm@44517
|
331 |
update_thy (make_deps master imports) theory))
|
wenzelm@24080
|
332 |
end;
|
wenzelm@6211
|
333 |
|
wenzelm@24080
|
334 |
|
wenzelm@24080
|
335 |
(* finish all theories *)
|
wenzelm@24080
|
336 |
|
haftmann@39265
|
337 |
fun finish () = change_thys (Graph.map (fn _ => fn (_, entry) => (NONE, entry)));
|
wenzelm@24080
|
338 |
|
wenzelm@6211
|
339 |
end;
|