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@1202
|
313 fun eq' (e1, e2) =
|
adamc@1202
|
314 case (e1, e2) of
|
adamc@1202
|
315 (Const p1, Const p2) => Prim.equal (p1, p2)
|
adamc@1202
|
316 | (Var n1, Var n2) => n1 = n2
|
adamc@1202
|
317
|
adamc@1202
|
318 | (Lvar n1, _) =>
|
adamc@1202
|
319 (case IM.find (!unif, n1) of
|
adamc@1202
|
320 SOME e1 => eq' (e1, e2)
|
adamc@1202
|
321 | NONE =>
|
adamc@1202
|
322 case e2 of
|
adamc@1202
|
323 Lvar n2 =>
|
adamc@1202
|
324 (case IM.find (!unif, n2) of
|
adamc@1202
|
325 SOME e2 => eq' (e1, e2)
|
adamc@1202
|
326 | NONE => n1 = n2
|
adamc@1208
|
327 orelse (unif := IM.insert (!unif, n2, e1);
|
adamc@1202
|
328 true))
|
adamc@1202
|
329 | _ =>
|
adamc@1202
|
330 if lvarIn n1 e2 then
|
adamc@1202
|
331 false
|
adamc@1202
|
332 else
|
adamc@1202
|
333 (unif := IM.insert (!unif, n1, e2);
|
adamc@1202
|
334 true))
|
adamc@1202
|
335
|
adamc@1202
|
336 | (_, Lvar n2) =>
|
adamc@1202
|
337 (case IM.find (!unif, n2) of
|
adamc@1202
|
338 SOME e2 => eq' (e1, e2)
|
adamc@1202
|
339 | NONE =>
|
adamc@1202
|
340 if lvarIn n2 e1 then
|
adamc@1202
|
341 false
|
adamc@1202
|
342 else
|
adamc@1213
|
343 ((*Print.prefaces "unif" [("n2", Print.PD.string (Int.toString n2)),
|
adamc@1213
|
344 ("e1", p_exp e1)];*)
|
adamc@1213
|
345 unif := IM.insert (!unif, n2, e1);
|
adamc@1202
|
346 true))
|
adamc@1202
|
347
|
adamc@1202
|
348 | (Func (f1, es1), Func (f2, es2)) => f1 = f2 andalso ListPair.allEq eq' (es1, es2)
|
adamc@1202
|
349 | (Recd xes1, Recd xes2) => ListPair.allEq (fn ((x1, e1), (x2, e2)) => x1 = x2 andalso eq' (e1, e2)) (xes1, xes2)
|
adamc@1202
|
350 | (Proj (e1, s1), Proj (e2, s2)) => eq' (e1, e2) andalso s1 = s2
|
adamc@1202
|
351 | (Finish, Finish) => true
|
adamc@1202
|
352 | _ => false
|
adamc@1202
|
353
|
adamc@1202
|
354 fun eq (e1, e2) =
|
adamc@1202
|
355 let
|
adamc@1203
|
356 val saved = save ()
|
adamc@1202
|
357 in
|
adamc@1202
|
358 if eq' (simplify e1, simplify e2) then
|
adamc@1202
|
359 true
|
adamc@1202
|
360 else
|
adamc@1203
|
361 (restore saved;
|
adamc@1202
|
362 false)
|
adamc@1202
|
363 end
|
adamc@1202
|
364
|
adamc@1208
|
365 val debug = ref false
|
adamc@1208
|
366
|
adamc@1211
|
367 fun eeq (e1, e2) =
|
adamc@1211
|
368 case (e1, e2) of
|
adamc@1211
|
369 (Const p1, Const p2) => Prim.equal (p1, p2)
|
adamc@1211
|
370 | (Var n1, Var n2) => n1 = n2
|
adamc@1211
|
371 | (Lvar n1, Lvar n2) => n1 = n2
|
adamc@1211
|
372 | (Func (f1, es1), Func (f2, es2)) => f1 = f2 andalso ListPair.allEq eeq (es1, es2)
|
adamc@1211
|
373 | (Recd xes1, Recd xes2) => length xes1 = length xes2 andalso
|
adamc@1211
|
374 List.all (fn (x2, e2) =>
|
adamc@1211
|
375 List.exists (fn (x1, e1) => x1 = x2 andalso eeq (e1, e2)) xes2) xes1
|
adamc@1211
|
376 | (Proj (e1, x1), Proj (e2, x2)) => eeq (e1, e2) andalso x1 = x2
|
adamc@1211
|
377 | (Finish, Finish) => true
|
adamc@1211
|
378 | _ => false
|
adamc@1211
|
379
|
adamc@1208
|
380 (* Congruence closure *)
|
adamc@1208
|
381 structure Cc :> sig
|
adamc@1215
|
382 type database
|
adamc@1215
|
383
|
adamc@1215
|
384 exception Contradiction
|
adamc@1215
|
385 exception Undetermined
|
adamc@1215
|
386
|
adamc@1215
|
387 val database : unit -> database
|
adamc@1215
|
388
|
adamc@1215
|
389 val assert : database * atom -> unit
|
adamc@1215
|
390 val check : database * atom -> bool
|
adamc@1215
|
391
|
adamc@1215
|
392 val p_database : database Print.printer
|
adamc@1218
|
393
|
adamc@1218
|
394 val builtFrom : database * {Base : exp list, Derived : exp} -> bool
|
adamc@1208
|
395 end = struct
|
adamc@1208
|
396
|
adamc@1215
|
397 exception Contradiction
|
adamc@1215
|
398 exception Undetermined
|
adamc@1208
|
399
|
adamc@1215
|
400 structure CM = BinaryMapFn(struct
|
adamc@1215
|
401 type ord_key = Prim.t
|
adamc@1215
|
402 val compare = Prim.compare
|
adamc@1215
|
403 end)
|
adamc@1208
|
404
|
adamc@1215
|
405 datatype node = Node of {Rep : node ref option ref,
|
adamc@1215
|
406 Cons : node ref SM.map ref,
|
adamc@1215
|
407 Variety : variety,
|
adamc@1215
|
408 Known : bool ref}
|
adamc@1208
|
409
|
adamc@1215
|
410 and variety =
|
adamc@1215
|
411 Dt0 of string
|
adamc@1215
|
412 | Dt1 of string * node ref
|
adamc@1215
|
413 | Prim of Prim.t
|
adamc@1221
|
414 | Recrd of node ref SM.map ref * bool
|
adamc@1215
|
415 | VFinish
|
adamc@1215
|
416 | Nothing
|
adamc@1208
|
417
|
adamc@1215
|
418 type representative = node ref
|
adamc@1215
|
419
|
adamc@1215
|
420 val finish = ref (Node {Rep = ref NONE,
|
adamc@1215
|
421 Cons = ref SM.empty,
|
adamc@1215
|
422 Variety = VFinish,
|
adamc@1218
|
423 Known = ref true})
|
adamc@1215
|
424
|
adamc@1215
|
425 type database = {Vars : representative IM.map ref,
|
adamc@1215
|
426 Consts : representative CM.map ref,
|
adamc@1215
|
427 Con0s : representative SM.map ref,
|
adamc@1215
|
428 Records : (representative SM.map * representative) list ref,
|
adamc@1215
|
429 Funcs : ((string * representative list) * representative) list ref }
|
adamc@1215
|
430
|
adamc@1215
|
431 fun database () = {Vars = ref IM.empty,
|
adamc@1215
|
432 Consts = ref CM.empty,
|
adamc@1215
|
433 Con0s = ref SM.empty,
|
adamc@1215
|
434 Records = ref [],
|
adamc@1215
|
435 Funcs = ref []}
|
adamc@1215
|
436
|
adamc@1215
|
437 fun unNode n =
|
adamc@1215
|
438 case !n of
|
adamc@1215
|
439 Node r => r
|
adamc@1215
|
440
|
adamc@1215
|
441 open Print
|
adamc@1215
|
442 val string = PD.string
|
adamc@1215
|
443 val newline = PD.newline
|
adamc@1215
|
444
|
adamc@1215
|
445 fun p_rep n =
|
adamc@1215
|
446 case !(#Rep (unNode n)) of
|
adamc@1215
|
447 SOME n => p_rep n
|
adamc@1215
|
448 | NONE =>
|
adamc@1221
|
449 box [string (Int.toString (Unsafe.cast n) ^ ":"),
|
adamc@1221
|
450 space,
|
adamc@1221
|
451 case #Variety (unNode n) of
|
adamc@1221
|
452 Nothing => string "?"
|
adamc@1221
|
453 | Dt0 s => string ("Dt0(" ^ s ^ ")")
|
adamc@1221
|
454 | Dt1 (s, n) => box[string ("Dt1(" ^ s ^ ","),
|
adamc@1221
|
455 space,
|
adamc@1221
|
456 p_rep n,
|
adamc@1221
|
457 string ")"]
|
adamc@1221
|
458 | Prim p => Prim.p_t p
|
adamc@1221
|
459 | Recrd (ref m, b) => box [string "{",
|
adamc@1221
|
460 p_list (fn (x, n) => box [string x,
|
adamc@1221
|
461 space,
|
adamc@1221
|
462 string "=",
|
adamc@1221
|
463 space,
|
adamc@1221
|
464 p_rep n]) (SM.listItemsi m),
|
adamc@1221
|
465 string "}",
|
adamc@1221
|
466 if b then
|
adamc@1221
|
467 box [space,
|
adamc@1221
|
468 string "(complete)"]
|
adamc@1221
|
469 else
|
adamc@1221
|
470 box []]
|
adamc@1221
|
471 | VFinish => string "FINISH"]
|
adamc@1215
|
472
|
adamc@1215
|
473 fun p_database (db : database) =
|
adamc@1215
|
474 box [string "Vars:",
|
adamc@1215
|
475 newline,
|
adamc@1215
|
476 p_list_sep newline (fn (i, n) => box [string ("x" ^ Int.toString i),
|
adamc@1215
|
477 space,
|
adamc@1215
|
478 string "=",
|
adamc@1215
|
479 space,
|
adamc@1218
|
480 p_rep n,
|
adamc@1218
|
481 if !(#Known (unNode n)) then
|
adamc@1218
|
482 box [space,
|
adamc@1218
|
483 string "(known)"]
|
adamc@1218
|
484 else
|
adamc@1218
|
485 box []]) (IM.listItemsi (!(#Vars db)))]
|
adamc@1215
|
486
|
adamc@1215
|
487 fun repOf (n : representative) : representative =
|
adamc@1215
|
488 case !(#Rep (unNode n)) of
|
adamc@1215
|
489 NONE => n
|
adamc@1215
|
490 | SOME r =>
|
adamc@1215
|
491 let
|
adamc@1215
|
492 val r = repOf r
|
adamc@1215
|
493 in
|
adamc@1215
|
494 #Rep (unNode n) := SOME r;
|
adamc@1215
|
495 r
|
adamc@1215
|
496 end
|
adamc@1215
|
497
|
adamc@1215
|
498 fun markKnown r =
|
adamc@1221
|
499 let
|
adamc@1221
|
500 val r = repOf r
|
adamc@1221
|
501 in
|
adamc@1221
|
502 (*Print.preface ("markKnown", p_rep r);*)
|
adamc@1221
|
503 if !(#Known (unNode r)) then
|
adamc@1221
|
504 ()(*TextIO.print "Already known\n"*)
|
adamc@1221
|
505 else
|
adamc@1221
|
506 (#Known (unNode r) := true;
|
adamc@1221
|
507 SM.app markKnown (!(#Cons (unNode r)));
|
adamc@1221
|
508 case #Variety (unNode r) of
|
adamc@1221
|
509 Dt1 (_, r) => markKnown r
|
adamc@1221
|
510 | Recrd (xes, _) => SM.app markKnown (!xes)
|
adamc@1221
|
511 | _ => ())
|
adamc@1221
|
512 end
|
adamc@1215
|
513
|
adamc@1215
|
514 fun representative (db : database, e) =
|
adamc@1208
|
515 let
|
adamc@1215
|
516 fun rep e =
|
adamc@1215
|
517 case e of
|
adamc@1215
|
518 Const p => (case CM.find (!(#Consts db), p) of
|
adamc@1215
|
519 SOME r => repOf r
|
adamc@1215
|
520 | NONE =>
|
adamc@1215
|
521 let
|
adamc@1215
|
522 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
523 Cons = ref SM.empty,
|
adamc@1215
|
524 Variety = Prim p,
|
adamc@1219
|
525 Known = ref true})
|
adamc@1215
|
526 in
|
adamc@1215
|
527 #Consts db := CM.insert (!(#Consts db), p, r);
|
adamc@1215
|
528 r
|
adamc@1215
|
529 end)
|
adamc@1215
|
530 | Var n => (case IM.find (!(#Vars db), n) of
|
adamc@1215
|
531 SOME r => repOf r
|
adamc@1215
|
532 | NONE =>
|
adamc@1215
|
533 let
|
adamc@1215
|
534 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
535 Cons = ref SM.empty,
|
adamc@1215
|
536 Variety = Nothing,
|
adamc@1215
|
537 Known = ref false})
|
adamc@1215
|
538 in
|
adamc@1215
|
539 #Vars db := IM.insert (!(#Vars db), n, r);
|
adamc@1215
|
540 r
|
adamc@1215
|
541 end)
|
adamc@1215
|
542 | Lvar n =>
|
adamc@1215
|
543 (case IM.find (!unif, n) of
|
adamc@1215
|
544 NONE => raise Undetermined
|
adamc@1215
|
545 | SOME e => rep e)
|
adamc@1215
|
546 | Func (DtCon0 f, []) => (case SM.find (!(#Con0s db), f) of
|
adamc@1215
|
547 SOME r => repOf r
|
adamc@1215
|
548 | NONE =>
|
adamc@1215
|
549 let
|
adamc@1215
|
550 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
551 Cons = ref SM.empty,
|
adamc@1215
|
552 Variety = Dt0 f,
|
adamc@1218
|
553 Known = ref true})
|
adamc@1215
|
554 in
|
adamc@1215
|
555 #Con0s db := SM.insert (!(#Con0s db), f, r);
|
adamc@1215
|
556 r
|
adamc@1215
|
557 end)
|
adamc@1215
|
558 | Func (DtCon0 _, _) => raise Fail "Iflow.rep: DtCon0"
|
adamc@1215
|
559 | Func (DtCon1 f, [e]) =>
|
adamc@1215
|
560 let
|
adamc@1215
|
561 val r = rep e
|
adamc@1215
|
562 in
|
adamc@1215
|
563 case SM.find (!(#Cons (unNode r)), f) of
|
adamc@1215
|
564 SOME r => repOf r
|
adamc@1215
|
565 | NONE =>
|
adamc@1215
|
566 let
|
adamc@1215
|
567 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
568 Cons = ref SM.empty,
|
adamc@1215
|
569 Variety = Dt1 (f, r),
|
adamc@1221
|
570 Known = ref (!(#Known (unNode r)))})
|
adamc@1215
|
571 in
|
adamc@1215
|
572 #Cons (unNode r) := SM.insert (!(#Cons (unNode r)), f, r');
|
adamc@1215
|
573 r'
|
adamc@1215
|
574 end
|
adamc@1215
|
575 end
|
adamc@1215
|
576 | Func (DtCon1 _, _) => raise Fail "Iflow.rep: DtCon1"
|
adamc@1215
|
577 | Func (UnCon f, [e]) =>
|
adamc@1215
|
578 let
|
adamc@1215
|
579 val r = rep e
|
adamc@1215
|
580 in
|
adamc@1215
|
581 case #Variety (unNode r) of
|
adamc@1215
|
582 Dt1 (f', n) => if f' = f then
|
adamc@1215
|
583 repOf n
|
adamc@1215
|
584 else
|
adamc@1215
|
585 raise Contradiction
|
adamc@1215
|
586 | Nothing =>
|
adamc@1215
|
587 let
|
adamc@1215
|
588 val cons = ref SM.empty
|
adamc@1215
|
589 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
590 Cons = cons,
|
adamc@1215
|
591 Variety = Nothing,
|
adamc@1221
|
592 Known = ref (!(#Known (unNode r)))})
|
adamc@1215
|
593
|
adamc@1215
|
594 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
595 Cons = #Cons (unNode r),
|
adamc@1215
|
596 Variety = Dt1 (f, r'),
|
adamc@1215
|
597 Known = #Known (unNode r)})
|
adamc@1215
|
598 in
|
adamc@1215
|
599 cons := SM.insert (!cons, f, r'');
|
adamc@1215
|
600 #Rep (unNode r) := SOME r'';
|
adamc@1215
|
601 r'
|
adamc@1215
|
602 end
|
adamc@1215
|
603 | VFinish => r
|
adamc@1215
|
604 | _ => raise Contradiction
|
adamc@1215
|
605 end
|
adamc@1215
|
606 | Func (UnCon _, _) => raise Fail "Iflow.rep: UnCon"
|
adamc@1215
|
607 | Func (Other f, es) =>
|
adamc@1215
|
608 let
|
adamc@1215
|
609 val rs = map rep es
|
adamc@1215
|
610 in
|
adamc@1215
|
611 case List.find (fn (x : string * representative list, _) => x = (f, rs)) (!(#Funcs db)) of
|
adamc@1215
|
612 NONE =>
|
adamc@1215
|
613 let
|
adamc@1215
|
614 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
615 Cons = ref SM.empty,
|
adamc@1215
|
616 Variety = Nothing,
|
adamc@1215
|
617 Known = ref false})
|
adamc@1215
|
618 in
|
adamc@1215
|
619 #Funcs db := ((f, rs), r) :: (!(#Funcs db));
|
adamc@1215
|
620 r
|
adamc@1215
|
621 end
|
adamc@1215
|
622 | SOME (_, r) => repOf r
|
adamc@1215
|
623 end
|
adamc@1215
|
624 | Recd xes =>
|
adamc@1215
|
625 let
|
adamc@1215
|
626 val xes = map (fn (x, e) => (x, rep e)) xes
|
adamc@1215
|
627 val len = length xes
|
adamc@1215
|
628 in
|
adamc@1215
|
629 case List.find (fn (xes', _) =>
|
adamc@1215
|
630 SM.numItems xes' = len
|
adamc@1215
|
631 andalso List.all (fn (x, n) =>
|
adamc@1215
|
632 case SM.find (xes', x) of
|
adamc@1215
|
633 NONE => false
|
adamc@1215
|
634 | SOME n' => n = repOf n') xes)
|
adamc@1215
|
635 (!(#Records db)) of
|
adamc@1215
|
636 SOME (_, r) => repOf r
|
adamc@1215
|
637 | NONE =>
|
adamc@1215
|
638 let
|
adamc@1215
|
639 val xes = foldl SM.insert' SM.empty xes
|
adamc@1215
|
640
|
adamc@1215
|
641 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
642 Cons = ref SM.empty,
|
adamc@1221
|
643 Variety = Recrd (ref xes, true),
|
adamc@1215
|
644 Known = ref false})
|
adamc@1215
|
645 in
|
adamc@1215
|
646 #Records db := (xes, r') :: (!(#Records db));
|
adamc@1215
|
647 r'
|
adamc@1215
|
648 end
|
adamc@1215
|
649 end
|
adamc@1215
|
650 | Proj (e, f) =>
|
adamc@1215
|
651 let
|
adamc@1215
|
652 val r = rep e
|
adamc@1215
|
653 in
|
adamc@1215
|
654 case #Variety (unNode r) of
|
adamc@1221
|
655 Recrd (xes, _) =>
|
adamc@1215
|
656 (case SM.find (!xes, f) of
|
adamc@1215
|
657 SOME r => repOf r
|
adamc@1216
|
658 | NONE => let
|
adamc@1215
|
659 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
660 Cons = ref SM.empty,
|
adamc@1215
|
661 Variety = Nothing,
|
adamc@1221
|
662 Known = ref (!(#Known (unNode r)))})
|
adamc@1215
|
663 in
|
adamc@1215
|
664 xes := SM.insert (!xes, f, r);
|
adamc@1215
|
665 r
|
adamc@1215
|
666 end)
|
adamc@1215
|
667 | Nothing =>
|
adamc@1215
|
668 let
|
adamc@1215
|
669 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
670 Cons = ref SM.empty,
|
adamc@1215
|
671 Variety = Nothing,
|
adamc@1221
|
672 Known = ref (!(#Known (unNode r)))})
|
adamc@1215
|
673
|
adamc@1215
|
674 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
675 Cons = #Cons (unNode r),
|
adamc@1221
|
676 Variety = Recrd (ref (SM.insert (SM.empty, f, r')), false),
|
adamc@1215
|
677 Known = #Known (unNode r)})
|
adamc@1215
|
678 in
|
adamc@1215
|
679 #Rep (unNode r) := SOME r'';
|
adamc@1215
|
680 r'
|
adamc@1215
|
681 end
|
adamc@1215
|
682 | VFinish => r
|
adamc@1215
|
683 | _ => raise Contradiction
|
adamc@1215
|
684 end
|
adamc@1215
|
685 | Finish => finish
|
adamc@1208
|
686 in
|
adamc@1215
|
687 rep e
|
adamc@1208
|
688 end
|
adamc@1208
|
689
|
adamc@1215
|
690 fun assert (db, a) =
|
adamc@1215
|
691 case a of
|
adamc@1215
|
692 ACond _ => ()
|
adamc@1215
|
693 | AReln x =>
|
adamc@1215
|
694 case x of
|
adamc@1221
|
695 (Known, [e]) =>
|
adamc@1221
|
696 ((*Print.prefaces "Before" [("e", p_exp e),
|
adamc@1221
|
697 ("db", p_database db)];*)
|
adamc@1221
|
698 markKnown (representative (db, e))(*;
|
adamc@1221
|
699 Print.prefaces "After" [("e", p_exp e),
|
adamc@1221
|
700 ("db", p_database db)]*))
|
adamc@1215
|
701 | (PCon0 f, [e]) =>
|
adamc@1215
|
702 let
|
adamc@1215
|
703 val r = representative (db, e)
|
adamc@1215
|
704 in
|
adamc@1215
|
705 case #Variety (unNode r) of
|
adamc@1215
|
706 Dt0 f' => if f = f' then
|
adamc@1215
|
707 ()
|
adamc@1215
|
708 else
|
adamc@1215
|
709 raise Contradiction
|
adamc@1215
|
710 | Nothing =>
|
adamc@1215
|
711 let
|
adamc@1215
|
712 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
713 Cons = ref SM.empty,
|
adamc@1215
|
714 Variety = Dt0 f,
|
adamc@1215
|
715 Known = ref false})
|
adamc@1215
|
716 in
|
adamc@1215
|
717 #Rep (unNode r) := SOME r'
|
adamc@1215
|
718 end
|
adamc@1215
|
719 | _ => raise Contradiction
|
adamc@1215
|
720 end
|
adamc@1215
|
721 | (PCon1 f, [e]) =>
|
adamc@1215
|
722 let
|
adamc@1215
|
723 val r = representative (db, e)
|
adamc@1215
|
724 in
|
adamc@1215
|
725 case #Variety (unNode r) of
|
adamc@1215
|
726 Dt1 (f', e') => if f = f' then
|
adamc@1215
|
727 ()
|
adamc@1215
|
728 else
|
adamc@1215
|
729 raise Contradiction
|
adamc@1215
|
730 | Nothing =>
|
adamc@1215
|
731 let
|
adamc@1215
|
732 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
733 Cons = ref SM.empty,
|
adamc@1215
|
734 Variety = Nothing,
|
adamc@1215
|
735 Known = ref false})
|
adamc@1214
|
736
|
adamc@1215
|
737 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
738 Cons = ref SM.empty,
|
adamc@1215
|
739 Variety = Dt1 (f, r''),
|
adamc@1215
|
740 Known = ref false})
|
adamc@1215
|
741 in
|
adamc@1215
|
742 #Rep (unNode r) := SOME r'
|
adamc@1215
|
743 end
|
adamc@1215
|
744 | _ => raise Contradiction
|
adamc@1215
|
745 end
|
adamc@1215
|
746 | (Eq, [e1, e2]) =>
|
adamc@1215
|
747 let
|
adamc@1215
|
748 fun markEq (r1, r2) =
|
adamc@1215
|
749 if r1 = r2 then
|
adamc@1215
|
750 ()
|
adamc@1215
|
751 else case (#Variety (unNode r1), #Variety (unNode r2)) of
|
adamc@1215
|
752 (Prim p1, Prim p2) => if Prim.equal (p1, p2) then
|
adamc@1215
|
753 ()
|
adamc@1215
|
754 else
|
adamc@1215
|
755 raise Contradiction
|
adamc@1215
|
756 | (Dt0 f1, Dt0 f2) => if f1 = f2 then
|
adamc@1215
|
757 ()
|
adamc@1215
|
758 else
|
adamc@1215
|
759 raise Contradiction
|
adamc@1215
|
760 | (Dt1 (f1, r1), Dt1 (f2, r2)) => if f1 = f2 then
|
adamc@1215
|
761 markEq (r1, r2)
|
adamc@1215
|
762 else
|
adamc@1215
|
763 raise Contradiction
|
adamc@1221
|
764 | (Recrd (xes1, _), Recrd (xes2, _)) =>
|
adamc@1215
|
765 let
|
adamc@1215
|
766 fun unif (xes1, xes2) =
|
adamc@1215
|
767 SM.appi (fn (x, r1) =>
|
adamc@1215
|
768 case SM.find (xes2, x) of
|
adamc@1215
|
769 NONE => ()
|
adamc@1215
|
770 | SOME r2 => markEq (r1, r2)) xes1
|
adamc@1215
|
771 in
|
adamc@1215
|
772 unif (!xes1, !xes2);
|
adamc@1215
|
773 unif (!xes2, !xes1)
|
adamc@1215
|
774 end
|
adamc@1215
|
775 | (VFinish, VFinish) => ()
|
adamc@1218
|
776 | (Nothing, _) => mergeNodes (r1, r2)
|
adamc@1218
|
777 | (_, Nothing) => mergeNodes (r2, r1)
|
adamc@1215
|
778 | _ => raise Contradiction
|
adamc@1214
|
779
|
adamc@1218
|
780 and mergeNodes (r1, r2) =
|
adamc@1218
|
781 (#Rep (unNode r1) := SOME r2;
|
adamc@1218
|
782 if !(#Known (unNode r1)) then
|
adamc@1218
|
783 markKnown r2
|
adamc@1218
|
784 else
|
adamc@1218
|
785 ();
|
adamc@1218
|
786 if !(#Known (unNode r2)) then
|
adamc@1218
|
787 markKnown r1
|
adamc@1218
|
788 else
|
adamc@1218
|
789 ();
|
adamc@1218
|
790 #Cons (unNode r2) := SM.unionWith #1 (!(#Cons (unNode r2)), !(#Cons (unNode r1)));
|
adamc@1218
|
791 compactFuncs ())
|
adamc@1218
|
792
|
adamc@1215
|
793 and compactFuncs () =
|
adamc@1214
|
794 let
|
adamc@1215
|
795 fun loop funcs =
|
adamc@1215
|
796 case funcs of
|
adamc@1215
|
797 [] => []
|
adamc@1215
|
798 | (fr as ((f, rs), r)) :: rest =>
|
adamc@1215
|
799 let
|
adamc@1215
|
800 val rest = List.filter (fn ((f' : string, rs'), r') =>
|
adamc@1215
|
801 if f' = f
|
adamc@1215
|
802 andalso ListPair.allEq (fn (r1, r2) =>
|
adamc@1215
|
803 repOf r1 = repOf r2)
|
adamc@1215
|
804 (rs, rs') then
|
adamc@1215
|
805 (markEq (r, r');
|
adamc@1215
|
806 false)
|
adamc@1215
|
807 else
|
adamc@1215
|
808 true) rest
|
adamc@1215
|
809 in
|
adamc@1215
|
810 fr :: loop rest
|
adamc@1215
|
811 end
|
adamc@1214
|
812 in
|
adamc@1215
|
813 #Funcs db := loop (!(#Funcs db))
|
adamc@1214
|
814 end
|
adamc@1215
|
815 in
|
adamc@1215
|
816 markEq (representative (db, e1), representative (db, e2))
|
adamc@1215
|
817 end
|
adamc@1215
|
818 | _ => ()
|
adamc@1214
|
819
|
adamc@1215
|
820 fun check (db, a) =
|
adamc@1215
|
821 case a of
|
adamc@1215
|
822 ACond _ => false
|
adamc@1215
|
823 | AReln x =>
|
adamc@1215
|
824 case x of
|
adamc@1221
|
825 (Known, [e]) =>
|
adamc@1221
|
826 let
|
adamc@1221
|
827 fun isKnown r =
|
adamc@1221
|
828 let
|
adamc@1221
|
829 val r = repOf r
|
adamc@1221
|
830 in
|
adamc@1221
|
831 !(#Known (unNode r))
|
adamc@1221
|
832 orelse case #Variety (unNode r) of
|
adamc@1221
|
833 Dt1 (_, r) => isKnown r
|
adamc@1221
|
834 | Recrd (xes, true) => List.all isKnown (SM.listItems (!xes))
|
adamc@1221
|
835 | _ => false
|
adamc@1221
|
836 end
|
adamc@1221
|
837
|
adamc@1221
|
838 val r = representative (db, e)
|
adamc@1221
|
839 in
|
adamc@1221
|
840 isKnown r
|
adamc@1221
|
841 end
|
adamc@1215
|
842 | (PCon0 f, [e]) =>
|
adamc@1215
|
843 (case #Variety (unNode (representative (db, e))) of
|
adamc@1215
|
844 Dt0 f' => f' = f
|
adamc@1215
|
845 | _ => false)
|
adamc@1215
|
846 | (PCon1 f, [e]) =>
|
adamc@1215
|
847 (case #Variety (unNode (representative (db, e))) of
|
adamc@1215
|
848 Dt1 (f', _) => f' = f
|
adamc@1215
|
849 | _ => false)
|
adamc@1215
|
850 | (Eq, [e1, e2]) =>
|
adamc@1214
|
851 let
|
adamc@1215
|
852 val r1 = representative (db, e1)
|
adamc@1215
|
853 val r2 = representative (db, e2)
|
adamc@1214
|
854 in
|
adamc@1215
|
855 repOf r1 = repOf r2
|
adamc@1214
|
856 end
|
adamc@1215
|
857 | _ => false
|
adamc@1212
|
858
|
adamc@1218
|
859 fun builtFrom (db, {Base = bs, Derived = d}) =
|
adamc@1218
|
860 let
|
adamc@1218
|
861 val bs = map (fn b => representative (db, b)) bs
|
adamc@1218
|
862
|
adamc@1218
|
863 fun loop d =
|
adamc@1218
|
864 let
|
adamc@1218
|
865 val d = repOf d
|
adamc@1218
|
866 in
|
adamc@1218
|
867 List.exists (fn b => repOf b = d) bs
|
adamc@1218
|
868 orelse case #Variety (unNode d) of
|
adamc@1218
|
869 Dt0 _ => true
|
adamc@1218
|
870 | Dt1 (_, d) => loop d
|
adamc@1218
|
871 | Prim _ => true
|
adamc@1221
|
872 | Recrd (xes, _) => List.all loop (SM.listItems (!xes))
|
adamc@1218
|
873 | VFinish => true
|
adamc@1218
|
874 | Nothing => false
|
adamc@1218
|
875 end
|
adamc@1218
|
876 in
|
adamc@1218
|
877 loop (representative (db, d))
|
adamc@1218
|
878 end
|
adamc@1218
|
879
|
adamc@1208
|
880 end
|
adamc@1208
|
881
|
adamc@1215
|
882 fun decomp fals or =
|
adamc@1215
|
883 let
|
adamc@1215
|
884 fun decomp p k =
|
adamc@1215
|
885 case p of
|
adamc@1215
|
886 True => k []
|
adamc@1215
|
887 | False => fals
|
adamc@1215
|
888 | Unknown => k []
|
adamc@1215
|
889 | And (p1, p2) =>
|
adamc@1215
|
890 decomp p1 (fn ps1 =>
|
adamc@1215
|
891 decomp p2 (fn ps2 =>
|
adamc@1215
|
892 k (ps1 @ ps2)))
|
adamc@1215
|
893 | Or (p1, p2) =>
|
adamc@1215
|
894 or (decomp p1 k, fn () => decomp p2 k)
|
adamc@1215
|
895 | Reln x => k [AReln x]
|
adamc@1215
|
896 | Cond x => k [ACond x]
|
adamc@1215
|
897 in
|
adamc@1215
|
898 decomp
|
adamc@1215
|
899 end
|
adamc@1202
|
900
|
adamc@1218
|
901 fun imply (hyps, goals, outs) =
|
adamc@1218
|
902 let
|
adamc@1218
|
903 fun gls goals onFail acc =
|
adamc@1218
|
904 case goals of
|
adamc@1218
|
905 [] =>
|
adamc@1218
|
906 (let
|
adamc@1218
|
907 val cc = Cc.database ()
|
adamc@1218
|
908 val () = app (fn a => Cc.assert (cc, a)) hyps
|
adamc@1218
|
909 in
|
adamc@1221
|
910 (*Print.preface ("db", Cc.p_database cc);*)
|
adamc@1218
|
911 (List.all (fn a =>
|
adamc@1218
|
912 if Cc.check (cc, a) then
|
adamc@1218
|
913 true
|
adamc@1218
|
914 else
|
adamc@1218
|
915 ((*Print.prefaces "Can't prove"
|
adamc@1218
|
916 [("a", p_atom a),
|
adamc@1218
|
917 ("hyps", Print.p_list p_atom hyps),
|
adamc@1218
|
918 ("db", Cc.p_database cc)];*)
|
adamc@1218
|
919 false)) acc
|
adamc@1218
|
920 (*andalso (Print.preface ("Finding", Cc.p_database cc); true)*)
|
adamc@1220
|
921 andalso (case outs of
|
adamc@1220
|
922 NONE => true
|
adamc@1220
|
923 | SOME outs => Cc.builtFrom (cc, {Derived = Var 0,
|
adamc@1220
|
924 Base = outs})))
|
adamc@1218
|
925 handle Cc.Contradiction => false
|
adamc@1218
|
926 end handle Cc.Undetermined => false)
|
adamc@1218
|
927 orelse onFail ()
|
adamc@1218
|
928 | (g as AReln (Sql gf, [ge])) :: goals =>
|
adamc@1218
|
929 let
|
adamc@1218
|
930 fun hps hyps =
|
adamc@1218
|
931 case hyps of
|
adamc@1218
|
932 [] => gls goals onFail (g :: acc)
|
adamc@1218
|
933 | (h as AReln (Sql hf, [he])) :: hyps =>
|
adamc@1218
|
934 if gf = hf then
|
adamc@1218
|
935 let
|
adamc@1218
|
936 val saved = save ()
|
adamc@1218
|
937 in
|
adamc@1218
|
938 if eq (ge, he) then
|
adamc@1218
|
939 let
|
adamc@1218
|
940 val changed = IM.numItems (!unif)
|
adamc@1218
|
941 <> IM.numItems saved
|
adamc@1218
|
942 in
|
adamc@1218
|
943 gls goals (fn () => (restore saved;
|
adamc@1218
|
944 changed
|
adamc@1218
|
945 andalso hps hyps))
|
adamc@1218
|
946 acc
|
adamc@1218
|
947 end
|
adamc@1218
|
948 else
|
adamc@1218
|
949 hps hyps
|
adamc@1218
|
950 end
|
adamc@1218
|
951 else
|
adamc@1218
|
952 hps hyps
|
adamc@1218
|
953 | _ :: hyps => hps hyps
|
adamc@1218
|
954 in
|
adamc@1218
|
955 hps hyps
|
adamc@1218
|
956 end
|
adamc@1218
|
957 | g :: goals => gls goals onFail (g :: acc)
|
adamc@1218
|
958 in
|
adamc@1218
|
959 reset ();
|
adamc@1218
|
960 (*Print.prefaces "Big go" [("hyps", Print.p_list p_atom hyps),
|
adamc@1218
|
961 ("goals", Print.p_list p_atom goals)];*)
|
adamc@1218
|
962 gls goals (fn () => false) []
|
adamc@1218
|
963 end handle Cc.Contradiction => true
|
adamc@1200
|
964
|
adamc@1200
|
965 fun patCon pc =
|
adamc@1200
|
966 case pc of
|
adamc@1200
|
967 PConVar n => "C" ^ Int.toString n
|
adamc@1200
|
968 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
|
adamc@1200
|
969
|
adamc@1200
|
970 datatype chunk =
|
adamc@1200
|
971 String of string
|
adamc@1200
|
972 | Exp of Mono.exp
|
adamc@1200
|
973
|
adamc@1200
|
974 fun chunkify e =
|
adamc@1200
|
975 case #1 e of
|
adamc@1200
|
976 EPrim (Prim.String s) => [String s]
|
adamc@1207
|
977 | EStrcat (e1, e2) =>
|
adamc@1207
|
978 let
|
adamc@1207
|
979 val chs1 = chunkify e1
|
adamc@1207
|
980 val chs2 = chunkify e2
|
adamc@1207
|
981 in
|
adamc@1207
|
982 case chs2 of
|
adamc@1207
|
983 String s2 :: chs2' =>
|
adamc@1207
|
984 (case List.last chs1 of
|
adamc@1207
|
985 String s1 => List.take (chs1, length chs1 - 1) @ String (s1 ^ s2) :: chs2'
|
adamc@1207
|
986 | _ => chs1 @ chs2)
|
adamc@1207
|
987 | _ => chs1 @ chs2
|
adamc@1207
|
988 end
|
adamc@1200
|
989 | _ => [Exp e]
|
adamc@1200
|
990
|
adamc@1201
|
991 type 'a parser = chunk list -> ('a * chunk list) option
|
adamc@1201
|
992
|
adamc@1201
|
993 fun always v chs = SOME (v, chs)
|
adamc@1201
|
994
|
adamc@1202
|
995 fun parse p s =
|
adamc@1202
|
996 case p (chunkify s) of
|
adamc@1201
|
997 SOME (v, []) => SOME v
|
adamc@1201
|
998 | _ => NONE
|
adamc@1201
|
999
|
adamc@1201
|
1000 fun const s chs =
|
adamc@1201
|
1001 case chs of
|
adamc@1201
|
1002 String s' :: chs => if String.isPrefix s s' then
|
adamc@1201
|
1003 SOME ((), if size s = size s' then
|
adamc@1201
|
1004 chs
|
adamc@1201
|
1005 else
|
adamc@1201
|
1006 String (String.extract (s', size s, NONE)) :: chs)
|
adamc@1201
|
1007 else
|
adamc@1201
|
1008 NONE
|
adamc@1201
|
1009 | _ => NONE
|
adamc@1201
|
1010
|
adamc@1201
|
1011 fun follow p1 p2 chs =
|
adamc@1201
|
1012 case p1 chs of
|
adamc@1201
|
1013 NONE => NONE
|
adamc@1201
|
1014 | SOME (v1, chs) =>
|
adamc@1201
|
1015 case p2 chs of
|
adamc@1201
|
1016 NONE => NONE
|
adamc@1201
|
1017 | SOME (v2, chs) => SOME ((v1, v2), chs)
|
adamc@1201
|
1018
|
adamc@1201
|
1019 fun wrap p f chs =
|
adamc@1201
|
1020 case p chs of
|
adamc@1201
|
1021 NONE => NONE
|
adamc@1201
|
1022 | SOME (v, chs) => SOME (f v, chs)
|
adamc@1201
|
1023
|
adamc@1209
|
1024 fun wrapP p f chs =
|
adamc@1209
|
1025 case p chs of
|
adamc@1209
|
1026 NONE => NONE
|
adamc@1209
|
1027 | SOME (v, chs) =>
|
adamc@1209
|
1028 case f v of
|
adamc@1209
|
1029 NONE => NONE
|
adamc@1209
|
1030 | SOME r => SOME (r, chs)
|
adamc@1209
|
1031
|
adamc@1201
|
1032 fun alt p1 p2 chs =
|
adamc@1201
|
1033 case p1 chs of
|
adamc@1201
|
1034 NONE => p2 chs
|
adamc@1201
|
1035 | v => v
|
adamc@1201
|
1036
|
adamc@1207
|
1037 fun altL ps =
|
adamc@1207
|
1038 case rev ps of
|
adamc@1207
|
1039 [] => (fn _ => NONE)
|
adamc@1207
|
1040 | p :: ps =>
|
adamc@1207
|
1041 foldl (fn (p1, p2) => alt p1 p2) p ps
|
adamc@1207
|
1042
|
adamc@1204
|
1043 fun opt p chs =
|
adamc@1204
|
1044 case p chs of
|
adamc@1204
|
1045 NONE => SOME (NONE, chs)
|
adamc@1204
|
1046 | SOME (v, chs) => SOME (SOME v, chs)
|
adamc@1204
|
1047
|
adamc@1201
|
1048 fun skip cp chs =
|
adamc@1201
|
1049 case chs of
|
adamc@1201
|
1050 String "" :: chs => skip cp chs
|
adamc@1201
|
1051 | String s :: chs' => if cp (String.sub (s, 0)) then
|
adamc@1201
|
1052 skip cp (String (String.extract (s, 1, NONE)) :: chs')
|
adamc@1201
|
1053 else
|
adamc@1201
|
1054 SOME ((), chs)
|
adamc@1201
|
1055 | _ => SOME ((), chs)
|
adamc@1201
|
1056
|
adamc@1201
|
1057 fun keep cp chs =
|
adamc@1201
|
1058 case chs of
|
adamc@1201
|
1059 String "" :: chs => keep cp chs
|
adamc@1201
|
1060 | String s :: chs' =>
|
adamc@1201
|
1061 let
|
adamc@1201
|
1062 val (befor, after) = Substring.splitl cp (Substring.full s)
|
adamc@1201
|
1063 in
|
adamc@1201
|
1064 if Substring.isEmpty befor then
|
adamc@1201
|
1065 NONE
|
adamc@1201
|
1066 else
|
adamc@1201
|
1067 SOME (Substring.string befor,
|
adamc@1201
|
1068 if Substring.isEmpty after then
|
adamc@1201
|
1069 chs'
|
adamc@1201
|
1070 else
|
adamc@1201
|
1071 String (Substring.string after) :: chs')
|
adamc@1201
|
1072 end
|
adamc@1201
|
1073 | _ => NONE
|
adamc@1201
|
1074
|
adamc@1204
|
1075 fun ws p = wrap (follow (skip (fn ch => ch = #" "))
|
adamc@1204
|
1076 (follow p (skip (fn ch => ch = #" ")))) (#1 o #2)
|
adamc@1204
|
1077
|
adamc@1204
|
1078 fun log name p chs =
|
adamc@1206
|
1079 (if !debug then
|
adamc@1206
|
1080 case chs of
|
adamc@1207
|
1081 String s :: _ => print (name ^ ": " ^ s ^ "\n")
|
adamc@1206
|
1082 | _ => print (name ^ ": blocked!\n")
|
adamc@1206
|
1083 else
|
adamc@1206
|
1084 ();
|
adamc@1204
|
1085 p chs)
|
adamc@1201
|
1086
|
adamc@1201
|
1087 fun list p chs =
|
adamc@1207
|
1088 altL [wrap (follow p (follow (ws (const ",")) (list p)))
|
adamc@1207
|
1089 (fn (v, ((), ls)) => v :: ls),
|
adamc@1207
|
1090 wrap (ws p) (fn v => [v]),
|
adamc@1207
|
1091 always []] chs
|
adamc@1201
|
1092
|
adamc@1201
|
1093 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
|
adamc@1201
|
1094
|
adamc@1211
|
1095 val t_ident = wrapP ident (fn s => if String.isPrefix "T_" s then
|
adamc@1211
|
1096 SOME (String.extract (s, 2, NONE))
|
adamc@1201
|
1097 else
|
adamc@1211
|
1098 NONE)
|
adamc@1211
|
1099 val uw_ident = wrapP ident (fn s => if String.isPrefix "uw_" s andalso size s >= 4 then
|
adamc@1211
|
1100 SOME (str (Char.toUpper (String.sub (s, 3)))
|
adamc@1211
|
1101 ^ String.extract (s, 4, NONE))
|
adamc@1211
|
1102 else
|
adamc@1211
|
1103 NONE)
|
adamc@1201
|
1104
|
adamc@1211
|
1105 val field = wrap (follow t_ident
|
adamc@1201
|
1106 (follow (const ".")
|
adamc@1201
|
1107 uw_ident))
|
adamc@1201
|
1108 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
1109
|
adamc@1206
|
1110 datatype Rel =
|
adamc@1206
|
1111 Exps of exp * exp -> prop
|
adamc@1206
|
1112 | Props of prop * prop -> prop
|
adamc@1206
|
1113
|
adamc@1204
|
1114 datatype sqexp =
|
adamc@1206
|
1115 SqConst of Prim.t
|
adamc@1206
|
1116 | Field of string * string
|
adamc@1206
|
1117 | Binop of Rel * sqexp * sqexp
|
adamc@1207
|
1118 | SqKnown of sqexp
|
adamc@1207
|
1119 | Inj of Mono.exp
|
adamc@1211
|
1120 | SqFunc of string * sqexp
|
adamc@1211
|
1121 | Count
|
adamc@1204
|
1122
|
adamc@1210
|
1123 fun cmp s r = wrap (const s) (fn () => Exps (fn (e1, e2) => Reln (r, [e1, e2])))
|
adamc@1210
|
1124
|
adamc@1210
|
1125 val sqbrel = altL [cmp "=" Eq,
|
adamc@1210
|
1126 cmp "<>" Ne,
|
adamc@1210
|
1127 cmp "<=" Le,
|
adamc@1210
|
1128 cmp "<" Lt,
|
adamc@1210
|
1129 cmp ">=" Ge,
|
adamc@1210
|
1130 cmp ">" Gt,
|
adamc@1207
|
1131 wrap (const "AND") (fn () => Props And),
|
adamc@1207
|
1132 wrap (const "OR") (fn () => Props Or)]
|
adamc@1204
|
1133
|
adamc@1204
|
1134 datatype ('a, 'b) sum = inl of 'a | inr of 'b
|
adamc@1204
|
1135
|
adamc@1209
|
1136 fun string chs =
|
adamc@1206
|
1137 case chs of
|
adamc@1209
|
1138 String s :: chs =>
|
adamc@1209
|
1139 if size s >= 2 andalso String.sub (s, 0) = #"'" then
|
adamc@1209
|
1140 let
|
adamc@1209
|
1141 fun loop (cs, acc) =
|
adamc@1209
|
1142 case cs of
|
adamc@1209
|
1143 [] => NONE
|
adamc@1209
|
1144 | c :: cs =>
|
adamc@1209
|
1145 if c = #"'" then
|
adamc@1209
|
1146 SOME (String.implode (rev acc), cs)
|
adamc@1209
|
1147 else if c = #"\\" then
|
adamc@1209
|
1148 case cs of
|
adamc@1209
|
1149 c :: cs => loop (cs, c :: acc)
|
adamc@1209
|
1150 | _ => raise Fail "Iflow.string: Unmatched backslash escape"
|
adamc@1209
|
1151 else
|
adamc@1209
|
1152 loop (cs, c :: acc)
|
adamc@1209
|
1153 in
|
adamc@1209
|
1154 case loop (String.explode (String.extract (s, 1, NONE)), []) of
|
adamc@1209
|
1155 NONE => NONE
|
adamc@1209
|
1156 | SOME (s, []) => SOME (s, chs)
|
adamc@1209
|
1157 | SOME (s, cs) => SOME (s, String (String.implode cs) :: chs)
|
adamc@1209
|
1158 end
|
adamc@1209
|
1159 else
|
adamc@1209
|
1160 NONE
|
adamc@1209
|
1161 | _ => NONE
|
adamc@1206
|
1162
|
adamc@1209
|
1163 val prim =
|
adamc@1209
|
1164 altL [wrap (follow (wrapP (follow (keep Char.isDigit) (follow (const ".") (keep Char.isDigit)))
|
adamc@1209
|
1165 (fn (x, ((), y)) => Option.map Prim.Float (Real64.fromString (x ^ "." ^ y))))
|
adamc@1209
|
1166 (opt (const "::float8"))) #1,
|
adamc@1209
|
1167 wrap (follow (wrapP (keep Char.isDigit)
|
adamc@1209
|
1168 (Option.map Prim.Int o Int64.fromString))
|
adamc@1209
|
1169 (opt (const "::int8"))) #1,
|
adamc@1209
|
1170 wrap (follow (opt (const "E")) (follow string (opt (const "::text"))))
|
adamc@1209
|
1171 (Prim.String o #1 o #2)]
|
adamc@1206
|
1172
|
adamc@1207
|
1173 fun known' chs =
|
adamc@1207
|
1174 case chs of
|
adamc@1207
|
1175 Exp (EFfi ("Basis", "sql_known"), _) :: chs => SOME ((), chs)
|
adamc@1207
|
1176 | _ => NONE
|
adamc@1207
|
1177
|
adamc@1207
|
1178 fun sqlify chs =
|
adamc@1207
|
1179 case chs of
|
adamc@1207
|
1180 Exp (EFfiApp ("Basis", f, [e]), _) :: chs =>
|
adamc@1207
|
1181 if String.isPrefix "sqlify" f then
|
adamc@1207
|
1182 SOME (e, chs)
|
adamc@1207
|
1183 else
|
adamc@1207
|
1184 NONE
|
adamc@1207
|
1185 | _ => NONE
|
adamc@1207
|
1186
|
adamc@1211
|
1187 fun constK s = wrap (const s) (fn () => s)
|
adamc@1211
|
1188
|
adamc@1211
|
1189 val funcName = altL [constK "COUNT",
|
adamc@1211
|
1190 constK "MIN",
|
adamc@1211
|
1191 constK "MAX",
|
adamc@1211
|
1192 constK "SUM",
|
adamc@1211
|
1193 constK "AVG"]
|
adamc@1211
|
1194
|
adamc@1204
|
1195 fun sqexp chs =
|
adamc@1206
|
1196 log "sqexp"
|
adamc@1207
|
1197 (altL [wrap prim SqConst,
|
adamc@1211
|
1198 wrap field Field,
|
adamc@1207
|
1199 wrap known SqKnown,
|
adamc@1211
|
1200 wrap func SqFunc,
|
adamc@1211
|
1201 wrap (const "COUNT(*)") (fn () => Count),
|
adamc@1207
|
1202 wrap sqlify Inj,
|
adamc@1211
|
1203 wrap (follow (const "COALESCE(") (follow sqexp (follow (const ",")
|
adamc@1211
|
1204 (follow (keep (fn ch => ch <> #")")) (const ")")))))
|
adamc@1211
|
1205 (fn ((), (e, _)) => e),
|
adamc@1207
|
1206 wrap (follow (ws (const "("))
|
adamc@1207
|
1207 (follow (wrap
|
adamc@1207
|
1208 (follow sqexp
|
adamc@1207
|
1209 (alt
|
adamc@1207
|
1210 (wrap
|
adamc@1207
|
1211 (follow (ws sqbrel)
|
adamc@1207
|
1212 (ws sqexp))
|
adamc@1207
|
1213 inl)
|
adamc@1207
|
1214 (always (inr ()))))
|
adamc@1207
|
1215 (fn (e1, sm) =>
|
adamc@1207
|
1216 case sm of
|
adamc@1207
|
1217 inl (bo, e2) => Binop (bo, e1, e2)
|
adamc@1207
|
1218 | inr () => e1))
|
adamc@1207
|
1219 (const ")")))
|
adamc@1207
|
1220 (fn ((), (e, ())) => e)])
|
adamc@1207
|
1221 chs
|
adamc@1206
|
1222
|
adamc@1207
|
1223 and known chs = wrap (follow known' (follow (const "(") (follow sqexp (const ")"))))
|
adamc@1211
|
1224 (fn ((), ((), (e, ()))) => e) chs
|
adamc@1211
|
1225
|
adamc@1211
|
1226 and func chs = wrap (follow funcName (follow (const "(") (follow sqexp (const ")"))))
|
adamc@1211
|
1227 (fn (f, ((), (e, ()))) => (f, e)) chs
|
adamc@1211
|
1228
|
adamc@1211
|
1229 datatype sitem =
|
adamc@1211
|
1230 SqField of string * string
|
adamc@1211
|
1231 | SqExp of sqexp * string
|
adamc@1211
|
1232
|
adamc@1211
|
1233 val sitem = alt (wrap field SqField)
|
adamc@1211
|
1234 (wrap (follow sqexp (follow (const " AS ") uw_ident))
|
adamc@1211
|
1235 (fn (e, ((), s)) => SqExp (e, s)))
|
adamc@1207
|
1236
|
adamc@1207
|
1237 val select = log "select"
|
adamc@1207
|
1238 (wrap (follow (const "SELECT ") (list sitem))
|
adamc@1207
|
1239 (fn ((), ls) => ls))
|
adamc@1201
|
1240
|
adamc@1201
|
1241 val fitem = wrap (follow uw_ident
|
adamc@1201
|
1242 (follow (const " AS ")
|
adamc@1201
|
1243 t_ident))
|
adamc@1201
|
1244 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
1245
|
adamc@1207
|
1246 val from = log "from"
|
adamc@1207
|
1247 (wrap (follow (const "FROM ") (list fitem))
|
adamc@1207
|
1248 (fn ((), ls) => ls))
|
adamc@1201
|
1249
|
adamc@1204
|
1250 val wher = wrap (follow (ws (const "WHERE ")) sqexp)
|
adamc@1204
|
1251 (fn ((), ls) => ls)
|
adamc@1204
|
1252
|
adamc@1207
|
1253 val query = log "query"
|
adamc@1207
|
1254 (wrap (follow (follow select from) (opt wher))
|
adamc@1207
|
1255 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
|
adamc@1201
|
1256
|
adamc@1220
|
1257 datatype dml =
|
adamc@1220
|
1258 Insert of string * (string * sqexp) list
|
adamc@1221
|
1259 | Delete of string * sqexp
|
adamc@1220
|
1260
|
adamc@1220
|
1261 val insert = log "insert"
|
adamc@1220
|
1262 (wrapP (follow (const "INSERT INTO ")
|
adamc@1220
|
1263 (follow uw_ident
|
adamc@1220
|
1264 (follow (const " (")
|
adamc@1220
|
1265 (follow (list uw_ident)
|
adamc@1220
|
1266 (follow (const ") VALUES (")
|
adamc@1220
|
1267 (follow (list sqexp)
|
adamc@1220
|
1268 (const ")")))))))
|
adamc@1220
|
1269 (fn ((), (tab, ((), (fs, ((), (es, ())))))) =>
|
adamc@1221
|
1270 (SOME (tab, ListPair.zipEq (fs, es)))
|
adamc@1220
|
1271 handle ListPair.UnequalLengths => NONE))
|
adamc@1220
|
1272
|
adamc@1221
|
1273 val delete = log "delete"
|
adamc@1221
|
1274 (wrap (follow (const "DELETE FROM ")
|
adamc@1221
|
1275 (follow uw_ident
|
adamc@1221
|
1276 (follow (const " AS T_T WHERE ")
|
adamc@1221
|
1277 sqexp)))
|
adamc@1221
|
1278 (fn ((), (tab, ((), es))) => (tab, es)))
|
adamc@1221
|
1279
|
adamc@1220
|
1280 val dml = log "dml"
|
adamc@1221
|
1281 (altL [wrap insert Insert,
|
adamc@1221
|
1282 wrap delete Delete])
|
adamc@1220
|
1283
|
adamc@1215
|
1284 fun removeDups (ls : (string * string) list) =
|
adamc@1211
|
1285 case ls of
|
adamc@1211
|
1286 [] => []
|
adamc@1211
|
1287 | x :: ls =>
|
adamc@1211
|
1288 let
|
adamc@1211
|
1289 val ls = removeDups ls
|
adamc@1211
|
1290 in
|
adamc@1211
|
1291 if List.exists (fn x' => x' = x) ls then
|
adamc@1211
|
1292 ls
|
adamc@1211
|
1293 else
|
adamc@1211
|
1294 x :: ls
|
adamc@1211
|
1295 end
|
adamc@1211
|
1296
|
adamc@1212
|
1297 datatype queryMode =
|
adamc@1218
|
1298 SomeCol
|
adamc@1212
|
1299 | AllCols of exp
|
adamc@1212
|
1300
|
adamc@1220
|
1301 fun expIn rv env rvOf =
|
adamc@1220
|
1302 let
|
adamc@1220
|
1303 fun expIn (e, rvN) =
|
adamc@1220
|
1304 let
|
adamc@1220
|
1305 fun default () =
|
adamc@1220
|
1306 let
|
adamc@1220
|
1307 val (rvN, e) = rv rvN
|
adamc@1220
|
1308 in
|
adamc@1220
|
1309 (inl e, rvN)
|
adamc@1220
|
1310 end
|
adamc@1220
|
1311 in
|
adamc@1220
|
1312 case e of
|
adamc@1220
|
1313 SqConst p => (inl (Const p), rvN)
|
adamc@1220
|
1314 | Field (v, f) => (inl (Proj (rvOf v, f)), rvN)
|
adamc@1220
|
1315 | Binop (bo, e1, e2) =>
|
adamc@1220
|
1316 let
|
adamc@1220
|
1317 val (e1, rvN) = expIn (e1, rvN)
|
adamc@1220
|
1318 val (e2, rvN) = expIn (e2, rvN)
|
adamc@1220
|
1319 in
|
adamc@1220
|
1320 (inr (case (bo, e1, e2) of
|
adamc@1220
|
1321 (Exps f, inl e1, inl e2) => f (e1, e2)
|
adamc@1220
|
1322 | (Props f, inr p1, inr p2) => f (p1, p2)
|
adamc@1220
|
1323 | _ => Unknown), rvN)
|
adamc@1220
|
1324 end
|
adamc@1220
|
1325 | SqKnown e =>
|
adamc@1220
|
1326 (case expIn (e, rvN) of
|
adamc@1220
|
1327 (inl e, rvN) => (inr (Reln (Known, [e])), rvN)
|
adamc@1220
|
1328 | _ => (inr Unknown, rvN))
|
adamc@1220
|
1329 | Inj e =>
|
adamc@1220
|
1330 let
|
adamc@1220
|
1331 fun deinj e =
|
adamc@1220
|
1332 case #1 e of
|
adamc@1220
|
1333 ERel n => (List.nth (env, n), rvN)
|
adamc@1220
|
1334 | EField (e, f) =>
|
adamc@1220
|
1335 let
|
adamc@1220
|
1336 val (e, rvN) = deinj e
|
adamc@1220
|
1337 in
|
adamc@1220
|
1338 (Proj (e, f), rvN)
|
adamc@1220
|
1339 end
|
adamc@1220
|
1340 | _ =>
|
adamc@1220
|
1341 let
|
adamc@1220
|
1342 val (rvN, e) = rv rvN
|
adamc@1220
|
1343 in
|
adamc@1220
|
1344 (e, rvN)
|
adamc@1220
|
1345 end
|
adamc@1220
|
1346
|
adamc@1220
|
1347 val (e, rvN) = deinj e
|
adamc@1220
|
1348 in
|
adamc@1220
|
1349 (inl e, rvN)
|
adamc@1220
|
1350 end
|
adamc@1220
|
1351 | SqFunc (f, e) =>
|
adamc@1220
|
1352 (case expIn (e, rvN) of
|
adamc@1220
|
1353 (inl e, rvN) => (inl (Func (Other f, [e])), rvN)
|
adamc@1220
|
1354 | _ => default ())
|
adamc@1220
|
1355
|
adamc@1220
|
1356 | Count => default ()
|
adamc@1220
|
1357 end
|
adamc@1220
|
1358 in
|
adamc@1220
|
1359 expIn
|
adamc@1220
|
1360 end
|
adamc@1216
|
1361
|
adamc@1214
|
1362 fun queryProp env rvN rv oe e =
|
adamc@1216
|
1363 let
|
adamc@1216
|
1364 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1216
|
1365 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1218
|
1366 (rvN, Unknown, Unknown, [], []))
|
adamc@1216
|
1367 in
|
adamc@1216
|
1368 case parse query e of
|
adamc@1216
|
1369 NONE => default ()
|
adamc@1216
|
1370 | SOME r =>
|
adamc@1216
|
1371 let
|
adamc@1216
|
1372 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1216
|
1373 let
|
adamc@1216
|
1374 val (rvN, e) = rv rvN
|
adamc@1216
|
1375 in
|
adamc@1216
|
1376 ((v, e), rvN)
|
adamc@1216
|
1377 end) rvN (#From r)
|
adamc@1214
|
1378
|
adamc@1216
|
1379 fun rvOf v =
|
adamc@1216
|
1380 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1216
|
1381 NONE => raise Fail "Iflow.queryProp: Bad table variable"
|
adamc@1216
|
1382 | SOME (_, e) => e
|
adamc@1214
|
1383
|
adamc@1216
|
1384 fun usedFields e =
|
adamc@1216
|
1385 case e of
|
adamc@1216
|
1386 SqConst _ => []
|
adamc@1216
|
1387 | Field (v, f) => [(v, f)]
|
adamc@1216
|
1388 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
|
adamc@1216
|
1389 | SqKnown _ => []
|
adamc@1216
|
1390 | Inj _ => []
|
adamc@1216
|
1391 | SqFunc (_, e) => usedFields e
|
adamc@1216
|
1392 | Count => []
|
adamc@1214
|
1393
|
adamc@1216
|
1394 val p =
|
adamc@1216
|
1395 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
|
adamc@1211
|
1396
|
adamc@1220
|
1397 val expIn = expIn rv env rvOf
|
adamc@1205
|
1398
|
adamc@1220
|
1399 val (p, rvN) = case #Where r of
|
adamc@1220
|
1400 NONE => (p, rvN)
|
adamc@1220
|
1401 | SOME e =>
|
adamc@1220
|
1402 case expIn (e, rvN) of
|
adamc@1220
|
1403 (inr p', rvN) => (And (p, p'), rvN)
|
adamc@1220
|
1404 | _ => (p, rvN)
|
adamc@1210
|
1405
|
adamc@1216
|
1406 fun normal () =
|
adamc@1218
|
1407 case oe of
|
adamc@1218
|
1408 SomeCol =>
|
adamc@1220
|
1409 let
|
adamc@1220
|
1410 val (sis, rvN) =
|
adamc@1220
|
1411 ListUtil.foldlMap
|
adamc@1220
|
1412 (fn (si, rvN) =>
|
adamc@1220
|
1413 case si of
|
adamc@1220
|
1414 SqField (v, f) => (Proj (rvOf v, f), rvN)
|
adamc@1220
|
1415 | SqExp (e, f) =>
|
adamc@1220
|
1416 case expIn (e, rvN) of
|
adamc@1220
|
1417 (inr _, _) =>
|
adamc@1220
|
1418 let
|
adamc@1220
|
1419 val (rvN, e) = rv rvN
|
adamc@1220
|
1420 in
|
adamc@1220
|
1421 (e, rvN)
|
adamc@1220
|
1422 end
|
adamc@1220
|
1423 | (inl e, rvN) => (e, rvN)) rvN (#Select r)
|
adamc@1220
|
1424 in
|
adamc@1220
|
1425 (rvN, p, True, sis)
|
adamc@1220
|
1426 end
|
adamc@1218
|
1427 | AllCols oe =>
|
adamc@1220
|
1428 let
|
adamc@1222
|
1429 val (ts, es, rvN) =
|
adamc@1222
|
1430 foldl (fn (si, (ts, es, rvN)) =>
|
adamc@1222
|
1431 case si of
|
adamc@1222
|
1432 SqField (v, f) =>
|
adamc@1222
|
1433 let
|
adamc@1222
|
1434 val fs = getOpt (SM.find (ts, v), SM.empty)
|
adamc@1222
|
1435 in
|
adamc@1222
|
1436 (SM.insert (ts, v, SM.insert (fs, f, Proj (rvOf v, f))), es, rvN)
|
adamc@1222
|
1437 end
|
adamc@1222
|
1438 | SqExp (e, f) =>
|
adamc@1222
|
1439 let
|
adamc@1222
|
1440 val (e, rvN) =
|
adamc@1220
|
1441 case expIn (e, rvN) of
|
adamc@1222
|
1442 (inr _, rvN) =>
|
adamc@1222
|
1443 let
|
adamc@1222
|
1444 val (rvN, e) = rv rvN
|
adamc@1222
|
1445 in
|
adamc@1222
|
1446 (e, rvN)
|
adamc@1222
|
1447 end
|
adamc@1222
|
1448 | (inl e, rvN) => (e, rvN)
|
adamc@1222
|
1449 in
|
adamc@1222
|
1450 (ts, SM.insert (es, f, e), rvN)
|
adamc@1222
|
1451 end)
|
adamc@1222
|
1452 (SM.empty, SM.empty, rvN) (#Select r)
|
adamc@1222
|
1453
|
adamc@1222
|
1454 val p' = Reln (Eq, [oe, Recd (map (fn (t, fs) => (t, Recd (SM.listItemsi fs)))
|
adamc@1222
|
1455 (SM.listItemsi ts)
|
adamc@1222
|
1456 @ SM.listItemsi es)])
|
adamc@1220
|
1457 in
|
adamc@1220
|
1458 (rvN, And (p, p'), True, [])
|
adamc@1220
|
1459 end
|
adamc@1217
|
1460
|
adamc@1218
|
1461 val (rvN, p, wp, outs) =
|
adamc@1217
|
1462 case #Select r of
|
adamc@1217
|
1463 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
|
adamc@1217
|
1464 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
|
adamc@1217
|
1465 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
|
adamc@1218
|
1466 (case oe of
|
adamc@1218
|
1467 SomeCol =>
|
adamc@1218
|
1468 let
|
adamc@1218
|
1469 val (rvN, oe) = rv rvN
|
adamc@1218
|
1470 in
|
adamc@1218
|
1471 (rvN,
|
adamc@1218
|
1472 Or (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.False", [])]),
|
adamc@1218
|
1473 And (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1474 p)),
|
adamc@1218
|
1475 Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1476 [oe])
|
adamc@1218
|
1477 end
|
adamc@1218
|
1478 | AllCols oe =>
|
adamc@1218
|
1479 let
|
adamc@1221
|
1480 fun oeEq e = Reln (Eq, [oe, Recd [(f, e)]])
|
adamc@1218
|
1481 in
|
adamc@1218
|
1482 (rvN,
|
adamc@1221
|
1483 Or (oeEq (Func (DtCon0 "Basis.bool.False", [])),
|
adamc@1221
|
1484 And (oeEq (Func (DtCon0 "Basis.bool.True", [])),
|
adamc@1218
|
1485 p)),
|
adamc@1221
|
1486 oeEq (Func (DtCon0 "Basis.bool.True", [])),
|
adamc@1218
|
1487 [])
|
adamc@1218
|
1488 end)
|
adamc@1217
|
1489 | _ => normal ())
|
adamc@1217
|
1490 | _ => normal ()
|
adamc@1216
|
1491 in
|
adamc@1217
|
1492 (rvN, p, wp, case #Where r of
|
adamc@1217
|
1493 NONE => []
|
adamc@1218
|
1494 | SOME e => map (fn (v, f) => Proj (rvOf v, f)) (usedFields e), outs)
|
adamc@1216
|
1495 end
|
adamc@1220
|
1496 end
|
adamc@1220
|
1497
|
adamc@1220
|
1498 fun insertProp rvN rv e =
|
adamc@1220
|
1499 let
|
adamc@1220
|
1500 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1220
|
1501 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1220
|
1502 Unknown)
|
adamc@1220
|
1503 in
|
adamc@1220
|
1504 case parse query e of
|
adamc@1220
|
1505 NONE => default ()
|
adamc@1220
|
1506 | SOME r =>
|
adamc@1220
|
1507 let
|
adamc@1220
|
1508 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1220
|
1509 let
|
adamc@1220
|
1510 val (rvN, e) = rv rvN
|
adamc@1220
|
1511 in
|
adamc@1220
|
1512 ((v, e), rvN)
|
adamc@1220
|
1513 end) rvN (#From r)
|
adamc@1220
|
1514
|
adamc@1220
|
1515 fun rvOf v =
|
adamc@1220
|
1516 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1220
|
1517 NONE => raise Fail "Iflow.insertProp: Bad table variable"
|
adamc@1220
|
1518 | SOME (_, e) => e
|
adamc@1220
|
1519
|
adamc@1220
|
1520 val p =
|
adamc@1220
|
1521 foldl (fn ((t, v), p) =>
|
adamc@1220
|
1522 let
|
adamc@1220
|
1523 val t =
|
adamc@1220
|
1524 case v of
|
adamc@1220
|
1525 "New" => "$New"
|
adamc@1220
|
1526 | _ => t
|
adamc@1220
|
1527 in
|
adamc@1220
|
1528 And (p, Reln (Sql t, [rvOf v]))
|
adamc@1220
|
1529 end) True (#From r)
|
adamc@1220
|
1530
|
adamc@1220
|
1531 val expIn = expIn rv [] rvOf
|
adamc@1220
|
1532 in
|
adamc@1220
|
1533 case #Where r of
|
adamc@1220
|
1534 NONE => p
|
adamc@1220
|
1535 | SOME e =>
|
adamc@1220
|
1536 case expIn (e, rvN) of
|
adamc@1220
|
1537 (inr p', _) => And (p, p')
|
adamc@1220
|
1538 | _ => p
|
adamc@1220
|
1539 end
|
adamc@1216
|
1540 end
|
adamc@1200
|
1541
|
adamc@1221
|
1542 fun deleteProp rvN rv e =
|
adamc@1221
|
1543 let
|
adamc@1221
|
1544 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1221
|
1545 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1221
|
1546 Unknown)
|
adamc@1221
|
1547 in
|
adamc@1221
|
1548 case parse query e of
|
adamc@1221
|
1549 NONE => default ()
|
adamc@1221
|
1550 | SOME r =>
|
adamc@1221
|
1551 let
|
adamc@1221
|
1552 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1221
|
1553 let
|
adamc@1221
|
1554 val (rvN, e) = rv rvN
|
adamc@1221
|
1555 in
|
adamc@1221
|
1556 ((v, e), rvN)
|
adamc@1221
|
1557 end) rvN (#From r)
|
adamc@1221
|
1558
|
adamc@1221
|
1559 fun rvOf v =
|
adamc@1221
|
1560 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1221
|
1561 NONE => raise Fail "Iflow.deleteProp: Bad table variable"
|
adamc@1221
|
1562 | SOME (_, e) => e
|
adamc@1221
|
1563
|
adamc@1221
|
1564 val p =
|
adamc@1221
|
1565 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
|
adamc@1221
|
1566
|
adamc@1221
|
1567 val expIn = expIn rv [] rvOf
|
adamc@1221
|
1568 in
|
adamc@1221
|
1569 And (Reln (Sql "$Old", [rvOf "Old"]),
|
adamc@1221
|
1570 case #Where r of
|
adamc@1221
|
1571 NONE => p
|
adamc@1221
|
1572 | SOME e =>
|
adamc@1221
|
1573 case expIn (e, rvN) of
|
adamc@1221
|
1574 (inr p', _) => And (p, p')
|
adamc@1221
|
1575 | _ => p)
|
adamc@1221
|
1576 end
|
adamc@1221
|
1577 end
|
adamc@1221
|
1578
|
adamc@1211
|
1579 fun evalPat env e (pt, _) =
|
adamc@1211
|
1580 case pt of
|
adamc@1211
|
1581 PWild => (env, True)
|
adamc@1211
|
1582 | PVar _ => (e :: env, True)
|
adamc@1211
|
1583 | PPrim _ => (env, True)
|
adamc@1215
|
1584 | PCon (_, pc, NONE) => (env, Reln (PCon0 (patCon pc), [e]))
|
adamc@1211
|
1585 | PCon (_, pc, SOME pt) =>
|
adamc@1211
|
1586 let
|
adamc@1215
|
1587 val (env, p) = evalPat env (Func (UnCon (patCon pc), [e])) pt
|
adamc@1211
|
1588 in
|
adamc@1215
|
1589 (env, And (p, Reln (PCon1 (patCon pc), [e])))
|
adamc@1211
|
1590 end
|
adamc@1211
|
1591 | PRecord xpts =>
|
adamc@1211
|
1592 foldl (fn ((x, pt, _), (env, p)) =>
|
adamc@1211
|
1593 let
|
adamc@1211
|
1594 val (env, p') = evalPat env (Proj (e, x)) pt
|
adamc@1211
|
1595 in
|
adamc@1211
|
1596 (env, And (p', p))
|
adamc@1211
|
1597 end) (env, True) xpts
|
adamc@1215
|
1598 | PNone _ => (env, Reln (PCon0 "None", [e]))
|
adamc@1211
|
1599 | PSome (_, pt) =>
|
adamc@1211
|
1600 let
|
adamc@1215
|
1601 val (env, p) = evalPat env (Func (UnCon "Some", [e])) pt
|
adamc@1211
|
1602 in
|
adamc@1215
|
1603 (env, And (p, Reln (PCon1 "Some", [e])))
|
adamc@1211
|
1604 end
|
adamc@1211
|
1605
|
adamc@1211
|
1606 fun peq (p1, p2) =
|
adamc@1211
|
1607 case (p1, p2) of
|
adamc@1211
|
1608 (True, True) => true
|
adamc@1211
|
1609 | (False, False) => true
|
adamc@1211
|
1610 | (Unknown, Unknown) => true
|
adamc@1211
|
1611 | (And (x1, y1), And (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
|
adamc@1211
|
1612 | (Or (x1, y1), Or (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
|
adamc@1211
|
1613 | (Reln (r1, es1), Reln (r2, es2)) => r1 = r2 andalso ListPair.allEq eeq (es1, es2)
|
adamc@1212
|
1614 | (Cond (e1, p1), Cond (e2, p2)) => eeq (e1, e2) andalso peq (p1, p2)
|
adamc@1211
|
1615 | _ => false
|
adamc@1211
|
1616
|
adamc@1211
|
1617 fun removeRedundant p1 =
|
adamc@1211
|
1618 let
|
adamc@1211
|
1619 fun rr p2 =
|
adamc@1211
|
1620 if peq (p1, p2) then
|
adamc@1211
|
1621 True
|
adamc@1211
|
1622 else
|
adamc@1211
|
1623 case p2 of
|
adamc@1211
|
1624 And (x, y) => And (rr x, rr y)
|
adamc@1211
|
1625 | Or (x, y) => Or (rr x, rr y)
|
adamc@1211
|
1626 | _ => p2
|
adamc@1211
|
1627 in
|
adamc@1211
|
1628 rr
|
adamc@1211
|
1629 end
|
adamc@1211
|
1630
|
adamc@1218
|
1631 datatype cflow = Case | Where
|
adamc@1218
|
1632 datatype flow = Data | Control of cflow
|
adamc@1218
|
1633 type check = ErrorMsg.span * exp * prop
|
adamc@1221
|
1634 type dml = ErrorMsg.span * prop
|
adamc@1218
|
1635
|
adamc@1217
|
1636 structure St :> sig
|
adamc@1217
|
1637 type t
|
adamc@1217
|
1638 val create : {Var : int,
|
adamc@1217
|
1639 Ambient : prop} -> t
|
adamc@1217
|
1640
|
adamc@1217
|
1641 val curVar : t -> int
|
adamc@1217
|
1642 val nextVar : t -> t * int
|
adamc@1217
|
1643
|
adamc@1217
|
1644 val ambient : t -> prop
|
adamc@1217
|
1645 val setAmbient : t * prop -> t
|
adamc@1217
|
1646
|
adamc@1218
|
1647 val paths : t -> (check * cflow) list
|
adamc@1218
|
1648 val addPath : t * (check * cflow) -> t
|
adamc@1218
|
1649 val addPaths : t * (check * cflow) list -> t
|
adamc@1218
|
1650 val clearPaths : t -> t
|
adamc@1218
|
1651 val setPaths : t * (check * cflow) list -> t
|
adamc@1217
|
1652
|
adamc@1218
|
1653 val sent : t -> (check * flow) list
|
adamc@1218
|
1654 val addSent : t * (check * flow) -> t
|
adamc@1218
|
1655 val setSent : t * (check * flow) list -> t
|
adamc@1220
|
1656
|
adamc@1221
|
1657 val inserted : t -> dml list
|
adamc@1221
|
1658 val addInsert : t * dml -> t
|
adamc@1221
|
1659
|
adamc@1221
|
1660 val deleted : t -> dml list
|
adamc@1221
|
1661 val addDelete : t * dml -> t
|
adamc@1217
|
1662 end = struct
|
adamc@1217
|
1663
|
adamc@1217
|
1664 type t = {Var : int,
|
adamc@1217
|
1665 Ambient : prop,
|
adamc@1218
|
1666 Path : (check * cflow) list,
|
adamc@1220
|
1667 Sent : (check * flow) list,
|
adamc@1221
|
1668 Insert : dml list,
|
adamc@1221
|
1669 Delete : dml list}
|
adamc@1217
|
1670
|
adamc@1217
|
1671 fun create {Var = v, Ambient = p} = {Var = v,
|
adamc@1217
|
1672 Ambient = p,
|
adamc@1217
|
1673 Path = [],
|
adamc@1220
|
1674 Sent = [],
|
adamc@1221
|
1675 Insert = [],
|
adamc@1221
|
1676 Delete = []}
|
adamc@1217
|
1677
|
adamc@1217
|
1678 fun curVar (t : t) = #Var t
|
adamc@1217
|
1679 fun nextVar (t : t) = ({Var = #Var t + 1,
|
adamc@1217
|
1680 Ambient = #Ambient t,
|
adamc@1217
|
1681 Path = #Path t,
|
adamc@1220
|
1682 Sent = #Sent t,
|
adamc@1221
|
1683 Insert = #Insert t,
|
adamc@1221
|
1684 Delete = #Delete t}, #Var t)
|
adamc@1217
|
1685
|
adamc@1217
|
1686 fun ambient (t : t) = #Ambient t
|
adamc@1217
|
1687 fun setAmbient (t : t, p) = {Var = #Var t,
|
adamc@1217
|
1688 Ambient = p,
|
adamc@1217
|
1689 Path = #Path t,
|
adamc@1220
|
1690 Sent = #Sent t,
|
adamc@1221
|
1691 Insert = #Insert t,
|
adamc@1221
|
1692 Delete = #Delete t}
|
adamc@1217
|
1693
|
adamc@1218
|
1694 fun paths (t : t) = #Path t
|
adamc@1217
|
1695 fun addPath (t : t, c) = {Var = #Var t,
|
adamc@1217
|
1696 Ambient = #Ambient t,
|
adamc@1217
|
1697 Path = c :: #Path t,
|
adamc@1220
|
1698 Sent = #Sent t,
|
adamc@1221
|
1699 Insert = #Insert t,
|
adamc@1221
|
1700 Delete = #Delete t}
|
adamc@1218
|
1701 fun addPaths (t : t, cs) = {Var = #Var t,
|
adamc@1218
|
1702 Ambient = #Ambient t,
|
adamc@1218
|
1703 Path = cs @ #Path t,
|
adamc@1220
|
1704 Sent = #Sent t,
|
adamc@1221
|
1705 Insert = #Insert t,
|
adamc@1221
|
1706 Delete = #Delete t}
|
adamc@1218
|
1707 fun clearPaths (t : t) = {Var = #Var t,
|
adamc@1218
|
1708 Ambient = #Ambient t,
|
adamc@1218
|
1709 Path = [],
|
adamc@1220
|
1710 Sent = #Sent t,
|
adamc@1221
|
1711 Insert = #Insert t,
|
adamc@1221
|
1712 Delete = #Delete t}
|
adamc@1218
|
1713 fun setPaths (t : t, cs) = {Var = #Var t,
|
adamc@1218
|
1714 Ambient = #Ambient t,
|
adamc@1218
|
1715 Path = cs,
|
adamc@1220
|
1716 Sent = #Sent t,
|
adamc@1221
|
1717 Insert = #Insert t,
|
adamc@1221
|
1718 Delete = #Delete t}
|
adamc@1217
|
1719
|
adamc@1217
|
1720 fun sent (t : t) = #Sent t
|
adamc@1217
|
1721 fun addSent (t : t, c) = {Var = #Var t,
|
adamc@1217
|
1722 Ambient = #Ambient t,
|
adamc@1217
|
1723 Path = #Path t,
|
adamc@1220
|
1724 Sent = c :: #Sent t,
|
adamc@1221
|
1725 Insert = #Insert t,
|
adamc@1221
|
1726 Delete = #Delete t}
|
adamc@1217
|
1727 fun setSent (t : t, cs) = {Var = #Var t,
|
adamc@1217
|
1728 Ambient = #Ambient t,
|
adamc@1217
|
1729 Path = #Path t,
|
adamc@1220
|
1730 Sent = cs,
|
adamc@1221
|
1731 Insert = #Insert t,
|
adamc@1221
|
1732 Delete = #Delete t}
|
adamc@1220
|
1733
|
adamc@1220
|
1734 fun inserted (t : t) = #Insert t
|
adamc@1220
|
1735 fun addInsert (t : t, c) = {Var = #Var t,
|
adamc@1220
|
1736 Ambient = #Ambient t,
|
adamc@1220
|
1737 Path = #Path t,
|
adamc@1220
|
1738 Sent = #Sent t,
|
adamc@1221
|
1739 Insert = c :: #Insert t,
|
adamc@1221
|
1740 Delete = #Delete t}
|
adamc@1221
|
1741
|
adamc@1221
|
1742 fun deleted (t : t) = #Delete t
|
adamc@1221
|
1743 fun addDelete (t : t, c) = {Var = #Var t,
|
adamc@1221
|
1744 Ambient = #Ambient t,
|
adamc@1221
|
1745 Path = #Path t,
|
adamc@1221
|
1746 Sent = #Sent t,
|
adamc@1221
|
1747 Insert = #Insert t,
|
adamc@1221
|
1748 Delete = c :: #Delete t}
|
adamc@1217
|
1749
|
adamc@1217
|
1750 end
|
adamc@1217
|
1751
|
adamc@1217
|
1752 fun evalExp env (e as (_, loc), st) =
|
adamc@1200
|
1753 let
|
adamc@1200
|
1754 fun default () =
|
adamc@1217
|
1755 let
|
adamc@1217
|
1756 val (st, nv) = St.nextVar st
|
adamc@1217
|
1757 in
|
adamc@1217
|
1758 (Var nv, st)
|
adamc@1217
|
1759 end
|
adamc@1200
|
1760
|
adamc@1217
|
1761 fun addSent (p, e, st) =
|
adamc@1218
|
1762 let
|
adamc@1218
|
1763 val st = if isKnown e then
|
adamc@1218
|
1764 st
|
adamc@1218
|
1765 else
|
adamc@1218
|
1766 St.addSent (st, ((loc, e, p), Data))
|
adamc@1218
|
1767
|
adamc@1218
|
1768 val st = foldl (fn ((c, fl), st) => St.addSent (st, (c, Control fl))) st (St.paths st)
|
adamc@1218
|
1769 in
|
adamc@1218
|
1770 St.clearPaths st
|
adamc@1218
|
1771 end
|
adamc@1200
|
1772 in
|
adamc@1200
|
1773 case #1 e of
|
adamc@1200
|
1774 EPrim p => (Const p, st)
|
adamc@1200
|
1775 | ERel n => (List.nth (env, n), st)
|
adamc@1200
|
1776 | ENamed _ => default ()
|
adamc@1215
|
1777 | ECon (_, pc, NONE) => (Func (DtCon0 (patCon pc), []), st)
|
adamc@1200
|
1778 | ECon (_, pc, SOME e) =>
|
adamc@1200
|
1779 let
|
adamc@1200
|
1780 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1781 in
|
adamc@1215
|
1782 (Func (DtCon1 (patCon pc), [e]), st)
|
adamc@1200
|
1783 end
|
adamc@1215
|
1784 | ENone _ => (Func (DtCon0 "None", []), st)
|
adamc@1200
|
1785 | ESome (_, e) =>
|
adamc@1200
|
1786 let
|
adamc@1200
|
1787 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1788 in
|
adamc@1215
|
1789 (Func (DtCon1 "Some", [e]), st)
|
adamc@1200
|
1790 end
|
adamc@1200
|
1791 | EFfi _ => default ()
|
adamc@1213
|
1792
|
adamc@1200
|
1793 | EFfiApp (m, s, es) =>
|
adamc@1200
|
1794 if m = "Basis" andalso SS.member (writers, s) then
|
adamc@1200
|
1795 let
|
adamc@1200
|
1796 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1797 in
|
adamc@1217
|
1798 (Recd [], foldl (fn (e, st) => addSent (St.ambient st, e, st)) st es)
|
adamc@1200
|
1799 end
|
adamc@1200
|
1800 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
|
adamc@1200
|
1801 default ()
|
adamc@1200
|
1802 else
|
adamc@1200
|
1803 let
|
adamc@1200
|
1804 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1805 in
|
adamc@1215
|
1806 (Func (Other (m ^ "." ^ s), es), st)
|
adamc@1200
|
1807 end
|
adamc@1213
|
1808
|
adamc@1213
|
1809 | EApp (e1, e2) =>
|
adamc@1213
|
1810 let
|
adamc@1213
|
1811 val (e1, st) = evalExp env (e1, st)
|
adamc@1213
|
1812 in
|
adamc@1213
|
1813 case e1 of
|
adamc@1213
|
1814 Finish => (Finish, st)
|
adamc@1213
|
1815 | _ => default ()
|
adamc@1213
|
1816 end
|
adamc@1213
|
1817
|
adamc@1200
|
1818 | EAbs _ => default ()
|
adamc@1200
|
1819 | EUnop (s, e1) =>
|
adamc@1200
|
1820 let
|
adamc@1200
|
1821 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1822 in
|
adamc@1215
|
1823 (Func (Other s, [e1]), st)
|
adamc@1200
|
1824 end
|
adamc@1200
|
1825 | EBinop (s, e1, e2) =>
|
adamc@1200
|
1826 let
|
adamc@1200
|
1827 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1828 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
1829 in
|
adamc@1215
|
1830 (Func (Other s, [e1, e2]), st)
|
adamc@1200
|
1831 end
|
adamc@1200
|
1832 | ERecord xets =>
|
adamc@1200
|
1833 let
|
adamc@1200
|
1834 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
|
adamc@1200
|
1835 let
|
adamc@1200
|
1836 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1837 in
|
adamc@1200
|
1838 ((x, e), st)
|
adamc@1200
|
1839 end) st xets
|
adamc@1200
|
1840 in
|
adamc@1200
|
1841 (Recd xes, st)
|
adamc@1200
|
1842 end
|
adamc@1200
|
1843 | EField (e, s) =>
|
adamc@1200
|
1844 let
|
adamc@1200
|
1845 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1846 in
|
adamc@1200
|
1847 (Proj (e, s), st)
|
adamc@1200
|
1848 end
|
adamc@1218
|
1849 | ECase (e, pes, {result = res, ...}) =>
|
adamc@1211
|
1850 let
|
adamc@1211
|
1851 val (e, st) = evalExp env (e, st)
|
adamc@1217
|
1852 val (st, r) = St.nextVar st
|
adamc@1217
|
1853 val orig = St.ambient st
|
adamc@1218
|
1854 val origPaths = St.paths st
|
adamc@1211
|
1855
|
adamc@1218
|
1856 val st = St.addPath (st, ((loc, e, orig), Case))
|
adamc@1218
|
1857
|
adamc@1218
|
1858 val (st, paths) =
|
adamc@1218
|
1859 foldl (fn ((pt, pe), (st, paths)) =>
|
adamc@1218
|
1860 let
|
adamc@1218
|
1861 val (env, pp) = evalPat env e pt
|
adamc@1218
|
1862 val (pe, st') = evalExp env (pe, St.setAmbient (st, And (orig, pp)))
|
adamc@1218
|
1863
|
adamc@1218
|
1864 val this = And (removeRedundant orig (St.ambient st'),
|
adamc@1218
|
1865 Reln (Eq, [Var r, pe]))
|
adamc@1218
|
1866 in
|
adamc@1218
|
1867 (St.setPaths (St.setAmbient (st', Or (St.ambient st, this)), origPaths),
|
adamc@1218
|
1868 St.paths st' @ paths)
|
adamc@1218
|
1869 end) (St.setAmbient (st, False), []) pes
|
adamc@1218
|
1870
|
adamc@1218
|
1871 val st = case #1 res of
|
adamc@1218
|
1872 TRecord [] => St.setPaths (st, origPaths)
|
adamc@1218
|
1873 | _ => St.setPaths (st, paths)
|
adamc@1211
|
1874 in
|
adamc@1217
|
1875 (Var r, St.setAmbient (st, And (orig, St.ambient st)))
|
adamc@1211
|
1876 end
|
adamc@1200
|
1877 | EStrcat (e1, e2) =>
|
adamc@1200
|
1878 let
|
adamc@1200
|
1879 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1880 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
1881 in
|
adamc@1215
|
1882 (Func (Other "cat", [e1, e2]), st)
|
adamc@1200
|
1883 end
|
adamc@1200
|
1884 | EError _ => (Finish, st)
|
adamc@1200
|
1885 | EReturnBlob {blob = b, mimeType = m, ...} =>
|
adamc@1200
|
1886 let
|
adamc@1200
|
1887 val (b, st) = evalExp env (b, st)
|
adamc@1200
|
1888 val (m, st) = evalExp env (m, st)
|
adamc@1200
|
1889 in
|
adamc@1217
|
1890 (Finish, addSent (St.ambient st, b, addSent (St.ambient st, m, st)))
|
adamc@1200
|
1891 end
|
adamc@1200
|
1892 | ERedirect (e, _) =>
|
adamc@1200
|
1893 let
|
adamc@1200
|
1894 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1895 in
|
adamc@1217
|
1896 (Finish, addSent (St.ambient st, e, st))
|
adamc@1200
|
1897 end
|
adamc@1200
|
1898 | EWrite e =>
|
adamc@1200
|
1899 let
|
adamc@1200
|
1900 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1901 in
|
adamc@1217
|
1902 (Recd [], addSent (St.ambient st, e, st))
|
adamc@1200
|
1903 end
|
adamc@1200
|
1904 | ESeq (e1, e2) =>
|
adamc@1200
|
1905 let
|
adamc@1200
|
1906 val (_, st) = evalExp env (e1, st)
|
adamc@1200
|
1907 in
|
adamc@1200
|
1908 evalExp env (e2, st)
|
adamc@1200
|
1909 end
|
adamc@1200
|
1910 | ELet (_, _, e1, e2) =>
|
adamc@1200
|
1911 let
|
adamc@1200
|
1912 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1913 in
|
adamc@1200
|
1914 evalExp (e1 :: env) (e2, st)
|
adamc@1200
|
1915 end
|
adamc@1200
|
1916 | EClosure (n, es) =>
|
adamc@1200
|
1917 let
|
adamc@1200
|
1918 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1919 in
|
adamc@1215
|
1920 (Func (Other ("Cl" ^ Int.toString n), es), st)
|
adamc@1200
|
1921 end
|
adamc@1200
|
1922
|
adamc@1200
|
1923 | EQuery {query = q, body = b, initial = i, ...} =>
|
adamc@1200
|
1924 let
|
adamc@1200
|
1925 val (_, st) = evalExp env (q, st)
|
adamc@1200
|
1926 val (i, st) = evalExp env (i, st)
|
adamc@1200
|
1927
|
adamc@1217
|
1928 val (st', r) = St.nextVar st
|
adamc@1217
|
1929 val (st', acc) = St.nextVar st'
|
adamc@1200
|
1930
|
adamc@1200
|
1931 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
|
adamc@1200
|
1932
|
adamc@1218
|
1933 val (st', qp, qwp, used, _) =
|
adamc@1214
|
1934 queryProp env
|
adamc@1217
|
1935 st' (fn st' =>
|
adamc@1217
|
1936 let
|
adamc@1217
|
1937 val (st', rv) = St.nextVar st'
|
adamc@1217
|
1938 in
|
adamc@1217
|
1939 (st', Var rv)
|
adamc@1217
|
1940 end)
|
adamc@1214
|
1941 (AllCols (Var r)) q
|
adamc@1200
|
1942
|
adamc@1217
|
1943 val p' = And (qp, St.ambient st')
|
adamc@1200
|
1944
|
adamc@1217
|
1945 val (st, res) = if varInP acc (St.ambient st') then
|
adamc@1217
|
1946 let
|
adamc@1217
|
1947 val (st, r) = St.nextVar st
|
adamc@1217
|
1948 in
|
adamc@1217
|
1949 (st, Var r)
|
adamc@1217
|
1950 end
|
adamc@1217
|
1951 else
|
adamc@1217
|
1952 let
|
adamc@1217
|
1953 val (st, out) = St.nextVar st'
|
adamc@1217
|
1954
|
adamc@1217
|
1955 val p = Or (Reln (Eq, [Var out, i]),
|
adamc@1217
|
1956 And (Reln (Eq, [Var out, b]),
|
adamc@1217
|
1957 p'))
|
adamc@1217
|
1958 in
|
adamc@1217
|
1959 (St.setAmbient (st, p), Var out)
|
adamc@1217
|
1960 end
|
adamc@1210
|
1961
|
adamc@1218
|
1962 val sent = map (fn ((loc, e, p), fl) => ((loc, e, And (qp, p)), fl)) (St.sent st')
|
adamc@1212
|
1963
|
adamc@1217
|
1964 val p' = And (p', qwp)
|
adamc@1218
|
1965 val paths = map (fn e => ((loc, e, p'), Where)) used
|
adamc@1200
|
1966 in
|
adamc@1218
|
1967 (res, St.addPaths (St.setSent (st, sent), paths))
|
adamc@1200
|
1968 end
|
adamc@1220
|
1969 | EDml e =>
|
adamc@1220
|
1970 (case parse dml e of
|
adamc@1220
|
1971 NONE => (print ("Warning: Information flow checker can't parse DML command at "
|
adamc@1220
|
1972 ^ ErrorMsg.spanToString loc ^ "\n");
|
adamc@1220
|
1973 default ())
|
adamc@1220
|
1974 | SOME d =>
|
adamc@1220
|
1975 case d of
|
adamc@1220
|
1976 Insert (tab, es) =>
|
adamc@1220
|
1977 let
|
adamc@1220
|
1978 val (st, new) = St.nextVar st
|
adamc@1220
|
1979
|
adamc@1220
|
1980 fun rv st =
|
adamc@1220
|
1981 let
|
adamc@1220
|
1982 val (st, n) = St.nextVar st
|
adamc@1220
|
1983 in
|
adamc@1220
|
1984 (st, Var n)
|
adamc@1220
|
1985 end
|
adamc@1220
|
1986
|
adamc@1220
|
1987 val expIn = expIn rv env (fn "New" => Var new
|
adamc@1221
|
1988 | _ => raise Fail "Iflow.evalExp: Bad field expression in UPDATE")
|
adamc@1220
|
1989
|
adamc@1220
|
1990 val (es, st) = ListUtil.foldlMap
|
adamc@1220
|
1991 (fn ((x, e), st) =>
|
adamc@1220
|
1992 let
|
adamc@1220
|
1993 val (e, st) = case expIn (e, st) of
|
adamc@1220
|
1994 (inl e, st) => (e, st)
|
adamc@1220
|
1995 | (inr _, _) => raise Fail
|
adamc@1220
|
1996 ("Iflow.evalExp: Selecting "
|
adamc@1220
|
1997 ^ "boolean expression")
|
adamc@1220
|
1998 in
|
adamc@1220
|
1999 ((x, e), st)
|
adamc@1220
|
2000 end)
|
adamc@1220
|
2001 st es
|
adamc@1220
|
2002 in
|
adamc@1220
|
2003 (Recd [], St.addInsert (st, (loc, And (St.ambient st,
|
adamc@1220
|
2004 Reln (Sql "$New", [Recd es])))))
|
adamc@1221
|
2005 end
|
adamc@1221
|
2006 | Delete (tab, e) =>
|
adamc@1221
|
2007 let
|
adamc@1221
|
2008 val (st, old) = St.nextVar st
|
adamc@1221
|
2009
|
adamc@1221
|
2010 fun rv st =
|
adamc@1221
|
2011 let
|
adamc@1221
|
2012 val (st, n) = St.nextVar st
|
adamc@1221
|
2013 in
|
adamc@1221
|
2014 (st, Var n)
|
adamc@1221
|
2015 end
|
adamc@1221
|
2016
|
adamc@1221
|
2017 val expIn = expIn rv env (fn "T" => Var old
|
adamc@1221
|
2018 | _ => raise Fail "Iflow.evalExp: Bad field expression in DELETE")
|
adamc@1221
|
2019
|
adamc@1221
|
2020 val (p, st) = case expIn (e, st) of
|
adamc@1221
|
2021 (inl e, _) => raise Fail "Iflow.evalExp: DELETE with non-boolean"
|
adamc@1221
|
2022 | (inr p, st) => (p, st)
|
adamc@1221
|
2023
|
adamc@1221
|
2024 val p = And (p,
|
adamc@1221
|
2025 And (Reln (Sql "$Old", [Var old]),
|
adamc@1221
|
2026 Reln (Sql tab, [Var old])))
|
adamc@1221
|
2027 in
|
adamc@1221
|
2028 (Recd [], St.addDelete (st, (loc, And (St.ambient st, p))))
|
adamc@1220
|
2029 end)
|
adamc@1220
|
2030
|
adamc@1200
|
2031 | ENextval _ => default ()
|
adamc@1200
|
2032 | ESetval _ => default ()
|
adamc@1200
|
2033
|
adamc@1213
|
2034 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
|
adamc@1217
|
2035 let
|
adamc@1217
|
2036 val (st, nv) = St.nextVar st
|
adamc@1217
|
2037 in
|
adamc@1217
|
2038 (Var nv, St.setAmbient (st, And (St.ambient st, Reln (Known, [Var nv]))))
|
adamc@1217
|
2039 end
|
adamc@1213
|
2040
|
adamc@1200
|
2041 | EUnurlify _ => default ()
|
adamc@1200
|
2042 | EJavaScript _ => default ()
|
adamc@1200
|
2043 | ESignalReturn _ => default ()
|
adamc@1200
|
2044 | ESignalBind _ => default ()
|
adamc@1200
|
2045 | ESignalSource _ => default ()
|
adamc@1200
|
2046 | EServerCall _ => default ()
|
adamc@1200
|
2047 | ERecv _ => default ()
|
adamc@1200
|
2048 | ESleep _ => default ()
|
adamc@1200
|
2049 | ESpawn _ => default ()
|
adamc@1200
|
2050 end
|
adamc@1200
|
2051
|
adamc@1200
|
2052 fun check file =
|
adamc@1200
|
2053 let
|
adamc@1213
|
2054 val file = MonoReduce.reduce file
|
adamc@1213
|
2055 val file = MonoOpt.optimize file
|
adamc@1213
|
2056 val file = Fuse.fuse file
|
adamc@1213
|
2057 val file = MonoOpt.optimize file
|
adamc@1216
|
2058 val file = MonoShake.shake file
|
adamc@1213
|
2059 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
|
adamc@1213
|
2060
|
adamc@1207
|
2061 val exptd = foldl (fn ((d, _), exptd) =>
|
adamc@1207
|
2062 case d of
|
adamc@1207
|
2063 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
|
adamc@1207
|
2064 | _ => exptd) IS.empty file
|
adamc@1207
|
2065
|
adamc@1221
|
2066 fun decl ((d, _), (vals, inserts, deletes, client, insert, delete)) =
|
adamc@1200
|
2067 case d of
|
adamc@1207
|
2068 DVal (_, n, _, e, _) =>
|
adamc@1200
|
2069 let
|
adamc@1207
|
2070 val isExptd = IS.member (exptd, n)
|
adamc@1207
|
2071
|
adamc@1207
|
2072 fun deAbs (e, env, nv, p) =
|
adamc@1200
|
2073 case #1 e of
|
adamc@1207
|
2074 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1,
|
adamc@1207
|
2075 if isExptd then
|
adamc@1207
|
2076 And (p, Reln (Known, [Var nv]))
|
adamc@1207
|
2077 else
|
adamc@1207
|
2078 p)
|
adamc@1207
|
2079 | _ => (e, env, nv, p)
|
adamc@1200
|
2080
|
adamc@1207
|
2081 val (e, env, nv, p) = deAbs (e, [], 1, True)
|
adamc@1200
|
2082
|
adamc@1217
|
2083 val (_, st) = evalExp env (e, St.create {Var = nv,
|
adamc@1217
|
2084 Ambient = p})
|
adamc@1200
|
2085 in
|
adamc@1221
|
2086 (St.sent st @ vals, St.inserted st @ inserts, St.deleted st @ deletes, client, insert, delete)
|
adamc@1200
|
2087 end
|
adamc@1202
|
2088
|
adamc@1220
|
2089 | DPolicy pol =>
|
adamc@1218
|
2090 let
|
adamc@1220
|
2091 fun rv rvN = (rvN + 1, Lvar rvN)
|
adamc@1218
|
2092 in
|
adamc@1220
|
2093 case pol of
|
adamc@1220
|
2094 PolClient e =>
|
adamc@1220
|
2095 let
|
adamc@1220
|
2096 val (_, p, _, _, outs) = queryProp [] 0 rv SomeCol e
|
adamc@1220
|
2097 in
|
adamc@1221
|
2098 (vals, inserts, deletes, (p, outs) :: client, insert, delete)
|
adamc@1220
|
2099 end
|
adamc@1220
|
2100 | PolInsert e =>
|
adamc@1220
|
2101 let
|
adamc@1220
|
2102 val p = insertProp 0 rv e
|
adamc@1220
|
2103 in
|
adamc@1221
|
2104 (vals, inserts, deletes, client, p :: insert, delete)
|
adamc@1221
|
2105 end
|
adamc@1221
|
2106 | PolDelete e =>
|
adamc@1221
|
2107 let
|
adamc@1221
|
2108 val p = deleteProp 0 rv e
|
adamc@1221
|
2109 in
|
adamc@1221
|
2110 (vals, inserts, deletes, client, insert, p :: delete)
|
adamc@1220
|
2111 end
|
adamc@1218
|
2112 end
|
adamc@1214
|
2113
|
adamc@1221
|
2114 | _ => (vals, inserts, deletes, client, insert, delete)
|
adamc@1202
|
2115
|
adamc@1203
|
2116 val () = reset ()
|
adamc@1202
|
2117
|
adamc@1221
|
2118 val (vals, inserts, deletes, client, insert, delete) = foldl decl ([], [], [], [], [], []) file
|
adamc@1220
|
2119
|
adamc@1220
|
2120 val decompH = decomp true (fn (e1, e2) => e1 andalso e2 ())
|
adamc@1220
|
2121 val decompG = decomp false (fn (e1, e2) => e1 orelse e2 ())
|
adamc@1221
|
2122
|
adamc@1221
|
2123 fun doDml (cmds, pols) =
|
adamc@1221
|
2124 app (fn (loc, p) =>
|
adamc@1221
|
2125 if decompH p
|
adamc@1221
|
2126 (fn hyps =>
|
adamc@1221
|
2127 List.exists (fn p' =>
|
adamc@1221
|
2128 decompG p'
|
adamc@1221
|
2129 (fn goals => imply (hyps, goals, NONE)))
|
adamc@1221
|
2130 pols) then
|
adamc@1221
|
2131 ()
|
adamc@1221
|
2132 else
|
adamc@1221
|
2133 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
|
adamc@1221
|
2134 Print.preface ("The state satisifies this predicate:", p_prop p))) cmds
|
adamc@1200
|
2135 in
|
adamc@1218
|
2136 app (fn ((loc, e, p), fl) =>
|
adamc@1207
|
2137 let
|
adamc@1213
|
2138 fun doOne e =
|
adamc@1213
|
2139 let
|
adamc@1213
|
2140 val p = And (p, Reln (Eq, [Var 0, e]))
|
adamc@1213
|
2141 in
|
adamc@1220
|
2142 if decompH p
|
adamc@1220
|
2143 (fn hyps =>
|
adamc@1220
|
2144 (fl <> Control Where
|
adamc@1220
|
2145 andalso imply (hyps, [AReln (Known, [Var 0])], SOME [Var 0]))
|
adamc@1220
|
2146 orelse List.exists (fn (p', outs) =>
|
adamc@1220
|
2147 decompG p'
|
adamc@1220
|
2148 (fn goals => imply (hyps, goals, SOME outs)))
|
adamc@1220
|
2149 client) then
|
adamc@1213
|
2150 ()
|
adamc@1213
|
2151 else
|
adamc@1213
|
2152 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
|
adamc@1216
|
2153 Print.preface ("The state satisifies this predicate:", p_prop p))
|
adamc@1213
|
2154 end
|
adamc@1213
|
2155
|
adamc@1213
|
2156 fun doAll e =
|
adamc@1213
|
2157 case e of
|
adamc@1213
|
2158 Const _ => ()
|
adamc@1213
|
2159 | Var _ => doOne e
|
adamc@1213
|
2160 | Lvar _ => raise Fail "Iflow.doAll: Lvar"
|
adamc@1216
|
2161 | Func (UnCon _, [_]) => doOne e
|
adamc@1215
|
2162 | Func (_, es) => app doAll es
|
adamc@1213
|
2163 | Recd xes => app (doAll o #2) xes
|
adamc@1213
|
2164 | Proj _ => doOne e
|
adamc@1213
|
2165 | Finish => ()
|
adamc@1207
|
2166 in
|
adamc@1213
|
2167 doAll e
|
adamc@1220
|
2168 end) vals;
|
adamc@1220
|
2169
|
adamc@1221
|
2170 doDml (inserts, insert);
|
adamc@1221
|
2171 doDml (deletes, delete)
|
adamc@1200
|
2172 end
|
adamc@1200
|
2173
|
adamc@1213
|
2174 val check = fn file =>
|
adamc@1213
|
2175 let
|
adamc@1213
|
2176 val oldInline = Settings.getMonoInline ()
|
adamc@1213
|
2177 in
|
adamc@1213
|
2178 (Settings.setMonoInline (case Int.maxInt of
|
adamc@1213
|
2179 NONE => 1000000
|
adamc@1213
|
2180 | SOME n => n);
|
adamc@1213
|
2181 check file;
|
adamc@1213
|
2182 Settings.setMonoInline oldInline)
|
adamc@1213
|
2183 handle ex => (Settings.setMonoInline oldInline;
|
adamc@1213
|
2184 raise ex)
|
adamc@1213
|
2185 end
|
adamc@1213
|
2186
|
adamc@1200
|
2187 end
|
adamc@1213
|
2188
|