annotate src/sqlcache.sml @ 2273:a3cac6cea625

Consildation of caches understands sqlification.
author Ziv Scully <ziv@mit.edu>
date Wed, 04 Nov 2015 20:12:07 -0500
parents 85f91c7452b0
children 0730e217fc9c
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@2268 18 (* Filled in by [addFlushing]. *)
ziv@2268 19 val ffiInfoRef : {index : int, params : int} list ref = ref []
ziv@2209 20
ziv@2268 21 fun resetFfiInfo () = ffiInfoRef := []
ziv@2227 22
ziv@2268 23 fun getFfiInfo () = !ffiInfoRef
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@2265 44 (* ASK: is it okay to hardcode Sqlcache functions as effectful? *)
ziv@2215 45 fn (m, f) => Settings.isEffectful (m, f)
ziv@2258 46 andalso not (m = "Basis" andalso SS.member (okayWrites, f))
ziv@2215 47 end
ziv@2215 48
ziv@2234 49 val cache = ref LruCache.cache
ziv@2233 50 fun setCache c = cache := c
ziv@2233 51 fun getCache () = !cache
ziv@2233 52
ziv@2248 53 (* Used to have type context for local variables in MonoUtil functions. *)
ziv@2248 54 val doBind =
ziv@2262 55 fn (env, MonoUtil.Exp.RelE (x, t)) => MonoEnv.pushERel env x t NONE
ziv@2262 56 | (env, MonoUtil.Exp.NamedE (x, n, t, eo, s)) => MonoEnv.pushENamed env x n t eo s
ziv@2262 57 | (env, MonoUtil.Exp.Datatype (x, n, cs)) => MonoEnv.pushDatatype env x n cs
ziv@2215 58
ziv@2271 59 val dummyLoc = ErrorMsg.dummySpan
ziv@2271 60
ziv@2271 61
ziv@2271 62 (*********************)
ziv@2271 63 (* General Utilities *)
ziv@2271 64 (*********************)
ziv@2266 65
ziv@2266 66 (* From the MLton wiki. *)
ziv@2273 67 infix 3 <\ fun x <\ f = fn y => f (x, y) (* Left section *)
ziv@2273 68 infix 3 \> fun f \> y = f y (* Left application *)
ziv@2266 69
ziv@2271 70 fun mapFst f (x, y) = (f x, y)
ziv@2271 71
ziv@2266 72 (* Option monad. *)
ziv@2266 73 fun obind (x, f) = Option.mapPartial f x
ziv@2266 74 fun oguard (b, x) = if b then x else NONE
ziv@2271 75 fun omap f = fn SOME x => SOME (f x) | _ => NONE
ziv@2271 76 fun omap2 f = fn (SOME x, SOME y) => SOME (f (x,y)) | _ => NONE
ziv@2271 77 fun osequence ys = List.foldr (omap2 op::) (SOME []) ys
ziv@2248 78
ziv@2271 79 fun indexOf test =
ziv@2271 80 let
ziv@2271 81 fun f n =
ziv@2271 82 fn [] => NONE
ziv@2271 83 | (x::xs) => if test x then SOME n else f (n+1) xs
ziv@2271 84 in
ziv@2271 85 f 0
ziv@2271 86 end
ziv@2268 87
ziv@2248 88 (*******************)
ziv@2248 89 (* Effect Analysis *)
ziv@2248 90 (*******************)
ziv@2215 91
ziv@2216 92 (* Makes an exception for [EWrite] (which is recorded when caching). *)
ziv@2248 93 fun effectful (effs : IS.set) =
ziv@2215 94 let
ziv@2248 95 val isFunction =
ziv@2248 96 fn (TFun _, _) => true
ziv@2248 97 | _ => false
ziv@2250 98 fun doExp (env, e) =
ziv@2248 99 case e of
ziv@2248 100 EPrim _ => false
ziv@2248 101 (* For now: variables of function type might be effectful, but
ziv@2248 102 others are fully evaluated and are therefore not effectful. *)
ziv@2250 103 | ERel n => isFunction (#2 (MonoEnv.lookupERel env n))
ziv@2248 104 | ENamed n => IS.member (effs, n)
ziv@2248 105 | EFfi (m, f) => ffiEffectful (m, f)
ziv@2248 106 | EFfiApp (m, f, _) => ffiEffectful (m, f)
ziv@2248 107 (* These aren't effectful unless a subexpression is. *)
ziv@2248 108 | ECon _ => false
ziv@2248 109 | ENone _ => false
ziv@2248 110 | ESome _ => false
ziv@2248 111 | EApp _ => false
ziv@2248 112 | EAbs _ => false
ziv@2248 113 | EUnop _ => false
ziv@2248 114 | EBinop _ => false
ziv@2248 115 | ERecord _ => false
ziv@2248 116 | EField _ => false
ziv@2248 117 | ECase _ => false
ziv@2248 118 | EStrcat _ => false
ziv@2248 119 (* EWrite is a special exception because we record writes when caching. *)
ziv@2248 120 | EWrite _ => false
ziv@2248 121 | ESeq _ => false
ziv@2248 122 | ELet _ => false
ziv@2250 123 | EUnurlify _ => false
ziv@2248 124 (* ASK: what should we do about closures? *)
ziv@2248 125 (* Everything else is some sort of effect. We could flip this and
ziv@2248 126 explicitly list bits of Mono that are effectful, but this is
ziv@2248 127 conservatively robust to future changes (however unlikely). *)
ziv@2248 128 | _ => true
ziv@2215 129 in
ziv@2248 130 MonoUtil.Exp.existsB {typ = fn _ => false, exp = doExp, bind = doBind}
ziv@2215 131 end
ziv@2215 132
ziv@2215 133 (* TODO: test this. *)
ziv@2252 134 fun effectfulDecls (decls, _) =
ziv@2215 135 let
ziv@2248 136 fun doVal ((_, name, _, e, _), effs) =
ziv@2250 137 if effectful effs MonoEnv.empty e
ziv@2248 138 then IS.add (effs, name)
ziv@2248 139 else effs
ziv@2215 140 val doDecl =
ziv@2248 141 fn ((DVal v, _), effs) => doVal (v, effs)
ziv@2248 142 (* Repeat the list of declarations a number of times equal to its size,
ziv@2248 143 making sure effectfulness propagates everywhere it should. This is
ziv@2248 144 analagous to the Bellman-Ford algorithm. *)
ziv@2248 145 | ((DValRec vs, _), effs) =>
ziv@2248 146 List.foldl doVal effs (List.concat (List.map (fn _ => vs) vs))
ziv@2215 147 (* ASK: any other cases? *)
ziv@2248 148 | (_, effs) => effs
ziv@2215 149 in
ziv@2248 150 List.foldl doDecl IS.empty decls
ziv@2215 151 end
ziv@2215 152
ziv@2215 153
ziv@2248 154 (*********************************)
ziv@2248 155 (* Boolean Formula Normalization *)
ziv@2248 156 (*********************************)
ziv@2216 157
ziv@2234 158 datatype junctionType = Conj | Disj
ziv@2216 159
ziv@2216 160 datatype 'atom formula =
ziv@2216 161 Atom of 'atom
ziv@2216 162 | Negate of 'atom formula
ziv@2234 163 | Combo of junctionType * 'atom formula list
ziv@2216 164
ziv@2243 165 (* Guaranteed to have all negation pushed to the atoms. *)
ziv@2243 166 datatype 'atom formula' =
ziv@2243 167 Atom' of 'atom
ziv@2243 168 | Combo' of junctionType * 'atom formula' list
ziv@2243 169
ziv@2234 170 val flipJt = fn Conj => Disj | Disj => Conj
ziv@2216 171
ziv@2236 172 fun concatMap f xs = List.concat (map f xs)
ziv@2216 173
ziv@2216 174 val rec cartesianProduct : 'a list list -> 'a list list =
ziv@2216 175 fn [] => [[]]
ziv@2236 176 | (xs :: xss) => concatMap (fn ys => concatMap (fn x => [x :: ys]) xs)
ziv@2236 177 (cartesianProduct xss)
ziv@2216 178
ziv@2218 179 (* Pushes all negation to the atoms.*)
ziv@2244 180 fun pushNegate (normalizeAtom : bool * 'atom -> 'atom) (negating : bool) =
ziv@2244 181 fn Atom x => Atom' (normalizeAtom (negating, x))
ziv@2244 182 | Negate f => pushNegate normalizeAtom (not negating) f
ziv@2244 183 | Combo (j, fs) => Combo' (if negating then flipJt j else j,
ziv@2244 184 map (pushNegate normalizeAtom negating) fs)
ziv@2218 185
ziv@2218 186 val rec flatten =
ziv@2243 187 fn Combo' (_, [f]) => flatten f
ziv@2243 188 | Combo' (j, fs) =>
ziv@2243 189 Combo' (j, List.foldr (fn (f, acc) =>
ziv@2243 190 case f of
ziv@2243 191 Combo' (j', fs') =>
ziv@2243 192 if j = j' orelse length fs' = 1
ziv@2243 193 then fs' @ acc
ziv@2243 194 else f :: acc
ziv@2243 195 | _ => f :: acc)
ziv@2243 196 []
ziv@2243 197 (map flatten fs))
ziv@2218 198 | f => f
ziv@2218 199
ziv@2243 200 (* [simplify] operates on the desired normal form. E.g., if [junc] is [Disj],
ziv@2243 201 consider the list of lists to be a disjunction of conjunctions. *)
ziv@2237 202 fun normalize' (simplify : 'a list list -> 'a list list)
ziv@2235 203 (junc : junctionType) =
ziv@2216 204 let
ziv@2235 205 fun norm junc =
ziv@2237 206 simplify
ziv@2243 207 o (fn Atom' x => [[x]]
ziv@2243 208 | Combo' (j, fs) =>
ziv@2235 209 let
ziv@2236 210 val fss = map (norm junc) fs
ziv@2235 211 in
ziv@2236 212 if j = junc
ziv@2236 213 then List.concat fss
ziv@2236 214 else map List.concat (cartesianProduct fss)
ziv@2235 215 end)
ziv@2216 216 in
ziv@2235 217 norm junc
ziv@2216 218 end
ziv@2216 219
ziv@2244 220 fun normalize simplify normalizeAtom junc =
ziv@2243 221 normalize' simplify junc
ziv@2235 222 o flatten
ziv@2244 223 o pushNegate normalizeAtom false
ziv@2216 224
ziv@2221 225 fun mapFormula mf =
ziv@2221 226 fn Atom x => Atom (mf x)
ziv@2221 227 | Negate f => Negate (mapFormula mf f)
ziv@2235 228 | Combo (j, fs) => Combo (j, map (mapFormula mf) fs)
ziv@2216 229
ziv@2230 230
ziv@2248 231 (****************)
ziv@2248 232 (* SQL Analysis *)
ziv@2248 233 (****************)
ziv@2213 234
ziv@2240 235 structure CmpKey = struct
ziv@2235 236
ziv@2235 237 type ord_key = Sql.cmp
ziv@2235 238
ziv@2235 239 val compare =
ziv@2235 240 fn (Sql.Eq, Sql.Eq) => EQUAL
ziv@2235 241 | (Sql.Eq, _) => LESS
ziv@2235 242 | (_, Sql.Eq) => GREATER
ziv@2235 243 | (Sql.Ne, Sql.Ne) => EQUAL
ziv@2235 244 | (Sql.Ne, _) => LESS
ziv@2235 245 | (_, Sql.Ne) => GREATER
ziv@2235 246 | (Sql.Lt, Sql.Lt) => EQUAL
ziv@2235 247 | (Sql.Lt, _) => LESS
ziv@2235 248 | (_, Sql.Lt) => GREATER
ziv@2235 249 | (Sql.Le, Sql.Le) => EQUAL
ziv@2235 250 | (Sql.Le, _) => LESS
ziv@2235 251 | (_, Sql.Le) => GREATER
ziv@2235 252 | (Sql.Gt, Sql.Gt) => EQUAL
ziv@2235 253 | (Sql.Gt, _) => LESS
ziv@2235 254 | (_, Sql.Gt) => GREATER
ziv@2235 255 | (Sql.Ge, Sql.Ge) => EQUAL
ziv@2235 256
ziv@2235 257 end
ziv@2235 258
ziv@2216 259 val rec chooseTwos : 'a list -> ('a * 'a) list =
ziv@2216 260 fn [] => []
ziv@2216 261 | x :: ys => map (fn y => (x, y)) ys @ chooseTwos ys
ziv@2213 262
ziv@2237 263 fun removeRedundant madeRedundantBy zs =
ziv@2237 264 let
ziv@2237 265 fun removeRedundant' (xs, ys) =
ziv@2237 266 case xs of
ziv@2237 267 [] => ys
ziv@2237 268 | x :: xs' =>
ziv@2237 269 removeRedundant' (xs',
ziv@2237 270 if List.exists (fn y => madeRedundantBy (x, y)) (xs' @ ys)
ziv@2237 271 then ys
ziv@2237 272 else x :: ys)
ziv@2237 273 in
ziv@2237 274 removeRedundant' (zs, [])
ziv@2237 275 end
ziv@2237 276
ziv@2216 277 datatype atomExp =
ziv@2216 278 QueryArg of int
ziv@2216 279 | DmlRel of int
ziv@2216 280 | Prim of Prim.t
ziv@2216 281 | Field of string * string
ziv@2216 282
ziv@2216 283 structure AtomExpKey : ORD_KEY = struct
ziv@2216 284
ziv@2234 285 type ord_key = atomExp
ziv@2216 286
ziv@2234 287 val compare =
ziv@2234 288 fn (QueryArg n1, QueryArg n2) => Int.compare (n1, n2)
ziv@2234 289 | (QueryArg _, _) => LESS
ziv@2234 290 | (_, QueryArg _) => GREATER
ziv@2234 291 | (DmlRel n1, DmlRel n2) => Int.compare (n1, n2)
ziv@2234 292 | (DmlRel _, _) => LESS
ziv@2234 293 | (_, DmlRel _) => GREATER
ziv@2234 294 | (Prim p1, Prim p2) => Prim.compare (p1, p2)
ziv@2234 295 | (Prim _, _) => LESS
ziv@2234 296 | (_, Prim _) => GREATER
ziv@2234 297 | (Field (t1, f1), Field (t2, f2)) =>
ziv@2234 298 case String.compare (t1, t2) of
ziv@2234 299 EQUAL => String.compare (f1, f2)
ziv@2234 300 | ord => ord
ziv@2216 301
ziv@2216 302 end
ziv@2216 303
ziv@2244 304 structure AtomOptionKey = OptionKeyFn(AtomExpKey)
ziv@2244 305
ziv@2271 306 val rec tablesOfQuery =
ziv@2271 307 fn Sql.Query1 {From = tablePairs, ...} => SS.fromList (map #1 tablePairs)
ziv@2271 308 | Sql.Union (q1, q2) => SS.union (tablesOfQuery q1, tablesOfQuery q2)
ziv@2271 309
ziv@2271 310 val tableOfDml =
ziv@2271 311 fn Sql.Insert (tab, _) => tab
ziv@2271 312 | Sql.Delete (tab, _) => tab
ziv@2271 313 | Sql.Update (tab, _, _) => tab
ziv@2271 314
ziv@2271 315 val freeVars =
ziv@2271 316 MonoUtil.Exp.foldB
ziv@2271 317 {typ = #2,
ziv@2271 318 exp = fn (bound, ERel n, vars) => if n < bound
ziv@2271 319 then vars
ziv@2271 320 else IS.add (vars, n - bound)
ziv@2271 321 | (_, _, vars) => vars,
ziv@2273 322 bind = fn (bound, MonoUtil.Exp.RelE _) => bound + 1
ziv@2273 323 | (bound, _) => bound}
ziv@2271 324 0
ziv@2271 325 IS.empty
ziv@2271 326
ziv@2271 327 datatype unbind = Known of exp | Unknowns of int
ziv@2271 328
ziv@2273 329 datatype cacheArg = AsIs of exp | Urlify of exp
ziv@2273 330
ziv@2271 331 structure InvalInfo :> sig
ziv@2271 332 type t
ziv@2271 333 type state = {tableToIndices : SIMM.multimap,
ziv@2271 334 indexToInvalInfo : (t * int) IntBinaryMap.map,
ziv@2271 335 ffiInfo : {index : int, params : int} list,
ziv@2271 336 index : int}
ziv@2271 337 val empty : t
ziv@2271 338 val singleton : Sql.query -> t
ziv@2271 339 val query : t -> Sql.query
ziv@2273 340 val orderArgs : t * IS.set -> cacheArg list
ziv@2271 341 val unbind : t * unbind -> t option
ziv@2271 342 val union : t * t -> t
ziv@2271 343 val updateState : t * int * state -> state
ziv@2271 344 end = struct
ziv@2271 345
ziv@2273 346 datatype sqlArg = FreeVar of int | Sqlify of string * string * sqlArg * typ
ziv@2273 347
ziv@2273 348 type subst = sqlArg IM.map
ziv@2273 349
ziv@2273 350 (* TODO: store free variables as well? *)
ziv@2273 351 type t = (Sql.query * subst) list
ziv@2271 352
ziv@2271 353 type state = {tableToIndices : SIMM.multimap,
ziv@2271 354 indexToInvalInfo : (t * int) IntBinaryMap.map,
ziv@2271 355 ffiInfo : {index : int, params : int} list,
ziv@2271 356 index : int}
ziv@2271 357
ziv@2273 358 structure AM = BinaryMapFn(struct
ziv@2273 359 type ord_key = sqlArg
ziv@2273 360 (* Saw this on MLton wiki. *)
ziv@2273 361 fun ifNotEq (cmp, thunk) = case cmp of
ziv@2273 362 EQUAL => thunk ()
ziv@2273 363 | _ => cmp
ziv@2273 364 fun try f x () = f x
ziv@2273 365 val rec compare =
ziv@2273 366 fn (FreeVar n1, FreeVar n2) =>
ziv@2273 367 Int.compare (n1, n2)
ziv@2273 368 | (FreeVar _, _) => LESS
ziv@2273 369 | (_, FreeVar _) => GREATER
ziv@2273 370 | (Sqlify (m1, x1, arg1, t1), Sqlify (m2, x2, arg2, t2)) =>
ziv@2273 371 String.compare (m1, m2)
ziv@2273 372 <\ifNotEq\> try String.compare (x1, x2)
ziv@2273 373 <\ifNotEq\> try MonoUtil.Typ.compare (t1, t2)
ziv@2273 374 <\ifNotEq\> try compare (arg1, arg2)
ziv@2273 375 end)
ziv@2271 376
ziv@2273 377 (* Traversal Utilities *)
ziv@2273 378 (* TODO: get rid of unused ones. *)
ziv@2271 379
ziv@2271 380 (* Need lift', etc. because we don't have rank-2 polymorphism. This should
ziv@2273 381 probably use a functor (an ML one, not Haskell) but works for now. *)
ziv@2271 382 fun traverseSqexp (pure, _, lift, _, lift'', lift2, _) f =
ziv@2271 383 let
ziv@2271 384 val rec tr =
ziv@2271 385 fn Sql.SqNot se => lift Sql.SqNot (tr se)
ziv@2271 386 | Sql.Binop (r, se1, se2) =>
ziv@2271 387 lift2 (fn (trse1, trse2) => Sql.Binop (r, trse1, trse2)) (tr se1, tr se2)
ziv@2271 388 | Sql.SqKnown se => lift Sql.SqKnown (tr se)
ziv@2271 389 | Sql.Inj (e', loc) => lift'' (fn fe' => Sql.Inj (fe', loc)) (f e')
ziv@2271 390 | Sql.SqFunc (s, se) => lift (fn trse => Sql.SqFunc (s, trse)) (tr se)
ziv@2271 391 | se => pure se
ziv@2271 392 in
ziv@2271 393 tr
ziv@2271 394 end
ziv@2271 395
ziv@2271 396 fun traverseQuery (ops as (_, pure', _, lift', _, _, lift2')) f =
ziv@2271 397 let
ziv@2271 398 val rec mp =
ziv@2271 399 fn Sql.Query1 q =>
ziv@2271 400 (case #Where q of
ziv@2271 401 NONE => pure' (Sql.Query1 q)
ziv@2271 402 | SOME se =>
ziv@2271 403 lift' (fn mpse => Sql.Query1 {Select = #Select q,
ziv@2271 404 From = #From q,
ziv@2271 405 Where = SOME mpse})
ziv@2271 406 (traverseSqexp ops f se))
ziv@2271 407 | Sql.Union (q1, q2) => lift2' Sql.Union (mp q1, mp q2)
ziv@2271 408 in
ziv@2271 409 mp
ziv@2271 410 end
ziv@2271 411
ziv@2273 412 (* Include unused tuple elements in argument for convenience of using same
ziv@2273 413 argument as [traverseQuery]. *)
ziv@2273 414 fun traverseIM (pure, _, _, _, _, lift2, _) f =
ziv@2273 415 IM.foldli (fn (k, v, acc) => lift2 (fn (acc, w) => IM.insert (acc, k, w)) (acc, f (k,v)))
ziv@2273 416 (pure IM.empty)
ziv@2271 417
ziv@2273 418 fun traverseSubst (ops as (_, pure', lift, _, _, _, lift2')) f =
ziv@2273 419 let
ziv@2273 420 val rec mp =
ziv@2273 421 fn FreeVar n => f n
ziv@2273 422 | Sqlify (m, x, arg, t) => lift (fn mparg => Sqlify (m, x, mparg, t)) (mp arg)
ziv@2273 423 in
ziv@2273 424 traverseIM ops (fn (_, v) => mp v)
ziv@2273 425 end
ziv@2273 426
ziv@2273 427 fun monoidOps plus zero = (fn _ => zero, fn _ => zero,
ziv@2273 428 fn _ => fn x => x, fn _ => fn x => x, fn _ => fn x => x,
ziv@2273 429 fn _ => plus, fn _ => plus)
ziv@2273 430
ziv@2273 431 val optionOps = (SOME, SOME, omap, omap, omap, omap2, omap2)
ziv@2273 432
ziv@2273 433 fun foldMapQuery plus zero = traverseQuery (monoidOps plus zero)
ziv@2273 434 val omapQuery = traverseQuery optionOps
ziv@2273 435 fun foldMapIM plus zero = traverseIM (monoidOps plus zero)
ziv@2273 436 fun omapIM f = traverseIM optionOps f
ziv@2273 437 fun foldMapSubst plus zero = traverseSubst (monoidOps plus zero)
ziv@2273 438 fun omapSubst f = traverseSubst optionOps f
ziv@2271 439
ziv@2271 440 val varsOfQuery = foldMapQuery IS.union
ziv@2271 441 IS.empty
ziv@2271 442 (fn e' => freeVars (e', dummyLoc))
ziv@2271 443
ziv@2273 444 val varsOfSubst = foldMapSubst IS.union IS.empty IS.singleton
ziv@2273 445
ziv@2271 446 val varsOfList =
ziv@2271 447 fn [] => IS.empty
ziv@2271 448 | (q::qs) => varsOfQuery (List.foldl Sql.Union q qs)
ziv@2271 449
ziv@2273 450 (* Signature Implementation *)
ziv@2273 451
ziv@2273 452 val empty = []
ziv@2273 453
ziv@2273 454 fun singleton q = [(q, IS.foldl (fn (n, acc) => IM.insert (acc, n, FreeVar n))
ziv@2273 455 IM.empty
ziv@2273 456 (varsOfQuery q))]
ziv@2273 457
ziv@2273 458 val union = op@
ziv@2273 459
ziv@2273 460 fun sqlArgsMap (qs : t) =
ziv@2271 461 let
ziv@2273 462 val args =
ziv@2273 463 List.foldl (fn ((q, subst), acc) =>
ziv@2273 464 IM.foldl (fn (arg, acc) => AM.insert (acc, arg, ())) acc subst)
ziv@2273 465 AM.empty
ziv@2273 466 qs
ziv@2273 467 val countRef = ref (~1)
ziv@2273 468 fun count () = (countRef := !countRef + 1; !countRef)
ziv@2273 469 in
ziv@2273 470 (* Maps each arg to a different consecutive integer, starting from 0. *)
ziv@2273 471 AM.map count args
ziv@2273 472 end
ziv@2273 473
ziv@2273 474 val rec expOfArg =
ziv@2273 475 fn FreeVar n => (ERel n, dummyLoc)
ziv@2273 476 | Sqlify (m, x, arg, t) => (EFfiApp (m, x, [(expOfArg arg, t)]), dummyLoc)
ziv@2273 477
ziv@2273 478 fun orderArgs (qs : t, vars) =
ziv@2273 479 let
ziv@2273 480 fun erel n = (ERel n, dummyLoc)
ziv@2273 481 val argsMap = sqlArgsMap qs
ziv@2273 482 val args = map (expOfArg o #1) (AM.listItemsi argsMap)
ziv@2273 483 val invalVars = List.foldl IS.union IS.empty (map freeVars args)
ziv@2271 484 in
ziv@2271 485 (* Put arguments we might invalidate by first. *)
ziv@2273 486 map AsIs args
ziv@2273 487 (* TODO: make sure these variables are okay to remove from the argument list. *)
ziv@2273 488 @ map (Urlify o erel) (IS.listItems (IS.difference (vars, invalVars)))
ziv@2271 489 end
ziv@2271 490
ziv@2271 491 (* As a kludge, we rename the variables in the query to correspond to the
ziv@2271 492 argument of the cache they're part of. *)
ziv@2273 493 fun query (qs : t) =
ziv@2271 494 let
ziv@2273 495 val argsMap = sqlArgsMap qs
ziv@2273 496 fun substitute subst =
ziv@2273 497 fn ERel n => IM.find (subst, n)
ziv@2273 498 <\obind\>
ziv@2273 499 (fn arg =>
ziv@2273 500 AM.find (argsMap, arg)
ziv@2273 501 <\obind\>
ziv@2273 502 (fn n' => SOME (ERel n')))
ziv@2271 503 | _ => raise Match
ziv@2271 504 in
ziv@2273 505 case (map #1 qs) of
ziv@2273 506 (q :: qs) =>
ziv@2273 507 let
ziv@2273 508 val q = List.foldl Sql.Union q qs
ziv@2273 509 val ns = IS.listItems (varsOfQuery q)
ziv@2273 510 val rename =
ziv@2273 511 fn ERel n => omap ERel (indexOf (fn n' => n' = n) ns)
ziv@2273 512 | _ => raise Match
ziv@2273 513 in
ziv@2273 514 case omapQuery rename q of
ziv@2273 515 SOME q => q
ziv@2273 516 (* We should never get NONE because indexOf should never fail. *)
ziv@2273 517 | NONE => raise Match
ziv@2273 518 end
ziv@2273 519 (* We should never reach this case because [updateState] won't
ziv@2273 520 put anything in the state if there are no queries. *)
ziv@2273 521 | [] => raise Match
ziv@2271 522 end
ziv@2271 523
ziv@2273 524 val rec argOfExp =
ziv@2273 525 fn (ERel n, _) => SOME (FreeVar n)
ziv@2273 526 | (EFfiApp ("Basis", x, [(exp, t)]), _) =>
ziv@2273 527 if String.isPrefix "sqlify" x
ziv@2273 528 then omap (fn arg => Sqlify ("Basis", x, arg, t)) (argOfExp exp)
ziv@2273 529 else NONE
ziv@2273 530 | _ => NONE
ziv@2273 531
ziv@2273 532 val unbind1 =
ziv@2273 533 fn Known e =>
ziv@2273 534 let
ziv@2273 535 val replacement = argOfExp e
ziv@2273 536 in
ziv@2273 537 omapSubst (fn 0 => replacement
ziv@2273 538 | n => SOME (FreeVar (n-1)))
ziv@2273 539 end
ziv@2273 540 | Unknowns k => omapSubst (fn n => if n >= k then NONE else SOME (FreeVar (n-k)))
ziv@2271 541
ziv@2271 542 fun unbind (qs, ub) =
ziv@2271 543 case ub of
ziv@2271 544 (* Shortcut if nothing's changing. *)
ziv@2271 545 Unknowns 0 => SOME qs
ziv@2273 546 | _ => osequence (map (fn (q, subst) => unbind1 ub subst
ziv@2273 547 <\obind\>
ziv@2273 548 (fn subst' => SOME (q, subst'))) qs)
ziv@2271 549
ziv@2273 550 fun updateState (qs, numArgs, state as {index, ...} : state) =
ziv@2273 551 {tableToIndices = List.foldr (fn ((q, _), acc) =>
ziv@2271 552 SS.foldl (fn (tab, acc) =>
ziv@2271 553 SIMM.insert (acc, tab, index))
ziv@2271 554 acc
ziv@2271 555 (tablesOfQuery q))
ziv@2271 556 (#tableToIndices state)
ziv@2271 557 qs,
ziv@2271 558 indexToInvalInfo = IM.insert (#indexToInvalInfo state, index, (qs, numArgs)),
ziv@2271 559 ffiInfo = {index = index, params = numArgs} :: #ffiInfo state,
ziv@2271 560 index = index + 1}
ziv@2271 561
ziv@2271 562 end
ziv@2271 563
ziv@2216 564 structure UF = UnionFindFn(AtomExpKey)
ziv@2234 565
ziv@2273 566 val rec sqexpToFormula =
ziv@2273 567 fn Sql.SqTrue => Combo (Conj, [])
ziv@2273 568 | Sql.SqFalse => Combo (Disj, [])
ziv@2273 569 | Sql.SqNot e => Negate (sqexpToFormula e)
ziv@2273 570 | Sql.Binop (Sql.RCmp c, e1, e2) => Atom (c, e1, e2)
ziv@2273 571 | Sql.Binop (Sql.RLop l, p1, p2) => Combo (case l of Sql.And => Conj | Sql.Or => Disj,
ziv@2273 572 [sqexpToFormula p1, sqexpToFormula p2])
ziv@2273 573 (* ASK: any other sqexps that can be props? *)
ziv@2273 574 | _ => raise Match
ziv@2273 575
ziv@2273 576 fun renameTables tablePairs =
ziv@2273 577 let
ziv@2273 578 fun renameString table =
ziv@2273 579 case List.find (fn (_, t) => table = t) tablePairs of
ziv@2273 580 NONE => table
ziv@2273 581 | SOME (realTable, _) => realTable
ziv@2273 582 val renameSqexp =
ziv@2273 583 fn Sql.Field (table, field) => Sql.Field (renameString table, field)
ziv@2273 584 | e => e
ziv@2273 585 fun renameAtom (cmp, e1, e2) = (cmp, renameSqexp e1, renameSqexp e2)
ziv@2273 586 in
ziv@2273 587 mapFormula renameAtom
ziv@2273 588 end
ziv@2273 589
ziv@2273 590 val rec queryToFormula =
ziv@2273 591 fn Sql.Query1 {Where = NONE, ...} => Combo (Conj, [])
ziv@2273 592 | Sql.Query1 {From = tablePairs, Where = SOME e, ...} =>
ziv@2273 593 renameTables tablePairs (sqexpToFormula e)
ziv@2273 594 | Sql.Union (q1, q2) => Combo (Disj, [queryToFormula q1, queryToFormula q2])
ziv@2273 595
ziv@2273 596 fun valsToFormula (table, vals) =
ziv@2273 597 Combo (Conj, map (fn (field, v) => Atom (Sql.Eq, Sql.Field (table, field), v)) vals)
ziv@2273 598
ziv@2273 599 val rec dmlToFormula =
ziv@2273 600 fn Sql.Insert (table, vals) => valsToFormula (table, vals)
ziv@2273 601 | Sql.Delete (table, wher) => renameTables [(table, "T")] (sqexpToFormula wher)
ziv@2273 602 | Sql.Update (table, vals, wher) =>
ziv@2273 603 let
ziv@2273 604 val fWhere = sqexpToFormula wher
ziv@2273 605 val fVals = valsToFormula (table, vals)
ziv@2273 606 val modifiedFields = SS.addList (SS.empty, map #1 vals)
ziv@2273 607 (* TODO: don't use field name hack. *)
ziv@2273 608 val markField =
ziv@2273 609 fn e as Sql.Field (t, v) => if SS.member (modifiedFields, v)
ziv@2273 610 then Sql.Field (t, v ^ "'")
ziv@2273 611 else e
ziv@2273 612 | e => e
ziv@2273 613 val mark = mapFormula (fn (cmp, e1, e2) => (cmp, markField e1, markField e2))
ziv@2273 614 in
ziv@2273 615 renameTables [(table, "T")]
ziv@2273 616 (Combo (Disj, [Combo (Conj, [fVals, mark fWhere]),
ziv@2273 617 Combo (Conj, [mark fVals, fWhere])]))
ziv@2273 618 end
ziv@2273 619
ziv@2273 620 (* val rec toFormula = *)
ziv@2273 621 (* fn (Sql.Union (q1, q2), d) => Combo (Disj, [toFormula (q1, d), toFormula (q2, d)]) *)
ziv@2273 622 (* | (q as Sql.Query1 {Select = items, ...}, d) => *)
ziv@2273 623 (* let *)
ziv@2273 624 (* val selected = osequence (map (fn )) *)
ziv@2273 625 (* in *)
ziv@2273 626 (* case selected of *)
ziv@2273 627 (* NONE => (Combo (Conj, [markQuery (), markDml fDml])) *)
ziv@2273 628 (* end *)
ziv@2273 629
ziv@2235 630 structure ConflictMaps = struct
ziv@2235 631
ziv@2235 632 structure TK = TripleKeyFn(structure I = CmpKey
ziv@2244 633 structure J = AtomOptionKey
ziv@2244 634 structure K = AtomOptionKey)
ziv@2244 635 structure TS : ORD_SET = BinarySetFn(TK)
ziv@2235 636
ziv@2235 637 val toKnownEquality =
ziv@2235 638 (* [NONE] here means unkown. Anything that isn't a comparison between two
ziv@2235 639 knowns shouldn't be used, and simply dropping unused terms is okay in
ziv@2235 640 disjunctive normal form. *)
ziv@2235 641 fn (Sql.Eq, SOME e1, SOME e2) => SOME (e1, e2)
ziv@2235 642 | _ => NONE
ziv@2235 643
ziv@2235 644 val equivClasses : (Sql.cmp * atomExp option * atomExp option) list -> atomExp list list =
ziv@2235 645 UF.classes
ziv@2235 646 o List.foldl UF.union' UF.empty
ziv@2235 647 o List.mapPartial toKnownEquality
ziv@2235 648
ziv@2235 649 fun addToEqs (eqs, n, e) =
ziv@2235 650 case IM.find (eqs, n) of
ziv@2235 651 (* Comparing to a constant is probably better than comparing to a
ziv@2235 652 variable? Checking that existing constants match a new ones is
ziv@2235 653 handled by [accumulateEqs]. *)
ziv@2235 654 SOME (Prim _) => eqs
ziv@2235 655 | _ => IM.insert (eqs, n, e)
ziv@2235 656
ziv@2235 657 val accumulateEqs =
ziv@2235 658 (* [NONE] means we have a contradiction. *)
ziv@2235 659 fn (_, NONE) => NONE
ziv@2235 660 | ((Prim p1, Prim p2), eqso) =>
ziv@2235 661 (case Prim.compare (p1, p2) of
ziv@2235 662 EQUAL => eqso
ziv@2235 663 | _ => NONE)
ziv@2235 664 | ((QueryArg n, Prim p), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
ziv@2235 665 | ((QueryArg n, DmlRel r), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
ziv@2235 666 | ((Prim p, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
ziv@2235 667 | ((DmlRel r, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
ziv@2235 668 (* TODO: deal with equalities between [DmlRel]s and [Prim]s.
ziv@2235 669 This would involve guarding the invalidation with a check for the
ziv@2235 670 relevant comparisons. *)
ziv@2235 671 | (_, eqso) => eqso
ziv@2235 672
ziv@2235 673 val eqsOfClass : atomExp list -> atomExp IM.map option =
ziv@2235 674 List.foldl accumulateEqs (SOME IM.empty)
ziv@2235 675 o chooseTwos
ziv@2235 676
ziv@2235 677 fun toAtomExps rel (cmp, e1, e2) =
ziv@2235 678 let
ziv@2235 679 val qa =
ziv@2235 680 (* Here [NONE] means unkown. *)
ziv@2235 681 fn Sql.SqConst p => SOME (Prim p)
ziv@2235 682 | Sql.Field tf => SOME (Field tf)
ziv@2235 683 | Sql.Inj (EPrim p, _) => SOME (Prim p)
ziv@2235 684 | Sql.Inj (ERel n, _) => SOME (rel n)
ziv@2235 685 (* We can't deal with anything else, e.g., CURRENT_TIMESTAMP
ziv@2235 686 becomes Sql.Unmodeled, which becomes NONE here. *)
ziv@2235 687 | _ => NONE
ziv@2235 688 in
ziv@2235 689 (cmp, qa e1, qa e2)
ziv@2235 690 end
ziv@2235 691
ziv@2244 692 val negateCmp =
ziv@2244 693 fn Sql.Eq => Sql.Ne
ziv@2244 694 | Sql.Ne => Sql.Eq
ziv@2244 695 | Sql.Lt => Sql.Ge
ziv@2244 696 | Sql.Le => Sql.Gt
ziv@2244 697 | Sql.Gt => Sql.Le
ziv@2244 698 | Sql.Ge => Sql.Lt
ziv@2244 699
ziv@2244 700 fun normalizeAtom (negating, (cmp, e1, e2)) =
ziv@2244 701 (* Restricting to Le/Lt and sorting the expressions in Eq/Ne helps with
ziv@2244 702 simplification, where we put the triples in sets. *)
ziv@2244 703 case (if negating then negateCmp cmp else cmp) of
ziv@2244 704 Sql.Eq => (case AtomOptionKey.compare (e1, e2) of
ziv@2244 705 LESS => (Sql.Eq, e2, e1)
ziv@2244 706 | _ => (Sql.Eq, e1, e2))
ziv@2244 707 | Sql.Ne => (case AtomOptionKey.compare (e1, e2) of
ziv@2244 708 LESS => (Sql.Ne, e2, e1)
ziv@2244 709 | _ => (Sql.Ne, e1, e2))
ziv@2244 710 | Sql.Lt => (Sql.Lt, e1, e2)
ziv@2244 711 | Sql.Le => (Sql.Le, e1, e2)
ziv@2244 712 | Sql.Gt => (Sql.Lt, e2, e1)
ziv@2244 713 | Sql.Ge => (Sql.Le, e2, e1)
ziv@2235 714
ziv@2235 715 val markQuery : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
ziv@2235 716 (Sql.cmp * atomExp option * atomExp option) formula =
ziv@2235 717 mapFormula (toAtomExps QueryArg)
ziv@2235 718
ziv@2235 719 val markDml : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
ziv@2235 720 (Sql.cmp * atomExp option * atomExp option) formula =
ziv@2235 721 mapFormula (toAtomExps DmlRel)
ziv@2250 722
ziv@2235 723 (* No eqs should have key conflicts because no variable is in two
ziv@2235 724 equivalence classes, so the [#1] could be [#2]. *)
ziv@2235 725 val mergeEqs : (atomExp IntBinaryMap.map option list
ziv@2235 726 -> atomExp IntBinaryMap.map option) =
ziv@2271 727 List.foldr (omap2 (IM.unionWith #1)) (SOME IM.empty)
ziv@2235 728
ziv@2239 729 val simplify =
ziv@2239 730 map TS.listItems
ziv@2239 731 o removeRedundant (fn (x, y) => TS.isSubset (y, x))
ziv@2239 732 o map (fn xs => TS.addList (TS.empty, xs))
ziv@2239 733
ziv@2235 734 fun dnf (fQuery, fDml) =
ziv@2244 735 normalize simplify normalizeAtom Disj (Combo (Conj, [markQuery fQuery, markDml fDml]))
ziv@2235 736
ziv@2235 737 val conflictMaps = List.mapPartial (mergeEqs o map eqsOfClass o equivClasses) o dnf
ziv@2235 738
ziv@2235 739 end
ziv@2235 740
ziv@2235 741 val conflictMaps = ConflictMaps.conflictMaps
ziv@2213 742
ziv@2213 743
ziv@2265 744 (*************************************)
ziv@2265 745 (* Program Instrumentation Utilities *)
ziv@2265 746 (*************************************)
ziv@2213 747
ziv@2233 748 val {check, store, flush, ...} = getCache ()
ziv@2233 749
ziv@2248 750 val dummyTyp = (TRecord [], dummyLoc)
ziv@2248 751
ziv@2230 752 fun stringExp s = (EPrim (Prim.String (Prim.Normal, s)), dummyLoc)
ziv@2230 753
ziv@2230 754 val stringTyp = (TFfi ("Basis", "string"), dummyLoc)
ziv@2213 755
ziv@2213 756 val sequence =
ziv@2213 757 fn (exp :: exps) =>
ziv@2213 758 let
ziv@2230 759 val loc = dummyLoc
ziv@2213 760 in
ziv@2213 761 List.foldl (fn (e', seq) => ESeq ((seq, loc), (e', loc))) exp exps
ziv@2213 762 end
ziv@2213 763 | _ => raise Match
ziv@2213 764
ziv@2248 765 (* Always increments negative indices as a hack we use later. *)
ziv@2248 766 fun incRels inc =
ziv@2215 767 MonoUtil.Exp.mapB
ziv@2248 768 {typ = fn t' => t',
ziv@2248 769 exp = fn bound =>
ziv@2248 770 (fn ERel n => ERel (if n >= bound orelse n < 0 then n + inc else n)
ziv@2248 771 | e' => e'),
ziv@2248 772 bind = fn (bound, MonoUtil.Exp.RelE _) => bound + 1 | (bound, _) => bound}
ziv@2248 773 0
ziv@2213 774
ziv@2262 775 fun fileTopLevelMapfoldB doTopLevelExp (decls, sideInfo) state =
ziv@2262 776 let
ziv@2262 777 fun doVal env ((x, n, t, exp, s), state) =
ziv@2262 778 let
ziv@2262 779 val (exp, state) = doTopLevelExp env exp state
ziv@2262 780 in
ziv@2262 781 ((x, n, t, exp, s), state)
ziv@2262 782 end
ziv@2262 783 fun doDecl' env (decl', state) =
ziv@2262 784 case decl' of
ziv@2262 785 DVal v =>
ziv@2262 786 let
ziv@2262 787 val (v, state) = doVal env (v, state)
ziv@2262 788 in
ziv@2262 789 (DVal v, state)
ziv@2262 790 end
ziv@2262 791 | DValRec vs =>
ziv@2262 792 let
ziv@2262 793 val (vs, state) = ListUtil.foldlMap (doVal env) state vs
ziv@2262 794 in
ziv@2262 795 (DValRec vs, state)
ziv@2262 796 end
ziv@2262 797 | _ => (decl', state)
ziv@2262 798 fun doDecl (decl as (decl', loc), (env, state)) =
ziv@2262 799 let
ziv@2262 800 val env = MonoEnv.declBinds env decl
ziv@2262 801 val (decl', state) = doDecl' env (decl', state)
ziv@2262 802 in
ziv@2262 803 ((decl', loc), (env, state))
ziv@2262 804 end
ziv@2262 805 val (decls, (_, state)) = (ListUtil.foldlMap doDecl (MonoEnv.empty, state) decls)
ziv@2262 806 in
ziv@2262 807 ((decls, sideInfo), state)
ziv@2262 808 end
ziv@2262 809
ziv@2262 810 fun fileAllMapfoldB doExp file start =
ziv@2248 811 case MonoUtil.File.mapfoldB
ziv@2248 812 {typ = Search.return2,
ziv@2250 813 exp = fn env => fn e' => fn s => Search.Continue (doExp env e' s),
ziv@2248 814 decl = fn _ => Search.return2,
ziv@2248 815 bind = doBind}
ziv@2250 816 MonoEnv.empty file start of
ziv@2213 817 Search.Continue x => x
ziv@2213 818 | Search.Return _ => raise Match
ziv@2213 819
ziv@2262 820 fun fileMap doExp file = #1 (fileAllMapfoldB (fn _ => fn e => fn _ => (doExp e, ())) file ())
ziv@2213 821
ziv@2267 822 (* TODO: make this a bit prettier.... *)
ziv@2267 823 val simplifySql =
ziv@2266 824 let
ziv@2267 825 fun factorOutNontrivial text =
ziv@2267 826 let
ziv@2267 827 val loc = dummyLoc
ziv@2267 828 fun strcat (e1, e2) = (EStrcat (e1, e2), loc)
ziv@2267 829 val chunks = Sql.chunkify text
ziv@2267 830 val (newText, newVariables) =
ziv@2267 831 (* Important that this is foldr (to oppose foldl below). *)
ziv@2267 832 List.foldr
ziv@2267 833 (fn (chunk, (qText, newVars)) =>
ziv@2267 834 (* Variable bound to the head of newVars will have the lowest index. *)
ziv@2267 835 case chunk of
ziv@2267 836 (* EPrim should always be a string in this case. *)
ziv@2267 837 Sql.Exp (e as (EPrim _, _)) => (strcat (e, qText), newVars)
ziv@2267 838 | Sql.Exp e =>
ziv@2267 839 let
ziv@2267 840 val n = length newVars
ziv@2267 841 in
ziv@2267 842 (* This is the (n+1)th new variable, so there are
ziv@2267 843 already n new variables bound, so we increment
ziv@2267 844 indices by n. *)
ziv@2267 845 (strcat ((ERel (~(n+1)), loc), qText), incRels n e :: newVars)
ziv@2267 846 end
ziv@2267 847 | Sql.String s => (strcat (stringExp s, qText), newVars))
ziv@2267 848 (stringExp "", [])
ziv@2267 849 chunks
ziv@2267 850 fun wrapLets e' =
ziv@2267 851 (* Important that this is foldl (to oppose foldr above). *)
ziv@2273 852 List.foldl (fn (v, e') => ELet ("sqlArg", stringTyp, v, (e', loc)))
ziv@2267 853 e'
ziv@2267 854 newVariables
ziv@2267 855 val numArgs = length newVariables
ziv@2267 856 in
ziv@2267 857 (newText, wrapLets, numArgs)
ziv@2267 858 end
ziv@2267 859 fun doExp exp' =
ziv@2267 860 let
ziv@2267 861 val text = case exp' of
ziv@2267 862 EQuery {query = text, ...} => text
ziv@2267 863 | EDml (text, _) => text
ziv@2267 864 | _ => raise Match
ziv@2267 865 val (newText, wrapLets, numArgs) = factorOutNontrivial text
ziv@2267 866 val newExp' = case exp' of
ziv@2267 867 EQuery q => EQuery {query = newText,
ziv@2267 868 exps = #exps q,
ziv@2267 869 tables = #tables q,
ziv@2267 870 state = #state q,
ziv@2267 871 body = #body q,
ziv@2267 872 initial = #initial q}
ziv@2267 873 | EDml (_, failureMode) => EDml (newText, failureMode)
ziv@2267 874 | _ => raise Match
ziv@2267 875 in
ziv@2267 876 (* Increment once for each new variable just made. This is
ziv@2267 877 where we use the negative De Bruijn indices hack. *)
ziv@2267 878 (* TODO: please don't use that hack. As anyone could have
ziv@2267 879 predicted, it was incomprehensible a year later.... *)
ziv@2267 880 wrapLets (#1 (incRels numArgs (newExp', dummyLoc)))
ziv@2267 881 end
ziv@2266 882 in
ziv@2267 883 fileMap (fn exp' => case exp' of
ziv@2267 884 EQuery _ => doExp exp'
ziv@2267 885 | EDml _ => doExp exp'
ziv@2267 886 | _ => exp')
ziv@2266 887 end
ziv@2266 888
ziv@2250 889
ziv@2250 890 (**********************)
ziv@2250 891 (* Mono Type Checking *)
ziv@2250 892 (**********************)
ziv@2250 893
ziv@2250 894 fun typOfExp' (env : MonoEnv.env) : exp' -> typ option =
ziv@2250 895 fn EPrim p => SOME (TFfi ("Basis", case p of
ziv@2250 896 Prim.Int _ => "int"
ziv@2250 897 | Prim.Float _ => "double"
ziv@2250 898 | Prim.String _ => "string"
ziv@2250 899 | Prim.Char _ => "char"),
ziv@2250 900 dummyLoc)
ziv@2250 901 | ERel n => SOME (#2 (MonoEnv.lookupERel env n))
ziv@2250 902 | ENamed n => SOME (#2 (MonoEnv.lookupENamed env n))
ziv@2250 903 (* ASK: okay to make a new [ref] each time? *)
ziv@2250 904 | ECon (dk, PConVar nCon, _) =>
ziv@2250 905 let
ziv@2250 906 val (_, _, nData) = MonoEnv.lookupConstructor env nCon
ziv@2250 907 val (_, cs) = MonoEnv.lookupDatatype env nData
ziv@2250 908 in
ziv@2250 909 SOME (TDatatype (nData, ref (dk, cs)), dummyLoc)
ziv@2250 910 end
ziv@2250 911 | ECon (_, PConFfi {mod = s, datatyp, ...}, _) => SOME (TFfi (s, datatyp), dummyLoc)
ziv@2250 912 | ENone t => SOME (TOption t, dummyLoc)
ziv@2250 913 | ESome (t, _) => SOME (TOption t, dummyLoc)
ziv@2250 914 | EFfi _ => NONE
ziv@2250 915 | EFfiApp _ => NONE
ziv@2250 916 | EApp (e1, e2) => (case typOfExp env e1 of
ziv@2250 917 SOME (TFun (_, t), _) => SOME t
ziv@2250 918 | _ => NONE)
ziv@2250 919 | EAbs (_, t1, t2, _) => SOME (TFun (t1, t2), dummyLoc)
ziv@2250 920 (* ASK: is this right? *)
ziv@2250 921 | EUnop (unop, e) => (case unop of
ziv@2250 922 "!" => SOME (TFfi ("Basis", "bool"), dummyLoc)
ziv@2250 923 | "-" => typOfExp env e
ziv@2250 924 | _ => NONE)
ziv@2250 925 (* ASK: how should this (and other "=> NONE" cases) work? *)
ziv@2250 926 | EBinop _ => NONE
ziv@2250 927 | ERecord fields => SOME (TRecord (map (fn (s, _, t) => (s, t)) fields), dummyLoc)
ziv@2250 928 | EField (e, s) => (case typOfExp env e of
ziv@2250 929 SOME (TRecord fields, _) =>
ziv@2250 930 (case List.find (fn (s', _) => s = s') fields of
ziv@2250 931 SOME (_, t) => SOME t
ziv@2250 932 | _ => NONE)
ziv@2250 933 | _ => NONE)
ziv@2250 934 | ECase (_, _, {result, ...}) => SOME result
ziv@2250 935 | EStrcat _ => SOME (TFfi ("Basis", "string"), dummyLoc)
ziv@2250 936 | EWrite _ => SOME (TRecord [], dummyLoc)
ziv@2250 937 | ESeq (_, e) => typOfExp env e
ziv@2250 938 | ELet (s, t, e1, e2) => typOfExp (MonoEnv.pushERel env s t (SOME e1)) e2
ziv@2250 939 | EClosure _ => NONE
ziv@2250 940 | EUnurlify (_, t, _) => SOME t
ziv@2269 941 | EQuery {state, ...} => SOME state
ziv@2256 942 | _ => NONE
ziv@2250 943
ziv@2250 944 and typOfExp env (e', loc) = typOfExp' env e'
ziv@2250 945
ziv@2250 946
ziv@2266 947 (***********)
ziv@2266 948 (* Caching *)
ziv@2266 949 (***********)
ziv@2250 950
ziv@2267 951 (*
ziv@2267 952
ziv@2267 953 To get the invalidations for a dml, we need (each <- is list-monad-y):
ziv@2267 954 * table <- dml
ziv@2267 955 * cache <- table
ziv@2267 956 * query <- cache
ziv@2267 957 * inval <- (query, dml),
ziv@2267 958 where inval is a list of query argument indices, so
ziv@2267 959 * way to change query args in inval to cache args.
ziv@2267 960 For now, the last one is just
ziv@2267 961 * a map from query arg number to the corresponding free variable (per query)
ziv@2267 962 * a map from free variable to cache arg number (per cache).
ziv@2267 963 Both queries and caches should have IDs.
ziv@2267 964
ziv@2267 965 *)
ziv@2267 966
ziv@2271 967 type state = InvalInfo.state
ziv@2271 968
ziv@2271 969 datatype subexp = Cachable of InvalInfo.t * (state -> exp * state) | Impure of exp
ziv@2271 970
ziv@2271 971 val isImpure =
ziv@2271 972 fn Cachable _ => false
ziv@2271 973 | Impure _ => true
ziv@2271 974
ziv@2271 975 val runSubexp : subexp * state -> exp * state =
ziv@2271 976 fn (Cachable (_, f), state) => f state
ziv@2271 977 | (Impure e, state) => (e, state)
ziv@2271 978
ziv@2271 979 val invalInfoOfSubexp =
ziv@2271 980 fn Cachable (invalInfo, _) => invalInfo
ziv@2271 981 | Impure _ => raise Match
ziv@2271 982
ziv@2271 983 fun cacheWrap (env, exp, typ, args, index) =
ziv@2265 984 let
ziv@2265 985 val loc = dummyLoc
ziv@2265 986 val rel0 = (ERel 0, loc)
ziv@2265 987 in
ziv@2271 988 case MonoFooify.urlify env (rel0, typ) of
ziv@2265 989 NONE => NONE
ziv@2265 990 | SOME urlified =>
ziv@2265 991 let
ziv@2265 992 (* We ensure before this step that all arguments aren't effectful.
ziv@2265 993 by turning them into local variables as needed. *)
ziv@2265 994 val argsInc = map (incRels 1) args
ziv@2268 995 val check = (check (index, args), loc)
ziv@2268 996 val store = (store (index, argsInc, urlified), loc)
ziv@2265 997 in
ziv@2271 998 SOME (ECase (check,
ziv@2271 999 [((PNone stringTyp, loc),
ziv@2273 1000 (ELet ("q", typ, exp, (ESeq (store, rel0), loc)), loc)),
ziv@2273 1001 ((PSome (stringTyp, (PVar ("hit", stringTyp), loc)), loc),
ziv@2271 1002 (* Boolean is false because we're not unurlifying from a cookie. *)
ziv@2271 1003 (EUnurlify (rel0, typ, false), loc))],
ziv@2271 1004 {disc = (TOption stringTyp, loc), result = typ}))
ziv@2265 1005 end
ziv@2265 1006 end
ziv@2265 1007
ziv@2258 1008 val expSize = MonoUtil.Exp.fold {typ = #2, exp = fn (_, n) => n+1} 0
ziv@2258 1009
ziv@2259 1010 (* TODO: pick a number. *)
ziv@2259 1011 val sizeWorthCaching = 5
ziv@2259 1012
ziv@2269 1013 val worthCaching =
ziv@2269 1014 fn EQuery _ => true
ziv@2269 1015 | exp' => expSize (exp', dummyLoc) > sizeWorthCaching
ziv@2269 1016
ziv@2273 1017 fun cacheExp (env, exp', invalInfo, state : state) =
ziv@2273 1018 case worthCaching exp' <\oguard\> typOfExp' env exp' of
ziv@2269 1019 NONE => NONE
ziv@2269 1020 | SOME (TFun _, _) => NONE
ziv@2269 1021 | SOME typ =>
ziv@2271 1022 let
ziv@2273 1023 val args = InvalInfo.orderArgs (invalInfo, freeVars (exp', dummyLoc))
ziv@2273 1024 val numArgs = length args
ziv@2273 1025 in (List.foldr (fn (arg, acc) =>
ziv@2273 1026 acc
ziv@2273 1027 <\obind\>
ziv@2273 1028 (fn args' =>
ziv@2273 1029 (case arg of
ziv@2273 1030 AsIs exp => SOME exp
ziv@2273 1031 | Urlify exp =>
ziv@2273 1032 typOfExp env exp
ziv@2273 1033 <\obind\>
ziv@2273 1034 (fn typ =>
ziv@2273 1035 (MonoFooify.urlify env (exp, typ))))
ziv@2273 1036 <\obind\>
ziv@2273 1037 (fn arg' => SOME (arg' :: args'))))
ziv@2273 1038 (SOME [])
ziv@2273 1039 args)
ziv@2273 1040 <\obind\>
ziv@2273 1041 (fn args' =>
ziv@2273 1042 cacheWrap (env, (exp', dummyLoc), typ, args', #index state)
ziv@2273 1043 <\obind\>
ziv@2273 1044 (fn cachedExp =>
ziv@2273 1045 SOME (cachedExp, InvalInfo.updateState (invalInfo, numArgs, state))))
ziv@2271 1046 end
ziv@2269 1047
ziv@2271 1048 fun cacheQuery (effs, env, q) : subexp =
ziv@2266 1049 let
ziv@2266 1050 (* We use dummyTyp here. I think this is okay because databases don't
ziv@2266 1051 store (effectful) functions, but perhaps there's some pathalogical
ziv@2266 1052 corner case missing.... *)
ziv@2266 1053 fun safe bound =
ziv@2266 1054 not
ziv@2266 1055 o effectful effs
ziv@2266 1056 (iterate (fn env => MonoEnv.pushERel env "_" dummyTyp NONE)
ziv@2266 1057 bound
ziv@2266 1058 env)
ziv@2271 1059 val {query = queryText, initial, body, ...} = q
ziv@2271 1060 (* DEBUG *)
ziv@2271 1061 (* val () = Print.preface ("sqlcache> ", MonoPrint.p_exp MonoEnv.empty queryText) *)
ziv@2266 1062 val attempt =
ziv@2266 1063 (* Ziv misses Haskell's do notation.... *)
ziv@2267 1064 (safe 0 queryText andalso safe 0 initial andalso safe 2 body)
ziv@2273 1065 <\oguard\>
ziv@2268 1066 Sql.parse Sql.query queryText
ziv@2273 1067 <\obind\>
ziv@2268 1068 (fn queryParsed =>
ziv@2271 1069 let
ziv@2271 1070 val invalInfo = InvalInfo.singleton queryParsed
ziv@2271 1071 fun mkExp state =
ziv@2271 1072 case cacheExp (env, EQuery q, invalInfo, state) of
ziv@2271 1073 NONE => ((EQuery q, dummyLoc), state)
ziv@2271 1074 | SOME (cachedExp, state) => ((cachedExp, dummyLoc), state)
ziv@2271 1075 in
ziv@2271 1076 SOME (Cachable (invalInfo, mkExp))
ziv@2271 1077 end)
ziv@2266 1078 in
ziv@2266 1079 case attempt of
ziv@2271 1080 NONE => Impure (EQuery q, dummyLoc)
ziv@2271 1081 | SOME subexp => subexp
ziv@2266 1082 end
ziv@2266 1083
ziv@2271 1084 fun cacheTree (effs : IS.set) ((env, exp as (exp', loc)), state) =
ziv@2250 1085 let
ziv@2271 1086 fun wrapBindN (f : exp list -> exp')
ziv@2271 1087 (args : ((MonoEnv.env * exp) * unbind) list) =
ziv@2250 1088 let
ziv@2271 1089 val (subexps, state) =
ziv@2271 1090 ListUtil.foldlMap (cacheTree effs)
ziv@2271 1091 state
ziv@2271 1092 (map #1 args)
ziv@2268 1093 fun mkExp state = mapFst (fn exps => (f exps, loc))
ziv@2268 1094 (ListUtil.foldlMap runSubexp state subexps)
ziv@2271 1095 val attempt =
ziv@2271 1096 if List.exists isImpure subexps
ziv@2271 1097 then NONE
ziv@2271 1098 else (List.foldl (omap2 InvalInfo.union)
ziv@2271 1099 (SOME InvalInfo.empty)
ziv@2271 1100 (ListPair.map
ziv@2271 1101 (fn (subexp, (_, unbinds)) =>
ziv@2271 1102 InvalInfo.unbind (invalInfoOfSubexp subexp, unbinds))
ziv@2271 1103 (subexps, args)))
ziv@2273 1104 <\obind\>
ziv@2271 1105 (fn invalInfo =>
ziv@2271 1106 SOME (Cachable (invalInfo,
ziv@2271 1107 fn state =>
ziv@2271 1108 case cacheExp (env,
ziv@2271 1109 f (map (#2 o #1) args),
ziv@2271 1110 invalInfo,
ziv@2271 1111 state) of
ziv@2271 1112 NONE => mkExp state
ziv@2271 1113 | SOME (e', state) => ((e', loc), state)),
ziv@2271 1114 state))
ziv@2250 1115 in
ziv@2271 1116 case attempt of
ziv@2271 1117 SOME (subexp, state) => (subexp, state)
ziv@2271 1118 | NONE => mapFst Impure (mkExp state)
ziv@2250 1119 end
ziv@2250 1120 fun wrapBind1 f arg =
ziv@2250 1121 wrapBindN (fn [arg] => f arg | _ => raise Match) [arg]
ziv@2250 1122 fun wrapBind2 f (arg1, arg2) =
ziv@2250 1123 wrapBindN (fn [arg1, arg2] => f (arg1, arg2) | _ => raise Match) [arg1, arg2]
ziv@2271 1124 fun wrapN f es = wrapBindN f (map (fn e => ((env, e), Unknowns 0)) es)
ziv@2271 1125 fun wrap1 f e = wrapBind1 f ((env, e), Unknowns 0)
ziv@2271 1126 fun wrap2 f (e1, e2) = wrapBind2 f (((env, e1), Unknowns 0), ((env, e2), Unknowns 0))
ziv@2250 1127 in
ziv@2250 1128 case exp' of
ziv@2250 1129 ECon (dk, pc, SOME e) => wrap1 (fn e => ECon (dk, pc, SOME e)) e
ziv@2250 1130 | ESome (t, e) => wrap1 (fn e => ESome (t, e)) e
ziv@2250 1131 | EFfiApp (s1, s2, args) =>
ziv@2258 1132 if ffiEffectful (s1, s2)
ziv@2266 1133 then (Impure exp, state)
ziv@2258 1134 else wrapN (fn es =>
ziv@2258 1135 EFfiApp (s1, s2, ListPair.map (fn (e, (_, t)) => (e, t)) (es, args)))
ziv@2258 1136 (map #1 args)
ziv@2250 1137 | EApp (e1, e2) => wrap2 EApp (e1, e2)
ziv@2250 1138 | EAbs (s, t1, t2, e) =>
ziv@2250 1139 wrapBind1 (fn e => EAbs (s, t1, t2, e))
ziv@2271 1140 ((MonoEnv.pushERel env s t1 NONE, e), Unknowns 1)
ziv@2250 1141 | EUnop (s, e) => wrap1 (fn e => EUnop (s, e)) e
ziv@2250 1142 | EBinop (bi, s, e1, e2) => wrap2 (fn (e1, e2) => EBinop (bi, s, e1, e2)) (e1, e2)
ziv@2250 1143 | ERecord fields =>
ziv@2250 1144 wrapN (fn es => ERecord (ListPair.map (fn (e, (s, _, t)) => (s, e, t)) (es, fields)))
ziv@2250 1145 (map #2 fields)
ziv@2250 1146 | EField (e, s) => wrap1 (fn e => EField (e, s)) e
ziv@2250 1147 | ECase (e, cases, {disc, result}) =>
ziv@2250 1148 wrapBindN (fn (e::es) =>
ziv@2250 1149 ECase (e,
ziv@2250 1150 (ListPair.map (fn (e, (p, _)) => (p, e)) (es, cases)),
ziv@2256 1151 {disc = disc, result = result})
ziv@2256 1152 | _ => raise Match)
ziv@2271 1153 (((env, e), Unknowns 0)
ziv@2271 1154 :: map (fn (p, e) =>
ziv@2271 1155 ((MonoEnv.patBinds env p, e), Unknowns (MonoEnv.patBindsN p)))
ziv@2271 1156 cases)
ziv@2250 1157 | EStrcat (e1, e2) => wrap2 EStrcat (e1, e2)
ziv@2250 1158 (* We record page writes, so they're cachable. *)
ziv@2250 1159 | EWrite e => wrap1 EWrite e
ziv@2250 1160 | ESeq (e1, e2) => wrap2 ESeq (e1, e2)
ziv@2250 1161 | ELet (s, t, e1, e2) =>
ziv@2250 1162 wrapBind2 (fn (e1, e2) => ELet (s, t, e1, e2))
ziv@2271 1163 (((env, e1), Unknowns 0),
ziv@2271 1164 ((MonoEnv.pushERel env s t (SOME e1), e2), Known e1))
ziv@2250 1165 (* ASK: | EClosure (n, es) => ? *)
ziv@2250 1166 | EUnurlify (e, t, b) => wrap1 (fn e => EUnurlify (e, t, b)) e
ziv@2271 1167 | EQuery q => (cacheQuery (effs, env, q), state)
ziv@2269 1168 | _ => (if effectful effs env exp
ziv@2269 1169 then Impure exp
ziv@2271 1170 else Cachable (InvalInfo.empty,
ziv@2271 1171 fn state =>
ziv@2271 1172 case cacheExp (env, exp', InvalInfo.empty, state) of
ziv@2269 1173 NONE => ((exp', loc), state)
ziv@2269 1174 | SOME (exp', state) => ((exp', loc), state)),
ziv@2269 1175 state)
ziv@2256 1176 end
ziv@2256 1177
ziv@2266 1178 fun addCaching file =
ziv@2256 1179 let
ziv@2266 1180 val effs = effectfulDecls file
ziv@2271 1181 fun doTopLevelExp env exp state = runSubexp (cacheTree effs ((env, exp), state))
ziv@2256 1182 in
ziv@2271 1183 (fileTopLevelMapfoldB doTopLevelExp
ziv@2271 1184 file
ziv@2271 1185 {tableToIndices = SIMM.empty,
ziv@2271 1186 indexToInvalInfo = IM.empty,
ziv@2271 1187 ffiInfo = [],
ziv@2271 1188 index = 0},
ziv@2271 1189 effs)
ziv@2265 1190 end
ziv@2265 1191
ziv@2265 1192
ziv@2265 1193 (************)
ziv@2265 1194 (* Flushing *)
ziv@2265 1195 (************)
ziv@2265 1196
ziv@2265 1197 structure Invalidations = struct
ziv@2265 1198
ziv@2265 1199 val loc = dummyLoc
ziv@2265 1200
ziv@2265 1201 val optionAtomExpToExp =
ziv@2265 1202 fn NONE => (ENone stringTyp, loc)
ziv@2265 1203 | SOME e => (ESome (stringTyp,
ziv@2265 1204 (case e of
ziv@2265 1205 DmlRel n => ERel n
ziv@2265 1206 | Prim p => EPrim p
ziv@2265 1207 (* TODO: make new type containing only these two. *)
ziv@2265 1208 | _ => raise Match,
ziv@2265 1209 loc)),
ziv@2265 1210 loc)
ziv@2265 1211
ziv@2265 1212 fun eqsToInvalidation numArgs eqs =
ziv@2269 1213 List.tabulate (numArgs, (fn n => IM.find (eqs, n)))
ziv@2265 1214
ziv@2265 1215 (* Tests if [ys] makes [xs] a redundant cache invalidation. [NONE] here
ziv@2265 1216 represents unknown, which means a wider invalidation. *)
ziv@2265 1217 val rec madeRedundantBy : atomExp option list * atomExp option list -> bool =
ziv@2265 1218 fn ([], []) => true
ziv@2265 1219 | (_ :: xs, NONE :: ys) => madeRedundantBy (xs, ys)
ziv@2265 1220 | (SOME x :: xs, SOME y :: ys) => (case AtomExpKey.compare (x, y) of
ziv@2265 1221 EQUAL => madeRedundantBy (xs, ys)
ziv@2265 1222 | _ => false)
ziv@2265 1223 | _ => false
ziv@2265 1224
ziv@2271 1225 fun invalidations ((invalInfo, numArgs), dml) =
ziv@2271 1226 let
ziv@2271 1227 val query = InvalInfo.query invalInfo
ziv@2271 1228 in
ziv@2271 1229 (map (map optionAtomExpToExp)
ziv@2271 1230 o removeRedundant madeRedundantBy
ziv@2271 1231 o map (eqsToInvalidation numArgs)
ziv@2273 1232 o conflictMaps)
ziv@2273 1233 (queryToFormula query, dmlToFormula dml)
ziv@2271 1234 end
ziv@2265 1235
ziv@2265 1236 end
ziv@2265 1237
ziv@2265 1238 val invalidations = Invalidations.invalidations
ziv@2265 1239
ziv@2265 1240 (* DEBUG *)
ziv@2265 1241 (* val gunk : ((Sql.query * int) * Sql.dml) list ref = ref [] *)
ziv@2265 1242 (* val gunk' : exp list ref = ref [] *)
ziv@2265 1243
ziv@2273 1244 fun addFlushing ((file, {tableToIndices, indexToInvalInfo, ffiInfo, ...} : state), effs) =
ziv@2265 1245 let
ziv@2265 1246 val flushes = List.concat
ziv@2265 1247 o map (fn (i, argss) => map (fn args => flush (i, args)) argss)
ziv@2265 1248 val doExp =
ziv@2267 1249 fn dmlExp as EDml (dmlText, failureMode) =>
ziv@2265 1250 let
ziv@2265 1251 (* DEBUG *)
ziv@2265 1252 (* val () = gunk' := origDmlText :: !gunk' *)
ziv@2265 1253 (* val () = Print.preface ("SQLCACHE: ", (MonoPrint.p_exp MonoEnv.empty origDmlText)) *)
ziv@2265 1254 val inval =
ziv@2265 1255 case Sql.parse Sql.dml dmlText of
ziv@2265 1256 SOME dmlParsed =>
ziv@2271 1257 SOME (map (fn i => (case IM.find (indexToInvalInfo, i) of
ziv@2271 1258 SOME invalInfo =>
ziv@2271 1259 (i, invalidations (invalInfo, dmlParsed))
ziv@2265 1260 (* TODO: fail more gracefully. *)
ziv@2271 1261 (* This probably means invalidating everything.... *)
ziv@2265 1262 | NONE => raise Match))
ziv@2271 1263 (SIMM.findList (tableToIndices, tableOfDml dmlParsed)))
ziv@2265 1264 | NONE => NONE
ziv@2265 1265 in
ziv@2265 1266 case inval of
ziv@2265 1267 (* TODO: fail more gracefully. *)
ziv@2265 1268 NONE => raise Match
ziv@2267 1269 | SOME invs => sequence (flushes invs @ [dmlExp])
ziv@2265 1270 end
ziv@2265 1271 | e' => e'
ziv@2265 1272 in
ziv@2265 1273 (* DEBUG *)
ziv@2265 1274 (* gunk := []; *)
ziv@2268 1275 ffiInfoRef := ffiInfo;
ziv@2266 1276 fileMap doExp file
ziv@2265 1277 end
ziv@2265 1278
ziv@2265 1279
ziv@2268 1280 (************************)
ziv@2268 1281 (* Compiler Entry Point *)
ziv@2268 1282 (************************)
ziv@2265 1283
ziv@2265 1284 val inlineSql =
ziv@2265 1285 let
ziv@2265 1286 val doExp =
ziv@2265 1287 (* TODO: EQuery, too? *)
ziv@2265 1288 (* ASK: should this live in [MonoOpt]? *)
ziv@2265 1289 fn EDml ((ECase (disc, cases, {disc = dTyp, ...}), loc), failureMode) =>
ziv@2265 1290 let
ziv@2265 1291 val newCases = map (fn (p, e) => (p, (EDml (e, failureMode), loc))) cases
ziv@2265 1292 in
ziv@2265 1293 ECase (disc, newCases, {disc = dTyp, result = (TRecord [], loc)})
ziv@2265 1294 end
ziv@2265 1295 | e => e
ziv@2265 1296 in
ziv@2265 1297 fileMap doExp
ziv@2265 1298 end
ziv@2265 1299
ziv@2262 1300 fun insertAfterDatatypes ((decls, sideInfo), newDecls) =
ziv@2262 1301 let
ziv@2262 1302 val (datatypes, others) = List.partition (fn (DDatatype _, _) => true | _ => false) decls
ziv@2262 1303 in
ziv@2262 1304 (datatypes @ newDecls @ others, sideInfo)
ziv@2262 1305 end
ziv@2262 1306
ziv@2267 1307 val go' = addFlushing o addCaching o simplifySql o inlineSql
ziv@2256 1308
ziv@2256 1309 fun go file =
ziv@2256 1310 let
ziv@2256 1311 (* TODO: do something nicer than [Sql] being in one of two modes. *)
ziv@2256 1312 val () = (resetFfiInfo (); Sql.sqlcacheMode := true)
ziv@2262 1313 val file = go' file
ziv@2262 1314 (* Important that this happens after [MonoFooify.urlify] calls! *)
ziv@2262 1315 val fmDecls = MonoFooify.getNewFmDecls ()
ziv@2256 1316 val () = Sql.sqlcacheMode := false
ziv@2256 1317 in
ziv@2262 1318 insertAfterDatatypes (file, rev fmDecls)
ziv@2250 1319 end
ziv@2250 1320
ziv@2209 1321 end