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 (*****************************************************************************)
39 (* ************************************************************************** *
40 CicEnvironment SETTINGS (trust and clean_tmp)
41 * ************************************************************************** *)
43 let cleanup_tmp = true;;
44 let trust = ref (fun _ -> true);;
45 let set_trust f = trust := f
46 let trust_obj uri = !trust uri
49 (* ************************************************************************** *
51 * ************************************************************************** *)
53 type type_checked_obj =
54 CheckedObj of (Cic.obj * CicUniv.universe_graph) (* cooked obj *)
55 | UncheckedObj of Cic.obj (* uncooked obj to proof-check *)
58 exception AlreadyCooked of string;;
59 exception CircularDependency of string;;
60 exception CouldNotFreeze of string;;
61 exception CouldNotUnfreeze of string;;
62 exception Term_not_found of UriManager.uri;;
65 (* ************************************************************************** *
66 HERE STARTS THE CACHE MODULE
67 * ************************************************************************** *)
69 (* Cache that uses == instead of = for testing equality *)
70 (* Invariant: an object is always in at most one of the *)
71 (* following states: unchecked, frozen and cooked. *)
74 val find_or_add_to_unchecked :
77 (UriManager.uri -> Cic.obj * CicUniv.universe_graph option) ->
78 Cic.obj * CicUniv.universe_graph
80 UriManager.uri -> bool
81 val unchecked_to_frozen :
82 UriManager.uri -> unit
83 val frozen_to_cooked :
84 uri:UriManager.uri -> unit
86 UriManager.uri -> CicUniv.universe_graph -> unit
88 key:UriManager.uri -> Cic.obj * CicUniv.universe_graph
90 key:UriManager.uri -> (Cic.obj * CicUniv.universe_graph) -> unit
91 val remove: UriManager.uri -> unit
92 val dump_to_channel : ?callback:(string -> unit) -> out_channel -> unit
93 val restore_from_channel : ?callback:(string -> unit) -> in_channel -> unit
94 val empty : unit -> unit
95 val is_in_frozen: UriManager.uri -> bool
96 val is_in_unchecked: UriManager.uri -> bool
97 val is_in_cooked: UriManager.uri -> bool
101 (*************************************************************************
103 The cacheOfCookedObjects will contain only objects with a valid universe
104 graph. valid means that not None (used if there is no universe file
105 in the universe generation phase).
106 **************************************************************************)
108 (* DATA: the data structure that implements the CACHE *)
111 type t = UriManager.uri
112 let equal = UriManager.eq
113 let hash = Hashtbl.hash
117 module HT = Hashtbl.Make(HashedType);;
119 let cacheOfCookedObjects = HT.create 1009;;
121 (* DATA: The parking lists
122 * the lists elements are (uri * (obj * universe_graph option))
123 * ( u, ( o, None )) means that the object has no universes file, this
124 * should happen only in the universe generation phase.
125 * FIXME: if the universe generation is integrated in the library
126 * exportation phase, the 'option' MUST be removed.
127 * ( u, ( o, Some g)) means that the object has a universes file,
131 (* frozen is used to detect circular dependency. *)
132 let frozen_list = ref [];;
133 (* unchecked is used to store objects just fetched, nothing more. *)
134 let unchecked_list = ref [];;
136 (* FIXED: should be ok even if not touched *)
137 (* used to hash cons uris on restore to grant URI structure unicity *)
139 let module C = Cic in
141 UriManager.uri_of_string (UriManager.string_of_uri uri)
143 let rec restore_in_term =
146 | C.Var (uri,exp_named_subst) ->
147 let uri' = recons uri in
148 let exp_named_subst' =
150 (function (uri,t) ->(recons uri,restore_in_term t))
153 C.Var (uri',exp_named_subst')
159 | Some t -> Some (restore_in_term t)
164 | C.Implicit _ as t -> t
165 | C.Cast (te,ty) -> C.Cast (restore_in_term te, restore_in_term ty)
166 | C.Prod (n,s,t) -> C.Prod (n, restore_in_term s, restore_in_term t)
167 | C.Lambda (n,s,t) -> C.Lambda (n, restore_in_term s, restore_in_term t)
168 | C.LetIn (n,s,t) -> C.LetIn (n, restore_in_term s, restore_in_term t)
169 | C.Appl l -> C.Appl (List.map restore_in_term l)
170 | C.Const (uri,exp_named_subst) ->
171 let uri' = recons uri in
172 let exp_named_subst' =
174 (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
176 C.Const (uri',exp_named_subst')
177 | C.MutInd (uri,tyno,exp_named_subst) ->
178 let uri' = recons uri in
179 let exp_named_subst' =
181 (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
183 C.MutInd (uri',tyno,exp_named_subst')
184 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
185 let uri' = recons uri in
186 let exp_named_subst' =
188 (function (uri,t) -> (recons uri,restore_in_term t)) exp_named_subst
190 C.MutConstruct (uri',tyno,consno,exp_named_subst')
191 | C.MutCase (uri,i,outty,t,pl) ->
192 C.MutCase (recons uri, i, restore_in_term outty, restore_in_term t,
193 List.map restore_in_term pl)
195 let len = List.length fl in
198 (fun (name, i, ty, bo) ->
199 (name, i, restore_in_term ty, restore_in_term bo))
204 let len = List.length fl in
207 (fun (name, ty, bo) -> (name, restore_in_term ty, restore_in_term bo))
210 C.CoFix (i, liftedfl)
213 C.Constant (name,bo,ty,params) ->
217 | Some bo -> Some (restore_in_term bo)
219 let ty' = restore_in_term ty in
220 let params' = List.map recons params in
221 C.Constant (name, bo', ty', params')
222 | C.CurrentProof (name,conjs,bo,ty,params) ->
225 (function (i,hyps,ty) ->
229 | Some (name,C.Decl t) ->
230 Some (name,C.Decl (restore_in_term t))
231 | Some (name,C.Def (bo,ty)) ->
235 | Some ty'' -> Some (restore_in_term ty'')
237 Some (name,C.Def (restore_in_term bo, ty'))) hyps,
241 let bo' = restore_in_term bo in
242 let ty' = restore_in_term ty in
243 let params' = List.map recons params in
244 C.CurrentProof (name, conjs', bo', ty', params')
245 | C.Variable (name,bo,ty,params) ->
249 | Some bo -> Some (restore_in_term bo)
251 let ty' = restore_in_term ty in
252 let params' = List.map recons params in
253 C.Variable (name, bo', ty', params')
254 | C.InductiveDefinition (tl,params,paramsno) ->
255 let params' = List.map recons params in
257 List.map (function (name, inductive, ty, constructors) ->
262 (function (name, ty) -> name, restore_in_term ty)
266 C.InductiveDefinition (tl', params', paramsno)
270 HT.clear cacheOfCookedObjects;
271 unchecked_list := [] ;
275 (* FIX: universe stuff?? *)
276 let dump_to_channel ?(callback = ignore) oc =
277 HT.iter (fun uri _ -> callback (UriManager.string_of_uri uri))
278 cacheOfCookedObjects;
279 Marshal.to_channel oc cacheOfCookedObjects []
282 (* FIX: universes stuff?? *)
283 let restore_from_channel ?(callback = ignore) ic =
284 let restored = Marshal.from_channel ic in
285 (* FIXME: should this empty clean the frozen and unchecked?
286 * if not, the only-one-empty-end-not-3 patch is wrong
291 callback (UriManager.string_of_uri k);
292 HT.add cacheOfCookedObjects
293 (UriManager.uri_of_string (UriManager.string_of_uri k))
294 (***********************************************
295 TSSI: FIXME add channel stuff for universes
296 ************************************************)
297 ((restore_uris v),CicUniv.empty_ugraph))
302 let is_in_frozen uri =
303 List.mem_assoc uri !frozen_list
306 let is_in_unchecked uri =
307 List.mem_assoc uri !unchecked_list
310 let is_in_cooked uri =
311 HT.mem cacheOfCookedObjects uri
315 (*******************************************************************
317 we need, in the universe generation phase, to traverse objects
318 that are not jet committed, so we search them in the frozen list.
319 Only uncommitted objects without a universe file (see the assertion)
320 can be searched with method
321 *******************************************************************)
323 let find_or_add_to_unchecked uri ~get_object_to_add =
325 let o,g = List.assq uri !unchecked_list in
327 (* FIXME: we accept both cases, as at the end of this function
328 * maybe the None universe outside the cache module should be
331 * another thing that should be removed if univ generation phase
332 * and lib exportation are unified.
334 None -> o,CicUniv.empty_ugraph
338 if List.mem_assq uri !frozen_list then
339 (* CIRCULAR DEPENDENCY DETECTED, print the error and raise *)
341 print_endline "\nCircularDependency!\nfrozen list: \n";
344 let su = UriManager.string_of_uri u in
345 let univ = if o = None then "NO_UNIV" else "" in
346 print_endline (su^" "^univ))
348 raise (CircularDependency (UriManager.string_of_uri uri))
351 if HT.mem cacheOfCookedObjects uri then
352 (* DOUBLE COOK DETECTED, raise the exception *)
353 raise (AlreadyCooked (UriManager.string_of_uri uri))
355 (* OK, it is not already frozen nor cooked *)
356 let obj,ugraph = get_object_to_add uri in
359 (* FIXME: not sure it is OK*)
360 None -> CicUniv.empty_ugraph
363 unchecked_list := (uri,(obj,ugraph))::!unchecked_list ;
367 let unchecked_to_frozen uri =
369 let obj,ugraph = List.assq uri !unchecked_list in
370 unchecked_list := List.remove_assq uri !unchecked_list ;
371 frozen_list := (uri,(obj,ugraph))::!frozen_list
373 Not_found -> raise (CouldNotFreeze (UriManager.string_of_uri uri))
377 (************************************************************
379 only object with a valid universe graph can be committed
381 this should disappear if the universe generation phase and the
382 library exportation are unified.
383 *************************************************************)
384 let frozen_to_cooked ~uri =
386 let obj,ugraph = List.assq uri !frozen_list in
389 assert false (* only NON dummy universes can be committed *)
391 frozen_list := List.remove_assq uri !frozen_list ;
392 HT.add cacheOfCookedObjects uri (obj,g)
394 Not_found -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
397 let can_be_cooked uri =
399 let obj,ugraph = List.assq uri !frozen_list in
400 (* FIXME: another thing to remove if univ generation phase and lib
401 * exportation are unified.
410 (* this function injects a real universe graph in a (uri, (obj, None))
411 * element of the frozen list.
413 * FIXME: another thing to remove if univ generation phase and lib
414 * exportation are unified.
416 let hack_univ uri real_ugraph =
418 let o,g = List.assq uri !frozen_list in
421 frozen_list := List.remove_assoc uri !frozen_list;
422 frozen_list := (uri,(o,Some real_ugraph))::!frozen_list;
425 "You are probably hacking an object already hacked or an"^
426 " object that has the universe file but is not"^
432 "You are hacking an object that is not in the"^
433 " frozen_list, this means you are probably generating an"^
434 " universe file for an object that already"^
435 " as an universe file");
439 let find_cooked ~key:uri = HT.find cacheOfCookedObjects uri ;;
441 let add_cooked ~key:uri (obj,ugraph) =
442 HT.add cacheOfCookedObjects uri (obj,ugraph)
447 * an object can be romeved from the cache only if we are not typechecking
448 * something. this means check and frozen must be empty.
451 if (!unchecked_list <> []) || (!frozen_list <> []) then
452 failwith "CicEnvironment.remove while type checking"
454 HT.remove cacheOfCookedObjects uri
460 (* ************************************************************************
461 HERE ENDS THE CACHE MODULE
462 * ************************************************************************ *)
464 (* exported cache functions *)
465 let dump_to_channel = Cache.dump_to_channel;;
466 let restore_from_channel = Cache.restore_from_channel;;
467 let empty = Cache.empty;;
469 let get_object_to_add uri =
470 let filename = Http_getter.getxml' uri in
472 match UriManager.bodyuri_of_uri uri with
476 ignore (Http_getter.resolve' bodyuri) ;
477 (* The body exists ==> it is not an axiom *)
478 Some (Http_getter.getxml' bodyuri)
480 Http_getter_types.Key_not_found _ ->
481 (* The body does not exist ==> we consider it an axiom *)
485 Unix.unlink filename ;
488 match filename_univ with
489 Some f -> Unix.unlink f
494 match bodyfilename with
495 Some f -> Unix.unlink f
499 (* this brakes something :
500 * let _ = CicUniv.restart_numbering () in
504 CicParser.obj_of_xml filename bodyfilename
509 let ugraph,filename_univ =
510 (* FIXME: decomment this when the universes will be part of the library
513 Http_getter.getxml' (
514 UriManager.uri_of_string (
515 (UriManager.string_of_uri uri) ^ ".univ"))
517 (Some (CicUniv.ugraph_of_xml filename_univ),Some filename_univ)
521 "WE HAVE NO UNIVERSE FILE FOR " ^ (UriManager.string_of_uri uri));
525 (**********************************************
526 TASSI: should fail when universes will be ON
527 ***********************************************)
528 (Some CicUniv.empty_ugraph,None)
534 (* this is the function to fetch the object in the unchecked list and
535 * nothing more (except returning it)
537 let find_or_add_to_unchecked uri =
538 Cache.find_or_add_to_unchecked uri ~get_object_to_add
540 (* set_type_checking_info uri *)
541 (* must be called once the type-checking of uri is finished *)
542 (* The object whose uri is uri is unfreezed *)
544 (* the replacement ugraph must be the one returned by the *)
545 (* typechecker, restricted with the CicUnivUtils.clean_and_fill *)
546 let set_type_checking_info ?(replace_ugraph=None) uri =
547 if Cache.can_be_cooked uri && replace_ugraph <> None then
549 "?replace_ugraph must be None if you are not committing an "^
550 "object that has a universe graph associated "^
551 "(can happen only in the fase of universes graphs generation).");
552 if not (Cache.can_be_cooked uri) && replace_ugraph = None then
554 "?replace_ugraph must be (Some ugraph) when committing an object that "^
555 "has no associated universe graph. If this is in make_univ phase you "^
556 "should drop this exception and let univ_make commit thi object with "^
559 match replace_ugraph with
561 | Some g -> Cache.hack_univ uri g
563 Cache.frozen_to_cooked uri
566 (* fetch, unfreeze and commit an uri to the cacheOfCookedObjects and
567 * return the object,ugraph
569 let add_trusted_uri_to_cache uri =
570 let o,u = find_or_add_to_unchecked uri in
571 Cache.unchecked_to_frozen uri;
572 set_type_checking_info uri;
574 Cache.find_cooked uri
575 with Not_found -> assert false
578 (* get the uri, if we trust it will be added to the cacheOfCookedObjects *)
579 let get_cooked_obj ?(trust=true) base_univ uri =
581 (* the object should be in the cacheOfCookedObjects *)
582 let o,u = Cache.find_cooked uri in
583 o,(CicUniv.merge_ugraphs base_univ u)
585 (* this should be an error case, but if we trust the uri... *)
586 if trust && trust_obj uri then
587 (* trusting means that we will fetch cook it on the fly *)
588 let o,u = add_trusted_uri_to_cache uri in
589 o,(CicUniv.merge_ugraphs base_univ u)
591 (* we don't trust the uri, so we fail *)
593 prerr_endline ("CACHE MISS: " ^ (UriManager.string_of_uri uri));
597 (* This has not the old semantic :( but is what the name suggests
599 * let is_type_checked ?(trust=true) uri =
601 * let _ = Cache.find_cooked uri in
605 * trust && trust_obj uri
608 * as the get_cooked_obj but returns a type_checked_obj
611 let is_type_checked ?(trust=true) base_univ uri =
613 let o,u = Cache.find_cooked uri in
614 CheckedObj (o,(CicUniv.merge_ugraphs base_univ u))
616 (* this should return UncheckedObj *)
617 if trust && trust_obj uri then
618 (* trusting means that we will fetch cook it on the fly *)
619 let o,u = add_trusted_uri_to_cache uri in
620 CheckedObj ( o, CicUniv.merge_ugraphs u base_univ )
622 let o,u = find_or_add_to_unchecked uri in
626 (* as the get cooked, but if not present the object is only fetched,
627 * not unfreezed and committed
629 let get_obj base_univ uri =
631 (* the object should be in the cacheOfCookedObjects *)
632 let o,u = Cache.find_cooked uri in
633 o,(CicUniv.merge_ugraphs base_univ u)
635 (* this should be an error case, but if we trust the uri... *)
636 if trust_obj uri then
637 (* trusting we add it to the unchecked list *)
638 let o,u = find_or_add_to_unchecked uri in
639 o,(CicUniv.merge_ugraphs base_univ u)
644 exception OnlyPutOfInductiveDefinitionsIsAllowed
646 let put_inductive_definition uri (obj,ugraph) =
648 Cic.InductiveDefinition _ -> Cache.add_cooked uri (obj,ugraph)
649 | _ -> raise OnlyPutOfInductiveDefinitionsIsAllowed
653 if Cache.is_in_cooked uri then
654 (prerr_endline "TROVATO NELLA CHECKED";true)
656 if Cache.is_in_frozen uri then
657 (prerr_endline "TROVATO NELLA FROZEN";true)
659 if Cache.is_in_unchecked uri then
660 (prerr_endline "TROVATO NELLA UNCHECKED";true)
662 (prerr_endline ("NON TROVATO:" ^ (UriManager.string_of_uri uri) );false)
665 let add_type_checked_term uri (obj,ugraph) =
667 Cic.Constant (s,(Some bo),ty,ul) ->
668 Cache.add_cooked ~key:uri (obj,ugraph)
673 let remove_term = Cache.remove