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@1207
|
32 structure IS = IntBinarySet
|
adamc@1202
|
33 structure IM = IntBinaryMap
|
adamc@1202
|
34
|
adamc@1215
|
35 structure SK = struct
|
adamc@1215
|
36 type ord_key = string
|
adamc@1215
|
37 val compare = String.compare
|
adamc@1215
|
38 end
|
adamc@1215
|
39
|
adamc@1215
|
40 structure SS = BinarySetFn(SK)
|
adamc@1215
|
41 structure SM = BinaryMapFn(SK)
|
adamc@1200
|
42
|
adamc@1200
|
43 val writers = ["htmlifyInt_w",
|
adamc@1200
|
44 "htmlifyFloat_w",
|
adamc@1200
|
45 "htmlifyString_w",
|
adamc@1200
|
46 "htmlifyBool_w",
|
adamc@1200
|
47 "htmlifyTime_w",
|
adamc@1200
|
48 "attrifyInt_w",
|
adamc@1200
|
49 "attrifyFloat_w",
|
adamc@1200
|
50 "attrifyString_w",
|
adamc@1200
|
51 "attrifyChar_w",
|
adamc@1200
|
52 "urlifyInt_w",
|
adamc@1200
|
53 "urlifyFloat_w",
|
adamc@1200
|
54 "urlifyString_w",
|
adamc@1213
|
55 "urlifyBool_w",
|
adamc@1213
|
56 "set_cookie"]
|
adamc@1200
|
57
|
adamc@1200
|
58 val writers = SS.addList (SS.empty, writers)
|
adamc@1200
|
59
|
adamc@1200
|
60 type lvar = int
|
adamc@1200
|
61
|
adamc@1215
|
62 datatype func =
|
adamc@1215
|
63 DtCon0 of string
|
adamc@1215
|
64 | DtCon1 of string
|
adamc@1215
|
65 | UnCon of string
|
adamc@1215
|
66 | Other of string
|
adamc@1215
|
67
|
adamc@1200
|
68 datatype exp =
|
adamc@1200
|
69 Const of Prim.t
|
adamc@1200
|
70 | Var of int
|
adamc@1200
|
71 | Lvar of lvar
|
adamc@1215
|
72 | Func of func * exp list
|
adamc@1200
|
73 | Recd of (string * exp) list
|
adamc@1200
|
74 | Proj of exp * string
|
adamc@1200
|
75 | Finish
|
adamc@1200
|
76
|
adamc@1200
|
77 datatype reln =
|
adamc@1207
|
78 Known
|
adamc@1207
|
79 | Sql of string
|
adamc@1215
|
80 | PCon0 of string
|
adamc@1215
|
81 | PCon1 of string
|
adamc@1200
|
82 | Eq
|
adamc@1210
|
83 | Ne
|
adamc@1210
|
84 | Lt
|
adamc@1210
|
85 | Le
|
adamc@1210
|
86 | Gt
|
adamc@1210
|
87 | Ge
|
adamc@1200
|
88
|
adamc@1200
|
89 datatype prop =
|
adamc@1200
|
90 True
|
adamc@1200
|
91 | False
|
adamc@1200
|
92 | Unknown
|
adamc@1200
|
93 | And of prop * prop
|
adamc@1200
|
94 | Or of prop * prop
|
adamc@1200
|
95 | Reln of reln * exp list
|
adamc@1212
|
96 | Cond of exp * prop
|
adamc@1200
|
97
|
adamc@1215
|
98 val unif = ref (IM.empty : exp IM.map)
|
adamc@1215
|
99
|
adamc@1215
|
100 fun reset () = unif := IM.empty
|
adamc@1215
|
101 fun save () = !unif
|
adamc@1215
|
102 fun restore x = unif := x
|
adamc@1215
|
103
|
adamc@1200
|
104 local
|
adamc@1207
|
105 open Print
|
adamc@1207
|
106 val string = PD.string
|
adamc@1207
|
107 in
|
adamc@1207
|
108
|
adamc@1215
|
109 fun p_func f =
|
adamc@1215
|
110 string (case f of
|
adamc@1215
|
111 DtCon0 s => s
|
adamc@1215
|
112 | DtCon1 s => s
|
adamc@1215
|
113 | UnCon s => "un" ^ s
|
adamc@1215
|
114 | Other s => s)
|
adamc@1215
|
115
|
adamc@1207
|
116 fun p_exp e =
|
adamc@1207
|
117 case e of
|
adamc@1207
|
118 Const p => Prim.p_t p
|
adamc@1207
|
119 | Var n => string ("x" ^ Int.toString n)
|
adamc@1215
|
120 | Lvar n =>
|
adamc@1215
|
121 (case IM.find (!unif, n) of
|
adamc@1215
|
122 NONE => string ("X" ^ Int.toString n)
|
adamc@1215
|
123 | SOME e => p_exp e)
|
adamc@1215
|
124 | Func (f, es) => box [p_func f,
|
adamc@1215
|
125 string "(",
|
adamc@1207
|
126 p_list p_exp es,
|
adamc@1207
|
127 string ")"]
|
adamc@1207
|
128 | Recd xes => box [string "{",
|
adamc@1210
|
129 p_list (fn (x, e) => box [string x,
|
adamc@1207
|
130 space,
|
adamc@1207
|
131 string "=",
|
adamc@1207
|
132 space,
|
adamc@1207
|
133 p_exp e]) xes,
|
adamc@1207
|
134 string "}"]
|
adamc@1207
|
135 | Proj (e, x) => box [p_exp e,
|
adamc@1207
|
136 string ("." ^ x)]
|
adamc@1207
|
137 | Finish => string "FINISH"
|
adamc@1207
|
138
|
adamc@1210
|
139 fun p_bop s es =
|
adamc@1210
|
140 case es of
|
adamc@1210
|
141 [e1, e2] => box [p_exp e1,
|
adamc@1210
|
142 space,
|
adamc@1210
|
143 string s,
|
adamc@1210
|
144 space,
|
adamc@1210
|
145 p_exp e2]
|
adamc@1210
|
146 | _ => raise Fail "Iflow.p_bop"
|
adamc@1210
|
147
|
adamc@1207
|
148 fun p_reln r es =
|
adamc@1207
|
149 case r of
|
adamc@1207
|
150 Known =>
|
adamc@1207
|
151 (case es of
|
adamc@1207
|
152 [e] => box [string "known(",
|
adamc@1207
|
153 p_exp e,
|
adamc@1207
|
154 string ")"]
|
adamc@1207
|
155 | _ => raise Fail "Iflow.p_reln: Known")
|
adamc@1207
|
156 | Sql s => box [string (s ^ "("),
|
adamc@1207
|
157 p_list p_exp es,
|
adamc@1207
|
158 string ")"]
|
adamc@1215
|
159 | PCon0 s => box [string (s ^ "("),
|
adamc@1215
|
160 p_list p_exp es,
|
adamc@1215
|
161 string ")"]
|
adamc@1215
|
162 | PCon1 s => box [string (s ^ "("),
|
adamc@1211
|
163 p_list p_exp es,
|
adamc@1211
|
164 string ")"]
|
adamc@1210
|
165 | Eq => p_bop "=" es
|
adamc@1210
|
166 | Ne => p_bop "<>" es
|
adamc@1210
|
167 | Lt => p_bop "<" es
|
adamc@1210
|
168 | Le => p_bop "<=" es
|
adamc@1210
|
169 | Gt => p_bop ">" es
|
adamc@1210
|
170 | Ge => p_bop ">=" es
|
adamc@1207
|
171
|
adamc@1207
|
172 fun p_prop p =
|
adamc@1207
|
173 case p of
|
adamc@1207
|
174 True => string "True"
|
adamc@1207
|
175 | False => string "False"
|
adamc@1207
|
176 | Unknown => string "??"
|
adamc@1207
|
177 | And (p1, p2) => box [string "(",
|
adamc@1207
|
178 p_prop p1,
|
adamc@1207
|
179 string ")",
|
adamc@1207
|
180 space,
|
adamc@1207
|
181 string "&&",
|
adamc@1207
|
182 space,
|
adamc@1207
|
183 string "(",
|
adamc@1207
|
184 p_prop p2,
|
adamc@1207
|
185 string ")"]
|
adamc@1207
|
186 | Or (p1, p2) => box [string "(",
|
adamc@1207
|
187 p_prop p1,
|
adamc@1207
|
188 string ")",
|
adamc@1207
|
189 space,
|
adamc@1207
|
190 string "||",
|
adamc@1207
|
191 space,
|
adamc@1207
|
192 string "(",
|
adamc@1207
|
193 p_prop p2,
|
adamc@1207
|
194 string ")"]
|
adamc@1207
|
195 | Reln (r, es) => p_reln r es
|
adamc@1212
|
196 | Cond (e, p) => box [string "(",
|
adamc@1212
|
197 p_exp e,
|
adamc@1212
|
198 space,
|
adamc@1212
|
199 string "==",
|
adamc@1212
|
200 space,
|
adamc@1212
|
201 p_prop p,
|
adamc@1212
|
202 string ")"]
|
adamc@1207
|
203
|
adamc@1207
|
204 end
|
adamc@1207
|
205
|
adamc@1207
|
206 local
|
adamc@1202
|
207 val count = ref 1
|
adamc@1200
|
208 in
|
adamc@1200
|
209 fun newLvar () =
|
adamc@1200
|
210 let
|
adamc@1200
|
211 val n = !count
|
adamc@1200
|
212 in
|
adamc@1200
|
213 count := n + 1;
|
adamc@1200
|
214 n
|
adamc@1200
|
215 end
|
adamc@1200
|
216 end
|
adamc@1200
|
217
|
adamc@1200
|
218 fun isKnown e =
|
adamc@1200
|
219 case e of
|
adamc@1200
|
220 Const _ => true
|
adamc@1200
|
221 | Func (_, es) => List.all isKnown es
|
adamc@1200
|
222 | Recd xes => List.all (isKnown o #2) xes
|
adamc@1200
|
223 | Proj (e, _) => isKnown e
|
adamc@1200
|
224 | _ => false
|
adamc@1200
|
225
|
adamc@1200
|
226 fun isFinish e =
|
adamc@1200
|
227 case e of
|
adamc@1200
|
228 Finish => true
|
adamc@1200
|
229 | _ => false
|
adamc@1200
|
230
|
adamc@1200
|
231 fun simplify e =
|
adamc@1200
|
232 case e of
|
adamc@1200
|
233 Const _ => e
|
adamc@1200
|
234 | Var _ => e
|
adamc@1208
|
235 | Lvar n =>
|
adamc@1208
|
236 (case IM.find (!unif, n) of
|
adamc@1208
|
237 NONE => e
|
adamc@1208
|
238 | SOME e => simplify e)
|
adamc@1215
|
239 | Func (f, es) => Func (f, map simplify es)
|
adamc@1215
|
240 | Recd xes => Recd (map (fn (x, e) => (x, simplify e)) xes)
|
adamc@1215
|
241 | Proj (e, s) => Proj (simplify e, s)
|
adamc@1200
|
242 | Finish => Finish
|
adamc@1200
|
243
|
adamc@1212
|
244 datatype atom =
|
adamc@1212
|
245 AReln of reln * exp list
|
adamc@1212
|
246 | ACond of exp * prop
|
adamc@1212
|
247
|
adamc@1212
|
248 fun p_atom a =
|
adamc@1212
|
249 p_prop (case a of
|
adamc@1212
|
250 AReln x => Reln x
|
adamc@1212
|
251 | ACond x => Cond x)
|
adamc@1212
|
252
|
adamc@1202
|
253 fun lvarIn lv =
|
adamc@1202
|
254 let
|
adamc@1202
|
255 fun lvi e =
|
adamc@1202
|
256 case e of
|
adamc@1202
|
257 Const _ => false
|
adamc@1202
|
258 | Var _ => false
|
adamc@1202
|
259 | Lvar lv' => lv' = lv
|
adamc@1202
|
260 | Func (_, es) => List.exists lvi es
|
adamc@1202
|
261 | Recd xes => List.exists (lvi o #2) xes
|
adamc@1202
|
262 | Proj (e, _) => lvi e
|
adamc@1202
|
263 | Finish => false
|
adamc@1202
|
264 in
|
adamc@1202
|
265 lvi
|
adamc@1202
|
266 end
|
adamc@1202
|
267
|
adamc@1212
|
268 fun lvarInP lv =
|
adamc@1212
|
269 let
|
adamc@1212
|
270 fun lvi p =
|
adamc@1212
|
271 case p of
|
adamc@1212
|
272 True => false
|
adamc@1212
|
273 | False => false
|
adamc@1212
|
274 | Unknown => true
|
adamc@1212
|
275 | And (p1, p2) => lvi p1 orelse lvi p2
|
adamc@1212
|
276 | Or (p1, p2) => lvi p1 orelse lvi p2
|
adamc@1212
|
277 | Reln (_, es) => List.exists (lvarIn lv) es
|
adamc@1212
|
278 | Cond (e, p) => lvarIn lv e orelse lvi p
|
adamc@1212
|
279 in
|
adamc@1212
|
280 lvi
|
adamc@1212
|
281 end
|
adamc@1212
|
282
|
adamc@1212
|
283 fun varIn lv =
|
adamc@1212
|
284 let
|
adamc@1212
|
285 fun lvi e =
|
adamc@1212
|
286 case e of
|
adamc@1212
|
287 Const _ => false
|
adamc@1212
|
288 | Lvar _ => false
|
adamc@1212
|
289 | Var lv' => lv' = lv
|
adamc@1212
|
290 | Func (_, es) => List.exists lvi es
|
adamc@1212
|
291 | Recd xes => List.exists (lvi o #2) xes
|
adamc@1212
|
292 | Proj (e, _) => lvi e
|
adamc@1212
|
293 | Finish => false
|
adamc@1212
|
294 in
|
adamc@1212
|
295 lvi
|
adamc@1212
|
296 end
|
adamc@1212
|
297
|
adamc@1212
|
298 fun varInP lv =
|
adamc@1212
|
299 let
|
adamc@1212
|
300 fun lvi p =
|
adamc@1212
|
301 case p of
|
adamc@1212
|
302 True => false
|
adamc@1212
|
303 | False => false
|
adamc@1212
|
304 | Unknown => false
|
adamc@1212
|
305 | And (p1, p2) => lvi p1 orelse lvi p2
|
adamc@1212
|
306 | Or (p1, p2) => lvi p1 orelse lvi p2
|
adamc@1212
|
307 | Reln (_, es) => List.exists (varIn lv) es
|
adamc@1212
|
308 | Cond (e, p) => varIn lv e orelse lvi p
|
adamc@1212
|
309 in
|
adamc@1212
|
310 lvi
|
adamc@1212
|
311 end
|
adamc@1212
|
312
|
adamc@1228
|
313 fun bumpLvars by =
|
adamc@1228
|
314 let
|
adamc@1228
|
315 fun lvi e =
|
adamc@1228
|
316 case e of
|
adamc@1228
|
317 Const _ => e
|
adamc@1228
|
318 | Var _ => e
|
adamc@1228
|
319 | Lvar lv => Lvar (lv + by)
|
adamc@1228
|
320 | Func (f, es) => Func (f, map lvi es)
|
adamc@1228
|
321 | Recd xes => Recd (map (fn (x, e) => (x, lvi e)) xes)
|
adamc@1228
|
322 | Proj (e, f) => Proj (lvi e, f)
|
adamc@1228
|
323 | Finish => e
|
adamc@1228
|
324 in
|
adamc@1228
|
325 lvi
|
adamc@1228
|
326 end
|
adamc@1228
|
327
|
adamc@1228
|
328 fun bumpLvarsP by =
|
adamc@1228
|
329 let
|
adamc@1228
|
330 fun lvi p =
|
adamc@1228
|
331 case p of
|
adamc@1228
|
332 True => p
|
adamc@1228
|
333 | False => p
|
adamc@1228
|
334 | Unknown => p
|
adamc@1228
|
335 | And (p1, p2) => And (lvi p1, lvi p2)
|
adamc@1228
|
336 | Or (p1, p2) => And (lvi p1, lvi p2)
|
adamc@1228
|
337 | Reln (r, es) => Reln (r, map (bumpLvars by) es)
|
adamc@1228
|
338 | Cond (e, p) => Cond (bumpLvars by e, lvi p)
|
adamc@1228
|
339 in
|
adamc@1228
|
340 lvi
|
adamc@1228
|
341 end
|
adamc@1228
|
342
|
adamc@1228
|
343 fun maxLvar e =
|
adamc@1228
|
344 let
|
adamc@1228
|
345 fun lvi e =
|
adamc@1228
|
346 case e of
|
adamc@1228
|
347 Const _ => 0
|
adamc@1228
|
348 | Var _ => 0
|
adamc@1228
|
349 | Lvar lv => lv
|
adamc@1228
|
350 | Func (f, es) => foldl Int.max 0 (map lvi es)
|
adamc@1228
|
351 | Recd xes => foldl Int.max 0 (map (lvi o #2) xes)
|
adamc@1228
|
352 | Proj (e, f) => lvi e
|
adamc@1228
|
353 | Finish => 0
|
adamc@1228
|
354 in
|
adamc@1228
|
355 lvi e
|
adamc@1228
|
356 end
|
adamc@1228
|
357
|
adamc@1228
|
358 fun maxLvarP p =
|
adamc@1228
|
359 let
|
adamc@1228
|
360 fun lvi p =
|
adamc@1228
|
361 case p of
|
adamc@1228
|
362 True => 0
|
adamc@1228
|
363 | False => 0
|
adamc@1228
|
364 | Unknown => 0
|
adamc@1228
|
365 | And (p1, p2) => Int.max (lvi p1, lvi p2)
|
adamc@1228
|
366 | Or (p1, p2) => Int.max (lvi p1, lvi p2)
|
adamc@1228
|
367 | Reln (r, es) => foldl Int.max 0 (map maxLvar es)
|
adamc@1228
|
368 | Cond (e, p) => Int.max (maxLvar e, lvi p)
|
adamc@1228
|
369 in
|
adamc@1228
|
370 lvi p
|
adamc@1228
|
371 end
|
adamc@1228
|
372
|
adamc@1202
|
373 fun eq' (e1, e2) =
|
adamc@1202
|
374 case (e1, e2) of
|
adamc@1202
|
375 (Const p1, Const p2) => Prim.equal (p1, p2)
|
adamc@1202
|
376 | (Var n1, Var n2) => n1 = n2
|
adamc@1202
|
377
|
adamc@1202
|
378 | (Lvar n1, _) =>
|
adamc@1202
|
379 (case IM.find (!unif, n1) of
|
adamc@1202
|
380 SOME e1 => eq' (e1, e2)
|
adamc@1202
|
381 | NONE =>
|
adamc@1202
|
382 case e2 of
|
adamc@1202
|
383 Lvar n2 =>
|
adamc@1202
|
384 (case IM.find (!unif, n2) of
|
adamc@1202
|
385 SOME e2 => eq' (e1, e2)
|
adamc@1202
|
386 | NONE => n1 = n2
|
adamc@1208
|
387 orelse (unif := IM.insert (!unif, n2, e1);
|
adamc@1202
|
388 true))
|
adamc@1202
|
389 | _ =>
|
adamc@1202
|
390 if lvarIn n1 e2 then
|
adamc@1202
|
391 false
|
adamc@1202
|
392 else
|
adamc@1202
|
393 (unif := IM.insert (!unif, n1, e2);
|
adamc@1202
|
394 true))
|
adamc@1202
|
395
|
adamc@1202
|
396 | (_, Lvar n2) =>
|
adamc@1202
|
397 (case IM.find (!unif, n2) of
|
adamc@1202
|
398 SOME e2 => eq' (e1, e2)
|
adamc@1202
|
399 | NONE =>
|
adamc@1202
|
400 if lvarIn n2 e1 then
|
adamc@1202
|
401 false
|
adamc@1202
|
402 else
|
adamc@1213
|
403 ((*Print.prefaces "unif" [("n2", Print.PD.string (Int.toString n2)),
|
adamc@1213
|
404 ("e1", p_exp e1)];*)
|
adamc@1213
|
405 unif := IM.insert (!unif, n2, e1);
|
adamc@1202
|
406 true))
|
adamc@1202
|
407
|
adamc@1202
|
408 | (Func (f1, es1), Func (f2, es2)) => f1 = f2 andalso ListPair.allEq eq' (es1, es2)
|
adamc@1202
|
409 | (Recd xes1, Recd xes2) => ListPair.allEq (fn ((x1, e1), (x2, e2)) => x1 = x2 andalso eq' (e1, e2)) (xes1, xes2)
|
adamc@1202
|
410 | (Proj (e1, s1), Proj (e2, s2)) => eq' (e1, e2) andalso s1 = s2
|
adamc@1202
|
411 | (Finish, Finish) => true
|
adamc@1202
|
412 | _ => false
|
adamc@1202
|
413
|
adamc@1202
|
414 fun eq (e1, e2) =
|
adamc@1202
|
415 let
|
adamc@1203
|
416 val saved = save ()
|
adamc@1202
|
417 in
|
adamc@1202
|
418 if eq' (simplify e1, simplify e2) then
|
adamc@1202
|
419 true
|
adamc@1202
|
420 else
|
adamc@1203
|
421 (restore saved;
|
adamc@1202
|
422 false)
|
adamc@1202
|
423 end
|
adamc@1202
|
424
|
adamc@1208
|
425 val debug = ref false
|
adamc@1208
|
426
|
adamc@1211
|
427 fun eeq (e1, e2) =
|
adamc@1211
|
428 case (e1, e2) of
|
adamc@1211
|
429 (Const p1, Const p2) => Prim.equal (p1, p2)
|
adamc@1211
|
430 | (Var n1, Var n2) => n1 = n2
|
adamc@1211
|
431 | (Lvar n1, Lvar n2) => n1 = n2
|
adamc@1211
|
432 | (Func (f1, es1), Func (f2, es2)) => f1 = f2 andalso ListPair.allEq eeq (es1, es2)
|
adamc@1211
|
433 | (Recd xes1, Recd xes2) => length xes1 = length xes2 andalso
|
adamc@1211
|
434 List.all (fn (x2, e2) =>
|
adamc@1211
|
435 List.exists (fn (x1, e1) => x1 = x2 andalso eeq (e1, e2)) xes2) xes1
|
adamc@1211
|
436 | (Proj (e1, x1), Proj (e2, x2)) => eeq (e1, e2) andalso x1 = x2
|
adamc@1211
|
437 | (Finish, Finish) => true
|
adamc@1211
|
438 | _ => false
|
adamc@1211
|
439
|
adamc@1208
|
440 (* Congruence closure *)
|
adamc@1208
|
441 structure Cc :> sig
|
adamc@1215
|
442 type database
|
adamc@1215
|
443
|
adamc@1215
|
444 exception Contradiction
|
adamc@1215
|
445 exception Undetermined
|
adamc@1215
|
446
|
adamc@1215
|
447 val database : unit -> database
|
adamc@1215
|
448
|
adamc@1215
|
449 val assert : database * atom -> unit
|
adamc@1215
|
450 val check : database * atom -> bool
|
adamc@1215
|
451
|
adamc@1215
|
452 val p_database : database Print.printer
|
adamc@1218
|
453
|
adamc@1218
|
454 val builtFrom : database * {Base : exp list, Derived : exp} -> bool
|
adamc@1226
|
455
|
adamc@1226
|
456 val p_repOf : database -> exp Print.printer
|
adamc@1208
|
457 end = struct
|
adamc@1208
|
458
|
adamc@1215
|
459 exception Contradiction
|
adamc@1215
|
460 exception Undetermined
|
adamc@1208
|
461
|
adamc@1215
|
462 structure CM = BinaryMapFn(struct
|
adamc@1215
|
463 type ord_key = Prim.t
|
adamc@1215
|
464 val compare = Prim.compare
|
adamc@1215
|
465 end)
|
adamc@1208
|
466
|
adamc@1215
|
467 datatype node = Node of {Rep : node ref option ref,
|
adamc@1215
|
468 Cons : node ref SM.map ref,
|
adamc@1215
|
469 Variety : variety,
|
adamc@1215
|
470 Known : bool ref}
|
adamc@1208
|
471
|
adamc@1215
|
472 and variety =
|
adamc@1215
|
473 Dt0 of string
|
adamc@1215
|
474 | Dt1 of string * node ref
|
adamc@1215
|
475 | Prim of Prim.t
|
adamc@1221
|
476 | Recrd of node ref SM.map ref * bool
|
adamc@1215
|
477 | Nothing
|
adamc@1208
|
478
|
adamc@1215
|
479 type representative = node ref
|
adamc@1215
|
480
|
adamc@1215
|
481 type database = {Vars : representative IM.map ref,
|
adamc@1215
|
482 Consts : representative CM.map ref,
|
adamc@1215
|
483 Con0s : representative SM.map ref,
|
adamc@1215
|
484 Records : (representative SM.map * representative) list ref,
|
adamc@1215
|
485 Funcs : ((string * representative list) * representative) list ref }
|
adamc@1215
|
486
|
adamc@1215
|
487 fun database () = {Vars = ref IM.empty,
|
adamc@1215
|
488 Consts = ref CM.empty,
|
adamc@1215
|
489 Con0s = ref SM.empty,
|
adamc@1215
|
490 Records = ref [],
|
adamc@1215
|
491 Funcs = ref []}
|
adamc@1215
|
492
|
adamc@1215
|
493 fun unNode n =
|
adamc@1215
|
494 case !n of
|
adamc@1215
|
495 Node r => r
|
adamc@1215
|
496
|
adamc@1215
|
497 open Print
|
adamc@1215
|
498 val string = PD.string
|
adamc@1215
|
499 val newline = PD.newline
|
adamc@1215
|
500
|
adamc@1215
|
501 fun p_rep n =
|
adamc@1215
|
502 case !(#Rep (unNode n)) of
|
adamc@1215
|
503 SOME n => p_rep n
|
adamc@1215
|
504 | NONE =>
|
adamc@1221
|
505 box [string (Int.toString (Unsafe.cast n) ^ ":"),
|
adamc@1221
|
506 space,
|
adamc@1221
|
507 case #Variety (unNode n) of
|
adamc@1221
|
508 Nothing => string "?"
|
adamc@1221
|
509 | Dt0 s => string ("Dt0(" ^ s ^ ")")
|
adamc@1221
|
510 | Dt1 (s, n) => box[string ("Dt1(" ^ s ^ ","),
|
adamc@1221
|
511 space,
|
adamc@1221
|
512 p_rep n,
|
adamc@1221
|
513 string ")"]
|
adamc@1221
|
514 | Prim p => Prim.p_t p
|
adamc@1221
|
515 | Recrd (ref m, b) => box [string "{",
|
adamc@1221
|
516 p_list (fn (x, n) => box [string x,
|
adamc@1221
|
517 space,
|
adamc@1221
|
518 string "=",
|
adamc@1221
|
519 space,
|
adamc@1221
|
520 p_rep n]) (SM.listItemsi m),
|
adamc@1221
|
521 string "}",
|
adamc@1221
|
522 if b then
|
adamc@1221
|
523 box [space,
|
adamc@1221
|
524 string "(complete)"]
|
adamc@1221
|
525 else
|
adamc@1226
|
526 box []]]
|
adamc@1215
|
527
|
adamc@1215
|
528 fun p_database (db : database) =
|
adamc@1215
|
529 box [string "Vars:",
|
adamc@1215
|
530 newline,
|
adamc@1215
|
531 p_list_sep newline (fn (i, n) => box [string ("x" ^ Int.toString i),
|
adamc@1215
|
532 space,
|
adamc@1215
|
533 string "=",
|
adamc@1215
|
534 space,
|
adamc@1218
|
535 p_rep n,
|
adamc@1218
|
536 if !(#Known (unNode n)) then
|
adamc@1218
|
537 box [space,
|
adamc@1218
|
538 string "(known)"]
|
adamc@1218
|
539 else
|
adamc@1218
|
540 box []]) (IM.listItemsi (!(#Vars db)))]
|
adamc@1215
|
541
|
adamc@1215
|
542 fun repOf (n : representative) : representative =
|
adamc@1215
|
543 case !(#Rep (unNode n)) of
|
adamc@1215
|
544 NONE => n
|
adamc@1215
|
545 | SOME r =>
|
adamc@1215
|
546 let
|
adamc@1215
|
547 val r = repOf r
|
adamc@1215
|
548 in
|
adamc@1215
|
549 #Rep (unNode n) := SOME r;
|
adamc@1215
|
550 r
|
adamc@1215
|
551 end
|
adamc@1215
|
552
|
adamc@1215
|
553 fun markKnown r =
|
adamc@1221
|
554 let
|
adamc@1221
|
555 val r = repOf r
|
adamc@1221
|
556 in
|
adamc@1221
|
557 (*Print.preface ("markKnown", p_rep r);*)
|
adamc@1221
|
558 if !(#Known (unNode r)) then
|
adamc@1221
|
559 ()(*TextIO.print "Already known\n"*)
|
adamc@1221
|
560 else
|
adamc@1221
|
561 (#Known (unNode r) := true;
|
adamc@1221
|
562 SM.app markKnown (!(#Cons (unNode r)));
|
adamc@1221
|
563 case #Variety (unNode r) of
|
adamc@1221
|
564 Dt1 (_, r) => markKnown r
|
adamc@1221
|
565 | Recrd (xes, _) => SM.app markKnown (!xes)
|
adamc@1221
|
566 | _ => ())
|
adamc@1221
|
567 end
|
adamc@1215
|
568
|
adamc@1215
|
569 fun representative (db : database, e) =
|
adamc@1208
|
570 let
|
adamc@1215
|
571 fun rep e =
|
adamc@1215
|
572 case e of
|
adamc@1215
|
573 Const p => (case CM.find (!(#Consts db), p) of
|
adamc@1215
|
574 SOME r => repOf r
|
adamc@1215
|
575 | NONE =>
|
adamc@1215
|
576 let
|
adamc@1215
|
577 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
578 Cons = ref SM.empty,
|
adamc@1215
|
579 Variety = Prim p,
|
adamc@1219
|
580 Known = ref true})
|
adamc@1215
|
581 in
|
adamc@1215
|
582 #Consts db := CM.insert (!(#Consts db), p, r);
|
adamc@1215
|
583 r
|
adamc@1215
|
584 end)
|
adamc@1215
|
585 | Var n => (case IM.find (!(#Vars db), n) of
|
adamc@1215
|
586 SOME r => repOf r
|
adamc@1215
|
587 | NONE =>
|
adamc@1215
|
588 let
|
adamc@1215
|
589 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
590 Cons = ref SM.empty,
|
adamc@1215
|
591 Variety = Nothing,
|
adamc@1215
|
592 Known = ref false})
|
adamc@1215
|
593 in
|
adamc@1215
|
594 #Vars db := IM.insert (!(#Vars db), n, r);
|
adamc@1215
|
595 r
|
adamc@1215
|
596 end)
|
adamc@1215
|
597 | Lvar n =>
|
adamc@1215
|
598 (case IM.find (!unif, n) of
|
adamc@1215
|
599 NONE => raise Undetermined
|
adamc@1215
|
600 | SOME e => rep e)
|
adamc@1215
|
601 | Func (DtCon0 f, []) => (case SM.find (!(#Con0s db), f) of
|
adamc@1215
|
602 SOME r => repOf r
|
adamc@1215
|
603 | NONE =>
|
adamc@1215
|
604 let
|
adamc@1215
|
605 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
606 Cons = ref SM.empty,
|
adamc@1215
|
607 Variety = Dt0 f,
|
adamc@1218
|
608 Known = ref true})
|
adamc@1215
|
609 in
|
adamc@1215
|
610 #Con0s db := SM.insert (!(#Con0s db), f, r);
|
adamc@1215
|
611 r
|
adamc@1215
|
612 end)
|
adamc@1215
|
613 | Func (DtCon0 _, _) => raise Fail "Iflow.rep: DtCon0"
|
adamc@1215
|
614 | Func (DtCon1 f, [e]) =>
|
adamc@1215
|
615 let
|
adamc@1215
|
616 val r = rep e
|
adamc@1215
|
617 in
|
adamc@1215
|
618 case SM.find (!(#Cons (unNode r)), f) of
|
adamc@1215
|
619 SOME r => repOf r
|
adamc@1215
|
620 | NONE =>
|
adamc@1215
|
621 let
|
adamc@1215
|
622 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
623 Cons = ref SM.empty,
|
adamc@1215
|
624 Variety = Dt1 (f, r),
|
adamc@1221
|
625 Known = ref (!(#Known (unNode r)))})
|
adamc@1215
|
626 in
|
adamc@1215
|
627 #Cons (unNode r) := SM.insert (!(#Cons (unNode r)), f, r');
|
adamc@1215
|
628 r'
|
adamc@1215
|
629 end
|
adamc@1215
|
630 end
|
adamc@1215
|
631 | Func (DtCon1 _, _) => raise Fail "Iflow.rep: DtCon1"
|
adamc@1215
|
632 | Func (UnCon f, [e]) =>
|
adamc@1215
|
633 let
|
adamc@1215
|
634 val r = rep e
|
adamc@1215
|
635 in
|
adamc@1215
|
636 case #Variety (unNode r) of
|
adamc@1215
|
637 Dt1 (f', n) => if f' = f then
|
adamc@1215
|
638 repOf n
|
adamc@1215
|
639 else
|
adamc@1215
|
640 raise Contradiction
|
adamc@1215
|
641 | Nothing =>
|
adamc@1215
|
642 let
|
adamc@1215
|
643 val cons = ref SM.empty
|
adamc@1215
|
644 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
645 Cons = cons,
|
adamc@1215
|
646 Variety = Nothing,
|
adamc@1221
|
647 Known = ref (!(#Known (unNode r)))})
|
adamc@1215
|
648
|
adamc@1215
|
649 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
650 Cons = #Cons (unNode r),
|
adamc@1215
|
651 Variety = Dt1 (f, r'),
|
adamc@1215
|
652 Known = #Known (unNode r)})
|
adamc@1215
|
653 in
|
adamc@1215
|
654 cons := SM.insert (!cons, f, r'');
|
adamc@1215
|
655 #Rep (unNode r) := SOME r'';
|
adamc@1215
|
656 r'
|
adamc@1215
|
657 end
|
adamc@1215
|
658 | _ => raise Contradiction
|
adamc@1215
|
659 end
|
adamc@1215
|
660 | Func (UnCon _, _) => raise Fail "Iflow.rep: UnCon"
|
adamc@1215
|
661 | Func (Other f, es) =>
|
adamc@1215
|
662 let
|
adamc@1215
|
663 val rs = map rep es
|
adamc@1215
|
664 in
|
adamc@1215
|
665 case List.find (fn (x : string * representative list, _) => x = (f, rs)) (!(#Funcs db)) of
|
adamc@1215
|
666 NONE =>
|
adamc@1215
|
667 let
|
adamc@1215
|
668 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
669 Cons = ref SM.empty,
|
adamc@1215
|
670 Variety = Nothing,
|
adamc@1215
|
671 Known = ref false})
|
adamc@1215
|
672 in
|
adamc@1215
|
673 #Funcs db := ((f, rs), r) :: (!(#Funcs db));
|
adamc@1215
|
674 r
|
adamc@1215
|
675 end
|
adamc@1215
|
676 | SOME (_, r) => repOf r
|
adamc@1215
|
677 end
|
adamc@1215
|
678 | Recd xes =>
|
adamc@1215
|
679 let
|
adamc@1215
|
680 val xes = map (fn (x, e) => (x, rep e)) xes
|
adamc@1215
|
681 val len = length xes
|
adamc@1215
|
682 in
|
adamc@1215
|
683 case List.find (fn (xes', _) =>
|
adamc@1215
|
684 SM.numItems xes' = len
|
adamc@1215
|
685 andalso List.all (fn (x, n) =>
|
adamc@1215
|
686 case SM.find (xes', x) of
|
adamc@1215
|
687 NONE => false
|
adamc@1215
|
688 | SOME n' => n = repOf n') xes)
|
adamc@1215
|
689 (!(#Records db)) of
|
adamc@1215
|
690 SOME (_, r) => repOf r
|
adamc@1215
|
691 | NONE =>
|
adamc@1215
|
692 let
|
adamc@1215
|
693 val xes = foldl SM.insert' SM.empty xes
|
adamc@1215
|
694
|
adamc@1215
|
695 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
696 Cons = ref SM.empty,
|
adamc@1221
|
697 Variety = Recrd (ref xes, true),
|
adamc@1215
|
698 Known = ref false})
|
adamc@1215
|
699 in
|
adamc@1215
|
700 #Records db := (xes, r') :: (!(#Records db));
|
adamc@1215
|
701 r'
|
adamc@1215
|
702 end
|
adamc@1215
|
703 end
|
adamc@1215
|
704 | Proj (e, f) =>
|
adamc@1215
|
705 let
|
adamc@1215
|
706 val r = rep e
|
adamc@1215
|
707 in
|
adamc@1215
|
708 case #Variety (unNode r) of
|
adamc@1221
|
709 Recrd (xes, _) =>
|
adamc@1215
|
710 (case SM.find (!xes, f) of
|
adamc@1215
|
711 SOME r => repOf r
|
adamc@1216
|
712 | NONE => let
|
adamc@1215
|
713 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
714 Cons = ref SM.empty,
|
adamc@1215
|
715 Variety = Nothing,
|
adamc@1221
|
716 Known = ref (!(#Known (unNode r)))})
|
adamc@1215
|
717 in
|
adamc@1215
|
718 xes := SM.insert (!xes, f, r);
|
adamc@1215
|
719 r
|
adamc@1215
|
720 end)
|
adamc@1215
|
721 | Nothing =>
|
adamc@1215
|
722 let
|
adamc@1215
|
723 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
724 Cons = ref SM.empty,
|
adamc@1215
|
725 Variety = Nothing,
|
adamc@1221
|
726 Known = ref (!(#Known (unNode r)))})
|
adamc@1215
|
727
|
adamc@1215
|
728 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
729 Cons = #Cons (unNode r),
|
adamc@1221
|
730 Variety = Recrd (ref (SM.insert (SM.empty, f, r')), false),
|
adamc@1215
|
731 Known = #Known (unNode r)})
|
adamc@1215
|
732 in
|
adamc@1215
|
733 #Rep (unNode r) := SOME r'';
|
adamc@1215
|
734 r'
|
adamc@1215
|
735 end
|
adamc@1215
|
736 | _ => raise Contradiction
|
adamc@1215
|
737 end
|
adamc@1226
|
738 | Finish => raise Contradiction
|
adamc@1208
|
739 in
|
adamc@1215
|
740 rep e
|
adamc@1208
|
741 end
|
adamc@1208
|
742
|
adamc@1226
|
743 fun p_repOf db e = p_rep (representative (db, e))
|
adamc@1226
|
744
|
adamc@1215
|
745 fun assert (db, a) =
|
adamc@1215
|
746 case a of
|
adamc@1215
|
747 ACond _ => ()
|
adamc@1215
|
748 | AReln x =>
|
adamc@1215
|
749 case x of
|
adamc@1221
|
750 (Known, [e]) =>
|
adamc@1221
|
751 ((*Print.prefaces "Before" [("e", p_exp e),
|
adamc@1221
|
752 ("db", p_database db)];*)
|
adamc@1221
|
753 markKnown (representative (db, e))(*;
|
adamc@1221
|
754 Print.prefaces "After" [("e", p_exp e),
|
adamc@1221
|
755 ("db", p_database db)]*))
|
adamc@1215
|
756 | (PCon0 f, [e]) =>
|
adamc@1215
|
757 let
|
adamc@1215
|
758 val r = representative (db, e)
|
adamc@1215
|
759 in
|
adamc@1215
|
760 case #Variety (unNode r) of
|
adamc@1215
|
761 Dt0 f' => if f = f' then
|
adamc@1215
|
762 ()
|
adamc@1215
|
763 else
|
adamc@1215
|
764 raise Contradiction
|
adamc@1215
|
765 | Nothing =>
|
adamc@1215
|
766 let
|
adamc@1215
|
767 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
768 Cons = ref SM.empty,
|
adamc@1215
|
769 Variety = Dt0 f,
|
adamc@1215
|
770 Known = ref false})
|
adamc@1215
|
771 in
|
adamc@1215
|
772 #Rep (unNode r) := SOME r'
|
adamc@1215
|
773 end
|
adamc@1215
|
774 | _ => raise Contradiction
|
adamc@1215
|
775 end
|
adamc@1215
|
776 | (PCon1 f, [e]) =>
|
adamc@1215
|
777 let
|
adamc@1215
|
778 val r = representative (db, e)
|
adamc@1215
|
779 in
|
adamc@1215
|
780 case #Variety (unNode r) of
|
adamc@1215
|
781 Dt1 (f', e') => if f = f' then
|
adamc@1215
|
782 ()
|
adamc@1215
|
783 else
|
adamc@1215
|
784 raise Contradiction
|
adamc@1215
|
785 | Nothing =>
|
adamc@1215
|
786 let
|
adamc@1215
|
787 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
788 Cons = ref SM.empty,
|
adamc@1215
|
789 Variety = Nothing,
|
adamc@1215
|
790 Known = ref false})
|
adamc@1214
|
791
|
adamc@1215
|
792 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
793 Cons = ref SM.empty,
|
adamc@1215
|
794 Variety = Dt1 (f, r''),
|
adamc@1215
|
795 Known = ref false})
|
adamc@1215
|
796 in
|
adamc@1215
|
797 #Rep (unNode r) := SOME r'
|
adamc@1215
|
798 end
|
adamc@1215
|
799 | _ => raise Contradiction
|
adamc@1215
|
800 end
|
adamc@1215
|
801 | (Eq, [e1, e2]) =>
|
adamc@1215
|
802 let
|
adamc@1215
|
803 fun markEq (r1, r2) =
|
adamc@1226
|
804 let
|
adamc@1226
|
805 val r1 = repOf r1
|
adamc@1226
|
806 val r2 = repOf r2
|
adamc@1226
|
807 in
|
adamc@1226
|
808 if r1 = r2 then
|
adamc@1226
|
809 ()
|
adamc@1226
|
810 else case (#Variety (unNode r1), #Variety (unNode r2)) of
|
adamc@1226
|
811 (Prim p1, Prim p2) => if Prim.equal (p1, p2) then
|
adamc@1226
|
812 ()
|
adamc@1226
|
813 else
|
adamc@1226
|
814 raise Contradiction
|
adamc@1226
|
815 | (Dt0 f1, Dt0 f2) => if f1 = f2 then
|
adamc@1226
|
816 ()
|
adamc@1226
|
817 else
|
adamc@1226
|
818 raise Contradiction
|
adamc@1226
|
819 | (Dt1 (f1, r1), Dt1 (f2, r2)) => if f1 = f2 then
|
adamc@1226
|
820 markEq (r1, r2)
|
adamc@1226
|
821 else
|
adamc@1226
|
822 raise Contradiction
|
adamc@1226
|
823 | (Recrd (xes1, _), Recrd (xes2, _)) =>
|
adamc@1226
|
824 let
|
adamc@1226
|
825 fun unif (xes1, xes2) =
|
adamc@1226
|
826 SM.appi (fn (x, r1) =>
|
adamc@1226
|
827 case SM.find (!xes2, x) of
|
adamc@1226
|
828 NONE => xes2 := SM.insert (!xes2, x, r1)
|
adamc@1226
|
829 | SOME r2 => markEq (r1, r2)) (!xes1)
|
adamc@1226
|
830 in
|
adamc@1226
|
831 unif (xes1, xes2);
|
adamc@1226
|
832 unif (xes2, xes1)
|
adamc@1226
|
833 end
|
adamc@1226
|
834 | (Nothing, _) => mergeNodes (r1, r2)
|
adamc@1226
|
835 | (_, Nothing) => mergeNodes (r2, r1)
|
adamc@1226
|
836 | _ => raise Contradiction
|
adamc@1226
|
837 end
|
adamc@1214
|
838
|
adamc@1218
|
839 and mergeNodes (r1, r2) =
|
adamc@1218
|
840 (#Rep (unNode r1) := SOME r2;
|
adamc@1218
|
841 if !(#Known (unNode r1)) then
|
adamc@1218
|
842 markKnown r2
|
adamc@1218
|
843 else
|
adamc@1218
|
844 ();
|
adamc@1218
|
845 if !(#Known (unNode r2)) then
|
adamc@1218
|
846 markKnown r1
|
adamc@1218
|
847 else
|
adamc@1218
|
848 ();
|
adamc@1218
|
849 #Cons (unNode r2) := SM.unionWith #1 (!(#Cons (unNode r2)), !(#Cons (unNode r1)));
|
adamc@1218
|
850 compactFuncs ())
|
adamc@1218
|
851
|
adamc@1215
|
852 and compactFuncs () =
|
adamc@1214
|
853 let
|
adamc@1215
|
854 fun loop funcs =
|
adamc@1215
|
855 case funcs of
|
adamc@1215
|
856 [] => []
|
adamc@1215
|
857 | (fr as ((f, rs), r)) :: rest =>
|
adamc@1215
|
858 let
|
adamc@1215
|
859 val rest = List.filter (fn ((f' : string, rs'), r') =>
|
adamc@1215
|
860 if f' = f
|
adamc@1215
|
861 andalso ListPair.allEq (fn (r1, r2) =>
|
adamc@1215
|
862 repOf r1 = repOf r2)
|
adamc@1215
|
863 (rs, rs') then
|
adamc@1215
|
864 (markEq (r, r');
|
adamc@1215
|
865 false)
|
adamc@1215
|
866 else
|
adamc@1215
|
867 true) rest
|
adamc@1215
|
868 in
|
adamc@1215
|
869 fr :: loop rest
|
adamc@1215
|
870 end
|
adamc@1214
|
871 in
|
adamc@1215
|
872 #Funcs db := loop (!(#Funcs db))
|
adamc@1214
|
873 end
|
adamc@1215
|
874 in
|
adamc@1215
|
875 markEq (representative (db, e1), representative (db, e2))
|
adamc@1215
|
876 end
|
adamc@1215
|
877 | _ => ()
|
adamc@1214
|
878
|
adamc@1215
|
879 fun check (db, a) =
|
adamc@1215
|
880 case a of
|
adamc@1215
|
881 ACond _ => false
|
adamc@1215
|
882 | AReln x =>
|
adamc@1215
|
883 case x of
|
adamc@1221
|
884 (Known, [e]) =>
|
adamc@1221
|
885 let
|
adamc@1221
|
886 fun isKnown r =
|
adamc@1221
|
887 let
|
adamc@1221
|
888 val r = repOf r
|
adamc@1221
|
889 in
|
adamc@1221
|
890 !(#Known (unNode r))
|
adamc@1221
|
891 orelse case #Variety (unNode r) of
|
adamc@1221
|
892 Dt1 (_, r) => isKnown r
|
adamc@1221
|
893 | Recrd (xes, true) => List.all isKnown (SM.listItems (!xes))
|
adamc@1221
|
894 | _ => false
|
adamc@1221
|
895 end
|
adamc@1221
|
896
|
adamc@1221
|
897 val r = representative (db, e)
|
adamc@1221
|
898 in
|
adamc@1221
|
899 isKnown r
|
adamc@1221
|
900 end
|
adamc@1215
|
901 | (PCon0 f, [e]) =>
|
adamc@1215
|
902 (case #Variety (unNode (representative (db, e))) of
|
adamc@1215
|
903 Dt0 f' => f' = f
|
adamc@1215
|
904 | _ => false)
|
adamc@1215
|
905 | (PCon1 f, [e]) =>
|
adamc@1215
|
906 (case #Variety (unNode (representative (db, e))) of
|
adamc@1215
|
907 Dt1 (f', _) => f' = f
|
adamc@1215
|
908 | _ => false)
|
adamc@1215
|
909 | (Eq, [e1, e2]) =>
|
adamc@1214
|
910 let
|
adamc@1215
|
911 val r1 = representative (db, e1)
|
adamc@1215
|
912 val r2 = representative (db, e2)
|
adamc@1214
|
913 in
|
adamc@1215
|
914 repOf r1 = repOf r2
|
adamc@1214
|
915 end
|
adamc@1215
|
916 | _ => false
|
adamc@1212
|
917
|
adamc@1218
|
918 fun builtFrom (db, {Base = bs, Derived = d}) =
|
adamc@1218
|
919 let
|
adamc@1218
|
920 val bs = map (fn b => representative (db, b)) bs
|
adamc@1218
|
921
|
adamc@1218
|
922 fun loop d =
|
adamc@1218
|
923 let
|
adamc@1218
|
924 val d = repOf d
|
adamc@1218
|
925 in
|
adamc@1218
|
926 List.exists (fn b => repOf b = d) bs
|
adamc@1218
|
927 orelse case #Variety (unNode d) of
|
adamc@1218
|
928 Dt0 _ => true
|
adamc@1218
|
929 | Dt1 (_, d) => loop d
|
adamc@1218
|
930 | Prim _ => true
|
adamc@1221
|
931 | Recrd (xes, _) => List.all loop (SM.listItems (!xes))
|
adamc@1218
|
932 | Nothing => false
|
adamc@1218
|
933 end
|
adamc@1218
|
934 in
|
adamc@1218
|
935 loop (representative (db, d))
|
adamc@1218
|
936 end
|
adamc@1218
|
937
|
adamc@1208
|
938 end
|
adamc@1208
|
939
|
adamc@1215
|
940 fun decomp fals or =
|
adamc@1215
|
941 let
|
adamc@1215
|
942 fun decomp p k =
|
adamc@1215
|
943 case p of
|
adamc@1215
|
944 True => k []
|
adamc@1215
|
945 | False => fals
|
adamc@1215
|
946 | Unknown => k []
|
adamc@1215
|
947 | And (p1, p2) =>
|
adamc@1215
|
948 decomp p1 (fn ps1 =>
|
adamc@1215
|
949 decomp p2 (fn ps2 =>
|
adamc@1215
|
950 k (ps1 @ ps2)))
|
adamc@1215
|
951 | Or (p1, p2) =>
|
adamc@1215
|
952 or (decomp p1 k, fn () => decomp p2 k)
|
adamc@1215
|
953 | Reln x => k [AReln x]
|
adamc@1215
|
954 | Cond x => k [ACond x]
|
adamc@1215
|
955 in
|
adamc@1215
|
956 decomp
|
adamc@1215
|
957 end
|
adamc@1202
|
958
|
adamc@1226
|
959 val tabs = ref (SM.empty : (string list * string list list) SM.map)
|
adamc@1226
|
960
|
adamc@1218
|
961 fun imply (hyps, goals, outs) =
|
adamc@1218
|
962 let
|
adamc@1218
|
963 fun gls goals onFail acc =
|
adamc@1218
|
964 case goals of
|
adamc@1218
|
965 [] =>
|
adamc@1218
|
966 (let
|
adamc@1218
|
967 val cc = Cc.database ()
|
adamc@1218
|
968 val () = app (fn a => Cc.assert (cc, a)) hyps
|
adamc@1226
|
969
|
adamc@1226
|
970 (* Take advantage of table key information *)
|
adamc@1226
|
971 fun findKeys hyps =
|
adamc@1226
|
972 case hyps of
|
adamc@1226
|
973 [] => ()
|
adamc@1226
|
974 | AReln (Sql tab, [r1]) :: hyps =>
|
adamc@1226
|
975 (case SM.find (!tabs, tab) of
|
adamc@1226
|
976 NONE => findKeys hyps
|
adamc@1226
|
977 | SOME (_, []) => findKeys hyps
|
adamc@1226
|
978 | SOME (_, ks) =>
|
adamc@1226
|
979 let
|
adamc@1226
|
980 fun finder hyps =
|
adamc@1226
|
981 case hyps of
|
adamc@1226
|
982 [] => ()
|
adamc@1226
|
983 | AReln (Sql tab', [r2]) :: hyps =>
|
adamc@1226
|
984 (if tab' = tab andalso
|
adamc@1226
|
985 List.exists (List.all (fn f =>
|
adamc@1226
|
986 let
|
adamc@1226
|
987 val r =
|
adamc@1226
|
988 Cc.check (cc,
|
adamc@1226
|
989 AReln (Eq, [Proj (r1, f),
|
adamc@1226
|
990 Proj (r2, f)]))
|
adamc@1226
|
991 in
|
adamc@1226
|
992 (*Print.prefaces "Fs"
|
adamc@1226
|
993 [("tab",
|
adamc@1226
|
994 Print.PD.string tab),
|
adamc@1226
|
995 ("r1",
|
adamc@1226
|
996 p_exp (Proj (r1, f))),
|
adamc@1226
|
997 ("r2",
|
adamc@1226
|
998 p_exp (Proj (r2, f))),
|
adamc@1226
|
999 ("r",
|
adamc@1226
|
1000 Print.PD.string
|
adamc@1226
|
1001 (Bool.toString r))];*)
|
adamc@1226
|
1002 r
|
adamc@1226
|
1003 end)) ks then
|
adamc@1226
|
1004 ((*Print.prefaces "Key match" [("tab", Print.PD.string tab),
|
adamc@1226
|
1005 ("r1", p_exp r1),
|
adamc@1226
|
1006 ("r2", p_exp r2),
|
adamc@1226
|
1007 ("rp1", Cc.p_repOf cc r1),
|
adamc@1226
|
1008 ("rp2", Cc.p_repOf cc r2)];*)
|
adamc@1226
|
1009 Cc.assert (cc, AReln (Eq, [r1, r2])))
|
adamc@1226
|
1010 else
|
adamc@1226
|
1011 ();
|
adamc@1226
|
1012 finder hyps)
|
adamc@1226
|
1013 | _ :: hyps => finder hyps
|
adamc@1226
|
1014 in
|
adamc@1226
|
1015 finder hyps;
|
adamc@1226
|
1016 findKeys hyps
|
adamc@1226
|
1017 end)
|
adamc@1226
|
1018 | _ :: hyps => findKeys hyps
|
adamc@1218
|
1019 in
|
adamc@1226
|
1020 findKeys hyps;
|
adamc@1226
|
1021
|
adamc@1221
|
1022 (*Print.preface ("db", Cc.p_database cc);*)
|
adamc@1218
|
1023 (List.all (fn a =>
|
adamc@1218
|
1024 if Cc.check (cc, a) then
|
adamc@1218
|
1025 true
|
adamc@1218
|
1026 else
|
adamc@1218
|
1027 ((*Print.prefaces "Can't prove"
|
adamc@1218
|
1028 [("a", p_atom a),
|
adamc@1218
|
1029 ("hyps", Print.p_list p_atom hyps),
|
adamc@1218
|
1030 ("db", Cc.p_database cc)];*)
|
adamc@1218
|
1031 false)) acc
|
adamc@1227
|
1032 andalso ((*Print.preface ("Finding", Cc.p_database cc);*) true)
|
adamc@1220
|
1033 andalso (case outs of
|
adamc@1220
|
1034 NONE => true
|
adamc@1220
|
1035 | SOME outs => Cc.builtFrom (cc, {Derived = Var 0,
|
adamc@1220
|
1036 Base = outs})))
|
adamc@1218
|
1037 handle Cc.Contradiction => false
|
adamc@1218
|
1038 end handle Cc.Undetermined => false)
|
adamc@1218
|
1039 orelse onFail ()
|
adamc@1218
|
1040 | (g as AReln (Sql gf, [ge])) :: goals =>
|
adamc@1218
|
1041 let
|
adamc@1218
|
1042 fun hps hyps =
|
adamc@1218
|
1043 case hyps of
|
adamc@1218
|
1044 [] => gls goals onFail (g :: acc)
|
adamc@1218
|
1045 | (h as AReln (Sql hf, [he])) :: hyps =>
|
adamc@1218
|
1046 if gf = hf then
|
adamc@1218
|
1047 let
|
adamc@1218
|
1048 val saved = save ()
|
adamc@1218
|
1049 in
|
adamc@1218
|
1050 if eq (ge, he) then
|
adamc@1218
|
1051 let
|
adamc@1218
|
1052 val changed = IM.numItems (!unif)
|
adamc@1218
|
1053 <> IM.numItems saved
|
adamc@1218
|
1054 in
|
adamc@1218
|
1055 gls goals (fn () => (restore saved;
|
adamc@1218
|
1056 changed
|
adamc@1218
|
1057 andalso hps hyps))
|
adamc@1218
|
1058 acc
|
adamc@1218
|
1059 end
|
adamc@1218
|
1060 else
|
adamc@1218
|
1061 hps hyps
|
adamc@1218
|
1062 end
|
adamc@1218
|
1063 else
|
adamc@1218
|
1064 hps hyps
|
adamc@1218
|
1065 | _ :: hyps => hps hyps
|
adamc@1218
|
1066 in
|
adamc@1218
|
1067 hps hyps
|
adamc@1218
|
1068 end
|
adamc@1218
|
1069 | g :: goals => gls goals onFail (g :: acc)
|
adamc@1218
|
1070 in
|
adamc@1218
|
1071 reset ();
|
adamc@1218
|
1072 (*Print.prefaces "Big go" [("hyps", Print.p_list p_atom hyps),
|
adamc@1223
|
1073 ("goals", Print.p_list p_atom goals)];*)
|
adamc@1218
|
1074 gls goals (fn () => false) []
|
adamc@1218
|
1075 end handle Cc.Contradiction => true
|
adamc@1200
|
1076
|
adamc@1200
|
1077 fun patCon pc =
|
adamc@1200
|
1078 case pc of
|
adamc@1200
|
1079 PConVar n => "C" ^ Int.toString n
|
adamc@1200
|
1080 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
|
adamc@1200
|
1081
|
adamc@1200
|
1082 datatype chunk =
|
adamc@1200
|
1083 String of string
|
adamc@1200
|
1084 | Exp of Mono.exp
|
adamc@1200
|
1085
|
adamc@1200
|
1086 fun chunkify e =
|
adamc@1200
|
1087 case #1 e of
|
adamc@1200
|
1088 EPrim (Prim.String s) => [String s]
|
adamc@1207
|
1089 | EStrcat (e1, e2) =>
|
adamc@1207
|
1090 let
|
adamc@1207
|
1091 val chs1 = chunkify e1
|
adamc@1207
|
1092 val chs2 = chunkify e2
|
adamc@1207
|
1093 in
|
adamc@1207
|
1094 case chs2 of
|
adamc@1207
|
1095 String s2 :: chs2' =>
|
adamc@1207
|
1096 (case List.last chs1 of
|
adamc@1207
|
1097 String s1 => List.take (chs1, length chs1 - 1) @ String (s1 ^ s2) :: chs2'
|
adamc@1207
|
1098 | _ => chs1 @ chs2)
|
adamc@1207
|
1099 | _ => chs1 @ chs2
|
adamc@1207
|
1100 end
|
adamc@1200
|
1101 | _ => [Exp e]
|
adamc@1200
|
1102
|
adamc@1201
|
1103 type 'a parser = chunk list -> ('a * chunk list) option
|
adamc@1201
|
1104
|
adamc@1201
|
1105 fun always v chs = SOME (v, chs)
|
adamc@1201
|
1106
|
adamc@1202
|
1107 fun parse p s =
|
adamc@1202
|
1108 case p (chunkify s) of
|
adamc@1201
|
1109 SOME (v, []) => SOME v
|
adamc@1201
|
1110 | _ => NONE
|
adamc@1201
|
1111
|
adamc@1201
|
1112 fun const s chs =
|
adamc@1201
|
1113 case chs of
|
adamc@1201
|
1114 String s' :: chs => if String.isPrefix s s' then
|
adamc@1201
|
1115 SOME ((), if size s = size s' then
|
adamc@1201
|
1116 chs
|
adamc@1201
|
1117 else
|
adamc@1201
|
1118 String (String.extract (s', size s, NONE)) :: chs)
|
adamc@1201
|
1119 else
|
adamc@1201
|
1120 NONE
|
adamc@1201
|
1121 | _ => NONE
|
adamc@1201
|
1122
|
adamc@1201
|
1123 fun follow p1 p2 chs =
|
adamc@1201
|
1124 case p1 chs of
|
adamc@1201
|
1125 NONE => NONE
|
adamc@1201
|
1126 | SOME (v1, chs) =>
|
adamc@1201
|
1127 case p2 chs of
|
adamc@1201
|
1128 NONE => NONE
|
adamc@1201
|
1129 | SOME (v2, chs) => SOME ((v1, v2), chs)
|
adamc@1201
|
1130
|
adamc@1201
|
1131 fun wrap p f chs =
|
adamc@1201
|
1132 case p chs of
|
adamc@1201
|
1133 NONE => NONE
|
adamc@1201
|
1134 | SOME (v, chs) => SOME (f v, chs)
|
adamc@1201
|
1135
|
adamc@1209
|
1136 fun wrapP p f chs =
|
adamc@1209
|
1137 case p chs of
|
adamc@1209
|
1138 NONE => NONE
|
adamc@1209
|
1139 | SOME (v, chs) =>
|
adamc@1209
|
1140 case f v of
|
adamc@1209
|
1141 NONE => NONE
|
adamc@1209
|
1142 | SOME r => SOME (r, chs)
|
adamc@1209
|
1143
|
adamc@1201
|
1144 fun alt p1 p2 chs =
|
adamc@1201
|
1145 case p1 chs of
|
adamc@1201
|
1146 NONE => p2 chs
|
adamc@1201
|
1147 | v => v
|
adamc@1201
|
1148
|
adamc@1207
|
1149 fun altL ps =
|
adamc@1207
|
1150 case rev ps of
|
adamc@1207
|
1151 [] => (fn _ => NONE)
|
adamc@1207
|
1152 | p :: ps =>
|
adamc@1207
|
1153 foldl (fn (p1, p2) => alt p1 p2) p ps
|
adamc@1207
|
1154
|
adamc@1204
|
1155 fun opt p chs =
|
adamc@1204
|
1156 case p chs of
|
adamc@1204
|
1157 NONE => SOME (NONE, chs)
|
adamc@1204
|
1158 | SOME (v, chs) => SOME (SOME v, chs)
|
adamc@1204
|
1159
|
adamc@1201
|
1160 fun skip cp chs =
|
adamc@1201
|
1161 case chs of
|
adamc@1201
|
1162 String "" :: chs => skip cp chs
|
adamc@1201
|
1163 | String s :: chs' => if cp (String.sub (s, 0)) then
|
adamc@1201
|
1164 skip cp (String (String.extract (s, 1, NONE)) :: chs')
|
adamc@1201
|
1165 else
|
adamc@1201
|
1166 SOME ((), chs)
|
adamc@1201
|
1167 | _ => SOME ((), chs)
|
adamc@1201
|
1168
|
adamc@1201
|
1169 fun keep cp chs =
|
adamc@1201
|
1170 case chs of
|
adamc@1201
|
1171 String "" :: chs => keep cp chs
|
adamc@1201
|
1172 | String s :: chs' =>
|
adamc@1201
|
1173 let
|
adamc@1201
|
1174 val (befor, after) = Substring.splitl cp (Substring.full s)
|
adamc@1201
|
1175 in
|
adamc@1201
|
1176 if Substring.isEmpty befor then
|
adamc@1201
|
1177 NONE
|
adamc@1201
|
1178 else
|
adamc@1201
|
1179 SOME (Substring.string befor,
|
adamc@1201
|
1180 if Substring.isEmpty after then
|
adamc@1201
|
1181 chs'
|
adamc@1201
|
1182 else
|
adamc@1201
|
1183 String (Substring.string after) :: chs')
|
adamc@1201
|
1184 end
|
adamc@1201
|
1185 | _ => NONE
|
adamc@1201
|
1186
|
adamc@1204
|
1187 fun ws p = wrap (follow (skip (fn ch => ch = #" "))
|
adamc@1204
|
1188 (follow p (skip (fn ch => ch = #" ")))) (#1 o #2)
|
adamc@1204
|
1189
|
adamc@1204
|
1190 fun log name p chs =
|
adamc@1206
|
1191 (if !debug then
|
adamc@1227
|
1192 (print (name ^ ": ");
|
adamc@1227
|
1193 app (fn String s => print s
|
adamc@1227
|
1194 | _ => print "???") chs;
|
adamc@1227
|
1195 print "\n")
|
adamc@1206
|
1196 else
|
adamc@1206
|
1197 ();
|
adamc@1204
|
1198 p chs)
|
adamc@1201
|
1199
|
adamc@1201
|
1200 fun list p chs =
|
adamc@1207
|
1201 altL [wrap (follow p (follow (ws (const ",")) (list p)))
|
adamc@1207
|
1202 (fn (v, ((), ls)) => v :: ls),
|
adamc@1207
|
1203 wrap (ws p) (fn v => [v]),
|
adamc@1207
|
1204 always []] chs
|
adamc@1201
|
1205
|
adamc@1201
|
1206 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
|
adamc@1201
|
1207
|
adamc@1211
|
1208 val t_ident = wrapP ident (fn s => if String.isPrefix "T_" s then
|
adamc@1211
|
1209 SOME (String.extract (s, 2, NONE))
|
adamc@1201
|
1210 else
|
adamc@1211
|
1211 NONE)
|
adamc@1211
|
1212 val uw_ident = wrapP ident (fn s => if String.isPrefix "uw_" s andalso size s >= 4 then
|
adamc@1211
|
1213 SOME (str (Char.toUpper (String.sub (s, 3)))
|
adamc@1211
|
1214 ^ String.extract (s, 4, NONE))
|
adamc@1211
|
1215 else
|
adamc@1211
|
1216 NONE)
|
adamc@1201
|
1217
|
adamc@1211
|
1218 val field = wrap (follow t_ident
|
adamc@1201
|
1219 (follow (const ".")
|
adamc@1201
|
1220 uw_ident))
|
adamc@1201
|
1221 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
1222
|
adamc@1206
|
1223 datatype Rel =
|
adamc@1206
|
1224 Exps of exp * exp -> prop
|
adamc@1206
|
1225 | Props of prop * prop -> prop
|
adamc@1206
|
1226
|
adamc@1204
|
1227 datatype sqexp =
|
adamc@1206
|
1228 SqConst of Prim.t
|
adamc@1206
|
1229 | Field of string * string
|
adamc@1206
|
1230 | Binop of Rel * sqexp * sqexp
|
adamc@1207
|
1231 | SqKnown of sqexp
|
adamc@1207
|
1232 | Inj of Mono.exp
|
adamc@1211
|
1233 | SqFunc of string * sqexp
|
adamc@1211
|
1234 | Count
|
adamc@1204
|
1235
|
adamc@1210
|
1236 fun cmp s r = wrap (const s) (fn () => Exps (fn (e1, e2) => Reln (r, [e1, e2])))
|
adamc@1210
|
1237
|
adamc@1210
|
1238 val sqbrel = altL [cmp "=" Eq,
|
adamc@1210
|
1239 cmp "<>" Ne,
|
adamc@1210
|
1240 cmp "<=" Le,
|
adamc@1210
|
1241 cmp "<" Lt,
|
adamc@1210
|
1242 cmp ">=" Ge,
|
adamc@1210
|
1243 cmp ">" Gt,
|
adamc@1207
|
1244 wrap (const "AND") (fn () => Props And),
|
adamc@1207
|
1245 wrap (const "OR") (fn () => Props Or)]
|
adamc@1204
|
1246
|
adamc@1204
|
1247 datatype ('a, 'b) sum = inl of 'a | inr of 'b
|
adamc@1204
|
1248
|
adamc@1209
|
1249 fun string chs =
|
adamc@1206
|
1250 case chs of
|
adamc@1209
|
1251 String s :: chs =>
|
adamc@1209
|
1252 if size s >= 2 andalso String.sub (s, 0) = #"'" then
|
adamc@1209
|
1253 let
|
adamc@1209
|
1254 fun loop (cs, acc) =
|
adamc@1209
|
1255 case cs of
|
adamc@1209
|
1256 [] => NONE
|
adamc@1209
|
1257 | c :: cs =>
|
adamc@1209
|
1258 if c = #"'" then
|
adamc@1209
|
1259 SOME (String.implode (rev acc), cs)
|
adamc@1209
|
1260 else if c = #"\\" then
|
adamc@1209
|
1261 case cs of
|
adamc@1209
|
1262 c :: cs => loop (cs, c :: acc)
|
adamc@1209
|
1263 | _ => raise Fail "Iflow.string: Unmatched backslash escape"
|
adamc@1209
|
1264 else
|
adamc@1209
|
1265 loop (cs, c :: acc)
|
adamc@1209
|
1266 in
|
adamc@1209
|
1267 case loop (String.explode (String.extract (s, 1, NONE)), []) of
|
adamc@1209
|
1268 NONE => NONE
|
adamc@1209
|
1269 | SOME (s, []) => SOME (s, chs)
|
adamc@1209
|
1270 | SOME (s, cs) => SOME (s, String (String.implode cs) :: chs)
|
adamc@1209
|
1271 end
|
adamc@1209
|
1272 else
|
adamc@1209
|
1273 NONE
|
adamc@1209
|
1274 | _ => NONE
|
adamc@1206
|
1275
|
adamc@1209
|
1276 val prim =
|
adamc@1209
|
1277 altL [wrap (follow (wrapP (follow (keep Char.isDigit) (follow (const ".") (keep Char.isDigit)))
|
adamc@1209
|
1278 (fn (x, ((), y)) => Option.map Prim.Float (Real64.fromString (x ^ "." ^ y))))
|
adamc@1209
|
1279 (opt (const "::float8"))) #1,
|
adamc@1209
|
1280 wrap (follow (wrapP (keep Char.isDigit)
|
adamc@1209
|
1281 (Option.map Prim.Int o Int64.fromString))
|
adamc@1209
|
1282 (opt (const "::int8"))) #1,
|
adamc@1209
|
1283 wrap (follow (opt (const "E")) (follow string (opt (const "::text"))))
|
adamc@1209
|
1284 (Prim.String o #1 o #2)]
|
adamc@1206
|
1285
|
adamc@1207
|
1286 fun known' chs =
|
adamc@1207
|
1287 case chs of
|
adamc@1207
|
1288 Exp (EFfi ("Basis", "sql_known"), _) :: chs => SOME ((), chs)
|
adamc@1207
|
1289 | _ => NONE
|
adamc@1207
|
1290
|
adamc@1207
|
1291 fun sqlify chs =
|
adamc@1207
|
1292 case chs of
|
adamc@1207
|
1293 Exp (EFfiApp ("Basis", f, [e]), _) :: chs =>
|
adamc@1207
|
1294 if String.isPrefix "sqlify" f then
|
adamc@1207
|
1295 SOME (e, chs)
|
adamc@1207
|
1296 else
|
adamc@1207
|
1297 NONE
|
adamc@1207
|
1298 | _ => NONE
|
adamc@1207
|
1299
|
adamc@1211
|
1300 fun constK s = wrap (const s) (fn () => s)
|
adamc@1211
|
1301
|
adamc@1211
|
1302 val funcName = altL [constK "COUNT",
|
adamc@1211
|
1303 constK "MIN",
|
adamc@1211
|
1304 constK "MAX",
|
adamc@1211
|
1305 constK "SUM",
|
adamc@1211
|
1306 constK "AVG"]
|
adamc@1211
|
1307
|
adamc@1204
|
1308 fun sqexp chs =
|
adamc@1206
|
1309 log "sqexp"
|
adamc@1207
|
1310 (altL [wrap prim SqConst,
|
adamc@1211
|
1311 wrap field Field,
|
adamc@1207
|
1312 wrap known SqKnown,
|
adamc@1211
|
1313 wrap func SqFunc,
|
adamc@1211
|
1314 wrap (const "COUNT(*)") (fn () => Count),
|
adamc@1207
|
1315 wrap sqlify Inj,
|
adamc@1211
|
1316 wrap (follow (const "COALESCE(") (follow sqexp (follow (const ",")
|
adamc@1211
|
1317 (follow (keep (fn ch => ch <> #")")) (const ")")))))
|
adamc@1211
|
1318 (fn ((), (e, _)) => e),
|
adamc@1207
|
1319 wrap (follow (ws (const "("))
|
adamc@1207
|
1320 (follow (wrap
|
adamc@1207
|
1321 (follow sqexp
|
adamc@1207
|
1322 (alt
|
adamc@1207
|
1323 (wrap
|
adamc@1207
|
1324 (follow (ws sqbrel)
|
adamc@1207
|
1325 (ws sqexp))
|
adamc@1207
|
1326 inl)
|
adamc@1207
|
1327 (always (inr ()))))
|
adamc@1207
|
1328 (fn (e1, sm) =>
|
adamc@1207
|
1329 case sm of
|
adamc@1207
|
1330 inl (bo, e2) => Binop (bo, e1, e2)
|
adamc@1207
|
1331 | inr () => e1))
|
adamc@1207
|
1332 (const ")")))
|
adamc@1207
|
1333 (fn ((), (e, ())) => e)])
|
adamc@1207
|
1334 chs
|
adamc@1206
|
1335
|
adamc@1207
|
1336 and known chs = wrap (follow known' (follow (const "(") (follow sqexp (const ")"))))
|
adamc@1211
|
1337 (fn ((), ((), (e, ()))) => e) chs
|
adamc@1211
|
1338
|
adamc@1211
|
1339 and func chs = wrap (follow funcName (follow (const "(") (follow sqexp (const ")"))))
|
adamc@1211
|
1340 (fn (f, ((), (e, ()))) => (f, e)) chs
|
adamc@1211
|
1341
|
adamc@1211
|
1342 datatype sitem =
|
adamc@1211
|
1343 SqField of string * string
|
adamc@1211
|
1344 | SqExp of sqexp * string
|
adamc@1211
|
1345
|
adamc@1211
|
1346 val sitem = alt (wrap field SqField)
|
adamc@1211
|
1347 (wrap (follow sqexp (follow (const " AS ") uw_ident))
|
adamc@1211
|
1348 (fn (e, ((), s)) => SqExp (e, s)))
|
adamc@1207
|
1349
|
adamc@1207
|
1350 val select = log "select"
|
adamc@1207
|
1351 (wrap (follow (const "SELECT ") (list sitem))
|
adamc@1207
|
1352 (fn ((), ls) => ls))
|
adamc@1201
|
1353
|
adamc@1201
|
1354 val fitem = wrap (follow uw_ident
|
adamc@1201
|
1355 (follow (const " AS ")
|
adamc@1201
|
1356 t_ident))
|
adamc@1201
|
1357 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
1358
|
adamc@1207
|
1359 val from = log "from"
|
adamc@1207
|
1360 (wrap (follow (const "FROM ") (list fitem))
|
adamc@1207
|
1361 (fn ((), ls) => ls))
|
adamc@1201
|
1362
|
adamc@1204
|
1363 val wher = wrap (follow (ws (const "WHERE ")) sqexp)
|
adamc@1204
|
1364 (fn ((), ls) => ls)
|
adamc@1204
|
1365
|
adamc@1227
|
1366 type query1 = {Select : sitem list,
|
adamc@1227
|
1367 From : (string * string) list,
|
adamc@1227
|
1368 Where : sqexp option}
|
adamc@1227
|
1369
|
adamc@1227
|
1370 val query1 = log "query1"
|
adamc@1207
|
1371 (wrap (follow (follow select from) (opt wher))
|
adamc@1207
|
1372 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
|
adamc@1201
|
1373
|
adamc@1227
|
1374 datatype query =
|
adamc@1227
|
1375 Query1 of query1
|
adamc@1227
|
1376 | Union of query * query
|
adamc@1227
|
1377
|
adamc@1227
|
1378 fun query chs = log "query"
|
adamc@1227
|
1379 (alt (wrap (follow (const "((")
|
adamc@1227
|
1380 (follow query
|
adamc@1227
|
1381 (follow (const ") UNION (")
|
adamc@1227
|
1382 (follow query (const "))")))))
|
adamc@1227
|
1383 (fn ((), (q1, ((), (q2, ())))) => Union (q1, q2)))
|
adamc@1227
|
1384 (wrap query1 Query1))
|
adamc@1227
|
1385 chs
|
adamc@1227
|
1386
|
adamc@1220
|
1387 datatype dml =
|
adamc@1220
|
1388 Insert of string * (string * sqexp) list
|
adamc@1221
|
1389 | Delete of string * sqexp
|
adamc@1223
|
1390 | Update of string * (string * sqexp) list * sqexp
|
adamc@1220
|
1391
|
adamc@1220
|
1392 val insert = log "insert"
|
adamc@1220
|
1393 (wrapP (follow (const "INSERT INTO ")
|
adamc@1220
|
1394 (follow uw_ident
|
adamc@1220
|
1395 (follow (const " (")
|
adamc@1220
|
1396 (follow (list uw_ident)
|
adamc@1220
|
1397 (follow (const ") VALUES (")
|
adamc@1220
|
1398 (follow (list sqexp)
|
adamc@1220
|
1399 (const ")")))))))
|
adamc@1220
|
1400 (fn ((), (tab, ((), (fs, ((), (es, ())))))) =>
|
adamc@1221
|
1401 (SOME (tab, ListPair.zipEq (fs, es)))
|
adamc@1220
|
1402 handle ListPair.UnequalLengths => NONE))
|
adamc@1220
|
1403
|
adamc@1221
|
1404 val delete = log "delete"
|
adamc@1221
|
1405 (wrap (follow (const "DELETE FROM ")
|
adamc@1221
|
1406 (follow uw_ident
|
adamc@1221
|
1407 (follow (const " AS T_T WHERE ")
|
adamc@1221
|
1408 sqexp)))
|
adamc@1221
|
1409 (fn ((), (tab, ((), es))) => (tab, es)))
|
adamc@1221
|
1410
|
adamc@1223
|
1411 val setting = log "setting"
|
adamc@1223
|
1412 (wrap (follow uw_ident (follow (const " = ") sqexp))
|
adamc@1223
|
1413 (fn (f, ((), e)) => (f, e)))
|
adamc@1223
|
1414
|
adamc@1223
|
1415 val update = log "update"
|
adamc@1223
|
1416 (wrap (follow (const "UPDATE ")
|
adamc@1223
|
1417 (follow uw_ident
|
adamc@1223
|
1418 (follow (const " AS T_T SET ")
|
adamc@1223
|
1419 (follow (list setting)
|
adamc@1223
|
1420 (follow (ws (const "WHERE "))
|
adamc@1223
|
1421 sqexp)))))
|
adamc@1223
|
1422 (fn ((), (tab, ((), (fs, ((), e))))) =>
|
adamc@1223
|
1423 (tab, fs, e)))
|
adamc@1223
|
1424
|
adamc@1220
|
1425 val dml = log "dml"
|
adamc@1221
|
1426 (altL [wrap insert Insert,
|
adamc@1223
|
1427 wrap delete Delete,
|
adamc@1223
|
1428 wrap update Update])
|
adamc@1220
|
1429
|
adamc@1215
|
1430 fun removeDups (ls : (string * string) list) =
|
adamc@1211
|
1431 case ls of
|
adamc@1211
|
1432 [] => []
|
adamc@1211
|
1433 | x :: ls =>
|
adamc@1211
|
1434 let
|
adamc@1211
|
1435 val ls = removeDups ls
|
adamc@1211
|
1436 in
|
adamc@1211
|
1437 if List.exists (fn x' => x' = x) ls then
|
adamc@1211
|
1438 ls
|
adamc@1211
|
1439 else
|
adamc@1211
|
1440 x :: ls
|
adamc@1211
|
1441 end
|
adamc@1211
|
1442
|
adamc@1212
|
1443 datatype queryMode =
|
adamc@1218
|
1444 SomeCol
|
adamc@1212
|
1445 | AllCols of exp
|
adamc@1212
|
1446
|
adamc@1220
|
1447 fun expIn rv env rvOf =
|
adamc@1220
|
1448 let
|
adamc@1220
|
1449 fun expIn (e, rvN) =
|
adamc@1220
|
1450 let
|
adamc@1220
|
1451 fun default () =
|
adamc@1220
|
1452 let
|
adamc@1220
|
1453 val (rvN, e) = rv rvN
|
adamc@1220
|
1454 in
|
adamc@1220
|
1455 (inl e, rvN)
|
adamc@1220
|
1456 end
|
adamc@1220
|
1457 in
|
adamc@1220
|
1458 case e of
|
adamc@1220
|
1459 SqConst p => (inl (Const p), rvN)
|
adamc@1220
|
1460 | Field (v, f) => (inl (Proj (rvOf v, f)), rvN)
|
adamc@1220
|
1461 | Binop (bo, e1, e2) =>
|
adamc@1220
|
1462 let
|
adamc@1220
|
1463 val (e1, rvN) = expIn (e1, rvN)
|
adamc@1220
|
1464 val (e2, rvN) = expIn (e2, rvN)
|
adamc@1220
|
1465 in
|
adamc@1220
|
1466 (inr (case (bo, e1, e2) of
|
adamc@1220
|
1467 (Exps f, inl e1, inl e2) => f (e1, e2)
|
adamc@1220
|
1468 | (Props f, inr p1, inr p2) => f (p1, p2)
|
adamc@1220
|
1469 | _ => Unknown), rvN)
|
adamc@1220
|
1470 end
|
adamc@1220
|
1471 | SqKnown e =>
|
adamc@1220
|
1472 (case expIn (e, rvN) of
|
adamc@1220
|
1473 (inl e, rvN) => (inr (Reln (Known, [e])), rvN)
|
adamc@1220
|
1474 | _ => (inr Unknown, rvN))
|
adamc@1220
|
1475 | Inj e =>
|
adamc@1220
|
1476 let
|
adamc@1220
|
1477 fun deinj e =
|
adamc@1220
|
1478 case #1 e of
|
adamc@1220
|
1479 ERel n => (List.nth (env, n), rvN)
|
adamc@1220
|
1480 | EField (e, f) =>
|
adamc@1220
|
1481 let
|
adamc@1220
|
1482 val (e, rvN) = deinj e
|
adamc@1220
|
1483 in
|
adamc@1220
|
1484 (Proj (e, f), rvN)
|
adamc@1220
|
1485 end
|
adamc@1220
|
1486 | _ =>
|
adamc@1220
|
1487 let
|
adamc@1220
|
1488 val (rvN, e) = rv rvN
|
adamc@1220
|
1489 in
|
adamc@1220
|
1490 (e, rvN)
|
adamc@1220
|
1491 end
|
adamc@1220
|
1492
|
adamc@1220
|
1493 val (e, rvN) = deinj e
|
adamc@1220
|
1494 in
|
adamc@1220
|
1495 (inl e, rvN)
|
adamc@1220
|
1496 end
|
adamc@1220
|
1497 | SqFunc (f, e) =>
|
adamc@1220
|
1498 (case expIn (e, rvN) of
|
adamc@1220
|
1499 (inl e, rvN) => (inl (Func (Other f, [e])), rvN)
|
adamc@1220
|
1500 | _ => default ())
|
adamc@1220
|
1501
|
adamc@1220
|
1502 | Count => default ()
|
adamc@1220
|
1503 end
|
adamc@1220
|
1504 in
|
adamc@1220
|
1505 expIn
|
adamc@1220
|
1506 end
|
adamc@1216
|
1507
|
adamc@1214
|
1508 fun queryProp env rvN rv oe e =
|
adamc@1216
|
1509 let
|
adamc@1216
|
1510 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1216
|
1511 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1227
|
1512 (rvN, Unknown, [], []))
|
adamc@1216
|
1513 in
|
adamc@1216
|
1514 case parse query e of
|
adamc@1216
|
1515 NONE => default ()
|
adamc@1227
|
1516 | SOME q =>
|
adamc@1216
|
1517 let
|
adamc@1227
|
1518 fun doQuery (q, rvN) =
|
adamc@1227
|
1519 case q of
|
adamc@1227
|
1520 Query1 r =>
|
adamc@1227
|
1521 let
|
adamc@1227
|
1522 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1227
|
1523 let
|
adamc@1227
|
1524 val (rvN, e) = rv rvN
|
adamc@1227
|
1525 in
|
adamc@1227
|
1526 ((v, e), rvN)
|
adamc@1227
|
1527 end) rvN (#From r)
|
adamc@1214
|
1528
|
adamc@1227
|
1529 fun rvOf v =
|
adamc@1227
|
1530 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1227
|
1531 NONE => raise Fail "Iflow.queryProp: Bad table variable"
|
adamc@1227
|
1532 | SOME (_, e) => e
|
adamc@1214
|
1533
|
adamc@1227
|
1534 fun usedFields e =
|
adamc@1227
|
1535 case e of
|
adamc@1227
|
1536 SqConst _ => []
|
adamc@1227
|
1537 | Field (v, f) => [(v, f)]
|
adamc@1227
|
1538 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
|
adamc@1227
|
1539 | SqKnown _ => []
|
adamc@1227
|
1540 | Inj _ => []
|
adamc@1227
|
1541 | SqFunc (_, e) => usedFields e
|
adamc@1227
|
1542 | Count => []
|
adamc@1214
|
1543
|
adamc@1227
|
1544 val p =
|
adamc@1227
|
1545 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
|
adamc@1211
|
1546
|
adamc@1227
|
1547 val expIn = expIn rv env rvOf
|
adamc@1205
|
1548
|
adamc@1227
|
1549 val (p, rvN) = case #Where r of
|
adamc@1227
|
1550 NONE => (p, rvN)
|
adamc@1227
|
1551 | SOME e =>
|
adamc@1227
|
1552 case expIn (e, rvN) of
|
adamc@1227
|
1553 (inr p', rvN) => (And (p, p'), rvN)
|
adamc@1227
|
1554 | _ => (p, rvN)
|
adamc@1210
|
1555
|
adamc@1227
|
1556 fun normal () =
|
adamc@1227
|
1557 case oe of
|
adamc@1227
|
1558 SomeCol =>
|
adamc@1227
|
1559 let
|
adamc@1227
|
1560 val (sis, rvN) =
|
adamc@1227
|
1561 ListUtil.foldlMap
|
adamc@1227
|
1562 (fn (si, rvN) =>
|
adamc@1227
|
1563 case si of
|
adamc@1227
|
1564 SqField (v, f) => (Proj (rvOf v, f), rvN)
|
adamc@1227
|
1565 | SqExp (e, f) =>
|
adamc@1227
|
1566 case expIn (e, rvN) of
|
adamc@1227
|
1567 (inr _, _) =>
|
adamc@1227
|
1568 let
|
adamc@1227
|
1569 val (rvN, e) = rv rvN
|
adamc@1227
|
1570 in
|
adamc@1227
|
1571 (e, rvN)
|
adamc@1227
|
1572 end
|
adamc@1227
|
1573 | (inl e, rvN) => (e, rvN)) rvN (#Select r)
|
adamc@1227
|
1574 in
|
adamc@1227
|
1575 (rvN, p, True, sis)
|
adamc@1227
|
1576 end
|
adamc@1227
|
1577 | AllCols oe =>
|
adamc@1227
|
1578 let
|
adamc@1227
|
1579 val (ts, es, rvN) =
|
adamc@1227
|
1580 foldl (fn (si, (ts, es, rvN)) =>
|
adamc@1227
|
1581 case si of
|
adamc@1227
|
1582 SqField (v, f) =>
|
adamc@1227
|
1583 let
|
adamc@1227
|
1584 val fs = getOpt (SM.find (ts, v), SM.empty)
|
adamc@1227
|
1585 in
|
adamc@1227
|
1586 (SM.insert (ts, v, SM.insert (fs, f, Proj (rvOf v, f))), es, rvN)
|
adamc@1227
|
1587 end
|
adamc@1227
|
1588 | SqExp (e, f) =>
|
adamc@1227
|
1589 let
|
adamc@1227
|
1590 val (e, rvN) =
|
adamc@1227
|
1591 case expIn (e, rvN) of
|
adamc@1227
|
1592 (inr _, rvN) =>
|
adamc@1227
|
1593 let
|
adamc@1227
|
1594 val (rvN, e) = rv rvN
|
adamc@1227
|
1595 in
|
adamc@1227
|
1596 (e, rvN)
|
adamc@1227
|
1597 end
|
adamc@1227
|
1598 | (inl e, rvN) => (e, rvN)
|
adamc@1227
|
1599 in
|
adamc@1227
|
1600 (ts, SM.insert (es, f, e), rvN)
|
adamc@1227
|
1601 end)
|
adamc@1227
|
1602 (SM.empty, SM.empty, rvN) (#Select r)
|
adamc@1227
|
1603
|
adamc@1227
|
1604 val p' = Reln (Eq, [oe, Recd (map (fn (t, fs) => (t, Recd (SM.listItemsi fs)))
|
adamc@1227
|
1605 (SM.listItemsi ts)
|
adamc@1227
|
1606 @ SM.listItemsi es)])
|
adamc@1227
|
1607 in
|
adamc@1227
|
1608 (rvN, And (p, p'), True, [])
|
adamc@1227
|
1609 end
|
adamc@1227
|
1610
|
adamc@1227
|
1611 val (rvN, p, wp, outs) =
|
adamc@1227
|
1612 case #Select r of
|
adamc@1227
|
1613 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
|
adamc@1227
|
1614 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
|
adamc@1227
|
1615 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
|
adamc@1227
|
1616 (case oe of
|
adamc@1227
|
1617 SomeCol =>
|
adamc@1227
|
1618 let
|
adamc@1227
|
1619 val (rvN, oe) = rv rvN
|
adamc@1227
|
1620 in
|
adamc@1227
|
1621 (rvN,
|
adamc@1227
|
1622 Or (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.False", [])]),
|
adamc@1227
|
1623 And (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1227
|
1624 p)),
|
adamc@1227
|
1625 Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1227
|
1626 [oe])
|
adamc@1227
|
1627 end
|
adamc@1227
|
1628 | AllCols oe =>
|
adamc@1227
|
1629 let
|
adamc@1227
|
1630 fun oeEq e = Reln (Eq, [oe, Recd [(f, e)]])
|
adamc@1227
|
1631 in
|
adamc@1227
|
1632 (rvN,
|
adamc@1227
|
1633 Or (oeEq (Func (DtCon0 "Basis.bool.False", [])),
|
adamc@1227
|
1634 And (oeEq (Func (DtCon0 "Basis.bool.True", [])),
|
adamc@1227
|
1635 p)),
|
adamc@1227
|
1636 oeEq (Func (DtCon0 "Basis.bool.True", [])),
|
adamc@1227
|
1637 [])
|
adamc@1227
|
1638 end)
|
adamc@1227
|
1639 | _ => normal ())
|
adamc@1227
|
1640 | _ => normal ()
|
adamc@1227
|
1641 in
|
adamc@1227
|
1642 (rvN, p, map (fn x => (wp, x))
|
adamc@1227
|
1643 (case #Where r of
|
adamc@1227
|
1644 NONE => []
|
adamc@1227
|
1645 | SOME e => map (fn (v, f) => Proj (rvOf v, f)) (usedFields e)), outs)
|
adamc@1227
|
1646 end
|
adamc@1227
|
1647 | Union (q1, q2) =>
|
adamc@1220
|
1648 let
|
adamc@1227
|
1649 val (rvN, p1, used1, outs1) = doQuery (q1, rvN)
|
adamc@1227
|
1650 val (rvN, p2, used2, outs2) = doQuery (q2, rvN)
|
adamc@1220
|
1651 in
|
adamc@1227
|
1652 case (outs1, outs2) of
|
adamc@1227
|
1653 ([], []) => (rvN, Or (p1, p2),
|
adamc@1227
|
1654 map (fn (p, e) => (And (p1, p), e)) used1
|
adamc@1227
|
1655 @ map (fn (p, e) => (And (p2, p), e)) used2, [])
|
adamc@1227
|
1656 | _ => default ()
|
adamc@1220
|
1657 end
|
adamc@1216
|
1658 in
|
adamc@1227
|
1659 doQuery (q, rvN)
|
adamc@1216
|
1660 end
|
adamc@1220
|
1661 end
|
adamc@1220
|
1662
|
adamc@1220
|
1663 fun insertProp rvN rv e =
|
adamc@1220
|
1664 let
|
adamc@1220
|
1665 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1220
|
1666 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1220
|
1667 Unknown)
|
adamc@1220
|
1668 in
|
adamc@1220
|
1669 case parse query e of
|
adamc@1227
|
1670 SOME (Query1 r) =>
|
adamc@1220
|
1671 let
|
adamc@1220
|
1672 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1220
|
1673 let
|
adamc@1220
|
1674 val (rvN, e) = rv rvN
|
adamc@1220
|
1675 in
|
adamc@1220
|
1676 ((v, e), rvN)
|
adamc@1220
|
1677 end) rvN (#From r)
|
adamc@1220
|
1678
|
adamc@1220
|
1679 fun rvOf v =
|
adamc@1220
|
1680 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1220
|
1681 NONE => raise Fail "Iflow.insertProp: Bad table variable"
|
adamc@1220
|
1682 | SOME (_, e) => e
|
adamc@1220
|
1683
|
adamc@1220
|
1684 val p =
|
adamc@1220
|
1685 foldl (fn ((t, v), p) =>
|
adamc@1220
|
1686 let
|
adamc@1220
|
1687 val t =
|
adamc@1220
|
1688 case v of
|
adamc@1220
|
1689 "New" => "$New"
|
adamc@1220
|
1690 | _ => t
|
adamc@1220
|
1691 in
|
adamc@1220
|
1692 And (p, Reln (Sql t, [rvOf v]))
|
adamc@1220
|
1693 end) True (#From r)
|
adamc@1220
|
1694
|
adamc@1220
|
1695 val expIn = expIn rv [] rvOf
|
adamc@1220
|
1696 in
|
adamc@1220
|
1697 case #Where r of
|
adamc@1220
|
1698 NONE => p
|
adamc@1220
|
1699 | SOME e =>
|
adamc@1220
|
1700 case expIn (e, rvN) of
|
adamc@1220
|
1701 (inr p', _) => And (p, p')
|
adamc@1220
|
1702 | _ => p
|
adamc@1220
|
1703 end
|
adamc@1227
|
1704 | _ => default ()
|
adamc@1216
|
1705 end
|
adamc@1200
|
1706
|
adamc@1221
|
1707 fun deleteProp rvN rv e =
|
adamc@1221
|
1708 let
|
adamc@1221
|
1709 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1221
|
1710 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1221
|
1711 Unknown)
|
adamc@1221
|
1712 in
|
adamc@1221
|
1713 case parse query e of
|
adamc@1227
|
1714 SOME (Query1 r) =>
|
adamc@1221
|
1715 let
|
adamc@1221
|
1716 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1221
|
1717 let
|
adamc@1221
|
1718 val (rvN, e) = rv rvN
|
adamc@1221
|
1719 in
|
adamc@1221
|
1720 ((v, e), rvN)
|
adamc@1221
|
1721 end) rvN (#From r)
|
adamc@1221
|
1722
|
adamc@1221
|
1723 fun rvOf v =
|
adamc@1221
|
1724 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1221
|
1725 NONE => raise Fail "Iflow.deleteProp: Bad table variable"
|
adamc@1221
|
1726 | SOME (_, e) => e
|
adamc@1221
|
1727
|
adamc@1221
|
1728 val p =
|
adamc@1221
|
1729 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
|
adamc@1221
|
1730
|
adamc@1221
|
1731 val expIn = expIn rv [] rvOf
|
adamc@1221
|
1732 in
|
adamc@1221
|
1733 And (Reln (Sql "$Old", [rvOf "Old"]),
|
adamc@1221
|
1734 case #Where r of
|
adamc@1221
|
1735 NONE => p
|
adamc@1221
|
1736 | SOME e =>
|
adamc@1221
|
1737 case expIn (e, rvN) of
|
adamc@1221
|
1738 (inr p', _) => And (p, p')
|
adamc@1221
|
1739 | _ => p)
|
adamc@1221
|
1740 end
|
adamc@1227
|
1741 | _ => default ()
|
adamc@1221
|
1742 end
|
adamc@1221
|
1743
|
adamc@1223
|
1744 fun updateProp rvN rv e =
|
adamc@1223
|
1745 let
|
adamc@1223
|
1746 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1223
|
1747 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1223
|
1748 Unknown)
|
adamc@1223
|
1749 in
|
adamc@1223
|
1750 case parse query e of
|
adamc@1227
|
1751 SOME (Query1 r) =>
|
adamc@1223
|
1752 let
|
adamc@1223
|
1753 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1223
|
1754 let
|
adamc@1223
|
1755 val (rvN, e) = rv rvN
|
adamc@1223
|
1756 in
|
adamc@1223
|
1757 ((v, e), rvN)
|
adamc@1223
|
1758 end) rvN (#From r)
|
adamc@1223
|
1759
|
adamc@1223
|
1760 fun rvOf v =
|
adamc@1223
|
1761 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1223
|
1762 NONE => raise Fail "Iflow.insertProp: Bad table variable"
|
adamc@1223
|
1763 | SOME (_, e) => e
|
adamc@1223
|
1764
|
adamc@1223
|
1765 val p =
|
adamc@1223
|
1766 foldl (fn ((t, v), p) =>
|
adamc@1223
|
1767 let
|
adamc@1223
|
1768 val t =
|
adamc@1223
|
1769 case v of
|
adamc@1223
|
1770 "New" => "$New"
|
adamc@1223
|
1771 | _ => t
|
adamc@1223
|
1772 in
|
adamc@1223
|
1773 And (p, Reln (Sql t, [rvOf v]))
|
adamc@1223
|
1774 end) True (#From r)
|
adamc@1223
|
1775
|
adamc@1223
|
1776 val expIn = expIn rv [] rvOf
|
adamc@1223
|
1777 in
|
adamc@1223
|
1778 And (Reln (Sql "$Old", [rvOf "Old"]),
|
adamc@1223
|
1779 case #Where r of
|
adamc@1223
|
1780 NONE => p
|
adamc@1223
|
1781 | SOME e =>
|
adamc@1223
|
1782 case expIn (e, rvN) of
|
adamc@1223
|
1783 (inr p', _) => And (p, p')
|
adamc@1223
|
1784 | _ => p)
|
adamc@1223
|
1785 end
|
adamc@1227
|
1786 | _ => default ()
|
adamc@1223
|
1787 end
|
adamc@1223
|
1788
|
adamc@1211
|
1789 fun evalPat env e (pt, _) =
|
adamc@1211
|
1790 case pt of
|
adamc@1211
|
1791 PWild => (env, True)
|
adamc@1211
|
1792 | PVar _ => (e :: env, True)
|
adamc@1211
|
1793 | PPrim _ => (env, True)
|
adamc@1215
|
1794 | PCon (_, pc, NONE) => (env, Reln (PCon0 (patCon pc), [e]))
|
adamc@1211
|
1795 | PCon (_, pc, SOME pt) =>
|
adamc@1211
|
1796 let
|
adamc@1215
|
1797 val (env, p) = evalPat env (Func (UnCon (patCon pc), [e])) pt
|
adamc@1211
|
1798 in
|
adamc@1215
|
1799 (env, And (p, Reln (PCon1 (patCon pc), [e])))
|
adamc@1211
|
1800 end
|
adamc@1211
|
1801 | PRecord xpts =>
|
adamc@1211
|
1802 foldl (fn ((x, pt, _), (env, p)) =>
|
adamc@1211
|
1803 let
|
adamc@1211
|
1804 val (env, p') = evalPat env (Proj (e, x)) pt
|
adamc@1211
|
1805 in
|
adamc@1211
|
1806 (env, And (p', p))
|
adamc@1211
|
1807 end) (env, True) xpts
|
adamc@1215
|
1808 | PNone _ => (env, Reln (PCon0 "None", [e]))
|
adamc@1211
|
1809 | PSome (_, pt) =>
|
adamc@1211
|
1810 let
|
adamc@1215
|
1811 val (env, p) = evalPat env (Func (UnCon "Some", [e])) pt
|
adamc@1211
|
1812 in
|
adamc@1215
|
1813 (env, And (p, Reln (PCon1 "Some", [e])))
|
adamc@1211
|
1814 end
|
adamc@1211
|
1815
|
adamc@1211
|
1816 fun peq (p1, p2) =
|
adamc@1211
|
1817 case (p1, p2) of
|
adamc@1211
|
1818 (True, True) => true
|
adamc@1211
|
1819 | (False, False) => true
|
adamc@1211
|
1820 | (Unknown, Unknown) => true
|
adamc@1211
|
1821 | (And (x1, y1), And (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
|
adamc@1211
|
1822 | (Or (x1, y1), Or (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
|
adamc@1211
|
1823 | (Reln (r1, es1), Reln (r2, es2)) => r1 = r2 andalso ListPair.allEq eeq (es1, es2)
|
adamc@1212
|
1824 | (Cond (e1, p1), Cond (e2, p2)) => eeq (e1, e2) andalso peq (p1, p2)
|
adamc@1211
|
1825 | _ => false
|
adamc@1211
|
1826
|
adamc@1211
|
1827 fun removeRedundant p1 =
|
adamc@1211
|
1828 let
|
adamc@1211
|
1829 fun rr p2 =
|
adamc@1211
|
1830 if peq (p1, p2) then
|
adamc@1211
|
1831 True
|
adamc@1211
|
1832 else
|
adamc@1211
|
1833 case p2 of
|
adamc@1211
|
1834 And (x, y) => And (rr x, rr y)
|
adamc@1211
|
1835 | Or (x, y) => Or (rr x, rr y)
|
adamc@1211
|
1836 | _ => p2
|
adamc@1211
|
1837 in
|
adamc@1211
|
1838 rr
|
adamc@1211
|
1839 end
|
adamc@1211
|
1840
|
adamc@1218
|
1841 datatype cflow = Case | Where
|
adamc@1218
|
1842 datatype flow = Data | Control of cflow
|
adamc@1218
|
1843 type check = ErrorMsg.span * exp * prop
|
adamc@1221
|
1844 type dml = ErrorMsg.span * prop
|
adamc@1218
|
1845
|
adamc@1217
|
1846 structure St :> sig
|
adamc@1217
|
1847 type t
|
adamc@1217
|
1848 val create : {Var : int,
|
adamc@1217
|
1849 Ambient : prop} -> t
|
adamc@1217
|
1850
|
adamc@1217
|
1851 val curVar : t -> int
|
adamc@1217
|
1852 val nextVar : t -> t * int
|
adamc@1217
|
1853
|
adamc@1217
|
1854 val ambient : t -> prop
|
adamc@1217
|
1855 val setAmbient : t * prop -> t
|
adamc@1217
|
1856
|
adamc@1218
|
1857 val paths : t -> (check * cflow) list
|
adamc@1218
|
1858 val addPath : t * (check * cflow) -> t
|
adamc@1218
|
1859 val addPaths : t * (check * cflow) list -> t
|
adamc@1218
|
1860 val clearPaths : t -> t
|
adamc@1218
|
1861 val setPaths : t * (check * cflow) list -> t
|
adamc@1217
|
1862
|
adamc@1218
|
1863 val sent : t -> (check * flow) list
|
adamc@1218
|
1864 val addSent : t * (check * flow) -> t
|
adamc@1218
|
1865 val setSent : t * (check * flow) list -> t
|
adamc@1220
|
1866
|
adamc@1221
|
1867 val inserted : t -> dml list
|
adamc@1221
|
1868 val addInsert : t * dml -> t
|
adamc@1221
|
1869
|
adamc@1221
|
1870 val deleted : t -> dml list
|
adamc@1221
|
1871 val addDelete : t * dml -> t
|
adamc@1223
|
1872
|
adamc@1223
|
1873 val updated : t -> dml list
|
adamc@1223
|
1874 val addUpdate : t * dml -> t
|
adamc@1217
|
1875 end = struct
|
adamc@1217
|
1876
|
adamc@1217
|
1877 type t = {Var : int,
|
adamc@1217
|
1878 Ambient : prop,
|
adamc@1218
|
1879 Path : (check * cflow) list,
|
adamc@1220
|
1880 Sent : (check * flow) list,
|
adamc@1221
|
1881 Insert : dml list,
|
adamc@1223
|
1882 Delete : dml list,
|
adamc@1223
|
1883 Update : dml list}
|
adamc@1217
|
1884
|
adamc@1217
|
1885 fun create {Var = v, Ambient = p} = {Var = v,
|
adamc@1217
|
1886 Ambient = p,
|
adamc@1217
|
1887 Path = [],
|
adamc@1220
|
1888 Sent = [],
|
adamc@1221
|
1889 Insert = [],
|
adamc@1223
|
1890 Delete = [],
|
adamc@1223
|
1891 Update = []}
|
adamc@1217
|
1892
|
adamc@1217
|
1893 fun curVar (t : t) = #Var t
|
adamc@1217
|
1894 fun nextVar (t : t) = ({Var = #Var t + 1,
|
adamc@1217
|
1895 Ambient = #Ambient t,
|
adamc@1217
|
1896 Path = #Path t,
|
adamc@1220
|
1897 Sent = #Sent t,
|
adamc@1221
|
1898 Insert = #Insert t,
|
adamc@1223
|
1899 Delete = #Delete t,
|
adamc@1223
|
1900 Update = #Update t}, #Var t)
|
adamc@1217
|
1901
|
adamc@1217
|
1902 fun ambient (t : t) = #Ambient t
|
adamc@1217
|
1903 fun setAmbient (t : t, p) = {Var = #Var t,
|
adamc@1217
|
1904 Ambient = p,
|
adamc@1217
|
1905 Path = #Path t,
|
adamc@1220
|
1906 Sent = #Sent t,
|
adamc@1221
|
1907 Insert = #Insert t,
|
adamc@1223
|
1908 Delete = #Delete t,
|
adamc@1223
|
1909 Update = #Update t}
|
adamc@1217
|
1910
|
adamc@1218
|
1911 fun paths (t : t) = #Path t
|
adamc@1217
|
1912 fun addPath (t : t, c) = {Var = #Var t,
|
adamc@1217
|
1913 Ambient = #Ambient t,
|
adamc@1217
|
1914 Path = c :: #Path t,
|
adamc@1220
|
1915 Sent = #Sent t,
|
adamc@1221
|
1916 Insert = #Insert t,
|
adamc@1223
|
1917 Delete = #Delete t,
|
adamc@1223
|
1918 Update = #Update t}
|
adamc@1218
|
1919 fun addPaths (t : t, cs) = {Var = #Var t,
|
adamc@1218
|
1920 Ambient = #Ambient t,
|
adamc@1218
|
1921 Path = cs @ #Path t,
|
adamc@1220
|
1922 Sent = #Sent t,
|
adamc@1221
|
1923 Insert = #Insert t,
|
adamc@1223
|
1924 Delete = #Delete t,
|
adamc@1223
|
1925 Update = #Update t}
|
adamc@1218
|
1926 fun clearPaths (t : t) = {Var = #Var t,
|
adamc@1218
|
1927 Ambient = #Ambient t,
|
adamc@1218
|
1928 Path = [],
|
adamc@1220
|
1929 Sent = #Sent t,
|
adamc@1221
|
1930 Insert = #Insert t,
|
adamc@1223
|
1931 Delete = #Delete t,
|
adamc@1223
|
1932 Update = #Update t}
|
adamc@1218
|
1933 fun setPaths (t : t, cs) = {Var = #Var t,
|
adamc@1218
|
1934 Ambient = #Ambient t,
|
adamc@1218
|
1935 Path = cs,
|
adamc@1220
|
1936 Sent = #Sent t,
|
adamc@1221
|
1937 Insert = #Insert t,
|
adamc@1223
|
1938 Delete = #Delete t,
|
adamc@1223
|
1939 Update = #Update t}
|
adamc@1217
|
1940
|
adamc@1217
|
1941 fun sent (t : t) = #Sent t
|
adamc@1217
|
1942 fun addSent (t : t, c) = {Var = #Var t,
|
adamc@1217
|
1943 Ambient = #Ambient t,
|
adamc@1217
|
1944 Path = #Path t,
|
adamc@1220
|
1945 Sent = c :: #Sent t,
|
adamc@1221
|
1946 Insert = #Insert t,
|
adamc@1223
|
1947 Delete = #Delete t,
|
adamc@1223
|
1948 Update = #Update t}
|
adamc@1217
|
1949 fun setSent (t : t, cs) = {Var = #Var t,
|
adamc@1217
|
1950 Ambient = #Ambient t,
|
adamc@1217
|
1951 Path = #Path t,
|
adamc@1220
|
1952 Sent = cs,
|
adamc@1221
|
1953 Insert = #Insert t,
|
adamc@1223
|
1954 Delete = #Delete t,
|
adamc@1223
|
1955 Update = #Update t}
|
adamc@1220
|
1956
|
adamc@1220
|
1957 fun inserted (t : t) = #Insert t
|
adamc@1220
|
1958 fun addInsert (t : t, c) = {Var = #Var t,
|
adamc@1220
|
1959 Ambient = #Ambient t,
|
adamc@1220
|
1960 Path = #Path t,
|
adamc@1220
|
1961 Sent = #Sent t,
|
adamc@1221
|
1962 Insert = c :: #Insert t,
|
adamc@1223
|
1963 Delete = #Delete t,
|
adamc@1223
|
1964 Update = #Update t}
|
adamc@1221
|
1965
|
adamc@1221
|
1966 fun deleted (t : t) = #Delete t
|
adamc@1221
|
1967 fun addDelete (t : t, c) = {Var = #Var t,
|
adamc@1221
|
1968 Ambient = #Ambient t,
|
adamc@1221
|
1969 Path = #Path t,
|
adamc@1221
|
1970 Sent = #Sent t,
|
adamc@1221
|
1971 Insert = #Insert t,
|
adamc@1223
|
1972 Delete = c :: #Delete t,
|
adamc@1223
|
1973 Update = #Update t}
|
adamc@1223
|
1974
|
adamc@1223
|
1975 fun updated (t : t) = #Update t
|
adamc@1223
|
1976 fun addUpdate (t : t, c) = {Var = #Var t,
|
adamc@1223
|
1977 Ambient = #Ambient t,
|
adamc@1223
|
1978 Path = #Path t,
|
adamc@1223
|
1979 Sent = #Sent t,
|
adamc@1223
|
1980 Insert = #Insert t,
|
adamc@1223
|
1981 Delete = #Delete t,
|
adamc@1223
|
1982 Update = c :: #Update t}
|
adamc@1217
|
1983
|
adamc@1217
|
1984 end
|
adamc@1217
|
1985
|
adamc@1217
|
1986 fun evalExp env (e as (_, loc), st) =
|
adamc@1200
|
1987 let
|
adamc@1200
|
1988 fun default () =
|
adamc@1217
|
1989 let
|
adamc@1217
|
1990 val (st, nv) = St.nextVar st
|
adamc@1217
|
1991 in
|
adamc@1217
|
1992 (Var nv, st)
|
adamc@1217
|
1993 end
|
adamc@1200
|
1994
|
adamc@1217
|
1995 fun addSent (p, e, st) =
|
adamc@1218
|
1996 let
|
adamc@1218
|
1997 val st = if isKnown e then
|
adamc@1218
|
1998 st
|
adamc@1218
|
1999 else
|
adamc@1218
|
2000 St.addSent (st, ((loc, e, p), Data))
|
adamc@1218
|
2001
|
adamc@1218
|
2002 val st = foldl (fn ((c, fl), st) => St.addSent (st, (c, Control fl))) st (St.paths st)
|
adamc@1218
|
2003 in
|
adamc@1218
|
2004 St.clearPaths st
|
adamc@1218
|
2005 end
|
adamc@1200
|
2006 in
|
adamc@1200
|
2007 case #1 e of
|
adamc@1200
|
2008 EPrim p => (Const p, st)
|
adamc@1200
|
2009 | ERel n => (List.nth (env, n), st)
|
adamc@1200
|
2010 | ENamed _ => default ()
|
adamc@1215
|
2011 | ECon (_, pc, NONE) => (Func (DtCon0 (patCon pc), []), st)
|
adamc@1200
|
2012 | ECon (_, pc, SOME e) =>
|
adamc@1200
|
2013 let
|
adamc@1200
|
2014 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
2015 in
|
adamc@1215
|
2016 (Func (DtCon1 (patCon pc), [e]), st)
|
adamc@1200
|
2017 end
|
adamc@1215
|
2018 | ENone _ => (Func (DtCon0 "None", []), st)
|
adamc@1200
|
2019 | ESome (_, e) =>
|
adamc@1200
|
2020 let
|
adamc@1200
|
2021 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
2022 in
|
adamc@1215
|
2023 (Func (DtCon1 "Some", [e]), st)
|
adamc@1200
|
2024 end
|
adamc@1200
|
2025 | EFfi _ => default ()
|
adamc@1213
|
2026
|
adamc@1200
|
2027 | EFfiApp (m, s, es) =>
|
adamc@1200
|
2028 if m = "Basis" andalso SS.member (writers, s) then
|
adamc@1200
|
2029 let
|
adamc@1200
|
2030 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
2031 in
|
adamc@1217
|
2032 (Recd [], foldl (fn (e, st) => addSent (St.ambient st, e, st)) st es)
|
adamc@1200
|
2033 end
|
adamc@1200
|
2034 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
|
adamc@1200
|
2035 default ()
|
adamc@1200
|
2036 else
|
adamc@1200
|
2037 let
|
adamc@1200
|
2038 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
2039 in
|
adamc@1215
|
2040 (Func (Other (m ^ "." ^ s), es), st)
|
adamc@1200
|
2041 end
|
adamc@1213
|
2042
|
adamc@1213
|
2043 | EApp (e1, e2) =>
|
adamc@1213
|
2044 let
|
adamc@1213
|
2045 val (e1, st) = evalExp env (e1, st)
|
adamc@1213
|
2046 in
|
adamc@1213
|
2047 case e1 of
|
adamc@1213
|
2048 Finish => (Finish, st)
|
adamc@1213
|
2049 | _ => default ()
|
adamc@1213
|
2050 end
|
adamc@1213
|
2051
|
adamc@1200
|
2052 | EAbs _ => default ()
|
adamc@1200
|
2053 | EUnop (s, e1) =>
|
adamc@1200
|
2054 let
|
adamc@1200
|
2055 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
2056 in
|
adamc@1215
|
2057 (Func (Other s, [e1]), st)
|
adamc@1200
|
2058 end
|
adamc@1200
|
2059 | EBinop (s, e1, e2) =>
|
adamc@1200
|
2060 let
|
adamc@1200
|
2061 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
2062 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
2063 in
|
adamc@1215
|
2064 (Func (Other s, [e1, e2]), st)
|
adamc@1200
|
2065 end
|
adamc@1200
|
2066 | ERecord xets =>
|
adamc@1200
|
2067 let
|
adamc@1200
|
2068 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
|
adamc@1200
|
2069 let
|
adamc@1200
|
2070 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
2071 in
|
adamc@1200
|
2072 ((x, e), st)
|
adamc@1200
|
2073 end) st xets
|
adamc@1200
|
2074 in
|
adamc@1200
|
2075 (Recd xes, st)
|
adamc@1200
|
2076 end
|
adamc@1200
|
2077 | EField (e, s) =>
|
adamc@1200
|
2078 let
|
adamc@1200
|
2079 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
2080 in
|
adamc@1200
|
2081 (Proj (e, s), st)
|
adamc@1200
|
2082 end
|
adamc@1218
|
2083 | ECase (e, pes, {result = res, ...}) =>
|
adamc@1211
|
2084 let
|
adamc@1211
|
2085 val (e, st) = evalExp env (e, st)
|
adamc@1217
|
2086 val (st, r) = St.nextVar st
|
adamc@1217
|
2087 val orig = St.ambient st
|
adamc@1218
|
2088 val origPaths = St.paths st
|
adamc@1211
|
2089
|
adamc@1218
|
2090 val st = St.addPath (st, ((loc, e, orig), Case))
|
adamc@1218
|
2091
|
adamc@1218
|
2092 val (st, paths) =
|
adamc@1218
|
2093 foldl (fn ((pt, pe), (st, paths)) =>
|
adamc@1218
|
2094 let
|
adamc@1218
|
2095 val (env, pp) = evalPat env e pt
|
adamc@1218
|
2096 val (pe, st') = evalExp env (pe, St.setAmbient (st, And (orig, pp)))
|
adamc@1218
|
2097
|
adamc@1218
|
2098 val this = And (removeRedundant orig (St.ambient st'),
|
adamc@1218
|
2099 Reln (Eq, [Var r, pe]))
|
adamc@1218
|
2100 in
|
adamc@1218
|
2101 (St.setPaths (St.setAmbient (st', Or (St.ambient st, this)), origPaths),
|
adamc@1218
|
2102 St.paths st' @ paths)
|
adamc@1218
|
2103 end) (St.setAmbient (st, False), []) pes
|
adamc@1218
|
2104
|
adamc@1218
|
2105 val st = case #1 res of
|
adamc@1218
|
2106 TRecord [] => St.setPaths (st, origPaths)
|
adamc@1218
|
2107 | _ => St.setPaths (st, paths)
|
adamc@1211
|
2108 in
|
adamc@1217
|
2109 (Var r, St.setAmbient (st, And (orig, St.ambient st)))
|
adamc@1211
|
2110 end
|
adamc@1200
|
2111 | EStrcat (e1, e2) =>
|
adamc@1200
|
2112 let
|
adamc@1200
|
2113 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
2114 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
2115 in
|
adamc@1215
|
2116 (Func (Other "cat", [e1, e2]), st)
|
adamc@1200
|
2117 end
|
adamc@1200
|
2118 | EError _ => (Finish, st)
|
adamc@1200
|
2119 | EReturnBlob {blob = b, mimeType = m, ...} =>
|
adamc@1200
|
2120 let
|
adamc@1200
|
2121 val (b, st) = evalExp env (b, st)
|
adamc@1200
|
2122 val (m, st) = evalExp env (m, st)
|
adamc@1200
|
2123 in
|
adamc@1217
|
2124 (Finish, addSent (St.ambient st, b, addSent (St.ambient st, m, st)))
|
adamc@1200
|
2125 end
|
adamc@1200
|
2126 | ERedirect (e, _) =>
|
adamc@1200
|
2127 let
|
adamc@1200
|
2128 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
2129 in
|
adamc@1217
|
2130 (Finish, addSent (St.ambient st, e, st))
|
adamc@1200
|
2131 end
|
adamc@1200
|
2132 | EWrite e =>
|
adamc@1200
|
2133 let
|
adamc@1200
|
2134 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
2135 in
|
adamc@1217
|
2136 (Recd [], addSent (St.ambient st, e, st))
|
adamc@1200
|
2137 end
|
adamc@1200
|
2138 | ESeq (e1, e2) =>
|
adamc@1200
|
2139 let
|
adamc@1200
|
2140 val (_, st) = evalExp env (e1, st)
|
adamc@1200
|
2141 in
|
adamc@1200
|
2142 evalExp env (e2, st)
|
adamc@1200
|
2143 end
|
adamc@1200
|
2144 | ELet (_, _, e1, e2) =>
|
adamc@1200
|
2145 let
|
adamc@1200
|
2146 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
2147 in
|
adamc@1200
|
2148 evalExp (e1 :: env) (e2, st)
|
adamc@1200
|
2149 end
|
adamc@1200
|
2150 | EClosure (n, es) =>
|
adamc@1200
|
2151 let
|
adamc@1200
|
2152 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
2153 in
|
adamc@1215
|
2154 (Func (Other ("Cl" ^ Int.toString n), es), st)
|
adamc@1200
|
2155 end
|
adamc@1200
|
2156
|
adamc@1200
|
2157 | EQuery {query = q, body = b, initial = i, ...} =>
|
adamc@1200
|
2158 let
|
adamc@1200
|
2159 val (_, st) = evalExp env (q, st)
|
adamc@1200
|
2160 val (i, st) = evalExp env (i, st)
|
adamc@1200
|
2161
|
adamc@1217
|
2162 val (st', r) = St.nextVar st
|
adamc@1217
|
2163 val (st', acc) = St.nextVar st'
|
adamc@1200
|
2164
|
adamc@1200
|
2165 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
|
adamc@1200
|
2166
|
adamc@1227
|
2167 val (st', qp, used, _) =
|
adamc@1214
|
2168 queryProp env
|
adamc@1217
|
2169 st' (fn st' =>
|
adamc@1217
|
2170 let
|
adamc@1217
|
2171 val (st', rv) = St.nextVar st'
|
adamc@1217
|
2172 in
|
adamc@1217
|
2173 (st', Var rv)
|
adamc@1217
|
2174 end)
|
adamc@1214
|
2175 (AllCols (Var r)) q
|
adamc@1200
|
2176
|
adamc@1217
|
2177 val p' = And (qp, St.ambient st')
|
adamc@1200
|
2178
|
adamc@1217
|
2179 val (st, res) = if varInP acc (St.ambient st') then
|
adamc@1217
|
2180 let
|
adamc@1217
|
2181 val (st, r) = St.nextVar st
|
adamc@1217
|
2182 in
|
adamc@1217
|
2183 (st, Var r)
|
adamc@1217
|
2184 end
|
adamc@1217
|
2185 else
|
adamc@1217
|
2186 let
|
adamc@1217
|
2187 val (st, out) = St.nextVar st'
|
adamc@1217
|
2188
|
adamc@1217
|
2189 val p = Or (Reln (Eq, [Var out, i]),
|
adamc@1217
|
2190 And (Reln (Eq, [Var out, b]),
|
adamc@1217
|
2191 p'))
|
adamc@1217
|
2192 in
|
adamc@1217
|
2193 (St.setAmbient (st, p), Var out)
|
adamc@1217
|
2194 end
|
adamc@1210
|
2195
|
adamc@1218
|
2196 val sent = map (fn ((loc, e, p), fl) => ((loc, e, And (qp, p)), fl)) (St.sent st')
|
adamc@1212
|
2197
|
adamc@1227
|
2198 val paths = map (fn (p'', e) => ((loc, e, And (p', p'')), Where)) used
|
adamc@1200
|
2199 in
|
adamc@1218
|
2200 (res, St.addPaths (St.setSent (st, sent), paths))
|
adamc@1200
|
2201 end
|
adamc@1220
|
2202 | EDml e =>
|
adamc@1220
|
2203 (case parse dml e of
|
adamc@1220
|
2204 NONE => (print ("Warning: Information flow checker can't parse DML command at "
|
adamc@1220
|
2205 ^ ErrorMsg.spanToString loc ^ "\n");
|
adamc@1220
|
2206 default ())
|
adamc@1220
|
2207 | SOME d =>
|
adamc@1220
|
2208 case d of
|
adamc@1220
|
2209 Insert (tab, es) =>
|
adamc@1220
|
2210 let
|
adamc@1220
|
2211 val (st, new) = St.nextVar st
|
adamc@1220
|
2212
|
adamc@1220
|
2213 fun rv st =
|
adamc@1220
|
2214 let
|
adamc@1220
|
2215 val (st, n) = St.nextVar st
|
adamc@1220
|
2216 in
|
adamc@1220
|
2217 (st, Var n)
|
adamc@1220
|
2218 end
|
adamc@1220
|
2219
|
adamc@1223
|
2220 val expIn = expIn rv env (fn _ => raise Fail "Iflow.evalExp: Bad field expression in INSERT")
|
adamc@1220
|
2221
|
adamc@1220
|
2222 val (es, st) = ListUtil.foldlMap
|
adamc@1220
|
2223 (fn ((x, e), st) =>
|
adamc@1220
|
2224 let
|
adamc@1220
|
2225 val (e, st) = case expIn (e, st) of
|
adamc@1220
|
2226 (inl e, st) => (e, st)
|
adamc@1220
|
2227 | (inr _, _) => raise Fail
|
adamc@1220
|
2228 ("Iflow.evalExp: Selecting "
|
adamc@1220
|
2229 ^ "boolean expression")
|
adamc@1220
|
2230 in
|
adamc@1220
|
2231 ((x, e), st)
|
adamc@1220
|
2232 end)
|
adamc@1220
|
2233 st es
|
adamc@1220
|
2234 in
|
adamc@1220
|
2235 (Recd [], St.addInsert (st, (loc, And (St.ambient st,
|
adamc@1220
|
2236 Reln (Sql "$New", [Recd es])))))
|
adamc@1221
|
2237 end
|
adamc@1221
|
2238 | Delete (tab, e) =>
|
adamc@1221
|
2239 let
|
adamc@1221
|
2240 val (st, old) = St.nextVar st
|
adamc@1221
|
2241
|
adamc@1221
|
2242 fun rv st =
|
adamc@1221
|
2243 let
|
adamc@1221
|
2244 val (st, n) = St.nextVar st
|
adamc@1221
|
2245 in
|
adamc@1221
|
2246 (st, Var n)
|
adamc@1221
|
2247 end
|
adamc@1221
|
2248
|
adamc@1221
|
2249 val expIn = expIn rv env (fn "T" => Var old
|
adamc@1221
|
2250 | _ => raise Fail "Iflow.evalExp: Bad field expression in DELETE")
|
adamc@1221
|
2251
|
adamc@1221
|
2252 val (p, st) = case expIn (e, st) of
|
adamc@1221
|
2253 (inl e, _) => raise Fail "Iflow.evalExp: DELETE with non-boolean"
|
adamc@1221
|
2254 | (inr p, st) => (p, st)
|
adamc@1221
|
2255
|
adamc@1221
|
2256 val p = And (p,
|
adamc@1221
|
2257 And (Reln (Sql "$Old", [Var old]),
|
adamc@1221
|
2258 Reln (Sql tab, [Var old])))
|
adamc@1221
|
2259 in
|
adamc@1221
|
2260 (Recd [], St.addDelete (st, (loc, And (St.ambient st, p))))
|
adamc@1223
|
2261 end
|
adamc@1223
|
2262 | Update (tab, fs, e) =>
|
adamc@1223
|
2263 let
|
adamc@1223
|
2264 val (st, new) = St.nextVar st
|
adamc@1223
|
2265 val (st, old) = St.nextVar st
|
adamc@1223
|
2266
|
adamc@1223
|
2267 fun rv st =
|
adamc@1223
|
2268 let
|
adamc@1223
|
2269 val (st, n) = St.nextVar st
|
adamc@1223
|
2270 in
|
adamc@1223
|
2271 (st, Var n)
|
adamc@1223
|
2272 end
|
adamc@1223
|
2273
|
adamc@1223
|
2274 val expIn = expIn rv env (fn "T" => Var old
|
adamc@1223
|
2275 | _ => raise Fail "Iflow.evalExp: Bad field expression in UPDATE")
|
adamc@1223
|
2276
|
adamc@1223
|
2277 val (fs, st) = ListUtil.foldlMap
|
adamc@1223
|
2278 (fn ((x, e), st) =>
|
adamc@1223
|
2279 let
|
adamc@1223
|
2280 val (e, st) = case expIn (e, st) of
|
adamc@1223
|
2281 (inl e, st) => (e, st)
|
adamc@1223
|
2282 | (inr _, _) => raise Fail
|
adamc@1223
|
2283 ("Iflow.evalExp: Selecting "
|
adamc@1223
|
2284 ^ "boolean expression")
|
adamc@1223
|
2285 in
|
adamc@1223
|
2286 ((x, e), st)
|
adamc@1223
|
2287 end)
|
adamc@1223
|
2288 st fs
|
adamc@1223
|
2289
|
adamc@1226
|
2290 val fs' = case SM.find (!tabs, tab) of
|
adamc@1224
|
2291 NONE => raise Fail "Iflow.evalExp: Updating unknown table"
|
adamc@1226
|
2292 | SOME (fs', _) => fs'
|
adamc@1224
|
2293
|
adamc@1224
|
2294 val fs = foldl (fn (f, fs) =>
|
adamc@1224
|
2295 if List.exists (fn (f', _) => f' = f) fs then
|
adamc@1224
|
2296 fs
|
adamc@1224
|
2297 else
|
adamc@1224
|
2298 (f, Proj (Var old, f)) :: fs) fs fs'
|
adamc@1224
|
2299
|
adamc@1223
|
2300 val (p, st) = case expIn (e, st) of
|
adamc@1223
|
2301 (inl e, _) => raise Fail "Iflow.evalExp: UPDATE with non-boolean"
|
adamc@1223
|
2302 | (inr p, st) => (p, st)
|
adamc@1223
|
2303
|
adamc@1223
|
2304 val p = And (p,
|
adamc@1223
|
2305 And (Reln (Sql "$New", [Recd fs]),
|
adamc@1223
|
2306 And (Reln (Sql "$Old", [Var old]),
|
adamc@1223
|
2307 Reln (Sql tab, [Var old]))))
|
adamc@1223
|
2308 in
|
adamc@1223
|
2309 (Recd [], St.addUpdate (st, (loc, And (St.ambient st, p))))
|
adamc@1220
|
2310 end)
|
adamc@1220
|
2311
|
adamc@1200
|
2312 | ENextval _ => default ()
|
adamc@1200
|
2313 | ESetval _ => default ()
|
adamc@1200
|
2314
|
adamc@1213
|
2315 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
|
adamc@1217
|
2316 let
|
adamc@1217
|
2317 val (st, nv) = St.nextVar st
|
adamc@1217
|
2318 in
|
adamc@1217
|
2319 (Var nv, St.setAmbient (st, And (St.ambient st, Reln (Known, [Var nv]))))
|
adamc@1217
|
2320 end
|
adamc@1213
|
2321
|
adamc@1200
|
2322 | EUnurlify _ => default ()
|
adamc@1200
|
2323 | EJavaScript _ => default ()
|
adamc@1200
|
2324 | ESignalReturn _ => default ()
|
adamc@1200
|
2325 | ESignalBind _ => default ()
|
adamc@1200
|
2326 | ESignalSource _ => default ()
|
adamc@1200
|
2327 | EServerCall _ => default ()
|
adamc@1200
|
2328 | ERecv _ => default ()
|
adamc@1200
|
2329 | ESleep _ => default ()
|
adamc@1200
|
2330 | ESpawn _ => default ()
|
adamc@1200
|
2331 end
|
adamc@1200
|
2332
|
adamc@1200
|
2333 fun check file =
|
adamc@1200
|
2334 let
|
adamc@1213
|
2335 val file = MonoReduce.reduce file
|
adamc@1213
|
2336 val file = MonoOpt.optimize file
|
adamc@1213
|
2337 val file = Fuse.fuse file
|
adamc@1213
|
2338 val file = MonoOpt.optimize file
|
adamc@1216
|
2339 val file = MonoShake.shake file
|
adamc@1213
|
2340 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
|
adamc@1213
|
2341
|
adamc@1207
|
2342 val exptd = foldl (fn ((d, _), exptd) =>
|
adamc@1207
|
2343 case d of
|
adamc@1207
|
2344 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
|
adamc@1207
|
2345 | _ => exptd) IS.empty file
|
adamc@1207
|
2346
|
adamc@1223
|
2347 fun decl ((d, _), (vals, inserts, deletes, updates, client, insert, delete, update)) =
|
adamc@1200
|
2348 case d of
|
adamc@1226
|
2349 DTable (tab, fs, pk, _) =>
|
adamc@1226
|
2350 let
|
adamc@1226
|
2351 val ks =
|
adamc@1226
|
2352 case #1 pk of
|
adamc@1226
|
2353 EPrim (Prim.String s) =>
|
adamc@1226
|
2354 (case String.tokens (fn ch => ch = #"," orelse ch = #" ") s of
|
adamc@1226
|
2355 [] => []
|
adamc@1226
|
2356 | pk => [pk])
|
adamc@1226
|
2357 | _ => []
|
adamc@1226
|
2358 in
|
adamc@1226
|
2359 if size tab >= 3 then
|
adamc@1226
|
2360 (tabs := SM.insert (!tabs, String.extract (tab, 3, NONE),
|
adamc@1226
|
2361 (map #1 fs,
|
adamc@1226
|
2362 map (map (fn s => str (Char.toUpper (String.sub (s, 3)))
|
adamc@1226
|
2363 ^ String.extract (s, 4, NONE))) ks));
|
adamc@1226
|
2364 (vals, inserts, deletes, updates, client, insert, delete, update))
|
adamc@1226
|
2365 else
|
adamc@1226
|
2366 raise Fail "Table name does not begin with uw_"
|
adamc@1226
|
2367 end
|
adamc@1224
|
2368 | DVal (_, n, _, e, _) =>
|
adamc@1200
|
2369 let
|
adamc@1207
|
2370 val isExptd = IS.member (exptd, n)
|
adamc@1207
|
2371
|
adamc@1207
|
2372 fun deAbs (e, env, nv, p) =
|
adamc@1200
|
2373 case #1 e of
|
adamc@1207
|
2374 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1,
|
adamc@1207
|
2375 if isExptd then
|
adamc@1207
|
2376 And (p, Reln (Known, [Var nv]))
|
adamc@1207
|
2377 else
|
adamc@1207
|
2378 p)
|
adamc@1207
|
2379 | _ => (e, env, nv, p)
|
adamc@1200
|
2380
|
adamc@1207
|
2381 val (e, env, nv, p) = deAbs (e, [], 1, True)
|
adamc@1200
|
2382
|
adamc@1217
|
2383 val (_, st) = evalExp env (e, St.create {Var = nv,
|
adamc@1217
|
2384 Ambient = p})
|
adamc@1200
|
2385 in
|
adamc@1223
|
2386 (St.sent st @ vals, St.inserted st @ inserts, St.deleted st @ deletes, St.updated st @ updates,
|
adamc@1223
|
2387 client, insert, delete, update)
|
adamc@1200
|
2388 end
|
adamc@1202
|
2389
|
adamc@1220
|
2390 | DPolicy pol =>
|
adamc@1218
|
2391 let
|
adamc@1220
|
2392 fun rv rvN = (rvN + 1, Lvar rvN)
|
adamc@1218
|
2393 in
|
adamc@1220
|
2394 case pol of
|
adamc@1220
|
2395 PolClient e =>
|
adamc@1220
|
2396 let
|
adamc@1227
|
2397 val (_, p, _, outs) = queryProp [] 0 rv SomeCol e
|
adamc@1220
|
2398 in
|
adamc@1223
|
2399 (vals, inserts, deletes, updates, (p, outs) :: client, insert, delete, update)
|
adamc@1220
|
2400 end
|
adamc@1220
|
2401 | PolInsert e =>
|
adamc@1220
|
2402 let
|
adamc@1220
|
2403 val p = insertProp 0 rv e
|
adamc@1220
|
2404 in
|
adamc@1223
|
2405 (vals, inserts, deletes, updates, client, p :: insert, delete, update)
|
adamc@1221
|
2406 end
|
adamc@1221
|
2407 | PolDelete e =>
|
adamc@1221
|
2408 let
|
adamc@1221
|
2409 val p = deleteProp 0 rv e
|
adamc@1221
|
2410 in
|
adamc@1223
|
2411 (vals, inserts, deletes, updates, client, insert, p :: delete, update)
|
adamc@1223
|
2412 end
|
adamc@1223
|
2413 | PolUpdate e =>
|
adamc@1223
|
2414 let
|
adamc@1223
|
2415 val p = updateProp 0 rv e
|
adamc@1223
|
2416 in
|
adamc@1223
|
2417 (vals, inserts, deletes, updates, client, insert, delete, p :: update)
|
adamc@1220
|
2418 end
|
adamc@1218
|
2419 end
|
adamc@1214
|
2420
|
adamc@1223
|
2421 | _ => (vals, inserts, deletes, updates, client, insert, delete, update)
|
adamc@1202
|
2422
|
adamc@1203
|
2423 val () = reset ()
|
adamc@1202
|
2424
|
adamc@1223
|
2425 val (vals, inserts, deletes, updates, client, insert, delete, update) =
|
adamc@1223
|
2426 foldl decl ([], [], [], [], [], [], [], []) file
|
adamc@1220
|
2427
|
adamc@1224
|
2428
|
adamc@1220
|
2429 val decompH = decomp true (fn (e1, e2) => e1 andalso e2 ())
|
adamc@1220
|
2430 val decompG = decomp false (fn (e1, e2) => e1 orelse e2 ())
|
adamc@1221
|
2431
|
adamc@1221
|
2432 fun doDml (cmds, pols) =
|
adamc@1221
|
2433 app (fn (loc, p) =>
|
adamc@1221
|
2434 if decompH p
|
adamc@1221
|
2435 (fn hyps =>
|
adamc@1221
|
2436 List.exists (fn p' =>
|
adamc@1221
|
2437 decompG p'
|
adamc@1221
|
2438 (fn goals => imply (hyps, goals, NONE)))
|
adamc@1221
|
2439 pols) then
|
adamc@1221
|
2440 ()
|
adamc@1221
|
2441 else
|
adamc@1221
|
2442 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
|
adamc@1221
|
2443 Print.preface ("The state satisifies this predicate:", p_prop p))) cmds
|
adamc@1200
|
2444 in
|
adamc@1218
|
2445 app (fn ((loc, e, p), fl) =>
|
adamc@1207
|
2446 let
|
adamc@1213
|
2447 fun doOne e =
|
adamc@1213
|
2448 let
|
adamc@1213
|
2449 val p = And (p, Reln (Eq, [Var 0, e]))
|
adamc@1213
|
2450 in
|
adamc@1220
|
2451 if decompH p
|
adamc@1220
|
2452 (fn hyps =>
|
adamc@1228
|
2453 let
|
adamc@1228
|
2454 val avail = foldl (fn (AReln (Sql tab, _), avail) => SS.add (avail, tab)
|
adamc@1228
|
2455 | (_, avail) => avail) SS.empty hyps
|
adamc@1228
|
2456
|
adamc@1228
|
2457 fun tryCombos (maxLv, pols, g, outs) =
|
adamc@1228
|
2458 case pols of
|
adamc@1228
|
2459 [] =>
|
adamc@1228
|
2460 decompG g
|
adamc@1228
|
2461 (fn goals => imply (hyps, goals, SOME outs))
|
adamc@1228
|
2462 | (g1, outs1) :: pols =>
|
adamc@1228
|
2463 let
|
adamc@1228
|
2464 val g1 = bumpLvarsP (maxLv + 1) g1
|
adamc@1228
|
2465 val outs1 = map (bumpLvars (maxLv + 1)) outs1
|
adamc@1228
|
2466 fun skip () = tryCombos (maxLv, pols, g, outs)
|
adamc@1228
|
2467 in
|
adamc@1228
|
2468 if decompG g1
|
adamc@1228
|
2469 (List.all (fn AReln (Sql tab, _) =>
|
adamc@1228
|
2470 SS.member (avail, tab)
|
adamc@1228
|
2471 | _ => true)) then
|
adamc@1228
|
2472 skip ()
|
adamc@1228
|
2473 orelse tryCombos (Int.max (maxLv,
|
adamc@1228
|
2474 maxLvarP g1),
|
adamc@1228
|
2475 pols,
|
adamc@1228
|
2476 And (g1, g),
|
adamc@1228
|
2477 outs1 @ outs)
|
adamc@1228
|
2478 else
|
adamc@1228
|
2479 skip ()
|
adamc@1228
|
2480 end
|
adamc@1228
|
2481 in
|
adamc@1228
|
2482 (fl <> Control Where
|
adamc@1228
|
2483 andalso imply (hyps, [AReln (Known, [Var 0])], SOME [Var 0]))
|
adamc@1228
|
2484 orelse List.exists (fn (p', outs) =>
|
adamc@1228
|
2485 decompG p'
|
adamc@1228
|
2486 (fn goals => imply (hyps, goals, SOME outs)))
|
adamc@1228
|
2487 client
|
adamc@1228
|
2488 orelse tryCombos (0, client, True, [])
|
adamc@1228
|
2489 orelse (reset ();
|
adamc@1228
|
2490 Print.preface ("Untenable hypotheses",
|
adamc@1228
|
2491 Print.p_list p_atom hyps);
|
adamc@1228
|
2492 false)
|
adamc@1228
|
2493 end) then
|
adamc@1213
|
2494 ()
|
adamc@1213
|
2495 else
|
adamc@1228
|
2496 ErrorMsg.errorAt loc "The information flow policy may be violated here."
|
adamc@1213
|
2497 end
|
adamc@1213
|
2498
|
adamc@1213
|
2499 fun doAll e =
|
adamc@1213
|
2500 case e of
|
adamc@1213
|
2501 Const _ => ()
|
adamc@1213
|
2502 | Var _ => doOne e
|
adamc@1213
|
2503 | Lvar _ => raise Fail "Iflow.doAll: Lvar"
|
adamc@1216
|
2504 | Func (UnCon _, [_]) => doOne e
|
adamc@1215
|
2505 | Func (_, es) => app doAll es
|
adamc@1213
|
2506 | Recd xes => app (doAll o #2) xes
|
adamc@1213
|
2507 | Proj _ => doOne e
|
adamc@1213
|
2508 | Finish => ()
|
adamc@1207
|
2509 in
|
adamc@1213
|
2510 doAll e
|
adamc@1220
|
2511 end) vals;
|
adamc@1220
|
2512
|
adamc@1221
|
2513 doDml (inserts, insert);
|
adamc@1223
|
2514 doDml (deletes, delete);
|
adamc@1223
|
2515 doDml (updates, update)
|
adamc@1200
|
2516 end
|
adamc@1200
|
2517
|
adamc@1213
|
2518 val check = fn file =>
|
adamc@1213
|
2519 let
|
adamc@1213
|
2520 val oldInline = Settings.getMonoInline ()
|
adamc@1213
|
2521 in
|
adamc@1213
|
2522 (Settings.setMonoInline (case Int.maxInt of
|
adamc@1213
|
2523 NONE => 1000000
|
adamc@1213
|
2524 | SOME n => n);
|
adamc@1213
|
2525 check file;
|
adamc@1213
|
2526 Settings.setMonoInline oldInline)
|
adamc@1213
|
2527 handle ex => (Settings.setMonoInline oldInline;
|
adamc@1213
|
2528 raise ex)
|
adamc@1213
|
2529 end
|
adamc@1213
|
2530
|
adamc@1200
|
2531 end
|
adamc@1213
|
2532
|