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