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 * exn;;
20 exception BadConstraint of string Lazy.t;;
22 let get_obj = ref (fun _ -> assert false);;
23 let set_get_obj f = get_obj := f;;
28 HExtlib.list_uniq ~eq:(fun (b1,u1) (b2,u2) -> b1=b2 && NUri.eq u1 u2)
29 (List.sort (fun (b1,u1) (b2,u2) ->
30 let res = compare b1 b2 in if res = 0 then NUri.compare u1 u2 else res)
33 let le_constraints = ref [] (* strict,a,b *)
35 let rec le_path_uri avoid strict a b =
36 (not strict && NUri.eq a b) ||
39 NUri.eq y b && not (List.exists (NUri.eq x) avoid) &&
40 le_path_uri (x::avoid) (strict && not strict') a x
44 let leq_path a b = le_path_uri [b] (fst a) (snd a) b;;
46 let universe_leq a b =
48 | a,[(false,b)] -> List.for_all (fun a -> leq_path a b) a
51 (lazy "trying to check if a universe is less or equal than an inferred universe"))
55 | [(false,_)], [(false,_)] -> universe_leq b a && universe_leq a b
57 | [(false,_)],_ -> false
60 (lazy "trying to check if two inferred universes are equal"))
63 let pp_constraint b x y =
64 NUri.name_of_uri x ^ (if b then " < " else " <= ") ^ NUri.name_of_uri y
67 let pp_constraints () =
68 String.concat "\n" (List.map (fun (b,x,y) -> pp_constraint b x y) !le_constraints)
71 let universes = ref [];;
73 let add_constraint strict a b =
75 | [false,a2],[false,b2] ->
76 if not (le_path_uri [] strict a2 b2) then (
77 if le_path_uri [] (not strict) b2 a2 then
78 (raise(BadConstraint(lazy("universe inconsistency adding "^pp_constraint strict a2 b2
79 ^ " to:\n" ^ pp_constraints ()))));
80 universes := a2 :: b2 ::
81 List.filter (fun x -> not (NUri.eq x a2 || NUri.eq x b2)) !universes;
82 le_constraints := (strict,a2,b2) :: !le_constraints)
83 | _ -> raise (BadConstraint
84 (lazy "trying to add a constraint on an inferred universe"))
91 let bigger_than acc (s1,n1) = List.filter (le_path_uri [] s1 n1) acc in
92 let solutions = List.fold_left bigger_than !universes l in
93 let rec aux = function
96 if List.exists (fun x -> le_path_uri [] true x u) solutions then aux tl
104 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
105 let set_typecheck_obj f =
115 let cache = NUri.UriHash.create 313;;
116 let frozen_list = ref [];;
118 exception Propagate of NUri.uri * exn;;
120 let get_checked_obj u =
121 if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
123 raise (CircularDependency (lazy (NUri.string_of_uri u)))
126 try NUri.UriHash.find cache u
129 let saved_frozen_list = !frozen_list in
131 let obj = !get_obj u in
132 frozen_list := (u,obj)::saved_frozen_list;
134 frozen_list := saved_frozen_list;
135 let obj = `WellTyped obj in
136 NUri.UriHash.add cache u obj;
140 frozen_list := saved_frozen_list;
142 | Propagate (u',old_exn) as e' ->
143 frozen_list := saved_frozen_list;
144 let exn = `Exn (BadDependency (lazy (NUri.string_of_uri u ^
145 " depends (recursively) on " ^ NUri.string_of_uri u' ^
146 " which is not well-typed"),
147 match old_exn with BadDependency (_,e) -> e | _ -> old_exn)) in
148 NUri.UriHash.add cache u exn;
149 if saved_frozen_list = [] then
154 frozen_list := saved_frozen_list;
156 NUri.UriHash.add cache u exn;
157 if saved_frozen_list = [] then
160 raise (Propagate (u,e))
167 let get_checked_decl = function
168 | Ref.Ref (uri, Ref.Decl) ->
169 (match get_checked_obj uri with
170 | _,height,_,_, C.Constant (rlv,name,None,ty,att) ->
171 rlv,name,ty,att,height
172 | _,_,_,_, C.Constant (_,_,Some _,_,_) ->
173 prerr_endline "get_checked_decl on a definition"; assert false
174 | _ -> prerr_endline "get_checked_decl on a non decl 2"; assert false)
175 | _ -> prerr_endline "get_checked_decl on a non decl"; assert false
178 let get_checked_def = function
179 | Ref.Ref (uri, Ref.Def _) ->
180 (match get_checked_obj uri with
181 | _,height,_,_, C.Constant (rlv,name,Some bo,ty,att) ->
182 rlv,name,bo,ty,att,height
183 | _,_,_,_, C.Constant (_,_,None,_,_) ->
184 prerr_endline "get_checked_def on an axiom"; assert false
185 | _ -> prerr_endline "get_checked_def on a non def 2"; assert false)
186 | _ -> prerr_endline "get_checked_def on a non def"; assert false
189 let get_checked_indtys = function
190 | Ref.Ref (uri, (Ref.Ind (_,n,_)|Ref.Con (n,_,_))) ->
191 (match get_checked_obj uri with
192 | _,_,_,_, C.Inductive (inductive,leftno,tys,att) ->
193 inductive,leftno,tys,att,n
194 | _ -> prerr_endline "get_checked_indtys on a non ind 2"; assert false)
195 | _ -> prerr_endline "get_checked_indtys on a non ind"; assert false
198 let get_checked_fixes_or_cofixes = function
199 | Ref.Ref (uri, (Ref.Fix _|Ref.CoFix _))->
200 (match get_checked_obj uri with
201 | _,height,_,_, C.Fixpoint (_,funcs,att) ->
203 | _ ->prerr_endline "get_checked_(co)fix on a non (co)fix 2";assert false)
204 | _ -> prerr_endline "get_checked_(co)fix on a non (co)fix"; assert false
207 let get_relevance (Ref.Ref (_, infos) as r) =
209 Ref.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
210 | Ref.Decl -> let res,_,_,_,_ = get_checked_decl r in res
212 let _,_,tl,_,n = get_checked_indtys r in
213 let res,_,_,_ = List.nth tl n in
216 let _,_,tl,_,n = get_checked_indtys r in
217 let _,_,_,cl = List.nth tl n in
218 let res,_,_ = List.nth cl (i - 1) in
220 | Ref.Fix (fixno,_,_)
222 let fl,_,_ = get_checked_fixes_or_cofixes r in
223 let res,_,_,_,_ = List.nth fl fixno in
229 assert (!frozen_list = []);
230 NUri.UriHash.clear cache