annotate src/sqlcache.sml @ 2261:f81f1930c5d6

Fix SQL-parsing and declaration-ordering bugs.
author Ziv Scully <ziv@mit.edu>
date Wed, 30 Sep 2015 00:33:52 -0400
parents 03b10c7fab9a
children 34ad83d9b729
rev   line source
ziv@2250 1 structure Sqlcache :> SQLCACHE = struct
ziv@2209 2
ziv@2209 3 open Mono
ziv@2209 4
ziv@2209 5 structure IS = IntBinarySet
ziv@2209 6 structure IM = IntBinaryMap
ziv@2213 7 structure SK = struct type ord_key = string val compare = String.compare end
ziv@2213 8 structure SS = BinarySetFn(SK)
ziv@2213 9 structure SM = BinaryMapFn(SK)
ziv@2213 10 structure SIMM = MultimapFn(structure KeyMap = SM structure ValSet = IS)
ziv@2209 11
ziv@2250 12 fun iterate f n x = if n < 0
ziv@2250 13 then raise Fail "Can't iterate function negative number of times."
ziv@2250 14 else if n = 0
ziv@2250 15 then x
ziv@2250 16 else iterate f (n-1) (f x)
ziv@2250 17
ziv@2216 18 (* Filled in by [cacheWrap] during [Sqlcache]. *)
ziv@2213 19 val ffiInfo : {index : int, params : int} list ref = ref []
ziv@2209 20
ziv@2227 21 fun resetFfiInfo () = ffiInfo := []
ziv@2227 22
ziv@2213 23 fun getFfiInfo () = !ffiInfo
ziv@2213 24
ziv@2215 25 (* Some FFIs have writing as their only effect, which the caching records. *)
ziv@2215 26 val ffiEffectful =
ziv@2223 27 (* ASK: how can this be less hard-coded? *)
ziv@2215 28 let
ziv@2258 29 val okayWrites = SS.fromList ["htmlifyInt_w",
ziv@2258 30 "htmlifyFloat_w",
ziv@2258 31 "htmlifyString_w",
ziv@2258 32 "htmlifyBool_w",
ziv@2258 33 "htmlifyTime_w",
ziv@2258 34 "attrifyInt_w",
ziv@2258 35 "attrifyFloat_w",
ziv@2258 36 "attrifyString_w",
ziv@2258 37 "attrifyChar_w",
ziv@2258 38 "urlifyInt_w",
ziv@2258 39 "urlifyFloat_w",
ziv@2258 40 "urlifyString_w",
ziv@2258 41 "urlifyBool_w",
ziv@2258 42 "urlifyChannel_w"]
ziv@2215 43 in
ziv@2259 44 (* ASK: nicer way than using [Settings.addEffectful] for each Sqlcache
ziv@2259 45 function? Right now they're all always effectful. *)
ziv@2215 46 fn (m, f) => Settings.isEffectful (m, f)
ziv@2258 47 andalso not (m = "Basis" andalso SS.member (okayWrites, f))
ziv@2215 48 end
ziv@2215 49
ziv@2234 50 val cache = ref LruCache.cache
ziv@2233 51 fun setCache c = cache := c
ziv@2233 52 fun getCache () = !cache
ziv@2233 53
ziv@2248 54 (* Used to have type context for local variables in MonoUtil functions. *)
ziv@2248 55 val doBind =
ziv@2250 56 fn (env, MonoUtil.Exp.RelE (s, t)) => MonoEnv.pushERel env s t NONE
ziv@2250 57 | (env, _) => env
ziv@2215 58
ziv@2248 59
ziv@2248 60 (*******************)
ziv@2248 61 (* Effect Analysis *)
ziv@2248 62 (*******************)
ziv@2215 63
ziv@2216 64 (* Makes an exception for [EWrite] (which is recorded when caching). *)
ziv@2248 65 fun effectful (effs : IS.set) =
ziv@2215 66 let
ziv@2248 67 val isFunction =
ziv@2248 68 fn (TFun _, _) => true
ziv@2248 69 | _ => false
ziv@2250 70 fun doExp (env, e) =
ziv@2248 71 case e of
ziv@2248 72 EPrim _ => false
ziv@2248 73 (* For now: variables of function type might be effectful, but
ziv@2248 74 others are fully evaluated and are therefore not effectful. *)
ziv@2250 75 | ERel n => isFunction (#2 (MonoEnv.lookupERel env n))
ziv@2248 76 | ENamed n => IS.member (effs, n)
ziv@2248 77 | EFfi (m, f) => ffiEffectful (m, f)
ziv@2248 78 | EFfiApp (m, f, _) => ffiEffectful (m, f)
ziv@2248 79 (* These aren't effectful unless a subexpression is. *)
ziv@2248 80 | ECon _ => false
ziv@2248 81 | ENone _ => false
ziv@2248 82 | ESome _ => false
ziv@2248 83 | EApp _ => false
ziv@2248 84 | EAbs _ => false
ziv@2248 85 | EUnop _ => false
ziv@2248 86 | EBinop _ => false
ziv@2248 87 | ERecord _ => false
ziv@2248 88 | EField _ => false
ziv@2248 89 | ECase _ => false
ziv@2248 90 | EStrcat _ => false
ziv@2248 91 (* EWrite is a special exception because we record writes when caching. *)
ziv@2248 92 | EWrite _ => false
ziv@2248 93 | ESeq _ => false
ziv@2248 94 | ELet _ => false
ziv@2250 95 | EUnurlify _ => false
ziv@2248 96 (* ASK: what should we do about closures? *)
ziv@2248 97 (* Everything else is some sort of effect. We could flip this and
ziv@2248 98 explicitly list bits of Mono that are effectful, but this is
ziv@2248 99 conservatively robust to future changes (however unlikely). *)
ziv@2248 100 | _ => true
ziv@2215 101 in
ziv@2248 102 MonoUtil.Exp.existsB {typ = fn _ => false, exp = doExp, bind = doBind}
ziv@2215 103 end
ziv@2215 104
ziv@2215 105 (* TODO: test this. *)
ziv@2252 106 fun effectfulDecls (decls, _) =
ziv@2215 107 let
ziv@2248 108 fun doVal ((_, name, _, e, _), effs) =
ziv@2250 109 if effectful effs MonoEnv.empty e
ziv@2248 110 then IS.add (effs, name)
ziv@2248 111 else effs
ziv@2215 112 val doDecl =
ziv@2248 113 fn ((DVal v, _), effs) => doVal (v, effs)
ziv@2248 114 (* Repeat the list of declarations a number of times equal to its size,
ziv@2248 115 making sure effectfulness propagates everywhere it should. This is
ziv@2248 116 analagous to the Bellman-Ford algorithm. *)
ziv@2248 117 | ((DValRec vs, _), effs) =>
ziv@2248 118 List.foldl doVal effs (List.concat (List.map (fn _ => vs) vs))
ziv@2215 119 (* ASK: any other cases? *)
ziv@2248 120 | (_, effs) => effs
ziv@2215 121 in
ziv@2248 122 List.foldl doDecl IS.empty decls
ziv@2215 123 end
ziv@2215 124
ziv@2215 125
ziv@2248 126 (*********************************)
ziv@2248 127 (* Boolean Formula Normalization *)
ziv@2248 128 (*********************************)
ziv@2216 129
ziv@2234 130 datatype junctionType = Conj | Disj
ziv@2216 131
ziv@2216 132 datatype 'atom formula =
ziv@2216 133 Atom of 'atom
ziv@2216 134 | Negate of 'atom formula
ziv@2234 135 | Combo of junctionType * 'atom formula list
ziv@2216 136
ziv@2243 137 (* Guaranteed to have all negation pushed to the atoms. *)
ziv@2243 138 datatype 'atom formula' =
ziv@2243 139 Atom' of 'atom
ziv@2243 140 | Combo' of junctionType * 'atom formula' list
ziv@2243 141
ziv@2234 142 val flipJt = fn Conj => Disj | Disj => Conj
ziv@2216 143
ziv@2236 144 fun concatMap f xs = List.concat (map f xs)
ziv@2216 145
ziv@2216 146 val rec cartesianProduct : 'a list list -> 'a list list =
ziv@2216 147 fn [] => [[]]
ziv@2236 148 | (xs :: xss) => concatMap (fn ys => concatMap (fn x => [x :: ys]) xs)
ziv@2236 149 (cartesianProduct xss)
ziv@2216 150
ziv@2218 151 (* Pushes all negation to the atoms.*)
ziv@2244 152 fun pushNegate (normalizeAtom : bool * 'atom -> 'atom) (negating : bool) =
ziv@2244 153 fn Atom x => Atom' (normalizeAtom (negating, x))
ziv@2244 154 | Negate f => pushNegate normalizeAtom (not negating) f
ziv@2244 155 | Combo (j, fs) => Combo' (if negating then flipJt j else j,
ziv@2244 156 map (pushNegate normalizeAtom negating) fs)
ziv@2218 157
ziv@2218 158 val rec flatten =
ziv@2243 159 fn Combo' (_, [f]) => flatten f
ziv@2243 160 | Combo' (j, fs) =>
ziv@2243 161 Combo' (j, List.foldr (fn (f, acc) =>
ziv@2243 162 case f of
ziv@2243 163 Combo' (j', fs') =>
ziv@2243 164 if j = j' orelse length fs' = 1
ziv@2243 165 then fs' @ acc
ziv@2243 166 else f :: acc
ziv@2243 167 | _ => f :: acc)
ziv@2243 168 []
ziv@2243 169 (map flatten fs))
ziv@2218 170 | f => f
ziv@2218 171
ziv@2243 172 (* [simplify] operates on the desired normal form. E.g., if [junc] is [Disj],
ziv@2243 173 consider the list of lists to be a disjunction of conjunctions. *)
ziv@2237 174 fun normalize' (simplify : 'a list list -> 'a list list)
ziv@2235 175 (junc : junctionType) =
ziv@2216 176 let
ziv@2235 177 fun norm junc =
ziv@2237 178 simplify
ziv@2243 179 o (fn Atom' x => [[x]]
ziv@2243 180 | Combo' (j, fs) =>
ziv@2235 181 let
ziv@2236 182 val fss = map (norm junc) fs
ziv@2235 183 in
ziv@2236 184 if j = junc
ziv@2236 185 then List.concat fss
ziv@2236 186 else map List.concat (cartesianProduct fss)
ziv@2235 187 end)
ziv@2216 188 in
ziv@2235 189 norm junc
ziv@2216 190 end
ziv@2216 191
ziv@2244 192 fun normalize simplify normalizeAtom junc =
ziv@2243 193 normalize' simplify junc
ziv@2235 194 o flatten
ziv@2244 195 o pushNegate normalizeAtom false
ziv@2216 196
ziv@2221 197 fun mapFormula mf =
ziv@2221 198 fn Atom x => Atom (mf x)
ziv@2221 199 | Negate f => Negate (mapFormula mf f)
ziv@2235 200 | Combo (j, fs) => Combo (j, map (mapFormula mf) fs)
ziv@2216 201
ziv@2230 202
ziv@2248 203 (****************)
ziv@2248 204 (* SQL Analysis *)
ziv@2248 205 (****************)
ziv@2213 206
ziv@2240 207 structure CmpKey = struct
ziv@2235 208
ziv@2235 209 type ord_key = Sql.cmp
ziv@2235 210
ziv@2235 211 val compare =
ziv@2235 212 fn (Sql.Eq, Sql.Eq) => EQUAL
ziv@2235 213 | (Sql.Eq, _) => LESS
ziv@2235 214 | (_, Sql.Eq) => GREATER
ziv@2235 215 | (Sql.Ne, Sql.Ne) => EQUAL
ziv@2235 216 | (Sql.Ne, _) => LESS
ziv@2235 217 | (_, Sql.Ne) => GREATER
ziv@2235 218 | (Sql.Lt, Sql.Lt) => EQUAL
ziv@2235 219 | (Sql.Lt, _) => LESS
ziv@2235 220 | (_, Sql.Lt) => GREATER
ziv@2235 221 | (Sql.Le, Sql.Le) => EQUAL
ziv@2235 222 | (Sql.Le, _) => LESS
ziv@2235 223 | (_, Sql.Le) => GREATER
ziv@2235 224 | (Sql.Gt, Sql.Gt) => EQUAL
ziv@2235 225 | (Sql.Gt, _) => LESS
ziv@2235 226 | (_, Sql.Gt) => GREATER
ziv@2235 227 | (Sql.Ge, Sql.Ge) => EQUAL
ziv@2235 228
ziv@2235 229 end
ziv@2235 230
ziv@2216 231 val rec chooseTwos : 'a list -> ('a * 'a) list =
ziv@2216 232 fn [] => []
ziv@2216 233 | x :: ys => map (fn y => (x, y)) ys @ chooseTwos ys
ziv@2213 234
ziv@2237 235 fun removeRedundant madeRedundantBy zs =
ziv@2237 236 let
ziv@2237 237 fun removeRedundant' (xs, ys) =
ziv@2237 238 case xs of
ziv@2237 239 [] => ys
ziv@2237 240 | x :: xs' =>
ziv@2237 241 removeRedundant' (xs',
ziv@2237 242 if List.exists (fn y => madeRedundantBy (x, y)) (xs' @ ys)
ziv@2237 243 then ys
ziv@2237 244 else x :: ys)
ziv@2237 245 in
ziv@2237 246 removeRedundant' (zs, [])
ziv@2237 247 end
ziv@2237 248
ziv@2216 249 datatype atomExp =
ziv@2216 250 QueryArg of int
ziv@2216 251 | DmlRel of int
ziv@2216 252 | Prim of Prim.t
ziv@2216 253 | Field of string * string
ziv@2216 254
ziv@2216 255 structure AtomExpKey : ORD_KEY = struct
ziv@2216 256
ziv@2234 257 type ord_key = atomExp
ziv@2216 258
ziv@2234 259 val compare =
ziv@2234 260 fn (QueryArg n1, QueryArg n2) => Int.compare (n1, n2)
ziv@2234 261 | (QueryArg _, _) => LESS
ziv@2234 262 | (_, QueryArg _) => GREATER
ziv@2234 263 | (DmlRel n1, DmlRel n2) => Int.compare (n1, n2)
ziv@2234 264 | (DmlRel _, _) => LESS
ziv@2234 265 | (_, DmlRel _) => GREATER
ziv@2234 266 | (Prim p1, Prim p2) => Prim.compare (p1, p2)
ziv@2234 267 | (Prim _, _) => LESS
ziv@2234 268 | (_, Prim _) => GREATER
ziv@2234 269 | (Field (t1, f1), Field (t2, f2)) =>
ziv@2234 270 case String.compare (t1, t2) of
ziv@2234 271 EQUAL => String.compare (f1, f2)
ziv@2234 272 | ord => ord
ziv@2216 273
ziv@2216 274 end
ziv@2216 275
ziv@2244 276 structure AtomOptionKey = OptionKeyFn(AtomExpKey)
ziv@2244 277
ziv@2216 278 structure UF = UnionFindFn(AtomExpKey)
ziv@2234 279
ziv@2235 280 structure ConflictMaps = struct
ziv@2235 281
ziv@2235 282 structure TK = TripleKeyFn(structure I = CmpKey
ziv@2244 283 structure J = AtomOptionKey
ziv@2244 284 structure K = AtomOptionKey)
ziv@2244 285 structure TS : ORD_SET = BinarySetFn(TK)
ziv@2235 286
ziv@2235 287 val toKnownEquality =
ziv@2235 288 (* [NONE] here means unkown. Anything that isn't a comparison between two
ziv@2235 289 knowns shouldn't be used, and simply dropping unused terms is okay in
ziv@2235 290 disjunctive normal form. *)
ziv@2235 291 fn (Sql.Eq, SOME e1, SOME e2) => SOME (e1, e2)
ziv@2235 292 | _ => NONE
ziv@2235 293
ziv@2235 294 val equivClasses : (Sql.cmp * atomExp option * atomExp option) list -> atomExp list list =
ziv@2235 295 UF.classes
ziv@2235 296 o List.foldl UF.union' UF.empty
ziv@2235 297 o List.mapPartial toKnownEquality
ziv@2235 298
ziv@2235 299 fun addToEqs (eqs, n, e) =
ziv@2235 300 case IM.find (eqs, n) of
ziv@2235 301 (* Comparing to a constant is probably better than comparing to a
ziv@2235 302 variable? Checking that existing constants match a new ones is
ziv@2235 303 handled by [accumulateEqs]. *)
ziv@2235 304 SOME (Prim _) => eqs
ziv@2235 305 | _ => IM.insert (eqs, n, e)
ziv@2235 306
ziv@2235 307 val accumulateEqs =
ziv@2235 308 (* [NONE] means we have a contradiction. *)
ziv@2235 309 fn (_, NONE) => NONE
ziv@2235 310 | ((Prim p1, Prim p2), eqso) =>
ziv@2235 311 (case Prim.compare (p1, p2) of
ziv@2235 312 EQUAL => eqso
ziv@2235 313 | _ => NONE)
ziv@2235 314 | ((QueryArg n, Prim p), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
ziv@2235 315 | ((QueryArg n, DmlRel r), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
ziv@2235 316 | ((Prim p, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
ziv@2235 317 | ((DmlRel r, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
ziv@2235 318 (* TODO: deal with equalities between [DmlRel]s and [Prim]s.
ziv@2235 319 This would involve guarding the invalidation with a check for the
ziv@2235 320 relevant comparisons. *)
ziv@2235 321 | (_, eqso) => eqso
ziv@2235 322
ziv@2235 323 val eqsOfClass : atomExp list -> atomExp IM.map option =
ziv@2235 324 List.foldl accumulateEqs (SOME IM.empty)
ziv@2235 325 o chooseTwos
ziv@2235 326
ziv@2235 327 fun toAtomExps rel (cmp, e1, e2) =
ziv@2235 328 let
ziv@2235 329 val qa =
ziv@2235 330 (* Here [NONE] means unkown. *)
ziv@2235 331 fn Sql.SqConst p => SOME (Prim p)
ziv@2235 332 | Sql.Field tf => SOME (Field tf)
ziv@2235 333 | Sql.Inj (EPrim p, _) => SOME (Prim p)
ziv@2235 334 | Sql.Inj (ERel n, _) => SOME (rel n)
ziv@2235 335 (* We can't deal with anything else, e.g., CURRENT_TIMESTAMP
ziv@2235 336 becomes Sql.Unmodeled, which becomes NONE here. *)
ziv@2235 337 | _ => NONE
ziv@2235 338 in
ziv@2235 339 (cmp, qa e1, qa e2)
ziv@2235 340 end
ziv@2235 341
ziv@2244 342 val negateCmp =
ziv@2244 343 fn Sql.Eq => Sql.Ne
ziv@2244 344 | Sql.Ne => Sql.Eq
ziv@2244 345 | Sql.Lt => Sql.Ge
ziv@2244 346 | Sql.Le => Sql.Gt
ziv@2244 347 | Sql.Gt => Sql.Le
ziv@2244 348 | Sql.Ge => Sql.Lt
ziv@2244 349
ziv@2244 350 fun normalizeAtom (negating, (cmp, e1, e2)) =
ziv@2244 351 (* Restricting to Le/Lt and sorting the expressions in Eq/Ne helps with
ziv@2244 352 simplification, where we put the triples in sets. *)
ziv@2244 353 case (if negating then negateCmp cmp else cmp) of
ziv@2244 354 Sql.Eq => (case AtomOptionKey.compare (e1, e2) of
ziv@2244 355 LESS => (Sql.Eq, e2, e1)
ziv@2244 356 | _ => (Sql.Eq, e1, e2))
ziv@2244 357 | Sql.Ne => (case AtomOptionKey.compare (e1, e2) of
ziv@2244 358 LESS => (Sql.Ne, e2, e1)
ziv@2244 359 | _ => (Sql.Ne, e1, e2))
ziv@2244 360 | Sql.Lt => (Sql.Lt, e1, e2)
ziv@2244 361 | Sql.Le => (Sql.Le, e1, e2)
ziv@2244 362 | Sql.Gt => (Sql.Lt, e2, e1)
ziv@2244 363 | Sql.Ge => (Sql.Le, e2, e1)
ziv@2235 364
ziv@2235 365 val markQuery : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
ziv@2235 366 (Sql.cmp * atomExp option * atomExp option) formula =
ziv@2235 367 mapFormula (toAtomExps QueryArg)
ziv@2235 368
ziv@2235 369 val markDml : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
ziv@2235 370 (Sql.cmp * atomExp option * atomExp option) formula =
ziv@2235 371 mapFormula (toAtomExps DmlRel)
ziv@2250 372
ziv@2235 373 (* No eqs should have key conflicts because no variable is in two
ziv@2235 374 equivalence classes, so the [#1] could be [#2]. *)
ziv@2235 375 val mergeEqs : (atomExp IntBinaryMap.map option list
ziv@2235 376 -> atomExp IntBinaryMap.map option) =
ziv@2235 377 List.foldr (fn (SOME eqs, SOME acc) => SOME (IM.unionWith #1 (eqs, acc)) | _ => NONE)
ziv@2235 378 (SOME IM.empty)
ziv@2235 379
ziv@2239 380 val simplify =
ziv@2239 381 map TS.listItems
ziv@2239 382 o removeRedundant (fn (x, y) => TS.isSubset (y, x))
ziv@2239 383 o map (fn xs => TS.addList (TS.empty, xs))
ziv@2239 384
ziv@2235 385 fun dnf (fQuery, fDml) =
ziv@2244 386 normalize simplify normalizeAtom Disj (Combo (Conj, [markQuery fQuery, markDml fDml]))
ziv@2235 387
ziv@2235 388 val conflictMaps = List.mapPartial (mergeEqs o map eqsOfClass o equivClasses) o dnf
ziv@2235 389
ziv@2235 390 end
ziv@2235 391
ziv@2235 392 val conflictMaps = ConflictMaps.conflictMaps
ziv@2213 393
ziv@2216 394 val rec sqexpToFormula =
ziv@2234 395 fn Sql.SqTrue => Combo (Conj, [])
ziv@2234 396 | Sql.SqFalse => Combo (Disj, [])
ziv@2216 397 | Sql.SqNot e => Negate (sqexpToFormula e)
ziv@2216 398 | Sql.Binop (Sql.RCmp c, e1, e2) => Atom (c, e1, e2)
ziv@2234 399 | Sql.Binop (Sql.RLop l, p1, p2) => Combo (case l of Sql.And => Conj | Sql.Or => Disj,
ziv@2216 400 [sqexpToFormula p1, sqexpToFormula p2])
ziv@2216 401 (* ASK: any other sqexps that can be props? *)
ziv@2216 402 | _ => raise Match
ziv@2213 403
ziv@2218 404 fun renameTables tablePairs =
ziv@2216 405 let
ziv@2216 406 fun renameString table =
ziv@2216 407 case List.find (fn (_, t) => table = t) tablePairs of
ziv@2216 408 NONE => table
ziv@2216 409 | SOME (realTable, _) => realTable
ziv@2216 410 val renameSqexp =
ziv@2216 411 fn Sql.Field (table, field) => Sql.Field (renameString table, field)
ziv@2216 412 | e => e
ziv@2218 413 fun renameAtom (cmp, e1, e2) = (cmp, renameSqexp e1, renameSqexp e2)
ziv@2216 414 in
ziv@2218 415 mapFormula renameAtom
ziv@2216 416 end
ziv@2218 417
ziv@2218 418 val rec queryToFormula =
ziv@2234 419 fn Sql.Query1 {Where = NONE, ...} => Combo (Conj, [])
ziv@2218 420 | Sql.Query1 {From = tablePairs, Where = SOME e, ...} =>
ziv@2218 421 renameTables tablePairs (sqexpToFormula e)
ziv@2234 422 | Sql.Union (q1, q2) => Combo (Disj, [queryToFormula q1, queryToFormula q2])
ziv@2216 423
ziv@2218 424 fun valsToFormula (table, vals) =
ziv@2234 425 Combo (Conj, map (fn (field, v) => Atom (Sql.Eq, Sql.Field (table, field), v)) vals)
ziv@2218 426
ziv@2216 427 val rec dmlToFormula =
ziv@2221 428 fn Sql.Insert (table, vals) => valsToFormula (table, vals)
ziv@2218 429 | Sql.Delete (table, wher) => renameTables [(table, "T")] (sqexpToFormula wher)
ziv@2218 430 | Sql.Update (table, vals, wher) =>
ziv@2218 431 let
ziv@2221 432 val fWhere = sqexpToFormula wher
ziv@2221 433 val fVals = valsToFormula (table, vals)
ziv@2237 434 val modifiedFields = SS.addList (SS.empty, map #1 vals)
ziv@2221 435 (* TODO: don't use field name hack. *)
ziv@2221 436 val markField =
ziv@2237 437 fn e as Sql.Field (t, v) => if SS.member (modifiedFields, v)
ziv@2237 438 then Sql.Field (t, v ^ "'")
ziv@2237 439 else e
ziv@2221 440 | e => e
ziv@2221 441 val mark = mapFormula (fn (cmp, e1, e2) => (cmp, markField e1, markField e2))
ziv@2218 442 in
ziv@2218 443 renameTables [(table, "T")]
ziv@2234 444 (Combo (Disj, [Combo (Conj, [fVals, mark fWhere]),
ziv@2244 445 Combo (Conj, [mark fVals, fWhere])]))
ziv@2218 446 end
ziv@2213 447
ziv@2213 448 val rec tablesQuery =
ziv@2216 449 fn Sql.Query1 {From = tablePairs, ...} => SS.fromList (map #1 tablePairs)
ziv@2216 450 | Sql.Union (q1, q2) => SS.union (tablesQuery q1, tablesQuery q2)
ziv@2213 451
ziv@2213 452 val tableDml =
ziv@2216 453 fn Sql.Insert (tab, _) => tab
ziv@2216 454 | Sql.Delete (tab, _) => tab
ziv@2216 455 | Sql.Update (tab, _, _) => tab
ziv@2213 456
ziv@2213 457
ziv@2248 458 (***************************)
ziv@2248 459 (* Program Instrumentation *)
ziv@2248 460 (***************************)
ziv@2213 461
ziv@2234 462 val varName =
ziv@2234 463 let
ziv@2234 464 val varNumber = ref 0
ziv@2234 465 in
ziv@2234 466 fn s => (varNumber := !varNumber + 1; s ^ Int.toString (!varNumber))
ziv@2234 467 end
ziv@2234 468
ziv@2233 469 val {check, store, flush, ...} = getCache ()
ziv@2233 470
ziv@2230 471 val dummyLoc = ErrorMsg.dummySpan
ziv@2216 472
ziv@2248 473 val dummyTyp = (TRecord [], dummyLoc)
ziv@2248 474
ziv@2230 475 fun stringExp s = (EPrim (Prim.String (Prim.Normal, s)), dummyLoc)
ziv@2230 476
ziv@2230 477 val stringTyp = (TFfi ("Basis", "string"), dummyLoc)
ziv@2213 478
ziv@2213 479 val sequence =
ziv@2213 480 fn (exp :: exps) =>
ziv@2213 481 let
ziv@2230 482 val loc = dummyLoc
ziv@2213 483 in
ziv@2213 484 List.foldl (fn (e', seq) => ESeq ((seq, loc), (e', loc))) exp exps
ziv@2213 485 end
ziv@2213 486 | _ => raise Match
ziv@2213 487
ziv@2248 488 (* Always increments negative indices as a hack we use later. *)
ziv@2248 489 fun incRels inc =
ziv@2215 490 MonoUtil.Exp.mapB
ziv@2248 491 {typ = fn t' => t',
ziv@2248 492 exp = fn bound =>
ziv@2248 493 (fn ERel n => ERel (if n >= bound orelse n < 0 then n + inc else n)
ziv@2248 494 | e' => e'),
ziv@2248 495 bind = fn (bound, MonoUtil.Exp.RelE _) => bound + 1 | (bound, _) => bound}
ziv@2248 496 0
ziv@2213 497
ziv@2256 498 fun cacheWrap (env, exp, resultTyp, args, i) =
ziv@2213 499 let
ziv@2230 500 val loc = dummyLoc
ziv@2255 501 val rel0 = (ERel 0, loc)
ziv@2261 502 (* DEBUG *)
ziv@2261 503 val () = print (Int.toString i ^ "\n")
ziv@2213 504 in
ziv@2256 505 case MonoFooify.urlify env (rel0, resultTyp) of
ziv@2256 506 NONE => NONE
ziv@2256 507 | SOME urlified =>
ziv@2256 508 let
ziv@2256 509 val () = ffiInfo := {index = i, params = length args} :: !ffiInfo
ziv@2256 510 (* We ensure before this step that all arguments aren't effectful.
ziv@2261 511 by turning them into local variables as needed. *)
ziv@2256 512 val argsInc = map (incRels 1) args
ziv@2256 513 val check = (check (i, args), loc)
ziv@2256 514 val store = (store (i, argsInc, urlified), loc)
ziv@2256 515 in
ziv@2256 516 SOME (ECase
ziv@2256 517 (check,
ziv@2256 518 [((PNone stringTyp, loc),
ziv@2256 519 (ELet (varName "q", resultTyp, exp, (ESeq (store, rel0), loc)), loc)),
ziv@2256 520 ((PSome (stringTyp, (PVar (varName "hit", stringTyp), loc)), loc),
ziv@2256 521 (* Boolean is false because we're not unurlifying from a cookie. *)
ziv@2256 522 (EUnurlify (rel0, resultTyp, false), loc))],
ziv@2256 523 {disc = (TOption stringTyp, loc), result = resultTyp}))
ziv@2256 524 end
ziv@2213 525 end
ziv@2213 526
ziv@2256 527 fun fileMapfoldB doExp file start =
ziv@2248 528 case MonoUtil.File.mapfoldB
ziv@2248 529 {typ = Search.return2,
ziv@2250 530 exp = fn env => fn e' => fn s => Search.Continue (doExp env e' s),
ziv@2248 531 decl = fn _ => Search.return2,
ziv@2248 532 bind = doBind}
ziv@2250 533 MonoEnv.empty file start of
ziv@2213 534 Search.Continue x => x
ziv@2213 535 | Search.Return _ => raise Match
ziv@2213 536
ziv@2256 537 fun fileMap doExp file = #1 (fileMapfoldB (fn _ => fn e => fn _ => (doExp e, ())) file ())
ziv@2213 538
ziv@2221 539 fun factorOutNontrivial text =
ziv@2221 540 let
ziv@2230 541 val loc = dummyLoc
ziv@2221 542 fun strcat (e1, e2) = (EStrcat (e1, e2), loc)
ziv@2221 543 val chunks = Sql.chunkify text
ziv@2221 544 val (newText, newVariables) =
ziv@2221 545 (* Important that this is foldr (to oppose foldl below). *)
ziv@2221 546 List.foldr
ziv@2221 547 (fn (chunk, (qText, newVars)) =>
ziv@2221 548 (* Variable bound to the head of newBs will have the lowest index. *)
ziv@2221 549 case chunk of
ziv@2221 550 Sql.Exp (e as (EPrim _, _)) => (strcat (e, qText), newVars)
ziv@2221 551 | Sql.Exp e =>
ziv@2221 552 let
ziv@2221 553 val n = length newVars
ziv@2221 554 in
ziv@2258 555 (* This is the (n+1)th new variable, so there are
ziv@2221 556 already n new variables bound, so we increment
ziv@2221 557 indices by n. *)
ziv@2221 558 (strcat ((ERel (~(n+1)), loc), qText), incRels n e :: newVars)
ziv@2221 559 end
ziv@2221 560 | Sql.String s => (strcat (stringExp s, qText), newVars))
ziv@2221 561 (stringExp "", [])
ziv@2221 562 chunks
ziv@2221 563 fun wrapLets e' =
ziv@2221 564 (* Important that this is foldl (to oppose foldr above). *)
ziv@2234 565 List.foldl (fn (v, e') => ELet (varName "sqlArg", stringTyp, v, (e', loc)))
ziv@2221 566 e'
ziv@2221 567 newVariables
ziv@2221 568 val numArgs = length newVariables
ziv@2221 569 in
ziv@2221 570 (newText, wrapLets, numArgs)
ziv@2221 571 end
ziv@2221 572
ziv@2215 573 fun addChecking file =
ziv@2213 574 let
ziv@2256 575 val effs = effectfulDecls file
ziv@2250 576 fun doExp env (queryInfo as (tableToIndices, indexToQueryNumArgs, index)) =
ziv@2223 577 fn e' as EQuery {query = origQueryText,
ziv@2223 578 state = resultTyp,
ziv@2223 579 initial, body, tables, exps} =>
ziv@2213 580 let
ziv@2221 581 val (newQueryText, wrapLets, numArgs) = factorOutNontrivial origQueryText
ziv@2215 582 (* Increment once for each new variable just made. *)
ziv@2221 583 val queryExp = incRels numArgs
ziv@2215 584 (EQuery {query = newQueryText,
ziv@2223 585 state = resultTyp,
ziv@2215 586 initial = initial,
ziv@2215 587 body = body,
ziv@2215 588 tables = tables,
ziv@2223 589 exps = exps},
ziv@2230 590 dummyLoc)
ziv@2215 591 val (EQuery {query = queryText, ...}, _) = queryExp
ziv@2235 592 (* DEBUG *)
ziv@2258 593 (* val () = Print.preface ("sqlcache> ", MonoPrint.p_exp MonoEnv.empty queryText) *)
ziv@2230 594 val args = List.tabulate (numArgs, fn n => (ERel n, dummyLoc))
ziv@2213 595 fun bind x f = Option.mapPartial f x
ziv@2215 596 fun guard b x = if b then x else NONE
ziv@2248 597 (* We use dummyTyp here. I think this is okay because databases
ziv@2250 598 don't store (effectful) functions, but perhaps there's some
ziv@2250 599 pathalogical corner case missing.... *)
ziv@2248 600 fun safe bound =
ziv@2250 601 not
ziv@2250 602 o effectful effs
ziv@2250 603 (iterate (fn env => MonoEnv.pushERel env "_" dummyTyp NONE)
ziv@2250 604 bound
ziv@2250 605 env)
ziv@2213 606 val attempt =
ziv@2213 607 (* Ziv misses Haskell's do notation.... *)
ziv@2215 608 guard (safe 0 queryText andalso safe 0 initial andalso safe 2 body) (
ziv@2216 609 bind (Sql.parse Sql.query queryText) (fn queryParsed =>
ziv@2256 610 bind (cacheWrap (env, queryExp, resultTyp, args, index)) (fn cachedExp =>
ziv@2256 611 SOME (wrapLets cachedExp,
ziv@2218 612 (SS.foldr (fn (tab, qi) => SIMM.insert (qi, tab, index))
ziv@2218 613 tableToIndices
ziv@2218 614 (tablesQuery queryParsed),
ziv@2223 615 IM.insert (indexToQueryNumArgs, index, (queryParsed, numArgs)),
ziv@2256 616 index + 1)))))
ziv@2213 617 in
ziv@2213 618 case attempt of
ziv@2213 619 SOME pair => pair
ziv@2261 620 (* We have to increment index conservatively. *)
ziv@2261 621 (* TODO: just use a reference for current index.... *)
ziv@2261 622 | NONE => (e', (tableToIndices, indexToQueryNumArgs, index + 1))
ziv@2213 623 end
ziv@2213 624 | e' => (e', queryInfo)
ziv@2213 625 in
ziv@2256 626 (fileMapfoldB (fn env => fn exp => fn state => doExp env state exp)
ziv@2256 627 file
ziv@2256 628 (SIMM.empty, IM.empty, 0),
ziv@2256 629 effs)
ziv@2213 630 end
ziv@2213 631
ziv@2235 632 structure Invalidations = struct
ziv@2235 633
ziv@2235 634 val loc = dummyLoc
ziv@2235 635
ziv@2235 636 val optionAtomExpToExp =
ziv@2235 637 fn NONE => (ENone stringTyp, loc)
ziv@2235 638 | SOME e => (ESome (stringTyp,
ziv@2235 639 (case e of
ziv@2235 640 DmlRel n => ERel n
ziv@2235 641 | Prim p => EPrim p
ziv@2235 642 (* TODO: make new type containing only these two. *)
ziv@2235 643 | _ => raise Match,
ziv@2235 644 loc)),
ziv@2235 645 loc)
ziv@2235 646
ziv@2235 647 fun eqsToInvalidation numArgs eqs =
ziv@2235 648 let
ziv@2235 649 fun inv n = if n < 0 then [] else IM.find (eqs, n) :: inv (n - 1)
ziv@2235 650 in
ziv@2235 651 inv (numArgs - 1)
ziv@2235 652 end
ziv@2235 653
ziv@2235 654 (* Tests if [ys] makes [xs] a redundant cache invalidation. [NONE] here
ziv@2235 655 represents unknown, which means a wider invalidation. *)
ziv@2235 656 val rec madeRedundantBy : atomExp option list * atomExp option list -> bool =
ziv@2235 657 fn ([], []) => true
ziv@2237 658 | (_ :: xs, NONE :: ys) => madeRedundantBy (xs, ys)
ziv@2235 659 | (SOME x :: xs, SOME y :: ys) => (case AtomExpKey.compare (x, y) of
ziv@2235 660 EQUAL => madeRedundantBy (xs, ys)
ziv@2235 661 | _ => false)
ziv@2235 662 | _ => false
ziv@2235 663
ziv@2235 664 fun eqss (query, dml) = conflictMaps (queryToFormula query, dmlToFormula dml)
ziv@2235 665
ziv@2235 666 fun invalidations ((query, numArgs), dml) =
ziv@2235 667 (map (map optionAtomExpToExp)
ziv@2237 668 o removeRedundant madeRedundantBy
ziv@2235 669 o map (eqsToInvalidation numArgs)
ziv@2235 670 o eqss)
ziv@2235 671 (query, dml)
ziv@2235 672
ziv@2235 673 end
ziv@2235 674
ziv@2235 675 val invalidations = Invalidations.invalidations
ziv@2235 676
ziv@2235 677 (* DEBUG *)
ziv@2235 678 val gunk : ((Sql.query * int) * Sql.dml) list ref = ref []
ziv@2261 679 val gunk' : exp list ref = ref []
ziv@2216 680
ziv@2256 681 fun addFlushing ((file, (tableToIndices, indexToQueryNumArgs, index)), effs) =
ziv@2213 682 let
ziv@2257 683 val flushes = List.concat
ziv@2257 684 o map (fn (i, argss) => map (fn args => flush (i, args)) argss)
ziv@2213 685 val doExp =
ziv@2221 686 fn EDml (origDmlText, failureMode) =>
ziv@2213 687 let
ziv@2261 688 (* DEBUG *)
ziv@2261 689 val () = gunk' := origDmlText :: !gunk'
ziv@2221 690 val (newDmlText, wrapLets, numArgs) = factorOutNontrivial origDmlText
ziv@2221 691 val dmlText = incRels numArgs newDmlText
ziv@2221 692 val dmlExp = EDml (dmlText, failureMode)
ziv@2235 693 (* DEBUG *)
ziv@2261 694 val () = Print.preface ("sqlcache> ", (MonoPrint.p_exp MonoEnv.empty origDmlText))
ziv@2261 695 val inval =
ziv@2216 696 case Sql.parse Sql.dml dmlText of
ziv@2218 697 SOME dmlParsed =>
ziv@2261 698 SOME (map (fn i => (case IM.find (indexToQueryNumArgs, i) of
ziv@2261 699 SOME queryNumArgs =>
ziv@2261 700 (* DEBUG *)
ziv@2261 701 (gunk := (queryNumArgs, dmlParsed) :: !gunk;
ziv@2261 702 (i, invalidations (queryNumArgs, dmlParsed)))
ziv@2261 703 (* TODO: fail more gracefully. *)
ziv@2261 704 | NONE => raise Match))
ziv@2261 705 (SIMM.findList (tableToIndices, tableDml dmlParsed)))
ziv@2261 706 | NONE => NONE
ziv@2213 707 in
ziv@2261 708 case inval of
ziv@2261 709 (* TODO: fail more gracefully. *)
ziv@2261 710 NONE => raise Match
ziv@2261 711 | SOME invs => wrapLets (sequence (flushes invs @ [dmlExp]))
ziv@2213 712 end
ziv@2213 713 | e' => e'
ziv@2213 714 in
ziv@2235 715 (* DEBUG *)
ziv@2235 716 gunk := [];
ziv@2256 717 (fileMap doExp file, index, effs)
ziv@2213 718 end
ziv@2213 719
ziv@2221 720 val inlineSql =
ziv@2221 721 let
ziv@2221 722 val doExp =
ziv@2221 723 (* TODO: EQuery, too? *)
ziv@2221 724 (* ASK: should this live in [MonoOpt]? *)
ziv@2221 725 fn EDml ((ECase (disc, cases, {disc = dTyp, ...}), loc), failureMode) =>
ziv@2221 726 let
ziv@2221 727 val newCases = map (fn (p, e) => (p, (EDml (e, failureMode), loc))) cases
ziv@2221 728 in
ziv@2221 729 ECase (disc, newCases, {disc = dTyp, result = (TRecord [], loc)})
ziv@2221 730 end
ziv@2221 731 | e => e
ziv@2221 732 in
ziv@2221 733 fileMap doExp
ziv@2221 734 end
ziv@2221 735
ziv@2250 736
ziv@2250 737 (**********************)
ziv@2250 738 (* Mono Type Checking *)
ziv@2250 739 (**********************)
ziv@2250 740
ziv@2250 741 fun typOfExp' (env : MonoEnv.env) : exp' -> typ option =
ziv@2250 742 fn EPrim p => SOME (TFfi ("Basis", case p of
ziv@2250 743 Prim.Int _ => "int"
ziv@2250 744 | Prim.Float _ => "double"
ziv@2250 745 | Prim.String _ => "string"
ziv@2250 746 | Prim.Char _ => "char"),
ziv@2250 747 dummyLoc)
ziv@2250 748 | ERel n => SOME (#2 (MonoEnv.lookupERel env n))
ziv@2250 749 | ENamed n => SOME (#2 (MonoEnv.lookupENamed env n))
ziv@2250 750 (* ASK: okay to make a new [ref] each time? *)
ziv@2250 751 | ECon (dk, PConVar nCon, _) =>
ziv@2250 752 let
ziv@2250 753 val (_, _, nData) = MonoEnv.lookupConstructor env nCon
ziv@2250 754 val (_, cs) = MonoEnv.lookupDatatype env nData
ziv@2250 755 in
ziv@2250 756 SOME (TDatatype (nData, ref (dk, cs)), dummyLoc)
ziv@2250 757 end
ziv@2250 758 | ECon (_, PConFfi {mod = s, datatyp, ...}, _) => SOME (TFfi (s, datatyp), dummyLoc)
ziv@2250 759 | ENone t => SOME (TOption t, dummyLoc)
ziv@2250 760 | ESome (t, _) => SOME (TOption t, dummyLoc)
ziv@2250 761 | EFfi _ => NONE
ziv@2250 762 | EFfiApp _ => NONE
ziv@2250 763 | EApp (e1, e2) => (case typOfExp env e1 of
ziv@2250 764 SOME (TFun (_, t), _) => SOME t
ziv@2250 765 | _ => NONE)
ziv@2250 766 | EAbs (_, t1, t2, _) => SOME (TFun (t1, t2), dummyLoc)
ziv@2250 767 (* ASK: is this right? *)
ziv@2250 768 | EUnop (unop, e) => (case unop of
ziv@2250 769 "!" => SOME (TFfi ("Basis", "bool"), dummyLoc)
ziv@2250 770 | "-" => typOfExp env e
ziv@2250 771 | _ => NONE)
ziv@2250 772 (* ASK: how should this (and other "=> NONE" cases) work? *)
ziv@2250 773 | EBinop _ => NONE
ziv@2250 774 | ERecord fields => SOME (TRecord (map (fn (s, _, t) => (s, t)) fields), dummyLoc)
ziv@2250 775 | EField (e, s) => (case typOfExp env e of
ziv@2250 776 SOME (TRecord fields, _) =>
ziv@2250 777 (case List.find (fn (s', _) => s = s') fields of
ziv@2250 778 SOME (_, t) => SOME t
ziv@2250 779 | _ => NONE)
ziv@2250 780 | _ => NONE)
ziv@2250 781 | ECase (_, _, {result, ...}) => SOME result
ziv@2250 782 | EStrcat _ => SOME (TFfi ("Basis", "string"), dummyLoc)
ziv@2250 783 | EWrite _ => SOME (TRecord [], dummyLoc)
ziv@2250 784 | ESeq (_, e) => typOfExp env e
ziv@2250 785 | ELet (s, t, e1, e2) => typOfExp (MonoEnv.pushERel env s t (SOME e1)) e2
ziv@2250 786 | EClosure _ => NONE
ziv@2250 787 | EUnurlify (_, t, _) => SOME t
ziv@2256 788 | _ => NONE
ziv@2250 789
ziv@2250 790 and typOfExp env (e', loc) = typOfExp' env e'
ziv@2250 791
ziv@2250 792
ziv@2250 793 (*******************************)
ziv@2250 794 (* Caching Pure Subexpressions *)
ziv@2250 795 (*******************************)
ziv@2250 796
ziv@2257 797 val freeVars =
ziv@2257 798 IS.listItems
ziv@2257 799 o MonoUtil.Exp.foldB
ziv@2257 800 {typ = #2,
ziv@2257 801 exp = fn (bound, ERel n, vars) => if n < bound
ziv@2257 802 then vars
ziv@2257 803 else IS.add (vars, n - bound)
ziv@2257 804 | (_, _, vars) => vars,
ziv@2257 805 bind = fn (bound, MonoUtil.Exp.RelE _) => bound + 1 | (bound, _) => bound}
ziv@2257 806 0
ziv@2257 807 IS.empty
ziv@2257 808
ziv@2258 809 val expSize = MonoUtil.Exp.fold {typ = #2, exp = fn (_, n) => n+1} 0
ziv@2258 810
ziv@2260 811 structure InvalidationInfo :> sig
ziv@2260 812 type t
ziv@2261 813 val empty : t
ziv@2260 814 val fromList : int list -> t
ziv@2260 815 val toList : t -> int list
ziv@2260 816 val union : t * t -> t
ziv@2260 817 val unbind : t * int -> t option
ziv@2260 818 end = struct
ziv@2260 819
ziv@2260 820 (* Keep track of the minimum explicitly. NONE is the empty set. *)
ziv@2260 821 type t = (int * IS.set) option
ziv@2260 822
ziv@2260 823 val fromList =
ziv@2260 824 List.foldl
ziv@2260 825 (fn (n, NONE) => SOME (n, IS.singleton n)
ziv@2260 826 | (n', SOME (n, ns)) => SOME (Int.min (n, n'), IS.add (ns, n')))
ziv@2260 827 NONE
ziv@2260 828
ziv@2261 829 val empty = fromList []
ziv@2261 830
ziv@2260 831 val toList =
ziv@2260 832 fn NONE => []
ziv@2260 833 | SOME (_, ns) => IS.listItems ns
ziv@2260 834
ziv@2260 835 val union =
ziv@2260 836 fn (SOME (n1, ns1), SOME (n2, ns2)) => SOME (Int.min (n1, n2), IS.union (ns1, ns2))
ziv@2261 837 | (NONE, info) => info
ziv@2261 838 | (info, NONE) => info
ziv@2260 839
ziv@2260 840 val unbind =
ziv@2260 841 fn (SOME (n, ns), unbound) =>
ziv@2260 842 let
ziv@2260 843 val n = n - unbound
ziv@2260 844 in
ziv@2260 845 if n < 0
ziv@2260 846 then NONE
ziv@2260 847 else SOME (SOME (n, IS.map (fn n => n - unbound) ns))
ziv@2260 848 end
ziv@2260 849 | _ => SOME NONE
ziv@2260 850
ziv@2260 851 end
ziv@2260 852
ziv@2261 853 val unionUnbind =
ziv@2261 854 List.foldl
ziv@2261 855 (fn (_, NONE) => NONE
ziv@2261 856 | ((info, unbound), SOME infoAcc) =>
ziv@2261 857 case InvalidationInfo.unbind (info, unbound) of
ziv@2261 858 NONE => NONE
ziv@2261 859 | SOME info => SOME (InvalidationInfo.union (info, infoAcc)))
ziv@2261 860 (SOME InvalidationInfo.empty)
ziv@2261 861
ziv@2250 862 datatype subexp = Pure of unit -> exp | Impure of exp
ziv@2250 863
ziv@2250 864 val isImpure =
ziv@2250 865 fn Pure _ => false
ziv@2250 866 | Impure _ => true
ziv@2250 867
ziv@2250 868 val expOfSubexp =
ziv@2250 869 fn Pure f => f ()
ziv@2250 870 | Impure e => e
ziv@2250 871
ziv@2259 872 (* TODO: pick a number. *)
ziv@2259 873 val sizeWorthCaching = 5
ziv@2259 874
ziv@2256 875 fun makeCache (env, exp', index) =
ziv@2256 876 case typOfExp' env exp' of
ziv@2256 877 NONE => NONE
ziv@2256 878 | SOME (TFun _, _) => NONE
ziv@2256 879 | SOME typ =>
ziv@2259 880 if expSize (exp', dummyLoc) < sizeWorthCaching
ziv@2258 881 then NONE
ziv@2258 882 else case List.foldr (fn ((_, _), NONE) => NONE
ziv@2258 883 | ((n, typ), SOME args) =>
ziv@2258 884 case MonoFooify.urlify env ((ERel n, dummyLoc), typ) of
ziv@2258 885 NONE => NONE
ziv@2258 886 | SOME arg => SOME (arg :: args))
ziv@2258 887 (SOME [])
ziv@2258 888 (map (fn n => (n, #2 (MonoEnv.lookupERel env n)))
ziv@2258 889 (freeVars (exp', dummyLoc))) of
ziv@2258 890 NONE => NONE
ziv@2258 891 | SOME args => cacheWrap (env, (exp', dummyLoc), typ, args, index)
ziv@2250 892
ziv@2256 893 fun pureCache (effs : IS.set) ((env, exp as (exp', loc)), index) : subexp * int =
ziv@2250 894 let
ziv@2250 895 fun wrapBindN f (args : (MonoEnv.env * exp) list) =
ziv@2250 896 let
ziv@2256 897 val (subexps, index) = ListUtil.foldlMap (pureCache effs) index args
ziv@2256 898 fun mkExp () = (f (map expOfSubexp subexps), loc)
ziv@2250 899 in
ziv@2250 900 if List.exists isImpure subexps
ziv@2256 901 then (Impure (mkExp ()), index)
ziv@2256 902 else (Pure (fn () => case makeCache (env, f (map #2 args), index) of
ziv@2256 903 NONE => mkExp ()
ziv@2256 904 | SOME e' => (e', loc)),
ziv@2256 905 (* Conservatively increment index. *)
ziv@2256 906 index + 1)
ziv@2250 907 end
ziv@2250 908 fun wrapBind1 f arg =
ziv@2250 909 wrapBindN (fn [arg] => f arg | _ => raise Match) [arg]
ziv@2250 910 fun wrapBind2 f (arg1, arg2) =
ziv@2250 911 wrapBindN (fn [arg1, arg2] => f (arg1, arg2) | _ => raise Match) [arg1, arg2]
ziv@2250 912 fun wrapN f es = wrapBindN f (map (fn e => (env, e)) es)
ziv@2250 913 fun wrap1 f e = wrapBind1 f (env, e)
ziv@2250 914 fun wrap2 f (e1, e2) = wrapBind2 f ((env, e1), (env, e2))
ziv@2250 915 in
ziv@2250 916 case exp' of
ziv@2250 917 ECon (dk, pc, SOME e) => wrap1 (fn e => ECon (dk, pc, SOME e)) e
ziv@2250 918 | ESome (t, e) => wrap1 (fn e => ESome (t, e)) e
ziv@2250 919 | EFfiApp (s1, s2, args) =>
ziv@2258 920 if ffiEffectful (s1, s2)
ziv@2258 921 then (Impure exp, index)
ziv@2258 922 else wrapN (fn es =>
ziv@2258 923 EFfiApp (s1, s2, ListPair.map (fn (e, (_, t)) => (e, t)) (es, args)))
ziv@2258 924 (map #1 args)
ziv@2250 925 | EApp (e1, e2) => wrap2 EApp (e1, e2)
ziv@2250 926 | EAbs (s, t1, t2, e) =>
ziv@2250 927 wrapBind1 (fn e => EAbs (s, t1, t2, e))
ziv@2250 928 (MonoEnv.pushERel env s t1 NONE, e)
ziv@2250 929 | EUnop (s, e) => wrap1 (fn e => EUnop (s, e)) e
ziv@2250 930 | EBinop (bi, s, e1, e2) => wrap2 (fn (e1, e2) => EBinop (bi, s, e1, e2)) (e1, e2)
ziv@2250 931 | ERecord fields =>
ziv@2250 932 wrapN (fn es => ERecord (ListPair.map (fn (e, (s, _, t)) => (s, e, t)) (es, fields)))
ziv@2250 933 (map #2 fields)
ziv@2250 934 | EField (e, s) => wrap1 (fn e => EField (e, s)) e
ziv@2250 935 | ECase (e, cases, {disc, result}) =>
ziv@2250 936 wrapBindN (fn (e::es) =>
ziv@2250 937 ECase (e,
ziv@2250 938 (ListPair.map (fn (e, (p, _)) => (p, e)) (es, cases)),
ziv@2256 939 {disc = disc, result = result})
ziv@2256 940 | _ => raise Match)
ziv@2250 941 ((env, e) :: map (fn (p, e) => (MonoEnv.patBinds env p, e)) cases)
ziv@2250 942 | EStrcat (e1, e2) => wrap2 EStrcat (e1, e2)
ziv@2250 943 (* We record page writes, so they're cachable. *)
ziv@2250 944 | EWrite e => wrap1 EWrite e
ziv@2250 945 | ESeq (e1, e2) => wrap2 ESeq (e1, e2)
ziv@2250 946 | ELet (s, t, e1, e2) =>
ziv@2250 947 wrapBind2 (fn (e1, e2) => ELet (s, t, e1, e2))
ziv@2250 948 ((env, e1), (MonoEnv.pushERel env s t (SOME e1), e2))
ziv@2250 949 (* ASK: | EClosure (n, es) => ? *)
ziv@2250 950 | EUnurlify (e, t, b) => wrap1 (fn e => EUnurlify (e, t, b)) e
ziv@2250 951 | _ => if effectful effs env exp
ziv@2256 952 then (Impure exp, index)
ziv@2256 953 else (Pure (fn () => (case makeCache (env, exp', index) of
ziv@2256 954 NONE => exp'
ziv@2256 955 | SOME e' => e',
ziv@2256 956 loc)),
ziv@2256 957 index + 1)
ziv@2256 958 end
ziv@2256 959
ziv@2261 960 fun addPure ((decls, sideInfo), indexStart, effs) =
ziv@2256 961 let
ziv@2261 962 fun doVal env ((x, n, t, exp, s), index) =
ziv@2256 963 let
ziv@2261 964 val (subexp, index) = pureCache effs ((env, exp), index)
ziv@2256 965 in
ziv@2256 966 ((x, n, t, expOfSubexp subexp, s), index)
ziv@2256 967 end
ziv@2261 968 fun doDecl' env (decl', index) =
ziv@2256 969 case decl' of
ziv@2256 970 DVal v =>
ziv@2256 971 let
ziv@2261 972 val (v, index) = doVal env (v, index)
ziv@2256 973 in
ziv@2256 974 (DVal v, index)
ziv@2256 975 end
ziv@2256 976 | DValRec vs =>
ziv@2256 977 let
ziv@2261 978 val (vs, index) = ListUtil.foldlMap (doVal env) index vs
ziv@2256 979 in
ziv@2256 980 (DValRec vs, index)
ziv@2256 981 end
ziv@2256 982 | _ => (decl', index)
ziv@2261 983 fun doDecl (decl as (decl', loc), (revDecls, env, index)) =
ziv@2256 984 let
ziv@2261 985 val env = MonoEnv.declBinds env decl
ziv@2261 986 val (decl', index) = doDecl' env (decl', index)
ziv@2261 987 (* Important that this happens after [MonoFooify.urlify] calls! *)
ziv@2261 988 val fmDecls = MonoFooify.getNewFmDecls ()
ziv@2256 989 in
ziv@2261 990 ((decl', loc) :: (fmDecls @ revDecls), env, index)
ziv@2256 991 end
ziv@2256 992 in
ziv@2261 993 (rev (#1 (List.foldl doDecl ([], MonoEnv.empty, indexStart) decls)), sideInfo)
ziv@2256 994 end
ziv@2256 995
ziv@2261 996 val go' = addPure o addFlushing o addChecking (* DEBUG: add back [o inlineSql]. *)
ziv@2256 997
ziv@2256 998 fun go file =
ziv@2256 999 let
ziv@2256 1000 (* TODO: do something nicer than [Sql] being in one of two modes. *)
ziv@2256 1001 val () = (resetFfiInfo (); Sql.sqlcacheMode := true)
ziv@2256 1002 val file' = go' file
ziv@2256 1003 val () = Sql.sqlcacheMode := false
ziv@2256 1004 in
ziv@2256 1005 file'
ziv@2250 1006 end
ziv@2250 1007
ziv@2209 1008 end