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' ~local:true ~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, innertypes =
74 if Helm_registry.get_bool "matita.system" then
75 let annobj, _, _, ids_to_inner_sorts, ids_to_inner_types, _, _ =
76 Cic2acic.acic_object_of_cic_object obj
79 Cic2Xml.print_inner_types
80 uri ~ids_to_inner_sorts ~ids_to_inner_types ~ask_dtd_to_the_getter:false
82 annobj, Some innertypesxml
84 let annobj = Cic2acic.plain_acic_object_of_cic_object obj in
90 uri ?ids_to_inner_sorts:None ~ask_dtd_to_the_getter:false annobj
92 let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
93 xmlunivgraphpath, univgraphuri =
94 paths_and_uris_of_obj uri
96 write (List.iter HExtlib.mkdir) (List.map Filename.dirname [xmlpath]);
97 (* now write to disk *)
98 write ensure_path_exists xmlpath;
99 write (Xml.pp ~gzip:true xml) (Some xmlpath);
100 write (CicUniv.write_xml_of_ugraph xmlunivgraphpath ugraph) univlist;
101 (* we return a list of uri,path we registered/created *)
103 (univgraphuri,xmlunivgraphpath) ::
104 (* now the optional inner types, both write and register *)
105 (match innertypes with
107 | Some innertypesxml ->
108 write ensure_path_exists innertypespath;
109 write (Xml.pp ~gzip:true innertypesxml) (Some innertypespath);
110 [innertypesuri, innertypespath]
112 (* now the optional body, both write and register *)
113 (match bodyxml,bodyuri with
115 | Some bodyxml,Some bodyuri->
116 write ensure_path_exists xmlbodypath;
117 write (Xml.pp ~gzip:true bodyxml) (Some xmlbodypath);
118 [bodyuri, xmlbodypath]
123 let profiler = HExtlib.profile "add_obj.typecheck_obj" in
124 fun uri obj -> profiler.HExtlib.profile (CicTypeChecker.typecheck_obj uri) obj
127 let profiler = HExtlib.profile "add_obj.index_obj" in
129 profiler.HExtlib.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
131 let add_single_obj uri obj refinement_toolkit =
132 let module RT = RefinementTool in
134 if (*List.mem `Generated (CicUtil.attributes_of_obj obj) &&*)
135 not (CoercDb.is_a_coercion' (Cic.Const (uri, [])))
137 refinement_toolkit.RT.pack_coercion_obj obj
141 let dbd = LibraryDb.instance () in
142 if CicEnvironment.in_library uri then
143 raise (AlreadyDefined uri)
145 (*CicUniv.reset_spent_time ();
146 let before = Unix.gettimeofday () in*)
147 typecheck_obj uri obj; (* 1 *)
148 (*let after = Unix.gettimeofday () in
149 let univ_time = CicUniv.get_spent_time () in
150 let total_time = after -. before in
152 (Printf.sprintf "QED: %%univ = %2.5f, total = %2.5f, univ = %2.5f, %s\n"
153 (univ_time *. 100. /. total_time) (total_time) (univ_time)
154 (UriManager.name_of_uri uri));*)
155 let _, ugraph, univlist =
156 CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph uri in
158 index_obj ~dbd ~uri; (* 2 must be in the env *)
161 let new_stuff = save_object_to_disk uri obj ugraph univlist in
164 (Printf.sprintf "%s defined" (UriManager.string_of_uri uri))
166 List.iter HExtlib.safe_remove (List.map snd new_stuff); (* -3 *)
169 ignore(LibraryDb.remove_uri uri); (* -2 *)
172 CicEnvironment.remove_obj uri; (* -1 *)
176 let remove_single_obj uri =
177 let derived_uris_of_uri uri =
178 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
179 innertypesuri::univgraphuri::(match bodyuri with None -> [] | Some u -> [u])
182 if UriManager.uri_is_ind uri then LibraryDb.xpointers_of_ind uri else [uri]
184 let files_to_remove = uri :: derived_uris_of_uri uri in
188 let file = Http_getter.resolve' ~local:true ~writable:true uri in
189 HExtlib.safe_remove file;
190 HExtlib.rmdir_descend (Filename.dirname file)
191 with Http_getter_types.Key_not_found _ -> ());
195 ignore (LibraryDb.remove_uri uri);
196 (*CoercGraph.remove_coercion uri;*)
198 CicEnvironment.remove_obj uri
200 (*** GENERATION OF AUXILIARY LEMMAS ***)
202 let generate_elimination_principles uri refinement_toolkit =
207 let uri,obj = CicElim.elim_of ~sort uri i in
208 add_single_obj uri obj refinement_toolkit;
210 with CicElim.Can_t_eliminate -> ()
214 elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
216 List.iter remove_single_obj !uris;
219 let (obj, univ) = (CicEnvironment.get_obj CicUniv.empty_ugraph uri) in
221 | Cic.InductiveDefinition (indTypes, _, _, _) ->
222 let counter = ref 0 in
223 List.iter (fun _ -> elim !counter; counter := !counter+1) indTypes;
226 failwith (Printf.sprintf "not an inductive definition (%s)"
227 (UriManager.string_of_uri uri))
229 (* COERCIONS ***********************************************************)
231 let remove_all_coercions () =
232 UriManager.UriHashtbl.clear coercion_hashtbl;
233 CoercDb.remove_coercion (fun (_,_,u1) -> true)
235 let add_coercion ~add_composites refinement_toolkit uri arity baseuri =
237 let coer = CicUtil.term_of_uri uri in
238 CicTypeChecker.type_of_aux' [] [] coer CicUniv.empty_ugraph
240 (* we have to get the source and the tgt type uri
241 * in Coq syntax we have already their names, but
242 * since we don't support Funclass and similar I think
243 * all the coercion should be of the form
245 * So we should be able to extract them from the coercion type
247 * Currently only (_:T1)T2 is supported.
248 * should we saturate it with metas in case we insert it?
252 let rec aux = function
253 | Cic.Prod( _, src, tgt) -> src::aux tgt
258 let src_carr, tgt_carr =
259 let list_remove_from_tail n l =
260 let rec aux n = function
261 | hd::tl when n > 0 -> aux (n-1) tl
267 let types = spline2list coer_ty in
268 match arity, list_remove_from_tail arity types with
270 (* if ~delta is true, it is impossible to define an identity coercion *)
271 CoercDb.coerc_carr_of_term (CicReduction.whd ~delta:false [] src),
272 CoercDb.coerc_carr_of_term (CicReduction.whd ~delta:false [] tgt)
274 CoercDb.coerc_carr_of_term (CicReduction.whd ~delta:false [] src),
278 let already_in_obj src_carr tgt_carr uri obj =
285 | Cic.Constant (_, Some bo, _, _, _) -> bo
288 CoercDb.eq_carr s src_carr &&
289 CoercDb.eq_carr t tgt_carr &&
290 if fst (CicReduction.are_convertible [] (CicUtil.term_of_uri u) bo
291 CicUniv.oblivion_ugraph)
295 UriManager.string_of_uri u ^ " and " ^ UriManager.string_of_uri
296 uri^" are not convertible, but are between the same nodes.\n"^
297 "From now on nification can fail randomly.");
302 if not add_composites then
303 (CoercDb.add_coercion (src_carr, tgt_carr, uri);[])
306 CicCoercion.close_coercion_graph src_carr tgt_carr uri
310 List.filter (fun (s,t,u,obj) -> not(already_in_obj s t u obj))
313 let composite_uris = List.map (fun (_,_,uri,_) -> uri) new_coercions in
316 (fun (src,tgt,uri,_) -> CoercDb.add_coercion (src,tgt,uri))
318 CoercDb.add_coercion (src_carr, tgt_carr, uri);
319 (* add the composites obj and they eventual lemmas *)
321 if add_composites then
323 (fun acc (_,tgt,uri,obj) ->
324 add_single_obj uri obj refinement_toolkit;
325 let arity = match tgt with CoercDb.Fun n -> n | _ -> 0 in
331 (* store that composite_uris are related to uri. the first component is
332 * the stuff in the DB while the second is stuff for remove_obj *)
334 prerr_endline ("adding: " ^
335 string_of_bool add_composites ^ UriManager.string_of_uri uri);
336 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
339 UriManager.UriHashtbl.add coercion_hashtbl uri
340 (composite_uris,if add_composites then composite_uris else []);
342 prerr_endline ("lemmas:");
343 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
345 prerr_endline ("lemmas END");*)
349 let remove_coercion uri =
351 let (composites_in_db, composites_in_lib) =
352 UriManager.UriHashtbl.find coercion_hashtbl uri
354 (*prerr_endline ("removing: " ^UriManager.string_of_uri uri);
355 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
357 UriManager.UriHashtbl.remove coercion_hashtbl uri;
358 CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq uri u);
359 (* remove from the DB *)
361 (fun u -> CoercDb.remove_coercion (fun (_,_,u1) -> UriManager.eq u u1))
363 (* remove composites from the lib *)
364 List.iter remove_single_obj composites_in_lib
366 Not_found -> () (* mhh..... *)
369 let generate_projections refinement_toolkit uri fields =
372 CicRecord.projections_of uri
373 (List.map (fun (x,_,_) -> x) fields)
377 (fun (uri, name, bo) (_name, coercion, arity) ->
380 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
381 let attrs = [`Class `Projection; `Generated] in
382 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
383 add_single_obj uri obj refinement_toolkit;
387 (*prerr_endline ("composite for " ^ UriManager.string_of_uri uri);*)
388 (*CSC: I think there is a bug here. The composite coercions
389 are not remembered in the .moo file. Thus they are re-generated
390 every time. Right? *)
392 add_coercion ~add_composites:true refinement_toolkit uri arity
393 (UriManager.buri_of_uri uri)
395 (*prerr_endline ("are: ");
396 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u)) x;
399 (*CSC: I throw the arity away. See comment above *)
405 uris := uri :: composites @ !uris
407 CicTypeChecker.TypeCheckerFailure s ->
409 ("Unable to create projection " ^ name ^ " cause: " ^ Lazy.force s);
410 | CicEnvironment.Object_not_found uri ->
411 let depend = UriManager.name_of_uri uri in
413 ("Unable to create projection " ^ name ^ " because it requires " ^
415 ) projections fields;
418 List.iter remove_single_obj !uris;
421 let build_inversion_principle = ref (fun a b -> assert false);;
423 let generate_inversion refinement_toolkit uri obj =
425 (fun (ind_uri,ind_obj) ->
426 add_single_obj ind_uri ind_obj refinement_toolkit;ind_uri)
427 (!build_inversion_principle uri obj)
430 generate_sibling_mutual_definitions refinement_toolkit uri attrs name_to_avoid
436 (fun (name,idx,ty,bo) (n,uris) ->
437 if name = name_to_avoid then
441 UriManager.uri_of_string
442 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con") in
443 let bo = Cic.Fix (n,funs) in
444 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
445 add_single_obj uri obj refinement_toolkit;
448 | Cic.CoFix (_,funs) ->
451 (fun (name,ty,bo) (n,uris) ->
452 if name = name_to_avoid then
456 UriManager.uri_of_string
457 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con") in
458 let bo = Cic.CoFix (n,funs) in
459 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
460 add_single_obj uri obj refinement_toolkit;
465 let add_obj refinement_toolkit uri obj =
466 add_single_obj uri obj refinement_toolkit;
468 let not_debug = not (Helm_registry.get_bool "matita.debug") in
472 | Cic.Constant (name,Some bo,_,_,attrs) when
473 List.mem (`Flavour `MutualDefinition) attrs ->
476 generate_sibling_mutual_definitions refinement_toolkit uri attrs
478 | Cic.Constant _ -> ()
479 | Cic.InductiveDefinition (_,_,_,attrs) ->
481 generate_elimination_principles uri refinement_toolkit;
482 uris := !uris @ generate_inversion refinement_toolkit uri obj;
483 let rec get_record_attrs =
486 | (`Class (`Record fields))::_ -> Some fields
487 | _::tl -> get_record_attrs tl
489 (match get_record_attrs attrs with
490 | None -> () (* not a record *)
493 (generate_projections refinement_toolkit uri fields))
495 | Cic.Variable _ -> assert false
497 UriManager.UriHashtbl.add auxiliary_lemmas_hashtbl uri !uris;
500 | exn when not_debug ->
501 List.iter remove_single_obj !uris;
507 let res = UriManager.UriHashtbl.find auxiliary_lemmas_hashtbl uri in
508 UriManager.UriHashtbl.remove auxiliary_lemmas_hashtbl uri;
511 Not_found -> [] (*assert false*)
513 List.iter remove_single_obj (uri::uris)