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