Source file library.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
open Pp
open CErrors
open Util
open Names
open Lib
open Libobject
let raw_extern_library f =
ObjFile.open_out ~file:f
let raw_intern_library f =
System.with_magic_number_check
(fun file -> ObjFile.open_in ~file) f
(** Serialized objects loaded on-the-fly *)
exception Faulty of string
module Delayed :
sig
type 'a delayed
val in_delayed : string -> ObjFile.in_handle -> segment:string -> 'a delayed * Digest.t
val fetch_delayed : 'a delayed -> 'a
end =
struct
type 'a delayed = {
del_file : string;
del_off : int64;
del_digest : Digest.t;
}
let in_delayed f ch ~segment =
let seg = ObjFile.get_segment ch ~segment in
let digest = seg.ObjFile.hash in
{ del_file = f; del_digest = digest; del_off = seg.ObjFile.pos; }, digest
(** Fetching a table of opaque terms at position [pos] in file [f],
expecting to find first a copy of [digest]. *)
let fetch_delayed del =
let { del_digest = digest; del_file = f; del_off = pos; } = del in
let ch = open_in_bin f in
let obj, digest' =
try
let () = LargeFile.seek_in ch pos in
let obj = System.marshal_in f ch in
let digest' = Digest.input ch in
obj, digest'
with e -> close_in ch; raise e
in
close_in ch;
if not (String.equal digest digest') then raise (Faulty f);
obj
end
open Delayed
type compilation_unit_name = DirPath.t
type library_disk = {
md_compiled : Safe_typing.compiled_library;
md_objects : Declaremods.library_objects;
}
type summary_disk = {
md_name : compilation_unit_name;
md_deps : (compilation_unit_name * Safe_typing.vodigest) array;
md_ocaml : string;
}
type library_t = {
library_name : compilation_unit_name;
library_data : library_disk;
library_deps : (compilation_unit_name * Safe_typing.vodigest) array;
library_digests : Safe_typing.vodigest;
library_extra_univs : Univ.ContextSet.t;
}
type library_summary = {
libsum_name : compilation_unit_name;
libsum_digests : Safe_typing.vodigest;
}
let libraries_table : library_summary DPmap.t ref =
Summary.ref DPmap.empty ~name:"LIBRARY"
let libraries_filename_table = ref DPmap.empty
let libraries_loaded_list = Summary.ref [] ~name:"LIBRARY-LOAD"
let find_library dir =
DPmap.find dir !libraries_table
let try_find_library dir =
try find_library dir
with Not_found ->
user_err ~hdr:"Library.find_library"
(str "Unknown library " ++ DirPath.print dir)
let register_library_filename dir f =
libraries_filename_table :=
DPmap.add dir f !libraries_filename_table
let library_full_filename dir =
try DPmap.find dir !libraries_filename_table
with Not_found -> "<unavailable filename>"
let overwrite_library_filenames f =
let f =
if Filename.is_relative f then Filename.concat (Sys.getcwd ()) f else f in
DPmap.iter (fun dir _ -> register_library_filename dir f)
!libraries_table
let library_is_loaded dir =
try let _ = find_library dir in true
with Not_found -> false
let register_loaded_library m =
let libname = m.libsum_name in
let rec aux = function
| [] ->
if Flags.get_native_compiler () then begin
let dirname = Filename.dirname (library_full_filename libname) in
Nativelib.enable_library dirname libname
end;
[libname]
| m'::_ as l when DirPath.equal m' libname -> l
| m'::l' -> m' :: aux l' in
libraries_loaded_list := aux !libraries_loaded_list;
libraries_table := DPmap.add libname m !libraries_table
let loaded_libraries () = !libraries_loaded_list
(** {6 Tables of opaque proof terms} *)
(** We now store opaque proof terms apart from the rest of the environment.
See the [Indirect] constructor in [Lazyconstr.lazy_constr]. This way,
we can quickly load a first half of a .vo file without these opaque
terms, and access them only when a specific command (e.g. Print or
Print Assumptions) needs it. *)
(** Delayed / available tables of opaque terms *)
type table_status =
| ToFetch of Opaqueproof.opaque_disk delayed
| Fetched of Opaqueproof.opaque_disk
let opaque_tables =
ref (DPmap.empty : table_status DPmap.t)
let add_opaque_table dp st =
opaque_tables := DPmap.add dp st !opaque_tables
let access_table what tables dp i =
let t = match DPmap.find dp !tables with
| Fetched t -> t
| ToFetch f ->
let dir_path = Names.DirPath.to_string dp in
Flags.if_verbose Feedback.msg_info (str"Fetching " ++ str what++str" from disk for " ++ str dir_path);
let t =
try fetch_delayed f
with Faulty f ->
user_err ~hdr:"Library.access_table"
(str "The file " ++ str f ++ str " (bound to " ++ str dir_path ++
str ") is corrupted,\ncannot load some " ++
str what ++ str " in it.\n")
in
tables := DPmap.add dp (Fetched t) !tables;
t
in
Opaqueproof.get_opaque_disk i t
let access_opaque_table dp i =
let what = "opaque proofs" in
access_table what opaque_tables dp i
let indirect_accessor = {
Opaqueproof.access_proof = access_opaque_table;
Opaqueproof.access_discharge = Cooking.cook_constr;
}
type seg_sum = summary_disk
type seg_lib = library_disk
type seg_univ =
Univ.ContextSet.t * bool
type seg_proofs = Opaqueproof.opaque_disk
let mk_library sd md digests univs =
{
library_name = sd.md_name;
library_data = md;
library_deps = sd.md_deps;
library_digests = digests;
library_extra_univs = univs;
}
let mk_summary m = {
libsum_name = m.library_name;
libsum_digests = m.library_digests;
}
let intern_from_file f =
let ch = raw_intern_library f in
let (lsd : seg_sum), digest_lsd = ObjFile.marshal_in_segment ch ~segment:"summary" in
let ((lmd : seg_lib), digest_lmd) = ObjFile.marshal_in_segment ch ~segment:"library" in
let (univs : seg_univ option), digest_u = ObjFile.marshal_in_segment ch ~segment:"universes" in
let ((del_opaque : seg_proofs delayed),_) = in_delayed f ch ~segment:"opaques" in
ObjFile.close_in ch;
System.check_caml_version ~caml:lsd.md_ocaml ~file:f;
register_library_filename lsd.md_name f;
add_opaque_table lsd.md_name (ToFetch del_opaque);
let open Safe_typing in
match univs with
| None -> mk_library lsd lmd (Dvo_or_vi digest_lmd) Univ.ContextSet.empty
| Some (uall,true) ->
mk_library lsd lmd (Dvivo (digest_lmd,digest_u)) uall
| Some (_,false) ->
mk_library lsd lmd (Dvo_or_vi digest_lmd) Univ.ContextSet.empty
let rec intern_library ~lib_resolver (needed, contents) (dir, f) from =
try (find_library dir).libsum_digests, (needed, contents)
with Not_found ->
try (DPmap.find dir contents).library_digests, (needed, contents)
with Not_found ->
Feedback.feedback(Feedback.FileDependency (from, DirPath.to_string dir));
let f = match f with Some f -> f | None -> lib_resolver dir in
let m = intern_from_file f in
if not (DirPath.equal dir m.library_name) then
user_err ~hdr:"load_physical_library"
(str "The file " ++ str f ++ str " contains library" ++ spc () ++
DirPath.print m.library_name ++ spc () ++ str "and not library" ++
spc() ++ DirPath.print dir);
Feedback.feedback (Feedback.FileLoaded(DirPath.to_string dir, f));
m.library_digests, intern_library_deps ~lib_resolver (needed, contents) dir m f
and intern_library_deps ~lib_resolver libs dir m from =
let needed, contents =
Array.fold_left (intern_mandatory_library ~lib_resolver dir from)
libs m.library_deps in
(dir :: needed, DPmap.add dir m contents )
and intern_mandatory_library ~lib_resolver caller from libs (dir,d) =
let digest, libs = intern_library ~lib_resolver libs (dir, None) (Some from) in
if not (Safe_typing.digest_match ~actual:digest ~required:d) then
user_err (str "Compiled library " ++ DirPath.print caller ++
str " (in file " ++ str from ++ str ") makes inconsistent assumptions \
over library " ++ DirPath.print dir);
libs
let rec_intern_library ~lib_resolver libs (dir, f) =
let _, libs = intern_library ~lib_resolver libs (dir, Some f) None in
libs
let native_name_from_filename f =
let ch = raw_intern_library f in
let (lmd : seg_sum), digest_lmd = ObjFile.marshal_in_segment ch ~segment:"summary" in
Nativecode.mod_uid_of_dirpath lmd.md_name
let register_library m =
let l = m.library_data in
Declaremods.register_library
m.library_name
l.md_compiled
l.md_objects
m.library_digests
m.library_extra_univs;
register_loaded_library (mk_summary m)
let load_require _ (_,(needed,modl,_)) =
List.iter register_library needed
let open_require i (_,(_,modl,export)) =
Option.iter (fun export ->
let mpl = List.map (fun m -> Unfiltered, MPfile m) modl in
Declaremods.import_modules ~export mpl)
export
let cache_require o =
load_require 1 o;
open_require 1 o
let discharge_require (_,o) = Some o
type require_obj = library_t list * DirPath.t list * bool option
let in_require : require_obj -> obj =
declare_object {(default_object "REQUIRE") with
cache_function = cache_require;
load_function = load_require;
open_function = (fun _ _ -> assert false);
discharge_function = discharge_require;
classify_function = (fun o -> Anticipate o) }
let warn_require_in_module =
CWarnings.create ~name:"require-in-module" ~category:"fragile"
(fun () -> strbrk "Use of “Require” inside a module is fragile." ++ spc() ++
strbrk "It is not recommended to use this functionality in finished proof scripts.")
let require_library_from_dirpath ~lib_resolver modrefl export =
let needed, contents = List.fold_left (rec_intern_library ~lib_resolver) ([], DPmap.empty) modrefl in
let needed = List.rev_map (fun dir -> DPmap.find dir contents) needed in
let modrefl = List.map fst modrefl in
if Lib.is_module_or_modtype () then
begin
warn_require_in_module ();
add_anonymous_leaf (in_require (needed,modrefl,None));
Option.iter (fun export ->
List.iter (fun m -> Declaremods.import_module Unfiltered ~export (MPfile m)) modrefl)
export
end
else
add_anonymous_leaf (in_require (needed,modrefl,export));
()
let load_library_todo f =
let ch = raw_intern_library f in
let (s0 : seg_sum), _ = ObjFile.marshal_in_segment ch ~segment:"summary" in
let (s1 : seg_lib), _ = ObjFile.marshal_in_segment ch ~segment:"library" in
let (s2 : seg_univ option), _ = ObjFile.marshal_in_segment ch ~segment:"universes" in
let tasks, _ = ObjFile.marshal_in_segment ch ~segment:"tasks" in
let (s4 : seg_proofs), _ = ObjFile.marshal_in_segment ch ~segment:"opaques" in
ObjFile.close_in ch;
System.check_caml_version ~caml:s0.md_ocaml ~file:f;
if tasks = None then user_err ~hdr:"restart" (str"not a .vio file");
if s2 = None then user_err ~hdr:"restart" (str"not a .vio file");
if snd (Option.get s2) then user_err ~hdr:"restart" (str"not a .vio file");
s0, s1, Option.get s2, Option.get tasks, s4
let current_deps () =
let map name =
let m = try_find_library name in
(name, m.libsum_digests)
in
List.map map !libraries_loaded_list
let error_recursively_dependent_library dir =
user_err
(strbrk "Unable to use logical name " ++ DirPath.print dir ++
strbrk " to save current library because" ++
strbrk " it already depends on a library of this name.")
let save_library_base f sum lib univs tasks proofs =
let ch = raw_extern_library f in
try
ObjFile.marshal_out_segment ch ~segment:"summary" (sum : seg_sum);
ObjFile.marshal_out_segment ch ~segment:"library" (lib : seg_lib);
ObjFile.marshal_out_segment ch ~segment:"universes" (univs : seg_univ option);
ObjFile.marshal_out_segment ch ~segment:"tasks" (tasks : 'tasks option);
ObjFile.marshal_out_segment ch ~segment:"opaques" (proofs : seg_proofs);
ObjFile.close_out ch
with reraise ->
let reraise = Exninfo.capture reraise in
ObjFile.close_out ch;
Feedback.msg_warning (str "Removed file " ++ str f);
Sys.remove f;
Exninfo.iraise reraise
type 'document todo_proofs =
| ProofsTodoNone
| ProofsTodoSomeEmpty of Future.UUIDSet.t
| ProofsTodoSome of Future.UUIDSet.t * ((Future.UUID.t,'document) Stateid.request * bool) list
let save_library_to todo_proofs ~output_native_objects dir f otab =
assert(
let expected_extension = match todo_proofs with
| ProofsTodoNone -> ".vo"
| ProofsTodoSomeEmpty _ -> ".vos"
| ProofsTodoSome _ -> ".vio"
in
Filename.check_suffix f expected_extension);
let except = match todo_proofs with
| ProofsTodoNone -> Future.UUIDSet.empty
| ProofsTodoSomeEmpty except -> except
| ProofsTodoSome (except,l) -> except
in
let cenv, seg, ast = Declaremods.end_library ~output_native_objects ~except dir in
let opaque_table, f2t_map = Opaqueproof.dump ~except otab in
let tasks, utab =
match todo_proofs with
| ProofsTodoNone -> None, None
| ProofsTodoSomeEmpty _except ->
None, Some (Univ.ContextSet.empty,false)
| ProofsTodoSome (_except, tasks) ->
let tasks =
List.map Stateid.(fun (r,b) ->
try { r with uuid = Some (Future.UUIDMap.find r.uuid f2t_map) }, b
with Not_found -> assert b; { r with uuid = None }, b)
tasks in
Some tasks,
Some (Univ.ContextSet.empty,false)
in
let sd = {
md_name = dir;
md_deps = Array.of_list (current_deps ());
md_ocaml = Coq_config.caml_version;
} in
let md = {
md_compiled = cenv;
md_objects = seg;
} in
if Array.exists (fun (d,_) -> DirPath.equal d dir) sd.md_deps then
error_recursively_dependent_library dir;
save_library_base f sd md utab tasks opaque_table;
if output_native_objects then
let fn = Filename.dirname f ^"/"^ Nativecode.mod_uid_of_dirpath dir in
Nativelib.compile_library ast fn
let save_library_raw f sum lib univs proofs =
save_library_base f sum lib (Some univs) None proofs
let get_used_load_paths () =
String.Set.elements
(List.fold_left (fun acc m -> String.Set.add
(Filename.dirname (library_full_filename m)) acc)
String.Set.empty !libraries_loaded_list)
let _ = Nativelib.get_load_paths := get_used_load_paths