]> matita.cs.unibo.it Git - helm.git/blobdiff - matita/components/ng_library/nCicLibrary.ml
Use of standard OCaml syntax
[helm.git] / matita / components / ng_library / nCicLibrary.ml
index cbc9c17b8b6809386ac2f5666d422135d1a3a829..1852673f896c7a55c12a6babaa459347e8b657d4 100644 (file)
@@ -25,27 +25,30 @@ let refresh_uri_in_universe =
 let refresh_uri_in_reference (NReference.Ref (uri,spec)) =
  NReference.reference_of_spec (refresh_uri uri) spec
 
-let rec refresh_uri_in_term =
- function
+let refresh_uri_in_term status =
+ let rec aux =
+  function
   | NCic.Meta (i,(n,NCic.Ctx l)) ->
-     NCic.Meta (i,(n,NCic.Ctx (List.map refresh_uri_in_term l)))
+     NCic.Meta (i,(n,NCic.Ctx (List.map aux l)))
   | NCic.Meta _ as t -> t
   | NCic.Const ref -> NCic.Const (refresh_uri_in_reference ref)
   | NCic.Sort (NCic.Type l) -> NCic.Sort (NCic.Type (refresh_uri_in_universe l))
   | NCic.Match (NReference.Ref (uri,spec),outtype,term,pl) ->
      let r = NReference.reference_of_spec (refresh_uri uri) spec in
-     let outtype = refresh_uri_in_term outtype in
-     let term = refresh_uri_in_term term in
-     let pl = List.map refresh_uri_in_term pl in
+     let outtype = aux outtype in
+     let term = aux term in
+     let pl = List.map aux pl in
       NCic.Match (r,outtype,term,pl)
-  | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
+  | t -> NCicUtils.map status (fun _ _ -> ()) () (fun _ -> aux) t
+in
+ aux
 ;;
 
-let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
+let refresh_uri_in_obj status (uri,height,metasenv,subst,obj_kind) =
  assert (metasenv = []);
  assert (subst = []);
  refresh_uri uri,height,metasenv,subst,
-  NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
+  NCicUntrusted.map_obj_kind (refresh_uri_in_term status) obj_kind
 ;;
 
 let ng_path_of_baseuri ?(no_suffix=false) baseuri =
@@ -141,20 +144,27 @@ let load_db,set_global_aliases,get_global_aliases,add_deps,get_deps,remove_deps=
 
 let init = load_db;;
 
-class status =
+class virtual status =
  object
+  inherit NCic.status
   val timestamp = (time0 : timestamp)
   method timestamp = timestamp
   method set_timestamp v = {< timestamp = v >}
  end
 
-let time_travel status =
+let time_travel0 (sto,ali) =
+ let diff_len = List.length !storage - List.length sto in
+ let to_be_deleted,_ = HExtlib.split_nth diff_len !storage in
+  if List.length to_be_deleted > 0 then
+   List.iter NCicEnvironment.invalidate_item to_be_deleted;
+  storage := sto; local_aliases := ali
+;;
+
+let time_travel status = time_travel0 status#timestamp;;
+
+let replace status =
  let sto,ali = status#timestamp in
-  let diff_len = List.length !storage - List.length sto in
-  let to_be_deleted,_ = HExtlib.split_nth diff_len !storage in
-   if List.length to_be_deleted > 0 then
-    List.iter NCicEnvironment.invalidate_item to_be_deleted;
-   storage := sto; local_aliases := ali
+  storage := sto; local_aliases := ali
 ;;
 
 type obj = string * Obj.t
@@ -173,7 +183,7 @@ class dumpable_status =
   method dump = db
   method set_dump v = {< db = v >}
   method set_dumpable_status
