]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicLibrary.ml
NG decompilation is now activated. However, it is called from the old
[helm.git] / helm / software / components / ng_kernel / nCicLibrary.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 (* $Id$ *)
13
14 exception LibraryOutOfSync of string Lazy.t
15
16 type timestamp =
17  (NUri.uri * NCic.obj) list *
18  (NUri.uri * string * NReference.reference) list *
19  NCic.obj NUri.UriMap.t;;
20
21 let time0 = [],[],NUri.UriMap.empty;;
22 let storage = ref [];;
23 let aliases = ref [];;
24 let cache = ref NUri.UriMap.empty;;
25
26 class status =
27  object
28   val timestamp = (time0 : timestamp)
29   method timestamp = timestamp
30   method set_timestamp v = {< timestamp = v >}
31   method set_library_status
32    : 'status. < timestamp : timestamp; .. > as 'status -> 'self
33    = fun o -> {< timestamp = o#timestamp >}
34  end
35
36 let time_travel status =
37  let sto,ali,cac = status#timestamp in
38   storage := sto; aliases := ali; cache := cac
39 ;;
40
41 let path_of_baseuri ?(no_suffix=false) baseuri =
42  let uri = NUri.string_of_uri baseuri in
43  let path = String.sub uri 4 (String.length uri - 4) in
44  let path = Helm_registry.get "matita.basedir" ^ path in
45  let dirname = Filename.dirname path in
46   HExtlib.mkdir dirname;
47   if no_suffix then
48    path
49   else
50    path ^ ".ng"
51 ;;
52
53 let magic = 0;;
54
55 let require0 ~baseuri =
56  let ch = open_in (path_of_baseuri baseuri) in
57  let mmagic,dump = Marshal.from_channel ch in
58   close_in ch;
59   if mmagic <> magic then
60    raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
61   else
62    dump
63 ;;
64
65 let serialize ~baseuri dump =
66  let ch = open_out (path_of_baseuri baseuri) in
67  Marshal.to_channel ch (magic,dump) [Marshal.Closures];
68  close_out ch;
69  List.iter
70   (fun (uri,obj) ->
71     let ch = open_out (path_of_baseuri uri) in
72     Marshal.to_channel ch (magic,obj) [];
73     close_out ch
74   ) !storage;
75  time_travel (new status)
76 ;;
77
78 let refresh_uri uri = NUri.uri_of_string (NUri.string_of_uri uri);;
79
80 let rec refresh_uri_in_term =
81  function
82     NCic.Const (NReference.Ref (u,spec)) ->
83      NCic.Const (NReference.reference_of_spec (refresh_uri u) spec)
84   | t -> NCicUtils.map (fun _ _ -> ()) () (fun _ -> refresh_uri_in_term) t
85 ;;
86
87 let refresh_uri_in_obj (uri,height,metasenv,subst,obj_kind) =
88  assert (metasenv = []);
89  assert (subst = []);
90  uri,height,metasenv,subst,
91   NCicUntrusted.map_obj_kind refresh_uri_in_term obj_kind
92 ;;
93
94 module type Serializer =
95  sig
96   type status
97   type obj
98   val register:
99    string ->
100     ('a -> refresh_uri_in_term:(NCic.term -> NCic.term) -> status -> status) ->
101     ('a -> obj)
102   val serialize: baseuri:NUri.uri -> obj list -> unit
103   val require: baseuri:NUri.uri -> status -> status
104  end
105
106 module Serializer(S: sig type status end) =
107  struct
108   type status = S.status
109   type obj = string * Obj.t
110
111   let require1 = ref (fun _ -> assert false (* unknown data*))
112   let already_registered = ref []
113
114   let register tag require =
115    assert (not (List.mem tag !already_registered));
116    already_registered := tag :: !already_registered;
117    require1 :=
118     (fun (tag',data) as x ->
119      if tag=tag' then
120       require (Obj.magic data) ~refresh_uri_in_term
121      else
122       !require1 x);
123    (fun x -> tag,Obj.repr x)
124
125   let serialize = serialize
126
127   let require ~baseuri status =
128    let dump = require0 ~baseuri in
129     List.fold_right !require1 dump status
130 end
131
132 let decompile ~baseuri =
133  HExtlib.safe_remove (path_of_baseuri baseuri);
134  let basepath = path_of_baseuri ~no_suffix:true baseuri in
135  try
136   let od = Unix.opendir basepath in
137   let rec aux names =
138    try
139     let name = Unix.readdir od in
140      if name <> "." && name <> ".." then aux (name::names) else aux names
141    with
142     End_of_file -> names in
143   let names = List.map (fun name -> basepath ^ "/" ^ name) (aux []) in
144    Unix.closedir od;
145    List.iter Unix.unlink names;
146    HExtlib.rmdir_descend basepath
147  with
148   Unix.Unix_error _ -> () (* raised by Unix.opendir, we hope :-) *)
149 ;;
150
151 LibraryClean.set_decompile_cb
152  (fun ~baseuri -> decompile ~baseuri:(NUri.uri_of_string baseuri));;
153
154 let fetch_obj uri =
155  let obj = require0 ~baseuri:uri in
156   refresh_uri_in_obj obj
157 ;;
158
159 let resolve name =
160  try
161   HExtlib.filter_map
162    (fun (_,name',nref) -> if name'=name then Some nref else None) !aliases
163  with
164   Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy name))
165 ;;
166
167 let aliases_of uri =
168  try
169   HExtlib.filter_map
170    (fun (uri',_,nref) -> if NUri.eq uri' uri then Some nref else None) !aliases
171  with
172   Not_found -> raise (NCicEnvironment.ObjectNotFound (lazy (NUri.string_of_uri uri)))
173 ;;
174
175 let add_obj status u obj =
176  storage := (u,obj)::!storage;
177   let _,height,_,_,obj = obj in
178   let references =
179    match obj with
180       NCic.Constant (_,name,None,_,_) ->
181        [u,name,NReference.reference_of_spec u NReference.Decl]
182     | NCic.Constant (_,name,Some _,_,_) ->
183        [u,name,NReference.reference_of_spec u (NReference.Def height)]
184     | NCic.Fixpoint (is_ind,fl,_) ->
185        HExtlib.list_mapi
186         (fun (_,name,recno,_,_) i ->
187           if is_ind then
188            u,name,NReference.reference_of_spec u(NReference.Fix(i,recno,height))
189           else
190            u,name,NReference.reference_of_spec u (NReference.CoFix i)) fl
191     | NCic.Inductive (inductive,leftno,il,_) ->
192        List.flatten
193         (HExtlib.list_mapi
194          (fun (_,iname,_,cl) i ->
195            HExtlib.list_mapi
196             (fun (_,cname,_) j->
197               u,cname,
198                NReference.reference_of_spec u (NReference.Con (i,j+1,leftno))
199             ) cl @
200            [u,iname,
201              NReference.reference_of_spec u
202               (NReference.Ind (inductive,i,leftno))]
203          ) il)
204   in
205   aliases := references @ !aliases;
206   status#set_timestamp (!storage,!aliases,!cache)
207 ;;
208
209 let get_obj u =
210  try List.assq u !storage
211  with Not_found ->
212   try fetch_obj u
213   with Sys_error _ ->
214    try NUri.UriMap.find u !cache
215    with Not_found ->
216     let ouri = NCic2OCic.ouri_of_nuri u in
217     try
218       let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph ouri in
219       let l = OCic2NCic.convert_obj ouri o in
220       List.iter (fun (u,_,_,_,_ as o) -> cache:= NUri.UriMap.add u o !cache) l;
221       HExtlib.list_last l
222     with CicEnvironment.Object_not_found u -> 
223       raise (NCicEnvironment.ObjectNotFound 
224                (lazy (NUri.string_of_uri (OCic2NCic.nuri_of_ouri u))))
225 ;;
226
227 let clear_cache () = cache := NUri.UriMap.empty;;
228
229 NCicEnvironment.set_get_obj get_obj;;
230 NCicPp.set_get_obj get_obj;;