]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaSync.ml
Much simpler (and slightly more performant) implementation of the UriManager.
[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 (** given a uri and a type list (the contructors types) builds a list of pairs
31  *  (name,uri) that is used to generate authomatic aliases **)
32 let extract_alias types uri = 
33   fst(List.fold_left (
34     fun (acc,i) (name, _, _, cl) -> 
35       (name, UriManager.string_of_uri (UriManager.uri_of_uriref uri i None))
36       ::
37       (fst(List.fold_left (
38         fun (acc,j) (name,_) ->
39           (((name,UriManager.string_of_uri (UriManager.uri_of_uriref uri i
40           (Some j))) :: acc) , j+1)
41         ) (acc,1) cl)),i+1
42   ) ([],0) types)
43
44 let env_of_list l env =
45  let l' = List.map (fun (name,suri) -> name,suri,CicUtil.term_of_uri (UriManager.uri_of_string suri)) l in
46   DisambiguateTypes.env_of_list l' env
47
48 let add_aliases_for_inductive_def status types suri = 
49   let uri = UriManager.uri_of_string suri in
50   let aliases = env_of_list (extract_alias types uri) status.aliases in
51   {status with aliases = aliases }
52
53 let add_alias_for_constant status suri =
54  let uri = UriManager.uri_of_string suri in
55  let name = UriManager.name_of_uri uri in
56  let new_env = env_of_list [(name,suri)] status.aliases in
57  {status with aliases = new_env }
58
59 let add_aliases_for_object status suri =
60  function
61     Cic.InductiveDefinition (types,_,_,_) ->
62      add_aliases_for_inductive_def status types suri
63   | Cic.Constant _ -> add_alias_for_constant status suri
64   | Cic.Variable _
65   | Cic.CurrentProof _ -> assert false
66   
67 let paths_and_uris_of_obj uri status =
68   let basedir = get_string_option status "basedir" in
69   let innertypesuri = UriManager.innertypesuri_of_uri uri in
70   let bodyuri = UriManager.bodyuri_of_uri uri in
71   let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
72         (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in
73   let innertypespath = basedir ^ "/" ^ innertypesfilename in
74   let xmlfilename = Str.replace_first (Str.regexp "^cic:/") ""
75         (UriManager.string_of_uri uri) ^ ".xml.gz" in
76   let xmlpath = basedir ^ "/" ^ xmlfilename in
77   let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") ""
78         (UriManager.string_of_uri uri) ^ ".body.xml.gz" in
79   let xmlbodypath = basedir ^ "/" ^  xmlbodyfilename in
80   xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri
81
82 let save_object_to_disk status uri obj =
83   let ensure_path_exists path =
84     let dir = Filename.dirname path in
85     MatitaMisc.mkdir dir
86   in
87   (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
88   let annobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
89     Cic2acic.acic_object_of_cic_object ~eta_fix:false obj
90   in 
91   (* prepare XML *)
92   let xml, bodyxml =
93    Cic2Xml.print_object uri ~ids_to_inner_sorts ~ask_dtd_to_the_getter:false
94     annobj 
95   in
96   let xmlinnertypes =
97    Cic2Xml.print_inner_types uri ~ids_to_inner_sorts ~ids_to_inner_types
98     ~ask_dtd_to_the_getter:false
99   in
100   let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri = 
101     paths_and_uris_of_obj uri status 
102   in
103   let path_scheme_of path = "file://" ^ path in
104   List.iter MatitaMisc.mkdir
105     (List.map Filename.dirname [innertypespath; xmlpath]);
106   (* now write to disk *)
107   ensure_path_exists innertypespath;
108   Xml.pp ~gzip:true xmlinnertypes (Some innertypespath) ;
109   ensure_path_exists xmlpath;
110   Xml.pp ~gzip:true xml (Some xmlpath) ;
111   (* now register to the getter *)
112   Http_getter.register' innertypesuri (path_scheme_of innertypespath); 
113   Http_getter.register' uri (path_scheme_of xmlpath);
114   (* we return a list of uri,path we registered/created *)
115   (uri,xmlpath) :: (innertypesuri,innertypespath) ::
116     (* now the optional body, both write and register *)
117     (match bodyxml,bodyuri with
118        None,None -> []
119      | Some bodyxml,Some bodyuri->
120          ensure_path_exists xmlbodypath;
121          Xml.pp ~gzip:true bodyxml (Some xmlbodypath) ;
122          Http_getter.register' bodyuri (path_scheme_of xmlbodypath);
123          [bodyuri,xmlbodypath]
124      | _-> assert false) 
125
126 let remove_object_from_disk uri path =
127   Sys.remove path;
128   Http_getter.unregister' uri
129
130 let add_obj uri obj status =
131   let dbd = MatitaDb.instance () in
132   let suri = UriManager.string_of_uri uri in
133   if CicEnvironment.in_library uri then
134     command_error (sprintf "%s already defined" suri)
135   else begin
136     CicTypeChecker.typecheck_obj uri obj;
137     MetadataDb.index_obj ~dbd ~uri; (* must be in the env *)
138     let new_stuff = save_object_to_disk status uri obj in  
139     MatitaLog.message (sprintf "%s defined" suri);
140     let status = add_aliases_for_object status suri obj in
141     { status with objects = new_stuff @ status.objects;
142                   proof_status = No_proof }
143   end
144    
145 module OrderedUri =
146 struct
147   type t = UriManager.uri * string
148   let compare (u1, _) (u2, _) = UriManager.compare u1 u2
149 end
150
151 module UriSet = Set.Make (OrderedUri)
152
153 (* returns the uri of objects that were added in the meanwhile...
154  * status1 ----> status2 
155  * assumption: objects defined in status2 are a superset of those defined in
156  * status1
157  *)
158 let delta_status status1 status2 =
159   let module S = UriSet in
160   let diff l1 l2 =
161     let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
162     let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
163     let diff = S.diff s2 s1 in
164     S.fold (fun uri uris -> uri :: uris) diff []
165   in
166   diff status1.objects status2.objects
167
168 let remove_coercion uri = 
169   CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq u uri)
170   
171 let time_travel ~present ~past =
172   let list_of_objs_to_remove = List.rev (delta_status past present) in
173     (* List.rev is just for the debugging code, which otherwise may list both
174     * something.ind and something.ind#xpointer ... (ask Enrico :-) *)
175   let debug_list = ref [] in
176   List.iter (fun (uri,p) -> 
177     remove_object_from_disk uri p;
178     remove_coercion uri;
179     (try 
180       CicEnvironment.remove_obj uri
181     with CicEnvironment.Object_not_found _ -> 
182       MatitaLog.debug
183         (sprintf "time_travel removes from cache %s that is not in" 
184           (UriManager.string_of_uri uri)));
185     let l = MatitaDb.remove_uri uri in
186     debug_list := UriManager.string_of_uri uri :: !debug_list @ l) 
187   list_of_objs_to_remove;
188   
189   (* this is debug code
190   * idea: debug_list is the list of objects to be removed as computed from the
191   * db, while list_of_objs_to_remove is the same list but computer from the
192   * differences between statuses *)
193   let l1 = List.sort Pervasives.compare !debug_list in
194   let l2 = List.sort Pervasives.compare 
195     (List.map (fun (x,_) -> UriManager.string_of_uri x) list_of_objs_to_remove)
196   in
197   let rec uniq = function 
198     | [] -> []
199     | h::[] -> [h]
200     | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl)
201     | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl
202   in
203   let l1 =  uniq l1 in
204   let l2 =  uniq l2 in
205   try
206     List.iter2 (fun x y -> if x <> y then raise Exit) l1 l2
207   with
208   | Invalid_argument _ | Exit -> 
209       MatitaLog.debug "It seems you garbaged something...";
210       MatitaLog.debug "l1:";
211       List.iter MatitaLog.debug l1;
212       MatitaLog.debug "l2:";
213       List.iter MatitaLog.debug l2
214     
215 let alias_diff ~from status = 
216   let module Map = DisambiguateTypes.Environment in
217   Map.fold
218     (fun domain_item codomain_item acc ->
219        if not (Map.mem domain_item from.aliases) then
220          Map.add domain_item codomain_item acc
221        else
222          acc)
223     status.aliases Map.empty
224