]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nCicEnvironment.ml
1) NCicTypechecker.typecheck_obj removed, since it did not add to the
[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 get_obj = ref (fun _ -> assert false);;
23 let set_get_obj f = get_obj := f;;
24
25 let type0 = []
26
27 let max l1 l2 =
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)
31       (l1 @ l2))
32
33 let le_constraints = ref [] (* strict,a,b *)
34
35 let rec le_path_uri avoid strict a b =
36  (not strict && NUri.eq a b) ||
37  List.exists
38   (fun (strict',x,y) ->
39      NUri.eq y b && not (List.exists (NUri.eq x) avoid) &&
40        le_path_uri (x::avoid) (strict && not strict') a x
41   ) !le_constraints
42 ;;
43
44 let leq_path a b = le_path_uri [b] (fst a) (snd a) b;;
45
46 let universe_leq a b = 
47   match a, b with
48   | a,[(false,b)] -> List.for_all (fun a -> leq_path a b) a
49   | _,_ ->
50      raise (BadConstraint
51       (lazy "trying to check if a universe is less or equal than an inferred universe"))
52
53 let universe_eq a b = 
54   match a,b with 
55   | [(false,_)], [(false,_)] -> universe_leq b a && universe_leq a b
56   | _, [(false,_)]
57   | [(false,_)],_ -> false
58   | _ ->
59      raise (BadConstraint
60       (lazy "trying to check if two inferred universes are equal"))
61 ;;
62
63 let pp_constraint b x y =  
64   NUri.name_of_uri x ^ (if b then " < " else " <= ") ^ NUri.name_of_uri y
65 ;;
66
67 let pp_constraints () =
68   String.concat "\n" (List.map (fun (b,x,y) -> pp_constraint b x y) !le_constraints)
69 ;;
70
71 let universes = ref [];;
72
73 let add_constraint strict a b = 
74   match a,b with
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"))
85 ;;
86
87 let sup l =
88   match l with
89   | [false,_] -> Some l
90   | l ->
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
94      | [] -> None
95      | u :: tl ->
96          if List.exists (fun x -> le_path_uri [] true x u) solutions then aux tl
97          else Some [false,u]
98    in
99     aux solutions
100 ;;
101
102
103
104 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
105 let set_typecheck_obj f =
106  if !already_set then
107   assert false
108  else
109   begin
110    typecheck_obj := f;
111    already_set := true
112   end
113 ;;
114
115 let cache = NUri.UriHash.create 313;;
116 let history = ref [];;
117 let frozen_list = ref [];;
118
119 let invalidate_obj uri =
120 prerr_endline ("INVALIDO: " ^ NUri.string_of_uri uri);
121 List.iter (fun uri -> prerr_endline ("INH: " ^ NUri.string_of_uri uri)) !history;
122  let rec aux to_be_deleted =
123   function
124      [] -> assert false
125    | uri'::tl when NUri.eq uri uri' -> uri'::to_be_deleted,tl
126    | uri'::tl -> aux (uri'::to_be_deleted) tl
127  in
128   let to_be_deleted,h = aux [] !history in
129    history := h;
130    List.iter (fun uri -> NUri.UriHash.remove cache uri) to_be_deleted
131 ;;
132
133 exception Propagate of NUri.uri * exn;;
134
135 let to_exn f x =
136  match f x with
137     `WellTyped o -> o
138   | `Exn e -> raise e
139 ;;
140
141 let check_and_add_obj ((u,_,_,_,_) as obj) =
142  let saved_frozen_list = !frozen_list in
143  try
144    frozen_list := (u,obj)::saved_frozen_list;
145    !typecheck_obj obj;
146    frozen_list := saved_frozen_list;
147    let obj = `WellTyped obj in
148    NUri.UriHash.add cache u obj;
149    history := u::!history;
150    obj
151  with
152     Sys.Break as e ->
153      frozen_list := saved_frozen_list;
154      raise e
155   | Propagate (u',old_exn) as e' ->
156      frozen_list := saved_frozen_list;
157      let exn = `Exn (BadDependency (lazy (NUri.string_of_uri u ^
158        " depends (recursively) on " ^ NUri.string_of_uri u' ^
159        " which is not well-typed"), 
160        match old_exn with BadDependency (_,e) -> e | _ -> old_exn)) in
161      NUri.UriHash.add cache u exn;
162      history := u::!history;
163      if saved_frozen_list = [] then
164       exn
165      else
166       raise e'
167   | e ->
168      frozen_list := saved_frozen_list;
169      let exn = `Exn e in
170      NUri.UriHash.add cache u exn;
171      history := u::!history;
172      if saved_frozen_list = [] then
173       exn
174      else
175       raise (Propagate (u,e))
176 ;;
177
178 let get_checked_obj u =
179  if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
180  then
181   raise (CircularDependency (lazy (NUri.string_of_uri u)))
182  else
183   try NUri.UriHash.find cache u
184   with Not_found -> check_and_add_obj (!get_obj u)
185 ;;
186
187 let get_checked_obj u = to_exn get_checked_obj u;;
188
189 let check_and_add_obj obj = ignore (to_exn check_and_add_obj obj);;
190
191 let get_checked_decl = function
192   | Ref.Ref (uri, Ref.Decl) ->
193       (match get_checked_obj uri with
194       | _,height,_,_, C.Constant (rlv,name,None,ty,att) ->
195           rlv,name,ty,att,height
196       | _,_,_,_, C.Constant (_,_,Some _,_,_) ->
197           prerr_endline "get_checked_decl on a definition"; assert false
198       | _ -> prerr_endline "get_checked_decl on a non decl 2"; assert false)
199   | _ -> prerr_endline "get_checked_decl on a non decl"; assert false
200 ;;
201
202 let get_checked_def = function
203   | Ref.Ref (uri, Ref.Def _) ->
204       (match get_checked_obj uri with
205       | _,height,_,_, C.Constant (rlv,name,Some bo,ty,att) ->
206           rlv,name,bo,ty,att,height
207       | _,_,_,_, C.Constant (_,_,None,_,_) ->
208           prerr_endline "get_checked_def on an axiom"; assert false
209       | _ -> prerr_endline "get_checked_def on a non def 2"; assert false)
210   | _ -> prerr_endline "get_checked_def on a non def"; assert false
211 ;;
212
213 let get_checked_indtys = function
214   | Ref.Ref (uri, (Ref.Ind (_,n,_)|Ref.Con (n,_,_))) ->
215       (match get_checked_obj uri with
216       | _,_,_,_, C.Inductive (inductive,leftno,tys,att) ->
217         inductive,leftno,tys,att,n
218       | _ -> prerr_endline "get_checked_indtys on a non ind 2"; assert false)
219   | _ -> prerr_endline "get_checked_indtys on a non ind"; assert false
220 ;;
221
222 let get_checked_fixes_or_cofixes = function
223   | Ref.Ref (uri, (Ref.Fix _|Ref.CoFix _))->
224       (match get_checked_obj uri with
225       | _,height,_,_, C.Fixpoint (_,funcs,att) ->
226          funcs, att, height
227       | _ ->prerr_endline "get_checked_(co)fix on a non (co)fix 2";assert false)
228   | _ -> prerr_endline "get_checked_(co)fix on a non (co)fix"; assert false
229 ;;
230
231 let get_relevance (Ref.Ref (_, infos) as r) =
232   match infos with
233      Ref.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
234    | Ref.Decl -> let res,_,_,_,_ = get_checked_decl r in res
235    | Ref.Ind _ ->
236        let _,_,tl,_,n = get_checked_indtys r in
237        let res,_,_,_ = List.nth tl n in
238          res
239     | Ref.Con (_,i,_) ->
240        let _,_,tl,_,n = get_checked_indtys r in
241        let _,_,_,cl = List.nth tl n in
242        let res,_,_ = List.nth cl (i - 1) in
243          res
244     | Ref.Fix (fixno,_,_)
245     | Ref.CoFix fixno ->
246         let fl,_,_ = get_checked_fixes_or_cofixes r in
247         let res,_,_,_,_ = List.nth fl fixno in
248           res
249 ;;
250
251
252 let invalidate _ = 
253   assert (!frozen_list = []);
254   NUri.UriHash.clear cache
255 ;;