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 83 - (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 :     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 70 (* typecheck an expression and translate it to AST *)
71 : jhr 71 fun checkExpr (env, cxt, e) = (case e
72 :     of PT.E_Mark m => checkExpr (env, #span m, #tree m)
73 :     | PT.E_Var x => (case Env.findVar (env, x)
74 : jhr 81 of SOME x' => let
75 :     val (args, ty) = Util.instantiate(Var.typeOf x')
76 :     in
77 :     (AST.E_Var(x', args, ty), ty)
78 :     end
79 : jhr 71 | NONE => raise Fail "undefined variable"
80 :     (* end case *))
81 :     | PT.E_Lit lit => checkLit lit
82 : jhr 81 | PT.E_OrElse(e1, e2) => let
83 :     val (e1', ty1) = checkExpr(env, cxt, e1)
84 :     val (e2', ty2) = checkExpr(env, cxt, e2)
85 :     in
86 :     case (ty1, ty2)
87 :     of (Ty.T_Bool, Ty.T_Bool) =>
88 :     (AST.E_Cond(e1', AST.E_Lit(Literal.Bool true), e2'), Ty.T_Bool)
89 :     | _ => raise Fail "arguments to \"||\" must have bool type"
90 :     (* end case *)
91 :     end
92 :     | PT.E_AndAlso(e1, e2) => let
93 :     val (e1', ty1) = checkExpr(env, cxt, e1)
94 :     val (e2', ty2) = checkExpr(env, cxt, e2)
95 :     in
96 :     case (ty1, ty2)
97 :     of (Ty.T_Bool, Ty.T_Bool) =>
98 :     (AST.E_Cond(e1', e2', AST.E_Lit(Literal.Bool false)), Ty.T_Bool)
99 :     | _ => raise Fail "arguments to \"||\" must have bool type"
100 :     (* end case *)
101 :     end
102 :     | PT.E_BinOp(e1, rator, e2) => let
103 :     val (e1', ty1) = checkExpr(env, cxt, e1)
104 :     val (e2', ty2) = checkExpr(env, cxt, e2)
105 :     in
106 :     case Basis.findOp rator
107 :     of [rator] => let
108 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = Util.instantiate(Var.typeOf rator)
109 :     in
110 :     if U.matchTypes(domTy, [ty1, ty2])
111 :     then (AST.E_Apply(rator, tyArgs, [e1', e2'], rngTy), rngTy)
112 :     else raise Fail "type error for binary operator"
113 :     end
114 : jhr 83 | ovldList => raise Fail "overloaded binops unimplemented" (* FIXME *)
115 : jhr 81 (* end case *)
116 :     end
117 :     | PT.E_UnaryOp(rator, e) => let
118 :     val (e', ty) = checkExpr(env, cxt, e)
119 :     in
120 :     case Basis.findOp rator
121 :     of [rator] => let
122 :     val (tyArgs, Ty.T_Fun([domTy], rngTy)) = Util.instantiate(Var.typeOf rator)
123 :     in
124 :     if U.matchType(domTy, ty)
125 :     then (AST.E_Apply(rator, tyArgs, [e'], rngTy), rngTy)
126 :     else raise Fail "type error for binary operator"
127 :     end
128 :     | ovldList => raise Fail "unimplemented" (* FIXME *)
129 :     (* end case *)
130 :     end
131 :     | PT.E_Tuple args => let
132 :     val (args, tys) = checkExprList (env, cxt, args)
133 :     in
134 :     raise Fail "E_Tuple not yet implemented"
135 :     end
136 :     | PT.E_Apply(f, args) => let
137 :     val (args, tys) = checkExprList (env, cxt, args)
138 :     in
139 :     case Env.findVar (env, f)
140 :     of SOME f => (case Util.instantiate(Var.typeOf f)
141 :     of (tyArgs, Ty.T_Fun(domTy, rngTy)) =>
142 :     if U.matchTypes(domTy, tys)
143 :     then (AST.E_Apply(f, tyArgs, args, rngTy), rngTy)
144 :     else raise Fail "type error for application"
145 :     | _ => raise Fail "application of non-function"
146 :     (* end case *))
147 :     | NONE => raise Fail "unknown function"
148 :     (* end case *)
149 :     end
150 :     | PT.E_Cons(ty, args) => let
151 :     val ty = checkTy(cxt, ty)
152 :     val (args, tys) = checkExprList (env, cxt, args)
153 :     in
154 : jhr 83 case (ty, tys)
155 :     of (Ty.T_Tensor(Ty.Shape[]), [Ty.T_Int]) => (* int to real conversion *)
156 :     (AST.E_Apply(BasisVars.i2r, [], args, ty), ty)
157 :     | (Ty.T_Tensor(Ty.Shape[]), _) => raise Fail "invalid \"real\" conversion"
158 :     | (Ty.T_Tensor(Ty.Shape dims), _) => let
159 :     fun getDim (Ty.DimConst k) = k
160 :     | getDim _ = raise Fail "unexpected dimension variable"
161 :     val resultArity = List.foldl (fn (dim, a) => getDim dim * a) 1 dims
162 :     val argArity = List.length args
163 :     in
164 :     if (resultArity = argArity)
165 :     then (AST.E_Cons(ty, args), ty)
166 :     else if (resultArity > argArity)
167 :     then let
168 :     val xArgs = List.tabulate (resultArity-argArity, fn _ => realZero)
169 :     in
170 :     (AST.E_Cons(ty, args@xArgs), ty)
171 :     end
172 :     else raise Fail "arity mismatch in tensor construction"
173 :     end
174 :     (* end case *)
175 : jhr 81 end
176 : jhr 70 (* end case *))
177 :    
178 : jhr 81 (* typecheck a list of expressions returning a list of AST expressions and a list
179 :     * of types of the expressions.
180 :     *)
181 :     and checkExprList (env, cxt, exprs) = let
182 :     fun chk (e, (es, tys)) = let
183 :     val (e, ty) = checkExpr (env, cxt, e)
184 :     in
185 :     (e::es, ty::tys)
186 :     end
187 :     in
188 :     List.foldr chk ([], []) exprs
189 :     end
190 :    
191 : jhr 72 fun checkVarDecl (env, cxt, kind, d) = (case d
192 :     of PT.VD_Mark m => checkVarDecl (env, #span m, kind, #tree m)
193 :     | PT.VD_Decl(ty, x, e) => let
194 : jhr 81 val ty = checkTy (cxt, ty)
195 : jhr 72 val x' = Var.new (x, kind, ty)
196 :     val (e', ty') = checkExpr (env, cxt, e)
197 :     in
198 :     (* FIXME: check types *)
199 : jhr 81 (x, x', e')
200 : jhr 72 end
201 :     (* end case *))
202 :    
203 : jhr 70 (* typecheck a statement and translate it to AST *)
204 : jhr 71 fun checkStmt (env, cxt, s) = (case s
205 :     of PT.S_Mark m => checkStmt (env, #span m, #tree m)
206 : jhr 72 | PT.S_Block stms => let
207 :     fun chk (_, [], stms) = AST.S_Block(List.rev stms)
208 :     | chk (env, s::ss, stms) = let
209 :     val (s', env') = checkStmt (env, cxt, s)
210 :     in
211 : jhr 81 chk (env', ss, s'::stms)
212 : jhr 72 end
213 :     in
214 :     (chk (env, stms, []), env)
215 :     end
216 :     | PT.S_Decl vd => let
217 : jhr 81 val (x, x', e) = checkVarDecl (env, cxt, Var.LocalVar, vd)
218 : jhr 72 in
219 : jhr 81 (AST.S_Decl(AST.VD_Decl(x', e)), Env.insertLocal(env, x, x'))
220 : jhr 72 end
221 :     | PT.S_IfThen(e, s) => let
222 :     val (e', ty) = checkExpr (env, cxt, e)
223 : jhr 81 val (s', _) = checkStmt (env, cxt, s)
224 : jhr 72 in
225 :     (* check that condition has bool type *)
226 :     case ty
227 :     of Ty.T_Bool => ()
228 :     | _ => raise Fail "condition not boolean type"
229 :     (* end case *);
230 :     (AST.S_IfThenElse(e', s', AST.S_Block[]), env)
231 :     end
232 :     | PT.S_IfThenElse(e, s1, s2) => let
233 :     val (e', ty) = checkExpr (env, cxt, e)
234 : jhr 81 val (s1', _) = checkStmt (env, cxt, s1)
235 :     val (s2', _) = checkStmt (env, cxt, s2)
236 : jhr 72 in
237 :     (* check that condition has bool type *)
238 :     case ty
239 :     of Ty.T_Bool => ()
240 :     | _ => raise Fail "condition not boolean type"
241 :     (* end case *);
242 :     (AST.S_IfThenElse(e', s1', s2'), env)
243 :     end
244 :     | PT.S_Assign(x, e) => (case Env.findVar (env, x)
245 :     of NONE => raise Fail "undefined variable"
246 :     | SOME x' => let
247 :     val (e', ty) = checkExpr (env, cxt, e)
248 :     in
249 :     (* FIXME: check types *)
250 :     (* check that x' is mutable *)
251 :     case Var.kindOf x'
252 :     of Var.ActorStateVar => ()
253 :     | Var.LocalVar => ()
254 :     | _ => raise Fail "assignment to immutable variable"
255 :     (* end case *);
256 :     (AST.S_Assign(x', e'), env)
257 :     end
258 :     (* end case *))
259 :     | PT.S_New(actor, args) => let
260 : jhr 81 val argsAndTys' = List.map (fn e => checkExpr(env, cxt, e)) args
261 :     val (args', tys') = ListPair.unzip argsAndTys'
262 : jhr 72 in
263 :     (* FIXME: check that actor is defined and has the argument types match *)
264 : jhr 81 (AST.S_New(actor, args'), env)
265 : jhr 72 end
266 :     | PT.S_Die => (AST.S_Die, env)
267 :     | PT.S_Stabilize => (AST.S_Stabilize, env)
268 : jhr 70 (* end case *))
269 :    
270 : jhr 82 fun checkParams (env, cxt, params) = let
271 :     fun chkParam (env, cxt, param) = (case param
272 :     of PT.P_Mark m => chkParam (env, #span m, #tree m)
273 :     | PT.P_Param(ty, x) => let
274 :     val x' = Var.new(x, AST.ActorParam, checkTy (cxt, ty))
275 :     in
276 :     (x', Env.insertLocal(env, x, x'))
277 :     end
278 :     (* end case *))
279 :     fun chk (param, (xs, env)) = let
280 :     val (x, env) = chkParam (env, cxt, param)
281 :     in
282 :     (x::xs, env)
283 :     end
284 :     in
285 :     (* FIXME: need to check for multiple occurences of the same parameter name! *)
286 :     List.foldr chk ([], env) params
287 :     end
288 :    
289 :     fun checkMethod (env, cxt, meth) = (case meth
290 :     of PT.M_Mark m => checkMethod (env, #span m, #tree m)
291 :     | PT.M_Method(name, body) => let
292 :     val (body, _) = checkStmt(env, cxt, body)
293 :     in
294 :     AST.M_Method(name, body)
295 :     end
296 :     (* end case *))
297 :    
298 :     fun checkActor (env, cxt, {name, params, state, methods}) = let
299 :     (* check the actor parameters *)
300 :     val (params, env) = checkParams (env, cxt, params)
301 :     (* check the actor state variable definitions *)
302 :     val (vds, env) = let
303 :     fun checkStateVar (vd, (vds, env)) = let
304 :     val (x, x', e') = checkVarDecl (env, cxt, AST.ActorStateVar, vd)
305 :     in
306 :     (AST.VD_Decl(x', e')::vds, Env.insertLocal(env, x, x'))
307 :     end
308 :     val (vds, env) = List.foldl checkStateVar ([], env) state
309 :     in
310 :     (List.rev vds, env)
311 :     end
312 :     (* check the actor methods *)
313 :     val methods = List.map (fn m => checkMethod (env, cxt, m)) methods
314 :     in
315 :     AST.D_Actor{name = name, params = params, state = vds, methods = methods}
316 :     end
317 :    
318 : jhr 71 fun checkDecl (env, cxt, d) = (case d
319 :     of PT.D_Mark m => checkDecl (env, #span m, #tree m)
320 : jhr 72 | PT.D_Input(ty, x, optExp) => let
321 : jhr 71 val ty = checkTy(cxt, ty)
322 :     val x' = Var.new(x, Var.InputVar, ty)
323 :     val dcl = (case optExp
324 :     of NONE => AST.D_Input(x', NONE)
325 :     | SOME e => let
326 :     val (e', ty') = checkExpr (env, cxt, e)
327 :     in
328 :     (* FIXME: check types *)
329 :     AST.D_Input(x', SOME e')
330 :     end
331 :     (* end case *))
332 :     in
333 :     (dcl, Env.insertGlobal(env, x, x'))
334 :     end
335 : jhr 72 | PT.D_Var vd => let
336 : jhr 81 val (x, x', e') = checkVarDecl (env, cxt, Var.GlobalVar, vd)
337 : jhr 72 in
338 : jhr 81 (AST.D_Var(AST.VD_Decl(x', e')), Env.insertGlobal(env, x, x'))
339 : jhr 72 end
340 : jhr 82 | PT.D_Actor arg => (checkActor(env, cxt, arg), env)
341 : jhr 81 | PT.D_InitialArray(bindings, iterators) => raise Fail "unimplemented" (* FIXME *)
342 :     | PT.D_InitialCollection(bindings, iterators) => raise Fail "unimplemented" (* FIXME *)
343 : jhr 70 (* end case *))
344 :    
345 : jhr 81 fun check (PT.Program{span, tree}) = let
346 :     fun chk (env, [], dcls') = AST.Program(List.rev dcls')
347 :     | chk (env, dcl::dcls, dcls') = let
348 :     val (dcl', env) = checkDecl (env, span, dcl)
349 :     in
350 :     chk (env, dcls, dcl'::dcls')
351 :     end
352 :     in
353 :     chk (Basis.env, tree, [])
354 :     end
355 : jhr 70
356 : jhr 69 end

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