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
15 exception IncludedFileNotCompiled of string * string
19 let refresh_uri uri = NUri.uri_of_string (NUri.string_of_uri uri);;
21 let refresh_uri_in_universe =
22 List.map (fun (x,u) -> x, refresh_uri u)
25 let rec refresh_uri_in_term =
27 | NCic.Meta (i,(n,NCic.Ctx l)) ->
28 NCic.Meta (i,(n,NCic.Ctx (List.map refresh_uri_in_term l)))
29 | NCic.Meta _ as t -> t
30 | NCic.Const (NReference.Ref (u,spec)) ->
31 NCic.Const (NReference.reference_of_spec (refresh_uri u) spec)
32 | NCic.Sort (NCic.Type l) -> NCic.Sort (NCic.Type (refresh_uri_in_universe l))
33 | NCic.Match (NReference.Ref (uri,spec),outtype,term,pl) ->
34 let r = NReference.reference_of_spec (refresh_uri uri) spec in
35 let outtype = refresh_uri_in_term outtype in
36 let term = refresh_uri_in_term term in
37 let pl = List.map refresh_uri_in_term pl in
38 NCic.Match (r,outtype,term,pl)
39 | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
42 let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
43 assert (metasenv = []);
45 refresh_uri uri,height,metasenv,subst,
46 NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
49 let path_of_baseuri ?(no_suffix=false) baseuri =
50 let uri = NUri.string_of_uri baseuri in
51 let path = String.sub uri 4 (String.length uri - 4) in
52 let path = Helm_registry.get "matita.basedir" ^ path in
53 let dirname = Filename.dirname path in
54 HExtlib.mkdir dirname;
61 let require_path path =
62 let ch = open_in path in
63 let mmagic,dump = Marshal.from_channel ch in
65 if mmagic <> magic then
66 raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
71 let require0 ~baseuri =
72 require_path (path_of_baseuri baseuri)
75 let db_path () = Helm_registry.get "matita.basedir" ^ "/ng_db.ng";;
79 [ `Obj of NUri.uri * NCic.obj
80 | `Constr of NCic.universe * NCic.universe] list *
81 (NUri.uri * string * NReference.reference) list *
82 NCic.obj NUri.UriMap.t *
86 let time0 = [],[],NUri.UriMap.empty,[];;
87 let storage = ref [];;
88 let local_aliases = ref [];;
89 let cache = ref NUri.UriMap.empty;;
90 let includes = ref [];;
92 let load_db,set_global_aliases,get_global_aliases,add_deps,get_deps,remove_deps=
93 let global_aliases = ref [] in
94 let rev_includes_map = ref NUri.UriMap.empty in
96 let ch = open_out (db_path ()) in
97 Marshal.to_channel ch (magic,(!global_aliases,!rev_includes_map)) [];
100 HExtlib.mkdir (Helm_registry.get "matita.basedir");
102 let ga,im = require_path (db_path ()) in
105 (fun (uri,name,NReference.Ref (uri2,spec)) ->
106 refresh_uri uri,name,NReference.reference_of_spec (refresh_uri uri2) spec
110 (fun u l im -> NUri.UriMap.add (refresh_uri u) (List.map refresh_uri l) im
111 ) im NUri.UriMap.empty
113 global_aliases := ga;
114 rev_includes_map := im
118 let get_deps_one_step u =
119 try NUri.UriMap.find u !rev_includes_map with Not_found -> [] in
124 if List.mem he res then
127 aux (he::res) (get_deps_one_step he @ tl)
131 rev_includes_map := NUri.UriMap.remove u !rev_includes_map;
134 (fun l -> List.filter (fun uri -> not (NUri.eq u uri)) l) !rev_includes_map;
138 (fun ga -> global_aliases := ga; store_db ()),
139 (fun () -> !global_aliases),
141 rev_includes_map := NUri.UriMap.add u (l @ get_deps u) !rev_includes_map;
149 class type g_status =
151 method timestamp: timestamp
156 val timestamp = (time0 : timestamp)
157 method timestamp = timestamp
158 method set_timestamp v = {< timestamp = v >}
159 method set_library_status
160 : 'status. #g_status as 'status -> 'self
161 = fun o -> self#set_timestamp o#timestamp
164 let time_travel status =
165 let sto,ali,cac,inc = status#timestamp in
166 let diff_len = List.length !storage - List.length sto in
167 let to_be_deleted,_ = HExtlib.split_nth diff_len !storage in
168 if List.length to_be_deleted > 0 then
169 NCicEnvironment.invalidate_item (HExtlib.list_last to_be_deleted);
170 storage := sto; local_aliases := ali; cache := cac; includes := inc
173 let serialize ~baseuri dump =
174 let ch = open_out (path_of_baseuri baseuri) in
175 Marshal.to_channel ch (magic,dump) [];
180 let ch = open_out (path_of_baseuri uri) in
181 Marshal.to_channel ch (magic,obj) [];
185 set_global_aliases (!local_aliases @ get_global_aliases ());
186 List.iter (fun u -> add_deps u [baseuri]) !includes;
187 time_travel (new status)
190 type obj = string * Obj.t
192 class dumpable_status =
194 val dump = ([] : obj list)
196 method set_dump v = {< dump = v >}
199 module type SerializerType =
203 type 'a register_type =
205 refresh_uri_in_universe:(NCic.universe -> NCic.universe) ->
206 refresh_uri_in_term:(NCic.term -> NCic.term) ->
207 dumpable_status -> dumpable_status
209 val register: < run: 'a. string -> 'a register_type -> ('a -> obj) >
210 val serialize: baseuri:NUri.uri -> obj list -> unit
211 (* the obj is the "include" command to be added to the dump list *)
212 val require: baseuri:NUri.uri -> dumpable_status -> dumpable_status * obj
215 module Serializer(D: sig type dumpable_status end) =
217 type dumpable_status = D.dumpable_status
218 type 'a register_type =
220 refresh_uri_in_universe:(NCic.universe -> NCic.universe) ->
221 refresh_uri_in_term:(NCic.term -> NCic.term) ->
222 dumpable_status -> dumpable_status
224 let require1 = ref (fun _ -> assert false) (* unknown data*)
225 let already_registered = ref []
229 method run : 'a. string -> 'a register_type -> ('a -> obj)
231 assert (not (List.mem tag !already_registered));
232 already_registered := tag :: !already_registered;
233 let old_require1 = !require1 in
235 (fun ((tag',data) as x) ->
237 require (Obj.magic data) ~refresh_uri_in_universe ~refresh_uri_in_term
240 (fun x -> tag,Obj.repr x)
243 let serialize = serialize
245 let require2 ~baseuri status =
247 includes := baseuri::!includes;
248 let dump = require0 ~baseuri in
249 List.fold_right !require1 dump status
252 raise (IncludedFileNotCompiled(path_of_baseuri baseuri,NUri.string_of_uri baseuri))
255 let aux baseuri ~refresh_uri_in_universe ~refresh_uri_in_term =
256 (* memorizzo baseuri in una tabella; *)
259 register#run "include" aux
261 let require ~baseuri status =
262 let status = require2 ~baseuri status in
263 status,record_include baseuri
267 let decompile ~baseuri =
268 let baseuris = get_deps baseuri in
269 List.iter (fun baseuri ->
271 HExtlib.safe_remove (path_of_baseuri baseuri);
272 let basepath = path_of_baseuri ~no_suffix:true baseuri in
274 let od = Unix.opendir basepath in
277 let name = Unix.readdir od in
278 if name <> "." && name <> ".." then aux (name::names) else aux names
280 End_of_file -> names in
281 let names = List.map (fun name -> basepath ^ "/" ^ name) (aux []) in
283 List.iter Unix.unlink names;
284 HExtlib.rmdir_descend basepath;
287 (fun (_,_,NReference.Ref (nuri,_)) ->
288 Filename.dirname (NUri.string_of_uri nuri) <> NUri.string_of_uri baseuri
289 ) (get_global_aliases ()))
291 Unix.Unix_error _ -> () (* raised by Unix.opendir, we hope :-) *)
295 LibraryClean.set_decompile_cb
296 (fun ~baseuri -> decompile ~baseuri:(NUri.uri_of_string baseuri));;
299 let obj = require0 ~baseuri:uri in
300 refresh_uri_in_obj obj
306 (fun (_,name',nref) -> if name'=name then Some nref else None)
307 (!local_aliases @ get_global_aliases ())
309 Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy name))
314 (fun (uri',_,nref) ->
315 if NUri.eq uri' uri then Some nref else None) !local_aliases
318 let add_obj status ((u,_,_,_,_) as obj) =
319 NCicEnvironment.check_and_add_obj obj;
320 storage := (`Obj (u,obj))::!storage;
321 let _,height,_,_,obj = obj in
324 NCic.Constant (_,name,None,_,_) ->
325 [u,name,NReference.reference_of_spec u NReference.Decl]
326 | NCic.Constant (_,name,Some _,_,_) ->
327 [u,name,NReference.reference_of_spec u (NReference.Def height)]
328 | NCic.Fixpoint (is_ind,fl,_) ->
330 (fun (_,name,recno,_,_) i ->
332 u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
334 u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
335 | NCic.Inductive (inductive,leftno,il,_) ->
338 (fun (_,iname,_,cl) i ->
342 NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
345 NReference.reference_of_spec u
346 (NReference.Ind (inductive,i,leftno))]
349 local_aliases := references @ !local_aliases;
350 status#set_timestamp (!storage,!local_aliases,!cache,!includes)
353 let add_constraint status u1 u2 =
354 NCicEnvironment.add_lt_constraint u1 u2;
355 storage := (`Constr (u1,u2)) :: !storage;
356 status#set_timestamp (!storage,!local_aliases,!cache,!includes)
363 (function `Obj (u,o) -> Some (u,o) | _ -> None )
368 try NUri.UriMap.find u !cache
370 raise (NCicEnvironment.ObjectNotFound
371 (lazy (NUri.string_of_uri u)))
374 let clear_cache () = cache := NUri.UriMap.empty;;
376 NCicEnvironment.set_get_obj get_obj;;
377 NCicPp.set_get_obj get_obj;;