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
67 module CacheOfCookedObjects :
69 val mem : UriManager.uri -> bool
70 val find : UriManager.uri -> Cic.obj
71 val add : UriManager.uri -> Cic.obj -> unit
77 type t = UriManager.uri
78 let equal = UriManager.eq
79 let hash = Hashtbl.hash
82 module HT = Hashtbl.Make(HashedType);;
83 let hashtable = HT.create 1009;;
90 let find uri = HT.find hashtable uri
93 HT.add hashtable uri obj
97 let frozen_list = ref [];;
98 let unchecked_list = ref [];;
100 let find_or_add_unchecked uri ~get_object_to_add =
102 List.assq uri !unchecked_list
105 if List.mem_assq uri !frozen_list then
106 raise (CircularDependency (UriManager.string_of_uri uri))
108 if CacheOfCookedObjects.mem uri then
109 raise (AlreadyCooked (UriManager.string_of_uri uri))
111 (* OK, it is not already frozen nor cooked *)
112 let obj = get_object_to_add () in
113 unchecked_list := (uri,obj)::!unchecked_list ;
116 let unchecked_to_frozen uri =
118 let obj = List.assq uri !unchecked_list in
119 unchecked_list := List.remove_assq uri !unchecked_list ;
120 frozen_list := (uri,obj)::!frozen_list
122 Not_found -> raise (CouldNotFreeze (UriManager.string_of_uri uri))
124 let frozen_to_cooked ~uri =
126 let obj = List.assq uri !frozen_list in
127 frozen_list := List.remove_assq uri !frozen_list ;
128 CacheOfCookedObjects.add uri obj
130 Not_found -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
132 let find_cooked ~key:uri = CacheOfCookedObjects.find uri;;
136 let find_or_add_unchecked_to_cache uri =
137 Cache.find_or_add_unchecked uri
140 let filename = Getter.getxml uri in
142 match UriManager.bodyuri_of_uri uri with
146 ignore (Getter.resolve bodyuri) ;
147 (* The body exists ==> it is not an axiom *)
148 Some (Getter.getxml bodyuri)
151 (* The body does not exist ==> we consider it an axiom *)
154 let obj = CicParser.obj_of_xml filename bodyfilename in
157 Unix.unlink filename ;
158 match bodyfilename with
159 Some f -> Unix.unlink f
166 (* set_type_checking_info uri *)
167 (* must be called once the type-checking of uri is finished *)
168 (* The object whose uri is uri is unfreezed *)
169 let set_type_checking_info uri =
170 Cache.frozen_to_cooked uri
173 (* is_type_checked uri *)
174 (* CSC: commento falso ed obsoleto *)
175 (* returns a CheckedObj if the term has been type-checked *)
176 (* otherwise it freezes the term for type-checking and returns
178 (* set_type_checking_info must be called to unfreeze the term *)
179 let is_type_checked ?(trust=true) uri =
181 CheckedObj (Cache.find_cooked uri)
184 let obj = find_or_add_unchecked_to_cache uri in
185 Cache.unchecked_to_frozen uri ;
186 if trust && trust_obj uri then
188 Logger.log (`Trusting uri) ;
189 set_type_checking_info uri ;
190 CheckedObj (Cache.find_cooked uri)
196 (* get_cooked_obj ~trust uri *)
197 (* returns the object if it is already type-checked or if it can be *)
198 (* trusted (if [trust] = true and the trusting function accepts it) *)
199 (* Otherwise it raises Not_found *)
200 let get_cooked_obj ?(trust=true) uri =
202 Cache.find_cooked uri
204 if trust && trust_obj uri then
206 match is_type_checked uri with
207 CheckedObj obj -> obj
212 prerr_endline ("@@@ OOOOOOOPS: get_cooked_obj(" ^ UriManager.string_of_uri uri ^ ") raises Not_found since the object is not type-checked nor trusted.") ;
218 (* returns the cic object whose uri is uri. If the term is not just in cache, *)
219 (* then it is parsed via CicParser.term_of_xml from the file whose name is *)
220 (* the result of Getter.getxml uri *)
226 find_or_add_unchecked_to_cache uri