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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/typechecker/typechecker.sml
ViewVC logotype

Annotation of /trunk/src/compiler/typechecker/typechecker.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88 - (view) (download)
Original Path: trunk/src/typechecker/typechecker.sml

1 : jhr 69 (* typechecker.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure Typechecker : sig
8 :    
9 : jhr 88 exception Error
10 :    
11 : jhr 86 val check : Error.err_stream -> ParseTree.program -> AST.program
12 : jhr 69
13 :     end = struct
14 :    
15 : jhr 70 structure PT = ParseTree
16 : jhr 69 structure Ty = Types
17 : jhr 81 structure U = Util
18 : jhr 69
19 : jhr 88 exception Error
20 :    
21 : jhr 86 type context = Error.err_stream * Error.span
22 :    
23 :     fun withContext ((errStrm, _), {span, tree}) =
24 :     ((errStrm, span), tree)
25 :     fun withEnvAndContext (env, (errStrm, _), {span, tree}) =
26 :     (env, (errStrm, span), tree)
27 :    
28 : jhr 88 fun error ((errStrm, span), msg) = (
29 :     Error.errorAt(errStrm, span, msg);
30 :     raise Error)
31 : jhr 86
32 : jhr 88 datatype token
33 :     = S of string | A of Atom.atom
34 :     | V of AST.var | TY of Types.ty | TYS of Types.ty list
35 :    
36 :     fun err (cxt, toks) = let
37 :     fun tok2str (S s) = s
38 :     | tok2str (A a) = Atom.toString a
39 :     | tok2str (V x) = Var.nameOf x
40 :     | tok2str (TY ty) = TypeUtil.toString ty
41 :     | tok2str (TYS []) = "()"
42 :     | tok2str (TYS[ty]) = TypeUtil.toString ty
43 :     | tok2str (TYS tys) = String.concat[
44 :     "(", String.concatWith " * " (List.map TypeUtil.toString tys), ")"
45 :     ]
46 :     in
47 :     error(cxt, List.map tok2str toks)
48 :     end
49 :    
50 : jhr 83 val realZero = AST.E_Lit(Literal.Float(FloatLit.zero true))
51 :    
52 : jhr 70 (* check a differentiation level, which muse be >= 0 *)
53 :     fun checkDiff (cxt, k) =
54 :     if (k < 0)
55 :     then raise Fail "differentiation must be >= 0"
56 : jhr 75 else Ty.DiffConst(IntInf.toInt k)
57 : jhr 70
58 :     (* check a dimension, which must be 2 or 3 *)
59 :     fun checkDim (cxt, d) =
60 : jhr 82 if (d <= 0)
61 :     then raise Fail "invalid dimension; must be > 0"
62 : jhr 75 else Ty.DimConst(IntInf.toInt d)
63 : jhr 70
64 :     (* check a shape *)
65 :     fun checkShape (cxt, shape) = let
66 :     fun chkDim d = if (d < 1)
67 :     then raise Fail "invalid shape dimension; must be >= 1"
68 : jhr 75 else Ty.DimConst(IntInf.toInt d)
69 : jhr 70 in
70 :     Ty.Shape(List.map chkDim shape)
71 :     end
72 :    
73 : jhr 69 (* check the well-formedness of a type and translate it to an AST type *)
74 : jhr 70 fun checkTy (cxt, ty) = (case ty
75 : jhr 86 of PT.T_Mark m => checkTy(withContext(cxt, m))
76 : jhr 70 | PT.T_Bool => Ty.T_Bool
77 :     | PT.T_Int => Ty.T_Int
78 :     | PT.T_Real => Ty.realTy
79 :     | PT.T_String => Ty.T_String
80 :     | PT.T_Vec n => (* NOTE: the parser guarantees that 2 <= n <= 4 *)
81 :     Ty.vecTy(IntInf.toInt n)
82 :     | PT.T_Kernel k => Ty.T_Kernel(checkDiff(cxt, k))
83 :     | PT.T_Field{diff, dim, shape} => Ty.T_Field{
84 :     diff = checkDiff (cxt, diff),
85 :     dim = checkDim (cxt, dim),
86 :     shape = checkShape (cxt, shape)
87 :     }
88 :     | PT.T_Tensor shape => Ty.T_Tensor(checkShape(cxt, shape))
89 :     | PT.T_Image{dim, shape} => Ty.T_Image{
90 :     dim = checkDim (cxt, dim),
91 :     shape = checkShape (cxt, shape)
92 :     }
93 :     | PT.T_Array(ty, dims) => raise Fail "Array type"
94 : jhr 69 (* end case *))
95 :    
96 : jhr 71 fun checkLit lit = (case lit
97 :     of (Literal.Int _) => (AST.E_Lit lit, Ty.T_Int)
98 :     | (Literal.Float _) => (AST.E_Lit lit, Ty.realTy)
99 :     | (Literal.String s) => (AST.E_Lit lit, Ty.T_String)
100 :     | (Literal.Bool _) => (AST.E_Lit lit, Ty.T_Bool)
101 :     (* end case *))
102 :    
103 : jhr 85 (* resolve overloading: we use a simple scheme that selects the first operator in the
104 :     * list that matches the argument types.
105 :     *)
106 :     fun resolveOverload (rator, argTys, args, candidates) = let
107 :     fun tryCandidates [] = raise Fail(concat[
108 :     "unable to resolve overloaded operator \"", Atom.toString rator, "\""
109 :     ])
110 :     | tryCandidates (x::xs) = let
111 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = Util.instantiate(Var.typeOf x)
112 :     in
113 :     if U.tryMatchTypes(domTy, argTys)
114 :     then (AST.E_Apply(x, tyArgs, args, rngTy), rngTy)
115 :     else tryCandidates xs
116 :     end
117 :     in
118 :     tryCandidates candidates
119 :     end
120 :    
121 : jhr 70 (* typecheck an expression and translate it to AST *)
122 : jhr 71 fun checkExpr (env, cxt, e) = (case e
123 : jhr 86 of PT.E_Mark m => checkExpr (withEnvAndContext (env, cxt, m))
124 : jhr 71 | PT.E_Var x => (case Env.findVar (env, x)
125 : jhr 81 of SOME x' => let
126 :     val (args, ty) = Util.instantiate(Var.typeOf x')
127 :     in
128 :     (AST.E_Var(x', args, ty), ty)
129 :     end
130 : jhr 88 | NONE => err(cxt, [S "undeclared variable ", A x])
131 : jhr 71 (* end case *))
132 :     | PT.E_Lit lit => checkLit lit
133 : jhr 81 | PT.E_OrElse(e1, e2) => let
134 :     val (e1', ty1) = checkExpr(env, cxt, e1)
135 :     val (e2', ty2) = checkExpr(env, cxt, e2)
136 :     in
137 :     case (ty1, ty2)
138 :     of (Ty.T_Bool, Ty.T_Bool) =>
139 :     (AST.E_Cond(e1', AST.E_Lit(Literal.Bool true), e2'), Ty.T_Bool)
140 :     | _ => raise Fail "arguments to \"||\" must have bool type"
141 :     (* end case *)
142 :     end
143 :     | PT.E_AndAlso(e1, e2) => let
144 :     val (e1', ty1) = checkExpr(env, cxt, e1)
145 :     val (e2', ty2) = checkExpr(env, cxt, e2)
146 :     in
147 :     case (ty1, ty2)
148 :     of (Ty.T_Bool, Ty.T_Bool) =>
149 :     (AST.E_Cond(e1', e2', AST.E_Lit(Literal.Bool false)), Ty.T_Bool)
150 :     | _ => raise Fail "arguments to \"||\" must have bool type"
151 :     (* end case *)
152 :     end
153 :     | PT.E_BinOp(e1, rator, e2) => let
154 :     val (e1', ty1) = checkExpr(env, cxt, e1)
155 :     val (e2', ty2) = checkExpr(env, cxt, e2)
156 :     in
157 :     case Basis.findOp rator
158 :     of [rator] => let
159 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = Util.instantiate(Var.typeOf rator)
160 :     in
161 :     if U.matchTypes(domTy, [ty1, ty2])
162 :     then (AST.E_Apply(rator, tyArgs, [e1', e2'], rngTy), rngTy)
163 : jhr 85 else raise Fail(concat[
164 :     "type error for binary operator \"", Var.nameOf rator, "\""
165 :     ])
166 : jhr 81 end
167 : jhr 85 | ovldList => resolveOverload (rator, [ty1, ty2], [e1', e2'], ovldList)
168 : jhr 81 (* end case *)
169 :     end
170 :     | PT.E_UnaryOp(rator, e) => let
171 :     val (e', ty) = checkExpr(env, cxt, e)
172 :     in
173 :     case Basis.findOp rator
174 :     of [rator] => let
175 :     val (tyArgs, Ty.T_Fun([domTy], rngTy)) = Util.instantiate(Var.typeOf rator)
176 :     in
177 :     if U.matchType(domTy, ty)
178 :     then (AST.E_Apply(rator, tyArgs, [e'], rngTy), rngTy)
179 : jhr 85 else raise Fail(concat[
180 :     "type error for unary operator \"", Var.nameOf rator, "\""
181 :     ])
182 : jhr 81 end
183 : jhr 85 | ovldList => resolveOverload (rator, [ty], [e'], ovldList)
184 : jhr 81 (* end case *)
185 :     end
186 :     | PT.E_Tuple args => let
187 :     val (args, tys) = checkExprList (env, cxt, args)
188 :     in
189 :     raise Fail "E_Tuple not yet implemented"
190 :     end
191 :     | PT.E_Apply(f, args) => let
192 :     val (args, tys) = checkExprList (env, cxt, args)
193 :     in
194 :     case Env.findVar (env, f)
195 :     of SOME f => (case Util.instantiate(Var.typeOf f)
196 :     of (tyArgs, Ty.T_Fun(domTy, rngTy)) =>
197 :     if U.matchTypes(domTy, tys)
198 :     then (AST.E_Apply(f, tyArgs, args, rngTy), rngTy)
199 : jhr 88 else err(cxt, [
200 :     S "type error in application of ", V f, S "\n",
201 :     S " expected: ", TYS domTy, S "\n",
202 :     S " but found: ", TYS tys, S "\n"
203 :     ])
204 : jhr 81 | _ => raise Fail "application of non-function"
205 :     (* end case *))
206 :     | NONE => raise Fail "unknown function"
207 :     (* end case *)
208 :     end
209 : jhr 86 | PT.E_Cons args => let
210 :     val (args, ty::tys) = checkExprList (env, cxt, args)
211 : jhr 81 in
212 : jhr 86 case Util.prune ty
213 :     of Ty.T_Tensor shape => let
214 :     fun chkTy ty' = U.matchType(ty, ty')
215 :     val resTy = Ty.T_Tensor(Ty.shapeExt(shape, Ty.DimConst(List.length args)))
216 : jhr 83 in
217 : jhr 86 if List.all chkTy tys
218 :     then (AST.E_Cons args, resTy)
219 :     else raise Fail "arguments of tensor construction must have same type"
220 : jhr 83 end
221 : jhr 86 | _ => raise Fail "Invalid argument type for tensor construction"
222 : jhr 83 (* end case *)
223 : jhr 81 end
224 : jhr 86 | PT.E_Real e => (case checkExpr (env, cxt, e)
225 :     of (e', Ty.T_Int) =>
226 :     (AST.E_Apply(BasisVars.i2r, [], [e'], Ty.realTy), Ty.realTy)
227 :     | _ => raise Fail "argument of real conversion must be int"
228 :     (* end case *))
229 : jhr 70 (* end case *))
230 :    
231 : jhr 81 (* typecheck a list of expressions returning a list of AST expressions and a list
232 :     * of types of the expressions.
233 :     *)
234 :     and checkExprList (env, cxt, exprs) = let
235 :     fun chk (e, (es, tys)) = let
236 :     val (e, ty) = checkExpr (env, cxt, e)
237 :     in
238 :     (e::es, ty::tys)
239 :     end
240 :     in
241 :     List.foldr chk ([], []) exprs
242 :     end
243 :    
244 : jhr 72 fun checkVarDecl (env, cxt, kind, d) = (case d
245 : jhr 86 of PT.VD_Mark m => checkVarDecl (env, (#1 cxt, #span m), kind, #tree m)
246 : jhr 72 | PT.VD_Decl(ty, x, e) => let
247 : jhr 81 val ty = checkTy (cxt, ty)
248 : jhr 72 val x' = Var.new (x, kind, ty)
249 :     val (e', ty') = checkExpr (env, cxt, e)
250 :     in
251 :     (* FIXME: check types *)
252 : jhr 81 (x, x', e')
253 : jhr 72 end
254 :     (* end case *))
255 :    
256 : jhr 70 (* typecheck a statement and translate it to AST *)
257 : jhr 71 fun checkStmt (env, cxt, s) = (case s
258 : jhr 86 of PT.S_Mark m => checkStmt (withEnvAndContext (env, cxt, m))
259 : jhr 72 | PT.S_Block stms => let
260 :     fun chk (_, [], stms) = AST.S_Block(List.rev stms)
261 :     | chk (env, s::ss, stms) = let
262 :     val (s', env') = checkStmt (env, cxt, s)
263 :     in
264 : jhr 81 chk (env', ss, s'::stms)
265 : jhr 72 end
266 :     in
267 :     (chk (env, stms, []), env)
268 :     end
269 :     | PT.S_Decl vd => let
270 : jhr 81 val (x, x', e) = checkVarDecl (env, cxt, Var.LocalVar, vd)
271 : jhr 72 in
272 : jhr 81 (AST.S_Decl(AST.VD_Decl(x', e)), Env.insertLocal(env, x, x'))
273 : jhr 72 end
274 :     | PT.S_IfThen(e, s) => let
275 :     val (e', ty) = checkExpr (env, cxt, e)
276 : jhr 81 val (s', _) = checkStmt (env, cxt, s)
277 : jhr 72 in
278 :     (* check that condition has bool type *)
279 :     case ty
280 :     of Ty.T_Bool => ()
281 :     | _ => raise Fail "condition not boolean type"
282 :     (* end case *);
283 :     (AST.S_IfThenElse(e', s', AST.S_Block[]), env)
284 :     end
285 :     | PT.S_IfThenElse(e, s1, s2) => let
286 :     val (e', ty) = checkExpr (env, cxt, e)
287 : jhr 81 val (s1', _) = checkStmt (env, cxt, s1)
288 :     val (s2', _) = checkStmt (env, cxt, s2)
289 : jhr 72 in
290 :     (* check that condition has bool type *)
291 :     case ty
292 :     of Ty.T_Bool => ()
293 :     | _ => raise Fail "condition not boolean type"
294 :     (* end case *);
295 :     (AST.S_IfThenElse(e', s1', s2'), env)
296 :     end
297 :     | PT.S_Assign(x, e) => (case Env.findVar (env, x)
298 :     of NONE => raise Fail "undefined variable"
299 :     | SOME x' => let
300 :     val (e', ty) = checkExpr (env, cxt, e)
301 :     in
302 :     (* FIXME: check types *)
303 :     (* check that x' is mutable *)
304 :     case Var.kindOf x'
305 :     of Var.ActorStateVar => ()
306 :     | Var.LocalVar => ()
307 :     | _ => raise Fail "assignment to immutable variable"
308 :     (* end case *);
309 :     (AST.S_Assign(x', e'), env)
310 :     end
311 :     (* end case *))
312 :     | PT.S_New(actor, args) => let
313 : jhr 81 val argsAndTys' = List.map (fn e => checkExpr(env, cxt, e)) args
314 :     val (args', tys') = ListPair.unzip argsAndTys'
315 : jhr 72 in
316 :     (* FIXME: check that actor is defined and has the argument types match *)
317 : jhr 81 (AST.S_New(actor, args'), env)
318 : jhr 72 end
319 :     | PT.S_Die => (AST.S_Die, env)
320 :     | PT.S_Stabilize => (AST.S_Stabilize, env)
321 : jhr 70 (* end case *))
322 :    
323 : jhr 82 fun checkParams (env, cxt, params) = let
324 :     fun chkParam (env, cxt, param) = (case param
325 : jhr 86 of PT.P_Mark m => chkParam (withEnvAndContext (env, cxt, m))
326 : jhr 82 | PT.P_Param(ty, x) => let
327 :     val x' = Var.new(x, AST.ActorParam, checkTy (cxt, ty))
328 :     in
329 :     (x', Env.insertLocal(env, x, x'))
330 :     end
331 :     (* end case *))
332 :     fun chk (param, (xs, env)) = let
333 :     val (x, env) = chkParam (env, cxt, param)
334 :     in
335 :     (x::xs, env)
336 :     end
337 :     in
338 :     (* FIXME: need to check for multiple occurences of the same parameter name! *)
339 :     List.foldr chk ([], env) params
340 :     end
341 :    
342 :     fun checkMethod (env, cxt, meth) = (case meth
343 : jhr 86 of PT.M_Mark m => checkMethod (withEnvAndContext (env, cxt, m))
344 : jhr 82 | PT.M_Method(name, body) => let
345 :     val (body, _) = checkStmt(env, cxt, body)
346 :     in
347 :     AST.M_Method(name, body)
348 :     end
349 :     (* end case *))
350 :    
351 :     fun checkActor (env, cxt, {name, params, state, methods}) = let
352 :     (* check the actor parameters *)
353 :     val (params, env) = checkParams (env, cxt, params)
354 :     (* check the actor state variable definitions *)
355 :     val (vds, env) = let
356 :     fun checkStateVar (vd, (vds, env)) = let
357 :     val (x, x', e') = checkVarDecl (env, cxt, AST.ActorStateVar, vd)
358 :     in
359 :     (AST.VD_Decl(x', e')::vds, Env.insertLocal(env, x, x'))
360 :     end
361 :     val (vds, env) = List.foldl checkStateVar ([], env) state
362 :     in
363 :     (List.rev vds, env)
364 :     end
365 :     (* check the actor methods *)
366 :     val methods = List.map (fn m => checkMethod (env, cxt, m)) methods
367 :     in
368 :     AST.D_Actor{name = name, params = params, state = vds, methods = methods}
369 :     end
370 :    
371 : jhr 71 fun checkDecl (env, cxt, d) = (case d
372 : jhr 86 of PT.D_Mark m => checkDecl (withEnvAndContext (env, cxt, m))
373 : jhr 72 | PT.D_Input(ty, x, optExp) => let
374 : jhr 71 val ty = checkTy(cxt, ty)
375 :     val x' = Var.new(x, Var.InputVar, ty)
376 :     val dcl = (case optExp
377 :     of NONE => AST.D_Input(x', NONE)
378 :     | SOME e => let
379 :     val (e', ty') = checkExpr (env, cxt, e)
380 :     in
381 :     (* FIXME: check types *)
382 :     AST.D_Input(x', SOME e')
383 :     end
384 :     (* end case *))
385 :     in
386 :     (dcl, Env.insertGlobal(env, x, x'))
387 :     end
388 : jhr 72 | PT.D_Var vd => let
389 : jhr 81 val (x, x', e') = checkVarDecl (env, cxt, Var.GlobalVar, vd)
390 : jhr 72 in
391 : jhr 81 (AST.D_Var(AST.VD_Decl(x', e')), Env.insertGlobal(env, x, x'))
392 : jhr 72 end
393 : jhr 82 | PT.D_Actor arg => (checkActor(env, cxt, arg), env)
394 : jhr 81 | PT.D_InitialArray(bindings, iterators) => raise Fail "unimplemented" (* FIXME *)
395 :     | PT.D_InitialCollection(bindings, iterators) => raise Fail "unimplemented" (* FIXME *)
396 : jhr 70 (* end case *))
397 :    
398 : jhr 86 fun check errStrm (PT.Program{span, tree}) = let
399 :     val cxt = (errStrm, span)
400 : jhr 81 fun chk (env, [], dcls') = AST.Program(List.rev dcls')
401 :     | chk (env, dcl::dcls, dcls') = let
402 : jhr 86 val (dcl', env) = checkDecl (env, cxt, dcl)
403 : jhr 81 in
404 :     chk (env, dcls, dcl'::dcls')
405 :     end
406 :     in
407 :     chk (Basis.env, tree, [])
408 :     end
409 : jhr 70
410 : jhr 69 end

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