annotate src/iflow.sml @ 1226:df5bd4115267

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