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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/ast/type-util.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/ast/type-util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1116 - (view) (download)
Original Path: trunk/src/compiler/ast/type-util.sml

1 : jhr 63 (* type-util.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 63 * All rights reserved.
5 :     *)
6 :    
7 :     structure TypeUtil : sig
8 :    
9 : jhr 381 (* constructor for building a tensor type of known order, but unknown
10 :     * dimensions.
11 :     *)
12 :     val mkTensorTy : int -> Types.ty
13 :    
14 : jhr 399 (* function to compute the slice of a tensor type based on a boolean
15 :     * mask. The value true in the mask means that the corresponding
16 :     * dimension is being indexed, while false means that it is being
17 :     * copied.
18 :     *)
19 :     val slice : Types.ty * bool list -> Types.ty
20 :    
21 : jhr 228 (* returns true if the type is a value type (bool, int, string, or tensor) *)
22 :     val isValueType : Types.ty -> bool
23 :    
24 : jhr 383 (* prune out instantiated meta variables from a type. We also normalize
25 :     * tensor shapes (i.e., remove 1s).
26 :     *)
27 : jhr 96 val prune : Types.ty -> Types.ty
28 :     val pruneDiff : Types.diff -> Types.diff
29 :     val pruneShape : Types.shape -> Types.shape
30 :     val pruneDim : Types.dim -> Types.dim
31 :    
32 :     (* prune the head of a type *)
33 :     val pruneHead : Types.ty -> Types.ty
34 :    
35 :     (* resolve meta variables to their instantiations (or else variable) *)
36 :     val resolve : Types.ty_var -> Types.ty
37 :     val resolveDiff : Types.diff_var -> Types.diff
38 :     val resolveShape : Types.shape_var -> Types.shape
39 :     val resolveDim : Types.dim_var -> Types.dim
40 :    
41 : jhr 1116 (* equality testing *)
42 :     val sameDim : Types.dim * Types.dim -> bool
43 :    
44 : jhr 95 (* string representations of types, etc *)
45 : jhr 63 val toString : Types.ty -> string
46 : jhr 95 val diffToString : Types.diff -> string
47 :     val shapeToString : Types.shape -> string
48 :     val dimToString : Types.dim -> string
49 : jhr 63
50 :     end = struct
51 :    
52 :     structure Ty = Types
53 : jhr 75 structure MV = MetaVar
54 : jhr 63
55 : jhr 381 (* constructor for building a tensor type of known order, but unknown
56 :     * dimensions.
57 :     *)
58 :     fun mkTensorTy order =
59 :     Ty.T_Tensor(
60 :     Ty.Shape(List.tabulate(order, fn _ => Ty.DimVar(MetaVar.newDimVar()))))
61 :    
62 : jhr 383 (* prune out instantiated meta variables from a type. We also normalize
63 :     * tensor dimensions (i.e., remove 1s).
64 :     *)
65 : jhr 96 fun prune ty = (case ty
66 :     of (ty as Ty.T_Var(Ty.TV{bind, ...})) => (case !bind
67 :     of NONE => ty
68 :     | SOME ty => prune ty
69 :     (* end case *))
70 : jhr 1116 | Ty.T_Sequence(ty, dim) => Ty.T_Sequence(prune ty, pruneDim dim)
71 : jhr 96 | (Ty.T_Kernel diff) => Ty.T_Kernel(pruneDiff diff)
72 :     | (Ty.T_Tensor shape) => Ty.T_Tensor(pruneShape shape)
73 :     | (Ty.T_Image{dim, shape}) => Ty.T_Image{
74 :     dim = pruneDim dim,
75 :     shape = pruneShape shape
76 :     }
77 :     | (Ty.T_Field{diff, dim, shape}) => Ty.T_Field{
78 :     diff = pruneDiff diff,
79 :     dim = pruneDim dim,
80 :     shape = pruneShape shape
81 :     }
82 :     | (Ty.T_Fun(tys1, ty2)) => Ty.T_Fun(List.map prune tys1, prune ty2)
83 :     | ty => ty
84 :     (* end case *))
85 : jhr 63
86 : jhr 96 and pruneDiff (Ty.DiffVar(Ty.DfV{bind=ref(SOME diff), ...}, i)) = (
87 :     case pruneDiff diff
88 :     of Ty.DiffVar(dv, i') => Ty.DiffVar(dv, i+i')
89 :     | Ty.DiffConst i' => Ty.DiffConst(i+i')
90 :     (* end case *))
91 :     | pruneDiff diff = diff
92 : jhr 75
93 : jhr 96 and pruneDim dim = (case dim
94 :     of Ty.DimVar(Ty.DV{bind=ref(SOME dim), ...}) => pruneDim dim
95 :     | dim => dim
96 :     (* end case *))
97 :    
98 : jhr 383 and filterDim dim = (case pruneDim dim
99 :     of Ty.DimConst 1 => NONE
100 :     | dim => SOME dim
101 :     (* end case *))
102 :    
103 : jhr 96 and pruneShape shape = (case shape
104 : jhr 383 of Ty.Shape dd => Ty.Shape(List.mapPartial filterDim dd)
105 : jhr 241 | Ty.ShapeVar(Ty.SV{bind=ref(SOME shape), ...}) => pruneShape shape
106 : jhr 383 | Ty.ShapeExt(shape, dim) => (case filterDim dim
107 :     of SOME dim => Ty.shapeExt(pruneShape shape, dim)
108 :     | NONE => pruneShape shape
109 :     (* end case *))
110 : jhr 96 | _ => shape
111 :     (* end case *))
112 :    
113 :     (* resolve meta variables to their instantiations (or else variable) *)
114 :     fun resolve (tv as Ty.TV{bind, ...}) = (case !bind
115 :     of NONE => Ty.T_Var tv
116 :     | SOME ty => prune ty
117 :     (* end case *))
118 :    
119 :     fun resolveDiff (dv as Ty.DfV{bind, ...}) = (case !bind
120 :     of NONE => Ty.DiffVar(dv, 0)
121 :     | SOME diff => pruneDiff diff
122 :     (* end case *))
123 :    
124 :     fun resolveShape (sv as Ty.SV{bind, ...}) = (case !bind
125 :     of NONE => Ty.ShapeVar sv
126 :     | SOME shape => pruneShape shape
127 :     (* end case *))
128 :    
129 :     fun resolveDim (dv as Ty.DV{bind, ...}) = (case !bind
130 :     of NONE => Ty.DimVar dv
131 :     | SOME dim => pruneDim dim
132 :     (* end case *))
133 :    
134 :     (* prune the head of a type *)
135 :     fun pruneHead ty = let
136 :     fun prune' (ty as Ty.T_Var(Ty.TV{bind, ...})) = (case !bind
137 :     of NONE => ty
138 :     | SOME ty => prune' ty
139 :     (* end case *))
140 : jhr 1116 | prune' (Ty.T_Sequence(ty, dim)) = Ty.T_Sequence(ty, pruneDim dim)
141 : jhr 96 | prune' (Ty.T_Kernel diff) = Ty.T_Kernel(pruneDiff diff)
142 :     | prune' (Ty.T_Tensor shape) = Ty.T_Tensor(pruneShape shape)
143 :     | prune' (Ty.T_Image{dim, shape}) = Ty.T_Image{
144 :     dim = pruneDim dim,
145 :     shape = pruneShape shape
146 :     }
147 :     | prune' (Ty.T_Field{diff, dim, shape}) = Ty.T_Field{
148 :     diff = pruneDiff diff,
149 :     dim = pruneDim dim,
150 :     shape = pruneShape shape
151 :     }
152 :     | prune' ty = ty
153 : jhr 63 in
154 : jhr 96 prune' ty
155 : jhr 63 end
156 :    
157 : jhr 1116 (* returns true if the type is a value type (bool, int, string, or tensor) *)
158 :     fun isValueType ty = (case prune ty
159 :     of Ty.T_Bool => true
160 :     | Ty.T_Int => true
161 :     | Ty.T_String => true
162 :     | Ty.T_Sequence _ => true
163 :     | Ty.T_Tensor _ => true
164 :     | _ => false
165 :     (* end case *))
166 :    
167 :     (* equality testing *)
168 :     fun sameDim (Ty.DimConst d1, Ty.DimConst d2) = (d1 = d2)
169 :     | sameDim (Ty.DimVar v1, Ty.DimVar v2) = MetaVar.sameDimVar(v1, v2)
170 :     | sameDim _ = false
171 :    
172 : jhr 96 fun listToString fmt sep items = String.concatWith sep (List.map fmt items)
173 : jhr 63
174 : jhr 96 fun diffToString diff = (case pruneDiff diff
175 :     of Ty.DiffConst n => Int.toString n
176 :     | Ty.DiffVar(dv, 0) => MV.diffVarToString dv
177 :     | Ty.DiffVar(dv, i) => if i < 0
178 :     then String.concat["(", MV.diffVarToString dv, "-", Int.toString(~i), ")"]
179 :     else String.concat["(", MV.diffVarToString dv, "+", Int.toString i, ")"]
180 :     (* end case *))
181 :    
182 :     fun shapeToString shape = (case pruneShape shape
183 :     of Ty.Shape shape => concat["[", listToString dimToString "," shape, "]"]
184 :     | Ty.ShapeVar sv => MV.shapeVarToString sv
185 :     | Ty.ShapeExt(shape, d) => let
186 :     fun toS (Ty.Shape shape) = (listToString dimToString "," shape) ^ ","
187 :     | toS (Ty.ShapeVar sv) = MV.shapeVarToString sv ^ ";"
188 :     | toS (Ty.ShapeExt(shape, d)) = concat[toS shape, dimToString d, ","]
189 :     in
190 :     toS shape ^ dimToString d
191 :     end
192 :     (* end case *))
193 :    
194 :     and dimToString dim = (case pruneDim dim
195 :     of Ty.DimConst n => Int.toString n
196 :     | Ty.DimVar v => MV.dimVarToString v
197 :     (* end case *))
198 :    
199 :     fun toString ty = (case pruneHead ty
200 : jhr 75 of Ty.T_Var tv => MV.tyVarToString tv
201 : jhr 63 | Ty.T_Bool => "bool"
202 :     | Ty.T_Int => "int"
203 :     | Ty.T_String => "string"
204 : jhr 1116 | Ty.T_Sequence(ty, dim) => concat[toString ty, "[", dimToString dim, "]"]
205 : jhr 75 | Ty.T_Kernel n => "kernel#" ^ diffToString n
206 :     | Ty.T_Tensor(Ty.Shape[]) => "real"
207 :     | Ty.T_Tensor(Ty.Shape[Ty.DimConst 2]) => "vec2"
208 :     | Ty.T_Tensor(Ty.Shape[Ty.DimConst 3]) => "vec3"
209 :     | Ty.T_Tensor(Ty.Shape[Ty.DimConst 4]) => "vec4"
210 : jhr 63 | Ty.T_Tensor shape => "tensor" ^ shapeToString shape
211 :     | Ty.T_Image{dim, shape} => concat[
212 : jhr 75 "image(", dimToString dim, ")", shapeToString shape
213 : jhr 63 ]
214 :     | Ty.T_Field{diff, dim, shape} => concat[
215 : jhr 75 "field#", diffToString diff, "(", dimToString dim,
216 : jhr 63 ")", shapeToString shape
217 :     ]
218 : jhr 81 | Ty.T_Fun(tys1, ty2) => let
219 : jhr 63 fun tysToString [] = "()"
220 :     | tysToString [ty] = toString ty
221 :     | tysToString tys = String.concat[
222 :     "(", listToString toString " * " tys, ")"
223 :     ]
224 :     in
225 : jhr 81 String.concat[tysToString tys1, " -> ", toString ty2]
226 : jhr 63 end
227 :     (* end case *))
228 :    
229 : jhr 399 fun slice (Ty.T_Tensor(Ty.Shape l), mask) = let
230 :     fun f (d, true, dd) = dd
231 :     | f (d, false, dd) = d::dd
232 :     in
233 :     Ty.T_Tensor(Ty.Shape(ListPair.foldr f [] (l, mask)))
234 :     end
235 :     | slice (ty, _) = raise Fail(concat["slice(", toString ty, ", _)"])
236 :    
237 : jhr 63 end

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