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.
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_______________________________________________________________ *)
20 module E = BagEnvironment
21 module R = BagReduction
23 exception TypeError of B.message
25 (* Internal functions *******************************************************)
30 raise (TypeError (L.ct_items1 s c t))
32 let error2 s1 c1 t1 s2 c2 t2 =
33 raise (TypeError (L.ct_items2 s1 c1 t1 s2 c2 t2))
36 let map t v = B.Appl (v, t) in
37 List.fold_left map (B.GRef u) l
39 let add_coercion f t (i, uri, vs) =
40 let rec add f x = match x with
46 let f tt = f (S.sh2 u uu t tt x B.cast) in
52 let f tt = f (S.sh2 v vv t tt x B.appl) in
56 | B.Bind (l, _, _, _) when i = l ->
57 if U.eq uri I.imp then f (mk_gref I.mt (vs @ [x])) else
58 if U.eq uri I.all then f (mk_gref I.alli (vs @ [x])) else
60 | B.Bind (l, id, B.Abst w, t) ->
62 let f tt = f (S.sh2 w ww t tt x (B.bind_abst l id)) in
66 | B.Bind (l, id, B.Abbr v, t) ->
68 let f tt = f (S.sh2 v vv t tt x (B.bind_abbr l id)) in
72 | B.Bind (l, id, B.Void, t) ->
73 let f tt = f (S.sh1 t tt x (B.bind l id B.Void)) in
78 let add_coercions f = C.list_fold_left f add_coercion
80 (* Interface functions ******************************************************)
82 let rec b_type_of f g c x =
83 L.log O.specs level (L.ct_items1 "Now checking" c x);
86 let f h = f x (B.Sort h) in H.apply f g h
89 | Some (_, B.Abst w) -> f x w
90 | Some (_, B.Abbr (B.Cast (w, v))) -> f x w
91 | Some (_, B.Abbr _) -> assert false
93 error1 "reference to excluded variable" c x
95 error1 "variable not found" c x
100 | _, _, B.Abst w -> f x w
101 | _, _, B.Abbr (B.Cast (w, v)) -> f x w
102 | _, _, B.Abbr _ -> assert false
104 error1 "reference to excluded object" c x
107 | B.Bind (l, id, B.Abbr v, t) ->
109 f (S.sh2 v xv t xt x (B.bind_abbr l id)) (B.bind_abbr l id xv tt)
111 let f xv cc = b_type_of (f xv) g cc t in
112 let f xv = B.push (f xv) c l id (B.Abbr xv) in
113 let f xv vv = match xv with
115 | _ -> f (B.Cast (vv, xv))
118 | B.Bind (l, id, B.Abst u, t) ->
120 f (S.sh2 u xu t xt x (B.bind_abst l id)) (B.bind_abst l id xu tt)
122 let f xu cc = b_type_of (f xu) g cc t in
123 let f xu _ = B.push (f xu) c l id (B.Abst xu) in
125 | B.Bind (l, id, B.Void, t) ->
127 f (S.sh1 t xt x (B.bind l id B.Void)) (B.bind l id B.Void tt)
129 let f cc = b_type_of f g cc t in
130 B.push f c l id B.Void
132 let f xv vv xt tt = function
135 L.log O.specs (succ level) (L.ct_items1 "Just scanned" c w);
136 L.unbox (succ level);
138 | Some [] -> f (S.sh2 v xv t xt x B.appl) (B.appl xv tt)
140 L.log O.specs level (L.items1 "Rechecking coerced term");
141 let f xv = b_type_of f g c (S.sh2 v xv t xt x B.appl) in
143 | None -> error2 "The term" c xv "must be of type" c w
145 R.are_convertible f c w vv
146 (* inserting a missing "modus ponens" *)
147 | R.GRef (uri, vs) when U.eq uri I.imp ->
148 L.log O.specs level (L.items1 "Rechecking coerced term");
149 b_type_of f g c (mk_gref I.mp (vs @ [xv; xt]))
150 | R.GRef (uri, vs) when U.eq uri I.all ->
151 L.log O.specs level (L.items1 "Rechecking coerced term");
152 b_type_of f g c (mk_gref I.alle (vs @ [xv; xt]))
154 error1 "not a function" c xt
156 let f xv vv xt tt = R.ho_whd (f xv vv xt tt) c tt in
157 let f xv vv = b_type_of (f xv vv) g c t in
160 let f xu xt = function
161 | Some [] -> f (S.sh2 u xu t xt x B.cast) xu
163 L.log O.specs level (L.items1 "Rechecking coerced term");
164 let f xt = b_type_of f g c (S.sh2 u xu t xt x B.cast) in
166 | None -> error2 "The term" c xt "must be of type" c xu
168 let f xu xt tt = R.are_convertible (f xu xt) c xu tt in
169 let f xu _ = b_type_of (f xu) g c t in
172 and type_of f g c x =
173 let f t u = L.unbox level; f t u in
174 L.box level; b_type_of f g c x