annotate src/iflow.sml @ 1215:360f1ed0a969

Implemented proper congruence closure, to the point where tests/policy works
author Adam Chlipala <adamc@hcoop.net>
date Thu, 08 Apr 2010 12:46:21 -0400
parents 648e6b087dfb
children 09caa8c780e5
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 type representative
adamc@1215 384
adamc@1215 385 exception Contradiction
adamc@1215 386 exception Undetermined
adamc@1215 387
adamc@1215 388 val database : unit -> database
adamc@1215 389 val representative : database * exp -> representative
adamc@1215 390
adamc@1215 391 val assert : database * atom -> unit
adamc@1215 392 val check : database * atom -> bool
adamc@1215 393
adamc@1215 394 val p_database : database Print.printer
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@1215 414 | Recrd of node ref SM.map ref
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@1215 423 Known = ref false})
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@1215 449 case #Variety (unNode n) of
adamc@1215 450 Nothing => string ("?" ^ Int.toString (Unsafe.cast n))
adamc@1215 451 | Dt0 s => string ("Dt0(" ^ s ^ ")")
adamc@1215 452 | Dt1 (s, n) => box[string ("Dt1(" ^ s ^ ","),
adamc@1215 453 space,
adamc@1215 454 p_rep n,
adamc@1215 455 string ")"]
adamc@1215 456 | Prim p => Prim.p_t p
adamc@1215 457 | Recrd (ref m) => box [string "{",
adamc@1215 458 p_list (fn (x, n) => box [string x,
adamc@1215 459 space,
adamc@1215 460 string "=",
adamc@1215 461 space,
adamc@1215 462 p_rep n]) (SM.listItemsi m),
adamc@1215 463 string "}"]
adamc@1215 464 | VFinish => string "FINISH"
adamc@1215 465
adamc@1215 466 fun p_database (db : database) =
adamc@1215 467 box [string "Vars:",
adamc@1215 468 newline,
adamc@1215 469 p_list_sep newline (fn (i, n) => box [string ("x" ^ Int.toString i),
adamc@1215 470 space,
adamc@1215 471 string "=",
adamc@1215 472 space,
adamc@1215 473 p_rep n]) (IM.listItemsi (!(#Vars db)))]
adamc@1215 474
adamc@1215 475 fun repOf (n : representative) : representative =
adamc@1215 476 case !(#Rep (unNode n)) of
adamc@1215 477 NONE => n
adamc@1215 478 | SOME r =>
adamc@1215 479 let
adamc@1215 480 val r = repOf r
adamc@1215 481 in
adamc@1215 482 #Rep (unNode n) := SOME r;
adamc@1215 483 r
adamc@1215 484 end
adamc@1215 485
adamc@1215 486 fun markKnown r =
adamc@1215 487 (#Known (unNode r) := true;
adamc@1215 488 case #Variety (unNode r) of
adamc@1215 489 Dt1 (_, r) => markKnown r
adamc@1215 490 | Recrd xes => SM.app markKnown (!xes)
adamc@1215 491 | _ => ())
adamc@1215 492
adamc@1215 493 fun representative (db : database, e) =
adamc@1208 494 let
adamc@1215 495 fun rep e =
adamc@1215 496 case e of
adamc@1215 497 Const p => (case CM.find (!(#Consts db), p) of
adamc@1215 498 SOME r => repOf r
adamc@1215 499 | NONE =>
adamc@1215 500 let
adamc@1215 501 val r = ref (Node {Rep = ref NONE,
adamc@1215 502 Cons = ref SM.empty,
adamc@1215 503 Variety = Prim p,
adamc@1215 504 Known = ref false})
adamc@1215 505 in
adamc@1215 506 #Consts db := CM.insert (!(#Consts db), p, r);
adamc@1215 507 r
adamc@1215 508 end)
adamc@1215 509 | Var n => (case IM.find (!(#Vars db), n) of
adamc@1215 510 SOME r => repOf r
adamc@1215 511 | NONE =>
adamc@1215 512 let
adamc@1215 513 val r = ref (Node {Rep = ref NONE,
adamc@1215 514 Cons = ref SM.empty,
adamc@1215 515 Variety = Nothing,
adamc@1215 516 Known = ref false})
adamc@1215 517 in
adamc@1215 518 #Vars db := IM.insert (!(#Vars db), n, r);
adamc@1215 519 r
adamc@1215 520 end)
adamc@1215 521 | Lvar n =>
adamc@1215 522 (case IM.find (!unif, n) of
adamc@1215 523 NONE => raise Undetermined
adamc@1215 524 | SOME e => rep e)
adamc@1215 525 | Func (DtCon0 f, []) => (case SM.find (!(#Con0s db), f) of
adamc@1215 526 SOME r => repOf r
adamc@1215 527 | NONE =>
adamc@1215 528 let
adamc@1215 529 val r = ref (Node {Rep = ref NONE,
adamc@1215 530 Cons = ref SM.empty,
adamc@1215 531 Variety = Dt0 f,
adamc@1215 532 Known = ref false})
adamc@1215 533 in
adamc@1215 534 #Con0s db := SM.insert (!(#Con0s db), f, r);
adamc@1215 535 r
adamc@1215 536 end)
adamc@1215 537 | Func (DtCon0 _, _) => raise Fail "Iflow.rep: DtCon0"
adamc@1215 538 | Func (DtCon1 f, [e]) =>
adamc@1215 539 let
adamc@1215 540 val r = rep e
adamc@1215 541 in
adamc@1215 542 case SM.find (!(#Cons (unNode r)), f) of
adamc@1215 543 SOME r => repOf r
adamc@1215 544 | NONE =>
adamc@1215 545 let
adamc@1215 546 val r' = ref (Node {Rep = ref NONE,
adamc@1215 547 Cons = ref SM.empty,
adamc@1215 548 Variety = Dt1 (f, r),
adamc@1215 549 Known = ref false})
adamc@1215 550 in
adamc@1215 551 #Cons (unNode r) := SM.insert (!(#Cons (unNode r)), f, r');
adamc@1215 552 r'
adamc@1215 553 end
adamc@1215 554 end
adamc@1215 555 | Func (DtCon1 _, _) => raise Fail "Iflow.rep: DtCon1"
adamc@1215 556 | Func (UnCon f, [e]) =>
adamc@1215 557 let
adamc@1215 558 val r = rep e
adamc@1215 559 in
adamc@1215 560 case #Variety (unNode r) of
adamc@1215 561 Dt1 (f', n) => if f' = f then
adamc@1215 562 repOf n
adamc@1215 563 else
adamc@1215 564 raise Contradiction
adamc@1215 565 | Nothing =>
adamc@1215 566 let
adamc@1215 567 val cons = ref SM.empty
adamc@1215 568 val r' = ref (Node {Rep = ref NONE,
adamc@1215 569 Cons = cons,
adamc@1215 570 Variety = Nothing,
adamc@1215 571 Known = ref false})
adamc@1215 572
adamc@1215 573 val r'' = ref (Node {Rep = ref NONE,
adamc@1215 574 Cons = #Cons (unNode r),
adamc@1215 575 Variety = Dt1 (f, r'),
adamc@1215 576 Known = #Known (unNode r)})
adamc@1215 577 in
adamc@1215 578 cons := SM.insert (!cons, f, r'');
adamc@1215 579 #Rep (unNode r) := SOME r'';
adamc@1215 580 r'
adamc@1215 581 end
adamc@1215 582 | VFinish => r
adamc@1215 583 | _ => raise Contradiction
adamc@1215 584 end
adamc@1215 585 | Func (UnCon _, _) => raise Fail "Iflow.rep: UnCon"
adamc@1215 586 | Func (Other f, es) =>
adamc@1215 587 let
adamc@1215 588 val rs = map rep es
adamc@1215 589 in
adamc@1215 590 case List.find (fn (x : string * representative list, _) => x = (f, rs)) (!(#Funcs db)) of
adamc@1215 591 NONE =>
adamc@1215 592 let
adamc@1215 593 val r = ref (Node {Rep = ref NONE,
adamc@1215 594 Cons = ref SM.empty,
adamc@1215 595 Variety = Nothing,
adamc@1215 596 Known = ref false})
adamc@1215 597 in
adamc@1215 598 #Funcs db := ((f, rs), r) :: (!(#Funcs db));
adamc@1215 599 r
adamc@1215 600 end
adamc@1215 601 | SOME (_, r) => repOf r
adamc@1215 602 end
adamc@1215 603 | Recd xes =>
adamc@1215 604 let
adamc@1215 605 val xes = map (fn (x, e) => (x, rep e)) xes
adamc@1215 606 val len = length xes
adamc@1215 607 in
adamc@1215 608 case List.find (fn (xes', _) =>
adamc@1215 609 SM.numItems xes' = len
adamc@1215 610 andalso List.all (fn (x, n) =>
adamc@1215 611 case SM.find (xes', x) of
adamc@1215 612 NONE => false
adamc@1215 613 | SOME n' => n = repOf n') xes)
adamc@1215 614 (!(#Records db)) of
adamc@1215 615 SOME (_, r) => repOf r
adamc@1215 616 | NONE =>
adamc@1215 617 let
adamc@1215 618 val xes = foldl SM.insert' SM.empty xes
adamc@1215 619
adamc@1215 620 val r' = ref (Node {Rep = ref NONE,
adamc@1215 621 Cons = ref SM.empty,
adamc@1215 622 Variety = Recrd (ref xes),
adamc@1215 623 Known = ref false})
adamc@1215 624 in
adamc@1215 625 #Records db := (xes, r') :: (!(#Records db));
adamc@1215 626 r'
adamc@1215 627 end
adamc@1215 628 end
adamc@1215 629 | Proj (e, f) =>
adamc@1215 630 let
adamc@1215 631 val r = rep e
adamc@1215 632 in
adamc@1215 633 case #Variety (unNode r) of
adamc@1215 634 Recrd xes =>
adamc@1215 635 (case SM.find (!xes, f) of
adamc@1215 636 SOME r => repOf r
adamc@1215 637 | NONE =>let
adamc@1215 638 val r = ref (Node {Rep = ref NONE,
adamc@1215 639 Cons = ref SM.empty,
adamc@1215 640 Variety = Nothing,
adamc@1215 641 Known = ref false})
adamc@1215 642 in
adamc@1215 643 xes := SM.insert (!xes, f, r);
adamc@1215 644 r
adamc@1215 645 end)
adamc@1215 646 | Nothing =>
adamc@1215 647 let
adamc@1215 648 val r' = ref (Node {Rep = ref NONE,
adamc@1215 649 Cons = ref SM.empty,
adamc@1215 650 Variety = Nothing,
adamc@1215 651 Known = ref false})
adamc@1215 652
adamc@1215 653 val r'' = ref (Node {Rep = ref NONE,
adamc@1215 654 Cons = #Cons (unNode r),
adamc@1215 655 Variety = Recrd (ref (SM.insert (SM.empty, f, r'))),
adamc@1215 656 Known = #Known (unNode r)})
adamc@1215 657 in
adamc@1215 658 #Rep (unNode r) := SOME r'';
adamc@1215 659 r'
adamc@1215 660 end
adamc@1215 661 | VFinish => r
adamc@1215 662 | _ => raise Contradiction
adamc@1215 663 end
adamc@1215 664 | Finish => finish
adamc@1208 665 in
adamc@1215 666 rep e
adamc@1208 667 end
adamc@1208 668
adamc@1215 669 fun assert (db, a) =
adamc@1215 670 case a of
adamc@1215 671 ACond _ => ()
adamc@1215 672 | AReln x =>
adamc@1215 673 case x of
adamc@1215 674 (Known, [e]) => markKnown (representative (db, e))
adamc@1215 675 | (PCon0 f, [e]) =>
adamc@1215 676 let
adamc@1215 677 val r = representative (db, e)
adamc@1215 678 in
adamc@1215 679 case #Variety (unNode r) of
adamc@1215 680 Dt0 f' => if f = f' then
adamc@1215 681 ()
adamc@1215 682 else
adamc@1215 683 raise Contradiction
adamc@1215 684 | Nothing =>
adamc@1215 685 let
adamc@1215 686 val r' = ref (Node {Rep = ref NONE,
adamc@1215 687 Cons = ref SM.empty,
adamc@1215 688 Variety = Dt0 f,
adamc@1215 689 Known = ref false})
adamc@1215 690 in
adamc@1215 691 #Rep (unNode r) := SOME r'
adamc@1215 692 end
adamc@1215 693 | _ => raise Contradiction
adamc@1215 694 end
adamc@1215 695 | (PCon1 f, [e]) =>
adamc@1215 696 let
adamc@1215 697 val r = representative (db, e)
adamc@1215 698 in
adamc@1215 699 case #Variety (unNode r) of
adamc@1215 700 Dt1 (f', e') => if f = f' then
adamc@1215 701 ()
adamc@1215 702 else
adamc@1215 703 raise Contradiction
adamc@1215 704 | Nothing =>
adamc@1215 705 let
adamc@1215 706 val r'' = ref (Node {Rep = ref NONE,
adamc@1215 707 Cons = ref SM.empty,
adamc@1215 708 Variety = Nothing,
adamc@1215 709 Known = ref false})
adamc@1214 710
adamc@1215 711 val r' = ref (Node {Rep = ref NONE,
adamc@1215 712 Cons = ref SM.empty,
adamc@1215 713 Variety = Dt1 (f, r''),
adamc@1215 714 Known = ref false})
adamc@1215 715 in
adamc@1215 716 #Rep (unNode r) := SOME r'
adamc@1215 717 end
adamc@1215 718 | _ => raise Contradiction
adamc@1215 719 end
adamc@1215 720 | (Eq, [e1, e2]) =>
adamc@1215 721 let
adamc@1215 722 fun markEq (r1, r2) =
adamc@1215 723 if r1 = r2 then
adamc@1215 724 ()
adamc@1215 725 else case (#Variety (unNode r1), #Variety (unNode r2)) of
adamc@1215 726 (Prim p1, Prim p2) => if Prim.equal (p1, p2) then
adamc@1215 727 ()
adamc@1215 728 else
adamc@1215 729 raise Contradiction
adamc@1215 730 | (Dt0 f1, Dt0 f2) => if f1 = f2 then
adamc@1215 731 ()
adamc@1215 732 else
adamc@1215 733 raise Contradiction
adamc@1215 734 | (Dt1 (f1, r1), Dt1 (f2, r2)) => if f1 = f2 then
adamc@1215 735 markEq (r1, r2)
adamc@1215 736 else
adamc@1215 737 raise Contradiction
adamc@1215 738 | (Recrd xes1, Recrd xes2) =>
adamc@1215 739 let
adamc@1215 740 fun unif (xes1, xes2) =
adamc@1215 741 SM.appi (fn (x, r1) =>
adamc@1215 742 case SM.find (xes2, x) of
adamc@1215 743 NONE => ()
adamc@1215 744 | SOME r2 => markEq (r1, r2)) xes1
adamc@1215 745 in
adamc@1215 746 unif (!xes1, !xes2);
adamc@1215 747 unif (!xes2, !xes1)
adamc@1215 748 end
adamc@1215 749 | (VFinish, VFinish) => ()
adamc@1215 750 | (Nothing, _) =>
adamc@1215 751 (#Rep (unNode r1) := SOME r2;
adamc@1215 752 if !(#Known (unNode r1)) andalso not (!(#Known (unNode r2))) then
adamc@1215 753 markKnown r2
adamc@1215 754 else
adamc@1215 755 ();
adamc@1215 756 #Cons (unNode r2) := SM.unionWith #1 (!(#Cons (unNode r2)), !(#Cons (unNode r1)));
adamc@1215 757 compactFuncs ())
adamc@1215 758 | (_, Nothing) =>
adamc@1215 759 (#Rep (unNode r2) := SOME r1;
adamc@1215 760 if !(#Known (unNode r2)) andalso not (!(#Known (unNode r1))) then
adamc@1215 761 markKnown r1
adamc@1215 762 else
adamc@1215 763 ();
adamc@1215 764 #Cons (unNode r1) := SM.unionWith #1 (!(#Cons (unNode r1)), !(#Cons (unNode r2)));
adamc@1215 765 compactFuncs ())
adamc@1215 766 | _ => raise Contradiction
adamc@1214 767
adamc@1215 768 and compactFuncs () =
adamc@1214 769 let
adamc@1215 770 fun loop funcs =
adamc@1215 771 case funcs of
adamc@1215 772 [] => []
adamc@1215 773 | (fr as ((f, rs), r)) :: rest =>
adamc@1215 774 let
adamc@1215 775 val rest = List.filter (fn ((f' : string, rs'), r') =>
adamc@1215 776 if f' = f
adamc@1215 777 andalso ListPair.allEq (fn (r1, r2) =>
adamc@1215 778 repOf r1 = repOf r2)
adamc@1215 779 (rs, rs') then
adamc@1215 780 (markEq (r, r');
adamc@1215 781 false)
adamc@1215 782 else
adamc@1215 783 true) rest
adamc@1215 784 in
adamc@1215 785 fr :: loop rest
adamc@1215 786 end
adamc@1214 787 in
adamc@1215 788 #Funcs db := loop (!(#Funcs db))
adamc@1214 789 end
adamc@1215 790 in
adamc@1215 791 markEq (representative (db, e1), representative (db, e2))
adamc@1215 792 end
adamc@1215 793 | _ => ()
adamc@1214 794
adamc@1215 795 fun check (db, a) =
adamc@1215 796 case a of
adamc@1215 797 ACond _ => false
adamc@1215 798 | AReln x =>
adamc@1215 799 case x of
adamc@1215 800 (Known, [e]) => !(#Known (unNode (representative (db, e))))
adamc@1215 801 | (PCon0 f, [e]) =>
adamc@1215 802 (case #Variety (unNode (representative (db, e))) of
adamc@1215 803 Dt0 f' => f' = f
adamc@1215 804 | _ => false)
adamc@1215 805 | (PCon1 f, [e]) =>
adamc@1215 806 (case #Variety (unNode (representative (db, e))) of
adamc@1215 807 Dt1 (f', _) => f' = f
adamc@1215 808 | _ => false)
adamc@1215 809 | (Eq, [e1, e2]) =>
adamc@1214 810 let
adamc@1215 811 val r1 = representative (db, e1)
adamc@1215 812 val r2 = representative (db, e2)
adamc@1214 813 in
adamc@1215 814 repOf r1 = repOf r2
adamc@1214 815 end
adamc@1215 816 | _ => false
adamc@1212 817
adamc@1208 818 end
adamc@1208 819
adamc@1215 820 fun decomp fals or =
adamc@1215 821 let
adamc@1215 822 fun decomp p k =
adamc@1215 823 case p of
adamc@1215 824 True => k []
adamc@1215 825 | False => fals
adamc@1215 826 | Unknown => k []
adamc@1215 827 | And (p1, p2) =>
adamc@1215 828 decomp p1 (fn ps1 =>
adamc@1215 829 decomp p2 (fn ps2 =>
adamc@1215 830 k (ps1 @ ps2)))
adamc@1215 831 | Or (p1, p2) =>
adamc@1215 832 or (decomp p1 k, fn () => decomp p2 k)
adamc@1215 833 | Reln x => k [AReln x]
adamc@1215 834 | Cond x => k [ACond x]
adamc@1215 835 in
adamc@1215 836 decomp
adamc@1215 837 end
adamc@1202 838
adamc@1202 839 fun imply (p1, p2) =
adamc@1215 840 (reset ();
adamc@1215 841 decomp true (fn (e1, e2) => e1 andalso e2 ()) p1
adamc@1215 842 (fn hyps =>
adamc@1215 843 decomp false (fn (e1, e2) => e1 orelse e2 ()) p2
adamc@1215 844 (fn goals =>
adamc@1215 845 let
adamc@1215 846 fun gls goals onFail acc =
adamc@1215 847 case goals of
adamc@1215 848 [] =>
adamc@1215 849 (let
adamc@1215 850 val cc = Cc.database ()
adamc@1215 851 val () = app (fn a => Cc.assert (cc, a)) hyps
adamc@1215 852 in
adamc@1215 853 (List.all (fn a =>
adamc@1215 854 if Cc.check (cc, a) then
adamc@1215 855 true
adamc@1215 856 else
adamc@1215 857 ((*Print.prefaces "Can't prove"
adamc@1215 858 [("a", p_atom a),
adamc@1215 859 ("hyps", Print.p_list p_atom hyps),
adamc@1215 860 ("db", Cc.p_database cc)];*)
adamc@1215 861 false)) acc
adamc@1215 862 orelse onFail ())
adamc@1215 863 handle Cc.Contradiction => onFail ()
adamc@1215 864 end handle Cc.Undetermined => onFail ())
adamc@1215 865 | AReln (Sql gf, [ge]) :: goals =>
adamc@1215 866 let
adamc@1215 867 fun hps hyps =
adamc@1215 868 case hyps of
adamc@1215 869 [] => onFail ()
adamc@1215 870 | AReln (Sql hf, [he]) :: hyps =>
adamc@1215 871 if gf = hf then
adamc@1215 872 let
adamc@1215 873 val saved = save ()
adamc@1215 874 in
adamc@1215 875 if eq (ge, he) then
adamc@1215 876 let
adamc@1215 877 val changed = IM.numItems (!unif)
adamc@1215 878 <> IM.numItems saved
adamc@1215 879 in
adamc@1215 880 gls goals (fn () => (restore saved;
adamc@1215 881 changed
adamc@1215 882 andalso hps hyps))
adamc@1215 883 acc
adamc@1215 884 end
adamc@1215 885 else
adamc@1215 886 hps hyps
adamc@1215 887 end
adamc@1215 888 else
adamc@1215 889 hps hyps
adamc@1215 890 | _ :: hyps => hps hyps
adamc@1215 891 in
adamc@1215 892 hps hyps
adamc@1215 893 end
adamc@1215 894 | g :: goals => gls goals onFail (g :: acc)
adamc@1215 895 in
adamc@1215 896 gls goals (fn () => false) []
adamc@1215 897 end handle Cc.Contradiction => true)))
adamc@1200 898
adamc@1200 899 fun patCon pc =
adamc@1200 900 case pc of
adamc@1200 901 PConVar n => "C" ^ Int.toString n
adamc@1200 902 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
adamc@1200 903
adamc@1200 904 datatype chunk =
adamc@1200 905 String of string
adamc@1200 906 | Exp of Mono.exp
adamc@1200 907
adamc@1200 908 fun chunkify e =
adamc@1200 909 case #1 e of
adamc@1200 910 EPrim (Prim.String s) => [String s]
adamc@1207 911 | EStrcat (e1, e2) =>
adamc@1207 912 let
adamc@1207 913 val chs1 = chunkify e1
adamc@1207 914 val chs2 = chunkify e2
adamc@1207 915 in
adamc@1207 916 case chs2 of
adamc@1207 917 String s2 :: chs2' =>
adamc@1207 918 (case List.last chs1 of
adamc@1207 919 String s1 => List.take (chs1, length chs1 - 1) @ String (s1 ^ s2) :: chs2'
adamc@1207 920 | _ => chs1 @ chs2)
adamc@1207 921 | _ => chs1 @ chs2
adamc@1207 922 end
adamc@1200 923 | _ => [Exp e]
adamc@1200 924
adamc@1201 925 type 'a parser = chunk list -> ('a * chunk list) option
adamc@1201 926
adamc@1201 927 fun always v chs = SOME (v, chs)
adamc@1201 928
adamc@1202 929 fun parse p s =
adamc@1202 930 case p (chunkify s) of
adamc@1201 931 SOME (v, []) => SOME v
adamc@1201 932 | _ => NONE
adamc@1201 933
adamc@1201 934 fun const s chs =
adamc@1201 935 case chs of
adamc@1201 936 String s' :: chs => if String.isPrefix s s' then
adamc@1201 937 SOME ((), if size s = size s' then
adamc@1201 938 chs
adamc@1201 939 else
adamc@1201 940 String (String.extract (s', size s, NONE)) :: chs)
adamc@1201 941 else
adamc@1201 942 NONE
adamc@1201 943 | _ => NONE
adamc@1201 944
adamc@1201 945 fun follow p1 p2 chs =
adamc@1201 946 case p1 chs of
adamc@1201 947 NONE => NONE
adamc@1201 948 | SOME (v1, chs) =>
adamc@1201 949 case p2 chs of
adamc@1201 950 NONE => NONE
adamc@1201 951 | SOME (v2, chs) => SOME ((v1, v2), chs)
adamc@1201 952
adamc@1201 953 fun wrap p f chs =
adamc@1201 954 case p chs of
adamc@1201 955 NONE => NONE
adamc@1201 956 | SOME (v, chs) => SOME (f v, chs)
adamc@1201 957
adamc@1209 958 fun wrapP p f chs =
adamc@1209 959 case p chs of
adamc@1209 960 NONE => NONE
adamc@1209 961 | SOME (v, chs) =>
adamc@1209 962 case f v of
adamc@1209 963 NONE => NONE
adamc@1209 964 | SOME r => SOME (r, chs)
adamc@1209 965
adamc@1201 966 fun alt p1 p2 chs =
adamc@1201 967 case p1 chs of
adamc@1201 968 NONE => p2 chs
adamc@1201 969 | v => v
adamc@1201 970
adamc@1207 971 fun altL ps =
adamc@1207 972 case rev ps of
adamc@1207 973 [] => (fn _ => NONE)
adamc@1207 974 | p :: ps =>
adamc@1207 975 foldl (fn (p1, p2) => alt p1 p2) p ps
adamc@1207 976
adamc@1204 977 fun opt p chs =
adamc@1204 978 case p chs of
adamc@1204 979 NONE => SOME (NONE, chs)
adamc@1204 980 | SOME (v, chs) => SOME (SOME v, chs)
adamc@1204 981
adamc@1201 982 fun skip cp chs =
adamc@1201 983 case chs of
adamc@1201 984 String "" :: chs => skip cp chs
adamc@1201 985 | String s :: chs' => if cp (String.sub (s, 0)) then
adamc@1201 986 skip cp (String (String.extract (s, 1, NONE)) :: chs')
adamc@1201 987 else
adamc@1201 988 SOME ((), chs)
adamc@1201 989 | _ => SOME ((), chs)
adamc@1201 990
adamc@1201 991 fun keep cp chs =
adamc@1201 992 case chs of
adamc@1201 993 String "" :: chs => keep cp chs
adamc@1201 994 | String s :: chs' =>
adamc@1201 995 let
adamc@1201 996 val (befor, after) = Substring.splitl cp (Substring.full s)
adamc@1201 997 in
adamc@1201 998 if Substring.isEmpty befor then
adamc@1201 999 NONE
adamc@1201 1000 else
adamc@1201 1001 SOME (Substring.string befor,
adamc@1201 1002 if Substring.isEmpty after then
adamc@1201 1003 chs'
adamc@1201 1004 else
adamc@1201 1005 String (Substring.string after) :: chs')
adamc@1201 1006 end
adamc@1201 1007 | _ => NONE
adamc@1201 1008
adamc@1204 1009 fun ws p = wrap (follow (skip (fn ch => ch = #" "))
adamc@1204 1010 (follow p (skip (fn ch => ch = #" ")))) (#1 o #2)
adamc@1204 1011
adamc@1204 1012 fun log name p chs =
adamc@1206 1013 (if !debug then
adamc@1206 1014 case chs of
adamc@1207 1015 String s :: _ => print (name ^ ": " ^ s ^ "\n")
adamc@1206 1016 | _ => print (name ^ ": blocked!\n")
adamc@1206 1017 else
adamc@1206 1018 ();
adamc@1204 1019 p chs)
adamc@1201 1020
adamc@1201 1021 fun list p chs =
adamc@1207 1022 altL [wrap (follow p (follow (ws (const ",")) (list p)))
adamc@1207 1023 (fn (v, ((), ls)) => v :: ls),
adamc@1207 1024 wrap (ws p) (fn v => [v]),
adamc@1207 1025 always []] chs
adamc@1201 1026
adamc@1201 1027 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
adamc@1201 1028
adamc@1211 1029 val t_ident = wrapP ident (fn s => if String.isPrefix "T_" s then
adamc@1211 1030 SOME (String.extract (s, 2, NONE))
adamc@1201 1031 else
adamc@1211 1032 NONE)
adamc@1211 1033 val uw_ident = wrapP ident (fn s => if String.isPrefix "uw_" s andalso size s >= 4 then
adamc@1211 1034 SOME (str (Char.toUpper (String.sub (s, 3)))
adamc@1211 1035 ^ String.extract (s, 4, NONE))
adamc@1211 1036 else
adamc@1211 1037 NONE)
adamc@1201 1038
adamc@1211 1039 val field = wrap (follow t_ident
adamc@1201 1040 (follow (const ".")
adamc@1201 1041 uw_ident))
adamc@1201 1042 (fn (t, ((), f)) => (t, f))
adamc@1201 1043
adamc@1206 1044 datatype Rel =
adamc@1206 1045 Exps of exp * exp -> prop
adamc@1206 1046 | Props of prop * prop -> prop
adamc@1206 1047
adamc@1204 1048 datatype sqexp =
adamc@1206 1049 SqConst of Prim.t
adamc@1206 1050 | Field of string * string
adamc@1206 1051 | Binop of Rel * sqexp * sqexp
adamc@1207 1052 | SqKnown of sqexp
adamc@1207 1053 | Inj of Mono.exp
adamc@1211 1054 | SqFunc of string * sqexp
adamc@1211 1055 | Count
adamc@1204 1056
adamc@1210 1057 fun cmp s r = wrap (const s) (fn () => Exps (fn (e1, e2) => Reln (r, [e1, e2])))
adamc@1210 1058
adamc@1210 1059 val sqbrel = altL [cmp "=" Eq,
adamc@1210 1060 cmp "<>" Ne,
adamc@1210 1061 cmp "<=" Le,
adamc@1210 1062 cmp "<" Lt,
adamc@1210 1063 cmp ">=" Ge,
adamc@1210 1064 cmp ">" Gt,
adamc@1207 1065 wrap (const "AND") (fn () => Props And),
adamc@1207 1066 wrap (const "OR") (fn () => Props Or)]
adamc@1204 1067
adamc@1204 1068 datatype ('a, 'b) sum = inl of 'a | inr of 'b
adamc@1204 1069
adamc@1209 1070 fun string chs =
adamc@1206 1071 case chs of
adamc@1209 1072 String s :: chs =>
adamc@1209 1073 if size s >= 2 andalso String.sub (s, 0) = #"'" then
adamc@1209 1074 let
adamc@1209 1075 fun loop (cs, acc) =
adamc@1209 1076 case cs of
adamc@1209 1077 [] => NONE
adamc@1209 1078 | c :: cs =>
adamc@1209 1079 if c = #"'" then
adamc@1209 1080 SOME (String.implode (rev acc), cs)
adamc@1209 1081 else if c = #"\\" then
adamc@1209 1082 case cs of
adamc@1209 1083 c :: cs => loop (cs, c :: acc)
adamc@1209 1084 | _ => raise Fail "Iflow.string: Unmatched backslash escape"
adamc@1209 1085 else
adamc@1209 1086 loop (cs, c :: acc)
adamc@1209 1087 in
adamc@1209 1088 case loop (String.explode (String.extract (s, 1, NONE)), []) of
adamc@1209 1089 NONE => NONE
adamc@1209 1090 | SOME (s, []) => SOME (s, chs)
adamc@1209 1091 | SOME (s, cs) => SOME (s, String (String.implode cs) :: chs)
adamc@1209 1092 end
adamc@1209 1093 else
adamc@1209 1094 NONE
adamc@1209 1095 | _ => NONE
adamc@1206 1096
adamc@1209 1097 val prim =
adamc@1209 1098 altL [wrap (follow (wrapP (follow (keep Char.isDigit) (follow (const ".") (keep Char.isDigit)))
adamc@1209 1099 (fn (x, ((), y)) => Option.map Prim.Float (Real64.fromString (x ^ "." ^ y))))
adamc@1209 1100 (opt (const "::float8"))) #1,
adamc@1209 1101 wrap (follow (wrapP (keep Char.isDigit)
adamc@1209 1102 (Option.map Prim.Int o Int64.fromString))
adamc@1209 1103 (opt (const "::int8"))) #1,
adamc@1209 1104 wrap (follow (opt (const "E")) (follow string (opt (const "::text"))))
adamc@1209 1105 (Prim.String o #1 o #2)]
adamc@1206 1106
adamc@1207 1107 fun known' chs =
adamc@1207 1108 case chs of
adamc@1207 1109 Exp (EFfi ("Basis", "sql_known"), _) :: chs => SOME ((), chs)
adamc@1207 1110 | _ => NONE
adamc@1207 1111
adamc@1207 1112 fun sqlify chs =
adamc@1207 1113 case chs of
adamc@1207 1114 Exp (EFfiApp ("Basis", f, [e]), _) :: chs =>
adamc@1207 1115 if String.isPrefix "sqlify" f then
adamc@1207 1116 SOME (e, chs)
adamc@1207 1117 else
adamc@1207 1118 NONE
adamc@1207 1119 | _ => NONE
adamc@1207 1120
adamc@1211 1121 fun constK s = wrap (const s) (fn () => s)
adamc@1211 1122
adamc@1211 1123 val funcName = altL [constK "COUNT",
adamc@1211 1124 constK "MIN",
adamc@1211 1125 constK "MAX",
adamc@1211 1126 constK "SUM",
adamc@1211 1127 constK "AVG"]
adamc@1211 1128
adamc@1204 1129 fun sqexp chs =
adamc@1206 1130 log "sqexp"
adamc@1207 1131 (altL [wrap prim SqConst,
adamc@1211 1132 wrap field Field,
adamc@1207 1133 wrap known SqKnown,
adamc@1211 1134 wrap func SqFunc,
adamc@1211 1135 wrap (const "COUNT(*)") (fn () => Count),
adamc@1207 1136 wrap sqlify Inj,
adamc@1211 1137 wrap (follow (const "COALESCE(") (follow sqexp (follow (const ",")
adamc@1211 1138 (follow (keep (fn ch => ch <> #")")) (const ")")))))
adamc@1211 1139 (fn ((), (e, _)) => e),
adamc@1207 1140 wrap (follow (ws (const "("))
adamc@1207 1141 (follow (wrap
adamc@1207 1142 (follow sqexp
adamc@1207 1143 (alt
adamc@1207 1144 (wrap
adamc@1207 1145 (follow (ws sqbrel)
adamc@1207 1146 (ws sqexp))
adamc@1207 1147 inl)
adamc@1207 1148 (always (inr ()))))
adamc@1207 1149 (fn (e1, sm) =>
adamc@1207 1150 case sm of
adamc@1207 1151 inl (bo, e2) => Binop (bo, e1, e2)
adamc@1207 1152 | inr () => e1))
adamc@1207 1153 (const ")")))
adamc@1207 1154 (fn ((), (e, ())) => e)])
adamc@1207 1155 chs
adamc@1206 1156
adamc@1207 1157 and known chs = wrap (follow known' (follow (const "(") (follow sqexp (const ")"))))
adamc@1211 1158 (fn ((), ((), (e, ()))) => e) chs
adamc@1211 1159
adamc@1211 1160 and func chs = wrap (follow funcName (follow (const "(") (follow sqexp (const ")"))))
adamc@1211 1161 (fn (f, ((), (e, ()))) => (f, e)) chs
adamc@1211 1162
adamc@1211 1163 datatype sitem =
adamc@1211 1164 SqField of string * string
adamc@1211 1165 | SqExp of sqexp * string
adamc@1211 1166
adamc@1211 1167 val sitem = alt (wrap field SqField)
adamc@1211 1168 (wrap (follow sqexp (follow (const " AS ") uw_ident))
adamc@1211 1169 (fn (e, ((), s)) => SqExp (e, s)))
adamc@1207 1170
adamc@1207 1171 val select = log "select"
adamc@1207 1172 (wrap (follow (const "SELECT ") (list sitem))
adamc@1207 1173 (fn ((), ls) => ls))
adamc@1201 1174
adamc@1201 1175 val fitem = wrap (follow uw_ident
adamc@1201 1176 (follow (const " AS ")
adamc@1201 1177 t_ident))
adamc@1201 1178 (fn (t, ((), f)) => (t, f))
adamc@1201 1179
adamc@1207 1180 val from = log "from"
adamc@1207 1181 (wrap (follow (const "FROM ") (list fitem))
adamc@1207 1182 (fn ((), ls) => ls))
adamc@1201 1183
adamc@1204 1184 val wher = wrap (follow (ws (const "WHERE ")) sqexp)
adamc@1204 1185 (fn ((), ls) => ls)
adamc@1204 1186
adamc@1207 1187 val query = log "query"
adamc@1207 1188 (wrap (follow (follow select from) (opt wher))
adamc@1207 1189 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
adamc@1201 1190
adamc@1215 1191 fun removeDups (ls : (string * string) list) =
adamc@1211 1192 case ls of
adamc@1211 1193 [] => []
adamc@1211 1194 | x :: ls =>
adamc@1211 1195 let
adamc@1211 1196 val ls = removeDups ls
adamc@1211 1197 in
adamc@1211 1198 if List.exists (fn x' => x' = x) ls then
adamc@1211 1199 ls
adamc@1211 1200 else
adamc@1211 1201 x :: ls
adamc@1211 1202 end
adamc@1211 1203
adamc@1212 1204 datatype queryMode =
adamc@1212 1205 SomeCol of exp
adamc@1212 1206 | AllCols of exp
adamc@1212 1207
adamc@1214 1208 fun queryProp env rvN rv oe e =
adamc@1202 1209 case parse query e of
adamc@1207 1210 NONE => (print ("Warning: Information flow checker can't parse SQL query at "
adamc@1207 1211 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
adamc@1214 1212 (rvN, Var 0, Unknown, []))
adamc@1201 1213 | SOME r =>
adamc@1202 1214 let
adamc@1214 1215 val (rvN, count) = rv rvN
adamc@1214 1216
adamc@1214 1217 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
adamc@1214 1218 let
adamc@1214 1219 val (rvN, e) = rv rvN
adamc@1214 1220 in
adamc@1214 1221 ((v, e), rvN)
adamc@1214 1222 end) rvN (#From r)
adamc@1214 1223
adamc@1214 1224 fun rvOf v =
adamc@1214 1225 case List.find (fn (v', _) => v' = v) rvs of
adamc@1214 1226 NONE => raise Fail "Iflow.queryProp: Bad table variable"
adamc@1214 1227 | SOME (_, e) => e
adamc@1214 1228
adamc@1211 1229 fun usedFields e =
adamc@1211 1230 case e of
adamc@1211 1231 SqConst _ => []
adamc@1211 1232 | Field (v, f) => [(v, f)]
adamc@1211 1233 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
adamc@1211 1234 | SqKnown _ => []
adamc@1211 1235 | Inj _ => []
adamc@1211 1236 | SqFunc (_, e) => usedFields e
adamc@1211 1237 | Count => []
adamc@1211 1238
adamc@1202 1239 val p =
adamc@1214 1240 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
adamc@1205 1241
adamc@1205 1242 fun expIn e =
adamc@1205 1243 case e of
adamc@1206 1244 SqConst p => inl (Const p)
adamc@1214 1245 | Field (v, f) => inl (Proj (rvOf v, f))
adamc@1205 1246 | Binop (bo, e1, e2) =>
adamc@1206 1247 inr (case (bo, expIn e1, expIn e2) of
adamc@1206 1248 (Exps f, inl e1, inl e2) => f (e1, e2)
adamc@1206 1249 | (Props f, inr p1, inr p2) => f (p1, p2)
adamc@1206 1250 | _ => Unknown)
adamc@1207 1251 | SqKnown e =>
adamc@1207 1252 inr (case expIn e of
adamc@1207 1253 inl e => Reln (Known, [e])
adamc@1207 1254 | _ => Unknown)
adamc@1207 1255 | Inj e =>
adamc@1207 1256 let
adamc@1207 1257 fun deinj (e, _) =
adamc@1207 1258 case e of
adamc@1207 1259 ERel n => List.nth (env, n)
adamc@1207 1260 | EField (e, f) => Proj (deinj e, f)
adamc@1207 1261 | _ => raise Fail "Iflow: non-variable injected into query"
adamc@1207 1262 in
adamc@1207 1263 inl (deinj e)
adamc@1207 1264 end
adamc@1211 1265 | SqFunc (f, e) =>
adamc@1211 1266 inl (case expIn e of
adamc@1215 1267 inl e => Func (Other f, [e])
adamc@1211 1268 | _ => raise Fail ("Iflow: non-expresion passed to function " ^ f))
adamc@1214 1269 | Count => inl count
adamc@1210 1270
adamc@1205 1271 val p = case #Where r of
adamc@1205 1272 NONE => p
adamc@1205 1273 | SOME e =>
adamc@1205 1274 case expIn e of
adamc@1205 1275 inr p' => And (p, p')
adamc@1205 1276 | _ => p
adamc@1202 1277 in
adamc@1214 1278 (rvN,
adamc@1214 1279 count,
adamc@1214 1280 And (p, case oe of
adamc@1212 1281 SomeCol oe =>
adamc@1212 1282 foldl (fn (si, p) =>
adamc@1211 1283 let
adamc@1211 1284 val p' = case si of
adamc@1214 1285 SqField (v, f) => Reln (Eq, [oe, Proj (rvOf v, f)])
adamc@1211 1286 | SqExp (e, f) =>
adamc@1211 1287 case expIn e of
adamc@1211 1288 inr _ => Unknown
adamc@1211 1289 | inl e => Reln (Eq, [oe, e])
adamc@1211 1290 in
adamc@1211 1291 Or (p, p')
adamc@1211 1292 end)
adamc@1212 1293 False (#Select r)
adamc@1212 1294 | AllCols oe =>
adamc@1212 1295 foldl (fn (si, p) =>
adamc@1212 1296 let
adamc@1212 1297 val p' = case si of
adamc@1212 1298 SqField (v, f) => Reln (Eq, [Proj (Proj (oe, v), f),
adamc@1214 1299 Proj (rvOf v, f)])
adamc@1212 1300 | SqExp (e, f) =>
adamc@1212 1301 case expIn e of
adamc@1212 1302 inr p => Cond (Proj (oe, f), p)
adamc@1212 1303 | inl e => Reln (Eq, [Proj (oe, f), e])
adamc@1212 1304 in
adamc@1212 1305 And (p, p')
adamc@1212 1306 end)
adamc@1212 1307 True (#Select r)),
adamc@1210 1308
adamc@1210 1309 case #Where r of
adamc@1210 1310 NONE => []
adamc@1214 1311 | SOME e => map (fn (v, f) => Proj (rvOf v, f)) (usedFields e))
adamc@1202 1312 end
adamc@1200 1313
adamc@1211 1314 fun evalPat env e (pt, _) =
adamc@1211 1315 case pt of
adamc@1211 1316 PWild => (env, True)
adamc@1211 1317 | PVar _ => (e :: env, True)
adamc@1211 1318 | PPrim _ => (env, True)
adamc@1215 1319 | PCon (_, pc, NONE) => (env, Reln (PCon0 (patCon pc), [e]))
adamc@1211 1320 | PCon (_, pc, SOME pt) =>
adamc@1211 1321 let
adamc@1215 1322 val (env, p) = evalPat env (Func (UnCon (patCon pc), [e])) pt
adamc@1211 1323 in
adamc@1215 1324 (env, And (p, Reln (PCon1 (patCon pc), [e])))
adamc@1211 1325 end
adamc@1211 1326 | PRecord xpts =>
adamc@1211 1327 foldl (fn ((x, pt, _), (env, p)) =>
adamc@1211 1328 let
adamc@1211 1329 val (env, p') = evalPat env (Proj (e, x)) pt
adamc@1211 1330 in
adamc@1211 1331 (env, And (p', p))
adamc@1211 1332 end) (env, True) xpts
adamc@1215 1333 | PNone _ => (env, Reln (PCon0 "None", [e]))
adamc@1211 1334 | PSome (_, pt) =>
adamc@1211 1335 let
adamc@1215 1336 val (env, p) = evalPat env (Func (UnCon "Some", [e])) pt
adamc@1211 1337 in
adamc@1215 1338 (env, And (p, Reln (PCon1 "Some", [e])))
adamc@1211 1339 end
adamc@1211 1340
adamc@1211 1341 fun peq (p1, p2) =
adamc@1211 1342 case (p1, p2) of
adamc@1211 1343 (True, True) => true
adamc@1211 1344 | (False, False) => true
adamc@1211 1345 | (Unknown, Unknown) => true
adamc@1211 1346 | (And (x1, y1), And (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
adamc@1211 1347 | (Or (x1, y1), Or (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
adamc@1211 1348 | (Reln (r1, es1), Reln (r2, es2)) => r1 = r2 andalso ListPair.allEq eeq (es1, es2)
adamc@1212 1349 | (Cond (e1, p1), Cond (e2, p2)) => eeq (e1, e2) andalso peq (p1, p2)
adamc@1211 1350 | _ => false
adamc@1211 1351
adamc@1211 1352 fun removeRedundant p1 =
adamc@1211 1353 let
adamc@1211 1354 fun rr p2 =
adamc@1211 1355 if peq (p1, p2) then
adamc@1211 1356 True
adamc@1211 1357 else
adamc@1211 1358 case p2 of
adamc@1211 1359 And (x, y) => And (rr x, rr y)
adamc@1211 1360 | Or (x, y) => Or (rr x, rr y)
adamc@1211 1361 | _ => p2
adamc@1211 1362 in
adamc@1211 1363 rr
adamc@1211 1364 end
adamc@1211 1365
adamc@1202 1366 fun evalExp env (e as (_, loc), st as (nv, p, sent)) =
adamc@1200 1367 let
adamc@1200 1368 fun default () =
adamc@1213 1369 ((*Print.preface ("Default" ^ Int.toString nv,
adamc@1213 1370 MonoPrint.p_exp MonoEnv.empty e);*)
adamc@1213 1371 (Var nv, (nv+1, p, sent)))
adamc@1200 1372
adamc@1200 1373 fun addSent (p, e, sent) =
adamc@1200 1374 if isKnown e then
adamc@1200 1375 sent
adamc@1200 1376 else
adamc@1202 1377 (loc, e, p) :: sent
adamc@1200 1378 in
adamc@1200 1379 case #1 e of
adamc@1200 1380 EPrim p => (Const p, st)
adamc@1200 1381 | ERel n => (List.nth (env, n), st)
adamc@1200 1382 | ENamed _ => default ()
adamc@1215 1383 | ECon (_, pc, NONE) => (Func (DtCon0 (patCon pc), []), st)
adamc@1200 1384 | ECon (_, pc, SOME e) =>
adamc@1200 1385 let
adamc@1200 1386 val (e, st) = evalExp env (e, st)
adamc@1200 1387 in
adamc@1215 1388 (Func (DtCon1 (patCon pc), [e]), st)
adamc@1200 1389 end
adamc@1215 1390 | ENone _ => (Func (DtCon0 "None", []), st)
adamc@1200 1391 | ESome (_, e) =>
adamc@1200 1392 let
adamc@1200 1393 val (e, st) = evalExp env (e, st)
adamc@1200 1394 in
adamc@1215 1395 (Func (DtCon1 "Some", [e]), st)
adamc@1200 1396 end
adamc@1200 1397 | EFfi _ => default ()
adamc@1213 1398
adamc@1200 1399 | EFfiApp (m, s, es) =>
adamc@1200 1400 if m = "Basis" andalso SS.member (writers, s) then
adamc@1200 1401 let
adamc@1200 1402 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1403 in
adamc@1214 1404 (Recd [], (#1 st, p, foldl (fn (e, sent) => addSent (#2 st, e, sent)) sent es))
adamc@1200 1405 end
adamc@1200 1406 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
adamc@1200 1407 default ()
adamc@1200 1408 else
adamc@1200 1409 let
adamc@1200 1410 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1411 in
adamc@1215 1412 (Func (Other (m ^ "." ^ s), es), st)
adamc@1200 1413 end
adamc@1213 1414
adamc@1213 1415 | EApp (e1, e2) =>
adamc@1213 1416 let
adamc@1213 1417 val (e1, st) = evalExp env (e1, st)
adamc@1213 1418 in
adamc@1213 1419 case e1 of
adamc@1213 1420 Finish => (Finish, st)
adamc@1213 1421 | _ => default ()
adamc@1213 1422 end
adamc@1213 1423
adamc@1200 1424 | EAbs _ => default ()
adamc@1200 1425 | EUnop (s, e1) =>
adamc@1200 1426 let
adamc@1200 1427 val (e1, st) = evalExp env (e1, st)
adamc@1200 1428 in
adamc@1215 1429 (Func (Other s, [e1]), st)
adamc@1200 1430 end
adamc@1200 1431 | EBinop (s, e1, e2) =>
adamc@1200 1432 let
adamc@1200 1433 val (e1, st) = evalExp env (e1, st)
adamc@1200 1434 val (e2, st) = evalExp env (e2, st)
adamc@1200 1435 in
adamc@1215 1436 (Func (Other s, [e1, e2]), st)
adamc@1200 1437 end
adamc@1200 1438 | ERecord xets =>
adamc@1200 1439 let
adamc@1200 1440 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
adamc@1200 1441 let
adamc@1200 1442 val (e, st) = evalExp env (e, st)
adamc@1200 1443 in
adamc@1200 1444 ((x, e), st)
adamc@1200 1445 end) st xets
adamc@1200 1446 in
adamc@1200 1447 (Recd xes, st)
adamc@1200 1448 end
adamc@1200 1449 | EField (e, s) =>
adamc@1200 1450 let
adamc@1200 1451 val (e, st) = evalExp env (e, st)
adamc@1200 1452 in
adamc@1200 1453 (Proj (e, s), st)
adamc@1200 1454 end
adamc@1211 1455 | ECase (e, pes, _) =>
adamc@1211 1456 let
adamc@1211 1457 val (e, st) = evalExp env (e, st)
adamc@1211 1458 val r = #1 st
adamc@1211 1459 val st = (r + 1, #2 st, #3 st)
adamc@1211 1460 val orig = #2 st
adamc@1211 1461
adamc@1211 1462 val st = foldl (fn ((pt, pe), st) =>
adamc@1211 1463 let
adamc@1211 1464 val (env, pp) = evalPat env e pt
adamc@1211 1465 val (pe, st') = evalExp env (pe, (#1 st, And (orig, pp), #3 st))
adamc@1211 1466
adamc@1211 1467 val this = And (removeRedundant orig (#2 st'), Reln (Eq, [Var r, pe]))
adamc@1211 1468 in
adamc@1211 1469 (#1 st', Or (#2 st, this), #3 st')
adamc@1211 1470 end) (#1 st, False, #3 st) pes
adamc@1211 1471 in
adamc@1211 1472 (Var r, (#1 st, And (orig, #2 st), #3 st))
adamc@1211 1473 end
adamc@1200 1474 | EStrcat (e1, e2) =>
adamc@1200 1475 let
adamc@1200 1476 val (e1, st) = evalExp env (e1, st)
adamc@1200 1477 val (e2, st) = evalExp env (e2, st)
adamc@1200 1478 in
adamc@1215 1479 (Func (Other "cat", [e1, e2]), st)
adamc@1200 1480 end
adamc@1200 1481 | EError _ => (Finish, st)
adamc@1200 1482 | EReturnBlob {blob = b, mimeType = m, ...} =>
adamc@1200 1483 let
adamc@1200 1484 val (b, st) = evalExp env (b, st)
adamc@1200 1485 val (m, st) = evalExp env (m, st)
adamc@1200 1486 in
adamc@1200 1487 (Finish, (#1 st, p, addSent (#2 st, b, addSent (#2 st, m, sent))))
adamc@1200 1488 end
adamc@1200 1489 | ERedirect (e, _) =>
adamc@1200 1490 let
adamc@1200 1491 val (e, st) = evalExp env (e, st)
adamc@1200 1492 in
adamc@1200 1493 (Finish, (#1 st, p, addSent (#2 st, e, sent)))
adamc@1200 1494 end
adamc@1200 1495 | EWrite e =>
adamc@1200 1496 let
adamc@1200 1497 val (e, st) = evalExp env (e, st)
adamc@1200 1498 in
adamc@1214 1499 (Recd [], (#1 st, p, addSent (#2 st, e, sent)))
adamc@1200 1500 end
adamc@1200 1501 | ESeq (e1, e2) =>
adamc@1200 1502 let
adamc@1200 1503 val (_, st) = evalExp env (e1, st)
adamc@1200 1504 in
adamc@1200 1505 evalExp env (e2, st)
adamc@1200 1506 end
adamc@1200 1507 | ELet (_, _, e1, e2) =>
adamc@1200 1508 let
adamc@1200 1509 val (e1, st) = evalExp env (e1, st)
adamc@1200 1510 in
adamc@1200 1511 evalExp (e1 :: env) (e2, st)
adamc@1200 1512 end
adamc@1200 1513 | EClosure (n, es) =>
adamc@1200 1514 let
adamc@1200 1515 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1516 in
adamc@1215 1517 (Func (Other ("Cl" ^ Int.toString n), es), st)
adamc@1200 1518 end
adamc@1200 1519
adamc@1200 1520 | EQuery {query = q, body = b, initial = i, ...} =>
adamc@1200 1521 let
adamc@1200 1522 val (_, st) = evalExp env (q, st)
adamc@1200 1523 val (i, st) = evalExp env (i, st)
adamc@1200 1524
adamc@1200 1525 val r = #1 st
adamc@1214 1526 val acc = #1 st + 1
adamc@1214 1527 val st' = (#1 st + 2, #2 st, #3 st)
adamc@1200 1528
adamc@1200 1529 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
adamc@1200 1530
adamc@1214 1531 val (rvN, count, qp, used) =
adamc@1214 1532 queryProp env
adamc@1214 1533 (#1 st') (fn rvN => (rvN + 1, Var rvN))
adamc@1214 1534 (AllCols (Var r)) q
adamc@1200 1535
adamc@1212 1536 val p' = And (qp, #2 st')
adamc@1200 1537
adamc@1212 1538 val (nvs, p, res) = if varInP acc (#2 st') then
adamc@1212 1539 (#1 st + 1, #2 st, Var r)
adamc@1212 1540 else
adamc@1212 1541 let
adamc@1214 1542 val out = rvN
adamc@1210 1543
adamc@1212 1544 val p = Or (Reln (Eq, [Var out, i]),
adamc@1212 1545 And (Reln (Eq, [Var out, b]),
adamc@1214 1546 And (Reln (Gt, [count,
adamc@1212 1547 Const (Prim.Int 0)]),
adamc@1212 1548 p')))
adamc@1212 1549 in
adamc@1212 1550 (out + 1, p, Var out)
adamc@1212 1551 end
adamc@1212 1552
adamc@1212 1553 val sent = map (fn (loc, e, p) => (loc, e, And (qp, p))) (#3 st')
adamc@1210 1554 val sent = map (fn e => (loc, e, p')) used @ sent
adamc@1200 1555 in
adamc@1212 1556 (res, (nvs, p, sent))
adamc@1200 1557 end
adamc@1200 1558 | EDml _ => default ()
adamc@1200 1559 | ENextval _ => default ()
adamc@1200 1560 | ESetval _ => default ()
adamc@1200 1561
adamc@1213 1562 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
adamc@1213 1563 (Var nv, (nv + 1, And (p, Reln (Known, [Var nv])), sent))
adamc@1213 1564
adamc@1200 1565 | EUnurlify _ => default ()
adamc@1200 1566 | EJavaScript _ => default ()
adamc@1200 1567 | ESignalReturn _ => default ()
adamc@1200 1568 | ESignalBind _ => default ()
adamc@1200 1569 | ESignalSource _ => default ()
adamc@1200 1570 | EServerCall _ => default ()
adamc@1200 1571 | ERecv _ => default ()
adamc@1200 1572 | ESleep _ => default ()
adamc@1200 1573 | ESpawn _ => default ()
adamc@1200 1574 end
adamc@1200 1575
adamc@1200 1576 fun check file =
adamc@1200 1577 let
adamc@1213 1578 val file = MonoReduce.reduce file
adamc@1213 1579 val file = MonoOpt.optimize file
adamc@1213 1580 val file = Fuse.fuse file
adamc@1213 1581 val file = MonoOpt.optimize file
adamc@1213 1582 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
adamc@1213 1583
adamc@1207 1584 val exptd = foldl (fn ((d, _), exptd) =>
adamc@1207 1585 case d of
adamc@1207 1586 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
adamc@1207 1587 | _ => exptd) IS.empty file
adamc@1207 1588
adamc@1202 1589 fun decl ((d, _), (vals, pols)) =
adamc@1200 1590 case d of
adamc@1207 1591 DVal (_, n, _, e, _) =>
adamc@1200 1592 let
adamc@1207 1593 val isExptd = IS.member (exptd, n)
adamc@1207 1594
adamc@1207 1595 fun deAbs (e, env, nv, p) =
adamc@1200 1596 case #1 e of
adamc@1207 1597 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1,
adamc@1207 1598 if isExptd then
adamc@1207 1599 And (p, Reln (Known, [Var nv]))
adamc@1207 1600 else
adamc@1207 1601 p)
adamc@1207 1602 | _ => (e, env, nv, p)
adamc@1200 1603
adamc@1207 1604 val (e, env, nv, p) = deAbs (e, [], 1, True)
adamc@1200 1605
adamc@1207 1606 val (e, (_, p, sent)) = evalExp env (e, (nv, p, []))
adamc@1200 1607 in
adamc@1207 1608 (sent @ vals, pols)
adamc@1200 1609 end
adamc@1202 1610
adamc@1214 1611 | DPolicy (PolClient e) => (vals, #3 (queryProp [] 0 (fn rvN => (rvN + 1, Lvar rvN))
adamc@1214 1612 (SomeCol (Var 0)) e) :: pols)
adamc@1214 1613
adamc@1202 1614 | _ => (vals, pols)
adamc@1202 1615
adamc@1203 1616 val () = reset ()
adamc@1202 1617
adamc@1202 1618 val (vals, pols) = foldl decl ([], []) file
adamc@1200 1619 in
adamc@1207 1620 app (fn (loc, e, p) =>
adamc@1207 1621 let
adamc@1213 1622 fun doOne e =
adamc@1213 1623 let
adamc@1213 1624 val p = And (p, Reln (Eq, [Var 0, e]))
adamc@1213 1625 in
adamc@1213 1626 if List.exists (fn pol => if imply (p, pol) then
adamc@1213 1627 (if !debug then
adamc@1213 1628 Print.prefaces "Match"
adamc@1213 1629 [("Hyp", p_prop p),
adamc@1213 1630 ("Goal", p_prop pol)]
adamc@1213 1631 else
adamc@1213 1632 ();
adamc@1213 1633 true)
adamc@1213 1634 else
adamc@1213 1635 false) pols then
adamc@1213 1636 ()
adamc@1213 1637 else
adamc@1213 1638 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
adamc@1213 1639 Print.preface ("The state satisifes this predicate:", p_prop p))
adamc@1213 1640 end
adamc@1213 1641
adamc@1213 1642 fun doAll e =
adamc@1213 1643 case e of
adamc@1213 1644 Const _ => ()
adamc@1213 1645 | Var _ => doOne e
adamc@1213 1646 | Lvar _ => raise Fail "Iflow.doAll: Lvar"
adamc@1215 1647 | Func (UnCon _, [e]) => doOne e
adamc@1215 1648 | Func (_, es) => app doAll es
adamc@1213 1649 | Recd xes => app (doAll o #2) xes
adamc@1213 1650 | Proj _ => doOne e
adamc@1213 1651 | Finish => ()
adamc@1207 1652 in
adamc@1213 1653 doAll e
adamc@1207 1654 end) vals
adamc@1200 1655 end
adamc@1200 1656
adamc@1213 1657 val check = fn file =>
adamc@1213 1658 let
adamc@1213 1659 val oldInline = Settings.getMonoInline ()
adamc@1213 1660 in
adamc@1213 1661 (Settings.setMonoInline (case Int.maxInt of
adamc@1213 1662 NONE => 1000000
adamc@1213 1663 | SOME n => n);
adamc@1213 1664 check file;
adamc@1213 1665 Settings.setMonoInline oldInline)
adamc@1213 1666 handle ex => (Settings.setMonoInline oldInline;
adamc@1213 1667 raise ex)
adamc@1213 1668 end
adamc@1213 1669
adamc@1200 1670 end
adamc@1213 1671