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 S = BrgSubstitution
21 module R = BrgReduction
23 exception TypeError of R.message
25 (* Internal functions *******************************************************)
29 let message1 st1 m t1 =
30 L.ct_items1 "In the context" m st1 t1
33 let s = s ^ " the term" in
34 L.log R.specs level (message1 s m t)
37 raise (TypeError (message1 s m t))
39 let message3 m t1 t2 ?mu t3 =
40 let sm, st1, st2 = "In the context", "the term", "is of type" in
43 let smu, st3 = "but in the context", "it must be of type" in
44 L.ct_items3 sm m st1 t1 st2 t2 ~sc3:smu ~c3:mu st3 t3
46 let st3 = "but it must be of type" in
47 L.ct_items3 sm m st1 t1 st2 t2 st3 t3
49 let error3 m t1 t2 ?mu t3 =
50 raise (TypeError (message3 m t1 t2 ?mu t3))
52 let assert_convertibility f ~si m u w v =
53 let err _ = error3 m v w u in
54 R.are_convertible err f ~si m u m w
56 let assert_applicability f ~si m u w v =
58 | B.Bind (B.Abst (_, u), _) ->
59 let err _ = error3 m v w ~mu u in
60 R.are_convertible err f ~si mu u m w
61 | _ -> error1 "not a function type" m u
65 let rec b_type_of f ~si g m x =
66 log1 "Now checking" m x;
69 let f h = f x (B.Sort (a, h)) in H.apply f g h
73 S.lift (f x) (succ i) (0) w
74 | B.Abbr (_, B.Cast (_, w, _)) ->
75 S.lift (f x) (succ i) (0) w
76 | B.Abbr _ -> assert false
78 error1 "reference to excluded variable" m x
80 let err _ = error1 "reference to unknown variable" m x in
84 | _, _, B.Abst (_, w) -> f x w
85 | _, _, B.Abbr (_, B.Cast (_, w, _)) -> f x w
86 | _, _, B.Abbr _ -> assert false
88 error1 "reference to excluded object" m x
90 let err _ = error1 "reference to unknown object" m x in
92 | B.Bind (B.Abbr (a, v), t) ->
94 f (A.sh2 v xv t xt x (B.bind_abbr a)) (B.bind_abbr a xv tt)
96 let f xv m = b_type_of (f xv) ~si g m t in
97 let f xv = R.push (f xv) m (B.abbr a xv) in
98 let f xv vv = match xv with
100 | _ -> f (B.Cast ([], vv, xv))
103 | B.Bind (B.Abst (a, u), t) ->
105 f (A.sh2 u xu t xt x (B.bind_abst a)) (B.bind_abst a xu tt)
107 let f xu m = b_type_of (f xu) ~si g m t in
108 let f xu _ = R.push (f xu) m (B.abst a xu) in
110 | B.Bind (B.Void a as b, t) ->
112 f (A.sh1 t xt x (B.bind b)) (B.bind b tt)
114 let f m = b_type_of f ~si g m t in
116 | B.Appl (a, v, t) ->
118 let f _ = f (A.sh2 v xv t xt x (B.appl a)) (B.appl a xv tt) in
119 assert_applicability f ~si m tt vv xv
121 let f xv vv = b_type_of (f xv vv) ~si g m t in
123 | B.Cast (a, u, t) ->
125 let f _ = f (A.sh2 u xu t xt x (B.cast a)) xu in
126 assert_convertibility f ~si m xu tt xt
128 let f xu _ = b_type_of (f xu) ~si g m t in
131 (* Interface functions ******************************************************)
133 and type_of f ?(si=false) g m x =
134 let f t u = L.unbox level; f t u in
135 L.box level; b_type_of f ~si g m x