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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/simplify/eval.sml
ViewVC logotype

Annotation of /trunk/src/compiler/simplify/eval.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 231 (* eval.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 231 * All rights reserved.
7 :     *
8 :     * Evaluation of "static" expressions.
9 :     *)
10 :    
11 : jhr 236 structure Eval : sig
12 : jhr 231
13 : jhr 1140 (* raised if there is an error due to faulty code or input values (e.g., loading an
14 :     * image of the wrong shape.
15 :     *)
16 :     exception Error of string list
17 :    
18 : jhr 236 datatype value
19 :     = BV of bool
20 :     | SV of string
21 :     | IV of IntInf.int
22 : jhr 1116 | RV of real
23 : jhr 2491 | TV of value list (* tensors: values will either be RV or TV *)
24 :     | ImgV of ImageInfo.info * SimpleVar.var
25 : jhr 236
26 : jhr 2491 val evalStatics : SimpleVar.Set.set * Simple.block -> value SimpleVar.Map.map
27 : jhr 236
28 :     end = struct
29 :    
30 : jhr 2491 structure Ty = SimpleTypes
31 : jhr 234 structure BV = BasisVars
32 : jhr 231 structure S = Simple
33 : jhr 2491 structure VMap = SimpleVar.Map
34 :     structure VSet = SimpleVar.Set
35 : jhr 231 structure VTbl = Var.Tbl
36 :    
37 : jhr 1140 exception Error of string list
38 :    
39 : jhr 231 datatype value
40 :     = BV of bool
41 : jhr 234 | SV of string
42 : jhr 231 | IV of IntInf.int
43 : jhr 1116 (* FIXME: we probably should use FloatLit.float values instead of reals! *)
44 :     | RV of real
45 : jhr 2491 | TV of value list (* tensors: values will either be RV or TV *)
46 :     | ImgV of ImageInfo.info * Simple.var
47 : jhr 231
48 : jhr 234 fun toString (BV b) = Bool.toString b
49 :     | toString (IV i) = IntInf.toString i
50 :     | toString (SV s) = concat["\"", String.toString s, "\""]
51 : jhr 1116 | toString (RV r) = Real.toString r
52 :     | toString (TV _) = "tensor"
53 :     (* FIXME: should include x in output *)
54 :     | toString (ImgV(info, x)) = ImageInfo.toString info
55 : jhr 231
56 :     val tbl : (value list -> value) VTbl.hash_table = let
57 : jhr 2491 val tbl = VTbl.mkTable (128, Fail "Eval table")
58 :     fun intBinOp rator [IV a, IV b] = IV(rator(a, b))
59 :     fun tensorBinOp rator [v1, v2] = let
60 :     fun f (TV v1, TV v2) = TV(ListPair.mapEq f (v1, v2))
61 :     | f (RV r1, RV r2) = RV(rator(r1, r2))
62 :     in
63 :     f (v1, v2)
64 :     end
65 :     fun realBinOp rator [RV a, RV b] = RV(rator(a, b))
66 :     fun realUnOp rator [RV a] = RV(rator a)
67 :     fun intCmp rator [IV a, IV b] = BV(rator(a, b))
68 :     fun realCmp rator [RV a, RV b] = BV(rator(a, b))
69 :     fun boolCmp rator [BV a, BV b] = BV(rator(a, b))
70 :     fun stringCmp rator [SV a, SV b] = BV(rator(a, b))
71 :     in
72 :     List.app (VTbl.insert tbl) [
73 :     (BV.add_ii, intBinOp (op +)),
74 :     (BV.add_tt, tensorBinOp (op +)),
75 :     (BV.sub_ii, intBinOp (op -)),
76 :     (BV.sub_tt, tensorBinOp (op -)),
77 :     (BV.mul_ii, intBinOp (op * )),
78 :     (BV.mul_rr, realBinOp (op * )),
79 : jhr 234 (*
80 : jhr 2491 (BV.mul_rt, tensorOp Op.Scale),
81 :     (BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])),
82 : jhr 234 *)
83 : jhr 2491 (BV.div_ii, intBinOp IntInf.quot),
84 :     (BV.div_rr, realBinOp (op /)),
85 : jhr 234 (*
86 : jhr 2491 (BV.div_tr, tensorOp Op.InvScale),
87 : jhr 234 *)
88 : jhr 2491 (BV.lt_ii, intCmp (op <)),
89 :     (BV.lt_rr, realCmp (op <)),
90 :     (BV.lte_ii, intCmp (op <=)),
91 :     (BV.lte_rr, realCmp (op <=)),
92 :     (BV.gte_ii, intCmp (op >=)),
93 :     (BV.gte_rr, realCmp (op >=)),
94 :     (BV.gt_ii, intCmp (op >)),
95 :     (BV.gt_rr, realCmp (op >)),
96 :     (BV.equ_bb, boolCmp (op =)),
97 :     (BV.equ_ii, intCmp (op =)),
98 :     (BV.equ_ss, stringCmp (op =)),
99 :     (BV.equ_rr, realCmp Real.==),
100 :     (BV.neq_bb, boolCmp (op <>)),
101 :     (BV.neq_ii, intCmp (op <>)),
102 :     (BV.neq_ss, stringCmp (op <>)),
103 :     (BV.neq_rr, realCmp Real.!=),
104 :     (BV.neg_i, fn [IV i] => IV(~i)),
105 : jhr 234 (*
106 : jhr 2491 (BV.neg_t, tensorOp Op.Neg),
107 :     (BV.neg_f, fn [FV fld] => FV(FieldDef.neg fld)),
108 :     (BV.op_at, fn (y, _, xs) => assign(y, Op.Probe, xs)),
109 :     (BV.op_D, fn [FV fld] => FV(FieldDef.diff fld)),
110 :     (BV.op_norm, tensorOp Op.Norm),
111 : jhr 245 *)
112 : jhr 2491 (BV.op_not, fn [BV b] => BV(not b)),
113 : jhr 231 (*
114 : jhr 2491 (BV.fn_CL, fn (y, _, xs) => assign(y, Op.CL, xs)),
115 :     (BV.op_convolve, fn [Img info, KV h] => FV(FieldDef.CONV(0, info, h))),
116 :     (BV.fn_inside, fn (y, _, xs) => assign(y, Op.Inside, xs)),
117 : jhr 234 *)
118 : jhr 2491 (BV.fn_max, realBinOp Real.min),
119 :     (BV.fn_min, realBinOp Real.max),
120 :     (BV.fn_modulate, tensorBinOp (op * )),
121 : jhr 231 (*
122 : jhr 2491 (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
123 :     (BV.kn_bspln3, kernel Kernel.bspln3),
124 :     (BV.kn_bspln5, kernel Kernel.bspln5),
125 :     (BV.kn_ctmr, kernel Kernel.ctmr),
126 :     (BV.kn_tent, kernel Kernel.tent),
127 :     (BV.kn_c1tent, kernel Kernel.c1tent),
128 :     (BV.kn_c2ctmr, kernel Kernel.c2ctmr),
129 : jhr 1116 *)
130 : jhr 2491 (BV.i2r, fn [IV i] => RV(real(IntInf.toInt i)))
131 :     ];
132 :     tbl
133 :     end
134 : jhr 231
135 : jhr 2491 fun loadImage ([Ty.DIM d, Ty.SHAPE dd], SV filename) = let
136 :     val info as ImageInfo.ImgInfo{dim, ty=(rng, _), ...} = ImageInfo.getInfo filename
137 :     fun rngToS [] = "real"
138 :     | rngToS dd = concat["tensor[", String.concatWith "," (List.map Int.toString dd), "]"]
139 :     fun error msg = raise Error("image file \"" :: filename :: "\" " :: msg)
140 :     in
141 :     (* check that the expected dimension and actual dimension match *)
142 :     if (d <> dim)
143 :     then error ["has dimension ", Int.toString dim, ", expected ", Int.toString d]
144 :     (* check that the expected shape and actual shape match *)
145 :     else if not(ListPair.allEq (op =) (dd, rng))
146 :     then error ["has range ", rngToS rng, ", expected ", rngToS dd]
147 :     else ();
148 :     info
149 :     end
150 : jhr 234
151 : jhr 231 fun evalVar env x = (case VMap.find (env, x)
152 : jhr 2491 of SOME v => v
153 :     | NONE => raise Fail("undefined variable " ^ SimpleVar.uniqueNameOf x)
154 :     (* end case *))
155 : jhr 231
156 : jhr 2491 (* apply Diderot builtin function *)
157 : jhr 269 fun apply (env, f, mvs, xs) =
158 : jhr 2491 if List.all (fn x => VMap.inDomain(env, x)) xs
159 :     then (* try *)(
160 :     if Var.same(f, BV.fn_load)
161 :     then let
162 :     val [imgName] = xs
163 :     in
164 :     SOME(ImgV(loadImage(mvs, evalVar env imgName), imgName))
165 :     end
166 :     else (case VTbl.find tbl f
167 :     of SOME evalFn => SOME(evalFn (List.map (evalVar env) xs))
168 :     | NONE => NONE
169 :     (* end case *))
170 :     ) handle ex as Error msg => raise ex
171 :     | ex => (
172 :     TextIO.output (TextIO.stdErr, concat [
173 :     Var.uniqueNameOf f, "(",
174 :     String.concatWith "," (List.map SimpleVar.uniqueNameOf xs),
175 :     ") fails with exception ", exnName ex, "\n"
176 :     ]);
177 :     raise ex)
178 :     else NONE
179 : jhr 269
180 :     fun evalExp (env, e) = (case e
181 : jhr 2491 of S.E_Var x => VMap.find (env, x)
182 :     | S.E_Lit(Literal.Int i) => SOME(IV i)
183 :     | S.E_Lit(Literal.Float f) => SOME(RV(FloatLit.toReal f))
184 :     | S.E_Lit(Literal.String s) => SOME(SV s)
185 :     | S.E_Lit(Literal.Bool b) => SOME(BV b)
186 :     | S.E_Tuple _ => raise Fail "E_Tuple"
187 :     | S.E_Apply _ => NONE
188 :     | S.E_Prim(f, mvs, xs, _) => apply(env, f, mvs, xs)
189 :     | S.E_Cons xs => (case evalArgs(env, xs)
190 :     of NONE => NONE
191 :     | SOME vs => SOME(TV vs)
192 :     (* end case *))
193 :     | S.E_Slice(x, indices, _) => (case VMap.find (env, x)
194 :     of SOME v => let
195 :     fun slice (TV vs, SOME ix :: ixs) = (case VMap.find (env, ix)
196 :     of SOME(IV i) => slice (List.nth(vs, IntInf.toInt i), ixs)
197 :     | NONE => raise Subscript
198 :     (* end case *))
199 :     | slice (TV vs, NONE :: ixs) =
200 :     TV(List.map (fn v => slice(v, ixs)) vs)
201 :     | slice (v, []) = v
202 :     in
203 :     SOME(slice(v, indices)) handle Subscript => NONE
204 :     end
205 :     | _ => NONE
206 :     (* end case *))
207 :     | S.E_Input(ty, name, desc, optDefault) => raise Fail "impossible"
208 :     | S.E_LoadImage info => SOME(ImgV info)
209 :     | S.E_Coerce{srcTy, dstTy, x} => NONE
210 :     (* end case *))
211 : jhr 269
212 : jhr 1116 and evalArgs (env, args) = let
213 : jhr 2491 fun eval ([], vs) = SOME(List.rev vs)
214 :     | eval (x::xs, vs) = (case VMap.find(env, x)
215 :     of SOME v => eval(xs, v::vs)
216 :     | NONE => NONE
217 :     (* end case *))
218 :     in
219 :     eval (args, [])
220 :     end
221 : jhr 1116
222 : jhr 2491 fun getInput (ty : Ty.ty, name, optDefault) = (case ty
223 :     of Ty.T_Bool =>
224 :     CmdLineInputs.getInput(name, (Option.map BV) o Bool.fromString, optDefault)
225 :     | Ty.T_Int =>
226 :     CmdLineInputs.getInput(name, (Option.map IV) o IntInf.fromString, optDefault)
227 :     | Ty.T_String => CmdLineInputs.getInput(name, fn s => SOME(SV s), optDefault)
228 :     | Ty.T_Tensor[] =>
229 :     CmdLineInputs.getInput(name, (Option.map RV) o Real.fromString, optDefault)
230 :     | Ty.T_Tensor[d] => let
231 :     fun fromString s = let
232 :     (* first split into fields by "," *)
233 :     val flds = String.fields (fn #"," => true | _ => false) s
234 :     (* then tokenize by white space and flatten *)
235 :     val toks = List.concat(List.map (String.tokens Char.isSpace) flds)
236 :     (* then convert to reals *)
237 :     val vals = List.map (RV o valOf o Real.fromString) toks
238 :     in
239 :     if (List.length vals = d)
240 :     then SOME(TV(vals))
241 :     else NONE
242 :     end
243 :     handle _ => NONE
244 :     in
245 :     CmdLineInputs.getInput(name, fromString, optDefault)
246 :     end
247 :     | Ty.T_Tensor shp => raise Fail "TODO: general tensor inputs"
248 :     | _ => raise Fail(concat[
249 :     "input ", name, " has invalid type ", Ty.toString ty
250 :     ])
251 :     (* end case *))
252 : jhr 1116
253 : jhr 269 fun evalStatics (statics, blk) = let
254 : jhr 2491 fun evalBlock (env, S.Block stms) = let
255 :     exception Done of value VMap.map
256 :     fun evalStm (stm, env) = (case stm
257 :     of S.S_Var _ => raise Fail "unexpected variable decl"
258 :     | S.S_Assign(x, S.E_Input(ty, name, desc, optDefault)) =>
259 :     if VSet.member(statics, x)
260 :     then let
261 :     val optDefault = Option.map (evalVar env) optDefault
262 :     val input = getInput (ty, name, optDefault)
263 :     in
264 :     case input
265 :     of SOME v => VMap.insert(env, x, v)
266 :     | NONE => raise Fail("error getting required input " ^ name)
267 :     (* end case *)
268 :     end
269 :     else env
270 :     | S.S_Assign(x, e) => (case evalExp(env, e)
271 :     of SOME v =>
272 :     (Log.msg(concat["eval assignment: ", SimpleVar.uniqueNameOf x, " = ", toString v, "\n"]);
273 :     VMap.insert(env, x, v)
274 : jhr 269 )
275 : jhr 2491 | NONE => env
276 :     (* end case *))
277 :     | S.S_IfThenElse(x, b1, b2) => (case VMap.find(env, x)
278 :     of SOME(BV true) => evalBlock(env, b1)
279 :     | SOME(BV false) => evalBlock(env, b2)
280 :     | SOME _ => raise Fail "type error"
281 :     | NONE => raise (Done env)
282 :     (* end case *))
283 :     | S.S_New _ => raise Fail "unexpected new strand"
284 :     | S.S_Die => raise Fail "unexpected die"
285 :     | S.S_Stabilize => raise Fail "unexpected stabilize"
286 :     | S.S_Print _ => raise Fail "unexpected print"
287 :     (* end case *))
288 :     in
289 :     (List.foldl evalStm env stms) handle Done env => env
290 :     end
291 :     in
292 :     evalBlock (VMap.empty, blk)
293 :     end
294 : jhr 236
295 : jhr 231 end

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