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
17 (NUri.uri * NCic.obj) list *
18 (NUri.uri * string * NReference.reference) list *
19 NCic.obj NUri.UriMap.t;;
21 let time0 = [],[],NUri.UriMap.empty;;
22 let storage = ref [];;
23 let local_aliases = ref [];;
24 let global_aliases = ref [];;
25 let cache = ref NUri.UriMap.empty;;
29 val timestamp = (time0 : timestamp)
30 method timestamp = timestamp
31 method set_timestamp v = {< timestamp = v >}
32 method set_library_status
33 : 'status. < timestamp : timestamp; .. > as 'status -> 'self
34 = fun o -> {< timestamp = o#timestamp >}
37 let time_travel status =
38 let sto,ali,cac = status#timestamp in
39 storage := sto; local_aliases := ali; cache := cac
42 let path_of_baseuri ?(no_suffix=false) baseuri =
43 let uri = NUri.string_of_uri baseuri in
44 let path = String.sub uri 4 (String.length uri - 4) in
45 let path = Helm_registry.get "matita.basedir" ^ path in
46 let dirname = Filename.dirname path in
47 HExtlib.mkdir dirname;
56 let require_path path =
57 let ch = open_in path in
58 let mmagic,dump = Marshal.from_channel ch in
60 if mmagic <> magic then
61 raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
66 let require0 ~baseuri = require_path (path_of_baseuri baseuri);;
68 let db_path () = Helm_registry.get "matita.basedir" ^ "/ng_db.ng";;
72 global_aliases := require_path (db_path ())
77 let serialize ~baseuri dump =
78 let ch = open_out (path_of_baseuri baseuri) in
79 Marshal.to_channel ch (magic,dump) [];
83 let ch = open_out (path_of_baseuri uri) in
84 Marshal.to_channel ch (magic,obj) [];
87 global_aliases := !local_aliases @ !global_aliases;
88 let ch = open_out (db_path ()) in
89 Marshal.to_channel ch (magic,!global_aliases) [];
91 time_travel (new status)
94 let refresh_uri uri = NUri.uri_of_string (NUri.string_of_uri uri);;
96 let rec refresh_uri_in_term =
98 NCic.Const (NReference.Ref (u,spec)) ->
99 NCic.Const (NReference.reference_of_spec (refresh_uri u) spec)
100 | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
103 let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
104 assert (metasenv = []);
106 uri,height,metasenv,subst,
107 NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
110 module type Serializer =
116 ('a -> refresh_uri_in_term:(NCic.term -> NCic.term) -> status -> status) ->
118 val serialize: baseuri:NUri.uri -> obj list -> unit
119 val require: baseuri:NUri.uri -> status -> status
122 module Serializer(S: sig type status end) =
124 type status = S.status
125 type obj = string * Obj.t
127 let require1 = ref (fun _ -> assert false (* unknown data*))
128 let already_registered = ref []
130 let register tag require =
131 assert (not (List.mem tag !already_registered));
132 already_registered := tag :: !already_registered;
134 (fun (tag',data) as x ->
136 require (Obj.magic data) ~refresh_uri_in_term
139 (fun x -> tag,Obj.repr x)
141 let serialize = serialize
143 let require ~baseuri status =
144 let dump = require0 ~baseuri in
145 List.fold_right !require1 dump status
148 let decompile ~baseuri =
149 HExtlib.safe_remove (path_of_baseuri baseuri);
150 let basepath = path_of_baseuri ~no_suffix:true baseuri in
152 let od = Unix.opendir basepath in
155 let name = Unix.readdir od in
156 if name <> "." && name <> ".." then aux (name::names) else aux names
158 End_of_file -> names in
159 let names = List.map (fun name -> basepath ^ "/" ^ name) (aux []) in
161 List.iter Unix.unlink names;
162 HExtlib.rmdir_descend basepath
164 Unix.Unix_error _ -> () (* raised by Unix.opendir, we hope :-) *)
167 LibraryClean.set_decompile_cb
168 (fun ~baseuri -> decompile ~baseuri:(NUri.uri_of_string baseuri));;
171 let obj = require0 ~baseuri:uri in
172 refresh_uri_in_obj obj
178 (fun (_,name',nref) -> if name'=name then Some nref else None)
179 (!local_aliases @ !global_aliases)
181 Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy name))
187 (fun (uri',_,nref) ->
188 if NUri.eq uri' uri then Some nref else None) !local_aliases
190 Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri uri)))
193 let add_obj status u obj =
194 storage := (u,obj)::!storage;
195 let _,height,_,_,obj = obj in
198 NCic.Constant (_,name,None,_,_) ->
199 [u,name,NReference.reference_of_spec u NReference.Decl]
200 | NCic.Constant (_,name,Some _,_,_) ->
201 [u,name,NReference.reference_of_spec u (NReference.Def height)]
202 | NCic.Fixpoint (is_ind,fl,_) ->
204 (fun (_,name,recno,_,_) i ->
206 u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
208 u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
209 | NCic.Inductive (inductive,leftno,il,_) ->
212 (fun (_,iname,_,cl) i ->
216 NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
219 NReference.reference_of_spec u
220 (NReference.Ind (inductive,i,leftno))]
223 local_aliases := references @ !local_aliases;
224 status#set_timestamp (!storage,!local_aliases,!cache)
228 try List.assq u !storage
232 try NUri.UriMap.find u !cache
234 let ouri = NCic2OCic.ouri_of_nuri u in
236 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph ouri in
237 let l = OCic2NCic.convert_obj ouri o in
238 List.iter (fun (u,_,_,_,_ as o) -> cache:= NUri.UriMap.add u o !cache) l;
240 with CicEnvironment.Object_not_found u ->
241 raise (NCicEnvironment.ObjectNotFound
242 (lazy (NUri.string_of_uri (OCic2NCic.nuri_of_ouri u))))
245 let clear_cache () = cache := NUri.UriMap.empty;;
247 NCicEnvironment.set_get_obj get_obj;;
248 NCicPp.set_get_obj get_obj;;