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