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_______________________________________________________________ *)
21 module ZE = BagEnvironment
22 module ZR = BagReduction
24 exception TypeError of Z.message
26 (* Internal functions *******************************************************)
31 let sc, st = s ^ " in the envireonment", "the term" in
32 L.log ZO.specs level (L.et_items1 sc c st t)
35 let sc = "In the envireonment" in
36 raise (TypeError (L.et_items1 sc c st t))
38 let error3 c t1 t2 t3 =
39 let sc, st1, st2, st3 =
40 "In the envireonment", "the term", "is of type", "but must be of type"
42 raise (TypeError (L.et_items3 sc c st1 t1 st2 t2 st3 t3))
45 let map t v = Z.Appl (v, t) in
46 List.fold_left map (Z.GRef u) l
48 (* Interface functions ******************************************************)
50 let rec b_type_of f st c x =
51 (* L.warn "Entering T.b_type_of"; *)
52 log1 "Now checking" c x;
55 let h = H.apply h in f x (Z.Sort h)
58 | Some (_, Z.Abst w) -> f x w
59 | Some (_, Z.Abbr (Z.Cast (w, v))) -> f x w
60 | Some (_, Z.Abbr _) -> assert false
62 error1 "reference to excluded variable" c x
64 error1 "variable not found" c x
69 | _, _, E.Abst (_, w) -> f x w
70 | _, _, E.Abbr (Z.Cast (w, v)) -> f x w
71 | _, _, E.Abbr _ -> assert false
72 | _, _, E.Void -> assert false
75 | Z.Bind (l, id, Z.Abbr v, t) ->
77 f (W.sh2 v xv t xt x (Z.bind_abbr l id)) (Z.bind_abbr l id xv tt)
79 let f xv cc = b_type_of (f xv) st cc t in
80 let f xv = Z.push "type abbr" (f xv) c l id (Z.Abbr xv) in
81 let f xv vv = match xv with
83 | _ -> f (Z.Cast (vv, xv))
86 | Z.Bind (l, id, Z.Abst u, t) ->
88 f (W.sh2 u xu t xt x (Z.bind_abst l id)) (Z.bind_abst l id xu tt)
90 let f xu cc = b_type_of (f xu) st cc t in
91 let f xu _ = Z.push "type abst" (f xu) c l id (Z.Abst xu) in
93 | Z.Bind (l, id, Z.Void, t) ->
95 f (W.sh1 t xt x (Z.bind l id Z.Void)) (Z.bind l id Z.Void tt)
97 let f cc = b_type_of f st cc t in
98 Z.push "type void" f c l id Z.Void
100 let f xv vv xt tt = function
103 L.log ZO.specs (succ level) (L.t_items1 "Just scanned" c w);
104 L.unbox (succ level);
106 (* L.warn (Printf.sprintf "Convertible: %b" a); *)
107 if a then f (W.sh2 v xv t xt x Z.appl) (Z.appl xv tt)
108 else error3 c xv vv w
110 ZR.are_convertible f ~si:st.S.si c w vv
112 error1 "not a function" c xt
114 let f xv vv xt tt = ZR.ho_whd (f xv vv xt tt) c tt in
115 let f xv vv = b_type_of (f xv vv) st c t in
119 (* L.warn (Printf.sprintf "Convertible: %b" a); *)
120 if a then f (W.sh2 u xu t xt x Z.cast) xu else error3 c xt tt xu
122 let f xu xt tt = ZR.are_convertible (f xu xt tt) ~si:st.S.si c xu tt in
123 let f xu _ = b_type_of (f xu) st c t in
126 and type_of f st c x =
127 let f t u = L.unbox level; f t u in
128 L.box level; b_type_of f st c x