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