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;;
21 exception AlreadyDefined of string Lazy.t;;
23 let cache = NUri.UriHash.create 313;;
24 let history = ref [];;
25 let frozen_list = ref [];;
27 let get_obj = ref (fun _ -> assert false);;
28 let set_get_obj f = get_obj := f;;
33 HExtlib.list_uniq ~eq:(fun (b1,u1) (b2,u2) -> b1=b2 && NUri.eq u1 u2)
34 (List.sort (fun (b1,u1) (b2,u2) ->
35 let res = compare b1 b2 in if res = 0 then NUri.compare u1 u2 else res)
38 let le_constraints = ref [] (* strict,a,b *)
40 let rec le_path_uri avoid strict a b =
41 (not strict && NUri.eq a b) ||
44 NUri.eq y b && not (List.exists (NUri.eq x) avoid) &&
45 le_path_uri (x::avoid) (strict && not strict') a x
49 let leq_path a b = le_path_uri [b] (fst a) (snd a) b;;
51 let universe_leq a b =
53 | a,[(false,b)] -> List.for_all (fun a -> leq_path a b) a
56 (lazy "trying to check if a universe is less or equal than an inferred universe"))
60 | [(false,_)], [(false,_)] -> universe_leq b a && universe_leq a b
62 | [(false,_)],_ -> false
65 (lazy "trying to check if two inferred universes are equal"))
68 let pp_constraint b x y =
69 NUri.name_of_uri x ^ (if b then " < " else " <= ") ^ NUri.name_of_uri y
72 let pp_constraints () =
73 String.concat "\n" (List.map (fun (b,x,y) -> pp_constraint b x y) !le_constraints)
76 let universes = ref [];;
78 let get_universes () = List.map (fun x -> [false,x]) !universes;;
80 let add_constraint strict a b =
82 | [false,a2],[false,b2] ->
83 if not (le_path_uri [] strict a2 b2) then (
84 if le_path_uri [] (not strict) b2 a2 then
85 (raise(BadConstraint(lazy("universe inconsistency adding "^pp_constraint strict a2 b2
86 ^ " to:\n" ^ pp_constraints ()))));
87 universes := a2 :: b2 ::
88 List.filter (fun x -> not (NUri.eq x a2 || NUri.eq x b2)) !universes;
89 le_constraints := (strict,a2,b2) :: !le_constraints);
90 history := (`Constr (strict,a,b))::!history;
91 | _ -> raise (BadConstraint
92 (lazy "trying to add a constraint on an inferred universe"))
99 let bigger_than acc (s1,n1) = List.filter (le_path_uri [] s1 n1) acc in
100 let solutions = List.fold_left bigger_than !universes l in
101 let rec aux = function
104 if List.exists (fun x -> le_path_uri [] true x u) solutions then aux tl
112 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
113 let set_typecheck_obj f =
123 let invalidate_item item =
126 | `Obj (u1,_), `Obj (u2,_) -> NUri.eq u1 u2
127 | `Constr _, `Constr _ -> a=b (* MAKE EFFICIENT *)
130 let rec aux to_be_deleted =
133 | item'::tl when item_eq item item' -> item'::to_be_deleted,tl
134 | item'::tl -> aux (item'::to_be_deleted) tl
136 let to_be_deleted,h = aux [] !history in
140 | `Obj (uri,_) -> NUri.UriHash.remove cache uri
141 | `Constr (strict,[_,u1],[_,u2]) as c ->
142 let w = strict,u1,u2 in
143 if not(List.mem c !history) then
144 le_constraints := List.filter ((<>) w) !le_constraints;
145 | `Constr _ -> assert false
149 exception Propagate of NUri.uri * exn;;
157 let check_and_add_obj ((u,_,_,_,_) as obj) =
158 let saved_frozen_list = !frozen_list in
160 frozen_list := (u,obj)::saved_frozen_list;
162 frozen_list := saved_frozen_list;
163 let obj' = `WellTyped obj in
164 NUri.UriHash.add cache u obj';
165 history := (`Obj (u,obj))::!history;
169 frozen_list := saved_frozen_list;
171 | Propagate (u',old_exn) as e' ->
172 frozen_list := saved_frozen_list;
173 let exn = `Exn (BadDependency (lazy (NUri.string_of_uri u ^
174 " depends (recursively) on " ^ NUri.string_of_uri u' ^
175 " which is not well-typed"),
176 match old_exn with BadDependency (_,e) -> e | _ -> old_exn)) in
177 NUri.UriHash.add cache u exn;
178 history := (`Obj (u,obj))::!history;
179 if saved_frozen_list = [] then
184 frozen_list := saved_frozen_list;
186 NUri.UriHash.add cache u exn;
187 history := (`Obj (u,obj))::!history;
188 if saved_frozen_list = [] then
191 raise (Propagate (u,e))
194 let get_checked_obj u =
195 if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
197 raise (CircularDependency (lazy (NUri.string_of_uri u)))
199 try NUri.UriHash.find cache u
200 with Not_found -> check_and_add_obj (!get_obj u)
203 let get_checked_obj u = to_exn get_checked_obj u;;
205 let check_and_add_obj ((u,_,_,_,_) as obj) =
206 if NUri.UriHash.mem cache u then
207 raise (AlreadyDefined (lazy (NUri.string_of_uri u)))
209 ignore (to_exn check_and_add_obj obj)
212 let get_checked_decl = function
213 | Ref.Ref (uri, Ref.Decl) ->
214 (match get_checked_obj uri with
215 | _,height,_,_, C.Constant (rlv,name,None,ty,att) ->
216 rlv,name,ty,att,height
217 | _,_,_,_, C.Constant (_,_,Some _,_,_) ->
218 prerr_endline "get_checked_decl on a definition"; assert false
219 | _ -> prerr_endline "get_checked_decl on a non decl 2"; assert false)
220 | _ -> prerr_endline "get_checked_decl on a non decl"; assert false
223 let get_checked_def = function
224 | Ref.Ref (uri, Ref.Def _) ->
225 (match get_checked_obj uri with
226 | _,height,_,_, C.Constant (rlv,name,Some bo,ty,att) ->
227 rlv,name,bo,ty,att,height
228 | _,_,_,_, C.Constant (_,_,None,_,_) ->
229 prerr_endline "get_checked_def on an axiom"; assert false
230 | _ -> prerr_endline "get_checked_def on a non def 2"; assert false)
231 | _ -> prerr_endline "get_checked_def on a non def"; assert false
234 let get_checked_indtys = function
235 | Ref.Ref (uri, (Ref.Ind (_,n,_)|Ref.Con (n,_,_))) ->
236 (match get_checked_obj uri with
237 | _,_,_,_, C.Inductive (inductive,leftno,tys,att) ->
238 inductive,leftno,tys,att,n
239 | _ -> prerr_endline "get_checked_indtys on a non ind 2"; assert false)
240 | _ -> prerr_endline "get_checked_indtys on a non ind"; assert false
243 let get_checked_fixes_or_cofixes = function
244 | Ref.Ref (uri, (Ref.Fix _|Ref.CoFix _))->
245 (match get_checked_obj uri with
246 | _,height,_,_, C.Fixpoint (_,funcs,att) ->
248 | _ ->prerr_endline "get_checked_(co)fix on a non (co)fix 2";assert false)
249 | _ -> prerr_endline "get_checked_(co)fix on a non (co)fix"; assert false
252 let get_relevance (Ref.Ref (_, infos) as r) =
254 Ref.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
255 | Ref.Decl -> let res,_,_,_,_ = get_checked_decl r in res
257 let _,_,tl,_,n = get_checked_indtys r in
258 let res,_,_,_ = List.nth tl n in
261 let _,_,tl,_,n = get_checked_indtys r in
262 let _,_,_,cl = List.nth tl n in
263 let res,_,_ = List.nth cl (i - 1) in
265 | Ref.Fix (fixno,_,_)
267 let fl,_,_ = get_checked_fixes_or_cofixes r in
268 let res,_,_,_,_ = List.nth fl fixno in
274 assert (!frozen_list = []);
275 NUri.UriHash.clear cache