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;;
24 let le_constraints = ref [] (* strict,a,b *)
26 let rec le_path_uri avoid strict a b =
27 (not strict && NUri.eq a b) ||
30 NUri.eq y b && not (List.exists (NUri.eq x) avoid) &&
31 le_path_uri (x::avoid) (strict && not strict') a x
35 let leq_path a b = le_path_uri [b] (fst a) (snd a) b;;
37 let universe_leq a b =
39 | a,[(false,b)] -> List.for_all (fun a -> leq_path a b) a
42 (lazy "trying to check if a universe is less or equal than an inferred universe"))
44 let universe_eq a b = universe_leq b a && universe_leq a b
46 let pp_constraint b x y =
47 NUri.name_of_uri x ^ (if b then " < " else " <= ") ^ NUri.name_of_uri y
50 let pp_constraints () =
51 String.concat "\n" (List.map (fun (b,x,y) -> pp_constraint b x y) !le_constraints)
54 let add_constraint strict a b =
56 | [false,a2],[false,b2] ->
57 if not (le_path_uri [] strict a2 b2) then (
58 if le_path_uri [] (not strict) b2 a2 then
59 (raise(BadConstraint(lazy("universe inconsistency adding "^pp_constraint strict a2 b2
60 ^ " to:\n" ^ pp_constraints ()))));
61 le_constraints := (strict,a2,b2) :: !le_constraints)
62 | _ -> raise (BadConstraint
63 (lazy "trying to add a constraint on an inferred universe"))
66 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
67 let set_typecheck_obj f =
77 let cache = NUri.UriHash.create 313;;
78 let frozen_list = ref [];;
80 exception Propagate of NUri.uri * exn;;
82 let get_checked_obj u =
83 if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
85 raise (CircularDependency (lazy (NUri.string_of_uri u)))
88 try NUri.UriHash.find cache u
91 let saved_frozen_list = !frozen_list in
94 try NCicLibrary.get_obj u
96 NCicLibrary.ObjectNotFound m -> raise (ObjectNotFound m)
98 frozen_list := (u,obj)::saved_frozen_list;
100 frozen_list := saved_frozen_list;
101 let obj = `WellTyped obj in
102 NUri.UriHash.add cache u obj;
106 frozen_list := saved_frozen_list;
108 | Propagate (u',_) as e' ->
109 frozen_list := saved_frozen_list;
110 let exn = `Exn (BadDependency (lazy (NUri.string_of_uri u ^
111 " depends (recursively) on " ^ NUri.string_of_uri u' ^
112 " which is not well-typed"))) in
113 NUri.UriHash.add cache u exn;
114 if saved_frozen_list = [] then
119 frozen_list := saved_frozen_list;
121 NUri.UriHash.add cache u exn;
122 if saved_frozen_list = [] then
125 raise (Propagate (u,e))
132 let get_checked_decl = function
133 | Ref.Ref (uri, Ref.Decl) ->
134 (match get_checked_obj uri with
135 | _,height,_,_, C.Constant (rlv,name,None,ty,att) ->
136 rlv,name,ty,att,height
137 | _,_,_,_, C.Constant (_,_,Some _,_,_) ->
138 prerr_endline "get_checked_decl on a definition"; assert false
139 | _ -> prerr_endline "get_checked_decl on a non decl 2"; assert false)
140 | _ -> prerr_endline "get_checked_decl on a non decl"; assert false
143 let get_checked_def = function
144 | Ref.Ref (uri, Ref.Def _) ->
145 (match get_checked_obj uri with
146 | _,height,_,_, C.Constant (rlv,name,Some bo,ty,att) ->
147 rlv,name,bo,ty,att,height
148 | _,_,_,_, C.Constant (_,_,None,_,_) ->
149 prerr_endline "get_checked_def on an axiom"; assert false
150 | _ -> prerr_endline "get_checked_def on a non def 2"; assert false)
151 | _ -> prerr_endline "get_checked_def on a non def"; assert false
154 let get_checked_indtys = function
155 | Ref.Ref (uri, (Ref.Ind (_,n,_)|Ref.Con (n,_,_))) ->
156 (match get_checked_obj uri with
157 | _,_,_,_, C.Inductive (inductive,leftno,tys,att) ->
158 inductive,leftno,tys,att,n
159 | _ -> prerr_endline "get_checked_indtys on a non ind 2"; assert false)
160 | _ -> prerr_endline "get_checked_indtys on a non ind"; assert false
163 let get_checked_fixes_or_cofixes = function
164 | Ref.Ref (uri, (Ref.Fix _|Ref.CoFix _))->
165 (match get_checked_obj uri with
166 | _,height,_,_, C.Fixpoint (_,funcs,att) ->
168 | _ ->prerr_endline "get_checked_(co)fix on a non (co)fix 2";assert false)
169 | _ -> prerr_endline "get_checked_(co)fix on a non (co)fix"; assert false
172 let get_relevance (Ref.Ref (_, infos) as r) =
174 Ref.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
175 | Ref.Decl -> let res,_,_,_,_ = get_checked_decl r in res
177 let _,_,tl,_,n = get_checked_indtys r in
178 let res,_,_,_ = List.nth tl n in
181 let _,_,tl,_,n = get_checked_indtys r in
182 let _,_,_,cl = List.nth tl n in
183 let res,_,_ = List.nth cl (i - 1) in
185 | Ref.Fix (fixno,_,_)
187 let fl,_,_ = get_checked_fixes_or_cofixes r in
188 let res,_,_,_,_ = List.nth fl fixno in
194 assert (!frozen_list = []);
195 NUri.UriHash.clear cache