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 (description1,_ as codomain_item) acc ->
35 let description2,_ = Map.find domain_item from.aliases in
36 if description1 <> description2 then
37 (domain_item,codomain_item)::acc
42 (domain_item,codomain_item)::acc)
46 let profiler = HExtlib.profile "alias_diff (conteggiato anche in include)" in
47 fun ~from status -> profiler.HExtlib.profile (alias_diff ~from) status
49 let set_proof_aliases status new_aliases =
51 List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
52 status.aliases new_aliases in
54 List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.cons d c acc)
55 status.multi_aliases new_aliases in
57 { status with multi_aliases = multi_aliases ; aliases = aliases}
59 if new_aliases = [] then
63 (DisambiguatePp.commands_of_domain_and_codomain_items_list new_aliases)
66 (** given a uri and a type list (the contructors types) builds a list of pairs
67 * (name,uri) that is used to generate automatic aliases **)
68 let extract_alias types uri =
70 fun (acc,i) (name, _, _, cl) ->
71 (name, UriManager.string_of_uri (UriManager.uri_of_uriref uri i None))
74 fun (acc,j) (name,_) ->
75 (((name,UriManager.string_of_uri (UriManager.uri_of_uriref uri i
76 (Some j))) :: acc) , j+1)
83 DisambiguateTypes.Id name,
84 (suri, fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string suri)))
86 let add_aliases_for_inductive_def status types suri =
87 let uri = UriManager.uri_of_string suri in
88 let aliases = build_aliases (extract_alias types uri) in
89 set_proof_aliases status aliases
91 let add_alias_for_constant status suri =
92 let uri = UriManager.uri_of_string suri in
93 let name = UriManager.name_of_uri uri in
94 let new_env = build_aliases [(name,suri)] in
95 set_proof_aliases status new_env
97 let add_aliases_for_object status suri =
99 Cic.InductiveDefinition (types,_,_,_) ->
100 add_aliases_for_inductive_def status types suri
101 | Cic.Constant _ -> add_alias_for_constant status suri
103 | Cic.CurrentProof _ -> assert false
105 let paths_and_uris_of_obj uri status =
106 let basedir = get_string_option status "basedir" ^ "/xml" in
107 let innertypesuri = UriManager.innertypesuri_of_uri uri in
108 let bodyuri = UriManager.bodyuri_of_uri uri in
109 let univgraphuri = UriManager.univgraphuri_of_uri uri in
110 let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
111 (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in
112 let innertypespath = basedir ^ "/" ^ innertypesfilename in
113 let xmlfilename = Str.replace_first (Str.regexp "^cic:/") ""
114 (UriManager.string_of_uri uri) ^ ".xml.gz" in
115 let xmlpath = basedir ^ "/" ^ xmlfilename in
116 let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") ""
117 (UriManager.string_of_uri uri) ^ ".body.xml.gz" in
118 let xmlbodypath = basedir ^ "/" ^ xmlbodyfilename in
119 let xmlunivgraphfilename = Str.replace_first (Str.regexp "^cic:/") ""
120 (UriManager.string_of_uri univgraphuri) ^ ".xml.gz" in
121 let xmlunivgraphpath = basedir ^ "/" ^ xmlunivgraphfilename in
122 xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
123 xmlunivgraphpath, univgraphuri
125 let save_object_to_disk status uri obj ugraph univlist =
126 let ensure_path_exists path =
127 let dir = Filename.dirname path in
130 (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
131 let annobj = Cic2acic.plain_acic_object_of_cic_object obj in
135 uri ?ids_to_inner_sorts:None ~ask_dtd_to_the_getter:false annobj
137 let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
138 xmlunivgraphpath, univgraphuri =
139 paths_and_uris_of_obj uri status
141 let path_scheme_of path = "file://" ^ path in
142 List.iter HExtlib.mkdir (List.map Filename.dirname [xmlpath]);
143 (* now write to disk *)
144 ensure_path_exists xmlpath;
145 Xml.pp ~gzip:true xml (Some xmlpath);
146 CicUniv.write_xml_of_ugraph xmlunivgraphpath ugraph univlist;
147 (* we return a list of uri,path we registered/created *)
149 (univgraphuri,xmlunivgraphpath) ::
150 (* now the optional body, both write and register *)
151 (match bodyxml,bodyuri with
153 | Some bodyxml,Some bodyuri->
154 ensure_path_exists xmlbodypath;
155 Xml.pp ~gzip:true bodyxml (Some xmlbodypath);
156 [bodyuri, xmlbodypath]
160 let profiler = HExtlib.profile "add_obj.typecheck_obj" in
161 fun uri obj -> profiler.HExtlib.profile (CicTypeChecker.typecheck_obj uri) obj
164 let profiler = HExtlib.profile "add_obj.index_obj" in
166 profiler.HExtlib.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
168 let add_obj uri obj status =
169 let dbd = MatitaDb.instance () in
170 let suri = UriManager.string_of_uri uri in
171 if CicEnvironment.in_library uri then
172 command_error (sprintf "%s already defined" suri)
174 typecheck_obj uri obj; (* 1 *)
175 let _, ugraph, univlist =
176 CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph uri in
178 index_obj ~dbd ~uri; (* 2 must be in the env *)
180 let new_stuff=save_object_to_disk status uri obj ugraph univlist in(*3*)
182 MatitaLog.message (sprintf "%s defined" suri);
183 let status = add_aliases_for_object status suri obj in
184 { status with objects = new_stuff @ status.objects;
185 proof_status = No_proof }
187 List.iter MatitaMisc.safe_remove (List.map snd new_stuff); (* -3 *)
190 ignore(MatitaDb.remove_uri uri); (* -2 *)
193 CicEnvironment.remove_obj uri; (* -1 *)
198 let profiler = HExtlib.profile "add_obj" in
199 fun uri obj status -> profiler.HExtlib.profile (add_obj uri obj) status
203 type t = UriManager.uri * string
204 let compare (u1, _) (u2, _) = UriManager.compare u1 u2
209 type t = CicNotation.notation_id
210 let compare = Pervasives.compare
213 module UriSet = Set.Make (OrderedUri)
214 module IdSet = Set.Make (OrderedId)
216 (** @return l2 \ l1 *)
217 let uri_list_diff l2 l1 =
218 let module S = UriSet in
219 let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
220 let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
221 let diff = S.diff s2 s1 in
222 S.fold (fun uri uris -> uri :: uris) diff []
224 (** @return l2 \ l1 *)
225 let id_list_diff l2 l1 =
226 let module S = IdSet in
227 let s1 = List.fold_left (fun set uri -> S.add uri set) S.empty l1 in
228 let s2 = List.fold_left (fun set uri -> S.add uri set) S.empty l2 in
229 let diff = S.diff s2 s1 in
230 S.fold (fun uri uris -> uri :: uris) diff []
232 let remove_coercion uri =
233 CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq u uri)
235 let time_travel ~present ~past =
236 let objs_to_remove = uri_list_diff present.objects past.objects in
237 let notation_to_remove =
238 id_list_diff present.notation_ids past.notation_ids
240 let debug_list = ref [] in
243 MatitaMisc.safe_remove p;
246 CicEnvironment.remove_obj uri
247 with CicEnvironment.Object_not_found _ ->
249 (sprintf "time_travel removes from cache %s that is not in"
250 (UriManager.string_of_uri uri)));
251 let l = MatitaDb.remove_uri uri in
252 debug_list := UriManager.string_of_uri uri :: !debug_list @ l)
254 List.iter CicNotation.remove_notation notation_to_remove
256 (* this is debug code
257 * idea: debug_list is the list of objects to be removed as computed from the
258 * db, while list_of_objs_to_remove is the same list but computer from the
259 * differences between statuses *)
260 let l1 = List.sort Pervasives.compare !debug_list in
261 let l2 = List.sort Pervasives.compare
262 (List.map (fun (x,_) -> UriManager.string_of_uri x) list_of_objs_to_remove)
264 let rec uniq = function
267 | h1::h2::tl when h1 = h2 -> uniq (h2 :: tl)
268 | h1::tl (* when h1 <> h2 *) -> h1 :: uniq tl
273 List.iter2 (fun x y -> if x <> y then raise Exit) l1 l2
275 | Invalid_argument _ | Exit ->
276 MatitaLog.debug "It seems you garbaged something...";
277 MatitaLog.debug "l1:";
278 List.iter MatitaLog.debug l1;
279 MatitaLog.debug "l2:";
280 List.iter MatitaLog.debug l2
283 let last_baseuri = ref ""
285 let remove ?(verbose=false) uri =
286 let derived_uris_of_uri uri =
287 UriManager.innertypesuri_of_uri uri ::
288 UriManager.univgraphuri_of_uri uri ::
289 (match UriManager.bodyuri_of_uri uri with
295 (if UriManager.uri_is_ind uri then MatitaDb.xpointers_of_ind uri else []) @
296 derived_uris_of_uri uri
301 (* WARNING: non reentrant debugging code *)
303 let baseuri = UriManager.buri_of_uri uri in
304 if !last_baseuri <> baseuri then
306 MatitaLog.debug ("Removing: " ^ baseuri ^ "/*");
307 last_baseuri := baseuri
309 let file = Http_getter.resolve' uri in
310 MatitaMisc.safe_remove file;
311 MatitaMisc.rmdir_descend (Filename.dirname file)
312 with Http_getter_types.Key_not_found _ -> ());
314 ignore (MatitaDb.remove_uri uri);
315 CicEnvironment.remove_obj uri)