]> matita.cs.unibo.it Git - helm.git/blob - components/ng_kernel/nCicEnvironment.ml
matita 0.5.1 tagged
[helm.git] / 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;;
20 exception BadConstraint of string Lazy.t;;
21
22 let type0 = []
23
24 let le_constraints = ref [] (* strict,a,b *)
25
26 let rec le_path_uri avoid strict a b =
27  (not strict && NUri.eq a b) ||
28  List.exists
29   (fun (strict',x,y) ->
30      NUri.eq y b && not (List.exists (NUri.eq x) avoid) &&
31        le_path_uri (x::avoid) (strict && not strict') a x
32   ) !le_constraints
33 ;;
34
35 let leq_path a b = le_path_uri [b] (fst a) (snd a) b;;
36
37 let universe_leq a b = 
38   match a, b with
39   | a,[(false,b)] -> List.for_all (fun a -> leq_path a b) a
40   | _,_ ->
41      raise (BadConstraint
42       (lazy "trying to check if a universe is less or equal than an inferred universe"))
43
44 let universe_eq a b = universe_leq b a && universe_leq a b
45
46 let add_constraint strict a b = 
47   match a,b with
48   | [false,a2],[false,b2] -> 
49       if not (le_path_uri [] strict a2 b2) then (
50         if le_path_uri [] (not strict) b2 a2 then
51          (raise (BadConstraint (lazy "universe inconsistency")));
52         le_constraints := (strict,a2,b2) :: !le_constraints)
53   | _ -> raise (BadConstraint
54           (lazy "trying to add a constraint on an inferred universe"))
55 ;;
56
57 let typecheck_obj,already_set = ref (fun _ -> assert false), ref false;;
58 let set_typecheck_obj f =
59  if !already_set then
60   assert false
61  else
62   begin
63    typecheck_obj := f;
64    already_set := true
65   end
66 ;;
67
68 let cache = NUri.UriHash.create 313;;
69 let frozen_list = ref [];;
70
71 exception Propagate of NUri.uri * exn;;
72
73 let get_checked_obj u =
74  if List.exists (fun (k,_) -> NUri.eq u k) !frozen_list
75  then
76   raise (CircularDependency (lazy (NUri.string_of_uri u)))
77  else
78   let obj =
79    try NUri.UriHash.find cache u
80    with
81     Not_found ->
82      let saved_frozen_list = !frozen_list in
83      try
84       let obj =
85        try NCicLibrary.get_obj u
86        with
87         NCicLibrary.ObjectNotFound m -> raise (ObjectNotFound m)
88       in
89         frozen_list := (u,obj)::saved_frozen_list;
90         !typecheck_obj obj;
91         frozen_list := saved_frozen_list;
92         let obj = `WellTyped obj in
93         NUri.UriHash.add cache u obj;
94         obj
95      with
96         Sys.Break as e ->
97          frozen_list := saved_frozen_list;
98          raise e
99       | Propagate (u',_) as e' ->
100          frozen_list := saved_frozen_list;
101          let exn = `Exn (BadDependency (lazy (NUri.string_of_uri u ^
102            " depends (recursively) on " ^ NUri.string_of_uri u' ^
103            " which is not well-typed"))) in
104          NUri.UriHash.add cache u exn;
105          if saved_frozen_list = [] then
106           exn
107          else
108           raise e'
109       | e ->
110          frozen_list := saved_frozen_list;
111          let exn = `Exn e in
112          NUri.UriHash.add cache u exn;
113          if saved_frozen_list = [] then
114           exn
115          else
116           raise (Propagate (u,e))
117   in
118    match obj with
119       `WellTyped o -> o
120     | `Exn e -> raise e
121 ;;
122
123 let get_checked_decl = function
124   | Ref.Ref (uri, Ref.Decl) ->
125       (match get_checked_obj uri with
126       | _,height,_,_, C.Constant (rlv,name,None,ty,att) ->
127           rlv,name,ty,att,height
128       | _,_,_,_, C.Constant (_,_,Some _,_,_) ->
129           prerr_endline "get_checked_decl on a definition"; assert false
130       | _ -> prerr_endline "get_checked_decl on a non decl 2"; assert false)
131   | _ -> prerr_endline "get_checked_decl on a non decl"; assert false
132 ;;
133
134 let get_checked_def = function
135   | Ref.Ref (uri, Ref.Def _) ->
136       (match get_checked_obj uri with
137       | _,height,_,_, C.Constant (rlv,name,Some bo,ty,att) ->
138           rlv,name,bo,ty,att,height
139       | _,_,_,_, C.Constant (_,_,None,_,_) ->
140           prerr_endline "get_checked_def on an axiom"; assert false
141       | _ -> prerr_endline "get_checked_def on a non def 2"; assert false)
142   | _ -> prerr_endline "get_checked_def on a non def"; assert false
143 ;;
144
145 let get_checked_indtys = function
146   | Ref.Ref (uri, (Ref.Ind (_,n,_)|Ref.Con (n,_,_))) ->
147       (match get_checked_obj uri with
148       | _,_,_,_, C.Inductive (inductive,leftno,tys,att) ->
149         inductive,leftno,tys,att,n
150       | _ -> prerr_endline "get_checked_indtys on a non ind 2"; assert false)
151   | _ -> prerr_endline "get_checked_indtys on a non ind"; assert false
152 ;;
153
154 let get_checked_fixes_or_cofixes = function
155   | Ref.Ref (uri, (Ref.Fix (fixno,_,_)|Ref.CoFix fixno))->
156       (match get_checked_obj uri with
157       | _,height,_,_, C.Fixpoint (_,funcs,att) ->
158          funcs, att, height
159       | _ ->prerr_endline "get_checked_(co)fix on a non (co)fix 2";assert false)
160   | r -> prerr_endline ("get_checked_(co)fix on " ^ Ref.string_of_reference r); assert false
161 ;;
162
163 let get_relevance (Ref.Ref (_, infos) as r) =
164   match infos with
165      Ref.Def _ -> let res,_,_,_,_,_ = get_checked_def r in res
166    | Ref.Decl -> let res,_,_,_,_ = get_checked_decl r in res
167    | Ref.Ind _ ->
168        let _,_,tl,_,n = get_checked_indtys r in
169        let res,_,_,_ = List.nth tl n in
170          res
171     | Ref.Con (_,i,_) ->
172        let _,_,tl,_,n = get_checked_indtys r in
173        let _,_,_,cl = List.nth tl n in
174        let res,_,_ = List.nth cl (i - 1) in
175          res
176     | Ref.Fix (fixno,_,_)
177     | Ref.CoFix fixno ->
178         let fl,_,_ = get_checked_fixes_or_cofixes r in
179         let res,_,_,_,_ = List.nth fl fixno in
180           res
181 ;;
182
183
184 let invalidate _ = 
185   assert (!frozen_list = []);
186   NUri.UriHash.clear cache
187 ;;