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 obj = CicParser.obj_of_xml filename uri in
91 let output = Unchecked obj in
92 HashTable.add hashtable (uri,0) output ;
96 let is_annotation_uri uri =
97 Str.string_match (Str.regexp ".*\.ann$") (UriManager.string_of_uri uri) 0
101 (* returns the cic object whose uri is uri. If the term is not just in cache, *)
102 (* then it is parsed via CicParser.term_of_xml from the file whose name is *)
103 (* the result of Getter.getxml uri *)
105 match get_obj_and_type_checking_info uri 0 with
111 (*CSC Commento falso *)
113 (* returns the cooked cic object whose uri is uri. The term must be present *)
114 (* and cooked in cache *)
115 let rec get_cooked_obj uri cookingsno =
116 match get_obj_and_type_checking_info uri cookingsno with
118 | Frozen _ -> raise UncookedObj
122 (* is_type_checked uri *)
123 (* CSC: commento falso ed obsoleto *)
124 (* returns true if the term has been type-checked *)
125 (* otherwise it returns false and freeze the term for type-checking *)
126 (* set_type_checking_info must be called to unfreeze the term *)
127 let is_type_checked uri cookingsno =
128 match get_obj_and_type_checking_info uri cookingsno with
129 Cooked obj -> CheckedObj obj
131 HashTable.remove hashtable (uri,0) ;
132 HashTable.add hashtable (uri,0) (Frozen obj) ;
134 | Frozen _ -> raise (CircularDependency (UriManager.string_of_uri uri))
137 (* set_type_checking_info uri *)
138 (* must be called once the type-checking of uri is finished *)
139 (* The object whose uri is uri is unfreezed *)
140 let set_type_checking_info uri =
141 match HashTable.find hashtable (uri,0) with
143 (* let's cook the object at every level *)
144 HashTable.remove hashtable (uri,0) ;
145 let obj' = CicSubstitution.undebrujin_inductive_def uri obj in
146 HashTable.add hashtable (uri,0) (Cooked obj') ;
147 let cooked_objs = !cook_obj obj' uri in
148 let last_cooked_level = ref 0 in
149 let last_cooked_obj = ref obj' in
152 for i = !last_cooked_level + 1 to n do
153 HashTable.add hashtable (uri,i) (Cooked !last_cooked_obj)
155 HashTable.add hashtable (uri,n + 1) (Cooked cobj) ;
156 last_cooked_level := n + 1 ;
157 last_cooked_obj := cobj
159 for i = !last_cooked_level + 1 to UriManager.depth_of_uri uri + 1 do
160 HashTable.add hashtable (uri,i) (Cooked !last_cooked_obj)
162 | _ -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))