]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_ag/bagType.ml
- improved logging
[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 I = AutItem
18 module B = Bag
19 module O = BagOutput
20 module E = BagEnvironment
21 module R = BagReduction
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 let mk_gref u l =
44    let map t v = B.Appl (v, t) in
45    List.fold_left map (B.GRef u) l
46
47 let add_coercion f t (i, uri, vs) =
48    let rec add f x = match x with
49       | B.Sort _
50       | B.LRef _
51       | B.GRef _                       -> f x
52       | B.Cast (u, t)                  -> 
53          let f uu =
54             let f tt = f (S.sh2 u uu t tt x B.cast) in
55             add f t
56          in
57          add f u
58       | B.Appl (v, t)                  ->
59          let f vv =
60             let f tt = f (S.sh2 v vv t tt x B.appl) in
61             add f t
62          in
63          add f v
64       | B.Bind (l, _, _, _) when i = l -> 
65          if U.eq uri I.imp then f (mk_gref I.mt (vs @ [x])) else
66          if U.eq uri I.all then f (mk_gref I.alli (vs @ [x])) else
67          assert false
68       | B.Bind (l, id, B.Abst w, t)    ->
69          let f ww =
70             let f tt = f (S.sh2 w ww t tt x (B.bind_abst l id)) in
71             add f t
72          in
73          add f w
74       | B.Bind (l, id, B.Abbr v, t)    ->
75          let f vv =
76             let f tt = f (S.sh2 v vv t tt x (B.bind_abbr l id)) in
77             add f t
78          in
79          add f v
80       | B.Bind (l, id, B.Void, t)      ->
81          let f tt = f (S.sh1 t tt x (B.bind l id B.Void)) in
82          add f t
83    in
84    add f t
85
86 let add_coercions f = C.list_fold_left f add_coercion
87
88 (* Interface functions ******************************************************)
89
90 let rec b_type_of f g c x = 
91    log1 "Now checking" c x;
92    match x with
93    | B.Sort h                    ->
94       let f h = f x (B.Sort h) in H.apply f g h
95    | B.LRef i                    ->
96       let f = function
97          | Some (_, B.Abst w)               -> f x w
98          | Some (_, B.Abbr (B.Cast (w, v))) -> f x w
99          | Some (_, B.Abbr _)               -> assert false
100          | Some (_, B.Void)                 -> 
101             error1 "reference to excluded variable" c x
102          | None                             ->
103             error1 "variable not found" c x      
104       in
105       B.get f c i
106    | B.GRef uri                  ->
107       let f = function
108          | _, _, B.Abst w               -> f x w
109          | _, _, B.Abbr (B.Cast (w, v)) -> f x w
110          | _, _, B.Abbr _               -> assert false
111          | _, _, B.Void                 ->
112             error1 "reference to excluded object" c x
113       in
114       E.get_obj f uri   
115    | B.Bind (l, id, B.Abbr v, t) ->
116       let f xv xt tt =
117          f (S.sh2 v xv t xt x (B.bind_abbr l id)) (B.bind_abbr l id xv tt)
118       in
119       let f xv cc = b_type_of (f xv) g cc t in
120       let f xv = B.push (f xv) c l id (B.Abbr xv) in
121       let f xv vv = match xv with 
122          | B.Cast _ -> f xv
123          | _        -> f (B.Cast (vv, xv))
124       in
125       type_of f g c v
126    | B.Bind (l, id, B.Abst u, t) ->
127       let f xu xt tt =
128          f (S.sh2 u xu t xt x (B.bind_abst l id)) (B.bind_abst l id xu tt)
129       in
130       let f xu cc = b_type_of (f xu) g cc t in
131       let f xu _ = B.push (f xu) c l id (B.Abst xu) in
132       type_of f g c u
133    | B.Bind (l, id, B.Void, t)   ->
134       let f xt tt = 
135          f (S.sh1 t xt x (B.bind l id B.Void)) (B.bind l id B.Void tt)
136       in
137       let f cc = b_type_of f g cc t in
138       B.push f c l id B.Void   
139    | B.Appl (v, t)            ->
140       let f xv vv xt tt = function
141          | R.Abst w                             -> 
142             L.box (succ level);
143             L.log O.specs (succ level) (L.t_items1 "Just scanned" c w);
144             L.unbox (succ level);
145             let f = function
146                | Some [] -> f (S.sh2 v xv t xt x B.appl) (B.appl xv tt)
147                | Some l  ->
148                   L.log O.specs level (L.items1 "Rechecking coerced term");
149                   let f xv = b_type_of f g c (S.sh2 v xv t xt x B.appl) in
150                   add_coercions f xv l
151                | None   -> error3 c xv vv w
152             in
153             R.are_convertible f c w vv
154 (* inserting a missing "modus ponens" *)
155          | R.GRef (uri, vs) when U.eq uri I.imp ->
156             L.log O.specs level (L.items1 "Rechecking coerced term");
157             b_type_of f g c (mk_gref I.mp (vs @ [xv; xt]))
158          | R.GRef (uri, vs) when U.eq uri I.all ->
159             L.log O.specs level (L.items1 "Rechecking coerced term");
160             b_type_of f g c (mk_gref I.alle (vs @ [xt; xv]))
161          | _                                    -> 
162             error1 "not a function" c xt
163       in
164       let f xv vv xt tt = R.ho_whd (f xv vv xt tt) c tt in
165       let f xv vv = b_type_of (f xv vv) g c t in
166       type_of f g c v
167    | B.Cast (u, t)            ->
168       let f xu xt tt = function 
169          | Some [] -> f (S.sh2 u xu t xt x B.cast) xu
170          | Some l  ->
171             L.log O.specs level (L.items1 "Rechecking coerced term");
172             let f xt = b_type_of f g c (S.sh2 u xu t xt x B.cast) in
173             add_coercions f xt l
174          | None    -> error3 c xt tt xu
175       in
176       let f xu xt tt = R.are_convertible (f xu xt tt) c xu tt in
177       let f xu _ = b_type_of (f xu) g c t in
178       type_of f g c u
179
180 and type_of f g c x =
181    let f t u = L.unbox level; f t u in
182    L.box level; b_type_of f g c x
183