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 type type_checked_obj =
39 CheckedObj of Cic.obj (* cooked obj *)
40 | UncheckedObj of Cic.obj (* uncooked obj to proof-check *)
43 exception NoFunctionProvided;;
45 let cook_obj = ref (fun obj uri -> raise NoFunctionProvided);;
47 let set_cooking_function foo =
51 exception AlreadyCooked of string;;
52 exception CircularDependency of string;;
53 exception CouldNotFreeze of string;;
54 exception CouldNotUnfreeze of string;;
56 (* Cache that uses == instead of = for testing equality *)
57 (* Invariant: an object is always in at most one of the *)
58 (* following states: unchecked, frozen and cooked. *)
61 val find_or_add_unchecked :
62 UriManager.uri -> get_object_to_add:(unit -> Cic.obj) -> Cic.obj
63 val unchecked_to_frozen : UriManager.uri -> unit
64 val frozen_to_cooked :
67 (object_to_cook:Cic.obj ->
68 add_cooked:(UriManager.uri * int-> Cic.obj -> unit)
72 val find_cooked : key:(UriManager.uri * int) -> Cic.obj
76 module CacheOfCookedObjects :
78 val mem : (UriManager.uri * int) -> bool
79 val find : (UriManager.uri * int) -> Cic.obj
80 val add : (UriManager.uri * int) -> Cic.obj -> unit
86 type t = UriManager.uri * int (* uri, livello di cottura *)
87 let equal (u1,n1) (u2,n2) = UriManager.eq u1 u2 && n1 = n2
88 let hash = Hashtbl.hash
91 module HT = Hashtbl.Make(HashedType);;
92 let hashtable = HT.create 1009;;
93 let mem = HT.mem hashtable;;
94 let find = HT.find hashtable;;
95 let add = HT.add hashtable;;
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,0) 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 ~cooking_procedure =
127 let obj = List.assq uri !frozen_list in
128 frozen_list := List.remove_assq uri !frozen_list ;
129 cooking_procedure ~object_to_cook:obj ~add_cooked:CacheOfCookedObjects.add
131 Not_found -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
133 let find_cooked = CacheOfCookedObjects.find;;
137 (* get_cooked_obj uri *)
138 (* returns the cooked cic object whose uri is uri. The term must be present *)
139 (* and cooked in cache *)
140 let get_cooked_obj uri cookingsno =
141 Cache.find_cooked (uri,cookingsno)
144 let find_or_add_unchecked_to_cache uri =
145 Cache.find_or_add_unchecked uri
148 let filename = Getter.getxml uri in
149 let obj = CicParser.obj_of_xml filename uri in
155 (* returns the cic object whose uri is uri. If the term is not just in cache, *)
156 (* then it is parsed via CicParser.term_of_xml from the file whose name is *)
157 (* the result of Getter.getxml uri *)
163 find_or_add_unchecked_to_cache uri
166 (* is_type_checked uri *)
167 (* CSC: commento falso ed obsoleto *)
168 (* returns a CheckedObj if the term has been type-checked *)
169 (* otherwise it freezes the term for type-checking and returns
171 (* set_type_checking_info must be called to unfreeze the term *)
172 let is_type_checked uri cookingsno =
174 CheckedObj (Cache.find_cooked (uri,cookingsno))
177 let obj = find_or_add_unchecked_to_cache uri in
178 Cache.unchecked_to_frozen uri ;
182 (* set_type_checking_info uri *)
183 (* must be called once the type-checking of uri is finished *)
184 (* The object whose uri is uri is unfreezed *)
185 let set_type_checking_info uri =
186 Cache.frozen_to_cooked uri
187 (fun ~object_to_cook:obj ~add_cooked ->
188 (* let's cook the object at every level *)
189 let obj' = CicSubstitution.undebrujin_inductive_def uri obj in
190 add_cooked (uri,0) obj' ;
191 let cooked_objs = !cook_obj obj' uri in
192 let last_cooked_level = ref 0 in
193 let last_cooked_obj = ref obj' in
196 for i = !last_cooked_level + 1 to n do
197 add_cooked (uri,i) !last_cooked_obj
199 add_cooked (uri,n + 1) cobj ;
200 last_cooked_level := n + 1 ;
201 last_cooked_obj := cobj
203 for i = !last_cooked_level + 1 to UriManager.depth_of_uri uri + 1 do
204 add_cooked (uri,i) !last_cooked_obj