annotate src/iflow.sml @ 1239:30f789d5e2ad

Parsing ORDER BY
author Adam Chlipala <adamc@hcoop.net>
date Thu, 15 Apr 2010 08:48:41 -0400
parents d6938ab3b5ae
children 58f5ac1bb849
rev   line source
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@1220 1460 fun expIn rv env rvOf =
adamc@1220 1461 let
adamc@1236 1462 fun expIn e =
adamc@1220 1463 let
adamc@1236 1464 fun default () = inl (rv ())
adamc@1220 1465 in
adamc@1220 1466 case e of
adamc@1236 1467 SqConst p => inl (Const p)
adamc@1236 1468 | Field (v, f) => inl (Proj (rvOf v, f))
adamc@1239 1469 | Computed _ => default ()
adamc@1220 1470 | Binop (bo, e1, e2) =>
adamc@1220 1471 let
adamc@1236 1472 val e1 = expIn e1
adamc@1236 1473 val e2 = expIn e2
adamc@1220 1474 in
adamc@1236 1475 inr (case (bo, e1, e2) of
adamc@1236 1476 (Exps f, inl e1, inl e2) => f (e1, e2)
adamc@1236 1477 | (Props f, inr p1, inr p2) => f (p1, p2)
adamc@1236 1478 | _ => Unknown)
adamc@1220 1479 end
adamc@1220 1480 | SqKnown e =>
adamc@1236 1481 (case expIn e of
adamc@1236 1482 inl e => inr (Reln (Known, [e]))
adamc@1236 1483 | _ => inr Unknown)
adamc@1220 1484 | Inj e =>
adamc@1220 1485 let
adamc@1220 1486 fun deinj e =
adamc@1220 1487 case #1 e of
adamc@1236 1488 ERel n => List.nth (env, n)
adamc@1236 1489 | EField (e, f) => Proj (deinj e, f)
adamc@1236 1490 | _ => rv ()
adamc@1220 1491 in
adamc@1236 1492 inl (deinj e)
adamc@1220 1493 end
adamc@1220 1494 | SqFunc (f, e) =>
adamc@1236 1495 (case expIn e of
adamc@1236 1496 inl e => inl (Func (Other f, [e]))
adamc@1220 1497 | _ => default ())
adamc@1220 1498
adamc@1220 1499 | Count => default ()
adamc@1220 1500 end
adamc@1220 1501 in
adamc@1220 1502 expIn
adamc@1220 1503 end
adamc@1216 1504
adamc@1236 1505 fun decomp {Save = save, Restore = restore, Add = add} =
adamc@1216 1506 let
adamc@1236 1507 fun go p k =
adamc@1236 1508 case p of
adamc@1238 1509 True => (k () handle Cc.Contradiction => ())
adamc@1236 1510 | False => ()
adamc@1236 1511 | Unknown => ()
adamc@1236 1512 | And (p1, p2) => go p1 (fn () => go p2 k)
adamc@1236 1513 | Or (p1, p2) =>
adamc@1236 1514 let
adamc@1236 1515 val saved = save ()
adamc@1236 1516 in
adamc@1236 1517 go p1 k;
adamc@1236 1518 restore saved;
adamc@1236 1519 go p2 k
adamc@1236 1520 end
adamc@1236 1521 | Reln x => (add (AReln x); k ())
adamc@1236 1522 | Cond x => (add (ACond x); k ())
adamc@1236 1523 in
adamc@1236 1524 go
adamc@1236 1525 end
adamc@1236 1526
adamc@1236 1527 datatype queryMode =
adamc@1238 1528 SomeCol of {New : (string * exp) option, Old : (string * exp) option, Outs : exp list} -> unit
adamc@1236 1529 | AllCols of exp -> unit
adamc@1236 1530
adamc@1236 1531 type 'a doQuery = {
adamc@1236 1532 Env : exp list,
adamc@1236 1533 NextVar : unit -> exp,
adamc@1236 1534 Add : atom -> unit,
adamc@1236 1535 Save : unit -> 'a,
adamc@1236 1536 Restore : 'a -> unit,
adamc@1236 1537 UsedExp : exp -> unit,
adamc@1236 1538 Cont : queryMode
adamc@1236 1539 }
adamc@1236 1540
adamc@1236 1541 fun doQuery (arg : 'a doQuery) e =
adamc@1236 1542 let
adamc@1236 1543 fun default () = print ("Warning: Information flow checker can't parse SQL query at "
adamc@1236 1544 ^ ErrorMsg.spanToString (#2 e) ^ "\n")
adamc@1216 1545 in
adamc@1216 1546 case parse query e of
adamc@1216 1547 NONE => default ()
adamc@1227 1548 | SOME q =>
adamc@1216 1549 let
adamc@1236 1550 fun doQuery q =
adamc@1227 1551 case q of
adamc@1227 1552 Query1 r =>
adamc@1227 1553 let
adamc@1238 1554 val new = ref NONE
adamc@1238 1555 val old = ref NONE
adamc@1238 1556
adamc@1238 1557 val rvs = map (fn (tab, v) =>
adamc@1238 1558 let
adamc@1238 1559 val nv = #NextVar arg ()
adamc@1238 1560 in
adamc@1238 1561 case v of
adamc@1238 1562 "New" => new := SOME (tab, nv)
adamc@1238 1563 | "Old" => old := SOME (tab, nv)
adamc@1238 1564 | _ => ();
adamc@1238 1565 (v, nv)
adamc@1238 1566 end) (#From r)
adamc@1214 1567
adamc@1227 1568 fun rvOf v =
adamc@1227 1569 case List.find (fn (v', _) => v' = v) rvs of
adamc@1227 1570 NONE => raise Fail "Iflow.queryProp: Bad table variable"
adamc@1227 1571 | SOME (_, e) => e
adamc@1214 1572
adamc@1236 1573 val expIn = expIn (#NextVar arg) (#Env arg) rvOf
adamc@1236 1574
adamc@1236 1575 val saved = #Save arg ()
adamc@1236 1576 fun addFrom () = app (fn (t, v) => #Add arg (AReln (Sql t, [rvOf v]))) (#From r)
adamc@1236 1577
adamc@1227 1578 fun usedFields e =
adamc@1227 1579 case e of
adamc@1227 1580 SqConst _ => []
adamc@1227 1581 | Field (v, f) => [(v, f)]
adamc@1239 1582 | Computed _ => []
adamc@1227 1583 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
adamc@1227 1584 | SqKnown _ => []
adamc@1227 1585 | Inj _ => []
adamc@1227 1586 | SqFunc (_, e) => usedFields e
adamc@1227 1587 | Count => []
adamc@1214 1588
adamc@1236 1589 fun doUsed () = case #Where r of
adamc@1236 1590 NONE => ()
adamc@1236 1591 | SOME e =>
adamc@1236 1592 #UsedExp arg (Recd (ListUtil.mapi
adamc@1236 1593 (fn (n, (v, f)) => (Int.toString n,
adamc@1236 1594 Proj (rvOf v, f)))
adamc@1236 1595 (usedFields e)))
adamc@1211 1596
adamc@1236 1597 fun normal' () =
adamc@1236 1598 case #Cont arg of
adamc@1236 1599 SomeCol k =>
adamc@1227 1600 let
adamc@1236 1601 val sis = map (fn si =>
adamc@1236 1602 case si of
adamc@1236 1603 SqField (v, f) => Proj (rvOf v, f)
adamc@1236 1604 | SqExp (e, f) =>
adamc@1236 1605 case expIn e of
adamc@1236 1606 inr _ => #NextVar arg ()
adamc@1236 1607 | inl e => e) (#Select r)
adamc@1227 1608 in
adamc@1238 1609 k {New = !new, Old = !old, Outs = sis}
adamc@1227 1610 end
adamc@1236 1611 | AllCols k =>
adamc@1227 1612 let
adamc@1236 1613 val (ts, es) =
adamc@1236 1614 foldl (fn (si, (ts, es)) =>
adamc@1227 1615 case si of
adamc@1227 1616 SqField (v, f) =>
adamc@1227 1617 let
adamc@1227 1618 val fs = getOpt (SM.find (ts, v), SM.empty)
adamc@1227 1619 in
adamc@1236 1620 (SM.insert (ts, v, SM.insert (fs, f, Proj (rvOf v, f))), es)
adamc@1227 1621 end
adamc@1227 1622 | SqExp (e, f) =>
adamc@1227 1623 let
adamc@1236 1624 val e =
adamc@1236 1625 case expIn e of
adamc@1236 1626 inr _ => #NextVar arg ()
adamc@1236 1627 | inl e => e
adamc@1227 1628 in
adamc@1236 1629 (ts, SM.insert (es, f, e))
adamc@1227 1630 end)
adamc@1236 1631 (SM.empty, SM.empty) (#Select r)
adamc@1227 1632 in
adamc@1236 1633 k (Recd (map (fn (t, fs) => (t, Recd (SM.listItemsi fs)))
adamc@1236 1634 (SM.listItemsi ts)
adamc@1236 1635 @ SM.listItemsi es))
adamc@1227 1636 end
adamc@1227 1637
adamc@1236 1638 fun doWhere final =
adamc@1236 1639 (addFrom ();
adamc@1236 1640 case #Where r of
adamc@1236 1641 NONE => (doUsed (); final ())
adamc@1236 1642 | SOME e =>
adamc@1236 1643 case expIn e of
adamc@1236 1644 inl _ => (doUsed (); final ())
adamc@1236 1645 | inr p =>
adamc@1236 1646 let
adamc@1236 1647 val saved = #Save arg ()
adamc@1236 1648 in
adamc@1236 1649 decomp {Save = #Save arg, Restore = #Restore arg, Add = #Add arg}
adamc@1236 1650 p (fn () => (doUsed (); final ()) handle Cc.Contradiction => ());
adamc@1236 1651 #Restore arg saved
adamc@1236 1652 end)
adamc@1236 1653 handle Cc.Contradiction => ()
adamc@1236 1654
adamc@1236 1655 fun normal () = doWhere normal'
adamc@1227 1656 in
adamc@1236 1657 (case #Select r of
adamc@1236 1658 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
adamc@1236 1659 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
adamc@1236 1660 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
adamc@1236 1661 (case #Cont arg of
adamc@1236 1662 SomeCol _ => ()
adamc@1236 1663 | AllCols k =>
adamc@1236 1664 let
adamc@1236 1665 fun answer e = k (Recd [(f, e)])
adamc@1236 1666
adamc@1236 1667 val saved = #Save arg ()
adamc@1238 1668 val () = (answer (Func (DtCon0 "Basis.bool.False", [])))
adamc@1238 1669 handle Cc.Contradiction => ()
adamc@1236 1670 in
adamc@1238 1671 #Restore arg saved;
adamc@1238 1672 (*print "True time!\n";*)
adamc@1236 1673 doWhere (fn () => answer (Func (DtCon0 "Basis.bool.True", [])));
adamc@1236 1674 #Restore arg saved
adamc@1236 1675 end)
adamc@1236 1676 | _ => normal ())
adamc@1236 1677 | _ => normal ())
adamc@1236 1678 before #Restore arg saved
adamc@1227 1679 end
adamc@1227 1680 | Union (q1, q2) =>
adamc@1220 1681 let
adamc@1236 1682 val saved = #Save arg ()
adamc@1220 1683 in
adamc@1236 1684 doQuery q1;
adamc@1236 1685 #Restore arg saved;
adamc@1236 1686 doQuery q2;
adamc@1236 1687 #Restore arg saved
adamc@1220 1688 end
adamc@1216 1689 in
adamc@1236 1690 doQuery q
adamc@1216 1691 end
adamc@1220 1692 end
adamc@1220 1693
adamc@1211 1694 fun evalPat env e (pt, _) =
adamc@1211 1695 case pt of
adamc@1236 1696 PWild => env
adamc@1236 1697 | PVar _ => e :: env
adamc@1236 1698 | PPrim _ => env
adamc@1236 1699 | PCon (_, pc, NONE) => (St.assert [AReln (PCon0 (patCon pc), [e])]; env)
adamc@1211 1700 | PCon (_, pc, SOME pt) =>
adamc@1211 1701 let
adamc@1236 1702 val env = evalPat env (Func (UnCon (patCon pc), [e])) pt
adamc@1211 1703 in
adamc@1236 1704 St.assert [AReln (PCon1 (patCon pc), [e])];
adamc@1236 1705 env
adamc@1211 1706 end
adamc@1211 1707 | PRecord xpts =>
adamc@1236 1708 foldl (fn ((x, pt, _), env) => evalPat env (Proj (e, x)) pt) env xpts
adamc@1236 1709 | PNone _ => (St.assert [AReln (PCon0 "None", [e])]; env)
adamc@1211 1710 | PSome (_, pt) =>
adamc@1211 1711 let
adamc@1236 1712 val env = evalPat env (Func (UnCon "Some", [e])) pt
adamc@1211 1713 in
adamc@1236 1714 St.assert [AReln (PCon1 "Some", [e])];
adamc@1236 1715 env
adamc@1211 1716 end
adamc@1211 1717
adamc@1236 1718 fun evalExp env (e as (_, loc)) k =
adamc@1236 1719 let
adamc@1238 1720 (*val () = St.debug ()*)
adamc@1236 1721 (*val () = Print.preface ("evalExp", MonoPrint.p_exp MonoEnv.empty e)*)
adamc@1211 1722
adamc@1236 1723 fun default () = k (Var (St.nextVar ()))
adamc@1234 1724
adamc@1234 1725 fun doFfi (m, s, es) =
adamc@1234 1726 if m = "Basis" andalso SS.member (writers, s) then
adamc@1234 1727 let
adamc@1236 1728 fun doArgs es =
adamc@1236 1729 case es of
adamc@1236 1730 [] => k (Recd [])
adamc@1236 1731 | e :: es =>
adamc@1238 1732 evalExp env e (fn e => (St.send true (e, loc); doArgs es))
adamc@1234 1733 in
adamc@1236 1734 doArgs es
adamc@1234 1735 end
adamc@1234 1736 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
adamc@1234 1737 default ()
adamc@1234 1738 else
adamc@1234 1739 let
adamc@1236 1740 fun doArgs (es, acc) =
adamc@1236 1741 case es of
adamc@1236 1742 [] => k (Func (Other (m ^ "." ^ s), rev acc))
adamc@1236 1743 | e :: es =>
adamc@1236 1744 evalExp env e (fn e => doArgs (es, e :: acc))
adamc@1234 1745 in
adamc@1236 1746 doArgs (es, [])
adamc@1234 1747 end
adamc@1200 1748 in
adamc@1200 1749 case #1 e of
adamc@1236 1750 EPrim p => k (Const p)
adamc@1236 1751 | ERel n => k (List.nth (env, n))
adamc@1200 1752 | ENamed _ => default ()
adamc@1236 1753 | ECon (_, pc, NONE) => k (Func (DtCon0 (patCon pc), []))
adamc@1236 1754 | ECon (_, pc, SOME e) => evalExp env e (fn e => k (Func (DtCon1 (patCon pc), [e])))
adamc@1236 1755 | ENone _ => k (Func (DtCon0 "None", []))
adamc@1236 1756 | ESome (_, e) => evalExp env e (fn e => k (Func (DtCon1 "Some", [e])))
adamc@1200 1757 | EFfi _ => default ()
adamc@1213 1758
adamc@1234 1759 | EFfiApp x => doFfi x
adamc@1234 1760 | EApp ((EFfi (m, s), _), e) => doFfi (m, s, [e])
adamc@1213 1761
adamc@1236 1762 | EApp (e1, e2) => evalExp env e1 (fn _ => evalExp env e2 (fn _ => default ()))
adamc@1213 1763
adamc@1200 1764 | EAbs _ => default ()
adamc@1236 1765 | EUnop (s, e1) => evalExp env e1 (fn e1 => k (Func (Other s, [e1])))
adamc@1236 1766 | EBinop (s, e1, e2) => evalExp env e1 (fn e1 => evalExp env e2 (fn e2 => k (Func (Other s, [e1, e2]))))
adamc@1200 1767 | ERecord xets =>
adamc@1200 1768 let
adamc@1236 1769 fun doFields (xes, acc) =
adamc@1236 1770 case xes of
adamc@1236 1771 [] => k (Recd (rev acc))
adamc@1236 1772 | (x, e, _) :: xes =>
adamc@1236 1773 evalExp env e (fn e => doFields (xes, (x, e) :: acc))
adamc@1200 1774 in
adamc@1236 1775 doFields (xets, [])
adamc@1200 1776 end
adamc@1236 1777 | EField (e, s) => evalExp env e (fn e => k (Proj (e, s)))
adamc@1218 1778 | ECase (e, pes, {result = res, ...}) =>
adamc@1236 1779 evalExp env e (fn e =>
adamc@1218 1780 let
adamc@1236 1781 val () = St.addPath (e, loc)
adamc@1218 1782 in
adamc@1236 1783 app (fn (p, pe) =>
adamc@1236 1784 let
adamc@1236 1785 val saved = St.stash ()
adamc@1236 1786 in
adamc@1238 1787 let
adamc@1238 1788 val env = evalPat env e p
adamc@1238 1789 in
adamc@1238 1790 evalExp env pe k;
adamc@1238 1791 St.reinstate saved
adamc@1238 1792 end
adamc@1238 1793 handle Cc.Contradiction => St.reinstate saved
adamc@1236 1794 end) pes
adamc@1238 1795 end)
adamc@1200 1796 | EStrcat (e1, e2) =>
adamc@1236 1797 evalExp env e1 (fn e1 =>
adamc@1236 1798 evalExp env e2 (fn e2 =>
adamc@1236 1799 k (Func (Other "cat", [e1, e2]))))
adamc@1238 1800 | EError (e, _) => evalExp env e (fn e => St.send true (e, loc))
adamc@1200 1801 | EReturnBlob {blob = b, mimeType = m, ...} =>
adamc@1236 1802 evalExp env b (fn b =>
adamc@1238 1803 (St.send true (b, loc);
adamc@1236 1804 evalExp env m
adamc@1238 1805 (fn m => St.send true (m, loc))))
adamc@1200 1806 | ERedirect (e, _) =>
adamc@1238 1807 evalExp env e (fn e => St.send true (e, loc))
adamc@1200 1808 | EWrite e =>
adamc@1238 1809 evalExp env e (fn e => (St.send true (e, loc);
adamc@1236 1810 k (Recd [])))
adamc@1200 1811 | ESeq (e1, e2) =>
adamc@1236 1812 evalExp env e1 (fn _ => evalExp env e2 k)
adamc@1200 1813 | ELet (_, _, e1, e2) =>
adamc@1236 1814 evalExp env e1 (fn e1 => evalExp (e1 :: env) e2 k)
adamc@1200 1815 | EClosure (n, es) =>
adamc@1200 1816 let
adamc@1236 1817 fun doArgs (es, acc) =
adamc@1236 1818 case es of
adamc@1236 1819 [] => k (Func (Other ("Cl" ^ Int.toString n), rev acc))
adamc@1236 1820 | e :: es =>
adamc@1236 1821 evalExp env e (fn e => doArgs (es, e :: acc))
adamc@1200 1822 in
adamc@1236 1823 doArgs (es, [])
adamc@1200 1824 end
adamc@1200 1825
adamc@1235 1826 | EQuery {query = q, body = b, initial = i, state = state, ...} =>
adamc@1238 1827 evalExp env i (fn i =>
adamc@1238 1828 let
adamc@1238 1829 val saved = St.stash ()
adamc@1200 1830
adamc@1238 1831 val () = (k i)
adamc@1238 1832 handle Cc.Contradiction => ()
adamc@1238 1833 val () = St.reinstate saved
adamc@1238 1834
adamc@1238 1835 val r = Var (St.nextVar ())
adamc@1238 1836 val acc = Var (St.nextVar ())
adamc@1238 1837 in
adamc@1238 1838 if MonoUtil.Exp.existsB {typ = fn _ => false,
adamc@1238 1839 exp = fn (n, e) =>
adamc@1238 1840 case e of
adamc@1238 1841 ERel n' => n' = n
adamc@1238 1842 | _ => false,
adamc@1238 1843 bind = fn (n, b) =>
adamc@1238 1844 case b of
adamc@1238 1845 MonoUtil.Exp.RelE _ => n + 1
adamc@1238 1846 | _ => n}
adamc@1238 1847 0 b then
adamc@1238 1848 doQuery {Env = env,
adamc@1238 1849 NextVar = Var o St.nextVar,
adamc@1238 1850 Add = fn a => St.assert [a],
adamc@1238 1851 Save = St.stash,
adamc@1238 1852 Restore = St.reinstate,
adamc@1238 1853 UsedExp = fn e => St.send false (e, loc),
adamc@1238 1854 Cont = AllCols (fn _ => evalExp
adamc@1238 1855 (acc :: r :: env)
adamc@1238 1856 b (fn _ => default ()))} q
adamc@1238 1857 else
adamc@1238 1858 doQuery {Env = env,
adamc@1238 1859 NextVar = Var o St.nextVar,
adamc@1238 1860 Add = fn a => St.assert [a],
adamc@1238 1861 Save = St.stash,
adamc@1238 1862 Restore = St.reinstate,
adamc@1238 1863 UsedExp = fn e => St.send false (e, loc),
adamc@1238 1864 Cont = AllCols (fn x =>
adamc@1238 1865 (St.assert [AReln (Eq, [r, x])];
adamc@1238 1866 evalExp (acc :: r :: env) b k))} q
adamc@1238 1867 end)
adamc@1220 1868 | EDml e =>
adamc@1220 1869 (case parse dml e of
adamc@1220 1870 NONE => (print ("Warning: Information flow checker can't parse DML command at "
adamc@1220 1871 ^ ErrorMsg.spanToString loc ^ "\n");
adamc@1220 1872 default ())
adamc@1220 1873 | SOME d =>
adamc@1220 1874 case d of
adamc@1220 1875 Insert (tab, es) =>
adamc@1220 1876 let
adamc@1236 1877 val new = St.nextVar ()
adamc@1220 1878
adamc@1236 1879 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1880 (fn _ => raise Fail "Iflow.evalExp: Bad field expression in INSERT [1]")
adamc@1220 1881
adamc@1236 1882 val es = map (fn (x, e) =>
adamc@1236 1883 case expIn e of
adamc@1236 1884 inl e => (x, e)
adamc@1236 1885 | inr _ => raise Fail "Iflow.evalExp: Bad field expression in INSERT [2]")
adamc@1236 1886 es
adamc@1220 1887
adamc@1236 1888 val saved = St.stash ()
adamc@1220 1889 in
adamc@1236 1890 St.assert [AReln (Sql (tab ^ "$New"), [Recd es])];
adamc@1236 1891 St.insert loc;
adamc@1236 1892 St.reinstate saved;
adamc@1236 1893 k (Recd [])
adamc@1221 1894 end
adamc@1221 1895 | Delete (tab, e) =>
adamc@1221 1896 let
adamc@1236 1897 val old = St.nextVar ()
adamc@1236 1898
adamc@1236 1899 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1900 (fn "T" => Var old
adamc@1236 1901 | _ => raise Fail "Iflow.evalExp: Bad field expression in DELETE")
adamc@1221 1902
adamc@1236 1903 val p = case expIn e of
adamc@1236 1904 inl e => raise Fail "Iflow.evalExp: DELETE with non-boolean"
adamc@1236 1905 | inr p => p
adamc@1236 1906
adamc@1236 1907 val saved = St.stash ()
adamc@1221 1908 in
adamc@1238 1909 St.assert [AReln (Sql (tab ^ "$Old"), [Var old])];
adamc@1236 1910 decomp {Save = St.stash,
adamc@1236 1911 Restore = St.reinstate,
adamc@1236 1912 Add = fn a => St.assert [a]} p
adamc@1236 1913 (fn () => (St.delete loc;
adamc@1236 1914 St.reinstate saved;
adamc@1236 1915 St.havocReln (Sql tab);
adamc@1236 1916 k (Recd []))
adamc@1236 1917 handle Cc.Contradiction => ())
adamc@1223 1918 end
adamc@1223 1919 | Update (tab, fs, e) =>
adamc@1223 1920 let
adamc@1236 1921 val new = St.nextVar ()
adamc@1236 1922 val old = St.nextVar ()
adamc@1223 1923
adamc@1236 1924 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1925 (fn "T" => Var old
adamc@1236 1926 | _ => raise Fail "Iflow.evalExp: Bad field expression in UPDATE")
adamc@1223 1927
adamc@1236 1928 val fs = map
adamc@1236 1929 (fn (x, e) =>
adamc@1236 1930 (x, case expIn e of
adamc@1236 1931 inl e => e
adamc@1236 1932 | inr _ => raise Fail
adamc@1236 1933 ("Iflow.evalExp: Selecting "
adamc@1236 1934 ^ "boolean expression")))
adamc@1236 1935 fs
adamc@1223 1936
adamc@1226 1937 val fs' = case SM.find (!tabs, tab) of
adamc@1224 1938 NONE => raise Fail "Iflow.evalExp: Updating unknown table"
adamc@1226 1939 | SOME (fs', _) => fs'
adamc@1224 1940
adamc@1224 1941 val fs = foldl (fn (f, fs) =>
adamc@1224 1942 if List.exists (fn (f', _) => f' = f) fs then
adamc@1224 1943 fs
adamc@1224 1944 else
adamc@1224 1945 (f, Proj (Var old, f)) :: fs) fs fs'
adamc@1224 1946
adamc@1236 1947 val p = case expIn e of
adamc@1236 1948 inl e => raise Fail "Iflow.evalExp: UPDATE with non-boolean"
adamc@1236 1949 | inr p => p
adamc@1236 1950 val saved = St.stash ()
adamc@1223 1951 in
adamc@1236 1952 St.assert [AReln (Sql (tab ^ "$New"), [Recd fs]),
adamc@1238 1953 AReln (Sql (tab ^ "$Old"), [Var old])];
adamc@1236 1954 decomp {Save = St.stash,
adamc@1236 1955 Restore = St.reinstate,
adamc@1236 1956 Add = fn a => St.assert [a]} p
adamc@1236 1957 (fn () => (St.update loc;
adamc@1236 1958 St.reinstate saved;
adamc@1236 1959 St.havocReln (Sql tab);
adamc@1236 1960 k (Recd []))
adamc@1236 1961 handle Cc.Contradiction => ())
adamc@1220 1962 end)
adamc@1220 1963
adamc@1229 1964 | ENextval (EPrim (Prim.String seq), _) =>
adamc@1229 1965 let
adamc@1236 1966 val nv = St.nextVar ()
adamc@1229 1967 in
adamc@1236 1968 St.assert [AReln (Sql (String.extract (seq, 3, NONE)), [Var nv])];
adamc@1236 1969 k (Var nv)
adamc@1229 1970 end
adamc@1200 1971 | ENextval _ => default ()
adamc@1200 1972 | ESetval _ => default ()
adamc@1200 1973
adamc@1238 1974 | EUnurlify ((EFfiApp ("Basis", "get_cookie", [(EPrim (Prim.String cname), _)]), _), _, _) =>
adamc@1217 1975 let
adamc@1238 1976 val e = Var (St.nextVar ())
adamc@1217 1977 in
adamc@1238 1978 St.assert [AReln (Known, [e])];
adamc@1238 1979 k e
adamc@1217 1980 end
adamc@1213 1981
adamc@1200 1982 | EUnurlify _ => default ()
adamc@1200 1983 | EJavaScript _ => default ()
adamc@1200 1984 | ESignalReturn _ => default ()
adamc@1200 1985 | ESignalBind _ => default ()
adamc@1200 1986 | ESignalSource _ => default ()
adamc@1200 1987 | EServerCall _ => default ()
adamc@1200 1988 | ERecv _ => default ()
adamc@1200 1989 | ESleep _ => default ()
adamc@1200 1990 | ESpawn _ => default ()
adamc@1200 1991 end
adamc@1200 1992
adamc@1200 1993 fun check file =
adamc@1200 1994 let
adamc@1236 1995 val () = St.reset ()
adamc@1236 1996
adamc@1213 1997 val file = MonoReduce.reduce file
adamc@1213 1998 val file = MonoOpt.optimize file
adamc@1213 1999 val file = Fuse.fuse file
adamc@1213 2000 val file = MonoOpt.optimize file
adamc@1216 2001 val file = MonoShake.shake file
adamc@1213 2002 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
adamc@1213 2003
adamc@1207 2004 val exptd = foldl (fn ((d, _), exptd) =>
adamc@1207 2005 case d of
adamc@1207 2006 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
adamc@1207 2007 | _ => exptd) IS.empty file
adamc@1207 2008
adamc@1236 2009 fun decl (d, _) =
adamc@1200 2010 case d of
adamc@1226 2011 DTable (tab, fs, pk, _) =>
adamc@1226 2012 let
adamc@1226 2013 val ks =
adamc@1226 2014 case #1 pk of
adamc@1226 2015 EPrim (Prim.String s) =>
adamc@1226 2016 (case String.tokens (fn ch => ch = #"," orelse ch = #" ") s of
adamc@1226 2017 [] => []
adamc@1226 2018 | pk => [pk])
adamc@1226 2019 | _ => []
adamc@1226 2020 in
adamc@1226 2021 if size tab >= 3 then
adamc@1236 2022 tabs := SM.insert (!tabs, String.extract (tab, 3, NONE),
adamc@1236 2023 (map #1 fs,
adamc@1236 2024 map (map (fn s => str (Char.toUpper (String.sub (s, 3)))
adamc@1236 2025 ^ String.extract (s, 4, NONE))) ks))
adamc@1226 2026 else
adamc@1226 2027 raise Fail "Table name does not begin with uw_"
adamc@1226 2028 end
adamc@1238 2029 | DVal (x, n, _, e, _) =>
adamc@1200 2030 let
adamc@1238 2031 (*val () = print ("\n=== " ^ x ^ " ===\n\n");*)
adamc@1238 2032
adamc@1207 2033 val isExptd = IS.member (exptd, n)
adamc@1207 2034
adamc@1236 2035 val saved = St.stash ()
adamc@1236 2036
adamc@1236 2037 fun deAbs (e, env, ps) =
adamc@1200 2038 case #1 e of
adamc@1236 2039 EAbs (_, _, _, e) =>
adamc@1236 2040 let
adamc@1236 2041 val nv = Var (St.nextVar ())
adamc@1236 2042 in
adamc@1236 2043 deAbs (e, nv :: env,
adamc@1236 2044 if isExptd then
adamc@1236 2045 AReln (Known, [nv]) :: ps
adamc@1236 2046 else
adamc@1236 2047 ps)
adamc@1236 2048 end
adamc@1236 2049 | _ => (e, env, ps)
adamc@1200 2050
adamc@1236 2051 val (e, env, ps) = deAbs (e, [], [])
adamc@1200 2052 in
adamc@1236 2053 St.assert ps;
adamc@1236 2054 (evalExp env e (fn _ => ()) handle Cc.Contradiction => ());
adamc@1236 2055 St.reinstate saved
adamc@1200 2056 end
adamc@1202 2057
adamc@1220 2058 | DPolicy pol =>
adamc@1218 2059 let
adamc@1236 2060 val rvN = ref 0
adamc@1236 2061 fun rv () =
adamc@1236 2062 let
adamc@1236 2063 val n = !rvN
adamc@1236 2064 in
adamc@1236 2065 rvN := n + 1;
adamc@1236 2066 Lvar n
adamc@1236 2067 end
adamc@1236 2068
adamc@1236 2069 val atoms = ref ([] : atom list)
adamc@1236 2070 fun doQ k = doQuery {Env = [],
adamc@1236 2071 NextVar = rv,
adamc@1236 2072 Add = fn a => atoms := a :: !atoms,
adamc@1236 2073 Save = fn () => !atoms,
adamc@1236 2074 Restore = fn ls => atoms := ls,
adamc@1236 2075 UsedExp = fn _ => (),
adamc@1238 2076 Cont = SomeCol (fn r => k (rev (!atoms), r))}
adamc@1238 2077
adamc@1238 2078 fun untab tab = List.filter (fn AReln (Sql tab', _) => tab' <> tab
adamc@1238 2079 | _ => true)
adamc@1218 2080 in
adamc@1220 2081 case pol of
adamc@1220 2082 PolClient e =>
adamc@1238 2083 doQ (fn (ats, {Outs = es, ...}) => St.allowSend (ats, es)) e
adamc@1220 2084 | PolInsert e =>
adamc@1238 2085 doQ (fn (ats, {New = SOME (tab, new), ...}) =>
adamc@1238 2086 St.allowInsert (AReln (Sql (tab ^ "$New"), [new]) :: untab tab ats)
adamc@1238 2087 | _ => raise Fail "Iflow: No New in mayInsert policy") e
adamc@1221 2088 | PolDelete e =>
adamc@1238 2089 doQ (fn (ats, {Old = SOME (tab, old), ...}) =>
adamc@1238 2090 St.allowDelete (AReln (Sql (tab ^ "$Old"), [old]) :: untab tab ats)
adamc@1238 2091 | _ => raise Fail "Iflow: No Old in mayDelete policy") e
adamc@1223 2092 | PolUpdate e =>
adamc@1238 2093 doQ (fn (ats, {New = SOME (tab, new), Old = SOME (_, old), ...}) =>
adamc@1238 2094 St.allowUpdate (AReln (Sql (tab ^ "$Old"), [old])
adamc@1238 2095 :: AReln (Sql (tab ^ "$New"), [new])
adamc@1238 2096 :: untab tab ats)
adamc@1238 2097 | _ => raise Fail "Iflow: No New or Old in mayUpdate policy") e
adamc@1229 2098 | PolSequence e =>
adamc@1229 2099 (case #1 e of
adamc@1229 2100 EPrim (Prim.String seq) =>
adamc@1229 2101 let
adamc@1236 2102 val p = AReln (Sql (String.extract (seq, 3, NONE)), [Lvar 0])
adamc@1229 2103 val outs = [Lvar 0]
adamc@1229 2104 in
adamc@1236 2105 St.allowSend ([p], outs)
adamc@1229 2106 end
adamc@1236 2107 | _ => ())
adamc@1218 2108 end
adamc@1214 2109
adamc@1236 2110 | _ => ()
adamc@1200 2111 in
adamc@1236 2112 app decl file
adamc@1200 2113 end
adamc@1200 2114
adamc@1213 2115 val check = fn file =>
adamc@1213 2116 let
adamc@1213 2117 val oldInline = Settings.getMonoInline ()
adamc@1213 2118 in
adamc@1213 2119 (Settings.setMonoInline (case Int.maxInt of
adamc@1213 2120 NONE => 1000000
adamc@1213 2121 | SOME n => n);
adamc@1213 2122 check file;
adamc@1213 2123 Settings.setMonoInline oldInline)
adamc@1213 2124 handle ex => (Settings.setMonoInline oldInline;
adamc@1213 2125 raise ex)
adamc@1213 2126 end
adamc@1213 2127
adamc@1200 2128 end