-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer-extensions.tex
388 lines (358 loc) · 15.9 KB
/
infer-extensions.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
\Dchapter{Extensions}
\label{infer:sec:extensions}
Two optimizations are crucial for practical implementations of the collection phase.
First, space-efficient tracking efficiently handles a common case with higher-order
functions where the same function is tracked at multiple paths.
Second, instead of tracking a potentially large value by eagerly traversing it, lazy tracking
offers a pay-as-you-go model by wrapping a value and only tracking subparts as they are accessed.
Both were necessary to collect samples from the compiler implementation we instrumented for
Experiment 1 (\secref{infer:sec:experiment1})
because it used many higher-order functions and its AST representation can be quite large
which made it intractible to eagerly traverse each time it got passed to one of dozens of functions.
%\Dsection{Polymorphic types}
%
%\begin{figure}
% \ifdefined\PAPER
% \footnotesize
% \fi
%\begin{mathpar}
% \begin{altgrammar}
% \e{} &::=& ...
% \alt \trackpolyE{\e{}}{\inferpath{}}{\e{}}
% \alt \genE{}
% &\mbox{Expressions}
% \end{altgrammar}
%
% \infer [B-Gen]
% { \num{} \text{ is fresh}
% }
% { \opsemtrack{\openv{}}{\genE{}}{\num{}}{\emptyres{}}}
%
% \infer [B-TrackPoly]
% { \bigstepgen{\openv{}}{\num0}{\e{}}{\v{}}{\res{1}}{\num1}
% \\\\
% \bigstepgen{\openv{}}{\num1}{\ep{}}{\num{}}{\res{2}}{\num2}
% \\\\
% \trackpolymeta{}(\v{}, \inferpath{}, {\num{}}) = \vp{}\ ; \res{3} }
% { \bigstepgen{\openv{}}{\num0}{\trackpolyE{\e{}}{\inferpath{}}{\ep{}}}{\vp{}}{\bigunionres{\ova{\res{}}}}{\num2} }
%
% \begin{array}{lllll}
% \trackpolymeta{}(\v{}, \inferpath{}, \num{}) = \v{}\ ;\ \res{}\\\\
%
% \trackpolymeta{}(\num{}, \inferpath{}, \num{}')
% &=&
% n\ ; \{\inferpath{} : \IntT{}\}
% \\
% \trackpolymeta{}([\lambda \xvar{}. \e{}, \openv{}], \inferpath{}, \num{})
% &=&
% [
% \lambda \yvar{}.
% \inferletliteral (\xvar{n} \genE{})
% \\&&
% FIXME
% %\trackpolyE{((\lambda \xvar{}. \e{}) \trackpolyE{\yvar{}}{\appendone{\inferpath{}}{\dompe{}}}{\xvar{n}})}
% % {\appendone{\inferpath{}}{\rngpe{}}}{\xvar{n}}
% % , \openv{}]
% % \ ; \{\inferpath{} : [\UnknownT{} \rightarrow \UnknownT{}] \}
% \\
% &&
% \text{where}\ \yvar{} \text{ is fresh}
% \\
% \trackpolymeta{}(\{\ova{\val1\ \val2}\}, \inferpath{}, \num{})
% &=&
% \{\ova{\val1\ \val2{}'}\}
% \ ;\ \ova{\sqcup\ \res{}}
% \sqcup
% \{\inferpath{} : \{\ova{\val1\ \UnknownT{}}\} \}
% \\
% &&
% \text{where}\ \ova{\trackpolymeta{}(\val2, \appendone{\inferpath{}}{\inferkeype{\ova{\val1}}{\val1}}) = \val2{}'\ ;\ \res{}}
% \end{array}
%\end{mathpar}
% \caption{Polymorphic type tracking extensions }
%\end{figure}
\Dsection{Space-efficient tracking}
\label{infer:sec:space-efficient-tracking}
To reduce the overhead of runtime tracking, we can borrow
the concept of ``space-efficient'' contract checking from
the gradual typing literature~\infercitep{Herman:2010}.
%
Instead of tracking just one path at once, a space-efficient
implementation of track threads through a set of paths.
When a tracked value flows into another tracked position,
we extract the unwrapped value, and then our new tracked value
tracks the paths that is the set of the old paths with the new path.
To model this, we introduce a new kind of value \ProxyV{\v{}}{\closure{\e{}}{\openv{}}}{\ova{\inferpath{}}}
that tracks old value \v{} as new value \closure{\e{}}{\openv{}} with the paths \ova{\inferpath{}}.
Proxy expressions are introduced when tracking functions, where instead of just returning
a new wrapped function, we return a proxy.
We can think of function proxies as a normal function with some extra metadata, so we
can reuse the existing semantics for function application---in fact we
can support space-efficient function tracking just by extending \trackEOp{}.
We present the extension in \figref{fig:infer:proxyext}.
The first two \trackEOp{} rules simply make inference
results for each of the paths.
The next rule says that a bare closure
reduces to a proxy that tracks the domain and range
of the closure with respect to the list of paths.
Attached to the proxy is everything needed to extend
it with more paths, which is the role of the
final rule. It extracts the original closure from the
proxy and creates a new proxy with updated paths
via the previous rule.
\begin{figure}
%\ifdefined\PAPER
%\footnotesize
%\fi
\begin{mathpar}
\begin{altgrammar}
\v{} &::=& ... \alt \ProxyVdiff{\closure{\uabs{\x{}}{\e{}}}{\openv{}}}{\closure{\uabs{\x{}}{\e{}}}{\openv{}}}{\ova{\inferpath{}}}
&\mbox{Values}
\end{altgrammar}
\arraycolsep=1.4pt
\begin{array}{lllll}
\trackmetaalign{\num{}}{\ovadiff{\inferpath{}}}{\num{}}{\proxyextdiff{\bigunionres{\ovadiff{\proxyextsame{\singletonres{\inferpath{}}{\IntT{}}}}}}}\\
\trackmetaalign{\kw{}}{\ovadiff{\inferpath{}}}{\kw{}}
{\proxyextdiff{\bigunionres{\ovadiff{\proxyextsame{\singletonres{\inferpath{}}{\Keyword{}}}}}}}\\
\trackmetaalign{\closure{\uabs{\x{}}{\e{}}}{\openv{}}}
{\ovadiff{\inferpath{}}}
{\ProxyVdiff{\closure{\uabs{\x{}}{\e{}}}{\openv{}}}
{\closure{\ep{}}{\openv{}}}
{\ova{\inferpath{}}}}
{\emptyres{}}
\\
&&
\begin{array}{@{}llll}
\text{where } \yvar{} \text{ is fresh},\\
\begin{array}{lllll}
\ep{} =
\uabs{\y{}}{\trackE{&\appexp{(\uabs{\x{}}{\e{}})}{\trackE{\yvar{}}{\ovadiff{\appendone{\inferpath{}}{\dompe{}}}}}}
{\\&\ovadiff{\appendone{\inferpath{}}{\rngpe{}}}}}
\end{array}
\end{array}
\\
\trackmetaalignsplice{\ProxyV{\closure{\uabs{\x{}}{\e{}}}{\openv{}}}{\closure{\ep{}}{\openvp{}}}{\ova{\inferpathp{}}}}{\ova{\inferpath{}}}
{\trackmetalhs{\closure{\uabs{\x{}}{\e{}}}{\openv{}}}{\ova{\inferpath{}} \cup \ova{\inferpathp{}}}}
\end{array}
\end{mathpar}
\caption{Space-efficient tracking extensions (\textcolor{red}{changes})}
\label{fig:infer:proxyext}
\end{figure}
\Dsection{Lazy tracking}
\label{infer:sec:lazy-tracking}
Building further on the extension of space-efficient functions,
we apply a similar idea for tracking maps.
In practice, eagerly walking data structures to gather inference
results is expensive.
Instead,
waiting until a data structure is used and tracking its contents
lazily can help ease this tradeoff, with the side-effect that
fewer inference results are discovered.
\figref{fig:infer:lazy} extends our system with lazy maps.
We add a new kind of value
\MProxyV{\curlymap{\ova{\kw{}\ \v{}}}}{\curlymap{\ova{\kwp{}\ \curlymap{\ova{\HMapreq{}\ \ova{\inferpath{}}}}}}}
that wraps a map \curlymap{\ova{\kw{}\ \v{}}} with tracking information.
Keyword entries \kwp{} are associated with pairs of type information \HMapreq{}
with paths \ova{\inferpath{}}.
The first \trackEOp{} rule demonstrates how to create
a lazily tracked map.
We calculate the possibly tagged entries in our type information in advance,
much like the equivalent rule in \figref{infer:fig:trackmeta}, and store
them for later use. Notice that non-keyword entries are not yet traversed,
and thus no inference results are derived from them.
The second \trackEOp{} rule adds new paths to track.
The subtleties of lazily tracking maps lie in the \constantopsemliteral{}
rules.
The \assocliteral{} and \dissocliteral{} rules ensure we no longer track overwritten entries.
Then, the \getliteral{} rules perform the tracking that was deferred
from the \trackEOp{} rule for maps in \figref{infer:fig:trackmeta}
(if the entry is still tracked).
In our experience, some combination of lazy and eager tracking of
maps strikes a good balance between performance overhead and
quantity of inference results.
Intuitively, if a function does not access parts of its argument,
they should not contribute to that function's type signature.
However, our inference algorithm combines information
\emph{across} function signatures to deduce useful, recursive
type aliases.
Some eager tracking helps normalize the quality of function annotations
with respect to unit test coverage.
For example, say functions \textsf{f} and \textsf{g} operate on the same
types of (deeply nested) arguments, and \textsf{f} has complete test coverage (but does not
traverse all of its arguments), and \textsf{g} has incomplete test coverage
(but fully traverses its arguments).
Eagerly tracking \textsf{f} would give better inference results,
but lazily tracking \textsf{g} is more efficient.
Forcing several layers of tracking helps strike this balance, which
our implementation exposes as a parameter.
This can be achieved in our formal system
by adding fuel arguments to \trackEOp{}
that contain depth and breadth tracking limits, and
defer to lazy tracking when out of fuel.
\begin{figure}
%\ifdefined\PAPER
%\footnotesize
%\fi
\begin{mathpar}
\begin{altgrammar}
\v{} &::=& ... \alt \MProxyVdiff{\curlymap{\ova{\kw{}\ \v{}}}}{\curlymap{\ova{\kw{}\ \curlymap{\ova{\HMapreq{}\ \ova{\inferpath{}}}}}}}
&\mbox{Values}
\end{altgrammar}
\arraycolsep=1.4pt
\begin{array}{lllll}
\trackmetaalign{\curlymap{\ova{\kw{}\ \kwp{}}\ \ova{\kwpp{}\ \v{}}}}
{\ovadiff{\inferpath{}}}
{\MProxyVdiff{\curlymap{\ova{\kw{}\ \kwp{}}\ \ova{\kwpp{}\ \proxyextdiff{\v{}}}}}
{\curlymap{\ova{\kw{}\ \textsf{t}}\ \ova{\kwpp{}\ \textsf{t}}}}}
{\proxyextdiff{\emptyres{}}}
\\
&&
\begin{array}{@{}llll}
\text{where } \textsf{t} = \{\curlymap{\ova{\kw{}\ \kwp{}}\ \ova{\kwpp{}\ \UnknownT{}}}\ \ova{\inferpath{}}\}
\end{array}
\\
\trackmetaalign{\MProxyVdiff{\curlymap{\ova{\kw{}\ \v{}}}}
{\curlymap{\ova{\kwp{}\ \{\HMapreq{}\ \ova{\inferpathp{}}\}}}}}
{\ovadiff{\inferpath{}}}
{\MProxyVdiff{\curlymap{\ova{\kw{}\ \v{}}}}
{\curlymap{\ova{\kwp{}\ \{\HMapreq{}\ (\ova{\inferpath{} \cup}\ \cup\ {\ova{\inferpathp{} \cup}})\}}}}}
{\emptyres{}}
\end{array}
\arraycolsep=1.4pt
\begin{array}{lllr}
\inferconstantopsemalignnospace
{\assocliteral{}}
{\MProxyVdiff{\curlymap{\ova{\kw{}\ \v{}}}}{\curlymap{\kwp{}\ \textsf{t}', \ova{\kwpp{}\ \textsf{t}}}}
, \kwp{}
, \vp{}}
{\MProxyVdiff{\updatemap{\curlymap{\ova{\kw{}\ \v{}}}}{\kwp{}}{\vp{}}}
{\curlymap{\ova{\kwpp{}\ \textsf{t}}}}}
{\emptyres{}}\\
\inferconstantopsemalignnospace
{\assocliteral{}}
{\MProxyVdiff{\curlymap{\ova{\kw{}\ \v{}}}}
{\curlymap{\ova{\kwpp{}\ \textsf{t}}}}
, \kwp{}
, \vp{}}
{\MProxyVdiff{\updatemap{\curlymap{\ova{\kw{}\ \v{}}}}{\kwp{}}{\vp{}}}
{\curlymap{\ova{\kwpp{}\ \textsf{t}}}}}
{\emptyres{}}\\
\inferconstantopsemalignsplice
{\getliteral{}}
{\MProxyVdiff{\curlymap{\kw{}\ \v{}, \ova{\kwp{}\ \vp{}}}}
{\curlymap{\kw{}\ \textsf{t}, \ova{\kwpp{}\ \textsf{t}'}}}
, \kw{}}
{\proxyextdiff{\trackmetalhs{\proxyextsame{\v{}}}{\ova{\inferpath{}}}}}\\
\begin{array}{lllll}
\text{ where } \ova{\inferpath{}} = \big[\appendone{\inferpath{}}{\inferkeype{\HMapreq{}}{\kw{}}}
\ |\
(\HMapreq{}, \ova{\inferpath{}}) \in \textsf{t},
\inferpath{} \in \ova{\inferpath{}}
\big]
\end{array}
\\
\inferconstantopsemalignsplice
{\getliteral{}}
{\MProxyVdiff{\curlymap{\kw{}\ \v{}, \ova{\kwp{}\ \vp{}}}}
{\curlymap{\ova{\kwpp{}\ \textsf{t}'}}}
, \kw{}}
{\v{}}\\
\inferconstantopsemalign
{\dissocliteral{}}
{\MProxyVdiff{\curlymap{\kw{}\ \v{}, \ova{\kwp{}\ \vp{}}}}
{\curlymap{\kw{}\ \textsf{t}, \ova{\kwpp{}\ \textsf{t}'}}}
, \kw{}}
{\MProxyVdiff{\curlymap{\ova{\kwp{}\ \vp{}}}}
{\curlymap{\ova{\kwpp{}\ \textsf{t}'}}}}
{\emptyres{}}
\\
\inferconstantopsemalign
{\dissocliteral{}}
{\MProxyVdiff{\curlymap{\kw{}\ \v{}, \ova{\kwp{}\ \vp{}}}}
{\curlymap{\ova{\kwpp{}\ \textsf{t}'}}}
, \kw{}}
{\MProxyVdiff{\curlymap{\ova{\kwp{}\ \vp{}}}}
{\curlymap{\ova{\kwpp{}\ \textsf{t}'}}}}
{\emptyres{}}
\end{array}
\end{mathpar}
\caption{Lazy tracking extensions (\textcolor{red}{changes})}
\label{fig:infer:lazy}
\end{figure}
\Dsection{Automatic contracts with clojure.spec}
\label{infer:sec:spec-extension}
While we originally designed our tool to generate Typed Clojure annotations,
it also supports generating ``specs'' for clojure.spec, Clojure's runtime
verification system.
There are key similarities between Typed Clojure and clojure.spec,
such as extensive support for potentially-tagged keyword maps,
however spec features a global registry of names
via \clj{s/def} and
an explicit way to declare unions of maps with a common
dispatch key in \clj{s/multi-spec}.
These require differences in both type and name generation.
The following generated specs
correspond to the first \clj{Op} case
of \figref{infer:fig:cljs}
(lines \ref{infer:listing:cljs:Op:op:bindingStart}-\ref{infer:listing:cljs:Op:op:bindingEnd}).
\begin{minipage}{\linewidth}%added minipage for dissertation
\begin{cljlistingnumbered}
(defmulti op-multi-spec :op) ;dispatch on :op key
(defmethod op-multi-spec :binding ;match :binding
[_] ;s/keys matches keyword maps
(s/keys :req-un [::op ...] ;required keys
:opt-un [::column ...])) ;optional keys
(s/def ::op #{:js :let ...}) ;:op key maps to keywords
(s/def ::column int?) ;:column key maps to ints
; register ::Op as union dispatching on :op entry
(s/def ::Op (s/multi-spec op-multi-spec :op))
; emit's first argument :ast has spec ::Op
(s/fdef emit :args (s/cat :ast ::Op) :ret nil?)
\end{cljlistingnumbered}
\end{minipage}
%However, there are several non-trivial details to manage.
% specs have fundamental differences.
%As they are checked at runtime, specs are naturally more expressive than types.
%However, our generated specs are roughly the same as types in their precision.
%\paragraph{Registered Specs}
%Central to spec's design is a global registry of named specs.
%The form \clj{(s/def k s)} registers spec \clj{s} under name \clj{k}.
%To avoid conflicts, \emph{namespaced keywords} are used. The \clj{::a} reader syntax
%auto-resolves the keyword under the current namespace, like \clj{:this-ns/a}.
%
%\paragraph{HMaps}
%Heterogeneous map keyword specs are called ``entity maps'' in spec,
%and are defined as collections
%of registered specs with \clj{s/keys}, and has quite different
%syntax and semantics than many other runtime verification systems.
%The Typed Clojure type
%\begin{cljlisting}
%(defalias M (HMap :mandatory {:a Int, ::a Bool}
% :optional {:b Int, ::b Bool}))
%\end{cljlisting}
%is equivalent to
%\begin{cljlisting}
%(s/def :int/a integer?)
%(s/def ::a boolean?)
%(s/def :int/b integer?)
%(s/def ::b boolean?)
%(s/def ::M (s/keys :req-un [:int/a], :req [::a],
% :opt-un [:int/b], :opt [::b]))
%\end{cljlisting}
%The \clj{:req-un} and \clj{:opt-un} options
%(\clj{un}[qualified] keyword \clj{req}[uired] and \clj{opt}[ional] entries)
%take vectors of registered specs and, for
%each spec \clj{:ns/name}, uses the registered spec \clj{:ns/name}
%to verify values at \clj{:name}.
%
%The \clj{:req} and \clj{:opt} options
%(qualified keyword \clj{req}[uired] and \clj{opt}[ional] entries)
%take vectors of registered specs and, for
%each spec \clj{:ns/name}, uses the registered spec \clj{:ns/name}
%to verify values at \clj{:ns/name}.
%
%Spec has several other subtleties we must account for, but omit
%for brevity.
%\paragraph{Tagged maps}
%
%\paragraph{Functions}