annotate src/iflow.sml @ 1237:a9c200f73f24

More descriptive info flow error message
author Adam Chlipala <adamc@hcoop.net>
date Tue, 13 Apr 2010 16:36:16 -0400
parents d5ecceb7d1a1
children d6938ab3b5ae
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@1237 1285 Print.prefaces "Situation" [("Hypotheses", Print.p_list p_atom hs),
adamc@1237 1286 ("User learns", p_exp e)]
adamc@1236 1287 end)
adamc@1236 1288 end
adamc@1236 1289
adamc@1236 1290 fun checkPaths () =
adamc@1236 1291 let
adamc@1236 1292 val hs = !hyps
adamc@1236 1293 in
adamc@1236 1294 app (fn r =>
adamc@1236 1295 case !r of
adamc@1236 1296 NONE => ()
adamc@1236 1297 | SOME (hs, e) =>
adamc@1236 1298 (r := NONE;
adamc@1236 1299 setHyps hs;
adamc@1236 1300 buildable e)) (!path);
adamc@1236 1301 setHyps hs
adamc@1236 1302 end
adamc@1236 1303
adamc@1236 1304 fun allowSend v = sendable := v :: !sendable
adamc@1236 1305
adamc@1236 1306 fun send (e, loc) = ((*Print.preface ("Send", p_exp e);*)
adamc@1236 1307 checkPaths ();
adamc@1236 1308 if isKnown e then
adamc@1236 1309 ()
adamc@1236 1310 else
adamc@1236 1311 buildable (e, loc))
adamc@1236 1312
adamc@1236 1313 fun doable pols (loc : ErrorMsg.span) =
adamc@1236 1314 let
adamc@1236 1315 val pols = !pols
adamc@1236 1316 in
adamc@1236 1317 if List.exists (fn goals =>
adamc@1236 1318 checkGoals goals IM.empty
adamc@1236 1319 (fn (_, goals) => List.all (fn a => Cc.check (db, a)) goals)
adamc@1236 1320 (fn () => false)) pols then
adamc@1236 1321 ()
adamc@1236 1322 else
adamc@1236 1323 let
adamc@1236 1324 val (_, hs) = !hyps
adamc@1236 1325 in
adamc@1236 1326 ErrorMsg.errorAt loc "The database update policy may be violated here.";
adamc@1236 1327 Print.preface ("Hypotheses", Print.p_list p_atom hs)
adamc@1236 1328 end
adamc@1236 1329 end
adamc@1236 1330
adamc@1236 1331 val insertable = ref ([] : atom list list)
adamc@1236 1332 fun allowInsert v = insertable := v :: !insertable
adamc@1236 1333 val insert = doable insertable
adamc@1236 1334
adamc@1236 1335 val updatable = ref ([] : atom list list)
adamc@1236 1336 fun allowUpdate v = updatable := v :: !updatable
adamc@1236 1337 val update = doable updatable
adamc@1236 1338
adamc@1236 1339 val deletable = ref ([] : atom list list)
adamc@1236 1340 fun allowDelete v = deletable := v :: !deletable
adamc@1236 1341 val delete = doable deletable
adamc@1236 1342
adamc@1236 1343 fun havocReln r =
adamc@1236 1344 let
adamc@1236 1345 val n = !hnames
adamc@1236 1346 val (_, hs) = !hyps
adamc@1236 1347 in
adamc@1236 1348 hnames := n + 1;
adamc@1236 1349 hyps := (n, List.filter (fn AReln (r', _) => r' <> r | _ => true) hs)
adamc@1236 1350 end
adamc@1236 1351
adamc@1236 1352 end
adamc@1236 1353
adamc@1236 1354
adamc@1215 1355 fun removeDups (ls : (string * string) list) =
adamc@1211 1356 case ls of
adamc@1211 1357 [] => []
adamc@1211 1358 | x :: ls =>
adamc@1211 1359 let
adamc@1211 1360 val ls = removeDups ls
adamc@1211 1361 in
adamc@1211 1362 if List.exists (fn x' => x' = x) ls then
adamc@1211 1363 ls
adamc@1211 1364 else
adamc@1211 1365 x :: ls
adamc@1211 1366 end
adamc@1211 1367
adamc@1220 1368 fun expIn rv env rvOf =
adamc@1220 1369 let
adamc@1236 1370 fun expIn e =
adamc@1220 1371 let
adamc@1236 1372 fun default () = inl (rv ())
adamc@1220 1373 in
adamc@1220 1374 case e of
adamc@1236 1375 SqConst p => inl (Const p)
adamc@1236 1376 | Field (v, f) => inl (Proj (rvOf v, f))
adamc@1220 1377 | Binop (bo, e1, e2) =>
adamc@1220 1378 let
adamc@1236 1379 val e1 = expIn e1
adamc@1236 1380 val e2 = expIn e2
adamc@1220 1381 in
adamc@1236 1382 inr (case (bo, e1, e2) of
adamc@1236 1383 (Exps f, inl e1, inl e2) => f (e1, e2)
adamc@1236 1384 | (Props f, inr p1, inr p2) => f (p1, p2)
adamc@1236 1385 | _ => Unknown)
adamc@1220 1386 end
adamc@1220 1387 | SqKnown e =>
adamc@1236 1388 (case expIn e of
adamc@1236 1389 inl e => inr (Reln (Known, [e]))
adamc@1236 1390 | _ => inr Unknown)
adamc@1220 1391 | Inj e =>
adamc@1220 1392 let
adamc@1220 1393 fun deinj e =
adamc@1220 1394 case #1 e of
adamc@1236 1395 ERel n => List.nth (env, n)
adamc@1236 1396 | EField (e, f) => Proj (deinj e, f)
adamc@1236 1397 | _ => rv ()
adamc@1220 1398 in
adamc@1236 1399 inl (deinj e)
adamc@1220 1400 end
adamc@1220 1401 | SqFunc (f, e) =>
adamc@1236 1402 (case expIn e of
adamc@1236 1403 inl e => inl (Func (Other f, [e]))
adamc@1220 1404 | _ => default ())
adamc@1220 1405
adamc@1220 1406 | Count => default ()
adamc@1220 1407 end
adamc@1220 1408 in
adamc@1220 1409 expIn
adamc@1220 1410 end
adamc@1216 1411
adamc@1236 1412 fun decomp {Save = save, Restore = restore, Add = add} =
adamc@1216 1413 let
adamc@1236 1414 fun go p k =
adamc@1236 1415 case p of
adamc@1236 1416 True => k ()
adamc@1236 1417 | False => ()
adamc@1236 1418 | Unknown => ()
adamc@1236 1419 | And (p1, p2) => go p1 (fn () => go p2 k)
adamc@1236 1420 | Or (p1, p2) =>
adamc@1236 1421 let
adamc@1236 1422 val saved = save ()
adamc@1236 1423 in
adamc@1236 1424 go p1 k;
adamc@1236 1425 restore saved;
adamc@1236 1426 go p2 k
adamc@1236 1427 end
adamc@1236 1428 | Reln x => (add (AReln x); k ())
adamc@1236 1429 | Cond x => (add (ACond x); k ())
adamc@1236 1430 in
adamc@1236 1431 go
adamc@1236 1432 end
adamc@1236 1433
adamc@1236 1434 datatype queryMode =
adamc@1236 1435 SomeCol of exp list -> unit
adamc@1236 1436 | AllCols of exp -> unit
adamc@1236 1437
adamc@1236 1438 type 'a doQuery = {
adamc@1236 1439 Env : exp list,
adamc@1236 1440 NextVar : unit -> exp,
adamc@1236 1441 Add : atom -> unit,
adamc@1236 1442 Save : unit -> 'a,
adamc@1236 1443 Restore : 'a -> unit,
adamc@1236 1444 UsedExp : exp -> unit,
adamc@1236 1445 Cont : queryMode
adamc@1236 1446 }
adamc@1236 1447
adamc@1236 1448 fun doQuery (arg : 'a doQuery) e =
adamc@1236 1449 let
adamc@1236 1450 fun default () = print ("Warning: Information flow checker can't parse SQL query at "
adamc@1236 1451 ^ ErrorMsg.spanToString (#2 e) ^ "\n")
adamc@1216 1452 in
adamc@1216 1453 case parse query e of
adamc@1216 1454 NONE => default ()
adamc@1227 1455 | SOME q =>
adamc@1216 1456 let
adamc@1236 1457 fun doQuery q =
adamc@1227 1458 case q of
adamc@1227 1459 Query1 r =>
adamc@1227 1460 let
adamc@1236 1461 val rvs = map (fn (_, v) => (v, #NextVar arg ())) (#From r)
adamc@1214 1462
adamc@1227 1463 fun rvOf v =
adamc@1227 1464 case List.find (fn (v', _) => v' = v) rvs of
adamc@1227 1465 NONE => raise Fail "Iflow.queryProp: Bad table variable"
adamc@1227 1466 | SOME (_, e) => e
adamc@1214 1467
adamc@1236 1468 val expIn = expIn (#NextVar arg) (#Env arg) rvOf
adamc@1236 1469
adamc@1236 1470 val saved = #Save arg ()
adamc@1236 1471 fun addFrom () = app (fn (t, v) => #Add arg (AReln (Sql t, [rvOf v]))) (#From r)
adamc@1236 1472
adamc@1227 1473 fun usedFields e =
adamc@1227 1474 case e of
adamc@1227 1475 SqConst _ => []
adamc@1227 1476 | Field (v, f) => [(v, f)]
adamc@1227 1477 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
adamc@1227 1478 | SqKnown _ => []
adamc@1227 1479 | Inj _ => []
adamc@1227 1480 | SqFunc (_, e) => usedFields e
adamc@1227 1481 | Count => []
adamc@1214 1482
adamc@1236 1483 fun doUsed () = case #Where r of
adamc@1236 1484 NONE => ()
adamc@1236 1485 | SOME e =>
adamc@1236 1486 #UsedExp arg (Recd (ListUtil.mapi
adamc@1236 1487 (fn (n, (v, f)) => (Int.toString n,
adamc@1236 1488 Proj (rvOf v, f)))
adamc@1236 1489 (usedFields e)))
adamc@1211 1490
adamc@1236 1491 fun normal' () =
adamc@1236 1492 case #Cont arg of
adamc@1236 1493 SomeCol k =>
adamc@1227 1494 let
adamc@1236 1495 val sis = map (fn si =>
adamc@1236 1496 case si of
adamc@1236 1497 SqField (v, f) => Proj (rvOf v, f)
adamc@1236 1498 | SqExp (e, f) =>
adamc@1236 1499 case expIn e of
adamc@1236 1500 inr _ => #NextVar arg ()
adamc@1236 1501 | inl e => e) (#Select r)
adamc@1227 1502 in
adamc@1236 1503 k sis
adamc@1227 1504 end
adamc@1236 1505 | AllCols k =>
adamc@1227 1506 let
adamc@1236 1507 val (ts, es) =
adamc@1236 1508 foldl (fn (si, (ts, es)) =>
adamc@1227 1509 case si of
adamc@1227 1510 SqField (v, f) =>
adamc@1227 1511 let
adamc@1227 1512 val fs = getOpt (SM.find (ts, v), SM.empty)
adamc@1227 1513 in
adamc@1236 1514 (SM.insert (ts, v, SM.insert (fs, f, Proj (rvOf v, f))), es)
adamc@1227 1515 end
adamc@1227 1516 | SqExp (e, f) =>
adamc@1227 1517 let
adamc@1236 1518 val e =
adamc@1236 1519 case expIn e of
adamc@1236 1520 inr _ => #NextVar arg ()
adamc@1236 1521 | inl e => e
adamc@1227 1522 in
adamc@1236 1523 (ts, SM.insert (es, f, e))
adamc@1227 1524 end)
adamc@1236 1525 (SM.empty, SM.empty) (#Select r)
adamc@1227 1526 in
adamc@1236 1527 k (Recd (map (fn (t, fs) => (t, Recd (SM.listItemsi fs)))
adamc@1236 1528 (SM.listItemsi ts)
adamc@1236 1529 @ SM.listItemsi es))
adamc@1227 1530 end
adamc@1227 1531
adamc@1236 1532 fun doWhere final =
adamc@1236 1533 (addFrom ();
adamc@1236 1534 case #Where r of
adamc@1236 1535 NONE => (doUsed (); final ())
adamc@1236 1536 | SOME e =>
adamc@1236 1537 case expIn e of
adamc@1236 1538 inl _ => (doUsed (); final ())
adamc@1236 1539 | inr p =>
adamc@1236 1540 let
adamc@1236 1541 val saved = #Save arg ()
adamc@1236 1542 in
adamc@1236 1543 decomp {Save = #Save arg, Restore = #Restore arg, Add = #Add arg}
adamc@1236 1544 p (fn () => (doUsed (); final ()) handle Cc.Contradiction => ());
adamc@1236 1545 #Restore arg saved
adamc@1236 1546 end)
adamc@1236 1547 handle Cc.Contradiction => ()
adamc@1236 1548
adamc@1236 1549 fun normal () = doWhere normal'
adamc@1227 1550 in
adamc@1236 1551 (case #Select r of
adamc@1236 1552 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
adamc@1236 1553 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
adamc@1236 1554 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
adamc@1236 1555 (case #Cont arg of
adamc@1236 1556 SomeCol _ => ()
adamc@1236 1557 | AllCols k =>
adamc@1236 1558 let
adamc@1236 1559 fun answer e = k (Recd [(f, e)])
adamc@1236 1560
adamc@1236 1561 val () = answer (Func (DtCon0 "Basis.bool.False", []))
adamc@1236 1562 val saved = #Save arg ()
adamc@1236 1563 in
adamc@1236 1564 doWhere (fn () => answer (Func (DtCon0 "Basis.bool.True", [])));
adamc@1236 1565 #Restore arg saved
adamc@1236 1566 end)
adamc@1236 1567 | _ => normal ())
adamc@1236 1568 | _ => normal ())
adamc@1236 1569 before #Restore arg saved
adamc@1227 1570 end
adamc@1227 1571 | Union (q1, q2) =>
adamc@1220 1572 let
adamc@1236 1573 val saved = #Save arg ()
adamc@1220 1574 in
adamc@1236 1575 doQuery q1;
adamc@1236 1576 #Restore arg saved;
adamc@1236 1577 doQuery q2;
adamc@1236 1578 #Restore arg saved
adamc@1220 1579 end
adamc@1216 1580 in
adamc@1236 1581 doQuery q
adamc@1216 1582 end
adamc@1220 1583 end
adamc@1220 1584
adamc@1211 1585 fun evalPat env e (pt, _) =
adamc@1211 1586 case pt of
adamc@1236 1587 PWild => env
adamc@1236 1588 | PVar _ => e :: env
adamc@1236 1589 | PPrim _ => env
adamc@1236 1590 | PCon (_, pc, NONE) => (St.assert [AReln (PCon0 (patCon pc), [e])]; env)
adamc@1211 1591 | PCon (_, pc, SOME pt) =>
adamc@1211 1592 let
adamc@1236 1593 val env = evalPat env (Func (UnCon (patCon pc), [e])) pt
adamc@1211 1594 in
adamc@1236 1595 St.assert [AReln (PCon1 (patCon pc), [e])];
adamc@1236 1596 env
adamc@1211 1597 end
adamc@1211 1598 | PRecord xpts =>
adamc@1236 1599 foldl (fn ((x, pt, _), env) => evalPat env (Proj (e, x)) pt) env xpts
adamc@1236 1600 | PNone _ => (St.assert [AReln (PCon0 "None", [e])]; env)
adamc@1211 1601 | PSome (_, pt) =>
adamc@1211 1602 let
adamc@1236 1603 val env = evalPat env (Func (UnCon "Some", [e])) pt
adamc@1211 1604 in
adamc@1236 1605 St.assert [AReln (PCon1 "Some", [e])];
adamc@1236 1606 env
adamc@1211 1607 end
adamc@1211 1608
adamc@1236 1609 fun evalExp env (e as (_, loc)) k =
adamc@1236 1610 let
adamc@1236 1611 (*val () = Print.preface ("evalExp", MonoPrint.p_exp MonoEnv.empty e)*)
adamc@1211 1612
adamc@1236 1613 fun default () = k (Var (St.nextVar ()))
adamc@1234 1614
adamc@1234 1615 fun doFfi (m, s, es) =
adamc@1234 1616 if m = "Basis" andalso SS.member (writers, s) then
adamc@1234 1617 let
adamc@1236 1618 fun doArgs es =
adamc@1236 1619 case es of
adamc@1236 1620 [] => k (Recd [])
adamc@1236 1621 | e :: es =>
adamc@1236 1622 evalExp env e (fn e => (St.send (e, loc); doArgs es))
adamc@1234 1623 in
adamc@1236 1624 doArgs es
adamc@1234 1625 end
adamc@1234 1626 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
adamc@1234 1627 default ()
adamc@1234 1628 else
adamc@1234 1629 let
adamc@1236 1630 fun doArgs (es, acc) =
adamc@1236 1631 case es of
adamc@1236 1632 [] => k (Func (Other (m ^ "." ^ s), rev acc))
adamc@1236 1633 | e :: es =>
adamc@1236 1634 evalExp env e (fn e => doArgs (es, e :: acc))
adamc@1234 1635 in
adamc@1236 1636 doArgs (es, [])
adamc@1234 1637 end
adamc@1200 1638 in
adamc@1200 1639 case #1 e of
adamc@1236 1640 EPrim p => k (Const p)
adamc@1236 1641 | ERel n => k (List.nth (env, n))
adamc@1200 1642 | ENamed _ => default ()
adamc@1236 1643 | ECon (_, pc, NONE) => k (Func (DtCon0 (patCon pc), []))
adamc@1236 1644 | ECon (_, pc, SOME e) => evalExp env e (fn e => k (Func (DtCon1 (patCon pc), [e])))
adamc@1236 1645 | ENone _ => k (Func (DtCon0 "None", []))
adamc@1236 1646 | ESome (_, e) => evalExp env e (fn e => k (Func (DtCon1 "Some", [e])))
adamc@1200 1647 | EFfi _ => default ()
adamc@1213 1648
adamc@1234 1649 | EFfiApp x => doFfi x
adamc@1234 1650 | EApp ((EFfi (m, s), _), e) => doFfi (m, s, [e])
adamc@1213 1651
adamc@1236 1652 | EApp (e1, e2) => evalExp env e1 (fn _ => evalExp env e2 (fn _ => default ()))
adamc@1213 1653
adamc@1200 1654 | EAbs _ => default ()
adamc@1236 1655 | EUnop (s, e1) => evalExp env e1 (fn e1 => k (Func (Other s, [e1])))
adamc@1236 1656 | EBinop (s, e1, e2) => evalExp env e1 (fn e1 => evalExp env e2 (fn e2 => k (Func (Other s, [e1, e2]))))
adamc@1200 1657 | ERecord xets =>
adamc@1200 1658 let
adamc@1236 1659 fun doFields (xes, acc) =
adamc@1236 1660 case xes of
adamc@1236 1661 [] => k (Recd (rev acc))
adamc@1236 1662 | (x, e, _) :: xes =>
adamc@1236 1663 evalExp env e (fn e => doFields (xes, (x, e) :: acc))
adamc@1200 1664 in
adamc@1236 1665 doFields (xets, [])
adamc@1200 1666 end
adamc@1236 1667 | EField (e, s) => evalExp env e (fn e => k (Proj (e, s)))
adamc@1218 1668 | ECase (e, pes, {result = res, ...}) =>
adamc@1236 1669 evalExp env e (fn e =>
adamc@1218 1670 let
adamc@1236 1671 val () = St.addPath (e, loc)
adamc@1218 1672 in
adamc@1236 1673 app (fn (p, pe) =>
adamc@1236 1674 let
adamc@1236 1675 val saved = St.stash ()
adamc@1236 1676
adamc@1236 1677 val env = evalPat env e p
adamc@1236 1678 in
adamc@1236 1679 evalExp env pe k;
adamc@1236 1680 St.reinstate saved
adamc@1236 1681 end) pes
adamc@1236 1682 end handle Cc.Contradiction => ())
adamc@1200 1683 | EStrcat (e1, e2) =>
adamc@1236 1684 evalExp env e1 (fn e1 =>
adamc@1236 1685 evalExp env e2 (fn e2 =>
adamc@1236 1686 k (Func (Other "cat", [e1, e2]))))
adamc@1236 1687 | EError (e, _) => evalExp env e (fn e => St.send (e, loc))
adamc@1200 1688 | EReturnBlob {blob = b, mimeType = m, ...} =>
adamc@1236 1689 evalExp env b (fn b =>
adamc@1236 1690 (St.send (b, loc);
adamc@1236 1691 evalExp env m
adamc@1236 1692 (fn m => St.send (m, loc))))
adamc@1200 1693 | ERedirect (e, _) =>
adamc@1236 1694 evalExp env e (fn e => St.send (e, loc))
adamc@1200 1695 | EWrite e =>
adamc@1236 1696 evalExp env e (fn e => (St.send (e, loc);
adamc@1236 1697 k (Recd [])))
adamc@1200 1698 | ESeq (e1, e2) =>
adamc@1236 1699 evalExp env e1 (fn _ => evalExp env e2 k)
adamc@1200 1700 | ELet (_, _, e1, e2) =>
adamc@1236 1701 evalExp env e1 (fn e1 => evalExp (e1 :: env) e2 k)
adamc@1200 1702 | EClosure (n, es) =>
adamc@1200 1703 let
adamc@1236 1704 fun doArgs (es, acc) =
adamc@1236 1705 case es of
adamc@1236 1706 [] => k (Func (Other ("Cl" ^ Int.toString n), rev acc))
adamc@1236 1707 | e :: es =>
adamc@1236 1708 evalExp env e (fn e => doArgs (es, e :: acc))
adamc@1200 1709 in
adamc@1236 1710 doArgs (es, [])
adamc@1200 1711 end
adamc@1200 1712
adamc@1235 1713 | EQuery {query = q, body = b, initial = i, state = state, ...} =>
adamc@1236 1714 evalExp env q (fn _ =>
adamc@1236 1715 evalExp env i (fn i =>
adamc@1236 1716 let
adamc@1236 1717 val saved = St.stash ()
adamc@1200 1718
adamc@1236 1719 val r = Var (St.nextVar ())
adamc@1236 1720 val acc = Var (St.nextVar ())
adamc@1236 1721 in
adamc@1236 1722 if MonoUtil.Exp.existsB {typ = fn _ => false,
adamc@1236 1723 exp = fn (n, e) =>
adamc@1236 1724 case e of
adamc@1236 1725 ERel n' => n' = n
adamc@1236 1726 | _ => false,
adamc@1236 1727 bind = fn (n, b) =>
adamc@1236 1728 case b of
adamc@1236 1729 MonoUtil.Exp.RelE _ => n + 1
adamc@1236 1730 | _ => n}
adamc@1236 1731 0 b then
adamc@1236 1732 doQuery {Env = env,
adamc@1236 1733 NextVar = Var o St.nextVar,
adamc@1236 1734 Add = fn a => St.assert [a],
adamc@1236 1735 Save = St.stash,
adamc@1236 1736 Restore = St.reinstate,
adamc@1236 1737 UsedExp = fn e => St.send (e, loc),
adamc@1236 1738 Cont = AllCols (fn _ => (St.reinstate saved;
adamc@1236 1739 evalExp
adamc@1236 1740 (acc :: r :: env)
adamc@1236 1741 b (fn _ => default ())))} q
adamc@1236 1742 else
adamc@1236 1743 doQuery {Env = env,
adamc@1236 1744 NextVar = Var o St.nextVar,
adamc@1236 1745 Add = fn a => St.assert [a],
adamc@1236 1746 Save = St.stash,
adamc@1236 1747 Restore = St.reinstate,
adamc@1236 1748 UsedExp = fn e => St.send (e, loc),
adamc@1236 1749 Cont = AllCols (fn x =>
adamc@1236 1750 (St.assert [AReln (Eq, [r, x])];
adamc@1236 1751 evalExp (acc :: r :: env) b k))} q
adamc@1236 1752 end))
adamc@1220 1753 | EDml e =>
adamc@1220 1754 (case parse dml e of
adamc@1220 1755 NONE => (print ("Warning: Information flow checker can't parse DML command at "
adamc@1220 1756 ^ ErrorMsg.spanToString loc ^ "\n");
adamc@1220 1757 default ())
adamc@1220 1758 | SOME d =>
adamc@1220 1759 case d of
adamc@1220 1760 Insert (tab, es) =>
adamc@1220 1761 let
adamc@1236 1762 val new = St.nextVar ()
adamc@1220 1763
adamc@1236 1764 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1765 (fn _ => raise Fail "Iflow.evalExp: Bad field expression in INSERT [1]")
adamc@1220 1766
adamc@1236 1767 val es = map (fn (x, e) =>
adamc@1236 1768 case expIn e of
adamc@1236 1769 inl e => (x, e)
adamc@1236 1770 | inr _ => raise Fail "Iflow.evalExp: Bad field expression in INSERT [2]")
adamc@1236 1771 es
adamc@1220 1772
adamc@1236 1773 val saved = St.stash ()
adamc@1220 1774 in
adamc@1236 1775 St.assert [AReln (Sql (tab ^ "$New"), [Recd es])];
adamc@1236 1776 St.insert loc;
adamc@1236 1777 St.reinstate saved;
adamc@1236 1778 k (Recd [])
adamc@1221 1779 end
adamc@1221 1780 | Delete (tab, e) =>
adamc@1221 1781 let
adamc@1236 1782 val old = St.nextVar ()
adamc@1236 1783
adamc@1236 1784 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1785 (fn "T" => Var old
adamc@1236 1786 | _ => raise Fail "Iflow.evalExp: Bad field expression in DELETE")
adamc@1221 1787
adamc@1236 1788 val p = case expIn e of
adamc@1236 1789 inl e => raise Fail "Iflow.evalExp: DELETE with non-boolean"
adamc@1236 1790 | inr p => p
adamc@1236 1791
adamc@1236 1792 val saved = St.stash ()
adamc@1221 1793 in
adamc@1236 1794 St.assert [AReln (Sql "$Old", [Var old]),
adamc@1236 1795 AReln (Sql tab, [Var old])];
adamc@1236 1796 decomp {Save = St.stash,
adamc@1236 1797 Restore = St.reinstate,
adamc@1236 1798 Add = fn a => St.assert [a]} p
adamc@1236 1799 (fn () => (St.delete loc;
adamc@1236 1800 St.reinstate saved;
adamc@1236 1801 St.havocReln (Sql tab);
adamc@1236 1802 k (Recd []))
adamc@1236 1803 handle Cc.Contradiction => ())
adamc@1223 1804 end
adamc@1223 1805 | Update (tab, fs, e) =>
adamc@1223 1806 let
adamc@1236 1807 val new = St.nextVar ()
adamc@1236 1808 val old = St.nextVar ()
adamc@1223 1809
adamc@1236 1810 val expIn = expIn (Var o St.nextVar) env
adamc@1236 1811 (fn "T" => Var old
adamc@1236 1812 | _ => raise Fail "Iflow.evalExp: Bad field expression in UPDATE")
adamc@1223 1813
adamc@1236 1814 val fs = map
adamc@1236 1815 (fn (x, e) =>
adamc@1236 1816 (x, case expIn e of
adamc@1236 1817 inl e => e
adamc@1236 1818 | inr _ => raise Fail
adamc@1236 1819 ("Iflow.evalExp: Selecting "
adamc@1236 1820 ^ "boolean expression")))
adamc@1236 1821 fs
adamc@1223 1822
adamc@1226 1823 val fs' = case SM.find (!tabs, tab) of
adamc@1224 1824 NONE => raise Fail "Iflow.evalExp: Updating unknown table"
adamc@1226 1825 | SOME (fs', _) => fs'
adamc@1224 1826
adamc@1224 1827 val fs = foldl (fn (f, fs) =>
adamc@1224 1828 if List.exists (fn (f', _) => f' = f) fs then
adamc@1224 1829 fs
adamc@1224 1830 else
adamc@1224 1831 (f, Proj (Var old, f)) :: fs) fs fs'
adamc@1224 1832
adamc@1236 1833 val p = case expIn e of
adamc@1236 1834 inl e => raise Fail "Iflow.evalExp: UPDATE with non-boolean"
adamc@1236 1835 | inr p => p
adamc@1236 1836 val saved = St.stash ()
adamc@1223 1837 in
adamc@1236 1838 St.assert [AReln (Sql (tab ^ "$New"), [Recd fs]),
adamc@1236 1839 AReln (Sql "$Old", [Var old]),
adamc@1236 1840 AReln (Sql tab, [Var old])];
adamc@1236 1841 decomp {Save = St.stash,
adamc@1236 1842 Restore = St.reinstate,
adamc@1236 1843 Add = fn a => St.assert [a]} p
adamc@1236 1844 (fn () => (St.update loc;
adamc@1236 1845 St.reinstate saved;
adamc@1236 1846 St.havocReln (Sql tab);
adamc@1236 1847 k (Recd []))
adamc@1236 1848 handle Cc.Contradiction => ())
adamc@1220 1849 end)
adamc@1220 1850
adamc@1229 1851 | ENextval (EPrim (Prim.String seq), _) =>
adamc@1229 1852 let
adamc@1236 1853 val nv = St.nextVar ()
adamc@1229 1854 in
adamc@1236 1855 St.assert [AReln (Sql (String.extract (seq, 3, NONE)), [Var nv])];
adamc@1236 1856 k (Var nv)
adamc@1229 1857 end
adamc@1200 1858 | ENextval _ => default ()
adamc@1200 1859 | ESetval _ => default ()
adamc@1200 1860
adamc@1213 1861 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
adamc@1217 1862 let
adamc@1236 1863 val nv = St.nextVar ()
adamc@1217 1864 in
adamc@1236 1865 St.assert [AReln (Known, [Var nv])];
adamc@1236 1866 k (Var nv)
adamc@1217 1867 end
adamc@1213 1868
adamc@1200 1869 | EUnurlify _ => default ()
adamc@1200 1870 | EJavaScript _ => default ()
adamc@1200 1871 | ESignalReturn _ => default ()
adamc@1200 1872 | ESignalBind _ => default ()
adamc@1200 1873 | ESignalSource _ => default ()
adamc@1200 1874 | EServerCall _ => default ()
adamc@1200 1875 | ERecv _ => default ()
adamc@1200 1876 | ESleep _ => default ()
adamc@1200 1877 | ESpawn _ => default ()
adamc@1200 1878 end
adamc@1200 1879
adamc@1200 1880 fun check file =
adamc@1200 1881 let
adamc@1236 1882 val () = St.reset ()
adamc@1236 1883
adamc@1213 1884 val file = MonoReduce.reduce file
adamc@1213 1885 val file = MonoOpt.optimize file
adamc@1213 1886 val file = Fuse.fuse file
adamc@1213 1887 val file = MonoOpt.optimize file
adamc@1216 1888 val file = MonoShake.shake file
adamc@1213 1889 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
adamc@1213 1890
adamc@1207 1891 val exptd = foldl (fn ((d, _), exptd) =>
adamc@1207 1892 case d of
adamc@1207 1893 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
adamc@1207 1894 | _ => exptd) IS.empty file
adamc@1207 1895
adamc@1236 1896 fun decl (d, _) =
adamc@1200 1897 case d of
adamc@1226 1898 DTable (tab, fs, pk, _) =>
adamc@1226 1899 let
adamc@1226 1900 val ks =
adamc@1226 1901 case #1 pk of
adamc@1226 1902 EPrim (Prim.String s) =>
adamc@1226 1903 (case String.tokens (fn ch => ch = #"," orelse ch = #" ") s of
adamc@1226 1904 [] => []
adamc@1226 1905 | pk => [pk])
adamc@1226 1906 | _ => []
adamc@1226 1907 in
adamc@1226 1908 if size tab >= 3 then
adamc@1236 1909 tabs := SM.insert (!tabs, String.extract (tab, 3, NONE),
adamc@1236 1910 (map #1 fs,
adamc@1236 1911 map (map (fn s => str (Char.toUpper (String.sub (s, 3)))
adamc@1236 1912 ^ String.extract (s, 4, NONE))) ks))
adamc@1226 1913 else
adamc@1226 1914 raise Fail "Table name does not begin with uw_"
adamc@1226 1915 end
adamc@1224 1916 | DVal (_, n, _, e, _) =>
adamc@1200 1917 let
adamc@1207 1918 val isExptd = IS.member (exptd, n)
adamc@1207 1919
adamc@1236 1920 val saved = St.stash ()
adamc@1236 1921
adamc@1236 1922 fun deAbs (e, env, ps) =
adamc@1200 1923 case #1 e of
adamc@1236 1924 EAbs (_, _, _, e) =>
adamc@1236 1925 let
adamc@1236 1926 val nv = Var (St.nextVar ())
adamc@1236 1927 in
adamc@1236 1928 deAbs (e, nv :: env,
adamc@1236 1929 if isExptd then
adamc@1236 1930 AReln (Known, [nv]) :: ps
adamc@1236 1931 else
adamc@1236 1932 ps)
adamc@1236 1933 end
adamc@1236 1934 | _ => (e, env, ps)
adamc@1200 1935
adamc@1236 1936 val (e, env, ps) = deAbs (e, [], [])
adamc@1200 1937 in
adamc@1236 1938 St.assert ps;
adamc@1236 1939 (evalExp env e (fn _ => ()) handle Cc.Contradiction => ());
adamc@1236 1940 St.reinstate saved
adamc@1200 1941 end
adamc@1202 1942
adamc@1220 1943 | DPolicy pol =>
adamc@1218 1944 let
adamc@1236 1945 val rvN = ref 0
adamc@1236 1946 fun rv () =
adamc@1236 1947 let
adamc@1236 1948 val n = !rvN
adamc@1236 1949 in
adamc@1236 1950 rvN := n + 1;
adamc@1236 1951 Lvar n
adamc@1236 1952 end
adamc@1236 1953
adamc@1236 1954 val atoms = ref ([] : atom list)
adamc@1236 1955 fun doQ k = doQuery {Env = [],
adamc@1236 1956 NextVar = rv,
adamc@1236 1957 Add = fn a => atoms := a :: !atoms,
adamc@1236 1958 Save = fn () => !atoms,
adamc@1236 1959 Restore = fn ls => atoms := ls,
adamc@1236 1960 UsedExp = fn _ => (),
adamc@1236 1961 Cont = SomeCol (fn es => k (!atoms, es))}
adamc@1218 1962 in
adamc@1220 1963 case pol of
adamc@1220 1964 PolClient e =>
adamc@1236 1965 doQ (fn (ats, es) => St.allowSend (ats, es)) e
adamc@1220 1966 | PolInsert e =>
adamc@1236 1967 doQ (fn (ats, _) => St.allowInsert ats) e
adamc@1221 1968 | PolDelete e =>
adamc@1236 1969 doQ (fn (ats, _) => St.allowDelete ats) e
adamc@1223 1970 | PolUpdate e =>
adamc@1236 1971 doQ (fn (ats, _) => St.allowUpdate ats) e
adamc@1229 1972 | PolSequence e =>
adamc@1229 1973 (case #1 e of
adamc@1229 1974 EPrim (Prim.String seq) =>
adamc@1229 1975 let
adamc@1236 1976 val p = AReln (Sql (String.extract (seq, 3, NONE)), [Lvar 0])
adamc@1229 1977 val outs = [Lvar 0]
adamc@1229 1978 in
adamc@1236 1979 St.allowSend ([p], outs)
adamc@1229 1980 end
adamc@1236 1981 | _ => ())
adamc@1218 1982 end
adamc@1214 1983
adamc@1236 1984 | _ => ()
adamc@1200 1985 in
adamc@1236 1986 app decl file
adamc@1200 1987 end
adamc@1200 1988
adamc@1213 1989 val check = fn file =>
adamc@1213 1990 let
adamc@1213 1991 val oldInline = Settings.getMonoInline ()
adamc@1213 1992 in
adamc@1213 1993 (Settings.setMonoInline (case Int.maxInt of
adamc@1213 1994 NONE => 1000000
adamc@1213 1995 | SOME n => n);
adamc@1213 1996 check file;
adamc@1213 1997 Settings.setMonoInline oldInline)
adamc@1213 1998 handle ex => (Settings.setMonoInline oldInline;
adamc@1213 1999 raise ex)
adamc@1213 2000 end
adamc@1213 2001
adamc@1200 2002 end