+module type Serializer =
+ sig
+ type status
+ type obj
+ val register:
+ string ->
+ ('a -> refresh_uri_in_term:(NCic.term -> NCic.term) -> status -> status) ->
+ ('a -> obj)
+ val serialize: baseuri:NUri.uri -> obj list -> unit
+ val require: baseuri:NUri.uri -> status -> status
+ end
+
+module Serializer(S: sig type status end) =
+ struct
+ type status = S.status
+ type obj = string * Obj.t
+
+ let require1 = ref (fun _ -> assert false (* unknown data*))
+ let already_registered = ref []
+
+ let register tag require =
+ assert (not (List.mem tag !already_registered));
+ already_registered := tag :: !already_registered;
+ require1 :=
+ (fun (tag',data) as x ->
+ if tag=tag' then
+ require (Obj.magic data) ~refresh_uri_in_term
+ else
+ !require1 x);
+ (fun x -> tag,Obj.repr x)
+
+ let serialize = serialize
+
+ let require ~baseuri status =
+ let dump = require0 ~baseuri in
+ List.fold_right !require1 dump status
+end
+
+let decompile ~baseuri =
+ Unix.unlink (path_of_baseuri baseuri)
+ (* WE ARE NOT REMOVING ALL THE OBJECTS YET! *)
+;;
+
+let fetch_obj uri =
+ let obj = require0 ~baseuri:uri in
+ refresh_uri_in_obj obj
+;;
+
+let resolve name =
+ try
+ HExtlib.filter_map
+ (fun (_,name',nref) -> if name'=name then Some nref else None) !aliases
+ with
+ Not_found -> raise (NCicEnvironment.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 (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri uri)))
+;;
+
+let add_obj status 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;
+ status#set_timestamp (!storage,!aliases,!cache)
+;;