]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicEnvironment.ml
HELM OCaml libraries with findlib support.
[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 raise e = print_endline "***" ; flush stdout ; print_endline (Printexc.to_string e) ; flush stdout ; raise e;;
39
40 (*CSC: forse i due seguenti tipi sono da unificare? *)
41 type cooked_obj =
42    Cooked of Cic.obj
43  | Frozen of Cic.obj
44  | Unchecked of Cic.obj
45 type type_checked_obj =
46    CheckedObj of Cic.obj     (* cooked obj *)
47  | UncheckedObj of Cic.obj   (* uncooked obj *)
48 ;;
49
50 exception NoFunctionProvided;;
51
52 let cook_obj = ref (fun obj uri -> raise NoFunctionProvided);;
53
54 let set_cooking_function foo =
55  cook_obj := foo
56 ;;
57
58 exception CircularDependency of string;;
59 exception CouldNotUnfreeze of string;;
60 exception Impossible;;
61 exception UncookedObj;;
62
63 module HashedType =
64  struct
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
68  end
69 ;;
70
71 (* Hashtable that uses == instead of = for testing equality *)
72 module HashTable = Hashtbl.Make(HashedType);;
73
74 let hashtable = HashTable.create 271;;
75
76 (* n is the number of time that the object must be cooked *)
77 let get_obj_and_type_checking_info uri n =
78  try
79    HashTable.find hashtable (uri,n)
80  with
81   Not_found -> 
82    try
83     match HashTable.find hashtable (uri,0) with
84         Cooked _
85       | Frozen _ -> raise Impossible
86       | Unchecked _ as t -> t
87    with
88     Not_found ->
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 ;
94          output
95 ;;
96
97 (* DANGEROUS!!!                                *)
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 =
104  let newobj =
105   match newobj with
106      Some newobj' -> Unchecked newobj'
107    | None         -> Frozen (Cic.Variable ("frozen-dummy", None, Cic.Implicit))
108  in
109   HashTable.remove hashtable (uri,0) ;
110   HashTable.add hashtable (uri,0) newobj
111 ;;
112
113 let is_annotation_uri uri =
114  Str.string_match (Str.regexp ".*\.ann$") (UriManager.string_of_uri uri) 0
115 ;;
116
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 ;
128 *) assert false ;
129     try
130       (annobj, ids_to_targets, HashTable.find hashtable (uri,0))
131     with
132      Not_found -> 
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)
137 ;;
138
139
140 (* get_obj uri                                                                *)
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                                            *)
144 let get_obj uri =
145  match get_obj_and_type_checking_info uri 0 with
146     Unchecked obj -> obj
147   | Frozen    obj -> obj
148   | Cooked    obj -> obj
149 ;;
150
151 (* get_annobj uri                                                             *)
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.    *)
154 let get_annobj uri =
155  let (ann, ids_to_targets, deann) = get_annobj_and_type_checking_info uri in
156   let deannobj =
157    match deann with
158       Unchecked obj -> obj
159     | Frozen    _   -> raise (CircularDependency (UriManager.string_of_uri uri))
160     | Cooked    obj -> obj
161   in
162    (ann, ids_to_targets, deannobj)
163 ;;
164
165 (*CSC Commento falso *)
166 (* get_obj uri                                                               *)
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
171     Unchecked _
172   | Frozen    _ -> raise UncookedObj
173   | Cooked obj -> obj
174 ;;
175
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
184   | Unchecked obj ->
185      HashTable.remove hashtable (uri,0) ;
186      HashTable.add hashtable (uri,0) (Frozen obj) ;
187      UncheckedObj obj
188   | Frozen _ -> raise (CircularDependency (UriManager.string_of_uri uri))
189 ;;
190
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
196     Frozen obj ->
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
204         List.iter
205          (fun (n,cobj) ->
206            for i = !last_cooked_level + 1 to n do
207             HashTable.add hashtable (uri,i) (Cooked !last_cooked_obj)
208            done ;
209            HashTable.add hashtable (uri,n + 1) (Cooked cobj) ;
210            last_cooked_level := n + 1 ;
211            last_cooked_obj := cobj
212          ) cooked_objs ;
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)
215         done
216   | _ -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
217 ;;