507dbf83e439e9141e77a6c13e2f679b5ff61e4a
[bitonic-mengthesis.git] / docs / background.tex
1 \documentclass[article, a4paper]{article}
2 \usepackage[T1]{fontenc}
3 \usepackage{fixltx2e}
4 \usepackage{enumitem}
5 \usepackage[normalem]{ulem}
6 \usepackage{graphicx}
7 \usepackage{subcaption}
8
9 %% -----------------------------------------------------------------------------
10 %% Fonts
11 %% \usepackage[osf]{libertine}
12 %% \usepackage{helvet}
13 %% \usepackage{lmodern}
14 %% \IfFileExists{microtype.sty}{\usepackage{microtype}}{}
15
16 %% -----------------------------------------------------------------------------
17 %% Diagrams
18 % \usepackage{tikz}
19 % \usetikzlibrary{positioning}
20 %% \usetikzlibrary{shapes}
21 %% \usetikzlibrary{arrows}
22 %% \usepackage{tikz-cd}
23 %% \usepackage{pgfplots}
24 \usepackage[all]{xy}
25
26 %% -----------------------------------------------------------------------------
27 %% Symbols
28 \usepackage{amssymb,amsmath}
29 \usepackage{wasysym}
30 \usepackage{turnstile}
31 \usepackage{centernot}
32
33 %% -----------------------------------------------------------------------------
34 %% Utils
35 \usepackage{bussproofs}
36 \usepackage{umoline}
37 \usepackage[export]{adjustbox}
38 \usepackage{multicol}
39
40 %% Numbering depth
41 %% \setcounter{secnumdepth}{0}
42
43 %% -----------------------------------------------------------------------------
44 % We will generate all images so they have a width \maxwidth. This means
45 % that they will get their normal width if they fit onto the page, but
46 % are scaled down if they would overflow the margins.
47 \makeatletter
48 \def\maxwidth{
49   \ifdim\Gin@nat@width>\linewidth\linewidth
50   \else\Gin@nat@width\fi
51 }
52 \makeatother
53
54 %% -----------------------------------------------------------------------------
55 %% Links
56 \usepackage[
57   setpagesize=false, % page size defined by xetex
58   unicode=false, % unicode breaks when used with xetex
59   xetex
60 ]{hyperref}
61
62 \hypersetup{
63   breaklinks=true,
64   bookmarks=true,
65   pdfauthor={Francesco Mazzoli <fm2209@ic.ac.uk>},
66   pdftitle={The Paths Towards Observational Equality},
67   colorlinks=false,
68   pdfborder={0 0 0}
69 }
70
71 % Make links footnotes instead of hotlinks:
72 \renewcommand{\href}[2]{#2\footnote{\url{#1}}}
73
74 % avoid problems with \sout in headers with hyperref:
75 \pdfstringdefDisableCommands{\renewcommand{\sout}{}}
76
77 \title{The Paths Towards Observational Equality}
78 \author{Francesco Mazzoli \url{<fm2209@ic.ac.uk>}}
79 \date{December 2012}
80
81 \begin{document}
82
83 \maketitle
84
85 The marriage between programming and logic has been a very fertile one.  In
86 particular, since the simply typed lambda calculus (STLC), a number of type
87 systems have been devised with increasing expressive power.
88
89 In the next sections I will give a very brief overview of STLC, and then
90 describe how to augment it to reach the theory I am interested in,
91 Inutitionistic Type Theory (ITT), also known as Martin-L\"{o}f Type Theory after
92 its inventor.
93
94 I will then explain why equality has been a tricky business in this theories,
95 and talk about the various attempts have been made.  One interesting development
96 has recently emerged: Observational Type theory.  I propose to explore the ways
97 to turn these ideas into useful practices for programming and theorem proving.
98
99 \section{Simple and not-so-simple types}
100
101 \subsection{Untyped $\lambda$-calculus}
102
103 Along with Turing's machines, the earliest attempts to formalise computation
104 lead to the $\lambda$-calculus.  This early programming language encodes
105 computation with a minimal sintax and most notably no ``data'' in the
106 traditional sense, but just functions.
107
108 The syntax of $\lambda$-terms consists of just three things: variables,
109 abstractions, and applications:
110
111 \newcommand{\appspace}{\hspace{0.07cm}}
112 \newcommand{\app}[2]{#1\appspace#2}
113 \newcommand{\abs}[2]{\lambda #1. #2}
114 \newcommand{\termt}{\mathrm{T}}
115 \newcommand{\termm}{\mathrm{M}}
116 \newcommand{\termn}{\mathrm{N}}
117 \newcommand{\termp}{\mathrm{P}}
118 \newcommand{\separ}{\ |\ }
119
120 \begin{eqnarray*}
121   \termt & ::= & x \separ (\abs{x}{\termt}) \separ (\app{\termt}{\termt}) \\
122        x & \in & \text{Some enumerable set of symbols, e.g.}\ \{x, y, z, \dots , x_1, x_2, \dots\}
123 \end{eqnarray*}
124
125 % I will omit parethesis in the usual manner. %TODO explain how
126
127 Intuitively, abstractions ($\abs{x}{\termt}$) introduce functions with a named
128 parameter ($x$), and applications ($\app{\termt}{\termm}$) apply a function
129 ($\termt$) to an argument ($\termm$).
130
131 The ``applying'' is more formally explained with a reduction rule:
132
133 \newcommand{\bred}{\to_{\beta}}
134
135 \begin{eqnarray*}
136   \app{(\abs{x}{\termt})}{\termm} & \bred & \termt[\termm / x] \\
137   \termt \bred \termm & \Rightarrow & \left \{
138     \begin{array}{l}
139       \app{\termt}{\termn} \bred \app{\termm}{\termn} \\
140       \app{\termn}{\termt} \bred \app{\termn}{\termm} \\
141       \abs{x}{\termt}      \bred \abs{x}{\termm}
142     \end{array}
143     \right.
144 \end{eqnarray*}
145
146 Where $\termt[\termm / x]$ expresses the operation that substitutes all
147 occurrences of $x$ with $\termm$ in $\termt$.
148
149 % % TODO put the trans closure
150
151 These few elements are of remarkable expressiveness, and in fact Turing
152 complete.  As a corollary, we must be able to devise a term that reduces forever
153 (``loops'' in imperative terms):
154 \begin{equation*}
155   \app{(\abs{x}{\app{x}{x}})}{(\abs{x}{\app{x}{x}})} \bred \app{(\abs{x}{\app{x}{x}})}{(\abs{x}{\app{x}{x}})} \bred \dots
156 \end{equation*}
157 Terms that can be reduced only a finite number of times (the non-looping ones)
158 are said to be \emph{normalising}, and the ``final'' term is called \emph{normal
159   form}.  These concepts (reduction and normal forms) will run through all the
160 material analysed.
161
162 \subsection{The simply typed $\lambda$-calculus}
163
164 \newcommand{\tya}{\mathrm{A}}
165 \newcommand{\tyb}{\mathrm{B}}
166 \newcommand{\tyc}{\mathrm{C}}
167
168 One way to ``discipline'' $\lambda$-terms is to assign \emph{types} to them, and
169 then check that the terms that we are forming make sense given our typing rules.
170
171 We wish to introduce rules of the form $\Gamma \vdash \termt : \tya$, which
172 reads ``in context $\Gamma$, term $\termt$ has type $\tya$''.
173
174 The syntax for types is as follows:
175
176 \newcommand{\tyarr}{\to}
177
178 \begin{equation*}
179   \tya ::= x \separ \tya \tyarr \tya
180 \end{equation*}
181
182 The $x$ represents all the primitive types that we might want to add to our
183 calculus, for example $\mathbb{N}$ or $\mathsf{Bool}$.
184
185 A context $\Gamma$ is a map from variables to types.  We use the notation
186 $\Gamma, x : \tya$ to augment it.  Note that, being a map, no variable can
187 appear twice as a subject in a context.
188
189 Predictably, $\tya \tyarr \tyb$ is the type of a function from $\tya$ to
190 $\tyb$.  We need to be able to decorate our abstractions with
191 types\footnote{Actually, we don't need to: computers can infer the right type
192   easily, but that is another story.}:
193 \begin{equation*}
194   \termt ::= \dots \separ (\abs{x : \tya}{\termt})
195 \end{equation*}
196 Now we are ready to give the typing judgements:
197
198 \begin{center}
199   \begin{prooftree}
200     \AxiomC{}
201     \UnaryInfC{$\Gamma, x : \tya \vdash x : \tya$}
202   \end{prooftree}
203   \begin{prooftree}
204     \AxiomC{$\Gamma, x : \tya \vdash \termt : \tyb$}
205     \UnaryInfC{$\Gamma \vdash \abs{x : \tya}{\termt} : \tya \tyarr \tyb$}
206   \end{prooftree}
207   \begin{prooftree}
208     \AxiomC{$\Gamma \vdash \termt : \tya \tyarr \tyb$}
209     \AxiomC{$\Gamma \vdash \termm : \tya$}
210     \BinaryInfC{$\Gamma \vdash \app{\termt}{\termm} : \tyb$}
211   \end{prooftree}
212 \end{center}
213
214 This typing system takes the name of ``simply typed lambda calculus'' (STLC),
215 and enjoys a number of properties.  Two of them are expected in most type
216 systems: %TODO add credit to pierce
217 \begin{description}
218   % TODO the definition of "stuck" thing is wrong
219 \item[Progress] A well-typed term is not stuck.  With stuck, we mean a compound
220   term (not a variable or a value) that cannot be reduced further.  In the raw
221   $\lambda$-calculus all we have is functions, but if we add other primitive
222   types and constructors it's easy to see how things can go bad - for example
223   trying to apply a boolean to something.
224 \item[Preservation] If a well-typed term takes a step of evaluation, then the
225   resulting term is also well typed.
226 \end{description}
227
228 However, STLC buys us much more: every well-typed term
229 is normalising.  It is easy to see that we can't fill the blanks if we want to
230 give types to the non-normalising term shown before:
231 \begin{equation*}
232   \app{(\abs{x : ?}{\app{x}{x}})}{(\abs{x : ?}{\app{x}{x}})}
233 \end{equation*}
234
235 \newcommand{\lcfix}[2]{\mathsf{fix} \appspace #1. #2}
236
237 This makes the STLC Turing incomplete.  We can recover the ability to loop by
238 adding a combinator that recurses:
239 \begin{equation*}
240   \termt ::= \dots \separ  \lcfix{x : \tya}{\termt}
241 \end{equation*}
242 \begin{equation*}
243   \lcfix{x : \tya}{\termt} \bred \termt[(\lcfix{x : \tya}{\termt}) / x]
244 \end{equation*}
245 \begin{center}
246   \begin{prooftree}
247     \AxiomC{$\Gamma,x : \tya \vdash \termt : \tya$}
248     \UnaryInfC{$\Gamma \vdash \lcfix{x : \tya}{\termt} : \tya$}
249   \end{prooftree}
250 \end{center}
251
252 However, we will keep STLC without such a facility. In the next section we shall
253 see why that is preferable for our needs.
254
255 \subsection{The Curry-Howard correspondence}
256
257 It turns out that the STLC can be seen a natural deduction system.  Terms are
258 proofs, and their types are the propositions they prove.  This remarkable fact
259 is known as the Curry-Howard isomorphism.
260
261 The ``arrow'' ($\to$) type corresponds to implication.  If we wished to
262 prove that $(\tya \tyarr \tyb) \tyarr (\tyb \tyarr \tyc) \tyarr (\tyc
263 \tyarr \tyc)$, all we need to do is to devise a $\lambda$-term that has the
264 correct type:
265 \begin{equation*}
266   \abs{f : (\tya \tyarr \tyb)}{\abs{g : (\tyb \tyarr \tyc)}{\abs{x : \tya}{\app{g}{(\app{f}{x})}}}}
267 \end{equation*}
268 That is, function composition.  We might want extend our bare lambda calculus
269 with a couple of terms to make our natural deduction more pleasant to use.  For
270 example, tagged unions (\texttt{Either} in Haskell) are disjunctions, and tuples
271 are conjunctions.  We also want to be able to express falsity, and that is done
272 by introducing a type inhabited by no terms.  If evidence of such a type is
273 presented, then we can derive any type, which expresses absurdity.
274
275 \newcommand{\lcinl}{\mathsf{inl}\appspace}
276 \newcommand{\lcinr}{\mathsf{inr}\appspace}
277 \newcommand{\lccase}[3]{\mathsf{case}\appspace#1\appspace#2\appspace#3}
278 \newcommand{\lcfst}{\mathsf{fst}\appspace}
279 \newcommand{\lcsnd}{\mathsf{snd}\appspace}
280 \newcommand{\orint}{\vee I_{1,2}}
281 \newcommand{\orintl}{\vee I_{1}}
282 \newcommand{\orintr}{\vee I_{2}}
283 \newcommand{\orel}{\vee E}
284 \newcommand{\andint}{\wedge I}
285 \newcommand{\andel}{\wedge E_{1,2}}
286 \newcommand{\botel}{\bot E}
287 \newcommand{\lcabsurd}{\mathsf{absurd}\appspace}
288
289 \begin{eqnarray*}
290   \termt & ::= & \dots \\
291          &  |  & \lcinl \termt \separ \lcinr \termt \separ \lccase{\termt}{\termt}{\termt} \\
292          &  |  & (\termt , \termt) \separ \lcfst \termt \separ \lcsnd \termt
293 \end{eqnarray*}
294 \begin{eqnarray*}
295   \lccase{(\lcinl \termt)}{\termm}{\termn} & \bred & \app{\termm}{\termt} \\
296   \lccase{(\lcinr \termt)}{\termm}{\termn} & \bred & \app{\termn}{\termt} \\
297   \lcfst (\termt , \termm)                 & \bred & \termt \\
298   \lcsnd (\termt , \termm)                 & \bred & \termm
299 \end{eqnarray*}
300 \begin{equation*}
301   \tya ::= \dots \separ \tya \vee \tya \separ \tya \wedge \tya \separ \bot
302 \end{equation*}
303 \begin{center}
304   \begin{prooftree}
305     \AxiomC{$\Gamma \vdash \termt : \tya$}
306     \RightLabel{$\orint$}
307     \UnaryInfC{$\Gamma \vdash \lcinl \termt : \tya \vee \tyb$}
308     \noLine
309     \UnaryInfC{$\Gamma \vdash \lcinr \termt : \tyb \vee \tya$}
310   \end{prooftree}
311   \begin{prooftree}
312     \AxiomC{$\Gamma \vdash \termt : \tya \vee \tyb$}
313     \AxiomC{$\Gamma \vdash \termm : \tya \tyarr \tyc$}
314     \AxiomC{$\Gamma \vdash \termn : \tyb \tyarr \tyc$}
315     \RightLabel{$\orel$}
316     \TrinaryInfC{$\Gamma \vdash \lccase{\termt}{\termm}{\termn} : \tyc$}
317   \end{prooftree}
318   \begin{prooftree}
319     \AxiomC{$\Gamma \vdash \termt : \tya$}
320     \AxiomC{$\Gamma \vdash \termm : \tyb$}
321     \RightLabel{$\andint$}
322     \BinaryInfC{$\Gamma \vdash (\tya , \tyb) : \tya \wedge \tyb$}
323   \end{prooftree}
324   \begin{prooftree}
325     \AxiomC{$\Gamma \vdash \termt : \tya \wedge \tyb$}
326     \RightLabel{$\andel$}
327     \UnaryInfC{$\Gamma \vdash \lcfst \termt : \tya$}
328     \noLine
329     \UnaryInfC{$\Gamma \vdash \lcsnd \termt : \tyb$}
330   \end{prooftree}
331   \begin{prooftree}
332     \AxiomC{$\Gamma \vdash \termt : \bot$}
333     \RightLabel{$\botel$}
334     \UnaryInfC{$\Gamma \vdash \lcabsurd \termt : \tya$}
335   \end{prooftree}
336 \end{center}
337
338 With these rules, our STLC now looks remarkably similar in power and use to the
339 natural deduction we already know.  $\neg A$ can be expressed as $A \tyarr
340 \bot$.  However, there is an important omission: there is no term of the type $A
341 \vee \neg A$ (excluded middle), or equivalently $\neg \neg A \tyarr A$ (double
342 negation).
343
344 This has a considerable effect on our logic and it's no coincidence, since there
345 is no obvious computational behaviour for laws like the excluded middle.
346 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
347 and all the systems analysed will have this characteristic since they build on
348 the foundation of the STLC.
349
350 \subsection{Extending the STLC}
351
352 \newcommand{\lctype}{\mathsf{Type}}
353 \newcommand{\lcite}[3]{\mathsf{if}\appspace#1\appspace\mathsf{then}\appspace#2\appspace\mathsf{else}\appspace#3}
354 \newcommand{\lcbool}{\mathsf{Bool}}
355
356 The STLC can be made more expressive in various ways.  Henk Barendregt
357 succinctly expressed geometrically how we can expand our type system:
358
359 \begin{equation*}
360 \xymatrix@!0@=1.5cm{
361   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
362   & & \lambda C \ar@{-}[dd]
363   \\
364   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
365   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
366   \\
367   & \lambda\underline\omega \ar@{-}'[r][rr]
368   & & \lambda P\underline\omega
369   \\
370   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
371   & & \lambda P \ar@{-}[ur]
372 }
373 \end{equation*}
374 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
375 3 dimensions:
376 \begin{description}
377 \item[Terms depending on types (towards $\lambda{2}$)] In other words, we can
378   quantify over types in our type signatures: $(\lambda A : \lctype. \lambda x :
379   A. x) : \forall A. A \to A$.  The first and most famous instance of this idea
380   has been System F.  This gives us a form of polymorphism and has been wildly
381   successful, also thanks to a well known inference algorithm for a restricted
382   version of System F known as Hindley-Milner.  Languages like Haskell and SML
383   are based on this discipline.
384 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] In other
385   words, we have type operators: $(\lambda A : \lctype. \lambda R : \lctype. (A \to R) \to R) : \lctype \to \lctype \to \lctype$.
386 \item[Types depending on terms (towards $\lambda{P}$)] Also known as ``dependent
387   types'', give great expressive power: $(\lambda x :
388   \lcbool. \lcite{x}{\mathbb{N}}{\mathbb{Q}}) : \lcbool \to \lctype$.
389 \end{description}
390
391 All the systems preserve the properties that make the STLC well behaved (some of
392 which I haven't mentioned yet).  The system we are going to focus on,
393 Intuitionistic Type Theory, has all of the above additions, and thus would sit
394 where $\lambda{C}$ sits in the ``$\lambda$-cube'' above.
395
396 \section{Intuitionistic Type Theory}
397
398 In this section I will describe 
399
400 \end{document}