-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer-algorithm.tex
368 lines (321 loc) · 10.6 KB
/
infer-algorithm.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
\Dchapter{The algorithm}
\Dsection{Step 1}
\begin{figure*}
\begin{mathpar}
\begin{array}{lllll}
groupBy\ f\ c = [(k, v) | e \in c, k = f(e), v = [a | a \in c, f(a) = k]]
\\
containsUnknown(t) = (t = (HMap\ \{k : ?, \overrightarrow{k': t'}\}\ o) \vee t = (HMap\ m \{k' : ?, \overrightarrow{k'': t''}\})))
\\
unionHMaps(hs) = hmapsmerged2
\\
\begin{array}{lllll}
\text{ where }
&commonkeys = \bigcap[k_m | \HMaptwo{\{\overrightarrow{k_m : t}\}}{o} \in hs]\\
&likelytags = [k | k \in commonkeys, \bigwedge [(k,k_1) \in m | (HMap\ m\ o) \in ts]]\\
&hmapsbykeys = \textbf{groupBy}(\lambda \HMaptwo{\{\overrightarrow{ks : t}\}}{o}. ks, hs)\\
&hmapsmerged = \textbf{if}\ likelytags = []\ \textbf{then}\ hs\ \textbf{else}\
[t | ts \in rng(hmapsbykeys), t = joinHMap*(ts)]
\\
&isolatetag() =
\begin{cases}
[(Map\ Any\ Any)], \text{if } \textbf{empty?}(commonkeys)\\
newmaps, \text{if } \neg \textbf{empty?}(likelytags)\\
\begin{array}{lllll}
\text{where}
&k_t = \textbf{first}(likelytags)\\
&hmapsbytags = %[(k_m, ts) | t \in hmapsmerged \wedge t = (HMap\ \{k_t : k_m, \overrightarrow{k': t'}\}\ o) \wedge
% ts = [t | t \in hmaps \wedge t = (HMap\ \{k_t : k_m, \overrightarrow{k'' : t''}\}\ o')]
% ]
\textbf{group-by}(\lambda (HMap\ \{k_t : k_m, \overrightarrow{k': t'}\}\ o). k_m)
hmapsmerged
\\
&newmaps = [t | ts \in hmapsbytags \wedge t = \textbf{fold}\ joinHMap\ ts]
\end{array}\\
hmapsmerged , \text{if} \neg\textbf{empty?}([t | t \in hmapsmerged \wedge containsUnknown(t)])\\
\textbf{fold}\ joinHMap\ hmapsmerged, \text{otherwise}
\end{cases}
\\
% check common keys
&hmapsmerged2 = \textbf{if}\ (\textbf{length}\ hmapsmerged) > 1\ \textbf{then}\ isolatetag() \textbf{else}\ hmapsmerged
\end{array}\\
\\
makeUnion(A, ts) = u
\\
\begin{array}{lllll}
\text{ where }
&hmaps = [(HMap\ m\ o) | (HMap\ m\ o) \in ts]\\
&nonhmaps = [t | t \in ts \wedge t \not= (HMap\ m\ o)]\\
&hmapsmerged = unionHMaps(hmaps)\\
&u = nonhmaps \cup hmapsmerged
\end{array}
\\
\\
\end{array}
\end{mathpar}
\caption{Union constructor. Does not create new aliases, merges HMaps on the same "level"
using optional entries where appropriate. Omitted: merging function types, upcasting certain
combinations of types.}
\end{figure*}
Where: \emph{make-Union}
This step begins by merging HMap's
and functions that are contained in the same union.
Algorithm: Traverse each union and merge members
of the same union with the following merge strategy.
HMaps with 100\% same keys are merged, with
entries being the union of the original HMaps.
In the special case where we can guess a common
dispatch key for all maps
(a common entry mapped to a keyword),
we instead merge using the value of that entry.
If there is a mix of collection and non-collection
types, we upcast the entire union to Any.
From experience, these combinations are rare in
non-polymorphic functions, so instead we require
a polymorphic type variable, which is future work.
Functions with the same arities are merged, with
the new domains being the join of the domains
point-wise, and the new range being the join
of the old ranges.
Assumption: No optional keys. No recursive types.
%TODO
\Dsection{Step 2.a}
\begin{figure*}
\begin{mathpar}
\begin{array}{lllll}
walk(A, t) =
\begin{cases}
doalias(A, t), \text{if } t = (HMap\ m\ o)\\
doalias(A, t), \text{if } t = (\cup\ \overrightarrow{ts}) \wedge \neg\textbf{every?}(\textbf{simple?},\ ts)\\
t, \text{otherwise}
\end{cases}
\\
\end{array}
\end{mathpar}
\caption{Step 2.a}
\end{figure*}
\begin{figure*}
\begin{mathpar}
\begin{array}{lllll}
doalias(A, t) = res\\
\begin{array}{lllll}
\text{ where }
&t' =
\begin{cases}
(\cup\ \overrightarrow{\textbf{map}(\textbf{fullyResolveType}, \overrightarrow{ts})}), \text{if } t = (\cup\ \overrightarrow{ts})\\
t, \text{otherwise}
\end{cases}\\
&n = \textbf{generateName}(A, t')\\
&A' = \textbf{registerAlias}(A, n, t')\\
&res = (A', n')
\end{array}
\\
walk(A, t) =
\begin{cases}
doalias(A, t), \text{if } t = (HMap\ m\ o)\\
doalias(A, t), \text{if } t = (\cup\ \overrightarrow{ts}) \wedge \neg\textbf{every?}(\textbf{simple?},\ ts)\\
t, \text{otherwise}
\end{cases}
\\
aliasHMap(A, t) = \textbf{postwalk}(A, t, walk)
\\
\end{array}
\end{mathpar}
\caption{Step 2.a}
\end{figure*}
Where: \emph{alias-HMap}
Relabel graph nodes to keysets.
Traverse graph up from leaves.
Label each HMap as a singleton set containing
its keyset.
Label each union as a set of keysets of its
members. Delete HMap labels of its members.
There are effectively two types of edges.
"Union edges" don't really exist, since
they're on the same level as their members.
Other edges are normal, thus we don't delete
HMaps labels they point to.
Assumption: No recursive types.
\Dsection{Step 2.b}
\begin{figure*}
\begin{mathpar}
\begin{array}{lllll}
tryMergeAliases(A, f, t) =
\begin{cases}
A2, \text{if } (\textbf{deep-keysets}(f) \cap \textbf{deep-keysets}(t)) \ne \varnothing\\
\begin{array}{lllll}
\text{ where }
% FIXME pretty sure each join argument needs `substAlias` first, check fixed implementation
&nt = join(\textbf{get}(A, f), \textbf{substAlias}(\textbf{get}(A, t), f, t))\\
&A1 = \textbf{assoc}(A, f, t)\\
&A2 = \textbf{assoc}(A1, t, nt)\\
\end{array}\\
A, \text{otherwise}
\end{cases}
\\
squash(A, t) = h(A, [t], [])\\
\begin{array}{lllll}
\text{ where }
h(A, w, d) =
\begin{cases}
A, \text{if } \textbf{empty?}(w)\\
h(A',w',d'), \text{otherwise}\\
\begin{array}{lllll}
\text{ where }
&t = \textbf{first}(w)\\
&as = \textbf{aliases-in}(A, \textbf{resolve-alias}(A, t))\\
&ap = d \setminus \{t\}\\
&A' =
\begin{cases}
\textbf{fold}(\lambda (A', f). tryMergeAliases(A', f, t), A, as \cup ap), \text{if } \textbf{contains?}(d, t)\\
A, \text{otherwise}
\end{cases}\\
&d' = (d \cup \{t\})
\\
&w' = \textbf{rest}(w) \cup (\textbf{aliases-in}(A, t) \setminus d')
\\
\end{array}
\end{cases}\\
\end{array}
\\
squashAll(A, t) = A'
\\
\begin{array}{lllll}
\text{ where }
&as = \textbf{aliases-in}(A, t)\\
&A' = \textbf{fold}(squash, A, as)\\
\end{array}
\end{array}
\end{mathpar}
\caption{Step 2.b}
\end{figure*}
Where: \emph{squash-all}
Vertical recursive types.
Now recursive types are derived by
depth traversal of the types.
We start at the top of each branch and remember
the keysets of the current node.
We then traverse down depth-first and remember
the keyset of each subsequent node.
If we find two nodes that have overlapping set of keysets,
we merge them.
The set of keysets of a HMap are every union of the keys
in its required entry set and the subsets of the
keys in its optional entry set.
The set of keysets of a type is the union of
all the keysets in its members (eg., for a union type,
take the union of the set of keysets of its members).
Here is the merge algorithm:
Given nodes m and n, where non-empty(intersection(keysets(m), keyset(n))),
and m was found before n.
1. Redirect all in/out edges on n to m.
2. Delete node n.
3. Let type(m) = join(type(m), type(n))
4. Let label(m) = union(label(m), label(n))
Question: This algorithm could run until a fixed point.
Should it? The next step does something similar that
merges types in different graph forests, but does
not handle recursive types.
Assumption: No recursive types before we start
(?but surely as we go?)
\Dsection{Step 3.a}
Where: \emph{alias-single-HMaps}
Relabel nodes to HMap keyset.
Relabel each node to its keyset if it is
a HMap, otherwise do not label.
\Dsection{Step 3.b}
Where: \emph{squash-horizontally}
Horizontally merge similar types.
Given a collection of sets of labelled nodes (to HMap
keysets), in each set, choose just the types
that are not recursive (by traversing the type and
noticing if a cycle is made to the original label)
and merge these nodes into the same graph.
The merge is performed by joining the types
of all the candidate nodes to a "new" node. Then reroute all
in/out edges belonging to the candidate
node to the "new" node.
Note: This only merges non-recursive nodes.
\Dsection{Step 4}
%\begin{figure*}
%\begin{mathpar}
% \begin{array}{lllll}
% followTEnv'(A, t, f, s) =
%\begin{cases}
% t, \text{if } \textbf{contains?}(s, f)\\
% t', \text{otherwise}\\
% \begin{array}{lllll}
% \text{ where }
% &t' =
%\begin{cases}
% followTEnv'(A, t, r, s \cup \{f\}), \text{if } \textbf{alias?}(r)\\
% t'', \text{otherwise } \textbf{alias?}(r)\\
% \begin{array}{lllll}
% \text{ where }
% &r' =
%\begin{cases}
% r, \text{if } \textbf{simpleAlias?}(A, f)\\
% f, \text{otherwise}
%\end{cases}\\
% &t'' = t[f \mapsto r']
% \end{array}
%
%\end{cases}\\
% \end{array}
%\end{cases}\\
% \begin{array}{lllll}
% \text{ where }
% &r = \textbf{resolve-alias}(A, f)
% \end{array}\\
% followTypeEnv(A, t) = t'\\
% \begin{array}{lllll}
% \text{ where }
% &(A', t') = \textbf{fold}(\lambda ((A, t), f). followTEnv'(A, t, f, []), (A, t), \textbf{aliases-in}(A, t))
% \end{array}\\
%
% followAEnv'(A, t, f, s) =
%\begin{cases}
% t, \text{if } \textbf{contains?}(s, f)\\
% t', \text{otherwise}\\
% \begin{array}{lllll}
% \text{ where }
% &t' =
%\begin{cases}
% followAEnv'(A, t, r, s \cup \{f\}), \text{if } \textbf{alias?}(r)\\
% t'', \text{otherwise } \textbf{alias?}(r)\\
% \begin{array}{lllll}
% \text{ where }
% &r' =
%\begin{cases}
% r, \text{if } \textbf{simpleAlias?}(A, f)\\
% f, \text{otherwise}
%\end{cases}\\
% &t'' = t[f \mapsto r']
% \end{array}
%
%\end{cases}\\
% \end{array}
%\end{cases}\\
% \begin{array}{lllll}
% \text{ where }
% &r = \textbf{resolve-alias}(A, f)
% \end{array}\\
%
% followAliasEnv(A, \Gamma) = \textbf{fold}(followAEnv', A, as)\\
% \begin{array}{lllll}
% \text{ where }
% &A = \{\overrightarrow{as \mapsto ts}\}
% \end{array}\\
% followall(A, \Gamma) = res\\
% \begin{array}{lllll}
% \text{ where }
% &(A1, \Gamma_1) = \textbf{fold}(followTypeEnv, (A, \Gamma), \Gamma)\\
% &(A2, \Gamma_2) = followAliasEnv(A1, \Gamma_1)\\
% &res = (A2, \Gamma_2)
% \end{array}\\
% \end{array}
%\end{mathpar}
%\caption{Follow-all: Simplify aliases in the type environment and alias environment.}
%\end{figure*}
Where: \emph{follow-all}
Remove aliases that just point to other aliases.