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 raise e = print_endline "***" ; flush stdout ; print_endline (Printexc.to_string e) ; flush stdout ; raise e;;
40 (*CSC: forse i due seguenti tipi sono da unificare? *)
44 | Unchecked of Cic.obj
45 type type_checked_obj =
46 CheckedObj of Cic.obj (* cooked obj *)
47 | UncheckedObj of Cic.obj (* uncooked obj *)
50 exception NoFunctionProvided;;
52 let cook_obj = ref (fun obj uri -> raise NoFunctionProvided);;
54 let set_cooking_function foo =
58 exception CircularDependency of string;;
59 exception CouldNotUnfreeze of string;;
60 exception Impossible;;
61 exception UncookedObj;;
65 type t = UriManager.uri * int (* uri, livello di cottura *)
66 let equal (u1,n1) (u2,n2) = UriManager.eq u1 u2 && n1 = n2
67 let hash = Hashtbl.hash
71 (* Hashtable that uses == instead of = for testing equality *)
72 module HashTable = Hashtbl.Make(HashedType);;
74 let hashtable = HashTable.create 271;;
76 (* n is the number of time that the object must be cooked *)
77 let get_obj_and_type_checking_info uri n =
79 HashTable.find hashtable (uri,n)
83 match HashTable.find hashtable (uri,0) with
85 | Frozen _ -> raise Impossible
86 | Unchecked _ as t -> t
89 let filename = Getter.getxml uri in
90 let (annobj,_) = CicParser.term_of_xml filename uri false in
91 let obj = Deannotate.deannotate_obj annobj in
92 let output = Unchecked obj in
93 HashTable.add hashtable (uri,0) output ;
98 (* USEFUL ONLY DURING THE FIXING OF THE FILES *)
99 (* change_obj uri (Some newobj) *)
100 (* maps uri to newobj in cache. *)
101 (* change_obj uri None *)
102 (* maps uri to a freeze dummy-object. *)
103 let change_obj uri newobj =
106 Some newobj' -> Unchecked newobj'
107 | None -> Frozen (Cic.Variable ("frozen-dummy", None, Cic.Implicit))
109 HashTable.remove hashtable (uri,0) ;
110 HashTable.add hashtable (uri,0) newobj
113 let is_annotation_uri uri =
114 Str.string_match (Str.regexp ".*\.ann$") (UriManager.string_of_uri uri) 0
117 (* returns both the annotated and deannotated uncooked forms (plus the *)
118 (* map from ids to annotation targets) *)
119 let get_annobj_and_type_checking_info uri =
120 let filename = Getter.getxml uri in
121 match CicParser.term_of_xml filename uri true with
122 (_, None) -> raise Impossible
123 | (annobj, Some ids_to_targets) ->
124 (* If uri is the uri of an annotation, let's use the annotation file *)
125 if is_annotation_uri uri then
126 (* CSC: la roba annotata non dovrebbe piu' servire
127 AnnotationParser.annotate (Getter.get_ann uri) ids_to_targets ;
130 (annobj, ids_to_targets, HashTable.find hashtable (uri,0))
133 let obj = Deannotate.deannotate_obj annobj in
134 let output = Unchecked obj in
135 HashTable.add hashtable (uri,0) output ;
136 (annobj, ids_to_targets, output)
141 (* returns the cic object whose uri is uri. If the term is not just in cache, *)
142 (* then it is parsed via CicParser.term_of_xml from the file whose name is *)
143 (* the result of Getter.getxml uri *)
145 match get_obj_and_type_checking_info uri 0 with
152 (* returns the cic object whose uri is uri either in annotated and *)
153 (* deannotated form. The term is put into the cache if it's not there yet. *)
155 let (ann, ids_to_targets, deann) = get_annobj_and_type_checking_info uri in
159 | Frozen _ -> raise (CircularDependency (UriManager.string_of_uri uri))
162 (ann, ids_to_targets, deannobj)
165 (*CSC Commento falso *)
167 (* returns the cooked cic object whose uri is uri. The term must be present *)
168 (* and cooked in cache *)
169 let rec get_cooked_obj uri cookingsno =
170 match get_obj_and_type_checking_info uri cookingsno with
172 | Frozen _ -> raise UncookedObj
176 (* is_type_checked uri *)
177 (* CSC: commento falso ed obsoleto *)
178 (* returns true if the term has been type-checked *)
179 (* otherwise it returns false and freeze the term for type-checking *)
180 (* set_type_checking_info must be called to unfreeze the term *)
181 let is_type_checked uri cookingsno =
182 match get_obj_and_type_checking_info uri cookingsno with
183 Cooked obj -> CheckedObj obj
185 HashTable.remove hashtable (uri,0) ;
186 HashTable.add hashtable (uri,0) (Frozen obj) ;
188 | Frozen _ -> raise (CircularDependency (UriManager.string_of_uri uri))
191 (* set_type_checking_info uri *)
192 (* must be called once the type-checking of uri is finished *)
193 (* The object whose uri is uri is unfreezed *)
194 let set_type_checking_info uri =
195 match HashTable.find hashtable (uri,0) with
197 (* let's cook the object at every level *)
198 HashTable.remove hashtable (uri,0) ;
199 let obj' = CicSubstitution.undebrujin_inductive_def uri obj in
200 HashTable.add hashtable (uri,0) (Cooked obj') ;
201 let cooked_objs = !cook_obj obj' uri in
202 let last_cooked_level = ref 0 in
203 let last_cooked_obj = ref obj' in
206 for i = !last_cooked_level + 1 to n do
207 HashTable.add hashtable (uri,i) (Cooked !last_cooked_obj)
209 HashTable.add hashtable (uri,n + 1) (Cooked cobj) ;
210 last_cooked_level := n + 1 ;
211 last_cooked_obj := cobj
213 for i = !last_cooked_level + 1 to UriManager.depth_of_uri uri + 1 do
214 HashTable.add hashtable (uri,i) (Cooked !last_cooked_obj)
216 | _ -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))