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_______________________________________________________________ *)
19 module E = BrgEnvironment
20 module R = BrgReduction
22 exception TypeError of B.message
24 (* Internal functions *******************************************************)
29 let sc, st = s ^ " in the context", "the term" in
30 L.log O.specs level (L.ct_items1 sc c st t)
33 let sc = "In the context" in
34 raise (TypeError (L.ct_items1 sc c st t))
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"
40 raise (TypeError (L.ct_items3 sc c st1 t1 st2 t2 st3 t3))
42 (* Interface functions ******************************************************)
46 let rec b_type_of f g c x =
47 (* L.warn "Entering T.b_type_of"; *)
48 log1 "Now checking" c x;
51 let f h = f x (B.Sort (a, h)) in H.apply f g h
54 | Some (_, B.Abst w) -> f x w
55 | Some (_, B.Abbr (B.Cast (_, w, _))) -> f x w
56 | Some (_, B.Abbr _) -> assert false
58 error1 "reference to excluded variable" c x
60 error1 "variable not found" c x
65 | _, _, B.Abst w -> f x w
66 | _, _, B.Abbr (B.Cast (_, w, _)) -> f x w
67 | _, _, B.Abbr _ -> assert false
69 error1 "reference to excluded object" c x
72 | B.Bind (a, B.Abbr v, t) ->
74 f (S.sh2 v xv t xt x (B.bind_abbr a)) (B.bind_abbr a xv tt)
76 let f xv cc = b_type_of (f xv) g cc t in
77 let f xv = B.push (f xv) c a (B.Abbr xv) in
78 let f xv vv = match xv with
80 | _ -> f (B.Cast ([], vv, xv))
83 | B.Bind (a, B.Abst u, t) ->
85 f (S.sh2 u xu t xt x (B.bind_abst a)) (B.bind_abst a xu tt)
87 let f xu cc = b_type_of (f xu) g cc t in
88 let f xu _ = B.push (f xu) c a (B.Abst xu) in
90 | B.Bind (a, B.Void, t) ->
92 f (S.sh1 t xt x (B.bind a B.Void)) (B.bind a B.Void tt)
94 let f cc = b_type_of f g cc t in
97 let f xv vv xt tt = function
100 L.log O.specs (succ level) (L.t_items1 "Just scanned" c w);
101 L.unbox (succ level);
103 (* L.warn (Printf.sprintf "Convertible: %b" a); *)
104 if r then f (S.sh2 v xv t xt x (B.appl a)) (B.appl a xv tt)
105 else error3 c xv vv w
107 R.are_convertible f ~si:!si c w vv
109 error1 "not a function" c xt
111 let f xv vv xt tt = R.domain (f xv vv xt tt) c tt in
112 let f xv vv = b_type_of (f xv vv) g c t in
114 | B.Cast (a, u, t) ->
116 (* L.warn (Printf.sprintf "Convertible: %b" a); *)
117 if r then f (S.sh2 u xu t xt x (B.cast a)) xu else error3 c xt tt xu
119 let f xu xt tt = R.are_convertible ~si:!si (f xu xt tt) c xu tt in
120 let f xu _ = b_type_of (f xu) g c t in
123 and type_of f g c x =
124 let f t u = L.unbox level; f t u in
125 L.box level; b_type_of f g c x