]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaSync.ml
The new_aliases argument of the functions alias_diff and set_proof_aliases
[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          (domain_item,codomain_item)::acc
38         else
39           acc
40       with
41        Not_found ->
42          (domain_item,codomain_item)::acc)
43     status.aliases []
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 new_aliases =
50  let aliases =
51   List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
52    status.aliases new_aliases in
53  let multi_aliases =
54   List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.cons d c acc)
55    status.multi_aliases new_aliases in
56  let new_status =
57    { status with multi_aliases = multi_aliases ; aliases = aliases}
58  in
59  if new_aliases = [] then
60    new_status
61  else
62    add_moo_content
63     (DisambiguatePp.commands_of_domain_and_codomain_items_list new_aliases)
64     new_status
65
66 let compute_diff_and_set_proof_aliases status aliases =
67  let new_status = { status with aliases = aliases } in
68  let diff = alias_diff ~from:status new_status in
69   set_proof_aliases status diff
70   
71 (** given a uri and a type list (the contructors types) builds a list of pairs
72  *  (name,uri) that is used to generate authomatic aliases **)
73 let extract_alias types uri = 
74   fst(List.fold_left (
75     fun (acc,i) (name, _, _, cl) -> 
76       (name, UriManager.string_of_uri (UriManager.uri_of_uriref uri i None))
77       ::
78       (fst(List.fold_left (
79         fun (acc,j) (name,_) ->
80           (((name,UriManager.string_of_uri (UriManager.uri_of_uriref uri i
81           (Some j))) :: acc) , j+1)
82         ) (acc,1) cl)),i+1
83   ) ([],0) types)
84
85 let build_aliases =
86  List.map
87   (fun (name,suri) ->
88     DisambiguateTypes.Id name,
89      (suri, fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string suri)))
90
91 let add_aliases_for_inductive_def status types suri = 
92   let uri = UriManager.uri_of_string suri in
93   let aliases = build_aliases (extract_alias types uri) in
94    set_proof_aliases status aliases
95
96 let add_alias_for_constant status suri =
97  let uri = UriManager.uri_of_string suri in
98  let name = UriManager.name_of_uri uri in
99  let new_env = build_aliases [(name,suri)] in
100  set_proof_aliases status new_env
101
102 let add_aliases_for_object status suri =
103  function
104     Cic.InductiveDefinition (types,_,_,_) ->
105      add_aliases_for_inductive_def status types suri
106   | Cic.Constant _ -> add_alias_for_constant status suri
107   | Cic.Variable _
108   | Cic.CurrentProof _ -> assert false
109   
110 let paths_and_uris_of_obj uri status =
111   let basedir = get_string_option status "basedir" ^ "/xml" in
112   let innertypesuri = UriManager.innertypesuri_of_uri uri in
113   let bodyuri = UriManager.bodyuri_of_uri uri in
114   let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
115         (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in
116   let innertypespath = basedir ^ "/" ^ innertypesfilename in
117   let xmlfilename = Str.replace_first (Str.regexp "^cic:/") ""
118         (UriManager.string_of_uri uri) ^ ".xml.gz" in
119   let xmlpath = basedir ^ "/" ^ xmlfilename in
120   let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") ""
121         (UriManager.string_of_uri uri) ^ ".body.xml.gz" in
122   let xmlbodypath = basedir ^ "/" ^  xmlbodyfilename in
123   xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri
124
125 let save_object_to_disk status uri obj =
126   let ensure_path_exists path =
127     let dir = Filename.dirname path in
128     MatitaMisc.mkdir dir
129   in
130   (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
131   let annobj = Cic2acic.plain_acic_object_of_cic_object obj in 
132   (* prepare XML *)
133   let xml, bodyxml =
134    Cic2Xml.print_object
135     uri ?ids_to_inner_sorts:None ~ask_dtd_to_the_getter:false annobj 
136   in
137   let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri = 
138     paths_and_uris_of_obj uri status 
139   in
140   let path_scheme_of path = "file://" ^ path in
141   List.iter MatitaMisc.mkdir
142     (List.map Filename.dirname [innertypespath; xmlpath]);
143   (* now write to disk *)
144   ensure_path_exists xmlpath;
145   Xml.pp ~gzip:true xml (Some xmlpath) ;
146   (* we return a list of uri,path we registered/created *)
147   (uri,xmlpath) ::
148     (* now the optional body, both write and register *)
149     (match bodyxml,bodyuri with
150        None,None -> []
151      | Some bodyxml,Some bodyuri->
152          ensure_path_exists xmlbodypath;
153          Xml.pp ~gzip:true bodyxml (Some xmlbodypath);
154          [bodyuri, xmlbodypath]
155      | _-> assert false) 
156
157 let typecheck_obj =
158  let profiler = CicUtil.profile "add_obj.typecheck_obj" in
159   fun uri obj -> profiler.CicUtil.profile (CicTypeChecker.typecheck_obj uri) obj
160
161 let index_obj =
162  let profiler = CicUtil.profile "add_obj.index_obj" in
163   fun ~dbd ~uri ->
164    profiler.CicUtil.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
165
166 let save_object_to_disk =
167  let profiler = CicUtil.profile "add_obj.save_object_to_disk" in
168   fun status uri obj ->
169    profiler.CicUtil.profile (save_object_to_disk status uri) obj
170
171 let add_obj uri obj status =
172   let dbd = MatitaDb.instance () in
173   let suri = UriManager.string_of_uri uri in
174   if CicEnvironment.in_library uri then
175     command_error (sprintf "%s already defined" suri)
176   else begin
177     typecheck_obj uri obj; (* 1 *)
178     try 
179       index_obj ~dbd ~uri; (* 2 must be in the env *)
180       try
181         let new_stuff = save_object_to_disk status uri obj in (* 3 *)
182         try 
183           MatitaLog.message (sprintf "%s defined" suri);
184           let status = add_aliases_for_object status suri obj in
185           { status with objects = new_stuff @ status.objects;
186                         proof_status = No_proof }
187         with exc ->
188           List.iter MatitaMisc.safe_remove (List.map snd new_stuff); (* -3 *)
189           raise exc
190       with exc ->
191         ignore(MatitaDb.remove_uri uri); (* -2 *)
192         raise exc
193     with exc ->
194       CicEnvironment.remove_obj uri; (* -1 *)
195       raise exc
196   end
197
198 let add_obj =
199  let profiler = CicUtil.profile "add_obj" in
200   fun uri obj status -> profiler.CicUtil.profile (add_obj uri obj) status
201    
202 module OrderedUri =
203 struct
204   type t = UriManager.uri * string
205   let compare (u1, _) (u2, _) = UriManager.compare u1 u2
206 end
207
208 module OrderedId = 
209 struct
210   type t = CicNotation.notation_id
211   let compare = Pervasives.compare
212 end
213
214 module UriSet = Set.Make (OrderedUri)
215 module IdSet  = Set.Make (OrderedId)
216
217   (** @return l2 \ l1 *)
218 let uri_list_diff l2 l1 =
219   let module S = UriSet in
220   let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
221   let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
222   let diff = S.diff s2 s1 in
223   S.fold (fun uri uris -> uri :: uris) diff []
224
225   (** @return l2 \ l1 *)
226 let id_list_diff l2 l1 =
227   let module S = IdSet in
228   let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
229   let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
230   let diff = S.diff s2 s1 in
231   S.fold (fun uri uris -> uri :: uris) diff []
232
233 let remove_coercion uri = 
234   CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq u uri)
235   
236 let time_travel ~present ~past =
237   let objs_to_remove = uri_list_diff present.objects past.objects in
238   let notation_to_remove =
239     id_list_diff present.notation_ids past.notation_ids
240   in
241   let debug_list = ref [] in
242   List.iter
243     (fun (uri,p) -> 
244       MatitaMisc.safe_remove p;
245       remove_coercion uri;
246       (try 
247         CicEnvironment.remove_obj uri
248       with CicEnvironment.Object_not_found _ -> 
249         MatitaLog.debug
250           (sprintf "time_travel removes from cache %s that is not in" 
251             (UriManager.string_of_uri uri)));
252       let l = MatitaDb.remove_uri uri in
253       debug_list := UriManager.string_of_uri uri :: !debug_list @ l) 
254     objs_to_remove;
255   List.iter CicNotation.remove_notation notation_to_remove
256   (*
257   (* this is debug code
258   * idea: debug_list is the list of objects to be removed as computed from the
259   * db, while list_of_objs_to_remove is the same list but computer from the
260   * differences between statuses *)
261   let l1 = List.sort Pervasives.compare !debug_list in
262   let l2 = List.sort Pervasives.compare 
263     (List.map (fun (x,_) -> UriManager.string_of_uri x) list_of_objs_to_remove)
264   in
265   let rec uniq = function 
266     | [] -> []
267     | h::[] -> [h]
268     | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl)
269     | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl
270   in
271   let l1 =  uniq l1 in
272   let l2 =  uniq l2 in
273   try
274     List.iter2 (fun x y -> if x <> y then raise Exit) l1 l2
275   with
276   | Invalid_argument _ | Exit -> 
277       MatitaLog.debug "It seems you garbaged something...";
278       MatitaLog.debug "l1:";
279       List.iter MatitaLog.debug l1;
280       MatitaLog.debug "l2:";
281       List.iter MatitaLog.debug l2
282       *)
283     
284 let remove ~verbose uri =
285   let derived_uris_of_uri uri =
286     UriManager.innertypesuri_of_uri uri ::
287     (match UriManager.bodyuri_of_uri uri with
288     | None -> []
289     | Some u -> [u])
290   in
291   let to_remove =
292     uri :: 
293     (if UriManager.uri_is_ind uri then MatitaDb.xpointers_of_ind uri else []) @
294     derived_uris_of_uri uri   
295   in   
296   List.iter 
297     (fun uri -> 
298       (try
299         if verbose then
300          MatitaLog.debug ("Removing: " ^ UriManager.string_of_uri uri);
301         MatitaMisc.safe_remove (Http_getter.resolve' uri)
302       with Http_getter_types.Key_not_found _ -> ());
303       remove_coercion uri; 
304       ignore (MatitaDb.remove_uri uri);
305       CicEnvironment.remove_obj uri)
306   to_remove