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