]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/basic_rg/brgType.ml
5515e4404759f24793a5e45083f43904a361353a
[helm.git] / helm / software / lambda-delta / src / basic_rg / brgType.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 module U  = NUri
13 module C  = Cps
14 module S  = Share
15 module L  = Log
16 module H  = Hierarchy
17 module E  = Entity
18 module B  = Brg
19 module BE = BrgEnvironment
20 module BS = BrgSubstitution
21 module BR = BrgReduction
22
23 type message = (BR.kam, B.term) Log.message
24
25 (* Internal functions *******************************************************)
26
27 let level = 4
28
29 let message1 st1 m t1 =
30    L.et_items1 "In the environment" m st1 t1
31
32 let log1 s m t =
33    let s =  s ^ " the term" in
34    L.log BR.specs level (message1 s m t) 
35
36 let error1 err s m t =
37    err (message1 s m t)
38
39 let message3 m t1 t2 ?mu t3 =    
40    let sm, st1, st2 = "In the environment", "the term", "is of type" in
41    match mu with
42       | Some mu ->
43          let smu, st3 = "but in the environment", "it must be of type" in
44          L.et_items3 sm m st1 t1 st2 t2 ~sc3:smu ~c3:mu st3 t3
45       | None    ->
46          let st3 = "but it must be of type" in
47          L.et_items3 sm m st1 t1 st2 t2 st3 t3
48    
49 let error3 err m t1 t2 ?mu t3 =
50    err (message3 m t1 t2 ?mu t3)
51
52 let assert_convertibility err f st m u w v =
53    if BR.are_convertible st m u m w then f () else
54    error3 err m v w u
55
56 let assert_applicability err f st m u w v =
57    match BR.xwhd st m u with 
58       | _, B.Sort _                 -> error1 err "not a function type" m u
59       | mu, B.Bind (_, B.Abst u, _) -> 
60          if BR.are_convertible st mu u m w then f () else
61          error3 err m v w ~mu u
62       | _                         -> assert false (**)
63
64 let rec b_type_of err f st m x =
65    log1 "Now checking" m x;
66    match x with
67    | B.Sort (a, h)           ->
68       let h = H.apply h in f x (B.Sort (a, h)) 
69    | B.LRef (_, i)           ->
70       begin match BR.get m i with
71          | B.Abst w                  ->
72             f x (BS.lift (succ i) (0) w)
73          | B.Abbr (B.Cast (_, w, _)) -> 
74             f x (BS.lift (succ i) (0) w)
75          | B.Abbr _                  -> assert false
76          | B.Void                    -> 
77             error1 err "reference to excluded variable" m x
78       end
79    | B.GRef (_, uri)         ->
80       begin match BE.get_entity uri with
81          | _, _, E.Abst w                  -> f x w
82          | _, _, E.Abbr (B.Cast (_, w, _)) -> f x w
83          | _, _, E.Abbr _                  -> assert false
84          | _, _, E.Void                    ->
85             error1 err "reference to unknown entry" m x
86       end
87    | B.Bind (a, B.Abbr v, t) ->
88       let f xv xt tt =
89          f (S.sh2 v xv t xt x (B.bind_abbr a)) (B.bind_abbr a xv tt)
90       in
91       let f xv m = b_type_of err (f xv) st m t in
92       let f xv = f xv (BR.push m a (B.abbr xv)) in
93       let f xv vv = match xv with 
94          | B.Cast _ -> f xv
95          | _        -> f (B.Cast ([], vv, xv))
96       in
97       type_of err f st m v
98    | B.Bind (a, B.Abst u, t) ->
99       let f xu xt tt =
100          f (S.sh2 u xu t xt x (B.bind_abst a)) (B.bind_abst a xu tt)
101       in
102       let f xu m = b_type_of err (f xu) st m t in
103       let f xu _ = f xu (BR.push m a (B.abst xu)) in
104       type_of err f st m u
105    | B.Bind (a, B.Void, t)   ->
106       let f xt tt = 
107          f (S.sh1 t xt x (B.bind_void a)) (B.bind_void a tt)
108       in
109       b_type_of err f st (BR.push m a B.Void) t
110          
111    | B.Appl (a, v, t)        ->
112       let f xv vv xt tt = 
113          let f _ = f (S.sh2 v xv t xt x (B.appl a)) (B.appl a xv tt) in
114          assert_applicability err f st m tt vv xv
115       in
116       let f xv vv = b_type_of err (f xv vv) st m t in
117       type_of err f st m v
118    | B.Cast (a, u, t)        ->
119       let f xu xt tt =  
120          let f _ = f (S.sh2 u xu t xt x (B.cast a)) xu in
121          assert_convertibility err f st m xu tt xt
122       in
123       let f xu _ = b_type_of err (f xu) st m t in
124       type_of err f st m u
125
126 (* Interface functions ******************************************************)
127
128 and type_of err f st m x =
129    let f t u = L.unbox level; f t u in
130    L.box level; b_type_of err f st m x