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