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