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/
28 exception AlreadyDefined of UriManager.uri
30 let auxiliary_lemmas_hashtbl = UriManager.UriHashtbl.create 29
32 (* uri |--> (derived_coercions_in_the_coercion_DB, derived_coercions_in_lib)
34 * in case of remove_coercion uri, the first component is removed from the
35 * coercion DB, while the second is passed to remove_obj (and is not [] only if
36 * add_coercion is called with add_composites
38 let coercion_hashtbl = UriManager.UriHashtbl.create 3
41 let innertypesuri = UriManager.innertypesuri_of_uri uri in
42 let bodyuri = UriManager.bodyuri_of_uri uri in
43 let univgraphuri = UriManager.univgraphuri_of_uri uri in
44 innertypesuri,bodyuri,univgraphuri
46 let paths_and_uris_of_obj uri ~basedir =
47 let basedir = basedir ^ "/xml" in
48 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
49 let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
50 (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in
51 let innertypespath = basedir ^ "/" ^ innertypesfilename in
52 let xmlfilename = Str.replace_first (Str.regexp "^cic:/") ""
53 (UriManager.string_of_uri uri) ^ ".xml.gz" in
54 let xmlpath = basedir ^ "/" ^ xmlfilename in
55 let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") ""
56 (UriManager.string_of_uri uri) ^ ".body.xml.gz" in
57 let xmlbodypath = basedir ^ "/" ^ xmlbodyfilename in
58 let xmlunivgraphfilename = Str.replace_first (Str.regexp "^cic:/") ""
59 (UriManager.string_of_uri univgraphuri) ^ ".xml.gz" in
60 let xmlunivgraphpath = basedir ^ "/" ^ xmlunivgraphfilename in
61 xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
62 xmlunivgraphpath, univgraphuri
64 let save_object_to_disk ~basedir uri obj ugraph univlist =
65 let ensure_path_exists path =
66 let dir = Filename.dirname path in
69 (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
70 let annobj = Cic2acic.plain_acic_object_of_cic_object obj in
74 uri ?ids_to_inner_sorts:None ~ask_dtd_to_the_getter:false annobj
76 let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
77 xmlunivgraphpath, univgraphuri =
78 paths_and_uris_of_obj uri basedir
80 List.iter HExtlib.mkdir (List.map Filename.dirname [xmlpath]);
81 (* now write to disk *)
82 ensure_path_exists xmlpath;
83 Xml.pp ~gzip:true xml (Some xmlpath);
84 CicUniv.write_xml_of_ugraph xmlunivgraphpath ugraph univlist;
85 (* we return a list of uri,path we registered/created *)
87 (univgraphuri,xmlunivgraphpath) ::
88 (* now the optional body, both write and register *)
89 (match bodyxml,bodyuri with
91 | Some bodyxml,Some bodyuri->
92 ensure_path_exists xmlbodypath;
93 Xml.pp ~gzip:true bodyxml (Some xmlbodypath);
94 [bodyuri, xmlbodypath]
99 let profiler = HExtlib.profile "add_obj.typecheck_obj" in
100 fun uri obj -> profiler.HExtlib.profile (CicTypeChecker.typecheck_obj uri) obj
103 let profiler = HExtlib.profile "add_obj.index_obj" in
105 profiler.HExtlib.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
107 let add_single_obj uri obj refinement_toolkit ~basedir =
108 let module RT = RefinementTool in
110 if (*List.mem `Generated (CicUtil.attributes_of_obj obj) &&*)
111 not (CoercGraph.is_a_coercion (Cic.Const (uri, [])))
113 refinement_toolkit.RT.pack_coercion_obj obj
117 let dbd = LibraryDb.instance () in
118 if CicEnvironment.in_library uri then
119 raise (AlreadyDefined uri)
121 (*CicUniv.reset_spent_time ();
122 let before = Unix.gettimeofday () in*)
123 typecheck_obj uri obj; (* 1 *)
124 (*let after = Unix.gettimeofday () in
125 let univ_time = CicUniv.get_spent_time () in
126 let total_time = after -. before in
128 (Printf.sprintf "QED: %%univ = %2.5f, total = %2.5f, univ = %2.5f, %s\n"
129 (univ_time *. 100. /. total_time) (total_time) (univ_time)
130 (UriManager.name_of_uri uri));*)
131 let _, ugraph, univlist =
132 CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph uri in
134 index_obj ~dbd ~uri; (* 2 must be in the env *)
137 let new_stuff = save_object_to_disk ~basedir uri obj ugraph univlist in
140 (Printf.sprintf "%s defined" (UriManager.string_of_uri uri))
142 List.iter HExtlib.safe_remove (List.map snd new_stuff); (* -3 *)
145 ignore(LibraryDb.remove_uri uri); (* -2 *)
148 CicEnvironment.remove_obj uri; (* -1 *)
152 let remove_single_obj uri =
153 let derived_uris_of_uri uri =
154 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
155 innertypesuri::univgraphuri::(match bodyuri with None -> [] | Some u -> [u])
159 (if UriManager.uri_is_ind uri then LibraryDb.xpointers_of_ind uri else []) @
160 derived_uris_of_uri uri
165 let file = Http_getter.resolve' uri in
166 HExtlib.safe_remove file;
167 HExtlib.rmdir_descend (Filename.dirname file)
168 with Http_getter_types.Key_not_found _ -> ());
169 ignore (LibraryDb.remove_uri uri);
170 (*CoercGraph.remove_coercion uri;*)
171 CicEnvironment.remove_obj uri)
174 (*** GENERATION OF AUXILIARY LEMMAS ***)
176 let generate_elimination_principles ~basedir uri refinement_toolkit =
180 let uri,obj = CicElim.elim_of ~sort uri 0 in
181 add_single_obj uri obj refinement_toolkit ~basedir;
183 with CicElim.Can_t_eliminate -> ()
186 List.iter elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
189 List.iter remove_single_obj !uris;
192 (* COERCIONS ***********************************************************)
194 let remove_all_coercions () =
195 UriManager.UriHashtbl.clear coercion_hashtbl;
196 CoercDb.remove_coercion (fun (_,_,u1) -> true)
198 let add_coercion ~basedir ~add_composites refinement_toolkit uri =
200 let coer = CicUtil.term_of_uri uri in
201 CicTypeChecker.type_of_aux' [] [] coer CicUniv.empty_ugraph
203 (* we have to get the source and the tgt type uri
204 * in Coq syntax we have already their names, but
205 * since we don't support Funclass and similar I think
206 * all the coercion should be of the form
208 * So we should be able to extract them from the coercion type
210 * Currently only (_:T1)T2 is supported.
211 * should we saturate it with metas in case we insert it?
214 let extract_last_two_p ty =
215 let rec aux = function
216 | Cic.Prod( _, _, ((Cic.Prod _) as t)) ->
218 | Cic.Prod( _, src, tgt) -> src, tgt
223 let ty_src, ty_tgt = extract_last_two_p coer_ty in
224 let src_carr = CoercDb.coerc_carr_of_term (CicReduction.whd [] ty_src) in
225 let tgt_carr = CoercDb.coerc_carr_of_term (CicReduction.whd [] ty_tgt) in
227 CicCoercion.close_coercion_graph refinement_toolkit src_carr tgt_carr uri in
228 let composite_uris = List.map (fun (_,_,uri,_) -> uri) new_coercions in
231 (fun (src,tgt,uri,_) -> CoercDb.add_coercion (src,tgt,uri))
233 CoercDb.add_coercion (src_carr, tgt_carr, uri);
234 (* add the composites obj and they eventual lemmas *)
236 if add_composites then
238 (fun acc (_,_,uri,obj) ->
239 add_single_obj ~basedir uri obj refinement_toolkit;
241 composite_uris new_coercions
245 (* store that composite_uris are related to uri. the first component is the
246 * stuff in the DB while the second is stuff for remove_obj *)
248 prerr_endline ("adding: " ^
249 string_of_bool add_composites ^ UriManager.string_of_uri uri);
250 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
253 UriManager.UriHashtbl.add coercion_hashtbl uri
254 (composite_uris,if add_composites then composite_uris else []);
257 let remove_coercion uri =
259 let (composites_in_db, composites_in_lib) =
260 UriManager.UriHashtbl.find coercion_hashtbl uri
262 prerr_endline ("removing: " ^UriManager.string_of_uri uri);
263 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
265 UriManager.UriHashtbl.remove coercion_hashtbl uri;
266 CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq uri u);
267 (* remove from the DB *)
269 (fun u -> CoercDb.remove_coercion (fun (_,_,u1) -> UriManager.eq u u1))
271 (* remove composites from the lib *)
272 List.iter remove_single_obj composites_in_lib
274 Not_found -> () (* mhh..... *)
277 let generate_projections ~basedir refinement_toolkit uri fields =
279 let projections = CicRecord.projections_of uri (List.map fst fields) in
282 (fun (uri, name, bo) (_name, coercion) ->
285 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
286 let attrs = [`Class `Projection; `Generated] in
287 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
288 add_single_obj ~basedir uri obj refinement_toolkit;
291 add_coercion ~basedir ~add_composites:true refinement_toolkit uri
295 uris := uri :: composites @ !uris
297 CicTypeChecker.TypeCheckerFailure s ->
299 ("Unable to create projection " ^ name ^ " cause: " ^ Lazy.force s);
300 | CicEnvironment.Object_not_found uri ->
301 let depend = UriManager.name_of_uri uri in
303 ("Unable to create projection " ^ name ^ " because it requires " ^
305 ) projections fields;
308 List.iter remove_single_obj !uris;
312 let add_obj refinement_toolkit uri obj ~basedir =
313 add_single_obj uri obj refinement_toolkit ~basedir;
318 | Cic.Constant _ -> ()
319 | Cic.InductiveDefinition (_,_,_,attrs) ->
321 generate_elimination_principles ~basedir uri refinement_toolkit;
322 let rec get_record_attrs =
325 | (`Class (`Record fields))::_ -> Some fields
326 | _::tl -> get_record_attrs tl
328 (match get_record_attrs attrs with
329 | None -> () (* not a record *)
332 (generate_projections ~basedir refinement_toolkit uri fields))
334 | Cic.Variable _ -> assert false
336 UriManager.UriHashtbl.add auxiliary_lemmas_hashtbl uri !uris;
339 List.iter remove_single_obj !uris;
345 let res = UriManager.UriHashtbl.find auxiliary_lemmas_hashtbl uri in
346 UriManager.UriHashtbl.remove auxiliary_lemmas_hashtbl uri;
349 Not_found -> [] (*assert false*)
351 List.iter remove_single_obj (uri::uris)