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