1 (* Copyright (C) 2004-2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 let alias_diff ~from status =
31 let module Map = DisambiguateTypes.Environment in
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
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
47 status.aliases Map.empty
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
53 CicTextualParser2.EnvironmentP3.to_string diff ::
54 status.moo_content_rev in
55 {new_status with moo_content_rev = moo_content_rev}
57 (** given a uri and a type list (the contructors types) builds a list of pairs
58 * (name,uri) that is used to generate authomatic aliases **)
59 let extract_alias types uri =
61 fun (acc,i) (name, _, _, cl) ->
62 (name, UriManager.string_of_uri (UriManager.uri_of_uriref uri i None))
65 fun (acc,j) (name,_) ->
66 (((name,UriManager.string_of_uri (UriManager.uri_of_uriref uri i
67 (Some j))) :: acc) , j+1)
71 let env_of_list l env =
72 let l' = List.map (fun (name,suri) -> name,suri,CicUtil.term_of_uri (UriManager.uri_of_string suri)) l in
73 DisambiguateTypes.env_of_list l' env
75 let add_aliases_for_inductive_def status types suri =
76 let uri = UriManager.uri_of_string suri in
77 let aliases = env_of_list (extract_alias types uri) status.aliases in
78 set_proof_aliases status aliases
80 let add_alias_for_constant status suri =
81 let uri = UriManager.uri_of_string suri in
82 let name = UriManager.name_of_uri uri in
83 let new_env = env_of_list [(name,suri)] status.aliases in
84 set_proof_aliases status new_env
86 let add_aliases_for_object status suri =
88 Cic.InductiveDefinition (types,_,_,_) ->
89 add_aliases_for_inductive_def status types suri
90 | Cic.Constant _ -> add_alias_for_constant status suri
92 | Cic.CurrentProof _ -> assert false
94 let paths_and_uris_of_obj uri status =
95 let basedir = get_string_option status "basedir" in
96 let innertypesuri = UriManager.innertypesuri_of_uri uri in
97 let bodyuri = UriManager.bodyuri_of_uri uri in
98 let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
99 (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in
100 let innertypespath = basedir ^ "/" ^ innertypesfilename in
101 let xmlfilename = Str.replace_first (Str.regexp "^cic:/") ""
102 (UriManager.string_of_uri uri) ^ ".xml.gz" in
103 let xmlpath = basedir ^ "/" ^ xmlfilename in
104 let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") ""
105 (UriManager.string_of_uri uri) ^ ".body.xml.gz" in
106 let xmlbodypath = basedir ^ "/" ^ xmlbodyfilename in
107 xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri
109 let save_object_to_disk status uri obj =
110 let ensure_path_exists path =
111 let dir = Filename.dirname path in
114 (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
115 let annobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
116 Cic2acic.acic_object_of_cic_object ~eta_fix:false obj
120 Cic2Xml.print_object uri ~ids_to_inner_sorts ~ask_dtd_to_the_getter:false
124 Cic2Xml.print_inner_types uri ~ids_to_inner_sorts ~ids_to_inner_types
125 ~ask_dtd_to_the_getter:false
127 let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri =
128 paths_and_uris_of_obj uri status
130 let path_scheme_of path = "file://" ^ path in
131 List.iter MatitaMisc.mkdir
132 (List.map Filename.dirname [innertypespath; xmlpath]);
133 (* now write to disk *)
134 ensure_path_exists innertypespath;
135 Xml.pp ~gzip:true xmlinnertypes (Some innertypespath);
136 ensure_path_exists xmlpath;
137 Xml.pp ~gzip:true xml (Some xmlpath) ;
138 (* we return a list of uri,path we registered/created *)
139 (uri,xmlpath) :: (innertypesuri,innertypespath) ::
140 (* now the optional body, both write and register *)
141 (match bodyxml,bodyuri with
143 | Some bodyxml,Some bodyuri->
144 ensure_path_exists xmlbodypath;
145 Xml.pp ~gzip:true bodyxml (Some xmlbodypath);
146 [bodyuri, xmlbodypath]
149 let add_obj uri obj status =
150 let dbd = MatitaDb.instance () in
151 let suri = UriManager.string_of_uri uri in
152 if CicEnvironment.in_library uri then
153 command_error (sprintf "%s already defined" suri)
155 CicTypeChecker.typecheck_obj uri obj;
156 MetadataDb.index_obj ~dbd ~uri; (* must be in the env *)
157 let new_stuff = save_object_to_disk status uri obj in
158 MatitaLog.message (sprintf "%s defined" suri);
159 let status = add_aliases_for_object status suri obj in
160 { status with objects = new_stuff @ status.objects;
161 proof_status = No_proof }
166 type t = UriManager.uri * string
167 let compare (u1, _) (u2, _) = UriManager.compare u1 u2
170 module UriSet = Set.Make (OrderedUri)
172 (* returns the uri of objects that were added in the meanwhile...
173 * status1 ----> status2
174 * assumption: objects defined in status2 are a superset of those defined in
177 let delta_status status1 status2 =
178 let module S = UriSet in
180 let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
181 let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
182 let diff = S.diff s2 s1 in
183 S.fold (fun uri uris -> uri :: uris) diff []
185 diff status1.objects status2.objects
187 let remove_coercion uri =
188 CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq u uri)
190 let time_travel ~present ~past =
191 let list_of_objs_to_remove = List.rev (delta_status past present) in
192 (* List.rev is just for the debugging code, which otherwise may list both
193 * something.ind and something.ind#xpointer ... (ask Enrico :-) *)
194 let debug_list = ref [] in
195 List.iter (fun (uri,p) ->
196 MatitaMisc.safe_remove p;
199 CicEnvironment.remove_obj uri
200 with CicEnvironment.Object_not_found _ ->
202 (sprintf "time_travel removes from cache %s that is not in"
203 (UriManager.string_of_uri uri)));
204 let l = MatitaDb.remove_uri uri in
205 debug_list := UriManager.string_of_uri uri :: !debug_list @ l)
206 list_of_objs_to_remove;
208 (* this is debug code
209 * idea: debug_list is the list of objects to be removed as computed from the
210 * db, while list_of_objs_to_remove is the same list but computer from the
211 * differences between statuses *)
212 let l1 = List.sort Pervasives.compare !debug_list in
213 let l2 = List.sort Pervasives.compare
214 (List.map (fun (x,_) -> UriManager.string_of_uri x) list_of_objs_to_remove)
216 let rec uniq = function
219 | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl)
220 | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl
225 List.iter2 (fun x y -> if x <> y then raise Exit) l1 l2
227 | Invalid_argument _ | Exit ->
228 MatitaLog.debug "It seems you garbaged something...";
229 MatitaLog.debug "l1:";
230 List.iter MatitaLog.debug l1;
231 MatitaLog.debug "l2:";
232 List.iter MatitaLog.debug l2
234 let remove ~verbose uri =
235 let derived_uris_of_uri uri =
236 UriManager.innertypesuri_of_uri uri ::
237 (match UriManager.bodyuri_of_uri uri with
243 (if UriManager.uri_is_ind uri then MatitaDb.xpointers_of_ind uri else []) @
244 derived_uris_of_uri uri
250 MatitaLog.debug ("Removing: " ^ UriManager.string_of_uri uri);
251 MatitaMisc.safe_remove (Http_getter.resolve' uri)
252 with Http_getter_types.Key_not_found _ -> ());
254 ignore (MatitaDb.remove_uri uri))