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