+let serialize ~baseuri dump =
+ let ch = open_out (path_of_baseuri baseuri) in
+ Marshal.to_channel ch (magic,dump) [Marshal.Closures];
+ close_out ch;
+ List.iter
+ (fun (uri,obj) ->
+ let ch = open_out (path_of_baseuri uri) in
+ Marshal.to_channel ch (magic,obj) [];
+ close_out ch
+ ) !storage;
+ time_travel time0;
+;;
+
+let require ~baseuri =
+ let ch = open_in (path_of_baseuri baseuri) in
+ let mmagic,dump = Marshal.from_channel ch in
+ close_in ch;
+ if mmagic <> magic then
+ raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
+ else
+ dump
+;;
+
+let decompile ~baseuri =
+ Unix.unlink (path_of_baseuri baseuri)
+ (* WE ARE NOT REMOVING ALL THE OBJECTS YET! *)
+;;
+
+(* the miracles of Marshal... *)
+let fetch_obj uri =
+ let obj = require ~baseuri:uri in
+ (* here we need to refresh the URIs! *)
+ obj
+;;
+
+let resolve name =
+ try
+ HExtlib.filter_map
+ (fun (_,name',nref) -> if name'=name then Some nref else None) !aliases
+ with
+ Not_found -> raise (ObjectNotFound (lazy name))
+;;
+
+let aliases_of uri =
+ try
+ HExtlib.filter_map
+ (fun (uri',_,nref) -> if NUri.eq uri' uri then Some nref else None) !aliases
+ with
+ Not_found -> raise (ObjectNotFound (lazy (NUri.string_of_uri uri)))
+;;
+
+let add_obj u obj =
+ storage := (u,obj)::!storage;
+ let _,height,_,_,obj = obj in
+ let references =
+ match obj with
+ NCic.Constant (_,name,None,_,_) ->
+ [u,name,NReference.reference_of_spec u NReference.Decl]
+ | NCic.Constant (_,name,Some _,_,_) ->
+ [u,name,NReference.reference_of_spec u (NReference.Def height)]
+ | NCic.Fixpoint (is_ind,fl,_) ->
+ HExtlib.list_mapi
+ (fun (_,name,recno,_,_) i ->
+ if is_ind then
+ u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
+ else
+ u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
+ | NCic.Inductive (inductive,leftno,il,_) ->
+ List.flatten
+ (HExtlib.list_mapi
+ (fun (_,iname,_,cl) i ->
+ HExtlib.list_mapi
+ (fun (_,cname,_) j->
+ u,cname,
+ NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
+ ) cl @
+ [u,iname,
+ NReference.reference_of_spec u
+ (NReference.Ind (inductive,i,leftno))]
+ ) il)
+ in
+ aliases := references @ !aliases;
+ !storage,!aliases,!cache
+;;