-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparametricity.tex
268 lines (235 loc) · 6.52 KB
/
parametricity.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
\begin{frame}[fragile]
\frametitle{Consider the following Java function \ldots}
\begin{block}{}
\begin{lstlisting}[style=java]
boolean boolean2boolean(boolean b) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{center}
How many possible programs can be written that satisfy the type?
i.e. from the type, how much knowledge have we gained?
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{What about this Java function \ldots}
\begin{block}{}
\begin{lstlisting}[style=java]
String string2string(String s) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{center}
from the type, how much knowledge have we gained?
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{OK now this Java function \ldots}
\begin{block}{}
\begin{lstlisting}[style=java]
<A> A any2any(A a) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{center}
How many possible programs can be written that satisfy the type?
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Polymorphic values}
\begin{block}{By utilising \emph{polymorphic} values in a type \ldots}
\begin{center}
we have gained a \textbf{lot} of knowledge of our function's behaviour
In this case, we have obtained \emph{total} knowledge
\end{center}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Polymorphic values}
\begin{block}{Parametricity}
\begin{center}
This idea of using parametric polymorphism to determine a function's behaviour is called \emph{parametricity}
\end{center}
\end{block}
\end{frame}
\begin{frame}
\frametitle{What is Parametricity and Free Theorems}
\begin{block}{Philip Wadler \cite{wadler1989theorems} tells us:}
\begin{quotation}
Write down the definition of a polymorphic function on a piece of paper. Tell me its type, but be careful not to let me see the function's definition. I will tell you a theorem that the function satisfies.
The purpose of this paper is to explain the trick.
\end{quotation}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Try this Java function \ldots}
\begin{block}{}
\begin{lstlisting}[style=java]
List<String> strings2strings(List<String> s) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{center}
from the type, how much knowledge have we gained?
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Polymorphic values}
\begin{block}{This type has no polymorphic values}
\begin{lstlisting}[style=java]
List<String> strings2strings(List<String> x) {
// hidden from view
}
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Polymorphic values}
\begin{block}{Can we determine function behaviour?}
\begin{lstlisting}[style=java]
<T> List<T> anythings2anythings(List<T> x) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{block}{Theorem}
\textbf{every element in the resulting list, appears in the input list}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Polymorphic values}
\begin{block}{Some amount of function behaviour}
\begin{lstlisting}[style=java]
<T> List<T> anythings2anythings(List<T> x) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{block}{Theorem}
We have \textbf{some} amount of information, but not \textbf{total} information
Let's write an \emph{automated} test
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Polymorphic values and tests}
\begin{block}{Can we determine function behaviour?}
\begin{lstlisting}[style=java]
<T> List<T> anythings2anythings(List<T> x) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{block}{Tests}
\begin{lstlisting}[style=haskell]
prop_anythings2anythings1 :: Property
prop_anythings2anythings1 =
property $ do
x <- forAll alpha
anythings2anythings [x] == [x]
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Polymorphic values and tests}
\begin{block}{Can we determine function behaviour?}
\begin{lstlisting}[style=java]
<T> List<T> anythings2anythings(List<T> x) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{block}{Tests}
\begin{lstlisting}[style=haskell]
prop_anythings2anythings2 :: Property
prop_anythings2anythings2 =
property $ do
x <- forAll (list (linear 0 100) alpha)
y <- forAll (list (linear 0 100) alpha)
anythings2anythings (x ++ y) ==
anythings2anythings y ++ anythings2anythings x
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Types and tests}
\begin{block}{By this method, it becomes very explicit that \ldots}
\begin{itemize}
\item Types alone provide a \emph{proof} of a proposition
\item Polymorphic types provide \emph{additional theorems}
i.e. free theorems
\item Tests provide a \emph{failed negative proof} of a proposition
\item This outcome is the \emph{only} difference between types and tests
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Once-inhabitance}
\begin{block}{}
\begin{lstlisting}[style=haskell]
<T> T anything2anything(T x) {
// hidden from view
}
\end{lstlisting}
\end{block}
\begin{center}
This type is an example of \emph{once-inhabitance}
There is only one function with this type
\textbf{It is not possible to write tests for it \textemdash tests are redundant}
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Types and tests}
\begin{block}{But these are trivial examples}
What about more realistic examples?
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Types and tests}
\begin{block}{}
\begin{lstlisting}[style=haskell]
-- the type implies this function does no I/O
validateWebForm ::
f WebForm
-> f (Either WebFormErrors ValidatedWebForm)
-- this function may do I/O
submitWebForm ::
AppState
-> WebForm
-> IO (Response, AppState)
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Types and tests}
\begin{block}{}
\begin{lstlisting}[style=haskell]
-- idempotence
prop_submitWebForm :: Property
prop_submitWebForm =
property $ do
w <- forAll genWebForm
s <- forAll genAppState
(_, s1) <- submitWebForm s (submitWebForm s w)
(_, s2) <- submitWebForm s w
s1 == s2
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Types and tests}
\begin{block}{Types and tests \ldots}
\begin{itemize}
\item We use types \textbf{first}
\item Where types fall short, we use \textbf{automated tests}
\item Tests are written using the hedgehog\footnote{\tiny{hedgehog} \href{https://hackage.haskell.org/package/hedgehog}{\beamergotobutton{Link}}} library
\item Tests are deterministic
``works on my machine today''
$\rightarrow$
``works on all machines at all times''
\end{itemize}
\end{block}
\end{frame}