annotate src/iflow.sml @ 1224:3950cf1f5736

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