]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgType.ml
cf997e473e0ac7fb02e0565cec13a1ebe88cefaa
[helm.git] / helm / software / lambda-delta / 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 A = Share
15 module L = Log
16 module H = Hierarchy
17 module B = Brg
18 module O = BrgOutput
19 module E = BrgEnvironment
20 module S = BrgSubstitution
21 module R = BrgReduction
22
23 type message = (R.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 R.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 ~si m u w v =
53    let err _ = error3 err m v w u in
54    R.are_convertible err f ~si m u m w 
55
56 let assert_applicability err f ~si m u w v =
57    let f mu = function
58       | B.Sort _                  -> error1 err "not a function type" m u
59       | B.Bind (B.Abst (_, u), _) -> 
60          let err _ = error3 err m v w ~mu u in 
61          R.are_convertible err f ~si mu u m w
62       | _                         -> assert false
63    in
64    R.xwhd f m u 
65
66 let rec b_type_of err f ~si g m x =
67    log1 "Now checking" m x;
68    match x with
69    | B.Sort (a, h)             ->
70       let f h = f x (B.Sort (a, h)) in H.apply f g h
71    | B.LRef (_, i)             ->
72       let f = function
73          | B.Abst (_, w)                ->
74             S.lift (f x) (succ i) (0) w
75          | B.Abbr (_, B.Cast (_, w, _)) -> 
76             S.lift (f x) (succ i) (0) w
77          | B.Abbr _                     -> assert false
78          | B.Void _                     -> 
79             error1 err "reference to excluded variable" m x
80       in
81       let err _ = error1 err "reference to unknown variable" m x in
82       R.get err f m i
83    | B.GRef (_, uri)           ->
84       let f = function
85          | _, _, B.Abst (_, w)                -> f x w
86          | _, _, B.Abbr (_, B.Cast (_, w, _)) -> f x w
87          | _, _, B.Abbr _                     -> assert false
88          | _, _, B.Void _                     ->
89             error1 err "reference to excluded entry" m x
90       in
91       let err _ = error1 err "reference to unknown entry" m x in
92       E.get_entry err f uri   
93    | B.Bind (B.Abbr (a, v), t) ->
94       let f xv xt tt =
95          f (A.sh2 v xv t xt x (B.bind_abbr a)) (B.bind_abbr a xv tt)
96       in
97       let f xv m = b_type_of err (f xv) ~si g m t in
98       let f xv = R.push (f xv) m (B.abbr a xv) in
99       let f xv vv = match xv with 
100          | B.Cast _ -> f xv
101          | _        -> f (B.Cast ([], vv, xv))
102       in
103       type_of err f ~si g m v
104    | B.Bind (B.Abst (a, u), t) ->
105       let f xu xt tt =
106          f (A.sh2 u xu t xt x (B.bind_abst a)) (B.bind_abst a xu tt)
107       in
108       let f xu m = b_type_of err (f xu) ~si g m t in
109       let f xu _ = R.push (f xu) m (B.abst a xu) in
110       type_of err f ~si g m u
111    | B.Bind (B.Void a as b, t) ->
112       let f xt tt = 
113          f (A.sh1 t xt x (B.bind b)) (B.bind b tt)
114       in
115       let f m = b_type_of err f ~si g m t in
116       R.push f m b   
117    | B.Appl (a, v, t)          ->
118       let f xv vv xt tt = 
119          let f _ = f (A.sh2 v xv t xt x (B.appl a)) (B.appl a xv tt) in
120          assert_applicability err f ~si m tt vv xv
121       in
122       let f xv vv = b_type_of err (f xv vv) ~si g m t in
123       type_of err f ~si g m v
124    | B.Cast (a, u, t)          ->
125       let f xu xt tt =  
126          let f _ = f (A.sh2 u xu t xt x (B.cast a)) xu in
127          assert_convertibility err f ~si m xu tt xt
128       in
129       let f xu _ = b_type_of err (f xu) ~si g m t in
130       type_of err f ~si g m u
131
132 (* Interface functions ******************************************************)
133
134 and type_of err f ?(si=false) g m x =
135    let f t u = L.unbox level; f t u in
136    L.box level; b_type_of err f ~si g m x