]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_kernel/nCicLibrary.ml
The global db is now updated during decompilation.
[helm.git] / helm / software / components / ng_kernel / nCicLibrary.ml
index 414e5c5de1fad5296fefc5f9fdd8549fbb87ffe6..a36b70bf4a6404b6b1302eb182076b95fa71c4ea 100644 (file)
 
 (* $Id$ *)
 
-exception ObjectNotFound of string Lazy.t
 exception LibraryOutOfSync of string Lazy.t
 
-type timestamp =
- (NUri.uri * NCic.obj) list *
- (NUri.uri * string * NReference.reference) list *
- NCic.obj NUri.UriMap.t;;
-
-let time0 = [],[],NUri.UriMap.empty;;
-let storage = ref [];;
-let aliases = ref [];;
-let cache = ref NUri.UriMap.empty;;
-
-let time_travel (sto,ali,cac) = storage := sto; aliases := ali; cache := cac;;
+let magic = 0;;
 
-let path_of_baseuri baseuri =
+let path_of_baseuri ?(no_suffix=false) baseuri =
  let uri = NUri.string_of_uri baseuri in
  let path = String.sub uri 4 (String.length uri - 4) in
  let path = Helm_registry.get "matita.basedir" ^ path in
  let dirname = Filename.dirname path in
   HExtlib.mkdir dirname;
-  path ^ ".ng"
+  if no_suffix then
+   path
+  else
+   path ^ ".ng"
 ;;
 
-let magic = 0;;
-
-let require0 ~baseuri =
- let ch = open_in (path_of_baseuri baseuri) in
+let require_path path =
+ let ch = open_in path in
  let mmagic,dump = Marshal.from_channel ch in
   close_in ch;
   if mmagic <> magic then
@@ -47,9 +37,56 @@ let require0 ~baseuri =
    dump
 ;;
 
