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

SCM Repository

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

Annotation of /branches/vis12/src/compiler/typechecker/util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1971 - (view) (download)

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

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