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

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

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