]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicEnvironment.ml
e9050cd8c1867ac6f0686abc2094facedfabcd9f
[helm.git] / helm / ocaml / cic_proof_checking / cicEnvironment.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 24/01/2000                                 *)
32 (*                                                                            *)
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)         *)
35 (*                                                                            *)
36 (******************************************************************************)
37
38 let cleanup_tmp = true;;
39
40 let trust_obj = function uri -> true;;
41
42 type type_checked_obj =
43    CheckedObj of Cic.obj     (* cooked obj *)
44  | UncheckedObj of Cic.obj   (* uncooked obj to proof-check *)
45 ;;
46
47
48 exception AlreadyCooked of string;;
49 exception CircularDependency of string;;
50 exception CouldNotFreeze of string;;
51 exception CouldNotUnfreeze of string;;
52
53 (* Cache that uses == instead of = for testing equality *)
54 (* Invariant: an object is always in at most one of the *)
55 (* following states: unchecked, frozen and cooked.      *)
56 module Cache :
57   sig
58    val find_or_add_unchecked :
59     UriManager.uri -> get_object_to_add:(unit -> Cic.obj) -> Cic.obj
60    val unchecked_to_frozen : UriManager.uri -> unit
61    val frozen_to_cooked :
62     uri:UriManager.uri -> unit
63    val find_cooked : key:UriManager.uri -> Cic.obj
64   end 
65 =
66   struct
67    module CacheOfCookedObjects :
68     sig
69      val mem  : UriManager.uri -> bool
70      val find : UriManager.uri -> Cic.obj
71      val add  : UriManager.uri -> Cic.obj -> unit
72     end
73    =
74     struct
75      module HashedType =
76       struct
77        type t = UriManager.uri
78        let equal = UriManager.eq
79        let hash = Hashtbl.hash
80       end
81      ;;
82      module HT = Hashtbl.Make(HashedType);;
83      let hashtable = HT.create 1009;;
84      let mem uri =
85       try
86        HT.mem hashtable uri
87       with
88        Not_found -> false
89      ;;
90      let find uri = HT.find hashtable uri
91      ;;
92      let add uri obj =
93       HT.add hashtable uri obj
94      ;;
95     end
96    ;;
97    let frozen_list = ref [];;
98    let unchecked_list = ref [];;
99
100    let find_or_add_unchecked uri ~get_object_to_add =
101     try
102      List.assq uri !unchecked_list
103     with
104      Not_found ->
105       if List.mem_assq uri !frozen_list then
106        raise (CircularDependency (UriManager.string_of_uri uri))
107       else
108        if CacheOfCookedObjects.mem uri then
109         raise (AlreadyCooked (UriManager.string_of_uri uri))
110        else
111         (* OK, it is not already frozen nor cooked *)
112         let obj = get_object_to_add () in
113          unchecked_list := (uri,obj)::!unchecked_list ;
114          obj
115    ;;
116    let unchecked_to_frozen uri =
117     try
118      let obj = List.assq uri !unchecked_list in
119       unchecked_list := List.remove_assq uri !unchecked_list ;
120       frozen_list := (uri,obj)::!frozen_list
121     with
122      Not_found -> raise (CouldNotFreeze (UriManager.string_of_uri uri))
123    ;;
124    let frozen_to_cooked ~uri =
125     try
126      let obj = List.assq uri !frozen_list in
127       frozen_list := List.remove_assq uri !frozen_list ;
128        CacheOfCookedObjects.add uri obj
129     with
130      Not_found -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
131    ;;
132    let find_cooked ~key:uri = CacheOfCookedObjects.find uri;;
133   end
134 ;;
135
136 let find_or_add_unchecked_to_cache uri =
137  Cache.find_or_add_unchecked uri
138   ~get_object_to_add:
139    (function () ->
140      let filename = Getter.getxml uri in
141      let bodyfilename =
142       match UriManager.bodyuri_of_uri uri with
143          None -> None
144        | Some bodyuri ->
145           try
146            ignore (Getter.resolve bodyuri) ;
147            (* The body exists ==> it is not an axiom *)
148            Some (Getter.getxml bodyuri)
149           with
150            Getter.Unresolved ->
151             (* The body does not exist ==> we consider it an axiom *)
152             None
153      in
154       let obj = CicParser.obj_of_xml filename bodyfilename in
155        if cleanup_tmp then
156         begin
157          Unix.unlink filename ;
158          match bodyfilename with
159             Some f -> Unix.unlink f
160           | None -> ()
161         end ;
162        obj
163    )
164 ;;
165
166 (* set_type_checking_info uri                               *)
167 (* must be called once the type-checking of uri is finished *)
168 (* The object whose uri is uri is unfreezed                 *)
169 let set_type_checking_info uri =
170  Cache.frozen_to_cooked uri
171 ;;
172
173 (* is_type_checked uri                                                *)
174 (* CSC: commento falso ed obsoleto *)
175 (* returns a CheckedObj if the term has been type-checked             *)
176 (* otherwise it freezes the term for type-checking and returns
177  it *)
178 (* set_type_checking_info must be called to unfreeze the term         *)
179 let is_type_checked ?(trust=true) uri =
180  try
181   CheckedObj (Cache.find_cooked uri)
182  with
183   Not_found ->
184    let obj = find_or_add_unchecked_to_cache uri in
185     Cache.unchecked_to_frozen uri ;
186     if trust && trust_obj uri then
187      begin
188       Logger.log (`Trusting uri) ;
189       set_type_checking_info uri ;
190       CheckedObj (Cache.find_cooked uri)
191      end
192     else
193      UncheckedObj obj
194 ;;
195
196 (* get_cooked_obj ~trust uri *)
197 (* returns the object if it is already type-checked or if it can be *)
198 (* trusted (if [trust] = true and the trusting function accepts it) *)
199 (* Otherwise it raises Not_found                                    *)
200 let get_cooked_obj ?(trust=true) uri =
201  try
202   Cache.find_cooked uri
203  with Not_found ->
204   if trust && trust_obj uri then
205    begin
206     match is_type_checked uri with
207        CheckedObj obj -> obj
208      | _ -> assert false
209    end
210   else
211    begin
212     prerr_endline ("@@@ OOOOOOOPS: get_cooked_obj(" ^ UriManager.string_of_uri uri ^ ") raises Not_found since the object is not type-checked nor trusted.") ;
213     raise Not_found
214    end
215 ;;
216
217 (* get_obj uri                                                                *)
218 (* returns the cic object whose uri is uri. If the term is not just in cache, *)
219 (* then it is parsed via CicParser.term_of_xml from the file whose name is    *)
220 (* the result of Getter.getxml uri                                            *)
221 let get_obj uri =
222  try
223   get_cooked_obj uri
224  with
225   Not_found ->
226    find_or_add_unchecked_to_cache uri
227 ;;