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 refresh_uri_in_universe =
21 List.map (fun (x,u) -> x, refresh_uri u)
24 let rec refresh_uri_in_term =
26 | NCic.Meta (i,(n,NCic.Ctx l)) ->
27 NCic.Meta (i,(n,NCic.Ctx (List.map refresh_uri_in_term l)))
28 | NCic.Meta _ as t -> t
29 | NCic.Const (NReference.Ref (u,spec)) ->
30 NCic.Const (NReference.reference_of_spec (refresh_uri u) spec)
31 | NCic.Sort (NCic.Type l) -> NCic.Sort (NCic.Type (refresh_uri_in_universe l))
32 | NCic.Match (NReference.Ref (uri,spec),outtype,term,pl) ->
33 let r = NReference.reference_of_spec (refresh_uri uri) spec in
34 let outtype = refresh_uri_in_term outtype in
35 let term = refresh_uri_in_term term in
36 let pl = List.map refresh_uri_in_term pl in
37 NCic.Match (r,outtype,term,pl)
38 | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
41 let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
42 assert (metasenv = []);
44 refresh_uri uri,height,metasenv,subst,
45 NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
48 let path_of_baseuri ?(no_suffix=false) baseuri =
49 let uri = NUri.string_of_uri baseuri in
50 let path = String.sub uri 4 (String.length uri - 4) in
51 let path = Helm_registry.get "matita.basedir" ^ path in
52 let dirname = Filename.dirname path in
53 HExtlib.mkdir dirname;
60 let require_path path =
61 let ch = open_in path in
62 let mmagic,dump = Marshal.from_channel ch in
64 if mmagic <> magic then
65 raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
70 let require0 ~baseuri = require_path (path_of_baseuri baseuri);;
72 let db_path () = Helm_registry.get "matita.basedir" ^ "/ng_db.ng";;
76 [ `Obj of NUri.uri * NCic.obj
77 | `Constr of NCic.universe * NCic.universe] list *
78 (NUri.uri * string * NReference.reference) list *
79 NCic.obj NUri.UriMap.t *
83 let time0 = [],[],NUri.UriMap.empty,[];;
84 let storage = ref [];;
85 let local_aliases = ref [];;
86 let cache = ref NUri.UriMap.empty;;
87 let includes = ref [];;
89 let load_db,set_global_aliases,get_global_aliases,add_deps,get_deps,remove_deps=
90 let global_aliases = ref [] in
91 let rev_includes_map = ref NUri.UriMap.empty in
93 let ch = open_out (db_path ()) in
94 Marshal.to_channel ch (magic,(!global_aliases,!rev_includes_map)) [];
97 HExtlib.mkdir (Helm_registry.get "matita.basedir");
99 let ga,im = require_path (db_path ()) in
102 (fun (uri,name,NReference.Ref (uri2,spec)) ->
103 refresh_uri uri,name,NReference.reference_of_spec (refresh_uri uri2) spec
107 (fun u l im -> NUri.UriMap.add (refresh_uri u) (List.map refresh_uri l) im
108 ) im NUri.UriMap.empty
110 global_aliases := ga;
111 rev_includes_map := im
115 let get_deps_one_step u =
116 try NUri.UriMap.find u !rev_includes_map with Not_found -> [] in
121 if List.mem he res then
124 aux (he::res) (get_deps_one_step he @ tl)
128 rev_includes_map := NUri.UriMap.remove u !rev_includes_map;
131 (fun l -> List.filter (fun uri -> not (NUri.eq u uri)) l) !rev_includes_map;
135 (fun ga -> global_aliases := ga; store_db ()),
136 (fun () -> !global_aliases),
138 rev_includes_map := NUri.UriMap.add u (l @ get_deps u) !rev_includes_map;
146 class type g_status =
148 inherit NRstatus.g_status
149 method timestamp: timestamp
154 inherit NRstatus.status
155 val timestamp = (time0 : timestamp)
156 method timestamp = timestamp
157 method set_timestamp v = {< timestamp = v >}
158 method set_library_status
159 : 'status. #g_status as 'status -> 'self
160 = fun o -> {< timestamp = o#timestamp >}
164 module type SerializerT =
170 ('a -> refresh_uri_in_universe:(NCic.universe -> NCic.universe) -> 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 let time_travel status =
177 let sto,ali,cac,inc = status#timestamp in
178 let diff_len = List.length !storage - List.length sto in
179 let to_be_deleted,_ = HExtlib.split_nth diff_len !storage in
180 if List.length to_be_deleted > 0 then
181 NCicEnvironment.invalidate_item (HExtlib.list_last to_be_deleted);
182 storage := sto; local_aliases := ali; cache := cac; includes := inc
185 let serialize ~baseuri dump =
186 let ch = open_out (path_of_baseuri baseuri) in
187 Marshal.to_channel ch (magic,dump) [];
192 let ch = open_out (path_of_baseuri uri) in
193 Marshal.to_channel ch (magic,obj) [];
197 set_global_aliases (!local_aliases @ get_global_aliases ());
198 List.iter (fun u -> add_deps u [baseuri]) !includes;
199 time_travel (new status)
202 module SerializerF(S: sig type status end) =
204 type status = S.status
205 type obj = string * Obj.t
207 let require1 = ref (fun _ -> assert false (* unknown data*))
208 let already_registered = ref []
210 let register tag require =
211 assert (not (List.mem tag !already_registered));
212 already_registered := tag :: !already_registered;
213 let old_require1 = !require1 in
215 (fun (tag',data) as x ->
217 require (Obj.magic data) ~refresh_uri_in_universe ~refresh_uri_in_term
220 (fun x -> tag,Obj.repr x)
222 let serialize = serialize
224 let require ~baseuri status =
225 includes := baseuri::!includes;
226 let dump = require0 ~baseuri in
227 List.fold_right !require1 dump status
230 type sstatus = status
234 include SerializerF(struct type status = sstatus end)
236 let require ~baseuri status =
237 let rstatus = require ~baseuri (status : #status :> status) in
238 let status = status#set_coerc_db (rstatus#coerc_db) in
239 let status = status#set_uhint_db (rstatus#uhint_db) in
240 let status = status#set_timestamp (rstatus#timestamp) in
244 class type g_dumpable_status =
247 method dump: Serializer.obj list
250 class dumpable_status =
253 val dump = ([] : Serializer.obj list)
255 method set_dump v = {< dump = v >}
256 method set_dumpable_status : 'status. #g_dumpable_status as 'status -> 'self
257 = fun o -> (self#set_dump o#dump)#set_coercion_status o
261 let decompile ~baseuri =
262 let baseuris = get_deps baseuri in
263 List.iter (fun baseuri ->
265 HExtlib.safe_remove (path_of_baseuri baseuri);
266 let basepath = path_of_baseuri ~no_suffix:true baseuri in
268 let od = Unix.opendir basepath in
271 let name = Unix.readdir od in
272 if name <> "." && name <> ".." then aux (name::names) else aux names
274 End_of_file -> names in
275 let names = List.map (fun name -> basepath ^ "/" ^ name) (aux []) in
277 List.iter Unix.unlink names;
278 HExtlib.rmdir_descend basepath;
281 (fun (_,_,NReference.Ref (nuri,_)) ->
282 Filename.dirname (NUri.string_of_uri nuri) <> NUri.string_of_uri baseuri
283 ) (get_global_aliases ()))
285 Unix.Unix_error _ -> () (* raised by Unix.opendir, we hope :-) *)
289 LibraryClean.set_decompile_cb
290 (fun ~baseuri -> decompile ~baseuri:(NUri.uri_of_string baseuri));;
293 let obj = require0 ~baseuri:uri in
294 refresh_uri_in_obj obj
300 (fun (_,name',nref) -> if name'=name then Some nref else None)
301 (!local_aliases @ get_global_aliases ())
303 Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy name))
308 (fun (uri',_,nref) ->
309 if NUri.eq uri' uri then Some nref else None) !local_aliases
312 let add_obj status ((u,_,_,_,_) as obj) =
313 NCicEnvironment.check_and_add_obj obj;
314 storage := (`Obj (u,obj))::!storage;
315 let _,height,_,_,obj = obj in
318 NCic.Constant (_,name,None,_,_) ->
319 [u,name,NReference.reference_of_spec u NReference.Decl]
320 | NCic.Constant (_,name,Some _,_,_) ->
321 [u,name,NReference.reference_of_spec u (NReference.Def height)]
322 | NCic.Fixpoint (is_ind,fl,_) ->
324 (fun (_,name,recno,_,_) i ->
326 u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
328 u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
329 | NCic.Inductive (inductive,leftno,il,_) ->
332 (fun (_,iname,_,cl) i ->
336 NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
339 NReference.reference_of_spec u
340 (NReference.Ind (inductive,i,leftno))]
343 local_aliases := references @ !local_aliases;
344 status#set_timestamp (!storage,!local_aliases,!cache,!includes)
347 let add_constraint status u1 u2 =
348 NCicEnvironment.add_lt_constraint u1 u2;
349 storage := (`Constr (u1,u2)) :: !storage;
350 status#set_timestamp (!storage,!local_aliases,!cache,!includes)
357 (function `Obj (u,o) -> Some (u,o) | _ -> None )
362 try NUri.UriMap.find u !cache
364 let ouri = NCic2OCic.ouri_of_nuri u in
366 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph ouri in
367 let l = OCic2NCic.convert_obj ouri o in
368 List.iter (fun (u,_,_,_,_ as o) -> cache:= NUri.UriMap.add u o !cache) l;
370 with CicEnvironment.Object_not_found u ->
371 raise (NCicEnvironment.ObjectNotFound
372 (lazy (NUri.string_of_uri (OCic2NCic.nuri_of_ouri u))))
375 let clear_cache () = cache := NUri.UriMap.empty;;
377 NCicEnvironment.set_get_obj get_obj;;
378 NCicPp.set_get_obj get_obj;;