]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgType.ml
the TypeError exception is back in place inside the Type modules
[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 exception TypeError of B.message
24
25 (* Internal functions *******************************************************)
26
27 let level = 4
28
29 let log1 s m t =
30    let s =  s ^ " the term" in
31    L.log O.specs level (R.message1 s m t) 
32
33 let error1 s m t =
34    raise (TypeError (R.message1 s m t))
35
36 let message3 m t1 t2 ?mu t3 =   
37    let st1, st2 = "the term", "is of type" in
38    match mu with
39       | Some mu ->
40          let smu, st3 = "but in the context", "it must be of type" in
41          R.message3 st1 st2 ~sm3:smu st3 m t1 t2 ~m3:mu t3
42       | None    ->
43          let st3 = "but it must be of type" in
44          R.message3 st1 st2 st3 m t1 t2 t3
45    
46 let error3 m t1 t2 ?mu t3 =
47    raise (TypeError (message3 m t1 t2 ?mu t3))
48
49 let assert_convertibility f ~si m u w v =
50    let err () = error3 m v w u in
51    R.are_convertible err f ~si m u m w 
52
53 let assert_applicability f ~si m u w v =
54    let f mu = function
55       | B.Bind (B.Abst (_, u), _) -> 
56          let err () = error3 m v w ~mu u in 
57          R.are_convertible err f ~si mu u m w
58       | _                         -> error1 "not a function type" m u
59    in
60    R.xwhd f m u 
61
62 let rec b_type_of f ~si g m x =
63    log1 "Now checking" m x;
64    match x with
65    | B.Sort (a, h)             ->
66       let f h = f x (B.Sort (a, h)) in H.apply f g h
67    | B.LRef (_, i)             ->
68       let f = function
69          | B.Abst (_, w)                ->
70             S.lift (f x) (succ i) (0) w
71          | B.Abbr (_, B.Cast (_, w, _)) -> 
72             S.lift (f x) (succ i) (0) w
73          | B.Abbr _                     -> assert false
74          | B.Void _                     -> 
75             error1 "reference to excluded variable" m x
76       in
77       R.get f m i
78    | B.GRef (_, uri)           ->
79       let f = function
80          | _, _, B.Abst (_, w)                -> f x w
81          | _, _, B.Abbr (_, B.Cast (_, w, _)) -> f x w
82          | _, _, B.Abbr _                     -> assert false
83          | _, _, B.Void _                     ->
84             error1 "reference to excluded object" m x
85       in
86       E.get_obj f uri   
87    | B.Bind (B.Abbr (a, v), t) ->
88       let f xv xt tt =
89          f (A.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 (f xv) ~si g m t in
92       let f xv = R.push (f xv) m (B.abbr a 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 f ~si g m v
98    | B.Bind (B.Abst (a, u), t) ->
99       let f xu xt tt =
100          f (A.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 (f xu) ~si g m t in
103       let f xu _ = R.push (f xu) m (B.abst a xu) in
104       type_of f ~si g m u
105    | B.Bind (B.Void a as b, t) ->
106       let f xt tt = 
107          f (A.sh1 t xt x (B.bind b)) (B.bind b tt)
108       in
109       let f m = b_type_of f ~si g m t in
110       R.push f m b   
111    | B.Appl (a, v, t)          ->
112       let f xv vv xt tt = 
113          let f () = f (A.sh2 v xv t xt x (B.appl a)) (B.appl a xv tt) in
114          assert_applicability f ~si m tt vv xv
115       in
116       let f xv vv = b_type_of (f xv vv) ~si g m t in
117       type_of f ~si g m v
118    | B.Cast (a, u, t)          ->
119       let f xu xt tt =  
120          let f () = f (A.sh2 u xu t xt x (B.cast a)) xu in
121          assert_convertibility f ~si m xu tt xt
122       in
123       let f xu _ = b_type_of (f xu) ~si g m t in
124       type_of f ~si g m u
125
126 (* Interface functions ******************************************************)
127
128 and type_of f ?(si=false) g m x =
129    let f t u = L.unbox level; f t u in
130    L.box level; b_type_of f ~si g m x