]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicEnvironment.ml
undo/serialization for universes implemented
[helm.git] / helm / software / components / ng_kernel / nCicEnvironment.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 (* $Id$ *)
13
14 module C = NCic
15 module Ref = NReference
16
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
22 let cache = NUri.UriHash.create 313;;
23 let history = ref [];;
24 let frozen_list = ref [];;
25
26 let get_obj = ref (fun _ -> assert false);;
27 let set_get_obj f = get_obj := f;;
28
29 let type0 = []
30
31 let max l1 l2 =
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)
35       (l1 @ l2))
36
37 let le_constraints = ref [] (* strict,a,b *)
38
39 let rec le_path_uri avoid strict a b =
40  (not strict && NUri.eq a b) ||
41  List.exists
42   (fun (strict',x,y) ->
43      NUri.eq y b && not (List.exists (NUri.eq x) avoid) &&
44        le_path_uri (x::avoid) (strict && not strict') a x
45   ) !le_constraints
46 ;;
47
48 let leq_path a b = le_path_uri [b] (fst a) (snd a) b;;
49
50 let universe_leq a b = 
51   match a, b with
52   | a,[(false,b)] -> List.for_all (fun a -> leq_path a b) a
53   | _,_ ->
54      raise (BadConstraint
55       (lazy "trying to check if a universe is less or equal than an inferred universe"))
56
57 let universe_eq a b = 
58   match a,b with 
59   | [(false,_)], [(false,_)] -> universe_leq b a && universe_leq a b
60   | _, [(false,_)]
61   | [(false,_)],_ -> false
62   | _ ->
63      raise (BadConstraint
64       (lazy "trying to check if two inferred universes are equal"))
65 ;;
66
67 let pp_constraint b x y =  
68   NUri.name_of_uri x ^ (if b then " < " else " <= ") ^ NUri.name_of_uri y
69 ;;
70
71 let pp_constraints () =
72   String.concat "\n" (List.map (fun (b,x,y) -> pp_constraint b x y) !le_constraints)
73 ;;
74
75 let universes = ref [];;
76
77 let add_constraint strict a b = 
78   match a,b with
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"))
90 ;;
91
92 let sup l =
93   match l with
94   | [false,_] -> Some l
95   | l ->
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
99      | [] -> None
100      | u :: tl ->
101          if List.exists (fun x -> le_path_uri [] true x u) solutions then aux tl
102          else Some [false,u]
103    in
104     aux solutions
105 ;;
106
107
108
109 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
110 let set_typecheck_obj f =
111  if !already_set then
112   assert false
113  else
114   begin
115    typecheck_obj := f;
116    already_set := true
117   end
118 ;;
119
120 let invalidate_item item =
121  let item_eq a b = 
122    match a, b with
123    | `Obj (u1,_), `Obj (u2,_) -> NUri.eq u1 u2
124    | `Constr _, `Constr _ -> a=b (* MAKE EFFICIENT *)
125    | _ -> false
126  in
127  let rec aux to_be_deleted =
128   function
129      [] -> assert false
130    | item'::tl when item_eq item item' -> item'::to_be_deleted,tl
131    | item'::tl -> aux (item'::to_be_deleted) tl
132  in
133   let to_be_deleted,h = aux [] !history in
134    history := h;
135    List.iter 
136      (function 
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
143      ) to_be_deleted
144 ;;
145
146 exception Propagate of NUri.uri * exn;;
147
148 let to_exn f x =
149  match f x with
150     `WellTyped o -> o
151   | `Exn e -> raise e
152 ;;
153
154 let check_and_add_obj ((u,_,_,_,_) as obj) =
155  let saved_frozen_list = !frozen_list in
156  try
157    frozen_list := (u,obj)::saved_frozen_list;
158    !typecheck_obj obj;
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;
163    obj'
164  with
165     Sys.Break as e ->
166      frozen_list := saved_frozen_list;
167      raise e
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
177       exn
178      else
179       raise e'
180   | e ->
181      frozen_list := saved_frozen_list;
182      let exn = `Exn e in
183      NUri.UriHash.add cache u exn;
184      history := (`Obj (u,obj))::!history;
185      if saved_frozen_list = [] then
186       exn
187      else
188       raise (Propagate (u,e))
189 ;;
190
191 let get_checked_obj u =
192  if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
193  then
194   raise (CircularDependency (lazy (NUri.string_of_uri u)))
195  else
196   try NUri.UriHash.find cache u
197   with Not_found -> check_and_add_obj (!get_obj u)
198 ;;
199
200 let get_checked_obj u = to_exn get_checked_obj u;;
201
202 let check_and_add_obj obj = ignore (to_exn check_and_add_obj obj);;
203
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
213 ;;
214
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
224 ;;
225
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
233 ;;
234
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) ->
239          funcs, att, height
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
242 ;;
243
244 let get_relevance (Ref.Ref (_, infos) as r) =
245   match infos with
246      Ref.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
247    | Ref.Decl -> let res,_,_,_,_ = get_checked_decl r in res
248    | Ref.Ind _ ->
249        let _,_,tl,_,n = get_checked_indtys r in
250        let res,_,_,_ = List.nth tl n in
251          res
252     | Ref.Con (_,i,_) ->
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
256          res
257     | Ref.Fix (fixno,_,_)
258     | Ref.CoFix fixno ->
259         let fl,_,_ = get_checked_fixes_or_cofixes r in
260         let res,_,_,_,_ = List.nth fl fixno in
261           res
262 ;;
263
264
265 let invalidate _ = 
266   assert (!frozen_list = []);
267   NUri.UriHash.clear cache
268 ;;