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 =
63 if not (Helm_registry.get_opt_default
64 Helm_registry.bool "matita.nodisk" ~default:false)
68 let ensure_path_exists path =
69 let dir = Filename.dirname path in
72 (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
73 let annobj = Cic2acic.plain_acic_object_of_cic_object obj in
77 uri ?ids_to_inner_sorts:None ~ask_dtd_to_the_getter:false annobj
79 let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
80 xmlunivgraphpath, univgraphuri =
81 paths_and_uris_of_obj uri
83 write (List.iter HExtlib.mkdir) (List.map Filename.dirname [xmlpath]);
84 (* now write to disk *)
85 write ensure_path_exists xmlpath;
86 write (Xml.pp ~gzip:true xml) (Some xmlpath);
87 write (CicUniv.write_xml_of_ugraph xmlunivgraphpath ugraph) univlist;
88 (* we return a list of uri,path we registered/created *)
90 (univgraphuri,xmlunivgraphpath) ::
91 (* now the optional body, both write and register *)
92 (match bodyxml,bodyuri with
94 | Some bodyxml,Some bodyuri->
95 write ensure_path_exists xmlbodypath;
96 write (Xml.pp ~gzip:true bodyxml) (Some xmlbodypath);
97 [bodyuri, xmlbodypath]
102 let profiler = HExtlib.profile "add_obj.typecheck_obj" in
103 fun uri obj -> profiler.HExtlib.profile (CicTypeChecker.typecheck_obj uri) obj
106 let profiler = HExtlib.profile "add_obj.index_obj" in
108 profiler.HExtlib.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
110 let add_single_obj uri obj refinement_toolkit =
111 let module RT = RefinementTool in
113 if (*List.mem `Generated (CicUtil.attributes_of_obj obj) &&*)
114 not (CoercGraph.is_a_coercion (Cic.Const (uri, [])))
116 refinement_toolkit.RT.pack_coercion_obj obj
120 let dbd = LibraryDb.instance () in
121 if CicEnvironment.in_library uri then
122 raise (AlreadyDefined uri)
124 (*CicUniv.reset_spent_time ();
125 let before = Unix.gettimeofday () in*)
126 typecheck_obj uri obj; (* 1 *)
127 (*let after = Unix.gettimeofday () in
128 let univ_time = CicUniv.get_spent_time () in
129 let total_time = after -. before in
131 (Printf.sprintf "QED: %%univ = %2.5f, total = %2.5f, univ = %2.5f, %s\n"
132 (univ_time *. 100. /. total_time) (total_time) (univ_time)
133 (UriManager.name_of_uri uri));*)
134 let _, ugraph, univlist =
135 CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph uri in
137 index_obj ~dbd ~uri; (* 2 must be in the env *)
140 let new_stuff = save_object_to_disk uri obj ugraph univlist in
143 (Printf.sprintf "%s defined" (UriManager.string_of_uri uri))
145 List.iter HExtlib.safe_remove (List.map snd new_stuff); (* -3 *)
148 ignore(LibraryDb.remove_uri uri); (* -2 *)
151 CicEnvironment.remove_obj uri; (* -1 *)
155 let remove_single_obj uri =
156 let derived_uris_of_uri uri =
157 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
158 innertypesuri::univgraphuri::(match bodyuri with None -> [] | Some u -> [u])
161 if UriManager.uri_is_ind uri then LibraryDb.xpointers_of_ind uri else [uri]
163 let files_to_remove = uri :: derived_uris_of_uri uri in
167 let file = Http_getter.resolve' ~writable:true uri in
168 HExtlib.safe_remove file;
169 HExtlib.rmdir_descend (Filename.dirname file)
170 with Http_getter_types.Key_not_found _ -> ());
174 ignore (LibraryDb.remove_uri uri);
175 (*CoercGraph.remove_coercion uri;*)
177 CicEnvironment.remove_obj uri
179 (*** GENERATION OF AUXILIARY LEMMAS ***)
181 let generate_elimination_principles uri refinement_toolkit =
186 let uri,obj = CicElim.elim_of ~sort uri i in
187 add_single_obj uri obj refinement_toolkit;
189 with CicElim.Can_t_eliminate -> ()
193 elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
195 List.iter remove_single_obj !uris;
198 let (obj, univ) = (CicEnvironment.get_obj CicUniv.empty_ugraph uri) in
200 | Cic.InductiveDefinition (indTypes, _, _, _) ->
201 let counter = ref 0 in
202 List.iter (fun _ -> elim !counter; counter := !counter+1) indTypes;
205 failwith (Printf.sprintf "not an inductive definition (%s)"
206 (UriManager.string_of_uri uri))
208 (* COERCIONS ***********************************************************)
210 let remove_all_coercions () =
211 UriManager.UriHashtbl.clear coercion_hashtbl;
212 CoercDb.remove_coercion (fun (_,_,u1) -> true)
214 let add_coercion ~add_composites refinement_toolkit uri arity =
216 let coer = CicUtil.term_of_uri uri in
217 CicTypeChecker.type_of_aux' [] [] coer CicUniv.empty_ugraph
219 (* we have to get the source and the tgt type uri
220 * in Coq syntax we have already their names, but
221 * since we don't support Funclass and similar I think
222 * all the coercion should be of the form
224 * So we should be able to extract them from the coercion type
226 * Currently only (_:T1)T2 is supported.
227 * should we saturate it with metas in case we insert it?
231 let rec aux = function
232 | Cic.Prod( _, src, tgt) -> src::aux tgt
237 let src_carr, tgt_carr =
238 let list_remove_from_tail n l =
239 let rec aux n = function
240 | hd::tl when n > 0 -> aux (n-1) tl
246 let types = spline2list coer_ty in
247 match arity, list_remove_from_tail arity types with
249 (* if ~delta is true, it is impossible to define an identity coercion *)
250 CoercDb.coerc_carr_of_term (CicReduction.whd ~delta:false [] src),
251 CoercDb.coerc_carr_of_term (CicReduction.whd ~delta:false [] tgt)
253 CoercDb.coerc_carr_of_term (CicReduction.whd ~delta:false [] src),
262 UriManager.eq u uri &&
263 CoercDb.eq_carr s src_carr &&
264 CoercDb.eq_carr t tgt_carr)
268 if not add_composites then
269 (CoercDb.add_coercion (src_carr, tgt_carr, uri);[])
272 CicCoercion.close_coercion_graph refinement_toolkit src_carr tgt_carr uri
274 let composite_uris = List.map (fun (_,_,uri,_) -> uri) new_coercions in
276 (* this if starts here just to be sure the closure function works fine *)
278 assert (new_coercions = []);
280 (UriManager.string_of_uri uri ^
281 " is already declared as a coercion! skipping...");
288 (fun (src,tgt,uri,_) -> CoercDb.add_coercion (src,tgt,uri))
290 CoercDb.add_coercion (src_carr, tgt_carr, uri);
291 (* add the composites obj and they eventual lemmas *)
293 if add_composites then
295 (fun acc (_,_,uri,obj) ->
296 add_single_obj uri obj refinement_toolkit;
302 (* store that composite_uris are related to uri. the first component is
303 * the stuff in the DB while the second is stuff for remove_obj *)
305 prerr_endline ("adding: " ^
306 string_of_bool add_composites ^ UriManager.string_of_uri uri);
307 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
310 UriManager.UriHashtbl.add coercion_hashtbl uri
311 (composite_uris,if add_composites then composite_uris else []);
313 prerr_endline ("lemmas:");
314 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
316 prerr_endline ("lemmas END");*)
321 let remove_coercion uri =
323 let (composites_in_db, composites_in_lib) =
324 UriManager.UriHashtbl.find coercion_hashtbl uri
326 (*prerr_endline ("removing: " ^UriManager.string_of_uri uri);
327 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
329 UriManager.UriHashtbl.remove coercion_hashtbl uri;
330 CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq uri u);
331 (* remove from the DB *)
333 (fun u -> CoercDb.remove_coercion (fun (_,_,u1) -> UriManager.eq u u1))
335 (* remove composites from the lib *)
336 List.iter remove_single_obj composites_in_lib
338 Not_found -> () (* mhh..... *)
341 let generate_projections refinement_toolkit uri fields =
344 CicRecord.projections_of uri
345 (List.map (fun (x,_,_) -> x) fields)
349 (fun (uri, name, bo) (_name, coercion, arity) ->
352 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
353 let attrs = [`Class `Projection; `Generated] in
354 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
355 add_single_obj uri obj refinement_toolkit;
359 (*prerr_endline ("composite for " ^ UriManager.string_of_uri uri);*)
361 add_coercion ~add_composites:true refinement_toolkit uri arity
363 (*prerr_endline ("are: ");
364 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u)) x;
372 uris := uri :: composites @ !uris
374 CicTypeChecker.TypeCheckerFailure s ->
376 ("Unable to create projection " ^ name ^ " cause: " ^ Lazy.force s);
377 | CicEnvironment.Object_not_found uri ->
378 let depend = UriManager.name_of_uri uri in
380 ("Unable to create projection " ^ name ^ " because it requires " ^
382 ) projections fields;
385 List.iter remove_single_obj !uris;
388 let build_inversion_principle = ref (fun a b -> assert false);;
390 let generate_inversion refinement_toolkit uri obj =
392 (fun (ind_uri,ind_obj) ->
393 add_single_obj ind_uri ind_obj refinement_toolkit;ind_uri)
394 (!build_inversion_principle uri obj)
396 let add_obj refinement_toolkit uri obj =
397 add_single_obj uri obj refinement_toolkit;
402 | Cic.Constant _ -> ()
403 | Cic.InductiveDefinition (_,_,_,attrs) ->
405 generate_elimination_principles uri refinement_toolkit;
406 uris := !uris @ generate_inversion refinement_toolkit uri obj;
407 let rec get_record_attrs =
410 | (`Class (`Record fields))::_ -> Some fields
411 | _::tl -> get_record_attrs tl
413 (match get_record_attrs attrs with
414 | None -> () (* not a record *)
417 (generate_projections refinement_toolkit uri fields))
419 | Cic.Variable _ -> assert false
421 UriManager.UriHashtbl.add auxiliary_lemmas_hashtbl uri !uris;
424 List.iter remove_single_obj !uris;
430 let res = UriManager.UriHashtbl.find auxiliary_lemmas_hashtbl uri in
431 UriManager.UriHashtbl.remove auxiliary_lemmas_hashtbl uri;
434 Not_found -> [] (*assert false*)
436 List.iter remove_single_obj (uri::uris)