annotate src/iflow.sml @ 1216:09caa8c780e5

Some serious debugging of the new Cc
author Adam Chlipala <adamc@hcoop.net>
date Thu, 08 Apr 2010 14:20:46 -0400
parents 360f1ed0a969
children 4d206e603300
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@1216 549 Known = #Known (unNode r)})
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@1216 571 Known = #Known (unNode r)})
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@1216 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@1216 641 Known = #Known (unNode r)})
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@1216 651 Known = #Known (unNode r)})
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@1216 841 (*Print.prefaces "Bigger go" [("p1", p_prop p1),
adamc@1216 842 ("p2", p_prop p2)];*)
adamc@1215 843 decomp true (fn (e1, e2) => e1 andalso e2 ()) p1
adamc@1215 844 (fn hyps =>
adamc@1215 845 decomp false (fn (e1, e2) => e1 orelse e2 ()) p2
adamc@1215 846 (fn goals =>
adamc@1215 847 let
adamc@1215 848 fun gls goals onFail acc =
adamc@1215 849 case goals of
adamc@1215 850 [] =>
adamc@1215 851 (let
adamc@1215 852 val cc = Cc.database ()
adamc@1215 853 val () = app (fn a => Cc.assert (cc, a)) hyps
adamc@1215 854 in
adamc@1215 855 (List.all (fn a =>
adamc@1215 856 if Cc.check (cc, a) then
adamc@1215 857 true
adamc@1215 858 else
adamc@1215 859 ((*Print.prefaces "Can't prove"
adamc@1215 860 [("a", p_atom a),
adamc@1215 861 ("hyps", Print.p_list p_atom hyps),
adamc@1215 862 ("db", Cc.p_database cc)];*)
adamc@1215 863 false)) acc
adamc@1215 864 orelse onFail ())
adamc@1215 865 handle Cc.Contradiction => onFail ()
adamc@1216 866 end handle Cc.Undetermined => ((*print "Undetermined\n";*) onFail ()))
adamc@1216 867 | (g as AReln (Sql gf, [ge])) :: goals =>
adamc@1215 868 let
adamc@1215 869 fun hps hyps =
adamc@1215 870 case hyps of
adamc@1216 871 [] => gls goals onFail (g :: acc)
adamc@1215 872 | AReln (Sql hf, [he]) :: hyps =>
adamc@1215 873 if gf = hf then
adamc@1215 874 let
adamc@1215 875 val saved = save ()
adamc@1215 876 in
adamc@1215 877 if eq (ge, he) then
adamc@1215 878 let
adamc@1215 879 val changed = IM.numItems (!unif)
adamc@1215 880 <> IM.numItems saved
adamc@1215 881 in
adamc@1215 882 gls goals (fn () => (restore saved;
adamc@1215 883 changed
adamc@1215 884 andalso hps hyps))
adamc@1215 885 acc
adamc@1215 886 end
adamc@1215 887 else
adamc@1215 888 hps hyps
adamc@1215 889 end
adamc@1215 890 else
adamc@1215 891 hps hyps
adamc@1215 892 | _ :: hyps => hps hyps
adamc@1215 893 in
adamc@1215 894 hps hyps
adamc@1215 895 end
adamc@1215 896 | g :: goals => gls goals onFail (g :: acc)
adamc@1215 897 in
adamc@1216 898 (*Print.prefaces "Big go" [("hyps", Print.p_list p_atom hyps),
adamc@1216 899 ("goals", Print.p_list p_atom goals)];*)
adamc@1215 900 gls goals (fn () => false) []
adamc@1215 901 end handle Cc.Contradiction => true)))
adamc@1200 902
adamc@1200 903 fun patCon pc =
adamc@1200 904 case pc of
adamc@1200 905 PConVar n => "C" ^ Int.toString n
adamc@1200 906 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
adamc@1200 907
adamc@1200 908 datatype chunk =
adamc@1200 909 String of string
adamc@1200 910 | Exp of Mono.exp
adamc@1200 911
adamc@1200 912 fun chunkify e =
adamc@1200 913 case #1 e of
adamc@1200 914 EPrim (Prim.String s) => [String s]
adamc@1207 915 | EStrcat (e1, e2) =>
adamc@1207 916 let
adamc@1207 917 val chs1 = chunkify e1
adamc@1207 918 val chs2 = chunkify e2
adamc@1207 919 in
adamc@1207 920 case chs2 of
adamc@1207 921 String s2 :: chs2' =>
adamc@1207 922 (case List.last chs1 of
adamc@1207 923 String s1 => List.take (chs1, length chs1 - 1) @ String (s1 ^ s2) :: chs2'
adamc@1207 924 | _ => chs1 @ chs2)
adamc@1207 925 | _ => chs1 @ chs2
adamc@1207 926 end
adamc@1200 927 | _ => [Exp e]
adamc@1200 928
adamc@1201 929 type 'a parser = chunk list -> ('a * chunk list) option
adamc@1201 930
adamc@1201 931 fun always v chs = SOME (v, chs)
adamc@1201 932
adamc@1202 933 fun parse p s =
adamc@1202 934 case p (chunkify s) of
adamc@1201 935 SOME (v, []) => SOME v
adamc@1201 936 | _ => NONE
adamc@1201 937
adamc@1201 938 fun const s chs =
adamc@1201 939 case chs of
adamc@1201 940 String s' :: chs => if String.isPrefix s s' then
adamc@1201 941 SOME ((), if size s = size s' then
adamc@1201 942 chs
adamc@1201 943 else
adamc@1201 944 String (String.extract (s', size s, NONE)) :: chs)
adamc@1201 945 else
adamc@1201 946 NONE
adamc@1201 947 | _ => NONE
adamc@1201 948
adamc@1201 949 fun follow p1 p2 chs =
adamc@1201 950 case p1 chs of
adamc@1201 951 NONE => NONE
adamc@1201 952 | SOME (v1, chs) =>
adamc@1201 953 case p2 chs of
adamc@1201 954 NONE => NONE
adamc@1201 955 | SOME (v2, chs) => SOME ((v1, v2), chs)
adamc@1201 956
adamc@1201 957 fun wrap p f chs =
adamc@1201 958 case p chs of
adamc@1201 959 NONE => NONE
adamc@1201 960 | SOME (v, chs) => SOME (f v, chs)
adamc@1201 961
adamc@1209 962 fun wrapP p f chs =
adamc@1209 963 case p chs of
adamc@1209 964 NONE => NONE
adamc@1209 965 | SOME (v, chs) =>
adamc@1209 966 case f v of
adamc@1209 967 NONE => NONE
adamc@1209 968 | SOME r => SOME (r, chs)
adamc@1209 969
adamc@1201 970 fun alt p1 p2 chs =
adamc@1201 971 case p1 chs of
adamc@1201 972 NONE => p2 chs
adamc@1201 973 | v => v
adamc@1201 974
adamc@1207 975 fun altL ps =
adamc@1207 976 case rev ps of
adamc@1207 977 [] => (fn _ => NONE)
adamc@1207 978 | p :: ps =>
adamc@1207 979 foldl (fn (p1, p2) => alt p1 p2) p ps
adamc@1207 980
adamc@1204 981 fun opt p chs =
adamc@1204 982 case p chs of
adamc@1204 983 NONE => SOME (NONE, chs)
adamc@1204 984 | SOME (v, chs) => SOME (SOME v, chs)
adamc@1204 985
adamc@1201 986 fun skip cp chs =
adamc@1201 987 case chs of
adamc@1201 988 String "" :: chs => skip cp chs
adamc@1201 989 | String s :: chs' => if cp (String.sub (s, 0)) then
adamc@1201 990 skip cp (String (String.extract (s, 1, NONE)) :: chs')
adamc@1201 991 else
adamc@1201 992 SOME ((), chs)
adamc@1201 993 | _ => SOME ((), chs)
adamc@1201 994
adamc@1201 995 fun keep cp chs =
adamc@1201 996 case chs of
adamc@1201 997 String "" :: chs => keep cp chs
adamc@1201 998 | String s :: chs' =>
adamc@1201 999 let
adamc@1201 1000 val (befor, after) = Substring.splitl cp (Substring.full s)
adamc@1201 1001 in
adamc@1201 1002 if Substring.isEmpty befor then
adamc@1201 1003 NONE
adamc@1201 1004 else
adamc@1201 1005 SOME (Substring.string befor,
adamc@1201 1006 if Substring.isEmpty after then
adamc@1201 1007 chs'
adamc@1201 1008 else
adamc@1201 1009 String (Substring.string after) :: chs')
adamc@1201 1010 end
adamc@1201 1011 | _ => NONE
adamc@1201 1012
adamc@1204 1013 fun ws p = wrap (follow (skip (fn ch => ch = #" "))
adamc@1204 1014 (follow p (skip (fn ch => ch = #" ")))) (#1 o #2)
adamc@1204 1015
adamc@1204 1016 fun log name p chs =
adamc@1206 1017 (if !debug then
adamc@1206 1018 case chs of
adamc@1207 1019 String s :: _ => print (name ^ ": " ^ s ^ "\n")
adamc@1206 1020 | _ => print (name ^ ": blocked!\n")
adamc@1206 1021 else
adamc@1206 1022 ();
adamc@1204 1023 p chs)
adamc@1201 1024
adamc@1201 1025 fun list p chs =
adamc@1207 1026 altL [wrap (follow p (follow (ws (const ",")) (list p)))
adamc@1207 1027 (fn (v, ((), ls)) => v :: ls),
adamc@1207 1028 wrap (ws p) (fn v => [v]),
adamc@1207 1029 always []] chs
adamc@1201 1030
adamc@1201 1031 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
adamc@1201 1032
adamc@1211 1033 val t_ident = wrapP ident (fn s => if String.isPrefix "T_" s then
adamc@1211 1034 SOME (String.extract (s, 2, NONE))
adamc@1201 1035 else
adamc@1211 1036 NONE)
adamc@1211 1037 val uw_ident = wrapP ident (fn s => if String.isPrefix "uw_" s andalso size s >= 4 then
adamc@1211 1038 SOME (str (Char.toUpper (String.sub (s, 3)))
adamc@1211 1039 ^ String.extract (s, 4, NONE))
adamc@1211 1040 else
adamc@1211 1041 NONE)
adamc@1201 1042
adamc@1211 1043 val field = wrap (follow t_ident
adamc@1201 1044 (follow (const ".")
adamc@1201 1045 uw_ident))
adamc@1201 1046 (fn (t, ((), f)) => (t, f))
adamc@1201 1047
adamc@1206 1048 datatype Rel =
adamc@1206 1049 Exps of exp * exp -> prop
adamc@1206 1050 | Props of prop * prop -> prop
adamc@1206 1051
adamc@1204 1052 datatype sqexp =
adamc@1206 1053 SqConst of Prim.t
adamc@1206 1054 | Field of string * string
adamc@1206 1055 | Binop of Rel * sqexp * sqexp
adamc@1207 1056 | SqKnown of sqexp
adamc@1207 1057 | Inj of Mono.exp
adamc@1211 1058 | SqFunc of string * sqexp
adamc@1211 1059 | Count
adamc@1204 1060
adamc@1210 1061 fun cmp s r = wrap (const s) (fn () => Exps (fn (e1, e2) => Reln (r, [e1, e2])))
adamc@1210 1062
adamc@1210 1063 val sqbrel = altL [cmp "=" Eq,
adamc@1210 1064 cmp "<>" Ne,
adamc@1210 1065 cmp "<=" Le,
adamc@1210 1066 cmp "<" Lt,
adamc@1210 1067 cmp ">=" Ge,
adamc@1210 1068 cmp ">" Gt,
adamc@1207 1069 wrap (const "AND") (fn () => Props And),
adamc@1207 1070 wrap (const "OR") (fn () => Props Or)]
adamc@1204 1071
adamc@1204 1072 datatype ('a, 'b) sum = inl of 'a | inr of 'b
adamc@1204 1073
adamc@1209 1074 fun string chs =
adamc@1206 1075 case chs of
adamc@1209 1076 String s :: chs =>
adamc@1209 1077 if size s >= 2 andalso String.sub (s, 0) = #"'" then
adamc@1209 1078 let
adamc@1209 1079 fun loop (cs, acc) =
adamc@1209 1080 case cs of
adamc@1209 1081 [] => NONE
adamc@1209 1082 | c :: cs =>
adamc@1209 1083 if c = #"'" then
adamc@1209 1084 SOME (String.implode (rev acc), cs)
adamc@1209 1085 else if c = #"\\" then
adamc@1209 1086 case cs of
adamc@1209 1087 c :: cs => loop (cs, c :: acc)
adamc@1209 1088 | _ => raise Fail "Iflow.string: Unmatched backslash escape"
adamc@1209 1089 else
adamc@1209 1090 loop (cs, c :: acc)
adamc@1209 1091 in
adamc@1209 1092 case loop (String.explode (String.extract (s, 1, NONE)), []) of
adamc@1209 1093 NONE => NONE
adamc@1209 1094 | SOME (s, []) => SOME (s, chs)
adamc@1209 1095 | SOME (s, cs) => SOME (s, String (String.implode cs) :: chs)
adamc@1209 1096 end
adamc@1209 1097 else
adamc@1209 1098 NONE
adamc@1209 1099 | _ => NONE
adamc@1206 1100
adamc@1209 1101 val prim =
adamc@1209 1102 altL [wrap (follow (wrapP (follow (keep Char.isDigit) (follow (const ".") (keep Char.isDigit)))
adamc@1209 1103 (fn (x, ((), y)) => Option.map Prim.Float (Real64.fromString (x ^ "." ^ y))))
adamc@1209 1104 (opt (const "::float8"))) #1,
adamc@1209 1105 wrap (follow (wrapP (keep Char.isDigit)
adamc@1209 1106 (Option.map Prim.Int o Int64.fromString))
adamc@1209 1107 (opt (const "::int8"))) #1,
adamc@1209 1108 wrap (follow (opt (const "E")) (follow string (opt (const "::text"))))
adamc@1209 1109 (Prim.String o #1 o #2)]
adamc@1206 1110
adamc@1207 1111 fun known' chs =
adamc@1207 1112 case chs of
adamc@1207 1113 Exp (EFfi ("Basis", "sql_known"), _) :: chs => SOME ((), chs)
adamc@1207 1114 | _ => NONE
adamc@1207 1115
adamc@1207 1116 fun sqlify chs =
adamc@1207 1117 case chs of
adamc@1207 1118 Exp (EFfiApp ("Basis", f, [e]), _) :: chs =>
adamc@1207 1119 if String.isPrefix "sqlify" f then
adamc@1207 1120 SOME (e, chs)
adamc@1207 1121 else
adamc@1207 1122 NONE
adamc@1207 1123 | _ => NONE
adamc@1207 1124
adamc@1211 1125 fun constK s = wrap (const s) (fn () => s)
adamc@1211 1126
adamc@1211 1127 val funcName = altL [constK "COUNT",
adamc@1211 1128 constK "MIN",
adamc@1211 1129 constK "MAX",
adamc@1211 1130 constK "SUM",
adamc@1211 1131 constK "AVG"]
adamc@1211 1132
adamc@1204 1133 fun sqexp chs =
adamc@1206 1134 log "sqexp"
adamc@1207 1135 (altL [wrap prim SqConst,
adamc@1211 1136 wrap field Field,
adamc@1207 1137 wrap known SqKnown,
adamc@1211 1138 wrap func SqFunc,
adamc@1211 1139 wrap (const "COUNT(*)") (fn () => Count),
adamc@1207 1140 wrap sqlify Inj,
adamc@1211 1141 wrap (follow (const "COALESCE(") (follow sqexp (follow (const ",")
adamc@1211 1142 (follow (keep (fn ch => ch <> #")")) (const ")")))))
adamc@1211 1143 (fn ((), (e, _)) => e),
adamc@1207 1144 wrap (follow (ws (const "("))
adamc@1207 1145 (follow (wrap
adamc@1207 1146 (follow sqexp
adamc@1207 1147 (alt
adamc@1207 1148 (wrap
adamc@1207 1149 (follow (ws sqbrel)
adamc@1207 1150 (ws sqexp))
adamc@1207 1151 inl)
adamc@1207 1152 (always (inr ()))))
adamc@1207 1153 (fn (e1, sm) =>
adamc@1207 1154 case sm of
adamc@1207 1155 inl (bo, e2) => Binop (bo, e1, e2)
adamc@1207 1156 | inr () => e1))
adamc@1207 1157 (const ")")))
adamc@1207 1158 (fn ((), (e, ())) => e)])
adamc@1207 1159 chs
adamc@1206 1160
adamc@1207 1161 and known chs = wrap (follow known' (follow (const "(") (follow sqexp (const ")"))))
adamc@1211 1162 (fn ((), ((), (e, ()))) => e) chs
adamc@1211 1163
adamc@1211 1164 and func chs = wrap (follow funcName (follow (const "(") (follow sqexp (const ")"))))
adamc@1211 1165 (fn (f, ((), (e, ()))) => (f, e)) chs
adamc@1211 1166
adamc@1211 1167 datatype sitem =
adamc@1211 1168 SqField of string * string
adamc@1211 1169 | SqExp of sqexp * string
adamc@1211 1170
adamc@1211 1171 val sitem = alt (wrap field SqField)
adamc@1211 1172 (wrap (follow sqexp (follow (const " AS ") uw_ident))
adamc@1211 1173 (fn (e, ((), s)) => SqExp (e, s)))
adamc@1207 1174
adamc@1207 1175 val select = log "select"
adamc@1207 1176 (wrap (follow (const "SELECT ") (list sitem))
adamc@1207 1177 (fn ((), ls) => ls))
adamc@1201 1178
adamc@1201 1179 val fitem = wrap (follow uw_ident
adamc@1201 1180 (follow (const " AS ")
adamc@1201 1181 t_ident))
adamc@1201 1182 (fn (t, ((), f)) => (t, f))
adamc@1201 1183
adamc@1207 1184 val from = log "from"
adamc@1207 1185 (wrap (follow (const "FROM ") (list fitem))
adamc@1207 1186 (fn ((), ls) => ls))
adamc@1201 1187
adamc@1204 1188 val wher = wrap (follow (ws (const "WHERE ")) sqexp)
adamc@1204 1189 (fn ((), ls) => ls)
adamc@1204 1190
adamc@1207 1191 val query = log "query"
adamc@1207 1192 (wrap (follow (follow select from) (opt wher))
adamc@1207 1193 (fn ((fs, ts), wher) => {Select = fs, From = ts, Where = wher}))
adamc@1201 1194
adamc@1215 1195 fun removeDups (ls : (string * string) list) =
adamc@1211 1196 case ls of
adamc@1211 1197 [] => []
adamc@1211 1198 | x :: ls =>
adamc@1211 1199 let
adamc@1211 1200 val ls = removeDups ls
adamc@1211 1201 in
adamc@1211 1202 if List.exists (fn x' => x' = x) ls then
adamc@1211 1203 ls
adamc@1211 1204 else
adamc@1211 1205 x :: ls
adamc@1211 1206 end
adamc@1211 1207
adamc@1212 1208 datatype queryMode =
adamc@1212 1209 SomeCol of exp
adamc@1212 1210 | AllCols of exp
adamc@1212 1211
adamc@1216 1212 exception Default
adamc@1216 1213
adamc@1214 1214 fun queryProp env rvN rv oe e =
adamc@1216 1215 let
adamc@1216 1216 fun default () = (print ("Warning: Information flow checker can't parse SQL query at "
adamc@1216 1217 ^ ErrorMsg.spanToString (#2 e) ^ "\n");
adamc@1216 1218 (rvN, Unknown, []))
adamc@1216 1219 in
adamc@1216 1220 case parse query e of
adamc@1216 1221 NONE => default ()
adamc@1216 1222 | SOME r =>
adamc@1216 1223 let
adamc@1216 1224 val (rvs, rvN) = ListUtil.foldlMap (fn ((_, v), rvN) =>
adamc@1216 1225 let
adamc@1216 1226 val (rvN, e) = rv rvN
adamc@1216 1227 in
adamc@1216 1228 ((v, e), rvN)
adamc@1216 1229 end) rvN (#From r)
adamc@1214 1230
adamc@1216 1231 fun rvOf v =
adamc@1216 1232 case List.find (fn (v', _) => v' = v) rvs of
adamc@1216 1233 NONE => raise Fail "Iflow.queryProp: Bad table variable"
adamc@1216 1234 | SOME (_, e) => e
adamc@1214 1235
adamc@1216 1236 fun usedFields e =
adamc@1216 1237 case e of
adamc@1216 1238 SqConst _ => []
adamc@1216 1239 | Field (v, f) => [(v, f)]
adamc@1216 1240 | Binop (_, e1, e2) => removeDups (usedFields e1 @ usedFields e2)
adamc@1216 1241 | SqKnown _ => []
adamc@1216 1242 | Inj _ => []
adamc@1216 1243 | SqFunc (_, e) => usedFields e
adamc@1216 1244 | Count => []
adamc@1214 1245
adamc@1216 1246 val p =
adamc@1216 1247 foldl (fn ((t, v), p) => And (p, Reln (Sql t, [rvOf v]))) True (#From r)
adamc@1211 1248
adamc@1216 1249 fun expIn e =
adamc@1216 1250 case e of
adamc@1216 1251 SqConst p => inl (Const p)
adamc@1216 1252 | Field (v, f) => inl (Proj (rvOf v, f))
adamc@1216 1253 | Binop (bo, e1, e2) =>
adamc@1216 1254 inr (case (bo, expIn e1, expIn e2) of
adamc@1216 1255 (Exps f, inl e1, inl e2) => f (e1, e2)
adamc@1216 1256 | (Props f, inr p1, inr p2) => f (p1, p2)
adamc@1216 1257 | _ => Unknown)
adamc@1216 1258 | SqKnown e =>
adamc@1216 1259 inr (case expIn e of
adamc@1216 1260 inl e => Reln (Known, [e])
adamc@1216 1261 | _ => Unknown)
adamc@1216 1262 | Inj e =>
adamc@1216 1263 let
adamc@1216 1264 fun deinj (e, _) =
adamc@1216 1265 case e of
adamc@1216 1266 ERel n => List.nth (env, n)
adamc@1216 1267 | EField (e, f) => Proj (deinj e, f)
adamc@1216 1268 | _ => raise Fail "Iflow: non-variable injected into query"
adamc@1216 1269 in
adamc@1216 1270 inl (deinj e)
adamc@1216 1271 end
adamc@1216 1272 | SqFunc (f, e) =>
adamc@1216 1273 inl (case expIn e of
adamc@1216 1274 inl e => Func (Other f, [e])
adamc@1216 1275 | _ => raise Fail ("Iflow: non-expresion passed to function " ^ f))
adamc@1216 1276 | Count => raise Default
adamc@1205 1277
adamc@1216 1278 val p = case #Where r of
adamc@1216 1279 NONE => p
adamc@1216 1280 | SOME e =>
adamc@1216 1281 case expIn e of
adamc@1216 1282 inr p' => And (p, p')
adamc@1216 1283 | _ => p
adamc@1210 1284
adamc@1216 1285 fun normal () =
adamc@1216 1286 (rvN,
adamc@1216 1287 And (p, case oe of
adamc@1216 1288 SomeCol oe =>
adamc@1216 1289 foldl (fn (si, p) =>
adamc@1216 1290 let
adamc@1216 1291 val p' = case si of
adamc@1216 1292 SqField (v, f) => Reln (Eq, [oe, Proj (rvOf v, f)])
adamc@1216 1293 | SqExp (e, f) =>
adamc@1216 1294 case expIn e of
adamc@1216 1295 inr _ => Unknown
adamc@1216 1296 | inl e => Reln (Eq, [oe, e])
adamc@1216 1297 in
adamc@1216 1298 Or (p, p')
adamc@1216 1299 end)
adamc@1216 1300 False (#Select r)
adamc@1216 1301 | AllCols oe =>
adamc@1216 1302 foldl (fn (si, p) =>
adamc@1216 1303 let
adamc@1216 1304 val p' = case si of
adamc@1216 1305 SqField (v, f) => Reln (Eq, [Proj (Proj (oe, v), f),
adamc@1216 1306 Proj (rvOf v, f)])
adamc@1216 1307 | SqExp (e, f) =>
adamc@1216 1308 case expIn e of
adamc@1216 1309 inr p => Cond (Proj (oe, f), p)
adamc@1216 1310 | inl e => Reln (Eq, [Proj (oe, f), e])
adamc@1216 1311 in
adamc@1216 1312 And (p, p')
adamc@1216 1313 end)
adamc@1216 1314 True (#Select r)),
adamc@1216 1315 case #Where r of
adamc@1216 1316 NONE => []
adamc@1216 1317 | SOME e => map (fn (v, f) => Proj (rvOf v, f)) (usedFields e))
adamc@1216 1318 in
adamc@1216 1319 case #Select r of
adamc@1216 1320 [SqExp (Binop (Exps bo, Count, SqConst (Prim.Int 0)), f)] =>
adamc@1216 1321 (case bo (Const (Prim.Int 1), Const (Prim.Int 2)) of
adamc@1216 1322 Reln (Gt, [Const (Prim.Int 1), Const (Prim.Int 2)]) =>
adamc@1216 1323 let
adamc@1216 1324 val oe = case oe of
adamc@1216 1325 SomeCol oe => oe
adamc@1216 1326 | AllCols oe => Proj (oe, f)
adamc@1216 1327 in
adamc@1216 1328 (rvN,
adamc@1216 1329 Or (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.False", [])]),
adamc@1216 1330 And (Reln (Eq, [oe, Func (DtCon0 "Basis.bool.True", [])]),
adamc@1216 1331 p)),
adamc@1216 1332 [])
adamc@1216 1333 end
adamc@1216 1334 | _ => normal ())
adamc@1216 1335 | _ => normal ()
adamc@1216 1336 end
adamc@1216 1337 handle Default => default ()
adamc@1216 1338 end
adamc@1200 1339
adamc@1211 1340 fun evalPat env e (pt, _) =
adamc@1211 1341 case pt of
adamc@1211 1342 PWild => (env, True)
adamc@1211 1343 | PVar _ => (e :: env, True)
adamc@1211 1344 | PPrim _ => (env, True)
adamc@1215 1345 | PCon (_, pc, NONE) => (env, Reln (PCon0 (patCon pc), [e]))
adamc@1211 1346 | PCon (_, pc, SOME pt) =>
adamc@1211 1347 let
adamc@1215 1348 val (env, p) = evalPat env (Func (UnCon (patCon pc), [e])) pt
adamc@1211 1349 in
adamc@1215 1350 (env, And (p, Reln (PCon1 (patCon pc), [e])))
adamc@1211 1351 end
adamc@1211 1352 | PRecord xpts =>
adamc@1211 1353 foldl (fn ((x, pt, _), (env, p)) =>
adamc@1211 1354 let
adamc@1211 1355 val (env, p') = evalPat env (Proj (e, x)) pt
adamc@1211 1356 in
adamc@1211 1357 (env, And (p', p))
adamc@1211 1358 end) (env, True) xpts
adamc@1215 1359 | PNone _ => (env, Reln (PCon0 "None", [e]))
adamc@1211 1360 | PSome (_, pt) =>
adamc@1211 1361 let
adamc@1215 1362 val (env, p) = evalPat env (Func (UnCon "Some", [e])) pt
adamc@1211 1363 in
adamc@1215 1364 (env, And (p, Reln (PCon1 "Some", [e])))
adamc@1211 1365 end
adamc@1211 1366
adamc@1211 1367 fun peq (p1, p2) =
adamc@1211 1368 case (p1, p2) of
adamc@1211 1369 (True, True) => true
adamc@1211 1370 | (False, False) => true
adamc@1211 1371 | (Unknown, Unknown) => true
adamc@1211 1372 | (And (x1, y1), And (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
adamc@1211 1373 | (Or (x1, y1), Or (x2, y2)) => peq (x1, x2) andalso peq (y1, y2)
adamc@1211 1374 | (Reln (r1, es1), Reln (r2, es2)) => r1 = r2 andalso ListPair.allEq eeq (es1, es2)
adamc@1212 1375 | (Cond (e1, p1), Cond (e2, p2)) => eeq (e1, e2) andalso peq (p1, p2)
adamc@1211 1376 | _ => false
adamc@1211 1377
adamc@1211 1378 fun removeRedundant p1 =
adamc@1211 1379 let
adamc@1211 1380 fun rr p2 =
adamc@1211 1381 if peq (p1, p2) then
adamc@1211 1382 True
adamc@1211 1383 else
adamc@1211 1384 case p2 of
adamc@1211 1385 And (x, y) => And (rr x, rr y)
adamc@1211 1386 | Or (x, y) => Or (rr x, rr y)
adamc@1211 1387 | _ => p2
adamc@1211 1388 in
adamc@1211 1389 rr
adamc@1211 1390 end
adamc@1211 1391
adamc@1202 1392 fun evalExp env (e as (_, loc), st as (nv, p, sent)) =
adamc@1200 1393 let
adamc@1200 1394 fun default () =
adamc@1213 1395 ((*Print.preface ("Default" ^ Int.toString nv,
adamc@1213 1396 MonoPrint.p_exp MonoEnv.empty e);*)
adamc@1213 1397 (Var nv, (nv+1, p, sent)))
adamc@1200 1398
adamc@1200 1399 fun addSent (p, e, sent) =
adamc@1200 1400 if isKnown e then
adamc@1200 1401 sent
adamc@1200 1402 else
adamc@1202 1403 (loc, e, p) :: sent
adamc@1200 1404 in
adamc@1200 1405 case #1 e of
adamc@1200 1406 EPrim p => (Const p, st)
adamc@1200 1407 | ERel n => (List.nth (env, n), st)
adamc@1200 1408 | ENamed _ => default ()
adamc@1215 1409 | ECon (_, pc, NONE) => (Func (DtCon0 (patCon pc), []), st)
adamc@1200 1410 | ECon (_, pc, SOME e) =>
adamc@1200 1411 let
adamc@1200 1412 val (e, st) = evalExp env (e, st)
adamc@1200 1413 in
adamc@1215 1414 (Func (DtCon1 (patCon pc), [e]), st)
adamc@1200 1415 end
adamc@1215 1416 | ENone _ => (Func (DtCon0 "None", []), st)
adamc@1200 1417 | ESome (_, e) =>
adamc@1200 1418 let
adamc@1200 1419 val (e, st) = evalExp env (e, st)
adamc@1200 1420 in
adamc@1215 1421 (Func (DtCon1 "Some", [e]), st)
adamc@1200 1422 end
adamc@1200 1423 | EFfi _ => default ()
adamc@1213 1424
adamc@1200 1425 | EFfiApp (m, s, es) =>
adamc@1200 1426 if m = "Basis" andalso SS.member (writers, s) then
adamc@1200 1427 let
adamc@1200 1428 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1429 in
adamc@1214 1430 (Recd [], (#1 st, p, foldl (fn (e, sent) => addSent (#2 st, e, sent)) sent es))
adamc@1200 1431 end
adamc@1200 1432 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
adamc@1200 1433 default ()
adamc@1200 1434 else
adamc@1200 1435 let
adamc@1200 1436 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1437 in
adamc@1215 1438 (Func (Other (m ^ "." ^ s), es), st)
adamc@1200 1439 end
adamc@1213 1440
adamc@1213 1441 | EApp (e1, e2) =>
adamc@1213 1442 let
adamc@1213 1443 val (e1, st) = evalExp env (e1, st)
adamc@1213 1444 in
adamc@1213 1445 case e1 of
adamc@1213 1446 Finish => (Finish, st)
adamc@1213 1447 | _ => default ()
adamc@1213 1448 end
adamc@1213 1449
adamc@1200 1450 | EAbs _ => default ()
adamc@1200 1451 | EUnop (s, e1) =>
adamc@1200 1452 let
adamc@1200 1453 val (e1, st) = evalExp env (e1, st)
adamc@1200 1454 in
adamc@1215 1455 (Func (Other s, [e1]), st)
adamc@1200 1456 end
adamc@1200 1457 | EBinop (s, e1, e2) =>
adamc@1200 1458 let
adamc@1200 1459 val (e1, st) = evalExp env (e1, st)
adamc@1200 1460 val (e2, st) = evalExp env (e2, st)
adamc@1200 1461 in
adamc@1215 1462 (Func (Other s, [e1, e2]), st)
adamc@1200 1463 end
adamc@1200 1464 | ERecord xets =>
adamc@1200 1465 let
adamc@1200 1466 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
adamc@1200 1467 let
adamc@1200 1468 val (e, st) = evalExp env (e, st)
adamc@1200 1469 in
adamc@1200 1470 ((x, e), st)
adamc@1200 1471 end) st xets
adamc@1200 1472 in
adamc@1200 1473 (Recd xes, st)
adamc@1200 1474 end
adamc@1200 1475 | EField (e, s) =>
adamc@1200 1476 let
adamc@1200 1477 val (e, st) = evalExp env (e, st)
adamc@1200 1478 in
adamc@1200 1479 (Proj (e, s), st)
adamc@1200 1480 end
adamc@1211 1481 | ECase (e, pes, _) =>
adamc@1211 1482 let
adamc@1211 1483 val (e, st) = evalExp env (e, st)
adamc@1211 1484 val r = #1 st
adamc@1211 1485 val st = (r + 1, #2 st, #3 st)
adamc@1211 1486 val orig = #2 st
adamc@1211 1487
adamc@1211 1488 val st = foldl (fn ((pt, pe), st) =>
adamc@1211 1489 let
adamc@1211 1490 val (env, pp) = evalPat env e pt
adamc@1211 1491 val (pe, st') = evalExp env (pe, (#1 st, And (orig, pp), #3 st))
adamc@1216 1492 (*val () = Print.prefaces "Case" [("loc", Print.PD.string
adamc@1216 1493 (ErrorMsg.spanToString (#2 pt))),
adamc@1216 1494 ("env", Print.p_list p_exp env),
adamc@1216 1495 ("sent", Print.p_list_sep Print.PD.newline
adamc@1216 1496 (fn (loc, e, p) =>
adamc@1216 1497 Print.box [Print.PD.string
adamc@1216 1498 (ErrorMsg.spanToString loc),
adamc@1216 1499 Print.PD.string ":",
adamc@1216 1500 Print.space,
adamc@1216 1501 p_exp e,
adamc@1216 1502 Print.space,
adamc@1216 1503 Print.PD.string "in",
adamc@1216 1504 Print.space,
adamc@1216 1505 p_prop p])
adamc@1216 1506 (List.take (#3 st', length (#3 st')
adamc@1216 1507 - length (#3 st))))]*)
adamc@1211 1508
adamc@1211 1509 val this = And (removeRedundant orig (#2 st'), Reln (Eq, [Var r, pe]))
adamc@1211 1510 in
adamc@1211 1511 (#1 st', Or (#2 st, this), #3 st')
adamc@1211 1512 end) (#1 st, False, #3 st) pes
adamc@1211 1513 in
adamc@1211 1514 (Var r, (#1 st, And (orig, #2 st), #3 st))
adamc@1211 1515 end
adamc@1200 1516 | EStrcat (e1, e2) =>
adamc@1200 1517 let
adamc@1200 1518 val (e1, st) = evalExp env (e1, st)
adamc@1200 1519 val (e2, st) = evalExp env (e2, st)
adamc@1200 1520 in
adamc@1215 1521 (Func (Other "cat", [e1, e2]), st)
adamc@1200 1522 end
adamc@1200 1523 | EError _ => (Finish, st)
adamc@1200 1524 | EReturnBlob {blob = b, mimeType = m, ...} =>
adamc@1200 1525 let
adamc@1200 1526 val (b, st) = evalExp env (b, st)
adamc@1200 1527 val (m, st) = evalExp env (m, st)
adamc@1200 1528 in
adamc@1200 1529 (Finish, (#1 st, p, addSent (#2 st, b, addSent (#2 st, m, sent))))
adamc@1200 1530 end
adamc@1200 1531 | ERedirect (e, _) =>
adamc@1200 1532 let
adamc@1200 1533 val (e, st) = evalExp env (e, st)
adamc@1200 1534 in
adamc@1200 1535 (Finish, (#1 st, p, addSent (#2 st, e, sent)))
adamc@1200 1536 end
adamc@1200 1537 | EWrite e =>
adamc@1200 1538 let
adamc@1200 1539 val (e, st) = evalExp env (e, st)
adamc@1200 1540 in
adamc@1214 1541 (Recd [], (#1 st, p, addSent (#2 st, e, sent)))
adamc@1200 1542 end
adamc@1200 1543 | ESeq (e1, e2) =>
adamc@1200 1544 let
adamc@1200 1545 val (_, st) = evalExp env (e1, st)
adamc@1200 1546 in
adamc@1200 1547 evalExp env (e2, st)
adamc@1200 1548 end
adamc@1200 1549 | ELet (_, _, e1, e2) =>
adamc@1200 1550 let
adamc@1200 1551 val (e1, st) = evalExp env (e1, st)
adamc@1200 1552 in
adamc@1200 1553 evalExp (e1 :: env) (e2, st)
adamc@1200 1554 end
adamc@1200 1555 | EClosure (n, es) =>
adamc@1200 1556 let
adamc@1200 1557 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 1558 in
adamc@1215 1559 (Func (Other ("Cl" ^ Int.toString n), es), st)
adamc@1200 1560 end
adamc@1200 1561
adamc@1200 1562 | EQuery {query = q, body = b, initial = i, ...} =>
adamc@1200 1563 let
adamc@1200 1564 val (_, st) = evalExp env (q, st)
adamc@1200 1565 val (i, st) = evalExp env (i, st)
adamc@1200 1566
adamc@1200 1567 val r = #1 st
adamc@1214 1568 val acc = #1 st + 1
adamc@1214 1569 val st' = (#1 st + 2, #2 st, #3 st)
adamc@1200 1570
adamc@1200 1571 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
adamc@1200 1572
adamc@1216 1573 val (rvN, qp, used) =
adamc@1214 1574 queryProp env
adamc@1214 1575 (#1 st') (fn rvN => (rvN + 1, Var rvN))
adamc@1214 1576 (AllCols (Var r)) q
adamc@1200 1577
adamc@1212 1578 val p' = And (qp, #2 st')
adamc@1200 1579
adamc@1212 1580 val (nvs, p, res) = if varInP acc (#2 st') then
adamc@1212 1581 (#1 st + 1, #2 st, Var r)
adamc@1212 1582 else
adamc@1212 1583 let
adamc@1214 1584 val out = rvN
adamc@1210 1585
adamc@1212 1586 val p = Or (Reln (Eq, [Var out, i]),
adamc@1212 1587 And (Reln (Eq, [Var out, b]),
adamc@1216 1588 p'))
adamc@1212 1589 in
adamc@1212 1590 (out + 1, p, Var out)
adamc@1212 1591 end
adamc@1212 1592
adamc@1212 1593 val sent = map (fn (loc, e, p) => (loc, e, And (qp, p))) (#3 st')
adamc@1210 1594 val sent = map (fn e => (loc, e, p')) used @ sent
adamc@1200 1595 in
adamc@1212 1596 (res, (nvs, p, sent))
adamc@1200 1597 end
adamc@1200 1598 | EDml _ => default ()
adamc@1200 1599 | ENextval _ => default ()
adamc@1200 1600 | ESetval _ => default ()
adamc@1200 1601
adamc@1213 1602 | EUnurlify ((EFfiApp ("Basis", "get_cookie", _), _), _, _) =>
adamc@1213 1603 (Var nv, (nv + 1, And (p, Reln (Known, [Var nv])), sent))
adamc@1213 1604
adamc@1200 1605 | EUnurlify _ => default ()
adamc@1200 1606 | EJavaScript _ => default ()
adamc@1200 1607 | ESignalReturn _ => default ()
adamc@1200 1608 | ESignalBind _ => default ()
adamc@1200 1609 | ESignalSource _ => default ()
adamc@1200 1610 | EServerCall _ => default ()
adamc@1200 1611 | ERecv _ => default ()
adamc@1200 1612 | ESleep _ => default ()
adamc@1200 1613 | ESpawn _ => default ()
adamc@1200 1614 end
adamc@1200 1615
adamc@1200 1616 fun check file =
adamc@1200 1617 let
adamc@1213 1618 val file = MonoReduce.reduce file
adamc@1213 1619 val file = MonoOpt.optimize file
adamc@1213 1620 val file = Fuse.fuse file
adamc@1213 1621 val file = MonoOpt.optimize file
adamc@1216 1622 val file = MonoShake.shake file
adamc@1213 1623 (*val () = Print.preface ("File", MonoPrint.p_file MonoEnv.empty file)*)
adamc@1213 1624
adamc@1207 1625 val exptd = foldl (fn ((d, _), exptd) =>
adamc@1207 1626 case d of
adamc@1207 1627 DExport (_, _, n, _, _, _) => IS.add (exptd, n)
adamc@1207 1628 | _ => exptd) IS.empty file
adamc@1207 1629
adamc@1202 1630 fun decl ((d, _), (vals, pols)) =
adamc@1200 1631 case d of
adamc@1207 1632 DVal (_, n, _, e, _) =>
adamc@1200 1633 let
adamc@1207 1634 val isExptd = IS.member (exptd, n)
adamc@1207 1635
adamc@1207 1636 fun deAbs (e, env, nv, p) =
adamc@1200 1637 case #1 e of
adamc@1207 1638 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1,
adamc@1207 1639 if isExptd then
adamc@1207 1640 And (p, Reln (Known, [Var nv]))
adamc@1207 1641 else
adamc@1207 1642 p)
adamc@1207 1643 | _ => (e, env, nv, p)
adamc@1200 1644
adamc@1207 1645 val (e, env, nv, p) = deAbs (e, [], 1, True)
adamc@1200 1646
adamc@1207 1647 val (e, (_, p, sent)) = evalExp env (e, (nv, p, []))
adamc@1200 1648 in
adamc@1207 1649 (sent @ vals, pols)
adamc@1200 1650 end
adamc@1202 1651
adamc@1216 1652 | DPolicy (PolClient e) => (vals, #2 (queryProp [] 0 (fn rvN => (rvN + 1, Lvar rvN))
adamc@1214 1653 (SomeCol (Var 0)) e) :: pols)
adamc@1214 1654
adamc@1202 1655 | _ => (vals, pols)
adamc@1202 1656
adamc@1203 1657 val () = reset ()
adamc@1202 1658
adamc@1202 1659 val (vals, pols) = foldl decl ([], []) file
adamc@1200 1660 in
adamc@1207 1661 app (fn (loc, e, p) =>
adamc@1207 1662 let
adamc@1213 1663 fun doOne e =
adamc@1213 1664 let
adamc@1213 1665 val p = And (p, Reln (Eq, [Var 0, e]))
adamc@1213 1666 in
adamc@1213 1667 if List.exists (fn pol => if imply (p, pol) then
adamc@1213 1668 (if !debug then
adamc@1213 1669 Print.prefaces "Match"
adamc@1213 1670 [("Hyp", p_prop p),
adamc@1213 1671 ("Goal", p_prop pol)]
adamc@1213 1672 else
adamc@1213 1673 ();
adamc@1213 1674 true)
adamc@1213 1675 else
adamc@1213 1676 false) pols then
adamc@1213 1677 ()
adamc@1213 1678 else
adamc@1213 1679 (ErrorMsg.errorAt loc "The information flow policy may be violated here.";
adamc@1216 1680 Print.preface ("The state satisifies this predicate:", p_prop p))
adamc@1213 1681 end
adamc@1213 1682
adamc@1213 1683 fun doAll e =
adamc@1213 1684 case e of
adamc@1213 1685 Const _ => ()
adamc@1213 1686 | Var _ => doOne e
adamc@1213 1687 | Lvar _ => raise Fail "Iflow.doAll: Lvar"
adamc@1216 1688 | Func (UnCon _, [_]) => doOne e
adamc@1215 1689 | Func (_, es) => app doAll es
adamc@1213 1690 | Recd xes => app (doAll o #2) xes
adamc@1213 1691 | Proj _ => doOne e
adamc@1213 1692 | Finish => ()
adamc@1207 1693 in
adamc@1213 1694 doAll e
adamc@1207 1695 end) vals
adamc@1200 1696 end
adamc@1200 1697
adamc@1213 1698 val check = fn file =>
adamc@1213 1699 let
adamc@1213 1700 val oldInline = Settings.getMonoInline ()
adamc@1213 1701 in
adamc@1213 1702 (Settings.setMonoInline (case Int.maxInt of
adamc@1213 1703 NONE => 1000000
adamc@1213 1704 | SOME n => n);
adamc@1213 1705 check file;
adamc@1213 1706 Settings.setMonoInline oldInline)
adamc@1213 1707 handle ex => (Settings.setMonoInline oldInline;
adamc@1213 1708 raise ex)
adamc@1213 1709 end
adamc@1213 1710
adamc@1200 1711 end
adamc@1213 1712