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@1219
|
513 Known = ref true})
|
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@1220
|
887 andalso (case outs of
|
adamc@1220
|
888 NONE => true
|
adamc@1220
|
889 | SOME outs => Cc.builtFrom (cc, {Derived = Var 0,
|
adamc@1220
|
890 Base = outs})))
|
adamc@1218
|
891 handle Cc.Contradiction => false
|
adamc@1218
|
892 end handle Cc.Undetermined => false)
|
adamc@1218
|
893 orelse onFail ()
|
adamc@1218
|
894 | (g as AReln (Sql gf, [ge])) :: goals =>
|
adamc@1218
|
895 let
|
adamc@1218
|
896 fun hps hyps =
|
adamc@1218
|
897 case hyps of
|
adamc@1218
|
898 [] => gls goals onFail (g :: acc)
|
adamc@1218
|
899 | (h as AReln (Sql hf, [he])) :: hyps =>
|
adamc@1218
|
900 if gf = hf then
|
adamc@1218
|
901 let
|
adamc@1218
|
902 val saved = save ()
|
adamc@1218
|
903 in
|
adamc@1218
|
904 if eq (ge, he) then
|
adamc@1218
|
905 let
|
adamc@1218
|
906 val changed = IM.numItems (!unif)
|
adamc@1218
|
907 <> IM.numItems saved
|
adamc@1218
|
908 in
|
adamc@1218
|
909 gls goals (fn () => (restore saved;
|
adamc@1218
|
910 changed
|
adamc@1218
|
911 andalso hps hyps))
|
adamc@1218
|
912 acc
|
adamc@1218
|
913 end
|
adamc@1218
|
914 else
|
adamc@1218
|
915 hps hyps
|
adamc@1218
|
916 end
|
adamc@1218
|
917 else
|
adamc@1218
|
918 hps hyps
|
adamc@1218
|
919 | _ :: hyps => hps hyps
|
adamc@1218
|
920 in
|
adamc@1218
|
921 hps hyps
|
adamc@1218
|
922 end
|
adamc@1218
|
923 | g :: goals => gls goals onFail (g :: acc)
|
adamc@1218
|
924 in
|
adamc@1218
|
925 reset ();
|
adamc@1218
|
926 (*Print.prefaces "Big go" [("hyps", Print.p_list p_atom hyps),
|
adamc@1218
|
927 ("goals", Print.p_list p_atom goals)];*)
|
adamc@1218
|
928 gls goals (fn () => false) []
|
adamc@1218
|
929 end handle Cc.Contradiction => true
|
adamc@1200
|
930
|
adamc@1200
|
931 fun patCon pc =
|
adamc@1200
|
932 case pc of
|
adamc@1200
|
933 PConVar n => "C" ^ Int.toString n
|
adamc@1200
|
934 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
|
adamc@1200
|
935
|
adamc@1200
|
936 datatype chunk =
|
adamc@1200
|
937 String of string
|
adamc@1200
|
938 | Exp of Mono.exp
|
adamc@1200
|
939
|
adamc@1200
|
940 fun chunkify e =
|
adamc@1200
|
941 case #1 e of
|
adamc@1200
|
942 EPrim (Prim.String s) => [String s]
|
adamc@1207
|
943 | EStrcat (e1, e2) =>
|
adamc@1207
|
944 let
|
adamc@1207
|
945 val chs1 = chunkify e1
|
adamc@1207
|
946 val chs2 = chunkify e2
|
adamc@1207
|
947 in
|
adamc@1207
|
948 case chs2 of
|
adamc@1207
|
949 String s2 :: chs2' =>
|
adamc@1207
|
950 (case List.last chs1 of
|
adamc@1207
|
951 String s1 => List.take (chs1, length chs1 - 1) @ String (s1 ^ s2) :: chs2'
|
adamc@1207
|
952 | _ => chs1 @ chs2)
|
adamc@1207
|
953 | _ => chs1 @ chs2
|
adamc@1207
|
954 end
|
adamc@1200
|
955 | _ => [Exp e]
|
adamc@1200
|
956
|
adamc@1201
|
957 type 'a parser = chunk list -> ('a * chunk list) option
|
adamc@1201
|
958
|
adamc@1201
|
959 fun always v chs = SOME (v, chs)
|
adamc@1201
|
960
|
adamc@1202
|
961 fun parse p s =
|
adamc@1202
|
962 case p (chunkify s) of
|
adamc@1201
|
963 SOME (v, []) => SOME v
|
adamc@1201
|
964 | _ => NONE
|
adamc@1201
|
965
|
adamc@1201
|
966 fun const s chs =
|
adamc@1201
|
967 case chs of
|
adamc@1201
|
968 String s' :: chs => if String.isPrefix s s' then
|
adamc@1201
|
969 SOME ((), if size s = size s' then
|
adamc@1201
|
970 chs
|
adamc@1201
|
971 else
|
adamc@1201
|
972 String (String.extract (s', size s, NONE)) :: chs)
|
adamc@1201
|
973 else
|
adamc@1201
|
974 NONE
|
adamc@1201
|
975 | _ => NONE
|
adamc@1201
|
976
|
adamc@1201
|
977 fun follow p1 p2 chs =
|
adamc@1201
|
978 case p1 chs of
|
adamc@1201
|
979 NONE => NONE
|
adamc@1201
|
980 | SOME (v1, chs) =>
|
adamc@1201
|
981 case p2 chs of
|
adamc@1201
|
982 NONE => NONE
|
adamc@1201
|
983 | SOME (v2, chs) => SOME ((v1, v2), chs)
|
adamc@1201
|
984
|
adamc@1201
|
985 fun wrap p f chs =
|
adamc@1201
|
986 case p chs of
|
adamc@1201
|
987 NONE => NONE
|
adamc@1201
|
988 | SOME (v, chs) => SOME (f v, chs)
|
adamc@1201
|
989
|
adamc@1209
|
990 fun wrapP p f chs =
|
adamc@1209
|
991 case p chs of
|
adamc@1209
|
992 NONE => NONE
|
adamc@1209
|
993 | SOME (v, chs) =>
|
adamc@1209
|
994 case f v of
|
adamc@1209
|
995 NONE => NONE
|
adamc@1209
|
996 | SOME r => SOME (r, chs)
|
adamc@1209
|
997
|
adamc@1201
|
998 fun alt p1 p2 chs =
|
adamc@1201
|
999 case p1 chs of
|
adamc@1201
|
1000 NONE => p2 chs
|
adamc@1201
|
1001 | v => v
|
adamc@1201
|
1002
|
adamc@1207
|
1003 fun altL ps =
|
adamc@1207
|
1004 case rev ps of
|
adamc@1207
|
1005 [] => (fn _ => NONE)
|
adamc@1207
|
1006 | p :: ps =>
|
adamc@1207
|
1007 foldl (fn (p1, p2) => alt p1 p2) p ps
|
adamc@1207
|
1008
|
adamc@1204
|
1009 fun opt p chs =
|
adamc@1204
|
1010 case p chs of
|
adamc@1204
|
1011 NONE => SOME (NONE, chs)
|
adamc@1204
|
1012 | SOME (v, chs) => SOME (SOME v, chs)
|
adamc@1204
|
1013
|
adamc@1201
|
1014 fun skip cp chs =
|
adamc@1201
|
1015 case chs of
|
adamc@1201
|
1016 String "" :: chs => skip cp chs
|
adamc@1201
|
1017 | String s :: chs' => if cp (String.sub (s, 0)) then
|
adamc@1201
|
1018 skip cp (String (String.extract (s, 1, NONE)) :: chs')
|
adamc@1201
|
1019 else
|
adamc@1201
|
1020 SOME ((), chs)
|
adamc@1201
|
1021 | _ => SOME ((), chs)
|
adamc@1201
|
1022
|
adamc@1201
|
1023 fun keep cp chs =
|
adamc@1201
|
1024 case chs of
|
adamc@1201
|
1025 String "" :: chs => keep cp chs
|
adamc@1201
|
1026 | String s :: chs' =>
|
adamc@1201
|
1027 let
|
adamc@1201
|
1028 val (befor, after) = Substring.splitl cp (Substring.full s)
|
adamc@1201
|
1029 in
|
adamc@1201
|
1030 if Substring.isEmpty befor then
|
adamc@1201
|
1031 NONE
|
adamc@1201
|
1032 else
|
adamc@1201
|
1033 SOME (Substring.string befor,
|
adamc@1201
|
1034 if Substring.isEmpty after then
|
adamc@1201
|
1035 chs'
|
adamc@1201
|
1036 else
|
adamc@1201
|
1037 String (Substring.string after) :: chs')
|
adamc@1201
|
1038 end
|
adamc@1201
|
1039 | _ => NONE
|
adamc@1201
|
1040
|
adamc@1204
|
1041 fun ws p = wrap (follow (skip (fn ch => ch = #" "))
|
adamc@1204
|
1042 (follow p (skip (fn ch => ch = #" ")))) (#1 o #2)
|
adamc@1204
|
1043
|
adamc@1204
|
1044 fun log name p chs =
|
adamc@1206
|
1045 (if !debug then
|
adamc@1206
|
1046 case chs of
|
adamc@1207
|
1047 String s :: _ => print (name ^ ": " ^ s ^ "\n")
|
adamc@1206
|
1048 | _ => print (name ^ ": blocked!\n")
|
adamc@1206
|
1049 else
|
adamc@1206
|
1050 ();
|
adamc@1204
|
1051 p chs)
|
adamc@1201
|
1052
|
adamc@1201
|
1053 fun list p chs =
|
adamc@1207
|
1054 altL [wrap (follow p (follow (ws (const ",")) (list p)))
|
adamc@1207
|
1055 (fn (v, ((), ls)) => v :: ls),
|
adamc@1207
|
1056 wrap (ws p) (fn v => [v]),
|
adamc@1207
|
1057 always []] chs
|
adamc@1201
|
1058
|
adamc@1201
|
1059 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
|
adamc@1201
|
1060
|
adamc@1211
|
1061 val t_ident = wrapP ident (fn s => if String.isPrefix "T_" s then
|
adamc@1211
|
1062 SOME (String.extract (s, 2, NONE))
|
adamc@1201
|
1063 else
|
adamc@1211
|
1064 NONE)
|
adamc@1211
|
1065 val uw_ident = wrapP ident (fn s => if String.isPrefix "uw_" s andalso size s >= 4 then
|
adamc@1211
|
1066 SOME (str (Char.toUpper (String.sub (s, 3)))
|
adamc@1211
|
1067 ^ String.extract (s, 4, NONE))
|
adamc@1211
|
1068 else
|
adamc@1211
|
1069 NONE)
|
adamc@1201
|
1070
|
adamc@1211
|
1071 val field = wrap (follow t_ident
|
adamc@1201
|
1072 (follow (const ".")
|
adamc@1201
|
1073 uw_ident))
|
adamc@1201
|
1074 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
1075
|
adamc@1206
|
1076 datatype Rel =
|
adamc@1206
|
1077 Exps of exp * exp -> prop
|
adamc@1206
|
1078 | Props of prop * prop -> prop
|
adamc@1206
|
1079
|
adamc@1204
|
1080 datatype sqexp =
|
adamc@1206
|
1081 SqConst of Prim.t
|
adamc@1206
|
1082 | Field of string * string
|
adamc@1206
|
1083 | Binop of Rel * sqexp * sqexp
|
adamc@1207
|
1084 | SqKnown of sqexp
|
adamc@1207
|
1085 | Inj of Mono.exp
|
adamc@1211
|
1086 | SqFunc of string * sqexp
|
adamc@1211
|
1087 | Count
|
adamc@1204
|
1088
|
adamc@1210
|
1089 fun cmp s r = wrap (const s) (fn () => Exps (fn (e1, e2) => Reln (r, [e1, e2])))
|
adamc@1210
|
1090
|
adamc@1210
|
1091 val sqbrel = altL [cmp "=" Eq,
|
adamc@1210
|
1092 cmp "<>" Ne,
|
adamc@1210
|
1093 cmp "<=" Le,
|
adamc@1210
|
1094 cmp "<" Lt,
|
adamc@1210
|
1095 cmp ">=" Ge,
|
adamc@1210
|
1096 cmp ">" Gt,
|
adamc@1207
|
1097 wrap (const "AND") (fn () => Props And),
|
adamc@1207
|
1098 wrap (const "OR") (fn () => Props Or)]
|
adamc@1204
|
1099
|
adamc@1204
|
1100 datatype ('a, 'b) sum = inl of 'a | inr of 'b
|
adamc@1204
|
1101
|
adamc@1209
|
1102 fun string chs =
|
adamc@1206
|
1103 case chs of
|
adamc@1209
|
1104 String s :: chs =>
|
adamc@1209
|
1105 if size s >= 2 andalso String.sub (s, 0) = #"'" then
|
adamc@1209
|
1106 let
|
adamc@1209
|
1107 fun loop (cs, acc) =
|
adamc@1209
|
1108 case cs of
|
adamc@1209
|
1109 [] => NONE
|
adamc@1209
|
1110 | c :: cs =>
|
adamc@1209
|
1111 if c = #"'" then
|
adamc@1209
|
1112 SOME (String.implode (rev acc), cs)
|
adamc@1209
|
1113 else if c = #"\\" then
|
adamc@1209
|
1114 case cs of
|
adamc@1209
|
1115 c :: cs => loop (cs, c :: acc)
|
adamc@1209
|
1116 | _ => raise Fail "Iflow.string: Unmatched backslash escape"
|
adamc@1209
|
1117 else
|
adamc@1209
|
1118 loop (cs, c :: acc)
|
adamc@1209
|
1119 in
|
adamc@1209
|
1120 case loop (String.explode (String.extract (s, 1, NONE)), []) of
|
adamc@1209
|
1121 NONE => NONE
|
adamc@1209
|
1122 | SOME (s, []) => SOME (s, chs)
|
adamc@1209
|
1123 | SOME (s, cs) => SOME (s, String (String.implode cs) :: chs)
|
adamc@1209
|
1124 end
|
adamc@1209
|
1125 else
|
adamc@1209
|
1126 NONE
|
adamc@1209
|
1127 | _ => NONE
|
adamc@1206
|
1128
|
adamc@1209
|
1129 val prim =
|
adamc@1209
|
1130 altL [wrap (follow (wrapP (follow (keep Char.isDigit) (follow (const ".") (keep Char.isDigit)))
|
adamc@1209
|
1131 (fn (x, ((), y)) => Option.map Prim.Float (Real64.fromString (x ^ "." ^ y))))
|
adamc@1209
|
1132 (opt (const "::float8"))) #1,
|
adamc@1209
|
1133 wrap (follow (wrapP (keep Char.isDigit)
|
adamc@1209
|
1134 (Option.map Prim.Int o Int64.fromString))
|
adamc@1209
|
1135 (opt (const "::int8"))) #1,
|
adamc@1209
|
1136 wrap (follow (opt (const "E")) (follow string (opt (const "::text"))))
|
adamc@1209
|
1137 (Prim.String o #1 o #2)]
|
adamc@1206
|
1138
|
adamc@1207
|
1139 fun known' chs =
|
adamc@1207
|
1140 case chs of
|
adamc@1207
|
1141 Exp (EFfi ("Basis", "sql_known"), _) :: chs => SOME ((), chs)
|
adamc@1207
|
1142 | _ => NONE
|
adamc@1207
|
1143
|
adamc@1207
|
1144 fun sqlify chs =
|
adamc@1207
|
1145 case chs of
|
adamc@1207
|
1146 Exp (EFfiApp ("Basis", f, [e]), _) :: chs =>
|
adamc@1207
|
1147 if String.isPrefix "sqlify" f then
|
adamc@1207
|
1148 SOME (e, chs)
|
adamc@1207
|
1149 else
|
adamc@1207
|
1150 NONE
|
adamc@1207
|
1151 | _ => NONE
|
adamc@1207
|
1152
|
adamc@1211
|
1153 fun constK s = wrap (const s) (fn () => s)
|
adamc@1211
|
1154
|
adamc@1211
|
1155 val funcName = altL [constK "COUNT",
|
adamc@1211
|
1156 constK "MIN",
|
adamc@1211
|
1157 constK "MAX",
|
adamc@1211
|
1158 constK "SUM",
|
adamc@1211
|
1159 constK "AVG"]
|
adamc@1211
|
1160
|
adamc@1204
|
1161 fun sqexp chs =
|
adamc@1206
|
1162 log "sqexp"
|
adamc@1207
|
1163 (altL [wrap prim SqConst,
|
adamc@1211
|
1164 wrap field Field,
|
adamc@1207
|
1165 wrap known SqKnown,
|
adamc@1211
|
1166 wrap func SqFunc,
|
adamc@1211
|
1167 wrap (const "COUNT(*)") (fn () => Count),
|
adamc@1207
|
1168 wrap sqlify Inj,
|
adamc@1211
|
1169 wrap (follow (const "COALESCE(") (follow sqexp (follow (const ",")
|
adamc@1211
|
1170 (follow (keep (fn ch => ch <> #")")) (const ")")))))
|
adamc@1211
|
1171 (fn ((), (e, _)) => e),
|
adamc@1207
|
1172 wrap (follow (ws (const "("))
|
adamc@1207
|
1173 (follow (wrap
|
adamc@1207
|
1174 (follow sqexp
|
adamc@1207
|
1175 (alt
|
adamc@1207
|
1176 (wrap
|
adamc@1207
|
1177 (follow (ws sqbrel)
|
adamc@1207
|
1178 (ws sqexp))
|
adamc@1207
|
1179 inl)
|
adamc@1207
|
1180 (always (inr ()))))
|
adamc@1207
|
1181 (fn (e1, sm) =>
|
adamc@1207
|
1182 case sm of
|
adamc@1207
|
1183 inl (bo, e2) => Binop (bo, e1, e2)
|
adamc@1207
|
1184 | inr () => e1))
|
adamc@1207
|
1185 (const ")")))
|
adamc@1207
|
1186 (fn ((), (e, ())) => e)])
|
adamc@1207
|
1187 chs
|
adamc@1206
|
1188
|
adamc@1207
|
1189 and known chs = wrap (follow known' (follow (const "(") (follow sqexp (const ")"))))
|
adamc@1211
|
1190 (fn ((), ((), (e, ()))) => e) chs
|
adamc@1211
|
1191
|
adamc@1211
|
1192 and func chs = wrap (follow funcName (follow (const "(") (follow sqexp (const ")"))))
|
adamc@1211
|
1193 (fn (f, ((), (e, ()))) => (f, e)) chs
|
adamc@1211
|
1194
|
adamc@1211
|
1195 datatype sitem =
|
adamc@1211
|
1196 SqField of string * string
|
adamc@1211
|
1197 | SqExp of sqexp * string
|
adamc@1211
|
1198
|
adamc@1211
|
1199 val sitem = alt (wrap field SqField)
|
adamc@1211
|
1200 (wrap (follow sqexp (follow (const " AS ") uw_ident))
|
adamc@1211
|
1201 (fn (e, ((), s)) => SqExp (e, s)))
|
adamc@1207
|
1202
|
adamc@1207
|
1203 val select = log "select"
|
adamc@1207
|
1204 (wrap (follow (const "SELECT ") (list sitem))
|
adamc@1207
|
1205 (fn ((), ls) => ls))
|
adamc@1201
|
1206
|
adamc@1201
|
1207 val fitem = wrap (follow uw_ident
|
adamc@1201
|
1208 (follow (const " AS ")
|
adamc@1201
|
1209 t_ident))
|
adamc@1201
|
1210 (fn (t, ((), f)) => (t, f))
|
adamc@1201
|
1211
|
adamc@1207
|
1212 val from = log "from"
|
adamc@1207
|
1213 (wrap (follow (const "FROM ") (list fitem))
|
adamc@1207
|
1214 (fn ((), ls) => ls))
|
adamc@1201
|
1215
|
adamc@1204
|
1216 val wher = wrap (follow (ws (const "WHERE ")) sqexp)
|
adamc@1204
|
1217 (fn ((), ls) => ls)
|
adamc@1204
|
1218
|
adamc@1207
|
1219 val query = log "query"
|
adamc@1207
|
1220 (wrap (follow (follow select from) (opt wher))
|
adamc@1207
|
1221 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
|
adamc@1201
|
1222
|
adamc@1220
|
1223 datatype dml =
|
adamc@1220
|
1224 Insert of string * (string * sqexp) list
|
adamc@1220
|
1225
|
adamc@1220
|
1226 val insert = log "insert"
|
adamc@1220
|
1227 (wrapP (follow (const "INSERT INTO ")
|
adamc@1220
|
1228 (follow uw_ident
|
adamc@1220
|
1229 (follow (const " (")
|
adamc@1220
|
1230 (follow (list uw_ident)
|
adamc@1220
|
1231 (follow (const ") VALUES (")
|
adamc@1220
|
1232 (follow (list sqexp)
|
adamc@1220
|
1233 (const ")")))))))
|
adamc@1220
|
1234 (fn ((), (tab, ((), (fs, ((), (es, ())))))) =>
|
adamc@1220
|
1235 (SOME (Insert (tab, ListPair.zipEq (fs, es))))
|
adamc@1220
|
1236 handle ListPair.UnequalLengths => NONE))
|
adamc@1220
|
1237
|
adamc@1220
|
1238 val dml = log "dml"
|
adamc@1220
|
1239 insert
|
adamc@1220
|
1240
|
adamc@1215
|
1241 fun removeDups (ls : (string * string) list) =
|
adamc@1211
|
1242 case ls of
|
adamc@1211
|
1243 [] => []
|
adamc@1211
|
1244 | x :: ls =>
|
adamc@1211
|
1245 let
|
adamc@1211
|
1246 val ls = removeDups ls
|
adamc@1211
|
1247 in
|
adamc@1211
|
1248 if List.exists (fn x' => x' = x) ls then
|
adamc@1211
|
1249 ls
|
adamc@1211
|
1250 else
|
adamc@1211
|
1251 x :: ls
|
adamc@1211
|
1252 end
|
adamc@1211
|
1253
|
adamc@1212
|
1254 datatype queryMode =
|
adamc@1218
|
1255 SomeCol
|
adamc@1212
|
1256 | AllCols of exp
|
adamc@1212
|
1257
|
adamc@1220
|
1258 fun expIn rv env rvOf =
|
adamc@1220
|
1259 let
|
adamc@1220
|
1260 fun expIn (e, rvN) =
|
adamc@1220
|
1261 let
|
adamc@1220
|
1262 fun default () =
|
adamc@1220
|
1263 let
|
adamc@1220
|
1264 val (rvN, e) = rv rvN
|
adamc@1220
|
1265 in
|
adamc@1220
|
1266 (inl e, rvN)
|
adamc@1220
|
1267 end
|
adamc@1220
|
1268 in
|
adamc@1220
|
1269 case e of
|
adamc@1220
|
1270 SqConst p => (inl (Const p), rvN)
|
adamc@1220
|
1271 | Field (v, f) => (inl (Proj (rvOf v, f)), rvN)
|
adamc@1220
|
1272 | Binop (bo, e1, e2) =>
|
adamc@1220
|
1273 let
|
adamc@1220
|
1274 val (e1, rvN) = expIn (e1, rvN)
|
adamc@1220
|
1275 val (e2, rvN) = expIn (e2, rvN)
|
adamc@1220
|
1276 in
|
adamc@1220
|
1277 (inr (case (bo, e1, e2) of
|
adamc@1220
|
1278 (Exps f, inl e1, inl e2) => f (e1, e2)
|
adamc@1220
|
1279 | (Props f, inr p1, inr p2) => f (p1, p2)
|
adamc@1220
|
1280 | _ => Unknown), rvN)
|
adamc@1220
|
1281 end
|
adamc@1220
|
1282 | SqKnown e =>
|
adamc@1220
|
1283 (case expIn (e, rvN) of
|
adamc@1220
|
1284 (inl e, rvN) => (inr (Reln (Known, [e])), rvN)
|
adamc@1220
|
1285 | _ => (inr Unknown, rvN))
|
adamc@1220
|
1286 | Inj e =>
|
adamc@1220
|
1287 let
|
adamc@1220
|
1288 fun deinj e =
|
adamc@1220
|
1289 case #1 e of
|
adamc@1220
|
1290 ERel n => (List.nth (env, n), rvN)
|
adamc@1220
|
1291 | EField (e, f) =>
|
adamc@1220
|
1292 let
|
adamc@1220
|
1293 val (e, rvN) = deinj e
|
adamc@1220
|
1294 in
|
adamc@1220
|
1295 (Proj (e, f), rvN)
|
adamc@1220
|
1296 end
|
adamc@1220
|
1297 | _ =>
|
adamc@1220
|
1298 let
|
adamc@1220
|
1299 val (rvN, e) = rv rvN
|
adamc@1220
|
1300 in
|
adamc@1220
|
1301 (e, rvN)
|
adamc@1220
|
1302 end
|
adamc@1220
|
1303
|
adamc@1220
|
1304 val (e, rvN) = deinj e
|
adamc@1220
|
1305 in
|
adamc@1220
|
1306 (inl e, rvN)
|
adamc@1220
|
1307 end
|
adamc@1220
|
1308 | SqFunc (f, e) =>
|
adamc@1220
|
1309 (case expIn (e, rvN) of
|
adamc@1220
|
1310 (inl e, rvN) => (inl (Func (Other f, [e])), rvN)
|
adamc@1220
|
1311 | _ => default ())
|
adamc@1220
|
1312
|
adamc@1220
|
1313 | Count => default ()
|
adamc@1220
|
1314 end
|
adamc@1220
|
1315 in
|
adamc@1220
|
1316 expIn
|
adamc@1220
|
1317 end
|
adamc@1216
|
1318
|
adamc@1214
|
1319 fun queryProp env rvN rv oe e =
|
adamc@1216
|
1320 let
|
adamc@1216
|
1321 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1216
|
1322 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1218
|
1323 (rvN, Unknown, Unknown, [], []))
|
adamc@1216
|
1324 in
|
adamc@1216
|
1325 case parse query e of
|
adamc@1216
|
1326 NONE => default ()
|
adamc@1216
|
1327 | SOME r =>
|
adamc@1216
|
1328 let
|
adamc@1216
|
1329 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1216
|
1330 let
|
adamc@1216
|
1331 val (rvN, e) = rv rvN
|
adamc@1216
|
1332 in
|
adamc@1216
|
1333 ((v, e), rvN)
|
adamc@1216
|
1334 end) rvN (#From r)
|
adamc@1214
|
1335
|
adamc@1216
|
1336 fun rvOf v =
|
adamc@1216
|
1337 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1216
|
1338 NONE => raise Fail "Iflow.queryProp: Bad table variable"
|
adamc@1216
|
1339 | SOME (_, e) => e
|
adamc@1214
|
1340
|
adamc@1216
|
1341 fun usedFields e =
|
adamc@1216
|
1342 case e of
|
adamc@1216
|
1343 SqConst _ => []
|
adamc@1216
|
1344 | Field (v, f) => [(v, f)]
|
adamc@1216
|
1345 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
|
adamc@1216
|
1346 | SqKnown _ => []
|
adamc@1216
|
1347 | Inj _ => []
|
adamc@1216
|
1348 | SqFunc (_, e) => usedFields e
|
adamc@1216
|
1349 | Count => []
|
adamc@1214
|
1350
|
adamc@1216
|
1351 val p =
|
adamc@1216
|
1352 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
|
adamc@1211
|
1353
|
adamc@1220
|
1354 val expIn = expIn rv env rvOf
|
adamc@1205
|
1355
|
adamc@1220
|
1356 val (p, rvN) = case #Where r of
|
adamc@1220
|
1357 NONE => (p, rvN)
|
adamc@1220
|
1358 | SOME e =>
|
adamc@1220
|
1359 case expIn (e, rvN) of
|
adamc@1220
|
1360 (inr p', rvN) => (And (p, p'), rvN)
|
adamc@1220
|
1361 | _ => (p, rvN)
|
adamc@1210
|
1362
|
adamc@1216
|
1363 fun normal () =
|
adamc@1218
|
1364 case oe of
|
adamc@1218
|
1365 SomeCol =>
|
adamc@1220
|
1366 let
|
adamc@1220
|
1367 val (sis, rvN) =
|
adamc@1220
|
1368 ListUtil.foldlMap
|
adamc@1220
|
1369 (fn (si, rvN) =>
|
adamc@1220
|
1370 case si of
|
adamc@1220
|
1371 SqField (v, f) => (Proj (rvOf v, f), rvN)
|
adamc@1220
|
1372 | SqExp (e, f) =>
|
adamc@1220
|
1373 case expIn (e, rvN) of
|
adamc@1220
|
1374 (inr _, _) =>
|
adamc@1220
|
1375 let
|
adamc@1220
|
1376 val (rvN, e) = rv rvN
|
adamc@1220
|
1377 in
|
adamc@1220
|
1378 (e, rvN)
|
adamc@1220
|
1379 end
|
adamc@1220
|
1380 | (inl e, rvN) => (e, rvN)) rvN (#Select r)
|
adamc@1220
|
1381 in
|
adamc@1220
|
1382 (rvN, p, True, sis)
|
adamc@1220
|
1383 end
|
adamc@1218
|
1384 | AllCols oe =>
|
adamc@1220
|
1385 let
|
adamc@1220
|
1386 val (p', rvN) =
|
adamc@1220
|
1387 foldl (fn (si, (p, rvN)) =>
|
adamc@1220
|
1388 let
|
adamc@1220
|
1389 val (p', rvN) =
|
adamc@1220
|
1390 case si of
|
adamc@1220
|
1391 SqField (v, f) => (Reln (Eq, [Proj (Proj (oe, v), f),
|
adamc@1220
|
1392 Proj (rvOf v, f)]), rvN)
|
adamc@1220
|
1393 | SqExp (e, f) =>
|
adamc@1220
|
1394 case expIn (e, rvN) of
|
adamc@1220
|
1395 (inr p, rvN) => (Cond (Proj (oe, f), p), rvN)
|
adamc@1220
|
1396 | (inl e, rvN) => (Reln (Eq, [Proj (oe, f), e]), rvN)
|
adamc@1220
|
1397 in
|
adamc@1220
|
1398 (And (p, p'), rvN)
|
adamc@1220
|
1399 end)
|
adamc@1220
|
1400 (True, rvN) (#Select r)
|
adamc@1220
|
1401 in
|
adamc@1220
|
1402 (rvN, And (p, p'), True, [])
|
adamc@1220
|
1403 end
|
adamc@1217
|
1404
|
adamc@1218
|
1405 val (rvN, p, wp, outs) =
|
adamc@1217
|
1406 case #Select r of
|
adamc@1217
|
1407 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
|
adamc@1217
|
1408 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
|
adamc@1217
|
1409 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
|
adamc@1218
|
1410 (case oe of
|
adamc@1218
|
1411 SomeCol =>
|
adamc@1218
|
1412 let
|
adamc@1218
|
1413 val (rvN, oe) = rv rvN
|
adamc@1218
|
1414 in
|
adamc@1218
|
1415 (rvN,
|
adamc@1218
|
1416 Or (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.False", [])]),
|
adamc@1218
|
1417 And (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1418 p)),
|
adamc@1218
|
1419 Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1420 [oe])
|
adamc@1218
|
1421 end
|
adamc@1218
|
1422 | AllCols oe =>
|
adamc@1218
|
1423 let
|
adamc@1218
|
1424 val oe = Proj (oe, f)
|
adamc@1218
|
1425 in
|
adamc@1218
|
1426 (rvN,
|
adamc@1218
|
1427 Or (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.False", [])]),
|
adamc@1218
|
1428 And (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1429 p)),
|
adamc@1218
|
1430 Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
|
adamc@1218
|
1431 [])
|
adamc@1218
|
1432 end)
|
adamc@1217
|
1433 | _ => normal ())
|
adamc@1217
|
1434 | _ => normal ()
|
adamc@1216
|
1435 in
|
adamc@1217
|
1436 (rvN, p, wp, case #Where r of
|
adamc@1217
|
1437 NONE => []
|
adamc@1218
|
1438 | SOME e => map (fn (v, f) => Proj (rvOf v, f)) (usedFields e), outs)
|
adamc@1216
|
1439 end
|
adamc@1220
|
1440 end
|
adamc@1220
|
1441
|
adamc@1220
|
1442 fun insertProp rvN rv e =
|
adamc@1220
|
1443 let
|
adamc@1220
|
1444 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
|
adamc@1220
|
1445 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
|
adamc@1220
|
1446 Unknown)
|
adamc@1220
|
1447 in
|
adamc@1220
|
1448 case parse query e of
|
adamc@1220
|
1449 NONE => default ()
|
adamc@1220
|
1450 | SOME r =>
|
adamc@1220
|
1451 let
|
adamc@1220
|
1452 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
|
adamc@1220
|
1453 let
|
adamc@1220
|
1454 val (rvN, e) = rv rvN
|
adamc@1220
|
1455 in
|
adamc@1220
|
1456 ((v, e), rvN)
|
adamc@1220
|
1457 end) rvN (#From r)
|
adamc@1220
|
1458
|
adamc@1220
|
1459 fun rvOf v =
|
adamc@1220
|
1460 case List.find (fn (v', _) => v' = v) rvs of
|
adamc@1220
|
1461 NONE => raise Fail "Iflow.insertProp: Bad table variable"
|
adamc@1220
|
1462 | SOME (_, e) => e
|
adamc@1220
|
1463
|
adamc@1220
|
1464 val p =
|
adamc@1220
|
1465 foldl (fn ((t, v), p) =>
|
adamc@1220
|
1466 let
|
adamc@1220
|
1467 val t =
|
adamc@1220
|
1468 case v of
|
adamc@1220
|
1469 "New" => "$New"
|
adamc@1220
|
1470 | _ => t
|
adamc@1220
|
1471 in
|
adamc@1220
|
1472 And (p, Reln (Sql t, [rvOf v]))
|
adamc@1220
|
1473 end) True (#From r)
|
adamc@1220
|
1474
|
adamc@1220
|
1475 val expIn = expIn rv [] rvOf
|
adamc@1220
|
1476 in
|
adamc@1220
|
1477 case #Where r of
|
adamc@1220
|
1478 NONE => p
|
adamc@1220
|
1479 | SOME e =>
|
adamc@1220
|
1480 case expIn (e, rvN) of
|
adamc@1220
|
1481 (inr p', _) => And (p, p')
|
adamc@1220
|
1482 | _ => p
|
adamc@1220
|
1483 end
|
adamc@1216
|
1484 end
|
adamc@1200
|
1485
|
adamc@1211
|
1486 fun evalPat env e (pt, _) =
|
adamc@1211
|
1487 case pt of
|
adamc@1211
|
1488 PWild => (env, True)
|
adamc@1211
|
1489 | PVar _ => (e :: env, True)
|
adamc@1211
|
1490 | PPrim _ => (env, True)
|
adamc@1215
|
1491 | PCon (_, pc, NONE) => (env, Reln (PCon0 (patCon pc), [e]))
|
adamc@1211
|
1492 | PCon (_, pc, SOME pt) =>
|
adamc@1211
|
1493 let
|
adamc@1215
|
1494 val (env, p) = evalPat env (Func (UnCon (patCon pc), [e])) pt
|
adamc@1211
|
1495 in
|
adamc@1215
|
1496 (env, And (p, Reln (PCon1 (patCon pc), [e])))
|
adamc@1211
|
1497 end
|
adamc@1211
|
1498 | PRecord xpts =>
|
adamc@1211
|
1499 foldl (fn ((x, pt, _), (env, p)) =>
|
adamc@1211
|
1500 let
|
adamc@1211
|
1501 val (env, p') = evalPat env (Proj (e, x)) pt
|
adamc@1211
|
1502 in
|
adamc@1211
|
1503 (env, And (p', p))
|
adamc@1211
|
1504 end) (env, True) xpts
|
adamc@1215
|
1505 | PNone _ => (env, Reln (PCon0 "None", [e]))
|
adamc@1211
|
1506 | PSome (_, pt) =>
|
adamc@1211
|
1507 let
|
adamc@1215
|
1508 val (env, p) = evalPat env (Func (UnCon "Some", [e])) pt
|
adamc@1211
|
1509 in
|
adamc@1215
|
1510 (env, And (p, Reln (PCon1 "Some", [e])))
|
adamc@1211
|
1511 end
|
adamc@1211
|
1512
|
adamc@1211
|
1513 fun peq (p1, p2) =
|
adamc@1211
|
1514 case (p1, p2) of
|
adamc@1211
|
1515 (True, True) => true
|
adamc@1211
|
1516 | (False, False) => true
|
adamc@1211
|
1517 | (Unknown, Unknown) => true
|
adamc@1211
|
1518 | (And (x1, y1), And (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
|
adamc@1211
|
1519 | (Or (x1, y1), Or (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
|
adamc@1211
|
1520 | (Reln (r1, es1), Reln (r2, es2)) => r1 = r2 andalso ListPair.allEq eeq (es1, es2)
|
adamc@1212
|
1521 | (Cond (e1, p1), Cond (e2, p2)) => eeq (e1, e2) andalso peq (p1, p2)
|
adamc@1211
|
1522 | _ => false
|
adamc@1211
|
1523
|
adamc@1211
|
1524 fun removeRedundant p1 =
|
adamc@1211
|
1525 let
|
adamc@1211
|
1526 fun rr p2 =
|
adamc@1211
|
1527 if peq (p1, p2) then
|
adamc@1211
|
1528 True
|
adamc@1211
|
1529 else
|
adamc@1211
|
1530 case p2 of
|
adamc@1211
|
1531 And (x, y) => And (rr x, rr y)
|
adamc@1211
|
1532 | Or (x, y) => Or (rr x, rr y)
|
adamc@1211
|
1533 | _ => p2
|
adamc@1211
|
1534 in
|
adamc@1211
|
1535 rr
|
adamc@1211
|
1536 end
|
adamc@1211
|
1537
|
adamc@1218
|
1538 datatype cflow = Case | Where
|
adamc@1218
|
1539 datatype flow = Data | Control of cflow
|
adamc@1218
|
1540 type check = ErrorMsg.span * exp * prop
|
adamc@1220
|
1541 type insert = ErrorMsg.span * prop
|
adamc@1218
|
1542
|
adamc@1217
|
1543 structure St :> sig
|
adamc@1217
|
1544 type t
|
adamc@1217
|
1545 val create : {Var : int,
|
adamc@1217
|
1546 Ambient : prop} -> t
|
adamc@1217
|
1547
|
adamc@1217
|
1548 val curVar : t -> int
|
adamc@1217
|
1549 val nextVar : t -> t * int
|
adamc@1217
|
1550
|
adamc@1217
|
1551 val ambient : t -> prop
|
adamc@1217
|
1552 val setAmbient : t * prop -> t
|
adamc@1217
|
1553
|
adamc@1218
|
1554 val paths : t -> (check * cflow) list
|
adamc@1218
|
1555 val addPath : t * (check * cflow) -> t
|
adamc@1218
|
1556 val addPaths : t * (check * cflow) list -> t
|
adamc@1218
|
1557 val clearPaths : t -> t
|
adamc@1218
|
1558 val setPaths : t * (check * cflow) list -> t
|
adamc@1217
|
1559
|
adamc@1218
|
1560 val sent : t -> (check * flow) list
|
adamc@1218
|
1561 val addSent : t * (check * flow) -> t
|
adamc@1218
|
1562 val setSent : t * (check * flow) list -> t
|
adamc@1220
|
1563
|
adamc@1220
|
1564 val inserted : t -> insert list
|
adamc@1220
|
1565 val addInsert : t * insert -> t
|
adamc@1217
|
1566 end = struct
|
adamc@1217
|
1567
|
adamc@1217
|
1568 type t = {Var : int,
|
adamc@1217
|
1569 Ambient : prop,
|
adamc@1218
|
1570 Path : (check * cflow) list,
|
adamc@1220
|
1571 Sent : (check * flow) list,
|
adamc@1220
|
1572 Insert : insert list}
|
adamc@1217
|
1573
|
adamc@1217
|
1574 fun create {Var = v, Ambient = p} = {Var = v,
|
adamc@1217
|
1575 Ambient = p,
|
adamc@1217
|
1576 Path = [],
|
adamc@1220
|
1577 Sent = [],
|
adamc@1220
|
1578 Insert = []}
|
adamc@1217
|
1579
|
adamc@1217
|
1580 fun curVar (t : t) = #Var t
|
adamc@1217
|
1581 fun nextVar (t : t) = ({Var = #Var t + 1,
|
adamc@1217
|
1582 Ambient = #Ambient t,
|
adamc@1217
|
1583 Path = #Path t,
|
adamc@1220
|
1584 Sent = #Sent t,
|
adamc@1220
|
1585 Insert = #Insert t}, #Var t)
|
adamc@1217
|
1586
|
adamc@1217
|
1587 fun ambient (t : t) = #Ambient t
|
adamc@1217
|
1588 fun setAmbient (t : t, p) = {Var = #Var t,
|
adamc@1217
|
1589 Ambient = p,
|
adamc@1217
|
1590 Path = #Path t,
|
adamc@1220
|
1591 Sent = #Sent t,
|
adamc@1220
|
1592 Insert = #Insert t}
|
adamc@1217
|
1593
|
adamc@1218
|
1594 fun paths (t : t) = #Path t
|
adamc@1217
|
1595 fun addPath (t : t, c) = {Var = #Var t,
|
adamc@1217
|
1596 Ambient = #Ambient t,
|
adamc@1217
|
1597 Path = c :: #Path t,
|
adamc@1220
|
1598 Sent = #Sent t,
|
adamc@1220
|
1599 Insert = #Insert t}
|
adamc@1218
|
1600 fun addPaths (t : t, cs) = {Var = #Var t,
|
adamc@1218
|
1601 Ambient = #Ambient t,
|
adamc@1218
|
1602 Path = cs @ #Path t,
|
adamc@1220
|
1603 Sent = #Sent t,
|
adamc@1220
|
1604 Insert = #Insert t}
|
adamc@1218
|
1605 fun clearPaths (t : t) = {Var = #Var t,
|
adamc@1218
|
1606 Ambient = #Ambient t,
|
adamc@1218
|
1607 Path = [],
|
adamc@1220
|
1608 Sent = #Sent t,
|
adamc@1220
|
1609 Insert = #Insert t}
|
adamc@1218
|
1610 fun setPaths (t : t, cs) = {Var = #Var t,
|
adamc@1218
|
1611 Ambient = #Ambient t,
|
adamc@1218
|
1612 Path = cs,
|
adamc@1220
|
1613 Sent = #Sent t,
|
adamc@1220
|
1614 Insert = #Insert t}
|
adamc@1217
|
1615
|
adamc@1217
|
1616 fun sent (t : t) = #Sent t
|
adamc@1217
|
1617 fun addSent (t : t, c) = {Var = #Var t,
|
adamc@1217
|
1618 Ambient = #Ambient t,
|
adamc@1217
|
1619 Path = #Path t,
|
adamc@1220
|
1620 Sent = c :: #Sent t,
|
adamc@1220
|
1621 Insert = #Insert t}
|
adamc@1217
|
1622 fun setSent (t : t, cs) = {Var = #Var t,
|
adamc@1217
|
1623 Ambient = #Ambient t,
|
adamc@1217
|
1624 Path = #Path t,
|
adamc@1220
|
1625 Sent = cs,
|
adamc@1220
|
1626 Insert = #Insert t}
|
adamc@1220
|
1627
|
adamc@1220
|
1628 fun inserted (t : t) = #Insert t
|
adamc@1220
|
1629 fun addInsert (t : t, c) = {Var = #Var t,
|
adamc@1220
|
1630 Ambient = #Ambient t,
|
adamc@1220
|
1631 Path = #Path t,
|
adamc@1220
|
1632 Sent = #Sent t,
|
adamc@1220
|
1633 Insert = c :: #Insert t}
|
adamc@1217
|
1634
|
adamc@1217
|
1635 end
|
adamc@1217
|
1636
|
adamc@1217
|
1637 fun evalExp env (e as (_, loc), st) =
|
adamc@1200
|
1638 let
|
adamc@1200
|
1639 fun default () =
|
adamc@1217
|
1640 let
|
adamc@1217
|
1641 val (st, nv) = St.nextVar st
|
adamc@1217
|
1642 in
|
adamc@1217
|
1643 (Var nv, st)
|
adamc@1217
|
1644 end
|
adamc@1200
|
1645
|
adamc@1217
|
1646 fun addSent (p, e, st) =
|
adamc@1218
|
1647 let
|
adamc@1218
|
1648 val st = if isKnown e then
|
adamc@1218
|
1649 st
|
adamc@1218
|
1650 else
|
adamc@1218
|
1651 St.addSent (st, ((loc, e, p), Data))
|
adamc@1218
|
1652
|
adamc@1218
|
1653 val st = foldl (fn ((c, fl), st) => St.addSent (st, (c, Control fl))) st (St.paths st)
|
adamc@1218
|
1654 in
|
adamc@1218
|
1655 St.clearPaths st
|
adamc@1218
|
1656 end
|
adamc@1200
|
1657 in
|
adamc@1200
|
1658 case #1 e of
|
adamc@1200
|
1659 EPrim p => (Const p, st)
|
adamc@1200
|
1660 | ERel n => (List.nth (env, n), st)
|
adamc@1200
|
1661 | ENamed _ => default ()
|
adamc@1215
|
1662 | ECon (_, pc, NONE) => (Func (DtCon0 (patCon pc), []), st)
|
adamc@1200
|
1663 | ECon (_, pc, SOME e) =>
|
adamc@1200
|
1664 let
|
adamc@1200
|
1665 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1666 in
|
adamc@1215
|
1667 (Func (DtCon1 (patCon pc), [e]), st)
|
adamc@1200
|
1668 end
|
adamc@1215
|
1669 | ENone _ => (Func (DtCon0 "None", []), st)
|
adamc@1200
|
1670 | ESome (_, e) =>
|
adamc@1200
|
1671 let
|
adamc@1200
|
1672 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1673 in
|
adamc@1215
|
1674 (Func (DtCon1 "Some", [e]), st)
|
adamc@1200
|
1675 end
|
adamc@1200
|
1676 | EFfi _ => default ()
|
adamc@1213
|
1677
|
adamc@1200
|
1678 | EFfiApp (m, s, es) =>
|
adamc@1200
|
1679 if m = "Basis" andalso SS.member (writers, s) then
|
adamc@1200
|
1680 let
|
adamc@1200
|
1681 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1682 in
|
adamc@1217
|
1683 (Recd [], foldl (fn (e, st) => addSent (St.ambient st, e, st)) st es)
|
adamc@1200
|
1684 end
|
adamc@1200
|
1685 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
|
adamc@1200
|
1686 default ()
|
adamc@1200
|
1687 else
|
adamc@1200
|
1688 let
|
adamc@1200
|
1689 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1690 in
|
adamc@1215
|
1691 (Func (Other (m ^ "." ^ s), es), st)
|
adamc@1200
|
1692 end
|
adamc@1213
|
1693
|
adamc@1213
|
1694 | EApp (e1, e2) =>
|
adamc@1213
|
1695 let
|
adamc@1213
|
1696 val (e1, st) = evalExp env (e1, st)
|
adamc@1213
|
1697 in
|
adamc@1213
|
1698 case e1 of
|
adamc@1213
|
1699 Finish => (Finish, st)
|
adamc@1213
|
1700 | _ => default ()
|
adamc@1213
|
1701 end
|
adamc@1213
|
1702
|
adamc@1200
|
1703 | EAbs _ => default ()
|
adamc@1200
|
1704 | EUnop (s, e1) =>
|
adamc@1200
|
1705 let
|
adamc@1200
|
1706 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1707 in
|
adamc@1215
|
1708 (Func (Other s, [e1]), st)
|
adamc@1200
|
1709 end
|
adamc@1200
|
1710 | EBinop (s, e1, e2) =>
|
adamc@1200
|
1711 let
|
adamc@1200
|
1712 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1713 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
1714 in
|
adamc@1215
|
1715 (Func (Other s, [e1, e2]), st)
|
adamc@1200
|
1716 end
|
adamc@1200
|
1717 | ERecord xets =>
|
adamc@1200
|
1718 let
|
adamc@1200
|
1719 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
|
adamc@1200
|
1720 let
|
adamc@1200
|
1721 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1722 in
|
adamc@1200
|
1723 ((x, e), st)
|
adamc@1200
|
1724 end) st xets
|
adamc@1200
|
1725 in
|
adamc@1200
|
1726 (Recd xes, st)
|
adamc@1200
|
1727 end
|
adamc@1200
|
1728 | EField (e, s) =>
|
adamc@1200
|
1729 let
|
adamc@1200
|
1730 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1731 in
|
adamc@1200
|
1732 (Proj (e, s), st)
|
adamc@1200
|
1733 end
|
adamc@1218
|
1734 | ECase (e, pes, {result = res, ...}) =>
|
adamc@1211
|
1735 let
|
adamc@1211
|
1736 val (e, st) = evalExp env (e, st)
|
adamc@1217
|
1737 val (st, r) = St.nextVar st
|
adamc@1217
|
1738 val orig = St.ambient st
|
adamc@1218
|
1739 val origPaths = St.paths st
|
adamc@1211
|
1740
|
adamc@1218
|
1741 val st = St.addPath (st, ((loc, e, orig), Case))
|
adamc@1218
|
1742
|
adamc@1218
|
1743 val (st, paths) =
|
adamc@1218
|
1744 foldl (fn ((pt, pe), (st, paths)) =>
|
adamc@1218
|
1745 let
|
adamc@1218
|
1746 val (env, pp) = evalPat env e pt
|
adamc@1218
|
1747 val (pe, st') = evalExp env (pe, St.setAmbient (st, And (orig, pp)))
|
adamc@1218
|
1748
|
adamc@1218
|
1749 val this = And (removeRedundant orig (St.ambient st'),
|
adamc@1218
|
1750 Reln (Eq, [Var r, pe]))
|
adamc@1218
|
1751 in
|
adamc@1218
|
1752 (St.setPaths (St.setAmbient (st', Or (St.ambient st, this)), origPaths),
|
adamc@1218
|
1753 St.paths st' @ paths)
|
adamc@1218
|
1754 end) (St.setAmbient (st, False), []) pes
|
adamc@1218
|
1755
|
adamc@1218
|
1756 val st = case #1 res of
|
adamc@1218
|
1757 TRecord [] => St.setPaths (st, origPaths)
|
adamc@1218
|
1758 | _ => St.setPaths (st, paths)
|
adamc@1211
|
1759 in
|
adamc@1217
|
1760 (Var r, St.setAmbient (st, And (orig, St.ambient st)))
|
adamc@1211
|
1761 end
|
adamc@1200
|
1762 | EStrcat (e1, e2) =>
|
adamc@1200
|
1763 let
|
adamc@1200
|
1764 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1765 val (e2, st) = evalExp env (e2, st)
|
adamc@1200
|
1766 in
|
adamc@1215
|
1767 (Func (Other "cat", [e1, e2]), st)
|
adamc@1200
|
1768 end
|
adamc@1200
|
1769 | EError _ => (Finish, st)
|
adamc@1200
|
1770 | EReturnBlob {blob = b, mimeType = m, ...} =>
|
adamc@1200
|
1771 let
|
adamc@1200
|
1772 val (b, st) = evalExp env (b, st)
|
adamc@1200
|
1773 val (m, st) = evalExp env (m, st)
|
adamc@1200
|
1774 in
|
adamc@1217
|
1775 (Finish, addSent (St.ambient st, b, addSent (St.ambient st, m, st)))
|
adamc@1200
|
1776 end
|
adamc@1200
|
1777 | ERedirect (e, _) =>
|
adamc@1200
|
1778 let
|
adamc@1200
|
1779 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1780 in
|
adamc@1217
|
1781 (Finish, addSent (St.ambient st, e, st))
|
adamc@1200
|
1782 end
|
adamc@1200
|
1783 | EWrite e =>
|
adamc@1200
|
1784 let
|
adamc@1200
|
1785 val (e, st) = evalExp env (e, st)
|
adamc@1200
|
1786 in
|
adamc@1217
|
1787 (Recd [], addSent (St.ambient st, e, st))
|
adamc@1200
|
1788 end
|
adamc@1200
|
1789 | ESeq (e1, e2) =>
|
adamc@1200
|
1790 let
|
adamc@1200
|
1791 val (_, st) = evalExp env (e1, st)
|
adamc@1200
|
1792 in
|
adamc@1200
|
1793 evalExp env (e2, st)
|
adamc@1200
|
1794 end
|
adamc@1200
|
1795 | ELet (_, _, e1, e2) =>
|
adamc@1200
|
1796 let
|
adamc@1200
|
1797 val (e1, st) = evalExp env (e1, st)
|
adamc@1200
|
1798 in
|
adamc@1200
|
1799 evalExp (e1 :: env) (e2, st)
|
adamc@1200
|
1800 end
|
adamc@1200
|
1801 | EClosure (n, es) =>
|
adamc@1200
|
1802 let
|
adamc@1200
|
1803 val (es, st) = ListUtil.foldlMap (evalExp env) st es
|
adamc@1200
|
1804 in
|
adamc@1215
|
1805 (Func (Other ("Cl" ^ Int.toString n), es), st)
|
adamc@1200
|
1806 end
|
adamc@1200
|
1807
|
adamc@1200
|
1808 | EQuery {query = q, body = b, initial = i, ...} =>
|
adamc@1200
|
1809 let
|
adamc@1200
|
1810 val (_, st) = evalExp env (q, st)
|
adamc@1200
|
1811 val (i, st) = evalExp env (i, st)
|
adamc@1200
|
1812
|
adamc@1217
|
1813 val (st', r) = St.nextVar st
|
adamc@1217
|
1814 val (st', acc) = St.nextVar st'
|
adamc@1200
|
1815
|
adamc@1200
|
1816 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
|
adamc@1200
|
1817
|
adamc@1218
|
1818 val (st', qp, qwp, used, _) =
|
adamc@1214
|
1819 queryProp env
|
adamc@1217
|
1820 st' (fn st' =>
|
adamc@1217
|
1821 let
|
adamc@1217
|
1822 val (st', rv) = St.nextVar st'
|
adamc@1217
|
1823 in
|
adamc@1217
|
1824 (st', Var rv)
|
adamc@1217
|
1825 end)
|
adamc@1214
|
1826 (AllCols (Var r)) q
|
adamc@1200
|
1827
|
adamc@1217
|
1828 val p' = And (qp, St.ambient st')
|
adamc@1200
|
1829
|
adamc@1217
|
1830 val (st, res) = if varInP acc (St.ambient st') then
|
adamc@1217
|
1831 let
|
adamc@1217
|
1832 val (st, r) = St.nextVar st
|
adamc@1217
|
1833 in
|
adamc@1217
|
1834 (st, Var r)
|
adamc@1217
|
1835 end
|
adamc@1217
|
1836 else
|
adamc@1217
|
1837 let
|
adamc@1217
|
1838 val (st, out) = St.nextVar st'
|
adamc@1217
|
1839
|
adamc@1217
|
1840 val p = Or (Reln (Eq, [Var out, i]),
|
adamc@1217
|
1841 And (Reln (Eq, [Var out, b]),
|
adamc@1217
|
1842 p'))
|
adamc@1217
|
1843 in
|
adamc@1217
|
1844 (St.setAmbient (st, p), Var out)
|
adamc@1217
|
1845 end
|
adamc@1210
|
1846
|
adamc@1218
|
1847 val sent = map (fn ((loc, e, p), fl) => ((loc, e, And (qp, p)), fl)) (St.sent st')
|
adamc@1212
|
1848
|
adamc@1217
|
1849 val p' = And (p', qwp)
|
adamc@1218
|
1850 val paths = map (fn e => ((loc, e, p'), Where)) used
|
adamc@1200
|
1851 in
|
adamc@1218
|
1852 (res, St.addPaths (St.setSent (st, sent), paths))
|
adamc@1200
|
1853 end
|
adamc@1220
|
1854 | EDml e =>
|
adamc@1220
|
1855 (case parse dml e of
|
adamc@1220
|
1856 NONE => (print ("Warning: Information flow checker can't parse DML command at "
|
adamc@1220
|
1857 ^ ErrorMsg.spanToString loc ^ "\n");
|
adamc@1220
|
1858 default ())
|
adamc@1220
|
1859 | SOME d =>
|
adamc@1220
|
1860 case d of
|
adamc@1220
|
1861 Insert (tab, es) =>
|
adamc@1220
|
1862 let
|
adamc@1220
|
1863 val (st, new) = St.nextVar st
|
adamc@1220
|
1864
|
adamc@1220
|
1865 fun rv st =
|
adamc@1220
|
1866 let
|
adamc@1220
|
1867 val (st, n) = St.nextVar st
|
adamc@1220
|
1868 in
|
adamc@1220
|
1869 (st, Var n)
|
adamc@1220
|
1870 end
|
adamc@1220
|
1871
|
adamc@1220
|
1872 val expIn = expIn rv env (fn "New" => Var new
|
adamc@1220
|
1873 | _ => raise Fail "Iflow.evalExp: Bad field expression in EDml")
|
adamc@1220
|
1874
|
adamc@1220
|
1875 val (es, st) = ListUtil.foldlMap
|
adamc@1220
|
1876 (fn ((x, e), st) =>
|
adamc@1220
|
1877 let
|
adamc@1220
|
1878 val (e, st) = case expIn (e, st) of
|
adamc@1220
|
1879 (inl e, st) => (e, st)
|
adamc@1220
|
1880 | (inr _, _) => raise Fail
|
adamc@1220
|
1881 ("Iflow.evalExp: Selecting "
|
adamc@1220
|
1882 ^ "boolean expression")
|
adamc@1220
|
1883 in
|
adamc@1220
|
1884 ((x, e), st)
|
adamc@1220
|
1885 end)
|
adamc@1220
|
1886 st es
|
adamc@1220
|
1887 in
|
adamc@1220
|
1888 (Recd [], St.addInsert (st, (loc, And (St.ambient st,
|
adamc@1220
|
1889 Reln (Sql "$New", [Recd es])))))
|
adamc@1220
|
1890 end)
|
adamc@1220
|
1891
|
adamc@1200
|
1892 | ENextval _ => default ()
|
adamc@1200
|
1893 | ESetval _ => default ()
|
adamc@1200
|
1894
|
adamc@1213
|
1895 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
|
adamc@1217
|
1896 let
|
adamc@1217
|
1897 val (st, nv) = St.nextVar st
|
adamc@1217
|
1898 in
|
adamc@1217
|
1899 (Var nv, St.setAmbient (st, And (St.ambient st, Reln (Known, [Var nv]))))
|
adamc@1217
|
1900 end
|
adamc@1213
|
1901
|
adamc@1200
|
1902 | EUnurlify _ => default ()
|
adamc@1200
|
1903 | EJavaScript _ => default ()
|
adamc@1200
|
1904 | ESignalReturn _ => default ()
|
adamc@1200
|
1905 | ESignalBind _ => default ()
|
adamc@1200
|
1906 | ESignalSource _ => default ()
|
adamc@1200
|
1907 | EServerCall _ => default ()
|
adamc@1200
|
1908 | ERecv _ => default ()
|
adamc@1200
|
1909 | ESleep _ => default ()
|
adamc@1200
|
1910 | ESpawn _ => default ()
|
adamc@1200
|
1911 end
|
adamc@1200
|
1912
|
adamc@1200
|
1913 fun check file =
|
adamc@1200
|
1914 let
|
adamc@1213
|
1915 val file = MonoReduce.reduce file
|
adamc@1213
|
1916 val file = MonoOpt.optimize file
|
adamc@1213
|
1917 val file = Fuse.fuse file
|
adamc@1213
|
1918 val file = MonoOpt.optimize file
|
adamc@1216
|
1919 val file = MonoShake.shake file
|
adamc@1213
|
1920 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
|
adamc@1213
|
1921
|
adamc@1207
|
1922 val exptd = foldl (fn ((d, _), exptd) =>
|
adamc@1207
|
1923 case d of
|
adamc@1207
|
1924 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
|
adamc@1207
|
1925 | _ => exptd) IS.empty file
|
adamc@1207
|
1926
|
adamc@1220
|
1927 fun decl ((d, _), (vals, inserts, client, insert)) =
|
adamc@1200
|
1928 case d of
|
adamc@1207
|
1929 DVal (_, n, _, e, _) =>
|
adamc@1200
|
1930 let
|
adamc@1207
|
1931 val isExptd = IS.member (exptd, n)
|
adamc@1207
|
1932
|
adamc@1207
|
1933 fun deAbs (e, env, nv, p) =
|
adamc@1200
|
1934 case #1 e of
|
adamc@1207
|
1935 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1,
|
adamc@1207
|
1936 if isExptd then
|
adamc@1207
|
1937 And (p, Reln (Known, [Var nv]))
|
adamc@1207
|
1938 else
|
adamc@1207
|
1939 p)
|
adamc@1207
|
1940 | _ => (e, env, nv, p)
|
adamc@1200
|
1941
|
adamc@1207
|
1942 val (e, env, nv, p) = deAbs (e, [], 1, True)
|
adamc@1200
|
1943
|
adamc@1217
|
1944 val (_, st) = evalExp env (e, St.create {Var = nv,
|
adamc@1217
|
1945 Ambient = p})
|
adamc@1200
|
1946 in
|
adamc@1220
|
1947 (St.sent st @ vals, St.inserted st @ inserts, client, insert)
|
adamc@1200
|
1948 end
|
adamc@1202
|
1949
|
adamc@1220
|
1950 | DPolicy pol =>
|
adamc@1218
|
1951 let
|
adamc@1220
|
1952 fun rv rvN = (rvN + 1, Lvar rvN)
|
adamc@1218
|
1953 in
|
adamc@1220
|
1954 case pol of
|
adamc@1220
|
1955 PolClient e =>
|
adamc@1220
|
1956 let
|
adamc@1220
|
1957 val (_, p, _, _, outs) = queryProp [] 0 rv SomeCol e
|
adamc@1220
|
1958 in
|
adamc@1220
|
1959 (vals, inserts, (p, outs) :: client, insert)
|
adamc@1220
|
1960 end
|
adamc@1220
|
1961 | PolInsert e =>
|
adamc@1220
|
1962 let
|
adamc@1220
|
1963 val p = insertProp 0 rv e
|
adamc@1220
|
1964 in
|
adamc@1220
|
1965 (vals, inserts,client, p :: insert)
|
adamc@1220
|
1966 end
|
adamc@1218
|
1967 end
|
adamc@1214
|
1968
|
adamc@1220
|
1969 | _ => (vals, inserts, client, insert)
|
adamc@1202
|
1970
|
adamc@1203
|
1971 val () = reset ()
|
adamc@1202
|
1972
|
adamc@1220
|
1973 val (vals, inserts, client, insert) = foldl decl ([], [], [], []) file
|
adamc@1220
|
1974
|
adamc@1220
|
1975 val decompH = decomp true (fn (e1, e2) => e1 andalso e2 ())
|
adamc@1220
|
1976 val decompG = decomp false (fn (e1, e2) => e1 orelse e2 ())
|
adamc@1200
|
1977 in
|
adamc@1218
|
1978 app (fn ((loc, e, p), fl) =>
|
adamc@1207
|
1979 let
|
adamc@1213
|
1980 fun doOne e =
|
adamc@1213
|
1981 let
|
adamc@1213
|
1982 val p = And (p, Reln (Eq, [Var 0, e]))
|
adamc@1213
|
1983 in
|
adamc@1220
|
1984 if decompH p
|
adamc@1220
|
1985 (fn hyps =>
|
adamc@1220
|
1986 (fl <> Control Where
|
adamc@1220
|
1987 andalso imply (hyps, [AReln (Known, [Var 0])], SOME [Var 0]))
|
adamc@1220
|
1988 orelse List.exists (fn (p', outs) =>
|
adamc@1220
|
1989 decompG p'
|
adamc@1220
|
1990 (fn goals => imply (hyps, goals, SOME outs)))
|
adamc@1220
|
1991 client) then
|
adamc@1213
|
1992 ()
|
adamc@1213
|
1993 else
|
adamc@1213
|
1994 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
|
adamc@1216
|
1995 Print.preface ("The state satisifies this predicate:", p_prop p))
|
adamc@1213
|
1996 end
|
adamc@1213
|
1997
|
adamc@1213
|
1998 fun doAll e =
|
adamc@1213
|
1999 case e of
|
adamc@1213
|
2000 Const _ => ()
|
adamc@1213
|
2001 | Var _ => doOne e
|
adamc@1213
|
2002 | Lvar _ => raise Fail "Iflow.doAll: Lvar"
|
adamc@1216
|
2003 | Func (UnCon _, [_]) => doOne e
|
adamc@1215
|
2004 | Func (_, es) => app doAll es
|
adamc@1213
|
2005 | Recd xes => app (doAll o #2) xes
|
adamc@1213
|
2006 | Proj _ => doOne e
|
adamc@1213
|
2007 | Finish => ()
|
adamc@1207
|
2008 in
|
adamc@1213
|
2009 doAll e
|
adamc@1220
|
2010 end) vals;
|
adamc@1220
|
2011
|
adamc@1220
|
2012 app (fn (loc, p) =>
|
adamc@1220
|
2013 if decompH p
|
adamc@1220
|
2014 (fn hyps =>
|
adamc@1220
|
2015 List.exists (fn p' =>
|
adamc@1220
|
2016 decompG p'
|
adamc@1220
|
2017 (fn goals => imply (hyps, goals, NONE)))
|
adamc@1220
|
2018 insert) then
|
adamc@1220
|
2019 ()
|
adamc@1220
|
2020 else
|
adamc@1220
|
2021 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
|
adamc@1220
|
2022 Print.preface ("The state satisifies this predicate:", p_prop p))) inserts
|
adamc@1200
|
2023 end
|
adamc@1200
|
2024
|
adamc@1213
|
2025 val check = fn file =>
|
adamc@1213
|
2026 let
|
adamc@1213
|
2027 val oldInline = Settings.getMonoInline ()
|
adamc@1213
|
2028 in
|
adamc@1213
|
2029 (Settings.setMonoInline (case Int.maxInt of
|
adamc@1213
|
2030 NONE => 1000000
|
adamc@1213
|
2031 | SOME n => n);
|
adamc@1213
|
2032 check file;
|
adamc@1213
|
2033 Settings.setMonoInline oldInline)
|
adamc@1213
|
2034 handle ex => (Settings.setMonoInline oldInline;
|
adamc@1213
|
2035 raise ex)
|
adamc@1213
|
2036 end
|
adamc@1213
|
2037
|
adamc@1200
|
2038 end
|
adamc@1213
|
2039
|