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 1640 - (view) (download)
Original Path: trunk/src/compiler/typechecker/util.sml

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 :     *)
8 :    
9 : jhr 85 structure Util : sig
10 : jhr 80
11 : jhr 85 val matchType : Types.ty * Types.ty -> bool
12 :     val matchTypes : Types.ty list * Types.ty list -> bool
13 :    
14 :     val tryMatchType : Types.ty * Types.ty -> bool
15 :     val tryMatchTypes : Types.ty list * Types.ty list -> bool
16 :    
17 :     val instantiate : Types.scheme -> (Types.meta_var list * Types.ty)
18 :    
19 : jhr 1113 val matchDim : Types.dim * Types.dim -> bool
20 :    
21 : jhr 85 end = struct
22 :    
23 : jhr 80 structure Ty = Types
24 : jhr 81 structure MV = MetaVar
25 : jhr 96 structure TU = TypeUtil
26 : jhr 80
27 : jhr 85 (* a patch list tracks the meta variables that have been updated so that we can undo
28 :     * the effects of unification when just testing for a possible type match.
29 :     *)
30 :    
31 :     fun bindTyVar (pl, tv as Ty.TV{bind as ref NONE, ...}, ty) = (
32 :     bind := SOME ty;
33 :     pl := Ty.TYPE tv :: !pl)
34 :     | bindTyVar _ = raise Fail "rebinding type variable"
35 :    
36 :     fun bindDiffVar (pl, dv as Ty.DfV{bind as ref NONE, ...}, diff) = (
37 :     bind := SOME diff;
38 :     pl := Ty.DIFF dv :: !pl)
39 :     | bindDiffVar _ = raise Fail "rebinding differentiation variable"
40 :    
41 :     fun bindShapeVar (pl, sv as Ty.SV{bind as ref NONE, ...}, shape) = (
42 :     bind := SOME shape;
43 :     pl := Ty.SHAPE sv :: !pl)
44 :     | bindShapeVar _ = raise Fail "rebinding shape variable"
45 :    
46 :     fun bindDimVar (pl, dv as Ty.DV{bind as ref NONE, ...}, dim) = (
47 :     bind := SOME dim;
48 :     pl := Ty.DIM dv :: !pl)
49 :     | bindDimVar _ = raise Fail "rebinding dimension variable"
50 :    
51 :     fun undo pl = let
52 :     fun undo1 (Ty.TYPE(Ty.TV{bind, ...})) = bind := NONE
53 :     | undo1 (Ty.DIFF(Ty.DfV{bind, ...})) = bind := NONE
54 :     | undo1 (Ty.SHAPE(Ty.SV{bind, ...})) = bind := NONE
55 :     | undo1 (Ty.DIM(Ty.DV{bind, ...})) = bind := NONE
56 :     in
57 :     List.map undo1 (!pl)
58 :     end
59 :    
60 : jhr 81 (* FIXME: what about the bounds? *)
61 : jhr 96 fun matchDiff (pl, diff1, diff2) = (case (TU.pruneDiff diff1, TU.pruneDiff diff2)
62 : jhr 81 of (Ty.DiffConst k1, Ty.DiffConst k2) => (k1 = k2)
63 : jhr 85 | (Ty.DiffConst k, Ty.DiffVar(dv, i)) => let
64 : jhr 81 val k' = k+i
65 :     in
66 :     if k' < 0 then false
67 : jhr 85 else (bindDiffVar(pl, dv, Ty.DiffConst k'); true)
68 : jhr 81 end
69 : jhr 85 | (Ty.DiffVar(dv, i), Ty.DiffConst k) => let
70 : jhr 81 val k' = k+i
71 :     in
72 :     if k' < 0 then false
73 : jhr 85 else (bindDiffVar(pl, dv, Ty.DiffConst k'); true)
74 : jhr 81 end
75 :     | (Ty.DiffVar(dv1, i1), Ty.DiffVar(dv2, i2)) => raise Fail "unimplemented" (* FIXME *)
76 :     (* end case *))
77 :    
78 : jhr 96 fun matchDim (pl, dim1, dim2) = (case (TU.pruneDim dim1, TU.pruneDim dim2)
79 : jhr 81 of (Ty.DimConst d1, Ty.DimConst d2) => (d1 = d2)
80 : jhr 85 | (Ty.DimVar dv, dim2) => (bindDimVar(pl, dv, dim2); true)
81 :     | (dim1, Ty.DimVar dv) => (bindDimVar(pl, dv, dim1); true)
82 : jhr 81 (* end case *))
83 :    
84 : jhr 96 fun matchShape (pl, shape1, shape2) = (case (TU.pruneShape shape1, TU.pruneShape shape2)
85 : jhr 81 of (Ty.Shape dd1, Ty.Shape dd2) => let
86 :     fun chk ([], []) = true
87 : jhr 85 | chk (d1::dd1, d2::dd2) = matchDim(pl, d1, d2) andalso chk (dd1, dd2)
88 : jhr 81 | chk _ = false
89 :     in
90 :     chk (dd1, dd2)
91 :     end
92 :     | (Ty.Shape dd, Ty.ShapeExt(shape, d2)) => let
93 :     fun chk ([], _) = false
94 :     | chk ([d], revDD) =
95 : jhr 85 matchDim(pl, d, d2) andalso matchShape(pl, Ty.Shape(List.rev revDD), shape)
96 : jhr 81 | chk (d::dd, revDD) = chk(dd, d::revDD)
97 :     in
98 :     chk (dd, [])
99 :     end
100 : jhr 85 | (Ty.ShapeVar sv, shape) => (bindShapeVar (pl, sv, shape); true)
101 : jhr 81 | (Ty.ShapeExt(shape1, d1), Ty.ShapeExt(shape2, d2)) =>
102 : jhr 85 matchDim(pl, d1, d2) andalso matchShape(pl, shape1, shape2)
103 :     | (shape1, shape2) => matchShape(pl, shape2, shape1)
104 : jhr 81 (* end case *))
105 :    
106 :     (* QUESTION: do we need an occurs check? *)
107 : jhr 85 fun unifyType (pl, ty1, ty2) = let
108 : jhr 1640 fun matchVar (tv1 as Ty.TV{id=id1, bind=b1}, tv2 as Ty.TV{id=id2, bind=b2}) =
109 : jhr 81 if Stamp.same(id1, id2)
110 : jhr 1640 then true
111 :     else (case (!b1, !b2)
112 :     of (SOME ty1, SOME ty2) => match(ty1, ty2)
113 :     | (SOME ty1, NONE) => (bindTyVar (pl, tv2, ty1); true)
114 :     | (NONE, SOME ty2) => (bindTyVar (pl, tv1, ty2); true)
115 :     | (NONE, NONE) => (bindTyVar (pl, tv1, Ty.T_Var tv2); true)
116 :     (* end case *))
117 :     and matchVarTy (tv as Ty.TV{bind, ...}, ty) = (case !bind
118 :     of NONE => (bindTyVar(pl, tv, ty); true)
119 :     | SOME ty' => match(ty', ty)
120 :     (* end case *))
121 :     and match (Ty.T_Var tv1, Ty.T_Var tv2) = matchVar(tv1, tv2)
122 :     | match (Ty.T_Var tv1, ty2) = matchVarTy(tv1, ty2)
123 :     | match (ty1, Ty.T_Var tv2) = matchVarTy(tv2, ty1)
124 : jhr 80 | match (Ty.T_Bool, Ty.T_Bool) = true
125 :     | match (Ty.T_Int, Ty.T_Int) = true
126 :     | match (Ty.T_String, Ty.T_String) = true
127 : jhr 1640 | match (Ty.T_Sequence(ty1, d1), Ty.T_Sequence(ty2, d2)) =
128 :     matchDim(pl, d1, d2) andalso match(ty1, ty2)
129 : jhr 85 | match (Ty.T_Kernel k1, Ty.T_Kernel k2) = matchDiff (pl, k1, k2)
130 :     | match (Ty.T_Tensor s1, Ty.T_Tensor s2) = matchShape (pl, s1, s2)
131 : jhr 80 | match (Ty.T_Image{dim=d1, shape=s1}, Ty.T_Image{dim=d2, shape=s2}) =
132 : jhr 85 matchDim (pl, d1, d2) andalso matchShape(pl, s1, s2)
133 : jhr 80 | match (Ty.T_Field{diff=k1, dim=d1, shape=s1}, Ty.T_Field{diff=k2, dim=d2, shape=s2}) =
134 : jhr 85 matchDiff (pl, k1, k2) andalso matchDim (pl, d1, d2) andalso matchShape(pl, s1, s2)
135 : jhr 81 | match (Ty.T_Fun(tys11, ty12), Ty.T_Fun(tys21, ty22)) =
136 : jhr 85 ListPair.allEq match (tys11, tys21) andalso match (ty12, ty22)
137 : jhr 80 | match _ = false
138 :     in
139 : jhr 96 match (TU.pruneHead ty1, TU.pruneHead ty2)
140 : jhr 80 end
141 :    
142 : jhr 85 fun matchTypes (tys1, tys2) = let
143 :     val pl = ref[]
144 :     in
145 :     ListPair.allEq (fn (ty1, ty2) => unifyType(pl, ty1, ty2)) (tys1, tys2)
146 :     end
147 : jhr 81
148 : jhr 85 fun matchType (ty1, ty2) = unifyType (ref[], ty1, ty2)
149 :    
150 :     (* try to match types; if we fail, all meta-variable bindings are undone *)
151 :     fun tryMatchType (ty1, ty2) = let
152 :     val pl = ref[]
153 :     in
154 :     unifyType(pl, ty1, ty2) orelse (undo pl; false)
155 :     end
156 :    
157 :     (* try to match types; if we fail, all meta-variable bindings are undone *)
158 :     fun tryMatchTypes (tys1, tys2) = let
159 :     val pl = ref[]
160 :     in
161 :     ListPair.allEq (fn (ty1, ty2) => unifyType(pl, ty1, ty2)) (tys1, tys2)
162 :     orelse (undo pl; false)
163 :     end
164 :    
165 : jhr 1113 (* rebind matchDim without patch-list argument *)
166 :     val matchDim = fn (d1, d2) => matchDim(ref [], d1, d2)
167 :    
168 : jhr 96 (* QUESTION: perhaps this function belongs in the TypeUtil module? *)
169 : jhr 81 (* instantiate a type scheme, returning the argument meta variables and the resulting type.
170 :     * Note that we assume that the scheme is closed.
171 :     *)
172 :     fun instantiate ([], ty) = ([], ty)
173 :     | instantiate (mvs, ty) = let
174 :     fun instantiateVar (mv, (mvs, env)) = let
175 :     val mv' = MV.copy mv
176 :     in
177 :     (mv'::mvs, MV.Map.insert(env, mv, mv'))
178 :     end
179 : jhr 242 val (mvs, env) = List.foldr instantiateVar ([], MV.Map.empty) mvs
180 : jhr 82 fun iDiff (Ty.DiffVar(k, i)) = (case MV.Map.find(env, Ty.DIFF k)
181 :     of SOME(Ty.DIFF k) => Ty.DiffVar(k, i)
182 :     | _ => raise Fail "impossible"
183 :     (* end case *))
184 :     | iDiff diff = diff
185 :     fun iDim (Ty.DimVar dv) = (case MV.Map.find(env, Ty.DIM dv)
186 :     of SOME(Ty.DIM dv) => Ty.DimVar dv
187 :     | _ => raise Fail "impossible"
188 :     (* end case *))
189 :     | iDim dim = dim
190 :     fun iShape (Ty.ShapeVar sv) = (case MV.Map.find(env, Ty.SHAPE sv)
191 :     of SOME(Ty.SHAPE sv) => Ty.ShapeVar sv
192 :     | _ => raise Fail "impossible"
193 :     (* end case *))
194 :     | iShape (Ty.ShapeExt(shape, dim)) = Ty.ShapeExt(iShape shape, iDim dim)
195 :     | iShape (Ty.Shape dims) = Ty.Shape(List.map iDim dims)
196 : jhr 81 fun ity (Ty.T_Var tv) = (case MV.Map.find(env, Ty.TYPE tv)
197 :     of SOME(Ty.TYPE tv) => Ty.T_Var tv
198 :     | _ => raise Fail "impossible"
199 :     (* end case *))
200 : jhr 1640 | ity (Ty.T_Sequence(ty, d)) = Ty.T_Sequence(ity ty, iDim d)
201 : jhr 82 | ity (Ty.T_Kernel k) = Ty.T_Kernel(iDiff k)
202 :     | ity (Ty.T_Tensor shape) = Ty.T_Tensor(iShape shape)
203 :     | ity (Ty.T_Image{dim, shape}) = Ty.T_Image{dim=iDim dim, shape=iShape shape}
204 :     | ity (Ty.T_Field{diff, dim, shape}) =
205 :     Ty.T_Field{diff=iDiff diff, dim=iDim dim, shape=iShape shape}
206 :     | ity (Ty.T_Fun(dom, rng)) = Ty.T_Fun(List.map ity dom, ity rng)
207 :     | ity ty = ty
208 : jhr 81 in
209 : jhr 82 (mvs, ity ty)
210 : jhr 81 end
211 :    
212 : jhr 80 end

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