Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/typechecker/check-expr.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/typechecker/check-expr.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3422 - (view) (download)

1 : jhr 3396 (* check-expr.sml
2 :     *
3 :     * The typechecker for expressions.
4 :     *
5 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
6 :     *
7 :     * COPYRIGHT (c) 2015 The University of Chicago
8 :     * All rights reserved.
9 :     *)
10 :    
11 :     structure CheckExpr : sig
12 :    
13 : jhr 3407 (* type check an expression *)
14 :     val check : Env.t * Env.context * ParseTree.expr -> (AST.expr * Types.ty)
15 : jhr 3396
16 : jhr 3410 (* type check a list of expressions *)
17 :     val checkList : Env.t * Env.context * ParseTree.expr list -> (AST.expr list * Types.ty list)
18 :    
19 :     (* type check a dimension that is given by a constant expression *)
20 :     val checkDim : Env.t * Env.context * ParseTree.expr -> IntLit.t option
21 :    
22 :     (* type check a tensor shape, where the dimensions are given by constant expressions *)
23 :     val checkShape : Env.t * Env.context * ParseTree.expr list -> Types.shape
24 :    
25 : jhr 3407 (* `resolveOverload (cxt, rator, tys, args, candidates)` resolves the application of
26 :     * the overloaded operator `rator` to `args`, where `tys` are the types of the arguments
27 :     * and `candidates` is the list of candidate definitions.
28 :     *)
29 :     val resolveOverload : Env.context * Atom.atom * Types.ty list * AST.expr list * Var.t list
30 :     -> (AST.expr * Types.ty)
31 :    
32 : jhr 3396 end = struct
33 :    
34 :     structure PT = ParseTree
35 :     structure L = Literal
36 :     structure E = Env
37 :     structure Ty = Types
38 :     structure BV = BasisVars
39 : jhr 3405 structure TU = TypeUtil
40 : jhr 3396
41 :     (* an expression to return when there is a type error *)
42 : jhr 3405 val bogusExp = AST.E_Lit(L.Int 0)
43 :     val bogusExpTy = (bogusExp, Ty.T_Error)
44 : jhr 3396
45 : jhr 3405 fun err arg = (TypeError.error arg; bogusExpTy)
46 : jhr 3396 val warn = TypeError.warning
47 :    
48 : jhr 3402 datatype token = datatype TypeError.token
49 : jhr 3396
50 : jhr 3407 (* mark a variable use with its location *)
51 : jhr 3413 fun useVar (cxt : Env.context, x) = (x, #2 cxt)
52 : jhr 3407
53 :     (* resolve overloading: we use a simple scheme that selects the first operator in the
54 :     * list that matches the argument types.
55 :     *)
56 :     fun resolveOverload (_, rator, _, _, []) = raise Fail(concat[
57 :     "resolveOverload: \"", Atom.toString rator, "\" has no candidates"
58 :     ])
59 :     | resolveOverload (cxt, rator, argTys, args, candidates) = let
60 :     (* FIXME: we could be more efficient by just checking for coercion matchs the first pass
61 :     * and remembering those that are not pure EQ matches.
62 :     *)
63 :     (* try to match candidates while allowing type coercions *)
64 :     fun tryMatchCandidates [] = err(cxt, [
65 :     S "unable to resolve overloaded operator ", A rator, S "\n",
66 :     S " argument type is: ", TYS argTys, S "\n"
67 :     ])
68 :     | tryMatchCandidates (x::xs) = let
69 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf x)
70 :     in
71 :     case Unify.tryMatchArgs (domTy, args, argTys)
72 :     of SOME args' => (AST.E_Prim(x, tyArgs, args', rngTy), rngTy)
73 :     | NONE => tryMatchCandidates xs
74 :     (* end case *)
75 :     end
76 :     fun tryCandidates [] = tryMatchCandidates candidates
77 :     | tryCandidates (x::xs) = let
78 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf x)
79 :     in
80 :     if Unify.tryEqualTypes(domTy, argTys)
81 :     then (AST.E_Prim(x, tyArgs, args, rngTy), rngTy)
82 :     else tryCandidates xs
83 :     end
84 :     in
85 :     tryCandidates candidates
86 :     end
87 :    
88 : jhr 3396 (* check the type of a literal *)
89 :     fun checkLit lit = (case lit
90 :     of (L.Int _) => (AST.E_Lit lit, Ty.T_Int)
91 :     | (L.Real _) => (AST.E_Lit lit, Ty.realTy)
92 :     | (L.String s) => (AST.E_Lit lit, Ty.T_String)
93 :     | (L.Bool _) => (AST.E_Lit lit, Ty.T_Bool)
94 :     (* end case *))
95 :    
96 : jhr 3405 (* type check a dot product, which has the constraint:
97 :     * ALL[sigma1, d1, sigma2] . tensor[sigma1, d1] * tensor[d1, sigma2] -> tensor[sigma1, sigma2]
98 :     * and similarly for fields.
99 :     *)
100 :     fun chkInnerProduct (cxt, e1, ty1, e2, ty2) = let
101 :     (* check the shape of the two arguments to verify that the inner constraint matches *)
102 :     fun chkShape (Ty.Shape(dd1 as _::_), Ty.Shape(d2::dd2)) = let
103 :     val (dd1, d1) = let
104 :     fun splitLast (prefix, [d]) = (List.rev prefix, d)
105 :     | splitLast (prefix, d::dd) = splitLast (d::prefix, dd)
106 :     | splitLast (_, []) = raise Fail "impossible"
107 :     in
108 :     splitLast ([], dd1)
109 :     end
110 :     in
111 :     if Unify.equalDim(d1, d2)
112 :     then SOME(Ty.Shape(dd1@dd2))
113 :     else NONE
114 :     end
115 :     | chkShape _ = NONE
116 :     fun error () = err (cxt, [
117 :     S "type error for arguments of binary operator '•'\n",
118 :     S " found: ", TYS[ty1, ty2], S "\n"
119 :     ])
120 :     in
121 :     case (TU.prune ty1, TU.prune ty2)
122 :     (* tensor * tensor inner product *)
123 :     of (Ty.T_Tensor s1, Ty.T_Tensor s2) => (case chkShape(s1, s2)
124 :     of SOME shp => let
125 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_inner_tt)
126 :     val resTy = Ty.T_Tensor shp
127 :     in
128 :     if Unify.equalTypes(domTy, [ty1, ty2]) andalso Unify.equalType(rngTy, resTy)
129 : jhr 3407 then (AST.E_Prim(BV.op_inner_tt, tyArgs, [e1, e2], rngTy), rngTy)
130 : jhr 3405 else error()
131 :     end
132 :     | NONE => error()
133 :     (* end case *))
134 :     (* tensor * field inner product *)
135 :     | (Ty.T_Tensor s1, Ty.T_Field{diff, dim, shape=s2}) => (case chkShape(s1, s2)
136 :     of SOME shp => let
137 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_inner_tf)
138 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
139 :     in
140 :     if Unify.equalTypes(domTy, [ty1, ty2])
141 :     andalso Unify.equalType(rngTy, resTy)
142 : jhr 3407 then (AST.E_Prim(BV.op_inner_tf, tyArgs, [e1, e2], rngTy), rngTy)
143 : jhr 3405 else error()
144 :     end
145 :     | NONE => error()
146 :     (* end case *))
147 :     (* field * tensor inner product *)
148 :     | (Ty.T_Field{diff, dim, shape=s1}, Ty.T_Tensor s2) => (case chkShape(s1, s2)
149 :     of SOME shp => let
150 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_inner_ft)
151 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
152 :     in
153 :     if Unify.equalTypes(domTy, [ty1, ty2])
154 :     andalso Unify.equalType(rngTy, resTy)
155 : jhr 3407 then (AST.E_Prim(BV.op_inner_ft, tyArgs, [e1, e2], rngTy), rngTy)
156 : jhr 3405 else error()
157 :     end
158 :     | NONE => error()
159 :     (* end case *))
160 :     (* field * field inner product *)
161 :     | (Ty.T_Field{diff=k1, dim=dim1, shape=s1}, Ty.T_Field{diff=k2, dim=dim2, shape=s2}) => (
162 :     case chkShape(s1, s2)
163 :     of SOME shp => let
164 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_inner_ff)
165 :     val resTy = Ty.T_Field{diff=k1, dim=dim1, shape=shp}
166 :     in
167 :     (* FIXME: the resulting differentiation should be the minimum of k1 and k2 *)
168 :     if Unify.equalDim(dim1, dim2)
169 :     andalso Unify.equalTypes(domTy, [ty1, ty2])
170 :     andalso Unify.equalType(rngTy, resTy)
171 : jhr 3407 then (AST.E_Prim(BV.op_inner_ff, tyArgs, [e1, e2], rngTy), rngTy)
172 : jhr 3405 else error()
173 :     end
174 :     | NONE => error()
175 :     (* end case *))
176 :     | (ty1, ty2) => error()
177 :     (* end case *)
178 :     end
179 :    
180 :     (* type check a colon product, which has the constraint:
181 :     * ALL[sigma1, d1, d2, sigma2] . tensor[sigma1, d1, d2] * tensor[d2, d1, sigma2] -> tensor[sigma1, sigma2]
182 :     * and similarly for fields.
183 :     *)
184 :     fun chkColonProduct (cxt, e1, ty1, e2, ty2) = let
185 :     (* check the shape of the two arguments to verify that the inner constraint matches *)
186 :     fun chkShape (Ty.Shape(dd1 as _::_::_), Ty.Shape(d21::d22::dd2)) = let
187 :     val (dd1, d11, d12) = let
188 :     fun splitLast2 (prefix, [d1, d2]) = (List.rev prefix, d1, d2)
189 :     | splitLast2 (prefix, d::dd) = splitLast2 (d::prefix, dd)
190 :     | splitLast2 (_, []) = raise Fail "impossible"
191 :     in
192 :     splitLast2 ([], dd1)
193 :     end
194 :     in
195 :     if Unify.equalDim(d11, d21) andalso Unify.equalDim(d12, d22)
196 :     then SOME(Ty.Shape(dd1@dd2))
197 :     else NONE
198 :     end
199 :     | chkShape _ = NONE
200 :     fun error () = err (cxt, [
201 :     S "type error for arguments of binary operator \":\"\n",
202 :     S " found: ", TYS[ty1, ty2], S "\n"
203 :     ])
204 :     in
205 :     case (TU.prune ty1, TU.prune ty2)
206 :     (* tensor * tensor colon product *)
207 :     of (Ty.T_Tensor s1, Ty.T_Tensor s2) => (case chkShape(s1, s2)
208 :     of SOME shp => let
209 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_colon_tt)
210 :     val resTy = Ty.T_Tensor shp
211 :     in
212 :     if Unify.equalTypes(domTy, [ty1, ty2])
213 :     andalso Unify.equalType(rngTy, resTy)
214 : jhr 3407 then (AST.E_Prim(BV.op_colon_tt, tyArgs, [e1, e2], rngTy), rngTy)
215 : jhr 3405 else error()
216 :     end
217 :     | NONE => error()
218 :     (* end case *))
219 :     (* field * tensor colon product *)
220 :     | (Ty.T_Field{diff, dim, shape=s1}, Ty.T_Tensor s2) => (case chkShape(s1, s2)
221 :     of SOME shp => let
222 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_colon_ft)
223 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
224 :     in
225 :     if Unify.equalTypes(domTy, [ty1, ty2]) andalso Unify.equalType(rngTy, resTy)
226 : jhr 3407 then (AST.E_Prim(BV.op_colon_ft, tyArgs, [e1, e2], rngTy), rngTy)
227 : jhr 3405 else error()
228 :     end
229 :     | NONE => error()
230 :     (* end case *))
231 :     (* tensor * field colon product *)
232 :     | (Ty.T_Tensor s1, Ty.T_Field{diff=diff, dim=dim, shape=s2}) => (case chkShape(s1, s2)
233 :     of SOME shp => let
234 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_colon_tf)
235 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
236 :     in
237 :     if Unify.equalTypes(domTy, [ty1, ty2]) andalso Unify.equalType(rngTy, resTy)
238 : jhr 3407 then (AST.E_Prim(BV.op_colon_tf, tyArgs, [e1, e2], rngTy), rngTy)
239 : jhr 3405 else error()
240 :     end
241 :     | NONE => error()
242 :     (* end case *))
243 :     (* field * field colon product *)
244 :     | (Ty.T_Field{diff=k1, dim=dim1, shape=s1}, Ty.T_Field{diff=k2, dim=dim2, shape=s2}) => (
245 :     case chkShape(s1, s2)
246 :     of SOME shp => let
247 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_colon_ff)
248 :     val resTy = Ty.T_Field{diff=k1, dim=dim1, shape=shp}
249 :     in
250 :     (* FIXME: the resulting differentiation should be the minimum of k1 and k2 *)
251 :     if Unify.equalDim(dim1, dim2)
252 :     andalso Unify.equalTypes(domTy, [ty1, ty2])
253 :     andalso Unify.equalType(rngTy, resTy)
254 : jhr 3407 then (AST.E_Prim(BV.op_colon_ff, tyArgs, [e1, e2], rngTy), rngTy)
255 : jhr 3405 else error()
256 :     end
257 :     | NONE => error()
258 :     (* end case *))
259 :     | (ty1, ty2) => error()
260 :     (* end case *)
261 :     end
262 :    
263 : jhr 3396 (* check the type of an expression *)
264 :     fun check (env, cxt, e) = (case e
265 : jhr 3405 of PT.E_Mark m => check (E.withEnvAndContext (env, cxt, m))
266 : jhr 3396 | PT.E_Cond(e1, cond, e2) => let
267 :     val eTy1 = check (env, cxt, e1)
268 :     val eTy2 = check (env, cxt, e2)
269 :     in
270 : jhr 3402 case check(env, cxt, cond)
271 : jhr 3396 of (cond', Ty.T_Bool) => (case Util.coerceType2(eTy1, eTy2)
272 : jhr 3405 of SOME(e1', e2', ty) => (AST.E_Cond(cond', e1', e2', ty), ty)
273 : jhr 3396 | NONE => err (cxt, [
274 :     S "types do not match in conditional expression\n",
275 :     S " true branch: ", TY(#2 eTy1), S "\n",
276 :     S " false branch: ", TY(#2 eTy2)
277 :     ])
278 : jhr 3398 (* end case *))
279 : jhr 3396 | (_, ty') => err (cxt, [S "expected bool type, but found ", TY ty'])
280 :     (* end case *)
281 :     end
282 :     | PT.E_Range(e1, e2) => (case (check (env, cxt, e1), check (env, cxt, e2))
283 :     of ((e1', Ty.T_Int), (e2', Ty.T_Int)) => let
284 : jhr 3398 val resTy = Ty.T_Sequence(Ty.T_Int, NONE)
285 : jhr 3396 in
286 : jhr 3407 (AST.E_Prim(BV.range, [], [e1', e2'], resTy), resTy)
287 : jhr 3396 end
288 :     | ((_, Ty.T_Int), (_, ty2)) =>
289 :     err (cxt, [S "expected type 'int' on rhs of '..', but found ", TY ty2])
290 :     | ((_, ty1), (_, Ty.T_Int)) =>
291 :     err (cxt, [S "expected type 'int' on lhs of '..', but found ", TY ty1])
292 :     | ((_, ty1), (_, ty2)) => err (cxt, [
293 :     S "arguments of '..' must have type 'int', found ",
294 :     TY ty1, S " and ", TY ty2
295 :     ])
296 :     (* end case *))
297 :     | PT.E_OrElse(e1, e2) =>
298 :     checkCondOp (env, cxt, e1, "||", e2,
299 :     fn (e1', e2') => AST.E_Cond(e1', AST.E_Lit(L.Bool true), e2', Ty.T_Bool))
300 :     | PT.E_AndAlso(e1, e2) =>
301 :     checkCondOp (env, cxt, e1, "&&", e2,
302 :     fn (e1', e2') => AST.E_Cond(e1', e2', AST.E_Lit(L.Bool false), Ty.T_Bool))
303 :     | PT.E_BinOp(e1, rator, e2) => let
304 :     val (e1', ty1) = check (env, cxt, e1)
305 :     val (e2', ty2) = check (env, cxt, e2)
306 :     in
307 :     if Atom.same(rator, BasisNames.op_dot)
308 : jhr 3405 then chkInnerProduct (cxt, e1', ty1, e2', ty2)
309 : jhr 3396 else if Atom.same(rator, BasisNames.op_colon)
310 : jhr 3405 then chkColonProduct (cxt, e1', ty1, e2', ty2)
311 :     else (case Env.findFunc (env, rator)
312 : jhr 3396 of Env.PrimFun[rator] => let
313 : jhr 3405 val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf rator)
314 : jhr 3396 in
315 : jhr 3402 case Unify.matchArgs(domTy, [e1', e2'], [ty1, ty2])
316 : jhr 3407 of SOME args => (AST.E_Prim(rator, tyArgs, args, rngTy), rngTy)
317 : jhr 3396 | NONE => err (cxt, [
318 : jhr 3418 S "type error for binary operator ", V rator, S "\n",
319 : jhr 3396 S " expected: ", TYS domTy, S "\n",
320 :     S " but found: ", TYS[ty1, ty2]
321 :     ])
322 :     (* end case *)
323 :     end
324 :     | Env.PrimFun ovldList =>
325 :     resolveOverload (cxt, rator, [ty1, ty2], [e1', e2'], ovldList)
326 :     | _ => raise Fail "impossible"
327 :     (* end case *))
328 :     end
329 : jhr 3398 | PT.E_UnaryOp(rator, e) => let
330 : jhr 3405 val eTy = check(env, cxt, e)
331 : jhr 3398 in
332 : jhr 3405 case Env.findFunc (env, rator)
333 : jhr 3398 of Env.PrimFun[rator] => let
334 : jhr 3405 val (tyArgs, Ty.T_Fun([domTy], rngTy)) = TU.instantiate(Var.typeOf rator)
335 : jhr 3398 in
336 : jhr 3405 case Util.coerceType (domTy, eTy)
337 : jhr 3410 of SOME e' => (AST.E_Prim(rator, tyArgs, [e'], rngTy), rngTy)
338 : jhr 3398 | NONE => err (cxt, [
339 : jhr 3418 S "type error for unary operator ", V rator, S "\n",
340 : jhr 3398 S " expected: ", TY domTy, S "\n",
341 : jhr 3405 S " but found: ", TY (#2 eTy)
342 : jhr 3398 ])
343 :     (* end case *)
344 :     end
345 : jhr 3405 | Env.PrimFun ovldList => resolveOverload (cxt, rator, [#2 eTy], [#1 eTy], ovldList)
346 : jhr 3398 | _ => raise Fail "impossible"
347 :     (* end case *)
348 :     end
349 : jhr 3407 | PT.E_Apply(e, args) => let
350 :     fun stripMark (_, PT.E_Mark{span, tree}) = stripMark(span, tree)
351 :     | stripMark (span, e) = (span, e)
352 :     val (args, tys) = checkList (env, cxt, args)
353 :     fun appTyError (f, paramTys, argTys) = err(cxt, [
354 :     S "type error in application of ", V f, S "\n",
355 :     S " expected: ", TYS paramTys, S "\n",
356 :     S " but found: ", TYS argTys
357 :     ])
358 :     fun checkPrimApp f = if Var.isPrim f
359 :     then (case TU.instantiate(Var.typeOf f)
360 :     of (tyArgs, Ty.T_Fun(domTy, rngTy)) => (
361 :     case Unify.matchArgs (domTy, args, tys)
362 :     of SOME args => (AST.E_Prim(f, tyArgs, args, rngTy), rngTy)
363 :     | NONE => appTyError (f, domTy, tys)
364 :     (* end case *))
365 :     | _ => err(cxt, [S "application of non-function/field ", V f])
366 :     (* end case *))
367 :     else raise Fail "unexpected user function"
368 :     (* check the application of a user-defined function *)
369 :     fun checkFunApp (cxt, f) = if Var.isPrim f
370 :     then raise Fail "unexpected primitive function"
371 :     else (case Var.monoTypeOf f
372 :     of Ty.T_Fun(domTy, rngTy) => (
373 :     case Unify.matchArgs (domTy, args, tys)
374 :     of SOME args => (AST.E_Apply(useVar(cxt, f), args, rngTy), rngTy)
375 :     | NONE => appTyError (f, domTy, tys)
376 :     (* end case *))
377 :     | _ => err(cxt, [S "application of non-function/field ", V f])
378 :     (* end case *))
379 :     fun checkFieldApp (e1', ty1) = (case (args, tys)
380 :     of ([e2'], [ty2]) => let
381 :     val (tyArgs, Ty.T_Fun([fldTy, domTy], rngTy)) =
382 :     TU.instantiate(Var.typeOf BV.op_probe)
383 :     fun tyError () = err (cxt, [
384 :     S "type error for field application\n",
385 :     S " expected: ", TYS[fldTy, domTy], S "\n",
386 :     S " but found: ", TYS[ty1, ty2]
387 :     ])
388 :     in
389 :     if Unify.equalType(fldTy, ty1)
390 :     then (case Util.coerceType(domTy, (e2', ty2))
391 : jhr 3410 of SOME e2' => (AST.E_Prim(BV.op_probe, tyArgs, [e1', e2'], rngTy), rngTy)
392 : jhr 3407 | NONE => tyError()
393 :     (* end case *))
394 :     else tyError()
395 :     end
396 :     | _ => err(cxt, [S "badly formed field application"])
397 :     (* end case *))
398 :     in
399 :     case stripMark(#2 cxt, e)
400 :     of (span, PT.E_Var f) => (case Env.findVar (env, f)
401 :     of SOME f' => checkFieldApp (
402 :     AST.E_Var(useVar((#1 cxt, span), f')),
403 :     Var.monoTypeOf f')
404 :     | NONE => (case Env.findFunc (env, f)
405 :     of Env.PrimFun[] => err(cxt, [S "unknown function ", A f])
406 :     | Env.PrimFun[f'] => checkPrimApp f'
407 :     | Env.PrimFun ovldList =>
408 :     resolveOverload ((#1 cxt, span), f, tys, args, ovldList)
409 :     | Env.UserFun f' => checkFunApp((#1 cxt, span), f')
410 :     (* end case *))
411 :     (* end case *))
412 :     | _ => checkFieldApp (check (env, cxt, e))
413 :     (* end case *)
414 :     end
415 : jhr 3398 | PT.E_Subscript(e, indices) => (case (check(env, cxt, e), indices)
416 :     of ((e', Ty.T_Sequence(elemTy, _)), [SOME e2]) => raise Fail "FIXME"
417 :     | ((e', Ty.T_Tensor shape), _) => raise Fail "FIXME"
418 :     | ((_, ty), _) => err(cxt, [
419 :     S "expected sequence or tensor type for object of subscripting, but found",
420 :     TY ty
421 :     ])
422 :     (* end case *))
423 :     | PT.E_Select(e, field) => (case check(env, cxt, e)
424 : jhr 3405 of (e', Ty.T_Named strand) => (case Env.findStrand(env, strand)
425 :     of SOME sEnv => (case StrandEnv.findStateVar(sEnv, field)
426 :     of SOME x' => let
427 :     val ty = Var.monoTypeOf x'
428 :     in
429 : jhr 3407 (AST.E_Select(e', useVar(cxt, x')), ty)
430 : jhr 3405 end
431 :     | NONE => err(cxt, [
432 : jhr 3418 S "strand ", A strand,
433 :     S " does not have state variable ", A field
434 : jhr 3405 ])
435 :     (* end case *))
436 : jhr 3418 | NONE => err(cxt, [S "unknown strand ", A strand])
437 : jhr 3398 (* end case *))
438 :     | (_, ty) => err (cxt, [
439 :     S "expected strand type, but found ", TY ty,
440 : jhr 3418 S " in selection of ", A field
441 : jhr 3398 ])
442 :     (* end case *))
443 : jhr 3396 | PT.E_Real e => (case check (env, cxt, e)
444 :     of (e', Ty.T_Int) =>
445 : jhr 3407 (AST.E_Prim(BV.i2r, [], [e'], Ty.realTy), Ty.realTy)
446 : jhr 3396 | (_, ty) => err(cxt, [
447 :     S "argument of 'real' must have type 'int', but found ",
448 :     TY ty
449 :     ])
450 :     (* end case *))
451 :     | PT.E_Load nrrd => let
452 : jhr 3418 val (tyArgs, Ty.T_Fun(_, rngTy)) = TU.instantiate(Var.typeOf(BV.fn_load))
453 : jhr 3396 in
454 : jhr 3407 case chkStringConstExpr (env, cxt, nrrd)
455 :     of SOME nrrd => (AST.E_LoadNrrd(tyArgs, nrrd, rngTy), rngTy)
456 :     | NONE => (bogusExp, rngTy)
457 :     (* end case *)
458 : jhr 3396 end
459 :     | PT.E_Image nrrd => let
460 : jhr 3418 val (tyArgs, Ty.T_Fun(_, rngTy)) = TU.instantiate(Var.typeOf(BV.fn_image))
461 : jhr 3396 in
462 : jhr 3407 case chkStringConstExpr (env, cxt, nrrd)
463 :     of SOME nrrd => (AST.E_LoadNrrd(tyArgs, nrrd, rngTy), rngTy)
464 :     | NONE => (bogusExp, rngTy)
465 :     (* end case *)
466 : jhr 3396 end
467 : jhr 3405 | PT.E_Var x => (case E.findVar (env, x)
468 : jhr 3407 of SOME x' => (AST.E_Var(useVar(cxt, x')), Var.monoTypeOf x')
469 : jhr 3396 | NONE => err(cxt, [S "undeclared variable ", A x])
470 :     (* end case *))
471 : jhr 3421 | PT.E_Kernel(kern, dim) => (case E.findVar (env, kern)
472 :     of SOME kern' => (case Var.monoTypeOf kern'
473 :     of ty as Ty.T_Kernel(Ty.DiffConst k) => let
474 :     val k' = Int.fromLarge dim handle Overflow => 1073741823
475 :     val e = AST.E_Var(useVar(cxt, kern'))
476 :     in
477 :     if (k = k')
478 :     then (e, ty)
479 :     else let
480 :     val ty' = Ty.T_Kernel(Ty.DiffConst k')
481 :     in
482 :     (AST.E_Coerce{srcTy = ty, dstTy = ty', e = e}, ty')
483 :     end
484 :     end
485 :     | _ => err(cxt, [S "expected kernel, but found ", S(Var.kindToString kern')])
486 :     (* end case *))
487 :     | NONE => err(cxt, [S "unknown kernel ", A kern])
488 :     (* end case *))
489 : jhr 3396 | PT.E_Lit lit => checkLit lit
490 :     | PT.E_Id d => let
491 :     val (tyArgs, Ty.T_Fun(_, rngTy)) =
492 : jhr 3405 TU.instantiate(Var.typeOf(BV.identity))
493 : jhr 3396 in
494 : jhr 3407 if Unify.equalType(Ty.T_Tensor(checkShape(env, cxt, [d, d])), rngTy)
495 :     then (AST.E_Prim(BV.identity, tyArgs, [], rngTy), rngTy)
496 : jhr 3396 else raise Fail "impossible"
497 :     end
498 :     | PT.E_Zero dd => let
499 :     val (tyArgs, Ty.T_Fun(_, rngTy)) =
500 : jhr 3405 TU.instantiate(Var.typeOf(BV.zero))
501 : jhr 3396 in
502 : jhr 3407 if Unify.equalType(Ty.T_Tensor(checkShape(env, cxt, dd)), rngTy)
503 :     then (AST.E_Prim(BV.zero, tyArgs, [], rngTy), rngTy)
504 : jhr 3396 else raise Fail "impossible"
505 :     end
506 :     | PT.E_NaN dd => let
507 :     val (tyArgs, Ty.T_Fun(_, rngTy)) =
508 : jhr 3405 TU.instantiate(Var.typeOf(BV.nan))
509 : jhr 3396 in
510 : jhr 3407 if Unify.equalType(Ty.T_Tensor(checkShape(env, cxt, dd)), rngTy)
511 :     then (AST.E_Prim(BV.nan, tyArgs, [], rngTy), rngTy)
512 : jhr 3396 else raise Fail "impossible"
513 :     end
514 : jhr 3422 | PT.E_Sequence exps => (case checkList (env, cxt, exps)
515 :     (* FIXME: need kind for concrete types here! *)
516 :     of ([], _) => let
517 :     val ty = Ty.T_Sequence(Ty.T_Var(MetaVar.newTyVar()), SOME(Ty.DimConst 0))
518 :     in
519 :     (AST.E_Seq([], ty), ty)
520 :     end
521 :     | (args, tys) => (case Util.coerceTypes(List.map TU.pruneHead tys)
522 :     of SOME ty => if TU.isValueType ty
523 :     then let
524 :     fun doExp eTy = valOf(Util.coerceType (ty, eTy))
525 :     val resTy = Ty.T_Sequence(ty, SOME(Ty.DimConst(List.length args)))
526 :     val args = ListPair.map doExp (args, tys)
527 :     in
528 :     (AST.E_Seq(args, resTy), resTy)
529 :     end
530 :     else err(cxt, [S "sequence expression of non-value argument type"])
531 :     | NONE => err(cxt, [S "arguments of sequence expression must have same type"])
532 :     (* end case *))
533 :     (* end case *))
534 : jhr 3398 | PT.E_SeqComp comp => raise Fail "FIXME"
535 : jhr 3396 | PT.E_Cons args => let
536 :     (* Note that we are guaranteed that args is non-empty *)
537 :     val (args, tys) = checkList (env, cxt, args)
538 :     (* extract the first non-error type in tys *)
539 :     val ty = (case List.find (fn Ty.T_Error => false | _ => true) tys
540 :     of NONE => Ty.T_Error
541 :     | SOME ty => ty
542 :     (* end case *))
543 : jhr 3405 (* process the arguments checking that they all have the expected type *)
544 :     fun chkArgs (ty, shape) = let
545 :     val Ty.Shape dd = TU.pruneShape shape (* NOTE: this may fail if we allow user polymorphism *)
546 :     val resTy = Ty.T_Tensor(Ty.Shape(Ty.DimConst(List.length args) :: dd))
547 :     fun chkArgs (arg::args, argTy::tys, args') = (
548 :     case Util.coerceType(ty, (arg, argTy))
549 : jhr 3410 of SOME arg' => chkArgs (args, tys, arg'::args')
550 : jhr 3405 | NONE => (
551 :     TypeError.error(cxt, [
552 :     S "arguments of tensor construction must have same type"
553 :     ]);
554 :     chkArgs (args, tys, bogusExp::args'))
555 :     (* end case *))
556 : jhr 3408 | chkArgs (_, _, args') = (AST.E_Tensor(List.rev args', resTy), resTy)
557 : jhr 3405 in
558 :     chkArgs (args, tys, [])
559 :     end
560 : jhr 3396 in
561 : jhr 3405 case TU.pruneHead ty
562 : jhr 3407 of Ty.T_Int => chkArgs(Ty.realTy, Ty.Shape[]) (* coerce integers to reals *)
563 : jhr 3405 | ty as Ty.T_Tensor shape => chkArgs(ty, shape)
564 : jhr 3396 | _ => err(cxt, [S "Invalid argument type for tensor construction"])
565 :     (* end case *)
566 :     end
567 :     | PT.E_Deprecate(msg, e) => (
568 :     warn (cxt, [S msg]);
569 : jhr 3402 check (env, cxt, e))
570 : jhr 3396 (* end case *))
571 :    
572 :     (* check a conditional operator (e.g., || or &&) *)
573 :     and checkCondOp (env, cxt, e1, rator, e2, mk) = (
574 :     case (check(env, cxt, e1), check(env, cxt, e2))
575 :     of ((e1', Ty.T_Bool), (e2', Ty.T_Bool)) => (mk(e1', e2'), Ty.T_Bool)
576 :     | ((_, Ty.T_Bool), (_, ty2)) =>
577 : jhr 3405 err (cxt, [S "expected type 'bool' on rhs of '", S rator, S "', but found ", TY ty2])
578 : jhr 3396 | ((_, ty1), (_, Ty.T_Bool)) =>
579 : jhr 3405 err (cxt, [S "expected type 'bool' on lhs of '", S rator, S "', but found ", TY ty1])
580 : jhr 3396 | ((_, ty1), (_, ty2)) => err (cxt, [
581 : jhr 3405 S "arguments of '", S rator, S "' must have type 'bool', but found ",
582 : jhr 3396 TY ty1, S " and ", TY ty2
583 :     ])
584 :     (* end case *))
585 :    
586 :     (* typecheck a list of expressions returning a list of AST expressions and a list
587 :     * of the types of the expressions.
588 :     *)
589 :     and checkList (env, cxt, exprs) = let
590 :     fun chk (e, (es, tys)) = let
591 : jhr 3402 val (e, ty) = check (env, cxt, e)
592 : jhr 3396 in
593 :     (e::es, ty::tys)
594 :     end
595 :     in
596 :     List.foldr chk ([], []) exprs
597 :     end
598 :    
599 : jhr 3407 (* check a string that is specified as a constant expression *)
600 :     and chkStringConstExpr (env, cxt, PT.E_Mark m) =
601 :     chkStringConstExpr (E.withEnvAndContext (env, cxt, m))
602 :     | chkStringConstExpr (env, cxt, e) = (case check (env, cxt, e)
603 :     of (e', Ty.T_String) => (case ConstExpr.eval (cxt, e')
604 :     of SOME(ConstExpr.String s) => SOME s
605 :     | SOME(ConstExpr.Expr e) => raise Fail "FIXME"
606 :     | NONE => NONE
607 :     | _ => raise Fail "impossible: wrong type for constant expr"
608 :     (* end case *))
609 :     | (_, ty) => (
610 :     TypeError.error (cxt, [
611 :     S "expected constant expression of type 'string', but found '",
612 :     TY ty, S "'"
613 :     ]);
614 :     NONE)
615 :     (* end case *))
616 :    
617 :     (* check a dimension that is given by a constant expression *)
618 :     and checkDim (env, cxt, dim) = (case check (env, cxt, dim)
619 :     of (e', Ty.T_Int) => (case ConstExpr.eval (cxt, e')
620 :     of SOME(ConstExpr.Int d) => SOME d
621 :     | SOME(ConstExpr.Expr e) => (
622 :     TypeError.error (cxt, [S "unable to evaluate constant dimension expression"]);
623 :     NONE)
624 :     | NONE => NONE
625 :     | _ => raise Fail "impossible: wrong type for constant expr"
626 :     (* end case *))
627 :     | (_, ty) => (
628 :     TypeError.error (cxt, [
629 :     S "expected constant expression of type 'int', but found '",
630 :     TY ty, S "'"
631 :     ]);
632 :     NONE)
633 :     (* end case *))
634 :    
635 :     (* check a tensor shape, where the dimensions are given by constant expressions *)
636 :     and checkShape (env, cxt, shape) = let
637 :     fun checkDim' e = (case checkDim (env, cxt, e)
638 :     of SOME d => (
639 :     if (d <= 1)
640 :     then TypeError.error (cxt, [
641 :     S "invalid tensor-shape dimension; must be > 1, but found ",
642 :     S (IntLit.toString d)
643 :     ])
644 :     else ();
645 :     Ty.DimConst(IntInf.toInt d))
646 :     | NONE => Ty.DimConst ~1
647 :     (* end case *))
648 :     in
649 :     Ty.Shape(List.map checkDim' shape)
650 :     end
651 :    
652 : jhr 3396 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0