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