annotate src/iflow.sml @ 1203:a75c66dd2aeb

Relax checking of table implications
author Adam Chlipala <adamc@hcoop.net>
date Sun, 04 Apr 2010 16:44:34 -0400
parents 509a6d7b60fb
children 7af5e2af64f4
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@1202 32 structure IM = IntBinaryMap
adamc@1202 33
adamc@1200 34 structure SS = BinarySetFn(struct
adamc@1200 35 type ord_key = string
adamc@1200 36 val compare = String.compare
adamc@1200 37 end)
adamc@1200 38
adamc@1200 39 val writers = ["htmlifyInt_w",
adamc@1200 40 "htmlifyFloat_w",
adamc@1200 41 "htmlifyString_w",
adamc@1200 42 "htmlifyBool_w",
adamc@1200 43 "htmlifyTime_w",
adamc@1200 44 "attrifyInt_w",
adamc@1200 45 "attrifyFloat_w",
adamc@1200 46 "attrifyString_w",
adamc@1200 47 "attrifyChar_w",
adamc@1200 48 "urlifyInt_w",
adamc@1200 49 "urlifyFloat_w",
adamc@1200 50 "urlifyString_w",
adamc@1200 51 "urlifyBool_w"]
adamc@1200 52
adamc@1200 53 val writers = SS.addList (SS.empty, writers)
adamc@1200 54
adamc@1200 55 type lvar = int
adamc@1200 56
adamc@1200 57 datatype exp =
adamc@1200 58 Const of Prim.t
adamc@1200 59 | Var of int
adamc@1200 60 | Lvar of lvar
adamc@1200 61 | Func of string * exp list
adamc@1200 62 | Recd of (string * exp) list
adamc@1200 63 | Proj of exp * string
adamc@1200 64 | Finish
adamc@1200 65
adamc@1200 66 datatype reln =
adamc@1200 67 Sql of string
adamc@1200 68 | Eq
adamc@1200 69
adamc@1200 70 datatype prop =
adamc@1200 71 True
adamc@1200 72 | False
adamc@1200 73 | Unknown
adamc@1200 74 | And of prop * prop
adamc@1200 75 | Or of prop * prop
adamc@1200 76 | Reln of reln * exp list
adamc@1200 77 | Select of int * lvar * lvar * prop * exp
adamc@1200 78
adamc@1200 79 local
adamc@1202 80 val count = ref 1
adamc@1200 81 in
adamc@1200 82 fun newLvar () =
adamc@1200 83 let
adamc@1200 84 val n = !count
adamc@1200 85 in
adamc@1200 86 count := n + 1;
adamc@1200 87 n
adamc@1200 88 end
adamc@1200 89 end
adamc@1200 90
adamc@1200 91 fun subExp (v, lv) =
adamc@1200 92 let
adamc@1200 93 fun sub e =
adamc@1200 94 case e of
adamc@1200 95 Const _ => e
adamc@1200 96 | Var v' => if v' = v then Lvar lv else e
adamc@1200 97 | Lvar _ => e
adamc@1200 98 | Func (f, es) => Func (f, map sub es)
adamc@1200 99 | Recd xes => Recd (map (fn (x, e) => (x, sub e)) xes)
adamc@1200 100 | Proj (e, s) => Proj (sub e, s)
adamc@1200 101 | Finish => Finish
adamc@1200 102 in
adamc@1200 103 sub
adamc@1200 104 end
adamc@1200 105
adamc@1200 106 fun subProp (v, lv) =
adamc@1200 107 let
adamc@1200 108 fun sub p =
adamc@1200 109 case p of
adamc@1200 110 True => p
adamc@1200 111 | False => p
adamc@1200 112 | Unknown => p
adamc@1200 113 | And (p1, p2) => And (sub p1, sub p2)
adamc@1200 114 | Or (p1, p2) => Or (sub p1, sub p2)
adamc@1200 115 | Reln (r, es) => Reln (r, map (subExp (v, lv)) es)
adamc@1200 116 | Select (v1, lv1, lv2, p, e) => Select (v1, lv1, lv2, sub p, subExp (v, lv) e)
adamc@1200 117 in
adamc@1200 118 sub
adamc@1200 119 end
adamc@1200 120
adamc@1200 121 fun isKnown e =
adamc@1200 122 case e of
adamc@1200 123 Const _ => true
adamc@1200 124 | Func (_, es) => List.all isKnown es
adamc@1200 125 | Recd xes => List.all (isKnown o #2) xes
adamc@1200 126 | Proj (e, _) => isKnown e
adamc@1200 127 | _ => false
adamc@1200 128
adamc@1200 129 fun isFinish e =
adamc@1200 130 case e of
adamc@1200 131 Finish => true
adamc@1200 132 | _ => false
adamc@1200 133
adamc@1200 134 fun simplify e =
adamc@1200 135 case e of
adamc@1200 136 Const _ => e
adamc@1200 137 | Var _ => e
adamc@1200 138 | Lvar _ => e
adamc@1200 139 | Func (f, es) =>
adamc@1200 140 let
adamc@1200 141 val es = map simplify es
adamc@1200 142 in
adamc@1200 143 if List.exists isFinish es then
adamc@1200 144 Finish
adamc@1200 145 else
adamc@1200 146 Func (f, es)
adamc@1200 147 end
adamc@1200 148 | Recd xes =>
adamc@1200 149 let
adamc@1200 150 val xes = map (fn (x, e) => (x, simplify e)) xes
adamc@1200 151 in
adamc@1200 152 if List.exists (isFinish o #2) xes then
adamc@1200 153 Finish
adamc@1200 154 else
adamc@1200 155 Recd xes
adamc@1200 156 end
adamc@1200 157 | Proj (e, s) =>
adamc@1200 158 (case simplify e of
adamc@1200 159 Recd xes =>
adamc@1200 160 getOpt (ListUtil.search (fn (x, e') => if x = s then SOME e' else NONE) xes, Recd xes)
adamc@1200 161 | e' =>
adamc@1200 162 if isFinish e' then
adamc@1200 163 Finish
adamc@1200 164 else
adamc@1200 165 Proj (e', s))
adamc@1200 166 | Finish => Finish
adamc@1200 167
adamc@1202 168 fun decomp fals or =
adamc@1200 169 let
adamc@1200 170 fun decomp p k =
adamc@1200 171 case p of
adamc@1200 172 True => k []
adamc@1202 173 | False => fals
adamc@1200 174 | Unknown => k []
adamc@1200 175 | And (p1, p2) =>
adamc@1200 176 decomp p1 (fn ps1 =>
adamc@1200 177 decomp p2 (fn ps2 =>
adamc@1200 178 k (ps1 @ ps2)))
adamc@1200 179 | Or (p1, p2) =>
adamc@1200 180 or (decomp p1 k, fn () => decomp p2 k)
adamc@1200 181 | Reln x => k [x]
adamc@1200 182 | Select _ => k []
adamc@1200 183 in
adamc@1200 184 decomp
adamc@1200 185 end
adamc@1200 186
adamc@1202 187 val unif = ref (IM.empty : exp IM.map)
adamc@1200 188
adamc@1203 189 fun reset () = unif := IM.empty
adamc@1203 190 fun save () = !unif
adamc@1203 191 fun restore x = unif := x
adamc@1203 192
adamc@1202 193 fun lvarIn lv =
adamc@1202 194 let
adamc@1202 195 fun lvi e =
adamc@1202 196 case e of
adamc@1202 197 Const _ => false
adamc@1202 198 | Var _ => false
adamc@1202 199 | Lvar lv' => lv' = lv
adamc@1202 200 | Func (_, es) => List.exists lvi es
adamc@1202 201 | Recd xes => List.exists (lvi o #2) xes
adamc@1202 202 | Proj (e, _) => lvi e
adamc@1202 203 | Finish => false
adamc@1202 204 in
adamc@1202 205 lvi
adamc@1202 206 end
adamc@1202 207
adamc@1202 208 fun eq' (e1, e2) =
adamc@1202 209 case (e1, e2) of
adamc@1202 210 (Const p1, Const p2) => Prim.equal (p1, p2)
adamc@1202 211 | (Var n1, Var n2) => n1 = n2
adamc@1202 212
adamc@1202 213 | (Lvar n1, _) =>
adamc@1202 214 (case IM.find (!unif, n1) of
adamc@1202 215 SOME e1 => eq' (e1, e2)
adamc@1202 216 | NONE =>
adamc@1202 217 case e2 of
adamc@1202 218 Lvar n2 =>
adamc@1202 219 (case IM.find (!unif, n2) of
adamc@1202 220 SOME e2 => eq' (e1, e2)
adamc@1202 221 | NONE => n1 = n2
adamc@1202 222 orelse (unif := IM.insert (!unif, n1, e2);
adamc@1202 223 true))
adamc@1202 224 | _ =>
adamc@1202 225 if lvarIn n1 e2 then
adamc@1202 226 false
adamc@1202 227 else
adamc@1202 228 (unif := IM.insert (!unif, n1, e2);
adamc@1202 229 true))
adamc@1202 230
adamc@1202 231 | (_, Lvar n2) =>
adamc@1202 232 (case IM.find (!unif, n2) of
adamc@1202 233 SOME e2 => eq' (e1, e2)
adamc@1202 234 | NONE =>
adamc@1202 235 if lvarIn n2 e1 then
adamc@1202 236 false
adamc@1202 237 else
adamc@1202 238 (unif := IM.insert (!unif, n2, e1);
adamc@1202 239 true))
adamc@1202 240
adamc@1202 241 | (Func (f1, es1), Func (f2, es2)) => f1 = f2 andalso ListPair.allEq eq' (es1, es2)
adamc@1202 242 | (Recd xes1, Recd xes2) => ListPair.allEq (fn ((x1, e1), (x2, e2)) => x1 = x2 andalso eq' (e1, e2)) (xes1, xes2)
adamc@1202 243 | (Proj (e1, s1), Proj (e2, s2)) => eq' (e1, e2) andalso s1 = s2
adamc@1202 244 | (Finish, Finish) => true
adamc@1202 245 | _ => false
adamc@1202 246
adamc@1202 247 fun eq (e1, e2) =
adamc@1202 248 let
adamc@1203 249 val saved = save ()
adamc@1202 250 in
adamc@1202 251 if eq' (simplify e1, simplify e2) then
adamc@1202 252 true
adamc@1202 253 else
adamc@1203 254 (restore saved;
adamc@1202 255 false)
adamc@1202 256 end
adamc@1202 257
adamc@1202 258 exception Imply of prop * prop
adamc@1202 259
adamc@1202 260 fun rimp ((r1, es1), (r2, es2)) =
adamc@1202 261 case (r1, r2) of
adamc@1202 262 (Sql r1', Sql r2') =>
adamc@1202 263 r1' = r2' andalso
adamc@1202 264 (case (es1, es2) of
adamc@1202 265 ([Recd xes1], [Recd xes2]) =>
adamc@1202 266 let
adamc@1203 267 val saved = save ()
adamc@1202 268 in
adamc@1202 269 if List.all (fn (f, e2) =>
adamc@1203 270 case ListUtil.search (fn (f', e1) => if f' = f then SOME e1 else NONE) xes1 of
adamc@1203 271 NONE => true
adamc@1203 272 | SOME e1 => eq (e1, e2)) xes2 then
adamc@1202 273 true
adamc@1202 274 else
adamc@1203 275 (restore saved;
adamc@1202 276 false)
adamc@1202 277 end
adamc@1202 278 | _ => false)
adamc@1202 279 | (Eq, Eq) =>
adamc@1202 280 (case (es1, es2) of
adamc@1202 281 ([x1, y1], [x2, y2]) =>
adamc@1202 282 let
adamc@1203 283 val saved = save ()
adamc@1202 284 in
adamc@1202 285 if eq (x1, x2) andalso eq (y1, y2) then
adamc@1202 286 true
adamc@1202 287 else
adamc@1203 288 (restore saved;
adamc@1202 289 (*raise Imply (Reln (Eq, es1), Reln (Eq, es2));*)
adamc@1202 290 eq (x1, y2) andalso eq (y1, x2))
adamc@1202 291 end
adamc@1202 292 | _ => false)
adamc@1202 293 | _ => false
adamc@1202 294
adamc@1202 295 fun imply (p1, p2) =
adamc@1203 296 (reset ();
adamc@1202 297 (*raise (Imply (p1, p2));*)
adamc@1202 298 decomp true (fn (e1, e2) => e1 andalso e2 ()) p1
adamc@1202 299 (fn hyps =>
adamc@1202 300 decomp false (fn (e1, e2) => e1 orelse e2 ()) p2
adamc@1202 301 (fn goals =>
adamc@1202 302 let
adamc@1202 303 fun gls goals onFail =
adamc@1202 304 case goals of
adamc@1202 305 [] => true
adamc@1202 306 | g :: goals =>
adamc@1202 307 let
adamc@1202 308 fun hps hyps =
adamc@1202 309 case hyps of
adamc@1202 310 [] => onFail ()
adamc@1202 311 | h :: hyps =>
adamc@1202 312 let
adamc@1203 313 val saved = save ()
adamc@1202 314 in
adamc@1202 315 if rimp (h, g) then
adamc@1202 316 let
adamc@1203 317 val changed = IM.numItems (!unif) <> IM.numItems saved
adamc@1202 318 in
adamc@1203 319 gls goals (fn () => (restore saved;
adamc@1202 320 changed andalso hps hyps))
adamc@1202 321 end
adamc@1202 322 else
adamc@1202 323 hps hyps
adamc@1202 324 end
adamc@1202 325 in
adamc@1202 326 hps hyps
adamc@1202 327 end
adamc@1202 328 in
adamc@1202 329 gls goals (fn () => false)
adamc@1202 330 end)))
adamc@1202 331
adamc@1200 332
adamc@1200 333 fun patCon pc =
adamc@1200 334 case pc of
adamc@1200 335 PConVar n => "C" ^ Int.toString n
adamc@1200 336 | PConFfi {mod = m, datatyp = d, con = c, ...} => m ^ "." ^ d ^ "." ^ c
adamc@1200 337
adamc@1202 338
adamc@1200 339
adamc@1200 340 datatype chunk =
adamc@1200 341 String of string
adamc@1200 342 | Exp of Mono.exp
adamc@1200 343
adamc@1200 344 fun chunkify e =
adamc@1200 345 case #1 e of
adamc@1200 346 EPrim (Prim.String s) => [String s]
adamc@1200 347 | EStrcat (e1, e2) => chunkify e1 @ chunkify e2
adamc@1200 348 | _ => [Exp e]
adamc@1200 349
adamc@1201 350 type 'a parser = chunk list -> ('a * chunk list) option
adamc@1201 351
adamc@1201 352 fun always v chs = SOME (v, chs)
adamc@1201 353
adamc@1202 354 fun parse p s =
adamc@1202 355 case p (chunkify s) of
adamc@1201 356 SOME (v, []) => SOME v
adamc@1201 357 | _ => NONE
adamc@1201 358
adamc@1201 359 fun const s chs =
adamc@1201 360 case chs of
adamc@1201 361 String s' :: chs => if String.isPrefix s s' then
adamc@1201 362 SOME ((), if size s = size s' then
adamc@1201 363 chs
adamc@1201 364 else
adamc@1201 365 String (String.extract (s', size s, NONE)) :: chs)
adamc@1201 366 else
adamc@1201 367 NONE
adamc@1201 368 | _ => NONE
adamc@1201 369
adamc@1201 370 fun follow p1 p2 chs =
adamc@1201 371 case p1 chs of
adamc@1201 372 NONE => NONE
adamc@1201 373 | SOME (v1, chs) =>
adamc@1201 374 case p2 chs of
adamc@1201 375 NONE => NONE
adamc@1201 376 | SOME (v2, chs) => SOME ((v1, v2), chs)
adamc@1201 377
adamc@1201 378 fun wrap p f chs =
adamc@1201 379 case p chs of
adamc@1201 380 NONE => NONE
adamc@1201 381 | SOME (v, chs) => SOME (f v, chs)
adamc@1201 382
adamc@1201 383 fun alt p1 p2 chs =
adamc@1201 384 case p1 chs of
adamc@1201 385 NONE => p2 chs
adamc@1201 386 | v => v
adamc@1201 387
adamc@1201 388 fun skip cp chs =
adamc@1201 389 case chs of
adamc@1201 390 String "" :: chs => skip cp chs
adamc@1201 391 | String s :: chs' => if cp (String.sub (s, 0)) then
adamc@1201 392 skip cp (String (String.extract (s, 1, NONE)) :: chs')
adamc@1201 393 else
adamc@1201 394 SOME ((), chs)
adamc@1201 395 | _ => SOME ((), chs)
adamc@1201 396
adamc@1201 397 fun keep cp chs =
adamc@1201 398 case chs of
adamc@1201 399 String "" :: chs => keep cp chs
adamc@1201 400 | String s :: chs' =>
adamc@1201 401 let
adamc@1201 402 val (befor, after) = Substring.splitl cp (Substring.full s)
adamc@1201 403 in
adamc@1201 404 if Substring.isEmpty befor then
adamc@1201 405 NONE
adamc@1201 406 else
adamc@1201 407 SOME (Substring.string befor,
adamc@1201 408 if Substring.isEmpty after then
adamc@1201 409 chs'
adamc@1201 410 else
adamc@1201 411 String (Substring.string after) :: chs')
adamc@1201 412 end
adamc@1201 413 | _ => NONE
adamc@1201 414
adamc@1201 415 fun ws p = wrap (follow p (skip (fn ch => ch = #" "))) #1
adamc@1201 416
adamc@1201 417 fun list p chs =
adamc@1201 418 (alt (wrap (follow p (follow (ws (const ",")) (list p)))
adamc@1201 419 (fn (v, ((), ls)) => v :: ls))
adamc@1201 420 (alt (wrap (ws p) (fn v => [v]))
adamc@1201 421 (always []))) chs
adamc@1201 422
adamc@1201 423 val ident = keep (fn ch => Char.isAlphaNum ch orelse ch = #"_")
adamc@1201 424
adamc@1201 425 val t_ident = wrap ident (fn s => if String.isPrefix "T_" s then
adamc@1201 426 String.extract (s, 2, NONE)
adamc@1201 427 else
adamc@1201 428 raise Fail "Iflow: Bad table variable")
adamc@1201 429 val uw_ident = wrap ident (fn s => if String.isPrefix "uw_" s then
adamc@1201 430 String.extract (s, 3, NONE)
adamc@1201 431 else
adamc@1201 432 raise Fail "Iflow: Bad uw_* variable")
adamc@1201 433
adamc@1201 434 val sitem = wrap (follow t_ident
adamc@1201 435 (follow (const ".")
adamc@1201 436 uw_ident))
adamc@1201 437 (fn (t, ((), f)) => (t, f))
adamc@1201 438
adamc@1201 439 val select = wrap (follow (const "SELECT ") (list sitem))
adamc@1201 440 (fn ((), ls) => ls)
adamc@1201 441
adamc@1201 442 val fitem = wrap (follow uw_ident
adamc@1201 443 (follow (const " AS ")
adamc@1201 444 t_ident))
adamc@1201 445 (fn (t, ((), f)) => (t, f))
adamc@1201 446
adamc@1201 447 val from = wrap (follow (const "FROM ") (list fitem))
adamc@1201 448 (fn ((), ls) => ls)
adamc@1201 449
adamc@1201 450 val query = wrap (follow select from)
adamc@1201 451 (fn (fs, ts) => {Select = fs, From = ts})
adamc@1201 452
adamc@1202 453 fun queryProp rv oe e =
adamc@1202 454 case parse query e of
adamc@1201 455 NONE => Unknown
adamc@1201 456 | SOME r =>
adamc@1202 457 let
adamc@1202 458 val p =
adamc@1202 459 foldl (fn ((t, v), p) =>
adamc@1202 460 And (p,
adamc@1202 461 Reln (Sql t,
adamc@1202 462 [Recd (foldl (fn ((v', f), fs) =>
adamc@1202 463 if v' = v then
adamc@1202 464 (f, Proj (Proj (Lvar rv, v), f)) :: fs
adamc@1202 465 else
adamc@1202 466 fs) [] (#Select r))])))
adamc@1202 467 True (#From r)
adamc@1202 468 in
adamc@1202 469 case oe of
adamc@1202 470 NONE => p
adamc@1202 471 | SOME oe =>
adamc@1202 472 And (p,
adamc@1202 473 foldl (fn ((v, f), p) =>
adamc@1202 474 Or (p,
adamc@1202 475 Reln (Eq, [oe, Proj (Proj (Lvar rv, v), f)])))
adamc@1202 476 False (#Select r))
adamc@1202 477 end
adamc@1200 478
adamc@1202 479 fun evalExp env (e as (_, loc), st as (nv, p, sent)) =
adamc@1200 480 let
adamc@1200 481 fun default () =
adamc@1200 482 (Var nv, (nv+1, p, sent))
adamc@1200 483
adamc@1200 484 fun addSent (p, e, sent) =
adamc@1200 485 if isKnown e then
adamc@1200 486 sent
adamc@1200 487 else
adamc@1202 488 (loc, e, p) :: sent
adamc@1200 489 in
adamc@1200 490 case #1 e of
adamc@1200 491 EPrim p => (Const p, st)
adamc@1200 492 | ERel n => (List.nth (env, n), st)
adamc@1200 493 | ENamed _ => default ()
adamc@1200 494 | ECon (_, pc, NONE) => (Func (patCon pc, []), st)
adamc@1200 495 | ECon (_, pc, SOME e) =>
adamc@1200 496 let
adamc@1200 497 val (e, st) = evalExp env (e, st)
adamc@1200 498 in
adamc@1200 499 (Func (patCon pc, [e]), st)
adamc@1200 500 end
adamc@1200 501 | ENone _ => (Func ("None", []), st)
adamc@1200 502 | ESome (_, e) =>
adamc@1200 503 let
adamc@1200 504 val (e, st) = evalExp env (e, st)
adamc@1200 505 in
adamc@1200 506 (Func ("Some", [e]), st)
adamc@1200 507 end
adamc@1200 508 | EFfi _ => default ()
adamc@1200 509 | EFfiApp (m, s, es) =>
adamc@1200 510 if m = "Basis" andalso SS.member (writers, s) then
adamc@1200 511 let
adamc@1200 512 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 513 in
adamc@1200 514 (Func ("unit", []), (#1 st, p, foldl (fn (e, sent) => addSent (#2 st, e, sent)) sent es))
adamc@1200 515 end
adamc@1200 516 else if Settings.isEffectful (m, s) andalso not (Settings.isBenignEffectful (m, s)) then
adamc@1200 517 default ()
adamc@1200 518 else
adamc@1200 519 let
adamc@1200 520 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 521 in
adamc@1200 522 (Func (m ^ "." ^ s, es), st)
adamc@1200 523 end
adamc@1200 524 | EApp _ => default ()
adamc@1200 525 | EAbs _ => default ()
adamc@1200 526 | EUnop (s, e1) =>
adamc@1200 527 let
adamc@1200 528 val (e1, st) = evalExp env (e1, st)
adamc@1200 529 in
adamc@1200 530 (Func (s, [e1]), st)
adamc@1200 531 end
adamc@1200 532 | EBinop (s, e1, e2) =>
adamc@1200 533 let
adamc@1200 534 val (e1, st) = evalExp env (e1, st)
adamc@1200 535 val (e2, st) = evalExp env (e2, st)
adamc@1200 536 in
adamc@1200 537 (Func (s, [e1, e2]), st)
adamc@1200 538 end
adamc@1200 539 | ERecord xets =>
adamc@1200 540 let
adamc@1200 541 val (xes, st) = ListUtil.foldlMap (fn ((x, e, _), st) =>
adamc@1200 542 let
adamc@1200 543 val (e, st) = evalExp env (e, st)
adamc@1200 544 in
adamc@1200 545 ((x, e), st)
adamc@1200 546 end) st xets
adamc@1200 547 in
adamc@1200 548 (Recd xes, st)
adamc@1200 549 end
adamc@1200 550 | EField (e, s) =>
adamc@1200 551 let
adamc@1200 552 val (e, st) = evalExp env (e, st)
adamc@1200 553 in
adamc@1200 554 (Proj (e, s), st)
adamc@1200 555 end
adamc@1200 556 | ECase _ => default ()
adamc@1200 557 | EStrcat (e1, e2) =>
adamc@1200 558 let
adamc@1200 559 val (e1, st) = evalExp env (e1, st)
adamc@1200 560 val (e2, st) = evalExp env (e2, st)
adamc@1200 561 in
adamc@1200 562 (Func ("cat", [e1, e2]), st)
adamc@1200 563 end
adamc@1200 564 | EError _ => (Finish, st)
adamc@1200 565 | EReturnBlob {blob = b, mimeType = m, ...} =>
adamc@1200 566 let
adamc@1200 567 val (b, st) = evalExp env (b, st)
adamc@1200 568 val (m, st) = evalExp env (m, st)
adamc@1200 569 in
adamc@1200 570 (Finish, (#1 st, p, addSent (#2 st, b, addSent (#2 st, m, sent))))
adamc@1200 571 end
adamc@1200 572 | ERedirect (e, _) =>
adamc@1200 573 let
adamc@1200 574 val (e, st) = evalExp env (e, st)
adamc@1200 575 in
adamc@1200 576 (Finish, (#1 st, p, addSent (#2 st, e, sent)))
adamc@1200 577 end
adamc@1200 578 | EWrite e =>
adamc@1200 579 let
adamc@1200 580 val (e, st) = evalExp env (e, st)
adamc@1200 581 in
adamc@1200 582 (Func ("unit", []), (#1 st, p, addSent (#2 st, e, sent)))
adamc@1200 583 end
adamc@1200 584 | ESeq (e1, e2) =>
adamc@1200 585 let
adamc@1200 586 val (_, st) = evalExp env (e1, st)
adamc@1200 587 in
adamc@1200 588 evalExp env (e2, st)
adamc@1200 589 end
adamc@1200 590 | ELet (_, _, e1, e2) =>
adamc@1200 591 let
adamc@1200 592 val (e1, st) = evalExp env (e1, st)
adamc@1200 593 in
adamc@1200 594 evalExp (e1 :: env) (e2, st)
adamc@1200 595 end
adamc@1200 596 | EClosure (n, es) =>
adamc@1200 597 let
adamc@1200 598 val (es, st) = ListUtil.foldlMap (evalExp env) st es
adamc@1200 599 in
adamc@1200 600 (Func ("Cl" ^ Int.toString n, es), st)
adamc@1200 601 end
adamc@1200 602
adamc@1200 603 | EQuery {query = q, body = b, initial = i, ...} =>
adamc@1200 604 let
adamc@1200 605 val (_, st) = evalExp env (q, st)
adamc@1200 606 val (i, st) = evalExp env (i, st)
adamc@1200 607
adamc@1200 608 val r = #1 st
adamc@1200 609 val acc = #1 st + 1
adamc@1200 610 val st' = (#1 st + 2, #2 st, #3 st)
adamc@1200 611
adamc@1200 612 val (b, st') = evalExp (Var acc :: Var r :: env) (b, st')
adamc@1200 613
adamc@1200 614 val r' = newLvar ()
adamc@1200 615 val acc' = newLvar ()
adamc@1202 616 val qp = queryProp r' NONE q
adamc@1200 617
adamc@1200 618 val doSubExp = subExp (r, r') o subExp (acc, acc')
adamc@1200 619 val doSubProp = subProp (r, r') o subProp (acc, acc')
adamc@1200 620
adamc@1200 621 val p = doSubProp (#2 st')
adamc@1200 622 val p = And (p, qp)
adamc@1200 623 val p = Select (r, r', acc', p, doSubExp b)
adamc@1200 624 in
adamc@1202 625 (Var r, (#1 st + 1, And (#2 st, p), map (fn (loc, e, p) => (loc,
adamc@1202 626 doSubExp e,
adamc@1202 627 And (qp, doSubProp p))) (#3 st')))
adamc@1200 628 end
adamc@1200 629 | EDml _ => default ()
adamc@1200 630 | ENextval _ => default ()
adamc@1200 631 | ESetval _ => default ()
adamc@1200 632
adamc@1200 633 | EUnurlify _ => default ()
adamc@1200 634 | EJavaScript _ => default ()
adamc@1200 635 | ESignalReturn _ => default ()
adamc@1200 636 | ESignalBind _ => default ()
adamc@1200 637 | ESignalSource _ => default ()
adamc@1200 638 | EServerCall _ => default ()
adamc@1200 639 | ERecv _ => default ()
adamc@1200 640 | ESleep _ => default ()
adamc@1200 641 | ESpawn _ => default ()
adamc@1200 642 end
adamc@1200 643
adamc@1200 644 fun check file =
adamc@1200 645 let
adamc@1202 646 fun decl ((d, _), (vals, pols)) =
adamc@1200 647 case d of
adamc@1200 648 DVal (x, _, _, e, _) =>
adamc@1200 649 let
adamc@1200 650 fun deAbs (e, env, nv) =
adamc@1200 651 case #1 e of
adamc@1200 652 EAbs (_, _, _, e) => deAbs (e, Var nv :: env, nv + 1)
adamc@1200 653 | _ => (e, env, nv)
adamc@1200 654
adamc@1202 655 val (e, env, nv) = deAbs (e, [], 1)
adamc@1200 656
adamc@1200 657 val (e, (_, p, sent)) = evalExp env (e, (nv, True, []))
adamc@1200 658 in
adamc@1202 659 ((x, e, p, sent) :: vals, pols)
adamc@1200 660 end
adamc@1202 661
adamc@1202 662 | DPolicy (PolQuery e) => (vals, queryProp 0 (SOME (Var 0)) e :: pols)
adamc@1202 663
adamc@1202 664 | _ => (vals, pols)
adamc@1202 665
adamc@1203 666 val () = reset ()
adamc@1202 667
adamc@1202 668 val (vals, pols) = foldl decl ([], []) file
adamc@1200 669 in
adamc@1202 670 app (fn (name, _, _, sent) =>
adamc@1202 671 app (fn (loc, e, p) =>
adamc@1202 672 let
adamc@1202 673 val p = And (p, Reln (Eq, [Var 0, e]))
adamc@1202 674 in
adamc@1202 675 if List.exists (fn pol => imply (p, pol)) pols then
adamc@1202 676 ()
adamc@1202 677 else
adamc@1202 678 ErrorMsg.errorAt loc "The information flow policy may be violated here."
adamc@1202 679 end) sent) vals
adamc@1200 680 end
adamc@1200 681
adamc@1200 682 end