]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgType.ml
- basic_rg: architectural bug fix
[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 L = Log
13 module B = Brg
14 module O = BrgOutput
15 module E = BrgEnvironment
16 module R = BrgReduction
17
18 exception TypeError of B.message
19
20 (* Internal functions *******************************************************)
21
22 let level = 4
23
24 let error1 s c t =
25    raise (TypeError (L.ct_items1 s c t))
26
27 let error2 s1 c1 t1 s2 c2 t2 =
28    raise (TypeError (L.ct_items2 s1 c1 t1 s2 c2 t2))
29
30 (* Interface functions ******************************************************)
31
32 let rec type_of f g c x = 
33    L.log O.specs level (L.ct_items1 "now checking" c x);
34    match x with
35    | B.Sort h                 -> f (B.Sort (g h))
36    | B.LRef i                 ->
37       let f = function
38          | Some (_, B.Abst w)               -> f w
39          | Some (_, B.Abbr (B.Cast (w, v))) -> f w
40          | Some (_, B.Abbr _)               -> assert false
41          | Some (_, B.Void)                 -> 
42             error1 "reference to excluded variable" c x
43          | None                             ->
44             error1 "variable not found" c x      
45       in
46       B.get f c i
47    | B.GRef uri               ->
48       let f = function
49          | _, _, B.Abst w               -> f w
50          | _, _, B.Abbr (B.Cast (w, v)) -> f w
51          | _, _, B.Abbr _               -> assert false
52          | _, _, B.Void                 ->
53             error1 "reference to excluded object" c x
54       in
55       E.get_obj f uri
56    | B.Bind (id, B.Abbr u, t) ->
57       let f tt = f (B.Bind (id, B.Abbr u, tt)) in
58       let f cc = type_of f g cc t in
59       let f u = B.push f c id (B.Abbr u) in
60       let f uu = match u with 
61          | B.Cast _ -> f u
62          | _        -> f (B.Cast (uu, u))
63       in
64       type_of f g c u
65    | B.Bind (id, B.Abst u, t) ->
66       let f tt = f (B.Bind (id, B.Abst u, tt)) in
67       let f cc = type_of f g cc t in
68       let f _ = B.push f c id (B.Abst u) in
69       type_of f g c u
70    | B.Bind (id, B.Void, t)   ->
71       let f tt = f (B.Bind (id, B.Void, tt)) in
72       let f cc = type_of f g cc t in
73       B.push f c id B.Void
74    | B.Appl (v, t)            ->
75       let f tt cc = function
76          | R.Sort _ -> error1 "not a function" c t
77          | R.Abst w -> 
78             L.log O.specs (succ level) (L.ct_items1 "Just scanned" cc w);
79             let f b =
80                if b then f (B.Appl (v, tt)) else
81                error2 "the term" c v "must be of type" cc w
82             in
83             type_of (R.are_convertible f cc w) g c v
84       in
85       let f tt = R.ho_whd (f tt) c t in
86       type_of f g c t 
87    | B.Cast (u, t)            ->
88       let f b = 
89          if b then f u else
90          error2 "the term" c t "must be of type" c u
91       in
92       let f _ = type_of (R.are_convertible f c u) g c t in
93       type_of f g c u