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 let sc, st = s ^ " in the context", "the term" in
31 L.log O.specs level (L.ct_items1 sc c st t)
34 let sc = "In the context" in
35 raise (TypeError (L.ct_items1 sc c st t))
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"
41 raise (TypeError (L.ct_items3 sc c st1 t1 st2 t2 st3 t3))
44 let map t v = B.Appl (v, t) in
45 List.fold_left map (B.GRef u) l
47 let add_coercion f t (i, uri, vs) =
48 let rec add f x = match x with
54 let f tt = f (S.sh2 u uu t tt x B.cast) in
60 let f tt = f (S.sh2 v vv t tt x B.appl) in
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
68 | B.Bind (l, id, B.Abst w, t) ->
70 let f tt = f (S.sh2 w ww t tt x (B.bind_abst l id)) in
74 | B.Bind (l, id, B.Abbr v, t) ->
76 let f tt = f (S.sh2 v vv t tt x (B.bind_abbr l id)) in
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
86 let add_coercions f = C.list_fold_left f add_coercion
88 (* Interface functions ******************************************************)
90 let rec b_type_of f g c x =
91 log1 "Now checking" c x;
94 let f h = f x (B.Sort h) in H.apply f g h
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
103 error1 "variable not found" c x
108 | _, _, B.Abst w -> f x w
109 | _, _, B.Abbr (B.Cast (w, v)) -> f x w
110 | _, _, B.Abbr _ -> assert false
112 error1 "reference to excluded object" c x
115 | B.Bind (l, id, B.Abbr v, t) ->
117 f (S.sh2 v xv t xt x (B.bind_abbr l id)) (B.bind_abbr l id xv tt)
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
123 | _ -> f (B.Cast (vv, xv))
126 | B.Bind (l, id, B.Abst u, t) ->
128 f (S.sh2 u xu t xt x (B.bind_abst l id)) (B.bind_abst l id xu tt)
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
133 | B.Bind (l, id, B.Void, t) ->
135 f (S.sh1 t xt x (B.bind l id B.Void)) (B.bind l id B.Void tt)
137 let f cc = b_type_of f g cc t in
138 B.push f c l id B.Void
140 let f xv vv xt tt = function
143 L.log O.specs (succ level) (L.t_items1 "Just scanned" c w);
144 L.unbox (succ level);
146 | Some [] -> f (S.sh2 v xv t xt x B.appl) (B.appl xv tt)
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
151 | None -> error3 c xv vv w
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]))
162 error1 "not a function" c xt
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
168 let f xu xt tt = function
169 | Some [] -> f (S.sh2 u xu t xt x B.cast) xu
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
174 | None -> error3 c xt tt xu
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
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