-   : 'status. #g_dumpable_status as 'status -> 'self
+   : 'status. (#g_dumpable_status as 'status) -> 'self
    = fun o -> {< db = o#dump >}
  end
 
@@ -185,6 +195,16 @@ let dump_obj status obj =
  status#set_dump {status#dump with objs = obj::status#dump.objs }
 ;;
 
+let remove_objects ~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 map name = Sys.remove (Filename.concat path name) in
+   if HExtlib.is_dir path && HExtlib.is_regular (path ^ ".ng") then begin
+      HLog.warn ("removing contents of baseuri: " ^ uri);
+      Array.iter map (Sys.readdir path)
+   end
+
 module type SerializerType =
  sig
   type dumpable_status
@@ -192,7 +212,7 @@ module type SerializerType =
   type 'a register_type =
    'a ->
     refresh_uri_in_universe:(NCic.universe -> NCic.universe) ->
-    refresh_uri_in_term:(NCic.term -> NCic.term) ->
+    refresh_uri_in_term:(NCic.status -> NCic.term -> NCic.term) ->
     refresh_uri_in_reference:(NReference.reference -> NReference.reference) ->
     alias_only:bool ->
      dumpable_status -> dumpable_status
@@ -205,13 +225,13 @@ module type SerializerType =
   val dependencies_of: baseuri:NUri.uri -> string list
  end
 
-module Serializer(D: sig type dumpable_s val get: dumpable_s -> dumpable_status  val set: dumpable_s -> dumpable_status -> dumpable_s end) =
+module Serializer(D: sig type dumpable_s = private #dumpable_status end) =
  struct
   type dumpable_status = D.dumpable_s
   type 'a register_type =
    'a ->
     refresh_uri_in_universe:(NCic.universe -> NCic.universe) ->
-    refresh_uri_in_term:(NCic.term -> NCic.term) ->
+    refresh_uri_in_term:(NCic.status -> NCic.term -> NCic.term) ->
     refresh_uri_in_reference:(NReference.reference -> NReference.reference) ->
     alias_only:bool ->
      dumpable_status -> dumpable_status
@@ -237,10 +257,13 @@ module Serializer(D: sig type dumpable_s val get: dumpable_s -> dumpable_status
    end
 
   let serialize ~baseuri status =
-   let status = D.get status in
    let ch = open_out (ng_path_of_baseuri baseuri) in
    Marshal.to_channel ch (magic,(status#dump.dependencies,status#dump.objs)) [];
    close_out ch;
+
+   if Helm_registry.get_bool "matita.remove_old_objects" then
+   remove_objects ~baseuri; (* FG: we remove the old objects before putting the new ones*)
+
    List.iter
     (function 
      | `Obj (uri,obj) ->
@@ -251,16 +274,14 @@ module Serializer(D: sig type dumpable_s val get: dumpable_s -> dumpable_status
     ) !storage;
    set_global_aliases (!local_aliases @ get_global_aliases ());
    List.iter (fun u -> add_deps u [baseuri]) status#dump.includes;
-   time_travel (new status)
+   time_travel0 time0
 
   let require2 ~baseuri ~alias_only status =
    try
-    let s = D.get status in
     let status =
-     D.set status
-      (s#set_dump
-        {s#dump with
-        includes = baseuri::List.filter ((<>) baseuri) s#dump.includes}) in
+     status#set_dump
+      {status#dump with
+        includes = baseuri::List.filter ((<>) baseuri) status#dump.includes} in
     let _dependencies,dump = require0 ~baseuri in
      List.fold_right (!require1 ~alias_only) dump status
    with
@@ -272,40 +293,34 @@ module Serializer(D: sig type dumpable_s val get: dumpable_s -> dumpable_status
   let record_include =
    let aux (baseuri,fname) ~refresh_uri_in_universe:_ ~refresh_uri_in_term:_
    ~refresh_uri_in_reference:_ ~alias_only status =
-     let alias_only =
-      alias_only || List.mem baseuri (get_transitively_included (D.get status))
-     in
-      HLog.warn ("include " ^ (if alias_only then "alias " else "") ^ fname);
+     let baseuri = refresh_uri baseuri in
+     if not alias_only && List.mem baseuri (get_transitively_included status) then status
+     else
+      (HLog.warn ("include " ^ (if alias_only then "alias " else "") ^ fname);
       let status = require2 ~baseuri ~alias_only status in
       HLog.warn ("done: include " ^ (if alias_only then "alias " else "") ^ fname);
-      status
+      status)
    in
     register#run "include" aux
 
   let require ~baseuri ~fname ~alias_only status =
-   let alias_only =
-    alias_only || List.mem baseuri (get_transitively_included (D.get status)) in
-   let status =
-    if not alias_only then
-     let s = D.get status in
-      D.set status
-       (s#set_dump
-         {s#dump with
-           includes = baseuri::s#dump.includes;
-           dependencies = fname::s#dump.dependencies})
-    else
-     status in
-   let status = require2 ~baseuri ~alias_only status in
-   let s = D.get status in
-    D.set status
-     (s#set_dump
-       {s#dump with
-         objs = record_include (baseuri,fname)::s#dump.objs })
+   if not alias_only && List.mem baseuri (get_transitively_included status) then status
+   else
+    (let status =
+     if not alias_only then
+      status#set_dump
+       {status#dump with dependencies = fname::status#dump.dependencies}
+     else
+      status in
+    let status = require2 ~baseuri ~alias_only status in
+      status#set_dump
+       {status#dump with
+         objs = record_include (baseuri,fname)::status#dump.objs })
 end
 
-let fetch_obj uri =
+let fetch_obj status uri =
  let obj = require0 ~baseuri:uri in
-  refresh_uri_in_obj obj
+  refresh_uri_in_obj status obj
 ;;
 
 let resolve name =
@@ -323,10 +338,10 @@ let aliases_of uri =
      if NUri.eq uri' uri then Some nref else None) !local_aliases
 ;;
 
-let add_obj status ((u,_,_,_,_) as obj) =
- NCicEnvironment.check_and_add_obj obj;
- storage := (`Obj (u,obj))::!storage;
-  let _,height,_,_,obj = obj in
+let add_obj status ((u,_,_,_,_) as orig_obj) =
+ NCicEnvironment.check_and_add_obj status orig_obj;
+ storage := (`Obj (u,orig_obj))::!storage;
+  let _,height,_,_,obj = orig_obj in
   let references =
    match obj with
       NCic.Constant (_,name,None,_,_) ->
@@ -358,7 +373,7 @@ let add_obj status ((u,_,_,_,_) as obj) =
   status#set_timestamp (!storage,!local_aliases)
 ;;
 
-let add_constraint status u1 u2 = 
+let add_constraint status ~acyclic u1 u2 = 
   if
    List.exists
     (function `Constr (u1',u2') when u1=u1' && u2=u2' -> true | _ -> false)
@@ -366,22 +381,21 @@ let add_constraint status u1 u2 =
   then
    (*CSC: raise an exception here! *)
    (prerr_endline "CANNOT ADD A CONSTRAINT TWICE"; assert false);
-  NCicEnvironment.add_lt_constraint u1 u2;
+  NCicEnvironment.add_lt_constraint ~acyclic u1 u2;
   storage := (`Constr (u1,u2)) :: !storage;
   status#set_timestamp (!storage,!local_aliases)
 ;;
 
-let get_obj u =
+let get_obj status u =
  try 
   List.assq u 
    (HExtlib.filter_map 
     (function `Obj (u,o) -> Some (u,o) | _ -> None )
     !storage)
  with Not_found ->
-  try fetch_obj u
+  try fetch_obj status u
   with Sys_error _ ->
    raise (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri u)))
 ;;
 
-NCicEnvironment.set_get_obj get_obj;;
-NCicPp.set_get_obj get_obj;;
+NCicEnvironment.set_get_obj get_obj