2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
14 exception CircularDependency of string Lazy.t;;
15 exception ObjectNotFound of string Lazy.t;;
16 exception BadDependency of string Lazy.t;;
18 let type0 = [false, NUri.uri_of_string ("cic:/matita/pts/Type.univ")]
20 let u_eq (a1,a2) (b1,b2) = a1=b1 && NUri.eq a2 b2
24 | (false,a2), (true,b2) -> NUri.eq a2 b2
28 let leq_constraints = ref []
31 List.exists (fun (x,y) -> u_eq y b && (u_eq a x || u_lt a x || path a x))
34 let universe_leq a b =
36 | a,[b] -> List.for_all (fun a -> path a b) a
39 let universe_eq a b = universe_leq b a || universe_leq a b
41 let add_lt_constraint a b =
43 | [false,a2 as a],[false,_ as b] ->
44 if path b a then (raise (Failure "universe inconsistency"));
45 leq_constraints := ((true,a2),b) :: !leq_constraints
49 let add_leq_constraint a b =
51 | [false,_ as a],[false,b2 as b] ->
52 if path (true,b2) a then (raise (Failure "universe inconsistency"));
53 leq_constraints := (a,b) :: !leq_constraints
59 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
60 let set_typecheck_obj f =
70 let cache = NUri.UriHash.create 313;;
71 let frozen_list = ref [];;
73 exception Propagate of NUri.uri * exn;;
75 let get_checked_obj u =
76 if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
78 raise (CircularDependency (lazy (NUri.string_of_uri u)))
81 try NUri.UriHash.find cache u
84 let saved_frozen_list = !frozen_list in
87 try NCicLibrary.get_obj u
89 NCicLibrary.ObjectNotFound m -> raise (ObjectNotFound m)
91 frozen_list := (u,obj)::saved_frozen_list;
93 frozen_list := saved_frozen_list;
94 let obj = `WellTyped obj in
95 NUri.UriHash.add cache u obj;
99 frozen_list := saved_frozen_list;
101 | Propagate (u',_) as e' ->
102 frozen_list := saved_frozen_list;
103 let exn = `Exn (BadDependency (lazy (NUri.string_of_uri u ^
104 " depends (recursively) on " ^ NUri.string_of_uri u' ^
105 " which is not well-typed"))) in
106 NUri.UriHash.add cache u exn;
107 if saved_frozen_list = [] then
112 frozen_list := saved_frozen_list;
114 NUri.UriHash.add cache u exn;
115 if saved_frozen_list = [] then
118 raise (Propagate (u,e))
125 let get_checked_decl = function
126 | NReference.Ref (uri, NReference.Decl) ->
127 (match get_checked_obj uri with
128 | _,height,_,_, NCic.Constant (rlv,name,None,ty,att) ->
129 rlv,name,ty,att,height
130 | _,_,_,_, NCic.Constant (_,_,Some _,_,_) ->
131 prerr_endline "get_checked_decl on a definition"; assert false
132 | _ -> prerr_endline "get_checked_decl on a non decl 2"; assert false)
133 | _ -> prerr_endline "get_checked_decl on a non decl"; assert false
136 let get_checked_def = function
137 | NReference.Ref (uri, NReference.Def _) ->
138 (match get_checked_obj uri with
139 | _,height,_,_, NCic.Constant (rlv,name,Some bo,ty,att) ->
140 rlv,name,bo,ty,att,height
141 | _,_,_,_, NCic.Constant (_,_,None,_,_) ->
142 prerr_endline "get_checked_def on an axiom"; assert false
143 | _ -> prerr_endline "get_checked_def on a non def 2"; assert false)
144 | _ -> prerr_endline "get_checked_def on a non def"; assert false
147 let get_checked_indtys = function
148 | NReference.Ref (uri, (NReference.Ind (_,n)|NReference.Con (n,_))) ->
149 (match get_checked_obj uri with
150 | _,_,_,_, NCic.Inductive (inductive,leftno,tys,att) ->
151 inductive,leftno,tys,att,n
152 | _ -> prerr_endline "get_checked_indtys on a non ind 2"; assert false)
153 | _ -> prerr_endline "get_checked_indtys on a non ind"; assert false
156 let get_checked_fixes_or_cofixes = function
157 | NReference.Ref (uri, (NReference.Fix (fixno,_,_)|NReference.CoFix fixno))->
158 (match get_checked_obj uri with
159 | _,height,_,_, NCic.Fixpoint (_,funcs,att) ->
161 | _ ->prerr_endline "get_checked_(co)fix on a non (co)fix 2";assert false)
162 | r -> prerr_endline ("get_checked_(co)fix on " ^ NReference.string_of_reference r); assert false
165 let get_indty_leftno = function
166 | NReference.Ref (uri, NReference.Ind _)
167 | NReference.Ref (uri, NReference.Con _) ->
168 (match get_checked_obj uri with
169 | _,_,_,_, NCic.Inductive (_,left,_,_) -> left
170 | _ ->prerr_endline "get_indty_leftno called on a non ind 2";assert false)
171 | _ -> prerr_endline "get_indty_leftno called on a non indty";assert false
174 let get_relevance (NReference.Ref (_, infos) as r) =
176 NReference.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
177 | NReference.Decl -> let res,_,_,_,_ = get_checked_decl r in res
178 | NReference.Ind _ ->
179 let _,_,tl,_,n = get_checked_indtys r in
180 let res,_,_,_ = List.nth tl n in
182 | NReference.Con (_,i) ->
183 let _,_,tl,_,n = get_checked_indtys r in
184 let _,_,_,cl = List.nth tl n in
185 let res,_,_ = List.nth cl (i - 1) in
187 | NReference.Fix (fixno,_,_)
188 | NReference.CoFix fixno ->
189 let fl,_,_ = get_checked_fixes_or_cofixes r in
190 let res,_,_,_,_ = List.nth fl fixno in
196 assert (!frozen_list = []);
197 NUri.UriHash.clear cache