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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 80 (* util.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 80 * All rights reserved.
7 :     *
8 :     * Utilities for typechecking
9 :     *)
10 :    
11 : jhr 85 structure Util : sig
12 : jhr 80
13 : jhr 2356 (* when matching two types (ty1 and ty2), there are three possible outcomes:
14 :     * EQ -- types are equal
15 :     * COERCE -- ty2 can be coerced to match ty1 (e.g., int -> float)
16 :     * FAIL -- types do not match
17 :     *)
18 :     datatype match = EQ | COERCE | FAIL
19 : jhr 85
20 : jhr 2356 val matchType : Types.ty * Types.ty -> match
21 : jhr 85
22 : jhr 2356 val tryMatchType : Types.ty * Types.ty -> match
23 :    
24 :     (* attempt to match a list of parameter types with a list of typed arguments. Return
25 :     * the arguments with any required coercions, or NONE on failure.
26 :     *)
27 :     val matchArgs : Types.ty list * AST.expr list * Types.ty list -> AST.expr list option
28 :     val tryMatchArgs : Types.ty list * AST.expr list * Types.ty list -> AST.expr list option
29 :    
30 :     val equalType : Types.ty * Types.ty -> bool
31 :     val equalTypes : Types.ty list * Types.ty list -> bool
32 :    
33 :     val tryEqualType : Types.ty * Types.ty -> bool
34 :     val tryEqualTypes : Types.ty list * Types.ty list -> bool
35 :    
36 : jhr 85 val instantiate : Types.scheme -> (Types.meta_var list * Types.ty)
37 :    
38 : jhr 3053 val equalDiff : Types.diff * Types.diff -> bool
39 : jhr 2356 val equalDim : Types.dim * Types.dim -> bool
40 : jhr 1113
41 : jhr 85 end = struct
42 :    
43 : jhr 80 structure Ty = Types
44 : jhr 81 structure MV = MetaVar
45 : jhr 96 structure TU = TypeUtil
46 : jhr 80
47 : jhr 2356 datatype match = EQ | COERCE | FAIL
48 :    
49 : jhr 85 (* a patch list tracks the meta variables that have been updated so that we can undo
50 :     * the effects of unification when just testing for a possible type match.
51 :     *)
52 :    
53 :     fun bindTyVar (pl, tv as Ty.TV{bind as ref NONE, ...}, ty) = (
54 :     bind := SOME ty;
55 :     pl := Ty.TYPE tv :: !pl)
56 :     | bindTyVar _ = raise Fail "rebinding type variable"
57 :    
58 :     fun bindDiffVar (pl, dv as Ty.DfV{bind as ref NONE, ...}, diff) = (
59 :     bind := SOME diff;
60 :     pl := Ty.DIFF dv :: !pl)
61 :     | bindDiffVar _ = raise Fail "rebinding differentiation variable"
62 :    
63 :     fun bindShapeVar (pl, sv as Ty.SV{bind as ref NONE, ...}, shape) = (
64 :     bind := SOME shape;
65 :     pl := Ty.SHAPE sv :: !pl)
66 :     | bindShapeVar _ = raise Fail "rebinding shape variable"
67 :    
68 :     fun bindDimVar (pl, dv as Ty.DV{bind as ref NONE, ...}, dim) = (
69 :     bind := SOME dim;
70 :     pl := Ty.DIM dv :: !pl)
71 :     | bindDimVar _ = raise Fail "rebinding dimension variable"
72 :    
73 :     fun undo pl = let
74 :     fun undo1 (Ty.TYPE(Ty.TV{bind, ...})) = bind := NONE
75 :     | undo1 (Ty.DIFF(Ty.DfV{bind, ...})) = bind := NONE
76 :     | undo1 (Ty.SHAPE(Ty.SV{bind, ...})) = bind := NONE
77 :     | undo1 (Ty.DIM(Ty.DV{bind, ...})) = bind := NONE
78 :     in
79 :     List.map undo1 (!pl)
80 :     end
81 :    
82 : jhr 81 (* FIXME: what about the bounds? *)
83 : jhr 2356 fun equalDiff (pl, diff1, diff2) = (case (TU.pruneDiff diff1, TU.pruneDiff diff2)
84 : jhr 81 of (Ty.DiffConst k1, Ty.DiffConst k2) => (k1 = k2)
85 : jhr 85 | (Ty.DiffConst k, Ty.DiffVar(dv, i)) => let
86 : jhr 81 val k' = k+i
87 :     in
88 :     if k' < 0 then false
89 : jhr 85 else (bindDiffVar(pl, dv, Ty.DiffConst k'); true)
90 : jhr 81 end
91 : jhr 85 | (Ty.DiffVar(dv, i), Ty.DiffConst k) => let
92 : jhr 81 val k' = k+i
93 :     in
94 :     if k' < 0 then false
95 : jhr 85 else (bindDiffVar(pl, dv, Ty.DiffConst k'); true)
96 : jhr 81 end
97 :     | (Ty.DiffVar(dv1, i1), Ty.DiffVar(dv2, i2)) => raise Fail "unimplemented" (* FIXME *)
98 :     (* end case *))
99 :    
100 : jhr 2356 (* match two differentiation constants where the first is allowed to be less than the second *)
101 :     fun matchDiff (diff1, diff2) = (case (TU.pruneDiff diff1, TU.pruneDiff diff2)
102 :     of (Ty.DiffConst k1, Ty.DiffConst k2) => (k1 <= k2)
103 :     | _ => raise Fail "unimplemented" (* FIXME *)
104 :     (* end case *))
105 :    
106 :     fun equalDim (pl, dim1, dim2) = (case (TU.pruneDim dim1, TU.pruneDim dim2)
107 : jhr 81 of (Ty.DimConst d1, Ty.DimConst d2) => (d1 = d2)
108 : jhr 85 | (Ty.DimVar dv, dim2) => (bindDimVar(pl, dv, dim2); true)
109 :     | (dim1, Ty.DimVar dv) => (bindDimVar(pl, dv, dim1); true)
110 : jhr 81 (* end case *))
111 :    
112 : jhr 2356 fun equalShape (pl, shape1, shape2) = (case (TU.pruneShape shape1, TU.pruneShape shape2)
113 : jhr 81 of (Ty.Shape dd1, Ty.Shape dd2) => let
114 :     fun chk ([], []) = true
115 : jhr 2356 | chk (d1::dd1, d2::dd2) = equalDim(pl, d1, d2) andalso chk (dd1, dd2)
116 : jhr 81 | chk _ = false
117 :     in
118 :     chk (dd1, dd2)
119 :     end
120 :     | (Ty.Shape dd, Ty.ShapeExt(shape, d2)) => let
121 :     fun chk ([], _) = false
122 :     | chk ([d], revDD) =
123 : jhr 2356 equalDim(pl, d, d2) andalso equalShape(pl, Ty.Shape(List.rev revDD), shape)
124 : jhr 81 | chk (d::dd, revDD) = chk(dd, d::revDD)
125 :     in
126 :     chk (dd, [])
127 :     end
128 : jhr 85 | (Ty.ShapeVar sv, shape) => (bindShapeVar (pl, sv, shape); true)
129 : jhr 81 | (Ty.ShapeExt(shape1, d1), Ty.ShapeExt(shape2, d2)) =>
130 : jhr 2356 equalDim(pl, d1, d2) andalso equalShape(pl, shape1, shape2)
131 :     | (shape1, shape2) => equalShape(pl, shape2, shape1)
132 : jhr 81 (* end case *))
133 :    
134 :     (* QUESTION: do we need an occurs check? *)
135 : jhr 85 fun unifyType (pl, ty1, ty2) = let
136 : jhr 1640 fun matchVar (tv1 as Ty.TV{id=id1, bind=b1}, tv2 as Ty.TV{id=id2, bind=b2}) =
137 : jhr 81 if Stamp.same(id1, id2)
138 : jhr 1640 then true
139 :     else (case (!b1, !b2)
140 :     of (SOME ty1, SOME ty2) => match(ty1, ty2)
141 :     | (SOME ty1, NONE) => (bindTyVar (pl, tv2, ty1); true)
142 :     | (NONE, SOME ty2) => (bindTyVar (pl, tv1, ty2); true)
143 :     | (NONE, NONE) => (bindTyVar (pl, tv1, Ty.T_Var tv2); true)
144 :     (* end case *))
145 :     and matchVarTy (tv as Ty.TV{bind, ...}, ty) = (case !bind
146 :     of NONE => (bindTyVar(pl, tv, ty); true)
147 :     | SOME ty' => match(ty', ty)
148 :     (* end case *))
149 :     and match (Ty.T_Var tv1, Ty.T_Var tv2) = matchVar(tv1, tv2)
150 :     | match (Ty.T_Var tv1, ty2) = matchVarTy(tv1, ty2)
151 :     | match (ty1, Ty.T_Var tv2) = matchVarTy(tv2, ty1)
152 : jhr 80 | match (Ty.T_Bool, Ty.T_Bool) = true
153 :     | match (Ty.T_Int, Ty.T_Int) = true
154 :     | match (Ty.T_String, Ty.T_String) = true
155 : jhr 1640 | match (Ty.T_Sequence(ty1, d1), Ty.T_Sequence(ty2, d2)) =
156 : jhr 2356 equalDim(pl, d1, d2) andalso match(ty1, ty2)
157 :     | match (Ty.T_Kernel k1, Ty.T_Kernel k2) = equalDiff (pl, k1, k2)
158 :     | match (Ty.T_Tensor s1, Ty.T_Tensor s2) = equalShape (pl, s1, s2)
159 : jhr 80 | match (Ty.T_Image{dim=d1, shape=s1}, Ty.T_Image{dim=d2, shape=s2}) =
160 : jhr 2356 equalDim (pl, d1, d2) andalso equalShape(pl, s1, s2)
161 : jhr 80 | match (Ty.T_Field{diff=k1, dim=d1, shape=s1}, Ty.T_Field{diff=k2, dim=d2, shape=s2}) =
162 : jhr 2356 equalDiff (pl, k1, k2) andalso equalDim (pl, d1, d2) andalso equalShape(pl, s1, s2)
163 : jhr 81 | match (Ty.T_Fun(tys11, ty12), Ty.T_Fun(tys21, ty22)) =
164 : jhr 85 ListPair.allEq match (tys11, tys21) andalso match (ty12, ty22)
165 : jhr 80 | match _ = false
166 :     in
167 : jhr 96 match (TU.pruneHead ty1, TU.pruneHead ty2)
168 : jhr 80 end
169 :    
170 : jhr 2356 fun unifyTypeWithCoercion (pl, ty1, ty2) = (case (TU.pruneHead ty1, TU.pruneHead ty2)
171 :     of (Ty.T_Tensor shp, Ty.T_Int) =>
172 :     if equalShape (pl, Ty.Shape[], shp) then COERCE else FAIL
173 :     | (Ty.T_Field{diff=k1, dim=d1, shape=s1}, Ty.T_Field{diff=k2, dim=d2, shape=s2}) =>
174 :     if unifyType(pl, ty1, ty2)
175 :     then EQ
176 :     else if matchDiff (k1, k2) andalso equalDim(pl, d1, d2)
177 :     andalso equalShape(pl, s1, s2)
178 :     then COERCE
179 :     else FAIL
180 :     | (ty1, ty2) => if unifyType(pl, ty1, ty2) then EQ else FAIL
181 :     (* end case *))
182 :     (* +DEBUG *
183 :     val unifyTypeWithCoercion = fn (pl, ty1, ty2) => let
184 :     val res = unifyTypeWithCoercion (pl, ty1, ty2)
185 :     val res' = (case res of EQ => "EQ" | COERCE => "COERCE" | FAIL => "FAIL")
186 :     in
187 :     print(concat["unifyTypeWithCoercion (_, ", TU.toString ty1, ", ", TU.toString ty2, ") = ", res', "\n"]);
188 :     res
189 :     end
190 :     * -DEBUG *)
191 :    
192 :     fun equalTypes (tys1, tys2) = let
193 : jhr 85 val pl = ref[]
194 :     in
195 :     ListPair.allEq (fn (ty1, ty2) => unifyType(pl, ty1, ty2)) (tys1, tys2)
196 :     end
197 : jhr 81
198 : jhr 2356 fun equalType (ty1, ty2) = unifyType (ref[], ty1, ty2)
199 : jhr 85
200 :     (* try to match types; if we fail, all meta-variable bindings are undone *)
201 : jhr 2356 fun tryEqualType (ty1, ty2) = let
202 : jhr 85 val pl = ref[]
203 :     in
204 :     unifyType(pl, ty1, ty2) orelse (undo pl; false)
205 :     end
206 :    
207 : jhr 2356 (* try to unify two types to equality; if we fail, all meta-variable bindings are undone *)
208 :     fun tryEqualTypes (tys1, tys2) = let
209 : jhr 85 val pl = ref[]
210 :     in
211 :     ListPair.allEq (fn (ty1, ty2) => unifyType(pl, ty1, ty2)) (tys1, tys2)
212 :     orelse (undo pl; false)
213 :     end
214 :    
215 : jhr 2356 fun matchType (ty1, ty2) = unifyTypeWithCoercion (ref[], ty1, ty2)
216 : jhr 1113
217 : jhr 2356 (* try to unify two type lists to equality; if we fail, all meta-variable bindings are undone *)
218 :     fun tryMatchType (ty1, ty2) = let
219 :     val pl = ref[]
220 :     in
221 :     case unifyTypeWithCoercion (pl, ty1, ty2)
222 :     of FAIL => (undo pl; FAIL)
223 :     | result => result
224 :     (* end case *)
225 :     end
226 :    
227 :     (* attempt to match a list of parameter types with a list of typed arguments. Return
228 :     * the arguments with any required coercions, or NONE on failure.
229 :     *)
230 :     local
231 :     fun matchArgs' (pl, paramTys, args, argTys) = let
232 :     fun matchArgTys ([], [], [], args') = SOME(List.rev args')
233 :     | matchArgTys (ty1::tys1, arg::args, ty2::tys2, args') = (
234 :     case unifyTypeWithCoercion (pl, ty1, ty2)
235 :     of EQ => matchArgTys (tys1, args, tys2, arg::args')
236 :     | COERCE => matchArgTys (tys1, args, tys2, AST.E_Coerce{srcTy=ty2, dstTy=ty1, e=arg}::args')
237 :     | _ => (undo pl; NONE)
238 :     (* end case *))
239 :     | matchArgTys _ = NONE
240 :     in
241 :     matchArgTys (paramTys, args, argTys, [])
242 :     end
243 :     in
244 :     fun matchArgs (paramTys, args, argTys) = matchArgs' (ref[], paramTys, args, argTys)
245 :     fun tryMatchArgs (paramTys, args, argTys) = let
246 :     val pl = ref[]
247 :     in
248 :     case matchArgs' (ref[], paramTys, args, argTys)
249 :     of NONE => (undo pl; NONE)
250 :     | someResult => someResult
251 :     (* end case *)
252 :     end
253 :     end
254 :    
255 : jhr 3053 (* rebind equalDiff without patch-list argument *)
256 :     val equalDiff = fn (diff1, diff2) => equalDiff (ref [], diff1, diff2)
257 :    
258 : jhr 2356 (* rebind equalDim without patch-list argument *)
259 :     val equalDim = fn (d1, d2) => equalDim(ref [], d1, d2)
260 :    
261 : jhr 96 (* QUESTION: perhaps this function belongs in the TypeUtil module? *)
262 : jhr 81 (* instantiate a type scheme, returning the argument meta variables and the resulting type.
263 :     * Note that we assume that the scheme is closed.
264 :     *)
265 :     fun instantiate ([], ty) = ([], ty)
266 :     | instantiate (mvs, ty) = let
267 :     fun instantiateVar (mv, (mvs, env)) = let
268 :     val mv' = MV.copy mv
269 :     in
270 :     (mv'::mvs, MV.Map.insert(env, mv, mv'))
271 :     end
272 : jhr 242 val (mvs, env) = List.foldr instantiateVar ([], MV.Map.empty) mvs
273 : jhr 82 fun iDiff (Ty.DiffVar(k, i)) = (case MV.Map.find(env, Ty.DIFF k)
274 :     of SOME(Ty.DIFF k) => Ty.DiffVar(k, i)
275 :     | _ => raise Fail "impossible"
276 :     (* end case *))
277 :     | iDiff diff = diff
278 :     fun iDim (Ty.DimVar dv) = (case MV.Map.find(env, Ty.DIM dv)
279 :     of SOME(Ty.DIM dv) => Ty.DimVar dv
280 :     | _ => raise Fail "impossible"
281 :     (* end case *))
282 :     | iDim dim = dim
283 :     fun iShape (Ty.ShapeVar sv) = (case MV.Map.find(env, Ty.SHAPE sv)
284 :     of SOME(Ty.SHAPE sv) => Ty.ShapeVar sv
285 :     | _ => raise Fail "impossible"
286 :     (* end case *))
287 :     | iShape (Ty.ShapeExt(shape, dim)) = Ty.ShapeExt(iShape shape, iDim dim)
288 :     | iShape (Ty.Shape dims) = Ty.Shape(List.map iDim dims)
289 : jhr 81 fun ity (Ty.T_Var tv) = (case MV.Map.find(env, Ty.TYPE tv)
290 :     of SOME(Ty.TYPE tv) => Ty.T_Var tv
291 :     | _ => raise Fail "impossible"
292 :     (* end case *))
293 : jhr 2356 | ity Ty.T_Bool = Ty.T_Bool
294 :     | ity Ty.T_Int = Ty.T_Int
295 :     | ity Ty.T_String = Ty.T_String
296 : jhr 1640 | ity (Ty.T_Sequence(ty, d)) = Ty.T_Sequence(ity ty, iDim d)
297 : jhr 82 | ity (Ty.T_Kernel k) = Ty.T_Kernel(iDiff k)
298 :     | ity (Ty.T_Tensor shape) = Ty.T_Tensor(iShape shape)
299 :     | ity (Ty.T_Image{dim, shape}) = Ty.T_Image{dim=iDim dim, shape=iShape shape}
300 :     | ity (Ty.T_Field{diff, dim, shape}) =
301 :     Ty.T_Field{diff=iDiff diff, dim=iDim dim, shape=iShape shape}
302 :     | ity (Ty.T_Fun(dom, rng)) = Ty.T_Fun(List.map ity dom, ity rng)
303 : jhr 81 in
304 : jhr 82 (mvs, ity ty)
305 : jhr 81 end
306 :    
307 : jhr 80 end

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