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 cache = NUri.UriHash.create 313;;
23 let history = ref [];;
24 let frozen_list = ref [];;
26 let get_obj = ref (fun _ -> assert false);;
27 let set_get_obj f = get_obj := f;;
32 HExtlib.list_uniq ~eq:(fun (b1,u1) (b2,u2) -> b1=b2 && NUri.eq u1 u2)
33 (List.sort (fun (b1,u1) (b2,u2) ->
34 let res = compare b1 b2 in if res = 0 then NUri.compare u1 u2 else res)
37 let le_constraints = ref [] (* strict,a,b *)
39 let rec le_path_uri avoid strict a b =
40 (not strict && NUri.eq a b) ||
43 NUri.eq y b && not (List.exists (NUri.eq x) avoid) &&
44 le_path_uri (x::avoid) (strict && not strict') a x
48 let leq_path a b = le_path_uri [b] (fst a) (snd a) b;;
50 let universe_leq a b =
52 | a,[(false,b)] -> List.for_all (fun a -> leq_path a b) a
55 (lazy "trying to check if a universe is less or equal than an inferred universe"))
59 | [(false,_)], [(false,_)] -> universe_leq b a && universe_leq a b
61 | [(false,_)],_ -> false
64 (lazy "trying to check if two inferred universes are equal"))
67 let pp_constraint b x y =
68 NUri.name_of_uri x ^ (if b then " < " else " <= ") ^ NUri.name_of_uri y
71 let pp_constraints () =
72 String.concat "\n" (List.map (fun (b,x,y) -> pp_constraint b x y) !le_constraints)
75 let universes = ref [];;
77 let add_constraint strict a b =
79 | [false,a2],[false,b2] ->
80 if not (le_path_uri [] strict a2 b2) then (
81 if le_path_uri [] (not strict) b2 a2 then
82 (raise(BadConstraint(lazy("universe inconsistency adding "^pp_constraint strict a2 b2
83 ^ " to:\n" ^ pp_constraints ()))));
84 universes := a2 :: b2 ::
85 List.filter (fun x -> not (NUri.eq x a2 || NUri.eq x b2)) !universes;
86 le_constraints := (strict,a2,b2) :: !le_constraints);
87 history := (`Constr (strict,a,b))::!history;
88 | _ -> raise (BadConstraint
89 (lazy "trying to add a constraint on an inferred universe"))
96 let bigger_than acc (s1,n1) = List.filter (le_path_uri [] s1 n1) acc in
97 let solutions = List.fold_left bigger_than !universes l in
98 let rec aux = function
101 if List.exists (fun x -> le_path_uri [] true x u) solutions then aux tl
109 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
110 let set_typecheck_obj f =
120 let invalidate_item item =
123 | `Obj (u1,_), `Obj (u2,_) -> NUri.eq u1 u2
124 | `Constr _, `Constr _ -> a=b (* MAKE EFFICIENT *)
127 let rec aux to_be_deleted =
130 | item'::tl when item_eq item item' -> item'::to_be_deleted,tl
131 | item'::tl -> aux (item'::to_be_deleted) tl
133 let to_be_deleted,h = aux [] !history in
137 | `Obj (uri,_) -> NUri.UriHash.remove cache uri
138 | `Constr (strict,[_,u1],[_,u2]) as c ->
139 let w = strict,u1,u2 in
140 if not(List.mem c !history) then
141 le_constraints := List.filter ((<>) w) !le_constraints;
142 | `Constr _ -> assert false
146 exception Propagate of NUri.uri * exn;;
154 let check_and_add_obj ((u,_,_,_,_) as obj) =
155 let saved_frozen_list = !frozen_list in
157 frozen_list := (u,obj)::saved_frozen_list;
159 frozen_list := saved_frozen_list;
160 let obj' = `WellTyped obj in
161 NUri.UriHash.add cache u obj';
162 history := (`Obj (u,obj))::!history;
166 frozen_list := saved_frozen_list;
168 | Propagate (u',old_exn) as e' ->
169 frozen_list := saved_frozen_list;
170 let exn = `Exn (BadDependency (lazy (NUri.string_of_uri u ^
171 " depends (recursively) on " ^ NUri.string_of_uri u' ^
172 " which is not well-typed"),
173 match old_exn with BadDependency (_,e) -> e | _ -> old_exn)) in
174 NUri.UriHash.add cache u exn;
175 history := (`Obj (u,obj))::!history;
176 if saved_frozen_list = [] then
181 frozen_list := saved_frozen_list;
183 NUri.UriHash.add cache u exn;
184 history := (`Obj (u,obj))::!history;
185 if saved_frozen_list = [] then
188 raise (Propagate (u,e))
191 let get_checked_obj u =
192 if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
194 raise (CircularDependency (lazy (NUri.string_of_uri u)))
196 try NUri.UriHash.find cache u
197 with Not_found -> check_and_add_obj (!get_obj u)
200 let get_checked_obj u = to_exn get_checked_obj u;;
202 let check_and_add_obj obj = ignore (to_exn check_and_add_obj obj);;
204 let get_checked_decl = function
205 | Ref.Ref (uri, Ref.Decl) ->
206 (match get_checked_obj uri with
207 | _,height,_,_, C.Constant (rlv,name,None,ty,att) ->
208 rlv,name,ty,att,height
209 | _,_,_,_, C.Constant (_,_,Some _,_,_) ->
210 prerr_endline "get_checked_decl on a definition"; assert false
211 | _ -> prerr_endline "get_checked_decl on a non decl 2"; assert false)
212 | _ -> prerr_endline "get_checked_decl on a non decl"; assert false
215 let get_checked_def = function
216 | Ref.Ref (uri, Ref.Def _) ->
217 (match get_checked_obj uri with
218 | _,height,_,_, C.Constant (rlv,name,Some bo,ty,att) ->
219 rlv,name,bo,ty,att,height
220 | _,_,_,_, C.Constant (_,_,None,_,_) ->
221 prerr_endline "get_checked_def on an axiom"; assert false
222 | _ -> prerr_endline "get_checked_def on a non def 2"; assert false)
223 | _ -> prerr_endline "get_checked_def on a non def"; assert false
226 let get_checked_indtys = function
227 | Ref.Ref (uri, (Ref.Ind (_,n,_)|Ref.Con (n,_,_))) ->
228 (match get_checked_obj uri with
229 | _,_,_,_, C.Inductive (inductive,leftno,tys,att) ->
230 inductive,leftno,tys,att,n
231 | _ -> prerr_endline "get_checked_indtys on a non ind 2"; assert false)
232 | _ -> prerr_endline "get_checked_indtys on a non ind"; assert false
235 let get_checked_fixes_or_cofixes = function
236 | Ref.Ref (uri, (Ref.Fix _|Ref.CoFix _))->
237 (match get_checked_obj uri with
238 | _,height,_,_, C.Fixpoint (_,funcs,att) ->
240 | _ ->prerr_endline "get_checked_(co)fix on a non (co)fix 2";assert false)
241 | _ -> prerr_endline "get_checked_(co)fix on a non (co)fix"; assert false
244 let get_relevance (Ref.Ref (_, infos) as r) =
246 Ref.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
247 | Ref.Decl -> let res,_,_,_,_ = get_checked_decl r in res
249 let _,_,tl,_,n = get_checked_indtys r in
250 let res,_,_,_ = List.nth tl n in
253 let _,_,tl,_,n = get_checked_indtys r in
254 let _,_,_,cl = List.nth tl n in
255 let res,_,_ = List.nth cl (i - 1) in
257 | Ref.Fix (fixno,_,_)
259 let fl,_,_ = get_checked_fixes_or_cofixes r in
260 let res,_,_,_,_ = List.nth fl fixno in
266 assert (!frozen_list = []);
267 NUri.UriHash.clear cache