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