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/
26 exception AlreadyDefined of UriManager.uri
29 let innertypesuri = UriManager.innertypesuri_of_uri uri in
30 let bodyuri = UriManager.bodyuri_of_uri uri in
31 let univgraphuri = UriManager.univgraphuri_of_uri uri in
32 innertypesuri,bodyuri,univgraphuri
34 let paths_and_uris_of_obj uri ~basedir =
35 let basedir = basedir ^ "/xml" in
36 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
37 let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
38 (UriManager.string_of_uri innertypesuri) ^ ".xml.gz" in
39 let innertypespath = basedir ^ "/" ^ innertypesfilename in
40 let xmlfilename = Str.replace_first (Str.regexp "^cic:/") ""
41 (UriManager.string_of_uri uri) ^ ".xml.gz" in
42 let xmlpath = basedir ^ "/" ^ xmlfilename in
43 let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:/") ""
44 (UriManager.string_of_uri uri) ^ ".body.xml.gz" in
45 let xmlbodypath = basedir ^ "/" ^ xmlbodyfilename in
46 let xmlunivgraphfilename = Str.replace_first (Str.regexp "^cic:/") ""
47 (UriManager.string_of_uri univgraphuri) ^ ".xml.gz" in
48 let xmlunivgraphpath = basedir ^ "/" ^ xmlunivgraphfilename in
49 xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
50 xmlunivgraphpath, univgraphuri
52 let save_object_to_disk ~basedir uri obj ugraph univlist =
53 let ensure_path_exists path =
54 let dir = Filename.dirname path in
57 (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
58 let annobj = Cic2acic.plain_acic_object_of_cic_object obj in
62 uri ?ids_to_inner_sorts:None ~ask_dtd_to_the_getter:false annobj
64 let xmlpath, xmlbodypath, innertypespath, bodyuri, innertypesuri,
65 xmlunivgraphpath, univgraphuri =
66 paths_and_uris_of_obj uri basedir
68 List.iter HExtlib.mkdir (List.map Filename.dirname [xmlpath]);
69 (* now write to disk *)
70 ensure_path_exists xmlpath;
71 Xml.pp ~gzip:true xml (Some xmlpath);
72 CicUniv.write_xml_of_ugraph xmlunivgraphpath ugraph univlist;
73 (* we return a list of uri,path we registered/created *)
75 (univgraphuri,xmlunivgraphpath) ::
76 (* now the optional body, both write and register *)
77 (match bodyxml,bodyuri with
79 | Some bodyxml,Some bodyuri->
80 ensure_path_exists xmlbodypath;
81 Xml.pp ~gzip:true bodyxml (Some xmlbodypath);
82 [bodyuri, xmlbodypath]
87 let profiler = HExtlib.profile "add_obj.typecheck_obj" in
88 fun uri obj -> profiler.HExtlib.profile (CicTypeChecker.typecheck_obj uri) obj
91 let profiler = HExtlib.profile "add_obj.index_obj" in
93 profiler.HExtlib.profile (fun uri -> MetadataDb.index_obj ~dbd ~uri) uri
95 let add_single_obj uri obj ~basedir =
96 let dbd = LibraryDb.instance () in
97 if CicEnvironment.in_library uri then
98 raise (AlreadyDefined uri)
100 typecheck_obj uri obj; (* 1 *)
101 let _, ugraph, univlist =
102 CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph uri in
104 index_obj ~dbd ~uri; (* 2 must be in the env *)
107 let new_stuff = save_object_to_disk ~basedir uri obj ugraph univlist in
110 (Printf.sprintf "%s defined" (UriManager.string_of_uri uri))
112 List.iter HExtlib.safe_remove (List.map snd new_stuff); (* -3 *)
115 ignore(LibraryDb.remove_uri uri); (* -2 *)
118 CicEnvironment.remove_obj uri; (* -1 *)
122 let remove_single_obj uri =
123 let derived_uris_of_uri uri =
124 let innertypesuri, bodyuri, univgraphuri = uris_of_obj uri in
125 innertypesuri::univgraphuri::(match bodyuri with None -> [] | Some u -> [u])
129 (if UriManager.uri_is_ind uri then LibraryDb.xpointers_of_ind uri else []) @
130 derived_uris_of_uri uri
135 let file = Http_getter.resolve' uri in
136 HExtlib.safe_remove file;
137 HExtlib.rmdir_descend (Filename.dirname file)
138 with Http_getter_types.Key_not_found _ -> ());
139 ignore (LibraryDb.remove_uri uri);
140 CoercDb.remove_coercion (fun (_,_,u) -> UriManager.eq u uri);
141 CicEnvironment.remove_obj uri)
144 (*** GENERATION OF AUXILIARY LEMMAS ***)
146 let generate_elimination_principles ~basedir uri =
150 let uri,obj = CicElim.elim_of ~sort uri 0 in
151 add_single_obj uri obj ~basedir;
153 with CicElim.Can_t_eliminate -> ()
156 List.iter elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
159 List.iter remove_single_obj !uris;
162 let generate_projections ~basedir uri fields =
164 let projections = CicRecord.projections_of uri fields in
167 (fun (uri, name, bo) ->
170 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
171 let attrs = [`Class `Projection; `Generated] in
172 let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
173 add_single_obj ~basedir uri obj;
176 CicTypeChecker.TypeCheckerFailure s ->
178 ("Unable to create projection " ^ name ^ " cause: " ^ Lazy.force s);
179 | CicEnvironment.Object_not_found uri ->
180 let depend = UriManager.name_of_uri uri in
182 ("Unable to create projection " ^ name ^ " because it requires " ^
187 List.iter remove_single_obj !uris;
190 let auxiliary_lemmas_hashtbl = UriManager.UriHashtbl.create 29
192 let add_obj uri obj ~basedir =
193 add_single_obj uri obj ~basedir;
198 | Cic.Constant _ -> ()
199 | Cic.InductiveDefinition (_,_,_,attrs) ->
200 uris := !uris @ generate_elimination_principles ~basedir uri;
201 let rec get_record_attrs =
204 | (`Class (`Record fields))::_ -> Some fields
205 | _::tl -> get_record_attrs tl
207 (match get_record_attrs attrs with
208 | None -> () (* not a record *)
210 uris := !uris @ (generate_projections ~basedir uri fields))
212 | Cic.Variable _ -> assert false
214 UriManager.UriHashtbl.add auxiliary_lemmas_hashtbl uri !uris;
217 List.iter remove_single_obj !uris;
223 UriManager.UriHashtbl.find auxiliary_lemmas_hashtbl uri
227 List.iter remove_single_obj (uri::uris)