...
[bitonic-mengthesis.git] / thesis.lagda
1 \documentclass[report]{article}
2
3 %% Narrow margins
4 % \usepackage{fullpage}
5
6 %% Bibtex
7 \usepackage{natbib}
8
9 %% Links
10 \usepackage{hyperref}
11
12 %% Frames
13 \usepackage{framed}
14
15 %% Symbols
16 \usepackage[fleqn]{amsmath}
17
18 %% Proof trees
19 \usepackage{bussproofs}
20
21 %% Diagrams
22 \usepackage[all]{xy}
23
24 %% -----------------------------------------------------------------------------
25 %% Commands for Agda
26 \usepackage[english]{babel}
27 \usepackage[conor]{agda}
28 \renewcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathrm{\underline{#1}}}}
29 \renewcommand{\AgdaFunction}[1]{\textbf{\textcolor{AgdaFunction}{#1}}}
30 \renewcommand{\AgdaField}{\AgdaFunction}
31 \definecolor{AgdaBound} {HTML}{000000}
32 \definecolor{AgdaHole} {HTML} {FFFF33}
33
34 \DeclareUnicodeCharacter{9665}{\ensuremath{\lhd}}
35 \DeclareUnicodeCharacter{964}{\ensuremath{\tau}}
36 \DeclareUnicodeCharacter{963}{\ensuremath{\sigma}}
37 \DeclareUnicodeCharacter{915}{\ensuremath{\Gamma}}
38 \DeclareUnicodeCharacter{8799}{\ensuremath{\stackrel{?}{=}}}
39
40
41 %% -----------------------------------------------------------------------------
42 %% Commands
43
44 \newcommand{\mysyn}{\AgdaKeyword}
45 \newcommand{\mytyc}{\AgdaDatatype}
46 \newcommand{\mydc}{\AgdaInductiveConstructor}
47 \newcommand{\myfld}{\AgdaField}
48 \newcommand{\myfun}{\AgdaFunction}
49 % TODO make this use AgdaBound
50 \newcommand{\myb}[1]{\ensuremath{#1}}
51 \newcommand{\myfield}{\AgdaField}
52 \newcommand{\myind}{\AgdaIndent}
53 \newcommand{\mykant}{\textsc{Kant}}
54 \newcommand{\mysynel}[1]{#1}
55 \newcommand{\mytmsyn}{\mysynel{term}}
56 \newcommand{\mysp}{\ }
57 % TODO \mathbin or \mathre here?
58 \newcommand{\myabs}[2]{\mydc{$\lambda$} #1 \mathrel{\mydc{$\mapsto$}} #2}
59 \newcommand{\myappsp}{\hspace{0.07cm}}
60 \newcommand{\myapp}[2]{#1 \myappsp #2}
61 \newcommand{\mysynsep}{\ \ |\ \ }
62
63 \FrameSep0.2cm
64 \newcommand{\mydesc}[3]{
65   \hfill \textbf{#1} $#2$
66   \vspace{-0.2cm}
67   \begin{framed}
68     #3
69   \end{framed}
70 }
71
72 % TODO is \mathbin the correct thing for arrow and times?
73
74 \newcommand{\mytmt}{\myb{T}}
75 \newcommand{\mytmm}{\myb{M}}
76 \newcommand{\mytmn}{\myb{N}}
77 \newcommand{\myred}{\leadsto}
78 \newcommand{\mysub}[3]{#1[#2 \mapsto #3]}
79 \newcommand{\mytysyn}{\mysynel{type}}
80 \newcommand{\mybasetys}{K}
81 % TODO change this name
82 \newcommand{\mybasety}[1]{B_{#1}}
83 \newcommand{\mytya}{\myb{A}}
84 \newcommand{\mytyb}{\myb{B}}
85 \newcommand{\mytycc}{\myb{C}}
86 \newcommand{\myarr}{\mathrel{\textcolor{AgdaDatatype}{\to}}}
87 \newcommand{\myprod}{\mathrel{\textcolor{AgdaDatatype}{\times}}}
88 \newcommand{\myctx}{\Gamma}
89 \newcommand{\myvalid}[1]{#1 \vdash \underline{\mathrm{valid}}}
90 \newcommand{\myjudd}[3]{#1 \vdash #2 : #3}
91 \newcommand{\myjud}[2]{\myjudd{\myctx}{#1}{#2}}
92 % TODO \mathbin or \mathrel here?
93 \newcommand{\myabss}[3]{\mydc{$\lambda$} #1 {:} #2 \mathrel{\mydc{$\mapsto$}} #3}
94 \newcommand{\mytt}{\mydc{tt}}
95 \newcommand{\myunit}{\mytyc{$\top$}}
96 \newcommand{\mypair}[2]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #2\mathclose{\mydc{$\rangle$}}}
97 \newcommand{\myfst}{\myfld{fst}}
98 \newcommand{\mysnd}{\myfld{snd}}
99 \newcommand{\myconst}{\myb{c}}
100 \newcommand{\myemptyctx}{\cdot}
101 \newcommand{\myhole}{\AgdaHole}
102 \newcommand{\myfix}[3]{\mysyn{fix} \myappsp #1 {:} #2 \mapsto #3}
103 \newcommand{\mysum}{\mathbin{\textcolor{AgdaDatatype}{+}}}
104 \newcommand{\myleft}[1]{\mydc{left}_{#1}}
105 \newcommand{\myright}[1]{\mydc{right}_{#1}}
106 \newcommand{\myempty}{\mytyc{$\bot$}}
107 \newcommand{\mycase}[2]{\mathopen{\myfun{[}}#1\mathpunct{\myfun{,}} #2 \mathclose{\myfun{]}}}
108 \newcommand{\myabsurd}[1]{\myfun{absurd}_{#1}}
109 \newcommand{\myarg}{\_}
110 \newcommand{\myderivsp}{\vspace{0.3cm}}
111 \newcommand{\mytyp}{\mytyc{Type}}
112 \newcommand{\myneg}{\myfun{$\neg$}}
113 \newcommand{\myar}{\,}
114 \newcommand{\mybool}{\mytyc{Bool}}
115 \newcommand{\mynat}{\mytyc{$\mathbb{N}$}}
116 \newcommand{\myrat}{\mytyc{$\mathbb{R}$}}
117 \newcommand{\myite}[3]{\mysyn{if}\,#1\,\mysyn{then}\,#2\,\mysyn{else}\,#3}
118 \newcommand{\myfora}[3]{(#1 {:} #2) \myarr #3}
119 \newcommand{\myexi}[3]{(#1 {:} #2) \mysum #3}
120
121 %% -----------------------------------------------------------------------------
122
123 \title{\mykant: Implementing Observational Equality}
124 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
125 \date{June 2013}
126
127 \begin{document}
128
129 \iffalse
130 \begin{code}
131 module thesis where
132 \end{code}
133 \fi
134
135 \maketitle
136
137 \begin{abstract}
138   The marriage between programming and logic has been a very fertile one.  In
139   particular, since the simply typed lambda calculus (STLC), a number of type
140   systems have been devised with increasing expressive power.
141
142   Section \ref{sec:types} will give a very brief overview of STLC, and then
143   illustrate how it can be interpreted as a natural deduction system.  Section
144   \ref{sec:itt} will introduce Inutitionistic Type Theory (ITT), which expands
145   on this concept, employing a more expressive logic.  The exposition is quite
146   dense since there is a lot of material to cover; for a more complete treatment
147   of the material the reader can refer to \citep{Thompson1991, Pierce2002}.
148   Section \ref{sec:equality} will explain why equality has always been a tricky
149   business in these theories, and talk about the various attempts that have been
150   made to make the situation better.  One interesting development has recently
151   emerged: Observational Type theory.
152
153   Section \ref{sec:practical} will describe common extensions found in the
154   systems currently in use.  Finally, section \ref{sec:kant} will describe a
155   system developed by the author that implements a core calculus based on the
156   principles described.
157 \end{abstract}
158
159 \tableofcontents
160
161 \section{Simple and not-so-simple types}
162 \label{sec:types}
163
164 \subsection{The untyped $\lambda$-calculus}
165
166 Along with Turing's machines, the earliest attempts to formalise computation
167 lead to the $\lambda$-calculus \citep{Church1936}.  This early programming
168 language encodes computation with a minimal syntax and no `data' in the
169 traditional sense, but just functions.  Here we give a brief overview of the
170 language, which will give the chance to introduce concepts central to the
171 analysis of all the following calculi.  The exposition follows the one found in
172 chapter 5 of \cite{Queinnec2003}.
173
174 The syntax of $\lambda$-terms consists of three things: variables, abstractions,
175 and applications:
176
177 \mydesc{syntax}{ }{
178   $
179   \begin{array}{r@{\ }c@{\ }l}
180     \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \\
181     x          & \in & \text{Some enumerable set of symbols}
182   \end{array}
183   $
184 }
185
186 Through this text, I will use $\mytmt$, $\mytmm$, $\mytmn$ to indicate a generic
187 term, and $x$, $y$ to refer to variables.  Parenthesis will be omitted in the
188 usual way: $\myapp{\myapp{\mytmt}{\mytmm}}{\mytmn} =
189 \myapp{(\myapp{\mytmt}{\mytmm})}{\mytmn}$.
190
191 Abstractions roughly corresponds to functions, and their semantics is more
192 formally explained by the $\beta$-reduction rule:
193
194 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
195   $
196   \begin{array}{l}
197     \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{x}{\mytmn}\text{, where} \\
198     \myind{1}
199     \begin{array}{l@{\ }c@{\ }l}
200       \mysub{x}{x}{\mytmn} & = & \mytmn \\
201       \mysub{y}{x}{\mytmn} & = & y\text{, with } x \neq y \\
202       \mysub{\myapp{\mytmt}{\mytmm}}{x}{\mytmn} & = & (\myapp{\mysub{\mytmt}{x}{\mytmn}}{\mysub{\mytmm}{x}{\mytmn}}) \\
203       \mysub{(\myabs{x}{\mytmm})}{x}{\mytmn} & = & \myabs{x}{\mytmm} \\
204       \mysub{(\myabs{y}{\mytmm})}{x}{\mytmn} & = & \myabs{z}{\mysub{\mysub{\mytmm}{y}{z}}{x}{\mytmn}}, \\
205       \multicolumn{3}{l}{\myind{1} \text{with $x \neq y$ and $z$ not free in $\myapp{\mytmm}{\mytmn}$}}
206     \end{array}
207   \end{array}
208   $
209 }
210
211 The care required during substituting variables for terms is required to avoid
212 name capturing.  We will use substitution in the future for other name-binding
213 constructs assuming similar precautions.
214
215 These few elements are of remarkable expressiveness, and in fact Turing
216 complete.  As a corollary, we must be able to devise a term that reduces forever
217 (`loops' in imperative terms):
218 \[
219   (\myapp{\omega}{\omega}) \myred (\myapp{\omega}{\omega}) \myred \dots\text{, with $\omega = \myabs{x}{\myapp{x}{x}}$}
220 \]
221
222 A \emph{redex} is a term that can be reduced.  In the untyped $\lambda$-calculus
223 this will be the case for an application in which the first term is an
224 abstraction, but in general we call aterm reducible if it appears to the left of
225 a reduction rule.  When a term contains no redexes it's said to be in
226 \emph{normal form}.  Given the observation above, not all terms reduce to a
227 normal forms: we call the ones that do \emph{normalising}, and the ones that
228 don't \emph{non-normalising}.
229
230 The reduction rule presented is not syntax directed, but \emph{evaluation
231   strategies} can be employed to reduce term systematically. Common evaluation
232 strategies include \emph{call by value} (or \emph{strict}), where arguments of
233 abstractions are reduced before being applied to the abstraction; and conversely
234 \emph{call by name} (or \emph{lazy}), where we reduce only when we need to do so
235 to proceed---in other words when we have an application where the function is
236 still not a $\lambda$. In both these reduction strategies we never reduce under
237 an abstraction: for this reason a weaker form of normalisation is used, where
238 both abstractions and normal forms are said to be in \emph{weak head normal
239   form}.
240
241 \subsection{The simply typed $\lambda$-calculus}
242
243 A convenient way to `discipline' and reason about $\lambda$-terms is to assign
244 \emph{types} to them, and then check that the terms that we are forming make
245 sense given our typing rules \citep{Curry1934}.  The first most basic instance
246 of this idea takes the name of \emph{simply typed $\lambda$ calculus}.
247
248 Our types contain a set of \emph{type variables} $\Phi$, which might correspond
249 to some `primitive' types; and $\myarr$, the type former for `arrow' types, the
250 types of functions.  The language is explicitly typed: when we bring a variable
251 into scope with an abstraction, we explicitly declare its type. $\mytya$,
252 $\mytyb$, $\mytycc$, will be used to refer to a generic type.  Reduction is
253 unchanged from the untyped $\lambda$-calculus.
254
255 \mydesc{syntax}{ }{
256   $
257   \begin{array}{r@{\ }c@{\ }l}
258     \mytmsyn   & ::= & \myb{x} \mysynsep \myabss{\myb{x}}{\mytysyn}{\mytmsyn} \mysynsep
259                        (\myapp{\mytmsyn}{\mytmsyn}) \\
260     \mytysyn   & ::= & \myb{\phi} \mysynsep \mytysyn \myarr \mytysyn  \mysynsep \\
261     \myb{x}    & \in & \text{Some enumerable set of symbols} \\
262     \myb{\phi} & \in & \Phi
263   \end{array}
264   $
265 }
266
267 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
268   \centering{
269     \begin{tabular}{ccc}
270       \AxiomC{$\myctx(x) = A$}
271       \UnaryInfC{$\myjud{\myb{x}}{A}$}
272       \DisplayProof
273       &
274       \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
275       \UnaryInfC{$\myjud{\myabss{x}{A}{\mytmt}}{\mytyb}$}
276       \DisplayProof
277       &
278       \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
279       \AxiomC{$\myjud{\mytmn}{\mytya}$}
280       \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
281       \DisplayProof
282     \end{tabular}
283   }
284 }
285
286 In the typing rules, a context $\myctx$ is used to store the types of bound
287 variables: $\myctx; \myb{x} : \mytya$ adds a variable to the context and
288 $\myctx(x)$ returns the type of the rightmost occurrence of $x$.
289
290 This typing system takes the name of `simply typed lambda calculus' (STLC), and
291 enjoys a number of properties.  Two of them are expected in most type systems
292 \citep{Pierce2002}:
293 \begin{description}
294 \item[Progress] A well-typed term is not stuck---it is either a variable, or its
295   constructor does not appear on the left of the $\myred$ relation (currently
296   only $\lambda$), or it can take a step according to the evaluation rules.
297 \item[Preservation] If a well-typed term takes a step of evaluation, then the
298   resulting term is also well-typed, and preserves the previous type.
299 \end{description}
300
301 However, STLC buys us much more: every well-typed term is normalising
302 \citep{Tait1967}.  It is easy to see that we can't fill the blanks if we want to
303 give types to the non-normalising term shown before:
304 \begin{equation*}
305   \myapp{(\myabss{x}{\myhole{?}}{\myapp{x}{x}})}{(\myabss{x}{\myhole{?}}{\myapp{x}{x}})}
306 \end{equation*}
307
308 This makes the STLC Turing incomplete.  We can recover the ability to loop by
309 adding a combinator that recurses:
310
311 % TODO make this more compact
312
313 \mydesc{syntax}{ } {
314   $ \mytmsyn ::= \dotsb \mysynsep \myfix{x}{\mytysyn}{\mytmsyn} $
315 }
316
317 \mydesc{typing:}{ } {
318   \AxiomC{$\myjudd{\myctx; x : \mytya}{\mytmt}{\mytya}$}
319   \UnaryInfC{$\myjud{\myfix{x}{\mytya}{\mytmt}}{\mytya}$}
320   \DisplayProof
321 }
322
323 \mydesc{reduction:}{ }{
324   $ \myfix{x}{\mytya}{\mytmt} \myred \mysub{\mytmt}{x}{(\myfix{x}{\mytya}{\mytmt})}$
325 }
326
327 This will deprive us of normalisation, which is a particularly bad thing if we
328 want to use the STLC as described in the next section.
329
330 \subsection{The Curry-Howard correspondence}
331
332 It turns out that the STLC can be seen a natural deduction system for
333 intuitionistic propositional logic.  Terms are proofs, and their types are the
334 propositions they prove.  This remarkable fact is known as the Curry-Howard
335 correspondence, or isomorphism.
336
337 The arrow ($\myarr$) type corresponds to implication.  If we wish to prove that
338 that $(\mytya \myarr \mytyb) \myarr (\mytyb \myarr \mytycc) \myarr (\mytya
339 \myarr \mytycc)$, all we need to do is to devise a $\lambda$-term that has the
340 correct type:
341 \[
342   \myabss{f}{(\mytya \myarr \mytyb)}{\myabss{g}{(\mytyb \myarr \mytycc)}{\myabss{x}{\mytya}{\myapp{g}{(\myapp{f}{x})}}}}
343 \]
344 That is, function composition.  We can extend our bare lambda calculus with
345 useful types to represent other logical constructs.
346
347 \mydesc{syntax}{ }{
348   $
349   \begin{array}{r@{\ }c@{\ }l}
350     \mytmsyn & ::= & \dots \\
351              &  |  & \mytt \mysynsep \myapp{\myabsurd{\mytysyn}}{\mytmsyn} \\
352              &  |  & \myapp{\myleft{\mytysyn}}{\mytmsyn} \mysynsep
353                      \myapp{\myright{\mytysyn}}{\mytmsyn} \mysynsep
354                      \myapp{\mycase{\mytmsyn}{\mytmsyn}}{\mytmsyn} \\
355              &  |  & \mypair{\mytmsyn}{\mytmsyn} \mysynsep
356                      \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
357     \mytysyn & ::= & \dots \mysynsep \myunit \mysynsep \myempty \mysynsep \mytmsyn \mysum \mytmsyn \mysynsep \mytysyn \myprod \mytysyn
358   \end{array}
359   $
360 }
361
362 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
363   \centering{
364     \begin{tabular}{cc}
365       $
366       \begin{array}{l@{ }l@{\ }c@{\ }l}
367         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myleft{\mytya} &}{\mytmt})} & \myred &
368           \myapp{\mytmm}{\mytmt} \\
369         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myright{\mytya} &}{\mytmt})} & \myred &
370           \myapp{\mytmn}{\mytmt}
371       \end{array}
372       $
373       &
374       $
375       \begin{array}{l@{ }l@{\ }c@{\ }l}
376         \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
377         \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
378       \end{array}
379       $
380     \end{tabular}
381   }
382 }
383
384 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
385   \centering{
386     \begin{tabular}{cc}
387       \AxiomC{}
388       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
389       \DisplayProof
390       &
391       \AxiomC{$\myjud{\mytmt}{\myempty}$}
392       \UnaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
393       \DisplayProof
394     \end{tabular}
395   }
396   \myderivsp
397   \centering{
398     \begin{tabular}{cc}
399       \AxiomC{$\myjud{\mytmt}{\mytya}$}
400       \UnaryInfC{$\myjud{\myapp{\myleft{\mytyb}}{\mytmt}}{\mytya \mysum \mytyb}$}
401       \DisplayProof
402       &
403       \AxiomC{$\myjud{\mytmt}{\mytyb}$}
404       \UnaryInfC{$\myjud{\myapp{\myright{\mytya}}{\mytmt}}{\mytya \mysum \mytyb}$}
405       \DisplayProof
406
407     \end{tabular}
408   }
409   \myderivsp
410   \centering{
411     \begin{tabular}{cc}
412       \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
413       \AxiomC{$\myjud{\mytmn}{\mytya \myarr \mytycc}$}
414       \AxiomC{$\myjud{\mytmt}{\mytya \mysum \mytyb}$}
415       \TrinaryInfC{$\myjud{\myapp{\mycase{\mytmm}{\mytmn}}{\mytmt}}{\mytycc}$}
416       \DisplayProof
417     \end{tabular}
418   }
419   \myderivsp
420   \centering{
421     \begin{tabular}{ccc}
422       \AxiomC{$\myjud{\mytmm}{\mytya}$}
423       \AxiomC{$\myjud{\mytmn}{\mytyb}$}
424       \BinaryInfC{$\myjud{\mypair{\mytmm}{\mytmn}}{\mytya \myprod \mytyb}$}
425       \DisplayProof
426       &
427       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
428       \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
429       \DisplayProof
430       &
431       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
432       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
433       \DisplayProof
434     \end{tabular}
435   }
436 }
437
438 Tagged unions (or sums, or coproducts---$\mysum$ here, \texttt{Either} in
439 Haskell) correspond to disjunctions, and dually tuples (or pairs, or
440 products---$\myprod$ here, tuples in Haskell) correspond to conjunctions.  This
441 is apparent looking at the ways to construct and destruct the values inhabiting
442 those types: for $\mysum$ $\myleft{ }$ and $\myright{ }$ correspond to $\vee$
443 introduction, and $\mycase{\_}{\_}$ to $\vee$ elimination; for $\myprod$
444 $\mypair{\_}{\_}$ corresponds to $\wedge$ introduction, $\myfst$ and $\mysnd$ to
445 $\wedge$ elimination.
446
447 The trivial type $\myunit$ corresponds to the logical $\top$, and dually
448 $\myempty$ corresponds to the logical $\bot$.  $\myunit$ has one introduction
449 rule ($\mytt$), and thus one inhabitant; and no eliminators.  $\myempty$ has no
450 introduction rules, and thus no inhabitants; and one eliminator ($\myabsurd{
451 }$), corresponding to the logical \emph{ex falso quodlibet}.  Note that in the
452 constructors for the sums and the destructor for $\myempty$ we need to include
453 some type information to keep type checking decidable.
454
455 As in logic, if we want to keep our system consistent, we must make sure that no
456 closed terms (in other words terms not under a $\lambda$) inhabit $\myempty$.
457 The variant of STLC presented here is indeed consistent, a result that follows
458 from the fact that it is normalising. % TODO explain
459 Going back to our $\myfix{ }{ }{ }$ combinator, it is easy to see how it breaks
460 our desire for consistency.  The following term works for every type $\mytya$,
461 including bottom:
462 \[
463 (\myfix{x}{\mytya}{x}) : \mytya
464 \]
465
466 With these rules, our STLC now looks remarkably similar in power and use to the
467 natural deduction we already know.  $\myneg \mytya$ can be expressed as $\mytya
468 \myarr \myempty$.  However, there is an important omission: there is no term of
469 the type $\mytya \mysum \myneg \mytya$ (excluded middle), or equivalently
470 $\myneg \myneg \mytya \myarr \mytya$ (double negation), or indeed any term with
471 a type equivalent to those.
472
473 This has a considerable effect on our logic and it's no coincidence, since there
474 is no obvious computational behaviour for laws like the excluded middle.
475 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
476 and all the systems analysed will have this characteristic since they build on
477 the foundation of the STLC\footnote{There is research to give computational
478   behaviour to classical logic, but I will not touch those subjects.}.
479
480
481 \section{Intuitionistic Type Theory}
482 \label{sec:itt}
483
484 \subsection{Extending the STLC}
485
486 The STLC can be made more expressive in various ways.  \cite{Barendregt1991}
487 succinctly expressed geometrically how we can add expressively:
488
489 $$
490 \xymatrix@!0@=1.5cm{
491   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
492   & & \lambda C \ar@{-}[dd]
493   \\
494   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
495   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
496   \\
497   & \lambda\underline\omega \ar@{-}'[r][rr]
498   & & \lambda P\underline\omega
499   \\
500   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
501   & & \lambda P \ar@{-}[ur]
502 }
503 $$
504 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
505 3 dimensions:
506 \begin{description}
507 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
508   types in our type signatures.  For example, we can defined a polymorphic
509   identity function:
510   $\displaystyle
511   (\myabss{\mytya}{\mytyp}{\myabss{x}{A}{x}}) : (\mytya : \mytyp) \myarr \mytya \myarr \mytya
512   $.
513   The first and most famous instance of this idea has been System F.  This gives
514   us a form of polymorphism and has been wildly successful, also thanks to a
515   well known inference algorithm for a restricted version of System F known as
516   Hindley-Milner.  Languages like Haskell and SML are based on this discipline.
517 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
518   type operators.  For example we could define a function that given types $R$
519   and $\mytya$ forms the type that represents a value of type $\mytya$ in
520   continuation passing style: $\displaystyle(\myabss{A \myar R}{\mytyp}{(\mytya
521     \myarr R) \myarr R}) : \mytyp \myarr \mytyp \myarr \mytyp$.
522 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
523   types', give great expressive power.  For example, we can have values of whose
524   type depend on a boolean:
525   $\displaystyle(\myabss{x}{\mybool}{\myite{x}{\mynat}{\myrat}}) : \mybool
526   \myarr \mytyp$.
527 \end{description}
528
529 All the systems preserve the properties that make the STLC well behaved.  The
530 system we are going to focus on, Intuitionistic Type Theory, has all of the
531 above additions, and thus would sit where $\lambda{C}$ sits in the
532 `$\lambda$-cube'.
533
534 \subsection{A Bit of History}
535
536 Logic frameworks and programming languages based on type theory have a long
537 history.  Per Martin-L\"{o}f described the first version of his theory in 1971,
538 but then revised it since the original version was inconsistent due to its
539 impredicativity\footnote{In the early version there was only one universe
540   $\mytyp$ and $\mytyp : \mytyp$, see section \ref{sec:core-tt} for an
541   explanation on why this causes problems.}.  For this reason he gave a revised
542 and consistent definition later \citep{Martin-Lof1984}.
543
544 A related development is the polymorphic $\lambda$-calculus, and specifically
545 the previously mentioned System F, which was developed independently by Girard
546 and Reynolds.  An overview can be found in \citep{Reynolds1994}.  The surprising
547 fact is that while System F is impredicative it is still consistent and strongly
548 normalising.  \cite{Coquand1986} further extended this line of work with the
549 Calculus of Constructions (CoC).
550
551 \subsection{A core type theory}
552 \label{sec:core-tt}
553
554 The calculus I present follows the exposition in \citep{Thompson1991}, and is
555 quite close to the original formulation of predicative ITT as found in
556 \citep{Martin-Lof1984}.
557
558 \mydesc{syntax}{ }{
559   $
560   \begin{array}{r@{\ }c@{\ }l}
561     \mytmsyn & ::= & \myb{x} \\
562              &  |  & \myunit \mysynsep \mytt \\
563              &  |  & \myempty \mysynsep \myapp{\myabsurd{\mytmsyn}}{\mytmsyn} \\
564              &  |  & \myfora{x}{\mytmsyn}{\mytmsyn} \mysynsep
565                      \myabss{x}{\mytmsyn}{\mytmsyn} \\
566              &  |  & \myexi{x}{\mytmsyn}{\mytmsyn} \mysynsep
567                      \mypair{\mytmsyn}{\mytmsyn} \mysynsep \myapp{\myfst}{\mytmsyn}
568   \end{array}
569   $
570 }
571
572 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
573
574 }
575
576 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
577
578 }
579
580 \section{The struggle for equality}
581 \label{sec:equality}
582
583 \section{Extending ITT}
584 \label{sec:practical}
585
586 \section{\mykant}
587 \label{sec:kant}
588
589 \appendix
590
591 \section{Notation and syntax}
592
593 Syntax, derivation rules, and reduction rules, are enclosed in frames describing
594 the type of relation being established and the syntactic elements appearing,
595 for example
596
597 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
598   Typing derivations here.
599 }
600
601 In the languages presented I also highlight the syntax, following a uniform
602 color and font convention:
603
604 \begin{center}
605   \begin{tabular}{c | l}
606     $\mytyc{Sans}$  & Type constructors. \\
607     $\mydc{sans}$  & Data constructors. \\
608     % $\myfld{sans}$  & Field accessors (e.g. \myfld{fst} and \myfld{snd} for products). \\
609     $\mysyn{roman}$ & Keywords of the language. \\
610     $\myfun{roman}$ & Defined values and destructors. \\
611     $\myb{math}$    & Bound variables.
612   \end{tabular}
613 \end{center}
614
615 \section{Agda code}
616 \label{app:agda-code}
617
618 \subsection{ITT}
619
620 \begin{code}
621 module ITT where
622   open import Level
623
624   data ⊥ : Set where
625
626   absurd : ∀ {a} {A : Set a} → ⊥ → A
627   absurd ()
628
629   record ⊤ : Set where
630     constructor tt
631
632   record _×_ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
633     constructor _,_
634     field
635       fst  : A
636       snd  : B fst
637
638   data Bool : Set where
639     true false : Bool
640
641   if_then_else_ : ∀ {a} {P : Bool → Set a} (x : Bool) → P true → P false → P x
642   if true then x else _ = x
643   if false then _ else x = x
644
645   data W {s p} (S : Set s) (P : S → Set p) : Set (s ⊔ p) where
646     _◁_ : (s : S) → (P s → W S P) → W S P
647
648   rec : ∀ {a b} {S : Set a} {P : S → Set b}
649     (C : W S P → Set) →       -- some conclusion we hope holds
650     ((s : S) →                -- given a shape...
651      (f : P s → W S P) →      -- ...and a bunch of kids...
652      ((p : P s) → C (f p)) →  -- ...and C for each kid in the bunch...
653      C (s ◁ f)) →             -- ...does C hold for the node?
654     (x : W S P) →             -- If so, ...
655     C x                       -- ...C always holds.
656   rec C c (s ◁ f) = c s f (λ p → rec C c (f p))
657 \end{code}
658
659 \nocite{*}
660 \bibliographystyle{authordate1}
661 \bibliography{thesis}
662
663 \end{document}