annotate src/iflow.sml @ 1236:d5ecceb7d1a1

Completely redid main Iflow logic; so far, policy and policy2 work
author Adam Chlipala <adamc@hcoop.net>
date Tue, 13 Apr 2010 16:30:46 -0400
parents a7b773f1d053
children a9c200f73f24
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@1218 246 val builtFrom : database * {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@1218 713 fun builtFrom (db, {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@1218 721 List.exists (fn b => repOf b = d) bs
adamc@1218 722 orelse case #Variety (unNode d) of
adamc@1218 723 Dt0 _ => true
adamc@1218 724 | Dt1 (_, d) => loop d
adamc@1218 725 | Prim _ => true
adamc@1221 726 | Recrd (xes, _) => List.all loop (SM.listItems (!xes))
adamc@1218 727 | Nothing => false
adamc@1218 728 end
adamc@1218 729 in
adamc@1218 730 loop (representative (db, d))
adamc@1218 731 end
adamc@1218 732
adamc@1208 733 end
adamc@1208 734
adamc@1226 735 val tabs = ref (SM.empty : (string list * string list list) SM.map)
adamc@1226 736
adamc@1231 737 fun ccOf hyps =
adamc@1231 738 let
adamc@1231 739 val cc = Cc.database ()
adamc@1231 740 val () = app (fn a => Cc.assert (cc, a)) hyps
adamc@1231 741
adamc@1231 742 (* Take advantage of table key information *)
adamc@1231 743 fun findKeys hyps =
adamc@1231 744 case hyps of
adamc@1231 745 [] => ()
adamc@1231 746 | AReln (Sql tab, [r1]) :: hyps =>
adamc@1231 747 (case SM.find (!tabs, tab) of
adamc@1231 748 NONE => findKeys hyps
adamc@1231 749 | SOME (_, []) => findKeys hyps
adamc@1231 750 | SOME (_, ks) =>
adamc@1231 751 let
adamc@1231 752 fun finder hyps =
adamc@1231 753 case hyps of
adamc@1231 754 [] => ()
adamc@1231 755 | AReln (Sql tab', [r2]) :: hyps =>
adamc@1231 756 (if tab' = tab andalso
adamc@1231 757 List.exists (List.all (fn f =>
adamc@1231 758 let
adamc@1231 759 val r =
adamc@1231 760 Cc.check (cc,
adamc@1231 761 AReln (Eq, [Proj (r1, f),
adamc@1231 762 Proj (r2, f)]))
adamc@1231 763 in
adamc@1231 764 (*Print.prefaces "Fs"
adamc@1231 765 [("tab",
adamc@1231 766 Print.PD.string tab),
adamc@1231 767 ("r1",
adamc@1231 768 p_exp (Proj (r1, f))),
adamc@1231 769 ("r2",
adamc@1231 770 p_exp (Proj (r2, f))),
adamc@1231 771 ("r",
adamc@1231 772 Print.PD.string
adamc@1231 773 (Bool.toString r))];*)
adamc@1231 774 r
adamc@1231 775 end)) ks then
adamc@1231 776 ((*Print.prefaces "Key match" [("tab", Print.PD.string tab),
adamc@1231 777 ("r1", p_exp r1),
adamc@1231 778 ("r2", p_exp r2),
adamc@1231 779 ("rp1", Cc.p_repOf cc r1),
adamc@1231 780 ("rp2", Cc.p_repOf cc r2)];*)
adamc@1231 781 Cc.assert (cc, AReln (Eq, [r1, r2])))
adamc@1231 782 else
adamc@1231 783 ();
adamc@1231 784 finder hyps)
adamc@1231 785 | _ :: hyps => finder hyps
adamc@1231 786 in
adamc@1231 787 finder hyps;
adamc@1231 788 findKeys hyps
adamc@1231 789 end)
adamc@1231 790 | _ :: hyps => findKeys hyps
adamc@1231 791 in
adamc@1231 792 findKeys hyps;
adamc@1231 793 cc
adamc@1231 794 end
adamc@1231 795
adamc@1200 796 fun patCon pc =
adamc@1200 797 case pc of
adamc@1200 798 PConVar n => "C" ^ Int.toString n
adamc@1200 799 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
adamc@1200 800
adamc@1200 801 datatype chunk =
adamc@1200 802 String of string
adamc@1200 803 | Exp of Mono.exp
adamc@1200 804
adamc@1200 805 fun chunkify e =
adamc@1200 806 case #1 e of
adamc@1200 807 EPrim (Prim.String s) => [String s]
adamc@1207 808 | EStrcat (e1, e2) =>
adamc@1207 809 let
adamc@1207 810 val chs1 = chunkify e1
adamc@1207 811 val chs2 = chunkify e2
adamc@1207 812 in
adamc@1207 813 case chs2 of
adamc@1207 814 String s2 :: chs2' =>
adamc@1207 815 (case List.last chs1 of
adamc@1207 816 String s1 => List.take (chs1, length chs1 - 1) @ String (s1 ^ s2) :: chs2'
adamc@1207 817 | _ => chs1 @ chs2)
adamc@1207 818 | _ => chs1 @ chs2
adamc@1207 819 end
adamc@1200 820 | _ => [Exp e]
adamc@1200 821
adamc@1201 822 type 'a parser = chunk list -> ('a * chunk list) option
adamc@1201 823
adamc@1201 824 fun always v chs = SOME (v, chs)
adamc@1201 825
adamc@1202 826 fun parse p s =
adamc@1202 827 case p (chunkify s) of
adamc@1201 828 SOME (v, []) => SOME v
adamc@1201 829 | _ => NONE
adamc@1201 830
adamc@1201 831 fun const s chs =
adamc@1201 832 case chs of
adamc@1201 833 String s' :: chs => if String.isPrefix s s' then
adamc@1201 834 SOME ((), if size s = size s' then
adamc@1201 835 chs
adamc@1201 836 else
adamc@1201 837 String (String.extract (s', size s, NONE)) :: chs)
adamc@1201 838 else
adamc@1201 839 NONE
adamc@1201 840 | _ => NONE
adamc@1201 841
adamc@1201 842 fun follow p1 p2 chs =
adamc@1201 843 case p1 chs of
adamc@1201 844 NONE => NONE
adamc@1201 845 | SOME (v1, chs) =>
adamc@1201 846 case p2 chs of
adamc@1201 847 NONE => NONE
adamc@1201 848 | SOME (v2, chs) => SOME ((v1, v2), chs)
adamc@1201 849
adamc@1201 850 fun wrap p f chs =
adamc@1201 851 case p chs of
adamc@1201 852 NONE => NONE
adamc@1201 853 | SOME (v, chs) => SOME (f v, chs)
adamc@1201 854
adamc@1209 855 fun wrapP p f chs =
adamc@1209 856 case p chs of
adamc@1209 857 NONE => NONE
adamc@1209 858 | SOME (v, chs) =>
adamc@1209 859 case f v of
adamc@1209 860 NONE => NONE
adamc@1209 861 | SOME r => SOME (r, chs)
adamc@1209 862
adamc@1201 863 fun alt p1 p2 chs =
adamc@1201 864 case p1 chs of
adamc@1201 865 NONE => p2 chs
adamc@1201 866 | v => v
adamc@1201 867
adamc@1207 868 fun altL ps =
adamc@1207 869 case rev ps of
adamc@1207 870 [] => (fn _ => NONE)
adamc@1207 871 | p :: ps =>
adamc@1207 872 foldl (fn (p1, p2) => alt p1 p2) p ps
adamc@1207 873
adamc@1204 874 fun opt p chs =
adamc@1204 875 case p chs of
adamc@1204 876 NONE => SOME (NONE, chs)
adamc@1204 877 | SOME (v, chs) => SOME (SOME v, chs)
adamc@1204 878
adamc@1201 879 fun skip cp chs =
adamc@1201 880 case chs of
adamc@1201 881 String "" :: chs => skip cp chs
adamc@1201 882 | String s :: chs' => if cp (String.sub (s, 0)) then
adamc@1201 883 skip cp (String (String.extract (s, 1, NONE)) :: chs')
adamc@1201 884 else
adamc@1201 885 SOME ((), chs)
adamc@1201 886 | _ => SOME ((), chs)
adamc@1201 887
adamc@1201 888 fun keep cp chs =
adamc@1201 889 case chs of
adamc@1201 890 String "" :: chs => keep cp chs
adamc@1201 891 | String s :: chs' =>
adamc@1201 892 let
adamc@1201 893 val (befor, after) = Substring.splitl cp (Substring.full s)
adamc@1201 894 in
adamc@1201 895 if Substring.isEmpty befor then
adamc@1201 896 NONE
adamc@1201 897 else
adamc@1201 898 SOME (Substring.string befor,
adamc@1201 899 if Substring.isEmpty after then
adamc@1201 900 chs'
adamc@1201 901 else
adamc@1201 902 String (Substring.string after) :: chs')
adamc@1201 903 end
adamc@1201 904 | _ => NONE
adamc@1201 905
adamc@1204 906 fun ws p = wrap (follow (skip (fn ch => ch = #" "))
adamc@1204 907 (follow p (skip (fn ch => ch = #" ")))) (#1 o #2)
adamc@1204 908
adamc@1204 909 fun log name p chs =
adamc@1206 910 (if !debug then
adamc@1227 911 (print (name ^ ": ");
adamc@1227 912 app (fn String s => print s
adamc@1227 913 | _ => print "???") chs;
adamc@1227 914 print "\n")
adamc@1206 915 else
adamc@1206 916 ();
adamc@1204 917 p chs)
adamc@1201 918
adamc@1201 919 fun list p chs =
adamc@1207 920 altL [wrap (follow p (follow (ws (const ",")) (list p)))
adamc@1207 921 (fn (v, ((), ls)) => v :: ls),
adamc@1207 922 wrap (ws p) (fn v => [v]),
adamc@1207 923 always []] chs
adamc@1201 924
adamc@1201 925 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
adamc@1201 926
adamc@1211 927 val t_ident = wrapP ident (fn s => if String.isPrefix "T_" s then
adamc@1211 928 SOME (String.extract (s, 2, NONE))
adamc@1201 929 else
adamc@1211 930 NONE)
adamc@1211 931 val uw_ident = wrapP ident (fn s => if String.isPrefix "uw_" s andalso size s >= 4 then
adamc@1211 932 SOME (str (Char.toUpper (String.sub (s, 3)))
adamc@1211 933 ^ String.extract (s, 4, NONE))
adamc@1211 934 else
adamc@1211 935 NONE)
adamc@1201 936
adamc@1211 937 val field = wrap (follow t_ident
adamc@1201 938 (follow (const ".")
adamc@1201 939 uw_ident))
adamc@1201 940 (fn (t, ((), f)) => (t, f))
adamc@1201 941
adamc@1206 942 datatype Rel =
adamc@1206 943 Exps of exp * exp -> prop
adamc@1206 944 | Props of prop * prop -> prop
adamc@1206 945
adamc@1204 946 datatype sqexp =
adamc@1206 947 SqConst of Prim.t
adamc@1206 948 | Field of string * string
adamc@1206 949 | Binop of Rel * sqexp * sqexp
adamc@1207 950 | SqKnown of sqexp
adamc@1207 951 | Inj of Mono.exp
adamc@1211 952 | SqFunc of string * sqexp
adamc@1211 953 | Count
adamc@1204 954
adamc@1210 955 fun cmp s r = wrap (const s) (fn () => Exps (fn (e1, e2) => Reln (r, [e1, e2])))
adamc@1210 956
adamc@1210 957 val sqbrel = altL [cmp "=" Eq,
adamc@1210 958 cmp "<>" Ne,
adamc@1210 959 cmp "<=" Le,
adamc@1210 960 cmp "<" Lt,
adamc@1210 961 cmp ">=" Ge,
adamc@1210 962 cmp ">" Gt,
adamc@1207 963 wrap (const "AND") (fn () => Props And),
adamc@1207 964 wrap (const "OR") (fn () => Props Or)]
adamc@1204 965
adamc@1204 966 datatype ('a, 'b) sum = inl of 'a | inr of 'b
adamc@1204 967
adamc@1209 968 fun string chs =
adamc@1206 969 case chs of
adamc@1209 970 String s :: chs =>
adamc@1209 971 if size s >= 2 andalso String.sub (s, 0) = #"'" then
adamc@1209 972 let
adamc@1209 973 fun loop (cs, acc) =
adamc@1209 974 case cs of
adamc@1209 975 [] => NONE
adamc@1209 976 | c :: cs =>
adamc@1209 977 if c = #"'" then
adamc@1209 978 SOME (String.implode (rev acc), cs)
adamc@1209 979 else if c = #"\\" then
adamc@1209 980 case cs of
adamc@1209 981 c :: cs => loop (cs, c :: acc)
adamc@1209 982 | _ => raise Fail "Iflow.string: Unmatched backslash escape"
adamc@1209 983 else
adamc@1209 984 loop (cs, c :: acc)
adamc@1209 985 in
adamc@1209 986 case loop (String.explode (String.extract (s, 1, NONE)), []) of
adamc@1209 987 NONE => NONE
adamc@1209 988 | SOME (s, []) => SOME (s, chs)
adamc@1209 989 | SOME (s, cs) => SOME (s, String (String.implode cs) :: chs)
adamc@1209 990 end
adamc@1209 991 else
adamc@1209 992 NONE
adamc@1209 993 | _ => NONE
adamc@1206 994
adamc@1209 995 val prim =
adamc@1209 996 altL [wrap (follow (wrapP (follow (keep Char.isDigit) (follow (const ".") (keep Char.isDigit)))
adamc@1209 997 (fn (x, ((), y)) => Option.map Prim.Float (Real64.fromString (x ^ "." ^ y))))
adamc@1209 998 (opt (const "::float8"))) #1,
adamc@1209 999 wrap (follow (wrapP (keep Char.isDigit)
adamc@1209 1000 (Option.map Prim.Int o Int64.fromString))
adamc@1209 1001 (opt (const "::int8"))) #1,
adamc@1209 1002 wrap (follow (opt (const "E")) (follow string (opt (const "::text"))))
adamc@1209 1003 (Prim.String o #1 o #2)]
adamc@1206 1004
adamc@1207 1005 fun known' chs =
adamc@1207 1006 case chs of
adamc@1207 1007 Exp (EFfi ("Basis", "sql_known"), _) :: chs => SOME ((), chs)
adamc@1207 1008 | _ => NONE
adamc@1207 1009
adamc@1207 1010 fun sqlify chs =
adamc@1207 1011 case chs of
adamc@1207 1012 Exp (EFfiApp ("Basis", f, [e]), _) :: chs =>
adamc@1207 1013 if String.isPrefix "sqlify" f then
adamc@1207 1014 SOME (e, chs)
adamc@1207 1015 else
adamc@1207 1016 NONE
adamc@1207 1017 | _ => NONE
adamc@1207 1018
adamc@1211 1019 fun constK s = wrap (const s) (fn () => s)
adamc@1211 1020
adamc@1211 1021 val funcName = altL [constK "COUNT",
adamc@1211 1022 constK "MIN",
adamc@1211 1023 constK "MAX",
adamc@1211 1024 constK "SUM",
adamc@1211 1025 constK "AVG"]
adamc@1211 1026
adamc@1204 1027 fun sqexp chs =
adamc@1206 1028 log "sqexp"
adamc@1207 1029 (altL [wrap prim SqConst,
adamc@1211 1030 wrap field Field,
adamc@1207 1031 wrap known SqKnown,
adamc@1211 1032 wrap func SqFunc,
adamc@1211 1033 wrap (const "COUNT(*)") (fn () => Count),
adamc@1207 1034 wrap sqlify Inj,
adamc@1211 1035 wrap (follow (const "COALESCE(") (follow sqexp (follow (const ",")
adamc@1211 1036 (follow (keep (fn ch => ch <> #")")) (const ")")))))
adamc@1211 1037 (fn ((), (e, _)) => e),
adamc@1207 1038 wrap (follow (ws (const "("))
adamc@1207 1039 (follow (wrap
adamc@1207 1040 (follow sqexp
adamc@1207 1041 (alt
adamc@1207 1042 (wrap
adamc@1207 1043 (follow (ws sqbrel)
adamc@1207 1044 (ws sqexp))
adamc@1207 1045 inl)
adamc@1207 1046 (always (inr ()))))
adamc@1207 1047 (fn (e1, sm) =>
adamc@1207 1048 case sm of
adamc@1207 1049 inl (bo, e2) => Binop (bo, e1, e2)
adamc@1207 1050 | inr () => e1))
adamc@1207 1051 (const ")")))
adamc@1207 1052 (fn ((), (e, ())) => e)])
adamc@1207 1053 chs
adamc@1206 1054
adamc@1207 1055 and known chs = wrap (follow known' (follow (const "(") (follow sqexp (const ")"))))
adamc@1211 1056 (fn ((), ((), (e, ()))) => e) chs
adamc@1211 1057
adamc@1211 1058 and func chs = wrap (follow funcName (follow (const "(") (follow sqexp (const ")"))))
adamc@1211 1059 (fn (f, ((), (e, ()))) => (f, e)) chs
adamc@1211 1060
adamc@1211 1061 datatype sitem =
adamc@1211 1062 SqField of string * string
adamc@1211 1063 | SqExp of sqexp * string
adamc@1211 1064
adamc@1211 1065 val sitem = alt (wrap field SqField)
adamc@1211 1066 (wrap (follow sqexp (follow (const " AS ") uw_ident))
adamc@1211 1067 (fn (e, ((), s)) => SqExp (e, s)))
adamc@1207 1068
adamc@1207 1069 val select = log "select"
adamc@1207 1070 (wrap (follow (const "SELECT ") (list sitem))
adamc@1207 1071 (fn ((), ls) => ls))
adamc@1201 1072
adamc@1201 1073 val fitem = wrap (follow uw_ident
adamc@1201 1074 (follow (const " AS ")
adamc@1201 1075 t_ident))
adamc@1201 1076 (fn (t, ((), f)) => (t, f))
adamc@1201 1077
adamc@1207 1078 val from = log "from"
adamc@1207 1079 (wrap (follow (const "FROM ") (list fitem))
adamc@1207 1080 (fn ((), ls) => ls))
adamc@1201 1081
adamc@1204 1082 val wher = wrap (follow (ws (const "WHERE ")) sqexp)
adamc@1204 1083 (fn ((), ls) => ls)
adamc@1204 1084
adamc@1227 1085 type query1 = {Select : sitem list,
adamc@1227 1086 From : (string * string) list,
adamc@1227 1087 Where : sqexp option}
adamc@1227 1088
adamc@1227 1089 val query1 = log "query1"
adamc@1207 1090 (wrap (follow (follow select from) (opt wher))
adamc@1207 1091 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
adamc@1201 1092
adamc@1227 1093 datatype query =
adamc@1227 1094 Query1 of query1
adamc@1227 1095 | Union of query * query
adamc@1227 1096
adamc@1227 1097 fun query chs = log "query"
adamc@1227 1098 (alt (wrap (follow (const "((")
adamc@1227 1099 (follow query
adamc@1227 1100 (follow (const ") UNION (")
adamc@1227 1101 (follow query (const "))")))))
adamc@1227 1102 (fn ((), (q1, ((), (q2, ())))) => Union (q1, q2)))
adamc@1227 1103 (wrap query1 Query1))
adamc@1227 1104 chs
adamc@1227 1105
adamc@1220 1106 datatype dml =
adamc@1220 1107 Insert of string * (string * sqexp) list
adamc@1221 1108 | Delete of string * sqexp
adamc@1223 1109 | Update of string * (string * sqexp) list * sqexp
adamc@1220 1110
adamc@1220 1111 val insert = log "insert"
adamc@1220 1112 (wrapP (follow (const "INSERT INTO ")
adamc@1220 1113 (follow uw_ident
adamc@1220 1114 (follow (const " (")
adamc@1220 1115 (follow (list uw_ident)
adamc@1220 1116 (follow (const ") VALUES (")
adamc@1220 1117 (follow (list sqexp)
adamc@1220 1118 (const ")")))))))
adamc@1220 1119 (fn ((), (tab, ((), (fs, ((), (es, ())))))) =>
adamc@1221 1120 (SOME (tab, ListPair.zipEq (fs, es)))
adamc@1220 1121 handle ListPair.UnequalLengths => NONE))
adamc@1220 1122
adamc@1221 1123 val delete = log "delete"
adamc@1221 1124 (wrap (follow (const "DELETE FROM ")
adamc@1221 1125 (follow uw_ident
adamc@1221 1126 (follow (const " AS T_T WHERE ")
adamc@1221 1127 sqexp)))
adamc@1221 1128 (fn ((), (tab, ((), es))) => (tab, es)))
adamc@1221 1129
adamc@1223 1130 val setting = log "setting"
adamc@1223 1131 (wrap (follow uw_ident (follow (const " = ") sqexp))
adamc@1223 1132 (fn (f, ((), e)) => (f, e)))
adamc@1223 1133
adamc@1223 1134 val update = log "update"
adamc@1223 1135 (wrap (follow (const "UPDATE ")
adamc@1223 1136 (follow uw_ident
adamc@1223 1137 (follow (const " AS T_T SET ")
adamc@1223 1138 (follow (list setting)
adamc@1223 1139 (follow (ws (const "WHERE "))
adamc@1223 1140 sqexp)))))
adamc@1223 1141 (fn ((), (tab, ((), (fs, ((), e))))) =>
adamc@1223 1142 (tab, fs, e)))
adamc@1223 1143
adamc@1220 1144 val dml = log "dml"
adamc@1221 1145 (altL [wrap insert Insert,
adamc@1223 1146 wrap delete Delete,
adamc@1223 1147 wrap update Update])
adamc@1220 1148
adamc@1236 1149 type check = exp * ErrorMsg.span
adamc@1236 1150
adamc@1236 1151 structure St :> sig
adamc@1236 1152 val reset : unit -> unit
adamc@1236 1153
adamc@1236 1154 type stashed
adamc@1236 1155 val stash : unit -> stashed
adamc@1236 1156 val reinstate : stashed -> unit
adamc@1236 1157
adamc@1236 1158 val nextVar : unit -> int
adamc@1236 1159
adamc@1236 1160 val assert : atom list -> unit
adamc@1236 1161
adamc@1236 1162 val addPath : check -> unit
adamc@1236 1163
adamc@1236 1164 val allowSend : atom list * exp list -> unit
adamc@1236 1165 val send : check -> unit
adamc@1236 1166
adamc@1236 1167 val allowInsert : atom list -> unit
adamc@1236 1168 val insert : ErrorMsg.span -> unit
adamc@1236 1169
adamc@1236 1170 val allowDelete : atom list -> unit
adamc@1236 1171 val delete : ErrorMsg.span -> unit
adamc@1236 1172
adamc@1236 1173 val allowUpdate : atom list -> unit
adamc@1236 1174 val update : ErrorMsg.span -> unit
adamc@1236 1175
adamc@1236 1176 val havocReln : reln -> unit
adamc@1236 1177 end = struct
adamc@1236 1178
adamc@1236 1179 val hnames = ref 1
adamc@1236 1180
adamc@1236 1181 type hyps = int * atom list
adamc@1236 1182
adamc@1236 1183 val db = Cc.database ()
adamc@1236 1184 val path = ref ([] : (hyps * check) option ref list)
adamc@1236 1185 val hyps = ref (0, [] : atom list)
adamc@1236 1186 val nvar = ref 0
adamc@1236 1187
adamc@1236 1188 fun reset () = (Cc.clear db;
adamc@1236 1189 path := [];
adamc@1236 1190 hyps := (0, []);
adamc@1236 1191 nvar := 0)
adamc@1236 1192
adamc@1236 1193 fun setHyps (h as (n', hs)) =
adamc@1236 1194 let
adamc@1236 1195 val (n, _) = !hyps
adamc@1236 1196 in
adamc@1236 1197 if n' = n then
adamc@1236 1198 ()
adamc@1236 1199 else
adamc@1236 1200 (hyps := h;
adamc@1236 1201 Cc.clear db;
adamc@1236 1202 app (fn a => Cc.assert (db, a)) hs)
adamc@1236 1203 end
adamc@1236 1204
adamc@1236 1205 type stashed = int * (hyps * check) option ref list * (int * atom list)
adamc@1236 1206 fun stash () = (!nvar, !path, !hyps)
adamc@1236 1207 fun reinstate (nv, p, h) =
adamc@1236 1208 (nvar := nv;
adamc@1236 1209 path := p;
adamc@1236 1210 setHyps h)
adamc@1236 1211
adamc@1236 1212 fun nextVar () =
adamc@1236 1213 let
adamc@1236 1214 val n = !nvar
adamc@1236 1215 in
adamc@1236 1216 nvar := n + 1;
adamc@1236 1217 n
adamc@1236 1218 end
adamc@1236 1219
adamc@1236 1220 fun assert ats =
adamc@1236 1221 let
adamc@1236 1222 val n = !hnames
adamc@1236 1223 val (_, hs) = !hyps
adamc@1236 1224 in
adamc@1236 1225 hnames := n + 1;
adamc@1236 1226 hyps := (n, ats @ hs);
adamc@1236 1227 app (fn a => Cc.assert (db, a)) ats
adamc@1236 1228 end
adamc@1236 1229
adamc@1236 1230 fun addPath c = path := ref (SOME (!hyps, c)) :: !path
adamc@1236 1231
adamc@1236 1232 val sendable = ref ([] : (atom list * exp list) list)
adamc@1236 1233
adamc@1236 1234 fun checkGoals goals unifs succ fail =
adamc@1236 1235 case goals of
adamc@1236 1236 [] => succ (unifs, [])
adamc@1236 1237 | AReln (Sql tab, [Lvar lv]) :: goals =>
adamc@1236 1238 let
adamc@1236 1239 val saved = stash ()
adamc@1236 1240 val (_, hyps) = !hyps
adamc@1236 1241
adamc@1236 1242 fun tryAll unifs hyps =
adamc@1236 1243 case hyps of
adamc@1236 1244 [] => fail ()
adamc@1236 1245 | AReln (Sql tab', [e]) :: hyps =>
adamc@1236 1246 if tab' = tab then
adamc@1236 1247 checkGoals goals (IM.insert (unifs, lv, e)) succ
adamc@1236 1248 (fn () => tryAll unifs hyps)
adamc@1236 1249 else
adamc@1236 1250 tryAll unifs hyps
adamc@1236 1251 | _ :: hyps => tryAll unifs hyps
adamc@1236 1252 in
adamc@1236 1253 tryAll unifs hyps
adamc@1236 1254 end
adamc@1236 1255 | AReln (r, es) :: goals => checkGoals goals unifs
adamc@1236 1256 (fn (unifs, ls) => succ (unifs, AReln (r, map (simplify unifs) es) :: ls))
adamc@1236 1257 fail
adamc@1236 1258 | ACond _ :: _ => fail ()
adamc@1236 1259
adamc@1236 1260 fun buildable (e, loc) =
adamc@1236 1261 let
adamc@1236 1262 fun doPols pols acc fail =
adamc@1236 1263 case pols of
adamc@1236 1264 [] => ((*Print.prefaces "buildable" [("Base", Print.p_list p_exp acc),
adamc@1236 1265 ("Derived", p_exp e),
adamc@1236 1266 ("Hyps", Print.p_list p_atom (#2 (!hyps)))];*)
adamc@1236 1267 if Cc.builtFrom (db, {Base = acc, Derived = e}) then
adamc@1236 1268 ()
adamc@1236 1269 else
adamc@1236 1270 fail ())
adamc@1236 1271 | (goals, es) :: pols =>
adamc@1236 1272 checkGoals goals IM.empty
adamc@1236 1273 (fn (unifs, goals) =>
adamc@1236 1274 if List.all (fn a => Cc.check (db, a)) goals then
adamc@1236 1275 doPols pols (map (simplify unifs) es @ acc) fail
adamc@1236 1276 else
adamc@1236 1277 doPols pols acc fail)
adamc@1236 1278 (fn () => doPols pols acc fail)
adamc@1236 1279 in
adamc@1236 1280 doPols (!sendable) []
adamc@1236 1281 (fn () => let
adamc@1236 1282 val (_, hs) = !hyps
adamc@1236 1283 in
adamc@1236 1284 ErrorMsg.errorAt loc "The information flow policy may be violated here.";
adamc@1236 1285 Print.preface ("Hypotheses", Print.p_list p_atom hs)
adamc@1236 1286 end)
adamc@1236 1287 end
adamc@1236 1288
adamc@1236 1289 fun checkPaths () =
adamc@1236 1290 let
adamc@1236 1291 val hs = !hyps
adamc@1236 1292 in
adamc@1236 1293 app (fn r =>
adamc@1236 1294 case !r of
adamc@1236 1295 NONE => ()
adamc@1236 1296 | SOME (hs, e) =>
adamc@1236 1297 (r := NONE;
adamc@1236 1298 setHyps hs;
adamc@1236 1299 buildable e)) (!path);
adamc@1236 1300 setHyps hs
adamc@1236 1301 end
adamc@1236 1302
adamc@1236 1303 fun allowSend v = sendable := v :: !sendable
adamc@1236 1304
adamc@1236 1305 fun send (e, loc) = ((*Print.preface ("Send", p_exp e);*)
adamc@1236 1306 checkPaths ();
adamc@1236 1307 if isKnown e then
adamc@1236 1308 ()
adamc@1236 1309 else
adamc@1236 1310 buildable (e, loc))
adamc@1236 1311
adamc@1236 1312 fun doable pols (loc : ErrorMsg.span) =
adamc@1236 1313 let
adamc@1236 1314 val pols = !pols
adamc@1236 1315 in
adamc@1236 1316 if List.exists (fn goals =>
adamc@1236 1317 checkGoals goals IM.empty
adamc@1236 1318 (fn (_, goals) => List.all (fn a => Cc.check (db, a)) goals)
adamc@1236 1319 (fn () => false)) pols then
adamc@1236 1320 ()
adamc@1236 1321 else
adamc@1236 1322 let
adamc@1236 1323 val (_, hs) = !hyps
adamc@1236 1324 in
adamc@1236 1325 ErrorMsg.errorAt loc "The database update policy may be violated here.";
adamc@1236 1326 Print.preface ("Hypotheses", Print.p_list p_atom hs)
adamc@1236 1327 end
adamc@1236 1328 end
adamc@1236 1329
adamc@1236 1330 val insertable = ref ([] : atom list list)
adamc@1236 1331 fun allowInsert v = insertable := v :: !insertable
adamc@1236 1332 val insert = doable insertable
adamc@1236 1333
adamc@1236 1334 val updatable = ref ([] : atom list list)
adamc@1236 1335 fun allowUpdate v = updatable := v :: !updatable
adamc@1236 1336 val update = doable updatable
adamc@1236 1337
adamc@1236 1338 val deletable = ref ([] : atom list list)
adamc@1236 1339 fun allowDelete v = deletable := v :: !deletable
adamc@1236 1340 val delete = doable deletable
adamc@1236 1341
adamc@1236 1342 fun havocReln r =
adamc@1236 1343 let
adamc@1236 1344 val n = !hnames
adamc@1236 1345 val (_, hs) = !hyps
adamc@1236 1346 in
adamc@1236 1347 hnames := n + 1;
adamc@1236 1348 hyps := (n, List.filter (fn AReln (r', _) => r' <> r | _ => true) hs)
adamc@1236 1349 end
adamc@1236 1350
adamc@1236 1351 end
adamc@1236 1352
adamc@1236 1353
adamc@1215 1354 fun removeDups (ls : (string * string) list) =
adamc@1211 1355 case ls of
adamc@1211 1356 [] => []
adamc@1211 1357 | x :: ls =>
adamc@1211 1358 let
adamc@1211 1359 val ls = removeDups ls
adamc@1211 1360 in
adamc@1211 1361 if List.exists (fn x' => x' = x) ls then
adamc@1211 1362 ls
adamc@1211 1363 else
adamc@1211 1364 x :: ls
adamc@1211 1365 end
adamc@1211 1366
adamc@1220 1367 fun expIn rv env rvOf =
adamc@1220 1368 let
adamc@1236 1369 fun expIn e =
adamc@1220 1370 let
adamc@1236 1371 fun default () = inl (rv ())
adamc@1220 1372 in
adamc@1220 1373 case e of
adamc@1236 1374 SqConst p => inl (Const p)
adamc@1236 1375 | Field (v, f) => inl (Proj (rvOf v, f))
adamc@1220 1376 | Binop (bo, e1, e2) =>
adamc@1220 1377 let
adamc@1236 1378 val e1 = expIn e1
adamc@1236 1379 val e2 = expIn e2
adamc@1220 1380 in
adamc@1236 1381 inr (case (bo, e1, e2) of
adamc@1236 1382 (Exps f, inl e1, inl e2) => f (e1, e2)
adamc@1236 1383 | (Props f, inr p1, inr p2) => f (p1, p2)
adamc@1236 1384 | _ => Unknown)
adamc@1220 1385 end
adamc@1220 1386 | SqKnown e =>
adamc@1236 1387 (case expIn e of
adamc@1236 1388 inl e => inr (Reln (Known, [e]))
adamc@1236 1389 | _ => inr Unknown)
adamc@1220 1390 | Inj e =>
adamc@1220 1391 let
adamc@1220 1392 fun deinj e =
adamc@1220 1393 case #1 e of
adamc@1236 1394 ERel n => List.nth (env, n)
adamc@1236 1395 | EField (e, f) => Proj (deinj e, f)
adamc@1236 1396 | _ => rv ()
adamc@1220 1397 in
adamc@1236 1398 inl (deinj e)
adamc@1220 1399 end
adamc@1220 1400 | SqFunc (f, e) =>
adamc@1236 1401 (case expIn e of
adamc@1236 1402 inl e => inl (Func (Other f, [e]))
adamc@1220 1403 | _ => default ())
adamc@1220 1404
adamc@1220 1405 | Count => default ()
adamc@1220 1406 end
adamc@1220 1407 in
adamc@1220 1408 expIn
adamc@1220 1409 end
adamc@1216 1410
adamc@1236 1411 fun decomp {Save = save, Restore = restore, Add = add} =
adamc@1216 1412 let
adamc@1236 1413 fun go p k =
adamc@1236 1414 case p of
adamc@1236 1415 True => k ()
adamc@1236 1416 | False => ()
adamc@1236 1417 | Unknown => ()
adamc@1236 1418 | And (p1, p2) => go p1 (fn () => go p2 k)
adamc@1236 1419 | Or (p1, p2) =>
adamc@1236 1420 let
adamc@1236 1421 val saved = save ()
adamc@1236 1422 in
adamc@1236 1423 go p1 k;
adamc@1236 1424 restore saved;
adamc@1236 1425 go p2 k
adamc@1236 1426 end
adamc@1236 1427 | Reln x => (add (AReln x); k ())
adamc@1236 1428 | Cond x => (add (ACond x); k ())
adamc@1236 1429 in
adamc@1236 1430 go
adamc@1236 1431 end
adamc@1236 1432
adamc@1236 1433 datatype queryMode =
adamc@1236 1434 SomeCol of exp list -> unit
adamc@1236 1435 | AllCols of exp -> unit
adamc@1236 1436
adamc@1236 1437 type 'a doQuery = {
adamc@1236 1438 Env : exp list,
adamc@1236 1439 NextVar : unit -> exp,
adamc@1236 1440 Add : atom -> unit,
adamc@1236 1441 Save : unit -> 'a,
adamc@1236 1442 Restore : 'a -> unit,
adamc@1236 1443 UsedExp : exp -> unit,
adamc@1236 1444 Cont : queryMode
adamc@1236 1445 }
adamc@1236 1446
adamc@1236 1447 fun doQuery (arg : 'a doQuery) e =
adamc@1236 1448 let
adamc@1236 1449 fun default () = print ("Warning: Information flow checker can't parse SQL query at "
adamc@1236 1450 ^ ErrorMsg.spanToString (#2 e) ^ "\n")
adamc@1216 1451 in
adamc@1216 1452 case parse query e of
adamc@1216 1453 NONE => default ()
adamc@1227 1454 | SOME q =>
adamc@1216 1455 let
adamc@1236 1456 fun doQuery q =
adamc@1227 1457 case q of
adamc@1227 1458 Query1 r =>
adamc@1227 1459 let
adamc@1236 1460 val rvs = map (fn (_, v) => (v, #NextVar arg ())) (#From r)
adamc@1214 1461
adamc@1227 1462 fun rvOf v =
adamc@1227 1463 case List.find (fn (v', _) => v' = v) rvs of
adamc@1227 1464 NONE => raise Fail "Iflow.queryProp: Bad table variable"
adamc@1227 1465 | SOME (_, e) => e
adamc@1214 1466
adamc@1236 1467 val expIn = expIn (#NextVar arg) (#Env arg) rvOf
adamc@1236 1468
adamc@1236 1469 val saved = #Save arg ()
adamc@1236 1470 fun addFrom () = app (fn (t, v) => #Add arg (AReln (Sql t, [rvOf v]))) (#From r)
adamc@1236 1471
adamc@1227 1472 fun usedFields e =
adamc@1227 1473 case e of
adamc@1227 1474 SqConst _ => []
adamc@1227 1475 | Field (v, f) => [(v, f)]
adamc@1227 1476 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
adamc@1227 1477 | SqKnown _ => []
adamc@1227 1478 | Inj _ => []
adamc@1227 1479 | SqFunc (_, e) => usedFields e
adamc@1227 1480 | Count => []
adamc@1214 1481
adamc@1236 1482 fun doUsed () = case #Where r of
adamc@1236 1483 NONE => ()
adamc@1236 1484 | SOME e =>
adamc@1236 1485 #UsedExp arg (Recd (ListUtil.mapi
adamc@1236 1486 (fn (n, (v, f)) => (Int.toString n,
adamc@1236 1487 Proj (rvOf v, f)))
adamc@1236 1488 (usedFields e)))
adamc@1211 1489
adamc@1236 1490 fun normal' () =
adamc@1236 1491 case #Cont arg of
adamc@1236 1492 SomeCol k =>
adamc@1227 1493 let
adamc@1236 1494 val sis = map (fn si =>
adamc@1236 1495 case si of
adamc@1236 1496 SqField (v, f) => Proj (rvOf v, f)
adamc@1236 1497 | SqExp (e, f) =>
adamc@1236 1498 case expIn e of
adamc@1236 1499 inr _ => #NextVar arg ()
adamc@1236 1500 | inl e => e) (#Select r)
adamc@1227 1501 in
adamc@1236 1502 k sis
adamc@1227 1503 end
adamc@1236 1504 | AllCols k =>
adamc@1227 1505 let
adamc@1236 1506 val (ts, es) =
adamc@1236 1507 foldl (fn (si, (ts, es)) =>
adamc@1227 1508 case si of
adamc@1227 1509 SqField (v, f) =>
adamc@1227 1510 let
adamc@1227 1511 val fs = getOpt (SM.find (ts, v), SM.empty)
adamc@1227 1512 in
adamc@1236 1513 (SM.insert (ts, v, SM.insert (fs, f, Proj (rvOf v, f))), es)
adamc@1227 1514 end
adamc@1227 1515 | SqExp (e, f) =>
adamc@1227 1516 let
adamc@1236 1517 val e =
adamc@1236 1518 case expIn e of
adamc@1236 1519 inr _ => #NextVar arg ()
adamc@1236 1520 | inl e => e
adamc@1227 1521 in
adamc@1236 1522 (ts, SM.insert (es, f, e))
adamc@1227 1523 end)
adamc@1236 1524 (SM.empty, SM.empty) (#Select r)
adamc@1227 1525 in
adamc@1236 1526 k (Recd (map (fn (t, fs) => (t, Recd (SM.listItemsi fs)))
adamc@1236 1527 (SM.listItemsi ts)
adamc@1236 1528 @ SM.listItemsi es))
adamc@1227 1529 end
adamc@1227 1530
adamc@1236 1531 fun doWhere final =
adamc@1236 1532 (addFrom ();
adamc@1236 1533 case #Where r of
adamc@1236 1534 NONE => (doUsed (); final ())
adamc@1236 1535 | SOME e =>
adamc@1236 1536 case expIn e of
adamc@1236 1537 inl _ => (doUsed (); final ())
adamc@1236 1538 | inr p =>
adamc@1236 1539 let
adamc@1236 1540 val saved = #Save arg ()
adamc@1236 1541 in
adamc@1236 1542 decomp {Save = #Save arg, Restore = #Restore arg, Add = #Add arg}
adamc@1236 1543 p (fn () => (doUsed (); final ()) handle Cc.Contradiction => ());
adamc@1236 1544 #Restore arg saved
adamc@1236 1545 end)
adamc@1236 1546 handle Cc.Contradiction => ()
adamc@1236 1547
adamc@1236 1548 fun normal () = doWhere normal'
adamc@1227 1549 in
adamc@1236 1550 (case #Select r of
adamc@1236 1551 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
adamc@1236 1552 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
adamc@1236 1553 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
adamc@1236 1554 (case #Cont arg of
adamc@1236 1555 SomeCol _ => ()
adamc@1236 1556 | AllCols k =>
adamc@1236 1557 let
adamc@1236 1558 fun answer e = k (Recd [(f, e)])
adamc@1236 1559
adamc@1236 1560 val () = answer (Func (DtCon0 "Basis.bool.False", []))
adamc@1236 1561 val saved = #Save arg ()
adamc@1236 1562 in
adamc@1236 1563 doWhere (fn () => answer (Func (DtCon0 "Basis.bool.True", [])));
adamc@1236 1564 #Restore arg saved
adamc@1236 1565 end)
adamc@1236 1566 | _ => normal ())
adamc@1236 1567 | _ => normal ())
adamc@1236 1568 before #Restore arg saved
adamc@1227 1569 end
adamc@1227 1570 | Union (q1, q2) =>
adamc@1220 1571 let
adamc@1236 1572 val saved = #Save arg ()
adamc@1220 1573 in
adamc@1236 1574 doQuery q1;
adamc@1236 1575 #Restore arg saved;
adamc@1236 1576 doQuery q2;
adamc@1236 1577 #Restore arg saved
adamc@1220 1578 end
adamc@1216 1579 in
adamc@1236 1580 doQuery q
adamc@1216 1581 end
adamc@1220 1582 end
adamc@1220 1583
adamc@1211 1584 fun evalPat env e (pt, _) =
adamc@1211 1585 case pt of
adamc@1236 1586 PWild => env
adamc@1236 1587 | PVar _ => e :: env
adamc@1236 1588 | PPrim _ => env
adamc@1236 1589 | PCon (_, pc, NONE) => (St.assert [AReln (PCon0 (patCon pc), [e])]; env)
adamc@1211 1590 | PCon (_, pc, SOME pt) =>
adamc@1211 1591 let
adamc@1236 1592 val env = evalPat env (Func (UnCon (patCon pc), [e])) pt
adamc@1211 1593 in
adamc@1236 1594 St.assert [AReln (PCon1 (patCon pc), [e])];
adamc@1236 1595 env
adamc@1211 1596 end
adamc@1211 1597 | PRecord xpts =>
adamc@1236 1598 foldl (fn ((x, pt, _), env) => evalPat env (Proj (e, x)) pt) env xpts
adamc@1236 1599 | PNone _ => (St.assert [AReln (PCon0 "None", [e])]; env)
adamc@1211 1600 | PSome (_, pt) =>
adamc@1211 1601 let
adamc@1236 1602 val env = evalPat env (Func (UnCon "Some", [e])) pt
adamc@1211 1603 in
adamc@1236 1604 St.assert [AReln (PCon1 "Some", [e])];
adamc@1236 1605 env
adamc@1211 1606 end
adamc@1211 1607
adamc@1236 1608 fun evalExp env (e as (_, loc)) k =
adamc@1236 1609 let
adamc@1236 1610 (*val () = Print.preface ("evalExp", MonoPrint.p_exp MonoEnv.empty e)*)
adamc@1211 1611
adamc@1236 1612 fun default () = k (Var (St.nextVar ()))
adamc@1234 1613
adamc@1234 1614 fun doFfi (m, s, es) =
adamc@1234 1615 if m = "Basis" andalso SS.member (writers, s) then
adamc@1234 1616 let
adamc@1236 1617 fun doArgs es =
adamc@1236 1618 case es of
adamc@1236 1619 [] => k (Recd [])
adamc@1236 1620 | e :: es =>
adamc@1236 1621 evalExp env e (fn e => (St.send (e, loc); doArgs es))
adamc@1234 1622 in
adamc@1236 1623 doArgs es
adamc@1234 1624 end
adamc@1234 1625 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
adamc@1234 1626 default ()
adamc@1234 1627 else
adamc@1234 1628 let
adamc@1236 1629 fun doArgs (es, acc) =
adamc@1236 1630 case es of
adamc@1236 1631 [] => k (Func (Other (m ^ "." ^ s), rev acc))
adamc@1236 1632 | e :: es =>
adamc@1236 1633 evalExp env e (fn e => doArgs (es, e :: acc))
adamc@1234 1634 in
adamc@1236 1635 doArgs (es, [])
adamc@1234 1636 end
adamc@1200 1637 in
adamc@1200 1638 case #1 e of
adamc@1236 1639 EPrim p => k (Const p)
adamc@1236 1640 | ERel n => k (List.nth (env, n))
adamc@1200 1641 | ENamed _ => default ()
adamc@1236 1642 | ECon (_, pc, NONE) => k (Func (DtCon0 (patCon pc), []))
adamc@1236 1643 | ECon (_, pc, SOME e) => evalExp env e (fn e => k (Func (DtCon1 (patCon pc), [e])))
adamc@1236 1644 | ENone _ => k (Func (DtCon0 "None", []))
adamc@1236 1645 | ESome (_, e) => evalExp env e (fn e => k (Func (DtCon1 "Some", [e])))
adamc@1200 1646 | EFfi _ => default ()
adamc@1213 1647
adamc@1234 1648 | EFfiApp x => doFfi x
adamc@1234 1649 | EApp ((EFfi (m, s), _), e) => doFfi (m, s, [e])
adamc@1213 1650
adamc@1236 1651 | EApp (e1, e2) => evalExp env e1 (fn _ => evalExp env e2 (fn _ => default ()))
adamc@1213 1652
adamc@1200 1653 | EAbs _ => default ()
adamc@1236 1654 | EUnop (s, e1) => evalExp env e1 (fn e1 => k (Func (Other s, [e1])))
adamc@1236 1655 | EBinop (s, e1, e2) => evalExp env e1 (fn e1 => evalExp env e2 (fn e2 => k (Func (Other s, [e1, e2]))))
adamc@1200 1656 | ERecord xets =>
adamc@1200 1657 let
adamc@1236 1658 fun doFields (xes, acc) =
adamc@1236 1659 case xes of
adamc@1236 1660 [] => k (Recd (rev acc))
adamc@1236 1661 | (x, e, _) :: xes =>
adamc@1236 1662 evalExp env e (fn e => doFields (xes, (x, e) :: acc))
adamc@1200 1663 in
adamc@1236 1664 doFields (xets, [])
adamc@1200 1665 end
adamc@1236 1666 | EField (e, s) => evalExp env e (fn e => k (Proj (e, s)))
adamc@1218 1667 | ECase (e, pes, {result = res, ...}) =>
adamc@1236 1668 evalExp env e (fn e =>
adamc@1218 1669 let
adamc@1236 1670 val () = St.addPath (e, loc)
adamc@1218 1671 in
adamc@1236 1672 app (fn (p, pe) =>
adamc@1236 1673 let
adamc@1236 1674 val saved = St.stash ()
adamc@1236 1675
adamc@1236 1676 val env = evalPat env e p
adamc@1236 1677 in
adamc@1236 1678 evalExp env pe k;
adamc@1236 1679 St.reinstate saved
adamc@1236 1680 end) pes
adamc@1236 1681 end handle Cc.Contradiction => ())
adamc@1200 1682 | EStrcat (e1, e2) =>
adamc@1236 1683 evalExp env e1 (fn e1 =>
adamc@1236 1684 evalExp env e2 (fn e2 =>
adamc@1236 1685 k (Func (Other "cat", [e1, e2]))))
adamc@1236 1686 | EError (e, _) => evalExp env e (fn e => St.send (e, loc))
adamc@1200 1687 | EReturnBlob {blob = b, mimeType = m, ...} =>
adamc@1236 1688 evalExp env b (fn b =>
adamc@1236 1689 (St.send (b, loc);
adamc@1236 1690 evalExp env m
adamc@1236 1691 (fn m => St.send (m, loc))))
adamc@1200 1692 | ERedirect (e, _) =>
adamc@1236 1693 evalExp env e (fn e => St.send (e, loc))
adamc@1200 1694 | EWrite e =>
adamc@1236 1695 evalExp env e (fn e => (St.send (e, loc);
adamc@1236 1696 k (Recd [])))
adamc@1200 1697 | ESeq (e1, e2) =>
adamc@1236 1698 evalExp env e1 (fn _ => evalExp env e2 k)
adamc@1200 1699 | ELet (_, _, e1, e2) =>
adamc@1236 1700 evalExp env e1 (fn e1 => evalExp (e1 :: env) e2 k)
adamc@1200 1701 | EClosure (n, es) =>
adamc@1200 1702 let
adamc@1236 1703 fun doArgs (es, acc) =
adamc@1236 1704 case es of
adamc@1236 1705 [] => k (Func (Other ("Cl" ^ Int.toString n), rev acc))
adamc@1236 1706 | e :: es =>
adamc@1236 1707 evalExp env e (fn e => doArgs (es, e :: acc))
adamc@1200 1708 in
adamc@1236 1709 doArgs (es, [])
adamc@1200 1710 end
adamc@1200 1711
adamc@1235 1712 | EQuery {query = q, body = b, initial = i, state = state, ...} =>
adamc@1236 1713 evalExp env q (fn _ =>
adamc@1236 1714 evalExp env i (fn i =>
adamc@1236 1715 let
adamc@1236 1716 val saved = St.stash ()
adamc@1200 1717
adamc@1236 1718 val r = Var (St.nextVar ())
adamc@1236 1719 val acc = Var (St.nextVar ())
adamc@1236 1720 in
adamc@1236 1721 if MonoUtil.Exp.existsB {typ = fn _ => false,
adamc@1236 1722 exp = fn (n, e) =>
adamc@1236 1723 case e of
adamc@1236 1724 ERel n' => n' = n
adamc@1236 1725 | _ => false,
adamc@1236 1726 bind = fn (n, b) =>
adamc@1236 1727 case b of
adamc@1236 1728 MonoUtil.Exp.RelE _ => n + 1
adamc@1236 1729 | _ => n}
adamc@1236 1730 0 b then
adamc@1236 1731 doQuery {Env = env,
adamc@1236 1732 NextVar = Var o St.nextVar,
adamc@1236 1733 Add = fn a => St.assert [a],
adamc@1236 1734 Save = St.stash,
adamc@1236 1735 Restore = St.reinstate,
adamc@1236 1736 UsedExp = fn e => St.send (e, loc),
adamc@1236 1737 Cont = AllCols (fn _ => (St.reinstate saved;
adamc@1236 1738 evalExp
adamc@1236 1739 (acc :: r :: env)
adamc@1236 1740 b (fn _ => default ())))} q
adamc@1236 1741 else
adamc@1236 1742 doQuery {Env = env,
adamc@1236 1743 NextVar = Var o St.nextVar,
adamc@1236 1744 Add = fn a => St.assert [a],
adamc@1236 1745 Save = St.stash,
adamc@1236 1746 Restore = St.reinstate,
adamc@1236 1747 UsedExp = fn e => St.send (e, loc),
adamc@1236 1748 Cont = AllCols (fn x =>
adamc@1236 1749 (St.assert [AReln (Eq, [r, x])];
adamc@1236 1750 evalExp (acc :: r :: env) b k))} q
adamc@1236 1751 end))
adamc@1220 1752 | EDml e =>
adamc@1220 1753 (case parse dml e of
adamc@1220 1754 NONE => (print ("Warning: Information flow checker can't parse DML command at "
adamc@1220 1755 ^ ErrorMsg.spanToString loc ^ "\n");
adamc@1220 1756 default ())
adamc@1220 1757 | SOME d =>
adamc@1220 1758 case d of
adamc@1220 1759 Insert (tab, es) =>
adamc@1220 1760 let
adamc@1236 1761 val new = St.nextVar ()
adamc@1220 1762
adamc@1236 1763 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1764 (fn _ => raise Fail "Iflow.evalExp: Bad field expression in INSERT [1]")
adamc@1220 1765
adamc@1236 1766 val es = map (fn (x, e) =>
adamc@1236 1767 case expIn e of
adamc@1236 1768 inl e => (x, e)
adamc@1236 1769 | inr _ => raise Fail "Iflow.evalExp: Bad field expression in INSERT [2]")
adamc@1236 1770 es
adamc@1220 1771
adamc@1236 1772 val saved = St.stash ()
adamc@1220 1773 in
adamc@1236 1774 St.assert [AReln (Sql (tab ^ "$New"), [Recd es])];
adamc@1236 1775 St.insert loc;
adamc@1236 1776 St.reinstate saved;
adamc@1236 1777 k (Recd [])
adamc@1221 1778 end
adamc@1221 1779 | Delete (tab, e) =>
adamc@1221 1780 let
adamc@1236 1781 val old = St.nextVar ()
adamc@1236 1782
adamc@1236 1783 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1784 (fn "T" => Var old
adamc@1236 1785 | _ => raise Fail "Iflow.evalExp: Bad field expression in DELETE")
adamc@1221 1786
adamc@1236 1787 val p = case expIn e of
adamc@1236 1788 inl e => raise Fail "Iflow.evalExp: DELETE with non-boolean"
adamc@1236 1789 | inr p => p
adamc@1236 1790
adamc@1236 1791 val saved = St.stash ()
adamc@1221 1792 in
adamc@1236 1793 St.assert [AReln (Sql "$Old", [Var old]),
adamc@1236 1794 AReln (Sql tab, [Var old])];
adamc@1236 1795 decomp {Save = St.stash,
adamc@1236 1796 Restore = St.reinstate,
adamc@1236 1797 Add = fn a => St.assert [a]} p
adamc@1236 1798 (fn () => (St.delete loc;
adamc@1236 1799 St.reinstate saved;
adamc@1236 1800 St.havocReln (Sql tab);
adamc@1236 1801 k (Recd []))
adamc@1236 1802 handle Cc.Contradiction => ())
adamc@1223 1803 end
adamc@1223 1804 | Update (tab, fs, e) =>
adamc@1223 1805 let
adamc@1236 1806 val new = St.nextVar ()
adamc@1236 1807 val old = St.nextVar ()
adamc@1223 1808
adamc@1236 1809 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1810 (fn "T" => Var old
adamc@1236 1811 | _ => raise Fail "Iflow.evalExp: Bad field expression in UPDATE")
adamc@1223 1812
adamc@1236 1813 val fs = map
adamc@1236 1814 (fn (x, e) =>
adamc@1236 1815 (x, case expIn e of
adamc@1236 1816 inl e => e
adamc@1236 1817 | inr _ => raise Fail
adamc@1236 1818 ("Iflow.evalExp: Selecting "
adamc@1236 1819 ^ "boolean expression")))
adamc@1236 1820 fs
adamc@1223 1821
adamc@1226 1822 val fs' = case SM.find (!tabs, tab) of
adamc@1224 1823 NONE => raise Fail "Iflow.evalExp: Updating unknown table"
adamc@1226 1824 | SOME (fs', _) => fs'
adamc@1224 1825
adamc@1224 1826 val fs = foldl (fn (f, fs) =>
adamc@1224 1827 if List.exists (fn (f', _) => f' = f) fs then
adamc@1224 1828 fs
adamc@1224 1829 else
adamc@1224 1830 (f, Proj (Var old, f)) :: fs) fs fs'
adamc@1224 1831
adamc@1236 1832 val p = case expIn e of
adamc@1236 1833 inl e => raise Fail "Iflow.evalExp: UPDATE with non-boolean"
adamc@1236 1834 | inr p => p
adamc@1236 1835 val saved = St.stash ()
adamc@1223 1836 in
adamc@1236 1837 St.assert [AReln (Sql (tab ^ "$New"), [Recd fs]),
adamc@1236 1838 AReln (Sql "$Old", [Var old]),
adamc@1236 1839 AReln (Sql tab, [Var old])];
adamc@1236 1840 decomp {Save = St.stash,
adamc@1236 1841 Restore = St.reinstate,
adamc@1236 1842 Add = fn a => St.assert [a]} p
adamc@1236 1843 (fn () => (St.update loc;
adamc@1236 1844 St.reinstate saved;
adamc@1236 1845 St.havocReln (Sql tab);
adamc@1236 1846 k (Recd []))
adamc@1236 1847 handle Cc.Contradiction => ())
adamc@1220 1848 end)
adamc@1220 1849
adamc@1229 1850 | ENextval (EPrim (Prim.String seq), _) =>
adamc@1229 1851 let
adamc@1236 1852 val nv = St.nextVar ()
adamc@1229 1853 in
adamc@1236 1854 St.assert [AReln (Sql (String.extract (seq, 3, NONE)), [Var nv])];
adamc@1236 1855 k (Var nv)
adamc@1229 1856 end
adamc@1200 1857 | ENextval _ => default ()
adamc@1200 1858 | ESetval _ => default ()
adamc@1200 1859
adamc@1213 1860 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
adamc@1217 1861 let
adamc@1236 1862 val nv = St.nextVar ()
adamc@1217 1863 in
adamc@1236 1864 St.assert [AReln (Known, [Var nv])];
adamc@1236 1865 k (Var nv)
adamc@1217 1866 end
adamc@1213 1867
adamc@1200 1868 | EUnurlify _ => default ()
adamc@1200 1869 | EJavaScript _ => default ()
adamc@1200 1870 | ESignalReturn _ => default ()
adamc@1200 1871 | ESignalBind _ => default ()
adamc@1200 1872 | ESignalSource _ => default ()
adamc@1200 1873 | EServerCall _ => default ()
adamc@1200 1874 | ERecv _ => default ()
adamc@1200 1875 | ESleep _ => default ()
adamc@1200 1876 | ESpawn _ => default ()
adamc@1200 1877 end
adamc@1200 1878
adamc@1200 1879 fun check file =
adamc@1200 1880 let
adamc@1236 1881 val () = St.reset ()
adamc@1236 1882
adamc@1213 1883 val file = MonoReduce.reduce file
adamc@1213 1884 val file = MonoOpt.optimize file
adamc@1213 1885 val file = Fuse.fuse file
adamc@1213 1886 val file = MonoOpt.optimize file
adamc@1216 1887 val file = MonoShake.shake file
adamc@1213 1888 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
adamc@1213 1889
adamc@1207 1890 val exptd = foldl (fn ((d, _), exptd) =>
adamc@1207 1891 case d of
adamc@1207 1892 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
adamc@1207 1893 | _ => exptd) IS.empty file
adamc@1207 1894
adamc@1236 1895 fun decl (d, _) =
adamc@1200 1896 case d of
adamc@1226 1897 DTable (tab, fs, pk, _) =>
adamc@1226 1898 let
adamc@1226 1899 val ks =
adamc@1226 1900 case #1 pk of
adamc@1226 1901 EPrim (Prim.String s) =>
adamc@1226 1902 (case String.tokens (fn ch => ch = #"," orelse ch = #" ") s of
adamc@1226 1903 [] => []
adamc@1226 1904 | pk => [pk])
adamc@1226 1905 | _ => []
adamc@1226 1906 in
adamc@1226 1907 if size tab >= 3 then
adamc@1236 1908 tabs := SM.insert (!tabs, String.extract (tab, 3, NONE),
adamc@1236 1909 (map #1 fs,
adamc@1236 1910 map (map (fn s => str (Char.toUpper (String.sub (s, 3)))
adamc@1236 1911 ^ String.extract (s, 4, NONE))) ks))
adamc@1226 1912 else
adamc@1226 1913 raise Fail "Table name does not begin with uw_"
adamc@1226 1914 end
adamc@1224 1915 | DVal (_, n, _, e, _) =>
adamc@1200 1916 let
adamc@1207 1917 val isExptd = IS.member (exptd, n)
adamc@1207 1918
adamc@1236 1919 val saved = St.stash ()
adamc@1236 1920
adamc@1236 1921 fun deAbs (e, env, ps) =
adamc@1200 1922 case #1 e of
adamc@1236 1923 EAbs (_, _, _, e) =>
adamc@1236 1924 let
adamc@1236 1925 val nv = Var (St.nextVar ())
adamc@1236 1926 in
adamc@1236 1927 deAbs (e, nv :: env,
adamc@1236 1928 if isExptd then
adamc@1236 1929 AReln (Known, [nv]) :: ps
adamc@1236 1930 else
adamc@1236 1931 ps)
adamc@1236 1932 end
adamc@1236 1933 | _ => (e, env, ps)
adamc@1200 1934
adamc@1236 1935 val (e, env, ps) = deAbs (e, [], [])
adamc@1200 1936 in
adamc@1236 1937 St.assert ps;
adamc@1236 1938 (evalExp env e (fn _ => ()) handle Cc.Contradiction => ());
adamc@1236 1939 St.reinstate saved
adamc@1200 1940 end
adamc@1202 1941
adamc@1220 1942 | DPolicy pol =>
adamc@1218 1943 let
adamc@1236 1944 val rvN = ref 0
adamc@1236 1945 fun rv () =
adamc@1236 1946 let
adamc@1236 1947 val n = !rvN
adamc@1236 1948 in
adamc@1236 1949 rvN := n + 1;
adamc@1236 1950 Lvar n
adamc@1236 1951 end
adamc@1236 1952
adamc@1236 1953 val atoms = ref ([] : atom list)
adamc@1236 1954 fun doQ k = doQuery {Env = [],
adamc@1236 1955 NextVar = rv,
adamc@1236 1956 Add = fn a => atoms := a :: !atoms,
adamc@1236 1957 Save = fn () => !atoms,
adamc@1236 1958 Restore = fn ls => atoms := ls,
adamc@1236 1959 UsedExp = fn _ => (),
adamc@1236 1960 Cont = SomeCol (fn es => k (!atoms, es))}
adamc@1218 1961 in
adamc@1220 1962 case pol of
adamc@1220 1963 PolClient e =>
adamc@1236 1964 doQ (fn (ats, es) => St.allowSend (ats, es)) e
adamc@1220 1965 | PolInsert e =>
adamc@1236 1966 doQ (fn (ats, _) => St.allowInsert ats) e
adamc@1221 1967 | PolDelete e =>
adamc@1236 1968 doQ (fn (ats, _) => St.allowDelete ats) e
adamc@1223 1969 | PolUpdate e =>
adamc@1236 1970 doQ (fn (ats, _) => St.allowUpdate ats) e
adamc@1229 1971 | PolSequence e =>
adamc@1229 1972 (case #1 e of
adamc@1229 1973 EPrim (Prim.String seq) =>
adamc@1229 1974 let
adamc@1236 1975 val p = AReln (Sql (String.extract (seq, 3, NONE)), [Lvar 0])
adamc@1229 1976 val outs = [Lvar 0]
adamc@1229 1977 in
adamc@1236 1978 St.allowSend ([p], outs)
adamc@1229 1979 end
adamc@1236 1980 | _ => ())
adamc@1218 1981 end
adamc@1214 1982
adamc@1236 1983 | _ => ()
adamc@1200 1984 in
adamc@1236 1985 app decl file
adamc@1200 1986 end
adamc@1200 1987
adamc@1213 1988 val check = fn file =>
adamc@1213 1989 let
adamc@1213 1990 val oldInline = Settings.getMonoInline ()
adamc@1213 1991 in
adamc@1213 1992 (Settings.setMonoInline (case Int.maxInt of
adamc@1213 1993 NONE => 1000000
adamc@1213 1994 | SOME n => n);
adamc@1213 1995 check file;
adamc@1213 1996 Settings.setMonoInline oldInline)
adamc@1213 1997 handle ex => (Settings.setMonoInline oldInline;
adamc@1213 1998 raise ex)
adamc@1213 1999 end
adamc@1213 2000
adamc@1200 2001 end