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 =
47 let resolved = Http_getter.filename' ~writable:true uri in
48 let basepath = Filename.dirname resolved in
49 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
50 let innertypesfilename=(UriManager.nameext_of_uri innertypesuri)^".xml.gz"in
51 let innertypespath = basepath ^ "/" ^ innertypesfilename in
52 let xmlfilename = (UriManager.nameext_of_uri uri) ^ ".xml.gz" in
53 let xmlpath = basepath ^ "/" ^ xmlfilename in
54 let xmlbodyfilename = (UriManager.nameext_of_uri uri) ^ ".body.xml.gz" in
55 let xmlbodypath = basepath ^ "/" ^ xmlbodyfilename in
56 let xmlunivgraphfilename=(UriManager.nameext_of_uri univgraphuri)^".xml.gz"in
57 let xmlunivgraphpath = basepath ^ "/" ^ xmlunivgraphfilename in
58 xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
59 xmlunivgraphpath, univgraphuri
61 let save_object_to_disk uri obj ugraph univlist =
62 let ensure_path_exists path =
63 let dir = Filename.dirname path in
66 (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
67 let annobj = Cic2acic.plain_acic_object_of_cic_object obj in
71 uri ?ids_to_inner_sorts:None ~ask_dtd_to_the_getter:false annobj
73 let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
74 xmlunivgraphpath, univgraphuri =
75 paths_and_uris_of_obj uri
77 List.iter HExtlib.mkdir (List.map Filename.dirname [xmlpath]);
78 (* now write to disk *)
79 ensure_path_exists xmlpath;
80 Xml.pp ~gzip:true xml (Some xmlpath);
81 CicUniv.write_xml_of_ugraph xmlunivgraphpath ugraph univlist;
82 (* we return a list of uri,path we registered/created *)
84 (univgraphuri,xmlunivgraphpath) ::
85 (* now the optional body, both write and register *)
86 (match bodyxml,bodyuri with
88 | Some bodyxml,Some bodyuri->
89 ensure_path_exists xmlbodypath;
90 Xml.pp ~gzip:true bodyxml (Some xmlbodypath);
91 [bodyuri, xmlbodypath]
96 let profiler = HExtlib.profile "add_obj.typecheck_obj" in
97 fun uri obj -> profiler.HExtlib.profile (CicTypeChecker.typecheck_obj uri) obj
100 let profiler = HExtlib.profile "add_obj.index_obj" in
102 profiler.HExtlib.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
104 let add_single_obj uri obj refinement_toolkit =
105 let module RT = RefinementTool in
107 if (*List.mem `Generated (CicUtil.attributes_of_obj obj) &&*)
108 not (CoercGraph.is_a_coercion (Cic.Const (uri, [])))
110 refinement_toolkit.RT.pack_coercion_obj obj
114 let dbd = LibraryDb.instance () in
115 if CicEnvironment.in_library uri then
116 raise (AlreadyDefined uri)
118 (*CicUniv.reset_spent_time ();
119 let before = Unix.gettimeofday () in*)
120 typecheck_obj uri obj; (* 1 *)
121 (*let after = Unix.gettimeofday () in
122 let univ_time = CicUniv.get_spent_time () in
123 let total_time = after -. before in
125 (Printf.sprintf "QED: %%univ = %2.5f, total = %2.5f, univ = %2.5f, %s\n"
126 (univ_time *. 100. /. total_time) (total_time) (univ_time)
127 (UriManager.name_of_uri uri));*)
128 let _, ugraph, univlist =
129 CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph uri in
131 index_obj ~dbd ~uri; (* 2 must be in the env *)
134 let new_stuff = save_object_to_disk uri obj ugraph univlist in
137 (Printf.sprintf "%s defined" (UriManager.string_of_uri uri))
139 List.iter HExtlib.safe_remove (List.map snd new_stuff); (* -3 *)
142 ignore(LibraryDb.remove_uri uri); (* -2 *)
145 CicEnvironment.remove_obj uri; (* -1 *)
149 let remove_single_obj uri =
150 let derived_uris_of_uri uri =
151 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
152 innertypesuri::univgraphuri::(match bodyuri with None -> [] | Some u -> [u])
156 (if UriManager.uri_is_ind uri then LibraryDb.xpointers_of_ind uri else []) @
157 derived_uris_of_uri uri
162 let file = Http_getter.resolve' ~writable:true uri in
163 HExtlib.safe_remove file;
164 HExtlib.rmdir_descend (Filename.dirname file)
165 with Http_getter_types.Key_not_found _ -> ());
166 ignore (LibraryDb.remove_uri uri);
167 (*CoercGraph.remove_coercion uri;*)
168 CicEnvironment.remove_obj uri)
171 (*** GENERATION OF AUXILIARY LEMMAS ***)
173 let generate_elimination_principles uri refinement_toolkit =
177 let uri,obj = CicElim.elim_of ~sort uri 0 in
178 add_single_obj uri obj refinement_toolkit;
180 with CicElim.Can_t_eliminate -> ()
183 List.iter elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
186 List.iter remove_single_obj !uris;
189 (* COERCIONS ***********************************************************)
191 let remove_all_coercions () =
192 UriManager.UriHashtbl.clear coercion_hashtbl;
193 CoercDb.remove_coercion (fun (_,_,u1) -> true)
195 let add_coercion ~add_composites refinement_toolkit uri =
197 let coer = CicUtil.term_of_uri uri in
198 CicTypeChecker.type_of_aux' [] [] coer CicUniv.empty_ugraph
200 (* we have to get the source and the tgt type uri
201 * in Coq syntax we have already their names, but
202 * since we don't support Funclass and similar I think
203 * all the coercion should be of the form
205 * So we should be able to extract them from the coercion type
207 * Currently only (_:T1)T2 is supported.
208 * should we saturate it with metas in case we insert it?
211 let extract_last_two_p ty =
212 let rec aux = function
213 | Cic.Prod( _, _, ((Cic.Prod _) as t)) ->
215 | Cic.Prod( _, src, tgt) -> src, tgt
222 (fun (_,_,ul) -> List.exists (fun u -> UriManager.eq u uri) ul)
225 let ty_src, ty_tgt = extract_last_two_p coer_ty in
226 let src_carr = CoercDb.coerc_carr_of_term (CicReduction.whd [] ty_src) in
227 let tgt_carr = CoercDb.coerc_carr_of_term (CicReduction.whd [] ty_tgt) in
228 if not add_composites then
229 (CoercDb.add_coercion (src_carr, tgt_carr, uri);[])
232 CicCoercion.close_coercion_graph refinement_toolkit src_carr tgt_carr uri
234 let composite_uris = List.map (fun (_,_,uri,_) -> uri) new_coercions in
236 (* this if starts here just to be sure the closure function works fine *)
238 assert (new_coercions = []);
240 (UriManager.string_of_uri uri ^
241 " is already declared as a coercion! skipping...");
248 (fun (src,tgt,uri,_) -> CoercDb.add_coercion (src,tgt,uri))
250 CoercDb.add_coercion (src_carr, tgt_carr, uri);
251 (* add the composites obj and they eventual lemmas *)
253 if add_composites then
255 (fun acc (_,_,uri,obj) ->
256 add_single_obj uri obj refinement_toolkit;
262 (* store that composite_uris are related to uri. the first component is
263 * the stuff in the DB while the second is stuff for remove_obj *)
265 prerr_endline ("adding: " ^
266 string_of_bool add_composites ^ UriManager.string_of_uri uri);
267 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
270 UriManager.UriHashtbl.add coercion_hashtbl uri
271 (composite_uris,if add_composites then composite_uris else []);
273 prerr_endline ("lemmas:");
274 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
276 prerr_endline ("lemmas END");*)
281 let remove_coercion uri =
283 let (composites_in_db, composites_in_lib) =
284 UriManager.UriHashtbl.find coercion_hashtbl uri
286 (*prerr_endline ("removing: " ^UriManager.string_of_uri uri);
287 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
289 UriManager.UriHashtbl.remove coercion_hashtbl uri;
290 CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq uri u);
291 (* remove from the DB *)
293 (fun u -> CoercDb.remove_coercion (fun (_,_,u1) -> UriManager.eq u u1))
295 (* remove composites from the lib *)
296 List.iter remove_single_obj composites_in_lib
298 Not_found -> () (* mhh..... *)
301 let generate_projections refinement_toolkit uri fields =
303 let projections = CicRecord.projections_of uri (List.map fst fields) in
306 (fun (uri, name, bo) (_name, coercion) ->
309 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
310 let attrs = [`Class `Projection; `Generated] in
311 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
312 add_single_obj uri obj refinement_toolkit;
316 prerr_endline ("composite for " ^ UriManager.string_of_uri uri);
317 let x = add_coercion ~add_composites:true refinement_toolkit uri
319 prerr_endline ("are: ");
320 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u)) x;
327 uris := uri :: composites @ !uris
329 CicTypeChecker.TypeCheckerFailure s ->
331 ("Unable to create projection " ^ name ^ " cause: " ^ Lazy.force s);
332 | CicEnvironment.Object_not_found uri ->
333 let depend = UriManager.name_of_uri uri in
335 ("Unable to create projection " ^ name ^ " because it requires " ^
337 ) projections fields;
340 List.iter remove_single_obj !uris;
343 let build_inversion_principle = ref (fun a b -> assert false);;
345 let generate_inversion refinement_toolkit uri obj =
346 match !build_inversion_principle uri obj with
348 | Some (ind_uri,ind_obj) ->
349 add_single_obj ind_uri ind_obj refinement_toolkit;
352 let add_obj refinement_toolkit uri obj =
353 add_single_obj uri obj refinement_toolkit;
358 | Cic.Constant _ -> ()
359 | Cic.InductiveDefinition (_,_,_,attrs) ->
361 generate_elimination_principles uri refinement_toolkit;
362 uris := !uris @ generate_inversion refinement_toolkit uri obj;
363 let rec get_record_attrs =
366 | (`Class (`Record fields))::_ -> Some fields
367 | _::tl -> get_record_attrs tl
369 (match get_record_attrs attrs with
370 | None -> () (* not a record *)
373 (generate_projections refinement_toolkit uri fields))
375 | Cic.Variable _ -> assert false
377 UriManager.UriHashtbl.add auxiliary_lemmas_hashtbl uri !uris;
380 List.iter remove_single_obj !uris;
386 let res = UriManager.UriHashtbl.find auxiliary_lemmas_hashtbl uri in
387 UriManager.UriHashtbl.remove auxiliary_lemmas_hashtbl uri;
390 Not_found -> [] (*assert false*)
392 List.iter remove_single_obj (uri::uris)