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 aliases = ref [];;
24 let cache = ref NUri.UriMap.empty;;
28 val timestamp = (time0 : timestamp)
29 method timestamp = timestamp
30 method set_timestamp v = {< timestamp = v >}
33 let time_travel status =
34 let sto,ali,cac = status#timestamp in
35 storage := sto; aliases := ali; cache := cac
38 let path_of_baseuri baseuri =
39 let uri = NUri.string_of_uri baseuri in
40 let path = String.sub uri 4 (String.length uri - 4) in
41 let path = Helm_registry.get "matita.basedir" ^ path in
42 let dirname = Filename.dirname path in
43 HExtlib.mkdir dirname;
49 let require0 ~baseuri =
50 let ch = open_in (path_of_baseuri baseuri) in
51 let mmagic,dump = Marshal.from_channel ch in
53 if mmagic <> magic then
54 raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
59 let serialize ~baseuri dump =
60 let ch = open_out (path_of_baseuri baseuri) in
61 Marshal.to_channel ch (magic,dump) [Marshal.Closures];
65 let ch = open_out (path_of_baseuri uri) in
66 Marshal.to_channel ch (magic,obj) [];
69 time_travel (new status)
72 let refresh_uri uri = NUri.uri_of_string (NUri.string_of_uri uri);;
74 let rec refresh_uri_in_term =
76 NCic.Const (NReference.Ref (u,spec)) ->
77 NCic.Const (NReference.reference_of_spec (refresh_uri u) spec)
78 | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
81 let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
82 assert (metasenv = []);
84 uri,height,metasenv,subst,
85 NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
88 module type Serializer =
94 ('a -> refresh_uri_in_term:(NCic.term -> NCic.term) -> status -> status) ->
96 val serialize: baseuri:NUri.uri -> obj list -> unit
97 val require: baseuri:NUri.uri -> status -> status
100 module Serializer(S: sig type status end) =
102 type status = S.status
103 type obj = string * Obj.t
105 let require1 = ref (fun _ -> assert false (* unknown data*))
106 let already_registered = ref []
108 let register tag require =
109 assert (not (List.mem tag !already_registered));
110 already_registered := tag :: !already_registered;
112 (fun (tag',data) as x ->
114 require (Obj.magic data) ~refresh_uri_in_term
117 (fun x -> tag,Obj.repr x)
119 let serialize = serialize
121 let require ~baseuri status =
122 let dump = require0 ~baseuri in
123 List.fold_right !require1 dump status
126 let decompile ~baseuri =
127 Unix.unlink (path_of_baseuri baseuri)
128 (* WE ARE NOT REMOVING ALL THE OBJECTS YET! *)
132 let obj = require0 ~baseuri:uri in
133 refresh_uri_in_obj obj
139 (fun (_,name',nref) -> if name'=name then Some nref else None) !aliases
141 Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy name))
147 (fun (uri',_,nref) -> if NUri.eq uri' uri then Some nref else None) !aliases
149 Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri uri)))
152 let add_obj status u obj =
153 storage := (u,obj)::!storage;
154 let _,height,_,_,obj = obj in
157 NCic.Constant (_,name,None,_,_) ->
158 [u,name,NReference.reference_of_spec u NReference.Decl]
159 | NCic.Constant (_,name,Some _,_,_) ->
160 [u,name,NReference.reference_of_spec u (NReference.Def height)]
161 | NCic.Fixpoint (is_ind,fl,_) ->
163 (fun (_,name,recno,_,_) i ->
165 u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
167 u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
168 | NCic.Inductive (inductive,leftno,il,_) ->
171 (fun (_,iname,_,cl) i ->
175 NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
178 NReference.reference_of_spec u
179 (NReference.Ind (inductive,i,leftno))]
182 aliases := references @ !aliases;
183 status#set_timestamp (!storage,!aliases,!cache)
187 try List.assq u !storage
191 try NUri.UriMap.find u !cache
193 let ouri = NCic2OCic.ouri_of_nuri u in
195 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph ouri in
196 let l = OCic2NCic.convert_obj ouri o in
197 List.iter (fun (u,_,_,_,_ as o) -> cache:= NUri.UriMap.add u o !cache) l;
199 with CicEnvironment.Object_not_found u ->
200 raise (NCicEnvironment.ObjectNotFound
201 (lazy (NUri.string_of_uri (OCic2NCic.nuri_of_ouri u))))
204 let clear_cache () = cache := NUri.UriMap.empty;;
206 NCicEnvironment.set_get_obj get_obj;;
207 NCicPp.set_get_obj get_obj;;