annotate src/iflow.sml @ 1238:d6938ab3b5ae

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