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 let object_declaration_hook = ref (fun _ _ -> ());;
29 let set_object_declaration_hook f =
30 object_declaration_hook := f
32 exception AlreadyDefined of UriManager.uri
34 let auxiliary_lemmas_hashtbl = UriManager.UriHashtbl.create 29
36 (* uri |--> (derived_coercions_in_the_coercion_DB, derived_coercions_in_lib)
38 * in case of remove_coercion uri, the first component is removed from the
39 * coercion DB, while the second is passed to remove_obj (and is not [] only if
40 * add_coercion is called with add_composites
42 let coercion_hashtbl = UriManager.UriHashtbl.create 3
45 let innertypesuri = UriManager.innertypesuri_of_uri uri in
46 let bodyuri = UriManager.bodyuri_of_uri uri in
47 let univgraphuri = UriManager.univgraphuri_of_uri uri in
48 innertypesuri,bodyuri,univgraphuri
50 let paths_and_uris_of_obj uri =
51 let resolved = Http_getter.filename' ~local:true ~writable:true uri in
52 let basepath = Filename.dirname resolved in
53 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
54 let innertypesfilename=(UriManager.nameext_of_uri innertypesuri)^".xml.gz"in
55 let innertypespath = basepath ^ "/" ^ innertypesfilename in
56 let xmlfilename = (UriManager.nameext_of_uri uri) ^ ".xml.gz" in
57 let xmlpath = basepath ^ "/" ^ xmlfilename in
58 let xmlbodyfilename = (UriManager.nameext_of_uri uri) ^ ".body.xml.gz" in
59 let xmlbodypath = basepath ^ "/" ^ xmlbodyfilename in
60 let xmlunivgraphfilename=(UriManager.nameext_of_uri univgraphuri)^".xml.gz"in
61 let xmlunivgraphpath = basepath ^ "/" ^ xmlunivgraphfilename in
62 xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
63 xmlunivgraphpath, univgraphuri
65 let save_object_to_disk uri obj ugraph univlist =
67 if not (Helm_registry.get_opt_default
68 Helm_registry.bool "matita.nodisk" ~default:false)
72 let ensure_path_exists path =
73 let dir = Filename.dirname path in
76 (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
77 let annobj, innertypes, ids_to_inner_sorts, generate_attributes =
78 if Helm_registry.get_bool "matita.system" &&
79 not (Helm_registry.get_bool "matita.noinnertypes")
81 let annobj, _, _, ids_to_inner_sorts, ids_to_inner_types, _, _ =
82 Cic2acic.acic_object_of_cic_object obj
85 Cic2Xml.print_inner_types
86 uri ~ids_to_inner_sorts ~ids_to_inner_types ~ask_dtd_to_the_getter:false
88 annobj, Some innertypesxml, Some ids_to_inner_sorts, false
90 let annobj = Cic2acic.plain_acic_object_of_cic_object obj in
91 annobj, None, None, true
96 uri ?ids_to_inner_sorts ~ask_dtd_to_the_getter:false
97 ~generate_attributes annobj
99 let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
100 xmlunivgraphpath, univgraphuri =
101 paths_and_uris_of_obj uri
103 write (List.iter HExtlib.mkdir) (List.map Filename.dirname [xmlpath]);
104 (* now write to disk *)
105 write ensure_path_exists xmlpath;
106 write (Xml.pp ~gzip:true xml) (Some xmlpath);
107 write (CicUniv.write_xml_of_ugraph xmlunivgraphpath ugraph) univlist;
108 (* we return a list of uri,path we registered/created *)
110 (univgraphuri,xmlunivgraphpath) ::
111 (* now the optional inner types, both write and register *)
112 (match innertypes with
114 | Some innertypesxml ->
115 write ensure_path_exists innertypespath;
116 write (Xml.pp ~gzip:true innertypesxml) (Some innertypespath);
117 [innertypesuri, innertypespath]
119 (* now the optional body, both write and register *)
120 (match bodyxml,bodyuri with
122 | Some bodyxml,Some bodyuri->
123 write ensure_path_exists xmlbodypath;
124 write (Xml.pp ~gzip:true bodyxml) (Some xmlbodypath);
125 [bodyuri, xmlbodypath]
130 let profiler = HExtlib.profile "add_obj.typecheck_obj" in
131 fun uri obj -> profiler.HExtlib.profile (CicTypeChecker.typecheck_obj uri) obj
134 let profiler = HExtlib.profile "add_obj.index_obj" in
136 profiler.HExtlib.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
138 let add_single_obj uri obj refinement_toolkit =
139 let module RT = RefinementTool in
141 if (*List.mem `Generated (CicUtil.attributes_of_obj obj) &&*)
142 not (CoercDb.is_a_coercion' (Cic.Const (uri, [])))
144 refinement_toolkit.RT.pack_coercion_obj obj
148 let dbd = LibraryDb.instance () in
149 if CicEnvironment.in_library uri then
150 raise (AlreadyDefined uri)
152 (*CicUniv.reset_spent_time ();
153 let before = Unix.gettimeofday () in*)
154 typecheck_obj uri obj; (* 1 *)
155 (*let after = Unix.gettimeofday () in
156 let univ_time = CicUniv.get_spent_time () in
157 let total_time = after -. before in
159 (Printf.sprintf "QED: %%univ = %2.5f, total = %2.5f, univ = %2.5f, %s\n"
160 (univ_time *. 100. /. total_time) (total_time) (univ_time)
161 (UriManager.name_of_uri uri));*)
162 let _, ugraph, univlist =
163 CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph uri in
165 index_obj ~dbd ~uri; (* 2 must be in the env *)
168 let new_stuff = save_object_to_disk uri obj ugraph univlist in
169 (* EXPERIMENTAL: pretty print the object in natural language *)
170 (try !object_declaration_hook uri obj
172 prerr_endline ("Error: object_declaration_hook failed"^
173 Printexc.to_string exc));
176 (Printf.sprintf "%s defined" (UriManager.string_of_uri uri))
178 List.iter HExtlib.safe_remove (List.map snd new_stuff); (* -3 *)
181 ignore(LibraryDb.remove_uri uri); (* -2 *)
184 CicEnvironment.remove_obj uri; (* -1 *)
188 let remove_single_obj uri =
189 let derived_uris_of_uri uri =
190 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
191 innertypesuri::univgraphuri::(match bodyuri with None -> [] | Some u -> [u])
194 if UriManager.uri_is_ind uri then LibraryDb.xpointers_of_ind uri else [uri]
196 let files_to_remove = uri :: derived_uris_of_uri uri in
200 let file = Http_getter.resolve' ~local:true ~writable:true uri in
201 HExtlib.safe_remove file;
202 HExtlib.rmdir_descend (Filename.dirname file)
203 with Http_getter_types.Key_not_found _ -> ());
207 ignore (LibraryDb.remove_uri uri);
208 (*CoercGraph.remove_coercion uri;*)
210 CicEnvironment.remove_obj uri
212 (*** GENERATION OF AUXILIARY LEMMAS ***)
214 let generate_elimination_principles uri refinement_toolkit =
219 let uri,obj = CicElim.elim_of ~sort uri i in
220 add_single_obj uri obj refinement_toolkit;
222 with CicElim.Can_t_eliminate -> ()
226 elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
228 List.iter remove_single_obj !uris;
231 let (obj, univ) = (CicEnvironment.get_obj CicUniv.empty_ugraph uri) in
233 | Cic.InductiveDefinition (indTypes, _, _, _) ->
234 let counter = ref 0 in
235 List.iter (fun _ -> elim !counter; counter := !counter+1) indTypes;
238 failwith (Printf.sprintf "not an inductive definition (%s)"
239 (UriManager.string_of_uri uri))
241 (* COERCIONS ***********************************************************)
243 let remove_all_coercions () =
244 UriManager.UriHashtbl.clear coercion_hashtbl;
245 CoercDb.remove_coercion (fun (_,_,_,_) -> true)
250 UriManager.UriHashtbl.fold
251 (fun k v acc -> (k,v) :: acc) h []
255 stack := (CoercDb.dump (), h2l coercion_hashtbl) :: !stack;
256 remove_all_coercions ()
261 | [] -> raise (Failure "Unable to POP from librarySync.ml")
264 remove_all_coercions ();
266 List.iter (fun (k,v) -> UriManager.UriHashtbl.add coercion_hashtbl k v)
270 let add_coercion ~add_composites refinement_toolkit uri arity saturations
274 let coer = CicUtil.term_of_uri uri in
275 CicTypeChecker.type_of_aux' [] [] coer CicUniv.empty_ugraph
277 (* we have to get the source and the tgt type uri
278 * in Coq syntax we have already their names, but
279 * since we don't support Funclass and similar I think
280 * all the coercion should be of the form
282 * So we should be able to extract them from the coercion type
284 * Currently only (_:T1)T2 is supported.
285 * should we saturate it with metas in case we insert it?
289 let rec aux = function
290 | Cic.Prod( _, src, tgt) -> src::aux tgt
295 let src_carr, tgt_carr =
296 let get_classes arity saturations l =
297 (* this is the ackerman's function revisited *)
298 let rec aux = function
299 0,0,None,tgt::src::_ -> src,Some (`Class tgt)
300 | 0,0,target,src::_ -> src,target
301 | 0,saturations,None,tgt::tl -> aux (0,saturations,Some (`Class tgt),tl)
302 | 0,saturations,target,_::tl -> aux (0,saturations - 1,target,tl)
303 | arity,saturations,None,_::tl ->
304 aux (arity, saturations, Some `Funclass, tl)
305 | arity,saturations,target,_::tl ->
306 aux (arity - 1, saturations, target, tl)
309 aux (arity,saturations,None,List.rev l)
311 let types = spine2list coer_ty in
312 let src,tgt = get_classes arity saturations types in
313 CoercDb.coerc_carr_of_term (CicReduction.whd ~delta:false [] src),
316 | Some `Funclass -> CoercDb.Fun arity
317 | Some (`Class tgt) ->
318 CoercDb.coerc_carr_of_term (CicReduction.whd ~delta:false [] tgt)
320 let already_in_obj src_carr tgt_carr uri obj =
327 | Cic.Constant (_, Some bo, _, _, _) -> bo
330 CoercDb.eq_carr s src_carr &&
331 CoercDb.eq_carr t tgt_carr &&
332 if fst (CicReduction.are_convertible [] (CicUtil.term_of_uri u) bo
333 CicUniv.oblivion_ugraph)
337 UriManager.string_of_uri u ^ " and " ^ UriManager.string_of_uri
338 uri^" are not convertible, but are between the same nodes.\n"^
339 "From now on unification can fail randomly.");
344 if not add_composites then
345 (CoercDb.add_coercion (src_carr, tgt_carr, uri, saturations);
346 UriManager.UriHashtbl.add coercion_hashtbl uri ([],[]);
350 CicCoercion.close_coercion_graph src_carr tgt_carr uri saturations
354 List.filter (fun (s,t,u,_,obj,_) -> not(already_in_obj s t u obj))
357 let composite_uris = List.map (fun (_,_,uri,_,_,_) -> uri) new_coercions in
360 (fun (src,tgt,uri,saturations,_,_) ->
361 CoercDb.add_coercion (src,tgt,uri,saturations))
363 CoercDb.add_coercion (src_carr, tgt_carr, uri, saturations);
364 (* add the composites obj and they eventual lemmas *)
367 (fun acc (_,tgt,uri,saturations,obj,arity) ->
368 add_single_obj uri obj refinement_toolkit;
369 (uri,arity,saturations)::acc)
372 (* store that composite_uris are related to uri. the first component is
373 * the stuff in the DB while the second is stuff for remove_obj *)
375 prerr_endline ("adding: " ^
376 string_of_bool add_composites ^ UriManager.string_of_uri uri);
377 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
380 UriManager.UriHashtbl.add
381 coercion_hashtbl uri (composite_uris,composite_uris);
383 prerr_endline ("lemmas:");
384 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
386 prerr_endline ("lemmas END");*)
390 let remove_coercion uri =
392 let (composites_in_db, composites_in_lib) =
393 UriManager.UriHashtbl.find coercion_hashtbl uri
395 (*prerr_endline ("removing: " ^UriManager.string_of_uri uri);
396 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u))
398 UriManager.UriHashtbl.remove coercion_hashtbl uri;
399 CoercDb.remove_coercion
400 (fun (_,_,u,_) -> UriManager.eq uri u);
401 (* remove from the DB *)
403 (fun u -> CoercDb.remove_coercion (fun (_,_,u1,_) -> UriManager.eq u u1))
405 (* remove composites from the lib *)
406 List.iter remove_single_obj composites_in_lib
408 Not_found -> HLog.warn "Coercion removal raise Not_found" (* mhh..... *)
411 let generate_projections refinement_toolkit uri fields =
414 CicRecord.projections_of uri
415 (List.map (fun (x,_,_) -> x) fields)
419 (fun (uri, name, bo) (_name, coercion, arity) ->
420 let saturations = 0 in
423 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
424 let attrs = [`Class `Projection; `Generated] in
425 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
426 add_single_obj uri obj refinement_toolkit;
430 (*prerr_endline ("composite for " ^ UriManager.string_of_uri uri);*)
431 (*CSC: I think there is a bug here. The composite coercions
432 are not remembered in the .moo file. Thus they are re-generated
433 every time. Right? *)
435 add_coercion ~add_composites:true refinement_toolkit uri arity
436 saturations (UriManager.buri_of_uri uri)
438 (*prerr_endline ("are: ");
439 List.iter (fun u -> prerr_endline (UriManager.string_of_uri u)) x;
442 (*CSC: I throw the arity away. See comment above *)
443 List.map (fun u,_,_ -> u) x
448 uris := uri :: composites @ !uris
450 CicTypeChecker.TypeCheckerFailure s ->
452 ("Unable to create projection " ^ name ^ " cause: " ^ Lazy.force s);
453 | CicEnvironment.Object_not_found uri ->
454 let depend = UriManager.name_of_uri uri in
456 ("Unable to create projection " ^ name ^ " because it requires " ^
458 ) projections fields;
461 List.iter remove_single_obj !uris;
464 let build_inversion_principle = ref (fun a b -> assert false);;
466 let generate_inversion refinement_toolkit uri obj =
468 (fun (ind_uri,ind_obj) ->
469 add_single_obj ind_uri ind_obj refinement_toolkit;ind_uri)
470 (!build_inversion_principle uri obj)
473 generate_sibling_mutual_definitions refinement_toolkit uri attrs name_to_avoid
479 (fun (name,idx,ty,bo) (n,uris) ->
480 if name = name_to_avoid then
484 UriManager.uri_of_string
485 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con") in
486 let bo = Cic.Fix (n,funs) in
487 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
488 add_single_obj uri obj refinement_toolkit;
491 | Cic.CoFix (_,funs) ->
494 (fun (name,ty,bo) (n,uris) ->
495 if name = name_to_avoid then
499 UriManager.uri_of_string
500 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con") in
501 let bo = Cic.CoFix (n,funs) in
502 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
503 add_single_obj uri obj refinement_toolkit;
508 let add_obj refinement_toolkit uri obj =
509 add_single_obj uri obj refinement_toolkit;
511 let not_debug = not (Helm_registry.get_bool "matita.debug") in
515 | Cic.Constant (name,Some bo,_,_,attrs) when
516 List.mem (`Flavour `MutualDefinition) attrs ->
519 generate_sibling_mutual_definitions refinement_toolkit uri attrs
521 | Cic.Constant _ -> ()
522 | Cic.InductiveDefinition (inductivefuns,_,_,attrs) ->
523 let _,inductive,_,_ = List.hd inductivefuns in
527 generate_elimination_principles uri refinement_toolkit;
528 uris := !uris @ generate_inversion refinement_toolkit uri obj;
530 let rec get_record_attrs =
533 | (`Class (`Record fields))::_ -> Some fields
534 | _::tl -> get_record_attrs tl
536 (match get_record_attrs attrs with
537 | None -> () (* not a record *)
540 (generate_projections refinement_toolkit uri fields))
542 | Cic.Variable _ -> assert false
544 UriManager.UriHashtbl.add auxiliary_lemmas_hashtbl uri !uris;
547 | exn when not_debug ->
548 List.iter remove_single_obj !uris;
554 let res = UriManager.UriHashtbl.find auxiliary_lemmas_hashtbl uri in
555 UriManager.UriHashtbl.remove auxiliary_lemmas_hashtbl uri;
558 Not_found -> [] (*assert false*)
560 List.iter remove_single_obj (uri::uris)