]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicEnvironment.ml
* Major code cleanup.
[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 obj = CicParser.obj_of_xml filename uri in
91        let output = Unchecked obj in
92         HashTable.add hashtable (uri,0) output ;
93         output
94 ;;
95
96 let is_annotation_uri uri =
97  Str.string_match (Str.regexp ".*\.ann$") (UriManager.string_of_uri uri) 0
98 ;;
99
100 (* get_obj uri                                                                *)
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                                            *)
104 let get_obj uri =
105  match get_obj_and_type_checking_info uri 0 with
106     Unchecked obj -> obj
107   | Frozen    obj -> obj
108   | Cooked    obj -> obj
109 ;;
110
111 (*CSC Commento falso *)
112 (* get_obj uri                                                               *)
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
117     Unchecked _
118   | Frozen    _ -> raise UncookedObj
119   | Cooked obj -> obj
120 ;;
121
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
130   | Unchecked obj ->
131      HashTable.remove hashtable (uri,0) ;
132      HashTable.add hashtable (uri,0) (Frozen obj) ;
133      UncheckedObj obj
134   | Frozen _ -> raise (CircularDependency (UriManager.string_of_uri uri))
135 ;;
136
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
142     Frozen obj ->
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
150         List.iter
151          (fun (n,cobj) ->
152            for i = !last_cooked_level + 1 to n do
153             HashTable.add hashtable (uri,i) (Cooked !last_cooked_obj)
154            done ;
155            HashTable.add hashtable (uri,n + 1) (Cooked cobj) ;
156            last_cooked_level := n + 1 ;
157            last_cooked_obj := cobj
158          ) cooked_objs ;
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)
161         done
162   | _ -> raise (CouldNotUnfreeze (UriManager.string_of_uri uri))
163 ;;