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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 85 - (view) (download)

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

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