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_______________________________________________________________ *)
15 module Ref = NReference
17 exception CircularDependency of string Lazy.t;;
18 exception ObjectNotFound of string Lazy.t;;
19 exception BadDependency of string Lazy.t;;
20 exception BadConstraint of string Lazy.t;;
23 let cprop = [false, NUri.uri_of_string ("cic:/matita/pts/CProp.univ")]
25 let le_constraints = ref [] (* strict,a,b *)
27 let rec le_path_uri avoid strict a b =
28 (not strict && NUri.eq a b) ||
31 NUri.eq y b && not (List.exists (NUri.eq x) avoid) &&
32 le_path_uri (x::avoid) (strict && not strict') a x
36 let leq_path a b = le_path_uri [b] (fst a) (snd a) b;;
38 let universe_leq a b =
40 | a,[(false,b)] -> List.for_all (fun a -> leq_path a b) a
43 (lazy "trying to check if a universe is less or equal than an inferred universe"))
45 let universe_eq a b = universe_leq b a && universe_leq a b
47 let add_constraint strict a b =
49 | [false,a2],[false,b2] ->
50 if not (le_path_uri [] strict a2 b2) then (
51 if le_path_uri [] (not strict) b2 a2 then
52 (raise (BadConstraint (lazy "universe inconsistency")));
53 le_constraints := (strict,a2,b2) :: !le_constraints)
54 | _ -> raise (BadConstraint
55 (lazy "trying to add a constraint on an inferred universe"))
58 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
59 let set_typecheck_obj f =
69 let cache = NUri.UriHash.create 313;;
70 let frozen_list = ref [];;
72 exception Propagate of NUri.uri * exn;;
74 let get_checked_obj u =
75 if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
77 raise (CircularDependency (lazy (NUri.string_of_uri u)))
80 try NUri.UriHash.find cache u
83 let saved_frozen_list = !frozen_list in
86 try NCicLibrary.get_obj u
88 NCicLibrary.ObjectNotFound m -> raise (ObjectNotFound m)
90 frozen_list := (u,obj)::saved_frozen_list;
92 frozen_list := saved_frozen_list;
93 let obj = `WellTyped obj in
94 NUri.UriHash.add cache u obj;
98 frozen_list := saved_frozen_list;
100 | Propagate (u',_) as e' ->
101 frozen_list := saved_frozen_list;
102 let exn = `Exn (BadDependency (lazy (NUri.string_of_uri u ^
103 " depends (recursively) on " ^ NUri.string_of_uri u' ^
104 " which is not well-typed"))) in
105 NUri.UriHash.add cache u exn;
106 if saved_frozen_list = [] then
111 frozen_list := saved_frozen_list;
113 NUri.UriHash.add cache u exn;
114 if saved_frozen_list = [] then
117 raise (Propagate (u,e))
124 let get_checked_decl = function
125 | Ref.Ref (uri, Ref.Decl) ->
126 (match get_checked_obj uri with
127 | _,height,_,_, C.Constant (rlv,name,None,ty,att) ->
128 rlv,name,ty,att,height
129 | _,_,_,_, C.Constant (_,_,Some _,_,_) ->
130 prerr_endline "get_checked_decl on a definition"; assert false
131 | _ -> prerr_endline "get_checked_decl on a non decl 2"; assert false)
132 | _ -> prerr_endline "get_checked_decl on a non decl"; assert false
135 let get_checked_def = function
136 | Ref.Ref (uri, Ref.Def _) ->
137 (match get_checked_obj uri with
138 | _,height,_,_, C.Constant (rlv,name,Some bo,ty,att) ->
139 rlv,name,bo,ty,att,height
140 | _,_,_,_, C.Constant (_,_,None,_,_) ->
141 prerr_endline "get_checked_def on an axiom"; assert false
142 | _ -> prerr_endline "get_checked_def on a non def 2"; assert false)
143 | _ -> prerr_endline "get_checked_def on a non def"; assert false
146 let get_checked_indtys = function
147 | Ref.Ref (uri, (Ref.Ind (_,n,_)|Ref.Con (n,_,_))) ->
148 (match get_checked_obj uri with
149 | _,_,_,_, C.Inductive (inductive,leftno,tys,att) ->
150 inductive,leftno,tys,att,n
151 | _ -> prerr_endline "get_checked_indtys on a non ind 2"; assert false)
152 | _ -> prerr_endline "get_checked_indtys on a non ind"; assert false
155 let get_checked_fixes_or_cofixes = function
156 | Ref.Ref (uri, (Ref.Fix (fixno,_,_)|Ref.CoFix fixno))->
157 (match get_checked_obj uri with
158 | _,height,_,_, C.Fixpoint (_,funcs,att) ->
160 | _ ->prerr_endline "get_checked_(co)fix on a non (co)fix 2";assert false)
161 | r -> prerr_endline ("get_checked_(co)fix on " ^ Ref.string_of_reference r); assert false
164 let get_relevance (Ref.Ref (_, infos) as r) =
166 Ref.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
167 | Ref.Decl -> let res,_,_,_,_ = get_checked_decl r in res
169 let _,_,tl,_,n = get_checked_indtys r in
170 let res,_,_,_ = List.nth tl n in
173 let _,_,tl,_,n = get_checked_indtys r in
174 let _,_,_,cl = List.nth tl n in
175 let res,_,_ = List.nth cl (i - 1) in
177 | Ref.Fix (fixno,_,_)
179 let fl,_,_ = get_checked_fixes_or_cofixes r in
180 let res,_,_,_,_ = List.nth fl fixno in
186 assert (!frozen_list = []);
187 NUri.UriHash.clear cache