annotate src/iflow.sml @ 1250:e80582b927f2

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