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