2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
14 exception LibraryOutOfSync of string Lazy.t
18 let refresh_uri uri = NUri.uri_of_string (NUri.string_of_uri uri);;
20 let rec refresh_uri_in_term =
22 NCic.Const (NReference.Ref (u,spec)) ->
23 NCic.Const (NReference.reference_of_spec (refresh_uri u) spec)
24 | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
27 let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
28 assert (metasenv = []);
30 uri,height,metasenv,subst,
31 NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
34 let path_of_baseuri ?(no_suffix=false) baseuri =
35 let uri = NUri.string_of_uri baseuri in
36 let path = String.sub uri 4 (String.length uri - 4) in
37 let path = Helm_registry.get "matita.basedir" ^ path in
38 let dirname = Filename.dirname path in
39 HExtlib.mkdir dirname;
46 let require_path path =
47 let ch = open_in path in
48 let mmagic,dump = Marshal.from_channel ch in
50 if mmagic <> magic then
51 raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
56 let require0 ~baseuri = require_path (path_of_baseuri baseuri);;
58 let db_path () = Helm_registry.get "matita.basedir" ^ "/ng_db.ng";;
61 (NUri.uri * NCic.obj) list *
62 (NUri.uri * string * NReference.reference) list *
63 NCic.obj NUri.UriMap.t *
66 let time0 = [],[],NUri.UriMap.empty,[];;
67 let storage = ref [];;
68 let local_aliases = ref [];;
69 let cache = ref NUri.UriMap.empty;;
70 let includes = ref [];;
72 let load_db,set_global_aliases,get_global_aliases,add_deps,get_deps,remove_deps=
73 let global_aliases = ref [] in
74 let rev_includes_map = ref NUri.UriMap.empty in
76 let ch = open_out (db_path ()) in
77 Marshal.to_channel ch (magic,(!global_aliases,!rev_includes_map)) [];
80 HExtlib.mkdir (Helm_registry.get "matita.basedir");
82 let ga,im = require_path (db_path ()) in
85 (fun (uri,name,NReference.Ref (uri2,spec)) ->
86 refresh_uri uri,name,NReference.reference_of_spec (refresh_uri uri2) spec
90 (fun u l im -> NUri.UriMap.add (refresh_uri u) (List.map refresh_uri l) im
91 ) im NUri.UriMap.empty
94 rev_includes_map := im
98 let get_deps_one_step u =
99 try NUri.UriMap.find u !rev_includes_map with Not_found -> [] in
104 if List.mem he res then
107 aux (he::res) (get_deps_one_step he @ tl)
111 rev_includes_map := NUri.UriMap.remove u !rev_includes_map;
114 (fun l -> List.filter (fun uri -> not (NUri.eq u uri)) l) !rev_includes_map;
118 (fun ga -> global_aliases := ga; store_db ()),
119 (fun () -> !global_aliases),
121 rev_includes_map := NUri.UriMap.add u (l @ get_deps u) !rev_includes_map;
131 val timestamp = (time0 : timestamp)
132 method timestamp = timestamp
133 method set_timestamp v = {< timestamp = v >}
134 method set_library_status
135 : 'status. < timestamp : timestamp; .. > as 'status -> 'self
136 = fun o -> {< timestamp = o#timestamp >}
139 let time_travel status =
140 let sto,ali,cac,inc = status#timestamp in
141 let diff_len = List.length !storage - List.length sto in
142 let to_be_deleted,_ = HExtlib.split_nth diff_len !storage in
143 if List.length to_be_deleted > 0 then
144 let u,_ = HExtlib.list_last to_be_deleted in
145 NCicEnvironment.invalidate_obj u;
146 storage := sto; local_aliases := ali; cache := cac; includes := inc
149 let serialize ~baseuri dump =
150 let ch = open_out (path_of_baseuri baseuri) in
151 Marshal.to_channel ch (magic,dump) [];
155 let ch = open_out (path_of_baseuri uri) in
156 Marshal.to_channel ch (magic,obj) [];
159 set_global_aliases (!local_aliases @ get_global_aliases ());
160 List.iter (fun u -> add_deps u [baseuri]) !includes;
161 time_travel (new status)
164 module type Serializer =
170 ('a -> refresh_uri_in_term:(NCic.term -> NCic.term) -> status -> status) ->
172 val serialize: baseuri:NUri.uri -> obj list -> unit
173 val require: baseuri:NUri.uri -> status -> status
176 module Serializer(S: sig type status end) =
178 type status = S.status
179 type obj = string * Obj.t
181 let require1 = ref (fun _ -> assert false (* unknown data*))
182 let already_registered = ref []
184 let register tag require =
185 assert (not (List.mem tag !already_registered));
186 already_registered := tag :: !already_registered;
188 (fun (tag',data) as x ->
190 require (Obj.magic data) ~refresh_uri_in_term
193 (fun x -> tag,Obj.repr x)
195 let serialize = serialize
197 let require ~baseuri status =
198 includes := baseuri::!includes;
199 let dump = require0 ~baseuri in
200 List.fold_right !require1 dump status
203 let decompile ~baseuri =
204 let baseuris = get_deps baseuri in
205 List.iter (fun baseuri ->
207 HExtlib.safe_remove (path_of_baseuri baseuri);
208 let basepath = path_of_baseuri ~no_suffix:true baseuri in
210 let od = Unix.opendir basepath in
213 let name = Unix.readdir od in
214 if name <> "." && name <> ".." then aux (name::names) else aux names
216 End_of_file -> names in
217 let names = List.map (fun name -> basepath ^ "/" ^ name) (aux []) in
219 List.iter Unix.unlink names;
220 HExtlib.rmdir_descend basepath;
223 (fun (_,_,NReference.Ref (nuri,_)) ->
224 Filename.dirname (NUri.string_of_uri nuri) <> NUri.string_of_uri baseuri
225 ) (get_global_aliases ()))
227 Unix.Unix_error _ -> () (* raised by Unix.opendir, we hope :-) *)
231 LibraryClean.set_decompile_cb
232 (fun ~baseuri -> decompile ~baseuri:(NUri.uri_of_string baseuri));;
235 let obj = require0 ~baseuri:uri in
236 refresh_uri_in_obj obj
242 (fun (_,name',nref) -> if name'=name then Some nref else None)
243 (!local_aliases @ get_global_aliases ())
245 Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy name))
251 (fun (uri',_,nref) ->
252 if NUri.eq uri' uri then Some nref else None) !local_aliases
254 Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri uri)))
257 let add_obj status ((u,_,_,_,_) as obj) =
258 NCicEnvironment.check_and_add_obj obj;
259 storage := (u,obj)::!storage;
260 let _,height,_,_,obj = obj in
263 NCic.Constant (_,name,None,_,_) ->
264 [u,name,NReference.reference_of_spec u NReference.Decl]
265 | NCic.Constant (_,name,Some _,_,_) ->
266 [u,name,NReference.reference_of_spec u (NReference.Def height)]
267 | NCic.Fixpoint (is_ind,fl,_) ->
269 (fun (_,name,recno,_,_) i ->
271 u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
273 u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
274 | NCic.Inductive (inductive,leftno,il,_) ->
277 (fun (_,iname,_,cl) i ->
281 NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
284 NReference.reference_of_spec u
285 (NReference.Ind (inductive,i,leftno))]
288 local_aliases := references @ !local_aliases;
289 status#set_timestamp (!storage,!local_aliases,!cache,!includes)
293 try List.assq u !storage
297 try NUri.UriMap.find u !cache
299 let ouri = NCic2OCic.ouri_of_nuri u in
301 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph ouri in
302 let l = OCic2NCic.convert_obj ouri o in
303 List.iter (fun (u,_,_,_,_ as o) -> cache:= NUri.UriMap.add u o !cache) l;
305 with CicEnvironment.Object_not_found u ->
306 raise (NCicEnvironment.ObjectNotFound
307 (lazy (NUri.string_of_uri (OCic2NCic.nuri_of_ouri u))))
310 let clear_cache () = cache := NUri.UriMap.empty;;
312 NCicEnvironment.set_get_obj get_obj;;
313 NCicPp.set_get_obj get_obj;;