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

SCM Repository

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

Annotation of /trunk/src/compiler/ast/type-util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 381 - (view) (download)

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

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