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