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;;
41 (*let trust_obj = function uri -> false;;*)
43 type type_checked_obj =
44 CheckedObj of Cic.obj (* cooked obj *)
45 | UncheckedObj of Cic.obj (* uncooked obj to proof-check *)
49 exception AlreadyCooked of string;;
50 exception CircularDependency of string;;
51 exception CouldNotFreeze of string;;
52 exception CouldNotUnfreeze of string;;
54 (* Cache that uses == instead of = for testing equality *)
55 (* Invariant: an object is always in at most one of the *)
56 (* following states: unchecked, frozen and cooked. *)
59 val find_or_add_unchecked :
60 UriManager.uri -> get_object_to_add:(unit -> Cic.obj) -> Cic.obj
61 val unchecked_to_frozen : UriManager.uri -> unit
62 val frozen_to_cooked :
63 uri:UriManager.uri -> unit
64 val find_cooked : key:UriManager.uri -> Cic.obj
65 val add_cooked : key:UriManager.uri -> Cic.obj -> unit
67 val dump_to_channel : ?callback:(string -> unit) -> out_channel -> unit
68 val restore_from_channel : ?callback:(string -> unit) -> in_channel -> unit
69 val empty : unit -> unit
73 module CacheOfCookedObjects :
75 val mem : UriManager.uri -> bool
76 val find : UriManager.uri -> Cic.obj
77 val add : UriManager.uri -> Cic.obj -> unit
79 (** (de)serialization of type checker cache *)
80 val dump_to_channel : ?callback:(string -> unit) -> out_channel -> unit
81 val restore_from_channel : ?callback:(string -> unit) -> in_channel -> unit
82 val empty : unit -> unit
88 type t = UriManager.uri
89 let equal = UriManager.eq
90 let hash = Hashtbl.hash
93 module HT = Hashtbl.Make(HashedType);;
94 let hashtable = HT.create 1009;;
101 let find uri = HT.find hashtable uri
104 HT.add hashtable uri obj
107 (* used to hash cons uris on restore to grant URI structure unicity *)
109 let module C = Cic in
111 UriManager.uri_of_string (UriManager.string_of_uri uri)
113 let rec restore_in_term =
116 | C.Var (uri,exp_named_subst) ->
117 let uri' = recons uri in
118 let exp_named_subst' =
120 (function (uri,t) ->(recons uri,restore_in_term t)) exp_named_subst
122 C.Var (uri',exp_named_subst')
128 | Some t -> Some (restore_in_term t)
133 | C.Implicit _ as t -> t
134 | C.Cast (te,ty) -> C.Cast (restore_in_term te, restore_in_term ty)
135 | C.Prod (n,s,t) -> C.Prod (n, restore_in_term s, restore_in_term t)
136 | C.Lambda (n,s,t) -> C.Lambda (n, restore_in_term s, restore_in_term t)
137 | C.LetIn (n,s,t) -> C.LetIn (n, restore_in_term s, restore_in_term t)
138 | C.Appl l -> C.Appl (List.map restore_in_term l)
139 | C.Const (uri,exp_named_subst) ->
140 let uri' = recons uri in
141 let exp_named_subst' =
143 (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
145 C.Const (uri',exp_named_subst')
146 | C.MutInd (uri,tyno,exp_named_subst) ->
147 let uri' = recons uri in
148 let exp_named_subst' =
150 (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
152 C.MutInd (uri',tyno,exp_named_subst')
153 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
154 let uri' = recons uri in
155 let exp_named_subst' =
157 (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
159 C.MutConstruct (uri',tyno,consno,exp_named_subst')
160 | C.MutCase (uri,i,outty,t,pl) ->
161 C.MutCase (recons uri, i, restore_in_term outty, restore_in_term t,
162 List.map restore_in_term pl)
164 let len = List.length fl in
167 (fun (name, i, ty, bo) ->
168 (name, i, restore_in_term ty, restore_in_term bo))
173 let len = List.length fl in
176 (fun (name, ty, bo) -> (name, restore_in_term ty, restore_in_term bo))
179 C.CoFix (i, liftedfl)
182 C.Constant (name,bo,ty,params) ->
186 | Some bo -> Some (restore_in_term bo)
188 let ty' = restore_in_term ty in
189 let params' = List.map recons params in
190 C.Constant (name, bo', ty', params')
191 | C.CurrentProof (name,conjs,bo,ty,params) ->
194 (function (i,hyps,ty) ->
198 | Some (name,C.Decl t) ->
199 Some (name,C.Decl (restore_in_term t))
200 | Some (name,C.Def (bo,ty)) ->
204 | Some ty'' -> Some (restore_in_term ty'')
206 Some (name,C.Def (restore_in_term bo, ty'))) hyps,
210 let bo' = restore_in_term bo in
211 let ty' = restore_in_term ty in
212 let params' = List.map recons params in
213 C.CurrentProof (name, conjs', bo', ty', params')
214 | C.Variable (name,bo,ty,params) ->
218 | Some bo -> Some (restore_in_term bo)
220 let ty' = restore_in_term ty in
221 let params' = List.map recons params in
222 C.Variable (name, bo', ty', params')
223 | C.InductiveDefinition (tl,params,paramsno) ->
224 let params' = List.map recons params in
226 List.map (function (name, inductive, ty, constructors) ->
231 (function (name, ty) -> name, restore_in_term ty)
235 C.InductiveDefinition (tl', params', paramsno)
237 let dump_to_channel ?(callback = ignore) oc =
238 HT.iter (fun uri _ -> callback (UriManager.string_of_uri uri)) hashtable;
239 Marshal.to_channel oc hashtable [] ;;
240 let empty () = HT.clear hashtable ;;
241 let restore_from_channel ?(callback = ignore) ic =
242 let restored = Marshal.from_channel ic in
246 callback (UriManager.string_of_uri k);
248 (UriManager.uri_of_string (UriManager.string_of_uri k))
255 let frozen_list = ref [];;
256 let unchecked_list = ref [];;
258 let find_or_add_unchecked uri ~get_object_to_add =
260 List.assq uri !unchecked_list
263 if List.mem_assq uri !frozen_list then
264 raise (CircularDependency (UriManager.string_of_uri uri))
266 if CacheOfCookedObjects.mem uri then
267 raise (AlreadyCooked (UriManager.string_of_uri uri))
269 (* OK, it is not already frozen nor cooked *)
270 let obj = get_object_to_add () in
271 unchecked_list := (uri,obj)::!unchecked_list ;
274 let unchecked_to_frozen uri =
276 let obj = List.assq uri !unchecked_list in
277 unchecked_list := List.remove_assq uri !unchecked_list ;
278 frozen_list := (uri,obj)::!frozen_list
280 Not_found -> raise (CouldNotFreeze (UriManager.string_of_uri uri))
282 let frozen_to_cooked ~uri =
284 let obj = List.assq uri !frozen_list in
285 frozen_list := List.remove_assq uri !frozen_list ;
286 CacheOfCookedObjects.add uri obj
288 Not_found -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
290 let find_cooked ~key:uri = CacheOfCookedObjects.find uri;;
291 let add_cooked ~key:uri obj = CacheOfCookedObjects.add uri obj;;
293 let dump_to_channel = CacheOfCookedObjects.dump_to_channel;;
294 let restore_from_channel = CacheOfCookedObjects.restore_from_channel;;
295 let empty = CacheOfCookedObjects.empty;;
299 let dump_to_channel = Cache.dump_to_channel;;
300 let restore_from_channel = Cache.restore_from_channel;;
301 let empty = Cache.empty;;
303 let find_or_add_unchecked_to_cache uri =
304 Cache.find_or_add_unchecked uri
307 let filename = Http_getter.getxml' uri in
309 match UriManager.bodyuri_of_uri uri with
313 ignore (Http_getter.resolve' bodyuri) ;
314 (* The body exists ==> it is not an axiom *)
315 Some (Http_getter.getxml' bodyuri)
317 Http_getter_types.Unresolvable_URI _ ->
318 (* The body does not exist ==> we consider it an axiom *)
321 CicUniv.directly_to_env_begin ();
322 let obj = CicParser.obj_of_xml filename bodyfilename in
323 CicUniv.directly_to_env_end ();
326 Unix.unlink filename ;
327 match bodyfilename with
328 Some f -> Unix.unlink f
335 (* set_type_checking_info uri *)
336 (* must be called once the type-checking of uri is finished *)
337 (* The object whose uri is uri is unfreezed *)
338 let set_type_checking_info uri =
339 Cache.frozen_to_cooked uri
342 (* is_type_checked uri *)
343 (* CSC: commento falso ed obsoleto *)
344 (* returns a CheckedObj if the term has been type-checked *)
345 (* otherwise it freezes the term for type-checking and returns
347 (* set_type_checking_info must be called to unfreeze the term *)
348 let is_type_checked ?(trust=true) uri =
350 CheckedObj (Cache.find_cooked uri)
353 let obj = find_or_add_unchecked_to_cache uri in
354 Cache.unchecked_to_frozen uri ;
355 if trust && trust_obj uri then
357 CicLogger.log (`Trusting uri) ;
358 set_type_checking_info uri ;
359 CheckedObj (Cache.find_cooked uri)
365 (* get_cooked_obj ~trust uri *)
366 (* returns the object if it is already type-checked or if it can be *)
367 (* trusted (if [trust] = true and the trusting function accepts it) *)
368 (* Otherwise it raises Not_found *)
369 let get_cooked_obj ?(trust=true) uri =
371 Cache.find_cooked uri
373 if trust && trust_obj uri then
375 match is_type_checked uri with
376 CheckedObj obj -> obj
381 prerr_endline ("@@@ OOOOOOOPS: get_cooked_obj(" ^ UriManager.string_of_uri uri ^ ") raises Not_found since the object is not type-checked nor trusted.") ;
387 (* returns the cic object whose uri is uri. If the term is not just in cache, *)
388 (* then it is parsed via CicParser.term_of_xml from the file whose name is *)
389 (* the result of Getter.getxml uri *)
395 find_or_add_unchecked_to_cache uri
398 exception OnlyPutOfInductiveDefinitionsIsAllowed
400 let put_inductive_definition uri obj =
402 Cic.InductiveDefinition _ -> Cache.add_cooked uri obj
403 | _ -> raise OnlyPutOfInductiveDefinitionsIsAllowed
408 ignore (Cache.find_cooked uri);true
409 with Not_found -> false
412 let add_type_checked_term uri obj =
414 Cic.Constant (s,(Some bo),ty,ul) ->
415 Cache.add_cooked ~key:uri obj