]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgType.ml
- brgOutput: the nodes count is now implemented
[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 U = NUri
13 module C = Cps
14 module A = Share
15 module L = Log
16 module H = Hierarchy
17 module B = Brg
18 module O = BrgOutput
19 module E = BrgEnvironment
20 module S = BrgSubstitution
21 module R = BrgReduction
22
23 exception TypeError of B.message
24
25 (* Internal functions *******************************************************)
26
27 let level = 4
28
29 let log1 s c t =
30    let sc, st = s ^ " in the context", "the term" in
31    L.log O.specs level (L.ct_items1 sc c st t)
32
33 let error1 st c t =
34    let sc = "In the context" in
35    raise (TypeError (L.ct_items1 sc c st t))
36
37 let error3 c t1 t2 t3 =
38    let sc, st1, st2, st3 = 
39       "In the context", "the term", "is of type", "but must be of type"
40    in
41    raise (TypeError (L.ct_items3 sc c st1 t1 st2 t2 st3 t3))
42
43 (* Interface functions ******************************************************)
44
45 let si = ref false
46
47 let rec b_type_of f g c x = 
48 (*   L.warn "Entering T.b_type_of"; *)
49    log1 "Now checking" c x;
50    match x with
51    | B.Sort (a, h)           ->
52       let f h = f x (B.Sort (a, h)) in H.apply f g h
53    | B.LRef (_, i)           ->
54       let f _ = function
55          | Some (_, B.Abst w)                  ->
56             S.lift (f x) (succ i) (0) w
57          | Some (_, B.Abbr (B.Cast (_, w, _))) -> 
58             S.lift (f x) (succ i) (0) w
59          | Some (_, B.Abbr _)                  -> assert false
60          | Some (_, B.Void)                    -> 
61             error1 "reference to excluded variable" c x
62          | None                             ->
63             error1 "variable not found" c x      
64       in
65       B.get f c i
66    | B.GRef (_, uri)         ->
67       let f = function
68          | _, _, B.Abst w                  -> f x w
69          | _, _, B.Abbr (B.Cast (_, w, _)) -> f x w
70          | _, _, B.Abbr _                  -> assert false
71          | _, _, B.Void                    ->
72             error1 "reference to excluded object" c x
73       in
74       E.get_obj f uri   
75    | B.Bind (a, B.Abbr v, t) ->
76       let f xv xt tt =
77          f (A.sh2 v xv t xt x (B.bind_abbr a)) (B.bind_abbr a xv tt)
78       in
79       let f xv cc = b_type_of (f xv) g cc t in
80       let f xv = B.push (f xv) c a (B.Abbr xv) in
81       let f xv vv = match xv with 
82          | B.Cast _ -> f xv
83          | _        -> assert false (* f (B.Cast ([], vv, xv)) *)
84       in
85       type_of f g c v
86    | B.Bind (a, B.Abst u, t) ->
87       let f xu xt tt =
88          f (A.sh2 u xu t xt x (B.bind_abst a)) (B.bind_abst a xu tt)
89       in
90       let f xu cc = b_type_of (f xu) g cc t in
91       let f xu _ = B.push (f xu) c a (B.Abst xu) in
92       type_of f g c u
93    | B.Bind (a, B.Void, t)   ->
94       let f xt tt = 
95          f (A.sh1 t xt x (B.bind a B.Void)) (B.bind a B.Void tt)
96       in
97       let f cc = b_type_of f g cc t in
98       B.push f c a B.Void   
99    | B.Appl (a, v, t)        ->
100       let f xv vv xt tt = function
101          | Some w -> 
102             L.box (succ level);
103             L.log O.specs (succ level) (L.t_items1 "Just scanned" c w);
104             L.unbox (succ level);
105             let f r =
106 (*             L.warn (Printf.sprintf "Convertible: %b" a); *)
107                if r then f (A.sh2 v xv t xt x (B.appl a)) (B.appl a xv tt)
108                else error3 c xv vv w
109             in
110             R.are_convertible f ~si:!si c w vv
111          | None   -> 
112             error1 "not a function" c xt
113       in
114       let f xv vv xt tt = R.domain (f xv vv xt tt) c tt in
115       let f xv vv = b_type_of (f xv vv) g c t in
116       type_of f g c v
117    | B.Cast (a, u, t)        ->
118       let f xu xt tt r =  
119          (* L.warn (Printf.sprintf "Convertible: %b" a); *)
120          if r then f (A.sh2 u xu t xt x (B.cast a)) xu else error3 c xt tt xu
121       in
122       let f xu xt tt = R.are_convertible ~si:!si (f xu xt tt) c xu tt in
123       let f xu _ = b_type_of (f xu) g c t in
124       type_of f g c u
125
126 and type_of f g c x =
127    let f t u = L.unbox level; f t u in
128    L.box level; b_type_of f g c x