annotate src/iflow.sml @ 1229:a2cd6664f57f

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