-
Notifications
You must be signed in to change notification settings - Fork 0
/
parametricity.tex
192 lines (174 loc) · 4.83 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
\begin{frame}[fragile]
\frametitle{Let's talk about}
\begin{center}
Parametricity
\end{center}
\end{frame}
\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}