]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaSync.ml
More profiling code added.
[helm.git] / helm / matita / matitaSync.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 open MatitaTypes
29
30 let alias_diff ~from status = 
31   let module Map = DisambiguateTypes.Environment in
32   Map.fold
33     (fun domain_item (description1,_ as codomain_item) acc ->
34       try
35        let description2,_ = Map.find domain_item from.aliases in
36         if description1 <> description2 then
37           Map.add domain_item codomain_item acc
38         else
39           acc
40       with
41        Not_found ->
42          Map.add domain_item codomain_item acc)
43     status.aliases Map.empty
44
45 let alias_diff =
46  let profiler = CicUtil.profile "alias_diff (conteggiato anche in include)" in
47  fun ~from status -> profiler.CicUtil.profile (alias_diff ~from) status
48
49 let set_proof_aliases status aliases =
50  let new_status = { status with aliases = aliases } in
51  let diff = alias_diff ~from:status new_status in
52  let multi_aliases =
53   DisambiguateTypes.Environment.fold DisambiguateTypes.Environment.cons
54    diff status.multi_aliases in
55  let new_status = { new_status with multi_aliases = multi_aliases } in
56  if DisambiguateTypes.Environment.is_empty diff then
57    new_status
58  else
59    add_moo_content (DisambiguatePp.commands_of_environment diff) new_status
60   
61 (** given a uri and a type list (the contructors types) builds a list of pairs
62  *  (name,uri) that is used to generate authomatic aliases **)
63 let extract_alias types uri = 
64   fst(List.fold_left (
65     fun (acc,i) (name, _, _, cl) -> 
66       (name, UriManager.string_of_uri (UriManager.uri_of_uriref uri i None))
67       ::
68       (fst(List.fold_left (
69         fun (acc,j) (name,_) ->
70           (((name,UriManager.string_of_uri (UriManager.uri_of_uriref uri i
71           (Some j))) :: acc) , j+1)
72         ) (acc,1) cl)),i+1
73   ) ([],0) types)
74
75 let env_of_list l env =
76  let l' = List.map (fun (name,suri) -> name,suri,CicUtil.term_of_uri (UriManager.uri_of_string suri)) l in
77   DisambiguateTypes.env_of_list l' env
78
79 let add_aliases_for_inductive_def status types suri = 
80   let uri = UriManager.uri_of_string suri in
81   let aliases = env_of_list (extract_alias types uri) status.aliases in
82   set_proof_aliases status aliases
83
84 let add_alias_for_constant status suri =
85  let uri = UriManager.uri_of_string suri in
86  let name = UriManager.name_of_uri uri in
87  let new_env = env_of_list [(name,suri)] status.aliases in
88  set_proof_aliases status new_env
89
90 let add_aliases_for_object status suri =
91  function
92     Cic.InductiveDefinition (types,_,_,_) ->
93      add_aliases_for_inductive_def status types suri
94   | Cic.Constant _ -> add_alias_for_constant status suri
95   | Cic.Variable _
96   | Cic.CurrentProof _ -> assert false
97   
98 let paths_and_uris_of_obj uri status =
99   let basedir = get_string_option status "basedir" ^ "/xml" in
100   let innertypesuri = UriManager.innertypesuri_of_uri uri in
101   let bodyuri = UriManager.bodyuri_of_uri uri in
102   let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
103         (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in
104   let innertypespath = basedir ^ "/" ^ innertypesfilename in
105   let xmlfilename = Str.replace_first (Str.regexp "^cic:/") ""
106         (UriManager.string_of_uri uri) ^ ".xml.gz" in
107   let xmlpath = basedir ^ "/" ^ xmlfilename in
108   let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") ""
109         (UriManager.string_of_uri uri) ^ ".body.xml.gz" in
110   let xmlbodypath = basedir ^ "/" ^  xmlbodyfilename in
111   xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri
112
113 let acic_object_of_cic_object =
114  let profiler = CicUtil.profile "add_obj.save_object_to_disk.acic_object_of_cic_object" in
115   fun ~eta_fix obj ->
116    profiler.CicUtil.profile (Cic2acic.acic_object_of_cic_object ~eta_fix) obj
117
118 let save_object_to_disk status uri obj =
119   let ensure_path_exists path =
120     let dir = Filename.dirname path in
121     MatitaMisc.mkdir dir
122   in
123   (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
124   let annobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
125     acic_object_of_cic_object ~eta_fix:false obj
126   in 
127   (* prepare XML *)
128   let xml, bodyxml =
129    Cic2Xml.print_object uri ~ids_to_inner_sorts ~ask_dtd_to_the_getter:false
130     annobj 
131   in
132   let xmlinnertypes =
133    Cic2Xml.print_inner_types uri ~ids_to_inner_sorts ~ids_to_inner_types
134     ~ask_dtd_to_the_getter:false
135   in
136   let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri = 
137     paths_and_uris_of_obj uri status 
138   in
139   let path_scheme_of path = "file://" ^ path in
140   List.iter MatitaMisc.mkdir
141     (List.map Filename.dirname [innertypespath; xmlpath]);
142   (* now write to disk *)
143   ensure_path_exists innertypespath;
144   Xml.pp ~gzip:true xmlinnertypes (Some innertypespath);
145   ensure_path_exists xmlpath;
146   Xml.pp ~gzip:true xml (Some xmlpath) ;
147   (* we return a list of uri,path we registered/created *)
148   (uri,xmlpath) :: (innertypesuri,innertypespath) ::
149     (* now the optional body, both write and register *)
150     (match bodyxml,bodyuri with
151        None,None -> []
152      | Some bodyxml,Some bodyuri->
153          ensure_path_exists xmlbodypath;
154          Xml.pp ~gzip:true bodyxml (Some xmlbodypath);
155          [bodyuri, xmlbodypath]
156      | _-> assert false) 
157
158 let typecheck_obj =
159  let profiler = CicUtil.profile "add_obj.typecheck_obj" in
160   fun uri obj -> profiler.CicUtil.profile (CicTypeChecker.typecheck_obj uri) obj
161
162 let index_obj =
163  let profiler = CicUtil.profile "add_obj.index_obj" in
164   fun ~dbd ~uri ->
165    profiler.CicUtil.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
166
167 let save_object_to_disk =
168  let profiler = CicUtil.profile "add_obj.save_object_to_disk" in
169   fun status uri obj ->
170    profiler.CicUtil.profile (save_object_to_disk status uri) obj
171
172 let add_obj uri obj status =
173   let dbd = MatitaDb.instance () in
174   let suri = UriManager.string_of_uri uri in
175   if CicEnvironment.in_library uri then
176     command_error (sprintf "%s already defined" suri)
177   else begin
178     typecheck_obj uri obj; (* 1 *)
179     try 
180       index_obj ~dbd ~uri; (* 2 must be in the env *)
181       try
182         let new_stuff = save_object_to_disk status uri obj in (* 3 *)
183         try 
184           MatitaLog.message (sprintf "%s defined" suri);
185           let status = add_aliases_for_object status suri obj in
186           { status with objects = new_stuff @ status.objects;
187                         proof_status = No_proof }
188         with exc ->
189           List.iter MatitaMisc.safe_remove (List.map snd new_stuff); (* -3 *)
190           raise exc
191       with exc ->
192         ignore(MatitaDb.remove_uri uri); (* -2 *)
193         raise exc
194     with exc ->
195       CicEnvironment.remove_obj uri; (* -1 *)
196       raise exc
197   end
198
199 let add_obj =
200  let profiler = CicUtil.profile "add_obj" in
201   fun uri obj status -> profiler.CicUtil.profile (add_obj uri obj) status
202    
203 module OrderedUri =
204 struct
205   type t = UriManager.uri * string
206   let compare (u1, _) (u2, _) = UriManager.compare u1 u2
207 end
208
209 module OrderedId = 
210 struct
211   type t = CicNotation.notation_id
212   let compare = Pervasives.compare
213 end
214
215 module UriSet = Set.Make (OrderedUri)
216 module IdSet  = Set.Make (OrderedId)
217
218   (** @return l2 \ l1 *)
219 let uri_list_diff l2 l1 =
220   let module S = UriSet in
221   let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
222   let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
223   let diff = S.diff s2 s1 in
224   S.fold (fun uri uris -> uri :: uris) diff []
225
226   (** @return l2 \ l1 *)
227 let id_list_diff l2 l1 =
228   let module S = IdSet in
229   let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
230   let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
231   let diff = S.diff s2 s1 in
232   S.fold (fun uri uris -> uri :: uris) diff []
233
234 let remove_coercion uri = 
235   CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq u uri)
236   
237 let time_travel ~present ~past =
238   let objs_to_remove = uri_list_diff present.objects past.objects in
239   let notation_to_remove =
240     id_list_diff present.notation_ids past.notation_ids
241   in
242   let debug_list = ref [] in
243   List.iter
244     (fun (uri,p) -> 
245       MatitaMisc.safe_remove p;
246       remove_coercion uri;
247       (try 
248         CicEnvironment.remove_obj uri
249       with CicEnvironment.Object_not_found _ -> 
250         MatitaLog.debug
251           (sprintf "time_travel removes from cache %s that is not in" 
252             (UriManager.string_of_uri uri)));
253       let l = MatitaDb.remove_uri uri in
254       debug_list := UriManager.string_of_uri uri :: !debug_list @ l) 
255     objs_to_remove;
256   List.iter CicNotation.remove_notation notation_to_remove
257   (*
258   (* this is debug code
259   * idea: debug_list is the list of objects to be removed as computed from the
260   * db, while list_of_objs_to_remove is the same list but computer from the
261   * differences between statuses *)
262   let l1 = List.sort Pervasives.compare !debug_list in
263   let l2 = List.sort Pervasives.compare 
264     (List.map (fun (x,_) -> UriManager.string_of_uri x) list_of_objs_to_remove)
265   in
266   let rec uniq = function 
267     | [] -> []
268     | h::[] -> [h]
269     | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl)
270     | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl
271   in
272   let l1 =  uniq l1 in
273   let l2 =  uniq l2 in
274   try
275     List.iter2 (fun x y -> if x <> y then raise Exit) l1 l2
276   with
277   | Invalid_argument _ | Exit -> 
278       MatitaLog.debug "It seems you garbaged something...";
279       MatitaLog.debug "l1:";
280       List.iter MatitaLog.debug l1;
281       MatitaLog.debug "l2:";
282       List.iter MatitaLog.debug l2
283       *)
284     
285 let remove ~verbose uri =
286   let derived_uris_of_uri uri =
287     UriManager.innertypesuri_of_uri uri ::
288     (match UriManager.bodyuri_of_uri uri with
289     | None -> []
290     | Some u -> [u])
291   in
292   let to_remove =
293     uri :: 
294     (if UriManager.uri_is_ind uri then MatitaDb.xpointers_of_ind uri else []) @
295     derived_uris_of_uri uri   
296   in   
297   List.iter 
298     (fun uri -> 
299       (try
300         if verbose then
301          MatitaLog.debug ("Removing: " ^ UriManager.string_of_uri uri);
302         MatitaMisc.safe_remove (Http_getter.resolve' uri)
303       with Http_getter_types.Key_not_found _ -> ());
304       remove_coercion uri; 
305       ignore (MatitaDb.remove_uri uri);
306       CicEnvironment.remove_obj uri)
307   to_remove