adamc@1200
|
1 (* Copyright (c) 2010, Adam Chlipala
|
adamc@1200
|
2 * All rights reserved.
|
adamc@1200
|
3 *
|
adamc@1200
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@1200
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@1200
|
6 *
|
adamc@1200
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@1200
|
8 * this list of conditions and the following disclaimer.
|
adamc@1200
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@1200
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@1200
|
11 * and/or other materials provided with the distribution.
|
adamc@1200
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@1200
|
13 * derived from this software without specific prior written permission.
|
adamc@1200
|
14 *
|
adamc@1200
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@1200
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@1200
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@1200
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@1200
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@1200
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@1200
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@1200
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@1200
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@1200
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@1200
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@1200
|
26 *)
|
adamc@1200
|
27
|
adamc@1200
|
28 structure Iflow :> IFLOW = struct
|
adamc@1200
|
29
|
adamc@1200
|
30 open Mono
|
adamc@1200
|
31
|
adamc@1202
|
32 structure IM = IntBinaryMap
|
adamc@1202
|
33
|
adamc@1200
|
34 structure SS = BinarySetFn(struct
|
adamc@1200
|
35 type ord_key = string
|
adamc@1200
|
36 val compare = String.compare
|
adamc@1200
|
37 end)
|
adamc@1200
|
38
|
adamc@1200
|
39 val writers = ["htmlifyInt_w",
|
adamc@1200
|
40 "htmlifyFloat_w",
|
adamc@1200
|
41 "htmlifyString_w",
|
adamc@1200
|
42 "htmlifyBool_w",
|
adamc@1200
|
43 "htmlifyTime_w",
|
adamc@1200
|
44 "attrifyInt_w",
|
adamc@1200
|
45 "attrifyFloat_w",
|
adamc@1200
|
46 "attrifyString_w",
|
adamc@1200
|
47 "attrifyChar_w",
|
adamc@1200
|
48 "urlifyInt_w",
|
adamc@1200
|
49 "urlifyFloat_w",
|
adamc@1200
|
50 "urlifyString_w",
|
adamc@1200
|
51 "urlifyBool_w"]
|
adamc@1200
|
52
|
adamc@1200
|
53 val writers = SS.addList (SS.empty, writers)
|
adamc@1200
|
54
|
adamc@1200
|
55 type lvar = int
|
adamc@1200
|
56
|
adamc@1200
|
57 datatype exp =
|
adamc@1200
|
58 Const of Prim.t
|
adamc@1200
|
59 | Var of int
|
adamc@1200
|
60 | Lvar of lvar
|
adamc@1200
|
61 | Func of string * exp list
|
adamc@1200
|
62 | Recd of (string * exp) list
|
adamc@1200
|
63 | Proj of exp * string
|
adamc@1200
|
64 | Finish
|
adamc@1200
|
65
|
adamc@1200
|
66 datatype reln =
|
adamc@1200
|
67 Sql of string
|
adamc@1200
|
68 | Eq
|
adamc@1200
|
69
|
adamc@1200
|
70 datatype prop =
|
adamc@1200
|
71 True
|
adamc@1200
|
72 | False
|
adamc@1200
|
73 | Unknown
|
adamc@1200
|
74 | And of prop * prop
|
adamc@1200
|
75 | Or of prop * prop
|
adamc@1200
|
76 | Reln of reln * exp list
|
adamc@1200
|
77 | Select of int * lvar * lvar * prop * exp
|
adamc@1200
|
78
|
adamc@1200
|
79 local
|
adamc@1202
|
80 val count = ref 1
|
adamc@1200
|
81 in
|
adamc@1200
|
82 fun newLvar () =
|
adamc@1200
|
83 let
|
adamc@1200
|
84 val n = !count
|
adamc@1200
|
85 in
|
adamc@1200
|
86 count := n + 1;
|
adamc@1200
|
87 n
|
adamc@1200
|
88 end
|
adamc@1200
|
89 end
|
adamc@1200
|
90
|
adamc@1200
|
91 fun subExp (v, lv) =
|
adamc@1200
|
92 let
|
adamc@1200
|
93 fun sub e =
|
adamc@1200
|
94 case e of
|
adamc@1200
|
95 Const _ => e
|
adamc@1200
|
96 | Var v' => if v' = v then Lvar lv else e
|
adamc@1200
|
97 | Lvar _ => e
|
adamc@1200
|
98 | Func (f, es) => Func (f, map sub es)
|
adamc@1200
|
99 | Recd xes => Recd (map (fn (x, e) => (x, sub e)) xes)
|
adamc@1200
|
100 | Proj (e, s) => Proj (sub e, s)
|
adamc@1200
|
101 | Finish => Finish
|
adamc@1200
|
102 in
|
adamc@1200
|
103 sub
|
adamc@1200
|
104 end
|
adamc@1200
|
105
|
adamc@1200
|
106 fun subProp (v, lv) =
|
adamc@1200
|
107 let
|
adamc@1200
|
108 fun sub p =
|
adamc@1200
|
109 case p of
|
adamc@1200
|
110 True => p
|
adamc@1200
|
111 | False => p
|
adamc@1200
|
112 | Unknown => p
|
adamc@1200
|
113 | And (p1, p2) => And (sub p1, sub p2)
|
adamc@1200
|
114 | Or (p1, p2) => Or (sub p1, sub p2)
|
adamc@1200
|
115 | Reln (r, es) => Reln (r, map (subExp (v, lv)) es)
|
adamc@1200
|
116 | Select (v1, lv1, lv2, p, e) => Select (v1, lv1, lv2, sub p, subExp (v, lv) e)
|
adamc@1200
|
117 in
|
adamc@1200
|
118 sub
|
adamc@1200
|
119 end
|
adamc@1200
|
120
|
adamc@1200
|
121 fun isKnown e =
|
adamc@1200
|
122 case e of
|
adamc@1200
|
123 Const _ => true
|
adamc@1200
|
124 | Func (_, es) => List.all isKnown es
|
adamc@1200
|
125 | Recd xes => List.all (isKnown o #2) xes
|
adamc@1200
|
126 | Proj (e, _) => isKnown e
|
adamc@1200
|
127 | _ => false
|
adamc@1200
|
128
|
adamc@1200
|
129 fun isFinish e =
|
adamc@1200
|
130 case e of
|
adamc@1200
|
131 Finish => true
|
adamc@1200
|
132 | _ => false
|
adamc@1200
|
133
|
adamc@1200
|
134 fun simplify e =
|
adamc@1200
|
135 case e of
|
adamc@1200
|
136 Const _ => e
|
adamc@1200
|
137 | Var _ => e
|
adamc@1200
|
138 | Lvar _ => e
|
adamc@1200
|
139 | Func (f, es) =>
|
adamc@1200
|
140 let
|
adamc@1200
|
141 val es = map simplify es
|
adamc@1200
|
142 in
|
adamc@1200
|
143 if List.exists isFinish es then
|
adamc@1200
|
144 Finish
|
adamc@1200
|
145 else
|
adamc@1200
|
146 Func (f, es)
|
adamc@1200
|
147 end
|
adamc@1200
|
148 | Recd xes =>
|
adamc@1200
|
149 let
|
adamc@1200
|
150 val xes = map (fn (x, e) => (x, simplify e)) xes
|
adamc@1200
|
151 in
|
adamc@1200
|
152 if List.exists (isFinish o #2) xes then
|
adamc@1200
|
153 Finish
|
adamc@1200
|
154 else
|
adamc@1200
|
155 Recd xes
|
adamc@1200
|
156 end
|
adamc@1200
|
157 | Proj (e, s) =>
|
adamc@1200
|
158 (case simplify e of
|
adamc@1200
|
159 Recd xes =>
|
adamc@1200
|
160 getOpt (ListUtil.search (fn (x, e') => if x = s then SOME e' else NONE) xes, Recd xes)
|
adamc@1200
|
161 | e' =>
|
adamc@1200
|
162 if isFinish e' then
|
adamc@1200
|
163 Finish
|
adamc@1200
|
164 else
|
adamc@1200
|
165 Proj (e', s))
|
adamc@1200
|
166 | Finish => Finish
|
adamc@1200
|
167
|
adamc@1202
|
168 fun decomp fals or =
|
adamc@1200
|
169 let
|
adamc@1200
|
170 fun decomp p k =
|
adamc@1200
|
171 case p of
|
adamc@1200
|
172 True => k []
|
adamc@1202
|
173 | False => fals
|
adamc@1200
|
174 | Unknown => k []
|
adamc@1200
|
175 | And (p1, p2) =>
|
adamc@1200
|
176 decomp p1 (fn ps1 =>
|
adamc@1200
|
177 decomp p2 (fn ps2 =>
|
adamc@1200
|
178 k (ps1 @ ps2)))
|
adamc@1200
|
179 | Or (p1, p2) =>
|
adamc@1200
|
180 or (decomp p1 k, fn () => decomp p2 k)
|
adamc@1200
|
181 | Reln x => k [x]
|
adamc@1200
|
182 | Select _ => k []
|
adamc@1200
|
183 in
|
adamc@1200
|
184 decomp
|
adamc@1200
|
185 end
|
adamc@1200
|
186
|
adamc@1202
|
187 val unif = ref (IM.empty : exp IM.map)
|
adamc@1200
|
188
|
adamc@1202
|
189 fun lvarIn lv =
|
adamc@1202
|
190 let
|
adamc@1202
|
191 fun lvi e =
|
adamc@1202
|
192 case e of
|
adamc@1202
|
193 Const _ => false
|
adamc@1202
|
194 | Var _ => false
|
adamc@1202
|
195 | Lvar lv' => lv' = lv
|
adamc@1202
|
196 | Func (_, es) => List.exists lvi es
|
adamc@1202
|
197 | Recd xes => List.exists (lvi o #2) xes
|
adamc@1202
|
198 | Proj (e, _) => lvi e
|
adamc@1202
|
199 | Finish => false
|
adamc@1202
|
200 in
|
adamc@1202
|
201 lvi
|
adamc@1202
|
202 end
|
adamc@1202
|
203
|
adamc@1202
|
204 fun eq' (e1, e2) =
|
adamc@1202
|
205 case (e1, e2) of
|
adamc@1202
|
206 (Const p1, Const p2) => Prim.equal (p1, p2)
|
adamc@1202
|
207 | (Var n1, Var n2) => n1 = n2
|
adamc@1202
|
208
|
adamc@1202
|
209 | (Lvar n1, _) =>
|
adamc@1202
|
210 (case IM.find (!unif, n1) of
|
adamc@1202
|
211 SOME e1 => eq' (e1, e2)
|
adamc@1202
|
212 | NONE =>
|
adamc@1202
|
213 case e2 of
|
adamc@1202
|
214 Lvar n2 =>
|
adamc@1202
|
215 (case IM.find (!unif, n2) of
|
adamc@1202
|
216 SOME e2 => eq' (e1, e2)
|
adamc@1202
|
217 | NONE => n1 = n2
|
adamc@1202
|
218 orelse (unif := IM.insert (!unif, n1, e2);
|
adamc@1202
|
219 true))
|
adamc@1202
|
220 | _ =>
|
adamc@1202
|
221 if lvarIn n1 e2 then
|
adamc@1202
|
222 false
|
adamc@1202
|
223 else
|
adamc@1202
|
224 (unif := IM.insert (!unif, n1, e2);
|
adamc@1202
|
225 true))
|
adamc@1202
|
226
|
adamc@1202
|
227 | (_, Lvar n2) =>
|
adamc@1202
|
228 (case IM.find (!unif, n2) of
|
adamc@1202
|
229 SOME e2 => eq' (e1, e2)
|
adamc@1202
|
230 | NONE =>
|
adamc@1202
|
231 if lvarIn n2 e1 then
|
adamc@1202
|
232 false
|
adamc@1202
|
233 else
|
adamc@1202
|
234 (unif := IM.insert (!unif, n2, e1);
|
adamc@1202
|
235 true))
|
adamc@1202
|
236
|
adamc@1202
|
237 | (Func (f1, es1), Func (f2, es2)) => f1 = f2 andalso ListPair.allEq eq' (es1, es2)
|
adamc@1202
|
238 | (Recd xes1, Recd xes2) => ListPair.allEq (fn ((x1, e1), (x2, e2)) => x1 = x2 andalso eq' (e1, e2)) (xes1, xes2)
|
adamc@1202
|
239 | (Proj (e1, s1), Proj (e2, s2)) => eq' (e1, e2) andalso s1 = s2
|
adamc@1202
|
240 | (Finish, Finish) => true
|
adamc@1202
|
241 | _ => false
|
adamc@1202
|
242
|
adamc@1202
|
243 fun eq (e1, e2) =
|
adamc@1202
|
244 let
|
adamc@1202
|
245 val saved = !unif
|
adamc@1202
|
246 in
|
adamc@1202
|
247 if eq' (simplify e1, simplify e2) then
|
adamc@1202
|
248 true
|
adamc@1202
|
249 else
|
adamc@1202
|
250 (unif := saved;
|
adamc@1202
|
251 false)
|
adamc@1202
|
252 end
|
adamc@1202
|
253
|
adamc@1202
|
254 exception Imply of prop * prop
|
adamc@1202
|
255
|
adamc@1202
|
256 fun rimp ((r1, es1), (r2, es2)) =
|
adamc@1202
|
257 case (r1, r2) of
|
adamc@1202
|
258 (Sql r1', Sql r2') =>
|
adamc@1202
|
259 r1' = r2' andalso
|
adamc@1202
|
260 (case (es1, es2) of
|
adamc@1202
|
261 ([Recd xes1], [Recd xes2]) =>
|
adamc@1202
|
262 let
|
adamc@1202
|
263 val saved = !unif
|
adamc@1202
|
264 in
|
adamc@1202
|
265 (*print ("Go: " ^ r1' ^ "\n");*)
|
adamc@1202
|
266 (*raise Imply (Reln (r1, es1), Reln (r2, es2));*)
|
adamc@1202
|
267 if List.all (fn (f, e2) =>
|
adamc@1202
|
268 List.exists (fn (f', e1) =>
|
adamc@1202
|
269 f' = f andalso eq (e1, e2)) xes1) xes2 then
|
adamc@1202
|
270 true
|
adamc@1202
|
271 else
|
adamc@1202
|
272 (unif := saved;
|
adamc@1202
|
273 false)
|
adamc@1202
|
274 end
|
adamc@1202
|
275 | _ => false)
|
adamc@1202
|
276 | (Eq, Eq) =>
|
adamc@1202
|
277 (case (es1, es2) of
|
adamc@1202
|
278 ([x1, y1], [x2, y2]) =>
|
adamc@1202
|
279 let
|
adamc@1202
|
280 val saved = !unif
|
adamc@1202
|
281 in
|
adamc@1202
|
282 if eq (x1, x2) andalso eq (y1, y2) then
|
adamc@1202
|
283 true
|
adamc@1202
|
284 else
|
adamc@1202
|
285 (unif := saved;
|
adamc@1202
|
286 (*raise Imply (Reln (Eq, es1), Reln (Eq, es2));*)
|
adamc@1202
|
287 eq (x1, y2) andalso eq (y1, x2))
|
adamc@1202
|
288 end
|
adamc@1202
|
289 | _ => false)
|
adamc@1202
|
290 | _ => false
|
adamc@1202
|
291
|
adamc@1202
|
292 fun imply (p1, p2) =
|
adamc@1202
|
293 (unif := IM.empty;
|
adamc@1202
|
294 (*raise (Imply (p1, p2));*)
|
adamc@1202
|
295 decomp true (fn (e1, e2) => e1 andalso e2 ()) p1
|
adamc@1202
|
296 (fn hyps =>
|
adamc@1202
|
297 decomp false (fn (e1, e2) => e1 orelse e2 ()) p2
|
adamc@1202
|
298 (fn goals =>
|
adamc@1202
|
299 let
|
adamc@1202
|
300 fun gls goals onFail =
|
adamc@1202
|
301 case goals of
|
adamc@1202
|
302 [] => true
|
adamc@1202
|
303 | g :: goals =>
|
adamc@1202
|
304 let
|
adamc@1202
|
305 fun hps hyps =
|
adamc@1202
|
306 case hyps of
|
adamc@1202
|
307 [] => onFail ()
|
adamc@1202
|
308 | h :: hyps =>
|
adamc@1202
|
309 let
|
adamc@1202
|
310 val saved = !unif
|
adamc@1202
|
311 in
|
adamc@1202
|
312 if rimp (h, g) then
|
adamc@1202
|
313 let
|
adamc@1202
|
314 val changed = IM.numItems (!unif) = IM.numItems saved
|
adamc@1202
|
315 in
|
adamc@1202
|
316 gls goals (fn () => (unif := saved;
|
adamc@1202
|
317 changed andalso hps hyps))
|
adamc@1202
|
318 end
|
adamc@1202
|
319 else
|
adamc@1202
|
320 hps hyps
|
adamc@1202
|
321 end
|
adamc@1202
|
322 in
|
adamc@1202
|
323 hps hyps
|
adamc@1202
|
324 end
|
adamc@1202
|
325 in
|
adamc@1202
|
326 gls goals (fn () => false)
|
adamc@1202
|
327 end)))
|
adamc@1202
|
328
|
adamc@1200
|
329
|
adamc@1200
|
330 fun patCon pc =
|
adamc@1200
|
331 case pc of
|
adamc@1200
|
332 PConVar n => "C" ^ Int.toString n
|
adamc@1200
|
333 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
|
adamc@1200
|
334
|
adamc@1202
|
335
|
adamc@1200
|
336
|
adamc@1200
|
337 datatype chunk =
|
adamc@1200
|
338 String of string
|
adamc@1200
|
339 | Exp of Mono.exp
|
adamc@1200
|
340
|
adamc@1200
|
341 fun chunkify e =
|
adamc@1200
|
342 case #1 e of
|
adamc@1200
|
343 EPrim (Prim.String s) => [String s]
|
adamc@1200
|
344 | EStrcat (e1, e2) => chunkify e1 @ chunkify e2
|
adamc@1200
|
345 | _ => [Exp e]
|
adamc@1200
|
346
|
adamc@1201
|
347 type 'a parser = chunk list -> ('a * chunk list) option
|
adamc@1201
|
348
|
adamc@1201
|
349 fun always v chs = SOME (v, chs)
|
adamc@1201
|
350
|
adamc@1202
|
351 fun parse p s =
|
adamc@1202
|
352 case p (chunkify s) of
|
adamc@1201
|
353 SOME (v, []) => SOME v
|
adamc@1201
|
354 | _ => NONE
|
adamc@1201
|
355
|
adamc@1201
|
356 fun const s chs =
|
adamc@1201
|
357 case chs of
|
adamc@1201
|
358 String s' :: chs => if String.isPrefix s s' then
|
adamc@1201
|
359 SOME ((), if size s = size s' then
|
adamc@1201
|
360 chs
|
adamc@1201
|
361 else
|
adamc@1201
|
362 String (String.extract (s', size s, NONE)) :: chs)
|
adamc@1201
|
363 else
|
adamc@1201
|
364 NONE
|
adamc@1201
|
365 | _ => NONE
|
adamc@1201
|
366
|
adamc@1201
|
367 fun follow p1 p2 chs =
|
adamc@1201
|
368 case p1 chs of
|
adamc@1201
|
369 NONE => NONE
|
adamc@1201
|
370 | SOME (v1, chs) =>
|
adamc@1201
|
371 case p2 chs of
|
adamc@1201
|
372 NONE => NONE
|
adamc@1201
|
373 | SOME (v2, chs) => SOME ((v1, v2), chs)
|
adamc@1201
|
374
|
adamc@1201
|
375 fun wrap p f chs =
|
adamc@1201
|
376 case p chs of
|
adamc@1201
|
377 NONE => NONE
|
adamc@1201
|
378 | SOME (v, chs) => SOME (f v, chs)
|
adamc@1201
|
379
|
adamc@1201
|
380 fun alt p1 p2 chs =
|
adamc@1201
|
381 case p1 chs of
|
adamc@1201
|
382 NONE => p2 chs
|
adamc@1201
|
383 | v => v
|
adamc@1201
|
384
|
adamc@1201
|
385 fun skip cp chs =
|
adamc@1201
|
386 case chs of
|
adamc@1201
|
387 String "" :: chs => skip cp chs
|
adamc@1201
|
388 | String s :: chs' => if cp (String.sub (s, 0)) then
|
adamc@1201
|
389 skip cp (String (String.extract (s, 1, NONE)) :: chs')
|
adamc@1201
|
390 else
|
adamc@1201
|
391 SOME ((), chs)
|
adamc@1201
|
392 | _ => SOME ((), chs)
|
adamc@1201
|
393
|
adamc@1201
|
394 fun keep cp chs =
|
adamc@1201
|
395 case chs of
|
adamc@1201
|
396 String "" :: chs => keep cp chs
|
adamc@1201
|
397 | String s :: chs' =>
|
adamc@1201
|
398 let
|
adamc@1201
|
399 val (befor, after) = Substring.splitl cp (Substring.full s)
|
adamc@1201
|
400 in
|
adamc@1201
|
401 if Substring.isEmpty befor then
|
adamc@1201
|
402 NONE
|
adamc@1201
|
403 else
|
adamc@1201
|
404 SOME (Substring.string befor,
|
adamc@1201
|
405 if Substring.isEmpty after then
|
adamc@1201
|
406 chs'
|
adamc@1201
|
407 else
|
adamc@1201
|
408 String (Substring.string after) :: chs')
|
adamc@1201
|
409 end
|
adamc@1201
|
410 | _ => NONE
|
adamc@1201
|
411
|
adamc@1201
|
412 fun ws p = wrap (follow p (skip (fn ch => ch = #" "))) #1
|
adamc@1201
|
413
|
adamc@1201
|
414 fun list p chs =
|
adamc@1201
|
415 (alt (wrap (follow p (follow (ws (const ",")) (list p)))
|
adamc@1201
|
416 (fn (v, ((), ls)) => v :: ls))
|
adamc@1201
|
417 (alt (wrap (ws p) (fn v => [v]))
|
adamc@1201
|
418 (always []))) chs
|
adamc@1201
|
419
|
adamc@1201
|
420 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
|
adamc@1201
|
421
|
adamc@1201
|
422 val t_ident = wrap ident (fn s => if String.isPrefix "T_" s then
|
adamc@1201
|
423 String.extract (s, 2, NONE)
|
adamc@1201
|
424 else
|
adamc@1201
|
425 raise Fail "Iflow: Bad table variable")
|
adamc@1201
|
426 val uw_ident = wrap ident (fn s => if String.isPrefix "uw_" s then
|
adamc@1201
|
427 String.extract (s, 3, NONE)
|
adamc@1201
|
428 else
|
adamc@1201
|
429 raise Fail "Iflow: Bad uw_* variable")
|
adamc@1201
|
430
|
adamc@1201
|
431 val sitem = wrap (follow t_ident
|
adamc@1201
|
432 (follow (const ".")
|
adamc@1201
|
433 uw_ident))
|
adamc@1201
|
434 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
435
|
adamc@1201
|
436 val select = wrap (follow (const "SELECT ") (list sitem))
|
adamc@1201
|
437 (fn ((), ls) => ls)
|
adamc@1201
|
438
|
adamc@1201
|
439 val fitem = wrap (follow uw_ident
|
adamc@1201
|
440 (follow (const " AS ")
|
adamc@1201
|
441 t_ident))
|
adamc@1201
|
442 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
443
|
adamc@1201
|
444 val from = wrap (follow (const "FROM ") (list fitem))
|
adamc@1201
|
445 (fn ((), ls) => ls)
|
adamc@1201
|
446
|
adamc@1201
|
447 val query = wrap (follow select from)
|
adamc@1201
|
448 (fn (fs, ts) => {Select = fs, From = ts})
|
adamc@1201
|
449
|
adamc@1202
|
450 fun queryProp rv oe e =
|
adamc@1202
|
451 case parse query e of
|
adamc@1201
|
452 NONE => Unknown
|
adamc@1201
|
453 | SOME r =>
|
adamc@1202
|
454 let
|
adamc@1202
|
455 val p =
|
adamc@1202
|
456 foldl (fn ((t, v), p) =>
|
adamc@1202
|
457 And (p,
|
adamc@1202
|
458 Reln (Sql t,
|
adamc@1202
|
459 [Recd (foldl (fn ((v', f), fs) =>
|
adamc@1202
|
460 if v' = v then
|
adamc@1202
|
461 (f, Proj (Proj (Lvar rv, v), f)) :: fs
|
adamc@1202
|
462 else
|
adamc@1202
|
463 fs) [] (#Select r))])))
|
adamc@1202
|
464 True (#From r)
|
adamc@1202
|
465 in
|
adamc@1202
|
466 case oe of
|
adamc@1202
|
467 NONE => p
|
adamc@1202
|
468 | SOME oe =>
|
adamc@1202
|
469 And (p,
|
adamc@1202
|
470 foldl (fn ((v, f), p) =>
|
adamc@1202
|
471 Or (p,
|
adamc@1202
|
472 Reln (Eq, [oe, Proj (Proj (Lvar rv, v), f)])))
|
adamc@1202
|
473 False (#Select r))
|
adamc@1202
|
474 end
|
adamc@1200
|
475
|
adamc@1202
|
476 fun evalExp env (e as (_, loc), st as (nv, p, sent)) =
|
adamc@1200
|
477 let
|
adamc@1200
|
478 fun default () =
|
adamc@1200
|
479 (Var nv, (nv+1, p, sent))
|
adamc@1200
|
480
|
adamc@1200
|
481 fun addSent (p, e, sent) =
|
adamc@1200
|
482 if isKnown e then
|
adamc@1200
|
483 sent
|
adamc@1200
|
484 else
|
adamc@1202
|
485 (loc, e, p) :: sent
|
adamc@1200
|
486 in
|
adamc@1200
|
487 case #1 e of
|
adamc@1200
|
488 EPrim p => (Const p, st)
|
adamc@1200
|
489 | ERel n => (List.nth (env, n), st)
|
adamc@1200
|
490 | ENamed _ => default ()
|
adamc@1200
|
491 | ECon (_, pc, NONE) => (Func (patCon pc, []), st)
|
adamc@1200
|
492 | ECon (_, pc, SOME e) =>
|
adamc@1200
|
493 let
|
adamc@1200
|
494 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
495 in
|
adamc@1200
|
496 (Func (patCon pc, [e]), st)
|
adamc@1200
|
497 end
|
adamc@1200
|
498 | ENone _ => (Func ("None", []), st)
|
adamc@1200
|
499 | ESome (_, e) =>
|
adamc@1200
|
500 let
|
adamc@1200
|
501 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
502 in
|
adamc@1200
|
503 (Func ("Some", [e]), st)
|
adamc@1200
|
504 end
|
adamc@1200
|
505 | EFfi _ => default ()
|
adamc@1200
|
506 | EFfiApp (m, s, es) =>
|
adamc@1200
|
507 if m = "Basis" andalso SS.member (writers, s) then
|
adamc@1200
|
508 let
|
adamc@1200
|
509 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
510 in
|
adamc@1200
|
511 (Func ("unit", []), (#1 st, p, foldl (fn (e, sent) => addSent (#2 st, e, sent)) sent es))
|
adamc@1200
|
512 end
|
adamc@1200
|
513 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
|
adamc@1200
|
514 default ()
|
adamc@1200
|
515 else
|
adamc@1200
|
516 let
|
adamc@1200
|
517 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
518 in
|
adamc@1200
|
519 (Func (m ^ "." ^ s, es), st)
|
adamc@1200
|
520 end
|
adamc@1200
|
521 | EApp _ => default ()
|
adamc@1200
|
522 | EAbs _ => default ()
|
adamc@1200
|
523 | EUnop (s, e1) =>
|
adamc@1200
|
524 let
|
adamc@1200
|
525 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
526 in
|
adamc@1200
|
527 (Func (s, [e1]), st)
|
adamc@1200
|
528 end
|
adamc@1200
|
529 | EBinop (s, e1, e2) =>
|
adamc@1200
|
530 let
|
adamc@1200
|
531 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
532 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
533 in
|
adamc@1200
|
534 (Func (s, [e1, e2]), st)
|
adamc@1200
|
535 end
|
adamc@1200
|
536 | ERecord xets =>
|
adamc@1200
|
537 let
|
adamc@1200
|
538 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
|
adamc@1200
|
539 let
|
adamc@1200
|
540 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
541 in
|
adamc@1200
|
542 ((x, e), st)
|
adamc@1200
|
543 end) st xets
|
adamc@1200
|
544 in
|
adamc@1200
|
545 (Recd xes, st)
|
adamc@1200
|
546 end
|
adamc@1200
|
547 | EField (e, s) =>
|
adamc@1200
|
548 let
|
adamc@1200
|
549 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
550 in
|
adamc@1200
|
551 (Proj (e, s), st)
|
adamc@1200
|
552 end
|
adamc@1200
|
553 | ECase _ => default ()
|
adamc@1200
|
554 | EStrcat (e1, e2) =>
|
adamc@1200
|
555 let
|
adamc@1200
|
556 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
557 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
558 in
|
adamc@1200
|
559 (Func ("cat", [e1, e2]), st)
|
adamc@1200
|
560 end
|
adamc@1200
|
561 | EError _ => (Finish, st)
|
adamc@1200
|
562 | EReturnBlob {blob = b, mimeType = m, ...} =>
|
adamc@1200
|
563 let
|
adamc@1200
|
564 val (b, st) = evalExp env (b, st)
|
adamc@1200
|
565 val (m, st) = evalExp env (m, st)
|
adamc@1200
|
566 in
|
adamc@1200
|
567 (Finish, (#1 st, p, addSent (#2 st, b, addSent (#2 st, m, sent))))
|
adamc@1200
|
568 end
|
adamc@1200
|
569 | ERedirect (e, _) =>
|
adamc@1200
|
570 let
|
adamc@1200
|
571 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
572 in
|
adamc@1200
|
573 (Finish, (#1 st, p, addSent (#2 st, e, sent)))
|
adamc@1200
|
574 end
|
adamc@1200
|
575 | EWrite e =>
|
adamc@1200
|
576 let
|
adamc@1200
|
577 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
578 in
|
adamc@1200
|
579 (Func ("unit", []), (#1 st, p, addSent (#2 st, e, sent)))
|
adamc@1200
|
580 end
|
adamc@1200
|
581 | ESeq (e1, e2) =>
|
adamc@1200
|
582 let
|
adamc@1200
|
583 val (_, st) = evalExp env (e1, st)
|
adamc@1200
|
584 in
|
adamc@1200
|
585 evalExp env (e2, st)
|
adamc@1200
|
586 end
|
adamc@1200
|
587 | ELet (_, _, e1, e2) =>
|
adamc@1200
|
588 let
|
adamc@1200
|
589 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
590 in
|
adamc@1200
|
591 evalExp (e1 :: env) (e2, st)
|
adamc@1200
|
592 end
|
adamc@1200
|
593 | EClosure (n, es) =>
|
adamc@1200
|
594 let
|
adamc@1200
|
595 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
596 in
|
adamc@1200
|
597 (Func ("Cl" ^ Int.toString n, es), st)
|
adamc@1200
|
598 end
|
adamc@1200
|
599
|
adamc@1200
|
600 | EQuery {query = q, body = b, initial = i, ...} =>
|
adamc@1200
|
601 let
|
adamc@1200
|
602 val (_, st) = evalExp env (q, st)
|
adamc@1200
|
603 val (i, st) = evalExp env (i, st)
|
adamc@1200
|
604
|
adamc@1200
|
605 val r = #1 st
|
adamc@1200
|
606 val acc = #1 st + 1
|
adamc@1200
|
607 val st' = (#1 st + 2, #2 st, #3 st)
|
adamc@1200
|
608
|
adamc@1200
|
609 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
|
adamc@1200
|
610
|
adamc@1200
|
611 val r' = newLvar ()
|
adamc@1200
|
612 val acc' = newLvar ()
|
adamc@1202
|
613 val qp = queryProp r' NONE q
|
adamc@1200
|
614
|
adamc@1200
|
615 val doSubExp = subExp (r, r') o subExp (acc, acc')
|
adamc@1200
|
616 val doSubProp = subProp (r, r') o subProp (acc, acc')
|
adamc@1200
|
617
|
adamc@1200
|
618 val p = doSubProp (#2 st')
|
adamc@1200
|
619 val p = And (p, qp)
|
adamc@1200
|
620 val p = Select (r, r', acc', p, doSubExp b)
|
adamc@1200
|
621 in
|
adamc@1202
|
622 (Var r, (#1 st + 1, And (#2 st, p), map (fn (loc, e, p) => (loc,
|
adamc@1202
|
623 doSubExp e,
|
adamc@1202
|
624 And (qp, doSubProp p))) (#3 st')))
|
adamc@1200
|
625 end
|
adamc@1200
|
626 | EDml _ => default ()
|
adamc@1200
|
627 | ENextval _ => default ()
|
adamc@1200
|
628 | ESetval _ => default ()
|
adamc@1200
|
629
|
adamc@1200
|
630 | EUnurlify _ => default ()
|
adamc@1200
|
631 | EJavaScript _ => default ()
|
adamc@1200
|
632 | ESignalReturn _ => default ()
|
adamc@1200
|
633 | ESignalBind _ => default ()
|
adamc@1200
|
634 | ESignalSource _ => default ()
|
adamc@1200
|
635 | EServerCall _ => default ()
|
adamc@1200
|
636 | ERecv _ => default ()
|
adamc@1200
|
637 | ESleep _ => default ()
|
adamc@1200
|
638 | ESpawn _ => default ()
|
adamc@1200
|
639 end
|
adamc@1200
|
640
|
adamc@1200
|
641 fun check file =
|
adamc@1200
|
642 let
|
adamc@1202
|
643 fun decl ((d, _), (vals, pols)) =
|
adamc@1200
|
644 case d of
|
adamc@1200
|
645 DVal (x, _, _, e, _) =>
|
adamc@1200
|
646 let
|
adamc@1200
|
647 fun deAbs (e, env, nv) =
|
adamc@1200
|
648 case #1 e of
|
adamc@1200
|
649 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1)
|
adamc@1200
|
650 | _ => (e, env, nv)
|
adamc@1200
|
651
|
adamc@1202
|
652 val (e, env, nv) = deAbs (e, [], 1)
|
adamc@1200
|
653
|
adamc@1200
|
654 val (e, (_, p, sent)) = evalExp env (e, (nv, True, []))
|
adamc@1200
|
655 in
|
adamc@1202
|
656 ((x, e, p, sent) :: vals, pols)
|
adamc@1200
|
657 end
|
adamc@1202
|
658
|
adamc@1202
|
659 | DPolicy (PolQuery e) => (vals, queryProp 0 (SOME (Var 0)) e :: pols)
|
adamc@1202
|
660
|
adamc@1202
|
661 | _ => (vals, pols)
|
adamc@1202
|
662
|
adamc@1202
|
663 val () = unif := IM.empty
|
adamc@1202
|
664
|
adamc@1202
|
665 val (vals, pols) = foldl decl ([], []) file
|
adamc@1200
|
666 in
|
adamc@1202
|
667 app (fn (name, _, _, sent) =>
|
adamc@1202
|
668 app (fn (loc, e, p) =>
|
adamc@1202
|
669 let
|
adamc@1202
|
670 val p = And (p, Reln (Eq, [Var 0, e]))
|
adamc@1202
|
671 in
|
adamc@1202
|
672 if List.exists (fn pol => imply (p, pol)) pols then
|
adamc@1202
|
673 ()
|
adamc@1202
|
674 else
|
adamc@1202
|
675 ErrorMsg.errorAt loc "The information flow policy may be violated here."
|
adamc@1202
|
676 end) sent) vals
|
adamc@1200
|
677 end
|
adamc@1200
|
678
|
adamc@1200
|
679 end
|