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@1215
|
414 | Recrd of node ref SM.map ref
|
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@1215
|
449 case #Variety (unNode n) of
|
adamc@1215
|
450 Nothing => string ("?" ^ Int.toString (Unsafe.cast n))
|
adamc@1215
|
451 | Dt0 s => string ("Dt0(" ^ s ^ ")")
|
adamc@1215
|
452 | Dt1 (s, n) => box[string ("Dt1(" ^ s ^ ","),
|
adamc@1215
|
453 space,
|
adamc@1215
|
454 p_rep n,
|
adamc@1215
|
455 string ")"]
|
adamc@1215
|
456 | Prim p => Prim.p_t p
|
adamc@1215
|
457 | Recrd (ref m) => box [string "{",
|
adamc@1215
|
458 p_list (fn (x, n) => box [string x,
|
adamc@1215
|
459 space,
|
adamc@1215
|
460 string "=",
|
adamc@1215
|
461 space,
|
adamc@1215
|
462 p_rep n]) (SM.listItemsi m),
|
adamc@1215
|
463 string "}"]
|
adamc@1215
|
464 | VFinish => string "FINISH"
|
adamc@1215
|
465
|
adamc@1215
|
466 fun p_database (db : database) =
|
adamc@1215
|
467 box [string "Vars:",
|
adamc@1215
|
468 newline,
|
adamc@1215
|
469 p_list_sep newline (fn (i, n) => box [string ("x" ^ Int.toString i),
|
adamc@1215
|
470 space,
|
adamc@1215
|
471 string "=",
|
adamc@1215
|
472 space,
|
adamc@1218
|
473 p_rep n,
|
adamc@1218
|
474 if !(#Known (unNode n)) then
|
adamc@1218
|
475 box [space,
|
adamc@1218
|
476 string "(known)"]
|
adamc@1218
|
477 else
|
adamc@1218
|
478 box []]) (IM.listItemsi (!(#Vars db)))]
|
adamc@1215
|
479
|
adamc@1215
|
480 fun repOf (n : representative) : representative =
|
adamc@1215
|
481 case !(#Rep (unNode n)) of
|
adamc@1215
|
482 NONE => n
|
adamc@1215
|
483 | SOME r =>
|
adamc@1215
|
484 let
|
adamc@1215
|
485 val r = repOf r
|
adamc@1215
|
486 in
|
adamc@1215
|
487 #Rep (unNode n) := SOME r;
|
adamc@1215
|
488 r
|
adamc@1215
|
489 end
|
adamc@1215
|
490
|
adamc@1215
|
491 fun markKnown r =
|
adamc@1218
|
492 if !(#Known (unNode r)) then
|
adamc@1218
|
493 ()
|
adamc@1218
|
494 else
|
adamc@1218
|
495 (#Known (unNode r) := true;
|
adamc@1218
|
496 SM.app markKnown (!(#Cons (unNode r)));
|
adamc@1218
|
497 case #Variety (unNode r) of
|
adamc@1218
|
498 Dt1 (_, r) => markKnown r
|
adamc@1218
|
499 | Recrd xes => SM.app markKnown (!xes)
|
adamc@1218
|
500 | _ => ())
|
adamc@1215
|
501
|
adamc@1215
|
502 fun representative (db : database, e) =
|
adamc@1208
|
503 let
|
adamc@1215
|
504 fun rep e =
|
adamc@1215
|
505 case e of
|
adamc@1215
|
506 Const p => (case CM.find (!(#Consts db), p) of
|
adamc@1215
|
507 SOME r => repOf r
|
adamc@1215
|
508 | NONE =>
|
adamc@1215
|
509 let
|
adamc@1215
|
510 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
511 Cons = ref SM.empty,
|
adamc@1215
|
512 Variety = Prim p,
|
adamc@1215
|
513 Known = ref false})
|
adamc@1215
|
514 in
|
adamc@1215
|
515 #Consts db := CM.insert (!(#Consts db), p, r);
|
adamc@1215
|
516 r
|
adamc@1215
|
517 end)
|
adamc@1215
|
518 | Var n => (case IM.find (!(#Vars db), n) 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 = Nothing,
|
adamc@1215
|
525 Known = ref false})
|
adamc@1215
|
526 in
|
adamc@1215
|
527 #Vars db := IM.insert (!(#Vars db), n, r);
|
adamc@1215
|
528 r
|
adamc@1215
|
529 end)
|
adamc@1215
|
530 | Lvar n =>
|
adamc@1215
|
531 (case IM.find (!unif, n) of
|
adamc@1215
|
532 NONE => raise Undetermined
|
adamc@1215
|
533 | SOME e => rep e)
|
adamc@1215
|
534 | Func (DtCon0 f, []) => (case SM.find (!(#Con0s db), f) of
|
adamc@1215
|
535 SOME r => repOf r
|
adamc@1215
|
536 | NONE =>
|
adamc@1215
|
537 let
|
adamc@1215
|
538 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
539 Cons = ref SM.empty,
|
adamc@1215
|
540 Variety = Dt0 f,
|
adamc@1218
|
541 Known = ref true})
|
adamc@1215
|
542 in
|
adamc@1215
|
543 #Con0s db := SM.insert (!(#Con0s db), f, r);
|
adamc@1215
|
544 r
|
adamc@1215
|
545 end)
|
adamc@1215
|
546 | Func (DtCon0 _, _) => raise Fail "Iflow.rep: DtCon0"
|
adamc@1215
|
547 | Func (DtCon1 f, [e]) =>
|
adamc@1215
|
548 let
|
adamc@1215
|
549 val r = rep e
|
adamc@1215
|
550 in
|
adamc@1215
|
551 case SM.find (!(#Cons (unNode r)), f) of
|
adamc@1215
|
552 SOME r => repOf r
|
adamc@1215
|
553 | NONE =>
|
adamc@1215
|
554 let
|
adamc@1215
|
555 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
556 Cons = ref SM.empty,
|
adamc@1215
|
557 Variety = Dt1 (f, r),
|
adamc@1216
|
558 Known = #Known (unNode r)})
|
adamc@1215
|
559 in
|
adamc@1215
|
560 #Cons (unNode r) := SM.insert (!(#Cons (unNode r)), f, r');
|
adamc@1215
|
561 r'
|
adamc@1215
|
562 end
|
adamc@1215
|
563 end
|
adamc@1215
|
564 | Func (DtCon1 _, _) => raise Fail "Iflow.rep: DtCon1"
|
adamc@1215
|
565 | Func (UnCon f, [e]) =>
|
adamc@1215
|
566 let
|
adamc@1215
|
567 val r = rep e
|
adamc@1215
|
568 in
|
adamc@1215
|
569 case #Variety (unNode r) of
|
adamc@1215
|
570 Dt1 (f', n) => if f' = f then
|
adamc@1215
|
571 repOf n
|
adamc@1215
|
572 else
|
adamc@1215
|
573 raise Contradiction
|
adamc@1215
|
574 | Nothing =>
|
adamc@1215
|
575 let
|
adamc@1215
|
576 val cons = ref SM.empty
|
adamc@1215
|
577 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
578 Cons = cons,
|
adamc@1215
|
579 Variety = Nothing,
|
adamc@1216
|
580 Known = #Known (unNode r)})
|
adamc@1215
|
581
|
adamc@1215
|
582 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
583 Cons = #Cons (unNode r),
|
adamc@1215
|
584 Variety = Dt1 (f, r'),
|
adamc@1215
|
585 Known = #Known (unNode r)})
|
adamc@1215
|
586 in
|
adamc@1215
|
587 cons := SM.insert (!cons, f, r'');
|
adamc@1215
|
588 #Rep (unNode r) := SOME r'';
|
adamc@1215
|
589 r'
|
adamc@1215
|
590 end
|
adamc@1215
|
591 | VFinish => r
|
adamc@1215
|
592 | _ => raise Contradiction
|
adamc@1215
|
593 end
|
adamc@1215
|
594 | Func (UnCon _, _) => raise Fail "Iflow.rep: UnCon"
|
adamc@1215
|
595 | Func (Other f, es) =>
|
adamc@1215
|
596 let
|
adamc@1215
|
597 val rs = map rep es
|
adamc@1215
|
598 in
|
adamc@1215
|
599 case List.find (fn (x : string * representative list, _) => x = (f, rs)) (!(#Funcs db)) of
|
adamc@1215
|
600 NONE =>
|
adamc@1215
|
601 let
|
adamc@1215
|
602 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
603 Cons = ref SM.empty,
|
adamc@1215
|
604 Variety = Nothing,
|
adamc@1215
|
605 Known = ref false})
|
adamc@1215
|
606 in
|
adamc@1215
|
607 #Funcs db := ((f, rs), r) :: (!(#Funcs db));
|
adamc@1215
|
608 r
|
adamc@1215
|
609 end
|
adamc@1215
|
610 | SOME (_, r) => repOf r
|
adamc@1215
|
611 end
|
adamc@1215
|
612 | Recd xes =>
|
adamc@1215
|
613 let
|
adamc@1215
|
614 val xes = map (fn (x, e) => (x, rep e)) xes
|
adamc@1215
|
615 val len = length xes
|
adamc@1215
|
616 in
|
adamc@1215
|
617 case List.find (fn (xes', _) =>
|
adamc@1215
|
618 SM.numItems xes' = len
|
adamc@1215
|
619 andalso List.all (fn (x, n) =>
|
adamc@1215
|
620 case SM.find (xes', x) of
|
adamc@1215
|
621 NONE => false
|
adamc@1215
|
622 | SOME n' => n = repOf n') xes)
|
adamc@1215
|
623 (!(#Records db)) of
|
adamc@1215
|
624 SOME (_, r) => repOf r
|
adamc@1215
|
625 | NONE =>
|
adamc@1215
|
626 let
|
adamc@1215
|
627 val xes = foldl SM.insert' SM.empty xes
|
adamc@1215
|
628
|
adamc@1215
|
629 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
630 Cons = ref SM.empty,
|
adamc@1215
|
631 Variety = Recrd (ref xes),
|
adamc@1215
|
632 Known = ref false})
|
adamc@1215
|
633 in
|
adamc@1215
|
634 #Records db := (xes, r') :: (!(#Records db));
|
adamc@1215
|
635 r'
|
adamc@1215
|
636 end
|
adamc@1215
|
637 end
|
adamc@1215
|
638 | Proj (e, f) =>
|
adamc@1215
|
639 let
|
adamc@1215
|
640 val r = rep e
|
adamc@1215
|
641 in
|
adamc@1215
|
642 case #Variety (unNode r) of
|
adamc@1215
|
643 Recrd xes =>
|
adamc@1215
|
644 (case SM.find (!xes, f) of
|
adamc@1215
|
645 SOME r => repOf r
|
adamc@1216
|
646 | NONE => let
|
adamc@1215
|
647 val r = ref (Node {Rep = ref NONE,
|
adamc@1215
|
648 Cons = ref SM.empty,
|
adamc@1215
|
649 Variety = Nothing,
|
adamc@1216
|
650 Known = #Known (unNode r)})
|
adamc@1215
|
651 in
|
adamc@1215
|
652 xes := SM.insert (!xes, f, r);
|
adamc@1215
|
653 r
|
adamc@1215
|
654 end)
|
adamc@1215
|
655 | Nothing =>
|
adamc@1215
|
656 let
|
adamc@1215
|
657 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
658 Cons = ref SM.empty,
|
adamc@1215
|
659 Variety = Nothing,
|
adamc@1216
|
660 Known = #Known (unNode r)})
|
adamc@1215
|
661
|
adamc@1215
|
662 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
663 Cons = #Cons (unNode r),
|
adamc@1215
|
664 Variety = Recrd (ref (SM.insert (SM.empty, f, r'))),
|
adamc@1215
|
665 Known = #Known (unNode r)})
|
adamc@1215
|
666 in
|
adamc@1215
|
667 #Rep (unNode r) := SOME r'';
|
adamc@1215
|
668 r'
|
adamc@1215
|
669 end
|
adamc@1215
|
670 | VFinish => r
|
adamc@1215
|
671 | _ => raise Contradiction
|
adamc@1215
|
672 end
|
adamc@1215
|
673 | Finish => finish
|
adamc@1208
|
674 in
|
adamc@1215
|
675 rep e
|
adamc@1208
|
676 end
|
adamc@1208
|
677
|
adamc@1215
|
678 fun assert (db, a) =
|
adamc@1215
|
679 case a of
|
adamc@1215
|
680 ACond _ => ()
|
adamc@1215
|
681 | AReln x =>
|
adamc@1215
|
682 case x of
|
adamc@1215
|
683 (Known, [e]) => markKnown (representative (db, e))
|
adamc@1215
|
684 | (PCon0 f, [e]) =>
|
adamc@1215
|
685 let
|
adamc@1215
|
686 val r = representative (db, e)
|
adamc@1215
|
687 in
|
adamc@1215
|
688 case #Variety (unNode r) of
|
adamc@1215
|
689 Dt0 f' => if f = f' then
|
adamc@1215
|
690 ()
|
adamc@1215
|
691 else
|
adamc@1215
|
692 raise Contradiction
|
adamc@1215
|
693 | Nothing =>
|
adamc@1215
|
694 let
|
adamc@1215
|
695 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
696 Cons = ref SM.empty,
|
adamc@1215
|
697 Variety = Dt0 f,
|
adamc@1215
|
698 Known = ref false})
|
adamc@1215
|
699 in
|
adamc@1215
|
700 #Rep (unNode r) := SOME r'
|
adamc@1215
|
701 end
|
adamc@1215
|
702 | _ => raise Contradiction
|
adamc@1215
|
703 end
|
adamc@1215
|
704 | (PCon1 f, [e]) =>
|
adamc@1215
|
705 let
|
adamc@1215
|
706 val r = representative (db, e)
|
adamc@1215
|
707 in
|
adamc@1215
|
708 case #Variety (unNode r) of
|
adamc@1215
|
709 Dt1 (f', e') => if f = f' then
|
adamc@1215
|
710 ()
|
adamc@1215
|
711 else
|
adamc@1215
|
712 raise Contradiction
|
adamc@1215
|
713 | Nothing =>
|
adamc@1215
|
714 let
|
adamc@1215
|
715 val r'' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
716 Cons = ref SM.empty,
|
adamc@1215
|
717 Variety = Nothing,
|
adamc@1215
|
718 Known = ref false})
|
adamc@1214
|
719
|
adamc@1215
|
720 val r' = ref (Node {Rep = ref NONE,
|
adamc@1215
|
721 Cons = ref SM.empty,
|
adamc@1215
|
722 Variety = Dt1 (f, r''),
|
adamc@1215
|
723 Known = ref false})
|
adamc@1215
|
724 in
|
adamc@1215
|
725 #Rep (unNode r) := SOME r'
|
adamc@1215
|
726 end
|
adamc@1215
|
727 | _ => raise Contradiction
|
adamc@1215
|
728 end
|
adamc@1215
|
729 | (Eq, [e1, e2]) =>
|
adamc@1215
|
730 let
|
adamc@1215
|
731 fun markEq (r1, r2) =
|
adamc@1215
|
732 if r1 = r2 then
|
adamc@1215
|
733 ()
|
adamc@1215
|
734 else case (#Variety (unNode r1), #Variety (unNode r2)) of
|
adamc@1215
|
735 (Prim p1, Prim p2) => if Prim.equal (p1, p2) then
|
adamc@1215
|
736 ()
|
adamc@1215
|
737 else
|
adamc@1215
|
738 raise Contradiction
|
adamc@1215
|
739 | (Dt0 f1, Dt0 f2) => if f1 = f2 then
|
adamc@1215
|
740 ()
|
adamc@1215
|
741 else
|
adamc@1215
|
742 raise Contradiction
|
adamc@1215
|
743 | (Dt1 (f1, r1), Dt1 (f2, r2)) => if f1 = f2 then
|
adamc@1215
|
744 markEq (r1, r2)
|
adamc@1215
|
745 else
|
adamc@1215
|
746 raise Contradiction
|
adamc@1215
|
747 | (Recrd xes1, Recrd xes2) =>
|
adamc@1215
|
748 let
|
adamc@1215
|
749 fun unif (xes1, xes2) =
|
adamc@1215
|
750 SM.appi (fn (x, r1) =>
|
adamc@1215
|
751 case SM.find (xes2, x) of
|
adamc@1215
|
752 NONE => ()
|
adamc@1215
|
753 | SOME r2 => markEq (r1, r2)) xes1
|
adamc@1215
|
754 in
|
adamc@1215
|
755 unif (!xes1, !xes2);
|
adamc@1215
|
756 unif (!xes2, !xes1)
|
adamc@1215
|
757 end
|
adamc@1215
|
758 | (VFinish, VFinish) => ()
|
adamc@1218
|
759 | (Nothing, _) => mergeNodes (r1, r2)
|
adamc@1218
|
760 | (_, Nothing) => mergeNodes (r2, r1)
|
adamc@1215
|
761 | _ => raise Contradiction
|
adamc@1214
|
762
|
adamc@1218
|
763 and mergeNodes (r1, r2) =
|
adamc@1218
|
764 (#Rep (unNode r1) := SOME r2;
|
adamc@1218
|
765 if !(#Known (unNode r1)) then
|
adamc@1218
|
766 markKnown r2
|
adamc@1218
|
767 else
|
adamc@1218
|
768 ();
|
adamc@1218
|
769 if !(#Known (unNode r2)) then
|
adamc@1218
|
770 markKnown r1
|
adamc@1218
|
771 else
|
adamc@1218
|
772 ();
|
adamc@1218
|
773 #Cons (unNode r2) := SM.unionWith #1 (!(#Cons (unNode r2)), !(#Cons (unNode r1)));
|
adamc@1218
|
774 compactFuncs ())
|
adamc@1218
|
775
|
adamc@1215
|
776 and compactFuncs () =
|
adamc@1214
|
777 let
|
adamc@1215
|
778 fun loop funcs =
|
adamc@1215
|
779 case funcs of
|
adamc@1215
|
780 [] => []
|
adamc@1215
|
781 | (fr as ((f, rs), r)) :: rest =>
|
adamc@1215
|
782 let
|
adamc@1215
|
783 val rest = List.filter (fn ((f' : string, rs'), r') =>
|
adamc@1215
|
784 if f' = f
|
adamc@1215
|
785 andalso ListPair.allEq (fn (r1, r2) =>
|
adamc@1215
|
786 repOf r1 = repOf r2)
|
adamc@1215
|
787 (rs, rs') then
|
adamc@1215
|
788 (markEq (r, r');
|
adamc@1215
|
789 false)
|
adamc@1215
|
790 else
|
adamc@1215
|
791 true) rest
|
adamc@1215
|
792 in
|
adamc@1215
|
793 fr :: loop rest
|
adamc@1215
|
794 end
|
adamc@1214
|
795 in
|
adamc@1215
|
796 #Funcs db := loop (!(#Funcs db))
|
adamc@1214
|
797 end
|
adamc@1215
|
798 in
|
adamc@1215
|
799 markEq (representative (db, e1), representative (db, e2))
|
adamc@1215
|
800 end
|
adamc@1215
|
801 | _ => ()
|
adamc@1214
|
802
|
adamc@1215
|
803 fun check (db, a) =
|
adamc@1215
|
804 case a of
|
adamc@1215
|
805 ACond _ => false
|
adamc@1215
|
806 | AReln x =>
|
adamc@1215
|
807 case x of
|
adamc@1215
|
808 (Known, [e]) => !(#Known (unNode (representative (db, e))))
|
adamc@1215
|
809 | (PCon0 f, [e]) =>
|
adamc@1215
|
810 (case #Variety (unNode (representative (db, e))) of
|
adamc@1215
|
811 Dt0 f' => f' = f
|
adamc@1215
|
812 | _ => false)
|
adamc@1215
|
813 | (PCon1 f, [e]) =>
|
adamc@1215
|
814 (case #Variety (unNode (representative (db, e))) of
|
adamc@1215
|
815 Dt1 (f', _) => f' = f
|
adamc@1215
|
816 | _ => false)
|
adamc@1215
|
817 | (Eq, [e1, e2]) =>
|
adamc@1214
|
818 let
|
adamc@1215
|
819 val r1 = representative (db, e1)
|
adamc@1215
|
820 val r2 = representative (db, e2)
|
adamc@1214
|
821 in
|
adamc@1215
|
822 repOf r1 = repOf r2
|
adamc@1214
|
823 end
|
adamc@1215
|
824 | _ => false
|
adamc@1212
|
825
|
adamc@1218
|
826 fun builtFrom (db, {Base = bs, Derived = d}) =
|
adamc@1218
|
827 let
|
adamc@1218
|
828 val bs = map (fn b => representative (db, b)) bs
|
adamc@1218
|
829
|
adamc@1218
|
830 fun loop d =
|
adamc@1218
|
831 let
|
adamc@1218
|
832 val d = repOf d
|
adamc@1218
|
833 in
|
adamc@1218
|
834 List.exists (fn b => repOf b = d) bs
|
adamc@1218
|
835 orelse case #Variety (unNode d) of
|
adamc@1218
|
836 Dt0 _ => true
|
adamc@1218
|
837 | Dt1 (_, d) => loop d
|
adamc@1218
|
838 | Prim _ => true
|
adamc@1218
|
839 | Recrd xes => List.all loop (SM.listItems (!xes))
|
adamc@1218
|
840 | VFinish => true
|
adamc@1218
|
841 | Nothing => false
|
adamc@1218
|
842 end
|
adamc@1218
|
843 in
|
adamc@1218
|
844 loop (representative (db, d))
|
adamc@1218
|
845 end
|
adamc@1218
|
846
|
adamc@1208
|
847 end
|
adamc@1208
|
848
|
adamc@1215
|
849 fun decomp fals or =
|
adamc@1215
|
850 let
|
adamc@1215
|
851 fun decomp p k =
|
adamc@1215
|
852 case p of
|
adamc@1215
|
853 True => k []
|
adamc@1215
|
854 | False => fals
|
adamc@1215
|
855 | Unknown => k []
|
adamc@1215
|
856 | And (p1, p2) =>
|
adamc@1215
|
857 decomp p1 (fn ps1 =>
|
adamc@1215
|
858 decomp p2 (fn ps2 =>
|
adamc@1215
|
859 k (ps1 @ ps2)))
|
adamc@1215
|
860 | Or (p1, p2) =>
|
adamc@1215
|
861 or (decomp p1 k, fn () => decomp p2 k)
|
adamc@1215
|
862 | Reln x => k [AReln x]
|
adamc@1215
|
863 | Cond x => k [ACond x]
|
adamc@1215
|
864 in
|
adamc@1215
|
865 decomp
|
adamc@1215
|
866 end
|
adamc@1202
|
867
|
adamc@1218
|
868 fun imply (hyps, goals, outs) =
|
adamc@1218
|
869 let
|
adamc@1218
|
870 fun gls goals onFail acc =
|
adamc@1218
|
871 case goals of
|
adamc@1218
|
872 [] =>
|
adamc@1218
|
873 (let
|
adamc@1218
|
874 val cc = Cc.database ()
|
adamc@1218
|
875 val () = app (fn a => Cc.assert (cc, a)) hyps
|
adamc@1218
|
876 in
|
adamc@1218
|
877 (List.all (fn a =>
|
adamc@1218
|
878 if Cc.check (cc, a) then
|
adamc@1218
|
879 true
|
adamc@1218
|
880 else
|
adamc@1218
|
881 ((*Print.prefaces "Can't prove"
|
adamc@1218
|
882 [("a", p_atom a),
|
adamc@1218
|
883 ("hyps", Print.p_list p_atom hyps),
|
adamc@1218
|
884 ("db", Cc.p_database cc)];*)
|
adamc@1218
|
885 false)) acc
|
adamc@1218
|
886 (*andalso (Print.preface ("Finding", Cc.p_database cc); true)*)
|
adamc@1218
|
887 andalso Cc.builtFrom (cc, {Derived = Var 0,
|
adamc@1218
|
888 Base = outs}))
|
adamc@1218
|
889 handle Cc.Contradiction => false
|
adamc@1218
|
890 end handle Cc.Undetermined => false)
|
adamc@1218
|
891 orelse onFail ()
|
adamc@1218
|
892 | (g as AReln (Sql gf, [ge])) :: goals =>
|
adamc@1218
|
893 let
|
adamc@1218
|
894 fun hps hyps =
|
adamc@1218
|
895 case hyps of
|
adamc@1218
|
896 [] => gls goals onFail (g :: acc)
|
adamc@1218
|
897 | (h as AReln (Sql hf, [he])) :: hyps =>
|
adamc@1218
|
898 if gf = hf then
|
adamc@1218
|
899 let
|
adamc@1218
|
900 val saved = save ()
|
adamc@1218
|
901 in
|
adamc@1218
|
902 if eq (ge, he) then
|
adamc@1218
|
903 let
|
adamc@1218
|
904 val changed = IM.numItems (!unif)
|
adamc@1218
|
905 <> IM.numItems saved
|
adamc@1218
|
906 in
|
adamc@1218
|
907 gls goals (fn () => (restore saved;
|
adamc@1218
|
908 changed
|
adamc@1218
|
909 andalso hps hyps))
|
adamc@1218
|
910 acc
|
adamc@1218
|
911 end
|
adamc@1218
|
912 else
|
adamc@1218
|
913 hps hyps
|
adamc@1218
|
914 end
|
adamc@1218
|
915 else
|
adamc@1218
|
916 hps hyps
|
adamc@1218
|
917 | _ :: hyps => hps hyps
|
adamc@1218
|
918 in
|
adamc@1218
|
919 hps hyps
|
adamc@1218
|
920 end
|
adamc@1218
|
921 | g :: goals => gls goals onFail (g :: acc)
|
adamc@1218
|
922 in
|
adamc@1218
|
923 reset ();
|
adamc@1218
|
924 (*Print.prefaces "Big go" [("hyps", Print.p_list p_atom hyps),
|
adamc@1218
|
925 ("goals", Print.p_list p_atom goals)];*)
|
adamc@1218
|
926 gls goals (fn () => false) []
|
adamc@1218
|
927 end handle Cc.Contradiction => true
|
adamc@1200
|
928
|
adamc@1200
|
929 fun patCon pc =
|
adamc@1200
|
930 case pc of
|
adamc@1200
|
931 PConVar n => "C" ^ Int.toString n
|
adamc@1200
|
932 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
|
adamc@1200
|
933
|
adamc@1200
|
934 datatype chunk =
|
adamc@1200
|
935 String of string
|
adamc@1200
|
936 | Exp of Mono.exp
|
adamc@1200
|
937
|
adamc@1200
|
938 fun chunkify e =
|
adamc@1200
|
939 case #1 e of
|
adamc@1200
|
940 EPrim (Prim.String s) => [String s]
|
adamc@1207
|
941 | EStrcat (e1, e2) =>
|
adamc@1207
|
942 let
|
adamc@1207
|
943 val chs1 = chunkify e1
|
adamc@1207
|
944 val chs2 = chunkify e2
|
adamc@1207
|
945 in
|
adamc@1207
|
946 case chs2 of
|
adamc@1207
|
947 String s2 :: chs2' =>
|
adamc@1207
|
948 (case List.last chs1 of
|
adamc@1207
|
949 String s1 => List.take (chs1, length chs1 - 1) @ String (s1 ^ s2) :: chs2'
|
adamc@1207
|
950 | _ => chs1 @ chs2)
|
adamc@1207
|
951 | _ => chs1 @ chs2
|
adamc@1207
|
952 end
|
adamc@1200
|
953 | _ => [Exp e]
|
adamc@1200
|
954
|
adamc@1201
|
955 type 'a parser = chunk list -> ('a * chunk list) option
|
adamc@1201
|
956
|
adamc@1201
|
957 fun always v chs = SOME (v, chs)
|
adamc@1201
|
958
|
adamc@1202
|
959 fun parse p s =
|
adamc@1202
|
960 case p (chunkify s) of
|
adamc@1201
|
961 SOME (v, []) => SOME v
|
adamc@1201
|
962 | _ => NONE
|
adamc@1201
|
963
|
adamc@1201
|
964 fun const s chs =
|
adamc@1201
|
965 case chs of
|
adamc@1201
|
966 String s' :: chs => if String.isPrefix s s' then
|
adamc@1201
|
967 SOME ((), if size s = size s' then
|
adamc@1201
|
968 chs
|
adamc@1201
|
969 else
|
adamc@1201
|
970 String (String.extract (s', size s, NONE)) :: chs)
|
adamc@1201
|
971 else
|
adamc@1201
|
972 NONE
|
adamc@1201
|
973 | _ => NONE
|
adamc@1201
|
974
|
adamc@1201
|
975 fun follow p1 p2 chs =
|
adamc@1201
|
976 case p1 chs of
|
adamc@1201
|
977 NONE => NONE
|
adamc@1201
|
978 | SOME (v1, chs) =>
|
adamc@1201
|
979 case p2 chs of
|
adamc@1201
|
980 NONE => NONE
|
adamc@1201
|
981 | SOME (v2, chs) => SOME ((v1, v2), chs)
|
adamc@1201
|
982
|
adamc@1201
|
983 fun wrap p f chs =
|
adamc@1201
|
984 case p chs of
|
adamc@1201
|
985 NONE => NONE
|
adamc@1201
|
986 | SOME (v, chs) => SOME (f v, chs)
|
adamc@1201
|
987
|
adamc@1209
|
988 fun wrapP p f chs =
|
adamc@1209
|
989 case p chs of
|
adamc@1209
|
990 NONE => NONE
|
adamc@1209
|
991 | SOME (v, chs) =>
|
adamc@1209
|
992 case f v of
|
adamc@1209
|
993 NONE => NONE
|
adamc@1209
|
994 | SOME r => SOME (r, chs)
|
adamc@1209
|
995
|
adamc@1201
|
996 fun alt p1 p2 chs =
|
adamc@1201
|
997 case p1 chs of
|
adamc@1201
|
998 NONE => p2 chs
|
adamc@1201
|
999 | v => v
|
adamc@1201
|
1000
|
adamc@1207
|
1001 fun altL ps =
|
adamc@1207
|
1002 case rev ps of
|
adamc@1207
|
1003 [] => (fn _ => NONE)
|
adamc@1207
|
1004 | p :: ps =>
|
adamc@1207
|
1005 foldl (fn (p1, p2) => alt p1 p2) p ps
|
adamc@1207
|
1006
|
adamc@1204
|
1007 fun opt p chs =
|
adamc@1204
|
1008 case p chs of
|
adamc@1204
|
1009 NONE => SOME (NONE, chs)
|
adamc@1204
|
1010 | SOME (v, chs) => SOME (SOME v, chs)
|
adamc@1204
|
1011
|
adamc@1201
|
1012 fun skip cp chs =
|
adamc@1201
|
1013 case chs of
|
adamc@1201
|
1014 String "" :: chs => skip cp chs
|
adamc@1201
|
1015 | String s :: chs' => if cp (String.sub (s, 0)) then
|
adamc@1201
|
1016 skip cp (String (String.extract (s, 1, NONE)) :: chs')
|
adamc@1201
|
1017 else
|
adamc@1201
|
1018 SOME ((), chs)
|
adamc@1201
|
1019 | _ => SOME ((), chs)
|
adamc@1201
|
1020
|
adamc@1201
|
1021 fun keep cp chs =
|
adamc@1201
|
1022 case chs of
|
adamc@1201
|
1023 String "" :: chs => keep cp chs
|
adamc@1201
|
1024 | String s :: chs' =>
|
adamc@1201
|
1025 let
|
adamc@1201
|
1026 val (befor, after) = Substring.splitl cp (Substring.full s)
|
adamc@1201
|
1027 in
|
adamc@1201
|
1028 if Substring.isEmpty befor then
|
adamc@1201
|
1029 NONE
|
adamc@1201
|
1030 else
|
adamc@1201
|
1031 SOME (Substring.string befor,
|
adamc@1201
|
1032 if Substring.isEmpty after then
|
adamc@1201
|
1033 chs'
|
adamc@1201
|
1034 else
|
adamc@1201
|
1035 String (Substring.string after) :: chs')
|
adamc@1201
|
1036 end
|
adamc@1201
|
1037 | _ => NONE
|
adamc@1201
|
1038
|
adamc@1204
|
1039 fun ws p = wrap (follow (skip (fn ch => ch = #" "))
|
adamc@1204
|
1040 (follow p (skip (fn ch => ch = #" ")))) (#1 o #2)
|
adamc@1204
|
1041
|
adamc@1204
|
1042 fun log name p chs =
|
adamc@1206
|
1043 (if !debug then
|
adamc@1206
|
1044 case chs of
|
adamc@1207
|
1045 String s :: _ => print (name ^ ": " ^ s ^ "\n")
|
adamc@1206
|
1046 | _ => print (name ^ ": blocked!\n")
|
adamc@1206
|
1047 else
|
adamc@1206
|
1048 ();
|
adamc@1204
|
1049 p chs)
|
adamc@1201
|
1050
|
adamc@1201
|
1051 fun list p chs =
|
adamc@1207
|
1052 altL [wrap (follow p (follow (ws (const ",")) (list p)))
|
adamc@1207
|
1053 (fn (v, ((), ls)) => v :: ls),
|
adamc@1207
|
1054 wrap (ws p) (fn v => [v]),
|
adamc@1207
|
1055 always []] chs
|
adamc@1201
|
1056
|
adamc@1201
|
1057 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
|
adamc@1201
|
1058
|
adamc@1211
|
1059 val t_ident = wrapP ident (fn s => if String.isPrefix "T_" s then
|
adamc@1211
|
1060 SOME (String.extract (s, 2, NONE))
|
adamc@1201
|
1061 else
|
adamc@1211
|
1062 NONE)
|
adamc@1211
|
1063 val uw_ident = wrapP ident (fn s => if String.isPrefix "uw_" s andalso size s >= 4 then
|
adamc@1211
|
1064 SOME (str (Char.toUpper (String.sub (s, 3)))
|
adamc@1211
|
1065 ^ String.extract (s, 4, NONE))
|
adamc@1211
|
1066 else
|
adamc@1211
|
1067 NONE)
|
adamc@1201
|
1068
|
adamc@1211
|
1069 val field = wrap (follow t_ident
|
adamc@1201
|
1070 (follow (const ".")
|
adamc@1201
|
1071 uw_ident))
|
adamc@1201
|
1072 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
1073
|
adamc@1206
|
1074 datatype Rel =
|
adamc@1206
|
1075 Exps of exp * exp -> prop
|
adamc@1206
|
1076 | Props of prop * prop -> prop
|
adamc@1206
|
1077
|
adamc@1204
|
1078 datatype sqexp =
|
adamc@1206
|
1079 SqConst of Prim.t
|
adamc@1206
|
1080 | Field of string * string
|
adamc@1206
|
1081 | Binop of Rel * sqexp * sqexp
|
adamc@1207
|
1082 | SqKnown of sqexp
|
adamc@1207
|
1083 | Inj of Mono.exp
|
adamc@1211
|
1084 | SqFunc of string * sqexp
|
adamc@1211
|
1085 | Count
|
adamc@1204
|
1086
|
adamc@1210
|
1087 fun cmp s r = wrap (const s) (fn () => Exps (fn (e1, e2) => Reln (r, [e1, e2])))
|
adamc@1210
|
1088
|
adamc@1210
|
1089 val sqbrel = altL [cmp "=" Eq,
|
adamc@1210
|
1090 cmp "<>" Ne,
|
adamc@1210
|
1091 cmp "<=" Le,
|
adamc@1210
|
1092 cmp "<" Lt,
|
adamc@1210
|
1093 cmp ">=" Ge,
|
adamc@1210
|
1094 cmp ">" Gt,
|
adamc@1207
|
1095 wrap (const "AND") (fn () => Props And),
|
adamc@1207
|
1096 wrap (const "OR") (fn () => Props Or)]
|
adamc@1204
|
1097
|
adamc@1204
|
1098 datatype ('a, 'b) sum = inl of 'a | inr of 'b
|
adamc@1204
|
1099
|
adamc@1209
|
1100 fun string chs =
|
adamc@1206
|
1101 case chs of
|
adamc@1209
|
1102 String s :: chs =>
|
adamc@1209
|
1103 if size s >= 2 andalso String.sub (s, 0) = #"'" then
|
adamc@1209
|
1104 let
|
adamc@1209
|
1105 fun loop (cs, acc) =
|
adamc@1209
|
1106 case cs of
|
adamc@1209
|
1107 [] => NONE
|
adamc@1209
|
1108 | c :: cs =>
|
adamc@1209
|
1109 if c = #"'" then
|
adamc@1209
|
1110 SOME (String.implode (rev acc), cs)
|
adamc@1209
|
1111 else if c = #"\\" then
|
adamc@1209
|
1112 case cs of
|
adamc@1209
|
1113 c :: cs => loop (cs, c :: acc)
|
adamc@1209
|
1114 | _ => raise Fail "Iflow.string: Unmatched backslash escape"
|
adamc@1209
|
1115 else
|
adamc@1209
|
1116 loop (cs, c :: acc)
|
adamc@1209
|
1117 in
|
adamc@1209
|
1118 case loop (String.explode (String.extract (s, 1, NONE)), []) of
|
adamc@1209
|
1119 NONE => NONE
|
adamc@1209
|
1120 | SOME (s, []) => SOME (s, chs)
|
adamc@1209
|
1121 | SOME (s, cs) => SOME (s, String (String.implode cs) :: chs)
|
adamc@1209
|
1122 end
|
adamc@1209
|
1123 else
|
adamc@1209
|
1124 NONE
|
adamc@1209
|
1125 | _ => NONE
|
adamc@1206
|
1126
|
adamc@1209
|
1127 val prim =
|
adamc@1209
|
1128 altL [wrap (follow (wrapP (follow (keep Char.isDigit) (follow (const ".") (keep Char.isDigit)))
|
adamc@1209
|
1129 (fn (x, ((), y)) => Option.map Prim.Float (Real64.fromString (x ^ "." ^ y))))
|
adamc@1209
|
1130 (opt (const "::float8"))) #1,
|
adamc@1209
|
1131 wrap (follow (wrapP (keep Char.isDigit)
|
adamc@1209
|
1132 (Option.map Prim.Int o Int64.fromString))
|
adamc@1209
|
1133 (opt (const "::int8"))) #1,
|
adamc@1209
|
1134 wrap (follow (opt (const "E")) (follow string (opt (const "::text"))))
|
adamc@1209
|
1135 (Prim.String o #1 o #2)]
|
adamc@1206
|
1136
|
adamc@1207
|
1137 fun known' chs =
|
adamc@1207
|
1138 case chs of
|
adamc@1207
|
1139 Exp (EFfi ("Basis", "sql_known"), _) :: chs => SOME ((), chs)
|
adamc@1207
|
1140 | _ => NONE
|
adamc@1207
|
1141
|
adamc@1207
|
1142 fun sqlify chs =
|
adamc@1207
|
1143 case chs of
|
adamc@1207
|
1144 Exp (EFfiApp ("Basis", f, [e]), _) :: chs =>
|
adamc@1207
|
1145 if String.isPrefix "sqlify" f then
|
adamc@1207
|
1146 SOME (e, chs)
|
adamc@1207
|
1147 else
|
adamc@1207
|
1148 NONE
|
adamc@1207
|
1149 | _ => NONE
|
adamc@1207
|
1150
|
adamc@1211
|
1151 fun constK s = wrap (const s) (fn () => s)
|
adamc@1211
|
1152
|
adamc@1211
|
1153 val funcName = altL [constK "COUNT",
|
adamc@1211
|
1154 constK "MIN",
|
adamc@1211
|
1155 constK "MAX",
|
adamc@1211
|
1156 constK "SUM",
|
adamc@1211
|
1157 constK "AVG"]
|
adamc@1211
|
1158
|
adamc@1204
|
1159 fun sqexp chs =
|
adamc@1206
|
1160 log "sqexp"
|
adamc@1207
|
1161 (altL [wrap prim SqConst,
|
adamc@1211
|
1162 wrap field Field,
|
adamc@1207
|
1163 wrap known SqKnown,
|
adamc@1211
|
1164 wrap func SqFunc,
|
adamc@1211
|
1165 wrap (const "COUNT(*)") (fn () => Count),
|
adamc@1207
|
1166 wrap sqlify Inj,
|
adamc@1211
|
1167 wrap (follow (const "COALESCE(") (follow sqexp (follow (const ",")
|
adamc@1211
|
1168 (follow (keep (fn ch => ch <> #")")) (const ")")))))
|
adamc@1211
|
1169 (fn ((), (e, _)) => e),
|
adamc@1207
|
1170 wrap (follow (ws (const "("))
|
adamc@1207
|
1171 (follow (wrap
|
adamc@1207
|
1172 (follow sqexp
|
adamc@1207
|
1173 (alt
|
adamc@1207
|
1174 (wrap
|
adamc@1207
|
1175 (follow (ws sqbrel)
|
adamc@1207
|
1176 (ws sqexp))
|
adamc@1207
|
1177 inl)
|
adamc@1207
|
1178 (always (inr ()))))
|
adamc@1207
|
1179 (fn (e1, sm) =>
|
adamc@1207
|
1180 case sm of
|
adamc@1207
|
1181 inl (bo, e2) => Binop (bo, e1, e2)
|
adamc@1207
|
1182 | inr () => e1))
|
adamc@1207
|
1183 (const ")")))
|
adamc@1207
|
1184 (fn ((), (e, ())) => e)])
|
adamc@1207
|
1185 chs
|
adamc@1206
|
1186
|
adamc@1207
|
1187 and known chs = wrap (follow known' (follow (const "(") (follow sqexp (const ")"))))
|
adamc@1211
|
1188 (fn ((), ((), (e, ()))) => e) chs
|
adamc@1211
|
1189
|
adamc@1211
|
1190 and func chs = wrap (follow funcName (follow (const "(") (follow sqexp (const ")"))))
|
adamc@1211
|
1191 (fn (f, ((), (e, ()))) => (f, e)) chs
|
adamc@1211
|
1192
|
adamc@1211
|
1193 datatype sitem =
|
adamc@1211
|
1194 SqField of string * string
|
adamc@1211
|
1195 | SqExp of sqexp * string
|
adamc@1211
|
1196
|
adamc@1211
|
1197 val sitem = alt (wrap field SqField)
|
adamc@1211
|
1198 (wrap (follow sqexp (follow (const " AS ") uw_ident))
|
adamc@1211
|
1199 (fn (e, ((), s)) => SqExp (e, s)))
|
adamc@1207
|
1200
|
adamc@1207
|
1201 val select = log "select"
|
adamc@1207
|
1202 (wrap (follow (const "SELECT ") (list sitem))
|
adamc@1207
|
1203 (fn ((), ls) => ls))
|
adamc@1201
|
1204
|
adamc@1201
|
1205 val fitem = wrap (follow uw_ident
|
adamc@1201
|
1206 (follow (const " AS ")
|
adamc@1201
|
1207 t_ident))
|
adamc@1201
|
1208 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
1209
|
adamc@1207
|
1210 val from = log "from"
|
adamc@1207
|
1211 (wrap (follow (const "FROM ") (list fitem))
|
adamc@1207
|
1212 (fn ((), ls) => ls))
|
adamc@1201
|
1213
|
adamc@1204
|
1214 val wher = wrap (follow (ws (const "WHERE ")) sqexp)
|
adamc@1204
|
1215 (fn ((), ls) => ls)
|
adamc@1204
|
1216
|
adamc@1207
|
1217 val query = log "query"
|
adamc@1207
|
1218 (wrap (follow (follow select from) (opt wher))
|
adamc@1207
|
1219 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
|
adamc@1201
|
1220
|
adamc@1215
|
1221 fun removeDups (ls : (string * string) list) =
|
adamc@1211
|
1222 case ls of
|
adamc@1211
|
1223 [] => []
|
adamc@1211
|
1224 | x :: ls =>
|
adamc@1211
|
1225 let
|
adamc@1211
|
1226 val ls = removeDups ls
|
adamc@1211
|
1227 in
|
adamc@1211
|
1228 if List.exists (fn x' => x' = x) ls then
|
adamc@1211
|
1229 ls
|
adamc@1211
|
1230 else
|
adamc@1211
|
1231 x :: ls
|
adamc@1211
|
1232 end
|
adamc@1211
|
1233
|
adamc@1212
|
1234 datatype queryMode =
|
adamc@1218
|
1235 SomeCol
|
adamc@1212
|
1236 | AllCols of exp
|
adamc@1212
|
1237
|
adamc@1216
|
1238 exception Default
|
adamc@1216
|
1239
|
adamc@1214
|
1240 fun queryProp env rvN rv oe e =
|
adamc@1216
|
1241 let
|
adamc@1216
|
1242 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1216
|
1243 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1218
|
1244 (rvN, Unknown, Unknown, [], []))
|
adamc@1216
|
1245 in
|
adamc@1216
|
1246 case parse query e of
|
adamc@1216
|
1247 NONE => default ()
|
adamc@1216
|
1248 | SOME r =>
|
adamc@1216
|
1249 let
|
adamc@1216
|
1250 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1216
|
1251 let
|
adamc@1216
|
1252 val (rvN, e) = rv rvN
|
adamc@1216
|
1253 in
|
adamc@1216
|
1254 ((v, e), rvN)
|
adamc@1216
|
1255 end) rvN (#From r)
|
adamc@1214
|
1256
|
adamc@1216
|
1257 fun rvOf v =
|
adamc@1216
|
1258 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1216
|
1259 NONE => raise Fail "Iflow.queryProp: Bad table variable"
|
adamc@1216
|
1260 | SOME (_, e) => e
|
adamc@1214
|
1261
|
adamc@1216
|
1262 fun usedFields e =
|
adamc@1216
|
1263 case e of
|
adamc@1216
|
1264 SqConst _ => []
|
adamc@1216
|
1265 | Field (v, f) => [(v, f)]
|
adamc@1216
|
1266 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
|
adamc@1216
|
1267 | SqKnown _ => []
|
adamc@1216
|
1268 | Inj _ => []
|
adamc@1216
|
1269 | SqFunc (_, e) => usedFields e
|
adamc@1216
|
1270 | Count => []
|
adamc@1214
|
1271
|
adamc@1216
|
1272 val p =
|
adamc@1216
|
1273 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
|
adamc@1211
|
1274
|
adamc@1216
|
1275 fun expIn e =
|
adamc@1216
|
1276 case e of
|
adamc@1216
|
1277 SqConst p => inl (Const p)
|
adamc@1216
|
1278 | Field (v, f) => inl (Proj (rvOf v, f))
|
adamc@1216
|
1279 | Binop (bo, e1, e2) =>
|
adamc@1216
|
1280 inr (case (bo, expIn e1, expIn e2) of
|
adamc@1216
|
1281 (Exps f, inl e1, inl e2) => f (e1, e2)
|
adamc@1216
|
1282 | (Props f, inr p1, inr p2) => f (p1, p2)
|
adamc@1216
|
1283 | _ => Unknown)
|
adamc@1216
|
1284 | SqKnown e =>
|
adamc@1216
|
1285 inr (case expIn e of
|
adamc@1216
|
1286 inl e => Reln (Known, [e])
|
adamc@1216
|
1287 | _ => Unknown)
|
adamc@1216
|
1288 | Inj e =>
|
adamc@1216
|
1289 let
|
adamc@1216
|
1290 fun deinj (e, _) =
|
adamc@1216
|
1291 case e of
|
adamc@1216
|
1292 ERel n => List.nth (env, n)
|
adamc@1216
|
1293 | EField (e, f) => Proj (deinj e, f)
|
adamc@1216
|
1294 | _ => raise Fail "Iflow: non-variable injected into query"
|
adamc@1216
|
1295 in
|
adamc@1216
|
1296 inl (deinj e)
|
adamc@1216
|
1297 end
|
adamc@1216
|
1298 | SqFunc (f, e) =>
|
adamc@1216
|
1299 inl (case expIn e of
|
adamc@1216
|
1300 inl e => Func (Other f, [e])
|
adamc@1216
|
1301 | _ => raise Fail ("Iflow: non-expresion passed to function " ^ f))
|
adamc@1216
|
1302 | Count => raise Default
|
adamc@1205
|
1303
|
adamc@1216
|
1304 val p = case #Where r of
|
adamc@1216
|
1305 NONE => p
|
adamc@1216
|
1306 | SOME e =>
|
adamc@1216
|
1307 case expIn e of
|
adamc@1216
|
1308 inr p' => And (p, p')
|
adamc@1216
|
1309 | _ => p
|
adamc@1210
|
1310
|
adamc@1216
|
1311 fun normal () =
|
adamc@1218
|
1312 case oe of
|
adamc@1218
|
1313 SomeCol =>
|
adamc@1218
|
1314 (rvN, p, True,
|
adamc@1218
|
1315 List.mapPartial (fn si =>
|
adamc@1218
|
1316 case si of
|
adamc@1218
|
1317 SqField (v, f) => SOME (Proj (rvOf v, f))
|
adamc@1218
|
1318 | SqExp (e, f) =>
|
adamc@1218
|
1319 case expIn e of
|
adamc@1218
|
1320 inr _ => NONE
|
adamc@1218
|
1321 | inl e => SOME e) (#Select r))
|
adamc@1218
|
1322 | AllCols oe =>
|
adamc@1218
|
1323 (rvN, And (p, foldl (fn (si, p) =>
|
adamc@1218
|
1324 let
|
adamc@1218
|
1325 val p' = case si of
|
adamc@1218
|
1326 SqField (v, f) => Reln (Eq, [Proj (Proj (oe, v), f),
|
adamc@1218
|
1327 Proj (rvOf v, f)])
|
adamc@1218
|
1328 | SqExp (e, f) =>
|
adamc@1218
|
1329 case expIn e of
|
adamc@1218
|
1330 inr p => Cond (Proj (oe, f), p)
|
adamc@1218
|
1331 | inl e => Reln (Eq, [Proj (oe, f), e])
|
adamc@1218
|
1332 in
|
adamc@1216
|
1333 And (p, p')
|
adamc@1218
|
1334 end)
|
adamc@1218
|
1335 True (#Select r)),
|
adamc@1218
|
1336 True, [])
|
adamc@1217
|
1337
|
adamc@1218
|
1338 val (rvN, p, wp, outs) =
|
adamc@1217
|
1339 case #Select r of
|
adamc@1217
|
1340 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
|
adamc@1217
|
1341 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
|
adamc@1217
|
1342 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
|
adamc@1218
|
1343 (case oe of
|
adamc@1218
|
1344 SomeCol =>
|
adamc@1218
|
1345 let
|
adamc@1218
|
1346 val (rvN, oe) = rv rvN
|
adamc@1218
|
1347 in
|
adamc@1218
|
1348 (rvN,
|
adamc@1218
|
1349 Or (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.False", [])]),
|
adamc@1218
|
1350 And (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1351 p)),
|
adamc@1218
|
1352 Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1353 [oe])
|
adamc@1218
|
1354 end
|
adamc@1218
|
1355 | AllCols oe =>
|
adamc@1218
|
1356 let
|
adamc@1218
|
1357 val oe = Proj (oe, f)
|
adamc@1218
|
1358 in
|
adamc@1218
|
1359 (rvN,
|
adamc@1218
|
1360 Or (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.False", [])]),
|
adamc@1218
|
1361 And (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1362 p)),
|
adamc@1218
|
1363 Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1364 [])
|
adamc@1218
|
1365 end)
|
adamc@1217
|
1366 | _ => normal ())
|
adamc@1217
|
1367 | _ => normal ()
|
adamc@1216
|
1368 in
|
adamc@1217
|
1369 (rvN, p, wp, case #Where r of
|
adamc@1217
|
1370 NONE => []
|
adamc@1218
|
1371 | SOME e => map (fn (v, f) => Proj (rvOf v, f)) (usedFields e), outs)
|
adamc@1216
|
1372 end
|
adamc@1216
|
1373 handle Default => default ()
|
adamc@1216
|
1374 end
|
adamc@1200
|
1375
|
adamc@1211
|
1376 fun evalPat env e (pt, _) =
|
adamc@1211
|
1377 case pt of
|
adamc@1211
|
1378 PWild => (env, True)
|
adamc@1211
|
1379 | PVar _ => (e :: env, True)
|
adamc@1211
|
1380 | PPrim _ => (env, True)
|
adamc@1215
|
1381 | PCon (_, pc, NONE) => (env, Reln (PCon0 (patCon pc), [e]))
|
adamc@1211
|
1382 | PCon (_, pc, SOME pt) =>
|
adamc@1211
|
1383 let
|
adamc@1215
|
1384 val (env, p) = evalPat env (Func (UnCon (patCon pc), [e])) pt
|
adamc@1211
|
1385 in
|
adamc@1215
|
1386 (env, And (p, Reln (PCon1 (patCon pc), [e])))
|
adamc@1211
|
1387 end
|
adamc@1211
|
1388 | PRecord xpts =>
|
adamc@1211
|
1389 foldl (fn ((x, pt, _), (env, p)) =>
|
adamc@1211
|
1390 let
|
adamc@1211
|
1391 val (env, p') = evalPat env (Proj (e, x)) pt
|
adamc@1211
|
1392 in
|
adamc@1211
|
1393 (env, And (p', p))
|
adamc@1211
|
1394 end) (env, True) xpts
|
adamc@1215
|
1395 | PNone _ => (env, Reln (PCon0 "None", [e]))
|
adamc@1211
|
1396 | PSome (_, pt) =>
|
adamc@1211
|
1397 let
|
adamc@1215
|
1398 val (env, p) = evalPat env (Func (UnCon "Some", [e])) pt
|
adamc@1211
|
1399 in
|
adamc@1215
|
1400 (env, And (p, Reln (PCon1 "Some", [e])))
|
adamc@1211
|
1401 end
|
adamc@1211
|
1402
|
adamc@1211
|
1403 fun peq (p1, p2) =
|
adamc@1211
|
1404 case (p1, p2) of
|
adamc@1211
|
1405 (True, True) => true
|
adamc@1211
|
1406 | (False, False) => true
|
adamc@1211
|
1407 | (Unknown, Unknown) => true
|
adamc@1211
|
1408 | (And (x1, y1), And (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
|
adamc@1211
|
1409 | (Or (x1, y1), Or (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
|
adamc@1211
|
1410 | (Reln (r1, es1), Reln (r2, es2)) => r1 = r2 andalso ListPair.allEq eeq (es1, es2)
|
adamc@1212
|
1411 | (Cond (e1, p1), Cond (e2, p2)) => eeq (e1, e2) andalso peq (p1, p2)
|
adamc@1211
|
1412 | _ => false
|
adamc@1211
|
1413
|
adamc@1211
|
1414 fun removeRedundant p1 =
|
adamc@1211
|
1415 let
|
adamc@1211
|
1416 fun rr p2 =
|
adamc@1211
|
1417 if peq (p1, p2) then
|
adamc@1211
|
1418 True
|
adamc@1211
|
1419 else
|
adamc@1211
|
1420 case p2 of
|
adamc@1211
|
1421 And (x, y) => And (rr x, rr y)
|
adamc@1211
|
1422 | Or (x, y) => Or (rr x, rr y)
|
adamc@1211
|
1423 | _ => p2
|
adamc@1211
|
1424 in
|
adamc@1211
|
1425 rr
|
adamc@1211
|
1426 end
|
adamc@1211
|
1427
|
adamc@1218
|
1428 datatype cflow = Case | Where
|
adamc@1218
|
1429 datatype flow = Data | Control of cflow
|
adamc@1218
|
1430 type check = ErrorMsg.span * exp * prop
|
adamc@1218
|
1431
|
adamc@1217
|
1432 structure St :> sig
|
adamc@1217
|
1433 type t
|
adamc@1217
|
1434 val create : {Var : int,
|
adamc@1217
|
1435 Ambient : prop} -> t
|
adamc@1217
|
1436
|
adamc@1217
|
1437 val curVar : t -> int
|
adamc@1217
|
1438 val nextVar : t -> t * int
|
adamc@1217
|
1439
|
adamc@1217
|
1440 val ambient : t -> prop
|
adamc@1217
|
1441 val setAmbient : t * prop -> t
|
adamc@1217
|
1442
|
adamc@1218
|
1443 val paths : t -> (check * cflow) list
|
adamc@1218
|
1444 val addPath : t * (check * cflow) -> t
|
adamc@1218
|
1445 val addPaths : t * (check * cflow) list -> t
|
adamc@1218
|
1446 val clearPaths : t -> t
|
adamc@1218
|
1447 val setPaths : t * (check * cflow) list -> t
|
adamc@1217
|
1448
|
adamc@1218
|
1449 val sent : t -> (check * flow) list
|
adamc@1218
|
1450 val addSent : t * (check * flow) -> t
|
adamc@1218
|
1451 val setSent : t * (check * flow) list -> t
|
adamc@1217
|
1452 end = struct
|
adamc@1217
|
1453
|
adamc@1217
|
1454 type t = {Var : int,
|
adamc@1217
|
1455 Ambient : prop,
|
adamc@1218
|
1456 Path : (check * cflow) list,
|
adamc@1218
|
1457 Sent : (check * flow) list}
|
adamc@1217
|
1458
|
adamc@1217
|
1459 fun create {Var = v, Ambient = p} = {Var = v,
|
adamc@1217
|
1460 Ambient = p,
|
adamc@1217
|
1461 Path = [],
|
adamc@1217
|
1462 Sent = []}
|
adamc@1217
|
1463
|
adamc@1217
|
1464 fun curVar (t : t) = #Var t
|
adamc@1217
|
1465 fun nextVar (t : t) = ({Var = #Var t + 1,
|
adamc@1217
|
1466 Ambient = #Ambient t,
|
adamc@1217
|
1467 Path = #Path t,
|
adamc@1217
|
1468 Sent = #Sent t}, #Var t)
|
adamc@1217
|
1469
|
adamc@1217
|
1470 fun ambient (t : t) = #Ambient t
|
adamc@1217
|
1471 fun setAmbient (t : t, p) = {Var = #Var t,
|
adamc@1217
|
1472 Ambient = p,
|
adamc@1217
|
1473 Path = #Path t,
|
adamc@1217
|
1474 Sent = #Sent t}
|
adamc@1217
|
1475
|
adamc@1218
|
1476 fun paths (t : t) = #Path t
|
adamc@1217
|
1477 fun addPath (t : t, c) = {Var = #Var t,
|
adamc@1217
|
1478 Ambient = #Ambient t,
|
adamc@1217
|
1479 Path = c :: #Path t,
|
adamc@1217
|
1480 Sent = #Sent t}
|
adamc@1218
|
1481 fun addPaths (t : t, cs) = {Var = #Var t,
|
adamc@1218
|
1482 Ambient = #Ambient t,
|
adamc@1218
|
1483 Path = cs @ #Path t,
|
adamc@1218
|
1484 Sent = #Sent t}
|
adamc@1218
|
1485 fun clearPaths (t : t) = {Var = #Var t,
|
adamc@1218
|
1486 Ambient = #Ambient t,
|
adamc@1218
|
1487 Path = [],
|
adamc@1218
|
1488 Sent = #Sent t}
|
adamc@1218
|
1489 fun setPaths (t : t, cs) = {Var = #Var t,
|
adamc@1218
|
1490 Ambient = #Ambient t,
|
adamc@1218
|
1491 Path = cs,
|
adamc@1218
|
1492 Sent = #Sent t}
|
adamc@1217
|
1493
|
adamc@1217
|
1494 fun sent (t : t) = #Sent t
|
adamc@1217
|
1495 fun addSent (t : t, c) = {Var = #Var t,
|
adamc@1217
|
1496 Ambient = #Ambient t,
|
adamc@1217
|
1497 Path = #Path t,
|
adamc@1217
|
1498 Sent = c :: #Sent t}
|
adamc@1217
|
1499 fun setSent (t : t, cs) = {Var = #Var t,
|
adamc@1217
|
1500 Ambient = #Ambient t,
|
adamc@1217
|
1501 Path = #Path t,
|
adamc@1217
|
1502 Sent = cs}
|
adamc@1217
|
1503
|
adamc@1217
|
1504 end
|
adamc@1217
|
1505
|
adamc@1217
|
1506 fun evalExp env (e as (_, loc), st) =
|
adamc@1200
|
1507 let
|
adamc@1200
|
1508 fun default () =
|
adamc@1217
|
1509 let
|
adamc@1217
|
1510 val (st, nv) = St.nextVar st
|
adamc@1217
|
1511 in
|
adamc@1217
|
1512 (Var nv, st)
|
adamc@1217
|
1513 end
|
adamc@1200
|
1514
|
adamc@1217
|
1515 fun addSent (p, e, st) =
|
adamc@1218
|
1516 let
|
adamc@1218
|
1517 val st = if isKnown e then
|
adamc@1218
|
1518 st
|
adamc@1218
|
1519 else
|
adamc@1218
|
1520 St.addSent (st, ((loc, e, p), Data))
|
adamc@1218
|
1521
|
adamc@1218
|
1522 val st = foldl (fn ((c, fl), st) => St.addSent (st, (c, Control fl))) st (St.paths st)
|
adamc@1218
|
1523 in
|
adamc@1218
|
1524 St.clearPaths st
|
adamc@1218
|
1525 end
|
adamc@1200
|
1526 in
|
adamc@1200
|
1527 case #1 e of
|
adamc@1200
|
1528 EPrim p => (Const p, st)
|
adamc@1200
|
1529 | ERel n => (List.nth (env, n), st)
|
adamc@1200
|
1530 | ENamed _ => default ()
|
adamc@1215
|
1531 | ECon (_, pc, NONE) => (Func (DtCon0 (patCon pc), []), st)
|
adamc@1200
|
1532 | ECon (_, pc, SOME e) =>
|
adamc@1200
|
1533 let
|
adamc@1200
|
1534 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1535 in
|
adamc@1215
|
1536 (Func (DtCon1 (patCon pc), [e]), st)
|
adamc@1200
|
1537 end
|
adamc@1215
|
1538 | ENone _ => (Func (DtCon0 "None", []), st)
|
adamc@1200
|
1539 | ESome (_, e) =>
|
adamc@1200
|
1540 let
|
adamc@1200
|
1541 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1542 in
|
adamc@1215
|
1543 (Func (DtCon1 "Some", [e]), st)
|
adamc@1200
|
1544 end
|
adamc@1200
|
1545 | EFfi _ => default ()
|
adamc@1213
|
1546
|
adamc@1200
|
1547 | EFfiApp (m, s, es) =>
|
adamc@1200
|
1548 if m = "Basis" andalso SS.member (writers, s) then
|
adamc@1200
|
1549 let
|
adamc@1200
|
1550 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1551 in
|
adamc@1217
|
1552 (Recd [], foldl (fn (e, st) => addSent (St.ambient st, e, st)) st es)
|
adamc@1200
|
1553 end
|
adamc@1200
|
1554 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
|
adamc@1200
|
1555 default ()
|
adamc@1200
|
1556 else
|
adamc@1200
|
1557 let
|
adamc@1200
|
1558 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1559 in
|
adamc@1215
|
1560 (Func (Other (m ^ "." ^ s), es), st)
|
adamc@1200
|
1561 end
|
adamc@1213
|
1562
|
adamc@1213
|
1563 | EApp (e1, e2) =>
|
adamc@1213
|
1564 let
|
adamc@1213
|
1565 val (e1, st) = evalExp env (e1, st)
|
adamc@1213
|
1566 in
|
adamc@1213
|
1567 case e1 of
|
adamc@1213
|
1568 Finish => (Finish, st)
|
adamc@1213
|
1569 | _ => default ()
|
adamc@1213
|
1570 end
|
adamc@1213
|
1571
|
adamc@1200
|
1572 | EAbs _ => default ()
|
adamc@1200
|
1573 | EUnop (s, e1) =>
|
adamc@1200
|
1574 let
|
adamc@1200
|
1575 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1576 in
|
adamc@1215
|
1577 (Func (Other s, [e1]), st)
|
adamc@1200
|
1578 end
|
adamc@1200
|
1579 | EBinop (s, e1, e2) =>
|
adamc@1200
|
1580 let
|
adamc@1200
|
1581 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1582 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
1583 in
|
adamc@1215
|
1584 (Func (Other s, [e1, e2]), st)
|
adamc@1200
|
1585 end
|
adamc@1200
|
1586 | ERecord xets =>
|
adamc@1200
|
1587 let
|
adamc@1200
|
1588 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
|
adamc@1200
|
1589 let
|
adamc@1200
|
1590 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1591 in
|
adamc@1200
|
1592 ((x, e), st)
|
adamc@1200
|
1593 end) st xets
|
adamc@1200
|
1594 in
|
adamc@1200
|
1595 (Recd xes, st)
|
adamc@1200
|
1596 end
|
adamc@1200
|
1597 | EField (e, s) =>
|
adamc@1200
|
1598 let
|
adamc@1200
|
1599 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1600 in
|
adamc@1200
|
1601 (Proj (e, s), st)
|
adamc@1200
|
1602 end
|
adamc@1218
|
1603 | ECase (e, pes, {result = res, ...}) =>
|
adamc@1211
|
1604 let
|
adamc@1211
|
1605 val (e, st) = evalExp env (e, st)
|
adamc@1217
|
1606 val (st, r) = St.nextVar st
|
adamc@1217
|
1607 val orig = St.ambient st
|
adamc@1218
|
1608 val origPaths = St.paths st
|
adamc@1211
|
1609
|
adamc@1218
|
1610 val st = St.addPath (st, ((loc, e, orig), Case))
|
adamc@1218
|
1611
|
adamc@1218
|
1612 val (st, paths) =
|
adamc@1218
|
1613 foldl (fn ((pt, pe), (st, paths)) =>
|
adamc@1218
|
1614 let
|
adamc@1218
|
1615 val (env, pp) = evalPat env e pt
|
adamc@1218
|
1616 val (pe, st') = evalExp env (pe, St.setAmbient (st, And (orig, pp)))
|
adamc@1218
|
1617
|
adamc@1218
|
1618 val this = And (removeRedundant orig (St.ambient st'),
|
adamc@1218
|
1619 Reln (Eq, [Var r, pe]))
|
adamc@1218
|
1620 in
|
adamc@1218
|
1621 (St.setPaths (St.setAmbient (st', Or (St.ambient st, this)), origPaths),
|
adamc@1218
|
1622 St.paths st' @ paths)
|
adamc@1218
|
1623 end) (St.setAmbient (st, False), []) pes
|
adamc@1218
|
1624
|
adamc@1218
|
1625 val st = case #1 res of
|
adamc@1218
|
1626 TRecord [] => St.setPaths (st, origPaths)
|
adamc@1218
|
1627 | _ => St.setPaths (st, paths)
|
adamc@1211
|
1628 in
|
adamc@1217
|
1629 (Var r, St.setAmbient (st, And (orig, St.ambient st)))
|
adamc@1211
|
1630 end
|
adamc@1200
|
1631 | EStrcat (e1, e2) =>
|
adamc@1200
|
1632 let
|
adamc@1200
|
1633 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1634 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
1635 in
|
adamc@1215
|
1636 (Func (Other "cat", [e1, e2]), st)
|
adamc@1200
|
1637 end
|
adamc@1200
|
1638 | EError _ => (Finish, st)
|
adamc@1200
|
1639 | EReturnBlob {blob = b, mimeType = m, ...} =>
|
adamc@1200
|
1640 let
|
adamc@1200
|
1641 val (b, st) = evalExp env (b, st)
|
adamc@1200
|
1642 val (m, st) = evalExp env (m, st)
|
adamc@1200
|
1643 in
|
adamc@1217
|
1644 (Finish, addSent (St.ambient st, b, addSent (St.ambient st, m, st)))
|
adamc@1200
|
1645 end
|
adamc@1200
|
1646 | ERedirect (e, _) =>
|
adamc@1200
|
1647 let
|
adamc@1200
|
1648 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1649 in
|
adamc@1217
|
1650 (Finish, addSent (St.ambient st, e, st))
|
adamc@1200
|
1651 end
|
adamc@1200
|
1652 | EWrite e =>
|
adamc@1200
|
1653 let
|
adamc@1200
|
1654 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1655 in
|
adamc@1217
|
1656 (Recd [], addSent (St.ambient st, e, st))
|
adamc@1200
|
1657 end
|
adamc@1200
|
1658 | ESeq (e1, e2) =>
|
adamc@1200
|
1659 let
|
adamc@1200
|
1660 val (_, st) = evalExp env (e1, st)
|
adamc@1200
|
1661 in
|
adamc@1200
|
1662 evalExp env (e2, st)
|
adamc@1200
|
1663 end
|
adamc@1200
|
1664 | ELet (_, _, e1, e2) =>
|
adamc@1200
|
1665 let
|
adamc@1200
|
1666 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1667 in
|
adamc@1200
|
1668 evalExp (e1 :: env) (e2, st)
|
adamc@1200
|
1669 end
|
adamc@1200
|
1670 | EClosure (n, es) =>
|
adamc@1200
|
1671 let
|
adamc@1200
|
1672 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1673 in
|
adamc@1215
|
1674 (Func (Other ("Cl" ^ Int.toString n), es), st)
|
adamc@1200
|
1675 end
|
adamc@1200
|
1676
|
adamc@1200
|
1677 | EQuery {query = q, body = b, initial = i, ...} =>
|
adamc@1200
|
1678 let
|
adamc@1200
|
1679 val (_, st) = evalExp env (q, st)
|
adamc@1200
|
1680 val (i, st) = evalExp env (i, st)
|
adamc@1200
|
1681
|
adamc@1217
|
1682 val (st', r) = St.nextVar st
|
adamc@1217
|
1683 val (st', acc) = St.nextVar st'
|
adamc@1200
|
1684
|
adamc@1200
|
1685 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
|
adamc@1200
|
1686
|
adamc@1218
|
1687 val (st', qp, qwp, used, _) =
|
adamc@1214
|
1688 queryProp env
|
adamc@1217
|
1689 st' (fn st' =>
|
adamc@1217
|
1690 let
|
adamc@1217
|
1691 val (st', rv) = St.nextVar st'
|
adamc@1217
|
1692 in
|
adamc@1217
|
1693 (st', Var rv)
|
adamc@1217
|
1694 end)
|
adamc@1214
|
1695 (AllCols (Var r)) q
|
adamc@1200
|
1696
|
adamc@1217
|
1697 val p' = And (qp, St.ambient st')
|
adamc@1200
|
1698
|
adamc@1217
|
1699 val (st, res) = if varInP acc (St.ambient st') then
|
adamc@1217
|
1700 let
|
adamc@1217
|
1701 val (st, r) = St.nextVar st
|
adamc@1217
|
1702 in
|
adamc@1217
|
1703 (st, Var r)
|
adamc@1217
|
1704 end
|
adamc@1217
|
1705 else
|
adamc@1217
|
1706 let
|
adamc@1217
|
1707 val (st, out) = St.nextVar st'
|
adamc@1217
|
1708
|
adamc@1217
|
1709 val p = Or (Reln (Eq, [Var out, i]),
|
adamc@1217
|
1710 And (Reln (Eq, [Var out, b]),
|
adamc@1217
|
1711 p'))
|
adamc@1217
|
1712 in
|
adamc@1217
|
1713 (St.setAmbient (st, p), Var out)
|
adamc@1217
|
1714 end
|
adamc@1210
|
1715
|
adamc@1218
|
1716 val sent = map (fn ((loc, e, p), fl) => ((loc, e, And (qp, p)), fl)) (St.sent st')
|
adamc@1212
|
1717
|
adamc@1217
|
1718 val p' = And (p', qwp)
|
adamc@1218
|
1719 val paths = map (fn e => ((loc, e, p'), Where)) used
|
adamc@1200
|
1720 in
|
adamc@1218
|
1721 (res, St.addPaths (St.setSent (st, sent), paths))
|
adamc@1200
|
1722 end
|
adamc@1200
|
1723 | EDml _ => default ()
|
adamc@1200
|
1724 | ENextval _ => default ()
|
adamc@1200
|
1725 | ESetval _ => default ()
|
adamc@1200
|
1726
|
adamc@1213
|
1727 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
|
adamc@1217
|
1728 let
|
adamc@1217
|
1729 val (st, nv) = St.nextVar st
|
adamc@1217
|
1730 in
|
adamc@1217
|
1731 (Var nv, St.setAmbient (st, And (St.ambient st, Reln (Known, [Var nv]))))
|
adamc@1217
|
1732 end
|
adamc@1213
|
1733
|
adamc@1200
|
1734 | EUnurlify _ => default ()
|
adamc@1200
|
1735 | EJavaScript _ => default ()
|
adamc@1200
|
1736 | ESignalReturn _ => default ()
|
adamc@1200
|
1737 | ESignalBind _ => default ()
|
adamc@1200
|
1738 | ESignalSource _ => default ()
|
adamc@1200
|
1739 | EServerCall _ => default ()
|
adamc@1200
|
1740 | ERecv _ => default ()
|
adamc@1200
|
1741 | ESleep _ => default ()
|
adamc@1200
|
1742 | ESpawn _ => default ()
|
adamc@1200
|
1743 end
|
adamc@1200
|
1744
|
adamc@1200
|
1745 fun check file =
|
adamc@1200
|
1746 let
|
adamc@1213
|
1747 val file = MonoReduce.reduce file
|
adamc@1213
|
1748 val file = MonoOpt.optimize file
|
adamc@1213
|
1749 val file = Fuse.fuse file
|
adamc@1213
|
1750 val file = MonoOpt.optimize file
|
adamc@1216
|
1751 val file = MonoShake.shake file
|
adamc@1213
|
1752 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
|
adamc@1213
|
1753
|
adamc@1207
|
1754 val exptd = foldl (fn ((d, _), exptd) =>
|
adamc@1207
|
1755 case d of
|
adamc@1207
|
1756 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
|
adamc@1207
|
1757 | _ => exptd) IS.empty file
|
adamc@1207
|
1758
|
adamc@1202
|
1759 fun decl ((d, _), (vals, pols)) =
|
adamc@1200
|
1760 case d of
|
adamc@1207
|
1761 DVal (_, n, _, e, _) =>
|
adamc@1200
|
1762 let
|
adamc@1207
|
1763 val isExptd = IS.member (exptd, n)
|
adamc@1207
|
1764
|
adamc@1207
|
1765 fun deAbs (e, env, nv, p) =
|
adamc@1200
|
1766 case #1 e of
|
adamc@1207
|
1767 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1,
|
adamc@1207
|
1768 if isExptd then
|
adamc@1207
|
1769 And (p, Reln (Known, [Var nv]))
|
adamc@1207
|
1770 else
|
adamc@1207
|
1771 p)
|
adamc@1207
|
1772 | _ => (e, env, nv, p)
|
adamc@1200
|
1773
|
adamc@1207
|
1774 val (e, env, nv, p) = deAbs (e, [], 1, True)
|
adamc@1200
|
1775
|
adamc@1217
|
1776 val (_, st) = evalExp env (e, St.create {Var = nv,
|
adamc@1217
|
1777 Ambient = p})
|
adamc@1200
|
1778 in
|
adamc@1217
|
1779 (St.sent st @ vals, pols)
|
adamc@1200
|
1780 end
|
adamc@1202
|
1781
|
adamc@1218
|
1782 | DPolicy (PolClient e) =>
|
adamc@1218
|
1783 let
|
adamc@1218
|
1784 val (_, p, _, _, outs) = queryProp [] 0 (fn rvN => (rvN + 1, Lvar rvN)) SomeCol e
|
adamc@1218
|
1785 in
|
adamc@1218
|
1786 (vals, (p, outs) :: pols)
|
adamc@1218
|
1787 end
|
adamc@1214
|
1788
|
adamc@1202
|
1789 | _ => (vals, pols)
|
adamc@1202
|
1790
|
adamc@1203
|
1791 val () = reset ()
|
adamc@1202
|
1792
|
adamc@1202
|
1793 val (vals, pols) = foldl decl ([], []) file
|
adamc@1200
|
1794 in
|
adamc@1218
|
1795 app (fn ((loc, e, p), fl) =>
|
adamc@1207
|
1796 let
|
adamc@1213
|
1797 fun doOne e =
|
adamc@1213
|
1798 let
|
adamc@1213
|
1799 val p = And (p, Reln (Eq, [Var 0, e]))
|
adamc@1213
|
1800 in
|
adamc@1218
|
1801 if decomp true (fn (e1, e2) => e1 andalso e2 ()) p
|
adamc@1218
|
1802 (fn hyps =>
|
adamc@1218
|
1803 (fl <> Control Where
|
adamc@1218
|
1804 andalso imply (hyps, [AReln (Known, [Var 0])], [Var 0]))
|
adamc@1218
|
1805 orelse List.exists (fn (p', outs) =>
|
adamc@1218
|
1806 decomp false (fn (e1, e2) => e1 orelse e2 ()) p'
|
adamc@1218
|
1807 (fn goals => imply (hyps, goals, outs)))
|
adamc@1218
|
1808 pols) then
|
adamc@1213
|
1809 ()
|
adamc@1213
|
1810 else
|
adamc@1213
|
1811 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
|
adamc@1216
|
1812 Print.preface ("The state satisifies this predicate:", p_prop p))
|
adamc@1213
|
1813 end
|
adamc@1213
|
1814
|
adamc@1213
|
1815 fun doAll e =
|
adamc@1213
|
1816 case e of
|
adamc@1213
|
1817 Const _ => ()
|
adamc@1213
|
1818 | Var _ => doOne e
|
adamc@1213
|
1819 | Lvar _ => raise Fail "Iflow.doAll: Lvar"
|
adamc@1216
|
1820 | Func (UnCon _, [_]) => doOne e
|
adamc@1215
|
1821 | Func (_, es) => app doAll es
|
adamc@1213
|
1822 | Recd xes => app (doAll o #2) xes
|
adamc@1213
|
1823 | Proj _ => doOne e
|
adamc@1213
|
1824 | Finish => ()
|
adamc@1207
|
1825 in
|
adamc@1213
|
1826 doAll e
|
adamc@1207
|
1827 end) vals
|
adamc@1200
|
1828 end
|
adamc@1200
|
1829
|
adamc@1213
|
1830 val check = fn file =>
|
adamc@1213
|
1831 let
|
adamc@1213
|
1832 val oldInline = Settings.getMonoInline ()
|
adamc@1213
|
1833 in
|
adamc@1213
|
1834 (Settings.setMonoInline (case Int.maxInt of
|
adamc@1213
|
1835 NONE => 1000000
|
adamc@1213
|
1836 | SOME n => n);
|
adamc@1213
|
1837 check file;
|
adamc@1213
|
1838 Settings.setMonoInline oldInline)
|
adamc@1213
|
1839 handle ex => (Settings.setMonoInline oldInline;
|
adamc@1213
|
1840 raise ex)
|
adamc@1213
|
1841 end
|
adamc@1213
|
1842
|
adamc@1200
|
1843 end
|
adamc@1213
|
1844
|