annotate src/iflow.sml @ 1221:00e628854005

Delete policies
author Adam Chlipala <adamc@hcoop.net>
date Sun, 11 Apr 2010 12:38:21 -0400
parents 526575a9537a
children 117f13bdc1fd
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@1218 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@1220 1260
adamc@1220 1261 val insert = log "insert"
adamc@1220 1262 (wrapP (follow (const "INSERT INTO ")
adamc@1220 1263 (follow uw_ident
adamc@1220 1264 (follow (const " (")
adamc@1220 1265 (follow (list uw_ident)
adamc@1220 1266 (follow (const ") VALUES (")
adamc@1220 1267 (follow (list sqexp)
adamc@1220 1268 (const ")")))))))
adamc@1220 1269 (fn ((), (tab, ((), (fs, ((), (es, ())))))) =>
adamc@1221 1270 (SOME (tab, ListPair.zipEq (fs, es)))
adamc@1220 1271 handle ListPair.UnequalLengths => NONE))
adamc@1220 1272
adamc@1221 1273 val delete = log "delete"
adamc@1221 1274 (wrap (follow (const "DELETE FROM ")
adamc@1221 1275 (follow uw_ident
adamc@1221 1276 (follow (const " AS T_T WHERE ")
adamc@1221 1277 sqexp)))
adamc@1221 1278 (fn ((), (tab, ((), es))) => (tab, es)))
adamc@1221 1279
adamc@1220 1280 val dml = log "dml"
adamc@1221 1281 (altL [wrap insert Insert,
adamc@1221 1282 wrap delete Delete])
adamc@1220 1283
adamc@1215 1284 fun removeDups (ls : (string * string) list) =
adamc@1211 1285 case ls of
adamc@1211 1286 [] => []
adamc@1211 1287 | x :: ls =>
adamc@1211 1288 let
adamc@1211 1289 val ls = removeDups ls
adamc@1211 1290 in
adamc@1211 1291 if List.exists (fn x' => x' = x) ls then
adamc@1211 1292 ls
adamc@1211 1293 else
adamc@1211 1294 x :: ls
adamc@1211 1295 end
adamc@1211 1296
adamc@1212 1297 datatype queryMode =
adamc@1218 1298 SomeCol
adamc@1212 1299 | AllCols of exp
adamc@1212 1300
adamc@1220 1301 fun expIn rv env rvOf =
adamc@1220 1302 let
adamc@1220 1303 fun expIn (e, rvN) =
adamc@1220 1304 let
adamc@1220 1305 fun default () =
adamc@1220 1306 let
adamc@1220 1307 val (rvN, e) = rv rvN
adamc@1220 1308 in
adamc@1220 1309 (inl e, rvN)
adamc@1220 1310 end
adamc@1220 1311 in
adamc@1220 1312 case e of
adamc@1220 1313 SqConst p => (inl (Const p), rvN)
adamc@1220 1314 | Field (v, f) => (inl (Proj (rvOf v, f)), rvN)
adamc@1220 1315 | Binop (bo, e1, e2) =>
adamc@1220 1316 let
adamc@1220 1317 val (e1, rvN) = expIn (e1, rvN)
adamc@1220 1318 val (e2, rvN) = expIn (e2, rvN)
adamc@1220 1319 in
adamc@1220 1320 (inr (case (bo, e1, e2) of
adamc@1220 1321 (Exps f, inl e1, inl e2) => f (e1, e2)
adamc@1220 1322 | (Props f, inr p1, inr p2) => f (p1, p2)
adamc@1220 1323 | _ => Unknown), rvN)
adamc@1220 1324 end
adamc@1220 1325 | SqKnown e =>
adamc@1220 1326 (case expIn (e, rvN) of
adamc@1220 1327 (inl e, rvN) => (inr (Reln (Known, [e])), rvN)
adamc@1220 1328 | _ => (inr Unknown, rvN))
adamc@1220 1329 | Inj e =>
adamc@1220 1330 let
adamc@1220 1331 fun deinj e =
adamc@1220 1332 case #1 e of
adamc@1220 1333 ERel n => (List.nth (env, n), rvN)
adamc@1220 1334 | EField (e, f) =>
adamc@1220 1335 let
adamc@1220 1336 val (e, rvN) = deinj e
adamc@1220 1337 in
adamc@1220 1338 (Proj (e, f), rvN)
adamc@1220 1339 end
adamc@1220 1340 | _ =>
adamc@1220 1341 let
adamc@1220 1342 val (rvN, e) = rv rvN
adamc@1220 1343 in
adamc@1220 1344 (e, rvN)
adamc@1220 1345 end
adamc@1220 1346
adamc@1220 1347 val (e, rvN) = deinj e
adamc@1220 1348 in
adamc@1220 1349 (inl e, rvN)
adamc@1220 1350 end
adamc@1220 1351 | SqFunc (f, e) =>
adamc@1220 1352 (case expIn (e, rvN) of
adamc@1220 1353 (inl e, rvN) => (inl (Func (Other f, [e])), rvN)
adamc@1220 1354 | _ => default ())
adamc@1220 1355
adamc@1220 1356 | Count => default ()
adamc@1220 1357 end
adamc@1220 1358 in
adamc@1220 1359 expIn
adamc@1220 1360 end
adamc@1216 1361
adamc@1214 1362 fun queryProp env rvN rv oe e =
adamc@1216 1363 let
adamc@1216 1364 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
adamc@1216 1365 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
adamc@1218 1366 (rvN, Unknown, Unknown, [], []))
adamc@1216 1367 in
adamc@1216 1368 case parse query e of
adamc@1216 1369 NONE => default ()
adamc@1216 1370 | SOME r =>
adamc@1216 1371 let
adamc@1216 1372 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
adamc@1216 1373 let
adamc@1216 1374 val (rvN, e) = rv rvN
adamc@1216 1375 in
adamc@1216 1376 ((v, e), rvN)
adamc@1216 1377 end) rvN (#From r)
adamc@1214 1378
adamc@1216 1379 fun rvOf v =
adamc@1216 1380 case List.find (fn (v', _) => v' = v) rvs of
adamc@1216 1381 NONE => raise Fail "Iflow.queryProp: Bad table variable"
adamc@1216 1382 | SOME (_, e) => e
adamc@1214 1383
adamc@1216 1384 fun usedFields e =
adamc@1216 1385 case e of
adamc@1216 1386 SqConst _ => []
adamc@1216 1387 | Field (v, f) => [(v, f)]
adamc@1216 1388 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
adamc@1216 1389 | SqKnown _ => []
adamc@1216 1390 | Inj _ => []
adamc@1216 1391 | SqFunc (_, e) => usedFields e
adamc@1216 1392 | Count => []
adamc@1214 1393
adamc@1216 1394 val p =
adamc@1216 1395 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
adamc@1211 1396
adamc@1220 1397 val expIn = expIn rv env rvOf
adamc@1205 1398
adamc@1220 1399 val (p, rvN) = case #Where r of
adamc@1220 1400 NONE => (p, rvN)
adamc@1220 1401 | SOME e =>
adamc@1220 1402 case expIn (e, rvN) of
adamc@1220 1403 (inr p', rvN) => (And (p, p'), rvN)
adamc@1220 1404 | _ => (p, rvN)
adamc@1210 1405
adamc@1216 1406 fun normal () =
adamc@1218 1407 case oe of
adamc@1218 1408 SomeCol =>
adamc@1220 1409 let
adamc@1220 1410 val (sis, rvN) =
adamc@1220 1411 ListUtil.foldlMap
adamc@1220 1412 (fn (si, rvN) =>
adamc@1220 1413 case si of
adamc@1220 1414 SqField (v, f) => (Proj (rvOf v, f), rvN)
adamc@1220 1415 | SqExp (e, f) =>
adamc@1220 1416 case expIn (e, rvN) of
adamc@1220 1417 (inr _, _) =>
adamc@1220 1418 let
adamc@1220 1419 val (rvN, e) = rv rvN
adamc@1220 1420 in
adamc@1220 1421 (e, rvN)
adamc@1220 1422 end
adamc@1220 1423 | (inl e, rvN) => (e, rvN)) rvN (#Select r)
adamc@1220 1424 in
adamc@1220 1425 (rvN, p, True, sis)
adamc@1220 1426 end
adamc@1218 1427 | AllCols oe =>
adamc@1220 1428 let
adamc@1220 1429 val (p', rvN) =
adamc@1220 1430 foldl (fn (si, (p, rvN)) =>
adamc@1220 1431 let
adamc@1220 1432 val (p', rvN) =
adamc@1220 1433 case si of
adamc@1220 1434 SqField (v, f) => (Reln (Eq, [Proj (Proj (oe, v), f),
adamc@1220 1435 Proj (rvOf v, f)]), rvN)
adamc@1220 1436 | SqExp (e, f) =>
adamc@1220 1437 case expIn (e, rvN) of
adamc@1220 1438 (inr p, rvN) => (Cond (Proj (oe, f), p), rvN)
adamc@1220 1439 | (inl e, rvN) => (Reln (Eq, [Proj (oe, f), e]), rvN)
adamc@1220 1440 in
adamc@1220 1441 (And (p, p'), rvN)
adamc@1220 1442 end)
adamc@1220 1443 (True, rvN) (#Select r)
adamc@1220 1444 in
adamc@1220 1445 (rvN, And (p, p'), True, [])
adamc@1220 1446 end
adamc@1217 1447
adamc@1218 1448 val (rvN, p, wp, outs) =
adamc@1217 1449 case #Select r of
adamc@1217 1450 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
adamc@1217 1451 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
adamc@1217 1452 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
adamc@1218 1453 (case oe of
adamc@1218 1454 SomeCol =>
adamc@1218 1455 let
adamc@1218 1456 val (rvN, oe) = rv rvN
adamc@1218 1457 in
adamc@1218 1458 (rvN,
adamc@1218 1459 Or (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.False", [])]),
adamc@1218 1460 And (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
adamc@1218 1461 p)),
adamc@1218 1462 Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
adamc@1218 1463 [oe])
adamc@1218 1464 end
adamc@1218 1465 | AllCols oe =>
adamc@1218 1466 let
adamc@1221 1467 fun oeEq e = Reln (Eq, [oe, Recd [(f, e)]])
adamc@1218 1468 in
adamc@1218 1469 (rvN,
adamc@1221 1470 Or (oeEq (Func (DtCon0 "Basis.bool.False", [])),
adamc@1221 1471 And (oeEq (Func (DtCon0 "Basis.bool.True", [])),
adamc@1218 1472 p)),
adamc@1221 1473 oeEq (Func (DtCon0 "Basis.bool.True", [])),
adamc@1218 1474 [])
adamc@1218 1475 end)
adamc@1217 1476 | _ => normal ())
adamc@1217 1477 | _ => normal ()
adamc@1216 1478 in
adamc@1217 1479 (rvN, p, wp, case #Where r of
adamc@1217 1480 NONE => []
adamc@1218 1481 | SOME e => map (fn (v, f) => Proj (rvOf v, f)) (usedFields e), outs)
adamc@1216 1482 end
adamc@1220 1483 end
adamc@1220 1484
adamc@1220 1485 fun insertProp rvN rv e =
adamc@1220 1486 let
adamc@1220 1487 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
adamc@1220 1488 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
adamc@1220 1489 Unknown)
adamc@1220 1490 in
adamc@1220 1491 case parse query e of
adamc@1220 1492 NONE => default ()
adamc@1220 1493 | SOME r =>
adamc@1220 1494 let
adamc@1220 1495 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
adamc@1220 1496 let
adamc@1220 1497 val (rvN, e) = rv rvN
adamc@1220 1498 in
adamc@1220 1499 ((v, e), rvN)
adamc@1220 1500 end) rvN (#From r)
adamc@1220 1501
adamc@1220 1502 fun rvOf v =
adamc@1220 1503 case List.find (fn (v', _) => v' = v) rvs of
adamc@1220 1504 NONE => raise Fail "Iflow.insertProp: Bad table variable"
adamc@1220 1505 | SOME (_, e) => e
adamc@1220 1506
adamc@1220 1507 val p =
adamc@1220 1508 foldl (fn ((t, v), p) =>
adamc@1220 1509 let
adamc@1220 1510 val t =
adamc@1220 1511 case v of
adamc@1220 1512 "New" => "$New"
adamc@1220 1513 | _ => t
adamc@1220 1514 in
adamc@1220 1515 And (p, Reln (Sql t, [rvOf v]))
adamc@1220 1516 end) True (#From r)
adamc@1220 1517
adamc@1220 1518 val expIn = expIn rv [] rvOf
adamc@1220 1519 in
adamc@1220 1520 case #Where r of
adamc@1220 1521 NONE => p
adamc@1220 1522 | SOME e =>
adamc@1220 1523 case expIn (e, rvN) of
adamc@1220 1524 (inr p', _) => And (p, p')
adamc@1220 1525 | _ => p
adamc@1220 1526 end
adamc@1216 1527 end
adamc@1200 1528
adamc@1221 1529 fun deleteProp rvN rv e =
adamc@1221 1530 let
adamc@1221 1531 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
adamc@1221 1532 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
adamc@1221 1533 Unknown)
adamc@1221 1534 in
adamc@1221 1535 case parse query e of
adamc@1221 1536 NONE => default ()
adamc@1221 1537 | SOME r =>
adamc@1221 1538 let
adamc@1221 1539 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
adamc@1221 1540 let
adamc@1221 1541 val (rvN, e) = rv rvN
adamc@1221 1542 in
adamc@1221 1543 ((v, e), rvN)
adamc@1221 1544 end) rvN (#From r)
adamc@1221 1545
adamc@1221 1546 fun rvOf v =
adamc@1221 1547 case List.find (fn (v', _) => v' = v) rvs of
adamc@1221 1548 NONE => raise Fail "Iflow.deleteProp: Bad table variable"
adamc@1221 1549 | SOME (_, e) => e
adamc@1221 1550
adamc@1221 1551 val p =
adamc@1221 1552 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
adamc@1221 1553
adamc@1221 1554 val expIn = expIn rv [] rvOf
adamc@1221 1555 in
adamc@1221 1556 And (Reln (Sql "$Old", [rvOf "Old"]),
adamc@1221 1557 case #Where r of
adamc@1221 1558 NONE => p
adamc@1221 1559 | SOME e =>
adamc@1221 1560 case expIn (e, rvN) of
adamc@1221 1561 (inr p', _) => And (p, p')
adamc@1221 1562 | _ => p)
adamc@1221 1563 end
adamc@1221 1564 end
adamc@1221 1565
adamc@1211 1566 fun evalPat env e (pt, _) =
adamc@1211 1567 case pt of
adamc@1211 1568 PWild => (env, True)
adamc@1211 1569 | PVar _ => (e :: env, True)
adamc@1211 1570 | PPrim _ => (env, True)
adamc@1215 1571 | PCon (_, pc, NONE) => (env, Reln (PCon0 (patCon pc), [e]))
adamc@1211 1572 | PCon (_, pc, SOME pt) =>
adamc@1211 1573 let
adamc@1215 1574 val (env, p) = evalPat env (Func (UnCon (patCon pc), [e])) pt
adamc@1211 1575 in
adamc@1215 1576 (env, And (p, Reln (PCon1 (patCon pc), [e])))
adamc@1211 1577 end
adamc@1211 1578 | PRecord xpts =>
adamc@1211 1579 foldl (fn ((x, pt, _), (env, p)) =>
adamc@1211 1580 let
adamc@1211 1581 val (env, p') = evalPat env (Proj (e, x)) pt
adamc@1211 1582 in
adamc@1211 1583 (env, And (p', p))
adamc@1211 1584 end) (env, True) xpts
adamc@1215 1585 | PNone _ => (env, Reln (PCon0 "None", [e]))
adamc@1211 1586 | PSome (_, pt) =>
adamc@1211 1587 let
adamc@1215 1588 val (env, p) = evalPat env (Func (UnCon "Some", [e])) pt
adamc@1211 1589 in
adamc@1215 1590 (env, And (p, Reln (PCon1 "Some", [e])))
adamc@1211 1591 end
adamc@1211 1592
adamc@1211 1593 fun peq (p1, p2) =
adamc@1211 1594 case (p1, p2) of
adamc@1211 1595 (True, True) => true
adamc@1211 1596 | (False, False) => true
adamc@1211 1597 | (Unknown, Unknown) => true
adamc@1211 1598 | (And (x1, y1), And (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
adamc@1211 1599 | (Or (x1, y1), Or (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
adamc@1211 1600 | (Reln (r1, es1), Reln (r2, es2)) => r1 = r2 andalso ListPair.allEq eeq (es1, es2)
adamc@1212 1601 | (Cond (e1, p1), Cond (e2, p2)) => eeq (e1, e2) andalso peq (p1, p2)
adamc@1211 1602 | _ => false
adamc@1211 1603
adamc@1211 1604 fun removeRedundant p1 =
adamc@1211 1605 let
adamc@1211 1606 fun rr p2 =
adamc@1211 1607 if peq (p1, p2) then
adamc@1211 1608 True
adamc@1211 1609 else
adamc@1211 1610 case p2 of
adamc@1211 1611 And (x, y) => And (rr x, rr y)
adamc@1211 1612 | Or (x, y) => Or (rr x, rr y)
adamc@1211 1613 | _ => p2
adamc@1211 1614 in
adamc@1211 1615 rr
adamc@1211 1616 end
adamc@1211 1617
adamc@1218 1618 datatype cflow = Case | Where
adamc@1218 1619 datatype flow = Data | Control of cflow
adamc@1218 1620 type check = ErrorMsg.span * exp * prop
adamc@1221 1621 type dml = ErrorMsg.span * prop
adamc@1218 1622
adamc@1217 1623 structure St :> sig
adamc@1217 1624 type t
adamc@1217 1625 val create : {Var : int,
adamc@1217 1626 Ambient : prop} -> t
adamc@1217 1627
adamc@1217 1628 val curVar : t -> int
adamc@1217 1629 val nextVar : t -> t * int
adamc@1217 1630
adamc@1217 1631 val ambient : t -> prop
adamc@1217 1632 val setAmbient : t * prop -> t
adamc@1217 1633
adamc@1218 1634 val paths : t -> (check * cflow) list
adamc@1218 1635 val addPath : t * (check * cflow) -> t
adamc@1218 1636 val addPaths : t * (check * cflow) list -> t
adamc@1218 1637 val clearPaths : t -> t
adamc@1218 1638 val setPaths : t * (check * cflow) list -> t
adamc@1217 1639
adamc@1218 1640 val sent : t -> (check * flow) list
adamc@1218 1641 val addSent : t * (check * flow) -> t
adamc@1218 1642 val setSent : t * (check * flow) list -> t
adamc@1220 1643
adamc@1221 1644 val inserted : t -> dml list
adamc@1221 1645 val addInsert : t * dml -> t
adamc@1221 1646
adamc@1221 1647 val deleted : t -> dml list
adamc@1221 1648 val addDelete : t * dml -> t
adamc@1217 1649 end = struct
adamc@1217 1650
adamc@1217 1651 type t = {Var : int,
adamc@1217 1652 Ambient : prop,
adamc@1218 1653 Path : (check * cflow) list,
adamc@1220 1654 Sent : (check * flow) list,
adamc@1221 1655 Insert : dml list,
adamc@1221 1656 Delete : dml list}
adamc@1217 1657
adamc@1217 1658 fun create {Var = v, Ambient = p} = {Var = v,
adamc@1217 1659 Ambient = p,
adamc@1217 1660 Path = [],
adamc@1220 1661 Sent = [],
adamc@1221 1662 Insert = [],
adamc@1221 1663 Delete = []}
adamc@1217 1664
adamc@1217 1665 fun curVar (t : t) = #Var t
adamc@1217 1666 fun nextVar (t : t) = ({Var = #Var t + 1,
adamc@1217 1667 Ambient = #Ambient t,
adamc@1217 1668 Path = #Path t,
adamc@1220 1669 Sent = #Sent t,
adamc@1221 1670 Insert = #Insert t,
adamc@1221 1671 Delete = #Delete t}, #Var t)
adamc@1217 1672
adamc@1217 1673 fun ambient (t : t) = #Ambient t
adamc@1217 1674 fun setAmbient (t : t, p) = {Var = #Var t,
adamc@1217 1675 Ambient = p,
adamc@1217 1676 Path = #Path t,
adamc@1220 1677 Sent = #Sent t,
adamc@1221 1678 Insert = #Insert t,
adamc@1221 1679 Delete = #Delete t}
adamc@1217 1680
adamc@1218 1681 fun paths (t : t) = #Path t
adamc@1217 1682 fun addPath (t : t, c) = {Var = #Var t,
adamc@1217 1683 Ambient = #Ambient t,
adamc@1217 1684 Path = c :: #Path t,
adamc@1220 1685 Sent = #Sent t,
adamc@1221 1686 Insert = #Insert t,
adamc@1221 1687 Delete = #Delete t}
adamc@1218 1688 fun addPaths (t : t, cs) = {Var = #Var t,
adamc@1218 1689 Ambient = #Ambient t,
adamc@1218 1690 Path = cs @ #Path t,
adamc@1220 1691 Sent = #Sent t,
adamc@1221 1692 Insert = #Insert t,
adamc@1221 1693 Delete = #Delete t}
adamc@1218 1694 fun clearPaths (t : t) = {Var = #Var t,
adamc@1218 1695 Ambient = #Ambient t,
adamc@1218 1696 Path = [],
adamc@1220 1697 Sent = #Sent t,
adamc@1221 1698 Insert = #Insert t,
adamc@1221 1699 Delete = #Delete t}
adamc@1218 1700 fun setPaths (t : t, cs) = {Var = #Var t,
adamc@1218 1701 Ambient = #Ambient t,
adamc@1218 1702 Path = cs,
adamc@1220 1703 Sent = #Sent t,
adamc@1221 1704 Insert = #Insert t,
adamc@1221 1705 Delete = #Delete t}
adamc@1217 1706
adamc@1217 1707 fun sent (t : t) = #Sent t
adamc@1217 1708 fun addSent (t : t, c) = {Var = #Var t,
adamc@1217 1709 Ambient = #Ambient t,
adamc@1217 1710 Path = #Path t,
adamc@1220 1711 Sent = c :: #Sent t,
adamc@1221 1712 Insert = #Insert t,
adamc@1221 1713 Delete = #Delete t}
adamc@1217 1714 fun setSent (t : t, cs) = {Var = #Var t,
adamc@1217 1715 Ambient = #Ambient t,
adamc@1217 1716 Path = #Path t,
adamc@1220 1717 Sent = cs,
adamc@1221 1718 Insert = #Insert t,
adamc@1221 1719 Delete = #Delete t}
adamc@1220 1720
adamc@1220 1721 fun inserted (t : t) = #Insert t
adamc@1220 1722 fun addInsert (t : t, c) = {Var = #Var t,
adamc@1220 1723 Ambient = #Ambient t,
adamc@1220 1724 Path = #Path t,
adamc@1220 1725 Sent = #Sent t,
adamc@1221 1726 Insert = c :: #Insert t,
adamc@1221 1727 Delete = #Delete t}
adamc@1221 1728
adamc@1221 1729 fun deleted (t : t) = #Delete t
adamc@1221 1730 fun addDelete (t : t, c) = {Var = #Var t,
adamc@1221 1731 Ambient = #Ambient t,
adamc@1221 1732 Path = #Path t,
adamc@1221 1733 Sent = #Sent t,
adamc@1221 1734 Insert = #Insert t,
adamc@1221 1735 Delete = c :: #Delete t}
adamc@1217 1736
adamc@1217 1737 end
adamc@1217 1738
adamc@1217 1739 fun evalExp env (e as (_, loc), st) =
adamc@1200 1740 let
adamc@1200 1741 fun default () =
adamc@1217 1742 let
adamc@1217 1743 val (st, nv) = St.nextVar st
adamc@1217 1744 in
adamc@1217 1745 (Var nv, st)
adamc@1217 1746 end
adamc@1200 1747
adamc@1217 1748 fun addSent (p, e, st) =
adamc@1218 1749 let
adamc@1218 1750 val st = if isKnown e then
adamc@1218 1751 st
adamc@1218 1752 else
adamc@1218 1753 St.addSent (st, ((loc, e, p), Data))
adamc@1218 1754
adamc@1218 1755 val st = foldl (fn ((c, fl), st) => St.addSent (st, (c, Control fl))) st (St.paths st)
adamc@1218 1756 in
adamc@1218 1757 St.clearPaths st
adamc@1218 1758 end
adamc@1200 1759 in
adamc@1200 1760 case #1 e of
adamc@1200 1761 EPrim p => (Const p, st)
adamc@1200 1762 | ERel n => (List.nth (env, n), st)
adamc@1200 1763 | ENamed _ => default ()
adamc@1215 1764 | ECon (_, pc, NONE) => (Func (DtCon0 (patCon pc), []), st)
adamc@1200 1765 | ECon (_, pc, SOME e) =>
adamc@1200 1766 let
adamc@1200 1767 val (e, st) = evalExp env (e, st)
adamc@1200 1768 in
adamc@1215 1769 (Func (DtCon1 (patCon pc), [e]), st)
adamc@1200 1770 end
adamc@1215 1771 | ENone _ => (Func (DtCon0 "None", []), st)
adamc@1200 1772 | ESome (_, e) =>
adamc@1200 1773 let
adamc@1200 1774 val (e, st) = evalExp env (e, st)
adamc@1200 1775 in
adamc@1215 1776 (Func (DtCon1 "Some", [e]), st)
adamc@1200 1777 end
adamc@1200 1778 | EFfi _ => default ()
adamc@1213 1779
adamc@1200 1780 | EFfiApp (m, s, es) =>
adamc@1200 1781 if m = "Basis" andalso SS.member (writers, s) then
adamc@1200 1782 let
adamc@1200 1783 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1784 in
adamc@1217 1785 (Recd [], foldl (fn (e, st) => addSent (St.ambient st, e, st)) st es)
adamc@1200 1786 end
adamc@1200 1787 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
adamc@1200 1788 default ()
adamc@1200 1789 else
adamc@1200 1790 let
adamc@1200 1791 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1792 in
adamc@1215 1793 (Func (Other (m ^ "." ^ s), es), st)
adamc@1200 1794 end
adamc@1213 1795
adamc@1213 1796 | EApp (e1, e2) =>
adamc@1213 1797 let
adamc@1213 1798 val (e1, st) = evalExp env (e1, st)
adamc@1213 1799 in
adamc@1213 1800 case e1 of
adamc@1213 1801 Finish => (Finish, st)
adamc@1213 1802 | _ => default ()
adamc@1213 1803 end
adamc@1213 1804
adamc@1200 1805 | EAbs _ => default ()
adamc@1200 1806 | EUnop (s, e1) =>
adamc@1200 1807 let
adamc@1200 1808 val (e1, st) = evalExp env (e1, st)
adamc@1200 1809 in
adamc@1215 1810 (Func (Other s, [e1]), st)
adamc@1200 1811 end
adamc@1200 1812 | EBinop (s, e1, e2) =>
adamc@1200 1813 let
adamc@1200 1814 val (e1, st) = evalExp env (e1, st)
adamc@1200 1815 val (e2, st) = evalExp env (e2, st)
adamc@1200 1816 in
adamc@1215 1817 (Func (Other s, [e1, e2]), st)
adamc@1200 1818 end
adamc@1200 1819 | ERecord xets =>
adamc@1200 1820 let
adamc@1200 1821 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
adamc@1200 1822 let
adamc@1200 1823 val (e, st) = evalExp env (e, st)
adamc@1200 1824 in
adamc@1200 1825 ((x, e), st)
adamc@1200 1826 end) st xets
adamc@1200 1827 in
adamc@1200 1828 (Recd xes, st)
adamc@1200 1829 end
adamc@1200 1830 | EField (e, s) =>
adamc@1200 1831 let
adamc@1200 1832 val (e, st) = evalExp env (e, st)
adamc@1200 1833 in
adamc@1200 1834 (Proj (e, s), st)
adamc@1200 1835 end
adamc@1218 1836 | ECase (e, pes, {result = res, ...}) =>
adamc@1211 1837 let
adamc@1211 1838 val (e, st) = evalExp env (e, st)
adamc@1217 1839 val (st, r) = St.nextVar st
adamc@1217 1840 val orig = St.ambient st
adamc@1218 1841 val origPaths = St.paths st
adamc@1211 1842
adamc@1218 1843 val st = St.addPath (st, ((loc, e, orig), Case))
adamc@1218 1844
adamc@1218 1845 val (st, paths) =
adamc@1218 1846 foldl (fn ((pt, pe), (st, paths)) =>
adamc@1218 1847 let
adamc@1218 1848 val (env, pp) = evalPat env e pt
adamc@1218 1849 val (pe, st') = evalExp env (pe, St.setAmbient (st, And (orig, pp)))
adamc@1218 1850
adamc@1218 1851 val this = And (removeRedundant orig (St.ambient st'),
adamc@1218 1852 Reln (Eq, [Var r, pe]))
adamc@1218 1853 in
adamc@1218 1854 (St.setPaths (St.setAmbient (st', Or (St.ambient st, this)), origPaths),
adamc@1218 1855 St.paths st' @ paths)
adamc@1218 1856 end) (St.setAmbient (st, False), []) pes
adamc@1218 1857
adamc@1218 1858 val st = case #1 res of
adamc@1218 1859 TRecord [] => St.setPaths (st, origPaths)
adamc@1218 1860 | _ => St.setPaths (st, paths)
adamc@1211 1861 in
adamc@1217 1862 (Var r, St.setAmbient (st, And (orig, St.ambient st)))
adamc@1211 1863 end
adamc@1200 1864 | EStrcat (e1, e2) =>
adamc@1200 1865 let
adamc@1200 1866 val (e1, st) = evalExp env (e1, st)
adamc@1200 1867 val (e2, st) = evalExp env (e2, st)
adamc@1200 1868 in
adamc@1215 1869 (Func (Other "cat", [e1, e2]), st)
adamc@1200 1870 end
adamc@1200 1871 | EError _ => (Finish, st)
adamc@1200 1872 | EReturnBlob {blob = b, mimeType = m, ...} =>
adamc@1200 1873 let
adamc@1200 1874 val (b, st) = evalExp env (b, st)
adamc@1200 1875 val (m, st) = evalExp env (m, st)
adamc@1200 1876 in
adamc@1217 1877 (Finish, addSent (St.ambient st, b, addSent (St.ambient st, m, st)))
adamc@1200 1878 end
adamc@1200 1879 | ERedirect (e, _) =>
adamc@1200 1880 let
adamc@1200 1881 val (e, st) = evalExp env (e, st)
adamc@1200 1882 in
adamc@1217 1883 (Finish, addSent (St.ambient st, e, st))
adamc@1200 1884 end
adamc@1200 1885 | EWrite e =>
adamc@1200 1886 let
adamc@1200 1887 val (e, st) = evalExp env (e, st)
adamc@1200 1888 in
adamc@1217 1889 (Recd [], addSent (St.ambient st, e, st))
adamc@1200 1890 end
adamc@1200 1891 | ESeq (e1, e2) =>
adamc@1200 1892 let
adamc@1200 1893 val (_, st) = evalExp env (e1, st)
adamc@1200 1894 in
adamc@1200 1895 evalExp env (e2, st)
adamc@1200 1896 end
adamc@1200 1897 | ELet (_, _, e1, e2) =>
adamc@1200 1898 let
adamc@1200 1899 val (e1, st) = evalExp env (e1, st)
adamc@1200 1900 in
adamc@1200 1901 evalExp (e1 :: env) (e2, st)
adamc@1200 1902 end
adamc@1200 1903 | EClosure (n, es) =>
adamc@1200 1904 let
adamc@1200 1905 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1906 in
adamc@1215 1907 (Func (Other ("Cl" ^ Int.toString n), es), st)
adamc@1200 1908 end
adamc@1200 1909
adamc@1200 1910 | EQuery {query = q, body = b, initial = i, ...} =>
adamc@1200 1911 let
adamc@1200 1912 val (_, st) = evalExp env (q, st)
adamc@1200 1913 val (i, st) = evalExp env (i, st)
adamc@1200 1914
adamc@1217 1915 val (st', r) = St.nextVar st
adamc@1217 1916 val (st', acc) = St.nextVar st'
adamc@1200 1917
adamc@1200 1918 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
adamc@1200 1919
adamc@1218 1920 val (st', qp, qwp, used, _) =
adamc@1214 1921 queryProp env
adamc@1217 1922 st' (fn st' =>
adamc@1217 1923 let
adamc@1217 1924 val (st', rv) = St.nextVar st'
adamc@1217 1925 in
adamc@1217 1926 (st', Var rv)
adamc@1217 1927 end)
adamc@1214 1928 (AllCols (Var r)) q
adamc@1200 1929
adamc@1217 1930 val p' = And (qp, St.ambient st')
adamc@1200 1931
adamc@1217 1932 val (st, res) = if varInP acc (St.ambient st') then
adamc@1217 1933 let
adamc@1217 1934 val (st, r) = St.nextVar st
adamc@1217 1935 in
adamc@1217 1936 (st, Var r)
adamc@1217 1937 end
adamc@1217 1938 else
adamc@1217 1939 let
adamc@1217 1940 val (st, out) = St.nextVar st'
adamc@1217 1941
adamc@1217 1942 val p = Or (Reln (Eq, [Var out, i]),
adamc@1217 1943 And (Reln (Eq, [Var out, b]),
adamc@1217 1944 p'))
adamc@1217 1945 in
adamc@1217 1946 (St.setAmbient (st, p), Var out)
adamc@1217 1947 end
adamc@1210 1948
adamc@1218 1949 val sent = map (fn ((loc, e, p), fl) => ((loc, e, And (qp, p)), fl)) (St.sent st')
adamc@1212 1950
adamc@1217 1951 val p' = And (p', qwp)
adamc@1218 1952 val paths = map (fn e => ((loc, e, p'), Where)) used
adamc@1200 1953 in
adamc@1218 1954 (res, St.addPaths (St.setSent (st, sent), paths))
adamc@1200 1955 end
adamc@1220 1956 | EDml e =>
adamc@1220 1957 (case parse dml e of
adamc@1220 1958 NONE => (print ("Warning: Information flow checker can't parse DML command at "
adamc@1220 1959 ^ ErrorMsg.spanToString loc ^ "\n");
adamc@1220 1960 default ())
adamc@1220 1961 | SOME d =>
adamc@1220 1962 case d of
adamc@1220 1963 Insert (tab, es) =>
adamc@1220 1964 let
adamc@1220 1965 val (st, new) = St.nextVar st
adamc@1220 1966
adamc@1220 1967 fun rv st =
adamc@1220 1968 let
adamc@1220 1969 val (st, n) = St.nextVar st
adamc@1220 1970 in
adamc@1220 1971 (st, Var n)
adamc@1220 1972 end
adamc@1220 1973
adamc@1220 1974 val expIn = expIn rv env (fn "New" => Var new
adamc@1221 1975 | _ => raise Fail "Iflow.evalExp: Bad field expression in UPDATE")
adamc@1220 1976
adamc@1220 1977 val (es, st) = ListUtil.foldlMap
adamc@1220 1978 (fn ((x, e), st) =>
adamc@1220 1979 let
adamc@1220 1980 val (e, st) = case expIn (e, st) of
adamc@1220 1981 (inl e, st) => (e, st)
adamc@1220 1982 | (inr _, _) => raise Fail
adamc@1220 1983 ("Iflow.evalExp: Selecting "
adamc@1220 1984 ^ "boolean expression")
adamc@1220 1985 in
adamc@1220 1986 ((x, e), st)
adamc@1220 1987 end)
adamc@1220 1988 st es
adamc@1220 1989 in
adamc@1220 1990 (Recd [], St.addInsert (st, (loc, And (St.ambient st,
adamc@1220 1991 Reln (Sql "$New", [Recd es])))))
adamc@1221 1992 end
adamc@1221 1993 | Delete (tab, e) =>
adamc@1221 1994 let
adamc@1221 1995 val (st, old) = St.nextVar st
adamc@1221 1996
adamc@1221 1997 fun rv st =
adamc@1221 1998 let
adamc@1221 1999 val (st, n) = St.nextVar st
adamc@1221 2000 in
adamc@1221 2001 (st, Var n)
adamc@1221 2002 end
adamc@1221 2003
adamc@1221 2004 val expIn = expIn rv env (fn "T" => Var old
adamc@1221 2005 | _ => raise Fail "Iflow.evalExp: Bad field expression in DELETE")
adamc@1221 2006
adamc@1221 2007 val (p, st) = case expIn (e, st) of
adamc@1221 2008 (inl e, _) => raise Fail "Iflow.evalExp: DELETE with non-boolean"
adamc@1221 2009 | (inr p, st) => (p, st)
adamc@1221 2010
adamc@1221 2011 val p = And (p,
adamc@1221 2012 And (Reln (Sql "$Old", [Var old]),
adamc@1221 2013 Reln (Sql tab, [Var old])))
adamc@1221 2014 in
adamc@1221 2015 (Recd [], St.addDelete (st, (loc, And (St.ambient st, p))))
adamc@1220 2016 end)
adamc@1220 2017
adamc@1200 2018 | ENextval _ => default ()
adamc@1200 2019 | ESetval _ => default ()
adamc@1200 2020
adamc@1213 2021 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
adamc@1217 2022 let
adamc@1217 2023 val (st, nv) = St.nextVar st
adamc@1217 2024 in
adamc@1217 2025 (Var nv, St.setAmbient (st, And (St.ambient st, Reln (Known, [Var nv]))))
adamc@1217 2026 end
adamc@1213 2027
adamc@1200 2028 | EUnurlify _ => default ()
adamc@1200 2029 | EJavaScript _ => default ()
adamc@1200 2030 | ESignalReturn _ => default ()
adamc@1200 2031 | ESignalBind _ => default ()
adamc@1200 2032 | ESignalSource _ => default ()
adamc@1200 2033 | EServerCall _ => default ()
adamc@1200 2034 | ERecv _ => default ()
adamc@1200 2035 | ESleep _ => default ()
adamc@1200 2036 | ESpawn _ => default ()
adamc@1200 2037 end
adamc@1200 2038
adamc@1200 2039 fun check file =
adamc@1200 2040 let
adamc@1213 2041 val file = MonoReduce.reduce file
adamc@1213 2042 val file = MonoOpt.optimize file
adamc@1213 2043 val file = Fuse.fuse file
adamc@1213 2044 val file = MonoOpt.optimize file
adamc@1216 2045 val file = MonoShake.shake file
adamc@1213 2046 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
adamc@1213 2047
adamc@1207 2048 val exptd = foldl (fn ((d, _), exptd) =>
adamc@1207 2049 case d of
adamc@1207 2050 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
adamc@1207 2051 | _ => exptd) IS.empty file
adamc@1207 2052
adamc@1221 2053 fun decl ((d, _), (vals, inserts, deletes, client, insert, delete)) =
adamc@1200 2054 case d of
adamc@1207 2055 DVal (_, n, _, e, _) =>
adamc@1200 2056 let
adamc@1207 2057 val isExptd = IS.member (exptd, n)
adamc@1207 2058
adamc@1207 2059 fun deAbs (e, env, nv, p) =
adamc@1200 2060 case #1 e of
adamc@1207 2061 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1,
adamc@1207 2062 if isExptd then
adamc@1207 2063 And (p, Reln (Known, [Var nv]))
adamc@1207 2064 else
adamc@1207 2065 p)
adamc@1207 2066 | _ => (e, env, nv, p)
adamc@1200 2067
adamc@1207 2068 val (e, env, nv, p) = deAbs (e, [], 1, True)
adamc@1200 2069
adamc@1217 2070 val (_, st) = evalExp env (e, St.create {Var = nv,
adamc@1217 2071 Ambient = p})
adamc@1200 2072 in
adamc@1221 2073 (St.sent st @ vals, St.inserted st @ inserts, St.deleted st @ deletes, client, insert, delete)
adamc@1200 2074 end
adamc@1202 2075
adamc@1220 2076 | DPolicy pol =>
adamc@1218 2077 let
adamc@1220 2078 fun rv rvN = (rvN + 1, Lvar rvN)
adamc@1218 2079 in
adamc@1220 2080 case pol of
adamc@1220 2081 PolClient e =>
adamc@1220 2082 let
adamc@1220 2083 val (_, p, _, _, outs) = queryProp [] 0 rv SomeCol e
adamc@1220 2084 in
adamc@1221 2085 (vals, inserts, deletes, (p, outs) :: client, insert, delete)
adamc@1220 2086 end
adamc@1220 2087 | PolInsert e =>
adamc@1220 2088 let
adamc@1220 2089 val p = insertProp 0 rv e
adamc@1220 2090 in
adamc@1221 2091 (vals, inserts, deletes, client, p :: insert, delete)
adamc@1221 2092 end
adamc@1221 2093 | PolDelete e =>
adamc@1221 2094 let
adamc@1221 2095 val p = deleteProp 0 rv e
adamc@1221 2096 in
adamc@1221 2097 (vals, inserts, deletes, client, insert, p :: delete)
adamc@1220 2098 end
adamc@1218 2099 end
adamc@1214 2100
adamc@1221 2101 | _ => (vals, inserts, deletes, client, insert, delete)
adamc@1202 2102
adamc@1203 2103 val () = reset ()
adamc@1202 2104
adamc@1221 2105 val (vals, inserts, deletes, client, insert, delete) = foldl decl ([], [], [], [], [], []) file
adamc@1220 2106
adamc@1220 2107 val decompH = decomp true (fn (e1, e2) => e1 andalso e2 ())
adamc@1220 2108 val decompG = decomp false (fn (e1, e2) => e1 orelse e2 ())
adamc@1221 2109
adamc@1221 2110 fun doDml (cmds, pols) =
adamc@1221 2111 app (fn (loc, p) =>
adamc@1221 2112 if decompH p
adamc@1221 2113 (fn hyps =>
adamc@1221 2114 List.exists (fn p' =>
adamc@1221 2115 decompG p'
adamc@1221 2116 (fn goals => imply (hyps, goals, NONE)))
adamc@1221 2117 pols) then
adamc@1221 2118 ()
adamc@1221 2119 else
adamc@1221 2120 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
adamc@1221 2121 Print.preface ("The state satisifies this predicate:", p_prop p))) cmds
adamc@1200 2122 in
adamc@1218 2123 app (fn ((loc, e, p), fl) =>
adamc@1207 2124 let
adamc@1213 2125 fun doOne e =
adamc@1213 2126 let
adamc@1213 2127 val p = And (p, Reln (Eq, [Var 0, e]))
adamc@1213 2128 in
adamc@1220 2129 if decompH p
adamc@1220 2130 (fn hyps =>
adamc@1220 2131 (fl <> Control Where
adamc@1220 2132 andalso imply (hyps, [AReln (Known, [Var 0])], SOME [Var 0]))
adamc@1220 2133 orelse List.exists (fn (p', outs) =>
adamc@1220 2134 decompG p'
adamc@1220 2135 (fn goals => imply (hyps, goals, SOME outs)))
adamc@1220 2136 client) then
adamc@1213 2137 ()
adamc@1213 2138 else
adamc@1213 2139 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
adamc@1216 2140 Print.preface ("The state satisifies this predicate:", p_prop p))
adamc@1213 2141 end
adamc@1213 2142
adamc@1213 2143 fun doAll e =
adamc@1213 2144 case e of
adamc@1213 2145 Const _ => ()
adamc@1213 2146 | Var _ => doOne e
adamc@1213 2147 | Lvar _ => raise Fail "Iflow.doAll: Lvar"
adamc@1216 2148 | Func (UnCon _, [_]) => doOne e
adamc@1215 2149 | Func (_, es) => app doAll es
adamc@1213 2150 | Recd xes => app (doAll o #2) xes
adamc@1213 2151 | Proj _ => doOne e
adamc@1213 2152 | Finish => ()
adamc@1207 2153 in
adamc@1213 2154 doAll e
adamc@1220 2155 end) vals;
adamc@1220 2156
adamc@1221 2157 doDml (inserts, insert);
adamc@1221 2158 doDml (deletes, delete)
adamc@1200 2159 end
adamc@1200 2160
adamc@1213 2161 val check = fn file =>
adamc@1213 2162 let
adamc@1213 2163 val oldInline = Settings.getMonoInline ()
adamc@1213 2164 in
adamc@1213 2165 (Settings.setMonoInline (case Int.maxInt of
adamc@1213 2166 NONE => 1000000
adamc@1213 2167 | SOME n => n);
adamc@1213 2168 check file;
adamc@1213 2169 Settings.setMonoInline oldInline)
adamc@1213 2170 handle ex => (Settings.setMonoInline oldInline;
adamc@1213 2171 raise ex)
adamc@1213 2172 end
adamc@1213 2173
adamc@1200 2174 end
adamc@1213 2175