]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgType.ml
new options activated
[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 (* 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 st s m t =
33    let s =  s ^ " the term" in
34    L.log st BR.specs (pred 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 zero = Some 0
53
54 let assert_convertibility err f st m u w v =
55    if BR.are_convertible st m zero u m zero w then f () else
56    error3 err m v w u
57
58 let assert_applicability err f st m x u w v =
59    let mode = if x then None else zero in
60    match BR.xwhd st m mode u with 
61       | _, B.Sort _                            ->
62          error1 err "not a function type" m u
63       | mu, B.Bind (_, B.Abst (true, n, u), _) -> 
64          if !G.cc && not (N.assert_not_zero st n) then error1 err "not a function type" m u else 
65          if BR.are_convertible st mu zero u m zero w then f () else
66          error3 err m v w ~mu u
67       | _                                      -> assert false (**)
68
69 let rec b_type_of err f st m y =
70    if !G.ct >= level then log1 st "Now checking" m y;
71    match y with
72    | B.Sort (a, h)                   ->
73       let h = H.apply h in f y (B.Sort (a, h)) 
74    | B.LRef (_, i)                   ->
75       begin match BR.get m i with
76          | B.Abst (_, _, w)          ->
77             f y (BS.lift (succ i) (0) w)
78          | B.Abbr (B.Cast (_, w, _)) -> 
79             f y (BS.lift (succ i) (0) w)
80          | B.Abbr _                  -> assert false
81          | B.Void                    -> 
82             error1 err "reference to excluded variable" m y
83       end
84    | B.GRef (_, uri)                 ->
85       begin match BE.get_entity uri with
86          | _, _, _, E.Abst w                  -> f y w
87          | _, _, _, E.Abbr (B.Cast (_, w, _)) -> f y w
88          | _, _, _, E.Abbr _                  -> assert false
89          | _, _, _, E.Void                    ->
90             error1 err "reference to unknown entry" m y
91       end
92    | B.Bind (a, B.Abbr v, t)         ->
93       let f rv rt tt =
94          f (S.sh2 v rv t rt y (B.bind_abbr a)) (B.bind_abbr a rv tt)
95       in
96       let f rv m = b_type_of err (f rv) st m t in
97       let f rv = f rv (BR.push m a (B.abbr rv)) in
98       let f rv vv = match rv with 
99          | B.Cast _ -> f rv
100          | _        -> f (B.Cast (E.empty_node, vv, rv))
101       in
102       type_of err f st m v
103    | B.Bind (a, B.Abst (r, n, u), t) ->
104       let f ru rt tt =
105          f (S.sh2 u ru t rt y (B.bind_abst r n a)) (B.bind_abst r (N.minus st n 1) a ru tt)
106       in
107       let f ru m = b_type_of err (f ru) st m t in
108       let f ru _ = f ru (BR.push m a (B.abst r n ru)) in
109       type_of err f st m u
110    | B.Bind (a, B.Void, t)           ->
111       let f rt tt = 
112          f (S.sh1 t rt y (B.bind_void a)) (B.bind_void a tt)
113       in
114       b_type_of err f st (BR.push m a B.Void) t
115    | B.Appl (a, x, v, t)             ->
116       let f rv vv rt tt = 
117          let f _ = f (S.sh2 v rv t rt y (B.appl a x)) (B.appl a x rv tt) in
118          assert_applicability err f st m x tt vv rv
119       in
120       let f rv vv = b_type_of err (f rv vv) st m t in
121       type_of err f st m v
122    | B.Cast (a, u, t)                ->
123       let f ru rt tt =  
124          let f _ = f (S.sh2 u ru t rt y (B.cast a)) ru in
125          assert_convertibility err f st m ru tt rt
126       in
127       let f ru _ = b_type_of err (f ru) 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 t = b_type_of err f st m t