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