1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
33 (* This module implements a trival cache system (an hash-table) for cic *)
34 (* objects. Uses the getter (getter.ml) and the parser (cicParser.ml) *)
36 (******************************************************************************)
38 let cleanup_tmp = true;;
40 let trust_obj = function uri -> true;;
42 type type_checked_obj =
43 CheckedObj of Cic.obj (* cooked obj *)
44 | UncheckedObj of Cic.obj (* uncooked obj to proof-check *)
48 exception AlreadyCooked of string;;
49 exception CircularDependency of string;;
50 exception CouldNotFreeze of string;;
51 exception CouldNotUnfreeze of string;;
53 (* Cache that uses == instead of = for testing equality *)
54 (* Invariant: an object is always in at most one of the *)
55 (* following states: unchecked, frozen and cooked. *)
58 val find_or_add_unchecked :
59 UriManager.uri -> get_object_to_add:(unit -> Cic.obj) -> Cic.obj
60 val unchecked_to_frozen : UriManager.uri -> unit
61 val frozen_to_cooked :
62 uri:UriManager.uri -> unit
63 val find_cooked : key:UriManager.uri -> Cic.obj
64 val add_cooked : key:UriManager.uri -> Cic.obj -> unit
68 module CacheOfCookedObjects :
70 val mem : UriManager.uri -> bool
71 val find : UriManager.uri -> Cic.obj
72 val add : UriManager.uri -> Cic.obj -> unit
78 type t = UriManager.uri
79 let equal = UriManager.eq
80 let hash = Hashtbl.hash
83 module HT = Hashtbl.Make(HashedType);;
84 let hashtable = HT.create 1009;;
91 let find uri = HT.find hashtable uri
94 HT.add hashtable uri obj
98 let frozen_list = ref [];;
99 let unchecked_list = ref [];;
101 let find_or_add_unchecked uri ~get_object_to_add =
103 List.assq uri !unchecked_list
106 if List.mem_assq uri !frozen_list then
107 raise (CircularDependency (UriManager.string_of_uri uri))
109 if CacheOfCookedObjects.mem uri then
110 raise (AlreadyCooked (UriManager.string_of_uri uri))
112 (* OK, it is not already frozen nor cooked *)
113 let obj = get_object_to_add () in
114 unchecked_list := (uri,obj)::!unchecked_list ;
117 let unchecked_to_frozen uri =
119 let obj = List.assq uri !unchecked_list in
120 unchecked_list := List.remove_assq uri !unchecked_list ;
121 frozen_list := (uri,obj)::!frozen_list
123 Not_found -> raise (CouldNotFreeze (UriManager.string_of_uri uri))
125 let frozen_to_cooked ~uri =
127 let obj = List.assq uri !frozen_list in
128 frozen_list := List.remove_assq uri !frozen_list ;
129 CacheOfCookedObjects.add uri obj
131 Not_found -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
133 let find_cooked ~key:uri = CacheOfCookedObjects.find uri;;
134 let add_cooked ~key:uri obj = CacheOfCookedObjects.add uri obj;;
138 let find_or_add_unchecked_to_cache uri =
139 Cache.find_or_add_unchecked uri
142 let filename = Getter.getxml uri in
144 match UriManager.bodyuri_of_uri uri with
148 ignore (Getter.resolve bodyuri) ;
149 (* The body exists ==> it is not an axiom *)
150 Some (Getter.getxml bodyuri)
153 (* The body does not exist ==> we consider it an axiom *)
156 let obj = CicParser.obj_of_xml filename bodyfilename in
159 Unix.unlink filename ;
160 match bodyfilename with
161 Some f -> Unix.unlink f
168 (* set_type_checking_info uri *)
169 (* must be called once the type-checking of uri is finished *)
170 (* The object whose uri is uri is unfreezed *)
171 let set_type_checking_info uri =
172 Cache.frozen_to_cooked uri
175 (* is_type_checked uri *)
176 (* CSC: commento falso ed obsoleto *)
177 (* returns a CheckedObj if the term has been type-checked *)
178 (* otherwise it freezes the term for type-checking and returns
180 (* set_type_checking_info must be called to unfreeze the term *)
181 let is_type_checked ?(trust=true) uri =
183 CheckedObj (Cache.find_cooked uri)
186 let obj = find_or_add_unchecked_to_cache uri in
187 Cache.unchecked_to_frozen uri ;
188 if trust && trust_obj uri then
190 Logger.log (`Trusting uri) ;
191 set_type_checking_info uri ;
192 CheckedObj (Cache.find_cooked uri)
198 (* get_cooked_obj ~trust uri *)
199 (* returns the object if it is already type-checked or if it can be *)
200 (* trusted (if [trust] = true and the trusting function accepts it) *)
201 (* Otherwise it raises Not_found *)
202 let get_cooked_obj ?(trust=true) uri =
204 Cache.find_cooked uri
206 if trust && trust_obj uri then
208 match is_type_checked uri with
209 CheckedObj obj -> obj
214 prerr_endline ("@@@ OOOOOOOPS: get_cooked_obj(" ^ UriManager.string_of_uri uri ^ ") raises Not_found since the object is not type-checked nor trusted.") ;
220 (* returns the cic object whose uri is uri. If the term is not just in cache, *)
221 (* then it is parsed via CicParser.term_of_xml from the file whose name is *)
222 (* the result of Getter.getxml uri *)
228 find_or_add_unchecked_to_cache uri
231 exception OnlyPutOfInductiveDefinitionsIsAllowed
233 let put_inductive_definition uri obj =
235 Cic.InductiveDefinition _ -> Cache.add_cooked uri obj
236 | _ -> raise OnlyPutOfInductiveDefinitionsIsAllowed