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 3991 - (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 : jhr 3424 (* type check an iteration expression (i.e., "x 'in' expr"), returning the iterator
20 :     * and the environment extended with a binding for x.
21 :     *)
22 :     val checkIter : Env.t * Env.context * ParseTree.iterator -> ((AST.var * AST.expr) * Env.t)
23 :    
24 : jhr 3410 (* type check a dimension that is given by a constant expression *)
25 :     val checkDim : Env.t * Env.context * ParseTree.expr -> IntLit.t option
26 :    
27 :     (* type check a tensor shape, where the dimensions are given by constant expressions *)
28 :     val checkShape : Env.t * Env.context * ParseTree.expr list -> Types.shape
29 :    
30 : jhr 3407 (* `resolveOverload (cxt, rator, tys, args, candidates)` resolves the application of
31 :     * the overloaded operator `rator` to `args`, where `tys` are the types of the arguments
32 :     * and `candidates` is the list of candidate definitions.
33 :     *)
34 :     val resolveOverload : Env.context * Atom.atom * Types.ty list * AST.expr list * Var.t list
35 :     -> (AST.expr * Types.ty)
36 :    
37 : jhr 3396 end = struct
38 :    
39 :     structure PT = ParseTree
40 :     structure L = Literal
41 :     structure E = Env
42 :     structure Ty = Types
43 :     structure BV = BasisVars
44 : jhr 3405 structure TU = TypeUtil
45 : jhr 3396
46 :     (* an expression to return when there is a type error *)
47 : jhr 3405 val bogusExp = AST.E_Lit(L.Int 0)
48 :     val bogusExpTy = (bogusExp, Ty.T_Error)
49 : jhr 3396
50 : jhr 3405 fun err arg = (TypeError.error arg; bogusExpTy)
51 : jhr 3396 val warn = TypeError.warning
52 :    
53 : jhr 3402 datatype token = datatype TypeError.token
54 : jhr 3396
55 : jhr 3407 (* mark a variable use with its location *)
56 : jhr 3413 fun useVar (cxt : Env.context, x) = (x, #2 cxt)
57 : jhr 3407
58 : jhr 3431 (* strip any marks that enclose an expression and return the span and the expression *)
59 :     fun stripMark (_, PT.E_Mark{span, tree}) = stripMark(span, tree)
60 :     | stripMark (span, e) = (span, e)
61 :    
62 : jhr 3407 (* resolve overloading: we use a simple scheme that selects the first operator in the
63 :     * list that matches the argument types.
64 :     *)
65 :     fun resolveOverload (_, rator, _, _, []) = raise Fail(concat[
66 :     "resolveOverload: \"", Atom.toString rator, "\" has no candidates"
67 :     ])
68 :     | resolveOverload (cxt, rator, argTys, args, candidates) = let
69 :     (* FIXME: we could be more efficient by just checking for coercion matchs the first pass
70 :     * and remembering those that are not pure EQ matches.
71 :     *)
72 : jhr 3499 (* build the result *)
73 :     fun done (rator, tyArgs, args, rngTy) = if Var.same(rator, BV.pow_si)
74 :     then let (* check that the second argument is a constant expression *)
75 :     val [e1, e2] = args
76 :     in
77 :     case CheckConst.eval (cxt, false, e2)
78 :     of SOME e2' =>
79 :     (AST.E_Prim(rator, tyArgs, [e1, ConstExpr.valueToExpr e2'], rngTy), rngTy)
80 :     | NONE => err(cxt, [
81 :     S "constant-integer exponent is required when lhs of '^' is a field"
82 :     ])
83 :     end
84 :     else (AST.E_Prim(rator, tyArgs, args, rngTy), rngTy)
85 : jhr 3407 (* try to match candidates while allowing type coercions *)
86 :     fun tryMatchCandidates [] = err(cxt, [
87 :     S "unable to resolve overloaded operator ", A rator, S "\n",
88 :     S " argument type is: ", TYS argTys, S "\n"
89 :     ])
90 :     | tryMatchCandidates (x::xs) = let
91 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf x)
92 :     in
93 :     case Unify.tryMatchArgs (domTy, args, argTys)
94 : jhr 3499 of SOME args' => done(x, tyArgs, args', rngTy)
95 : jhr 3407 | NONE => tryMatchCandidates xs
96 :     (* end case *)
97 :     end
98 : jhr 3499 (* try to match candidates without type coercions *)
99 : jhr 3407 fun tryCandidates [] = tryMatchCandidates candidates
100 :     | tryCandidates (x::xs) = let
101 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf x)
102 :     in
103 :     if Unify.tryEqualTypes(domTy, argTys)
104 : jhr 3499 then done(x, tyArgs, args, rngTy)
105 : jhr 3407 else tryCandidates xs
106 :     end
107 :     in
108 :     tryCandidates candidates
109 :     end
110 :    
111 : jhr 3396 (* check the type of a literal *)
112 : jhr 3433 fun checkLit lit = (AST.E_Lit lit, TypeOf.literal lit)
113 : jhr 3396
114 : jhr 3405 (* type check a dot product, which has the constraint:
115 :     * ALL[sigma1, d1, sigma2] . tensor[sigma1, d1] * tensor[d1, sigma2] -> tensor[sigma1, sigma2]
116 :     * and similarly for fields.
117 :     *)
118 :     fun chkInnerProduct (cxt, e1, ty1, e2, ty2) = let
119 :     (* check the shape of the two arguments to verify that the inner constraint matches *)
120 :     fun chkShape (Ty.Shape(dd1 as _::_), Ty.Shape(d2::dd2)) = let
121 :     val (dd1, d1) = let
122 :     fun splitLast (prefix, [d]) = (List.rev prefix, d)
123 :     | splitLast (prefix, d::dd) = splitLast (d::prefix, dd)
124 :     | splitLast (_, []) = raise Fail "impossible"
125 :     in
126 :     splitLast ([], dd1)
127 :     end
128 :     in
129 :     if Unify.equalDim(d1, d2)
130 :     then SOME(Ty.Shape(dd1@dd2))
131 :     else NONE
132 :     end
133 :     | chkShape _ = NONE
134 :     fun error () = err (cxt, [
135 :     S "type error for arguments of binary operator '•'\n",
136 :     S " found: ", TYS[ty1, ty2], S "\n"
137 :     ])
138 :     in
139 :     case (TU.prune ty1, TU.prune ty2)
140 :     (* tensor * tensor inner product *)
141 :     of (Ty.T_Tensor s1, Ty.T_Tensor s2) => (case chkShape(s1, s2)
142 :     of SOME shp => let
143 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_inner_tt)
144 :     val resTy = Ty.T_Tensor shp
145 :     in
146 :     if Unify.equalTypes(domTy, [ty1, ty2]) andalso Unify.equalType(rngTy, resTy)
147 : jhr 3407 then (AST.E_Prim(BV.op_inner_tt, tyArgs, [e1, e2], rngTy), rngTy)
148 : jhr 3405 else error()
149 :     end
150 :     | NONE => error()
151 :     (* end case *))
152 :     (* tensor * field inner product *)
153 :     | (Ty.T_Tensor s1, Ty.T_Field{diff, dim, shape=s2}) => (case chkShape(s1, s2)
154 :     of SOME shp => let
155 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_inner_tf)
156 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
157 :     in
158 :     if Unify.equalTypes(domTy, [ty1, ty2])
159 :     andalso Unify.equalType(rngTy, resTy)
160 : jhr 3407 then (AST.E_Prim(BV.op_inner_tf, tyArgs, [e1, e2], rngTy), rngTy)
161 : jhr 3405 else error()
162 : jhr 3712 end
163 : jhr 3405 | NONE => error()
164 :     (* end case *))
165 :     (* field * tensor inner product *)
166 :     | (Ty.T_Field{diff, dim, shape=s1}, Ty.T_Tensor s2) => (case chkShape(s1, s2)
167 : jhr 3712 of SOME shp => let
168 : jhr 3405 val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_inner_ft)
169 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
170 :     in
171 :     if Unify.equalTypes(domTy, [ty1, ty2])
172 :     andalso Unify.equalType(rngTy, resTy)
173 : jhr 3407 then (AST.E_Prim(BV.op_inner_ft, tyArgs, [e1, e2], rngTy), rngTy)
174 : jhr 3405 else error()
175 : jhr 3712 end
176 : jhr 3405 | NONE => error()
177 :     (* end case *))
178 :     (* field * field inner product *)
179 :     | (Ty.T_Field{diff=k1, dim=dim1, shape=s1}, Ty.T_Field{diff=k2, dim=dim2, shape=s2}) => (
180 :     case chkShape(s1, s2)
181 : jhr 3712 of SOME shp => let
182 : jhr 3405 val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_inner_ff)
183 : jhr 3712 val resTy = Ty.T_Field{diff=k1, dim=dim1, shape=shp}
184 : jhr 3405 in
185 :     (* FIXME: the resulting differentiation should be the minimum of k1 and k2 *)
186 :     if Unify.equalDim(dim1, dim2)
187 :     andalso Unify.equalTypes(domTy, [ty1, ty2])
188 :     andalso Unify.equalType(rngTy, resTy)
189 : jhr 3407 then (AST.E_Prim(BV.op_inner_ff, tyArgs, [e1, e2], rngTy), rngTy)
190 : jhr 3405 else error()
191 :     end
192 :     | NONE => error()
193 :     (* end case *))
194 :     | (ty1, ty2) => error()
195 :     (* end case *)
196 :     end
197 :    
198 : jhr 3807 (* type check an outer product, which has the constraint:
199 :     * ALL[sigma1, sigma2] . tensor[sigma1] * tensor[sigma2] -> tensor[sigma1, sigma2]
200 :     * and similarly for fields.
201 :     *)
202 :     fun chkOuterProduct (cxt, e1, ty1, e2, ty2) = let
203 :     fun mergeShp (Ty.Shape dd1, Ty.Shape dd2) = SOME(Ty.Shape(dd1@dd2))
204 :     | mergeShp _ = NONE
205 :     fun shapeError () = err (cxt, [
206 :     S "unable to determine result shape of outer product\n",
207 :     S " found: ", TYS[ty1, ty2], S "\n"
208 :     ])
209 :     fun error () = err (cxt, [
210 :     S "type error for arguments of binary operator \"⊗\"\n",
211 :     S " found: ", TYS[ty1, ty2], S "\n"
212 :     ])
213 :     in
214 :     case (TU.prune ty1, TU.prune ty2)
215 :     (* tensor * tensor outer product *)
216 :     of (Ty.T_Tensor s1, Ty.T_Tensor s2) => (case mergeShp(s1, s2)
217 :     of SOME shp => let
218 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_outer_tt)
219 :     val resTy = Ty.T_Tensor shp
220 :     in
221 :     if Unify.equalTypes(domTy, [ty1, ty2])
222 :     andalso Unify.equalType(rngTy, resTy)
223 :     then (AST.E_Prim(BV.op_outer_tt, tyArgs, [e1, e2], rngTy), rngTy)
224 :     else error()
225 :     end
226 :     | NONE => shapeError()
227 :     (* end case *))
228 :     (* field * tensor outer product *)
229 :     | (Ty.T_Field{diff, dim, shape=s1}, Ty.T_Tensor s2) => (case mergeShp(s1, s2)
230 :     of SOME shp => let
231 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_outer_ft)
232 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
233 :     in
234 :     if Unify.equalTypes(domTy, [ty1, ty2]) andalso Unify.equalType(rngTy, resTy)
235 :     then (AST.E_Prim(BV.op_outer_ft, tyArgs, [e1, e2], rngTy), rngTy)
236 :     else error()
237 :     end
238 :     | NONE => shapeError()
239 :     (* end case *))
240 :     (* tensor * field outer product *)
241 :     | (Ty.T_Tensor s1, Ty.T_Field{diff=diff, dim=dim, shape=s2}) => (case mergeShp(s1, s2)
242 :     of SOME shp => let
243 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_outer_tf)
244 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
245 :     in
246 :     if Unify.equalTypes(domTy, [ty1, ty2]) andalso Unify.equalType(rngTy, resTy)
247 :     then (AST.E_Prim(BV.op_outer_tf, tyArgs, [e1, e2], rngTy), rngTy)
248 :     else error()
249 :     end
250 :     | NONE => shapeError()
251 :     (* end case *))
252 :     (* field * field outer product *)
253 :     | (Ty.T_Field{diff=k1, dim=dim1, shape=s1}, Ty.T_Field{diff=k2, dim=dim2, shape=s2}) => (
254 :     case mergeShp(s1, s2)
255 :     of SOME shp => let
256 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_outer_ff)
257 :     val resTy = Ty.T_Field{diff=k1, dim=dim1, shape=shp}
258 :     in
259 :     (* FIXME: the resulting differentiation should be the minimum of k1 and k2 *)
260 :     if Unify.equalDim(dim1, dim2)
261 :     andalso Unify.equalTypes(domTy, [ty1, ty2])
262 :     andalso Unify.equalType(rngTy, resTy)
263 :     then (AST.E_Prim(BV.op_outer_ff, tyArgs, [e1, e2], rngTy), rngTy)
264 :     else error()
265 :     end
266 :     | NONE => shapeError()
267 :     (* end case *))
268 :     | _ => error()
269 :     (* end case *)
270 :     end
271 :    
272 : jhr 3405 (* type check a colon product, which has the constraint:
273 :     * ALL[sigma1, d1, d2, sigma2] . tensor[sigma1, d1, d2] * tensor[d2, d1, sigma2] -> tensor[sigma1, sigma2]
274 :     * and similarly for fields.
275 :     *)
276 :     fun chkColonProduct (cxt, e1, ty1, e2, ty2) = let
277 :     (* check the shape of the two arguments to verify that the inner constraint matches *)
278 :     fun chkShape (Ty.Shape(dd1 as _::_::_), Ty.Shape(d21::d22::dd2)) = let
279 :     val (dd1, d11, d12) = let
280 :     fun splitLast2 (prefix, [d1, d2]) = (List.rev prefix, d1, d2)
281 :     | splitLast2 (prefix, d::dd) = splitLast2 (d::prefix, dd)
282 :     | splitLast2 (_, []) = raise Fail "impossible"
283 :     in
284 :     splitLast2 ([], dd1)
285 :     end
286 :     in
287 :     if Unify.equalDim(d11, d21) andalso Unify.equalDim(d12, d22)
288 :     then SOME(Ty.Shape(dd1@dd2))
289 :     else NONE
290 :     end
291 :     | chkShape _ = NONE
292 :     fun error () = err (cxt, [
293 :     S "type error for arguments of binary operator \":\"\n",
294 :     S " found: ", TYS[ty1, ty2], S "\n"
295 :     ])
296 :     in
297 :     case (TU.prune ty1, TU.prune ty2)
298 :     (* tensor * tensor colon product *)
299 :     of (Ty.T_Tensor s1, Ty.T_Tensor s2) => (case chkShape(s1, s2)
300 :     of SOME shp => let
301 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_colon_tt)
302 :     val resTy = Ty.T_Tensor shp
303 :     in
304 :     if Unify.equalTypes(domTy, [ty1, ty2])
305 :     andalso Unify.equalType(rngTy, resTy)
306 : jhr 3407 then (AST.E_Prim(BV.op_colon_tt, tyArgs, [e1, e2], rngTy), rngTy)
307 : jhr 3405 else error()
308 :     end
309 :     | NONE => error()
310 :     (* end case *))
311 :     (* field * tensor colon product *)
312 :     | (Ty.T_Field{diff, dim, shape=s1}, Ty.T_Tensor s2) => (case chkShape(s1, s2)
313 :     of SOME shp => let
314 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_colon_ft)
315 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
316 :     in
317 :     if Unify.equalTypes(domTy, [ty1, ty2]) andalso Unify.equalType(rngTy, resTy)
318 : jhr 3407 then (AST.E_Prim(BV.op_colon_ft, tyArgs, [e1, e2], rngTy), rngTy)
319 : jhr 3405 else error()
320 :     end
321 :     | NONE => error()
322 :     (* end case *))
323 :     (* tensor * field colon product *)
324 :     | (Ty.T_Tensor s1, Ty.T_Field{diff=diff, dim=dim, shape=s2}) => (case chkShape(s1, s2)
325 :     of SOME shp => let
326 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_colon_tf)
327 :     val resTy = Ty.T_Field{diff=diff, dim=dim, shape=shp}
328 :     in
329 :     if Unify.equalTypes(domTy, [ty1, ty2]) andalso Unify.equalType(rngTy, resTy)
330 : jhr 3407 then (AST.E_Prim(BV.op_colon_tf, tyArgs, [e1, e2], rngTy), rngTy)
331 : jhr 3405 else error()
332 :     end
333 :     | NONE => error()
334 :     (* end case *))
335 :     (* field * field colon product *)
336 :     | (Ty.T_Field{diff=k1, dim=dim1, shape=s1}, Ty.T_Field{diff=k2, dim=dim2, shape=s2}) => (
337 :     case chkShape(s1, s2)
338 :     of SOME shp => let
339 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf BV.op_colon_ff)
340 :     val resTy = Ty.T_Field{diff=k1, dim=dim1, shape=shp}
341 :     in
342 :     (* FIXME: the resulting differentiation should be the minimum of k1 and k2 *)
343 :     if Unify.equalDim(dim1, dim2)
344 :     andalso Unify.equalTypes(domTy, [ty1, ty2])
345 :     andalso Unify.equalType(rngTy, resTy)
346 : jhr 3407 then (AST.E_Prim(BV.op_colon_ff, tyArgs, [e1, e2], rngTy), rngTy)
347 : jhr 3405 else error()
348 :     end
349 :     | NONE => error()
350 :     (* end case *))
351 :     | (ty1, ty2) => error()
352 :     (* end case *)
353 :     end
354 :    
355 : jhr 3396 (* check the type of an expression *)
356 :     fun check (env, cxt, e) = (case e
357 : jhr 3405 of PT.E_Mark m => check (E.withEnvAndContext (env, cxt, m))
358 : jhr 3396 | PT.E_Cond(e1, cond, e2) => let
359 :     val eTy1 = check (env, cxt, e1)
360 :     val eTy2 = check (env, cxt, e2)
361 :     in
362 : jhr 3431 case checkAndPrune(env, cxt, cond)
363 : jhr 3396 of (cond', Ty.T_Bool) => (case Util.coerceType2(eTy1, eTy2)
364 : jhr 3405 of SOME(e1', e2', ty) => (AST.E_Cond(cond', e1', e2', ty), ty)
365 : jhr 3396 | NONE => err (cxt, [
366 :     S "types do not match in conditional expression\n",
367 :     S " true branch: ", TY(#2 eTy1), S "\n",
368 :     S " false branch: ", TY(#2 eTy2)
369 :     ])
370 : jhr 3398 (* end case *))
371 : jhr 3431 | (_, Ty.T_Error) => bogusExpTy
372 : jhr 3396 | (_, ty') => err (cxt, [S "expected bool type, but found ", TY ty'])
373 :     (* end case *)
374 :     end
375 :     | PT.E_Range(e1, e2) => (case (check (env, cxt, e1), check (env, cxt, e2))
376 :     of ((e1', Ty.T_Int), (e2', Ty.T_Int)) => let
377 : jhr 3398 val resTy = Ty.T_Sequence(Ty.T_Int, NONE)
378 : jhr 3396 in
379 : jhr 3407 (AST.E_Prim(BV.range, [], [e1', e2'], resTy), resTy)
380 : jhr 3396 end
381 :     | ((_, Ty.T_Int), (_, ty2)) =>
382 :     err (cxt, [S "expected type 'int' on rhs of '..', but found ", TY ty2])
383 :     | ((_, ty1), (_, Ty.T_Int)) =>
384 :     err (cxt, [S "expected type 'int' on lhs of '..', but found ", TY ty1])
385 :     | ((_, ty1), (_, ty2)) => err (cxt, [
386 :     S "arguments of '..' must have type 'int', found ",
387 :     TY ty1, S " and ", TY ty2
388 :     ])
389 :     (* end case *))
390 :     | PT.E_OrElse(e1, e2) =>
391 :     checkCondOp (env, cxt, e1, "||", e2,
392 :     fn (e1', e2') => AST.E_Cond(e1', AST.E_Lit(L.Bool true), e2', Ty.T_Bool))
393 :     | PT.E_AndAlso(e1, e2) =>
394 :     checkCondOp (env, cxt, e1, "&&", e2,
395 :     fn (e1', e2') => AST.E_Cond(e1', e2', AST.E_Lit(L.Bool false), Ty.T_Bool))
396 :     | PT.E_BinOp(e1, rator, e2) => let
397 :     val (e1', ty1) = check (env, cxt, e1)
398 :     val (e2', ty2) = check (env, cxt, e2)
399 :     in
400 :     if Atom.same(rator, BasisNames.op_dot)
401 : jhr 3405 then chkInnerProduct (cxt, e1', ty1, e2', ty2)
402 : jhr 3807 else if Atom.same(rator, BasisNames.op_outer)
403 :     then chkOuterProduct (cxt, e1', ty1, e2', ty2)
404 : jhr 3396 else if Atom.same(rator, BasisNames.op_colon)
405 : jhr 3405 then chkColonProduct (cxt, e1', ty1, e2', ty2)
406 : jhr 3807 else (case Env.findFunc (env, rator)
407 : jhr 3396 of Env.PrimFun[rator] => let
408 : jhr 3405 val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf rator)
409 : jhr 3396 in
410 : jhr 3402 case Unify.matchArgs(domTy, [e1', e2'], [ty1, ty2])
411 : jhr 3407 of SOME args => (AST.E_Prim(rator, tyArgs, args, rngTy), rngTy)
412 : jhr 3396 | NONE => err (cxt, [
413 : jhr 3418 S "type error for binary operator ", V rator, S "\n",
414 : jhr 3396 S " expected: ", TYS domTy, S "\n",
415 :     S " but found: ", TYS[ty1, ty2]
416 :     ])
417 :     (* end case *)
418 :     end
419 :     | Env.PrimFun ovldList =>
420 :     resolveOverload (cxt, rator, [ty1, ty2], [e1', e2'], ovldList)
421 :     | _ => raise Fail "impossible"
422 :     (* end case *))
423 :     end
424 : jhr 3398 | PT.E_UnaryOp(rator, e) => let
425 : jhr 3405 val eTy = check(env, cxt, e)
426 : jhr 3398 in
427 : jhr 3405 case Env.findFunc (env, rator)
428 : jhr 3398 of Env.PrimFun[rator] => let
429 : jhr 3405 val (tyArgs, Ty.T_Fun([domTy], rngTy)) = TU.instantiate(Var.typeOf rator)
430 : jhr 3398 in
431 : jhr 3405 case Util.coerceType (domTy, eTy)
432 : jhr 3410 of SOME e' => (AST.E_Prim(rator, tyArgs, [e'], rngTy), rngTy)
433 : jhr 3398 | NONE => err (cxt, [
434 : jhr 3418 S "type error for unary operator ", V rator, S "\n",
435 : jhr 3398 S " expected: ", TY domTy, S "\n",
436 : jhr 3405 S " but found: ", TY (#2 eTy)
437 : jhr 3398 ])
438 :     (* end case *)
439 :     end
440 : jhr 3405 | Env.PrimFun ovldList => resolveOverload (cxt, rator, [#2 eTy], [#1 eTy], ovldList)
441 : jhr 3398 | _ => raise Fail "impossible"
442 :     (* end case *)
443 :     end
444 : jhr 3407 | PT.E_Apply(e, args) => let
445 :     val (args, tys) = checkList (env, cxt, args)
446 :     fun appTyError (f, paramTys, argTys) = err(cxt, [
447 :     S "type error in application of ", V f, S "\n",
448 :     S " expected: ", TYS paramTys, S "\n",
449 :     S " but found: ", TYS argTys
450 :     ])
451 :     fun checkPrimApp f = if Var.isPrim f
452 :     then (case TU.instantiate(Var.typeOf f)
453 :     of (tyArgs, Ty.T_Fun(domTy, rngTy)) => (
454 :     case Unify.matchArgs (domTy, args, tys)
455 :     of SOME args => (AST.E_Prim(f, tyArgs, args, rngTy), rngTy)
456 :     | NONE => appTyError (f, domTy, tys)
457 :     (* end case *))
458 :     | _ => err(cxt, [S "application of non-function/field ", V f])
459 :     (* end case *))
460 :     else raise Fail "unexpected user function"
461 :     (* check the application of a user-defined function *)
462 :     fun checkFunApp (cxt, f) = if Var.isPrim f
463 :     then raise Fail "unexpected primitive function"
464 :     else (case Var.monoTypeOf f
465 :     of Ty.T_Fun(domTy, rngTy) => (
466 :     case Unify.matchArgs (domTy, args, tys)
467 :     of SOME args => (AST.E_Apply(useVar(cxt, f), args, rngTy), rngTy)
468 :     | NONE => appTyError (f, domTy, tys)
469 :     (* end case *))
470 :     | _ => err(cxt, [S "application of non-function/field ", V f])
471 :     (* end case *))
472 :     fun checkFieldApp (e1', ty1) = (case (args, tys)
473 :     of ([e2'], [ty2]) => let
474 :     val (tyArgs, Ty.T_Fun([fldTy, domTy], rngTy)) =
475 :     TU.instantiate(Var.typeOf BV.op_probe)
476 :     fun tyError () = err (cxt, [
477 :     S "type error for field application\n",
478 :     S " expected: ", TYS[fldTy, domTy], S "\n",
479 :     S " but found: ", TYS[ty1, ty2]
480 :     ])
481 :     in
482 :     if Unify.equalType(fldTy, ty1)
483 :     then (case Util.coerceType(domTy, (e2', ty2))
484 : jhr 3410 of SOME e2' => (AST.E_Prim(BV.op_probe, tyArgs, [e1', e2'], rngTy), rngTy)
485 : jhr 3407 | NONE => tyError()
486 :     (* end case *))
487 :     else tyError()
488 :     end
489 :     | _ => err(cxt, [S "badly formed field application"])
490 :     (* end case *))
491 :     in
492 :     case stripMark(#2 cxt, e)
493 :     of (span, PT.E_Var f) => (case Env.findVar (env, f)
494 :     of SOME f' => checkFieldApp (
495 :     AST.E_Var(useVar((#1 cxt, span), f')),
496 :     Var.monoTypeOf f')
497 :     | NONE => (case Env.findFunc (env, f)
498 :     of Env.PrimFun[] => err(cxt, [S "unknown function ", A f])
499 :     | Env.PrimFun[f'] => checkPrimApp f'
500 :     | Env.PrimFun ovldList =>
501 :     resolveOverload ((#1 cxt, span), f, tys, args, ovldList)
502 :     | Env.UserFun f' => checkFunApp((#1 cxt, span), f')
503 :     (* end case *))
504 :     (* end case *))
505 :     | _ => checkFieldApp (check (env, cxt, e))
506 :     (* end case *)
507 :     end
508 : jhr 3424 | PT.E_Subscript(e, indices) => let
509 :     fun expectedTensor ty = err(cxt, [
510 :     S "expected tensor type for slicing, but found ", TY ty
511 : jhr 3398 ])
512 : jhr 3424 fun chkIndex e = let
513 :     val eTy as (_, ty) = check(env, cxt, e)
514 :     in
515 :     if Unify.equalType(ty, Ty.T_Int)
516 :     then eTy
517 :     else err (cxt, [
518 :     S "expected type 'int' for index, but found ", TY ty
519 :     ])
520 :     end
521 :     in
522 :     case (check(env, cxt, e), indices)
523 :     of ((e', Ty.T_Error), _) => (
524 :     List.app (ignore o Option.map chkIndex) indices;
525 :     bogusExpTy)
526 :     | ((e1', ty1 as Ty.T_Sequence(elemTy, optDim)), [SOME e2]) => let
527 :     val (e2', ty2) = chkIndex e2
528 :     val rator = if isSome optDim
529 :     then BV.subscript
530 :     else BV.dynSubscript
531 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = TU.instantiate(Var.typeOf rator)
532 :     in
533 :     if Unify.equalTypes(domTy, [ty1, ty2])
534 :     then let
535 :     val exp = AST.E_Prim(rator, tyArgs, [e1', e2'], rngTy)
536 :     in
537 :     (exp, rngTy)
538 :     end
539 :     else raise Fail "unexpected unification failure"
540 :     end
541 :     | ((e', ty as Ty.T_Sequence _), [NONE]) => expectedTensor ty
542 :     | ((e', ty as Ty.T_Sequence _), _) => expectedTensor ty
543 : cchiw 3991 | ((e', ty as _), _) => let
544 : jhr 3712 (* for tensor slicing/indexing, the indices must be constant expressions *)
545 :     fun chkConstIndex NONE = NONE
546 :     | chkConstIndex (SOME e) = (case chkIndex e
547 :     of (_, Ty.T_Error) => SOME bogusExp
548 :     | (e', _) => (case CheckConst.eval (cxt, false, e')
549 : jhr 3797 (* FIXME: should check that index is in range for type! *)
550 : jhr 3712 of SOME cexp => SOME(ConstExpr.valueToExpr cexp)
551 :     | NONE => SOME e' (* use e' to preserve variable uses *)
552 :     (* end case *))
553 :     (* end case *))
554 :     val indices' = List.map chkConstIndex indices
555 : jhr 3427 val order = List.length indices'
556 : cchiw 3991 (* val expectedTy = TU.mkTensorTy order*)
557 :     val expectedTy =( case ty
558 :     of Ty.T_Field{diff, dim, shape=s as Ty.Shape(d2::dd2)}
559 :     => Ty.T_Field{diff=diff, dim=dim, shape=s}
560 :     | Ty.T_Tensor shape => TU.mkTensorTy order
561 :     | Ty.T_Field _ => raise Fail "unknow field type"
562 :     | _ => raise Fail "unknow type"
563 :     (*end case*))
564 : jhr 3427 val resultTy = TU.slice(expectedTy, List.map Option.isSome indices')
565 :     in
566 :     if Unify.equalType(ty, expectedTy)
567 :     then (AST.E_Slice(e', indices', resultTy), resultTy)
568 :     else err (cxt, [
569 :     S "type error in slice operation\n",
570 :     S " expected: ", S(Int.toString order), S "-order tensor\n",
571 :     S " but found: ", TY ty
572 :     ])
573 :     end
574 : jhr 3424 (* end case *)
575 :     end
576 : jhr 3431 | PT.E_Select(e, field) => (case stripMark(#2 cxt, e)
577 :     of (_, PT.E_Var x) => (case E.findStrand (env, x)
578 :     of SOME _ => if E.inGlobalUpdate env
579 :     then (case E.findSetFn (env, field)
580 :     of SOME setFn => let
581 :     val (mvs, ty) = TU.instantiate (Var.typeOf setFn)
582 : jhr 3846 val resTy = Ty.T_Sequence(Ty.T_Strand x, NONE)
583 : jhr 3431 in
584 :     E.recordProp (env, Properties.StrandSets);
585 :     if Unify.equalType(ty, Ty.T_Fun([], resTy))
586 :     then (AST.E_Prim(setFn, mvs, [], resTy), resTy)
587 :     else raise Fail "impossible"
588 :     end
589 :     | _ => err (cxt, [
590 :     S "unknown strand-set specifier ", A field
591 :     ])
592 :     (* end case *))
593 :     else err (cxt, [
594 :     S "illegal strand set specification in ",
595 :     S(E.scopeToString(E.currentScope env))
596 :     ])
597 :     | _ => checkSelect (env, cxt, e, field)
598 :     (* end case *))
599 :     | _ => checkSelect (env, cxt, e, field)
600 :     (* end case *))
601 :     | PT.E_Real e => (case checkAndPrune (env, cxt, e)
602 : jhr 3396 of (e', Ty.T_Int) =>
603 : jhr 3407 (AST.E_Prim(BV.i2r, [], [e'], Ty.realTy), Ty.realTy)
604 : jhr 3428 | (e', Ty.T_Error) => bogusExpTy
605 : jhr 3396 | (_, ty) => err(cxt, [
606 :     S "argument of 'real' must have type 'int', but found ",
607 :     TY ty
608 :     ])
609 :     (* end case *))
610 :     | PT.E_Load nrrd => let
611 : jhr 3418 val (tyArgs, Ty.T_Fun(_, rngTy)) = TU.instantiate(Var.typeOf(BV.fn_load))
612 : jhr 3396 in
613 : jhr 3407 case chkStringConstExpr (env, cxt, nrrd)
614 :     of SOME nrrd => (AST.E_LoadNrrd(tyArgs, nrrd, rngTy), rngTy)
615 :     | NONE => (bogusExp, rngTy)
616 :     (* end case *)
617 : jhr 3396 end
618 :     | PT.E_Image nrrd => let
619 : jhr 3418 val (tyArgs, Ty.T_Fun(_, rngTy)) = TU.instantiate(Var.typeOf(BV.fn_image))
620 : jhr 3396 in
621 : jhr 3407 case chkStringConstExpr (env, cxt, nrrd)
622 :     of SOME nrrd => (AST.E_LoadNrrd(tyArgs, nrrd, rngTy), rngTy)
623 :     | NONE => (bogusExp, rngTy)
624 :     (* end case *)
625 : jhr 3396 end
626 : jhr 3405 | PT.E_Var x => (case E.findVar (env, x)
627 : jhr 3407 of SOME x' => (AST.E_Var(useVar(cxt, x')), Var.monoTypeOf x')
628 : jhr 3396 | NONE => err(cxt, [S "undeclared variable ", A x])
629 :     (* end case *))
630 : jhr 3421 | PT.E_Kernel(kern, dim) => (case E.findVar (env, kern)
631 :     of SOME kern' => (case Var.monoTypeOf kern'
632 :     of ty as Ty.T_Kernel(Ty.DiffConst k) => let
633 :     val k' = Int.fromLarge dim handle Overflow => 1073741823
634 :     val e = AST.E_Var(useVar(cxt, kern'))
635 :     in
636 :     if (k = k')
637 :     then (e, ty)
638 :     else let
639 :     val ty' = Ty.T_Kernel(Ty.DiffConst k')
640 :     in
641 :     (AST.E_Coerce{srcTy = ty, dstTy = ty', e = e}, ty')
642 :     end
643 :     end
644 :     | _ => err(cxt, [S "expected kernel, but found ", S(Var.kindToString kern')])
645 :     (* end case *))
646 :     | NONE => err(cxt, [S "unknown kernel ", A kern])
647 :     (* end case *))
648 : jhr 3396 | PT.E_Lit lit => checkLit lit
649 :     | PT.E_Id d => let
650 :     val (tyArgs, Ty.T_Fun(_, rngTy)) =
651 : jhr 3405 TU.instantiate(Var.typeOf(BV.identity))
652 : jhr 3396 in
653 : jhr 3407 if Unify.equalType(Ty.T_Tensor(checkShape(env, cxt, [d, d])), rngTy)
654 :     then (AST.E_Prim(BV.identity, tyArgs, [], rngTy), rngTy)
655 : jhr 3396 else raise Fail "impossible"
656 :     end
657 :     | PT.E_Zero dd => let
658 :     val (tyArgs, Ty.T_Fun(_, rngTy)) =
659 : jhr 3405 TU.instantiate(Var.typeOf(BV.zero))
660 : jhr 3396 in
661 : jhr 3407 if Unify.equalType(Ty.T_Tensor(checkShape(env, cxt, dd)), rngTy)
662 :     then (AST.E_Prim(BV.zero, tyArgs, [], rngTy), rngTy)
663 : jhr 3396 else raise Fail "impossible"
664 :     end
665 :     | PT.E_NaN dd => let
666 :     val (tyArgs, Ty.T_Fun(_, rngTy)) =
667 : jhr 3405 TU.instantiate(Var.typeOf(BV.nan))
668 : jhr 3396 in
669 : jhr 3407 if Unify.equalType(Ty.T_Tensor(checkShape(env, cxt, dd)), rngTy)
670 :     then (AST.E_Prim(BV.nan, tyArgs, [], rngTy), rngTy)
671 : jhr 3396 else raise Fail "impossible"
672 :     end
673 : jhr 3422 | PT.E_Sequence exps => (case checkList (env, cxt, exps)
674 :     (* FIXME: need kind for concrete types here! *)
675 :     of ([], _) => let
676 :     val ty = Ty.T_Sequence(Ty.T_Var(MetaVar.newTyVar()), SOME(Ty.DimConst 0))
677 :     in
678 :     (AST.E_Seq([], ty), ty)
679 :     end
680 :     | (args, tys) => (case Util.coerceTypes(List.map TU.pruneHead tys)
681 :     of SOME ty => if TU.isValueType ty
682 :     then let
683 :     fun doExp eTy = valOf(Util.coerceType (ty, eTy))
684 :     val resTy = Ty.T_Sequence(ty, SOME(Ty.DimConst(List.length args)))
685 :     val args = ListPair.map doExp (args, tys)
686 :     in
687 :     (AST.E_Seq(args, resTy), resTy)
688 :     end
689 :     else err(cxt, [S "sequence expression of non-value argument type"])
690 :     | NONE => err(cxt, [S "arguments of sequence expression must have same type"])
691 :     (* end case *))
692 :     (* end case *))
693 : jhr 3428 | PT.E_SeqComp comp => chkComprehension (env, cxt, comp)
694 : jhr 3396 | PT.E_Cons args => let
695 :     (* Note that we are guaranteed that args is non-empty *)
696 :     val (args, tys) = checkList (env, cxt, args)
697 :     (* extract the first non-error type in tys *)
698 :     val ty = (case List.find (fn Ty.T_Error => false | _ => true) tys
699 :     of NONE => Ty.T_Error
700 :     | SOME ty => ty
701 :     (* end case *))
702 : jhr 3405 (* process the arguments checking that they all have the expected type *)
703 :     fun chkArgs (ty, shape) = let
704 :     val Ty.Shape dd = TU.pruneShape shape (* NOTE: this may fail if we allow user polymorphism *)
705 :     val resTy = Ty.T_Tensor(Ty.Shape(Ty.DimConst(List.length args) :: dd))
706 :     fun chkArgs (arg::args, argTy::tys, args') = (
707 :     case Util.coerceType(ty, (arg, argTy))
708 : jhr 3410 of SOME arg' => chkArgs (args, tys, arg'::args')
709 : jhr 3405 | NONE => (
710 :     TypeError.error(cxt, [
711 :     S "arguments of tensor construction must have same type"
712 :     ]);
713 :     chkArgs (args, tys, bogusExp::args'))
714 :     (* end case *))
715 : jhr 3408 | chkArgs (_, _, args') = (AST.E_Tensor(List.rev args', resTy), resTy)
716 : jhr 3405 in
717 :     chkArgs (args, tys, [])
718 :     end
719 : jhr 3396 in
720 : jhr 3405 case TU.pruneHead ty
721 : jhr 3407 of Ty.T_Int => chkArgs(Ty.realTy, Ty.Shape[]) (* coerce integers to reals *)
722 : jhr 3405 | ty as Ty.T_Tensor shape => chkArgs(ty, shape)
723 : jhr 3396 | _ => err(cxt, [S "Invalid argument type for tensor construction"])
724 :     (* end case *)
725 :     end
726 :     | PT.E_Deprecate(msg, e) => (
727 :     warn (cxt, [S msg]);
728 : jhr 3402 check (env, cxt, e))
729 : jhr 3396 (* end case *))
730 :    
731 : jhr 3431 (* typecheck and the prune the result *)
732 :     and checkAndPrune (env, cxt, e) = let
733 :     val (e, ty) = check (env, cxt, e)
734 :     in
735 :     (e, TU.prune ty)
736 :     end
737 :    
738 : jhr 3396 (* check a conditional operator (e.g., || or &&) *)
739 :     and checkCondOp (env, cxt, e1, rator, e2, mk) = (
740 :     case (check(env, cxt, e1), check(env, cxt, e2))
741 :     of ((e1', Ty.T_Bool), (e2', Ty.T_Bool)) => (mk(e1', e2'), Ty.T_Bool)
742 :     | ((_, Ty.T_Bool), (_, ty2)) =>
743 : jhr 3405 err (cxt, [S "expected type 'bool' on rhs of '", S rator, S "', but found ", TY ty2])
744 : jhr 3396 | ((_, ty1), (_, Ty.T_Bool)) =>
745 : jhr 3405 err (cxt, [S "expected type 'bool' on lhs of '", S rator, S "', but found ", TY ty1])
746 : jhr 3396 | ((_, ty1), (_, ty2)) => err (cxt, [
747 : jhr 3405 S "arguments of '", S rator, S "' must have type 'bool', but found ",
748 : jhr 3396 TY ty1, S " and ", TY ty2
749 :     ])
750 :     (* end case *))
751 :    
752 : jhr 3431 (* check a field select that is _not_ a strand-set *)
753 :     and checkSelect (env, cxt, e, field) = (case checkAndPrune (env, cxt, e)
754 : jhr 3846 of (e', Ty.T_Strand strand) => (case Env.findStrand(env, strand)
755 : jhr 3431 of SOME sEnv => (case StrandEnv.findStateVar(sEnv, field)
756 :     of SOME x' => let
757 :     val ty = Var.monoTypeOf x'
758 :     in
759 :     (AST.E_Select(e', useVar(cxt, x')), ty)
760 :     end
761 :     | NONE => err(cxt, [
762 :     S "strand ", A strand,
763 :     S " does not have state variable ", A field
764 :     ])
765 :     (* end case *))
766 :     | NONE => err(cxt, [S "unknown strand ", A strand])
767 :     (* end case *))
768 :     | (_, Ty.T_Error) => bogusExpTy
769 :     | (_, ty) => err (cxt, [
770 :     S "expected strand type, but found ", TY ty,
771 :     S " in selection of ", A field
772 :     ])
773 :     (* end case *))
774 :    
775 : jhr 3424 and chkComprehension (env, cxt, PT.COMP_Mark m) =
776 :     chkComprehension(E.withEnvAndContext(env, cxt, m))
777 :     | chkComprehension (env, cxt, PT.COMP_Comprehension(e, [iter])) = let
778 :     val (iter', env') = checkIter (E.blockScope env, cxt, iter)
779 :     val (e', ty) = check (env', cxt, e)
780 :     val resTy = Ty.T_Sequence(ty, NONE)
781 :     in
782 : jhr 3463 case iter'
783 : jhr 3467 of (x, AST.E_Prim(f, _, [], _)) =>
784 :     if Basis.isStrandSet f
785 : jhr 3463 andalso not(Env.inGlobalInit env orelse Env.inGlobalUpdate env)
786 : jhr 3464 then err (cxt, [
787 : jhr 3467 S "use of strand set ", V f,
788 : jhr 3463 S " outside of global initialization or update"
789 :     ])
790 : jhr 3467 else (AST.E_ParallelMap(e', x, f, resTy), resTy)
791 : jhr 3464 | _ => (AST.E_Comprehension(e', iter', resTy), resTy)
792 :     (* end case *)
793 : jhr 3424 end
794 :     | chkComprehension _ = raise Fail "impossible"
795 :    
796 :     and checkIter (env, cxt, PT.I_Mark m) = checkIter (E.withEnvAndContext (env, cxt, m))
797 :     | checkIter (env, cxt, PT.I_Iterator({span, tree=x}, e)) = (
798 : jhr 3431 case checkAndPrune (env, cxt, e)
799 : jhr 3424 of (e', ty as Ty.T_Sequence(elemTy, _)) => let
800 :     val x' = Var.new(x, Error.location(#1 cxt, span), Var.LocalVar, elemTy)
801 :     in
802 :     ((x', e'), E.insertLocal(env, cxt, x, x'))
803 :     end
804 :     | (e', ty) => let
805 :     val x' = Var.new(x, Error.UNKNOWN, Var.IterVar, Ty.T_Error)
806 :     in
807 : jhr 3431 if TU.isErrorType ty
808 :     then ()
809 :     else TypeError.error (cxt, [
810 :     S "expected sequence type in iteration, but found '", TY ty, S "'"
811 :     ]);
812 : jhr 3424 ((x', bogusExp), E.insertLocal(env, cxt, x, x'))
813 :     end
814 :     (* end case *))
815 :    
816 : jhr 3396 (* typecheck a list of expressions returning a list of AST expressions and a list
817 :     * of the types of the expressions.
818 :     *)
819 :     and checkList (env, cxt, exprs) = let
820 :     fun chk (e, (es, tys)) = let
821 : jhr 3431 val (e, ty) = checkAndPrune (env, cxt, e)
822 : jhr 3396 in
823 :     (e::es, ty::tys)
824 :     end
825 :     in
826 :     List.foldr chk ([], []) exprs
827 :     end
828 :    
829 : jhr 3407 (* check a string that is specified as a constant expression *)
830 :     and chkStringConstExpr (env, cxt, PT.E_Mark m) =
831 :     chkStringConstExpr (E.withEnvAndContext (env, cxt, m))
832 : jhr 3431 | chkStringConstExpr (env, cxt, e) = (case checkAndPrune (env, cxt, e)
833 : jhr 3455 of (e', Ty.T_String) => (case CheckConst.eval (cxt, false, e')
834 : jhr 3407 of SOME(ConstExpr.String s) => SOME s
835 :     | SOME(ConstExpr.Expr e) => raise Fail "FIXME"
836 :     | NONE => NONE
837 :     | _ => raise Fail "impossible: wrong type for constant expr"
838 :     (* end case *))
839 : jhr 3431 | (_, Ty.T_Error) => NONE
840 : jhr 3407 | (_, ty) => (
841 :     TypeError.error (cxt, [
842 :     S "expected constant expression of type 'string', but found '",
843 :     TY ty, S "'"
844 :     ]);
845 :     NONE)
846 :     (* end case *))
847 :    
848 :     (* check a dimension that is given by a constant expression *)
849 : jhr 3431 and checkDim (env, cxt, dim) = (case checkAndPrune (env, cxt, dim)
850 : jhr 3455 of (e', Ty.T_Int) => (case CheckConst.eval (cxt, false, e')
851 : jhr 3407 of SOME(ConstExpr.Int d) => SOME d
852 :     | SOME(ConstExpr.Expr e) => (
853 :     TypeError.error (cxt, [S "unable to evaluate constant dimension expression"]);
854 :     NONE)
855 :     | NONE => NONE
856 :     | _ => raise Fail "impossible: wrong type for constant expr"
857 :     (* end case *))
858 : jhr 3431 | (_, Ty.T_Error) => NONE
859 : jhr 3407 | (_, ty) => (
860 :     TypeError.error (cxt, [
861 :     S "expected constant expression of type 'int', but found '",
862 :     TY ty, S "'"
863 :     ]);
864 :     NONE)
865 :     (* end case *))
866 :    
867 :     (* check a tensor shape, where the dimensions are given by constant expressions *)
868 :     and checkShape (env, cxt, shape) = let
869 :     fun checkDim' e = (case checkDim (env, cxt, e)
870 :     of SOME d => (
871 :     if (d <= 1)
872 :     then TypeError.error (cxt, [
873 :     S "invalid tensor-shape dimension; must be > 1, but found ",
874 :     S (IntLit.toString d)
875 :     ])
876 :     else ();
877 :     Ty.DimConst(IntInf.toInt d))
878 :     | NONE => Ty.DimConst ~1
879 :     (* end case *))
880 :     in
881 :     Ty.Shape(List.map checkDim' shape)
882 :     end
883 :    
884 : jhr 3396 end

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