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