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