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