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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/typechecker/unify.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/typechecker/unify.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3405 - (view) (download)

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

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