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
59 | B.Bind (l, id, B.Abst w, t) ->
61 let f tt = f (S.sh2 w ww t tt x (B.bind_abst l id)) in
65 | B.Bind (l, id, B.Abbr v, t) ->
67 let f tt = f (S.sh2 v vv t tt x (B.bind_abbr l id)) in
71 | B.Bind (l, id, B.Void, t) ->
72 let f tt = f (S.sh1 t tt x (B.bind l id B.Void)) in
77 let add_coercions f = C.list_fold_left f add_coercion
79 (* Interface functions ******************************************************)
81 let rec b_type_of f g c x =
82 L.log O.specs level (L.ct_items1 "Now checking" c x);
85 let f h = f x (B.Sort h) in H.apply f g h
88 | Some (_, B.Abst w) -> f x w
89 | Some (_, B.Abbr (B.Cast (w, v))) -> f x w
90 | Some (_, B.Abbr _) -> assert false
92 error1 "reference to excluded variable" c x
94 error1 "variable not found" c x
99 | _, _, B.Abst w -> f x w
100 | _, _, B.Abbr (B.Cast (w, v)) -> f x w
101 | _, _, B.Abbr _ -> assert false
103 error1 "reference to excluded object" c x
106 | B.Bind (l, id, B.Abbr v, t) ->
108 f (S.sh2 v xv t xt x (B.bind_abbr l id)) (B.bind_abbr l id xv tt)
110 let f xv cc = b_type_of (f xv) g cc t in
111 let f xv = B.push (f xv) c l id (B.Abbr xv) in
112 let f xv vv = match xv with
114 | _ -> f (B.Cast (vv, xv))
117 | B.Bind (l, id, B.Abst u, t) ->
119 f (S.sh2 u xu t xt x (B.bind_abst l id)) (B.bind_abst l id xu tt)
121 let f xu cc = b_type_of (f xu) g cc t in
122 let f xu _ = B.push (f xu) c l id (B.Abst xu) in
124 | B.Bind (l, id, B.Void, t) ->
126 f (S.sh1 t xt x (B.bind l id B.Void)) (B.bind l id B.Void tt)
128 let f cc = b_type_of f g cc t in
129 B.push f c l id B.Void
131 let h xv vv xt tt cc = function
132 | R.Sort _ -> error1 "not a function" c xt
135 L.log O.specs (succ level) (L.ct_items1 "Just scanned" cc w);
136 L.unbox (succ level);
138 | Some l -> f (S.sh2 v xv t xt x B.appl) (B.appl xv tt)
139 | None -> error2 "the term" cc xv "must be of type" cc w
141 R.are_convertible f cc w vv
143 let f xv vv xt = function
144 (* inserting a missing "modus ponens" *)
145 | B.Appl (y2, B.Appl (y1, B.GRef uri)) when U.eq uri I.imp ->
146 b_type_of f g c (mk_gref I.mp [y1; y2; xv; xt])
147 | B.Appl (y1, B.GRef uri) when U.eq uri I.not ->
148 b_type_of f g c (mk_gref I.mp [y1; B.GRef I.con; xv; xt])
149 | tt -> R.ho_whd (h xv vv xt tt) c tt
151 let f xv vv = b_type_of (f xv vv) g c t in
154 let f xu xt = function
155 | Some [] -> f (S.sh2 u xu t xt x B.cast) xu
157 let f xt = type_of f g c (S.sh2 u xu t xt x B.cast) in
159 | None -> error2 "the term" c xt "must be of type" c xu
161 let f xu xt tt = R.are_convertible (f xu xt) c xu tt in
162 let f xu _ = b_type_of (f xu) g c t in
165 and type_of f g c x =
166 let f t u = L.unbox level; f t u in
167 L.box level; b_type_of f g c x