+let require0 ~baseuri = require_path (path_of_baseuri baseuri);;
+
+let db_path () = Helm_registry.get "matita.basedir" ^ "/ng_db.ng";;
+
+type timestamp =
+ (NUri.uri * NCic.obj) list *
+ (NUri.uri * string * NReference.reference) list *
+ NCic.obj NUri.UriMap.t;;
+
+let time0 = [],[],NUri.UriMap.empty;;
+let storage = ref [];;
+let local_aliases = ref [];;
+let set_global_aliases,get_global_aliases =
+ let global_aliases = ref [] in
+  let store_db () =
+   let ch = open_out (db_path ()) in
+   Marshal.to_channel ch (magic,!global_aliases) [];
+   close_out ch;
+  in
+  (fun ga -> global_aliases := ga; store_db ()),
+  (fun () -> !global_aliases)
+;;
+
+let init () =
+ try
+  set_global_aliases (require_path (db_path ()))
+ with
+  Sys_error _ -> ()
+;;
+
+let cache = ref NUri.UriMap.empty;;
+
+class status =
+ object
+  val timestamp = (time0 : timestamp)
+  method timestamp = timestamp
+  method set_timestamp v = {< timestamp = v >}
+  method set_library_status
+   : 'status. < timestamp : timestamp; .. > as 'status -> 'self
+   = fun o -> {< timestamp = o#timestamp >}
+ end
+
+let time_travel status =
+ let sto,ali,cac = status#timestamp in
+  storage := sto; local_aliases := ali; cache := cac
+;;
+
 let serialize ~baseuri dump =
  let ch = open_out (path_of_baseuri baseuri) in
- Marshal.to_channel ch (magic,dump) [Marshal.Closures];
+ Marshal.to_channel ch (magic,dump) [];
  close_out ch;
  List.iter
   (fun (uri,obj) ->
@@ -57,14 +94,34 @@ let serialize ~baseuri dump =
     Marshal.to_channel ch (magic,obj) [];
     close_out ch
   ) !storage;
- time_travel time0
+ set_global_aliases (!local_aliases @ get_global_aliases ());
+ time_travel (new status)
+;;
+
+let refresh_uri uri = NUri.uri_of_string (NUri.string_of_uri uri);;
+
+let rec refresh_uri_in_term =
+ function
+    NCic.Const (NReference.Ref (u,spec)) ->
+     NCic.Const (NReference.reference_of_spec (refresh_uri u) spec)
+  | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
+;;
+
+let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
+ assert (metasenv = []);
+ assert (subst = []);
+ uri,height,metasenv,subst,
+  NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
 ;;
 
 module type Serializer =
  sig
   type status
   type obj
-  val register: string -> ('a -> status -> status) -> ('a -> 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
@@ -72,7 +129,7 @@ module type Serializer =
 module Serializer(S: sig type status end) =
  struct
   type status = S.status
-  type obj = Obj.t
+  type obj = string * Obj.t
 
   let require1 = ref (fun _ -> assert false (* unknown data*))
   let already_registered = ref []
@@ -82,45 +139,70 @@ module Serializer(S: sig type status end) =
    already_registered := tag :: !already_registered;
    require1 :=
     (fun (tag',data) as x ->
-     if tag=tag' then Obj.magic require data else Obj.magic !require1 x);
-   Obj.magic (fun x -> tag,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 (Obj.magic !require1) dump status
+    List.fold_right !require1 dump status
 end
 
 let decompile ~baseuri =
- Unix.unlink (path_of_baseuri baseuri)
- (* WE ARE NOT REMOVING ALL THE OBJECTS YET! *)
+ HExtlib.safe_remove (path_of_baseuri baseuri);
+ let basepath = path_of_baseuri ~no_suffix:true baseuri in
+ try
+  let od = Unix.opendir basepath in
+  let rec aux names =
+   try
+    let name = Unix.readdir od in
+     if name <> "." && name <> ".." then aux (name::names) else aux names
+   with
+    End_of_file -> names in
+  let names = List.map (fun name -> basepath ^ "/" ^ name) (aux []) in
+   Unix.closedir od;
+   List.iter Unix.unlink names;
+   HExtlib.rmdir_descend basepath;
+   set_global_aliases
+    (List.filter
+     (fun (_,_,NReference.Ref (nuri,_)) ->
+       Filename.dirname (NUri.string_of_uri nuri) <> NUri.string_of_uri baseuri
+     ) (get_global_aliases ()))
+ with
+  Unix.Unix_error _ -> () (* raised by Unix.opendir, we hope :-) *)
 ;;
 
-(* the miracles of Marshal... *)
+LibraryClean.set_decompile_cb
+ (fun ~baseuri -> decompile ~baseuri:(NUri.uri_of_string baseuri));;
+
 let fetch_obj uri =
  let obj = require0 ~baseuri:uri in
-  (* here we need to refresh the URIs! *)
-  obj
+  refresh_uri_in_obj obj
 ;;
 
 let resolve name =
  try
   HExtlib.filter_map
-   (fun (_,name',nref) -> if name'=name then Some nref else None) !aliases
+   (fun (_,name',nref) -> if name'=name then Some nref else None)
+   (!local_aliases @ get_global_aliases ())
  with
-  Not_found -> raise (ObjectNotFound (lazy name))
+  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
+   (fun (uri',_,nref) ->
+     if NUri.eq uri' uri then Some nref else None) !local_aliases
  with
-  Not_found -> raise (ObjectNotFound (lazy (NUri.string_of_uri uri)))
+  Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri uri)))
 ;;
 
-let add_obj u obj =
+let add_obj status u obj =
  storage := (u,obj)::!storage;
   let _,height,_,_,obj = obj in
   let references =
@@ -150,8 +232,8 @@ let add_obj u obj =
               (NReference.Ind (inductive,i,leftno))]
          ) il)
   in
-  aliases := references @ !aliases;
-  !storage,!aliases,!cache
+  local_aliases := references @ !local_aliases;
+  status#set_timestamp (!storage,!local_aliases,!cache)
 ;;
 
 let get_obj u =
@@ -168,8 +250,11 @@ let get_obj u =
       List.iter (fun (u,_,_,_,_ as o) -> cache:= NUri.UriMap.add u o !cache) l;
       HExtlib.list_last l
     with CicEnvironment.Object_not_found u -> 
-      raise (ObjectNotFound 
+      raise (NCicEnvironment.ObjectNotFound 
                (lazy (NUri.string_of_uri (OCic2NCic.nuri_of_ouri u))))
 ;;
 
 let clear_cache () = cache := NUri.UriMap.empty;;
+
+NCicEnvironment.set_get_obj get_obj;;
+NCicPp.set_get_obj get_obj;;